From: "Rohr, Stephan" <stephan.rohr@intel.com>
To: Robert Dubner <rdubner@symas.com>,
"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH] gdb: Preserve signed zero in floating-point negation and conversion
Date: Thu, 5 Nov 2026 13:58:09 +0000 [thread overview]
Message-ID: <IA3PR11MB444632CF259337D6B7227CE50E93772@IA3PR11MB444632.namprd11.prod.outlook.com> (raw)
In-Reply-To: <039501dd6bc3$0b81beb0$22853c10$@symas.com>
Hi Robert,
thanks for the patch.
I think it makes sense to put these patches into a single series.
The implementation looks good.
Could you please add a test for this? I think a unit test in target-float.c
is good.
One Nit for the commit message, please see below.
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Tuesday, 3 November 2026 19:42
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH] gdb: Preserve signed zero in floating-point negation and
> conversion
>
> From dd27fbf06bdf6c71a83bbbf0d7a4f3471f288d15 Mon Sep 17 00:00:00
> 2001
> From: Robert Dubner <rdubner@symas.com>
> Date: Tue, 3 Nov 2026 10:20:18 -0500
> Subject: [PATCH] gdb: Preserve signed zero in floating-point negation and
> conversion
>
> Assigning -0.0 to a floating-point variable produced positive zero.
>
> There are two causes. value_neg implements floating-point unary
> negation as zero minus the operand, but subtracting positive zero from
> positive zero produces positive zero. Use multiplication by minus one
> instead, preserving signed zero while leaving integer negation unchanged.
>
> The generic MPFR encoder also discards the sign of zero by returning
> before setting the sign bit. Use mpfr_signbit and handle the sign before
> returning for zero. Preserve existing behavior for formats without
> signed-zero support.
>
> The following program demonstrates the problem:
>
> float varfl;
> double vardbl;
> _Float128 var128;
>
Do we need both the float and double types? I think one of them
suffices. This would shorten the commit message.
> int main (int argc, char **argv)
> {
> return 0;
> }
>
> Compile with gcc -ggdb -O0 and stop at main.
>
> Before this change:
>
> (gdb) print varfl = -0.0
> $1 = 0
> (gdb) print vardbl = -0.0
> $2 = 0
> (gdb) print var128 = -0.0
> $3 = 0
>
> After this change:
>
> (gdb) print varfl = -0.0
> $1 = -0
> (gdb) print vardbl = -0.0
> $2 = -0
> (gdb) print var128 = -0.0
> $3 = -0
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
> ---
> gdb/target-float.c | 13 ++++++++-----
> gdb/valarith.c | 8 +++++++-
> 2 files changed, 15 insertions(+), 6 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 31b5ac642a7..b278295138e 100644
> --- a/gdb/target-float.c
> +++ b/gdb/target-float.c
> @@ -1379,9 +1379,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 +1393,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);
> + /* Formats without an infinity/NaN exponent (e.g. VAX) do not
> + support signed zero. */
> + 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;
> +
> if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */
> {
> /* Infinity exponent is same as NaN's. */
> diff --git a/gdb/valarith.c b/gdb/valarith.c
> index 62a35841f1f..b73d0b7f0aa 100644
> --- a/gdb/valarith.c
> +++ b/gdb/valarith.c
> @@ -1743,7 +1743,13 @@ value_neg (struct value *arg1)
> arg1 = coerce_ref (arg1);
> type = check_typedef (arg1->type ());
>
> - if (is_integral_type (type) || is_floating_type (type))
> + if (is_floating_type (type))
> + {
> + /* Subtracting from positive zero does not negate positive zero.
> + Multiply by minus one to preserve the sign of zero. */
> + return value_binop (value_from_longest (type, -1), arg1,
> BINOP_MUL);
> + }
> + else if (is_integral_type (type))
> return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
> else if (is_fixed_point_type (type))
> return value_binop (value::zero (type, not_lval), arg1, BINOP_SUB);
> --
> 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.
prev parent reply other threads:[~2026-11-05 13:58 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 18:42 Robert Dubner
2026-11-05 13:58 ` Rohr, Stephan [this message]
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=IA3PR11MB444632CF259337D6B7227CE50E93772@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