From: Robert Dubner <rdubner@symas.com>
To: "Rohr, Stephan" <stephan.rohr@intel.com>, <gdb-patches@sourceware.org>
Subject: RE: [PATCH v4 5/5] gdb: Avoid oversized shifts when writing floating-point fields
Date: Thu, 12 Nov 2026 11:05:58 -0600 (CST) [thread overview]
Message-ID: <011701dd72c8$169fa5d0$43def170$@symas.com> (raw)
In-Reply-To: <IA3PR11MB444632654982C79263880AE1A393702@IA3PR11MB444632.namprd11.prod.outlook.com>
> -----Original Message-----
> From: Rohr, Stephan <stephan.rohr@intel.com>
> Sent: Thursday, November 12, 2026 04:48
> To: Robert Dubner <rdubner@symas.com>; gdb-patches@sourceware.org
> Subject: RE: [PATCH v4 5/5] gdb: Avoid oversized shifts when writing
> floating-point fields
>
> Hi Robert,
>
> thanks for the patch. A few comments, see below.
>
> The patch is new in this series and wasn't posted before?
I actually thought I had posted it before. As we have worked through this
here, I have been forced to focus on multiple unrelated things and I have
gotten confused more than once.
>
> Please don't carry the "Reviewed-By" from one patch to a new
> patch when sending a new version of the series.
>
> Thanks
> Stephan
>
> Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>
>
> > -----Original Message-----
> > From: Robert Dubner <rdubner@symas.com>
> > Sent: Wednesday, 11 November 2026 18:43
> > To: gdb-patches@sourceware.org
> > Cc: Rohr, Stephan <stephan.rohr@intel.com>
> > Subject: [PATCH v4 5/5] gdb: Avoid oversized shifts when writing
> > floating-
> > point fields
> >
> > put_field can write fields wider than unsigned long, such as the
> > 112-bit binary128 fraction field used when encoding infinities and
> > NaNs. Its loop can shift stuff_to_put by its width or more, which is
> > undefined even when the value is zero.
> >
> > Use zero once the shift reaches the width of unsigned long. Apply
> > the guarded value to both full-byte and partial-byte writes,
> > preserving the existing neighboring-bit masks.
> >
> > Add unit tests in target-float.c covering wide fields, alignment,
> > both byte orders, neighboring bits, buffer guards, and binary128
> > infinity and NaN encodings.
> >
> > Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>
> > ---
> > gdb/target-float.c | 102
> > +++++++++++++++++++++++++++++++++++++++++++--
> > 1 file changed, 99 insertions(+), 3 deletions(-)
> >
> > diff --git a/gdb/target-float.c b/gdb/target-float.c
> > index fc0b7931860..068310932f0 100644
> > --- a/gdb/target-float.c
> > +++ b/gdb/target-float.c
> > @@ -245,6 +245,7 @@ put_field (unsigned char *data, enum
> > floatformat_byteorders order,
> > {
> > unsigned int cur_byte;
> > int cur_bitshift;
> > + constexpr int value_bits = sizeof (stuff_to_put) *
> > FLOATFORMAT_CHAR_BIT;
> >
> > /* Caller must byte-swap words before calling this routine. */
> > gdb_assert (order == floatformat_little || order == floatformat_big);
> > @@ -282,17 +283,21 @@ put_field (unsigned char *data, enum
> > floatformat_byteorders order,
> > /* Move towards the most significant part of the field. */
> > while (cur_bitshift < len)
> > {
> > + /* Fields such as a binary128 fraction can be wider than
> > STUFF_TO_PUT.
> > + Zero-extend rather than shifting by the width of unsigned long or
> > + more. */
> > + unsigned long shifted
> > + = cur_bitshift < value_bits ? stuff_to_put >> cur_bitshift : 0;
> > if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
> > {
> > /* 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;
> > + *(data + cur_byte) |= shifted & mask;
> > }
> > else
> > - *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
> > - & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
> > + *(data + cur_byte) = shifted & ((1 << FLOATFORMAT_CHAR_BIT) - 1);
> > cur_bitshift += FLOATFORMAT_CHAR_BIT;
> > if (order == floatformat_little)
> > ++cur_byte;
> > @@ -2927,6 +2932,93 @@ signed_zero_test ()
> > }
> > }
> >
> > +/* Check zero extension when a field is wider than unsigned long.
> > Build
> > + expected bytes with arbitrary-precision integer operations,
> > independently
> > + of put_field and get_field, and preserve neighboring bits and
> > guards. */
> > +
> > +static void
> > +put_field_wide_test ()
> > +{
> > + constexpr unsigned int value_bits
> > + = sizeof (unsigned long) * FLOATFORMAT_CHAR_BIT;
> > + struct test_case
> > + {
> > + unsigned int start;
> > + unsigned int len;
> > + };
> > + const test_case tests[] =
> > + {
> > + {0, 128},
> > + {16, 112}, /* The binary128 fraction field. */
> > + {15, 112},
> > + {12, 113},
> > + {8, value_bits},
> > + {7, value_bits},
> > + {7, value_bits + 1},
> > + {0, value_bits + 8}
> > + };
> > +
> > + for (auto order : {floatformat_big, floatformat_little})
> > + for (const test_case &test : tests)
> > + for (unsigned long value : {0UL, 1UL, 0xa5UL, ~0UL,
> > + 1UL << (value_bits - 1)})
> > + for (gdb_byte initial : {0x00, 0xa5, 0xff})
> > + {
> > + gdb_byte data[18];
> > + memset (data, initial, sizeof (data));
> > + data[0] = 0x3c;
> > + data[17] = 0xc3;
> > + std::vector<gdb_byte> expected (16, initial);
> > + bfd_endian byte_order = order == floatformat_big
> > + ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
>
> Nit: this is not correctly indented. Also, it is missing parathesis. The
> continuation
> should start at the next line.
>
> > + unsigned int shift = 128 - test.start - test.len;
> > + gdb_mpz image;
> > + image.read (expected, byte_order, true);
> > + gdb_mpz mask = ((gdb_mpz (1) << test.len) - gdb_mpz (1)) << shift;
> > + mask.complement ();
> > + image = (image & mask) | (gdb_mpz (value) << shift);
> > + image.write (expected, byte_order, true);
> > +
> > + put_field (data + 1, order, 128, test.start, test.len, value);
> > + SELF_CHECK (data[0] == 0x3c);
> > + SELF_CHECK (std::equal (expected.begin (), expected.end (),
> > + data + 1));
> > + SELF_CHECK (data[17] == 0xc3);
> > + }
> > +}
> > +
> > +/* The MPFR encoder writes a complete binary128 fraction for infinities
> > and
> > + NaNs. Exercise that caller as well as the field writer itself. */
> > +
> > +static void
> > +wide_special_test ()
> > +{
> > + mpfr_float_ops ops;
> > + type_allocator alloc (get_current_arch ());
> > + for (const floatformat *fmt : {&floatformat_ieee_quad_big,
> > + &floatformat_ieee_quad_little})
> > + {
> > + const floatformat *pair[] = {fmt, fmt};
>
> Patch 2/5 introduced "subnormal_type (const floatformat *fmt)". As this
> patch
> comes after this, we can use it here, too.
>
> > + type *ftype = init_float_type (alloc, -1, "selftest_quad", pair,
> > + BFD_ENDIAN_BIG);
> > + bfd_endian order = fmt->byteorder == floatformat_big
> > + ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
>
> Nit: same here.
>
> > + for (const char *text : {"inf", "-inf", "nan"})
> > + {
> > + gdb_mpz bits = gdb_mpz (0x7fff) << 112;
> > + if (text[0] == '-')
> > + bits = bits | (gdb_mpz (1) << 127);
> > + else if (text[0] == 'n')
> > + bits += 1;
> > + std::vector<gdb_byte> expected (16);
> > + bits.write (expected, order, true);
> > + std::vector<gdb_byte> actual (16, 0xa5);
> > + SELF_CHECK (ops.from_string (actual.data (), ftype, text));
> > + SELF_CHECK (actual == expected);
> > + }
> > + }
> > +}
> > +
> > } /* namespace selftests */
> > #endif /* GDB_SELF_TEST */
> >
> > @@ -2937,6 +3029,10 @@ INIT_GDB_FILE (target_float)
> > selftests::put_field_test);
> > selftests::register_test ("target-float-signed-zero",
> > selftests::signed_zero_test);
> > + selftests::register_test ("target-float-put-field-wide",
> > + selftests::put_field_wide_test);
> > + selftests::register_test ("target-float-wide-specials",
> > + selftests::wide_special_test);
> > selftests::register_test ("target-float-subnormal",
> > selftests::subnormal_test);
> > selftests::register_test ("target-float-subnormal-rounding",
> > --
> > 2.34.1
>
> ________________________________________
> Intel Deutschland GmbH
>
> Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
>
> Tel: +49 (89) 99143-0
>
> www.intel.de
>
> Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran
> Sitaraman
>
> Chairperson of the Supervisory Board: Sonja Pierer
>
> Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB
> 186928
>
> This e-mail and any attachments may contain confidential material for
> the sole use of the intended recipient(s). Any review or distribution
> by others is strictly prohibited. If you are not the intended
> recipient, please contact the sender and delete all copies.
prev parent reply other threads:[~2026-11-12 17:06 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 17:42 [PATCH v4 0/5] gdb: Fix target floating-point encoding and signed-zero negation Robert Dubner
2026-11-11 17:42 ` [PATCH v4 1/5] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-12 9:04 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 2/5] gdb: Preserve subnormal values in target floating-point operations Robert Dubner
2026-11-12 9:25 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 3/5] gdb: Preserve signed zero in MPFR target conversions Robert Dubner
2026-11-12 9:30 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 4/5] gdb: Preserve signed zero in floating-point unary negation Robert Dubner
2026-11-12 9:32 ` Rohr, Stephan
2026-11-11 17:42 ` [PATCH v4 5/5] gdb: Avoid oversized shifts when writing floating-point fields Robert Dubner
2026-11-12 9:48 ` Rohr, Stephan
2026-11-12 17:05 ` Robert Dubner [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='011701dd72c8$169fa5d0$43def170$@symas.com' \
--to=rdubner@symas.com \
--cc=gdb-patches@sourceware.org \
--cc=stephan.rohr@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox