14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H 15 #define LLVM_SUPPORT_MATHEXTRAS_H 24 #include <type_traits> 26 #ifdef __ANDROID_NDK__ 27 #include <android/api-level.h> 35 unsigned char _BitScanForward(
unsigned long *_Index,
unsigned long _Mask);
36 unsigned char _BitScanForward64(
unsigned long *_Index,
unsigned __int64 _Mask);
37 unsigned char _BitScanReverse(
unsigned long *_Index,
unsigned long _Mask);
38 unsigned char _BitScanReverse64(
unsigned long *_Index,
unsigned __int64 _Mask);
57 return std::numeric_limits<T>::digits;
62 std::size_t ZeroBits = 0;
63 T Shift = std::numeric_limits<T>::digits >> 1;
66 if ((Val & Mask) == 0) {
77 #if __GNUC__ >= 4 || defined(_MSC_VER) 83 #if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0) 84 return __builtin_ctz(Val);
85 #elif defined(_MSC_VER) 87 _BitScanForward(&Index, Val);
93 #if !defined(_MSC_VER) || defined(_M_X64) 99 #if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0) 100 return __builtin_ctzll(Val);
101 #elif defined(_MSC_VER) 103 _BitScanForward64(&Index, Val);
119 template <
typename T>
121 static_assert(std::numeric_limits<T>::is_integer &&
122 !std::numeric_limits<T>::is_signed,
123 "Only unsigned integral types are allowed.");
131 return std::numeric_limits<T>::digits;
134 std::size_t ZeroBits = 0;
135 for (
T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
136 T Tmp = Val >> Shift;
146 #if __GNUC__ >= 4 || defined(_MSC_VER) 152 #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0) 153 return __builtin_clz(Val);
154 #elif defined(_MSC_VER) 156 _BitScanReverse(&Index, Val);
162 #if !defined(_MSC_VER) || defined(_M_X64) 168 #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0) 169 return __builtin_clzll(Val);
170 #elif defined(_MSC_VER) 172 _BitScanReverse64(&Index, Val);
188 template <
typename T>
190 static_assert(std::numeric_limits<T>::is_integer &&
191 !std::numeric_limits<T>::is_signed,
192 "Only unsigned integral types are allowed.");
204 if (ZB ==
ZB_Max && Val == 0)
213 static_assert(std::is_unsigned<T>::value,
"Invalid type!");
214 const unsigned Bits = CHAR_BIT *
sizeof(
T);
215 assert(N <= Bits &&
"Invalid bit index");
216 return N == 0 ? 0 : (
T(-1) >> (Bits -
N));
222 return ~maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
228 return maskLeadingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
234 return maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
245 if (ZB ==
ZB_Max && Val == 0)
251 (std::numeric_limits<T>::digits - 1);
258 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64 259 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16) 260 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4) 268 template <
typename T>
270 unsigned char in[
sizeof(Val)];
271 unsigned char out[
sizeof(Val)];
273 for (
unsigned i = 0; i <
sizeof(Val); ++i)
274 out[(
sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
285 return static_cast<uint32_t>(Value >> 32);
290 return static_cast<uint32_t>(Value);
295 return ((uint64_t)High << 32) | (uint64_t)Low;
299 template <
unsigned N> constexpr
inline bool isInt(int64_t x) {
300 return N >= 64 || (-(INT64_C(1)<<(
N-1)) <= x && x < (INT64_C(1)<<(
N-1)));
303 template <> constexpr
inline bool isInt<8>(int64_t x) {
304 return static_cast<int8_t
>(x) == x;
306 template <> constexpr
inline bool isInt<16>(int64_t x) {
307 return static_cast<int16_t
>(x) == x;
309 template <> constexpr
inline bool isInt<32>(int64_t x) {
310 return static_cast<int32_t
>(x) == x;
314 template <
unsigned N,
unsigned S>
317 N > 0,
"isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
318 static_assert(
N + S <= 64,
"isShiftedInt<N, S> with N + S > 64 is too wide.");
319 return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
330 template <
unsigned N>
331 constexpr
inline typename std::enable_if<(N < 64), bool>::type
333 static_assert(
N > 0,
"isUInt<0> doesn't make sense");
334 return X < (UINT64_C(1) << (
N));
336 template <
unsigned N>
337 constexpr
inline typename std::enable_if<N >= 64,
bool>
::type 343 template <> constexpr
inline bool isUInt<8>(uint64_t x) {
344 return static_cast<uint8_t
>(x) == x;
347 return static_cast<uint16_t
>(x) == x;
350 return static_cast<uint32_t>(x) == x;
354 template <
unsigned N,
unsigned S>
357 N > 0,
"isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
358 static_assert(
N + S <= 64,
359 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
362 return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
367 assert(N > 0 && N <= 64 &&
"integer width out of range");
378 assert(N > 0 && N <= 64 &&
"integer width out of range");
380 return -(UINT64_C(1)<<(N-1));
385 assert(N > 0 && N <= 64 &&
"integer width out of range");
389 return (UINT64_C(1) << (N - 1)) - 1;
406 return Value && ((Value + 1) & Value) == 0;
412 return Value && ((Value + 1) & Value) == 0;
418 return Value &&
isMask_32((Value - 1) | Value);
424 return Value &&
isMask_64((Value - 1) | Value);
430 return Value && !(Value & (Value - 1));
435 return Value && !(Value & (Value - 1));
461 template <
typename T>
463 static_assert(std::numeric_limits<T>::is_integer &&
464 !std::numeric_limits<T>::is_signed,
465 "Only unsigned integral types are allowed.");
466 return countLeadingZeros<T>(~Value, ZB);
477 template <
typename T>
479 static_assert(std::numeric_limits<T>::is_integer &&
480 !std::numeric_limits<T>::is_signed,
481 "Only unsigned integral types are allowed.");
482 return countTrailingZeros<T>(~Value, ZB);
489 static_assert(SizeOfT <= 4,
"Not implemented!");
491 return __builtin_popcount(Value);
494 v = v - ((v >> 1) & 0x55555555);
495 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
496 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
504 return __builtin_popcountll(Value);
507 v = v - ((v >> 1) & 0x5555555555555555ULL);
508 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
509 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
510 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
519 template <
typename T>
521 static_assert(std::numeric_limits<T>::is_integer &&
522 !std::numeric_limits<T>::is_signed,
523 "Only unsigned integral types are allowed.");
529 #if defined(__ANDROID_API__) && __ANDROID_API__ < 18 530 return __builtin_log(Value) / __builtin_log(2.0);
575 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
576 memcpy(&D, &Bits,
sizeof(Bits));
583 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
584 memcpy(&F, &Bits,
sizeof(Bits));
593 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
594 memcpy(&Bits, &Double,
sizeof(Double));
603 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
604 memcpy(&Bits, &Float,
sizeof(Float));
610 constexpr
inline uint64_t
MinAlign(uint64_t A, uint64_t
B) {
616 return (A | B) & (1 + ~(A |
B));
623 inline uintptr_t
alignAddr(
const void *Addr,
size_t Alignment) {
625 "Alignment is not a power of two!");
627 assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
629 return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
635 return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
686 assert(Align != 0u &&
"Align can't be 0.");
688 return (Value + Align - 1 - Skew) / Align * Align + Skew;
693 template <u
int64_t Align> constexpr
inline uint64_t
alignTo(uint64_t
Value) {
694 static_assert(
Align != 0u,
"Align must be non-zero");
699 inline uint64_t
divideCeil(uint64_t Numerator, uint64_t Denominator) {
700 return alignTo(Numerator, Denominator) / Denominator;
707 template <u
int64_t Align>
709 static_assert(
Align != 0u,
"Align must be non-zero");
710 template <u
int64_t Value>
719 assert(Align != 0u &&
"Align can't be 0.");
721 return (Value - Skew) / Align * Align + Skew;
728 return alignTo(Value, Align) - Value;
734 static_assert(
B > 0,
"Bit width can't be 0.");
735 static_assert(
B <= 32,
"Bit width out of range.");
736 return int32_t(X << (32 -
B)) >> (32 -
B);
742 assert(B > 0 &&
"Bit width can't be 0.");
743 assert(B <= 32 &&
"Bit width out of range.");
744 return int32_t(X << (32 - B)) >> (32 -
B);
749 template <
unsigned B> constexpr
inline int64_t
SignExtend64(uint64_t x) {
750 static_assert(
B > 0,
"Bit width can't be 0.");
751 static_assert(
B <= 64,
"Bit width out of range.");
752 return int64_t(x << (64 -
B)) >> (64 -
B);
758 assert(B > 0 &&
"Bit width can't be 0.");
759 assert(B <= 64 &&
"Bit width out of range.");
760 return int64_t(X << (64 - B)) >> (64 -
B);
765 template <
typename T>
766 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 768 return std::max(X, Y) - std::min(X, Y);
774 template <
typename T>
775 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 778 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
781 Overflowed = (Z < X || Z <
Y);
791 template <
typename T>
792 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 795 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
810 if (Log2Z < Log2Max) {
813 if (Log2Z > Log2Max) {
822 if (Z & ~(Max >> 1)) {
837 template <
typename T>
838 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 841 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
constexpr bool isUInt< 32 >(uint64_t x)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
T findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
Return the greatest common divisor of the values using Euclid's algorithm.
This class represents lattice values for constants.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
static unsigned count(T Value)
float BitsToFloat(uint32_t Bits)
This function takes a 32-bit integer and returns the bit equivalent float.
constexpr bool isInt< 8 >(int64_t x)
ZeroBehavior
The behavior an operation has on an input of 0.
constexpr bool isInt< 16 >(int64_t x)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
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...
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingMultiply(T X, T Y, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, of type T.
constexpr bool isMask_32(uint32_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the largest uint64_t less than or equal to Value and is Skew mod Align.
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.
static unsigned count(T Value)
uint32_t ByteSwap_32(uint32_t Value)
Return a byte-swapped representation of the 32-bit argument.
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
uint16_t ByteSwap_16(uint16_t Value)
Return a byte-swapped representation of the 16-bit argument.
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
uint32_t FloatToBits(float Float)
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...
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr bool isUInt< 8 >(uint64_t x)
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...
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
size_t alignmentAdjustment(const void *Ptr, size_t Alignment)
Returns the necessary adjustment for aligning Ptr to Alignment bytes, rounding up.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
The returned value is numeric_limits<T>::max()
static double log2(double V)
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
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...
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, and add the unsigned integer, A to the product.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
constexpr std::enable_if<(N< 64), bool >::type isUInt(uint64_t X)
Checks if an unsigned integer fits into the given bit width.
constexpr bool isInt< 32 >(int64_t x)
double Log2(double Value)
Return the log base 2 of the specified value.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
uint64_t DoubleToBits(double Double)
This function takes a double and returns the bit equivalent 64-bit integer.
double BitsToDouble(uint64_t Bits)
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)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
alignTo for contexts where a constant expression is required.
uintptr_t alignAddr(const void *Addr, size_t Alignment)
Aligns Addr to Alignment bytes, rounding up.
static std::size_t count(T Val, ZeroBehavior)
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
T maskLeadingZeros(unsigned N)
Create a bitmask with the N left-most bits set to 0, and all other bits set to 1. ...
uint64_t ByteSwap_64(uint64_t Value)
Return a byte-swapped representation of the 64-bit argument.
std::enable_if< std::is_unsigned< T >::value, T >::type AbsoluteDifference(T X, T Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result...
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
constexpr bool isUInt< 16 >(uint64_t x)
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
LLVM Value Representation.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
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...
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make a 64-bit integer from a high / low pair of 32-bit integers.
constexpr bool isShiftedMask_32(uint32_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (32 bit ver...
bool isUIntN(unsigned N, uint64_t x)
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)
Return the floor log base 2 of the specified value, -1 if the value is zero.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0. ...