14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
47 typename enable_if_c<std::numeric_limits<T>::is_integer &&
48 !std::numeric_limits<T>::is_signed, std::size_t>::type
53 return std::numeric_limits<T>::digits;
58 std::size_t ZeroBits = 0;
59 T Shift = std::numeric_limits<T>::digits >> 1;
60 T Mask = std::numeric_limits<T>::max() >> Shift;
62 if ((Val & Mask) == 0) {
74 typename enable_if_c<std::numeric_limits<T>::is_integer &&
75 std::numeric_limits<T>::is_signed, std::size_t>::type
78 #if __GNUC__ >= 4 || _MSC_VER
80 inline std::size_t countTrailingZeros<uint32_t>(uint32_t Val,
ZeroBehavior ZB) {
84 #if __has_builtin(__builtin_ctz) || __GNUC_PREREQ(4, 0)
85 return __builtin_ctz(Val);
88 _BitScanForward(&Index, Val);
93 #if !defined(_MSC_VER) || defined(_M_X64)
95 inline std::size_t countTrailingZeros<uint64_t>(uint64_t Val,
ZeroBehavior ZB) {
99 #if __has_builtin(__builtin_ctzll) || __GNUC_PREREQ(4, 0)
100 return __builtin_ctzll(Val);
103 _BitScanForward64(&Index, Val);
117 template <
typename T>
118 typename enable_if_c<std::numeric_limits<T>::is_integer &&
119 !std::numeric_limits<T>::is_signed, std::size_t>::type
124 return std::numeric_limits<T>::digits;
127 std::size_t ZeroBits = 0;
128 for (
T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
129 T Tmp = Val >> Shift;
139 template <
typename T>
140 typename enable_if_c<std::numeric_limits<T>::is_integer &&
141 std::numeric_limits<T>::is_signed, std::size_t>::type
144 #if __GNUC__ >= 4 || _MSC_VER
146 inline std::size_t countLeadingZeros<uint32_t>(uint32_t Val,
ZeroBehavior ZB) {
150 #if __has_builtin(__builtin_clz) || __GNUC_PREREQ(4, 0)
151 return __builtin_clz(Val);
154 _BitScanReverse(&Index, Val);
159 #if !defined(_MSC_VER) || defined(_M_X64)
161 inline std::size_t countLeadingZeros<uint64_t>(uint64_t Val,
ZeroBehavior ZB) {
165 #if __has_builtin(__builtin_clzll) || __GNUC_PREREQ(4, 0)
166 return __builtin_clzll(Val);
169 _BitScanReverse64(&Index, Val);
183 template <
typename T>
184 typename enable_if_c<std::numeric_limits<T>::is_integer &&
185 !std::numeric_limits<T>::is_signed,
T>::type
187 if (ZB ==
ZB_Max && Val == 0)
188 return std::numeric_limits<T>::max();
194 template <
typename T>
195 typename enable_if_c<std::numeric_limits<T>::is_integer &&
196 std::numeric_limits<T>::is_signed,
T>::type
206 template <typename
T>
207 typename enable_if_c<std::numeric_limits<
T>::is_integer &&
208 !std::numeric_limits<
T>::is_signed,
T>::type
210 if (ZB == ZB_Max && Val == 0)
211 return std::numeric_limits<T>::max();
216 (std::numeric_limits<T>::digits - 1);
220 template <
typename T>
221 typename enable_if_c<std::numeric_limits<T>::is_integer &&
222 std::numeric_limits<T>::is_signed,
T>::type
229 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
230 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
231 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
236 template <
typename T>
238 unsigned char in[
sizeof(Val)];
239 unsigned char out[
sizeof(Val)];
241 for (
unsigned i = 0; i <
sizeof(Val); ++i)
253 return static_cast<uint32_t
>(Value >> 32);
258 return static_cast<uint32_t
>(Value);
264 return N >= 64 || (-(INT64_C(1)<<(
N-1)) <= x && x < (INT64_C(1)<<(
N-1)));
269 return static_cast<int8_t
>(x) == x;
273 return static_cast<int16_t
>(x) == x;
277 return static_cast<int32_t
>(x) == x;
282 template<
unsigned N,
unsigned S>
284 return isInt<N+S>(x) && (x % (1<<S) == 0);
290 return N >= 64 || x < (UINT64_C(1)<<(
N));
295 return static_cast<uint8_t
>(x) == x;
299 return static_cast<uint16_t
>(x) == x;
303 return static_cast<uint32_t
>(x) == x;
308 template<
unsigned N,
unsigned S>
310 return isUInt<N+S>(x) && (x % (1<<S) == 0);
316 return x == (x & (~0ULL >> (64 -
N)));
322 return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
329 return Value && ((Value + 1) & Value) == 0;
336 return Value && ((Value + 1) & Value) == 0;
355 return Value && !(Value & (Value - 1));
361 return Value && !(Value & (Value - int64_t(1L)));
419 return __builtin_popcount(Value);
421 uint32_t v = Value - ((Value >> 1) & 0x55555555);
422 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
423 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
431 return __builtin_popcountll(Value);
433 uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
434 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
435 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
436 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
541 return (A | B) & (1 + ~(A | B));
566 return ((Value + Align - 1) / Align) *
Align;
580 return (x < 0) ? -x : x;
586 return int32_t(x << (32 - B)) >> (32 - B);
592 return int32_t(X << (32 - B)) >> (32 - B);
598 return int64_t(x << (64 - B)) >> (64 - B);
604 return int64_t(X << (64 - B)) >> (64 - B);
607 #if defined(_MSC_VER)
610 const float huge_valf = std::numeric_limits<float>::infinity();
bool isInt< 32 >(int64_t x)
bool isUInt< 8 >(uint64_t x)
unsigned Log2_32_Ceil(uint32_t Value)
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
float BitsToFloat(uint32_t Bits)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, T >::type findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
ZeroBehavior
The behavior an operation has on an input of 0.
bool isUInt(uint64_t x)
isUInt - Checks if an unsigned integer fits into the given bit width.
bool isShiftedMask_32(uint32_t Value)
uint32_t SwapByteOrder_32(uint32_t value)
unsigned CountTrailingOnes_64(uint64_t Value)
The returned value is numeric_limits<T>::digits.
The returned value is undefined.
bool isInt< 8 >(int64_t x)
uint32_t ByteSwap_32(uint32_t Value)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
bool isShiftedInt(int64_t x)
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
uint16_t ByteSwap_16(uint16_t Value)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
bool isMask_64(uint64_t Value)
uint32_t FloatToBits(float Float)
uint16_t SwapByteOrder_16(uint16_t value)
int IsInf(float f)
Platform-independent wrappers for the C99 isinf() function.
uint32_t Lo_32(uint64_t Value)
Lo_32 - This function returns the low 32 bits of a 64 bit value.
The returned value is numeric_limits<T>::max()
unsigned CountPopulation_64(uint64_t Value)
bool isIntN(unsigned N, int64_t x)
uint64_t NextPowerOf2(uint64_t A)
bool isShiftedUInt(uint64_t x)
uint64_t SwapByteOrder_64(uint64_t value)
bool isShiftedMask_64(uint64_t Value)
unsigned CountPopulation_32(uint32_t Value)
unsigned CountTrailingOnes_32(uint32_t Value)
int64_t SignExtend64(uint64_t x)
uint64_t DoubleToBits(double Double)
unsigned CountLeadingOnes_64(uint64_t Value)
double BitsToDouble(uint64_t Bits)
unsigned Log2_64_Ceil(uint64_t Value)
#define LLVM_DELETED_FUNCTION
unsigned Log2_32(uint32_t Value)
bool isUInt< 32 >(uint64_t x)
bool isMask_32(uint32_t Value)
bool isPowerOf2_64(uint64_t Value)
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
uint64_t MinAlign(uint64_t A, uint64_t B)
uint64_t ByteSwap_64(uint64_t Value)
int IsNAN(float f)
Platform-independent wrappers for the C99 isnan() function.
int32_t SignExtend32(uint32_t x)
uint32_t Hi_32(uint64_t Value)
Hi_32 - This function returns the high 32 bits of a 64 bit value.
bool isInt< 16 >(int64_t x)
LLVM Value Representation.
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
bool isUInt< 16 >(uint64_t x)
bool isInt(int64_t x)
isInt - Checks if an integer fits into the given bit width.
bool isPowerOf2_32(uint32_t Value)
bool isUIntN(unsigned N, uint64_t x)
T reverseBits(T Val)
Reverse the bits in Val.
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
unsigned Log2_64(uint64_t Value)
unsigned CountLeadingOnes_32(uint32_t Value)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, T >::type findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.