[PATCH 3/4] util: Add L_BITS_{SET|CLEAR}

Denis Kenzior <[email protected]> Mon, 15 Apr 2024 14:14:35 -0500
Newsgroups dev.linux.lists.ell
Message-ID <[email protected]>
These macros can set or clear an arbitrary number of bits in a single
call, as follows:

	uint64_t bitmap = 0;

	L_BITS_CLEAR(&bitmap, 0, 1, 5, 15, 32, 63);
---
 ell/util.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/ell/util.h b/ell/util.h
index 53a932c771e6..1d14ffb1570d 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -384,6 +384,20 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p)
 #define L_BIT_TEST(bits, nr)						\
 	((~_L_BIT_TO_OFFSET(bits, nr) & _L_BIT_TO_MASK(bits, nr)) == 0)
 
+#define L_BITS_SET(bits, ...) __extension__ ({				\
+	const unsigned int __elems[] = {__VA_ARGS__};			\
+	size_t __i;							\
+	for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++)		\
+		L_BIT_SET(bits, __elems[__i]);				\
+})
+
+#define L_BITS_CLEAR(bits, ...) __extension__ ({			\
+	const unsigned int __elems[] = {__VA_ARGS__};			\
+	size_t __i;							\
+	for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++)		\
+		L_BIT_CLEAR(bits, __elems[__i]);			\
+})
+
 /*
  * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to
  * ensure the compiler does not try to optimize the constant time behavior.
-- 
2.44.0