Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb: Preserve signed zero in floating-point negation and conversion
@ 2026-11-03 18:42 Robert Dubner
  2026-11-05 13:58 ` Rohr, Stephan
  0 siblings, 1 reply; 2+ messages in thread
From: Robert Dubner @ 2026-11-03 18:42 UTC (permalink / raw)
  To: gdb-patches; +Cc: stephan.rohr

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;

  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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: [PATCH] gdb: Preserve signed zero in floating-point negation and conversion
  2026-11-03 18:42 [PATCH] gdb: Preserve signed zero in floating-point negation and conversion Robert Dubner
@ 2026-11-05 13:58 ` Rohr, Stephan
  0 siblings, 0 replies; 2+ messages in thread
From: Rohr, Stephan @ 2026-11-05 13:58 UTC (permalink / raw)
  To: Robert Dubner, gdb-patches

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-05 13:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 18:42 [PATCH] gdb: Preserve signed zero in floating-point negation and conversion Robert Dubner
2026-11-05 13:58 ` Rohr, Stephan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox