[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <93523b80-6033-bafa-c1ec-4bc2c3f9c53b@laposte.net>
Date: Mon, 5 Dec 2016 18:49:30 +0100
From: Sebastian Frias <sf84@...oste.net>
To: zijun_hu <zijun_hu@....com>,
Andrew Morton <akpm@...ux-foundation.org>,
linux-kernel@...r.kernel.org,
Linus Torvalds <torvalds@...ux-foundation.org>
Cc: Mason <slash.tmp@...e.fr>,
Harvey Harrison <harvey.harrison@...il.com>,
Borislav Petkov <bp@...en8.de>
Subject: [PATCH v2] add equivalent of BIT(x) for bitfields
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.
This is useful mostly for creating values to be packed together
via OR operations, ex:
u32 val = 0x11110000;
val |= GENVALUE(19, 12, 0x5a);
now 'val = 0x1115a000'
Signed-off-by: Sebastian Frias <sf84@...oste.net>
---
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
---
include/linux/bitops.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index a83c822..641675d 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -24,6 +24,20 @@
#define GENMASK_ULL(h, l) \
(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
+#ifdef __KERNEL__
+/*
+ * Equivalent of BIT(x) but for contiguous bitfields
+ * GENVALUE(1, 0,0xff) = 0x00000003
+ * GENVALUE(3, 0,0xff) = 0x0000000f
+ * GENVALUE(15,8,0xff) = 0x0000ff00
+ * GENVALUE(6, 6, 1) = 0x00000040 == BIT(6)
+ */
+#define GENVALUE(msb, lsb, val) \
+ (((val) << (lsb)) & (GENMASK((msb), (lsb))))
+#define GENVALUE_ULL(msb, lsb, val) \
+ (((val) << (lsb)) & (GENMASK_ULL((msb), (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
Powered by blists - more mailing lists