[llvm] r302208 - [ADT] A few minor improvements to BitVector
Zachary Turner via llvm-commits
llvm-commits at lists.llvm.org
Thu May 4 17:19:57 PDT 2017
Author: zturner
Date: Thu May 4 19:19:57 2017
New Revision: 302208
URL: http://llvm.org/viewvc/llvm-project?rev=302208&view=rev
Log:
[ADT] A few minor improvements to BitVector
Fixes some spelling mistakes, uses a helper function, and
adds an additional test case.
Modified:
llvm/trunk/include/llvm/ADT/BitVector.h
llvm/trunk/include/llvm/Support/MathExtras.h
llvm/trunk/unittests/ADT/BitVectorTest.cpp
Modified: llvm/trunk/include/llvm/ADT/BitVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/BitVector.h?rev=302208&r1=302207&r2=302208&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/BitVector.h (original)
+++ llvm/trunk/include/llvm/ADT/BitVector.h Thu May 4 19:19:57 2017
@@ -217,7 +217,7 @@ public:
unsigned BitPos = Prev % BITWORD_SIZE;
BitWord Copy = Bits[WordPos];
// Mask off previous bits.
- Copy &= ~0UL << BitPos;
+ Copy &= maskTrailingZeros<BitWord>(BitPos);
if (Copy != 0)
return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
@@ -229,7 +229,7 @@ public:
return -1;
}
- /// find_next_unset - Returns the index of the next usnet bit following the
+ /// find_next_unset - Returns the index of the next unset bit following the
/// "Prev" bit. Returns -1 if all remaining bits are set.
int find_next_unset(unsigned Prev) const {
++Prev;
Modified: llvm/trunk/include/llvm/Support/MathExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=302208&r1=302207&r2=302208&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Thu May 4 19:19:57 2017
@@ -214,6 +214,18 @@ template <typename T> T maskLeadingOnes(
return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
}
+/// \brief Create a bitmask with the N right-most bits set to 0, and all other
+/// bits set to 1. Only unsigned types are allowed.
+template <typename T> T maskTrailingZeros(unsigned N) {
+ return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
+}
+
+/// \brief Create a bitmask with the N left-most bits set to 0, and all other
+/// bits set to 1. Only unsigned types are allowed.
+template <typename T> T maskLeadingZeros(unsigned N) {
+ return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
+}
+
/// \brief Get the index of the last set bit starting from the least
/// significant bit.
///
Modified: llvm/trunk/unittests/ADT/BitVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/BitVectorTest.cpp?rev=302208&r1=302207&r2=302208&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/BitVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/BitVectorTest.cpp Thu May 4 19:19:57 2017
@@ -227,6 +227,25 @@ TYPED_TEST(BitVectorTest, FindOperations
EXPECT_EQ(-1, A.find_last());
EXPECT_EQ(0, A.find_first_unset());
EXPECT_EQ(99, A.find_last_unset());
+
+ // Also test with a vector that is small enough to fit in 1 word.
+ A.resize(20);
+ A.set(3);
+ A.set(4);
+ A.set(16);
+ EXPECT_EQ(16, A.find_last());
+ EXPECT_EQ(3, A.find_first());
+ EXPECT_EQ(3, A.find_next(1));
+ EXPECT_EQ(4, A.find_next(3));
+ EXPECT_EQ(16, A.find_next(4));
+ EXPECT_EQ(-1, A.find_next(16));
+
+ EXPECT_EQ(0, A.find_first_unset());
+ EXPECT_EQ(19, A.find_last_unset());
+ EXPECT_EQ(5, A.find_next_unset(3));
+ EXPECT_EQ(5, A.find_next_unset(4));
+ EXPECT_EQ(13, A.find_next_unset(12));
+ EXPECT_EQ(17, A.find_next_unset(15));
}
TYPED_TEST(BitVectorTest, CompoundAssignment) {
More information about the llvm-commits
mailing list