1 // std::from_chars implementation for floating-point types -*- C++ -*-
3 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 // 23.2.9 Primitive numeric input conversion [utility.from.chars]
30 // Prefer to use std::pmr::string if possible, which requires the cxx11 ABI.
31 #define _GLIBCXX_USE_CXX11_ABI 1
40 #include <memory_resource>
48 #include <bits/functexcept.h>
49 #if _GLIBCXX_HAVE_XLOCALE_H
53 #if _GLIBCXX_HAVE_USELOCALE
54 // FIXME: This should be reimplemented so it doesn't use strtod and newlocale.
55 // That will avoid the need for any memory allocation, meaning that the
56 // non-conforming errc::not_enough_memory result cannot happen.
57 # define USE_STRTOD_FOR_FROM_CHARS 1
60 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
61 #ifndef __LONG_DOUBLE_IBM128__
62 #error "floating_from_chars.cc must be compiled with -mabi=ibmlongdouble"
64 // strtold for __ieee128
65 extern "C" __ieee128
__strtoieee128(const char*, char**);
66 #elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \
67 && defined(__GLIBC_PREREQ) && defined(USE_STRTOD_FOR_FROM_CHARS)
68 #define USE_STRTOF128_FOR_FROM_CHARS 1
69 extern "C" _Float128
__strtof128(const char*, char**)
71 #ifndef _GLIBCXX_HAVE_FLOAT128_MATH
72 __attribute__((__weak__
))
77 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
78 && __SIZE_WIDTH__ >= 32
79 # define USE_LIB_FAST_FLOAT 1
82 #if USE_LIB_FAST_FLOAT
83 # define FASTFLOAT_DEBUG_ASSERT __glibcxx_assert
86 # include "fast_float/fast_float.h"
91 // Wrappers around float for std::{,b}float16_t promoted to float.
92 struct floating_type_float16_t
97 struct floating_type_bfloat16_t
105 binary_format
<floating_type_float16_t
>::mantissa_explicit_bits()
110 binary_format
<floating_type_bfloat16_t
>::mantissa_explicit_bits()
113 // 10 bits of stored mantissa, pow(5,q) <= 0x4p+10 implies q <= 5
116 binary_format
<floating_type_float16_t
>::max_exponent_round_to_even()
119 // 7 bits of stored mantissa, pow(5,q) <= 0x4p+7 implies q <= 3
122 binary_format
<floating_type_bfloat16_t
>::max_exponent_round_to_even()
125 // 10 bits of stored mantissa, pow(5,-q) < 0x1p+64 / 0x1p+11 implies q >= -22
128 binary_format
<floating_type_float16_t
>::min_exponent_round_to_even()
131 // 7 bits of stored mantissa, pow(5,-q) < 0x1p+64 / 0x1p+8 implies q >= -24
134 binary_format
<floating_type_bfloat16_t
>::min_exponent_round_to_even()
139 binary_format
<floating_type_float16_t
>::minimum_exponent()
144 binary_format
<floating_type_bfloat16_t
>::minimum_exponent()
149 binary_format
<floating_type_float16_t
>::infinite_power()
154 binary_format
<floating_type_bfloat16_t
>::infinite_power()
159 binary_format
<floating_type_float16_t
>::sign_index()
164 binary_format
<floating_type_bfloat16_t
>::sign_index()
169 binary_format
<floating_type_float16_t
>::largest_power_of_ten()
174 binary_format
<floating_type_bfloat16_t
>::largest_power_of_ten()
179 binary_format
<floating_type_float16_t
>::smallest_power_of_ten()
184 binary_format
<floating_type_bfloat16_t
>::smallest_power_of_ten()
189 binary_format
<floating_type_float16_t
>::max_digits()
194 binary_format
<floating_type_bfloat16_t
>::max_digits()
197 // negative_digit_comp converts adjusted_mantissa to the (originally only)
198 // floating type and immediately back with slight tweaks (e.g. explicit
199 // leading bit instead of implicit for normals).
200 // Avoid going through the floating point type.
202 fastfloat_really_inline
void
203 to_float
<floating_type_float16_t
>(bool negative
, adjusted_mantissa am
,
204 floating_type_float16_t
&value
)
206 constexpr int mantissa_bits
207 = binary_format
<floating_type_float16_t
>::mantissa_explicit_bits();
208 value
.bits
= (am
.mantissa
209 | (uint16_t(am
.power2
) << mantissa_bits
)
210 | (negative
? 0x8000 : 0));
214 fastfloat_really_inline
void
215 to_float
<floating_type_bfloat16_t
>(bool negative
, adjusted_mantissa am
,
216 floating_type_bfloat16_t
&value
)
218 constexpr int mantissa_bits
219 = binary_format
<floating_type_bfloat16_t
>::mantissa_explicit_bits();
220 value
.bits
= (am
.mantissa
221 | (uint16_t(am
.power2
) << mantissa_bits
)
222 | (negative
? 0x8000 : 0));
226 fastfloat_really_inline adjusted_mantissa
227 to_extended
<floating_type_float16_t
>(floating_type_float16_t value
) noexcept
229 adjusted_mantissa am
;
230 constexpr int mantissa_bits
231 = binary_format
<floating_type_float16_t
>::mantissa_explicit_bits();
234 - binary_format
<floating_type_float16_t
>::minimum_exponent());
235 constexpr uint16_t exponent_mask
= 0x7C00;
236 constexpr uint16_t mantissa_mask
= 0x03FF;
237 constexpr uint16_t hidden_bit_mask
= 0x0400;
238 if ((value
.bits
& exponent_mask
) == 0) {
240 am
.power2
= 1 - bias
;
241 am
.mantissa
= value
.bits
& mantissa_mask
;
244 am
.power2
= int32_t((value
.bits
& exponent_mask
) >> mantissa_bits
);
246 am
.mantissa
= (value
.bits
& mantissa_mask
) | hidden_bit_mask
;
252 fastfloat_really_inline adjusted_mantissa
253 to_extended
<floating_type_bfloat16_t
>(floating_type_bfloat16_t value
) noexcept
255 adjusted_mantissa am
;
256 constexpr int mantissa_bits
257 = binary_format
<floating_type_bfloat16_t
>::mantissa_explicit_bits();
260 - binary_format
<floating_type_bfloat16_t
>::minimum_exponent());
261 constexpr uint16_t exponent_mask
= 0x7F80;
262 constexpr uint16_t mantissa_mask
= 0x007F;
263 constexpr uint16_t hidden_bit_mask
= 0x0080;
264 if ((value
.bits
& exponent_mask
) == 0) {
266 am
.power2
= 1 - bias
;
267 am
.mantissa
= value
.bits
& mantissa_mask
;
270 am
.power2
= int32_t((value
.bits
& exponent_mask
) >> mantissa_bits
);
272 am
.mantissa
= (value
.bits
& mantissa_mask
) | hidden_bit_mask
;
277 // Like fast_float.h from_chars_advanced, but for 16-bit float.
280 from_chars_16(const char* first
, const char* last
, T
&value
,
281 chars_format fmt
) noexcept
283 parse_options options
{fmt
};
285 from_chars_result answer
;
288 answer
.ec
= std::errc::invalid_argument
;
293 parsed_number_string pns
= parse_number_string(first
, last
, options
);
295 return detail::parse_infnan(first
, last
, *value
.x
);
297 answer
.ec
= std::errc();
298 answer
.ptr
= pns
.lastmatch
;
301 = compute_float
<binary_format
<T
>>(pns
.exponent
, pns
.mantissa
);
302 if (pns
.too_many_digits
&& am
.power2
>= 0)
304 if (am
!= compute_float
<binary_format
<T
>>(pns
.exponent
,
306 am
= compute_error
<binary_format
<T
>>(pns
.exponent
, pns
.mantissa
);
309 // If we called compute_float<binary_format<T>>(pns.exponent, pns.mantissa)
310 // and we have an invalid power (am.power2 < 0),
311 // then we need to go the long way around again. This is very uncommon.
313 am
= digit_comp
<T
>(pns
, am
);
315 if ((pns
.mantissa
!= 0 && am
.mantissa
== 0 && am
.power2
== 0)
316 || am
.power2
== binary_format
<T
>::infinite_power())
318 // In case of over/underflow, return result_out_of_range and don't
319 // modify value, as per [charconv.from.chars]/1. Note that LWG 3081 wants
320 // to modify value in this case too.
321 answer
.ec
= std::errc::result_out_of_range
;
325 // Transform the {,b}float16_t to float32_t before to_float.
326 if constexpr (std::is_same_v
<T
, floating_type_float16_t
>)
332 int n
= (std::numeric_limits
<unsigned int>::digits
333 - __builtin_clz (am
.mantissa
)) - 1;
334 am
.mantissa
&= ~(static_cast<decltype(am
.mantissa
)>(1) << n
);
335 am
.mantissa
<<= (binary_format
<float>::mantissa_explicit_bits()
337 am
.power2
= n
+ 0x67;
348 to_float(pns
.negative
, am
, *value
.x
);
356 namespace std
_GLIBCXX_VISIBILITY(default)
358 _GLIBCXX_BEGIN_NAMESPACE_VERSION
362 #if USE_STRTOD_FOR_FROM_CHARS
363 // A memory resource with a static buffer that can be used for small
364 // allocations. At most one allocation using the freestore can be done
365 // if the static buffer is insufficient. The callers below only require
366 // a single allocation, so there's no need for anything more complex.
367 struct buffer_resource
: pmr::memory_resource
369 ~buffer_resource() { if (m_ptr
) operator delete(m_ptr
, m_bytes
); }
372 do_allocate(size_t bytes
, size_t alignment
[[maybe_unused
]]) override
374 // Allocate from the buffer if it will fit.
375 if (m_bytes
< sizeof(m_buf
) && (m_bytes
+ bytes
) <= sizeof(m_buf
))
376 return m_buf
+ std::__exchange(m_bytes
, m_bytes
+ bytes
);
378 __glibcxx_assert(m_ptr
== nullptr);
380 m_ptr
= operator new(bytes
);
386 do_deallocate(void*, size_t, size_t) noexcept override
387 { /* like pmr::monotonic_buffer_resource, do nothing here */ }
390 do_is_equal(const pmr::memory_resource
& other
) const noexcept override
391 { return &other
== this; }
393 static constexpr int guaranteed_capacity() { return sizeof(m_buf
); }
398 void* m_ptr
= nullptr;
401 #if _GLIBCXX_USE_CXX11_ABI
402 using buffered_string
= std::pmr::string
;
404 using buffered_string
= std::string
;
407 inline bool valid_fmt(chars_format fmt
)
409 return fmt
!= chars_format
{}
410 && ((fmt
& chars_format::general
) == fmt
411 || (fmt
& chars_format::hex
) == fmt
);
414 constexpr char hex_digits
[] = "abcdefABCDEF0123456789";
415 constexpr auto dec_digits
= hex_digits
+ 12;
417 // Find initial portion of [first, last) containing a floating-point number.
418 // The string `digits` is either `dec_digits` or `hex_digits`
419 // and `exp` is "eE", "pP" or NULL.
421 find_end_of_float(const char* first
, const char* last
, const char* digits
,
424 while (first
< last
&& strchr(digits
, *first
) != nullptr)
426 if (first
< last
&& *first
== '.')
429 while (first
< last
&& strchr(digits
, *first
))
432 if (first
< last
&& exp
!= nullptr && (*first
== exp
[0] || *first
== exp
[1]))
435 if (first
< last
&& (*first
== '-' || *first
== '+'))
437 while (first
< last
&& strchr(dec_digits
, *first
) != nullptr)
443 // Determine the prefix of [first, last) that matches the pattern
444 // corresponding to `fmt`.
445 // Returns a NTBS containing the pattern, using `buf` to allocate
446 // additional storage if needed.
447 // Returns a nullptr if a valid pattern is not present.
449 pattern(const char* const first
, const char* last
,
450 chars_format
& fmt
, buffered_string
& buf
)
452 // fmt has the value of one of the enumerators of chars_format.
453 __glibcxx_assert(valid_fmt(fmt
));
457 if (first
== last
|| *first
== '+') [[unlikely
]]
460 const int neg
= (*first
== '-');
462 if (std::memchr("iInN", (unsigned char)first
[neg
], 4))
464 ptrdiff_t len
= last
- first
;
468 // possible infinity or NaN, let strtod decide
469 if (first
[neg
] == 'i' || first
[neg
] == 'I')
471 // Need at most 9 chars for "-INFINITY", ignore anything after it.
472 len
= std::min(len
, ptrdiff_t(neg
+ 8));
474 else if (len
> (neg
+ 3) && first
[neg
+ 3] == '(')
476 // Look for end of "NAN(n-char-sequence)"
477 if (void* p
= std::memchr(const_cast<char*>(first
)+4, ')', len
-4))
478 len
= static_cast<char*>(p
) + 1 - first
;
479 #ifndef __cpp_exceptions
480 if (len
> buffer_resource::guaranteed_capacity())
482 // The character sequence is too large for the buffer.
483 // Allocation failure could terminate the process,
484 // so just return an error via the fmt parameter.
485 fmt
= chars_format
{};
490 else // Only need 4 chars for "-NAN"
493 buf
.assign(first
, 0, len
);
494 // prevent make_result correcting for "0x"
495 fmt
= chars_format::general
;
502 // Assign [first,last) to a std::string to get a NTBS that can be used
503 // with strspn, strtod etc.
504 // If the string would be longer than the fixed buffer inside the
505 // buffer_resource type use find_end_of_float to try to reduce how
506 // much memory is needed, to reduce the chance of std::bad_alloc.
508 if (fmt
== chars_format::hex
)
512 if ((last
- first
+ 2) > buffer_resource::guaranteed_capacity())
514 last
= find_end_of_float(first
+ neg
, last
, digits
, "pP");
515 #ifndef __cpp_exceptions
516 if ((last
- first
+ 2) > buffer_resource::guaranteed_capacity())
518 // The character sequence is still too large for the buffer.
519 // Allocation failure could terminate the process,
520 // so just return an error via the fmt parameter.
521 fmt
= chars_format
{};
528 buf
.append(first
+ neg
, last
);
529 ptr
= buf
.data() + neg
+ 2;
535 if ((last
- first
) > buffer_resource::guaranteed_capacity())
537 last
= find_end_of_float(first
+ neg
, last
, digits
,
538 fmt
== chars_format::fixed
? nullptr : "eE");
539 #ifndef __cpp_exceptions
540 if ((last
- first
) > buffer_resource::guaranteed_capacity())
542 // The character sequence is still too large for the buffer.
543 // Allocation failure could terminate the process,
544 // so just return an error via the fmt parameter.
545 fmt
= chars_format
{};
550 buf
.assign(first
, last
);
551 ptr
= buf
.data() + neg
;
554 // "A non-empty sequence of decimal digits" or
555 // "A non-empty sequence of hexadecimal digits"
556 size_t len
= std::strspn(ptr
, digits
);
557 // "possibly containing a radix character,"
560 const size_t len2
= std::strspn(ptr
+ len
+ 1, digits
);
562 ptr
+= len
+ 1 + len2
;
566 else if (len
== 0) [[unlikely
]]
571 if (fmt
== chars_format::fixed
)
573 // Truncate the string to stop strtod parsing past this point.
576 else if (fmt
== chars_format::scientific
)
578 // Check for required exponent part which starts with 'e' or 'E'
579 if (*ptr
!= 'e' && *ptr
!= 'E')
581 // then an optional plus or minus sign
582 const int sign
= (ptr
[1] == '-' || ptr
[1] == '+');
583 // then a nonempty sequence of decimal digits
584 if (!std::memchr(dec_digits
, (unsigned char)ptr
[1+sign
], 10))
587 else if (fmt
== chars_format::general
)
589 if (*ptr
== 'x' || *ptr
== 'X')
596 // RAII type to change and restore the locale.
599 #if _GLIBCXX_HAVE_USELOCALE
600 // When we have uselocale we can change the current thread's locale.
605 : loc(::newlocale(LC_ALL_MASK
, "C", (locale_t
)0))
608 orig
= ::uselocale(loc
);
622 // Otherwise, we can't change the locale and so strtod can't be used.
623 auto_locale() = delete;
626 explicit operator bool() const noexcept
{ return ec
== errc
{}; }
630 auto_locale(const auto_locale
&) = delete;
631 auto_locale
& operator=(const auto_locale
&) = delete;
634 // RAII type to change and restore the floating-point environment.
635 struct auto_ferounding
637 #if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
638 const int rounding
= std::fegetround();
642 if (rounding
!= FE_TONEAREST
)
643 std::fesetround(FE_TONEAREST
);
648 if (rounding
!= FE_TONEAREST
)
649 std::fesetround(rounding
);
652 auto_ferounding() = default;
655 auto_ferounding(const auto_ferounding
&) = delete;
656 auto_ferounding
& operator=(const auto_ferounding
&) = delete;
659 // Convert the NTBS `str` to a floating-point value of type `T`.
660 // If `str` cannot be converted, `value` is unchanged and `0` is returned.
661 // Otherwise, let N be the number of characters consumed from `str`.
662 // On success `value` is set to the converted value and N is returned.
663 // If the converted value is out of range, `value` is unchanged and
667 from_chars_impl(const char* str
, T
& value
, errc
& ec
) noexcept
673 auto_ferounding rounding
;
674 const int save_errno
= errno
;
678 #if _GLIBCXX_USE_C99_STDLIB
679 if constexpr (is_same_v
<T
, float>)
680 tmpval
= std::strtof(str
, &endptr
);
681 else if constexpr (is_same_v
<T
, double>)
682 tmpval
= std::strtod(str
, &endptr
);
683 else if constexpr (is_same_v
<T
, long double>)
684 tmpval
= std::strtold(str
, &endptr
);
685 # ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
686 else if constexpr (is_same_v
<T
, __ieee128
>)
687 tmpval
= __strtoieee128(str
, &endptr
);
688 # elif defined(USE_STRTOF128_FOR_FROM_CHARS)
689 else if constexpr (is_same_v
<T
, _Float128
>)
691 #ifndef _GLIBCXX_HAVE_FLOAT128_MATH
692 if (&__strtof128
== nullptr)
693 tmpval
= _Float128(std::strtold(str
, &endptr
));
696 tmpval
= __strtof128(str
, &endptr
);
700 tmpval
= std::strtod(str
, &endptr
);
702 const int conv_errno
= std::__exchange(errno
, save_errno
);
704 const ptrdiff_t n
= endptr
- str
;
705 if (conv_errno
== ERANGE
) [[unlikely
]]
707 if (__builtin_isinf(tmpval
)) // overflow
708 ec
= errc::result_out_of_range
;
709 else if (tmpval
== 0) // underflow (LWG 3081 wants to set value = tmpval here)
710 ec
= errc::result_out_of_range
;
711 else // denormal value
730 inline from_chars_result
731 make_result(const char* str
, ptrdiff_t n
, chars_format fmt
, errc ec
) noexcept
733 from_chars_result result
= { str
, ec
};
736 if (fmt
== chars_format::hex
)
737 n
-= 2; // correct for the "0x" inserted into the pattern
740 else if (fmt
== chars_format
{}) [[unlikely
]]
742 // FIXME: the standard does not allow this result.
743 ec
= errc::not_enough_memory
;
748 #if ! _GLIBCXX_USE_CXX11_ABI
750 reserve_string(std::string
& s
) noexcept
754 s
.reserve(buffer_resource::guaranteed_capacity());
756 __catch (const std::bad_alloc
&)
766 from_chars_strtod(const char* first
, const char* last
, T
& value
,
767 chars_format fmt
) noexcept
769 errc ec
= errc::invalid_argument
;
770 #if _GLIBCXX_USE_CXX11_ABI
772 pmr::string
buf(&mr
);
775 if (!reserve_string(buf
))
776 return make_result(first
, 0, {}, ec
);
781 if (const char* pat
= pattern(first
, last
, fmt
, buf
)) [[likely
]]
782 len
= from_chars_impl(pat
, value
, ec
);
784 __catch (const std::bad_alloc
&)
786 fmt
= chars_format
{};
788 return make_result(first
, len
, fmt
, ec
);
790 #endif // USE_STRTOD_FOR_FROM_CHARS
792 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
793 // Return true iff [FIRST,LAST) begins with PREFIX, ignoring case.
794 // PREFIX is assumed to not contain any uppercase letters.
796 starts_with_ci(const char* first
, const char* last
, string_view prefix
)
798 __glibcxx_requires_valid_range(first
, last
);
800 // A lookup table that maps uppercase letters to lowercase and
801 // is otherwise the identity mapping.
802 static constexpr auto upper_to_lower_table
= [] {
803 constexpr unsigned char lower_letters
[27] = "abcdefghijklmnopqrstuvwxyz";
804 constexpr unsigned char upper_letters
[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
805 std::array
<unsigned char, (1u << __CHAR_BIT__
)> table
= {};
806 for (unsigned i
= 0; i
< table
.size(); ++i
)
808 for (unsigned i
= 0; i
< 26; ++i
)
809 table
[upper_letters
[i
]] = lower_letters
[i
];
813 if (last
- first
< static_cast<ptrdiff_t>(prefix
.length()))
816 for (const unsigned char pch
: prefix
)
818 // __glibcxx_assert(pch == upper_to_lower_table[pch]);
819 const unsigned char ch
= *first
;
820 if (ch
!= pch
&& upper_to_lower_table
[ch
] != pch
)
828 // An implementation of hexadecimal float parsing for binary32/64.
831 __floating_from_chars_hex(const char* first
, const char* last
, T
& value
)
833 using uint_t
= conditional_t
<is_same_v
<T
, float>, uint32_t,
834 conditional_t
<is_same_v
<T
, double>, uint64_t,
836 #if USE_LIB_FAST_FLOAT
837 constexpr int mantissa_bits
838 = fast_float::binary_format
<T
>::mantissa_explicit_bits();
839 constexpr int exponent_bits
840 = is_same_v
<T
, double> ? 11
841 : is_same_v
<T
, fast_float::floating_type_float16_t
> ? 5 : 8;
843 constexpr int mantissa_bits
= is_same_v
<T
, float> ? 23 : 52;
844 constexpr int exponent_bits
= is_same_v
<T
, float> ? 8 : 11;
846 constexpr int exponent_bias
= (1 << (exponent_bits
- 1)) - 1;
848 __glibcxx_requires_valid_range(first
, last
);
850 return {first
, errc::invalid_argument
};
852 // Consume the sign bit.
853 const char* const orig_first
= first
;
854 bool sign_bit
= false;
861 // Handle "inf", "infinity", "NaN" and variants thereof.
863 if (*first
== 'i' || *first
== 'I' || *first
== 'n' || *first
== 'N') [[unlikely
]]
865 if (starts_with_ci(first
, last
, "inf"sv
))
867 first
+= strlen("inf");
868 if (starts_with_ci(first
, last
, "inity"sv
))
869 first
+= strlen("inity");
871 if constexpr (is_same_v
<T
, float> || is_same_v
<T
, double>)
875 result
<<= exponent_bits
;
876 result
|= (1ull << exponent_bits
) - 1;
877 result
<<= mantissa_bits
;
878 memcpy(&value
, &result
, sizeof(result
));
883 uint32_t result
= 0x7F800000 | (sign_bit
? 0x80000000U
: 0);
884 memcpy(value
.x
, &result
, sizeof(result
));
887 return {first
, errc
{}};
889 else if (starts_with_ci(first
, last
, "nan"))
891 first
+= strlen("nan");
893 if (first
!= last
&& *first
== '(')
895 // Tentatively consume the '(' as we look for an optional
896 // n-char-sequence followed by a ')'.
897 const char* const fallback_first
= first
;
903 first
= fallback_first
;
914 || __detail::__from_chars_alnum_to_val(ch
) < 127)
918 first
= fallback_first
;
924 // We make the implementation-defined decision of ignoring the
925 // sign bit and the n-char-sequence when assembling the NaN.
926 if constexpr (is_same_v
<T
, float> || is_same_v
<T
, double>)
929 result
<<= exponent_bits
;
930 result
|= (1ull << exponent_bits
) - 1;
931 result
<<= mantissa_bits
;
932 result
|= (1ull << (mantissa_bits
- 1)) | 1;
933 memcpy(&value
, &result
, sizeof(result
));
938 uint32_t result
= 0x7FC00001;
939 memcpy(value
.x
, &result
, sizeof(result
));
942 return {first
, errc
{}};
946 // Consume all insignificant leading zeros in the whole part of the
948 bool seen_hexit
= false;
949 while (first
!= last
&& *first
== '0')
955 // Now consume the rest of the written mantissa, populating MANTISSA with
956 // the first MANTISSA_BITS+k significant bits of the written mantissa, where
957 // 1 <= k <= 4 is the bit width of the leading significant written hexit.
960 // After parsing "1.2f3", MANTISSA is 0x12f30000000000 (bit_width=52+1).
961 // After parsing ".0000f0e", MANTISSA is 0xf0e00000000000 (bit_width=52+4).
962 // After parsing ".1234567890abcd8", MANTISSA is 0x1234567890abcd (bit_width=52+1)
963 // and MIDPOINT_BIT is true (and NONZERO_TAIL is false).
965 int mantissa_idx
= mantissa_bits
; // The current bit index into MANTISSA
966 // into which we'll write the next hexit.
967 int exponent_adjustment
= 0; // How much we'd have to adjust the written
968 // exponent in order to represent the mantissa
969 // in scientific form h.hhhhhhhhhhhhh.
970 bool midpoint_bit
= false; // Whether the MANTISSA_BITS+k+1 significant
971 // bit is set in the written mantissa.
972 bool nonzero_tail
= false; // Whether some bit thereafter is set in the
974 bool seen_decimal_point
= false;
975 for (; first
!= last
; ++first
)
978 if (ch
== '.' && !seen_decimal_point
)
980 seen_decimal_point
= true;
984 int hexit
= __detail::__from_chars_alnum_to_val(ch
);
989 if (!seen_decimal_point
&& mantissa
!= 0)
990 exponent_adjustment
+= 4;
991 else if (seen_decimal_point
&& mantissa
== 0)
993 exponent_adjustment
-= 4;
998 if (mantissa_idx
>= 0)
999 mantissa
|= uint_t(hexit
) << mantissa_idx
;
1000 else if (mantissa_idx
>= -4)
1002 if constexpr (is_same_v
<T
, float>
1003 #if USE_LIB_FAST_FLOAT
1005 fast_float::floating_type_bfloat16_t
>
1009 __glibcxx_assert(mantissa_idx
== -1);
1010 mantissa
|= hexit
>> 1;
1011 midpoint_bit
= (hexit
& 0b0001) != 0;
1013 else if constexpr (is_same_v
<T
, double>)
1015 __glibcxx_assert(mantissa_idx
== -4);
1016 midpoint_bit
= (hexit
& 0b1000) != 0;
1017 nonzero_tail
= (hexit
& 0b0111) != 0;
1021 __glibcxx_assert(mantissa_idx
== -2);
1022 mantissa
|= hexit
>> 2;
1023 midpoint_bit
= (hexit
& 0b0010) != 0;
1024 nonzero_tail
= (hexit
& 0b0001) != 0;
1028 nonzero_tail
|= (hexit
!= 0x0);
1033 __glibcxx_assert(__bit_width(mantissa
) >= mantissa_bits
+ 1
1034 && __bit_width(mantissa
) <= mantissa_bits
+ 4);
1036 __glibcxx_assert(!midpoint_bit
&& !nonzero_tail
);
1039 // If we haven't seen any hexit at this point, the parse failed.
1040 return {orig_first
, errc::invalid_argument
};
1042 // Parse the written exponent.
1043 int written_exponent
= 0;
1044 if (first
!= last
&& (*first
== 'p' || *first
== 'P'))
1046 // Tentatively consume the 'p' and try to parse a decimal number.
1047 const char* const fallback_first
= first
;
1049 if (first
!= last
&& *first
== '+')
1051 from_chars_result fcr
= from_chars(first
, last
, written_exponent
, 10);
1052 if (fcr
.ptr
== first
)
1053 // The parse failed, so undo consuming the 'p' and carry on as if the
1054 // exponent was omitted (i.e. is 0).
1055 first
= fallback_first
;
1059 if (mantissa
!= 0 && fcr
.ec
== errc::result_out_of_range
)
1060 // Punt on very large exponents for now. FIXME
1061 return {first
, errc::result_out_of_range
};
1064 int biased_exponent
= written_exponent
+ exponent_bias
;
1065 if (exponent_adjustment
!= 0)
1066 // The mantissa wasn't written in scientific form. Adjust the exponent
1067 // so that we may assume scientific form.
1070 // For input "a.bcp5", EXPONENT_ADJUSTMENT would be 0 since this
1071 // written mantissa is already in scientific form.
1072 // For input "ab.cp5", EXPONENT_ADJUSTMENT would be 4 since the
1073 // scientific form is "a.bcp9".
1074 // For input 0.0abcp5", EXPONENT_ADJUSTMENT would be -8 since the
1075 // scientific form is "a.bcp-3".
1076 biased_exponent
+= exponent_adjustment
;
1078 // Shifts the mantissa to the right by AMOUNT while updating
1079 // BIASED_EXPONENT, MIDPOINT_BIT and NONZERO_TAIL accordingly.
1080 auto shift_mantissa
= [&] (int amount
) {
1081 __glibcxx_assert(amount
>= 0);
1082 if (amount
> mantissa_bits
+ 1)
1084 // Shifting the mantissa by an amount greater than its precision.
1085 nonzero_tail
|= midpoint_bit
;
1086 nonzero_tail
|= mantissa
!= 0;
1087 midpoint_bit
= false;
1089 biased_exponent
+= amount
;
1091 else if (amount
!= 0)
1093 nonzero_tail
|= midpoint_bit
;
1094 nonzero_tail
|= (mantissa
& ((1ull << (amount
- 1)) - 1)) != 0;
1095 midpoint_bit
= (mantissa
& (1ull << (amount
- 1))) != 0;
1096 mantissa
>>= amount
;
1097 biased_exponent
+= amount
;
1103 // If the leading hexit is not '1', shift MANTISSA to make it so.
1104 // This normalizes input like "4.08p0" into "1.02p2".
1105 const int leading_hexit
= mantissa
>> mantissa_bits
;
1106 const int leading_hexit_width
= __bit_width(leading_hexit
); // FIXME: optimize?
1107 __glibcxx_assert(leading_hexit_width
>= 1 && leading_hexit_width
<= 4);
1108 shift_mantissa(leading_hexit_width
- 1);
1109 // After this adjustment, we can assume the leading hexit is '1'.
1110 __glibcxx_assert((mantissa
>> mantissa_bits
) == 0x1);
1113 if (biased_exponent
<= 0)
1115 // This number is too small to be represented as a normal number, so
1116 // try for a subnormal number by shifting the mantissa sufficiently.
1117 // We need to shift by 1 more than -BIASED_EXPONENT because the leading
1118 // mantissa bit is omitted in the representation of a normal number but
1119 // not in a subnormal number.
1120 shift_mantissa(-biased_exponent
+ 1);
1121 __glibcxx_assert(!(mantissa
& (1ull << mantissa_bits
)));
1122 __glibcxx_assert(biased_exponent
== 1);
1123 biased_exponent
= 0;
1126 // Perform round-to-nearest, tie-to-even rounding according to
1127 // MIDPOINT_BIT and NONZERO_TAIL.
1128 if (midpoint_bit
&& (nonzero_tail
|| (mantissa
% 2) != 0))
1130 // Rounding away from zero.
1132 midpoint_bit
= false;
1133 nonzero_tail
= false;
1135 // Deal with a couple of corner cases after rounding.
1136 if (mantissa
== (1ull << mantissa_bits
))
1138 // We rounded the subnormal number 1.fffffffffffff...p-1023
1139 // up to the normal number 1p-1022.
1140 __glibcxx_assert(biased_exponent
== 0);
1143 else if (mantissa
& (1ull << (mantissa_bits
+ 1)))
1145 // We rounded the normal number 1.fffffffffffff8pN (with maximal
1146 // mantissa) up to to 1p(N+1).
1153 // Rounding toward zero.
1155 if (mantissa
== 0 && (midpoint_bit
|| nonzero_tail
))
1157 // A nonzero number that rounds to zero is unrepresentable.
1158 __glibcxx_assert(biased_exponent
== 0);
1159 return {first
, errc::result_out_of_range
};
1162 midpoint_bit
= false;
1163 nonzero_tail
= false;
1166 if (mantissa
!= 0 && biased_exponent
>= (1 << exponent_bits
) - 1)
1167 // The exponent of this number is too large to be representable.
1168 return {first
, errc::result_out_of_range
};
1173 // Assemble a (possibly signed) zero.
1175 result
|= 1ull << (exponent_bits
+ mantissa_bits
);
1179 // Assemble a nonzero normal or subnormal value.
1181 result
<<= exponent_bits
;
1182 result
|= biased_exponent
;
1183 result
<<= mantissa_bits
;
1184 result
|= mantissa
& ((1ull << mantissa_bits
) - 1);
1185 // The implicit leading mantissa bit is set iff the number is normal.
1186 __glibcxx_assert(((mantissa
& (1ull << mantissa_bits
)) != 0)
1187 == (biased_exponent
!= 0));
1189 if constexpr (is_same_v
<T
, float> || is_same_v
<T
, double>)
1190 memcpy(&value
, &result
, sizeof(result
));
1191 #if USE_LIB_FAST_FLOAT
1192 else if constexpr (is_same_v
<T
, fast_float::floating_type_bfloat16_t
>)
1194 uint32_t res
= uint32_t{result
} << 16;
1195 memcpy(value
.x
, &res
, sizeof(res
));
1199 // Otherwise float16_t which needs to be converted to float32_t.
1201 if ((result
& 0x7FFF) == 0)
1202 res
= uint32_t{result
} << 16; // +/-0.0f16
1203 else if ((result
& 0x7C00) == 0)
1205 unsigned n
= (std::numeric_limits
<unsigned int>::digits
1206 - __builtin_clz (result
& 0x3FF) - 1);
1207 res
= uint32_t{result
} & 0x3FF & ~(uint32_t{1} << n
);
1209 res
|= (((uint32_t{n
} + 0x67) << 23)
1210 | ((uint32_t{result
} & 0x8000) << 16));
1213 res
= (((uint32_t{result
} & 0x3FF) << 13)
1214 | ((((uint32_t{result
} >> 10) & 0x1F) + 0x70) << 23)
1215 | ((uint32_t{result
} & 0x8000) << 16));
1216 memcpy(value
.x
, &res
, sizeof(res
));
1220 return {first
, errc
{}};
1222 #endif // _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
1226 #if USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS
1229 from_chars(const char* first
, const char* last
, float& value
,
1230 chars_format fmt
) noexcept
1232 #if USE_LIB_FAST_FLOAT
1233 if (fmt
== chars_format::hex
)
1234 return __floating_from_chars_hex(first
, last
, value
);
1236 return fast_float::from_chars(first
, last
, value
, fmt
);
1238 return from_chars_strtod(first
, last
, value
, fmt
);
1243 from_chars(const char* first
, const char* last
, double& value
,
1244 chars_format fmt
) noexcept
1246 #if USE_LIB_FAST_FLOAT
1247 if (fmt
== chars_format::hex
)
1248 return __floating_from_chars_hex(first
, last
, value
);
1250 return fast_float::from_chars(first
, last
, value
, fmt
);
1252 return from_chars_strtod(first
, last
, value
, fmt
);
1257 from_chars(const char* first
, const char* last
, long double& value
,
1258 chars_format fmt
) noexcept
1260 #if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__ || !defined USE_STRTOD_FOR_FROM_CHARS
1261 // Either long double is the same as double, or we can't use strtold.
1262 // In the latter case, this might give an incorrect result (e.g. values
1263 // out of range of double give an error, even if they fit in long double).
1265 from_chars_result result
;
1266 if (fmt
== chars_format::hex
)
1267 result
= __floating_from_chars_hex(first
, last
, dbl_value
);
1269 result
= fast_float::from_chars(first
, last
, dbl_value
, fmt
);
1270 if (result
.ec
== errc
{})
1274 return from_chars_strtod(first
, last
, value
, fmt
);
1278 #if USE_LIB_FAST_FLOAT
1279 // Entrypoints for 16-bit floats.
1280 [[gnu::cold
]] from_chars_result
1281 __from_chars_float16_t(const char* first
, const char* last
, float& value
,
1282 chars_format fmt
) noexcept
1284 struct fast_float::floating_type_float16_t val
{ &value
, 0 };
1285 if (fmt
== chars_format::hex
)
1286 return __floating_from_chars_hex(first
, last
, val
);
1288 return fast_float::from_chars_16(first
, last
, val
, fmt
);
1291 [[gnu::cold
]] from_chars_result
1292 __from_chars_bfloat16_t(const char* first
, const char* last
, float& value
,
1293 chars_format fmt
) noexcept
1295 struct fast_float::floating_type_bfloat16_t val
{ &value
, 0 };
1296 if (fmt
== chars_format::hex
)
1297 return __floating_from_chars_hex(first
, last
, val
);
1299 return fast_float::from_chars_16(first
, last
, val
, fmt
);
1303 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
1304 // Make std::from_chars for 64-bit long double an alias for the overload
1306 extern "C" from_chars_result
1307 _ZSt10from_charsPKcS0_ReSt12chars_format(const char* first
, const char* last
,
1309 chars_format fmt
) noexcept
1310 __attribute__((alias ("_ZSt10from_charsPKcS0_RdSt12chars_format")));
1313 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1315 from_chars(const char* first
, const char* last
, __ieee128
& value
,
1316 chars_format fmt
) noexcept
1318 // fast_float doesn't support IEEE binary128 format, but we can use strtold.
1319 return from_chars_strtod(first
, last
, value
, fmt
);
1322 extern "C" from_chars_result
1323 _ZSt10from_charsPKcS0_RDF128_St12chars_format(const char* first
,
1326 chars_format fmt
) noexcept
1327 __attribute__((alias ("_ZSt10from_charsPKcS0_Ru9__ieee128St12chars_format")));
1328 #elif defined(USE_STRTOF128_FOR_FROM_CHARS)
1329 // Overload for _Float128 is not defined inline in <charconv>, define it here.
1331 from_chars(const char* first
, const char* last
, _Float128
& value
,
1332 chars_format fmt
) noexcept
1334 // fast_float doesn't support IEEE binary128 format, but we can use strtold.
1335 return from_chars_strtod(first
, last
, value
, fmt
);
1339 #endif // USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS
1341 _GLIBCXX_END_NAMESPACE_VERSION