c++assemblyrdtsc

what is the C++ equivalent for this assembly code


I'm trying to figure out how to read this assembly code in C++.

This is the code:

unsigned __int64 high_perf_time;
unsigned __int64 *dest = &high_perf_time;
__asm 
{
    _emit 0xf        // these two bytes form the 'rdtsc' asm instruction,
    _emit 0x31       //  available on Pentium I and later.
    mov esi, dest
    mov [esi  ], eax    // lower 32 bits of tsc
    mov [esi+4], edx    // upper 32 bits of tsc
}
__int64 time_s     = (__int64)(high_perf_time / frequency);  // unsigned->sign conversion should be safe here
__int64 time_fract = (__int64)(high_perf_time % frequency);  // unsigned->sign conversion should be safe here

I know 0xf 0x31 is rdtsc eax, edx, but what is mov esi,dest? How can I write that in C++?


Solution

  • The C++ code is

    #include <intrin.h>
    unsigned __int64 high_perf_time = __rdtsc();
    

    Documentation here: https://learn.microsoft.com/en-us/cpp/intrinsics/rdtsc

    But you probably want QueryPerformanceCounter() and QueryPerformanceFrequency() instead, else you will have a race condition if your thread is scheduled on a different processor core between successive __rdtsc() calls.