LLVM  16.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"
17 #include "llvm/Support/Compiler.h"
18 #include <cassert>
19 #include <climits>
20 #include <cmath>
21 #include <cstdint>
22 #include <cstring>
23 #include <limits>
24 #include <type_traits>
25 
26 #ifdef _MSC_VER
27 // Declare these intrinsics manually rather including intrin.h. It's very
28 // expensive, and MathExtras.h is popular.
29 // #include <intrin.h>
30 extern "C" {
31 unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
32 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
33 unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
34 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
35 }
36 #endif
37 
38 namespace llvm {
39 
40 /// The behavior an operation has on an input of 0.
42  /// The returned value is undefined.
44  /// The returned value is numeric_limits<T>::max()
46  /// The returned value is numeric_limits<T>::digits
48 };
49 
50 /// Mathematical constants.
51 namespace numbers {
52 // TODO: Track C++20 std::numbers.
53 // TODO: Favor using the hexadecimal FP constants (requires C++17).
54 constexpr double e = 2.7182818284590452354, // (0x1.5bf0a8b145749P+1) https://oeis.org/A001113
55  egamma = .57721566490153286061, // (0x1.2788cfc6fb619P-1) https://oeis.org/A001620
56  ln2 = .69314718055994530942, // (0x1.62e42fefa39efP-1) https://oeis.org/A002162
57  ln10 = 2.3025850929940456840, // (0x1.24bb1bbb55516P+1) https://oeis.org/A002392
58  log2e = 1.4426950408889634074, // (0x1.71547652b82feP+0)
59  log10e = .43429448190325182765, // (0x1.bcb7b1526e50eP-2)
60  pi = 3.1415926535897932385, // (0x1.921fb54442d18P+1) https://oeis.org/A000796
61  inv_pi = .31830988618379067154, // (0x1.45f306bc9c883P-2) https://oeis.org/A049541
62  sqrtpi = 1.7724538509055160273, // (0x1.c5bf891b4ef6bP+0) https://oeis.org/A002161
63  inv_sqrtpi = .56418958354775628695, // (0x1.20dd750429b6dP-1) https://oeis.org/A087197
64  sqrt2 = 1.4142135623730950488, // (0x1.6a09e667f3bcdP+0) https://oeis.org/A00219
65  inv_sqrt2 = .70710678118654752440, // (0x1.6a09e667f3bcdP-1)
66  sqrt3 = 1.7320508075688772935, // (0x1.bb67ae8584caaP+0) https://oeis.org/A002194
67  inv_sqrt3 = .57735026918962576451, // (0x1.279a74590331cP-1)
68  phi = 1.6180339887498948482; // (0x1.9e3779b97f4a8P+0) https://oeis.org/A001622
69 constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A001113
70  egammaf = .577215665F, // (0x1.2788d0P-1) https://oeis.org/A001620
71  ln2f = .693147181F, // (0x1.62e430P-1) https://oeis.org/A002162
72  ln10f = 2.30258509F, // (0x1.26bb1cP+1) https://oeis.org/A002392
73  log2ef = 1.44269504F, // (0x1.715476P+0)
74  log10ef = .434294482F, // (0x1.bcb7b2P-2)
75  pif = 3.14159265F, // (0x1.921fb6P+1) https://oeis.org/A000796
76  inv_pif = .318309886F, // (0x1.45f306P-2) https://oeis.org/A049541
77  sqrtpif = 1.77245385F, // (0x1.c5bf8aP+0) https://oeis.org/A002161
78  inv_sqrtpif = .564189584F, // (0x1.20dd76P-1) https://oeis.org/A087197
79  sqrt2f = 1.41421356F, // (0x1.6a09e6P+0) https://oeis.org/A002193
80  inv_sqrt2f = .707106781F, // (0x1.6a09e6P-1)
81  sqrt3f = 1.73205081F, // (0x1.bb67aeP+0) https://oeis.org/A002194
82  inv_sqrt3f = .577350269F, // (0x1.279a74P-1)
83  phif = 1.61803399F; // (0x1.9e377aP+0) https://oeis.org/A001622
84 } // namespace numbers
85 
86 namespace detail {
87 template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
88  static unsigned count(T Val, ZeroBehavior) {
89  if (!Val)
90  return std::numeric_limits<T>::digits;
91  if (Val & 0x1)
92  return 0;
93 
94  // Bisection method.
95  unsigned ZeroBits = 0;
96  T Shift = std::numeric_limits<T>::digits >> 1;
98  while (Shift) {
99  if ((Val & Mask) == 0) {
100  Val >>= Shift;
101  ZeroBits |= Shift;
102  }
103  Shift >>= 1;
104  Mask >>= Shift;
105  }
106  return ZeroBits;
107  }
108 };
109 
110 #if defined(__GNUC__) || defined(_MSC_VER)
111 template <typename T> struct TrailingZerosCounter<T, 4> {
112  static unsigned count(T Val, ZeroBehavior ZB) {
113  if (ZB != ZB_Undefined && Val == 0)
114  return 32;
115 
116 #if __has_builtin(__builtin_ctz) || defined(__GNUC__)
117  return __builtin_ctz(Val);
118 #elif defined(_MSC_VER)
119  unsigned long Index;
120  _BitScanForward(&Index, Val);
121  return Index;
122 #endif
123  }
124 };
125 
126 #if !defined(_MSC_VER) || defined(_M_X64)
127 template <typename T> struct TrailingZerosCounter<T, 8> {
128  static unsigned count(T Val, ZeroBehavior ZB) {
129  if (ZB != ZB_Undefined && Val == 0)
130  return 64;
131 
132 #if __has_builtin(__builtin_ctzll) || defined(__GNUC__)
133  return __builtin_ctzll(Val);
134 #elif defined(_MSC_VER)
135  unsigned long Index;
136  _BitScanForward64(&Index, Val);
137  return Index;
138 #endif
139  }
140 };
141 #endif
142 #endif
143 } // namespace detail
144 
145 /// Count number of 0's from the least significant bit to the most
146 /// stopping at the first 1.
147 ///
148 /// Only unsigned integral types are allowed.
149 ///
150 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
151 /// valid arguments.
152 template <typename T>
153 unsigned countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
154  static_assert(std::is_unsigned_v<T>,
155  "Only unsigned integral types are allowed.");
156  return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB);
157 }
158 
159 namespace detail {
160 template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
161  static unsigned count(T Val, ZeroBehavior) {
162  if (!Val)
163  return std::numeric_limits<T>::digits;
164 
165  // Bisection method.
166  unsigned ZeroBits = 0;
167  for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
168  T Tmp = Val >> Shift;
169  if (Tmp)
170  Val = Tmp;
171  else
172  ZeroBits |= Shift;
173  }
174  return ZeroBits;
175  }
176 };
177 
178 #if defined(__GNUC__) || defined(_MSC_VER)
179 template <typename T> struct LeadingZerosCounter<T, 4> {
180  static unsigned count(T Val, ZeroBehavior ZB) {
181  if (ZB != ZB_Undefined && Val == 0)
182  return 32;
183 
184 #if __has_builtin(__builtin_clz) || defined(__GNUC__)
185  return __builtin_clz(Val);
186 #elif defined(_MSC_VER)
187  unsigned long Index;
188  _BitScanReverse(&Index, Val);
189  return Index ^ 31;
190 #endif
191  }
192 };
193 
194 #if !defined(_MSC_VER) || defined(_M_X64)
195 template <typename T> struct LeadingZerosCounter<T, 8> {
196  static unsigned count(T Val, ZeroBehavior ZB) {
197  if (ZB != ZB_Undefined && Val == 0)
198  return 64;
199 
200 #if __has_builtin(__builtin_clzll) || defined(__GNUC__)
201  return __builtin_clzll(Val);
202 #elif defined(_MSC_VER)
203  unsigned long Index;
204  _BitScanReverse64(&Index, Val);
205  return Index ^ 63;
206 #endif
207  }
208 };
209 #endif
210 #endif
211 } // namespace detail
212 
213 /// Count number of 0's from the most significant bit to the least
214 /// stopping at the first 1.
215 ///
216 /// Only unsigned integral types are allowed.
217 ///
218 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
219 /// valid arguments.
220 template <typename T>
221 unsigned countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
222  static_assert(std::is_unsigned_v<T>,
223  "Only unsigned integral types are allowed.");
224  return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
225 }
226 
227 /// Get the index of the first set bit starting from the least
228 /// significant bit.
229 ///
230 /// Only unsigned integral types are allowed.
231 ///
232 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
233 /// valid arguments.
234 template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
235  if (ZB == ZB_Max && Val == 0)
237 
238  return countTrailingZeros(Val, ZB_Undefined);
239 }
240 
241 /// Create a bitmask with the N right-most bits set to 1, and all other
242 /// bits set to 0. Only unsigned types are allowed.
243 template <typename T> T maskTrailingOnes(unsigned N) {
244  static_assert(std::is_unsigned<T>::value, "Invalid type!");
245  const unsigned Bits = CHAR_BIT * sizeof(T);
246  assert(N <= Bits && "Invalid bit index");
247  return N == 0 ? 0 : (T(-1) >> (Bits - N));
248 }
249 
250 /// Create a bitmask with the N left-most bits set to 1, and all other
251 /// bits set to 0. Only unsigned types are allowed.
252 template <typename T> T maskLeadingOnes(unsigned N) {
253  return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
254 }
255 
256 /// Create a bitmask with the N right-most bits set to 0, and all other
257 /// bits set to 1. Only unsigned types are allowed.
258 template <typename T> T maskTrailingZeros(unsigned N) {
259  return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
260 }
261 
262 /// Create a bitmask with the N left-most bits set to 0, and all other
263 /// bits set to 1. Only unsigned types are allowed.
264 template <typename T> T maskLeadingZeros(unsigned N) {
265  return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
266 }
267 
268 /// Get the index of the last set bit starting from the least
269 /// significant bit.
270 ///
271 /// Only unsigned integral types are allowed.
272 ///
273 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
274 /// valid arguments.
275 template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
276  if (ZB == ZB_Max && Val == 0)
278 
279  // Use ^ instead of - because both gcc and llvm can remove the associated ^
280  // in the __builtin_clz intrinsic on x86.
281  return countLeadingZeros(Val, ZB_Undefined) ^
282  (std::numeric_limits<T>::digits - 1);
283 }
284 
285 /// Macro compressed bit reversal table for 256 bits.
286 ///
287 /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
288 static const unsigned char BitReverseTable256[256] = {
289 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
290 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
291 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
292  R6(0), R6(2), R6(1), R6(3)
293 #undef R2
294 #undef R4
295 #undef R6
296 };
297 
298 /// Reverse the bits in \p Val.
299 template <typename T> T reverseBits(T Val) {
300 #if __has_builtin(__builtin_bitreverse8)
301  if constexpr (std::is_same_v<T, uint8_t>)
302  return __builtin_bitreverse8(Val);
303 #endif
304 #if __has_builtin(__builtin_bitreverse16)
305  if constexpr (std::is_same_v<T, uint16_t>)
306  return __builtin_bitreverse16(Val);
307 #endif
308 #if __has_builtin(__builtin_bitreverse32)
309  if constexpr (std::is_same_v<T, uint32_t>)
310  return __builtin_bitreverse32(Val);
311 #endif
312 #if __has_builtin(__builtin_bitreverse64)
313  if constexpr (std::is_same_v<T, uint64_t>)
314  return __builtin_bitreverse64(Val);
315 #endif
316 
317  unsigned char in[sizeof(Val)];
318  unsigned char out[sizeof(Val)];
319  std::memcpy(in, &Val, sizeof(Val));
320  for (unsigned i = 0; i < sizeof(Val); ++i)
321  out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
322  std::memcpy(&Val, out, sizeof(Val));
323  return Val;
324 }
325 
326 // NOTE: The following support functions use the _32/_64 extensions instead of
327 // type overloading so that signed and unsigned integers can be used without
328 // ambiguity.
329 
330 /// Return the high 32 bits of a 64 bit value.
331 constexpr inline uint32_t Hi_32(uint64_t Value) {
332  return static_cast<uint32_t>(Value >> 32);
333 }
334 
335 /// Return the low 32 bits of a 64 bit value.
336 constexpr inline uint32_t Lo_32(uint64_t Value) {
337  return static_cast<uint32_t>(Value);
338 }
339 
340 /// Make a 64-bit integer from a high / low pair of 32-bit integers.
341 constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
342  return ((uint64_t)High << 32) | (uint64_t)Low;
343 }
344 
345 /// Checks if an integer fits into the given bit width.
346 template <unsigned N> constexpr inline bool isInt(int64_t x) {
347  if constexpr (N == 8)
348  return static_cast<int8_t>(x) == x;
349  if constexpr (N == 16)
350  return static_cast<int16_t>(x) == x;
351  if constexpr (N == 32)
352  return static_cast<int32_t>(x) == x;
353  if constexpr (N < 64)
354  return -(INT64_C(1) << (N - 1)) <= x && x < (INT64_C(1) << (N - 1));
355  (void)x; // MSVC v19.25 warns that x is unused.
356  return true;
357 }
358 
359 /// Checks if a signed integer is an N bit number shifted left by S.
360 template <unsigned N, unsigned S>
361 constexpr inline bool isShiftedInt(int64_t x) {
362  static_assert(
363  N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
364  static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide.");
365  return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
366 }
367 
368 /// Checks if an unsigned integer fits into the given bit width.
369 template <unsigned N> constexpr inline bool isUInt(uint64_t x) {
370  static_assert(N > 0, "isUInt<0> doesn't make sense");
371  if constexpr (N == 8)
372  return static_cast<uint8_t>(x) == x;
373  if constexpr (N == 16)
374  return static_cast<uint16_t>(x) == x;
375  if constexpr (N == 32)
376  return static_cast<uint32_t>(x) == x;
377  if constexpr (N < 64)
378  return x < (UINT64_C(1) << (N));
379  (void)x; // MSVC v19.25 warns that x is unused.
380  return true;
381 }
382 
383 /// Checks if a unsigned integer is an N bit number shifted left by S.
384 template <unsigned N, unsigned S>
385 constexpr inline bool isShiftedUInt(uint64_t x) {
386  static_assert(
387  N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
388  static_assert(N + S <= 64,
389  "isShiftedUInt<N, S> with N + S > 64 is too wide.");
390  // Per the two static_asserts above, S must be strictly less than 64. So
391  // 1 << S is not undefined behavior.
392  return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
393 }
394 
395 /// Gets the maximum value for a N-bit unsigned integer.
397  assert(N > 0 && N <= 64 && "integer width out of range");
398 
399  // uint64_t(1) << 64 is undefined behavior, so we can't do
400  // (uint64_t(1) << N) - 1
401  // without checking first that N != 64. But this works and doesn't have a
402  // branch.
403  return UINT64_MAX >> (64 - N);
404 }
405 
406 /// Gets the minimum value for a N-bit signed integer.
407 inline int64_t minIntN(int64_t N) {
408  assert(N > 0 && N <= 64 && "integer width out of range");
409 
410  return UINT64_C(1) + ~(UINT64_C(1) << (N - 1));
411 }
412 
413 /// Gets the maximum value for a N-bit signed integer.
414 inline int64_t maxIntN(int64_t N) {
415  assert(N > 0 && N <= 64 && "integer width out of range");
416 
417  // This relies on two's complement wraparound when N == 64, so we convert to
418  // int64_t only at the very end to avoid UB.
419  return (UINT64_C(1) << (N - 1)) - 1;
420 }
421 
422 /// Checks if an unsigned integer fits into the given (dynamic) bit width.
423 inline bool isUIntN(unsigned N, uint64_t x) {
424  return N >= 64 || x <= maxUIntN(N);
425 }
426 
427 /// Checks if an signed integer fits into the given (dynamic) bit width.
428 inline bool isIntN(unsigned N, int64_t x) {
429  return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
430 }
431 
432 /// Return true if the argument is a non-empty sequence of ones starting at the
433 /// least significant bit with the remainder zero (32 bit version).
434 /// Ex. isMask_32(0x0000FFFFU) == true.
435 constexpr inline bool isMask_32(uint32_t Value) {
436  return Value && ((Value + 1) & Value) == 0;
437 }
438 
439 /// Return true if the argument is a non-empty sequence of ones starting at the
440 /// least significant bit with the remainder zero (64 bit version).
441 constexpr inline bool isMask_64(uint64_t Value) {
442  return Value && ((Value + 1) & Value) == 0;
443 }
444 
445 /// Return true if the argument contains a non-empty sequence of ones with the
446 /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
447 constexpr inline bool isShiftedMask_32(uint32_t Value) {
448  return Value && isMask_32((Value - 1) | Value);
449 }
450 
451 /// Return true if the argument contains a non-empty sequence of ones with the
452 /// remainder zero (64 bit version.)
453 constexpr inline bool isShiftedMask_64(uint64_t Value) {
454  return Value && isMask_64((Value - 1) | Value);
455 }
456 
457 /// Return true if the argument is a power of two > 0.
458 /// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
459 constexpr inline bool isPowerOf2_32(uint32_t Value) {
460  return llvm::has_single_bit(Value);
461 }
462 
463 /// Return true if the argument is a power of two > 0 (64 bit edition.)
464 constexpr inline bool isPowerOf2_64(uint64_t Value) {
465  return llvm::has_single_bit(Value);
466 }
467 
468 /// Count the number of ones from the most significant bit to the first
469 /// zero bit.
470 ///
471 /// Ex. countLeadingOnes(0xFF0FFF00) == 8.
472 /// Only unsigned integral types are allowed.
473 ///
474 /// \param ZB the behavior on an input of all ones. Only ZB_Width and
475 /// ZB_Undefined are valid arguments.
476 template <typename T>
478  static_assert(std::is_unsigned_v<T>,
479  "Only unsigned integral types are allowed.");
480  return countLeadingZeros<T>(~Value, ZB);
481 }
482 
483 /// Count the number of ones from the least significant bit to the first
484 /// zero bit.
485 ///
486 /// Ex. countTrailingOnes(0x00FF00FF) == 8.
487 /// Only unsigned integral types are allowed.
488 ///
489 /// \param ZB the behavior on an input of all ones. Only ZB_Width and
490 /// ZB_Undefined are valid arguments.
491 template <typename T>
493  static_assert(std::is_unsigned_v<T>,
494  "Only unsigned integral types are allowed.");
495  return countTrailingZeros<T>(~Value, ZB);
496 }
497 
498 /// Count the number of set bits in a value.
499 /// Ex. countPopulation(0xF000F000) = 8
500 /// Returns 0 if the word is zero.
501 template <typename T>
502 inline unsigned countPopulation(T Value) {
503  static_assert(std::is_unsigned_v<T>,
504  "Only unsigned integral types are allowed.");
505  return (unsigned)llvm::popcount(Value);
506 }
507 
508 /// Return true if the argument contains a non-empty sequence of ones with the
509 /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
510 /// If true, \p MaskIdx will specify the index of the lowest set bit and \p
511 /// MaskLen is updated to specify the length of the mask, else neither are
512 /// updated.
513 inline bool isShiftedMask_32(uint32_t Value, unsigned &MaskIdx,
514  unsigned &MaskLen) {
515  if (!isShiftedMask_32(Value))
516  return false;
517  MaskIdx = countTrailingZeros(Value);
518  MaskLen = countPopulation(Value);
519  return true;
520 }
521 
522 /// Return true if the argument contains a non-empty sequence of ones with the
523 /// remainder zero (64 bit version.) If true, \p MaskIdx will specify the index
524 /// of the lowest set bit and \p MaskLen is updated to specify the length of the
525 /// mask, else neither are updated.
526 inline bool isShiftedMask_64(uint64_t Value, unsigned &MaskIdx,
527  unsigned &MaskLen) {
528  if (!isShiftedMask_64(Value))
529  return false;
530  MaskIdx = countTrailingZeros(Value);
531  MaskLen = countPopulation(Value);
532  return true;
533 }
534 
535 /// Compile time Log2.
536 /// Valid only for positive powers of two.
537 template <size_t kValue> constexpr inline size_t CTLog2() {
538  static_assert(kValue > 0 && llvm::isPowerOf2_64(kValue),
539  "Value is not a valid power of 2");
540  return 1 + CTLog2<kValue / 2>();
541 }
542 
543 template <> constexpr inline size_t CTLog2<1>() { return 0; }
544 
545 /// Return the floor log base 2 of the specified value, -1 if the value is zero.
546 /// (32 bit edition.)
547 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
548 inline unsigned Log2_32(uint32_t Value) {
549  return 31 - countLeadingZeros(Value);
550 }
551 
552 /// Return the floor log base 2 of the specified value, -1 if the value is zero.
553 /// (64 bit edition.)
554 inline unsigned Log2_64(uint64_t Value) {
555  return 63 - countLeadingZeros(Value);
556 }
557 
558 /// Return the ceil log base 2 of the specified value, 32 if the value is zero.
559 /// (32 bit edition).
560 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
561 inline unsigned Log2_32_Ceil(uint32_t Value) {
562  return 32 - countLeadingZeros(Value - 1);
563 }
564 
565 /// Return the ceil log base 2 of the specified value, 64 if the value is zero.
566 /// (64 bit edition.)
567 inline unsigned Log2_64_Ceil(uint64_t Value) {
568  return 64 - countLeadingZeros(Value - 1);
569 }
570 
571 /// This function takes a 64-bit integer and returns the bit equivalent double.
572 inline double BitsToDouble(uint64_t Bits) {
573  static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
574  return llvm::bit_cast<double>(Bits);
575 }
576 
577 /// This function takes a 32-bit integer and returns the bit equivalent float.
578 inline float BitsToFloat(uint32_t Bits) {
579  static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
580  return llvm::bit_cast<float>(Bits);
581 }
582 
583 /// This function takes a double and returns the bit equivalent 64-bit integer.
584 /// Note that copying doubles around changes the bits of NaNs on some hosts,
585 /// notably x86, so this routine cannot be used if these bits are needed.
586 inline uint64_t DoubleToBits(double Double) {
587  static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
588  return llvm::bit_cast<uint64_t>(Double);
589 }
590 
591 /// This function takes a float and returns the bit equivalent 32-bit integer.
592 /// Note that copying floats around changes the bits of NaNs on some hosts,
593 /// notably x86, so this routine cannot be used if these bits are needed.
594 inline uint32_t FloatToBits(float Float) {
595  static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
596  return llvm::bit_cast<uint32_t>(Float);
597 }
598 
599 /// A and B are either alignments or offsets. Return the minimum alignment that
600 /// may be assumed after adding the two together.
601 constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
602  // The largest power of 2 that divides both A and B.
603  //
604  // Replace "-Value" by "1+~Value" in the following commented code to avoid
605  // MSVC warning C4146
606  // return (A | B) & -(A | B);
607  return (A | B) & (1 + ~(A | B));
608 }
609 
610 /// Returns the next power of two (in 64-bits) that is strictly greater than A.
611 /// Returns zero on overflow.
612 constexpr inline uint64_t NextPowerOf2(uint64_t A) {
613  A |= (A >> 1);
614  A |= (A >> 2);
615  A |= (A >> 4);
616  A |= (A >> 8);
617  A |= (A >> 16);
618  A |= (A >> 32);
619  return A + 1;
620 }
621 
622 /// Returns the power of two which is less than or equal to the given value.
623 /// Essentially, it is a floor operation across the domain of powers of two.
625  if (!A) return 0;
626  return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
627 }
628 
629 /// Returns the power of two which is greater than or equal to the given value.
630 /// Essentially, it is a ceil operation across the domain of powers of two.
632  if (!A)
633  return 0;
634  return NextPowerOf2(A - 1);
635 }
636 
637 /// Returns the next integer (mod 2**64) that is greater than or equal to
638 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
639 ///
640 /// Examples:
641 /// \code
642 /// alignTo(5, 8) = 8
643 /// alignTo(17, 8) = 24
644 /// alignTo(~0LL, 8) = 0
645 /// alignTo(321, 255) = 510
646 /// \endcode
648  assert(Align != 0u && "Align can't be 0.");
649  return (Value + Align - 1) / Align * Align;
650 }
651 
653  assert(Align != 0 && (Align & (Align - 1)) == 0 &&
654  "Align must be a power of 2");
655  return (Value + Align - 1) & -Align;
656 }
657 
658 /// If non-zero \p Skew is specified, the return value will be a minimal integer
659 /// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
660 /// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
661 /// Skew mod \p A'. \p Align must be non-zero.
662 ///
663 /// Examples:
664 /// \code
665 /// alignTo(5, 8, 7) = 7
666 /// alignTo(17, 8, 1) = 17
667 /// alignTo(~0LL, 8, 3) = 3
668 /// alignTo(321, 255, 42) = 552
669 /// \endcode
671  assert(Align != 0u && "Align can't be 0.");
672  Skew %= Align;
673  return alignTo(Value - Skew, Align) + Skew;
674 }
675 
676 /// Returns the next integer (mod 2**64) that is greater than or equal to
677 /// \p Value and is a multiple of \c Align. \c Align must be non-zero.
678 template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
679  static_assert(Align != 0u, "Align must be non-zero");
680  return (Value + Align - 1) / Align * Align;
681 }
682 
683 /// Returns the integer ceil(Numerator / Denominator).
684 inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
685  return alignTo(Numerator, Denominator) / Denominator;
686 }
687 
688 /// Returns the integer nearest(Numerator / Denominator).
689 inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) {
690  return (Numerator + (Denominator / 2)) / Denominator;
691 }
692 
693 /// Returns the largest uint64_t less than or equal to \p Value and is
694 /// \p Skew mod \p Align. \p Align must be non-zero
696  assert(Align != 0u && "Align can't be 0.");
697  Skew %= Align;
698  return (Value - Skew) / Align * Align + Skew;
699 }
700 
701 /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
702 /// Requires 0 < B <= 32.
703 template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) {
704  static_assert(B > 0, "Bit width can't be 0.");
705  static_assert(B <= 32, "Bit width out of range.");
706  return int32_t(X << (32 - B)) >> (32 - B);
707 }
708 
709 /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
710 /// Requires 0 < B <= 32.
711 inline int32_t SignExtend32(uint32_t X, unsigned B) {
712  assert(B > 0 && "Bit width can't be 0.");
713  assert(B <= 32 && "Bit width out of range.");
714  return int32_t(X << (32 - B)) >> (32 - B);
715 }
716 
717 /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
718 /// Requires 0 < B <= 64.
719 template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) {
720  static_assert(B > 0, "Bit width can't be 0.");
721  static_assert(B <= 64, "Bit width out of range.");
722  return int64_t(x << (64 - B)) >> (64 - B);
723 }
724 
725 /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
726 /// Requires 0 < B <= 64.
727 inline int64_t SignExtend64(uint64_t X, unsigned B) {
728  assert(B > 0 && "Bit width can't be 0.");
729  assert(B <= 64 && "Bit width out of range.");
730  return int64_t(X << (64 - B)) >> (64 - B);
731 }
732 
733 /// Subtract two unsigned integers, X and Y, of type T and return the absolute
734 /// value of the result.
735 template <typename T>
736 std::enable_if_t<std::is_unsigned<T>::value, T> AbsoluteDifference(T X, T Y) {
737  return X > Y ? (X - Y) : (Y - X);
738 }
739 
740 /// Add two unsigned integers, X and Y, of type T. Clamp the result to the
741 /// maximum representable value of T on overflow. ResultOverflowed indicates if
742 /// the result is larger than the maximum representable value of type T.
743 template <typename T>
744 std::enable_if_t<std::is_unsigned<T>::value, T>
745 SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
746  bool Dummy;
747  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
748  // Hacker's Delight, p. 29
749  T Z = X + Y;
750  Overflowed = (Z < X || Z < Y);
751  if (Overflowed)
753  else
754  return Z;
755 }
756 
757 /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the
758 /// maximum representable value of T on overflow. ResultOverflowed indicates if
759 /// the result is larger than the maximum representable value of type T.
760 template <typename T>
761 std::enable_if_t<std::is_unsigned<T>::value, T>
762 SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
763  bool Dummy;
764  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
765 
766  // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
767  // because it fails for uint16_t (where multiplication can have undefined
768  // behavior due to promotion to int), and requires a division in addition
769  // to the multiplication.
770 
771  Overflowed = false;
772 
773  // Log2(Z) would be either Log2Z or Log2Z + 1.
774  // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
775  // will necessarily be less than Log2Max as desired.
776  int Log2Z = Log2_64(X) + Log2_64(Y);
777  const T Max = std::numeric_limits<T>::max();
778  int Log2Max = Log2_64(Max);
779  if (Log2Z < Log2Max) {
780  return X * Y;
781  }
782  if (Log2Z > Log2Max) {
783  Overflowed = true;
784  return Max;
785  }
786 
787  // We're going to use the top bit, and maybe overflow one
788  // bit past it. Multiply all but the bottom bit then add
789  // that on at the end.
790  T Z = (X >> 1) * Y;
791  if (Z & ~(Max >> 1)) {
792  Overflowed = true;
793  return Max;
794  }
795  Z <<= 1;
796  if (X & 1)
797  return SaturatingAdd(Z, Y, ResultOverflowed);
798 
799  return Z;
800 }
801 
802 /// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to
803 /// the product. Clamp the result to the maximum representable value of T on
804 /// overflow. ResultOverflowed indicates if the result is larger than the
805 /// maximum representable value of type T.
806 template <typename T>
807 std::enable_if_t<std::is_unsigned<T>::value, T>
808 SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
809  bool Dummy;
810  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
811 
812  T Product = SaturatingMultiply(X, Y, &Overflowed);
813  if (Overflowed)
814  return Product;
815 
816  return SaturatingAdd(A, Product, &Overflowed);
817 }
818 
819 /// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
820 extern const float huge_valf;
821 
822 
823 /// Add two signed integers, computing the two's complement truncated result,
824 /// returning true if overflow occurred.
825 template <typename T>
826 std::enable_if_t<std::is_signed<T>::value, T> AddOverflow(T X, T Y, T &Result) {
827 #if __has_builtin(__builtin_add_overflow)
828  return __builtin_add_overflow(X, Y, &Result);
829 #else
830  // Perform the unsigned addition.
831  using U = std::make_unsigned_t<T>;
832  const U UX = static_cast<U>(X);
833  const U UY = static_cast<U>(Y);
834  const U UResult = UX + UY;
835 
836  // Convert to signed.
837  Result = static_cast<T>(UResult);
838 
839  // Adding two positive numbers should result in a positive number.
840  if (X > 0 && Y > 0)
841  return Result <= 0;
842  // Adding two negatives should result in a negative number.
843  if (X < 0 && Y < 0)
844  return Result >= 0;
845  return false;
846 #endif
847 }
848 
849 /// Subtract two signed integers, computing the two's complement truncated
850 /// result, returning true if an overflow ocurred.
851 template <typename T>
852 std::enable_if_t<std::is_signed<T>::value, T> SubOverflow(T X, T Y, T &Result) {
853 #if __has_builtin(__builtin_sub_overflow)
854  return __builtin_sub_overflow(X, Y, &Result);
855 #else
856  // Perform the unsigned addition.
857  using U = std::make_unsigned_t<T>;
858  const U UX = static_cast<U>(X);
859  const U UY = static_cast<U>(Y);
860  const U UResult = UX - UY;
861 
862  // Convert to signed.
863  Result = static_cast<T>(UResult);
864 
865  // Subtracting a positive number from a negative results in a negative number.
866  if (X <= 0 && Y > 0)
867  return Result >= 0;
868  // Subtracting a negative number from a positive results in a positive number.
869  if (X >= 0 && Y < 0)
870  return Result <= 0;
871  return false;
872 #endif
873 }
874 
875 /// Multiply two signed integers, computing the two's complement truncated
876 /// result, returning true if an overflow ocurred.
877 template <typename T>
878 std::enable_if_t<std::is_signed<T>::value, T> MulOverflow(T X, T Y, T &Result) {
879  // Perform the unsigned multiplication on absolute values.
880  using U = std::make_unsigned_t<T>;
881  const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
882  const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y);
883  const U UResult = UX * UY;
884 
885  // Convert to signed.
886  const bool IsNegative = (X < 0) ^ (Y < 0);
887  Result = IsNegative ? (0 - UResult) : UResult;
888 
889  // If any of the args was 0, result is 0 and no overflow occurs.
890  if (UX == 0 || UY == 0)
891  return false;
892 
893  // UX and UY are in [1, 2^n], where n is the number of digits.
894  // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
895  // positive) divided by an argument compares to the other.
896  if (IsNegative)
897  return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
898  else
899  return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
900 }
901 
902 } // End llvm namespace
903 
904 #endif
llvm::SaturatingMultiply
std::enable_if_t< std::is_unsigned< T >::value, T > SaturatingMultiply(T X, T Y, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:762
i
i
Definition: README.txt:29
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:156
llvm::findFirstSet
T findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
Definition: MathExtras.h:234
llvm::numbers::sqrt3f
constexpr float sqrt3f
Definition: MathExtras.h:81
llvm::CTLog2< 1 >
constexpr size_t CTLog2< 1 >()
Definition: MathExtras.h:543
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::detail::LeadingZerosCounter::count
static unsigned count(T Val, ZeroBehavior)
Definition: MathExtras.h:161
llvm::SaturatingAdd
std::enable_if_t< std::is_unsigned< T >::value, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:745
llvm::maskTrailingOnes
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:243
llvm::numbers::sqrt3
constexpr double sqrt3
Definition: MathExtras.h:66
llvm::maskTrailingZeros
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:258
llvm::reverseBits
T reverseBits(T Val)
Reverse the bits in Val.
Definition: MathExtras.h:299
llvm::detail::TrailingZerosCounter::count
static unsigned count(T Val, ZeroBehavior)
Definition: MathExtras.h:88
llvm::CTLog2
constexpr size_t CTLog2()
Compile time Log2.
Definition: MathExtras.h:537
High
uint64_t High
Definition: NVVMIntrRange.cpp:61
llvm::huge_valf
const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
Definition: MathExtras.cpp:28
llvm::detail::TrailingZerosCounter
Definition: MathExtras.h:87
llvm::isShiftedMask_32
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:447
llvm::has_single_bit
constexpr bool has_single_bit(T Value) noexcept
Definition: bit.h:39
Shift
bool Shift
Definition: README.txt:468
llvm::tgtok::Bits
@ Bits
Definition: TGLexer.h:50
llvm::FloatToBits
uint32_t FloatToBits(float Float)
This function takes a float and returns the bit equivalent 32-bit integer.
Definition: MathExtras.h:594
llvm::maxIntN
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h:414
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::numbers::log10e
constexpr double log10e
Definition: MathExtras.h:59
llvm::detail::LeadingZerosCounter
Definition: MathExtras.h:160
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::PowerOf2Ceil
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:631
llvm::ZB_Width
@ ZB_Width
The returned value is numeric_limits<T>::digits.
Definition: MathExtras.h:47
llvm::isPowerOf2_32
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:459
llvm::countLeadingOnes
unsigned countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
Definition: MathExtras.h:477
llvm::isInt
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition: MathExtras.h:346
llvm::Lo_32
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition: MathExtras.h:336
llvm::numbers::log10ef
constexpr float log10ef
Definition: MathExtras.h:74
llvm::ZB_Undefined
@ ZB_Undefined
The returned value is undefined.
Definition: MathExtras.h:43
llvm::BitmaskEnumDetail::Mask
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
llvm::MinAlign
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:601
llvm::numbers::egammaf
constexpr float egammaf
Definition: MathExtras.h:70
llvm::Log2_64
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:554
llvm::numbers::inv_sqrt2
constexpr double inv_sqrt2
Definition: MathExtras.h:65
llvm::PowerOf2Floor
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
Definition: MathExtras.h:624
llvm::popcount
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition: bit.h:78
llvm::isShiftedMask_64
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:453
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::DoubleToBits
uint64_t DoubleToBits(double Double)
This function takes a double and returns the bit equivalent 64-bit integer.
Definition: MathExtras.h:586
UINT64_MAX
#define UINT64_MAX
Definition: DataTypes.h:77
llvm::NextPowerOf2
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:612
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::numbers::log2ef
constexpr float log2ef
Definition: MathExtras.h:73
llvm::alignDown
uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the largest uint64_t less than or equal to Value and is Skew mod Align.
Definition: MathExtras.h:695
llvm::Log2_32
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:548
llvm::numbers::ln10f
constexpr float ln10f
Definition: MathExtras.h:72
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::isUIntN
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:423
in
The object format emitted by the WebAssembly backed is documented in
Definition: README.txt:11
llvm::ThreadPriority::Low
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
llvm::AddOverflow
std::enable_if_t< std::is_signed< T >::value, 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:826
llvm::numbers::inv_sqrt3f
constexpr float inv_sqrt3f
Definition: MathExtras.h:82
llvm::numbers::sqrt2
constexpr double sqrt2
Definition: MathExtras.h:64
Align
uint64_t Align
Definition: ELFObjHandler.cpp:81
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::numbers::inv_sqrt2f
constexpr float inv_sqrt2f
Definition: MathExtras.h:80
llvm::isIntN
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:428
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::Log2_32_Ceil
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:561
llvm::Hi_32
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition: MathExtras.h:331
llvm::Make_64
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:341
llvm::SignExtend32
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:703
llvm::divideCeil
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:684
llvm::countPopulation
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:502
llvm::minIntN
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition: MathExtras.h:407
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1700
Index
uint32_t Index
Definition: ELFObjHandler.cpp:82
uint64_t
llvm::alignToPowerOf2
uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align)
Definition: MathExtras.h:652
llvm::numbers::egamma
constexpr double egamma
Definition: MathExtras.h:55
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:54
llvm::numbers::inv_sqrt3
constexpr double inv_sqrt3
Definition: MathExtras.h:67
llvm::ZB_Max
@ ZB_Max
The returned value is numeric_limits<T>::max()
Definition: MathExtras.h:45
llvm::SaturatingMultiplyAdd
std::enable_if_t< std::is_unsigned< T >::value, 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:808
llvm::countTrailingOnes
unsigned countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
Definition: MathExtras.h:492
llvm::maskLeadingZeros
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:264
llvm::numbers::log2e
constexpr double log2e
Definition: MathExtras.h:58
llvm::Log2_64_Ceil
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:567
numbers
SSE has instructions for doing operations on complex numbers
Definition: README-SSE.txt:22
llvm::isUInt
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition: MathExtras.h:369
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::numbers::inv_pif
constexpr float inv_pif
Definition: MathExtras.h:76
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::isShiftedUInt
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition: MathExtras.h:385
llvm::findLastSet
T findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.
Definition: MathExtras.h:275
llvm::numbers::inv_sqrtpi
constexpr double inv_sqrtpi
Definition: MathExtras.h:63
R6
#define R6(n)
llvm::isMask_32
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:435
llvm::NVPTXISD::Dummy
@ Dummy
Definition: NVPTXISelLowering.h:60
llvm::countTrailingZeros
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: MathExtras.h:153
llvm::BitReverseTable256
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
Definition: MathExtras.h:288
uint32_t
Compiler.h
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::BitsToFloat
float BitsToFloat(uint32_t Bits)
This function takes a 32-bit integer and returns the bit equivalent float.
Definition: MathExtras.h:578
llvm::SignExtend64
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:719
llvm::ZeroBehavior
ZeroBehavior
The behavior an operation has on an input of 0.
Definition: MathExtras.h:41
llvm::numbers::inv_pi
constexpr double inv_pi
Definition: MathExtras.h:61
llvm::numbers::ef
constexpr float ef
Definition: MathExtras.h:69
uint16_t
llvm::numbers::inv_sqrtpif
constexpr float inv_sqrtpif
Definition: MathExtras.h:78
llvm::maskLeadingOnes
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:252
bit.h
llvm::MulOverflow
std::enable_if_t< std::is_signed< T >::value, 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:878
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::isShiftedInt
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition: MathExtras.h:361
x
TODO unsigned x
Definition: README.txt:10
llvm::numbers::phif
constexpr float phif
Definition: MathExtras.h:83
llvm::ARCCC::Z
@ Z
Definition: ARCInfo.h:41
llvm::countLeadingZeros
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: MathExtras.h:221
llvm::SubOverflow
std::enable_if_t< std::is_signed< T >::value, 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:852
llvm::numbers::sqrtpi
constexpr double sqrtpi
Definition: MathExtras.h:62
llvm::BitsToDouble
double BitsToDouble(uint64_t Bits)
This function takes a 64-bit integer and returns the bit equivalent double.
Definition: MathExtras.h:572
llvm::numbers::ln2
constexpr double ln2
Definition: MathExtras.h:56
llvm::numbers::phi
constexpr double phi
Definition: MathExtras.h:68
llvm::numbers::pi
constexpr double pi
Definition: MathExtras.h:60
llvm::numbers::sqrt2f
constexpr float sqrt2f
Definition: MathExtras.h:79
N
#define N
llvm::numbers::sqrtpif
constexpr float sqrtpif
Definition: MathExtras.h:77
llvm::numbers::pif
constexpr float pif
Definition: MathExtras.h:75
llvm::maxUIntN
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition: MathExtras.h:396
llvm::isMask_64
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:441
llvm::isPowerOf2_64
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:464
llvm::numbers::ln2f
constexpr float ln2f
Definition: MathExtras.h:71
llvm::divideNearest
uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator)
Returns the integer nearest(Numerator / Denominator).
Definition: MathExtras.h:689
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::numbers::ln10
constexpr double ln10
Definition: MathExtras.h:57
llvm::AbsoluteDifference
std::enable_if_t< std::is_unsigned< T >::value, T > AbsoluteDifference(T X, T Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result.
Definition: MathExtras.h:736