From cf95deaf6ddbcbd1cf649e1cba189f836023b23b Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Wed, 17 Feb 2021 20:23:06 -0800 Subject: Allow specifying StorageType for BitField This is useful for BitFields that are bools. --- Source/Core/Common/BitField.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'Source/Core/Common/BitField.h') 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"); }; -- cgit v1.2.3 From 1273c5e3953e61e93ac67531285a766b5356fe3d Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 9 Feb 2021 14:46:27 -0800 Subject: Add fmt support to BitField --- Source/Core/Common/BitField.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'Source/Core/Common/BitField.h') diff --git a/Source/Core/Common/BitField.h b/Source/Core/Common/BitField.h index 045509ef07..caaf91dc3d 100644 --- a/Source/Core/Common/BitField.h +++ b/Source/Core/Common/BitField.h @@ -32,6 +32,7 @@ #pragma once #include +#include #include #include @@ -99,6 +100,8 @@ * explicit cast must be performed on the BitField object to make sure it gets * passed correctly, e.g.: * printf("Value: %d", (s32)some_register.some_signed_fields); + * Note that this does not apply when using fmt, as a formatter is provided that + * handles this conversion automatically. * * 2) * Not really a caveat, but potentially irritating: This class is used in some @@ -186,3 +189,16 @@ private: static_assert(bits > 0, "Invalid number of bits"); }; #pragma pack() + +// Use the underlying type's formatter for BitFields, if one exists +template +struct fmt::formatter> +{ + fmt::formatter m_formatter; + constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); } + template + auto format(const BitField& bitfield, FormatContext& ctx) + { + return m_formatter.format(bitfield.Value(), ctx); + } +}; -- cgit v1.2.3 From f697e17dd1005eeea172f6a34497cae47bdfc8d6 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 16 Feb 2021 22:49:30 -0800 Subject: Create BitFieldArray --- Source/Core/Common/BitField.h | 303 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 303 insertions(+) (limited to 'Source/Core/Common/BitField.h') 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); + } +}; -- cgit v1.2.3