diff options
Diffstat (limited to 'src/common/steady_clock.cpp')
| -rw-r--r-- | src/common/steady_clock.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/common/steady_clock.cpp b/src/common/steady_clock.cpp new file mode 100644 index 000000000..0d5908aa7 --- /dev/null +++ b/src/common/steady_clock.cpp | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #if defined(_WIN32) | ||
| 5 | #include <windows.h> | ||
| 6 | #else | ||
| 7 | #include <time.h> | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #include "common/steady_clock.h" | ||
| 11 | |||
| 12 | namespace Common { | ||
| 13 | |||
| 14 | #ifdef _WIN32 | ||
| 15 | static s64 WindowsQueryPerformanceFrequency() { | ||
| 16 | LARGE_INTEGER frequency; | ||
| 17 | QueryPerformanceFrequency(&frequency); | ||
| 18 | return frequency.QuadPart; | ||
| 19 | } | ||
| 20 | |||
| 21 | static s64 WindowsQueryPerformanceCounter() { | ||
| 22 | LARGE_INTEGER counter; | ||
| 23 | QueryPerformanceCounter(&counter); | ||
| 24 | return counter.QuadPart; | ||
| 25 | } | ||
| 26 | #endif | ||
| 27 | |||
| 28 | SteadyClock::time_point SteadyClock::Now() noexcept { | ||
| 29 | #if defined(_WIN32) | ||
| 30 | static const auto freq = WindowsQueryPerformanceFrequency(); | ||
| 31 | const auto counter = WindowsQueryPerformanceCounter(); | ||
| 32 | |||
| 33 | // 10 MHz is a very common QPC frequency on modern PCs. | ||
| 34 | // Optimizing for this specific frequency can double the performance of | ||
| 35 | // this function by avoiding the expensive frequency conversion path. | ||
| 36 | static constexpr s64 TenMHz = 10'000'000; | ||
| 37 | |||
| 38 | if (freq == TenMHz) [[likely]] { | ||
| 39 | static_assert(period::den % TenMHz == 0); | ||
| 40 | static constexpr s64 Multiplier = period::den / TenMHz; | ||
| 41 | return time_point{duration{counter * Multiplier}}; | ||
| 42 | } | ||
| 43 | |||
| 44 | const auto whole = (counter / freq) * period::den; | ||
| 45 | const auto part = (counter % freq) * period::den / freq; | ||
| 46 | return time_point{duration{whole + part}}; | ||
| 47 | #elif defined(__APPLE__) | ||
| 48 | return time_point{duration{clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW)}}; | ||
| 49 | #else | ||
| 50 | timespec ts; | ||
| 51 | clock_gettime(CLOCK_MONOTONIC, &ts); | ||
| 52 | return time_point{std::chrono::seconds{ts.tv_sec} + std::chrono::nanoseconds{ts.tv_nsec}}; | ||
| 53 | #endif | ||
| 54 | } | ||
| 55 | |||
| 56 | }; // namespace Common | ||