1 //===-- String to float conversion utils ------------------------*- 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 #ifndef LIBC_SRC_SUPPORT_STR_TO_FLOAT_H 10 #define LIBC_SRC_SUPPORT_STR_TO_FLOAT_H 11 12 #include "src/__support/CPP/Limits.h" 13 #include "src/__support/FPUtil/FPBits.h" 14 #include "src/__support/ctype_utils.h" 15 #include "src/__support/detailed_powers_of_ten.h" 16 #include "src/__support/high_precision_decimal.h" 17 #include "src/__support/str_to_integer.h" 18 #include <errno.h> 19 20 namespace __llvm_libc { 21 namespace internal { 22 23 // Shifts right and rounds according to the following rules: 24 // 1) If the part being cut off is more than 2^(amountToShift - 1) then round 25 // up 26 // 2) If it is less than that number then round down 27 // 3) If it is exactly that number, then round so that the final number will be 28 // even 29 template <class T> 30 static inline T shiftRightAndRound(T numToShift, unsigned int amountToShift) { 31 T result = numToShift >> amountToShift; 32 T truncated = numToShift & ((1 << amountToShift) - 1); 33 34 if (truncated < (1 << (amountToShift - 1))) { 35 return result; 36 } else if (truncated > (1 << (amountToShift - 1))) { 37 return result + 1; 38 } else { 39 return result + (result & 1); // This rounds towards even. 40 } 41 } 42 43 template <class T> uint32_t leadingZeroes(T inputNumber) { 44 // TODO(michaelrj): investigate the portability of using something like 45 // __builtin_clz for specific types. 46 constexpr uint32_t bitsInT = sizeof(T) * 8; 47 if (inputNumber == 0) { 48 return bitsInT; 49 } 50 uint32_t curGuess = bitsInT / 2; 51 uint32_t rangeSize = bitsInT / 2; 52 // while either shifting by curGuess does not get rid of all of the bits or 53 // shifting by one less also gets rid of all of the bits then we have not 54 // found the first bit. 55 while (((inputNumber >> curGuess) > 0) || 56 ((inputNumber >> (curGuess - 1)) == 0)) { 57 // Binary search for the first set bit 58 rangeSize /= 2; 59 if (rangeSize == 0) { 60 break; 61 } 62 if ((inputNumber >> curGuess) > 0) { 63 curGuess += rangeSize; 64 } else { 65 curGuess -= rangeSize; 66 } 67 } 68 if (inputNumber >> curGuess > 0) { 69 curGuess++; 70 } 71 return bitsInT - curGuess; 72 } 73 74 static inline uint64_t low64(__uint128_t num) { 75 return static_cast<uint64_t>(num & 0xffffffffffffffff); 76 } 77 78 static inline uint64_t high64(__uint128_t num) { 79 return static_cast<uint64_t>(num >> 64); 80 } 81 82 // This Eisel-Lemire implementation is based on the algorithm described in the 83 // paper Number Parsing at a Gigabyte per Second, Software: Practice and 84 // Experience 51 (8), 2021 (https://arxiv.org/abs/2101.11408), as well as the 85 // description by Nigel Tao 86 // (https://nigeltao.github.io/blog/2020/eisel-lemire.html) and the golang 87 // implementation, also by Nigel Tao 88 // (https://github.com/golang/go/blob/release-branch.go1.16/src/strconv/eisel_lemire.go#L25) 89 // for some optimizations as well as handling 32 bit floats. 90 template <class T> 91 static inline bool 92 eiselLemire(typename fputil::FPBits<T>::UIntType mantissa, int32_t exp10, 93 typename fputil::FPBits<T>::UIntType *outputMantissa, 94 uint32_t *outputExp2) { 95 96 using BitsType = typename fputil::FPBits<T>::UIntType; 97 constexpr uint32_t BITS_IN_MANTISSA = sizeof(mantissa) * 8; 98 99 if (sizeof(T) > 8) { // This algorithm cannot handle anything longer than a 100 // double, so we skip straight to the fallback. 101 return false; 102 } 103 104 // Exp10 Range 105 if (exp10 < DETAILED_POWERS_OF_TEN_MIN_EXP_10 || 106 exp10 > DETAILED_POWERS_OF_TEN_MAX_EXP_10) { 107 return false; 108 } 109 110 // Normalization 111 uint32_t clz = leadingZeroes<BitsType>(mantissa); 112 mantissa <<= clz; 113 114 uint32_t exp2 = exp10ToExp2(exp10) + BITS_IN_MANTISSA + 115 fputil::FloatProperties<T>::exponentBias - clz; 116 117 // Multiplication 118 const uint64_t *powerOfTen = 119 DETAILED_POWERS_OF_TEN[exp10 - DETAILED_POWERS_OF_TEN_MIN_EXP_10]; 120 121 __uint128_t firstApprox = static_cast<__uint128_t>(mantissa) * 122 static_cast<__uint128_t>(powerOfTen[1]); 123 124 // Wider Approximation 125 __uint128_t finalApprox; 126 // The halfway constant is used to check if the bits that will be shifted away 127 // intially are all 1. For doubles this is 64 (bitstype size) - 52 (final 128 // mantissa size) - 3 (we shift away the last two bits separately for 129 // accuracy, and the most significant bit is ignored.) = 9. Similarly, it's 6 130 // for floats in this case. 131 const uint64_t halfwayConstant = sizeof(T) == 8 ? 0x1FF : 0x3F; 132 if ((high64(firstApprox) & halfwayConstant) == halfwayConstant && 133 low64(firstApprox) + mantissa < mantissa) { 134 __uint128_t lowBits = static_cast<__uint128_t>(mantissa) * 135 static_cast<__uint128_t>(powerOfTen[0]); 136 __uint128_t secondApprox = 137 firstApprox + static_cast<__uint128_t>(high64(lowBits)); 138 139 if ((high64(secondApprox) & halfwayConstant) == halfwayConstant && 140 low64(secondApprox) + 1 == 0 && low64(lowBits) + mantissa < mantissa) { 141 return false; 142 } 143 finalApprox = secondApprox; 144 } else { 145 finalApprox = firstApprox; 146 } 147 148 // Shifting to 54 bits for doubles and 25 bits for floats 149 BitsType msb = high64(finalApprox) >> (BITS_IN_MANTISSA - 1); 150 BitsType finalMantissa = 151 high64(finalApprox) >> (msb + BITS_IN_MANTISSA - 152 (fputil::FloatProperties<T>::mantissaWidth + 3)); 153 exp2 -= 1 ^ msb; // same as !msb 154 155 // Half-way ambiguity 156 if (low64(finalApprox) == 0 && (high64(finalApprox) & halfwayConstant) == 0 && 157 (finalMantissa & 3) == 1) { 158 return false; 159 } 160 161 // From 54 to 53 bits for doubles and 25 to 24 bits for floats 162 finalMantissa += finalMantissa & 1; 163 finalMantissa >>= 1; 164 if ((finalMantissa >> (fputil::FloatProperties<T>::mantissaWidth + 1)) > 0) { 165 finalMantissa >>= 1; 166 ++exp2; 167 } 168 169 // The if block is equivalent to (but has fewer branches than): 170 // if exp2 <= 0 || exp2 >= 0x7FF { etc } 171 if (exp2 - 1 >= (1 << fputil::FloatProperties<T>::exponentWidth) - 2) { 172 return false; 173 } 174 175 *outputMantissa = finalMantissa; 176 *outputExp2 = exp2; 177 return true; 178 } 179 180 // The nth item in POWERS_OF_TWO represents the greatest power of two less than 181 // 10^n. This tells us how much we can safely shift without overshooting. 182 constexpr uint8_t POWERS_OF_TWO[19] = { 183 0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36, 39, 43, 46, 49, 53, 56, 59, 184 }; 185 constexpr int32_t NUM_POWERS_OF_TWO = 186 sizeof(POWERS_OF_TWO) / sizeof(POWERS_OF_TWO[0]); 187 188 // Takes a mantissa and base 10 exponent and converts it into its closest 189 // floating point type T equivalent. This is the fallback algorithm used when 190 // the Eisel-Lemire algorithm fails, it's slower but more accurate. It's based 191 // on the Simple Decimal Conversion algorithm by Nigel Tao, described at this 192 // link: https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html 193 template <class T> 194 static inline void 195 simpleDecimalConversion(const char *__restrict numStart, 196 typename fputil::FPBits<T>::UIntType *outputMantissa, 197 uint32_t *outputExp2) { 198 199 int32_t exp2 = 0; 200 HighPrecisionDecimal hpd = HighPrecisionDecimal(numStart); 201 202 if (hpd.getNumDigits() == 0) { 203 *outputMantissa = 0; 204 *outputExp2 = 0; 205 return; 206 } 207 208 // If the exponent is too large and can't be represented in this size of 209 // float, return inf. 210 if (hpd.getDecimalPoint() > 0 && 211 exp10ToExp2(hpd.getDecimalPoint() - 1) > 212 static_cast<int64_t>(fputil::FloatProperties<T>::exponentBias)) { 213 *outputMantissa = 0; 214 *outputExp2 = fputil::FPBits<T>::maxExponent; 215 errno = ERANGE; // NOLINT 216 return; 217 } 218 // If the exponent is too small even for a subnormal, return 0. 219 if (hpd.getDecimalPoint() < 0 && 220 exp10ToExp2(-hpd.getDecimalPoint()) > 221 static_cast<int64_t>(fputil::FloatProperties<T>::exponentBias + 222 fputil::FloatProperties<T>::mantissaWidth)) { 223 *outputMantissa = 0; 224 *outputExp2 = 0; 225 errno = ERANGE; // NOLINT 226 return; 227 } 228 229 // Right shift until the number is smaller than 1. 230 while (hpd.getDecimalPoint() > 0) { 231 int32_t shiftAmount = 0; 232 if (hpd.getDecimalPoint() >= NUM_POWERS_OF_TWO) { 233 shiftAmount = 60; 234 } else { 235 shiftAmount = POWERS_OF_TWO[hpd.getDecimalPoint()]; 236 } 237 exp2 += shiftAmount; 238 hpd.shift(-shiftAmount); 239 } 240 241 // Left shift until the number is between 1/2 and 1 242 while (hpd.getDecimalPoint() < 0 || 243 (hpd.getDecimalPoint() == 0 && hpd.getDigits()[0] < 5)) { 244 int32_t shiftAmount = 0; 245 246 if (-hpd.getDecimalPoint() >= NUM_POWERS_OF_TWO) { 247 shiftAmount = 60; 248 } else if (hpd.getDecimalPoint() != 0) { 249 shiftAmount = POWERS_OF_TWO[-hpd.getDecimalPoint()]; 250 } else { // This handles the case of the number being between .1 and .5 251 shiftAmount = 1; 252 } 253 exp2 -= shiftAmount; 254 hpd.shift(shiftAmount); 255 } 256 257 // Left shift once so that the number is between 1 and 2 258 --exp2; 259 hpd.shift(1); 260 261 // Get the biased exponent 262 exp2 += fputil::FloatProperties<T>::exponentBias; 263 264 // Handle the exponent being too large (and return inf). 265 if (exp2 >= fputil::FPBits<T>::maxExponent) { 266 *outputMantissa = 0; 267 *outputExp2 = fputil::FPBits<T>::maxExponent; 268 errno = ERANGE; // NOLINT 269 return; 270 } 271 272 // Shift left to fill the mantissa 273 hpd.shift(fputil::FloatProperties<T>::mantissaWidth); 274 typename fputil::FPBits<T>::UIntType finalMantissa = 275 hpd.roundToIntegerType<typename fputil::FPBits<T>::UIntType>(); 276 277 // Handle subnormals 278 if (exp2 <= 0) { 279 // Shift right until there is a valid exponent 280 while (exp2 < 0) { 281 hpd.shift(-1); 282 ++exp2; 283 } 284 // Shift right one more time to compensate for the left shift to get it 285 // between 1 and 2. 286 hpd.shift(-1); 287 finalMantissa = 288 hpd.roundToIntegerType<typename fputil::FPBits<T>::UIntType>(); 289 290 // Check if by shifting right we've caused this to round to a normal number. 291 if ((finalMantissa >> fputil::FloatProperties<T>::mantissaWidth) != 0) { 292 ++exp2; 293 } 294 } 295 296 // Check if rounding added a bit, and shift down if that's the case. 297 if (finalMantissa == typename fputil::FPBits<T>::UIntType(2) 298 << fputil::FloatProperties<T>::mantissaWidth) { 299 finalMantissa >>= 1; 300 ++exp2; 301 } 302 303 *outputMantissa = finalMantissa; 304 *outputExp2 = exp2; 305 } 306 307 // This class is used for templating the constants for Clinger's Fast Path, 308 // described as a method of approximation in 309 // Clinger WD. How to Read Floating Point Numbers Accurately. SIGPLAN Not 1990 310 // Jun;25(6):92–101. https://doi.org/10.1145/93548.93557. 311 // As well as the additions by Gay that extend the useful range by the number of 312 // exact digits stored by the float type, described in 313 // Gay DM, Correctly rounded binary-decimal and decimal-binary conversions; 314 // 1990. AT&T Bell Laboratories Numerical Analysis Manuscript 90-10. 315 template <class T> class ClingerConsts; 316 317 template <> class ClingerConsts<float> { 318 public: 319 static constexpr float powersOfTenArray[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 320 1e6, 1e7, 1e8, 1e9, 1e10}; 321 static constexpr int32_t exactPowersOfTen = 10; 322 static constexpr int32_t digitsInMantissa = 7; 323 static constexpr float maxExactInt = 16777215.0; 324 }; 325 326 template <> class ClingerConsts<double> { 327 public: 328 static constexpr double powersOfTenArray[] = { 329 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 330 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22}; 331 static constexpr int32_t exactPowersOfTen = 22; 332 static constexpr int32_t digitsInMantissa = 15; 333 static constexpr double maxExactInt = 9007199254740991.0; 334 }; 335 336 // Take an exact mantissa and exponent and attempt to convert it using only 337 // exact floating point arithmetic. This only handles numbers with low 338 // exponents, but handles them quickly. This is an implementation of Clinger's 339 // Fast Path, as described above. 340 template <class T> 341 static inline bool 342 clingerFastPath(typename fputil::FPBits<T>::UIntType mantissa, int32_t exp10, 343 typename fputil::FPBits<T>::UIntType *outputMantissa, 344 uint32_t *outputExp2) { 345 if (mantissa >> fputil::FloatProperties<T>::mantissaWidth > 0) { 346 return false; 347 } 348 349 fputil::FPBits<T> result; 350 T floatMantissa = static_cast<T>(mantissa); 351 352 if (exp10 == 0) { 353 result = fputil::FPBits<T>(floatMantissa); 354 } 355 if (exp10 > 0) { 356 if (exp10 > ClingerConsts<T>::exactPowersOfTen + 357 ClingerConsts<T>::digitsInMantissa) { 358 return false; 359 } 360 if (exp10 > ClingerConsts<T>::exactPowersOfTen) { 361 floatMantissa = 362 floatMantissa * 363 ClingerConsts< 364 T>::powersOfTenArray[exp10 - ClingerConsts<T>::exactPowersOfTen]; 365 exp10 = ClingerConsts<T>::exactPowersOfTen; 366 } 367 if (floatMantissa > ClingerConsts<T>::maxExactInt) { 368 return false; 369 } 370 result = fputil::FPBits<T>(floatMantissa * 371 ClingerConsts<T>::powersOfTenArray[exp10]); 372 } else if (exp10 < 0) { 373 if (-exp10 > ClingerConsts<T>::exactPowersOfTen) { 374 return false; 375 } 376 result = fputil::FPBits<T>(floatMantissa / 377 ClingerConsts<T>::powersOfTenArray[-exp10]); 378 } 379 *outputMantissa = result.getMantissa(); 380 *outputExp2 = result.getUnbiasedExponent(); 381 return true; 382 } 383 384 // Takes a mantissa and base 10 exponent and converts it into its closest 385 // floating point type T equivalient. First we try the Eisel-Lemire algorithm, 386 // then if that fails then we fall back to a more accurate algorithm for 387 // accuracy. The resulting mantissa and exponent are placed in outputMantissa 388 // and outputExp2. 389 template <class T> 390 static inline void 391 decimalExpToFloat(typename fputil::FPBits<T>::UIntType mantissa, int32_t exp10, 392 const char *__restrict numStart, bool truncated, 393 typename fputil::FPBits<T>::UIntType *outputMantissa, 394 uint32_t *outputExp2) { 395 // If the exponent is too large and can't be represented in this size of 396 // float, return inf. These bounds are very loose, but are mostly serving as a 397 // first pass. Some close numbers getting through is okay. 398 if (exp10 > 399 static_cast<int64_t>(fputil::FloatProperties<T>::exponentBias) / 3) { 400 *outputMantissa = 0; 401 *outputExp2 = fputil::FPBits<T>::maxExponent; 402 errno = ERANGE; // NOLINT 403 return; 404 } 405 // If the exponent is too small even for a subnormal, return 0. 406 if (exp10 < 0 && 407 -static_cast<int64_t>(exp10) > 408 static_cast<int64_t>(fputil::FloatProperties<T>::exponentBias + 409 fputil::FloatProperties<T>::mantissaWidth) / 410 2) { 411 *outputMantissa = 0; 412 *outputExp2 = 0; 413 errno = ERANGE; // NOLINT 414 return; 415 } 416 417 if (!truncated) { 418 if (clingerFastPath<T>(mantissa, exp10, outputMantissa, outputExp2)) { 419 return; 420 } 421 } 422 423 // Try Eisel-Lemire 424 if (eiselLemire<T>(mantissa, exp10, outputMantissa, outputExp2)) { 425 if (!truncated) { 426 return; 427 } 428 // If the mantissa is truncated, then the result may be off by the LSB, so 429 // check if rounding the mantissa up changes the result. If not, then it's 430 // safe, else use the fallback. 431 typename fputil::FPBits<T>::UIntType firstMantissa = *outputMantissa; 432 uint32_t firstExp2 = *outputExp2; 433 if (eiselLemire<T>(mantissa + 1, exp10, outputMantissa, outputExp2)) { 434 if (*outputMantissa == firstMantissa && *outputExp2 == firstExp2) { 435 return; 436 } 437 } 438 } 439 440 simpleDecimalConversion<T>(numStart, outputMantissa, outputExp2); 441 442 return; 443 } 444 445 // checks if the next 4 characters of the string pointer are the start of a 446 // hexadecimal floating point number. Does not advance the string pointer. 447 static inline bool is_float_hex_start(const char *__restrict src, 448 const char decimalPoint) { 449 if (!(*src == '0' && (*(src + 1) | 32) == 'x')) { 450 return false; 451 } 452 if (*(src + 2) == decimalPoint) { 453 return isalnum(*(src + 3)) && b36_char_to_int(*(src + 3)) < 16; 454 } else { 455 return isalnum(*(src + 2)) && b36_char_to_int(*(src + 2)) < 16; 456 } 457 } 458 459 // Takes a pointer to a string and a pointer to a string pointer. This function 460 // is used as the backend for all of the string to float functions. 461 template <class T> 462 static inline T strtofloatingpoint(const char *__restrict src, 463 char **__restrict strEnd) { 464 using BitsType = typename fputil::FPBits<T>::UIntType; 465 fputil::FPBits<T> result = fputil::FPBits<T>(); 466 const char *originalSrc = src; 467 bool seenDigit = false; 468 src = first_non_whitespace(src); 469 470 if (*src == '+' || *src == '-') { 471 if (*src == '-') { 472 result.setSign(true); 473 } 474 ++src; 475 } 476 477 static constexpr char DECIMAL_POINT = '.'; 478 static const char *INF_STRING = "infinity"; 479 static const char *NAN_STRING = "nan"; 480 481 bool truncated = false; 482 483 if (isdigit(*src) || *src == DECIMAL_POINT) { // regular number 484 int base = 10; 485 char exponentMarker = 'e'; 486 if (is_float_hex_start(src, DECIMAL_POINT)) { 487 base = 16; 488 src += 2; 489 exponentMarker = 'p'; 490 seenDigit = true; 491 } 492 const char *__restrict numStart = src; 493 bool afterDecimal = false; 494 495 BitsType mantissa = 0; 496 int32_t exponent = 0; 497 498 // The goal for the first step of parsing is to convert the number in src to 499 // the format mantissa * (base ^ exponent) 500 501 constexpr BitsType MANTISSA_MAX = 502 BitsType(1) << (fputil::FloatProperties<T>::mantissaWidth + 503 1); // The extra bit is to give space for the implicit 1 504 const BitsType BITSTYPE_MAX_DIV_BY_BASE = 505 __llvm_libc::cpp::NumericLimits<BitsType>::max() / base; 506 while ((isalnum(*src) || *src == DECIMAL_POINT) && 507 mantissa < BITSTYPE_MAX_DIV_BY_BASE) { 508 if (*src == DECIMAL_POINT && afterDecimal) { 509 break; // this means that *src points to a second decimal point, ending 510 // the number. 511 } else if (*src == DECIMAL_POINT) { 512 afterDecimal = true; 513 ++src; 514 continue; 515 } 516 int digit = b36_char_to_int(*src); 517 if (digit >= base) { 518 break; 519 } 520 521 mantissa = (mantissa * base) + digit; 522 seenDigit = true; 523 if (afterDecimal) { 524 --exponent; 525 } 526 527 ++src; 528 } 529 530 // The second loop is to run through the remaining digits after we've filled 531 // the mantissa. 532 while (isalnum(*src) || *src == DECIMAL_POINT) { 533 if (*src == DECIMAL_POINT && afterDecimal) { 534 break; // this means that *src points to a second decimal point, ending 535 // the number. 536 } else if (*src == DECIMAL_POINT) { 537 afterDecimal = true; 538 ++src; 539 continue; 540 } 541 int digit = b36_char_to_int(*src); 542 if (digit >= base) { 543 break; 544 } 545 546 if (digit > 0) { 547 truncated = true; 548 } 549 550 if (!afterDecimal) { 551 exponent++; 552 } 553 554 ++src; 555 } 556 557 // if our base is 16 then convert the exponent to base 2 558 if (base == 16) { 559 exponent *= 4; 560 } 561 562 if ((*src | 32) == exponentMarker) { 563 if (*(src + 1) == '+' || *(src + 1) == '-' || isdigit(*(src + 1))) { 564 ++src; 565 char *tempStrEnd; 566 int32_t add_to_exponent = strtointeger<int32_t>(src, &tempStrEnd, 10); 567 if (add_to_exponent > 100000) { 568 add_to_exponent = 100000; 569 } else if (add_to_exponent < -100000) { 570 add_to_exponent = -100000; 571 } 572 src += tempStrEnd - src; 573 exponent += add_to_exponent; 574 } 575 } 576 577 if (mantissa == 0) { // if we have a 0, then also 0 the exponent. 578 exponent = 0; 579 } else if (base == 16) { 580 581 // These two loops should normalize the number if we assume the decimal 582 // point is after the bit at mantissaWidth. 583 // For example if type T is a 32 bit float, this should result in a 584 // mantissa with its most significant 1 being at bit 23. 585 while (mantissa < (MANTISSA_MAX >> 1)) { 586 mantissa = mantissa << 1; 587 --exponent; 588 } 589 BitsType mantissaCopy = mantissa; 590 unsigned int amountToShift = 0; 591 while (mantissaCopy > MANTISSA_MAX) { 592 mantissaCopy = mantissaCopy >> 1; 593 ++amountToShift; 594 } 595 exponent += amountToShift; 596 mantissa = shiftRightAndRound(mantissa, amountToShift); 597 598 // Account for the fact that the mantissa represented an integer 599 // previously, but now represents the fractional part of a normalized 600 // number. 601 exponent += fputil::FloatProperties<T>::mantissaWidth; 602 603 int32_t biasedExponent = exponent + fputil::FPBits<T>::exponentBias; 604 if (biasedExponent <= 0) { 605 // handle subnormals here 606 607 // the most mantissa is currently normalized, meaning that the msb is 608 // one bit left of where the decimal point should go. 609 amountToShift = 1; 610 mantissaCopy = mantissa >> 1; 611 while (biasedExponent < 0 && mantissaCopy > 0) { 612 mantissaCopy = mantissaCopy >> 1; 613 ++amountToShift; 614 ++biasedExponent; 615 } 616 // If we cut off any bits to fit this number into a subnormal, then it's 617 // out of range for this size of float. 618 if ((mantissa & ((1 << amountToShift) - 1)) > 0) { 619 errno = ERANGE; // NOLINT 620 } 621 mantissa = shiftRightAndRound(mantissa, amountToShift); 622 if (mantissa == 0) { 623 biasedExponent = 0; 624 } 625 } else if (biasedExponent > result.maxExponent) { 626 // This indicates an overflow, so we make the result INF and set errno. 627 biasedExponent = result.maxExponent; 628 mantissa = 0; 629 errno = ERANGE; // NOLINT 630 } 631 632 result.setUnbiasedExponent(biasedExponent); 633 result.setMantissa(mantissa); 634 } else { // base is 10 635 BitsType outputMantissa = 0; 636 uint32_t outputExponent = 0; 637 decimalExpToFloat<T>(mantissa, exponent, numStart, truncated, 638 &outputMantissa, &outputExponent); 639 result.setMantissa(outputMantissa); 640 result.setUnbiasedExponent(outputExponent); 641 } 642 643 } else if ((*src | 32) == 'n') { // NaN 644 if ((src[1] | 32) == NAN_STRING[1] && (src[2] | 32) == NAN_STRING[2]) { 645 seenDigit = true; 646 src += 3; 647 BitsType NaNMantissa = 0; 648 if (*src == '(') { 649 char *tempSrc = 0; 650 if (isdigit(*(src + 1)) || *(src + 1) == ')') { 651 NaNMantissa = strtointeger<BitsType>(src + 1, &tempSrc, 0); 652 if (*tempSrc != ')') { 653 NaNMantissa = 0; 654 } else { 655 src = tempSrc + 1; 656 } 657 } 658 } 659 NaNMantissa |= fputil::FloatProperties<T>::quietNaNMask; 660 if (result.getSign()) { 661 result = fputil::FPBits<T>(result.buildNaN(NaNMantissa)); 662 result.setSign(true); 663 } else { 664 result.setSign(false); 665 result = fputil::FPBits<T>(result.buildNaN(NaNMantissa)); 666 } 667 } 668 } else if ((*src | 32) == 'i') { // INF 669 if ((src[1] | 32) == INF_STRING[1] && (src[2] | 32) == INF_STRING[2]) { 670 seenDigit = true; 671 if (result.getSign()) 672 result = result.negInf(); 673 else 674 result = result.inf(); 675 if ((src[3] | 32) == INF_STRING[3] && (src[4] | 32) == INF_STRING[4] && 676 (src[5] | 32) == INF_STRING[5] && (src[6] | 32) == INF_STRING[6] && 677 (src[7] | 32) == INF_STRING[7]) { 678 // if the string is "INFINITY" then strEnd needs to be set to src + 8. 679 src += 8; 680 } else { 681 src += 3; 682 } 683 } 684 } 685 if (!seenDigit) { // If there is nothing to actually parse, then return 0. 686 if (strEnd != nullptr) 687 *strEnd = const_cast<char *>(originalSrc); 688 return T(0); 689 } 690 691 if (strEnd != nullptr) 692 *strEnd = const_cast<char *>(src); 693 694 return T(result); 695 } 696 697 } // namespace internal 698 } // namespace __llvm_libc 699 700 #endif // LIBC_SRC_SUPPORT_STR_TO_FLOAT_H 701