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_UTILS_CPP_BITSET_H
10 #define LLVM_LIBC_UTILS_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 / BitsPerUnit] |= (uintptr_t{1} << (Index % BitsPerUnit));
24   }
25 
testBitset26   constexpr bool test(size_t Index) const {
27     return Data[Index / BitsPerUnit] & (uintptr_t{1} << (Index % BitsPerUnit));
28   }
29 
30 private:
31   static constexpr size_t BitsPerByte = 8;
32   static constexpr size_t BitsPerUnit = BitsPerByte * sizeof(uintptr_t);
33   uintptr_t Data[(NumberOfBits + BitsPerUnit - 1) / BitsPerUnit] = {0};
34 };
35 
36 } // namespace cpp
37 } // namespace __llvm_libc
38 
39 #endif // LLVM_LIBC_UTILS_CPP_BITSET_H
40