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 6452e0e40eb1a29e169b549c4a96a2f421ae6c2a Author: ouyangxiangzhen <[email protected]> AuthorDate: Tue Nov 17 15:48:28 2026 +0800 lib/math32: declare up_umulh64() in math32.h with a weak fallback The portable limb form of up_umulh64() is inlined at every user of the primitive, about twenty instructions per expansion in every kernel that divides or scales by a runtime value, and an architecture has no way to replace it. Declare up_umulh64() in math32.h itself. When the compiler has a native 128-bit multiply (_uint128_t, __INT128_DEFINED) it is a static inline that compiles to a single UMULH, so 64-bit targets need nothing else. Otherwise math32.h only declares it, and the definition is one out-of-line copy per image in libs/libc/machine: the architecture form in libs/libc/machine/<arch>/arch_umulh64.c, plus a weak portable fallback in libs/libc/machine/up_umulh64.c for the architectures that provide none. The weak fallback is listed after the per-architecture sources in both Make.defs and CMakeLists.txt: libc.a members are scanned in order, and a weak definition listed first would satisfy the reference before the strong one is reached. Only the declaration and the fallback land here; the ARM form follows in "libs/libc/machine/arm: implement up_umulh64() in UMULL assembly". Assisted-By: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/lib/math32.h | 38 ++++++++----------------- libs/libc/machine/CMakeLists.txt | 6 ++++ libs/libc/machine/Make.defs | 7 +++++ libs/libc/machine/up_umulh64.c | 61 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 27 deletions(-) diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h index 7cd6e4d1ecb..e5db10b7039 100644 --- a/include/nuttx/lib/math32.h +++ b/include/nuttx/lib/math32.h @@ -504,41 +504,25 @@ do \ } \ while(0) -/* 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. +/* UMULH: high 64 bits of a * b. One instruction inline when the + * compiler has a native 128-bit multiply; otherwise one out-of-line copy + * per image from libs/libc/machine (the architecture form, or the weak + * fallback). */ -#ifdef up_umulh64 -# define umulh64(a, b) up_umulh64(a, b) -#else +#ifdef __INT128_DEFINED -static inline_function uint64_t umulh64(uint64_t a, uint64_t b) +static inline_function uint64_t up_umulh64(uint64_t a, uint64_t b) { -#ifndef __INT128_DEFINED - /* If the compiler do not support _uint128_t */ - - uint64_t al = a & UINT32_MAX; - uint64_t ah = a >> 32; - uint64_t bl = b & UINT32_MAX; - uint64_t bh = b >> 32; - - uint64_t m1 = al * bl; - uint64_t m2 = ah * bl; - uint64_t m3 = al * bh; - uint64_t m4 = ah * bh; - - uint64_t c = (m1 >> 32) + (m2 & UINT32_MAX) + (m3 & UINT32_MAX); - - return m4 + (m2 >> 32) + (m3 >> 32) + (c >> 32); -#else /* Compiles to a single UMULH instruction on x86_64 and AArch64. */ _uint128_t a128 = a; _uint128_t res128 = a128 * b; return res128 >> 64; -#endif } + +#else +uint64_t up_umulh64(uint64_t a, uint64_t b); #endif /**************************************************************************** @@ -618,10 +602,10 @@ uint64_t invdiv_u64(uint64_t n, FAR const invdiv_param64_t *ip) uint8_t sh2 = ip->shift; /* t1 is the high half of the 128-bit product n * m. Both operands - * are runtime values here, so umulh64() is the right primitive. + * are runtime values here, so up_umulh64() is the right primitive. */ - t1 = umulh64(n, m); + t1 = up_umulh64(n, m); t2 = (n - t1) >> sh1; return (t1 + t2) >> sh2; diff --git a/libs/libc/machine/CMakeLists.txt b/libs/libc/machine/CMakeLists.txt index 6e0b698b1fc..41d3928a4d9 100644 --- a/libs/libc/machine/CMakeLists.txt +++ b/libs/libc/machine/CMakeLists.txt @@ -31,3 +31,9 @@ endif() if(CONFIG_MM_KASAN) target_sources(c PRIVATE arch_libc.c) endif() + +# The weak up_umulh64() fallback must come after the per-architecture sources: +# libc.a members are scanned in order and a weak definition satisfies the +# reference before the strong one is reached. + +target_sources(c PRIVATE up_umulh64.c) diff --git a/libs/libc/machine/Make.defs b/libs/libc/machine/Make.defs index 6da3930a51a..92644125f31 100644 --- a/libs/libc/machine/Make.defs +++ b/libs/libc/machine/Make.defs @@ -63,6 +63,13 @@ endif ifeq ($(CONFIG_ARCH_TRICORE),y) include $(TOPDIR)/libs/libc/machine/tricore/Make.defs endif + +# The weak up_umulh64() fallback must come after the per-architecture +# sources: libc.a members are scanned in order and a weak definition +# satisfies the reference before the strong one is reached. + +CSRCS += up_umulh64.c + DEPPATH += --dep-path machine VPATH += :machine diff --git a/libs/libc/machine/up_umulh64.c b/libs/libc/machine/up_umulh64.c new file mode 100644 index 00000000000..435b22db262 --- /dev/null +++ b/libs/libc/machine/up_umulh64.c @@ -0,0 +1,61 @@ +/**************************************************************************** + * libs/libc/machine/up_umulh64.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> +#include <stdint.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/* UMULH: high 64 bits of the 128-bit product a * b, the portable form of + * up_umulh64(). Weak so that libs/libc/machine/<arch> can replace it; + * this copy is used only when the architecture supplies none. A target + * with a native 128-bit multiply inlines up_umulh64() from + * include/nuttx/lib/math32.h and needs no copy here. + */ + +#if !defined(__INT128_DEFINED) + +uint64_t weak_function up_umulh64(uint64_t a, uint64_t b) +{ + uint64_t al = a & UINT32_MAX; + uint64_t ah = a >> 32; + uint64_t bl = b & UINT32_MAX; + uint64_t bh = b >> 32; + + uint64_t m1 = al * bl; + uint64_t m2 = ah * bl; + uint64_t m3 = al * bh; + uint64_t m4 = ah * bh; + + uint64_t c = (m1 >> 32) + (m2 & UINT32_MAX) + (m3 & UINT32_MAX); + + return m4 + (m2 >> 32) + (m3 >> 32) + (c >> 32); +} + +#endif /* !__INT128_DEFINED */
