1#ifndef NEFORCE_CORE_MEMORY_BIT_HPP__
2#define NEFORCE_CORE_MEMORY_BIT_HPP__
12NEFORCE_BEGIN_NAMESPACE__
26 x = x - ((x >> 1) & 0x5555555555555555ULL);
27 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
28 x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
32 return static_cast<int>(x & 0x7FULL);
78 v = v - ((v >> 1) & 0x55555555U);
79 v = (v & 0x33333333U) + ((v >> 2) & 0x33333333U);
80 v = (v + (v >> 4)) & 0x0F0F0F0FU;
83 return static_cast<int>(v & 0x3FU);
100 if ((x >> 24) == 0) {
104 if ((x >> 28) == 0) {
108 if ((x >> 30) == 0) {
112 if ((x >> 31) == 0) {
124#ifdef NEFORCE_ARCH_BITS_64
137#ifdef NEFORCE_ARCH_BITS_64
158#ifdef NEFORCE_ARCH_BITS_64
166#ifdef NEFORCE_ARCH_BITS_64
167 if ((v & 0xFFFFFFFFULL) == 0) {
172 if ((v & 0xFFFFU) == 0) {
176 if ((v & 0xFFU) == 0) {
180 if ((v & 0xFU) == 0) {
184 if ((v & 0x3U) == 0) {
188 if ((v & 0x1U) == 0) {
218#ifdef NEFORCE_ARCH_BITS_64
219 return 63 -
clz64(x);
221 return 31 -
clz32(x);
261#ifdef NEFORCE_ARCH_BITS_64
275#ifdef NEFORCE_ARCH_BITS_64
316 const unsigned shift =
static_cast<unsigned>(s) & 31U;
317 return (x << shift) | (x >> ((0U - shift) & 31U));
335 const unsigned shift =
static_cast<unsigned>(s) & 63U;
336 return (x << shift) | (x >> ((0U - shift) & 63U));
354#ifdef NEFORCE_ARCH_BITS_64
368#ifdef NEFORCE_ARCH_BITS_64
384 return (x >> pos) & ((
uintptr_t{1} << len) - 1);
396 const int len)
noexcept {
398 return (x & ~mask) | ((bits << pos) & mask);
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);
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);
437#ifdef NEFORCE_ARCH_BITS_64
452 return ((
uintptr_t{1} << (to - from + 1)) - 1) << from;
457NEFORCE_END_NAMESPACE__
constexpr int bit_width(const uintptr_t x) noexcept
计算表示整数所需的最小位宽
constexpr int popcount64(uint64_t x) noexcept
计算64位整数中1的个数
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的个数
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位整数前导零的个数
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位整数前导零的个数
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
向下取整