41 #ifndef __BASE_BITFIELD_HH__ 42 #define __BASE_BITFIELD_HH__ 47 #include <type_traits> 61 return (nbits >= 64) ? (uint64_t)-1
LL : (1
ULL << nbits) - 1;
73 int nbits = first - last + 1;
74 return (val >> last) &
mask(nbits);
85 return bits(val, bit, bit);
97 return val & (
mask(first+1) & ~
mask(last));
103 return mbits((uint64_t)-1
LL, first, last);
114 int sign_bit =
bits(val, N-1, N-1);
115 return sign_bit ? (val | ~
mask(N)) : val;
128 template <
class T,
class B>
133 T t_bit_val = bit_val;
134 T bmask =
mask(first - last + 1) << last;
135 return ((t_bit_val << last) & bmask) | (val & ~bmask);
141 template <
class T,
class B>
153 template <
class T,
class B>
162 template <
class T,
class B>
186 static_assert(std::is_integral<T>::value,
"Expecting an integer type");
188 assert(size <=
sizeof(T));
191 for (
auto byte = 0; byte < size; byte++, val = static_cast<T>(val >> 8)) {
207 if (
bits(val, 63,32)) { msb += 32; val >>= 32; }
208 if (
bits(val, 31,16)) { msb += 16; val >>= 16; }
209 if (
bits(val, 15,8)) { msb += 8; val >>= 8; }
210 if (
bits(val, 7,4)) { msb += 4; val >>= 4; }
211 if (
bits(val, 3,2)) { msb += 2; val >>= 2; }
212 if (
bits(val, 1,1)) { msb += 1; }
223 return sizeof(
val) * 8;
224 if (!
bits(val, 31,0)) { lsb += 32; val >>= 32; }
225 if (!
bits(val, 15,0)) { lsb += 16; val >>= 16; }
226 if (!
bits(val, 7,0)) { lsb += 8; val >>= 8; }
227 if (!
bits(val, 3,0)) { lsb += 4; val >>= 4; }
228 if (!
bits(val, 1,0)) { lsb += 2; val >>= 2; }
229 if (!
bits(val, 0,0)) { lsb += 1; }
239 return (v & (v - 1)) == (T)0;
249 #ifndef __has_builtin 250 #define __has_builtin(foo) 0 252 #if defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl)) 253 return __builtin_popcountl(val);
255 const uint64_t m1 = 0x5555555555555555;
256 const uint64_t m2 = 0x3333333333333333;
257 const uint64_t m4 = 0x0f0f0f0f0f0f0f0f;
258 const uint64_t
sum = 0x0101010101010101;
260 val -= (val >> 1) & m1;
261 val = (val & m2) + ((val >> 2) & m2);
262 val = (val + (val >> 4)) & m4;
263 return (val * sum) >> 56;
264 #endif // defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl)) 299 return value ? __builtin_ctzl(value) : 32;
310 return value ? __builtin_ctzll(value) : 64;
313 #endif // __BASE_BITFIELD_HH__ uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
const uint8_t reverseLookUpTable[]
Lookup table used for High Speed bit reversing.
int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
static void output(const char *filename)
#define LL(N)
int64_t constant
T reverseBits(T val, std::size_t size=sizeof(T))
Takes a variable lenght word and returns the mirrored version (Bit by bit, LSB=>MSB).
int popCount(uint64_t val)
Returns the number of set ones in the provided value.
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.
T insertBits(T val, int first, int last, B bit_val)
Returns val with bits first to last set to the LSBs of bit_val.
#define ULL(N)
uint64_t constant
int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
T mbits(T val, int first, int last)
Mask off the given bits in place like bits() but without shifting.
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.
bool isPow2(T v)
Checks if a number is a power of two, or zero.
uint64_t mask(int nbits)
Generate a 64-bit mask of 'nbits' 1s, right justified.