Skip to content

Commit e89ba3c

Browse files
committed
ADT: Add constructor from uint64_t array for Bitset
Avoids exposing the implementation detail of uintptr_t to the constructor. This is a replacement of b738f63 which avoids needing tablegen to know the underlying storage type.
1 parent 8c9c91f commit e89ba3c

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

llvm/include/llvm/ADT/Bitset.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,18 @@ class Bitset {
4545
StorageType Bits{};
4646

4747
protected:
48-
constexpr Bitset(const StorageType &B) : Bits{B} {}
48+
constexpr Bitset(const std::array<uint64_t, (NumBits + 63) / 64> &B) {
49+
if (sizeof(BitWord) == sizeof(uint64_t)) {
50+
for (size_t I = 0; I != B.size(); ++I)
51+
Bits[I] = B[I];
52+
} else {
53+
for (size_t I = 0; I != B.size(); ++I) {
54+
uint64_t Elt = B[I];
55+
Bits[2 * I] = static_cast<uint32_t>(Elt);
56+
Bits[2 * I + 1] = static_cast<uint32_t>(Elt >> 32);
57+
}
58+
}
59+
}
4960

5061
public:
5162
constexpr Bitset() = default;

llvm/unittests/ADT/BitsetTest.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===- llvm/unittest/Support/BitsetTest.cpp -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ADT/Bitset.h"
10+
#include "gtest/gtest.h"
11+
12+
using namespace llvm;
13+
14+
namespace {
15+
16+
template <unsigned NumBits>
17+
class TestBitsetUInt64Array : public Bitset<NumBits> {
18+
static constexpr unsigned NumElts = (NumBits + 63) / 64;
19+
20+
public:
21+
TestBitsetUInt64Array(const std::array<uint64_t, NumElts> &B)
22+
: Bitset<NumBits>(B) {}
23+
24+
bool verifyValue(const std::array<uint64_t, NumElts> &B) const {
25+
for (unsigned I = 0; I != NumBits; ++I) {
26+
bool ReferenceVal =
27+
(B[(I / 64)] & (static_cast<uint64_t>(1) << (I % 64))) != 0;
28+
if (ReferenceVal != this->test(I))
29+
return false;
30+
}
31+
32+
return true;
33+
}
34+
};
35+
36+
TEST(BitsetTest, Construction) {
37+
std::array<uint64_t, 2> TestVals = {0x123456789abcdef3, 0x1337d3a0b22c24};
38+
TestBitsetUInt64Array<96> Test(TestVals);
39+
EXPECT_TRUE(Test.verifyValue(TestVals));
40+
41+
TestBitsetUInt64Array<65> Test1(TestVals);
42+
EXPECT_TRUE(Test1.verifyValue(TestVals));
43+
}
44+
} // namespace

llvm/unittests/ADT/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_llvm_unittest(ADTTests
1212
BitFieldsTest.cpp
1313
BitmaskEnumTest.cpp
1414
BitTest.cpp
15+
BitsetTest.cpp
1516
BitVectorTest.cpp
1617
BreadthFirstIteratorTest.cpp
1718
BumpPtrListTest.cpp

0 commit comments

Comments
 (0)