forked from kmowery/libfixedtimefixedpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf_test.c
113 lines (96 loc) · 2.97 KB
/
perf_test.c
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
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include "ftfp.h"
#define PERF_ITRS 2000000
static inline uint64_t rdtscp(){
uint64_t v;
__asm__ volatile("rdtscp;"
"shl $32,%%rdx;"
"or %%rdx,%%rax;"
: "=a" (v)
:
: "%rcx","%rdx");
return v;
}
#define TEST_INTERNALS( code ) \
int ctr = 0; \
uint64_t st = rdtscp(); \
/* Offset for running the loop and rdtscp */ \
for(ctr=0;ctr<PERF_ITRS;ctr++){ \
} \
uint64_t end = rdtscp(); \
uint64_t offset = end-st; \
\
st = rdtscp(); \
for(ctr=0;ctr<PERF_ITRS;ctr++){ \
code; \
} \
end = rdtscp(); \
\
/* Run everything for real, previous was just warmup */ \
st = rdtscp(); \
for(ctr=0;ctr<PERF_ITRS;ctr++){ \
} \
end = rdtscp(); \
offset = end-st; \
\
st = rdtscp(); \
for(ctr=0;ctr<PERF_ITRS;ctr++){ \
code; \
} \
end = rdtscp(); \
printf("%s %" PRIu64 "\n",name,(end-st-offset)/PERF_ITRS);
void run_test_d(char* name, fixed (*function) (fixed,fixed), fixed a, fixed b){
TEST_INTERNALS( (*function)(a,b) );
}
void run_test_s(char* name, fixed (*function) (fixed), fixed a){
TEST_INTERNALS( (*function)(a) );
}
void run_test_p(char* name, void (*function) (char*,fixed), fixed a){
char buf[100];
TEST_INTERNALS( (*function)(buf, a); )
}
void run_test_sb(char* name, int8_t (*function) (fixed), fixed a){
TEST_INTERNALS( (*function)(a); )
}
void run_test_db(char* name, int8_t (*function) (fixed,fixed), fixed a, fixed b){
TEST_INTERNALS( (*function)(a, b); )
}
int main(int argc, char* argv[]){
printf( "function "" cycles\n");
printf( "=================\n");
run_test_s ("fix_neg ",fix_neg,10);
run_test_s ("fix_abs ",fix_abs,10);
run_test_sb("fix_is_neg ",fix_is_neg,10);
run_test_sb("fix_is_nan ",fix_is_nan,10);
run_test_sb("fix_is_inf_pos ",fix_is_nan,10);
run_test_sb("fix_is_inf_neg ",fix_is_nan,10);
run_test_db("fix_eq ",fix_eq,10,10);
run_test_db("fix_eq_nan ",fix_eq_nan,10,10);
run_test_db("fix_cmp ",fix_cmp,10,10);
printf("\n");
run_test_d ("fix_add ",fix_add,0,0);
run_test_d ("fix_sub ",fix_sub,0,0);
run_test_d ("fix_mul ",fix_mul,0,0);
run_test_d ("fix_div ",fix_div,0,0);
printf("\n");
run_test_s ("fix_floor ",fix_floor,10);
run_test_s ("fix_ceil ",fix_ceil,10);
printf("\n");
run_test_s ("fix_exp ",fix_exp,10);
run_test_s ("fix_ln ",fix_ln,10);
run_test_s ("fix_log2 ",fix_log2,10);
run_test_s ("fix_log10 ",fix_log10,10);
printf("\n");
run_test_s ("fix_sqrt ",fix_sqrt,10);
run_test_d ("fix_pow ",fix_pow,10,10);
printf("\n");
run_test_s ("fix_sin ",fix_sin,10);
run_test_s ("fix_cos ",fix_cos,10);
run_test_s ("fix_tan ",fix_tan,10);
//run_test_s ("fix_sin_fast ",fix_sin_fast,10);
printf("\n");
run_test_p ("fix_sprint ",fix_sprint,10);
}