aboutsummaryrefslogtreecommitdiff
path: root/quantum/bits.h
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2024-11-27 21:29:12 +1100
committerNick Brassel <nick@tzarc.org>2024-11-27 21:29:12 +1100
commit9f76541b29056150cf57d69569a14a59e13995c7 (patch)
tree66c7398d873fcda88aebe3090603aefe045ff52e /quantum/bits.h
parent57be4871616ee9a8fe042f6186010d436ec7d4b1 (diff)
parenteb04b94eecbb9b3e64ca9c74f937b5f762440b17 (diff)
Merge branch 'develop'
Diffstat (limited to 'quantum/bits.h')
-rw-r--r--quantum/bits.h33
1 files changed, 33 insertions, 0 deletions
diff --git a/quantum/bits.h b/quantum/bits.h
new file mode 100644
index 0000000000..2f3c343762
--- /dev/null
+++ b/quantum/bits.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#pragma once
+
+#include <stdint.h>
+
+/* Remove these once we transitioned to C23 across all platfroms */
+#define UINT32_WIDTH 32
+#define UINT64_WIDTH 64
+
+/**
+ * @brief Mask for the little endian nth bit (0-31) in a 32-bit integer.
+ */
+#define BIT32(n) (UINT32_C(1) << (n))
+
+/**
+ * @brief Mask for the little endian nth bit (0-63) in a 64-bit integer.
+ */
+#define BIT64(n) (UINT64_C(1) << (n))
+
+/**
+ * @brief Create a contiguous 32-bit wide bitmask starting at bit position @l
+ * and ending at position @h. The range is inclusive, meaning GENMASK32(20, 10)
+ * gives us the 32-bit mask 0x001ffc00.
+ */
+#define GENMASK32(h, l) (((~UINT32_C(0)) - (UINT32_C(1) << (l)) + 1) & (~UINT32_C(0) >> (UINT32_WIDTH - 1 - (h))))
+
+/**
+ * @brief Create a contiguous 64-bit wide bitmask starting at bit position @l
+ * and ending at position @h. The range is inclusive, meaning GENMASK64(39, 21)
+ * gives us the 64-bit mask 0x000000ffffe00000.
+ */
+#define GENMASK64(h, l) (((~UINT64_C(0)) - (UINT64_C(1) << (l)) + 1) & (~UINT64_C(0) >> (UINT64_WIDTH - 1 - (h))))