LLVM 22.0.0git
Bitset.h
Go to the documentation of this file.
1//=== llvm/ADT/Bitset.h - constexpr std::bitset -----------------*- C++ -*-===//
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// Defines a std::bitset like container that can be used in constexprs.
10// That constructor and many of the methods are constexpr. std::bitset doesn't
11// get constexpr methods until C++23. This class also provides a constexpr
12// constructor that accepts an initializer_list of bits to set.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ADT_BITSET_H
17#define LLVM_ADT_BITSET_H
18
19#include <llvm/ADT/STLExtras.h>
20#include <array>
21#include <climits>
22#include <cstdint>
23
24namespace llvm {
25
26/// This is a constexpr reimplementation of a subset of std::bitset. It would be
27/// nice to use std::bitset directly, but it doesn't support constant
28/// initialization.
29template <unsigned NumBits>
30class Bitset {
31 typedef uintptr_t BitWord;
32
33 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
34
35 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
36 "Unsupported word size");
37
38 static constexpr unsigned NumWords =
39 (NumBits + BITWORD_SIZE - 1) / BITWORD_SIZE;
40
41protected:
42 using StorageType = std::array<BitWord, NumWords>;
43
44private:
45 StorageType Bits{};
46
47protected:
48 constexpr Bitset(const StorageType &B) : Bits{B} {}
49
50public:
51 constexpr Bitset() = default;
52 constexpr Bitset(std::initializer_list<unsigned> Init) {
53 for (auto I : Init)
54 set(I);
55 }
56
58 llvm::fill(Bits, -BitWord(0));
59 return *this;
60 }
61
62 constexpr Bitset &set(unsigned I) {
63 Bits[I / BITWORD_SIZE] |= BitWord(1) << (I % BITWORD_SIZE);
64 return *this;
65 }
66
67 constexpr Bitset &reset(unsigned I) {
68 Bits[I / BITWORD_SIZE] &= ~(BitWord(1) << (I % BITWORD_SIZE));
69 return *this;
70 }
71
72 constexpr Bitset &flip(unsigned I) {
73 Bits[I / BITWORD_SIZE] ^= BitWord(1) << (I % BITWORD_SIZE);
74 return *this;
75 }
76
77 constexpr bool operator[](unsigned I) const {
78 BitWord Mask = BitWord(1) << (I % BITWORD_SIZE);
79 return (Bits[I / BITWORD_SIZE] & Mask) != 0;
80 }
81
82 constexpr bool test(unsigned I) const { return (*this)[I]; }
83
84 constexpr size_t size() const { return NumBits; }
85
86 bool any() const {
87 return llvm::any_of(Bits, [](BitWord I) { return I != 0; });
88 }
89 bool none() const { return !any(); }
90 size_t count() const {
91 size_t Count = 0;
92 for (auto B : Bits)
93 Count += llvm::popcount(B);
94 return Count;
95 }
96
97 constexpr Bitset &operator^=(const Bitset &RHS) {
98 for (unsigned I = 0, E = Bits.size(); I != E; ++I) {
99 Bits[I] ^= RHS.Bits[I];
100 }
101 return *this;
102 }
103 constexpr Bitset operator^(const Bitset &RHS) const {
104 Bitset Result = *this;
105 Result ^= RHS;
106 return Result;
107 }
108
109 constexpr Bitset &operator&=(const Bitset &RHS) {
110 for (unsigned I = 0, E = Bits.size(); I != E; ++I)
111 Bits[I] &= RHS.Bits[I];
112 return *this;
113 }
114 constexpr Bitset operator&(const Bitset &RHS) const {
115 Bitset Result = *this;
116 Result &= RHS;
117 return Result;
118 }
119
120 constexpr Bitset &operator|=(const Bitset &RHS) {
121 for (unsigned I = 0, E = Bits.size(); I != E; ++I) {
122 Bits[I] |= RHS.Bits[I];
123 }
124 return *this;
125 }
126 constexpr Bitset operator|(const Bitset &RHS) const {
127 Bitset Result = *this;
128 Result |= RHS;
129 return Result;
130 }
131
132 constexpr Bitset operator~() const {
133 Bitset Result = *this;
134 for (auto &B : Result.Bits)
135 B = ~B;
136 return Result;
137 }
138
139 bool operator==(const Bitset &RHS) const {
140 return std::equal(std::begin(Bits), std::end(Bits), std::begin(RHS.Bits));
141 }
142
143 bool operator!=(const Bitset &RHS) const { return !(*this == RHS); }
144
145 bool operator < (const Bitset &Other) const {
146 for (unsigned I = 0, E = size(); I != E; ++I) {
147 bool LHS = test(I), RHS = Other.test(I);
148 if (LHS != RHS)
149 return LHS < RHS;
150 }
151 return false;
152 }
153};
154
155} // end namespace llvm
156
157#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition: MD5.cpp:58
modulo schedule test
This file contains some templates that are useful if you are working with the STL at all.
Value * RHS
Value * LHS
This is a constexpr reimplementation of a subset of std::bitset.
Definition: Bitset.h:30
constexpr Bitset operator^(const Bitset &RHS) const
Definition: Bitset.h:103
constexpr Bitset & set(unsigned I)
Definition: Bitset.h:62
constexpr Bitset & flip(unsigned I)
Definition: Bitset.h:72
bool operator<(const Bitset &Other) const
Definition: Bitset.h:145
bool operator!=(const Bitset &RHS) const
Definition: Bitset.h:143
constexpr Bitset()=default
constexpr Bitset & operator^=(const Bitset &RHS)
Definition: Bitset.h:97
constexpr Bitset operator&(const Bitset &RHS) const
Definition: Bitset.h:114
constexpr bool operator[](unsigned I) const
Definition: Bitset.h:77
constexpr size_t size() const
Definition: Bitset.h:84
size_t count() const
Definition: Bitset.h:90
Bitset & set()
Definition: Bitset.h:57
bool any() const
Definition: Bitset.h:86
constexpr Bitset & operator|=(const Bitset &RHS)
Definition: Bitset.h:120
bool operator==(const Bitset &RHS) const
Definition: Bitset.h:139
constexpr Bitset(std::initializer_list< unsigned > Init)
Definition: Bitset.h:52
std::array< BitWord, NumWords > StorageType
Definition: Bitset.h:42
constexpr Bitset & operator&=(const Bitset &RHS)
Definition: Bitset.h:109
constexpr bool test(unsigned I) const
Definition: Bitset.h:82
constexpr Bitset operator|(const Bitset &RHS) const
Definition: Bitset.h:126
constexpr Bitset(const StorageType &B)
Definition: Bitset.h:48
constexpr Bitset & reset(unsigned I)
Definition: Bitset.h:67
bool none() const
Definition: Bitset.h:89
constexpr Bitset operator~() const
Definition: Bitset.h:132
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1764
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition: bit.h:307
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
@ Other
Any other memory.