From: "Rohr, Stephan" <stephan.rohr@intel.com>
To: Robert Dubner <rdubner@symas.com>,
"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH 1/2] gdb: Preserve signed zero in MPFR target conversions
Date: Wed, 11 Nov 2026 13:52:42 +0000 [thread overview]
Message-ID: <IA3PR11MB44463217EC5E8CDE390EAFD0BC93712@IA3PR11MB444632.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20261111050412.3340227-2-rdubner@symas.com>
Hi Robert,
I have one concern regarding the second series. I applied this on top
of the put_field and subnormales series. If I apply the patch, it has some conflicts.
This is not the critical point. However, git silently drops content, which changes the
meaning of the series. I added a comment in the code. We need to pay attention
this does not happen when rebasing the series.
Apart from this, I have a few small nits, see below.
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Wednesday, 11 November 2026 06:04
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH 1/2] 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.
> ---
> gdb/target-float.c | 138
> +++++++++++++++++++++++++++++++++++++++++++--
> 1 file changed, 133 insertions(+), 5 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 31b5ac642a7..4e5819d2665 100644
> --- a/gdb/target-float.c
> +++ b/gdb/target-float.c
> @@ -21,6 +21,11 @@
> #include "floatformat.h"
> #include "target-float.h"
> #include "gdbarch.h"
> +#include "gdbsupport/selftest.h"
> +
> +#if GDB_SELF_TEST
> +#include "arch-utils.h"
> +#endif
>
> /* Target floating-point operations.
>
> @@ -1379,9 +1384,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 */
> @@ -1396,12 +1398,18 @@ 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 non-IEEE formats (e.g. VAX)
> + which do not have signed zero. */
Nit: maybe rephrase:
/* 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);
> }
>
> + if (mpfr_zero_p (tmp.val))
> + goto finalize_byteorder;
> +
This does not merge correctly on top of the subnormal patch.
The subnormal series adds the same two lines. Git merges
the two checks into one, without producing an error. When rebasing,
please pay attention this is handled correctly.
IIUC, the correct order after applying the patch, should be:
if (mpfr_signbit (tmp.val))
{ ... }
if (mpfr_zero_p (tmp.val)) /* before the subnormal block */
goto finalize_byteorder;
/* subnormal block */
> if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */
> {
> /* Infinity exponent is same as NaN's. */
> @@ -2491,3 +2499,123 @@ target_float_compare (const gdb_byte *x, const
> struct type *type_x,
> const target_float_ops *ops = get_target_float_ops (type_x, type_y);
> return ops->compare (x, type_x, y, type_y);
> }
> +
> +#if GDB_SELF_TEST
> +namespace selftests
> +{
> +
> +/* 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))
Nit: line to long.
> + == 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 */
> +
> +INIT_GDB_FILE (target_float)
> +{
> +#if GDB_SELF_TEST
> + selftests::register_test ("target-float-signed-zero",
> + selftests::signed_zero_test);
> +#endif
> +}
> --
> 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-11 13:53 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 5:04 [PATCH 0/2] gdb: Preserve signed zero in conversions and unary negation Robert Dubner
2026-11-11 5:04 ` [PATCH 1/2] gdb: Preserve signed zero in MPFR target conversions Robert Dubner
2026-11-11 13:52 ` Rohr, Stephan [this message]
2026-11-11 5:04 ` [PATCH 2/2] gdb: Preserve signed zero in floating-point unary negation Robert Dubner
2026-11-11 13:50 ` Rohr, Stephan
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=IA3PR11MB44463217EC5E8CDE390EAFD0BC93712@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