From: Robert Dubner <rdubner@symas.com>
To: gdb-patches@sourceware.org
Cc: stephan.rohr@intel.com
Subject: [PATCH v4 1/5] gdb: Preserve neighboring bits when writing floating-point fields
Date: Wed, 11 Nov 2026 12:42:43 -0500 [thread overview]
Message-ID: <20261111174247.3382098-2-rdubner@symas.com> (raw)
In-Reply-To: <20261111174247.3382098-1-rdubner@symas.com>
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.
Reviewed-By: Rohr, Stephan <stephan.rohr@intel.com>
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34927
---
gdb/target-float.c | 118 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 107 insertions(+), 11 deletions(-)
diff --git a/gdb/target-float.c b/gdb/target-float.c
index 31b5ac642a7..89a95067f2f 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,96 @@ 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 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));
+ 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
+}
base-commit: 9db3b515b964c1bbdc4d20c1202fbf27719be269
--
2.34.1
next prev parent reply other threads:[~2026-11-11 17:43 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 ` Robert Dubner [this message]
2026-11-12 9:04 ` [PATCH v4 1/5] gdb: Preserve neighboring bits when writing floating-point fields 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
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=20261111174247.3382098-2-rdubner@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