From f697e17dd1005eeea172f6a34497cae47bdfc8d6 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 16 Feb 2021 22:49:30 -0800 Subject: [PATCH] Create BitFieldArray --- Source/Core/Common/BitField.h | 303 +++++++++++++++++++++++ Source/UnitTests/Common/BitFieldTest.cpp | 292 ++++++++++++++++++++++ 2 files changed, 595 insertions(+) diff --git a/Source/Core/Common/BitField.h b/Source/Core/Common/BitField.h index caaf91dc3d..e7af066d2f 100644 --- a/Source/Core/Common/BitField.h +++ b/Source/Core/Common/BitField.h @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -202,3 +203,305 @@ struct fmt::formatter> return m_formatter.format(bitfield.Value(), ctx); } }; + +// Language limitations require the following to make these formattable +// (formatter::Ref> is not legal) +template +class BitFieldArrayConstRef; +template +class BitFieldArrayRef; +template +class BitFieldArrayConstIterator; +template +class BitFieldArrayIterator; + +#pragma pack(1) +template ::type directly. + typename StorageType = typename std::conditional_t< + std::is_enum::value, std::underlying_type, std::enable_if>::type> +struct BitFieldArray +{ + using Ref = BitFieldArrayRef; + using ConstRef = BitFieldArrayConstRef; + using Iterator = BitFieldArrayIterator; + using ConstIterator = BitFieldArrayConstIterator; + +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(), index); } + void SetValue(size_t index, T value) + { + const size_t pos = position + bits * index; + storage = (storage & ~GetElementMask(index)) | + ((static_cast(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; + + 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((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((storage & GetElementMask(index)) >> pos); + } + + static constexpr StorageType GetElementMask(size_t index) + { + const size_t pos = position + bits * index; + return (std::numeric_limits::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 +class BitFieldArrayConstRef +{ + friend struct BitFieldArray; + friend class BitFieldArrayConstIterator; + +public: + constexpr T Value() const { return m_array->Value(m_index); }; + constexpr operator T() const { return Value(); } + +private: + constexpr BitFieldArrayConstRef(const BitFieldArray* array, + size_t index) + : m_array(array), m_index(index) + { + } + + const BitFieldArray* const m_array; + const size_t m_index; +}; + +template +class BitFieldArrayRef +{ + friend struct BitFieldArray; + friend class BitFieldArrayIterator; + +public: + constexpr T Value() const { return m_array->Value(m_index); }; + constexpr operator T() const { return Value(); } + T operator=(const BitFieldArrayRef& 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* array, size_t index) + : m_array(array), m_index(index) + { + } + + BitFieldArray* 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 +class BitFieldArrayIterator +{ + friend struct BitFieldArray; + +public: + using iterator_category = std::output_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = void; + using reference = BitFieldArrayRef; + +private: + constexpr BitFieldArrayIterator(BitFieldArray* 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* 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 +class BitFieldArrayConstIterator +{ + friend struct BitFieldArray; + +public: + using iterator_category = std::input_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = void; + using reference = BitFieldArrayConstRef; + +private: + constexpr BitFieldArrayConstIterator(const BitFieldArray* 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* m_array; + size_t m_index; +}; + +template +struct fmt::formatter> +{ + fmt::formatter m_formatter; + constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); } + template + auto format(const BitFieldArrayRef& ref, FormatContext& ctx) + { + return m_formatter.format(ref.Value(), ctx); + } +}; + +template +struct fmt::formatter> +{ + fmt::formatter m_formatter; + constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); } + template + auto format(const BitFieldArrayConstRef& ref, FormatContext& ctx) + { + return m_formatter.format(ref.Value(), ctx); + } +}; diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index d68b18b4c6..e7a7bb764c 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -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(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++]); + } +}