-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.c
270 lines (233 loc) · 9.17 KB
/
segment.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
#include "segment.h"
#include <stdlib.h>
#include <string.h>
#include "magic.h"
#include "utils.h"
#include "wheel.h"
static const u32 step_finder[30] =
{
0, 0, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
6, 6, 6, 6, 7, 7, 7, 7, 7, 7
};
static const u32 square_finder[8] =
{
0, /*0, 0, 0, 0, 0, 0, 0,*/
/*0,*/ 1, /*2, 3, 3, 4, 5, 6,*/
/*0, 2,*/ 4, /*4, 6, 6, 8, 10,*/
/*0, 3, 4,*/ 5, /*7, 8, 9, 12,*/
/*0, 3, 6, 7,*/ 9,/*10, 13, 16,*/
/*0, 4, 6, 8, 10,*/12,/*14, 18,*/
/*0, 5, 8, 9, 13, 14,*/17,/*22,*/
/*0, 6, 10, 12, 16, 18, 22,*/28
};
static const s8 set_bits[256][8] =
{
{-1}, {0,-1}, {1,-1}, {0,1,-1},
{2,-1}, {0,2,-1}, {1,2,-1}, {0,1,2,-1},
{3,-1}, {0,3,-1}, {1,3,-1}, {0,1,3,-1},
{2,3,-1}, {0,2,3,-1}, {1,2,3,-1}, {0,1,2,3,-1},
{4,-1}, {0,4,-1}, {1,4,-1}, {0,1,4,-1},
{2,4,-1}, {0,2,4,-1}, {1,2,4,-1}, {0,1,2,4,-1},
{3,4,-1}, {0,3,4,-1}, {1,3,4,-1}, {0,1,3,4,-1},
{2,3,4,-1}, {0,2,3,4,-1}, {1,2,3,4,-1}, {0,1,2,3,4,-1},
{5,-1}, {0,5,-1}, {1,5,-1}, {0,1,5,-1},
{2,5,-1}, {0,2,5,-1}, {1,2,5,-1}, {0,1,2,5,-1},
{3,5,-1}, {0,3,5,-1}, {1,3,5,-1}, {0,1,3,5,-1},
{2,3,5,-1}, {0,2,3,5,-1}, {1,2,3,5,-1}, {0,1,2,3,5,-1},
{4,5,-1}, {0,4,5,-1}, {1,4,5,-1}, {0,1,4,5,-1},
{2,4,5,-1}, {0,2,4,5,-1}, {1,2,4,5,-1}, {0,1,2,4,5,-1},
{3,4,5,-1}, {0,3,4,5,-1}, {1,3,4,5,-1}, {0,1,3,4,5,-1},
{2,3,4,5,-1}, {0,2,3,4,5,-1}, {1,2,3,4,5,-1}, {0,1,2,3,4,5,-1},
{6,-1}, {0,6,-1}, {1,6,-1}, {0,1,6,-1},
{2,6,-1}, {0,2,6,-1}, {1,2,6,-1}, {0,1,2,6,-1},
{3,6,-1}, {0,3,6,-1}, {1,3,6,-1}, {0,1,3,6,-1},
{2,3,6,-1}, {0,2,3,6,-1}, {1,2,3,6,-1}, {0,1,2,3,6,-1},
{4,6,-1}, {0,4,6,-1}, {1,4,6,-1}, {0,1,4,6,-1},
{2,4,6,-1}, {0,2,4,6,-1}, {1,2,4,6,-1}, {0,1,2,4,6,-1},
{3,4,6,-1}, {0,3,4,6,-1}, {1,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,-1},
{4,5,6,7,-1}, {0,4,5,6,7,-1}, {1,4,5,6,7,-1}, {0,1,4,5,6,7,-1},
{2,4,5,6,7,-1}, {0,2,4,5,6,7,-1}, {1,2,4,5,6,7,-1}, {0,1,2,4,5,6,7,-1},
{3,4,5,6,7,-1}, {0,3,4,5,6,7,-1}, {1,3,4,5,6,7,-1}, {0,1,3,4,5,6,7,-1},
{2,3,4,5,6,7,-1}, {0,2,3,4,5,6,7,-1},{1,2,3,4,5,6,7,-1},{/*0,1,...,7,*/-1}
};
static u32 find_square(u32 div, u32 mod)
{
return div * (div * 30 + wheel[mod] * 2) + square_finder[mod];
}
segment *segment_new(u32 max_size)
{
segment *s;
s = ez_malloc(sizeof(*s));
s->data = ez_malloc(max_size);
return s;
}
segment *segment_bootstrap(u32 upper, u8 *magic, u32 magic_size)
{
prime p;
segment *s;
u32 size;
u32 i, j;
u32 val;
s32 pos;
u32 square;
size = (upper - 1) / 30 + 1;
s = segment_new(size);
segment_init(s, magic, magic_size, 0, size);
s->data[0] &= ~1; /* Marks 1 as not prime. */
/*
* If a mask is used, the mask primes will be marked as not prime and will
* not be extracted. This is good since it saves us the trouble of having
* to skip those primes later.
*/
for (i = 0; i < size; ++i) {
val = s->data[i];
if (val == 0)
continue;
pos = set_bits[val][0];
j = 1;
do {
square = find_square(i, pos);
if (square >= size)
return s;
init_prime(&p, i, pos, pos, square);
mark_multiples(&p, s->data, size);
pos = set_bits[val][j++];
} while (pos != -1);
}
return s;
}
void segment_init(segment *s,
u8 *magic,
u32 magic_size,
u64 lower_div,
u64 upper_div)
{
s->size = upper_div - lower_div;
s->lower = lower_div * 30;
s->lower_div = lower_div;
s->upper = upper_div * 30;
s->upper_div = upper_div;
if (magic)
magic_apply(magic, magic_size, s->data, s->size, lower_div);
else
memset(s->data, 0xff, s->size);
}
void segment_sieve(segment *s, u32 *primes, u32 primes_size)
{
prime p;
u32 n;
u64 intervals;
u32 remainder;
u64 next;
u32 i;
for (i = 0; i < primes_size; ++i) {
n = primes[i];
/* This assumes the primes are listed in ascending order. */
if ((u64)n * n >= s->upper)
return;
/* Computes the next wheel-aligned multiple >= n^2. */
intervals = (s->lower - 1) / n + 1;
intervals = MAX(intervals, n);
remainder = intervals % 30;
p.step = step_finder[remainder];
intervals += wheel[p.step] - remainder;
next = intervals * n;
/* The second condition handles overflow. */
if (next >= s->upper || next < s->lower)
continue;
p.div = n / 30;
p.mod = step_finder[n % 30];
p.offset = (next - s->lower) / 30;
mark_multiples(&p, s->data, s->size);
}
}
void segment_trim_lower(segment *s, u64 lower)
{
u32 start;
u32 i;
if (s->size == 0)
return;
start = step_finder[lower % 30];
for (i = 0; i < start; ++i)
s->data[0] &= ~(1 << i);
}
void segment_trim_upper(segment *s, u64 upper)
{
u32 end;
u32 i;
if (s->size == 0)
return;
end = step_finder[(upper + 1) % 30];
if (end == 0)
return;
for (i = end; i < 8; ++i)
s->data[s->size - 1] &= ~(1 << i);
}
void segment_extract(segment *s, callback cb, void *ctx)
{
u32 i, j;
u32 val;
s32 pos;
/*
* This should not be used on a segment with 0xff bytes (not an issue if
* the segment was sieved).
*/
for (i = 0; i < s->size; ++i) {
val = s->data[i];
if (val == 0)
continue;
pos = set_bits[val][0];
j = 1;
do {
cb(ctx, (s->lower_div + i) * 30 + wheel[pos]);
pos = set_bits[val][j++];
} while (pos != -1);
}
}
u32 segment_count(segment *s)
{
return popcount(s->data, s->size);
}
void segment_free(segment *s)
{
free(s->data);
free(s);
}