mcap/modules/Basic/timing_macos.jai

30 lines
932 B
Plaintext

// Any CPU, its own clock is right under Rosetta, where RDTSC and CPUID are emulated.
// mach_absolute_time counts at the rate mach_timebase_info gives: 125/3 ns per tick on
// Apple Silicon, the 24 MHz ARM generic timer, and 1 ns per tick on x64, Rosetta included.
TIMING_SUPPORTED :: true;
timing_read_ticks :: () -> u64 #expand {
return mach_absolute_time();
}
timing_read_tick_frequency_hz :: () -> u64 {
info: Mach_Timebase_Info;
if mach_timebase_info(*info) != 0 || info.numer == 0 return 0;
// numer/denom converts ticks to nanoseconds, so ticks per second is 1e9 * denom / numer.
return 1_000_000_000 * cast(u64) info.denom / info.numer;
}
#scope_file
Mach_Timebase_Info :: struct {
numer: u32;
denom: u32;
}
mach_absolute_time :: () -> u64 #foreign timing_libsystem;
mach_timebase_info :: (info: *Mach_Timebase_Info) -> s32 #foreign timing_libsystem;
timing_libsystem :: #library,system "libSystem";