refactor(rdtsc): clean up tick logic,

fix intrinsics, and resolve x86 compiler issues

- fix `time.hpp` compiler warnings (errors) and use proper intrinsic for gcc
- simplify tick calculation logic
- fix: exclude intrinsics include for non x86 builds and fix style
- fix: get() on clock_ unique ptr
This commit is contained in:
Soham Nandy
2025-04-11 00:40:38 +05:30
parent dbc1b4439e
commit 0271e39474
2 changed files with 9 additions and 15 deletions

View File

@@ -6,6 +6,8 @@
#if defined(_MSC_VER)
#include <intrin.h>
#pragma intrinsic(__rdtsc)
#elif defined(__x86_64__) || defined(__i386__) || defined(__amd64__)
#include <x86intrin.h>
#endif
constexpr auto HUNDRED_NANOSECONDS_IN_ONE_SECOND = 10000000LL;
@@ -44,12 +46,10 @@ namespace utils
#endif
#elif defined(__x86_64__) || defined(__i386__) || defined(__amd64__) // If we are using clang or gcc
unsigned int lo, hi;
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)hi << 32) | lo;
return __rdtsc(); // 64-bit with clang/gcc intrinsic
#endif
return static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) *
3.8; // should be base cpu frequency here;
int64_t count = std::chrono::high_resolution_clock::now().time_since_epoch().count();
return static_cast<uint64_t>((count * 38LL) / 10LL);
}
};

View File

@@ -448,21 +448,15 @@ void windows_emulator::setup_hooks()
});
this->emu().hook_instruction(x64_hookable_instructions::rdtsc, [&] {
uint64_t ticks = this->clock_.get()->timestamp_counter();
uint64_t ticks = this->clock_->timestamp_counter();
static uint64_t fake_ticks = ticks;
static uint64_t prev_ticks = 0;
if (prev_ticks != 0)
if (ticks > prev_ticks)
{
if (ticks > prev_ticks)
{
fake_ticks += (ticks - prev_ticks);
}
}
if (fake_ticks > ticks)
{
fake_ticks = 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);