]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/src/c++17/floating_from_chars.cc
libstdc++: Fix up libstdc++ build against glibc 2.25 or older [PR107562]
[gcc.git] / libstdc++-v3 / src / c++17 / floating_from_chars.cc
1 // std::from_chars implementation for floating-point types -*- C++ -*-
2
3 // Copyright (C) 2020-2022 Free Software Foundation, Inc.
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
30 // Prefer to use std::pmr::string if possible, which requires the cxx11 ABI.
31 #define _GLIBCXX_USE_CXX11_ABI 1
32
33 #include <array>
34 #include <charconv>
35 #include <bit>
36 #include <string>
37 #include <memory_resource>
38 #include <cfenv>
39 #include <cfloat>
40 #include <cmath>
41 #include <cstdlib>
42 #include <cstring>
43 #include <locale.h>
44 #include <bits/functexcept.h>
45 #if _GLIBCXX_HAVE_XLOCALE_H
46 # include <xlocale.h>
47 #endif
48
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
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
61 extern "C" __ieee128 __strtoieee128(const char*, char**);
62 #elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \
63 && defined(__GLIBC_PREREQ)
64 #define USE_STRTOF128_FOR_FROM_CHARS 1
65 extern "C" _Float128 __strtof128(const char*, char**)
66 __asm ("strtof128")
67 #ifndef _GLIBCXX_HAVE_FLOAT128_MATH
68 __attribute__((__weak__))
69 #endif
70 ;
71 #endif
72
73 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
74 && __SIZE_WIDTH__ >= 32
75 # define USE_LIB_FAST_FLOAT 1
76 # if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
77 // No need to use strtold.
78 # undef USE_STRTOD_FOR_FROM_CHARS
79 # endif
80 #endif
81
82 #if USE_LIB_FAST_FLOAT
83 # define FASTFLOAT_DEBUG_ASSERT __glibcxx_assert
84 namespace
85 {
86 # include "fast_float/fast_float.h"
87
88 namespace 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
353 } // anon namespace
354 #endif
355
356 namespace std _GLIBCXX_VISIBILITY(default)
357 {
358 _GLIBCXX_BEGIN_NAMESPACE_VERSION
359
360 namespace
361 {
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
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);
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
401 #if _GLIBCXX_USE_CXX11_ABI
402 using buffered_string = std::pmr::string;
403 #else
404 using buffered_string = std::string;
405 #endif
406
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`
419 // and `exp` is "eE", "pP" or NULL.
420 const char*
421 find_end_of_float(const char* first, const char* last, const char* digits,
422 const char *exp)
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 }
432 if (first < last && exp != nullptr && (*first == exp[0] || *first == exp[1]))
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,
450 chars_format& fmt, buffered_string& buf)
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 {
514 last = find_end_of_float(first + neg, last, digits, "pP");
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,
538 fmt == chars_format::fixed ? nullptr : "eE");
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 {
606 if (locale_t loc = ::newlocale(LC_ALL_MASK, "C", (locale_t)0)) [[likely]]
607 {
608 locale_t orig = ::uselocale(loc);
609
610 #if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
611 const int rounding = std::fegetround();
612 if (rounding != FE_TONEAREST)
613 std::fesetround(FE_TONEAREST);
614 #endif
615
616 const int save_errno = errno;
617 errno = 0;
618 char* endptr;
619 T tmpval;
620 #if _GLIBCXX_USE_C99_STDLIB
621 if constexpr (is_same_v<T, float>)
622 tmpval = std::strtof(str, &endptr);
623 else if constexpr (is_same_v<T, double>)
624 tmpval = std::strtod(str, &endptr);
625 else if constexpr (is_same_v<T, long double>)
626 tmpval = std::strtold(str, &endptr);
627 # ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
628 else if constexpr (is_same_v<T, __ieee128>)
629 tmpval = __strtoieee128(str, &endptr);
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)
635 tmpval = _Float128(std::strtold(str, &endptr);
636 else
637 #endif
638 tmpval = __strtof128(str, &endptr);
639 }
640 # endif
641 #else
642 tmpval = std::strtod(str, &endptr);
643 #endif
644 const int conv_errno = std::__exchange(errno, save_errno);
645
646 #if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
647 if (rounding != FE_TONEAREST)
648 std::fesetround(rounding);
649 #endif
650
651 ::uselocale(orig);
652 ::freelocale(loc);
653
654 const ptrdiff_t n = endptr - str;
655 if (conv_errno == ERANGE) [[unlikely]]
656 {
657 if (__builtin_isinf(tmpval)) // overflow
658 ec = errc::result_out_of_range;
659 else if (tmpval == 0) // underflow (LWG 3081 wants to set value = tmpval here)
660 ec = errc::result_out_of_range;
661 else // denormal value
662 {
663 value = tmpval;
664 ec = errc();
665 }
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
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
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 }
740 #endif // USE_STRTOD_FOR_FROM_CHARS
741
742 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
743 // Return true iff [FIRST,LAST) begins with PREFIX, ignoring case.
744 // PREFIX is assumed to not contain any uppercase letters.
745 bool
746 starts_with_ci(const char* first, const char* last, string_view prefix)
747 {
748 __glibcxx_requires_valid_range(first, last);
749
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)
767 {
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)
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 {
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>>;
786 constexpr int mantissa_bits
787 = fast_float::binary_format<T>::mantissa_explicit_bits();
788 constexpr int exponent_bits
789 = is_same_v<T, double> ? 11
790 : is_same_v<T, fast_float::floating_type_float16_t> ? 5 : 8;
791 constexpr int exponent_bias = (1 << (exponent_bits - 1)) - 1;
792
793 __glibcxx_requires_valid_range(first, last);
794 if (first == last)
795 return {first, errc::invalid_argument};
796
797 // Consume the sign bit.
798 const char* const orig_first = first;
799 bool sign_bit = false;
800 if (*first == '-')
801 {
802 sign_bit = true;
803 ++first;
804 }
805
806 // Handle "inf", "infinity", "NaN" and variants thereof.
807 if (first != last)
808 if (*first == 'i' || *first == 'I' || *first == 'n' || *first == 'N') [[unlikely]]
809 {
810 if (starts_with_ci(first, last, "inf"sv))
811 {
812 first += strlen("inf");
813 if (starts_with_ci(first, last, "inity"sv))
814 first += strlen("inity");
815
816 if constexpr (is_same_v<T, float> || is_same_v<T, double>)
817 {
818 uint_t result = 0;
819 result |= sign_bit;
820 result <<= exponent_bits;
821 result |= (1ull << exponent_bits) - 1;
822 result <<= mantissa_bits;
823 memcpy(&value, &result, sizeof(result));
824 }
825 else
826 {
827 // float +/-Inf.
828 uint32_t result = 0x7F800000 | (sign_bit ? 0x80000000U : 0);
829 memcpy(value.x, &result, sizeof(result));
830 }
831
832 return {first, errc{}};
833 }
834 else if (starts_with_ci(first, last, "nan"))
835 {
836 first += strlen("nan");
837
838 if (first != last && *first == '(')
839 {
840 // Tentatively consume the '(' as we look for an optional
841 // n-char-sequence followed by a ')'.
842 const char* const fallback_first = first;
843 for (;;)
844 {
845 ++first;
846 if (first == last)
847 {
848 first = fallback_first;
849 break;
850 }
851
852 char ch = *first;
853 if (ch == ')')
854 {
855 ++first;
856 break;
857 }
858 else if (ch == '_'
859 || __detail::__from_chars_alnum_to_val(ch) < 127)
860 continue;
861 else
862 {
863 first = fallback_first;
864 break;
865 }
866 }
867 }
868
869 // We make the implementation-defined decision of ignoring the
870 // sign bit and the n-char-sequence when assembling the NaN.
871 if constexpr (is_same_v<T, float> || is_same_v<T, double>)
872 {
873 uint_t result = 0;
874 result <<= exponent_bits;
875 result |= (1ull << exponent_bits) - 1;
876 result <<= mantissa_bits;
877 result |= (1ull << (mantissa_bits - 1)) | 1;
878 memcpy(&value, &result, sizeof(result));
879 }
880 else
881 {
882 // float qNaN.
883 uint32_t result = 0x7FC00001;
884 memcpy(value.x, &result, sizeof(result));
885 }
886
887 return {first, errc{}};
888 }
889 }
890
891 // Consume all insignificant leading zeros in the whole part of the
892 // mantissa.
893 bool seen_hexit = false;
894 while (first != last && *first == '0')
895 {
896 seen_hexit = true;
897 ++first;
898 }
899
900 // Now consume the rest of the written mantissa, populating MANTISSA with
901 // the first MANTISSA_BITS+k significant bits of the written mantissa, where
902 // 1 <= k <= 4 is the bit width of the leading significant written hexit.
903 //
904 // Examples:
905 // After parsing "1.2f3", MANTISSA is 0x12f30000000000 (bit_width=52+1).
906 // After parsing ".0000f0e", MANTISSA is 0xf0e00000000000 (bit_width=52+4).
907 // After parsing ".1234567890abcd8", MANTISSA is 0x1234567890abcd (bit_width=52+1)
908 // and MIDPOINT_BIT is true (and NONZERO_TAIL is false).
909 uint_t mantissa = 0;
910 int mantissa_idx = mantissa_bits; // The current bit index into MANTISSA
911 // into which we'll write the next hexit.
912 int exponent_adjustment = 0; // How much we'd have to adjust the written
913 // exponent in order to represent the mantissa
914 // in scientific form h.hhhhhhhhhhhhh.
915 bool midpoint_bit = false; // Whether the MANTISSA_BITS+k+1 significant
916 // bit is set in the written mantissa.
917 bool nonzero_tail = false; // Whether some bit thereafter is set in the
918 // written mantissa.
919 bool seen_decimal_point = false;
920 for (; first != last; ++first)
921 {
922 char ch = *first;
923 if (ch == '.' && !seen_decimal_point)
924 {
925 seen_decimal_point = true;
926 continue;
927 }
928
929 int hexit = __detail::__from_chars_alnum_to_val(ch);
930 if (hexit >= 16)
931 break;
932 seen_hexit = true;
933
934 if (!seen_decimal_point && mantissa != 0)
935 exponent_adjustment += 4;
936 else if (seen_decimal_point && mantissa == 0)
937 {
938 exponent_adjustment -= 4;
939 if (hexit == 0x0)
940 continue;
941 }
942
943 if (mantissa_idx >= 0)
944 mantissa |= uint_t(hexit) << mantissa_idx;
945 else if (mantissa_idx >= -4)
946 {
947 if constexpr (is_same_v<T, float>
948 || is_same_v<T,
949 fast_float::floating_type_bfloat16_t>)
950 {
951 __glibcxx_assert(mantissa_idx == -1);
952 mantissa |= hexit >> 1;
953 midpoint_bit = (hexit & 0b0001) != 0;
954 }
955 else if constexpr (is_same_v<T, double>)
956 {
957 __glibcxx_assert(mantissa_idx == -4);
958 midpoint_bit = (hexit & 0b1000) != 0;
959 nonzero_tail = (hexit & 0b0111) != 0;
960 }
961 else
962 {
963 __glibcxx_assert(mantissa_idx == -2);
964 mantissa |= hexit >> 2;
965 midpoint_bit = (hexit & 0b0010) != 0;
966 nonzero_tail = (hexit & 0b0001) != 0;
967 }
968 }
969 else
970 nonzero_tail |= (hexit != 0x0);
971
972 mantissa_idx -= 4;
973 }
974 if (mantissa != 0)
975 __glibcxx_assert(__bit_width(mantissa) >= mantissa_bits + 1
976 && __bit_width(mantissa) <= mantissa_bits + 4);
977 else
978 __glibcxx_assert(!midpoint_bit && !nonzero_tail);
979
980 if (!seen_hexit)
981 // If we haven't seen any hexit at this point, the parse failed.
982 return {orig_first, errc::invalid_argument};
983
984 // Parse the written exponent.
985 int written_exponent = 0;
986 if (first != last && (*first == 'p' || *first == 'P'))
987 {
988 // Tentatively consume the 'p' and try to parse a decimal number.
989 const char* const fallback_first = first;
990 ++first;
991 if (first != last && *first == '+')
992 ++first;
993 from_chars_result fcr = from_chars(first, last, written_exponent, 10);
994 if (fcr.ptr == first)
995 // The parse failed, so undo consuming the 'p' and carry on as if the
996 // exponent was omitted (i.e. is 0).
997 first = fallback_first;
998 else
999 {
1000 first = fcr.ptr;
1001 if (mantissa != 0 && fcr.ec == errc::result_out_of_range)
1002 // Punt on very large exponents for now. FIXME
1003 return {first, errc::result_out_of_range};
1004 }
1005 }
1006 int biased_exponent = written_exponent + exponent_bias;
1007 if (exponent_adjustment != 0)
1008 // The mantissa wasn't written in scientific form. Adjust the exponent
1009 // so that we may assume scientific form.
1010 //
1011 // Examples;
1012 // For input "a.bcp5", EXPONENT_ADJUSTMENT would be 0 since this
1013 // written mantissa is already in scientific form.
1014 // For input "ab.cp5", EXPONENT_ADJUSTMENT would be 4 since the
1015 // scientific form is "a.bcp9".
1016 // For input 0.0abcp5", EXPONENT_ADJUSTMENT would be -8 since the
1017 // scientific form is "a.bcp-3".
1018 biased_exponent += exponent_adjustment;
1019
1020 // Shifts the mantissa to the right by AMOUNT while updating
1021 // BIASED_EXPONENT, MIDPOINT_BIT and NONZERO_TAIL accordingly.
1022 auto shift_mantissa = [&] (int amount) {
1023 __glibcxx_assert(amount >= 0);
1024 if (amount > mantissa_bits + 1)
1025 {
1026 // Shifting the mantissa by an amount greater than its precision.
1027 nonzero_tail |= midpoint_bit;
1028 nonzero_tail |= mantissa != 0;
1029 midpoint_bit = false;
1030 mantissa = 0;
1031 biased_exponent += amount;
1032 }
1033 else if (amount != 0)
1034 {
1035 nonzero_tail |= midpoint_bit;
1036 nonzero_tail |= (mantissa & ((1ull << (amount - 1)) - 1)) != 0;
1037 midpoint_bit = (mantissa & (1ull << (amount - 1))) != 0;
1038 mantissa >>= amount;
1039 biased_exponent += amount;
1040 }
1041 };
1042
1043 if (mantissa != 0)
1044 {
1045 // If the leading hexit is not '1', shift MANTISSA to make it so.
1046 // This normalizes input like "4.08p0" into "1.02p2".
1047 const int leading_hexit = mantissa >> mantissa_bits;
1048 const int leading_hexit_width = __bit_width(leading_hexit); // FIXME: optimize?
1049 __glibcxx_assert(leading_hexit_width >= 1 && leading_hexit_width <= 4);
1050 shift_mantissa(leading_hexit_width - 1);
1051 // After this adjustment, we can assume the leading hexit is '1'.
1052 __glibcxx_assert((mantissa >> mantissa_bits) == 0x1);
1053 }
1054
1055 if (biased_exponent <= 0)
1056 {
1057 // This number is too small to be represented as a normal number, so
1058 // try for a subnormal number by shifting the mantissa sufficiently.
1059 // We need to shift by 1 more than -BIASED_EXPONENT because the leading
1060 // mantissa bit is omitted in the representation of a normal number but
1061 // not in a subnormal number.
1062 shift_mantissa(-biased_exponent + 1);
1063 __glibcxx_assert(!(mantissa & (1ull << mantissa_bits)));
1064 __glibcxx_assert(biased_exponent == 1);
1065 biased_exponent = 0;
1066 }
1067
1068 // Perform round-to-nearest, tie-to-even rounding according to
1069 // MIDPOINT_BIT and NONZERO_TAIL.
1070 if (midpoint_bit && (nonzero_tail || (mantissa % 2) != 0))
1071 {
1072 // Rounding away from zero.
1073 ++mantissa;
1074 midpoint_bit = false;
1075 nonzero_tail = false;
1076
1077 // Deal with a couple of corner cases after rounding.
1078 if (mantissa == (1ull << mantissa_bits))
1079 {
1080 // We rounded the subnormal number 1.fffffffffffff...p-1023
1081 // up to the normal number 1p-1022.
1082 __glibcxx_assert(biased_exponent == 0);
1083 ++biased_exponent;
1084 }
1085 else if (mantissa & (1ull << (mantissa_bits + 1)))
1086 {
1087 // We rounded the normal number 1.fffffffffffff8pN (with maximal
1088 // mantissa) up to to 1p(N+1).
1089 mantissa >>= 1;
1090 ++biased_exponent;
1091 }
1092 }
1093 else
1094 {
1095 // Rounding toward zero.
1096
1097 if (mantissa == 0 && (midpoint_bit || nonzero_tail))
1098 {
1099 // A nonzero number that rounds to zero is unrepresentable.
1100 __glibcxx_assert(biased_exponent == 0);
1101 return {first, errc::result_out_of_range};
1102 }
1103
1104 midpoint_bit = false;
1105 nonzero_tail = false;
1106 }
1107
1108 if (mantissa != 0 && biased_exponent >= (1 << exponent_bits) - 1)
1109 // The exponent of this number is too large to be representable.
1110 return {first, errc::result_out_of_range};
1111
1112 uint_t result = 0;
1113 if (mantissa == 0)
1114 {
1115 // Assemble a (possibly signed) zero.
1116 if (sign_bit)
1117 result |= 1ull << (exponent_bits + mantissa_bits);
1118 }
1119 else
1120 {
1121 // Assemble a nonzero normal or subnormal value.
1122 result |= sign_bit;
1123 result <<= exponent_bits;
1124 result |= biased_exponent;
1125 result <<= mantissa_bits;
1126 result |= mantissa & ((1ull << mantissa_bits) - 1);
1127 // The implicit leading mantissa bit is set iff the number is normal.
1128 __glibcxx_assert(((mantissa & (1ull << mantissa_bits)) != 0)
1129 == (biased_exponent != 0));
1130 }
1131 if constexpr (is_same_v<T, float> || is_same_v<T, double>)
1132 memcpy(&value, &result, sizeof(result));
1133 else if constexpr (is_same_v<T, fast_float::floating_type_bfloat16_t>)
1134 {
1135 uint32_t res = uint32_t{result} << 16;
1136 memcpy(value.x, &res, sizeof(res));
1137 }
1138 else
1139 {
1140 // Otherwise float16_t which needs to be converted to float32_t.
1141 uint32_t res;
1142 if ((result & 0x7FFF) == 0)
1143 res = uint32_t{result} << 16; // +/-0.0f16
1144 else if ((result & 0x7C00) == 0)
1145 { // denormal
1146 unsigned n = (std::numeric_limits<unsigned int>::digits
1147 - __builtin_clz (result & 0x3FF) - 1);
1148 res = uint32_t{result} & 0x3FF & ~(uint32_t{1} << n);
1149 res <<= 23 - n;
1150 res |= (((uint32_t{n} + 0x67) << 23)
1151 | ((uint32_t{result} & 0x8000) << 16));
1152 }
1153 else
1154 res = (((uint32_t{result} & 0x3FF) << 13)
1155 | ((((uint32_t{result} >> 10) & 0x1F) + 0x70) << 23)
1156 | ((uint32_t{result} & 0x8000) << 16));
1157 memcpy(value.x, &res, sizeof(res));
1158 }
1159
1160 return {first, errc{}};
1161 }
1162 #endif // _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
1163
1164 } // namespace
1165
1166 #if USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS
1167
1168 from_chars_result
1169 from_chars(const char* first, const char* last, float& value,
1170 chars_format fmt) noexcept
1171 {
1172 #if USE_LIB_FAST_FLOAT
1173 if (fmt == chars_format::hex)
1174 return __floating_from_chars_hex(first, last, value);
1175 else
1176 return fast_float::from_chars(first, last, value, fmt);
1177 #else
1178 return from_chars_strtod(first, last, value, fmt);
1179 #endif
1180 }
1181
1182 from_chars_result
1183 from_chars(const char* first, const char* last, double& value,
1184 chars_format fmt) noexcept
1185 {
1186 #if USE_LIB_FAST_FLOAT
1187 if (fmt == chars_format::hex)
1188 return __floating_from_chars_hex(first, last, value);
1189 else
1190 return fast_float::from_chars(first, last, value, fmt);
1191 #else
1192 return from_chars_strtod(first, last, value, fmt);
1193 #endif
1194 }
1195
1196 from_chars_result
1197 from_chars(const char* first, const char* last, long double& value,
1198 chars_format fmt) noexcept
1199 {
1200 #if ! USE_STRTOD_FOR_FROM_CHARS
1201 // Either long double is the same as double, or we can't use strtold.
1202 // In the latter case, this might give an incorrect result (e.g. values
1203 // out of range of double give an error, even if they fit in long double).
1204 double dbl_value;
1205 from_chars_result result;
1206 if (fmt == chars_format::hex)
1207 result = __floating_from_chars_hex(first, last, dbl_value);
1208 else
1209 result = fast_float::from_chars(first, last, dbl_value, fmt);
1210 if (result.ec == errc{})
1211 value = dbl_value;
1212 return result;
1213 #else
1214 return from_chars_strtod(first, last, value, fmt);
1215 #endif
1216 }
1217
1218 #if USE_LIB_FAST_FLOAT
1219 // Entrypoints for 16-bit floats.
1220 [[gnu::cold]] from_chars_result
1221 __from_chars_float16_t(const char* first, const char* last, float& value,
1222 chars_format fmt) noexcept
1223 {
1224 struct fast_float::floating_type_float16_t val{ &value, 0 };
1225 if (fmt == chars_format::hex)
1226 return __floating_from_chars_hex(first, last, val);
1227 else
1228 return fast_float::from_chars_16(first, last, val, fmt);
1229 }
1230
1231 [[gnu::cold]] from_chars_result
1232 __from_chars_bfloat16_t(const char* first, const char* last, float& value,
1233 chars_format fmt) noexcept
1234 {
1235 struct fast_float::floating_type_bfloat16_t val{ &value, 0 };
1236 if (fmt == chars_format::hex)
1237 return __floating_from_chars_hex(first, last, val);
1238 else
1239 return fast_float::from_chars_16(first, last, val, fmt);
1240 }
1241 #endif
1242
1243 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
1244 // Make std::from_chars for 64-bit long double an alias for the overload
1245 // for double.
1246 extern "C" from_chars_result
1247 _ZSt10from_charsPKcS0_ReSt12chars_format(const char* first, const char* last,
1248 long double& value,
1249 chars_format fmt) noexcept
1250 __attribute__((alias ("_ZSt10from_charsPKcS0_RdSt12chars_format")));
1251 #endif
1252
1253 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1254 from_chars_result
1255 from_chars(const char* first, const char* last, __ieee128& value,
1256 chars_format fmt) noexcept
1257 {
1258 // fast_float doesn't support IEEE binary128 format, but we can use strtold.
1259 return from_chars_strtod(first, last, value, fmt);
1260 }
1261 #elif defined(USE_STRTOF128_FOR_FROM_CHARS)
1262 from_chars_result
1263 from_chars(const char* first, const char* last, _Float128& value,
1264 chars_format fmt) noexcept
1265 {
1266 // fast_float doesn't support IEEE binary128 format, but we can use strtold.
1267 return from_chars_strtod(first, last, value, fmt);
1268 }
1269 #endif
1270
1271 #endif // USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS
1272
1273 _GLIBCXX_END_NAMESPACE_VERSION
1274 } // namespace std
This page took 0.11107 seconds and 5 git commands to generate.