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