13 #ifndef LLVM_SUPPORT_MATHEXTRAS_H 14 #define LLVM_SUPPORT_MATHEXTRAS_H 23 #include <type_traits> 25 #ifdef __ANDROID_NDK__ 26 #include <android/api-level.h> 34 unsigned char _BitScanForward(
unsigned long *_Index,
unsigned long _Mask);
35 unsigned char _BitScanForward64(
unsigned long *_Index,
unsigned __int64 _Mask);
36 unsigned char _BitScanReverse(
unsigned long *_Index,
unsigned long _Mask);
37 unsigned char _BitScanReverse64(
unsigned long *_Index,
unsigned __int64 _Mask);
57 constexpr
double e = 2.7182818284590452354,
59 ln2 = .69314718055994530942,
60 ln10 = 2.3025850929940456840,
63 pi = 3.1415926535897932385,
71 phi = 1.6180339887498948482;
72 constexpr
float ef = 2.71828183F,
93 return std::numeric_limits<T>::digits;
98 unsigned ZeroBits = 0;
99 T Shift = std::numeric_limits<T>::digits >> 1;
102 if ((Val & Mask) == 0) {
113 #if defined(__GNUC__) || defined(_MSC_VER) 119 #if __has_builtin(__builtin_ctz) || defined(__GNUC__) 120 return __builtin_ctz(Val);
121 #elif defined(_MSC_VER) 123 _BitScanForward(&Index, Val);
129 #if !defined(_MSC_VER) || defined(_M_X64) 135 #if __has_builtin(__builtin_ctzll) || defined(__GNUC__) 136 return __builtin_ctzll(Val);
137 #elif defined(_MSC_VER) 139 _BitScanForward64(&Index, Val);
155 template <
typename T>
157 static_assert(std::numeric_limits<T>::is_integer &&
158 !std::numeric_limits<T>::is_signed,
159 "Only unsigned integral types are allowed.");
167 return std::numeric_limits<T>::digits;
170 unsigned ZeroBits = 0;
171 for (
T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
172 T Tmp = Val >> Shift;
182 #if defined(__GNUC__) || defined(_MSC_VER) 188 #if __has_builtin(__builtin_clz) || defined(__GNUC__) 189 return __builtin_clz(Val);
190 #elif defined(_MSC_VER) 192 _BitScanReverse(&Index, Val);
198 #if !defined(_MSC_VER) || defined(_M_X64) 204 #if __has_builtin(__builtin_clzll) || defined(__GNUC__) 205 return __builtin_clzll(Val);
206 #elif defined(_MSC_VER) 208 _BitScanReverse64(&Index, Val);
224 template <
typename T>
226 static_assert(std::numeric_limits<T>::is_integer &&
227 !std::numeric_limits<T>::is_signed,
228 "Only unsigned integral types are allowed.");
240 if (ZB ==
ZB_Max && Val == 0)
249 static_assert(std::is_unsigned<T>::value,
"Invalid type!");
250 const unsigned Bits = CHAR_BIT *
sizeof(
T);
251 assert(N <= Bits &&
"Invalid bit index");
252 return N == 0 ? 0 : (
T(-1) >> (Bits -
N));
258 return ~maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
264 return maskLeadingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
270 return maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
281 if (ZB ==
ZB_Max && Val == 0)
287 (std::numeric_limits<T>::digits - 1);
294 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64 295 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16) 296 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4) 304 template <
typename T>
306 unsigned char in[
sizeof(Val)];
307 unsigned char out[
sizeof(Val)];
308 std::memcpy(in, &Val,
sizeof(Val));
309 for (
unsigned i = 0; i <
sizeof(Val); ++i)
310 out[(
sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
311 std::memcpy(&Val, out,
sizeof(Val));
321 return static_cast<uint32_t>(Value >> 32);
331 return ((uint64_t)High << 32) | (uint64_t)Low;
335 template <
unsigned N> constexpr
inline bool isInt(int64_t x) {
336 return N >= 64 || (-(INT64_C(1)<<(
N-1)) <= x && x < (INT64_C(1)<<(
N-1)));
339 template <> constexpr
inline bool isInt<8>(int64_t x) {
340 return static_cast<int8_t
>(x) == x;
342 template <> constexpr
inline bool isInt<16>(int64_t x) {
343 return static_cast<int16_t
>(x) == x;
345 template <> constexpr
inline bool isInt<32>(int64_t x) {
346 return static_cast<int32_t
>(x) == x;
350 template <
unsigned N,
unsigned S>
353 N > 0,
"isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
354 static_assert(
N + S <= 64,
"isShiftedInt<N, S> with N + S > 64 is too wide.");
355 return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
366 template <
unsigned N>
367 constexpr
inline typename std::enable_if<(N < 64), bool>::type
369 static_assert(
N > 0,
"isUInt<0> doesn't make sense");
370 return X < (UINT64_C(1) << (
N));
372 template <
unsigned N>
373 constexpr
inline typename std::enable_if<N >= 64,
bool>
::type 379 template <> constexpr
inline bool isUInt<8>(uint64_t x) {
380 return static_cast<uint8_t
>(x) == x;
383 return static_cast<uint16_t>(x) == x;
386 return static_cast<uint32_t>(x) == x;
390 template <
unsigned N,
unsigned S>
393 N > 0,
"isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
394 static_assert(
N + S <= 64,
395 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
398 return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
403 assert(N > 0 && N <= 64 &&
"integer width out of range");
414 assert(N > 0 && N <= 64 &&
"integer width out of range");
416 return -(UINT64_C(1)<<(N-1));
421 assert(N > 0 && N <= 64 &&
"integer width out of range");
425 return (UINT64_C(1) << (N - 1)) - 1;
442 return Value && ((Value + 1) & Value) == 0;
448 return Value && ((Value + 1) & Value) == 0;
454 return Value &&
isMask_32((Value - 1) | Value);
460 return Value &&
isMask_64((Value - 1) | Value);
466 return Value && !(Value & (Value - 1));
471 return Value && !(Value & (Value - 1));
497 template <
typename T>
499 static_assert(std::numeric_limits<T>::is_integer &&
500 !std::numeric_limits<T>::is_signed,
501 "Only unsigned integral types are allowed.");
502 return countLeadingZeros<T>(~Value, ZB);
513 template <
typename T>
515 static_assert(std::numeric_limits<T>::is_integer &&
516 !std::numeric_limits<T>::is_signed,
517 "Only unsigned integral types are allowed.");
518 return countTrailingZeros<T>(~Value, ZB);
525 static_assert(SizeOfT <= 4,
"Not implemented!");
526 #if defined(__GNUC__) 527 return __builtin_popcount(Value);
530 v = v - ((v >> 1) & 0x55555555);
531 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
532 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
539 #if defined(__GNUC__) 540 return __builtin_popcountll(Value);
543 v = v - ((v >> 1) & 0x5555555555555555ULL);
544 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
545 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
546 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
555 template <
typename T>
557 static_assert(std::numeric_limits<T>::is_integer &&
558 !std::numeric_limits<T>::is_signed,
559 "Only unsigned integral types are allowed.");
565 template <
size_t kValue> constexpr
inline size_t CTLog2() {
567 "Value is not a valid power of 2");
568 return 1 +
CTLog2<kValue / 2>();
571 template <> constexpr
inline size_t CTLog2<1>() {
return 0; }
575 #if defined(__ANDROID_API__) && __ANDROID_API__ < 18 576 return __builtin_log(Value) / __builtin_log(2.0);
609 template <
typename T>
620 return greatestCommonDivisor<uint64_t>(A,
B);
626 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
627 memcpy(&D, &Bits,
sizeof(Bits));
634 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
635 memcpy(&F, &Bits,
sizeof(Bits));
644 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
645 memcpy(&Bits, &Double,
sizeof(Double));
654 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
655 memcpy(&Bits, &Float,
sizeof(Float));
661 constexpr
inline uint64_t
MinAlign(uint64_t A, uint64_t
B) {
667 return (A | B) & (1 + ~(A |
B));
718 assert(Align != 0u &&
"Align can't be 0.");
720 return (Value + Align - 1 - Skew) / Align * Align + Skew;
725 template <u
int64_t Align> constexpr
inline uint64_t
alignTo(uint64_t
Value) {
726 static_assert(
Align != 0u,
"Align must be non-zero");
731 inline uint64_t
divideCeil(uint64_t Numerator, uint64_t Denominator) {
732 return alignTo(Numerator, Denominator) / Denominator;
737 return (Numerator + (Denominator / 2)) / Denominator;
743 assert(Align != 0u &&
"Align can't be 0.");
745 return (Value - Skew) / Align * Align + Skew;
751 static_assert(
B > 0,
"Bit width can't be 0.");
752 static_assert(
B <= 32,
"Bit width out of range.");
753 return int32_t(X << (32 -
B)) >> (32 -
B);
759 assert(B > 0 &&
"Bit width can't be 0.");
760 assert(B <= 32 &&
"Bit width out of range.");
761 return int32_t(X << (32 - B)) >> (32 -
B);
766 template <
unsigned B> constexpr
inline int64_t
SignExtend64(uint64_t x) {
767 static_assert(
B > 0,
"Bit width can't be 0.");
768 static_assert(
B <= 64,
"Bit width out of range.");
769 return int64_t(x << (64 -
B)) >> (64 -
B);
775 assert(B > 0 &&
"Bit width can't be 0.");
776 assert(B <= 64 &&
"Bit width out of range.");
777 return int64_t(X << (64 - B)) >> (64 -
B);
782 template <
typename T>
783 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 785 return std::max(X, Y) - std::min(X, Y);
791 template <
typename T>
792 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 795 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
798 Overflowed = (Z < X || Z <
Y);
808 template <
typename T>
809 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 812 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
827 if (Log2Z < Log2Max) {
830 if (Log2Z > Log2Max) {
839 if (Z & ~(Max >> 1)) {
854 template <
typename T>
855 typename std::enable_if<std::is_unsigned<T>::value,
T>
::type 858 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
873 template <
typename T>
874 typename std::enable_if<std::is_signed<T>::value,
T>
::type 876 #if __has_builtin(__builtin_add_overflow) 877 return __builtin_add_overflow(X, Y, &Result);
880 using U =
typename std::make_unsigned<T>::type;
881 const U UX =
static_cast<U
>(
X);
882 const U UY =
static_cast<U
>(
Y);
883 const U UResult = UX + UY;
886 Result =
static_cast<T>(UResult);
900 template <
typename T>
901 typename std::enable_if<std::is_signed<T>::value,
T>
::type 903 #if __has_builtin(__builtin_sub_overflow) 904 return __builtin_sub_overflow(X, Y, &Result);
907 using U =
typename std::make_unsigned<T>::type;
908 const U UX =
static_cast<U
>(
X);
909 const U UY =
static_cast<U
>(
Y);
910 const U UResult = UX - UY;
913 Result =
static_cast<T>(UResult);
928 template <
typename T>
929 typename std::enable_if<std::is_signed<T>::value,
T>
::type 932 using U =
typename std::make_unsigned<T>::type;
933 const U UX = X < 0 ? (0 -
static_cast<U
>(
X)) :
static_cast<U
>(
X);
934 const U UY = Y < 0 ? (0 -
static_cast<U
>(
Y)) :
static_cast<U
>(
Y);
935 const U UResult = UX * UY;
938 const bool IsNegative = (X < 0) ^ (Y < 0);
939 Result = IsNegative ? (0 - UResult) : UResult;
942 if (UX == 0 || UY == 0)
unsigned Log2(Align A)
Returns the log2 of the alignment.
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.
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
This class represents lattice values for constants.
unsigned countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
static unsigned count(T Value)
constexpr float inv_sqrtpif
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)
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.
constexpr double inv_sqrtpi
unsigned 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...
static unsigned count(T Val, ZeroBehavior)
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)
This function returns a byte-swapped representation of the 32-bit argument.
The returned value is numeric_limits<T>::digits.
std::enable_if< std::is_signed< T >::value, T >::type MulOverflow(T X, T Y, T &Result)
Multiply two signed integers, computing the two's complement truncated result, returning true if an o...
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.
static unsigned count(T Val, ZeroBehavior)
T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
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...
T greatestCommonDivisor(T A, T B)
Return the greatest common divisor of the values using Euclid's algorithm.
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)
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.
std::enable_if< std::is_signed< T >::value, T >::type AddOverflow(T X, T Y, T &Result)
Add two signed integers, computing the two's complement truncated result, returning true if overflow ...
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.
unsigned 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...
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)
This function returns a byte-swapped representation of the 64-bit argument.
constexpr size_t CTLog2< 1 >()
This struct is a compact representation of a valid (non-zero power of two) alignment.
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.
uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator)
Returns the integer nearest(Numerator / Denominator).
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)
Align max(MaybeAlign Lhs, Align Rhs)
constexpr float inv_sqrt3f
constexpr size_t CTLog2()
Compile time Log2.
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.
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.
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 alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
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.
constexpr double inv_sqrt2
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.
unsigned countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
constexpr float inv_sqrt2f
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.
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.
std::enable_if< std::is_signed< T >::value, T >::type SubOverflow(T X, T Y, T &Result)
Subtract two signed integers, computing the two's complement truncated result, returning true if an o...
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...
constexpr double inv_sqrt3
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.
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. ...