From daeefb4a7f989ad7eb4de60ac0de464376a1c800 Mon Sep 17 00:00:00 2001 From: Soham Nandy Date: Fri, 11 Apr 2025 11:42:36 +0530 Subject: [PATCH] refactor(rdtsc): clean up code overall Change type of chrono return to be auto Co-authored-by: Maurice Heumann simplify conditional compile and add #else to prevent clang-tidy breaking CI Co-authored-by: Maurice Heumann --- src/common/utils/time.hpp | 15 ++++++--------- src/windows-emulator/windows_emulator.cpp | 14 ++------------ 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/common/utils/time.hpp b/src/common/utils/time.hpp index 25236fe0..255045fc 100644 --- a/src/common/utils/time.hpp +++ b/src/common/utils/time.hpp @@ -40,16 +40,13 @@ namespace utils /// TODO: find better solution for ARM and Figure out better CPU base frequency heuristics virtual uint64_t timestamp_counter() { -#if defined(_MSC_VER) -#if defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86) - return __rdtsc(); // 64-bit with MSVC intrinsic -#endif - -#elif defined(__x86_64__) || defined(__i386__) || defined(__amd64__) // If we are using clang or gcc - return __rdtsc(); // 64-bit with clang/gcc intrinsic -#endif - int64_t count = std::chrono::high_resolution_clock::now().time_since_epoch().count(); +#if defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86) || defined(__x86_64__) || defined(__i386__) || \ + defined(__amd64__) + return __rdtsc(); // any x86 system will have this instrinsic +#else + const auto count = std::chrono::high_resolution_clock::now().time_since_epoch().count(); return static_cast((count * 38LL) / 10LL); +#endif } }; diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index 4db7c843..4adbb7ab 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -449,18 +449,8 @@ void windows_emulator::setup_hooks() this->emu().hook_instruction(x64_hookable_instructions::rdtsc, [&] { uint64_t ticks = this->clock_->timestamp_counter(); - static uint64_t fake_ticks = ticks; - static uint64_t prev_ticks = 0; - - if (ticks > prev_ticks) - { - fake_ticks += (ticks - prev_ticks); - } - fake_ticks = std::min(fake_ticks, ticks); - prev_ticks = ticks; - - this->emu().reg(x64_register::rax, fake_ticks & 0xFFFFFFFF); - this->emu().reg(x64_register::rdx, (fake_ticks >> 32) & 0xFFFFFFFF); + this->emu().reg(x64_register::rax, ticks & 0xFFFFFFFF); + this->emu().reg(x64_register::rdx, (ticks >> 32) & 0xFFFFFFFF); return instruction_hook_continuation::skip_instruction; });