-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitset.hpp
More file actions
755 lines (684 loc) · 26.3 KB
/
Copy pathbitset.hpp
File metadata and controls
755 lines (684 loc) · 26.3 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#pragma once
#include <immintrin.h>
#include <climits>
#include <compare>
#include <cstring>
#include <type_traits>
#include <utility>
#include "def.hpp"
#pragma GCC push_options
#pragma GCC target("avx2")
namespace cp
{
namespace details
{
template <typename E>
inline constexpr usize expr_bit_size_v = std::remove_cvref_t<E>::BIT_SIZE;
template <typename E>
inline constexpr usize expr_block_count_v = std::remove_cvref_t<E>::BLOCK_COUNT;
template <typename E>
using BitsetExprStorage = std::conditional_t<
std::is_lvalue_reference_v<E>,
E,
std::remove_cvref_t<E>
>;
template <typename XE, typename YE>
concept SameBitsetExprSize = expr_bit_size_v<XE> == expr_bit_size_v<YE>;
inline __m256i bitset_mask(usize bits) {
constexpr usize WORD_SIZE = sizeof(u64) * CHAR_BIT;
constexpr usize BLOCK_SIZE = sizeof(__m256i) / sizeof(u64);
bits %= sizeof(__m256i) * CHAR_BIT;
if (!bits) return _mm256_set1_epi8(-1);
u64 words[BLOCK_SIZE] = {};
for (usize i = 0; i != BLOCK_SIZE && bits; ++i) {
usize take = bits < WORD_SIZE ? bits : WORD_SIZE;
words[i] = take == WORD_SIZE ? ~0ULL : (1ULL << take) - 1;
bits -= take;
}
return _mm256_loadu_si256((const __m256i*)words);
}
template <typename E>
struct BitsetNot {
static constexpr usize BIT_SIZE = expr_bit_size_v<E>;
static constexpr usize BLOCK_COUNT = expr_block_count_v<E>;
BitsetExprStorage<E> e_;
__m256i block_at(usize i) const {
return _mm256_xor_si256(e_.block_at(i), _mm256_set1_epi64x(-1));
}
};
template <typename XE, typename YE>
struct BitsetAnd {
static_assert(SameBitsetExprSize<XE, YE>);
static constexpr usize BIT_SIZE = expr_bit_size_v<XE>;
static constexpr usize BLOCK_COUNT = expr_block_count_v<XE>;
BitsetExprStorage<XE> xe_;
BitsetExprStorage<YE> ye_;
__m256i block_at(usize i) const {
return _mm256_and_si256(ye_.block_at(i), xe_.block_at(i));
}
};
template <typename XE, typename YE>
struct BitsetOr {
static_assert(SameBitsetExprSize<XE, YE>);
static constexpr usize BIT_SIZE = expr_bit_size_v<XE>;
static constexpr usize BLOCK_COUNT = expr_block_count_v<XE>;
BitsetExprStorage<XE> xe_;
BitsetExprStorage<YE> ye_;
__m256i block_at(usize i) const {
return _mm256_or_si256(ye_.block_at(i), xe_.block_at(i));
}
};
template <typename XE, typename YE>
struct BitsetXor {
static_assert(SameBitsetExprSize<XE, YE>);
static constexpr usize BIT_SIZE = expr_bit_size_v<XE>;
static constexpr usize BLOCK_COUNT = expr_block_count_v<XE>;
BitsetExprStorage<XE> xe_;
BitsetExprStorage<YE> ye_;
__m256i block_at(usize i) const {
return _mm256_xor_si256(ye_.block_at(i), xe_.block_at(i));
}
};
template <typename XE, typename YE>
struct BitsetAnt {
static_assert(SameBitsetExprSize<XE, YE>);
static constexpr usize BIT_SIZE = expr_bit_size_v<XE>;
static constexpr usize BLOCK_COUNT = expr_block_count_v<XE>;
BitsetExprStorage<XE> xe_;
BitsetExprStorage<YE> ye_;
__m256i block_at(usize i) const {
return _mm256_andnot_si256(ye_.block_at(i), xe_.block_at(i));
}
};
} // namespace details
template <typename E>
concept BitsetExpr = requires(const std::remove_reference_t<E>& e, usize i) {
e.block_at(i);
details::expr_bit_size_v<E>;
details::expr_block_count_v<E>;
};
template <BitsetExpr E>
inline auto operator~(E&& e) {
return details::BitsetNot<E>{std::forward<E>(e)};
}
template <BitsetExpr XE, BitsetExpr YE>
requires details::SameBitsetExprSize<XE, YE>
inline auto operator&(XE&& xe, YE&& ye) {
return details::BitsetAnd<XE, YE>{std::forward<XE>(xe),
std::forward<YE>(ye)};
}
template <BitsetExpr XE, BitsetExpr YE>
requires details::SameBitsetExprSize<XE, YE>
inline auto operator|(XE&& xe, YE&& ye) {
return details::BitsetOr<XE, YE>{std::forward<XE>(xe),
std::forward<YE>(ye)};
}
template <BitsetExpr XE, BitsetExpr YE>
requires details::SameBitsetExprSize<XE, YE>
inline auto operator^(XE&& xe, YE&& ye) {
return details::BitsetXor<XE, YE>{std::forward<XE>(xe),
std::forward<YE>(ye)};
}
template <BitsetExpr XE, BitsetExpr YE>
requires details::SameBitsetExprSize<XE, YE>
inline auto operator-(XE&& xe, YE&& ye) {
return details::BitsetAnt<XE, YE>{std::forward<XE>(xe),
std::forward<YE>(ye)};
}
template <BitsetExpr XE, BitsetExpr YE>
requires details::SameBitsetExprSize<XE, YE>
inline bool operator==(XE&& xe, YE&& ye) {
for (usize i = 0; i != details::expr_block_count_v<XE>; ++i) {
auto bxa = _mm256_xor_si256(ye.block_at(i), xe.block_at(i));
if (i + 1 == details::expr_block_count_v<XE>)
bxa = _mm256_and_si256(
bxa, details::bitset_mask(details::expr_bit_size_v<XE>)
);
if (!_mm256_testz_si256(bxa, bxa)) return false;
}
return true;
}
template <BitsetExpr XE, BitsetExpr YE>
requires details::SameBitsetExprSize<XE, YE>
inline bool operator!=(XE&& xe, YE&& ye) {
return !(xe == ye);
}
template <BitsetExpr XE, BitsetExpr YE>
requires details::SameBitsetExprSize<XE, YE>
inline std::partial_ordering operator<=>(XE&& xe, YE&& ye) {
bool x_extra = false, y_extra = false;
for (usize i = 0; i != details::expr_block_count_v<XE>; ++i) {
auto x = xe.block_at(i), y = ye.block_at(i);
if (i + 1 == details::expr_block_count_v<XE>) {
auto mask = details::bitset_mask(details::expr_bit_size_v<XE>);
x = _mm256_and_si256(x, mask);
y = _mm256_and_si256(y, mask);
}
x_extra |= !_mm256_testc_si256(y, x);
y_extra |= !_mm256_testc_si256(x, y);
if (x_extra && y_extra) return std::partial_ordering::unordered;
}
return x_extra ? std::partial_ordering::greater
: y_extra ? std::partial_ordering::less
: std::partial_ordering::equivalent;
}
template <usize SIZE>
class Bitset {
using Word = u64;
using Block = __m256i;
public:
static constexpr usize BIT_SIZE = SIZE;
static const usize WORD_SIZE = sizeof(Word) * CHAR_BIT;
static const usize BLOCK_SIZE = sizeof(Block) / sizeof(Word);
static const usize WORD_COUNT = (SIZE + WORD_SIZE - 1) / WORD_SIZE;
static const usize BLOCK_COUNT = (WORD_COUNT + BLOCK_SIZE - 1) / BLOCK_SIZE;
private:
alignas(Block) Word data_[BLOCK_COUNT * BLOCK_SIZE]{};
public:
Bitset() = default;
~Bitset() = default;
Word word_at(usize position) const { return data_[position]; }
Block block_at(usize position) const {
return _mm256_load_si256((const Block*)&data_[position * BLOCK_SIZE]);
}
protected:
void word_set(usize position, Word value) { data_[position] = value; }
void block_set(usize position, Block value) {
_mm256_store_si256((Block*)&data_[position * BLOCK_SIZE], value);
}
void trim() {
if (SIZE % WORD_SIZE)
data_[WORD_COUNT - 1] &= (1ULL << (SIZE % WORD_SIZE)) - 1;
if (BLOCK_COUNT * BLOCK_SIZE > WORD_COUNT)
memset(
data_ + WORD_COUNT, 0,
(BLOCK_COUNT * BLOCK_SIZE - WORD_COUNT) * sizeof(Word)
);
}
void normalize() {
if (SIZE % WORD_SIZE)
data_[WORD_COUNT - 1] &= (1ULL << (SIZE % WORD_SIZE)) - 1;
if (BLOCK_COUNT * BLOCK_SIZE > WORD_COUNT)
memset(
data_ + WORD_COUNT, 0,
(BLOCK_COUNT * BLOCK_SIZE - WORD_COUNT) * sizeof(Word)
);
}
public:
Bitset(const Bitset&) = default;
Bitset(Bitset&&) = default;
Bitset(BitsetExpr auto&& e) {
for (usize i = 0; i != BLOCK_COUNT; ++i) block_set(i, e.block_at(i));
trim();
}
Bitset& operator=(const Bitset&) = default;
Bitset& operator=(Bitset&&) = default;
Bitset& operator=(BitsetExpr auto&& e) {
for (usize i = 0; i != BLOCK_COUNT; ++i) block_set(i, e.block_at(i));
trim();
return *this;
}
bool operator[](usize position) const {
if (position >= SIZE) return false;
return data_[position / WORD_SIZE] >> position % WORD_SIZE & 1;
}
void set_bit(usize position) {
if (position >= SIZE) return;
data_[position / WORD_SIZE] |= 1ULL << position % WORD_SIZE;
}
void unset_bit(usize position) {
if (position >= SIZE) return;
data_[position / WORD_SIZE] &= ~(1ULL << position % WORD_SIZE);
}
void flip_bit(usize position) {
if (position >= SIZE) return;
data_[position / WORD_SIZE] ^= 1ULL << position % WORD_SIZE;
}
usize size() const { return SIZE; }
usize length() const { return SIZE; }
usize count() const {
const Block MASK = _mm256_set1_epi8(0x0F);
const Block TABLE = _mm256_setr_epi8(
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 0, 1, 1, 2, 1, 2, 2,
3, 1, 2, 2, 3, 2, 3, 3, 4
);
Block result = _mm256_setzero_si256();
for (usize i = 0; i != BLOCK_COUNT; ++i) {
Block block = block_at(i);
Block low =
_mm256_shuffle_epi8(TABLE, _mm256_and_si256(block, MASK));
Block high = _mm256_shuffle_epi8(
TABLE, _mm256_and_si256(_mm256_srli_epi16(block, 4), MASK)
);
result = _mm256_add_epi64(
result,
_mm256_sad_epu8(
_mm256_add_epi8(low, high), _mm256_setzero_si256()
)
);
}
Word parts[BLOCK_SIZE];
_mm256_storeu_si256((Block*)parts, result);
return usize(parts[0] + parts[1] + parts[2] + parts[3]);
}
usize popcnt() const { return count(); }
bool none() const {
const usize FULL_BLOCKS = SIZE / (sizeof(Block) * CHAR_BIT);
for (usize i = 0; i != FULL_BLOCKS; ++i) {
Block mask = _mm256_set1_epi8(-1), block = block_at(i);
if (!_mm256_testz_si256(block, mask)) return false;
}
if (usize remaining = SIZE % (sizeof(Block) * CHAR_BIT)) {
Word mask_array[BLOCK_SIZE] = {};
for (usize i = 0; i != BLOCK_SIZE; ++i) {
if (remaining >= WORD_SIZE) {
mask_array[i] = ~0ULL;
remaining -= WORD_SIZE;
} else {
mask_array[i] = (1ULL << remaining) - 1;
break;
}
}
Block mask = _mm256_loadu_si256((const Block*)mask_array),
block = block_at(FULL_BLOCKS);
if (!_mm256_testz_si256(block, mask)) return false;
}
return true;
}
bool any() const {
const usize FULL_BLOCKS = SIZE / (sizeof(Block) * CHAR_BIT);
for (usize i = 0; i != FULL_BLOCKS; ++i) {
Block mask = _mm256_set1_epi8(-1), block = block_at(i);
if (!_mm256_testz_si256(block, mask)) return true;
}
if (usize remaining = SIZE % (sizeof(Block) * CHAR_BIT)) {
Word mask_array[BLOCK_SIZE] = {};
for (usize i = 0; i != BLOCK_SIZE; ++i) {
if (remaining >= WORD_SIZE) {
mask_array[i] = ~0ULL;
remaining -= WORD_SIZE;
} else {
mask_array[i] = (1ULL << remaining) - 1;
break;
}
}
Block mask = _mm256_loadu_si256((const Block*)mask_array),
block = block_at(FULL_BLOCKS);
if (!_mm256_testz_si256(block, mask)) return true;
}
return false;
}
bool all() const {
const usize FULL_BLOCKS = SIZE / (sizeof(Block) * CHAR_BIT);
for (usize i = 0; i != FULL_BLOCKS; ++i) {
Block mask = _mm256_set1_epi8(-1), block = block_at(i);
if (!_mm256_testc_si256(block, mask)) return false;
}
if (usize remaining = SIZE % (sizeof(Block) * CHAR_BIT)) {
Word mask_array[BLOCK_SIZE] = {};
for (usize i = 0; i != BLOCK_SIZE; ++i) {
if (remaining >= WORD_SIZE) {
mask_array[i] = ~0ULL;
remaining -= WORD_SIZE;
} else {
mask_array[i] = (1ULL << remaining) - 1;
break;
}
}
Block mask = _mm256_loadu_si256((const Block*)mask_array),
block = block_at(FULL_BLOCKS);
if (!_mm256_testc_si256(block, mask)) return false;
}
return true;
}
void set_all() {
const Block value = _mm256_set1_epi64x(-1);
for (usize i = 0; i != BLOCK_COUNT; ++i) block_set(i, value);
trim();
}
void unset_all() {
const Block value = _mm256_setzero_si256();
for (usize i = 0; i != BLOCK_COUNT; ++i) block_set(i, value);
trim();
}
void flip_all() {
const Block value = _mm256_set1_epi64x(-1);
for (usize i = 0; i != BLOCK_COUNT; ++i)
block_set(i, _mm256_xor_si256(block_at(i), value));
trim();
}
Bitset& operator&=(BitsetExpr auto&& e) {
for (usize i = 0; i != BLOCK_COUNT; ++i)
block_set(i, _mm256_and_si256(block_at(i), e.block_at(i)));
trim();
return *this;
}
Bitset& operator|=(BitsetExpr auto&& e) {
for (usize i = 0; i != BLOCK_COUNT; ++i)
block_set(i, _mm256_or_si256(block_at(i), e.block_at(i)));
trim();
return *this;
}
Bitset& operator^=(BitsetExpr auto&& e) {
for (usize i = 0; i != BLOCK_COUNT; ++i)
block_set(i, _mm256_xor_si256(block_at(i), e.block_at(i)));
trim();
return *this;
}
Bitset& operator-=(BitsetExpr auto&& e) {
for (usize i = 0; i != BLOCK_COUNT; ++i)
block_set(i, _mm256_andnot_si256(e.block_at(i), block_at(i)));
trim();
return *this;
}
usize find_first_set(usize position) const {
if (position >= SIZE) return usize(-1);
usize word_index = position / WORD_SIZE,
bit_in_word = position % WORD_SIZE;
usize block_index = word_index / BLOCK_SIZE,
word_in_block = word_index % BLOCK_SIZE;
if (Word word = data_[word_index] & ~((1ULL << bit_in_word) - 1)) {
usize result =
word_index * WORD_SIZE + usize(__builtin_ctzll(word));
return result < SIZE ? result : usize(-1);
}
while (++word_in_block != BLOCK_SIZE) {
usize current = block_index * BLOCK_SIZE + word_in_block;
if (current >= WORD_COUNT) break;
if (Word word = data_[current]) {
usize result = current * WORD_SIZE + __builtin_ctzll(word);
return result < SIZE ? result : usize(-1);
}
}
while (++block_index != BLOCK_COUNT) {
Block mask = _mm256_set1_epi64x(-1), block = block_at(block_index);
if (_mm256_testz_si256(block, mask)) continue;
for (usize i = 0; i != BLOCK_SIZE; ++i) {
usize current = block_index * BLOCK_SIZE + i;
if (current >= WORD_COUNT) break;
if (Word word = data_[current]) {
usize result = current * WORD_SIZE + __builtin_ctzll(word);
return result < SIZE ? result : usize(-1);
}
}
}
return usize(-1);
}
usize find_first_unset(usize position) const {
if (position >= SIZE) return usize(-1);
usize word_index = position / WORD_SIZE,
bit_in_word = position % WORD_SIZE;
usize block_index = word_index / BLOCK_SIZE,
word_in_block = word_index % BLOCK_SIZE;
if (Word word = ~data_[word_index] & ~((1ULL << bit_in_word) - 1)) {
usize result =
word_index * WORD_SIZE + usize(__builtin_ctzll(word));
return result < SIZE ? result : usize(-1);
}
while (++word_in_block != BLOCK_SIZE) {
usize current = block_index * BLOCK_SIZE + word_in_block;
if (current >= WORD_COUNT) break;
if (Word word = ~data_[current]) {
usize result = current * WORD_SIZE + __builtin_ctzll(word);
return result < SIZE ? result : usize(-1);
}
}
while (++block_index != BLOCK_COUNT) {
Block mask = _mm256_set1_epi64x(-1), block = block_at(block_index);
if (_mm256_testc_si256(block, mask)) continue;
for (usize i = 0; i != BLOCK_SIZE; ++i) {
usize current = block_index * BLOCK_SIZE + i;
if (current >= WORD_COUNT) break;
if (Word word = ~data_[current]) {
usize result = current * WORD_SIZE + __builtin_ctzll(word);
return result < SIZE ? result : usize(-1);
}
}
}
return usize(-1);
}
void set_range(usize position, usize length) {
if (position + length > SIZE || !length) return;
usize word_index = position / WORD_SIZE,
bit_in_word = position % WORD_SIZE;
if (bit_in_word) {
usize head_length = WORD_SIZE - bit_in_word < length
? WORD_SIZE - bit_in_word
: length;
data_[word_index++] |= ((1ULL << head_length) - 1) << bit_in_word;
length -= head_length;
}
for (
const Block value = _mm256_set1_epi64x(-1);
length >= sizeof(Block) * CHAR_BIT;
) {
_mm256_storeu_si256((Block*)&data_[word_index], value);
length -= sizeof(Block) * CHAR_BIT;
word_index += BLOCK_SIZE;
}
while (length >= WORD_SIZE) {
data_[word_index++] = -1ULL;
length -= WORD_SIZE;
}
data_[word_index] |= (1ULL << length) - 1;
}
void unset_range(usize position, usize length) {
if (position + length > SIZE || !length) return;
usize word_index = position / WORD_SIZE,
bit_in_word = position % WORD_SIZE;
if (bit_in_word) {
usize head_length = WORD_SIZE - bit_in_word < length
? WORD_SIZE - bit_in_word
: length;
data_[word_index++] &=
~(((1ULL << head_length) - 1) << bit_in_word);
length -= head_length;
}
for (
const Block value = _mm256_setzero_si256();
length >= sizeof(Block) * CHAR_BIT;
) {
_mm256_storeu_si256((Block*)&data_[word_index], value);
length -= sizeof(Block) * CHAR_BIT;
word_index += BLOCK_SIZE;
}
while (length >= WORD_SIZE) {
data_[word_index++] = 0ULL;
length -= WORD_SIZE;
}
data_[word_index] &= ~((1ULL << length) - 1);
}
void flip_range(usize position, usize length) {
if (position + length > SIZE || !length) return;
usize word_index = position / WORD_SIZE,
bit_in_word = position % WORD_SIZE;
if (bit_in_word) {
usize head_length = WORD_SIZE - bit_in_word < length
? WORD_SIZE - bit_in_word
: length;
data_[word_index++] ^= ((1ULL << head_length) - 1) << bit_in_word;
length -= head_length;
}
for (
const Block value = _mm256_set1_epi64x(-1);
length >= sizeof(Block) * CHAR_BIT;
) {
_mm256_storeu_si256(
(Block*)&data_[word_index],
_mm256_xor_si256(
_mm256_loadu_si256((const Block*)&data_[word_index]), value
)
);
length -= sizeof(Block) * CHAR_BIT;
word_index += BLOCK_SIZE;
}
while (length >= WORD_SIZE) {
data_[word_index++] ^= -1ULL;
length -= WORD_SIZE;
}
data_[word_index] ^= (1ULL << length) - 1;
}
Bitset& operator<<=(usize step) {
if (step >= SIZE) return unset_all(), *this;
usize word_shift = step / WORD_SIZE, bit_shift = step % WORD_SIZE;
if (!bit_shift) {
memmove(
data_ + word_shift, data_,
(WORD_COUNT - word_shift) * sizeof(Word)
);
memset(data_, 0, word_shift * sizeof(Word));
} else {
usize remaining = WORD_COUNT - word_shift - 1;
Word *destination = data_ + WORD_COUNT,
*source = destination - word_shift;
__m128i l_shift = _mm_cvtsi32_si128(int(bit_shift)),
r_shift = _mm_cvtsi32_si128(int(WORD_SIZE - bit_shift));
while (remaining >= BLOCK_SIZE) {
destination -= BLOCK_SIZE, source -= BLOCK_SIZE;
Block low = _mm256_srl_epi64(
_mm256_loadu_si256((const Block*)&source[-1]), r_shift
);
Block high = _mm256_sll_epi64(
_mm256_loadu_si256((const Block*)&source[0]), l_shift
);
_mm256_storeu_si256(
(Block*)destination, _mm256_or_si256(low, high)
);
remaining -= BLOCK_SIZE;
}
while (remaining) {
--destination, --source;
*destination = (source[0] << bit_shift)
| (source[-1] >> (WORD_SIZE - bit_shift));
--remaining;
}
*--destination = *--source << bit_shift;
memset(data_, 0, word_shift * sizeof(Word));
}
trim();
return *this;
}
Bitset& operator>>=(usize step) {
if (step >= SIZE) return unset_all(), *this;
usize word_shift = step / WORD_SIZE, bit_shift = step % WORD_SIZE;
if (!bit_shift) {
memmove(
data_, data_ + word_shift,
(WORD_COUNT - word_shift) * sizeof(Word)
);
memset(
data_ + WORD_COUNT - word_shift, 0, word_shift * sizeof(Word)
);
} else {
usize remaining = WORD_COUNT - word_shift - 1;
Word *destination = data_, *source = data_ + word_shift;
__m128i r_shift = _mm_cvtsi32_si128(int(bit_shift)),
l_shift = _mm_cvtsi32_si128(int(WORD_SIZE - bit_shift));
while (remaining >= BLOCK_SIZE) {
Block low = _mm256_srl_epi64(
_mm256_loadu_si256((const Block*)&source[0]), r_shift
);
Block high = _mm256_sll_epi64(
_mm256_loadu_si256((const Block*)&source[1]), l_shift
);
_mm256_storeu_si256(
(Block*)destination, _mm256_or_si256(low, high)
);
destination += BLOCK_SIZE, source += BLOCK_SIZE;
remaining -= BLOCK_SIZE;
}
while (remaining) {
*destination = (source[0] >> bit_shift)
| (source[1] << (WORD_SIZE - bit_shift));
++destination, ++source;
--remaining;
}
*destination = *source >> bit_shift;
memset(
data_ + WORD_COUNT - word_shift, 0, word_shift * sizeof(Word)
);
}
trim();
return *this;
}
Bitset operator<<(usize step) const {
Bitset result;
if (step >= SIZE) return result;
usize word_shift = step / WORD_SIZE, bit_shift = step % WORD_SIZE;
if (!bit_shift) {
memcpy(
result.data_ + word_shift, data_,
(WORD_COUNT - word_shift) * sizeof(Word)
);
} else {
usize remaining = WORD_COUNT - word_shift - 1;
Word* destination = result.data_ + WORD_COUNT;
const Word* source = data_ + WORD_COUNT - word_shift;
__m128i l_shift = _mm_cvtsi32_si128(int(bit_shift)),
r_shift = _mm_cvtsi32_si128(int(WORD_SIZE - bit_shift));
while (remaining >= BLOCK_SIZE) {
destination -= BLOCK_SIZE, source -= BLOCK_SIZE;
Block low = _mm256_srl_epi64(
_mm256_loadu_si256((const Block*)&source[-1]), r_shift
);
Block high = _mm256_sll_epi64(
_mm256_loadu_si256((const Block*)&source[0]), l_shift
);
_mm256_storeu_si256(
(Block*)destination, _mm256_or_si256(low, high)
);
remaining -= BLOCK_SIZE;
}
while (remaining) {
--destination, --source;
*destination = (source[0] << bit_shift)
| (source[-1] >> (WORD_SIZE - bit_shift));
--remaining;
}
*--destination = *--source << bit_shift;
}
result.trim();
return result;
}
Bitset operator>>(usize step) const {
Bitset result;
if (step >= SIZE) return result;
usize word_shift = step / WORD_SIZE, bit_shift = step % WORD_SIZE;
if (!bit_shift) {
memcpy(
result.data_, data_ + word_shift,
(WORD_COUNT - word_shift) * sizeof(Word)
);
} else {
usize remaining = WORD_COUNT - word_shift - 1;
Word* destination = result.data_;
const Word* source = data_ + word_shift;
__m128i r_shift = _mm_cvtsi32_si128(int(bit_shift)),
l_shift = _mm_cvtsi32_si128(int(WORD_SIZE - bit_shift));
while (remaining >= BLOCK_SIZE) {
Block low = _mm256_srl_epi64(
_mm256_loadu_si256((const Block*)&source[0]), r_shift
);
Block high = _mm256_sll_epi64(
_mm256_loadu_si256((const Block*)&source[1]), l_shift
);
_mm256_storeu_si256(
(Block*)destination, _mm256_or_si256(low, high)
);
destination += BLOCK_SIZE, source += BLOCK_SIZE;
remaining -= BLOCK_SIZE;
}
while (remaining) {
*destination = (source[0] >> bit_shift)
| (source[1] << (WORD_SIZE - bit_shift));
++destination, ++source;
--remaining;
}
*destination = *source >> bit_shift;
}
result.trim();
return result;
}
};
} // namespace cp
#pragma GCC pop_options