Repository navigation
Description
Description / Steps to reproduce the issue*
The optimized constant 64-bit division implementation (div64_const / div64_const32) in include/nuttx/lib/math32.h is effectively unreachable because it is guarded by CONFIG_HAVE_EXPRESSION_STATEMENT, which is not a defined NuttX configuration option.
The guard has been ineffective since the optimization was introduced in commit 7fc03e0084. A subsequent change (c32b683) removed the other condition, leaving the undefined symbol as the only guard.
As a result, affected call sites fall back to ordinary 64-bit division. On 32-bit targets, this can pull in software division helpers (__aeabi_ldivmod → _udivmoddi4) instead of the intended Granlund–Montgomery reciprocal multiplication.
Resolution:
Addressed in PR #21214.
The fix includes:
- Added reusable
HAVE_EXPRESSION_STATEMENTdetection ininclude/nuttx/compiler.h, enabling GCC and Clang while excluding Green Hills. - Replaced the dead guard in
math32.hwith the centralized capability macro. - Replaced
sizeof(typeof(n))with standard-Csizeof(n)in three affected macros to support strict ISO C compilation. - Added
CONFIG_LIBC_DISABLE_DIV64_OPTIMIZATION(defaultn) inlibs/libc/Kconfig, allowing users to disable the optimization on size-constrained targets.
Performance and code-size impact:
The primary objective is performance, not code-size reduction. The optimization replaces software 64-by-32-bit division with reciprocal multiplication.
MemBrowse measurements show varying binary-size impacts:
| Target | Text-size change |
|---|---|
| mirtoo | -1,664 B |
| arduino-mega2560 | -208 B |
| stm32-nucleo-f103rb | -88 B |
| rx65n-rsk2mb | 0 B |
| hifive1-revb | +372 B |
| esp32-devkitc | +260 B |
| qemu-intel64 | +26 B |
| s698pm-dkit | +46,464 B (+12.1%) |
The substantial increase on s698pm-dkit is documented for maintainer consideration. The new Kconfig option allows users to disable the optimization where code size is a priority.
Verification:
- GCC and Clang capability detection verified.
- Green Hills exclusion checked through preprocessor simulation (no genuine GHS toolchain available).
- Optimized and fallback paths tested with the Kconfig option undefined,
n, andy. - Arithmetic equivalence verified for both implementations.
- Strict C99 and C11 compilation tested, including
clkcnt.h. - Full
stm32f4discoverybuild completed usingarm-none-eabi-gcc 16.2.0. - Linker-map and disassembly comparison confirmed removal of
__aeabi_ldivmodin the optimized build. - Kconfig validation completed using kconfiglib.
nxstyleandgit diff --checkpassed.
Native Renesas RX65N and genuine Green Hills builds could not be performed because the respective toolchains were unavailable.
PR: #21214
Final implementation commit: 286e7fd8ab
Environment
On which OS does this issue occur?*
NuttX (target RTOS)
What is the version of your OS?*
N/A — source-level issue in NuttX.
NuttX Version*
Apache NuttX master.
Issue Architecture*
Multiple architectures, particularly 32-bit targets using software 64-bit division.
Tested on ARM Cortex-M4 (stm32f4discovery). MemBrowse comparisons also cover AVR, ESP32, RISC-V, MIPS, x86-64 and Renesas RX.
Issue Area*
Library / libc / Math / Compiler portability
Host information
macOS (Darwin), Apple Clang 21.0.0 and arm-none-eabi-gcc 16.2.0.
Verification
I have verified before submitting this report.
Activity
lib/math32: enable unreachable constant 64-bit division optimization
lib/math32: enable unreachable constant 64-bit division optimization
lib/math32: enable unreachable constant 64-bit division optimization
lib/math32: enable unreachable constant 64-bit division optimization
lib/math32: enable unreachable constant 64-bit division optimization
lib/math32: enable unreachable constant 64-bit division optimization
lib/math32: enable unreachable constant 64-bit division optimization
harshilv17 commentedon Nov 10, 2026
I'd like to take this. Plan: replace the undefined
CONFIG_HAVE_EXPRESSION_STATEMENTguard with a check for compiler support of statement expressions (GCC/Clang), sodiv64_constis used where it was meant to be, and verify the result against plain division on thesimtarget.xiaoxiang781216 commentedon Nov 10, 2026
@harshilv17 #21491 already fix the problem.
harshilv17 commentedon Nov 10, 2026
oh sorry!
xiaoxiang781216 commentedon Nov 10, 2026
no problem, please review it.