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