Create BitFieldArray

This commit is contained in:
Pokechu22 2021-02-16 22:49:30 -08:00
parent 1273c5e395
commit f697e17dd1
2 changed files with 595 additions and 0 deletions

View File

@ -33,6 +33,7 @@
#include <cstddef>
#include <fmt/format.h>
#include <iterator>
#include <limits>
#include <type_traits>
@ -202,3 +203,305 @@ struct fmt::formatter<BitField<position, bits, T, S>>
return m_formatter.format(bitfield.Value(), ctx);
}
};
// Language limitations require the following to make these formattable
// (formatter<BitFieldArray<position, bits, size, T>::Ref> is not legal)
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstRef;
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayRef;
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstIterator;
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayIterator;
#pragma pack(1)
template <std::size_t position, std::size_t bits, std::size_t size, typename T,
// StorageType is T for non-enum types and the underlying type of T if
// T is an enumeration. Note that T is wrapped within an enable_if in the
// former case to workaround compile errors which arise when using
// std::underlying_type<T>::type directly.
typename StorageType = typename std::conditional_t<
std::is_enum<T>::value, std::underlying_type<T>, std::enable_if<true, T>>::type>
struct BitFieldArray
{
using Ref = BitFieldArrayRef<position, bits, size, T, StorageType>;
using ConstRef = BitFieldArrayConstRef<position, bits, size, T, StorageType>;
using Iterator = BitFieldArrayIterator<position, bits, size, T, StorageType>;
using ConstIterator = BitFieldArrayConstIterator<position, bits, size, T, StorageType>;
private:
// This constructor might be considered ambiguous:
// Would it initialize the storage or just the bitfield?
// Hence, delete it. Use the assignment operator to set bitfield values!
BitFieldArray(T val) = delete;
public:
// Force default constructor to be created
// so that we can use this within unions
constexpr BitFieldArray() = default;
// Visual Studio (as of VS2017) considers BitField to not be trivially
// copyable if we delete this copy assignment operator.
// https://developercommunity.visualstudio.com/content/problem/101208/c-compiler-is-overly-strict-regarding-whether-a-cl.html
#ifndef _MSC_VER
// We explicitly delete the copy assignment operator here, because the
// default copy assignment would copy the full storage value, rather than
// just the bits relevant to this particular bit field.
// Ideally, we would just implement the copy assignment to copy only the
// relevant bits, but we're prevented from doing that because the savestate
// code expects that this class is trivially copyable.
BitFieldArray& operator=(const BitFieldArray&) = delete;
#endif
public:
constexpr std::size_t StartBit() const { return position; }
constexpr std::size_t NumBits() const { return bits; }
constexpr std::size_t Size() const { return size; }
constexpr std::size_t TotalNumBits() const { return bits * size; }
constexpr T Value(size_t index) const { return Value(std::is_signed<T>(), index); }
void SetValue(size_t index, T value)
{
const size_t pos = position + bits * index;
storage = (storage & ~GetElementMask(index)) |
((static_cast<StorageType>(value) << pos) & GetElementMask(index));
}
Ref operator[](size_t index) { return Ref(this, index); }
constexpr const ConstRef operator[](size_t index) const { return ConstRef(this, index); }
constexpr Iterator begin() { return Iterator(this, 0); }
constexpr Iterator end() { return Iterator(this, size); }
constexpr ConstIterator begin() const { return ConstIterator(this, 0); }
constexpr ConstIterator end() const { return ConstIterator(this, size); }
constexpr ConstIterator cbegin() const { return begin(); }
constexpr ConstIterator cend() const { return end(); }
private:
// Unsigned version of StorageType
using StorageTypeU = std::make_unsigned_t<StorageType>;
constexpr T Value(std::true_type, size_t index) const
{
const size_t pos = position + bits * index;
const size_t shift_amount = 8 * sizeof(StorageType) - bits;
return static_cast<T>((storage << (shift_amount - pos)) >> shift_amount);
}
constexpr T Value(std::false_type, size_t index) const
{
const size_t pos = position + bits * index;
return static_cast<T>((storage & GetElementMask(index)) >> pos);
}
static constexpr StorageType GetElementMask(size_t index)
{
const size_t pos = position + bits * index;
return (std::numeric_limits<StorageTypeU>::max() >> (8 * sizeof(StorageType) - bits)) << pos;
}
StorageType storage;
static_assert(bits * size + position <= 8 * sizeof(StorageType), "Bitfield array out of range");
static_assert(sizeof(T) <= sizeof(StorageType), "T must fit in StorageType");
// And, you know, just in case people specify something stupid like bits=position=0x80000000
static_assert(position < 8 * sizeof(StorageType), "Invalid position");
static_assert(bits <= 8 * sizeof(T), "Invalid number of bits");
static_assert(bits > 0, "Invalid number of bits");
static_assert(size <= 8 * sizeof(StorageType), "Invalid size");
static_assert(size > 0, "Invalid size");
};
#pragma pack()
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstRef
{
friend struct BitFieldArray<position, bits, size, T, S>;
friend class BitFieldArrayConstIterator<position, bits, size, T, S>;
public:
constexpr T Value() const { return m_array->Value(m_index); };
constexpr operator T() const { return Value(); }
private:
constexpr BitFieldArrayConstRef(const BitFieldArray<position, bits, size, T, S>* array,
size_t index)
: m_array(array), m_index(index)
{
}
const BitFieldArray<position, bits, size, T, S>* const m_array;
const size_t m_index;
};
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayRef
{
friend struct BitFieldArray<position, bits, size, T, S>;
friend class BitFieldArrayIterator<position, bits, size, T, S>;
public:
constexpr T Value() const { return m_array->Value(m_index); };
constexpr operator T() const { return Value(); }
T operator=(const BitFieldArrayRef<position, bits, size, T, S>& value) const
{
m_array->SetValue(m_index, value);
return value;
}
T operator=(T value) const
{
m_array->SetValue(m_index, value);
return value;
}
private:
constexpr BitFieldArrayRef(BitFieldArray<position, bits, size, T, S>* array, size_t index)
: m_array(array), m_index(index)
{
}
BitFieldArray<position, bits, size, T, S>* const m_array;
const size_t m_index;
};
// Satisfies LegacyOutputIterator / std::output_iterator.
// Does not satisfy LegacyInputIterator / std::input_iterator as std::output_iterator_tag does not
// extend std::input_iterator_tag.
// Does not satisfy LegacyForwardIterator / std::forward_iterator, as that requires use of real
// references instead of proxy objects.
// This iterator allows use of BitFieldArray in range-based for loops, and with fmt::join.
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayIterator
{
friend struct BitFieldArray<position, bits, size, T, S>;
public:
using iterator_category = std::output_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = void;
using reference = BitFieldArrayRef<position, bits, size, T, S>;
private:
constexpr BitFieldArrayIterator(BitFieldArray<position, bits, size, T, S>* array, size_t index)
: m_array(array), m_index(index)
{
}
public:
// Required by std::input_or_output_iterator
constexpr BitFieldArrayIterator() = default;
// Required by LegacyIterator
constexpr BitFieldArrayIterator(const BitFieldArrayIterator& other) = default;
// Required by LegacyIterator
BitFieldArrayIterator& operator=(const BitFieldArrayIterator& other) = default;
// Move constructor and assignment operators, explicitly defined for completeness
constexpr BitFieldArrayIterator(BitFieldArrayIterator&& other) = default;
BitFieldArrayIterator& operator=(BitFieldArrayIterator&& other) = default;
public:
BitFieldArrayIterator& operator++()
{
m_index++;
return *this;
}
BitFieldArrayIterator operator++(int)
{
BitFieldArrayIterator other(*this);
++*this;
return other;
}
constexpr reference operator*() const { return reference(m_array, m_index); }
constexpr bool operator==(BitFieldArrayIterator other) const { return m_index == other.m_index; }
constexpr bool operator!=(BitFieldArrayIterator other) const { return m_index != other.m_index; }
private:
BitFieldArray<position, bits, size, T, S>* m_array;
size_t m_index;
};
// Satisfies LegacyInputIterator / std::input_iterator.
// Does not satisfy LegacyForwardIterator / std::forward_iterator, as that requires use of real
// references instead of proxy objects.
// This iterator allows use of BitFieldArray in range-based for loops, and with fmt::join.
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstIterator
{
friend struct BitFieldArray<position, bits, size, T, S>;
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = void;
using reference = BitFieldArrayConstRef<position, bits, size, T, S>;
private:
constexpr BitFieldArrayConstIterator(const BitFieldArray<position, bits, size, T, S>* array,
size_t index)
: m_array(array), m_index(index)
{
}
public:
// Required by std::input_or_output_iterator
constexpr BitFieldArrayConstIterator() = default;
// Required by LegacyIterator
constexpr BitFieldArrayConstIterator(const BitFieldArrayConstIterator& other) = default;
// Required by LegacyIterator
BitFieldArrayConstIterator& operator=(const BitFieldArrayConstIterator& other) = default;
// Move constructor and assignment operators, explicitly defined for completeness
constexpr BitFieldArrayConstIterator(BitFieldArrayConstIterator&& other) = default;
BitFieldArrayConstIterator& operator=(BitFieldArrayConstIterator&& other) = default;
public:
BitFieldArrayConstIterator& operator++()
{
m_index++;
return *this;
}
BitFieldArrayConstIterator operator++(int)
{
BitFieldArrayConstIterator other(*this);
++*this;
return other;
}
constexpr reference operator*() const { return reference(m_array, m_index); }
constexpr bool operator==(BitFieldArrayConstIterator other) const
{
return m_index == other.m_index;
}
constexpr bool operator!=(BitFieldArrayConstIterator other) const
{
return m_index != other.m_index;
}
private:
const BitFieldArray<position, bits, size, T, S>* m_array;
size_t m_index;
};
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
struct fmt::formatter<BitFieldArrayRef<position, bits, size, T, S>>
{
fmt::formatter<T> m_formatter;
constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); }
template <typename FormatContext>
auto format(const BitFieldArrayRef<position, bits, size, T, S>& ref, FormatContext& ctx)
{
return m_formatter.format(ref.Value(), ctx);
}
};
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
struct fmt::formatter<BitFieldArrayConstRef<position, bits, size, T, S>>
{
fmt::formatter<T> m_formatter;
constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); }
template <typename FormatContext>
auto format(const BitFieldArrayConstRef<position, bits, size, T, S>& ref, FormatContext& ctx)
{
return m_formatter.format(ref.Value(), ctx);
}
};

View File

@ -251,3 +251,295 @@ TEST(BitField, Fmt)
EXPECT_EQ(fmt::format("{:s}", object.enum_2), fmt::format("{:s}", object.enum_2.Value()));
}
}
union TestUnion2
{
u32 hex;
BitField<0, 2, u32> a;
BitField<2, 2, u32> b;
BitField<4, 2, u32> c;
BitFieldArray<0, 2, 3, u32> arr;
};
TEST(BitFieldArray, Unsigned)
{
TestUnion2 object;
object.hex = 0;
const TestUnion2& objectc = object;
for (u32 value : object.arr)
{
EXPECT_EQ(value, 0u);
}
object.arr[0] = 2;
EXPECT_EQ(object.arr[0], 2u);
EXPECT_EQ(object.a, 2u);
EXPECT_EQ(object.hex, 0b00'00'10u);
object.arr[1] = 3;
EXPECT_EQ(object.arr[1], 3u);
EXPECT_EQ(object.b, 3u);
EXPECT_EQ(object.hex, 0b00'11'10u);
object.arr[2] = object.arr[1];
EXPECT_EQ(object.arr[2], 3u);
EXPECT_EQ(object.c, 3u);
EXPECT_EQ(object.hex, 0b11'11'10u);
object.arr[1] = objectc.arr[0];
EXPECT_EQ(object.arr[1], 2u);
EXPECT_EQ(object.b, 2u);
EXPECT_EQ(object.hex, 0b11'10'10u);
for (auto ref : object.arr)
{
ref = 1;
}
EXPECT_EQ(object.a, 1u);
EXPECT_EQ(object.b, 1u);
EXPECT_EQ(object.c, 1u);
EXPECT_EQ(object.hex, 0b01'01'01u);
std::fill_n(object.arr.begin(), object.arr.Size(), 3);
EXPECT_EQ(object.arr[0], 3u);
EXPECT_EQ(object.arr[1], 3u);
EXPECT_EQ(object.arr[2], 3u);
EXPECT_EQ(object.hex, 0b11'11'11u);
for (u32 i = 0; i < object.arr.Size(); i++)
{
object.arr[i] = i;
}
EXPECT_EQ(object.hex, 0b10'01'00u);
EXPECT_EQ(objectc.arr[0], 0u);
EXPECT_EQ(objectc.arr[1], 1u);
EXPECT_EQ(objectc.arr[2], 2u);
u32 counter = 0;
for (u32 value : objectc.arr)
{
EXPECT_EQ(value, counter);
counter++;
}
EXPECT_EQ("[0, 1, 2]", fmt::format("[{}]", fmt::join(object.arr, ", ")));
EXPECT_EQ("[0b00, 0b01, 0b10]", fmt::format("[{:#04b}]", fmt::join(object.arr, ", ")));
}
union TestUnion3
{
s32 hex;
BitField<5, 2, s32> a;
BitField<7, 2, s32> b;
BitField<9, 2, s32> c;
BitFieldArray<5, 2, 3, s32> arr;
};
TEST(BitFieldArray, Signed)
{
TestUnion3 object;
object.hex = 0;
const TestUnion3& objectc = object;
for (s32 value : object.arr)
{
EXPECT_EQ(value, 0);
}
object.arr[0] = -2;
EXPECT_EQ(object.arr[0], -2);
EXPECT_EQ(object.a, -2);
EXPECT_EQ(object.hex, 0b00'00'10'00000);
object.arr[1] = -1;
EXPECT_EQ(object.arr[1], -1);
EXPECT_EQ(object.b, -1);
EXPECT_EQ(object.hex, 0b00'11'10'00000);
object.arr[2] = object.arr[1];
EXPECT_EQ(object.arr[2], -1);
EXPECT_EQ(object.c, -1);
EXPECT_EQ(object.hex, 0b11'11'10'00000);
object.arr[1] = objectc.arr[0];
EXPECT_EQ(object.arr[1], -2);
EXPECT_EQ(object.b, -2);
EXPECT_EQ(object.hex, 0b11'10'10'00000);
for (auto ref : object.arr)
{
ref = 1;
}
EXPECT_EQ(object.a, 1);
EXPECT_EQ(object.b, 1);
EXPECT_EQ(object.c, 1);
EXPECT_EQ(object.hex, 0b01'01'01'00000);
std::fill_n(object.arr.begin(), object.arr.Size(), -1);
EXPECT_EQ(object.arr[0], -1);
EXPECT_EQ(object.arr[1], -1);
EXPECT_EQ(object.arr[2], -1);
EXPECT_EQ(object.hex, 0b11'11'11'00000);
for (u32 i = 0; i < object.arr.Size(); i++)
{
object.arr[i] = i;
}
EXPECT_EQ(object.hex, 0b10'01'00'00000);
EXPECT_EQ(objectc.arr[0], 0);
EXPECT_EQ(objectc.arr[1], 1);
EXPECT_EQ(objectc.arr[2], -2);
u32 counter = 0;
for (s32 value : objectc.arr)
{
EXPECT_EQ(value, object.arr[counter++]);
}
EXPECT_EQ("[0, 1, -2]", fmt::format("[{}]", fmt::join(object.arr, ", ")));
EXPECT_EQ("[+0b00, +0b01, -0b10]", fmt::format("[{:+#05b}]", fmt::join(object.arr, ", ")));
}
union TestUnion4
{
u64 hex;
BitField<30, 2, TestEnum> a;
BitField<32, 2, TestEnum> b;
BitField<34, 2, TestEnum> c;
BitField<36, 2, TestEnum> d;
BitFieldArray<30, 2, 4, TestEnum> arr;
};
TEST(BitFieldArray, Enum)
{
TestUnion4 object;
object.hex = 0;
const TestUnion4& objectc = object;
for (TestEnum value : object.arr)
{
EXPECT_EQ(value, TestEnum::A);
}
object.arr[0] = TestEnum::B;
EXPECT_EQ(object.arr[0], TestEnum::B);
EXPECT_EQ(object.a, TestEnum::B);
EXPECT_EQ(object.hex, 0b00'00'00'01ull << 30);
object.arr[1] = TestEnum::C;
EXPECT_EQ(object.arr[1], TestEnum::C);
EXPECT_EQ(object.b, TestEnum::C);
EXPECT_EQ(object.hex, 0b00'00'10'01ull << 30);
object.arr[2] = object.arr[1];
EXPECT_EQ(object.arr[2], TestEnum::C);
EXPECT_EQ(object.c, TestEnum::C);
EXPECT_EQ(object.hex, 0b00'10'10'01ull << 30);
object.arr[3] = objectc.arr[0];
EXPECT_EQ(object.arr[3], TestEnum::B);
EXPECT_EQ(object.d, TestEnum::B);
EXPECT_EQ(object.hex, 0b01'10'10'01ull << 30);
for (auto ref : object.arr)
{
ref = TestEnum::D;
}
EXPECT_EQ(object.a, TestEnum::D);
EXPECT_EQ(object.b, TestEnum::D);
EXPECT_EQ(object.c, TestEnum::D);
EXPECT_EQ(object.d, TestEnum::D);
EXPECT_EQ(object.hex, 0b11'11'11'11ull << 30);
std::fill_n(object.arr.begin(), object.arr.Size(), TestEnum::C);
EXPECT_EQ(object.a, TestEnum::C);
EXPECT_EQ(object.b, TestEnum::C);
EXPECT_EQ(object.c, TestEnum::C);
EXPECT_EQ(object.d, TestEnum::C);
EXPECT_EQ(object.hex, 0b10'10'10'10ull << 30);
for (u32 i = 0; i < object.arr.Size(); i++)
{
object.arr[i] = static_cast<TestEnum>(i);
}
EXPECT_EQ(object.hex, 0b11'10'01'00ull << 30);
EXPECT_EQ(objectc.arr[0], TestEnum::A);
EXPECT_EQ(objectc.arr[1], TestEnum::B);
EXPECT_EQ(objectc.arr[2], TestEnum::C);
EXPECT_EQ(objectc.arr[3], TestEnum::D);
u32 counter = 0;
for (TestEnum value : objectc.arr)
{
EXPECT_EQ(value, object.arr[counter++]);
}
EXPECT_EQ("[A (0), B (1), C (2), D (3)]", fmt::format("[{}]", fmt::join(object.arr, ", ")));
EXPECT_EQ("[0x0u /* A */, 0x1u /* B */, 0x2u /* C */, 0x3u /* D */]",
fmt::format("[{:s}]", fmt::join(object.arr, ", ")));
}
union TestUnion5
{
u64 hex;
BitFieldArray<0, 5, 6, u8, u64> arr1;
BitFieldArray<30, 1, 4, bool, u64> arr2;
};
TEST(BitFieldArray, StorageType)
{
TestUnion5 object;
const u64 arr2_hex_1 = 0b1010ull << 30;
object.hex = arr2_hex_1;
const TestUnion5& objectc = object;
EXPECT_FALSE(object.arr2[0]);
EXPECT_TRUE(object.arr2[1]);
EXPECT_FALSE(object.arr2[2]);
EXPECT_TRUE(object.arr2[3]);
object.arr1[0] = 0;
object.arr1[1] = 1;
object.arr1[2] = 2;
object.arr1[3] = 4;
object.arr1[4] = 8;
object.arr1[5] = 16;
const u64 arr1_hex = 0b10000'01000'00100'00010'00001'00000;
EXPECT_EQ(object.hex, arr1_hex | arr2_hex_1);
object.arr2[2] = object.arr2[0] = true;
object.arr2[3] = object.arr2[1] = false;
const u64 arr2_hex_2 = 0b0101ull << 30;
EXPECT_EQ(object.hex, arr1_hex | arr2_hex_2);
object.arr2[2] = object.arr2[1];
object.arr2[3] = objectc.arr2[0];
const u64 arr2_hex_3 = 0b1001ull << 30;
EXPECT_EQ(object.hex, arr1_hex | arr2_hex_3);
u32 counter = 0;
for (u8 value : object.arr1)
{
EXPECT_EQ(value, object.arr1[counter++]);
}
counter = 0;
for (bool value : object.arr2)
{
EXPECT_EQ(value, object.arr2[counter++]);
}
counter = 0;
for (u8 value : objectc.arr1)
{
EXPECT_EQ(value, object.arr1[counter++]);
}
counter = 0;
for (bool value : objectc.arr2)
{
EXPECT_EQ(value, object.arr2[counter++]);
}
}