git: a6e81841486a - main - libkern: Bitfield extraction: Stick to standard, separate sign extension

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 20:44:23 UTC
The branch main has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=a6e81841486aee1981d356040c597c79efaa4432

commit a6e81841486aee1981d356040c597c79efaa4432
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2026-11-17 12:38:06 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2026-11-23 20:35:54 +0000

    libkern: Bitfield extraction: Stick to standard, separate sign extension
    
    Rename signed_extend32() and signed_extend64() to
    extract_signed_bitfield_32() and extract_signed_bitfield_64()
    respectively, as these names more clearly describe what these functions
    are doing and to make room for functions really doing sign extension.
    
    Change implementation of extract_signed_bitfield_32() and
    extract_signed_bitfield_64() to stop relying on GCC/clang-specific
    implementation-defined behavior on right shifts of negative integers.
    They now rely on two-complement arithmetic, which all machines we
    support do, and which is the future direction for C (it is a requirement
    since C23).
    
    While here, separate the sign bit extension into sign_extend_32() and
    sign_extend_64() respectively, as these could be useful on their own in
    addition to serving to implement extract_signed_bitfield_32() and
    extract_signed_bitfield_64().
    
    Reviewed by:    fuz
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D61762
---
 sys/dev/iicbus/sensor/tmp461.c |  2 +-
 sys/sys/libkern.h              | 65 ++++++++++++++++++++++++++++++++++++++----
 2 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/sys/dev/iicbus/sensor/tmp461.c b/sys/dev/iicbus/sensor/tmp461.c
index 90a0a7047cdb..d30230ee7da3 100644
--- a/sys/dev/iicbus/sensor/tmp461.c
+++ b/sys/dev/iicbus/sensor/tmp461.c
@@ -263,7 +263,7 @@ tmp461_read_temperature(device_t dev, int32_t *temperature, bool remote_measure)
 		goto fail;
 
 	data -= offset;
-	*temperature = signed_extend32(data, 0, 8) << 4;
+	*temperature = extract_signed_bitfield_32(data, 0, 8) << 4;
 
 	if (remote_measure) {
 		if (sc->conf & TMP461_REMOTE_TEMP_DOUBLE_REG) {
diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h
index 64b1688fe768..0ce0e0c483f0 100644
--- a/sys/sys/libkern.h
+++ b/sys/sys/libkern.h
@@ -295,20 +295,73 @@ rindex(const char *p, int ch)
 	return (strrchr(p, ch));
 }
 
+/*
+ * Returns a sign extension of 'val' from its 'sign_idx_bit'.
+ *
+ * All bits above 'sign_idx_bit' are set to 0 or 1 accordingly.
+ */
 static __inline int64_t
-signed_extend64(uint64_t bitmap, int lsb, int width)
+sign_extend_64(uint64_t val, int sign_bit_idx)
 {
+	uint64_t sign_bit_mask;
 
-	return ((int64_t)(bitmap << (63 - lsb - (width - 1)))) >>
-	    (63 - (width - 1));
+	KASSERT(0 <= sign_bit_idx && sign_bit_idx < sizeof(val) * 8,
+	    ("%s: Sign bit index %d out of range.", __func__, sign_bit_idx));
+
+	sign_bit_mask = 1ull << sign_bit_idx;
+	return (-(sign_bit_mask & val) | ((sign_bit_mask - 1) & val));
 }
 
+/* See 64-bit version above. */
 static __inline int32_t
-signed_extend32(uint32_t bitmap, int lsb, int width)
+sign_extend_32(uint32_t val, int sign_bit_idx)
+{
+	uint32_t sign_bit_mask;
+
+	KASSERT(0 <= sign_bit_idx && sign_bit_idx < sizeof(val) * 8,
+	    ("%s: Sign bit index %d out of range.", __func__, sign_bit_idx));
+
+	sign_bit_mask = 1u << sign_bit_idx;
+	return (-(sign_bit_mask & val) | ((sign_bit_mask - 1) & val));
+}
+
+/*
+ * Extract signed bitfield.
+ *
+ * Extract value of bitfield in 'bitmap' that starts at least-significant bit
+ * 'lsb' and with number of bits 'width', sign extending the result.
+ */
+static __inline int64_t
+extract_signed_bitfield_64(uint64_t bitmap, int lsb, int width)
 {
+	KASSERT(0 <= lsb && lsb < sizeof(bitmap) * 8,
+	    ("%s: Bitfield start index %d out of range.", __func__, lsb));
+	KASSERT(width > 0, ("%s: Bitfield width %d not strictly positive.",
+	    __func__, width));
+	KASSERT(width <= sizeof(bitmap) * 8,
+	    ("%s: Bitfield too wide (width %d).", __func__, width));
+	KASSERT(lsb + width <= sizeof(bitmap) * 8,
+	    ("%s: Bitfield overflows bitmap (last bit index %d).", __func__,
+	    lsb + width - 1));
+
+	return (sign_extend_64(bitmap >> lsb, width - 1));
+}
 
-	return ((int32_t)(bitmap << (31 - lsb - (width - 1)))) >>
-	    (31 - (width - 1));
+/* See 64-bit variant above. */
+static __inline int32_t
+extract_signed_bitfield_32(uint32_t bitmap, int lsb, int width)
+{
+	KASSERT(0 <= lsb && lsb < sizeof(bitmap) * 8,
+	    ("%s: Bitfield start index %d out of range.", __func__, lsb));
+	KASSERT(width > 0, ("%s: Bitfield width %d not strictly positive.",
+	    __func__, width));
+	KASSERT(width <= sizeof(bitmap) * 8,
+	    ("%s: Bitfield too wide (width %d).", __func__, width));
+	KASSERT(lsb + width <= sizeof(bitmap) * 8,
+	    ("%s: Bitfield overflows bitmap (last bit index %d).", __func__,
+	    lsb + width - 1));
+
+	return (sign_extend_32(bitmap >> lsb, width - 1));
 }
 
 /* fnmatch() return values. */