Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions llvm/include/llvm/ADT/Bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@ class Bitset {
static constexpr unsigned NumWords =
(NumBits + BitwordBits - 1) / BitwordBits;

protected:
using StorageType = std::array<BitWord, NumWords>;

private:
StorageType Bits{};

protected:
constexpr Bitset(const StorageType &B) : Bits{B} {}
constexpr Bitset(const std::array<uint64_t, (NumBits + 63) / 64> &B) {
if constexpr (sizeof(BitWord) == sizeof(uint64_t)) {
for (size_t I = 0; I != B.size(); ++I)
Bits[I] = B[I];
} else {
for (size_t I = 0; I != B.size(); ++I) {
uint64_t Elt = B[I];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we static assert that sizeof(BitWord) == sizeof(uint32_t);?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already static_asserted above, I guess I could move the assertion down

Bits[2 * I] = static_cast<uint32_t>(Elt);
Bits[2 * I + 1] = static_cast<uint32_t>(Elt >> 32);
}
}
}

public:
constexpr Bitset() = default;
Expand Down
44 changes: 44 additions & 0 deletions llvm/unittests/ADT/BitsetTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===- llvm/unittest/Support/BitsetTest.cpp -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Bitset.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

template <unsigned NumBits>
class TestBitsetUInt64Array : public Bitset<NumBits> {
static constexpr unsigned NumElts = (NumBits + 63) / 64;

public:
TestBitsetUInt64Array(const std::array<uint64_t, NumElts> &B)
: Bitset<NumBits>(B) {}

bool verifyValue(const std::array<uint64_t, NumElts> &B) const {
for (unsigned I = 0; I != NumBits; ++I) {
bool ReferenceVal =
(B[(I / 64)] & (static_cast<uint64_t>(1) << (I % 64))) != 0;
if (ReferenceVal != this->test(I))
return false;
}

return true;
}
};

TEST(BitsetTest, Construction) {
std::array<uint64_t, 2> TestVals = {0x123456789abcdef3, 0x1337d3a0b22c24};
TestBitsetUInt64Array<96> Test(TestVals);
EXPECT_TRUE(Test.verifyValue(TestVals));

TestBitsetUInt64Array<65> Test1(TestVals);
EXPECT_TRUE(Test1.verifyValue(TestVals));
}
} // namespace
1 change: 1 addition & 0 deletions llvm/unittests/ADT/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_llvm_unittest(ADTTests
BitFieldsTest.cpp
BitmaskEnumTest.cpp
BitTest.cpp
BitsetTest.cpp
BitVectorTest.cpp
BreadthFirstIteratorTest.cpp
BumpPtrListTest.cpp
Expand Down