14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
17 #include "llvm/Compiler.h"
34 static std::size_t count(T Val, ZeroBehavior) {
36 return std::numeric_limits<T>::digits;
39 std::size_t ZeroBits = 0;
40 for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
51 #if __GNUC__ >= 4 || _MSC_VER
53 static std::size_t count(T Val, ZeroBehavior ZB) {
54 if (ZB != ZB_Undefined && Val == 0)
57 #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
58 return __builtin_clz(Val);
61 _BitScanReverse(&Index, Val);
67 #if !defined(_MSC_VER) || defined(_M_X64)
68 template <
typename T>
struct LeadingZerosCounter<T, 8> {
69 static std::size_t count(T Val, ZeroBehavior ZB) {
70 if (ZB != ZB_Undefined && Val == 0)
73 #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
74 return __builtin_clzll(Val);
77 _BitScanReverse64(&Index, Val);
94 std::size_t countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
95 static_assert(std::numeric_limits<T>::is_integer &&
96 !std::numeric_limits<T>::is_signed,
97 "Only unsigned integral types are allowed.");
98 return detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
104 inline unsigned Log2_32(uint32_t Value) {
105 return 31 - countLeadingZeros(Value);
110 inline unsigned Log2_64(uint64_t Value) {
111 return 63 - countLeadingZeros(Value);
117 inline unsigned Log2_32_Ceil(uint32_t Value) {
118 return 32 - countLeadingZeros(Value - 1);
123 inline unsigned Log2_64_Ceil(uint64_t Value) {
124 return 64 - countLeadingZeros(Value - 1);
129 inline double BitsToDouble(uint64_t Bits) {
140 inline float BitsToFloat(uint32_t Bits) {
153 inline uint64_t DoubleToBits(
double Double) {
166 inline uint32_t FloatToBits(
float Float) {
177 inline uint64_t NextPowerOf2(uint64_t A) {
Definition: MathExtras.h:33