14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
21 #include <type_traits>
27 #ifdef __ANDROID_NDK__
28 #include <android/api-level.h>
46 return std::numeric_limits<T>::digits;
51 std::size_t ZeroBits = 0;
52 T Shift = std::numeric_limits<T>::digits >> 1;
53 T Mask = std::numeric_limits<T>::max() >> Shift;
55 if ((Val & Mask) == 0) {
66 #if __GNUC__ >= 4 || _MSC_VER
67 template <
typename T>
struct TrailingZerosCounter<
T, 4> {
72 #if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0)
73 return __builtin_ctz(Val);
76 _BitScanForward(&Index, Val);
82 #if !defined(_MSC_VER) || defined(_M_X64)
83 template <
typename T>
struct TrailingZerosCounter<
T, 8> {
88 #if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0)
89 return __builtin_ctzll(Val);
92 _BitScanForward64(&Index, Val);
108 template <
typename T>
110 static_assert(std::numeric_limits<T>::is_integer &&
111 !std::numeric_limits<T>::is_signed,
112 "Only unsigned integral types are allowed.");
120 return std::numeric_limits<T>::digits;
123 std::size_t ZeroBits = 0;
124 for (
T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
125 T Tmp = Val >> Shift;
135 #if __GNUC__ >= 4 || _MSC_VER
136 template <
typename T>
struct LeadingZerosCounter<
T, 4> {
141 #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
142 return __builtin_clz(Val);
145 _BitScanReverse(&Index, Val);
151 #if !defined(_MSC_VER) || defined(_M_X64)
152 template <
typename T>
struct LeadingZerosCounter<
T, 8> {
157 #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
158 return __builtin_clzll(Val);
161 _BitScanReverse64(&Index, Val);
177 template <
typename T>
179 static_assert(std::numeric_limits<T>::is_integer &&
180 !std::numeric_limits<T>::is_signed,
181 "Only unsigned integral types are allowed.");
193 if (ZB ==
ZB_Max && Val == 0)
194 return std::numeric_limits<T>::max();
207 if (ZB ==
ZB_Max && Val == 0)
208 return std::numeric_limits<T>::max();
213 (std::numeric_limits<T>::digits - 1);
220 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
221 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
222 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
230 template <
typename T>
232 unsigned char in[
sizeof(Val)];
233 unsigned char out[
sizeof(Val)];
234 std::memcpy(in, &Val,
sizeof(Val));
235 for (
unsigned i = 0; i <
sizeof(Val); ++i)
237 std::memcpy(&Val, out,
sizeof(Val));
247 return static_cast<uint32_t
>(Value >> 32);
252 return static_cast<uint32_t
>(Value);
257 inline uint64_t
Make_64(uint32_t High, uint32_t Low) {
258 return ((uint64_t)High << 32) | (uint64_t)Low;
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;
343 return Value &&
isMask_32((Value - 1) | Value);
349 return Value &&
isMask_64((Value - 1) | Value);
355 return Value && !(Value & (Value - 1));
361 return Value && !(Value & (Value - int64_t(1L)));
390 template <
typename T>
392 static_assert(std::numeric_limits<T>::is_integer &&
393 !std::numeric_limits<T>::is_signed,
394 "Only unsigned integral types are allowed.");
406 template <
typename T>
408 static_assert(std::numeric_limits<T>::is_integer &&
409 !std::numeric_limits<T>::is_signed,
410 "Only unsigned integral types are allowed.");
418 static_assert(SizeOfT <= 4,
"Not implemented!");
420 return __builtin_popcount(Value);
423 v = v - ((v >> 1) & 0x55555555);
424 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
425 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
433 return __builtin_popcountll(Value);
436 v = v - ((v >> 1) & 0x5555555555555555ULL);
437 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
438 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
439 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
448 template <
typename T>
450 static_assert(std::numeric_limits<T>::is_integer &&
451 !std::numeric_limits<T>::is_signed,
452 "Only unsigned integral types are allowed.");
458 #if defined(__ANDROID_API__) && __ANDROID_API__ < 18
459 return __builtin_log(Value) / __builtin_log(2.0);
558 return (A | B) & (1 + ~(A | B));
565 inline uintptr_t
alignAddr(
const void *Addr,
size_t Alignment) {
567 "Alignment is not a power of two!");
569 assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
571 return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
577 return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
610 return (Value + Align - 1) / Align *
Align;
623 return int32_t(x << (32 - B)) >> (32 - B);
629 return int32_t(X << (32 - B)) >> (32 - B);
635 return int64_t(x << (64 - B)) >> (64 - B);
641 return int64_t(X << (64 - B)) >> (64 - B);
bool isInt< 32 >(int64_t x)
bool isUInt< 8 >(uint64_t x)
unsigned Log2_32_Ceil(uint32_t Value)
Log2_32_Ceil - This function returns the ceil log base 2 of the specified value, 32 if the value is z...
T findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
GreatestCommonDivisor64 - Return the greatest common divisor of the two values using Euclid's algorit...
static unsigned count(T Value)
float BitsToFloat(uint32_t Bits)
BitsToFloat - This function takes a 32-bit integer and returns the bit equivalent float...
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.
std::size_t 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 isShiftedMask_32(uint32_t Value)
isShiftedMask_32 - This function returns true if the argument contains a non-empty sequence of ones w...
uint32_t SwapByteOrder_32(uint32_t value)
SwapByteOrder_32 - This function returns a byte-swapped representation of the 32-bit argument...
The returned value is numeric_limits<T>::digits.
std::size_t countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
The returned value is undefined.
bool isInt< 8 >(int64_t x)
static unsigned count(T Value)
uint32_t ByteSwap_32(uint32_t Value)
ByteSwap_32 - This function returns a byte-swapped representation of the 32-bit argument, Value.
bool isShiftedInt(int64_t x)
isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted left by S...
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
uint16_t ByteSwap_16(uint16_t Value)
ByteSwap_16 - This function returns a byte-swapped representation of the 16-bit argument, Value.
bool isMask_64(uint64_t Value)
isMask_64 - This function returns true if the argument is a non-empty sequence of ones starting at th...
uint32_t FloatToBits(float Float)
FloatToBits - This function takes a float and returns the bit equivalent 32-bit integer.
uint16_t SwapByteOrder_16(uint16_t value)
SwapByteOrder_16 - This function returns a byte-swapped representation of the 16-bit argument...
std::size_t 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...
size_t alignmentAdjustment(const void *Ptr, size_t Alignment)
Returns the necessary adjustment for aligning Ptr to Alignment bytes, rounding up.
uint32_t Lo_32(uint64_t Value)
Lo_32 - This function returns the low 32 bits of a 64 bit value.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
The returned value is numeric_limits<T>::max()
bool isIntN(unsigned N, int64_t x)
isIntN - Checks if an signed integer fits into the given (dynamic) bit width.
uint64_t NextPowerOf2(uint64_t A)
NextPowerOf2 - Returns the next power of two (in 64-bits) that is strictly greater than A...
bool isShiftedUInt(uint64_t x)
isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted left by S...
T findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
uint64_t SwapByteOrder_64(uint64_t value)
SwapByteOrder_64 - This function returns a byte-swapped representation of the 64-bit argument...
bool isShiftedMask_64(uint64_t Value)
isShiftedMask_64 - This function returns true if the argument contains a non-empty sequence of ones w...
unsigned countPopulation(T Value)
Count the number of set bits in a value.
int64_t SignExtend64(uint64_t x)
SignExtend64 - Sign extend B-bit number x to 64-bit int.
double Log2(double Value)
Log2 - This function returns the log base 2 of the specified value.
uint64_t DoubleToBits(double Double)
DoubleToBits - This function takes a double and returns the bit equivalent 64-bit integer...
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
double BitsToDouble(uint64_t Bits)
BitsToDouble - This function takes a 64-bit integer and returns the bit equivalent double...
static std::size_t count(T Val, ZeroBehavior)
unsigned Log2_64_Ceil(uint64_t Value)
Log2_64_Ceil - This function returns the ceil log base 2 of the specified value, 64 if the value is z...
unsigned Log2_32(uint32_t Value)
Log2_32 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
bool isUInt< 32 >(uint64_t x)
bool isMask_32(uint32_t Value)
isMask_32 - This function returns true if the argument is a non-empty sequence of ones starting at th...
bool isPowerOf2_64(uint64_t Value)
isPowerOf2_64 - This function returns true if the argument is a power of two 0 (64 bit edition...
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
uintptr_t alignAddr(const void *Addr, size_t Alignment)
Aligns Addr to Alignment bytes, rounding up.
uint64_t MinAlign(uint64_t A, uint64_t B)
MinAlign - A and B are either alignments or offsets.
static std::size_t count(T Val, ZeroBehavior)
uint64_t ByteSwap_64(uint64_t Value)
ByteSwap_64 - This function returns a byte-swapped representation of the 64-bit argument, Value.
int32_t SignExtend32(uint32_t x)
SignExtend32 - Sign extend B-bit number x to 32-bit int.
uint32_t Hi_32(uint64_t Value)
Hi_32 - This function returns the high 32 bits of a 64 bit value.
uint64_t Make_64(uint32_t High, uint32_t Low)
Make_64 - This functions makes a 64-bit integer from a high / low pair of 32-bit integers.
bool isInt< 16 >(int64_t x)
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
LLVM Value Representation.
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
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)
isPowerOf2_32 - This function returns true if the argument is a power of two > 0. ...
bool isUIntN(unsigned N, uint64_t x)
isUIntN - Checks if an unsigned integer fits into the given (dynamic) bit width.
T reverseBits(T Val)
Reverse the bits in Val.
std::size_t countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
unsigned Log2_64(uint64_t Value)
Log2_64 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...