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