44 #ifndef __BASE_BITFIELD_HH__
45 #define __BASE_BITFIELD_HH__
55 return (nbits == 64) ? (uint64_t)-1
LL : (1
ULL << nbits) - 1;
69 int nbits = first - last + 1;
70 return (val >> last) &
mask(nbits);
81 return bits(val, bit, bit);
93 return val & (
mask(first+1) & ~
mask(last));
99 return mbits((uint64_t)-1
LL, first, last);
110 int sign_bit =
bits(val, N-1, N-1);
111 return sign_bit ? (val | ~
mask(N)) : val;
117 template <
class T,
class B>
122 T t_bit_val = bit_val;
123 T bmask =
mask(first - last + 1) << last;
124 return ((t_bit_val << last) & bmask) | (val & ~bmask);
130 template <
class T,
class B>
142 template <
class T,
class B>
151 template <
class T,
class B>
167 if (
bits(val, 63,32)) { msb += 32; val >>= 32; }
168 if (
bits(val, 31,16)) { msb += 16; val >>= 16; }
169 if (
bits(val, 15,8)) { msb += 8; val >>= 8; }
170 if (
bits(val, 7,4)) { msb += 4; val >>= 4; }
171 if (
bits(val, 3,2)) { msb += 2; val >>= 2; }
172 if (
bits(val, 1,1)) { msb += 1; }
183 return sizeof(
val) * 8;
184 if (!
bits(val, 31,0)) { lsb += 32; val >>= 32; }
185 if (!
bits(val, 15,0)) { lsb += 16; val >>= 16; }
186 if (!
bits(val, 7,0)) { lsb += 8; val >>= 8; }
187 if (!
bits(val, 3,0)) { lsb += 4; val >>= 4; }
188 if (!
bits(val, 1,0)) { lsb += 2; val >>= 2; }
189 if (!
bits(val, 0,0)) { lsb += 1; }
199 return (v & (v - 1)) == (T)0;
209 #ifndef __has_builtin
210 #define __has_builtin(foo) 0
212 #if defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl))
213 return __builtin_popcountl(val);
215 const uint64_t m1 = 0x5555555555555555;
216 const uint64_t m2 = 0x3333333333333333;
217 const uint64_t m4 = 0x0f0f0f0f0f0f0f0f;
218 const uint64_t
sum = 0x0101010101010101;
220 val -= (val >> 1) & m1;
221 val = (val & m2) + ((val >> 2) & m2);
222 val = (val + (val >> 4)) & m4;
223 return (val * sum) >> 56;
224 #endif // defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl))
251 #endif // __BASE_BITFIELD_HH__
uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
#define LL(N)
int64_t constant
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)
Return val with bits first to last set to 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.