From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id cTAnHywL9mo6mAYAWB0awg (envelope-from ) for ; Thu, 12 Nov 2026 13:53:32 -0500 Received: by simark.ca (Postfix, from userid 112) id 7C1AD1E087; Thu, 12 Nov 2026 13:53:32 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C7DA01E087 for ; Thu, 12 Nov 2026 13:53:31 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A6944BA2E37 for ; Thu, 12 Nov 2026 18:53:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A6944BA2E37 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id 8A16D4BA2E29 for ; Thu, 12 Nov 2026 18:53:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A16D4BA2E29 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=symas.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=dubner.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8A16D4BA2E29 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=166.84.1.89 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794509584; cv=none; b=GBW0crGC9qUP7wqMc4d10oTRFOrEPVj/3dya/lvIuAYIi24eSkBygDc1CCURl8PXytef6PCXP/v6HnHNXKmmhi8+Qd8djWLahOBFR9TwClelQFztjZD+9dLhZ94H/AGYIw3CXP/EVZDm9Pt1i/RNEdeakrBXakBUek1/fO0XUmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794509584; c=relaxed/simple; bh=pEzbPmEkr16ji7mMfq+O4zS9hFe7YHKY44N4zMUI8xU=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=UuiI8W8QHvem7sf1Yt/l/8Ad3LxkeyQKrNd7G6bTiFbFX23KwedN34dBf1rqg5P9DG7rwhQv6AvuBDhQSo28jb6O2zYmDJVOA1A0wlZPH3Fin5I4HvC18yPxZLTT8x1e5DGAvdHC7x+et6+n0UB7d5ogWoCBOWMD7ek2MwigZmE= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8A16D4BA2E29 Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jQTzh23Fcz4J8t; Thu, 12 Nov 2026 13:53:04 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id 0BD8D28E7A26; Thu, 12 Nov 2026 13:53:04 -0500 (EST) From: Robert Dubner To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com Subject: [PATCH v5 3/5] gdb: Preserve signed zero in MPFR target conversions Date: Thu, 12 Nov 2026 13:52:49 -0500 Message-Id: <20261112185251.1617554-4-rdubner@symas.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261112185251.1617554-1-rdubner@symas.com> References: <20261112185251.1617554-1-rdubner@symas.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org The MPFR encoder returns for zero before examining its sign, turning negative zero into positive zero during conversions and arithmetic. Process the sign before returning for zero, using mpfr_signbit to recognize negative zero. Retain all-zero encodings for formats without signed-zero support. Add unit tests in target-float.c covering both signs of zero through parsing, printing, host conversion, arithmetic, and target-format conversion across floating-point formats and byte orders. Reviewed-By: Rohr, Stephan Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927 --- gdb/target-float.c | 123 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 118 insertions(+), 5 deletions(-) diff --git a/gdb/target-float.c b/gdb/target-float.c index a267330d192..1d7fe55fa3b 100644 --- a/gdb/target-float.c +++ b/gdb/target-float.c @@ -1391,9 +1391,6 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, gdb_mpfr tmp (from); - if (mpfr_zero_p (from.val)) - goto finalize_byteorder; /* Result is zero */ - mpfr_set (tmp.val, from.val, MPFR_RNDN); if (mpfr_nan_p (tmp.val)) /* Result is NaN */ @@ -1408,13 +1405,19 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, } /* If negative, set the sign bit. */ - if (mpfr_sgn (tmp.val) < 0) + if (mpfr_signbit (tmp.val)) { - put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1); + /* Keep zero encodings unchanged for formats that do not have + signed zero, e.g. VAX. */ + if (!mpfr_zero_p (tmp.val) || fmt->exp_nan != 0) + put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1); mpfr_neg (tmp.val, tmp.val, MPFR_RNDN); inexact = -inexact; } + if (mpfr_zero_p (tmp.val)) + goto finalize_byteorder; + /* MPFR represents a nonzero finite value as M * 2^E, where 0.5 <= abs (M) < 1. The smallest normal target value is 2^(1 - exp_bias), so its MPFR exponent is 2 - exp_bias. @@ -1440,6 +1443,8 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, mpfr_subnormalize (tmp.val, inexact, MPFR_RNDN); } + /* Check again for zero because mpfr_check_range and mpfr_subnormalize can + turn a non-zero input into zero. */ if (mpfr_zero_p (tmp.val)) goto finalize_byteorder; @@ -2815,6 +2820,112 @@ subnormal_other_formats_test () SELF_CHECK (actual == std::vector ({0, 0x3f, 0, 0})); } +/* Exercise the MPFR backend directly so these checks do not depend on the + host's floating-point formats. Check both signs of zero through parsing, + host conversion, arithmetic, and conversion between target formats. */ + +static void +signed_zero_test () +{ + struct test_case + { + const floatformat *fmt; + unsigned int sign_byte; + }; + + static const test_case tests[] = + { + {&floatformat_ieee_half_big, 0}, + {&floatformat_ieee_half_little, 1}, + {&floatformat_ieee_single_big, 0}, + {&floatformat_ieee_single_little, 3}, + {&floatformat_ieee_double_big, 0}, + {&floatformat_ieee_double_little, 7}, + {&floatformat_ieee_quad_big, 0}, + {&floatformat_ieee_quad_little, 15}, + {&floatformat_i387_ext, 9}, + {&floatformat_m68881_ext, 0}, + {&floatformat_ieee_double_littlebyte_bigword, 3}, + {&floatformat_ibm_long_double_big, 0}, + {&floatformat_ibm_long_double_little, 7}, + /* VAX has no signed zero. Its negative-zero inputs must still + produce the all-zero encoding. */ + {&floatformat_vax_f, 1} + }; + + type_allocator alloc (get_current_arch ()); + std::vector types; + for (const test_case &test : tests) + { + const floatformat *pair[] = {test.fmt, test.fmt}; + types.push_back (init_float_type (alloc, -1, "selftest_float", pair, + BFD_ENDIAN_BIG)); + } + + mpfr_float_ops ops; + for (size_t i = 0; i < ARRAY_SIZE (tests); ++i) + { + type *ftype = types[i]; + std::vector zero (ftype->length ()); + std::vector one (ftype->length ()); + ops.from_ulongest (one.data (), ftype, 1); + auto minus_one = one; + minus_one[tests[i].sign_byte] |= 0x80; + + for (bool negative : {false, true}) + { + bool signed_negative = negative && tests[i].fmt->exp_nan != 0; + auto expected = zero; + if (signed_negative) + expected[tests[i].sign_byte] = 0x80; + std::vector actual (ftype->length (), 0xa5); + + SELF_CHECK (ops.from_string (actual.data (), ftype, + negative ? "-0" : "0")); + SELF_CHECK (actual == expected); + ops.from_host_double (actual.data (), ftype, negative ? -0.0 : 0.0); + SELF_CHECK (actual == expected); + SELF_CHECK (std::signbit (ops.to_host_double (expected.data (), + ftype)) + == signed_negative); + + std::string text = ops.to_string (expected.data (), ftype, nullptr); + SELF_CHECK (ops.from_string (actual.data (), ftype, text)); + SELF_CHECK (actual == expected); + + for (exp_opcode op : {BINOP_MUL, BINOP_DIV}) + { + ops.binop (op, expected.data (), ftype, one.data (), ftype, + actual.data (), ftype); + SELF_CHECK (actual == expected); + } + ops.binop (BINOP_ADD, expected.data (), ftype, + expected.data (), ftype, actual.data (), ftype); + SELF_CHECK (actual == expected); + + for (size_t j = 0; j < ARRAY_SIZE (tests); ++j) + { + std::vector converted (types[j]->length (), 0xa5); + std::vector wanted (types[j]->length ()); + if (signed_negative && tests[j].fmt->exp_nan != 0) + wanted[tests[j].sign_byte] = 0x80; + ops.convert (expected.data (), ftype, + converted.data (), types[j]); + SELF_CHECK (converted == wanted); + } + } + + /* Produce negative zero from nonnegative zero, without relying on + the parser to have preserved a negative-zero input. Construct -1 + from its sign bit to avoid depending on other encoder fixes. */ + ops.binop (BINOP_MUL, zero.data (), ftype, minus_one.data (), ftype, + one.data (), ftype); + if (tests[i].fmt->exp_nan != 0) + zero[tests[i].sign_byte] = 0x80; + SELF_CHECK (one == zero); + } +} + } /* namespace selftests */ #endif /* GDB_SELF_TEST */ @@ -2823,6 +2934,8 @@ INIT_GDB_FILE (target_float) #if GDB_SELF_TEST selftests::register_test ("target-float-put-field", selftests::put_field_test); + selftests::register_test ("target-float-signed-zero", + selftests::signed_zero_test); selftests::register_test ("target-float-subnormal", selftests::subnormal_test); selftests::register_test ("target-float-subnormal-rounding", -- 2.34.1