13#ifndef LLVM_SUPPORT_MATHEXTRAS_H
14#define LLVM_SUPPORT_MATHEXTRAS_H
31constexpr double e = 2.7182818284590452354,
33 ln2 = .69314718055994530942,
34 ln10 = 2.3025850929940456840,
37 pi = 3.1415926535897932385,
45 phi = 1.6180339887498948482;
46constexpr float ef = 2.71828183F,
72 static_assert(std::is_unsigned_v<T>,
73 "Only unsigned integral types are allowed.");
86 static_assert(std::is_unsigned_v<T>,
87 "Only unsigned integral types are allowed.");
94 static_assert(std::is_unsigned_v<T>,
"Invalid type!");
95 const unsigned Bits = CHAR_BIT *
sizeof(
T);
96 assert(
N <= Bits &&
"Invalid bit index");
97 return N == 0 ? 0 : (
T(-1) >> (Bits -
N));
103 return ~maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
109 return maskLeadingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
115 return maskTrailingOnes<T>(CHAR_BIT *
sizeof(
T) -
N);
122#define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
123#define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
124#define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
133#if __has_builtin(__builtin_bitreverse8)
134 if constexpr (std::is_same_v<T, uint8_t>)
135 return __builtin_bitreverse8(Val);
137#if __has_builtin(__builtin_bitreverse16)
138 if constexpr (std::is_same_v<T, uint16_t>)
139 return __builtin_bitreverse16(Val);
141#if __has_builtin(__builtin_bitreverse32)
142 if constexpr (std::is_same_v<T, uint32_t>)
143 return __builtin_bitreverse32(Val);
145#if __has_builtin(__builtin_bitreverse64)
146 if constexpr (std::is_same_v<T, uint64_t>)
147 return __builtin_bitreverse64(Val);
150 unsigned char in[
sizeof(Val)];
151 unsigned char out[
sizeof(Val)];
152 std::memcpy(in, &Val,
sizeof(Val));
153 for (
unsigned i = 0; i <
sizeof(Val); ++i)
155 std::memcpy(&Val, out,
sizeof(Val));
179template <
unsigned N>
constexpr inline bool isInt(int64_t x) {
180 if constexpr (
N == 8)
181 return static_cast<int8_t
>(x) == x;
182 if constexpr (
N == 16)
183 return static_cast<int16_t
>(x) == x;
184 if constexpr (
N == 32)
185 return static_cast<int32_t
>(x) == x;
186 if constexpr (
N < 64)
187 return -(INT64_C(1) << (
N - 1)) <= x && x < (INT64_C(1) << (
N - 1));
193template <
unsigned N,
unsigned S>
196 N > 0,
"isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
197 static_assert(
N + S <= 64,
"isShiftedInt<N, S> with N + S > 64 is too wide.");
198 return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
203 static_assert(
N > 0,
"isUInt<0> doesn't make sense");
204 if constexpr (
N == 8)
205 return static_cast<uint8_t
>(x) == x;
206 if constexpr (
N == 16)
207 return static_cast<uint16_t>(x) == x;
208 if constexpr (
N == 32)
209 return static_cast<uint32_t>(x) == x;
210 if constexpr (
N < 64)
211 return x < (UINT64_C(1) << (
N));
217template <
unsigned N,
unsigned S>
220 N > 0,
"isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
221 static_assert(
N + S <= 64,
222 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
225 return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
230 assert(
N > 0 &&
N <= 64 &&
"integer width out of range");
241 assert(
N > 0 &&
N <= 64 &&
"integer width out of range");
243 return UINT64_C(1) + ~(UINT64_C(1) << (
N - 1));
248 assert(
N > 0 &&
N <= 64 &&
"integer width out of range");
252 return (UINT64_C(1) << (
N - 1)) - 1;
311 static_assert(std::is_unsigned_v<T>,
312 "Only unsigned integral types are allowed.");
313 return llvm::countl_one<T>(
Value);
326 static_assert(std::is_unsigned_v<T>,
327 "Only unsigned integral types are allowed.");
328 return llvm::countr_one<T>(
Value);
337 static_assert(std::is_unsigned_v<T>,
338 "Only unsigned integral types are allowed.");
371template <
size_t kValue>
constexpr inline size_t CTLog2() {
373 "Value is not a valid power of 2");
374 return 1 +
CTLog2<kValue / 2>();
377template <>
constexpr inline size_t CTLog2<1>() {
return 0; }
413 return (
A |
B) & (1 + ~(
A |
B));
453 "Align must be a power of 2");
478 static_assert(
Align != 0u,
"Align must be non-zero");
484 return alignTo(Numerator, Denominator) / Denominator;
489 return (Numerator + (Denominator / 2)) / Denominator;
503 static_assert(
B > 0,
"Bit width can't be 0.");
504 static_assert(
B <= 32,
"Bit width out of range.");
505 return int32_t(
X << (32 -
B)) >> (32 -
B);
511 assert(
B > 0 &&
"Bit width can't be 0.");
512 assert(
B <= 32 &&
"Bit width out of range.");
513 return int32_t(
X << (32 -
B)) >> (32 -
B);
519 static_assert(
B > 0,
"Bit width can't be 0.");
520 static_assert(
B <= 64,
"Bit width out of range.");
521 return int64_t(x << (64 -
B)) >> (64 -
B);
527 assert(
B > 0 &&
"Bit width can't be 0.");
528 assert(
B <= 64 &&
"Bit width out of range.");
529 return int64_t(
X << (64 -
B)) >> (64 -
B);
536 return X >
Y ? (
X -
Y) : (
Y -
X);
543std::enable_if_t<std::is_unsigned_v<T>,
T>
546 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
549 Overflowed = (Z <
X || Z <
Y);
551 return std::numeric_limits<T>::max();
558template <
class T,
class... Ts>
561 bool Overflowed =
false;
564 return SaturatingAdd(std::numeric_limits<T>::max(),
T(1), Args...);
572std::enable_if_t<std::is_unsigned_v<T>,
T>
575 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
588 const T Max = std::numeric_limits<T>::max();
590 if (Log2Z < Log2Max) {
593 if (Log2Z > Log2Max) {
602 if (Z & ~(Max >> 1)) {
618std::enable_if_t<std::is_unsigned_v<T>,
T>
621 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
638#if __has_builtin(__builtin_add_overflow)
639 return __builtin_add_overflow(
X,
Y, &Result);
642 using U = std::make_unsigned_t<T>;
643 const U UX =
static_cast<U
>(
X);
644 const U UY =
static_cast<U
>(
Y);
645 const U UResult = UX + UY;
648 Result =
static_cast<T>(UResult);
664#if __has_builtin(__builtin_sub_overflow)
665 return __builtin_sub_overflow(
X,
Y, &Result);
668 using U = std::make_unsigned_t<T>;
669 const U UX =
static_cast<U
>(
X);
670 const U UY =
static_cast<U
>(
Y);
671 const U UResult = UX - UY;
674 Result =
static_cast<T>(UResult);
691 using U = std::make_unsigned_t<T>;
692 const U UX =
X < 0 ? (0 -
static_cast<U
>(
X)) :
static_cast<U
>(
X);
693 const U UY =
Y < 0 ? (0 -
static_cast<U
>(
Y)) :
static_cast<U
>(
Y);
694 const U UResult = UX * UY;
697 const bool IsNegative = (
X < 0) ^ (
Y < 0);
698 Result = IsNegative ? (0 - UResult) : UResult;
701 if (UX == 0 || UY == 0)
708 return UX > (
static_cast<U
>(std::numeric_limits<T>::max()) + U(1)) / UY;
710 return UX > (
static_cast<U
>(std::numeric_limits<T>::max())) / UY;
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DEPRECATED(MSG, FIX)
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.
@ 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.
uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align)
int popcount(T Value) noexcept
Count the number of set bits in a value.
constexpr size_t CTLog2()
Compile time Log2.
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
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 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...
std::enable_if_t< std::is_unsigned_v< T >, T > AbsoluteDifference(T X, T Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result.
constexpr bool has_single_bit(T Value) noexcept
uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator)
Returns the integer nearest(Numerator / Denominator).
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.
unsigned countTrailingZeros(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
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.
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 uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
unsigned countLeadingOnes(T Value)
Count the number of ones from the most significant bit to the first zero bit.
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 uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
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.
unsigned countLeadingZeros(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
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 ...
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 countTrailingOnes(T Value)
Count the number of ones from the least significant bit to the first zero bit.
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.
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.