16#ifndef LLVM_ADT_BITSET_H
17#define LLVM_ADT_BITSET_H
30template <
unsigned NumBits>
class Bitset {
31 using BitWord = uintptr_t;
33 static constexpr unsigned BitwordBits =
sizeof(BitWord) * CHAR_BIT;
34 static constexpr unsigned RemainderNumBits = NumBits % BitwordBits;
35 static constexpr BitWord RemainderMask =
36 RemainderNumBits == 0 ? ~BitWord(0)
37 : ((BitWord(1) << RemainderNumBits) - 1);
39 static_assert(BitwordBits == 64 || BitwordBits == 32,
40 "Unsupported word size");
42 static constexpr unsigned NumWords =
43 (NumBits + BitwordBits - 1) / BitwordBits;
48 static constexpr unsigned getLastWordIndex() {
return NumWords - 1; }
50 using StorageType = std::array<BitWord, NumWords>;
53 constexpr void maskLastWord() { Bits[getLastWordIndex()] &= RemainderMask; }
57 const std::array<
uint64_t, (NumBits + 63) / 64> &
B) {
58 if constexpr (
sizeof(BitWord) ==
sizeof(
uint64_t)) {
59 for (
size_t I = 0;
I !=
B.size(); ++
I)
62 unsigned BitsToAssign = NumBits;
63 for (
size_t I = 0;
I !=
B.size() && BitsToAssign; ++
I) {
69 BitsToAssign = BitsToAssign >= 32 ? BitsToAssign - 32 : 0;
82 constexpr const BitWord
AllOnes = ~BitWord(0);
83 for (BitWord &
B : Bits)
90 Bits[
I / BitwordBits] |= BitWord(1) << (
I % BitwordBits);
95 Bits[
I / BitwordBits] &= ~(BitWord(1) << (
I % BitwordBits));
100 Bits[
I / BitwordBits] ^= BitWord(1) << (
I % BitwordBits);
105 BitWord Mask = BitWord(1) << (
I % BitwordBits);
106 return (Bits[
I / BitwordBits] & Mask) != 0;
109 constexpr bool test(
unsigned I)
const {
return (*
this)[
I]; }
111 constexpr size_t size()
const {
return NumBits; }
113 constexpr bool any()
const {
114 for (BitWord
B : Bits)
120 constexpr bool none()
const {
return !
any(); }
122 constexpr bool all()
const {
123 constexpr const BitWord
AllOnes = ~BitWord(0);
124 for (
unsigned I = 0;
I < getLastWordIndex(); ++
I)
127 return Bits[getLastWordIndex()] == RemainderMask;
132 for (BitWord Word : Bits)
138 for (
unsigned I = 0,
E = Bits.size();
I !=
E; ++
I) {
139 Bits[
I] ^=
RHS.Bits[
I];
150 for (
unsigned I = 0,
E = Bits.size();
I !=
E; ++
I)
151 Bits[
I] &=
RHS.Bits[
I];
161 for (
unsigned I = 0,
E = Bits.size();
I !=
E; ++
I) {
162 Bits[
I] |=
RHS.Bits[
I];
174 for (BitWord &
B : Result.Bits)
176 Result.maskLastWord();
181 for (
unsigned I = 0;
I < NumWords; ++
I)
182 if (Bits[
I] !=
RHS.Bits[
I])
190 for (
unsigned I = 0,
E =
size();
I !=
E; ++
I) {
203 const unsigned WordShift =
N / BitwordBits;
204 const unsigned BitShift =
N % BitwordBits;
206 for (
unsigned I = NumWords;
I > WordShift;) {
208 Bits[
I] = Bits[
I - WordShift];
211 const unsigned CarryShift = BitwordBits - BitShift;
212 for (
unsigned I = NumWords - 1;
I > WordShift; --
I) {
213 Bits[
I] = (Bits[
I - WordShift] << BitShift) |
214 (Bits[
I - WordShift - 1] >> CarryShift);
216 Bits[WordShift] = Bits[0] << BitShift;
218 for (
unsigned I = 0;
I < WordShift; ++
I)
235 const unsigned WordShift =
N / BitwordBits;
236 const unsigned BitShift =
N % BitwordBits;
238 for (
unsigned I = 0;
I < NumWords - WordShift; ++
I)
239 Bits[
I] = Bits[
I + WordShift];
241 const unsigned CarryShift = BitwordBits - BitShift;
242 for (
unsigned I = 0;
I < NumWords - WordShift - 1; ++
I) {
243 Bits[
I] = (Bits[
I + WordShift] >> BitShift) |
244 (Bits[
I + WordShift + 1] << CarryShift);
246 Bits[NumWords - WordShift - 1] = Bits[NumWords - 1] >> BitShift;
248 for (
unsigned I = NumWords - WordShift;
I < NumWords; ++
I)
268 if constexpr (BitwordBits == 64) {
272 uint64_t Hi = (2 *
I + 1 < NumWords) ? Bits[2 *
I + 1] : 0;
273 return Lo | (
Hi << 32);
279 for (
unsigned I = NumWords;
I > 0;) {
282 return I * BitwordBits +
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file implements the C++20 <bit> header.
constexpr Bitset operator^(const Bitset &RHS) const
constexpr size_t count() const
constexpr Bitset & set(unsigned I)
constexpr Bitset & flip(unsigned I)
constexpr bool all() const
constexpr Bitset & operator>>=(unsigned N)
constexpr bool any() const
constexpr Bitset(const std::array< uint64_t,(NumBits+63)/64 > &B)
constexpr Bitset()=default
constexpr Bitset & operator<<=(unsigned N)
constexpr bool operator==(const Bitset &RHS) const
constexpr bool operator<(const Bitset &Other) const
constexpr Bitset & operator^=(const Bitset &RHS)
static constexpr unsigned getNumWords64()
Return the number of 64-bit words needed to hold all bits.
constexpr uint64_t getWord64(unsigned I) const
Return the I-th 64-bit word of the bitset, from least significant to most.
constexpr Bitset operator&(const Bitset &RHS) const
constexpr bool none() const
constexpr bool operator!=(const Bitset &RHS) const
constexpr int findLastSet() const
Return the index of the highest set bit, or -1 if no bits are set.
constexpr bool operator[](unsigned I) const
constexpr Bitset operator<<(unsigned N) const
constexpr size_t size() const
constexpr Bitset & operator|=(const Bitset &RHS)
constexpr Bitset(std::initializer_list< unsigned > Init)
constexpr Bitset operator>>(unsigned N) const
constexpr Bitset & operator&=(const Bitset &RHS)
constexpr bool test(unsigned I) const
constexpr Bitset operator|(const Bitset &RHS) const
constexpr Bitset & reset(unsigned I)
constexpr Bitset operator~() const
This is an optimization pass for GlobalISel generic memory operations.
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
constexpr int countl_zero_constexpr(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
FunctionAddr VTableAddr Count