3

I don't know what exactly does this code:

int rdtsc(){
    __asm__ __volatile__("rdtsc");

Please, someone can explain me? why "rdtsc"?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user17629
  • 159
  • 2
  • 6

2 Answers2

11

Actually, that's not very good code at all.

RDTSC is the x86 instruction "ReaD TimeStamp Counter" - it reads a 64-bit counter that counts up at every clock cycle of your processor.

But since it's a 64-bit number, it's stored in EAX (low part) and EDX (high part), and if this code is ever used in a case where it is inlined, the compiler doesn't know that EDX is being clobbered. Or that the inline assembly sets EAX before falling off the end of a non-void function.

The compiler doesn't "understand" the assembler code, it's a black box which you must describe with input/output operands so it knows there's an output in EDX:EAX. (Or an output in EAX with EDX being clobbered). I would do this:

uint64_t rdtsc()
{
   uint32_t hi, lo;
   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
   return ( (uint64_t)lo)|( ((uint64_t)hi)<<32 );
}

thus giving a time-count that doesn't wrap around every second or two on a modern machine, and which tells the compiler which registers your asm statement modifies.

Or use the __rdtsc() intrinsic to get the compiler to emit the rdtsc instruction itself, and know where the outputs are. See Get CPU cycle count?.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Comment from another user: you're missing a parenthesis in the last code block – Artjom B. Jun 15 '15 at 17:34
  • 1
    why does "=a", and "=d" work in assembly? how does it know to refer to eax/edx? – galois Oct 21 '16 at 16:56
  • The first colon in an `asm` statement describes the "output parameters", the register naming is described in the x86 part of the gcc inline assembler syntax. You can use google to search for that syntax - there are several different pages describing gcc x86 syntax. – Mats Petersson Oct 22 '16 at 06:53
  • 1
    No need for C-style casts here. Plain `uint64_t(hi) << 32 | lo` does it nicely. – Maxim Egorushkin Feb 22 '17 at 12:21
7

The often-cited inline assembly for rdtsc produces supeflous code with gcc-7 and earlier versions.

A more efficient solution is to use __builtin_ia32_rdtsc built-in function:

uint64_t tsc = __builtin_ia32_rdtsc();
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271