Skip to content

Fix clock_gettime() and tick_nanosecs() macro#263

Merged
fincs merged 1 commit intodevkitPro:masterfrom
mardy:clock_gettime-fix
Apr 26, 2026
Merged

Fix clock_gettime() and tick_nanosecs() macro#263
fincs merged 1 commit intodevkitPro:masterfrom
mardy:clock_gettime-fix

Conversation

@mardy
Copy link
Copy Markdown
Contributor

@mardy mardy commented Apr 25, 2026

clock_gettime() uses the tick_nanosecs() macro to extract the sub-second component of a time expressed in PPC ticks, and returns the sub-second component expressed in nanoseconds.

To do so, it relied on the PPCTicksToNs() function, which will overflow and return incorrect results if its argument is sufficiently large (in my case it was 50446082889576108). This in turn causes clock_gettime() to return the wrong number of nanoseconds, which confuses applications because they occasionally see the time jump back.

We fix it by using a simpler formula for tick_nanosecs(), which is guaranteed not to overflow on u64 variables.

@mardy mardy mentioned this pull request Apr 25, 2026
clock_gettime() uses the tick_nanosecs() macro to extract the sub-second
component of a time expressed in PPC ticks, and returns the sub-second
component expressed in nanoseconds.

To do so, it relied on the PPCTicksToNs() function, which will overflow
and return incorrect results if its argument is sufficiently large (in
my case it was 50446082889576108). This in turn causes clock_gettime()
to return the wrong number of nanoseconds, which confuses applications
because they occasionally see the time jump back.

We fix it by passint to tick_nanosecs() the number of ticks modulo the
number of tics per second, so it's guaranteed not to overflow.

We also adjust tick_microsecs() accordingly, even though it was not
wrong.
@mardy mardy force-pushed the clock_gettime-fix branch from 0f352c3 to 3391cc4 Compare April 26, 2026 12:25
@fincs fincs merged commit 96c2d63 into devkitPro:master Apr 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants