From 79189c7e3edfd2d628a927ea4be707eb0deeb4e3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 4 Jun 2019 19:41:48 -0400 Subject: [PATCH 1/3] core/core_timing_utils: Simplify overload set Removes unused overloads, simplifying the overall interface, deduplicating some code. --- src/core/core_timing_util.cpp | 20 ++------------------ src/core/core_timing_util.h | 31 ------------------------------- 2 files changed, 2 insertions(+), 49 deletions(-) diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index c0f08cddb5..4d73a0d898 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp @@ -26,15 +26,7 @@ s64 usToCycles(s64 us) { } s64 usToCycles(u64 us) { - if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) { - LOG_ERROR(Core_Timing, "Integer overflow, use max value"); - return std::numeric_limits::max(); - } - if (us > MAX_VALUE_TO_MULTIPLY) { - LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return BASE_CLOCK_RATE * static_cast(us / 1000000); - } - return (BASE_CLOCK_RATE * static_cast(us)) / 1000000; + return usToCycles(static_cast(us)); } s64 nsToCycles(s64 ns) { @@ -50,15 +42,7 @@ s64 nsToCycles(s64 ns) { } s64 nsToCycles(u64 ns) { - if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) { - LOG_ERROR(Core_Timing, "Integer overflow, use max value"); - return std::numeric_limits::max(); - } - if (ns > MAX_VALUE_TO_MULTIPLY) { - LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return BASE_CLOCK_RATE * (static_cast(ns) / 1000000000); - } - return (BASE_CLOCK_RATE * static_cast(ns)) / 1000000000; + return nsToCycles(static_cast(ns)); } u64 CpuCyclesToClockCycles(u64 ticks) { diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index 679aa31231..c8749ff12a 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h @@ -13,41 +13,10 @@ namespace Core::Timing { constexpr u64 BASE_CLOCK_RATE = 1019215872; // Switch clock speed is 1020MHz un/docked constexpr u64 CNTFREQ = 19200000; // Value from fusee. -inline s64 msToCycles(int ms) { - // since ms is int there is no way to overflow - return BASE_CLOCK_RATE * static_cast(ms) / 1000; -} - -inline s64 msToCycles(float ms) { - return static_cast(BASE_CLOCK_RATE * (0.001f) * ms); -} - -inline s64 msToCycles(double ms) { - return static_cast(BASE_CLOCK_RATE * (0.001) * ms); -} - -inline s64 usToCycles(float us) { - return static_cast(BASE_CLOCK_RATE * (0.000001f) * us); -} - -inline s64 usToCycles(int us) { - return (BASE_CLOCK_RATE * static_cast(us) / 1000000); -} - s64 usToCycles(s64 us); - s64 usToCycles(u64 us); -inline s64 nsToCycles(float ns) { - return static_cast(BASE_CLOCK_RATE * (0.000000001f) * ns); -} - -inline s64 nsToCycles(int ns) { - return BASE_CLOCK_RATE * static_cast(ns) / 1000000000; -} - s64 nsToCycles(s64 ns); - s64 nsToCycles(u64 ns); inline u64 cyclesToNs(s64 cycles) { From 42f5fd0ab32b117901d0cae228103811719606ff Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 4 Jun 2019 19:52:42 -0400 Subject: [PATCH 2/3] 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/audio_core/stream.cpp | 4 ++- src/core/core_timing_util.cpp | 36 ++++++++++--------- src/core/core_timing_util.h | 21 ++++++----- src/core/hle/kernel/thread.cpp | 4 +-- .../service/nvdrv/devices/nvhost_ctrl_gpu.cpp | 3 +- src/core/hle/service/time/time.cpp | 9 ++--- src/video_core/gpu_thread.cpp | 2 +- 7 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp index 22a3f8c84d..11481a776f 100644 --- a/src/audio_core/stream.cpp +++ b/src/audio_core/stream.cpp @@ -57,7 +57,9 @@ Stream::State Stream::GetState() const { s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const { const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()}; - return Core::Timing::usToCycles((static_cast(num_samples) * 1000000) / sample_rate); + const auto us = + std::chrono::microseconds((static_cast(num_samples) * 1000000) / sample_rate); + return Core::Timing::usToCycles(us); } static void VolumeAdjustSamples(std::vector& samples) { diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index 4d73a0d898..a10472a95f 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) { diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index c8749ff12a..1dcd38dd16 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h @@ -4,6 +4,7 @@ #pragma once +#include #include "common/common_types.h" namespace Core::Timing { @@ -13,22 +14,20 @@ namespace Core::Timing { constexpr u64 BASE_CLOCK_RATE = 1019215872; // Switch clock speed is 1020MHz un/docked constexpr u64 CNTFREQ = 19200000; // Value from fusee. -s64 usToCycles(s64 us); -s64 usToCycles(u64 us); +s64 msToCycles(std::chrono::milliseconds ms); +s64 usToCycles(std::chrono::microseconds us); +s64 nsToCycles(std::chrono::nanoseconds ns); -s64 nsToCycles(s64 ns); -s64 nsToCycles(u64 ns); - -inline u64 cyclesToNs(s64 cycles) { - return cycles * 1000000000 / BASE_CLOCK_RATE; +inline std::chrono::milliseconds cyclesToMs(s64 cycles) { + return std::chrono::milliseconds(cycles * 1000 / BASE_CLOCK_RATE); } -inline s64 cyclesToUs(s64 cycles) { - return cycles * 1000000 / BASE_CLOCK_RATE; +inline std::chrono::nanoseconds cyclesToNs(s64 cycles) { + return std::chrono::nanoseconds(cycles * 1000000000 / BASE_CLOCK_RATE); } -inline u64 cyclesToMs(s64 cycles) { - return cycles * 1000 / BASE_CLOCK_RATE; +inline std::chrono::microseconds cyclesToUs(s64 cycles) { + return std::chrono::microseconds(cycles * 1000000 / BASE_CLOCK_RATE); } u64 CpuCyclesToClockCycles(u64 ticks); diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 2abf9efca4..c73a409773 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -75,9 +75,9 @@ void Thread::WakeAfterDelay(s64 nanoseconds) { // This function might be called from any thread so we have to be cautious and use the // thread-safe version of ScheduleEvent. + const s64 cycles = Core::Timing::nsToCycles(std::chrono::nanoseconds{nanoseconds}); Core::System::GetInstance().CoreTiming().ScheduleEventThreadsafe( - Core::Timing::nsToCycles(nanoseconds), kernel.ThreadWakeupCallbackEventType(), - callback_handle); + cycles, kernel.ThreadWakeupCallbackEventType(), callback_handle); } 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 45812d2389..f425305d67 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& input, std::vector& o IoctlGetGpuTime params{}; std::memcpy(¶ms, input.data(), input.size()); - params.gpu_time = Core::Timing::cyclesToNs(Core::System::GetInstance().CoreTiming().GetTicks()); + const auto ns = Core::Timing::cyclesToNs(Core::System::GetInstance().CoreTiming().GetTicks()); + params.gpu_time = static_cast(ns.count()); std::memcpy(output.data(), ¶ms, output.size()); return 0; } diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp index aa115935d8..a6bb3beb03 100644 --- a/src/core/hle/service/time/time.cpp +++ b/src/core/hle/service/time/time.cpp @@ -108,8 +108,9 @@ private: LOG_DEBUG(Service_Time, "called"); const auto& core_timing = Core::System::GetInstance().CoreTiming(); - const SteadyClockTimePoint steady_clock_time_point{ - Core::Timing::cyclesToMs(core_timing.GetTicks()) / 1000}; + const auto ms = Core::Timing::cyclesToMs(core_timing.GetTicks()); + const SteadyClockTimePoint steady_clock_time_point{static_cast(ms.count() / 1000), + {}}; IPC::ResponseBuilder rb{ctx, (sizeof(SteadyClockTimePoint) / 4) + 2}; rb.Push(RESULT_SUCCESS); rb.PushRaw(steady_clock_time_point); @@ -284,8 +285,8 @@ void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) { } const auto& core_timing = Core::System::GetInstance().CoreTiming(); - const SteadyClockTimePoint steady_clock_time_point{ - Core::Timing::cyclesToMs(core_timing.GetTicks()) / 1000, {}}; + const auto ms = Core::Timing::cyclesToMs(core_timing.GetTicks()); + const SteadyClockTimePoint steady_clock_time_point{static_cast(ms.count() / 1000), {}}; CalendarTime calendar_time{}; calendar_time.year = tm->tm_year + 1900; diff --git a/src/video_core/gpu_thread.cpp b/src/video_core/gpu_thread.cpp index 1e2ff46b0e..3f0939ec9e 100644 --- a/src/video_core/gpu_thread.cpp +++ b/src/video_core/gpu_thread.cpp @@ -75,7 +75,7 @@ void ThreadManager::StartThread(VideoCore::RendererBase& renderer, Tegra::DmaPus void ThreadManager::SubmitList(Tegra::CommandList&& entries) { const u64 fence{PushCommand(SubmitListCommand(std::move(entries)))}; - const s64 synchronization_ticks{Core::Timing::usToCycles(9000)}; + const s64 synchronization_ticks{Core::Timing::usToCycles(std::chrono::microseconds{9000})}; system.CoreTiming().ScheduleEvent(synchronization_ticks, synchronization_event, fence); } From 2548661c08e006c6a56f976a4ef1c05653b3c6db Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 4 Jun 2019 20:11:04 -0400 Subject: [PATCH 3/3] core/core_timing_util: Amend casing of cyclesTo* functions Makes the casing consistent with all of our general function naming conventions. --- src/core/core_timing_util.h | 6 +++--- src/core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.cpp | 2 +- src/core/hle/service/time/time.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index 1dcd38dd16..cdd84d70ff 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h @@ -18,15 +18,15 @@ s64 msToCycles(std::chrono::milliseconds ms); s64 usToCycles(std::chrono::microseconds us); s64 nsToCycles(std::chrono::nanoseconds ns); -inline std::chrono::milliseconds cyclesToMs(s64 cycles) { +inline std::chrono::milliseconds CyclesToMs(s64 cycles) { return std::chrono::milliseconds(cycles * 1000 / BASE_CLOCK_RATE); } -inline std::chrono::nanoseconds cyclesToNs(s64 cycles) { +inline std::chrono::nanoseconds CyclesToNs(s64 cycles) { return std::chrono::nanoseconds(cycles * 1000000000 / BASE_CLOCK_RATE); } -inline std::chrono::microseconds cyclesToUs(s64 cycles) { +inline std::chrono::microseconds CyclesToUs(s64 cycles) { return std::chrono::microseconds(cycles * 1000000 / BASE_CLOCK_RATE); } 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 f425305d67..0e28755bdd 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,7 @@ u32 nvhost_ctrl_gpu::GetGpuTime(const std::vector& input, std::vector& o IoctlGetGpuTime params{}; std::memcpy(¶ms, input.data(), input.size()); - const auto ns = Core::Timing::cyclesToNs(Core::System::GetInstance().CoreTiming().GetTicks()); + const auto ns = Core::Timing::CyclesToNs(Core::System::GetInstance().CoreTiming().GetTicks()); params.gpu_time = static_cast(ns.count()); std::memcpy(output.data(), ¶ms, output.size()); return 0; diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp index a6bb3beb03..346bad80dc 100644 --- a/src/core/hle/service/time/time.cpp +++ b/src/core/hle/service/time/time.cpp @@ -108,7 +108,7 @@ private: LOG_DEBUG(Service_Time, "called"); const auto& core_timing = Core::System::GetInstance().CoreTiming(); - const auto ms = Core::Timing::cyclesToMs(core_timing.GetTicks()); + const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks()); const SteadyClockTimePoint steady_clock_time_point{static_cast(ms.count() / 1000), {}}; IPC::ResponseBuilder rb{ctx, (sizeof(SteadyClockTimePoint) / 4) + 2}; @@ -285,7 +285,7 @@ void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) { } const auto& core_timing = Core::System::GetInstance().CoreTiming(); - const auto ms = Core::Timing::cyclesToMs(core_timing.GetTicks()); + const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks()); const SteadyClockTimePoint steady_clock_time_point{static_cast(ms.count() / 1000), {}}; CalendarTime calendar_time{};