[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

solution: pentium timer code



Here's a complete program to extract and print the
current 64 bit pentium timer value.

Many thanks to  Andris Pavenis <pavenis@lanet.lv>
who forwarded the essential assembly routine.
- Doug



//gcc pentiumtimer.c -o pentiumtimer -O2 ; ./pentiumtimer

#include <stdio.h>

#ifndef __RDTSC_H
#define __RDTSC_H

  inline long long rdtsc(void)
  {
     union { long L[2]; long long LL; } W;
     asm __volatile__ (".byte 0x0F,0x31" : "=a"(W.L[0]), "=d"(W.L[1]));
     return W.LL;
  }

#endif


main()
{
   unsigned long prev = 0L;
   union { unsigned long L[2];
           unsigned long long LL;
          }X;

        X.LL = rdtsc();
        printf("cnt %u %u\n", X.L[1], prev = X.L[0]);

        X.LL = rdtsc();
        printf("cnt %u %u delta %u\n", X.L[1], X.L[0], X.L[0] - prev );
}