-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclocks.h
91 lines (83 loc) · 2.07 KB
/
clocks.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
#ifndef _CLOCKS_H_
#define _CLOCKS_H_
#include<stdint.h>
#include<stdlib.h>
#ifdef __INTEL_COMPILER
#define BARRIER __memory_barrier()
#else
#define BARRIER __asm__ __volatile__ ("" ::: "memory")
#endif
#define CLOCKS_RANDOM(RANDOM,FUNCTION) \
do{ \
uint64_t start,end;int64_t i_bench,j_bench; \
unsigned cycles_high0,cycles_low0; \
unsigned cycles_high1,cycles_low1; \
__asm__ __volatile__ (\
"mfence\n\t" \
"RDTSC\n\t" \
"mov %%edx, %0\n\t" \
"mov %%eax, %1\n\t": "=r" (cycles_high0), "=r" (cycles_low0):: \
"%rax", "%rbx", "%rcx", "%rdx"); \
BARRIER;\
i_bench=BENCH;\
do{\
j_bench=BENCH;\
RANDOM;\
do{\
FUNCTION;\
j_bench--;\
}while( j_bench != 0 );\
i_bench--;\
}while( i_bench != 0 );\
BARRIER;\
__asm__ __volatile__("RDTSCP\n\t" \
"mov %%edx, %0\n\t" \
"mov %%eax, %1\n\t" \
"mfence\n\t" \
: "=r" (cycles_high1), "=r" (cycles_low1):: \
"%rax", "%rbx", "%rcx", "%rdx"); \
start= (((uint64_t)cycles_high0)<<32)|cycles_low0;\
end = (((uint64_t)cycles_high1)<<32)|cycles_low1;\
printf("%s: %lu cc\n",#FUNCTION,(end-start)/(BENCH*BENCH));\
}while(0)
#define CLOCKS(FUNCTION) CLOCKS_RANDOM(while(0),FUNCTION)
/**
* Taken from
* agl/curve25519-donna
* https://github.com/agl/curve25519-donna/blob/master/speed-curve25519.c
*
* ticks - not tested on anything other than x86
* */
#define oper_second_random(RANDOM,FUNCTION) \
do{\
\
unsigned i;\
uint64_t start, end;\
const unsigned iterations = 100000;\
uint64_t start_c, end_c;\
printf("Operations per second: %s.\n",#FUNCTION);\
RANDOM;\
\
/* Load the caches*/\
for (i = 0; i < 1000; ++i) {\
FUNCTION ;\
}\
\
start = time_now();\
start_c = cycles_now();\
for (i = 0; i < iterations; ++i) {\
FUNCTION; \
}\
end = time_now();\
end_c = cycles_now();\
\
printf("%lu us, %g op/s, %lu cycles/op\n",\
(unsigned long) ((end - start) / iterations),\
iterations*1000000. / (end - start),\
(unsigned long)((end_c-start_c)/iterations) );\
}while(0)
#define oper_second(FUNCTION) oper_second_random(while(0),FUNCTION)
uint64_t time_now();
uint64_t cycles_now(void);
/* _CLOCKS_H_ */
#endif