31 #ifndef __BASE_BITUNION_HH__
32 #define __BASE_BITUNION_HH__
47 namespace BitfieldBackend
84 template<
int first,
int last=first>
87 static_assert(first >= last,
88 "Bitfield ranges must be specified as <msb, lsb>");
91 operator uint64_t ()
const
93 return this->
getBits(first, last);
99 this->
setBits(first, last, _data);
106 return *
this = (uint64_t)other;
115 template<
int first,
int last=first>
127 template<
int first,
int last=first>
131 operator uint64_t ()
const;
146 template<
int first,
int last=first>
150 operator int64_t ()
const
152 return sext<first - last + 1>(this->
getBits(first, last));
158 this->
setBits(first, last, _data);
165 return *
this = (int64_t)other;
174 template<
int first,
int last=first>
186 template<
int first,
int last=first>
190 operator int64_t ()
const;
208 template <
class Type,
class Base>
214 Base::__data = _data;
219 operator const Type ()
const
227 Base::__data = _data;
234 Base::__data = other;
241 return Base::__data < base.__data;
247 return Base::__data == base.__data;
267 #define __BitUnion(type, name) \
268 class BitfieldUnderlyingClasses##name : \
269 public BitfieldBackend::BitfieldTypes<type> \
272 typedef type __DataType; \
280 #define EndBitUnion(name) \
283 typedef BitfieldBackend::BitUnionOperators< \
284 BitfieldUnderlyingClasses##name::__DataType, \
285 BitfieldUnderlyingClasses##name> name;
292 #define __SubBitUnion(fieldType, first, last, name) \
293 class : public BitfieldBackend::BitfieldTypes<__DataType> \
297 fieldType<first, last> __data;
304 #define EndSubBitUnion(name) \
306 inline operator __DataType () const \
309 inline __DataType operator = (const __DataType & _data) \
310 { return __data = _data;} \
315 #define SubBitUnion(name, first, last) \
316 __SubBitUnion(Bitfield, first, last, name)
320 #define SignedSubBitUnion(name, first, last) \
321 __SubBitUnion(SignedBitfield, first, last, name)
324 #define BitUnion(type, name) __BitUnion(type, name)
327 #define BitUnion64(name) __BitUnion(uint64_t, name)
328 #define BitUnion32(name) __BitUnion(uint32_t, name)
329 #define BitUnion16(name) __BitUnion(uint16_t, name)
330 #define BitUnion8(name) __BitUnion(uint8_t, name)
332 #endif // __BASE_BITUNION_HH__
bool operator<(Base const &base) const
void setBits(int first, int last, uint64_t val)
Type operator=(Type const &_data)
uint64_t getBits(int first, int last) const
uint64_t operator=(Bitfield< first, last > const &other)
BitUnionOperators(Type const &_data)
uint64_t operator=(const uint64_t _data)
uint64_t operator=(const uint64_t _data)
int64_t operator=(const int64_t _data)
void replaceBits(T &val, int first, int last, B bit_val)
A convenience function to replace bits first to last of val with bit_val in place.
Type operator=(BitUnionOperators const &other)
int64_t operator=(SignedBitfield< first, last > const &other)
int64_t operator=(const int64_t _data)
bool operator==(Base const &base) const
T bits(T val, int first, int last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it...
uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.