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>
This commit is contained in:
Craig Hesling 2019-12-18 10:40:01 -08:00 committed by Commit Bot
parent 7ac25f043a
commit f776a28736
4 changed files with 74 additions and 0 deletions

View File

@ -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 */

View File

@ -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
@ -101,6 +102,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

View File

@ -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();
}

View File

@ -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 */