[llvm] r183521 - Optimize BitVector::all().
Benjamin Kramer
benny.kra at googlemail.com
Fri Jun 7 07:14:38 PDT 2013
Author: d0k
Date: Fri Jun 7 09:14:38 2013
New Revision: 183521
URL: http://llvm.org/viewvc/llvm-project?rev=183521&view=rev
Log:
Optimize BitVector::all().
Modified:
llvm/trunk/include/llvm/ADT/BitVector.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=183521&r1=183520&r2=183521&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/BitVector.h (original)
+++ llvm/trunk/include/llvm/ADT/BitVector.h Fri Jun 7 09:14:38 2013
@@ -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.
Modified: llvm/trunk/unittests/ADT/BitVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/BitVectorTest.cpp?rev=183521&r1=183520&r2=183521&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/BitVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/BitVectorTest.cpp Fri Jun 7 09:14:38 2013
@@ -141,6 +141,14 @@ TYPED_TEST(BitVectorTest, TrivialOperati
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());
More information about the llvm-commits
mailing list