From caff9eba2899181654d9a2589236b510a19f8ce3 Mon Sep 17 00:00:00 2001 From: Yanfeng Liu Date: Thu, 30 May 2024 15:10:06 +0800 Subject: [PATCH] lib/math32.h: add IS_POWER_OF_2 macro This adds IS_POWER_OF_2 macro to help catching config errors at build time. Signed-off-by: Yanfeng Liu Signed-off-by: Yanfeng Liu --- include/nuttx/lib/math32.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h index 125e8ffab3..cafbd23e56 100644 --- a/include/nuttx/lib/math32.h +++ b/include/nuttx/lib/math32.h @@ -52,11 +52,15 @@ #define FLS2(n) ((n) & 0x2 ? 1 + FLS1 ((n) >> 1) : FLS1 (n)) #define FLS1(n) ((n) & 0x1 ? 1 : 0) +/* Checks if an integer is power of two at compile time */ + +#define IS_POWER_OF_2(n) ((n) > 0 && ((n) & (n - 1)) == 0) + /* Returns round up and round down value of log2(n). Note: it can be used at * compile time. */ -#define LOG2_CEIL(n) ((n) & (n - 1) ? FLS(n) : FLS(n) - 1) +#define LOG2_CEIL(n) (IS_POWER_OF_2(n) ? FLS(n) - 1 : FLS(n)) #define LOG2_FLOOR(n) (FLS(n) - 1) /****************************************************************************