Skip to content

Fix clock_gettime() and tick_nanosecs() macro#263

Open
mardy wants to merge 1 commit intodevkitPro:masterfrom
mardy:clock_gettime-fix
Open

Fix clock_gettime() and tick_nanosecs() macro#263
mardy wants to merge 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.

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
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.

1 participant