From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 8B46D3858CDA; Wed, 2 Nov 2022 09:08:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B46D3858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667380098; bh=lldVwe9uCWLygoHai/mwWJV2qF0T6Aj73B55fXk2fjY=; h=From:To:Subject:Date:From; b=rvRQJV8RWmDU9vO0ZqbHWeyiI22CEvVSmi0cC5vLGiaMsbz8dG2b/Q12MYvR+CRNY UzUEXxYPVmXlhewcmJPDAyWIJQHbIfSLse2bob4qCX8m3ZVSX0tBnQ16HyUcDiw0iK /iZZOYp8OUEviUCLtkCeX4FZE+Lkj4XFPOiWCxdg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-3604] libstdc++: Shortest denormal hex std::to_chars X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: eaba55ffef961c28f6a15d845a4d6b77b8a8bab1 X-Git-Newrev: b65e391da94476fbc5c8947c81af61aa74941dc8 Message-Id: <20221102090818.8B46D3858CDA@sourceware.org> Date: Wed, 2 Nov 2022 09:08:18 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b65e391da94476fbc5c8947c81af61aa74941dc8 commit r13-3604-gb65e391da94476fbc5c8947c81af61aa74941dc8 Author: Jakub Jelinek Date: Wed Nov 2 10:06:24 2022 +0100 libstdc++: Shortest denormal hex std::to_chars On Fri, Oct 28, 2022 at 12:52:44PM -0400, Patrick Palka wrote: > > The following patch on top of > > https://gcc.gnu.org/pipermail/libstdc++/2022-October/054849.html > > adds std::{,b}float16_t support for std::to_chars. > > When precision is specified (or for std::bfloat16_t for hex mode even if not), > > I believe we can just use the std::to_chars float (when float is mode > > compatible with std::float32_t) overloads, both formats are proper subsets > > of std::float32_t. > > Unfortunately when precision is not specified and we are supposed to emit > > shortest string, the std::{,b}float16_t strings are usually much shorter. > > E.g. 1.e7p-14f16 shortest fixed representation is > > 0.0001161 and shortest scientific representation is > > 1.161e-04 while 1.e7p-14f32 (same number promoted to std::float32_t) > > 0.00011610985 and > > 1.1610985e-04. > > Similarly for 1.38p-112bf16, > > 0.000000000000000000000000000000000235 > > 2.35e-34 vs. 1.38p-112f32 > > 0.00000000000000000000000000000000023472271 > > 2.3472271e-34 > > For std::float16_t there are differences even in the shortest hex, say: > > 0.01p-14 vs. 1p-22 > > but only for denormal std::float16_t values (where all std::float16_t > > denormals converted to std::float32_t are normal), __FLT16_MIN__ and > > everything larger in absolute value than that is the same. Unless > > that is a bug and we should try to discover shorter representations > > even for denormals... > > IIRC for hex formatting of denormals I opted to be consistent with how > glibc printf formats them, instead of outputting the truly shortest > form. > > I wouldn't be against using the float32 overloads even for shortest hex > formatting of float16. The output is shorter but equivalent so it > shouldn't cause any problems. The following patch changes the behavior of the shortest hex denormals, such that they are printed like normals (so for has_implicit_leading_bit with 1p-149 instead of 0.000002p-126 etc., otherwise (Intel extended) with the leading digit before dot being [89abcdef]). I think for all the supported format it is never longer, it can be equal length e.g. for 0.fffffep-126 vs. 1.fffffcp-127 but fortunately no largest subnormal in any format has the unbiased exponent like -9, -99, -999, -9999 because then it would be longer and often it is shorter, sometimes much shorter. For the cases with precision it keeps the handling as is. While for !has_implicit_leading_bit we for normals or with this patch even denormals have really shortest representation, for other formats we sometimes do not, but this patch doesn't deal with that (we always use 1.NNN while we could use 1.NNN up to f.NNN and by that shortening by the last hexit if the last hexit doesn't have least significant bit set and unbiased exponent is not -9, -99, -999 or -9999. 2022-11-02 Jakub Jelinek * src/c++17/floating_to_chars.cc (__floating_to_chars_hex): Drop const from unbiased_exponent. Canonicalize denormals such that they have the leading bit set by shifting effective mantissa up and decreasing unbiased_exponent. (__floating_to_chars_shortest): Don't instantiate __floating_to_chars_hex for float16_t either and use float instead. * testsuite/20_util/to_chars/float.cc (float_to_chars_test_cases): Adjust testcases for shortest hex denormals. * testsuite/20_util/to_chars/double.cc (double_to_chars_test_cases): Likewise. Diff: --- libstdc++-v3/src/c++17/floating_to_chars.cc | 21 +++++++++++++++++---- libstdc++-v3/testsuite/20_util/to_chars/double.cc | 4 ++-- libstdc++-v3/testsuite/20_util/to_chars/float.cc | 4 ++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/src/c++17/floating_to_chars.cc b/libstdc++-v3/src/c++17/floating_to_chars.cc index 43e1ff35119..a253ee42416 100644 --- a/libstdc++-v3/src/c++17/floating_to_chars.cc +++ b/libstdc++-v3/src/c++17/floating_to_chars.cc @@ -844,9 +844,9 @@ template const bool is_normal_number = (biased_exponent != 0); // Calculate the unbiased exponent. - const int32_t unbiased_exponent = (is_normal_number - ? biased_exponent - exponent_bias - : 1 - exponent_bias); + int32_t unbiased_exponent = (is_normal_number + ? biased_exponent - exponent_bias + : 1 - exponent_bias); // Shift the mantissa so that its bitwidth is a multiple of 4. constexpr unsigned rounded_mantissa_bits = (mantissa_bits + 3) / 4 * 4; @@ -863,6 +863,16 @@ template __glibcxx_assert(effective_mantissa & (mantissa_t{1} << (mantissa_bits - 1u))); } + else if (!precision.has_value() && effective_mantissa) + { + // 1.8p-23 is shorter than 0.00cp-14, so if precision is + // omitted, try to canonicalize denormals such that they + // have the leading bit set. + int width = __bit_width(effective_mantissa); + int shift = rounded_mantissa_bits - width + has_implicit_leading_bit; + unbiased_exponent -= shift; + effective_mantissa <<= shift; + } // Compute the shortest precision needed to print this value exactly, // disregarding trailing zeros. @@ -1061,7 +1071,10 @@ template // std::bfloat16_t has the same exponent range as std::float32_t // and so we can avoid instantiation of __floating_to_chars_hex // for bfloat16_t. Shortest hex will be the same as for float. - if constexpr (is_same_v) + // When we print shortest form even for denormals, we can do it + // for std::float16_t as well. + if constexpr (is_same_v + || is_same_v) return __floating_to_chars_hex(first, last, value.x, nullopt); else return __floating_to_chars_hex(first, last, value, nullopt); diff --git a/libstdc++-v3/testsuite/20_util/to_chars/double.cc b/libstdc++-v3/testsuite/20_util/to_chars/double.cc index 9a965d28053..ac9a43099a2 100644 --- a/libstdc++-v3/testsuite/20_util/to_chars/double.cc +++ b/libstdc++-v3/testsuite/20_util/to_chars/double.cc @@ -2821,8 +2821,8 @@ inline constexpr double_to_chars_testcase double_to_chars_test_cases[] = { // Test hexfloat corner cases. {0x1.728p+0, chars_format::hex, "1.728p+0"}, // instead of "2.e5p-1" - {0x0.0000000000001p-1022, chars_format::hex, "0.0000000000001p-1022"}, // instead of "1p-1074", min subnormal - {0x0.fffffffffffffp-1022, chars_format::hex, "0.fffffffffffffp-1022"}, // max subnormal + {0x0.0000000000001p-1022, chars_format::hex, "1p-1074"}, // min subnormal + {0x0.fffffffffffffp-1022, chars_format::hex, "1.ffffffffffffep-1023"}, // max subnormal {0x1p-1022, chars_format::hex, "1p-1022"}, // min normal {0x1.fffffffffffffp+1023, chars_format::hex, "1.fffffffffffffp+1023"}, // max normal diff --git a/libstdc++-v3/testsuite/20_util/to_chars/float.cc b/libstdc++-v3/testsuite/20_util/to_chars/float.cc index bcbd78f0987..5a5de63eae5 100644 --- a/libstdc++-v3/testsuite/20_util/to_chars/float.cc +++ b/libstdc++-v3/testsuite/20_util/to_chars/float.cc @@ -521,8 +521,8 @@ inline constexpr float_to_chars_testcase float_to_chars_test_cases[] = { // Test hexfloat corner cases. {0x1.728p+0f, chars_format::hex, "1.728p+0"}, // instead of "2.e5p-1" - {0x0.000002p-126f, chars_format::hex, "0.000002p-126"}, // instead of "1p-149", min subnormal - {0x0.fffffep-126f, chars_format::hex, "0.fffffep-126"}, // max subnormal + {0x0.000002p-126f, chars_format::hex, "1p-149"}, // min subnormal + {0x0.fffffep-126f, chars_format::hex, "1.fffffcp-127"}, // max subnormal {0x1p-126f, chars_format::hex, "1p-126"}, // min normal {0x1.fffffep+127f, chars_format::hex, "1.fffffep+127"}, // max normal