From 42f5fd0ab32b117901d0cae228103811719606ff Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 4 Jun 2019 19:52:42 -0400 Subject: core/core_timing_util: Use std::chrono types for specifying time units Makes the interface more type-safe and consistent in terms of return values. --- src/core/core_timing_util.cpp | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/core/core_timing_util.cpp') diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index 4d73a0d89..a10472a95 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp @@ -13,36 +13,40 @@ namespace Core::Timing { constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits::max() / BASE_CLOCK_RATE; -s64 usToCycles(s64 us) { - if (static_cast(us / 1000000) > MAX_VALUE_TO_MULTIPLY) { +s64 msToCycles(std::chrono::milliseconds ms) { + if (static_cast(ms.count() / 1000) > MAX_VALUE_TO_MULTIPLY) { LOG_ERROR(Core_Timing, "Integer overflow, use max value"); return std::numeric_limits::max(); } - if (static_cast(us) > MAX_VALUE_TO_MULTIPLY) { + if (static_cast(ms.count()) > MAX_VALUE_TO_MULTIPLY) { LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return BASE_CLOCK_RATE * (us / 1000000); + return BASE_CLOCK_RATE * (ms.count() / 1000); } - return (BASE_CLOCK_RATE * us) / 1000000; + return (BASE_CLOCK_RATE * ms.count()) / 1000; } -s64 usToCycles(u64 us) { - return usToCycles(static_cast(us)); -} - -s64 nsToCycles(s64 ns) { - if (static_cast(ns / 1000000000) > MAX_VALUE_TO_MULTIPLY) { +s64 usToCycles(std::chrono::microseconds us) { + if (static_cast(us.count() / 1000000) > MAX_VALUE_TO_MULTIPLY) { LOG_ERROR(Core_Timing, "Integer overflow, use max value"); return std::numeric_limits::max(); } - if (static_cast(ns) > MAX_VALUE_TO_MULTIPLY) { + if (static_cast(us.count()) > MAX_VALUE_TO_MULTIPLY) { LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return BASE_CLOCK_RATE * (ns / 1000000000); + return BASE_CLOCK_RATE * (us.count() / 1000000); } - return (BASE_CLOCK_RATE * ns) / 1000000000; + return (BASE_CLOCK_RATE * us.count()) / 1000000; } -s64 nsToCycles(u64 ns) { - return nsToCycles(static_cast(ns)); +s64 nsToCycles(std::chrono::nanoseconds ns) { + if (static_cast(ns.count() / 1000000000) > MAX_VALUE_TO_MULTIPLY) { + LOG_ERROR(Core_Timing, "Integer overflow, use max value"); + return std::numeric_limits::max(); + } + if (static_cast(ns.count()) > MAX_VALUE_TO_MULTIPLY) { + LOG_DEBUG(Core_Timing, "Time very big, do rounding"); + return BASE_CLOCK_RATE * (ns.count() / 1000000000); + } + return (BASE_CLOCK_RATE * ns.count()) / 1000000000; } u64 CpuCyclesToClockCycles(u64 ticks) { -- cgit v1.2.3