Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13