summaryrefslogtreecommitdiffstats
path: root/src/core/core_timing_util.cpp
diff options
context:
space:
mode:
authorMerryMage <MerryMage@users.noreply.github.com>2018-07-24 12:03:24 +0200
committerMerryMage <MerryMage@users.noreply.github.com>2018-07-24 12:03:24 +0200
commit44646e2ea0b87dffd9a1d175c53e3cea67bce344 (patch)
treefd2dcd57424487cf054d98a018ae9d5c62cfd60a /src/core/core_timing_util.cpp
parentCMakeLists: Sort filenames (diff)
downloadyuzu-44646e2ea0b87dffd9a1d175c53e3cea67bce344.tar
yuzu-44646e2ea0b87dffd9a1d175c53e3cea67bce344.tar.gz
yuzu-44646e2ea0b87dffd9a1d175c53e3cea67bce344.tar.bz2
yuzu-44646e2ea0b87dffd9a1d175c53e3cea67bce344.tar.lz
yuzu-44646e2ea0b87dffd9a1d175c53e3cea67bce344.tar.xz
yuzu-44646e2ea0b87dffd9a1d175c53e3cea67bce344.tar.zst
yuzu-44646e2ea0b87dffd9a1d175c53e3cea67bce344.zip
Diffstat (limited to '')
-rw-r--r--src/core/core_timing_util.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp
new file mode 100644
index 000000000..73dea4edb
--- /dev/null
+++ b/src/core/core_timing_util.cpp
@@ -0,0 +1,63 @@
+// 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"
+
+namespace CoreTiming {
+
+constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE;
+
+s64 usToCycles(s64 us) {
+ if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
+ LOG_ERROR(Core_Timing, "Integer overflow, use max value");
+ return std::numeric_limits<s64>::max();
+ }
+ if (us > MAX_VALUE_TO_MULTIPLY) {
+ 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) {
+ if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
+ LOG_ERROR(Core_Timing, "Integer overflow, use max value");
+ return std::numeric_limits<s64>::max();
+ }
+ if (us > MAX_VALUE_TO_MULTIPLY) {
+ LOG_DEBUG(Core_Timing, "Time very big, do rounding");
+ return BASE_CLOCK_RATE * static_cast<s64>(us / 1000000);
+ }
+ return (BASE_CLOCK_RATE * static_cast<s64>(us)) / 1000000;
+}
+
+s64 nsToCycles(s64 ns) {
+ if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
+ LOG_ERROR(Core_Timing, "Integer overflow, use max value");
+ return std::numeric_limits<s64>::max();
+ }
+ if (ns > MAX_VALUE_TO_MULTIPLY) {
+ 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) {
+ if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
+ LOG_ERROR(Core_Timing, "Integer overflow, use max value");
+ return std::numeric_limits<s64>::max();
+ }
+ if (ns > MAX_VALUE_TO_MULTIPLY) {
+ LOG_DEBUG(Core_Timing, "Time very big, do rounding");
+ return BASE_CLOCK_RATE * (static_cast<s64>(ns) / 1000000000);
+ }
+ return (BASE_CLOCK_RATE * static_cast<s64>(ns)) / 1000000000;
+}
+
+} // namespace CoreTiming