diff --git a/include/toolchain/gcc.h b/include/toolchain/gcc.h index 1c7008a9664..65ebbc08900 100644 --- a/include/toolchain/gcc.h +++ b/include/toolchain/gcc.h @@ -462,5 +462,21 @@ do { \ _value_a_ < _value_b_ ? _value_a_ : _value_b_; \ }) +/** + * @brief Calculate power of two ceiling for some nonzero value + * + * @param x Nonzero unsigned long value + * @return X rounded up to the next power of two + */ +#ifdef CONFIG_64BIT +#define Z_POW2_CEIL(x) ((1UL << (63U - __builtin_clzl(x))) < x ? \ + 1UL << (63U - __builtin_clzl(x) + 1U) : \ + 1UL << (63U - __builtin_clzl(x))) +#else +#define Z_POW2_CEIL(x) ((1UL << (31U - __builtin_clzl(x))) < x ? \ + 1UL << (31U - __builtin_clzl(x) + 1U) : \ + 1UL << (31U - __builtin_clzl(x))) +#endif + #endif /* !_LINKER */ #endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */ diff --git a/tests/arch/arm/arm_sw_vector_relay/src/arm_sw_vector_relay.c b/tests/arch/arm/arm_sw_vector_relay/src/arm_sw_vector_relay.c index 3631a5be21e..3fee52d2640 100644 --- a/tests/arch/arm/arm_sw_vector_relay/src/arm_sw_vector_relay.c +++ b/tests/arch/arm/arm_sw_vector_relay/src/arm_sw_vector_relay.c @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -42,7 +43,7 @@ void test_arm_sw_vector_relay(void) * interrupt vector table respect the VTOR.TBLOFF alignment * requirements. */ - uint32_t mask = MAX(128, POW2_CEIL(4 * (16 + CONFIG_NUM_IRQS))) - 1; + uint32_t mask = MAX(128, Z_POW2_CEIL(4 * (16 + CONFIG_NUM_IRQS))) - 1; zassert_true(((vector_table_addr) & mask) == 0, "vector table not properly aligned: 0x%x\n",