* [PATCH v2 0/2] gdb: Fix floating-point field writes and subnormal results
@ 2026-11-09 21:24 Robert Dubner
2026-11-09 21:24 ` [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-09 21:24 ` [PATCH v2 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
0 siblings, 2 replies; 6+ messages in thread
From: Robert Dubner @ 2026-11-09 21:24 UTC (permalink / raw)
To: gdb-patches; +Cc: stephan.rohr
This series addresses two target floating-point encoding problems
reported in PR gdb/34927.
Changes since v1:
- Add unit tests directly in target-float.c for both fixes.
- Explain the subnormal boundary, minimum-exponent calculation,
and significand scaling.
Tested on x86_64 GNU/Linux with MPFR enabled: all 202 selftests
pass, and targeted DejaGNU testing reports 76 expected passes
with no failures.
Robert Dubner (2):
gdb: Preserve neighboring bits when writing floating-point fields
gdb: Preserve subnormal values in target floating-point operations
gdb/target-float.c | 396 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 353 insertions(+), 43 deletions(-)
base-commit: 9db3b515b964c1bbdc4d20c1202fbf27719be269
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields
2026-11-09 21:24 [PATCH v2 0/2] gdb: Fix floating-point field writes and subnormal results Robert Dubner
@ 2026-11-09 21:24 ` Robert Dubner
2026-11-10 10:23 ` Rohr, Stephan
2026-11-09 21:24 ` [PATCH v2 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
1 sibling, 1 reply; 6+ messages in thread
From: Robert Dubner @ 2026-11-09 21:24 UTC (permalink / raw)
To: gdb-patches; +Cc: stephan.rohr
put_field can clear bits outside the requested field when writing part
of a byte. For IEEE binary16, writing the exponent can clear the
adjacent sign bit, turning a negative value positive.
Limit both clearing and setting to the requested field width. Derive
the mask from cur_bitshift to account for little-endian formats whose
total size is not a multiple of eight bits. Correct the function
comment to describe bit lengths and the input requirements.
Add a unit test in target-float.c covering neighboring-bit preservation
in both byte orders, including formats with partial final bytes.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
---
gdb/target-float.c | 88 ++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 81 insertions(+), 7 deletions(-)
diff --git a/gdb/target-float.c b/gdb/target-float.c
index 31b5ac642a7..8032d4b7738 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/selftest.h"
/* Target floating-point operations.
@@ -229,8 +230,9 @@ get_field (const bfd_byte *data, enum floatformat_byteorders order,
return result;
}
-/* Set a field which starts at START and is LEN bytes long. DATA and
- TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
+/* Set a field which starts at START and is LEN bits long. DATA is the
+ buffer we are storing into, with TOTAL_LEN bits in byteorder ORDER.
+ STUFF_TO_PUT must fit in LEN bits. */
static void
put_field (unsigned char *data, enum floatformat_byteorders order,
unsigned int total_len, unsigned int start, unsigned int len,
@@ -260,11 +262,11 @@ put_field (unsigned char *data, enum floatformat_byteorders order,
}
if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
{
- *(data + cur_byte) &=
- ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
- << (-cur_bitshift));
- *(data + cur_byte) |=
- (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
+ unsigned int bits
+ = std::min (len, (unsigned int) (FLOATFORMAT_CHAR_BIT + cur_bitshift));
+ unsigned int mask = ((1 << bits) - 1) << (-cur_bitshift);
+ *(data + cur_byte) &= ~mask;
+ *(data + cur_byte) |= (stuff_to_put << (-cur_bitshift)) & mask;
}
cur_bitshift += FLOATFORMAT_CHAR_BIT;
if (order == floatformat_little)
@@ -2491,3 +2493,75 @@ target_float_compare (const gdb_byte *x, const struct type *type_x,
const target_float_ops *ops = get_target_float_ops (type_x, type_y);
return ops->compare (x, type_x, y, type_y);
}
+
+#if GDB_SELF_TEST
+namespace selftests
+{
+
+/* Check that writing a field preserves neighboring bits and buffer guards.
+ Use explicit expected bytes, independently of get_field and host floating-
+ point formats. */
+
+static void
+put_field_test ()
+{
+ struct test_case
+ {
+ floatformat_byteorders order;
+ unsigned int total_len;
+ unsigned int start;
+ unsigned int len;
+ unsigned long value;
+ unsigned char before[2];
+ unsigned char after[2];
+ };
+
+ const test_case tests[] =
+ {
+ /* Writing the binary16 exponent must preserve the sign and fraction.
+ The first pair produces the encoding of -1.5. */
+ {floatformat_big, 16, 1, 5, 15, {0x82, 0x00}, {0xbe, 0x00}},
+ {floatformat_little, 16, 1, 5, 15, {0x00, 0x82}, {0x00, 0xbe}},
+ {floatformat_big, 16, 1, 5, 0, {0xff, 0xff}, {0x83, 0xff}},
+ {floatformat_little, 16, 1, 5, 0, {0xff, 0xff}, {0xff, 0x83}},
+ {floatformat_big, 16, 1, 5, 31, {0x80, 0x03}, {0xfc, 0x03}},
+ {floatformat_little, 16, 1, 5, 31, {0x03, 0x80}, {0x03, 0xfc}},
+
+ /* Check fields that span bytes and end on a byte boundary. */
+ {floatformat_big, 16, 4, 12, 0x123, {0xff, 0xff}, {0xf1, 0x23}},
+ {floatformat_little, 16, 4, 12, 0x123, {0xff, 0xff}, {0x23, 0xf1}},
+ {floatformat_big, 16, 0, 16, 0xa55a, {0xff, 0xff}, {0xa5, 0x5a}},
+ {floatformat_little, 16, 0, 16, 0xa55a, {0xff, 0xff}, {0x5a, 0xa5}},
+
+ /* A non-byte-sized format also exercises the little-endian excess
+ adjustment. Preserve its padding and neighboring field bits. */
+ {floatformat_big, 13, 1, 3, 0, {0xff, 0xff}, {0x8f, 0xff}},
+ {floatformat_little, 13, 1, 3, 0, {0xff, 0xff}, {0xff, 0xf1}},
+ {floatformat_big, 13, 1, 3, 5, {0xff, 0xff}, {0xdf, 0xff}},
+ {floatformat_little, 13, 1, 3, 5, {0xff, 0xff}, {0xff, 0xfb}},
+ {floatformat_big, 13, 1, 5, 0, {0xff, 0xff}, {0x83, 0xff}},
+ {floatformat_little, 13, 1, 5, 0, {0xff, 0xff}, {0x7f, 0xf0}}
+ };
+
+ for (const test_case &test : tests)
+ {
+ unsigned char data[] = {0xa5, test.before[0], test.before[1], 0x5a};
+ put_field (data + 1, test.order, test.total_len, test.start, test.len,
+ test.value);
+ SELF_CHECK (data[0] == 0xa5);
+ SELF_CHECK (data[1] == test.after[0]);
+ SELF_CHECK (data[2] == test.after[1]);
+ SELF_CHECK (data[3] == 0x5a);
+ }
+}
+
+} /* namespace selftests */
+#endif /* GDB_SELF_TEST */
+
+INIT_GDB_FILE (target_float)
+{
+#if GDB_SELF_TEST
+ selftests::register_test ("target-float-put-field",
+ selftests::put_field_test);
+#endif
+}
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 2/2] gdb: Preserve subnormal values in target floating-point operations
2026-11-09 21:24 [PATCH v2 0/2] gdb: Fix floating-point field writes and subnormal results Robert Dubner
2026-11-09 21:24 ` [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
@ 2026-11-09 21:24 ` Robert Dubner
2026-11-10 10:40 ` Rohr, Stephan
1 sibling, 1 reply; 6+ messages in thread
From: Robert Dubner @ 2026-11-09 21:24 UTC (permalink / raw)
To: gdb-patches; +Cc: stephan.rohr
The MPFR encoder can flush representable subnormal values to zero or
encode their significands incorrectly.
Encode subnormals with a zero exponent field and align their fractions
to the minimum normal exponent. Discard the implicit leading bit only
for normal values.
Carry MPFR's ternary results through parsing, conversions, and arithmetic
to avoid double rounding. Temporarily adjust the minimum exponent while
rounding, restoring it afterward. Preserve flush-to-zero behavior for
formats without subnormal support.
Explain the subnormal boundary, minimum-exponent calculation, and
significand scaling.
Add unit tests in target-float.c covering both byte orders, exact
subnormals, rounding boundaries, narrowing conversions, arithmetic,
explicit integer bits, and restoration of MPFR's exponent range.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
---
gdb/target-float.c | 308 +++++++++++++++++++++++++++++++++++++++------
1 file changed, 272 insertions(+), 36 deletions(-)
diff --git a/gdb/target-float.c b/gdb/target-float.c
index 8032d4b7738..080b79d455c 100644
--- a/gdb/target-float.c
+++ b/gdb/target-float.c
@@ -21,8 +21,13 @@
#include "floatformat.h"
#include "target-float.h"
#include "gdbarch.h"
+#include "gdbsupport/scope-exit.h"
#include "gdbsupport/selftest.h"
+#if GDB_SELF_TEST
+#include "arch-utils.h"
+#endif
+
/* Target floating-point operations.
We provide multiple implementations of those operations, which differ
@@ -1218,10 +1223,12 @@ class mpfr_float_ops : public target_float_ops
void from_target (const struct type *type,
const gdb_byte *from, gdb_mpfr &to) const;
+ /* INEXACT is the MPFR ternary result of the computation producing FROM.
+ Carry it into subnormal rounding to avoid rounding twice. */
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;
};
@@ -1344,12 +1351,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];
@@ -1402,8 +1411,37 @@ mpfr_float_ops::to_target (const struct floatformat *fmt,
{
put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1);
mpfr_neg (tmp.val, tmp.val, MPFR_RNDN);
+ inexact = -inexact;
+ }
+
+ /* MPFR represents a nonzero finite value as M * 2^E, where
+ 0.5 <= abs (M) < 1. The smallest normal target value is
+ 2^(1 - exp_bias), so its MPFR exponent is 2 - exp_bias.
+ Smaller exponents (E + exp_bias <= 1) require subnormal rounding.
+ 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);
+ };
+
+ /* With precision P, the least subnormal is
+ 2^(1 - exp_bias - (P - 1)). MPFR's exponent is one greater
+ than the power of two, giving emin = 3 - exp_bias - P.
+ Restrict the range only while rounding: the encoding below needs
+ intermediates outside the target's range (e.g. for binary16).
+ Use the operation's ternary result to avoid double rounding. */
+ 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))
+ goto finalize_byteorder;
+
if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */
{
/* Infinity exponent is same as NaN's. */
@@ -1417,14 +1455,24 @@ 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);
+ /* Subnormals use the minimum normal exponent, but have no leading
+ one. Scale the fraction returned by mpfr_frexp to that exponent,
+ leaving the leading zero explicit, even for formats with an
+ implicit integer bit. The loop below extracts successive 32-bit
+ chunks of this fraction. An explicit integer bit occupies its
+ first bit, so shift one place further right for those formats. */
+ 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;
}
@@ -1440,7 +1488,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;
@@ -1454,11 +1502,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;
@@ -1495,12 +1540,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,
@@ -1558,13 +1603,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;
}
@@ -1587,8 +1632,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
@@ -1599,8 +1644,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,
@@ -1622,8 +1667,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
@@ -1637,8 +1682,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
@@ -1656,34 +1701,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:
@@ -1691,7 +1737,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
@@ -2555,6 +2601,190 @@ put_field_test ()
}
}
+/* Exercise the MPFR implementation directly, independently of the host's
+ floating-point formats. Each pair contains big- and little-endian forms. */
+
+static const floatformat *const subnormal_formats[][2] =
+ {
+ {&floatformat_ieee_half_big, &floatformat_ieee_half_little},
+ {&floatformat_ieee_single_big, &floatformat_ieee_single_little},
+ {&floatformat_ieee_double_big, &floatformat_ieee_double_little},
+ {&floatformat_ieee_quad_big, &floatformat_ieee_quad_little}
+ };
+
+static type *
+subnormal_type (const floatformat *fmt)
+{
+ /* Supply FMT in both slots so it can be selected independently of the
+ architecture's byte order. init_float_type retains only that format. */
+ const floatformat *pair[] = {fmt, fmt};
+ type_allocator alloc (get_current_arch ());
+ return init_float_type (alloc, -1, "selftest_float", pair, BFD_ENDIAN_BIG);
+}
+
+/* Encode integer BITS independently of the floating-point encoder. */
+
+static std::vector<gdb_byte>
+subnormal_bytes (const gdb_mpz &bits, const type *ftype)
+{
+ std::vector<gdb_byte> bytes (ftype->length ());
+ bfd_endian order = floatformat_from_type (ftype)->byteorder == floatformat_big
+ ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
+ bits.write (bytes, order, true);
+ return bytes;
+}
+
+/* Spell COEFFICIENT * 2^EXPONENT exactly, without host floating-point math. */
+
+static std::string
+subnormal_string (const gdb_mpz &coefficient, int exponent)
+{
+ return coefficient.hex () + "p" + std::to_string (exponent);
+}
+
+/* Check exact subnormals, text round trips, byte-order conversion, and the
+ boundary between normal and subnormal arithmetic. */
+
+static void
+subnormal_test ()
+{
+ mpfr_float_ops ops;
+ for (const auto &formats : subnormal_formats)
+ for (int endian = 0; endian < 2; ++endian)
+ {
+ const floatformat *fmt = formats[endian];
+ type *ftype = subnormal_type (fmt);
+ type *opposite = subnormal_type (formats[1 - endian]);
+ int quantum = 1 - fmt->exp_bias - (int) fmt->man_len;
+ gdb_mpz normal = gdb_mpz (1) << fmt->man_len;
+ gdb_mpz sign = gdb_mpz (1) << (fmt->totalsize - 1);
+ for (const gdb_mpz &magnitude : {gdb_mpz (1), gdb_mpz (2),
+ normal - gdb_mpz (1), normal})
+ for (bool negative : {false, true})
+ {
+ gdb_mpz bits = negative ? magnitude | sign : magnitude;
+ auto expected = subnormal_bytes (bits, ftype);
+ std::vector<gdb_byte> actual (ftype->length ());
+ std::string text = subnormal_string (magnitude, quantum);
+ if (negative)
+ text.insert (0, "-");
+ SELF_CHECK (ops.from_string (actual.data (), ftype, text));
+ SELF_CHECK (actual == expected);
+ text = ops.to_string (expected.data (), ftype, nullptr);
+ SELF_CHECK (ops.from_string (actual.data (), ftype, text));
+ SELF_CHECK (actual == expected);
+ ops.convert (expected.data (), ftype, actual.data (), opposite);
+ SELF_CHECK (actual == subnormal_bytes (bits, opposite));
+ }
+
+ auto least = subnormal_bytes (gdb_mpz (1), ftype);
+ auto largest = subnormal_bytes (normal - gdb_mpz (1), ftype);
+ auto minimum = subnormal_bytes (normal, ftype);
+ std::vector<gdb_byte> actual (ftype->length ());
+ ops.binop (BINOP_SUB, minimum.data (), ftype, least.data (), ftype,
+ actual.data (), ftype);
+ SELF_CHECK (actual == largest);
+ ops.binop (BINOP_ADD, largest.data (), ftype, least.data (), ftype,
+ actual.data (), ftype);
+ SELF_CHECK (actual == minimum);
+ }
+}
+
+/* Test halfway values and both sides of each tie, including digits lost by
+ rounding to normal precision before subnormal rounding. Expected results
+ follow round-to-nearest, ties-to-even, without using MPFR as an oracle. */
+
+static void
+subnormal_rounding_test ()
+{
+ mpfr_float_ops ops;
+ mpfr_exp_t saved_emin = mpfr_get_emin (), saved_emax = mpfr_get_emax ();
+ for (const auto &formats : subnormal_formats)
+ for (const floatformat *fmt : formats)
+ {
+ type *ftype = subnormal_type (fmt);
+ type *wide = subnormal_type (&floatformat_ieee_quad_big);
+ int quantum = 1 - fmt->exp_bias - (int) fmt->man_len;
+ int tail = fmt->man_len + 9;
+ gdb_mpz normal = gdb_mpz (1) << fmt->man_len;
+ gdb_mpz sign = gdb_mpz (1) << (fmt->totalsize - 1);
+ for (const gdb_mpz &lower : {gdb_mpz (0), gdb_mpz (1), gdb_mpz (2),
+ normal - gdb_mpz (1)})
+ for (int side : {-1, 0, 1})
+ for (bool negative : {false, true})
+ {
+ gdb_mpz coefficient = ((lower * gdb_mpz (2) + 1) << (tail - 1))
+ + gdb_mpz (side);
+ gdb_mpz expected = lower;
+ if (side > 0 || (side == 0 && (lower & gdb_mpz (1)) == 1))
+ expected += 1;
+ if (negative)
+ expected = expected | sign;
+ auto bytes = subnormal_bytes (expected, ftype);
+ std::string text
+ = subnormal_string (coefficient, quantum - tail);
+ if (negative)
+ text.insert (0, "-");
+ std::vector<gdb_byte> actual (ftype->length ());
+ SELF_CHECK (ops.from_string (actual.data (), ftype, text));
+ SELF_CHECK (actual == bytes);
+
+ /* Binary128 represents these binary16/32/64 cases exactly.
+ Check the ternary result from narrowing and each operation,
+ as well as parsing directly at the destination precision. */
+ if (fmt->man_len < floatformat_ieee_quad_big.man_len)
+ {
+ std::vector<gdb_byte> source (wide->length ());
+ SELF_CHECK (ops.from_string (source.data (), wide, text));
+ ops.convert (source.data (), wide, actual.data (), ftype);
+ SELF_CHECK (actual == bytes);
+ for (exp_opcode op : {BINOP_ADD, BINOP_SUB, BINOP_MUL,
+ BINOP_DIV, BINOP_EXP, BINOP_MIN, BINOP_MAX})
+ {
+ std::vector<gdb_byte> right (wide->length ());
+ LONGEST rhs = op == BINOP_ADD || op == BINOP_SUB ? 0
+ : op == BINOP_MAX ? -1 : 1;
+ ops.from_longest (right.data (), wide, rhs);
+ ops.binop (op, source.data (), wide, right.data (), wide,
+ actual.data (), ftype);
+ SELF_CHECK (actual == bytes);
+ }
+ }
+ SELF_CHECK (mpfr_get_emin () == saved_emin);
+ SELF_CHECK (mpfr_get_emax () == saved_emax);
+ }
+ }
+}
+
+/* Also check encoding with explicit integer bits and preserve the existing
+ flush-to-zero policy of formats without subnormal support. */
+
+static void
+subnormal_other_formats_test ()
+{
+ mpfr_float_ops ops;
+ for (const floatformat *fmt : {&floatformat_i387_ext,
+ &floatformat_m68881_ext})
+ {
+ type *ftype = subnormal_type (fmt);
+ std::vector<gdb_byte> actual (ftype->length ());
+ std::vector<gdb_byte> expected (ftype->length ());
+ expected[fmt->byteorder == floatformat_little ? 0 : 11] = 1;
+ int quantum = 2 - fmt->exp_bias - floatformat_precision (fmt);
+ SELF_CHECK (ops.from_string (actual.data (), ftype,
+ subnormal_string (gdb_mpz (1), quantum)));
+ SELF_CHECK (actual == expected);
+ }
+
+ type *ftype = subnormal_type (&floatformat_vax_f);
+ std::vector<gdb_byte> actual (ftype->length ());
+ std::vector<gdb_byte> zero (ftype->length ());
+ SELF_CHECK (ops.from_string (actual.data (), ftype, "0x1p-200"));
+ SELF_CHECK (actual == zero);
+ SELF_CHECK (ops.from_string (actual.data (), ftype, "0.125"));
+ SELF_CHECK (actual == std::vector<gdb_byte> ({0, 0x3f, 0, 0}));
+}
+
} /* namespace selftests */
#endif /* GDB_SELF_TEST */
@@ -2563,5 +2793,11 @@ INIT_GDB_FILE (target_float)
#if GDB_SELF_TEST
selftests::register_test ("target-float-put-field",
selftests::put_field_test);
+ selftests::register_test ("target-float-subnormal",
+ selftests::subnormal_test);
+ selftests::register_test ("target-float-subnormal-rounding",
+ selftests::subnormal_rounding_test);
+ selftests::register_test ("target-float-subnormal-other-formats",
+ selftests::subnormal_other_formats_test);
#endif
}
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields
2026-11-09 21:24 ` [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
@ 2026-11-10 10:23 ` Rohr, Stephan
2026-11-11 4:03 ` Robert Dubner
0 siblings, 1 reply; 6+ messages in thread
From: Rohr, Stephan @ 2026-11-10 10:23 UTC (permalink / raw)
To: Robert Dubner, gdb-patches
Hi Robert,
thanks for fixing this.
What happened to the signed zero patch?
I have one optional proposal for this patch, see below.
Thanks
Stephan
Reviewed-By: Stephan Rohr <stephan.rohr@intel.com>
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Monday, 9 November 2026 22:25
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-
> point fields
>
> put_field can clear bits outside the requested field when writing part
> of a byte. For IEEE binary16, writing the exponent can clear the
> adjacent sign bit, turning a negative value positive.
>
> Limit both clearing and setting to the requested field width. Derive
> the mask from cur_bitshift to account for little-endian formats whose
> total size is not a multiple of eight bits. Correct the function
> comment to describe bit lengths and the input requirements.
>
> Add a unit test in target-float.c covering neighboring-bit preservation
> in both byte orders, including formats with partial final bytes.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
> ---
> gdb/target-float.c | 88
> ++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 81 insertions(+), 7 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 31b5ac642a7..8032d4b7738 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/selftest.h"
>
> /* Target floating-point operations.
>
> @@ -229,8 +230,9 @@ get_field (const bfd_byte *data, enum
> floatformat_byteorders order,
> return result;
> }
>
> -/* Set a field which starts at START and is LEN bytes long. DATA and
> - TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
> +/* Set a field which starts at START and is LEN bits long. DATA is the
> + buffer we are storing into, with TOTAL_LEN bits in byteorder ORDER.
> + STUFF_TO_PUT must fit in LEN bits. */
> static void
> put_field (unsigned char *data, enum floatformat_byteorders order,
> unsigned int total_len, unsigned int start, unsigned int len,
> @@ -260,11 +262,11 @@ put_field (unsigned char *data, enum
> floatformat_byteorders order,
> }
> if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
> {
> - *(data + cur_byte) &=
> - ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
> - << (-cur_bitshift));
> - *(data + cur_byte) |=
> - (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-
> cur_bitshift);
> + unsigned int bits
> + = std::min (len, (unsigned int) (FLOATFORMAT_CHAR_BIT +
> cur_bitshift));
> + unsigned int mask = ((1 << bits) - 1) << (-cur_bitshift);
> + *(data + cur_byte) &= ~mask;
> + *(data + cur_byte) |= (stuff_to_put << (-cur_bitshift)) & mask;
> }
I think it would be also good to properly mask the last byte in the while loop:
/* Move towards the most significant part of the field. */
while (cur_bitshift < len)
{
if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
{
/* This is the last byte. */
*(data + cur_byte) &=
~((1 << (len - cur_bitshift)) - 1);
*(data + cur_byte) |= ((stuff_to_put >> cur_bitshift)
& ((1 << (len - cur_bitshift)) - 1));
}
This is not a bug in your patch, but as you're masking the bits properly here,
it would be nice to also extend the loop. For example, writing the binary16
mantissa with an 11-bit value
put_field (data, floatformat_big, 16, 6, 10, 0x7ff);
on { 0x00, 0x00 } gives { 0x07, 0xff } instead of { 0x03, 0xff }
> cur_bitshift += FLOATFORMAT_CHAR_BIT;
> if (order == floatformat_little)
> @@ -2491,3 +2493,75 @@ target_float_compare (const gdb_byte *x, const
> struct type *type_x,
> const target_float_ops *ops = get_target_float_ops (type_x, type_y);
> return ops->compare (x, type_x, y, type_y);
> }
> +
> +#if GDB_SELF_TEST
> +namespace selftests
> +{
> +
> +/* Check that writing a field preserves neighboring bits and buffer guards.
> + Use explicit expected bytes, independently of get_field and host floating-
> + point formats. */
> +
> +static void
> +put_field_test ()
> +{
> + struct test_case
> + {
> + floatformat_byteorders order;
> + unsigned int total_len;
> + unsigned int start;
> + unsigned int len;
> + unsigned long value;
> + unsigned char before[2];
> + unsigned char after[2];
> + };
> +
> + const test_case tests[] =
> + {
> + /* Writing the binary16 exponent must preserve the sign and fraction.
> + The first pair produces the encoding of -1.5. */
> + {floatformat_big, 16, 1, 5, 15, {0x82, 0x00}, {0xbe, 0x00}},
> + {floatformat_little, 16, 1, 5, 15, {0x00, 0x82}, {0x00, 0xbe}},
> + {floatformat_big, 16, 1, 5, 0, {0xff, 0xff}, {0x83, 0xff}},
> + {floatformat_little, 16, 1, 5, 0, {0xff, 0xff}, {0xff, 0x83}},
> + {floatformat_big, 16, 1, 5, 31, {0x80, 0x03}, {0xfc, 0x03}},
> + {floatformat_little, 16, 1, 5, 31, {0x03, 0x80}, {0x03, 0xfc}},
> +
> + /* Check fields that span bytes and end on a byte boundary. */
> + {floatformat_big, 16, 4, 12, 0x123, {0xff, 0xff}, {0xf1, 0x23}},
> + {floatformat_little, 16, 4, 12, 0x123, {0xff, 0xff}, {0x23, 0xf1}},
> + {floatformat_big, 16, 0, 16, 0xa55a, {0xff, 0xff}, {0xa5, 0x5a}},
> + {floatformat_little, 16, 0, 16, 0xa55a, {0xff, 0xff}, {0x5a, 0xa5}},
> +
> + /* A non-byte-sized format also exercises the little-endian excess
> + adjustment. Preserve its padding and neighboring field bits. */
> + {floatformat_big, 13, 1, 3, 0, {0xff, 0xff}, {0x8f, 0xff}},
> + {floatformat_little, 13, 1, 3, 0, {0xff, 0xff}, {0xff, 0xf1}},
> + {floatformat_big, 13, 1, 3, 5, {0xff, 0xff}, {0xdf, 0xff}},
> + {floatformat_little, 13, 1, 3, 5, {0xff, 0xff}, {0xff, 0xfb}},
> + {floatformat_big, 13, 1, 5, 0, {0xff, 0xff}, {0x83, 0xff}},
> + {floatformat_little, 13, 1, 5, 0, {0xff, 0xff}, {0x7f, 0xf0}}
> + };
> +
> + for (const test_case &test : tests)
> + {
> + unsigned char data[] = {0xa5, test.before[0], test.before[1], 0x5a};
> + put_field (data + 1, test.order, test.total_len, test.start, test.len,
> + test.value);
> + SELF_CHECK (data[0] == 0xa5);
> + SELF_CHECK (data[1] == test.after[0]);
> + SELF_CHECK (data[2] == test.after[1]);
> + SELF_CHECK (data[3] == 0x5a);
> + }
> +}
> +
> +} /* namespace selftests */
> +#endif /* GDB_SELF_TEST */
> +
> +INIT_GDB_FILE (target_float)
> +{
> +#if GDB_SELF_TEST
> + selftests::register_test ("target-float-put-field",
> + selftests::put_field_test);
> +#endif
> +}
> --
> 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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [PATCH v2 2/2] gdb: Preserve subnormal values in target floating-point operations
2026-11-09 21:24 ` [PATCH v2 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
@ 2026-11-10 10:40 ` Rohr, Stephan
0 siblings, 0 replies; 6+ messages in thread
From: Rohr, Stephan @ 2026-11-10 10:40 UTC (permalink / raw)
To: Robert Dubner, gdb-patches
Hi Robert,
thanks for the patch. I still have a few comments, see below.
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Monday, 9 November 2026 22:25
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH v2 2/2] gdb: Preserve subnormal values in target floating-
> point operations
>
> The MPFR encoder can flush representable subnormal values to zero or
> encode their significands incorrectly.
>
> Encode subnormals with a zero exponent field and align their fractions
> to the minimum normal exponent. Discard the implicit leading bit only
Nit: two spaces in "... exponent. Discard ...".
> for normal values.
>
> Carry MPFR's ternary results through parsing, conversions, and arithmetic
> to avoid double rounding. Temporarily adjust the minimum exponent while
Single space again.
> rounding, restoring it afterward. Preserve flush-to-zero behavior for
Single space.
> formats without subnormal support.
>
> Explain the subnormal boundary, minimum-exponent calculation, and
> significand scaling.
>
> Add unit tests in target-float.c covering both byte orders, exact
I think we don't have to mention that the unit tests go into "target-float.c"
explicitly.
> subnormals, rounding boundaries, narrowing conversions, arithmetic,
> explicit integer bits, and restoration of MPFR's exponent range.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
> ---
> gdb/target-float.c | 308 +++++++++++++++++++++++++++++++++++++++-
> -----
> 1 file changed, 272 insertions(+), 36 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 8032d4b7738..080b79d455c 100644
> --- a/gdb/target-float.c
> +++ b/gdb/target-float.c
> @@ -21,8 +21,13 @@
> #include "floatformat.h"
> #include "target-float.h"
> #include "gdbarch.h"
> +#include "gdbsupport/scope-exit.h"
> #include "gdbsupport/selftest.h"
>
> +#if GDB_SELF_TEST
> +#include "arch-utils.h"
> +#endif
> +
> /* Target floating-point operations.
>
> We provide multiple implementations of those operations, which differ
> @@ -1218,10 +1223,12 @@ class mpfr_float_ops : public target_float_ops
> void from_target (const struct type *type,
> const gdb_byte *from, gdb_mpfr &to) const;
>
> + /* INEXACT is the MPFR ternary result of the computation producing FROM.
> + Carry it into subnormal rounding to avoid rounding twice. */
> 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;
> };
>
>
> @@ -1344,12 +1351,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];
>
> @@ -1402,8 +1411,37 @@ mpfr_float_ops::to_target (const struct
> floatformat *fmt,
> {
> put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1);
> mpfr_neg (tmp.val, tmp.val, MPFR_RNDN);
> + inexact = -inexact;
> + }
> +
> + /* MPFR represents a nonzero finite value as M * 2^E, where
> + 0.5 <= abs (M) < 1. The smallest normal target value is
> + 2^(1 - exp_bias), so its MPFR exponent is 2 - exp_bias.
> + Smaller exponents (E + exp_bias <= 1) require subnormal rounding.
> + 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);
> + };
> +
> + /* With precision P, the least subnormal is
> + 2^(1 - exp_bias - (P - 1)). MPFR's exponent is one greater
> + than the power of two, giving emin = 3 - exp_bias - P.
> + Restrict the range only while rounding: the encoding below needs
> + intermediates outside the target's range (e.g. for binary16).
> + Use the operation's ternary result to avoid double rounding. */
> + 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))
> + goto finalize_byteorder;
> +
> if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */
> {
> /* Infinity exponent is same as NaN's. */
> @@ -1417,14 +1455,24 @@ 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;
Could you add a comment why we need to check for a subnormal once more?
IIUC, this is because the rounding above may produce the smallest normal number?
> + 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);
> + /* Subnormals use the minimum normal exponent, but have no leading
> + one. Scale the fraction returned by mpfr_frexp to that exponent,
> + leaving the leading zero explicit, even for formats with an
> + implicit integer bit. The loop below extracts successive 32-bit
> + chunks of this fraction. An explicit integer bit occupies its
> + first bit, so shift one place further right for those formats. */
> + 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;
> }
>
> @@ -1440,7 +1488,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;
> @@ -1454,11 +1502,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;
> @@ -1495,12 +1540,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
Nit: Line too long.
> {
> /* 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,
> @@ -1558,13 +1603,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;
> }
>
> @@ -1587,8 +1632,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
> @@ -1599,8 +1644,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,
> @@ -1622,8 +1667,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
> @@ -1637,8 +1682,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
> @@ -1656,34 +1701,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:
> @@ -1691,7 +1737,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
> @@ -2555,6 +2601,190 @@ put_field_test ()
> }
> }
>
> +/* Exercise the MPFR implementation directly, independently of the host's
> + floating-point formats. Each pair contains big- and little-endian forms. */
Nit: exceeds the 80 characters limit.
> +
> +static const floatformat *const subnormal_formats[][2] =
> + {
> + {&floatformat_ieee_half_big, &floatformat_ieee_half_little},
> + {&floatformat_ieee_single_big, &floatformat_ieee_single_little},
> + {&floatformat_ieee_double_big, &floatformat_ieee_double_little},
> + {&floatformat_ieee_quad_big, &floatformat_ieee_quad_little}
> + };
> +
> +static type *
> +subnormal_type (const floatformat *fmt)
> +{
> + /* Supply FMT in both slots so it can be selected independently of the
> + architecture's byte order. init_float_type retains only that format. */
> + const floatformat *pair[] = {fmt, fmt};
> + type_allocator alloc (get_current_arch ());
> + return init_float_type (alloc, -1, "selftest_float", pair, BFD_ENDIAN_BIG);
> +}
> +
> +/* Encode integer BITS independently of the floating-point encoder. */
> +
> +static std::vector<gdb_byte>
> +subnormal_bytes (const gdb_mpz &bits, const type *ftype)
> +{
> + std::vector<gdb_byte> bytes (ftype->length ());
> + bfd_endian order = floatformat_from_type (ftype)->byteorder ==
> floatformat_big
> + ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
Nit: I think the line is too long. Also, I think we need parentheses:
bfd_endian order
= (floatformat_from_type (ftype)->byteorder == floatformat_big
? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
> + bits.write (bytes, order, true);
> + return bytes;
> +}
> +
> +/* Spell COEFFICIENT * 2^EXPONENT exactly, without host floating-point
> math. */
Nit: line exceeds 80 characters.
> +
> +static std::string
> +subnormal_string (const gdb_mpz &coefficient, int exponent)
> +{
> + return coefficient.hex () + "p" + std::to_string (exponent);
> +}
> +
> +/* Check exact subnormals, text round trips, byte-order conversion, and the
> + boundary between normal and subnormal arithmetic. */
> +
> +static void
> +subnormal_test ()
> +{
> + mpfr_float_ops ops;
> + for (const auto &formats : subnormal_formats)
> + for (int endian = 0; endian < 2; ++endian)
> + {
> + const floatformat *fmt = formats[endian];
> + type *ftype = subnormal_type (fmt);
> + type *opposite = subnormal_type (formats[1 - endian]);
> + int quantum = 1 - fmt->exp_bias - (int) fmt->man_len;
> + gdb_mpz normal = gdb_mpz (1) << fmt->man_len;
> + gdb_mpz sign = gdb_mpz (1) << (fmt->totalsize - 1);
> + for (const gdb_mpz &magnitude : {gdb_mpz (1), gdb_mpz (2),
> + normal - gdb_mpz (1), normal})
> + for (bool negative : {false, true})
> + {
> + gdb_mpz bits = negative ? magnitude | sign : magnitude;
> + auto expected = subnormal_bytes (bits, ftype);
> + std::vector<gdb_byte> actual (ftype->length ());
> + std::string text = subnormal_string (magnitude, quantum);
> + if (negative)
> + text.insert (0, "-");
> + SELF_CHECK (ops.from_string (actual.data (), ftype, text));
> + SELF_CHECK (actual == expected);
> + text = ops.to_string (expected.data (), ftype, nullptr);
> + SELF_CHECK (ops.from_string (actual.data (), ftype, text));
> + SELF_CHECK (actual == expected);
> + ops.convert (expected.data (), ftype, actual.data (), opposite);
> + SELF_CHECK (actual == subnormal_bytes (bits, opposite));
> + }
> +
> + auto least = subnormal_bytes (gdb_mpz (1), ftype);
> + auto largest = subnormal_bytes (normal - gdb_mpz (1), ftype);
> + auto minimum = subnormal_bytes (normal, ftype);
> + std::vector<gdb_byte> actual (ftype->length ());
> + ops.binop (BINOP_SUB, minimum.data (), ftype, least.data (), ftype,
> + actual.data (), ftype);
> + SELF_CHECK (actual == largest);
> + ops.binop (BINOP_ADD, largest.data (), ftype, least.data (), ftype,
> + actual.data (), ftype);
> + SELF_CHECK (actual == minimum);
> + }
> +}
> +
> +/* Test halfway values and both sides of each tie, including digits lost by
> + rounding to normal precision before subnormal rounding. Expected results
> + follow round-to-nearest, ties-to-even, without using MPFR as an oracle. */
> +
> +static void
> +subnormal_rounding_test ()
> +{
> + mpfr_float_ops ops;
> + mpfr_exp_t saved_emin = mpfr_get_emin (), saved_emax = mpfr_get_emax
> ();
> + for (const auto &formats : subnormal_formats)
> + for (const floatformat *fmt : formats)
> + {
> + type *ftype = subnormal_type (fmt);
> + type *wide = subnormal_type (&floatformat_ieee_quad_big);
We can move the "wide" type above the loop to avoid additional allocations.
> + int quantum = 1 - fmt->exp_bias - (int) fmt->man_len;
> + int tail = fmt->man_len + 9;
> + gdb_mpz normal = gdb_mpz (1) << fmt->man_len;
> + gdb_mpz sign = gdb_mpz (1) << (fmt->totalsize - 1);
> + for (const gdb_mpz &lower : {gdb_mpz (0), gdb_mpz (1), gdb_mpz
> (2),
> + normal - gdb_mpz (1)})
> + for (int side : {-1, 0, 1})
> + for (bool negative : {false, true})
> + {
> + gdb_mpz coefficient = ((lower * gdb_mpz (2) + 1) << (tail - 1))
> + + gdb_mpz (side);
> + gdb_mpz expected = lower;
> + if (side > 0 || (side == 0 && (lower & gdb_mpz (1)) == 1))
> + expected += 1;
> + if (negative)
> + expected = expected | sign;
> + auto bytes = subnormal_bytes (expected, ftype);
> + std::string text
> + = subnormal_string (coefficient, quantum - tail);
> + if (negative)
> + text.insert (0, "-");
> + std::vector<gdb_byte> actual (ftype->length ());
> + SELF_CHECK (ops.from_string (actual.data (), ftype, text));
> + SELF_CHECK (actual == bytes);
> +
> + /* Binary128 represents these binary16/32/64 cases exactly.
> + Check the ternary result from narrowing and each operation,
> + as well as parsing directly at the destination precision. */
Nit: line too long.
> + if (fmt->man_len < floatformat_ieee_quad_big.man_len)
> + {
> + std::vector<gdb_byte> source (wide->length ());
> + SELF_CHECK (ops.from_string (source.data (), wide, text));
> + ops.convert (source.data (), wide, actual.data (), ftype);
> + SELF_CHECK (actual == bytes);
> + for (exp_opcode op : {BINOP_ADD, BINOP_SUB,
> BINOP_MUL,
> + BINOP_DIV, BINOP_EXP,
> BINOP_MIN, BINOP_MAX})
Nit: line too long.
> + {
> + std::vector<gdb_byte> right (wide->length ());
> + LONGEST rhs = op == BINOP_ADD || op ==
> BINOP_SUB ? 0
> + : op == BINOP_MAX ? -1 : 1;
> + ops.from_longest (right.data (), wide, rhs);
> + ops.binop (op, source.data (), wide, right.data (),
> wide,
Nit: line too long.
> + actual.data (), ftype);
> + SELF_CHECK (actual == bytes);
> + }
> + }
> + SELF_CHECK (mpfr_get_emin () == saved_emin);
> + SELF_CHECK (mpfr_get_emax () == saved_emax);
> + }
> + }
> +}
> +
> +/* Also check encoding with explicit integer bits and preserve the existing
> + flush-to-zero policy of formats without subnormal support. */
> +
> +static void
> +subnormal_other_formats_test ()
> +{
> + mpfr_float_ops ops;
> + for (const floatformat *fmt : {&floatformat_i387_ext,
> + &floatformat_m68881_ext})
AFAIK, the smallest subnormal in M68881 is one less compared to x87 format.
I think the test passes as "to_target" does not handle Motorola's special case.
I would prefer to drop the M68881 case here. A separate test could be added
once the M68881 format is properly handled.
> + {
> + type *ftype = subnormal_type (fmt);
> + std::vector<gdb_byte> actual (ftype->length ());
> + std::vector<gdb_byte> expected (ftype->length ());
> + expected[fmt->byteorder == floatformat_little ? 0 : 11] = 1;
> + int quantum = 2 - fmt->exp_bias - floatformat_precision (fmt);
> + SELF_CHECK (ops.from_string (actual.data (), ftype,
> + subnormal_string (gdb_mpz (1),
> quantum)));
> + SELF_CHECK (actual == expected);
> + }
> +
> + type *ftype = subnormal_type (&floatformat_vax_f);
> + std::vector<gdb_byte> actual (ftype->length ());
> + std::vector<gdb_byte> zero (ftype->length ());
> + SELF_CHECK (ops.from_string (actual.data (), ftype, "0x1p-200"));
> + SELF_CHECK (actual == zero);
> + SELF_CHECK (ops.from_string (actual.data (), ftype, "0.125"));
> + SELF_CHECK (actual == std::vector<gdb_byte> ({0, 0x3f, 0, 0}));
> +}
> +
> } /* namespace selftests */
> #endif /* GDB_SELF_TEST */
>
> @@ -2563,5 +2793,11 @@ INIT_GDB_FILE (target_float)
> #if GDB_SELF_TEST
> selftests::register_test ("target-float-put-field",
> selftests::put_field_test);
> + selftests::register_test ("target-float-subnormal",
> + selftests::subnormal_test);
> + selftests::register_test ("target-float-subnormal-rounding",
> + selftests::subnormal_rounding_test);
> + selftests::register_test ("target-float-subnormal-other-formats",
> + selftests::subnormal_other_formats_test);
> #endif
> }
> --
> 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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields
2026-11-10 10:23 ` Rohr, Stephan
@ 2026-11-11 4:03 ` Robert Dubner
0 siblings, 0 replies; 6+ messages in thread
From: Robert Dubner @ 2026-11-11 4:03 UTC (permalink / raw)
To: Rohr, Stephan, gdb-patches
> -----Original Message-----
> From: Rohr, Stephan <stephan.rohr@intel.com>
> Sent: Tuesday, November 10, 2026 05:23
> To: Robert Dubner <rdubner@symas.com>; gdb-patches@sourceware.org
> Subject: RE: [PATCH v2 1/2] gdb: Preserve neighboring bits when writing
> floating-point fields
>
> Hi Robert,
>
> thanks for fixing this.
>
> What happened to the signed zero patch?
I set it aside. I was getting confused just struggling with these two.
I think I mentioned that my experience with GCC, where I mostly just
self-commit changes to the COBOL front end code, hasn't prepared me for
offering a patch as an ordinary user. The reviews you have done and the
suggestions for improvement have been absolutely invaluable. And it
encouraged me to do some things that needed doing.
I had never configured my development machine for sendmail; that now works.
I've never used "git send-email". I have never needed it; I simply push
stuff into the GCC repository and just e-mail (for the record) the
`format-patch` output with the subject marked as "committed". So I didn't
even know about the incorrect formatting I was creating. (It turned out to
be because of my company's commercial e-mail handler; it's reaching into
messages and adding newlines, if you can believe it.) "git send-email" is,
I believe, working because the Ubuntu box is talking directly to an SMTP
outgoing sender without using the Windows/Outlook/ThirdParty chain that
otherwise corrupts the messages.
I now know more about GIT; I had never, for example, done an interactive
rebase with "edit" as the per-commit option. I now have some idea of GDB's
preferences about commit messages.
So, with what is hopefully the final version of these two patches ready for
you, I can return to the separate signed zero fix. And maybe I can get it
right closer to the first attempt.
Thanks again!
Bob Dubner
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-11 4:04 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 21:24 [PATCH v2 0/2] gdb: Fix floating-point field writes and subnormal results Robert Dubner
2026-11-09 21:24 ` [PATCH v2 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-10 10:23 ` Rohr, Stephan
2026-11-11 4:03 ` Robert Dubner
2026-11-09 21:24 ` [PATCH v2 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
2026-11-10 10:40 ` Rohr, Stephan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox