Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
fwk/macros: Add a power of two check macro
Browse files Browse the repository at this point in the history
Adding a macro to determine if a value is a power of two number or not.

Signed-off-by: Mohamed Omar Asaker <mohamed.omarasaker@arm.com>
  • Loading branch information
mohamedasaker-arm committed Jul 30, 2024
1 parent e5c8d02 commit 7e6c114
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
12 changes: 12 additions & 0 deletions framework/include/fwk_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
*/
#define FWK_ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

/*!
* \brief Check whether the value is power of two or not
*
* \param VALUE Value to be checked.
*
* \return boolean represent the check result.
* \retval True when the \p VALUE is power of two.
* \retval False when the \p VALUE is not power of two.
*/
#define FWK_IS_VALUE_POWER_OF_TWO(VALUE) \
((VALUE) <= 0) ? false : (((VALUE) & ((VALUE)-1)) == (0))

/*!
* \brief Aligns a value to the next multiple.
*
Expand Down
45 changes: 43 additions & 2 deletions framework/test/test_fwk_macros.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Arm SCP/MCP Software
* Copyright (c) 2015-2021, Arm Limited and Contributors. All rights reserved.
* Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
Expand All @@ -12,11 +12,13 @@
static void test_fwk_macros_array_size(void);
static void test_fwk_macros_align_next(void);
static void test_fwk_macros_align_previous(void);
static void test_fwk_macros_power_of_two_check(void);

static const struct fwk_test_case_desc test_case_table[] = {
FWK_TEST_CASE(test_fwk_macros_array_size),
FWK_TEST_CASE(test_fwk_macros_align_next),
FWK_TEST_CASE(test_fwk_macros_align_previous)
FWK_TEST_CASE(test_fwk_macros_align_previous),
FWK_TEST_CASE(test_fwk_macros_power_of_two_check)
};

struct fwk_test_suite_desc test_suite = {
Expand Down Expand Up @@ -95,3 +97,42 @@ static void test_fwk_macros_align_previous(void)
result = FWK_ALIGN_PREVIOUS(value, interval);
assert(result == 8);
}

static void test_fwk_macros_power_of_two_check(void)
{
bool result;
unsigned int value;
struct power_of_two_test_data {
unsigned long long value;
bool verdict;
} test_data[] = {
{ 0x00000, false }, { 0x00017, false }, { 0x0006C, false },
{ 0x0004D, false }, { 0x00017, false }, { 0x002aC, false },
{ 0x102aC, false }, { 0x00001, true }, { 0x00100, true },
{ 0x00004, true }, { 0x00080, true }, { 0x100000, true },
{ 0x400000, true }, { 0x80000000, true }, { 0xFFFFFFFF, false },
};
unsigned int test_count = sizeof(test_data) / sizeof(test_data[0]);

/* Precedence tests */
value = 3;
result = FWK_IS_VALUE_POWER_OF_TWO(value & 1);
assert(result == true);

value = 3;
result = FWK_IS_VALUE_POWER_OF_TWO(value & 2);
assert(result == true);

value = 5;
result = FWK_IS_VALUE_POWER_OF_TWO(value & 1);
assert(result == true);

value = 5;
result = FWK_IS_VALUE_POWER_OF_TWO(value & 4);
assert(result == true);

for (unsigned int i = 0; i < test_count; ++i) {
result = FWK_IS_VALUE_POWER_OF_TWO(test_data[i].value);
assert(result == test_data[i].verdict);
}
}

0 comments on commit 7e6c114

Please sign in to comment.