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

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

commit 995d7b4d2d2b17fe1f9949a6f3d01d6c098a0064
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Thu Nov 26 10:22:04 2026 +0800

    lib/math32: optimize invdiv_init_param code size.
    
    For any runtime divisor, invdiv_init_param may expand LOG2_CEIL(n) to 
multiple bit
    operations that increases code size significantly. This commit fixed the
    issue.
    
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/lib/math32.h | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h
index a8b7cfe9d5d..b03096a681f 100644
--- a/include/nuttx/lib/math32.h
+++ b/include/nuttx/lib/math32.h
@@ -257,8 +257,13 @@ extern "C"
 
 #define invdiv_init_param(d, ip) \
   sizeof((ip)->mult) == sizeof(uint32_t) ? \
-    invdiv_init_param32((d), (FAR invdiv_param32_t *)(ip)) : \
-    invdiv_init_param64((d), (FAR invdiv_param64_t *)(ip))
+    invdiv_init_param32((d), log2ceil(d), (FAR invdiv_param32_t *)(ip)) : \
+    invdiv_init_param64((d), log2ceil(d), (FAR invdiv_param64_t *)(ip))
+
+#define invdiv_init_param_const(d, ip) \
+  sizeof((ip)->mult) == sizeof(uint32_t) ? \
+    invdiv_init_param32((d), LOG2_CEIL(d), (FAR invdiv_param32_t *)(ip)) : \
+    invdiv_init_param64((d), LOG2_CEIL(d), (FAR invdiv_param64_t *)(ip))
 
 /****************************************************************************
  * Name: invdiv_init_param32
@@ -269,6 +274,7 @@ extern "C"
  *
  * Input Parameters:
  *   d - The divisor (unsigned integer). d != 0 and d != 1 is required.
+ *   l - log2ceil(d)
  *
  * Output Parameters:
  *   ip    - The invariant division parameter to be cached.
@@ -276,9 +282,8 @@ extern "C"
  ****************************************************************************/
 
 static inline_function
-void invdiv_init_param32(uint32_t d, FAR invdiv_param32_t *ip)
+void invdiv_init_param32(uint32_t d, int l, FAR invdiv_param32_t *ip)
 {
-  int      l  = LOG2_CEIL(d);
   uint64_t t1 = (uint64_t)1u << 32;
   uint64_t t2 = (uint64_t)1u << l;
 
@@ -458,6 +463,7 @@ uint64_t up_umulh64(uint64_t a, uint64_t b);
  *   d - The divisor (unsigned integer), d != 0 and d != 1 is required,
  *       and d <= 2^63 is asserted: the 1 << l setup is undefined for
  *       larger divisors.
+ *   l - log2ceil(d)
  *
  * Output Parameters:
  *   ip    - The invariant division parameter to be cached.
@@ -465,9 +471,8 @@ uint64_t up_umulh64(uint64_t a, uint64_t b);
  ****************************************************************************/
 
 static inline_function
-void invdiv_init_param64(uint64_t d, FAR invdiv_param64_t *ip)
+void invdiv_init_param64(uint64_t d, int l, FAR invdiv_param64_t *ip)
 {
-  int      l = LOG2_CEIL(d);
   uint64_t t = ((uint64_t)1u << l) - d;
 
   DEBUGASSERT(d > 1u && d <= ((uint64_t)1u << 63));
@@ -525,7 +530,7 @@ static inline_function uint64_t div64_const32(uint64_t n, 
uint32_t b)
   invdiv_param64_t ip;
 
   DEBUGASSERT(b > 1u);
-  invdiv_init_param(b, &ip);
+  invdiv_init_param_const(b, &ip);
 
   return invdiv_u64(n, &ip);
 }

Reply via email to