38 #include <gtest/gtest.h>
46 TEST(BitfieldTest, Mask0Bits)
51 TEST(BitfieldTest, Mask1Bit)
56 TEST(BitfieldTest, Mask8Bits)
61 TEST(BitfieldTest, Mask16Bits)
66 TEST(BitfieldTest, Mask32Bits)
71 TEST(BitfieldTest, MaskAllBits)
76 TEST(BitfieldTest, MaskAllBitsGreaterThan64)
88 TEST(BitfieldTest, MaskOneBit)
93 TEST(BitfieldTest, MaskTwoBits)
98 TEST(BitfieldTest, MaskThreeBits)
103 TEST(BitfieldTest, MaskEntireRange)
108 TEST(BitfieldTest, MaskOutsideOfRange)
118 TEST(BitfieldTest, ExtractOneBit)
124 TEST(BitfieldTest, Extract63rdBit)
126 int64_t
x = 1
ULL << 63;
130 TEST(BitfieldTest, ExtractFirstBit)
136 TEST(BitfieldTest, ExtractFirstBitFirstBitZero)
142 TEST(BitfieldTest, ExtractThreeBits)
144 uint64_t
x = 1 << 31;
153 TEST(BitfieldTest, MbitsStandardCase)
155 uint64_t
x = (1 << 10) + (1 << 1);
159 TEST(BitfieldTest, MbitsEntireRange)
161 uint64_t
x = (1
ULL << 63) + 1;
169 TEST(BitfieldTest, SignExtendPositiveInput)
176 TEST(BitfieldTest, SignExtendNegativeInput)
183 TEST(BitfieldTest, SignExtendPositiveInputOutsideRange)
188 TEST(BitfieldTest, SignExtendNegativeInputOutsideRange)
190 uint64_t
val = 0x4800000010000008;
191 uint64_t
output = 0xF800000010000008;
199 TEST(BitfieldTest, InsertOneBitTo3)
202 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
206 TEST(BitfieldTest, InsertOneBitTo18)
209 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
213 TEST(BitfieldTest, InsertOneBitTo3LsbZero)
216 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
220 TEST(BitfieldTest, InsertOneBitTo18LsbZero)
223 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
227 TEST(BitfieldTest, InsertOnBitTo8LsbZero)
229 int64_t
val = (1 << 8);
230 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
234 TEST(BitfieldTest, InsertMultipleBits)
236 int64_t
val = (1
ULL << 63);
237 int64_t
bits = (1 << 2) + 1;
241 TEST(BitfieldTest, InsertMultipleBitsOverwrite)
243 int64_t
val = (1 << 29);
244 int64_t
bits = (1 << 2) + 1;
249 TEST(BitfieldTest, ReverseBits8Bit)
251 uint8_t value = (1 << 7);
255 TEST(BitfieldTest, ReverseBits64Bit)
257 uint64_t value = 0xF0F0F0F0F0F0F0F1;
266 uint64_t
val = (1 << 29) + (1 << 1);
272 uint64_t
val = (1
ULL << 63) + (1
ULL << 60) + (1 << 1);
277 TEST(BitfieldTest, FindMsbZero)
284 uint64_t
val = (1
ULL << 63) + (1 << 1);
288 TEST(BitfieldTest, FindLsbZero)
315 TEST(BitfieldTest, PopCountNoBits)
320 TEST(BitfieldTest, PopCountOneBit)
322 int64_t
val = (1 << 9);
326 TEST(BitfieldTest, PopCountManyBits)
328 int64_t
val = (1 << 22) + (1 << 21) + (1 << 15) + (1 << 9) + 1;
332 TEST(BitfieldTest, PopCountAllOnes)
334 int64_t
val = 0xFFFFFFFFFFFFFFFF;
343 TEST(BitfieldTest, AlignToPowerOfTwo0)
348 TEST(BitfieldTest, AlignToPowerOfTwo3)
353 TEST(BitfieldTest, AlignToPowerOfTwo5)
358 TEST(BitfieldTest, AlignToPowerOfTwo10)
363 TEST(BitfieldTest, AlignToPowerOfTwo16)
368 TEST(BitfieldTest, AlignToPowerOfTwo31)
379 TEST(BitfieldTest, CountTrailingZeros32BitsNoTrailing)
385 TEST(BitfieldTest, CountTrailingZeros32Bits)
387 uint32_t value = (1 << 30) + (1 << 29);
391 TEST(BitfieldTest, CountTrailingZeros64BitsNoTrailing)
393 uint64_t value = (1 << 29) + 1;
397 TEST(BitfieldTest, CountTrailingZeros64Bits)
399 uint64_t value = 1
ULL << 63;
403 TEST(BitfieldTest, CountTrailingZero64AllZeros)