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 exp10 = 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 (exp10 < DETAILED_POWERS_OF_TEN_MIN_EXP_10 ||
106 exp10 > 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 exp2 = exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS -
115 static_cast<int32_t>(clz);
116
117 // Multiplication
118 const uint64_t *power_of_ten =
119 DETAILED_POWERS_OF_TEN[exp10 - 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 exp2 -= 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 ++exp2;
184 }
185
186 // The if block is equivalent to (but has fewer branches than):
187 // if exp2 <= 0 || exp2 >= 0x7FF { etc }
188 if (static_cast<uint32_t>(exp2) - 1 >= (1 << FPBits::EXP_LEN) - 2) {
189 return cpp::nullopt;
190 }
191
192 ExpandedFloat<T> output;
193 output.mantissa = final_mantissa;
194 output.exponent = exp2;
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 exp10 = 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 (exp10 < DETAILED_POWERS_OF_TEN_MIN_EXP_10 ||
223 exp10 > 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 exp2 =
233 exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - clz;
234
235 // Multiplication
236 const uint64_t *power_of_ten =
237 DETAILED_POWERS_OF_TEN[exp10 - 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 exp2 -= 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 ++exp2;
309 }
310
311 // The if block is equivalent to (but has fewer branches than):
312 // if exp2 <= 0 || exp2 >= MANTISSA_MAX { etc }
313 if (exp2 - 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 = exp2;
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 exp2 = 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 exp2 += 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 exp2 -= shift_amount;
398 hpd.shift(shift_amount);
399 }
400
401 // Left shift once so that the number is between 1 and 2
402 --exp2;
403 hpd.shift(shift_amount: 1);
404
405 // Get the biased exponent
406 exp2 += FPBits::EXP_BIAS;
407
408 // Handle the exponent being too large (and return inf).
409 if (exp2 >= 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 (exp2 <= 0) {
421 // Shift right until there is a valid exponent
422 while (exp2 < 0) {
423 hpd.shift(shift_amount: -1);
424 ++exp2;
425 }
426 // Shift right one more time to compensate for the left shift to get it
427 // between 1 and 2.
428 hpd.shift(shift_amount: -1);
429 final_mantissa = hpd.round_to_integer_type<StorageType>(round);
430
431 // Check if by shifting right we've caused this to round to a normal number.
432 if ((final_mantissa >> FPBits::FRACTION_LEN) != 0) {
433 ++exp2;
434 }
435 }
436
437 // Check if rounding added a bit, and shift down if that's the case.
438 if (final_mantissa == StorageType(2) << FPBits::FRACTION_LEN) {
439 final_mantissa >>= 1;
440 ++exp2;
441
442 // Check if this rounding causes exp2 to go out of range and make the result
443 // INF. If this is the case, then finalMantissa and exp2 are already the
444 // correct values for an INF result.
445 if (exp2 >= FPBits::MAX_BIASED_EXPONENT) {
446 output.error = ERANGE;
447 }
448 }
449
450 if (exp2 == 0) {
451 output.error = ERANGE;
452 }
453
454 output.num = {final_mantissa, exp2};
455 return output;
456}
457
458// This class is used for templating the constants for Clinger's Fast Path,
459// described as a method of approximation in
460// Clinger WD. How to Read Floating Point Numbers Accurately. SIGPLAN Not 1990
461// Jun;25(6):92–101. https://doi.org/10.1145/93548.93557.
462// As well as the additions by Gay that extend the useful range by the number of
463// exact digits stored by the float type, described in
464// Gay DM, Correctly rounded binary-decimal and decimal-binary conversions;
465// 1990. AT&T Bell Laboratories Numerical Analysis Manuscript 90-10.
466template <class T> class ClingerConsts;
467
468template <> class ClingerConsts<float> {
469public:
470 static constexpr float POWERS_OF_TEN_ARRAY[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5,
471 1e6, 1e7, 1e8, 1e9, 1e10};
472 static constexpr int32_t EXACT_POWERS_OF_TEN = 10;
473 static constexpr int32_t DIGITS_IN_MANTISSA = 7;
474 static constexpr float MAX_EXACT_INT = 16777215.0;
475};
476
477template <> class ClingerConsts<double> {
478public:
479 static constexpr double POWERS_OF_TEN_ARRAY[] = {
480 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
481 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
482 static constexpr int32_t EXACT_POWERS_OF_TEN = 22;
483 static constexpr int32_t DIGITS_IN_MANTISSA = 15;
484 static constexpr double MAX_EXACT_INT = 9007199254740991.0;
485};
486
487#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
488template <> class ClingerConsts<long double> {
489public:
490 static constexpr long double POWERS_OF_TEN_ARRAY[] = {
491 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
492 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
493 static constexpr int32_t EXACT_POWERS_OF_TEN =
494 ClingerConsts<double>::EXACT_POWERS_OF_TEN;
495 static constexpr int32_t DIGITS_IN_MANTISSA =
496 ClingerConsts<double>::DIGITS_IN_MANTISSA;
497 static constexpr long double MAX_EXACT_INT =
498 ClingerConsts<double>::MAX_EXACT_INT;
499};
500#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
501template <> class ClingerConsts<long double> {
502public:
503 static constexpr long double POWERS_OF_TEN_ARRAY[] = {
504 1e0L, 1e1L, 1e2L, 1e3L, 1e4L, 1e5L, 1e6L, 1e7L, 1e8L, 1e9L,
505 1e10L, 1e11L, 1e12L, 1e13L, 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L,
506 1e20L, 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L};
507 static constexpr int32_t EXACT_POWERS_OF_TEN = 27;
508 static constexpr int32_t DIGITS_IN_MANTISSA = 21;
509 static constexpr long double MAX_EXACT_INT = 18446744073709551615.0L;
510};
511#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
512template <> class ClingerConsts<long double> {
513public:
514 static constexpr long double POWERS_OF_TEN_ARRAY[] = {
515 1e0L, 1e1L, 1e2L, 1e3L, 1e4L, 1e5L, 1e6L, 1e7L, 1e8L, 1e9L,
516 1e10L, 1e11L, 1e12L, 1e13L, 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L,
517 1e20L, 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L, 1e28L, 1e29L,
518 1e30L, 1e31L, 1e32L, 1e33L, 1e34L, 1e35L, 1e36L, 1e37L, 1e38L, 1e39L,
519 1e40L, 1e41L, 1e42L, 1e43L, 1e44L, 1e45L, 1e46L, 1e47L, 1e48L};
520 static constexpr int32_t EXACT_POWERS_OF_TEN = 48;
521 static constexpr int32_t DIGITS_IN_MANTISSA = 33;
522 static constexpr long double MAX_EXACT_INT =
523 10384593717069655257060992658440191.0L;
524};
525#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE)
526// TODO: Add proper double double type support here, currently using constants
527// for double since it should be safe.
528template <> class ClingerConsts<long double> {
529public:
530 static constexpr double POWERS_OF_TEN_ARRAY[] = {
531 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
532 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
533 static constexpr int32_t EXACT_POWERS_OF_TEN = 22;
534 static constexpr int32_t DIGITS_IN_MANTISSA = 15;
535 static constexpr double MAX_EXACT_INT = 9007199254740991.0;
536};
537#else
538#error "Unknown long double type"
539#endif
540
541// Take an exact mantissa and exponent and attempt to convert it using only
542// exact floating point arithmetic. This only handles numbers with low
543// exponents, but handles them quickly. This is an implementation of Clinger's
544// Fast Path, as described above.
545template <class T>
546LIBC_INLINE cpp::optional<ExpandedFloat<T>>
547clinger_fast_path(ExpandedFloat<T> init_num,
548 RoundDirection round = RoundDirection::Nearest) {
549 using FPBits = typename fputil::FPBits<T>;
550 using StorageType = typename FPBits::StorageType;
551
552 StorageType mantissa = init_num.mantissa;
553 int32_t exp10 = init_num.exponent;
554
555 if ((mantissa >> FPBits::FRACTION_LEN) > 0) {
556 return cpp::nullopt;
557 }
558
559 FPBits result;
560 T float_mantissa;
561 if constexpr (is_big_int_v<StorageType> || sizeof(T) > sizeof(uint64_t)) {
562 float_mantissa =
563 (static_cast<T>(uint64_t(mantissa >> 64)) * static_cast<T>(0x1.0p64)) +
564 static_cast<T>(uint64_t(mantissa));
565 } else {
566 float_mantissa = static_cast<T>(mantissa);
567 }
568
569 if (exp10 == 0) {
570 result = FPBits(float_mantissa);
571 }
572 if (exp10 > 0) {
573 if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN +
574 ClingerConsts<T>::DIGITS_IN_MANTISSA) {
575 return cpp::nullopt;
576 }
577 if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
578 float_mantissa = float_mantissa *
579 ClingerConsts<T>::POWERS_OF_TEN_ARRAY
580 [exp10 - ClingerConsts<T>::EXACT_POWERS_OF_TEN];
581 exp10 = ClingerConsts<T>::EXACT_POWERS_OF_TEN;
582 }
583 if (float_mantissa > ClingerConsts<T>::MAX_EXACT_INT) {
584 return cpp::nullopt;
585 }
586 result =
587 FPBits(float_mantissa * ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
588 } else if (exp10 < 0) {
589 if (-exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
590 return cpp::nullopt;
591 }
592 result =
593 FPBits(float_mantissa / ClingerConsts<T>::POWERS_OF_TEN_ARRAY[-exp10]);
594 }
595
596 // If the rounding mode is not nearest, then the sign of the number may affect
597 // the result. To make sure the rounding mode is respected properly, the
598 // calculation is redone with a negative result, and the rounding mode is used
599 // to select the correct result.
600 if (round != RoundDirection::Nearest) {
601 FPBits negative_result;
602 // I'm 99% sure this will break under fast math optimizations.
603 negative_result = FPBits((-float_mantissa) *
604 ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
605
606 // If the results are equal, then we don't need to use the rounding mode.
607 if (result.get_val() != -negative_result.get_val()) {
608 FPBits lower_result;
609 FPBits higher_result;
610
611 if (result.get_val() < -negative_result.get_val()) {
612 lower_result = result;
613 higher_result = negative_result;
614 } else {
615 lower_result = negative_result;
616 higher_result = result;
617 }
618
619 if (round == RoundDirection::Up) {
620 result = higher_result;
621 } else {
622 result = lower_result;
623 }
624 }
625 }
626
627 ExpandedFloat<T> output;
628 output.mantissa = result.get_explicit_mantissa();
629 output.exponent = result.get_biased_exponent();
630 return output;
631}
632
633// The upper bound is the highest base-10 exponent that could possibly give a
634// non-inf result for this size of float. The value is
635// log10(2^(exponent bias)).
636// The generic approximation uses the fact that log10(2^x) ~= x/3
637template <typename T> LIBC_INLINE constexpr int32_t get_upper_bound() {
638 return fputil::FPBits<T>::EXP_BIAS / 3;
639}
640
641template <> LIBC_INLINE constexpr int32_t get_upper_bound<float>() {
642 return 39;
643}
644
645template <> LIBC_INLINE constexpr int32_t get_upper_bound<double>() {
646 return 309;
647}
648
649// The lower bound is the largest negative base-10 exponent that could possibly
650// give a non-zero result for this size of float. The value is
651// log10(2^(exponent bias + final mantissa width + intermediate mantissa width))
652// The intermediate mantissa is the integer that's been parsed from the string,
653// and the final mantissa is the fractional part of the output number. A very
654// low base 10 exponent with a very high intermediate mantissa can cancel each
655// other out, and subnormal numbers allow for the result to be at the very low
656// end of the final mantissa.
657template <typename T> LIBC_INLINE constexpr int32_t get_lower_bound() {
658 using FPBits = typename fputil::FPBits<T>;
659 return -((FPBits::EXP_BIAS +
660 static_cast<int32_t>(FPBits::FRACTION_LEN + FPBits::STORAGE_LEN)) /
661 3);
662}
663
664template <> LIBC_INLINE constexpr int32_t get_lower_bound<float>() {
665 return -(39 + 6 + 10);
666}
667
668template <> LIBC_INLINE constexpr int32_t get_lower_bound<double>() {
669 return -(309 + 15 + 20);
670}
671
672// -----------------------------------------------------------------------------
673// **** WARNING ****
674// This interface is shared with libc++, if you change this interface you need
675// to update it in both libc and libc++.
676// -----------------------------------------------------------------------------
677// Takes a mantissa and base 10 exponent and converts it into its closest
678// floating point type T equivalient. First we try the Eisel-Lemire algorithm,
679// then if that fails then we fall back to a more accurate algorithm for
680// accuracy.
681template <typename T, typename CharType>
682LIBC_INLINE FloatConvertReturn<T> decimal_exp_to_float(
683 ExpandedFloat<T> init_num, bool truncated, RoundDirection round,
684 const CharType *__restrict numStart,
685 const size_t num_len = cpp::numeric_limits<size_t>::max()) {
686 using FPBits = typename fputil::FPBits<T>;
687
688 int32_t exp10 = init_num.exponent;
689
690 FloatConvertReturn<T> output;
691 cpp::optional<ExpandedFloat<T>> opt_output;
692
693 // If the exponent is too large and can't be represented in this size of
694 // float, return inf. These bounds are relatively loose, but are mostly
695 // serving as a first pass. Some close numbers getting through is okay.
696 if (exp10 > get_upper_bound<T>()) {
697 output.num = {0, FPBits::MAX_BIASED_EXPONENT};
698 output.error = ERANGE;
699 return output;
700 }
701 // If the exponent is too small even for a subnormal, return 0.
702 if (exp10 < get_lower_bound<T>()) {
703 output.num = {0, 0};
704 output.error = ERANGE;
705 return output;
706 }
707
708 // Clinger's Fast Path and Eisel-Lemire can't set errno, but they can fail.
709 // For this reason the "error" field in their return values is used to
710 // represent whether they've failed as opposed to the errno value. Any
711 // non-zero value represents a failure.
712
713#ifndef LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH
714 if (!truncated) {
715 opt_output = clinger_fast_path<T>(init_num, round);
716 // If the algorithm succeeded the error will be 0, else it will be a
717 // non-zero number.
718 if (opt_output.has_value()) {
719 return {opt_output.value(), 0};
720 }
721 }
722#endif // LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH
723
724#ifndef LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE
725 // Try Eisel-Lemire
726 using StorageType = typename FPBits::StorageType;
727 StorageType mantissa = init_num.mantissa;
728 opt_output = eisel_lemire<T>(init_num, round);
729 if (opt_output.has_value()) {
730 if (!truncated) {
731 return {opt_output.value(), 0};
732 }
733 // If the mantissa is truncated, then the result may be off by the LSB, so
734 // check if rounding the mantissa up changes the result. If not, then it's
735 // safe, else use the fallback.
736 auto second_output = eisel_lemire<T>({mantissa + 1, exp10}, round);
737 if (second_output.has_value()) {
738 if (opt_output->mantissa == second_output->mantissa &&
739 opt_output->exponent == second_output->exponent) {
740 return {opt_output.value(), 0};
741 }
742 }
743 }
744#endif // LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE
745
746#ifndef LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION
747 output = simple_decimal_conversion<T>(numStart, num_len, round);
748#else
749#warning "Simple decimal conversion is disabled, result may not be correct."
750#endif // LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION
751
752 return output;
753}
754
755// -----------------------------------------------------------------------------
756// **** WARNING ****
757// This interface is shared with libc++, if you change this interface you need
758// to update it in both libc and libc++.
759// -----------------------------------------------------------------------------
760// Takes a mantissa and base 2 exponent and converts it into its closest
761// floating point type T equivalient. Since the exponent is already in the right
762// form, this is mostly just shifting and rounding. This is used for hexadecimal
763// numbers since a base 16 exponent multiplied by 4 is the base 2 exponent.
764template <class T>
765LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
766 bool truncated,
767 RoundDirection round) {
768 using FPBits = typename fputil::FPBits<T>;
769 using StorageType = typename FPBits::StorageType;
770
771 StorageType mantissa = init_num.mantissa;
772 int32_t exp2 = init_num.exponent;
773
774 FloatConvertReturn<T> output;
775
776 // This is the number of leading zeroes a properly normalized float of type T
777 // should have.
778 constexpr int32_t INF_EXP = (1 << FPBits::EXP_LEN) - 1;
779
780 // Normalization step 1: Bring the leading bit to the highest bit of
781 // StorageType.
782 uint32_t amount_to_shift_left = cpp::countl_zero<StorageType>(mantissa);
783 mantissa <<= amount_to_shift_left;
784
785 // Keep exp2 representing the exponent of the lowest bit of StorageType.
786 exp2 -= amount_to_shift_left;
787
788 // biased_exponent represents the biased exponent of the most significant bit.
789 int32_t biased_exponent = exp2 + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - 1;
790
791 // Handle numbers that're too large and get squashed to inf
792 if (biased_exponent >= INF_EXP) {
793 // This indicates an overflow, so we make the result INF and set errno.
794 output.num = {0, (1 << FPBits::EXP_LEN) - 1};
795 output.error = ERANGE;
796 return output;
797 }
798
799 uint32_t amount_to_shift_right =
800 FPBits::STORAGE_LEN - FPBits::FRACTION_LEN - 1;
801
802 // Handle subnormals.
803 if (biased_exponent <= 0) {
804 amount_to_shift_right += static_cast<uint32_t>(1 - biased_exponent);
805 biased_exponent = 0;
806
807 if (amount_to_shift_right > FPBits::STORAGE_LEN) {
808 // Return 0 if the exponent is too small.
809 output.num = {0, 0};
810 output.error = ERANGE;
811 return output;
812 }
813 }
814
815 StorageType round_bit_mask = StorageType(1) << (amount_to_shift_right - 1);
816 StorageType sticky_mask = round_bit_mask - 1;
817 bool round_bit = static_cast<bool>(mantissa & round_bit_mask);
818 bool sticky_bit = static_cast<bool>(mantissa & sticky_mask) || truncated;
819
820 if (amount_to_shift_right < FPBits::STORAGE_LEN) {
821 // Shift the mantissa and clear the implicit bit.
822 mantissa >>= amount_to_shift_right;
823 mantissa &= FPBits::FRACTION_MASK;
824 } else {
825 mantissa = 0;
826 }
827 bool least_significant_bit = static_cast<bool>(mantissa & StorageType(1));
828
829 // TODO: check that this rounding behavior is correct.
830
831 if (round == RoundDirection::Nearest) {
832 // Perform rounding-to-nearest, tie-to-even.
833 if (round_bit && (least_significant_bit || sticky_bit)) {
834 ++mantissa;
835 }
836 } else if (round == RoundDirection::Up) {
837 if (round_bit || sticky_bit) {
838 ++mantissa;
839 }
840 } else /* (round == RoundDirection::Down)*/ {
841 if (round_bit && sticky_bit) {
842 ++mantissa;
843 }
844 }
845
846 if (mantissa > FPBits::FRACTION_MASK) {
847 // Rounding causes the exponent to increase.
848 ++biased_exponent;
849
850 if (biased_exponent == INF_EXP) {
851 output.error = ERANGE;
852 }
853 }
854
855 if (biased_exponent == 0) {
856 output.error = ERANGE;
857 }
858
859 output.num = {mantissa & FPBits::FRACTION_MASK, biased_exponent};
860 return output;
861}
862
863// Checks if the first characters of the string pointer are the start of a
864// hexadecimal floating point number. Does not advance the string pointer.
865template <typename CharType>
866LIBC_INLINE static bool is_float_hex_start(const CharType *__restrict src) {
867 if (!is_char_or_wchar(src[0], '0', L'0') ||
868 !is_char_or_wchar(tolower(src[1]), 'x', L'x')) {
869 return false;
870 }
871 size_t first_digit = 2;
872 if (src[2] == constants<CharType>::DECIMAL_POINT) {
873 ++first_digit;
874 }
875 return isalnum(src[first_digit]) && b36_char_to_int(src[first_digit]) < 16;
876}
877
878// Verifies that first prefix_len characters of str, when lowercased, match the
879// specified prefix.
880template <typename CharType>
881LIBC_INLINE static bool tolower_starts_with(const CharType *str,
882 size_t prefix_len,
883 const CharType *prefix) {
884 for (size_t i = 0; i < prefix_len; ++i) {
885 if (tolower(str[i]) != prefix[i])
886 return false;
887 }
888 return true;
889}
890
891// Attempts parsing a decimal floating point number at the start of the string.
892template <typename T, typename CharType>
893LIBC_INLINE static StrToNumResult<ExpandedFloat<T>>
894decimal_string_to_float(const CharType *__restrict src, RoundDirection round) {
895 using FPBits = typename fputil::FPBits<T>;
896 using StorageType = typename FPBits::StorageType;
897
898 constexpr uint32_t BASE = 10;
899 bool truncated = false;
900 bool seen_digit = false;
901 bool after_decimal = false;
902 StorageType mantissa = 0;
903 int32_t exponent = 0;
904
905 size_t index = 0;
906
907 StrToNumResult<ExpandedFloat<T>> output({0, 0});
908
909 // The goal for the first step of parsing is to convert the number in src to
910 // the format mantissa * (base ^ exponent)
911
912 // The loop fills the mantissa with as many digits as it can hold
913 const StorageType bitstype_max_div_by_base =
914 cpp::numeric_limits<StorageType>::max() / BASE;
915 while (true) {
916 if (isdigit(src[index])) {
917 uint32_t digit = static_cast<uint32_t>(b36_char_to_int(src[index]));
918 seen_digit = true;
919
920 if (mantissa < bitstype_max_div_by_base) {
921 mantissa = (mantissa * BASE) + digit;
922 if (after_decimal) {
923 --exponent;
924 }
925 } else {
926 if (digit > 0)
927 truncated = true;
928 if (!after_decimal)
929 ++exponent;
930 }
931
932 ++index;
933 continue;
934 }
935 if (src[index] == constants<CharType>::DECIMAL_POINT) {
936 if (after_decimal) {
937 break; // this means that src[index] points to a second decimal point,
938 // ending the number.
939 }
940 after_decimal = true;
941 ++index;
942 continue;
943 }
944 // The character is neither a digit nor a decimal point.
945 break;
946 }
947
948 if (!seen_digit)
949 return output;
950
951 // TODO: When adding max length argument, handle the case of a trailing
952 // exponent marker, see scanf for more details.
953 if (tolower(src[index]) == constants<CharType>::DECIMAL_EXPONENT_MARKER) {
954 int sign = get_sign(src + index + 1);
955 if (isdigit(src[index + 1 + static_cast<size_t>(sign != 0)])) {
956 ++index;
957 auto result = strtointeger<int32_t>(src + index, 10);
958 if (result.has_error())
959 output.error = result.error;
960 int32_t add_to_exponent = result.value;
961 index += static_cast<size_t>(result.parsed_len);
962
963 // Here we do this operation as int64 to avoid overflow.
964 int64_t temp_exponent = static_cast<int64_t>(exponent) +
965 static_cast<int64_t>(add_to_exponent);
966
967 // If the result is in the valid range, then we use it. The valid range is
968 // also within the int32 range, so this prevents overflow issues.
969 if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
970 exponent = FPBits::MAX_BIASED_EXPONENT;
971 } else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
972 exponent = -FPBits::MAX_BIASED_EXPONENT;
973 } else {
974 exponent = static_cast<int32_t>(temp_exponent);
975 }
976 }
977 }
978
979 output.parsed_len = index;
980 if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
981 output.value = {0, 0};
982 } else {
983 auto temp =
984 decimal_exp_to_float<T>({mantissa, exponent}, truncated, round, src);
985 output.value = temp.num;
986 output.error = temp.error;
987 }
988 return output;
989}
990
991// Attempts parsing a hexadecimal floating point number at the start of the
992// string.
993template <typename T, typename CharType>
994LIBC_INLINE static StrToNumResult<ExpandedFloat<T>>
995hexadecimal_string_to_float(const CharType *__restrict src,
996 RoundDirection round) {
997 using FPBits = typename fputil::FPBits<T>;
998 using StorageType = typename FPBits::StorageType;
999
1000 constexpr uint32_t BASE = 16;
1001 bool truncated = false;
1002 bool seen_digit = false;
1003 bool after_decimal = false;
1004 StorageType mantissa = 0;
1005 int32_t exponent = 0;
1006
1007 size_t index = 0;
1008
1009 StrToNumResult<ExpandedFloat<T>> output({0, 0});
1010
1011 // The goal for the first step of parsing is to convert the number in src to
1012 // the format mantissa * (base ^ exponent)
1013
1014 // The loop fills the mantissa with as many digits as it can hold
1015 const StorageType bitstype_max_div_by_base =
1016 cpp::numeric_limits<StorageType>::max() / BASE;
1017 while (true) {
1018 if (isalnum(src[index])) {
1019 uint32_t digit = static_cast<uint32_t>(b36_char_to_int(src[index]));
1020 if (digit < BASE)
1021 seen_digit = true;
1022 else
1023 break;
1024
1025 if (mantissa < bitstype_max_div_by_base) {
1026 mantissa = (mantissa * BASE) + digit;
1027 if (after_decimal)
1028 --exponent;
1029 } else {
1030 if (digit > 0)
1031 truncated = true;
1032 if (!after_decimal)
1033 ++exponent;
1034 }
1035 ++index;
1036 continue;
1037 }
1038 if (src[index] == constants<CharType>::DECIMAL_POINT) {
1039 if (after_decimal) {
1040 break; // this means that src[index] points to a second decimal point,
1041 // ending the number.
1042 }
1043 after_decimal = true;
1044 ++index;
1045 continue;
1046 }
1047 // The character is neither a hexadecimal digit nor a decimal point.
1048 break;
1049 }
1050
1051 if (!seen_digit)
1052 return output;
1053
1054 // Convert the exponent from having a base of 16 to having a base of 2.
1055 exponent *= 4;
1056
1057 if (tolower(src[index]) == constants<CharType>::HEX_EXPONENT_MARKER) {
1058 int sign = get_sign(src + index + 1);
1059 if (isdigit(src[index + 1 + static_cast<size_t>(sign != 0)])) {
1060 ++index;
1061 auto result = strtointeger<int32_t>(src + index, 10);
1062 if (result.has_error())
1063 output.error = result.error;
1064
1065 int32_t add_to_exponent = result.value;
1066 index += static_cast<size_t>(result.parsed_len);
1067
1068 // Here we do this operation as int64 to avoid overflow.
1069 int64_t temp_exponent = static_cast<int64_t>(exponent) +
1070 static_cast<int64_t>(add_to_exponent);
1071
1072 // If the result is in the valid range, then we use it. The valid range is
1073 // also within the int32 range, so this prevents overflow issues.
1074 if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
1075 exponent = FPBits::MAX_BIASED_EXPONENT;
1076 } else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
1077 exponent = -FPBits::MAX_BIASED_EXPONENT;
1078 } else {
1079 exponent = static_cast<int32_t>(temp_exponent);
1080 }
1081 }
1082 }
1083 output.parsed_len = index;
1084 if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
1085 output.value.exponent = 0;
1086 output.value.mantissa = 0;
1087 } else {
1088 auto temp = binary_exp_to_float<T>({mantissa, exponent}, truncated, round);
1089 output.error = temp.error;
1090 output.value = temp.num;
1091 }
1092 return output;
1093}
1094
1095template <typename T, typename CharType>
1096LIBC_INLINE typename fputil::FPBits<T>::StorageType
1097nan_mantissa_from_ncharseq(const CharType *str, size_t len) {
1098 using FPBits = typename fputil::FPBits<T>;
1099 using StorageType = typename FPBits::StorageType;
1100
1101 StorageType nan_mantissa = 0;
1102
1103 if (len > 0 && isdigit(str[0])) {
1104 StrToNumResult<StorageType> strtoint_result =
1105 strtointeger<StorageType>(str, 0, len);
1106 if (!strtoint_result.has_error())
1107 nan_mantissa = strtoint_result.value;
1108
1109 if (strtoint_result.parsed_len != static_cast<ptrdiff_t>(len))
1110 nan_mantissa = 0;
1111 }
1112
1113 return nan_mantissa;
1114}
1115
1116// Takes a pointer to a string and a pointer to a string pointer. This function
1117// is used as the backend for all of the string to float functions.
1118// TODO: Add src_len member to match strtointeger.
1119// TODO: Next, move from char* and length to string_view
1120template <typename T, typename CharType>
1121LIBC_INLINE StrToNumResult<T>
1122strtofloatingpoint(const CharType *__restrict src) {
1123 using FPBits = typename fputil::FPBits<T>;
1124 using StorageType = typename FPBits::StorageType;
1125
1126 FPBits result = FPBits();
1127 bool seen_digit = false;
1128 int error = 0;
1129
1130 size_t index = first_non_whitespace(src);
1131 int sign = get_sign(src + index);
1132 bool is_positive = (sign >= 0);
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 switch (fputil::quick_get_round()) {
1150 case FE_TONEAREST:
1151 round_direction = RoundDirection::Nearest;
1152 break;
1153 case FE_UPWARD:
1154 round_direction = is_positive ? RoundDirection::Up : RoundDirection::Down;
1155 break;
1156 case FE_DOWNWARD:
1157 round_direction = is_positive ? RoundDirection::Down : RoundDirection::Up;
1158 break;
1159 case FE_TOWARDZERO:
1160 round_direction = RoundDirection::Down;
1161 break;
1162 }
1163
1164 StrToNumResult<ExpandedFloat<T>> parse_result({0, 0});
1165 if (base == 16) {
1166 parse_result =
1167 hexadecimal_string_to_float<T>(src + index, round_direction);
1168 } else { // base is 10
1169 parse_result = decimal_string_to_float<T>(src + index, round_direction);
1170 }
1171 seen_digit = parse_result.parsed_len != 0;
1172 result.set_mantissa(parse_result.value.mantissa);
1173 result.set_biased_exponent(parse_result.value.exponent);
1174 index += parse_result.parsed_len;
1175 error = parse_result.error;
1176 } else if (tolower_starts_with(src + index, 3,
1177 constants<CharType>::NAN_STRING)) {
1178 // NAN
1179 seen_digit = true;
1180 index += 3;
1181 StorageType nan_mantissa = 0;
1182 // this handles the case of `NaN(n-character-sequence)`, where the
1183 // n-character-sequence is made of 0 or more letters, numbers, or
1184 // underscore characters in any order.
1185 if (is_char_or_wchar(src[index], '(', L'(')) {
1186 size_t left_paren = index;
1187 ++index;
1188 while (isalnum(src[index]) || is_char_or_wchar(src[index], '_', L'_'))
1189 ++index;
1190 if (is_char_or_wchar(src[index], ')', L')')) {
1191 ++index;
1192 nan_mantissa = nan_mantissa_from_ncharseq<T>(src + (left_paren + 1),
1193 index - left_paren - 2);
1194 } else {
1195 index = left_paren;
1196 }
1197 }
1198 result = FPBits(result.quiet_nan(result.sign(), nan_mantissa));
1199 } else if (tolower_starts_with(src + index, 8,
1200 constants<CharType>::INF_STRING)) {
1201 // INFINITY
1202 seen_digit = true;
1203 result = FPBits(result.inf(result.sign()));
1204 index += 8;
1205 } else if (tolower_starts_with(src + index, 3,
1206 constants<CharType>::INF_STRING)) {
1207 // INF
1208 seen_digit = true;
1209 result = FPBits(result.inf(result.sign()));
1210 index += 3;
1211 }
1212
1213 if (!seen_digit) { // If there is nothing to actually parse, then return 0.
1214 return {T(0), 0, error};
1215 }
1216
1217 // This function only does something if T is long double and the platform uses
1218 // special 80 bit long doubles. Otherwise it should be inlined out.
1219 set_implicit_bit<T>(result);
1220
1221 return {result.get_val(), static_cast<ptrdiff_t>(index), error};
1222}
1223
1224template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
1225 using FPBits = typename fputil::FPBits<T>;
1226 using StorageType = typename FPBits::StorageType;
1227
1228 LIBC_CRASH_ON_NULLPTR(arg);
1229
1230 FPBits result;
1231 int error = 0;
1232 StorageType nan_mantissa = 0;
1233
1234 ptrdiff_t index = 0;
1235 while (isalnum(ch: arg[index]) || arg[index] == '_')
1236 ++index;
1237
1238 if (arg[index] == '\0')
1239 nan_mantissa = nan_mantissa_from_ncharseq<T>(arg, index);
1240
1241 result = FPBits::quiet_nan(Sign::POS, nan_mantissa);
1242 return {result.get_val(), 0, error};
1243}
1244
1245} // namespace internal
1246} // namespace LIBC_NAMESPACE_DECL
1247
1248#endif // LLVM_LIBC_SRC___SUPPORT_STR_TO_FLOAT_H
1249