From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id uXapDupV6mqFYhAAWB0awg (envelope-from ) for ; Tue, 03 Nov 2026 16:45:14 -0500 Received: by simark.ca (Postfix, from userid 112) id 2D822378003; Tue, 03 Nov 2026 16:45:14 -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 211CD1E043 for ; Tue, 03 Nov 2026 16:45:01 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E1994BA23DD for ; Tue, 3 Nov 2026 21:44:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E1994BA23DD Received: from zmcc-4-mx.zmailcloud.com (zmcc-4-mx.zmailcloud.com [34.203.170.87]) by sourceware.org (Postfix) with ESMTPS id 18A054BA799B for ; Tue, 3 Nov 2026 21:44:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 18A054BA799B 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 18A054BA799B 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=1793742254; cv=none; b=netjfp9AOTcZOF86n35kxbLIoCBeC8vHDmrpn6nHG5WskuC5MBSU/+TzsMdxvPg+/38pklhQPry6YNSymqqZy+nvlw0oFi6DI+0SPceH3LRAv496RAEy6el6ooNW8gyJYhwlcYDxOrX4cXqpFPCi4joCAvIQLnmW9wDrid0WKCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793742254; c=relaxed/simple; bh=245RhT47IIX8hNkapfID+X4FpDQkGbuP8TylU2BKBoM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=ZfwPDrGxGMjCoALt367oDcjKZhhnJybZF35nhxyy/NrEJWavthdbmqsUQVxgVAhEt1cy+YWqqu6zkzMBdcQ304PjPSPJzmRumT89LE4LAaRvCwgjTTUJ5zUT5NKQ/MktBhpDMDKvaWvsjvTbpdE6kqgy5slp/1Aay7WOYKx52A8= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18A054BA799B 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 6E2027819CD3; Tue, 3 Nov 2026 15:44: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 556D1104F24A; Tue, 3 Nov 2026 15:44:13 -0600 (CST) Received: from localhost (localhost [127.0.0.1]) by zmcc-3-mta-1.zmailcloud.com (Postfix) with ESMTP id 3E2981043EBC; Tue, 3 Nov 2026 15:44: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 kStP842Df_bY; Tue, 3 Nov 2026 15:44: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 16122104F24A; Tue, 3 Nov 2026 15:44:13 -0600 (CST) From: Robert Dubner To: Cc: Subject: [PATCH 1/1] gdb: Preserve subnormal values in target floating-point operations Thread-Topic: [PATCH 1/1] gdb: Preserve subnormal values in target floating-point operations Date: Tue, 3 Nov 2026 15:44:13 -0600 (CST) Message-ID: <03ce01dd6bdc$788f5760$69ae0620$@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 R4792) Thread-Index: Ad1r27jtt7VzeHnRSzSfIsRNF5ORXg== Content-Language: en-us X-Zimbra-Message-Verification: hash=SHA256; guid=YWU1OTMxZmI0ODM2Nzg5ODM5OTYxZGYwMjZlYzg0ZTAyYjhiMDIwYjA5YjBmNDY2NTE0NTIyYjVjZDllNGExNw== 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 3112ecea54484f1dbbf4ad06bf34c89db339a1bf Mon Sep 17 00:00:00 2001 From: Robert Dubner Date: Tue, 3 Nov 2026 16:14:07 -0500 Subject: [PATCH 1/1] gdb: Preserve subnormal values in target floating-point operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The generic MPFR encoder does not correctly encode subnormal values. It can flush representable values to zero or encode their significands incorrectly. Encode subnormals with a zero exponent field and align their significands to the minimum normal exponent. Discard the implicit leading bit only for normal values. Preserve MPFR's indication of whether each result is exact, rounded upward, or rounded downward, so subnormal rounding avoids double- rounding errors. Temporarily adjust MPFR's minimum exponent during rounding and restore it afterward. Preserve the existing flush-to-zero behavior for formats without subnormal support. With compiler-initialized binary128 values: __float128 least128 = 0x1p-16494Q; __float128 largest_subnormal128 = 0x0.ffffffffffffffffffffffffffffp-16382Q; __float128 normal128 = 0x1p-16382Q; __float128 result; The corrected operations produce: (gdb) set var result = least128 * 1 (gdb) print result == least128 $1 = 1 (gdb) print result == 0 $2 = 0 (gdb) print normal128 - least128 == largest_subnormal128 $3 = 1 (gdb) print largest_subnormal128 + least128 == normal128 $4 = 1 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927 --- gdb/target-float.c | 101 +++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/gdb/target-float.c b/gdb/target-float.c index b278295138e..94e7480a8fb 100644 --- a/gdb/target-float.c +++ b/gdb/target-float.c @@ -21,6 +21,7 @@ #include "floatformat.h" #include "target-float.h" #include "gdbarch.h" +#include "gdbsupport/scope-exit.h" /* Target floating-point operations. @@ -1217,9 +1218,9 @@ class mpfr_float_ops : public target_float_ops const gdb_byte *from, gdb_mpfr &to) const; void to_target (const struct type *type, - const gdb_mpfr &from, gdb_byte *to) const; + const gdb_mpfr &from, gdb_byte *to, int inexact = 0) const; void to_target (const struct floatformat *fmt, - const gdb_mpfr &from, gdb_byte *to) const; + const gdb_mpfr &from, gdb_byte *to, int inexact = 0) const; }; @@ -1342,12 +1343,14 @@ mpfr_float_ops::from_target (const struct type *type, void mpfr_float_ops::to_target (const struct floatformat *fmt, - const gdb_mpfr &from, gdb_byte *orig_to) const + const gdb_mpfr &from, gdb_byte *orig_to, + int inexact) const { unsigned char *to = orig_to; mpfr_exp_t exponent; unsigned int mant_bits, mant_off; int mant_bits_left; + bool subnormal; enum floatformat_byteorders order = fmt->byteorder; unsigned char newto[FLOATFORMAT_LARGEST_BYTES]; @@ -1400,6 +1403,28 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, 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); + inexact = -inexact; + } + + if (mpfr_zero_p (tmp.val)) + goto finalize_byteorder; + + /* Subnormal values have fewer significant bits than normal values. Use + the operation's ternary result to avoid double rounding at halfway + cases. Restrict the exponent range only while rounding; the encoding + below needs intermediates outside the target's range (e.g. binary16). + Formats without an infinity/NaN exponent do not support subnormals. */ + if (!mpfr_inf_p (tmp.val) && fmt->exp_nan != 0 + && mpfr_get_exp (tmp.val) + fmt->exp_bias <= 1) + { + mpfr_exp_t saved_emin = mpfr_get_emin (); + SCOPE_EXIT + { + mpfr_set_emin (saved_emin); + }; + mpfr_set_emin (3 - fmt->exp_bias - floatformat_precision (fmt)); + inexact = mpfr_check_range (tmp.val, inexact, MPFR_RNDN); + mpfr_subnormalize (tmp.val, inexact, MPFR_RNDN); } if (mpfr_zero_p (tmp.val)) @@ -1418,14 +1443,20 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN); - if (exponent + fmt->exp_bias <= 0) + subnormal = fmt->exp_nan != 0 && exponent + fmt->exp_bias <= 1; + if (subnormal) { - /* The value is too small to be expressed in the destination - type (not enough bits in the exponent. Treat as 0. */ - put_field (to, order, fmt->totalsize, fmt->exp_start, - fmt->exp_len, 0); - put_field (to, order, fmt->totalsize, fmt->man_start, - fmt->man_len, 0); + /* Align to the minimum normal exponent, leaving the leading zero + explicit even for formats with an implicit integer bit. */ + mpfr_mul_2si (tmp.val, tmp.val, + exponent + fmt->exp_bias + - (fmt->intbit == floatformat_intbit_no ? 1 : 2), + MPFR_RNDN); + } + else if (exponent + fmt->exp_bias <= 0) + { + /* Formats without subnormals flush values that are too small to + represent to zero. The exponent and mantissa are already zero. */ goto finalize_byteorder; } @@ -1441,7 +1472,7 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, } put_field (to, order, fmt->totalsize, fmt->exp_start, fmt->exp_len, - exponent + fmt->exp_bias - 1); + subnormal ? 0 : exponent + fmt->exp_bias - 1); mant_bits_left = fmt->man_len; mant_off = fmt->man_start; @@ -1455,11 +1486,8 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, mant_long = mpfr_get_ui (tmp.val, MPFR_RNDZ) & 0xffffffffL; mpfr_sub_ui (tmp.val, tmp.val, mant_long, MPFR_RNDZ); - /* If the integer bit is implicit, then we need to discard it. - If we are discarding a zero, we should be (but are not) creating - a denormalized number which means adjusting the exponent - (I think). */ - if (mant_bits_left == fmt->man_len + /* Only normal values have an implicit leading one to discard. */ + if (!subnormal && mant_bits_left == fmt->man_len && fmt->intbit == floatformat_intbit_no) { mant_long <<= 1; @@ -1496,12 +1524,12 @@ mpfr_float_ops::to_target (const struct floatformat *fmt, void mpfr_float_ops::to_target (const struct type *type, - const gdb_mpfr &from, gdb_byte *to) const + const gdb_mpfr &from, gdb_byte *to, int inexact) const { /* Ensure possible padding bytes in the target buffer are zeroed out. */ memset (to, 0, type->length ()); - to_target (floatformat_from_type (type), from, to); + to_target (floatformat_from_type (type), from, to, inexact); } /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, @@ -1559,13 +1587,13 @@ mpfr_float_ops::from_string (gdb_byte *addr, gdb_mpfr tmp (type); char *endptr; - mpfr_strtofr (tmp.val, in.c_str (), &endptr, 0, MPFR_RNDN); + int inexact = mpfr_strtofr (tmp.val, in.c_str (), &endptr, 0, MPFR_RNDN); /* We only accept the whole string. */ if (*endptr) return false; - to_target (type, tmp, addr); + to_target (type, tmp, addr, inexact); return true; } @@ -1588,8 +1616,8 @@ mpfr_float_ops::from_longest (gdb_byte *addr, LONGEST val) const { gdb_mpfr tmp (type); - mpfr_set_sj (tmp.val, val, MPFR_RNDN); - to_target (type, tmp, addr); + int inexact = mpfr_set_sj (tmp.val, val, MPFR_RNDN); + to_target (type, tmp, addr, inexact); } /* Convert unsigned integer VAL to a target floating-number of type TYPE @@ -1600,8 +1628,8 @@ mpfr_float_ops::from_ulongest (gdb_byte *addr, ULONGEST val) const { gdb_mpfr tmp (type); - mpfr_set_uj (tmp.val, val, MPFR_RNDN); - to_target (type, tmp, addr); + int inexact = mpfr_set_uj (tmp.val, val, MPFR_RNDN); + to_target (type, tmp, addr, inexact); } /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, @@ -1623,8 +1651,8 @@ mpfr_float_ops::from_host_double (gdb_byte *addr, double val) const { gdb_mpfr tmp (type); - mpfr_set_d (tmp.val, val, MPFR_RNDN); - to_target (type, tmp, addr); + int inexact = mpfr_set_d (tmp.val, val, MPFR_RNDN); + to_target (type, tmp, addr, inexact); } /* Convert a floating-point number of type FROM_TYPE from the target @@ -1638,8 +1666,8 @@ mpfr_float_ops::convert (const gdb_byte *from, { gdb_mpfr from_tmp (from_type), to_tmp (to_type); from_target (from_type, from, from_tmp); - mpfr_set (to_tmp.val, from_tmp.val, MPFR_RNDN); - to_target (to_type, to_tmp, to); + int inexact = mpfr_set (to_tmp.val, from_tmp.val, MPFR_RNDN); + to_target (to_type, to_tmp, to, inexact); } /* Perform the binary operation indicated by OPCODE, using as operands the @@ -1657,34 +1685,35 @@ mpfr_float_ops::binop (enum exp_opcode op, from_target (type_x, x, x_tmp); from_target (type_y, y, y_tmp); + int inexact; switch (op) { case BINOP_ADD: - mpfr_add (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); + inexact = mpfr_add (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); break; case BINOP_SUB: - mpfr_sub (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); + inexact = mpfr_sub (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); break; case BINOP_MUL: - mpfr_mul (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); + inexact = mpfr_mul (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); break; case BINOP_DIV: - mpfr_div (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); + inexact = mpfr_div (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); break; case BINOP_EXP: - mpfr_pow (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); + inexact = mpfr_pow (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); break; case BINOP_MIN: - mpfr_min (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); + inexact = mpfr_min (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); break; case BINOP_MAX: - mpfr_max (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); + inexact = mpfr_max (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); break; default: @@ -1692,7 +1721,7 @@ mpfr_float_ops::binop (enum exp_opcode op, break; } - to_target (type_res, tmp, res); + to_target (type_res, tmp, res, inexact); } /* Compare the two target byte streams X and Y, interpreted as floating-point -- 2.34.1