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 e1a84eb886d84b32ca23fb767b46d2d9ceba73bd Author: ouyangxiangzhen <[email protected]> AuthorDate: Tue Nov 10 14:07:31 2026 +0800 lib/math32: dispatch div_const() on compile-time constness div_const() picked the magic path purely from the dividend width, which had two bad cases: a runtime divisor ran the unfolded magic setup at every call (measured 6-17x slower than a plain division), and a non-constant argument kept the expression from being an integer constant expression, so static initializers with constant arguments failed to compile ("initializer element is not constant"). Dispatch on is_constexpr() instead. A non-constant divisor takes the plain division directly, and a constant dividend with a constant divisor also takes the plain division so the whole expression stays an ICE. Only a runtime dividend with a compile-time constant divisor keeps the magic path, gated by the dividend and pointer width: only on 32-bit targets, where a plain division would become a __udivdi3 call. On 64-bit targets the compiler's own division-by-constant sequence is smaller and faster than the portable magic (aarch64 GCC 14 -O2 compiles div_const(n, 1000) to 8 instructions with one umulh versus 26 with four umaddl; rv64gc grows 30 to 108 bytes the other way), so plain division is kept there, matching the original div64_const() design. The family takes non-negative arguments only; negative values are outside the contract and unspecified, so no sign gating is needed in the dispatch. The call sites were audited against this contract: the direct callers pass uint64_t/clkcnt_t values, and the *2TICK() conversions see only constants, unsigned ioctl arguments and timespec values within their POSIX domain (a negative tv_nsec is an invalid timespec), so every in-tree use is inside the domain. Compilers whose is_constexpr() is (0) always take the plain division: correct, just without the optimization. Evidence: static initializers with constant arguments now compile and fold to the hand-computed values on gcc -m64/-m32, clang and TCC (run output) and on SDCC z80/mcs51 (asm constants 100/142/143/143/6); with gcc -O2 a runtime divisor disassembles to a single div instruction with no magic setup while the constant-divisor path disassembles to the folded multiply-shift sequence with zero div instructions; the differential test against the pre-change macro snapshots stays at 0 mismatches over 1804 non-negative checks; qemu-intel64 build passes. Assisted-By: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/lib/math32.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h index b9da9150f06..f82c02b79ff 100644 --- a/include/nuttx/lib/math32.h +++ b/include/nuttx/lib/math32.h @@ -358,8 +358,15 @@ static inline_function uint64_t div64_const(uint64_t n, uint64_t base) } #endif +/* The div_const*() family takes non-negative arguments only, + * negative values are outside the contract. + */ + #define div_const(n, base) \ - ((sizeof((n)) == sizeof(uint64_t)) ? div64_const(n, base) : ((n) / (base))) + ((is_constexpr(base) && !is_constexpr(n) && \ + sizeof((n)) == sizeof(uint64_t) && UINTPTR_MAX == UINT32_MAX) ? \ + div64_const(n, base) : ((n) / (base))) + /* Unit constant in the essential type of x (x > 0), so MISRA C Rule 10.4 * holds for both signed and unsigned arguments. */
