From: Robert Dubner <rdubner@symas.com>
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 [thread overview]
Message-ID: <20261112185251.1617554-4-rdubner@symas.com> (raw)
In-Reply-To: <20261112185251.1617554-1-rdubner@symas.com>
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 <stephan.rohr@intel.com>
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<gdb_byte> ({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<type *> 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<gdb_byte> zero (ftype->length ());
+ std::vector<gdb_byte> 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<gdb_byte> 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<gdb_byte> converted (types[j]->length (), 0xa5);
+ std::vector<gdb_byte> 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
next prev parent reply other threads:[~2026-11-12 18:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 18:52 [PATCH v5 0/5] gdb: Fix target floating-point encoding and signed-zero negation Robert Dubner
2026-11-12 18:52 ` [PATCH v5 1/5] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-12 18:52 ` [PATCH v5 2/5] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
2026-11-12 18:52 ` Robert Dubner [this message]
2026-11-12 18:52 ` [PATCH v5 4/5] gdb: Preserve signed zero in floating-point unary negation Robert Dubner
2026-11-12 18:52 ` [PATCH v5 5/5] gdb: Avoid oversized shifts when writing floating-point fields Robert Dubner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261112185251.1617554-4-rdubner@symas.com \
--to=rdubner@symas.com \
--cc=gdb-patches@sourceware.org \
--cc=stephan.rohr@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox