Skip to content

[BUG] lib/math32: constant 64-bit division optimization (div64_const) is unreachable #21213

Closed

Description

@arnavsharma990

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:

  1. Added reusable HAVE_EXPRESSION_STATEMENT detection in include/nuttx/compiler.h, enabling GCC and Clang while excluding Green Hills.
  2. Replaced the dead guard in math32.h with the centralized capability macro.
  3. Replaced sizeof(typeof(n)) with standard-C sizeof(n) in three affected macros to support strict ISO C compilation.
  4. Added CONFIG_LIBC_DISABLE_DIV64_OPTIMIZATION (default n) in libs/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, and y.
  • Arithmetic equivalence verified for both implementations.
  • Strict C99 and C11 compilation tested, including clkcnt.h.
  • Full stm32f4discovery build completed using arm-none-eabi-gcc 16.2.0.
  • Linker-map and disassembly comparison confirmed removal of __aeabi_ldivmod in the optimized build.
  • Kconfig validation completed using kconfiglib.
  • nxstyle and git diff --check passed.

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

  1. added
    Type: Bug
    on Oct 30, 2026
  2. added
    Arch: arm
    OS: Mac
    on Oct 30, 2026
  3. added 7 commits that reference this issue on Oct 30, 2026
    e28138e
    fb21fc1
    4b50241
    491a21d
    74fe292
    286e7fd
    5680cf0
  4. harshilv17 commented on Nov 10, 2026

    @harshilv17

    I'd like to take this. Plan: replace the undefined CONFIG_HAVE_EXPRESSION_STATEMENT guard with a check for compiler support of statement expressions (GCC/Clang), so div64_const is used where it was meant to be, and verify the result against plain division on the sim target.

  5. xiaoxiang781216 commented on Nov 10, 2026

    @xiaoxiang781216
    Contributor

    I'd like to take this. Plan: replace the undefined CONFIG_HAVE_EXPRESSION_STATEMENT guard with a check for compiler support of statement expressions (GCC/Clang), so div64_const is used where it was meant to be, and verify the result against plain division on the sim target.

    @harshilv17 #21491 already fix the problem.

  6. harshilv17 commented on Nov 10, 2026

    @harshilv17

    oh sorry!

  7. xiaoxiang781216 commented on Nov 10, 2026

    @xiaoxiang781216
    Contributor

    oh sorry!

    no problem, please review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arch: armArea: Build systemOS: MacType: Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions