diff --git a/Source/Core/Common/BitField.h b/Source/Core/Common/BitField.h index 00c5345533..045509ef07 100644 --- a/Source/Core/Common/BitField.h +++ b/Source/Core/Common/BitField.h @@ -110,7 +110,13 @@ * symptoms. */ #pragma pack(1) -template +template ::type directly. + typename StorageType = typename std::conditional_t< + std::is_enum::value, std::underlying_type, std::enable_if>::type> struct BitField { private: @@ -149,20 +155,13 @@ public: constexpr std::size_t NumBits() const { return bits; } private: - // 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::type directly. - using StorageType = typename std::conditional_t::value, std::underlying_type, - std::enable_if>::type; - // Unsigned version of StorageType using StorageTypeU = std::make_unsigned_t; constexpr T Value(std::true_type) const { - using shift_amount = std::integral_constant; - return static_cast((storage << (shift_amount() - position)) >> shift_amount()); + const size_t shift_amount = 8 * sizeof(StorageType) - bits; + return static_cast((storage << (shift_amount - position)) >> shift_amount); } constexpr T Value(std::false_type) const @@ -172,15 +171,17 @@ private: static constexpr StorageType GetMask() { - return (std::numeric_limits::max() >> (8 * sizeof(T) - bits)) << position; + return (std::numeric_limits::max() >> (8 * sizeof(StorageType) - bits)) + << position; } StorageType storage; - static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range"); + static_assert(bits + position <= 8 * sizeof(StorageType), "Bitfield 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(T), "Invalid position"); + 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"); }; diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index 32b587469f..cb82a33911 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -21,6 +21,8 @@ union TestUnion BitField<30, 4, s64> at_dword_boundary; // goes over the boundary of two u32 values BitField<15, 1, s64> signed_1bit; // allowed values: -1 and 0 + + BitField<63, 1, bool, u64> flag; }; // table of raw numbers to test with @@ -51,6 +53,7 @@ TEST(BitField, Storage) EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_signed)); EXPECT_EQ(sizeof(TestUnion), sizeof(object.at_dword_boundary)); EXPECT_EQ(sizeof(TestUnion), sizeof(object.signed_1bit)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.flag)); // Now write some values to one field and check if this reflects properly // in the others. @@ -82,6 +85,7 @@ TEST(BitField, Read) EXPECT_EQ(object.regular_field_signed, (s64)object.regular_field_signed); EXPECT_EQ(object.at_dword_boundary, (s64)object.at_dword_boundary); EXPECT_EQ(object.signed_1bit, (s64)object.signed_1bit); + EXPECT_EQ(object.flag, (bool)object.flag); // Now make sure the value is indeed correct EXPECT_EQ(val, object.full_u64); @@ -91,6 +95,7 @@ TEST(BitField, Read) EXPECT_EQ(((s64)(object.hex << 52)) >> 61, object.regular_field_signed); EXPECT_EQ(((s64)(object.hex << 30)) >> 60, object.at_dword_boundary); EXPECT_EQ(((object.hex >> 15) & 1) ? -1 : 0, object.signed_1bit); + EXPECT_EQ((bool)object.flag, ((object.hex >> 63) & 1)); } } @@ -122,6 +127,10 @@ TEST(BitField, Assignment) // Assignment from other BitField object.at_dword_boundary = object.regular_field_signed; EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); + + // Assignment to field of a type with a size smaller than the underlying type + object.flag = (val & 2); + EXPECT_EQ(object.flag, (val & 2) != 0); } } @@ -165,5 +174,9 @@ TEST(BitField, Alignment) // Assignment from other BitField object.at_dword_boundary = object.regular_field_signed; EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); + + // Assignment to field of a type with a size smaller than the underlying type + object.flag = (val & 2); + EXPECT_EQ(object.flag, (val & 2) != 0); } }