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 - arith1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.0 lcov report (development 29732-95c6201d93) Lines: 2072 2272 91.2 %
Date: 2024-11-21 09:08:54 Functions: 216 230 93.9 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /*********************************************************************/
      16             : /**                     ARITHMETIC FUNCTIONS                        **/
      17             : /**                         (first part)                            **/
      18             : /*********************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : #define DEBUGLEVEL DEBUGLEVEL_arith
      23             : 
      24             : /******************************************************************/
      25             : /*                 GENERATOR of (Z/mZ)*                           */
      26             : /******************************************************************/
      27             : static GEN
      28        1812 : remove2(GEN q) { long v = vali(q); return v? shifti(q, -v): q; }
      29             : static ulong
      30      463762 : u_remove2(ulong q) { return q >> vals(q); }
      31             : GEN
      32        1812 : odd_prime_divisors(GEN q) { return gel(Z_factor(remove2(q)), 1); }
      33             : static GEN
      34      463761 : u_odd_prime_divisors(ulong q) { return gel(factoru(u_remove2(q)), 1); }
      35             : /* p odd prime, q=(p-1)/2; L0 list of (some) divisors of q = (p-1)/2 or NULL
      36             :  * (all prime divisors of q); return the q/l, l in L0 */
      37             : static GEN
      38        4909 : is_gener_expo(GEN p, GEN L0)
      39             : {
      40        4909 :   GEN L, q = shifti(p,-1);
      41             :   long i, l;
      42        4909 :   if (L0) {
      43        3134 :     l = lg(L0);
      44        3134 :     L = cgetg(l, t_VEC);
      45             :   } else {
      46        1775 :     L0 = L = odd_prime_divisors(q);
      47        1775 :     l = lg(L);
      48             :   }
      49       14199 :   for (i=1; i<l; i++) gel(L,i) = diviiexact(q, gel(L0,i));
      50        4909 :   return L;
      51             : }
      52             : static GEN
      53      531122 : u_is_gener_expo(ulong p, GEN L0)
      54             : {
      55      531122 :   const ulong q = p >> 1;
      56             :   long i;
      57             :   GEN L;
      58      531122 :   if (!L0) L0 = u_odd_prime_divisors(q);
      59      531126 :   L = cgetg_copy(L0,&i);
      60     1149619 :   while (--i) L[i] = q / uel(L0,i);
      61      531125 :   return L;
      62             : }
      63             : 
      64             : int
      65     1627969 : is_gener_Fl(ulong x, ulong p, ulong p_1, GEN L)
      66             : {
      67             :   long i;
      68     1627969 :   if (krouu(x, p) >= 0) return 0;
      69     1374594 :   for (i=lg(L)-1; i; i--)
      70             :   {
      71      838727 :     ulong t = Fl_powu(x, uel(L,i), p);
      72      838724 :     if (t == p_1 || t == 1) return 0;
      73             :   }
      74      535867 :   return 1;
      75             : }
      76             : /* assume p prime */
      77             : ulong
      78     1050444 : pgener_Fl_local(ulong p, GEN L0)
      79             : {
      80     1050444 :   const pari_sp av = avma;
      81     1050444 :   const ulong p_1 = p-1;
      82             :   long x;
      83             :   GEN L;
      84     1050444 :   if (p <= 19) switch(p)
      85             :   { /* quick trivial cases */
      86          56 :     case 2:  return 1;
      87      116121 :     case 7:
      88      116121 :     case 17: return 3;
      89      403195 :     default: return 2;
      90             :   }
      91      531072 :   L = u_is_gener_expo(p,L0);
      92     1620202 :   for (x = 2;; x++)
      93     1620202 :     if (is_gener_Fl(x,p,p_1,L)) return gc_ulong(av, x);
      94             : }
      95             : ulong
      96      574514 : pgener_Fl(ulong p) { return pgener_Fl_local(p, NULL); }
      97             : 
      98             : /* L[i] = set of (p-1)/2l, l ODD prime divisor of p-1 (l=2 can be included,
      99             :  * but wasteful) */
     100             : int
     101       13953 : is_gener_Fp(GEN x, GEN p, GEN p_1, GEN L)
     102             : {
     103       13953 :   long i, t = lgefint(x)==3? kroui(x[2], p): kronecker(x, p);
     104       13953 :   if (t >= 0) return 0;
     105       22013 :   for (i = lg(L)-1; i; i--)
     106             :   {
     107       14482 :     GEN t = Fp_pow(x, gel(L,i), p);
     108       14482 :     if (equalii(t, p_1) || equali1(t)) return 0;
     109             :   }
     110        7531 :   return 1;
     111             : }
     112             : 
     113             : /* assume p prime, return a generator of all L[i]-Sylows in F_p^*. */
     114             : GEN
     115      358116 : pgener_Fp_local(GEN p, GEN L0)
     116             : {
     117      358116 :   pari_sp av0 = avma;
     118             :   GEN x, p_1, L;
     119      358116 :   if (lgefint(p) == 3)
     120             :   {
     121             :     ulong z;
     122      353212 :     if (p[2] == 2) return gen_1;
     123      258225 :     if (L0) L0 = ZV_to_nv(L0);
     124      258223 :     z = pgener_Fl_local(uel(p,2), L0);
     125      258268 :     return gc_utoipos(av0, z);
     126             :   }
     127        4904 :   p_1 = subiu(p,1); L = is_gener_expo(p, L0);
     128        4904 :   x = utoipos(2);
     129        9927 :   for (;; x[2]++) { if (is_gener_Fp(x, p, p_1, L)) break; }
     130        4904 :   return gc_utoipos(av0, uel(x,2));
     131             : }
     132             : 
     133             : GEN
     134       44226 : pgener_Fp(GEN p) { return pgener_Fp_local(p, NULL); }
     135             : 
     136             : ulong
     137      205339 : pgener_Zl(ulong p)
     138             : {
     139      205339 :   if (p == 2) pari_err_DOMAIN("pgener_Zl","p","=",gen_2,gen_2);
     140             :   /* only p < 2^32 such that znprimroot(p) != znprimroot(p^2) */
     141      205339 :   if (p == 40487) return 10;
     142             : #ifndef LONG_IS_64BIT
     143       29755 :   return pgener_Fl(p);
     144             : #else
     145      175584 :   if (p < (1UL<<32)) return pgener_Fl(p);
     146             :   else
     147             :   {
     148          30 :     const pari_sp av = avma;
     149          30 :     const ulong p_1 = p-1;
     150             :     long x ;
     151          30 :     GEN p2 = sqru(p), L = u_is_gener_expo(p, NULL);
     152         102 :     for (x=2;;x++)
     153         102 :       if (is_gener_Fl(x,p,p_1,L) && !is_pm1(Fp_powu(utoipos(x),p_1,p2)))
     154          30 :         return gc_ulong(av, x);
     155             :   }
     156             : #endif
     157             : }
     158             : 
     159             : /* p prime. Return a primitive root modulo p^e, e > 1 */
     160             : GEN
     161      170583 : pgener_Zp(GEN p)
     162             : {
     163      170583 :   if (lgefint(p) == 3) return utoipos(pgener_Zl(p[2]));
     164             :   else
     165             :   {
     166           5 :     const pari_sp av = avma;
     167           5 :     GEN p_1 = subiu(p,1), p2 = sqri(p), L = is_gener_expo(p,NULL);
     168           5 :     GEN x = utoipos(2);
     169          12 :     for (;; x[2]++)
     170          17 :       if (is_gener_Fp(x,p,p_1,L) && !equali1(Fp_pow(x,p_1,p2))) break;
     171           5 :     return gc_utoipos(av, uel(x,2));
     172             :   }
     173             : }
     174             : 
     175             : static GEN
     176         259 : gener_Zp(GEN q, GEN F)
     177             : {
     178         259 :   GEN p = NULL;
     179         259 :   long e = 0;
     180         259 :   if (F)
     181             :   {
     182          14 :     GEN P = gel(F,1), E = gel(F,2);
     183          14 :     long i, l = lg(P);
     184          42 :     for (i = 1; i < l; i++)
     185             :     {
     186          28 :       p = gel(P,i);
     187          28 :       if (absequaliu(p, 2)) continue;
     188          14 :       if (i < l-1) pari_err_DOMAIN("znprimroot", "n","=",F,F);
     189          14 :       e = itos(gel(E,i));
     190             :     }
     191          14 :     if (!p) pari_err_DOMAIN("znprimroot", "n","=",F,F);
     192             :   }
     193             :   else
     194         245 :     e = Z_isanypower(q, &p);
     195         259 :   if (!BPSW_psp(e? p: q)) pari_err_DOMAIN("znprimroot", "n","=", q,q);
     196         245 :   return e > 1? pgener_Zp(p): pgener_Fp(q);
     197             : }
     198             : 
     199             : GEN
     200         329 : znprimroot(GEN N)
     201             : {
     202         329 :   pari_sp av = avma;
     203             :   GEN x, n, F;
     204             : 
     205         329 :   if ((F = check_arith_non0(N,"znprimroot")))
     206             :   {
     207          14 :     F = clean_Z_factor(F);
     208          14 :     N = typ(N) == t_VEC? gel(N,1): factorback(F);
     209             :   }
     210         322 :   N = absi_shallow(N);
     211         322 :   if (abscmpiu(N, 4) <= 0) { set_avma(av); return mkintmodu(N[2]-1,N[2]); }
     212         273 :   switch(mod4(N))
     213             :   {
     214          14 :     case 0: /* N = 0 mod 4 */
     215          14 :       pari_err_DOMAIN("znprimroot", "n","=",N,N);
     216           0 :       x = NULL; break;
     217          28 :     case 2: /* N = 2 mod 4 */
     218          28 :       n = shifti(N,-1); /* becomes odd */
     219          28 :       x = gener_Zp(n,F); if (!mod2(x)) x = addii(x,n);
     220          21 :       break;
     221         231 :     default: /* N odd */
     222         231 :       x = gener_Zp(N,F);
     223         224 :       break;
     224             :   }
     225         245 :   return gerepilecopy(av, mkintmod(x, N));
     226             : }
     227             : 
     228             : /* n | (p-1), returns a primitive n-th root of 1 in F_p^* */
     229             : GEN
     230           0 : rootsof1_Fp(GEN n, GEN p)
     231             : {
     232           0 :   pari_sp av = avma;
     233           0 :   GEN L = odd_prime_divisors(n); /* 2 implicit in pgener_Fp_local */
     234           0 :   GEN z = pgener_Fp_local(p, L);
     235           0 :   z = Fp_pow(z, diviiexact(subiu(p,1), n), p); /* prim. n-th root of 1 */
     236           0 :   return gerepileuptoint(av, z);
     237             : }
     238             : 
     239             : GEN
     240        3033 : rootsof1u_Fp(ulong n, GEN p)
     241             : {
     242        3033 :   pari_sp av = avma;
     243        3033 :   GEN z, L = u_odd_prime_divisors(n); /* 2 implicit in pgener_Fp_local */
     244        3033 :   z = pgener_Fp_local(p, Flv_to_ZV(L));
     245        3033 :   z = Fp_pow(z, diviuexact(subiu(p,1), n), p); /* prim. n-th root of 1 */
     246        3033 :   return gerepileuptoint(av, z);
     247             : }
     248             : 
     249             : ulong
     250      215511 : rootsof1_Fl(ulong n, ulong p)
     251             : {
     252      215511 :   pari_sp av = avma;
     253      215511 :   GEN L = u_odd_prime_divisors(n); /* 2 implicit in pgener_Fl_local */
     254      215511 :   ulong z = pgener_Fl_local(p, L);
     255      215511 :   z = Fl_powu(z, (p-1) / n, p); /* prim. n-th root of 1 */
     256      215511 :   return gc_ulong(av,z);
     257             : }
     258             : 
     259             : /*********************************************************************/
     260             : /**                     INVERSE TOTIENT FUNCTION                    **/
     261             : /*********************************************************************/
     262             : /* N t_INT, L a ZV containing all prime divisors of N, and possibly other
     263             :  * primes. Return factor(N) */
     264             : GEN
     265      350651 : Z_factor_listP(GEN N, GEN L)
     266             : {
     267      350651 :   long i, k, l = lg(L);
     268      350651 :   GEN P = cgetg(l, t_COL), E = cgetg(l, t_COL);
     269     1346688 :   for (i = k = 1; i < l; i++)
     270             :   {
     271      996037 :     GEN p = gel(L,i);
     272      996037 :     long v = Z_pvalrem(N, p, &N);
     273      996037 :     if (v)
     274             :     {
     275      792176 :       gel(P,k) = p;
     276      792176 :       gel(E,k) = utoipos(v);
     277      792176 :       k++;
     278             :     }
     279             :   }
     280      350651 :   setlg(P, k);
     281      350651 :   setlg(E, k); return mkmat2(P,E);
     282             : }
     283             : 
     284             : /* look for x such that phi(x) = n, p | x => p > m (if m = NULL: no condition).
     285             :  * L is a list of primes containing all prime divisors of n. */
     286             : static long
     287      621565 : istotient_i(GEN n, GEN m, GEN L, GEN *px)
     288             : {
     289      621565 :   pari_sp av = avma, av2;
     290             :   GEN k, D;
     291             :   long i, v;
     292      621565 :   if (m && mod2(n))
     293             :   {
     294      270914 :     if (!equali1(n)) return 0;
     295       69986 :     if (px) *px = gen_1;
     296       69986 :     return 1;
     297             :   }
     298      350651 :   D = divisors(Z_factor_listP(shifti(n, -1), L));
     299             :   /* loop through primes p > m, d = p-1 | n */
     300      350651 :   av2 = avma;
     301      350651 :   if (!m)
     302             :   { /* special case p = 2, d = 1 */
     303       69986 :     k = n;
     304       69986 :     for (v = 1;; v++) {
     305       69986 :       if (istotient_i(k, gen_2, L, px)) {
     306       69986 :         if (px) *px = shifti(*px, v);
     307       69986 :         return 1;
     308             :       }
     309           0 :       if (mod2(k)) break;
     310           0 :       k = shifti(k,-1);
     311             :     }
     312           0 :     set_avma(av2);
     313             :   }
     314     1099462 :   for (i = 1; i < lg(D); ++i)
     315             :   {
     316     1001588 :     GEN p, d = shifti(gel(D, i), 1); /* even divisors of n */
     317     1001588 :     if (m && cmpii(d, m) < 0) continue;
     318      677782 :     p = addiu(d, 1);
     319      677782 :     if (!isprime(p)) continue;
     320      442064 :     k = diviiexact(n, d);
     321      481593 :     for (v = 1;; v++) {
     322             :       GEN r;
     323      481593 :       if (istotient_i(k, p, L, px)) {
     324      182791 :         if (px) *px = mulii(*px, powiu(p, v));
     325      182791 :         return 1;
     326             :       }
     327      298802 :       k = dvmdii(k, p, &r);
     328      298802 :       if (r != gen_0) break;
     329             :     }
     330      259273 :     set_avma(av2);
     331             :   }
     332       97874 :   return gc_long(av,0);
     333             : }
     334             : 
     335             : /* find x such that phi(x) = n */
     336             : long
     337       70000 : istotient(GEN n, GEN *px)
     338             : {
     339       70000 :   pari_sp av = avma;
     340       70000 :   if (typ(n) != t_INT) pari_err_TYPE("istotient", n);
     341       70000 :   if (signe(n) < 1) return 0;
     342       70000 :   if (mod2(n))
     343             :   {
     344          14 :     if (!equali1(n)) return 0;
     345          14 :     if (px) *px = gen_1;
     346          14 :     return 1;
     347             :   }
     348       69986 :   if (istotient_i(n, NULL, gel(Z_factor(n), 1), px))
     349             :   {
     350       69986 :     if (!px) set_avma(av);
     351             :     else
     352       69986 :       *px = gerepileuptoint(av, *px);
     353       69986 :     return 1;
     354             :   }
     355           0 :   return gc_long(av,0);
     356             : }
     357             : 
     358             : /*********************************************************************/
     359             : /**                        KRONECKER SYMBOL                         **/
     360             : /*********************************************************************/
     361             : /* t = 3,5 mod 8 ?  (= 2 not a square mod t) */
     362             : static int
     363   321086368 : ome(long t)
     364             : {
     365   321086368 :   switch(t & 7)
     366             :   {
     367   182118398 :     case 3:
     368   182118398 :     case 5: return 1;
     369   138967970 :     default: return 0;
     370             :   }
     371             : }
     372             : /* t a t_INT, is t = 3,5 mod 8 ? */
     373             : static int
     374     5601798 : gome(GEN t)
     375     5601798 : { return signe(t)? ome( mod2BIL(t) ): 0; }
     376             : 
     377             : /* assume y odd, return kronecker(x,y) * s */
     378             : static long
     379   227925128 : krouu_s(ulong x, ulong y, long s)
     380             : {
     381   227925128 :   ulong x1 = x, y1 = y, z;
     382  1032183875 :   while (x1)
     383             :   {
     384   804267074 :     long r = vals(x1);
     385   804334526 :     if (r)
     386             :     {
     387   427562576 :       if (odd(r) && ome(y1)) s = -s;
     388   427486797 :       x1 >>= r;
     389             :     }
     390   804258747 :     if (x1 & y1 & 2) s = -s;
     391   804258747 :     z = y1 % x1; y1 = x1; x1 = z;
     392             :   }
     393   227916801 :   return (y1 == 1)? s: 0;
     394             : }
     395             : 
     396             : long
     397    11969999 : kronecker(GEN x, GEN y)
     398             : {
     399    11969999 :   pari_sp av = avma;
     400    11969999 :   long s = 1, r;
     401             :   ulong xu;
     402             : 
     403    11969999 :   if (typ(x) != t_INT) pari_err_TYPE("kronecker",x);
     404    11969999 :   if (typ(y) != t_INT) pari_err_TYPE("kronecker",y);
     405    11969999 :   switch (signe(y))
     406             :   {
     407          63 :     case -1: y = negi(y); if (signe(x) < 0) s = -1; break;
     408         133 :     case 0: return is_pm1(x);
     409             :   }
     410    11969866 :   r = vali(y);
     411    11969859 :   if (r)
     412             :   {
     413     1348912 :     if (!mpodd(x)) return gc_long(av,0);
     414      321711 :     if (odd(r) && gome(x)) s = -s;
     415      321711 :     y = shifti(y,-r);
     416             :   }
     417    10942658 :   x = modii(x,y);
     418    13336011 :   while (lgefint(x) > 3) /* x < y */
     419             :   {
     420             :     GEN z;
     421     2393411 :     r = vali(x);
     422     2393059 :     if (r)
     423             :     {
     424     1307043 :       if (odd(r) && gome(y)) s = -s;
     425     1306958 :       x = shifti(x,-r);
     426             :     }
     427             :     /* x=3 mod 4 && y=3 mod 4 ? (both are odd here) */
     428     2392431 :     if (mod2BIL(x) & mod2BIL(y) & 2) s = -s;
     429     2391656 :     z = remii(y,x); y = x; x = z;
     430     2393452 :     if (gc_needed(av,2))
     431             :     {
     432           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"kronecker");
     433           0 :       gerepileall(av, 2, &x, &y);
     434             :     }
     435             :   }
     436    10942600 :   xu = itou(x);
     437    10942596 :   if (!xu) return is_pm1(y)? s: 0;
     438    10845267 :   r = vals(xu);
     439    10845266 :   if (r)
     440             :   {
     441     5760577 :     if (odd(r) && gome(y)) s = -s;
     442     5760577 :     xu >>= r;
     443             :   }
     444             :   /* x=3 mod 4 && y=3 mod 4 ? (both are odd here) */
     445    10845266 :   if (xu & mod2BIL(y) & 2) s = -s;
     446    10845266 :   return gc_long(av, krouu_s(umodiu(y,xu), xu, s));
     447             : }
     448             : 
     449             : long
     450       39753 : krois(GEN x, long y)
     451             : {
     452             :   ulong yu;
     453       39753 :   long s = 1;
     454             : 
     455       39753 :   if (y <= 0)
     456             :   {
     457          28 :     if (y == 0) return is_pm1(x);
     458           0 :     yu = (ulong)-y; if (signe(x) < 0) s = -1;
     459             :   }
     460             :   else
     461       39725 :     yu = (ulong)y;
     462       39725 :   if (!odd(yu))
     463             :   {
     464             :     long r;
     465       18417 :     if (!mpodd(x)) return 0;
     466       12467 :     r = vals(yu); yu >>= r;
     467       12467 :     if (odd(r) && gome(x)) s = -s;
     468             :   }
     469       33775 :   return krouu_s(umodiu(x, yu), yu, s);
     470             : }
     471             : /* assume y != 0 */
     472             : long
     473    27629652 : kroiu(GEN x, ulong y)
     474             : {
     475             :   long r;
     476    27629652 :   if (odd(y)) return krouu_s(umodiu(x,y), y, 1);
     477      303064 :   if (!mpodd(x)) return 0;
     478      208356 :   r = vals(y); y >>= r;
     479      208356 :   return krouu_s(umodiu(x,y), y, (odd(r) && gome(x))? -1: 1);
     480             : }
     481             : 
     482             : /* assume y > 0, odd, return s * kronecker(x,y) */
     483             : static long
     484      178232 : krouodd(ulong x, GEN y, long s)
     485             : {
     486             :   long r;
     487      178232 :   if (lgefint(y) == 3) return krouu_s(x, y[2], s);
     488       27969 :   if (!x) return 0; /* y != 1 */
     489       27969 :   r = vals(x);
     490       27969 :   if (r)
     491             :   {
     492       14507 :     if (odd(r) && gome(y)) s = -s;
     493       14507 :     x >>= r;
     494             :   }
     495             :   /* x=3 mod 4 && y=3 mod 4 ? (both are odd here) */
     496       27969 :   if (x & mod2BIL(y) & 2) s = -s;
     497       27969 :   return krouu_s(umodiu(y,x), x, s);
     498             : }
     499             : 
     500             : long
     501      143221 : krosi(long x, GEN y)
     502             : {
     503      143221 :   const pari_sp av = avma;
     504      143221 :   long s = 1, r;
     505      143221 :   switch (signe(y))
     506             :   {
     507           0 :     case -1: y = negi(y); if (x < 0) s = -1; break;
     508           0 :     case 0: return (x==1 || x==-1);
     509             :   }
     510      143221 :   r = vali(y);
     511      143221 :   if (r)
     512             :   {
     513       16884 :     if (!odd(x)) return gc_long(av,0);
     514       16884 :     if (odd(r) && ome(x)) s = -s;
     515       16884 :     y = shifti(y,-r);
     516             :   }
     517      143221 :   if (x < 0) { x = -x; if (mod4(y) == 3) s = -s; }
     518      143221 :   return gc_long(av, krouodd((ulong)x, y, s));
     519             : }
     520             : 
     521             : long
     522       35011 : kroui(ulong x, GEN y)
     523             : {
     524       35011 :   const pari_sp av = avma;
     525       35011 :   long s = 1, r;
     526       35011 :   switch (signe(y))
     527             :   {
     528           0 :     case -1: y = negi(y); break;
     529           0 :     case 0: return x==1UL;
     530             :   }
     531       35011 :   r = vali(y);
     532       35011 :   if (r)
     533             :   {
     534           0 :     if (!odd(x)) return gc_long(av,0);
     535           0 :     if (odd(r) && ome(x)) s = -s;
     536           0 :     y = shifti(y,-r);
     537             :   }
     538       35011 :   return gc_long(av, krouodd(x, y, s));
     539             : }
     540             : 
     541             : long
     542    97730392 : kross(long x, long y)
     543             : {
     544             :   ulong yu;
     545    97730392 :   long s = 1;
     546             : 
     547    97730392 :   if (y <= 0)
     548             :   {
     549       68943 :     if (y == 0) return (labs(x)==1);
     550       68915 :     yu = (ulong)-y; if (x < 0) s = -1;
     551             :   }
     552             :   else
     553    97661449 :     yu = (ulong)y;
     554    97730364 :   if (!odd(yu))
     555             :   {
     556             :     long r;
     557    23570710 :     if (!odd(x)) return 0;
     558    16652170 :     r = vals(yu); yu >>= r;
     559    16652170 :     if (odd(r) && ome(x)) s = -s;
     560             :   }
     561    90811824 :   x %= (long)yu; if (x < 0) x += yu;
     562    90811824 :   return krouu_s((ulong)x, yu, s);
     563             : }
     564             : 
     565             : long
     566    98539692 : krouu(ulong x, ulong y)
     567             : {
     568             :   long r;
     569    98539692 :   if (odd(y)) return krouu_s(x, y, 1);
     570       16995 :   if (!odd(x)) return 0;
     571       17127 :   r = vals(y); y >>= r;
     572       17127 :   return krouu_s(x, y, (odd(r) && ome(x))? -1: 1);
     573             : }
     574             : 
     575             : /*********************************************************************/
     576             : /**                          HILBERT SYMBOL                         **/
     577             : /*********************************************************************/
     578             : /* x,y are t_INT or t_REAL */
     579             : static long
     580        7329 : mphilbertoo(GEN x, GEN y)
     581             : {
     582        7329 :   long sx = signe(x), sy = signe(y);
     583        7329 :   if (!sx || !sy) return 0;
     584        7329 :   return (sx < 0 && sy < 0)? -1: 1;
     585             : }
     586             : 
     587             : long
     588      140826 : hilbertii(GEN x, GEN y, GEN p)
     589             : {
     590             :   pari_sp av;
     591             :   long oddvx, oddvy, z;
     592             : 
     593      140826 :   if (!p) return mphilbertoo(x,y);
     594      133518 :   if (is_pm1(p) || signe(p) < 0) pari_err_PRIME("hilbertii",p);
     595      133518 :   if (!signe(x) || !signe(y)) return 0;
     596      133497 :   av = avma;
     597      133497 :   oddvx = odd(Z_pvalrem(x,p,&x));
     598      133497 :   oddvy = odd(Z_pvalrem(y,p,&y));
     599             :   /* x, y are p-units, compute hilbert(x * p^oddvx, y * p^oddvy, p) */
     600      133497 :   if (absequaliu(p, 2))
     601             :   {
     602       12355 :     z = (Mod4(x) == 3 && Mod4(y) == 3)? -1: 1;
     603       12355 :     if (oddvx && gome(y)) z = -z;
     604       12355 :     if (oddvy && gome(x)) z = -z;
     605             :   }
     606             :   else
     607             :   {
     608      121142 :     z = (oddvx && oddvy && mod4(p) == 3)? -1: 1;
     609      121142 :     if (oddvx && kronecker(y,p) < 0) z = -z;
     610      121142 :     if (oddvy && kronecker(x,p) < 0) z = -z;
     611             :   }
     612      133497 :   return gc_long(av, z);
     613             : }
     614             : 
     615             : static void
     616         196 : err_prec(void) { pari_err_PREC("hilbert"); }
     617             : static void
     618         161 : err_p(GEN p, GEN q) { pari_err_MODULUS("hilbert", p,q); }
     619             : static void
     620          56 : err_oo(GEN p) { pari_err_MODULUS("hilbert", p, strtoGENstr("oo")); }
     621             : 
     622             : /* x t_INTMOD, *pp = prime or NULL [ unset, set it to x.mod ].
     623             :  * Return lift(x) provided it's p-adic accuracy is large enough to decide
     624             :  * hilbert()'s value [ problem at p = 2 ] */
     625             : static GEN
     626         420 : lift_intmod(GEN x, GEN *pp)
     627             : {
     628         420 :   GEN p = *pp, N = gel(x,1);
     629         420 :   x = gel(x,2);
     630         420 :   if (!p)
     631             :   {
     632         266 :     *pp = p = N;
     633         266 :     switch(itos_or_0(p))
     634             :     {
     635         126 :       case 2:
     636         126 :       case 4: err_prec();
     637             :     }
     638         140 :     return x;
     639             :   }
     640         154 :   if (!signe(p)) err_oo(N);
     641         112 :   if (absequaliu(p,2))
     642          42 :   { if (vali(N) <= 2) err_prec(); }
     643             :   else
     644          70 :   { if (!dvdii(N,p)) err_p(N,p); }
     645          28 :   if (!signe(x)) err_prec();
     646          21 :   return x;
     647             : }
     648             : /* x t_PADIC, *pp = prime or NULL [ unset, set it to x.p ].
     649             :  * Return lift(x)*p^(v(x) mod 2) provided it's p-adic accuracy is large enough
     650             :  * to decide hilbert()'s value [ problem at p = 2 ]*/
     651             : static GEN
     652         210 : lift_padic(GEN x, GEN *pp)
     653             : {
     654         210 :   GEN p = *pp, q = gel(x,2), y = gel(x,4);
     655         210 :   if (!p) *pp = p = q;
     656         147 :   else if (!equalii(p,q)) err_p(p, q);
     657         105 :   if (absequaliu(p,2) && precp(x) <= 2) err_prec();
     658          70 :   if (!signe(y)) err_prec();
     659          70 :   return odd(valp(x))? mulii(p,y): y;
     660             : }
     661             : 
     662             : long
     663       62314 : hilbert(GEN x, GEN y, GEN p)
     664             : {
     665       62314 :   pari_sp av = avma;
     666       62314 :   long tx = typ(x), ty = typ(y);
     667             : 
     668       62314 :   if (p && typ(p) != t_INT) pari_err_TYPE("hilbert",p);
     669       62314 :   if (tx == t_REAL)
     670             :   {
     671          77 :     if (p && signe(p)) err_oo(p);
     672          63 :     switch (ty)
     673             :     {
     674           7 :       case t_INT:
     675           7 :       case t_REAL: return mphilbertoo(x,y);
     676           0 :       case t_FRAC: return mphilbertoo(x,gel(y,1));
     677          56 :       default: pari_err_TYPE2("hilbert",x,y);
     678             :     }
     679             :   }
     680       62237 :   if (ty == t_REAL)
     681             :   {
     682          14 :     if (p && signe(p)) err_oo(p);
     683          14 :     switch (tx)
     684             :     {
     685          14 :       case t_INT:
     686          14 :       case t_REAL: return mphilbertoo(x,y);
     687           0 :       case t_FRAC: return mphilbertoo(gel(x,1),y);
     688           0 :       default: pari_err_TYPE2("hilbert",x,y);
     689             :     }
     690             :   }
     691       62223 :   if (tx == t_INTMOD) { x = lift_intmod(x, &p); tx = t_INT; }
     692       62020 :   if (ty == t_INTMOD) { y = lift_intmod(y, &p); ty = t_INT; }
     693             : 
     694       61964 :   if (tx == t_PADIC) { x = lift_padic(x, &p); tx = t_INT; }
     695       61901 :   if (ty == t_PADIC) { y = lift_padic(y, &p); ty = t_INT; }
     696             : 
     697       61824 :   if (tx == t_FRAC) { tx = t_INT; x = p? mulii(gel(x,1),gel(x,2)): gel(x,1); }
     698       61824 :   if (ty == t_FRAC) { ty = t_INT; y = p? mulii(gel(y,1),gel(y,2)): gel(y,1); }
     699             : 
     700       61824 :   if (tx != t_INT || ty != t_INT) pari_err_TYPE2("hilbert",x,y);
     701       61824 :   if (p && !signe(p)) p = NULL;
     702       61824 :   return gc_long(av, hilbertii(x,y,p));
     703             : }
     704             : 
     705             : /*******************************************************************/
     706             : /*                       SQUARE ROOT MODULO p                      */
     707             : /*******************************************************************/
     708             : static void
     709     2260639 : checkp(ulong q, ulong p)
     710     2260639 : { if (!q) pari_err_PRIME("Fl_nonsquare",utoipos(p)); }
     711             : /* p = 1 (mod 4) prime, return the first quadratic nonresidue, a prime */
     712             : static ulong
     713    11621253 : nonsquare1_Fl(ulong p)
     714             : {
     715             :   forprime_t S;
     716             :   ulong q;
     717    11621253 :   if ((p & 7UL) != 1) return 2UL;
     718     4365714 :   q = p % 3; if (q == 2) return 3UL;
     719     1409088 :   checkp(q, p);
     720     1414147 :   q = p % 5; if (q == 2 || q == 3) return 5UL;
     721      523812 :   checkp(q, p);
     722      523815 :   q = p % 7; if (q != 4 && q >= 3) return 7UL;
     723      195154 :   checkp(q, p);
     724             :   /* log^2(2^64) < 1968 is enough under GRH (and p^(1/4)log(p) without it)*/
     725      195187 :   u_forprime_init(&S, 11, 1967);
     726      322891 :   while ((q = u_forprime_next(&S)))
     727             :   {
     728      322889 :     if (krouu(q, p) < 0) return q;
     729      127696 :     checkp(q, p);
     730             :   }
     731           0 :   checkp(0, p);
     732             :   return 0; /*LCOV_EXCL_LINE*/
     733             : }
     734             : /* p > 2 a prime */
     735             : ulong
     736        7935 : nonsquare_Fl(ulong p)
     737        7935 : { return ((p & 3UL) == 3)? p-1: nonsquare1_Fl(p); }
     738             : 
     739             : /* allow pi = 0 */
     740             : ulong
     741      177361 : Fl_2gener_pre(ulong p, ulong pi)
     742             : {
     743      177361 :   ulong p1 = p-1;
     744      177361 :   long e = vals(p1);
     745      177349 :   if (e == 1) return p1;
     746       65779 :   return Fl_powu_pre(nonsquare1_Fl(p), p1 >> e, p, pi);
     747             : }
     748             : 
     749             : ulong
     750       67842 : Fl_2gener_pre_i(ulong  ns, ulong p, ulong pi)
     751             : {
     752       67842 :   ulong p1 = p-1;
     753       67842 :   long e = vals(p1);
     754       67842 :   if (e == 1) return p1;
     755       26006 :   return Fl_powu_pre(ns, p1 >> e, p, pi);
     756             : }
     757             : 
     758             : static ulong
     759    12269386 : Fl_sqrt_i(ulong a, ulong y, ulong p)
     760             : {
     761             :   long i, e, k;
     762             :   ulong p1, q, v, w;
     763             : 
     764    12269386 :   if (!a) return 0;
     765    10995389 :   p1 = p - 1; e = vals(p1);
     766    10996365 :   if (e == 0) /* p = 2 */
     767             :   {
     768      649832 :     if (p != 2) pari_err_PRIME("Fl_sqrt [modulus]",utoi(p));
     769      650983 :     return ((a & 1) == 0)? 0: 1;
     770             :   }
     771    10346533 :   if (e == 1)
     772             :   {
     773     4883857 :     v = Fl_powu(a, (p+1) >> 2, p);
     774     4883934 :     if (Fl_sqr(v, p) != a) return ~0UL;
     775     4879068 :     p1 = p - v; if (v > p1) v = p1;
     776     4879068 :     return v;
     777             :   }
     778     5462676 :   q = p1 >> e; /* q = (p-1)/2^oo is odd */
     779     5462676 :   p1 = Fl_powu(a, q >> 1, p); /* a ^ [(q-1)/2] */
     780     5462700 :   if (!p1) return 0;
     781     5462700 :   v = Fl_mul(a, p1, p);
     782     5462692 :   w = Fl_mul(v, p1, p);
     783     5462677 :   if (!y) y = Fl_powu(nonsquare1_Fl(p), q, p);
     784     9289289 :   while (w != 1)
     785             :   { /* a*w = v^2, y primitive 2^e-th root of 1
     786             :        a square --> w even power of y, hence w^(2^(e-1)) = 1 */
     787     3828498 :     p1 = Fl_sqr(w, p);
     788     6358099 :     for (k=1; p1 != 1 && k < e; k++) p1 = Fl_sqr(p1, p);
     789     3828489 :     if (k == e) return ~0UL;
     790             :     /* w ^ (2^k) = 1 --> w = y ^ (u * 2^(e-k)), u odd */
     791     3826412 :     p1 = y;
     792     5102400 :     for (i=1; i < e-k; i++) p1 = Fl_sqr(p1, p);
     793     3826411 :     y = Fl_sqr(p1, p); e = k;
     794     3826452 :     w = Fl_mul(y, w, p);
     795     3826453 :     v = Fl_mul(v, p1, p);
     796             :   }
     797     5460791 :   p1 = p - v; if (v > p1) v = p1;
     798     5460791 :   return v;
     799             : }
     800             : 
     801             : /* Tonelli-Shanks. Assume p is prime and (a,p) != -1. Allow pi = 0 */
     802             : ulong
     803    33536047 : Fl_sqrt_pre_i(ulong a, ulong y, ulong p, ulong pi)
     804             : {
     805             :   long i, e, k;
     806             :   ulong p1, q, v, w;
     807             : 
     808    33536047 :   if (!pi) return Fl_sqrt_i(a, y, p);
     809    21266673 :   if (!a) return 0;
     810    21141288 :   p1 = p - 1; e = vals(p1);
     811    21144891 :   if (e == 0) /* p = 2 */
     812             :   {
     813           0 :     if (p != 2) pari_err_PRIME("Fl_sqrt [modulus]",utoi(p));
     814           0 :     return ((a & 1) == 0)? 0: 1;
     815             :   }
     816    21159872 :   if (e == 1)
     817             :   {
     818    15019364 :     v = Fl_powu_pre(a, (p+1) >> 2, p, pi);
     819    15020943 :     if (Fl_sqr_pre(v, p, pi) != a) return ~0UL;
     820    15018959 :     p1 = p - v; if (v > p1) v = p1;
     821    15018959 :     return v;
     822             :   }
     823     6140508 :   q = p1 >> e; /* q = (p-1)/2^oo is odd */
     824     6140508 :   p1 = Fl_powu_pre(a, q >> 1, p, pi); /* a ^ [(q-1)/2] */
     825     6141326 :   if (!p1) return 0;
     826     6141326 :   v = Fl_mul_pre(a, p1, p, pi);
     827     6141884 :   w = Fl_mul_pre(v, p1, p, pi);
     828     6142051 :   if (!y) y = Fl_powu_pre(nonsquare1_Fl(p), q, p, pi);
     829    11688709 :   while (w != 1)
     830             :   { /* a*w = v^2, y primitive 2^e-th root of 1
     831             :        a square --> w even power of y, hence w^(2^(e-1)) = 1 */
     832     5546421 :     p1 = Fl_sqr_pre(w,p,pi);
     833    10379827 :     for (k=1; p1 != 1 && k < e; k++) p1 = Fl_sqr_pre(p1,p,pi);
     834     5545780 :     if (k == e) return ~0UL;
     835             :     /* w ^ (2^k) = 1 --> w = y ^ (u * 2^(e-k)), u odd */
     836     5545688 :     p1 = y;
     837     7286070 :     for (i=1; i < e-k; i++) p1 = Fl_sqr_pre(p1, p, pi);
     838     5545789 :     y = Fl_sqr_pre(p1, p, pi); e = k;
     839     5547049 :     w = Fl_mul_pre(y, w, p, pi);
     840     5546669 :     v = Fl_mul_pre(v, p1, p, pi);
     841             :   }
     842     6142288 :   p1 = p - v; if (v > p1) v = p1;
     843     6142288 :   return v;
     844             : }
     845             : 
     846             : ulong
     847    12324486 : Fl_sqrt(ulong a, ulong p)
     848    12324486 : { ulong pi = (p & HIGHMASK)? get_Fl_red(p): 0; return Fl_sqrt_pre_i(a, 0, p, pi); }
     849             : 
     850             : ulong
     851    21047907 : Fl_sqrt_pre(ulong a, ulong p, ulong pi)
     852    21047907 : { return Fl_sqrt_pre_i(a, 0, p, pi); }
     853             : 
     854             : /* allow pi = 0 */
     855             : static ulong
     856      141292 : Fl_lgener_pre_all(ulong l, long e, ulong r, ulong p, ulong pi, ulong *pt_m)
     857             : {
     858      141292 :   ulong x, y, m, le1 = upowuu(l, e-1);
     859      141293 :   for (x = 2; ; x++)
     860             :   {
     861      172222 :     y = Fl_powu_pre(x, r, p, pi);
     862      172222 :     if (y==1) continue;
     863      155077 :     m = Fl_powu_pre(y, le1, p, pi);
     864      155078 :     if (m != 1) break;
     865             :   }
     866      141294 :   *pt_m = m; return y;
     867             : }
     868             : 
     869             : /* solve x^l = a , l prime in G of order q.
     870             :  *
     871             :  * q =  (l^e)*r, e >= 1, (r,l) = 1
     872             :  * y generates the l-Sylow of G
     873             :  * m = y^(l^(e-1)) != 1 */
     874             : static ulong
     875      225519 : Fl_sqrtl_raw(ulong a, ulong l, ulong e, ulong r, ulong p, ulong pi, ulong y, ulong m)
     876             : {
     877             :   ulong u2, p1, v, w, z, dl;
     878      225519 :   if (a==0) return a;
     879      225513 :   u2 = Fl_inv(l%r, r);
     880      225513 :   v = Fl_powu_pre(a, u2, p, pi);
     881      225508 :   w = Fl_powu_pre(v, l, p, pi);
     882      225506 :   w = pi? Fl_mul_pre(w, Fl_inv(a, p), p, pi): Fl_div(w, a, p);
     883      225495 :   if (w==1) return v;
     884      139175 :   if (y==0) y = Fl_lgener_pre_all(l, e, r, p, pi, &m);
     885      164683 :   while (w!=1)
     886             :   {
     887      144612 :     ulong k = 0;
     888      144612 :     p1 = w;
     889             :     do
     890             :     {
     891      188208 :       z = p1; p1 = Fl_powu_pre(p1, l, p, pi);
     892      188207 :       if (++k == e) return ULONG_MAX;
     893       69102 :     } while (p1!=1);
     894       25506 :     dl = Fl_log_pre(z, m, l, p, pi);
     895       25506 :     dl = Fl_neg(dl, l);
     896       25506 :     p1 = Fl_powu_pre(y,dl*upowuu(l,e-k-1),p,pi);
     897       25506 :     m = Fl_powu_pre(m, dl, p, pi);
     898       25506 :     e = k;
     899       25506 :     v = pi? Fl_mul_pre(p1,v,p,pi): Fl_mul(p1,v,p);
     900       25506 :     y = Fl_powu_pre(p1,l,p,pi);
     901       25506 :     w = pi? Fl_mul_pre(y,w,p,pi): Fl_mul(y,w,p);
     902             :   }
     903       20071 :   return v;
     904             : }
     905             : 
     906             : /* allow pi = 0 */
     907             : static ulong
     908      223107 : Fl_sqrtl_i(ulong a, ulong l, ulong p, ulong pi, ulong y, ulong m)
     909             : {
     910      223107 :   ulong r, e = u_lvalrem(p-1, l, &r);
     911      223107 :   return Fl_sqrtl_raw(a, l, e, r, p, pi, y, m);
     912             : }
     913             : /* allow pi = 0 */
     914             : ulong
     915      223107 : Fl_sqrtl_pre(ulong a, ulong l, ulong p, ulong pi)
     916      223107 : { return Fl_sqrtl_i(a, l, p, pi, 0, 0); }
     917             : 
     918             : ulong
     919           0 : Fl_sqrtl(ulong a, ulong l, ulong p)
     920           0 : { ulong pi = (p & HIGHMASK)? get_Fl_red(p): 0;
     921           0 :   return Fl_sqrtl_i(a, l, p, pi, 0, 0); }
     922             : 
     923             : /* allow pi = 0 */
     924             : ulong
     925      196860 : Fl_sqrtn_pre(ulong a, long n, ulong p, ulong pi, ulong *zetan)
     926             : {
     927      196860 :   ulong m, q = p-1, z;
     928      196860 :   ulong nn = n >= 0 ? (ulong)n: -(ulong)n;
     929      196860 :   if (a==0)
     930             :   {
     931      116389 :     if (n < 0) pari_err_INV("Fl_sqrtn", mkintmod(gen_0,utoi(p)));
     932      116382 :     if (zetan) *zetan = 1UL;
     933      116382 :     return 0;
     934             :   }
     935       80471 :   if (n==1)
     936             :   {
     937         420 :     if (zetan) *zetan = 1;
     938         420 :     return n < 0? Fl_inv(a,p): a;
     939             :   }
     940       80051 :   if (n==2)
     941             :   {
     942        7006 :     if (zetan) *zetan = p-1;
     943        7006 :     return Fl_sqrt_pre_i(a, 0, p, pi);
     944             :   }
     945       73045 :   if (a == 1 && !zetan) return a;
     946       43587 :   m = ugcd(nn, q);
     947       43587 :   z = 1;
     948       43587 :   if (m!=1)
     949             :   {
     950        2112 :     GEN F = factoru(m);
     951             :     long i, j, e;
     952             :     ulong r, zeta, y, l;
     953        4607 :     for (i = nbrows(F); i; i--)
     954             :     {
     955        2558 :       l = ucoeff(F,i,1);
     956        2558 :       j = ucoeff(F,i,2);
     957        2558 :       e = u_lvalrem(q,l, &r);
     958        2558 :       y = Fl_lgener_pre_all(l, e, r, p, pi, &zeta);
     959        2558 :       if (zetan)
     960             :       {
     961         784 :         ulong Y = Fl_powu_pre(y, upowuu(l,e-j), p, pi);
     962         784 :         z = pi? Fl_mul_pre(z, Y, p, pi): Fl_mul(z, Y, p);
     963             :       }
     964        2558 :       if (a!=1)
     965             :         do
     966             :         {
     967        2411 :           a = Fl_sqrtl_raw(a, l, e, r, p, pi, y, zeta);
     968        2397 :           if (a==ULONG_MAX) return ULONG_MAX;
     969        2348 :         } while (--j);
     970             :     }
     971             :   }
     972       43524 :   if (m != nn)
     973             :   {
     974       41496 :     ulong qm = q/m, nm = (nn/m) % qm;
     975       41496 :     a = Fl_powu_pre(a, Fl_inv(nm, qm), p, pi);
     976             :   }
     977       43524 :   if (n < 0) a = Fl_inv(a, p);
     978       43524 :   if (zetan) *zetan = z;
     979       43524 :   return a;
     980             : }
     981             : 
     982             : ulong
     983      196860 : Fl_sqrtn(ulong a, long n, ulong p, ulong *zetan)
     984             : {
     985      196860 :   ulong pi = (p & HIGHMASK)? get_Fl_red(p): 0;
     986      196860 :   return Fl_sqrtn_pre(a, n, p, pi, zetan);
     987             : }
     988             : 
     989             : /* Cipolla is better than Tonelli-Shanks when e = v_2(p-1) is "too big".
     990             :  * Otherwise, is a constant times worse; for p = 3 (mod 4), is about 3 times worse,
     991             :  * and in average is about 2 or 2.5 times worse. But try both algorithms for
     992             :  * S(n) = (2^n+3)^2-8 with n = 750, 771, 779, 790, 874, 1176, 1728, 2604, etc.
     993             :  *
     994             :  * If X^2 := t^2 - a  is not a square in F_p (so X is in F_p^2), then
     995             :  *   (t+X)^(p+1) = (t-X)(t+X) = a,   hence  sqrt(a) = (t+X)^((p+1)/2)  in F_p^2.
     996             :  * If (a|p)=1, then sqrt(a) is in F_p.
     997             :  * cf: LNCS 2286, pp 430-434 (2002)  [Gonzalo Tornaria] */
     998             : 
     999             : /* compute y^2, y = y[1] + y[2] X */
    1000             : static GEN
    1001           0 : sqrt_Cipolla_sqr(void *data, GEN y)
    1002             : {
    1003           0 :   GEN u = gel(y,1), v = gel(y,2), p = gel(data,2), n = gel(data,3);
    1004           0 :   GEN u2 = sqri(u), v2 = sqri(v);
    1005           0 :   v = subii(sqri(addii(v,u)), addii(u2,v2));
    1006           0 :   u = addii(u2, mulii(v2,n));
    1007           0 :   retmkvec2(modii(u,p), modii(v,p));
    1008             : }
    1009             : /* compute (t+X) y^2 */
    1010             : static GEN
    1011           0 : sqrt_Cipolla_msqr(void *data, GEN y)
    1012             : {
    1013           0 :   GEN u = gel(y,1), v = gel(y,2), a = gel(data,1), p = gel(data,2);
    1014           0 :   ulong t = gel(data,4)[2];
    1015           0 :   GEN d = addii(u, mului(t,v)), d2 = sqri(d);
    1016           0 :   GEN b = remii(mulii(a,v), p);
    1017           0 :   u = subii(mului(t,d2), mulii(b,addii(u,d)));
    1018           0 :   v = subii(d2, mulii(b,v));
    1019           0 :   retmkvec2(modii(u,p), modii(v,p));
    1020             : }
    1021             : /* assume a reduced mod p [ otherwise correct but inefficient ] */
    1022             : static GEN
    1023           0 : sqrt_Cipolla(GEN a, GEN p)
    1024             : {
    1025             :   pari_sp av;
    1026             :   GEN u, n, y, pov2;
    1027             :   ulong t;
    1028             : 
    1029           0 :   if (kronecker(a, p) < 0) return NULL;
    1030           0 :   pov2 = shifti(p,-1); /* center to avoid multiplying by huge base*/
    1031           0 :   if (cmpii(a,pov2) > 0) a = subii(a,p);
    1032           0 :   av = avma;
    1033           0 :   for (t=1; ; t++, set_avma(av))
    1034             :   {
    1035           0 :     n = subsi((long)(t*t), a);
    1036           0 :     if (kronecker(n, p) < 0) break;
    1037             :   }
    1038             : 
    1039             :   /* compute (t+X)^((p-1)/2) =: u+vX */
    1040           0 :   u = utoipos(t);
    1041           0 :   y = gen_pow_fold(mkvec2(u, gen_1), pov2, mkvec4(a,p,n,u),
    1042             :                    sqrt_Cipolla_sqr, sqrt_Cipolla_msqr);
    1043             :   /* Now u+vX = (t+X)^((p-1)/2); thus
    1044             :    *   (u+vX)(t+X) = sqrt(a) + 0 X
    1045             :    * Whence,
    1046             :    *   sqrt(a) = (u+vt)t - v*a
    1047             :    *   0       = (u+vt)
    1048             :    * Thus a square root is v*a */
    1049           0 :   return Fp_mul(gel(y,2), a, p);
    1050             : }
    1051             : 
    1052             : /* Return NULL if p is found to be composite.
    1053             :  * p odd, q = (p-1)/2^oo is odd */
    1054             : static GEN
    1055        5910 : Fp_2gener_all(GEN q, GEN p)
    1056             : {
    1057             :   long k;
    1058        5910 :   for (k = 2;; k++)
    1059       11613 :   {
    1060       17523 :     long i = kroui(k, p);
    1061       17523 :     if (i < 0) return Fp_pow(utoipos(k), q, p);
    1062       11613 :     if (i == 0) return NULL;
    1063             :   }
    1064             : }
    1065             : 
    1066             : /* Return NULL if p is found to be composite */
    1067             : GEN
    1068        3192 : Fp_2gener(GEN p)
    1069             : {
    1070        3192 :   GEN q = subiu(p, 1);
    1071        3192 :   long e = Z_lvalrem(q, 2, &q);
    1072        3192 :   if (e == 0 && !equaliu(p,2)) return NULL;
    1073        3192 :   return Fp_2gener_all(q, p);
    1074             : }
    1075             : 
    1076             : GEN
    1077       19931 : Fp_2gener_i(GEN ns, GEN p)
    1078             : {
    1079       19931 :   GEN q = subiu(p,1);
    1080       19931 :   long e = vali(q);
    1081       19931 :   if (e == 1) return q;
    1082       18546 :   return Fp_pow(ns, shifti(q,-e), p);
    1083             : }
    1084             : 
    1085             : static GEN
    1086        1472 : nonsquare_Fp(GEN p)
    1087             : {
    1088             :   forprime_t T;
    1089             :   ulong a;
    1090        1472 :   if (mod4(p)==3) return gen_m1;
    1091        1472 :   if (mod8(p)==5) return gen_2;
    1092         721 :   u_forprime_init(&T, 3, ULONG_MAX);
    1093        1382 :   while((a = u_forprime_next(&T)))
    1094        1382 :     if (kroui(a,p) < 0) return utoi(a);
    1095           0 :   pari_err_PRIME("Fp_sqrt [modulus]",p);
    1096             :   return NULL; /* LCOV_EXCL_LINE */
    1097             : }
    1098             : 
    1099             : static GEN
    1100         796 : Fp_rootsof1(ulong l, GEN p)
    1101             : {
    1102         796 :   GEN z, pl = diviuexact(subis(p,1),l);
    1103             :   ulong a;
    1104             :   forprime_t T;
    1105         796 :   u_forprime_init(&T, 3, ULONG_MAX);
    1106        1062 :   while((a = u_forprime_next(&T)))
    1107             :   {
    1108        1062 :     z = Fp_pow(utoi(a), pl, p);
    1109        1062 :     if (!equali1(z)) return z;
    1110             :   }
    1111           0 :   pari_err_PRIME("Fp_sqrt [modulus]",p);
    1112             :   return NULL; /* LCOV_EXCL_LINE */
    1113             : }
    1114             : 
    1115             : static GEN
    1116         334 : Fp_gausssum(long D, GEN p)
    1117             : {
    1118         334 :   long i, l = labs(D);
    1119         334 :   GEN z = Fp_rootsof1(l, p);
    1120         334 :   GEN s = z, x = z;
    1121        3020 :   for(i = 2; i < l; i++)
    1122             :   {
    1123        2686 :     long k = kross(i,l);
    1124        2686 :     x = mulii(x, z);
    1125        2686 :     if (k==1) s = addii(s, x);
    1126        1510 :     else if (k==-1) s = subii(s, x);
    1127             :   }
    1128         334 :   return s;
    1129             : }
    1130             : 
    1131             : static GEN
    1132       19197 : Fp_sqrts(long a, GEN p)
    1133             : {
    1134       19197 :   long v = vals(a)>>1;
    1135       19197 :   GEN r = gen_0;
    1136       19197 :   a >>= v << 1;
    1137       19197 :   switch(a)
    1138             :   {
    1139           8 :     case 1:
    1140           8 :       r = gen_1;
    1141           8 :       break;
    1142        1128 :     case -1:
    1143        1128 :       if (mod4(p)==1)
    1144        1128 :         r = Fp_pow(nonsquare_Fp(p), shifti(p,-2),p);
    1145             :       else
    1146           0 :         r = NULL;
    1147        1128 :       break;
    1148         140 :     case 2:
    1149         140 :       if (mod8(p)==1)
    1150             :       {
    1151         140 :         GEN z = Fp_pow(nonsquare_Fp(p), shifti(p,-3),p);
    1152         140 :         r = Fp_mul(z,Fp_sub(gen_1,Fp_sqr(z,p),p),p);
    1153           0 :       } else if (mod8(p)==7)
    1154           0 :         r = Fp_pow(gen_2, shifti(addiu(p,1),-2),p);
    1155             :       else
    1156           0 :         return NULL;
    1157         140 :       break;
    1158         204 :     case -2:
    1159         204 :       if (mod8(p)==1)
    1160             :       {
    1161         204 :         GEN z = Fp_pow(nonsquare_Fp(p), shifti(p,-3),p);
    1162         204 :         r = Fp_mul(z,Fp_add(gen_1,Fp_sqr(z,p),p),p);
    1163           0 :       } else if (mod8(p)==3)
    1164           0 :         r = Fp_pow(gen_m2, shifti(addiu(p,1),-2),p);
    1165             :       else
    1166           0 :         return NULL;
    1167         204 :       break;
    1168         462 :     case -3:
    1169         462 :       if (umodiu(p,3)==1)
    1170             :       {
    1171         462 :         GEN z = Fp_rootsof1(3, p);
    1172         462 :         r = Fp_sub(z,Fp_sqr(z,p),p);
    1173             :       }
    1174             :       else
    1175           0 :         return NULL;
    1176         462 :       break;
    1177        2213 :     case 5: case 13: case 17: case 21: case 29: case 33:
    1178             :     case -7: case -11: case -15: case -19: case -23:
    1179        2213 :       if (umodiu(p,labs(a))==1)
    1180         334 :         r = Fp_gausssum(a,p);
    1181             :       else
    1182        1880 :         return gen_0;
    1183         334 :       break;
    1184       15042 :     default:
    1185       15042 :       return gen_0;
    1186             :   }
    1187        2276 :   return remii(shifti(r, v), p);
    1188             : }
    1189             : 
    1190             : static GEN
    1191       77483 : Fp_sqrt_ii(GEN a, GEN y, GEN p)
    1192             : {
    1193       77483 :   pari_sp av = avma;
    1194       77483 :   GEN  q, v, w, p1 = subiu(p,1);
    1195       77483 :   long i, k, e = vali(p1), as;
    1196             : 
    1197             :   /* direct formulas more efficient */
    1198       77482 :   if (e == 0) pari_err_PRIME("Fp_sqrt [modulus]",p); /* p != 2 */
    1199       77482 :   if (e == 1)
    1200             :   {
    1201       19087 :     q = addiu(shifti(p1,-2),1); /* (p+1) / 4 */
    1202       19086 :     v = Fp_pow(a, q, p);
    1203             :     /* must check equality in case (a/p) = -1 or p not prime */
    1204       19093 :     av = avma; e = equalii(Fp_sqr(v,p), a); set_avma(av);
    1205       19093 :     return e? v: NULL;
    1206             :   }
    1207       58395 :   as = itos_or_0(a);
    1208       58396 :   if (!as) as = itos_or_0(subii(a,p));
    1209       58397 :   if (as)
    1210             :   {
    1211       19197 :     GEN res = Fp_sqrts(as, p);
    1212       19198 :     if (!res) return gc_NULL(av);
    1213       19198 :     if (signe(res)) return gerepileupto(av, res);
    1214             :   }
    1215       56122 :   if (e == 2)
    1216             :   { /* Atkin's formula */
    1217       17285 :     GEN I, a2 = shifti(a,1);
    1218       17284 :     if (cmpii(a2,p) >= 0) a2 = subii(a2,p);
    1219       17283 :     q = shifti(p1, -3); /* (p-5)/8 */
    1220       17284 :     v = Fp_pow(a2, q, p);
    1221       17287 :     I = Fp_mul(a2, Fp_sqr(v,p), p); /* I^2 = -1 */
    1222       17287 :     v = Fp_mul(a, Fp_mul(v, subiu(I,1), p), p);
    1223             :     /* must check equality in case (a/p) = -1 or p not prime */
    1224       17287 :     av = avma; e = equalii(Fp_sqr(v,p), a); set_avma(av);
    1225       17287 :     return e? v: NULL;
    1226             :   }
    1227             :   /* On average, Cipolla is better than Tonelli/Shanks if and only if
    1228             :    * e(e-1) > 8*log2(n)+20, see LNCS 2286 pp 430 [GTL] */
    1229       38837 :   if (e*(e-1) > 20 + 8 * expi(p)) return sqrt_Cipolla(a,p);
    1230             :   /* Tonelli-Shanks */
    1231       38837 :   av = avma; q = shifti(p1,-e); /* q = (p-1)/2^oo is odd */
    1232       38838 :   if (!y)
    1233             :   {
    1234        2718 :     y = Fp_2gener_all(q, p);
    1235        2718 :     if (!y) pari_err_PRIME("Fp_sqrt [modulus]",p);
    1236             :   }
    1237       38838 :   p1 = Fp_pow(a, shifti(q,-1), p); /* a ^ (q-1)/2 */
    1238       38837 :   v = Fp_mul(a, p1, p);
    1239       38838 :   w = Fp_mul(v, p1, p);
    1240       92617 :   while (!equali1(w))
    1241             :   { /* a*w = v^2, y primitive 2^e-th root of 1
    1242             :        a square --> w even power of y, hence w^(2^(e-1)) = 1 */
    1243       53822 :     p1 = Fp_sqr(w,p);
    1244      110479 :     for (k=1; !equali1(p1) && k < e; k++) p1 = Fp_sqr(p1,p);
    1245       53822 :     if (k == e) return NULL; /* p composite or (a/p) != 1 */
    1246             :     /* w ^ (2^k) = 1 --> w = y ^ (u * 2^(e-k)), u odd */
    1247       53779 :     p1 = y;
    1248       76516 :     for (i=1; i < e-k; i++) p1 = Fp_sqr(p1,p);
    1249       53779 :     y = Fp_sqr(p1, p); e = k;
    1250       53779 :     w = Fp_mul(y, w, p);
    1251       53779 :     v = Fp_mul(v, p1, p);
    1252       53779 :     if (gc_needed(av,1))
    1253             :     {
    1254           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Fp_sqrt");
    1255           0 :       gerepileall(av,3, &y,&w,&v);
    1256             :     }
    1257             :   }
    1258       38795 :   return v;
    1259             : }
    1260             : 
    1261             : /* Assume p is prime and return NULL if (a,p) = -1; y = NULL or generator
    1262             :  * of Fp^* 2-Sylow */
    1263             : GEN
    1264     4892360 : Fp_sqrt_i(GEN a, GEN y, GEN p)
    1265             : {
    1266     4892360 :   pari_sp av = avma, av2;
    1267             :   GEN q;
    1268             : 
    1269     4892360 :   if (lgefint(p) == 3)
    1270             :   {
    1271     4814772 :     ulong pp = uel(p,2), u = umodiu(a, pp);
    1272     4814783 :     if (!u) return gen_0;
    1273     3603614 :     u = Fl_sqrt(u, pp);
    1274     3603723 :     return (u == ~0UL)? NULL: utoipos(u);
    1275             :   }
    1276       77588 :   a = modii(a, p); if (!signe(a)) return gen_0;
    1277       77483 :   a = Fp_sqrt_ii(a, y, p); if (!a) return gc_NULL(av);
    1278             :   /* smallest square root */
    1279       77076 :   av2 = avma; q = subii(p, a);
    1280       77076 :   if (cmpii(a, q) > 0) a = q; else set_avma(av2);
    1281       77076 :   return gerepileuptoint(av, a);
    1282             : }
    1283             : GEN
    1284     4835341 : Fp_sqrt(GEN a, GEN p) { return Fp_sqrt_i(a, NULL, p); }
    1285             : 
    1286             : /*********************************************************************/
    1287             : /**                        GCD & BEZOUT                             **/
    1288             : /*********************************************************************/
    1289             : 
    1290             : GEN
    1291    54610564 : lcmii(GEN x, GEN y)
    1292             : {
    1293             :   pari_sp av;
    1294             :   GEN a, b;
    1295    54610564 :   if (!signe(x) || !signe(y)) return gen_0;
    1296    54610574 :   av = avma; a = gcdii(x,y);
    1297    54609337 :   if (absequalii(a,y)) { set_avma(av); return absi(x); }
    1298    12075533 :   if (!equali1(a)) y = diviiexact(y,a);
    1299    12075514 :   b = mulii(x,y); setabssign(b); return gerepileuptoint(av, b);
    1300             : }
    1301             : 
    1302             : /* given x in assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
    1303             :  * set *pd = gcd(x,N) */
    1304             : GEN
    1305     5921676 : Fp_invgen(GEN x, GEN N, GEN *pd)
    1306             : {
    1307             :   GEN d, d0, e, v;
    1308     5921676 :   if (lgefint(N) == 3)
    1309             :   {
    1310     5154050 :     ulong dd, NN = N[2], xx = umodiu(x,NN);
    1311     5154155 :     if (!xx) { *pd = N; return gen_0; }
    1312     5154155 :     xx = Fl_invgen(xx, NN, &dd);
    1313     5155476 :     *pd = utoi(dd); return utoi(xx);
    1314             :   }
    1315      767626 :   *pd = d = bezout(x, N, &v, NULL);
    1316      767640 :   if (equali1(d)) return v;
    1317             :   /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
    1318      672006 :   e = diviiexact(N,d);
    1319      672005 :   d0 = Z_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
    1320      672006 :   if (equali1(d0)) return v;
    1321      533977 :   if (!equalii(d,d0)) e = lcmii(e, diviiexact(d,d0));
    1322      533977 :   return Z_chinese_coprime(v, gen_1, e, d0, mulii(e,d0));
    1323             : }
    1324             : 
    1325             : /*********************************************************************/
    1326             : /**                      CHINESE REMAINDERS                         **/
    1327             : /*********************************************************************/
    1328             : 
    1329             : /* Chinese Remainder Theorem.  x and y must have the same type (integermod,
    1330             :  * polymod, or polynomial/vector/matrix recursively constructed with these
    1331             :  * as coefficients). Creates (with the same type) a z in the same residue
    1332             :  * class as x and the same residue class as y, if it is possible.
    1333             :  *
    1334             :  * We also allow (during recursion) two identical objects even if they are
    1335             :  * not integermod or polymod. For example:
    1336             :  *
    1337             :  * ? x = [1, Mod(5, 11), Mod(X + Mod(2, 7), X^2 + 1)];
    1338             :  * ? y = [1, Mod(7, 17), Mod(X + Mod(0, 3), X^2 + 1)];
    1339             :  * ? chinese(x, y)
    1340             :  * %3 = [1, Mod(16, 187), Mod(X + mod(9, 21), X^2 + 1)] */
    1341             : 
    1342             : static GEN
    1343     2415373 : gen_chinese(GEN x, GEN(*f)(GEN,GEN))
    1344             : {
    1345     2415373 :   GEN z = gassoc_proto(f,x,NULL);
    1346     2415366 :   if (z == gen_1) retmkintmod(gen_0,gen_1);
    1347     2415331 :   return z;
    1348             : }
    1349             : 
    1350             : /* x t_INTMOD, y t_POLMOD; promote x to t_POLMOD mod Pol(x.mod) then
    1351             :  * call chinese: makes Mod(0,1) a better "neutral" element */
    1352             : static GEN
    1353          21 : chinese_intpol(GEN x,GEN y)
    1354             : {
    1355          21 :   pari_sp av = avma;
    1356          21 :   GEN z = mkpolmod(gel(x,2), scalarpol_shallow(gel(x,1), varn(gel(y,1))));
    1357          21 :   return gerepileupto(av, chinese(z, y));
    1358             : }
    1359             : 
    1360             : GEN
    1361        2415 : chinese1(GEN x) { return gen_chinese(x,chinese); }
    1362             : 
    1363             : GEN
    1364        5495 : chinese(GEN x, GEN y)
    1365             : {
    1366             :   pari_sp av;
    1367        5495 :   long tx = typ(x), ty;
    1368             :   GEN z,p1,p2,d,u,v;
    1369             : 
    1370        5495 :   if (!y) return chinese1(x);
    1371        5446 :   if (gidentical(x,y)) return gcopy(x);
    1372        5439 :   ty = typ(y);
    1373        5439 :   if (tx == ty) switch(tx)
    1374             :   {
    1375        3892 :     case t_POLMOD:
    1376             :     {
    1377        3892 :       GEN A = gel(x,1), B = gel(y,1);
    1378        3892 :       GEN a = gel(x,2), b = gel(y,2);
    1379        3892 :       if (varn(A)!=varn(B)) pari_err_VAR("chinese",A,B);
    1380        3892 :       if (RgX_equal(A,B)) retmkpolmod(chinese(a,b), gcopy(A)); /*same modulus*/
    1381        3892 :       av = avma;
    1382        3892 :       d = RgX_extgcd(A,B,&u,&v);
    1383        3892 :       p2 = gsub(b, a);
    1384        3892 :       if (!gequal0(gmod(p2, d))) break;
    1385        3892 :       p1 = gdiv(A,d);
    1386        3892 :       p2 = gadd(a, gmul(gmul(u,p1), p2));
    1387             : 
    1388        3892 :       z = cgetg(3, t_POLMOD);
    1389        3892 :       gel(z,1) = gmul(p1,B);
    1390        3892 :       gel(z,2) = gmod(p2,gel(z,1));
    1391        3892 :       return gerepileupto(av, z);
    1392             :     }
    1393        1505 :     case t_INTMOD:
    1394             :     {
    1395        1505 :       GEN A = gel(x,1), B = gel(y,1);
    1396        1505 :       GEN a = gel(x,2), b = gel(y,2), c, d, C, U;
    1397        1505 :       z = cgetg(3,t_INTMOD);
    1398        1505 :       Z_chinese_pre(A, B, &C, &U, &d);
    1399        1505 :       c = Z_chinese_post(a, b, C, U, d);
    1400        1505 :       if (!c) pari_err_OP("chinese", x,y);
    1401        1505 :       set_avma((pari_sp)z);
    1402        1505 :       gel(z,1) = icopy(C);
    1403        1505 :       gel(z,2) = icopy(c); return z;
    1404             :     }
    1405          14 :     case t_POL:
    1406             :     {
    1407          14 :       long i, lx = lg(x), ly = lg(y);
    1408          14 :       if (varn(x) != varn(y)) break;
    1409          14 :       if (lx < ly) { swap(x,y); lswap(lx,ly); }
    1410          14 :       z = cgetg(lx, t_POL); z[1] = x[1];
    1411          42 :       for (i=2; i<ly; i++) gel(z,i) = chinese(gel(x,i),gel(y,i));
    1412          14 :       if (i < lx)
    1413             :       {
    1414          14 :         GEN _0 = Rg_get_0(y);
    1415          28 :         for (   ; i<lx; i++) gel(z,i) = chinese(gel(x,i),_0);
    1416             :       }
    1417          14 :       return z;
    1418             :     }
    1419           7 :     case t_VEC: case t_COL: case t_MAT:
    1420             :     {
    1421             :       long i, lx;
    1422           7 :       z = cgetg_copy(x, &lx); if (lx!=lg(y)) break;
    1423          21 :       for (i=1; i<lx; i++) gel(z,i) = chinese(gel(x,i),gel(y,i));
    1424           7 :       return z;
    1425             :     }
    1426             :   }
    1427          21 :   if (tx == t_POLMOD && ty == t_INTMOD) return chinese_intpol(y,x);
    1428           7 :   if (ty == t_POLMOD && tx == t_INTMOD) return chinese_intpol(x,y);
    1429           0 :   pari_err_OP("chinese",x,y);
    1430             :   return NULL; /* LCOV_EXCL_LINE */
    1431             : }
    1432             : 
    1433             : /* init chinese(Mod(.,A), Mod(.,B)) */
    1434             : void
    1435      271286 : Z_chinese_pre(GEN A, GEN B, GEN *pC, GEN *pU, GEN *pd)
    1436             : {
    1437      271286 :   GEN u, d = bezout(A,B,&u,NULL); /* U = u(A/d), u(A/d) + v(B/d) = 1 */
    1438      271293 :   GEN t = diviiexact(A,d);
    1439      271286 :   *pU = mulii(u, t);
    1440      271280 :   *pC = mulii(t, B);
    1441      271273 :   if (pd) *pd = d;
    1442      271273 : }
    1443             : /* Assume C = lcm(A, B), U = 0 mod (A/d), U = 1 mod (B/d), a = b mod d,
    1444             :  * where d = gcd(A,B) or NULL, return x = a (mod A), b (mod B).
    1445             :  * If d not NULL, check whether a = b mod d. */
    1446             : GEN
    1447     2998512 : Z_chinese_post(GEN a, GEN b, GEN C, GEN U, GEN d)
    1448             : {
    1449             :   GEN b_a;
    1450     2998512 :   if (!signe(a))
    1451             :   {
    1452      797133 :     if (d && !dvdii(b, d)) return NULL;
    1453      797133 :     return Fp_mul(b, U, C);
    1454             :   }
    1455     2201379 :   b_a = subii(b,a);
    1456     2201379 :   if (d && !dvdii(b_a, d)) return NULL;
    1457     2201379 :   return modii(addii(a, mulii(U, b_a)), C);
    1458             : }
    1459             : static ulong
    1460     1598657 : u_chinese_post(ulong a, ulong b, ulong C, ulong U)
    1461             : {
    1462     1598657 :   if (!a) return Fl_mul(b, U, C);
    1463     1598657 :   return Fl_add(a, Fl_mul(U, Fl_sub(b,a,C), C), C);
    1464             : }
    1465             : 
    1466             : GEN
    1467        2142 : Z_chinese(GEN a, GEN b, GEN A, GEN B)
    1468             : {
    1469        2142 :   pari_sp av = avma;
    1470        2142 :   GEN C, U; Z_chinese_pre(A, B, &C, &U, NULL);
    1471        2142 :   return gerepileuptoint(av, Z_chinese_post(a,b, C, U, NULL));
    1472             : }
    1473             : GEN
    1474      267582 : Z_chinese_all(GEN a, GEN b, GEN A, GEN B, GEN *pC)
    1475             : {
    1476      267582 :   GEN U; Z_chinese_pre(A, B, pC, &U, NULL);
    1477      267569 :   return Z_chinese_post(a,b, *pC, U, NULL);
    1478             : }
    1479             : 
    1480             : /* return lift(chinese(a mod A, b mod B))
    1481             :  * assume(A,B)=1, a,b,A,B integers and C = A*B */
    1482             : GEN
    1483      535237 : Z_chinese_coprime(GEN a, GEN b, GEN A, GEN B, GEN C)
    1484             : {
    1485      535237 :   pari_sp av = avma;
    1486      535237 :   GEN U = mulii(Fp_inv(A,B), A);
    1487      535237 :   return gerepileuptoint(av, Z_chinese_post(a,b,C,U, NULL));
    1488             : }
    1489             : ulong
    1490     1598639 : u_chinese_coprime(ulong a, ulong b, ulong A, ulong B, ulong C)
    1491     1598639 : { return u_chinese_post(a,b,C, A * Fl_inv(A % B,B)); }
    1492             : 
    1493             : /* chinese1 for coprime moduli in Z */
    1494             : static GEN
    1495     2191737 : chinese1_coprime_Z_aux(GEN x, GEN y)
    1496             : {
    1497     2191737 :   GEN z = cgetg(3, t_INTMOD);
    1498     2191737 :   GEN A = gel(x,1), a = gel(x, 2);
    1499     2191737 :   GEN B = gel(y,1), b = gel(y, 2), C = mulii(A,B);
    1500     2191737 :   pari_sp av = avma;
    1501     2191737 :   GEN U = mulii(Fp_inv(A,B), A);
    1502     2191737 :   gel(z,2) = gerepileuptoint(av, Z_chinese_post(a,b,C,U, NULL));
    1503     2191737 :   gel(z,1) = C; return z;
    1504             : }
    1505             : GEN
    1506     2412958 : chinese1_coprime_Z(GEN x) {return gen_chinese(x,chinese1_coprime_Z_aux);}
    1507             : 
    1508             : /*********************************************************************/
    1509             : /**                    MODULAR EXPONENTIATION                       **/
    1510             : /*********************************************************************/
    1511             : /* xa ZV or nv */
    1512             : GEN
    1513     2497544 : ZV_producttree(GEN xa)
    1514             : {
    1515     2497544 :   long n = lg(xa)-1;
    1516     2497544 :   long m = n==1 ? 1: expu(n-1)+1;
    1517     2497544 :   GEN T = cgetg(m+1, t_VEC), t;
    1518             :   long i, j, k;
    1519     2497541 :   t = cgetg(((n+1)>>1)+1, t_VEC);
    1520     2497539 :   if (typ(xa)==t_VECSMALL)
    1521             :   {
    1522     3289050 :     for (j=1, k=1; k<n; j++, k+=2)
    1523     2102416 :       gel(t, j) = muluu(xa[k], xa[k+1]);
    1524     1186634 :     if (k==n) gel(t, j) = utoi(xa[k]);
    1525             :   } else {
    1526     2715731 :     for (j=1, k=1; k<n; j++, k+=2)
    1527     1404827 :       gel(t, j) = mulii(gel(xa,k), gel(xa,k+1));
    1528     1310904 :     if (k==n) gel(t, j) = icopy(gel(xa,k));
    1529             :   }
    1530     2497537 :   gel(T,1) = t;
    1531     3978294 :   for (i=2; i<=m; i++)
    1532             :   {
    1533     1480761 :     GEN u = gel(T, i-1);
    1534     1480761 :     long n = lg(u)-1;
    1535     1480761 :     t = cgetg(((n+1)>>1)+1, t_VEC);
    1536     3306131 :     for (j=1, k=1; k<n; j++, k+=2)
    1537     1825374 :       gel(t, j) = mulii(gel(u, k), gel(u, k+1));
    1538     1480757 :     if (k==n) gel(t, j) = gel(u, k);
    1539     1480757 :     gel(T, i) = t;
    1540             :   }
    1541     2497533 :   return T;
    1542             : }
    1543             : 
    1544             : /* return [A mod P[i], i=1..#P], T = ZV_producttree(P) */
    1545             : GEN
    1546    57221226 : Z_ZV_mod_tree(GEN A, GEN P, GEN T)
    1547             : {
    1548             :   long i,j,k;
    1549    57221226 :   long m = lg(T)-1, n = lg(P)-1;
    1550             :   GEN t;
    1551    57221226 :   GEN Tp = cgetg(m+1, t_VEC);
    1552    57162109 :   gel(Tp, m) = mkvec(modii(A, gmael(T,m,1)));
    1553   119217842 :   for (i=m-1; i>=1; i--)
    1554             :   {
    1555    62114328 :     GEN u = gel(T, i);
    1556    62114328 :     GEN v = gel(Tp, i+1);
    1557    62114328 :     long n = lg(u)-1;
    1558    62114328 :     t = cgetg(n+1, t_VEC);
    1559   148304792 :     for (j=1, k=1; k<n; j++, k+=2)
    1560             :     {
    1561    86271306 :       gel(t, k)   = modii(gel(v, j), gel(u, k));
    1562    86297119 :       gel(t, k+1) = modii(gel(v, j), gel(u, k+1));
    1563             :     }
    1564    62033486 :     if (k==n) gel(t, k) = gel(v, j);
    1565    62033486 :     gel(Tp, i) = t;
    1566             :   }
    1567             :   {
    1568    57103514 :     GEN u = gel(T, i+1);
    1569    57103514 :     GEN v = gel(Tp, i+1);
    1570    57103514 :     long l = lg(u)-1;
    1571    57103514 :     if (typ(P)==t_VECSMALL)
    1572             :     {
    1573    54609115 :       GEN R = cgetg(n+1, t_VECSMALL);
    1574   194981784 :       for (j=1, k=1; j<=l; j++, k+=2)
    1575             :       {
    1576   140065328 :         uel(R,k) = umodiu(gel(v, j), P[k]);
    1577   140342791 :         if (k < n)
    1578   110373830 :           uel(R,k+1) = umodiu(gel(v, j), P[k+1]);
    1579             :       }
    1580    54916456 :       return R;
    1581             :     }
    1582             :     else
    1583             :     {
    1584     2494399 :       GEN R = cgetg(n+1, t_VEC);
    1585     6816825 :       for (j=1, k=1; j<=l; j++, k+=2)
    1586             :       {
    1587     4319640 :         gel(R,k) = modii(gel(v, j), gel(P,k));
    1588     4319651 :         if (k < n)
    1589     3504084 :           gel(R,k+1) = modii(gel(v, j), gel(P,k+1));
    1590             :       }
    1591     2497185 :       return R;
    1592             :     }
    1593             :   }
    1594             : }
    1595             : 
    1596             : /* T = ZV_producttree(P), R = ZV_chinesetree(P,T) */
    1597             : GEN
    1598    39587182 : ZV_chinese_tree(GEN A, GEN P, GEN T, GEN R)
    1599             : {
    1600    39587182 :   long m = lg(T)-1, n = lg(A)-1;
    1601             :   long i,j,k;
    1602    39587182 :   GEN Tp = cgetg(m+1, t_VEC);
    1603    39575720 :   GEN M = gel(T, 1);
    1604    39575720 :   GEN t = cgetg(lg(M), t_VEC);
    1605    39522418 :   if (typ(P)==t_VECSMALL)
    1606             :   {
    1607    83926093 :     for (j=1, k=1; k<n; j++, k+=2)
    1608             :     {
    1609    60773508 :       pari_sp av = avma;
    1610    60773508 :       GEN a = mului(A[k], gel(R,k)), b = mului(A[k+1], gel(R,k+1));
    1611    60672198 :       GEN tj = modii(addii(mului(P[k],b), mului(P[k+1],a)), gel(M,j));
    1612    60767699 :       gel(t, j) = gerepileuptoint(av, tj);
    1613             :     }
    1614    23152585 :     if (k==n) gel(t, j) = modii(mului(A[k], gel(R,k)), gel(M, j));
    1615             :   } else
    1616             :   {
    1617    34809755 :     for (j=1, k=1; k<n; j++, k+=2)
    1618             :     {
    1619    18394177 :       pari_sp av = avma;
    1620    18394177 :       GEN a = mulii(gel(A,k), gel(R,k)), b = mulii(gel(A,k+1), gel(R,k+1));
    1621    18395226 :       GEN tj = modii(addii(mulii(gel(P,k),b), mulii(gel(P,k+1),a)), gel(M,j));
    1622    18441646 :       gel(t, j) = gerepileuptoint(av, tj);
    1623             :     }
    1624    16415578 :     if (k==n) gel(t, j) = modii(mulii(gel(A,k), gel(R,k)), gel(M, j));
    1625             :   }
    1626    39563184 :   gel(Tp, 1) = t;
    1627    73929156 :   for (i=2; i<=m; i++)
    1628             :   {
    1629    34349234 :     GEN u = gel(T, i-1), M = gel(T, i);
    1630    34349234 :     GEN t = cgetg(lg(M), t_VEC);
    1631    34339786 :     GEN v = gel(Tp, i-1);
    1632    34339786 :     long n = lg(v)-1;
    1633    89827637 :     for (j=1, k=1; k<n; j++, k+=2)
    1634             :     {
    1635    55461665 :       pari_sp av = avma;
    1636    55427210 :       gel(t, j) = gerepileuptoint(av, modii(addii(mulii(gel(u, k), gel(v, k+1)),
    1637    55461665 :             mulii(gel(u, k+1), gel(v, k))), gel(M, j)));
    1638             :     }
    1639    34365972 :     if (k==n) gel(t, j) = gel(v, k);
    1640    34365972 :     gel(Tp, i) = t;
    1641             :   }
    1642    39579922 :   return gmael(Tp,m,1);
    1643             : }
    1644             : 
    1645             : static GEN
    1646     1440383 : ncV_polint_center_tree(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1647             : {
    1648     1440383 :   long i, l = lg(gel(vA,1)), n = lg(P);
    1649     1440383 :   GEN mod = gmael(T, lg(T)-1, 1), V = cgetg(l, t_COL);
    1650    33761199 :   for (i=1; i < l; i++)
    1651             :   {
    1652    32321153 :     pari_sp av = avma;
    1653    32321153 :     GEN c, A = cgetg(n, typ(P));
    1654             :     long j;
    1655   185847006 :     for (j=1; j < n; j++) A[j] = mael(vA,j,i);
    1656    32284131 :     c = Fp_center(ZV_chinese_tree(A, P, T, R), mod, m2);
    1657    32315389 :     gel(V,i) = gerepileuptoint(av, c);
    1658             :   }
    1659     1440046 :   return V;
    1660             : }
    1661             : 
    1662             : static GEN
    1663      718562 : nxV_polint_center_tree(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1664             : {
    1665      718562 :   long i, j, l, n = lg(P);
    1666      718562 :   GEN mod = gmael(T, lg(T)-1, 1), V, w;
    1667      718562 :   w = cgetg(n, t_VECSMALL);
    1668     2529625 :   for(j=1; j<n; j++) w[j] = lg(gel(vA,j));
    1669      718572 :   l = vecsmall_max(w);
    1670      718568 :   V = cgetg(l, t_POL);
    1671      718487 :   V[1] = mael(vA,1,1);
    1672     5274654 :   for (i=2; i < l; i++)
    1673             :   {
    1674     4556138 :     pari_sp av = avma;
    1675     4556138 :     GEN c, A = cgetg(n, typ(P));
    1676     4555648 :     if (typ(P)==t_VECSMALL)
    1677    12015724 :       for (j=1; j < n; j++) A[j] = i < w[j] ? mael(vA,j,i): 0;
    1678             :     else
    1679     5741190 :       for (j=1; j < n; j++) gel(A,j) = i < w[j] ? gmael(vA,j,i): gen_0;
    1680     4555648 :     c = Fp_center(ZV_chinese_tree(A, P, T, R), mod, m2);
    1681     4556207 :     gel(V,i) = gerepileuptoint(av, c);
    1682             :   }
    1683      718516 :   return ZX_renormalize(V, l);
    1684             : }
    1685             : 
    1686             : static GEN
    1687        4614 : nxCV_polint_center_tree(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1688             : {
    1689        4614 :   long i, j, l = lg(gel(vA,1)), n = lg(P);
    1690        4614 :   GEN A = cgetg(n, t_VEC);
    1691        4614 :   GEN V = cgetg(l, t_COL);
    1692       90823 :   for (i=1; i < l; i++)
    1693             :   {
    1694      334882 :     for (j=1; j < n; j++) gel(A,j) = gmael(vA,j,i);
    1695       86210 :     gel(V,i) = nxV_polint_center_tree(A, P, T, R, m2);
    1696             :   }
    1697        4613 :   return V;
    1698             : }
    1699             : 
    1700             : static GEN
    1701      372869 : polint_chinese(GEN worker, GEN mA, GEN P)
    1702             : {
    1703      372869 :   long cnt, pending, n, i, j, l = lg(gel(mA,1));
    1704             :   struct pari_mt pt;
    1705             :   GEN done, va, M, A;
    1706             :   pari_timer ti;
    1707             : 
    1708      372869 :   if (l == 1) return cgetg(1, t_MAT);
    1709      343817 :   cnt = pending = 0;
    1710      343817 :   n = lg(P);
    1711      343817 :   A = cgetg(n, t_VEC);
    1712      343817 :   va = mkvec(A);
    1713      343817 :   M = cgetg(l, t_MAT);
    1714      343817 :   if (DEBUGLEVEL>4) timer_start(&ti);
    1715      343817 :   if (DEBUGLEVEL>5) err_printf("Start parallel Chinese remainder: ");
    1716      343817 :   mt_queue_start_lim(&pt, worker, l-1);
    1717     1320681 :   for (i=1; i<l || pending; i++)
    1718             :   {
    1719             :     long workid;
    1720     3700616 :     for(j=1; j < n; j++) gel(A,j) = gmael(mA,j,i);
    1721      976865 :     mt_queue_submit(&pt, i, i<l? va: NULL);
    1722      976864 :     done = mt_queue_get(&pt, &workid, &pending);
    1723      976864 :     if (done)
    1724             :     {
    1725      937611 :       gel(M,workid) = done;
    1726      937611 :       if (DEBUGLEVEL>5) err_printf("%ld%% ",(++cnt)*100/(l-1));
    1727             :     }
    1728             :   }
    1729      343816 :   if (DEBUGLEVEL>5) err_printf("\n");
    1730      343816 :   if (DEBUGLEVEL>4) timer_printf(&ti, "nmV_chinese_center");
    1731      343816 :   mt_queue_end(&pt);
    1732      343817 :   return M;
    1733             : }
    1734             : 
    1735             : GEN
    1736         840 : nxMV_polint_center_tree_worker(GEN vA, GEN T, GEN R, GEN P, GEN m2)
    1737             : {
    1738         840 :   return nxCV_polint_center_tree(vA, P, T, R, m2);
    1739             : }
    1740             : 
    1741             : static GEN
    1742         430 : nxMV_polint_center_tree_seq(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1743             : {
    1744         430 :   long i, j, l = lg(gel(vA,1)), n = lg(P);
    1745         430 :   GEN A = cgetg(n, t_VEC);
    1746         430 :   GEN V = cgetg(l, t_MAT);
    1747        4204 :   for (i=1; i < l; i++)
    1748             :   {
    1749       15299 :     for (j=1; j < n; j++) gel(A,j) = gmael(vA,j,i);
    1750        3774 :     gel(V,i) = nxCV_polint_center_tree(A, P, T, R, m2);
    1751             :   }
    1752         430 :   return V;
    1753             : }
    1754             : 
    1755             : static GEN
    1756          90 : nxMV_polint_center_tree(GEN mA, GEN P, GEN T, GEN R, GEN m2)
    1757             : {
    1758          90 :   GEN worker = snm_closure(is_entry("_nxMV_polint_worker"), mkvec4(T, R, P, m2));
    1759          90 :   return polint_chinese(worker, mA, P);
    1760             : }
    1761             : 
    1762             : static GEN
    1763      109261 : nmV_polint_center_tree_seq(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1764             : {
    1765      109261 :   long i, j, l = lg(gel(vA,1)), n = lg(P);
    1766      109261 :   GEN A = cgetg(n, t_VEC);
    1767      109261 :   GEN V = cgetg(l, t_MAT);
    1768      597194 :   for (i=1; i < l; i++)
    1769             :   {
    1770     2746810 :     for (j=1; j < n; j++) gel(A,j) = gmael(vA,j,i);
    1771      487935 :     gel(V,i) = ncV_polint_center_tree(A, P, T, R, m2);
    1772             :   }
    1773      109259 :   return V;
    1774             : }
    1775             : 
    1776             : GEN
    1777      936720 : nmV_polint_center_tree_worker(GEN vA, GEN T, GEN R, GEN P, GEN m2)
    1778             : {
    1779      936720 :   return ncV_polint_center_tree(vA, P, T, R, m2);
    1780             : }
    1781             : 
    1782             : static GEN
    1783      372779 : nmV_polint_center_tree(GEN mA, GEN P, GEN T, GEN R, GEN m2)
    1784             : {
    1785      372779 :   GEN worker = snm_closure(is_entry("_polint_worker"), mkvec4(T, R, P, m2));
    1786      372779 :   return polint_chinese(worker, mA, P);
    1787             : }
    1788             : 
    1789             : /* return [A mod P[i], i=1..#P] */
    1790             : GEN
    1791           0 : Z_ZV_mod(GEN A, GEN P)
    1792             : {
    1793           0 :   pari_sp av = avma;
    1794           0 :   return gerepilecopy(av, Z_ZV_mod_tree(A, P, ZV_producttree(P)));
    1795             : }
    1796             : /* P a t_VECSMALL */
    1797             : GEN
    1798           0 : Z_nv_mod(GEN A, GEN P)
    1799             : {
    1800           0 :   pari_sp av = avma;
    1801           0 :   return gerepileuptoleaf(av, Z_ZV_mod_tree(A, P, ZV_producttree(P)));
    1802             : }
    1803             : /* B a ZX, T = ZV_producttree(P) */
    1804             : GEN
    1805     2382880 : ZX_nv_mod_tree(GEN B, GEN A, GEN T)
    1806             : {
    1807             :   pari_sp av;
    1808     2382880 :   long i, j, l = lg(B), n = lg(A)-1;
    1809     2382880 :   GEN V = cgetg(n+1, t_VEC);
    1810    11275095 :   for (j=1; j <= n; j++)
    1811             :   {
    1812     8892389 :     gel(V, j) = cgetg(l, t_VECSMALL);
    1813     8892222 :     mael(V, j, 1) = B[1]&VARNBITS;
    1814             :   }
    1815     2382706 :   av = avma;
    1816    15531833 :   for (i=2; i < l; i++)
    1817             :   {
    1818    13150221 :     GEN v = Z_ZV_mod_tree(gel(B, i), A, T);
    1819    86911644 :     for (j=1; j <= n; j++)
    1820    73768307 :       mael(V, j, i) = v[j];
    1821    13143337 :     set_avma(av);
    1822             :   }
    1823    11274288 :   for (j=1; j <= n; j++)
    1824     8892665 :     (void) Flx_renormalize(gel(V, j), l);
    1825     2381623 :   return V;
    1826             : }
    1827             : 
    1828             : static GEN
    1829     1189985 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
    1830             : 
    1831             : GEN
    1832       86571 : ZXX_nv_mod_tree(GEN P, GEN xa, GEN T, long w)
    1833             : {
    1834       86571 :   pari_sp av = avma;
    1835       86571 :   long i, j, l = lg(P), n = lg(xa)-1;
    1836       86571 :   GEN V = cgetg(n+1, t_VEC);
    1837      372874 :   for (j=1; j <= n; j++)
    1838             :   {
    1839      286303 :     gel(V, j) = cgetg(l, t_POL);
    1840      286303 :     mael(V, j, 1) = P[1]&VARNBITS;
    1841             :   }
    1842     1195300 :   for (i=2; i < l; i++)
    1843             :   {
    1844     1108729 :     GEN v = ZX_nv_mod_tree(to_ZX(gel(P, i), w), xa, T);
    1845     4829706 :     for (j=1; j <= n; j++)
    1846     3720977 :       gmael(V, j, i) = gel(v,j);
    1847             :   }
    1848      372874 :   for (j=1; j <= n; j++)
    1849      286303 :     (void) FlxX_renormalize(gel(V, j), l);
    1850       86571 :   return gerepilecopy(av, V);
    1851             : }
    1852             : 
    1853             : GEN
    1854        4048 : ZXC_nv_mod_tree(GEN C, GEN xa, GEN T, long w)
    1855             : {
    1856        4048 :   pari_sp av = avma;
    1857        4048 :   long i, j, l = lg(C), n = lg(xa)-1;
    1858        4048 :   GEN V = cgetg(n+1, t_VEC);
    1859       16952 :   for (j = 1; j <= n; j++)
    1860       12904 :     gel(V, j) = cgetg(l, t_COL);
    1861       85313 :   for (i = 1; i < l; i++)
    1862             :   {
    1863       81266 :     GEN v = ZX_nv_mod_tree(to_ZX(gel(C, i), w), xa, T);
    1864      359833 :     for (j = 1; j <= n; j++)
    1865      278568 :       gmael(V, j, i) = gel(v,j);
    1866             :   }
    1867        4047 :   return gerepilecopy(av, V);
    1868             : }
    1869             : 
    1870             : GEN
    1871         430 : ZXM_nv_mod_tree(GEN M, GEN xa, GEN T, long w)
    1872             : {
    1873         430 :   pari_sp av = avma;
    1874         430 :   long i, j, l = lg(M), n = lg(xa)-1;
    1875         430 :   GEN V = cgetg(n+1, t_VEC);
    1876        2083 :   for (j=1; j <= n; j++)
    1877        1653 :     gel(V, j) = cgetg(l, t_MAT);
    1878        4204 :   for (i=1; i < l; i++)
    1879             :   {
    1880        3774 :     GEN v = ZXC_nv_mod_tree(gel(M, i), xa, T, w);
    1881       15299 :     for (j=1; j <= n; j++)
    1882       11525 :       gmael(V, j, i) = gel(v,j);
    1883             :   }
    1884         430 :   return gerepilecopy(av, V);
    1885             : }
    1886             : 
    1887             : GEN
    1888     1171121 : ZV_nv_mod_tree(GEN B, GEN A, GEN T)
    1889             : {
    1890             :   pari_sp av;
    1891     1171121 :   long i, j, l = lg(B), n = lg(A)-1;
    1892     1171121 :   GEN V = cgetg(n+1, t_VEC);
    1893     5806974 :   for (j=1; j <= n; j++) gel(V, j) = cgetg(l, t_VECSMALL);
    1894     1171031 :   av = avma;
    1895    42674429 :   for (i=1; i < l; i++)
    1896             :   {
    1897    41507807 :     GEN v = Z_ZV_mod_tree(gel(B, i), A, T);
    1898   219378292 :     for (j=1; j <= n; j++) mael(V, j, i) = v[j];
    1899    41464759 :     set_avma(av);
    1900             :   }
    1901     1166622 :   return V;
    1902             : }
    1903             : 
    1904             : GEN
    1905      176828 : ZM_nv_mod_tree(GEN M, GEN xa, GEN T)
    1906             : {
    1907      176828 :   pari_sp av = avma;
    1908      176828 :   long i, j, l = lg(M), n = lg(xa)-1;
    1909      176828 :   GEN V = cgetg(n+1, t_VEC);
    1910      864978 :   for (j=1; j <= n; j++) gel(V, j) = cgetg(l, t_MAT);
    1911     1347720 :   for (i=1; i < l; i++)
    1912             :   {
    1913     1170892 :     GEN v = ZV_nv_mod_tree(gel(M, i), xa, T);
    1914     5806425 :     for (j=1; j <= n; j++) gmael(V, j, i) = gel(v,j);
    1915             :   }
    1916      176828 :   return gerepilecopy(av, V);
    1917             : }
    1918             : 
    1919             : static GEN
    1920     2493333 : ZV_sqr(GEN z)
    1921             : {
    1922     2493333 :   long i,l = lg(z);
    1923     2493333 :   GEN x = cgetg(l, t_VEC);
    1924     2493331 :   if (typ(z)==t_VECSMALL)
    1925     5832867 :     for (i=1; i<l; i++) gel(x,i) = sqru(z[i]);
    1926             :   else
    1927     4458593 :     for (i=1; i<l; i++) gel(x,i) = sqri(gel(z,i));
    1928     2493327 :   return x;
    1929             : }
    1930             : 
    1931             : static GEN
    1932    12819255 : ZT_sqr(GEN x)
    1933             : {
    1934    12819255 :   if (typ(x) == t_INT) return sqri(x);
    1935    16786941 :   pari_APPLY_type(t_VEC, ZT_sqr(gel(x,i)))
    1936             : }
    1937             : 
    1938             : static GEN
    1939     2493326 : ZV_invdivexact(GEN y, GEN x)
    1940             : {
    1941     2493326 :   long i, l = lg(y);
    1942     2493326 :   GEN z = cgetg(l,t_VEC);
    1943     2493327 :   if (typ(x)==t_VECSMALL)
    1944     5832618 :     for (i=1; i<l; i++)
    1945             :     {
    1946     4646350 :       pari_sp av = avma;
    1947     4646350 :       ulong a = Fl_inv(umodiu(diviuexact(gel(y,i),x[i]), x[i]), x[i]);
    1948     4646524 :       set_avma(av); gel(z,i) = utoi(a);
    1949             :     }
    1950             :   else
    1951     4458617 :     for (i=1; i<l; i++)
    1952     3151549 :       gel(z,i) = Fp_inv(diviiexact(gel(y,i), gel(x,i)), gel(x,i));
    1953     2493336 :   return z;
    1954             : }
    1955             : 
    1956             : /* P t_VECSMALL or t_VEC of t_INT  */
    1957             : GEN
    1958     2493322 : ZV_chinesetree(GEN P, GEN T)
    1959             : {
    1960     2493322 :   GEN T2 = ZT_sqr(T), P2 = ZV_sqr(P);
    1961     2493325 :   GEN mod = gmael(T,lg(T)-1,1);
    1962     2493325 :   return ZV_invdivexact(Z_ZV_mod_tree(mod, P2, T2), P);
    1963             : }
    1964             : 
    1965             : static GEN
    1966      957745 : gc_chinese(pari_sp av, GEN T, GEN a, GEN *pt_mod)
    1967             : {
    1968      957745 :   if (!pt_mod)
    1969       12377 :     return gerepileupto(av, a);
    1970             :   else
    1971             :   {
    1972      945368 :     GEN mod = gmael(T, lg(T)-1, 1);
    1973      945368 :     gerepileall(av, 2, &a, &mod);
    1974      945369 :     *pt_mod = mod;
    1975      945369 :     return a;
    1976             :   }
    1977             : }
    1978             : 
    1979             : GEN
    1980      154221 : ZV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    1981             : {
    1982      154221 :   pari_sp av = avma;
    1983      154221 :   GEN T = ZV_producttree(P);
    1984      154221 :   GEN R = ZV_chinesetree(P, T);
    1985      154221 :   GEN a = ZV_chinese_tree(A, P, T, R);
    1986      154221 :   GEN mod = gmael(T, lg(T)-1, 1);
    1987      154221 :   GEN ca = Fp_center(a, mod, shifti(mod,-1));
    1988      154221 :   return gc_chinese(av, T, ca, pt_mod);
    1989             : }
    1990             : 
    1991             : GEN
    1992        5141 : ZV_chinese(GEN A, GEN P, GEN *pt_mod)
    1993             : {
    1994        5141 :   pari_sp av = avma;
    1995        5141 :   GEN T = ZV_producttree(P);
    1996        5141 :   GEN R = ZV_chinesetree(P, T);
    1997        5141 :   GEN a = ZV_chinese_tree(A, P, T, R);
    1998        5141 :   return gc_chinese(av, T, a, pt_mod);
    1999             : }
    2000             : 
    2001             : GEN
    2002      217053 : nxV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2003             : {
    2004      217053 :   pari_sp av = avma;
    2005      217053 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2006      217051 :   GEN a = nxV_polint_center_tree(A, P, T, R, m2);
    2007      217053 :   return gerepileupto(av, a);
    2008             : }
    2009             : 
    2010             : GEN
    2011      415252 : nxV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2012             : {
    2013      415252 :   pari_sp av = avma;
    2014      415252 :   GEN T = ZV_producttree(P);
    2015      415252 :   GEN R = ZV_chinesetree(P, T);
    2016      415252 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2017      415251 :   GEN a = nxV_polint_center_tree(A, P, T, R, m2);
    2018      415251 :   return gc_chinese(av, T, a, pt_mod);
    2019             : }
    2020             : 
    2021             : GEN
    2022       10263 : ncV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2023             : {
    2024       10263 :   pari_sp av = avma;
    2025       10263 :   GEN T = ZV_producttree(P);
    2026       10263 :   GEN R = ZV_chinesetree(P, T);
    2027       10263 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2028       10263 :   GEN a = ncV_polint_center_tree(A, P, T, R, m2);
    2029       10263 :   return gc_chinese(av, T, a, pt_mod);
    2030             : }
    2031             : 
    2032             : GEN
    2033        5457 : ncV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2034             : {
    2035        5457 :   pari_sp av = avma;
    2036        5457 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2037        5457 :   GEN a = ncV_polint_center_tree(A, P, T, R, m2);
    2038        5457 :   return gerepileupto(av, a);
    2039             : }
    2040             : 
    2041             : GEN
    2042           0 : nmV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2043             : {
    2044           0 :   pari_sp av = avma;
    2045           0 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2046           0 :   GEN a = nmV_polint_center_tree(A, P, T, R, m2);
    2047           0 :   return gerepileupto(av, a);
    2048             : }
    2049             : 
    2050             : GEN
    2051      109261 : nmV_chinese_center_tree_seq(GEN A, GEN P, GEN T, GEN R)
    2052             : {
    2053      109261 :   pari_sp av = avma;
    2054      109261 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2055      109261 :   GEN a = nmV_polint_center_tree_seq(A, P, T, R, m2);
    2056      109259 :   return gerepileupto(av, a);
    2057             : }
    2058             : 
    2059             : GEN
    2060      372779 : nmV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2061             : {
    2062      372779 :   pari_sp av = avma;
    2063      372779 :   GEN T = ZV_producttree(P);
    2064      372779 :   GEN R = ZV_chinesetree(P, T);
    2065      372779 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2066      372779 :   GEN a = nmV_polint_center_tree(A, P, T, R, m2);
    2067      372779 :   return gc_chinese(av, T, a, pt_mod);
    2068             : }
    2069             : 
    2070             : GEN
    2071           0 : nxCV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2072             : {
    2073           0 :   pari_sp av = avma;
    2074           0 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2075           0 :   GEN a = nxCV_polint_center_tree(A, P, T, R, m2);
    2076           0 :   return gerepileupto(av, a);
    2077             : }
    2078             : 
    2079             : GEN
    2080           0 : nxCV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2081             : {
    2082           0 :   pari_sp av = avma;
    2083           0 :   GEN T = ZV_producttree(P);
    2084           0 :   GEN R = ZV_chinesetree(P, T);
    2085           0 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2086           0 :   GEN a = nxCV_polint_center_tree(A, P, T, R, m2);
    2087           0 :   return gc_chinese(av, T, a, pt_mod);
    2088             : }
    2089             : 
    2090             : GEN
    2091         430 : nxMV_chinese_center_tree_seq(GEN A, GEN P, GEN T, GEN R)
    2092             : {
    2093         430 :   pari_sp av = avma;
    2094         430 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2095         430 :   GEN a = nxMV_polint_center_tree_seq(A, P, T, R, m2);
    2096         430 :   return gerepileupto(av, a);
    2097             : }
    2098             : 
    2099             : GEN
    2100          90 : nxMV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2101             : {
    2102          90 :   pari_sp av = avma;
    2103          90 :   GEN T = ZV_producttree(P);
    2104          90 :   GEN R = ZV_chinesetree(P, T);
    2105          90 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2106          90 :   GEN a = nxMV_polint_center_tree(A, P, T, R, m2);
    2107          90 :   return gc_chinese(av, T, a, pt_mod);
    2108             : }
    2109             : 
    2110             : /**********************************************************************
    2111             :  **                    Powering  over (Z/NZ)^*, small N              **
    2112             :  **********************************************************************/
    2113             : 
    2114             : /* 2^n mod p; assume n > 1 */
    2115             : static ulong
    2116    11835563 : Fl_2powu_pre(ulong n, ulong p, ulong pi)
    2117             : {
    2118    11835563 :   ulong y = 2;
    2119    11835563 :   int j = 1+bfffo(n);
    2120             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
    2121    11835563 :   n<<=j; j = BITS_IN_LONG-j; /* first bit is now implicit */
    2122   519461239 :   for (; j; n<<=1,j--)
    2123             :   {
    2124   507638115 :     y = Fl_sqr_pre(y,p,pi);
    2125   507641374 :     if (n & HIGHBIT) y = Fl_double(y, p);
    2126             :   }
    2127    11823124 :   return y;
    2128             : }
    2129             : 
    2130             : /* 2^n mod p; assume n > 1 and !(p & HIGHMASK) */
    2131             : static ulong
    2132     4305502 : Fl_2powu(ulong n, ulong p)
    2133             : {
    2134     4305502 :   ulong y = 2;
    2135     4305502 :   int j = 1+bfffo(n);
    2136             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
    2137     4305502 :   n<<=j; j = BITS_IN_LONG-j; /* first bit is now implicit */
    2138    25452348 :   for (; j; n<<=1,j--)
    2139             :   {
    2140    21146845 :     y = (y*y) % p;
    2141    21146845 :     if (n & HIGHBIT) y = Fl_double(y, p);
    2142             :   }
    2143     4305503 :   return y;
    2144             : }
    2145             : 
    2146             : /* allow pi = 0 */
    2147             : ulong
    2148   150476929 : Fl_powu_pre(ulong x, ulong n0, ulong p, ulong pi)
    2149             : {
    2150             :   ulong y, z, n;
    2151   150476929 :   if (!pi) return Fl_powu(x, n0, p);
    2152   148037384 :   if (n0 <= 1)
    2153             :   { /* frequent special cases */
    2154    10103223 :     if (n0 == 1) return x;
    2155      105108 :     if (n0 == 0) return 1;
    2156             :   }
    2157   137934201 :   if (x <= 2)
    2158             :   {
    2159    12093516 :     if (x == 2) return Fl_2powu_pre(n0, p, pi);
    2160      257627 :     return x; /* 0 or 1 */
    2161             :   }
    2162   125840685 :   y = 1; z = x; n = n0;
    2163             :   for(;;)
    2164             :   {
    2165   645503854 :     if (n&1) y = Fl_mul_pre(y,z,p,pi);
    2166   645709048 :     n>>=1; if (!n) return y;
    2167   519472251 :     z = Fl_sqr_pre(z,p,pi);
    2168             :   }
    2169             : }
    2170             : 
    2171             : ulong
    2172   139714181 : Fl_powu(ulong x, ulong n0, ulong p)
    2173             : {
    2174             :   ulong y, z, n;
    2175   139714181 :   if (n0 <= 2)
    2176             :   { /* frequent special cases */
    2177    65821732 :     if (n0 == 2) return Fl_sqr(x,p);
    2178    32227639 :     if (n0 == 1) return x;
    2179     1914203 :     if (n0 == 0) return 1;
    2180             :   }
    2181    73863304 :   if (x <= 1) return x; /* 0 or 1 */
    2182    73429158 :   if (p & HIGHMASK)
    2183     7900383 :     return Fl_powu_pre(x, n0, p, get_Fl_red(p));
    2184    65528775 :   if (x == 2) return Fl_2powu(n0, p);
    2185    61223272 :   y = 1; z = x; n = n0;
    2186             :   for(;;)
    2187             :   {
    2188   263063384 :     if (n&1) y = (y*z) % p;
    2189   263063384 :     n>>=1; if (!n) return y;
    2190   201840112 :     z = (z*z) % p;
    2191             :   }
    2192             : }
    2193             : 
    2194             : /* Reduce data dependency to maximize internal parallelism; allow pi = 0 */
    2195             : GEN
    2196    12786854 : Fl_powers_pre(ulong x, long n, ulong p, ulong pi)
    2197             : {
    2198             :   long i, k;
    2199    12786854 :   GEN z = cgetg(n + 2, t_VECSMALL);
    2200    12783734 :   z[1] = 1; if (n == 0) return z;
    2201    12783734 :   z[2] = x;
    2202    12783734 :   if (pi)
    2203             :   {
    2204    90068185 :     for (i = 3, k=2; i <= n; i+=2, k++)
    2205             :     {
    2206    77490353 :       z[i] = Fl_sqr_pre(z[k], p, pi);
    2207    77495999 :       z[i+1] = Fl_mul_pre(z[k], z[k+1], p, pi);
    2208             :     }
    2209    12577832 :     if (i==n+1) z[i] = Fl_sqr_pre(z[k], p, pi);
    2210             :   }
    2211      212953 :   else if (p & HIGHMASK)
    2212             :   {
    2213           0 :     for (i = 3, k=2; i <= n; i+=2, k++)
    2214             :     {
    2215           0 :       z[i] = Fl_sqr(z[k], p);
    2216           0 :       z[i+1] = Fl_mul(z[k], z[k+1], p);
    2217             :     }
    2218           0 :     if (i==n+1) z[i] = Fl_sqr(z[k], p);
    2219             :   }
    2220             :   else
    2221   400498055 :     for (i = 2; i <= n; i++) z[i+1] = (z[i] * x) % p;
    2222    12791727 :   return z;
    2223             : }
    2224             : 
    2225             : GEN
    2226      296029 : Fl_powers(ulong x, long n, ulong p)
    2227             : {
    2228      296029 :   return Fl_powers_pre(x, n, p, (p & HIGHMASK)? get_Fl_red(p): 0);
    2229             : }
    2230             : 
    2231             : /**********************************************************************
    2232             :  **                    Powering  over (Z/NZ)^*, large N              **
    2233             :  **********************************************************************/
    2234             : typedef struct muldata {
    2235             :   GEN (*sqr)(void * E, GEN x);
    2236             :   GEN (*mul)(void * E, GEN x, GEN y);
    2237             :   GEN (*mul2)(void * E, GEN x);
    2238             : } muldata;
    2239             : 
    2240             : /* modified Barrett reduction with one fold */
    2241             : /* See Fast Modular Reduction, W. Hasenplaugh, G. Gaubatz, V. Gopal, ARITH 18 */
    2242             : 
    2243             : static GEN
    2244       14991 : Fp_invmBarrett(GEN p, long s)
    2245             : {
    2246       14991 :   GEN R, Q = dvmdii(int2n(3*s),p,&R);
    2247       14991 :   return mkvec2(Q,R);
    2248             : }
    2249             : 
    2250             : /* a <= (N-1)^2, 2^(2s-2) <= N < 2^(2s). Return 0 <= r < N such that
    2251             :  * a = r (mod N) */
    2252             : static GEN
    2253     9211658 : Fp_rem_mBarrett(GEN a, GEN B, long s, GEN N)
    2254             : {
    2255     9211658 :   pari_sp av = avma;
    2256     9211658 :   GEN P = gel(B, 1), Q = gel(B, 2); /* 2^(3s) = P N + Q, 0 <= Q < N */
    2257     9211658 :   long t = expi(P)+1; /* 2^(t-1) <= P < 2^t */
    2258     9211658 :   GEN u = shifti(a, -3*s), v = remi2n(a, 3*s); /* a = 2^(3s)u + v */
    2259     9211658 :   GEN A = addii(v, mulii(Q,u)); /* 0 <= A < 2^(3s+1) */
    2260     9211658 :   GEN q = shifti(mulii(shifti(A, t-3*s), P), -t); /* A/N - 4 < q <= A/N */
    2261     9211658 :   GEN r = subii(A, mulii(q, N));
    2262     9211658 :   GEN sr= subii(r,N);     /* 0 <= r < 4*N */
    2263     9211658 :   if (signe(sr)<0) return gerepileuptoint(av, r);
    2264     5028008 :   r=sr; sr = subii(r,N);  /* 0 <= r < 3*N */
    2265     5028008 :   if (signe(sr)<0) return gerepileuptoint(av, r);
    2266      186059 :   r=sr; sr = subii(r,N);  /* 0 <= r < 2*N */
    2267      186059 :   return gerepileuptoint(av, signe(sr)>=0 ? sr:r);
    2268             : }
    2269             : 
    2270             : /* Montgomery reduction */
    2271             : 
    2272             : INLINE ulong
    2273      670659 : init_montdata(GEN N) { return (ulong) -invmod2BIL(mod2BIL(N)); }
    2274             : 
    2275             : struct montred
    2276             : {
    2277             :   GEN N;
    2278             :   ulong inv;
    2279             : };
    2280             : 
    2281             : /* Montgomery reduction */
    2282             : static GEN
    2283    67916117 : _sqr_montred(void * E, GEN x)
    2284             : {
    2285    67916117 :   struct montred * D = (struct montred *) E;
    2286    67916117 :   return red_montgomery(sqri(x), D->N, D->inv);
    2287             : }
    2288             : 
    2289             : /* Montgomery reduction */
    2290             : static GEN
    2291     6957632 : _mul_montred(void * E, GEN x, GEN y)
    2292             : {
    2293     6957632 :   struct montred * D = (struct montred *) E;
    2294     6957632 :   return red_montgomery(mulii(x, y), D->N, D->inv);
    2295             : }
    2296             : 
    2297             : static GEN
    2298    11064632 : _mul2_montred(void * E, GEN x)
    2299             : {
    2300    11064632 :   struct montred * D = (struct montred *) E;
    2301    11064632 :   GEN z = shifti(_sqr_montred(E, x), 1);
    2302    11062803 :   long l = lgefint(D->N);
    2303    11700901 :   while (lgefint(z) > l) z = subii(z, D->N);
    2304    11062949 :   return z;
    2305             : }
    2306             : 
    2307             : static GEN
    2308    22955544 : _sqr_remii(void* N, GEN x)
    2309    22955544 : { return remii(sqri(x), (GEN) N); }
    2310             : 
    2311             : static GEN
    2312     1508434 : _mul_remii(void* N, GEN x, GEN y)
    2313     1508434 : { return remii(mulii(x, y), (GEN) N); }
    2314             : 
    2315             : static GEN
    2316     3183394 : _mul2_remii(void* N, GEN x)
    2317     3183394 : { return Fp_double(_sqr_remii(N, x), (GEN)N); }
    2318             : 
    2319             : struct redbarrett
    2320             : {
    2321             :   GEN iM, N;
    2322             :   long s;
    2323             : };
    2324             : 
    2325             : static GEN
    2326     8437352 : _sqr_remiibar(void *E, GEN x)
    2327             : {
    2328     8437352 :   struct redbarrett * D = (struct redbarrett *) E;
    2329     8437352 :   return Fp_rem_mBarrett(sqri(x), D->iM, D->s, D->N);
    2330             : }
    2331             : 
    2332             : static GEN
    2333      774306 : _mul_remiibar(void *E, GEN x, GEN y)
    2334             : {
    2335      774306 :   struct redbarrett * D = (struct redbarrett *) E;
    2336      774306 :   return Fp_rem_mBarrett(mulii(x, y), D->iM, D->s, D->N);
    2337             : }
    2338             : 
    2339             : static GEN
    2340     2079460 : _mul2_remiibar(void *E, GEN x)
    2341             : {
    2342     2079460 :   struct redbarrett * D = (struct redbarrett *) E;
    2343     2079460 :   return Fp_double(_sqr_remiibar(E, x), D->N);
    2344             : }
    2345             : 
    2346             : static long
    2347      865435 : Fp_select_red(GEN *y, ulong k, GEN N, long lN, muldata *D, void **pt_E)
    2348             : {
    2349      865435 :   if (lN >= Fp_POW_BARRETT_LIMIT && (k==0 || ((double)k)*expi(*y) > 2 + expi(N)))
    2350             :   {
    2351       14991 :     struct redbarrett * E = (struct redbarrett *) stack_malloc(sizeof(struct redbarrett));
    2352       14991 :     D->sqr = &_sqr_remiibar;
    2353       14991 :     D->mul = &_mul_remiibar;
    2354       14991 :     D->mul2 = &_mul2_remiibar;
    2355       14991 :     E->N = N;
    2356       14991 :     E->s = 1+(expi(N)>>1);
    2357       14991 :     E->iM = Fp_invmBarrett(N, E->s);
    2358       14991 :     *pt_E = (void*) E;
    2359       14991 :     return 0;
    2360             :   }
    2361      850444 :   else if (mod2(N) && lN < Fp_POW_REDC_LIMIT)
    2362             :   {
    2363      670654 :     struct montred * E = (struct montred *) stack_malloc(sizeof(struct montred));
    2364      670654 :     *y = remii(shifti(*y, bit_accuracy(lN)), N);
    2365      670660 :     D->sqr = &_sqr_montred;
    2366      670660 :     D->mul = &_mul_montred;
    2367      670660 :     D->mul2 = &_mul2_montred;
    2368      670660 :     E->N = N;
    2369      670660 :     E->inv = init_montdata(N);
    2370      670659 :     *pt_E = (void*) E;
    2371      670659 :     return 1;
    2372             :   }
    2373             :   else
    2374             :   {
    2375      179792 :     D->sqr = &_sqr_remii;
    2376      179792 :     D->mul = &_mul_remii;
    2377      179792 :     D->mul2 = &_mul2_remii;
    2378      179792 :     *pt_E = (void*) N;
    2379      179792 :     return 0;
    2380             :   }
    2381             : }
    2382             : 
    2383             : GEN
    2384     3059810 : Fp_powu(GEN A, ulong k, GEN N)
    2385             : {
    2386     3059810 :   long lN = lgefint(N);
    2387             :   int base_is_2, use_montgomery;
    2388             :   muldata D;
    2389             :   void *E;
    2390             :   pari_sp av;
    2391             : 
    2392     3059810 :   if (lN == 3) {
    2393     1608374 :     ulong n = uel(N,2);
    2394     1608374 :     return utoi( Fl_powu(umodiu(A, n), k, n) );
    2395             :   }
    2396     1451436 :   if (k <= 2)
    2397             :   { /* frequent special cases */
    2398      854444 :     if (k == 2) return Fp_sqr(A,N);
    2399      301598 :     if (k == 1) return A;
    2400           0 :     if (k == 0) return gen_1;
    2401             :   }
    2402      596992 :   av = avma; A = modii(A,N);
    2403      596993 :   base_is_2 = 0;
    2404      596993 :   if (lgefint(A) == 3) switch(A[2])
    2405             :   {
    2406         833 :     case 1: set_avma(av); return gen_1;
    2407       33985 :     case 2:  base_is_2 = 1; break;
    2408             :   }
    2409             : 
    2410             :   /* TODO: Move this out of here and use for general modular computations */
    2411      596160 :   use_montgomery = Fp_select_red(&A, k, N, lN, &D, &E);
    2412      596160 :   if (base_is_2)
    2413       33985 :     A = gen_powu_fold_i(A, k, E, D.sqr, D.mul2);
    2414             :   else
    2415      562175 :     A = gen_powu_i(A, k, E, D.sqr, D.mul);
    2416      596160 :   if (use_montgomery)
    2417             :   {
    2418      499947 :     A = red_montgomery(A, N, ((struct montred *) E)->inv);
    2419      499947 :     if (cmpii(A, N) >= 0) A = subii(A,N);
    2420             :   }
    2421      596160 :   return gerepileuptoint(av, A);
    2422             : }
    2423             : 
    2424             : GEN
    2425       29435 : Fp_pows(GEN A, long k, GEN N)
    2426             : {
    2427       29435 :   if (lgefint(N) == 3) {
    2428       14977 :     ulong n = N[2];
    2429       14977 :     ulong a = umodiu(A, n);
    2430       14977 :     if (k < 0) {
    2431         133 :       a = Fl_inv(a, n);
    2432         133 :       k = -k;
    2433             :     }
    2434       14977 :     return utoi( Fl_powu(a, (ulong)k, n) );
    2435             :   }
    2436       14458 :   if (k < 0) { A = Fp_inv(A, N); k = -k; };
    2437       14458 :   return Fp_powu(A, (ulong)k, N);
    2438             : }
    2439             : 
    2440             : /* A^K mod N */
    2441             : GEN
    2442    36207934 : Fp_pow(GEN A, GEN K, GEN N)
    2443             : {
    2444             :   pari_sp av;
    2445    36207934 :   long s, lN = lgefint(N), sA, sy;
    2446             :   int base_is_2, use_montgomery;
    2447             :   GEN y;
    2448             :   muldata D;
    2449             :   void *E;
    2450             : 
    2451    36207934 :   s = signe(K);
    2452    36207934 :   if (!s) return dvdii(A,N)? gen_0: gen_1;
    2453    35177198 :   if (lN == 3 && lgefint(K) == 3)
    2454             :   {
    2455    34465208 :     ulong n = N[2], a = umodiu(A, n);
    2456    34465529 :     if (s < 0) a = Fl_inv(a, n);
    2457    34465606 :     if (a <= 1) return utoi(a); /* 0 or 1 */
    2458    30925903 :     return utoi(Fl_powu(a, uel(K,2), n));
    2459             :   }
    2460             : 
    2461      711990 :   av = avma;
    2462      711990 :   if (s < 0) y = Fp_inv(A,N);
    2463             :   else
    2464             :   {
    2465      710053 :     y = modii(A,N);
    2466      710261 :     if (!signe(y)) { set_avma(av); return gen_0; }
    2467             :   }
    2468      712198 :   if (lgefint(K) == 3) return gerepileuptoint(av, Fp_powu(y, K[2], N));
    2469             : 
    2470      269481 :   base_is_2 = 0;
    2471      269481 :   sy = abscmpii(y, shifti(N,-1)) > 0;
    2472      269475 :   if (sy) y = subii(N,y);
    2473      269479 :   sA = sy && mod2(K);
    2474      269478 :   if (lgefint(y) == 3) switch(y[2])
    2475             :   {
    2476         207 :     case 1:  set_avma(av); return sA ? subis(N,1): gen_1;
    2477      145794 :     case 2:  base_is_2 = 1; break;
    2478             :   }
    2479             : 
    2480             :   /* TODO: Move this out of here and use for general modular computations */
    2481      269271 :   use_montgomery = Fp_select_red(&y, 0UL, N, lN, &D, &E);
    2482      269289 :   if (base_is_2)
    2483      145810 :     y = gen_pow_fold_i(y, K, E, D.sqr, D.mul2);
    2484             :   else
    2485      123479 :     y = gen_pow_i(y, K, E, D.sqr, D.mul);
    2486      269299 :   if (use_montgomery)
    2487             :   {
    2488      170716 :     y = red_montgomery(y, N, ((struct montred *) E)->inv);
    2489      170717 :     if (cmpii(y,N) >= 0) y = subii(y,N);
    2490             :   }
    2491      269300 :   if (sA) y = subii(N, y);
    2492      269301 :   return gerepileuptoint(av,y);
    2493             : }
    2494             : 
    2495             : static GEN
    2496    14128125 : _Fp_mul(void *E, GEN x, GEN y) { return Fp_mul(x,y,(GEN)E); }
    2497             : static GEN
    2498     8134253 : _Fp_sqr(void *E, GEN x) { return Fp_sqr(x,(GEN)E); }
    2499             : static GEN
    2500       47162 : _Fp_one(void *E) { (void) E; return gen_1; }
    2501             : 
    2502             : GEN
    2503         105 : Fp_pow_init(GEN x, GEN n, long k, GEN p)
    2504         105 : { return gen_pow_init(x, n, k, (void*)p, &_Fp_sqr, &_Fp_mul); }
    2505             : 
    2506             : GEN
    2507       43694 : Fp_pow_table(GEN R, GEN n, GEN p)
    2508       43694 : { return gen_pow_table(R, n, (void*)p, &_Fp_one, &_Fp_mul); }
    2509             : 
    2510             : GEN
    2511        5931 : Fp_powers(GEN x, long n, GEN p)
    2512             : {
    2513        5931 :   if (lgefint(p) == 3)
    2514        2463 :     return Flv_to_ZV(Fl_powers(umodiu(x, uel(p, 2)), n, uel(p, 2)));
    2515        3468 :   return gen_powers(x, n, 1, (void*)p, _Fp_sqr, _Fp_mul, _Fp_one);
    2516             : }
    2517             : 
    2518             : GEN
    2519         497 : FpV_prod(GEN V, GEN p) { return gen_product(V, (void *)p, &_Fp_mul); }
    2520             : 
    2521             : static GEN
    2522    27877108 : _Fp_pow(void *E, GEN x, GEN n) { return Fp_pow(x,n,(GEN)E); }
    2523             : static GEN
    2524         147 : _Fp_rand(void *E) { return addiu(randomi(subiu((GEN)E,1)),1); }
    2525             : 
    2526             : static GEN Fp_easylog(void *E, GEN a, GEN g, GEN ord);
    2527             : static const struct bb_group Fp_star={_Fp_mul,_Fp_pow,_Fp_rand,hash_GEN,
    2528             :                                       equalii,equali1,Fp_easylog};
    2529             : 
    2530             : static GEN
    2531      901624 : _Fp_red(void *E, GEN x) { return Fp_red(x, (GEN)E); }
    2532             : static GEN
    2533     1205873 : _Fp_add(void *E, GEN x, GEN y) { (void) E; return addii(x,y); }
    2534             : static GEN
    2535     1115040 : _Fp_neg(void *E, GEN x) { (void) E; return negi(x); }
    2536             : static GEN
    2537      585377 : _Fp_rmul(void *E, GEN x, GEN y) { (void) E; return mulii(x,y); }
    2538             : static GEN
    2539       34435 : _Fp_inv(void *E, GEN x) { return Fp_inv(x,(GEN)E); }
    2540             : static int
    2541      261718 : _Fp_equal0(GEN x) { return signe(x)==0; }
    2542             : static GEN
    2543       32894 : _Fp_s(void *E, long x) { (void) E; return stoi(x); }
    2544             : 
    2545             : static const struct bb_field Fp_field={_Fp_red,_Fp_add,_Fp_rmul,_Fp_neg,
    2546             :                                         _Fp_inv,_Fp_equal0,_Fp_s};
    2547             : 
    2548        6967 : const struct bb_field *get_Fp_field(void **E, GEN p)
    2549        6967 : { *E = (void*)p; return &Fp_field; }
    2550             : 
    2551             : /*********************************************************************/
    2552             : /**               ORDER of INTEGERMOD x  in  (Z/nZ)*                **/
    2553             : /*********************************************************************/
    2554             : ulong
    2555      482515 : Fl_order(ulong a, ulong o, ulong p)
    2556             : {
    2557      482515 :   pari_sp av = avma;
    2558             :   GEN m, P, E;
    2559             :   long i;
    2560      482515 :   if (a==1) return 1;
    2561      439601 :   if (!o) o = p-1;
    2562      439601 :   m = factoru(o);
    2563      439601 :   P = gel(m,1);
    2564      439601 :   E = gel(m,2);
    2565     1252877 :   for (i = lg(P)-1; i; i--)
    2566             :   {
    2567      813276 :     ulong j, l = P[i], e = E[i], t = o / upowuu(l,e), y = Fl_powu(a, t, p);
    2568      813276 :     if (y == 1) o = t;
    2569      773868 :     else for (j = 1; j < e; j++)
    2570             :     {
    2571      385722 :       y = Fl_powu(y, l, p);
    2572      385722 :       if (y == 1) { o = t *  upowuu(l, j); break; }
    2573             :     }
    2574             :   }
    2575      439601 :   return gc_ulong(av, o);
    2576             : }
    2577             : 
    2578             : /*Find the exact order of a assuming a^o==1*/
    2579             : GEN
    2580       75413 : Fp_order(GEN a, GEN o, GEN p) {
    2581       75413 :   if (lgefint(p) == 3 && (!o || typ(o) == t_INT))
    2582             :   {
    2583          21 :     ulong pp = p[2], oo = (o && lgefint(o)==3)? uel(o,2): pp-1;
    2584          21 :     return utoi( Fl_order(umodiu(a, pp), oo, pp) );
    2585             :   }
    2586       75392 :   return gen_order(a, o, (void*)p, &Fp_star);
    2587             : }
    2588             : GEN
    2589          70 : Fp_factored_order(GEN a, GEN o, GEN p)
    2590          70 : { return gen_factored_order(a, o, (void*)p, &Fp_star); }
    2591             : 
    2592             : /* return order of a mod p^e, e > 0, pe = p^e */
    2593             : static GEN
    2594          70 : Zp_order(GEN a, GEN p, long e, GEN pe)
    2595             : {
    2596             :   GEN ap, op;
    2597          70 :   if (absequaliu(p, 2))
    2598             :   {
    2599          56 :     if (e == 1) return gen_1;
    2600          56 :     if (e == 2) return mod4(a) == 1? gen_1: gen_2;
    2601          49 :     if (mod4(a) == 1) op = gen_1; else { op = gen_2; a = Fp_sqr(a, pe); }
    2602             :   } else {
    2603          14 :     ap = (e == 1)? a: remii(a,p);
    2604          14 :     op = Fp_order(ap, subiu(p,1), p);
    2605          14 :     if (e == 1) return op;
    2606           0 :     a = Fp_pow(a, op, pe); /* 1 mod p */
    2607             :   }
    2608          49 :   if (equali1(a)) return op;
    2609           7 :   return mulii(op, powiu(p, e - Z_pval(subiu(a,1), p)));
    2610             : }
    2611             : 
    2612             : GEN
    2613          63 : znorder(GEN x, GEN o)
    2614             : {
    2615          63 :   pari_sp av = avma;
    2616             :   GEN b, a;
    2617             : 
    2618          63 :   if (typ(x) != t_INTMOD) pari_err_TYPE("znorder [t_INTMOD expected]",x);
    2619          56 :   b = gel(x,1); a = gel(x,2);
    2620          56 :   if (!equali1(gcdii(a,b))) pari_err_COPRIME("znorder", a,b);
    2621          49 :   if (!o)
    2622             :   {
    2623          35 :     GEN fa = Z_factor(b), P = gel(fa,1), E = gel(fa,2);
    2624          35 :     long i, l = lg(P);
    2625          35 :     o = gen_1;
    2626          70 :     for (i = 1; i < l; i++)
    2627             :     {
    2628          35 :       GEN p = gel(P,i);
    2629          35 :       long e = itos(gel(E,i));
    2630             : 
    2631          35 :       if (l == 2)
    2632          35 :         o = Zp_order(a, p, e, b);
    2633             :       else {
    2634           0 :         GEN pe = powiu(p,e);
    2635           0 :         o = lcmii(o, Zp_order(remii(a,pe), p, e, pe));
    2636             :       }
    2637             :     }
    2638          35 :     return gerepileuptoint(av, o);
    2639             :   }
    2640          14 :   return Fp_order(a, o, b);
    2641             : }
    2642             : 
    2643             : /*********************************************************************/
    2644             : /**               DISCRETE LOGARITHM  in  (Z/nZ)*                   **/
    2645             : /*********************************************************************/
    2646             : static GEN
    2647       60976 : Fp_log_halfgcd(ulong bnd, GEN C, GEN g, GEN p)
    2648             : {
    2649       60976 :   pari_sp av = avma;
    2650             :   GEN h1, h2, F, G;
    2651       60976 :   if (!Fp_ratlift(g,p,C,shifti(C,-1),&h1,&h2)) return gc_NULL(av);
    2652       36774 :   if ((F = Z_issmooth_fact(h1, bnd)) && (G = Z_issmooth_fact(h2, bnd)))
    2653             :   {
    2654         126 :     GEN M = cgetg(3, t_MAT);
    2655         126 :     gel(M,1) = vecsmall_concat(gel(F, 1),gel(G, 1));
    2656         126 :     gel(M,2) = vecsmall_concat(gel(F, 2),zv_neg_inplace(gel(G, 2)));
    2657         126 :     return gerepileupto(av, M);
    2658             :   }
    2659       36648 :   return gc_NULL(av);
    2660             : }
    2661             : 
    2662             : static GEN
    2663       60976 : Fp_log_find_rel(GEN b, ulong bnd, GEN C, GEN p, GEN *g, long *e)
    2664             : {
    2665             :   GEN rel;
    2666       60976 :   do { (*e)++; *g = Fp_mul(*g, b, p); rel = Fp_log_halfgcd(bnd, C, *g, p); }
    2667       60976 :   while (!rel);
    2668         126 :   return rel;
    2669             : }
    2670             : 
    2671             : struct Fp_log_rel
    2672             : {
    2673             :   GEN rel;
    2674             :   ulong prmax;
    2675             :   long nbrel, nbmax, nbgen;
    2676             : };
    2677             : 
    2678             : static long
    2679       59731 : tr(long i) { return odd(i) ? (i+1)>>1: -(i>>1); }
    2680             : 
    2681             : static long
    2682      169813 : rt(long i) { return i>0 ? 2*i-1: -2*i; }
    2683             : 
    2684             : /* add u^e */
    2685             : static void
    2686        2163 : addifsmooth1(struct Fp_log_rel *r, GEN z, long u, long e)
    2687             : {
    2688        2163 :   pari_sp av = avma;
    2689        2163 :   long off = r->prmax+1;
    2690        2163 :   GEN F = cgetg(3, t_MAT);
    2691        2163 :   gel(F,1) = vecsmall_append(gel(z,1), off+rt(u));
    2692        2163 :   gel(F,2) = vecsmall_append(gel(z,2), e);
    2693        2163 :   gel(r->rel,++r->nbrel) = gerepileupto(av, F);
    2694        2163 : }
    2695             : 
    2696             : /* add u^-1 v^-1 */
    2697             : static void
    2698       83825 : addifsmooth2(struct Fp_log_rel *r, GEN z, long u, long v)
    2699             : {
    2700       83825 :   pari_sp av = avma;
    2701       83825 :   long off = r->prmax+1;
    2702       83825 :   GEN P = mkvecsmall2(off+rt(u),off+rt(v)), E = mkvecsmall2(-1,-1);
    2703       83825 :   GEN F = cgetg(3, t_MAT);
    2704       83825 :   gel(F,1) = vecsmall_concat(gel(z,1), P);
    2705       83825 :   gel(F,2) = vecsmall_concat(gel(z,2), E);
    2706       83825 :   gel(r->rel,++r->nbrel) = gerepileupto(av, F);
    2707       83825 : }
    2708             : 
    2709             : /* Let p=C^2+c
    2710             :  * Solve h = (C+x)*(C+a)-p = 0 [mod l]
    2711             :  * h= -c+x*(C+a)+C*a = 0  [mod l]
    2712             :  * x = (c-C*a)/(C+a) [mod l]
    2713             :  * h = -c+C*(x+a)+a*x */
    2714             : GEN
    2715       30247 : Fp_log_sieve_worker(long a, long prmax, GEN C, GEN c, GEN Ci, GEN ci, GEN pi, GEN sz)
    2716             : {
    2717       30247 :   pari_sp ltop = avma;
    2718       30247 :   long i, j, th, n = lg(pi)-1, rel = 1, ab = labs(a), ae;
    2719       30247 :   GEN sieve = zero_zv(2*ab+2)+1+ab;
    2720       30256 :   GEN L = cgetg(1+2*ab+2, t_VEC);
    2721       30236 :   pari_sp av = avma;
    2722       30236 :   GEN z, h = addis(C,a);
    2723       30242 :   if ((z = Z_issmooth_fact(h, prmax)))
    2724             :   {
    2725        2169 :     gel(L, rel++) = mkvec2(z, mkvecsmall3(1, a, -1));
    2726        2168 :     av = avma;
    2727             :   }
    2728    12457523 :   for (i=1; i<=n; i++)
    2729             :   {
    2730    12428474 :     ulong li = pi[i], s = sz[i], al = smodss(a,li);
    2731    12402497 :     ulong iv = Fl_invsafe(Fl_add(Ci[i],al,li),li);
    2732             :     long u;
    2733    12681892 :     if (!iv) continue;
    2734    12367546 :     u = Fl_add(Fl_mul(Fl_sub(ci[i],Fl_mul(Ci[i],al,li),li), iv ,li),ab%li,li)-ab;
    2735    46176756 :     for(j = u; j<=ab; j+=li) sieve[j] += s;
    2736             :   }
    2737       29049 :   if (a)
    2738             :   {
    2739       30156 :     long e = expi(mulis(C,a));
    2740       30174 :     th = e - expu(e) - 1;
    2741          54 :   } else th = -1;
    2742       30247 :   ae = a>=0 ? ab-1: ab;
    2743    15521764 :   for (j = 1-ab; j <= ae; j++)
    2744    15490942 :     if (sieve[j]>=th)
    2745             :     {
    2746      108890 :       GEN h = absi(addis(subii(mulis(C,a+j),c), a*j));
    2747      108766 :       if ((z = Z_issmooth_fact(h, prmax)))
    2748             :       {
    2749      106486 :         gel(L, rel++) = mkvec2(z, mkvecsmall3(2, a, j));
    2750      106496 :         av = avma;
    2751        2291 :       } else set_avma(av);
    2752             :     }
    2753             :   /* j = a */
    2754       30822 :   if (sieve[a]>=th)
    2755             :   {
    2756         448 :     GEN h = absi(addiu(subii(mulis(C,2*a),c), a*a));
    2757         448 :     if ((z = Z_issmooth_fact(h, prmax)))
    2758         364 :       gel(L, rel++) = mkvec2(z, mkvecsmall3(1, a, -2));
    2759             :   }
    2760       30822 :   setlg(L, rel); return gerepilecopy(ltop, L);
    2761             : }
    2762             : 
    2763             : static long
    2764          63 : Fp_log_sieve(struct Fp_log_rel *r, GEN C, GEN c, GEN Ci, GEN ci, GEN pi, GEN sz)
    2765             : {
    2766             :   struct pari_mt pt;
    2767          63 :   long i, j, nb = 0;
    2768          63 :   GEN worker = snm_closure(is_entry("_Fp_log_sieve_worker"),
    2769             :                mkvecn(7, utoi(r->prmax), C, c, Ci, ci, pi, sz));
    2770          63 :   long running, pending = 0;
    2771          63 :   GEN W = zerovec(r->nbgen);
    2772          63 :   mt_queue_start_lim(&pt, worker, r->nbgen);
    2773       30459 :   for (i = 0; (running = (i < r->nbgen)) || pending; i++)
    2774             :   {
    2775             :     GEN done;
    2776             :     long idx;
    2777       30396 :     mt_queue_submit(&pt, i, running ? mkvec(stoi(tr(i))): NULL);
    2778       30396 :     done = mt_queue_get(&pt, &idx, &pending);
    2779       30396 :     if (!done || lg(done)==1) continue;
    2780       27636 :     gel(W, idx+1) = done;
    2781       27636 :     nb += lg(done)-1;
    2782       27636 :     if (DEBUGLEVEL && (i&127)==0)
    2783           0 :       err_printf("%ld%% ",100*nb/r->nbmax);
    2784             :   }
    2785          63 :   mt_queue_end(&pt);
    2786       26362 :   for(j = 1; j <= r->nbgen && r->nbrel < r->nbmax; j++)
    2787             :   {
    2788             :     long ll, m;
    2789       26299 :     GEN L = gel(W,j);
    2790       26299 :     if (isintzero(L)) continue;
    2791       23681 :     ll = lg(L);
    2792      109669 :     for (m=1; m<ll && r->nbrel < r->nbmax ; m++)
    2793             :     {
    2794       85988 :       GEN Lm = gel(L,m), h = gel(Lm, 1), v = gel(Lm, 2);
    2795       85988 :       if (v[1] == 1)
    2796        2163 :         addifsmooth1(r, h, v[2], v[3]);
    2797             :       else
    2798       83825 :         addifsmooth2(r, h, v[2], v[3]);
    2799             :     }
    2800             :   }
    2801          63 :   return j;
    2802             : }
    2803             : 
    2804             : static GEN
    2805         837 : ECP_psi(GEN x, GEN y)
    2806             : {
    2807         837 :   long prec = realprec(x);
    2808         837 :   GEN lx = glog(x, prec), ly = glog(y, prec);
    2809         837 :   GEN u = gdiv(lx, ly);
    2810         837 :   return gpow(u, gneg(u),prec);
    2811             : }
    2812             : 
    2813             : struct computeG
    2814             : {
    2815             :   GEN C;
    2816             :   long bnd, nbi;
    2817             : };
    2818             : 
    2819             : static GEN
    2820         837 : _computeG(void *E, GEN gen)
    2821             : {
    2822         837 :   struct computeG * d = (struct computeG *) E;
    2823         837 :   GEN ps = ECP_psi(gmul(gen,d->C), stoi(d->bnd));
    2824         837 :   return gsub(gmul(gsqr(gen),ps),gmulgs(gaddgs(gen,d->nbi),3));
    2825             : }
    2826             : 
    2827             : static long
    2828          63 : compute_nbgen(GEN C, long bnd, long nbi)
    2829             : {
    2830             :   struct computeG d;
    2831          63 :   d.C = shifti(C, 1);
    2832          63 :   d.bnd = bnd;
    2833          63 :   d.nbi = nbi;
    2834          63 :   return itos(ground(zbrent((void*)&d, _computeG, gen_2, stoi(bnd), DEFAULTPREC)));
    2835             : }
    2836             : 
    2837             : static GEN
    2838        1714 : _psi(void*E, GEN y)
    2839             : {
    2840        1714 :   GEN lx = (GEN) E;
    2841        1714 :   long prec = realprec(lx);
    2842        1714 :   GEN ly = glog(y, prec);
    2843        1714 :   GEN u = gdiv(lx, ly);
    2844        1714 :   return gsub(gdiv(y ,ly), gpow(u, u, prec));
    2845             : }
    2846             : 
    2847             : static GEN
    2848          63 : opt_param(GEN x, long prec)
    2849             : {
    2850          63 :   return zbrent((void*)glog(x,prec), _psi, gen_2, x, prec);
    2851             : }
    2852             : 
    2853             : static GEN
    2854          63 : check_kernel(long nbg, long N, long prmax, GEN C, GEN M, GEN p, GEN m)
    2855             : {
    2856          63 :   pari_sp av = avma;
    2857          63 :   long lM = lg(M)-1, nbcol = lM;
    2858          63 :   long tbs = maxss(1, expu(nbg/expi(m)));
    2859             :   for (;;)
    2860          42 :   {
    2861         105 :     GEN K = FpMs_leftkernel_elt_col(M, nbcol, N, m);
    2862             :     GEN tab;
    2863         105 :     long i, f=0;
    2864         105 :     long l = lg(K), lm = lgefint(m);
    2865         105 :     GEN idx = diviiexact(subiu(p,1),m), g;
    2866             :     pari_timer ti;
    2867         105 :     if (DEBUGLEVEL) timer_start(&ti);
    2868         210 :     for(i=1; i<l; i++)
    2869         210 :       if (signe(gel(K,i)))
    2870         105 :         break;
    2871         105 :     g = Fp_pow(utoi(i), idx, p);
    2872         105 :     tab = Fp_pow_init(g, p, tbs, p);
    2873         105 :     K = FpC_Fp_mul(K, Fp_inv(gel(K,i), m), m);
    2874      121520 :     for(i=1; i<l; i++)
    2875             :     {
    2876      121415 :       GEN k = gel(K,i);
    2877      121415 :       GEN j = i<=prmax ? utoi(i): addis(C,tr(i-(prmax+1)));
    2878      121415 :       if (signe(k)==0 || !equalii(Fp_pow_table(tab, k, p), Fp_pow(j, idx, p)))
    2879       82369 :         gel(K,i) = cgetineg(lm);
    2880             :       else
    2881       39046 :         f++;
    2882             :     }
    2883         105 :     if (DEBUGLEVEL) timer_printf(&ti,"found %ld/%ld logs", f, nbg);
    2884         105 :     if(f > (nbg>>1)) return gerepileupto(av, K);
    2885       10024 :     for(i=1; i<=nbcol; i++)
    2886             :     {
    2887        9982 :       long a = 1+random_Fl(lM);
    2888        9982 :       swap(gel(M,a),gel(M,i));
    2889             :     }
    2890          42 :     if (4*nbcol>5*nbg) nbcol = nbcol*9/10;
    2891             :   }
    2892             : }
    2893             : 
    2894             : static GEN
    2895         126 : Fp_log_find_ind(GEN a, GEN K, long prmax, GEN C, GEN p, GEN m)
    2896             : {
    2897         126 :   pari_sp av=avma;
    2898         126 :   GEN aa = gen_1;
    2899         126 :   long AV = 0;
    2900             :   for(;;)
    2901           0 :   {
    2902         126 :     GEN A = Fp_log_find_rel(a, prmax, C, p, &aa, &AV);
    2903         126 :     GEN F = gel(A,1), E = gel(A,2);
    2904         126 :     GEN Ao = gen_0;
    2905         126 :     long i, l = lg(F);
    2906         779 :     for(i=1; i<l; i++)
    2907             :     {
    2908         653 :       GEN Ki = gel(K,F[i]);
    2909         653 :       if (signe(Ki)<0) break;
    2910         653 :       Ao = addii(Ao, mulis(Ki, E[i]));
    2911             :     }
    2912         126 :     if (i==l) return Fp_divu(Ao, AV, m);
    2913           0 :     aa = gerepileuptoint(av, aa);
    2914             :   }
    2915             : }
    2916             : 
    2917             : static GEN
    2918          63 : Fp_log_index(GEN a, GEN b, GEN m, GEN p)
    2919             : {
    2920          63 :   pari_sp av = avma, av2;
    2921          63 :   long i, j, nbi, nbr = 0, nbrow, nbg;
    2922             :   GEN C, c, Ci, ci, pi, pr, sz, l, Ao, Bo, K, d, p_1;
    2923             :   pari_timer ti;
    2924             :   struct Fp_log_rel r;
    2925          63 :   ulong bnds = itou(roundr_safe(opt_param(sqrti(p),DEFAULTPREC)));
    2926          63 :   ulong bnd = 4*bnds;
    2927          63 :   if (!bnds || cmpii(sqru(bnds),m)>=0) return NULL;
    2928             : 
    2929          63 :   p_1 = subiu(p,1);
    2930          63 :   if (!is_pm1(gcdii(m,diviiexact(p_1,m))))
    2931           0 :     m = diviiexact(p_1, Z_ppo(p_1, m));
    2932          63 :   pr = primes_upto_zv(bnd);
    2933          63 :   nbi = lg(pr)-1;
    2934          63 :   C = sqrtremi(p, &c);
    2935          63 :   av2 = avma;
    2936       12796 :   for (i = 1; i <= nbi; ++i)
    2937             :   {
    2938       12733 :     ulong lp = pr[i];
    2939       26894 :     while (lp <= bnd)
    2940             :     {
    2941       14161 :       nbr++;
    2942       14161 :       lp *= pr[i];
    2943             :     }
    2944             :   }
    2945          63 :   pi = cgetg(nbr+1,t_VECSMALL);
    2946          63 :   Ci = cgetg(nbr+1,t_VECSMALL);
    2947          63 :   ci = cgetg(nbr+1,t_VECSMALL);
    2948          63 :   sz = cgetg(nbr+1,t_VECSMALL);
    2949       12796 :   for (i = 1, j = 1; i <= nbi; ++i)
    2950             :   {
    2951       12733 :     ulong lp = pr[i], sp = expu(2*lp-1);
    2952       26894 :     while (lp <= bnd)
    2953             :     {
    2954       14161 :       pi[j] = lp;
    2955       14161 :       Ci[j] = umodiu(C, lp);
    2956       14161 :       ci[j] = umodiu(c, lp);
    2957       14161 :       sz[j] = sp;
    2958       14161 :       lp *= pr[i];
    2959       14161 :       j++;
    2960             :     }
    2961             :   }
    2962          63 :   r.nbrel = 0;
    2963          63 :   r.nbgen = compute_nbgen(C, bnd, nbi);
    2964          63 :   r.nbmax = 2*(nbi+r.nbgen);
    2965          63 :   r.rel = cgetg(r.nbmax+1,t_VEC);
    2966          63 :   r.prmax = pr[nbi];
    2967          63 :   if (DEBUGLEVEL)
    2968             :   {
    2969           0 :     err_printf("bnd=%lu Size FB=%ld extra gen=%ld \n", bnd, nbi, r.nbgen);
    2970           0 :     timer_start(&ti);
    2971             :   }
    2972          63 :   nbg = Fp_log_sieve(&r, C, c, Ci, ci, pi, sz);
    2973          63 :   nbrow = r.prmax + nbg;
    2974          63 :   if (DEBUGLEVEL)
    2975             :   {
    2976           0 :     err_printf("\n");
    2977           0 :     timer_printf(&ti," %ld relations, %ld generators", r.nbrel, nbi+nbg);
    2978             :   }
    2979          63 :   setlg(r.rel,r.nbrel+1);
    2980          63 :   r.rel = gerepilecopy(av2, r.rel);
    2981          63 :   K = check_kernel(nbi+nbrow-r.prmax, nbrow, r.prmax, C, r.rel, p, m);
    2982          63 :   if (DEBUGLEVEL) timer_start(&ti);
    2983          63 :   Ao = Fp_log_find_ind(a, K, r.prmax, C, p, m);
    2984          63 :   if (DEBUGLEVEL) timer_printf(&ti," log element");
    2985          63 :   Bo = Fp_log_find_ind(b, K, r.prmax, C, p, m);
    2986          63 :   if (DEBUGLEVEL) timer_printf(&ti," log generator");
    2987          63 :   d = gcdii(Ao,Bo);
    2988          63 :   l = Fp_div(diviiexact(Ao, d) ,diviiexact(Bo, d), m);
    2989          63 :   if (!equalii(a,Fp_pow(b,l,p))) pari_err_BUG("Fp_log_index");
    2990          63 :   return gerepileuptoint(av, l);
    2991             : }
    2992             : 
    2993             : static int
    2994     4663041 : Fp_log_use_index(long e, long p)
    2995             : {
    2996     4663041 :   return (e >= 27 && 20*(p+6)<=e*e);
    2997             : }
    2998             : 
    2999             : /* Trivial cases a = 1, -1. Return x s.t. g^x = a or [] if no such x exist */
    3000             : static GEN
    3001     8463171 : Fp_easylog(void *E, GEN a, GEN g, GEN ord)
    3002             : {
    3003     8463171 :   pari_sp av = avma;
    3004     8463171 :   GEN p = (GEN)E;
    3005             :   /* assume a reduced mod p, p not necessarily prime */
    3006     8463171 :   if (equali1(a)) return gen_0;
    3007             :   /* p > 2 */
    3008     5439748 :   if (equalii(subiu(p,1), a))  /* -1 */
    3009             :   {
    3010             :     pari_sp av2;
    3011             :     GEN t;
    3012     1323166 :     ord = get_arith_Z(ord);
    3013     1323166 :     if (mpodd(ord)) { set_avma(av); return cgetg(1, t_VEC); } /* no solution */
    3014     1323152 :     t = shifti(ord,-1); /* only possible solution */
    3015     1323152 :     av2 = avma;
    3016     1323152 :     if (!equalii(Fp_pow(g, t, p), a)) { set_avma(av); return cgetg(1, t_VEC); }
    3017     1323124 :     set_avma(av2); return gerepileuptoint(av, t);
    3018             :   }
    3019     4116585 :   if (typ(ord)==t_INT && BPSW_psp(p) && Fp_log_use_index(expi(ord),expi(p)))
    3020          63 :     return Fp_log_index(a, g, ord, p);
    3021     4116522 :   return gc_NULL(av); /* not easy */
    3022             : }
    3023             : 
    3024             : GEN
    3025     3925731 : Fp_log(GEN a, GEN g, GEN ord, GEN p)
    3026             : {
    3027     3925731 :   GEN v = get_arith_ZZM(ord);
    3028     3925702 :   GEN F = gmael(v,2,1);
    3029     3925702 :   long lF = lg(F)-1, lmax;
    3030     3925702 :   if (lF == 0) return equali1(a)? gen_0: cgetg(1, t_VEC);
    3031     3925674 :   lmax = expi(gel(F,lF));
    3032     3925672 :   if (BPSW_psp(p) && Fp_log_use_index(lmax,expi(p)))
    3033          91 :     v = mkvec2(gel(v,1),ZM_famat_limit(gel(v,2),int2n(27)));
    3034     3925667 :   return gen_PH_log(a,g,v,(void*)p,&Fp_star);
    3035             : }
    3036             : 
    3037             : /* assume !(p & HIGHMASK) */
    3038             : static ulong
    3039      132629 : Fl_log_naive(ulong a, ulong g, ulong ord, ulong p)
    3040             : {
    3041      132629 :   ulong i, h=1;
    3042      364884 :   for (i = 0; i < ord; i++, h = (h * g) % p)
    3043      364884 :     if (a==h) return i;
    3044           0 :   return ~0UL;
    3045             : }
    3046             : 
    3047             : static ulong
    3048       25107 : Fl_log_naive_pre(ulong a, ulong g, ulong ord, ulong p, ulong pi)
    3049             : {
    3050       25107 :   ulong i, h=1;
    3051       64357 :   for (i = 0; i < ord; i++, h = Fl_mul_pre(h, g, p, pi))
    3052       64357 :     if (a==h) return i;
    3053           0 :   return ~0UL;
    3054             : }
    3055             : 
    3056             : static ulong
    3057           0 : Fl_log_Fp(ulong a, ulong g, ulong ord, ulong p)
    3058             : {
    3059           0 :   pari_sp av = avma;
    3060           0 :   GEN r = Fp_log(utoi(a),utoi(g),utoi(ord),utoi(p));
    3061           0 :   return gc_ulong(av, typ(r)==t_INT ? itou(r): ~0UL);
    3062             : }
    3063             : 
    3064             : /* allow pi = 0 */
    3065             : ulong
    3066       25506 : Fl_log_pre(ulong a, ulong g, ulong ord, ulong p, ulong pi)
    3067             : {
    3068       25506 :   if (!pi) return Fl_log(a, g, ord, p);
    3069       25107 :   if (ord <= 200) return Fl_log_naive_pre(a, g, ord, p, pi);
    3070           0 :   return Fl_log_Fp(a, g, ord, p);
    3071             : }
    3072             : 
    3073             : ulong
    3074      132629 : Fl_log(ulong a, ulong g, ulong ord, ulong p)
    3075             : {
    3076      132629 :   if (ord <= 200)
    3077           0 :     return (p&HIGHMASK)? Fl_log_naive_pre(a, g, ord, p, get_Fl_red(p))
    3078      132629 :                        : Fl_log_naive(a, g, ord, p);
    3079           0 :   return Fl_log_Fp(a, g, ord, p);
    3080             : }
    3081             : 
    3082             : /* find x such that h = g^x mod N > 1, N = prod_{i <= l} P[i]^E[i], P[i] prime.
    3083             :  * PHI[l] = eulerphi(N / P[l]^E[l]).   Destroys P/E */
    3084             : static GEN
    3085         126 : znlog_rec(GEN h, GEN g, GEN N, GEN P, GEN E, GEN PHI)
    3086             : {
    3087         126 :   long l = lg(P) - 1, e = E[l];
    3088         126 :   GEN p = gel(P, l), phi = gel(PHI,l), pe = e == 1? p: powiu(p, e);
    3089             :   GEN a,b, hp,gp, hpe,gpe, ogpe; /* = order(g mod p^e) | p^(e-1)(p-1) */
    3090             : 
    3091         126 :   if (l == 1) {
    3092          98 :     hpe = h;
    3093          98 :     gpe = g;
    3094             :   } else {
    3095          28 :     hpe = modii(h, pe);
    3096          28 :     gpe = modii(g, pe);
    3097             :   }
    3098         126 :   if (e == 1) {
    3099          42 :     hp = hpe;
    3100          42 :     gp = gpe;
    3101             :   } else {
    3102          84 :     hp = remii(hpe, p);
    3103          84 :     gp = remii(gpe, p);
    3104             :   }
    3105         126 :   if (hp == gen_0 || gp == gen_0) return NULL;
    3106         105 :   if (absequaliu(p, 2))
    3107             :   {
    3108          35 :     GEN n = int2n(e);
    3109          35 :     ogpe = Zp_order(gpe, gen_2, e, n);
    3110          35 :     a = Fp_log(hpe, gpe, ogpe, n);
    3111          35 :     if (typ(a) != t_INT) return NULL;
    3112             :   }
    3113             :   else
    3114             :   { /* Avoid black box groups: (Z/p^2)^* / (Z/p)^* ~ (Z/pZ, +), where DL
    3115             :        is trivial */
    3116             :     /* [order(gp), factor(order(gp))] */
    3117          70 :     GEN v = Fp_factored_order(gp, subiu(p,1), p);
    3118          70 :     GEN ogp = gel(v,1);
    3119          70 :     if (!equali1(Fp_pow(hp, ogp, p))) return NULL;
    3120          70 :     a = Fp_log(hp, gp, v, p);
    3121          70 :     if (typ(a) != t_INT) return NULL;
    3122          70 :     if (e == 1) ogpe = ogp;
    3123             :     else
    3124             :     { /* find a s.t. g^a = h (mod p^e), p odd prime, e > 0, (h,p) = 1 */
    3125             :       /* use p-adic log: O(log p + e) mul*/
    3126             :       long vpogpe, vpohpe;
    3127             : 
    3128          28 :       hpe = Fp_mul(hpe, Fp_pow(gpe, negi(a), pe), pe);
    3129          28 :       gpe = Fp_pow(gpe, ogp, pe);
    3130             :       /* g,h = 1 mod p; compute b s.t. h = g^b */
    3131             : 
    3132             :       /* v_p(order g mod pe) */
    3133          28 :       vpogpe = equali1(gpe)? 0: e - Z_pval(subiu(gpe,1), p);
    3134             :       /* v_p(order h mod pe) */
    3135          28 :       vpohpe = equali1(hpe)? 0: e - Z_pval(subiu(hpe,1), p);
    3136          28 :       if (vpohpe > vpogpe) return NULL;
    3137             : 
    3138          28 :       ogpe = mulii(ogp, powiu(p, vpogpe)); /* order g mod p^e */
    3139          28 :       if (is_pm1(gpe)) return is_pm1(hpe)? a: NULL;
    3140          28 :       b = gdiv(Qp_log(cvtop(hpe, p, e)), Qp_log(cvtop(gpe, p, e)));
    3141          28 :       a = addii(a, mulii(ogp, padic_to_Q(b)));
    3142             :     }
    3143             :   }
    3144             :   /* gp^a = hp => x = a mod ogpe => generalized Pohlig-Hellman strategy */
    3145          91 :   if (l == 1) return a;
    3146             : 
    3147          28 :   N = diviiexact(N, pe); /* make N coprime to p */
    3148          28 :   h = Fp_mul(h, Fp_pow(g, modii(negi(a), phi), N), N);
    3149          28 :   g = Fp_pow(g, modii(ogpe, phi), N);
    3150          28 :   setlg(P, l); /* remove last element */
    3151          28 :   setlg(E, l);
    3152          28 :   b = znlog_rec(h, g, N, P, E, PHI);
    3153          28 :   if (!b) return NULL;
    3154          28 :   return addmulii(a, b, ogpe);
    3155             : }
    3156             : 
    3157             : static GEN
    3158          98 : get_PHI(GEN P, GEN E)
    3159             : {
    3160          98 :   long i, l = lg(P);
    3161          98 :   GEN PHI = cgetg(l, t_VEC);
    3162          98 :   gel(PHI,1) = gen_1;
    3163         126 :   for (i=1; i<l-1; i++)
    3164             :   {
    3165          28 :     GEN t, p = gel(P,i);
    3166          28 :     long e = E[i];
    3167          28 :     t = mulii(powiu(p, e-1), subiu(p,1));
    3168          28 :     if (i > 1) t = mulii(t, gel(PHI,i));
    3169          28 :     gel(PHI,i+1) = t;
    3170             :   }
    3171          98 :   return PHI;
    3172             : }
    3173             : 
    3174             : GEN
    3175         238 : znlog(GEN h, GEN g, GEN o)
    3176             : {
    3177         238 :   pari_sp av = avma;
    3178             :   GEN N, fa, P, E, x;
    3179         238 :   switch (typ(g))
    3180             :   {
    3181          28 :     case t_PADIC:
    3182             :     {
    3183          28 :       GEN p = gel(g,2);
    3184          28 :       long v = valp(g);
    3185          28 :       if (v < 0) pari_err_DIM("znlog");
    3186          28 :       if (v > 0) {
    3187           0 :         long k = gvaluation(h, p);
    3188           0 :         if (k % v) return cgetg(1,t_VEC);
    3189           0 :         k /= v;
    3190           0 :         if (!gequal(h, gpowgs(g,k))) { set_avma(av); return cgetg(1,t_VEC); }
    3191           0 :         return gc_stoi(av, k);
    3192             :       }
    3193          28 :       N = gel(g,3);
    3194          28 :       g = Rg_to_Fp(g, N);
    3195          28 :       break;
    3196             :     }
    3197         203 :     case t_INTMOD:
    3198         203 :       N = gel(g,1);
    3199         203 :       g = gel(g,2); break;
    3200           7 :     default: pari_err_TYPE("znlog", g);
    3201             :       return NULL; /* LCOV_EXCL_LINE */
    3202             :   }
    3203         231 :   if (equali1(N)) { set_avma(av); return gen_0; }
    3204         231 :   h = Rg_to_Fp(h, N);
    3205         224 :   if (o) return gerepileupto(av, Fp_log(h, g, o, N));
    3206          98 :   fa = Z_factor(N);
    3207          98 :   P = gel(fa,1);
    3208          98 :   E = vec_to_vecsmall(gel(fa,2));
    3209          98 :   x = znlog_rec(h, g, N, P, E, get_PHI(P,E));
    3210          98 :   if (!x) { set_avma(av); return cgetg(1,t_VEC); }
    3211          63 :   return gerepileuptoint(av, x);
    3212             : }
    3213             : 
    3214             : GEN
    3215      173327 : Fp_sqrtn(GEN a, GEN n, GEN p, GEN *zeta)
    3216             : {
    3217      173327 :   if (lgefint(p)==3)
    3218             :   {
    3219      172893 :     long nn = itos_or_0(n);
    3220      172893 :     if (nn)
    3221             :     {
    3222      172893 :       ulong pp = p[2];
    3223             :       ulong uz;
    3224      172893 :       ulong r = Fl_sqrtn(umodiu(a,pp),nn,pp, zeta ? &uz:NULL);
    3225      172872 :       if (r==ULONG_MAX) return NULL;
    3226      172816 :       if (zeta) *zeta = utoi(uz);
    3227      172816 :       return utoi(r);
    3228             :     }
    3229             :   }
    3230         434 :   a = modii(a,p);
    3231         434 :   if (!signe(a))
    3232             :   {
    3233           0 :     if (zeta) *zeta = gen_1;
    3234           0 :     if (signe(n) < 0) pari_err_INV("Fp_sqrtn", mkintmod(gen_0,p));
    3235           0 :     return gen_0;
    3236             :   }
    3237         434 :   if (absequaliu(n,2))
    3238             :   {
    3239         238 :     if (zeta) *zeta = subiu(p,1);
    3240         238 :     return signe(n) > 0 ? Fp_sqrt(a,p): Fp_sqrt(Fp_inv(a, p),p);
    3241             :   }
    3242         196 :   return gen_Shanks_sqrtn(a,n,subiu(p,1),zeta,(void*)p,&Fp_star);
    3243             : }
    3244             : 
    3245             : /*********************************************************************/
    3246             : /**                              FACTORIAL                          **/
    3247             : /*********************************************************************/
    3248             : GEN
    3249       90649 : mulu_interval_step(ulong a, ulong b, ulong step)
    3250             : {
    3251       90649 :   pari_sp av = avma;
    3252             :   ulong k, l, N, n;
    3253             :   long lx;
    3254             :   GEN x;
    3255             : 
    3256       90649 :   if (!a) return gen_0;
    3257       90649 :   if (step == 1) return mulu_interval(a, b);
    3258       90649 :   n = 1 + (b-a) / step;
    3259       90649 :   b -= (b-a) % step;
    3260       90649 :   if (n < 61)
    3261             :   {
    3262       89266 :     if (n == 1) return utoipos(a);
    3263       68720 :     x = muluu(a,a+step); if (n == 2) return x;
    3264      539172 :     for (k=a+2*step; k<=b; k+=step) x = mului(k,x);
    3265       53912 :     return gerepileuptoint(av, x);
    3266             :   }
    3267             :   /* step | b-a */
    3268        1383 :   lx = 1; x = cgetg(2 + n/2, t_VEC);
    3269        1384 :   N = b + a;
    3270        1384 :   for (k = a;; k += step)
    3271             :   {
    3272      227443 :     l = N - k; if (l <= k) break;
    3273      226059 :     gel(x,lx++) = muluu(k,l);
    3274             :   }
    3275        1384 :   if (l == k) gel(x,lx++) = utoipos(k);
    3276        1384 :   setlg(x, lx);
    3277        1384 :   return gerepileuptoint(av, ZV_prod(x));
    3278             : }
    3279             : /* return a * (a+1) * ... * b. Assume a <= b  [ note: factoring out powers of 2
    3280             :  * first is slower ... ] */
    3281             : GEN
    3282      158925 : mulu_interval(ulong a, ulong b)
    3283             : {
    3284      158925 :   pari_sp av = avma;
    3285             :   ulong k, l, N, n;
    3286             :   long lx;
    3287             :   GEN x;
    3288             : 
    3289      158925 :   if (!a) return gen_0;
    3290      158925 :   n = b - a + 1;
    3291      158925 :   if (n < 61)
    3292             :   {
    3293      158195 :     if (n == 1) return utoipos(a);
    3294      107879 :     x = muluu(a,a+1); if (n == 2) return x;
    3295      403414 :     for (k=a+2; k<b; k++) x = mului(k,x);
    3296             :     /* avoid k <= b: broken if b = ULONG_MAX */
    3297       93788 :     return gerepileuptoint(av, mului(b,x));
    3298             :   }
    3299         730 :   lx = 1; x = cgetg(2 + n/2, t_VEC);
    3300         732 :   N = b + a;
    3301         732 :   for (k = a;; k++)
    3302             :   {
    3303       27312 :     l = N - k; if (l <= k) break;
    3304       26586 :     gel(x,lx++) = muluu(k,l);
    3305             :   }
    3306         726 :   if (l == k) gel(x,lx++) = utoipos(k);
    3307         726 :   setlg(x, lx);
    3308         726 :   return gerepileuptoint(av, ZV_prod(x));
    3309             : }
    3310             : GEN
    3311         560 : muls_interval(long a, long b)
    3312             : {
    3313         560 :   pari_sp av = avma;
    3314         560 :   long lx, k, l, N, n = b - a + 1;
    3315             :   GEN x;
    3316             : 
    3317         560 :   if (a <= 0 && b >= 0) return gen_0;
    3318         287 :   if (n < 61)
    3319             :   {
    3320         287 :     x = stoi(a);
    3321         511 :     for (k=a+1; k<=b; k++) x = mulsi(k,x);
    3322         287 :     return gerepileuptoint(av, x);
    3323             :   }
    3324           0 :   lx = 1; x = cgetg(2 + n/2, t_VEC);
    3325           0 :   N = b + a;
    3326           0 :   for (k = a;; k++)
    3327             :   {
    3328           0 :     l = N - k; if (l <= k) break;
    3329           0 :     gel(x,lx++) = mulss(k,l);
    3330             :   }
    3331           0 :   if (l == k) gel(x,lx++) = stoi(k);
    3332           0 :   setlg(x, lx);
    3333           0 :   return gerepileuptoint(av, ZV_prod(x));
    3334             : }
    3335             : 
    3336             : GEN
    3337         105 : mpprimorial(long n)
    3338             : {
    3339         105 :   pari_sp av = avma;
    3340         105 :   if (n <= 12) switch(n)
    3341             :   {
    3342          14 :     case 0: case 1: return gen_1;
    3343           7 :     case 2: return gen_2;
    3344          14 :     case 3: case 4: return utoipos(6);
    3345          14 :     case 5: case 6: return utoipos(30);
    3346          28 :     case 7: case 8: case 9: case 10: return utoipos(210);
    3347          14 :     case 11: case 12: return utoipos(2310);
    3348           7 :     default: pari_err_DOMAIN("primorial", "argument","<",gen_0,stoi(n));
    3349             :   }
    3350           7 :   return gerepileuptoint(av, zv_prod_Z(primes_upto_zv(n)));
    3351             : }
    3352             : 
    3353             : GEN
    3354      496549 : mpfact(long n)
    3355             : {
    3356      496549 :   pari_sp av = avma;
    3357             :   GEN a, v;
    3358             :   long k;
    3359      496549 :   if (n <= 12) switch(n)
    3360             :   {
    3361      428640 :     case 0: case 1: return gen_1;
    3362       24338 :     case 2: return gen_2;
    3363        3388 :     case 3: return utoipos(6);
    3364        4145 :     case 4: return utoipos(24);
    3365        2887 :     case 5: return utoipos(120);
    3366        2542 :     case 6: return utoipos(720);
    3367        2448 :     case 7: return utoipos(5040);
    3368        2437 :     case 8: return utoipos(40320);
    3369        2458 :     case 9: return utoipos(362880);
    3370        2694 :     case 10:return utoipos(3628800);
    3371        1409 :     case 11:return utoipos(39916800);
    3372         577 :     case 12:return utoipos(479001600);
    3373           0 :     default: pari_err_DOMAIN("factorial", "argument","<",gen_0,stoi(n));
    3374             :   }
    3375       18586 :   v = cgetg(expu(n) + 2, t_VEC);
    3376       18574 :   for (k = 1;; k++)
    3377       86856 :   {
    3378      105430 :     long m = n >> (k-1), l;
    3379      105430 :     if (m <= 2) break;
    3380       86842 :     l = (1 + (n >> k)) | 1;
    3381             :     /* product of odd numbers in ]n / 2^k, n / 2^(k-1)] */
    3382       86842 :     a = mulu_interval_step(l, m, 2);
    3383       86807 :     gel(v,k) = k == 1? a: powiu(a, k);
    3384             :   }
    3385       86869 :   a = gel(v,--k); while (--k) a = mulii(a, gel(v,k));
    3386       18589 :   a = shifti(a, factorial_lval(n, 2));
    3387       18584 :   return gerepileuptoint(av, a);
    3388             : }
    3389             : 
    3390             : ulong
    3391       56629 : factorial_Fl(long n, ulong p)
    3392             : {
    3393             :   long k;
    3394             :   ulong v;
    3395       56629 :   if (p <= (ulong)n) return 0;
    3396       56629 :   v = Fl_powu(2, factorial_lval(n, 2), p);
    3397       56700 :   for (k = 1;; k++)
    3398      142174 :   {
    3399      198874 :     long m = n >> (k-1), l, i;
    3400      198874 :     ulong a = 1;
    3401      198874 :     if (m <= 2) break;
    3402      142201 :     l = (1 + (n >> k)) | 1;
    3403             :     /* product of odd numbers in ]n / 2^k, 2 / 2^(k-1)] */
    3404      778368 :     for (i=l; i<=m; i+=2)
    3405      636174 :       a = Fl_mul(a, i, p);
    3406      142194 :     v = Fl_mul(v, k == 1? a: Fl_powu(a, k, p), p);
    3407             :   }
    3408       56673 :   return v;
    3409             : }
    3410             : 
    3411             : GEN
    3412         158 : factorial_Fp(long n, GEN p)
    3413             : {
    3414         158 :   pari_sp av = avma;
    3415             :   long k;
    3416         158 :   GEN v = Fp_powu(gen_2, factorial_lval(n, 2), p);
    3417         158 :   for (k = 1;; k++)
    3418         344 :   {
    3419         502 :     long m = n >> (k-1), l, i;
    3420         502 :     GEN a = gen_1;
    3421         502 :     if (m <= 2) break;
    3422         344 :     l = (1 + (n >> k)) | 1;
    3423             :     /* product of odd numbers in ]n / 2^k, 2 / 2^(k-1)] */
    3424         850 :     for (i=l; i<=m; i+=2)
    3425         506 :       a = Fp_mulu(a, i, p);
    3426         344 :     v = Fp_mul(v, k == 1? a: Fp_powu(a, k, p), p);
    3427         344 :     v = gerepileuptoint(av, v);
    3428             :   }
    3429         158 :   return v;
    3430             : }
    3431             : 
    3432             : /*******************************************************************/
    3433             : /**                      LUCAS & FIBONACCI                        **/
    3434             : /*******************************************************************/
    3435             : static void
    3436          56 : lucas(ulong n, GEN *a, GEN *b)
    3437             : {
    3438             :   GEN z, t, zt;
    3439          56 :   if (!n) { *a = gen_2; *b = gen_1; return; }
    3440          49 :   lucas(n >> 1, &z, &t); zt = mulii(z, t);
    3441          49 :   switch(n & 3) {
    3442          14 :     case  0: *a = subiu(sqri(z),2); *b = subiu(zt,1); break;
    3443          14 :     case  1: *a = subiu(zt,1);      *b = addiu(sqri(t),2); break;
    3444           7 :     case  2: *a = addiu(sqri(z),2); *b = addiu(zt,1); break;
    3445          14 :     case  3: *a = addiu(zt,1);      *b = subiu(sqri(t),2);
    3446             :   }
    3447             : }
    3448             : 
    3449             : GEN
    3450           7 : fibo(long n)
    3451             : {
    3452           7 :   pari_sp av = avma;
    3453             :   GEN a, b;
    3454           7 :   if (!n) return gen_0;
    3455           7 :   lucas((ulong)(labs(n)-1), &a, &b);
    3456           7 :   a = diviuexact(addii(shifti(a,1),b), 5);
    3457           7 :   if (n < 0 && !odd(n)) setsigne(a, -1);
    3458           7 :   return gerepileuptoint(av, a);
    3459             : }
    3460             : 
    3461             : /*******************************************************************/
    3462             : /*                      CONTINUED FRACTIONS                        */
    3463             : /*******************************************************************/
    3464             : static GEN
    3465     3136994 : icopy_lg(GEN x, long l)
    3466             : {
    3467     3136994 :   long lx = lgefint(x);
    3468             :   GEN y;
    3469             : 
    3470     3136994 :   if (lx >= l) return icopy(x);
    3471          49 :   y = cgeti(l); affii(x, y); return y;
    3472             : }
    3473             : 
    3474             : /* continued fraction of a/b. If y != NULL, stop when partial quotients
    3475             :  * differ from y */
    3476             : static GEN
    3477     3137344 : Qsfcont(GEN a, GEN b, GEN y, ulong k)
    3478             : {
    3479             :   GEN  z, c;
    3480     3137344 :   ulong i, l, ly = lgefint(b);
    3481             : 
    3482             :   /* times 1 / log2( (1+sqrt(5)) / 2 )  */
    3483     3137344 :   l = (ulong)(3 + bit_accuracy_mul(ly, 1.44042009041256));
    3484     3137344 :   if (k > 0 && k+1 > 0 && l > k+1) l = k+1; /* beware overflow */
    3485     3137344 :   if (l > LGBITS) l = LGBITS;
    3486             : 
    3487     3137344 :   z = cgetg(l,t_VEC);
    3488     3137344 :   l--;
    3489     3137344 :   if (y) {
    3490         350 :     pari_sp av = avma;
    3491         350 :     if (l >= (ulong)lg(y)) l = lg(y)-1;
    3492       25209 :     for (i = 1; i <= l; i++)
    3493             :     {
    3494       24985 :       GEN q = gel(y,i);
    3495       24985 :       gel(z,i) = q;
    3496       24985 :       c = b; if (!gequal1(q)) c = mulii(q, b);
    3497       24985 :       c = subii(a, c);
    3498       24985 :       if (signe(c) < 0)
    3499             :       { /* partial quotient too large */
    3500          96 :         c = addii(c, b);
    3501          96 :         if (signe(c) >= 0) i++; /* by 1 */
    3502          96 :         break;
    3503             :       }
    3504       24889 :       if (cmpii(c, b) >= 0)
    3505             :       { /* partial quotient too small */
    3506          30 :         c = subii(c, b);
    3507          30 :         if (cmpii(c, b) < 0) {
    3508             :           /* by 1. If next quotient is 1 in y, add 1 */
    3509          12 :           if (i < l && equali1(gel(y,i+1))) gel(z,i) = addiu(q,1);
    3510          12 :           i++;
    3511             :         }
    3512          30 :         break;
    3513             :       }
    3514       24859 :       if ((i & 0xff) == 0) gerepileall(av, 2, &b, &c);
    3515       24859 :       a = b; b = c;
    3516             :     }
    3517             :   } else {
    3518     3136994 :     a = icopy_lg(a, ly);
    3519     3136994 :     b = icopy(b);
    3520    24524282 :     for (i = 1; i <= l; i++)
    3521             :     {
    3522    24523964 :       gel(z,i) = truedvmdii(a,b,&c);
    3523    24523964 :       if (c == gen_0) { i++; break; }
    3524    21387288 :       affii(c, a); cgiv(c); c = a;
    3525    21387288 :       a = b; b = c;
    3526             :     }
    3527             :   }
    3528     3137344 :   i--;
    3529     3137344 :   if (i > 1 && gequal1(gel(z,i)))
    3530             :   {
    3531         101 :     cgiv(gel(z,i)); --i;
    3532         101 :     gel(z,i) = addui(1, gel(z,i)); /* unclean: leave old z[i] on stack */
    3533             :   }
    3534     3137344 :   setlg(z,i+1); return z;
    3535             : }
    3536             : 
    3537             : static GEN
    3538           0 : sersfcont(GEN a, GEN b, long k)
    3539             : {
    3540           0 :   long i, l = typ(a) == t_POL? lg(a): 3;
    3541             :   GEN y, c;
    3542           0 :   if (lg(b) > l) l = lg(b);
    3543           0 :   if (k > 0 && l > k+1) l = k+1;
    3544           0 :   y = cgetg(l,t_VEC);
    3545           0 :   for (i=1; i<l; i++)
    3546             :   {
    3547           0 :     gel(y,i) = poldivrem(a,b,&c);
    3548           0 :     if (gequal0(c)) { i++; break; }
    3549           0 :     a = b; b = c;
    3550             :   }
    3551           0 :   setlg(y, i); return y;
    3552             : }
    3553             : 
    3554             : GEN
    3555     3142293 : gboundcf(GEN x, long k)
    3556             : {
    3557             :   pari_sp av;
    3558     3142293 :   long tx = typ(x), e;
    3559             :   GEN y, a, b, c;
    3560             : 
    3561     3142293 :   if (k < 0) pari_err_DOMAIN("gboundcf","nmax","<",gen_0,stoi(k));
    3562     3142286 :   if (is_scalar_t(tx))
    3563             :   {
    3564     3142286 :     if (gequal0(x)) return mkvec(gen_0);
    3565     3142181 :     switch(tx)
    3566             :     {
    3567        5180 :       case t_INT: return mkveccopy(x);
    3568         357 :       case t_REAL:
    3569         357 :         av = avma;
    3570         357 :         c = mantissa_real(x,&e);
    3571         357 :         if (e < 0) pari_err_PREC("gboundcf");
    3572         350 :         y = int2n(e);
    3573         350 :         a = Qsfcont(c,y, NULL, k);
    3574         350 :         b = addsi(signe(x), c);
    3575         350 :         return gerepilecopy(av, Qsfcont(b,y, a, k));
    3576             : 
    3577     3136644 :       case t_FRAC:
    3578     3136644 :         av = avma;
    3579     3136644 :         return gerepileupto(av, Qsfcont(gel(x,1),gel(x,2), NULL, k));
    3580             :     }
    3581           0 :     pari_err_TYPE("gboundcf",x);
    3582             :   }
    3583             : 
    3584           0 :   switch(tx)
    3585             :   {
    3586           0 :     case t_POL: return mkveccopy(x);
    3587           0 :     case t_SER:
    3588           0 :       av = avma;
    3589           0 :       return gerepileupto(av, gboundcf(ser2rfrac_i(x), k));
    3590           0 :     case t_RFRAC:
    3591           0 :       av = avma;
    3592           0 :       return gerepilecopy(av, sersfcont(gel(x,1), gel(x,2), k));
    3593             :   }
    3594           0 :   pari_err_TYPE("gboundcf",x);
    3595             :   return NULL; /* LCOV_EXCL_LINE */
    3596             : }
    3597             : 
    3598             : static GEN
    3599          14 : sfcont2(GEN b, GEN x, long k)
    3600             : {
    3601          14 :   pari_sp av = avma;
    3602          14 :   long lb = lg(b), tx = typ(x), i;
    3603             :   GEN y,p1;
    3604             : 
    3605          14 :   if (k)
    3606             :   {
    3607           7 :     if (k >= lb) pari_err_DIM("contfrac [too few denominators]");
    3608           0 :     lb = k+1;
    3609             :   }
    3610           7 :   y = cgetg(lb,t_VEC);
    3611           7 :   if (lb==1) return y;
    3612           7 :   if (is_scalar_t(tx))
    3613             :   {
    3614           7 :     if (!is_intreal_t(tx) && tx != t_FRAC) pari_err_TYPE("sfcont2",x);
    3615             :   }
    3616           0 :   else if (tx == t_SER) x = ser2rfrac_i(x);
    3617             : 
    3618           7 :   if (!gequal1(gel(b,1))) x = gmul(gel(b,1),x);
    3619           7 :   for (i = 1;;)
    3620             :   {
    3621          35 :     if (tx == t_REAL)
    3622             :     {
    3623          35 :       long e = expo(x);
    3624          35 :       if (e > 0 && nbits2prec(e+1) > realprec(x)) break;
    3625          35 :       gel(y,i) = floorr(x);
    3626          35 :       p1 = subri(x, gel(y,i));
    3627             :     }
    3628             :     else
    3629             :     {
    3630           0 :       gel(y,i) = gfloor(x);
    3631           0 :       p1 = gsub(x, gel(y,i));
    3632             :     }
    3633          35 :     if (++i >= lb) break;
    3634          28 :     if (gequal0(p1)) break;
    3635          28 :     x = gdiv(gel(b,i),p1);
    3636             :   }
    3637           7 :   setlg(y,i);
    3638           7 :   return gerepilecopy(av,y);
    3639             : }
    3640             : 
    3641             : GEN
    3642         112 : gcf(GEN x) { return gboundcf(x,0); }
    3643             : GEN
    3644           0 : gcf2(GEN b, GEN x) { return contfrac0(x,b,0); }
    3645             : GEN
    3646          49 : contfrac0(GEN x, GEN b, long nmax)
    3647             : {
    3648             :   long tb;
    3649             : 
    3650          49 :   if (!b) return gboundcf(x,nmax);
    3651          28 :   tb = typ(b);
    3652          28 :   if (tb == t_INT) return gboundcf(x,itos(b));
    3653          21 :   if (! is_vec_t(tb)) pari_err_TYPE("contfrac0",b);
    3654          21 :   if (nmax < 0) pari_err_DOMAIN("contfrac","nmax","<",gen_0,stoi(nmax));
    3655          14 :   return sfcont2(b,x,nmax);
    3656             : }
    3657             : 
    3658             : GEN
    3659         252 : contfracpnqn(GEN x, long n)
    3660             : {
    3661         252 :   pari_sp av = avma;
    3662         252 :   long i, lx = lg(x);
    3663             :   GEN M,A,B, p0,p1, q0,q1;
    3664             : 
    3665         252 :   if (lx == 1)
    3666             :   {
    3667          28 :     if (! is_matvec_t(typ(x))) pari_err_TYPE("pnqn",x);
    3668          21 :     if (n >= 0) return cgetg(1,t_MAT);
    3669           7 :     return matid(2);
    3670             :   }
    3671         224 :   switch(typ(x))
    3672             :   {
    3673         182 :     case t_VEC: case t_COL: A = x; B = NULL; break;
    3674          42 :     case t_MAT:
    3675          42 :       switch(lgcols(x))
    3676             :       {
    3677           0 :         case 2: A = row(x,1); B = NULL; break;
    3678          35 :         case 3: A = row(x,2); B = row(x,1); break;
    3679           7 :         default: pari_err_DIM("pnqn [ nbrows != 1,2 ]");
    3680             :                  return NULL; /*LCOV_EXCL_LINE*/
    3681             :       }
    3682          35 :       break;
    3683           0 :     default: pari_err_TYPE("pnqn",x);
    3684             :       return NULL; /*LCOV_EXCL_LINE*/
    3685             :   }
    3686         217 :   p1 = gel(A,1);
    3687         217 :   q1 = B? gel(B,1): gen_1; /* p[0], q[0] */
    3688         217 :   if (n >= 0)
    3689             :   {
    3690         182 :     lx = minss(lx, n+2);
    3691         182 :     if (lx == 2) return gerepilecopy(av, mkmat(mkcol2(p1,q1)));
    3692             :   }
    3693          35 :   else if (lx == 2)
    3694           7 :     return gerepilecopy(av, mkmat2(mkcol2(p1,q1), mkcol2(gen_1,gen_0)));
    3695             :   /* lx >= 3 */
    3696         119 :   p0 = gen_1;
    3697         119 :   q0 = gen_0; /* p[-1], q[-1] */
    3698         119 :   M = cgetg(lx, t_MAT);
    3699         119 :   gel(M,1) = mkcol2(p1,q1);
    3700         399 :   for (i=2; i<lx; i++)
    3701             :   {
    3702         280 :     GEN a = gel(A,i), p2,q2;
    3703         280 :     if (B) {
    3704          84 :       GEN b = gel(B,i);
    3705          84 :       p0 = gmul(b,p0);
    3706          84 :       q0 = gmul(b,q0);
    3707             :     }
    3708         280 :     p2 = gadd(gmul(a,p1),p0); p0=p1; p1=p2;
    3709         280 :     q2 = gadd(gmul(a,q1),q0); q0=q1; q1=q2;
    3710         280 :     gel(M,i) = mkcol2(p1,q1);
    3711             :   }
    3712         119 :   if (n < 0) M = mkmat2(gel(M,lx-1), gel(M,lx-2));
    3713         119 :   return gerepilecopy(av, M);
    3714             : }
    3715             : GEN
    3716           0 : pnqn(GEN x) { return contfracpnqn(x,-1); }
    3717             : /* x = [a0, ..., an] from gboundcf, n >= 0;
    3718             :  * return [[p0, ..., pn], [q0,...,qn]] */
    3719             : GEN
    3720      894782 : ZV_allpnqn(GEN x)
    3721             : {
    3722      894782 :   long i, lx = lg(x);
    3723      894782 :   GEN p0, p1, q0, q1, p2, q2, P,Q, v = cgetg(3,t_VEC);
    3724             : 
    3725      894782 :   gel(v,1) = P = cgetg(lx, t_VEC);
    3726      894782 :   gel(v,2) = Q = cgetg(lx, t_VEC);
    3727      894782 :   p0 = gen_1; q0 = gen_0;
    3728      894782 :   gel(P, 1) = p1 = gel(x,1); gel(Q, 1) = q1 = gen_1;
    3729     3106138 :   for (i=2; i<lx; i++)
    3730             :   {
    3731     2211356 :     GEN a = gel(x,i);
    3732     2211356 :     gel(P, i) = p2 = addmulii(p0, a, p1); p0 = p1; p1 = p2;
    3733     2211356 :     gel(Q, i) = q2 = addmulii(q0, a, q1); q0 = q1; q1 = q2;
    3734             :   }
    3735      894782 :   return v;
    3736             : }
    3737             : 
    3738             : /* write Mod(x,N) as a/b, gcd(a,b) = 1, b <= B (no condition if B = NULL) */
    3739             : static GEN
    3740          42 : mod_to_frac(GEN x, GEN N, GEN B)
    3741             : {
    3742             :   GEN a, b, A;
    3743          42 :   if (B) A = divii(shifti(N, -1), B);
    3744             :   else
    3745             :   {
    3746          14 :     A = sqrti(shifti(N, -1));
    3747          14 :     B = A;
    3748             :   }
    3749          42 :   if (!Fp_ratlift(x, N, A,B,&a,&b) || !equali1( gcdii(a,b) )) return NULL;
    3750          28 :   return equali1(b)? a: mkfrac(a,b);
    3751             : }
    3752             : 
    3753             : static GEN
    3754         112 : mod_to_rfrac(GEN x, GEN N, long B)
    3755             : {
    3756             :   GEN a, b;
    3757         112 :   long A, d = degpol(N);
    3758         112 :   if (B >= 0) A = d-1 - B;
    3759             :   else
    3760             :   {
    3761          42 :     B = d >> 1;
    3762          42 :     A = odd(d)? B : B-1;
    3763             :   }
    3764         112 :   if (varn(N) != varn(x)) x = scalarpol(x, varn(N));
    3765         112 :   if (!RgXQ_ratlift(x, N, A, B, &a,&b) || degpol(RgX_gcd(a,b)) > 0) return NULL;
    3766          91 :   return gdiv(a,b);
    3767             : }
    3768             : 
    3769             : /* k > 0 t_INT, x a t_FRAC, returns the convergent a/b
    3770             :  * of the continued fraction of x with b <= k maximal */
    3771             : static GEN
    3772           7 : bestappr_frac(GEN x, GEN k)
    3773             : {
    3774             :   pari_sp av;
    3775             :   GEN p0, p1, p, q0, q1, q, a, y;
    3776             : 
    3777           7 :   if (cmpii(gel(x,2),k) <= 0) return gcopy(x);
    3778           0 :   av = avma; y = x;
    3779           0 :   p1 = gen_1; p0 = truedvmdii(gel(x,1), gel(x,2), &a); /* = floor(x) */
    3780           0 :   q1 = gen_0; q0 = gen_1;
    3781           0 :   x = mkfrac(a, gel(x,2)); /* = frac(x); now 0<= x < 1 */
    3782             :   for(;;)
    3783             :   {
    3784           0 :     x = ginv(x); /* > 1 */
    3785           0 :     a = typ(x)==t_INT? x: divii(gel(x,1), gel(x,2));
    3786           0 :     if (cmpii(a,k) > 0)
    3787             :     { /* next partial quotient will overflow limits */
    3788             :       GEN n, d;
    3789           0 :       a = divii(subii(k, q1), q0);
    3790           0 :       p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3791           0 :       q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3792             :       /* compare |y-p0/q0|, |y-p1/q1| */
    3793           0 :       n = gel(y,1);
    3794           0 :       d = gel(y,2);
    3795           0 :       if (abscmpii(mulii(q1, subii(mulii(q0,n), mulii(d,p0))),
    3796             :                    mulii(q0, subii(mulii(q1,n), mulii(d,p1)))) < 0)
    3797           0 :                    { p1 = p0; q1 = q0; }
    3798           0 :       break;
    3799             :     }
    3800           0 :     p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3801           0 :     q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3802             : 
    3803           0 :     if (cmpii(q0,k) > 0) break;
    3804           0 :     x = gsub(x,a); /* 0 <= x < 1 */
    3805           0 :     if (typ(x) == t_INT) { p1 = p0; q1 = q0; break; } /* x = 0 */
    3806             : 
    3807             :   }
    3808           0 :   return gerepileupto(av, gdiv(p1,q1));
    3809             : }
    3810             : /* k > 0 t_INT, x != 0 a t_REAL, returns the convergent a/b
    3811             :  * of the continued fraction of x with b <= k maximal */
    3812             : static GEN
    3813     1250440 : bestappr_real(GEN x, GEN k)
    3814             : {
    3815     1250440 :   pari_sp av = avma;
    3816     1250440 :   GEN kr, p0, p1, p, q0, q1, q, a, y = x;
    3817             : 
    3818     1250440 :   p1 = gen_1; a = p0 = floorr(x);
    3819     1250351 :   q1 = gen_0; q0 = gen_1;
    3820     1250351 :   x = subri(x,a); /* 0 <= x < 1 */
    3821     1250395 :   if (!signe(x)) { cgiv(x); return a; }
    3822     1134684 :   kr = itor(k, realprec(x));
    3823             :   for(;;)
    3824     1228035 :   {
    3825             :     long d;
    3826     2362761 :     x = invr(x); /* > 1 */
    3827     2362486 :     if (cmprr(x,kr) > 0)
    3828             :     { /* next partial quotient will overflow limits */
    3829     1112721 :       a = divii(subii(k, q1), q0);
    3830     1112709 :       p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3831     1112732 :       q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3832             :       /* compare |y-p0/q0|, |y-p1/q1| */
    3833     1112724 :       if (abscmprr(mulir(q1, subri(mulir(q0,y), p0)),
    3834             :                    mulir(q0, subri(mulir(q1,y), p1))) < 0)
    3835      124988 :                    { p1 = p0; q1 = q0; }
    3836     1112775 :       break;
    3837             :     }
    3838     1249910 :     d = nbits2prec(expo(x) + 1);
    3839     1249910 :     if (d > realprec(x)) { p1 = p0; q1 = q0; break; } /* original x was ~ 0 */
    3840             : 
    3841     1249720 :     a = truncr(x); /* truncr(x) will NOT raise e_PREC */
    3842     1249683 :     p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3843     1249703 :     q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3844             : 
    3845     1249708 :     if (cmpii(q0,k) > 0) break;
    3846     1243453 :     x = subri(x,a); /* 0 <= x < 1 */
    3847     1243463 :     if (!signe(x)) { p1 = p0; q1 = q0; break; }
    3848             :   }
    3849     1134648 :   if (signe(q1) < 0) { togglesign_safe(&p1); togglesign_safe(&q1); }
    3850     1134648 :   return gerepilecopy(av, equali1(q1)? p1: mkfrac(p1,q1));
    3851             : }
    3852             : 
    3853             : /* k t_INT or NULL */
    3854             : static GEN
    3855     2250273 : bestappr_Q(GEN x, GEN k)
    3856             : {
    3857     2250273 :   long lx, tx = typ(x), i;
    3858             :   GEN a, y;
    3859             : 
    3860     2250273 :   switch(tx)
    3861             :   {
    3862         154 :     case t_INT: return icopy(x);
    3863           7 :     case t_FRAC: return k? bestappr_frac(x, k): gcopy(x);
    3864     1506326 :     case t_REAL:
    3865     1506326 :       if (!signe(x)) return gen_0;
    3866             :       /* i <= e iff nbits2lg(e+1) > lg(x) iff floorr(x) fails */
    3867     1250406 :       i = bit_prec(x); if (i <= expo(x)) return NULL;
    3868     1250438 :       return bestappr_real(x, k? k: int2n(i));
    3869             : 
    3870          28 :     case t_INTMOD: {
    3871          28 :       pari_sp av = avma;
    3872          28 :       a = mod_to_frac(gel(x,2), gel(x,1), k); if (!a) return NULL;
    3873          21 :       return gerepilecopy(av, a);
    3874             :     }
    3875          14 :     case t_PADIC: {
    3876          14 :       pari_sp av = avma;
    3877          14 :       long v = valp(x);
    3878          14 :       a = mod_to_frac(gel(x,4), gel(x,3), k); if (!a) return NULL;
    3879           7 :       if (v) a = gmul(a, powis(gel(x,2), v));
    3880           7 :       return gerepilecopy(av, a);
    3881             :     }
    3882             : 
    3883        5453 :     case t_COMPLEX: {
    3884        5453 :       pari_sp av = avma;
    3885        5453 :       y = cgetg(3, t_COMPLEX);
    3886        5453 :       gel(y,2) = bestappr(gel(x,2), k);
    3887        5453 :       gel(y,1) = bestappr(gel(x,1), k);
    3888        5453 :       if (gequal0(gel(y,2))) return gerepileupto(av, gel(y,1));
    3889          91 :       return y;
    3890             :     }
    3891           0 :     case t_SER:
    3892           0 :       if (ser_isexactzero(x)) return gcopy(x);
    3893             :       /* fall through */
    3894             :     case t_POLMOD: case t_POL: case t_RFRAC:
    3895             :     case t_VEC: case t_COL: case t_MAT:
    3896      738291 :       y = cgetg_copy(x, &lx);
    3897      738334 :       if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    3898     2883936 :       for (; i<lx; i++)
    3899             :       {
    3900     2145614 :         a = bestappr_Q(gel(x,i),k); if (!a) return NULL;
    3901     2145602 :         gel(y,i) = a;
    3902             :       }
    3903      738322 :       if (tx == t_POL) return normalizepol(y);
    3904      738308 :       if (tx == t_SER) return normalizeser(y);
    3905      738308 :       return y;
    3906             :   }
    3907           0 :   pari_err_TYPE("bestappr_Q",x);
    3908             :   return NULL; /* LCOV_EXCL_LINE */
    3909             : }
    3910             : 
    3911             : static GEN
    3912          98 : bestappr_ser(GEN x, long B)
    3913             : {
    3914          98 :   long dN, v = valser(x), lx = lg(x);
    3915             :   GEN t;
    3916          98 :   x = normalizepol(ser2pol_i(x, lx));
    3917          98 :   dN = lx-2;
    3918          98 :   if (v > 0)
    3919             :   {
    3920          21 :     x = RgX_shift_shallow(x, v);
    3921          21 :     dN += v;
    3922             :   }
    3923          77 :   else if (v < 0)
    3924             :   {
    3925          14 :     if (B >= 0) B = maxss(B+v, 0);
    3926             :   }
    3927          98 :   t = mod_to_rfrac(x, pol_xn(dN, varn(x)), B);
    3928          98 :   if (!t) return NULL;
    3929          77 :   if (v < 0)
    3930             :   {
    3931             :     GEN a, b;
    3932             :     long vx;
    3933          14 :     if (typ(t) == t_POL) return RgX_mulXn(t, v);
    3934             :     /* t_RFRAC */
    3935          14 :     vx = varn(x);
    3936          14 :     a = gel(t,1);
    3937          14 :     b = gel(t,2);
    3938          14 :     v -= RgX_valrem(b, &b);
    3939          14 :     if (typ(a) == t_POL && varn(a) == vx) v += RgX_valrem(a, &a);
    3940          14 :     if (v < 0) b = RgX_shift_shallow(b, -v);
    3941           0 :     else if (v > 0) {
    3942           0 :       if (typ(a) != t_POL || varn(a) != vx) a = scalarpol_shallow(a, vx);
    3943           0 :       a = RgX_shift_shallow(a, v);
    3944             :     }
    3945          14 :     t = mkrfraccopy(a, b);
    3946             :   }
    3947          77 :   return t;
    3948             : }
    3949             : static GEN
    3950          42 : gc_empty(pari_sp av) { set_avma(av); return cgetg(1, t_VEC); }
    3951             : static GEN
    3952         112 : _gc_upto(pari_sp av, GEN x) { return x? gerepileupto(av, x): NULL; }
    3953             : 
    3954             : static GEN bestappr_RgX(GEN x, long B);
    3955             : /* B >= 0 or < 0 [omit condition on B].
    3956             :  * Look for coprime t_POL a,b, deg(b)<=B, such that a/b ~ x */
    3957             : static GEN
    3958         119 : bestappr_RgX(GEN x, long B)
    3959             : {
    3960             :   pari_sp av;
    3961         119 :   switch(typ(x))
    3962             :   {
    3963           0 :     case t_INT: case t_REAL: case t_INTMOD: case t_FRAC: case t_FFELT:
    3964             :     case t_COMPLEX: case t_PADIC: case t_QUAD: case t_POL:
    3965           0 :       return gcopy(x);
    3966          14 :     case t_RFRAC:
    3967          14 :       if (B < 0 || degpol(gel(x,2)) <= B) return gcopy(x);
    3968           7 :       av = avma; return _gc_upto(av, bestappr_ser(rfrac_to_ser_i(x, 2*B+1), B));
    3969          14 :     case t_POLMOD:
    3970          14 :       av = avma; return _gc_upto(av, mod_to_rfrac(gel(x,2), gel(x,1), B));
    3971          91 :     case t_SER:
    3972          91 :       av = avma; return _gc_upto(av, bestappr_ser(x, B));
    3973           0 :     case t_VEC: case t_COL: case t_MAT: {
    3974             :       long i, lx;
    3975           0 :       GEN y = cgetg_copy(x, &lx);
    3976           0 :       for (i = 1; i < lx; i++)
    3977             :       {
    3978           0 :         GEN t = bestappr_RgX(gel(x,i),B); if (!t) return NULL;
    3979           0 :         gel(y,i) = t;
    3980             :       }
    3981           0 :       return y;
    3982             :     }
    3983             :   }
    3984           0 :   pari_err_TYPE("bestappr_RgX",x);
    3985             :   return NULL; /* LCOV_EXCL_LINE */
    3986             : }
    3987             : 
    3988             : /* allow k = NULL: maximal accuracy */
    3989             : GEN
    3990      104655 : bestappr(GEN x, GEN k)
    3991             : {
    3992      104655 :   pari_sp av = avma;
    3993      104655 :   if (k) { /* replace by floor(k) */
    3994      104333 :     switch(typ(k))
    3995             :     {
    3996       33026 :       case t_INT:
    3997       33026 :         break;
    3998       71307 :       case t_REAL: case t_FRAC:
    3999       71307 :         k = floor_safe(k); /* left on stack for efficiency */
    4000       71307 :         if (!signe(k)) k = gen_1;
    4001       71307 :         break;
    4002           0 :       default:
    4003           0 :         pari_err_TYPE("bestappr [bound type]", k);
    4004           0 :         break;
    4005             :     }
    4006             :   }
    4007      104655 :   x = bestappr_Q(x, k);
    4008      104653 :   return x? x: gc_empty(av);
    4009             : }
    4010             : GEN
    4011         119 : bestapprPade(GEN x, long B)
    4012             : {
    4013         119 :   pari_sp av = avma;
    4014         119 :   GEN t = bestappr_RgX(x, B);
    4015         119 :   return t? t: gc_empty(av);
    4016             : }
    4017             : 
    4018             : static GEN
    4019          49 : serPade(GEN S, long p, long q)
    4020             : {
    4021          49 :   pari_sp av = avma;
    4022          49 :   long va, v, t = typ(S);
    4023          49 :   if (t!=t_SER && t!=t_POL && t!=t_RFRAC) pari_err_TYPE("bestapprPade", S);
    4024          49 :   va = gvar(S); v = gvaluation(S, pol_x(va));
    4025          49 :   if (p < 0) pari_err_DOMAIN("bestapprPade", "p", "<", gen_0, stoi(p));
    4026          49 :   if (q < 0) pari_err_DOMAIN("bestapprPade", "q", "<", gen_0, stoi(q));
    4027          49 :   if (v == LONG_MAX) return gc_empty(av);
    4028          42 :   S = gadd(S, zeroser(va, p + q + 1 + v));
    4029          42 :   return gerepileupto(av, bestapprPade(S, q));
    4030             : }
    4031             : 
    4032             : GEN
    4033         126 : bestapprPade0(GEN x, long p, long q)
    4034             : {
    4035          77 :   return (p >= 0 && q >= 0)? serPade(x, p, q)
    4036         203 :                            : bestapprPade(x, p >= 0? p: q);
    4037             : }

Generated by: LCOV version 1.16