diff options
| author | Anthony <Helios747@users.noreply.github.com> | 2017-03-28 09:15:37 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-28 09:15:37 -0700 |
| commit | 2c531889b26da2ac3fa7a046e988c15fe2e043b2 (patch) | |
| tree | 514df2cb2cf8f65688f915f4b86251a6f4dec5f2 /Source/Core/Common | |
| parent | 85d74a506f6e4024ae1ffdf857d68ebed96fa4a9 (diff) | |
| parent | b711daee5ff18479b11657262a338f3c431d0ef0 (diff) | |
Merge pull request #5139 from lioncash/bitfield
BitField: Minor cleanup
Diffstat (limited to 'Source/Core/Common')
| -rw-r--r-- | Source/Core/Common/BitField.h | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/Source/Core/Common/BitField.h b/Source/Core/Common/BitField.h index 10312f1c38..85a595239c 100644 --- a/Source/Core/Common/BitField.h +++ b/Source/Core/Common/BitField.h @@ -121,7 +121,7 @@ private: public: // Force default constructor to be created // so that we can use this within unions - BitField() = default; + constexpr BitField() = default; // We explicitly delete the copy assignment operator here, because the // default copy assignment would copy the full storage value, rather than @@ -139,34 +139,33 @@ public: return *this; } - __forceinline T Value() const - { - if (std::numeric_limits<T>::is_signed) - { - std::size_t shift = 8 * sizeof(T) - bits; - return (T)((storage << (shift - position)) >> shift); - } - else - { - return (T)((storage & GetMask()) >> position); - } - } - - __forceinline operator T() const { return Value(); } + constexpr T Value() const { return Value(std::is_signed<T>()); } + constexpr operator T() const { return Value(); } 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<T>::type directly. - typedef typename std::conditional<std::is_enum<T>::value, std::underlying_type<T>, - std::enable_if<true, T>>::type::type StorageType; + using StorageType = typename std::conditional_t<std::is_enum<T>::value, std::underlying_type<T>, + std::enable_if<true, T>>::type; // Unsigned version of StorageType - typedef typename std::make_unsigned<StorageType>::type StorageTypeU; + using StorageTypeU = std::make_unsigned_t<StorageType>; + + constexpr T Value(std::true_type) const + { + using shift_amount = std::integral_constant<size_t, 8 * sizeof(T) - bits>; + return static_cast<T>((storage << (shift_amount() - position)) >> shift_amount()); + } + + constexpr T Value(std::false_type) const + { + return static_cast<T>((storage & GetMask()) >> position); + } - __forceinline StorageType GetMask() const + static constexpr StorageType GetMask() { - return (((StorageTypeU)~0) >> (8 * sizeof(T) - bits)) << position; + return (std::numeric_limits<StorageTypeU>::max() >> (8 * sizeof(T) - bits)) << position; } StorageType storage; |
