-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitarray-impl.hh
419 lines (400 loc) · 12.4 KB
/
bitarray-impl.hh
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
template <typename T>
concept VariableSize = requires(T t) {
t._size;
};
template <typename Specific, typename Traits>
struct bitarray_impl
{
using Container = typename Traits::Container;
using WordType = Container::value_type;
using self_type = bitarray_impl<Specific, Traits>;
static constexpr auto variable_sized = VariableSize<Specific>;
Container data_;
bitarray_impl() = default;
bitarray_impl(Container c) : data_(c) {}
template <typename Other>
bitarray_impl(Other l)
{
std::copy(l.begin(), l.begin() + std::min(l.size(), data_.size()), data_.begin());
sanitize();
}
static constexpr auto WordBits = std::numeric_limits<WordType>::digits;
template <class CharT, class CTraits>
friend std::basic_ostream<CharT, CTraits> &operator<<(std::basic_ostream<CharT, CTraits> &os, const self_type &x)
{
int base_digits = 0;
if (os.flags() & std::ios_base::dec) {
base_digits = 1; //XXX this is a hack, rather than default to decimal, we default to binary
os << "0b";
} else if (os.flags() & std::ios_base::oct) {
base_digits = 3;
os << "0o";
} else if (os.flags() & std::ios_base::hex) {
base_digits = 4;
os << "0x";
}
for (ssize_t i = x.size(); i -= base_digits;) {
unsigned char digit = 0;
for (int j = 0; j < base_digits; j++) {
digit <<= 1;
digit += x[i + j];
}
os << std::to_string(digit);
}
return os;
}
size_t size() const
{
if constexpr (!variable_sized)
{
return std::size(data_) * WordBits;
}
else
{
if (Specific::_size == std::dynamic_extent)
{
return std::size(data_) * WordBits;
}
else
{
return Specific::_size;
}
}
}
Container &data()
{
return data_;
}
private:
constexpr WordType zero() const {
return static_cast<WordType>(0);
}
constexpr WordType one() const {
return static_cast<WordType>(1);
}
constexpr WordType ones() const {
return ~static_cast<WordType>(0);
}
protected:
void sanitize() {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshift-count-overflow"
if (size() % WordBits != 0) {
data_.back() &= ones() >> (WordBits - size() % WordBits);
}
#pragma GCC diagnostic pop
}
public:
bool all() const {
if (size() % WordBits == 0) {
for (size_t i = 0; i < std::size(data_); i++)
if (data_[i] != ones())
return false;
} else {
if (data_.back() != ones() >> (WordBits - size() % WordBits))
return false;
for (size_t i = 0; i + 1 < std::size(data_); i++)
if (data_[i] != ones())
return false;
}
return true;
}
bool any() const {
for (auto &x : data_)
if (x != 0)
return true;
return false;
}
bool none() const {
for (auto &x : data_)
if (x != 0)
return false;
return true;
}
int popcount() const {
return count();
}
size_t count() const {
size_t count = 0;
for (auto &x : data_)
if constexpr (sizeof(WordType) <= 8) {
count += std::popcount(x);
} else if (sizeof(WordType) <= 16) {
count += std::popcount(static_cast<uint64_t>(x >> 64)) + std::popcount(static_cast<uint64_t>(x));
}
return count;
}
bool has_single_bit() const {
return count() == 1;
}
int countr_zero() const {
for (size_t i = 0; i < std::size(data_); i++)
if (data_[i] != 0)
return i * WordBits + std::countr_zero(data_[i]);
return size();
}
int countr_one() const {
for (size_t i = 0; i < std::size(data_); i++)
if (data_[i] != ones())
return i * WordBits + std::countr_one(data_[i]);
return size();
}
int countl_zero() const {
for (size_t i = std::size(data_); i--;)
if (data_[i] != 0)
return size() - i * WordBits - (WordBits - std::countl_zero(data_[i]));
return size();
}
int countl_one() const {
size_t i = std::size(data_);
if (size() % WordBits != 0) {
if (data_.back() != (ones() >> (WordBits - size() % WordBits)))
return std::countl_one(data_.back() << (WordBits - size() % WordBits));
i--;
}
for (; i--;)
if (data_[i] != ones())
return size() - i * WordBits - (WordBits - std::countl_one(data_[i]));
return size();
}
int bit_width() const {
return size() - countl_zero();
}
self_type bit_floor() const {
int w = bit_width();
reset();
if (w != 0) {
set(w - 1);
}
return *this;
}
self_type bit_ceil() const {
int w = bit_width();
bool x = has_single_bit();
reset();
if (x) {
set(w - 1);
} else {
set(w);
}
return *this;
}
void wordswap() {
std::reverse(std::begin(data_), std::end(data_));
}
void byteswap() {
wordswap();
for (auto &x : data_)
{
//std::byteswap(x);
}
}
void bitswap() {
byteswap();
//TODO bitswap
}
self_type set() {
for (auto &x : data_)
x = ones();
sanitize();
return *this;
}
constexpr self_type set(size_t pos, bool value = true) {
if (pos >= size()) {
throw std::out_of_range{"set() called with pos " + std::to_string(pos) + " on bitset of size " + std::to_string(size())};
}
if (value) {
data_[pos / WordBits] |= one() << (pos % WordBits);
} else {
data_[pos / WordBits] &= ~(one() << (pos % WordBits));
}
return *this;
}
self_type reset() {
for (auto &x : data_)
x = zero();
return *this;
}
self_type reset(size_t pos) {
if (pos >= size()) {
throw std::out_of_range{"reset() called with pos " + std::to_string(pos) + " on bitset of size " + std::to_string(size())};
}
data_[pos / WordBits] &= ~(one() << (pos % WordBits));
return *this;
}
self_type flip() {
for (auto &x : data_)
x = ~x;
sanitize();
return *this;
}
self_type flip(size_t pos) {
if (pos >= size()) {
throw std::out_of_range{"flip() called with pos " + std::to_string(pos) + " on bitset of size " + std::to_string(size())};
}
data_[pos / WordBits] ^= one() << (pos % WordBits);
return *this;
}
void set_word_at_pos(WordType x, size_t pos) {
if (pos >= size()) {
return;
}
size_t offset = pos % WordBits;
data_[pos / WordBits] |= x << offset;
if (offset != 0 && pos / WordBits + 1 < std::size(data_))
{
data_[pos / WordBits + 1] |= x >> (WordBits - offset);
}
}
WordType get_word_at_pos(size_t pos) const {
if (pos >= size()) {
throw std::out_of_range{"get_word_at_pos() called with pos " + std::to_string(pos) + " on bitset of size " + std::to_string(size())};
}
size_t offset = pos % WordBits;
WordType out = data_[pos / WordBits] >> offset;
if (offset != 0 && pos / WordBits + 1 < std::size(data_))
{
out |= data_[pos / WordBits + 1] << (WordBits - offset);
}
return out;
}
bool operator==(const self_type& rhs) const {
for (size_t i = 0; i < std::size(data_); i++)
if (data_[i] != rhs.data_[i])
return false;
return true;
}
bool operator!=(const self_type& rhs) const {
return !(*this == rhs);
}
constexpr bool at(size_t pos) const {
if (pos >= size()) {
throw std::out_of_range{"at() called with pos " + std::to_string(pos) + " on bitset of size " + std::to_string(size())};
}
return *this[pos];
}
constexpr bool operator[](size_t pos) const {
return static_cast<bool>((data_[pos / WordBits] >> (pos % WordBits)) & 1);
}
self_type operator&=(const self_type& rhs) {
for (size_t i = 0; i < std::size(data_); i++)
data_[i] &= rhs.data_[i];
return *this;
}
self_type operator|=(const self_type& rhs) {
for (size_t i = 0; i < std::size(data_); i++)
data_[i] |= rhs.data_[i];
return *this;
}
self_type operator^=(const self_type& rhs) {
for (size_t i = 0; i < std::size(data_); i++)
data_[i] ^= rhs.data_[i];
sanitize();
return *this;
}
self_type operator<<=(size_t shift) {
for (size_t i = std::size(data_); i--;)
{
auto x = data_[i];
data_[i] = 0;
set_word_at_pos(x, i * WordBits + shift);
}
return *this;
}
self_type operator<<(size_t shift) {
self_type x = *this;
x <<= shift;
return x;
}
self_type operator>>=(size_t shift) {
for (size_t i = 0; i < std::size(data_); i++)
{
if (shift + i * WordBits < size()) {
data_[i] = get_word_at_pos(shift + i * WordBits);
} else {
data_[i] = 0;
}
}
return *this;
}
self_type operator>>(size_t shift) {
self_type x = *this;
x >>= shift;
return x;
}
self_type rotl(int shift) {
if (shift < 0) {
return rotr(-shift);
}
shift %= size();
//FIXME implement without temporaries
//return (*this << shift) | (*this >> (size() - shift));
return *this;
}
self_type rotr(int shift) {
if (shift < 0) {
return rotl(-shift);
}
shift %= size();
//FIXME implement without temporaries
//return (*this >> shift) | (*this << (size() - shift));
return *this;
}
/*
FIXME
template<size_t M, size_t O = M>
bitarray<O, WordType> gather(bitarray<M, WordType> mask) {
static_assert(M <= size(), "gather operation mask length must be <= input length");
bitarray<O, WordType> output {};
for (size_t i = 0, pos = 0; i < mask.std::size(data_); i++) {
output.set_word_at_pos(pext(
data_[i],
mask.data_[i]
), pos);
pos += std::popcount(mask.data_[i]);
}
return output;
}
template<size_t M>
bitarray<M, WordType> scatter(bitarray<M, WordType> mask) {
static_assert(M >= size(), "scatter operation mask length must be >= input length");
bitarray<M, WordType> output {};
for (size_t i = 0, pos = 0; i < output.std::size(data_); i++) {
output.data_[i] = pdep(
get_word_at_pos(pos),
mask.data_[i]
);
pos += std::popcount(mask.data_[i]);
}
return output;
}
template<size_t Len, size_t Num>
static constexpr std::array<bitarray<Len>, Num> interleave_masks() {
std::array<bitarray<Len>, Num> x{};
for (size_t i = 0; i < Num; i++) {
for (size_t j = i; j < Len; j += Num) {
x[i].set(j);
}
}
return x;
}
template<size_t Len, size_t Num>
static bitarray<Len * Num> interleave(std::array<bitarray<Len>, Num> input) {
bitarray<Len * Num> output {};
std::array<bitarray<Len * Num>, Num> masks = interleave_masks<Len * Num, Num>();
for (size_t j = 0; j < input.size(); j++) {
output |= input[j].template scatter<Len * Num>(masks[j]);
}
return output;
}
template<size_t Len, size_t Num>
static std::array<bitarray<Len>, Num> deinterleave(bitarray<Len * Num> input) {
std::array<bitarray<Len>, Num> output {};
std::array<bitarray<Len * Num>, Num> masks = interleave_masks<Len * Num, Num>();
for (size_t j = 0; j < output.size(); j++) {
output[j] = input.template gather<Len * Num, Len>(masks[j]);
}
return output;
}
*/
};