From: "Rohr, Stephan" <stephan.rohr@intel.com>
To: Robert Dubner <rdubner@symas.com>,
"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH 2/2] gdb: Preserve signed zero in floating-point unary negation
Date: Wed, 11 Nov 2026 13:50:21 +0000 [thread overview]
Message-ID: <IA3PR11MB4446325AC722C643B0FE8BE95193712@IA3PR11MB444632.namprd11.prod.outlook.com> (raw)
In-Reply-To: <20261111050412.3340227-3-rdubner@symas.com>
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.
prev parent reply other threads:[~2026-11-11 13:51 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 " 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 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=IA3PR11MB4446325AC722C643B0FE8BE95193712@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