From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id MSg5CnD382qMDgEAWB0awg (envelope-from ) for ; Wed, 11 Nov 2026 00:04:48 -0500 Received: by simark.ca (Postfix, from userid 112) id 270AC1E043; Wed, 11 Nov 2026 00:04:48 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id D69661E043 for ; Wed, 11 Nov 2026 00:04:46 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 556F74BA7988 for ; Wed, 11 Nov 2026 05:04:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 556F74BA7988 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id 163C34BA2E25 for ; Wed, 11 Nov 2026 05:04:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 163C34BA2E25 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=symas.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=dubner.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 163C34BA2E25 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=166.84.1.89 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794373460; cv=none; b=h1I0F08cO2zQO8FuvqyRzuEAR9WsM44XMN44+vVXUdze95/I/Ls4R7E3ND1o0aBzfwIZbrqW4D+7OzMaEiMW0HAPMdBwoOigTVRWQ3YId34x1T5sDdBMPGtlagfTmg/uZ5suOjnuM0Pp6s0eCVDGmWr27KVHxoL0XvYV5rZvE7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794373460; c=relaxed/simple; bh=hlBC5DzmK5B8uMunUmo1DzQ8V6LfZ+kUJW1/r8QwEzw=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=TwcrYKskF6Fl3oRnbOVqyV2QMp7R4a4eYUDE0MhsDsaHxsXc3U1RiCuUejaGXEMV35B9qKMJwxCXzepTfbVXblHz8jGU+vOovB5sUR/Ze/LRBuz3HAIhTzbMnr9zczH4t0IHO1Wek4At+V1XzvJlrQJep5uHfaB38U0YNVcVxYU= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 163C34BA2E25 Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jPWdv413pz45Q5; Wed, 11 Nov 2026 00:04:19 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id 4465B28E902A; Wed, 11 Nov 2026 00:04:19 -0500 (EST) From: Robert Dubner To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com Subject: [PATCH 2/2] gdb: Preserve signed zero in floating-point unary negation Date: Wed, 11 Nov 2026 00:04:12 -0500 Message-Id: <20261111050412.3340227-3-rdubner@symas.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261111050412.3340227-1-rdubner@symas.com> References: <20261111050412.3340227-1-rdubner@symas.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 . */ + +/* 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 . + +# 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