-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrdtsc.h
80 lines (63 loc) · 1.88 KB
/
rdtsc.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* This is the RDTSC timer.
* RDTSC is an instruction on several Intel and compatible CPUs that Reads the
* Time Stamp Counter. The Intel manuals contain more information.
*/
#ifndef RDTSC_H_INCLUDED
#define RDTSC_H_INCLUDED
#define COUNTER_LO(a) ((a).int32.lo)
#define COUNTER_HI(a) ((a).int32.hi)
#define COUNTER_VAL(a) ((a).int64)
#define COUNTER(a) \
((double)COUNTER_VAL(a))
#define COUNTER_DIFF(a,b) \
(COUNTER(a)-COUNTER(b))
/* ==================== GNU C and possibly other UNIX compilers ===================== */
#ifndef WIN32
#if defined(__GNUC__) || defined(__linux__)
#define VOLATILE __volatile__
#define ASM __asm__
#else
/* if we're neither compiling with gcc or under linux, we can hope
* the following lines work, they probably won't */
#define ASM asm
#define VOLATILE
#endif
#define myInt64 unsigned long long
#define INT32 unsigned int
typedef union
{ myInt64 int64;
struct {INT32 lo, hi;} int32;
} tsc_counter;
#if defined(__ia64__)
#if defined(__INTEL_COMPILER)
#define RDTSC(tsc) (tsc).int64=__getReg(3116)
#else
#define RDTSC(tsc) ASM VOLATILE ("mov %0=ar.itc" : "=r" ((tsc).int64) )
#endif
#define CPUID() do{/*No need for serialization on Itanium*/}while(0)
#else
#define RDTSC(cpu_c) \
ASM VOLATILE ("rdtsc" : "=a" ((cpu_c).int32.lo), "=d"((cpu_c).int32.hi))
#define CPUID() \
ASM VOLATILE ("cpuid" : : "a" (0) : "bx", "cx", "dx" )
#endif
/* ======================== WIN32 ======================= */
#else
#define myInt64 signed __int64
#define INT32 unsigned __int32
typedef union
{ myInt64 int64;
struct {INT32 lo, hi;} int32;
} tsc_counter;
#define RDTSC(cpu_c) \
{ __asm rdtsc \
__asm mov (cpu_c).int32.lo,eax \
__asm mov (cpu_c).int32.hi,edx \
}
#define CPUID() \
{ \
__asm mov eax, 0 \
__asm cpuid \
}
#endif
#endif