ScaledNumber.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //==- lib/Support/ScaledNumber.cpp - Support for scaled numbers -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Implementation of some scaled number algorithms.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/ScaledNumber.h"
  14. using namespace llvm;
  15. using namespace llvm::ScaledNumbers;
  16. std::pair<uint64_t, int16_t> ScaledNumbers::multiply64(uint64_t LHS,
  17. uint64_t RHS) {
  18. // Separate into two 32-bit digits (U.L).
  19. auto getU = [](uint64_t N) { return N >> 32; };
  20. auto getL = [](uint64_t N) { return N & UINT32_MAX; };
  21. uint64_t UL = getU(LHS), LL = getL(LHS), UR = getU(RHS), LR = getL(RHS);
  22. // Compute cross products.
  23. uint64_t P1 = UL * UR, P2 = UL * LR, P3 = LL * UR, P4 = LL * LR;
  24. // Sum into two 64-bit digits.
  25. uint64_t Upper = P1, Lower = P4;
  26. auto addWithCarry = [&](uint64_t N) {
  27. uint64_t NewLower = Lower + (getL(N) << 32);
  28. Upper += getU(N) + (NewLower < Lower);
  29. Lower = NewLower;
  30. };
  31. addWithCarry(P2);
  32. addWithCarry(P3);
  33. // Check whether the upper digit is empty.
  34. if (!Upper)
  35. return std::make_pair(Lower, 0);
  36. // Shift as little as possible to maximize precision.
  37. unsigned LeadingZeros = countLeadingZeros(Upper);
  38. int Shift = 64 - LeadingZeros;
  39. if (LeadingZeros)
  40. Upper = Upper << LeadingZeros | Lower >> Shift;
  41. return getRounded(Upper, Shift,
  42. Shift && (Lower & UINT64_C(1) << (Shift - 1)));
  43. }
  44. static uint64_t getHalf(uint64_t N) { return (N >> 1) + (N & 1); }
  45. std::pair<uint32_t, int16_t> ScaledNumbers::divide32(uint32_t Dividend,
  46. uint32_t Divisor) {
  47. assert(Dividend && "expected non-zero dividend");
  48. assert(Divisor && "expected non-zero divisor");
  49. // Use 64-bit math and canonicalize the dividend to gain precision.
  50. uint64_t Dividend64 = Dividend;
  51. int Shift = 0;
  52. if (int Zeros = countLeadingZeros(Dividend64)) {
  53. Shift -= Zeros;
  54. Dividend64 <<= Zeros;
  55. }
  56. uint64_t Quotient = Dividend64 / Divisor;
  57. uint64_t Remainder = Dividend64 % Divisor;
  58. // If Quotient needs to be shifted, leave the rounding to getAdjusted().
  59. if (Quotient > UINT32_MAX)
  60. return getAdjusted<uint32_t>(Quotient, Shift);
  61. // Round based on the value of the next bit.
  62. return getRounded<uint32_t>(Quotient, Shift, Remainder >= getHalf(Divisor));
  63. }
  64. std::pair<uint64_t, int16_t> ScaledNumbers::divide64(uint64_t Dividend,
  65. uint64_t Divisor) {
  66. assert(Dividend && "expected non-zero dividend");
  67. assert(Divisor && "expected non-zero divisor");
  68. // Minimize size of divisor.
  69. int Shift = 0;
  70. if (int Zeros = countTrailingZeros(Divisor)) {
  71. Shift -= Zeros;
  72. Divisor >>= Zeros;
  73. }
  74. // Check for powers of two.
  75. if (Divisor == 1)
  76. return std::make_pair(Dividend, Shift);
  77. // Maximize size of dividend.
  78. if (int Zeros = countLeadingZeros(Dividend)) {
  79. Shift -= Zeros;
  80. Dividend <<= Zeros;
  81. }
  82. // Start with the result of a divide.
  83. uint64_t Quotient = Dividend / Divisor;
  84. Dividend %= Divisor;
  85. // Continue building the quotient with long division.
  86. while (!(Quotient >> 63) && Dividend) {
  87. // Shift Dividend and check for overflow.
  88. bool IsOverflow = Dividend >> 63;
  89. Dividend <<= 1;
  90. --Shift;
  91. // Get the next bit of Quotient.
  92. Quotient <<= 1;
  93. if (IsOverflow || Divisor <= Dividend) {
  94. Quotient |= 1;
  95. Dividend -= Divisor;
  96. }
  97. }
  98. return getRounded(Quotient, Shift, Dividend >= getHalf(Divisor));
  99. }
  100. int ScaledNumbers::compareImpl(uint64_t L, uint64_t R, int ScaleDiff) {
  101. assert(ScaleDiff >= 0 && "wrong argument order");
  102. assert(ScaleDiff < 64 && "numbers too far apart");
  103. uint64_t L_adjusted = L >> ScaleDiff;
  104. if (L_adjusted < R)
  105. return -1;
  106. if (L_adjusted > R)
  107. return 1;
  108. return L > L_adjusted << ScaleDiff ? 1 : 0;
  109. }