Go to the documentation of this file.
13 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
14 #define LLVM_SUPPORT_MATHEXTRAS_H
23 #include <type_traits>
31 constexpr
double e = 2.7182818284590452354,
33 ln2 = .69314718055994530942,
34 ln10 = 2.3025850929940456840,
37 pi = 3.1415926535897932385,
45 phi = 1.6180339887498948482;
46 constexpr
float ef = 2.71828183F,
70 static_assert(std::is_unsigned_v<T>,
71 "Only unsigned integral types are allowed.");
82 static_assert(std::is_unsigned_v<T>,
83 "Only unsigned integral types are allowed.");
90 static_assert(std::is_unsigned<T>::value,
"Invalid type!");
91 const unsigned Bits = CHAR_BIT *
sizeof(
T);
93 return N == 0 ? 0 : (
T(-1) >> (
Bits -
N));
99 return ~maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
105 return maskLeadingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
111 return maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
118 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
119 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
120 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
129 #if __has_builtin(__builtin_bitreverse8)
130 if constexpr (std::is_same_v<T, uint8_t>)
131 return __builtin_bitreverse8(Val);
133 #if __has_builtin(__builtin_bitreverse16)
134 if constexpr (std::is_same_v<T, uint16_t>)
135 return __builtin_bitreverse16(Val);
137 #if __has_builtin(__builtin_bitreverse32)
138 if constexpr (std::is_same_v<T, uint32_t>)
139 return __builtin_bitreverse32(Val);
141 #if __has_builtin(__builtin_bitreverse64)
142 if constexpr (std::is_same_v<T, uint64_t>)
143 return __builtin_bitreverse64(Val);
146 unsigned char in[
sizeof(Val)];
147 unsigned char out[
sizeof(Val)];
149 for (
unsigned i = 0;
i <
sizeof(Val); ++
i)
175 template <
unsigned N> constexpr
inline bool isInt(int64_t
x) {
176 if constexpr (
N == 8)
177 return static_cast<int8_t
>(
x) ==
x;
178 if constexpr (
N == 16)
179 return static_cast<int16_t
>(
x) ==
x;
180 if constexpr (
N == 32)
181 return static_cast<int32_t
>(
x) ==
x;
182 if constexpr (
N < 64)
183 return -(INT64_C(1) << (
N - 1)) <=
x &&
x < (INT64_C(1) << (
N - 1));
189 template <
unsigned N,
unsigned S>
192 N > 0,
"isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
193 static_assert(
N +
S <= 64,
"isShiftedInt<N, S> with N + S > 64 is too wide.");
194 return isInt<N + S>(
x) && (
x % (UINT64_C(1) <<
S) == 0);
199 static_assert(
N > 0,
"isUInt<0> doesn't make sense");
200 if constexpr (
N == 8)
201 return static_cast<uint8_t
>(
x) ==
x;
202 if constexpr (
N == 16)
204 if constexpr (
N == 32)
206 if constexpr (
N < 64)
207 return x < (UINT64_C(1) << (
N));
213 template <
unsigned N,
unsigned S>
216 N > 0,
"isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
217 static_assert(
N +
S <= 64,
218 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
221 return isUInt<N + S>(
x) && (
x % (UINT64_C(1) <<
S) == 0);
226 assert(
N > 0 &&
N <= 64 &&
"integer width out of range");
237 assert(
N > 0 &&
N <= 64 &&
"integer width out of range");
239 return UINT64_C(1) + ~(UINT64_C(1) << (
N - 1));
244 assert(
N > 0 &&
N <= 64 &&
"integer width out of range");
248 return (UINT64_C(1) << (
N - 1)) - 1;
305 static_assert(std::is_unsigned_v<T>,
306 "Only unsigned integral types are allowed.");
307 return llvm::countl_one<T>(
Value);
318 static_assert(std::is_unsigned_v<T>,
319 "Only unsigned integral types are allowed.");
320 return llvm::countr_one<T>(
Value);
326 template <
typename T>
328 static_assert(std::is_unsigned_v<T>,
329 "Only unsigned integral types are allowed.");
362 template <
size_t kValue> constexpr
inline size_t CTLog2() {
364 "Value is not a valid power of 2");
365 return 1 +
CTLog2<kValue / 2>();
368 template <> constexpr
inline size_t CTLog2<1>() {
return 0; }
398 static_assert(
sizeof(
uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
399 return llvm::bit_cast<double>(
Bits);
404 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
405 return llvm::bit_cast<float>(
Bits);
412 static_assert(
sizeof(
uint64_t) ==
sizeof(
double),
"Unexpected type sizes");
413 return llvm::bit_cast<uint64_t>(Double);
420 static_assert(
sizeof(
uint32_t) ==
sizeof(
float),
"Unexpected type sizes");
421 return llvm::bit_cast<uint32_t>(Float);
432 return (A |
B) & (1 + ~(A |
B));
478 "Align must be a power of 2");
503 static_assert(
Align != 0u,
"Align must be non-zero");
509 return alignTo(Numerator, Denominator) / Denominator;
514 return (Numerator + (Denominator / 2)) / Denominator;
528 static_assert(
B > 0,
"Bit width can't be 0.");
529 static_assert(
B <= 32,
"Bit width out of range.");
530 return int32_t(
X << (32 -
B)) >> (32 -
B);
536 assert(
B > 0 &&
"Bit width can't be 0.");
537 assert(
B <= 32 &&
"Bit width out of range.");
538 return int32_t(
X << (32 -
B)) >> (32 -
B);
544 static_assert(
B > 0,
"Bit width can't be 0.");
545 static_assert(
B <= 64,
"Bit width out of range.");
546 return int64_t(
x << (64 -
B)) >> (64 -
B);
552 assert(
B > 0 &&
"Bit width can't be 0.");
553 assert(
B <= 64 &&
"Bit width out of range.");
554 return int64_t(
X << (64 -
B)) >> (64 -
B);
559 template <
typename T>
561 return X >
Y ? (
X -
Y) : (
Y -
X);
567 template <
typename T>
568 std::enable_if_t<std::is_unsigned<T>::value,
T>
571 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
574 Overflowed = (
Z <
X ||
Z <
Y);
583 template <
class T,
class... Ts>
586 bool Overflowed =
false;
596 template <
typename T>
597 std::enable_if_t<std::is_unsigned<T>::value,
T>
600 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
615 if (Log2Z < Log2Max) {
618 if (Log2Z > Log2Max) {
627 if (
Z & ~(Max >> 1)) {
642 template <
typename T>
643 std::enable_if_t<std::is_unsigned<T>::value,
T>
646 bool &Overflowed = ResultOverflowed ? *ResultOverflowed :
Dummy;
661 template <
typename T>
663 #if __has_builtin(__builtin_add_overflow)
664 return __builtin_add_overflow(
X,
Y, &Result);
667 using U = std::make_unsigned_t<T>;
668 const U UX =
static_cast<U
>(
X);
669 const U UY =
static_cast<U
>(
Y);
670 const U UResult = UX + UY;
673 Result =
static_cast<T>(UResult);
687 template <
typename T>
689 #if __has_builtin(__builtin_sub_overflow)
690 return __builtin_sub_overflow(
X,
Y, &Result);
693 using U = std::make_unsigned_t<T>;
694 const U UX =
static_cast<U
>(
X);
695 const U UY =
static_cast<U
>(
Y);
696 const U UResult = UX - UY;
699 Result =
static_cast<T>(UResult);
713 template <
typename T>
716 using U = std::make_unsigned_t<T>;
717 const U UX =
X < 0 ? (0 -
static_cast<U
>(
X)) :
static_cast<U
>(
X);
718 const U UY =
Y < 0 ? (0 -
static_cast<U
>(
Y)) :
static_cast<U
>(
Y);
719 const U UResult = UX * UY;
722 const bool IsNegative = (
X < 0) ^ (
Y < 0);
723 Result = IsNegative ? (0 - UResult) : UResult;
726 if (UX == 0 || UY == 0)
std::enable_if_t< std::is_unsigned< T >::value, T > SaturatingMultiply(T X, T Y, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, of type T.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
constexpr size_t CTLog2< 1 >()
This is an optimization pass for GlobalISel generic memory operations.
std::enable_if_t< std::is_unsigned< T >::value, T > 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.
T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
T reverseBits(T Val)
Reverse the bits in Val.
constexpr size_t CTLog2()
Compile time Log2.
const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
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 bool has_single_bit(T Value) noexcept
uint32_t FloatToBits(float Float)
This function takes a float and returns the bit equivalent 32-bit integer.
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
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.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
constexpr double inv_sqrt2
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
int popcount(T Value) noexcept
Count the number of set bits in a value.
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...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
uint64_t DoubleToBits(double Double)
This function takes a double and returns the bit equivalent 64-bit integer.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
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.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
The object format emitted by the WebAssembly backed is documented in
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
std::enable_if_t< std::is_signed< T >::value, T > AddOverflow(T X, T Y, T &Result)
Add two signed integers, computing the two's complement truncated result, returning true if overflow ...
constexpr float inv_sqrt3f
unsigned countTrailingZeros(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
This struct is a compact representation of a valid (non-zero power of two) alignment.
constexpr float inv_sqrt2f
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit 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.
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).
unsigned countPopulation(T Value)
Count the number of set bits in a value.
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align)
constexpr double inv_sqrt3
std::enable_if_t< std::is_unsigned< T >::value, T > 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.
T maskLeadingZeros(unsigned N)
Create a bitmask with the N left-most bits set to 0, and all other bits set to 1.
unsigned Log2_64_Ceil(uint64_t Value)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
SSE has instructions for doing operations on complex numbers
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
constexpr double inv_sqrtpi
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...
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
float BitsToFloat(uint32_t Bits)
This function takes a 32-bit integer and returns the bit equivalent float.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
unsigned countLeadingOnes(T Value)
Count the number of ones from the most significant bit to the first zero bit.
constexpr float inv_sqrtpif
T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0.
unsigned countTrailingOnes(T Value)
Count the number of ones from the least significant bit to the first zero bit.
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
std::enable_if_t< std::is_signed< T >::value, T > MulOverflow(T X, T Y, T &Result)
Multiply two signed integers, computing the two's complement truncated result, returning true if an o...
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
std::enable_if_t< std::is_signed< T >::value, T > SubOverflow(T X, T Y, T &Result)
Subtract two signed integers, computing the two's complement truncated result, returning true if an o...
double BitsToDouble(uint64_t Bits)
This function takes a 64-bit integer and returns the bit equivalent double.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
unsigned countLeadingZeros(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
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...
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator)
Returns the integer nearest(Numerator / Denominator).
LLVM Value Representation.
std::enable_if_t< std::is_unsigned< T >::value, T > AbsoluteDifference(T X, T Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result.