forked from vreverdy/bit-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift.hpp
471 lines (434 loc) · 16.4 KB
/
shift.hpp
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
// ================================ SHIFT ================================== //
// Project: The Experimental Bit Algorithms Library
// Name: shift.hpp
// Description: Implementation of shift_left and shift_right
// Creator: Vincent Reverdy
// Contributor(s): Bryce Kille [2019]
// License: BSD 3-Clause License
// ========================================================================== //
#ifndef _SHIFT_HPP_INCLUDED
#define _SHIFT_HPP_INCLUDED
// ========================================================================== //
// ================================ PREAMBLE ================================ //
// C++ standard library
// Project sources
// Third-party libraries
#include <iterator>
#define is_aligned(POINTER, BYTE_COUNT) \
(((uintptr_t)(const void *)(POINTER)) % (BYTE_COUNT) == 0)
#include <simdpp/simd.h>
// Miscellaneous
namespace bit {
// ========================================================================== //
// --------------------------- Shift Algorithms ----------------------------- //
template <class ForwardIt>
bit_iterator<ForwardIt> shift_right_dispatch(
bit_iterator<ForwardIt> first,
bit_iterator<ForwardIt> last,
typename bit_iterator<ForwardIt>::difference_type n,
std::forward_iterator_tag
)
{
// Assertions
_assert_range_viability(first, last);
// Types and constants
using word_type = typename bit_iterator<ForwardIt>::word_type;
using size_type = typename bit_iterator<ForwardIt>::size_type;
constexpr size_type digits = binary_digits<word_type>::value;
// Initialization
size_type word_shifts = n / digits;
size_type remaining_bitshifts = n - digits*(word_shifts);
const bool is_first_aligned = first.position() == 0;
const bool is_last_aligned = last.position() == 0;
auto d = distance(first, last);
// Out of range cases
if (n <= 0) return first;
else if (n >= d) return last;
// Single word case
if (first.base() == last.base()) {
*first.base() = _bitblend<word_type>(
*first.base(),
(
*first.base() & (
static_cast<word_type>(-1) << first.position()
)
) << n,
first.position(),
last.position() - first.position()
);
return bit_iterator<ForwardIt>(
first.base(),
first.position() + n
);
}
// Multiple word case
word_type first_value = *first.base();
word_type last_value = !is_last_aligned ? *last.base() : 0;
word_type mask = is_first_aligned ?
static_cast<word_type>(-1)
:
static_cast<word_type>(
(static_cast<word_type>(1) << (digits - first.position())) - 1
) << first.position();
*first.base() = *first.base() & mask;
// Shift words to the right
ForwardIt it = word_shift_right(first.base(),
std::next(last.base(),
!is_last_aligned
),
word_shifts
);
bit_iterator<ForwardIt> d_first(it, first.position());
// Shift bit sequence to the msb
if (remaining_bitshifts) {
word_type temp_1 = *it;
word_type temp_2;
*it = *it << remaining_bitshifts;
it++;
//TODO probably a way to do this with 1 temp or
// at least no value swapping
for (; it != std::next(last.base(), !is_last_aligned); ++it) {
temp_2 = *it;
*it = _shld<word_type>(*it, temp_1, remaining_bitshifts);
temp_1 = temp_2;
}
}
// Blend bits of the first element
if (!is_first_aligned) {
*first.base() = _bitblend<word_type>(
first_value,
*first.base(),
first.position(),
digits - first.position()
);
}
// Blend bits of the last element
if (!is_last_aligned) {
*last.base() = _bitblend<word_type>(
*last.base(),
last_value,
last.position(),
digits - last.position()
);
}
std::advance(d_first, remaining_bitshifts);
return d_first;
}
template <class ForwardIt>
bit_iterator<ForwardIt> shift_left_dispatch(
bit_iterator<ForwardIt> first,
bit_iterator<ForwardIt> last,
typename bit_iterator<ForwardIt>::difference_type n,
std::forward_iterator_tag
)
{
// Types and constants
using word_type = typename bit_iterator<ForwardIt>::word_type;
using size_type = typename bit_iterator<ForwardIt>::size_type;
constexpr size_type digits = binary_digits<word_type>::value;
// Initialization
size_type word_shifts = n / digits;
size_type remaining_bitshifts = n - digits*(word_shifts);
const bool is_first_aligned = first.position() == 0;
const bool is_last_aligned = last.position() == 0;
auto d = distance(first, last);
// Multiple word case
word_type first_value = *first.base();
word_type last_value = !is_last_aligned ? *last.base() : 0;
// Shift words to the left using std::shift
ForwardIt new_last_base = word_shift_left(first.base(),
last.base(),
word_shifts
);
if (!is_last_aligned) {
// Mask out-of-range bits so that we don't incorporate them
*last.base() &= (static_cast<word_type>(1) << last.position()) - 1;
*new_last_base = *last.base();
if (word_shifts > 0) {
*last.base() = 0;
}
}
// Shift bit sequence to the lsb
if (remaining_bitshifts) {
ForwardIt it = first.base();
// _shrd all words except the last
for (; std::next(it, is_last_aligned) != new_last_base; ++it) {
*it = _shrd<word_type>(*it, *std::next(it), remaining_bitshifts);
//latent_it = it;
}
// For the last word simpy right shift
*it >>= remaining_bitshifts;
}
// Blend bits of the first element
if (!is_first_aligned) {
*first.base() = _bitblend<word_type>(
first_value,
*first.base(),
first.position(),
digits - first.position()
);
}
// Blend bits of the last element
if (!is_last_aligned) {
*last.base() = _bitblend<word_type>(
*last.base(),
last_value,
last.position(),
digits - last.position()
);
}
//TODO is this more or less inefficient than having a latent iterator?
bit_iterator<ForwardIt> d_last = next(first, d-n);
return d_last;
}
template <class RandomAccessIt>
bit_iterator<RandomAccessIt> shift_right_dispatch(
bit_iterator<RandomAccessIt> first,
bit_iterator<RandomAccessIt> last,
typename bit_iterator<RandomAccessIt>::difference_type n,
std::random_access_iterator_tag
) {
// Types and constants
using word_type = typename bit_iterator<RandomAccessIt>::word_type;
using size_type = typename bit_iterator<RandomAccessIt>::size_type;
constexpr size_type digits = binary_digits<word_type>::value;
// Initialization
size_type word_shifts = n / digits;
size_type remaining_bitshifts = n - digits*(word_shifts);
const bool is_first_aligned = first.position() == 0;
const bool is_last_aligned = last.position() == 0;
// Multiple word case
word_type first_value = *first.base();
word_type last_value = !is_last_aligned ? *last.base() : 0;
word_type mask = is_first_aligned ?
static_cast<word_type>(-1)
:
static_cast<word_type>(
(static_cast<word_type>(1) << (digits - first.position())) - 1
) << first.position();
*first.base() = *first.base() & mask;
// Shift words to the right
RandomAccessIt new_first_base = word_shift_right(
first.base(),
std::next(
last.base(),
!is_last_aligned),
word_shifts
);
bit_iterator<RandomAccessIt> d_first(new_first_base, first.position());
// Shift bit sequence to the msb
if (remaining_bitshifts) {
auto it = is_last_aligned ? last.base() - 1 : last.base();
// Align iterator
const auto N = SIMDPP_FAST_INT64_SIZE;
const auto N_native_words = (N*64)/digits;
for (; std::distance(new_first_base, it)*digits >= N_native_words + 2 && !is_aligned(&(*(it - N_native_words + 1)), 64); --it) {
*it = _shld<word_type>(*it, *(it - 1), remaining_bitshifts);
}
for (; std::distance(new_first_base, it) >= (unsigned int) N_native_words + 2; it -= N_native_words ) {
using vec_type = simdpp::uint64<N>;
auto it_rewind = it - N_native_words + 1;
vec_type v = simdpp::load(&(*it_rewind));
vec_type v_minus1 = simdpp::load_u(&(*(it_rewind-1)));
vec_type ls = simdpp::shift_l(v, remaining_bitshifts);
vec_type rs = simdpp::shift_r(v_minus1, digits - remaining_bitshifts);
vec_type ret = simdpp::bit_or(ls, rs);
simdpp::store(&(*(it_rewind)), ret);
}
for(; it != new_first_base; --it) {
*it = _shld<word_type>(*it, *(it - 1), remaining_bitshifts);
}
*it <<= remaining_bitshifts;
}
// Blend bits of the first element
if (!is_first_aligned) {
*first.base() = _bitblend<word_type>(
first_value,
*first.base(),
first.position(),
digits - first.position()
);
}
// Blend bits of the last element
if (!is_last_aligned) {
*last.base() = _bitblend<word_type>(
*last.base(),
last_value,
last.position(),
digits - last.position()
);
}
std::advance(d_first, remaining_bitshifts);
return d_first;
}
template <class RandomAccessIt>
bit_iterator<RandomAccessIt> shift_left_dispatch(
bit_iterator<RandomAccessIt> first,
bit_iterator<RandomAccessIt> last,
typename bit_iterator<RandomAccessIt>::difference_type n,
std::random_access_iterator_tag
) {
// Types and constants
using word_type = typename bit_iterator<RandomAccessIt>::word_type;
using size_type = typename bit_iterator<RandomAccessIt>::size_type;
constexpr size_type digits = binary_digits<word_type>::value;
// Initialization
size_type word_shifts = n / digits;
size_type remaining_bitshifts = n - digits*(word_shifts);
const bool is_first_aligned = first.position() == 0;
const bool is_last_aligned = last.position() == 0;
auto d = distance(first, last);
// Multiple word case
word_type first_value = *first.base();
word_type last_value = !is_last_aligned ? *last.base() : 0;
// Shift words to the left using std::shift
RandomAccessIt new_last_base = word_shift_left(first.base(),
last.base(),
word_shifts
);
if (!is_last_aligned) {
// Mask out-of-range bits so that we don't incorporate them
*last.base() &= (static_cast<word_type>(1) << last.position()) - 1;
*new_last_base = *last.base();
if (word_shifts > 0) {
*last.base() = 0;
}
}
// Shift bit sequence to the lsb
if (remaining_bitshifts) {
RandomAccessIt it = first.base();
// _shrd all words except the last until we reach alignment
// TODO set alignment based off of instruction set used.
for (; std::next(it, is_last_aligned) != new_last_base && !is_aligned(&*it, 64); ++it) {
*it = _shrd<word_type>(*it, *std::next(it), remaining_bitshifts);
}
// For the last word simply right shift
const auto N = SIMDPP_FAST_INT64_SIZE;
for (; std::distance(it, new_last_base)*digits >= (N+2)*64 ; it += N*(64/digits)) {
using vec_type = simdpp::uint64<N>;
vec_type v = simdpp::load(&(*(it)));
vec_type v_plus1 = simdpp::load_u(&(*(it+1)));
vec_type rs = simdpp::shift_r(v, remaining_bitshifts);
vec_type ls = simdpp::shift_l(v_plus1, digits - remaining_bitshifts);
vec_type ret = simdpp::bit_or(ls, rs);
simdpp::store(&(*it), ret);
}
// _shrd all words except the last
for (; std::next(it, is_last_aligned) != new_last_base; ++it) {
*it = _shrd<word_type>(*it, *std::next(it), remaining_bitshifts);
}
// For the last word simply right shift
*it >>= remaining_bitshifts;
}
// Blend bits of the first element
if (!is_first_aligned) {
*first.base() = _bitblend<word_type>(
first_value,
*first.base(),
first.position(),
digits - first.position()
);
}
// Blend bits of the last element
if (!is_last_aligned) {
*last.base() = _bitblend<word_type>(
*last.base(),
last_value,
last.position(),
digits - last.position()
);
}
//TODO is this more or less inefficient than having a latent iterator?
bit_iterator<RandomAccessIt> d_last = next(first, d-n);
return d_last;
}
template <class ForwardIt>
bit_iterator<ForwardIt> shift_left(
bit_iterator<ForwardIt> first,
bit_iterator<ForwardIt> last,
typename bit_iterator<ForwardIt>::difference_type n
) {
// Assertions
_assert_range_viability(first, last);
// Types and constants
using word_type = typename bit_iterator<ForwardIt>::word_type;
using size_type = typename bit_iterator<ForwardIt>::size_type;
constexpr size_type digits = binary_digits<word_type>::value;
// Initialization
auto d = distance(first, last);
const bool is_last_aligned = last.position() == 0;
// Out of range cases
if (n <= 0) return last;
if (n >= d) return first;
// Single word case
if (std::next(first.base(), is_last_aligned) == last.base()) {
*first.base() = _bitblend<word_type>(
*first.base(),
((
*first.base() & (
static_cast<word_type>(-1) >> (
digits - (is_last_aligned ? digits : last.position())
)
)
)) >> n,
first.position(),
(is_last_aligned ? digits : last.position()) - first.position()
);
return bit_iterator<ForwardIt>(
first.base(),
first.position() + d - n
);
}
else {
return shift_left_dispatch(
first,
last,
n,
typename std::iterator_traits<ForwardIt>::iterator_category()
);
}
}
template <class ForwardIt>
bit_iterator<ForwardIt> shift_right(
bit_iterator<ForwardIt> first,
bit_iterator<ForwardIt> last,
typename bit_iterator<ForwardIt>::difference_type n
) {
// Types and constants
using word_type = typename bit_iterator<ForwardIt>::word_type;
// Initialization
const bool is_last_aligned = last.position() == 0;
constexpr auto digits = binary_digits<word_type>::value;
auto d = distance(first, last);
// Out of range cases
if (n <= 0) return first;
else if (n >= d) return last;
// Single word case
if (std::next(first.base(), is_last_aligned) == last.base()) {
*first.base() = _bitblend<word_type>(
*first.base(),
(
*first.base() & (
static_cast<word_type>(-1) << first.position()
)
) << n,
first.position(),
(is_last_aligned ? digits : last.position()) - first.position()
);
return bit_iterator<ForwardIt>(
first.base(),
first.position() + n
);
}
return shift_right_dispatch(
first,
last,
n,
typename std::iterator_traits<ForwardIt>::iterator_category()
);
}
// -------------------------------------------------------------------------- //
// ========================================================================== //
} // namespace bit
#endif // _SHIFT_HPP_INCLUDED
// ========================================================================== //