NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
bit.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_MEMORY_BIT_HPP__
2#define NEFORCE_CORE_MEMORY_BIT_HPP__
3
10
12NEFORCE_BEGIN_NAMESPACE__
13
19
25constexpr int popcount64(uint64_t x) noexcept {
26 x = x - ((x >> 1) & 0x5555555555555555ULL);
27 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
28 x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
29 x = x + (x >> 8);
30 x = x + (x >> 16);
31 x = x + (x >> 32);
32 return static_cast<int>(x & 0x7FULL);
33}
34
40NEFORCE_CONSTEXPR14 int clz64(uint64_t x) noexcept {
41 if (x == 0) {
42 return 64;
43 }
44 int n = 0;
45 if ((x >> 32) == 0) {
46 n += 32;
47 x <<= 32;
48 }
49 if ((x >> 48) == 0) {
50 n += 16;
51 x <<= 16;
52 }
53 if ((x >> 56) == 0) {
54 n += 8;
55 x <<= 8;
56 }
57 if ((x >> 60) == 0) {
58 n += 4;
59 x <<= 4;
60 }
61 if ((x >> 62) == 0) {
62 n += 2;
63 x <<= 2;
64 }
65 if ((x >> 63) == 0) {
66 n += 1;
67 }
68 return n;
69}
70
76constexpr int popcount32(const uint32_t x) noexcept {
77 auto v = x;
78 v = v - ((v >> 1) & 0x55555555U);
79 v = (v & 0x33333333U) + ((v >> 2) & 0x33333333U);
80 v = (v + (v >> 4)) & 0x0F0F0F0FU;
81 v = v + (v >> 8);
82 v = v + (v >> 16);
83 return static_cast<int>(v & 0x3FU);
84}
85
91NEFORCE_CONSTEXPR14 int clz32(uint32_t x) noexcept {
92 if (x == 0) {
93 return 32;
94 }
95 int n = 0;
96 if ((x >> 16) == 0) {
97 n += 16;
98 x <<= 16;
99 }
100 if ((x >> 24) == 0) {
101 n += 8;
102 x <<= 8;
103 }
104 if ((x >> 28) == 0) {
105 n += 4;
106 x <<= 4;
107 }
108 if ((x >> 30) == 0) {
109 n += 2;
110 x <<= 2;
111 }
112 if ((x >> 31) == 0) {
113 n += 1;
114 }
115 return n;
116}
117
123constexpr int popcount(const uintptr_t x) noexcept {
124#ifdef NEFORCE_ARCH_BITS_64
125 return popcount64(x);
126#else
127 return popcount32(x);
128#endif
129}
130
136constexpr int countl_zero(const uintptr_t x) noexcept {
137#ifdef NEFORCE_ARCH_BITS_64
138 return clz64(x);
139#else
140 return clz32(x);
141#endif
142}
143
149constexpr int countl_one(const uintptr_t x) noexcept { return countl_zero(~x); }
150
156constexpr int countr_zero(const uintptr_t x) noexcept {
157 if (x == 0) {
158#ifdef NEFORCE_ARCH_BITS_64
159 return 64;
160#else
161 return 32;
162#endif
163 }
164 int n = 0;
165 uintptr_t v = x;
166#ifdef NEFORCE_ARCH_BITS_64
167 if ((v & 0xFFFFFFFFULL) == 0) {
168 n += 32;
169 v >>= 32;
170 }
171#endif
172 if ((v & 0xFFFFU) == 0) {
173 n += 16;
174 v >>= 16;
175 }
176 if ((v & 0xFFU) == 0) {
177 n += 8;
178 v >>= 8;
179 }
180 if ((v & 0xFU) == 0) {
181 n += 4;
182 v >>= 4;
183 }
184 if ((v & 0x3U) == 0) {
185 n += 2;
186 v >>= 2;
187 }
188 if ((v & 0x1U) == 0) {
189 n += 1;
190 }
191 return n;
192}
193
199constexpr int countr_one(const uintptr_t x) noexcept { return countr_zero(~x); }
200
201
207constexpr int lowest_set_bit_pos(const intptr_t x) noexcept { return x == 0 ? -1 : countr_zero(x); }
208
214NEFORCE_CONSTEXPR14 int highest_set_bit_pos(const intptr_t x) noexcept {
215 if (x == 0) {
216 return -1;
217 }
218#ifdef NEFORCE_ARCH_BITS_64
219 return 63 - clz64(x);
220#else
221 return 31 - clz32(x);
222#endif
223}
224
225
231NEFORCE_CONSTEXPR14 bool parity32(uint32_t x) noexcept {
232 x ^= x >> 16;
233 x ^= x >> 8;
234 x ^= x >> 4;
235 x ^= x >> 2;
236 x ^= x >> 1;
237 return (x & 1) != 0;
238}
239
245NEFORCE_CONSTEXPR14 bool parity64(uint64_t x) noexcept {
246 x ^= x >> 32;
247 x ^= x >> 16;
248 x ^= x >> 8;
249 x ^= x >> 4;
250 x ^= x >> 2;
251 x ^= x >> 1;
252 return (x & 1) != 0;
253}
254
260constexpr bool parity(const uintptr_t x) noexcept {
261#ifdef NEFORCE_ARCH_BITS_64
262 return parity64(x);
263#else
264 return parity32(x);
265#endif
266}
267
268
274constexpr int bit_width(const uintptr_t x) noexcept {
275#ifdef NEFORCE_ARCH_BITS_64
276 return x == 0 ? 0 : 64 - countl_zero(x);
277#else
278 return x == 0 ? 0 : 32 - countl_zero(x);
279#endif
280}
281
287constexpr uintptr_t bit_floor(const uintptr_t x) noexcept { return x == 0 ? 0 : uintptr_t{1} << (bit_width(x) - 1); }
288
294NEFORCE_CONSTEXPR14 uint64_t bit_ceil(const uintptr_t x) noexcept {
295 if (x <= 1) {
296 return 1;
297 }
298 const uint64_t floor = bit_floor(x);
299 return floor == x ? x : floor << 1;
300}
301
307constexpr bool has_single_bit(const uintptr_t x) noexcept { return x != 0 && (x & (x - 1)) == 0; }
308
315NEFORCE_CONSTEXPR14 uint32_t rotate_l32(const uint32_t x, const int s) noexcept {
316 const unsigned shift = static_cast<unsigned>(s) & 31U;
317 return (x << shift) | (x >> ((0U - shift) & 31U));
318}
319
326NEFORCE_CONSTEXPR14 uint32_t rotate_r32(const uint32_t x, const int s) noexcept { return rotate_l32(x, -s); }
327
334NEFORCE_CONSTEXPR14 uint64_t rotate_l64(const uint64_t x, const int s) noexcept {
335 const unsigned shift = static_cast<unsigned>(s) & 63U;
336 return (x << shift) | (x >> ((0U - shift) & 63U));
337}
338
345NEFORCE_CONSTEXPR14 uint64_t rotate_r64(const uint64_t x, const int s) noexcept { return rotate_l64(x, -s); }
346
353NEFORCE_CONSTEXPR14 uintptr_t rotate_l(const uintptr_t x, const int s) noexcept {
354#ifdef NEFORCE_ARCH_BITS_64
355 return rotate_l64(x, s);
356#else
357 return rotate_l32(x, s);
358#endif
359}
360
367NEFORCE_CONSTEXPR14 uintptr_t rotate_r(const uintptr_t x, const int s) noexcept {
368#ifdef NEFORCE_ARCH_BITS_64
369 return rotate_r64(x, s);
370#else
371 return rotate_r32(x, s);
372#endif
373}
374
375
383constexpr uintptr_t bit_extract(const uintptr_t x, const int pos, const int len) noexcept {
384 return (x >> pos) & ((uintptr_t{1} << len) - 1);
385}
386
395NEFORCE_CONSTEXPR14 uintptr_t bit_insert(const uintptr_t x, const uintptr_t bits, const int pos,
396 const int len) noexcept {
397 const uintptr_t mask = ((uintptr_t{1} << len) - 1) << pos;
398 return (x & ~mask) | ((bits << pos) & mask);
399}
400
401
407NEFORCE_CONSTEXPR14 uint32_t reverse_bits32(uint32_t x) noexcept {
408 x = ((x >> 1) & 0x55555555U) | ((x & 0x55555555U) << 1);
409 x = ((x >> 2) & 0x33333333U) | ((x & 0x33333333U) << 2);
410 x = ((x >> 4) & 0x0F0F0F0FU) | ((x & 0x0F0F0F0FU) << 4);
411 x = ((x >> 8) & 0x00FF00FFU) | ((x & 0x00FF00FFU) << 8);
412 x = ((x >> 16) & 0x0000FFFFU) | ((x & 0x0000FFFFU) << 16);
413 return x;
414}
415
421NEFORCE_CONSTEXPR14 uint64_t reverse_bits64(uint64_t x) noexcept {
422 x = ((x >> 1) & 0x5555555555555555ULL) | ((x & 0x5555555555555555ULL) << 1);
423 x = ((x >> 2) & 0x3333333333333333ULL) | ((x & 0x3333333333333333ULL) << 2);
424 x = ((x >> 4) & 0x0F0F0F0F0F0F0F0FULL) | ((x & 0x0F0F0F0F0F0F0F0FULL) << 4);
425 x = ((x >> 8) & 0x00FF00FF00FF00FFULL) | ((x & 0x00FF00FF00FF00FFULL) << 8);
426 x = ((x >> 16) & 0x0000FFFF0000FFFFULL) | ((x & 0x0000FFFF0000FFFFULL) << 16);
427 x = (x >> 32) | (x << 32);
428 return x;
429}
430
436constexpr uintptr_t reverse_bits(const uintptr_t x) noexcept {
437#ifdef NEFORCE_ARCH_BITS_64
438 return reverse_bits64(x);
439#else
440 return reverse_bits32(x);
441#endif
442}
443
444
451constexpr uintptr_t mask_from_to(const int from, const int to) noexcept {
452 return ((uintptr_t{1} << (to - from + 1)) - 1) << from;
453}
454 // BitManipulation
456
457NEFORCE_END_NAMESPACE__
458#endif // NEFORCE_CORE_MEMORY_BIT_HPP__
constexpr int bit_width(const uintptr_t x) noexcept
计算表示整数所需的最小位宽
constexpr int popcount64(uint64_t x) noexcept
计算64位整数中1的个数
定义 bit.hpp:25
constexpr uint64_t bit_ceil(const uintptr_t x) noexcept
获取不小于x的最小2的幂
constexpr bool has_single_bit(const uintptr_t x) noexcept
检查整数是否为2的幂
constexpr uintptr_t bit_insert(const uintptr_t x, const uintptr_t bits, const int pos, const int len) noexcept
向整数中插入指定位段
constexpr int countl_zero(const uintptr_t x) noexcept
计算整数前导零的个数
constexpr int popcount32(const uint32_t x) noexcept
计算32位整数中1的个数
定义 bit.hpp:76
constexpr uintptr_t bit_extract(const uintptr_t x, const int pos, const int len) noexcept
从整数中提取指定位段
constexpr uintptr_t mask_from_to(const int from, const int to) noexcept
生成从from到to的位掩码
constexpr int countl_one(const uintptr_t x) noexcept
计算整数前导1的个数
constexpr uint32_t reverse_bits32(uint32_t x) noexcept
反转32位整数的位顺序
constexpr int countr_one(const uintptr_t x) noexcept
计算整数尾随1的个数
constexpr uint32_t rotate_l32(const uint32_t x, const int s) noexcept
32位整数循环左移
constexpr uintptr_t bit_floor(const uintptr_t x) noexcept
获取不大于x的最大2的幂
constexpr uintptr_t rotate_l(const uintptr_t x, const int s) noexcept
整数循环左移
constexpr bool parity64(uint64_t x) noexcept
计算64位整数的奇偶性
constexpr uint32_t rotate_r32(const uint32_t x, const int s) noexcept
32位整数循环右移
constexpr uintptr_t rotate_r(const uintptr_t x, const int s) noexcept
整数循环右移
constexpr bool parity32(uint32_t x) noexcept
计算32位整数的奇偶性
constexpr int clz32(uint32_t x) noexcept
计算32位整数前导零的个数
定义 bit.hpp:91
constexpr uint64_t rotate_l64(const uint64_t x, const int s) noexcept
64位整数循环左移
constexpr uintptr_t reverse_bits(const uintptr_t x) noexcept
反转整数的位顺序
constexpr uint64_t rotate_r64(const uint64_t x, const int s) noexcept
64位整数循环右移
constexpr int lowest_set_bit_pos(const intptr_t x) noexcept
获取最低设置位的位置,从0开始
constexpr uint64_t reverse_bits64(uint64_t x) noexcept
反转64位整数的位顺序
constexpr int highest_set_bit_pos(const intptr_t x) noexcept
获取最高设置位的位置
constexpr int clz64(uint64_t x) noexcept
计算64位整数前导零的个数
定义 bit.hpp:40
constexpr int popcount(const uintptr_t x) noexcept
计算整数中1的个数
constexpr bool parity(const uintptr_t x) noexcept
计算整数的奇偶性
constexpr int countr_zero(const uintptr_t x) noexcept
计算整数尾随零的个数
unsigned int uint32_t
32位无符号整数类型
unsigned long uint64_t
64位无符号整数类型
constexpr decimal_t floor(const decimal_t x) noexcept
向下取整
int64_t intptr_t
可容纳指针的有符号整数类型
uint64_t uintptr_t
可容纳指针的无符号整数类型
基本类型别名