bit.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/types.hpp>
9 
10 #include <cuda/std/climits>
11 
12 #include <cassert>
13 
19 namespace CUDF_EXPORT cudf {
20 namespace detail {
21 
28 template <typename T>
29 constexpr CUDF_HOST_DEVICE inline std::size_t size_in_bits()
30 {
31  static_assert(CHAR_BIT == 8, "Size of a byte must be 8 bits.");
32  return sizeof(T) * CHAR_BIT;
33 }
34 } // namespace detail
35 
48 constexpr CUDF_HOST_DEVICE inline size_type word_index(size_type bit_index)
49 {
50  return bit_index / detail::size_in_bits<bitmask_type>();
51 }
52 
60 {
61  return bit_index % detail::size_in_bits<bitmask_type>();
62 }
63 
73 CUDF_HOST_DEVICE inline void set_bit_unsafe(bitmask_type* bitmask, size_type bit_index)
74 {
75  assert(nullptr != bitmask);
76  bitmask[word_index(bit_index)] |= (bitmask_type{1} << intra_word_index(bit_index));
77 }
78 
88 CUDF_HOST_DEVICE inline void clear_bit_unsafe(bitmask_type* bitmask, size_type bit_index)
89 {
90  assert(nullptr != bitmask);
91  bitmask[word_index(bit_index)] &= ~(bitmask_type{1} << intra_word_index(bit_index));
92 }
93 
102 CUDF_HOST_DEVICE inline bool bit_is_set(bitmask_type const* bitmask, size_type bit_index)
103 {
104  assert(nullptr != bitmask);
105  return bitmask[word_index(bit_index)] & (bitmask_type{1} << intra_word_index(bit_index));
106 }
107 
118 CUDF_HOST_DEVICE inline bool bit_value_or(bitmask_type const* bitmask,
119  size_type bit_index,
120  bool default_value)
121 {
122  return bitmask != nullptr ? bit_is_set(bitmask, bit_index) : default_value;
123 }
124 
134 {
135  assert(0 <= n && n < static_cast<size_type>(detail::size_in_bits<bitmask_type>()));
136  return ((bitmask_type{1} << n) - 1);
137 }
138 
148 {
149  constexpr size_type word_size{detail::size_in_bits<bitmask_type>()};
150  assert(0 <= n && n < word_size);
151  return ~((bitmask_type{1} << (word_size - n)) - 1);
152 }
153 
154 #ifdef __CUDACC__
155 
169 __device__ inline void set_bit(bitmask_type* bitmask, size_type bit_index)
170 {
171  assert(nullptr != bitmask);
172  atomicOr(&bitmask[word_index(bit_index)], (bitmask_type{1} << intra_word_index(bit_index)));
173 }
174 
187 __device__ inline void clear_bit(bitmask_type* bitmask, size_type bit_index)
188 {
189  assert(nullptr != bitmask);
190  atomicAnd(&bitmask[word_index(bit_index)], ~(bitmask_type{1} << intra_word_index(bit_index)));
191 }
192 #endif // end of group
194 } // 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:29
CUDF_HOST_DEVICE void set_bit_unsafe(bitmask_type *bitmask, size_type bit_index)
Sets the specified bit to 1
Definition: bit.hpp:73
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:59
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:48
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:133
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:118
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:102
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:147
CUDF_HOST_DEVICE void clear_bit_unsafe(bitmask_type *bitmask, size_type bit_index)
Sets the specified bit to 0
Definition: bit.hpp:88
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:84
uint32_t bitmask_type
Bitmask type stored as 32-bit unsigned integer.
Definition: types.hpp:85
cuDF interfaces
Definition: host_udf.hpp:26
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:21