From: "Rohr, Stephan" <stephan.rohr@intel.com>
To: Robert Dubner <rdubner@symas.com>,
"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH v4 3/5] gdb: Preserve signed zero in MPFR target conversions
Date: Thu, 12 Nov 2026 09:30:02 +0000 [thread overview]
Message-ID: <IA3PR11MB4446327A0DAFD7F42C76F9CCDE93702@IA3PR11MB444632.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20261111174247.3382098-4-rdubner@symas.com>
Hi Robert,
the patch looks good. One small nit, see below.
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Wednesday, 11 November 2026 18:43
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH v4 3/5] gdb: Preserve signed zero in MPFR target conversions
>
> 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>
> ---
> gdb/target-float.c | 124
> +++++++++++++++++++++++++++++++++++++++++++--
> 1 file changed, 119 insertions(+), 5 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 1994b6c92ed..fc0b7931860 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,20 @@ 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. */
> +
Nit: Remove the blank line.
> + 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 +1444,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 +2821,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 +2935,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
________________________________________
Intel Deutschland GmbH
Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
Tel: +49 (89) 99143-0
www.intel.de
Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman
Chairperson of the Supervisory Board: Sonja Pierer
Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
next prev parent reply other threads:[~2026-11-12 9:30 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 17:42 [PATCH v4 0/5] gdb: Fix target floating-point encoding and signed-zero negation Robert Dubner
2026-11-11 17:42 ` [PATCH v4 1/5] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-12 9:04 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 2/5] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
2026-11-12 9:25 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 3/5] gdb: Preserve signed zero in MPFR target conversions Robert Dubner
2026-11-12 9:30 ` Rohr, Stephan [this message]
2026-11-11 17:42 ` [PATCH v4 4/5] gdb: Preserve signed zero in floating-point unary negation Robert Dubner
2026-11-12 9:32 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 5/5] gdb: Avoid oversized shifts when writing floating-point fields Robert Dubner
2026-11-12 9:48 ` Rohr, Stephan
2026-11-12 17:05 ` 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=IA3PR11MB4446327A0DAFD7F42C76F9CCDE93702@IA3PR11MB444632.namprd11.prod.outlook.com \
--to=stephan.rohr@intel.com \
--cc=gdb-patches@sourceware.org \
--cc=rdubner@symas.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