-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdtsc.h
139 lines (107 loc) · 2.82 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef __RDTSC_H_DEFINED__
#define __RDTSC_H_DEFINED__
#include <linux/types.h>
#if defined(__i386__)
static __inline__ __u64 rdtsc(void)
{
__u64 x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#elif defined(__x86_64__)
static __inline__ __u64 rdtsc(void)
{
__u32 hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ( (__u64)lo)|( ((__u64)hi)<<32 );
}
#elif defined(__powerpc__)
static __inline__ __u64 rdtsc(void)
{
__u64 result=0;
__u32 upper, lower,tmp;
__asm__ volatile(
"0:\n"
"\tmftbu %0\n"
"\tmftb %1\n"
"\tmftbu %2\n"
"\tcmpw %2,%0\n"
"\tbne 0b\n"
: "=r"(upper),"=r"(lower),"=r"(tmp)
);
result = upper;
result = result<<32;
result = result|lower;
return(result);
}
#else
#error "No tick counter is available!"
#endif
#if 0
#if defined(__i386__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#elif defined(__x86_64__)
typedef unsigned long long int unsigned long long;
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
#elif defined(__powerpc__)
typedef unsigned long long int unsigned long long;
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int result=0;
unsigned long int upper, lower,tmp;
__asm__ volatile(
"0: \n"
"\tmftbu %0 \n"
"\tmftb %1 \n"
"\tmftbu %2 \n"
"\tcmpw %2,%0 \n"
"\tbne 0b \n"
: "=r"(upper),"=r"(lower),"=r"(tmp)
);
result = upper;
result = result<<32;
result = result|lower;
return(result);
}
#else
#error "No tick counter is available!"
#endif
#endif
/* $RCSfile: $ $Author: kazutomo $
* $Revision: 1.6 $ $Date: 2005/04/13 18:49:58 $
*/
/*
* Pentium class cpu has an instruction to read the current time-stamp counter variable ,which is a
* 64-bit variable, into registers (edx:eax). TSC(time stamp counter) is incremented every cpu tick
* (1/CPU_HZ). For example, at 1GHz cpu, TSC is incremented by 10^9 per second. It allows to
* measure time activety in an accurate fashion.
*
* PowerPC provides similar capability but PowerPC time counter is increments at either equal the
* processor core clock rate or as driven by a separate timer clock input. PowerPC time counter is
* also a 64-bit.
*
* The example..
*/
#if 0
#include <stdio.h>
#include "rdtsc.h"
int main(int argc, char* argv[])
{
unsigned long long a,b;
a = rdtsc();
b = rdtsc();
printf("%llu\n", b-a);
return 0;
}
#endif
#endif /* __RDTSC_H_DEFINED__ */