LLVM 20.0.0git
MathExtras.h
Go to the documentation of this file.
1//===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains some functions that are useful for math stuff.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_MATHEXTRAS_H
14#define LLVM_SUPPORT_MATHEXTRAS_H
15
16#include "llvm/ADT/bit.h"
18#include <cassert>
19#include <climits>
20#include <cstdint>
21#include <cstring>
22#include <limits>
23#include <type_traits>
24
25namespace llvm {
26/// Some template parameter helpers to optimize for bitwidth, for functions that
27/// take multiple arguments.
28
29// We can't verify signedness, since callers rely on implicit coercions to
30// signed/unsigned.
31template <typename T, typename U>
33 std::enable_if_t<std::is_integral_v<T> && std::is_integral_v<U>>;
34
35// Use std::common_type_t to widen only up to the widest argument.
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>>;
42
43/// Mathematical constants.
44namespace numbers {
45// TODO: Track C++20 std::numbers.
46// TODO: Favor using the hexadecimal FP constants (requires C++17).
47constexpr double e = 2.7182818284590452354, // (0x1.5bf0a8b145749P+1) https://oeis.org/A001113
48 egamma = .57721566490153286061, // (0x1.2788cfc6fb619P-1) https://oeis.org/A001620
49 ln2 = .69314718055994530942, // (0x1.62e42fefa39efP-1) https://oeis.org/A002162
50 ln10 = 2.3025850929940456840, // (0x1.24bb1bbb55516P+1) https://oeis.org/A002392
51 log2e = 1.4426950408889634074, // (0x1.71547652b82feP+0)
52 log10e = .43429448190325182765, // (0x1.bcb7b1526e50eP-2)
53 pi = 3.1415926535897932385, // (0x1.921fb54442d18P+1) https://oeis.org/A000796
54 inv_pi = .31830988618379067154, // (0x1.45f306bc9c883P-2) https://oeis.org/A049541
55 sqrtpi = 1.7724538509055160273, // (0x1.c5bf891b4ef6bP+0) https://oeis.org/A002161
56 inv_sqrtpi = .56418958354775628695, // (0x1.20dd750429b6dP-1) https://oeis.org/A087197
57 sqrt2 = 1.4142135623730950488, // (0x1.6a09e667f3bcdP+0) https://oeis.org/A00219
58 inv_sqrt2 = .70710678118654752440, // (0x1.6a09e667f3bcdP-1)
59 sqrt3 = 1.7320508075688772935, // (0x1.bb67ae8584caaP+0) https://oeis.org/A002194
60 inv_sqrt3 = .57735026918962576451, // (0x1.279a74590331cP-1)
61 phi = 1.6180339887498948482; // (0x1.9e3779b97f4a8P+0) https://oeis.org/A001622
62constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A001113
63 egammaf = .577215665F, // (0x1.2788d0P-1) https://oeis.org/A001620
64 ln2f = .693147181F, // (0x1.62e430P-1) https://oeis.org/A002162
65 ln10f = 2.30258509F, // (0x1.26bb1cP+1) https://oeis.org/A002392
66 log2ef = 1.44269504F, // (0x1.715476P+0)
67 log10ef = .434294482F, // (0x1.bcb7b2P-2)
68 pif = 3.14159265F, // (0x1.921fb6P+1) https://oeis.org/A000796
69 inv_pif = .318309886F, // (0x1.45f306P-2) https://oeis.org/A049541
70 sqrtpif = 1.77245385F, // (0x1.c5bf8aP+0) https://oeis.org/A002161
71 inv_sqrtpif = .564189584F, // (0x1.20dd76P-1) https://oeis.org/A087197
72 sqrt2f = 1.41421356F, // (0x1.6a09e6P+0) https://oeis.org/A002193
73 inv_sqrt2f = .707106781F, // (0x1.6a09e6P-1)
74 sqrt3f = 1.73205081F, // (0x1.bb67aeP+0) https://oeis.org/A002194
75 inv_sqrt3f = .577350269F, // (0x1.279a74P-1)
76 phif = 1.61803399F; // (0x1.9e377aP+0) https://oeis.org/A001622
77} // namespace numbers
78
79/// Create a bitmask with the N right-most bits set to 1, and all other
80/// bits set to 0. Only unsigned types are allowed.
81template <typename T> T maskTrailingOnes(unsigned N) {
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");
85 if (N == 0)
86 return 0;
87 return T(-1) >> (Bits - N);
88}
89
90/// Create a bitmask with the N left-most bits set to 1, and all other
91/// bits set to 0. Only unsigned types are allowed.
92template <typename T> T maskLeadingOnes(unsigned N) {
93 return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
94}
95
96/// Create a bitmask with the N right-most bits set to 0, and all other
97/// bits set to 1. Only unsigned types are allowed.
98template <typename T> T maskTrailingZeros(unsigned N) {
99 return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
100}
101
102/// Create a bitmask with the N left-most bits set to 0, and all other
103/// bits set to 1. Only unsigned types are allowed.
104template <typename T> T maskLeadingZeros(unsigned N) {
105 return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
106}
107
108/// Macro compressed bit reversal table for 256 bits.
109///
110/// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
111static const unsigned char BitReverseTable256[256] = {
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)
115 R6(0), R6(2), R6(1), R6(3)
116#undef R2
117#undef R4
118#undef R6
119};
120
121/// Reverse the bits in \p Val.
122template <typename T> T reverseBits(T Val) {
123#if __has_builtin(__builtin_bitreverse8)
124 if constexpr (std::is_same_v<T, uint8_t>)
125 return __builtin_bitreverse8(Val);
126#endif
127#if __has_builtin(__builtin_bitreverse16)
128 if constexpr (std::is_same_v<T, uint16_t>)
129 return __builtin_bitreverse16(Val);
130#endif
131#if __has_builtin(__builtin_bitreverse32)
132 if constexpr (std::is_same_v<T, uint32_t>)
133 return __builtin_bitreverse32(Val);
134#endif
135#if __has_builtin(__builtin_bitreverse64)
136 if constexpr (std::is_same_v<T, uint64_t>)
137 return __builtin_bitreverse64(Val);
138#endif
139
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)
144 out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
145 std::memcpy(&Val, out, sizeof(Val));
146 return Val;
147}
148
149// NOTE: The following support functions use the _32/_64 extensions instead of
150// type overloading so that signed and unsigned integers can be used without
151// ambiguity.
152
153/// Return the high 32 bits of a 64 bit value.
155 return static_cast<uint32_t>(Value >> 32);
156}
157
158/// Return the low 32 bits of a 64 bit value.
160 return static_cast<uint32_t>(Value);
161}
162
163/// Make a 64-bit integer from a high / low pair of 32-bit integers.
165 return ((uint64_t)High << 32) | (uint64_t)Low;
166}
167
168/// Checks if an integer fits into the given bit width.
169template <unsigned N> constexpr bool isInt(int64_t x) {
170 if constexpr (N == 0)
171 return 0 == x;
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));
180 (void)x; // MSVC v19.25 warns that x is unused.
181 return true;
182}
183
184/// Checks if a signed integer is an N bit number shifted left by S.
185template <unsigned N, unsigned S>
186constexpr bool isShiftedInt(int64_t x) {
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);
190}
191
192/// Checks if an unsigned integer fits into the given bit width.
193template <unsigned N> constexpr bool isUInt(uint64_t x) {
194 if constexpr (N == 0)
195 return 0 == x;
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));
204 (void)x; // MSVC v19.25 warns that x is unused.
205 return true;
206}
207
208/// Checks if a unsigned integer is an N bit number shifted left by S.
209template <unsigned N, unsigned S>
210constexpr bool isShiftedUInt(uint64_t x) {
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.");
214 // S must be strictly less than 64. So 1 << S is not undefined behavior.
215 return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
216}
217
218/// Gets the maximum value for a N-bit unsigned integer.
220 assert(N <= 64 && "integer width out of range");
221
222 // uint64_t(1) << 64 is undefined behavior, so we can't do
223 // (uint64_t(1) << N) - 1
224 // without checking first that N != 64. But this works and doesn't have a
225 // branch for N != 0.
226 // Unfortunately, shifting a uint64_t right by 64 bit is undefined
227 // behavior, so the condition on N == 0 is necessary. Fortunately, most
228 // optimizers do not emit branches for this check.
229 if (N == 0)
230 return 0;
231 return UINT64_MAX >> (64 - N);
232}
233
234/// Gets the minimum value for a N-bit signed integer.
235inline int64_t minIntN(int64_t N) {
236 assert(N <= 64 && "integer width out of range");
237
238 if (N == 0)
239 return 0;
240 return UINT64_C(1) + ~(UINT64_C(1) << (N - 1));
241}
242
243/// Gets the maximum value for a N-bit signed integer.
244inline int64_t maxIntN(int64_t N) {
245 assert(N <= 64 && "integer width out of range");
246
247 // This relies on two's complement wraparound when N == 64, so we convert to
248 // int64_t only at the very end to avoid UB.
249 if (N == 0)
250 return 0;
251 return (UINT64_C(1) << (N - 1)) - 1;
252}
253
254/// Checks if an unsigned integer fits into the given (dynamic) bit width.
255inline bool isUIntN(unsigned N, uint64_t x) {
256 return N >= 64 || x <= maxUIntN(N);
257}
258
259/// Checks if an signed integer fits into the given (dynamic) bit width.
260inline bool isIntN(unsigned N, int64_t x) {
261 return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
262}
263
264/// Return true if the argument is a non-empty sequence of ones starting at the
265/// least significant bit with the remainder zero (32 bit version).
266/// Ex. isMask_32(0x0000FFFFU) == true.
267constexpr bool isMask_32(uint32_t Value) {
268 return Value && ((Value + 1) & Value) == 0;
269}
270
271/// Return true if the argument is a non-empty sequence of ones starting at the
272/// least significant bit with the remainder zero (64 bit version).
273constexpr bool isMask_64(uint64_t Value) {
274 return Value && ((Value + 1) & Value) == 0;
275}
276
277/// Return true if the argument contains a non-empty sequence of ones with the
278/// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
280 return Value && isMask_32((Value - 1) | Value);
281}
282
283/// Return true if the argument contains a non-empty sequence of ones with the
284/// remainder zero (64 bit version.)
286 return Value && isMask_64((Value - 1) | Value);
287}
288
289/// Return true if the argument is a power of two > 0.
290/// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
291constexpr bool isPowerOf2_32(uint32_t Value) {
293}
294
295/// Return true if the argument is a power of two > 0 (64 bit edition.)
296constexpr bool isPowerOf2_64(uint64_t Value) {
298}
299
300/// Return true if the argument contains a non-empty sequence of ones with the
301/// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
302/// If true, \p MaskIdx will specify the index of the lowest set bit and \p
303/// MaskLen is updated to specify the length of the mask, else neither are
304/// updated.
305inline bool isShiftedMask_32(uint32_t Value, unsigned &MaskIdx,
306 unsigned &MaskLen) {
308 return false;
309 MaskIdx = llvm::countr_zero(Value);
310 MaskLen = llvm::popcount(Value);
311 return true;
312}
313
314/// Return true if the argument contains a non-empty sequence of ones with the
315/// remainder zero (64 bit version.) If true, \p MaskIdx will specify the index
316/// of the lowest set bit and \p MaskLen is updated to specify the length of the
317/// mask, else neither are updated.
318inline bool isShiftedMask_64(uint64_t Value, unsigned &MaskIdx,
319 unsigned &MaskLen) {
321 return false;
322 MaskIdx = llvm::countr_zero(Value);
323 MaskLen = llvm::popcount(Value);
324 return true;
325}
326
327/// Compile time Log2.
328/// Valid only for positive powers of two.
329template <size_t kValue> constexpr size_t CTLog2() {
330 static_assert(kValue > 0 && llvm::isPowerOf2_64(kValue),
331 "Value is not a valid power of 2");
332 return 1 + CTLog2<kValue / 2>();
333}
334
335template <> constexpr size_t CTLog2<1>() { return 0; }
336
337/// Return the floor log base 2 of the specified value, -1 if the value is zero.
338/// (32 bit edition.)
339/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
340inline unsigned Log2_32(uint32_t Value) {
341 return 31 - llvm::countl_zero(Value);
342}
343
344/// Return the floor log base 2 of the specified value, -1 if the value is zero.
345/// (64 bit edition.)
346inline unsigned Log2_64(uint64_t Value) {
347 return 63 - llvm::countl_zero(Value);
348}
349
350/// Return the ceil log base 2 of the specified value, 32 if the value is zero.
351/// (32 bit edition).
352/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
353inline unsigned Log2_32_Ceil(uint32_t Value) {
354 return 32 - llvm::countl_zero(Value - 1);
355}
356
357/// Return the ceil log base 2 of the specified value, 64 if the value is zero.
358/// (64 bit edition.)
359inline unsigned Log2_64_Ceil(uint64_t Value) {
360 return 64 - llvm::countl_zero(Value - 1);
361}
362
363/// A and B are either alignments or offsets. Return the minimum alignment that
364/// may be assumed after adding the two together.
365template <typename U, typename V, typename T = common_uint<U, V>>
366constexpr T MinAlign(U A, V B) {
367 // The largest power of 2 that divides both A and B.
368 //
369 // Replace "-Value" by "1+~Value" in the following commented code to avoid
370 // MSVC warning C4146
371 // return (A | B) & -(A | B);
372 return (A | B) & (1 + ~(A | B));
373}
374
375/// Fallback when arguments aren't integral.
377 return (A | B) & (1 + ~(A | B));
378}
379
380/// Returns the next power of two (in 64-bits) that is strictly greater than A.
381/// Returns zero on overflow.
383 A |= (A >> 1);
384 A |= (A >> 2);
385 A |= (A >> 4);
386 A |= (A >> 8);
387 A |= (A >> 16);
388 A |= (A >> 32);
389 return A + 1;
390}
391
392/// Returns the power of two which is greater than or equal to the given value.
393/// Essentially, it is a ceil operation across the domain of powers of two.
395 if (!A || A > UINT64_MAX / 2)
396 return 0;
397 return UINT64_C(1) << Log2_64_Ceil(A);
398}
399
400/// Returns the integer ceil(Numerator / Denominator). Unsigned version.
401/// Guaranteed to never overflow.
402template <typename U, typename V, typename T = common_uint<U, V>>
403constexpr T divideCeil(U Numerator, V Denominator) {
404 assert(Denominator && "Division by zero");
405 T Bias = (Numerator != 0);
406 return (Numerator - Bias) / Denominator + Bias;
407}
408
409/// Fallback when arguments aren't integral.
410constexpr uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
411 assert(Denominator && "Division by zero");
412 uint64_t Bias = (Numerator != 0);
413 return (Numerator - Bias) / Denominator + Bias;
414}
415
416// Check whether divideCeilSigned or divideFloorSigned would overflow. This
417// happens only when Numerator = INT_MIN and Denominator = -1.
418template <typename U, typename V>
419constexpr bool divideSignedWouldOverflow(U Numerator, V Denominator) {
420 return Numerator == std::numeric_limits<U>::min() && Denominator == -1;
421}
422
423/// Returns the integer ceil(Numerator / Denominator). Signed version.
424/// Overflow is explicitly forbidden with an assert.
425template <typename U, typename V, typename T = common_sint<U, V>>
426constexpr T divideCeilSigned(U Numerator, V Denominator) {
427 assert(Denominator && "Division by zero");
428 assert(!divideSignedWouldOverflow(Numerator, Denominator) &&
429 "Divide would overflow");
430 if (!Numerator)
431 return 0;
432 // C's integer division rounds towards 0.
433 T Bias = Denominator >= 0 ? 1 : -1;
434 bool SameSign = (Numerator >= 0) == (Denominator >= 0);
435 return SameSign ? (Numerator - Bias) / Denominator + 1
436 : Numerator / Denominator;
437}
438
439/// Returns the integer floor(Numerator / Denominator). Signed version.
440/// Overflow is explicitly forbidden with an assert.
441template <typename U, typename V, typename T = common_sint<U, V>>
442constexpr T divideFloorSigned(U Numerator, V Denominator) {
443 assert(Denominator && "Division by zero");
444 assert(!divideSignedWouldOverflow(Numerator, Denominator) &&
445 "Divide would overflow");
446 if (!Numerator)
447 return 0;
448 // C's integer division rounds towards 0.
449 T Bias = Denominator >= 0 ? -1 : 1;
450 bool SameSign = (Numerator >= 0) == (Denominator >= 0);
451 return SameSign ? Numerator / Denominator
452 : (Numerator - Bias) / Denominator - 1;
453}
454
455/// Returns the remainder of the Euclidean division of LHS by RHS. Result is
456/// always non-negative.
457template <typename U, typename V, typename T = common_sint<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;
462}
463
464/// Returns (Numerator / Denominator) rounded by round-half-up. Guaranteed to
465/// never overflow.
466template <typename U, typename V, typename T = common_uint<U, V>>
467constexpr T divideNearest(U Numerator, V Denominator) {
468 assert(Denominator && "Division by zero");
469 T Mod = Numerator % Denominator;
470 return (Numerator / Denominator) +
471 (Mod > (static_cast<T>(Denominator) - 1) / 2);
472}
473
474/// Returns the next integer (mod 2**nbits) that is greater than or equal to
475/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
476///
477/// Examples:
478/// \code
479/// alignTo(5, 8) = 8
480/// alignTo(17, 8) = 24
481/// alignTo(~0LL, 8) = 0
482/// alignTo(321, 255) = 510
483/// \endcode
484///
485/// Will overflow only if result is not representable in T.
486template <typename U, typename V, typename T = common_uint<U, V>>
487constexpr T alignTo(U Value, V Align) {
488 assert(Align != 0u && "Align can't be 0.");
489 T CeilDiv = divideCeil(Value, Align);
490 return CeilDiv * Align;
491}
492
493/// Fallback when arguments aren't integral.
495 assert(Align != 0u && "Align can't be 0.");
496 uint64_t CeilDiv = divideCeil(Value, Align);
497 return CeilDiv * Align;
498}
499
500/// Will overflow only if result is not representable in T.
501template <typename U, typename V, typename T = common_uint<U, V>>
502constexpr T alignToPowerOf2(U Value, V Align) {
503 assert(Align != 0 && (Align & (Align - 1)) == 0 &&
504 "Align must be a power of 2");
505 T NegAlign = static_cast<T>(0) - Align;
506 return (Value + (Align - 1)) & NegAlign;
507}
508
509/// Fallback when arguments aren't integral.
511 assert(Align != 0 && (Align & (Align - 1)) == 0 &&
512 "Align must be a power of 2");
513 uint64_t NegAlign = 0 - Align;
514 return (Value + (Align - 1)) & NegAlign;
515}
516
517/// If non-zero \p Skew is specified, the return value will be a minimal integer
518/// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
519/// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
520/// Skew mod \p A'. \p Align must be non-zero.
521///
522/// Examples:
523/// \code
524/// alignTo(5, 8, 7) = 7
525/// alignTo(17, 8, 1) = 17
526/// alignTo(~0LL, 8, 3) = 3
527/// alignTo(321, 255, 42) = 552
528/// \endcode
529///
530/// May overflow.
531template <typename U, typename V, typename W,
532 typename T = common_uint<common_uint<U, V>, W>>
533constexpr T alignTo(U Value, V Align, W Skew) {
534 assert(Align != 0u && "Align can't be 0.");
535 Skew %= Align;
536 return alignTo(Value - Skew, Align) + Skew;
537}
538
539/// Returns the next integer (mod 2**nbits) that is greater than or equal to
540/// \p Value and is a multiple of \c Align. \c Align must be non-zero.
541///
542/// Will overflow only if result is not representable in T.
543template <auto Align, typename V, typename T = common_uint<decltype(Align), V>>
544constexpr T alignTo(V Value) {
545 static_assert(Align != 0u, "Align must be non-zero");
546 T CeilDiv = divideCeil(Value, Align);
547 return CeilDiv * Align;
548}
549
550/// Returns the largest unsigned integer less than or equal to \p Value and is
551/// \p Skew mod \p Align. \p Align must be non-zero. Guaranteed to never
552/// overflow.
553template <typename U, typename V, typename W = uint8_t,
554 typename T = common_uint<common_uint<U, V>, W>>
555constexpr T alignDown(U Value, V Align, W Skew = 0) {
556 assert(Align != 0u && "Align can't be 0.");
557 Skew %= Align;
558 return (Value - Skew) / Align * Align + Skew;
559}
560
561/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
562/// Requires B <= 32.
563template <unsigned B> constexpr int32_t SignExtend32(uint32_t X) {
564 static_assert(B <= 32, "Bit width out of range.");
565 if constexpr (B == 0)
566 return 0;
567 return int32_t(X << (32 - B)) >> (32 - B);
568}
569
570/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
571/// Requires B <= 32.
572inline int32_t SignExtend32(uint32_t X, unsigned B) {
573 assert(B <= 32 && "Bit width out of range.");
574 if (B == 0)
575 return 0;
576 return int32_t(X << (32 - B)) >> (32 - B);
577}
578
579/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
580/// Requires B <= 64.
581template <unsigned B> constexpr int64_t SignExtend64(uint64_t x) {
582 static_assert(B <= 64, "Bit width out of range.");
583 if constexpr (B == 0)
584 return 0;
585 return int64_t(x << (64 - B)) >> (64 - B);
586}
587
588/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
589/// Requires B <= 64.
590inline int64_t SignExtend64(uint64_t X, unsigned B) {
591 assert(B <= 64 && "Bit width out of range.");
592 if (B == 0)
593 return 0;
594 return int64_t(X << (64 - B)) >> (64 - B);
595}
596
597/// Subtract two unsigned integers, X and Y, of type T and return the absolute
598/// value of the result.
599template <typename U, typename V, typename T = common_uint<U, V>>
600constexpr T AbsoluteDifference(U X, V Y) {
601 return X > Y ? (X - Y) : (Y - X);
602}
603
604/// Add two unsigned integers, X and Y, of type T. Clamp the result to the
605/// maximum representable value of T on overflow. ResultOverflowed indicates if
606/// the result is larger than the maximum representable value of type T.
607template <typename T>
608std::enable_if_t<std::is_unsigned_v<T>, T>
609SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
610 bool Dummy;
611 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
612 // Hacker's Delight, p. 29
613 T Z = X + Y;
614 Overflowed = (Z < X || Z < Y);
615 if (Overflowed)
616 return std::numeric_limits<T>::max();
617 else
618 return Z;
619}
620
621/// Add multiple unsigned integers of type T. Clamp the result to the
622/// maximum representable value of T on overflow.
623template <class T, class... Ts>
624std::enable_if_t<std::is_unsigned_v<T>, T> SaturatingAdd(T X, T Y, T Z,
625 Ts... Args) {
626 bool Overflowed = false;
627 T XY = SaturatingAdd(X, Y, &Overflowed);
628 if (Overflowed)
629 return SaturatingAdd(std::numeric_limits<T>::max(), T(1), Args...);
630 return SaturatingAdd(XY, Z, Args...);
631}
632
633/// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the
634/// maximum representable value of T on overflow. ResultOverflowed indicates if
635/// the result is larger than the maximum representable value of type T.
636template <typename T>
637std::enable_if_t<std::is_unsigned_v<T>, T>
638SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
639 bool Dummy;
640 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
641
642 // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
643 // because it fails for uint16_t (where multiplication can have undefined
644 // behavior due to promotion to int), and requires a division in addition
645 // to the multiplication.
646
647 Overflowed = false;
648
649 // Log2(Z) would be either Log2Z or Log2Z + 1.
650 // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
651 // will necessarily be less than Log2Max as desired.
652 int Log2Z = Log2_64(X) + Log2_64(Y);
653 const T Max = std::numeric_limits<T>::max();
654 int Log2Max = Log2_64(Max);
655 if (Log2Z < Log2Max) {
656 return X * Y;
657 }
658 if (Log2Z > Log2Max) {
659 Overflowed = true;
660 return Max;
661 }
662
663 // We're going to use the top bit, and maybe overflow one
664 // bit past it. Multiply all but the bottom bit then add
665 // that on at the end.
666 T Z = (X >> 1) * Y;
667 if (Z & ~(Max >> 1)) {
668 Overflowed = true;
669 return Max;
670 }
671 Z <<= 1;
672 if (X & 1)
673 return SaturatingAdd(Z, Y, ResultOverflowed);
674
675 return Z;
676}
677
678/// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to
679/// the product. Clamp the result to the maximum representable value of T on
680/// overflow. ResultOverflowed indicates if the result is larger than the
681/// maximum representable value of type T.
682template <typename T>
683std::enable_if_t<std::is_unsigned_v<T>, T>
684SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
685 bool Dummy;
686 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
687
688 T Product = SaturatingMultiply(X, Y, &Overflowed);
689 if (Overflowed)
690 return Product;
691
692 return SaturatingAdd(A, Product, &Overflowed);
693}
694
695/// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
696extern const float huge_valf;
697
698/// Add two signed integers, computing the two's complement truncated result,
699/// returning true if overflow occurred.
700template <typename T>
701std::enable_if_t<std::is_signed_v<T>, T> AddOverflow(T X, T Y, T &Result) {
702#if __has_builtin(__builtin_add_overflow)
703 return __builtin_add_overflow(X, Y, &Result);
704#else
705 // Perform the unsigned addition.
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;
710
711 // Convert to signed.
712 Result = static_cast<T>(UResult);
713
714 // Adding two positive numbers should result in a positive number.
715 if (X > 0 && Y > 0)
716 return Result <= 0;
717 // Adding two negatives should result in a negative number.
718 if (X < 0 && Y < 0)
719 return Result >= 0;
720 return false;
721#endif
722}
723
724/// Subtract two signed integers, computing the two's complement truncated
725/// result, returning true if an overflow ocurred.
726template <typename T>
727std::enable_if_t<std::is_signed_v<T>, T> SubOverflow(T X, T Y, T &Result) {
728#if __has_builtin(__builtin_sub_overflow)
729 return __builtin_sub_overflow(X, Y, &Result);
730#else
731 // Perform the unsigned addition.
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;
736
737 // Convert to signed.
738 Result = static_cast<T>(UResult);
739
740 // Subtracting a positive number from a negative results in a negative number.
741 if (X <= 0 && Y > 0)
742 return Result >= 0;
743 // Subtracting a negative number from a positive results in a positive number.
744 if (X >= 0 && Y < 0)
745 return Result <= 0;
746 return false;
747#endif
748}
749
750/// Multiply two signed integers, computing the two's complement truncated
751/// result, returning true if an overflow ocurred.
752template <typename T>
753std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
754#if __has_builtin(__builtin_mul_overflow)
755 return __builtin_mul_overflow(X, Y, &Result);
756#else
757 // Perform the unsigned multiplication on absolute values.
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;
762
763 // Convert to signed.
764 const bool IsNegative = (X < 0) ^ (Y < 0);
765 Result = IsNegative ? (0 - UResult) : UResult;
766
767 // If any of the args was 0, result is 0 and no overflow occurs.
768 if (UX == 0 || UY == 0)
769 return false;
770
771 // UX and UY are in [1, 2^n], where n is the number of digits.
772 // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
773 // positive) divided by an argument compares to the other.
774 if (IsNegative)
775 return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
776 else
777 return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
778#endif
779}
780
781/// Type to force float point values onto the stack, so that x86 doesn't add
782/// hidden precision, avoiding rounding differences on various platforms.
783#if defined(__i386__) || defined(_M_IX86)
784using stack_float_t = volatile float;
785#else
786using stack_float_t = float;
787#endif
788
789} // namespace llvm
790
791#endif
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")
#define R6(n)
#define T
uint64_t High
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.
Definition: Value.h:74
#define UINT64_MAX
Definition: DataTypes.h:77
constexpr float inv_sqrtpif
Definition: MathExtras.h:71
constexpr double sqrt2
Definition: MathExtras.h:57
constexpr double inv_sqrt2
Definition: MathExtras.h:58
constexpr double inv_pi
Definition: MathExtras.h:54
constexpr double sqrtpi
Definition: MathExtras.h:55
constexpr float pif
Definition: MathExtras.h:68
constexpr float sqrtpif
Definition: MathExtras.h:70
constexpr float log10ef
Definition: MathExtras.h:67
constexpr float ln10f
Definition: MathExtras.h:65
constexpr double ln2
Definition: MathExtras.h:49
constexpr double inv_sqrt3
Definition: MathExtras.h:60
constexpr double egamma
Definition: MathExtras.h:48
constexpr float phif
Definition: MathExtras.h:76
constexpr float sqrt3f
Definition: MathExtras.h:74
constexpr double ln10
Definition: MathExtras.h:50
constexpr double inv_sqrtpi
Definition: MathExtras.h:56
constexpr float log2ef
Definition: MathExtras.h:66
constexpr double e
Definition: MathExtras.h:47
constexpr double phi
Definition: MathExtras.h:61
constexpr float sqrt2f
Definition: MathExtras.h:72
constexpr double sqrt3
Definition: MathExtras.h:59
constexpr float inv_pif
Definition: MathExtras.h:69
constexpr float inv_sqrt2f
Definition: MathExtras.h:73
constexpr double log10e
Definition: MathExtras.h:52
constexpr double log2e
Definition: MathExtras.h:51
constexpr float egammaf
Definition: MathExtras.h:63
constexpr double pi
Definition: MathExtras.h:53
constexpr float ln2f
Definition: MathExtras.h:64
constexpr float ef
Definition: MathExtras.h:62
constexpr float inv_sqrt3f
Definition: MathExtras.h:75
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::common_type_t< std::make_unsigned_t< T >, std::make_unsigned_t< U > > common_uint
Definition: MathExtras.h:38
float stack_float_t
Type to force float point values onto the stack, so that x86 doesn't add hidden precision,...
Definition: MathExtras.h:786
@ 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.
Definition: MathExtras.h:353
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...
Definition: MathExtras.h:753
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h:244
constexpr bool divideSignedWouldOverflow(U Numerator, V Denominator)
Definition: MathExtras.h:419
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt mod(const DynamicAPInt &LHS, const DynamicAPInt &RHS)
is always non-negative.
Definition: DynamicAPInt.h:382
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition: bit.h:385
constexpr size_t CTLog2()
Compile time Log2.
Definition: MathExtras.h:329
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition: MathExtras.h:169
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:255
constexpr size_t CTLog2< 1 >()
Definition: MathExtras.h:335
unsigned Log2_64_Ceil(uint64_t Value)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
Definition: MathExtras.h:359
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...
Definition: MathExtras.h:267
constexpr T divideFloorSigned(U Numerator, V Denominator)
Returns the integer floor(Numerator / Denominator).
Definition: MathExtras.h:442
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.
Definition: MathExtras.h:555
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:296
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...
Definition: MathExtras.h:279
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:346
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:394
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:215
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...
Definition: MathExtras.h:285
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
Definition: MathExtras.h:366
constexpr T divideNearest(U Numerator, V Denominator)
Returns (Numerator / Denominator) rounded by round-half-up.
Definition: MathExtras.h:467
constexpr bool has_single_bit(T Value) noexcept
Definition: bit.h:146
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:340
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: bit.h:281
T maskLeadingZeros(unsigned N)
Create a bitmask with the N left-most bits set to 0, and all other bits set to 1.
Definition: MathExtras.h:104
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:291
T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition: MathExtras.h:81
T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
Definition: MathExtras.h:98
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition: MathExtras.h:154
std::common_type_t< std::make_signed_t< T >, std::make_signed_t< U > > common_sint
Definition: MathExtras.h:41
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition: MathExtras.h:502
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...
Definition: MathExtras.h:273
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.
Definition: MathExtras.h:684
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition: MathExtras.h:193
constexpr T divideCeilSigned(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:426
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition: MathExtras.h:159
@ Mod
The access may modify the value stored in memory.
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:403
const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
Definition: MathExtras.cpp:28
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.
Definition: MathExtras.h:638
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:260
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
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.
Definition: MathExtras.h:600
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition: MathExtras.h:235
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition: MathExtras.h:186
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
Definition: MathExtras.h:563
T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0.
Definition: MathExtras.h:92
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:581
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 ...
Definition: MathExtras.h:701
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...
Definition: MathExtras.h:727
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
Definition: MathExtras.h:111
T reverseBits(T Val)
Reverse the bits in Val.
Definition: MathExtras.h:122
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.
Definition: MathExtras.h:609
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.
Definition: MathExtras.h:33
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition: MathExtras.h:210
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.
Definition: MathExtras.h:164
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition: MathExtras.h:219
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:382
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39