Reading rdtsc on amd64

On an 386 you can read the rdtsc by simply doing

unsigned long long result;
__asm__ __volatile__("rdtsc" : "=A" (result));

Note, however, in the gcc docs the documentation for the =A constraint has an important caveat

Thus this is not what you want when using amd64 in 64 bit mode with 64 bit registers. Follow the example of the kernel code, and do the shifts by hand

#define rdtscll(val) do { \
     unsigned int __a,__d; \
     asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
     (val) = ((unsigned long)__a) | (((unsigned long)__d)<<32); \
} while(0)

IA64wiki: rdtscOnAmd64 (last edited 2009-12-10 03:13:46 by localhost)

Gelato@UNSW is sponsored by
the University of New South Wales National ICT Australia The Gelato Federation Hewlett-Packard Company Australian Research Council
Please contact us with any questions or comments.