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"
17 #include "llvm/Support/Compiler.h"
18 #include <cassert>
19 #include <climits>
20 #include <cstdint>
21 #include <cstring>
22 #include <limits>
23 #include <type_traits>
24
25 namespace 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.
31 template <typename T, typename U>
32 using enableif_int =
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.
36 template <typename T, typename U, typename = enableif_int<T, U>>
37 using common_uint =
38 std::common_type_t<std::make_unsigned_t<T>, std::make_unsigned_t<U>>;
39 template <typename T, typename U, typename = enableif_int<T, U>>
40 using common_sint =
41 std::common_type_t<std::make_signed_t<T>, std::make_signed_t<U>>;
42
43 /// Mathematical constants.
44 namespace numbers {
45 // TODO: Track C++20 std::numbers.
46 // TODO: Favor using the hexadecimal FP constants (requires C++17).
47 constexpr 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
62 constexpr 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.
maskTrailingOnes(unsigned N)81 template <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.
maskLeadingOnes(unsigned N)92 template <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.
maskTrailingZeros(unsigned N)98 template <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.
maskLeadingZeros(unsigned N)104 template <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
111 static 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.
reverseBits(T Val)122 template <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.
Hi_32(uint64_t Value)154 constexpr uint32_t Hi_32(uint64_t Value) {
155 return static_cast<uint32_t>(Value >> 32);
156 }
157
158 /// Return the low 32 bits of a 64 bit value.
Lo_32(uint64_t Value)159 constexpr uint32_t Lo_32(uint64_t 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.
Make_64(uint32_t High,uint32_t Low)164 constexpr uint64_t Make_64(uint32_t High, uint32_t Low) {
165 return ((uint64_t)High << 32) | (uint64_t)Low;
166 }
167
168 /// Checks if an integer fits into the given bit width.
isInt(int64_t x)169 template <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.
185 template <unsigned N, unsigned S>
isShiftedInt(int64_t x)186 constexpr 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.
isUInt(uint64_t x)193 template <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.
209 template <unsigned N, unsigned S>
isShiftedUInt(uint64_t x)210 constexpr 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.
maxUIntN(uint64_t N)219 inline uint64_t maxUIntN(uint64_t N) {
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.
minIntN(int64_t N)235 inline 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.
maxIntN(int64_t N)244 inline 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.
isUIntN(unsigned N,uint64_t x)255 inline 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.
isIntN(unsigned N,int64_t x)260 inline 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.
isMask_32(uint32_t Value)267 constexpr 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).
isMask_64(uint64_t Value)273 constexpr 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.
isShiftedMask_32(uint32_t Value)279 constexpr bool isShiftedMask_32(uint32_t Value) {
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.)
isShiftedMask_64(uint64_t Value)285 constexpr bool isShiftedMask_64(uint64_t Value) {
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.)
isPowerOf2_32(uint32_t Value)291 constexpr bool isPowerOf2_32(uint32_t Value) {
292 return llvm::has_single_bit(Value);
293 }
294
295 /// Return true if the argument is a power of two > 0 (64 bit edition.)
isPowerOf2_64(uint64_t Value)296 constexpr bool isPowerOf2_64(uint64_t Value) {
297 return llvm::has_single_bit(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.
isShiftedMask_32(uint32_t Value,unsigned & MaskIdx,unsigned & MaskLen)305 inline bool isShiftedMask_32(uint32_t Value, unsigned &MaskIdx,
306 unsigned &MaskLen) {
307 if (!isShiftedMask_32(Value))
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.
isShiftedMask_64(uint64_t Value,unsigned & MaskIdx,unsigned & MaskLen)318 inline bool isShiftedMask_64(uint64_t Value, unsigned &MaskIdx,
319 unsigned &MaskLen) {
320 if (!isShiftedMask_64(Value))
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.
CTLog2()329 template <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
335 template <> 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
Log2_32(uint32_t Value)340 inline 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.)
Log2_64(uint64_t Value)346 inline 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
Log2_32_Ceil(uint32_t Value)353 inline 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.)
Log2_64_Ceil(uint64_t Value)359 inline 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.
365 template <typename U, typename V, typename T = common_uint<U, V>>
MinAlign(U A,V B)366 constexpr 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.
MinAlign(uint64_t A,uint64_t B)376 constexpr uint64_t MinAlign(uint64_t A, uint64_t B) {
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.
NextPowerOf2(uint64_t A)382 constexpr uint64_t NextPowerOf2(uint64_t A) {
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.
PowerOf2Ceil(uint64_t A)394 inline uint64_t PowerOf2Ceil(uint64_t A) {
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.
402 template <typename U, typename V, typename T = common_uint<U, V>>
divideCeil(U Numerator,V Denominator)403 constexpr 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.
divideCeil(uint64_t Numerator,uint64_t Denominator)410 constexpr 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.
418 template <typename U, typename V>
divideSignedWouldOverflow(U Numerator,V Denominator)419 constexpr 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.
425 template <typename U, typename V, typename T = common_sint<U, V>>
divideCeilSigned(U Numerator,V Denominator)426 constexpr 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.
441 template <typename U, typename V, typename T = common_sint<U, V>>
divideFloorSigned(U Numerator,V Denominator)442 constexpr 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.
457 template <typename U, typename V, typename T = common_sint<U, V>>
mod(U Numerator,V Denominator)458 constexpr 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.
466 template <typename U, typename V, typename T = common_uint<U, V>>
divideNearest(U Numerator,V Denominator)467 constexpr 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.
486 template <typename U, typename V, typename T = common_uint<U, V>>
alignTo(U Value,V Align)487 constexpr 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.
alignTo(uint64_t Value,uint64_t Align)494 constexpr uint64_t alignTo(uint64_t Value, uint64_t Align) {
495 assert(Align != 0u && "Align can't be 0.");
496 uint64_t CeilDiv = divideCeil(Value, Align);
497 return CeilDiv * Align;
498 }
499
alignToPowerOf2(uint64_t Value,uint64_t Align)500 constexpr uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
501 assert(Align != 0 && (Align & (Align - 1)) == 0 &&
502 "Align must be a power of 2");
503 // Replace unary minus to avoid compilation error on Windows:
504 // "unary minus operator applied to unsigned type, result still unsigned"
505 uint64_t NegAlign = (~Align) + 1;
506 return (Value + Align - 1) & NegAlign;
507 }
508
509 /// If non-zero \p Skew is specified, the return value will be a minimal integer
510 /// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
511 /// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
512 /// Skew mod \p A'. \p Align must be non-zero.
513 ///
514 /// Examples:
515 /// \code
516 /// alignTo(5, 8, 7) = 7
517 /// alignTo(17, 8, 1) = 17
518 /// alignTo(~0LL, 8, 3) = 3
519 /// alignTo(321, 255, 42) = 552
520 /// \endcode
521 ///
522 /// May overflow.
523 template <typename U, typename V, typename W,
524 typename T = common_uint<common_uint<U, V>, W>>
alignTo(U Value,V Align,W Skew)525 constexpr T alignTo(U Value, V Align, W Skew) {
526 assert(Align != 0u && "Align can't be 0.");
527 Skew %= Align;
528 return alignTo(Value - Skew, Align) + Skew;
529 }
530
531 /// Returns the next integer (mod 2**nbits) that is greater than or equal to
532 /// \p Value and is a multiple of \c Align. \c Align must be non-zero.
533 ///
534 /// Will overflow only if result is not representable in T.
535 template <auto Align, typename V, typename T = common_uint<decltype(Align), V>>
alignTo(V Value)536 constexpr T alignTo(V Value) {
537 static_assert(Align != 0u, "Align must be non-zero");
538 T CeilDiv = divideCeil(Value, Align);
539 return CeilDiv * Align;
540 }
541
542 /// Returns the largest unsigned integer less than or equal to \p Value and is
543 /// \p Skew mod \p Align. \p Align must be non-zero. Guaranteed to never
544 /// overflow.
545 template <typename U, typename V, typename W = uint8_t,
546 typename T = common_uint<common_uint<U, V>, W>>
547 constexpr T alignDown(U Value, V Align, W Skew = 0) {
548 assert(Align != 0u && "Align can't be 0.");
549 Skew %= Align;
550 return (Value - Skew) / Align * Align + Skew;
551 }
552
553 /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
554 /// Requires B <= 32.
SignExtend32(uint32_t X)555 template <unsigned B> constexpr int32_t SignExtend32(uint32_t X) {
556 static_assert(B <= 32, "Bit width out of range.");
557 if constexpr (B == 0)
558 return 0;
559 return int32_t(X << (32 - B)) >> (32 - B);
560 }
561
562 /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
563 /// Requires B <= 32.
SignExtend32(uint32_t X,unsigned B)564 inline int32_t SignExtend32(uint32_t X, unsigned B) {
565 assert(B <= 32 && "Bit width out of range.");
566 if (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 64-bit integer.
572 /// Requires B <= 64.
SignExtend64(uint64_t x)573 template <unsigned B> constexpr int64_t SignExtend64(uint64_t x) {
574 static_assert(B <= 64, "Bit width out of range.");
575 if constexpr (B == 0)
576 return 0;
577 return int64_t(x << (64 - B)) >> (64 - B);
578 }
579
580 /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
581 /// Requires B <= 64.
SignExtend64(uint64_t X,unsigned B)582 inline int64_t SignExtend64(uint64_t X, unsigned B) {
583 assert(B <= 64 && "Bit width out of range.");
584 if (B == 0)
585 return 0;
586 return int64_t(X << (64 - B)) >> (64 - B);
587 }
588
589 /// Subtract two unsigned integers, X and Y, of type T and return the absolute
590 /// value of the result.
591 template <typename U, typename V, typename T = common_uint<U, V>>
AbsoluteDifference(U X,V Y)592 constexpr T AbsoluteDifference(U X, V Y) {
593 return X > Y ? (X - Y) : (Y - X);
594 }
595
596 /// Add two unsigned integers, X and Y, of type T. Clamp the result to the
597 /// maximum representable value of T on overflow. ResultOverflowed indicates if
598 /// the result is larger than the maximum representable value of type T.
599 template <typename T>
600 std::enable_if_t<std::is_unsigned_v<T>, T>
601 SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
602 bool Dummy;
603 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
604 // Hacker's Delight, p. 29
605 T Z = X + Y;
606 Overflowed = (Z < X || Z < Y);
607 if (Overflowed)
608 return std::numeric_limits<T>::max();
609 else
610 return Z;
611 }
612
613 /// Add multiple unsigned integers of type T. Clamp the result to the
614 /// maximum representable value of T on overflow.
615 template <class T, class... Ts>
SaturatingAdd(T X,T Y,T Z,Ts...Args)616 std::enable_if_t<std::is_unsigned_v<T>, T> SaturatingAdd(T X, T Y, T Z,
617 Ts... Args) {
618 bool Overflowed = false;
619 T XY = SaturatingAdd(X, Y, &Overflowed);
620 if (Overflowed)
621 return SaturatingAdd(std::numeric_limits<T>::max(), T(1), Args...);
622 return SaturatingAdd(XY, Z, Args...);
623 }
624
625 /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the
626 /// maximum representable value of T on overflow. ResultOverflowed indicates if
627 /// the result is larger than the maximum representable value of type T.
628 template <typename T>
629 std::enable_if_t<std::is_unsigned_v<T>, T>
630 SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
631 bool Dummy;
632 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
633
634 // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
635 // because it fails for uint16_t (where multiplication can have undefined
636 // behavior due to promotion to int), and requires a division in addition
637 // to the multiplication.
638
639 Overflowed = false;
640
641 // Log2(Z) would be either Log2Z or Log2Z + 1.
642 // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
643 // will necessarily be less than Log2Max as desired.
644 int Log2Z = Log2_64(X) + Log2_64(Y);
645 const T Max = std::numeric_limits<T>::max();
646 int Log2Max = Log2_64(Max);
647 if (Log2Z < Log2Max) {
648 return X * Y;
649 }
650 if (Log2Z > Log2Max) {
651 Overflowed = true;
652 return Max;
653 }
654
655 // We're going to use the top bit, and maybe overflow one
656 // bit past it. Multiply all but the bottom bit then add
657 // that on at the end.
658 T Z = (X >> 1) * Y;
659 if (Z & ~(Max >> 1)) {
660 Overflowed = true;
661 return Max;
662 }
663 Z <<= 1;
664 if (X & 1)
665 return SaturatingAdd(Z, Y, ResultOverflowed);
666
667 return Z;
668 }
669
670 /// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to
671 /// the product. Clamp the result to the maximum representable value of T on
672 /// overflow. ResultOverflowed indicates if the result is larger than the
673 /// maximum representable value of type T.
674 template <typename T>
675 std::enable_if_t<std::is_unsigned_v<T>, T>
676 SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
677 bool Dummy;
678 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
679
680 T Product = SaturatingMultiply(X, Y, &Overflowed);
681 if (Overflowed)
682 return Product;
683
684 return SaturatingAdd(A, Product, &Overflowed);
685 }
686
687 /// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
688 extern const float huge_valf;
689
690 /// Add two signed integers, computing the two's complement truncated result,
691 /// returning true if overflow occurred.
692 template <typename T>
AddOverflow(T X,T Y,T & Result)693 std::enable_if_t<std::is_signed_v<T>, T> AddOverflow(T X, T Y, T &Result) {
694 #if __has_builtin(__builtin_add_overflow)
695 return __builtin_add_overflow(X, Y, &Result);
696 #else
697 // Perform the unsigned addition.
698 using U = std::make_unsigned_t<T>;
699 const U UX = static_cast<U>(X);
700 const U UY = static_cast<U>(Y);
701 const U UResult = UX + UY;
702
703 // Convert to signed.
704 Result = static_cast<T>(UResult);
705
706 // Adding two positive numbers should result in a positive number.
707 if (X > 0 && Y > 0)
708 return Result <= 0;
709 // Adding two negatives should result in a negative number.
710 if (X < 0 && Y < 0)
711 return Result >= 0;
712 return false;
713 #endif
714 }
715
716 /// Subtract two signed integers, computing the two's complement truncated
717 /// result, returning true if an overflow ocurred.
718 template <typename T>
SubOverflow(T X,T Y,T & Result)719 std::enable_if_t<std::is_signed_v<T>, T> SubOverflow(T X, T Y, T &Result) {
720 #if __has_builtin(__builtin_sub_overflow)
721 return __builtin_sub_overflow(X, Y, &Result);
722 #else
723 // Perform the unsigned addition.
724 using U = std::make_unsigned_t<T>;
725 const U UX = static_cast<U>(X);
726 const U UY = static_cast<U>(Y);
727 const U UResult = UX - UY;
728
729 // Convert to signed.
730 Result = static_cast<T>(UResult);
731
732 // Subtracting a positive number from a negative results in a negative number.
733 if (X <= 0 && Y > 0)
734 return Result >= 0;
735 // Subtracting a negative number from a positive results in a positive number.
736 if (X >= 0 && Y < 0)
737 return Result <= 0;
738 return false;
739 #endif
740 }
741
742 /// Multiply two signed integers, computing the two's complement truncated
743 /// result, returning true if an overflow ocurred.
744 template <typename T>
MulOverflow(T X,T Y,T & Result)745 std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
746 #if __has_builtin(__builtin_mul_overflow)
747 return __builtin_mul_overflow(X, Y, &Result);
748 #else
749 // Perform the unsigned multiplication on absolute values.
750 using U = std::make_unsigned_t<T>;
751 const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
752 const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y);
753 const U UResult = UX * UY;
754
755 // Convert to signed.
756 const bool IsNegative = (X < 0) ^ (Y < 0);
757 Result = IsNegative ? (0 - UResult) : UResult;
758
759 // If any of the args was 0, result is 0 and no overflow occurs.
760 if (UX == 0 || UY == 0)
761 return false;
762
763 // UX and UY are in [1, 2^n], where n is the number of digits.
764 // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
765 // positive) divided by an argument compares to the other.
766 if (IsNegative)
767 return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
768 else
769 return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
770 #endif
771 }
772
773 /// Type to force float point values onto the stack, so that x86 doesn't add
774 /// hidden precision, avoiding rounding differences on various platforms.
775 #if defined(__i386__) || defined(_M_IX86)
776 using stack_float_t = volatile float;
777 #else
778 using stack_float_t = float;
779 #endif
780
781 } // namespace llvm
782
783 #endif
784