]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/src/c++17/floating_from_chars.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / src / c++17 / floating_from_chars.cc
CommitLineData
932fbc86
JW
1// std::from_chars implementation for floating-point types -*- C++ -*-
2
83ffe9cd 3// Copyright (C) 2020-2023 Free Software Foundation, Inc.
932fbc86
JW
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25//
26// ISO C++ 14882:2017
27// 23.2.9 Primitive numeric input conversion [utility.from.chars]
28//
29
de77abee
FD
30// Prefer to use std::pmr::string if possible, which requires the cxx11 ABI.
31#define _GLIBCXX_USE_CXX11_ABI 1
32
93dd7f36 33#include <array>
932fbc86 34#include <charconv>
cc3bf340 35#include <bit>
932fbc86
JW
36#include <string>
37#include <memory_resource>
2251b4a5 38#include <cfenv>
490e2303 39#include <cfloat>
932fbc86
JW
40#include <cmath>
41#include <cstdlib>
42#include <cstring>
932fbc86
JW
43#include <locale.h>
44#include <bits/functexcept.h>
45#if _GLIBCXX_HAVE_XLOCALE_H
46# include <xlocale.h>
47#endif
48
416b6fc7
JW
49#if _GLIBCXX_HAVE_USELOCALE
50// FIXME: This should be reimplemented so it doesn't use strtod and newlocale.
51// That will avoid the need for any memory allocation, meaning that the
52// non-conforming errc::not_enough_memory result cannot happen.
53# define USE_STRTOD_FOR_FROM_CHARS 1
54#endif
55
7c1e7eed
JW
56#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
57#ifndef __LONG_DOUBLE_IBM128__
58#error "floating_from_chars.cc must be compiled with -mabi=ibmlongdouble"
59#endif
60// strtold for __ieee128
61extern "C" __ieee128 __strtoieee128(const char*, char**);
e5bcbcd0
JJ
62#elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \
63 && defined(__GLIBC_PREREQ)
64#define USE_STRTOF128_FOR_FROM_CHARS 1
65extern "C" _Float128 __strtof128(const char*, char**)
b457b779 66 __asm ("strtof128")
e5bcbcd0
JJ
67#ifndef _GLIBCXX_HAVE_FLOAT128_MATH
68 __attribute__((__weak__))
69#endif
b457b779 70 ;
7c1e7eed
JW
71#endif
72
a8db9b90
JW
73#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
74 && __SIZE_WIDTH__ >= 32
490e2303 75# define USE_LIB_FAST_FLOAT 1
416b6fc7 76# if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
5a4e2080 77// No need to use strtold.
416b6fc7
JW
78# undef USE_STRTOD_FOR_FROM_CHARS
79# endif
490e2303
PP
80#endif
81
82#if USE_LIB_FAST_FLOAT
83# define FASTFLOAT_DEBUG_ASSERT __glibcxx_assert
84namespace
85{
86# include "fast_float/fast_float.h"
81f98afa
JJ
87
88namespace fast_float
89{
90
91 // Wrappers around float for std::{,b}float16_t promoted to float.
92 struct floating_type_float16_t
93 {
94 float* x;
95 uint16_t bits;
96 };
97 struct floating_type_bfloat16_t
98 {
99 float* x;
100 uint16_t bits;
101 };
102
103 template<>
104 constexpr int
105 binary_format<floating_type_float16_t>::mantissa_explicit_bits()
106 { return 10; }
107
108 template<>
109 constexpr int
110 binary_format<floating_type_bfloat16_t>::mantissa_explicit_bits()
111 { return 7; }
112
113 // 10 bits of stored mantissa, pow(5,q) <= 0x4p+10 implies q <= 5
114 template<>
115 constexpr int
116 binary_format<floating_type_float16_t>::max_exponent_round_to_even()
117 { return 5; }
118
119 // 7 bits of stored mantissa, pow(5,q) <= 0x4p+7 implies q <= 3
120 template<>
121 constexpr int
122 binary_format<floating_type_bfloat16_t>::max_exponent_round_to_even()
123 { return 3; }
124
125 // 10 bits of stored mantissa, pow(5,-q) < 0x1p+64 / 0x1p+11 implies q >= -22
126 template<>
127 constexpr int
128 binary_format<floating_type_float16_t>::min_exponent_round_to_even()
129 { return -22; }
130
131 // 7 bits of stored mantissa, pow(5,-q) < 0x1p+64 / 0x1p+8 implies q >= -24
132 template<>
133 constexpr int
134 binary_format<floating_type_bfloat16_t>::min_exponent_round_to_even()
135 { return -24; }
136
137 template<>
138 constexpr int
139 binary_format<floating_type_float16_t>::minimum_exponent()
140 { return -15; }
141
142 template<>
143 constexpr int
144 binary_format<floating_type_bfloat16_t>::minimum_exponent()
145 { return -127; }
146
147 template<>
148 constexpr int
149 binary_format<floating_type_float16_t>::infinite_power()
150 { return 0x1F; }
151
152 template<>
153 constexpr int
154 binary_format<floating_type_bfloat16_t>::infinite_power()
155 { return 0xFF; }
156
157 template<>
158 constexpr int
159 binary_format<floating_type_float16_t>::sign_index()
160 { return 15; }
161
162 template<>
163 constexpr int
164 binary_format<floating_type_bfloat16_t>::sign_index()
165 { return 15; }
166
167 template<>
168 constexpr int
169 binary_format<floating_type_float16_t>::largest_power_of_ten()
170 { return 4; }
171
172 template<>
173 constexpr int
174 binary_format<floating_type_bfloat16_t>::largest_power_of_ten()
175 { return 38; }
176
177 template<>
178 constexpr int
179 binary_format<floating_type_float16_t>::smallest_power_of_ten()
180 { return -27; }
181
182 template<>
183 constexpr int
184 binary_format<floating_type_bfloat16_t>::smallest_power_of_ten()
185 { return -60; }
186
187 template<>
188 constexpr size_t
189 binary_format<floating_type_float16_t>::max_digits()
190 { return 22; }
191
192 template<>
193 constexpr size_t
194 binary_format<floating_type_bfloat16_t>::max_digits()
195 { return 98; }
196
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.
201 template<>
202 fastfloat_really_inline void
203 to_float<floating_type_float16_t>(bool negative, adjusted_mantissa am,
204 floating_type_float16_t &value)
205 {
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));
211 }
212
213 template<>
214 fastfloat_really_inline void
215 to_float<floating_type_bfloat16_t>(bool negative, adjusted_mantissa am,
216 floating_type_bfloat16_t &value)
217 {
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));
223 }
224
225 template <>
226 fastfloat_really_inline adjusted_mantissa
227 to_extended<floating_type_float16_t>(floating_type_float16_t value) noexcept
228 {
229 adjusted_mantissa am;
230 constexpr int mantissa_bits
231 = binary_format<floating_type_float16_t>::mantissa_explicit_bits();
232 int32_t bias
233 = (mantissa_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) {
239 // denormal
240 am.power2 = 1 - bias;
241 am.mantissa = value.bits & mantissa_mask;
242 } else {
243 // normal
244 am.power2 = int32_t((value.bits & exponent_mask) >> mantissa_bits);
245 am.power2 -= bias;
246 am.mantissa = (value.bits & mantissa_mask) | hidden_bit_mask;
247 }
248 return am;
249 }
250
251 template <>
252 fastfloat_really_inline adjusted_mantissa
253 to_extended<floating_type_bfloat16_t>(floating_type_bfloat16_t value) noexcept
254 {
255 adjusted_mantissa am;
256 constexpr int mantissa_bits
257 = binary_format<floating_type_bfloat16_t>::mantissa_explicit_bits();
258 int32_t bias
259 = (mantissa_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) {
265 // denormal
266 am.power2 = 1 - bias;
267 am.mantissa = value.bits & mantissa_mask;
268 } else {
269 // normal
270 am.power2 = int32_t((value.bits & exponent_mask) >> mantissa_bits);
271 am.power2 -= bias;
272 am.mantissa = (value.bits & mantissa_mask) | hidden_bit_mask;
273 }
274 return am;
275 }
276
277 // Like fast_float.h from_chars_advanced, but for 16-bit float.
278 template<typename T>
279 from_chars_result
280 from_chars_16(const char* first, const char* last, T &value,
281 chars_format fmt) noexcept
282 {
283 parse_options options{fmt};
284
285 from_chars_result answer;
286 if (first == last)
287 {
288 answer.ec = std::errc::invalid_argument;
289 answer.ptr = first;
290 return answer;
291 }
292
293 parsed_number_string pns = parse_number_string(first, last, options);
294 if (!pns.valid)
295 return detail::parse_infnan(first, last, *value.x);
296
297 answer.ec = std::errc();
298 answer.ptr = pns.lastmatch;
299
300 adjusted_mantissa am
301 = compute_float<binary_format<T>>(pns.exponent, pns.mantissa);
302 if (pns.too_many_digits && am.power2 >= 0)
303 {
304 if (am != compute_float<binary_format<T>>(pns.exponent,
305 pns.mantissa + 1))
306 am = compute_error<binary_format<T>>(pns.exponent, pns.mantissa);
307 }
308
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.
312 if (am.power2 < 0)
313 am = digit_comp<T>(pns, am);
314
315 if ((pns.mantissa != 0 && am.mantissa == 0 && am.power2 == 0)
316 || am.power2 == binary_format<T>::infinite_power())
317 {
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;
322 return answer;
323 }
324
325 // Transform the {,b}float16_t to float32_t before to_float.
326 if constexpr (std::is_same_v<T, floating_type_float16_t>)
327 {
328 if (am.power2 == 0)
329 {
330 if (am.mantissa)
331 {
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()
336 - n);
337 am.power2 = n + 0x67;
338 }
339 }
340 else
341 {
342 am.mantissa <<= 13;
343 am.power2 += 0x70;
344 }
345 }
346 else
347 am.mantissa <<= 16;
348 to_float(pns.negative, am, *value.x);
349 return answer;
350 }
351} // fast_float
352
490e2303
PP
353} // anon namespace
354#endif
355
932fbc86
JW
356namespace std _GLIBCXX_VISIBILITY(default)
357{
358_GLIBCXX_BEGIN_NAMESPACE_VERSION
359
360namespace
361{
416b6fc7 362#if USE_STRTOD_FOR_FROM_CHARS
932fbc86
JW
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
368 {
369 ~buffer_resource() { if (m_ptr) operator delete(m_ptr, m_bytes); }
370
371 void*
372 do_allocate(size_t bytes, size_t alignment [[maybe_unused]]) override
373 {
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);
377
378 __glibcxx_assert(m_ptr == nullptr);
932fbc86
JW
379
380 m_ptr = operator new(bytes);
381 m_bytes = bytes;
382 return m_ptr;
383 }
384
385 void
386 do_deallocate(void*, size_t, size_t) noexcept override
387 { /* like pmr::monotonic_buffer_resource, do nothing here */ }
388
389 bool
390 do_is_equal(const pmr::memory_resource& other) const noexcept override
391 { return &other == this; }
392
393 static constexpr int guaranteed_capacity() { return sizeof(m_buf); }
394
395 private:
396 char m_buf[512];
397 size_t m_bytes = 0;
398 void* m_ptr = nullptr;
399 };
400
de77abee
FD
401#if _GLIBCXX_USE_CXX11_ABI
402 using buffered_string = std::pmr::string;
403#else
404 using buffered_string = std::string;
405#endif
406
932fbc86
JW
407 inline bool valid_fmt(chars_format fmt)
408 {
409 return fmt != chars_format{}
410 && ((fmt & chars_format::general) == fmt
411 || (fmt & chars_format::hex) == fmt);
412 }
413
414 constexpr char hex_digits[] = "abcdefABCDEF0123456789";
415 constexpr auto dec_digits = hex_digits + 12;
416
417 // Find initial portion of [first, last) containing a floating-point number.
418 // The string `digits` is either `dec_digits` or `hex_digits`
86d821dd 419 // and `exp` is "eE", "pP" or NULL.
932fbc86
JW
420 const char*
421 find_end_of_float(const char* first, const char* last, const char* digits,
86d821dd 422 const char *exp)
932fbc86
JW
423 {
424 while (first < last && strchr(digits, *first) != nullptr)
425 ++first;
426 if (first < last && *first == '.')
427 {
428 ++first;
429 while (first < last && strchr(digits, *first))
430 ++first;
431 }
86d821dd 432 if (first < last && exp != nullptr && (*first == exp[0] || *first == exp[1]))
932fbc86
JW
433 {
434 ++first;
435 if (first < last && (*first == '-' || *first == '+'))
436 ++first;
437 while (first < last && strchr(dec_digits, *first) != nullptr)
438 ++first;
439 }
440 return first;
441 }
442
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.
448 const char*
449 pattern(const char* const first, const char* last,
de77abee 450 chars_format& fmt, buffered_string& buf)
932fbc86
JW
451 {
452 // fmt has the value of one of the enumerators of chars_format.
453 __glibcxx_assert(valid_fmt(fmt));
454
455 string_view res;
456
457 if (first == last || *first == '+') [[unlikely]]
458 return nullptr;
459
460 const int neg = (*first == '-');
461
462 if (std::memchr("iInN", (unsigned char)first[neg], 4))
463 {
464 ptrdiff_t len = last - first;
465 if (len < (3 + neg))
466 return nullptr;
467
468 // possible infinity or NaN, let strtod decide
469 if (first[neg] == 'i' || first[neg] == 'I')
470 {
471 // Need at most 9 chars for "-INFINITY", ignore anything after it.
472 len = std::min(len, ptrdiff_t(neg + 8));
473 }
474 else if (len > (neg + 3) && first[neg + 3] == '(')
475 {
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())
481 {
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{};
486 return nullptr;
487 }
488#endif
489 }
490 else // Only need 4 chars for "-NAN"
491 len = neg + 3;
492
493 buf.assign(first, 0, len);
494 // prevent make_result correcting for "0x"
495 fmt = chars_format::general;
496 return buf.c_str();
497 }
498
499 const char* digits;
500 char* ptr;
501
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.
507
508 if (fmt == chars_format::hex)
509 {
510 digits = hex_digits;
511
512 if ((last - first + 2) > buffer_resource::guaranteed_capacity())
513 {
86d821dd 514 last = find_end_of_float(first + neg, last, digits, "pP");
932fbc86
JW
515#ifndef __cpp_exceptions
516 if ((last - first + 2) > buffer_resource::guaranteed_capacity())
517 {
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{};
522 return nullptr;
523 }
524#endif
525 }
526
527 buf = "-0x" + !neg;
528 buf.append(first + neg, last);
529 ptr = buf.data() + neg + 2;
530 }
531 else
532 {
533 digits = dec_digits;
534
535 if ((last - first) > buffer_resource::guaranteed_capacity())
536 {
537 last = find_end_of_float(first + neg, last, digits,
86d821dd 538 fmt == chars_format::fixed ? nullptr : "eE");
932fbc86
JW
539#ifndef __cpp_exceptions
540 if ((last - first) > buffer_resource::guaranteed_capacity())
541 {
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{};
546 return nullptr;
547 }
548#endif
549 }
550 buf.assign(first, last);
551 ptr = buf.data() + neg;
552 }
553
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,"
558 if (ptr[len] == '.')
559 {
560 const size_t len2 = std::strspn(ptr + len + 1, digits);
561 if (len + len2)
562 ptr += len + 1 + len2;
563 else
564 return nullptr;
565 }
566 else if (len == 0) [[unlikely]]
567 return nullptr;
568 else
569 ptr += len;
570
571 if (fmt == chars_format::fixed)
572 {
573 // Truncate the string to stop strtod parsing past this point.
574 *ptr = '\0';
575 }
576 else if (fmt == chars_format::scientific)
577 {
578 // Check for required exponent part which starts with 'e' or 'E'
579 if (*ptr != 'e' && *ptr != 'E')
580 return nullptr;
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))
585 return nullptr;
586 }
587 else if (fmt == chars_format::general)
588 {
589 if (*ptr == 'x' || *ptr == 'X')
590 *ptr = '\0';
591 }
592
593 return buf.c_str();
594 }
595
596 // Convert the NTBS `str` to a floating-point value of type `T`.
597 // If `str` cannot be converted, `value` is unchanged and `0` is returned.
598 // Otherwise, let N be the number of characters consumed from `str`.
599 // On success `value` is set to the converted value and N is returned.
600 // If the converted value is out of range, `value` is unchanged and
601 // -N is returned.
602 template<typename T>
603 ptrdiff_t
604 from_chars_impl(const char* str, T& value, errc& ec) noexcept
605 {
4143efc1 606 if (locale_t loc = ::newlocale(LC_ALL_MASK, "C", (locale_t)0)) [[likely]]
932fbc86
JW
607 {
608 locale_t orig = ::uselocale(loc);
609
266d7464 610#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
2251b4a5
JW
611 const int rounding = std::fegetround();
612 if (rounding != FE_TONEAREST)
613 std::fesetround(FE_TONEAREST);
614#endif
615
932fbc86
JW
616 const int save_errno = errno;
617 errno = 0;
618 char* endptr;
619 T tmpval;
e513e9aa 620#if _GLIBCXX_USE_C99_STDLIB
932fbc86
JW
621 if constexpr (is_same_v<T, float>)
622 tmpval = std::strtof(str, &endptr);
e513e9aa 623 else if constexpr (is_same_v<T, double>)
932fbc86
JW
624 tmpval = std::strtod(str, &endptr);
625 else if constexpr (is_same_v<T, long double>)
626 tmpval = std::strtold(str, &endptr);
7c1e7eed
JW
627# ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
628 else if constexpr (is_same_v<T, __ieee128>)
629 tmpval = __strtoieee128(str, &endptr);
e5bcbcd0
JJ
630# elif defined(USE_STRTOF128_FOR_FROM_CHARS)
631 else if constexpr (is_same_v<T, _Float128>)
632 {
633#ifndef _GLIBCXX_HAVE_FLOAT128_MATH
634 if (&__strtof128 == nullptr)
8d032694 635 tmpval = _Float128(std::strtold(str, &endptr));
e5bcbcd0
JJ
636 else
637#endif
638 tmpval = __strtof128(str, &endptr);
639 }
7c1e7eed 640# endif
e513e9aa
JW
641#else
642 tmpval = std::strtod(str, &endptr);
643#endif
932fbc86
JW
644 const int conv_errno = std::__exchange(errno, save_errno);
645
266d7464 646#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
2251b4a5
JW
647 if (rounding != FE_TONEAREST)
648 std::fesetround(rounding);
649#endif
650
932fbc86
JW
651 ::uselocale(orig);
652 ::freelocale(loc);
653
654 const ptrdiff_t n = endptr - str;
655 if (conv_errno == ERANGE) [[unlikely]]
656 {
7c1e7eed 657 if (__builtin_isinf(tmpval)) // overflow
932fbc86 658 ec = errc::result_out_of_range;
6d9dbdf5 659 else if (tmpval == 0) // underflow (LWG 3081 wants to set value = tmpval here)
932fbc86 660 ec = errc::result_out_of_range;
6d9dbdf5
PP
661 else // denormal value
662 {
663 value = tmpval;
664 ec = errc();
665 }
932fbc86
JW
666 }
667 else if (n)
668 {
669 value = tmpval;
670 ec = errc();
671 }
672 return n;
673 }
674 else if (errno == ENOMEM)
675 ec = errc::not_enough_memory;
676
677 return 0;
678 }
679
680 inline from_chars_result
681 make_result(const char* str, ptrdiff_t n, chars_format fmt, errc ec) noexcept
682 {
683 from_chars_result result = { str, ec };
684 if (n != 0)
685 {
686 if (fmt == chars_format::hex)
687 n -= 2; // correct for the "0x" inserted into the pattern
688 result.ptr += n;
689 }
690 else if (fmt == chars_format{}) [[unlikely]]
691 {
692 // FIXME: the standard does not allow this result.
693 ec = errc::not_enough_memory;
694 }
695 return result;
696 }
697
de77abee
FD
698#if ! _GLIBCXX_USE_CXX11_ABI
699 inline bool
700 reserve_string(std::string& s) noexcept
701 {
702 __try
703 {
704 s.reserve(buffer_resource::guaranteed_capacity());
705 }
706 __catch (const std::bad_alloc&)
707 {
708 return false;
709 }
710 return true;
711 }
712#endif
5a4e2080
JW
713
714 template<typename T>
715 from_chars_result
716 from_chars_strtod(const char* first, const char* last, T& value,
717 chars_format fmt) noexcept
718 {
719 errc ec = errc::invalid_argument;
720#if _GLIBCXX_USE_CXX11_ABI
721 buffer_resource mr;
722 pmr::string buf(&mr);
723#else
724 string buf;
725 if (!reserve_string(buf))
726 return make_result(first, 0, {}, ec);
727#endif
728 size_t len = 0;
729 __try
730 {
731 if (const char* pat = pattern(first, last, fmt, buf)) [[likely]]
732 len = from_chars_impl(pat, value, ec);
733 }
734 __catch (const std::bad_alloc&)
735 {
736 fmt = chars_format{};
737 }
738 return make_result(first, len, fmt, ec);
739 }
416b6fc7 740#endif // USE_STRTOD_FOR_FROM_CHARS
de77abee 741
cc3bf340 742#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
cc3bf340 743 // Return true iff [FIRST,LAST) begins with PREFIX, ignoring case.
93dd7f36 744 // PREFIX is assumed to not contain any uppercase letters.
cc3bf340
PP
745 bool
746 starts_with_ci(const char* first, const char* last, string_view prefix)
747 {
748 __glibcxx_requires_valid_range(first, last);
749
93dd7f36
PP
750 // A lookup table that maps uppercase letters to lowercase and
751 // is otherwise the identity mapping.
752 static constexpr auto upper_to_lower_table = [] {
753 constexpr unsigned char lower_letters[27] = "abcdefghijklmnopqrstuvwxyz";
754 constexpr unsigned char upper_letters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
755 std::array<unsigned char, (1u << __CHAR_BIT__)> table = {};
756 for (unsigned i = 0; i < table.size(); ++i)
757 table[i] = i;
758 for (unsigned i = 0; i < 26; ++i)
759 table[upper_letters[i]] = lower_letters[i];
760 return table;
761 }();
762
763 if (last - first < static_cast<ptrdiff_t>(prefix.length()))
764 return false;
765
766 for (const unsigned char pch : prefix)
cc3bf340 767 {
93dd7f36
PP
768 // __glibcxx_assert(pch == upper_to_lower_table[pch]);
769 const unsigned char ch = *first;
770 if (ch != pch && upper_to_lower_table[ch] != pch)
cc3bf340
PP
771 return false;
772 ++first;
773 }
774
775 return true;
776 }
777
778 // An implementation of hexadecimal float parsing for binary32/64.
779 template<typename T>
780 from_chars_result
781 __floating_from_chars_hex(const char* first, const char* last, T& value)
782 {
81f98afa
JJ
783 using uint_t = conditional_t<is_same_v<T, float>, uint32_t,
784 conditional_t<is_same_v<T, double>, uint64_t,
785 uint16_t>>;
e6a32c12 786#if USE_LIB_FAST_FLOAT
81f98afa
JJ
787 constexpr int mantissa_bits
788 = fast_float::binary_format<T>::mantissa_explicit_bits();
789 constexpr int exponent_bits
790 = is_same_v<T, double> ? 11
791 : is_same_v<T, fast_float::floating_type_float16_t> ? 5 : 8;
e6a32c12
JJ
792#else
793 constexpr int mantissa_bits = is_same_v<T, float> ? 23 : 52;
794 constexpr int exponent_bits = is_same_v<T, float> ? 8 : 11;
795#endif
cc3bf340
PP
796 constexpr int exponent_bias = (1 << (exponent_bits - 1)) - 1;
797
798 __glibcxx_requires_valid_range(first, last);
799 if (first == last)
800 return {first, errc::invalid_argument};
801
802 // Consume the sign bit.
803 const char* const orig_first = first;
804 bool sign_bit = false;
805 if (*first == '-')
806 {
807 sign_bit = true;
808 ++first;
809 }
810
811 // Handle "inf", "infinity", "NaN" and variants thereof.
812 if (first != last)
813 if (*first == 'i' || *first == 'I' || *first == 'n' || *first == 'N') [[unlikely]]
814 {
815 if (starts_with_ci(first, last, "inf"sv))
816 {
817 first += strlen("inf");
818 if (starts_with_ci(first, last, "inity"sv))
819 first += strlen("inity");
820
81f98afa
JJ
821 if constexpr (is_same_v<T, float> || is_same_v<T, double>)
822 {
823 uint_t result = 0;
824 result |= sign_bit;
825 result <<= exponent_bits;
826 result |= (1ull << exponent_bits) - 1;
827 result <<= mantissa_bits;
828 memcpy(&value, &result, sizeof(result));
829 }
830 else
831 {
832 // float +/-Inf.
833 uint32_t result = 0x7F800000 | (sign_bit ? 0x80000000U : 0);
834 memcpy(value.x, &result, sizeof(result));
835 }
cc3bf340
PP
836
837 return {first, errc{}};
838 }
839 else if (starts_with_ci(first, last, "nan"))
840 {
841 first += strlen("nan");
842
843 if (first != last && *first == '(')
844 {
845 // Tentatively consume the '(' as we look for an optional
846 // n-char-sequence followed by a ')'.
847 const char* const fallback_first = first;
848 for (;;)
849 {
850 ++first;
851 if (first == last)
852 {
853 first = fallback_first;
854 break;
855 }
856
857 char ch = *first;
858 if (ch == ')')
859 {
860 ++first;
861 break;
862 }
93dd7f36
PP
863 else if (ch == '_'
864 || __detail::__from_chars_alnum_to_val(ch) < 127)
cc3bf340
PP
865 continue;
866 else
867 {
868 first = fallback_first;
869 break;
870 }
871 }
872 }
873
874 // We make the implementation-defined decision of ignoring the
875 // sign bit and the n-char-sequence when assembling the NaN.
81f98afa
JJ
876 if constexpr (is_same_v<T, float> || is_same_v<T, double>)
877 {
878 uint_t result = 0;
879 result <<= exponent_bits;
880 result |= (1ull << exponent_bits) - 1;
881 result <<= mantissa_bits;
882 result |= (1ull << (mantissa_bits - 1)) | 1;
883 memcpy(&value, &result, sizeof(result));
884 }
885 else
886 {
887 // float qNaN.
888 uint32_t result = 0x7FC00001;
889 memcpy(value.x, &result, sizeof(result));
890 }
cc3bf340
PP
891
892 return {first, errc{}};
893 }
894 }
895
896 // Consume all insignificant leading zeros in the whole part of the
897 // mantissa.
898 bool seen_hexit = false;
899 while (first != last && *first == '0')
900 {
901 seen_hexit = true;
902 ++first;
903 }
904
905 // Now consume the rest of the written mantissa, populating MANTISSA with
906 // the first MANTISSA_BITS+k significant bits of the written mantissa, where
907 // 1 <= k <= 4 is the bit width of the leading significant written hexit.
908 //
909 // Examples:
910 // After parsing "1.2f3", MANTISSA is 0x12f30000000000 (bit_width=52+1).
911 // After parsing ".0000f0e", MANTISSA is 0xf0e00000000000 (bit_width=52+4).
912 // After parsing ".1234567890abcd8", MANTISSA is 0x1234567890abcd (bit_width=52+1)
913 // and MIDPOINT_BIT is true (and NONZERO_TAIL is false).
914 uint_t mantissa = 0;
915 int mantissa_idx = mantissa_bits; // The current bit index into MANTISSA
916 // into which we'll write the next hexit.
917 int exponent_adjustment = 0; // How much we'd have to adjust the written
918 // exponent in order to represent the mantissa
919 // in scientific form h.hhhhhhhhhhhhh.
920 bool midpoint_bit = false; // Whether the MANTISSA_BITS+k+1 significant
921 // bit is set in the written mantissa.
922 bool nonzero_tail = false; // Whether some bit thereafter is set in the
923 // written mantissa.
924 bool seen_decimal_point = false;
925 for (; first != last; ++first)
926 {
927 char ch = *first;
928 if (ch == '.' && !seen_decimal_point)
929 {
930 seen_decimal_point = true;
931 continue;
932 }
933
93dd7f36 934 int hexit = __detail::__from_chars_alnum_to_val(ch);
a54137c8 935 if (hexit >= 16)
cc3bf340
PP
936 break;
937 seen_hexit = true;
938
939 if (!seen_decimal_point && mantissa != 0)
940 exponent_adjustment += 4;
941 else if (seen_decimal_point && mantissa == 0)
942 {
943 exponent_adjustment -= 4;
944 if (hexit == 0x0)
945 continue;
946 }
947
948 if (mantissa_idx >= 0)
949 mantissa |= uint_t(hexit) << mantissa_idx;
950 else if (mantissa_idx >= -4)
951 {
81f98afa 952 if constexpr (is_same_v<T, float>
e6a32c12 953#if USE_LIB_FAST_FLOAT
81f98afa 954 || is_same_v<T,
e6a32c12
JJ
955 fast_float::floating_type_bfloat16_t>
956#endif
957 )
cc3bf340
PP
958 {
959 __glibcxx_assert(mantissa_idx == -1);
960 mantissa |= hexit >> 1;
961 midpoint_bit = (hexit & 0b0001) != 0;
962 }
81f98afa 963 else if constexpr (is_same_v<T, double>)
cc3bf340
PP
964 {
965 __glibcxx_assert(mantissa_idx == -4);
966 midpoint_bit = (hexit & 0b1000) != 0;
967 nonzero_tail = (hexit & 0b0111) != 0;
968 }
81f98afa
JJ
969 else
970 {
971 __glibcxx_assert(mantissa_idx == -2);
972 mantissa |= hexit >> 2;
973 midpoint_bit = (hexit & 0b0010) != 0;
974 nonzero_tail = (hexit & 0b0001) != 0;
975 }
cc3bf340
PP
976 }
977 else
978 nonzero_tail |= (hexit != 0x0);
979
980 mantissa_idx -= 4;
981 }
982 if (mantissa != 0)
983 __glibcxx_assert(__bit_width(mantissa) >= mantissa_bits + 1
984 && __bit_width(mantissa) <= mantissa_bits + 4);
985 else
986 __glibcxx_assert(!midpoint_bit && !nonzero_tail);
987
988 if (!seen_hexit)
989 // If we haven't seen any hexit at this point, the parse failed.
990 return {orig_first, errc::invalid_argument};
991
992 // Parse the written exponent.
993 int written_exponent = 0;
576f975c 994 if (first != last && (*first == 'p' || *first == 'P'))
cc3bf340
PP
995 {
996 // Tentatively consume the 'p' and try to parse a decimal number.
997 const char* const fallback_first = first;
998 ++first;
999 if (first != last && *first == '+')
1000 ++first;
1001 from_chars_result fcr = from_chars(first, last, written_exponent, 10);
1002 if (fcr.ptr == first)
1003 // The parse failed, so undo consuming the 'p' and carry on as if the
1004 // exponent was omitted (i.e. is 0).
1005 first = fallback_first;
1006 else
1007 {
1008 first = fcr.ptr;
1009 if (mantissa != 0 && fcr.ec == errc::result_out_of_range)
1010 // Punt on very large exponents for now. FIXME
1011 return {first, errc::result_out_of_range};
1012 }
1013 }
1014 int biased_exponent = written_exponent + exponent_bias;
1015 if (exponent_adjustment != 0)
1016 // The mantissa wasn't written in scientific form. Adjust the exponent
1017 // so that we may assume scientific form.
1018 //
1019 // Examples;
1020 // For input "a.bcp5", EXPONENT_ADJUSTMENT would be 0 since this
1021 // written mantissa is already in scientific form.
1022 // For input "ab.cp5", EXPONENT_ADJUSTMENT would be 4 since the
1023 // scientific form is "a.bcp9".
1024 // For input 0.0abcp5", EXPONENT_ADJUSTMENT would be -8 since the
1025 // scientific form is "a.bcp-3".
1026 biased_exponent += exponent_adjustment;
1027
1028 // Shifts the mantissa to the right by AMOUNT while updating
1029 // BIASED_EXPONENT, MIDPOINT_BIT and NONZERO_TAIL accordingly.
1030 auto shift_mantissa = [&] (int amount) {
1031 __glibcxx_assert(amount >= 0);
1032 if (amount > mantissa_bits + 1)
1033 {
1034 // Shifting the mantissa by an amount greater than its precision.
1035 nonzero_tail |= midpoint_bit;
1036 nonzero_tail |= mantissa != 0;
1037 midpoint_bit = false;
1038 mantissa = 0;
1039 biased_exponent += amount;
1040 }
1041 else if (amount != 0)
1042 {
1043 nonzero_tail |= midpoint_bit;
1044 nonzero_tail |= (mantissa & ((1ull << (amount - 1)) - 1)) != 0;
1045 midpoint_bit = (mantissa & (1ull << (amount - 1))) != 0;
1046 mantissa >>= amount;
1047 biased_exponent += amount;
1048 }
1049 };
1050
1051 if (mantissa != 0)
1052 {
1053 // If the leading hexit is not '1', shift MANTISSA to make it so.
1054 // This normalizes input like "4.08p0" into "1.02p2".
1055 const int leading_hexit = mantissa >> mantissa_bits;
1056 const int leading_hexit_width = __bit_width(leading_hexit); // FIXME: optimize?
1057 __glibcxx_assert(leading_hexit_width >= 1 && leading_hexit_width <= 4);
1058 shift_mantissa(leading_hexit_width - 1);
1059 // After this adjustment, we can assume the leading hexit is '1'.
1060 __glibcxx_assert((mantissa >> mantissa_bits) == 0x1);
1061 }
1062
1063 if (biased_exponent <= 0)
1064 {
1065 // This number is too small to be represented as a normal number, so
1066 // try for a subnormal number by shifting the mantissa sufficiently.
1067 // We need to shift by 1 more than -BIASED_EXPONENT because the leading
1068 // mantissa bit is omitted in the representation of a normal number but
1069 // not in a subnormal number.
1070 shift_mantissa(-biased_exponent + 1);
1071 __glibcxx_assert(!(mantissa & (1ull << mantissa_bits)));
1072 __glibcxx_assert(biased_exponent == 1);
1073 biased_exponent = 0;
1074 }
1075
1076 // Perform round-to-nearest, tie-to-even rounding according to
1077 // MIDPOINT_BIT and NONZERO_TAIL.
1078 if (midpoint_bit && (nonzero_tail || (mantissa % 2) != 0))
1079 {
1080 // Rounding away from zero.
1081 ++mantissa;
1082 midpoint_bit = false;
1083 nonzero_tail = false;
1084
1085 // Deal with a couple of corner cases after rounding.
1086 if (mantissa == (1ull << mantissa_bits))
1087 {
1088 // We rounded the subnormal number 1.fffffffffffff...p-1023
1089 // up to the normal number 1p-1022.
1090 __glibcxx_assert(biased_exponent == 0);
1091 ++biased_exponent;
1092 }
1093 else if (mantissa & (1ull << (mantissa_bits + 1)))
1094 {
1095 // We rounded the normal number 1.fffffffffffff8pN (with maximal
1096 // mantissa) up to to 1p(N+1).
1097 mantissa >>= 1;
1098 ++biased_exponent;
1099 }
1100 }
1101 else
1102 {
1103 // Rounding toward zero.
1104
1105 if (mantissa == 0 && (midpoint_bit || nonzero_tail))
1106 {
1107 // A nonzero number that rounds to zero is unrepresentable.
1108 __glibcxx_assert(biased_exponent == 0);
1109 return {first, errc::result_out_of_range};
1110 }
1111
1112 midpoint_bit = false;
1113 nonzero_tail = false;
1114 }
1115
1116 if (mantissa != 0 && biased_exponent >= (1 << exponent_bits) - 1)
1117 // The exponent of this number is too large to be representable.
1118 return {first, errc::result_out_of_range};
1119
1120 uint_t result = 0;
1121 if (mantissa == 0)
1122 {
1123 // Assemble a (possibly signed) zero.
1124 if (sign_bit)
1125 result |= 1ull << (exponent_bits + mantissa_bits);
1126 }
1127 else
1128 {
1129 // Assemble a nonzero normal or subnormal value.
1130 result |= sign_bit;
1131 result <<= exponent_bits;
1132 result |= biased_exponent;
1133 result <<= mantissa_bits;
1134 result |= mantissa & ((1ull << mantissa_bits) - 1);
1135 // The implicit leading mantissa bit is set iff the number is normal.
1136 __glibcxx_assert(((mantissa & (1ull << mantissa_bits)) != 0)
1137 == (biased_exponent != 0));
1138 }
81f98afa
JJ
1139 if constexpr (is_same_v<T, float> || is_same_v<T, double>)
1140 memcpy(&value, &result, sizeof(result));
e6a32c12 1141#if USE_LIB_FAST_FLOAT
81f98afa
JJ
1142 else if constexpr (is_same_v<T, fast_float::floating_type_bfloat16_t>)
1143 {
1144 uint32_t res = uint32_t{result} << 16;
1145 memcpy(value.x, &res, sizeof(res));
1146 }
1147 else
1148 {
1149 // Otherwise float16_t which needs to be converted to float32_t.
1150 uint32_t res;
1151 if ((result & 0x7FFF) == 0)
1152 res = uint32_t{result} << 16; // +/-0.0f16
1153 else if ((result & 0x7C00) == 0)
1154 { // denormal
1155 unsigned n = (std::numeric_limits<unsigned int>::digits
1156 - __builtin_clz (result & 0x3FF) - 1);
1157 res = uint32_t{result} & 0x3FF & ~(uint32_t{1} << n);
1158 res <<= 23 - n;
1159 res |= (((uint32_t{n} + 0x67) << 23)
1160 | ((uint32_t{result} & 0x8000) << 16));
1161 }
1162 else
1163 res = (((uint32_t{result} & 0x3FF) << 13)
1164 | ((((uint32_t{result} >> 10) & 0x1F) + 0x70) << 23)
1165 | ((uint32_t{result} & 0x8000) << 16));
1166 memcpy(value.x, &res, sizeof(res));
1167 }
e6a32c12 1168#endif
cc3bf340
PP
1169
1170 return {first, errc{}};
1171 }
416b6fc7 1172#endif // _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
cc3bf340 1173
932fbc86
JW
1174} // namespace
1175
416b6fc7 1176#if USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS
932fbc86
JW
1177
1178from_chars_result
1179from_chars(const char* first, const char* last, float& value,
1180 chars_format fmt) noexcept
1181{
5a4e2080 1182#if USE_LIB_FAST_FLOAT
cc3bf340
PP
1183 if (fmt == chars_format::hex)
1184 return __floating_from_chars_hex(first, last, value);
490e2303 1185 else
81f98afa 1186 return fast_float::from_chars(first, last, value, fmt);
490e2303 1187#else
5a4e2080 1188 return from_chars_strtod(first, last, value, fmt);
490e2303 1189#endif
932fbc86
JW
1190}
1191
1192from_chars_result
1193from_chars(const char* first, const char* last, double& value,
1194 chars_format fmt) noexcept
1195{
5a4e2080 1196#if USE_LIB_FAST_FLOAT
cc3bf340
PP
1197 if (fmt == chars_format::hex)
1198 return __floating_from_chars_hex(first, last, value);
490e2303 1199 else
81f98afa 1200 return fast_float::from_chars(first, last, value, fmt);
490e2303 1201#else
5a4e2080 1202 return from_chars_strtod(first, last, value, fmt);
490e2303 1203#endif
932fbc86
JW
1204}
1205
1206from_chars_result
1207from_chars(const char* first, const char* last, long double& value,
1208 chars_format fmt) noexcept
1209{
5a4e2080
JW
1210#if ! USE_STRTOD_FOR_FROM_CHARS
1211 // Either long double is the same as double, or we can't use strtold.
1212 // In the latter case, this might give an incorrect result (e.g. values
1213 // out of range of double give an error, even if they fit in long double).
416b6fc7
JW
1214 double dbl_value;
1215 from_chars_result result;
1216 if (fmt == chars_format::hex)
1217 result = __floating_from_chars_hex(first, last, dbl_value);
1218 else
81f98afa 1219 result = fast_float::from_chars(first, last, dbl_value, fmt);
416b6fc7
JW
1220 if (result.ec == errc{})
1221 value = dbl_value;
1222 return result;
1223#else
5a4e2080 1224 return from_chars_strtod(first, last, value, fmt);
416b6fc7 1225#endif
932fbc86
JW
1226}
1227
81f98afa
JJ
1228#if USE_LIB_FAST_FLOAT
1229// Entrypoints for 16-bit floats.
1230[[gnu::cold]] from_chars_result
1231__from_chars_float16_t(const char* first, const char* last, float& value,
1232 chars_format fmt) noexcept
1233{
1234 struct fast_float::floating_type_float16_t val{ &value, 0 };
1235 if (fmt == chars_format::hex)
1236 return __floating_from_chars_hex(first, last, val);
1237 else
1238 return fast_float::from_chars_16(first, last, val, fmt);
1239}
1240
1241[[gnu::cold]] from_chars_result
1242__from_chars_bfloat16_t(const char* first, const char* last, float& value,
1243 chars_format fmt) noexcept
1244{
1245 struct fast_float::floating_type_bfloat16_t val{ &value, 0 };
1246 if (fmt == chars_format::hex)
1247 return __floating_from_chars_hex(first, last, val);
1248 else
1249 return fast_float::from_chars_16(first, last, val, fmt);
1250}
1251#endif
1252
932fbc86 1253#ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
7c1e7eed
JW
1254// Make std::from_chars for 64-bit long double an alias for the overload
1255// for double.
932fbc86
JW
1256extern "C" from_chars_result
1257_ZSt10from_charsPKcS0_ReSt12chars_format(const char* first, const char* last,
1258 long double& value,
1259 chars_format fmt) noexcept
1260__attribute__((alias ("_ZSt10from_charsPKcS0_RdSt12chars_format")));
1261#endif
1262
7c1e7eed
JW
1263#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1264from_chars_result
1265from_chars(const char* first, const char* last, __ieee128& value,
1266 chars_format fmt) noexcept
1267{
5a4e2080
JW
1268 // fast_float doesn't support IEEE binary128 format, but we can use strtold.
1269 return from_chars_strtod(first, last, value, fmt);
7c1e7eed 1270}
e5bcbcd0
JJ
1271#elif defined(USE_STRTOF128_FOR_FROM_CHARS)
1272from_chars_result
1273from_chars(const char* first, const char* last, _Float128& value,
1274 chars_format fmt) noexcept
1275{
1276 // fast_float doesn't support IEEE binary128 format, but we can use strtold.
1277 return from_chars_strtod(first, last, value, fmt);
1278}
7c1e7eed
JW
1279#endif
1280
416b6fc7
JW
1281#endif // USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS
1282
932fbc86
JW
1283_GLIBCXX_END_NAMESPACE_VERSION
1284} // namespace std