mirror of
https://git.suyu.dev/suyu/suyu.git
synced 2024-12-23 17:00:57 +01:00
time: Use compile time division for TimeSpanType conversion
This commit is contained in:
parent
8e56a84566
commit
9dcc7bde8b
5 changed files with 15 additions and 11 deletions
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <ratio>
|
||||||
|
|
||||||
#include "common/common_funcs.h"
|
#include "common/common_funcs.h"
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
#include "common/uuid.h"
|
#include "common/uuid.h"
|
||||||
|
@ -74,18 +76,19 @@ static_assert(std::is_trivially_copyable_v<ContinuousAdjustmentTimePoint>,
|
||||||
/// https://switchbrew.org/wiki/Glue_services#TimeSpanType
|
/// https://switchbrew.org/wiki/Glue_services#TimeSpanType
|
||||||
struct TimeSpanType {
|
struct TimeSpanType {
|
||||||
s64 nanoseconds{};
|
s64 nanoseconds{};
|
||||||
static constexpr s64 ns_per_second{1000000000ULL};
|
|
||||||
|
|
||||||
s64 ToSeconds() const {
|
s64 ToSeconds() const {
|
||||||
return nanoseconds / ns_per_second;
|
return nanoseconds / std::nano::den;
|
||||||
}
|
}
|
||||||
|
|
||||||
static TimeSpanType FromSeconds(s64 seconds) {
|
static TimeSpanType FromSeconds(s64 seconds) {
|
||||||
return {seconds * ns_per_second};
|
return {seconds * std::nano::den};
|
||||||
}
|
}
|
||||||
|
|
||||||
static TimeSpanType FromTicks(u64 ticks, u64 frequency) {
|
template <u64 Frequency>
|
||||||
return FromSeconds(static_cast<s64>(ticks) / static_cast<s64>(frequency));
|
static TimeSpanType FromTicks(u64 ticks) {
|
||||||
|
using TicksToNSRatio = std::ratio<std::nano::den, Frequency>;
|
||||||
|
return {static_cast<s64>(ticks * TicksToNSRatio::num / TicksToNSRatio::den)};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
static_assert(sizeof(TimeSpanType) == 8, "TimeSpanType is incorrect size");
|
static_assert(sizeof(TimeSpanType) == 8, "TimeSpanType is incorrect size");
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace Service::Time::Clock {
|
||||||
|
|
||||||
TimeSpanType StandardSteadyClockCore::GetCurrentRawTimePoint(Core::System& system) {
|
TimeSpanType StandardSteadyClockCore::GetCurrentRawTimePoint(Core::System& system) {
|
||||||
const TimeSpanType ticks_time_span{
|
const TimeSpanType ticks_time_span{
|
||||||
TimeSpanType::FromTicks(system.CoreTiming().GetClockTicks(), Core::Hardware::CNTFREQ)};
|
TimeSpanType::FromTicks<Core::Hardware::CNTFREQ>(system.CoreTiming().GetClockTicks())};
|
||||||
TimeSpanType raw_time_point{setup_value.nanoseconds + ticks_time_span.nanoseconds};
|
TimeSpanType raw_time_point{setup_value.nanoseconds + ticks_time_span.nanoseconds};
|
||||||
|
|
||||||
if (raw_time_point.nanoseconds < cached_raw_time_point.nanoseconds) {
|
if (raw_time_point.nanoseconds < cached_raw_time_point.nanoseconds) {
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace Service::Time::Clock {
|
||||||
|
|
||||||
SteadyClockTimePoint TickBasedSteadyClockCore::GetTimePoint(Core::System& system) {
|
SteadyClockTimePoint TickBasedSteadyClockCore::GetTimePoint(Core::System& system) {
|
||||||
const TimeSpanType ticks_time_span{
|
const TimeSpanType ticks_time_span{
|
||||||
TimeSpanType::FromTicks(system.CoreTiming().GetClockTicks(), Core::Hardware::CNTFREQ)};
|
TimeSpanType::FromTicks<Core::Hardware::CNTFREQ>(system.CoreTiming().GetClockTicks())};
|
||||||
|
|
||||||
return {ticks_time_span.ToSeconds(), GetClockSourceId()};
|
return {ticks_time_span.ToSeconds(), GetClockSourceId()};
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,8 +240,8 @@ void Module::Interface::CalculateMonotonicSystemClockBaseTimePoint(HLERequestCon
|
||||||
const auto current_time_point{steady_clock_core.GetCurrentTimePoint(system)};
|
const auto current_time_point{steady_clock_core.GetCurrentTimePoint(system)};
|
||||||
|
|
||||||
if (current_time_point.clock_source_id == context.steady_time_point.clock_source_id) {
|
if (current_time_point.clock_source_id == context.steady_time_point.clock_source_id) {
|
||||||
const auto ticks{Clock::TimeSpanType::FromTicks(system.CoreTiming().GetClockTicks(),
|
const auto ticks{Clock::TimeSpanType::FromTicks<Core::Hardware::CNTFREQ>(
|
||||||
Core::Hardware::CNTFREQ)};
|
system.CoreTiming().GetClockTicks())};
|
||||||
const s64 base_time_point{context.offset + current_time_point.time_point -
|
const s64 base_time_point{context.offset + current_time_point.time_point -
|
||||||
ticks.ToSeconds()};
|
ticks.ToSeconds()};
|
||||||
IPC::ResponseBuilder rb{ctx, (sizeof(s64) / 4) + 2};
|
IPC::ResponseBuilder rb{ctx, (sizeof(s64) / 4) + 2};
|
||||||
|
|
|
@ -21,8 +21,9 @@ SharedMemory::~SharedMemory() = default;
|
||||||
|
|
||||||
void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id,
|
void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id,
|
||||||
Clock::TimeSpanType current_time_point) {
|
Clock::TimeSpanType current_time_point) {
|
||||||
const Clock::TimeSpanType ticks_time_span{Clock::TimeSpanType::FromTicks(
|
const Clock::TimeSpanType ticks_time_span{
|
||||||
system.CoreTiming().GetClockTicks(), Core::Hardware::CNTFREQ)};
|
Clock::TimeSpanType::FromTicks<Core::Hardware::CNTFREQ>(
|
||||||
|
system.CoreTiming().GetClockTicks())};
|
||||||
const Clock::SteadyClockContext context{
|
const Clock::SteadyClockContext context{
|
||||||
static_cast<u64>(current_time_point.nanoseconds - ticks_time_span.nanoseconds),
|
static_cast<u64>(current_time_point.nanoseconds - ticks_time_span.nanoseconds),
|
||||||
clock_source_id};
|
clock_source_id};
|
||||||
|
|
Loading…
Reference in a new issue