"rdtsc": "=a" (a0), "=d" (d0) what does this do? [duplicate]
Asked Answered
F

1

6

I'm new to C++ and benchmarking

I don't understand what the this part of the code does? So I found something about the edx, eax registers, but I don't fully understand how that plays into the code. So I understand this code essentially returns the current tick of the cpu cycle. So, does it store the current tick into the registers, one part in the hi and the other part in the lo. And, does the "=a", and "=d" specify which register to store it in.

And what is the significance of breaking it into two parts.

"rdtsc" : "=a" (lo), "=d" (hi) 

Code in context:

int64_t rdtsc(){
    unsigned int lo,hi;
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return ((uint64_t)hi << 32) | lo;
}
Fawkes answered 8/7, 2019 at 17:41 Comment(2)
Related: How to get the CPU cycle count in x86_64 from C++? proposed this asm without explaining it. It's safe on both 32-bit and 64-bit x86. But you should actually use the intrinsic. Also, you could make this slightly more efficient by declaring the outputs unsigned long, so the compiler knows it doesn't have to zero-extend EAX into RAX; it's already zero extended. Using the intrinsic saves you from little details like that.Threecolor
I added a section to Mysticial's answer on that question which explains how the asm works. So this is now pretty much a duplicate of that canonical Q&A. Since you're new to benchmarking, you should definitely read my answer for caveats and gotchas about RDTSC.Threecolor
U
7

It uses inline assembly to call the rdtsc opcode which returns an 64 bit integer. The high part is stored to hi and the low to lo.

In Windows and Visual Studio where inline assembly is not available in x64, you would use the __rdtsc.

Underscore answered 8/7, 2019 at 17:43 Comment(1)
In gcc (and compatible), there's also __builtin_ia32_rdtsc(). I'm a big fan of gcc.gnu.org/wiki/DontUseInlineAsm (says the guy who wrote it).Licketysplit

© 2022 - 2024 — McMap. All rights reserved.