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 leading_zeroes(T inputNumber) {
24   // TODO(michaelrj): investigate the portability of using something like
25   // __builtin_clz for specific types.
26   constexpr uint32_t BITS_IN_T = sizeof(T) * 8;
27   if (inputNumber == 0) {
28     return BITS_IN_T;
29   }
30   uint32_t cur_guess = BITS_IN_T / 2;
31   uint32_t range_size = BITS_IN_T / 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 >> cur_guess) > 0) ||
36          ((inputNumber >> (cur_guess - 1)) == 0)) {
37     // Binary search for the first set bit
38     range_size /= 2;
39     if (range_size == 0) {
40       break;
41     }
42     if ((inputNumber >> cur_guess) > 0) {
43       cur_guess += range_size;
44     } else {
45       cur_guess -= range_size;
46     }
47   }
48   if (inputNumber >> cur_guess > 0) {
49     cur_guess++;
50   }
51   return BITS_IN_T - cur_guess;
52 }
53 
54 template <> uint32_t inline leading_zeroes<uint32_t>(uint32_t inputNumber) {
55   return inputNumber == 0 ? 32 : __builtin_clz(inputNumber);
56 }
57 
58 template <> uint32_t inline leading_zeroes<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 template <class T> inline void set_implicit_bit(fputil::FPBits<T> &result) {
71   return;
72 }
73 
74 #if defined(SPECIAL_X86_LONG_DOUBLE)
75 template <>
76 inline void set_implicit_bit<long double>(fputil::FPBits<long double> &result) {
77   result.set_implicit_bit(result.get_unbiased_exponent() != 0);
78 }
79 #endif
80 
81 // This Eisel-Lemire implementation is based on the algorithm described in the
82 // paper Number Parsing at a Gigabyte per Second, Software: Practice and
83 // Experience 51 (8), 2021 (https://arxiv.org/abs/2101.11408), as well as the
84 // description by Nigel Tao
85 // (https://nigeltao.github.io/blog/2020/eisel-lemire.html) and the golang
86 // implementation, also by Nigel Tao
87 // (https://github.com/golang/go/blob/release-branch.go1.16/src/strconv/eisel_lemire.go#L25)
88 // for some optimizations as well as handling 32 bit floats.
89 template <class T>
90 static inline bool
91 eisel_lemire(typename fputil::FPBits<T>::UIntType mantissa, int32_t exp10,
92              typename fputil::FPBits<T>::UIntType *outputMantissa,
93              uint32_t *outputExp2) {
94 
95   using BitsType = typename fputil::FPBits<T>::UIntType;
96   constexpr uint32_t BITS_IN_MANTISSA = sizeof(mantissa) * 8;
97 
98   if (sizeof(T) > 8) { // This algorithm cannot handle anything longer than a
99                        // double, so we skip straight to the fallback.
100     return false;
101   }
102 
103   // Exp10 Range
104   if (exp10 < DETAILED_POWERS_OF_TEN_MIN_EXP_10 ||
105       exp10 > DETAILED_POWERS_OF_TEN_MAX_EXP_10) {
106     return false;
107   }
108 
109   // Normalization
110   uint32_t clz = leading_zeroes<BitsType>(mantissa);
111   mantissa <<= clz;
112 
113   uint32_t exp2 = exp10_to_exp2(exp10) + BITS_IN_MANTISSA +
114                   fputil::FloatProperties<T>::EXPONENT_BIAS - clz;
115 
116   // Multiplication
117   const uint64_t *power_of_ten =
118       DETAILED_POWERS_OF_TEN[exp10 - DETAILED_POWERS_OF_TEN_MIN_EXP_10];
119 
120   __uint128_t first_approx = static_cast<__uint128_t>(mantissa) *
121                              static_cast<__uint128_t>(power_of_ten[1]);
122 
123   // Wider Approximation
124   __uint128_t final_approx;
125   // The halfway constant is used to check if the bits that will be shifted away
126   // intially are all 1. For doubles this is 64 (bitstype size) - 52 (final
127   // mantissa size) - 3 (we shift away the last two bits separately for
128   // accuracy, and the most significant bit is ignored.) = 9 bits. Similarly,
129   // it's 6 bits for floats in this case.
130   const uint64_t halfway_constant =
131       (uint64_t(1) << (BITS_IN_MANTISSA -
132                        fputil::FloatProperties<T>::MANTISSA_WIDTH - 3)) -
133       1;
134   if ((high64(first_approx) & halfway_constant) == halfway_constant &&
135       low64(first_approx) + mantissa < mantissa) {
136     __uint128_t low_bits = static_cast<__uint128_t>(mantissa) *
137                            static_cast<__uint128_t>(power_of_ten[0]);
138     __uint128_t second_approx =
139         first_approx + static_cast<__uint128_t>(high64(low_bits));
140 
141     if ((high64(second_approx) & halfway_constant) == halfway_constant &&
142         low64(second_approx) + 1 == 0 &&
143         low64(low_bits) + mantissa < mantissa) {
144       return false;
145     }
146     final_approx = second_approx;
147   } else {
148     final_approx = first_approx;
149   }
150 
151   // Shifting to 54 bits for doubles and 25 bits for floats
152   BitsType msb = high64(final_approx) >> (BITS_IN_MANTISSA - 1);
153   BitsType final_mantissa = high64(final_approx) >>
154                             (msb + BITS_IN_MANTISSA -
155                              (fputil::FloatProperties<T>::MANTISSA_WIDTH + 3));
156   exp2 -= 1 ^ msb; // same as !msb
157 
158   // Half-way ambiguity
159   if (low64(final_approx) == 0 &&
160       (high64(final_approx) & halfway_constant) == 0 &&
161       (final_mantissa & 3) == 1) {
162     return false;
163   }
164 
165   // From 54 to 53 bits for doubles and 25 to 24 bits for floats
166   final_mantissa += final_mantissa & 1;
167   final_mantissa >>= 1;
168   if ((final_mantissa >> (fputil::FloatProperties<T>::MANTISSA_WIDTH + 1)) >
169       0) {
170     final_mantissa >>= 1;
171     ++exp2;
172   }
173 
174   // The if block is equivalent to (but has fewer branches than):
175   //   if exp2 <= 0 || exp2 >= 0x7FF { etc }
176   if (exp2 - 1 >= (1 << fputil::FloatProperties<T>::EXPONENT_WIDTH) - 2) {
177     return false;
178   }
179 
180   *outputMantissa = final_mantissa;
181   *outputExp2 = exp2;
182   return true;
183 }
184 
185 #if !defined(LONG_DOUBLE_IS_DOUBLE)
186 template <>
187 inline bool eisel_lemire<long double>(
188     typename fputil::FPBits<long double>::UIntType mantissa, int32_t exp10,
189     typename fputil::FPBits<long double>::UIntType *outputMantissa,
190     uint32_t *outputExp2) {
191   using BitsType = typename fputil::FPBits<long double>::UIntType;
192   constexpr uint32_t BITS_IN_MANTISSA = sizeof(mantissa) * 8;
193 
194   // Exp10 Range
195   // This doesn't reach very far into the range for long doubles, since it's
196   // sized for doubles and their 11 exponent bits, and not for long doubles and
197   // their 15 exponent bits (max exponent of ~300 for double vs ~5000 for long
198   // double). This is a known tradeoff, and was made because a proper long
199   // double table would be approximately 16 times larger. This would have
200   // significant memory and storage costs all the time to speed up a relatively
201   // uncommon path. In addition the exp10_to_exp2 function only approximates
202   // multiplying by log(10)/log(2), and that approximation may not be accurate
203   // out to the full long double range.
204   if (exp10 < DETAILED_POWERS_OF_TEN_MIN_EXP_10 ||
205       exp10 > DETAILED_POWERS_OF_TEN_MAX_EXP_10) {
206     return false;
207   }
208 
209   // Normalization
210   uint32_t clz = leading_zeroes<BitsType>(mantissa);
211   mantissa <<= clz;
212 
213   uint32_t exp2 = exp10_to_exp2(exp10) + BITS_IN_MANTISSA +
214                   fputil::FloatProperties<long double>::EXPONENT_BIAS - clz;
215 
216   // Multiplication
217   const uint64_t *power_of_ten =
218       DETAILED_POWERS_OF_TEN[exp10 - DETAILED_POWERS_OF_TEN_MIN_EXP_10];
219 
220   // Since the input mantissa is more than 64 bits, we have to multiply with the
221   // full 128 bits of the power of ten to get an approximation with the same
222   // number of significant bits. This means that we only get the one
223   // approximation, and that approximation is 256 bits long.
224   __uint128_t approx_upper = static_cast<__uint128_t>(high64(mantissa)) *
225                              static_cast<__uint128_t>(power_of_ten[1]);
226 
227   __uint128_t approx_middle = static_cast<__uint128_t>(high64(mantissa)) *
228                                   static_cast<__uint128_t>(power_of_ten[0]) +
229                               static_cast<__uint128_t>(low64(mantissa)) *
230                                   static_cast<__uint128_t>(power_of_ten[1]);
231 
232   __uint128_t approx_lower = static_cast<__uint128_t>(low64(mantissa)) *
233                              static_cast<__uint128_t>(power_of_ten[0]);
234 
235   __uint128_t final_approx_lower =
236       approx_lower + (static_cast<__uint128_t>(low64(approx_middle)) << 64);
237   __uint128_t final_approx_upper = approx_upper + high64(approx_middle) +
238                                    (final_approx_lower < approx_lower ? 1 : 0);
239 
240   // The halfway constant is used to check if the bits that will be shifted away
241   // intially are all 1. For 80 bit floats this is 128 (bitstype size) - 64
242   // (final mantissa size) - 3 (we shift away the last two bits separately for
243   // accuracy, and the most significant bit is ignored.) = 61 bits. Similarly,
244   // it's 12 bits for 128 bit floats in this case.
245   constexpr __uint128_t HALFWAY_CONSTANT =
246       (__uint128_t(1) << (BITS_IN_MANTISSA -
247                           fputil::FloatProperties<long double>::MANTISSA_WIDTH -
248                           3)) -
249       1;
250 
251   if ((final_approx_upper & HALFWAY_CONSTANT) == HALFWAY_CONSTANT &&
252       final_approx_lower + mantissa < mantissa) {
253     return false;
254   }
255 
256   // Shifting to 65 bits for 80 bit floats and 113 bits for 128 bit floats
257   BitsType msb = final_approx_upper >> (BITS_IN_MANTISSA - 1);
258   BitsType final_mantissa =
259       final_approx_upper >>
260       (msb + BITS_IN_MANTISSA -
261        (fputil::FloatProperties<long double>::MANTISSA_WIDTH + 3));
262   exp2 -= 1 ^ msb; // same as !msb
263 
264   // Half-way ambiguity
265   if (final_approx_lower == 0 && (final_approx_upper & HALFWAY_CONSTANT) == 0 &&
266       (final_mantissa & 3) == 1) {
267     return false;
268   }
269 
270   // From 65 to 64 bits for 80 bit floats and 113  to 112 bits for 128 bit
271   // floats
272   final_mantissa += final_mantissa & 1;
273   final_mantissa >>= 1;
274   if ((final_mantissa >>
275        (fputil::FloatProperties<long double>::MANTISSA_WIDTH + 1)) > 0) {
276     final_mantissa >>= 1;
277     ++exp2;
278   }
279 
280   // The if block is equivalent to (but has fewer branches than):
281   //   if exp2 <= 0 || exp2 >= MANTISSA_MAX { etc }
282   if (exp2 - 1 >=
283       (1 << fputil::FloatProperties<long double>::EXPONENT_WIDTH) - 2) {
284     return false;
285   }
286 
287   *outputMantissa = final_mantissa;
288   *outputExp2 = exp2;
289   return true;
290 }
291 #endif
292 
293 // The nth item in POWERS_OF_TWO represents the greatest power of two less than
294 // 10^n. This tells us how much we can safely shift without overshooting.
295 constexpr uint8_t POWERS_OF_TWO[19] = {
296     0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36, 39, 43, 46, 49, 53, 56, 59,
297 };
298 constexpr int32_t NUM_POWERS_OF_TWO =
299     sizeof(POWERS_OF_TWO) / sizeof(POWERS_OF_TWO[0]);
300 
301 // Takes a mantissa and base 10 exponent and converts it into its closest
302 // floating point type T equivalent. This is the fallback algorithm used when
303 // the Eisel-Lemire algorithm fails, it's slower but more accurate. It's based
304 // on the Simple Decimal Conversion algorithm by Nigel Tao, described at this
305 // link: https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html
306 template <class T>
307 static inline void
308 simple_decimal_conversion(const char *__restrict numStart,
309                           typename fputil::FPBits<T>::UIntType *outputMantissa,
310                           uint32_t *outputExp2) {
311 
312   int32_t exp2 = 0;
313   HighPrecisionDecimal hpd = HighPrecisionDecimal(numStart);
314 
315   if (hpd.get_num_digits() == 0) {
316     *outputMantissa = 0;
317     *outputExp2 = 0;
318     return;
319   }
320 
321   // If the exponent is too large and can't be represented in this size of
322   // float, return inf.
323   if (hpd.get_decimal_point() > 0 &&
324       exp10_to_exp2(hpd.get_decimal_point() - 1) >
325           static_cast<int64_t>(fputil::FloatProperties<T>::EXPONENT_BIAS)) {
326     *outputMantissa = 0;
327     *outputExp2 = fputil::FPBits<T>::MAX_EXPONENT;
328     errno = ERANGE;
329     return;
330   }
331   // If the exponent is too small even for a subnormal, return 0.
332   if (hpd.get_decimal_point() < 0 &&
333       exp10_to_exp2(-hpd.get_decimal_point()) >
334           static_cast<int64_t>(fputil::FloatProperties<T>::EXPONENT_BIAS +
335                                fputil::FloatProperties<T>::MANTISSA_WIDTH)) {
336     *outputMantissa = 0;
337     *outputExp2 = 0;
338     errno = ERANGE;
339     return;
340   }
341 
342   // Right shift until the number is smaller than 1.
343   while (hpd.get_decimal_point() > 0) {
344     int32_t shift_amount = 0;
345     if (hpd.get_decimal_point() >= NUM_POWERS_OF_TWO) {
346       shift_amount = 60;
347     } else {
348       shift_amount = POWERS_OF_TWO[hpd.get_decimal_point()];
349     }
350     exp2 += shift_amount;
351     hpd.shift(-shift_amount);
352   }
353 
354   // Left shift until the number is between 1/2 and 1
355   while (hpd.get_decimal_point() < 0 ||
356          (hpd.get_decimal_point() == 0 && hpd.get_digits()[0] < 5)) {
357     int32_t shift_amount = 0;
358 
359     if (-hpd.get_decimal_point() >= NUM_POWERS_OF_TWO) {
360       shift_amount = 60;
361     } else if (hpd.get_decimal_point() != 0) {
362       shift_amount = POWERS_OF_TWO[-hpd.get_decimal_point()];
363     } else { // This handles the case of the number being between .1 and .5
364       shift_amount = 1;
365     }
366     exp2 -= shift_amount;
367     hpd.shift(shift_amount);
368   }
369 
370   // Left shift once so that the number is between 1 and 2
371   --exp2;
372   hpd.shift(1);
373 
374   // Get the biased exponent
375   exp2 += fputil::FloatProperties<T>::EXPONENT_BIAS;
376 
377   // Handle the exponent being too large (and return inf).
378   if (exp2 >= fputil::FPBits<T>::MAX_EXPONENT) {
379     *outputMantissa = 0;
380     *outputExp2 = fputil::FPBits<T>::MAX_EXPONENT;
381     errno = ERANGE;
382     return;
383   }
384 
385   // Shift left to fill the mantissa
386   hpd.shift(fputil::FloatProperties<T>::MANTISSA_WIDTH);
387   typename fputil::FPBits<T>::UIntType final_mantissa =
388       hpd.round_to_integer_type<typename fputil::FPBits<T>::UIntType>();
389 
390   // Handle subnormals
391   if (exp2 <= 0) {
392     // Shift right until there is a valid exponent
393     while (exp2 < 0) {
394       hpd.shift(-1);
395       ++exp2;
396     }
397     // Shift right one more time to compensate for the left shift to get it
398     // between 1 and 2.
399     hpd.shift(-1);
400     final_mantissa =
401         hpd.round_to_integer_type<typename fputil::FPBits<T>::UIntType>();
402 
403     // Check if by shifting right we've caused this to round to a normal number.
404     if ((final_mantissa >> fputil::FloatProperties<T>::MANTISSA_WIDTH) != 0) {
405       ++exp2;
406     }
407   }
408 
409   // Check if rounding added a bit, and shift down if that's the case.
410   if (final_mantissa == typename fputil::FPBits<T>::UIntType(2)
411                             << fputil::FloatProperties<T>::MANTISSA_WIDTH) {
412     final_mantissa >>= 1;
413     ++exp2;
414 
415     // Check if this rounding causes exp2 to go out of range and make the result
416     // INF. If this is the case, then finalMantissa and exp2 are already the
417     // correct values for an INF result.
418     if (exp2 >= fputil::FPBits<T>::MAX_EXPONENT) {
419       errno = ERANGE; // NOLINT
420     }
421   }
422 
423   if (exp2 == 0) {
424     errno = ERANGE;
425   }
426 
427   *outputMantissa = final_mantissa;
428   *outputExp2 = exp2;
429 }
430 
431 // This class is used for templating the constants for Clinger's Fast Path,
432 // described as a method of approximation in
433 // Clinger WD. How to Read Floating Point Numbers Accurately. SIGPLAN Not 1990
434 // Jun;25(6):92–101. https://doi.org/10.1145/93548.93557.
435 // As well as the additions by Gay that extend the useful range by the number of
436 // exact digits stored by the float type, described in
437 // Gay DM, Correctly rounded binary-decimal and decimal-binary conversions;
438 // 1990. AT&T Bell Laboratories Numerical Analysis Manuscript 90-10.
439 template <class T> class ClingerConsts;
440 
441 template <> class ClingerConsts<float> {
442 public:
443   static constexpr float POWERS_OF_TEN_ARRAY[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5,
444                                                   1e6, 1e7, 1e8, 1e9, 1e10};
445   static constexpr int32_t EXACT_POWERS_OF_TEN = 10;
446   static constexpr int32_t DIGITS_IN_MANTISSA = 7;
447   static constexpr float MAX_EXACT_INT = 16777215.0;
448 };
449 
450 template <> class ClingerConsts<double> {
451 public:
452   static constexpr double POWERS_OF_TEN_ARRAY[] = {
453       1e0,  1e1,  1e2,  1e3,  1e4,  1e5,  1e6,  1e7,  1e8,  1e9,  1e10, 1e11,
454       1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
455   static constexpr int32_t EXACT_POWERS_OF_TEN = 22;
456   static constexpr int32_t DIGITS_IN_MANTISSA = 15;
457   static constexpr double MAX_EXACT_INT = 9007199254740991.0;
458 };
459 
460 #if defined(LONG_DOUBLE_IS_DOUBLE)
461 template <> class ClingerConsts<long double> {
462 public:
463   static constexpr long double POWERS_OF_TEN_ARRAY[] =
464       ClingerConsts<double>::POWERS_OF_TEN_ARRAY;
465   static constexpr int32_t EXACT_POWERS_OF_TEN =
466       ClingerConsts<double>::EXACT_POWERS_OF_TEN;
467   static constexpr int32_t DIGITS_IN_MANTISSA =
468       ClingerConsts<double>::DIGITS_IN_MANTISSA;
469   static constexpr long double MAX_EXACT_INT =
470       ClingerConsts<double>::MAX_EXACT_INT;
471 };
472 #elif defined(SPECIAL_X86_LONG_DOUBLE)
473 template <> class ClingerConsts<long double> {
474 public:
475   static constexpr long double POWERS_OF_TEN_ARRAY[] = {
476       1e0L,  1e1L,  1e2L,  1e3L,  1e4L,  1e5L,  1e6L,  1e7L,  1e8L,  1e9L,
477       1e10L, 1e11L, 1e12L, 1e13L, 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L,
478       1e20L, 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L};
479   static constexpr int32_t EXACT_POWERS_OF_TEN = 27;
480   static constexpr int32_t DIGITS_IN_MANTISSA = 21;
481   static constexpr long double MAX_EXACT_INT = 18446744073709551615.0L;
482 };
483 #else
484 template <> class ClingerConsts<long double> {
485 public:
486   static constexpr long double POWERS_OF_TEN_ARRAY[] = {
487       1e0L,  1e1L,  1e2L,  1e3L,  1e4L,  1e5L,  1e6L,  1e7L,  1e8L,  1e9L,
488       1e10L, 1e11L, 1e12L, 1e13L, 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L,
489       1e20L, 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L, 1e28L, 1e29L,
490       1e30L, 1e31L, 1e32L, 1e33L, 1e34L, 1e35L, 1e36L, 1e37L, 1e38L, 1e39L,
491       1e40L, 1e41L, 1e42L, 1e43L, 1e44L, 1e45L, 1e46L, 1e47L, 1e48L};
492   static constexpr int32_t EXACT_POWERS_OF_TEN = 48;
493   static constexpr int32_t DIGITS_IN_MANTISSA = 33;
494   static constexpr long double MAX_EXACT_INT =
495       10384593717069655257060992658440191.0L;
496 };
497 #endif
498 
499 // Take an exact mantissa and exponent and attempt to convert it using only
500 // exact floating point arithmetic. This only handles numbers with low
501 // exponents, but handles them quickly. This is an implementation of Clinger's
502 // Fast Path, as described above.
503 template <class T>
504 static inline bool
505 clinger_fast_path(typename fputil::FPBits<T>::UIntType mantissa, int32_t exp10,
506                   typename fputil::FPBits<T>::UIntType *outputMantissa,
507                   uint32_t *outputExp2) {
508   if (mantissa >> fputil::FloatProperties<T>::MANTISSA_WIDTH > 0) {
509     return false;
510   }
511 
512   fputil::FPBits<T> result;
513   T float_mantissa = static_cast<T>(mantissa);
514 
515   if (exp10 == 0) {
516     result = fputil::FPBits<T>(float_mantissa);
517   }
518   if (exp10 > 0) {
519     if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN +
520                     ClingerConsts<T>::DIGITS_IN_MANTISSA) {
521       return false;
522     }
523     if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
524       float_mantissa = float_mantissa *
525                        ClingerConsts<T>::POWERS_OF_TEN_ARRAY
526                            [exp10 - ClingerConsts<T>::EXACT_POWERS_OF_TEN];
527       exp10 = ClingerConsts<T>::EXACT_POWERS_OF_TEN;
528     }
529     if (float_mantissa > ClingerConsts<T>::MAX_EXACT_INT) {
530       return false;
531     }
532     result = fputil::FPBits<T>(float_mantissa *
533                                ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
534   } else if (exp10 < 0) {
535     if (-exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
536       return false;
537     }
538     result = fputil::FPBits<T>(float_mantissa /
539                                ClingerConsts<T>::POWERS_OF_TEN_ARRAY[-exp10]);
540   }
541   *outputMantissa = result.get_mantissa();
542   *outputExp2 = result.get_unbiased_exponent();
543   return true;
544 }
545 
546 // Takes a mantissa and base 10 exponent and converts it into its closest
547 // floating point type T equivalient. First we try the Eisel-Lemire algorithm,
548 // then if that fails then we fall back to a more accurate algorithm for
549 // accuracy. The resulting mantissa and exponent are placed in outputMantissa
550 // and outputExp2.
551 template <class T>
552 static inline void
553 decimal_exp_to_float(typename fputil::FPBits<T>::UIntType mantissa,
554                      int32_t exp10, const char *__restrict numStart,
555                      bool truncated,
556                      typename fputil::FPBits<T>::UIntType *outputMantissa,
557                      uint32_t *outputExp2) {
558   // If the exponent is too large and can't be represented in this size of
559   // float, return inf. These bounds are very loose, but are mostly serving as a
560   // first pass. Some close numbers getting through is okay.
561   if (exp10 >
562       static_cast<int64_t>(fputil::FloatProperties<T>::EXPONENT_BIAS) / 3) {
563     *outputMantissa = 0;
564     *outputExp2 = fputil::FPBits<T>::MAX_EXPONENT;
565     errno = ERANGE;
566     return;
567   }
568   // If the exponent is too small even for a subnormal, return 0.
569   if (exp10 < 0 &&
570       -static_cast<int64_t>(exp10) >
571           static_cast<int64_t>(fputil::FloatProperties<T>::EXPONENT_BIAS +
572                                fputil::FloatProperties<T>::MANTISSA_WIDTH) /
573               2) {
574     *outputMantissa = 0;
575     *outputExp2 = 0;
576     errno = ERANGE;
577     return;
578   }
579 
580   if (!truncated) {
581     if (clinger_fast_path<T>(mantissa, exp10, outputMantissa, outputExp2)) {
582       return;
583     }
584   }
585 
586   // Try Eisel-Lemire
587   if (eisel_lemire<T>(mantissa, exp10, outputMantissa, outputExp2)) {
588     if (!truncated) {
589       return;
590     }
591     // If the mantissa is truncated, then the result may be off by the LSB, so
592     // check if rounding the mantissa up changes the result. If not, then it's
593     // safe, else use the fallback.
594     typename fputil::FPBits<T>::UIntType first_mantissa = *outputMantissa;
595     uint32_t first_exp2 = *outputExp2;
596     if (eisel_lemire<T>(mantissa + 1, exp10, outputMantissa, outputExp2)) {
597       if (*outputMantissa == first_mantissa && *outputExp2 == first_exp2) {
598         return;
599       }
600     }
601   }
602 
603   simple_decimal_conversion<T>(numStart, outputMantissa, outputExp2);
604 
605   return;
606 }
607 
608 // Takes a mantissa and base 2 exponent and converts it into its closest
609 // floating point type T equivalient. Since the exponent is already in the right
610 // form, this is mostly just shifting and rounding. This is used for hexadecimal
611 // numbers since a base 16 exponent multiplied by 4 is the base 2 exponent.
612 template <class T>
613 static inline void
614 binary_exp_to_float(typename fputil::FPBits<T>::UIntType mantissa, int32_t exp2,
615                     bool truncated,
616                     typename fputil::FPBits<T>::UIntType *outputMantissa,
617                     uint32_t *outputExp2) {
618   using BitsType = typename fputil::FPBits<T>::UIntType;
619 
620   // This is the number of leading zeroes a properly normalized float of type T
621   // should have.
622   constexpr int32_t NUMBITS = sizeof(BitsType) * 8;
623   constexpr int32_t INF_EXP =
624       (1 << fputil::FloatProperties<T>::EXPONENT_WIDTH) - 1;
625 
626   // Normalization step 1: Bring the leading bit to the highest bit of BitsType.
627   uint32_t amount_to_shift_left = leading_zeroes<BitsType>(mantissa);
628   mantissa <<= amount_to_shift_left;
629 
630   // Keep exp2 representing the exponent of the lowest bit of BitsType.
631   exp2 -= amount_to_shift_left;
632 
633   // biasedExponent represents the biased exponent of the most significant bit.
634   int32_t biased_exponent =
635       exp2 + NUMBITS + fputil::FPBits<T>::EXPONENT_BIAS - 1;
636 
637   // Handle numbers that're too large and get squashed to inf
638   if (biased_exponent >= INF_EXP) {
639     // This indicates an overflow, so we make the result INF and set errno.
640     *outputExp2 = (1 << fputil::FloatProperties<T>::EXPONENT_WIDTH) - 1;
641     *outputMantissa = 0;
642     errno = ERANGE;
643     return;
644   }
645 
646   uint32_t amount_to_shift_right =
647       NUMBITS - fputil::FloatProperties<T>::MANTISSA_WIDTH - 1;
648 
649   // Handle subnormals.
650   if (biased_exponent <= 0) {
651     amount_to_shift_right += 1 - biased_exponent;
652     biased_exponent = 0;
653 
654     if (amount_to_shift_right > NUMBITS) {
655       // Return 0 if the exponent is too small.
656       *outputMantissa = 0;
657       *outputExp2 = 0;
658       errno = ERANGE;
659       return;
660     }
661   }
662 
663   BitsType round_bit_mask = BitsType(1) << (amount_to_shift_right - 1);
664   BitsType sticky_mask = round_bit_mask - 1;
665   bool round_bit = mantissa & round_bit_mask;
666   bool sticky_bit = static_cast<bool>(mantissa & sticky_mask) || truncated;
667 
668   if (amount_to_shift_right < NUMBITS) {
669     // Shift the mantissa and clear the implicit bit.
670     mantissa >>= amount_to_shift_right;
671     mantissa &= fputil::FloatProperties<T>::MANTISSA_MASK;
672   } else {
673     mantissa = 0;
674   }
675   bool least_significant_bit = mantissa & BitsType(1);
676   // Perform rounding-to-nearest, tie-to-even.
677   if (round_bit && (least_significant_bit || sticky_bit)) {
678     ++mantissa;
679   }
680 
681   if (mantissa > fputil::FloatProperties<T>::MANTISSA_MASK) {
682     // Rounding causes the exponent to increase.
683     ++biased_exponent;
684 
685     if (biased_exponent == INF_EXP) {
686       errno = ERANGE;
687     }
688   }
689 
690   if (biased_exponent == 0) {
691     errno = ERANGE;
692   }
693 
694   *outputMantissa = mantissa & fputil::FloatProperties<T>::MANTISSA_MASK;
695   *outputExp2 = biased_exponent;
696 }
697 
698 // checks if the next 4 characters of the string pointer are the start of a
699 // hexadecimal floating point number. Does not advance the string pointer.
700 static inline bool is_float_hex_start(const char *__restrict src,
701                                       const char decimalPoint) {
702   if (!(*src == '0' && (*(src + 1) | 32) == 'x')) {
703     return false;
704   }
705   if (*(src + 2) == decimalPoint) {
706     return isalnum(*(src + 3)) && b36_char_to_int(*(src + 3)) < 16;
707   } else {
708     return isalnum(*(src + 2)) && b36_char_to_int(*(src + 2)) < 16;
709   }
710 }
711 
712 // Takes the start of a string representing a decimal float, as well as the
713 // local decimalPoint. It returns if it suceeded in parsing any digits, and if
714 // the return value is true then the outputs are pointer to the end of the
715 // number, and the mantissa and exponent for the closest float T representation.
716 // If the return value is false, then it is assumed that there is no number
717 // here.
718 template <class T>
719 static inline bool
720 decimal_string_to_float(const char *__restrict src, const char DECIMAL_POINT,
721                         char **__restrict strEnd,
722                         typename fputil::FPBits<T>::UIntType *outputMantissa,
723                         uint32_t *outputExponent) {
724   using BitsType = typename fputil::FPBits<T>::UIntType;
725   constexpr uint32_t BASE = 10;
726   constexpr char EXPONENT_MARKER = 'e';
727 
728   const char *__restrict num_start = src;
729   bool truncated = false;
730   bool seen_digit = false;
731   bool after_decimal = false;
732   BitsType mantissa = 0;
733   int32_t exponent = 0;
734 
735   // The goal for the first step of parsing is to convert the number in src to
736   // the format mantissa * (base ^ exponent)
737 
738   // The loop fills the mantissa with as many digits as it can hold
739   const BitsType bitstype_max_div_by_base =
740       __llvm_libc::cpp::NumericLimits<BitsType>::max() / BASE;
741   while (true) {
742     if (isdigit(*src)) {
743       uint32_t digit = *src - '0';
744       seen_digit = true;
745 
746       if (mantissa < bitstype_max_div_by_base) {
747         mantissa = (mantissa * BASE) + digit;
748         if (after_decimal) {
749           --exponent;
750         }
751       } else {
752         if (digit > 0)
753           truncated = true;
754         if (!after_decimal)
755           ++exponent;
756       }
757 
758       ++src;
759       continue;
760     }
761     if (*src == DECIMAL_POINT) {
762       if (after_decimal) {
763         break; // this means that *src points to a second decimal point, ending
764                // the number.
765       }
766       after_decimal = true;
767       ++src;
768       continue;
769     }
770     // The character is neither a digit nor a decimal point.
771     break;
772   }
773 
774   if (!seen_digit)
775     return false;
776 
777   if ((*src | 32) == EXPONENT_MARKER) {
778     if (*(src + 1) == '+' || *(src + 1) == '-' || isdigit(*(src + 1))) {
779       ++src;
780       char *temp_str_end;
781       int32_t add_to_exponent = strtointeger<int32_t>(src, &temp_str_end, 10);
782       if (add_to_exponent > 100000)
783         add_to_exponent = 100000;
784       else if (add_to_exponent < -100000)
785         add_to_exponent = -100000;
786 
787       src = temp_str_end;
788       exponent += add_to_exponent;
789     }
790   }
791 
792   *strEnd = const_cast<char *>(src);
793   if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
794     *outputMantissa = 0;
795     *outputExponent = 0;
796   } else {
797     decimal_exp_to_float<T>(mantissa, exponent, num_start, truncated,
798                             outputMantissa, outputExponent);
799   }
800   return true;
801 }
802 
803 // Takes the start of a string representing a hexadecimal float, as well as the
804 // local decimal point. It returns if it suceeded in parsing any digits, and if
805 // the return value is true then the outputs are pointer to the end of the
806 // number, and the mantissa and exponent for the closest float T representation.
807 // If the return value is false, then it is assumed that there is no number
808 // here.
809 template <class T>
810 static inline bool hexadecimal_string_to_float(
811     const char *__restrict src, const char DECIMAL_POINT,
812     char **__restrict strEnd,
813     typename fputil::FPBits<T>::UIntType *outputMantissa,
814     uint32_t *outputExponent) {
815   using BitsType = typename fputil::FPBits<T>::UIntType;
816   constexpr uint32_t BASE = 16;
817   constexpr char EXPONENT_MARKER = 'p';
818 
819   bool truncated = false;
820   bool seen_digit = false;
821   bool after_decimal = false;
822   BitsType mantissa = 0;
823   int32_t exponent = 0;
824 
825   // The goal for the first step of parsing is to convert the number in src to
826   // the format mantissa * (base ^ exponent)
827 
828   // The loop fills the mantissa with as many digits as it can hold
829   const BitsType bitstype_max_div_by_base =
830       __llvm_libc::cpp::NumericLimits<BitsType>::max() / BASE;
831   while (true) {
832     if (isalnum(*src)) {
833       uint32_t digit = b36_char_to_int(*src);
834       if (digit < BASE)
835         seen_digit = true;
836       else
837         break;
838 
839       if (mantissa < bitstype_max_div_by_base) {
840         mantissa = (mantissa * BASE) + digit;
841         if (after_decimal)
842           --exponent;
843       } else {
844         if (digit > 0)
845           truncated = true;
846         if (!after_decimal)
847           ++exponent;
848       }
849       ++src;
850       continue;
851     }
852     if (*src == DECIMAL_POINT) {
853       if (after_decimal) {
854         break; // this means that *src points to a second decimal point, ending
855                // the number.
856       }
857       after_decimal = true;
858       ++src;
859       continue;
860     }
861     // The character is neither a hexadecimal digit nor a decimal point.
862     break;
863   }
864 
865   if (!seen_digit)
866     return false;
867 
868   // Convert the exponent from having a base of 16 to having a base of 2.
869   exponent *= 4;
870 
871   if ((*src | 32) == EXPONENT_MARKER) {
872     if (*(src + 1) == '+' || *(src + 1) == '-' || isdigit(*(src + 1))) {
873       ++src;
874       char *temp_str_end;
875       int32_t add_to_exponent = strtointeger<int32_t>(src, &temp_str_end, 10);
876       if (add_to_exponent > 100000)
877         add_to_exponent = 100000;
878       else if (add_to_exponent < -100000)
879         add_to_exponent = -100000;
880       src = temp_str_end;
881       exponent += add_to_exponent;
882     }
883   }
884   *strEnd = const_cast<char *>(src);
885   if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
886     *outputMantissa = 0;
887     *outputExponent = 0;
888   } else {
889     binary_exp_to_float<T>(mantissa, exponent, truncated, outputMantissa,
890                            outputExponent);
891   }
892   return true;
893 }
894 
895 // Takes a pointer to a string and a pointer to a string pointer. This function
896 // is used as the backend for all of the string to float functions.
897 template <class T>
898 static inline T strtofloatingpoint(const char *__restrict src,
899                                    char **__restrict strEnd) {
900   using BitsType = typename fputil::FPBits<T>::UIntType;
901   fputil::FPBits<T> result = fputil::FPBits<T>();
902   const char *original_src = src;
903   bool seen_digit = false;
904   src = first_non_whitespace(src);
905 
906   if (*src == '+' || *src == '-') {
907     if (*src == '-') {
908       result.set_sign(true);
909     }
910     ++src;
911   }
912 
913   static constexpr char DECIMAL_POINT = '.';
914   static const char *inf_string = "infinity";
915   static const char *nan_string = "nan";
916 
917   // bool truncated = false;
918 
919   if (isdigit(*src) || *src == DECIMAL_POINT) { // regular number
920     int base = 10;
921     if (is_float_hex_start(src, DECIMAL_POINT)) {
922       base = 16;
923       src += 2;
924       seen_digit = true;
925     }
926     char *new_str_end = nullptr;
927 
928     BitsType output_mantissa = 0;
929     uint32_t output_exponent = 0;
930     if (base == 16) {
931       seen_digit = hexadecimal_string_to_float<T>(
932           src, DECIMAL_POINT, &new_str_end, &output_mantissa, &output_exponent);
933     } else { // base is 10
934       seen_digit = decimal_string_to_float<T>(
935           src, DECIMAL_POINT, &new_str_end, &output_mantissa, &output_exponent);
936     }
937 
938     if (seen_digit) {
939       src += new_str_end - src;
940       result.set_mantissa(output_mantissa);
941       result.set_unbiased_exponent(output_exponent);
942     }
943   } else if ((*src | 32) == 'n') { // NaN
944     if ((src[1] | 32) == nan_string[1] && (src[2] | 32) == nan_string[2]) {
945       seen_digit = true;
946       src += 3;
947       BitsType nan_mantissa = 0;
948       // this handles the case of `NaN(n-character-sequence)`, where the
949       // n-character-sequence is made of 0 or more letters and numbers in any
950       // order.
951       if (*src == '(') {
952         const char *left_paren = src;
953         ++src;
954         while (isalnum(*src))
955           ++src;
956         if (*src == ')') {
957           ++src;
958           char *temp_src = 0;
959           if (isdigit(*(left_paren + 1))) {
960             // This is to prevent errors when BitsType is larger than 64 bits,
961             // since strtointeger only supports up to 64 bits. This is actually
962             // more than is required by the specification, which says for the
963             // input type "NAN(n-char-sequence)" that "the meaning of
964             // the n-char sequence is implementation-defined."
965             nan_mantissa = static_cast<BitsType>(
966                 strtointeger<uint64_t>(left_paren + 1, &temp_src, 0));
967             if (*temp_src != ')')
968               nan_mantissa = 0;
969           }
970         } else
971           src = left_paren;
972       }
973       nan_mantissa |= fputil::FloatProperties<T>::QUIET_NAN_MASK;
974       if (result.get_sign()) {
975         result = fputil::FPBits<T>(result.build_nan(nan_mantissa));
976         result.set_sign(true);
977       } else {
978         result.set_sign(false);
979         result = fputil::FPBits<T>(result.build_nan(nan_mantissa));
980       }
981     }
982   } else if ((*src | 32) == 'i') { // INF
983     if ((src[1] | 32) == inf_string[1] && (src[2] | 32) == inf_string[2]) {
984       seen_digit = true;
985       if (result.get_sign())
986         result = result.neg_inf();
987       else
988         result = result.inf();
989       if ((src[3] | 32) == inf_string[3] && (src[4] | 32) == inf_string[4] &&
990           (src[5] | 32) == inf_string[5] && (src[6] | 32) == inf_string[6] &&
991           (src[7] | 32) == inf_string[7]) {
992         // if the string is "INFINITY" then strEnd needs to be set to src + 8.
993         src += 8;
994       } else {
995         src += 3;
996       }
997     }
998   }
999   if (!seen_digit) { // If there is nothing to actually parse, then return 0.
1000     if (strEnd != nullptr)
1001       *strEnd = const_cast<char *>(original_src);
1002     return T(0);
1003   }
1004 
1005   if (strEnd != nullptr)
1006     *strEnd = const_cast<char *>(src);
1007 
1008   // This function only does something if T is long double and the platform uses
1009   // special 80 bit long doubles. Otherwise it should be inlined out.
1010   set_implicit_bit<T>(result);
1011 
1012   return T(result);
1013 }
1014 
1015 } // namespace internal
1016 } // namespace __llvm_libc
1017 
1018 #endif // LIBC_SRC_SUPPORT_STR_TO_FLOAT_H
1019