From patchwork Thu May 11 11:56:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 92488 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4308577vqo; Thu, 11 May 2023 04:58:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Nh7lgzbL0+4cfJ2zKGI1lltMtVkM9ZEgAgJBxR9yreE9lX6zqBlVCq0MP1v/HOhx1cq6I X-Received: by 2002:a17:907:9345:b0:958:4870:8d09 with SMTP id bv5-20020a170907934500b0095848708d09mr19519082ejc.37.1683806289036; Thu, 11 May 2023 04:58:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683806289; cv=none; d=google.com; s=arc-20160816; b=G91ZvMPfSBv4wwCNTny5UlZkU8LUJ7BD2ZXDBvDC1TZUMTbQvo4S03yKgcBuoV+/D7 is8o1uQRlAbmjDpyMsxmYFtqPx+lCBO5eTy99hbwBdeOL7ZQqSPzhtC9RiGd1RmfUDgQ 66gf7UitzATwBrvdOSdg13I4pjb36SuGOIFAB3jDzvzZuyZslYNnQlhyfYKKUfFwPTXe E6QlOHWY5/YNzu0FkfdHbOb3MlOZyrE1LFYbtgyD6v8Gk5nr9RjnI/pdprYoeMh6iVRR Nxdq6x1UpGpXB6hILaRk5Dxy2t5+iQ03Hy8k270NpUJhh8dR38tMwbKoL7dBHD9Jk99t gJOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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=reBVhG75NwDMbMFoPZD4GbNW4PbhehVCBc9oN+3wVDM=; b=ObmhMG0J2ZCzO97UVtZBj33yWZd7S2d0MDZOyR9O/xNWqEK2LBTeXesz0i0p9gYBi/ xe1zfOMSG70hVnwq1Pa+hA1VQP94s79JWQFCUuI+lBZv40UkH50U31OEQ/Gio9ARZ4Pc vHz8fCb4Old7uQEtQAp6y2BYBUrimyPMGtpIa6qN5vofrKxIYuKnHyV2lVzLtJpPTN/R 0u6bJX+88QBi131kUGzNvD2cP5VgYkNIi5FeqtihfI2t3E93+r1iy8cBPQQ+8wvjRpbK X8LEXo7xPGvVI8CjnZWeAoDetwjA6HjtVUZDTfSnn+0Fm009v9quy4mL+G3MhwbngSWc oq2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="pkYB+HG/"; 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 q14-20020aa7d44e000000b0050bc97f47b9si4675429edr.148.2023.05.11.04.58.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 May 2023 04:58:09 -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="pkYB+HG/"; 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 BC722385B534 for ; Thu, 11 May 2023 11:57:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC722385B534 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683806262; bh=reBVhG75NwDMbMFoPZD4GbNW4PbhehVCBc9oN+3wVDM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pkYB+HG/68w7ou0l6L5YWY79CRGJnnuNdyP4RawhNL3YChtCgD2++a/IgwyglDnVT YpyBh6bMyH259gLwz0klqPGF2gaJ6XwMjy5cBf+PyDj5B+EzkyKZUr7dRAwlWxDJOR DK7sC2Bbf+OqpLu4y1eWY5E3MRAKRHlKDEsQNFB4= 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 6334A3858C83 for ; Thu, 11 May 2023 11:56:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6334A3858C83 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-244-h5WJGbXQN06Ka5NhpUinmg-1; Thu, 11 May 2023 07:56:53 -0400 X-MC-Unique: h5WJGbXQN06Ka5NhpUinmg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 65DAC3C10245; Thu, 11 May 2023 11:56:53 +0000 (UTC) Received: from localhost (unknown [10.42.28.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C96FC15BA0; Thu, 11 May 2023 11:56:52 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::abs(__float128) for -NaN and -0.0 [PR109758] Date: Thu, 11 May 2023 12:56:36 +0100 Message-Id: <20230511115636.964614-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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, T_SCC_BODY_TEXT_LINE, URI_HEX 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: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely 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?1765598863266244666?= X-GMAIL-MSGID: =?utf-8?q?1765598863266244666?= Tested powerpc64le-linux (both -mabi={ibm,ieee}longdouble options) and x86_64-linux. Pushed to trunk. -- >8 -- The current implementation of this non-standard overload of std::abs incorrectly returns a negative value for negative NaNs and negative zero, because x < 0 is false in both cases. Use fabsl(long double) or fabsf128(_Float128) if those do the right thing. Otherwise, use __builtin_signbit(x) instead of x < 0 to detect negative inputs. This assumes that __builtin_signbit handles __float128 correctly, but that seems to be true for all of GCC, clang and icc. libstdc++-v3/ChangeLog: PR libstdc++/109758 * include/bits/std_abs.h (abs(__float128)): Handle negative NaN and negative zero correctly. * testsuite/26_numerics/headers/cmath/109758.cc: New test. --- libstdc++-v3/include/bits/std_abs.h | 13 ++++- .../26_numerics/headers/cmath/109758.cc | 52 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc diff --git a/libstdc++-v3/include/bits/std_abs.h b/libstdc++-v3/include/bits/std_abs.h index 1bb7ffbc2da..c70c8e4edcf 100644 --- a/libstdc++-v3/include/bits/std_abs.h +++ b/libstdc++-v3/include/bits/std_abs.h @@ -135,11 +135,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __extension__ inline _GLIBCXX_CONSTEXPR __float128 abs(__float128 __x) - { return __x < 0 ? -__x : __x; } + { +#if defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) + return __builtin_fabsl(__x); +#elif defined(_GLIBCXX_HAVE_FLOAT128_MATH) + return __builtin_fabsf128(__x); +#else + // Assume that __builtin_signbit works for __float128. + return __builtin_signbit(__x) ? -__x : __x; +#endif + } #endif _GLIBCXX_END_NAMESPACE_VERSION } // namespace -} // extern "C"++" +} // extern "C++" #endif // _GLIBCXX_BITS_STD_ABS_H diff --git a/libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc b/libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc new file mode 100644 index 00000000000..c9716d3d372 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc @@ -0,0 +1,52 @@ +// { dg-do run } +// PR libstdc++/109758 std::abs(__float128) doesn't support NaN + +#include +#include + +#if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128) +void +test_nan() +{ + __float128 nan = __builtin_nanl(""); + VERIFY( !__builtin_signbit(std::abs(nan)) ); + VERIFY( !__builtin_signbit(std::abs(-nan)) ); +} + +void +test_zero() +{ + __float128 zero = 0.0; + VERIFY( !__builtin_signbit(std::abs(zero)) ); + VERIFY( !__builtin_signbit(std::abs(zero * -2.0)) ); +} + +void +test_neg() +{ + VERIFY( std::abs((__float128)-1.0) == -1.0 ); + VERIFY( std::abs((__float128)-2e9) == -2e9 ); + VERIFY( std::abs((__float128)-3e-4) == 3e-4 ); +} + +void +test_inf() +{ + __float128 inf = __builtin_huge_vall(); + VERIFY( std::abs(inf) == inf ); + VERIFY( std::abs(-inf) == inf ); +} + +#if __cplusplus >= 201103L +static_assert( std::abs((__float128)-1.0) == (__float128)1.0, + "std::abs(__float128) is usable in constant expressions" ); +#endif + +int main() +{ + test_nan(); + test_zero(); +} +#else +int main() { } +#endif