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 24191387206224b3aa1b95a77c58748111fbc926
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Tue Nov 17 11:29:33 2026 +0800

    lib/math32: unify the invdiv init behind invdiv_init_param()
    
    The invdiv setup had no shared entry: callers named the width-specific
    initializer (invdiv_init_param32/64()) themselves, so every call site
    carried the width knowledge of its parameter block, and each init
    computed ceil(log2 d) with log2ceil() while the header had just
    standardized the value on LOG2_CEIL() ("lib/math32: pick the
    LOG2_CEIL()/LOG2_FLOOR() form by toolchain").  A separate foldable
    constant entry would only be needed because ceil(log2 d) once had two
    spellings; with one spelling per toolchain the split buys nothing.
    
    Add one dispatch and drop the rest: invdiv_init_param(d, ip) is a
    ternary on sizeof((ip)->mult) that casts the block to
    invdiv_param32_t or invdiv_param64_t and selects the initializer at
    compile time, and invdiv_init_param32/64() keep the (d, ip) signature
    with l = LOG2_CEIL(d) computed inside as the first statement.  A
    run-time divisor and a constant divisor now share one entry; the
    separate constant entry (invdiv_init_param_const()) is retired and not
    carried.  The call sites in include/nuttx/timers/oneshot.h and
    arch/tricore/src/common/tricore_perf.c go through the dispatch instead
    of naming an initializer.
    
    No functional change: LOG2_CEIL(d) and the former log2ceil(d) agree by
    the pick above, and at both call sites the parameter block is
    invdiv_param64_t (oneshot lower->invdiv_freq, tricore g_invdiv_param),
    so the dispatch selects invdiv_init_param64() exactly as the direct
    call did.  Host battery at this commit, 4000000 random (n, b) pairs
    checking div64_const32() and invdiv_u32() against native division:
    "u64 4M fail=0  u32 4M fail=0", with and without
    CONFIG_HAVE_BUILTIN_CLZ.
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 arch/tricore/src/common/tricore_perf.c |  2 +-
 include/nuttx/lib/math32.h             | 60 ++++++++++++++++++++++++----------
 include/nuttx/timers/oneshot.h         |  2 +-
 3 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/arch/tricore/src/common/tricore_perf.c 
b/arch/tricore/src/common/tricore_perf.c
index e5ed504dba8..0f9e23708e9 100644
--- a/arch/tricore/src/common/tricore_perf.c
+++ b/arch/tricore/src/common/tricore_perf.c
@@ -81,7 +81,7 @@ void up_perf_init(void *arg)
 {
   g_cpu_freq = (unsigned long)(uintptr_t)arg;
 
-  invdiv_init_param64(g_cpu_freq, &g_invdiv_param);
+  invdiv_init_param(g_cpu_freq, &g_invdiv_param);
 
   tricore_reset_ccnt();
 }
diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h
index 5aed59b089d..17bbc66c257 100644
--- a/include/nuttx/lib/math32.h
+++ b/include/nuttx/lib/math32.h
@@ -78,9 +78,14 @@
  * As with FLS(), n is evaluated multiple times.
  */
 
-#define LOG2_CEIL(n)  (FLS((n) - 1))
 #define LOG2_FLOOR(n) (FLS(n) - 1)
 
+#ifdef CONFIG_HAVE_BUILTIN_CLZ
+#  define LOG2_CEIL(n) log2ceil(n)
+#else
+#  define LOG2_CEIL(n) (FLS((n) - 1))
+#endif
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -396,12 +401,34 @@ static inline_function uint64_t div64_const(uint64_t n, 
uint64_t base)
  * into multiplication and right-shifting.
  *
  * Usage:
- * If you want to do n/d division where d is an invariant integer,
- * initialize the parameters with invdiv_init_param32() /
- * invdiv_init_param64() first, then do the division with
- * invdiv_u32() / invdiv_u64().
+ * For n/d where d is an invariant integer, fill the parameter block
+ * with invdiv_init_param(d, &ip), then divide with invdiv_u32() or
+ * invdiv_u64() to match the block width.
  */
 
+/****************************************************************************
+ * Name: invdiv_init_param
+ *
+ * Description:
+ *   Fill an invdiv_param32_t or an invdiv_param64_t block with the
+ *   invariant-division parameters of d; the width of ip->mult selects
+ *   the initializer.  d may be a constant or a run-time value.
+ *
+ * Input Parameters:
+ *   d  - The divisor, d > 1.
+ *   ip - The parameter block; its type selects the initializer.
+ *
+ ****************************************************************************/
+
+/* Dispatch on the multiplier width of the parameter block: d may be a
+ * runtime value or a constant, either way l is computed inside.
+ */
+
+#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))
+
 /****************************************************************************
  * Name: invdiv_init_param32
  *
@@ -420,7 +447,7 @@ static inline_function uint64_t div64_const(uint64_t n, 
uint64_t base)
 static inline_function
 void invdiv_init_param32(uint32_t d, FAR invdiv_param32_t *ip)
 {
-  int      l  = log2ceil(d);
+  int      l  = LOG2_CEIL(d);
   uint64_t t1 = (uint64_t)1 << 32;
   uint64_t t2 = (uint64_t)1 << l;
 
@@ -591,18 +618,15 @@ uint64_t up_umulh64(uint64_t a, uint64_t b);
  * Name: invdiv_init_param64
  *
  * Description:
- *   Calculate the invariant-division parameters from the divisor: the
- *   multiplier is floor((2^l - d) * 2^64 / d) + 1
- *   through the single 128-by-64 division invdiv_udiv128(), with
- *   l = ceil(log2 d).  The division's normalizer s = clz(d) is the
- *   branch-free 64 - l - (((d - 1) & d) == 0): l is one more than
- *   floor(log2 d) exactly when d is not a power of two (d >= 2 makes
- *   the bare bit test exact).
+ *   Calculate the invariant-division parameters from the divisor, with
+ *   l = ceil(log2 d) and the multiplier floor((2^l - d) * 2^64 / d) + 1
+ *   from one 128-by-64 division.  The normalizer s = clz(d) is derived
+ *   from l without a branch.
  *
  * Input Parameters:
- *   d - The divisor (unsigned integer), d != 0 and d != 1 is required;
- *       d > 2^63 keeps the pre-existing undefined behavior of the
- *       1 << l setup (d == 2^63 is in contract).
+ *   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.
  *
  * Output Parameters:
  *   ip    - The invariant division parameter to be cached.
@@ -612,10 +636,10 @@ 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)
 {
-  int      l = log2ceil(d);
+  int      l = LOG2_CEIL(d);
   uint64_t t = ((uint64_t)1 << l) - d;
 
-  DEBUGASSERT(d > 1);
+  DEBUGASSERT(d > 1 && d <= ((uint64_t)1 << 63));
 
 #ifdef __INT128_DEFINED
   _uint128_t one128 = 1;
diff --git a/include/nuttx/timers/oneshot.h b/include/nuttx/timers/oneshot.h
index d332017f5ae..e5eb5dcb072 100644
--- a/include/nuttx/timers/oneshot.h
+++ b/include/nuttx/timers/oneshot.h
@@ -320,7 +320,7 @@ void oneshot_count_init(FAR struct oneshot_lowerhalf_s 
*lower,
 
   ASSERT(frequency > 1);
 
-  invdiv_init_param64(frequency, &lower->invdiv_freq);
+  invdiv_init_param(frequency, &lower->invdiv_freq);
 #  endif
 }
 

Reply via email to