| 44 | |
| 45 | template<typename T> |
| 46 | constexpr int CLZ(T x) { |
| 47 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 48 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
| 49 | static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); |
| 50 | static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t), |
| 51 | "Unsupported sizeof(T)"); |
| 52 | DCHECK_NE(x, 0u); |
| 53 | constexpr bool is_64_bit = (sizeof(T) == sizeof(uint64_t)); |
| 54 | constexpr size_t adjustment = |
| 55 | is_64_bit ? 0u : std::numeric_limits<uint32_t>::digits - std::numeric_limits<T>::digits; |
| 56 | return is_64_bit ? __builtin_clzll(x) : __builtin_clz(x) - adjustment; |
| 57 | } |
| 58 | |
| 59 | // Similar to CLZ except that on zero input it returns bitwidth and supports signed integers. |
| 60 | template<typename T> |
no outgoing calls
no test coverage detected