* [PATCH v3 0/2] Subject: [PATCH v3 0/2] gdb: Fix floating-point field writes and subnormal results
@ 2026-11-11 3:39 Robert Dubner
2026-11-11 3:39 ` [PATCH v3 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-11 3:39 ` [PATCH v3 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
0 siblings, 2 replies; 5+ messages in thread
From: Robert Dubner @ 2026-11-11 3:39 UTC (permalink / raw)
To: gdb-patches; +Cc: stephan.rohr
This series addresses two floating-point encoding problems described in
PR gdb/34927.
The first patch prevents put_field from changing bits outside the
requested field, including the sign bit adjacent to a binary16 exponent.
The second patch corrects subnormal encoding and carries MPFR's ternary
rounding results through conversions and arithmetic to avoid double
rounding.
Both patches include unit tests directly in target-float.c.
Changes in v3:
- Mask the final partial byte without shifting by the full field width.
- Add a 32-bit field test with unaligned endpoints in both byte orders.
- Explain why subnormal classification is repeated after rounding.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
Many thanks to Stephan Rohr for the review and guidance.
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 | 436 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 388 insertions(+), 48 deletions(-)
base-commit: 9db3b515b964c1bbdc4d20c1202fbf27719be269
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3 1/2] gdb: Preserve neighboring bits when writing floating-point fields
2026-11-11 3:39 [PATCH v3 0/2] Subject: [PATCH v3 0/2] gdb: Fix floating-point field writes and subnormal results Robert Dubner
@ 2026-11-11 3:39 ` Robert Dubner
2026-11-11 13:16 ` Rohr, Stephan
2026-11-11 3:39 ` [PATCH v3 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
1 sibling, 1 reply; 5+ messages in thread
From: Robert Dubner @ 2026-11-11 3:39 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 | 117 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 106 insertions(+), 11 deletions(-)
diff --git a/gdb/target-float.c b/gdb/target-float.c
index 31b5ac642a7..836f79b8e31 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 is trimmed to 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)
@@ -277,10 +279,11 @@ put_field (unsigned char *data, enum floatformat_byteorders order,
{
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);
+ /* This is the last byte. Make sure we don't OR in extra high-order
+ bits when STUFF_TO_PUT violates the LEN restriction. */
+ unsigned int mask = (1U << (len - cur_bitshift)) - 1;
+ *(data + cur_byte) &= ~mask;
+ *(data + cur_byte) |= (stuff_to_put >> cur_bitshift) & mask;
}
else
*(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
@@ -2491,3 +2494,95 @@ 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}},
+
+ /* Make sure the code forcing stuff_to_put fits in len bits works. */
+ {floatformat_big, 16, 6, 10, 0x7ff, {0x00, 0x00}, {0x03, 0xff}},
+ {floatformat_little, 16, 6, 10, 0x7ff, {0x00, 0x00}, {0xff, 0x03}},
+ };
+
+ 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);
+ }
+
+ /* A 32-bit field that starts and ends partway through a byte. */
+ for (auto order : {floatformat_big, floatformat_little})
+ {
+ unsigned char data[] = {0x12, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0x34};
+ unsigned char expected[] = {0xaf, 0xed, 0xcb, 0xa9, 0x85};
+
+ if (order == floatformat_little)
+ std::reverse (expected, expected + sizeof (expected));
+
+ put_field (data + 1, order, 40, 4, 32, 0xfedcba98UL);
+
+ SELF_CHECK (data[0] == 0x12);
+ SELF_CHECK (std::equal (expected, expected + sizeof (expected),
+ data + 1));
+ SELF_CHECK (data[6] == 0x34);
+ }
+}
+} /* 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] 5+ messages in thread
* [PATCH v3 2/2] gdb: Preserve subnormal values in target floating-point operations
2026-11-11 3:39 [PATCH v3 0/2] Subject: [PATCH v3 0/2] gdb: Fix floating-point field writes and subnormal results Robert Dubner
2026-11-11 3:39 ` [PATCH v3 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
@ 2026-11-11 3:39 ` Robert Dubner
2026-11-11 13:16 ` Rohr, Stephan
1 sibling, 1 reply; 5+ messages in thread
From: Robert Dubner @ 2026-11-11 3:39 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 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 | 319 +++++++++++++++++++++++++++++++++++++++------
1 file changed, 282 insertions(+), 37 deletions(-)
diff --git a/gdb/target-float.c b/gdb/target-float.c
index 836f79b8e31..e9d9e283a48 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
@@ -1219,10 +1224,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;
};
@@ -1345,12 +1352,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];
@@ -1403,8 +1412,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. */
@@ -1418,14 +1456,28 @@ mpfr_float_ops::to_target (const struct floatformat *fmt,
mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN);
- if (exponent + fmt->exp_bias <= 0)
- {
- /* 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);
+ /* Test to see if the value is subnormal after it was rounded. For example,
+ a value halfway between the largest subnormal and the smallest normal
+ tests as subnormal up above, but after rounding will test as normal
+ here. So this second test has to be done.*/
+ subnormal = fmt->exp_nan != 0 && exponent + fmt->exp_bias <= 1;
+ if (subnormal)
+ {
+ /* 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;
}
@@ -1441,7 +1493,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 +1507,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 +1545,13 @@ 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 +1609,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 +1638,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 +1650,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 +1673,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 +1688,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 +1707,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 +1743,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
@@ -2576,6 +2627,194 @@ put_field_test ()
SELF_CHECK (data[6] == 0x34);
}
}
+/* 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 ();
+ type *wide = subnormal_type (&floatformat_ieee_quad_big);
+ for (const auto &formats : subnormal_formats)
+ for (const floatformat *fmt : formats)
+ {
+ type *ftype = subnormal_type (fmt);
+ 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})
+ {
+ 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 */
@@ -2584,5 +2823,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] 5+ messages in thread
* RE: [PATCH v3 1/2] gdb: Preserve neighboring bits when writing floating-point fields
2026-11-11 3:39 ` [PATCH v3 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
@ 2026-11-11 13:16 ` Rohr, Stephan
0 siblings, 0 replies; 5+ messages in thread
From: Rohr, Stephan @ 2026-11-11 13:16 UTC (permalink / raw)
To: Robert Dubner, gdb-patches
Hi Robert,
I have a few small nits, please see below.
When submitting v4, you can add the Reviewed-By tag to the commit
message:
Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Wednesday, 11 November 2026 04:40
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH v3 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 | 117
> ++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 106 insertions(+), 11 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 31b5ac642a7..836f79b8e31 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 is trimmed to 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)
> @@ -277,10 +279,11 @@ put_field (unsigned char *data, enum
> floatformat_byteorders order,
> {
> 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);
> + /* This is the last byte. Make sure we don't OR in extra high-order
> + bits when STUFF_TO_PUT violates the LEN restriction. */
Nit: dot-space-space at the end of the comment, e.g.:
... violates the LEN restriction. */
> + unsigned int mask = (1U << (len - cur_bitshift)) - 1;
> + *(data + cur_byte) &= ~mask;
> + *(data + cur_byte) |= (stuff_to_put >> cur_bitshift) & mask;
> }
> else
> *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
> @@ -2491,3 +2494,95 @@ 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}},
> +
> + /* Make sure the code forcing stuff_to_put fits in len bits works. */
What do you mean by "forcing"? Maybe rephrase to something like "Make
sure STUFF_TO_PUT does not change bits outside the field."
> + {floatformat_big, 16, 6, 10, 0x7ff, {0x00, 0x00}, {0x03, 0xff}},
> + {floatformat_little, 16, 6, 10, 0x7ff, {0x00, 0x00}, {0xff, 0x03}},
> + };
> +
> + 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);
> + }
> +
> + /* A 32-bit field that starts and ends partway through a byte. */
> + for (auto order : {floatformat_big, floatformat_little})
> + {
> + unsigned char data[] = {0x12, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0x34};
> + unsigned char expected[] = {0xaf, 0xed, 0xcb, 0xa9, 0x85};
> +
> + if (order == floatformat_little)
> + std::reverse (expected, expected + sizeof (expected));
> +
> + put_field (data + 1, order, 40, 4, 32, 0xfedcba98UL);
> +
> + SELF_CHECK (data[0] == 0x12);
> + SELF_CHECK (std::equal (expected, expected + sizeof (expected),
> + data + 1));
Nit: this should be indented to "expected":
SELF_CHECK (std::equal (expected, expected + sizeof (expected),
data + 1));
> + SELF_CHECK (data[6] == 0x34);
> + }
> +}
Nit: add one blank new line.
> +} /* 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] 5+ messages in thread
* RE: [PATCH v3 2/2] gdb: Preserve subnormal values in target floating-point operations
2026-11-11 3:39 ` [PATCH v3 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
@ 2026-11-11 13:16 ` Rohr, Stephan
0 siblings, 0 replies; 5+ messages in thread
From: Rohr, Stephan @ 2026-11-11 13:16 UTC (permalink / raw)
To: Robert Dubner, gdb-patches
Hi Robert,
I have a few small nits, please see below.
When submitting v4, you can add the Reviewed-By tag to the commit
message:
Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Wednesday, 11 November 2026 04:40
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH v3 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
> 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 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 | 319 +++++++++++++++++++++++++++++++++++++++-
> -----
> 1 file changed, 282 insertions(+), 37 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 836f79b8e31..e9d9e283a48 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
> @@ -1219,10 +1224,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;
> };
>
>
> @@ -1345,12 +1352,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];
>
> @@ -1403,8 +1412,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. */
> @@ -1418,14 +1456,28 @@ mpfr_float_ops::to_target (const struct
> floatformat *fmt,
>
> mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN);
>
> - if (exponent + fmt->exp_bias <= 0)
> - {
> - /* 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);
> + /* Test to see if the value is subnormal after it was rounded. For example,
> + a value halfway between the largest subnormal and the smallest normal
> + tests as subnormal up above, but after rounding will test as normal
Nit: "up above" -> "above".
> + here. So this second test has to be done.*/
Nit: dot-space-space.
> + subnormal = fmt->exp_nan != 0 && exponent + fmt->exp_bias <= 1;
> + if (subnormal)
> + {
> + /* 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;
> }
>
> @@ -1441,7 +1493,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 +1507,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 +1545,13 @@ 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 +1609,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 +1638,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 +1650,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 +1673,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 +1688,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 +1707,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 +1743,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
> @@ -2576,6 +2627,194 @@ put_field_test ()
> SELF_CHECK (data[6] == 0x34);
> }
> }
> +/* 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 =
Nit: The "=" should be moved to the next line, e.g.,
bfd_endian order
= (floatformat_from_type (ftype)->byteorder == floatformat_big
? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
> + (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
> ();
> + type *wide = subnormal_type (&floatformat_ieee_quad_big);
> + for (const auto &formats : subnormal_formats)
> + for (const floatformat *fmt : formats)
> + {
> + type *ftype = subnormal_type (fmt);
> + 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})
I think we can remove the loop. If we enable the Motorola format,
we'd need to handle this separately anyways.
> + {
> + 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;
With the loop removed, this is expected[0] = 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 */
>
> @@ -2584,5 +2823,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] 5+ messages in thread
end of thread, other threads:[~2026-11-11 13:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-11 3:39 [PATCH v3 0/2] Subject: [PATCH v3 0/2] gdb: Fix floating-point field writes and subnormal results Robert Dubner
2026-11-11 3:39 ` [PATCH v3 1/2] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-11 13:16 ` Rohr, Stephan
2026-11-11 3:39 ` [PATCH v3 2/2] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
2026-11-11 13:16 ` Rohr, Stephan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox