14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
24 #include <type_traits>
30 #ifdef __ANDROID_NDK__
31 #include <android/api-level.h>
49 return std::numeric_limits<T>::digits;
54 std::size_t ZeroBits = 0;
55 T Shift = std::numeric_limits<T>::digits >> 1;
58 if ((Val & Mask) == 0) {
69 #if __GNUC__ >= 4 || defined(_MSC_VER)
70 template <
typename T>
struct TrailingZerosCounter<
T, 4> {
75 #if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0)
76 return __builtin_ctz(Val);
77 #elif defined(_MSC_VER)
79 _BitScanForward(&Index, Val);
85 #if !defined(_MSC_VER) || defined(_M_X64)
86 template <
typename T>
struct TrailingZerosCounter<
T, 8> {
91 #if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0)
92 return __builtin_ctzll(Val);
93 #elif defined(_MSC_VER)
95 _BitScanForward64(&Index, Val);
111 template <
typename T>
113 static_assert(std::numeric_limits<T>::is_integer &&
114 !std::numeric_limits<T>::is_signed,
115 "Only unsigned integral types are allowed.");
123 return std::numeric_limits<T>::digits;
126 std::size_t ZeroBits = 0;
127 for (
T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
128 T Tmp = Val >> Shift;
138 #if __GNUC__ >= 4 || defined(_MSC_VER)
139 template <
typename T>
struct LeadingZerosCounter<
T, 4> {
144 #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
145 return __builtin_clz(Val);
146 #elif defined(_MSC_VER)
148 _BitScanReverse(&Index, Val);
154 #if !defined(_MSC_VER) || defined(_M_X64)
155 template <
typename T>
struct LeadingZerosCounter<
T, 8> {
160 #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
161 return __builtin_clzll(Val);
162 #elif defined(_MSC_VER)
164 _BitScanReverse64(&Index, Val);
180 template <
typename T>
182 static_assert(std::numeric_limits<T>::is_integer &&
183 !std::numeric_limits<T>::is_signed,
184 "Only unsigned integral types are allowed.");
196 if (ZB ==
ZB_Max && Val == 0)
205 static_assert(std::is_unsigned<T>::value,
"Invalid type!");
206 const unsigned Bits = CHAR_BIT *
sizeof(
T);
207 assert(N <= Bits &&
"Invalid bit index");
208 return N == 0 ? 0 : (
T(-1) >> (Bits -
N));
214 return ~maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
220 return maskLeadingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
226 return maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) - N);
237 if (ZB ==
ZB_Max && Val == 0)
243 (std::numeric_limits<T>::digits - 1);
250 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
251 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
252 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
260 template <
typename T>
262 unsigned char in[
sizeof(Val)];
263 unsigned char out[
sizeof(Val)];
264 std::memcpy(in, &Val,
sizeof(Val));
265 for (
unsigned i = 0; i <
sizeof(Val); ++i)
267 std::memcpy(&Val, out,
sizeof(Val));
277 return static_cast<uint32_t>(Value >> 32);
282 return static_cast<uint32_t>(Value);
287 return ((uint64_t)High << 32) | (uint64_t)Low;
291 template <
unsigned N> constexpr
inline bool isInt(int64_t x) {
292 return N >= 64 || (-(INT64_C(1)<<(
N-1)) <= x && x < (INT64_C(1)<<(
N-1)));
295 template <> constexpr
inline bool isInt<8>(int64_t x) {
296 return static_cast<int8_t
>(x) == x;
298 template <> constexpr
inline bool isInt<16>(int64_t x) {
299 return static_cast<int16_t
>(x) == x;
301 template <> constexpr
inline bool isInt<32>(int64_t x) {
302 return static_cast<int32_t
>(x) == x;
306 template <
unsigned N,
unsigned S>
309 N > 0,
"isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
310 static_assert(
N + S <= 64,
"isShiftedInt<N, S> with N + S > 64 is too wide.");
311 return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
322 template <
unsigned N>
323 constexpr
inline typename std::enable_if<(N < 64), bool>::type
325 static_assert(
N > 0,
"isUInt<0> doesn't make sense");
326 return X < (UINT64_C(1) << (
N));
328 template <
unsigned N>
329 constexpr
inline typename std::enable_if<N >= 64,
bool>::type
335 template <> constexpr
inline bool isUInt<8>(uint64_t x) {
336 return static_cast<uint8_t
>(x) == x;
339 return static_cast<uint16_t
>(x) == x;
342 return static_cast<uint32_t>(x) == x;
346 template <
unsigned N,
unsigned S>
349 N > 0,
"isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
350 static_assert(
N + S <= 64,
351 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
354 return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
359 assert(N > 0 && N <= 64 &&
"integer width out of range");
365 return UINT64_MAX >> (64 -
N);
370 assert(N > 0 && N <= 64 &&
"integer width out of range");
372 return -(UINT64_C(1)<<(N-1));
377 assert(N > 0 && N <= 64 &&
"integer width out of range");
381 return (UINT64_C(1) << (N - 1)) - 1;
398 return Value && ((Value + 1) & Value) == 0;
404 return Value && ((Value + 1) & Value) == 0;
410 return Value &&
isMask_32((Value - 1) | Value);
416 return Value &&
isMask_64((Value - 1) | Value);
422 return Value && !(Value & (Value - 1));
427 return Value && !(Value & (Value - int64_t(1
L)));
453 template <
typename T>
455 static_assert(std::numeric_limits<T>::is_integer &&
456 !std::numeric_limits<T>::is_signed,
457 "Only unsigned integral types are allowed.");
469 template <
typename T>
471 static_assert(std::numeric_limits<T>::is_integer &&
472 !std::numeric_limits<T>::is_signed,
473 "Only unsigned integral types are allowed.");
481 static_assert(SizeOfT <= 4,
"Not implemented!");
483 return __builtin_popcount(Value);
486 v = v - ((v >> 1) & 0x55555555);
487 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
488 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
496 return __builtin_popcountll(Value);
499 v = v - ((v >> 1) & 0x5555555555555555ULL);
500 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
501 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
502 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
511 template <
typename T>
513 static_assert(std::numeric_limits<T>::is_integer &&
514 !std::numeric_limits<T>::is_signed,
515 "Only unsigned integral types are allowed.");
521 #if defined(__ANDROID_API__) && __ANDROID_API__ < 18
522 return __builtin_log(Value) / __builtin_log(2.0);
567 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
568 memcpy(&D, &Bits,
sizeof(Bits));
575 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
576 memcpy(&F, &Bits,
sizeof(Bits));
585 static_assert(
sizeof(uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
586 memcpy(&Bits, &Double,
sizeof(Double));
595 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
596 memcpy(&Bits, &Float,
sizeof(Float));
608 return (A | B) & (1 + ~(A |
B));
615 inline uintptr_t
alignAddr(
const void *Addr,
size_t Alignment) {
617 "Alignment is not a power of two!");
619 assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
621 return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
627 return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
678 assert(Align != 0u &&
"Align can't be 0.");
680 return (Value + Align - 1 - Skew) / Align * Align + Skew;
685 template <u
int64_t Align> constexpr
inline uint64_t
alignTo(uint64_t
Value) {
686 static_assert(
Align != 0u,
"Align must be non-zero");
694 template <u
int64_t Align>
696 static_assert(
Align != 0u,
"Align must be non-zero");
697 template <u
int64_t Value>
706 assert(Align != 0u &&
"Align can't be 0.");
708 return (Value - Skew) / Align * Align + Skew;
715 return alignTo(Value, Align) - Value;
721 static_assert(
B > 0,
"Bit width can't be 0.");
722 static_assert(
B <= 32,
"Bit width out of range.");
723 return int32_t(X << (32 -
B)) >> (32 -
B);
729 assert(B > 0 &&
"Bit width can't be 0.");
730 assert(B <= 32 &&
"Bit width out of range.");
731 return int32_t(X << (32 - B)) >> (32 -
B);
736 template <
unsigned B> constexpr
inline int64_t
SignExtend64(uint64_t x) {
737 static_assert(
B > 0,
"Bit width can't be 0.");
738 static_assert(
B <= 64,
"Bit width out of range.");
739 return int64_t(x << (64 -
B)) >> (64 -
B);
745 assert(B > 0 &&
"Bit width can't be 0.");
746 assert(B <= 64 &&
"Bit width out of range.");
747 return int64_t(X << (64 - B)) >> (64 -
B);
752 template <
typename T>
753 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
761 template <
typename T>
762 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
765 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
768 Overflowed = (Z < X || Z <
Y);
778 template <
typename T>
779 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
782 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
797 if (Log2Z < Log2Max) {
800 if (Log2Z > Log2Max) {
809 if (Z & ~(Max >> 1)) {
824 template <
typename T>
825 typename std::enable_if<std::is_unsigned<T>::value,
T>::type
828 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.
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.
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 tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
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.
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.
zlib-gnu style compression
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.
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.)
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)
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.
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 char Align[]
Key for Kernel::Arg::Metadata::mAlign.
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)
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...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
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.
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.
T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0. ...