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