-
Notifications
You must be signed in to change notification settings - Fork 6
/
bit.hpp
363 lines (326 loc) · 11 KB
/
bit.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
#pragma once
#include <stdx/concepts.hpp>
#include <stdx/type_traits.hpp>
#include <stdx/utility.hpp>
#include <climits>
#include <cstdint>
#include <limits>
#include <type_traits>
#if __has_include(<bit>)
#include <bit>
#endif
namespace stdx {
inline namespace v1 {
// endian
#if __cpp_lib_endian < 201907L
enum struct endian {
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));
}
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 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 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 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 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;
}};
namespace detail {
template <typename T, std::size_t Bit>
constexpr auto
mask_bits() -> std::enable_if_t<Bit <= std::numeric_limits<T>::digits, T> {
if constexpr (Bit == std::numeric_limits<T>::digits) {
return std::numeric_limits<T>::max();
} else {
return static_cast<T>(T{1} << Bit) - T{1};
}
}
template <typename T> constexpr auto mask_bits(std::size_t Bit) -> T {
if (Bit == std::numeric_limits<T>::digits) {
return std::numeric_limits<T>::max();
}
return static_cast<T>(T{1} << Bit) - T{1};
}
} // namespace detail
template <typename T, std::size_t Msb = std::numeric_limits<T>::digits - 1,
std::size_t Lsb = 0>
[[nodiscard]] constexpr auto bit_mask() noexcept
-> std::enable_if_t<std::is_unsigned_v<T> and Msb >= Lsb, T> {
static_assert(Msb < std::numeric_limits<T>::digits);
return detail::mask_bits<T, Msb + 1>() - detail::mask_bits<T, Lsb>();
}
template <typename T>
[[nodiscard]] constexpr auto bit_mask(std::size_t Msb,
std::size_t Lsb = 0) noexcept
-> std::enable_if_t<std::is_unsigned_v<T>, T> {
return detail::mask_bits<T>(Msb + 1) - detail::mask_bits<T>(Lsb);
}
template <typename T> constexpr auto bit_size() -> std::size_t {
return sizeof(T) * CHAR_BIT;
}
template <std::size_t N, typename S = void> CONSTEVAL auto smallest_uint() {
if constexpr (not std::is_same_v<S, void>) {
static_assert(std::is_unsigned_v<S>,
"smallest_uint override must be an unsigned type");
return S{};
} else 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, typename S = void>
using smallest_uint_t = decltype(smallest_uint<N, S>());
} // namespace v1
} // namespace stdx