[llvm] r366657 - [SmallBitVector] Fix bug in find_next_unset for small types with indices >=32

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 21 09:06:27 PDT 2019


Author: rksimon
Date: Sun Jul 21 09:06:26 2019
New Revision: 366657

URL: http://llvm.org/viewvc/llvm-project?rev=366657&view=rev
Log:
[SmallBitVector] Fix bug in find_next_unset for small types with indices >=32

We were creating a bitmask from a shift of unsigned instead of uintptr_t, meaning we couldn't create masks for indices above 31.

Noticed due to a MSVC analyzer warning.

Modified:
    llvm/trunk/include/llvm/ADT/SmallBitVector.h
    llvm/trunk/unittests/ADT/BitVectorTest.cpp

Modified: llvm/trunk/include/llvm/ADT/SmallBitVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallBitVector.h?rev=366657&r1=366656&r2=366657&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallBitVector.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallBitVector.h Sun Jul 21 09:06:26 2019
@@ -290,7 +290,7 @@ public:
       ++Prev;
       uintptr_t Bits = getSmallBits();
       // Mask in previous bits.
-      uintptr_t Mask = (1 << Prev) - 1;
+      uintptr_t Mask = (uintptr_t(1) << Prev) - 1;
       Bits |= Mask;
 
       if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())

Modified: llvm/trunk/unittests/ADT/BitVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/BitVectorTest.cpp?rev=366657&r1=366656&r2=366657&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/BitVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/BitVectorTest.cpp Sun Jul 21 09:06:26 2019
@@ -228,6 +228,38 @@ TYPED_TEST(BitVectorTest, SimpleFindOpsM
   EXPECT_EQ(99, A.find_next_unset(98));
 }
 
+// Test finding next set and unset bits in a BitVector/SmallBitVector within a
+// uintptr_t - check both 32-bit (Multi) and 64-bit (Small) targets.
+TYPED_TEST(BitVectorTest, SimpleFindOps64Bit) {
+  TypeParam A;
+
+  A.resize(57);
+  A.set(12);
+  A.set(13);
+  A.set(47);
+
+  EXPECT_EQ(47, A.find_last());
+  EXPECT_EQ(12, A.find_first());
+  EXPECT_EQ(13, A.find_next(12));
+  EXPECT_EQ(47, A.find_next(13));
+  EXPECT_EQ(-1, A.find_next(47));
+
+  EXPECT_EQ(-1, A.find_prev(12));
+  EXPECT_EQ(12, A.find_prev(13));
+  EXPECT_EQ(13, A.find_prev(47));
+  EXPECT_EQ(47, A.find_prev(56));
+
+  EXPECT_EQ(0, A.find_first_unset());
+  EXPECT_EQ(56, A.find_last_unset());
+  EXPECT_EQ(14, A.find_next_unset(11));
+  EXPECT_EQ(14, A.find_next_unset(12));
+  EXPECT_EQ(14, A.find_next_unset(13));
+  EXPECT_EQ(16, A.find_next_unset(15));
+  EXPECT_EQ(48, A.find_next_unset(46));
+  EXPECT_EQ(48, A.find_next_unset(47));
+  EXPECT_EQ(-1, A.find_next_unset(56));
+}
+
 // Check if a SmallBitVector is in small mode. This check is used in tests
 // that run for both SmallBitVector and BitVector. This check doesn't apply
 // to BitVector so we provide an overload that returns true to get the tests




More information about the llvm-commits mailing list