41#ifndef __BASE_BITFIELD_HH__
42#define __BASE_BITFIELD_HH__
68 return (nbits >= 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
81 assert(first >= last);
82 int nbits = first - last + 1;
83 return (
val >> last) &
mask(nbits);
108 return val & (
mask(first + 1) & ~mask(last));
115mask(
unsigned first,
unsigned last)
117 return mbits((uint64_t)-1LL, first, last);
131 bool sign_bit =
bits(
val, N - 1);
147 bool sign_bit =
bits(
val, N - 1);
163 bool sign_bit =
bits(
val, N - 1);
183template <
class T,
class B>
187 assert(first >= last);
188 T bmask =
mask(first, last);
190 val |= ((T)bit_val << last) & bmask;
199template <
class T,
class B>
214template <
class T,
class B>
226template <
class T,
class B>
254std::enable_if_t<std::is_integral_v<T>, T>
257 assert(size <=
sizeof(T));
259 if constexpr (
sizeof(T) == 1) {
264 for (
size_t byte = 0;
byte < size;
byte++) {
318# define __has_builtin(foo) 0
320#if defined(__has_builtin) && __has_builtin(__builtin_ctz)
321 return sizeof(
unsigned long long) >=
sizeof(T);
329findLsbSetFallback(uint64_t
val) {
332 return sizeof(
val) * 8;
370 if (
val == 0)
return 64;
372 if constexpr (hasBuiltinCtz<decltype(val)>()) {
373 return __builtin_ctzll(
val);
375 return findLsbSetFallback(
val);
384 if constexpr (N <= 64) {
387 if (
bs.none())
return N;
389 constexpr std::bitset<N>
mask(std::numeric_limits<uint64_t>::max());
391 auto nextQword{
bs &
mask};
393 while (nextQword.none()) {
402 uint64_t remaining{nextQword.to_ullong()};
418# define __has_builtin(foo) 0
420#if defined(__GNUC__) || \
421 (defined(__clang__) && __has_builtin(__builtin_popcountl))
422 return __builtin_popcountl(
val);
424 const uint64_t m1 = 0x5555555555555555ULL;
425 const uint64_t m2 = 0x3333333333333333ULL;
426 const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL;
427 const uint64_t
sum = 0x0101010101010101ULL;
475 return value ? __builtin_ctzl(value) : 32;
489 return value ? __builtin_ctzll(value) : 64;
503 return value ? __builtin_clz(value) : 32;
517 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 That function will either use a builtin ...
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.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
const uint8_t reverseBitsLookUpTable[]
Lookup table used for High Speed bit reversing.
static void output(const char *filename)