1// Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
3// Copyright (C) 2017-2025 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/>.
25/** @file include/charconv
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_CHARCONV
30#define _GLIBCXX_CHARCONV 1
33#pragma GCC system_header
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wpedantic" // __int128
39#include <bits/requires_hosted.h> // for error codes
41// As an extension we support <charconv> in C++14, but this header should not
42// be included by any other library headers in C++14 mode. This ensures that
43// the names defined in this header are not added to namespace std unless a
44// user explicitly includes <charconv> in C++14 code.
45#if __cplusplus >= 201402L
48#include <bit> // for __bit_width
49#include <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
50#include <bits/error_constants.h> // for std::errc
51#include <ext/numeric_traits.h>
53#define __glibcxx_want_to_chars
54#define __glibcxx_want_constexpr_charconv
55#include <bits/version.h>
57namespace std _GLIBCXX_VISIBILITY(default)
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
61 /// Result type of std::to_chars
62 struct to_chars_result
67#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
69 operator==(const to_chars_result&, const to_chars_result&) = default;
71#if __cplusplus > 202302L
72 constexpr explicit operator bool() const noexcept { return ec == errc{}; }
76 /// Result type of std::from_chars
77 struct from_chars_result
82#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
84 operator==(const from_chars_result&, const from_chars_result&) = default;
86#if __cplusplus > 202302L
87 constexpr explicit operator bool() const noexcept { return ec == errc{}; }
93 // Pick an unsigned type of suitable size. This is used to reduce the
94 // number of specializations of __to_chars_len, __to_chars etc. that
95 // get instantiated. For example, to_chars<char> and to_chars<short>
96 // and to_chars<unsigned> will all use the same code, and so will
97 // to_chars<long> when sizeof(int) == sizeof(long).
98 template<typename _Tp>
99 struct __to_chars_unsigned_type : __make_unsigned_selector_base
101 using _UInts = _List<unsigned int, unsigned long, unsigned long long
102#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
106 using type = typename __select<sizeof(_Tp), _UInts>::__type;
109 template<typename _Tp>
110 using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
112 // Generic implementation for arbitrary bases.
113 // Defined in <bits/charconv.h>.
114 template<typename _Tp>
116 __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
118 template<typename _Tp>
120 __to_chars_len_2(_Tp __value) noexcept
121 { return std::__bit_width(__value); }
123 // Generic implementation for arbitrary bases.
124 template<typename _Tp>
125 constexpr to_chars_result
126 __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
128 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
130 to_chars_result __res;
132 const unsigned __len = __to_chars_len(__val, __base);
134 if (__builtin_expect(size_t(__last - __first) < __len, 0))
137 __res.ec = errc::value_too_large;
141 unsigned __pos = __len - 1;
143 constexpr char __digits[] = {
144 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
145 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
146 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
147 'u', 'v', 'w', 'x', 'y', 'z'
150 while (__val >= (unsigned)__base)
152 auto const __quo = __val / __base;
153 auto const __rem = __val % __base;
154 __first[__pos--] = __digits[__rem];
157 *__first = __digits[__val];
159 __res.ptr = __first + __len;
164 template<typename _Tp>
165 constexpr to_chars_result
166 __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
168 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
170 to_chars_result __res;
172 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
174 if (__builtin_expect(size_t(__last - __first) < __len, 0))
177 __res.ec = errc::value_too_large;
181 constexpr char __digits[] = {
182 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
183 'a', 'b', 'c', 'd', 'e', 'f'
185 unsigned __pos = __len - 1;
186 while (__val >= 0x100)
188 auto __num = __val & 0xF;
190 __first[__pos] = __digits[__num];
193 __first[__pos - 1] = __digits[__num];
198 const auto __num = __val & 0xF;
200 __first[1] = __digits[__num];
201 __first[0] = __digits[__val];
204 __first[0] = __digits[__val];
205 __res.ptr = __first + __len;
210 template<typename _Tp>
211 constexpr to_chars_result
212 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
214 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
216 to_chars_result __res;
218 const unsigned __len = __to_chars_len(__val, 10);
220 if (__builtin_expect(size_t(__last - __first) < __len, 0))
223 __res.ec = errc::value_too_large;
227 __detail::__to_chars_10_impl(__first, __len, __val);
228 __res.ptr = __first + __len;
233 template<typename _Tp>
234 constexpr to_chars_result
235 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
237 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
239 to_chars_result __res;
242 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
244 __len = __val > 077777u ? 6u
245 : __val > 07777u ? 5u
252 __len = (__to_chars_len_2(__val) + 2) / 3;
254 if (__builtin_expect(size_t(__last - __first) < __len, 0))
257 __res.ec = errc::value_too_large;
261 unsigned __pos = __len - 1;
262 while (__val >= 0100)
264 auto __num = __val & 7;
266 __first[__pos] = '0' + __num;
269 __first[__pos - 1] = '0' + __num;
274 auto const __num = __val & 7;
276 __first[1] = '0' + __num;
277 __first[0] = '0' + __val;
280 __first[0] = '0' + __val;
281 __res.ptr = __first + __len;
286 template<typename _Tp>
287 constexpr to_chars_result
288 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
290 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
292 to_chars_result __res;
294 const unsigned __len = __to_chars_len_2(__val);
296 if (__builtin_expect(size_t(__last - __first) < __len, 0))
299 __res.ec = errc::value_too_large;
303 unsigned __pos = __len - 1;
307 __first[__pos--] = '0' + (__val & 1);
310 // First digit is always '1' because __to_chars_len_2 skips
311 // leading zero bits and std::to_chars handles zero values
315 __res.ptr = __first + __len;
320} // namespace __detail
322 template<typename _Tp>
323 constexpr to_chars_result
324 __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
326 __glibcxx_assert(2 <= __base && __base <= 36);
328 using _Up = __detail::__unsigned_least_t<_Tp>;
329 _Up __unsigned_val = __value;
331 if (__first >= __last) [[__unlikely__]]
332 return { __last, errc::value_too_large };
337 return { __first + 1, errc{} };
339 else if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
343 __unsigned_val = _Up(~__value) + _Up(1);
349 return __detail::__to_chars_16(__first, __last, __unsigned_val);
351 return __detail::__to_chars_10(__first, __last, __unsigned_val);
353 return __detail::__to_chars_8(__first, __last, __unsigned_val);
355 return __detail::__to_chars_2(__first, __last, __unsigned_val);
357 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
361#define _GLIBCXX_TO_CHARS(T) \
362 _GLIBCXX23_CONSTEXPR inline to_chars_result \
363 to_chars(char* __first, char* __last, T __value, int __base = 10) \
364 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
365_GLIBCXX_TO_CHARS(char)
366_GLIBCXX_TO_CHARS(signed char)
367_GLIBCXX_TO_CHARS(unsigned char)
368_GLIBCXX_TO_CHARS(signed short)
369_GLIBCXX_TO_CHARS(unsigned short)
370_GLIBCXX_TO_CHARS(signed int)
371_GLIBCXX_TO_CHARS(unsigned int)
372_GLIBCXX_TO_CHARS(signed long)
373_GLIBCXX_TO_CHARS(unsigned long)
374_GLIBCXX_TO_CHARS(signed long long)
375_GLIBCXX_TO_CHARS(unsigned long long)
376#if defined(__GLIBCXX_TYPE_INT_N_0)
377_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
378_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
380#if defined(__GLIBCXX_TYPE_INT_N_1)
381_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
382_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
384#if defined(__GLIBCXX_TYPE_INT_N_2)
385_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
386_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
388#if defined(__GLIBCXX_TYPE_INT_N_3)
389_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
390_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
392#undef _GLIBCXX_TO_CHARS
394 // _GLIBCXX_RESOLVE_LIB_DEFECTS
395 // 3266. to_chars(bool) should be deleted
396 to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
400 template<typename _Tp>
402 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
404 if (__builtin_mul_overflow(__val, __base, &__val)
405 || __builtin_add_overflow(__val, __c, &__val))
410 template<bool _DecOnly>
411 struct __from_chars_alnum_to_val_table
413 struct type { unsigned char __data[1u << __CHAR_BIT__] = {}; };
415 // Construct and return a lookup table that maps 0-9, A-Z and a-z to their
416 // corresponding base-36 value and maps all other characters to 127.
417 static constexpr type
420 constexpr unsigned char __lower_letters[27] = "abcdefghijklmnopqrstuvwxyz";
421 constexpr unsigned char __upper_letters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
423 for (auto& __entry : __table.__data)
425 for (int __i = 0; __i < 10; ++__i)
426 __table.__data['0' + __i] = __i;
427 for (int __i = 0; __i < 26; ++__i)
429 __table.__data[__lower_letters[__i]] = 10 + __i;
430 __table.__data[__upper_letters[__i]] = 10 + __i;
435 // This initializer is made superficially dependent in order
436 // to prevent the compiler from wastefully constructing the
437 // table ahead of time when it's not needed.
438 static constexpr type value = (_DecOnly, _S_make_table());
441#if ! __cpp_inline_variables
442 template<bool _DecOnly>
443 const typename __from_chars_alnum_to_val_table<_DecOnly>::type
444 __from_chars_alnum_to_val_table<_DecOnly>::value;
447 // If _DecOnly is true: if the character is a decimal digit, then
448 // return its corresponding base-10 value, otherwise return a value >= 127.
449 // If _DecOnly is false: if the character is an alphanumeric digit, then
450 // return its corresponding base-36 value, otherwise return a value >= 127.
451 template<bool _DecOnly = false>
452 _GLIBCXX20_CONSTEXPR unsigned char
453 __from_chars_alnum_to_val(unsigned char __c)
455 if _GLIBCXX17_CONSTEXPR (_DecOnly)
456 return static_cast<unsigned char>(__c - '0');
458 return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
461 /// std::from_chars implementation for integers in a power-of-two base.
462 /// If _DecOnly is true, then we may assume __base is at most 8.
463 template<bool _DecOnly, typename _Tp>
464 _GLIBCXX23_CONSTEXPR bool
465 __from_chars_pow2_base(const char*& __first, const char* __last, _Tp& __val,
468 static_assert(is_integral<_Tp>::value, "implementation bug");
469 static_assert(is_unsigned<_Tp>::value, "implementation bug");
471 // __glibcxx_assert((__base & (__base - 1)) == 0);
472 // __glibcxx_assert(_DecOnly ? __base <= 8 : __base <= 32);
473 const int __log2_base = __countr_zero(unsigned(__base & 0x3f));
475 const ptrdiff_t __len = __last - __first;
477 while (__i < __len && __first[__i] == '0')
479 const ptrdiff_t __leading_zeroes = __i;
480 if (__i >= __len) [[__unlikely__]]
486 // Remember the leading significant digit value if necessary.
487 unsigned char __leading_c = 0;
490 __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
491 // __glibcxx_assert(__leading_c != 0);
492 if (__leading_c >= __base) [[__unlikely__]]
501 for (; __i < __len; ++__i)
503 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
506 __val = (__val << __log2_base) | __c;
509 auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
511 // Compensate for a leading significant digit that didn't use all
512 // of its available bits.
513 __significant_bits -= __log2_base - __bit_width(__leading_c);
514 // __glibcxx_assert(__significant_bits >= 0);
515 return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
518 /// std::from_chars implementation for integers in any base.
519 /// If _DecOnly is true, then we may assume __base is at most 10.
520 template<bool _DecOnly, typename _Tp>
522 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
525 // __glibcxx_assert(_DecOnly ? __base <= 10 : __base <= 36);
527 const int __bits_per_digit = __bit_width(unsigned(__base & 0x3f));
528 int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
529 for (; __first != __last; ++__first)
531 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
535 __unused_bits_lower_bound -= __bits_per_digit;
536 if (__unused_bits_lower_bound >= 0) [[__likely__]]
537 // We're definitely not going to overflow.
538 __val = __val * __base + __c;
539 else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
541 while (++__first != __last
542 && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
550} // namespace __detail
552 /// std::from_chars for integral types.
553 template<typename _Tp,
554 enable_if_t<__or_<__is_standard_integer<_Tp>,
555 is_same<char, remove_cv_t<_Tp>>>::value, int> = 0>
556 _GLIBCXX23_CONSTEXPR from_chars_result
557 from_chars(const char* __first, const char* __last, _Tp& __value,
560 __glibcxx_assert(2 <= __base && __base <= 36);
562 from_chars_result __res{__first, {}};
565 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
566 if (__first != __last && *__first == '-')
572 using _Up = __detail::__unsigned_least_t<_Tp>;
575 const auto __start = __first;
577 if ((__base & (__base - 1)) == 0)
580 __valid = __detail::__from_chars_pow2_base<true>(__first, __last, __val, __base);
582 __valid = __detail::__from_chars_pow2_base<false>(__first, __last, __val, __base);
584 else if (__base <= 10)
585 __valid = __detail::__from_chars_alnum<true>(__first, __last, __val, __base);
587 __valid = __detail::__from_chars_alnum<false>(__first, __last, __val, __base);
589 if (__builtin_expect(__first == __start, 0))
590 __res.ec = errc::invalid_argument;
595 __res.ec = errc::result_out_of_range;
598 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
601 if (__builtin_mul_overflow(__val, __sign, &__tmp))
602 __res.ec = errc::result_out_of_range;
608 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Up>::__max
609 > __gnu_cxx::__int_traits<_Tp>::__max)
611 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
612 __res.ec = errc::result_out_of_range;
624 /// floating-point format for primitive numerical conversion
625 enum class chars_format
627 scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
631 constexpr chars_format
632 operator|(chars_format __lhs, chars_format __rhs) noexcept
633 { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
636 constexpr chars_format
637 operator&(chars_format __lhs, chars_format __rhs) noexcept
638 { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
641 constexpr chars_format
642 operator^(chars_format __lhs, chars_format __rhs) noexcept
643 { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
646 constexpr chars_format
647 operator~(chars_format __fmt) noexcept
648 { return (chars_format)~(unsigned)__fmt; }
650 constexpr chars_format&
651 operator|=(chars_format& __lhs, chars_format __rhs) noexcept
652 { return __lhs = __lhs | __rhs; }
654 constexpr chars_format&
655 operator&=(chars_format& __lhs, chars_format __rhs) noexcept
656 { return __lhs = __lhs & __rhs; }
658 constexpr chars_format&
659 operator^=(chars_format& __lhs, chars_format __rhs) noexcept
660 { return __lhs = __lhs ^ __rhs; }
662#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
664 from_chars(const char* __first, const char* __last, float& __value,
665 chars_format __fmt = chars_format::general) noexcept;
668 from_chars(const char* __first, const char* __last, double& __value,
669 chars_format __fmt = chars_format::general) noexcept;
672 from_chars(const char* __first, const char* __last, long double& __value,
673 chars_format __fmt = chars_format::general) noexcept;
675 // Library routines for 16-bit extended floating point formats
676 // using float as interchange format.
678 __from_chars_float16_t(const char* __first, const char* __last,
680 chars_format __fmt = chars_format::general) noexcept;
682 __from_chars_bfloat16_t(const char* __first, const char* __last,
684 chars_format __fmt = chars_format::general) noexcept;
686#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
687 && defined(__cpp_lib_to_chars)
688 inline from_chars_result
689 from_chars(const char* __first, const char* __last, _Float16& __value,
690 chars_format __fmt = chars_format::general) noexcept
693 from_chars_result __res
694 = __from_chars_float16_t(__first, __last, __val, __fmt);
695 if (__res.ec == errc{})
696 __value = _Float16(__val);
701#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
702 inline from_chars_result
703 from_chars(const char* __first, const char* __last, _Float32& __value,
704 chars_format __fmt = chars_format::general) noexcept
707 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
708 if (__res.ec == errc{})
709 __value = _Float32(__val);
714#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
715 inline from_chars_result
716 from_chars(const char* __first, const char* __last, _Float64& __value,
717 chars_format __fmt = chars_format::general) noexcept
720 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
721 if (__res.ec == errc{})
722 __value = _Float64(__val);
727#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
728 inline from_chars_result
729 from_chars(const char* __first, const char* __last, _Float128& __value,
730 chars_format __fmt = chars_format::general) noexcept
733 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
734 if (__res.ec == errc{})
735 __value = _Float128(__val);
738#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
739#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
740 __extension__ from_chars_result
741 from_chars(const char* __first, const char* __last, __ieee128& __value,
742 chars_format __fmt = chars_format::general) noexcept;
744 inline from_chars_result
745 from_chars(const char* __first, const char* __last, _Float128& __value,
746 chars_format __fmt = chars_format::general) noexcept
748 __extension__ __ieee128 __val;
749 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
750 if (__res.ec == errc{})
751 __value = _Float128(__val);
756 from_chars(const char* __first, const char* __last, _Float128& __value,
757 chars_format __fmt = chars_format::general) noexcept;
761#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
762 && defined(__cpp_lib_to_chars)
763 inline from_chars_result
764 from_chars(const char* __first, const char* __last,
765 __gnu_cxx::__bfloat16_t & __value,
766 chars_format __fmt = chars_format::general) noexcept
769 from_chars_result __res
770 = __from_chars_bfloat16_t(__first, __last, __val, __fmt);
771 if (__res.ec == errc{})
772 __value = __gnu_cxx::__bfloat16_t(__val);
778#if defined __cpp_lib_to_chars
779 // Floating-point std::to_chars
781 // Overloads for float.
782 to_chars_result to_chars(char* __first, char* __last, float __value) noexcept;
783 to_chars_result to_chars(char* __first, char* __last, float __value,
784 chars_format __fmt) noexcept;
785 to_chars_result to_chars(char* __first, char* __last, float __value,
786 chars_format __fmt, int __precision) noexcept;
788 // Overloads for double.
789 to_chars_result to_chars(char* __first, char* __last, double __value) noexcept;
790 to_chars_result to_chars(char* __first, char* __last, double __value,
791 chars_format __fmt) noexcept;
792 to_chars_result to_chars(char* __first, char* __last, double __value,
793 chars_format __fmt, int __precision) noexcept;
795 // Overloads for long double.
796 to_chars_result to_chars(char* __first, char* __last, long double __value)
798 to_chars_result to_chars(char* __first, char* __last, long double __value,
799 chars_format __fmt) noexcept;
800 to_chars_result to_chars(char* __first, char* __last, long double __value,
801 chars_format __fmt, int __precision) noexcept;
803 // Library routines for 16-bit extended floating point formats
804 // using float as interchange format.
805 to_chars_result __to_chars_float16_t(char* __first, char* __last,
807 chars_format __fmt) noexcept;
808 to_chars_result __to_chars_bfloat16_t(char* __first, char* __last,
810 chars_format __fmt) noexcept;
812#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
813 inline to_chars_result
814 to_chars(char* __first, char* __last, _Float16 __value) noexcept
816 return __to_chars_float16_t(__first, __last, float(__value),
819 inline to_chars_result
820 to_chars(char* __first, char* __last, _Float16 __value,
821 chars_format __fmt) noexcept
822 { return __to_chars_float16_t(__first, __last, float(__value), __fmt); }
823 inline to_chars_result
824 to_chars(char* __first, char* __last, _Float16 __value,
825 chars_format __fmt, int __precision) noexcept
826 { return to_chars(__first, __last, float(__value), __fmt, __precision); }
829#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
830 inline to_chars_result
831 to_chars(char* __first, char* __last, _Float32 __value) noexcept
832 { return to_chars(__first, __last, float(__value)); }
833 inline to_chars_result
834 to_chars(char* __first, char* __last, _Float32 __value,
835 chars_format __fmt) noexcept
836 { return to_chars(__first, __last, float(__value), __fmt); }
837 inline to_chars_result
838 to_chars(char* __first, char* __last, _Float32 __value,
839 chars_format __fmt, int __precision) noexcept
840 { return to_chars(__first, __last, float(__value), __fmt, __precision); }
843#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
844 inline to_chars_result
845 to_chars(char* __first, char* __last, _Float64 __value) noexcept
846 { return to_chars(__first, __last, double(__value)); }
847 inline to_chars_result
848 to_chars(char* __first, char* __last, _Float64 __value,
849 chars_format __fmt) noexcept
850 { return to_chars(__first, __last, double(__value), __fmt); }
851 inline to_chars_result
852 to_chars(char* __first, char* __last, _Float64 __value,
853 chars_format __fmt, int __precision) noexcept
854 { return to_chars(__first, __last, double(__value), __fmt, __precision); }
857#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
858 inline to_chars_result
859 to_chars(char* __first, char* __last, _Float128 __value) noexcept
860 { return to_chars(__first, __last, static_cast<long double>(__value)); }
861 inline to_chars_result
862 to_chars(char* __first, char* __last, _Float128 __value,
863 chars_format __fmt) noexcept
865 return to_chars(__first, __last, static_cast<long double>(__value), __fmt);
867 inline to_chars_result
868 to_chars(char* __first, char* __last, _Float128 __value,
869 chars_format __fmt, int __precision) noexcept
871 return to_chars(__first, __last, static_cast<long double>(__value), __fmt,
874#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
875#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
876 __extension__ to_chars_result
877 to_chars(char* __first, char* __last, __float128 __value) noexcept;
878 __extension__ to_chars_result
879 to_chars(char* __first, char* __last, __float128 __value,
880 chars_format __fmt) noexcept;
881 __extension__ to_chars_result
882 to_chars(char* __first, char* __last, __float128 __value,
883 chars_format __fmt, int __precision) noexcept;
885 inline to_chars_result
886 to_chars(char* __first, char* __last, _Float128 __value) noexcept
888 return __extension__ to_chars(__first, __last,
889 static_cast<__float128>(__value));
891 inline to_chars_result
892 to_chars(char* __first, char* __last, _Float128 __value,
893 chars_format __fmt) noexcept
896 return __extension__ to_chars(__first, __last,
897 static_cast<__float128>(__value), __fmt);
899 inline to_chars_result
900 to_chars(char* __first, char* __last, _Float128 __value,
901 chars_format __fmt, int __precision) noexcept
904 return __extension__ to_chars(__first, __last,
905 static_cast<__float128>(__value), __fmt,
909 to_chars_result to_chars(char* __first, char* __last, _Float128 __value)
911 to_chars_result to_chars(char* __first, char* __last, _Float128 __value,
912 chars_format __fmt) noexcept;
913 to_chars_result to_chars(char* __first, char* __last, _Float128 __value,
914 chars_format __fmt, int __precision) noexcept;
918#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
919 inline to_chars_result
920 to_chars(char* __first, char* __last,
921 __gnu_cxx::__bfloat16_t __value) noexcept
923 return __to_chars_bfloat16_t(__first, __last, float(__value),
926 inline to_chars_result
927 to_chars(char* __first, char* __last, __gnu_cxx::__bfloat16_t __value,
928 chars_format __fmt) noexcept
929 { return __to_chars_bfloat16_t(__first, __last, float(__value), __fmt); }
930 inline to_chars_result
931 to_chars(char* __first, char* __last, __gnu_cxx::__bfloat16_t __value,
932 chars_format __fmt, int __precision) noexcept
933 { return to_chars(__first, __last, float(__value), __fmt, __precision); }
937_GLIBCXX_END_NAMESPACE_VERSION
940#pragma GCC diagnostic pop
941#endif // _GLIBCXX_CHARCONV