10b57cec5SDimitry Andric //==- lib/Support/ScaledNumber.cpp - Support for scaled numbers -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implementation of some scaled number algorithms.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/Support/ScaledNumber.h"
140b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h"
150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
160b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
170b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric using namespace llvm::ScaledNumbers;
210b57cec5SDimitry Andric
multiply64(uint64_t LHS,uint64_t RHS)220b57cec5SDimitry Andric std::pair<uint64_t, int16_t> ScaledNumbers::multiply64(uint64_t LHS,
230b57cec5SDimitry Andric uint64_t RHS) {
240b57cec5SDimitry Andric // Separate into two 32-bit digits (U.L).
250b57cec5SDimitry Andric auto getU = [](uint64_t N) { return N >> 32; };
260b57cec5SDimitry Andric auto getL = [](uint64_t N) { return N & UINT32_MAX; };
270b57cec5SDimitry Andric uint64_t UL = getU(LHS), LL = getL(LHS), UR = getU(RHS), LR = getL(RHS);
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric // Compute cross products.
300b57cec5SDimitry Andric uint64_t P1 = UL * UR, P2 = UL * LR, P3 = LL * UR, P4 = LL * LR;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric // Sum into two 64-bit digits.
330b57cec5SDimitry Andric uint64_t Upper = P1, Lower = P4;
340b57cec5SDimitry Andric auto addWithCarry = [&](uint64_t N) {
350b57cec5SDimitry Andric uint64_t NewLower = Lower + (getL(N) << 32);
360b57cec5SDimitry Andric Upper += getU(N) + (NewLower < Lower);
370b57cec5SDimitry Andric Lower = NewLower;
380b57cec5SDimitry Andric };
390b57cec5SDimitry Andric addWithCarry(P2);
400b57cec5SDimitry Andric addWithCarry(P3);
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric // Check whether the upper digit is empty.
430b57cec5SDimitry Andric if (!Upper)
440b57cec5SDimitry Andric return std::make_pair(Lower, 0);
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric // Shift as little as possible to maximize precision.
47*06c3fb27SDimitry Andric unsigned LeadingZeros = llvm::countl_zero(Upper);
480b57cec5SDimitry Andric int Shift = 64 - LeadingZeros;
490b57cec5SDimitry Andric if (LeadingZeros)
500b57cec5SDimitry Andric Upper = Upper << LeadingZeros | Lower >> Shift;
510b57cec5SDimitry Andric return getRounded(Upper, Shift,
520b57cec5SDimitry Andric Shift && (Lower & UINT64_C(1) << (Shift - 1)));
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric
getHalf(uint64_t N)550b57cec5SDimitry Andric static uint64_t getHalf(uint64_t N) { return (N >> 1) + (N & 1); }
560b57cec5SDimitry Andric
divide32(uint32_t Dividend,uint32_t Divisor)570b57cec5SDimitry Andric std::pair<uint32_t, int16_t> ScaledNumbers::divide32(uint32_t Dividend,
580b57cec5SDimitry Andric uint32_t Divisor) {
590b57cec5SDimitry Andric assert(Dividend && "expected non-zero dividend");
600b57cec5SDimitry Andric assert(Divisor && "expected non-zero divisor");
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric // Use 64-bit math and canonicalize the dividend to gain precision.
630b57cec5SDimitry Andric uint64_t Dividend64 = Dividend;
640b57cec5SDimitry Andric int Shift = 0;
65*06c3fb27SDimitry Andric if (int Zeros = llvm::countl_zero(Dividend64)) {
660b57cec5SDimitry Andric Shift -= Zeros;
670b57cec5SDimitry Andric Dividend64 <<= Zeros;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric uint64_t Quotient = Dividend64 / Divisor;
700b57cec5SDimitry Andric uint64_t Remainder = Dividend64 % Divisor;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric // If Quotient needs to be shifted, leave the rounding to getAdjusted().
730b57cec5SDimitry Andric if (Quotient > UINT32_MAX)
740b57cec5SDimitry Andric return getAdjusted<uint32_t>(Quotient, Shift);
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric // Round based on the value of the next bit.
770b57cec5SDimitry Andric return getRounded<uint32_t>(Quotient, Shift, Remainder >= getHalf(Divisor));
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
divide64(uint64_t Dividend,uint64_t Divisor)800b57cec5SDimitry Andric std::pair<uint64_t, int16_t> ScaledNumbers::divide64(uint64_t Dividend,
810b57cec5SDimitry Andric uint64_t Divisor) {
820b57cec5SDimitry Andric assert(Dividend && "expected non-zero dividend");
830b57cec5SDimitry Andric assert(Divisor && "expected non-zero divisor");
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric // Minimize size of divisor.
860b57cec5SDimitry Andric int Shift = 0;
87*06c3fb27SDimitry Andric if (int Zeros = llvm::countr_zero(Divisor)) {
880b57cec5SDimitry Andric Shift -= Zeros;
890b57cec5SDimitry Andric Divisor >>= Zeros;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric // Check for powers of two.
930b57cec5SDimitry Andric if (Divisor == 1)
940b57cec5SDimitry Andric return std::make_pair(Dividend, Shift);
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric // Maximize size of dividend.
97*06c3fb27SDimitry Andric if (int Zeros = llvm::countl_zero(Dividend)) {
980b57cec5SDimitry Andric Shift -= Zeros;
990b57cec5SDimitry Andric Dividend <<= Zeros;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric // Start with the result of a divide.
1030b57cec5SDimitry Andric uint64_t Quotient = Dividend / Divisor;
1040b57cec5SDimitry Andric Dividend %= Divisor;
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // Continue building the quotient with long division.
1070b57cec5SDimitry Andric while (!(Quotient >> 63) && Dividend) {
1080b57cec5SDimitry Andric // Shift Dividend and check for overflow.
1090b57cec5SDimitry Andric bool IsOverflow = Dividend >> 63;
1100b57cec5SDimitry Andric Dividend <<= 1;
1110b57cec5SDimitry Andric --Shift;
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric // Get the next bit of Quotient.
1140b57cec5SDimitry Andric Quotient <<= 1;
1150b57cec5SDimitry Andric if (IsOverflow || Divisor <= Dividend) {
1160b57cec5SDimitry Andric Quotient |= 1;
1170b57cec5SDimitry Andric Dividend -= Divisor;
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric return getRounded(Quotient, Shift, Dividend >= getHalf(Divisor));
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
compareImpl(uint64_t L,uint64_t R,int ScaleDiff)1240b57cec5SDimitry Andric int ScaledNumbers::compareImpl(uint64_t L, uint64_t R, int ScaleDiff) {
1250b57cec5SDimitry Andric assert(ScaleDiff >= 0 && "wrong argument order");
1260b57cec5SDimitry Andric assert(ScaleDiff < 64 && "numbers too far apart");
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric uint64_t L_adjusted = L >> ScaleDiff;
1290b57cec5SDimitry Andric if (L_adjusted < R)
1300b57cec5SDimitry Andric return -1;
1310b57cec5SDimitry Andric if (L_adjusted > R)
1320b57cec5SDimitry Andric return 1;
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric return L > L_adjusted << ScaleDiff ? 1 : 0;
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
appendDigit(std::string & Str,unsigned D)1370b57cec5SDimitry Andric static void appendDigit(std::string &Str, unsigned D) {
1380b57cec5SDimitry Andric assert(D < 10);
1390b57cec5SDimitry Andric Str += '0' + D % 10;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric
appendNumber(std::string & Str,uint64_t N)1420b57cec5SDimitry Andric static void appendNumber(std::string &Str, uint64_t N) {
1430b57cec5SDimitry Andric while (N) {
1440b57cec5SDimitry Andric appendDigit(Str, N % 10);
1450b57cec5SDimitry Andric N /= 10;
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric
doesRoundUp(char Digit)1490b57cec5SDimitry Andric static bool doesRoundUp(char Digit) {
1500b57cec5SDimitry Andric switch (Digit) {
1510b57cec5SDimitry Andric case '5':
1520b57cec5SDimitry Andric case '6':
1530b57cec5SDimitry Andric case '7':
1540b57cec5SDimitry Andric case '8':
1550b57cec5SDimitry Andric case '9':
1560b57cec5SDimitry Andric return true;
1570b57cec5SDimitry Andric default:
1580b57cec5SDimitry Andric return false;
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric
toStringAPFloat(uint64_t D,int E,unsigned Precision)1620b57cec5SDimitry Andric static std::string toStringAPFloat(uint64_t D, int E, unsigned Precision) {
1630b57cec5SDimitry Andric assert(E >= ScaledNumbers::MinScale);
1640b57cec5SDimitry Andric assert(E <= ScaledNumbers::MaxScale);
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric // Find a new E, but don't let it increase past MaxScale.
1670b57cec5SDimitry Andric int LeadingZeros = ScaledNumberBase::countLeadingZeros64(D);
1680b57cec5SDimitry Andric int NewE = std::min(ScaledNumbers::MaxScale, E + 63 - LeadingZeros);
1690b57cec5SDimitry Andric int Shift = 63 - (NewE - E);
1700b57cec5SDimitry Andric assert(Shift <= LeadingZeros);
1710b57cec5SDimitry Andric assert(Shift == LeadingZeros || NewE == ScaledNumbers::MaxScale);
1720b57cec5SDimitry Andric assert(Shift >= 0 && Shift < 64 && "undefined behavior");
1730b57cec5SDimitry Andric D <<= Shift;
1740b57cec5SDimitry Andric E = NewE;
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric // Check for a denormal.
1770b57cec5SDimitry Andric unsigned AdjustedE = E + 16383;
1780b57cec5SDimitry Andric if (!(D >> 63)) {
1790b57cec5SDimitry Andric assert(E == ScaledNumbers::MaxScale);
1800b57cec5SDimitry Andric AdjustedE = 0;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric // Build the float and print it.
1840b57cec5SDimitry Andric uint64_t RawBits[2] = {D, AdjustedE};
1850b57cec5SDimitry Andric APFloat Float(APFloat::x87DoubleExtended(), APInt(80, RawBits));
1860b57cec5SDimitry Andric SmallVector<char, 24> Chars;
1870b57cec5SDimitry Andric Float.toString(Chars, Precision, 0);
1880b57cec5SDimitry Andric return std::string(Chars.begin(), Chars.end());
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric
stripTrailingZeros(const std::string & Float)1910b57cec5SDimitry Andric static std::string stripTrailingZeros(const std::string &Float) {
1920b57cec5SDimitry Andric size_t NonZero = Float.find_last_not_of('0');
1930b57cec5SDimitry Andric assert(NonZero != std::string::npos && "no . in floating point string");
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric if (Float[NonZero] == '.')
1960b57cec5SDimitry Andric ++NonZero;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric return Float.substr(0, NonZero + 1);
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric
toString(uint64_t D,int16_t E,int Width,unsigned Precision)2010b57cec5SDimitry Andric std::string ScaledNumberBase::toString(uint64_t D, int16_t E, int Width,
2020b57cec5SDimitry Andric unsigned Precision) {
2030b57cec5SDimitry Andric if (!D)
2040b57cec5SDimitry Andric return "0.0";
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric // Canonicalize exponent and digits.
2070b57cec5SDimitry Andric uint64_t Above0 = 0;
2080b57cec5SDimitry Andric uint64_t Below0 = 0;
2090b57cec5SDimitry Andric uint64_t Extra = 0;
2100b57cec5SDimitry Andric int ExtraShift = 0;
2110b57cec5SDimitry Andric if (E == 0) {
2120b57cec5SDimitry Andric Above0 = D;
2130b57cec5SDimitry Andric } else if (E > 0) {
2140b57cec5SDimitry Andric if (int Shift = std::min(int16_t(countLeadingZeros64(D)), E)) {
2150b57cec5SDimitry Andric D <<= Shift;
2160b57cec5SDimitry Andric E -= Shift;
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric if (!E)
2190b57cec5SDimitry Andric Above0 = D;
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric } else if (E > -64) {
2220b57cec5SDimitry Andric Above0 = D >> -E;
2230b57cec5SDimitry Andric Below0 = D << (64 + E);
2240b57cec5SDimitry Andric } else if (E == -64) {
2250b57cec5SDimitry Andric // Special case: shift by 64 bits is undefined behavior.
2260b57cec5SDimitry Andric Below0 = D;
2270b57cec5SDimitry Andric } else if (E > -120) {
2280b57cec5SDimitry Andric Below0 = D >> (-E - 64);
2290b57cec5SDimitry Andric Extra = D << (128 + E);
2300b57cec5SDimitry Andric ExtraShift = -64 - E;
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andric // Fall back on APFloat for very small and very large numbers.
2340b57cec5SDimitry Andric if (!Above0 && !Below0)
2350b57cec5SDimitry Andric return toStringAPFloat(D, E, Precision);
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric // Append the digits before the decimal.
2380b57cec5SDimitry Andric std::string Str;
2390b57cec5SDimitry Andric size_t DigitsOut = 0;
2400b57cec5SDimitry Andric if (Above0) {
2410b57cec5SDimitry Andric appendNumber(Str, Above0);
2420b57cec5SDimitry Andric DigitsOut = Str.size();
2430b57cec5SDimitry Andric } else
2440b57cec5SDimitry Andric appendDigit(Str, 0);
2450b57cec5SDimitry Andric std::reverse(Str.begin(), Str.end());
2460b57cec5SDimitry Andric
2470b57cec5SDimitry Andric // Return early if there's nothing after the decimal.
2480b57cec5SDimitry Andric if (!Below0)
2490b57cec5SDimitry Andric return Str + ".0";
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric // Append the decimal and beyond.
2520b57cec5SDimitry Andric Str += '.';
2530b57cec5SDimitry Andric uint64_t Error = UINT64_C(1) << (64 - Width);
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric // We need to shift Below0 to the right to make space for calculating
2560b57cec5SDimitry Andric // digits. Save the precision we're losing in Extra.
2570b57cec5SDimitry Andric Extra = (Below0 & 0xf) << 56 | (Extra >> 8);
2580b57cec5SDimitry Andric Below0 >>= 4;
2590b57cec5SDimitry Andric size_t SinceDot = 0;
2600b57cec5SDimitry Andric size_t AfterDot = Str.size();
2610b57cec5SDimitry Andric do {
2620b57cec5SDimitry Andric if (ExtraShift) {
2630b57cec5SDimitry Andric --ExtraShift;
2640b57cec5SDimitry Andric Error *= 5;
2650b57cec5SDimitry Andric } else
2660b57cec5SDimitry Andric Error *= 10;
2670b57cec5SDimitry Andric
2680b57cec5SDimitry Andric Below0 *= 10;
2690b57cec5SDimitry Andric Extra *= 10;
2700b57cec5SDimitry Andric Below0 += (Extra >> 60);
2710b57cec5SDimitry Andric Extra = Extra & (UINT64_MAX >> 4);
2720b57cec5SDimitry Andric appendDigit(Str, Below0 >> 60);
2730b57cec5SDimitry Andric Below0 = Below0 & (UINT64_MAX >> 4);
2740b57cec5SDimitry Andric if (DigitsOut || Str.back() != '0')
2750b57cec5SDimitry Andric ++DigitsOut;
2760b57cec5SDimitry Andric ++SinceDot;
2770b57cec5SDimitry Andric } while (Error && (Below0 << 4 | Extra >> 60) >= Error / 2 &&
2780b57cec5SDimitry Andric (!Precision || DigitsOut <= Precision || SinceDot < 2));
2790b57cec5SDimitry Andric
2800b57cec5SDimitry Andric // Return early for maximum precision.
2810b57cec5SDimitry Andric if (!Precision || DigitsOut <= Precision)
2820b57cec5SDimitry Andric return stripTrailingZeros(Str);
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric // Find where to truncate.
2850b57cec5SDimitry Andric size_t Truncate =
2860b57cec5SDimitry Andric std::max(Str.size() - (DigitsOut - Precision), AfterDot + 1);
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric // Check if there's anything to truncate.
2890b57cec5SDimitry Andric if (Truncate >= Str.size())
2900b57cec5SDimitry Andric return stripTrailingZeros(Str);
2910b57cec5SDimitry Andric
2920b57cec5SDimitry Andric bool Carry = doesRoundUp(Str[Truncate]);
2930b57cec5SDimitry Andric if (!Carry)
2940b57cec5SDimitry Andric return stripTrailingZeros(Str.substr(0, Truncate));
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric // Round with the first truncated digit.
2970b57cec5SDimitry Andric for (std::string::reverse_iterator I(Str.begin() + Truncate), E = Str.rend();
2980b57cec5SDimitry Andric I != E; ++I) {
2990b57cec5SDimitry Andric if (*I == '.')
3000b57cec5SDimitry Andric continue;
3010b57cec5SDimitry Andric if (*I == '9') {
3020b57cec5SDimitry Andric *I = '0';
3030b57cec5SDimitry Andric continue;
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andric ++*I;
3070b57cec5SDimitry Andric Carry = false;
3080b57cec5SDimitry Andric break;
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric // Add "1" in front if we still need to carry.
3120b57cec5SDimitry Andric return stripTrailingZeros(std::string(Carry, '1') + Str.substr(0, Truncate));
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric
print(raw_ostream & OS,uint64_t D,int16_t E,int Width,unsigned Precision)3150b57cec5SDimitry Andric raw_ostream &ScaledNumberBase::print(raw_ostream &OS, uint64_t D, int16_t E,
3160b57cec5SDimitry Andric int Width, unsigned Precision) {
3170b57cec5SDimitry Andric return OS << toString(D, E, Width, Precision);
3180b57cec5SDimitry Andric }
3190b57cec5SDimitry Andric
dump(uint64_t D,int16_t E,int Width)3200b57cec5SDimitry Andric void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
3210b57cec5SDimitry Andric print(dbgs(), D, E, Width, 0) << "[" << Width << ":" << D << "*2^" << E
3220b57cec5SDimitry Andric << "]";
3230b57cec5SDimitry Andric }
324