clock_cycle.h 1.22 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#ifndef CLOCK_CYCLE_H
#define CLOCK_CYCLE_H

typedef unsigned long long int u64;

u64 start_rdtsc( )
{
        unsigned high, low;
        
        __asm__ volatile("CPUID\n\t"
                         "RDTSC\n\t"
                         "mov %%edx, %0\n\t"
                         "mov %%eax, %1\n\t": "=r" (high),
                         "=r" (low):: "%rax", "%rbx", "%rcx", "%rdx");
        return ( ((u64)low) | (((u64)high) << 32));
}

u64 end_rdtsc( )
{
        unsigned high, low;
        
        __asm__ volatile("RDTSCP\n\t"
                         "mov %%edx, %0\n\t"
                         "mov %%eax,%1\n\t"
                         "CPUID\n\t": "=r" (high), "=r" (low)::
                         "%rax", "%rbx", "%rcx", "%rdx");
        
        return ( ((u64)low) | (((u64)high) << 32));
}

static inline u64 cpucycles( )
{
        u64 result;
        asm volatile (".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
                      : "=a" (result) ::  "%rdx");
        return result;
}

/*#ifdef __x86_64__
 #define mycpucycles(RES) \
 __asm__ volatile("rdtsc;shlq $32,%%rdx;orq %%rdx,%%rax" : "=a" (RES) ::  "%rdx");
 #else
 #define mycpucycles(RES) \
 __asm__ volatile(".byte 15;.byte 49" : "=A" (RES));
 #endif
 */

#endif