Optimize BitVector::all().
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 7 Jun 2013 14:14:38 +0000 (14:14 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 7 Jun 2013 14:14:38 +0000 (14:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183521 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/BitVector.h
unittests/ADT/BitVectorTest.cpp

index 3789db4e45d3a6c9e9efa05ff2438a9f8dfd8e3c..8f512f5c2ca6eed695471ae3326711eeee13e2e3 100644 (file)
@@ -138,8 +138,16 @@ public:
 
   /// all - Returns true if all bits are set.
   bool all() const {
-    // TODO: Optimize this.
-    return count() == size();
+    if (empty())
+      return true;
+
+    for (unsigned i = 0; i < NumBitWords(size()) - 1; ++i)
+      if (Bits[i] != ~0UL)
+        return false;
+
+    // For the last word check that the lower bits are ones. The unused bits are
+    // always zero.
+    return Bits[NumBitWords(size()) - 1] == ~(~0UL << (Size % BITWORD_SIZE));
   }
 
   /// none - Returns true if none of the bits are set.
index dc298a83d571a7ed0c810a05f4b746e8d676733f..f97be22fd2755fbdd84133aca0e1d86eda7236a9 100644 (file)
@@ -141,6 +141,14 @@ TYPED_TEST(BitVectorTest, TrivialOperation) {
   EXPECT_TRUE(Vec.none());
   EXPECT_FALSE(Vec.empty());
 
+  Vec.flip();
+  EXPECT_EQ(130U, Vec.count());
+  EXPECT_EQ(130U, Vec.size());
+  EXPECT_TRUE(Vec.any());
+  EXPECT_TRUE(Vec.all());
+  EXPECT_FALSE(Vec.none());
+  EXPECT_FALSE(Vec.empty());
+
   Inv = TypeParam().flip();
   EXPECT_EQ(0U, Inv.count());
   EXPECT_EQ(0U, Inv.size());