[net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()

Luiz Angelo Daros de Luca posted 10 patches 9 hours ago
[net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Posted by Luiz Angelo Daros de Luca 9 hours ago
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@gmail.com>
---
 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
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Posted by Yury Norov 6 hours ago
On Tue, Mar 31, 2026 at 08:00:04PM -0300, Luiz Angelo Daros de Luca wrote:
> 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@gmail.com>
> ---
>  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)						\

Please no underscored names unless necessary.

> +	({								\
> +		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: ");	\
> +		__bf_shf(~FIELD_MAX(_mask));				\
> +	})

I believe, this should be:

  #define FIELD_WIDTH(mask) ({                                  \
        __BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: ");     \
        HWEIGHT(mask);                                          \
  })

Thanks,
Yury

> +
>  /**
>   * FIELD_FIT() - check if value fits in the field
>   * @_mask: shifted mask defining the field's length and position
> 
> -- 
> 2.53.0
>