|
| 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 | + |
| 24 | +namespace llvm { |
| 25 | + |
| 26 | +/// Container class for subtarget features. |
| 27 | +/// This is a constexpr reimplementation of a subset of std::bitset. It would be |
| 28 | +/// nice to use std::bitset directly, but it doesn't support constant |
| 29 | +/// initialization. |
| 30 | +template <unsigned NumBits> |
| 31 | +class Bitset { |
| 32 | + typedef uintptr_t BitWord; |
| 33 | + |
| 34 | + enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT }; |
| 35 | + |
| 36 | + static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32, |
| 37 | + "Unsupported word size"); |
| 38 | + |
| 39 | + static constexpr unsigned NumWords = (NumBits + BITWORD_SIZE-1) / BITWORD_SIZE; |
| 40 | + std::array<BitWord, NumWords> Bits{}; |
| 41 | + |
| 42 | +protected: |
| 43 | + constexpr Bitset(const std::array<BitWord, NumWords> &B) |
| 44 | + : Bits{B} {} |
| 45 | + |
| 46 | +public: |
| 47 | + constexpr Bitset() = default; |
| 48 | + constexpr Bitset(std::initializer_list<unsigned> Init) { |
| 49 | + for (auto I : Init) |
| 50 | + set(I); |
| 51 | + } |
| 52 | + |
| 53 | + Bitset &set() { |
| 54 | + std::fill(std::begin(Bits), std::end(Bits), -BitWord(0)); |
| 55 | + return *this; |
| 56 | + } |
| 57 | + |
| 58 | + constexpr Bitset &set(unsigned I) { |
| 59 | + // GCC <6.2 crashes if this is written in a single statement. |
| 60 | + BitWord NewBits = Bits[I / BITWORD_SIZE] | (BitWord(1) << (I % BITWORD_SIZE)); |
| 61 | + Bits[I / BITWORD_SIZE] = NewBits; |
| 62 | + return *this; |
| 63 | + } |
| 64 | + |
| 65 | + constexpr Bitset &reset(unsigned I) { |
| 66 | + // GCC <6.2 crashes if this is written in a single statement. |
| 67 | + BitWord NewBits = Bits[I / BITWORD_SIZE] & ~(BitWord(1) << (I % BITWORD_SIZE)); |
| 68 | + Bits[I / BITWORD_SIZE] = NewBits; |
| 69 | + return *this; |
| 70 | + } |
| 71 | + |
| 72 | + constexpr Bitset &flip(unsigned I) { |
| 73 | + // GCC <6.2 crashes if this is written in a single statement. |
| 74 | + BitWord NewBits = Bits[I / BITWORD_SIZE] ^ (BitWord(1) << (I % BITWORD_SIZE)); |
| 75 | + Bits[I / BITWORD_SIZE] = NewBits; |
| 76 | + return *this; |
| 77 | + } |
| 78 | + |
| 79 | + constexpr bool operator[](unsigned I) const { |
| 80 | + BitWord Mask = BitWord(1) << (I % BITWORD_SIZE); |
| 81 | + return (Bits[I / BITWORD_SIZE] & Mask) != 0; |
| 82 | + } |
| 83 | + |
| 84 | + constexpr bool test(unsigned I) const { return (*this)[I]; } |
| 85 | + |
| 86 | + constexpr size_t size() const { return NumBits; } |
| 87 | + |
| 88 | + bool any() const { |
| 89 | + return llvm::any_of(Bits, [](BitWord I) { return I != 0; }); |
| 90 | + } |
| 91 | + bool none() const { return !any(); } |
| 92 | + size_t count() const { |
| 93 | + size_t Count = 0; |
| 94 | + for (auto B : Bits) |
| 95 | + Count += llvm::popcount(B); |
| 96 | + return Count; |
| 97 | + } |
| 98 | + |
| 99 | + constexpr Bitset &operator^=(const Bitset &RHS) { |
| 100 | + for (unsigned I = 0, E = Bits.size(); I != E; ++I) { |
| 101 | + Bits[I] ^= RHS.Bits[I]; |
| 102 | + } |
| 103 | + return *this; |
| 104 | + } |
| 105 | + constexpr Bitset operator^(const Bitset &RHS) const { |
| 106 | + Bitset Result = *this; |
| 107 | + Result ^= RHS; |
| 108 | + return Result; |
| 109 | + } |
| 110 | + |
| 111 | + constexpr Bitset &operator&=(const Bitset &RHS) { |
| 112 | + for (unsigned I = 0, E = Bits.size(); I != E; ++I) { |
| 113 | + Bits[I] &= RHS.Bits[I]; |
| 114 | + } |
| 115 | + return *this; |
| 116 | + } |
| 117 | + constexpr Bitset operator&(const Bitset &RHS) const { |
| 118 | + Bitset Result = *this; |
| 119 | + Result &= RHS; |
| 120 | + return Result; |
| 121 | + } |
| 122 | + |
| 123 | + constexpr Bitset &operator|=(const Bitset &RHS) { |
| 124 | + for (unsigned I = 0, E = Bits.size(); I != E; ++I) { |
| 125 | + Bits[I] |= RHS.Bits[I]; |
| 126 | + } |
| 127 | + return *this; |
| 128 | + } |
| 129 | + constexpr Bitset operator|(const Bitset &RHS) const { |
| 130 | + Bitset Result = *this; |
| 131 | + Result |= RHS; |
| 132 | + return Result; |
| 133 | + } |
| 134 | + |
| 135 | + constexpr Bitset operator~() const { |
| 136 | + Bitset Result = *this; |
| 137 | + for (auto &B : Result.Bits) |
| 138 | + B = ~B; |
| 139 | + return Result; |
| 140 | + } |
| 141 | + |
| 142 | + bool operator==(const Bitset &RHS) const { |
| 143 | + return std::equal(std::begin(Bits), std::end(Bits), std::begin(RHS.Bits)); |
| 144 | + } |
| 145 | + |
| 146 | + bool operator!=(const Bitset &RHS) const { return !(*this == RHS); } |
| 147 | + |
| 148 | + bool operator < (const Bitset &Other) const { |
| 149 | + for (unsigned I = 0, E = size(); I != E; ++I) { |
| 150 | + bool LHS = test(I), RHS = Other.test(I); |
| 151 | + if (LHS != RHS) |
| 152 | + return LHS < RHS; |
| 153 | + } |
| 154 | + return false; |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +} // end namespace llvm |
| 159 | + |
| 160 | +#endif |
0 commit comments