Go to the documentation of this file.
41 #ifndef __BASE_BITFIELD_HH__
42 #define __BASE_BITFIELD_HH__
47 #include <type_traits>
63 return (nbits >= 64) ? (uint64_t)-1
LL : (1
ULL << nbits) - 1;
77 int nbits = first - last + 1;
78 assert((first - last) >= 0);
79 return (
val >> last) &
mask(nbits);
115 return mbits((uint64_t)-1
LL, first, last);
128 int sign_bit =
bits(
val, N-1, N-1);
144 template <
class T,
class B>
149 T t_bit_val = bit_val;
150 assert((first - last) >= 0);
151 T bmask =
mask(first - last + 1) << last;
152 return ((t_bit_val << last) & bmask) | (
val & ~bmask);
160 template <
class T,
class B>
176 template <
class T,
class B>
189 template <
class T,
class B>
215 static_assert(std::is_integral<T>::value,
"Expecting an integer type");
217 assert(size <=
sizeof(T));
220 for (
auto byte = 0; byte < size; byte++, val = static_cast<T>(
val >> 8)) {
238 if (
bits(
val, 63,32)) { msb += 32;
val >>= 32; }
239 if (
bits(
val, 31,16)) { msb += 16;
val >>= 16; }
243 if (
bits(
val, 1,1)) { msb += 1; }
256 return sizeof(
val) * 8;
257 if (!
bits(
val, 31,0)) { lsb += 32;
val >>= 32; }
258 if (!
bits(
val, 15,0)) { lsb += 16;
val >>= 16; }
262 if (!
bits(
val, 0,0)) { lsb += 1; }
274 return (
v & (
v - 1)) == (T)0;
286 #ifndef __has_builtin
287 #define __has_builtin(foo) 0
289 #if defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl))
290 return __builtin_popcountl(
val);
292 const uint64_t m1 = 0x5555555555555555;
293 const uint64_t m2 = 0x3333333333333333;
294 const uint64_t m4 = 0x0f0f0f0f0f0f0f0f;
295 const uint64_t
sum = 0x0101010101010101;
301 #endif // defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl))
340 return value ? __builtin_ctzl(value) : 32;
353 return value ? __builtin_ctzll(value) : 64;
356 #endif // __BASE_BITFIELD_HH__
static void output(const char *filename)
#define LL(N)
int64_t constant
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.
uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
T mbits(T val, int first, int last)
Mask off the given bits in place like bits() but without shifting.
int popCount(uint64_t val)
Returns the number of set ones in the provided value.
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.
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.
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 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.
uint64_t mask(int nbits)
Generate a 64-bit mask of 'nbits' 1s, right justified.
bool isPow2(T v)
Checks if a number is a power of two, or zero.
#define ULL(N)
uint64_t constant
T bits(T val, int first, int last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17