diff options
Diffstat (limited to 'common/alnumeric.h')
-rw-r--r-- | common/alnumeric.h | 86 |
1 files changed, 0 insertions, 86 deletions
diff --git a/common/alnumeric.h b/common/alnumeric.h index b9384a7f..c16f3e62 100644 --- a/common/alnumeric.h +++ b/common/alnumeric.h @@ -103,92 +103,6 @@ inline size_t RoundUp(size_t value, size_t r) noexcept } -/* Define CountTrailingZeros (count trailing zero bits, starting from the lsb) - * and PopCount (population count/count 1 bits) methods, for 32- and 64-bit - * integers. The CountTrailingZeros results are *UNDEFINED* if the value is 0. - */ -#ifdef __GNUC__ - -/* Define variations for unsigned (long (long)) int, since we don't know what - * uint32/64_t are typedef'd to. - */ -inline int PopCount(unsigned long long val) { return __builtin_popcountll(val); } -inline int PopCount(unsigned long val) { return __builtin_popcountl(val); } -inline int PopCount(unsigned int val) { return __builtin_popcount(val); } - -inline int CountTrailingZeros(unsigned long long val) { return __builtin_ctzll(val); } -inline int CountTrailingZeros(unsigned long val) { return __builtin_ctzl(val); } -inline int CountTrailingZeros(unsigned int val) { return __builtin_ctz(val); } - -#else - -/* There be black magics here. The popcnt method is derived from - * https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel - * while the ctz-utilizing-popcnt algorithm is shown here - * http://www.hackersdelight.org/hdcodetxt/ntz.c.txt - * as the ntz2 variant. These likely aren't the most efficient methods, but - * they're good enough if the GCC built-ins aren't available. - */ -inline int PopCount(uint32_t v) -{ - v = v - ((v >> 1) & 0x55555555u); - v = (v & 0x33333333u) + ((v >> 2) & 0x33333333u); - v = (v + (v >> 4)) & 0x0f0f0f0fu; - return static_cast<int>((v * 0x01010101u) >> 24); -} -inline int PopCount(uint64_t v) -{ - v = v - ((v >> 1) & 0x5555555555555555_u64); - v = (v & 0x3333333333333333_u64) + ((v >> 2) & 0x3333333333333333_u64); - v = (v + (v >> 4)) & 0x0f0f0f0f0f0f0f0f_u64; - return static_cast<int>((v * 0x0101010101010101_u64) >> 56); -} - -#if defined(_WIN64) - -inline int CountTrailingZeros(uint32_t v) -{ - unsigned long idx = 32; - _BitScanForward(&idx, v); - return static_cast<int>(idx); -} -inline int CountTrailingZeros(uint64_t v) -{ - unsigned long idx = 64; - _BitScanForward64(&idx, v); - return static_cast<int>(idx); -} - -#elif defined(_WIN32) - -inline int CountTrailingZeros(uint32_t v) -{ - unsigned long idx = 32; - _BitScanForward(&idx, v); - return static_cast<int>(idx); -} -inline int CountTrailingZeros(uint64_t v) -{ - unsigned long idx = 64; - if(!_BitScanForward(&idx, static_cast<uint32_t>(v&0xffffffff))) - { - if(_BitScanForward(&idx, static_cast<uint32_t>(v>>32))) - idx += 32; - } - return static_cast<int>(idx); -} - -#else - -inline int CountTrailingZeros(uint32_t value) -{ return PopCount(~value & (value - 1)); } -inline int CountTrailingZeros(uint64_t value) -{ return PopCount(~value & (value - 1)); } - -#endif -#endif - - /** * Fast float-to-int conversion. No particular rounding mode is assumed; the * IEEE-754 default is round-to-nearest with ties-to-even, though an app could |