-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheel.c
347 lines (323 loc) · 13.7 KB
/
wheel.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "wheel.h"
#define M0 ~(1 << 0)
#define M1 ~(1 << 1)
#define M2 ~(1 << 2)
#define M3 ~(1 << 3)
#define M4 ~(1 << 4)
#define M5 ~(1 << 5)
#define M6 ~(1 << 6)
#define M7 ~(1 << 7)
const u32 wheel[8] = { 1, 7, 11, 13, 17, 19, 23, 29 };
void init_prime(prime *p, u32 div, u32 mod, u32 step, u32 offset)
{
p->div = div;
p->mod = mod;
p->step = step;
p->offset = offset;
}
void mark_multiples(prime *p, u8 *sieve, u32 sieve_size)
{
u32 i, i2, i4, i6;
u32 interval;
u32 limit;
i = p->offset;
if (i >= sieve_size)
goto out;
i2 = p->div * 2;
i4 = i2 * 2;
i6 = i4 + i2;
interval = p->div * 30 + wheel[p->mod] - (i2 + 1);
limit = (interval >= sieve_size) ? 0 : sieve_size - interval;
switch (p->mod * 8 + p->step) {
case 0: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M0; i += i6;
case 1: if (i >= sieve_size)
goto s1;
sieve[i] &= M1; i += i4;
case 2: if (i >= sieve_size)
goto s2;
sieve[i] &= M2; i += i2;
case 3: if (i >= sieve_size)
goto s3;
sieve[i] &= M3; i += i4;
case 4: if (i >= sieve_size)
goto s4;
sieve[i] &= M4; i += i2;
case 5: if (i >= sieve_size)
goto s5;
sieve[i] &= M5; i += i4;
case 6: if (i >= sieve_size)
goto s6;
sieve[i] &= M6; i += i6;
case 7: if (i >= sieve_size)
goto s7;
sieve[i] &= M7; i += i2 + 1;
while (i < limit) {
sieve[i] &= M0; i += i6;
sieve[i] &= M1; i += i4;
sieve[i] &= M2; i += i2;
sieve[i] &= M3; i += i4;
sieve[i] &= M4; i += i2;
sieve[i] &= M5; i += i4;
sieve[i] &= M6; i += i6;
sieve[i] &= M7; i += i2 + 1;
}
} while (1);
case 8: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M1; i += i6 + 1;
case 9: if (i >= sieve_size)
goto s1;
sieve[i] &= M5; i += i4 + 1;
case 10: if (i >= sieve_size)
goto s2;
sieve[i] &= M4; i += i2 + 1;
case 11: if (i >= sieve_size)
goto s3;
sieve[i] &= M0; i += i4;
case 12: if (i >= sieve_size)
goto s4;
sieve[i] &= M7; i += i2 + 1;
case 13: if (i >= sieve_size)
goto s5;
sieve[i] &= M3; i += i4 + 1;
case 14: if (i >= sieve_size)
goto s6;
sieve[i] &= M2; i += i6 + 1;
case 15: if (i >= sieve_size)
goto s7;
sieve[i] &= M6; i += i2 + 1;
while (i < limit) {
sieve[i] &= M1; i += i6 + 1;
sieve[i] &= M5; i += i4 + 1;
sieve[i] &= M4; i += i2 + 1;
sieve[i] &= M0; i += i4;
sieve[i] &= M7; i += i2 + 1;
sieve[i] &= M3; i += i4 + 1;
sieve[i] &= M2; i += i6 + 1;
sieve[i] &= M6; i += i2 + 1;
}
} while (1);
case 16: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M2; i += i6 + 2;
case 17: if (i >= sieve_size)
goto s1;
sieve[i] &= M4; i += i4 + 2;
case 18: if (i >= sieve_size)
goto s2;
sieve[i] &= M0; i += i2;
case 19: if (i >= sieve_size)
goto s3;
sieve[i] &= M6; i += i4 + 2;
case 20: if (i >= sieve_size)
goto s4;
sieve[i] &= M1; i += i2;
case 21: if (i >= sieve_size)
goto s5;
sieve[i] &= M7; i += i4 + 2;
case 22: if (i >= sieve_size)
goto s6;
sieve[i] &= M3; i += i6 + 2;
case 23: if (i >= sieve_size)
goto s7;
sieve[i] &= M5; i += i2 + 1;
while (i < limit) {
sieve[i] &= M2; i += i6 + 2;
sieve[i] &= M4; i += i4 + 2;
sieve[i] &= M0; i += i2;
sieve[i] &= M6; i += i4 + 2;
sieve[i] &= M1; i += i2;
sieve[i] &= M7; i += i4 + 2;
sieve[i] &= M3; i += i6 + 2;
sieve[i] &= M5; i += i2 + 1;
}
} while (1);
case 24: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M3; i += i6 + 3;
case 25: if (i >= sieve_size)
goto s1;
sieve[i] &= M0; i += i4 + 1;
case 26: if (i >= sieve_size)
goto s2;
sieve[i] &= M6; i += i2 + 1;
case 27: if (i >= sieve_size)
goto s3;
sieve[i] &= M5; i += i4 + 2;
case 28: if (i >= sieve_size)
goto s4;
sieve[i] &= M2; i += i2 + 1;
case 29: if (i >= sieve_size)
goto s5;
sieve[i] &= M1; i += i4 + 1;
case 30: if (i >= sieve_size)
goto s6;
sieve[i] &= M7; i += i6 + 3;
case 31: if (i >= sieve_size)
goto s7;
sieve[i] &= M4; i += i2 + 1;
while (i < limit) {
sieve[i] &= M3; i += i6 + 3;
sieve[i] &= M0; i += i4 + 1;
sieve[i] &= M6; i += i2 + 1;
sieve[i] &= M5; i += i4 + 2;
sieve[i] &= M2; i += i2 + 1;
sieve[i] &= M1; i += i4 + 1;
sieve[i] &= M7; i += i6 + 3;
sieve[i] &= M4; i += i2 + 1;
}
} while (1);
case 32: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M4; i += i6 + 3;
case 33: if (i >= sieve_size)
goto s1;
sieve[i] &= M7; i += i4 + 3;
case 34: if (i >= sieve_size)
goto s2;
sieve[i] &= M1; i += i2 + 1;
case 35: if (i >= sieve_size)
goto s3;
sieve[i] &= M2; i += i4 + 2;
case 36: if (i >= sieve_size)
goto s4;
sieve[i] &= M5; i += i2 + 1;
case 37: if (i >= sieve_size)
goto s5;
sieve[i] &= M6; i += i4 + 3;
case 38: if (i >= sieve_size)
goto s6;
sieve[i] &= M0; i += i6 + 3;
case 39: if (i >= sieve_size)
goto s7;
sieve[i] &= M3; i += i2 + 1;
while (i < limit) {
sieve[i] &= M4; i += i6 + 3;
sieve[i] &= M7; i += i4 + 3;
sieve[i] &= M1; i += i2 + 1;
sieve[i] &= M2; i += i4 + 2;
sieve[i] &= M5; i += i2 + 1;
sieve[i] &= M6; i += i4 + 3;
sieve[i] &= M0; i += i6 + 3;
sieve[i] &= M3; i += i2 + 1;
}
} while (1);
case 40: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M5; i += i6 + 4;
case 41: if (i >= sieve_size)
goto s1;
sieve[i] &= M3; i += i4 + 2;
case 42: if (i >= sieve_size)
goto s2;
sieve[i] &= M7; i += i2 + 2;
case 43: if (i >= sieve_size)
goto s3;
sieve[i] &= M1; i += i4 + 2;
case 44: if (i >= sieve_size)
goto s4;
sieve[i] &= M6; i += i2 + 2;
case 45: if (i >= sieve_size)
goto s5;
sieve[i] &= M0; i += i4 + 2;
case 46: if (i >= sieve_size)
goto s6;
sieve[i] &= M4; i += i6 + 4;
case 47: if (i >= sieve_size)
goto s7;
sieve[i] &= M2; i += i2 + 1;
while (i < limit) {
sieve[i] &= M5; i += i6 + 4;
sieve[i] &= M3; i += i4 + 2;
sieve[i] &= M7; i += i2 + 2;
sieve[i] &= M1; i += i4 + 2;
sieve[i] &= M6; i += i2 + 2;
sieve[i] &= M0; i += i4 + 2;
sieve[i] &= M4; i += i6 + 4;
sieve[i] &= M2; i += i2 + 1;
}
} while (1);
case 48: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M6; i += i6 + 5;
case 49: if (i >= sieve_size)
goto s1;
sieve[i] &= M2; i += i4 + 3;
case 50: if (i >= sieve_size)
goto s2;
sieve[i] &= M3; i += i2 + 1;
case 51: if (i >= sieve_size)
goto s3;
sieve[i] &= M7; i += i4 + 4;
case 52: if (i >= sieve_size)
goto s4;
sieve[i] &= M0; i += i2 + 1;
case 53: if (i >= sieve_size)
goto s5;
sieve[i] &= M4; i += i4 + 3;
case 54: if (i >= sieve_size)
goto s6;
sieve[i] &= M5; i += i6 + 5;
case 55: if (i >= sieve_size)
goto s7;
sieve[i] &= M1; i += i2 + 1;
while (i < limit) {
sieve[i] &= M6; i += i6 + 5;
sieve[i] &= M2; i += i4 + 3;
sieve[i] &= M3; i += i2 + 1;
sieve[i] &= M7; i += i4 + 4;
sieve[i] &= M0; i += i2 + 1;
sieve[i] &= M4; i += i4 + 3;
sieve[i] &= M5; i += i6 + 5;
sieve[i] &= M1; i += i2 + 1;
}
} while (1);
case 56: do { if (i >= sieve_size)
goto s0;
sieve[i] &= M7; i += i6 + 6;
case 57: if (i >= sieve_size)
goto s1;
sieve[i] &= M6; i += i4 + 4;
case 58: if (i >= sieve_size)
goto s2;
sieve[i] &= M5; i += i2 + 2;
case 59: if (i >= sieve_size)
goto s3;
sieve[i] &= M4; i += i4 + 4;
case 60: if (i >= sieve_size)
goto s4;
sieve[i] &= M3; i += i2 + 2;
case 61: if (i >= sieve_size)
goto s5;
sieve[i] &= M2; i += i4 + 4;
case 62: if (i >= sieve_size)
goto s6;
sieve[i] &= M1; i += i6 + 6;
case 63: if (i >= sieve_size)
goto s7;
sieve[i] &= M0; i += i2 + 1;
while (i < limit) {
sieve[i] &= M7; i += i6 + 6;
sieve[i] &= M6; i += i4 + 4;
sieve[i] &= M5; i += i2 + 2;
sieve[i] &= M4; i += i4 + 4;
sieve[i] &= M3; i += i2 + 2;
sieve[i] &= M2; i += i4 + 4;
sieve[i] &= M1; i += i6 + 6;
sieve[i] &= M0; i += i2 + 1;
}
} while (1);
}
s0: p->step = 0; goto out;
s1: p->step = 1; goto out;
s2: p->step = 2; goto out;
s3: p->step = 3; goto out;
s4: p->step = 4; goto out;
s5: p->step = 5; goto out;
s6: p->step = 6; goto out;
s7: p->step = 7; goto out;
out:
p->offset = i - sieve_size;
}