Skip to content

libs/libc: enable div64_const on 32bit targets - #20996

Closed
yf13 wants to merge 7 commits into
apache:masterfrom
yf13:pull-20414-div64
Closed

yf13 wants to merge 7 commits into
apache:masterfrom
yf13:pull-20414-div64

Conversation

@yf13

@yf13 yf13 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This tries to help issues/20414 by enabling div64_const in math32.h for 32-bit devices, as div64_const uses multiply operations to avoid compiler's 64bit dividing library when divisor is constant.

The macro exists in math32.h for time but not enabled. This enables it selectively by adding the missed CONFIG_HAVE_EXPRESSION_STATEMENT in compliler.h for selected optimization and archs.

Note if 64bit divisor library is unavoidable (e.g. stm32f334-disco/buckboost), this method doesnt help.

Impact

It enables div64_const() macro in math32.h, and uses it for lib_ultoa_invert.c to drop the dependency to 64bit divide library functions. Some MemBrowse result shall show reduced flash usage.

Testing

Checked on Ubuntu 22 with local small config rv-virt:hello with stock riscv64-unknown-elf 10.2:

$ size -G nuttx ../orig/nuttx
         text       data        bss      total filename
        23364        964       3760      28088 nuttx
        24492        868       3760      29120 ../orig/nuttx

$ qemu-system-riscv32 -M virt,aclint=on -semihosting -nographic -bios nuttx
Hello, World!!
0 11 12 20 1777777777777777777777
0 9 10 16 18446744073709551615
0 9 a 10 ffffffffffffffff

And local small stm32f103-minimum:hello (compile only) with stock arm-none-eabi-gcc 10.3.1 :

```sh
$ size -G nuttx ../orig/nuttx
      text       data        bss      total filename
     17865        248       1680      19793 nuttx
     18713        256       1680      20649 ../bbb/nuttx
```

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .data +32 B, .text +266 B, .trampolines -4 B (+0.5%, 65,426 B / 262,144 B, total: 25% used)
  • sram: .data +32 B (+1.2%, 2,722 B / 8,192 B, total: 33% used)

esp32-devkitc

  • ROM: .flash.rodata +104 B, .flash.text +492 B (+0.5%, 128,368 B / 4,194,272 B, total: 3% used)
  • drom0_0_seg: .flash.rodata +104 B (+0.8%, 13,900 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +492 B (+0.5%, 94,240 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +244 B (+0.3%, 84,792 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text -4,572 B (-7.0%, 62,148 B / 131,072 B, total: 47% used)

qemu-armv8a

  • Code: .rodata +32 B, .text.file_seek +28 B, .text.file_vopen +132 B, .text.fnmatch +12 B, .text.getrlimit -16 B, .text.init_main -16 B, .text.load_module +16 B, .text.mm_memalign +12 B, .text.mqueue_test +60 B, .text.nxsig_action -208 B, .text.nxsig_clockwait +136 B, .text.nxsig_timedwait +4 B, .text.nxtask_setup_fork +32 B, .text.readline_fd_ex -4 B, .text.spinlock_test -16 B, .text.task_uninit_info +28 B, .text.tmpfs_alloc_directory.isra.0 +28 B, .text.tmpfs_fstat +8 B, .text.tmpfs_open +28 B, .text.tmpfs_readdir +8 B, .text.tmpfs_stat +8 B, .text.uart_ioctl +24 B, .text.vsprintf_internal.constprop.0 -64 B, .text.wakeup_action +28 B (+0.3%, 354,564 B)
  • Data: .bss.g_kthread_group +8 B (+0.0%, 77,591 B)

qemu-intel64

  • Code: .text +1,446 B (+0.0%, 8,688,173 B)
  • Data: .data +32 B, .rodata +32 B (+0.1%, 125,081 B)

rx65n-rsk2mb

  • RAM: .bss +16 B (+0.2%, 6,554 B / 262,144 B, total: 3% used)
  • ROM: .rodata +32 B, .text +624 B (+0.7%, 90,440 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +3,392 B (+0.9%, 385,568 B)

stm32-nucleo-f103rb

  • flash: .text -636 B (-1.9%, 33,476 B / 131,072 B, total: 26% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

anchao
anchao previously approved these changes Oct 26, 2026
@xiaoxiang781216 xiaoxiang781216 linked an issue Oct 26, 2026 that may be closed by this pull request
1 task done
Comment thread sched/Kconfig
config DISABLE_ALL_SIGNALS
bool "Disable all signal support"
select DISABLE_POSIX_TIMERS
select DISABLE_MQUEUE_NOTIFICATION

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let change select to depend on

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to new pr, don't mix the different fix in one pr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is encountered here when using DEFAULT_SMALL with DISABLE_ALL_SIGNALS, otherwise I wont see it. Since it is already a separate commit, what is the additional benefit of an extra PR?

Comment thread include/nuttx/lib/math32.h Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
@yf13
yf13 force-pushed the pull-20414-div64 branch 3 times, most recently from 3496698 to f5b1b3e Compare October 27, 2026 02:39
Comment thread sched/Kconfig Resolved
Comment thread include/nuttx/lib/math32.h Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
@yf13
yf13 force-pushed the pull-20414-div64 branch from f5b1b3e to b8d9d5a Compare October 27, 2026 06:22
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: S The size of the change in this PR is small labels Oct 27, 2026

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit "math32.h: fix CI errors" fixes code from "libc: use div64_const for non-64bit".

Fix: squash it into that commit and keep wangzhi16's Signed-off-by.


No test prints numbers, so the ffs() error was not seen.

Fix: add a log that prints 0, 9, 10, 16 and UINT64_MAX with %llo, %llu and %llx on a 32-bit target.

Comment thread libs/libc/stream/lib_ultoa_invert.c Resolved
Comment thread include/nuttx/lib/math32.h Resolved
Comment thread sched/Kconfig Resolved
@yf13
yf13 force-pushed the pull-20414-div64 branch from b8d9d5a to 29249fe Compare October 28, 2026 06:51
@github-actions github-actions Bot added Size: S The size of the change in this PR is small Board: arm Board: risc-v and removed Size: M The size of the change in this PR is medium labels Oct 28, 2026
@yf13
yf13 force-pushed the pull-20414-div64 branch 2 times, most recently from 2f97d4a to 420351f Compare October 28, 2026 07:32
GUIDINGLI
GUIDINGLI previously approved these changes Oct 30, 2026
@Brijesh-Thakkar

Copy link
Copy Markdown
Contributor

@yf13
Agreed, excluding 8-bit AVR when defining CONFIG_HAVE_EXPRESSION_STATEMENT makes sense: there
div64_const doesn't fold, so it only adds code around the same libgcc __udivdi3 call. I'll build
breadxavr:nsh with this PR as-is vs. with AVR excluded and post both sizes here.

@jerpelea jerpelea changed the title enable div64_const on 32bit targets libs/libc: enable div64_const on 32bit targets Oct 30, 2026
jerpelea
jerpelea previously approved these changes Oct 30, 2026
@Brijesh-Thakkar

Copy link
Copy Markdown
Contributor

@yf13 correction to my earlier comment: I measured it, and excluding AVR would be a regression,
so please don't add that exclusion based on my note.

Head 5c3a8e0 (commits 07c63f2, 0e1276e, 5c3a8e0 cherry-picked onto master 3122150),
avr-gcc 14.3.0, DEBUG_FULLOPT:

config master this PR as-is AVR excluded
breadxavr:nsh 58485 58367 (-118) 58485 (same as master)
arduino-mega2560:nsh 71462 71334 (-128) 71468 (same code as master)

Even though __ultoa_invert still calls __udivdi3 on AVR (the div64_const path doesn't fully
fold there), the PR still saves 118-128 B overall because the signed 64-bit division helpers
(__divdi3/__moddi3) are no longer linked. That matches the MemBrowse -74 B on arduino-mega2560.

so i guess we should avr enabled

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR conflicts with master in boards/arm/stm32f3/stm32f334-disco/configs/buckboost/defconfig (aed8c30 removed CONFIG_ARCH_STACKDUMP there). On current master, buckboost links with the other commits of this PR and without CONFIG_LTO_FULL: 64096 of 65536 bytes with GCC 13.2. That commit also adds CONFIG_BASE_DEFCONFIG, which no defconfig on master has.

Fix: drop "stm32f334-disco/buckboost: turn on LTO_FULL", rebase, and correct the subject "compler.h" to "include/nuttx/compiler.h".

Note for Marco: a COMMENT review does not clear our CHANGES_REQUESTED. To unblock it: approve after the rebase
(the rebase dismisses all approvals anyway), or dismiss our 10-28 review now. Resolve our fixed threads
(clock.h, lib_ultoa_invert.c:77, compiler.h:663) when posting.

yf13 added 3 commits November 1, 2026 18:30
This fixes Kconfig error when using DISABLE_ALL_SIGNALS together w/
DEFAULT_SMALL:

```
warning: DISABLE_MQUEUE_NOTIFICATION (defined
  at sched/Kconfig:1827) has direct dependencies !DISABLE_MQUEUE ||
  !DISABLE_MQUEUE_SYSV with value n, but is currently being y-selected by the
  following symbols:

  - DISABLE_ALL_SIGNALS (defined at sched/Kconfig:1642)
```

Signed-off-by: Yanfeng Liu <[email protected]>
This fixes nxtyle issue in lib_ultoa_invert.c

Signed-off-by: Yanfeng Liu <[email protected]>
This adds MSEC2TICK32 marco to fix build errors like:
```
error: braced-group within expression allowed only inside a function
92 |     .ddelay     = MSEC2TICK(CONFIG_FRDMK28F_SDHC_AUTOMOUNT_DDELAY),
```
when MSEC2TICK uses expression statement which is only for use in
functions.

Signed-off-by: Yanfeng Liu <[email protected]>
@arnavsharma990

arnavsharma990 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Hi @yf13 — I reviewed #20996 after receiving feedback on #21214. Our changes overlap significantly in math32.h/compiler.h, but #21214 takes a different approach to two issues raised during the review:

  1. File-scope initializers — Statement expressions cannot be used in file-scope constant initializers, affecting board automount configurations using MSEC2TICK(). Fix/math32 constant division guard #21214 addresses this by converting div64_const into a static inline_function, avoiding the need for MSEC2TICK32 or board-specific edits.

  2. Type promotion — The conditional operator (?:) can promote 32-bit dividend results to uint64_t, breaking existing format-string expectations. Fix/math32 constant division guard #21214 uses __builtin_choose_expr to preserve the selected branch's type.

Would you be open to integrating these approaches into #20996 while retaining your DEBUG_FULLOPT and architecture gating? This would give us one consistent policy for enabling the optimization, while potentially retaining #21214's CONFIG_LIBC_DISABLE_DIV64_OPTIMIZATION opt-out.

I'm happy to port and test the changes, including the size-sensitive configurations flagged by @casaroli.
I'll close #21214 to avoid parallel changes to the same macros.

yf13 added 4 commits November 1, 2026 18:31
This enables LTO_FULL to reduce flash usage

Signed-off-by: Yanfeng Liu <[email protected]>
This defines CONFIG_HAVE_EXPRESSION_STATEMENT for 32bit device
using __GNUC__ and DEBUG_FULLOPT to enable div64_const.

This is disabled for some archs for now.

Signed-off-by: Yanfeng Liu <[email protected]>
This adds type casting for result of div_const() macro.

Signed-off-by: Yanfeng Liu <[email protected]>
This enables use of div_const for non-64bit devices to avoid
pulling in 64bit divide libraries in toolchain.

Checked on Ubuntu 22 with local small config rv-virt:hello:

```sh
$ size -G nuttx ../orig/nuttx
      text       data        bss      total filename
     23364        964       3760      28088 nuttx
     24492        868       3760      29120 ../orig/nuttx
$ qemu-system-riscv32 -M virt,aclint=on -semihosting -nographic -bios nuttx
Hello, World!!
0 11 12 20 1777777777777777777777
0 9 10 16 18446744073709551615
0 9 a 10 ffffffffffffffff
```
And local small stm32f103-minimum:hello (compile only):

```sh
$ size -G nuttx ../orig/nuttx
      text       data        bss      total filename
     18061        248       1680      19989 nuttx
     18713        256       1680      20649 ../bbb/nuttx
```

Signed-off-by: Yanfeng Liu <[email protected]>
@yf13

yf13 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

@arnavsharma990, per my test with rv32, using macro saves more than inline function, that is why I stopped using inline. The quoted Linux comment above also says that inline func isnt doing better than macro. Maybe you can double check if it is the same for your target.

@yf13
yf13 dismissed stale reviews from jerpelea and GUIDINGLI via c2bd2c7 November 1, 2026 10:49
@yf13
yf13 force-pushed the pull-20414-div64 branch from 5c3a8e0 to c2bd2c7 Compare November 1, 2026 10:49
@arnavsharma990

Copy link
Copy Markdown
Contributor

Thanks for explaining @yf13 ,
I'll benchmark both approaches on RV32 and STM32F103 under identical configurations and compiler flags, then compare the binary sizes.

Comment thread include/nuttx/clock.h
#define NSEC2TICK(nsec) div_const_roundup(nsec, NSEC_PER_TICK)
#define USEC2TICK(usec) div_const_roundup(usec, USEC_PER_TICK)

#define div_roundup32(n, b) (((n) + (b) - 1) / (b))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is here because it is only for MSEC2TICK32 below to use.
Let me know if there is better place for it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look where we put div_roundup

Comment thread include/nuttx/compiler.h
#if defined(CONFIG_DEBUG_FULLOPT) && !defined(CONFIG_ARCH_64BIT) && \
!defined(CONFIG_ARCH_RENESAS) && !defined(CONFIG_ARCH_XTENSA) && \
!defined(CONFIG_ARCH_SPARC)
# define CONFIG_HAVE_EXPRESSION_STATEMENT 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expression statement is compiler property why check CONFIG_DEBUG_FULLOPT/CONFIG_ARCH_xxx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The div64_const optimization doesnt work when -O0 is used. Also it increases sizes for 64bit. The disabled archs showed size growth before in membrowse so they are disabled for now.

{
int v;

#if defined(CONFIG_HAVE_EXPRESSION_STATEMENT)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why check

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rv-virt:nsh grows by 802 bytes of text with this PR (229998 to 230800, gcc 14.3, DEBUG_FULLOPT). div_const now expands inline in oneshot_callback (+192), timer_settime (+182), nxsig_clockwait (+112), up_alarm_set_lowerhalf (+98) and sysinfo (+92), and libgcc does not shrink on this target. On Cortex-M3 the same PR saves 912 bytes.

Fix: state the growth in the PR text, or apply the macro change only where it removes the libgcc division.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@Fix-Point will work on these patches in the upcoming day.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

let continue on #21491

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

Labels

Area: OS Components OS Components issues Board: arm Board: risc-v Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants