ScaledNumber.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //==- lib/Support/ScaledNumber.cpp - Support for scaled numbers -*- 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. // Implementation of some scaled number algorithms.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/ScaledNumber.h"
  13. #include "llvm/ADT/APFloat.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace llvm;
  18. using namespace llvm::ScaledNumbers;
  19. std::pair<uint64_t, int16_t> ScaledNumbers::multiply64(uint64_t LHS,
  20. uint64_t RHS) {
  21. // Separate into two 32-bit digits (U.L).
  22. auto getU = [](uint64_t N) { return N >> 32; };
  23. auto getL = [](uint64_t N) { return N & UINT32_MAX; };
  24. uint64_t UL = getU(LHS), LL = getL(LHS), UR = getU(RHS), LR = getL(RHS);
  25. // Compute cross products.
  26. uint64_t P1 = UL * UR, P2 = UL * LR, P3 = LL * UR, P4 = LL * LR;
  27. // Sum into two 64-bit digits.
  28. uint64_t Upper = P1, Lower = P4;
  29. auto addWithCarry = [&](uint64_t N) {
  30. uint64_t NewLower = Lower + (getL(N) << 32);
  31. Upper += getU(N) + (NewLower < Lower);
  32. Lower = NewLower;
  33. };
  34. addWithCarry(P2);
  35. addWithCarry(P3);
  36. // Check whether the upper digit is empty.
  37. if (!Upper)
  38. return std::make_pair(Lower, 0);
  39. // Shift as little as possible to maximize precision.
  40. unsigned LeadingZeros = countLeadingZeros(Upper);
  41. int Shift = 64 - LeadingZeros;
  42. if (LeadingZeros)
  43. Upper = Upper << LeadingZeros | Lower >> Shift;
  44. return getRounded(Upper, Shift,
  45. Shift && (Lower & UINT64_C(1) << (Shift - 1)));
  46. }
  47. static uint64_t getHalf(uint64_t N) { return (N >> 1) + (N & 1); }
  48. std::pair<uint32_t, int16_t> ScaledNumbers::divide32(uint32_t Dividend,
  49. uint32_t Divisor) {
  50. assert(Dividend && "expected non-zero dividend");
  51. assert(Divisor && "expected non-zero divisor");
  52. // Use 64-bit math and canonicalize the dividend to gain precision.
  53. uint64_t Dividend64 = Dividend;
  54. int Shift = 0;
  55. if (int Zeros = countLeadingZeros(Dividend64)) {
  56. Shift -= Zeros;
  57. Dividend64 <<= Zeros;
  58. }
  59. uint64_t Quotient = Dividend64 / Divisor;
  60. uint64_t Remainder = Dividend64 % Divisor;
  61. // If Quotient needs to be shifted, leave the rounding to getAdjusted().
  62. if (Quotient > UINT32_MAX)
  63. return getAdjusted<uint32_t>(Quotient, Shift);
  64. // Round based on the value of the next bit.
  65. return getRounded<uint32_t>(Quotient, Shift, Remainder >= getHalf(Divisor));
  66. }
  67. std::pair<uint64_t, int16_t> ScaledNumbers::divide64(uint64_t Dividend,
  68. uint64_t Divisor) {
  69. assert(Dividend && "expected non-zero dividend");
  70. assert(Divisor && "expected non-zero divisor");
  71. // Minimize size of divisor.
  72. int Shift = 0;
  73. if (int Zeros = countTrailingZeros(Divisor)) {
  74. Shift -= Zeros;
  75. Divisor >>= Zeros;
  76. }
  77. // Check for powers of two.
  78. if (Divisor == 1)
  79. return std::make_pair(Dividend, Shift);
  80. // Maximize size of dividend.
  81. if (int Zeros = countLeadingZeros(Dividend)) {
  82. Shift -= Zeros;
  83. Dividend <<= Zeros;
  84. }
  85. // Start with the result of a divide.
  86. uint64_t Quotient = Dividend / Divisor;
  87. Dividend %= Divisor;
  88. // Continue building the quotient with long division.
  89. while (!(Quotient >> 63) && Dividend) {
  90. // Shift Dividend and check for overflow.
  91. bool IsOverflow = Dividend >> 63;
  92. Dividend <<= 1;
  93. --Shift;
  94. // Get the next bit of Quotient.
  95. Quotient <<= 1;
  96. if (IsOverflow || Divisor <= Dividend) {
  97. Quotient |= 1;
  98. Dividend -= Divisor;
  99. }
  100. }
  101. return getRounded(Quotient, Shift, Dividend >= getHalf(Divisor));
  102. }
  103. int ScaledNumbers::compareImpl(uint64_t L, uint64_t R, int ScaleDiff) {
  104. assert(ScaleDiff >= 0 && "wrong argument order");
  105. assert(ScaleDiff < 64 && "numbers too far apart");
  106. uint64_t L_adjusted = L >> ScaleDiff;
  107. if (L_adjusted < R)
  108. return -1;
  109. if (L_adjusted > R)
  110. return 1;
  111. return L > L_adjusted << ScaleDiff ? 1 : 0;
  112. }
  113. static void appendDigit(std::string &Str, unsigned D) {
  114. assert(D < 10);
  115. Str += '0' + D % 10;
  116. }
  117. static void appendNumber(std::string &Str, uint64_t N) {
  118. while (N) {
  119. appendDigit(Str, N % 10);
  120. N /= 10;
  121. }
  122. }
  123. static bool doesRoundUp(char Digit) {
  124. switch (Digit) {
  125. case '5':
  126. case '6':
  127. case '7':
  128. case '8':
  129. case '9':
  130. return true;
  131. default:
  132. return false;
  133. }
  134. }
  135. static std::string toStringAPFloat(uint64_t D, int E, unsigned Precision) {
  136. assert(E >= ScaledNumbers::MinScale);
  137. assert(E <= ScaledNumbers::MaxScale);
  138. // Find a new E, but don't let it increase past MaxScale.
  139. int LeadingZeros = ScaledNumberBase::countLeadingZeros64(D);
  140. int NewE = std::min(ScaledNumbers::MaxScale, E + 63 - LeadingZeros);
  141. int Shift = 63 - (NewE - E);
  142. assert(Shift <= LeadingZeros);
  143. assert(Shift == LeadingZeros || NewE == ScaledNumbers::MaxScale);
  144. assert(Shift >= 0 && Shift < 64 && "undefined behavior");
  145. D <<= Shift;
  146. E = NewE;
  147. // Check for a denormal.
  148. unsigned AdjustedE = E + 16383;
  149. if (!(D >> 63)) {
  150. assert(E == ScaledNumbers::MaxScale);
  151. AdjustedE = 0;
  152. }
  153. // Build the float and print it.
  154. uint64_t RawBits[2] = {D, AdjustedE};
  155. APFloat Float(APFloat::x87DoubleExtended(), APInt(80, RawBits));
  156. SmallVector<char, 24> Chars;
  157. Float.toString(Chars, Precision, 0);
  158. return std::string(Chars.begin(), Chars.end());
  159. }
  160. static std::string stripTrailingZeros(const std::string &Float) {
  161. size_t NonZero = Float.find_last_not_of('0');
  162. assert(NonZero != std::string::npos && "no . in floating point string");
  163. if (Float[NonZero] == '.')
  164. ++NonZero;
  165. return Float.substr(0, NonZero + 1);
  166. }
  167. std::string ScaledNumberBase::toString(uint64_t D, int16_t E, int Width,
  168. unsigned Precision) {
  169. if (!D)
  170. return "0.0";
  171. // Canonicalize exponent and digits.
  172. uint64_t Above0 = 0;
  173. uint64_t Below0 = 0;
  174. uint64_t Extra = 0;
  175. int ExtraShift = 0;
  176. if (E == 0) {
  177. Above0 = D;
  178. } else if (E > 0) {
  179. if (int Shift = std::min(int16_t(countLeadingZeros64(D)), E)) {
  180. D <<= Shift;
  181. E -= Shift;
  182. if (!E)
  183. Above0 = D;
  184. }
  185. } else if (E > -64) {
  186. Above0 = D >> -E;
  187. Below0 = D << (64 + E);
  188. } else if (E == -64) {
  189. // Special case: shift by 64 bits is undefined behavior.
  190. Below0 = D;
  191. } else if (E > -120) {
  192. Below0 = D >> (-E - 64);
  193. Extra = D << (128 + E);
  194. ExtraShift = -64 - E;
  195. }
  196. // Fall back on APFloat for very small and very large numbers.
  197. if (!Above0 && !Below0)
  198. return toStringAPFloat(D, E, Precision);
  199. // Append the digits before the decimal.
  200. std::string Str;
  201. size_t DigitsOut = 0;
  202. if (Above0) {
  203. appendNumber(Str, Above0);
  204. DigitsOut = Str.size();
  205. } else
  206. appendDigit(Str, 0);
  207. std::reverse(Str.begin(), Str.end());
  208. // Return early if there's nothing after the decimal.
  209. if (!Below0)
  210. return Str + ".0";
  211. // Append the decimal and beyond.
  212. Str += '.';
  213. uint64_t Error = UINT64_C(1) << (64 - Width);
  214. // We need to shift Below0 to the right to make space for calculating
  215. // digits. Save the precision we're losing in Extra.
  216. Extra = (Below0 & 0xf) << 56 | (Extra >> 8);
  217. Below0 >>= 4;
  218. size_t SinceDot = 0;
  219. size_t AfterDot = Str.size();
  220. do {
  221. if (ExtraShift) {
  222. --ExtraShift;
  223. Error *= 5;
  224. } else
  225. Error *= 10;
  226. Below0 *= 10;
  227. Extra *= 10;
  228. Below0 += (Extra >> 60);
  229. Extra = Extra & (UINT64_MAX >> 4);
  230. appendDigit(Str, Below0 >> 60);
  231. Below0 = Below0 & (UINT64_MAX >> 4);
  232. if (DigitsOut || Str.back() != '0')
  233. ++DigitsOut;
  234. ++SinceDot;
  235. } while (Error && (Below0 << 4 | Extra >> 60) >= Error / 2 &&
  236. (!Precision || DigitsOut <= Precision || SinceDot < 2));
  237. // Return early for maximum precision.
  238. if (!Precision || DigitsOut <= Precision)
  239. return stripTrailingZeros(Str);
  240. // Find where to truncate.
  241. size_t Truncate =
  242. std::max(Str.size() - (DigitsOut - Precision), AfterDot + 1);
  243. // Check if there's anything to truncate.
  244. if (Truncate >= Str.size())
  245. return stripTrailingZeros(Str);
  246. bool Carry = doesRoundUp(Str[Truncate]);
  247. if (!Carry)
  248. return stripTrailingZeros(Str.substr(0, Truncate));
  249. // Round with the first truncated digit.
  250. for (std::string::reverse_iterator I(Str.begin() + Truncate), E = Str.rend();
  251. I != E; ++I) {
  252. if (*I == '.')
  253. continue;
  254. if (*I == '9') {
  255. *I = '0';
  256. continue;
  257. }
  258. ++*I;
  259. Carry = false;
  260. break;
  261. }
  262. // Add "1" in front if we still need to carry.
  263. return stripTrailingZeros(std::string(Carry, '1') + Str.substr(0, Truncate));
  264. }
  265. raw_ostream &ScaledNumberBase::print(raw_ostream &OS, uint64_t D, int16_t E,
  266. int Width, unsigned Precision) {
  267. return OS << toString(D, E, Width, Precision);
  268. }
  269. void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
  270. print(dbgs(), D, E, Width, 0) << "[" << Width << ":" << D << "*2^" << E
  271. << "]";
  272. }