-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbit.hpp
More file actions
476 lines (424 loc) · 14.7 KB
/
bit.hpp
File metadata and controls
476 lines (424 loc) · 14.7 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
#pragma once
#include <stdx/compiler.hpp>
#include <stdx/concepts.hpp>
#include <stdx/type_traits.hpp>
#include <stdx/utility.hpp>
#include <array>
#include <climits>
#include <cstdint>
#include <iterator>
#include <limits>
#include <type_traits>
#if __has_include(<bit>)
#include <bit>
#endif
// NOLINTBEGIN(modernize-use-constraints)
namespace stdx {
inline namespace v1 {
// endian
#if __cpp_lib_endian < 201907L
// NOLINTNEXTLINE(performance-enum-size)
enum struct endian : underlying_type_t<decltype(__BYTE_ORDER__)> {
little = __ORDER_LITTLE_ENDIAN__,
big = __ORDER_BIG_ENDIAN__,
native = __BYTE_ORDER__
};
#else
using endian = std::endian;
#endif
// bit_cast
#if __cpp_lib_bit_cast < 201806L
template <typename To, typename From>
[[nodiscard]] constexpr auto bit_cast(From &&from) noexcept -> To {
return __builtin_bit_cast(To, from);
}
#else
using std::bit_cast;
#endif
// byteswap
#if __cpp_lib_byteswap < 202110L
template <typename T>
[[nodiscard]] constexpr auto byteswap(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if constexpr (sizeof(T) == sizeof(std::uint16_t)) {
return __builtin_bswap16(x);
} else if constexpr (sizeof(T) == sizeof(std::uint32_t)) {
return __builtin_bswap32(x);
} else if constexpr (sizeof(T) == sizeof(std::uint64_t)) {
return __builtin_bswap64(x);
} else {
return x;
}
}
#else
using std::byteswap;
#endif
// bit ops
#if __cpp_lib_bitops < 201907L
template <typename T>
[[nodiscard]] constexpr auto countl_zero(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, int> {
if (x == 0) {
return std::numeric_limits<T>::digits;
}
if constexpr (sizeof(T) == sizeof(unsigned int)) {
return __builtin_clz(x);
} else if constexpr (sizeof(T) ==
sizeof(unsigned long)) { // NOLINT(google-runtime-int)
return __builtin_clzl(x);
} else if constexpr (
sizeof(T) == sizeof(unsigned long long)) { // NOLINT(google-runtime-int)
return __builtin_clzll(x);
} else {
return __builtin_clzll(x) + std::numeric_limits<T>::digits -
std::numeric_limits<
unsigned long long>::digits; // NOLINT(google-runtime-int)
}
}
template <typename T>
[[nodiscard]] constexpr auto countr_zero(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, int> {
if (x == 0) {
return std::numeric_limits<T>::digits;
}
if constexpr (sizeof(T) == sizeof(unsigned int)) {
return __builtin_ctz(x);
} else if constexpr (sizeof(T) ==
sizeof(unsigned long)) { // NOLINT(google-runtime-int)
return __builtin_ctzl(x);
} else {
return __builtin_ctzll(x);
}
}
template <typename T>
[[nodiscard]] constexpr auto countl_one(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, int> {
return countl_zero(T(~x));
}
template <typename T>
[[nodiscard]] constexpr auto countr_one(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, int> {
return countr_zero(T(~x));
}
template <typename T>
[[nodiscard]] constexpr auto popcount(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, int> {
if constexpr (sizeof(T) <= sizeof(unsigned int)) {
return __builtin_popcount(x);
} else if constexpr (sizeof(T) <=
sizeof(unsigned long)) { // NOLINT(google-runtime-int)
return __builtin_popcountl(x);
} else {
return __builtin_popcountll(x);
}
}
namespace detail {
template <typename T>
[[nodiscard]] constexpr auto rotl(T x, T s) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
#ifdef __clang__
if constexpr (sizeof(T) == sizeof(std::uint8_t)) {
return __builtin_rotateleft8(x, s);
} else if constexpr (sizeof(T) == sizeof(std::uint16_t)) {
return __builtin_rotateleft16(x, s);
} else if constexpr (sizeof(T) == sizeof(std::uint32_t)) {
return __builtin_rotateleft32(x, s);
} else {
return __builtin_rotateleft64(x, s);
}
#else
return static_cast<T>((x << s) |
(x >> (std::numeric_limits<T>::digits - s)));
#endif
}
template <typename T>
[[nodiscard]] constexpr auto rotr(T x, T s) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
#ifdef __clang__
if constexpr (sizeof(T) == sizeof(std::uint8_t)) {
return __builtin_rotateright8(x, s);
} else if constexpr (sizeof(T) == sizeof(std::uint16_t)) {
return __builtin_rotateright16(x, s);
} else if constexpr (sizeof(T) == sizeof(std::uint32_t)) {
return __builtin_rotateright32(x, s);
} else {
return __builtin_rotateright64(x, s);
}
#else
return static_cast<T>((x >> s) |
(x << (std::numeric_limits<T>::digits - s)));
#endif
}
} // namespace detail
template <typename T>
[[nodiscard]] constexpr auto rotl(T x, int s) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if (s == 0) {
return x;
}
if (s < 0) {
return detail::rotr(x, T(-s));
}
return detail::rotl(x, T(s));
}
template <typename T>
[[nodiscard]] constexpr auto rotr(T x, int s) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if (s == 0) {
return x;
}
if (s < 0) {
return detail::rotl(x, T(-s));
}
return detail::rotr(x, T(s));
}
#else
using std::countl_one;
using std::countl_zero;
using std::countr_one;
using std::countr_zero;
using std::popcount;
using std::rotl;
using std::rotr;
#endif
// pow2
#if __cpp_lib_int_pow2 < 202002L
template <typename T>
[[nodiscard]] constexpr auto has_single_bit(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, bool> {
return x and not(x & (x - 1));
}
template <typename T>
[[nodiscard]] constexpr auto bit_width(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, int> {
return std::numeric_limits<T>::digits - countl_zero(x);
}
template <typename T>
[[nodiscard]] constexpr auto bit_ceil(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if (x <= 1U) {
return 1U;
}
return T(1U << bit_width(x - 1U));
}
template <typename T>
[[nodiscard]] constexpr auto bit_floor(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if (x == 0) {
return x;
}
return T(1U << (bit_width(x) - 1));
}
#else
using std::bit_ceil;
using std::bit_floor;
using std::bit_width;
using std::has_single_bit;
#endif
template <typename T>
[[nodiscard]] constexpr auto to_le(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if constexpr (stdx::endian::native == stdx::endian::big) {
return byteswap(x);
} else {
return x;
}
}
template <typename T>
[[nodiscard]] constexpr auto to_be(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if constexpr (stdx::endian::native == stdx::endian::little) {
return byteswap(x);
} else {
return x;
}
}
template <typename T>
[[nodiscard]] constexpr auto from_le(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if constexpr (stdx::endian::native == stdx::endian::big) {
return byteswap(x);
} else {
return x;
}
}
template <typename T>
[[nodiscard]] constexpr auto from_be(T x) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
if constexpr (stdx::endian::native == stdx::endian::little) {
return byteswap(x);
} else {
return x;
}
}
template <typename To>
constexpr auto bit_pack = [](auto... args) {
static_assert(stdx::always_false_v<To, decltype(args)...>,
"bit_pack is undefined for those types");
};
template <>
constexpr inline auto bit_pack<std::uint16_t> =
[](std::uint8_t hi, std::uint8_t lo) -> std::uint16_t {
return static_cast<std::uint16_t>((static_cast<std::uint32_t>(hi) << 8u) |
lo);
};
template <>
constexpr inline auto bit_pack<std::uint32_t> =
stdx::overload{[](std::uint16_t hi, std::uint16_t lo) -> std::uint32_t {
return (static_cast<std::uint32_t>(hi) << 16u) | lo;
},
[](std::uint8_t b0, std::uint8_t b1, std::uint8_t b2,
std::uint8_t b3) -> std::uint32_t {
return (static_cast<std::uint32_t>(b0) << 24u) |
(static_cast<std::uint32_t>(b1) << 16u) |
(static_cast<std::uint32_t>(b2) << 8u) | b3;
}};
template <>
constexpr inline auto bit_pack<std::uint64_t> =
stdx::overload{[](std::uint32_t hi, std::uint32_t lo) -> std::uint64_t {
return (static_cast<std::uint64_t>(hi) << 32u) | lo;
},
[](std::uint16_t w0, std::uint16_t w1, std::uint16_t w2,
std::uint16_t w3) -> std::uint64_t {
return (static_cast<std::uint64_t>(w0) << 48u) |
(static_cast<std::uint64_t>(w1) << 32u) |
(static_cast<std::uint64_t>(w2) << 16u) | w3;
},
[](std::uint8_t b0, std::uint8_t b1, std::uint8_t b2,
std::uint8_t b3, std::uint8_t b4, std::uint8_t b5,
std::uint8_t b6, std::uint8_t b7) -> std::uint64_t {
return (static_cast<std::uint64_t>(b0) << 56u) |
(static_cast<std::uint64_t>(b1) << 48u) |
(static_cast<std::uint64_t>(b2) << 40u) |
(static_cast<std::uint64_t>(b3) << 32u) |
(static_cast<std::uint64_t>(b4) << 24u) |
(static_cast<std::uint64_t>(b5) << 16u) |
(static_cast<std::uint64_t>(b6) << 8u) | b7;
}};
template <typename To, typename From> constexpr auto bit_unpack(From arg) {
static_assert(unsigned_integral<To> and unsigned_integral<From>,
"bit_unpack is undefined for those types");
constexpr auto sz = sized<From>{1}.template in<To>();
return [&]() -> std::array<To, sz> {
auto r = bit_cast<std::array<To, sz>>(to_be(arg));
for (auto &elem : r) {
elem = from_be(elem);
}
return r;
}();
}
namespace detail {
template <typename T> struct num_digits_t {
constexpr static std::size_t value = std::numeric_limits<T>::digits;
};
template <typename T> constexpr auto num_digits_v = num_digits_t<T>::value;
template <typename T, std::size_t N> struct num_digits_t<std::array<T, N>> {
constexpr static std::size_t value = (N * num_digits_v<T>);
};
template <typename T> struct mask_bits_t {
static_assert(std::is_unsigned_v<T>,
"bit_mask must be used with unsigned types");
constexpr auto operator()(std::size_t bit) const -> T {
if (bit == num_digits_v<T>) {
return std::numeric_limits<T>::max();
}
return static_cast<T>(T{1} << bit) - T{1};
}
};
template <typename T, std::size_t N> struct mask_bits_t<std::array<T, N>> {
constexpr auto operator()(std::size_t bit) const -> std::array<T, N> {
constexpr auto t_bits = num_digits_v<T>;
auto const quot = bit / t_bits;
auto const rem = bit % t_bits;
std::array<T, N> r{};
T *p = std::data(r);
for (auto i = std::size_t{}; i < quot; ++i) {
*p++ = mask_bits_t<T>{}(t_bits);
}
if (rem != 0) {
*p = mask_bits_t<T>{}(rem);
}
return r;
}
};
template <typename T> struct bitmask_subtract {
static_assert(std::is_unsigned_v<T>,
"bit_mask must be used with unsigned types");
constexpr auto operator()(T x, T y) const -> T { return x ^ y; }
};
template <typename T, std::size_t N> struct bitmask_subtract<std::array<T, N>> {
constexpr auto operator()(std::array<T, N> const &x,
std::array<T, N> const &y) const
-> std::array<T, N> {
std::array<T, N> r{};
for (auto i = std::size_t{}; i < N; ++i) {
r[i] = bitmask_subtract<T>{}(x[i], y[i]);
}
return r;
}
};
} // namespace detail
template <typename T, std::size_t Msb = detail::num_digits_v<T> - 1,
std::size_t Lsb = 0>
[[nodiscard]] CONSTEVAL auto bit_mask() noexcept -> T {
static_assert(Msb < detail::num_digits_v<T>,
"bit_mask requested exceeds the range of the type");
static_assert(Msb >= Lsb, "bit_mask range is invalid");
return detail::bitmask_subtract<T>{}(detail::mask_bits_t<T>{}(Msb + 1),
detail::mask_bits_t<T>{}(Lsb));
}
template <typename T>
[[nodiscard]] constexpr auto bit_mask(std::size_t Msb,
std::size_t Lsb = 0) noexcept -> T {
return detail::bitmask_subtract<T>{}(detail::mask_bits_t<T>{}(Msb + 1),
detail::mask_bits_t<T>{}(Lsb));
}
template <typename T> constexpr auto bit_size() -> std::size_t {
return sizeof(T) * CHAR_BIT;
}
template <std::size_t N> CONSTEVAL auto smallest_uint() {
if constexpr (N <= std::numeric_limits<std::uint8_t>::digits) {
return std::uint8_t{};
} else if constexpr (N <= std::numeric_limits<std::uint16_t>::digits) {
return std::uint16_t{};
} else if constexpr (N <= std::numeric_limits<std::uint32_t>::digits) {
return std::uint32_t{};
} else {
return std::uint64_t{};
}
}
template <std::size_t N> using smallest_uint_t = decltype(smallest_uint<N>());
namespace bit_detail {
template <std::size_t... Offsets>
constexpr auto shifts = []()->std::array<std::size_t, sizeof...(Offsets) + 1> {
constexpr auto offsets = std::array{std::size_t{}, Offsets...};
auto s = std::array<std::size_t, sizeof...(Offsets) + 1>{};
for (auto i = std::size_t{}; i < sizeof...(Offsets); ++i) {
s[i + 1] = offsets[i + 1] - offsets[i];
}
return s;
}
();
template <std::size_t Shift, std::size_t Msb, typename T>
constexpr auto shift_extract(T &t) -> T {
t = static_cast<T>(t >> Shift);
constexpr auto mask = bit_mask<T, Msb>();
return static_cast<T>(t & mask);
}
template <std::size_t... Offsets, typename T, std::size_t... Is>
constexpr auto bit_destructure_impl(T t, std::index_sequence<Is...>) {
return std::array{
shift_extract<shifts<Offsets...>[Is], shifts<Offsets...>[Is + 1] - 1>(
t)...};
}
} // namespace bit_detail
template <std::size_t... Offsets, typename T>
constexpr auto bit_destructure(T t)
-> std::enable_if_t<unsigned_integral<T>,
std::array<T, sizeof...(Offsets) + 1>> {
return bit_detail::bit_destructure_impl<Offsets..., bit_size<T>()>(
t, std::make_index_sequence<sizeof...(Offsets) + 1>{});
}
} // namespace v1
} // namespace stdx
// NOLINTEND(modernize-use-constraints)