-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_sieve.cpp
190 lines (139 loc) · 4.48 KB
/
test_sieve.cpp
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
#include "bjit.h"
#ifdef _WIN32
#pragma comment(lib,"winmm.lib")
#include <windows.h>
static inline unsigned getTimeMs()
{
return timeGetTime();
}
#else
#include <sys/time.h>
static inline unsigned getTimeMs()
{
timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000) + (time.tv_usec / 1000);
}
#endif
int sieve(char * flags, int size)
{
int count = 0;
for (int i = 0; i < size; ++i) flags[i] = true;
for (int i = 2; i < size; ++i)
{
if (flags[i])
{
int prime = i + 1;
int k = i + prime;
while (k < size)
{
flags[k] = false;
k += prime;
}
++count;
}
}
return count;
}
static char data[819000];
int main()
{
bjit::Module module;
{
bjit::Proc pr(0, "ii");
int _flags = 0;
int _size = 1;
// i = 0
int _i = pr.env.size(); pr.env.push_back(pr.lci(0));
// count = 0
int _count = pr.env.size(); pr.env.push_back(pr.lci(0));
auto ls0 = pr.newLabel();
auto lb0 = pr.newLabel();
auto le0 = pr.newLabel();
pr.jmp(ls0);
pr.emitLabel(ls0);
// while i < size
pr.jz(pr.ilt(pr.env[_i], pr.env[_size]), le0, lb0);
pr.emitLabel(lb0);
// *(flags + i) = 1
pr.si8(pr.lci(1), pr.iadd(pr.env[_flags], pr.env[_i]), 0);
// ++i
pr.env[_i] = pr.iadd(pr.env[_i], pr.lci(1));
pr.jmp(ls0);
pr.emitLabel(le0);
// i = 2
pr.env[_i] = pr.lci(2);
auto ls1 = pr.newLabel();
auto lb1 = pr.newLabel();
auto le1 = pr.newLabel();
pr.jmp(ls1);
pr.emitLabel(ls1);
// while i < size
pr.jz(pr.ilt(pr.env[_i], pr.env[_size]), le1, lb1);
pr.emitLabel(lb1);
auto bt = pr.newLabel();
auto be = pr.newLabel();
// if flags[i] != 0
pr.jnz(pr.li8(pr.iadd(pr.env[_flags], pr.env[_i]), 0), bt, be);
pr.emitLabel(bt);
// prime = i + 1
int _prime = pr.env.size();
pr.env.push_back(pr.iadd(pr.env[_i], pr.lci(1)));
// k = i + prim
int _k = pr.env.size();
pr.env.push_back(pr.iadd(pr.env[_i], pr.env[_prime]));
auto ls2 = pr.newLabel();
auto lb2 = pr.newLabel();
auto le2 = pr.newLabel();
pr.jmp(ls2);
pr.emitLabel(ls2);
// while k < size
pr.jnz(pr.ilt(pr.env[_k], pr.env[_size]), lb2, le2);
pr.emitLabel(lb2);
// flags[k] = false;
pr.si8(pr.lci(0), pr.iadd(pr.env[_flags], pr.env[_k]), 0);
// k = k + prime
pr.env[_k] = pr.iadd(pr.env[_k], pr.env[_prime]);
pr.jmp(ls2);
pr.emitLabel(le2);
pr.env.pop_back(); // k
pr.env.pop_back(); // prime
pr.env[_count] = pr.iadd(pr.env[_count], pr.lci(1));
pr.jmp(be);
pr.emitLabel(be);
// ++i
pr.env[_i] = pr.iadd(pr.env[_i], pr.lci(1));
pr.jmp(ls1);
pr.emitLabel(le1);
pr.iret(pr.env[_count]);
pr.debug();
module.compile(pr);
}
auto & codeOut = module.getBytes();
FILE * f = fopen("out.bin", "wb");
fwrite(codeOut.data(), 1, codeOut.size(), f);
fclose(f);
printf(" - Wrote out.bin\n");
BJIT_ASSERT(module.load());
auto proc = module.getPointer<int(char*,int)>(0);
printf("C-sieve: %d primes\n", sieve(data, sizeof(data)));
printf("BJIT-sieve: %d primes\n", proc(data, sizeof(data)));
BJIT_ASSERT(sieve(data, sizeof(data)) == proc(data, sizeof(data)));
printf("Iterating 1000 times...\n");
{
auto start = getTimeMs();
for(int i = 0; i < 1000; ++i)
{
sieve(data, sizeof(data));
}
printf("C time: %dms\n", getTimeMs() - start);
}
{
auto start = getTimeMs();
for(int i = 0; i < 1000; ++i)
{
proc(data, sizeof(data));
}
printf("BJIT time: %dms\n", getTimeMs() - start);
}
}