Skip to content

Commit

Permalink
Simplify SmallBitVector::applyMask by consolidating common code for 3…
Browse files Browse the repository at this point in the history
…2- and 64-bit builds

and assert when mask is too large to apply in the small case,
previously the extra words were silently ignored.
clang-format the entire function to match current code standards.

This is a rewrite of r247972 which was reverted in r247983 due to
warning and possible UB on 32-bits hosts.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247993 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
yrnkrn committed Sep 18, 2015
1 parent 45f4130 commit 81a5ddc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions include/llvm/ADT/SmallBitVector.h
Expand Up @@ -551,19 +551,18 @@ class SmallBitVector {
}

private:
template<bool AddBits, bool InvertMask>
template <bool AddBits, bool InvertMask>
void applyMask(const uint32_t *Mask, unsigned MaskWords) {
if (NumBaseBits == 64 && MaskWords >= 2) {
uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
if (InvertMask) M = ~M;
if (AddBits) setSmallBits(getSmallBits() | M);
else setSmallBits(getSmallBits() & ~M);
} else {
uint32_t M = Mask[0];
if (InvertMask) M = ~M;
if (AddBits) setSmallBits(getSmallBits() | M);
else setSmallBits(getSmallBits() & ~M);
}
assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
uintptr_t M = Mask[0];
if (NumBaseBits == 64)
M |= uint64_t(Mask[1]) << 32;
if (InvertMask)
M = ~M;
if (AddBits)
setSmallBits(getSmallBits() | M);
else
setSmallBits(getSmallBits() & ~M);
}
};

Expand Down
4 changes: 2 additions & 2 deletions unittests/ADT/BitVectorTest.cpp
Expand Up @@ -235,12 +235,12 @@ TYPED_TEST(BitVectorTest, PortableBitMask) {
const uint32_t Mask1[] = { 0x80000000, 6, 5 };

A.resize(10);
A.setBitsInMask(Mask1, 3);
A.setBitsInMask(Mask1, 1);
EXPECT_EQ(10u, A.size());
EXPECT_FALSE(A.test(0));

A.resize(32);
A.setBitsInMask(Mask1, 3);
A.setBitsInMask(Mask1, 1);
EXPECT_FALSE(A.test(0));
EXPECT_TRUE(A.test(31));
EXPECT_EQ(1u, A.count());
Expand Down

0 comments on commit 81a5ddc

Please sign in to comment.