bitset.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
8 
9 #include <cstddef>
10 #include <type_traits>
11 #include <variant>
12 
13 #ifndef __CUDACC__
14 #include <math.h>
15 #endif
16 
17 namespace ML {
18 namespace fil {
19 namespace detail {
20 template <typename index_t = size_t, typename storage_t = std::byte>
21 struct bitset {
22  using storage_type = storage_t;
23  using index_type = index_t;
24 
25  auto constexpr static const bin_width = index_type(sizeof(storage_type) * 8);
26 
27  HOST DEVICE bitset() : data_{nullptr}, num_bits_{0} {}
28 
29  HOST DEVICE bitset(storage_type* data, index_type size) : data_{data}, num_bits_{size} {}
30 
31  HOST DEVICE bitset(storage_type* data) : data_{data}, num_bits_(sizeof(storage_type) * 8) {}
32 
33  HOST DEVICE auto size() const { return num_bits_; }
34  HOST DEVICE auto bin_count() const
35  {
36  return num_bits_ / bin_width + (num_bits_ % bin_width != 0);
37  }
38 
39  // Standard bit-wise mutators and accessor
40  HOST DEVICE auto& set(index_type index)
41  {
42  data_[bin_from_index(index)] |= mask_in_bin(index);
43  return *this;
44  }
45  HOST DEVICE auto& clear(index_type index)
46  {
47  data_[bin_from_index(index)] &= ~mask_in_bin(index);
48  return *this;
49  }
50  HOST DEVICE auto test(index_type index) const
51  {
52  auto result = false;
53  if (index < num_bits_) { result = ((data_[bin_from_index(index)] & mask_in_bin(index)) != 0); }
54  return result;
55  }
56  HOST DEVICE auto& flip()
57  {
58  for (auto i = index_type{}; i < bin_count(); ++i) {
59  data_[i] = ~data_[i];
60  }
61  return *this;
62  }
63 
64  // Bit-wise boolean operations
66  {
67  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
68  data_[i] &= other.data_[i];
69  }
70  return *this;
71  }
73  {
74  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
75  data_[i] |= other.data_[i];
76  }
77  return *this;
78  }
80  {
81  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
82  data_[i] ^= other.data_[i];
83  }
84  return *this;
85  }
86  HOST DEVICE auto& operator~() const
87  {
88  flip();
89  return *this;
90  }
91 
92  private:
93  storage_type* data_;
94  index_type num_bits_;
95 
96  HOST DEVICE auto mask_in_bin(index_type index) const
97  {
98  return storage_type{1} << (index % bin_width);
99  }
100 
101  HOST DEVICE auto bin_from_index(index_type index) const { return index / bin_width; }
102 };
103 
104 } // namespace detail
105 } // namespace fil
106 } // namespace ML
#define DEVICE
Definition: gpu_support.hpp:24
#define HOST
Definition: gpu_support.hpp:23
uint32_t index_type
Definition: index_type.hpp:9
Definition: dbscan.hpp:18
Definition: bitset.hpp:21
HOST DEVICE auto & set(index_type index)
Definition: bitset.hpp:40
HOST DEVICE bitset()
Definition: bitset.hpp:27
HOST DEVICE auto & operator&=(bitset< storage_type > const &other)
Definition: bitset.hpp:65
HOST DEVICE auto & operator~() const
Definition: bitset.hpp:86
HOST DEVICE bitset(storage_type *data)
Definition: bitset.hpp:31
HOST DEVICE auto & clear(index_type index)
Definition: bitset.hpp:45
constexpr static auto const bin_width
Definition: bitset.hpp:25
HOST DEVICE auto bin_count() const
Definition: bitset.hpp:34
index_t index_type
Definition: bitset.hpp:23
HOST DEVICE auto size() const
Definition: bitset.hpp:33
HOST DEVICE bitset(storage_type *data, index_type size)
Definition: bitset.hpp:29
storage_t storage_type
Definition: bitset.hpp:22
HOST DEVICE auto & flip()
Definition: bitset.hpp:56
HOST DEVICE auto & operator^=(bitset< storage_type > const &other)
Definition: bitset.hpp:79
HOST DEVICE auto & operator|=(bitset< storage_type > const &other)
Definition: bitset.hpp:72
HOST DEVICE auto test(index_type index) const
Definition: bitset.hpp:50