[PATCH v5] add macros for bitfield manipulation "Ã la" GENMASK

From: Sebastian Frias
Date: Tue Dec 06 2016 - 11:41:39 EST


Introduce GENVALUE(msb, lsb, value) macro to ease dealing with
continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

These are useful mostly for creating values to be packed together
via OR operations, ex:

u32 val = 0x11110000;
val |= GENVALUE(19, 12, 0x5a);

=> 'val = 0x1115a000'

Introduce BITFIELD_INSERT(target, msb, lsb, val) and its counter-part
BITFIELD_EXTRACT(source, msb, lsb)

BITFIELD_INSERT_ULL/BITFIELD_EXTRACT_ULL are also added.

These are useful for:

a = 0x111a5000;
b = BITFIELD_EXTRACT(a, 19, 12);

=> 'b = 0xa5'

BITFIELD_INSERT(a, 19, 12, 0xff5a);

=> 'a = 0x1115a000'


Signed-off-by: Sebastian Frias <sf84@xxxxxxxxxxx>
Link: https://marc.info/?l=linux-kernel&m=148094498711000&w=2
---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as
proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters
(essentially 'lsb' but also 'msb') are not constants as
proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects;
'msb' is subjected to same constraints for consistency.

Change in v4:
- add BITFIELD_INSERT/BITFIELD_EXTRACT as suggested by Geert
- modify commit message to account for that

Change in v5:
- fix minor formatting in commit message

---
include/linux/bitops.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index a83c822..491f966 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -24,6 +24,72 @@
#define GENMASK_ULL(h, l) \
(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))

+#ifdef __KERNEL__
+/*
+ * Equivalent of BIT(x) but for contiguous bitfields
+ *
+ * NOTE: 'val' is truncated to the size of the bitfield [msb:lsb]
+ *
+ * GENVALUE(1, 0,0xffff) = 0x00000003
+ * GENVALUE(3, 0,0xffff) = 0x0000000f
+ * GENVALUE(15,8,0xffff) = 0x0000ff00
+ * GENVALUE(6, 6, 1) = 0x00000040 == BIT(6)
+ */
+#define GENVALUE(msb, lsb, val) \
+ ( \
+ /* BUILD_BUG_ON_ZERO() evaluates to 0 */ \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) | \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) | \
+ (((val) << (lsb)) & (GENMASK((msb), (lsb)))) \
+ )
+
+#define GENVALUE_ULL(msb, lsb, val) \
+ ( \
+ /* BUILD_BUG_ON_ZERO() evaluates to 0 */ \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) | \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) | \
+ (((val) << (lsb)) & (GENMASK_ULL((msb), (lsb)))) \
+ )
+
+/*
+ * Takes 'val' and inserts it as bitfield [msb:lsb] into 'target'
+ *
+ * NOTE: 'val' is truncated to the size of the bitfield [msb:lsb]
+ *
+ * a = 0x111a5000;
+ * BITFIELD_INSERT(a, 19, 12, 0xff5a);
+ * => 'a = 0x1115a000'
+ */
+#define BITFIELD_INSERT(target, msb, lsb, val) \
+ (target = ((target & ~GENMASK(msb, lsb)) | \
+ GENVALUE(msb, lsb, val)))
+
+#define BITFIELD_INSERT_ULL(target, msb, lsb, val) \
+ (target = ((target & ~GENMASK_ULL(msb, lsb)) | \
+ GENVALUE_ULL(msb, lsb, val)))
+
+/*
+ * Extracts bitfield [msb:lsb] from 'source'
+ *
+ * a = 0x111a5000;
+ * b = BITFIELD_EXTRACT(a, 19, 12);
+ * => 'b = 0xa5'
+ */
+#define BITFIELD_EXTRACT(source, msb, lsb) \
+ ( \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) | \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) | \
+ (((source) & GENMASK(msb, lsb)) >> (lsb)) \
+ )
+
+#define BITFIELD_EXTRACT_ULL(source, msb, lsb) \
+ ( \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) | \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) | \
+ (((source) & GENMASK_ULL(msb, lsb)) >> (lsb)) \
+ )
+#endif
+
extern unsigned int __sw_hweight8(unsigned int w);
extern unsigned int __sw_hweight16(unsigned int w);
extern unsigned int __sw_hweight32(unsigned int w);
--
1.8.3.1