diff options
author | yazevnul <yazevnul@yandex-team.ru> | 2022-02-10 16:46:48 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:48 +0300 |
commit | 9abfb1a53b7f7b791444d1378e645d8fad9b06ed (patch) | |
tree | 49e222ea1c5804306084bb3ae065bb702625360f /util/generic/bitops_ut.cpp | |
parent | 8cbc307de0221f84c80c42dcbe07d40727537e2c (diff) | |
download | ydb-9abfb1a53b7f7b791444d1378e645d8fad9b06ed.tar.gz |
Restoring authorship annotation for <yazevnul@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/generic/bitops_ut.cpp')
-rw-r--r-- | util/generic/bitops_ut.cpp | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/util/generic/bitops_ut.cpp b/util/generic/bitops_ut.cpp index 4a1a6b799e..d23c2b5c27 100644 --- a/util/generic/bitops_ut.cpp +++ b/util/generic/bitops_ut.cpp @@ -4,45 +4,45 @@ #include <util/string/builder.h> -template <typename T> -static void TestCTZ() { - for (unsigned int i = 0; i < (sizeof(T) << 3); ++i) { - UNIT_ASSERT_VALUES_EQUAL(CountTrailingZeroBits(T(1) << i), i); - } -} - -template <typename T> -static void TestFastClp2ForEachPowerOf2() { - for (size_t i = 0; i < sizeof(T) * 8 - 1; ++i) { - const auto current = T(1) << i; - UNIT_ASSERT_VALUES_EQUAL(FastClp2(current), current); - } - - UNIT_ASSERT_VALUES_EQUAL(FastClp2(T(1)), T(1)); - for (size_t i = 1; i < sizeof(T) * 8 - 1; ++i) { - for (size_t j = 0; j < i; ++j) { - const auto value = (T(1) << i) | (T(1) << j); - const auto next = T(1) << (i + 1); - UNIT_ASSERT_VALUES_EQUAL(FastClp2(value), next); - } - } -} - -template <typename T> -static T ReverseBitsSlow(T v) { - T r = v; // r will be reversed bits of v; first get LSB of v - ui32 s = sizeof(v) * 8 - 1; // extra shift needed at end - - for (v >>= 1; v; v >>= 1) { - r <<= 1; - r |= v & 1; +template <typename T> +static void TestCTZ() { + for (unsigned int i = 0; i < (sizeof(T) << 3); ++i) { + UNIT_ASSERT_VALUES_EQUAL(CountTrailingZeroBits(T(1) << i), i); + } +} + +template <typename T> +static void TestFastClp2ForEachPowerOf2() { + for (size_t i = 0; i < sizeof(T) * 8 - 1; ++i) { + const auto current = T(1) << i; + UNIT_ASSERT_VALUES_EQUAL(FastClp2(current), current); + } + + UNIT_ASSERT_VALUES_EQUAL(FastClp2(T(1)), T(1)); + for (size_t i = 1; i < sizeof(T) * 8 - 1; ++i) { + for (size_t j = 0; j < i; ++j) { + const auto value = (T(1) << i) | (T(1) << j); + const auto next = T(1) << (i + 1); + UNIT_ASSERT_VALUES_EQUAL(FastClp2(value), next); + } + } +} + +template <typename T> +static T ReverseBitsSlow(T v) { + T r = v; // r will be reversed bits of v; first get LSB of v + ui32 s = sizeof(v) * 8 - 1; // extra shift needed at end + + for (v >>= 1; v; v >>= 1) { + r <<= 1; + r |= v & 1; --s; - } - - r <<= s; // shift when v's highest bits are zero - return r; -} - + } + + r <<= s; // shift when v's highest bits are zero + return r; +} + // DO_NOT_STYLE Y_UNIT_TEST_SUITE(TBitOpsTest) { Y_UNIT_TEST(TestCountTrailingZeroBits) { @@ -71,7 +71,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { TestFastClp2ForEachPowerOf2<unsigned long>(); TestFastClp2ForEachPowerOf2<unsigned long long>(); } - + Y_UNIT_TEST(TestMask) { for (ui32 i = 0; i < 64; ++i) { UNIT_ASSERT_VALUES_EQUAL(MaskLowerBits(i), (ui64{1} << i) - 1); @@ -79,8 +79,8 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(MaskLowerBits(i, i / 2), (ui64{1} << i) - 1 << (i / 2)); UNIT_ASSERT_VALUES_EQUAL(InverseMaskLowerBits(i, i / 2), ~MaskLowerBits(i, i / 2)); } - } - + } + Y_UNIT_TEST(TestMostSignificantBit) { static_assert(MostSignificantBitCT(0) == 0, "."); static_assert(MostSignificantBitCT(1) == 0, "."); @@ -93,8 +93,8 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { for (ui32 i = 0; i < 63; ++i) { UNIT_ASSERT_VALUES_EQUAL(i + 1, MostSignificantBit(ui64{3} << i)); } - } - + } + Y_UNIT_TEST(TestLeastSignificantBit) { for (ui32 i = 0; i < 64; ++i) { UNIT_ASSERT_VALUES_EQUAL(i, LeastSignificantBit(ui64{1} << i)); @@ -112,13 +112,13 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { Y_UNIT_TEST(TestCeilLog2) { UNIT_ASSERT_VALUES_EQUAL(CeilLog2(ui64{1}), 1); - + for (ui32 i = 2; i < 64; ++i) { UNIT_ASSERT_VALUES_EQUAL(CeilLog2(ui64{1} << i), i); UNIT_ASSERT_VALUES_EQUAL(CeilLog2((ui64{1} << i) | ui64{1}), i + 1); } - } - + } + Y_UNIT_TEST(TestReverse) { for (ui64 i = 0; i < 0x100; ++i) { UNIT_ASSERT_VALUES_EQUAL(ReverseBits((ui8)i), ReverseBitsSlow((ui8)i)); @@ -129,23 +129,23 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(ReverseBits((ui32)~i), ReverseBitsSlow((ui32)~i)); UNIT_ASSERT_VALUES_EQUAL(ReverseBits((ui64)~i), ReverseBitsSlow((ui64)~i)); } - + ui32 v = 0xF0F0F0F0; // 11110000111100001111000011110000 for (ui32 i = 0; i < 4; ++i) { UNIT_ASSERT_VALUES_EQUAL(ReverseBits(v, i + 1), v); UNIT_ASSERT_VALUES_EQUAL(ReverseBits(v, 4 + 2 * i, 4 - i), v); } - + UNIT_ASSERT_VALUES_EQUAL(ReverseBits(v, 8), 0xF0F0F00Fu); UNIT_ASSERT_VALUES_EQUAL(ReverseBits(v, 8, 4), 0xF0F0FF00u); - + for (ui32 i = 0; i < 0x10000; ++i) { for (ui32 j = 0; j <= 32; ++j) { UNIT_ASSERT_VALUES_EQUAL_C(i, ReverseBits(ReverseBits(i, j), j), (TString)(TStringBuilder() << i << " " << j)); } - } - } - + } + } + Y_UNIT_TEST(TestRotateBitsLeft) { static_assert(RotateBitsLeftCT<ui8>(0b00000000u, 0) == 0b00000000u, ""); static_assert(RotateBitsLeftCT<ui8>(0b00000001u, 0) == 0b00000001u, ""); @@ -155,7 +155,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsLeftCT<ui8>(0b00000101u, 1) == 0b00001010u, ""); static_assert(RotateBitsLeftCT<ui8>(0b10100000u, 1) == 0b01000001u, ""); static_assert(RotateBitsLeftCT<ui8>(0b10000000u, 7) == 0b01000000u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui8>(0b00000000u, 0), 0b00000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui8>(0b00000001u, 0), 0b00000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui8>(0b10000000u, 0), 0b10000000u); @@ -164,7 +164,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui8>(0b00000101u, 1), 0b00001010u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui8>(0b10100000u, 1), 0b01000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui8>(0b10000000u, 7), 0b01000000u); - + static_assert(RotateBitsLeftCT<ui16>(0b0000000000000000u, 0) == 0b0000000000000000u, ""); static_assert(RotateBitsLeftCT<ui16>(0b0000000000000001u, 0) == 0b0000000000000001u, ""); static_assert(RotateBitsLeftCT<ui16>(0b1000000000000000u, 0) == 0b1000000000000000u, ""); @@ -173,7 +173,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsLeftCT<ui16>(0b0000000000000101u, 1) == 0b0000000000001010u, ""); static_assert(RotateBitsLeftCT<ui16>(0b1010000000000000u, 1) == 0b0100000000000001u, ""); static_assert(RotateBitsLeftCT<ui16>(0b1000000000000000u, 15) == 0b0100000000000000u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui16>(0b0000000000000000u, 0), 0b0000000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui16>(0b0000000000000001u, 0), 0b0000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui16>(0b1000000000000000u, 0), 0b1000000000000000u); @@ -182,7 +182,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui16>(0b0000000000000101u, 1), 0b0000000000001010u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui16>(0b1010000000000000u, 1), 0b0100000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui16>(0b1000000000000000u, 15), 0b0100000000000000u); - + static_assert(RotateBitsLeftCT<ui32>(0b00000000000000000000000000000000u, 0) == 0b00000000000000000000000000000000u, ""); static_assert(RotateBitsLeftCT<ui32>(0b00000000000000000000000000000001u, 0) == 0b00000000000000000000000000000001u, ""); static_assert(RotateBitsLeftCT<ui32>(0b10000000000000000000000000000000u, 0) == 0b10000000000000000000000000000000u, ""); @@ -191,7 +191,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsLeftCT<ui32>(0b00000000000000000000000000000101u, 1) == 0b00000000000000000000000000001010u, ""); static_assert(RotateBitsLeftCT<ui32>(0b10100000000000000000000000000000u, 1) == 0b01000000000000000000000000000001u, ""); static_assert(RotateBitsLeftCT<ui32>(0b10000000000000000000000000000000u, 31) == 0b01000000000000000000000000000000u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui32>(0b00000000000000000000000000000000u, 0), 0b00000000000000000000000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui32>(0b00000000000000000000000000000001u, 0), 0b00000000000000000000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui32>(0b10000000000000000000000000000000u, 0), 0b10000000000000000000000000000000u); @@ -200,7 +200,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui32>(0b00000000000000000000000000000101u, 1), 0b00000000000000000000000000001010u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui32>(0b10100000000000000000000000000000u, 1), 0b01000000000000000000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui32>(0b10000000000000000000000000000000u, 31), 0b01000000000000000000000000000000u); - + static_assert(RotateBitsLeftCT<ui64>(0b0000000000000000000000000000000000000000000000000000000000000000u, 0) == 0b0000000000000000000000000000000000000000000000000000000000000000u, ""); static_assert(RotateBitsLeftCT<ui64>(0b0000000000000000000000000000000000000000000000000000000000000001u, 0) == 0b0000000000000000000000000000000000000000000000000000000000000001u, ""); static_assert(RotateBitsLeftCT<ui64>(0b1000000000000000000000000000000000000000000000000000000000000000u, 0) == 0b1000000000000000000000000000000000000000000000000000000000000000u, ""); @@ -209,7 +209,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsLeftCT<ui64>(0b0000000000000000000000000000000000000000000000000000000000000101u, 1) == 0b0000000000000000000000000000000000000000000000000000000000001010u, ""); static_assert(RotateBitsLeftCT<ui64>(0b1010000000000000000000000000000000000000000000000000000000000000u, 1) == 0b0100000000000000000000000000000000000000000000000000000000000001u, ""); static_assert(RotateBitsLeftCT<ui64>(0b1000000000000000000000000000000000000000000000000000000000000000u, 63) == 0b0100000000000000000000000000000000000000000000000000000000000000u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui64>(0b0000000000000000000000000000000000000000000000000000000000000000u, 0), 0b0000000000000000000000000000000000000000000000000000000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui64>(0b0000000000000000000000000000000000000000000000000000000000000001u, 0), 0b0000000000000000000000000000000000000000000000000000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui64>(0b1000000000000000000000000000000000000000000000000000000000000000u, 0), 0b1000000000000000000000000000000000000000000000000000000000000000u); @@ -219,7 +219,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui64>(0b1010000000000000000000000000000000000000000000000000000000000000u, 1), 0b0100000000000000000000000000000000000000000000000000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsLeft<ui64>(0b1000000000000000000000000000000000000000000000000000000000000000u, 63), 0b0100000000000000000000000000000000000000000000000000000000000000u); } - + Y_UNIT_TEST(TestRotateBitsRight) { static_assert(RotateBitsRightCT<ui8>(0b00000000u, 0) == 0b00000000u, ""); static_assert(RotateBitsRightCT<ui8>(0b00000001u, 0) == 0b00000001u, ""); @@ -229,7 +229,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsRightCT<ui8>(0b00000101u, 1) == 0b10000010u, ""); static_assert(RotateBitsRightCT<ui8>(0b10100000u, 1) == 0b01010000u, ""); static_assert(RotateBitsRightCT<ui8>(0b00000001u, 7) == 0b00000010u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui8>(0b00000000u, 0), 0b00000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui8>(0b00000001u, 0), 0b00000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui8>(0b10000000u, 0), 0b10000000u); @@ -238,7 +238,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui8>(0b00000101u, 1), 0b10000010u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui8>(0b10100000u, 1), 0b01010000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui8>(0b00000001u, 7), 0b00000010u); - + static_assert(RotateBitsRightCT<ui16>(0b0000000000000000u, 0) == 0b0000000000000000u, ""); static_assert(RotateBitsRightCT<ui16>(0b0000000000000001u, 0) == 0b0000000000000001u, ""); static_assert(RotateBitsRightCT<ui16>(0b1000000000000000u, 0) == 0b1000000000000000u, ""); @@ -247,7 +247,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsRightCT<ui16>(0b0000000000000101u, 1) == 0b1000000000000010u, ""); static_assert(RotateBitsRightCT<ui16>(0b1010000000000000u, 1) == 0b0101000000000000u, ""); static_assert(RotateBitsRightCT<ui16>(0b0000000000000001u, 15) == 0b0000000000000010u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui16>(0b0000000000000000u, 0), 0b0000000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui16>(0b0000000000000001u, 0), 0b0000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui16>(0b1000000000000000u, 0), 0b1000000000000000u); @@ -256,7 +256,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui16>(0b0000000000000101u, 1), 0b1000000000000010u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui16>(0b1010000000000000u, 1), 0b0101000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui16>(0b0000000000000001u, 15), 0b0000000000000010u); - + static_assert(RotateBitsRightCT<ui32>(0b00000000000000000000000000000000u, 0) == 0b00000000000000000000000000000000u, ""); static_assert(RotateBitsRightCT<ui32>(0b00000000000000000000000000000001u, 0) == 0b00000000000000000000000000000001u, ""); static_assert(RotateBitsRightCT<ui32>(0b10000000000000000000000000000000u, 0) == 0b10000000000000000000000000000000u, ""); @@ -265,7 +265,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsRightCT<ui32>(0b00000000000000000000000000000101u, 1) == 0b10000000000000000000000000000010u, ""); static_assert(RotateBitsRightCT<ui32>(0b10100000000000000000000000000000u, 1) == 0b01010000000000000000000000000000u, ""); static_assert(RotateBitsRightCT<ui32>(0b00000000000000000000000000000001u, 31) == 0b00000000000000000000000000000010u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui32>(0b00000000000000000000000000000000u, 0), 0b00000000000000000000000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui32>(0b00000000000000000000000000000001u, 0), 0b00000000000000000000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui32>(0b10000000000000000000000000000000u, 0), 0b10000000000000000000000000000000u); @@ -274,7 +274,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui32>(0b00000000000000000000000000000101u, 1), 0b10000000000000000000000000000010u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui32>(0b10100000000000000000000000000000u, 1), 0b01010000000000000000000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui32>(0b00000000000000000000000000000001u, 31), 0b00000000000000000000000000000010u); - + static_assert(RotateBitsRightCT<ui64>(0b0000000000000000000000000000000000000000000000000000000000000000u, 0) == 0b0000000000000000000000000000000000000000000000000000000000000000u, ""); static_assert(RotateBitsRightCT<ui64>(0b0000000000000000000000000000000000000000000000000000000000000001u, 0) == 0b0000000000000000000000000000000000000000000000000000000000000001u, ""); static_assert(RotateBitsRightCT<ui64>(0b1000000000000000000000000000000000000000000000000000000000000000u, 0) == 0b1000000000000000000000000000000000000000000000000000000000000000u, ""); @@ -283,7 +283,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { static_assert(RotateBitsRightCT<ui64>(0b0000000000000000000000000000000000000000000000000000000000000101u, 1) == 0b1000000000000000000000000000000000000000000000000000000000000010u, ""); static_assert(RotateBitsRightCT<ui64>(0b1010000000000000000000000000000000000000000000000000000000000000u, 1) == 0b0101000000000000000000000000000000000000000000000000000000000000u, ""); static_assert(RotateBitsRightCT<ui64>(0b0000000000000000000000000000000000000000000000000000000000000001u, 63) == 0b0000000000000000000000000000000000000000000000000000000000000010u, ""); - + UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui64>(0b0000000000000000000000000000000000000000000000000000000000000000u, 0), 0b0000000000000000000000000000000000000000000000000000000000000000u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui64>(0b0000000000000000000000000000000000000000000000000000000000000001u, 0), 0b0000000000000000000000000000000000000000000000000000000000000001u); UNIT_ASSERT_VALUES_EQUAL(RotateBitsRight<ui64>(0b1000000000000000000000000000000000000000000000000000000000000000u, 0), 0b1000000000000000000000000000000000000000000000000000000000000000u); |