Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Robert Dubner <rdubner@symas.com>
To: <gdb-patches@sourceware.org>
Cc: "'Rohr, Stephan'" <stephan.rohr@intel.com>
Subject: [PATCH] gdb: Preserve neighboring bits when writing floating-point fields
Date: Tue, 3 Nov 2026 12:38:45 -0600 (CST)	[thread overview]
Message-ID: <039301dd6bc2$8fbb09d0$af311d70$@symas.com> (raw)

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


             reply	other threads:[~2026-11-03 18:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 18:38 Robert Dubner [this message]
2026-11-05 12:48 ` Rohr, Stephan

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='039301dd6bc2$8fbb09d0$af311d70$@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