From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 ED7053858C55 for ; Wed, 2 Nov 2022 11:20:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ED7053858C55 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667388049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NRIJDckFri9B1C/YQazLq4SMWOPVpTblKsnyOMh7Yh8=; b=g9x1KlCPN9qs/jjR93mKGjNGQNcU8BwxfIbz42AWLcvE0QYkEZmjaKs2VmMcqYhP0GFOvs 7ThZBfetEKblLa1wb9r+WsRmMMHKz1Qx9NpgX6n3UmgkJs73Q6Vajcmuzsguuj3aTdUqQe IMCy4p2DoNfJE2VJ8hxsZPpvm77XRe4= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-450-MUb0fPX2PiyMnzbeadeGCw-1; Wed, 02 Nov 2022 07:20:48 -0400 X-MC-Unique: MUb0fPX2PiyMnzbeadeGCw-1 Received: by mail-ej1-f72.google.com with SMTP id qk31-20020a1709077f9f00b00791a3e02c80so9833921ejc.21 for ; Wed, 02 Nov 2022 04:20:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NRIJDckFri9B1C/YQazLq4SMWOPVpTblKsnyOMh7Yh8=; b=fxGc1EkxpqGBLGcqrbDh4NUZiqG6d2GaI23dPcNffsxo3sA2A9i6fYxXO0/gn2ihWb q5a6Cm+oBKBgTKg45mBiRw6PTgUB7zGO0IWR3H/eH2hG+ZATYptlkWJ9OjPzzwVPyCFO i2GjGMyAhtgnsipuTzg0ZH8ITqDG4aE6CGuh8Iz1Ic2wHQ6gvH1kaIJbQW3mmI0I3dRZ TBN1rz5MgJ+6UZNWVCQOc8kaQg6zTdDHclA5b7QGvBhUxt/S0f/6TFme7jObm8w5lILk sIKacLYk4QwiGnHk42zFV7d8IsBfr3d4L5s0euOMnD2mIxJPPrbK9s/O3ns85axnt5+M tYaQ== X-Gm-Message-State: ACrzQf3gdonMpfXAnN1EzY3x8yVPN4HIFHp97NZtOTUY119khaqvde86 EPkhkSGOKMLibS+uhOjIEfw4kXqaov5czcEmve39D9SRdDoYOBmZJOlaPs2OKKdqdPrXP9TZ+LZ ngDksoP0bDsMJrl1CeEMl48OFEiMBgA0= X-Received: by 2002:a17:907:3f95:b0:7a2:36c7:31bd with SMTP id hr21-20020a1709073f9500b007a236c731bdmr23684996ejc.353.1667388047571; Wed, 02 Nov 2022 04:20:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7gpk9pM/r/8TT4Ehw6T4/21hFsdy8VNygMiAgdoe9uhAgrq0FBEUm7tkDApRRW8gAHKrxuIDZMPbhU+ZeByS4= X-Received: by 2002:a17:907:3f95:b0:7a2:36c7:31bd with SMTP id hr21-20020a1709073f9500b007a236c731bdmr23684971ejc.353.1667388047356; Wed, 02 Nov 2022 04:20:47 -0700 (PDT) MIME-Version: 1.0 References: <20221101213029.940043-1-ppalka@redhat.com> In-Reply-To: <20221101213029.940043-1-ppalka@redhat.com> From: Jonathan Wakely Date: Wed, 2 Nov 2022 11:20:36 +0000 Message-ID: Subject: Re: [PATCH] libstdc++: Fix ERANGE behavior for fallback FP std::from_chars To: Patrick Palka Cc: gcc-patches@gcc.gnu.org, jakub@redhat.com, libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.9 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, 1 Nov 2022 at 21:30, Patrick Palka via Libstdc++ 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 >