Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

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