bit.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
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 
47 constexpr CUDF_HOST_DEVICE inline size_type word_index(size_type bit_index)
48 {
49  return bit_index / detail::size_in_bits<bitmask_type>();
50 }
51 
59 {
60  return bit_index % detail::size_in_bits<bitmask_type>();
61 }
62 
72 CUDF_HOST_DEVICE inline void set_bit_unsafe(bitmask_type* bitmask, size_type bit_index)
73 {
74  assert(nullptr != bitmask);
75  bitmask[word_index(bit_index)] |= (bitmask_type{1} << intra_word_index(bit_index));
76 }
77 
87 CUDF_HOST_DEVICE inline void clear_bit_unsafe(bitmask_type* bitmask, size_type bit_index)
88 {
89  assert(nullptr != bitmask);
90  bitmask[word_index(bit_index)] &= ~(bitmask_type{1} << intra_word_index(bit_index));
91 }
92 
101 CUDF_HOST_DEVICE inline bool bit_is_set(bitmask_type const* bitmask, size_type bit_index)
102 {
103  assert(nullptr != bitmask);
104  return bitmask[word_index(bit_index)] & (bitmask_type{1} << intra_word_index(bit_index));
105 }
106 
117 CUDF_HOST_DEVICE inline bool bit_value_or(bitmask_type const* bitmask,
118  size_type bit_index,
119  bool default_value)
120 {
121  return bitmask != nullptr ? bit_is_set(bitmask, bit_index) : default_value;
122 }
123 
133 {
134  assert(0 <= n && n < static_cast<size_type>(detail::size_in_bits<bitmask_type>()));
135  return ((bitmask_type{1} << n) - 1);
136 }
137 
147 {
148  constexpr size_type word_size{detail::size_in_bits<bitmask_type>()};
149  assert(0 <= n && n < word_size);
150  return ~((bitmask_type{1} << (word_size - n)) - 1);
151 }
152 
153 #ifdef __CUDACC__
154 
168 __device__ inline void set_bit(bitmask_type* bitmask, size_type bit_index)
169 {
170  assert(nullptr != bitmask);
171  atomicOr(&bitmask[word_index(bit_index)], (bitmask_type{1} << intra_word_index(bit_index)));
172 }
173 
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:72
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:58
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:47
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:132
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:117
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:101
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:146
CUDF_HOST_DEVICE void clear_bit_unsafe(bitmask_type *bitmask, size_type bit_index)
Sets the specified bit to 0
Definition: bit.hpp:87
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