Skip to content

Instantly share code, notes, and snippets.

@vejuhust
Created October 5, 2012 06:03
Show Gist options
  • Save vejuhust/3838331 to your computer and use it in GitHub Desktop.
Save vejuhust/3838331 to your computer and use it in GitHub Desktop.
Read Time Stamp Counter from Intel x86 CPU
#include <stdio.h>
int main (int argc, const char * argv[]) {
unsigned int hi;
unsigned int lo;
long double result;
/* Set *hi and *lo to the high and low order bits of the cycle counter.
Implementation requires assembly code to use the rdtsc instruction. */
asm("rdtsc; movl %%edx,%0; movl %%eax,%1" /* Read cycle counter */
: "=r" (hi), "=r" (lo) /* and move results to */
: /* No input here */ /* the two outputs */
: "%edx", "%eax");
/* Computer result. Use 2^30*2^2 instead of 2^32 to avoid overflow */
result = ((long double) hi) * (1 << 30) * 4 + ((long double) lo);
/* Output the result */
printf("%.0Lf", result);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment