aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Kenzior <denkenz@gmail.com>2024-04-12 14:58:10 -0500
committerDenis Kenzior <denkenz@gmail.com>2024-04-16 14:06:53 -0500
commitf0320e5492bcd16200b49b3451d344e0c7093329 (patch)
tree7a19659f7004f0125d01aa5fc361ca3850a5147d
parentf5456000a1e75d4fb566844cd997eef11da2516c (diff)
util: Add L_BITS_{SET|CLEAR}
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);
-rw-r--r--ell/util.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/ell/util.h b/ell/util.h
index 6283296c..f4fc4d11 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -396,6 +396,20 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p)
(*_offset & _L_BIT_TO_MASK(_offset, _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.