Line data Source code
1 : /* Copyright (C) 2016 The PARI group.
2 :
3 : This file is part of the PARI/GP package.
4 :
5 : PARI/GP is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 :
15 : #include "pari.h"
16 : #include "paripriv.h"
17 :
18 : /**********************************************************************/
19 : /*** ***/
20 : /*** Public prime table ***/
21 : /*** ***/
22 : /**********************************************************************/
23 :
24 : static ulong _maxprimelim = 0;
25 : static GEN _prodprimes, _prodprimeslim;
26 : typedef unsigned char *byteptr;
27 :
28 : /* Build/Rebuild table of prime differences. The actual work is done by the
29 : * following two subroutines; the user entry point is the function
30 : * initprimes() below; initprimes1() is the basecase, called when
31 : * maxnum (size) is moderate. Must be called after pari_init_stack() )*/
32 : static void
33 1883 : initprimes1(ulong size, long *lenp, pari_prime *p1)
34 : {
35 1883 : pari_sp av = avma;
36 : long k;
37 1883 : byteptr q, r, s, p = (byteptr)stack_calloc(size+2), fin = p + size;
38 : pari_prime *re;
39 :
40 22596 : for (r=q=p,k=1; r<=fin; )
41 : {
42 33894 : do { r+=k; k+=2; r+=k; } while (*++q);
43 909489 : for (s=r; s<=fin; s+=k) *s = 1;
44 : }
45 1883 : re = p1; *re++ = 2; *re++ = 3; /* 2 and 3 */
46 1883 : for (s=q=p+1; ; s=q)
47 : {
48 962213 : do q++; while (*q);
49 321993 : if (q > fin) break;
50 320110 : *re++ = (pari_prime) 2*(q-p)+1;
51 : }
52 1883 : *re++ = 0;
53 1883 : *lenp = re - p1;
54 1883 : set_avma(av);
55 1883 : }
56 :
57 : /* Timing in ms (Athlon/850; reports 512K of secondary cache; looks
58 : like there is 64K of quickier cache too).
59 :
60 : arena| 30m 100m 300m 1000m 2000m <-- primelimit
61 : =================================================
62 : 16K 1.1053 1.1407 1.2589 1.4368 1.6086
63 : 24K 1.0000 1.0625 1.1320 1.2443 1.3095
64 : 32K 1.0000 1.0469 1.0761 1.1336 1.1776
65 : 48K 1.0000 1.0000 1.0254 1.0445 1.0546
66 : 50K 1.0000 1.0000 1.0152 1.0345 1.0464
67 : 52K 1.0000 1.0000 1.0203 1.0273 1.0362
68 : 54K 1.0000 1.0000 1.0812 1.0216 1.0281
69 : 56K 1.0526 1.0000 1.0051 1.0144 1.0205
70 : 58K 1.0000 1.0000 1.0000 1.0086 1.0123
71 : 60K 0.9473 0.9844 1.0051 1.0014 1.0055
72 : 62K 1.0000 0.9844 0.9949 0.9971 0.9993
73 : 64K 1.0000 1.0000 1.0000 1.0000 1.0000
74 : 66K 1.2632 1.2187 1.2183 1.2055 1.1953
75 : 68K 1.4211 1.4844 1.4721 1.4425 1.4188
76 : 70K 1.7368 1.7188 1.7107 1.6767 1.6421
77 : 72K 1.9474 1.9531 1.9594 1.9023 1.8573
78 : 74K 2.2105 2.1875 2.1827 2.1207 2.0650
79 : 76K 2.4211 2.4219 2.4010 2.3305 2.2644
80 : 78K 2.5789 2.6250 2.6091 2.5330 2.4571
81 : 80K 2.8421 2.8125 2.8223 2.7213 2.6380
82 : 84K 3.1053 3.1875 3.1776 3.0819 2.9802
83 : 88K 3.5263 3.5312 3.5228 3.4124 3.2992
84 : 92K 3.7895 3.8438 3.8375 3.7213 3.5971
85 : 96K 4.0000 4.1093 4.1218 3.9986 3.9659
86 : 112K 4.3684 4.5781 4.5787 4.4583 4.6115
87 : 128K 4.7368 4.8750 4.9188 4.8075 4.8997
88 : 192K 5.5263 5.7188 5.8020 5.6911 5.7064
89 : 256K 6.0000 6.2187 6.3045 6.1954 6.1033
90 : 384K 6.7368 6.9531 7.0405 6.9181 6.7912
91 : 512K 7.3158 7.5156 7.6294 7.5000 7.4654
92 : 768K 9.1579 9.4531 9.6395 9.5014 9.1075
93 : 1024K 10.368 10.7497 10.9999 10.878 10.8201
94 : 1536K 12.579 13.3124 13.7660 13.747 13.4739
95 : 2048K 13.737 14.4839 15.0509 15.151 15.1282
96 : 3076K 14.789 15.5780 16.2993 16.513 16.3365
97 :
98 : Now the same number relative to the model
99 :
100 : (1 + 0.36*sqrt(primelimit)/arena) * (arena <= 64 ? 1.05 : (arena-64)**0.38)
101 :
102 : [SLOW2_IN_ROOTS = 0.36, ALPHA = 0.38]
103 :
104 : arena| 30m 100m 300m 1000m 2000m <-- primelimit
105 : =================================================
106 : 16K 1.014 0.9835 0.9942 0.9889 1.004
107 : 24K 0.9526 0.9758 0.9861 0.9942 0.981
108 : 32K 0.971 0.9939 0.9884 0.9849 0.9806
109 : 48K 0.9902 0.9825 0.996 0.9945 0.9885
110 : 50K 0.9917 0.9853 0.9906 0.9926 0.9907
111 : 52K 0.9932 0.9878 0.9999 0.9928 0.9903
112 : 54K 0.9945 0.9902 1.064 0.9939 0.9913
113 : 56K 1.048 0.9924 0.9925 0.993 0.9921
114 : 58K 0.9969 0.9945 0.9909 0.9932 0.9918
115 : 60K 0.9455 0.9809 0.9992 0.9915 0.9923
116 : 62K 0.9991 0.9827 0.9921 0.9924 0.9929
117 : 64K 1 1 1 1 1
118 : 66K 1.02 0.9849 0.9857 0.9772 0.9704
119 : 68K 0.8827 0.9232 0.9176 0.9025 0.8903
120 : 70K 0.9255 0.9177 0.9162 0.9029 0.8881
121 : 72K 0.9309 0.936 0.9429 0.9219 0.9052
122 : 74K 0.9715 0.9644 0.967 0.9477 0.9292
123 : 76K 0.9935 0.9975 0.9946 0.9751 0.9552
124 : 78K 0.9987 1.021 1.021 1.003 0.9819
125 : 80K 1.047 1.041 1.052 1.027 1.006
126 : 84K 1.052 1.086 1.092 1.075 1.053
127 : 88K 1.116 1.125 1.133 1.117 1.096
128 : 92K 1.132 1.156 1.167 1.155 1.134
129 : 96K 1.137 1.177 1.195 1.185 1.196
130 : 112K 1.067 1.13 1.148 1.15 1.217
131 : 128K 1.04 1.083 1.113 1.124 1.178
132 : 192K 0.9368 0.985 1.025 1.051 1.095
133 : 256K 0.8741 0.9224 0.9619 0.995 1.024
134 : 384K 0.8103 0.8533 0.8917 0.9282 0.9568
135 : 512K 0.7753 0.8135 0.8537 0.892 0.935
136 : 768K 0.8184 0.8638 0.9121 0.9586 0.9705
137 : 1024K 0.8241 0.8741 0.927 0.979 1.03
138 : 1536K 0.8505 0.9212 0.9882 1.056 1.096
139 : 2048K 0.8294 0.8954 0.9655 1.041 1.102
140 :
141 : */
142 :
143 : #ifndef SLOW2_IN_ROOTS
144 : /* SLOW2_IN_ROOTS below 3: some slowdown starts to be noticable
145 : * when things fit into the cache on Sparc.
146 : * The choice of 2.6 gives a slowdown of 1-2% on UltraSparcII,
147 : * but makes calculations for "maximum" of 436273009
148 : * fit into 256K cache (still common for some architectures).
149 : *
150 : * One may change it when small caches become uncommon, but the gain
151 : * is not going to be very noticable... */
152 : # ifdef i386 /* gcc defines this? */
153 : # define SLOW2_IN_ROOTS 0.36
154 : # else
155 : # define SLOW2_IN_ROOTS 2.6
156 : # endif
157 : #endif
158 : #ifndef CACHE_ARENA
159 : # ifdef i386 /* gcc defines this? */
160 : /* Due to smaller SLOW2_IN_ROOTS, smaller arena is OK; fit L1 cache */
161 : # define CACHE_ARENA (63 * 1024UL) /* No slowdown even with 64K L1 cache */
162 : # else
163 : # define CACHE_ARENA (200 * 1024UL) /* No slowdown even with 256K L2 cache */
164 : # endif
165 : #endif
166 :
167 : #define CACHE_ALPHA (0.38) /* Cache performance model parameter */
168 : #define CACHE_CUTOFF (0.018) /* Cache performance not smooth here */
169 :
170 : static double slow2_in_roots = SLOW2_IN_ROOTS;
171 :
172 : typedef struct {
173 : ulong arena;
174 : double power;
175 : double cutoff;
176 : ulong bigarena;
177 : } cache_model_t;
178 :
179 : static cache_model_t cache_model = { CACHE_ARENA, CACHE_ALPHA, CACHE_CUTOFF, 0 };
180 :
181 : /* Assume that some calculation requires a chunk of memory to be
182 : accessed often in more or less random fashion (as in sieving).
183 : Assume that the calculation can be done in steps by subdividing the
184 : chunk into smaller subchunks (arenas) and treating them
185 : separately. Assume that the overhead of subdivision is equivalent
186 : to the number of arenas.
187 :
188 : Find an optimal size of the arena taking into account the overhead
189 : of subdivision, and the overhead of arena not fitting into the
190 : cache. Assume that arenas of size slow2_in_roots slows down the
191 : calculation 2x (comparing to very big arenas; when cache hits do
192 : not matter). Since cache performance varies wildly with
193 : architecture, load, and wheather (especially with cache coloring
194 : enabled), use an idealized cache model based on benchmarks above.
195 :
196 : Assume that an independent region of FIXED_TO_CACHE bytes is accessed
197 : very often concurrently with the arena access.
198 : */
199 : static ulong
200 1883 : good_arena_size(ulong slow2_size, ulong total, ulong fixed_to_cache,
201 : cache_model_t *cache_model)
202 : {
203 1883 : ulong asize, cache_arena = cache_model->arena;
204 : double Xmin, Xmax, A, B, C1, C2, D, V;
205 1883 : double alpha = cache_model->power, cut_off = cache_model->cutoff;
206 :
207 : /* Estimated relative slowdown,
208 : with overhead = max((fixed_to_cache+arena)/cache_arena - 1, 0):
209 :
210 : 1 + slow2_size/arena due to initialization overhead;
211 :
212 : max(1, 4.63 * overhead^0.38 ) due to footprint > cache size.
213 :
214 : [The latter is hard to substantiate theoretically, but this
215 : function describes benchmarks pretty close; it does not hurt that
216 : one can minimize it explicitly too ;-). The switch between
217 : different choices of max() happens when overhead=0.018.]
218 :
219 : Thus the problem is minimizing (1 + slow2_size/arena)*overhead**0.29.
220 : This boils down to F=((X+A)/(X+B))X^alpha, X=overhead,
221 : B = (1 - fixed_to_cache/cache_arena), A = B + slow2_size/cache_arena,
222 : alpha = 0.38, and X>=0.018, X>-B.
223 :
224 : We need to find the rightmost root of (X+A)*(X+B) - alpha(A-B)X to the
225 : right of 0.018 (if such exists and is below Xmax). Then we manually
226 : check the remaining region [0, 0.018].
227 :
228 : Since we cannot trust the purely-experimental cache-hit slowdown
229 : function, as a sanity check always prefer fitting into the
230 : cache (or "almost fitting") if F-law predicts that the larger
231 : value of the arena provides less than 10% speedup.
232 : */
233 :
234 : /* The simplest case: we fit into cache */
235 1883 : asize = cache_arena - fixed_to_cache;
236 1883 : if (total <= asize) return total;
237 : /* The simple case: fitting into cache doesn't slow us down more than 10% */
238 1883 : if (asize > 10 * slow2_size) return asize;
239 : /* Slowdown of not fitting into cache is significant. Try to optimize.
240 : Do not be afraid to spend some time on optimization - in trivial
241 : cases we do not reach this point; any gain we get should
242 : compensate the time spent on optimization. */
243 :
244 0 : B = (1 - ((double)fixed_to_cache)/cache_arena);
245 0 : A = B + ((double)slow2_size)/cache_arena;
246 0 : C2 = A*B;
247 0 : C1 = (A + B - 1/alpha*(A - B))/2;
248 0 : D = C1*C1 - C2;
249 0 : if (D > 0)
250 0 : V = cut_off*cut_off + 2*C1*cut_off + C2; /* Value at CUT_OFF */
251 : else
252 0 : V = 0; /* Peacify the warning */
253 0 : Xmin = cut_off;
254 0 : Xmax = ((double)total - fixed_to_cache)/cache_arena; /* Two candidates */
255 :
256 0 : if ( D <= 0 || (V >= 0 && C1 + cut_off >= 0) ) /* slowdown increasing */
257 0 : Xmax = cut_off; /* Only one candidate */
258 0 : else if (V >= 0 && /* slowdown concave down */
259 0 : ((Xmax + C1) <= 0 || (Xmax*Xmax + 2*C1*Xmax + C2) <= 0))
260 : /* DO NOTHING */; /* Keep both candidates */
261 0 : else if (V <= 0 && (Xmax*Xmax + 2*C1*Xmax + C2) <= 0) /*slowdown decreasing*/
262 0 : Xmin = cut_off; /* Only one candidate */
263 : else /* Now we know: 2 roots, the largest is in CUT_OFF..Xmax */
264 0 : Xmax = sqrt(D) - C1;
265 0 : if (Xmax != Xmin) { /* Xmin == CUT_OFF; Check which one is better */
266 0 : double v1 = (cut_off + A)/(cut_off + B);
267 0 : double v2 = 2.33 * (Xmax + A)/(Xmax + B) * pow(Xmax, alpha);
268 :
269 0 : if (1.1 * v2 >= v1) /* Prefer fitting into the cache if slowdown < 10% */
270 0 : V = v1;
271 : else
272 0 : { Xmin = Xmax; V = v2; }
273 0 : } else if (B > 0) /* We need V */
274 0 : V = 2.33 * (Xmin + A)/(Xmin + B) * pow(Xmin, alpha);
275 0 : if (B > 0 && 1.1 * V > A/B) /* Now Xmin is the minumum. Compare with 0 */
276 0 : Xmin = 0;
277 :
278 0 : asize = (ulong)((1 + Xmin)*cache_arena - fixed_to_cache);
279 0 : if (asize > total) asize = total; /* May happen due to approximations */
280 0 : return asize;
281 : }
282 :
283 : /* Use as in
284 : install(set_optimize,lLDG) \\ Through some M too?
285 : set_optimize(2,1) \\ disable dependence on limit
286 : \\ 1: how much cache usable, 2: slowdown of setup, 3: alpha, 4: cutoff,
287 : \\ 5: cache size (typically whole L2 or L3) in bytes to use in forprime()
288 : \\ 2,3,4 are in units of 0.001
289 :
290 : { time_primes_arena(ar,limit) = \\ ar = arena size in K
291 : set_optimize(1,floor(ar*1024));
292 : default(primelimit, 200 000); \\ 100000 results in *larger* malloc()!
293 : gettime;
294 : default(primelimit, floor(limit));
295 : if(ar >= 1, ar=floor(ar));
296 : print("arena "ar"K => "gettime"ms");
297 : }
298 : */
299 : long
300 0 : set_optimize(long what, GEN g)
301 : {
302 0 : long ret = 0;
303 :
304 0 : switch (what) {
305 0 : case 1:
306 0 : ret = (long)cache_model.arena;
307 0 : break;
308 0 : case 2:
309 0 : ret = (long)(slow2_in_roots * 1000);
310 0 : break;
311 0 : case 3:
312 0 : ret = (long)(cache_model.power * 1000);
313 0 : break;
314 0 : case 4:
315 0 : ret = (long)(cache_model.cutoff * 1000);
316 0 : break;
317 0 : case 5:
318 0 : ret = (long)(cache_model.bigarena);
319 0 : break;
320 0 : default:
321 0 : pari_err_BUG("set_optimize");
322 0 : break;
323 : }
324 0 : if (g != NULL) {
325 0 : ulong val = itou(g);
326 :
327 0 : switch (what) {
328 0 : case 1: cache_model.arena = val; break;
329 0 : case 2: slow2_in_roots = (double)val / 1000.; break;
330 0 : case 3: cache_model.power = (double)val / 1000.; break;
331 0 : case 4: cache_model.cutoff = (double)val / 1000.; break;
332 0 : case 5: cache_model.bigarena = val; break;
333 : }
334 : }
335 0 : return ret;
336 : }
337 :
338 : /* s is odd; prime (starting from 3 = known_primes[2]), terminated by a 0 byte.
339 : * Checks n odd numbers starting at 'start', setting bytes to 0 (composite)
340 : * or 1 (prime), starting at data */
341 : static void
342 7215 : sieve_chunk(pari_prime *known_primes, ulong s, byteptr data, ulong n)
343 : {
344 7215 : ulong p, cnt = n-1, start = s;
345 : pari_prime *q;
346 :
347 7215 : memset(data, 0, n);
348 7215 : start >>= 1; /* (start - 1)/2 */
349 7215 : start += n; /* Corresponds to the end */
350 : /* data corresponds to start, q runs over primediffs */
351 1036500 : for (q = known_primes + 1, p = 3; p; p = *++q)
352 : { /* first odd number >= start > p and divisible by p
353 : = last odd number <= start + 2p - 2 and 0 (mod p)
354 : = p + last number <= start + p - 2 and 0 (mod 2p)
355 : = p + start+p-2 - (start+p-2) % 2p
356 : = start + 2(p - 1 - ((start-1)/2 + (p-1)/2) % p). */
357 1029285 : long off = cnt - ((start+(p>>1)) % p);
358 1643745787 : while (off >= 0) { data[off] = 1; off -= p; }
359 : }
360 7215 : }
361 :
362 : static long
363 1883 : ZV_size(GEN x)
364 : {
365 1883 : long i, l = lg(x), s = l;
366 26362 : for (i = 1; i < l; i++) s += lgefint(gel(x,i));
367 1883 : return s;
368 : }
369 : /* avoid gcopy_avma so as to deallocate using free() in pari_close_primes */
370 : static GEN
371 1883 : ZV_copy_alloc(GEN x)
372 : {
373 1883 : long i, l = lg(x), s = ZV_size(x);
374 1883 : GEN z = (GEN)pari_malloc(s * sizeof(long)), av = z + s;
375 26362 : for (i = 1; i < l; i++) gel(z,i) = av = icopy_avma(gel(x,i), (pari_sp)av);
376 1883 : z[0] = x[0] & (TYPBITS|LGBITS); return z;
377 : }
378 :
379 : static void
380 1883 : set_prodprimes(void)
381 : {
382 1883 : pari_sp av = avma;
383 1883 : ulong b = 1UL << 8, M = minuu(maxprime(), GP_DATA->factorlimit);
384 1883 : GEN W, v = primes_interval_zv(3, M);
385 1883 : long u, j, jold, l = lg(v);
386 :
387 1883 : W = cgetg(64+1, t_VEC);
388 1883 : M = v[l-1]; /* = precprime(M) */
389 1883 : _prodprimeslim = cgetalloc(64+1, t_VECSMALL);
390 1883 : if (b > M) b = M;
391 154453075 : for (jold = j = u = 1; j < l; j++)
392 154451192 : if (uel(v,j) >= b) /* if j = l-1, then b = M = v[j] */
393 : {
394 24479 : long lw = (j == l-1? l: j) - jold + 1;
395 24479 : GEN w = v+jold-1;
396 24479 : w[0] = evaltyp(t_VECSMALL) | _evallg(lw);
397 24479 : _prodprimeslim[u] = w[lw - 1];
398 : /* product of primes from
399 : * p_jold = 3 if first entry, else nextprime(_prodprime_lim[u - 1] + 1)
400 : * to
401 : * p_{j-1} = _prodprimeslim[u] = precprime(M or 2^{u+7}) */
402 24479 : gel(W,u++) = zv_prod_Z(w);
403 24479 : jold = j; b *= 2;
404 24479 : if (b > M) b = M; /* truncate last run */
405 : }
406 24479 : for (j = 2; j < u; j++) gel(W,j) = mulii(gel(W,j-1), gel(W,j));
407 1883 : setlg(W, u); _prodprimes = ZV_copy_alloc(W);
408 1883 : setlg(_prodprimeslim, u); set_avma(av);
409 1883 : }
410 :
411 : static void
412 1883 : initprimes0(ulong maxnum, long *lenp, pari_prime *p1)
413 : {
414 1883 : pari_sp av = avma, bot = pari_mainstack->bot;
415 : long alloced, psize;
416 : byteptr q, end, p;
417 : ulong remains, curlow, rootnum, asize, prime_above, last;
418 : pari_prime *end1, *curdiff, *p_prime_above;
419 :
420 1883 : if (!odd(maxnum)) maxnum--; /* make it odd. */
421 : /* base case */
422 1883 : if (maxnum < 1ul<<17) { initprimes1(maxnum>>1, lenp, p1); return; }
423 :
424 : /* Checked to be enough up to 40e6, attained at 155893 */
425 1883 : rootnum = usqrt(maxnum) | 1;
426 1883 : initprimes1(rootnum>>1, &psize, p1);
427 1883 : last = rootnum;
428 1883 : end1 = p1 + psize - 1;
429 1883 : remains = (maxnum - last) >> 1; /* number of odd numbers to check */
430 : /* we access primes array of psize too; but we access it consecutively,
431 : * thus we do not include it in fixed_to_cache */
432 1883 : asize = good_arena_size((ulong)(rootnum * slow2_in_roots), remains+1, 0,
433 : &cache_model) - 1;
434 : /* enough room on the stack ? */
435 1883 : alloced = (((byteptr)avma) <= ((byteptr)bot) + asize);
436 1883 : p = (byteptr)(alloced? pari_malloc(asize+1): stack_malloc(asize+1));
437 1883 : end = p + asize; /* the 0 sentinel goes at end. */
438 1883 : curlow = last + 2; /* First candidate: know primes up to last (odd). */
439 1883 : curdiff = end1;
440 :
441 : /* During each iteration p..end-1 represents a range of odd
442 : numbers. */
443 1883 : p_prime_above = p1 + 2;
444 1883 : prime_above = 3;
445 9098 : while (remains)
446 : { /* cycle over arenas; performance not crucial */
447 : pari_prime was_delta;
448 7215 : if (asize > remains) { asize = remains; end = p + asize; }
449 : /* Fake the upper limit appropriate for the given arena */
450 329208 : while (prime_above*prime_above <= curlow + (asize << 1) && *p_prime_above)
451 321993 : prime_above = *p_prime_above++;
452 7215 : was_delta = *p_prime_above;
453 7215 : *p_prime_above = 0; /* sentinel for sieve_chunk */
454 7215 : sieve_chunk(p1, curlow, p, asize);
455 7215 : *p_prime_above = was_delta; /* restore */
456 :
457 7215 : p[asize] = 0; /* sentinel */
458 7215 : for (q = p; ; q++)
459 : { /* q runs over addresses corresponding to primes */
460 986277423 : while (*q) q++; /* use sentinel at end */
461 154136414 : if (q >= end) break;
462 154129199 : *curdiff++ = (pari_prime) 2*(q-p) + curlow;
463 : }
464 7215 : remains -= asize;
465 7215 : curlow += (asize<<1);
466 : }
467 1883 : *curdiff++ = 0; /* sentinel */
468 1883 : *lenp = curdiff - p1;
469 1883 : if (alloced) pari_free(p); else set_avma(av);
470 : }
471 :
472 : ulong
473 33332226 : maxprime(void) { return pari_PRIMES? pari_PRIMES[pari_PRIMES[0]]: 0; }
474 : ulong
475 72772040 : maxprimelim(void) { return pari_PRIMES? _maxprimelim: 0; }
476 : ulong
477 196 : maxprimeN(void) { return pari_PRIMES? pari_PRIMES[0]: 0; }
478 : GEN
479 932794 : prodprimes(void) { return pari_PRIMES? _prodprimes: NULL; }
480 : GEN
481 932794 : prodprimeslim(void) { return pari_PRIMES? _prodprimeslim: NULL; }
482 : void
483 0 : maxprime_check(ulong c) { if (maxprime() < c) pari_err_MAXPRIME(c); }
484 :
485 : static pari_prime*
486 1883 : initprimes(ulong maxnum)
487 : {
488 : pari_prime *t;
489 : long len;
490 : ulong N;
491 1883 : if (maxnum < 65537)
492 : {
493 0 : maxnum = 65537;
494 0 : N = 6543;
495 : }
496 : else
497 1883 : N = (long) ceil(primepi_upper_bound((double)maxnum));
498 1883 : t = (pari_prime*) pari_malloc(sizeof(*t) * (N+2));
499 1883 : initprimes0(maxnum, &len, t+1); t[0] = (pari_prime)(len-1);
500 1883 : _maxprimelim = maxnum;
501 1883 : return (pari_prime*) pari_realloc(t, sizeof(*t) * (len+1));
502 : }
503 :
504 : void
505 1883 : initprimetable(ulong maxnum)
506 : {
507 1883 : pari_prime *old = pari_PRIMES;
508 : #ifdef LONG_IS_64BIT
509 1622 : maxnum = minuu(maxnum, 4294967295);
510 : #endif
511 1883 : pari_PRIMES = initprimes(maxnum);
512 1883 : if (old) free(old);
513 1883 : set_prodprimes();
514 1883 : }
515 :
516 : /**********************************************************************/
517 : /*** ***/
518 : /*** forprime ***/
519 : /*** ***/
520 : /**********************************************************************/
521 :
522 : /* return good chunk size for sieve, 16 | chunk + 2 */
523 : static ulong
524 8452305 : optimize_chunk(ulong a, ulong b)
525 : {
526 : /* TODO: Optimize size (surely < 512k to stay in L2 cache, but not so large
527 : * as to force recalculating too often). */
528 : /* bigarena is in bytes, we use bits, and only odds */
529 8452305 : ulong defchunk = (a>>31) > 1 ? 0x80000UL : 0x8000;
530 8452305 : ulong chunk = (cache_model.bigarena ? cache_model.bigarena : defchunk)<<4;
531 8452305 : ulong tmp = (b - a) / chunk + 1;
532 :
533 8452305 : if (tmp == 1)
534 202 : chunk = b - a + 16;
535 : else
536 8452103 : chunk = (b - a) / tmp + 15;
537 : /* ensure 16 | chunk + 2 */
538 8452305 : return (((chunk + 2)>>4)<<4) - 2;
539 : }
540 : static void
541 8452305 : sieve_init(forprime_t *T, ulong a, ulong b)
542 : {
543 8452305 : T->sieveb = b;
544 8452305 : T->chunk = optimize_chunk(a, b);
545 : /* >> 1 [only odds] + 3 [convert from bits to bytes] */
546 8452309 : T->isieve = (unsigned char*)stack_malloc(((T->chunk+2) >> 4) + 1);
547 8452277 : T->cache[0] = 0;
548 8452277 : T->a = a;
549 8452277 : T->end = minuu(a + T->chunk, b);
550 8452267 : T->pos = T->maxpos = 0;
551 8452267 : }
552 :
553 : enum {PRST_none, PRST_diffptr, PRST_sieve, PRST_unextprime, PRST_nextprime};
554 :
555 : static void
556 8000986 : u_forprime_set_prime_table(forprime_t *T, ulong a)
557 : {
558 8000986 : T->strategy = PRST_diffptr;
559 8000986 : if (a < 3)
560 : {
561 2400023 : T->p = 0;
562 2400023 : T->n = 0;
563 : }
564 : else
565 : {
566 5600963 : long n = PRIMES_search(a - 1);
567 5600810 : if (n < 0) n = - n - 1;
568 5600810 : T->n = n;
569 5600810 : T->p = pari_PRIMES[n];
570 : }
571 8000833 : }
572 :
573 : /* Set p so that p + q the smallest integer = c (mod q) and > original p.
574 : * Assume 0 < c < q. */
575 : static void
576 101996 : arith_set(forprime_t *T)
577 : {
578 101996 : ulong r = T->p % T->q; /* 0 <= r <= min(p, q-1) */
579 101996 : pari_sp av = avma;
580 101996 : GEN d = adduu(T->p - r, T->c); /* = c mod q */
581 101996 : if (T->c > r) d = subiu(d, T->q);
582 : /* d = c mod q, d = c > r? p-r+c-q: p-r+c, so that
583 : * d <= p and d+q = c>r? p-r+c : p-r+c+q > p */
584 101996 : if (signe(d) <= 0)
585 : {
586 20 : T->p = 0;
587 20 : T->strategy = PRST_nextprime;
588 20 : affii(d, T->pp);
589 : }
590 : else
591 101976 : T->p = itou_or_0(d);
592 101996 : set_avma(av);
593 101996 : }
594 :
595 : /* Run through primes in arithmetic progression = c (mod q).
596 : * Warning: b = ULONG_MAX may signal that we are called by higher level
597 : * function handling a continuation for larger b; this sentinel value
598 : * must not be modified */
599 : static int
600 23286193 : u_forprime_sieve_arith_init(forprime_t *T, struct pari_sieve *psieve,
601 : ulong a, ulong b, ulong c, ulong q)
602 : {
603 : #ifdef LONG_IS_64BIT
604 19945527 : const ulong UPRIME_MAX = 18446744073709551557UL;
605 : #else
606 3340666 : const ulong UPRIME_MAX = 4294967291UL;
607 : #endif
608 : ulong Plim, P, P2, Y, sieveb;
609 :
610 23286193 : if (!odd(b) && b > 2) b--;
611 23286048 : if (a > b || b < 2)
612 : {
613 883603 : T->strategy = PRST_diffptr; /* paranoia */
614 883603 : T->p = 0; /* empty */
615 883603 : T->b = 0; /* empty */
616 883603 : T->n = 0;
617 883603 : return 0;
618 : }
619 22402445 : P = maxprime();
620 22401559 : if (b != ULONG_MAX && b > UPRIME_MAX) b = UPRIME_MAX;
621 22401559 : if (q != 1)
622 : {
623 : ulong D;
624 553027 : c %= q; D = ugcd(c, q);
625 553086 : if (D != 1) { a = maxuu(a,D); if (b != ULONG_MAX) b = minuu(b,D); }
626 553086 : if (odd(q) && (a > 2 || c != 2))
627 : { /* only *odd* primes. If a <= c = 2, then p = 2 must be included :-( */
628 474249 : if (!odd(c)) c += q;
629 475320 : q <<= 1;
630 : }
631 : }
632 22402455 : T->q = q;
633 22402455 : T->c = c;
634 22402455 : T->strategy = PRST_none; /* unknown */
635 22402455 : T->psieve = psieve; /* unused for now */
636 22402455 : T->isieve = NULL; /* unused for now */
637 22402455 : T->b = b;
638 22402455 : if (P >= b) { /* [a,b] \subset prime table */
639 4424671 : u_forprime_set_prime_table(T, a);
640 4424618 : return 1;
641 : }
642 : /* b > P */
643 17977784 : if (a >= P)
644 : {
645 14401660 : T->p = a - 1;
646 14401660 : if (T->q != 1) arith_set(T);
647 : }
648 : else
649 3576124 : u_forprime_set_prime_table(T, a);
650 17977637 : if (T->strategy == PRST_none) T->strategy = PRST_unextprime;
651 : /* now strategy is either PRST_diffptr or PRST_unextprime */
652 :
653 17977637 : P2 = (P & HIGHMASK)? 0 : P*P;
654 17977637 : sieveb = b; if (P2 && P2 < b) sieveb = P2;
655 : /* maxprime^2 >= sieveb */
656 17977637 : Plim = maxprimelim();
657 17977679 : if (a <= Plim) a = Plim + 1;
658 17977679 : if (sieveb < a + 16) return 1;
659 8919151 : Y = sieveb - a + 1; /* number of integers in sievable interval > 16 */
660 8919151 : P = usqrt(sieveb); /* largest sieving prime */
661 : /* FIXME: should sieve as well if q != 1, adapt sieve code */
662 8919190 : if (q == 1 && (!P2 || P2 > a) && 3/M_LN2 * Y >= uprimepi(P))
663 : /* Sieve implemented & possible & not too costly. Cost model is
664 : * - nextprime: about Y / log(b) primes to test [neglect cost for composites]
665 : * individual cost average = 3 log2(b) mulmod, total = 3 Y / log(2) mulmod
666 : * - sieve: pi(P) mod + Y loglog(b) add
667 : * Since loglog(b) < 4, and add < 10*mulmod, we neglect the Y loglog(b) term.
668 : * We have mod < mulmod < 2*mod; for now, assume mulmod ~ mod. */
669 : {
670 8452318 : if (T->strategy == PRST_unextprime) T->strategy = PRST_sieve;
671 8452318 : sieve_init(T, a, sieveb);
672 : }
673 8919310 : return 1;
674 : }
675 :
676 : int
677 17951601 : u_forprime_arith_init(forprime_t *T, ulong a, ulong b, ulong c, ulong q)
678 17951601 : { return u_forprime_sieve_arith_init(T, NULL, a, b, c, q); }
679 :
680 : /* will run through primes in [a,b] */
681 : int
682 17395791 : u_forprime_init(forprime_t *T, ulong a, ulong b)
683 17395791 : { return u_forprime_arith_init(T, a,b, 0,1); }
684 :
685 : /* will run through primes in [a,b] */
686 : static int
687 5334178 : u_forprime_sieve_init(forprime_t *T, struct pari_sieve *s, ulong b)
688 5334178 : { return u_forprime_sieve_arith_init(T, s, s->start, b, s->c, s->q); }
689 :
690 : /* now only run through primes <= c; assume c <= b above */
691 : void
692 63 : u_forprime_restrict(forprime_t *T, ulong c) { T->b = c; }
693 :
694 : /* b = NULL: loop forever */
695 : int
696 2427 : forprimestep_init(forprime_t *T, GEN a, GEN b, GEN q)
697 : {
698 2427 : GEN c = NULL;
699 : long lb;
700 :
701 2427 : a = gceil(a); if (typ(a) != t_INT) pari_err_TYPE("forprime_init",a);
702 2427 : T->qq = NULL; T->q = 1; T->c = 0;
703 2427 : if (q)
704 : {
705 133 : switch(typ(q))
706 : {
707 56 : case t_INT:
708 56 : c = a; break;
709 77 : case t_INTMOD:
710 77 : c = gel(q,2); q = gel(q,1);
711 : /* first int >= initial a which is = c (mod q) */
712 77 : a = addii(a, modii(subii(c,a), q)); break;
713 0 : default: pari_err_TYPE("forprimestep_init",q);
714 : }
715 133 : if (signe(q) <= 0) pari_err_TYPE("forprimestep_init (q <= 0)",q);
716 133 : if (equali1(q)) c = q = NULL;
717 : else
718 : {
719 133 : GEN D = gcdii(c, q);
720 133 : if (!is_pm1(D))
721 : { /* at most one prime: c */
722 42 : if (cmpii(a, D) < 0) a = D;
723 42 : if (gcmp(b, D) > 0) b = D;
724 : }
725 133 : if ((T->q = itou_or_0(q)))
726 125 : T->c = umodiu(c, T->q);
727 : else
728 8 : T->qq = q;
729 : }
730 : }
731 2427 : if (signe(a) <= 0) a = q? modii(a, q): gen_1;
732 2427 : if (b && typ(b) != t_INFINITY)
733 : {
734 1020 : b = gfloor(b);
735 1020 : if (typ(b) != t_INT) pari_err_TYPE("forprime_init",b);
736 1020 : if (signe(b) < 0 || cmpii(a,b) > 0)
737 : {
738 21 : T->strategy = PRST_nextprime; /* paranoia */
739 21 : T->bb = T->pp = gen_0; return 0;
740 : }
741 999 : lb = lgefint(b);
742 999 : T->bb = b;
743 : }
744 1407 : else if (!b || inf_get_sign(b) > 0)
745 : {
746 1407 : lb = lgefint(a) + 4;
747 1407 : T->bb = NULL;
748 : }
749 : else /* b == -oo */
750 : {
751 0 : T->strategy = PRST_nextprime; /* paranoia */
752 0 : T->bb = T->pp = gen_0; return 0;
753 : }
754 2406 : T->pp = cgeti(T->qq? maxuu(lb, lgefint(T->qq)): lb);
755 : /* a, b are positive integers, a <= b */
756 2406 : if (!T->qq && lgefint(a) == 3) /* lb == 3 implies b != NULL */
757 2262 : return u_forprime_arith_init(T, uel(a,2), lb == 3? uel(b,2): ULONG_MAX,
758 : T->c, T->q);
759 144 : T->strategy = PRST_nextprime;
760 144 : affii(T->qq? subii(a,T->qq): subiu(a,T->q), T->pp); return 1;
761 : }
762 : int
763 1315 : forprime_init(forprime_t *T, GEN a, GEN b)
764 1315 : { return forprimestep_init(T,a,b,NULL); }
765 :
766 : /* assume a <= b <= maxprime()^2, a,b odd, sieve[n] corresponds to
767 : * a+16*n, a+16*n+2, ..., a+16*n+14 (bits 0 to 7)
768 : * maxpos = index of last sieve cell.
769 : * b-a+2 must be divisible by 16 for use by u_forprime_next */
770 : static void
771 9156 : sieve_block(ulong a, ulong b, ulong maxpos, unsigned char* sieve)
772 : {
773 9156 : ulong i, N = pari_PRIMES[0], lim = usqrt(b), sz = (b-a) >> 1;
774 9156 : (void)memset(sieve, 0, maxpos+1);
775 25084833 : for (i = 2; i <= N; i++)
776 : { /* p is odd */
777 25084827 : ulong k, r, p = pari_PRIMES[i]; /* starts at p = 3 */
778 25084827 : if (p > lim) break;
779 :
780 : /* solve a + 2k = 0 (mod p) */
781 25075677 : r = a % p;
782 25075677 : if (r == 0)
783 16069 : k = 0;
784 : else
785 : {
786 25059608 : k = p - r;
787 25059608 : if (odd(k)) k += p;
788 25059608 : k >>= 1;
789 : }
790 : /* m = a + 2k is the smallest odd m >= a, p | m */
791 : /* position n (corresponds to a+2n) is sieve[n>>3], bit n&7 */
792 5757062882 : while (k <= sz) { sieve[k>>3] |= 1 << (k&7); k += p; /* 2k += 2p */ }
793 : }
794 9156 : }
795 :
796 : static void
797 1883 : pari_sieve_init(struct pari_sieve *s, ulong a, ulong b)
798 : {
799 1883 : ulong maxpos= (b - a) >> 4;
800 1883 : s->start = a; s->end = b;
801 1883 : s->sieve = (unsigned char*) pari_malloc(maxpos+1);
802 1883 : s->c = 0; s->q = 1;
803 1883 : sieve_block(a, b, maxpos, s->sieve);
804 1883 : s->maxpos = maxpos; /* must be last in case of SIGINT */
805 1883 : }
806 :
807 : static struct pari_sieve pari_sieve_modular;
808 : void
809 1883 : pari_init_primes(ulong maxprime)
810 : {
811 1883 : ulong a = (1UL<<31) + 1, b = a + (1UL<<20)-2;
812 1883 : initprimetable(maxprime);
813 1883 : pari_sieve_init(&pari_sieve_modular, a, b);
814 1883 : }
815 :
816 : void
817 1883 : pari_close_primes(void)
818 : {
819 1883 : if (pari_PRIMES)
820 : {
821 1883 : pari_free(pari_PRIMES);
822 1883 : pari_free(_prodprimes);
823 1883 : pari_free(_prodprimeslim);
824 : }
825 1883 : pari_free(pari_sieve_modular.sieve);
826 1883 : }
827 :
828 : void
829 4477620 : init_modular_small(forprime_t *S)
830 : {
831 : #ifdef LONG_IS_64BIT
832 3837614 : u_forprime_sieve_init(S, &pari_sieve_modular, ULONG_MAX);
833 : #else
834 640006 : ulong a = (1UL<<((BITS_IN_LONG-2)>>1))+1;
835 640006 : u_forprime_init(S, a, ULONG_MAX);
836 : #endif
837 4477633 : }
838 :
839 : void
840 10458782 : init_modular_big(forprime_t *S)
841 : {
842 : #ifdef LONG_IS_64BIT
843 8962215 : u_forprime_init(S, HIGHBIT + 1, ULONG_MAX);
844 : #else
845 1496567 : u_forprime_sieve_init(S, &pari_sieve_modular, ULONG_MAX);
846 : #endif
847 10458837 : }
848 :
849 : /* T->cache is a 0-terminated list of primes, return the first one and
850 : * remove it from list. Most of the time the list contains a single prime */
851 : static ulong
852 130079012 : shift_cache(forprime_t *T)
853 : {
854 : long i;
855 130079012 : T->p = T->cache[0];
856 173916406 : for (i = 1;; i++) /* remove one prime from cache */
857 173916406 : if (! (T->cache[i-1] = T->cache[i]) ) break;
858 130079012 : return T->p;
859 : }
860 :
861 : ulong
862 204032868 : u_forprime_next(forprime_t *T)
863 : {
864 204032868 : if (T->strategy == PRST_diffptr)
865 : {
866 : for(;;)
867 : {
868 216782154 : if (++T->n <= pari_PRIMES[0])
869 : {
870 216781993 : T->p = pari_PRIMES[T->n];
871 216781993 : if (T->p > T->b) return 0;
872 216589478 : if (T->q == 1 || T->p % T->q == T->c) return T->p;
873 : }
874 : else
875 : { /* beyond the table */
876 161 : T->strategy = T->isieve? PRST_sieve: PRST_unextprime;
877 161 : if (T->q != 1) { arith_set(T); if (!T->p) return 0; }
878 : /* T->p possibly not a prime if q != 1 */
879 161 : break;
880 : }
881 : }
882 : }
883 145056853 : if (T->strategy == PRST_sieve)
884 : { /* require sieveb - a >= 16 */
885 : ulong n;
886 130079619 : if (T->cache[0]) return shift_cache(T);
887 93037039 : NEXT_CHUNK:
888 93045215 : if (T->psieve)
889 : {
890 5334185 : T->sieve = T->psieve->sieve;
891 5334185 : T->end = T->psieve->end;
892 5334185 : if (T->end > T->sieveb) T->end = T->sieveb;
893 5334185 : T->maxpos = T->psieve->maxpos;
894 5334185 : T->pos = 0;
895 5334185 : T->psieve = NULL;
896 : }
897 140906473 : for (n = T->pos; n < T->maxpos; n++)
898 140896018 : if (T->sieve[n] != 0xFF)
899 : {
900 93034760 : unsigned char mask = T->sieve[n];
901 93034760 : ulong p = T->a + (n<<4);
902 93034760 : long i = 0;
903 93034760 : T->pos = n;
904 93034760 : if (!(mask & 1)) T->cache[i++] = p;
905 93034760 : if (!(mask & 2)) T->cache[i++] = p+2;
906 93034760 : if (!(mask & 4)) T->cache[i++] = p+4;
907 93034760 : if (!(mask & 8)) T->cache[i++] = p+6;
908 93034760 : if (!(mask & 16)) T->cache[i++] = p+8;
909 93034760 : if (!(mask & 32)) T->cache[i++] = p+10;
910 93034760 : if (!(mask & 64)) T->cache[i++] = p+12;
911 93034760 : if (!(mask &128)) T->cache[i++] = p+14;
912 93034760 : T->cache[i] = 0;
913 93034760 : T->pos = n+1;
914 93034760 : return shift_cache(T);
915 : }
916 : /* n = T->maxpos, last cell: check p <= b */
917 10455 : if (T->maxpos && n == T->maxpos && T->sieve[n] != 0xFF)
918 : {
919 2879 : unsigned char mask = T->sieve[n];
920 2879 : ulong p = T->a + (n<<4);
921 2879 : long i = 0;
922 2879 : T->pos = n;
923 2879 : if (!(mask & 1) && p <= T->sieveb) T->cache[i++] = p;
924 2879 : if (!(mask & 2) && p <= T->sieveb-2) T->cache[i++] = p+2;
925 2879 : if (!(mask & 4) && p <= T->sieveb-4) T->cache[i++] = p+4;
926 2879 : if (!(mask & 8) && p <= T->sieveb-6) T->cache[i++] = p+6;
927 2879 : if (!(mask & 16) && p <= T->sieveb-8) T->cache[i++] = p+8;
928 2879 : if (!(mask & 32) && p <= T->sieveb-10) T->cache[i++] = p+10;
929 2879 : if (!(mask & 64) && p <= T->sieveb-12) T->cache[i++] = p+12;
930 2879 : if (!(mask &128) && p <= T->sieveb-14) T->cache[i++] = p+14;
931 2879 : if (i)
932 : {
933 2772 : T->cache[i] = 0;
934 2772 : T->pos = n+1;
935 2772 : return shift_cache(T);
936 : }
937 : }
938 :
939 7683 : if (T->maxpos && T->end >= T->sieveb) /* done with sieves ? */
940 : {
941 425 : if (T->sieveb == T->b && T->b != ULONG_MAX) return 0;
942 7 : T->strategy = PRST_unextprime;
943 : }
944 : else
945 : { /* initialize next chunk */
946 7258 : T->sieve = T->isieve;
947 7258 : if (T->maxpos == 0)
948 3372 : T->a |= 1; /* first time; ensure odd */
949 : else
950 3886 : T->a = (T->end + 2) | 1;
951 7258 : T->end = T->a + T->chunk; /* may overflow */
952 7258 : if (T->end < T->a || T->end > T->sieveb) T->end = T->sieveb;
953 : /* end and a are odd; sieve[k] contains the a + 8*2k + (0,2,...,14).
954 : * The largest k is (end-a) >> 4 */
955 7258 : T->pos = 0;
956 7258 : T->maxpos = (T->end - T->a) >> 4; /* >= 1 */
957 7258 : sieve_block(T->a, T->end, T->maxpos, T->sieve);
958 8176 : goto NEXT_CHUNK;
959 : }
960 : }
961 14977241 : if (T->strategy == PRST_unextprime)
962 : {
963 14962127 : if (T->q == 1)
964 : {
965 : #ifdef LONG_IS_64BIT
966 14808992 : switch(T->p)
967 : {
968 : #define retp(x) return T->p = (HIGHBIT+x <= T->b)? HIGHBIT+x: 0
969 8962216 : case HIGHBIT: retp(29);
970 3189271 : case HIGHBIT + 29: retp(99);
971 363973 : case HIGHBIT + 99: retp(123);
972 212127 : case HIGHBIT +123: retp(131);
973 153848 : case HIGHBIT +131: retp(155);
974 132825 : case HIGHBIT +155: retp(255);
975 111286 : case HIGHBIT +255: retp(269);
976 101462 : case HIGHBIT +269: retp(359);
977 81195 : case HIGHBIT +359: retp(435);
978 61866 : case HIGHBIT +435: retp(449);
979 55055 : case HIGHBIT +449: retp(453);
980 52104 : case HIGHBIT +453: retp(485);
981 46041 : case HIGHBIT +485: retp(491);
982 42930 : case HIGHBIT +491: retp(543);
983 40524 : case HIGHBIT +543: retp(585);
984 37908 : case HIGHBIT +585: retp(599);
985 31724 : case HIGHBIT +599: retp(753);
986 30884 : case HIGHBIT +753: retp(849);
987 29654 : case HIGHBIT +849: retp(879);
988 27938 : case HIGHBIT +879: retp(885);
989 27176 : case HIGHBIT +885: retp(903);
990 26666 : case HIGHBIT +903: retp(995);
991 : #undef retp
992 : }
993 : #endif
994 990391 : T->p = unextprime(T->p + 1);
995 990437 : if (T->p > T->b) return 0;
996 : }
997 : else do {
998 2798512 : T->p += T->q;
999 2798512 : if (T->p < T->q || T->p > T->b) { T->p = 0; break; } /* overflow */
1000 2798486 : } while (!uisprime(T->p));
1001 1143437 : if (T->p && T->p <= T->b) return T->p;
1002 : /* overflow ulong, switch to GEN */
1003 49 : T->strategy = PRST_nextprime;
1004 : }
1005 15163 : return 0; /* overflow */
1006 : }
1007 :
1008 : GEN
1009 45413399 : forprime_next(forprime_t *T)
1010 : {
1011 : pari_sp av;
1012 : GEN p;
1013 45413399 : if (T->strategy != PRST_nextprime)
1014 : {
1015 45401600 : ulong u = u_forprime_next(T);
1016 45401600 : if (u) { affui(u, T->pp); return T->pp; }
1017 : /* failure */
1018 820 : if (T->strategy != PRST_nextprime) return NULL; /* we're done */
1019 : /* overflow ulong, switch to GEN */
1020 48 : u = ULONG_MAX;
1021 48 : if (T->q > 1) u -= (ULONG_MAX-T->c) % T->q;
1022 48 : affui(u, T->pp);
1023 : }
1024 11847 : av = avma; p = T->pp;
1025 11847 : if (T->q == 1)
1026 : {
1027 11669 : p = nextprime(addiu(p, 1));
1028 11669 : if (T->bb && abscmpii(p, T->bb) > 0) return gc_NULL(av);
1029 : } else do {
1030 3341 : p = T->qq? addii(p, T->qq): addiu(p, T->q);
1031 3341 : if (T->bb && abscmpii(p, T->bb) > 0) return gc_NULL(av);
1032 3285 : } while (!BPSW_psp(p));
1033 11663 : affii(p, T->pp); return gc_const(av, T->pp);
1034 : }
1035 :
1036 : void
1037 1092 : forprimestep(GEN a, GEN b, GEN q, GEN code)
1038 : {
1039 1092 : pari_sp av = avma;
1040 : forprime_t T;
1041 :
1042 1092 : if (!forprimestep_init(&T, a,b,q)) { set_avma(av); return; }
1043 :
1044 1078 : push_lex(T.pp,code);
1045 337262 : while(forprime_next(&T))
1046 : {
1047 336611 : closure_evalvoid(code); if (loop_break()) break;
1048 : /* p changed in 'code', complain */
1049 336191 : if (get_lex(-1) != T.pp)
1050 7 : pari_err(e_MISC,"prime index read-only: was changed to %Ps", get_lex(-1));
1051 : }
1052 1071 : pop_lex(1); set_avma(av);
1053 : }
1054 : void
1055 966 : forprime(GEN a, GEN b, GEN code) { return forprimestep(a,b,NULL,code); }
1056 :
1057 : int
1058 70 : forcomposite_init(forcomposite_t *C, GEN a, GEN b)
1059 : {
1060 70 : pari_sp av = avma;
1061 70 : a = gceil(a);
1062 70 : if (typ(a)!=t_INT) pari_err_TYPE("forcomposite",a);
1063 70 : if (b) {
1064 63 : if (typ(b) == t_INFINITY) b = NULL;
1065 : else
1066 : {
1067 56 : b = gfloor(b);
1068 56 : if (typ(b)!=t_INT) pari_err_TYPE("forcomposite",b);
1069 : }
1070 : }
1071 70 : if (signe(a) < 0) pari_err_DOMAIN("forcomposite", "a", "<", gen_0, a);
1072 70 : if (abscmpiu(a, 4) < 0) a = utoipos(4);
1073 70 : C->first = 1;
1074 70 : if (!forprime_init(&C->T, a,b) && cmpii(a,b) > 0)
1075 : {
1076 7 : C->n = gen_1; /* in case caller forgets to check the return value */
1077 7 : C->b = gen_0; return gc_bool(av,0);
1078 : }
1079 63 : C->n = setloop(a);
1080 63 : C->b = b;
1081 63 : C->p = NULL; return 1;
1082 : }
1083 :
1084 : GEN
1085 238 : forcomposite_next(forcomposite_t *C)
1086 : {
1087 238 : if (C->first) /* first call ever */
1088 : {
1089 63 : C->first = 0;
1090 63 : C->p = forprime_next(&C->T);
1091 : }
1092 : else
1093 175 : C->n = incloop(C->n);
1094 238 : if (C->p)
1095 : {
1096 161 : if (cmpii(C->n, C->p) < 0) return C->n;
1097 77 : C->n = incloop(C->n);
1098 : /* n = p+1 */
1099 77 : C->p = forprime_next(&C->T); /* nextprime(p) > n */
1100 77 : if (C->p) return C->n;
1101 : }
1102 105 : if (!C->b || cmpii(C->n, C->b) <= 0) return C->n;
1103 42 : return NULL;
1104 : }
1105 :
1106 : void
1107 70 : forcomposite(GEN a, GEN b, GEN code)
1108 : {
1109 70 : pari_sp av = avma;
1110 : forcomposite_t T;
1111 : GEN n;
1112 70 : if (!forcomposite_init(&T,a,b)) return;
1113 63 : push_lex(T.n,code);
1114 238 : while((n = forcomposite_next(&T)))
1115 : {
1116 196 : closure_evalvoid(code); if (loop_break()) break;
1117 : /* n changed in 'code', complain */
1118 182 : if (get_lex(-1) != n)
1119 7 : pari_err(e_MISC,"index read-only: was changed to %Ps", get_lex(-1));
1120 : }
1121 56 : pop_lex(1); set_avma(av);
1122 : }
|