-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_cycles.h
98 lines (92 loc) · 2 KB
/
get_cycles.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
/*
* Copyright (c) 2022 Tino Reichardt <[email protected]>
*/
#ifndef _SYS_GET_CYCLES_H
#define _SYS_GET_CYCLES_H
#ifdef __cplusplus
extern "C" {
#endif
/* cycle counter for various platforms
*
* usage:
* cycles_t c_start, c_stop;
* c_start = get_cycles();
* <some code to test>
* c_stop = get_cycles();
* printf("ticks needed for code are: %u\n", c_stop - c_start);
*/
#if defined(__i386__)
typedef unsigned long long cycles_t;
static inline cycles_t get_cycles(void)
{
cycles_t tick;
asm volatile ("rdtsc":"=A" (tick));
return tick;
}
#elif defined(__x86_64__)
typedef unsigned long long cycles_t;
static inline cycles_t get_cycles(void)
{
unsigned int tickl, tickh;
asm volatile ("rdtsc":"=a" (tickl), "=d"(tickh));
return ((cycles_t) tickh << 32) | tickl;
}
#elif defined(__s390__)
typedef unsigned long long cycles_t;
static inline cycles_t get_cycles(void)
{
cycles_t tick;
asm volatile ("stck %0":"=Q" (tick)::"cc");
return tick >> 2;
}
#elif defined(__alpha__)
typedef unsigned int cycles_t;
static inline cycles_t get_cycles(void)
{
unsigned int tick;
asm volatile ("rpcc %0":"=r" (tick));
return tick;
}
#elif defined(__sparc__)
typedef long long cycles_t;
static inline cycles_t get_cycles(void)
{
long long tick;
asm volatile ("rd %%tick,%0":"=r" (tick));
return tick;
}
#elif defined(_MSC_VER)
#include <intrin.h>
#pragma intrinsic(__rdtsc)
typedef unsigned __int64 cycles_t;
static inline cycles_t get_cycles(void)
{
return __rdtsc();
}
#elif defined(HAVE_ARMV7A_CNTVCT)
typedef uint64_t ticks;
static inline ticks getticks(void)
{
uint32_t Rt, Rt2 = 0;
asm volatile("mrrc p15, 1, %0, %1, c14" : "=r"(Rt), "=r"(Rt2));
return (((uint64_t)Rt) | (((uint64_t)Rt2) << 32));
}
#elif defined(HAVE_ARMV7A_PMCCNTR)
typedef uint64_t ticks;
static inline ticks getticks(void)
{
uint32_t r;
asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(r));
return r;
}
#else
typedef uint64_t cycles_t;
static inline cycles_t get_cycles(void)
{
return 0;
}
#endif
#ifdef __cplusplus
}
#endif
#endif /* _SYS_GET_CYCLES_H */