forked from libmoon/libmoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdtsc.h
48 lines (43 loc) · 1.11 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
#ifndef RDTSC_H__
#define RDTSC_H__
#include <stdint.h>
union tsc_t {
uint64_t tsc_64;
struct {
uint32_t lo_32;
uint32_t hi_32;
};
};
/**
* Read value of TSC register without enforced serialization.
* Taken from "How to Benchmark Code Execution Times on Intel IA-32 and IA-64 Instruction Set Architectures"
*
* @return
* Value of TSC register
*/
inline static uint64_t read_rdtsc() {
union tsc_t tsc;
asm volatile("RDTSC\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t"
: "=r" (tsc.hi_32),
"=r" (tsc.lo_32):: "%rax", "%rbx", "%rcx", "%rdx");
return tsc.tsc_64;
}
/**
* Read value of TSC register with enforced serialization.
* Taken from "How to Benchmark Code Execution Times on Intel IA-32 and IA-64 Instruction Set Architectures"
*
* @return
* Value of TSC register
*/
inline uint64_t read_rdtscp(void) {
union tsc_t tsc;
asm volatile("RDTSCP\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t"
: "=r" (tsc.hi_32),
"=r" (tsc.lo_32):: "%rax", "%rbx", "%rcx", "%rdx");
return tsc.tsc_64;
}
#endif