Skip to content

Commit a16477a

Browse files
authored
ADT: Add constructor from uint64_t array for Bitset (#162703)
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 4df4b36 commit a16477a

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

llvm/include/llvm/ADT/Bitset.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,22 @@ class Bitset {
3838
static constexpr unsigned NumWords =
3939
(NumBits + BitwordBits - 1) / BitwordBits;
4040

41-
protected:
4241
using StorageType = std::array<BitWord, NumWords>;
43-
44-
private:
4542
StorageType Bits{};
4643

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

5058
public:
5159
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)