arnavsharma990 opened a new issue, #21213:
URL: https://github.com/apache/nuttx/issues/21213
### Description / Steps to reproduce the issue
<html><head></head><body><h3><span>What is the
bug?</span></h3><p><code><span>include/nuttx/lib/math32.h</span></code><span>
guards the optimized constant 64-bit division path
(</span><code><span>div64_const</span></code><span> /
</span><code><span>div64_const32</span></code><span>) behind
</span><code><span>CONFIG_HAVE_EXPRESSION_STATEMENT</span></code><span>. This
symbol is never defined anywhere in the repository, making the guard
permanently false. Consequently, affected call sites fall back to ordinary
64-bit division, potentially pulling in software division helpers
(</span><code><span>__aeabi_ldivmod</span></code><span> /
</span><code><span>_udivmoddi4</span></code><span>) on 32-bit
targets.</span></p><h3><span>Why is this a bug?</span></h3><p><span>The
optimization was introduced in commit
</span><code><span>7fc03e0084</span></code><span> but guarded by two undefined
symbols: </span><code><span>CONFIG_HAVE_LONG_LONG</span></code><span> and
</span><code><span>CONFIG_HAVE_EX
PRESSION_STATEMENT</span></code><span>. Commit
</span><code><span>c32b683</span></code><span> later removed the first guard,
leaving the optimization dependent on
</span><code><span>CONFIG_HAVE_EXPRESSION_STATEMENT</span></code><span>, which
is still undefined.</span></p><h3><span>Steps to reproduce</span></h3><ol
start="1"><li><p><span>Run:</span></p><pre><code><span>grep -rn
"HAVE_EXPRESSION_STATEMENT" nuttx nuttx-apps</span></code></pre><p><span>Only
match:
</span><code><span>include/nuttx/lib/math32.h:321</span></code><span>.</span></p></li><li><p><span>Confirm
no Kconfig definition exists:</span></p><pre><code><span>grep -rn "config
HAVE_EXPRESSION_STATEMENT" nuttx</span></code></pre></li><li><p><span>Inspect
</span><code><span>math32.h</span></code><span>:</span></p><ul><li><p><span>Lines
262–318:
</span><code><span>div64_const32</span></code><span>.</span></p></li><li><p><span>Lines
321–346: guarded
</span><code><span>div64_const</span></code><span>.</span></p></li><li><p
><span>Lines 348–352: fallback division
>wrappers.</span></p></li></ul></li><li><p><span>Build a minimal 32-bit target
>with a non-power-of-two tick rate and inspect the linker map. The clock path
>uses </span><code><span>__aeabi_ldivmod</span></code><span> instead of the
>intended reciprocal multiplication.</span></p></li></ol><h3><span>Affected
>call
>sites</span></h3><ul><li><p><code><span>include/nuttx/clock.h</span></code><span>
> — clock conversion functions and
>macros.</span></p></li><li><p><code><span>sched/wdog/wd_start.c:369</span></code><span>
> — watchdog
>timing.</span></p></li><li><p><code><span>include/nuttx/timers/oneshot.h:366</span></code><span>
> — oneshot
>timer.</span></p></li><li><p><code><span>include/nuttx/timers/clkcnt.h</span></code><span>
> — clock counter conversions.</span></p></li></ul><h3><span>Measured
>improvement</span></h3><p><span>Controlled experiment on
></span><code><span>stm32f4discovery</span></code><span> minimal
>build:</span></p>
| Text | Data | BSS
-- | -- | -- | --
Baseline | 28,212 | 800 | 2,136
Optimized guard enabled | 28,108 | 800 | 2,136
Difference | −104 | 0 | 0
<p><span>Disassembly confirmed that
</span><code><span>clock_systime_timespec</span></code><span> replaced the
</span><code><span>__aeabi_ldivmod</span></code><span> call with hardware
multiply instructions (</span><code><span>umlal</span></code><span>). This
measurement is specific to this configuration.</span></p><h3><span>Compiler
portability</span></h3><p><span>The optimized macros use GNU statement
expressions and </span><code><span>typeof</span></code><span>. GCC and Clang
support these, but Green Hills (</span><code><span>__ghs__</span></code><span>)
compatibility needs consideration. The replacement guard should follow NuttX's
existing compiler capability conventions. I am leaving the exact portable
solution open for maintainer discussion.</span></p></body></html>
### On which OS does this issue occur?
[OS: Mac]
### What is the version of your OS?
macOS (Darwin)
### NuttX Version
master (commit `635ac55e50`)
### Issue Architecture
[Arch: arm]
### Issue Area
[Area: Other], [Area: Build System]
### Host information
`arm-none-eabi-gcc 16.2.0` (Homebrew), `make` host_info not available for
this configuration
### Verification
- [x] I have verified before submitting the report.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]