core_timing_util.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include "core/core_timing_util.h"
  5. #include <cinttypes>
  6. #include <limits>
  7. #include "common/logging/log.h"
  8. namespace CoreTiming {
  9. constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE;
  10. s64 usToCycles(s64 us) {
  11. if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
  12. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  13. return std::numeric_limits<s64>::max();
  14. }
  15. if (us > MAX_VALUE_TO_MULTIPLY) {
  16. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  17. return BASE_CLOCK_RATE * (us / 1000000);
  18. }
  19. return (BASE_CLOCK_RATE * us) / 1000000;
  20. }
  21. s64 usToCycles(u64 us) {
  22. if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
  23. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  24. return std::numeric_limits<s64>::max();
  25. }
  26. if (us > MAX_VALUE_TO_MULTIPLY) {
  27. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  28. return BASE_CLOCK_RATE * static_cast<s64>(us / 1000000);
  29. }
  30. return (BASE_CLOCK_RATE * static_cast<s64>(us)) / 1000000;
  31. }
  32. s64 nsToCycles(s64 ns) {
  33. if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
  34. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  35. return std::numeric_limits<s64>::max();
  36. }
  37. if (ns > MAX_VALUE_TO_MULTIPLY) {
  38. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  39. return BASE_CLOCK_RATE * (ns / 1000000000);
  40. }
  41. return (BASE_CLOCK_RATE * ns) / 1000000000;
  42. }
  43. s64 nsToCycles(u64 ns) {
  44. if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
  45. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  46. return std::numeric_limits<s64>::max();
  47. }
  48. if (ns > MAX_VALUE_TO_MULTIPLY) {
  49. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  50. return BASE_CLOCK_RATE * (static_cast<s64>(ns) / 1000000000);
  51. }
  52. return (BASE_CLOCK_RATE * static_cast<s64>(ns)) / 1000000000;
  53. }
  54. } // namespace CoreTiming