Loading [MathJax]/extensions/tex2jax.js
cuML C++ API  23.12
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bitset.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, 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
17 #include <cstddef>
18 #ifndef __CUDACC__
19 #include <math.h>
20 #endif
23 #include <stddef.h>
24 #include <type_traits>
25 #include <variant>
26 
27 namespace ML {
28 namespace experimental {
29 namespace fil {
30 namespace detail {
31 template <typename index_t = size_t, typename storage_t = std::byte>
32 struct bitset {
33  using storage_type = storage_t;
34  using index_type = index_t;
35 
36  auto constexpr static const bin_width = index_type(sizeof(storage_type) * 8);
37 
38  HOST DEVICE bitset() : data_{nullptr}, num_bits_{0} {}
39 
40  HOST DEVICE bitset(storage_type* data, index_type size) : data_{data}, num_bits_{size} {}
41 
42  HOST DEVICE bitset(storage_type* data) : data_{data}, num_bits_(sizeof(storage_type) * 8) {}
43 
44  HOST DEVICE auto size() const { return num_bits_; }
45  HOST DEVICE auto bin_count() const
46  {
47  return num_bits_ / bin_width + (num_bits_ % bin_width != 0);
48  }
49 
50  // Standard bit-wise mutators and accessor
51  HOST DEVICE auto& set(index_type index)
52  {
53  data_[bin_from_index(index)] |= mask_in_bin(index);
54  return *this;
55  }
56  HOST DEVICE auto& clear(index_type index)
57  {
58  data_[bin_from_index(index)] &= ~mask_in_bin(index);
59  return *this;
60  }
61  HOST DEVICE auto test(index_type index) const
62  {
63  auto result = false;
64  if (index < num_bits_) { result = ((data_[bin_from_index(index)] & mask_in_bin(index)) != 0); }
65  return result;
66  }
67  HOST DEVICE auto& flip()
68  {
69  for (auto i = index_type{}; i < bin_count(); ++i) {
70  data_[i] = ~data_[i];
71  }
72  return *this;
73  }
74 
75  // Bit-wise boolean operations
77  {
78  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
79  data_[i] &= other.data_[i];
80  }
81  return *this;
82  }
84  {
85  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
86  data_[i] |= other.data_[i];
87  }
88  return *this;
89  }
91  {
92  for (auto i = index_type{}; i < min(size(), other.size()); ++i) {
93  data_[i] ^= other.data_[i];
94  }
95  return *this;
96  }
97  HOST DEVICE auto& operator~() const
98  {
99  flip();
100  return *this;
101  }
102 
103  private:
104  storage_type* data_;
105  index_type num_bits_;
106 
107  HOST DEVICE auto mask_in_bin(index_type index) const
108  {
109  return storage_type{1} << (index % bin_width);
110  }
111 
112  HOST DEVICE auto bin_from_index(index_type index) const { return index / bin_width; }
113 };
114 
115 } // namespace detail
116 } // namespace fil
117 } // namespace experimental
118 } // namespace ML
#define DEVICE
Definition: gpu_support.hpp:34
#define HOST
Definition: gpu_support.hpp:33
uint32_t index_type
Definition: index_type.hpp:21
Definition: dbscan.hpp:27
Definition: bitset.hpp:32
HOST DEVICE auto & set(index_type index)
Definition: bitset.hpp:51
HOST DEVICE auto size() const
Definition: bitset.hpp:44
index_t index_type
Definition: bitset.hpp:34
HOST DEVICE auto & flip()
Definition: bitset.hpp:67
HOST DEVICE auto & operator^=(bitset< storage_type > const &other)
Definition: bitset.hpp:90
HOST DEVICE auto bin_count() const
Definition: bitset.hpp:45
HOST DEVICE auto & operator~() const
Definition: bitset.hpp:97
HOST DEVICE bitset(storage_type *data, index_type size)
Definition: bitset.hpp:40
HOST DEVICE auto & operator&=(bitset< storage_type > const &other)
Definition: bitset.hpp:76
HOST DEVICE auto & clear(index_type index)
Definition: bitset.hpp:56
HOST DEVICE bitset()
Definition: bitset.hpp:38
HOST DEVICE auto & operator|=(bitset< storage_type > const &other)
Definition: bitset.hpp:83
storage_t storage_type
Definition: bitset.hpp:33
HOST DEVICE bitset(storage_type *data)
Definition: bitset.hpp:42
HOST DEVICE auto test(index_type index) const
Definition: bitset.hpp:61
constexpr static auto const bin_width
Definition: bitset.hpp:36