gcc/libstdc++-v3/include
Patrick Palka 3c57e69235 libstdc++: Add floating-point std::to_chars implementation
This implements the floating-point std::to_chars overloads for float,
double and long double.  We use the Ryu library to compute the shortest
round-trippable fixed and scientific forms for float, double and long
double.  We also use Ryu for performing explicit-precision fixed and
scientific formatting for float and double. For explicit-precision
formatting for long double we fall back to using printf.  Hexadecimal
formatting for float, double and long double is implemented from
scratch.

The supported long double binary formats are binary64, binary80 (x86
80-bit extended precision), binary128 and ibm128.

Much of the complexity of the implementation is in computing the exact
output length before handing it off to Ryu (which doesn't do bounds
checking).  In some cases it's hard to compute the output length
beforehand, so in these cases we instead compute an upper bound on the
output length and use a sufficiently-sized intermediate buffer only if
necessary.

Another source of complexity is in the general-with-precision formatting
mode, where we need to do zero-trimming of the string returned by Ryu,
and where we also take care to avoid having to format the number through
Ryu a second time when the general formatting mode resolves to fixed
(which we determine by doing a scientific formatting first and
inspecting the scientific exponent).  We avoid going through Ryu twice
by instead transforming the scientific form to the corresponding fixed
form via in-place string manipulation.

This implementation is non-conforming in a couple of ways:

1. For the shortest hexadecimal formatting, we currently follow the
   Microsoft implementation's decision to be consistent with the
   output of printf's '%a' specifier at the expense of sometimes not
   printing the shortest representation.  For example, the shortest hex
   form for the number 1.08p+0 is 2.1p-1, but we output the former
   instead of the latter, as does printf.

2. The Ryu routine generic_binary_to_decimal that we use for performing
   shortest formatting for large floating point types is implemented
   using the __int128 type, but some targets with a large long double
   type lack __int128 (e.g. i686), so we can't perform shortest
   formatting of long double on such targets through Ryu.  As a
   temporary stopgap this patch makes the long double to_chars overloads
   just dispatch to the double overloads on these targets, which means
   we lose precision in the output.  (We could potentially fix this by
   writing a specialized version of Ryu's generic_binary_to_decimal
   routine that uses uint64_t instead of __int128.)  [Though I wonder if
   there's a better way to work around the lack of __int128 on i686
   specifically?]

3. Our shortest formatting for __ibm128 doesn't guarantee the round-trip
   property if the difference between the high- and low-order exponent
   is large.  This is because we treat __ibm128 as if it has a
   contiguous 105-bit mantissa by merging the mantissas of the high-
   and low-order parts (using code extracted from glibc), so we
   potentially lose precision from the low-order part.  This seems to be
   consistent with how glibc printf formats __ibm128.

libstdc++-v3/ChangeLog:

	* config/abi/pre/gnu.ver: Add new exports.
	* include/std/charconv (to_chars): Declare the floating-point
	overloads for float, double and long double.
	* src/c++17/Makefile.am (sources): Add floating_to_chars.cc.
	* src/c++17/Makefile.in: Regenerate.
	* src/c++17/floating_to_chars.cc: New file.
	(to_chars): Define for float, double and long double.
	* testsuite/20_util/to_chars/long_double.cc: New test.
2020-12-17 23:11:34 -05:00
..
backward libstdc++: Add deprecated attributes to old iostream members 2020-08-19 12:13:23 +01:00
bits libstdc++: Fix preprocessor condition [PR 98344] 2020-12-17 14:03:00 +00:00
c Update copyright years. 2020-01-01 12:51:42 +01:00
c_compatibility Update copyright years. 2020-01-01 12:51:42 +01:00
c_global libstdc++: Add parentheses around assignments used as truth values 2020-09-10 17:09:16 +01:00
c_std Update copyright years. 2020-01-01 12:51:42 +01:00
debug libstdc++: Fix _GLIBCXX_DEBUG mode constexpr compatibility 2020-12-12 18:07:45 +01:00
decimal Update copyright years. 2020-01-01 12:51:42 +01:00
experimental libstdc++: Fix errors from Library Fundamentals TS headers in C++11 [PR 98319] 2020-12-16 13:37:17 +00:00
ext libstdc++: Add C++ runtime support for new 128-bit long double format 2020-12-16 23:25:01 +00:00
parallel libstdc++: Define new C++17 std::search overload for Parallel Mode [PR 94971] 2020-11-04 13:36:32 +00:00
precompiled libstdc++: Add new headers to stdc++.h 2020-11-26 11:25:55 +00:00
pstl libstdc++: Rebase include/pstl to current upstream 2020-10-21 06:11:28 -07:00
std libstdc++: Add floating-point std::to_chars implementation 2020-12-17 23:11:34 -05:00
tr1 libstdc++: Prevent deprecation warnings from <tr1/shared_ptr> 2020-10-29 22:47:22 +00:00
tr2 Update copyright years. 2020-01-01 12:51:42 +01:00
Makefile.am libstdc++: Add C++ runtime support for new 128-bit long double format 2020-12-16 23:25:01 +00:00
Makefile.in libstdc++: Add C++ runtime support for new 128-bit long double format 2020-12-16 23:25:01 +00:00