Blender V5.3
BLI_bit_ref.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
19
20#include "BLI_utildefines.hh"
21
22#include <iosfwd>
23
24namespace blender {
25
26namespace bits {
27
31static constexpr int64_t BitsPerInt = int64_t(sizeof(BitInt) * 8);
33static constexpr int64_t BitToIntIndexShift = 3 + (sizeof(BitInt) >= 2) + (sizeof(BitInt) >= 4) +
34 (sizeof(BitInt) >= 8);
36static constexpr BitInt BitIndexMask = (BitInt(1) << BitToIntIndexShift) - 1;
37
39{
40 BLI_assert(n >= 0);
42 if (n == BitsPerInt) {
43 return BitInt(-1);
44 }
45 return (BitInt(1) << n) - 1;
46}
47
49{
51}
52
53inline BitInt mask_range_bits(const int64_t start, const int64_t size)
54{
55 BLI_assert(start >= 0);
56 BLI_assert(size >= 0);
57 const int64_t end = start + size;
58 BLI_assert(end <= BitsPerInt);
59 if (end == BitsPerInt) {
60 return mask_last_n_bits(size);
61 }
62 return ((BitInt(1) << end) - 1) & ~((BitInt(1) << start) - 1);
63}
64
65inline BitInt mask_single_bit(const int64_t bit_index)
66{
67 BLI_assert(bit_index >= 0);
68 BLI_assert(bit_index < BitsPerInt);
69 return BitInt(1) << bit_index;
70}
71
72inline BitInt *int_containing_bit(BitInt *data, const int64_t bit_index)
73{
74 return data + (bit_index >> BitToIntIndexShift);
75}
76
77inline const BitInt *int_containing_bit(const BitInt *data, const int64_t bit_index)
78{
79 return data + (bit_index >> BitToIntIndexShift);
80}
81
86class BitRef {
87 private:
89 const BitInt *int_;
91 BitInt mask_;
92
93 friend class MutableBitRef;
94
95 public:
96 BitRef() = default;
97
102 BitRef(const BitInt *data, const int64_t bit_index)
103 {
104 int_ = int_containing_bit(data, bit_index);
105 mask_ = mask_single_bit(bit_index & BitIndexMask);
106 }
107
111 bool test() const
112 {
113 const BitInt value = *int_;
114 const BitInt masked_value = value & mask_;
115 return masked_value != 0;
116 }
117
118 operator bool() const
119 {
120 return this->test();
121 }
122};
123
128 private:
130 BitInt *int_;
132 BitInt mask_;
133
134 public:
135 MutableBitRef() = default;
136
141 MutableBitRef(BitInt *data, const int64_t bit_index)
142 {
143 int_ = int_containing_bit(data, bit_index);
144 mask_ = mask_single_bit(bit_index & BitIndexMask);
145 }
146
150 operator BitRef() const
151 {
152 BitRef bit_ref;
153 bit_ref.int_ = int_;
154 bit_ref.mask_ = mask_;
155 return bit_ref;
156 }
157
161 bool test() const
162 {
163 const BitInt value = *int_;
164 const BitInt masked_value = value & mask_;
165 return masked_value != 0;
166 }
167
168 operator bool() const
169 {
170 return this->test();
171 }
172
176 void set()
177 {
178 *int_ |= mask_;
179 }
180
184 void reset()
185 {
186 *int_ &= ~mask_;
187 }
188
193 void set(const bool value)
194 {
195 if (value) {
196 this->set();
197 }
198 else {
199 this->reset();
200 }
201 }
202
209 void set_branchless(const bool value)
210 {
211 const BitInt value_int = BitInt(value);
212 BLI_assert(ELEM(value_int, 0, 1));
213 const BitInt old = *int_;
214 *int_ =
215 /* Unset bit. */
216 (~mask_ & old)
217 /* Optionally set it again. The -1 turns a 1 into `0x00...` and a 0 into `0xff...`. */
218 | (mask_ & ~(value_int - 1));
219 }
220
221 MutableBitRef &operator|=(const bool value)
222 {
223 if (value) {
224 this->set();
225 }
226 return *this;
227 }
228
229 MutableBitRef &operator&=(const bool value)
230 {
231 if (!value) {
232 this->reset();
233 }
234 return *this;
235 }
236};
237
238std::ostream &operator<<(std::ostream &stream, const BitRef &bit);
239std::ostream &operator<<(std::ostream &stream, const MutableBitRef &bit);
240
241} // namespace bits
242
243using bits::BitRef;
244using bits::MutableBitRef;
245
246} // namespace blender
#define ELEM(...)
BitRef()=default
BitRef(const BitInt *data, const int64_t bit_index)
friend class MutableBitRef
MutableBitRef(BitInt *data, const int64_t bit_index)
MutableBitRef & operator|=(const bool value)
void set(const bool value)
void set_branchless(const bool value)
MutableBitRef & operator&=(const bool value)
std::ostream & operator<<(std::ostream &stream, const AssetCatalogPath &path_to_append)
uint64_t BitInt
static constexpr BitInt BitIndexMask
static constexpr int64_t BitsPerInt
BitInt * int_containing_bit(BitInt *data, const int64_t bit_index)
static constexpr int64_t BitToIntIndexShift
BitInt mask_single_bit(const int64_t bit_index)
BitInt mask_last_n_bits(const int64_t n)
BitInt mask_first_n_bits(const int64_t n)
BitInt mask_range_bits(const int64_t start, const int64_t size)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
BMesh const char void * data