bitset.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
19 
20 #include <cstddef>
21 #include <type_traits>
22 #include <variant>
23 
24 #ifndef __CUDACC__
25 #include <math.h>
26 #endif
27 
28 namespace ML {
29 namespace experimental {
30 namespace fil {
31 namespace detail {
32 template <typename index_t = size_t, typename storage_t = std::byte>
33 struct bitset {
34  using storage_type = storage_t;
35  using index_type = index_t;
36 
37  auto constexpr static const bin_width = index_type(sizeof(storage_type) * 8);
38 
39  HOST DEVICE bitset() : data_{nullptr}, num_bits_{0} {}
40 
41  HOST DEVICE bitset(storage_type* data, index_type size) : data_{data}, num_bits_{size} {}
42 
43  HOST DEVICE bitset(storage_type* data) : data_{data}, num_bits_(sizeof(storage_type) * 8) {}
44 
45  HOST DEVICE auto size() const { return num_bits_; }
46  HOST DEVICE auto bin_count() const
47  {
48  return num_bits_ / bin_width + (num_bits_ % bin_width != 0);
49  }
50 
51  // Standard bit-wise mutators and accessor
52  HOST DEVICE auto& set(index_type index)
53  {
54  data_[bin_from_index(index)] |= mask_in_bin(index);
55  return *this;
56  }
57  HOST DEVICE auto& clear(index_type index)
58  {
59  data_[bin_from_index(index)] &= ~mask_in_bin(index);
60  return *this;
61  }
62  HOST DEVICE auto test(index_type index) const
63  {
64  auto result = false;
65  if (index < num_bits_) { result = ((data_[bin_from_index(index)] & mask_in_bin(index)) != 0); }
66  return result;
67  }
68  HOST DEVICE auto& flip()
69  {
70  for (auto i = index_type{}; i < bin_count(); ++i) {
71  data_[i] = ~data_[i];
72  }
73  return *this;
74  }
75 
76  // Bit-wise boolean operations
78  {
79  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
80  data_[i] &= other.data_[i];
81  }
82  return *this;
83  }
85  {
86  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
87  data_[i] |= other.data_[i];
88  }
89  return *this;
90  }
92  {
93  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
94  data_[i] ^= other.data_[i];
95  }
96  return *this;
97  }
98  HOST DEVICE auto& operator~() const
99  {
100  flip();
101  return *this;
102  }
103 
104  private:
105  storage_type* data_;
106  index_type num_bits_;
107 
108  HOST DEVICE auto mask_in_bin(index_type index) const
109  {
110  return storage_type{1} << (index % bin_width);
111  }
112 
113  HOST DEVICE auto bin_from_index(index_type index) const { return index / bin_width; }
114 };
115 
116 } // namespace detail
117 } // namespace fil
118 } // namespace experimental
119 } // namespace ML
#define DEVICE
Definition: gpu_support.hpp:35
#define HOST
Definition: gpu_support.hpp:34
uint32_t index_type
Definition: index_type.hpp:21
Definition: dbscan.hpp:30
Definition: bitset.hpp:33
HOST DEVICE auto & set(index_type index)
Definition: bitset.hpp:52
HOST DEVICE auto size() const
Definition: bitset.hpp:45
index_t index_type
Definition: bitset.hpp:35
HOST DEVICE auto & flip()
Definition: bitset.hpp:68
HOST DEVICE auto & operator^=(bitset< storage_type > const &other)
Definition: bitset.hpp:91
HOST DEVICE auto bin_count() const
Definition: bitset.hpp:46
HOST DEVICE auto & operator~() const
Definition: bitset.hpp:98
HOST DEVICE bitset(storage_type *data, index_type size)
Definition: bitset.hpp:41
HOST DEVICE auto & operator&=(bitset< storage_type > const &other)
Definition: bitset.hpp:77
HOST DEVICE auto & clear(index_type index)
Definition: bitset.hpp:57
HOST DEVICE bitset()
Definition: bitset.hpp:39
HOST DEVICE auto & operator|=(bitset< storage_type > const &other)
Definition: bitset.hpp:84
storage_t storage_type
Definition: bitset.hpp:34
HOST DEVICE bitset(storage_type *data)
Definition: bitset.hpp:43
HOST DEVICE auto test(index_type index) const
Definition: bitset.hpp:62
constexpr static auto const bin_width
Definition: bitset.hpp:37