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, and once more to compensate
422 // for the left shift to get it between 1 and 2.
423 hpd.shift(shift_amount: exp2 - 1);
424 exp2 = 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 ++exp2;
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 ++exp2;
437
438 // Check if this rounding causes exp2 to go out of range and make the result
439 // INF. If this is the case, then finalMantissa and exp2 are already the
440 // correct values for an INF result.
441 if (exp2 >= FPBits::MAX_BIASED_EXPONENT) {
442 output.error = ERANGE;
443 }
444 }
445
446 if (exp2 == 0) {
447 output.error = ERANGE;
448 }
449
450 output.num = {final_mantissa, exp2};
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 exp10 = 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 (exp10 == 0) {
566 result = FPBits(float_mantissa);
567 }
568 if (exp10 > 0) {
569 if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN +
570 ClingerConsts<T>::DIGITS_IN_MANTISSA) {
571 return cpp::nullopt;
572 }
573 if (exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
574 float_mantissa = float_mantissa *
575 ClingerConsts<T>::POWERS_OF_TEN_ARRAY
576 [exp10 - ClingerConsts<T>::EXACT_POWERS_OF_TEN];
577 exp10 = 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[exp10]);
584 } else if (exp10 < 0) {
585 if (-exp10 > ClingerConsts<T>::EXACT_POWERS_OF_TEN) {
586 return cpp::nullopt;
587 }
588 result =
589 FPBits(float_mantissa / ClingerConsts<T>::POWERS_OF_TEN_ARRAY[-exp10]);
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 FPBits negative_result;
598 // I'm 99% sure this will break under fast math optimizations.
599 negative_result = FPBits((-float_mantissa) *
600 ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
601
602 // If the results are equal, then we don't need to use the rounding mode.
603 if (result.get_val() != -negative_result.get_val()) {
604 FPBits lower_result;
605 FPBits higher_result;
606
607 if (result.get_val() < -negative_result.get_val()) {
608 lower_result = result;
609 higher_result = negative_result;
610 } else {
611 lower_result = negative_result;
612 higher_result = result;
613 }
614
615 if (round == RoundDirection::Up) {
616 result = higher_result;
617 } else {
618 result = lower_result;
619 }
620 }
621 }
622
623 ExpandedFloat<T> output;
624 output.mantissa = result.get_explicit_mantissa();
625 output.exponent = result.get_biased_exponent();
626 return output;
627}
628
629// The upper bound is the highest base-10 exponent that could possibly give a
630// non-inf result for this size of float. The value is
631// log10(2^(exponent bias)).
632// The generic approximation uses the fact that log10(2^x) ~= x/3
633template <typename T> LIBC_INLINE constexpr int32_t get_upper_bound() {
634 return fputil::FPBits<T>::EXP_BIAS / 3;
635}
636
637template <> LIBC_INLINE constexpr int32_t get_upper_bound<float>() {
638 return 39;
639}
640
641template <> LIBC_INLINE constexpr int32_t get_upper_bound<double>() {
642 return 309;
643}
644
645// The lower bound is the largest negative base-10 exponent that could possibly
646// give a non-zero result for this size of float. The value is
647// log10(2^(exponent bias + final mantissa width + intermediate mantissa width))
648// The intermediate mantissa is the integer that's been parsed from the string,
649// and the final mantissa is the fractional part of the output number. A very
650// low base 10 exponent with a very high intermediate mantissa can cancel each
651// other out, and subnormal numbers allow for the result to be at the very low
652// end of the final mantissa.
653template <typename T> LIBC_INLINE constexpr int32_t get_lower_bound() {
654 using FPBits = typename fputil::FPBits<T>;
655 return -((FPBits::EXP_BIAS +
656 static_cast<int32_t>(FPBits::FRACTION_LEN + FPBits::STORAGE_LEN)) /
657 3);
658}
659
660template <> LIBC_INLINE constexpr int32_t get_lower_bound<float>() {
661 return -(39 + 6 + 10);
662}
663
664template <> LIBC_INLINE constexpr int32_t get_lower_bound<double>() {
665 return -(309 + 15 + 20);
666}
667
668// -----------------------------------------------------------------------------
669// **** WARNING ****
670// This interface is shared with libc++, if you change this interface you need
671// to update it in both libc and libc++.
672// -----------------------------------------------------------------------------
673// Takes a mantissa and base 10 exponent and converts it into its closest
674// floating point type T equivalient. First we try the Eisel-Lemire algorithm,
675// then if that fails then we fall back to a more accurate algorithm for
676// accuracy.
677template <typename T, typename CharType>
678LIBC_INLINE FloatConvertReturn<T> decimal_exp_to_float(
679 ExpandedFloat<T> init_num, [[maybe_unused]] bool truncated,
680 RoundDirection round, const CharType *__restrict numStart,
681 const size_t num_len = cpp::numeric_limits<size_t>::max()) {
682 using FPBits = typename fputil::FPBits<T>;
683
684 int32_t exp10 = init_num.exponent;
685
686 FloatConvertReturn<T> output;
687 [[maybe_unused]] cpp::optional<ExpandedFloat<T>> opt_output;
688
689 // If the exponent is too large and can't be represented in this size of
690 // float, return inf. These bounds are relatively loose, but are mostly
691 // serving as a first pass. Some close numbers getting through is okay.
692 if (exp10 > get_upper_bound<T>()) {
693 output.num = {0, FPBits::MAX_BIASED_EXPONENT};
694 output.error = ERANGE;
695 return output;
696 }
697 // If the exponent is too small even for a subnormal, return 0.
698 if (exp10 < get_lower_bound<T>()) {
699 output.num = {0, 0};
700 output.error = ERANGE;
701 return output;
702 }
703
704 // Clinger's Fast Path and Eisel-Lemire can't set errno, but they can fail.
705 // For this reason the "error" field in their return values is used to
706 // represent whether they've failed as opposed to the errno value. Any
707 // non-zero value represents a failure.
708
709#ifndef LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH
710 if (!truncated) {
711 opt_output = clinger_fast_path<T>(init_num, round);
712 // If the algorithm succeeded the error will be 0, else it will be a
713 // non-zero number.
714 if (opt_output.has_value()) {
715 return {opt_output.value(), 0};
716 }
717 }
718#endif // LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH
719
720#ifndef LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE
721 // Try Eisel-Lemire
722 using StorageType = typename FPBits::StorageType;
723 StorageType mantissa = init_num.mantissa;
724 opt_output = eisel_lemire<T>(init_num, round);
725 if (opt_output.has_value()) {
726 if (!truncated) {
727 return {opt_output.value(), 0};
728 }
729 // If the mantissa is truncated, then the result may be off by the LSB, so
730 // check if rounding the mantissa up changes the result. If not, then it's
731 // safe, else use the fallback.
732 auto second_output = eisel_lemire<T>({mantissa + 1, exp10}, round);
733 if (second_output.has_value()) {
734 if (opt_output->mantissa == second_output->mantissa &&
735 opt_output->exponent == second_output->exponent) {
736 return {opt_output.value(), 0};
737 }
738 }
739 }
740#endif // LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE
741
742#ifndef LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION
743 output = simple_decimal_conversion<T>(numStart, num_len, round);
744#else
745#warning "Simple decimal conversion is disabled, result may not be correct."
746#endif // LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION
747
748 return output;
749}
750
751// -----------------------------------------------------------------------------
752// **** WARNING ****
753// This interface is shared with libc++, if you change this interface you need
754// to update it in both libc and libc++.
755// -----------------------------------------------------------------------------
756// Takes a mantissa and base 2 exponent and converts it into its closest
757// floating point type T equivalient. Since the exponent is already in the right
758// form, this is mostly just shifting and rounding. This is used for hexadecimal
759// numbers since a base 16 exponent multiplied by 4 is the base 2 exponent.
760template <class T>
761LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
762 bool truncated,
763 RoundDirection round) {
764 using FPBits = typename fputil::FPBits<T>;
765 using StorageType = typename FPBits::StorageType;
766
767 StorageType mantissa = init_num.mantissa;
768 int32_t exp2 = init_num.exponent;
769
770 FloatConvertReturn<T> output;
771
772 // This is the number of leading zeroes a properly normalized float of type T
773 // should have.
774 constexpr int32_t INF_EXP = (1 << FPBits::EXP_LEN) - 1;
775
776 // Normalization step 1: Bring the leading bit to the highest bit of
777 // StorageType.
778 uint32_t amount_to_shift_left = cpp::countl_zero<StorageType>(mantissa);
779 mantissa <<= amount_to_shift_left;
780
781 // Keep exp2 representing the exponent of the lowest bit of StorageType.
782 exp2 -= amount_to_shift_left;
783
784 // biased_exponent represents the biased exponent of the most significant bit.
785 int32_t biased_exponent = exp2 + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - 1;
786
787 // Handle numbers that're too large and get squashed to inf
788 if (biased_exponent >= INF_EXP) {
789 // This indicates an overflow, so we make the result INF and set errno.
790 output.num = {0, (1 << FPBits::EXP_LEN) - 1};
791 output.error = ERANGE;
792 return output;
793 }
794
795 uint32_t amount_to_shift_right =
796 FPBits::STORAGE_LEN - FPBits::FRACTION_LEN - 1;
797
798 // Handle subnormals.
799 if (biased_exponent <= 0) {
800 amount_to_shift_right += static_cast<uint32_t>(1 - biased_exponent);
801 biased_exponent = 0;
802
803 if (amount_to_shift_right > FPBits::STORAGE_LEN) {
804 // Return 0 if the exponent is too small.
805 output.num = {0, 0};
806 output.error = ERANGE;
807 return output;
808 }
809 }
810
811 StorageType round_bit_mask = StorageType(1) << (amount_to_shift_right - 1);
812 StorageType sticky_mask = round_bit_mask - 1;
813 bool round_bit = static_cast<bool>(mantissa & round_bit_mask);
814 bool sticky_bit = static_cast<bool>(mantissa & sticky_mask) || truncated;
815
816 if (amount_to_shift_right < FPBits::STORAGE_LEN) {
817 // Shift the mantissa and clear the implicit bit.
818 mantissa >>= amount_to_shift_right;
819 mantissa &= FPBits::FRACTION_MASK;
820 } else {
821 mantissa = 0;
822 }
823 bool least_significant_bit = static_cast<bool>(mantissa & StorageType(1));
824
825 // TODO: check that this rounding behavior is correct.
826
827 if (round == RoundDirection::Nearest) {
828 // Perform rounding-to-nearest, tie-to-even.
829 if (round_bit && (least_significant_bit || sticky_bit)) {
830 ++mantissa;
831 }
832 } else if (round == RoundDirection::Up) {
833 if (round_bit || sticky_bit) {
834 ++mantissa;
835 }
836 } else /* (round == RoundDirection::Down)*/ {
837 if (round_bit && sticky_bit) {
838 ++mantissa;
839 }
840 }
841
842 if (mantissa > FPBits::FRACTION_MASK) {
843 // Rounding causes the exponent to increase.
844 ++biased_exponent;
845
846 if (biased_exponent == INF_EXP) {
847 output.error = ERANGE;
848 }
849 }
850
851 if (biased_exponent == 0) {
852 output.error = ERANGE;
853 }
854
855 output.num = {mantissa & FPBits::FRACTION_MASK, biased_exponent};
856 return output;
857}
858
859// Checks if the first characters of the string pointer are the start of a
860// hexadecimal floating point number. Does not advance the string pointer.
861template <typename CharType>
862LIBC_INLINE static bool is_float_hex_start(const CharType *__restrict src) {
863 if (!is_char_or_wchar(src[0], '0', L'0') ||
864 !is_char_or_wchar(tolower(src[1]), 'x', L'x')) {
865 return false;
866 }
867 size_t first_digit = 2;
868 if (src[2] == constants<CharType>::DECIMAL_POINT) {
869 ++first_digit;
870 }
871 return isalnum(src[first_digit]) && b36_char_to_int(src[first_digit]) < 16;
872}
873
874// Verifies that first prefix_len characters of str, when lowercased, match the
875// specified prefix.
876template <typename CharType>
877LIBC_INLINE static bool tolower_starts_with(const CharType *str,
878 size_t prefix_len,
879 const CharType *prefix) {
880 for (size_t i = 0; i < prefix_len; ++i) {
881 if (tolower(str[i]) != prefix[i])
882 return false;
883 }
884 return true;
885}
886
887// Attempts parsing a decimal floating point number at the start of the string.
888template <typename T, typename CharType>
889LIBC_INLINE static StrToNumResult<ExpandedFloat<T>>
890decimal_string_to_float(const CharType *__restrict src, RoundDirection round) {
891 using FPBits = typename fputil::FPBits<T>;
892 using StorageType = typename FPBits::StorageType;
893
894 constexpr uint32_t BASE = 10;
895 bool truncated = false;
896 bool seen_digit = false;
897 bool after_decimal = false;
898 StorageType mantissa = 0;
899 int32_t exponent = 0;
900
901 size_t index = 0;
902
903 StrToNumResult<ExpandedFloat<T>> output({0, 0});
904
905 // The goal for the first step of parsing is to convert the number in src to
906 // the format mantissa * (base ^ exponent)
907
908 // The loop fills the mantissa with as many digits as it can hold
909 const StorageType bitstype_max_div_by_base =
910 cpp::numeric_limits<StorageType>::max() / BASE;
911 while (true) {
912 if (isdigit(src[index])) {
913 uint32_t digit = static_cast<uint32_t>(b36_char_to_int(src[index]));
914 seen_digit = true;
915
916 if (mantissa < bitstype_max_div_by_base) {
917 mantissa = (mantissa * BASE) + digit;
918 if (after_decimal) {
919 --exponent;
920 }
921 } else {
922 if (digit > 0)
923 truncated = true;
924 if (!after_decimal)
925 ++exponent;
926 }
927
928 ++index;
929 continue;
930 }
931 if (src[index] == constants<CharType>::DECIMAL_POINT) {
932 if (after_decimal) {
933 break; // this means that src[index] points to a second decimal point,
934 // ending the number.
935 }
936 after_decimal = true;
937 ++index;
938 continue;
939 }
940 // The character is neither a digit nor a decimal point.
941 break;
942 }
943
944 if (!seen_digit)
945 return output;
946
947 // TODO: When adding max length argument, handle the case of a trailing
948 // exponent marker, see scanf for more details.
949 if (tolower(src[index]) == constants<CharType>::DECIMAL_EXPONENT_MARKER) {
950 int sign = get_sign(src + index + 1);
951 if (isdigit(src[index + 1 + static_cast<size_t>(sign != 0)])) {
952 ++index;
953 auto result = strtointeger<int32_t>(src + index, 10);
954 if (result.has_error())
955 output.error = result.error;
956 int32_t add_to_exponent = result.value;
957 index += static_cast<size_t>(result.parsed_len);
958
959 // Here we do this operation as int64 to avoid overflow.
960 int64_t temp_exponent = static_cast<int64_t>(exponent) +
961 static_cast<int64_t>(add_to_exponent);
962
963 // If the result is in the valid range, then we use it. The valid range is
964 // also within the int32 range, so this prevents overflow issues.
965 if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
966 exponent = FPBits::MAX_BIASED_EXPONENT;
967 } else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
968 exponent = -FPBits::MAX_BIASED_EXPONENT;
969 } else {
970 exponent = static_cast<int32_t>(temp_exponent);
971 }
972 }
973 }
974
975 output.parsed_len = index;
976 if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
977 output.value = {0, 0};
978 } else {
979 auto temp =
980 decimal_exp_to_float<T>({mantissa, exponent}, truncated, round, src);
981 output.value = temp.num;
982 output.error = temp.error;
983 }
984 return output;
985}
986
987// Attempts parsing a hexadecimal floating point number at the start of the
988// string.
989template <typename T, typename CharType>
990LIBC_INLINE static StrToNumResult<ExpandedFloat<T>>
991hexadecimal_string_to_float(const CharType *__restrict src,
992 RoundDirection round) {
993 using FPBits = typename fputil::FPBits<T>;
994 using StorageType = typename FPBits::StorageType;
995
996 constexpr uint32_t BASE = 16;
997 bool truncated = false;
998 bool seen_digit = false;
999 bool after_decimal = false;
1000 StorageType mantissa = 0;
1001 int32_t exponent = 0;
1002
1003 size_t index = 0;
1004
1005 StrToNumResult<ExpandedFloat<T>> output({0, 0});
1006
1007 // The goal for the first step of parsing is to convert the number in src to
1008 // the format mantissa * (base ^ exponent)
1009
1010 // The loop fills the mantissa with as many digits as it can hold
1011 const StorageType bitstype_max_div_by_base =
1012 cpp::numeric_limits<StorageType>::max() / BASE;
1013 while (true) {
1014 if (isalnum(src[index])) {
1015 uint32_t digit = static_cast<uint32_t>(b36_char_to_int(src[index]));
1016 if (digit < BASE)
1017 seen_digit = true;
1018 else
1019 break;
1020
1021 if (mantissa < bitstype_max_div_by_base) {
1022 mantissa = (mantissa * BASE) + digit;
1023 if (after_decimal)
1024 --exponent;
1025 } else {
1026 if (digit > 0)
1027 truncated = true;
1028 if (!after_decimal)
1029 ++exponent;
1030 }
1031 ++index;
1032 continue;
1033 }
1034 if (src[index] == constants<CharType>::DECIMAL_POINT) {
1035 if (after_decimal) {
1036 break; // this means that src[index] points to a second decimal point,
1037 // ending the number.
1038 }
1039 after_decimal = true;
1040 ++index;
1041 continue;
1042 }
1043 // The character is neither a hexadecimal digit nor a decimal point.
1044 break;
1045 }
1046
1047 if (!seen_digit)
1048 return output;
1049
1050 // Convert the exponent from having a base of 16 to having a base of 2.
1051 exponent *= 4;
1052
1053 if (tolower(src[index]) == constants<CharType>::HEX_EXPONENT_MARKER) {
1054 int sign = get_sign(src + index + 1);
1055 if (isdigit(src[index + 1 + static_cast<size_t>(sign != 0)])) {
1056 ++index;
1057 auto result = strtointeger<int32_t>(src + index, 10);
1058 if (result.has_error())
1059 output.error = result.error;
1060
1061 int32_t add_to_exponent = result.value;
1062 index += static_cast<size_t>(result.parsed_len);
1063
1064 // Here we do this operation as int64 to avoid overflow.
1065 int64_t temp_exponent = static_cast<int64_t>(exponent) +
1066 static_cast<int64_t>(add_to_exponent);
1067
1068 // If the result is in the valid range, then we use it. The valid range is
1069 // also within the int32 range, so this prevents overflow issues.
1070 if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
1071 exponent = FPBits::MAX_BIASED_EXPONENT;
1072 } else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
1073 exponent = -FPBits::MAX_BIASED_EXPONENT;
1074 } else {
1075 exponent = static_cast<int32_t>(temp_exponent);
1076 }
1077 }
1078 }
1079 output.parsed_len = index;
1080 if (mantissa == 0) { // if we have a 0, then also 0 the exponent.
1081 output.value.exponent = 0;
1082 output.value.mantissa = 0;
1083 } else {
1084 auto temp = binary_exp_to_float<T>({mantissa, exponent}, truncated, round);
1085 output.error = temp.error;
1086 output.value = temp.num;
1087 }
1088 return output;
1089}
1090
1091template <typename T, typename CharType>
1092LIBC_INLINE constexpr typename fputil::FPBits<T>::StorageType
1093nan_mantissa_from_ncharseq(const CharType *str, size_t len) {
1094 using FPBits = typename fputil::FPBits<T>;
1095 using StorageType = typename FPBits::StorageType;
1096
1097 StorageType nan_mantissa = 0;
1098
1099 if (len > 0 && isdigit(str[0])) {
1100 StrToNumResult<StorageType> strtoint_result =
1101 strtointeger<StorageType>(str, 0, len);
1102 if (!strtoint_result.has_error())
1103 nan_mantissa = strtoint_result.value;
1104
1105 if (strtoint_result.parsed_len != static_cast<ptrdiff_t>(len))
1106 nan_mantissa = 0;
1107 }
1108
1109 return nan_mantissa;
1110}
1111
1112// Takes a pointer to a string and a pointer to a string pointer. This function
1113// is used as the backend for all of the string to float functions.
1114// TODO: Add src_len member to match strtointeger.
1115// TODO: Next, move from char* and length to string_view
1116template <typename T, typename CharType>
1117LIBC_INLINE StrToNumResult<T>
1118strtofloatingpoint(const CharType *__restrict src) {
1119 using FPBits = typename fputil::FPBits<T>;
1120 using StorageType = typename FPBits::StorageType;
1121
1122 FPBits result = FPBits();
1123 bool seen_digit = false;
1124 int error = 0;
1125
1126 size_t index = first_non_whitespace(src);
1127 int sign = get_sign(src + index);
1128#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
1129 bool is_positive = (sign >= 0);
1130#endif
1131 index += (sign != 0);
1132
1133 if (sign < 0) {
1134 result.set_sign(Sign::NEG);
1135 }
1136
1137 if (isdigit(src[index]) ||
1138 src[index] == constants<CharType>::DECIMAL_POINT) { // regular number
1139 int base = 10;
1140 if (is_float_hex_start(src + index)) {
1141 base = 16;
1142 index += 2;
1143 seen_digit = true;
1144 }
1145
1146 RoundDirection round_direction = RoundDirection::Nearest;
1147#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
1148 switch (fputil::quick_get_round()) {
1149 case FE_TONEAREST:
1150 round_direction = RoundDirection::Nearest;
1151 break;
1152 case FE_UPWARD:
1153 round_direction = is_positive ? RoundDirection::Up : RoundDirection::Down;
1154 break;
1155 case FE_DOWNWARD:
1156 round_direction = is_positive ? RoundDirection::Down : RoundDirection::Up;
1157 break;
1158 case FE_TOWARDZERO:
1159 round_direction = RoundDirection::Down;
1160 break;
1161 }
1162#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
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>
1225LIBC_INLINE constexpr StrToNumResult<T> strtonan(const char *arg) {
1226 using FPBits = typename fputil::FPBits<T>;
1227 using StorageType = typename FPBits::StorageType;
1228
1229 LIBC_CRASH_ON_NULLPTR(arg);
1230
1231 FPBits result;
1232 int error = 0;
1233 StorageType nan_mantissa = 0;
1234
1235 ptrdiff_t index = 0;
1236 while (isalnum(ch: arg[index]) || arg[index] == '_')
1237 ++index;
1238
1239 if (arg[index] == '\0')
1240 nan_mantissa = nan_mantissa_from_ncharseq<T>(arg, index);
1241
1242 result = FPBits::quiet_nan(Sign::POS, nan_mantissa);
1243 return {result.get_val(), 0, error};
1244}
1245
1246} // namespace internal
1247} // namespace LIBC_NAMESPACE_DECL
1248
1249#endif // LLVM_LIBC_SRC___SUPPORT_STR_TO_FLOAT_H
1250