[Base] Clock reworked.

- Time progression is now equal and in sync on all threads.
- Floating point imprecisions do not interfere with timing.
This commit is contained in:
Joel Linn
2019-11-20 00:28:08 +01:00
committed by Rick Gibbed
parent 6a3a56b3b9
commit d6ce72ddc9
3 changed files with 121 additions and 44 deletions

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Copyright 2019 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -15,6 +15,7 @@
#include <cstdint>
#include <cstring>
#include <limits>
#include <numeric>
#include <type_traits>
#include "xenia/base/platform.h"
@@ -59,6 +60,34 @@ T next_pow2(T value) {
return value;
}
#if __cpp_lib_gcd_lcm
template <typename T>
inline constexpr T greatest_common_divisor(T a, T b) {
return std::gcd(a, b);
}
#else
template <typename T>
constexpr T greatest_common_divisor(T a, T b) {
// Use the Euclid algorithm to calculate the greatest common divisor
while (b) {
a = std::exchange(b, a % b);
}
return a;
}
#endif
template <typename T>
inline constexpr void reduce_fraction(T& numerator, T& denominator) {
auto gcd = greatest_common_divisor(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
template <typename T>
inline constexpr void reduce_fraction(std::pair<T, T>& fraction) {
reduce_fraction<T>(fraction.first, fraction.second);
}
constexpr uint32_t make_bitmask(uint32_t a, uint32_t b) {
return (static_cast<uint32_t>(-1) >> (31 - b)) & ~((1u << a) - 1);
}