Nebula
Loading...
Searching...
No Matches
bitfield.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
18#include "core/types.h"
19
20//------------------------------------------------------------------------------
21namespace Util
22{
23template <unsigned int NUMBITS> class BitField
24{
25 static_assert(NUMBITS > 0);
26
27public:
31 constexpr BitField(std::initializer_list<unsigned int> list);
33 BitField(const BitField<NUMBITS>& rhs) = default;
34
36 constexpr bool operator==(const BitField<NUMBITS>& rhs) const;
38 constexpr bool operator!=(const BitField<NUMBITS>& rhs) const;
39
41 constexpr bool IsSet(const uint64_t bitIndex) const;
43 template <uint64_t bitIndex> constexpr bool IsSet() const;
45 void Clear();
47 bool IsNull() const;
49 constexpr void SetBit(const uint64_t bitIndex);
51 constexpr void SetBitIf(const uint64_t bitIndex, uint64_t cond);
53 template <uint64_t bitIndex> constexpr void SetBit();
55 void ClearBit(const uint64_t bitIndex);
56
62 bool SectionIsNull(uint64_t section) const;
63
65 static constexpr BitField<NUMBITS> Or(const BitField<NUMBITS>& b0, const BitField<NUMBITS>& b1);
67 static constexpr BitField<NUMBITS> And(const BitField<NUMBITS>& b0, const BitField<NUMBITS>& b1);
68
69private:
70 static constexpr uint64_t BASE = NUMBITS > 32 ? 64 : NUMBITS > 16 ? 32 : NUMBITS > 8 ? 16 : 8;
71 static const int size = ((NUMBITS + BASE - 1) / BASE);
72
73 // Template magic to automatically use the smallest type possible
74 template <size_t S> struct BitType
75 {
76 using T = uint8_t;
77 };
78 template <> struct BitType<16>
79 {
80 using T = uint16_t;
81 };
82 template <> struct BitType<32>
83 {
84 using T = uint32_t;
85 };
86 template <> struct BitType<64>
87 {
88 using T = uint64_t;
89 };
90
91 using TYPE = typename BitType<BASE>::T;
92
94};
95
96//------------------------------------------------------------------------------
99template <unsigned int NUMBITS> BitField<NUMBITS>::BitField()
100{
101 IndexT i;
102 for (i = 0; i < size; i++)
103 {
104 this->bits[i] = 0;
105 }
106}
107
108//------------------------------------------------------------------------------
111template <unsigned int NUMBITS> constexpr BitField<NUMBITS>::BitField(std::initializer_list<unsigned int> list)
112{
113 for (IndexT i = 0; i < size; i++)
114 {
115 this->bits[i] = 0;
116 }
117
118 for (auto bit : list)
119 {
120 this->SetBit(bit);
121 }
122}
123
124//------------------------------------------------------------------------------
127template <unsigned int NUMBITS>
128constexpr bool
130{
131 for (IndexT i = 0; i < size; i++)
132 {
133 if (this->bits[i] != rhs.bits[i])
134 {
135 return false;
136 }
137 }
138 return true;
139}
140
141//------------------------------------------------------------------------------
144template <unsigned int NUMBITS>
145constexpr bool
147{
148 return !(*this == rhs);
149}
150
151//------------------------------------------------------------------------------
154template <unsigned int NUMBITS>
155constexpr bool
156BitField<NUMBITS>::IsSet(const uint64_t bitIndex) const
157{
158 n_assert(bitIndex < NUMBITS);
159 const TYPE i = (1ull << (bitIndex % BASE));
160 const TYPE index = bitIndex / BASE;
161 return (this->bits[index] & i) == i;
162}
163
164//------------------------------------------------------------------------------
167template <unsigned int NUMBITS>
168template <uint64_t bitIndex>
169constexpr bool
171{
172 static_assert(bitIndex < NUMBITS);
173 constexpr TYPE i = (1ull << (bitIndex % BASE));
174 constexpr TYPE index = bitIndex / BASE;
175 return (this->bits[index] & i) == i;
176}
177
178//------------------------------------------------------------------------------
181template <unsigned int NUMBITS>
182void
184{
185 IndexT i;
186 for (i = 0; i < size; i++)
187 {
188 this->bits[i] = 0;
189 }
190}
191
192//------------------------------------------------------------------------------
195template <unsigned int NUMBITS>
196bool
198{
199 IndexT i;
200 for (i = 0; i < size; i++)
201 {
202 if (this->bits[i] != 0)
203 {
204 return false;
205 }
206 }
207 return true;
208}
209
210//------------------------------------------------------------------------------
213template <unsigned int NUMBITS>
214constexpr void
216{
217 n_assert(i < NUMBITS);
218 const TYPE index = i / BASE;
219 const TYPE bit = (1ull << (i % BASE));
220 this->bits[index] |= bit;
221}
222
223//------------------------------------------------------------------------------
226template <unsigned int NUMBITS>
227constexpr void
228BitField<NUMBITS>::SetBitIf(const uint64_t i, uint64_t cond)
229{
230 n_assert(i < NUMBITS);
231 const TYPE index = i / BASE;
232 const TYPE bit = (1ull << (i % BASE));
233 this->bits[index] |= bit * cond;
234}
235
236//------------------------------------------------------------------------------
239template <unsigned int NUMBITS>
240template <uint64_t i>
241constexpr void
243{
244 static_assert(i < NUMBITS);
245 constexpr TYPE index = i / BASE;
246 constexpr TYPE bit = (1ull << (i % BASE));
247 this->bits[index] |= bit;
248}
249
250//------------------------------------------------------------------------------
253template <unsigned int NUMBITS>
254void
256{
257 n_assert(i < NUMBITS);
258 const TYPE index = i / BASE;
259 const TYPE bit = ~(1ull << (i % BASE));
260 this->bits[index] &= bit;
261}
262
263//------------------------------------------------------------------------------
266template <unsigned int NUMBITS>
267bool
268BitField<NUMBITS>::SectionIsNull(uint64_t section) const
269{
270 n_assert(section < this->size);
271 return this->bits[section] == 0;
272}
273
274//------------------------------------------------------------------------------
277template <unsigned int NUMBITS>
278constexpr BitField<NUMBITS>
280{
282 for (IndexT i = 0; i < size; i++)
283 {
284 res.bits[i] = b0.bits[i] | b1.bits[i];
285 }
286 return res;
287}
288
289//------------------------------------------------------------------------------
292template <unsigned int NUMBITS>
293constexpr BitField<NUMBITS>
295{
297 for (IndexT i = 0; i < size; i++)
298 {
299 res.bits[i] = b0.bits[i] & b1.bits[i];
300 }
301 return res;
302}
303
304} // namespace Util
305//------------------------------------------------------------------------------
Implements large bit field.
Definition bitfield.h:24
constexpr bool operator==(const BitField< NUMBITS > &rhs) const
equality operator
Definition bitfield.h:129
static constexpr uint64_t BASE
Definition bitfield.h:70
void ClearBit(const uint64_t bitIndex)
clear a bit by index
Definition bitfield.h:255
constexpr bool operator!=(const BitField< NUMBITS > &rhs) const
inequality operator
Definition bitfield.h:146
BitField(const BitField< NUMBITS > &rhs)=default
copy constructor
bool IsNull() const
return true if all bits are 0
Definition bitfield.h:197
constexpr void SetBit()
set a bit by index
Definition bitfield.h:242
bool SectionIsNull(uint64_t section) const
Check if a section of the bitfield is set.
Definition bitfield.h:268
static constexpr BitField< NUMBITS > And(const BitField< NUMBITS > &b0, const BitField< NUMBITS > &b1)
set bitfield to AND combination
Definition bitfield.h:294
typename BitType< BASE >::T TYPE
Definition bitfield.h:91
constexpr void SetBitIf(const uint64_t bitIndex, uint64_t cond)
set a bit by index. Multiplies the bit with cond before OR-ing which means it won't set the bit if mu...
Definition bitfield.h:228
constexpr void SetBit(const uint64_t bitIndex)
set a bit by index
Definition bitfield.h:215
constexpr BitField(std::initializer_list< unsigned int > list)
constructs a bitfield based on multiple values
Definition bitfield.h:111
void Clear()
clear content
Definition bitfield.h:183
BitField()
constructor
Definition bitfield.h:99
constexpr bool IsSet(const uint64_t bitIndex) const
Check if single bit is set.
Definition bitfield.h:156
TYPE bits[size]
Definition bitfield.h:93
constexpr bool IsSet() const
Check if single bit is set.
Definition bitfield.h:170
static constexpr BitField< NUMBITS > Or(const BitField< NUMBITS > &b0, const BitField< NUMBITS > &b1)
set bitfield to OR combination
Definition bitfield.h:279
static const int size
Definition bitfield.h:71
#define n_assert(exp)
Definition debug.h:50
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
constexpr uint64 SetBit(uint64 mask, uint8 bit)
Definition bit.h:21
uint16_t T
Definition bitfield.h:80
uint32_t T
Definition bitfield.h:84
uint64_t T
Definition bitfield.h:88
Definition bitfield.h:75
uint8_t T
Definition bitfield.h:76
int IndexT
Definition types.h:48