diff options
| author | 2019-06-05 15:52:37 -0400 | |
|---|---|---|
| committer | 2019-06-05 15:52:37 -0400 | |
| commit | 81e09bb1213720c31b7881c9396385375dac5749 (patch) | |
| tree | 06ea501a4f516ac82f1ac91f953d4325475940bc /src/core | |
| parent | Merge pull request #2541 from lioncash/input (diff) | |
| parent | core/core_timing_util: Amend casing of cyclesTo* functions (diff) | |
| download | yuzu-81e09bb1213720c31b7881c9396385375dac5749.tar.gz yuzu-81e09bb1213720c31b7881c9396385375dac5749.tar.xz yuzu-81e09bb1213720c31b7881c9396385375dac5749.zip | |
Merge pull request #2545 from lioncash/timing
core/core_timing_util: Use std::chrono types for specifying time units
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/core_timing_util.cpp | 42 | ||||
| -rw-r--r-- | src/core/core_timing_util.h | 52 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.cpp | 4 | ||||
| -rw-r--r-- | src/core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.cpp | 3 | ||||
| -rw-r--r-- | src/core/hle/service/time/time.cpp | 9 |
5 files changed, 34 insertions, 76 deletions
diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index c0f08cddb..a10472a95 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp | |||
| @@ -13,52 +13,40 @@ namespace Core::Timing { | |||
| 13 | 13 | ||
| 14 | constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE; | 14 | constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE; |
| 15 | 15 | ||
| 16 | s64 usToCycles(s64 us) { | 16 | s64 msToCycles(std::chrono::milliseconds ms) { |
| 17 | if (static_cast<u64>(us / 1000000) > MAX_VALUE_TO_MULTIPLY) { | 17 | if (static_cast<u64>(ms.count() / 1000) > MAX_VALUE_TO_MULTIPLY) { |
| 18 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | 18 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); |
| 19 | return std::numeric_limits<s64>::max(); | 19 | return std::numeric_limits<s64>::max(); |
| 20 | } | 20 | } |
| 21 | if (static_cast<u64>(us) > MAX_VALUE_TO_MULTIPLY) { | 21 | if (static_cast<u64>(ms.count()) > MAX_VALUE_TO_MULTIPLY) { |
| 22 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | 22 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); |
| 23 | return BASE_CLOCK_RATE * (us / 1000000); | 23 | return BASE_CLOCK_RATE * (ms.count() / 1000); |
| 24 | } | 24 | } |
| 25 | return (BASE_CLOCK_RATE * us) / 1000000; | 25 | return (BASE_CLOCK_RATE * ms.count()) / 1000; |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | s64 usToCycles(u64 us) { | 28 | s64 usToCycles(std::chrono::microseconds us) { |
| 29 | if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) { | 29 | if (static_cast<u64>(us.count() / 1000000) > MAX_VALUE_TO_MULTIPLY) { |
| 30 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | 30 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); |
| 31 | return std::numeric_limits<s64>::max(); | 31 | return std::numeric_limits<s64>::max(); |
| 32 | } | 32 | } |
| 33 | if (us > MAX_VALUE_TO_MULTIPLY) { | 33 | if (static_cast<u64>(us.count()) > MAX_VALUE_TO_MULTIPLY) { |
| 34 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | 34 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); |
| 35 | return BASE_CLOCK_RATE * static_cast<s64>(us / 1000000); | 35 | return BASE_CLOCK_RATE * (us.count() / 1000000); |
| 36 | } | 36 | } |
| 37 | return (BASE_CLOCK_RATE * static_cast<s64>(us)) / 1000000; | 37 | return (BASE_CLOCK_RATE * us.count()) / 1000000; |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | s64 nsToCycles(s64 ns) { | 40 | s64 nsToCycles(std::chrono::nanoseconds ns) { |
| 41 | if (static_cast<u64>(ns / 1000000000) > MAX_VALUE_TO_MULTIPLY) { | 41 | if (static_cast<u64>(ns.count() / 1000000000) > MAX_VALUE_TO_MULTIPLY) { |
| 42 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | 42 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); |
| 43 | return std::numeric_limits<s64>::max(); | 43 | return std::numeric_limits<s64>::max(); |
| 44 | } | 44 | } |
| 45 | if (static_cast<u64>(ns) > MAX_VALUE_TO_MULTIPLY) { | 45 | if (static_cast<u64>(ns.count()) > MAX_VALUE_TO_MULTIPLY) { |
| 46 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | 46 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); |
| 47 | return BASE_CLOCK_RATE * (ns / 1000000000); | 47 | return BASE_CLOCK_RATE * (ns.count() / 1000000000); |
| 48 | } | 48 | } |
| 49 | return (BASE_CLOCK_RATE * ns) / 1000000000; | 49 | return (BASE_CLOCK_RATE * ns.count()) / 1000000000; |
| 50 | } | ||
| 51 | |||
| 52 | s64 nsToCycles(u64 ns) { | ||
| 53 | if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) { | ||
| 54 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | ||
| 55 | return std::numeric_limits<s64>::max(); | ||
| 56 | } | ||
| 57 | if (ns > MAX_VALUE_TO_MULTIPLY) { | ||
| 58 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | ||
| 59 | return BASE_CLOCK_RATE * (static_cast<s64>(ns) / 1000000000); | ||
| 60 | } | ||
| 61 | return (BASE_CLOCK_RATE * static_cast<s64>(ns)) / 1000000000; | ||
| 62 | } | 50 | } |
| 63 | 51 | ||
| 64 | u64 CpuCyclesToClockCycles(u64 ticks) { | 52 | u64 CpuCyclesToClockCycles(u64 ticks) { |
diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index 679aa3123..cdd84d70f 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <chrono> | ||
| 7 | #include "common/common_types.h" | 8 | #include "common/common_types.h" |
| 8 | 9 | ||
| 9 | namespace Core::Timing { | 10 | namespace Core::Timing { |
| @@ -13,53 +14,20 @@ namespace Core::Timing { | |||
| 13 | constexpr u64 BASE_CLOCK_RATE = 1019215872; // Switch clock speed is 1020MHz un/docked | 14 | constexpr u64 BASE_CLOCK_RATE = 1019215872; // Switch clock speed is 1020MHz un/docked |
| 14 | constexpr u64 CNTFREQ = 19200000; // Value from fusee. | 15 | constexpr u64 CNTFREQ = 19200000; // Value from fusee. |
| 15 | 16 | ||
| 16 | inline s64 msToCycles(int ms) { | 17 | s64 msToCycles(std::chrono::milliseconds ms); |
| 17 | // since ms is int there is no way to overflow | 18 | s64 usToCycles(std::chrono::microseconds us); |
| 18 | return BASE_CLOCK_RATE * static_cast<s64>(ms) / 1000; | 19 | s64 nsToCycles(std::chrono::nanoseconds ns); |
| 19 | } | ||
| 20 | |||
| 21 | inline s64 msToCycles(float ms) { | ||
| 22 | return static_cast<s64>(BASE_CLOCK_RATE * (0.001f) * ms); | ||
| 23 | } | ||
| 24 | |||
| 25 | inline s64 msToCycles(double ms) { | ||
| 26 | return static_cast<s64>(BASE_CLOCK_RATE * (0.001) * ms); | ||
| 27 | } | ||
| 28 | |||
| 29 | inline s64 usToCycles(float us) { | ||
| 30 | return static_cast<s64>(BASE_CLOCK_RATE * (0.000001f) * us); | ||
| 31 | } | ||
| 32 | |||
| 33 | inline s64 usToCycles(int us) { | ||
| 34 | return (BASE_CLOCK_RATE * static_cast<s64>(us) / 1000000); | ||
| 35 | } | ||
| 36 | |||
| 37 | s64 usToCycles(s64 us); | ||
| 38 | |||
| 39 | s64 usToCycles(u64 us); | ||
| 40 | |||
| 41 | inline s64 nsToCycles(float ns) { | ||
| 42 | return static_cast<s64>(BASE_CLOCK_RATE * (0.000000001f) * ns); | ||
| 43 | } | ||
| 44 | |||
| 45 | inline s64 nsToCycles(int ns) { | ||
| 46 | return BASE_CLOCK_RATE * static_cast<s64>(ns) / 1000000000; | ||
| 47 | } | ||
| 48 | |||
| 49 | s64 nsToCycles(s64 ns); | ||
| 50 | |||
| 51 | s64 nsToCycles(u64 ns); | ||
| 52 | 20 | ||
| 53 | inline u64 cyclesToNs(s64 cycles) { | 21 | inline std::chrono::milliseconds CyclesToMs(s64 cycles) { |
| 54 | return cycles * 1000000000 / BASE_CLOCK_RATE; | 22 | return std::chrono::milliseconds(cycles * 1000 / BASE_CLOCK_RATE); |
| 55 | } | 23 | } |
| 56 | 24 | ||
| 57 | inline s64 cyclesToUs(s64 cycles) { | 25 | inline std::chrono::nanoseconds CyclesToNs(s64 cycles) { |
| 58 | return cycles * 1000000 / BASE_CLOCK_RATE; | 26 | return std::chrono::nanoseconds(cycles * 1000000000 / BASE_CLOCK_RATE); |
| 59 | } | 27 | } |
| 60 | 28 | ||
| 61 | inline u64 cyclesToMs(s64 cycles) { | 29 | inline std::chrono::microseconds CyclesToUs(s64 cycles) { |
| 62 | return cycles * 1000 / BASE_CLOCK_RATE; | 30 | return std::chrono::microseconds(cycles * 1000000 / BASE_CLOCK_RATE); |
| 63 | } | 31 | } |
| 64 | 32 | ||
| 65 | u64 CpuCyclesToClockCycles(u64 ticks); | 33 | u64 CpuCyclesToClockCycles(u64 ticks); |
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 2abf9efca..c73a40977 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp | |||
| @@ -75,9 +75,9 @@ void Thread::WakeAfterDelay(s64 nanoseconds) { | |||
| 75 | 75 | ||
| 76 | // This function might be called from any thread so we have to be cautious and use the | 76 | // This function might be called from any thread so we have to be cautious and use the |
| 77 | // thread-safe version of ScheduleEvent. | 77 | // thread-safe version of ScheduleEvent. |
| 78 | const s64 cycles = Core::Timing::nsToCycles(std::chrono::nanoseconds{nanoseconds}); | ||
| 78 | Core::System::GetInstance().CoreTiming().ScheduleEventThreadsafe( | 79 | Core::System::GetInstance().CoreTiming().ScheduleEventThreadsafe( |
| 79 | Core::Timing::nsToCycles(nanoseconds), kernel.ThreadWakeupCallbackEventType(), | 80 | cycles, kernel.ThreadWakeupCallbackEventType(), callback_handle); |
| 80 | callback_handle); | ||
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | void Thread::CancelWakeupTimer() { | 83 | void Thread::CancelWakeupTimer() { |
diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.cpp index 45812d238..0e28755bd 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.cpp | |||
| @@ -185,7 +185,8 @@ u32 nvhost_ctrl_gpu::GetGpuTime(const std::vector<u8>& input, std::vector<u8>& o | |||
| 185 | 185 | ||
| 186 | IoctlGetGpuTime params{}; | 186 | IoctlGetGpuTime params{}; |
| 187 | std::memcpy(¶ms, input.data(), input.size()); | 187 | std::memcpy(¶ms, input.data(), input.size()); |
| 188 | params.gpu_time = Core::Timing::cyclesToNs(Core::System::GetInstance().CoreTiming().GetTicks()); | 188 | const auto ns = Core::Timing::CyclesToNs(Core::System::GetInstance().CoreTiming().GetTicks()); |
| 189 | params.gpu_time = static_cast<u64_le>(ns.count()); | ||
| 189 | std::memcpy(output.data(), ¶ms, output.size()); | 190 | std::memcpy(output.data(), ¶ms, output.size()); |
| 190 | return 0; | 191 | return 0; |
| 191 | } | 192 | } |
diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp index aa115935d..346bad80d 100644 --- a/src/core/hle/service/time/time.cpp +++ b/src/core/hle/service/time/time.cpp | |||
| @@ -108,8 +108,9 @@ private: | |||
| 108 | LOG_DEBUG(Service_Time, "called"); | 108 | LOG_DEBUG(Service_Time, "called"); |
| 109 | 109 | ||
| 110 | const auto& core_timing = Core::System::GetInstance().CoreTiming(); | 110 | const auto& core_timing = Core::System::GetInstance().CoreTiming(); |
| 111 | const SteadyClockTimePoint steady_clock_time_point{ | 111 | const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks()); |
| 112 | Core::Timing::cyclesToMs(core_timing.GetTicks()) / 1000}; | 112 | const SteadyClockTimePoint steady_clock_time_point{static_cast<u64_le>(ms.count() / 1000), |
| 113 | {}}; | ||
| 113 | IPC::ResponseBuilder rb{ctx, (sizeof(SteadyClockTimePoint) / 4) + 2}; | 114 | IPC::ResponseBuilder rb{ctx, (sizeof(SteadyClockTimePoint) / 4) + 2}; |
| 114 | rb.Push(RESULT_SUCCESS); | 115 | rb.Push(RESULT_SUCCESS); |
| 115 | rb.PushRaw(steady_clock_time_point); | 116 | rb.PushRaw(steady_clock_time_point); |
| @@ -284,8 +285,8 @@ void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) { | |||
| 284 | } | 285 | } |
| 285 | 286 | ||
| 286 | const auto& core_timing = Core::System::GetInstance().CoreTiming(); | 287 | const auto& core_timing = Core::System::GetInstance().CoreTiming(); |
| 287 | const SteadyClockTimePoint steady_clock_time_point{ | 288 | const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks()); |
| 288 | Core::Timing::cyclesToMs(core_timing.GetTicks()) / 1000, {}}; | 289 | const SteadyClockTimePoint steady_clock_time_point{static_cast<u64_le>(ms.count() / 1000), {}}; |
| 289 | 290 | ||
| 290 | CalendarTime calendar_time{}; | 291 | CalendarTime calendar_time{}; |
| 291 | calendar_time.year = tm->tm_year + 1900; | 292 | calendar_time.year = tm->tm_year + 1900; |