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