| 
									
										
										
										
											2018-07-24 11:03:24 +01: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-15 20:04:11 -04:00
										 |  |  | #include "common/uint128.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 12:32:15 -05:00
										 |  |  | namespace Core::Timing { | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  | constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / Hardware::BASE_CLOCK_RATE; | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-04 19:52:42 -04:00
										 |  |  | s64 msToCycles(std::chrono::milliseconds ms) { | 
					
						
							|  |  |  |     if (static_cast<u64>(ms.count() / 1000) > MAX_VALUE_TO_MULTIPLY) { | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  |         LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | 
					
						
							|  |  |  |         return std::numeric_limits<s64>::max(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-04 19:52:42 -04:00
										 |  |  |     if (static_cast<u64>(ms.count()) > MAX_VALUE_TO_MULTIPLY) { | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  |         LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  |         return Hardware::BASE_CLOCK_RATE * (ms.count() / 1000); | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  |     return (Hardware::BASE_CLOCK_RATE * ms.count()) / 1000; | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-04 19:52:42 -04:00
										 |  |  | s64 usToCycles(std::chrono::microseconds us) { | 
					
						
							|  |  |  |     if (static_cast<u64>(us.count() / 1000000) > MAX_VALUE_TO_MULTIPLY) { | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  |         LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | 
					
						
							|  |  |  |         return std::numeric_limits<s64>::max(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-04 19:52:42 -04:00
										 |  |  |     if (static_cast<u64>(us.count()) > MAX_VALUE_TO_MULTIPLY) { | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  |         LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  |         return Hardware::BASE_CLOCK_RATE * (us.count() / 1000000); | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  |     return (Hardware::BASE_CLOCK_RATE * us.count()) / 1000000; | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-04 19:52:42 -04:00
										 |  |  | s64 nsToCycles(std::chrono::nanoseconds ns) { | 
					
						
							|  |  |  |     if (static_cast<u64>(ns.count() / 1000000000) > MAX_VALUE_TO_MULTIPLY) { | 
					
						
							|  |  |  |         LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | 
					
						
							|  |  |  |         return std::numeric_limits<s64>::max(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (static_cast<u64>(ns.count()) > MAX_VALUE_TO_MULTIPLY) { | 
					
						
							|  |  |  |         LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  |         return Hardware::BASE_CLOCK_RATE * (ns.count() / 1000000000); | 
					
						
							| 
									
										
										
										
											2019-06-04 19:52:42 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  |     return (Hardware::BASE_CLOCK_RATE * ns.count()) / 1000000000; | 
					
						
							| 
									
										
										
										
											2018-07-24 11:03:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 14:45:08 -04:00
										 |  |  | u64 msToClockCycles(std::chrono::milliseconds ns) { | 
					
						
							|  |  |  |     const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | 
					
						
							|  |  |  |     return Common::Divide128On32(temp, 1000).first; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | u64 usToClockCycles(std::chrono::microseconds ns) { | 
					
						
							|  |  |  |     const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | 
					
						
							|  |  |  |     return Common::Divide128On32(temp, 1000000).first; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 19:12:27 -04:00
										 |  |  | u64 nsToClockCycles(std::chrono::nanoseconds ns) { | 
					
						
							| 
									
										
										
										
											2020-02-10 14:45:08 -04:00
										 |  |  |     const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | 
					
						
							|  |  |  |     return Common::Divide128On32(temp, 1000000000).first; | 
					
						
							| 
									
										
										
										
											2020-02-05 19:12:27 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-15 13:15:28 -04:00
										 |  |  | u64 CpuCyclesToClockCycles(u64 ticks) { | 
					
						
							| 
									
										
										
										
											2020-02-11 19:56:24 -04:00
										 |  |  |     const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); | 
					
						
							|  |  |  |     return Common::Divide128On32(temporal, static_cast<u32>(Hardware::BASE_CLOCK_RATE)).first; | 
					
						
							| 
									
										
										
										
											2019-02-15 13:15:28 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 12:32:15 -05:00
										 |  |  | } // namespace Core::Timing
 |