-
Notifications
You must be signed in to change notification settings - Fork 9
/
gcd_iter.c
199 lines (168 loc) · 5.38 KB
/
gcd_iter.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//------------------------------------------------------------------------------
// gcd_iter libjit sample. Iterative version of gcd.c
//
// Eli Bendersky (eliben@gmail.com)
// This code is in the public domain
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <jit/jit.h>
// Native version for benchmarking
int __attribute__ ((noinline)) gcd_iter_native(int u, int v) {
int t;
while (v) {
t = u;
u = v;
v = t % v;
}
return u < 0 ? -u : u; /* abs(u) */
}
// Compute difference between two timespecs, as a timespec.
struct timespec timespec_diff(struct timespec start, struct timespec end) {
const long SEC = 1000 * 1000 * 1000; // 1 sec in ns
struct timespec diff;
if (end.tv_nsec < start.tv_nsec) {
diff.tv_sec = end.tv_sec - start.tv_sec - 1;
diff.tv_nsec = SEC + end.tv_nsec - start.tv_nsec;
} else {
diff.tv_sec = end.tv_sec - start.tv_sec;
diff.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return diff;
}
void print_diff(struct timespec start, struct timespec end) {
struct timespec diff = timespec_diff(start, end);
printf("Elapsed %ld . %ld\n", diff.tv_sec, diff.tv_nsec);
}
// Run a benchmark
void benchmark(jit_function_t jit_f) {
int n1 = 49979687;
int n2 = 982451653;
const int N = 5 * 1000 * 1000;
struct timespec t1, t2, d;
typedef int (*FF)(int, int);
int i, g;
// Arguments for the JIT invocation
void* args[] = {&n1, &n2};
jit_int result;
printf("Native\n");
clock_gettime(CLOCK_REALTIME, &t1);
for (i = 0; i < N; ++i) {
g = gcd_iter_native(n1, n2);
}
clock_gettime(CLOCK_REALTIME, &t2);
print_diff(t1, t2);
printf("\nJIT apply\n");
clock_gettime(CLOCK_REALTIME, &t1);
for (i = 0; i < N; ++i) {
jit_function_apply(jit_f, args, &result);
}
clock_gettime(CLOCK_REALTIME, &t2);
print_diff(t1, t2);
printf("\nJIT closure\n");
FF clos = jit_function_to_closure(jit_f);
clock_gettime(CLOCK_REALTIME, &t1);
for (i = 0; i < N; ++i) {
g = clos(n1, n2);
}
clock_gettime(CLOCK_REALTIME, &t2);
print_diff(t1, t2);
}
// Builds a function that performs the iterative GCD computation, implementing
// the following C code:
//
// int gcd_iter(int u, int v) {
// int t;
// while (v) {
// t = u;
// u = v;
// v = t % v;
// }
// return u < 0 ? -u : u; /* abs(u) */
// }
//
// Returns an uncompiled jit_function_t.
jit_function_t build_gcd_func(jit_context_t context) {
jit_context_build_start(context);
// Create function signature and object. int (*)(int, int)
jit_type_t params[2] = {jit_type_int, jit_type_int};
jit_type_t signature = jit_type_create_signature(
jit_abi_cdecl, jit_type_int, params, 2, 1);
jit_function_t F = jit_function_create(context, signature);
// u, v are function parameters; t is a temporary value.
jit_value_t u, v, t;
u = jit_value_get_param(F, 0);
v = jit_value_get_param(F, 1);
t = jit_value_create(F, jit_type_int);
// Create the while (v) condition with a label that allows to loop back.
//
// label_while:
// if (v == 0) goto label_after_while
// .. contents of while loop
//
// label_after_while is created as undefined at this point, so that
// instructions can have forward references to it. It will be placed later.
jit_label_t label_while = jit_label_undefined;
jit_label_t label_after_while = jit_label_undefined;
jit_value_t const0 = jit_value_create_nint_constant(F, jit_type_int, 0);
jit_insn_label(F, &label_while);
jit_value_t cmp_v_0 = jit_insn_eq(F, v, const0);
jit_insn_branch_if(F, cmp_v_0, &label_after_while);
// t = u
jit_insn_store(F, t, u);
// u = v
jit_insn_store(F, u, v);
// v = t % v
jit_value_t rem = jit_insn_rem(F, t, v);
jit_insn_store(F, v, rem);
// goto label_while
// label_after_while:
// ...
jit_insn_branch(F, &label_while);
jit_insn_label(F, &label_after_while);
// if (u >= 0) goto label_positive
// return -u
// label_pos:
// return u
jit_label_t label_positive = jit_label_undefined;
jit_value_t cmp_u_0 = jit_insn_ge(F, u, const0);
jit_insn_branch_if(F, cmp_u_0, &label_positive);
jit_value_t minus_u = jit_insn_neg(F, u);
jit_insn_return(F, minus_u);
jit_insn_label(F, &label_positive);
jit_insn_return(F, u);
jit_context_build_end(context);
return F;
}
int main(int argc, char** argv) {
jit_init();
jit_context_t context = jit_context_create();
jit_function_t gcd = build_gcd_func(context);
// This will dump the uncompiled function, showing libjit opcodes
jit_dump_function(stdout, gcd, "gcd [uncompiled]");
printf("Optimization level: %u\n", jit_function_get_optimization_level(gcd));
// Compile (JIT) the function to machine code
jit_context_build_start(context);
jit_function_compile(gcd);
jit_context_build_end(context);
// This will dump the disassembly of the machine code for the function
jit_dump_function(stdout, gcd, "gcd [compiled]");
// Run the function on argv input
if (argc > 2) {
int u = atoi(argv[1]);
int v = atoi(argv[2]);
void* args[2] = {&u, &v};
printf("gcd(%d, %d) --> ", u, v);
jit_int result;
jit_function_apply(gcd, args, &result);
/*typedef int (*FF)(int, int);*/
/*FF gcd_f = jit_function_to_closure(gcd);*/
/*int result = gcd_f(u, v);*/
printf("%d\n", (int)result);
}
/*benchmark(gcd);*/
jit_context_destroy(context);
return 0;
}