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 check alignment macro
Browse files Browse the repository at this point in the history
Adding a macro to determine if a value is aligned with a given
alignment or not.

Signed-off-by: Mohamed Omar Asaker <mohamed.omarasaker@arm.com>
  • Loading branch information
mohamedasaker-arm committed Jul 30, 2024
1 parent 7e6c114 commit 75c1086
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
15 changes: 14 additions & 1 deletion framework/include/fwk_macros.h
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 Down Expand Up @@ -64,6 +64,19 @@
#define FWK_ALIGN_PREVIOUS(VALUE, INTERVAL) ( \
((VALUE) / (INTERVAL)) * (INTERVAL))

/*!
* \brief Check the alignment of a value.
*
* \param VALUE Value to be aligned.
*
* \param ALIGNMENT The alignment which needs to be a power of two number.
*
* \return Boolean represent the check result.
* \retval True when the \p VALUE is aligned with the given \p ALIGNMENT value.
* \retval False otherwise.
*/
#define FWK_IS_ALIGNED(VALUE, ALIGNMENT) \
(FWK_IS_VALUE_POWER_OF_TWO(ALIGNMENT) && (((VALUE) & ((ALIGNMENT)-1)) == 0))
/*!
* \brief Hertz unit.
*/
Expand Down
41 changes: 40 additions & 1 deletion framework/test/test_fwk_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ 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 void test_fwk_macros_align_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_power_of_two_check)
FWK_TEST_CASE(test_fwk_macros_power_of_two_check),
FWK_TEST_CASE(test_fwk_macros_align_check),
};

struct fwk_test_suite_desc test_suite = {
Expand Down Expand Up @@ -136,3 +138,40 @@ static void test_fwk_macros_power_of_two_check(void)
assert(result == test_data[i].verdict);
}
}

static void test_fwk_macros_align_check(void)
{
bool result;
struct alignment_test_data {
unsigned int value;
unsigned int alignment;
bool verdict;
} test_data[] = {
{ 0x0000, 0, false }, { 0x0000, 4, true },
{ 0x0000, 32, true }, { 0x4000, 4, true },
{ 0x4000, 8, true }, { 0x2004, 8, false },
{ 0x1000, 32, true }, { 0x4020, 32, true },
{ 0x4000, 15, false }, { 0x4000, 17, false },
{ 0x3008, 16, false }, { 0x9000, 64, true },
{ 0x9040, 64, true }, { 0x9048, 64, false },
{ 0x9040, 67, false }, { 0xFFFFFFFF, 0, false },
{ 0xFFFFFFFF, 4, false }, { 0xFFFFFFFF, 32, false },
};
unsigned int test_count = sizeof(test_data) / sizeof(test_data[0]);
struct alignment_test_data test;
/* Precedence tests */
test.value = 3;
test.alignment = 4;
result = FWK_IS_ALIGNED(test.value & 1, test.alignment + 1);
assert(result == false);

test.value = 0x4000;
test.alignment = 3;
result = FWK_IS_ALIGNED(test.value, test.alignment + 1);
assert(result == true);

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

0 comments on commit 75c1086

Please sign in to comment.