Skip to content

sched/clock: use 32x32->64 multiply in clock_basetime - #21212

Closed
Brijesh-Thakkar wants to merge 1 commit into
apache:masterfrom
Brijesh-Thakkar:sched/clock-basetime-32bit-mul
Closed

Brijesh-Thakkar wants to merge 1 commit into
apache:masterfrom
Brijesh-Thakkar:sched/clock-basetime-32bit-mul

Conversation

@Brijesh-Thakkar

Copy link
Copy Markdown
Contributor

Summary

clock_basetime() computes the initial time as tv_sec = jdn * SEC_PER_DAY, where jdn is
promoted to 64-bit. On targets without a native 64-bit multiply this pulls in libgcc's
__muldi3.

jdn is the day count from clock_calendar2utc() for CONFIG_START_YEAR (>= 1970), so it is
non-negative and well below 2^32 (at most ~49,710 for the allowed year range). A 32x32->64
multiply is therefore sufficient:

tp->tv_sec = (uint64_t)(uint32_t)jdn * SEC_PER_DAY;

A comment in the code documents why the cast is safe.

This is a small follow-up from the size investigation in #18849: after #20996, this is the
only remaining 64-bit libgcc call site in the common clock code where a portable C change
reduces size on AVR. Replacing the remaining 64-bit divisions with portable C was measured to
be larger than libgcc's routines on AVR, so it is not proposed.

Refs #18849

Impact

  • No functional change; same result for every legal jdn (verified, see Testing).
  • No API/ABI change; time_t stays int64_t.
  • Size (master 3122150 vs this PR, size total in bytes):
config master this PR delta notes
breadxavr:nsh (AVR, avr-gcc 14.3.0) 58485 58403 -82 __muldi3 replaced by smaller __umulsidi3
stm32vldiscovery:nsh (Cortex-M3) 40680 40684 +4 text identical; +4 is g_version build-path string in .data
nucleo-f072rb:nsh (Cortex-M0, DEBUG_NOOPT) 86839 86851 +12 (+8 text) M0 has no 32x32->64 multiply instruction, so __aeabi_lmul is still used
nucleo-f072rb:nsh (Cortex-M0, DEBUG_FULLOPT) ... ... ... ...

Testing

  • Host test comparing (uint64_t)(uint32_t)jdn * 86400 against the original
    (int64_t)jdn * 86400 over the full legal range (strided, with edge values):
    1,178,898 checks, 0 mismatches.
  • sim:nsh: builds, ostest exits with status 0, /proc/uptime and date behave normally.
    Note: sim uses CONFIG_RTC=y, so it does not execute clock_basetime(); this is a
    no-regression check only, and the arithmetic is covered by the host test above.
  • Built breadxavr:nsh, stm32vldiscovery:nsh, nucleo-f072rb:nsh (arm-none-eabi-gcc 14.2.1).
  • tools/checkpatch.sh -c -u -m -g HEAD~1..HEAD clean.

clock_basetime() computes jdn * SEC_PER_DAY with a 64-bit jdn and a
constant multiplier. jdn is a day count since 1970 and CONFIG_START_YEAR
is in the range 1970-2106, so it is non-negative and always fits in 32
bits. Widen it explicitly so the compiler can use a 32x32->64 multiply
instead of a full 64x64 one.

On targets without a native 64-bit multiply this avoids pulling in
__muldi3 for this function: on AVR the call is replaced by the much
smaller __umulsidi3. Targets with a 32x32->64 multiply instruction
(Cortex-M3 and up) generate the same code as before.

Size (nsh, upstream/master 3122150, avr-gcc 14.3 / arm-none-eabi-gcc
14.2, text/data/bss/total):

  avrdx breadxavr:nsh          57731/70/684/58485 -> 57649/70/684/58403
                               (-82 bytes, __muldi3 -> __umulsidi3)
  stm32vldiscovery:nsh (M3)    text unchanged (38316)
  nucleo-f072rb:nsh (M0)       text +8 (85251 -> 85259); Cortex-M0 has no
                               32x32->64 multiply so __aeabi_lmul is still
                               used there. This config builds with
                               DEBUG_NOOPT.

Testing: built the configurations above; sim:nsh builds and passes ostest
(CONFIG_RTC=y there, so it does not use this code path). The arithmetic
was checked on a host against native 64-bit multiplication for all day
counts 0..131072 and the full uint32 range (strided, plus edges), with
no mismatches.

Refs apache#18849.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 15:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

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

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +8 B (+0.0%, 65,394 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text -8 B (-0.0%, 128,060 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text -8 B (-0.0%, 93,932 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text -8 B (-0.0%, 67,260 B / 131,072 B, total: 51% used)

qemu-intel64

  • Code: .text +16 B (+0.0%, 8,687,557 B)

rx65n-rsk2mb

  • ROM: .text -16 B (-0.0%, 90,040 B / 2,097,152 B, total: 4% used)

stm32-nucleo-f103rb

  • flash: .text -4 B (-0.0%, 34,160 B / 131,072 B, total: 26% used)

No memory changes detected for:


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

@Brijesh-Thakkar

Copy link
Copy Markdown
Contributor Author

Closing this after checking the MemBrowse +8 B on arduino-mega2560.

The CI image installs gcc-avr from Ubuntu 22.04 apt, which is avr-gcc 5.4.0. Reproduced locally
with the same toolchain and config (arduino-mega2560:nsh, FULLOPT, RTC off):

toolchain master this PR clock_basetime __muldi3
avr-gcc 5.4.0 (CI) - +8 B .text 114 -> 122 B still linked
avr-gcc 14.3.0 71462 71338 (-124) 114 -> 72 B dropped, smaller __umulsidi3 used

avr-gcc 5.4.0 doesn't recognize the 32x32->64 widening multiply, so the cast only adds register
zero-fill and still calls __muldi3. Cortex-M0 has no widening multiply instruction either
(+4 B with FULLOPT), and Cortex-M3 is unchanged.

So on the toolchain NuttX CI uses, this change doesn't pay off. Closing; the measurements are
summarized in #18849.

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 Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants