[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libc / src / __support / CPP / bitset.h
blobcfac749e0d84e3a92c5b9f2e1ab869b23b22bffe
1 //===-- A self contained equivalent of 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_BITSET_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_BITSET_H
12 #include "src/__support/macros/attributes.h"
13 #include <stddef.h> // For size_t.
15 namespace LIBC_NAMESPACE::cpp {
17 template <size_t NumberOfBits> struct bitset {
18 static_assert(NumberOfBits != 0,
19 "Cannot create a LIBC_NAMESPACE::cpp::bitset of size 0.");
21 LIBC_INLINE constexpr void set(size_t Index) {
22 Data[Index / BITS_PER_UNIT] |= mask(Index);
25 LIBC_INLINE constexpr void reset() {
26 for (size_t i = 0; i < NUMBER_OF_UNITS; ++i)
27 Data[i] = 0;
30 LIBC_INLINE constexpr bool test(size_t Index) const {
31 return Data[Index / BITS_PER_UNIT] & mask(Index);
34 LIBC_INLINE constexpr void flip() {
35 for (size_t i = 0; i < NUMBER_OF_UNITS; ++i)
36 Data[i] = ~Data[i];
39 // This function sets all bits in the range from Start to End (inclusive) to
40 // true. It assumes that Start <= End.
41 LIBC_INLINE constexpr void set_range(size_t Start, size_t End) {
42 size_t start_index = Start / BITS_PER_UNIT;
43 size_t end_index = End / BITS_PER_UNIT;
45 if (start_index == end_index) {
46 // The reason the left shift is split into two parts (instead of just left
47 // shifting by End - Start + 1) is because when a number is shifted left
48 // by 64 then it wraps around to doing nothing, but shifting by 63 and the
49 // shifting by 1 correctly shifts away all of the bits.
50 size_t bit_mask = (((size_t(1) << (End - Start)) << 1) - 1)
51 << (Start - (start_index * BITS_PER_UNIT));
52 Data[start_index] |= bit_mask;
53 } else {
54 size_t low_bit_mask =
55 ~((size_t(1) << (Start - (start_index * BITS_PER_UNIT))) - 1);
56 Data[start_index] |= low_bit_mask;
58 for (size_t i = start_index + 1; i < end_index; ++i)
59 Data[i] = ~size_t(0);
61 // Same as above, by splitting the shift the behavior is more consistent.
62 size_t high_bit_mask =
63 ((size_t(1) << (End - (end_index * BITS_PER_UNIT))) << 1) - 1;
64 Data[end_index] |= high_bit_mask;
68 LIBC_INLINE constexpr bool operator==(const bitset<NumberOfBits> &other) {
69 for (size_t i = 0; i < NUMBER_OF_UNITS; ++i) {
70 if (Data[i] != other.Data[i])
71 return false;
73 return true;
76 private:
77 static constexpr size_t BITS_PER_BYTE = 8;
78 static constexpr size_t BITS_PER_UNIT = BITS_PER_BYTE * sizeof(size_t);
79 static constexpr size_t NUMBER_OF_UNITS =
80 (NumberOfBits + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
82 LIBC_INLINE static constexpr size_t mask(size_t Index) {
83 return size_t{1} << (Index % BITS_PER_UNIT);
85 size_t Data[NUMBER_OF_UNITS] = {0};
88 } // namespace LIBC_NAMESPACE::cpp
90 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_BITSET_H