This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit a821a92637cb93d1ffc52cb1765d08dcd631da8a
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Fri Nov 13 17:19:00 2026 +0800

    lib/math32: express log2ceil() as fls(n - 1)
    
    ceil(log2 n) is fls(n - 1) for n >= 1, so log2ceil() and LOG2_CEIL()
    can drop the power-of-two test and become a single fls() (or FLS)
    evaluation of n - 1.  This also fixes the multiple evaluation of n in
    the old ternary macros (n appeared four times in log2ceil()).
    
    fls() of 0 is undefined: CONFIG_HAVE_BUILTIN_CLZ makes it
    __builtin_clz(), which the GCC manual defines only for non-zero
    arguments.  n == 1 is exactly that input, so keep the zero away
    arithmetically -- flsx(((n) - 1) | 1) - ((n) == 1) -- instead of a
    ternary: it costs no branch on Thumb-1, where the conditional form
    degrades to a jump, and it stays an integer constant expression on
    clang, which folds __builtin_clz(0) to a non-constant.
    
    log2() itself is undefined for 0 and for negative arguments: outside
    n >= 1 the old macros' behavior was accidental, not a contract (n = 0
    simply changes from returning 0 to returning the bit width).  All
    callers pass n >= 1 -- the invdiv initializer requires d > 1, mm.h
    uses sizeof(), lib_fdcheck.c uses OPEN_MAX, nx_start.c uses
    CONFIG_PID_INITIAL_COUNT and the RISC-V MPU sites use region sizes --
    and the precondition is now stated next to the macros.
    
    Verified: the new macro agrees with the pre-series power-of-two form on
    every n in 1..2^24 and on the 64-bit boundary set (2^32-1, 2^32, 2^40,
    2^62, 2^63, 2^63+1, 2^64-1), 0 differences; it is an integer constant
    expression on gcc and clang for n = 1, 2, 3, 8, 9, 1024, 1025; the
    cortex-m3 and cortex-m0 images of the macro contain no conditional
    branch.
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/lib/math32.h | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h
index 368ebd610bb..b35bdcfbfd8 100644
--- a/include/nuttx/lib/math32.h
+++ b/include/nuttx/lib/math32.h
@@ -73,11 +73,12 @@
 
 #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.
+/* Returns the log2(n) rounded up and down; usable at compile time.
+ * n >= 1 is required: log2 is undefined for 0 and negative arguments.
+ * As with FLS(), n is evaluated multiple times.
  */
 
-#define LOG2_CEIL(n)  (IS_POWER_OF_2(n) ? FLS(n) - 1 : FLS(n))
+#define LOG2_CEIL(n)  (FLS((n) - 1))
 #define LOG2_FLOOR(n) (FLS(n) - 1)
 
 /****************************************************************************
@@ -149,7 +150,12 @@ extern "C"
  *
  ****************************************************************************/
 
-#define log2ceil(n) (IS_POWER_OF_2(n) ? (flsx(n) - 1) : flsx(n))
+/* ceil(log2 n) is fls(n - 1).  fls() of 0 -- the n == 1 case -- is
+ * undefined under __builtin_clz(), so OR the zero away and subtract the
+ * n == 1 term: branch-free, and still an integer constant expression.
+ */
+
+#define log2ceil(n) (flsx(((n) - 1) | 1) - ((n) == 1))
 
 /****************************************************************************
  * Name: log2floor

Reply via email to