[net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
From: Luiz Angelo Daros de Luca
Date: Tue Mar 31 2026 - 19:02:56 EST
Some hardware stores logical fields split across multiple registers,
requiring partial values to be reassembled using shifts and masks.
Add FIELD_WIDTH(), a helper that returns the number of contiguous bits
covered by a bitfield mask. This allows callers to determine how much a
partial value needs to be shifted when reconstructing a field.
This complements FIELD_MAX() and FIELD_FIT(), and avoids reimplementing
bit counting logic in drivers.
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@xxxxxxxxx>
---
include/linux/bitfield.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 54aeeef1f0ec..8d6c1c309c3b 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -111,6 +111,19 @@
(typeof(_mask))((_mask) >> __bf_shf(_mask)); \
})
+/**
+ * FIELD_WIDTH() - return the width of a bitfield
+ * @_mask: shifted mask defining the field's length and position
+ *
+ * Returns the number of contiguous bits covered by @_mask.
+ * This corresponds to the bit width of FIELD_MAX(@_mask).
+ */
+#define FIELD_WIDTH(_mask) \
+ ({ \
+ __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
+ __bf_shf(~FIELD_MAX(_mask)); \
+ })
+
/**
* FIELD_FIT() - check if value fits in the field
* @_mask: shifted mask defining the field's length and position
--
2.53.0