40 #include <gtest/gtest.h> 48 TEST(BitfieldTest, Mask0Bits)
53 TEST(BitfieldTest, Mask1Bit)
58 TEST(BitfieldTest, Mask8Bits)
63 TEST(BitfieldTest, Mask16Bits)
68 TEST(BitfieldTest, Mask32Bits)
73 TEST(BitfieldTest, MaskAllBits)
78 TEST(BitfieldTest, MaskAllBitsGreaterThan64)
90 TEST(BitfieldTest, MaskOneBit)
95 TEST(BitfieldTest, MaskTwoBits)
100 TEST(BitfieldTest, MaskThreeBits)
105 TEST(BitfieldTest, MaskEntireRange)
110 TEST(BitfieldTest, MaskOutsideOfRange)
120 TEST(BitfieldTest, ExtractOneBit)
126 TEST(BitfieldTest, Extract63rdBit)
128 int64_t
x = 1
ULL << 63;
132 TEST(BitfieldTest, ExtractFirstBit)
138 TEST(BitfieldTest, ExtractFirstBitFirstBitZero)
144 TEST(BitfieldTest, ExtractThreeBits)
146 uint64_t
x = 1 << 31;
155 TEST(BitfieldTest, MbitsStandardCase)
157 uint64_t
x = (1 << 10) + (1 << 1);
161 TEST(BitfieldTest, MbitsEntireRange)
163 uint64_t
x = (1
ULL << 63) + 1;
171 TEST(BitfieldTest, SignExtendPositiveInput)
178 TEST(BitfieldTest, SignExtendNegativeInput)
185 TEST(BitfieldTest, SignExtendPositiveInputOutsideRange)
190 TEST(BitfieldTest, SignExtendNegativeInputOutsideRange)
192 uint64_t
val = 0x4800000010000008;
193 uint64_t
output = 0xF800000010000008;
201 TEST(BitfieldTest, InsertOneBitTo3)
204 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
208 TEST(BitfieldTest, InsertOneBitTo18)
211 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
215 TEST(BitfieldTest, InsertOneBitTo3LsbZero)
218 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
222 TEST(BitfieldTest, InsertOneBitTo18LsbZero)
225 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
229 TEST(BitfieldTest, InsertOnBitTo8LsbZero)
231 int64_t
val = (1 << 8);
232 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
236 TEST(BitfieldTest, InsertMultipleBits)
238 int64_t
val = (1
ULL << 63);
239 int64_t
bits = (1 << 2) + 1;
243 TEST(BitfieldTest, InsertMultipleBitsOverwrite)
245 int64_t
val = (1 << 29);
246 int64_t
bits = (1 << 2) + 1;
251 TEST(BitfieldTest, ReverseBits8Bit)
253 uint8_t value = (1 << 7);
257 TEST(BitfieldTest, ReverseBits64Bit)
259 uint64_t value = 0xF0F0F0F0F0F0F0F1;
268 uint64_t
val = (1 << 29) + (1 << 1);
274 uint64_t
val = (1
ULL << 63) + (1
ULL << 60) + (1 << 1);
279 TEST(BitfieldTest, FindMsbZero)
286 uint64_t
val = (1
ULL << 63) + (1 << 1);
290 TEST(BitfieldTest, FindLsbZero)
317 TEST(BitfieldTest, PopCountNoBits)
322 TEST(BitfieldTest, PopCountOneBit)
324 int64_t
val = (1 << 9);
328 TEST(BitfieldTest, PopCountManyBits)
330 int64_t
val = (1 << 22) + (1 << 21) + (1 << 15) + (1 << 9) + 1;
334 TEST(BitfieldTest, PopCountAllOnes)
336 int64_t
val = 0xFFFFFFFFFFFFFFFF;
345 TEST(BitfieldTest, AlignToPowerOfTwo0)
350 TEST(BitfieldTest, AlignToPowerOfTwo3)
355 TEST(BitfieldTest, AlignToPowerOfTwo5)
360 TEST(BitfieldTest, AlignToPowerOfTwo10)
365 TEST(BitfieldTest, AlignToPowerOfTwo16)
370 TEST(BitfieldTest, AlignToPowerOfTwo31)
381 TEST(BitfieldTest, CountTrailingZeros32BitsNoTrailing)
387 TEST(BitfieldTest, CountTrailingZeros32Bits)
389 uint32_t value = (1 << 30) + (1 << 29);
393 TEST(BitfieldTest, CountTrailingZeros64BitsNoTrailing)
395 uint64_t value = (1 << 29) + 1;
399 TEST(BitfieldTest, CountTrailingZeros64Bits)
401 uint64_t value = 1
ULL << 63;
405 TEST(BitfieldTest, CountTrailingZero64AllZeros)
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.
int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
static void output(const char *filename)
TEST(BitfieldTest, Mask0Bits)
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.
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
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 EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
#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...
bool isPow2(T v)
Checks if a number is a power of two, or zero.
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.