From patchwork Tue Nov 1 12:18:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 13644 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2925975wru; Tue, 1 Nov 2022 05:21:09 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5xfdfjf2hOTf1swFQmE8NnViyR5UwAremkGdAktBxdkfq/MYAGlF9pPgG1+oqrsoVgnEGZ X-Received: by 2002:aa7:d694:0:b0:461:7638:bde5 with SMTP id d20-20020aa7d694000000b004617638bde5mr19207445edr.105.1667305269492; Tue, 01 Nov 2022 05:21:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667305269; cv=none; d=google.com; s=arc-20160816; b=p+WGg2j7CBJTYcu2IkpNY5kuyvoF3SoKykogRWRnEi2FBlKG+AoUaIzOxWXTCLdpED 4GudqWOoXb2mQo5XMcF/HWkJQtzYbjmFJfYrEcnyxHj1DQvX7lLXE8vXkciMGJYlHuT5 XFVIrtLyY/lTBNu4jRHqXKPpt5RqxuK59pUaK34YEQyApRoDsQ0kGIKZl6RbKvI//9Zl V+h3TvOWAdanrjBOBW/mL7m5BaeUelrHmB1RYQnFhpcaoL9mNj4DD9dKeGihY13k1Y/G /iJcmZKTZr5crTOS7PbDBEGoWKpmpDR5gO6xwLCAkzcHVDe31pXM0UHJBuoUyKSo+Faf Cksw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=80C9jzCdC694Ojex8FN44YSSL6jFFlM9naOCJYFUa7o=; b=ZaLfaU4s0YeFPtrQDANXpd4uiaZF9ci2juUxGvdgmebDjOLvRr05XEIGzJGeY0OhTy GKhCBYokRnR2Wj03hXJ/Nof1tqp/pwekhNGYz1x4ji8SFdzPQ8tIwEyuYrTLPRVmWuye p4Bgq45oEA6ccpKzBaeo+pUKGwtnPmV0QGsumGnJOHTZxeaHT8xMjQTHmW7iVc6tFMvB zMenjvdkyHuZ6uKMISxZmR/fE4IXsua7pFuU5O+5aQY9XhZX+jSbtEIm5cRpd6JCpEmW MZBlR/ES6ZVHZQTkgWlLesqkjfsCYyc1N47Yemt9TKeO56ijZp8jcGoryK4fiElpKabf X/LQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ee2U5slo; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id p10-20020a05640210ca00b00460346c1763si10328519edu.449.2022.11.01.05.21.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 05:21:09 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ee2U5slo; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 71DAD3857405 for ; Tue, 1 Nov 2022 12:20:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 71DAD3857405 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667305249; bh=80C9jzCdC694Ojex8FN44YSSL6jFFlM9naOCJYFUa7o=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Ee2U5sloe9VwUlyjpa2ey5281rMpDdYc7r89EdDhVIBkAAo1K8Z9GDED4HrwlNWMu 2iNpCT+9G8fOZHqPN78TeSHFTKY2TN0njekcWQO9xvOEkZJWNrTwIUoOaD12oMRE9s a9gkc5xAqbrG0TREJ4pjXdQFzMfcijElw1gGaiZU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 008DF3858424 for ; Tue, 1 Nov 2022 12:18:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 008DF3858424 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-448-TM32W0MbPzeWculaoHBOtQ-1; Tue, 01 Nov 2022 08:18:09 -0400 X-MC-Unique: TM32W0MbPzeWculaoHBOtQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8B6AA833A06; Tue, 1 Nov 2022 12:18:09 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.252]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 47E9940C6EC4; Tue, 1 Nov 2022 12:18:09 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2A1CI6ZN172669 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 1 Nov 2022 13:18:07 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2A1CI5Tl172668; Tue, 1 Nov 2022 13:18:05 +0100 Date: Tue, 1 Nov 2022 13:18:05 +0100 To: Patrick Palka Subject: [PATCH] libstdc++: Shortest denormal hex std::to_chars Message-ID: References: <4ba36955-3f9a-00c5-c406-45821ec2a4db@idea> MIME-Version: 1.0 In-Reply-To: <4ba36955-3f9a-00c5-c406-45821ec2a4db@idea> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: Jonathan Wakely , libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748296290122580631?= X-GMAIL-MSGID: =?utf-8?q?1748296290122580631?= 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. Tested on x86_64-linux (on top of the 3 to/from_chars {,b}float16_t patches). 2022-11-01 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. Jakub --- libstdc++-v3/src/c++17/floating_to_chars.cc.jj 2022-10-31 22:20:35.881121902 +0100 +++ libstdc++-v3/src/c++17/floating_to_chars.cc 2022-11-01 12:16:14.352652455 +0100 @@ -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); --- libstdc++-v3/testsuite/20_util/to_chars/float.cc.jj 2022-01-11 22:31:41.605755528 +0100 +++ libstdc++-v3/testsuite/20_util/to_chars/float.cc 2022-11-01 12:34:21.370882443 +0100 @@ -521,8 +521,8 @@ inline constexpr float_to_chars_testcase // 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 --- libstdc++-v3/testsuite/20_util/to_chars/double.cc.jj 2022-01-11 22:31:41.604755542 +0100 +++ libstdc++-v3/testsuite/20_util/to_chars/double.cc 2022-11-01 12:42:39.753112522 +0100 @@ -2821,8 +2821,8 @@ inline constexpr double_to_chars_testcas // 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