Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - language - forprime.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25819-e703fe1174) Lines: 407 482 84.4 %
Date: 2020-09-18 06:10:04 Functions: 34 39 87.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13