diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2013-05-24 20:29:47 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2013-05-24 20:29:47 +0000 |
commit | 36fe3f2b5651882b12e24b49dc7818ebb1a5d79f (patch) | |
tree | af75fb97cce42aa218cbc34686ca1e63c4a5579e /unittests | |
parent | 959ecb2eec9cffd762edc46fd496a7bb6c65a2e6 (diff) | |
download | external_llvm-36fe3f2b5651882b12e24b49dc7818ebb1a5d79f.zip external_llvm-36fe3f2b5651882b12e24b49dc7818ebb1a5d79f.tar.gz external_llvm-36fe3f2b5651882b12e24b49dc7818ebb1a5d79f.tar.bz2 |
[Support] Add type generic bit utilities to MathExtras.h
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182667 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Support/MathExtrasTest.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/unittests/Support/MathExtrasTest.cpp b/unittests/Support/MathExtrasTest.cpp index 0a6724c..5fd638f 100644 --- a/unittests/Support/MathExtrasTest.cpp +++ b/unittests/Support/MathExtrasTest.cpp @@ -14,6 +14,97 @@ using namespace llvm; namespace { +TEST(MathExtras, countTrailingZeros) { + uint8_t Z8 = 0; + uint16_t Z16 = 0; + uint32_t Z32 = 0; + uint64_t Z64 = 0; + EXPECT_EQ(8, countTrailingZeros(Z8)); + EXPECT_EQ(16, countTrailingZeros(Z16)); + EXPECT_EQ(32, countTrailingZeros(Z32)); + EXPECT_EQ(64, countTrailingZeros(Z64)); + + uint8_t NZ8 = 42; + uint16_t NZ16 = 42; + uint32_t NZ32 = 42; + uint64_t NZ64 = 42; + EXPECT_EQ(1, countTrailingZeros(NZ8)); + EXPECT_EQ(1, countTrailingZeros(NZ16)); + EXPECT_EQ(1, countTrailingZeros(NZ32)); + EXPECT_EQ(1, countTrailingZeros(NZ64)); +} + +TEST(MathExtras, countLeadingZeros) { + uint8_t Z8 = 0; + uint16_t Z16 = 0; + uint32_t Z32 = 0; + uint64_t Z64 = 0; + EXPECT_EQ(8, countLeadingZeros(Z8)); + EXPECT_EQ(16, countLeadingZeros(Z16)); + EXPECT_EQ(32, countLeadingZeros(Z32)); + EXPECT_EQ(64, countLeadingZeros(Z64)); + + uint8_t NZ8 = 42; + uint16_t NZ16 = 42; + uint32_t NZ32 = 42; + uint64_t NZ64 = 42; + EXPECT_EQ(2, countLeadingZeros(NZ8)); + EXPECT_EQ(10, countLeadingZeros(NZ16)); + EXPECT_EQ(26, countLeadingZeros(NZ32)); + EXPECT_EQ(58, countLeadingZeros(NZ64)); +} + +TEST(MathExtras, findFirstSet) { + uint8_t Z8 = 0; + uint16_t Z16 = 0; + uint32_t Z32 = 0; + uint64_t Z64 = 0; + EXPECT_EQ(0xFF, findFirstSet(Z8)); + EXPECT_EQ(0xFFFF, findFirstSet(Z16)); + EXPECT_EQ(0xFFFFFFFF, findFirstSet(Z32)); + EXPECT_EQ(0xFFFFFFFFFFFFFFFF, findFirstSet(Z64)); + + uint8_t NZ8 = 42; + uint16_t NZ16 = 42; + uint32_t NZ32 = 42; + uint64_t NZ64 = 42; + EXPECT_EQ(1, findFirstSet(NZ8)); + EXPECT_EQ(1, findFirstSet(NZ16)); + EXPECT_EQ(1, findFirstSet(NZ32)); + EXPECT_EQ(1, findFirstSet(NZ64)); +} + +TEST(MathExtras, findLastSet) { + uint8_t Z8 = 0; + uint16_t Z16 = 0; + uint32_t Z32 = 0; + uint64_t Z64 = 0; + EXPECT_EQ(0xFF, findLastSet(Z8)); + EXPECT_EQ(0xFFFF, findLastSet(Z16)); + EXPECT_EQ(0xFFFFFFFF, findLastSet(Z32)); + EXPECT_EQ(0xFFFFFFFFFFFFFFFF, findLastSet(Z64)); + + uint8_t NZ8 = 42; + uint16_t NZ16 = 42; + uint32_t NZ32 = 42; + uint64_t NZ64 = 42; + EXPECT_EQ(5, findLastSet(NZ8)); + EXPECT_EQ(5, findLastSet(NZ16)); + EXPECT_EQ(5, findLastSet(NZ32)); + EXPECT_EQ(5, findLastSet(NZ64)); +} + +TEST(MathExtras, reverseBits) { + uint8_t NZ8 = 42; + uint16_t NZ16 = 42; + uint32_t NZ32 = 42; + uint64_t NZ64 = 42; + EXPECT_EQ(0x54, reverseBits(NZ8)); + EXPECT_EQ(0x5400, reverseBits(NZ16)); + EXPECT_EQ(0x54000000, reverseBits(NZ32)); + EXPECT_EQ(0x5400000000000000, reverseBits(NZ64)); +} + TEST(MathExtras, isPowerOf2_32) { EXPECT_TRUE(isPowerOf2_32(1 << 6)); EXPECT_TRUE(isPowerOf2_32(1 << 12)); |