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 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 - basemath - ifactor1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21343-6216058) Lines: 1428 1760 81.1 %
Date: 2017-11-19 06:21:17 Functions: 84 96 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  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             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : 
      16             : /***********************************************************************/
      17             : /**                       PRIMES IN SUCCESSION                        **/
      18             : /***********************************************************************/
      19             : 
      20             : /* map from prime residue classes mod 210 to their numbers in {0...47}.
      21             :  * Subscripts into this array take the form ((k-1)%210)/2, ranging from
      22             :  * 0 to 104.  Unused entries are */
      23             : #define NPRC 128 /* non-prime residue class */
      24             : 
      25             : static unsigned char prc210_no[] = {
      26             :   0, NPRC, NPRC, NPRC, NPRC, 1, 2, NPRC, 3, 4, NPRC, /* 21 */
      27             :   5, NPRC, NPRC, 6, 7, NPRC, NPRC, 8, NPRC, 9, /* 41 */
      28             :   10, NPRC, 11, NPRC, NPRC, 12, NPRC, NPRC, 13, 14, NPRC, /* 63 */
      29             :   NPRC, 15, NPRC, 16, 17, NPRC, NPRC, 18, NPRC, 19, /* 83 */
      30             :   NPRC, NPRC, 20, NPRC, NPRC, NPRC, 21, NPRC, 22, 23, NPRC, /* 105 */
      31             :   24, 25, NPRC, 26, NPRC, NPRC, NPRC, 27, NPRC, NPRC, /* 125 */
      32             :   28, NPRC, 29, NPRC, NPRC, 30, 31, NPRC, 32, NPRC, NPRC, /* 147 */
      33             :   33, 34, NPRC, NPRC, 35, NPRC, NPRC, 36, NPRC, 37, /* 167 */
      34             :   38, NPRC, 39, NPRC, NPRC, 40, 41, NPRC, NPRC, 42, NPRC, /* 189 */
      35             :   43, 44, NPRC, 45, 46, NPRC, NPRC, NPRC, NPRC, 47, /* 209 */
      36             : };
      37             : 
      38             : /* first differences of the preceding */
      39             : static unsigned char prc210_d1[] = {
      40             :   10, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6,
      41             :   4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6,
      42             :   2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2, 10, 2,
      43             : };
      44             : 
      45             : /* return 0 for overflow */
      46             : ulong
      47     3693939 : unextprime(ulong n)
      48             : {
      49             :   long rc, rc0, rcd, rcn;
      50             : 
      51     3693939 :   switch(n) {
      52        5567 :     case 0: case 1: case 2: return 2;
      53        2238 :     case 3: return 3;
      54        1801 :     case 4: case 5: return 5;
      55        1393 :     case 6: case 7: return 7;
      56             :   }
      57             : #ifdef LONG_IS_64BIT
      58     3005997 :   if (n > (ulong)-59) return 0;
      59             : #else
      60      676943 :   if (n > (ulong)-5) return 0;
      61             : #endif
      62             :   /* here n > 7 */
      63     3682918 :   n |= 1; /* make it odd */
      64     3682918 :   rc = rc0 = n % 210;
      65             :   /* find next prime residue class mod 210 */
      66             :   for(;;)
      67             :   {
      68     7299298 :     rcn = (long)(prc210_no[rc>>1]);
      69     7299298 :     if (rcn != NPRC) break;
      70     3616380 :     rc += 2; /* cannot wrap since 209 is coprime and rc odd */
      71     3616380 :   }
      72     3682918 :   if (rc > rc0) n += rc - rc0;
      73             :   /* now find an actual (pseudo)prime */
      74             :   for(;;)
      75             :   {
      76    14745335 :     if (uisprime(n)) break;
      77    11062417 :     rcd = prc210_d1[rcn];
      78    11062417 :     if (++rcn > 47) rcn = 0;
      79    11062417 :     n += rcd;
      80    11062417 :   }
      81     3682918 :   return n;
      82             : }
      83             : 
      84             : GEN
      85     2036032 : nextprime(GEN n)
      86             : {
      87             :   long rc, rc0, rcd, rcn;
      88     2036032 :   pari_sp av = avma;
      89             : 
      90     2036032 :   if (typ(n) != t_INT)
      91             :   {
      92          14 :     n = gceil(n);
      93          14 :     if (typ(n) != t_INT) pari_err_TYPE("nextprime",n);
      94             :   }
      95     2036025 :   if (signe(n) <= 0) { avma = av; return gen_2; }
      96     2035934 :   if (lgefint(n) == 3)
      97             :   {
      98     2028090 :     ulong k = unextprime(uel(n,2));
      99     2028090 :     avma = av;
     100     2028090 :     if (k) return utoipos(k);
     101             : #ifdef LONG_IS_64BIT
     102           6 :     return uutoi(1,13);
     103             : #else
     104           1 :     return uutoi(1,15);
     105             : #endif
     106             :   }
     107             :   /* here n > 7 */
     108        7844 :   if (!mod2(n)) n = addui(1,n);
     109        7844 :   rc = rc0 = umodiu(n, 210);
     110             :   /* find next prime residue class mod 210 */
     111             :   for(;;)
     112             :   {
     113       17185 :     rcn = (long)(prc210_no[rc>>1]);
     114       17185 :     if (rcn != NPRC) break;
     115        9341 :     rc += 2; /* cannot wrap since 209 is coprime and rc odd */
     116        9341 :   }
     117        7844 :   if (rc > rc0) n = addui(rc - rc0, n);
     118             :   /* now find an actual (pseudo)prime */
     119             :   for(;;)
     120             :   {
     121       78056 :     if (BPSW_psp(n)) break;
     122       70212 :     rcd = prc210_d1[rcn];
     123       70212 :     if (++rcn > 47) rcn = 0;
     124       70212 :     n = addui(rcd, n);
     125       70212 :   }
     126        7844 :   if (avma == av) return icopy(n);
     127        7844 :   return gerepileuptoint(av, n);
     128             : }
     129             : 
     130             : ulong
     131          32 : uprecprime(ulong n)
     132             : {
     133             :   long rc, rc0, rcd, rcn;
     134             :   { /* check if n <= 10 */
     135          32 :     if (n <= 1)  return 0;
     136          25 :     if (n == 2)  return 2;
     137          18 :     if (n <= 4)  return 3;
     138          18 :     if (n <= 6)  return 5;
     139          18 :     if (n <= 10) return 7;
     140             :   }
     141             :   /* here n >= 11 */
     142          18 :   if (!(n % 2)) n--;
     143          18 :   rc = rc0 = n % 210;
     144             :   /* find previous prime residue class mod 210 */
     145             :   for(;;)
     146             :   {
     147          36 :     rcn = (long)(prc210_no[rc>>1]);
     148          36 :     if (rcn != NPRC) break;
     149          18 :     rc -= 2; /* cannot wrap since 1 is coprime and rc odd */
     150          18 :   }
     151          18 :   if (rc < rc0) n += rc - rc0;
     152             :   /* now find an actual (pseudo)prime */
     153             :   for(;;)
     154             :   {
     155          36 :     if (uisprime(n)) break;
     156          18 :     if (--rcn < 0) rcn = 47;
     157          18 :     rcd = prc210_d1[rcn];
     158          18 :     n -= rcd;
     159          18 :   }
     160          18 :   return n;
     161             : }
     162             : 
     163             : GEN
     164          49 : precprime(GEN n)
     165             : {
     166             :   long rc, rc0, rcd, rcn;
     167          49 :   pari_sp av = avma;
     168             : 
     169          49 :   if (typ(n) != t_INT)
     170             :   {
     171          14 :     n = gfloor(n);
     172          14 :     if (typ(n) != t_INT) pari_err_TYPE("nextprime",n);
     173             :   }
     174          42 :   if (signe(n) <= 0) { avma = av; return gen_0; }
     175          42 :   if (lgefint(n) <= 3)
     176             :   {
     177          32 :     ulong k = uel(n,2);
     178          32 :     avma = av;
     179          32 :     return utoi(uprecprime(k));
     180             :   }
     181          10 :   if (!mod2(n)) n = subiu(n,1);
     182          10 :   rc = rc0 = umodiu(n, 210);
     183             :   /* find previous prime residue class mod 210 */
     184             :   for(;;)
     185             :   {
     186          20 :     rcn = (long)(prc210_no[rc>>1]);
     187          20 :     if (rcn != NPRC) break;
     188          10 :     rc -= 2; /* cannot wrap since 1 is coprime and rc odd */
     189          10 :   }
     190          10 :   if (rc0 > rc) n = subiu(n, rc0 - rc);
     191             :   /* now find an actual (pseudo)prime */
     192             :   for(;;)
     193             :   {
     194          48 :     if (BPSW_psp(n)) break;
     195          38 :     if (--rcn < 0) rcn = 47;
     196          38 :     rcd = prc210_d1[rcn];
     197          38 :     n = subiu(n, rcd);
     198          38 :   }
     199          10 :   if (avma == av) return icopy(n);
     200          10 :   return gerepileuptoint(av, n);
     201             : }
     202             : 
     203             : /* Find next single-word prime strictly larger than p.
     204             :  * If **d is non-NULL (somewhere in a diffptr), this is p + *(*d)++;
     205             :  * otherwise imitate nextprime().
     206             :  * *rcn = NPRC or the correct residue class for the current p; we'll use this
     207             :  * to track the current prime residue class mod 210 once we're out of range of
     208             :  * the diffptr table, and we'll update it before that if it isn't NPRC.
     209             :  *
     210             :  * *q is incremented whenever q!=NULL and we wrap from 209 mod 210 to
     211             :  * 1 mod 210
     212             :  * k =  second argument for Fl_MR_Jaeschke(). --GN1998Aug22 */
     213             : ulong
     214     4581948 : snextpr(ulong p, byteptr *d, long *rcn, long *q, long k)
     215             : {
     216             :   ulong n;
     217     4581948 :   if (**d)
     218             :   {
     219     4581948 :     byteptr dd = *d;
     220     4581948 :     long d1 = 0;
     221             : 
     222     4581948 :     NEXT_PRIME_VIADIFF(d1,dd);
     223             :     /* d1 = nextprime(p+1) - p */
     224     4581948 :     if (*rcn != NPRC)
     225             :     {
     226    20602922 :       while (d1 > 0)
     227             :       {
     228    11473010 :         d1 -= prc210_d1[*rcn];
     229    11473010 :         if (++*rcn > 47) { *rcn = 0; if (q) (*q)++; }
     230             :       }
     231             :       /* assert(d1 == 0) */
     232             :     }
     233     4581948 :     NEXT_PRIME_VIADIFF(p,*d);
     234     4581948 :     return p;
     235             :   }
     236             :   /* we are beyond the diffptr table */
     237             :   /* initialize */
     238           0 :   if (*rcn == NPRC) *rcn = prc210_no[(p % 210) >> 1]; /* != NPRC */
     239             :   /* look for the next one */
     240           0 :   n = p + prc210_d1[*rcn];
     241           0 :   if (++*rcn > 47) *rcn = 0;
     242           0 :   while (!Fl_MR_Jaeschke(n, k))
     243             :   {
     244           0 :     n += prc210_d1[*rcn];
     245           0 :     if (n <= 11) pari_err_OVERFLOW("snextpr");
     246           0 :     if (++*rcn > 47) { *rcn = 0; if (q) (*q)++; }
     247             :   }
     248           0 :   return n;
     249             : }
     250             : 
     251             : /********************************************************************/
     252             : /**                                                                **/
     253             : /**                     INTEGER FACTORIZATION                      **/
     254             : /**                                                                **/
     255             : /********************************************************************/
     256             : int factor_add_primes = 0, factor_proven = 0;
     257             : 
     258             : /***********************************************************************/
     259             : /**                                                                   **/
     260             : /**                 FACTORIZATION (ECM) -- GN Jul-Aug 1998            **/
     261             : /**   Integer factorization using the elliptic curves method (ECM).   **/
     262             : /**   ellfacteur() returns a non trivial factor of N, assuming N>0,   **/
     263             : /**   is composite, and has no prime divisor below 2^14 or so.        **/
     264             : /**   Thanks to Paul Zimmermann for much helpful advice and to        **/
     265             : /**   Guillaume Hanrot and Igor Schein for intensive testing          **/
     266             : /**                                                                   **/
     267             : /***********************************************************************/
     268             : #define nbcmax 64 /* max number of simultaneous curves */
     269             : 
     270             : static const ulong TB1[] = {
     271             :   142,172,208,252,305,370,450,545,661,801,972,1180,1430,
     272             :   1735,2100,2550,3090,3745,4540,5505,6675,8090,9810,11900,
     273             :   14420,17490,21200,25700,31160,37780UL,45810UL,55550UL,67350UL,
     274             :   81660UL,99010UL,120050UL,145550UL,176475UL,213970UL,259430UL,
     275             :   314550UL,381380UL,462415UL,560660UL,679780UL,824220UL,999340UL,
     276             :   1211670UL,1469110UL,1781250UL,2159700UL,2618600UL,3175000UL,
     277             :   3849600UL,4667500UL,5659200UL,6861600UL,8319500UL,10087100UL,
     278             :   12230300UL,14828900UL,17979600UL,21799700UL,26431500UL,
     279             :   32047300UL,38856400UL, /* 110 times that still fits into 32bits */
     280             : #ifdef LONG_IS_64BIT
     281             :   47112200UL,57122100UL,69258800UL,83974200UL,101816200UL,
     282             :   123449000UL,149678200UL,181480300UL,220039400UL,266791100UL,
     283             :   323476100UL,392204900UL,475536500UL,576573500UL,699077800UL,
     284             :   847610500UL,1027701900UL,1246057200UL,1510806400UL,1831806700UL,
     285             :   2221009800UL,2692906700UL,3265067200UL,3958794400UL,4799917500UL
     286             : #endif
     287             : };
     288             : static const ulong TB1_for_stage[] = {
     289             :  /* Start below the optimal B1 for finding factors which would just have been
     290             :   * missed by pollardbrent(), and escalate, changing curves to give good
     291             :   * coverage of the small factor ranges. Entries grow faster than what would
     292             :   * be optimal but a table instead of a 2D array keeps the code simple */
     293             :   500,520,560,620,700,800,900,1000,1150,1300,1450,1600,1800,2000,
     294             :   2200,2450,2700,2950,3250,3600,4000,4400,4850,5300,5800,6400,
     295             :   7100,7850,8700,9600,10600,11700,12900,14200,15700,17300,
     296             :   19000,21000,23200,25500,28000,31000,34500UL,38500UL,43000UL,
     297             :   48000UL,53800UL,60400UL,67750UL,76000UL,85300UL,95700UL,
     298             :   107400UL,120500UL,135400UL,152000UL,170800UL,191800UL,215400UL,
     299             :   241800UL,271400UL,304500UL,341500UL,383100UL,429700UL,481900UL,
     300             :   540400UL,606000UL,679500UL,761800UL,854100UL,957500UL,1073500UL
     301             : };
     302             : 
     303             : /* addition/doubling/multiplication of a point on an 'elliptic curve mod N'
     304             :  * may result in one of three things:
     305             :  * - a new bona fide point
     306             :  * - a point at infinity (denominator divisible by N)
     307             :  * - a point at infinity mod some p | N but finite mod q | N betraying itself
     308             :  *   by a denominator which has nontrivial gcd with N.
     309             :  *
     310             :  * In the second case, addition/doubling aborts, copying one of the summands
     311             :  * to the destination array of points unless they coincide.
     312             :  * Multiplication will stop at some unpredictable intermediate stage:  The
     313             :  * destination will contain _some_ multiple of the input point, but not
     314             :  * necessarily the desired one, which doesn't matter.  As long as we're
     315             :  * multiplying (B1 phase) we simply carry on with the next multiplier.
     316             :  * During the B2 phase, the only additions are the giant steps, and the
     317             :  * worst that can happen here is that we lose one residue class mod 210
     318             :  * of prime multipliers on 4 of the curves, so again, we ignore the problem
     319             :  * and just carry on.)
     320             :  *
     321             :  * Idea: select nbc curves mod N and one point P on each of them. For each
     322             :  * such P, compute [M]P = Q where M is the product of all powers <= B2 of
     323             :  * primes <= nextprime(B1). Then check whether [p]Q for p < nextprime(B2)
     324             :  * betrays a factor. This second stage looks separately at the primes in
     325             :  * each residue class mod 210, four curves at a time, and steps additively
     326             :  * to ever larger multipliers, by comparing X coordinates of points which we
     327             :  * would need to add in order to reach another prime multiplier in the same
     328             :  * residue class. 'Comparing' means that we accumulate a product of
     329             :  * differences of X coordinates, and from time to time take a gcd of this
     330             :  * product with N. Montgomery's multi-inverse trick is used heavily. */
     331             : 
     332             : /* *** auxiliary functions for ellfacteur: *** */
     333             : /* (Rx,Ry) <- (Px,Py)+(Qx,Qy) over Z/NZ, z=1/(Px-Qx). If Ry = NULL, don't set */
     334             : static void
     335     8349008 : FpE_add_i(GEN N, GEN z, GEN Px, GEN Py, GEN Qx, GEN Qy, GEN *Rx, GEN *Ry)
     336             : {
     337     8349008 :   GEN slope = modii(mulii(subii(Py, Qy), z), N);
     338     8349008 :   GEN t = subii(sqri(slope), addii(Qx, Px));
     339     8349008 :   affii(modii(t, N), *Rx);
     340     8349008 :   if (Ry) {
     341     8275860 :     t = subii(mulii(slope, subii(Px, *Rx)), Py);
     342     8275860 :     affii(modii(t, N), *Ry);
     343             :   }
     344     8349008 : }
     345             : /* X -> Z; cannot add on one of the curves: make sure Z contains
     346             :  * something useful before letting caller proceed */
     347             : static void
     348       27974 : ZV_aff(long n, GEN *X, GEN *Z)
     349             : {
     350       27974 :   if (X != Z) {
     351             :     long k;
     352       27456 :     for (k = n; k--; ) affii(X[k],Z[k]);
     353             :   }
     354       27974 : }
     355             : 
     356             : /* Parallel addition on nbc curves, assigning the result to locations at and
     357             :  * following *X3, *Y3. (If Y-coords of result not desired, set Y=NULL.)
     358             :  * Safe even if (X3,Y3) = (X2,Y2), _not_ if (X1,Y1). It is also safe to
     359             :  * overwrite Y2 with X3. If nbc1 < nbc, the first summand is
     360             :  * assumed to hold only nbc1 distinct points, repeated as often as we need
     361             :  * them  (to add one point on each of a few curves to several other points on
     362             :  * the same curves): only used with nbc1 = nbc or nbc1 = 4 | nbc.
     363             :  *
     364             :  * Return 0 [SUCCESS], 1 [N | den], 2 [gcd(den, N) is a factor of N, preserved
     365             :  * in gl.
     366             :  * Stack space is bounded by a constant multiple of lgefint(N)*nbc:
     367             :  * - Phase 2 creates 12 items on the stack per iteration, of which 4 are twice
     368             :  *   as long and 1 is thrice as long as N, i.e. 18 units per iteration.
     369             :  * - Phase  1 creates 4 units.
     370             :  * Total can be as large as 4*nbcmax + 18*8 units; ecm_elladd2() is
     371             :  * just as bad, and elldouble() comes to 3*nbcmax + 29*8 units. */
     372             : static int
     373      247445 : ecm_elladd0(GEN N, GEN *gl, long nbc, long nbc1,
     374             :             GEN *X1, GEN *Y1, GEN *X2, GEN *Y2, GEN *X3, GEN *Y3)
     375             : {
     376      247445 :   const ulong mask = (nbc1 == 4)? 3: ~0UL; /*nbc1 = 4 or nbc*/
     377      247445 :   GEN W[2*nbcmax], *A = W+nbc; /* W[0],A[0] unused */
     378             :   long i;
     379      247445 :   pari_sp av = avma;
     380             : 
     381      247445 :   W[1] = subii(X1[0], X2[0]);
     382     7888840 :   for (i=1; i<nbc; i++)
     383             :   { /*prepare for multi-inverse*/
     384     7641395 :     A[i] = subii(X1[i&mask], X2[i]); /* don't waste time reducing mod N */
     385     7641395 :     W[i+1] = modii(mulii(A[i], W[i]), N);
     386             :   }
     387      247445 :   if (!invmod(W[nbc], N, gl))
     388             :   {
     389         494 :     if (!equalii(N,*gl)) return 2;
     390         462 :     ZV_aff(nbc, X2,X3);
     391         462 :     if (Y3) ZV_aff(nbc, Y2,Y3);
     392         462 :     avma = av; return 1;
     393             :   }
     394             : 
     395     8127479 :   while (i--) /* nbc times */
     396             :   {
     397     7880528 :     pari_sp av2 = avma;
     398     7880528 :     GEN Px = X1[i&mask], Py = Y1[i&mask], Qx = X2[i], Qy = Y2[i];
     399     7880528 :     GEN z = i? mulii(*gl,W[i]): *gl; /*1/(Px-Qx)*/
     400     7880528 :     FpE_add_i(N,z,  Px,Py,Qx,Qy, X3+i, Y3? Y3+i: NULL);
     401     7880528 :     if (!i) break;
     402     7633577 :     avma = av2; *gl = modii(mulii(*gl, A[i]), N);
     403             :   }
     404      246951 :   avma = av; return 0;
     405             : }
     406             : 
     407             : /* Shortcut, for use in cases where Y coordinates follow their corresponding
     408             :  * X coordinates, and first summand doesn't need to be repeated */
     409             : static int
     410      241333 : ecm_elladd(GEN N, GEN *gl, long nbc, GEN *X1, GEN *X2, GEN *X3) {
     411      241333 :   return ecm_elladd0(N, gl, nbc, nbc, X1, X1+nbc, X2, X2+nbc, X3, X3+nbc);
     412             : }
     413             : 
     414             : /* As ecm_elladd except it does twice as many additions (and hides even more
     415             :  * of the cost of the modular inverse); the net effect is the same as
     416             :  * ecm_elladd(nbc,X1,X2,X3) && ecm_elladd(nbc,X4,X5,X6). Safe to
     417             :  * have X2=X3, X5=X6, or X1,X2 coincide with X4,X5 in any order. */
     418             : static int
     419        7334 : ecm_elladd2(GEN N, GEN *gl, long nbc,
     420             :             GEN *X1, GEN *X2, GEN *X3, GEN *X4, GEN *X5, GEN *X6)
     421             : {
     422        7334 :   GEN *Y1 = X1+nbc, *Y2 = X2+nbc, *Y3 = X3+nbc;
     423        7334 :   GEN *Y4 = X4+nbc, *Y5 = X5+nbc, *Y6 = X6+nbc;
     424        7334 :   GEN W[4*nbcmax], *A = W+2*nbc; /* W[0],A[0] unused */
     425             :   long i, j;
     426        7334 :   pari_sp av = avma;
     427             : 
     428        7334 :   W[1] = subii(X1[0], X2[0]);
     429      234352 :   for (i=1; i<nbc; i++)
     430             :   {
     431      227018 :     A[i] = subii(X1[i], X2[i]); /* don't waste time reducing mod N here */
     432      227018 :     W[i+1] = modii(mulii(A[i], W[i]), N);
     433             :   }
     434      241686 :   for (j=0; j<nbc; i++,j++)
     435             :   {
     436      234352 :     A[i] = subii(X4[j], X5[j]);
     437      234352 :     W[i+1] = modii(mulii(A[i], W[i]), N);
     438             :   }
     439        7334 :   if (!invmod(W[2*nbc], N, gl))
     440             :   {
     441          14 :     if (!equalii(N,*gl)) return 2;
     442          14 :     ZV_aff(2*nbc, X2,X3); /* hack: 2*nbc => copy Y2->Y3 */
     443          14 :     ZV_aff(2*nbc, X5,X6); /* also copy Y5->Y6 */
     444          14 :     avma = av; return 1;
     445             :   }
     446             : 
     447      248880 :   while (j--) /* nbc times */
     448             :   {
     449      234240 :     pari_sp av2 = avma;
     450      234240 :     GEN Px = X4[j], Py = Y4[j], Qx = X5[j], Qy = Y5[j];
     451      234240 :     GEN z = mulii(*gl,W[--i]); /*1/(Px-Qx)*/
     452      234240 :     FpE_add_i(N,z, Px,Py, Qx,Qy, X6+j,Y6+j);
     453      234240 :     avma = av2; *gl = modii(mulii(*gl, A[i]), N);
     454             :   }
     455      241560 :   while (i--) /* nbc times */
     456             :   {
     457      234240 :     pari_sp av2 = avma;
     458      234240 :     GEN Px = X1[i], Py = Y1[i], Qx = X2[i], Qy = Y2[i];
     459      234240 :     GEN z = i? mulii(*gl, W[i]): *gl; /*1/(Px-Qx)*/
     460      234240 :     FpE_add_i(N,z, Px,Py, Qx,Qy, X3+i,Y3+i);
     461      234240 :     if (!i) break;
     462      226920 :     avma = av2; *gl = modii(mulii(*gl, A[i]), N);
     463             :   }
     464        7320 :   avma = av; return 0;
     465             : }
     466             : 
     467             : /* Parallel doubling on nbc curves, assigning the result to locations at
     468             :  * and following *X2.  Safe to be called with X2 equal to X1.  Return
     469             :  * value as for ecm_elladd.  If we find a point at infinity mod N,
     470             :  * and if X1 != X2, we copy the points at X1 to X2. */
     471             : static int
     472       42166 : elldouble(GEN N, GEN *gl, long nbc, GEN *X1, GEN *X2)
     473             : {
     474       42166 :   GEN *Y1 = X1+nbc, *Y2 = X2+nbc;
     475             :   GEN W[nbcmax+1]; /* W[0] unused */
     476             :   long i;
     477       42166 :   pari_sp av = avma;
     478       42166 :   /*W[0] = gen_1;*/ W[1] = Y1[0];
     479       42166 :   for (i=1; i<nbc; i++) W[i+1] = modii(mulii(Y1[i], W[i]), N);
     480       42166 :   if (!invmod(W[nbc], N, gl))
     481             :   {
     482           0 :     if (!equalii(N,*gl)) return 2;
     483           0 :     ZV_aff(2*nbc,X1,X2); /* also copies Y1->Y2 */
     484           0 :     avma = av; return 1;
     485             :   }
     486     1334620 :   while (i--) /* nbc times */
     487             :   {
     488             :     pari_sp av2;
     489     1250288 :     GEN v, w, L, z = i? mulii(*gl,W[i]): *gl;
     490     1250288 :     if (i) *gl = modii(mulii(*gl, Y1[i]), N);
     491     1250288 :     av2 = avma;
     492     1250288 :     L = modii(mulii(addui(1, mului(3, Fp_sqr(X1[i],N))), z), N);
     493     1250288 :     if (signe(L)) /* half of zero is still zero */
     494     1250288 :       L = shifti(mod2(L)? addii(L, N): L, -1);
     495     1250288 :     v = modii(subii(sqri(L), shifti(X1[i],1)), N);
     496     1250288 :     w = modii(subii(mulii(L, subii(X1[i], v)), Y1[i]), N);
     497     1250288 :     affii(v, X2[i]);
     498     1250288 :     affii(w, Y2[i]);
     499     1250288 :     avma = av2;
     500             :   }
     501       42166 :   avma = av; return 0;
     502             : }
     503             : 
     504             : /* Parallel multiplication by an odd prime k on nbc curves, storing the
     505             :  * result to locations at and following *X2. Safe to be called with X2 = X1.
     506             :  * Return values as ecm_elladd. Uses (a simplified variant of) Montgomery's
     507             :  * PRAC algorithm; see ftp://ftp.cwi.nl/pub/pmontgom/Lucas.ps.gz .
     508             :  * With thanks to Paul Zimmermann for the reference.  --GN1998Aug13 */
     509             : static int
     510      213753 : get_rule(ulong d, ulong e)
     511             : {
     512      213753 :   if (d <= e + (e>>2)) /* floor(1.25*e) */
     513             :   {
     514       17029 :     if ((d+e)%3 == 0) return 0; /* rule 1 */
     515       10187 :     if ((d-e)%6 == 0) return 1;  /* rule 2 */
     516             :   }
     517             :   /* d <= 4*e but no ofl */
     518      206857 :   if ((d+3)>>2 <= e) return 2; /* rule 3, common case */
     519       12400 :   if ((d&1)==(e&1))  return 1; /* rule 4 = rule 2 */
     520        6442 :   if (!(d&1))        return 3; /* rule 5 */
     521        1783 :   if (d%3 == 0)      return 4; /* rule 6 */
     522         417 :   if ((d+e)%3 == 0)  return 5; /* rule 7 */
     523           0 :   if ((d-e)%3 == 0)  return 6; /* rule 8 */
     524             :   /* when we get here, e is even, otherwise one of rules 4,5 would apply */
     525           0 :   return 7; /* rule 9 */
     526             : }
     527             : 
     528             : /* PRAC implementation notes - main changes against the paper version:
     529             :  * (1) The general function [m+n]P = f([m]P,[n]P,[m-n]P) collapses (for m!=n)
     530             :  * to an ecm_elladd() which does not depend on the third argument; thus
     531             :  * references to the third variable (C in the paper) can be eliminated.
     532             :  * (2) Since our multipliers are prime, the outer loop of the paper
     533             :  * version executes only once, and thus is invisible above.
     534             :  * (3) The first step in the inner loop of the paper version will always be
     535             :  * rule 3, but the addition requested by this rule amounts to a doubling, and
     536             :  * will always be followed by a swap, so we have unrolled this first iteration.
     537             :  * (4) Simplifications in rules 6 and 7 are possible given the above, and we
     538             :  * save one addition in each of the two cases.  NB none of the other
     539             :  * ecm_elladd()s in the loop can ever degenerate into an elldouble.
     540             :  * (5) I tried to optimize for rule 3, which is used more frequently than all
     541             :  * others together, but it didn't improve things, so I removed the nested
     542             :  * tight loop again.  --GN */
     543             : /* The main loop body of ellfacteur() runs _slower_ under PRAC than under a
     544             :  * straightforward left-shift binary multiplication when N has <30 digits and
     545             :  * B1 is small;  PRAC wins when N and B1 get larger.  Weird. --GN */
     546             : /* k>2 assumed prime, XAUX = scratchpad */
     547             : static int
     548       27036 : ellmult(GEN N, GEN *gl, long nbc, ulong k, GEN *X1, GEN *X2, GEN *XAUX)
     549             : {
     550             :   ulong r, d, e, e1;
     551             :   int res;
     552       27036 :   GEN *A = X2, *B = XAUX, *T = XAUX + 2*nbc;
     553             : 
     554       27036 :   ZV_aff(2*nbc,X1,XAUX);
     555             :   /* first doubling picks up X1;  after this we'll be working in XAUX and
     556             :    * X2 only, mostly via A and B and T */
     557       27036 :   if ((res = elldouble(N, gl, nbc, X1, X2)) != 0) return res;
     558             : 
     559             :   /* split the work at the golden ratio */
     560       27036 :   r = (ulong)(k*0.61803398875 + .5);
     561       27036 :   d = k - r;
     562       27036 :   e = r - d; /* d+e == r, so no danger of ofl below */
     563      267629 :   while (d != e)
     564             :   { /* apply one of the nine transformations from PM's Table 4. */
     565      213753 :     switch(get_rule(d,e))
     566             :     {
     567             :     case 0: /* rule 1 */
     568        6842 :       if ( (res = ecm_elladd(N, gl, nbc, A, B, T)) ) return res;
     569        6828 :       if ( (res = ecm_elladd2(N, gl, nbc, T, A, A, T, B, B)) != 0) return res;
     570        6814 :       e1 = d - e; d = (d + e1)/3; e = (e - e1)/3; break;
     571             :     case 1: /* rules 2 and 4 */
     572        6012 :       if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
     573        5991 :       if ( (res = elldouble(N, gl, nbc, A, A)) ) return res;
     574        5991 :       d = (d-e)>>1; break;
     575             :     case 3: /* rule 5 */
     576        4659 :       if ( (res = elldouble(N, gl, nbc, A, A)) ) return res;
     577        4659 :       d >>= 1; break;
     578             :     case 4: /* rule 6 */
     579        1366 :       if ( (res = elldouble(N, gl, nbc, A, T)) ) return res;
     580        1366 :       if ( (res = ecm_elladd(N, gl, nbc, T, A, A)) ) return res;
     581        1366 :       if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
     582        1366 :       d = d/3 - e; break;
     583             :     case 2: /* rule 3 */
     584      194457 :       if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
     585      194310 :       d -= e; break;
     586             :     case 5: /* rule 7 */
     587         417 :       if ( (res = elldouble(N, gl, nbc, A, T)) ) return res;
     588         417 :       if ( (res = ecm_elladd2(N, gl, nbc, T, A, A, T, B, B)) != 0) return res;
     589         417 :       d = (d - 2*e)/3; break;
     590             :     case 6: /* rule 8 */
     591           0 :       if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
     592           0 :       if ( (res = elldouble(N, gl, nbc, A, T)) ) return res;
     593           0 :       if ( (res = ecm_elladd(N, gl, nbc, T, A, A)) ) return res;
     594           0 :       d = (d - e)/3; break;
     595             :     case 7: /* rule 9 */
     596           0 :       if ( (res = elldouble(N, gl, nbc, B, B)) ) return res;
     597           0 :       e >>= 1; break;
     598             :     }
     599             :     /* swap d <-> e and A <-> B if necessary */
     600      213557 :     if (d < e) { lswap(d,e); pswap(A,B); }
     601             :   }
     602       26840 :   return ecm_elladd(N, gl, nbc, XAUX, X2, X2);
     603             : }
     604             : 
     605             : struct ECM {
     606             :   pari_timer T;
     607             :   long nbc, nbc2, seed;
     608             :   GEN *X, *XAUX, *XT, *XD, *XB, *XB2, *XH, *Xh, *Yh;
     609             : };
     610             : 
     611             : /* memory layout in ellfacteur():  a large array of GEN pointers, and one
     612             :  * huge chunk of memory containing all the actual GEN (t_INT) objects.
     613             :  * nbc is constant throughout the invocation:
     614             :  * - The B1 stage of each iteration through the main loop needs little
     615             :  * space:  enough for the X and Y coordinates of the current points,
     616             :  * and twice as much again as scratchpad for ellmult().
     617             :  * - The B2 stage, starting from some current set of points Q, needs, in
     618             :  * succession:
     619             :  *   + space for [2]Q, [4]Q, ..., [10]Q, and [p]Q for building the helix;
     620             :  *   + space for 48*nbc X and Y coordinates to hold the helix.  This could
     621             :  *   re-use [2]Q,...,[8]Q, but only with difficulty, since we don't
     622             :  *   know in advance which residue class mod 210 our p is going to be in.
     623             :  *   It can and should re-use [p]Q, though;
     624             :  *   + space for (temporarily [30]Q and then) [210]Q, [420]Q, and several
     625             :  *   further doublings until the giant step multiplier is reached.  This
     626             :  *   can re-use the remaining cells from above.  The computation of [210]Q
     627             :  *   will have been the last call to ellmult() within this iteration of the
     628             :  *   main loop, so the scratchpad is now also free to be re-used. We also
     629             :  *   compute [630]Q by a parallel addition;  we'll need it later to get the
     630             :  *   baby-step table bootstrapped a little faster.
     631             :  *   + Finally, for no more than 4 curves at a time, room for up to 1024 X
     632             :  *   coordinates only: the Y coordinates needed whilst setting up this baby
     633             :  *   step table are temporarily stored in the upper half, and overwritten
     634             :  *   during the last series of additions.
     635             :  *
     636             :  * Graphically:  after end of B1 stage (X,Y are the coords of Q):
     637             :  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
     638             :  * | X Y |  scratch  | [2]Q| [4]Q| [6]Q| [8]Q|[10]Q|    ...    | ...
     639             :  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
     640             :  * *X    *XAUX *XT   *XD                                       *XB
     641             :  *
     642             :  * [30]Q is computed from [10]Q.  [210]Q can go into XY, etc:
     643             :  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
     644             :  * |[210]|[420]|[630]|[840]|[1680,3360,6720,...,2048*210]      |bstp table...
     645             :  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
     646             :  * *X    *XAUX *XT   *XD      [*XG, somewhere here]            *XB .... *XH
     647             :  *
     648             :  * So we need (13 + 48) * 2 * nbc slots here + 4096 slots for the baby step
     649             :  * table (not all of which will be used when we start with a small B1, but
     650             :  * better to allocate and initialize ahead of time all the slots that might
     651             :  * be needed later).
     652             :  *
     653             :  * Note on memory locality:  During the B2 phase, accesses to the helix
     654             :  * (once it is set up) will be clustered by curves (4 out of nbc at a time).
     655             :  * Accesses to the baby steps table will wander from one end of the array to
     656             :  * the other and back, one such cycle per giant step, and during a full cycle
     657             :  * we would expect on the order of 2E4 accesses when using the largest giant
     658             :  * step size.  Thus we shouldn't be doing too bad with respect to thrashing
     659             :  * a 512KBy L2 cache.  However, we don't want the baby step table to grow
     660             :  * larger than this, even if it would reduce the number of EC operations by a
     661             :  * few more per cent for very large B2, lest cache thrashing slow down
     662             :  * everything disproportionally. --GN */
     663             : /* Auxiliary routines need < (3*nbc+240)*lN words on the PARI stack, in
     664             :  * addition to the spc*(lN+1) words occupied by our main table. */
     665             : static void
     666          71 : ECM_alloc(struct ECM *E, long lN)
     667             : {
     668          71 :   const long bstpmax = 1024; /* max number of baby step table entries */
     669          71 :   long spc = (13 + 48) * E->nbc2 + bstpmax * 4;
     670          71 :   long len = spc + 385 + spc*lN;
     671          71 :   long i, tw = evallg(lN) | evaltyp(t_INT);
     672          71 :   GEN w, *X = (GEN*)new_chunk(len);
     673             :   /* hack for X[i] = cgeti(lN). X = current point in B1 phase */
     674          71 :   w = (GEN)(X + spc + 385);
     675          71 :   for (i = spc-1; i >= 0; i--) { X[i] = w; *w = tw; w += lN; }
     676          71 :   E->X = X;
     677          71 :   E->XAUX = E->X    + E->nbc2; /* scratchpad for ellmult() */
     678          71 :   E->XT   = E->XAUX + E->nbc2; /* ditto, will later hold [3*210]Q */
     679          71 :   E->XD   = E->XT   + E->nbc2; /* room for various multiples */
     680          71 :   E->XB   = E->XD   + 10*E->nbc2; /* start of baby steps table */
     681          71 :   E->XB2  = E->XB   + 2 * bstpmax; /* middle of baby steps table */
     682          71 :   E->XH   = E->XB2  + 2 * bstpmax; /* end of bstps table, start of helix */
     683          71 :   E->Xh   = E->XH   + 48*E->nbc2; /* little helix, X coords */
     684          71 :   E->Yh   = E->XH   + 192;     /* ditto, Y coords */
     685             :   /* XG,YG set inside the main loop, since they depend on B2 */
     686             :   /* E.Xh range of 384 pointers not set; these will later duplicate the pointers
     687             :    * in the E.XH range, 4 curves at a time. Some of the cells reserved here for
     688             :    * the E.XB range will never be used, instead, we'll warp the pointers to
     689             :    * connect to (read-only) GENs in the X/E.XD range */
     690          71 : }
     691             : /* N.B. E->seed is not initialized here */
     692             : static void
     693          71 : ECM_init(struct ECM *E, GEN N, long nbc)
     694             : {
     695          71 :   if (nbc < 0)
     696             :   { /* choose a sensible default */
     697          57 :     const long size = expi(N) + 1;
     698          57 :     nbc = ((size >> 3) << 2) - 80;
     699          57 :     if (nbc < 8) nbc = 8;
     700             :   }
     701          71 :   if (nbc > nbcmax) nbc = nbcmax;
     702          71 :   E->nbc = nbc;
     703          71 :   E->nbc2 = nbc << 1;
     704          71 :   ECM_alloc(E, lgefint(N));
     705          71 : }
     706             : 
     707             : static GEN
     708         121 : ECM_loop(struct ECM *E, GEN N, ulong B1)
     709             : {
     710         121 :   const long MR_foolproof = 16;/* B1 phase, foolproof below 10^12 */
     711         121 :   const long MR_fast = 1; /* B2 phase, not foolproof, 2xfaster */
     712             : /* MR_fast will let thousands of composites slip through, which doesn't
     713             :  * harm ECM; but ellmult() in the B1 phase should only be fed actual primes */
     714         121 :   const ulong B2 = 110 * B1, B2_rt = usqrt(B2);
     715         121 :   const ulong nbc = E->nbc, nbc2 = E->nbc2;
     716             :   pari_sp av1, avtmp;
     717         121 :   byteptr d0, d = diffptr;
     718             :   long i, gse, gss, bstp, bstp0, rcn0, rcn;
     719             :   ulong B2_p, m, p, p0;
     720             :   GEN g, *XG, *YG;
     721         121 :   GEN *X = E->X, *XAUX = E->XAUX, *XT = E->XT, *XD = E->XD;
     722         121 :   GEN *XB = E->XB, *XB2 = E->XB2, *XH = E->XH, *Xh = E->Xh, *Yh = E->Yh;
     723             :   /* pick curves */
     724         121 :   for (i = nbc2; i--; ) affui(E->seed++, X[i]);
     725             :   /* pick giant step exponent and size */
     726         121 :   gse = B1 < 656
     727             :           ? (B1 < 200? 5: 6)
     728             :           : (B1 < 10500
     729             :             ? (B1 < 2625? 7: 8)
     730             :             : (B1 < 42000? 9: 10));
     731         121 :   gss = 1UL << gse;
     732             :   /* With 32 baby steps, a giant step corresponds to 32*420 = 13440,
     733             :    * appropriate for the smallest B2s. With 1024, a giant step will be 430080;
     734             :    * appropriate for B1 >~ 42000, where 512 baby steps would imply roughly
     735             :    * the same number of curve additions. */
     736         121 :   XG = XT + gse*nbc2; /* will later hold [2^(gse+1)*210]Q */
     737         121 :   YG = XG + nbc;
     738             : 
     739         121 :   if (DEBUGLEVEL >= 4) {
     740           0 :     err_printf("ECM: time = %6ld ms\nECM: B1 = %4lu,", timer_delay(&E->T), B1);
     741           0 :     err_printf("\tB2 = %6lu,\tgss = %4ld*420\n", B2, gss);
     742             :   }
     743         121 :   p = 0;
     744         121 :   NEXT_PRIME_VIADIFF(p,d);
     745             : 
     746             :   /* ---B1 PHASE--- */
     747             :   /* treat p=2 separately */
     748         121 :   B2_p = B2 >> 1;
     749        1995 :   for (m=1; m<=B2_p; m<<=1)
     750             :   {
     751        1874 :     int fl = elldouble(N, &g, nbc, X, X);
     752        1874 :     if (fl > 1) return g; else if (fl) break;
     753             :   }
     754         121 :   rcn = NPRC; /* multipliers begin at the beginning */
     755             :   /* p=3,...,nextprime(B1) */
     756        7226 :   while (p < B1 && p <= B2_rt)
     757             :   {
     758        6998 :     pari_sp av2 = avma;
     759        6998 :     p = snextpr(p, &d, &rcn, NULL, MR_foolproof);
     760        6998 :     B2_p = B2/p; /* beware integer overflow on 32-bit CPUs */
     761       23680 :     for (m=1; m<=B2_p; m*=p)
     762             :     {
     763       16864 :       int fl = ellmult(N, &g, nbc, p, X, X, XAUX);
     764       16864 :       if (fl > 1) return g; else if (fl) break;
     765       16682 :       avma = av2;
     766             :     }
     767        6984 :     avma = av2;
     768             :   }
     769             :   /* primes p larger than sqrt(B2) appear only to the 1st power */
     770       10101 :   while (p < B1)
     771             :   {
     772        9905 :     pari_sp av2 = avma;
     773        9905 :     p = snextpr(p, &d, &rcn, NULL, MR_foolproof);
     774        9905 :     if (ellmult(N, &g, nbc, p, X, X, XAUX) > 1) return g;
     775        9887 :     avma = av2;
     776             :   }
     777          89 :   if (DEBUGLEVEL >= 4) {
     778           0 :     err_printf("ECM: time = %6ld ms, B1 phase done, ", timer_delay(&E->T));
     779           0 :     err_printf("p = %lu, setting up for B2\n", p);
     780             :   }
     781             : 
     782             :   /* ---B2 PHASE--- */
     783             :   /* compute [2]Q,...,[10]Q, needed to build the helix */
     784          89 :   if (elldouble(N, &g, nbc, X, XD) > 1) return g; /*[2]Q*/
     785          89 :   if (elldouble(N, &g, nbc, XD, XD + nbc2) > 1) return g; /*[4]Q*/
     786         178 :   if (ecm_elladd(N, &g, nbc,
     787          89 :         XD, XD + nbc2, XD + (nbc<<2)) > 1) return g; /* [6]Q */
     788         178 :   if (ecm_elladd2(N, &g, nbc,
     789             :         XD, XD + (nbc<<2), XT + (nbc<<3),
     790          89 :         XD + nbc2, XD + (nbc<<2), XD + (nbc<<3)) > 1)
     791           0 :     return g; /* [8]Q and [10]Q */
     792          89 :   if (DEBUGLEVEL >= 7) err_printf("\t(got [2]Q...[10]Q)\n");
     793             : 
     794             :   /* get next prime (still using the foolproof test) */
     795          89 :   p = snextpr(p, &d, &rcn, NULL, MR_foolproof);
     796             :   /* make sure we have the residue class number (mod 210) */
     797          89 :   if (rcn == NPRC)
     798             :   {
     799          89 :     rcn = prc210_no[(p % 210) >> 1];
     800          89 :     if (rcn == NPRC)
     801             :     {
     802           0 :       err_printf("ECM: %lu should have been prime but isn\'t\n", p);
     803           0 :       pari_err_BUG("ellfacteur");
     804             :     }
     805             :   }
     806             : 
     807             :   /* compute [p]Q and put it into its place in the helix */
     808          89 :   if (ellmult(N, &g, nbc, p, X, XH + rcn*nbc2, XAUX) > 1)
     809           0 :     return g;
     810          89 :   if (DEBUGLEVEL >= 7)
     811           0 :     err_printf("\t(got [p]Q, p = %lu = prc210_rp[%ld] mod 210)\n", p, rcn);
     812             : 
     813             :   /* save current p, d, and rcn;  we'll need them more than once below */
     814          89 :   p0 = p;
     815          89 :   d0 = d;
     816          89 :   rcn0 = rcn; /* remember where the helix wraps */
     817          89 :   bstp0 = 0; /* p is at baby-step offset 0 from itself */
     818             : 
     819             :   /* fill up the helix, stepping forward through the prime residue classes
     820             :    * mod 210 until we're back at the r'class of p0.  Keep updating p so
     821             :    * that we can print meaningful diagnostics if a factor shows up; don't
     822             :    * bother checking which of these p's are in fact prime */
     823        4272 :   for (i = 47; i; i--) /* 47 iterations */
     824             :   {
     825        4183 :     ulong dp = (ulong)prc210_d1[rcn];
     826        4183 :     p += dp;
     827        4183 :     if (rcn == 47)
     828             :     { /* wrap mod 210 */
     829         178 :       if (ecm_elladd(N, &g, nbc,
     830         178 :             XT+dp*nbc, XH+rcn*nbc2, XH) > 1) return g;
     831          89 :       rcn = 0; continue;
     832             :     }
     833       12282 :     if (ecm_elladd(N, &g, nbc,
     834       12282 :           XT+dp*nbc, XH+rcn*nbc2, XH+rcn*nbc2+nbc2) > 1)
     835           0 :       return g;
     836        4094 :     rcn++;
     837             :   }
     838          89 :   if (DEBUGLEVEL >= 7) err_printf("\t(got initial helix)\n");
     839             :   /* compute [210]Q etc, needed for the baby step table */
     840          89 :   if (ellmult(N, &g, nbc, 3, XD + (nbc<<3), X, XAUX) > 1)
     841           0 :     return g;
     842          89 :   if (ellmult(N, &g, nbc, 7, X, X, XAUX) > 1)
     843           0 :     return g; /* [210]Q */
     844             :   /* this was the last call to ellmult() in the main loop body; may now
     845             :    * overwrite XAUX and slots XD and following */
     846          89 :   if (elldouble(N, &g, nbc, X, XAUX) > 1) return g; /* [420]Q */
     847          89 :   if (ecm_elladd(N, &g, nbc, X, XAUX, XT) > 1) return g;/*[630]Q*/
     848          89 :   if (ecm_elladd(N, &g, nbc, X, XT, XD) > 1) return g;  /*[840]Q*/
     849         645 :   for (i=1; i <= gse; i++)
     850         556 :     if (elldouble(N, &g, nbc, XT + i*nbc2, XD + i*nbc2) > 1)
     851           0 :       return g;
     852             :   /* (the last iteration has initialized XG to [210*2^(gse+1)]Q) */
     853             : 
     854          89 :   if (DEBUGLEVEL >= 4)
     855           0 :     err_printf("ECM: time = %6ld ms, entering B2 phase, p = %lu\n",
     856             :                timer_delay(&E->T), p);
     857             : 
     858         433 :   for (i = nbc - 4; i >= 0; i -= 4)
     859             :   { /* loop over small sets of 4 curves at a time */
     860             :     GEN *Xb;
     861             :     long j, k;
     862         351 :     if (DEBUGLEVEL >= 6)
     863           0 :       err_printf("ECM: finishing curves %ld...%ld\n", i, i+3);
     864             :     /* Copy relevant pointers from XH to Xh. Memory layout in XH:
     865             :      * nbc X coordinates, nbc Y coordinates for residue class
     866             :      * 1 mod 210, then the same for r.c. 11 mod 210, etc. Memory layout for
     867             :      * Xh is: four X coords for 1 mod 210, four for 11 mod 210, ..., four
     868             :      * for 209 mod 210, then the corresponding Y coordinates in the same
     869             :      * order. This allows a giant step on Xh using just three calls to
     870             :      * ecm_elladd0() each acting on 64 points in parallel */
     871       17550 :     for (j = 48; j--; )
     872             :     {
     873       16848 :       k = nbc2*j + i;
     874       16848 :       m = j << 2; /* X coordinates */
     875       16848 :       Xh[m]   = XH[k];   Xh[m+1] = XH[k+1];
     876       16848 :       Xh[m+2] = XH[k+2]; Xh[m+3] = XH[k+3];
     877       16848 :       k += nbc; /* Y coordinates */
     878       16848 :       Yh[m]   = XH[k];   Yh[m+1] = XH[k+1];
     879       16848 :       Yh[m+2] = XH[k+2]; Yh[m+3] = XH[k+3];
     880             :     }
     881             :     /* Build baby step table of X coords of multiples of [210]Q.  XB[4*j]
     882             :      * will point at X coords on four curves from [(j+1)*210]Q.  Until
     883             :      * we're done, we need some Y coords as well, which we keep in the
     884             :      * second half of the table, overwriting them at the end when gse=10.
     885             :      * Multiples which we already have  (by 1,2,3,4,8,16,...,2^gse) are
     886             :      * entered simply by copying the pointers, ignoring the few slots in w
     887             :      * that were initially reserved for them. Here are the initial entries */
     888        1053 :     for (Xb=XB,k=2,j=i; k--; Xb=XB2,j+=nbc) /* first X, then Y coords */
     889             :     {
     890         702 :       Xb[0]  = X[j];      Xb[1]  = X[j+1]; /* [210]Q */
     891         702 :       Xb[2]  = X[j+2];    Xb[3]  = X[j+3];
     892         702 :       Xb[4]  = XAUX[j];   Xb[5]  = XAUX[j+1]; /* [420]Q */
     893         702 :       Xb[6]  = XAUX[j+2]; Xb[7]  = XAUX[j+3];
     894         702 :       Xb[8]  = XT[j];     Xb[9]  = XT[j+1]; /* [630]Q */
     895         702 :       Xb[10] = XT[j+2];   Xb[11] = XT[j+3];
     896         702 :       Xb += 4; /* points at [420]Q */
     897             :       /* ... entries at powers of 2 times 210 .... */
     898        4309 :       for (m = 2; m < (ulong)gse+k; m++) /* omit Y coords of [2^gse*210]Q */
     899             :       {
     900        3607 :         long m2 = m*nbc2 + j;
     901        3607 :         Xb += (2UL<<m); /* points at [2^m*210]Q */
     902        3607 :         Xb[0] = XAUX[m2];   Xb[1] = XAUX[m2+1];
     903        3607 :         Xb[2] = XAUX[m2+2]; Xb[3] = XAUX[m2+3];
     904             :       }
     905             :     }
     906         351 :     if (DEBUGLEVEL >= 7)
     907           0 :       err_printf("\t(extracted precomputed helix / baby step entries)\n");
     908             :     /* ... glue in between, up to 16*210 ... */
     909         351 :     if (ecm_elladd0(N, &g, 12, 4, /* 12 pts + (4 pts replicated thrice) */
     910             :           XB + 12, XB2 + 12,
     911             :           XB,      XB2,
     912           0 :           XB + 16, XB2 + 16) > 1) return g; /*4+{1,2,3} = {5,6,7}*/
     913         351 :     if (ecm_elladd0(N, &g, 28, 4, /* 28 pts + (4 pts replicated 7fold) */
     914             :           XB + 28, XB2 + 28,
     915             :           XB,      XB2,
     916           0 :           XB + 32, XB2 + 32) > 1) return g;/*8+{1...7} = {9...15}*/
     917             :     /* ... and the remainder of the lot */
     918        1277 :     for (m = 5; m <= (ulong)gse; m++)
     919             :     { /* fill in from 2^(m-1)+1 to 2^m-1 in chunks of 64 and 60 points */
     920         926 :       ulong m2 = 2UL << m; /* will point at 2^(m-1)+1 */
     921        2005 :       for (j = 0; (ulong)j < m2-64; j+=64) /* executed 0 times when m = 5 */
     922             :       {
     923        6716 :         if (ecm_elladd0(N, &g, 64, 4,
     924        2148 :               XB + m2-4, XB2 + m2-4,
     925        2158 :               XB + j,    XB2 + j,
     926        2410 :               XB + m2+j, (m<(ulong)gse? XB2+m2+j: NULL)) > 1)
     927           0 :           return g;
     928             :       } /* j = m2-64 here, 60 points left */
     929        6103 :       if (ecm_elladd0(N, &g, 60, 4,
     930        1824 :             XB + m2-4, XB2 + m2-4,
     931        1852 :             XB + j,    XB2 + j,
     932        2427 :             XB + m2+j, (m<(ulong)gse? XB2+m2+j: NULL)) > 1)
     933           0 :         return g;
     934             :       /* when m=gse, drop Y coords of result, and when both equal 1024,
     935             :        * overwrite Y coords of second argument with X coords of result */
     936             :     }
     937         351 :     if (DEBUGLEVEL >= 7) err_printf("\t(baby step table complete)\n");
     938             :     /* initialize a few other things */
     939         351 :     bstp = bstp0;
     940         351 :     p = p0; d = d0; rcn = rcn0;
     941         351 :     g = gen_1; av1 = avma;
     942             :     /* scratchspace for prod (x_i-x_j) */
     943         351 :     avtmp = (pari_sp)new_chunk(8 * lgefint(N));
     944             :     /* The correct entry in XB to use depends on bstp and on where we are
     945             :      * on the helix. As we skip from prime to prime, bstp is incremented
     946             :      * by snextpr each time we wrap around through residue class number 0
     947             :      * (1 mod 210), but the baby step should not be taken until rcn>=rcn0,
     948             :      * i.e. until we pass again the residue class of p0.
     949             :      *
     950             :      * The correct signed multiplier is thus k = bstp - (rcn < rcn0),
     951             :      * and the offset from XB is four times (|k| - 1).  When k=0, we ignore
     952             :      * the current prime: if it had led to a factorization, this
     953             :      * would have been noted during the last giant step, or -- when we
     954             :      * first get here -- whilst initializing the helix.  When k > gss,
     955             :      * we must do a giant step and bump bstp back by -2*gss.
     956             :      *
     957             :      * The gcd of the product of X coord differences against N is taken just
     958             :      * before we do a giant step. */
     959     4565651 :     while (p < B2)
     960             :     {/* loop over probable primes p0 < p <= nextprime(B2), inserting giant
     961             :       * steps as necessary */
     962     4564956 :       p = snextpr(p, &d, &rcn, &bstp, MR_fast); /* next probable prime */
     963             :       /* work out the corresponding baby-step multiplier */
     964     4564956 :       k = bstp - (rcn < rcn0 ? 1 : 0);
     965     4564956 :       if (k > gss)
     966             :       { /* giant-step time, take gcd */
     967        1142 :         g = gcdii(g, N);
     968        1142 :         if (!is_pm1(g) && !equalii(g, N)) return g;
     969        1135 :         g = gen_1; avma = av1;
     970        3405 :         while (k > gss)
     971             :         { /* giant step */
     972        1135 :           if (DEBUGLEVEL >= 7) err_printf("\t(giant step at p = %lu)\n", p);
     973        1135 :           if (ecm_elladd0(N, &g, 64, 4, XG + i, YG + i,
     974           0 :                 Xh, Yh, Xh, Yh) > 1) return g;
     975        1135 :           if (ecm_elladd0(N, &g, 64, 4, XG + i, YG + i,
     976             :                 Xh + 64, Yh + 64, Xh + 64, Yh + 64) > 1)
     977           0 :             return g;
     978        1135 :           if (ecm_elladd0(N, &g, 64, 4, XG + i, YG + i,
     979             :                 Xh + 128, Yh + 128, Xh + 128, Yh + 128) > 1)
     980           0 :             return g;
     981        1135 :           bstp -= (gss << 1);
     982        1135 :           k = bstp - (rcn < rcn0? 1: 0); /* recompute multiplier */
     983             :         }
     984             :       }
     985     4564949 :       if (!k) continue; /* point of interest is already in Xh */
     986     4538548 :       if (k < 0) k = -k;
     987     4538548 :       m = ((ulong)k - 1) << 2;
     988             :       /* accumulate product of differences of X coordinates */
     989     4538548 :       j = rcn<<2;
     990     4538548 :       avma = avtmp; /* go to garbage zone */
     991     4538548 :       g = modii(mulii(g, subii(XB[m],   Xh[j])), N);
     992     4538548 :       g = modii(mulii(g, subii(XB[m+1], Xh[j+1])), N);
     993     4538548 :       g = modii(mulii(g, subii(XB[m+2], Xh[j+2])), N);
     994     4538548 :       g = mulii(g, subii(XB[m+3], Xh[j+3]));
     995     4538548 :       avma = av1;
     996     4538548 :       g = modii(g, N);
     997             :     }
     998         344 :     avma = av1;
     999             :   }
    1000          82 :   return NULL;
    1001             : }
    1002             : 
    1003             : /* ellfacteur() tuned to be useful as a first stage before MPQS, especially for
    1004             :  * large arguments, when 'insist' is false, and now also for the case when
    1005             :  * 'insist' is true, vaguely following suggestions by Paul Zimmermann
    1006             :  * (http://www.loria.fr/~zimmerma/records/ecmnet.html). --GN 1998Jul,Aug */
    1007             : static GEN
    1008         393 : ellfacteur(GEN N, int insist)
    1009             : {
    1010         393 :   const long size = expi(N) + 1;
    1011         393 :   pari_sp av = avma;
    1012             :   struct ECM E;
    1013         393 :   long nbc, dsn, dsnmax, rep = 0;
    1014         393 :   if (insist)
    1015             :   {
    1016          14 :     const long DSNMAX = numberof(TB1)-1;
    1017          14 :     dsnmax = (size >> 2) - 10;
    1018          14 :     if (dsnmax < 0) dsnmax = 0;
    1019           0 :     else if (dsnmax > DSNMAX) dsnmax = DSNMAX;
    1020          14 :     E.seed = 1 + (nbcmax<<7)*(size&0xffff); /* seed for choice of curves */
    1021             : 
    1022          14 :     dsn = (size >> 3) - 5;
    1023          14 :     if (dsn < 0) dsn = 0; else if (dsn > 47) dsn = 47;
    1024             :     /* pick up the torch where non-insistent stage would have given up */
    1025          14 :     nbc = dsn + (dsn >> 2) + 9; /* 8 or more curves in parallel */
    1026          14 :     nbc &= ~3; /* 4 | nbc */
    1027             :   }
    1028             :   else
    1029             :   {
    1030         379 :     dsn = (size - 140) >> 3;
    1031         379 :     if (dsn < 0)
    1032             :     {
    1033             : #ifndef __EMX__ /* unless DOS/EMX: MPQS's disk access is abysmally slow */
    1034         322 :       if (DEBUGLEVEL >= 4)
    1035           0 :         err_printf("ECM: number too small to justify this stage\n");
    1036         322 :       return NULL; /* too small, decline the task */
    1037             : #endif
    1038             :       dsn = 0;
    1039          57 :     } else if (dsn > 12) dsn = 12;
    1040          57 :     rep = (size <= 248 ?
    1041          75 :            (size <= 176 ? (size - 124) >> 4 : (size - 148) >> 3) :
    1042          18 :            (size - 224) >> 1);
    1043             : #ifdef __EMX__ /* DOS/EMX: extra rounds (shun MPQS) */
    1044             :     rep += 20;
    1045             : #endif
    1046          57 :     dsnmax = 72;
    1047             :     /* Use disjoint sets of curves for non-insist and insist phases; moreover,
    1048             :      * repeated calls acting on factors of the same original number should try
    1049             :      * to use fresh curves. The following achieves this */
    1050          57 :     E.seed = 1 + (nbcmax<<3)*(size & 0xf);
    1051          57 :     nbc = -1;
    1052             :   }
    1053          71 :   ECM_init(&E, N, nbc);
    1054          71 :   if (DEBUGLEVEL >= 4)
    1055             :   {
    1056           0 :     timer_start(&E.T);
    1057           0 :     err_printf("ECM: working on %ld curves at a time; initializing", E.nbc);
    1058           0 :     if (!insist)
    1059             :     {
    1060           0 :       if (rep == 1) err_printf(" for one round");
    1061           0 :       else          err_printf(" for up to %ld rounds", rep);
    1062             :     }
    1063           0 :     err_printf("...\n");
    1064             :   }
    1065          71 :   if (dsn > dsnmax) dsn = dsnmax;
    1066             :   for(;;)
    1067             :   {
    1068         121 :     ulong B1 = insist? TB1[dsn]: TB1_for_stage[dsn];
    1069         121 :     GEN g = ECM_loop(&E, N, B1);
    1070         121 :     if (g)
    1071             :     {
    1072          39 :       if (DEBUGLEVEL >= 4)
    1073           0 :         err_printf("ECM: time = %6ld ms\n\tfound factor = %Ps\n",
    1074             :                    timer_delay(&E.T), g);
    1075          39 :       return gerepilecopy(av, g);
    1076             :     }
    1077          82 :     if (dsn < dsnmax)
    1078             :     {
    1079          68 :       if (insist) dsn++;
    1080          68 :       else { dsn += 2; if (dsn > dsnmax) dsn = dsnmax; }
    1081             :     }
    1082          82 :     if (!insist && !--rep)
    1083             :     {
    1084          32 :       if (DEBUGLEVEL >= 4)
    1085           0 :         err_printf("ECM: time = %6ld ms,\tellfacteur giving up.\n",
    1086             :                    timer_delay(&E.T));
    1087          32 :       avma = av; return NULL;
    1088             :     }
    1089          50 :   }
    1090             : }
    1091             : /* assume rounds >= 1, seed >= 1, B1 <= ULONG_MAX / 110 */
    1092             : GEN
    1093           0 : Z_ECM(GEN N, long rounds, long seed, ulong B1)
    1094             : {
    1095           0 :   pari_sp av = avma;
    1096             :   struct ECM E;
    1097             :   long i;
    1098           0 :   E.seed = seed;
    1099           0 :   ECM_init(&E, N, -1);
    1100           0 :   if (DEBUGLEVEL >= 4) timer_start(&E.T);
    1101           0 :   for (i = rounds; i--; )
    1102             :   {
    1103           0 :     GEN g = ECM_loop(&E, N, B1);
    1104           0 :     if (g) return gerepilecopy(av, g);
    1105             :   }
    1106           0 :   avma = av; return NULL;
    1107             : }
    1108             : 
    1109             : /***********************************************************************/
    1110             : /**                                                                   **/
    1111             : /**                FACTORIZATION (Pollard-Brent rho) --GN1998Jun18-26 **/
    1112             : /**  pollardbrent() returns a nontrivial factor of n, assuming n is   **/
    1113             : /**  composite and has no small prime divisor, or NULL if going on    **/
    1114             : /**  would take more time than we want to spend.  Sometimes it finds  **/
    1115             : /**  more than one factor, and returns a structure suitable for       **/
    1116             : /**  interpretation by ifac_crack. (Cf Algo 8.5.2 in ACiCNT)          **/
    1117             : /**                                                                   **/
    1118             : /***********************************************************************/
    1119             : #define VALUE(x) gel(x,0)
    1120             : #define EXPON(x) gel(x,1)
    1121             : #define CLASS(x) gel(x,2)
    1122             : 
    1123             : INLINE void
    1124       32901 : INIT(GEN x, GEN v, GEN e, GEN c) {
    1125       32901 :   VALUE(x) = v;
    1126       32901 :   EXPON(x) = e;
    1127       32901 :   CLASS(x) = c;
    1128       32901 : }
    1129             : static void
    1130       29007 : ifac_delete(GEN x) { INIT(x,NULL,NULL,NULL); }
    1131             : 
    1132             : static void
    1133           0 : rho_dbg(pari_timer *T, long c, long msg_mask)
    1134             : {
    1135           0 :   if (c & msg_mask) return;
    1136           0 :   err_printf("Rho: time = %6ld ms,\t%3ld round%s\n",
    1137             :              timer_delay(T), c, (c==1?"":"s"));
    1138             : }
    1139             : 
    1140             : static void
    1141    29092195 : one_iter(GEN *x, GEN *P, GEN x1, GEN n, long delta)
    1142             : {
    1143    29092195 :   *x = addis(remii(sqri(*x), n), delta);
    1144    29052346 :   *P = modii(mulii(*P, subii(x1, *x)), n);
    1145    29101522 : }
    1146             : /* Return NULL when we run out of time, or a single t_INT containing a
    1147             :  * nontrivial factor of n, or a vector of t_INTs, each triple of successive
    1148             :  * entries containing a factor, an exponent (equal to one),  and a factor
    1149             :  * class (NULL for unknown or zero for known composite),  matching the
    1150             :  * internal representation used by the ifac_*() routines below. Repeated
    1151             :  * factors may arise; the caller will sort the factors anyway. Result
    1152             :  * is not gerepile-able (contains NULL) */
    1153             : static GEN
    1154        3387 : pollardbrent_i(GEN n, long size, long c0, long retries)
    1155             : {
    1156        3387 :   long tf = lgefint(n), delta, msg_mask, c, k, k1, l;
    1157             :   pari_sp av;
    1158             :   GEN x, x1, y, P, g, g1, res;
    1159             :   pari_timer T;
    1160             : 
    1161        3387 :   if (DEBUGLEVEL >= 4) timer_start(&T);
    1162        3387 :   c = c0 << 5; /* 2^5 iterations per round */
    1163        6774 :   msg_mask = (size >= 448? 0x1fff:
    1164        3387 :                            (size >= 192? (256L<<((size-128)>>6))-1: 0xff));
    1165        3387 :   y = cgeti(tf);
    1166        3387 :   x1= cgeti(tf);
    1167        3387 :   av = avma;
    1168             : 
    1169             : PB_RETRY:
    1170             :  /* trick to make a 'random' choice determined by n.  Don't use x^2+0 or
    1171             :   * x^2-2, ever.  Don't use x^2-3 or x^2-7 with a starting value of 2.
    1172             :   * x^2+4, x^2+9 are affine conjugate to x^2+1, so don't use them either.
    1173             :   *
    1174             :   * (the point being that when we get called again on a composite cofactor
    1175             :   * of something we've already seen, we had better avoid the same delta) */
    1176        3387 :   switch ((size + retries) & 7)
    1177             :   {
    1178         673 :     case 0:  delta=  1; break;
    1179         428 :     case 1:  delta= -1; break;
    1180         571 :     case 2:  delta=  3; break;
    1181         245 :     case 3:  delta=  5; break;
    1182         420 :     case 4:  delta= -5; break;
    1183         343 :     case 5:  delta=  7; break;
    1184         329 :     case 6:  delta= 11; break;
    1185             :     /* case 7: */
    1186         378 :     default: delta=-11; break;
    1187             :   }
    1188        3387 :   if (DEBUGLEVEL >= 4)
    1189             :   {
    1190           0 :     if (!retries)
    1191           0 :       err_printf("Rho: searching small factor of %ld-bit integer\n", size);
    1192             :     else
    1193           0 :       err_printf("Rho: restarting for remaining rounds...\n");
    1194           0 :     err_printf("Rho: using X^2%+1ld for up to %ld rounds of 32 iterations\n",
    1195             :                delta, c >> 5);
    1196             :   }
    1197        3387 :   x = gen_2; P = gen_1; g1 = NULL; k = 1; l = 1;
    1198        3387 :   affui(2, y);
    1199        3387 :   affui(2, x1);
    1200             :   for (;;) /* terminated under the control of c */
    1201             :   { /* use the polynomial  x^2 + delta */
    1202    13596888 :     one_iter(&x, &P, x1, n, delta);
    1203             : 
    1204    13596906 :     if ((--c & 0x1f)==0)
    1205             :     { /* one round complete */
    1206      421796 :       g = gcdii(n, P); if (!is_pm1(g)) goto fin;
    1207      420544 :       if (c <= 0)
    1208             :       { /* getting bored */
    1209        1517 :         if (DEBUGLEVEL >= 4)
    1210           0 :           err_printf("Rho: time = %6ld ms,\tPollard-Brent giving up.\n",
    1211             :                      timer_delay(&T));
    1212        1517 :         return NULL;
    1213             :       }
    1214      419027 :       P = gen_1;
    1215      419027 :       if (DEBUGLEVEL >= 4) rho_dbg(&T, c0-(c>>5), msg_mask);
    1216      419027 :       affii(x,y); x = y; avma = av;
    1217             :     }
    1218             : 
    1219    13594135 :     if (--k) continue; /* normal end of loop body */
    1220             : 
    1221       30640 :     if (c & 0x1f) /* otherwise, we already checked */
    1222             :     {
    1223       20322 :       g = gcdii(n, P); if (!is_pm1(g)) goto fin;
    1224       20301 :       P = gen_1;
    1225             :     }
    1226             : 
    1227             :    /* Fast forward phase, doing l inner iterations without computing gcds.
    1228             :     * Check first whether it would take us beyond the alloted time.
    1229             :     * Fast forward rounds count only half (although they're taking
    1230             :     * more like 2/3 the time of normal rounds).  This to counteract the
    1231             :     * nuisance that all c0 between 4096 and 6144 would act exactly as
    1232             :     * 4096;  with the halving trick only the range 4096..5120 collapses
    1233             :     * (similarly for all other powers of two) */
    1234       30619 :     if ((c -= (l>>1)) <= 0)
    1235             :     { /* got bored */
    1236         613 :       if (DEBUGLEVEL >= 4)
    1237           0 :         err_printf("Rho: time = %6ld ms,\tPollard-Brent giving up.\n",
    1238             :                    timer_delay(&T));
    1239         613 :       return NULL;
    1240             :     }
    1241       30006 :     c &= ~0x1f; /* keep it on multiples of 32 */
    1242             : 
    1243             :     /* Fast forward loop */
    1244       30006 :     affii(x, x1); avma = av; x = x1;
    1245       30006 :     k = l; l <<= 1;
    1246             :     /* don't show this for the first several (short) fast forward phases. */
    1247       30006 :     if (DEBUGLEVEL >= 4 && (l>>7) > msg_mask)
    1248           0 :       err_printf("Rho: fast forward phase (%ld rounds of 64)...\n", l>>7);
    1249    15543053 :     for (k1=k; k1; k1--)
    1250             :     {
    1251    15513047 :       one_iter(&x, &P, x1, n, delta);
    1252    15511915 :       if ((k1 & 0x1f) == 0) gerepileall(av, 2, &x, &P);
    1253             :     }
    1254       30006 :     if (DEBUGLEVEL >= 4 && (l>>7) > msg_mask)
    1255           0 :       err_printf("Rho: time = %6ld ms,\t%3ld rounds, back to normal mode\n",
    1256           0 :                  timer_delay(&T), c0-(c>>5));
    1257       30006 :     affii(x,y); avma = av; x = y;
    1258    13593501 :   } /* forever */
    1259             : 
    1260             : fin:
    1261             :   /* An accumulated gcd was > 1 */
    1262        1257 :   if  (!equalii(g,n))
    1263             :   { /* if it isn't n, and looks prime, return it */
    1264        1131 :     if (MR_Jaeschke(g))
    1265             :     {
    1266        1124 :       if (DEBUGLEVEL >= 4)
    1267             :       {
    1268           0 :         rho_dbg(&T, c0-(c>>5), 0);
    1269           0 :         err_printf("\tfound factor = %Ps\n",g);
    1270             :       }
    1271        1124 :       return g;
    1272             :     }
    1273           7 :     avma = av; g1 = icopy(g);  /* known composite, keep it safe */
    1274           7 :     av = avma;
    1275             :   }
    1276         126 :   else g1 = n; /* and work modulo g1 for backtracking */
    1277             : 
    1278             :   /* Here g1 is known composite */
    1279         133 :   if (DEBUGLEVEL >= 4 && size > 192)
    1280           0 :     err_printf("Rho: hang on a second, we got something here...\n");
    1281         133 :   x = y;
    1282             :   for(;;)
    1283             :   { /* backtrack until period recovered. Must terminate */
    1284       10066 :     x = addis(remii(sqri(x), g1), delta);
    1285       10066 :     g = gcdii(subii(x1, x), g1); if (!is_pm1(g)) break;
    1286             : 
    1287        9933 :     if (DEBUGLEVEL >= 4 && (--c & 0x1f) == 0) rho_dbg(&T, c0-(c>>5), msg_mask);
    1288        9933 :   }
    1289             : 
    1290         133 :   if (g1 == n || equalii(g,g1))
    1291             :   {
    1292         126 :     if (g1 == n && equalii(g,g1))
    1293             :     { /* out of luck */
    1294           0 :       if (DEBUGLEVEL >= 4)
    1295             :       {
    1296           0 :         rho_dbg(&T, c0-(c>>5), 0);
    1297           0 :         err_printf("\tPollard-Brent failed.\n");
    1298             :       }
    1299           0 :       if (++retries >= 4) pari_err_BUG("");
    1300           0 :       goto PB_RETRY;
    1301             :     }
    1302             :     /* half lucky: we've split n, but g1 equals either g or n */
    1303         126 :     if (DEBUGLEVEL >= 4)
    1304             :     {
    1305           0 :       rho_dbg(&T, c0-(c>>5), 0);
    1306           0 :       err_printf("\tfound %sfactor = %Ps\n", (g1!=n ? "composite " : ""), g);
    1307             :     }
    1308         126 :     res = cgetg(7, t_VEC);
    1309             :     /* g^1: known composite when g1!=n */
    1310         126 :     INIT(res+1, g, gen_1, (g1!=n? gen_0: NULL));
    1311             :     /* cofactor^1: status unknown */
    1312         126 :     INIT(res+4, diviiexact(n,g), gen_1, NULL);
    1313         126 :     return res;
    1314             :   }
    1315             :   /* g < g1 < n : our lucky day -- we've split g1, too */
    1316           7 :   res = cgetg(10, t_VEC);
    1317             :   /* unknown status for all three factors */
    1318           7 :   INIT(res+1, g,                gen_1, NULL);
    1319           7 :   INIT(res+4, diviiexact(g1,g), gen_1, NULL);
    1320           7 :   INIT(res+7, diviiexact(n,g1), gen_1, NULL);
    1321           7 :   if (DEBUGLEVEL >= 4)
    1322             :   {
    1323           0 :     rho_dbg(&T, c0-(c>>5), 0);
    1324           0 :     err_printf("\tfound factors = %Ps, %Ps,\n\tand %Ps\n",
    1325           0 :                gel(res,1), gel(res,4), gel(res,7));
    1326             :   }
    1327           7 :   return res;
    1328             : }
    1329             : /* Tuning parameter:  for input up to 64 bits long, we must not spend more
    1330             :  * than a very short time, for fear of slowing things down on average.
    1331             :  * With the current tuning formula, increase our efforts somewhat at 49 bit
    1332             :  * input (an extra round for each bit at first),  and go up more and more
    1333             :  * rapidly after we pass 80 bits.-- Changed this to adjust for the presence of
    1334             :  * squfof, which will finish input up to 59 bits quickly. */
    1335             : static GEN
    1336        3387 : pollardbrent(GEN n)
    1337             : {
    1338        3387 :   const long tune_pb_min = 14; /* even 15 seems too much. */
    1339        3387 :   long c0, size = expi(n) + 1;
    1340        3387 :   if (size <= 28)
    1341           0 :     c0 = 32;/* amounts very nearly to 'insist'. Now that we have squfof(), we
    1342             :              * don't insist any more when input is 2^29 ... 2^32 */
    1343        3387 :   else if (size <= 42)
    1344        1079 :     c0 = tune_pb_min;
    1345        2308 :   else if (size <= 59) /* match squfof() cutoff point */
    1346        1554 :     c0 = tune_pb_min + ((size - 42)<<1);
    1347         754 :   else if (size <= 72)
    1348         434 :     c0 = tune_pb_min + size - 24;
    1349         320 :   else if (size <= 301)
    1350             :     /* nonlinear increase in effort, kicking in around 80 bits */
    1351             :     /* 301 gives 48121 + tune_pb_min */
    1352         626 :     c0 = tune_pb_min + size - 60 +
    1353         313 :       ((size-73)>>1)*((size-70)>>3)*((size-56)>>4);
    1354             :   else
    1355           7 :     c0 = 49152; /* ECM is faster when it'd take longer */
    1356        3387 :   return pollardbrent_i(n, size, c0, 0);
    1357             : }
    1358             : GEN
    1359           0 : Z_pollardbrent(GEN n, long rounds, long seed)
    1360             : {
    1361           0 :   pari_sp av = avma;
    1362           0 :   GEN v = pollardbrent_i(n, expi(n)+1, rounds, seed);
    1363           0 :   if (!v) return NULL;
    1364           0 :   if (typ(v) == t_INT) v = mkvec2(v, diviiexact(n,v));
    1365           0 :   else if (lg(v) == 7) v = mkvec2(gel(v,1), gel(v,4));
    1366           0 :   else v = mkvec3(gel(v,1), gel(v,4), gel(v,7));
    1367           0 :   return gerepilecopy(av, v);
    1368             : }
    1369             : 
    1370             : /***********************************************************************/
    1371             : /**              FACTORIZATION (Shanks' SQUFOF) --GN2000Sep30-Oct01   **/
    1372             : /**  squfof() returns a nontrivial factor of n, assuming n is odd,    **/
    1373             : /**  composite, not a pure square, and has no small prime divisor,    **/
    1374             : /**  or NULL if it fails to find one.  It works on two discriminants  **/
    1375             : /**  simultaneously  (n and 5n for n=1(4), 3n and 4n for n=3(4)).     **/
    1376             : /**  Present implementation is limited to input <2^59, and works most **/
    1377             : /**  of the time in signed arithmetic on integers <2^31 in absolute   **/
    1378             : /**  size. (Cf. Algo 8.7.2 in ACiCNT)                                 **/
    1379             : /***********************************************************************/
    1380             : 
    1381             : /* The following is invoked to walk back along the ambiguous cycle* until we
    1382             :  * hit an ambiguous form and thus the desired factor, which it returns.  If it
    1383             :  * fails for any reason, it returns 0.  It doesn't interfere with timing and
    1384             :  * diagnostics, which it leaves to squfof().
    1385             :  *
    1386             :  * Before we invoke this, we've found a form (A, B, -C) with A = a^2, where a
    1387             :  * isn't blacklisted and where gcd(a, B) = 1.  According to ACiCANT, we should
    1388             :  * now proceed reducing the form (a, -B, -aC), but it is easy to show that the
    1389             :  * first reduction step always sends this to (-aC, B, a), and the next one,
    1390             :  * with q computed as usual from B and a (occupying the c position), gives a
    1391             :  * reduced form, whose third member is easiest to recover by going back to D.
    1392             :  * From this point onwards, we're once again working with single-word numbers.
    1393             :  * No need to track signs, just work with the abs values of the coefficients. */
    1394             : static long
    1395        2304 : squfof_ambig(long a, long B, long dd, GEN D)
    1396             : {
    1397             :   long b, c, q, qa, qc, qcb, a0, b0, b1, c0;
    1398        2304 :   long cnt = 0; /* count reduction steps on the cycle */
    1399             : 
    1400        2304 :   q = (dd + (B>>1)) / a;
    1401        2304 :   qa = q * a;
    1402        2304 :   b = (qa - B) + qa; /* avoid overflow */
    1403             :   {
    1404        2304 :     pari_sp av = avma;
    1405        2304 :     c = itos(divis(shifti(subii(D, sqrs(b)), -2), a));
    1406        2304 :     avma = av;
    1407             :   }
    1408             : #ifdef DEBUG_SQUFOF
    1409             :   err_printf("SQUFOF: ambigous cycle of discriminant %Ps\n", D);
    1410             :   err_printf("SQUFOF: Form on ambigous cycle (%ld, %ld, %ld)\n", a, b, c);
    1411             : #endif
    1412             : 
    1413        2304 :   a0 = a; b0 = b1 = b;        /* end of loop detection and safeguard */
    1414             : 
    1415             :   for (;;) /* reduced cycles are finite */
    1416             :   { /* reduction step */
    1417     4177818 :     c0 = c;
    1418     4177818 :     if (c0 > dd)
    1419     1165480 :       q = 1;
    1420             :     else
    1421     3012338 :       q = (dd + (b>>1)) / c0;
    1422     4177818 :     if (q == 1)
    1423             :     {
    1424     1737282 :       qcb = c0 - b; b = c0 + qcb; c = a - qcb;
    1425             :     }
    1426             :     else
    1427             :     {
    1428     2440536 :       qc = q*c0; qcb = qc - b; b = qc + qcb; c = a - q*qcb;
    1429             :     }
    1430     4177818 :     a = c0;
    1431             : 
    1432     4177818 :     cnt++; if (b == b1) break;
    1433             : 
    1434             :     /* safeguard against infinite loop: recognize when we've walked the entire
    1435             :      * cycle in vain. (I don't think this can actually happen -- exercise.) */
    1436     4175514 :     if (b == b0 && a == a0) return 0;
    1437             : 
    1438     4175514 :     b1 = b;
    1439     4175514 :   }
    1440        2304 :   q = a&1 ? a : a>>1;
    1441        2304 :   if (DEBUGLEVEL >= 4)
    1442             :   {
    1443           0 :     if (q > 1)
    1444           0 :       err_printf("SQUFOF: found factor %ld from ambiguous form\n"
    1445             :                  "\tafter %ld steps on the ambiguous cycle\n",
    1446           0 :                  q / ugcd(q,15), cnt);
    1447             :     else
    1448           0 :       err_printf("SQUFOF: ...found nothing on the ambiguous cycle\n"
    1449             :                  "\tafter %ld steps there\n", cnt);
    1450           0 :     if (DEBUGLEVEL >= 6) err_printf("SQUFOF: squfof_ambig returned %ld\n", q);
    1451             :   }
    1452        2304 :   return q;
    1453             : }
    1454             : 
    1455             : #define SQUFOF_BLACKLIST_SZ 64
    1456             : 
    1457             : /* assume 2,3,5 do not divide n */
    1458             : static GEN
    1459        2130 : squfof(GEN n)
    1460             : {
    1461             :   ulong d1, d2;
    1462        2130 :   long tf = lgefint(n), nm4, cnt = 0;
    1463             :   long a1, b1, c1, dd1, L1, a2, b2, c2, dd2, L2, a, q, c, qc, qcb;
    1464             :   GEN D1, D2;
    1465        2130 :   pari_sp av = avma;
    1466             :   long blacklist1[SQUFOF_BLACKLIST_SZ], blacklist2[SQUFOF_BLACKLIST_SZ];
    1467        2130 :   long blp1 = 0, blp2 = 0;
    1468        2130 :   int act1 = 1, act2 = 1;
    1469             : 
    1470             : #ifdef LONG_IS_64BIT
    1471        1854 :   if (tf > 3 || (tf == 3 && uel(n,2)             >= (1UL << (BITS_IN_LONG-5))))
    1472             : #else  /* 32 bits */
    1473         276 :   if (tf > 4 || (tf == 4 && (ulong)(*int_MSW(n)) >= (1UL << (BITS_IN_LONG-5))))
    1474             : #endif
    1475         365 :     return NULL; /* n too large */
    1476             : 
    1477             :   /* now we have 5 < n < 2^59 */
    1478        1765 :   nm4 = mod4(n);
    1479        1765 :   if (nm4 == 1)
    1480             :   { /* n = 1 (mod4):  run one iteration on D1 = n, another on D2 = 5n */
    1481         812 :     D1 = n;
    1482         812 :     D2 = mului(5,n); d2 = itou(sqrti(D2)); dd2 = (long)((d2>>1) + (d2&1));
    1483         812 :     b2 = (long)((d2-1) | 1);        /* b1, b2 will always stay odd */
    1484             :   }
    1485             :   else
    1486             :   { /* n = 3 (mod4):  run one iteration on D1 = 3n, another on D2 = 4n */
    1487         953 :     D1 = mului(3,n);
    1488         953 :     D2 = shifti(n,2); dd2 = itou(sqrti(n)); d2 =  dd2 << 1;
    1489         953 :     b2 = (long)(d2 & (~1UL)); /* largest even below d2, will stay even */
    1490             :   }
    1491        1765 :   d1 = itou(sqrti(D1));
    1492        1765 :   b1 = (long)((d1-1) | 1); /* largest odd number not exceeding d1 */
    1493        1765 :   c1 = itos(shifti(subii(D1, sqru((ulong)b1)), -2));
    1494        1765 :   if (!c1) pari_err_BUG("squfof [caller of] (n or 3n is a square)");
    1495        1765 :   c2 = itos(shifti(subii(D2, sqru((ulong)b2)), -2));
    1496        1765 :   if (!c2) pari_err_BUG("squfof [caller of] (5n is a square)");
    1497        1765 :   L1 = (long)usqrt(d1);
    1498        1765 :   L2 = (long)usqrt(d2);
    1499             :   /* dd1 used to compute floor((d1+b1)/2) as dd1+floor(b1/2), without
    1500             :    * overflowing the 31bit signed integer size limit. Same for dd2. */
    1501        1765 :   dd1 = (long) ((d1>>1) + (d1&1));
    1502        1765 :   a1 = a2 = 1;
    1503             : 
    1504             :   /* The two (identity) forms (a1,b1,-c1) and (a2,b2,-c2) are now set up.
    1505             :    *
    1506             :    * a1 and c1 represent the absolute values of the a,c coefficients; we keep
    1507             :    * track of the sign separately, via the iteration counter cnt: when cnt is
    1508             :    * even, c is understood to be negative, else c is positive and a < 0.
    1509             :    *
    1510             :    * L1, L2 are the limits for blacklisting small leading coefficients
    1511             :    * on the principal cycle, to guarantee that when we find a square form,
    1512             :    * its square root will belong to an ambiguous cycle  (i.e. won't be an
    1513             :    * earlier form on the principal cycle).
    1514             :    *
    1515             :    * When n = 3(mod 4), D2 = 12(mod 16), and b^2 is always 0 or 4 mod 16.
    1516             :    * It follows that 4*a*c must be 4 or 8 mod 16, respectively, so at most
    1517             :    * one of a,c can be divisible by 2 at most to the first power.  This fact
    1518             :    * is used a couple of times below.
    1519             :    *
    1520             :    * The flags act1, act2 remain true while the respective cycle is still
    1521             :    * active;  we drop them to false when we return to the identity form with-
    1522             :    * out having found a square form  (or when the blacklist overflows, which
    1523             :    * shouldn't happen). */
    1524        1765 :   if (DEBUGLEVEL >= 4)
    1525           0 :     err_printf("SQUFOF: entering main loop with forms\n"
    1526             :                "\t(1, %ld, %ld) and (1, %ld, %ld)\n\tof discriminants\n"
    1527             :                "\t%Ps and %Ps, respectively\n", b1, -c1, b2, -c2, D1, D2);
    1528             : 
    1529             :   /* MAIN LOOP: walk around the principal cycle looking for a square form.
    1530             :    * Blacklist small leading coefficients.
    1531             :    *
    1532             :    * The reduction operator can be computed entirely in 32-bit arithmetic:
    1533             :    * Let q = floor(floor((d1+b1)/2)/c1)  (when c1>dd1, q=1, which happens
    1534             :    * often enough to special-case it).  Then the new b1 = (q*c1-b1) + q*c1,
    1535             :    * which does not overflow, and the new c1 = a1 - q*(q*c1-b1), which is
    1536             :    * bounded by d1 in abs size since both the old and the new a1 are positive
    1537             :    * and bounded by d1. */
    1538     6012359 :   while (act1 || act2)
    1539             :   {
    1540     6010580 :     if (act1)
    1541             :     { /* send first form through reduction operator if active */
    1542     6010496 :       c = c1;
    1543     6010496 :       q = (c > dd1)? 1: (dd1 + (b1>>1)) / c;
    1544     6010496 :       if (q == 1)
    1545     2489872 :       { qcb = c - b1; b1 = c + qcb; c1 = a1 - qcb; }
    1546             :       else
    1547     3520624 :       { qc = q*c; qcb = qc - b1; b1 = qc + qcb; c1 = a1 - q*qcb; }
    1548     6010496 :       a1 = c;
    1549             : 
    1550     6010496 :       if (a1 <= L1)
    1551             :       { /* blacklist this */
    1552        1827 :         if (blp1 >= SQUFOF_BLACKLIST_SZ) /* overflows: shouldn't happen */
    1553           0 :           act1 = 0; /* silently */
    1554             :         else
    1555             :         {
    1556        1827 :           if (DEBUGLEVEL >= 6)
    1557           0 :             err_printf("SQUFOF: blacklisting a = %ld on first cycle\n", a1);
    1558        1827 :           blacklist1[blp1++] = a1;
    1559             :         }
    1560             :       }
    1561             :     }
    1562     6010580 :     if (act2)
    1563             :     { /* send second form through reduction operator if active */
    1564     6009390 :       c = c2;
    1565     6009390 :       q = (c > dd2)? 1: (dd2 + (b2>>1)) / c;
    1566     6009390 :       if (q == 1)
    1567     2497034 :       { qcb = c - b2; b2 = c + qcb; c2 = a2 - qcb; }
    1568             :       else
    1569     3512356 :       { qc = q*c; qcb = qc - b2; b2 = qc + qcb; c2 = a2 - q*qcb; }
    1570     6009390 :       a2 = c;
    1571             : 
    1572     6009390 :       if (a2 <= L2)
    1573             :       { /* blacklist this */
    1574        1380 :         if (blp2 >= SQUFOF_BLACKLIST_SZ) /* overflows: shouldn't happen */
    1575           0 :           act2 = 0; /* silently */
    1576             :         else
    1577             :         {
    1578        1380 :           if (DEBUGLEVEL >= 6)
    1579           0 :             err_printf("SQUFOF: blacklisting a = %ld on second cycle\n", a2);
    1580        1380 :           blacklist2[blp2++] = a2;
    1581             :         }
    1582             :       }
    1583             :     }
    1584             : 
    1585             :     /* bump counter, loop if this is an odd iteration (i.e. if the real
    1586             :      * leading coefficients are negative) */
    1587     6010580 :     if (++cnt & 1) continue;
    1588             : 
    1589             :     /* second half of main loop entered only when the leading coefficients
    1590             :      * are positive (i.e., during even-numbered iterations) */
    1591             : 
    1592             :     /* examine first form if active */
    1593     3005290 :     if (act1 && a1 == 1) /* back to identity */
    1594             :     { /* drop this discriminant */
    1595          14 :       act1 = 0;
    1596          14 :       if (DEBUGLEVEL >= 4)
    1597           0 :         err_printf("SQUFOF: first cycle exhausted after %ld iterations,\n"
    1598             :                    "\tdropping it\n", cnt);
    1599             :     }
    1600     3005290 :     if (act1)
    1601             :     {
    1602     3005234 :       if (uissquareall((ulong)a1, (ulong*)&a))
    1603             :       { /* square form */
    1604        1820 :         if (DEBUGLEVEL >= 4)
    1605           0 :           err_printf("SQUFOF: square form (%ld^2, %ld, %ld) on first cycle\n"
    1606             :                      "\tafter %ld iterations\n", a, b1, -c1, cnt);
    1607        1820 :         if (a <= L1)
    1608             :         { /* blacklisted? */
    1609             :           long j;
    1610        3899 :           for (j = 0; j < blp1; j++)
    1611        2926 :             if (a == blacklist1[j]) { a = 0; break; }
    1612             :         }
    1613        1820 :         if (a > 0)
    1614             :         { /* not blacklisted */
    1615         973 :           q = ugcd(a, b1); /* imprimitive form? */
    1616         973 :           if (q > 1)
    1617             :           { /* q^2 divides D1 hence n [ assuming n % 3 != 0 ] */
    1618           0 :             avma = av;
    1619           0 :             if (DEBUGLEVEL >= 4) err_printf("SQUFOF: found factor %ld^2\n", q);
    1620           0 :             return mkvec3(utoipos(q), gen_2, NULL);/* exponent 2, unknown status */
    1621             :           }
    1622             :           /* chase the inverse root form back along the ambiguous cycle */
    1623         973 :           q = squfof_ambig(a, b1, dd1, D1);
    1624         973 :           if (nm4 == 3 && q % 3 == 0) q /= 3;
    1625         973 :           if (q > 1) { avma = av; return utoipos(q); } /* SUCCESS! */
    1626             :         }
    1627         847 :         else if (DEBUGLEVEL >= 4) /* blacklisted */
    1628           0 :           err_printf("SQUFOF: ...but the root form seems to be on the "
    1629             :                      "principal cycle\n");
    1630             :       }
    1631             :     }
    1632             : 
    1633             :     /* examine second form if active */
    1634     3004555 :     if (act2 && a2 == 1) /* back to identity form */
    1635             :     { /* drop this discriminant */
    1636          21 :       act2 = 0;
    1637          21 :       if (DEBUGLEVEL >= 4)
    1638           0 :         err_printf("SQUFOF: second cycle exhausted after %ld iterations,\n"
    1639             :                    "\tdropping it\n", cnt);
    1640             :     }
    1641     3004555 :     if (act2)
    1642             :     {
    1643     3003946 :       if (uissquareall((ulong)a2, (ulong*)&a))
    1644             :       { /* square form */
    1645        1597 :         if (DEBUGLEVEL >= 4)
    1646           0 :           err_printf("SQUFOF: square form (%ld^2, %ld, %ld) on second cycle\n"
    1647             :                      "\tafter %ld iterations\n", a, b2, -c2, cnt);
    1648        1597 :         if (a <= L2)
    1649             :         { /* blacklisted? */
    1650             :           long j;
    1651        2865 :           for (j = 0; j < blp2; j++)
    1652        1534 :             if (a == blacklist2[j]) { a = 0; break; }
    1653             :         }
    1654        1597 :         if (a > 0)
    1655             :         { /* not blacklisted */
    1656        1331 :           q = ugcd(a, b2); /* imprimitive form? */
    1657             :           /* NB if b2 is even, a is odd, so the gcd is always odd */
    1658        1331 :           if (q > 1)
    1659             :           { /* q^2 divides D2 hence n [ assuming n % 5 != 0 ] */
    1660           0 :             avma = av;
    1661           0 :             if (DEBUGLEVEL >= 4) err_printf("SQUFOF: found factor %ld^2\n", q);
    1662           0 :             return mkvec3(utoipos(q), gen_2, NULL);/* exponent 2, unknown status */
    1663             :           }
    1664             :           /* chase the inverse root form along the ambiguous cycle */
    1665        1331 :           q = squfof_ambig(a, b2, dd2, D2);
    1666        1331 :           if (nm4 == 1 && q % 5 == 0) q /= 5;
    1667        1331 :           if (q > 1) { avma = av; return utoipos(q); } /* SUCCESS! */
    1668             :         }
    1669         266 :         else if (DEBUGLEVEL >= 4)        /* blacklisted */
    1670           0 :           err_printf("SQUFOF: ...but the root form seems to be on the "
    1671             :                      "principal cycle\n");
    1672             :       }
    1673             :     }
    1674             :   } /* end main loop */
    1675             : 
    1676             :   /* both discriminants turned out to be useless. */
    1677          14 :   if (DEBUGLEVEL>=4) err_printf("SQUFOF: giving up\n");
    1678          14 :   avma = av; return NULL;
    1679             : }
    1680             : 
    1681             : /***********************************************************************/
    1682             : /*                    DETECTING ODD POWERS  --GN1998Jun28              */
    1683             : /*   Factoring engines like MPQS which ultimately rely on computing    */
    1684             : /*   gcd(N, x^2-y^2) to find a nontrivial factor of N can't split      */
    1685             : /*   N = p^k for an odd prime p, since (Z/p^k)^* is then cyclic. Here  */
    1686             : /*   is an analogue of Z_issquareall() for 3rd, 5th and 7th powers.    */
    1687             : /*   The general case is handled by is_kth_power                       */
    1688             : /***********************************************************************/
    1689             : 
    1690             : /* Multistage sieve. First stages work mod 211, 209, 61, 203 in this order
    1691             :  * (first reduce mod the product of these and then take the remainder apart).
    1692             :  * Second stages use 117, 31, 43, 71. Moduli which are no longer interesting
    1693             :  * are skipped. Everything is encoded in a table of 106 24-bit masks. We only
    1694             :  * need the first half of the residues.  Three bits per modulus indicate which
    1695             :  * residues are 7th (bit 2), 5th (bit 1) or 3rd (bit 0) powers; the eight
    1696             :  * moduli above are assigned right-to-left. The table was generated using: */
    1697             : 
    1698             : #if 0
    1699             : L = [71, 43, 31, [O(3^2),O(13)], [O(7),O(29)], 61, [O(11),O(19)], 211];
    1700             : ispow(x, N, k)=
    1701             : {
    1702             :   if (type(N) == "t_INT", return (ispower(Mod(x,N), k)));
    1703             :   for (i = 1, #N, if (!ispower(x + N[i], k), return (0))); 1
    1704             : }
    1705             : check(r) =
    1706             : {
    1707             :   print1("  0");
    1708             :   for (i=1,#L,
    1709             :     N = 0;
    1710             :     if (ispow(r, L[i], 3), N += 1);
    1711             :     if (ispow(r, L[i], 5), N += 2);
    1712             :     if (ispow(r, L[i], 7), N += 4);
    1713             :     print1(N);
    1714             :   ); print("ul,  /* ", r, " */")
    1715             : }
    1716             : for (r = 0, 105, check(r))
    1717             : #endif
    1718             : static ulong powersmod[106] = {
    1719             :   077777777ul,  /* 0 */
    1720             :   077777777ul,  /* 1 */
    1721             :   013562440ul,  /* 2 */
    1722             :   012402540ul,  /* 3 */
    1723             :   013562440ul,  /* 4 */
    1724             :   052662441ul,  /* 5 */
    1725             :   016603440ul,  /* 6 */
    1726             :   016463450ul,  /* 7 */
    1727             :   013573551ul,  /* 8 */
    1728             :   012462540ul,  /* 9 */
    1729             :   012462464ul,  /* 10 */
    1730             :   013462771ul,  /* 11 */
    1731             :   012406473ul,  /* 12 */
    1732             :   012463641ul,  /* 13 */
    1733             :   052463646ul,  /* 14 */
    1734             :   012503446ul,  /* 15 */
    1735             :   013562440ul,  /* 16 */
    1736             :   052466440ul,  /* 17 */
    1737             :   012472451ul,  /* 18 */
    1738             :   012462454ul,  /* 19 */
    1739             :   032463550ul,  /* 20 */
    1740             :   013403664ul,  /* 21 */
    1741             :   013463460ul,  /* 22 */
    1742             :   032562565ul,  /* 23 */
    1743             :   012402540ul,  /* 24 */
    1744             :   052662441ul,  /* 25 */
    1745             :   032672452ul,  /* 26 */
    1746             :   013573551ul,  /* 27 */
    1747             :   012467541ul,  /* 28 */
    1748             :   012567640ul,  /* 29 */
    1749             :   032706450ul,  /* 30 */
    1750             :   012762452ul,  /* 31 */
    1751             :   033762662ul,  /* 32 */
    1752             :   012502562ul,  /* 33 */
    1753             :   032463562ul,  /* 34 */
    1754             :   013563440ul,  /* 35 */
    1755             :   016663440ul,  /* 36 */
    1756             :   036662550ul,  /* 37 */
    1757             :   012462552ul,  /* 38 */
    1758             :   033502450ul,  /* 39 */
    1759             :   012462643ul,  /* 40 */
    1760             :   033467540ul,  /* 41 */
    1761             :   017403441ul,  /* 42 */
    1762             :   017463462ul,  /* 43 */
    1763             :   017472460ul,  /* 44 */
    1764             :   033462470ul,  /* 45 */
    1765             :   052566450ul,  /* 46 */
    1766             :   013562640ul,  /* 47 */
    1767             :   032403640ul,  /* 48 */
    1768             :   016463450ul,  /* 49 */
    1769             :   016463752ul,  /* 50 */
    1770             :   033402440ul,  /* 51 */
    1771             :   012462540ul,  /* 52 */
    1772             :   012472540ul,  /* 53 */
    1773             :   053562462ul,  /* 54 */
    1774             :   012463465ul,  /* 55 */
    1775             :   012663470ul,  /* 56 */
    1776             :   052607450ul,  /* 57 */
    1777             :   012566553ul,  /* 58 */
    1778             :   013466440ul,  /* 59 */
    1779             :   012502741ul,  /* 60 */
    1780             :   012762744ul,  /* 61 */
    1781             :   012763740ul,  /* 62 */
    1782             :   012763443ul,  /* 63 */
    1783             :   013573551ul,  /* 64 */
    1784             :   013462471ul,  /* 65 */
    1785             :   052502460ul,  /* 66 */
    1786             :   012662463ul,  /* 67 */
    1787             :   012662451ul,  /* 68 */
    1788             :   012403550ul,  /* 69 */
    1789             :   073567540ul,  /* 70 */
    1790             :   072463445ul,  /* 71 */
    1791             :   072462740ul,  /* 72 */
    1792             :   012472442ul,  /* 73 */
    1793             :   012462644ul,  /* 74 */
    1794             :   013406650ul,  /* 75 */
    1795             :   052463471ul,  /* 76 */
    1796             :   012563474ul,  /* 77 */
    1797             :   013503460ul,  /* 78 */
    1798             :   016462441ul,  /* 79 */
    1799             :   016462440ul,  /* 80 */
    1800             :   012462540ul,  /* 81 */
    1801             :   013462641ul,  /* 82 */
    1802             :   012463454ul,  /* 83 */
    1803             :   013403550ul,  /* 84 */
    1804             :   057563540ul,  /* 85 */
    1805             :   017466441ul,  /* 86 */
    1806             :   017606471ul,  /* 87 */
    1807             :   053666573ul,  /* 88 */
    1808             :   012562561ul,  /* 89 */
    1809             :   013473641ul,  /* 90 */
    1810             :   032573440ul,  /* 91 */
    1811             :   016763440ul,  /* 92 */
    1812             :   016702640ul,  /* 93 */
    1813             :   033762552ul,  /* 94 */
    1814             :   012562550ul,  /* 95 */
    1815             :   052402451ul,  /* 96 */
    1816             :   033563441ul,  /* 97 */
    1817             :   012663561ul,  /* 98 */
    1818             :   012677560ul,  /* 99 */
    1819             :   012462464ul,  /* 100 */
    1820             :   032562642ul,  /* 101 */
    1821             :   013402551ul,  /* 102 */
    1822             :   032462450ul,  /* 103 */
    1823             :   012467445ul,  /* 104 */
    1824             :   032403440ul,  /* 105 */
    1825             : };
    1826             : 
    1827             : static int
    1828     1865712 : check_res(ulong x, ulong N, int shift, ulong *mask)
    1829             : {
    1830     1865712 :   long r = x%N; if ((ulong)r> (N>>1)) r = N - r;
    1831     1865712 :   *mask &= (powersmod[r] >> shift);
    1832     1865712 :   return *mask;
    1833             : }
    1834             : 
    1835             : /* is x mod 211*209*61*203*117*31*43*71 a 3rd, 5th or 7th power ? */
    1836             : int
    1837     1028883 : uis_357_powermod(ulong x, ulong *mask)
    1838             : {
    1839     1028883 :   if (             !check_res(x, 211UL, 0, mask)) return 0;
    1840      527769 :   if (*mask & 3 && !check_res(x, 209UL, 3, mask)) return 0;
    1841      255679 :   if (*mask & 3 && !check_res(x,  61UL, 6, mask)) return 0;
    1842      168611 :   if (*mask & 5 && !check_res(x, 203UL, 9, mask)) return 0;
    1843       38489 :   if (*mask & 1 && !check_res(x, 117UL,12, mask)) return 0;
    1844       27810 :   if (*mask & 3 && !check_res(x,  31UL,15, mask)) return 0;
    1845       21240 :   if (*mask & 5 && !check_res(x,  43UL,18, mask)) return 0;
    1846        4919 :   if (*mask & 6 && !check_res(x,  71UL,21, mask)) return 0;
    1847        1674 :   return 1;
    1848             : }
    1849             : /* asume x > 0 and pt != NULL */
    1850             : int
    1851      983269 : uis_357_power(ulong x, ulong *pt, ulong *mask)
    1852             : {
    1853             :   double logx;
    1854      983269 :   if (!odd(x))
    1855             :   {
    1856         259 :     long v = vals(x);
    1857         259 :     if (v % 7) *mask &= ~4;
    1858         259 :     if (v % 5) *mask &= ~2;
    1859         259 :     if (v % 3) *mask &= ~1;
    1860         259 :     if (!*mask) return 0;
    1861             :   }
    1862      983129 :   if (!uis_357_powermod(x, mask)) return 0;
    1863        1422 :   logx = log((double)x);
    1864        3628 :   while (*mask)
    1865             :   {
    1866             :     long e, b;
    1867             :     ulong y, ye;
    1868        1422 :     if (*mask & 1)      { b = 1; e = 3; }
    1869         650 :     else if (*mask & 2) { b = 2; e = 5; }
    1870         343 :     else                { b = 4; e = 7; }
    1871        1422 :     y = (ulong)(exp(logx / e) + 0.5);
    1872        1422 :     ye = upowuu(y,e);
    1873        1422 :     if (ye == x) { *pt = y; return e; }
    1874             : #ifdef LONG_IS_64BIT
    1875         672 :     if (ye > x) y--; else y++;
    1876         672 :     ye = upowuu(y,e);
    1877         672 :     if (ye == x) { *pt = y; return e; }
    1878             : #endif
    1879         784 :     *mask &= ~b; /* turn the bit off */
    1880             :   }
    1881         784 :   return 0;
    1882             : }
    1883             : 
    1884             : #ifndef LONG_IS_64BIT
    1885             : /* as above, split in two functions */
    1886             : /* is x mod 211*209*61*203 a 3rd, 5th or 7th power ? */
    1887             : static int
    1888       10810 : uis_357_powermod_32bit_1(ulong x, ulong *mask)
    1889             : {
    1890       10810 :   if (             !check_res(x, 211UL, 0, mask)) return 0;
    1891        5896 :   if (*mask & 3 && !check_res(x, 209UL, 3, mask)) return 0;
    1892        2973 :   if (*mask & 3 && !check_res(x,  61UL, 6, mask)) return 0;
    1893        2037 :   if (*mask & 5 && !check_res(x, 203UL, 9, mask)) return 0;
    1894         503 :   return 1;
    1895             : }
    1896             : /* is x mod 117*31*43*71 a 3rd, 5th or 7th power ? */
    1897             : static int
    1898         503 : uis_357_powermod_32bit_2(ulong x, ulong *mask)
    1899             : {
    1900         503 :   if (*mask & 1 && !check_res(x, 117UL,12, mask)) return 0;
    1901         388 :   if (*mask & 3 && !check_res(x,  31UL,15, mask)) return 0;
    1902         284 :   if (*mask & 5 && !check_res(x,  43UL,18, mask)) return 0;
    1903          92 :   if (*mask & 6 && !check_res(x,  71UL,21, mask)) return 0;
    1904          51 :   return 1;
    1905             : }
    1906             : #endif
    1907             : 
    1908             : /* Returns 3, 5, or 7 if x is a cube (but not a 5th or 7th power),  a 5th
    1909             :  * power (but not a 7th),  or a 7th power, and in this case creates the
    1910             :  * base on the stack and assigns its address to *pt.  Otherwise returns 0.
    1911             :  * x must be of type t_INT and positive;  this is not checked.  The *mask
    1912             :  * argument tells us which things to check -- bit 0: 3rd, bit 1: 5th,
    1913             :  * bit 2: 7th pwr;  set a bit to have the corresponding power examined --
    1914             :  * and is updated appropriately for a possible follow-up call */
    1915             : int
    1916     1397021 : is_357_power(GEN x, GEN *pt, ulong *mask)
    1917             : {
    1918     1397021 :   long lx = lgefint(x);
    1919             :   ulong r;
    1920             :   pari_sp av;
    1921             :   GEN y;
    1922             : 
    1923     1397021 :   if (!*mask) return 0; /* useful when running in a loop */
    1924     1027106 :   if (DEBUGLEVEL>4) err_printf("OddPwrs: examining %ld-bit integer\n", expi(x));
    1925     1027106 :   if (lgefint(x) == 3) {
    1926             :     ulong t;
    1927      970542 :     long e = uis_357_power(x[2], &t, mask);
    1928      970542 :     if (e)
    1929             :     {
    1930         625 :       if (pt) *pt = utoi(t);
    1931         625 :       return e;
    1932             :     }
    1933      969917 :     return 0;
    1934             :   }
    1935             : #ifdef LONG_IS_64BIT
    1936       45754 :   r = (lx == 3)? uel(x,2): umodiu(x, 6046846918939827UL);
    1937       45754 :   if (!uis_357_powermod(r, mask)) return 0;
    1938             : #else
    1939       10810 :   r = (lx == 3)? uel(x,2): umodiu(x, 211*209*61*203);
    1940       10810 :   if (!uis_357_powermod_32bit_1(r, mask)) return 0;
    1941         503 :   r = (lx == 3)? uel(x,2): umodiu(x, 117*31*43*71);
    1942         503 :   if (!uis_357_powermod_32bit_2(r, mask)) return 0;
    1943             : #endif
    1944         303 :   av = avma;
    1945         671 :   while (*mask)
    1946             :   {
    1947             :     long e, b;
    1948             :     /* priority to higher powers: if we have a 21st, it is easier to rediscover
    1949             :      * that its 7th root is a cube than that its cube root is a 7th power */
    1950         303 :          if (*mask & 4) { b = 4; e = 7; }
    1951         238 :     else if (*mask & 2) { b = 2; e = 5; }
    1952         124 :     else                { b = 1; e = 3; }
    1953         303 :     y = mpround( sqrtnr(itor(x, nbits2prec(64 + bit_accuracy(lx) / e)), e) );
    1954         303 :     if (equalii(powiu(y,e), x))
    1955             :     {
    1956         238 :       if (!pt) { avma = av; return e; }
    1957         238 :       avma = (pari_sp)y; *pt = gerepileuptoint(av, y);
    1958         238 :       return e;
    1959             :     }
    1960          65 :     if (DEBUGLEVEL>4)
    1961           0 :       err_printf("\tBut it nevertheless wasn't a %ld%s power.\n", e,eng_ord(e));
    1962          65 :     *mask &= ~b; /* turn the bit off */
    1963          65 :     avma = av;
    1964             :   }
    1965          65 :   return 0;
    1966             : }
    1967             : 
    1968             : /* Is x a n-th power ?
    1969             :  * if d = NULL, n not necessarily prime, otherwise, n prime and d the
    1970             :  * corresponding diffptr to go on looping over primes.
    1971             :  * If pt != NULL, it receives the n-th root */
    1972             : ulong
    1973      260435 : is_kth_power(GEN x, ulong n, GEN *pt)
    1974             : {
    1975             :   forprime_t T;
    1976             :   long j;
    1977             :   ulong q, residue;
    1978             :   GEN y;
    1979      260435 :   pari_sp av = avma;
    1980             : 
    1981      260435 :   (void)u_forprime_arith_init(&T, odd(n)? 2*n+1: n+1, ULONG_MAX, 1,n);
    1982             :   /* we'll start at q, smallest prime >= n */
    1983             : 
    1984             :   /* Modular checks, use small primes q congruent 1 mod n */
    1985             :   /* A non n-th power nevertheless passes the test with proba n^(-#checks),
    1986             :    * We'd like this < 1e-6 but let j = floor(log(1e-6) / log(n)) which
    1987             :    * ensures much less. */
    1988      260435 :   if (n < 16)
    1989       32498 :     j = 5;
    1990      227937 :   else if (n < 32)
    1991       76155 :     j = 4;
    1992      151782 :   else if (n < 101)
    1993      131468 :     j = 3;
    1994       20314 :   else if (n < 1001)
    1995        4011 :     j = 2;
    1996       16303 :   else if (n < 17886697) /* smallest such that smallest suitable q is > 2^32 */
    1997       16303 :     j = 1;
    1998             :   else
    1999           0 :     j = 0;
    2000      269976 :   for (; j > 0; j--)
    2001             :   {
    2002      269857 :     if (!(q = u_forprime_next(&T))) break;
    2003             :     /* q a prime = 1 mod n */
    2004      269857 :     residue = umodiu(x, q);
    2005      269857 :     if (residue == 0)
    2006             :     {
    2007          35 :       if (Z_lval(x,q) % n) { avma = av; return 0; }
    2008           0 :       continue;
    2009             :     }
    2010             :     /* n-th power mod q ? */
    2011      269822 :     if (Fl_powu(residue, (q-1)/n, q) != 1) { avma = av; return 0; }
    2012             :   }
    2013         119 :   avma = av;
    2014             : 
    2015         119 :   if (DEBUGLEVEL>4) err_printf("\nOddPwrs: [%lu] passed modular checks\n",n);
    2016             :   /* go to the horse's mouth... */
    2017         119 :   y = roundr( sqrtnr(itor(x, nbits2prec((expi(x)+16*n)/n)), n) );
    2018         119 :   if (!equalii(powiu(y, n), x)) {
    2019           0 :     if (DEBUGLEVEL>4) err_printf("\tBut it wasn't a pure power.\n");
    2020           0 :     avma = av; return 0;
    2021             :   }
    2022         119 :   if (!pt) avma = av; else { avma = (pari_sp)y; *pt = gerepileuptoint(av, y); }
    2023         119 :   return 1;
    2024             : }
    2025             : 
    2026             : /* is x a p^i-th power, p >= 11 prime ? Similar to is_357_power(), but instead
    2027             :  * of the mask, we keep the current test exponent around. Cut off when
    2028             :  * log_2 x^(1/k) < cutoffbits since we would have found it by trial division.
    2029             :  * Everything needed here (primitive roots etc.) is computed from scratch on
    2030             :  * the fly; compared to the size of numbers under consideration, these
    2031             :  * word-sized computations take negligible time.
    2032             :  * Any cutoffbits > 0 is safe, but direct root extraction attempts are faster
    2033             :  * when trial division has been used to discover very small bases. We become
    2034             :  * competitive at cutoffbits ~ 10 */
    2035             : int
    2036       42604 : is_pth_power(GEN x, GEN *pt, forprime_t *T, ulong cutoffbits)
    2037             : {
    2038       42604 :   long cnt=0, size = expi(x) /* not +1 */;
    2039             :   ulong p;
    2040       42604 :   pari_sp av = avma;
    2041      345377 :   while ((p = u_forprime_next(T)) && size/p >= cutoffbits) {
    2042      260211 :     long v = 1;
    2043      260211 :     if (DEBUGLEVEL>5 && cnt++==2000)
    2044           0 :       { cnt=0; err_printf("%lu%% ", 100*p*cutoffbits/size); }
    2045      520478 :     while (is_kth_power(x, p, pt)) {
    2046          56 :       v *= p; x = *pt;
    2047          56 :       size = expi(x);
    2048             :     }
    2049      260211 :     if (v > 1)
    2050             :     {
    2051          42 :       if (DEBUGLEVEL>5) err_printf("\nOddPwrs: is a %ld power\n",v);
    2052          42 :       return v;
    2053             :     }
    2054             :   }
    2055       42562 :   if (DEBUGLEVEL>5) err_printf("\nOddPwrs: not a power\n",p);
    2056       42562 :   avma = av; return 0; /* give up */
    2057             : }
    2058             : 
    2059             : /***********************************************************************/
    2060             : /**                FACTORIZATION  (master iteration)                  **/
    2061             : /**      Driver for the various methods of finding large factors      **/
    2062             : /**      (after trial division has cast out the very small ones).     **/
    2063             : /**                        GN1998Jun24--30                            **/
    2064             : /***********************************************************************/
    2065             : 
    2066             : /* Direct use:
    2067             :  *  ifac_start_hint(n,moebius,hint) registers with the iterative factorizer
    2068             :  *  - an integer n (without prime factors  < tridiv_bound(n))
    2069             :  *  - registers whether or not we should terminate early if we find a square
    2070             :  *    factor,
    2071             :  *  - a hint about which method(s) to use.
    2072             :  *  This must always be called first. If input is not composite, oo loop.
    2073             :  *  The routine decomposes n nontrivially into a product of two factors except
    2074             :  *  in squarefreeness ('Moebius') mode.
    2075             :  *
    2076             :  *  ifac_start(n,moebius) same using default hint.
    2077             :  *
    2078             :  *  ifac_primary_factor()  returns a prime divisor (not necessarily the
    2079             :  *    smallest) and the corresponding exponent.
    2080             :  *
    2081             :  * Encapsulated user interface: Many arithmetic functions have a 'contributor'
    2082             :  * ifac_xxx, to be called on any large composite cofactor left over after trial
    2083             :  * division by small primes: xxx is one of moebius, issquarefree, totient, etc.
    2084             :  *
    2085             :  * We never test whether the input number is prime or composite, since
    2086             :  * presumably it will have come out of the small factors finder stage
    2087             :  * (which doesn't really exist yet but which will test the left-over
    2088             :  * cofactor for primality once it does). */
    2089             : 
    2090             : /* The data structure in which we preserve whatever we know about our number N
    2091             :  * is kept on the PARI stack, and updated as needed.
    2092             :  * This makes the machinery re-entrant, and avoids memory leaks when a lengthy
    2093             :  * factorization is interrupted. We try to keep the whole affair connected,
    2094             :  * and the parent object is always older than its children.  This may in
    2095             :  * rare cases lead to some extra copying around, and knowing what is garbage
    2096             :  * at any given time is not trivial. See below for examples how to do it right.
    2097             :  * (Connectedness is destroyed if callers of ifac_main() create stuff on the
    2098             :  * stack in between calls. This is harmless as long as ifac_realloc() is used
    2099             :  * to re-create a connected object at the head of the stack just before
    2100             :  * collecting garbage.)
    2101             :  * A t_INT may well have > 10^6 distinct prime factors larger than 2^16. Since
    2102             :  * we need not find factors in order of increasing size, we must be prepared to
    2103             :  * drag a very large amount of data around.  We start with a small structure
    2104             :  * and extend it when necessary. */
    2105             : 
    2106             : /* The idea of the algorithm is:
    2107             :  * Let N0 be whatever is currently left of N after dividing off all the
    2108             :  * prime powers we have already returned to the caller.  Then we maintain
    2109             :  * N0 as a product
    2110             :  * (1) N0 = \prod_i P_i^{e_i} * \prod_j Q_j^{f_j} * \prod_k C_k^{g_k}
    2111             :  * where the P_i and Q_j are distinct primes, each C_k is known composite,
    2112             :  * none of the P_i divides any C_k, and we also know the total ordering
    2113             :  * of all the P_i, Q_j and C_k; in particular, we will never try to divide
    2114             :  * a C_k by a larger Q_j.  Some of the C_k may have common factors.
    2115             :  *
    2116             :  * Caveat implementor:  Taking gcds among C_k's is very likely to cost at
    2117             :  * least as much time as dividing off any primes as we find them, and book-
    2118             :  * keeping would be tough (since D=gcd(C_1,C_2) can still have common factors
    2119             :  * with both C_1/D and C_2/D, and so on...).
    2120             :  *
    2121             :  * At startup, we just initialize the structure to
    2122             :  * (2) N = C_1^1   (composite).
    2123             :  *
    2124             :  * Whenever ifac_primary_factor() or one of the arithmetic user interface
    2125             :  * routines needs a primary factor, and the smallest thing in our list is P_1,
    2126             :  * we return that and its exponent, and remove it from our list. (When nothing
    2127             :  * is left, we return a sentinel value -- gen_1.  And in Moebius mode, when we
    2128             :  * see something with exponent > 1, whether prime or composite, we return gen_0
    2129             :  * or 0, depending on the function). In all other cases, ifac_main() iterates
    2130             :  * the following steps until we have a P_1 in the smallest position.
    2131             :  *
    2132             :  * When the smallest item is C_1, as it is initially:
    2133             :  * (3.1) Crack C_1 into a nontrivial product  U_1 * U_2  by whatever method
    2134             :  * comes to mind for this size. (U for 'unknown'.)  Cracking will detect
    2135             :  * perfect powers, so we may instead see a power of some U_1 here, or even
    2136             :  * something of the form U_1^k*U_2^k; of course the exponent already attached
    2137             :  * to C_1 is taken into account in the following.
    2138             :  * (3.2) If we have U_1*U_2, sort the two factors (distinct: squares are caught
    2139             :  * in stage 3.1). N.B. U_1 and U_2 are smaller than anything else in our list.
    2140             :  * (3.3) Check U_1 and U_2 for primality, and flag them accordingly.
    2141             :  * (3.4) Iterate.
    2142             :  *
    2143             :  * When the smallest item is Q_1:
    2144             :  * This is the unpleasant case.  We go through the entire list and try to
    2145             :  * divide Q_1 off each of the current C_k's, which usually fails, but may
    2146             :  * succeed several times. When a division was successful, the corresponding
    2147             :  * C_k is removed from our list, and the cofactor becomes a U_l for the moment
    2148             :  * unless it is 1 (which happens when C_k was a power of Q_1).  When we're
    2149             :  * through we upgrade Q_1 to P_1 status, then do a primality check on each U_l
    2150             :  * and sort it back into the list either as a Q_j or as a C_k.  If during the
    2151             :  * insertion sort we discover that some U_l equals some P_i or Q_j or C_k we
    2152             :  * already have, we just add U_l's exponent to that of its twin. (The sorting
    2153             :  * therefore happens before the primality test). Since this may produce one or
    2154             :  * more elements smaller than the P_1 we just confirmed, we may have to repeat
    2155             :  * the iteration.
    2156             :  * A trick avoids some Q_1 instances: just after the sweep classifying
    2157             :  * all current unknowns as either composites or primes, we do another downward
    2158             :  * sweep beginning with the largest current factor and stopping just above the
    2159             :  * largest current composite.  Every Q_j we pass is turned into a P_i.
    2160             :  * (Different primes are automatically coprime among each other, and primes do
    2161             :  * not divide smaller composites.)
    2162             :  * NB: We have no use for comparing the square of a prime to N0.  Normally
    2163             :  * we will get called after casting out only the smallest primes, and
    2164             :  * since we cannot guarantee that we see the large prime factors in as-
    2165             :  * cending order, we cannot stop when we find one larger than sqrt(N0). */
    2166             : 
    2167             : /* Data structure: We keep everything in a single t_VEC of t_INTs.  The
    2168             :  * first 2 components are read-only:
    2169             :  * 1) the first records whether we're doing full (NULL) or Moebius (gen_1)
    2170             :  * factorization; in the latter case subroutines return a sentinel value as
    2171             :  * soon as they spot an exponent > 1.
    2172             :  * 2) the second records the hint from factorint()'s optional flag, for use by
    2173             :  * ifac_crack().
    2174             :  *
    2175             :  * The remaining components (initially 15) are used in groups of three:
    2176             :  * [ factor (t_INT), exponent (t_INT), factor class ], where factor class is
    2177             :  *  NULL : unknown
    2178             :  *  gen_0: known composite C_k
    2179             :  *  gen_1: known prime Q_j awaiting trial division
    2180             :  *  gen_2: finished prime P_i.
    2181             :  * When during the division stage we re-sort a C_k-turned-U_l to a lower
    2182             :  * position, we rotate any intervening material upward towards its old
    2183             :  * slot.  When a C_k was divided down to 1, its slot is left empty at
    2184             :  * first; similarly when the re-sorting detects a repeated factor.
    2185             :  * After the sorting phase, we de-fragment the list and squeeze all the
    2186             :  * occupied slots together to the high end, so that ifac_crack() has room
    2187             :  * for new factors.  When this doesn't suffice, we abandon the current vector
    2188             :  * and allocate a somewhat larger one, defragmenting again while copying.
    2189             :  *
    2190             :  * For internal use: note that all exponents will fit into C longs, given
    2191             :  * PARI's lgefint field size.  When we work with them, we sometimes read
    2192             :  * out the GEN pointer, and sometimes do an itos, whatever is more con-
    2193             :  * venient for the task at hand. */
    2194             : 
    2195             : /*** Overview ***/
    2196             : 
    2197             : /* The '*where' argument in the following points into *partial at the first of
    2198             :  * the three fields of the first occupied slot.  It's there because the caller
    2199             :  * would already know where 'here' is, so we don't want to search for it again.
    2200             :  * We do not preserve this from one user-interface call to the next. */
    2201             : 
    2202             : /* In the most common cases, control flows from the user interface to
    2203             :  * ifac_main() and then to a succession of ifac_crack()s and ifac_divide()s,
    2204             :  * with (typically) none of the latter finding anything. */
    2205             : 
    2206             : static long ifac_insert_multiplet(GEN *, GEN *, GEN, long);
    2207             : 
    2208             : #define LAST(x) x+lg(x)-3
    2209             : #define FIRST(x) x+3
    2210             : 
    2211             : #define MOEBIUS(x) gel(x,1)
    2212             : #define HINT(x) gel(x,2)
    2213             : 
    2214             : /* y <- x */
    2215             : INLINE void
    2216           0 : SHALLOWCOPY(GEN x, GEN y) {
    2217           0 :   VALUE(y) = VALUE(x);
    2218           0 :   EXPON(y) = EXPON(x);
    2219           0 :   CLASS(y) = CLASS(x);
    2220           0 : }
    2221             : /* y <- x */
    2222             : INLINE void
    2223           0 : COPY(GEN x, GEN y) {
    2224           0 :   icopyifstack(VALUE(x), VALUE(y));
    2225           0 :   icopyifstack(EXPON(x), EXPON(y));
    2226           0 :   CLASS(y) = CLASS(x);
    2227           0 : }
    2228             : 
    2229             : /* Diagnostics */
    2230             : static void
    2231           0 : ifac_factor_dbg(GEN x)
    2232             : {
    2233           0 :   GEN c = CLASS(x), v = VALUE(x);
    2234           0 :   if (c == gen_2) err_printf("IFAC: factor %Ps\n\tis prime (finished)\n", v);
    2235           0 :   else if (c == gen_1) err_printf("IFAC: factor %Ps\n\tis prime\n", v);
    2236           0 :   else if (c == gen_0) err_printf("IFAC: factor %Ps\n\tis composite\n", v);
    2237           0 : }
    2238             : static void
    2239           0 : ifac_check(GEN partial, GEN where)
    2240             : {
    2241           0 :   if (!where || where < FIRST(partial) || where > LAST(partial))
    2242           0 :     pari_err_BUG("ifac_check ['where' out of bounds]");
    2243           0 : }
    2244             : static void
    2245           0 : ifac_print(GEN part, GEN where)
    2246             : {
    2247           0 :   long l = lg(part);
    2248             :   GEN p;
    2249             : 
    2250           0 :   err_printf("ifac partial factorization structure: %ld slots, ", (l-3)/3);
    2251           0 :   if (MOEBIUS(part)) err_printf("Moebius mode, ");
    2252           0 :   err_printf("hint = %ld\n", itos(HINT(part)));
    2253           0 :   ifac_check(part, where);
    2254           0 :   for (p = part+3; p < part + l; p += 3)
    2255             :   {
    2256           0 :     GEN v = VALUE(p), e = EXPON(p), c = CLASS(p);
    2257           0 :     const char *s = "";
    2258           0 :     if (!v) { err_printf("[empty slot]\n"); continue; }
    2259           0 :     if (c == NULL) s = "unknown";
    2260           0 :     else if (c == gen_0) s = "composite";
    2261           0 :     else if (c == gen_1) s = "unfinished prime";
    2262           0 :     else if (c == gen_2) s = "prime";
    2263           0 :     else pari_err_BUG("unknown factor class");
    2264           0 :     err_printf("[%Ps, %Ps, %s]\n", v, e, s);
    2265             :   }
    2266           0 :   err_printf("Done.\n");
    2267           0 : }
    2268             : 
    2269             : static const long decomp_default_hint = 0;
    2270             : /* assume n a non-zero t_INT */
    2271             : /* return initial data structure, see ifac_crack() for the hint argument */
    2272             : static GEN
    2273        3516 : ifac_start_hint(GEN n, int moebius, long hint)
    2274             : {
    2275        3516 :   const long ifac_initial_length = 3 + 7*3;
    2276             :   /* codeword, moebius, hint, 7 slots -- a 512-bit product of distinct 8-bit
    2277             :    * primes needs at most 7 slots at a time) */
    2278        3516 :   GEN here, part = cgetg(ifac_initial_length, t_VEC);
    2279             : 
    2280        3516 :   MOEBIUS(part) = moebius? gen_1 : NULL;
    2281        3516 :   HINT(part) = stoi(hint);
    2282        3516 :   if (isonstack(n)) n = absi(n);
    2283             :   /* make copy, because we'll later want to replace it in place.
    2284             :    * If it's not on stack, then we assume it is a clone made for us by
    2285             :    * ifactor, and we assume the sign has already been set positive */
    2286             :   /* fill first slot at the top end */
    2287        3516 :   here = part + ifac_initial_length - 3; /* LAST(part) */
    2288        3516 :   INIT(here, n,gen_1,gen_0); /* n^1: composite */
    2289        3516 :   while ((here -= 3) > part) ifac_delete(here);
    2290        3516 :   return part;
    2291             : }
    2292             : GEN
    2293        1416 : ifac_start(GEN n, int moebius)
    2294        1416 : { return ifac_start_hint(n,moebius,decomp_default_hint); }
    2295             : 
    2296             : /* Return next nonempty slot after 'here', NULL if none exist */
    2297             : static GEN
    2298       10411 : ifac_find(GEN partial)
    2299             : {
    2300       10411 :   GEN scan, end = partial + lg(partial);
    2301             : 
    2302             : #ifdef IFAC_DEBUG
    2303             :   ifac_check(partial, partial);
    2304             : #endif
    2305       76330 :   for (scan = partial+3; scan < end; scan += 3)
    2306       72856 :     if (VALUE(scan)) return scan;
    2307        3474 :   return NULL;
    2308             : }
    2309             : 
    2310             : /* Defragment: squeeze out unoccupied slots above *where. Unoccupied slots
    2311             :  * arise when a composite factor dissolves completely whilst dividing off a
    2312             :  * prime, or when ifac_resort() spots a coincidence and merges two factors.
    2313             :  * Update *where */
    2314             : static void
    2315         210 : ifac_defrag(GEN *partial, GEN *where)
    2316             : {
    2317         210 :   GEN scan_new = LAST(*partial), scan_old;
    2318             : 
    2319         644 :   for (scan_old = scan_new; scan_old >= *where; scan_old -= 3)
    2320             :   {
    2321         434 :     if (!VALUE(scan_old)) continue; /* empty slot */
    2322         434 :     if (scan_old < scan_new) SHALLOWCOPY(scan_old, scan_new);
    2323         434 :     scan_new -= 3; /* point at next slot to be written */
    2324             :   }
    2325         210 :   scan_new += 3; /* back up to last slot written */
    2326         210 :   *where = scan_new;
    2327         210 :   while ((scan_new -= 3) > *partial) ifac_delete(scan_new); /* erase junk */
    2328         210 : }
    2329             : 
    2330             : /* Move to a larger main vector, updating *where if it points into it, and
    2331             :  * *partial in any case. Can be used as a specialized gcopy before
    2332             :  * a gerepileupto() (pass 0 as the new length). Normally, one would pass
    2333             :  * new_lg=1 to let this function guess the new size.  To be used sparingly.
    2334             :  * Complex version of ifac_defrag(), combined with reallocation.  If new_lg
    2335             :  * is 0, use the old length, so this acts just like gcopy except that the
    2336             :  * 'where' pointer is carried along; if it is 1, we make an educated guess.
    2337             :  * Exception:  If new_lg is 0, the vector is full to the brim, and the first
    2338             :  * entry is composite, we make it longer to avoid being called again a
    2339             :  * microsecond later. It is safe to call this with *where = NULL:
    2340             :  * if it doesn't point anywhere within the old structure, it is left alone */
    2341             : static void
    2342           0 : ifac_realloc(GEN *partial, GEN *where, long new_lg)
    2343             : {
    2344           0 :   long old_lg = lg(*partial);
    2345             :   GEN newpart, scan_new, scan_old;
    2346             : 
    2347           0 :   if (new_lg == 1)
    2348           0 :     new_lg = 2*old_lg - 6;        /* from 7 slots to 13 to 25... */
    2349           0 :   else if (new_lg <= old_lg)        /* includes case new_lg == 0 */
    2350             :   {
    2351           0 :     GEN first = *partial + 3;
    2352           0 :     new_lg = old_lg;
    2353             :     /* structure full and first entry composite or unknown */
    2354           0 :     if (VALUE(first) && (CLASS(first) == gen_0 || CLASS(first)==NULL))
    2355           0 :       new_lg += 6; /* give it a little more breathing space */
    2356             :   }
    2357           0 :   newpart = cgetg(new_lg, t_VEC);
    2358           0 :   if (DEBUGMEM >= 3)
    2359           0 :     err_printf("IFAC: new partial factorization structure (%ld slots)\n",
    2360           0 :                (new_lg - 3)/3);
    2361           0 :   MOEBIUS(newpart) = MOEBIUS(*partial);
    2362           0 :   icopyifstack(HINT(*partial), HINT(newpart));
    2363             :   /* Downward sweep through the old *partial. Pick up 'where' and carry it
    2364             :    * over if we pass it. (Only useful if it pointed at a non-empty slot.)
    2365             :    * Factors are COPY'd so that we again have a nice object (parent older
    2366             :    * than children, connected), except the one factor that may still be living
    2367             :    * in a clone where n originally was; exponents are similarly copied if they
    2368             :    * aren't global constants; class-of-factor fields are global constants so we
    2369             :    * need only copy them as pointers. Caller may then do a gerepileupto() */
    2370           0 :   scan_new = newpart + new_lg - 3; /* LAST(newpart) */
    2371           0 :   scan_old = *partial + old_lg - 3; /* LAST(*partial) */
    2372           0 :   for (; scan_old > *partial + 2; scan_old -= 3)
    2373             :   {
    2374           0 :     if (*where == scan_old) *where = scan_new;
    2375           0 :     if (!VALUE(scan_old)) continue; /* skip empty slots */
    2376           0 :     COPY(scan_old, scan_new); scan_new -= 3;
    2377             :   }
    2378           0 :   scan_new += 3; /* back up to last slot written */
    2379           0 :   while ((scan_new -= 3) > newpart) ifac_delete(scan_new);
    2380           0 :   *partial = newpart;
    2381           0 : }
    2382             : 
    2383             : /* Re-sort one (typically unknown) entry from washere to a new position,
    2384             :  * rotating intervening entries upward to fill the vacant space. If the new
    2385             :  * position is the same as the old one, or the new value of the entry coincides
    2386             :  * with a value already occupying a lower slot, then we just add exponents (and
    2387             :  * use the 'more known' class, and return 1 immediately when in Moebius mode).
    2388             :  * Slots between *where and washere must be in sorted order, so a sweep using
    2389             :  * this to re-sort several unknowns must proceed upward, see ifac_resort().
    2390             :  * Bubble-sort-of-thing sort. Won't be exercised frequently, so this is ok */
    2391             : static void
    2392         105 : ifac_sort_one(GEN *where, GEN washere)
    2393             : {
    2394         105 :   GEN old, scan = washere - 3;
    2395             :   GEN value, exponent, class0, class1;
    2396             :   long cmp_res;
    2397             : 
    2398         105 :   if (scan < *where) return; /* nothing to do, washere==*where */
    2399         105 :   value    = VALUE(washere);
    2400         105 :   exponent = EXPON(washere);
    2401         105 :   class0 = CLASS(washere);
    2402         105 :   cmp_res = -1; /* sentinel */
    2403         210 :   while (scan >= *where) /* at least once */
    2404             :   {
    2405         105 :     if (VALUE(scan))
    2406             :     { /* current slot nonempty, check against where */
    2407         105 :       cmp_res = cmpii(value, VALUE(scan));
    2408         105 :       if (cmp_res >= 0) break; /* have found where to stop */
    2409             :     }
    2410             :     /* copy current slot upward by one position and move pointers down */
    2411           0 :     SHALLOWCOPY(scan, scan+3);
    2412           0 :     scan -= 3;
    2413             :   }
    2414         105 :   scan += 3;
    2415             :   /* At this point there are the following possibilities:
    2416             :    * 1) cmp_res == -1. Either value is less than that at *where, or *where was
    2417             :    * pointing at vacant slots and any factors we saw en route were larger than
    2418             :    * value. At any rate, scan == *where now, and scan is pointing at an empty
    2419             :    * slot, into which we'll stash our entry.
    2420             :    * 2) cmp_res == 0. The entry at scan-3 is the one, we compare class0
    2421             :    * fields and add exponents, and put it all into the vacated scan slot,
    2422             :    * NULLing the one at scan-3 (and possibly updating *where).
    2423             :    * 3) cmp_res == 1. The slot at scan is the one to store our entry into. */
    2424         105 :   if (cmp_res)
    2425             :   {
    2426         105 :     if (cmp_res < 0 && scan != *where)
    2427           0 :       pari_err_BUG("ifact_sort_one [misaligned partial]");
    2428         105 :     INIT(scan, value, exponent, class0); return;
    2429             :   }
    2430             :   /* case cmp_res == 0: repeated factor detected */
    2431           0 :   if (DEBUGLEVEL >= 4)
    2432           0 :     err_printf("IFAC: repeated factor %Ps\n\tin ifac_sort_one\n", value);
    2433           0 :   old = scan - 3;
    2434             :   /* if old class0 was composite and new is prime, or vice versa, complain
    2435             :    * (and if one class0 was unknown and the other wasn't, use the known one) */
    2436           0 :   class1 = CLASS(old);
    2437           0 :   if (class0) /* should never be used */
    2438             :   {
    2439           0 :     if (class1)
    2440             :     {
    2441           0 :       if (class0 == gen_0 && class1 != gen_0)
    2442           0 :         pari_err_BUG("ifac_sort_one (composite = prime)");
    2443           0 :       else if (class0 != gen_0 && class1 == gen_0)
    2444           0 :         pari_err_BUG("ifac_sort_one (prime = composite)");
    2445           0 :       else if (class0 == gen_2)
    2446           0 :         CLASS(scan) = class0;
    2447             :     }
    2448             :     else
    2449           0 :       CLASS(scan) = class0;
    2450             :   }
    2451             :   /* else stay with the existing known class0 */
    2452           0 :   CLASS(scan) = class1;
    2453             :   /* in any case, add exponents */
    2454           0 :   if (EXPON(old) == gen_1 && exponent == gen_1)
    2455           0 :     EXPON(scan) = gen_2;
    2456             :   else
    2457           0 :     EXPON(scan) = addii(EXPON(old), exponent);
    2458             :   /* move the value over and null out the vacated slot below */
    2459           0 :   old = scan - 3;
    2460           0 :   *scan = *old;
    2461           0 :   ifac_delete(old);
    2462             :   /* finally, see whether *where should be pulled in */
    2463           0 :   if (old == *where) *where += 3;
    2464             : }
    2465             : 
    2466             : /* Sort all current unknowns downward to where they belong. Sweeps in the
    2467             :  * upward direction. Not needed after ifac_crack(), only when ifac_divide()
    2468             :  * returned true. Update *where. */
    2469             : static void
    2470         105 : ifac_resort(GEN *partial, GEN *where)
    2471             : {
    2472             :   GEN scan, end;
    2473         105 :   ifac_defrag(partial, where); end = LAST(*partial);
    2474         322 :   for (scan = *where; scan <= end; scan += 3)
    2475         217 :     if (VALUE(scan) && !CLASS(scan)) ifac_sort_one(where, scan); /*unknown*/
    2476         105 :   ifac_defrag(partial, where); /* remove newly created gaps */
    2477         105 : }
    2478             : 
    2479             : /* Let x be a t_INT known not to have small divisors (< 2^14). Return 0 if x
    2480             :  * is a proven composite. Return 1 if we believe it to be prime (fully proven
    2481             :  * prime if factor_proven is set).  */
    2482             : int
    2483       10293 : ifac_isprime(GEN x)
    2484             : {
    2485       10293 :   if (!BPSW_psp_nosmalldiv(x)) return 0; /* composite */
    2486        8383 :   if (factor_proven && ! BPSW_isprime(x))
    2487             :   {
    2488           0 :     pari_warn(warner,
    2489             :               "IFAC: pseudo-prime %Ps\n\tis not prime. PLEASE REPORT!\n", x);
    2490           0 :     return 0;
    2491             :   }
    2492        8383 :   return 1;
    2493             : }
    2494             : 
    2495             : static int
    2496        7218 : ifac_checkprime(GEN x)
    2497             : {
    2498        7218 :   int res = ifac_isprime(VALUE(x));
    2499        7218 :   CLASS(x) = res? gen_1: gen_0;
    2500        7218 :   if (DEBUGLEVEL>2) ifac_factor_dbg(x);
    2501        7218 :   return res;
    2502             : }
    2503             : 
    2504             : /* Determine primality or compositeness of all current unknowns, and set
    2505             :  * class Q primes to finished (class P) if everything larger is already
    2506             :  * known to be prime.  When after_crack >= 0, only look at the
    2507             :  * first after_crack things in the list (do nothing when it's 0) */
    2508             : static void
    2509        3761 : ifac_whoiswho(GEN *partial, GEN *where, long after_crack)
    2510             : {
    2511        3761 :   GEN scan, scan_end = LAST(*partial);
    2512             : 
    2513             : #ifdef IFAC_DEBUG
    2514             :   ifac_check(*partial, *where);
    2515             : #endif
    2516        7522 :   if (after_crack == 0) return;
    2517        3492 :   if (after_crack > 0) /* check at most after_crack entries */
    2518        3387 :     scan = *where + 3*(after_crack - 1); /* assert(scan <= scan_end) */
    2519             :   else
    2520         301 :     for (scan = scan_end; scan >= *where; scan -= 3)
    2521             :     {
    2522         203 :       if (CLASS(scan))
    2523             :       { /* known class of factor */
    2524         105 :         if (CLASS(scan) == gen_0) break;
    2525          98 :         if (CLASS(scan) == gen_1)
    2526             :         {
    2527           0 :           if (DEBUGLEVEL>=3)
    2528             :           {
    2529           0 :             err_printf("IFAC: factor %Ps\n\tis prime (no larger composite)\n",
    2530           0 :                        VALUE(*where));
    2531           0 :             err_printf("IFAC: prime %Ps\n\tappears with exponent = %ld\n",
    2532           0 :                        VALUE(*where), itos(EXPON(*where)));
    2533             :           }
    2534           0 :           CLASS(scan) = gen_2;
    2535             :         }
    2536          98 :         continue;
    2537             :       }
    2538          98 :       if (!ifac_checkprime(scan)) break; /* must disable Q-to-P */
    2539          98 :       CLASS(scan) = gen_2; /* P_i, finished prime */
    2540          98 :       if (DEBUGLEVEL>2) ifac_factor_dbg(scan);
    2541             :     }
    2542             :   /* go on, Q-to-P trick now disabled */
    2543       10315 :   for (; scan >= *where; scan -= 3)
    2544             :   {
    2545        6823 :     if (CLASS(scan)) continue;
    2546        6809 :     (void)ifac_checkprime(scan); /* Qj | Ck */
    2547             :   }
    2548             : }
    2549             : 
    2550             : /* Divide all current composites by first (prime, class Q) entry, updating its
    2551             :  * exponent, and turning it into a finished prime (class P).  Return 1 if any
    2552             :  * such divisions succeeded  (in Moebius mode, the update may then not have
    2553             :  * been completed), or 0 if none of them succeeded.  Doesn't modify *where.
    2554             :  * Here we normally do not check that the first entry is a not-finished
    2555             :  * prime.  Stack management: we may allocate a new exponent */
    2556             : static long
    2557        6766 : ifac_divide(GEN *partial, GEN *where, long moebius_mode)
    2558             : {
    2559        6766 :   GEN scan, scan_end = LAST(*partial);
    2560        6766 :   long res = 0, exponent, newexp, otherexp;
    2561             : 
    2562             : #ifdef IFAC_DEBUG
    2563             :   ifac_check(*partial, *where);
    2564             :   if (CLASS(*where) != gen_1)
    2565             :     pari_err_BUG("ifac_divide [division by composite or finished prime]");
    2566             :   if (!VALUE(*where)) pari_err_BUG("ifac_divide [division by nothing]");
    2567             : #endif
    2568        6766 :   newexp = exponent = itos(EXPON(*where));
    2569        6766 :   if (exponent > 1 && moebius_mode) return 1;
    2570             :   /* should've been caught by caller */
    2571             : 
    2572       10195 :   for (scan = *where+3; scan <= scan_end; scan += 3)
    2573             :   {
    2574        3436 :     if (CLASS(scan) != gen_0) continue; /* the other thing ain't composite */
    2575         298 :     otherexp = 0;
    2576             :     /* divide in place to keep stack clutter minimal */
    2577         701 :     while (dvdiiz(VALUE(scan), VALUE(*where), VALUE(scan)))
    2578             :     {
    2579         112 :       if (moebius_mode) return 1; /* immediately */
    2580         105 :       if (!otherexp) otherexp = itos(EXPON(scan));
    2581         105 :       newexp += otherexp;
    2582             :     }
    2583         291 :     if (newexp > exponent)        /* did anything happen? */
    2584             :     {
    2585         105 :       EXPON(*where) = (newexp == 2 ? gen_2 : utoipos(newexp));
    2586         105 :       exponent = newexp;
    2587         105 :       if (is_pm1((GEN)*scan)) /* factor dissolved completely */
    2588             :       {
    2589           0 :         ifac_delete(scan);
    2590           0 :         if (DEBUGLEVEL >= 4)
    2591           0 :           err_printf("IFAC: a factor was a power of another prime factor\n");
    2592             :       } else {
    2593         105 :         CLASS(scan) = NULL;        /* at any rate it's Unknown now */
    2594         105 :         if (DEBUGLEVEL >= 4)
    2595           0 :           err_printf("IFAC: a factor was divisible by another prime factor,\n"
    2596             :                      "\tleaving a cofactor = %Ps\n", VALUE(scan));
    2597             :       }
    2598         105 :       res = 1;
    2599         105 :       if (DEBUGLEVEL >= 5)
    2600           0 :         err_printf("IFAC: prime %Ps\n\tappears at least to the power %ld\n",
    2601           0 :                    VALUE(*where), newexp);
    2602             :     }
    2603             :   } /* for */
    2604        6759 :   CLASS(*where) = gen_2; /* make it a finished prime */
    2605        6759 :   if (DEBUGLEVEL >= 3)
    2606           0 :     err_printf("IFAC: prime %Ps\n\tappears with exponent = %ld\n",
    2607           0 :                VALUE(*where), newexp);
    2608        6759 :   return res;
    2609             : }
    2610             : 
    2611             : /* found out our integer was factor^exp. Update */
    2612             : static void
    2613         367 : update_pow(GEN where, GEN factor, long exp, pari_sp *av)
    2614             : {
    2615         367 :   GEN ex = EXPON(where);
    2616         367 :   if (DEBUGLEVEL>3)
    2617           0 :     err_printf("IFAC: found %Ps =\n\t%Ps ^%ld\n", *where, factor, exp);
    2618         367 :   affii(factor, VALUE(where)); avma = *av;
    2619         367 :   if (ex == gen_1)
    2620         318 :   { EXPON(where) = exp == 2? gen_2: utoipos(exp); *av = avma; }
    2621          49 :   else if (ex == gen_2)
    2622          42 :   { EXPON(where) = utoipos(exp<<1); *av = avma; }
    2623             :   else
    2624           7 :     affsi(exp * itos(ex), EXPON(where));
    2625         367 : }
    2626             : /* hint = 0 : Use a default strategy
    2627             :  * hint & 1 : avoid MPQS
    2628             :  * hint & 2 : avoid first-stage ECM (may fall back to ECM if MPQS gives up)
    2629             :  * hint & 4 : avoid Pollard and SQUFOF stages.
    2630             :  * hint & 8 : avoid final ECM; may flag a composite as prime. */
    2631             : #define get_hint(partial) (itos(HINT(*partial)) & 15)
    2632             : 
    2633             : /* Split the first (composite) entry.  There _must_ already be room for another
    2634             :  * factor below *where, and *where is updated. Two cases:
    2635             :  * - entry = factor^k is a pure power: factor^k is inserted, leaving *where
    2636             :  *   unchanged;
    2637             :  * - entry = factor * cofactor (not necessarily coprime): both factors are
    2638             :  *   inserted in the correct order, updating *where
    2639             :  * The inserted factors class is set to unknown, they inherit the exponent
    2640             :  * (or a multiple thereof) of their ancestor.
    2641             :  *
    2642             :  * Returns number of factors written into the structure, normally 2 (1 if pure
    2643             :  * power, maybe > 2 if a factoring engine returned a vector of factors instead
    2644             :  * of a single factor). Can reallocate the data structure in the
    2645             :  * vector-of-factors case, not in the most common single-factor case.
    2646             :  * Stack housekeeping:  this routine may create one or more objects  (a new
    2647             :  * factor, or possibly several, and perhaps one or more new exponents > 2) */
    2648             : static long
    2649        3663 : ifac_crack(GEN *partial, GEN *where, long moebius_mode)
    2650             : {
    2651        3663 :   long cmp_res, hint = get_hint(partial);
    2652             :   GEN factor, exponent;
    2653             : 
    2654             : #ifdef IFAC_DEBUG
    2655             :   ifac_check(*partial, *where);
    2656             :   if (*where < *partial + 6)
    2657             :     pari_err_BUG("ifac_crack ['*where' out of bounds]");
    2658             :   if (!(VALUE(*where)) || typ(VALUE(*where)) != t_INT)
    2659             :     pari_err_BUG("ifac_crack [incorrect VALUE(*where)]");
    2660             :   if (CLASS(*where) != gen_0)
    2661             :     pari_err_BUG("ifac_crack [operand not known composite]");
    2662             : #endif
    2663             : 
    2664        3663 :   if (DEBUGLEVEL>2) {
    2665           0 :     err_printf("IFAC: cracking composite\n\t%Ps\n", **where);
    2666           0 :     if (DEBUGLEVEL>3) err_printf("IFAC: checking for pure square\n");
    2667             :   }
    2668             :   /* MPQS cannot factor prime powers. Look for pure powers even if MPQS is
    2669             :    * blocked by hint: fast and useful in bounded factorization */
    2670             :   {
    2671             :     forprime_t T;
    2672        3663 :     ulong exp = 1, mask = 7;
    2673        3663 :     long good = 0;
    2674        3663 :     pari_sp av = avma;
    2675        3663 :     (void)u_forprime_init(&T, 11, ULONG_MAX);
    2676             :     /* crack squares */
    2677        3663 :     while (Z_issquareall(VALUE(*where), &factor))
    2678             :     {
    2679         332 :       good = 1; /* remember we succeeded once */
    2680         332 :       update_pow(*where, factor, 2, &av);
    2681         608 :       if (moebius_mode) return 0; /* no need to carry on */
    2682             :     }
    2683        7347 :     while ( (exp = is_357_power(VALUE(*where), &factor, &mask)) )
    2684             :     {
    2685          35 :       good = 1; /* remember we succeeded once */
    2686          35 :       update_pow(*where, factor, exp, &av);
    2687          35 :       if (moebius_mode) return 0; /* no need to carry on */
    2688             :     }
    2689             :     /* cutoff at 14 bits as trial division must have found everything below */
    2690        7312 :     while ( (exp = is_pth_power(VALUE(*where), &factor, &T, 15)) )
    2691             :     {
    2692           0 :       good = 1; /* remember we succeeded once */
    2693           0 :       update_pow(*where, factor, exp, &av);
    2694           0 :       if (moebius_mode) return 0; /* no need to carry on */
    2695             :     }
    2696             : 
    2697        3656 :     if (good && hint != 15 && ifac_checkprime(*where))
    2698             :     { /* our composite was a prime power */
    2699         269 :       if (DEBUGLEVEL>3)
    2700           0 :         err_printf("IFAC: factor %Ps\n\tis prime\n", VALUE(*where));
    2701         269 :       return 0; /* bypass subsequent ifac_whoiswho() call */
    2702             :     }
    2703             :   } /* pure power stage */
    2704             : 
    2705        3387 :   factor = NULL;
    2706        3387 :   if (!(hint & 4))
    2707             :   { /* pollardbrent() Rho usually gets a first chance */
    2708        3387 :     if (DEBUGLEVEL >= 4) err_printf("IFAC: trying Pollard-Brent rho method\n");
    2709        3387 :     factor = pollardbrent(VALUE(*where));
    2710        3387 :     if (!factor)
    2711             :     { /* Shanks' squfof() */
    2712        2130 :       if (DEBUGLEVEL >= 4)
    2713           0 :         err_printf("IFAC: trying Shanks' SQUFOF, will fail silently if input\n"
    2714             :                    "      is too large for it.\n");
    2715        2130 :       factor = squfof(VALUE(*where));
    2716             :     }
    2717             :   }
    2718        3387 :   if (!factor && !(hint & 2))
    2719             :   { /* First ECM stage */
    2720         379 :     if (DEBUGLEVEL >= 4) err_printf("IFAC: trying Lenstra-Montgomery ECM\n");
    2721         379 :     factor = ellfacteur(VALUE(*where), 0); /* do not insist */
    2722             :   }
    2723        3387 :   if (!factor && !(hint & 1))
    2724             :   { /* MPQS stage */
    2725         340 :     if (DEBUGLEVEL >= 4) err_printf("IFAC: trying MPQS\n");
    2726         340 :     factor = mpqs(VALUE(*where));
    2727             :   }
    2728        3387 :   if (!factor)
    2729             :   {
    2730          14 :     if (!(hint & 8))
    2731             :     { /* still no luck? Final ECM stage, guaranteed to succeed */
    2732          14 :       if (DEBUGLEVEL >= 4)
    2733           0 :         err_printf("IFAC: forcing ECM, may take some time\n");
    2734          14 :       factor = ellfacteur(VALUE(*where), 1);
    2735             :     }
    2736             :     else
    2737             :     { /* limited factorization */
    2738           0 :       if (DEBUGLEVEL >= 2)
    2739             :       {
    2740           0 :         if (hint != 15)
    2741           0 :           pari_warn(warner, "IFAC: unfactored composite declared prime");
    2742             :         else
    2743           0 :           pari_warn(warner, "IFAC: untested integer declared prime");
    2744             : 
    2745             :         /* don't print it out at level 3 or above, where it would appear
    2746             :          * several times before and after this message already */
    2747           0 :         if (DEBUGLEVEL == 2) err_printf("\t%Ps\n", VALUE(*where));
    2748             :       }
    2749           0 :       CLASS(*where) = gen_1; /* might as well trial-divide by it... */
    2750           0 :       return 1;
    2751             :     }
    2752             :   }
    2753        3387 :   if (typ(factor) == t_VEC) /* delegate this case */
    2754         473 :     return ifac_insert_multiplet(partial, where, factor, moebius_mode);
    2755             :   /* typ(factor) == t_INT */
    2756             :   /* got single integer back:  work out the cofactor (in place) */
    2757        2914 :   if (!dvdiiz(VALUE(*where), factor, VALUE(*where)))
    2758             :   {
    2759           0 :     err_printf("IFAC: factoring %Ps\n", VALUE(*where));
    2760           0 :     err_printf("\tyielded 'factor' %Ps\n\twhich isn't!\n", factor);
    2761           0 :     pari_err_BUG("factoring");
    2762             :   }
    2763             :   /* factoring engines report the factor found; tell about the cofactor */
    2764        2914 :   if (DEBUGLEVEL >= 4) err_printf("IFAC: cofactor = %Ps\n", VALUE(*where));
    2765             : 
    2766             :   /* The two factors are 'factor' and VALUE(*where), find out which is larger */
    2767        2914 :   cmp_res = cmpii(factor, VALUE(*where));
    2768        2914 :   CLASS(*where) = NULL; /* mark factor /cofactor 'unknown' */
    2769        2914 :   exponent = EXPON(*where);
    2770        2914 :   *where -= 3;
    2771        2914 :   CLASS(*where) = NULL; /* mark factor /cofactor 'unknown' */
    2772        2914 :   EXPON(*where) = isonstack(exponent)? icopy(exponent): exponent;
    2773        2914 :   if (cmp_res < 0)
    2774        2739 :     VALUE(*where) = factor; /* common case */
    2775         175 :   else if (cmp_res > 0)
    2776             :   { /* factor > cofactor, rearrange */
    2777         175 :     GEN old = *where + 3;
    2778         175 :     VALUE(*where) = VALUE(old); /* move cofactor pointer to lowest slot */
    2779         175 :     VALUE(old) = factor; /* save factor */
    2780             :   }
    2781           0 :   else pari_err_BUG("ifac_crack [Z_issquareall miss]");
    2782        2914 :   return 2;
    2783             : }
    2784             : 
    2785             : /* Gets called to complete ifac_crack's job when a factoring engine splits
    2786             :  * the current factor into a product of three or more new factors. Makes room
    2787             :  * for them if necessary, sorts them, gives them the right exponents and class.
    2788             :  * Also returns the number of factors actually written, which may be less than
    2789             :  * the number of components in facvec if there are duplicates.--- Vectors of
    2790             :  * factors  (cf pollardbrent()) actually contain 'slots' of three GENs per
    2791             :  * factor with the three fields interpreted as in our partial factorization
    2792             :  * data structure.  Thus 'engines' can tell us what they already happen to
    2793             :  * know about factors being prime or composite and/or appearing to a power
    2794             :  * larger than the first.
    2795             :  * Don't collect garbage.  No diagnostics: the factoring engine should have
    2796             :  * printed what it found. facvec contains slots of three components per factor;
    2797             :  * repeated factors are allowed  (and their classes shouldn't contradict each
    2798             :  * other whereas their exponents will be added up) */
    2799             : static long
    2800         473 : ifac_insert_multiplet(GEN *partial, GEN *where, GEN facvec, long moebius_mode)
    2801             : {
    2802         473 :   long j,k=1, lfv=lg(facvec)-1, nf=lfv/3, room=(long)(*where-*partial);
    2803             :   /* one of the factors will go into the *where slot, so room is now 3 times
    2804             :    * the number of slots we can use */
    2805         473 :   long needroom = lfv - room;
    2806         473 :   GEN e, newexp, cur, sorted, auxvec = cgetg(nf+1, t_VEC), factor;
    2807         473 :   long exponent = itos(EXPON(*where)); /* the old exponent */
    2808             : 
    2809         473 :   if (DEBUGLEVEL >= 5) /* squfof may return a single squared factor as a set */
    2810           0 :     err_printf("IFAC: incorporating set of %ld factor(s)\n", nf);
    2811         473 :   if (needroom > 0) /* one extra slot for paranoia, errm, future use */
    2812           0 :     ifac_realloc(partial, where, lg(*partial) + needroom + 3);
    2813             : 
    2814             :   /* create sort permutation from the values of the factors */
    2815         473 :   for (j=nf; j; j--) auxvec[j] = facvec[3*j-2]; /* just the pointers */
    2816         473 :   sorted = indexsort(auxvec);
    2817             :   /* and readjust the result for the triple spacing */
    2818         473 :   for (j=nf; j; j--) sorted[j] = 3*sorted[j]-2;
    2819             : 
    2820             :   /* store factors, beginning at *where, and catching any duplicates */
    2821         473 :   cur = facvec + sorted[nf];
    2822         473 :   VALUE(*where) = VALUE(cur);
    2823         473 :   newexp = EXPON(cur);
    2824         473 :   if (newexp != gen_1) /* new exponent > 1 */
    2825             :   {
    2826           0 :     if (exponent == 1)
    2827           0 :       e = isonstack(newexp)? icopy(newexp): newexp;
    2828             :     else
    2829           0 :       e = mului(exponent, newexp);
    2830           0 :     EXPON(*where) = e;
    2831             :   } /* if new exponent is 1, the old exponent already in place will do */
    2832         473 :   CLASS(*where) = CLASS(cur);
    2833         473 :   if (DEBUGLEVEL >= 6) err_printf("\tstored (largest) factor no. %ld...\n", nf);
    2834             : 
    2835         974 :   for (j=nf-1; j; j--)
    2836             :   {
    2837         501 :     cur = facvec + sorted[j];
    2838         501 :     factor = VALUE(cur);
    2839         501 :     if (equalii(factor, VALUE(*where)))
    2840             :     {
    2841           0 :       if (DEBUGLEVEL >= 6)
    2842           0 :         err_printf("\tfactor no. %ld is a duplicate%s\n", j, (j>1? "...": ""));
    2843             :       /* update exponent, ignore class which would already have been set,
    2844             :        * then forget current factor */
    2845           0 :       newexp = EXPON(cur);
    2846           0 :       if (newexp != gen_1) /* new exp > 1 */
    2847           0 :         e = addis(EXPON(*where), exponent * itos(newexp));
    2848           0 :       else if (EXPON(*where) == gen_1 && exponent == 1)
    2849           0 :         e = gen_2;
    2850             :       else
    2851           0 :         e = addis(EXPON(*where), exponent);
    2852           0 :       EXPON(*where) = e;
    2853             : 
    2854           0 :       if (moebius_mode) return 0; /* stop now, but with exponent updated */
    2855           0 :       continue;
    2856             :     }
    2857             : 
    2858         501 :     *where -= 3;
    2859         501 :     CLASS(*where) = CLASS(cur);        /* class as given */
    2860         501 :     newexp = EXPON(cur);
    2861         501 :     if (newexp != gen_1) /* new exp > 1 */
    2862             :     {
    2863           0 :       if (exponent == 1 && newexp == gen_2)
    2864           0 :         e = gen_2;
    2865             :       else /* exponent*newexp > 2 */
    2866           0 :         e = mului(exponent, newexp);
    2867             :     }
    2868             :     else
    2869         529 :       e = (exponent == 1 ? gen_1 :
    2870          28 :             (exponent == 2 ? gen_2 :
    2871           0 :                utoipos(exponent))); /* inherit parent's exponent */
    2872         501 :     EXPON(*where) = e;
    2873             :     /* keep components younger than *partial */
    2874         501 :     VALUE(*where) = isonstack(factor) ? icopy(factor) : factor;
    2875         501 :     k++;
    2876         501 :     if (DEBUGLEVEL >= 6)
    2877           0 :       err_printf("\tfactor no. %ld was unique%s\n", j, j>1? " (so far)...": "");
    2878             :   }
    2879             :   /* make the 'sorted' object safe for garbage collection (it should be in the
    2880             :    * garbage zone from everybody's perspective, but it's easy to do it) */
    2881         473 :   *sorted = evaltyp(t_INT) | evallg(nf+1);
    2882         473 :   return k;
    2883             : }
    2884             : 
    2885             : /* main loop:  iterate until smallest entry is a finished prime;  returns
    2886             :  * a 'where' pointer, or NULL if nothing left, or gen_0 in Moebius mode if
    2887             :  * we aren't squarefree */
    2888             : static GEN
    2889       10291 : ifac_main(GEN *partial)
    2890             : {
    2891       10291 :   const long moebius_mode = !!MOEBIUS(*partial);
    2892       10291 :   GEN here = ifac_find(*partial);
    2893             :   long nf;
    2894             : 
    2895       10291 :   if (!here) return NULL; /* nothing left */
    2896             :   /* loop until first entry is a finished prime.  May involve reallocations,
    2897             :    * thus updates of *partial */
    2898       24129 :   while (CLASS(here) != gen_2)
    2899             :   {
    2900       10429 :     if (CLASS(here) == gen_0) /* composite: crack it */
    2901             :     { /* make sure there's room for another factor */
    2902        3663 :       if (here < *partial + 6)
    2903             :       {
    2904           0 :         ifac_defrag(partial, &here);
    2905           0 :         if (here < *partial + 6) ifac_realloc(partial, &here, 1); /* no luck */
    2906             :       }
    2907        3663 :       nf = ifac_crack(partial, &here, moebius_mode);
    2908        3663 :       if (moebius_mode && EXPON(here) != gen_1) /* that was a power */
    2909             :       {
    2910           7 :         if (DEBUGLEVEL >= 3)
    2911           0 :           err_printf("IFAC: main loop: repeated new factor\n\t%Ps\n", *here);
    2912           7 :         return gen_0;
    2913             :       }
    2914             :       /* deal with the new unknowns.  No sort: ifac_crack did it */
    2915        3656 :       ifac_whoiswho(partial, &here, nf);
    2916        3656 :       continue;
    2917             :     }
    2918        6766 :     if (CLASS(here) == gen_1) /* prime but not yet finished: finish it */
    2919             :     {
    2920        6766 :       if (ifac_divide(partial, &here, moebius_mode))
    2921             :       {
    2922         112 :         if (moebius_mode)
    2923             :         {
    2924           7 :           if (DEBUGLEVEL >= 3)
    2925           0 :             err_printf("IFAC: main loop: another factor was divisible by\n"
    2926             :                        "\t%Ps\n", *here);
    2927           7 :           return gen_0;
    2928             :         }
    2929         105 :         ifac_resort(partial, &here); /* sort new cofactors down */
    2930         105 :         ifac_whoiswho(partial, &here, -1);
    2931             :       }
    2932        6759 :       continue;
    2933             :     }
    2934           0 :     pari_err_BUG("ifac_main [non-existent factor class]");
    2935             :   } /* while */
    2936        6843 :   if (moebius_mode && EXPON(here) != gen_1)
    2937             :   {
    2938           0 :     if (DEBUGLEVEL >= 3)
    2939           0 :       err_printf("IFAC: after main loop: repeated old factor\n\t%Ps\n", *here);
    2940           0 :     return gen_0;
    2941             :   }
    2942        6843 :   if (DEBUGLEVEL >= 4)
    2943             :   {
    2944           0 :     nf = (*partial + lg(*partial) - here - 3)/3;
    2945           0 :     if (nf)
    2946           0 :       err_printf("IFAC: main loop: %ld factor%s left\n", nf, (nf>1)? "s": "");
    2947             :     else
    2948           0 :       err_printf("IFAC: main loop: this was the last factor\n");
    2949             :   }
    2950        6843 :   if (factor_add_primes && !(get_hint(partial) & 8))
    2951             :   {
    2952           0 :     GEN p = VALUE(here);
    2953           0 :     if (lgefint(p)>3 || uel(p,2) > 0x1000000UL) (void)addprimes(p);
    2954             :   }
    2955        6843 :   return here;
    2956             : }
    2957             : 
    2958             : /* Encapsulated routines */
    2959             : 
    2960             : /* prime/exponent pairs need to appear contiguously on the stack, but we also
    2961             :  * need our data structure somewhere, and we don't know in advance how many
    2962             :  * primes will turn up.  The following discipline achieves this:  When
    2963             :  * ifac_decomp() is called, n should point at an object older than the oldest
    2964             :  * small prime/exponent pair  (ifactor() guarantees this).
    2965             :  * We allocate sufficient space to accommodate several pairs -- eleven pairs
    2966             :  * ought to fit in a space not much larger than n itself -- before calling
    2967             :  * ifac_start().  If we manage to complete the factorization before we run out
    2968             :  * of space, we free the data structure and cull the excess reserved space
    2969             :  * before returning.  When we do run out, we have to leapfrog to generate more
    2970             :  * (guesstimating the requirements from what is left in the partial
    2971             :  * factorization structure);  room for fresh pairs is allocated at the head of
    2972             :  * the stack, followed by an ifac_realloc() to reconnect the data structure and
    2973             :  * move it out of the way, followed by a few pointer tweaks to connect the new
    2974             :  * pairs space to the old one. This whole affair translates into a surprisingly
    2975             :  * compact routine. */
    2976             : 
    2977             : /* find primary factors of n */
    2978             : static long
    2979        1222 : ifac_decomp(GEN n, long hint)
    2980             : {
    2981        1222 :   pari_sp av = avma;
    2982        1222 :   long nb = 0;
    2983        1222 :   GEN part, here, workspc, pairs = (GEN)av;
    2984             : 
    2985             :   /* workspc will be doled out in pairs of smaller t_INTs. For n = prod p^{e_p}
    2986             :    * (p not necessarily prime), need room to store all p and e_p [ cgeti(3) ],
    2987             :    * bounded by
    2988             :    *    sum_{p | n} ( log_{2^BIL} (p) + 6 ) <= log_{2^BIL} n + 6 log_2 n */
    2989        1222 :   workspc = new_chunk((expi(n) + 1) * 7);
    2990        1222 :   part = ifac_start_hint(n, 0, hint);
    2991             :   for (;;)
    2992             :   {
    2993        3651 :     here = ifac_main(&part);
    2994        3651 :     if (!here) break;
    2995        2429 :     if (gc_needed(av,1))
    2996             :     {
    2997             :       long offset;
    2998           0 :       if(DEBUGMEM>1)
    2999             :       {
    3000           0 :         pari_warn(warnmem,"[2] ifac_decomp");
    3001           0 :         ifac_print(part, here);
    3002             :       }
    3003           0 :       ifac_realloc(&part, &here, 0);
    3004           0 :       offset = here - part;
    3005           0 :       part = gerepileupto((pari_sp)workspc, part);
    3006           0 :       here = part + offset;
    3007             :     }
    3008        2429 :     nb++;
    3009        2429 :     pairs = icopy_avma(VALUE(here), (pari_sp)pairs);
    3010        2429 :     pairs = icopy_avma(EXPON(here), (pari_sp)pairs);
    3011        2429 :     ifac_delete(here);
    3012        2429 :   }
    3013        1222 :   avma = (pari_sp)pairs;
    3014        1222 :   if (DEBUGLEVEL >= 3)
    3015           0 :     err_printf("IFAC: found %ld large prime (power) factor%s.\n",
    3016             :                nb, (nb>1? "s": ""));
    3017        1222 :   return nb;
    3018             : }
    3019             : 
    3020             : /***********************************************************************/
    3021             : /**            ARITHMETIC FUNCTIONS WITH EARLY-ABORT                  **/
    3022             : /**  needing direct access to the factoring machinery to avoid work:  **/
    3023             : /**  e.g. if we find a square factor, moebius returns 0, core doesn't **/
    3024             : /**  need to factor it, etc.                                          **/
    3025             : /***********************************************************************/
    3026             : /* memory management */
    3027             : static void
    3028           0 : ifac_GC(pari_sp av, GEN *part)
    3029             : {
    3030           0 :   GEN here = NULL;
    3031           0 :   if(DEBUGMEM>1) pari_warn(warnmem,"ifac_xxx");
    3032           0 :   ifac_realloc(part, &here, 0);
    3033           0 :   *part = gerepileupto(av, *part);
    3034           0 : }
    3035             : 
    3036             : static long
    3037         196 : ifac_moebius(GEN n)
    3038             : {
    3039         196 :   long mu = 1;
    3040         196 :   pari_sp av = avma;
    3041         196 :   GEN part = ifac_start(n, 1);
    3042             :   for(;;)
    3043             :   {
    3044             :     long v;
    3045             :     GEN p;
    3046         756 :     if (!ifac_next(&part,&p,&v)) return v? 0: mu;
    3047         364 :     mu = -mu;
    3048         364 :     if (gc_needed(av,1)) ifac_GC(av,&part);
    3049         364 :   }
    3050             : }
    3051             : 
    3052             : int
    3053          88 : ifac_read(GEN part, GEN *p, long *e)
    3054             : {
    3055          88 :   GEN here = ifac_find(part);
    3056          88 :   if (!here) return 0;
    3057          48 :   *p = VALUE(here);
    3058          48 :   *e = EXPON(here)[2];
    3059          48 :   return 1;
    3060             : }
    3061             : void
    3062          32 : ifac_skip(GEN part)
    3063             : {
    3064          32 :   GEN here = ifac_find(part);
    3065          32 :   if (here) ifac_delete(here);
    3066          32 : }
    3067             : 
    3068             : static int
    3069           7 : ifac_ispowerful(GEN n)
    3070             : {
    3071           7 :   pari_sp av = avma;
    3072           7 :   GEN part = ifac_start(n, 0);
    3073             :   for(;;)
    3074             :   {
    3075             :     long e;
    3076             :     GEN p;
    3077          21 :     if (!ifac_read(part,&p,&e)) return 1;
    3078             :     /* power: skip */
    3079           7 :     if (e != 1 || Z_isanypower(p,NULL)) { ifac_skip(part); continue; }
    3080           0 :     if (!ifac_next(&part,&p,&e)) return 1;
    3081           0 :     if (e == 1) return 0;
    3082           0 :     if (gc_needed(av,1)) ifac_GC(av,&part);
    3083           7 :   }
    3084             : }
    3085             : static GEN
    3086          33 : ifac_core(GEN n)
    3087             : {
    3088          33 :   GEN m = gen_1, c = cgeti(lgefint(n));
    3089          33 :   pari_sp av = avma;
    3090          33 :   GEN part = ifac_start(n, 0);
    3091             :   for(;;)
    3092             :   {
    3093             :     long e;
    3094             :     GEN p;
    3095         107 :     if (!ifac_read(part,&p,&e)) return m;
    3096             :     /* square: skip */
    3097          41 :     if (!odd(e) || Z_issquare(p)) { ifac_skip(part); continue; }
    3098          16 :     if (!ifac_next(&part,&p,&e)) return m;
    3099          16 :     if (odd(e)) m = mulii(m, p);
    3100          16 :     if (gc_needed(av,1)) { affii(m,c); m=c; ifac_GC(av,&part); }
    3101          41 :   }
    3102             : }
    3103             : 
    3104             : /* Where to stop trial dividing in factorization. Guaranteed >= 2^14 */
    3105             : ulong
    3106       23202 : tridiv_bound(GEN n)
    3107             : {
    3108       23202 :   ulong l = (ulong)expi(n) + 1;
    3109       23203 :   if (l <= 32)  return 1UL<<14;
    3110       22622 :   if (l <= 512) return (l-16) << 10;
    3111          56 :   return 1UL<<19; /* Rho is generally faster above this */
    3112             : }
    3113             : 
    3114             : /* return a value <= (48 << 10) = 49152 < primelinit */
    3115             : static ulong
    3116    15072247 : utridiv_bound(ulong n)
    3117             : {
    3118             : #ifdef LONG_IS_64BIT
    3119    12866064 :   if (n & HIGHMASK)
    3120       84768 :     return ((ulong)expu(n) + 1 - 16) << 10;
    3121             : #else
    3122             :   (void)n;
    3123             : #endif
    3124    14987479 :   return 1UL<<14;
    3125             : }
    3126             : 
    3127             : static void
    3128         878 : ifac_factoru(GEN n, long hint, GEN P, GEN E, long *pi)
    3129             : {
    3130         878 :   GEN part = ifac_start_hint(n, 0, hint);
    3131             :   for(;;)
    3132             :   {
    3133             :     long v;
    3134             :     GEN p;
    3135        3500 :     if (!ifac_next(&part,&p,&v)) return;
    3136        1744 :     P[*pi] = itou(p);
    3137        1744 :     E[*pi] = v;
    3138        1744 :     (*pi)++;
    3139        1744 :   }
    3140             : }
    3141             : static long
    3142        1068 : ifac_moebiusu(GEN n)
    3143             : {
    3144        1068 :   GEN part = ifac_start(n, 1);
    3145        1068 :   long s = 1;
    3146             :   for(;;)
    3147             :   {
    3148             :     long v;
    3149             :     GEN p;
    3150        4272 :     if (!ifac_next(&part,&p,&v)) return v? 0: s;
    3151        2136 :     s = -s;
    3152        2136 :   }
    3153             : }
    3154             : 
    3155             : INLINE ulong
    3156   433194994 : u_forprime_next_fast(forprime_t *T)
    3157             : {
    3158   433194994 :   if (*(T->d))
    3159             :   {
    3160   433176961 :     NEXT_PRIME_VIADIFF(T->p, T->d);
    3161   433176961 :     return T->p > T->b ? 0: T->p;
    3162             :   }
    3163       18033 :   return u_forprime_next(T);
    3164             : }
    3165             : 
    3166             : /* Factor n and output [p,e] where
    3167             :  * p, e are vecsmall with n = prod{p[i]^e[i]} */
    3168             : static GEN
    3169    15375245 : factoru_sign(ulong n, ulong all, long hint)
    3170             : {
    3171             :   GEN f, E, E2, P, P2;
    3172             :   pari_sp av;
    3173             :   ulong p, lim;
    3174             :   long i;
    3175             :   forprime_t S;
    3176             : 
    3177    15375245 :   if (n == 0) retmkvec2(mkvecsmall(0), mkvecsmall(1));
    3178    15375245 :   if (n == 1) retmkvec2(cgetg(1,t_VECSMALL), cgetg(1,t_VECSMALL));
    3179             : 
    3180    14983806 :   f = cgetg(3,t_VEC); av = avma;
    3181    14983771 :   lim = all; if (!lim) lim = utridiv_bound(n);
    3182             :   /* enough room to store <= 15 primes and exponents (OK if n < 2^64) */
    3183    14983779 :   (void)new_chunk(16*2);
    3184    14983818 :   P = cgetg(16, t_VECSMALL); i = 1;
    3185    14983767 :   E = cgetg(16, t_VECSMALL);
    3186    14983776 :   if (lim > 2)
    3187             :   {
    3188    14983776 :     long v = vals(n), oldi;
    3189    14983813 :     if (v)
    3190             :     {
    3191     4654247 :       P[1] = 2; E[1] = v; i = 2;
    3192     4654247 :       n >>= v; if (n == 1) goto END;
    3193             :     }
    3194    13884870 :     u_forprime_init(&S, 3, lim);
    3195    13884896 :     oldi = i;
    3196   121828460 :     while ( (p = u_forprime_next_fast(&S)) )
    3197             :     {
    3198             :       int stop;
    3199             :       /* tiny integers without small factors are often primes */
    3200   107942426 :       if (p == 673)
    3201             :       {
    3202       11241 :         oldi = i;
    3203    13895043 :         if (uisprime_661(n)) { P[i] = n; E[i] = 1; i++; goto END; }
    3204             :       }
    3205   107937641 :       v = u_lvalrem_stop(&n, p, &stop);
    3206   107937685 :       if (v) {
    3207    10077706 :         P[i] = p;
    3208    10077706 :         E[i] = v; i++;
    3209             :       }
    3210   107937685 :       if (stop) {
    3211    13879017 :         if (n != 1) { P[i] = n; E[i] = 1; i++; }
    3212    13879017 :         goto END;
    3213             :       }
    3214             :     }
    3215        1094 :     if (oldi != i && uisprime_661(n)) { P[i] = n; E[i] = 1; i++; goto END; }
    3216             :   }
    3217         884 :   if (all)
    3218             :   { /* smallfact: look for easy pure powers then stop */
    3219             : #ifdef LONG_IS_64BIT
    3220           6 :     ulong mask = all > 563 ? (all > 7129 ? 1: 3): 7;
    3221             : #else
    3222           0 :     ulong mask = all > 22 ? (all > 83 ? 1: 3): 7;
    3223             : #endif
    3224           6 :     long k = 1, ex;
    3225           6 :     while (uissquareall(n, &n)) k <<= 1;
    3226           6 :     while ( (ex = uis_357_power(n, &n, &mask)) ) k *= ex;
    3227           6 :     P[i] = n; E[i] = k; i++; goto END;
    3228             :   }
    3229             :   {
    3230             :     GEN perm;
    3231         878 :     ifac_factoru(utoipos(n), hint, P, E, &i);
    3232         878 :     setlg(P, i);
    3233         878 :     perm = vecsmall_indexsort(P);
    3234         878 :     P = vecsmallpermute(P, perm);
    3235         878 :     E = vecsmallpermute(E, perm);
    3236             :   }
    3237             : END:
    3238    14983839 :   avma = av;
    3239    14983839 :   P2 = cgetg(i, t_VECSMALL); gel(f,1) = P2;
    3240    14983806 :   E2 = cgetg(i, t_VECSMALL); gel(f,2) = E2;
    3241    14983810 :   while (--i >= 1) { P2[i] = P[i]; E2[i] = E[i]; }
    3242    14983810 :   return f;
    3243             : }
    3244             : GEN
    3245     4553072 : factoru(ulong n)
    3246     4553072 : { return factoru_sign(n, 0, decomp_default_hint); }
    3247             : 
    3248             : long
    3249           0 : moebiusu_fact(GEN f)
    3250             : {
    3251           0 :   GEN E = gel(f,2);
    3252           0 :   long i, l = lg(E);
    3253           0 :   for (i = 1; i < l; i++)
    3254           0 :     if (E[i] > 1) return 0;
    3255           0 :   return odd(l)? 1: -1;
    3256             : }
    3257             : 
    3258             : long
    3259      171130 : moebiusu(ulong n)
    3260             : {
    3261             :   pari_sp av;
    3262             :   ulong p;
    3263             :   long s, v, test_prime;
    3264             :   forprime_t S;
    3265             : 
    3266      171130 :   switch(n)
    3267             :   {
    3268           0 :     case 0: (void)check_arith_non0(gen_0,"moebius");/*error*/
    3269       14455 :     case 1: return  1;
    3270        6621 :     case 2: return -1;
    3271             :   }
    3272      150062 :   v = vals(n);
    3273      150405 :   if (v == 0)
    3274       87305 :     s = 1;
    3275             :   else
    3276             :   {
    3277       63100 :     if (v > 1) return 0;
    3278       22841 :     n >>= 1;
    3279       22841 :     s = -1;
    3280             :   }
    3281      110146 :   av = avma;
    3282      110146 :   u_forprime_init(&S, 3, utridiv_bound(n));
    3283      109831 :   test_prime = 0;
    3284     7558042 :   while ((p = u_forprime_next_fast(&S)))
    3285             :   {
    3286             :     int stop;
    3287             :     /* tiny integers without small factors are often primes */
    3288     7445640 :     if (p == 673)
    3289             :     {
    3290        3734 :       test_prime = 0;
    3291      112268 :       if (uisprime_661(n)) { avma = av; return -s; }
    3292             :     }
    3293     7444239 :     v = u_lvalrem_stop(&n, p, &stop);
    3294     7445513 :     if (v) {
    3295       80505 :       if (v > 1) { avma = av; return 0; }
    3296       64296 :       test_prime = 1;
    3297       64296 :       s = -s;
    3298             :     }
    3299     7429304 :     if (stop) { avma = av; return n == 1? s: -s; }
    3300             :   }
    3301        1518 :   avma = av;
    3302        1518 :   if (test_prime && uisprime_661(n)) return -s;
    3303             :   else
    3304             :   {
    3305        1068 :     long t = ifac_moebiusu(utoipos(n));
    3306        1068 :     avma = av;
    3307        1068 :     if (t == 0) return 0;
    3308        1068 :     return (s == t)? 1: -1;
    3309             :   }
    3310             : }
    3311             : 
    3312             : long
    3313       61775 : moebius(GEN n)
    3314             : {
    3315       61775 :   pari_sp av = avma;
    3316             :   GEN F;
    3317             :   ulong p;
    3318             :   long i, l, s, v;
    3319             :   forprime_t S;
    3320             : 
    3321       61775 :   if ((F = check_arith_non0(n,"moebius")))
    3322             :   {
    3323             :     GEN E;
    3324         728 :     F = clean_Z_factor(F);
    3325         728 :     E = gel(F,2);
    3326         728 :     l = lg(E);
    3327        1428 :     for(i = 1; i < l; i++)
    3328         980 :       if (!equali1(gel(E,i))) { avma = av; return 0; }
    3329         448 :     avma = av; return odd(l)? 1: -1;
    3330             :   }
    3331       61214 :   if (lgefint(n) == 3) return moebiusu(uel(n,2));
    3332         791 :   p = mod4(n); if (!p) return 0;
    3333         791 :   if (p == 2) { s = -1; n = shifti(n, -1); } else { s = 1; n = icopy(n); }
    3334         791 :   setabssign(n);
    3335             : 
    3336         791 :   u_forprime_init(&S, 3, tridiv_bound(n));
    3337         791 :   while ((p = u_forprime_next_fast(&S)))
    3338             :   {
    3339             :     int stop;
    3340     2234565 :     v = Z_lvalrem_stop(&n, p, &stop);
    3341     2234565 :     if (v)
    3342             :     {
    3343        1678 :       if (v > 1) { avma = av; return 0; }
    3344        1307 :       s = -s;
    3345        1307 :       if (stop) { avma = av; return is_pm1(n)? s: -s; }
    3346             :     }
    3347             :   }
    3348         563 :   l = lg(primetab);
    3349         573 :   for (i = 1; i < l; i++)
    3350             :   {
    3351          25 :     v = Z_pvalrem(n, gel(primetab,i), &n);
    3352          25 :     if (v)
    3353             :     {
    3354          25 :       if (v > 1) { avma = av; return 0; }
    3355          11 :       s = -s;
    3356          11 :       if (is_pm1(n)) { avma = av; return s; }
    3357             :     }
    3358             :   }
    3359         548 :   if (ifac_isprime(n)) { avma = av; return -s; }
    3360             :   /* large composite without small factors */
    3361         196 :   v = ifac_moebius(n);
    3362         196 :   avma = av; return (s<0 ? -v : v); /* correct also if v==0 */
    3363             : }
    3364             : 
    3365             : long
    3366        1708 : ispowerful(GEN n)
    3367             : {
    3368        1708 :   pari_sp av = avma;
    3369             :   GEN F;
    3370             :   ulong p, bound;
    3371             :   long i, l, v;
    3372             :   forprime_t S;
    3373             : 
    3374        1708 :   if ((F = check_arith_all(n, "ispowerful")))
    3375             :   {
    3376         742 :     GEN p, P = gel(F,1), E = gel(F,2);
    3377         742 :     if (lg(P) == 1) return 1; /* 1 */
    3378         728 :     p = gel(P,1);
    3379         728 :     if (!signe(p)) return 1; /* 0 */
    3380         707 :     i = is_pm1(p)? 2: 1; /* skip -1 */
    3381         707 :     l = lg(E);
    3382         980 :     for (; i < l; i++)
    3383         847 :       if (equali1(gel(E,i))) return 0;
    3384         133 :     return 1;
    3385             :   }
    3386         966 :   if (!signe(n)) return 1;
    3387             : 
    3388         952 :   if (mod4(n) == 2) return 0;
    3389         623 :   n = shifti(n, -vali(n));
    3390         623 :   if (is_pm1(n)) return 1;
    3391         546 :   setabssign(n);
    3392         546 :   bound = tridiv_bound(n);
    3393         546 :   u_forprime_init(&S, 3, bound);
    3394         546 :   while ((p = u_forprime_next_fast(&S)))
    3395             :   {
    3396             :     int stop;
    3397      307790 :     v = Z_lvalrem_stop(&n, p, &stop);
    3398      307790 :     if (v)
    3399             :     {
    3400        1113 :       if (v == 1) { avma = av; return 0; }
    3401         203 :       if (stop) { avma = av; return is_pm1(n); }
    3402             :     }
    3403             :   }
    3404           7 :   l = lg(primetab);
    3405           7 :   for (i = 1; i < l; i++)
    3406             :   {
    3407           0 :     v = Z_pvalrem(n, gel(primetab,i), &n);
    3408           0 :     if (v)
    3409             :     {
    3410           0 :       if (v == 1) { avma = av; return 0; }
    3411           0 :       if (is_pm1(n)) { avma = av; return 1; }
    3412             :     }
    3413             :   }
    3414             :   /* no need to factor: must be p^2 or not powerful */
    3415           7 :   if(cmpii(powuu(bound+1, 3), n) > 0) {
    3416           0 :     long res = Z_issquare(n);
    3417           0 :     avma = av; return res;
    3418             :   }
    3419             : 
    3420           7 :   if (ifac_isprime(n)) { avma=av; return 0; }
    3421             :   /* large composite without small factors */
    3422           7 :   v = ifac_ispowerful(n);
    3423           7 :   avma = av; return v;
    3424             : }
    3425             : 
    3426             : ulong
    3427        5219 : coreu_fact(GEN f)
    3428             : {
    3429        5219 :   GEN P = gel(f,1), E = gel(f,2);
    3430        5219 :   long i, l = lg(P), m = 1;
    3431       13855 :   for (i = 1; i < l; i++)
    3432             :   {
    3433        8636 :     ulong p = P[i], e = E[i];
    3434        8636 :     if (e & 1) m *= p;
    3435             :   }
    3436        5219 :   return m;
    3437             : }
    3438             : ulong
    3439        5219 : coreu(ulong n)
    3440             : {
    3441        5219 :   if (n == 0) return 0;
    3442             :   else
    3443             :   {
    3444        5219 :     pari_sp av = avma;
    3445        5219 :     long m = coreu_fact(factoru(n));
    3446        5219 :     avma = av; return m;
    3447             :   }
    3448             : }
    3449             : GEN
    3450        3185 : core(GEN n)
    3451             : {
    3452        3185 :   pari_sp av = avma;
    3453             :   GEN m, F;
    3454             :   ulong p;
    3455             :   long i, l, v;
    3456             :   forprime_t S;
    3457             : 
    3458        3185 :   if ((F = check_arith_all(n, "core")))
    3459             :   {
    3460        1491 :     GEN p, x, P = gel(F,1), E = gel(F,2);
    3461        1491 :     long j = 1;
    3462        1491 :     if (lg(P) == 1) return gen_1;
    3463        1463 :     p = gel(P,1);
    3464        1463 :     if (!signe(p)) return gen_0;
    3465        1421 :     l = lg(P); x = cgetg(l, t_VEC);
    3466        4382 :     for (i = 1; i < l; i++)
    3467        2961 :       if (mpodd(gel(E,i))) gel(x,j++) = gel(P,i);
    3468        1421 :     setlg(x, j); return ZV_prod(x);
    3469             :   }
    3470        1694 :   switch(lgefint(n))
    3471             :   {
    3472          28 :     case 2: return gen_0;
    3473             :     case 3:
    3474        1613 :       p = coreu(uel(n,2));
    3475        1613 :       return signe(n) > 0? utoipos(p): utoineg(p);
    3476             :   }
    3477             : 
    3478          53 :   m = signe(n) < 0? gen_m1: gen_1;
    3479          53 :   n = absi(n);
    3480          53 :   u_forprime_init(&S, 2, tridiv_bound(n));
    3481      320872 :   while ((p = u_forprime_next_fast(&S)))
    3482             :   {
    3483             :     int stop;
    3484      320769 :     v = Z_lvalrem_stop(&n, p, &stop);
    3485      320769 :     if (v)
    3486             :     {
    3487         199 :       if (v & 1) m = muliu(m, p);
    3488         199 :       if (stop)
    3489             :       {
    3490           3 :         if (!is_pm1(n)) m = mulii(m, n);
    3491           3 :         return gerepileuptoint(av, m);
    3492             :       }
    3493             :     }
    3494             :   }
    3495          50 :   l = lg(primetab);
    3496          82 :   for (i = 1; i < l; i++)
    3497             :   {
    3498          40 :     GEN q = gel(primetab,i);
    3499          40 :     v = Z_pvalrem(n, q, &n);
    3500          40 :     if (v)
    3501             :     {
    3502          32 :       if (v & 1) m = mulii(m, q);
    3503          32 :       if (is_pm1(n)) return gerepileuptoint(av, m);
    3504             :     }
    3505             :   }
    3506          42 :   if (ifac_isprime(n)) { m = mulii(m, n); return gerepileuptoint(av, m); }
    3507             :   /* large composite without small factors */
    3508          33 :   return gerepileuptoint(av, mulii(m, ifac_core(n)));
    3509             : }
    3510             : 
    3511             : long
    3512           0 : Z_issmooth(GEN m, ulong lim)
    3513             : {
    3514           0 :   pari_sp av=avma;
    3515           0 :   ulong p = 2;
    3516             :   forprime_t S;
    3517           0 :   u_forprime_init(&S, 2, lim);
    3518           0 :   while ((p = u_forprime_next_fast(&S)))
    3519             :   {
    3520             :     int stop;
    3521           0 :     (void)Z_lvalrem_stop(&m, p, &stop);
    3522           0 :     if (stop) { avma = av; return abscmpiu(m,lim)<=0; }
    3523             :   }
    3524           0 :   avma = av; return 0;
    3525             : }
    3526             : 
    3527             : GEN
    3528      193217 : Z_issmooth_fact(GEN m, ulong lim)
    3529             : {
    3530      193217 :   pari_sp av=avma;
    3531             :   GEN F, P, E;
    3532             :   ulong p;
    3533      193217 :   long i = 1, l = expi(m)+1;
    3534             :   forprime_t S;
    3535      193336 :   P = cgetg(l, t_VECSMALL);
    3536      193319 :   E = cgetg(l, t_VECSMALL);
    3537      193307 :   F = mkmat2(P,E);
    3538      193324 :   u_forprime_init(&S, 2, lim);
    3539    54212100 :   while ((p = u_forprime_next_fast(&S)))
    3540             :   {
    3541             :     long v;
    3542             :     int stop;
    3543    53718138 :     if ((v = Z_lvalrem_stop(&m, p, &stop)))
    3544             :     {
    3545      741374 :       P[i] = p;
    3546      741374 :       E[i] = v; i++;
    3547      741374 :       if (stop)
    3548             :       {
    3549      128831 :         if (abscmpiu(m,lim) > 0) break;
    3550      109771 :         P[i] = m[2];
    3551      109771 :         E[i] = 1; i++;
    3552      109771 :         setlg(P, i);
    3553      109754 :         setlg(E, i); avma = (pari_sp)F; return F;
    3554             :       }
    3555             :     }
    3556             :   }
    3557       83611 :   avma = av; return NULL;
    3558             : }
    3559             : 
    3560             : /***********************************************************************/
    3561             : /**                                                                   **/
    3562             : /**       COMPUTING THE MATRIX OF PRIME DIVISORS AND EXPONENTS        **/
    3563             : /**                                                                   **/
    3564             : /***********************************************************************/
    3565             : static GEN
    3566       31965 : aux_end(GEN M, GEN n, long nb)
    3567             : {
    3568       31965 :   GEN P,E, z = (GEN)avma;
    3569             :   long i;
    3570             : 
    3571       31965 :   if (n) gunclone(n);
    3572       31965 :   P = cgetg(nb+1,t_COL);
    3573       31965 :   E = cgetg(nb+1,t_COL);
    3574      188067 :   for (i=nb; i; i--)
    3575             :   { /* allow a stackdummy in the middle */
    3576      156102 :     while (typ(z) != t_INT) z += lg(z);
    3577      156102 :     gel(E,i) = z; z += lg(z);
    3578      156102 :     gel(P,i) = z; z += lg(z);
    3579             :   }
    3580       31965 :   gel(M,1) = P;
    3581       31965 :   gel(M,2) = E;
    3582       31965 :   return sort_factor(M, (void*)&abscmpii, cmp_nodata);
    3583             : }
    3584             : 
    3585             : static void
    3586      153673 : STORE(long *nb, GEN x, long e) { (*nb)++; (void)x; (void)utoipos(e); }
    3587             : static void
    3588      136821 : STOREu(long *nb, ulong x, long e) { STORE(nb, utoipos(x), e); }
    3589             : static void
    3590       16831 : STOREi(long *nb, GEN x, long e) { STORE(nb, icopy(x), e); }
    3591             : /* no prime less than p divides n */
    3592             : static int
    3593       10223 : special_primes(GEN n, ulong p, long *nb, GEN T)
    3594             : {
    3595       10223 :   long i, l = lg(T);
    3596       10223 :   if (l > 1)
    3597             :   { /* pp = square of biggest p tried so far */
    3598         184 :     long pp[] = { evaltyp(t_INT)|_evallg(4), 0,0,0 };
    3599         184 :     pari_sp av = avma; affii(sqru(p), pp); avma = av;
    3600             : 
    3601         265 :     for (i = 1; i < l; i++)
    3602         200 :       if (dvdiiz(n,gel(T,i), n))
    3603             :       {
    3604         168 :         long k = 1; while (dvdiiz(n,gel(T,i), n)) k++;
    3605         168 :         STOREi(nb, gel(T,i), k);
    3606         168 :         if (abscmpii(pp, n) > 0) return 1;
    3607             :       }
    3608             :   }
    3609       10104 :   return 0;
    3610             : }
    3611             : 
    3612             : /* factor(sn*|n|), where sn = -1,1 or 0.
    3613             :  * all != 0 : only look for prime divisors < all */
    3614             : static GEN
    3615    10854169 : ifactor_sign(GEN n, ulong all, long hint, long sn)
    3616             : {
    3617             :   GEN M, N;
    3618             :   pari_sp av;
    3619    10854169 :   long nb = 0, i;
    3620             :   ulong lim;
    3621             :   forprime_t T;
    3622             : 
    3623    10854169 :   if (!sn) retmkmat2(mkcol(gen_0), mkcol(gen_1));
    3624    10854141 :   if (lgefint(n) == 3)
    3625             :   { /* small integer */
    3626    10822176 :     GEN f, Pf, Ef, P, E, F = cgetg(3, t_MAT);
    3627             :     long l;
    3628    10822181 :     av = avma;
    3629             :     /* enough room to store <= 15 primes and exponents (OK if n < 2^64) */
    3630    10822181 :     (void)new_chunk((15*3 + 15 + 1) * 2);
    3631    10822181 :     f = factoru_sign(uel(n,2), all, hint);
    3632    10822174 :     avma = av;
    3633    10822174 :     Pf = gel(f,1);
    3634    10822174 :     Ef = gel(f,2);
    3635    10822174 :     l = lg(Pf);
    3636    10822174 :     if (sn < 0)
    3637             :     { /* add sign */
    3638         385 :       long L = l+1;
    3639         385 :       gel(F,1) = P = cgetg(L, t_COL);
    3640         385 :       gel(F,2) = E = cgetg(L, t_COL);
    3641         383 :       gel(P,1) = gen_m1; P++;
    3642         383 :       gel(E,1) = gen_1;  E++;
    3643             :     }
    3644             :     else
    3645             :     {
    3646    10821789 :       gel(F,1) = P = cgetg(l, t_COL);
    3647    10821794 :       gel(F,2) = E = cgetg(l, t_COL);
    3648             :     }
    3649    29041208 :     for (i = 1; i < l; i++)
    3650             :     {
    3651    18219026 :       gel(P,i) = utoipos(Pf[i]);
    3652    18219027 :       gel(E,i) = utoipos(Ef[i]);
    3653             :     }
    3654    10822182 :     return F;
    3655             :   }
    3656       31965 :   M = cgetg(3,t_MAT);
    3657       31965 :   if (sn < 0) STORE(&nb, utoineg(1), 1);
    3658       31965 :   if (is_pm1(n)) return aux_end(M,NULL,nb);
    3659             : 
    3660       31965 :   n = N = gclone(n); setabssign(n);
    3661             :   /* trial division bound */
    3662       31965 :   lim = all; if (!lim) lim = tridiv_bound(n);
    3663       31965 :   if (lim > 2)
    3664             :   {
    3665             :     ulong maxp, p;
    3666             :     pari_sp av2;
    3667       31951 :     i = vali(n);
    3668       31951 :     if (i)
    3669             :     {
    3670       23323 :       STOREu(&nb, 2, i);
    3671       23323 :       av = avma; affii(shifti(n,-i), n); avma = av;
    3672             :     }
    3673       31951 :     if (is_pm1(n)) return aux_end(M,n,nb);
    3674             :     /* trial division */
    3675       31840 :     maxp = maxprime();
    3676       31840 :     av = avma; u_forprime_init(&T, 3, minss(lim, maxp)); av2 = avma;
    3677             :     /* first pass: known to fit in private prime table */
    3678   261059173 :     while ((p = u_forprime_next_fast(&T)))
    3679             :     {
    3680   261017303 :       pari_sp av3 = avma;
    3681             :       int stop;
    3682   261017303 :       long k = Z_lvalrem_stop(&n, p, &stop);
    3683   261017161 :       if (k)
    3684             :       {
    3685      113449 :         affii(n, N); n = N; avma = av3;
    3686      113449 :         STOREu(&nb, p, k);
    3687             :       }
    3688   261017119 :       if (stop)
    3689             :       {
    3690       21624 :         if (!is_pm1(n)) STOREi(&nb, n, 1);
    3691       21624 :         stackdummy(av, av2);
    3692       21624 :         return aux_end(M,n,nb);
    3693             :       }
    3694             :     }
    3695       10216 :     stackdummy(av, av2);
    3696       10216 :     if (lim > maxp)
    3697             :     { /* second pass, usually empty: outside private prime table */
    3698         900 :       av = avma; u_forprime_init(&T, maxp+1, lim); av2 = avma;
    3699       28099 :       while ((p = u_forprime_next(&T)))
    3700             :       {
    3701       26306 :         pari_sp av3 = avma;
    3702             :         int stop;
    3703       26306 :         long k = Z_lvalrem_stop(&n, p, &stop);
    3704       26306 :         if (k)
    3705             :         {
    3706          49 :           affii(n, N); n = N; avma = av3;
    3707          49 :           STOREu(&nb, p, k);
    3708             :         }
    3709       26306 :         if (stop)
    3710             :         {
    3711           7 :           if (!is_pm1(n)) STOREi(&nb, n, 1);
    3712           7 :           stackdummy(av, av2);
    3713           7 :           return aux_end(M,n,nb);
    3714             :         }
    3715             :       }
    3716         893 :       stackdummy(av, av2);
    3717             :     }
    3718             :   }
    3719             :   /* trial divide by the special primes */
    3720       10223 :   if (special_primes(n, lim, &nb, primetab))
    3721             :   {
    3722         119 :     if (!is_pm1(n)) STOREi(&nb, n, 1);
    3723         119 :     return aux_end(M,n,nb);
    3724             :   }
    3725             : 
    3726       10104 :   if (all)
    3727             :   { /* smallfact: look for easy pure powers then stop. Cf Z_isanypower */
    3728             :     GEN x;
    3729             :     long k;
    3730        7836 :     av = avma;
    3731        7836 :     k = isanypower_nosmalldiv(n, &x);
    3732        7836 :     if (k > 1) affii(x, n);
    3733        7836 :     avma = av; STOREi(&nb, n, k);
    3734        7836 :     if (DEBUGLEVEL >= 2) {
    3735           0 :       pari_warn(warner,
    3736             :         "IFAC: untested %ld-bit integer declared prime", expi(n));
    3737           0 :       if (expi(n) <= 256)
    3738           0 :         err_printf("\t%Ps\n", n);
    3739             :     }
    3740        7836 :     return aux_end(M,n,nb);
    3741             :   }
    3742        2268 :   if (ifac_isprime(n)) { STOREi(&nb, n, 1); return aux_end(M,n,nb); }
    3743        1222 :   nb += ifac_decomp(n, hint);
    3744        1222 :   return aux_end(M,n, nb);
    3745             : }
    3746             : 
    3747             : static GEN
    3748     6452577 : ifactor(GEN n, ulong all, long hint)
    3749     6452577 : { return ifactor_sign(n, all, hint, signe(n)); }
    3750             : 
    3751             : int
    3752        6640 : ifac_next(GEN *part, GEN *p, long *e)
    3753             : {
    3754        6640 :   GEN here = ifac_main(part);
    3755        6640 :   if (here == gen_0) { *p = NULL; *e = 1; return 0; }
    3756        6626 :   if (!here) { *p = NULL; *e = 0; return 0; }
    3757        4414 :   *p = VALUE(here);
    3758        4414 :   *e = EXPON(here)[2];
    3759        4414 :   ifac_delete(here); return 1;
    3760             : }
    3761             : 
    3762             : /* see before ifac_crack for current semantics of 'hint' (factorint's 'flag') */
    3763             : GEN
    3764          14 : factorint(GEN n, long flag)
    3765             : {
    3766             :   GEN F;
    3767          14 :   if ((F = check_arith_all(n,"factorint"))) return gcopy(F);
    3768          14 :   return ifactor(n,0,flag);
    3769             : }
    3770             : 
    3771             : GEN
    3772        9363 : Z_factor_limit(GEN n, ulong all)
    3773             : {
    3774        9363 :   if (!all) all = GP_DATA->primelimit + 1;
    3775        9363 :   return ifactor(n,all,decomp_default_hint);
    3776             : }
    3777             : GEN
    3778       24255 : absZ_factor_limit(GEN n, ulong all)
    3779             : {
    3780       24255 :   if (!all) all = GP_DATA->primelimit + 1;
    3781       24255 :   return ifactor_sign(n,all,decomp_default_hint, signe(n)?1 : 0);
    3782             : }
    3783             : GEN
    3784     6443170 : Z_factor(GEN n)
    3785     6443170 : { return ifactor(n,0,decomp_default_hint); }
    3786             : GEN
    3787     4377340 : absZ_factor(GEN n)
    3788     4377340 : { return ifactor_sign(n, 0, decomp_default_hint, signe(n)? 1: 0); }
    3789             : 
    3790             : /* Factor until the unfactored part is smaller than limit. Return the
    3791             :  * factored part. Hence factorback(output) may be smaller than n */
    3792             : GEN
    3793          28 : Z_factor_until(GEN n, GEN limit)
    3794             : {
    3795          28 :   pari_sp av2, av = avma;
    3796          28 :   ulong B = tridiv_bound(n);
    3797          28 :   GEN q, part, F = ifactor(n, B, decomp_default_hint);
    3798          28 :   GEN P = gel(F,1), E = gel(F,2);
    3799          28 :   long l = lg(P);
    3800             : 
    3801          28 :   av2 = avma;
    3802          28 :   q = gel(P,l-1);
    3803          28 :   if (abscmpiu(q, B) <= 0 || cmpii(q, sqru(B)) < 0 || ifac_isprime(q))
    3804             :   {
    3805          14 :     avma = av2; return F;
    3806             :   }
    3807             :   /* q = composite unfactored part, remove from P/E */
    3808          14 :   setlg(E,l-1);
    3809          14 :   setlg(P,l-1);
    3810          14 :   if (cmpii(q, limit) > 0)
    3811             :   { /* factor further */
    3812          14 :     long l2 = expi(q)+1;
    3813          14 :     GEN P2 = vectrunc_init(l2);
    3814          14 :     GEN E2 = vectrunc_init(l2);
    3815          14 :     GEN F2 = mkmat2(P2,E2);
    3816          14 :     part = ifac_start(q, 0);
    3817             :     for(;;)
    3818             :     {
    3819             :       long e;
    3820             :       GEN p;
    3821          28 :       if (!ifac_next(&part,&p,&e)) break;
    3822          14 :       vectrunc_append(P2, p);
    3823          14 :       vectrunc_append(E2, utoipos(e));
    3824          14 :       q = diviiexact(q, powiu(p, e));
    3825          14 :       if (cmpii(q, limit) <= 0) break;
    3826           0 :     }
    3827          14 :     F2 = sort_factor(F2, (void*)&abscmpii, cmp_nodata);
    3828          14 :     F = merge_factor(F, F2, (void*)&abscmpii, cmp_nodata);
    3829             :   }
    3830          14 :   return gerepilecopy(av, F);
    3831             : }
    3832             : 
    3833             : static void
    3834   135654481 : matsmalltrunc_append(GEN m, ulong p, ulong e)
    3835             : {
    3836   135654481 :   GEN P = gel(m,1), E = gel(m,2);
    3837   135654481 :   long l = lg(P);
    3838   135654481 :   P[l] = p; lg_increase(P);
    3839   135654481 :   E[l] = e; lg_increase(E);
    3840   135654481 : }
    3841             : static GEN
    3842    48379113 : matsmalltrunc_init(long l)
    3843             : {
    3844    48379113 :   GEN P = vecsmalltrunc_init(l);
    3845    48379113 :   GEN E = vecsmalltrunc_init(l); return mkvec2(P,E);
    3846             : }
    3847             : 
    3848             : GEN
    3849       45666 : vecfactoru_i(ulong a, ulong b)
    3850             : {
    3851       45666 :   ulong N, k, p, n = b-a+1;
    3852       45666 :   GEN v = const_vecsmall(n, 1);
    3853       45666 :   GEN L = cgetg(n+1, t_VEC);
    3854             :   forprime_t T;
    3855       45666 :   if (b < 510510UL) N = 7;
    3856        8853 :   else if (b < 9699690UL) N = 8;
    3857             : #ifdef LONG_IS_64BIT
    3858           0 :   else if (b < 223092870UL) N = 9;
    3859           0 :   else if (b < 6469693230UL) N = 10;
    3860           0 :   else if (b < 200560490130UL) N = 11;
    3861           0 :   else if (b < 7420738134810UL) N = 12;
    3862           0 :   else if (b < 304250263527210UL) N = 13;
    3863           0 :   else N = 16; /* don't bother */
    3864             : #else
    3865           0 :   else N = 9;
    3866             : #endif
    3867       45666 :   for (k = 1; k <= n; k++) gel(L,k) = matsmalltrunc_init(N);
    3868       45666 :   u_forprime_init(&T, 2, usqrt(b));
    3869     4533949 :   while ((p = u_forprime_next(&T)))
    3870             :   { /* p <= sqrt(b) */
    3871     4442617 :     ulong pk = p, K = ulogint(b, p);
    3872    15698755 :     for (k = 1; k <= K; k++)
    3873             :     {
    3874             :       ulong ap, j, t;
    3875    11256138 :       t = a / pk;
    3876    11256138 :       ap = t * pk;
    3877    11256138 :       if (ap < a) { ap += pk; t++; }
    3878             :       /* t = (j+a-1) \ pk */
    3879   149114526 :       for (j = ap-a+1; j <= n; j += pk, t++)
    3880   137858388 :         if (t % p) { v[j] *= pk; matsmalltrunc_append(gel(L,j), p,k); }
    3881    11256138 :       pk *= p;
    3882             :     }
    3883             :   }
    3884             :   /* complete factorisation of non-sqrt(b)-smooth numbers */
    3885    48424779 :   for (k = 1, N = a; k <= n; k++, N++)
    3886    48379113 :     if (uel(v,k) != N) matsmalltrunc_append(gel(L,k), N/uel(v,k),1UL);
    3887       45666 :   return L;
    3888             : }
    3889             : GEN
    3890       13776 : vecfactoru(ulong a, ulong b)
    3891             : {
    3892       13776 :   pari_sp av = avma;
    3893       13776 :   return gerepilecopy(av, vecfactoru_i(a,b));
    3894             : }

Generated by: LCOV version 1.11