All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] linux/bitfield.h: import FIELD_PREP_CONST macro from Linux Kernel
@ 2025-06-03  8:51 Christian Marangi
  2025-06-03  8:51 ` [PATCH 2/2] mtd: spinand: winbond: add Winbond W25N04KV flash support Christian Marangi
  2025-06-03 15:08 ` [PATCH 1/2] linux/bitfield.h: import FIELD_PREP_CONST macro from Linux Kernel Quentin Schulz
  0 siblings, 2 replies; 6+ messages in thread
From: Christian Marangi @ 2025-06-03  8:51 UTC (permalink / raw)
  To: Dario Binacchi, Michael Trimarchi, Frieder Schrempf, Tom Rini,
	Christian Marangi, u-boot

Import FIELD_PREP_CONST macro from Linux Kernel to permit usage of
FIELD_PREP with scenario where a constant value is needed.

Refer to commit e2192de59e45 ("bitfield: add FIELD_PREP_CONST()") in
Linux kernel for extensive explaination of why this is useful.

This is also to better align with the Linux Kernel for easier porting of
driver.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 include/linux/bitfield.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 7ad02f8cbb9..1b73ba6f9fa 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -90,6 +90,32 @@
 		((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask);	\
 	})
 
+#define __BF_CHECK_POW2(n)	BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
+
+/**
+ * FIELD_PREP_CONST() - prepare a constant bitfield element
+ * @_mask: shifted mask defining the field's length and position
+ * @_val:  value to put in the field
+ *
+ * FIELD_PREP_CONST() masks and shifts up the value.  The result should
+ * be combined with other fields of the bitfield using logical OR.
+ *
+ * Unlike FIELD_PREP() this is a constant expression and can therefore
+ * be used in initializers. Error checking is less comfortable for this
+ * version, and non-constant masks cannot be used.
+ */
+#define FIELD_PREP_CONST(_mask, _val)					\
+	(								\
+		/* mask must be non-zero */				\
+		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
+		/* check if value fits */				\
+		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
+		/* check if mask is contiguous */			\
+		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
+		/* and create the value */				\
+		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
+	)
+
 /**
  * FIELD_GET() - extract a bitfield element
  * @_mask: shifted mask defining the field's length and position
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-06-04  8:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-03  8:51 [PATCH 1/2] linux/bitfield.h: import FIELD_PREP_CONST macro from Linux Kernel Christian Marangi
2025-06-03  8:51 ` [PATCH 2/2] mtd: spinand: winbond: add Winbond W25N04KV flash support Christian Marangi
2025-06-03 15:08 ` [PATCH 1/2] linux/bitfield.h: import FIELD_PREP_CONST macro from Linux Kernel Quentin Schulz
2025-06-03 20:37   ` Christian Marangi
2025-06-03 23:50     ` Tom Rini
2025-06-04  8:55     ` Quentin Schulz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.