• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LLVM_LIBC_SRC___SUPPORT_STR_TO_FLOAT_H
10 #define LLVM_LIBC_SRC___SUPPORT_STR_TO_FLOAT_H
11 
12 #include "src/__support/CPP/bit.h"
13 #include "src/__support/CPP/limits.h"
14 #include "src/__support/CPP/optional.h"
15 #include "src/__support/CPP/string_view.h"
16 #include "src/__support/FPUtil/FEnvImpl.h"
17 #include "src/__support/FPUtil/FPBits.h"
18 #include "src/__support/FPUtil/dyadic_float.h"
19 #include "src/__support/FPUtil/rounding_mode.h"
20 #include "src/__support/common.h"
21 #include "src/__support/ctype_utils.h"
22 #include "src/__support/detailed_powers_of_ten.h"
23 #include "src/__support/high_precision_decimal.h"
24 #include "src/__support/str_to_integer.h"
25 #include "src/__support/str_to_num_result.h"
26 #include "src/__support/uint128.h"
27 #include "src/errno/libc_errno.h" // For ERANGE
28 
29 namespace LIBC_NAMESPACE {
30 namespace internal {
31 
32 template <class T> struct ExpandedFloat {
33   typename fputil::FPBits<T>::StorageType mantissa;
34   int32_t exponent;
35 };
36 
37 template <class T> struct FloatConvertReturn {
38   ExpandedFloat<T> num = {0, 0};
39   int error = 0;
40 };
41 
low64(const UInt128 & num)42 LIBC_INLINE uint64_t low64(const UInt128 &num) {
43   return static_cast<uint64_t>(num & 0xffffffffffffffff);
44 }
45 
high64(const UInt128 & num)46 LIBC_INLINE uint64_t high64(const UInt128 &num) {
47   return static_cast<uint64_t>(num >> 64);
48 }
49 
set_implicit_bit(fputil::FPBits<T> &)50 template <class T> LIBC_INLINE void set_implicit_bit(fputil::FPBits<T> &) {
51   return;
52 }
53 
54 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
55 template <>
56 LIBC_INLINE void
57 set_implicit_bit<long double>(fputil::FPBits<long double> &result) {
58   result.set_implicit_bit(result.get_biased_exponent() != 0);
59 }
60 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
61 
62 // This Eisel-Lemire implementation is based on the algorithm described in the
63 // paper Number Parsing at a Gigabyte per Second, Software: Practice and
64 // Experience 51 (8), 2021 (https://arxiv.org/abs/2101.11408), as well as the
65 // description by Nigel Tao
66 // (https://nigeltao.github.io/blog/2020/eisel-lemire.html) and the golang
67 // implementation, also by Nigel Tao
68 // (https://github.com/golang/go/blob/release-branch.go1.16/src/strconv/eisel_lemire.go#L25)
69 // for some optimizations as well as handling 32 bit floats.
70 template <class T>
71 LIBC_INLINE cpp::optional<ExpandedFloat<T>>
72 eisel_lemire(ExpandedFloat<T> init_num,
73              RoundDirection round = RoundDirection::Nearest) {
74   using FPBits = typename fputil::FPBits<T>;
75   using StorageType = typename FPBits::StorageType;
76 
77   StorageType mantissa = init_num.mantissa;
78   int32_t exp10 = init_num.exponent;
79 
80   if (sizeof(T) > 8) { // This algorithm cannot handle anything longer than a
81                        // double, so we skip straight to the fallback.
82     return cpp::nullopt;
83   }
84 
85   // Exp10 Range
86   if (exp10 < DETAILED_POWERS_OF_TEN_MIN_EXP_10 ||
87       exp10 > DETAILED_POWERS_OF_TEN_MAX_EXP_10) {
88     return cpp::nullopt;
89   }
90 
91   // Normalization
92   uint32_t clz = cpp::countl_zero<StorageType>(mantissa);
93   mantissa <<= clz;
94 
95   int32_t exp2 =
96       exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - clz;
97 
98   // Multiplication
99   const uint64_t *power_of_ten =
100       DETAILED_POWERS_OF_TEN[exp10 - DETAILED_POWERS_OF_TEN_MIN_EXP_10];
101 
102   UInt128 first_approx =
103       static_cast<UInt128>(mantissa) * static_cast<UInt128>(power_of_ten[1]);
104 
105   // Wider Approximation
106   UInt128 final_approx;
107   // The halfway constant is used to check if the bits that will be shifted away
108   // intially are all 1. For doubles this is 64 (bitstype size) - 52 (final
109   // mantissa size) - 3 (we shift away the last two bits separately for
110   // accuracy, and the most significant bit is ignored.) = 9 bits. Similarly,
111   // it's 6 bits for floats in this case.
112   const uint64_t halfway_constant =
113       (uint64_t(1) << (FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3))) - 1;
114   if ((high64(first_approx) & halfway_constant) == halfway_constant &&
115       low64(first_approx) + mantissa < mantissa) {
116     UInt128 low_bits =
117         static_cast<UInt128>(mantissa) * static_cast<UInt128>(power_of_ten[0]);
118     UInt128 second_approx =
119         first_approx + static_cast<UInt128>(high64(low_bits));
120 
121     if ((high64(second_approx) & halfway_constant) == halfway_constant &&
122         low64(second_approx) + 1 == 0 &&
123         low64(low_bits) + mantissa < mantissa) {
124       return cpp::nullopt;
125     }
126     final_approx = second_approx;
127   } else {
128     final_approx = first_approx;
129   }
130 
131   // Shifting to 54 bits for doubles and 25 bits for floats
132   StorageType msb = static_cast<StorageType>(high64(final_approx) >>
133                                              (FPBits::STORAGE_LEN - 1));
134   StorageType final_mantissa = static_cast<StorageType>(
135       high64(final_approx) >>
136       (msb + FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3)));
137   exp2 -= static_cast<uint32_t>(1 ^ msb); // same as !msb
138 
139   if (round == RoundDirection::Nearest) {
140     // Half-way ambiguity
141     if (low64(final_approx) == 0 &&
142         (high64(final_approx) & halfway_constant) == 0 &&
143         (final_mantissa & 3) == 1) {
144       return cpp::nullopt;
145     }
146 
147     // Round to even.
148     final_mantissa += final_mantissa & 1;
149 
150   } else if (round == RoundDirection::Up) {
151     // If any of the bits being rounded away are non-zero, then round up.
152     if (low64(final_approx) > 0 ||
153         (high64(final_approx) & halfway_constant) > 0) {
154       // Add two since the last current lowest bit is about to be shifted away.
155       final_mantissa += 2;
156     }
157   }
158   // else round down, which has no effect.
159 
160   // From 54 to 53 bits for doubles and 25 to 24 bits for floats
161   final_mantissa >>= 1;
162   if ((final_mantissa >> (FPBits::FRACTION_LEN + 1)) > 0) {
163     final_mantissa >>= 1;
164     ++exp2;
165   }
166 
167   // The if block is equivalent to (but has fewer branches than):
168   //   if exp2 <= 0 || exp2 >= 0x7FF { etc }
169   if (static_cast<uint32_t>(exp2) - 1 >= (1 << FPBits::EXP_LEN) - 2) {
170     return cpp::nullopt;
171   }
172 
173   ExpandedFloat<T> output;
174   output.mantissa = final_mantissa;
175   output.exponent = exp2;
176   return output;
177 }
178 
179 #if !defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
180 template <>
181 LIBC_INLINE cpp::optional<ExpandedFloat<long double>>
182 eisel_lemire<long double>(ExpandedFloat<long double> init_num,
183                           RoundDirection round) {
184   using FPBits = typename fputil::FPBits<long double>;
185   using StorageType = typename FPBits::StorageType;
186 
187   StorageType mantissa = init_num.mantissa;
188   int32_t exp10 = init_num.exponent;
189 
190   // Exp10 Range
191   // This doesn't reach very far into the range for long doubles, since it's
192   // sized for doubles and their 11 exponent bits, and not for long doubles and
193   // their 15 exponent bits (max exponent of ~300 for double vs ~5000 for long
194   // double). This is a known tradeoff, and was made because a proper long
195   // double table would be approximately 16 times larger. This would have
196   // significant memory and storage costs all the time to speed up a relatively
197   // uncommon path. In addition the exp10_to_exp2 function only approximates
198   // multiplying by log(10)/log(2), and that approximation may not be accurate
199   // out to the full long double range.
200   if (exp10 < DETAILED_POWERS_OF_TEN_MIN_EXP_10 ||
201       exp10 > DETAILED_POWERS_OF_TEN_MAX_EXP_10) {
202     return cpp::nullopt;
203   }
204 
205   // Normalization
206   uint32_t clz = cpp::countl_zero<StorageType>(mantissa);
207   mantissa <<= clz;
208 
209   int32_t exp2 =
210       exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - clz;
211 
212   // Multiplication
213   const uint64_t *power_of_ten =
214       DETAILED_POWERS_OF_TEN[exp10 - DETAILED_POWERS_OF_TEN_MIN_EXP_10];
215 
216   // Since the input mantissa is more than 64 bits, we have to multiply with the
217   // full 128 bits of the power of ten to get an approximation with the same
218   // number of significant bits. This means that we only get the one
219   // approximation, and that approximation is 256 bits long.
220   UInt128 approx_upper = static_cast<UInt128>(high64(mantissa)) *
221                          static_cast<UInt128>(power_of_ten[1]);
222 
223   UInt128 approx_middle_a = static_cast<UInt128>(high64(mantissa)) *
224                             static_cast<UInt128>(power_of_ten[0]);
225   UInt128 approx_middle_b = static_cast<UInt128>(low64(mantissa)) *
226                             static_cast<UInt128>(power_of_ten[1]);
227 
228   UInt128 approx_middle = approx_middle_a + approx_middle_b;
229 
230   // Handle overflow in the middle
231   approx_upper += (approx_middle < approx_middle_a) ? UInt128(1) << 64 : 0;
232 
233   UInt128 approx_lower = static_cast<UInt128>(low64(mantissa)) *
234                          static_cast<UInt128>(power_of_ten[0]);
235 
236   UInt128 final_approx_lower =
237       approx_lower + (static_cast<UInt128>(low64(approx_middle)) << 64);
238   UInt128 final_approx_upper = approx_upper + high64(approx_middle) +
239                                (final_approx_lower < approx_lower ? 1 : 0);
240 
241   // The halfway constant is used to check if the bits that will be shifted away
242   // intially are all 1. For 80 bit floats this is 128 (bitstype size) - 64
243   // (final mantissa size) - 3 (we shift away the last two bits separately for
244   // accuracy, and the most significant bit is ignored.) = 61 bits. Similarly,
245   // it's 12 bits for 128 bit floats in this case.
246   constexpr UInt128 HALFWAY_CONSTANT =
247       (UInt128(1) << (FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3))) - 1;
248 
249   if ((final_approx_upper & HALFWAY_CONSTANT) == HALFWAY_CONSTANT &&
250       final_approx_lower + mantissa < mantissa) {
251     return cpp::nullopt;
252   }
253 
254   // Shifting to 65 bits for 80 bit floats and 113 bits for 128 bit floats
255   uint32_t msb =
256       static_cast<uint32_t>(final_approx_upper >> (FPBits::STORAGE_LEN - 1));
257   StorageType final_mantissa =
258       final_approx_upper >>
259       (msb + FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3));
260   exp2 -= static_cast<uint32_t>(1 ^ msb); // same as !msb
261 
262   if (round == RoundDirection::Nearest) {
263     // Half-way ambiguity
264     if (final_approx_lower == 0 &&
265         (final_approx_upper & HALFWAY_CONSTANT) == 0 &&
266         (final_mantissa & 3) == 1) {
267       return cpp::nullopt;
268     }
269     // Round to even.
270     final_mantissa += final_mantissa & 1;
271 
272   } else if (round == RoundDirection::Up) {
273     // If any of the bits being rounded away are non-zero, then round up.
274     if (final_approx_lower > 0 || (final_approx_upper & HALFWAY_CONSTANT) > 0) {
275       // Add two since the last current lowest bit is about to be shifted away.
276       final_mantissa += 2;
277     }
278   }
279   // else round down, which has no effect.
280 
281   // From 65 to 64 bits for 80 bit floats and 113  to 112 bits for 128 bit
282   // floats
283   final_mantissa >>= 1;
284   if ((final_mantissa >> (FPBits::FRACTION_LEN + 1)) > 0) {
285     final_mantissa >>= 1;
286     ++exp2;
287   }
288 
289   // The if block is equivalent to (but has fewer branches than):
290   //   if exp2 <= 0 || exp2 >= MANTISSA_MAX { etc }
291   if (exp2 - 1 >= (1 << FPBits::EXP_LEN) - 2) {
292     return cpp::nullopt;
293   }
294 
295   ExpandedFloat<long double> output;
296   output.mantissa = final_mantissa;
297   output.exponent = exp2;
298   return output;
299 }
300 #endif // !defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
301 
302 // The nth item in POWERS_OF_TWO represents the greatest power of two less than
303 // 10^n. This tells us how much we can safely shift without overshooting.
304 constexpr uint8_t POWERS_OF_TWO[19] = {
305     0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36, 39, 43, 46, 49, 53, 56, 59,
306 };
307 constexpr int32_t NUM_POWERS_OF_TWO =
308     sizeof(POWERS_OF_TWO) / sizeof(POWERS_OF_TWO[0]);
309 
310 // Takes a mantissa and base 10 exponent and converts it into its closest
311 // floating point type T equivalent. This is the fallback algorithm used when
312 // the Eisel-Lemire algorithm fails, it's slower but more accurate. It's based
313 // on the Simple Decimal Conversion algorithm by Nigel Tao, described at this
314 // link: https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html
315 template <class T>
316 LIBC_INLINE FloatConvertReturn<T> simple_decimal_conversion(
317     const char *__restrict numStart,
318     const size_t num_len = cpp::numeric_limits<size_t>::max(),
319     RoundDirection round = RoundDirection::Nearest) {
320   using FPBits = typename fputil::FPBits<T>;
321   using StorageType = typename FPBits::StorageType;
322 
323   int32_t exp2 = 0;
324   HighPrecisionDecimal hpd = HighPrecisionDecimal(numStart, num_len);
325 
326   FloatConvertReturn<T> output;
327 
328   if (hpd.get_num_digits() == 0) {
329     output.num = {0, 0};
330     return output;
331   }
332 
333   // If the exponent is too large and can't be represented in this size of
334   // float, return inf.
335   if (hpd.get_decimal_point() > 0 &&
336       exp10_to_exp2(hpd.get_decimal_point() - 1) > FPBits::EXP_BIAS) {
337     output.num = {0, fputil::FPBits<T>::MAX_BIASED_EXPONENT};
338     output.error = ERANGE;
339     return output;
340   }
341   // If the exponent is too small even for a subnormal, return 0.
342   if (hpd.get_decimal_point() < 0 &&
343       exp10_to_exp2(-hpd.get_decimal_point()) >
344           (FPBits::EXP_BIAS + static_cast<int32_t>(FPBits::FRACTION_LEN))) {
345     output.num = {0, 0};
346     output.error = ERANGE;
347     return output;
348   }
349 
350   // Right shift until the number is smaller than 1.
351   while (hpd.get_decimal_point() > 0) {
352     int32_t shift_amount = 0;
353     if (hpd.get_decimal_point() >= NUM_POWERS_OF_TWO) {
354       shift_amount = 60;
355     } else {
356       shift_amount = POWERS_OF_TWO[hpd.get_decimal_point()];
357     }
358     exp2 += shift_amount;
359     hpd.shift(-shift_amount);
360   }
361 
362   // Left shift until the number is between 1/2 and 1
363   while (hpd.get_decimal_point() < 0 ||
364          (hpd.get_decimal_point() == 0 && hpd.get_digits()[0] < 5)) {
365     int32_t shift_amount = 0;
366 
367     if (-hpd.get_decimal_point() >= NUM_POWERS_OF_TWO) {
368       shift_amount = 60;
369     } else if (hpd.get_decimal_point() != 0) {
370       shift_amount = POWERS_OF_TWO[-hpd.get_decimal_point()];
371     } else { // This handles the case of the number being between .1 and .5
372       shift_amount = 1;
373     }
374     exp2 -= shift_amount;
375     hpd.shift(shift_amount);
376   }
377 
378   // Left shift once so that the number is between 1 and 2
379   --exp2;
380   hpd.shift(1);
381 
382   // Get the biased exponent
383   exp2 += FPBits::EXP_BIAS;
384 
385   // Handle the exponent being too large (and return inf).
386   if (exp2 >= FPBits::MAX_BIASED_EXPONENT) {
387     output.num = {0, FPBits::MAX_BIASED_EXPONENT};
388     output.error = ERANGE;
389     return output;
390   }
391 
392   // Shift left to fill the mantissa
393   hpd.shift(FPBits::FRACTION_LEN);
394   StorageType final_mantissa = hpd.round_to_integer_type<StorageType>();
395 
396   // Handle subnormals
397   if (exp2 <= 0) {
398     // Shift right until there is a valid exponent
399     while (exp2 < 0) {
400       hpd.shift(-1);
401       ++exp2;
402     }
403     // Shift right one more time to compensate for the left shift to get it
404     // between 1 and 2.
405     hpd.shift(-1);
406     final_mantissa = hpd.round_to_integer_type<StorageType>(round);
407 
408     // Check if by shifting right we've caused this to round to a normal number.
409     if ((final_mantissa >> FPBits::FRACTION_LEN) != 0) {
410       ++exp2;
411     }
412   }
413 
414   // Check if rounding added a bit, and shift down if that's the case.
415   if (final_mantissa == StorageType(2) << FPBits::FRACTION_LEN) {
416     final_mantissa >>= 1;
417     ++exp2;
418 
419     // Check if this rounding causes exp2 to go out of range and make the result
420     // INF. If this is the case, then finalMantissa and exp2 are already the
421     // correct values for an INF result.
422     if (exp2 >= FPBits::MAX_BIASED_EXPONENT) {
423       output.error = ERANGE;
424     }
425   }
426 
427   if (exp2 == 0) {
428     output.error = ERANGE;
429   }
430 
431   output.num = {final_mantissa, exp2};
432   return output;
433 }
434 
435 // This class is used for templating the constants for Clinger's Fast Path,
436 // described as a method of approximation in
437 // Clinger WD. How to Read Floating Point Numbers Accurately. SIGPLAN Not 1990
438 // Jun;25(6):92–101. https://doi.org/10.1145/93548.93557.
439 // As well as the additions by Gay that extend the useful range by the number of
440 // exact digits stored by the float type, described in
441 // Gay DM, Correctly rounded binary-decimal and decimal-binary conversions;
442 // 1990. AT&T Bell Laboratories Numerical Analysis Manuscript 90-10.
443 template <class T> class ClingerConsts;
444 
445 template <> class ClingerConsts<float> {
446 public:
447   static constexpr float POWERS_OF_TEN_ARRAY[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5,
448                                                   1e6, 1e7, 1e8, 1e9, 1e10};
449   static constexpr int32_t EXACT_POWERS_OF_TEN = 10;
450   static constexpr int32_t DIGITS_IN_MANTISSA = 7;
451   static constexpr float MAX_EXACT_INT = 16777215.0;
452 };
453 
454 template <> class ClingerConsts<double> {
455 public:
456   static constexpr double POWERS_OF_TEN_ARRAY[] = {
457       1e0,  1e1,  1e2,  1e3,  1e4,  1e5,  1e6,  1e7,  1e8,  1e9,  1e10, 1e11,
458       1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
459   static constexpr int32_t EXACT_POWERS_OF_TEN = 22;
460   static constexpr int32_t DIGITS_IN_MANTISSA = 15;
461   static constexpr double MAX_EXACT_INT = 9007199254740991.0;
462 };
463 
464 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
465 template <> class ClingerConsts<long double> {
466 public:
467   static constexpr long double POWERS_OF_TEN_ARRAY[] = {
468       1e0,  1e1,  1e2,  1e3,  1e4,  1e5,  1e6,  1e7,  1e8,  1e9,  1e10, 1e11,
469       1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
470   static constexpr int32_t EXACT_POWERS_OF_TEN =
471       ClingerConsts<double>::EXACT_POWERS_OF_TEN;
472   static constexpr int32_t DIGITS_IN_MANTISSA =
473       ClingerConsts<double>::DIGITS_IN_MANTISSA;
474   static constexpr long double MAX_EXACT_INT =
475       ClingerConsts<double>::MAX_EXACT_INT;
476 };
477 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
478 template <> class ClingerConsts<long double> {
479 public:
480   static constexpr long double POWERS_OF_TEN_ARRAY[] = {
481       1e0L,  1e1L,  1e2L,  1e3L,  1e4L,  1e5L,  1e6L,  1e7L,  1e8L,  1e9L,
482       1e10L, 1e11L, 1e12L, 1e13L, 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L,
483       1e20L, 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L};
484   static constexpr int32_t EXACT_POWERS_OF_TEN = 27;
485   static constexpr int32_t DIGITS_IN_MANTISSA = 21;
486   static constexpr long double MAX_EXACT_INT = 18446744073709551615.0L;
487 };
488 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
489 template <> class ClingerConsts<long double> {
490 public:
491   static constexpr long double POWERS_OF_TEN_ARRAY[] = {
492       1e0L,  1e1L,  1e2L,  1e3L,  1e4L,  1e5L,  1e6L,  1e7L,  1e8L,  1e9L,
493       1e10L, 1e11L, 1e12L, 1e13L, 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L,
494       1e20L, 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L, 1e28L, 1e29L,
495       1e30L, 1e31L, 1e32L, 1e33L, 1e34L, 1e35L, 1e36L, 1e37L, 1e38L, 1e39L,
496       1e40L, 1e41L, 1e42L, 1e43L, 1e44L, 1e45L, 1e46L, 1e47L, 1e48L};
497   static constexpr int32_t EXACT_POWERS_OF_TEN = 48;
498   static constexpr int32_t DIGITS_IN_MANTISSA = 33;
499   static constexpr long double MAX_EXACT_INT =
500       10384593717069655257060992658440191.0L;
501 };
502 #else
503 #error "Unknown long double type"
504 #endif
505 
506 // Take an exact mantissa and exponent and attempt to convert it using only
507 // exact floating point arithmetic. This only handles numbers with low
508 // exponents, but handles them quickly. This is an implementation of Clinger's
509 // Fast Path, as described above.
510 template <class T>
511 LIBC_INLINE cpp::optional<ExpandedFloat<T>>
512 clinger_fast_path(ExpandedFloat<T> init_num,
513                   RoundDirection round = RoundDirection::Nearest) {
514   using FPBits = typename fputil::FPBits<T>;
515   using StorageType = typename FPBits::StorageType;
516 
517   StorageType mantissa = init_num.mantissa;
518   int32_t exp10 = init_num.exponent;
519 
520   if ((mantissa >> FPBits::FRACTION_LEN) > 0) {
521     return cpp::nullopt;
522   }
523 
524   FPBits result;
525   T float_mantissa;
526   if constexpr (cpp::is_same_v<StorageType, UInt<128>>) {
527     float_mantissa = static_cast<T>(fputil::DyadicFloat<128>(
528         Sign::POS, 0,
529         fputil::DyadicFloat<128>::MantissaType(
530             {uint64_t(mantissa), uint64_t(mantissa >> 64)})));
531   } else {
532     float_mantissa = static_cast<T>(mantissa);
533   }
534 
535   if (exp10 == 0) {
536     result = FPBits(float_mantissa);
537   }
538   if (exp10 > 0) {
539     if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN +
540                     ClingerConsts<T>::DIGITS_IN_MANTISSA) {
541       return cpp::nullopt;
542     }
543     if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
544       float_mantissa = float_mantissa *
545                        ClingerConsts<T>::POWERS_OF_TEN_ARRAY
546                            [exp10 - ClingerConsts<T>::EXACT_POWERS_OF_TEN];
547       exp10 = ClingerConsts<T>::EXACT_POWERS_OF_TEN;
548     }
549     if (float_mantissa > ClingerConsts<T>::MAX_EXACT_INT) {
550       return cpp::nullopt;
551     }
552     result =
553         FPBits(float_mantissa * ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
554   } else if (exp10 < 0) {
555     if (-exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
556       return cpp::nullopt;
557     }
558     result =
559         FPBits(float_mantissa / ClingerConsts<T>::POWERS_OF_TEN_ARRAY[-exp10]);
560   }
561 
562   // If the rounding mode is not nearest, then the sign of the number may affect
563   // the result. To make sure the rounding mode is respected properly, the
564   // calculation is redone with a negative result, and the rounding mode is used
565   // to select the correct result.
566   if (round != RoundDirection::Nearest) {
567     FPBits negative_result;
568     // I'm 99% sure this will break under fast math optimizations.
569     negative_result = FPBits((-float_mantissa) *
570                              ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
571 
572     // If the results are equal, then we don't need to use the rounding mode.
573     if (result.get_val() != -negative_result.get_val()) {
574       FPBits lower_result;
575       FPBits higher_result;
576 
577       if (result.get_val() < -negative_result.get_val()) {
578         lower_result = result;
579         higher_result = negative_result;
580       } else {
581         lower_result = negative_result;
582         higher_result = result;
583       }
584 
585       if (round == RoundDirection::Up) {
586         result = higher_result;
587       } else {
588         result = lower_result;
589       }
590     }
591   }
592 
593   ExpandedFloat<T> output;
594   output.mantissa = result.get_mantissa();
595   output.exponent = result.get_biased_exponent();
596   return output;
597 }
598 
599 // The upper bound is the highest base-10 exponent that could possibly give a
600 // non-inf result for this size of float. The value is
601 // log10(2^(exponent bias)).
602 // The generic approximation uses the fact that log10(2^x) ~= x/3
get_upper_bound()603 template <typename T> LIBC_INLINE constexpr int32_t get_upper_bound() {
604   return fputil::FPBits<T>::EXP_BIAS / 3;
605 }
606 
607 template <> LIBC_INLINE constexpr int32_t get_upper_bound<float>() {
608   return 39;
609 }
610 
611 template <> LIBC_INLINE constexpr int32_t get_upper_bound<double>() {
612   return 309;
613 }
614 
615 // The lower bound is the largest negative base-10 exponent that could possibly
616 // give a non-zero result for this size of float. The value is
617 // log10(2^(exponent bias + final mantissa width + intermediate mantissa width))
618 // The intermediate mantissa is the integer that's been parsed from the string,
619 // and the final mantissa is the fractional part of the output number. A very
620 // low base 10 exponent with a very high intermediate mantissa can cancel each
621 // other out, and subnormal numbers allow for the result to be at the very low
622 // end of the final mantissa.
get_lower_bound()623 template <typename T> LIBC_INLINE constexpr int32_t get_lower_bound() {
624   using FPBits = typename fputil::FPBits<T>;
625   return -((FPBits::EXP_BIAS +
626             static_cast<int32_t>(FPBits::FRACTION_LEN + FPBits::STORAGE_LEN)) /
627            3);
628 }
629 
630 template <> LIBC_INLINE constexpr int32_t get_lower_bound<float>() {
631   return -(39 + 6 + 10);
632 }
633 
634 template <> LIBC_INLINE constexpr int32_t get_lower_bound<double>() {
635   return -(309 + 15 + 20);
636 }
637 
638 // Takes a mantissa and base 10 exponent and converts it into its closest
639 // floating point type T equivalient. First we try the Eisel-Lemire algorithm,
640 // then if that fails then we fall back to a more accurate algorithm for
641 // accuracy. The resulting mantissa and exponent are placed in outputMantissa
642 // and outputExp2.
643 template <class T>
644 LIBC_INLINE FloatConvertReturn<T> decimal_exp_to_float(
645     ExpandedFloat<T> init_num, bool truncated, RoundDirection round,
646     const char *__restrict numStart,
647     const size_t num_len = cpp::numeric_limits<size_t>::max()) {
648   using FPBits = typename fputil::FPBits<T>;
649   using StorageType = typename FPBits::StorageType;
650 
651   StorageType mantissa = init_num.mantissa;
652   int32_t exp10 = init_num.exponent;
653 
654   FloatConvertReturn<T> output;
655   cpp::optional<ExpandedFloat<T>> opt_output;
656 
657   // If the exponent is too large and can't be represented in this size of
658   // float, return inf. These bounds are relatively loose, but are mostly
659   // serving as a first pass. Some close numbers getting through is okay.
660   if (exp10 > get_upper_bound<T>()) {
661     output.num = {0, FPBits::MAX_BIASED_EXPONENT};
662     output.error = ERANGE;
663     return output;
664   }
665   // If the exponent is too small even for a subnormal, return 0.
666   if (exp10 < get_lower_bound<T>()) {
667     output.num = {0, 0};
668     output.error = ERANGE;
669     return output;
670   }
671 
672   // Clinger's Fast Path and Eisel-Lemire can't set errno, but they can fail.
673   // For this reason the "error" field in their return values is used to
674   // represent whether they've failed as opposed to the errno value. Any
675   // non-zero value represents a failure.
676 
677 #ifndef LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH
678   if (!truncated) {
679     opt_output = clinger_fast_path<T>(init_num, round);
680     // If the algorithm succeeded the error will be 0, else it will be a
681     // non-zero number.
682     if (opt_output.has_value()) {
683       return {opt_output.value(), 0};
684     }
685   }
686 #endif // LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH
687 
688 #ifndef LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE
689   // Try Eisel-Lemire
690   opt_output = eisel_lemire<T>(init_num, round);
691   if (opt_output.has_value()) {
692     if (!truncated) {
693       return {opt_output.value(), 0};
694     }
695     // If the mantissa is truncated, then the result may be off by the LSB, so
696     // check if rounding the mantissa up changes the result. If not, then it's
697     // safe, else use the fallback.
698     auto second_output = eisel_lemire<T>({mantissa + 1, exp10}, round);
699     if (second_output.has_value()) {
700       if (opt_output->mantissa == second_output->mantissa &&
701           opt_output->exponent == second_output->exponent) {
702         return {opt_output.value(), 0};
703       }
704     }
705   }
706 #endif // LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE
707 
708 #ifndef LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION
709   output = simple_decimal_conversion<T>(numStart, num_len, round);
710 #else
711 #warning "Simple decimal conversion is disabled, result may not be correct."
712 #endif // LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION
713 
714   return output;
715 }
716 
717 // Takes a mantissa and base 2 exponent and converts it into its closest
718 // floating point type T equivalient. Since the exponent is already in the right
719 // form, this is mostly just shifting and rounding. This is used for hexadecimal
720 // numbers since a base 16 exponent multiplied by 4 is the base 2 exponent.
721 template <class T>
binary_exp_to_float(ExpandedFloat<T> init_num,bool truncated,RoundDirection round)722 LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
723                                                       bool truncated,
724                                                       RoundDirection round) {
725   using FPBits = typename fputil::FPBits<T>;
726   using StorageType = typename FPBits::StorageType;
727 
728   StorageType mantissa = init_num.mantissa;
729   int32_t exp2 = init_num.exponent;
730 
731   FloatConvertReturn<T> output;
732 
733   // This is the number of leading zeroes a properly normalized float of type T
734   // should have.
735   constexpr int32_t INF_EXP = (1 << FPBits::EXP_LEN) - 1;
736 
737   // Normalization step 1: Bring the leading bit to the highest bit of
738   // StorageType.
739   uint32_t amount_to_shift_left = cpp::countl_zero<StorageType>(mantissa);
740   mantissa <<= amount_to_shift_left;
741 
742   // Keep exp2 representing the exponent of the lowest bit of StorageType.
743   exp2 -= amount_to_shift_left;
744 
745   // biased_exponent represents the biased exponent of the most significant bit.
746   int32_t biased_exponent = exp2 + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - 1;
747 
748   // Handle numbers that're too large and get squashed to inf
749   if (biased_exponent >= INF_EXP) {
750     // This indicates an overflow, so we make the result INF and set errno.
751     output.num = {0, (1 << FPBits::EXP_LEN) - 1};
752     output.error = ERANGE;
753     return output;
754   }
755 
756   uint32_t amount_to_shift_right =
757       FPBits::STORAGE_LEN - FPBits::FRACTION_LEN - 1;
758 
759   // Handle subnormals.
760   if (biased_exponent <= 0) {
761     amount_to_shift_right += 1 - biased_exponent;
762     biased_exponent = 0;
763 
764     if (amount_to_shift_right > FPBits::STORAGE_LEN) {
765       // Return 0 if the exponent is too small.
766       output.num = {0, 0};
767       output.error = ERANGE;
768       return output;
769     }
770   }
771 
772   StorageType round_bit_mask = StorageType(1) << (amount_to_shift_right - 1);
773   StorageType sticky_mask = round_bit_mask - 1;
774   bool round_bit = static_cast<bool>(mantissa & round_bit_mask);
775   bool sticky_bit = static_cast<bool>(mantissa & sticky_mask) || truncated;
776 
777   if (amount_to_shift_right < FPBits::STORAGE_LEN) {
778     // Shift the mantissa and clear the implicit bit.
779     mantissa >>= amount_to_shift_right;
780     mantissa &= FPBits::FRACTION_MASK;
781   } else {
782     mantissa = 0;
783   }
784   bool least_significant_bit = static_cast<bool>(mantissa & StorageType(1));
785 
786   // TODO: check that this rounding behavior is correct.
787 
788   if (round == RoundDirection::Nearest) {
789     // Perform rounding-to-nearest, tie-to-even.
790     if (round_bit && (least_significant_bit || sticky_bit)) {
791       ++mantissa;
792     }
793   } else if (round == RoundDirection::Up) {
794     if (round_bit || sticky_bit) {
795       ++mantissa;
796     }
797   } else /* (round == RoundDirection::Down)*/ {
798     if (round_bit && sticky_bit) {
799       ++mantissa;
800     }
801   }
802 
803   if (mantissa > FPBits::FRACTION_MASK) {
804     // Rounding causes the exponent to increase.
805     ++biased_exponent;
806 
807     if (biased_exponent == INF_EXP) {
808       output.error = ERANGE;
809     }
810   }
811 
812   if (biased_exponent == 0) {
813     output.error = ERANGE;
814   }
815 
816   output.num = {mantissa & FPBits::FRACTION_MASK, biased_exponent};
817   return output;
818 }
819 
820 // checks if the next 4 characters of the string pointer are the start of a
821 // hexadecimal floating point number. Does not advance the string pointer.
is_float_hex_start(const char * __restrict src,const char decimalPoint)822 LIBC_INLINE bool is_float_hex_start(const char *__restrict src,
823                                     const char decimalPoint) {
824   if (!(src[0] == '0' && tolower(src[1]) == 'x')) {
825     return false;
826   }
827   size_t first_digit = 2;
828   if (src[2] == decimalPoint) {
829     ++first_digit;
830   }
831   return isalnum(src[first_digit]) && b36_char_to_int(src[first_digit]) < 16;
832 }
833 
834 // Takes the start of a string representing a decimal float, as well as the
835 // local decimalPoint. It returns if it suceeded in parsing any digits, and if
836 // the return value is true then the outputs are pointer to the end of the
837 // number, and the mantissa and exponent for the closest float T representation.
838 // If the return value is false, then it is assumed that there is no number
839 // here.
840 template <class T>
841 LIBC_INLINE StrToNumResult<ExpandedFloat<T>>
decimal_string_to_float(const char * __restrict src,const char DECIMAL_POINT,RoundDirection round)842 decimal_string_to_float(const char *__restrict src, const char DECIMAL_POINT,
843                         RoundDirection round) {
844   using FPBits = typename fputil::FPBits<T>;
845   using StorageType = typename FPBits::StorageType;
846 
847   constexpr uint32_t BASE = 10;
848   constexpr char EXPONENT_MARKER = 'e';
849 
850   bool truncated = false;
851   bool seen_digit = false;
852   bool after_decimal = false;
853   StorageType mantissa = 0;
854   int32_t exponent = 0;
855 
856   size_t index = 0;
857 
858   StrToNumResult<ExpandedFloat<T>> output({0, 0});
859 
860   // The goal for the first step of parsing is to convert the number in src to
861   // the format mantissa * (base ^ exponent)
862 
863   // The loop fills the mantissa with as many digits as it can hold
864   const StorageType bitstype_max_div_by_base =
865       cpp::numeric_limits<StorageType>::max() / BASE;
866   while (true) {
867     if (isdigit(src[index])) {
868       uint32_t digit = src[index] - '0';
869       seen_digit = true;
870 
871       if (mantissa < bitstype_max_div_by_base) {
872         mantissa = (mantissa * BASE) + digit;
873         if (after_decimal) {
874           --exponent;
875         }
876       } else {
877         if (digit > 0)
878           truncated = true;
879         if (!after_decimal)
880           ++exponent;
881       }
882 
883       ++index;
884       continue;
885     }
886     if (src[index] == DECIMAL_POINT) {
887       if (after_decimal) {
888         break; // this means that src[index] points to a second decimal point,
889                // ending the number.
890       }
891       after_decimal = true;
892       ++index;
893       continue;
894     }
895     // The character is neither a digit nor a decimal point.
896     break;
897   }
898 
899   if (!seen_digit)
900     return output;
901 
902   // TODO: When adding max length argument, handle the case of a trailing
903   // EXPONENT MARKER, see scanf for more details.
904   if (tolower(src[index]) == EXPONENT_MARKER) {
905     bool has_sign = false;
906     if (src[index + 1] == '+' || src[index + 1] == '-') {
907       has_sign = true;
908     }
909     if (isdigit(src[index + 1 + static_cast<size_t>(has_sign)])) {
910       ++index;
911       auto result = strtointeger<int32_t>(src + index, 10);
912       if (result.has_error())
913         output.error = result.error;
914       int32_t add_to_exponent = result.value;
915       index += result.parsed_len;
916 
917       // Here we do this operation as int64 to avoid overflow.
918       int64_t temp_exponent = static_cast<int64_t>(exponent) +
919                               static_cast<int64_t>(add_to_exponent);
920 
921       // If the result is in the valid range, then we use it. The valid range is
922       // also within the int32 range, so this prevents overflow issues.
923       if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
924         exponent = FPBits::MAX_BIASED_EXPONENT;
925       } else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
926         exponent = -FPBits::MAX_BIASED_EXPONENT;
927       } else {
928         exponent = static_cast<int32_t>(temp_exponent);
929       }
930     }
931   }
932 
933   output.parsed_len = index;
934   if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
935     output.value = {0, 0};
936   } else {
937     auto temp =
938         decimal_exp_to_float<T>({mantissa, exponent}, truncated, round, src);
939     output.value = temp.num;
940     output.error = temp.error;
941   }
942   return output;
943 }
944 
945 // Takes the start of a string representing a hexadecimal float, as well as the
946 // local decimal point. It returns if it suceeded in parsing any digits, and if
947 // the return value is true then the outputs are pointer to the end of the
948 // number, and the mantissa and exponent for the closest float T representation.
949 // If the return value is false, then it is assumed that there is no number
950 // here.
951 template <class T>
952 LIBC_INLINE StrToNumResult<ExpandedFloat<T>>
hexadecimal_string_to_float(const char * __restrict src,const char DECIMAL_POINT,RoundDirection round)953 hexadecimal_string_to_float(const char *__restrict src,
954                             const char DECIMAL_POINT, RoundDirection round) {
955   using FPBits = typename fputil::FPBits<T>;
956   using StorageType = typename FPBits::StorageType;
957 
958   constexpr uint32_t BASE = 16;
959   constexpr char EXPONENT_MARKER = 'p';
960 
961   bool truncated = false;
962   bool seen_digit = false;
963   bool after_decimal = false;
964   StorageType mantissa = 0;
965   int32_t exponent = 0;
966 
967   size_t index = 0;
968 
969   StrToNumResult<ExpandedFloat<T>> output({0, 0});
970 
971   // The goal for the first step of parsing is to convert the number in src to
972   // the format mantissa * (base ^ exponent)
973 
974   // The loop fills the mantissa with as many digits as it can hold
975   const StorageType bitstype_max_div_by_base =
976       cpp::numeric_limits<StorageType>::max() / BASE;
977   while (true) {
978     if (isalnum(src[index])) {
979       uint32_t digit = b36_char_to_int(src[index]);
980       if (digit < BASE)
981         seen_digit = true;
982       else
983         break;
984 
985       if (mantissa < bitstype_max_div_by_base) {
986         mantissa = (mantissa * BASE) + digit;
987         if (after_decimal)
988           --exponent;
989       } else {
990         if (digit > 0)
991           truncated = true;
992         if (!after_decimal)
993           ++exponent;
994       }
995       ++index;
996       continue;
997     }
998     if (src[index] == DECIMAL_POINT) {
999       if (after_decimal) {
1000         break; // this means that src[index] points to a second decimal point,
1001                // ending the number.
1002       }
1003       after_decimal = true;
1004       ++index;
1005       continue;
1006     }
1007     // The character is neither a hexadecimal digit nor a decimal point.
1008     break;
1009   }
1010 
1011   if (!seen_digit)
1012     return output;
1013 
1014   // Convert the exponent from having a base of 16 to having a base of 2.
1015   exponent *= 4;
1016 
1017   if (tolower(src[index]) == EXPONENT_MARKER) {
1018     bool has_sign = false;
1019     if (src[index + 1] == '+' || src[index + 1] == '-') {
1020       has_sign = true;
1021     }
1022     if (isdigit(src[index + 1 + static_cast<size_t>(has_sign)])) {
1023       ++index;
1024       auto result = strtointeger<int32_t>(src + index, 10);
1025       if (result.has_error())
1026         output.error = result.error;
1027 
1028       int32_t add_to_exponent = result.value;
1029       index += result.parsed_len;
1030 
1031       // Here we do this operation as int64 to avoid overflow.
1032       int64_t temp_exponent = static_cast<int64_t>(exponent) +
1033                               static_cast<int64_t>(add_to_exponent);
1034 
1035       // If the result is in the valid range, then we use it. The valid range is
1036       // also within the int32 range, so this prevents overflow issues.
1037       if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
1038         exponent = FPBits::MAX_BIASED_EXPONENT;
1039       } else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
1040         exponent = -FPBits::MAX_BIASED_EXPONENT;
1041       } else {
1042         exponent = static_cast<int32_t>(temp_exponent);
1043       }
1044     }
1045   }
1046   output.parsed_len = index;
1047   if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
1048     output.value.exponent = 0;
1049     output.value.mantissa = 0;
1050   } else {
1051     auto temp = binary_exp_to_float<T>({mantissa, exponent}, truncated, round);
1052     output.error = temp.error;
1053     output.value = temp.num;
1054   }
1055   return output;
1056 }
1057 
1058 template <class T>
1059 LIBC_INLINE typename fputil::FPBits<T>::StorageType
nan_mantissa_from_ncharseq(const cpp::string_view ncharseq)1060 nan_mantissa_from_ncharseq(const cpp::string_view ncharseq) {
1061   using FPBits = typename fputil::FPBits<T>;
1062   using StorageType = typename FPBits::StorageType;
1063 
1064   StorageType nan_mantissa = 0;
1065 
1066   if (ncharseq.data() != nullptr && isdigit(ncharseq[0])) {
1067     StrToNumResult<StorageType> strtoint_result =
1068         strtointeger<StorageType>(ncharseq.data(), 0);
1069     if (!strtoint_result.has_error())
1070       nan_mantissa = strtoint_result.value;
1071 
1072     if (strtoint_result.parsed_len != static_cast<ptrdiff_t>(ncharseq.size()))
1073       nan_mantissa = 0;
1074   }
1075 
1076   return nan_mantissa;
1077 }
1078 
1079 // Takes a pointer to a string and a pointer to a string pointer. This function
1080 // is used as the backend for all of the string to float functions.
1081 // TODO: Add src_len member to match strtointeger.
1082 // TODO: Next, move from char* and length to string_view
1083 template <class T>
strtofloatingpoint(const char * __restrict src)1084 LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
1085   using FPBits = typename fputil::FPBits<T>;
1086   using StorageType = typename FPBits::StorageType;
1087 
1088   FPBits result = FPBits();
1089   bool seen_digit = false;
1090   char sign = '+';
1091 
1092   int error = 0;
1093 
1094   ptrdiff_t index = first_non_whitespace(src) - src;
1095 
1096   if (src[index] == '+' || src[index] == '-') {
1097     sign = src[index];
1098     ++index;
1099   }
1100 
1101   if (sign == '-') {
1102     result.set_sign(Sign::NEG);
1103   }
1104 
1105   static constexpr char DECIMAL_POINT = '.';
1106   static const char *inf_string = "infinity";
1107   static const char *nan_string = "nan";
1108 
1109   if (isdigit(src[index]) || src[index] == DECIMAL_POINT) { // regular number
1110     int base = 10;
1111     if (is_float_hex_start(src + index, DECIMAL_POINT)) {
1112       base = 16;
1113       index += 2;
1114       seen_digit = true;
1115     }
1116 
1117     RoundDirection round_direction = RoundDirection::Nearest;
1118 
1119     switch (fputil::quick_get_round()) {
1120     case FE_TONEAREST:
1121       round_direction = RoundDirection::Nearest;
1122       break;
1123     case FE_UPWARD:
1124       if (sign == '+') {
1125         round_direction = RoundDirection::Up;
1126       } else {
1127         round_direction = RoundDirection::Down;
1128       }
1129       break;
1130     case FE_DOWNWARD:
1131       if (sign == '+') {
1132         round_direction = RoundDirection::Down;
1133       } else {
1134         round_direction = RoundDirection::Up;
1135       }
1136       break;
1137     case FE_TOWARDZERO:
1138       round_direction = RoundDirection::Down;
1139       break;
1140     }
1141 
1142     StrToNumResult<ExpandedFloat<T>> parse_result({0, 0});
1143     if (base == 16) {
1144       parse_result = hexadecimal_string_to_float<T>(src + index, DECIMAL_POINT,
1145                                                     round_direction);
1146     } else { // base is 10
1147       parse_result = decimal_string_to_float<T>(src + index, DECIMAL_POINT,
1148                                                 round_direction);
1149     }
1150     seen_digit = parse_result.parsed_len != 0;
1151     result.set_mantissa(parse_result.value.mantissa);
1152     result.set_biased_exponent(parse_result.value.exponent);
1153     index += parse_result.parsed_len;
1154     error = parse_result.error;
1155   } else if (tolower(src[index]) == 'n') { // NaN
1156     if (tolower(src[index + 1]) == nan_string[1] &&
1157         tolower(src[index + 2]) == nan_string[2]) {
1158       seen_digit = true;
1159       index += 3;
1160       StorageType nan_mantissa = 0;
1161       // this handles the case of `NaN(n-character-sequence)`, where the
1162       // n-character-sequence is made of 0 or more letters and numbers in any
1163       // order.
1164       if (src[index] == '(') {
1165         size_t left_paren = index;
1166         ++index;
1167         // Apparently it's common for underscores to also be accepted. No idea
1168         // why, but it's causing fuzz failures.
1169         while (isalnum(src[index]) || src[index] == '_')
1170           ++index;
1171         if (src[index] == ')') {
1172           ++index;
1173           nan_mantissa = nan_mantissa_from_ncharseq<T>(
1174               cpp::string_view(src + (left_paren + 1), index - left_paren - 2));
1175         } else {
1176           index = left_paren;
1177         }
1178       }
1179       result = FPBits(result.quiet_nan(result.sign(), nan_mantissa));
1180     }
1181   } else if (tolower(src[index]) == 'i') { // INF
1182     if (tolower(src[index + 1]) == inf_string[1] &&
1183         tolower(src[index + 2]) == inf_string[2]) {
1184       seen_digit = true;
1185       result = FPBits(result.inf(result.sign()));
1186       if (tolower(src[index + 3]) == inf_string[3] &&
1187           tolower(src[index + 4]) == inf_string[4] &&
1188           tolower(src[index + 5]) == inf_string[5] &&
1189           tolower(src[index + 6]) == inf_string[6] &&
1190           tolower(src[index + 7]) == inf_string[7]) {
1191         // if the string is "INFINITY" then consume 8 characters.
1192         index += 8;
1193       } else {
1194         index += 3;
1195       }
1196     }
1197   }
1198   if (!seen_digit) { // If there is nothing to actually parse, then return 0.
1199     return {T(0), 0, error};
1200   }
1201 
1202   // This function only does something if T is long double and the platform uses
1203   // special 80 bit long doubles. Otherwise it should be inlined out.
1204   set_implicit_bit<T>(result);
1205 
1206   return {result.get_val(), index, error};
1207 }
1208 
strtonan(const char * arg)1209 template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
1210   using FPBits = typename fputil::FPBits<T>;
1211   using StorageType = typename FPBits::StorageType;
1212 
1213   FPBits result;
1214   int error = 0;
1215   StorageType nan_mantissa = 0;
1216 
1217   ptrdiff_t index = 0;
1218   while (isalnum(arg[index]) || arg[index] == '_')
1219     ++index;
1220 
1221   if (arg[index] == '\0')
1222     nan_mantissa = nan_mantissa_from_ncharseq<T>(cpp::string_view(arg, index));
1223 
1224   result = FPBits::quiet_nan(Sign::POS, nan_mantissa);
1225   return {result.get_val(), 0, error};
1226 }
1227 
1228 } // namespace internal
1229 } // namespace LIBC_NAMESPACE
1230 
1231 #endif // LLVM_LIBC_SRC___SUPPORT_STR_TO_FLOAT_H
1232