38#include <gtest/gtest.h>
48TEST(BitfieldTest, Mask0Bits)
50 EXPECT_EQ(0x0,
mask(0));
53TEST(BitfieldTest, Mask1Bit)
55 EXPECT_EQ(0x1,
mask(1));
58TEST(BitfieldTest, Mask8Bits)
60 EXPECT_EQ(0xFF,
mask(8));
63TEST(BitfieldTest, Mask16Bits)
65 EXPECT_EQ(0xFFFF,
mask(16));
68TEST(BitfieldTest, Mask32Bits)
70 EXPECT_EQ(0xFFFFFFFF,
mask(32));
73TEST(BitfieldTest, MaskAllBits)
75 EXPECT_EQ(0xFFFFFFFFFFFFFFFF,
mask(64));
78TEST(BitfieldTest, MaskAllBitsGreaterThan64)
83 EXPECT_EQ(0xFFFFFFFFFFFFFFFF,
mask(70));
90TEST(BitfieldTest, MaskOneBit)
92 EXPECT_EQ(1,
mask(0, 0));
95TEST(BitfieldTest, MaskTwoBits)
97 EXPECT_EQ((1 << 1) + 1,
mask(1, 0));
100TEST(BitfieldTest, MaskThreeBits)
102 EXPECT_EQ((1 << 5) + (1 << 4) + (1 << 3),
mask(5, 3));
105TEST(BitfieldTest, MaskEntireRange)
107 EXPECT_EQ(0xFFFFFFFFFFFFFFFF,
mask(63, 0));
110TEST(BitfieldTest, MaskOutsideOfRange)
113 EXPECT_EQ(0xFFFFFFFFFFFFFFFF,
mask(100, 0));
120TEST(BitfieldTest, ExtractOneBit)
123 EXPECT_EQ(1,
bits(
x, 31));
126TEST(BitfieldTest, Extract63rdBit)
128 int64_t
x = 1ULL << 63;
129 EXPECT_EQ(1,
bits(
x, 63));
132TEST(BitfieldTest, ExtractFirstBit)
135 EXPECT_EQ(1,
bits(
x, 0));
138TEST(BitfieldTest, ExtractFirstBitFirstBitZero)
141 EXPECT_EQ(0,
bits(
x, 0));
144TEST(BitfieldTest, ExtractThreeBits)
146 uint64_t
x = 1 << 31;
147 EXPECT_EQ((1 << 2),
bits(
x, 31, 29));
155TEST(BitfieldTest, MbitsStandardCase)
157 uint64_t
x = (1 << 10) + (1 << 1);
158 EXPECT_EQ((1 << 10),
mbits(
x, 10, 8));
161TEST(BitfieldTest, MbitsEntireRange)
163 uint64_t
x = (1ULL << 63) + 1;
164 EXPECT_EQ((1ULL << 63) + 1,
mbits(
x, 63, 0));
172TEST(BitfieldTest, SignExtendPositiveInput)
179TEST(BitfieldTest, SignExtendNegativeInput)
186TEST(BitfieldTest, SignExtendPositiveInputOutsideRange)
188 EXPECT_EQ((1 << 10),
sext<8>(1 << 10));
191TEST(BitfieldTest, SignExtendNegativeInputOutsideRange)
193 uint64_t
val = 0x4800000010000008;
194 uint64_t
output = 0xF800000010000008;
202TEST(BitfieldTest, SignZeroExtendPositiveInput)
209TEST(BitfieldTest, SignZeroExtendNegativeInput)
216TEST(BitfieldTest, SignZeroExtendPositiveInputOutsideRange)
221TEST(BitfieldTest, SignZeroExtendNegativeInputOutsideRange)
223 uint64_t
val = 0x4800000010000008;
224 uint64_t
output = 0xF800000010000008;
232TEST(BitfieldTest, InsertOneBitTo3)
235 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
239TEST(BitfieldTest, InsertOneBitTo18)
242 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
246TEST(BitfieldTest, InsertOneBitTo3LsbZero)
249 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
253TEST(BitfieldTest, InsertOneBitTo18LsbZero)
256 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
260TEST(BitfieldTest, InsertOnBitTo8LsbZero)
262 int64_t
val = (1 << 8);
263 int64_t
bits = (1 << 3) + (1 << 2) + (1 << 1);
267TEST(BitfieldTest, InsertMultipleBits)
269 int64_t
val = (1ULL << 63);
270 int64_t
bits = (1 << 2) + 1;
274TEST(BitfieldTest, InsertMultipleBitsOverwrite)
276 int64_t
val = (1 << 29);
277 int64_t
bits = (1 << 2) + 1;
282TEST(BitfieldTest, ReverseBits8Bit)
284 uint8_t value = (1 << 7);
288TEST(BitfieldTest, ReverseBits64Bit)
290 uint64_t value = 0xF0F0F0F0F0F0F0F1;
299 uint64_t
val = (1 << 29) + (1 << 1);
305 uint64_t
val = (1ULL << 63) + (1ULL << 60) + (1 << 1);
317 uint64_t
val = (1ULL << 63) + (1 << 1);
319 EXPECT_EQ(1, findLsbSetFallback(
val));
327TEST(BitfieldTest, FindLsbGeneralized)
329 static constexpr size_t N{1000};
330 std::bitset<N>
bs{0};
332 for (
size_t i{0};
i < N ; ++
i) {
333 bs = std::bitset<N>{1} <<
i;
337 const auto leadingOne = std::bitset<N>{1} << (N-1);
338 for (
size_t i{0};
i < N ; ++
i) {
339 bs = leadingOne | (std::bitset<N>{1} <<
i);
348TEST(BitfieldTest, PopCountNoBits)
353TEST(BitfieldTest, PopCountOneBit)
355 int64_t
val = (1 << 9);
359TEST(BitfieldTest, PopCountManyBits)
361 int64_t
val = (1 << 22) + (1 << 21) + (1 << 15) + (1 << 9) + 1;
365TEST(BitfieldTest, PopCountAllOnes)
367 int64_t
val = 0xFFFFFFFFFFFFFFFF;
376TEST(BitfieldTest, AlignToPowerOfTwo0)
381TEST(BitfieldTest, AlignToPowerOfTwo3)
386TEST(BitfieldTest, AlignToPowerOfTwo5)
391TEST(BitfieldTest, AlignToPowerOfTwo10)
396TEST(BitfieldTest, AlignToPowerOfTwo16)
401TEST(BitfieldTest, AlignToPowerOfTwo31)
412TEST(BitfieldTest, CountTrailingZeros32BitsNoTrailing)
415 EXPECT_EQ(0,
ctz32(value));
418TEST(BitfieldTest, CountTrailingZeros32Bits)
420 uint32_t value = (1 << 30) + (1 << 29);
421 EXPECT_EQ(29,
ctz32(value));
424TEST(BitfieldTest, CountTrailingZeros64BitsNoTrailing)
426 uint64_t value = (1 << 29) + 1;
427 EXPECT_EQ(0,
ctz64(value));
430TEST(BitfieldTest, CountTrailingZeros64Bits)
432 uint64_t value = 1ULL << 63;
433 EXPECT_EQ(63,
ctz64(value));
436TEST(BitfieldTest, CountTrailingZero64AllZeros)
439 EXPECT_EQ(64,
ctz64(value));
448TEST(BitfieldTest, CountLeadingZeros32BitsNoTrailing)
451 EXPECT_EQ(31,
clz32(value));
454TEST(BitfieldTest, CountLeadingZeros32Bits)
456 uint32_t value = (1 << 30) + (1 << 29);
457 EXPECT_EQ(1,
clz32(value));
460TEST(BitfieldTest, CountLeadingZeros64BitsNoTrailing)
462 uint64_t value = (1 << 29) + 1;
463 EXPECT_EQ(34,
clz64(value));
466TEST(BitfieldTest, CountLeadingZeros64Bits)
468 uint64_t value = 1ULL << 63;
469 EXPECT_EQ(0,
clz64(value));
472TEST(BitfieldTest, CountLeadingZero64AllZeros)
475 EXPECT_EQ(64,
clz64(value));
TEST(BitfieldTest, Mask0Bits)
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 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.
static void output(const char *filename)