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 9edc61f7ee0eb0b67a9c852080f665e6af82df89 Author: ouyangxiangzhen <[email protected]> AuthorDate: Thu Nov 5 16:55:47 2026 +0800 lib/math32: expose the UMULH primitive as up_umulh64() invdiv_umulh64() computes the high 64 bits of a 64x64 product -- the UMULH primitive that some architectures implement in a single instruction and that others best provide in assembly when the compiler lacks uint128_t -- but it was a fixed static inline with no override point. Give it the up_umulh64() name, following the up_* convention for architecture-provided primitives, and let an architecture decide how to provide it. The portable form stays as the fallback. Behavior is unchanged: the fallback passes edge cases and 1M randomized differential tests against the native 128-bit computation on both the uint128_t path and the software decomposition path. Assisted-By: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/lib/math32.h | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h index 7c099b4be56..b4694991691 100644 --- a/include/nuttx/lib/math32.h +++ b/include/nuttx/lib/math32.h @@ -479,11 +479,16 @@ do \ } \ while(0) -/* Helper function to calculate 2 64-bit unsigned integers multiplication - * The result is the highest half of the 128-bit unsigned integer. +/* UMULH: high 64 bits of a * b. Architectures may override it with a + * native or assembly implementation via up_umulh64(), which must + * evaluate both arguments. */ -static inline_function uint64_t invdiv_umulh64(uint64_t a, uint64_t b) +#ifdef up_umulh64 +# define umulh64(a, b) up_umulh64(a, b) +#else + +static inline_function uint64_t umulh64(uint64_t a, uint64_t b) { #ifndef __SIZEOF_INT128__ /* If the compiler do not support uint128_t */ @@ -502,21 +507,14 @@ static inline_function uint64_t invdiv_umulh64(uint64_t a, uint64_t b) return m4 + (m2 >> 32) + (m3 >> 32) + (c >> 32); #else - /* This code will be compiled to UMULH instruction - * on the architectures like x86_64 and AArch64. - * The UMULH instruction can calculate the highest half - * of the 128-bit multiplication result. - * It is faster than any of software implementation. - * If the compiler do not support uint128_t, it is better to manually - * implement this function using UMULH instructions via - * the inline assembly. - */ + /* Compiles to a single UMULH instruction on x86_64 and AArch64. */ __uint128_t a128 = a; __uint128_t res128 = a128 * b; return res128 >> 64; #endif } +#endif /**************************************************************************** * Name: invdiv_init_param64 @@ -594,12 +592,11 @@ uint64_t invdiv_u64(uint64_t n, FAR const invdiv_param64_t *ip) uint8_t sh1 = 1; uint8_t sh2 = ip->shift; - /* Calculate the 128-bit mres = 64-bit n * 64-bit m, where t1 = mres >> 64. - * Please do not use `umul64_const` or `umul64` here. - * The `invdiv_umulh64` has significant better performance over them. + /* t1 is the high half of the 128-bit product n * m. Both operands + * are runtime values here, so umulh64() is the right primitive. */ - t1 = invdiv_umulh64(n, m); + t1 = umulh64(n, m); t2 = (n - t1) >> sh1; return (t1 + t2) >> sh2;
