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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_BITSET_H
10 #define LLVM_LIBC_SRC_SUPPORT_CPP_BITSET_H
11 
12 #include <stddef.h> // For size_t.
13 #include <stdint.h> // For uintptr_t.
14 
15 namespace __llvm_libc {
16 namespace cpp {
17 
18 template <size_t NumberOfBits> struct Bitset {
19   static_assert(NumberOfBits != 0,
20                 "Cannot create a __llvm_libc::cpp::Bitset of size 0.");
21 
setBitset22   constexpr void set(size_t Index) {
23     Data[Index / BITS_PER_UNIT] |= (uintptr_t{1} << (Index % BITS_PER_UNIT));
24   }
25 
testBitset26   constexpr bool test(size_t Index) const {
27     return Data[Index / BITS_PER_UNIT] &
28            (uintptr_t{1} << (Index % BITS_PER_UNIT));
29   }
30 
31 private:
32   static constexpr size_t BITS_PER_BYTE = 8;
33   static constexpr size_t BITS_PER_UNIT = BITS_PER_BYTE * sizeof(uintptr_t);
34   uintptr_t Data[(NumberOfBits + BITS_PER_UNIT - 1) / BITS_PER_UNIT] = {0};
35 };
36 
37 } // namespace cpp
38 } // namespace __llvm_libc
39 
40 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_BITSET_H
41