41#ifndef __BASE_BITFIELD_HH__
42#define __BASE_BITFIELD_HH__
65 return (nbits >= 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
78 assert(first >= last);
79 int nbits = first - last + 1;
80 return (
val >> last) &
mask(nbits);
105 return val & (
mask(first + 1) & ~mask(last));
112mask(
unsigned first,
unsigned last)
114 return mbits((uint64_t)-1LL, first, last);
128 bool sign_bit =
bits(
val, N - 1);
144 bool sign_bit =
bits(
val, N - 1);
160 bool sign_bit =
bits(
val, N - 1);
180template <
class T,
class B>
184 assert(first >= last);
185 T bmask =
mask(first, last);
187 val |= ((T)bit_val << last) & bmask;
196template <
class T,
class B>
211template <
class T,
class B>
223template <
class T,
class B>
251std::enable_if_t<std::is_integral_v<T>, T>
254 assert(size <=
sizeof(T));
256 if constexpr (
sizeof(T) == 1) {
261 for (
size_t byte = 0;
byte < size;
byte++) {
316 return sizeof(
val) * 8;
353# define __has_builtin(foo) 0
355#if defined(__GNUC__) || \
356 (defined(__clang__) && __has_builtin(__builtin_popcountl))
357 return __builtin_popcountl(
val);
359 const uint64_t m1 = 0x5555555555555555ULL;
360 const uint64_t m2 = 0x3333333333333333ULL;
361 const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL;
362 const uint64_t
sum = 0x0101010101010101ULL;
410 return value ? __builtin_ctzl(value) : 32;
424 return value ? __builtin_ctzll(value) : 64;
438 return value ? __builtin_clz(value) : 32;
452 return value ? __builtin_clzll(value) : 64;
constexpr int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
constexpr int clz32(uint32_t value)
Count leading zeros in a 32-bit value.
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
constexpr int popCount(uint64_t val)
Returns the number of set ones in the provided value.
constexpr T mbits(T val, unsigned first, unsigned last)
Mask off the given bits in place like bits() but without shifting.
constexpr int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
constexpr uint64_t szext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
constexpr T insertBits(T val, unsigned first, unsigned last, B bit_val)
Returns val with bits first to last set to the LSBs of bit_val.
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
constexpr int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
constexpr int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
constexpr void replaceBits(T &val, unsigned first, unsigned last, B bit_val)
A convenience function to replace bits first to last of val with bit_val in place.
constexpr int clz64(uint64_t value)
Count leading zeros in a 64-bit value.
std::enable_if_t< std::is_integral_v< T >, T > reverseBits(T val, size_t size=sizeof(T))
Takes a value and returns the bit reversed version.
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
const uint8_t reverseBitsLookUpTable[]
Lookup table used for High Speed bit reversing.
static void output(const char *filename)