Repository navigation
sched/clock: use 32x32->64 multiply in clock_basetime - #21212
Closed
Brijesh-Thakkar wants to merge 1 commit into
Closed
Brijesh-Thakkar wants to merge 1 commit into
Brijesh-Thakkar wants to merge 1 commit into
Conversation
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]>
Brijesh-Thakkar
requested review from
michallenc and
xiaoxiang781216
as code owners
October 30, 2026 15:06
|
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
avr-gcc 5.4.0 doesn't recognize the 32x32->64 widening multiply, so the cast only adds register So on the toolchain NuttX CI uses, this change doesn't pay off. Closing; the measurements are |
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
clock_basetime()computes the initial time astv_sec = jdn * SEC_PER_DAY, wherejdnispromoted to 64-bit. On targets without a native 64-bit multiply this pulls in libgcc's
__muldi3.jdnis the day count fromclock_calendar2utc()forCONFIG_START_YEAR(>= 1970), so it isnon-negative and well below 2^32 (at most ~49,710 for the allowed year range). A 32x32->64
multiply is therefore sufficient:
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
jdn(verified, see Testing).time_tstaysint64_t.sizetotal in bytes):__muldi3replaced by smaller__umulsidi3g_versionbuild-path string in .data__aeabi_lmulis still usedTesting
(uint64_t)(uint32_t)jdn * 86400against the original(int64_t)jdn * 86400over the full legal range (strided, with edge values):1,178,898 checks, 0 mismatches.
sim:nsh: builds,ostestexits with status 0,/proc/uptimeanddatebehave normally.Note: sim uses
CONFIG_RTC=y, so it does not executeclock_basetime(); this is ano-regression check only, and the arithmetic is covered by the host test above.
tools/checkpatch.sh -c -u -m -g HEAD~1..HEADclean.