summaryrefslogtreecommitdiff
path: root/include/compile_time_macros.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/compile_time_macros.h')
-rw-r--r--include/compile_time_macros.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/include/compile_time_macros.h b/include/compile_time_macros.h
index bf133d60f4..b05e2569e1 100644
--- a/include/compile_time_macros.h
+++ b/include/compile_time_macros.h
@@ -35,4 +35,26 @@
#define member_size(type, member) sizeof(((type *)0)->member)
+/*
+ * Bit operation macros.
+ */
+#define BIT(nr) (1UL << (nr))
+#define BIT_ULL(nr) (1ULL << (nr))
+
+/*
+ * Create a bit mask from least significant bit |l|
+ * to bit |h|, inclusive.
+ *
+ * Examples:
+ * GENMASK(31, 0) ==> 0xFF_FF_FF_FF
+ * GENMASK(3, 0) ==> 0x00_00_00_0F
+ * GENMASK(7, 4) ==> 0x00_00_00_F0
+ * GENMASK(b, b) ==> BIT(b)
+ *
+ * Note that we shift after using BIT() to avoid compiler
+ * warnings for BIT(31+1).
+ */
+#define GENMASK(h, l) (((BIT(h)<<1) - 1) ^ (BIT(l) - 1))
+#define GENMASK_ULL(h, l) (((BIT_ULL(h)<<1) - 1) ^ (BIT_ULL(l) - 1))
+
#endif /* __CROS_EC_COMPILE_TIME_MACROS_H */