summaryrefslogtreecommitdiff
path: root/src/common/bit_field.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/bit_field.h')
-rw-r--r--src/common/bit_field.h27
1 files changed, 10 insertions, 17 deletions
diff --git a/src/common/bit_field.h b/src/common/bit_field.h
index 4748999ed..030f7caeb 100644
--- a/src/common/bit_field.h
+++ b/src/common/bit_field.h
@@ -1,7 +1,6 @@
1// Licensed under GPLv2 or any later version 1// Licensed under GPLv2 or any later version
2// Refer to the license.txt file included. 2// Refer to the license.txt file included.
3 3
4
5// Copyright 2014 Tony Wasserka 4// Copyright 2014 Tony Wasserka
6// All rights reserved. 5// All rights reserved.
7// 6//
@@ -29,13 +28,11 @@
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 30
32
33#pragma once 31#pragma once
34 32
35#include <cstddef> 33#include <cstddef>
36#include <limits> 34#include <limits>
37#include <type_traits> 35#include <type_traits>
38
39#include "common/common_funcs.h" 36#include "common/common_funcs.h"
40 37
41/* 38/*
@@ -111,9 +108,8 @@
111 * symptoms. 108 * symptoms.
112 */ 109 */
113#pragma pack(1) 110#pragma pack(1)
114template<std::size_t position, std::size_t bits, typename T> 111template <std::size_t position, std::size_t bits, typename T>
115struct BitField 112struct BitField {
116{
117private: 113private:
118 // We hide the copy assigment operator here, because the default copy 114 // We hide the copy assigment operator here, because the default copy
119 // assignment would copy the full storage value, rather than just the bits 115 // assignment would copy the full storage value, rather than just the bits
@@ -141,13 +137,10 @@ public:
141 } 137 }
142 138
143 FORCE_INLINE T Value() const { 139 FORCE_INLINE T Value() const {
144 if (std::numeric_limits<T>::is_signed) 140 if (std::numeric_limits<T>::is_signed) {
145 { 141 std::size_t shift = 8 * sizeof(T) - bits;
146 std::size_t shift = 8 * sizeof(T)-bits;
147 return (T)((storage << (shift - position)) >> shift); 142 return (T)((storage << (shift - position)) >> shift);
148 } 143 } else {
149 else
150 {
151 return (T)((storage & GetMask()) >> position); 144 return (T)((storage & GetMask()) >> position);
152 } 145 }
153 } 146 }
@@ -162,15 +155,14 @@ private:
162 // T is an enumeration. Note that T is wrapped within an enable_if in the 155 // T is an enumeration. Note that T is wrapped within an enable_if in the
163 // former case to workaround compile errors which arise when using 156 // former case to workaround compile errors which arise when using
164 // std::underlying_type<T>::type directly. 157 // std::underlying_type<T>::type directly.
165 typedef typename std::conditional < std::is_enum<T>::value, 158 typedef typename std::conditional<std::is_enum<T>::value, std::underlying_type<T>,
166 std::underlying_type<T>, 159 std::enable_if<true, T>>::type::type StorageType;
167 std::enable_if < true, T >> ::type::type StorageType;
168 160
169 // Unsigned version of StorageType 161 // Unsigned version of StorageType
170 typedef typename std::make_unsigned<StorageType>::type StorageTypeU; 162 typedef typename std::make_unsigned<StorageType>::type StorageTypeU;
171 163
172 FORCE_INLINE StorageType GetMask() const { 164 FORCE_INLINE StorageType GetMask() const {
173 return (((StorageTypeU)~0) >> (8 * sizeof(T)-bits)) << position; 165 return (((StorageTypeU)~0) >> (8 * sizeof(T) - bits)) << position;
174 } 166 }
175 167
176 StorageType storage; 168 StorageType storage;
@@ -186,5 +178,6 @@ private:
186#pragma pack() 178#pragma pack()
187 179
188#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) 180#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER)
189static_assert(std::is_trivially_copyable<BitField<0, 1, unsigned>>::value, "BitField must be trivially copyable"); 181static_assert(std::is_trivially_copyable<BitField<0, 1, unsigned>>::value,
182 "BitField must be trivially copyable");
190#endif 183#endif