From ff5bad078782b6472d6cd0974bf08fe3473050e6 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Tue, 17 Jan 2012 01:24:32 +0000 Subject: Add portable bit mask operations to BitVector. BitVector uses the native word size for its internal representation. That doesn't work well for literal bit masks in source code. This patch adds BitVector operations to efficiently apply literal bit masks specified as arrays of uint32_t. Since each array entry always holds exactly 32 bits, these portable bit masks can be source code literals, probably produced by TableGen. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148272 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/BitVectorTest.cpp | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'unittests/ADT/BitVectorTest.cpp') diff --git a/unittests/ADT/BitVectorTest.cpp b/unittests/ADT/BitVectorTest.cpp index fa663121a8..f733e13fdf 100644 --- a/unittests/ADT/BitVectorTest.cpp +++ b/unittests/ADT/BitVectorTest.cpp @@ -196,6 +196,52 @@ TEST(BitVectorTest, ProxyIndex) { EXPECT_TRUE(Vec.none()); } +TEST(BitVectorTest, PortableBitMask) { + BitVector A; + const uint32_t Mask1[] = { 0x80000000, 6, 5 }; + + A.resize(10); + A.setBitsInMask(Mask1, 3); + EXPECT_EQ(10u, A.size()); + EXPECT_FALSE(A.test(0)); + + A.resize(32); + A.setBitsInMask(Mask1, 3); + EXPECT_FALSE(A.test(0)); + EXPECT_TRUE(A.test(31)); + EXPECT_EQ(1u, A.count()); + + A.resize(33); + A.setBitsInMask(Mask1, 1); + EXPECT_EQ(1u, A.count()); + A.setBitsInMask(Mask1, 2); + EXPECT_EQ(1u, A.count()); + + A.resize(34); + A.setBitsInMask(Mask1, 2); + EXPECT_EQ(2u, A.count()); + + A.resize(65); + A.setBitsInMask(Mask1, 3); + EXPECT_EQ(4u, A.count()); + + A.setBitsNotInMask(Mask1, 1); + EXPECT_EQ(32u+3u, A.count()); + + A.setBitsNotInMask(Mask1, 3); + EXPECT_EQ(65u, A.count()); + + A.resize(96); + EXPECT_EQ(65u, A.count()); + + A.clear(); + A.resize(128); + A.setBitsNotInMask(Mask1, 3); + EXPECT_EQ(96u-5u, A.count()); + + A.clearBitsNotInMask(Mask1, 1); + EXPECT_EQ(64-4u, A.count()); +} } #endif -- cgit v1.2.3-70-g09d2