From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 5rzIMh4r6mp/BAwAWB0awg (envelope-from ) for ; Tue, 03 Nov 2026 13:42:38 -0500 Received: by simark.ca (Postfix, from userid 112) id BA7111E043; Tue, 03 Nov 2026 13:42:38 -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 2C2661E043 for ; Tue, 03 Nov 2026 13:42:38 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3DE304BA799F for ; Tue, 3 Nov 2026 18:42:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DE304BA799F Received: from zmcc-4-mx.zmailcloud.com (zmcc-4-mx.zmailcloud.com [34.203.170.87]) by sourceware.org (Postfix) with ESMTPS id 8CEAD4BA2E21 for ; Tue, 3 Nov 2026 18:42:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8CEAD4BA2E21 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=symas.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=symas.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8CEAD4BA2E21 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=34.203.170.87 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793731333; cv=none; b=KH9/FYiKnQzriWeHXmvyO6GkydEvDgQh6weoNOnWWAmSGuKVsmC71Ccu1MBz4S3YLMrkYV0ADd1ean//4+f6Jcz/wyN42sjY1CEoGJMFnrTjw0eNSz0laPuhlU2nXri45kRsroZAPVpsA4Oj96BenUStj7onflu6CB0nlmRZOOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793731333; c=relaxed/simple; bh=5UnMP17azLRCNH8c8nEtXWt295WoEtNisTAn5vpvNWk=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Uk2LO0cHM+mZoYKA6ILxper+ro/Ak8D0AbVC69jLJL2HfiqZBH/5d4S3k0+YPAHhDDMjMjgDapNlqWH3MSy8fNl1lnztNWgPrGTPEhrF4d7jVQeHKHMaGYKSxB9j+36kxVtN5Ug0CkKMSOw7S96gSGFaBouG3D1NzEIvbZVLgVs= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8CEAD4BA2E21 Received: from zmcc-3.zmailcloud.com (ec2-3-15-255-223.us-east-2.compute.amazonaws.com [3.15.255.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by zmcc-4-mx.zmailcloud.com (Postfix) with ESMTPS id 628D17801DA2; Tue, 3 Nov 2026 12:42:13 -0600 (CST) Received: from zmcc-3.zmailcloud.com (localhost [127.0.0.1]) by zmcc-3-mta-1.zmailcloud.com (Postfix) with ESMTPS id 46CEA104F24A; Tue, 3 Nov 2026 12:42:13 -0600 (CST) Received: from localhost (localhost [127.0.0.1]) by zmcc-3-mta-1.zmailcloud.com (Postfix) with ESMTP id 2FF941043EBB; Tue, 3 Nov 2026 12:42:13 -0600 (CST) Received: from zmcc-3.zmailcloud.com ([127.0.0.1]) by localhost (zmcc-3-mta-1.zmailcloud.com [127.0.0.1]) (amavis, port 10026) with ESMTP id 5YV8P6MnAAzw; Tue, 3 Nov 2026 12:42:13 -0600 (CST) Received: from zmcc-3-mailbox-1.zmailcloud.com (zmcc-3-mailbox-1.zmailcloud.com [172.31.18.168]) by zmcc-3-mta-1.zmailcloud.com (Postfix) with ESMTP id 11446104F24A; Tue, 3 Nov 2026 12:42:13 -0600 (CST) From: Robert Dubner To: Cc: Subject: [PATCH] gdb: Preserve signed zero in floating-point negation and conversion Thread-Topic: [PATCH] gdb: Preserve signed zero in floating-point negation and conversion Date: Tue, 3 Nov 2026 12:42:12 -0600 (CST) Message-ID: <039501dd6bc3$0b81beb0$22853c10$@symas.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 16.0 X-Mailer: Zimbra 10.1.21_GA_4899 (Zimbra-ZCO/1951 (10.0.26200 en-US) P4838 T2870 R4206) Thread-Index: Ad1rwuNd/xVFKSVuT6C6ni0mKUMRWA== Content-Language: en-us X-Zimbra-Message-Verification: hash=SHA256; guid=YWMzYzM3Yzk3MDBmMjkxZmMzMTdiYzM2NzY4YWVkMzAzNWQ1YmNjODVkNWQ0Yjk3Y2NhODc3YzJlODVjNzJhOA== 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 >From dd27fbf06bdf6c71a83bbbf0d7a4f3471f288d15 Mon Sep 17 00:00:00 2001 From: Robert Dubner Date: Tue, 3 Nov 2026 10:20:18 -0500 Subject: [PATCH] gdb: Preserve signed zero in floating-point negation and conversion Assigning -0.0 to a floating-point variable produced positive zero. There are two causes. value_neg implements floating-point unary negation as zero minus the operand, but subtracting positive zero from positive zero produces positive zero. Use multiplication by minus one instead, preserving signed zero while leaving integer negation unchanged. The generic MPFR encoder also discards the sign of zero by returning before setting the sign bit. Use mpfr_signbit and handle the sign before returning for zero. Preserve existing behavior for formats without signed-zero support. The following program demonstrates the problem: float varfl; double vardbl; _Float128 var128; int main (int argc, char **argv) { return 0; } Compile with gcc -ggdb -O0 and stop at main. Before this change: (gdb) print varfl = -0.0 $1 = 0 (gdb) print vardbl = -0.0 $2 = 0 (gdb) print var128 = -0.0 $3 = 0 After this change: (gdb) print varfl = -0.0 $1 = -0 (gdb) print vardbl = -0.0 $2 = -0 (gdb) print var128 = -0.0 $3 = -0 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927 --- gdb/target-float.c | 13 ++++++++----- gdb/valarith.c | 8 +++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/gdb/target-float.c b/gdb/target-float.c index 31b5ac642a7..b278295138e 100644 --- a/gdb/target-float.c +++ b/gdb/target-float.c @@ -1379,9 +1379,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 +1393,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); + /* Formats without an infinity/NaN exponent (e.g. VAX) do not + support 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. */ diff --git a/gdb/valarith.c b/gdb/valarith.c index 62a35841f1f..b73d0b7f0aa 100644 --- a/gdb/valarith.c +++ b/gdb/valarith.c @@ -1743,7 +1743,13 @@ 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_floating_type (type)) + { + /* Subtracting from positive zero does not negate positive zero. + Multiply by minus one to preserve the sign of zero. */ + return value_binop (value_from_longest (type, -1), arg1, BINOP_MUL); + } + else if (is_integral_type (type)) return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB); else if (is_fixed_point_type (type)) return value_binop (value::zero (type, not_lval), arg1, BINOP_SUB); -- 2.34.1