1 //===-- lib/Decimal/decimal-to-binary.cpp ---------------------------------===// 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 #include "big-radix-floating-point.h" 10 #include "flang/Common/bit-population-count.h" 11 #include "flang/Common/leading-zero-bit-count.h" 12 #include "flang/Decimal/binary-floating-point.h" 13 #include "flang/Decimal/decimal.h" 14 #include <cinttypes> 15 #include <cstring> 16 #include <ctype.h> 17 18 namespace Fortran::decimal { 19 20 template <int PREC, int LOG10RADIX> 21 bool BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ParseNumber( 22 const char *&p, bool &inexact) { 23 SetToZero(); 24 while (*p == ' ') { 25 ++p; 26 } 27 const char *q{p}; 28 isNegative_ = *q == '-'; 29 if (*q == '-' || *q == '+') { 30 ++q; 31 } 32 const char *start{q}; 33 while (*q == '0') { 34 ++q; 35 } 36 const char *first{q}; 37 for (; *q >= '0' && *q <= '9'; ++q) { 38 } 39 const char *point{nullptr}; 40 if (*q == '.') { 41 point = q; 42 for (++q; *q >= '0' && *q <= '9'; ++q) { 43 } 44 } 45 if (q == start || (q == start + 1 && *start == '.')) { 46 return false; // require at least one digit 47 } 48 // There's a valid number here; set the reference argument to point to 49 // the first character afterward. 50 p = q; 51 // Strip off trailing zeroes 52 if (point) { 53 while (q[-1] == '0') { 54 --q; 55 } 56 if (q[-1] == '.') { 57 point = nullptr; 58 --q; 59 } 60 } 61 if (!point) { 62 while (q > first && q[-1] == '0') { 63 --q; 64 ++exponent_; 65 } 66 } 67 // Trim any excess digits 68 const char *limit{first + maxDigits * log10Radix + (point != nullptr)}; 69 if (q > limit) { 70 inexact = true; 71 if (point >= limit) { 72 q = point; 73 point = nullptr; 74 } 75 if (!point) { 76 exponent_ += q - limit; 77 } 78 q = limit; 79 } 80 if (point) { 81 exponent_ -= static_cast<int>(q - point - 1); 82 } 83 if (q == first) { 84 exponent_ = 0; // all zeros 85 } 86 // Rack the decimal digits up into big Digits. 87 for (auto times{radix}; q-- > first;) { 88 if (*q != '.') { 89 if (times == radix) { 90 digit_[digits_++] = *q - '0'; 91 times = 10; 92 } else { 93 digit_[digits_ - 1] += times * (*q - '0'); 94 times *= 10; 95 } 96 } 97 } 98 // Look for an optional exponent field. 99 q = p; 100 switch (*q) { 101 case 'e': 102 case 'E': 103 case 'd': 104 case 'D': 105 case 'q': 106 case 'Q': { 107 bool negExpo{*++q == '-'}; 108 if (*q == '-' || *q == '+') { 109 ++q; 110 } 111 if (*q >= '0' && *q <= '9') { 112 int expo{0}; 113 while (*q == '0') { 114 ++q; 115 } 116 const char *expDig{q}; 117 while (*q >= '0' && *q <= '9') { 118 expo = 10 * expo + *q++ - '0'; 119 } 120 if (q >= expDig + 8) { 121 // There's a ridiculous number of nonzero exponent digits. 122 // The decimal->binary conversion routine will cope with 123 // returning 0 or Inf, but we must ensure that "expo" didn't 124 // overflow back around to something legal. 125 expo = 10 * Real::decimalRange; 126 exponent_ = 0; 127 } 128 p = q; // exponent was valid 129 if (negExpo) { 130 exponent_ -= expo; 131 } else { 132 exponent_ += expo; 133 } 134 } 135 } break; 136 default: 137 break; 138 } 139 return true; 140 } 141 142 // This local utility class represents an unrounded nonnegative 143 // binary floating-point value with an unbiased (i.e., signed) 144 // binary exponent, an integer value (not a fraction) with an implied 145 // binary point to its *right*, and some guard bits for rounding. 146 template <int PREC> class IntermediateFloat { 147 public: 148 static constexpr int precision{PREC}; 149 using IntType = common::HostUnsignedIntType<precision>; 150 static constexpr IntType topBit{IntType{1} << (precision - 1)}; 151 static constexpr IntType mask{topBit + (topBit - 1)}; 152 153 IntermediateFloat() {} 154 IntermediateFloat(const IntermediateFloat &) = default; 155 156 // Assumes that exponent_ is valid on entry, and may increment it. 157 // Returns the number of guard_ bits that have been determined. 158 template <typename UINT> bool SetTo(UINT n) { 159 static constexpr int nBits{CHAR_BIT * sizeof n}; 160 if constexpr (precision >= nBits) { 161 value_ = n; 162 guard_ = 0; 163 return 0; 164 } else { 165 int shift{common::BitsNeededFor(n) - precision}; 166 if (shift <= 0) { 167 value_ = n; 168 guard_ = 0; 169 return 0; 170 } else { 171 value_ = n >> shift; 172 exponent_ += shift; 173 n <<= nBits - shift; 174 guard_ = (n >> (nBits - guardBits)) | ((n << guardBits) != 0); 175 return shift; 176 } 177 } 178 } 179 180 void ShiftIn(int bit = 0) { value_ = value_ + value_ + bit; } 181 bool IsFull() const { return value_ >= topBit; } 182 void AdjustExponent(int by) { exponent_ += by; } 183 void SetGuard(int g) { 184 guard_ |= (static_cast<GuardType>(g & 6) << (guardBits - 3)) | (g & 1); 185 } 186 187 ConversionToBinaryResult<PREC> ToBinary( 188 bool isNegative, FortranRounding) const; 189 190 private: 191 static constexpr int guardBits{3}; // guard, round, sticky 192 using GuardType = int; 193 static constexpr GuardType oneHalf{GuardType{1} << (guardBits - 1)}; 194 195 IntType value_{0}; 196 GuardType guard_{0}; 197 int exponent_{0}; 198 }; 199 200 template <int PREC> 201 ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary( 202 bool isNegative, FortranRounding rounding) const { 203 using Binary = BinaryFloatingPointNumber<PREC>; 204 // Create a fraction with a binary point to the left of the integer 205 // value_, and bias the exponent. 206 IntType fraction{value_}; 207 GuardType guard{guard_}; 208 int expo{exponent_ + Binary::exponentBias + (precision - 1)}; 209 while (expo < 1 && (fraction > 0 || guard > oneHalf)) { 210 guard = (guard & 1) | (guard >> 1) | 211 ((static_cast<GuardType>(fraction) & 1) << (guardBits - 1)); 212 fraction >>= 1; 213 ++expo; 214 } 215 int flags{Exact}; 216 if (guard != 0) { 217 flags |= Inexact; 218 } 219 if (fraction == 0 && guard <= oneHalf) { 220 return {Binary{}, static_cast<enum ConversionResultFlags>(flags)}; 221 } 222 // The value is nonzero; normalize it. 223 while (fraction < topBit && expo > 1) { 224 --expo; 225 fraction = fraction * 2 + (guard >> (guardBits - 2)); 226 guard = (((guard >> (guardBits - 2)) & 1) << (guardBits - 1)) | (guard & 1); 227 } 228 // Apply rounding 229 bool incr{false}; 230 switch (rounding) { 231 case RoundNearest: 232 case RoundDefault: 233 incr = guard > oneHalf || (guard == oneHalf && (fraction & 1)); 234 break; 235 case RoundUp: 236 incr = guard != 0 && !isNegative; 237 break; 238 case RoundDown: 239 incr = guard != 0 && isNegative; 240 break; 241 case RoundToZero: 242 break; 243 case RoundCompatible: 244 incr = guard >= oneHalf; 245 break; 246 } 247 if (incr) { 248 if (fraction == mask) { 249 // rounding causes a carry 250 ++expo; 251 fraction = topBit; 252 } else { 253 ++fraction; 254 } 255 } 256 if (expo == 1 && fraction < topBit) { 257 expo = 0; // subnormal 258 } 259 if (expo >= Binary::maxExponent) { 260 expo = Binary::maxExponent; // Inf 261 flags |= Overflow; 262 fraction = 0; 263 } 264 using Raw = typename Binary::RawType; 265 Raw raw = static_cast<Raw>(isNegative) << (Binary::bits - 1); 266 raw |= static_cast<Raw>(expo) << Binary::significandBits; 267 if constexpr (Binary::isImplicitMSB) { 268 fraction &= ~topBit; 269 } 270 raw |= fraction; 271 return {Binary(raw), static_cast<enum ConversionResultFlags>(flags)}; 272 } 273 274 template <int PREC, int LOG10RADIX> 275 ConversionToBinaryResult<PREC> 276 BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary() { 277 // On entry, *this holds a multi-precision integer value in a radix of a 278 // large power of ten. Its radix point is defined to be to the right of its 279 // digits, and "exponent_" is the power of ten by which it is to be scaled. 280 Normalize(); 281 if (digits_ == 0) { // zero value 282 return {Real{SignBit()}}; 283 } 284 // The value is not zero: x = D. * 10.**E 285 // Shift our perspective on the radix (& decimal) point so that 286 // it sits to the *left* of the digits: i.e., x = .D * 10.**E 287 exponent_ += digits_ * log10Radix; 288 // Sanity checks for ridiculous exponents 289 static constexpr int crazy{2 * Real::decimalRange + log10Radix}; 290 if (exponent_ < -crazy) { // underflow to +/-0. 291 return {Real{SignBit()}, Inexact}; 292 } else if (exponent_ > crazy) { // overflow to +/-Inf. 293 return {Real{Infinity()}, Overflow}; 294 } 295 // Apply any negative decimal exponent by multiplication 296 // by a power of two, adjusting the binary exponent to compensate. 297 IntermediateFloat<PREC> f; 298 while (exponent_ < log10Radix) { 299 // x = 0.D * 10.**E * 2.**(f.ex) -> 512 * 0.D * 10.**E * 2.**(f.ex-9) 300 f.AdjustExponent(-9); 301 digitLimit_ = digits_; 302 if (int carry{MultiplyWithoutNormalization<512>()}) { 303 // x = c.D * 10.**E * 2.**(f.ex) -> .cD * 10.**(E+16) * 2.**(f.ex) 304 PushCarry(carry); 305 exponent_ += log10Radix; 306 } 307 } 308 // Apply any positive decimal exponent greater than 309 // is needed to treat the topmost digit as an integer 310 // part by multiplying by 10 or 10000 repeatedly. 311 while (exponent_ > log10Radix) { 312 digitLimit_ = digits_; 313 int carry; 314 if (exponent_ >= log10Radix + 4) { 315 // x = 0.D * 10.**E * 2.**(f.ex) -> 625 * .D * 10.**(E-4) * 2.**(f.ex+4) 316 exponent_ -= 4; 317 carry = MultiplyWithoutNormalization<(5 * 5 * 5 * 5)>(); 318 f.AdjustExponent(4); 319 } else { 320 // x = 0.D * 10.**E * 2.**(f.ex) -> 5 * .D * 10.**(E-1) * 2.**(f.ex+1) 321 --exponent_; 322 carry = MultiplyWithoutNormalization<5>(); 323 f.AdjustExponent(1); 324 } 325 if (carry != 0) { 326 // x = c.D * 10.**E * 2.**(f.ex) -> .cD * 10.**(E+16) * 2.**(f.ex) 327 PushCarry(carry); 328 exponent_ += log10Radix; 329 } 330 } 331 // So exponent_ is now log10Radix, meaning that the 332 // MSD can be taken as an integer part and transferred 333 // to the binary result. 334 // x = .jD * 10.**16 * 2.**(f.ex) -> .D * j * 2.**(f.ex) 335 int guardShift{f.SetTo(digit_[--digits_])}; 336 // Transfer additional bits until the result is normal. 337 digitLimit_ = digits_; 338 while (!f.IsFull()) { 339 // x = ((b.D)/2) * j * 2.**(f.ex) -> .D * (2j + b) * 2.**(f.ex-1) 340 f.AdjustExponent(-1); 341 std::uint32_t carry = MultiplyWithoutNormalization<2>(); 342 f.ShiftIn(carry); 343 } 344 // Get the next few bits for rounding. Allow for some guard bits 345 // that may have already been set in f.SetTo() above. 346 int guard{0}; 347 if (guardShift == 0) { 348 guard = MultiplyWithoutNormalization<4>(); 349 } else if (guardShift == 1) { 350 guard = MultiplyWithoutNormalization<2>(); 351 } 352 guard = guard + guard + !IsZero(); 353 f.SetGuard(guard); 354 return f.ToBinary(isNegative_, rounding_); 355 } 356 357 template <int PREC, int LOG10RADIX> 358 ConversionToBinaryResult<PREC> 359 BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary(const char *&p) { 360 bool inexact{false}; 361 if (ParseNumber(p, inexact)) { 362 auto result{ConvertToBinary()}; 363 if (inexact) { 364 result.flags = 365 static_cast<enum ConversionResultFlags>(result.flags | Inexact); 366 } 367 return result; 368 } else { 369 // Could not parse a decimal floating-point number. p has been 370 // advanced over any leading spaces. 371 if (toupper(p[0]) == 'N' && toupper(p[1]) == 'A' && toupper(p[2]) == 'N') { 372 // NaN 373 p += 3; 374 return {Real{NaN()}}; 375 } else { 376 // Try to parse Inf, maybe with a sign 377 const char *q{p}; 378 isNegative_ = *q == '-'; 379 if (*q == '-' || *q == '+') { 380 ++q; 381 } 382 if (toupper(q[0]) == 'I' && toupper(q[1]) == 'N' && 383 toupper(q[2]) == 'F') { 384 p = q + 3; 385 return {Real{Infinity()}}; 386 } else { 387 // Invalid input 388 return {Real{NaN()}, Invalid}; 389 } 390 } 391 } 392 } 393 394 template <int PREC> 395 ConversionToBinaryResult<PREC> ConvertToBinary( 396 const char *&p, enum FortranRounding rounding) { 397 return BigRadixFloatingPointNumber<PREC>{rounding}.ConvertToBinary(p); 398 } 399 400 template ConversionToBinaryResult<8> ConvertToBinary<8>( 401 const char *&, enum FortranRounding); 402 template ConversionToBinaryResult<11> ConvertToBinary<11>( 403 const char *&, enum FortranRounding); 404 template ConversionToBinaryResult<24> ConvertToBinary<24>( 405 const char *&, enum FortranRounding); 406 template ConversionToBinaryResult<53> ConvertToBinary<53>( 407 const char *&, enum FortranRounding); 408 template ConversionToBinaryResult<64> ConvertToBinary<64>( 409 const char *&, enum FortranRounding); 410 template ConversionToBinaryResult<113> ConvertToBinary<113>( 411 const char *&, enum FortranRounding); 412 413 extern "C" { 414 enum ConversionResultFlags ConvertDecimalToFloat( 415 const char **p, float *f, enum FortranRounding rounding) { 416 auto result{Fortran::decimal::ConvertToBinary<24>(*p, rounding)}; 417 std::memcpy(reinterpret_cast<void *>(f), 418 reinterpret_cast<const void *>(&result.binary), sizeof *f); 419 return result.flags; 420 } 421 enum ConversionResultFlags ConvertDecimalToDouble( 422 const char **p, double *d, enum FortranRounding rounding) { 423 auto result{Fortran::decimal::ConvertToBinary<53>(*p, rounding)}; 424 std::memcpy(reinterpret_cast<void *>(d), 425 reinterpret_cast<const void *>(&result.binary), sizeof *d); 426 return result.flags; 427 } 428 #if __x86_64__ && !defined(_MSC_VER) 429 enum ConversionResultFlags ConvertDecimalToLongDouble( 430 const char **p, long double *ld, enum FortranRounding rounding) { 431 auto result{Fortran::decimal::ConvertToBinary<64>(*p, rounding)}; 432 std::memcpy(reinterpret_cast<void *>(ld), 433 reinterpret_cast<const void *>(&result.binary), sizeof *ld); 434 return result.flags; 435 } 436 #endif 437 } 438 } // namespace Fortran::decimal 439