diff options
Diffstat (limited to 'src/common/bit_util.h')
| -rw-r--r-- | src/common/bit_util.h | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 1eea17ba1..d032df413 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h | |||
| @@ -32,7 +32,7 @@ inline u32 CountLeadingZeroes32(u32 value) { | |||
| 32 | return 32; | 32 | return 32; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | inline u64 CountLeadingZeroes64(u64 value) { | 35 | inline u32 CountLeadingZeroes64(u64 value) { |
| 36 | unsigned long leading_zero = 0; | 36 | unsigned long leading_zero = 0; |
| 37 | 37 | ||
| 38 | if (_BitScanReverse64(&leading_zero, value) != 0) { | 38 | if (_BitScanReverse64(&leading_zero, value) != 0) { |
| @@ -47,15 +47,54 @@ inline u32 CountLeadingZeroes32(u32 value) { | |||
| 47 | return 32; | 47 | return 32; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | return __builtin_clz(value); | 50 | return static_cast<u32>(__builtin_clz(value)); |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | inline u64 CountLeadingZeroes64(u64 value) { | 53 | inline u32 CountLeadingZeroes64(u64 value) { |
| 54 | if (value == 0) { | 54 | if (value == 0) { |
| 55 | return 64; | 55 | return 64; |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | return __builtin_clzll(value); | 58 | return static_cast<u32>(__builtin_clzll(value)); |
| 59 | } | 59 | } |
| 60 | #endif | 60 | #endif |
| 61 | |||
| 62 | #ifdef _MSC_VER | ||
| 63 | inline u32 CountTrailingZeroes32(u32 value) { | ||
| 64 | unsigned long trailing_zero = 0; | ||
| 65 | |||
| 66 | if (_BitScanForward(&trailing_zero, value) != 0) { | ||
| 67 | return trailing_zero; | ||
| 68 | } | ||
| 69 | |||
| 70 | return 32; | ||
| 71 | } | ||
| 72 | |||
| 73 | inline u32 CountTrailingZeroes64(u64 value) { | ||
| 74 | unsigned long trailing_zero = 0; | ||
| 75 | |||
| 76 | if (_BitScanForward64(&trailing_zero, value) != 0) { | ||
| 77 | return trailing_zero; | ||
| 78 | } | ||
| 79 | |||
| 80 | return 64; | ||
| 81 | } | ||
| 82 | #else | ||
| 83 | inline u32 CountTrailingZeroes32(u32 value) { | ||
| 84 | if (value == 0) { | ||
| 85 | return 32; | ||
| 86 | } | ||
| 87 | |||
| 88 | return static_cast<u32>(__builtin_ctz(value)); | ||
| 89 | } | ||
| 90 | |||
| 91 | inline u32 CountTrailingZeroes64(u64 value) { | ||
| 92 | if (value == 0) { | ||
| 93 | return 64; | ||
| 94 | } | ||
| 95 | |||
| 96 | return static_cast<u32>(__builtin_ctzll(value)); | ||
| 97 | } | ||
| 98 | #endif | ||
| 99 | |||
| 61 | } // namespace Common | 100 | } // namespace Common |