diff options
author | Tom Hughes <tomhughes@chromium.org> | 2020-05-14 10:16:37 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-05-15 03:03:15 +0000 |
commit | f21e39083c70a3a04aed865cef65ce4ef95d78b8 (patch) | |
tree | 88488d70442770d1db63805532902eefaf5f4e99 | |
parent | bca6d843094cbc8bc49aea01c87b8338b529fd7b (diff) | |
download | chrome-ec-f21e39083c70a3a04aed865cef65ce4ef95d78b8.tar.gz |
common/util: Add is_aligned
Helper function to check power-of-two alignment.
BRANCH=none
BUG=b:155229277, b:156501835
TEST=make buildall -j
TEST=make run-utils
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Change-Id: Iadcdaeb59e496f10035bd6c7f9660a3cc33a4898
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2202849
Commit-Queue: Ting Shen <phoenixshen@chromium.org>
Reviewed-by: Ting Shen <phoenixshen@chromium.org>
-rw-r--r-- | common/util.c | 8 | ||||
-rw-r--r-- | include/util.h | 9 | ||||
-rw-r--r-- | test/utils.c | 18 |
3 files changed, 35 insertions, 0 deletions
diff --git a/common/util.c b/common/util.c index 8d12dc59f4..48374ed08a 100644 --- a/common/util.c +++ b/common/util.c @@ -552,6 +552,14 @@ bool bytes_are_trivial(const uint8_t *buffer, size_t size) return (result0 == 0) || (result1 == 0); } +bool is_aligned(uint32_t addr, uint32_t align) +{ + if (!POWER_OF_TWO(align)) + return false; + + return (addr & (align - 1)) == 0; +} + /****************************************************************************/ /* stateful conditional stuff */ diff --git a/include/util.h b/include/util.h index ed5ed26142..7c7e356592 100644 --- a/include/util.h +++ b/include/util.h @@ -187,6 +187,15 @@ int get_next_bit(uint32_t *mask); bool bytes_are_trivial(const uint8_t *buffer, size_t size); /** + * Checks if address is power-of-two aligned to specified alignment. + * + * @param addr address + * @param align power-of-two alignment + * @return true if addr is aligned to align, false otherwise + */ +bool is_aligned(uint32_t addr, uint32_t align); + +/** * Reverse's the byte-order of the provided buffer. */ void reverse(void *dest, size_t len); diff --git a/test/utils.c b/test/utils.c index 201f607f0c..292b2c84c7 100644 --- a/test/utils.c +++ b/test/utils.c @@ -424,6 +424,23 @@ static int test_bytes_are_trivial(void) return EC_SUCCESS; } +test_static int test_is_aligned(void) +{ + TEST_EQ(is_aligned(2, 0), false, "%d"); + TEST_EQ(is_aligned(2, 1), true, "%d"); + TEST_EQ(is_aligned(2, 2), true, "%d"); + TEST_EQ(is_aligned(2, 3), false, "%d"); + TEST_EQ(is_aligned(2, 4), false, "%d"); + + TEST_EQ(is_aligned(3, 0), false, "%d"); + TEST_EQ(is_aligned(3, 1), true, "%d"); + TEST_EQ(is_aligned(3, 2), false, "%d"); + TEST_EQ(is_aligned(3, 3), false, "%d"); + TEST_EQ(is_aligned(3, 4), false, "%d"); + + return EC_SUCCESS; +} + void run_test(void) { test_reset(); @@ -442,6 +459,7 @@ void run_test(void) RUN_TEST(test_mula32); RUN_TEST(test_swap); RUN_TEST(test_bytes_are_trivial); + RUN_TEST(test_is_aligned); test_print_result(); } |