gdb: Preserve neighboring bits when writing floating-point fields
Checks
Commit Message
From 2c1ada5e34da71f2575d7857ae78db559a972126 Mon Sep 17 00:00:00 2001
From: Robert Dubner <rdubner@symas.com>
Date: Tue, 3 Nov 2026 13:12:57 -0500
Subject: [PATCH] gdb: Preserve neighboring bits when writing
floating-point
fields
put_field can clear bits outside the requested field when the field
fits within 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 so it accounts for the little-endian
adjustment for formats whose total size is not a multiple of eight bits.
Also correct the function comment to describe lengths in bits and
storing into the buffer, and document that STUFF_TO_PUT must fit in
LEN bits.
With a volatile _Float16 variable f16, before this change:
(gdb) set var f16 = -1.5
(gdb) print f16
$1 = 1.5
After this change:
(gdb) set var f16 = -1.5
(gdb) print f16
$1 = -1.5
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
---
gdb/target-float.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
put_field (unsigned char *data, enum floatformat_byteorders order,
unsigned int total_len, unsigned int start, unsigned int len,
@@ -260,11 +261,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)
Comments
Hi Robert,
thanks for the patch.
I think the implementation and the commit message looks good now.
It would be nice to add a test for this. I'd suggest adding a unit-test
in target-float.c.
Please format the patches such that they can be correctly applied:
git format-patch -v2 --cover-letter --base=auto -o <out dir>
Then, use git send-email to send:
git send-email --suppress-cc=all --confirm=always --validate \
--to=gdb-patches@sourceware.org --cc=.. <dir>
Thanks
Stephan
> -----Original Message-----
> From: Robert Dubner <rdubner@symas.com>
> Sent: Tuesday, 3 November 2026 19:39
> To: gdb-patches@sourceware.org
> Cc: Rohr, Stephan <stephan.rohr@intel.com>
> Subject: [PATCH] gdb: Preserve neighboring bits when writing floating-point
> fields
>
> From 2c1ada5e34da71f2575d7857ae78db559a972126 Mon Sep 17
> 00:00:00 2001
> From: Robert Dubner <rdubner@symas.com>
> Date: Tue, 3 Nov 2026 13:12:57 -0500
> Subject: [PATCH] gdb: Preserve neighboring bits when writing
> floating-point
> fields
>
> put_field can clear bits outside the requested field when the field
> fits within 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 so it accounts for the little-endian
> adjustment for formats whose total size is not a multiple of eight bits.
>
> Also correct the function comment to describe lengths in bits and
> storing into the buffer, and document that STUFF_TO_PUT must fit in
> LEN bits.
>
> With a volatile _Float16 variable f16, before this change:
>
> (gdb) set var f16 = -1.5
> (gdb) print f16
> $1 = 1.5
>
> After this change:
>
> (gdb) set var f16 = -1.5
> (gdb) print f16
> $1 = -1.5
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
> ---
> gdb/target-float.c | 15 ++++++++-------
> 1 file changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index b278295138e..bf86a158fa0 100644
> --- a/gdb/target-float.c
> +++ b/gdb/target-float.c
> @@ -229,8 +229,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 +261,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)
> --
> 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.
@@ -229,8 +229,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