From patchwork Fri Aug 2 10:30:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1968293 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ijWNYgJ0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wb2JV0CmJz1yZv for ; Fri, 2 Aug 2024 20:34:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 438D73858C5F for ; Fri, 2 Aug 2024 10:34:32 +0000 (GMT) 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 ESMTP id 9D102385840C for ; Fri, 2 Aug 2024 10:33:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9D102385840C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9D102385840C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722594818; cv=none; b=tNoZT2pNG713Ef3qPGypW/nSCt8D0szEJBY1HBhmUTi9gULy92ohDTrNf9K/YHWKK3WYCbUCbNLlbuPpRyM2R90zkGTOlC99foxbI1ryzdURBbw8ZYfbds2AGKnxT1xUgmu+obE4/H4+vWLLzCiWO/ITYyAV1rJxyKFLMBrjKsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722594818; c=relaxed/simple; bh=RG0IIX5Beq/p54JsiJj7mZpvb8yWUsfnv/qXalN7Ulo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=mBPhe/RdYNCQaHy0u0rsEuwLtq/yDMG9cPDLREVkEn3TpHjQk8LMabsghCqemvV66pBT6okDUbYKaQ1+9hfCx1y6aClu/AcqRW/YWg8kX2IoT3a7vrtdULNdBk8ru25w+A5oW2ZyUTw6BsfYXgUg8eoNTtnnSM5BEmCAxzWkUCk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722594816; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LeP91Cwv1Ag3Mlqm2FIozkxjzz/OjQ2AOimLd9Y7l64=; b=ijWNYgJ0CVvo1mGWGRNp8lWwpp1XwqpwUXbVpabftfg/TKRwFh8xyZyI6TjTko9AqaeJ35 cERxqoZT0h2JCYP3TvJM/0mWo/AlcPnpJdZBKMUQYv0ItZ4lMEsxHv3cGG6hutIJPOJBYq ISWEgkd3ecCvgUka2jbcuUQyiwjh4UU= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-621-YZ2_PXGHM1u_V6lEWYjseQ-1; Fri, 02 Aug 2024 06:33:31 -0400 X-MC-Unique: YZ2_PXGHM1u_V6lEWYjseQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7EDE41955BF1; Fri, 2 Aug 2024 10:33:29 +0000 (UTC) Received: from localhost (unknown [10.42.28.21]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6C51219560B2; Fri, 2 Aug 2024 10:33:27 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Edwin Lu , Andrew Waterman Subject: [PATCH, v2] libstdc++: Preserve signbit of nan when converting float to double [PR113578] Date: Fri, 2 Aug 2024 11:30:53 +0100 Message-ID: <20240802103325.474858-1-jwakely@redhat.com> In-Reply-To: <20240801213927.388966-6-jwakely@redhat.com> References: <20240801213927.388966-6-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Rivos CI found a problem in the v1 patch (see the PR comments). This should solve it, by using 'constexpr' not _GLIBCXX17_CONSTEXPR. That requires diagnostic pragmas to suppress -Wc++17-extensions and isn't possible at all in C++98 mode, so just use the slightly less efficient __builtin_signbit(__f) ? _To(-1.0) : _To(+1.0) code for C++98. I also noticed that the definition of __sign needed to be outside the #if group. Tested x86_64-linux. -- >8 -- LWG 117 specifies that inserting a float into an ostream should cast it to double, because there's no std::num_put::put member that takes a float. However, on RISC-V converting a NaN float to double loses the sign, which means that negative NaN floats are printed as positive. This has been reported as LWG 4101 and there is good support for fixing the standard to preserve the sign bit when printing negative NaN values. This change uses copysign((double)f, (double)std::bit_cast(f)) to get a double that preserves the sign. The bit_cast gives us an integer with the same signbit, and casting that to the target type preserves the signbit. We don't care about the value, as copysign only uses the signbit. The inserters for extended floating-point types need the same treatment, so add a new _S_cast_flt helper to do the signbit-preserving conversion generically. So far only RISC-V has been confirmed to need this treatment, but we might need to extend it to other targets later. libstdc++-v3/ChangeLog: PR libstdc++/113578 * include/std/ostream (_S_cast_flt): New static member function to restore signbit after casting to double or long double. (operator<<(float), operator<<(_Float16), operator<<(_Float32)) (operator<<(_Float64), operator(_Float128)) (operator<<(__bfloat16_t)): Use _S_cast_flt. testsuite/27_io/basic_ostream/inserters_arithmetic/lwg4101.cc: New test. Co-authored-by: Andrew Waterman --- libstdc++-v3/include/std/ostream | 43 ++++++++++++++++--- .../inserters_arithmetic/lwg4101.cc | 14 ++++++ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/lwg4101.cc diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream index 12be6c4fd17..09572f8389c 100644 --- a/libstdc++-v3/include/std/ostream +++ b/libstdc++-v3/include/std/ostream @@ -233,7 +233,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { // _GLIBCXX_RESOLVE_LIB_DEFECTS // 117. basic_ostream uses nonexistent num_put member functions. - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } __ostream_type& @@ -246,7 +246,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float16 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -255,7 +255,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float32 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -264,7 +264,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float64 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -273,7 +273,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float128 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -282,7 +282,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(__gnu_cxx::__bfloat16_t __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -473,7 +473,38 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_write(const char_type* __s, streamsize __n) { std::__ostream_insert(*this, __s, __n); } #endif + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wc++17-extensions" // for if-constexpr + template + static _To + _S_cast_flt(_From __f) + { + _To __d = static_cast<_To>(__f); + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 4101: LWG 117 loses the sign for negative NaN on some arches. +#if defined __riscv + _To __sign; +#if __cpp_constexpr && __has_builtin(__builtin_bit_cast) + if constexpr (sizeof(__f) == sizeof(short)) + __sign = static_cast<_To>(__builtin_bit_cast(short, __f)); + else if constexpr (sizeof(__f) == sizeof(int)) + __sign = static_cast<_To>(__builtin_bit_cast(int, __f)); + else if constexpr (sizeof(__f) == sizeof(long long)) + __sign = static_cast<_To>(__builtin_bit_cast(long long, __f)); + else +#endif + __sign = __builtin_signbit(__f) ? _To(-1.0) : _To(+1.0); + + if _GLIBCXX17_CONSTEXPR (__is_same(_To, double)) + __d = __builtin_copysign(__d, __sign); + else if _GLIBCXX17_CONSTEXPR (__is_same(_To, long double)) + __d = __builtin_copysignl(__d, __sign); +#endif + return __d; + } }; +#pragma GCC diagnostic pop /** * @brief Performs setup work for output streams. diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/lwg4101.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/lwg4101.cc new file mode 100644 index 00000000000..1e1b8e08535 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/lwg4101.cc @@ -0,0 +1,14 @@ +// { dg-do run } +// LWG 4101. LWG 117 loses the sign for negative NaN on some architectures + +#include +#include +#include + +int main() +{ + float nan = std::numeric_limits::quiet_NaN(); + std::ostringstream os; + os << -nan; + VERIFY( os.str()[0] == '-' ); +}