Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 0/2] gdb: Preserve signed zero in conversions and unary negation
@ 2026-11-11  5:04 Robert Dubner
  2026-11-11  5:04 ` [PATCH 1/2] gdb: Preserve signed zero in MPFR target conversions Robert Dubner
  2026-11-11  5:04 ` [PATCH 2/2] gdb: Preserve signed zero in floating-point unary negation Robert Dubner
  0 siblings, 2 replies; 5+ messages in thread
From: Robert Dubner @ 2026-11-11  5:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: stephan.rohr

This series fixes two places where GDB loses the sign of floating-point
zero.

The first patch preserves signed zero in the MPFR target encoder.
The encoder currently returns for zero before examining its sign.
Process the sign first, using mpfr_signbit, while retaining the existing
zero encoding for formats without signed-zero support.

The second patch corrects floating-point unary negation.  Computing
zero minus the operand does not negate positive zero.  Multiply
floating-point operands by -1 instead, leaving the integer and
fixed-point paths unchanged.

Together, these changes preserve the distinction between assigning
integer -0, which produces positive zero, and assigning floating-point
-0. or -.0, which produces negative zero.

adds DejaGNU coverage for literal types, assignments, both zero signs,
nested negation, and integer controls.

Robert Dubner (2):
  gdb: Preserve signed zero in MPFR target conversions
  gdb: Preserve signed zero in floating-point unary negation

 gdb/target-float.c                   | 138 ++++++++++++++++++++++++++-
 gdb/testsuite/gdb.base/float-neg.c   |  30 ++++++
 gdb/testsuite/gdb.base/float-neg.exp |  73 ++++++++++++++
 gdb/valarith.c                       |   8 +-
 4 files changed, 243 insertions(+), 6 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/float-neg.c
 create mode 100644 gdb/testsuite/gdb.base/float-neg.exp


base-commit: 9db3b515b964c1bbdc4d20c1202fbf27719be269
-- 
2.34.1


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

* [PATCH 1/2] gdb: Preserve signed zero in MPFR target conversions
  2026-11-11  5:04 [PATCH 0/2] gdb: Preserve signed zero in conversions and unary negation Robert Dubner
@ 2026-11-11  5:04 ` Robert Dubner
  2026-11-11 13:52   ` Rohr, Stephan
  2026-11-11  5:04 ` [PATCH 2/2] gdb: Preserve signed zero in floating-point unary negation Robert Dubner
  1 sibling, 1 reply; 5+ messages in thread
From: Robert Dubner @ 2026-11-11  5:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: stephan.rohr

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.  */
+      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.  */
@@ -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))
+		      == 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


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

* [PATCH 2/2] gdb: Preserve signed zero in floating-point unary negation
  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  5:04 ` Robert Dubner
  2026-11-11 13:50   ` Rohr, Stephan
  1 sibling, 1 reply; 5+ messages in thread
From: Robert Dubner @ 2026-11-11  5:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: stephan.rohr

value_neg implements negation as zero minus the operand.  For positive
floating-point zero, this produces positive zero rather than negative
zero.

Multiply floating-point operands by -1 instead.  Preserve the existing
integer and fixed-point negation paths.

Add regression tests distinguishing integer -0 from floating-point -0.
and -.0, including assignments to float, double, and long double.  Also
cover negation of both zero signs, nested negation, nonzero values, and
integer controls.
---
 gdb/testsuite/gdb.base/float-neg.c   | 30 ++++++++++++
 gdb/testsuite/gdb.base/float-neg.exp | 73 ++++++++++++++++++++++++++++
 gdb/valarith.c                       |  8 ++-
 3 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.base/float-neg.c
 create mode 100644 gdb/testsuite/gdb.base/float-neg.exp

diff --git a/gdb/testsuite/gdb.base/float-neg.c b/gdb/testsuite/gdb.base/float-neg.c
new file mode 100644
index 00000000000..483373dc91c
--- /dev/null
+++ b/gdb/testsuite/gdb.base/float-neg.c
@@ -0,0 +1,30 @@
+/* This test program is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Compiler-initialized negative zero provides an input independent of
+   GDB's unary-negation implementation.  */
+double positive_zero = 0.0;
+double negative_zero = -0.0;
+float f = 1.0f;
+double d = 1.0;
+long double ld = 1.0L;
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/float-neg.exp b/gdb/testsuite/gdb.base/float-neg.exp
new file mode 100644
index 00000000000..6fdf081c3c4
--- /dev/null
+++ b/gdb/testsuite/gdb.base/float-neg.exp
@@ -0,0 +1,73 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Check that floating-point negation changes the sign of zero, while
+# integer -0 remains integer zero.
+
+standard_testfile
+
+if {![allow_float_test]} {
+    return
+}
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
+    return
+}
+if {![runto_main]} {
+    return
+}
+
+# Do not require signed zero on targets whose floating-point format lacks it.
+set have_signed_zero 0
+gdb_test_multiple "print negative_zero" "target supports signed zero" {
+    -re -wrap " = -0" {
+	pass $gdb_test_name
+	set have_signed_zero 1
+    }
+    -re -wrap " = 0" {
+	unsupported $gdb_test_name
+    }
+}
+if {!$have_signed_zero} {
+    return
+}
+
+foreach {literal expected literal_type} {
+    -0 0 int
+    -0. -0 double
+    -.0 -0 double
+} {
+    with_test_prefix "literal $literal" {
+	gdb_test "whatis $literal" "type = $literal_type"
+	gdb_test "print $literal" " = $expected"
+	foreach var {f d ld} {
+	    with_test_prefix $var {
+		gdb_test "print $var = $literal" " = $expected" "assign"
+		gdb_test "print $var" " = $expected" "read back"
+	    }
+	}
+    }
+}
+
+gdb_test "print -positive_zero" " = -0"
+gdb_test "print -negative_zero" " = 0"
+gdb_test "print -(-negative_zero)" " = -0"
+gdb_test "print -1.5" { = -1\.5}
+gdb_test "print -(-1.5)" { = 1\.5}
+
+# Integer negation, including unsigned arithmetic, retains its behavior.
+gdb_test "print -5" " = -5"
+gdb_test "print -(-5)" " = 5"
+gdb_test "print -0u" " = 0"
+gdb_test "print -5u == 0u - 5u" " = 1"
diff --git a/gdb/valarith.c b/gdb/valarith.c
index 62a35841f1f..0052ed269b2 100644
--- a/gdb/valarith.c
+++ b/gdb/valarith.c
@@ -1743,8 +1743,14 @@ 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_integral_type (type))
     return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
+  else if (is_floating_type (type))
+    {
+      /* Subtracting from zero does not change the sign of positive zero.
+	 Multiplication by -1 negates both signs of floating-point zero.  */
+      return value_binop (arg1, value_from_longest (type, -1), BINOP_MUL);
+    }
   else if (is_fixed_point_type (type))
     return value_binop (value::zero (type, not_lval), arg1, BINOP_SUB);
   else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
-- 
2.34.1


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

* RE: [PATCH 2/2] gdb: Preserve signed zero in floating-point unary negation
  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
  0 siblings, 0 replies; 5+ messages in thread
From: Rohr, Stephan @ 2026-11-11 13:50 UTC (permalink / raw)
  To: Robert Dubner, gdb-patches

Hi Robert,

I'd only like to propose one fix for the testcase, see below.

With this addressed:

Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>

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 2/2] gdb: Preserve signed zero in floating-point unary
> negation
> 
> value_neg implements negation as zero minus the operand.  For positive
> floating-point zero, this produces positive zero rather than negative
> zero.
> 
> Multiply floating-point operands by -1 instead.  Preserve the existing
> integer and fixed-point negation paths.
> 
> Add regression tests distinguishing integer -0 from floating-point -0.
> and -.0, including assignments to float, double, and long double.  Also
> cover negation of both zero signs, nested negation, nonzero values, and
> integer controls.
> ---
>  gdb/testsuite/gdb.base/float-neg.c   | 30 ++++++++++++
>  gdb/testsuite/gdb.base/float-neg.exp | 73
> ++++++++++++++++++++++++++++
>  gdb/valarith.c                       |  8 ++-
>  3 files changed, 110 insertions(+), 1 deletion(-)
>  create mode 100644 gdb/testsuite/gdb.base/float-neg.c
>  create mode 100644 gdb/testsuite/gdb.base/float-neg.exp
> 
> diff --git a/gdb/testsuite/gdb.base/float-neg.c
> b/gdb/testsuite/gdb.base/float-neg.c
> new file mode 100644
> index 00000000000..483373dc91c
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/float-neg.c
> @@ -0,0 +1,30 @@
> +/* This test program is part of GDB, the GNU debugger.
> +
> +   Copyright 2026 Free Software Foundation, Inc.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +/* Compiler-initialized negative zero provides an input independent of
> +   GDB's unary-negation implementation.  */
> +double positive_zero = 0.0;
> +double negative_zero = -0.0;
> +float f = 1.0f;
> +double d = 1.0;
> +long double ld = 1.0L;

I think it would be nice to handle a non-host type, too.  For example:

  #ifdef __FLT128_MAX__
  _Float128 f128 = 1.0;
  #endif

Then, test this conditionally, in the .exp, too.

> +
> +int
> +main (void)
> +{
> +  return 0;
> +}
> diff --git a/gdb/testsuite/gdb.base/float-neg.exp
> b/gdb/testsuite/gdb.base/float-neg.exp
> new file mode 100644
> index 00000000000..6fdf081c3c4
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/float-neg.exp
> @@ -0,0 +1,73 @@
> +# Copyright 2026 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +
> +# Check that floating-point negation changes the sign of zero, while
> +# integer -0 remains integer zero.
> +
> +standard_testfile
> +
> +if {![allow_float_test]} {
> +    return
> +}
> +if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
> +    return
> +}
> +if {![runto_main]} {
> +    return
> +}
> +
> +# Do not require signed zero on targets whose floating-point format lacks it.
> +set have_signed_zero 0
> +gdb_test_multiple "print negative_zero" "target supports signed zero" {
> +    -re -wrap " = -0" {
> +	pass $gdb_test_name
> +	set have_signed_zero 1
> +    }
> +    -re -wrap " = 0" {
> +	unsupported $gdb_test_name
> +    }
> +}
> +if {!$have_signed_zero} {
> +    return
> +}
> +
> +foreach {literal expected literal_type} {
> +    -0 0 int
> +    -0. -0 double
> +    -.0 -0 double
> +} {
> +    with_test_prefix "literal $literal" {
> +	gdb_test "whatis $literal" "type = $literal_type"
> +	gdb_test "print $literal" " = $expected"
> +	foreach var {f d ld} {
> +	    with_test_prefix $var {
> +		gdb_test "print $var = $literal" " = $expected" "assign"
> +		gdb_test "print $var" " = $expected" "read back"
> +	    }
> +	}
> +    }
> +}
> +
> +gdb_test "print -positive_zero" " = -0"
> +gdb_test "print -negative_zero" " = 0"
> +gdb_test "print -(-negative_zero)" " = -0"
> +gdb_test "print -1.5" { = -1\.5}
> +gdb_test "print -(-1.5)" { = 1\.5}
> +
> +# Integer negation, including unsigned arithmetic, retains its behavior.
> +gdb_test "print -5" " = -5"
> +gdb_test "print -(-5)" " = 5"
> +gdb_test "print -0u" " = 0"
> +gdb_test "print -5u == 0u - 5u" " = 1"
> diff --git a/gdb/valarith.c b/gdb/valarith.c
> index 62a35841f1f..0052ed269b2 100644
> --- a/gdb/valarith.c
> +++ b/gdb/valarith.c
> @@ -1743,8 +1743,14 @@ 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_integral_type (type))
>      return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
> +  else if (is_floating_type (type))
> +    {
> +      /* Subtracting from zero does not change the sign of positive zero.
> +	 Multiplication by -1 negates both signs of floating-point zero.  */
> +      return value_binop (arg1, value_from_longest (type, -1), BINOP_MUL);
> +    }
>    else if (is_fixed_point_type (type))
>      return value_binop (value::zero (type, not_lval), arg1, BINOP_SUB);
>    else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
> --
> 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] 5+ messages in thread

* RE: [PATCH 1/2] gdb: Preserve signed zero in MPFR target conversions
  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
  0 siblings, 0 replies; 5+ messages in thread
From: Rohr, Stephan @ 2026-11-11 13:52 UTC (permalink / raw)
  To: Robert Dubner, gdb-patches

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.

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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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