NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
bit.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_MEMORY_BIT_HPP__
2#define NEFORCE_CORE_MEMORY_BIT_HPP__
3
11
13NEFORCE_BEGIN_NAMESPACE__
14
15NEFORCE_BEGIN_CONSTANTS__
16
22
30NEFORCE_INLINE17 constexpr byte_t POPCOUNT_TABLE[256] = {
31 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2,
32 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
33 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
34 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
35 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4,
36 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
37 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
38 // BitManipulation
40
41NEFORCE_END_CONSTANTS__
42
48
56constexpr int popcount64(const uint64_t x) noexcept {
57 return constants::POPCOUNT_TABLE[static_cast<byte_t>(x & 0xFFULL)] +
58 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 8) & 0xFFULL)] +
59 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 16) & 0xFFULL)] +
60 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 24) & 0xFFULL)] +
61 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 32) & 0xFFULL)] +
62 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 40) & 0xFFULL)] +
63 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 48) & 0xFFULL)] +
64 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 56) & 0xFFULL)];
65}
66
74NEFORCE_CONSTEXPR14 int clz64(uint64_t x) noexcept {
75 if (x == 0) {
76 return 64;
77 }
78 int n = 0;
79 if (x <= 0x00000000FFFFFFFFULL) {
80 n += 32;
81 x <<= 32;
82 }
83 if (x <= 0x0000FFFFFFFFFFFFULL) {
84 n += 16;
85 x <<= 16;
86 }
87 if (x <= 0x00FFFFFFFFFFFFFFULL) {
88 n += 8;
89 x <<= 8;
90 }
91 if (x <= 0x0FFFFFFFFFFFFFFFULL) {
92 n += 4;
93 x <<= 4;
94 }
95 if (x <= 0x3FFFFFFFFFFFFFFFULL) {
96 n += 2;
97 x <<= 2;
98 }
99 if (x <= 0x7FFFFFFFFFFFFFFFULL) {
100 n += 1;
101 }
102 return n;
103}
104
112constexpr int popcount32(const uint32_t x) noexcept {
113 return constants::POPCOUNT_TABLE[static_cast<byte_t>(x & 0xFFU)] +
114 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 8) & 0xFFU)] +
115 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 16) & 0xFFU)] +
116 constants::POPCOUNT_TABLE[static_cast<byte_t>((x >> 24) & 0xFFU)];
117}
118
126NEFORCE_CONSTEXPR14 int clz32(uint32_t x) noexcept {
127 if (x == 0) {
128 return 32;
129 }
130 int n = 0;
131 if (x <= 0x0000FFFFU) {
132 n += 16;
133 x <<= 16;
134 }
135 if (x <= 0x00FFFFFFU) {
136 n += 8;
137 x <<= 8;
138 }
139 if (x <= 0x0FFFFFFFU) {
140 n += 4;
141 x <<= 4;
142 }
143 if (x <= 0x3FFFFFFFU) {
144 n += 2;
145 x <<= 2;
146 }
147 if (x <= 0x7FFFFFFFU) {
148 n += 1;
149 }
150 return n;
151}
152
160constexpr int popcount(const uintptr_t x) noexcept {
161#ifdef NEFORCE_ARCH_BITS_64
162 return popcount64(x);
163#else
164 return popcount32(x);
165#endif
166}
167
175constexpr int countl_zero(const uintptr_t x) noexcept {
176#ifdef NEFORCE_ARCH_BITS_64
177 return clz64(x);
178#else
179 return clz32(x);
180#endif
181}
182
190constexpr int countl_one(const uintptr_t x) noexcept { return countl_zero(~x); }
191
199constexpr int countr_zero(const uintptr_t x) noexcept { return popcount((x & (~x + 1)) - 1); }
200
208constexpr int countr_one(const uintptr_t x) noexcept { return countr_zero(~x); }
209
210
216constexpr int lowest_set_bit_pos(const intptr_t x) noexcept { return x == 0 ? -1 : countr_zero(x); }
217
223NEFORCE_CONSTEXPR14 int highest_set_bit_pos(const intptr_t x) noexcept {
224 if (x == 0) {
225 return -1;
226 }
227#ifdef NEFORCE_ARCH_BITS_64
228 return 63 - clz64(x);
229#else
230 return 31 - clz32(x);
231#endif
232}
233
234
242NEFORCE_CONSTEXPR14 bool parity32(uint32_t x) noexcept {
243 x ^= x >> 16;
244 x ^= x >> 8;
245 x ^= x >> 4;
246 x ^= x >> 2;
247 x ^= x >> 1;
248 return (x & 1) != 0;
249}
250
256NEFORCE_CONSTEXPR14 bool parity64(uint64_t x) noexcept {
257 x ^= x >> 32;
258 x ^= x >> 16;
259 x ^= x >> 8;
260 x ^= x >> 4;
261 x ^= x >> 2;
262 x ^= x >> 1;
263 return (x & 1) != 0;
264}
265
273constexpr bool parity(const uintptr_t x) noexcept {
274#ifdef NEFORCE_ARCH_BITS_64
275 return parity64(x);
276#else
277 return parity32(x);
278#endif
279}
280
281
287constexpr int bit_width(const uintptr_t x) noexcept {
288#ifdef NEFORCE_ARCH_BITS_64
289 return x == 0 ? 0 : 64 - countl_zero(x);
290#else
291 return x == 0 ? 0 : 32 - countl_zero(x);
292#endif
293}
294
300constexpr uintptr_t bit_floor(const uintptr_t x) noexcept { return x == 0 ? 0 : uintptr_t{1} << (bit_width(x) - 1); }
301
307NEFORCE_CONSTEXPR14 uint64_t bit_ceil(const uintptr_t x) noexcept {
308 if (x <= 1) {
309 return 1;
310 }
311 const uint64_t floor = bit_floor(x);
312 return floor == x ? x : floor << 1;
313}
314
320constexpr bool has_single_bit(const uintptr_t x) noexcept { return x != 0 && (x & (x - 1)) == 0; }
321
328NEFORCE_CONSTEXPR14 uint32_t rotate_l32(const uint32_t x, const int s) noexcept {
329 constexpr int bits = 32;
330 const int shift = ((s % bits) + bits) % bits;
331 if (shift == 0) {
332 return x;
333 }
334 return (x << shift) | (x >> (bits - shift));
335}
336
343NEFORCE_CONSTEXPR14 uint32_t rotate_r32(const uint32_t x, const int s) noexcept { return rotate_l32(x, -s); }
344
351NEFORCE_CONSTEXPR14 uint64_t rotate_l64(const uint64_t x, const int s) noexcept {
352 constexpr int bits = 64;
353 const int shift = ((s % bits) + bits) % bits;
354 if (shift == 0) {
355 return x;
356 }
357 return (x << shift) | (x >> (bits - shift));
358}
359
366NEFORCE_CONSTEXPR14 uint64_t rotate_r64(const uint64_t x, const int s) noexcept { return rotate_l64(x, -s); }
367
376NEFORCE_CONSTEXPR14 uintptr_t rotate_l(const uintptr_t x, const int s) noexcept {
377#ifdef NEFORCE_ARCH_BITS_64
378 return rotate_l64(x, s);
379#else
380 return rotate_l32(x, s);
381#endif
382}
383
392NEFORCE_CONSTEXPR14 uintptr_t rotate_r(const uintptr_t x, const int s) noexcept {
393#ifdef NEFORCE_ARCH_BITS_64
394 return rotate_r64(x, s);
395#else
396 return rotate_r32(x, s);
397#endif
398}
399
400
408constexpr uintptr_t bit_extract(const uintptr_t x, const int pos, const int len) noexcept {
409 return (x >> pos) & ((uintptr_t{1} << len) - 1);
410}
411
420NEFORCE_CONSTEXPR14 uintptr_t bit_insert(const uintptr_t x, const uintptr_t bits, const int pos,
421 const int len) noexcept {
422 const uintptr_t mask = ((uintptr_t{1} << len) - 1) << pos;
423 return (x & ~mask) | ((bits << pos) & mask);
424}
425
426
432NEFORCE_CONSTEXPR14 uint32_t reverse_bits32(uint32_t x) noexcept {
433 x = ((x >> 1) & 0x55555555U) | ((x & 0x55555555U) << 1);
434 x = ((x >> 2) & 0x33333333U) | ((x & 0x33333333U) << 2);
435 x = ((x >> 4) & 0x0F0F0F0FU) | ((x & 0x0F0F0F0FU) << 4);
436 x = ((x >> 8) & 0x00FF00FFU) | ((x & 0x00FF00FFU) << 8);
437 x = ((x >> 16) & 0x0000FFFFU) | ((x & 0x0000FFFFU) << 16);
438 return x;
439}
440
446NEFORCE_CONSTEXPR14 uint64_t reverse_bits64(uint64_t x) noexcept {
447 x = ((x >> 1) & 0x5555555555555555ULL) | ((x & 0x5555555555555555ULL) << 1);
448 x = ((x >> 2) & 0x3333333333333333ULL) | ((x & 0x3333333333333333ULL) << 2);
449 x = ((x >> 4) & 0x0F0F0F0F0F0F0F0FULL) | ((x & 0x0F0F0F0F0F0F0F0FULL) << 4);
450 x = ((x >> 8) & 0x00FF00FF00FF00FFULL) | ((x & 0x00FF00FF00FF00FFULL) << 8);
451 x = ((x >> 16) & 0x0000FFFF0000FFFFULL) | ((x & 0x0000FFFF0000FFFFULL) << 16);
452 x = (x >> 32) | (x << 32);
453 return x;
454}
455
463constexpr uintptr_t reverse_bits(const uintptr_t x) noexcept {
464#ifdef NEFORCE_ARCH_BITS_64
465 return reverse_bits64(x);
466#else
467 return reverse_bits32(x);
468#endif
469}
470
471
478constexpr uintptr_t mask_from_to(const int from, const int to) noexcept {
479 return ((uintptr_t{1} << (to - from + 1)) - 1) << from;
480}
481 // BitManipulation
483
484NEFORCE_END_NAMESPACE__
485#endif // NEFORCE_CORE_MEMORY_BIT_HPP__
constexpr uint64_t rotate_r64(const uint64_t x, const int s) noexcept
64位整数循环右移
constexpr uint64_t reverse_bits64(uint64_t x) noexcept
反转64位整数的位顺序
constexpr uint64_t bit_ceil(const uintptr_t x) noexcept
获取不小于x的最小2的幂
constexpr bool parity(const uintptr_t x) noexcept
计算整数的奇偶性
constexpr int countl_one(const uintptr_t x) noexcept
计算整数前导1的个数
constexpr int popcount32(const uint32_t x) noexcept
计算32位整数中1的个数
constexpr uint32_t reverse_bits32(uint32_t x) noexcept
反转32位整数的位顺序
constexpr uintptr_t mask_from_to(const int from, const int to) noexcept
生成从from到to的位掩码
constexpr uint64_t rotate_l64(const uint64_t x, const int s) noexcept
64位整数循环左移
constexpr uintptr_t rotate_r(const uintptr_t x, const int s) noexcept
整数循环右移
constexpr int lowest_set_bit_pos(const intptr_t x) noexcept
获取最低设置位的位置,从0开始
constexpr uintptr_t reverse_bits(const uintptr_t x) noexcept
反转整数的位顺序
constexpr uint32_t rotate_l32(const uint32_t x, const int s) noexcept
32位整数循环左移
constexpr int popcount(const uintptr_t x) noexcept
计算整数中1的个数
constexpr uintptr_t rotate_l(const uintptr_t x, const int s) noexcept
整数循环左移
constexpr int popcount64(const uint64_t x) noexcept
计算64位整数中1的个数
定义 bit.hpp:56
constexpr uint32_t rotate_r32(const uint32_t x, const int s) noexcept
32位整数循环右移
constexpr int countr_one(const uintptr_t x) noexcept
计算整数尾随1的个数
constexpr byte_t POPCOUNT_TABLE[256]
popcount查找表
定义 bit.hpp:30
constexpr int clz64(uint64_t x) noexcept
计算64位整数前导零的个数
定义 bit.hpp:74
constexpr uintptr_t bit_extract(const uintptr_t x, const int pos, const int len) noexcept
从整数中提取指定位段
constexpr bool has_single_bit(const uintptr_t x) noexcept
检查整数是否为2的幂
constexpr int highest_set_bit_pos(const intptr_t x) noexcept
获取最高设置位的位置
constexpr int countl_zero(const uintptr_t x) noexcept
计算整数前导零的个数
constexpr uintptr_t bit_floor(const uintptr_t x) noexcept
获取不大于x的最大2的幂
constexpr bool parity64(uint64_t x) noexcept
计算64位整数的奇偶性
constexpr uintptr_t bit_insert(const uintptr_t x, const uintptr_t bits, const int pos, const int len) noexcept
向整数中插入指定位段
constexpr int bit_width(const uintptr_t x) noexcept
计算表示整数所需的最小位宽
constexpr bool parity32(uint32_t x) noexcept
计算32位整数的奇偶性
constexpr int countr_zero(const uintptr_t x) noexcept
计算整数尾随零的个数
constexpr int clz32(uint32_t x) noexcept
计算32位整数前导零的个数
unsigned char byte_t
字节类型,定义为无符号字符
unsigned int uint32_t
32位无符号整数类型
unsigned long long uint64_t
64位无符号整数类型
constexpr decimal_t floor(const decimal_t x) noexcept
向下取整
int64_t intptr_t
可容纳指针的有符号整数类型
uint64_t uintptr_t
可容纳指针的无符号整数类型
基本类型别名