2018-07-24 12:03:24 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "core/core_timing_util.h"
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <limits>
|
|
|
|
#include "common/logging/log.h"
|
2019-02-16 01:04:11 +01:00
|
|
|
#include "common/uint128.h"
|
2018-07-24 12:03:24 +02:00
|
|
|
|
2019-02-12 18:32:15 +01:00
|
|
|
namespace Core::Timing {
|
2018-07-24 12:03:24 +02:00
|
|
|
|
|
|
|
constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE;
|
|
|
|
|
|
|
|
s64 usToCycles(s64 us) {
|
2019-05-25 23:01:16 +02:00
|
|
|
if (static_cast<u64>(us / 1000000) > MAX_VALUE_TO_MULTIPLY) {
|
2018-07-24 12:03:24 +02:00
|
|
|
LOG_ERROR(Core_Timing, "Integer overflow, use max value");
|
|
|
|
return std::numeric_limits<s64>::max();
|
|
|
|
}
|
2019-05-25 23:01:16 +02:00
|
|
|
if (static_cast<u64>(us) > MAX_VALUE_TO_MULTIPLY) {
|
2018-07-24 12:03:24 +02:00
|
|
|
LOG_DEBUG(Core_Timing, "Time very big, do rounding");
|
|
|
|
return BASE_CLOCK_RATE * (us / 1000000);
|
|
|
|
}
|
|
|
|
return (BASE_CLOCK_RATE * us) / 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 usToCycles(u64 us) {
|
2019-06-05 01:41:48 +02:00
|
|
|
return usToCycles(static_cast<s64>(us));
|
2018-07-24 12:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s64 nsToCycles(s64 ns) {
|
2019-05-25 23:01:16 +02:00
|
|
|
if (static_cast<u64>(ns / 1000000000) > MAX_VALUE_TO_MULTIPLY) {
|
2018-07-24 12:03:24 +02:00
|
|
|
LOG_ERROR(Core_Timing, "Integer overflow, use max value");
|
|
|
|
return std::numeric_limits<s64>::max();
|
|
|
|
}
|
2019-05-25 23:01:16 +02:00
|
|
|
if (static_cast<u64>(ns) > MAX_VALUE_TO_MULTIPLY) {
|
2018-07-24 12:03:24 +02:00
|
|
|
LOG_DEBUG(Core_Timing, "Time very big, do rounding");
|
|
|
|
return BASE_CLOCK_RATE * (ns / 1000000000);
|
|
|
|
}
|
|
|
|
return (BASE_CLOCK_RATE * ns) / 1000000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 nsToCycles(u64 ns) {
|
2019-06-05 01:41:48 +02:00
|
|
|
return nsToCycles(static_cast<s64>(ns));
|
2018-07-24 12:03:24 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 18:15:28 +01:00
|
|
|
u64 CpuCyclesToClockCycles(u64 ticks) {
|
2019-02-16 21:52:24 +01:00
|
|
|
const u128 temporal = Common::Multiply64Into128(ticks, CNTFREQ);
|
|
|
|
return Common::Divide128On32(temporal, static_cast<u32>(BASE_CLOCK_RATE)).first;
|
2019-02-15 18:15:28 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 18:32:15 +01:00
|
|
|
} // namespace Core::Timing
|