From: Robert Dubner <rdubner@symas.com>
To: gdb-patches@sourceware.org
Cc: stephan.rohr@intel.com
Subject: [PATCH v4 4/5] gdb: Preserve signed zero in floating-point unary negation
Date: Wed, 11 Nov 2026 12:42:46 -0500 [thread overview]
Message-ID: <20261111174247.3382098-5-rdubner@symas.com> (raw)
In-Reply-To: <20261111174247.3382098-1-rdubner@symas.com>
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.
Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>
---
gdb/testsuite/gdb.base/float-neg.c | 36 +++++++++++++
gdb/testsuite/gdb.base/float-neg.exp | 79 ++++++++++++++++++++++++++++
gdb/valarith.c | 8 ++-
3 files changed, 122 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..c2f990a9dd5
--- /dev/null
+++ b/gdb/testsuite/gdb.base/float-neg.c
@@ -0,0 +1,36 @@
+/* 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;
+#ifdef __FLT128_MAX__
+_Float128 f128 = 1.0;
+int have_float128 = 1;
+#else
+int have_float128 = 0;
+#endif
+
+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..f8b522d583b
--- /dev/null
+++ b/gdb/testsuite/gdb.base/float-neg.exp
@@ -0,0 +1,79 @@
+# 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
+}
+
+set variables {f d ld}
+if {[get_integer_valueof "have_float128" 0]} {
+ lappend variables f128
+}
+
+foreach {literal expected literal_type negated} {
+ -0 0 int -0
+ -0. -0 double 0
+ -.0 -0 double 0
+} {
+ with_test_prefix "literal $literal" {
+ gdb_test "whatis $literal" "type = $literal_type"
+ gdb_test "print $literal" " = $expected"
+ foreach var $variables {
+ with_test_prefix $var {
+ gdb_test "print $var = $literal" " = $expected" "assign"
+ gdb_test "print $var" " = $expected" "read back"
+ gdb_test "print -$var" " = $negated" "negate"
+ }
+ }
+ }
+}
+
+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
next prev parent reply other threads:[~2026-11-11 17:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 17:42 [PATCH v4 0/5] gdb: Fix target floating-point encoding and signed-zero negation Robert Dubner
2026-11-11 17:42 ` [PATCH v4 1/5] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-12 9:04 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 2/5] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
2026-11-12 9:25 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 3/5] gdb: Preserve signed zero in MPFR target conversions Robert Dubner
2026-11-12 9:30 ` Rohr, Stephan
2026-11-11 17:42 ` Robert Dubner [this message]
2026-11-12 9:32 ` [PATCH v4 4/5] gdb: Preserve signed zero in floating-point unary negation Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 5/5] gdb: Avoid oversized shifts when writing floating-point fields Robert Dubner
2026-11-12 9:48 ` Rohr, Stephan
2026-11-12 17:05 ` Robert Dubner
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=20261111174247.3382098-5-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