summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Hesling <hesling@chromium.org>2019-12-18 10:40:01 -0800
committerCommit Bot <commit-bot@chromium.org>2021-09-17 00:18:34 +0000
commitad40e89582369678e9c205b4ba45d480036aa5c6 (patch)
tree92a684c7564fa6c98512fdadab9501b2f1c6f6ef
parent6e6387a2f17bfb13df86b74b8c3394ff0dc485c8 (diff)
downloadchrome-ec-ad40e89582369678e9c205b4ba45d480036aa5c6.tar.gz
compile_time_macros: Add GENMASK and GENMASK_ULL
BRANCH=all BUG=none TEST=make buildall TEST=make run-compile_time_macros Change-Id: I586e009dac20e33701ded9a05c51ee806e466cae Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1974356 Tested-by: Craig Hesling <hesling@chromium.org> Auto-Submit: Craig Hesling <hesling@chromium.org> Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Jett Rink <jettrink@chromium.org> Commit-Queue: Jett Rink <jettrink@chromium.org> (cherry picked from commit f776a287369e4ac46b17eb9001f3b78621f87325) Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3128748 Reviewed-by: Craig Hesling <hesling@chromium.org> Reviewed-by: JuHyun Kim <jkim@invensense.com> Tested-by: JuHyun Kim <jkim@invensense.com>
-rw-r--r--include/compile_time_macros.h16
-rw-r--r--test/build.mk2
-rw-r--r--test/compile_time_macros.c47
-rw-r--r--test/compile_time_macros.tasklist9
4 files changed, 74 insertions, 0 deletions
diff --git a/include/compile_time_macros.h b/include/compile_time_macros.h
index 30a3e901b3..b05e2569e1 100644
--- a/include/compile_time_macros.h
+++ b/include/compile_time_macros.h
@@ -41,4 +41,20 @@
#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 */
diff --git a/test/build.mk b/test/build.mk
index 31cec03c9a..b3fee3ab6d 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -23,6 +23,7 @@ test-list-host += cec
test-list-host += charge_manager
test-list-host += charge_manager_drp_charging
test-list-host += charge_ramp
+test-list-host += compile_time_macros
test-list-host += console_edit
test-list-host += crc32
test-list-host += entropy
@@ -80,6 +81,7 @@ cec-y=cec.o
charge_manager-y=charge_manager.o
charge_manager_drp_charging-y=charge_manager.o
charge_ramp-y+=charge_ramp.o
+compile_time_macros-y=compile_time_macros.o
console_edit-y=console_edit.o
crc32-y=crc32.o
entropy-y=entropy.o
diff --git a/test/compile_time_macros.c b/test/compile_time_macros.c
new file mode 100644
index 0000000000..28e7af500b
--- /dev/null
+++ b/test/compile_time_macros.c
@@ -0,0 +1,47 @@
+/* Copyright 2019 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Test compile_time_macros.h
+ */
+
+#include "common.h"
+#include "test_util.h"
+
+
+static int test_GENMASK(void)
+{
+ TEST_EQ(GENMASK(0, 0), 0x00000001U, "%u");
+ TEST_EQ(GENMASK(31, 0), 0xFFFFFFFFU, "%u");
+ TEST_EQ(GENMASK(4, 4), 0x00000010U, "%u");
+ TEST_EQ(GENMASK(4, 0), 0x0000001FU, "%u");
+ TEST_EQ(GENMASK(21, 21), 0x00200000U, "%u");
+ TEST_EQ(GENMASK(31, 31), 0x80000000U, "%u");
+
+ return EC_SUCCESS;
+}
+
+static int test_GENMASK_ULL(void)
+{
+ TEST_EQ(GENMASK_ULL(0, 0), 0x0000000000000001ULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(31, 0), 0x00000000FFFFFFFFULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(63, 0), 0xFFFFFFFFFFFFFFFFULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(4, 4), 0x0000000000000010ULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(4, 0), 0x000000000000001FULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(21, 21), 0x0000000000200000ULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(31, 31), 0x0000000080000000ULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(63, 63), 0x8000000000000000ULL, "%Lu");
+ TEST_EQ(GENMASK_ULL(62, 60), 0x7000000000000000ULL, "%Lu");
+
+ return EC_SUCCESS;
+}
+
+void run_test(void)
+{
+ test_reset();
+
+ RUN_TEST(test_GENMASK);
+ RUN_TEST(test_GENMASK_ULL);
+
+ test_print_result();
+}
diff --git a/test/compile_time_macros.tasklist b/test/compile_time_macros.tasklist
new file mode 100644
index 0000000000..5ffe662d01
--- /dev/null
+++ b/test/compile_time_macros.tasklist
@@ -0,0 +1,9 @@
+/* Copyright 2019 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * See CONFIG_TASK_LIST in config.h for details.
+ */
+#define CONFIG_TEST_TASK_LIST /* No test task */