13#ifndef LLVM_SUPPORT_MATHEXTRAS_H
14#define LLVM_SUPPORT_MATHEXTRAS_H
31template <
typename T,
typename U>
33 std::enable_if_t<std::is_integral_v<T> && std::is_integral_v<U>>;
36template <
typename T,
typename U,
typename = enableif_
int<T, U>>
38 std::common_type_t<std::make_unsigned_t<T>, std::make_unsigned_t<U>>;
39template <
typename T,
typename U,
typename = enableif_
int<T, U>>
41 std::common_type_t<std::make_signed_t<T>, std::make_signed_t<U>>;
47constexpr double e = 2.7182818284590452354,
49 ln2 = .69314718055994530942,
50 ln10 = 2.3025850929940456840,
53 pi = 3.1415926535897932385,
61 phi = 1.6180339887498948482;
62constexpr float ef = 2.71828183F,
82 static_assert(std::is_unsigned_v<T>,
"Invalid type!");
83 const unsigned Bits = CHAR_BIT *
sizeof(
T);
84 assert(
N <= Bits &&
"Invalid bit index");
87 return T(-1) >> (Bits -
N);
93 return ~maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
99 return maskLeadingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
105 return maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
112#define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
113#define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
114#define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
123#if __has_builtin(__builtin_bitreverse8)
124 if constexpr (std::is_same_v<T, uint8_t>)
125 return __builtin_bitreverse8(Val);
127#if __has_builtin(__builtin_bitreverse16)
128 if constexpr (std::is_same_v<T, uint16_t>)
129 return __builtin_bitreverse16(Val);
131#if __has_builtin(__builtin_bitreverse32)
132 if constexpr (std::is_same_v<T, uint32_t>)
133 return __builtin_bitreverse32(Val);
135#if __has_builtin(__builtin_bitreverse64)
136 if constexpr (std::is_same_v<T, uint64_t>)
137 return __builtin_bitreverse64(Val);
140 unsigned char in[
sizeof(Val)];
141 unsigned char out[
sizeof(Val)];
142 std::memcpy(in, &Val,
sizeof(Val));
143 for (
unsigned i = 0; i <
sizeof(Val); ++i)
145 std::memcpy(&Val, out,
sizeof(Val));
169template <
unsigned N>
constexpr bool isInt(int64_t x) {
170 if constexpr (
N == 0)
172 if constexpr (
N == 8)
173 return static_cast<int8_t
>(x) == x;
174 if constexpr (
N == 16)
175 return static_cast<int16_t
>(x) == x;
176 if constexpr (
N == 32)
177 return static_cast<int32_t
>(x) == x;
178 if constexpr (
N < 64)
179 return -(INT64_C(1) << (
N - 1)) <= x && x < (INT64_C(1) << (
N - 1));
185template <
unsigned N,
unsigned S>
187 static_assert(S < 64,
"isShiftedInt<N, S> with S >= 64 is too much.");
188 static_assert(
N + S <= 64,
"isShiftedInt<N, S> with N + S > 64 is too wide.");
189 return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
194 if constexpr (
N == 0)
196 if constexpr (
N == 8)
197 return static_cast<uint8_t>(x) == x;
198 if constexpr (
N == 16)
199 return static_cast<uint16_t>(x) == x;
200 if constexpr (
N == 32)
201 return static_cast<uint32_t>(x) == x;
202 if constexpr (
N < 64)
203 return x < (UINT64_C(1) << (
N));
209template <
unsigned N,
unsigned S>
211 static_assert(S < 64,
"isShiftedUInt<N, S> with S >= 64 is too much.");
212 static_assert(
N + S <= 64,
213 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
215 return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
220 assert(
N <= 64 &&
"integer width out of range");
236 assert(
N <= 64 &&
"integer width out of range");
240 return UINT64_C(1) + ~(UINT64_C(1) << (
N - 1));
245 assert(
N <= 64 &&
"integer width out of range");
251 return (UINT64_C(1) << (
N - 1)) - 1;
329template <
size_t kValue>
constexpr size_t CTLog2() {
331 "Value is not a valid power of 2");
332 return 1 +
CTLog2<kValue / 2>();
365template <
typename U,
typename V,
typename T = common_u
int<U, V>>
372 return (
A |
B) & (1 + ~(
A |
B));
377 return (
A |
B) & (1 + ~(
A |
B));
402template <
typename U,
typename V,
typename T = common_u
int<U, V>>
404 assert(Denominator &&
"Division by zero");
405 T Bias = (Numerator != 0);
406 return (Numerator - Bias) / Denominator + Bias;
411 assert(Denominator &&
"Division by zero");
413 return (Numerator - Bias) / Denominator + Bias;
418template <
typename U,
typename V>
420 return Numerator == std::numeric_limits<U>::min() && Denominator == -1;
425template <
typename U,
typename V,
typename T = common_s
int<U, V>>
427 assert(Denominator &&
"Division by zero");
429 "Divide would overflow");
433 T Bias = Denominator >= 0 ? 1 : -1;
434 bool SameSign = (Numerator >= 0) == (Denominator >= 0);
435 return SameSign ? (Numerator - Bias) / Denominator + 1
436 : Numerator / Denominator;
441template <
typename U,
typename V,
typename T = common_s
int<U, V>>
443 assert(Denominator &&
"Division by zero");
445 "Divide would overflow");
449 T Bias = Denominator >= 0 ? -1 : 1;
450 bool SameSign = (Numerator >= 0) == (Denominator >= 0);
451 return SameSign ? Numerator / Denominator
452 : (Numerator - Bias) / Denominator - 1;
457template <
typename U,
typename V,
typename T = common_s
int<U, V>>
458constexpr T mod(U Numerator, V Denominator) {
459 assert(Denominator >= 1 &&
"Mod by non-positive number");
460 T Mod = Numerator % Denominator;
461 return Mod < 0 ?
Mod + Denominator :
Mod;
466template <
typename U,
typename V,
typename T = common_u
int<U, V>>
468 assert(Denominator &&
"Division by zero");
469 T Mod = Numerator % Denominator;
470 return (Numerator / Denominator) +
471 (
Mod > (
static_cast<T>(Denominator) - 1) / 2);
486template <
typename U,
typename V,
typename T = common_u
int<U, V>>
490 return CeilDiv *
Align;
497 return CeilDiv *
Align;
501template <
typename U,
typename V,
typename T = common_u
int<U, V>>
504 "Align must be a power of 2");
505 T NegAlign =
static_cast<T>(0) -
Align;
512 "Align must be a power of 2");
531template <
typename U,
typename V,
typename W,
532 typename T = common_uint<common_uint<U, V>, W>>
543template <auto Align,
typename V,
typename T = common_u
int<decltype(Align), V>>
545 static_assert(
Align != 0u,
"Align must be non-zero");
547 return CeilDiv *
Align;
553template <
typename U,
typename V,
typename W =
uint8_t,
554 typename T = common_uint<common_uint<U, V>, W>>
564 static_assert(
B <= 32,
"Bit width out of range.");
565 if constexpr (
B == 0)
567 return int32_t(
X << (32 -
B)) >> (32 -
B);
573 assert(
B <= 32 &&
"Bit width out of range.");
576 return int32_t(
X << (32 -
B)) >> (32 -
B);
582 static_assert(
B <= 64,
"Bit width out of range.");
583 if constexpr (
B == 0)
585 return int64_t(x << (64 -
B)) >> (64 -
B);
591 assert(
B <= 64 &&
"Bit width out of range.");
594 return int64_t(
X << (64 -
B)) >> (64 -
B);
599template <
typename U,
typename V,
typename T = common_u
int<U, V>>
601 return X >
Y ? (
X -
Y) : (
Y -
X);
608std::enable_if_t<std::is_unsigned_v<T>,
T>
611 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
614 Overflowed = (Z <
X || Z <
Y);
616 return std::numeric_limits<T>::max();
623template <
class T,
class... Ts>
626 bool Overflowed =
false;
629 return SaturatingAdd(std::numeric_limits<T>::max(),
T(1), Args...);
637std::enable_if_t<std::is_unsigned_v<T>,
T>
640 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
653 const T Max = std::numeric_limits<T>::max();
655 if (Log2Z < Log2Max) {
658 if (Log2Z > Log2Max) {
667 if (Z & ~(Max >> 1)) {
683std::enable_if_t<std::is_unsigned_v<T>,
T>
686 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
702#if __has_builtin(__builtin_add_overflow)
703 return __builtin_add_overflow(
X,
Y, &Result);
706 using U = std::make_unsigned_t<T>;
707 const U UX =
static_cast<U
>(
X);
708 const U UY =
static_cast<U
>(
Y);
709 const U UResult = UX + UY;
712 Result =
static_cast<T>(UResult);
728#if __has_builtin(__builtin_sub_overflow)
729 return __builtin_sub_overflow(
X,
Y, &Result);
732 using U = std::make_unsigned_t<T>;
733 const U UX =
static_cast<U
>(
X);
734 const U UY =
static_cast<U
>(
Y);
735 const U UResult = UX - UY;
738 Result =
static_cast<T>(UResult);
754#if __has_builtin(__builtin_mul_overflow)
755 return __builtin_mul_overflow(
X,
Y, &Result);
758 using U = std::make_unsigned_t<T>;
759 const U UX =
X < 0 ? (0 -
static_cast<U
>(
X)) :
static_cast<U
>(
X);
760 const U UY =
Y < 0 ? (0 -
static_cast<U
>(
Y)) :
static_cast<U
>(
Y);
761 const U UResult = UX * UY;
764 const bool IsNegative = (
X < 0) ^ (
Y < 0);
765 Result = IsNegative ? (0 - UResult) : UResult;
768 if (UX == 0 || UY == 0)
775 return UX > (
static_cast<U
>(std::numeric_limits<T>::max()) + U(1)) / UY;
777 return UX > (
static_cast<U
>(std::numeric_limits<T>::max())) / UY;
783#if defined(__i386__) || defined(_M_IX86)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements the C++20 <bit> header.
LLVM Value Representation.
constexpr float inv_sqrtpif
constexpr double inv_sqrt2
constexpr double inv_sqrt3
constexpr double inv_sqrtpi
constexpr float inv_sqrt2f
constexpr float inv_sqrt3f
This is an optimization pass for GlobalISel generic memory operations.
std::common_type_t< std::make_unsigned_t< T >, std::make_unsigned_t< U > > common_uint
float stack_float_t
Type to force float point values onto the stack, so that x86 doesn't add hidden precision,...
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
std::enable_if_t< std::is_signed_v< T >, T > MulOverflow(T X, T Y, T &Result)
Multiply two signed integers, computing the two's complement truncated result, returning true if an o...
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
constexpr bool divideSignedWouldOverflow(U Numerator, V Denominator)
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt mod(const DynamicAPInt &LHS, const DynamicAPInt &RHS)
is always non-negative.
int popcount(T Value) noexcept
Count the number of set bits in a value.
constexpr size_t CTLog2()
Compile time Log2.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
constexpr size_t CTLog2< 1 >()
unsigned Log2_64_Ceil(uint64_t Value)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
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...
constexpr T divideFloorSigned(U Numerator, V Denominator)
Returns the integer floor(Numerator / Denominator).
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
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...
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.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
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...
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
constexpr T divideNearest(U Numerator, V Denominator)
Returns (Numerator / Denominator) rounded by round-half-up.
constexpr bool has_single_bit(T Value) noexcept
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
T maskLeadingZeros(unsigned N)
Create a bitmask with the N left-most bits set to 0, and all other bits set to 1.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
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.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
std::common_type_t< std::make_signed_t< T >, std::make_signed_t< U > > common_sint
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
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...
std::enable_if_t< std::is_unsigned_v< T >, 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.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
constexpr T divideCeilSigned(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
@ Mod
The access may modify the value stored in memory.
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingMultiply(T X, T Y, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, of type T.
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
constexpr T AbsoluteDifference(U X, V Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result.
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
std::enable_if_t< std::is_signed_v< T >, T > AddOverflow(T X, T Y, T &Result)
Add two signed integers, computing the two's complement truncated result, returning true if overflow ...
std::enable_if_t< std::is_signed_v< T >, T > SubOverflow(T X, T Y, T &Result)
Subtract two signed integers, computing the two's complement truncated result, returning true if an o...
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
T reverseBits(T Val)
Reverse the bits in Val.
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
std::enable_if_t< std::is_integral_v< T > &&std::is_integral_v< U > > enableif_int
Some template parameter helpers to optimize for bitwidth, for functions that take multiple arguments.
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
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.
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
This struct is a compact representation of a valid (non-zero power of two) alignment.