Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb: Preserve neighboring bits when writing floating-point fields
@ 2026-11-03 18:38 Robert Dubner
  2026-11-05 12:48 ` Rohr, Stephan
  0 siblings, 1 reply; 2+ messages in thread
From: Robert Dubner @ 2026-11-03 18:38 UTC (permalink / raw)
  To: gdb-patches; +Cc: 'Rohr, Stephan'

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-05 12:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 18:38 [PATCH] gdb: Preserve neighboring bits when writing floating-point fields Robert Dubner
2026-11-05 12:48 ` Rohr, Stephan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox