Message ID | 20221101213029.940043-1-ppalka@redhat.com |
---|---|
State | Accepted |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3219495wru; Tue, 1 Nov 2022 14:31:41 -0700 (PDT) X-Google-Smtp-Source: AMsMyM53DxS3aj8V8BifEgQPlVVuYhl3GlaE4MWLGUOn5Mox6GEIpx08WSRglCg9hnD9RsY1Lp0z X-Received: by 2002:a17:907:980e:b0:78d:b6ea:25b3 with SMTP id ji14-20020a170907980e00b0078db6ea25b3mr19907894ejc.98.1667338301666; Tue, 01 Nov 2022 14:31:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667338301; cv=none; d=google.com; s=arc-20160816; b=M6duPRL2AUzM1SPwKz9d0nCYAqo89z5yhz+RrqayvU/mgtO56ZMzKCW+phx4fs5h+2 tRMEBXD3lU+RabJ5OvaXMWs2xQ+hjkeNRmR6bipQXJlD95OSgs0eoOrRaBcIDo25ylWJ E1mH8Z9w/DMz/FTJcktSI61KTdiJhpnSELhisBDRXeeiuebxG8Yg4nOKtYLE2vu11md9 oC6O0yNLAsx0Cmj5IMzQssurmmV6kQ2mQ33FBlzV3iRCXTTcdG5i4rpE3dbJFQyn9d9f dj6sxAyqBOZ2beswl6EjJwQLl1XtC1IIPEImY8tHqQAWSG27cSXE6RyASV93XXrt4KAi JgdQ== 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-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=B1oGOaPsuPJ+XeBkLeSxO0Dp+xhL7nHqfc4x41Rey6k=; b=TYThLHqLyuOC676YWwm5V5a2pev4El0BvJhBV5eDtjWvE8WKkKj25/749DkvUEKJDS YjcjFsfgDstKcF9yWTpv4bBOfkYktOq7H3GhJakYRPk3m/sm2KRMIxvIgrGvv7j919rR l+fTRn/CAdwYXX1AyAQPvUrHDTHOo9c3N4kdQ7OCsmmifbLjJKIAq6rMlcgRXpaQ1soK omJATHA3vGvzfdVvz8un4WI0xcHYfOyPb40VizGwtkmnx994gdYV1rTlJOew3nzIFEev ayPz05FPsfKl/6beH/zwYO2d4+mBqStWd+uwNmRQvakRo/oSo3t1rM4+TbhRDTnERObD fskg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JMPB7QEp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u2-20020a170906408200b0078da30cb4bfsi9985383ejj.428.2022.11.01.14.31.41 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 14:31:41 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JMPB7QEp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 799383857BBF for <ouuuleilei@gmail.com>; Tue, 1 Nov 2022 21:31:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 799383857BBF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667338289; bh=B1oGOaPsuPJ+XeBkLeSxO0Dp+xhL7nHqfc4x41Rey6k=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=JMPB7QEpY+IC6N6/zBsLO8IFzlgO3rgH8GO3PdsymayEZUa+Gokxlt9NcuaqONdym RfSimxDkJpemrwz+NHW2y5OKyPRBo2i3JacJDCWtU1J1rQmYaTrxf3LCwEfz2WOlyY M04MXTnEz31c/UPCo0fD1Jh3IhN+VUI0QTeCQR5c= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 786283858D28 for <gcc-patches@gcc.gnu.org>; Tue, 1 Nov 2022 21:30:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 786283858D28 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-29-kj_GLeBFNamSWskSlXodEA-1; Tue, 01 Nov 2022 17:30:40 -0400 X-MC-Unique: kj_GLeBFNamSWskSlXodEA-1 Received: by mail-qt1-f198.google.com with SMTP id gc12-20020a05622a59cc00b003a5444280e1so76116qtb.13 for <gcc-patches@gcc.gnu.org>; Tue, 01 Nov 2022 14:30:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=B1oGOaPsuPJ+XeBkLeSxO0Dp+xhL7nHqfc4x41Rey6k=; b=gio0ms+moHKqZ8rxrnGGRXRsCJQOUXtoWQEWzMktrfN2Py7V+N05pesEPGoW2XMb+I iUf7dCaRsxn9QL/QKOkCTea4gQitrL/weXn8jke4Rla3pi4gG0oIqA/vh6L8dXn1wwcF w4bxJe8C9bQSAckimbgBCFxbe6PH/obiUWNx0kXTKvYwfZZQ6NAkE28MfmgQwVmoJl66 hRZbx4Im5kj4GfH/yjX8xz/dJSDQojMweaBs6kObqm/MB9x2NqHCzH/TXjDKgAt1ysWv TXR9QGhLkeW7DrjCoWQblLVA1MzZwIhhy4+kZ2tSZlKw0gqocaFDp/vVw0hU4gfHfNLO qLBw== X-Gm-Message-State: ACrzQf1d8wLfI0+o+LNLniBbdekGW0tivkknARd+OlDvpxq4x/ujIPoO jugLDA/218V8zzuHh/NKIYYff5k0YrvOQjZ4uMPF71gOFgBCFLS3N3zX/WLyBW2g96hDHci2Z8l A+P6dqN/7fXRsnOZrLC+r0NnjL9SDYefyt+FSEsdGgjHSIuuivMV3U5ie54rU+Lg5oDM= X-Received: by 2002:a37:ad0d:0:b0:6fa:39fa:973f with SMTP id f13-20020a37ad0d000000b006fa39fa973fmr6701594qkm.563.1667338239291; Tue, 01 Nov 2022 14:30:39 -0700 (PDT) X-Received: by 2002:a37:ad0d:0:b0:6fa:39fa:973f with SMTP id f13-20020a37ad0d000000b006fa39fa973fmr6701570qkm.563.1667338238979; Tue, 01 Nov 2022 14:30:38 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id z63-20020a37b042000000b006f9e103260dsm7173419qke.91.2022.11.01.14.30.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 14:30:38 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Fix ERANGE behavior for fallback FP std::from_chars Date: Tue, 1 Nov 2022 17:30:29 -0400 Message-Id: <20221101213029.940043-1-ppalka@redhat.com> X-Mailer: git-send-email 2.38.1.381.gc03801e19c MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Patrick Palka via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Patrick Palka <ppalka@redhat.com> Cc: jakub@redhat.com, libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748330926910964428?= X-GMAIL-MSGID: =?utf-8?q?1748330926910964428?= |
Series |
libstdc++: Fix ERANGE behavior for fallback FP std::from_chars
|
|
Checks
Context | Check | Description |
---|---|---|
snail/gcc-patch-check | success | Github commit url |
Commit Message
Patrick Palka
Nov. 1, 2022, 9:30 p.m. UTC
The fallback implementation of floating-point std::from_chars for e.g. float80 just calls the C library's strtod family of functions. In case of overflow of the parsed result, the behavior of these functions is rigidly specified: If the correct value overflows and default rounding is in effect, plus or minus HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned (according to the return type and sign of the value), and the value of the macro ERANGE is stored in errno. But in case of underflow, implementations are given more leeway: If the result underflows the functions return a value whose magnitude is no greater than the smallest normalized positive number in the return type; whether errno acquires the value ERANGE is implementation-defined. Thus we can (and do) portably detect overflow, but we can't portably detect underflow. However, glibc (and presumably other high-quality C library implementations) will reliably set errno to ERANGE in case of underflow too, and it will also return the nearest denormal number to the parsed result (including zero in case of true underflow). Since we can't be perfect here, this patch takes the best effort approach of assuming a high quality C library implementation that allows us to distinguish between a denormal parsed result and true underflow by inspecting the return value. Tested on x86_64-pc-linux-gnu, does this look OK for trunk? Dunno if we should backport this too. No test because we can't portably test this IIUC. libstdc++-v3/ChangeLog: * src/c++17/floating_from_chars.cc (from_chars_impl): In the ERANGE case, also check for a 0 return value before returning result_out_of_range, occurred, otherwise assume it's a denormal number. --- libstdc++-v3/src/c++17/floating_from_chars.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
Comments
On Tue, 1 Nov 2022 at 21:30, Patrick Palka via Libstdc++ <libstdc++@gcc.gnu.org> wrote: > > The fallback implementation of floating-point std::from_chars for e.g. > float80 just calls the C library's strtod family of functions. In case > of overflow of the parsed result, the behavior of these functions is > rigidly specified: > > If the correct value overflows and default rounding is in effect, plus > or minus HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned (according to > the return type and sign of the value), and the value of the macro > ERANGE is stored in errno. > > But in case of underflow, implementations are given more leeway: > > If the result underflows the functions return a value whose magnitude > is no greater than the smallest normalized positive number in the > return type; whether errno acquires the value ERANGE is > implementation-defined. > > Thus we can (and do) portably detect overflow, but we can't portably > detect underflow. However, glibc (and presumably other high-quality C > library implementations) will reliably set errno to ERANGE in case of > underflow too, and it will also return the nearest denormal number to > the parsed result (including zero in case of true underflow). > > Since we can't be perfect here, this patch takes the best effort > approach of assuming a high quality C library implementation that > allows us to distinguish between a denormal parsed result and true > underflow by inspecting the return value. > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? Dunno OK for trunk. > if we should backport this too. No test because we can't portably > test this IIUC. I think it's worth backporting to 11 and 12 because this is a C++17 feature and that's our default mode since GCC 11. But give it some soak time on trunk first please. > > libstdc++-v3/ChangeLog: > > * src/c++17/floating_from_chars.cc (from_chars_impl): In the > ERANGE case, also check for a 0 return value before returning > result_out_of_range, occurred, otherwise assume it's a denormal > number. > --- > libstdc++-v3/src/c++17/floating_from_chars.cc | 7 ++++++- > 1 file changed, 6 insertions(+), 1 deletion(-) > > diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc > index a25ac5ce3aa..939c751f861 100644 > --- a/libstdc++-v3/src/c++17/floating_from_chars.cc > +++ b/libstdc++-v3/src/c++17/floating_from_chars.cc > @@ -637,8 +637,13 @@ namespace > { > if (__builtin_isinf(tmpval)) // overflow > ec = errc::result_out_of_range; > - else // underflow (LWG 3081 wants to set value = tmpval here) > + else if (tmpval == 0) // underflow (LWG 3081 wants to set value = tmpval here) > ec = errc::result_out_of_range; > + else // denormal value > + { > + value = tmpval; > + ec = errc(); > + } > } > else if (n) > { > -- > 2.38.1.381.gc03801e19c >
diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc index a25ac5ce3aa..939c751f861 100644 --- a/libstdc++-v3/src/c++17/floating_from_chars.cc +++ b/libstdc++-v3/src/c++17/floating_from_chars.cc @@ -637,8 +637,13 @@ namespace { if (__builtin_isinf(tmpval)) // overflow ec = errc::result_out_of_range; - else // underflow (LWG 3081 wants to set value = tmpval here) + else if (tmpval == 0) // underflow (LWG 3081 wants to set value = tmpval here) ec = errc::result_out_of_range; + else // denormal value + { + value = tmpval; + ec = errc(); + } } else if (n) {