Skip to content

Commit 85ab051

Browse files
committed
Update get_time_info() to use mult_frac() for precise time calculations
Refactor the get_time_info() function to use the newly added mult_frac() function for calculations involving time conversions. This change improves precision and mitigates the risk of overflow in the following areas: - Converte the computation of nanoseconds from mach_absolute_time() using the timebase information. - Update the calculation of nanoseconds from clock() in low-resolution timer mode. These updates make the time calculations in get_time_info() more robust and reliable by leveraging the precise arithmetic capabilities of mult_frac(). This change enhances the overall accuracy of time-related computations in the code.
1 parent 100ddbd commit 85ab051

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/utils.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ static void get_time_info(int32_t *tv_sec, int32_t *tv_nsec)
5353
*/
5454
if (info.denom == 0)
5555
(void) mach_timebase_info(&info);
56-
/* Hope that the multiplication doesn't overflow. */
57-
uint64_t nsecs = mach_absolute_time() * info.numer / info.denom;
56+
uint64_t nsecs = mult_frac(mach_absolute_time(), info.numer, info.denom);
5857
*tv_sec = nsecs / 1e9;
5958
*tv_nsec = nsecs - (*tv_sec * 1e9);
6059
#else /* low resolution timer */
6160
clock_t t = clock();
6261
*tv_sec = t / CLOCKS_PER_SEC;
63-
*tv_nsec = (t % CLOCKS_PER_SEC) * (1e9 / CLOCKS_PER_SEC);
62+
*tv_nsec = mult_frac(t % CLOCKS_PER_SEC, 1e9, CLOCKS_PER_SEC);
6463
#endif
6564
}
6665

0 commit comments

Comments
 (0)