From: "Rohr, Stephan" <stephan.rohr@intel.com>
To: Robert Dubner <rdubner@symas.com>,
"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH 1/1] gdb: Preserve subnormal values in target floating-point operations
Date: Fri, 6 Nov 2026 08:37:57 +0000 [thread overview]
Message-ID: <IA3PR11MB4446322CE1C7A6D89C75585AFF93762@IA3PR11MB444632.namprd11.prod.outlook.com> (raw)
In-Reply-To: <03ce01dd6bdc$788f5760$69ae0620$@symas.com>
Hi Robert,
thanks for the patch.
Again, I think a unit test in target-float.c helps.
I have a few small comments, see below.
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Tuesday, 3 November 2026 22:44
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH 1/1] gdb: Preserve subnormal values in target floating-point
> operations
>
> From 3112ecea54484f1dbbf4ad06bf34c89db339a1bf Mon Sep 17 00:00:00
> 2001
> From: Robert Dubner <rdubner@symas.com>
> 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.
> */
I think it would help understandability why we have subnormals here, i.e.,
the condition EXP (tmp.val) + fmt->exp-bias <= 1. I think this is because
MPFR converts the number.
> + 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));
Could you add a comment on this? Why "3 - exp_bias - precision"?
> + 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. */
Could you extend this comment to make it easier to understand? I understood
subnormals don't have the implicit leading one. Also, a "," between "explicit"
and "even" improves readability. What does the call below do? I think it shifts
bits that are read in the "while (mant_bits_left > 0)" loop below?
> + 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
________________________________________
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-06 8:38 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 21:44 Robert Dubner
2026-11-04 16:34 ` Tom Tromey
2026-11-06 8:37 ` 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=IA3PR11MB4446322CE1C7A6D89C75585AFF93762@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