#if COMPILER_MSVC #include #else #include #endif internal inline u64 ReadCPUTimer(void) { return (u64)__rdtsc(); } internal u64 base_os_monotonic_ns(void) { #if PLATFORM_WINDOWS LARGE_INTEGER counter, frequency; QueryPerformanceCounter(&counter); QueryPerformanceFrequency(&frequency); return (u64)((f64)counter.QuadPart * 1e9 / (f64)frequency.QuadPart); #else struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); return (u64)now.tv_sec * 1000000000ull + (u64)now.tv_nsec; #endif } internal u64 ReadCPUTimerFreq(void) { // NOTE: count ticks over ~10ms of wall clock, once local_persist u64 frequency = 0; if(!frequency) { u64 start_ns = base_os_monotonic_ns(); u64 start = ReadCPUTimer(); while(base_os_monotonic_ns() - start_ns < 10000000ull) {} u64 elapsed_ns = base_os_monotonic_ns() - start_ns; frequency = (u64)((f64)(ReadCPUTimer() - start) * 1e9 / (f64)elapsed_ns); } return frequency; }