From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id gdTTLC0L9mo6mAYAWB0awg (envelope-from ) for ; Thu, 12 Nov 2026 13:53:33 -0500 Received: by simark.ca (Postfix, from userid 112) id B47CD1E0BC; Thu, 12 Nov 2026 13:53:33 -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 1D04F1E087 for ; Thu, 12 Nov 2026 13:53:33 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D47C44BA79A4 for ; Thu, 12 Nov 2026 18:53:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D47C44BA79A4 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by sourceware.org (Postfix) with ESMTPS id 0ABC54BA2E2A for ; Thu, 12 Nov 2026 18:53:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0ABC54BA2E2A 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 0ABC54BA2E2A 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=1794509585; cv=none; b=FcIoqzZdsOEIhUzreOhyjL8gcQ+gaAsT76uy7i8g21okIBQhhaSQBduXWZqxW4xyEFZLKr65PY3JWKQyBSXrSkjHbWDCjI+JdN7dU/jw2zx8BIrWQeXmooWBVqfhiiNNFlkw50jQCMgYEagYn4k0/Gm/fO9CTrtVpNBcNYdCXwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794509585; c=relaxed/simple; bh=cCVP6vAnQODENE8UkkTegXyBIMdKiFpIT/BGnPBQ+cY=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=gI/T8oKUBYl3v3dC7FAiwXW6KBllYSty8BTHYI378uMFmiHaf6OLwTeOIxZD4KPGR7g+PIBJvuL4maeDGc0TuIJvE6gJ16VHRggtPI3zD6GRB+Ko2mSIGnQL/m48WHL2fmAARcf4Wq55+6hWw82xfCIiJ7wayRcYyFe3BQCc2GQ= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0ABC54BA2E2A Received: from doobie (unknown [67.245.74.220]) by mailbackend.panix.com (Postfix) with ESMTPSA id 4jQTzh5Jzcz4HfJ; Thu, 12 Nov 2026 13:53:04 -0500 (EST) Received: by doobie (Postfix, from userid 1000) id 7438028E7A26; Thu, 12 Nov 2026 13:53:04 -0500 (EST) From: Robert Dubner To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com Subject: [PATCH v5 4/5] gdb: Preserve signed zero in floating-point unary negation Date: Thu, 12 Nov 2026 13:52:50 -0500 Message-Id: <20261112185251.1617554-5-rdubner@symas.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261112185251.1617554-1-rdubner@symas.com> References: <20261112185251.1617554-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. Reviewed-By: Rohr, Stephan Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927 --- gdb/testsuite/gdb.base/float-neg.c | 38 +++++++++++++ gdb/testsuite/gdb.base/float-neg.exp | 79 ++++++++++++++++++++++++++++ gdb/valarith.c | 8 ++- 3 files changed, 124 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..e27b177fe8f --- /dev/null +++ b/gdb/testsuite/gdb.base/float-neg.c @@ -0,0 +1,38 @@ +/* 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; +#ifdef __FLT128_MAX__ +/* This variable is used for regression testing of assignment of -0.0 to a + 128-bit floating point variable. */ +_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 . + +# 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