-
Notifications
You must be signed in to change notification settings - Fork 1
/
detail_bitmask.h
367 lines (327 loc) · 11 KB
/
detail_bitmask.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#ifndef PROTOTYPE_DETAIL_BITMASK_H_
#define PROTOTYPE_DETAIL_BITMASK_H_
#include "detail.h"
#include "constexpr_wrapper.h"
#include <type_traits>
#include <bitset>
namespace std::__detail
{
inline constexpr size_t
__div_roundup(size_t __a, size_t __b)
{ return (__a + __b - 1) / __b; }
template <size_t _Np, bool _Sanitized>
struct _BitMask
{
static_assert(_Np > 0);
static constexpr size_t _NBytes = __div_roundup(_Np, __CHAR_BIT__);
using _Tp = conditional_t<_Np == 1, bool,
make_unsigned_t<__mask_integer_from<
std::min(sizeof(0ULL), std::__bit_ceil(_NBytes))
>>>;
static constexpr int _S_array_size = __div_roundup(_NBytes, sizeof(_Tp));
_Tp _M_bits[_S_array_size];
static constexpr int _S_unused_bits
= _Np == 1 ? 0 : _S_array_size * sizeof(_Tp) * __CHAR_BIT__ - _Np;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wbool-operation"
static constexpr _Tp _S_bitmask = +_Tp(~_Tp()) >> _S_unused_bits;
#pragma GCC diagnostic pop
constexpr _BitMask() noexcept = default;
template <unsigned_integral _Up>
constexpr _BitMask(_Up __x) noexcept
: _M_bits{_Sanitized ? static_cast<_Tp>(_S_bitmask & __x) : static_cast<_Tp>(__x)} {}
template <integral _Up>
requires (_Sanitized)
_GLIBCXX_SIMD_ALWAYS_INLINE static constexpr _BitMask
__create_unchecked(_Up __x)
{ return {{static_cast<_Tp>(__x)}}; }
constexpr
_BitMask(bitset<_Np> __x) noexcept
: _M_bits(static_cast<_Tp>(__x.to_ullong()))
{}
constexpr _BitMask(const _BitMask&) noexcept = default;
template <bool _RhsSanitized>
requires (_RhsSanitized == false and _Sanitized == true)
constexpr
_BitMask(const _BitMask<_Np, _RhsSanitized>& __rhs) noexcept
: _BitMask(__rhs._M_sanitized()) {}
constexpr _Tp
_M_to_bits() const noexcept
{
static_assert(_Sanitized);
static_assert(_S_array_size == 1);
return _M_bits[0];
}
constexpr _Tp
_M_to_unsanitized_bits() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
constexpr bitset<_Np>
_M_to_bitset() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
constexpr decltype(auto)
_M_sanitized() const noexcept
{
if constexpr (_Sanitized)
return *this;
else if constexpr (_Np == 1)
return _SanitizedBitMask<1>::__create_unchecked(_M_bits[0]);
else
{
_SanitizedBitMask<_Np> __r = {};
for (int __i = 0; __i < _S_array_size; ++__i)
__r._M_bits[__i] = _M_bits[__i];
if constexpr (_S_unused_bits > 0)
__r._M_bits[_S_array_size - 1] &= _S_bitmask;
return __r;
}
}
template <size_t _Mp, bool _LSanitized>
constexpr _BitMask<_Np + _Mp, _Sanitized>
_M_prepend(_BitMask<_Mp, _LSanitized> __lsb) const noexcept
{
constexpr size_t _RN = _Np + _Mp;
using _Rp = _BitMask<_RN, _Sanitized>;
if constexpr (_Rp::_S_array_size == 1)
{
_Rp __r{{_M_bits[0]}};
__r._M_bits[0] <<= _Mp;
__r._M_bits[0] |= __lsb._M_sanitized()._M_bits[0];
return __r;
}
else
__assert_unreachable<_Rp>();
}
// Return a new _BitMask with size _NewSize while dropping _DropLsb least
// significant bits. If the operation implicitly produces a sanitized bitmask,
// the result type will have _Sanitized set.
template <size_t _DropLsb, size_t _NewSize = _Np - _DropLsb>
constexpr auto
_M_extract() const noexcept
{
static_assert(_Np > _DropLsb);
static_assert(_DropLsb + _NewSize <= sizeof(0ULL) * __CHAR_BIT__,
"not implemented for bitmasks larger than one ullong");
if constexpr (_NewSize == 1)
return _SanitizedBitMask<1>::__create_unchecked(_M_bits[0] >> _DropLsb);
else
return _BitMask<_NewSize,
((_NewSize + _DropLsb == sizeof(_Tp) * __CHAR_BIT__
&& _NewSize + _DropLsb <= _Np)
|| ((_Sanitized || _Np == sizeof(_Tp) * __CHAR_BIT__)
&& _NewSize + _DropLsb >= _Np))>(_M_bits[0]
>> _DropLsb);
}
// True if all bits are set. Implicitly sanitizes if _Sanitized == false.
constexpr bool
all() const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().all();
else
{
constexpr _Tp __allbits = ~_Tp();
for (int __i = 0; __i < _S_array_size - 1; ++__i)
if (_M_bits[__i] != __allbits)
return false;
return _M_bits[_S_array_size - 1] == _S_bitmask;
}
}
// True if at least one bit is set. Implicitly sanitizes if _Sanitized ==
// false.
constexpr bool
any() const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().any();
else
{
for (int __i = 0; __i < _S_array_size - 1; ++__i)
if (_M_bits[__i] != 0)
return true;
return _M_bits[_S_array_size - 1] != 0;
}
}
// True if no bit is set. Implicitly sanitizes if _Sanitized == false.
constexpr bool
none() const noexcept
{
if constexpr (_Np == 1)
return !_M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().none();
else
{
for (int __i = 0; __i < _S_array_size - 1; ++__i)
if (_M_bits[__i] != 0)
return false;
return _M_bits[_S_array_size - 1] == 0;
}
}
// Returns the number of set bits. Implicitly sanitizes if _Sanitized ==
// false.
constexpr int
count() const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().none();
else
{
int __result = __builtin_popcountll(_M_bits[0]);
for (int __i = 1; __i < _S_array_size; ++__i)
__result += __builtin_popcountll(_M_bits[__i]);
return __result;
}
}
// Returns the bit at offset __i as bool.
constexpr bool
operator[](size_t __i) const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (_S_array_size == 1)
return (_M_bits[0] >> __i) & 1;
else
{
const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
return (_M_bits[__j] >> __shift) & 1;
}
}
constexpr bool
operator[](vir::constexpr_value auto __i) const noexcept
{
static_assert(__i < _Np);
constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
return static_cast<bool>(_M_bits[__j] & (_Tp(1) << __shift));
}
// Set the bit at offset __i to __x.
constexpr void
set(size_t __i, bool __x) noexcept
{
if constexpr (_Np == 1)
_M_bits[0] = __x;
else if constexpr (_S_array_size == 1)
{
_M_bits[0] &= ~_Tp(_Tp(1) << __i);
_M_bits[0] |= _Tp(_Tp(__x) << __i);
}
else
{
const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
_M_bits[__j] &= ~_Tp(_Tp(1) << __shift);
_M_bits[__j] |= _Tp(_Tp(__x) << __shift);
}
}
constexpr void
set(vir::constexpr_value auto __i, bool __x) noexcept
{
static_assert(__i < _Np);
if constexpr (_Np == 1)
_M_bits[0] = __x;
else
{
constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
constexpr _Tp __mask = ~_Tp(_Tp(1) << __shift);
_M_bits[__j] &= __mask;
_M_bits[__j] |= _Tp(_Tp(__x) << __shift);
}
}
// Inverts all bits. Sanitized input leads to sanitized output.
constexpr _BitMask
operator~() const noexcept
{
if constexpr (_Np == 1)
return !_M_bits[0];
else
{
_BitMask __result{};
for (int __i = 0; __i < _S_array_size - 1; ++__i)
__result._M_bits[__i] = ~_M_bits[__i];
if constexpr (_Sanitized)
__result._M_bits[_S_array_size - 1]
= _M_bits[_S_array_size - 1] ^ _S_bitmask;
else
__result._M_bits[_S_array_size - 1] = ~_M_bits[_S_array_size - 1];
return __result;
}
}
constexpr _BitMask&
operator^=(const _BitMask& __b) & noexcept
{
_GLIBCXX_SIMD_INT_PACK(_S_array_size, _Is, {
((_M_bits[_Is] ^= __b._M_bits[_Is]), ...);
});
return *this;
}
constexpr _BitMask&
operator|=(const _BitMask& __b) & noexcept
{
_GLIBCXX_SIMD_INT_PACK(_S_array_size, _Is, {
((_M_bits[_Is] |= __b._M_bits[_Is]), ...);
});
return *this;
}
constexpr _BitMask&
operator&=(const _BitMask& __b) & noexcept
{
_GLIBCXX_SIMD_INT_PACK(_S_array_size, _Is, {
((_M_bits[_Is] &= __b._M_bits[_Is]), ...);
});
return *this;
}
friend constexpr _BitMask
operator^(const _BitMask& __a, const _BitMask& __b) noexcept
{
_BitMask __r = __a;
__r ^= __b;
return __r;
}
friend constexpr _BitMask
operator|(const _BitMask& __a, const _BitMask& __b) noexcept
{
_BitMask __r = __a;
__r |= __b;
return __r;
}
friend constexpr _BitMask
operator&(const _BitMask& __a, const _BitMask& __b) noexcept
{
_BitMask __r = __a;
__r &= __b;
return __r;
}
_GLIBCXX_SIMD_INTRINSIC
constexpr bool
_M_is_constprop() const
{
if constexpr (_S_array_size == 0)
return __builtin_constant_p(_M_bits[0]);
else
{
for (int __i = 0; __i < _S_array_size; ++__i)
if (!__builtin_constant_p(_M_bits[__i]))
return false;
return true;
}
}
};
template <integral _Tp>
_BitMask(_Tp)
-> _BitMask<__CHAR_BIT__ * sizeof(_Tp)>;
}
#endif // PROTOTYPE_DETAIL_BITMASK_H_