From patchwork Thu Sep 26 20:38:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1989927 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GFMmKV+I; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4XF57L04WXz1xtG for ; Fri, 27 Sep 2024 06:39:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0BC6D3858428 for ; Thu, 26 Sep 2024 20:39:40 +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.133.124]) by sourceware.org (Postfix) with ESMTP id E91123858D29 for ; Thu, 26 Sep 2024 20:39:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E91123858D29 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 E91123858D29 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727383157; cv=none; b=pZJUIQHJpGuyHRsIpJ3gYyq11fBzXMfFUEgAbqJYf2V2ysgvkUIWX3RKXTMeAXpMEJowcfoDjmTmIO7oKx2EdwzJSbUH+LU8B/GesiVTqPMV1giclvd/Nqedr/cG7HXzU9qdki+CGbTYhN7jipME/QVWSikBVOoZCBkVh+zm2FU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727383157; c=relaxed/simple; bh=+h/hA9q20arA4v1GRL1L93xpyNcMGuovpypGSxZe9Yc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rBWlRVdnAj43R+WJBO4SIgP5V2hmR9qRY4lT7ep5T5uyzmzdCqcltT1KoGunywURy9r6DSSQOcZfyTTbDtELQol9pESDBrnRlSfDJXPfjx16NYM3DVZaHNWT5sIURZcs1QyQrIoHiCRv8KHiE219x7HlKT4B5YVL4zpS0LUjupM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727383155; 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; bh=/sma6Yev869ph0sSmcEKyvEQfytKsFMaANegJPpzyNQ=; b=GFMmKV+IeU4ZYsoBfKc2XyhJDiPmMyUECmSp+3LFp0+zvTUcUL132VKGnv5CqtDcnhfEeY n3TCxoTyZmIF9dk3n2eeq5+w7y8xULsAf4CAxfDcsykBMJQIPRSlbmw4M8/tqO/OfKOhgJ DPwuGPiWbk1ySbtJU8FSrkGNz7bu2xY= 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-216-1BskBRKZOU2gorkiop7Emw-1; Thu, 26 Sep 2024 16:39:14 -0400 X-MC-Unique: 1BskBRKZOU2gorkiop7Emw-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.4]) (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 52C2D1943CD6; Thu, 26 Sep 2024 20:39:13 +0000 (UTC) Received: from localhost (unknown [10.42.28.136]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AC9EC3003DEC; Thu, 26 Sep 2024 20:39:12 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Andrew Waterman Subject: [committed] libstdc++: Preserve signbit of nan when converting float to double [PR113578] Date: Thu, 26 Sep 2024 21:38:34 +0100 Message-ID: <20240926203911.2071550-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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.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 Tested x86_64-linux (and passed CI for ARM and RISV-V). Pushed to trunk. -- >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 d8462efe764..a1cb7ce1765 100644 --- a/libstdc++-v3/include/std/ostream +++ b/libstdc++-v3/include/std/ostream @@ -235,7 +235,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& @@ -248,7 +248,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float16 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -257,7 +257,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float32 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -266,7 +266,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float64 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -275,7 +275,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ostream_type& operator<<(_Float128 __f) { - return _M_insert(static_cast(__f)); + return _M_insert(_S_cast_flt(__f)); } #endif @@ -284,7 +284,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 @@ -475,7 +475,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] == '-' ); +}