diff options
author | Craig Hesling <hesling@chromium.org> | 2019-12-18 10:40:01 -0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-12-23 15:07:36 +0000 |
commit | 890d57187e8280417426d2cbb03dc011a49e33aa (patch) | |
tree | be13cfb621ef6f79d8c734c74caa48d0d543c5dc | |
parent | 69572dcea71d4da3eb1c091d5f6c3b5dec77df7e (diff) | |
download | chrome-ec-890d57187e8280417426d2cbb03dc011a49e33aa.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>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1979587
Tested-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Commit-Queue: Tim Wawrzynczak <twawrzynczak@chromium.org>
-rw-r--r-- | include/compile_time_macros.h | 16 | ||||
-rw-r--r-- | test/build.mk | 2 | ||||
-rw-r--r-- | test/compile_time_macros.c | 47 | ||||
-rw-r--r-- | test/compile_time_macros.tasklist | 9 |
4 files changed, 74 insertions, 0 deletions
diff --git a/include/compile_time_macros.h b/include/compile_time_macros.h index cdf8b1998b..e5c1242f4c 100644 --- a/include/compile_time_macros.h +++ b/include/compile_time_macros.h @@ -41,4 +41,20 @@ #define BIT(nr) (1U << (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 74e602151c..b220bc9a69 100644 --- a/test/build.mk +++ b/test/build.mk @@ -21,6 +21,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 @@ -99,6 +100,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 */ |