summaryrefslogtreecommitdiff
path: root/src/fundamental
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-08-05 13:16:27 +0200
committerLennart Poettering <lennart@poettering.net>2022-08-05 13:34:11 +0200
commitc51e4c796d53f370750b27768fc979f6aa0cb263 (patch)
tree9cced2e8886c881d4af1ad37c1da0d1c7666fb65 /src/fundamental
parent14e7bc2e77d2699498a1f74d7e4f905f11eca335 (diff)
downloadsystemd-c51e4c796d53f370750b27768fc979f6aa0cb263.tar.gz
macro: add macro for checking if integer is power of 2
Diffstat (limited to 'src/fundamental')
-rw-r--r--src/fundamental/macro-fundamental.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h
index e5f6dc7f8c..5c67d3e2f7 100644
--- a/src/fundamental/macro-fundamental.h
+++ b/src/fundamental/macro-fundamental.h
@@ -200,6 +200,19 @@
MIN(_c, z); \
})
+/* Returns true if the passed integer is a positive power of two */
+#define CONST_ISPOWEROF2(x) \
+ ((x) > 0 && ((x) & ((x) - 1)) == 0)
+
+#define ISPOWEROF2(x) \
+ __builtin_choose_expr( \
+ __builtin_constant_p(x), \
+ CONST_ISPOWEROF2(x), \
+ ({ \
+ const typeof(x) _x = (x); \
+ CONST_ISPOWEROF2(_x); \
+ }))
+
#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
#define __LESS_BY(aq, a, bq, b) \
({ \