aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/MathExtrasTest.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2017-04-19 15:45:31 +0000
committerZachary Turner <zturner@google.com>2017-04-19 15:45:31 +0000
commitf19b0c7f6be6928cbea4f8681b99a259519138f2 (patch)
tree4fb401aae3f76df4a65fd33b360706563a0d284e /llvm/unittests/Support/MathExtrasTest.cpp
parent13c0c12b66ed6d36ceebc819a1798ff471e60768 (diff)
downloadllvm-f19b0c7f6be6928cbea4f8681b99a259519138f2.zip
llvm-f19b0c7f6be6928cbea4f8681b99a259519138f2.tar.gz
llvm-f19b0c7f6be6928cbea4f8681b99a259519138f2.tar.bz2
[Support] Add some helpers to generate bitmasks.
Frequently you you want a bitmask consisting of a specified number of 1s, either at the beginning or end of a word. The naive way to do this is to write template<typename T> T leadingBitMask(unsigned N) { return (T(1) << N) - 1; } but using this function you cannot produce a word with every bit set to 1 (i.e. leadingBitMask<uint8_t>(8)) because left shift is undefined when N is greater than or equal to the number of bits in the word. This patch provides an efficient, branch-free implementation that works for all values of N in [0, CHAR_BIT*sizeof(T)] Differential Revision: https://reviews.llvm.org/D32212 llvm-svn: 300710
Diffstat (limited to 'llvm/unittests/Support/MathExtrasTest.cpp')
-rw-r--r--llvm/unittests/Support/MathExtrasTest.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp
index b2c3779..de82721 100644
--- a/llvm/unittests/Support/MathExtrasTest.cpp
+++ b/llvm/unittests/Support/MathExtrasTest.cpp
@@ -66,6 +66,26 @@ TEST(MathExtras, countLeadingZeros) {
}
}
+TEST(MathExtras, onesMask) {
+ EXPECT_EQ(0U, maskLeadingOnes<uint8_t>(0));
+ EXPECT_EQ(0U, maskTrailingOnes<uint8_t>(0));
+ EXPECT_EQ(0U, maskLeadingOnes<uint16_t>(0));
+ EXPECT_EQ(0U, maskTrailingOnes<uint16_t>(0));
+ EXPECT_EQ(0U, maskLeadingOnes<uint32_t>(0));
+ EXPECT_EQ(0U, maskTrailingOnes<uint32_t>(0));
+ EXPECT_EQ(0U, maskLeadingOnes<uint64_t>(0));
+ EXPECT_EQ(0U, maskTrailingOnes<uint64_t>(0));
+
+ EXPECT_EQ(0x00000003U, maskTrailingOnes<uint32_t>(2U));
+ EXPECT_EQ(0xC0000000U, maskLeadingOnes<uint32_t>(2U));
+
+ EXPECT_EQ(0x000007FFU, maskTrailingOnes<uint32_t>(11U));
+ EXPECT_EQ(0xFFE00000U, maskLeadingOnes<uint32_t>(11U));
+
+ EXPECT_EQ(0xFFFFFFFFU, maskTrailingOnes<uint32_t>(32U));
+ EXPECT_EQ(0xFFFFFFFFU, maskLeadingOnes<uint32_t>(32U));
+}
+
TEST(MathExtras, findFirstSet) {
uint8_t Z8 = 0;
uint16_t Z16 = 0;