summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx.manpages@gmail.com>2021-11-19 21:50:29 +0100
committerAlejandro Colomar <alx.manpages@gmail.com>2021-11-20 13:22:21 +0100
commite2469fced9f3d786a42ce90082ffd977f5e613ee (patch)
tree95189cdb70706840700d1cde2947c40ec35a3c86
parent44eae19045a2fc77b17c7bd047b652e3a72a6c7b (diff)
linux/power_of_2.h: Add __IS_POWER_OF_2(n) and __IS_POWER_OF_2_OR_0(n) macros
Tested: $ cat pow2.c #include <stdio.h> #define is_power_of_2_or_0(n) (((n) & ((n) - 1)) == 0) #define is_power_of_2(n) (is_power_of_2_or_0(n) && ((n) != 0)) int main(void) { printf("%i, %i, %i\n", 8, is_power_of_2_or_0(8), is_power_of_2(8)); printf("%i, %i, %i\n", 7, is_power_of_2_or_0(7), is_power_of_2(7)); printf("%i, %i, %i\n", 6, is_power_of_2_or_0(6), is_power_of_2(6)); printf("%i, %i, %i\n", 5, is_power_of_2_or_0(5), is_power_of_2(5)); printf("%i, %i, %i\n", 4, is_power_of_2_or_0(4), is_power_of_2(4)); printf("%i, %i, %i\n", 3, is_power_of_2_or_0(3), is_power_of_2(3)); printf("%i, %i, %i\n", 2, is_power_of_2_or_0(2), is_power_of_2(2)); printf("%i, %i, %i\n", 1, is_power_of_2_or_0(1), is_power_of_2(1)); printf("%i, %i, %i\n", 0, is_power_of_2_or_0(0), is_power_of_2(0)); } $ cc pow2.c $ ./a.out 8, 1, 1 7, 0, 0 6, 0, 0 5, 0, 0 4, 1, 1 3, 0, 0 2, 1, 1 1, 1, 1 0, 1, 0 Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
-rw-r--r--include/linux/power_of_2.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/include/linux/power_of_2.h b/include/linux/power_of_2.h
new file mode 100644
index 000000000000..812fe86eefcd
--- /dev/null
+++ b/include/linux/power_of_2.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_POWER_OF_2_H
+#define _LINUX_POWER_OF_2_H
+
+
+#define __IS_POWER_OF_2_OR_0(n) (((n) & ((n) - 1)) == 0)
+#define __IS_POWER_OF_2(n) (__IS_POWER_OF_2_OR_0(n) && ((n) != 0))
+
+
+#endif /* _LINUX_POWER_OF_2_H */