mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] bitops: add equivalent of BIT(x) for bitfields
@ 2016-12-05 13:36 Sebastian Frias
  2016-12-05 15:34 ` Borislav Petkov
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Sebastian Frias @ 2016-12-05 13:36 UTC (permalink / raw)
  To: zijun_hu, Sasha Levin, Andrew Morton, linux-kernel, Linus Torvalds
  Cc: Mason, Maxime Coquelin, Harvey Harrison, Borislav Petkov

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

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

Signed-off-by: Sebastian Frias <sf84@laposte.net>
---

Code protected with "#ifdef __KERNEL__" just as the BIT(x)
macros.

I would have preferred another name, like BITS(x) but it is
already used.

Suggestions for other names welcome.
---
 include/linux/bitops.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index a83c822..4659237 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
+ * SETBITFIELD(1, 0,0xff) = 0x00000003
+ * SETBITFIELD(3, 0,0xff) = 0x0000000f
+ * SETBITFIELD(15,8,0xff) = 0x0000ff00
+ * SETBITFIELD(6, 6,   1) = 0x00000040 == BIT(6)
+ */
+#define SETBITFIELD(msb, lsb, val)     \
+	(((val) << (lsb)) & (GENMASK((msb), (lsb))))
+#define SETBITFIELD_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

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

end of thread, other threads:[~2016-12-07  8:34 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-05 13:36 [PATCH] bitops: add equivalent of BIT(x) for bitfields Sebastian Frias
2016-12-05 15:34 ` Borislav Petkov
2016-12-05 17:02   ` Sebastian Frias
2016-12-05 17:13 ` Linus Torvalds
2016-12-05 17:47   ` Sebastian Frias
2016-12-05 17:48 ` Geert Uytterhoeven
2016-12-06 10:31   ` Sebastian Frias
2016-12-06 10:42     ` Geert Uytterhoeven
2016-12-06 11:03       ` Sebastian Frias
2016-12-06 11:12         ` Geert Uytterhoeven
2016-12-06 14:56           ` Sebastian Frias
2016-12-07  8:34 ` Kalle Valo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®