Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Robert Dubner <rdubner@symas.com>
To: gdb-patches@sourceware.org
Cc: stephan.rohr@intel.com
Subject: [PATCH 1/2] gdb: Preserve signed zero in MPFR target conversions
Date: Wed, 11 Nov 2026 00:04:11 -0500	[thread overview]
Message-ID: <20261111050412.3340227-2-rdubner@symas.com> (raw)
In-Reply-To: <20261111050412.3340227-1-rdubner@symas.com>

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


  reply	other threads:[~2026-11-11  5:04 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 ` Robert Dubner [this message]
2026-11-11 13:52   ` [PATCH 1/2] gdb: Preserve signed zero in MPFR target conversions 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

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=20261111050412.3340227-2-rdubner@symas.com \
    --to=rdubner@symas.com \
    --cc=gdb-patches@sourceware.org \
    --cc=stephan.rohr@intel.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