14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
22 #include <type_traits>
29 #ifdef __ANDROID_NDK__
30 #include <android/api-level.h>
48 return std::numeric_limits<T>::digits;
53 std::size_t ZeroBits = 0;
54 T Shift = std::numeric_limits<T>::digits >> 1;
55 T Mask = std::numeric_limits<T>::max() >> Shift;
57 if ((Val & Mask) == 0) {
68 #if __GNUC__ >= 4 || defined(_MSC_VER)
69 template <
typename T>
struct TrailingZerosCounter<
T, 4> {
74 #if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0)
75 return __builtin_ctz(Val);
76 #elif defined(_MSC_VER)
78 _BitScanForward(&Index, Val);
84 #if !defined(_MSC_VER) || defined(_M_X64)
85 template <
typename T>
struct TrailingZerosCounter<
T, 8> {
90 #if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0)
91 return __builtin_ctzll(Val);
92 #elif defined(_MSC_VER)
94 _BitScanForward64(&Index, Val);
110 template <
typename T>
112 static_assert(std::numeric_limits<T>::is_integer &&
113 !std::numeric_limits<T>::is_signed,
114 "Only unsigned integral types are allowed.");
122 return std::numeric_limits<T>::digits;
125 std::size_t ZeroBits = 0;
126 for (
T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
127 T Tmp = Val >> Shift;
137 #if __GNUC__ >= 4 || defined(_MSC_VER)
138 template <
typename T>
struct LeadingZerosCounter<
T, 4> {
143 #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
144 return __builtin_clz(Val);
145 #elif defined(_MSC_VER)
147 _BitScanReverse(&Index, Val);
153 #if !defined(_MSC_VER) || defined(_M_X64)
154 template <
typename T>
struct LeadingZerosCounter<
T, 8> {
159 #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
160 return __builtin_clzll(Val);
161 #elif defined(_MSC_VER)
163 _BitScanReverse64(&Index, Val);
179 template <
typename T>
181 static_assert(std::numeric_limits<T>::is_integer &&
182 !std::numeric_limits<T>::is_signed,
183 "Only unsigned integral types are allowed.");
195 if (ZB ==
ZB_Max && Val == 0)
196 return std::numeric_limits<T>::max();
209 if (ZB ==
ZB_Max && Val == 0)
210 return std::numeric_limits<T>::max();
215 (std::numeric_limits<T>::digits - 1);
222 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
223 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
224 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
232 template <
typename T>
234 unsigned char in[
sizeof(Val)];
235 unsigned char out[
sizeof(Val)];
236 std::memcpy(in, &Val,
sizeof(Val));
237 for (
unsigned i = 0;
i <
sizeof(Val); ++
i)
239 std::memcpy(&Val, out,
sizeof(Val));
249 return static_cast<uint32_t>(Value >> 32);
254 return static_cast<uint32_t>(Value);
260 return ((uint64_t)High << 32) | (uint64_t)Low;
264 template <
unsigned N> constexpr
inline bool isInt(int64_t x) {
265 return N >= 64 || (-(INT64_C(1)<<(
N-1)) <= x && x < (INT64_C(1)<<(
N-1)));
268 template <> constexpr
inline bool isInt<8>(int64_t x) {
269 return static_cast<int8_t
>(x) == x;
271 template <> constexpr
inline bool isInt<16>(int64_t x) {
272 return static_cast<int16_t
>(x) == x;
274 template <> constexpr
inline bool isInt<32>(int64_t x) {
275 return static_cast<int32_t
>(x) == x;
280 template <
unsigned N,
unsigned S>
283 N > 0,
"isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
284 static_assert(
N + S <= 64,
"isShiftedInt<N, S> with N + S > 64 is too wide.");
285 return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
296 template <
unsigned N>
297 constexpr
inline typename std::enable_if<(N < 64), bool>::type
299 static_assert(
N > 0,
"isUInt<0> doesn't make sense");
300 return X < (UINT64_C(1) << (
N));
302 template <
unsigned N>
303 constexpr
inline typename std::enable_if<N >= 64,
bool>::type
309 template <> constexpr
inline bool isUInt<8>(uint64_t x) {
310 return static_cast<uint8_t
>(x) == x;
313 return static_cast<uint16_t
>(x) == x;
316 return static_cast<uint32_t>(x) == x;
320 template <
unsigned N,
unsigned S>
323 N > 0,
"isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
324 static_assert(
N + S <= 64,
325 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
328 return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
333 assert(N > 0 && N <= 64 &&
"integer width out of range");
339 return UINT64_MAX >> (64 -
N);
344 assert(N > 0 && N <= 64 &&
"integer width out of range");
346 return -(UINT64_C(1)<<(N-1));
351 assert(N > 0 && N <= 64 &&
"integer width out of range");
355 return (UINT64_C(1) << (N - 1)) - 1;
374 return Value && ((Value + 1) & Value) == 0;
381 return Value && ((Value + 1) & Value) == 0;
388 return Value &&
isMask_32((Value - 1) | Value);
394 return Value &&
isMask_64((Value - 1) | Value);
400 return Value && !(Value & (Value - 1));
406 return Value && !(Value & (Value - int64_t(1
L)));
435 template <
typename T>
437 static_assert(std::numeric_limits<T>::is_integer &&
438 !std::numeric_limits<T>::is_signed,
439 "Only unsigned integral types are allowed.");
451 template <
typename T>
453 static_assert(std::numeric_limits<T>::is_integer &&
454 !std::numeric_limits<T>::is_signed,
455 "Only unsigned integral types are allowed.");
463 static_assert(SizeOfT <= 4,
"Not implemented!");
465 return __builtin_popcount(Value);
468 v = v - ((v >> 1) & 0x55555555);
469 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
470 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
478 return __builtin_popcountll(Value);
481 v = v - ((v >> 1) & 0x5555555555555555ULL);
482 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
483 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
484 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
493 template <
typename T>
495 static_assert(std::numeric_limits<T>::is_integer &&
496 !std::numeric_limits<T>::is_signed,
497 "Only unsigned integral types are allowed.");
503 #if defined(__ANDROID_API__) && __ANDROID_API__ < 18
504 return __builtin_log(Value) / __builtin_log(2.0);
551 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
552 memcpy(&D, &Bits,
sizeof(Bits));
560 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
561 memcpy(&F, &Bits,
sizeof(Bits));
571 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
572 memcpy(&Bits, &Double,
sizeof(Double));
582 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
583 memcpy(&Bits, &Float,
sizeof(Float));
595 return (A | B) & (1 + ~(A |
B));
602 inline uintptr_t
alignAddr(
const void *Addr,
size_t Alignment) {
604 "Alignment is not a power of two!");
606 assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
608 return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
614 return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
664 inline uint64_t
alignTo(uint64_t
Value, uint64_t Align, uint64_t Skew = 0) {
665 assert(Align != 0u &&
"Align can't be 0.");
667 return (Value + Align - 1 - Skew) / Align * Align + Skew;
672 template <u
int64_t Align> constexpr
inline uint64_t
alignTo(uint64_t
Value) {
673 static_assert(Align != 0u,
"Align must be non-zero");
674 return (Value + Align - 1) / Align * Align;
681 template <u
int64_t Align>
683 static_assert(Align != 0u,
"Align must be non-zero");
684 template <u
int64_t Value>
686 static const uint64_t
value = (
Value + Align - 1) / Align * Align;
693 assert(Align != 0u &&
"Align can't be 0.");
695 return (Value - Skew) / Align * Align + Skew;
702 return alignTo(Value, Align) - Value;
708 static_assert(
B > 0,
"Bit width can't be 0.");
709 static_assert(
B <= 32,
"Bit width out of range.");
710 return int32_t(X << (32 -
B)) >> (32 -
B);
716 assert(B > 0 &&
"Bit width can't be 0.");
717 assert(B <= 32 &&
"Bit width out of range.");
718 return int32_t(X << (32 - B)) >> (32 -
B);
723 template <
unsigned B> constexpr
inline int64_t
SignExtend64(uint64_t x) {
724 static_assert(
B > 0,
"Bit width can't be 0.");
725 static_assert(
B <= 64,
"Bit width out of range.");
726 return int64_t(x << (64 -
B)) >> (64 -
B);
732 assert(B > 0 &&
"Bit width can't be 0.");
733 assert(B <= 64 &&
"Bit width out of range.");
734 return int64_t(X << (64 - B)) >> (64 -
B);
739 template <
typename T>
740 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
742 return std::max(X, Y) -
std::min(X, Y);
748 template <
typename T>
749 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
752 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
755 Overflowed = (Z < X || Z <
Y);
757 return std::numeric_limits<T>::max();
765 template <
typename T>
766 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
769 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
782 const T Max = std::numeric_limits<T>::max();
784 if (Log2Z < Log2Max) {
787 if (Log2Z > Log2Max) {
796 if (Z & ~(Max >> 1)) {
811 template <
typename T>
812 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
815 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
constexpr bool isUInt< 32 >(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...
constexpr uint32_t Lo_32(uint64_t Value)
Lo_32 - This function returns the low 32 bits of a 64 bit value.
static unsigned count(T Value)
float BitsToFloat(uint32_t Bits)
BitsToFloat - 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)
isMask_32 - This function returns true if the argument is a non-empty sequence of ones starting at th...
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.
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 GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
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.
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)
isMask_64 - This function returns true if the argument is a non-empty sequence of ones starting at th...
uint16_t ByteSwap_16(uint16_t Value)
ByteSwap_16 - This function returns a byte-swapped representation of the 16-bit argument, Value.
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 GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
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...
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
MinAlign - A and B are either alignments or offsets.
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)
isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted left by S...
constexpr bool isPowerOf2_32(uint32_t Value)
isPowerOf2_32 - This function returns true if the argument is a power of two > 0. ...
constexpr bool isInt(int64_t x)
isInt - 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)
isPowerOf2_64 - This function returns true if the argument is a power of two 0 (64 bit edition...
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...
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)
isUInt - Checks if an unsigned integer fits into the given bit width.
constexpr bool isInt< 32 >(int64_t x)
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...
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...
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)
isShiftedMask_64 - This function returns true if the argument contains a non-empty sequence of ones w...
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
uint64_t ByteSwap_64(uint64_t Value)
ByteSwap_64 - This function returns a byte-swapped representation of the 64-bit argument, Value.
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.
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)
Hi_32 - This function returns 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_64 - This functions makes a 64-bit integer from a high / low pair of 32-bit integers.
constexpr bool isShiftedMask_32(uint32_t Value)
isShiftedMask_32 - This function returns true if the argument contains a non-empty sequence of ones w...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
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.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
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...
static const uint64_t value
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.