From c3a671057e0410a47e9d42a7ac5a7caec441dc37 Mon Sep 17 00:00:00 2001 From: Kuan-Wei Chiu Date: Fri, 13 Sep 2024 00:38:39 +0800 Subject: [PATCH] Optimize semu_timer_clocksource() precision on macOS Improve the precision of semu_timer_clocksource() by deferring the division by 1e9 until the final step. Since t.numer and t.denom are typically much smaller compared to the clock frequency and 1e9, performing the division earlier in the calculation could result in a significant loss of precision. This change ensures that precision is preserved by delaying the division until the end of the computation. --- utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils.c b/utils.c index 3f81a81..29f9575 100644 --- a/utils.c +++ b/utils.c @@ -48,8 +48,8 @@ static uint64_t semu_timer_clocksource(uint64_t freq) static mach_timebase_info_data_t t; if (t.denom == 0) (void) mach_timebase_info(&t); - return mult_frac(mult_frac(mach_absolute_time(), freq, 1e9), t.numer, - t.denom); + return mult_frac(mult_frac(mach_absolute_time(), t.numer, t.denom), freq, + 1e9); #else return time(0) * freq; #endif