bit.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, 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 
17 #pragma once
18 
19 #include <cudf/types.hpp>
20 
21 #include <cuda/std/climits>
22 
23 #include <cassert>
24 
30 namespace CUDF_EXPORT cudf {
31 namespace detail {
32 
39 template <typename T>
40 constexpr CUDF_HOST_DEVICE inline std::size_t size_in_bits()
41 {
42  static_assert(CHAR_BIT == 8, "Size of a byte must be 8 bits.");
43  return sizeof(T) * CHAR_BIT;
44 }
45 } // namespace detail
46 
59 constexpr CUDF_HOST_DEVICE inline size_type word_index(size_type bit_index)
60 {
61  return bit_index / detail::size_in_bits<bitmask_type>();
62 }
63 
71 {
72  return bit_index % detail::size_in_bits<bitmask_type>();
73 }
74 
84 CUDF_HOST_DEVICE inline void set_bit_unsafe(bitmask_type* bitmask, size_type bit_index)
85 {
86  assert(nullptr != bitmask);
87  bitmask[word_index(bit_index)] |= (bitmask_type{1} << intra_word_index(bit_index));
88 }
89 
99 CUDF_HOST_DEVICE inline void clear_bit_unsafe(bitmask_type* bitmask, size_type bit_index)
100 {
101  assert(nullptr != bitmask);
102  bitmask[word_index(bit_index)] &= ~(bitmask_type{1} << intra_word_index(bit_index));
103 }
104 
113 CUDF_HOST_DEVICE inline bool bit_is_set(bitmask_type const* bitmask, size_type bit_index)
114 {
115  assert(nullptr != bitmask);
116  return bitmask[word_index(bit_index)] & (bitmask_type{1} << intra_word_index(bit_index));
117 }
118 
129 CUDF_HOST_DEVICE inline bool bit_value_or(bitmask_type const* bitmask,
130  size_type bit_index,
131  bool default_value)
132 {
133  return bitmask != nullptr ? bit_is_set(bitmask, bit_index) : default_value;
134 }
135 
145 {
146  assert(0 <= n && n < static_cast<size_type>(detail::size_in_bits<bitmask_type>()));
147  return ((bitmask_type{1} << n) - 1);
148 }
149 
159 {
160  constexpr size_type word_size{detail::size_in_bits<bitmask_type>()};
161  assert(0 <= n && n < word_size);
162  return ~((bitmask_type{1} << (word_size - n)) - 1);
163 }
164 
165 #ifdef __CUDACC__
166 
180 __device__ inline void set_bit(bitmask_type* bitmask, size_type bit_index)
181 {
182  assert(nullptr != bitmask);
183  atomicOr(&bitmask[word_index(bit_index)], (bitmask_type{1} << intra_word_index(bit_index)));
184 }
185 
198 __device__ inline void clear_bit(bitmask_type* bitmask, size_type bit_index)
199 {
200  assert(nullptr != bitmask);
201  atomicAnd(&bitmask[word_index(bit_index)], ~(bitmask_type{1} << intra_word_index(bit_index)));
202 }
203 #endif // end of group
205 } // namespace CUDF_EXPORT cudf
constexpr CUDF_HOST_DEVICE std::size_t size_in_bits()
Returns the number of bits the given type can hold.
Definition: bit.hpp:40
CUDF_HOST_DEVICE void set_bit_unsafe(bitmask_type *bitmask, size_type bit_index)
Sets the specified bit to 1
Definition: bit.hpp:84
constexpr CUDF_HOST_DEVICE size_type intra_word_index(size_type bit_index)
Returns the position within a word of the specified bit.
Definition: bit.hpp:70
constexpr CUDF_HOST_DEVICE size_type word_index(size_type bit_index)
Returns the index of the word containing the specified bit.
Definition: bit.hpp:59
constexpr CUDF_HOST_DEVICE bitmask_type set_least_significant_bits(size_type n)
Returns a bitmask word with the n least significant bits set.
Definition: bit.hpp:144
CUDF_HOST_DEVICE bool bit_value_or(bitmask_type const *bitmask, size_type bit_index, bool default_value)
optional-like interface to check if a specified bit of a bitmask is set.
Definition: bit.hpp:129
CUDF_HOST_DEVICE bool bit_is_set(bitmask_type const *bitmask, size_type bit_index)
Indicates whether the specified bit is set to 1
Definition: bit.hpp:113
constexpr CUDF_HOST_DEVICE bitmask_type set_most_significant_bits(size_type n)
Returns a bitmask word with the n most significant bits set.
Definition: bit.hpp:158
CUDF_HOST_DEVICE void clear_bit_unsafe(bitmask_type *bitmask, size_type bit_index)
Sets the specified bit to 0
Definition: bit.hpp:99
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
uint32_t bitmask_type
Bitmask type stored as 32-bit unsigned integer.
Definition: types.hpp:96
cuDF interfaces
Definition: host_udf.hpp:37
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:32