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 29804-254f602fce) Lines: 2076 2276 91.2 %
Date: 2024-12-18 09:08:59 Functions: 218 232 94.0 %
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      463755 : 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      463753 : 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      531130 :   L = cgetg_copy(L0,&i);
      60     1149614 :   while (--i) L[i] = q / uel(L0,i);
      61      531129 :   return L;
      62             : }
      63             : 
      64             : int
      65     1627945 : is_gener_Fl(ulong x, ulong p, ulong p_1, GEN L)
      66             : {
      67             :   long i;
      68     1627945 :   if (krouu(x, p) >= 0) return 0;
      69     1374576 :   for (i=lg(L)-1; i; i--)
      70             :   {
      71      838707 :     ulong t = Fl_powu(x, uel(L,i), p);
      72      838704 :     if (t == p_1 || t == 1) return 0;
      73             :   }
      74      535869 :   return 1;
      75             : }
      76             : /* assume p prime */
      77             : ulong
      78     1050469 : pgener_Fl_local(ulong p, GEN L0)
      79             : {
      80     1050469 :   const pari_sp av = avma;
      81     1050469 :   const ulong p_1 = p-1;
      82             :   long x;
      83             :   GEN L;
      84     1050469 :   if (p <= 19) switch(p)
      85             :   { /* quick trivial cases */
      86          56 :     case 2:  return 1;
      87      116120 :     case 7:
      88      116120 :     case 17: return 3;
      89      403220 :     default: return 2;
      90             :   }
      91      531073 :   L = u_is_gener_expo(p,L0);
      92     1620192 :   for (x = 2;; x++)
      93     1620192 :     if (is_gener_Fl(x,p,p_1,L)) return gc_ulong(av, x);
      94             : }
      95             : ulong
      96      574532 : 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       13928 : is_gener_Fp(GEN x, GEN p, GEN p_1, GEN L)
     102             : {
     103       13928 :   long i, t = lgefint(x)==3? kroui(x[2], p): kronecker(x, p);
     104       13928 :   if (t >= 0) return 0;
     105       21988 :   for (i = lg(L)-1; i; i--)
     106             :   {
     107       14457 :     GEN t = Fp_pow(x, gel(L,i), p);
     108       14457 :     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      358129 : pgener_Fp_local(GEN p, GEN L0)
     116             : {
     117      358129 :   pari_sp av0 = avma;
     118             :   GEN x, p_1, L;
     119      358129 :   if (lgefint(p) == 3)
     120             :   {
     121             :     ulong z;
     122      353227 :     if (p[2] == 2) return gen_1;
     123      258238 :     if (L0) L0 = ZV_to_nv(L0);
     124      258229 :     z = pgener_Fl_local(uel(p,2), L0);
     125      258267 :     return gc_utoipos(av0, z);
     126             :   }
     127        4902 :   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      205360 : pgener_Zl(ulong p)
     138             : {
     139      205360 :   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      205360 :   if (p == 40487) return 10;
     142             : #ifndef LONG_IS_64BIT
     143       29758 :   return pgener_Fl(p);
     144             : #else
     145      175602 :   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      170604 : pgener_Zp(GEN p)
     162             : {
     163      170604 :   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      215510 : rootsof1_Fl(ulong n, ulong p)
     251             : {
     252      215510 :   pari_sp av = avma;
     253      215510 :   GEN L = u_odd_prime_divisors(n); /* 2 implicit in pgener_Fl_local */
     254      215510 :   ulong z = pgener_Fl_local(p, L);
     255      215510 :   z = Fl_powu(z, (p-1) / n, p); /* prim. n-th root of 1 */
     256      215510 :   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   321267037 : ome(long t)
     364             : {
     365   321267037 :   switch(t & 7)
     366             :   {
     367   182198934 :     case 3:
     368   182198934 :     case 5: return 1;
     369   139068103 :     default: return 0;
     370             :   }
     371             : }
     372             : /* t a t_INT, is t = 3,5 mod 8 ? */
     373             : static int
     374     5599056 : gome(GEN t)
     375     5599056 : { return signe(t)? ome( mod2BIL(t) ): 0; }
     376             : 
     377             : /* assume y odd, return kronecker(x,y) * s */
     378             : static long
     379   227969038 : krouu_s(ulong x, ulong y, long s)
     380             : {
     381   227969038 :   ulong x1 = x, y1 = y, z;
     382  1032746326 :   while (x1)
     383             :   {
     384   804789885 :     long r = vals(x1);
     385   804838414 :     if (r)
     386             :     {
     387   427818145 :       if (odd(r) && ome(y1)) s = -s;
     388   427757019 :       x1 >>= r;
     389             :     }
     390   804777288 :     if (x1 & y1 & 2) s = -s;
     391   804777288 :     z = y1 % x1; y1 = x1; x1 = z;
     392             :   }
     393   227956441 :   return (y1 == 1)? s: 0;
     394             : }
     395             : 
     396             : long
     397    11962436 : kronecker(GEN x, GEN y)
     398             : {
     399    11962436 :   pari_sp av = avma;
     400    11962436 :   long s = 1, r;
     401             :   ulong xu;
     402             : 
     403    11962436 :   if (typ(x) != t_INT) pari_err_TYPE("kronecker",x);
     404    11962436 :   if (typ(y) != t_INT) pari_err_TYPE("kronecker",y);
     405    11962436 :   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    11962303 :   r = vali(y);
     411    11962294 :   if (r)
     412             :   {
     413     1348911 :     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    10935094 :   x = modii(x,y);
     418    13328935 :   while (lgefint(x) > 3) /* x < y */
     419             :   {
     420             :     GEN z;
     421     2393892 :     r = vali(x);
     422     2393764 :     if (r)
     423             :     {
     424     1306999 :       if (odd(r) && gome(y)) s = -s;
     425     1306974 :       x = shifti(x,-r);
     426             :     }
     427             :     /* x=3 mod 4 && y=3 mod 4 ? (both are odd here) */
     428     2392815 :     if (mod2BIL(x) & mod2BIL(y) & 2) s = -s;
     429     2391798 :     z = remii(y,x); y = x; x = z;
     430     2393780 :     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    10935043 :   xu = itou(x);
     437    10935040 :   if (!xu) return is_pm1(y)? s: 0;
     438    10837319 :   r = vals(xu);
     439    10837313 :   if (r)
     440             :   {
     441     5754762 :     if (odd(r) && gome(y)) s = -s;
     442     5754762 :     xu >>= r;
     443             :   }
     444             :   /* x=3 mod 4 && y=3 mod 4 ? (both are odd here) */
     445    10837313 :   if (xu & mod2BIL(y) & 2) s = -s;
     446    10837315 :   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    27627722 : kroiu(GEN x, ulong y)
     474             : {
     475             :   long r;
     476    27627722 :   if (odd(y)) return krouu_s(umodiu(x,y), y, 1);
     477      302995 :   if (!mpodd(x)) return 0;
     478      208348 :   r = vals(y); y >>= r;
     479      208354 :   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      178229 : krouodd(ulong x, GEN y, long s)
     485             : {
     486             :   long r;
     487      178229 :   if (lgefint(y) == 3) return krouu_s(x, y[2], s);
     488       27991 :   if (!x) return 0; /* y != 1 */
     489       27991 :   r = vals(x);
     490       27991 :   if (r)
     491             :   {
     492       14518 :     if (odd(r) && gome(y)) s = -s;
     493       14518 :     x >>= r;
     494             :   }
     495             :   /* x=3 mod 4 && y=3 mod 4 ? (both are odd here) */
     496       27991 :   if (x & mod2BIL(y) & 2) s = -s;
     497       27991 :   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       35008 : kroui(ulong x, GEN y)
     523             : {
     524       35008 :   const pari_sp av = avma;
     525       35008 :   long s = 1, r;
     526       35008 :   switch (signe(y))
     527             :   {
     528           0 :     case -1: y = negi(y); break;
     529           0 :     case 0: return x==1UL;
     530             :   }
     531       35008 :   r = vali(y);
     532       35008 :   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       35008 :   return gc_long(av, krouodd(x, y, s));
     539             : }
     540             : 
     541             : long
     542    97777677 : kross(long x, long y)
     543             : {
     544             :   ulong yu;
     545    97777677 :   long s = 1;
     546             : 
     547    97777677 :   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    97708734 :     yu = (ulong)y;
     554    97777649 :   if (!odd(yu))
     555             :   {
     556             :     long r;
     557    23582769 :     if (!odd(x)) return 0;
     558    16659931 :     r = vals(yu); yu >>= r;
     559    16659931 :     if (odd(r) && ome(x)) s = -s;
     560             :   }
     561    90854811 :   x %= (long)yu; if (x < 0) x += yu;
     562    90854811 :   return krouu_s((ulong)x, yu, s);
     563             : }
     564             : 
     565             : long
     566    98548570 : krouu(ulong x, ulong y)
     567             : {
     568             :   long r;
     569    98548570 :   if (odd(y)) return krouu_s(x, y, 1);
     570       16956 :   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     2250479 : checkp(ulong q, ulong p)
     710     2250479 : { 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    11603225 : nonsquare1_Fl(ulong p)
     714             : {
     715             :   forprime_t S;
     716             :   ulong q;
     717    11603225 :   if ((p & 7UL) != 1) return 2UL;
     718     4352880 :   q = p % 3; if (q == 2) return 3UL;
     719     1401594 :   checkp(q, p);
     720     1409658 :   q = p % 5; if (q == 2 || q == 3) return 5UL;
     721      521020 :   checkp(q, p);
     722      521018 :   q = p % 7; if (q != 4 && q >= 3) return 7UL;
     723      194014 :   checkp(q, p);
     724             :   /* log^2(2^64) < 1968 is enough under GRH (and p^(1/4)log(p) without it)*/
     725      194051 :   u_forprime_init(&S, 11, 1967);
     726      320079 :   while ((q = u_forprime_next(&S)))
     727             :   {
     728      320076 :     if (krouu(q, p) < 0) return q;
     729      126024 :     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      177396 : Fl_2gener_pre(ulong p, ulong pi)
     742             : {
     743      177396 :   ulong p1 = p-1;
     744      177396 :   long e = vals(p1);
     745      177388 :   if (e == 1) return p1;
     746       65814 :   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    12266054 : Fl_sqrt_i(ulong a, ulong y, ulong p)
     760             : {
     761             :   long i, e, k;
     762             :   ulong p1, q, v, w;
     763             : 
     764    12266054 :   if (!a) return 0;
     765    10991780 :   p1 = p - 1; e = vals(p1);
     766    10992727 :   if (e == 0) /* p = 2 */
     767             :   {
     768      649441 :     if (p != 2) pari_err_PRIME("Fl_sqrt [modulus]",utoi(p));
     769      650983 :     return ((a & 1) == 0)? 0: 1;
     770             :   }
     771    10343286 :   if (e == 1)
     772             :   {
     773     4881757 :     v = Fl_powu(a, (p+1) >> 2, p);
     774     4881785 :     if (Fl_sqr(v, p) != a) return ~0UL;
     775     4876935 :     p1 = p - v; if (v > p1) v = p1;
     776     4876935 :     return v;
     777             :   }
     778     5461529 :   q = p1 >> e; /* q = (p-1)/2^oo is odd */
     779     5461529 :   p1 = Fl_powu(a, q >> 1, p); /* a ^ [(q-1)/2] */
     780     5461520 :   if (!p1) return 0;
     781     5461520 :   v = Fl_mul(a, p1, p);
     782     5461526 :   w = Fl_mul(v, p1, p);
     783     5461532 :   if (!y) y = Fl_powu(nonsquare1_Fl(p), q, p);
     784     9288312 :   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     3828451 :     p1 = Fl_sqr(w, p);
     788     6359026 :     for (k=1; p1 != 1 && k < e; k++) p1 = Fl_sqr(p1, p);
     789     3828462 :     if (k == e) return ~0UL;
     790             :     /* w ^ (2^k) = 1 --> w = y ^ (u * 2^(e-k)), u odd */
     791     3826385 :     p1 = y;
     792     5102822 :     for (i=1; i < e-k; i++) p1 = Fl_sqr(p1, p);
     793     3826383 :     y = Fl_sqr(p1, p); e = k;
     794     3826410 :     w = Fl_mul(y, w, p);
     795     3826427 :     v = Fl_mul(v, p1, p);
     796             :   }
     797     5459861 :   p1 = p - v; if (v > p1) v = p1;
     798     5459861 :   return v;
     799             : }
     800             : 
     801             : /* Tonelli-Shanks. Assume p is prime and (a,p) != -1. Allow pi = 0 */
     802             : ulong
     803    33544727 : 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    33544727 :   if (!pi) return Fl_sqrt_i(a, y, p);
     809    21278845 :   if (!a) return 0;
     810    21153471 :   p1 = p - 1; e = vals(p1);
     811    21152709 :   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    21165613 :   if (e == 1)
     817             :   {
     818    15034375 :     v = Fl_powu_pre(a, (p+1) >> 2, p, pi);
     819    15000628 :     if (Fl_sqr_pre(v, p, pi) != a) return ~0UL;
     820    15011523 :     p1 = p - v; if (v > p1) v = p1;
     821    15011523 :     return v;
     822             :   }
     823     6131238 :   q = p1 >> e; /* q = (p-1)/2^oo is odd */
     824     6131238 :   p1 = Fl_powu_pre(a, q >> 1, p, pi); /* a ^ [(q-1)/2] */
     825     6125984 :   if (!p1) return 0;
     826     6125984 :   v = Fl_mul_pre(a, p1, p, pi);
     827     6126785 :   w = Fl_mul_pre(v, p1, p, pi);
     828     6125192 :   if (!y) y = Fl_powu_pre(nonsquare1_Fl(p), q, p, pi);
     829    11658536 :   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     5531313 :     p1 = Fl_sqr_pre(w,p,pi);
     833    10359725 :     for (k=1; p1 != 1 && k < e; k++) p1 = Fl_sqr_pre(p1,p,pi);
     834     5531605 :     if (k == e) return ~0UL;
     835             :     /* w ^ (2^k) = 1 --> w = y ^ (u * 2^(e-k)), u odd */
     836     5531513 :     p1 = y;
     837     7268229 :     for (i=1; i < e-k; i++) p1 = Fl_sqr_pre(p1, p, pi);
     838     5531657 :     y = Fl_sqr_pre(p1, p, pi); e = k;
     839     5533757 :     w = Fl_mul_pre(y, w, p, pi);
     840     5531711 :     v = Fl_mul_pre(v, p1, p, pi);
     841             :   }
     842     6127223 :   p1 = p - v; if (v > p1) v = p1;
     843     6127223 :   return v;
     844             : }
     845             : 
     846             : ulong
     847    12320864 : Fl_sqrt(ulong a, ulong p)
     848    12320864 : { ulong pi = (p & HIGHMASK)? get_Fl_red(p): 0; return Fl_sqrt_pre_i(a, 0, p, pi); }
     849             : 
     850             : ulong
     851    21062975 : Fl_sqrt_pre(ulong a, ulong p, ulong pi)
     852    21062975 : { return Fl_sqrt_pre_i(a, 0, p, pi); }
     853             : 
     854             : /* allow pi = 0 */
     855             : static ulong
     856      141324 : Fl_lgener_pre_all(ulong l, long e, ulong r, ulong p, ulong pi, ulong *pt_m)
     857             : {
     858      141324 :   ulong x, y, m, le1 = upowuu(l, e-1);
     859      141325 :   for (x = 2; ; x++)
     860             :   {
     861      172232 :     y = Fl_powu_pre(x, r, p, pi);
     862      172232 :     if (y==1) continue;
     863      155085 :     m = Fl_powu_pre(y, le1, p, pi);
     864      155085 :     if (m != 1) break;
     865             :   }
     866      141325 :   *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      225599 : 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      225599 :   if (a==0) return a;
     879      225593 :   u2 = Fl_inv(l%r, r);
     880      225596 :   v = Fl_powu_pre(a, u2, p, pi);
     881      225588 :   w = Fl_powu_pre(v, l, p, pi);
     882      225592 :   w = pi? Fl_mul_pre(w, Fl_inv(a, p), p, pi): Fl_div(w, a, p);
     883      225579 :   if (w==1) return v;
     884      139207 :   if (y==0) y = Fl_lgener_pre_all(l, e, r, p, pi, &m);
     885      164695 :   while (w!=1)
     886             :   {
     887      144620 :     ulong k = 0;
     888      144620 :     p1 = w;
     889             :     do
     890             :     {
     891      188176 :       z = p1; p1 = Fl_powu_pre(p1, l, p, pi);
     892      188176 :       if (++k == e) return ULONG_MAX;
     893       69043 :     } while (p1!=1);
     894       25487 :     dl = Fl_log_pre(z, m, l, p, pi);
     895       25487 :     dl = Fl_neg(dl, l);
     896       25487 :     p1 = Fl_powu_pre(y,dl*upowuu(l,e-k-1),p,pi);
     897       25487 :     m = Fl_powu_pre(m, dl, p, pi);
     898       25487 :     e = k;
     899       25487 :     v = pi? Fl_mul_pre(p1,v,p,pi): Fl_mul(p1,v,p);
     900       25487 :     y = Fl_powu_pre(p1,l,p,pi);
     901       25487 :     w = pi? Fl_mul_pre(y,w,p,pi): Fl_mul(y,w,p);
     902             :   }
     903       20075 :   return v;
     904             : }
     905             : 
     906             : /* allow pi = 0 */
     907             : static ulong
     908      223188 : Fl_sqrtl_i(ulong a, ulong l, ulong p, ulong pi, ulong y, ulong m)
     909             : {
     910      223188 :   ulong r, e = u_lvalrem(p-1, l, &r);
     911      223188 :   return Fl_sqrtl_raw(a, l, e, r, p, pi, y, m);
     912             : }
     913             : /* allow pi = 0 */
     914             : ulong
     915      223187 : Fl_sqrtl_pre(ulong a, ulong l, ulong p, ulong pi)
     916      223187 : { 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      196859 : Fl_sqrtn_pre(ulong a, long n, ulong p, ulong pi, ulong *zetan)
     926             : {
     927      196859 :   ulong m, q = p-1, z;
     928      196859 :   ulong nn = n >= 0 ? (ulong)n: -(ulong)n;
     929      196859 :   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       80470 :   if (n==1)
     936             :   {
     937         420 :     if (zetan) *zetan = 1;
     938         420 :     return n < 0? Fl_inv(a,p): a;
     939             :   }
     940       80050 :   if (n==2)
     941             :   {
     942        7005 :     if (zetan) *zetan = p-1;
     943        7005 :     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      196859 : Fl_sqrtn(ulong a, long n, ulong p, ulong *zetan)
     984             : {
     985      196859 :   ulong pi = (p & HIGHMASK)? get_Fl_red(p): 0;
     986      196859 :   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        5914 : Fp_2gener_all(GEN q, GEN p)
    1056             : {
    1057             :   long k;
    1058        5914 :   for (k = 2;; k++)
    1059       11631 :   {
    1060       17545 :     long i = kroui(k, p);
    1061       17545 :     if (i < 0) return Fp_pow(utoipos(k), q, p);
    1062       11631 :     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       19791 : Fp_2gener_i(GEN ns, GEN p)
    1078             : {
    1079       19791 :   GEN q = subiu(p,1);
    1080       19791 :   long e = vali(q);
    1081       19791 :   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       19201 : Fp_sqrts(long a, GEN p)
    1133             : {
    1134       19201 :   long v = vals(a)>>1;
    1135       19201 :   GEN r = gen_0;
    1136       19201 :   a >>= v << 1;
    1137       19201 :   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        2214 :     case 5: case 13: case 17: case 21: case 29: case 33:
    1178             :     case -7: case -11: case -15: case -19: case -23:
    1179        2214 :       if (umodiu(p,labs(a))==1)
    1180         334 :         r = Fp_gausssum(a,p);
    1181             :       else
    1182        1880 :         return gen_0;
    1183         334 :       break;
    1184       15045 :     default:
    1185       15045 :       return gen_0;
    1186             :   }
    1187        2276 :   return remii(shifti(r, v), p);
    1188             : }
    1189             : 
    1190             : static GEN
    1191       77203 : Fp_sqrt_ii(GEN a, GEN y, GEN p)
    1192             : {
    1193       77203 :   pari_sp av = avma;
    1194       77203 :   GEN  q, v, w, p1 = subiu(p,1);
    1195       77203 :   long i, k, e = vali(p1), as;
    1196             : 
    1197             :   /* direct formulas more efficient */
    1198       77202 :   if (e == 0) pari_err_PRIME("Fp_sqrt [modulus]",p); /* p != 2 */
    1199       77202 :   if (e == 1)
    1200             :   {
    1201       18800 :     q = addiu(shifti(p1,-2),1); /* (p+1) / 4 */
    1202       18800 :     v = Fp_pow(a, q, p);
    1203             :     /* must check equality in case (a/p) = -1 or p not prime */
    1204       18805 :     av = avma; e = equalii(Fp_sqr(v,p), a); set_avma(av);
    1205       18804 :     return e? v: NULL;
    1206             :   }
    1207       58402 :   as = itos_or_0(a);
    1208       58405 :   if (!as) as = itos_or_0(subii(a,p));
    1209       58413 :   if (as)
    1210             :   {
    1211       19201 :     GEN res = Fp_sqrts(as, p);
    1212       19201 :     if (!res) return gc_NULL(av);
    1213       19201 :     if (signe(res)) return gerepileupto(av, res);
    1214             :   }
    1215       56137 :   if (e == 2)
    1216             :   { /* Atkin's formula */
    1217       17296 :     GEN I, a2 = shifti(a,1);
    1218       17292 :     if (cmpii(a2,p) >= 0) a2 = subii(a2,p);
    1219       17292 :     q = shifti(p1, -3); /* (p-5)/8 */
    1220       17293 :     v = Fp_pow(a2, q, p);
    1221       17296 :     I = Fp_mul(a2, Fp_sqr(v,p), p); /* I^2 = -1 */
    1222       17296 :     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       17296 :     av = avma; e = equalii(Fp_sqr(v,p), a); set_avma(av);
    1225       17296 :     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       38841 :   if (e*(e-1) > 20 + 8 * expi(p)) return sqrt_Cipolla(a,p);
    1230             :   /* Tonelli-Shanks */
    1231       38841 :   av = avma; q = shifti(p1,-e); /* q = (p-1)/2^oo is odd */
    1232       38840 :   if (!y)
    1233             :   {
    1234        2722 :     y = Fp_2gener_all(q, p);
    1235        2721 :     if (!y) pari_err_PRIME("Fp_sqrt [modulus]",p);
    1236             :   }
    1237       38839 :   p1 = Fp_pow(a, shifti(q,-1), p); /* a ^ (q-1)/2 */
    1238       38842 :   v = Fp_mul(a, p1, p);
    1239       38842 :   w = Fp_mul(v, p1, p);
    1240       92626 :   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       53826 :     p1 = Fp_sqr(w,p);
    1244      110484 :     for (k=1; !equali1(p1) && k < e; k++) p1 = Fp_sqr(p1,p);
    1245       53827 :     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       53784 :     p1 = y;
    1248       76524 :     for (i=1; i < e-k; i++) p1 = Fp_sqr(p1,p);
    1249       53784 :     y = Fp_sqr(p1, p); e = k;
    1250       53786 :     w = Fp_mul(y, w, p);
    1251       53785 :     v = Fp_mul(v, p1, p);
    1252       53784 :     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       38799 :   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     4889276 : Fp_sqrt_i(GEN a, GEN y, GEN p)
    1265             : {
    1266     4889276 :   pari_sp av = avma, av2;
    1267             :   GEN q;
    1268             : 
    1269     4889276 :   if (lgefint(p) == 3)
    1270             :   {
    1271     4811969 :     ulong pp = uel(p,2), u = umodiu(a, pp);
    1272     4811970 :     if (!u) return gen_0;
    1273     3600409 :     u = Fl_sqrt(u, pp);
    1274     3600559 :     return (u == ~0UL)? NULL: utoipos(u);
    1275             :   }
    1276       77307 :   a = modii(a, p); if (!signe(a)) return gen_0;
    1277       77203 :   a = Fp_sqrt_ii(a, y, p); if (!a) return gc_NULL(av);
    1278             :   /* smallest square root */
    1279       76800 :   av2 = avma; q = subii(p, a);
    1280       76799 :   if (cmpii(a, q) > 0) a = q; else set_avma(av2);
    1281       76801 :   return gerepileuptoint(av, a);
    1282             : }
    1283             : GEN
    1284     4832541 : Fp_sqrt(GEN a, GEN p) { return Fp_sqrt_i(a, NULL, p); }
    1285             : 
    1286             : /*********************************************************************/
    1287             : /**                        GCD & BEZOUT                             **/
    1288             : /*********************************************************************/
    1289             : 
    1290             : GEN
    1291    54627472 : lcmii(GEN x, GEN y)
    1292             : {
    1293             :   pari_sp av;
    1294             :   GEN a, b;
    1295    54627472 :   if (!signe(x) || !signe(y)) return gen_0;
    1296    54627482 :   av = avma; a = gcdii(x,y);
    1297    54626096 :   if (absequalii(a,y)) { set_avma(av); return absi(x); }
    1298    12077107 :   if (!equali1(a)) y = diviiexact(y,a);
    1299    12077086 :   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     5921447 : Fp_invgen(GEN x, GEN N, GEN *pd)
    1306             : {
    1307             :   GEN d, d0, e, v;
    1308     5921447 :   if (lgefint(N) == 3)
    1309             :   {
    1310     5153882 :     ulong dd, NN = N[2], xx = umodiu(x,NN);
    1311     5153952 :     if (!xx) { *pd = N; return gen_0; }
    1312     5153952 :     xx = Fl_invgen(xx, NN, &dd);
    1313     5155470 :     *pd = utoi(dd); return utoi(xx);
    1314             :   }
    1315      767565 :   *pd = d = bezout(x, N, &v, NULL);
    1316      767580 :   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      671975 :   e = diviiexact(N,d);
    1319      671975 :   d0 = Z_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
    1320      671975 :   if (equali1(d0)) return v;
    1321      533903 :   if (!equalii(d,d0)) e = lcmii(e, diviiexact(d,d0));
    1322      533903 :   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     2415377 : gen_chinese(GEN x, GEN(*f)(GEN,GEN))
    1344             : {
    1345     2415377 :   GEN z = gassoc_proto(f,x,NULL);
    1346     2415370 :   if (z == gen_1) retmkintmod(gen_0,gen_1);
    1347     2415335 :   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      271285 : Z_chinese_pre(GEN A, GEN B, GEN *pC, GEN *pU, GEN *pd)
    1436             : {
    1437      271285 :   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      271284 :   *pU = mulii(u, t);
    1440      271287 :   *pC = mulii(t, B);
    1441      271289 :   if (pd) *pd = d;
    1442      271289 : }
    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     2998452 : Z_chinese_post(GEN a, GEN b, GEN C, GEN U, GEN d)
    1448             : {
    1449             :   GEN b_a;
    1450     2998452 :   if (!signe(a))
    1451             :   {
    1452      797148 :     if (d && !dvdii(b, d)) return NULL;
    1453      797148 :     return Fp_mul(b, U, C);
    1454             :   }
    1455     2201304 :   b_a = subii(b,a);
    1456     2201304 :   if (d && !dvdii(b_a, d)) return NULL;
    1457     2201304 :   return modii(addii(a, mulii(U, b_a)), C);
    1458             : }
    1459             : static ulong
    1460     1598612 : u_chinese_post(ulong a, ulong b, ulong C, ulong U)
    1461             : {
    1462     1598612 :   if (!a) return Fl_mul(b, U, C);
    1463     1598612 :   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      267583 : Z_chinese_all(GEN a, GEN b, GEN A, GEN B, GEN *pC)
    1475             : {
    1476      267583 :   GEN U; Z_chinese_pre(A, B, pC, &U, NULL);
    1477      267584 :   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      535163 : Z_chinese_coprime(GEN a, GEN b, GEN A, GEN B, GEN C)
    1484             : {
    1485      535163 :   pari_sp av = avma;
    1486      535163 :   GEN U = mulii(Fp_inv(A,B), A);
    1487      535163 :   return gerepileuptoint(av, Z_chinese_post(a,b,C,U, NULL));
    1488             : }
    1489             : ulong
    1490     1598612 : u_chinese_coprime(ulong a, ulong b, ulong A, ulong B, ulong C)
    1491     1598612 : { 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     2412962 : 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     2497588 : ZV_producttree(GEN xa)
    1514             : {
    1515     2497588 :   long n = lg(xa)-1;
    1516     2497588 :   long m = n==1 ? 1: expu(n-1)+1;
    1517     2497588 :   GEN T = cgetg(m+1, t_VEC), t;
    1518             :   long i, j, k;
    1519     2497589 :   t = cgetg(((n+1)>>1)+1, t_VEC);
    1520     2497585 :   if (typ(xa)==t_VECSMALL)
    1521             :   {
    1522     3289194 :     for (j=1, k=1; k<n; j++, k+=2)
    1523     2102503 :       gel(t, j) = muluu(xa[k], xa[k+1]);
    1524     1186691 :     if (k==n) gel(t, j) = utoi(xa[k]);
    1525             :   } else {
    1526     2715707 :     for (j=1, k=1; k<n; j++, k+=2)
    1527     1404814 :       gel(t, j) = mulii(gel(xa,k), gel(xa,k+1));
    1528     1310893 :     if (k==n) gel(t, j) = icopy(gel(xa,k));
    1529             :   }
    1530     2497587 :   gel(T,1) = t;
    1531     3978379 :   for (i=2; i<=m; i++)
    1532             :   {
    1533     1480791 :     GEN u = gel(T, i-1);
    1534     1480791 :     long n = lg(u)-1;
    1535     1480791 :     t = cgetg(((n+1)>>1)+1, t_VEC);
    1536     3306209 :     for (j=1, k=1; k<n; j++, k+=2)
    1537     1825417 :       gel(t, j) = mulii(gel(u, k), gel(u, k+1));
    1538     1480792 :     if (k==n) gel(t, j) = gel(u, k);
    1539     1480792 :     gel(T, i) = t;
    1540             :   }
    1541     2497588 :   return T;
    1542             : }
    1543             : 
    1544             : /* return [A mod P[i], i=1..#P], T = ZV_producttree(P) */
    1545             : GEN
    1546    57197311 : Z_ZV_mod_tree(GEN A, GEN P, GEN T)
    1547             : {
    1548             :   long i,j,k;
    1549    57197311 :   long m = lg(T)-1, n = lg(P)-1;
    1550             :   GEN t;
    1551    57197311 :   GEN Tp = cgetg(m+1, t_VEC);
    1552    57158567 :   gel(Tp, m) = mkvec(modii(A, gmael(T,m,1)));
    1553   119266433 :   for (i=m-1; i>=1; i--)
    1554             :   {
    1555    62168091 :     GEN u = gel(T, i);
    1556    62168091 :     GEN v = gel(Tp, i+1);
    1557    62168091 :     long n = lg(u)-1;
    1558    62168091 :     t = cgetg(n+1, t_VEC);
    1559   148608506 :     for (j=1, k=1; k<n; j++, k+=2)
    1560             :     {
    1561    86561471 :       gel(t, k)   = modii(gel(v, j), gel(u, k));
    1562    86631903 :       gel(t, k+1) = modii(gel(v, j), gel(u, k+1));
    1563             :     }
    1564    62047035 :     if (k==n) gel(t, k) = gel(v, j);
    1565    62047035 :     gel(Tp, i) = t;
    1566             :   }
    1567             :   {
    1568    57098342 :     GEN u = gel(T, i+1);
    1569    57098342 :     GEN v = gel(Tp, i+1);
    1570    57098342 :     long l = lg(u)-1;
    1571    57098342 :     if (typ(P)==t_VECSMALL)
    1572             :     {
    1573    54607613 :       GEN R = cgetg(n+1, t_VECSMALL);
    1574   195090146 :       for (j=1, k=1; j<=l; j++, k+=2)
    1575             :       {
    1576   140133512 :         uel(R,k) = umodiu(gel(v, j), P[k]);
    1577   140462663 :         if (k < n)
    1578   110474416 :           uel(R,k+1) = umodiu(gel(v, j), P[k+1]);
    1579             :       }
    1580    54956634 :       return R;
    1581             :     }
    1582             :     else
    1583             :     {
    1584     2490729 :       GEN R = cgetg(n+1, t_VEC);
    1585     6816941 :       for (j=1, k=1; j<=l; j++, k+=2)
    1586             :       {
    1587     4319710 :         gel(R,k) = modii(gel(v, j), gel(P,k));
    1588     4319703 :         if (k < n)
    1589     3504114 :           gel(R,k+1) = modii(gel(v, j), gel(P,k+1));
    1590             :       }
    1591     2497231 :       return R;
    1592             :     }
    1593             :   }
    1594             : }
    1595             : 
    1596             : /* T = ZV_producttree(P), R = ZV_chinesetree(P,T) */
    1597             : GEN
    1598    39604471 : ZV_chinese_tree(GEN A, GEN P, GEN T, GEN R)
    1599             : {
    1600    39604471 :   long m = lg(T)-1, n = lg(A)-1;
    1601             :   long i,j,k;
    1602    39604471 :   GEN Tp = cgetg(m+1, t_VEC);
    1603    39597468 :   GEN M = gel(T, 1);
    1604    39597468 :   GEN t = cgetg(lg(M), t_VEC);
    1605    39553443 :   if (typ(P)==t_VECSMALL)
    1606             :   {
    1607    84200864 :     for (j=1, k=1; k<n; j++, k+=2)
    1608             :     {
    1609    61035160 :       pari_sp av = avma;
    1610    61035160 :       GEN a = mului(A[k], gel(R,k)), b = mului(A[k+1], gel(R,k+1));
    1611    60919406 :       GEN tj = modii(addii(mului(P[k],b), mului(P[k+1],a)), gel(M,j));
    1612    61016502 :       gel(t, j) = gerepileuptoint(av, tj);
    1613             :     }
    1614    23165704 :     if (k==n) gel(t, j) = modii(mului(A[k], gel(R,k)), gel(M, j));
    1615             :   } else
    1616             :   {
    1617    34860482 :     for (j=1, k=1; k<n; j++, k+=2)
    1618             :     {
    1619    18423491 :       pari_sp av = avma;
    1620    18423491 :       GEN a = mulii(gel(A,k), gel(R,k)), b = mulii(gel(A,k+1), gel(R,k+1));
    1621    18433191 :       GEN tj = modii(addii(mulii(gel(P,k),b), mulii(gel(P,k+1),a)), gel(M,j));
    1622    18484390 :       gel(t, j) = gerepileuptoint(av, tj);
    1623             :     }
    1624    16436991 :     if (k==n) gel(t, j) = modii(mulii(gel(A,k), gel(R,k)), gel(M, j));
    1625             :   }
    1626    39597521 :   gel(Tp, 1) = t;
    1627    74050658 :   for (i=2; i<=m; i++)
    1628             :   {
    1629    34431933 :     GEN u = gel(T, i-1), M = gel(T, i);
    1630    34431933 :     GEN t = cgetg(lg(M), t_VEC);
    1631    34431764 :     GEN v = gel(Tp, i-1);
    1632    34431764 :     long n = lg(v)-1;
    1633    90166584 :     for (j=1, k=1; k<n; j++, k+=2)
    1634             :     {
    1635    55713447 :       pari_sp av = avma;
    1636    55658555 :       gel(t, j) = gerepileuptoint(av, modii(addii(mulii(gel(u, k), gel(v, k+1)),
    1637    55713447 :             mulii(gel(u, k+1), gel(v, k))), gel(M, j)));
    1638             :     }
    1639    34453137 :     if (k==n) gel(t, j) = gel(v, k);
    1640    34453137 :     gel(Tp, i) = t;
    1641             :   }
    1642    39618725 :   return gmael(Tp,m,1);
    1643             : }
    1644             : 
    1645             : static GEN
    1646     1440327 : ncV_polint_center_tree(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1647             : {
    1648     1440327 :   long i, l = lg(gel(vA,1)), n = lg(P);
    1649     1440327 :   GEN mod = gmael(T, lg(T)-1, 1), V = cgetg(l, t_COL);
    1650    33790211 :   for (i=1; i < l; i++)
    1651             :   {
    1652    32350219 :     pari_sp av = avma;
    1653    32350219 :     GEN c, A = cgetg(n, typ(P));
    1654             :     long j;
    1655   186153584 :     for (j=1; j < n; j++) A[j] = mael(vA,j,i);
    1656    32314962 :     c = Fp_center(ZV_chinese_tree(A, P, T, R), mod, m2);
    1657    32346007 :     gel(V,i) = gerepileuptoint(av, c);
    1658             :   }
    1659     1439992 :   return V;
    1660             : }
    1661             : 
    1662             : static GEN
    1663      718676 : nxV_polint_center_tree(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1664             : {
    1665      718676 :   long i, j, l, n = lg(P);
    1666      718676 :   GEN mod = gmael(T, lg(T)-1, 1), V, w;
    1667      718676 :   w = cgetg(n, t_VECSMALL);
    1668     2529894 :   for(j=1; j<n; j++) w[j] = lg(gel(vA,j));
    1669      718655 :   l = vecsmall_max(w);
    1670      718655 :   V = cgetg(l, t_POL);
    1671      718630 :   V[1] = mael(vA,1,1);
    1672     5277709 :   for (i=2; i < l; i++)
    1673             :   {
    1674     4559064 :     pari_sp av = avma;
    1675     4559064 :     GEN c, A = cgetg(n, typ(P));
    1676     4558451 :     if (typ(P)==t_VECSMALL)
    1677    12020847 :       for (j=1; j < n; j++) A[j] = i < w[j] ? mael(vA,j,i): 0;
    1678             :     else
    1679     5745226 :       for (j=1; j < n; j++) gel(A,j) = i < w[j] ? gmael(vA,j,i): gen_0;
    1680     4558451 :     c = Fp_center(ZV_chinese_tree(A, P, T, R), mod, m2);
    1681     4559149 :     gel(V,i) = gerepileuptoint(av, c);
    1682             :   }
    1683      718645 :   return ZX_renormalize(V, l);
    1684             : }
    1685             : 
    1686             : static GEN
    1687        4620 : nxCV_polint_center_tree(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1688             : {
    1689        4620 :   long i, j, l = lg(gel(vA,1)), n = lg(P);
    1690        4620 :   GEN A = cgetg(n, t_VEC);
    1691        4620 :   GEN V = cgetg(l, t_COL);
    1692       90929 :   for (i=1; i < l; i++)
    1693             :   {
    1694      335171 :     for (j=1; j < n; j++) gel(A,j) = gmael(vA,j,i);
    1695       86310 :     gel(V,i) = nxV_polint_center_tree(A, P, T, R, m2);
    1696             :   }
    1697        4619 :   return V;
    1698             : }
    1699             : 
    1700             : static GEN
    1701      372854 : polint_chinese(GEN worker, GEN mA, GEN P)
    1702             : {
    1703      372854 :   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      372854 :   if (l == 1) return cgetg(1, t_MAT);
    1709      343802 :   cnt = pending = 0;
    1710      343802 :   n = lg(P);
    1711      343802 :   A = cgetg(n, t_VEC);
    1712      343802 :   va = mkvec(A);
    1713      343802 :   M = cgetg(l, t_MAT);
    1714      343802 :   if (DEBUGLEVEL>4) timer_start(&ti);
    1715      343802 :   if (DEBUGLEVEL>5) err_printf("Start parallel Chinese remainder: ");
    1716      343802 :   mt_queue_start_lim(&pt, worker, l-1);
    1717     1320599 :   for (i=1; i<l || pending; i++)
    1718             :   {
    1719             :     long workid;
    1720     3700404 :     for(j=1; j < n; j++) gel(A,j) = gmael(mA,j,i);
    1721      976797 :     mt_queue_submit(&pt, i, i<l? va: NULL);
    1722      976796 :     done = mt_queue_get(&pt, &workid, &pending);
    1723      976796 :     if (done)
    1724             :     {
    1725      937544 :       gel(M,workid) = done;
    1726      937544 :       if (DEBUGLEVEL>5) err_printf("%ld%% ",(++cnt)*100/(l-1));
    1727             :     }
    1728             :   }
    1729      343802 :   if (DEBUGLEVEL>5) err_printf("\n");
    1730      343802 :   if (DEBUGLEVEL>4) timer_printf(&ti, "nmV_chinese_center");
    1731      343802 :   mt_queue_end(&pt);
    1732      343802 :   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         431 : nxMV_polint_center_tree_seq(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1743             : {
    1744         431 :   long i, j, l = lg(gel(vA,1)), n = lg(P);
    1745         431 :   GEN A = cgetg(n, t_VEC);
    1746         431 :   GEN V = cgetg(l, t_MAT);
    1747        4211 :   for (i=1; i < l; i++)
    1748             :   {
    1749       15317 :     for (j=1; j < n; j++) gel(A,j) = gmael(vA,j,i);
    1750        3780 :     gel(V,i) = nxCV_polint_center_tree(A, P, T, R, m2);
    1751             :   }
    1752         431 :   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      109258 : nmV_polint_center_tree_seq(GEN vA, GEN P, GEN T, GEN R, GEN m2)
    1764             : {
    1765      109258 :   long i, j, l = lg(gel(vA,1)), n = lg(P);
    1766      109258 :   GEN A = cgetg(n, t_VEC);
    1767      109259 :   GEN V = cgetg(l, t_MAT);
    1768      597192 :   for (i=1; i < l; i++)
    1769             :   {
    1770     2746841 :     for (j=1; j < n; j++) gel(A,j) = gmael(vA,j,i);
    1771      487932 :     gel(V,i) = ncV_polint_center_tree(A, P, T, R, m2);
    1772             :   }
    1773      109260 :   return V;
    1774             : }
    1775             : 
    1776             : GEN
    1777      936674 : nmV_polint_center_tree_worker(GEN vA, GEN T, GEN R, GEN P, GEN m2)
    1778             : {
    1779      936674 :   return ncV_polint_center_tree(vA, P, T, R, m2);
    1780             : }
    1781             : 
    1782             : static GEN
    1783      372764 : nmV_polint_center_tree(GEN mA, GEN P, GEN T, GEN R, GEN m2)
    1784             : {
    1785      372764 :   GEN worker = snm_closure(is_entry("_polint_worker"), mkvec4(T, R, P, m2));
    1786      372764 :   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     2383122 : ZX_nv_mod_tree(GEN B, GEN A, GEN T)
    1806             : {
    1807             :   pari_sp av;
    1808     2383122 :   long i, j, l = lg(B), n = lg(A)-1;
    1809     2383122 :   GEN V = cgetg(n+1, t_VEC);
    1810    11276084 :   for (j=1; j <= n; j++)
    1811             :   {
    1812     8893112 :     gel(V, j) = cgetg(l, t_VECSMALL);
    1813     8892982 :     mael(V, j, 1) = B[1]&VARNBITS;
    1814             :   }
    1815     2382972 :   av = avma;
    1816    15533794 :   for (i=2; i < l; i++)
    1817             :   {
    1818    13151169 :     GEN v = Z_ZV_mod_tree(gel(B, i), A, T);
    1819    86896790 :     for (j=1; j <= n; j++)
    1820    73750262 :       mael(V, j, i) = v[j];
    1821    13146528 :     set_avma(av);
    1822             :   }
    1823    11275989 :   for (j=1; j <= n; j++)
    1824     8893345 :     (void) Flx_renormalize(gel(V, j), l);
    1825     2382644 :   return V;
    1826             : }
    1827             : 
    1828             : static GEN
    1829     1190182 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
    1830             : 
    1831             : GEN
    1832       86604 : ZXX_nv_mod_tree(GEN P, GEN xa, GEN T, long w)
    1833             : {
    1834       86604 :   pari_sp av = avma;
    1835       86604 :   long i, j, l = lg(P), n = lg(xa)-1;
    1836       86604 :   GEN V = cgetg(n+1, t_VEC);
    1837      372988 :   for (j=1; j <= n; j++)
    1838             :   {
    1839      286384 :     gel(V, j) = cgetg(l, t_POL);
    1840      286384 :     mael(V, j, 1) = P[1]&VARNBITS;
    1841             :   }
    1842     1195583 :   for (i=2; i < l; i++)
    1843             :   {
    1844     1108979 :     GEN v = ZX_nv_mod_tree(to_ZX(gel(P, i), w), xa, T);
    1845     4830572 :     for (j=1; j <= n; j++)
    1846     3721593 :       gmael(V, j, i) = gel(v,j);
    1847             :   }
    1848      372988 :   for (j=1; j <= n; j++)
    1849      286384 :     (void) FlxX_renormalize(gel(V, j), l);
    1850       86604 :   return gerepilecopy(av, V);
    1851             : }
    1852             : 
    1853             : GEN
    1854        4053 : ZXC_nv_mod_tree(GEN C, GEN xa, GEN T, long w)
    1855             : {
    1856        4053 :   pari_sp av = avma;
    1857        4053 :   long i, j, l = lg(C), n = lg(xa)-1;
    1858        4053 :   GEN V = cgetg(n+1, t_VEC);
    1859       16966 :   for (j = 1; j <= n; j++)
    1860       12913 :     gel(V, j) = cgetg(l, t_COL);
    1861       85248 :   for (i = 1; i < l; i++)
    1862             :   {
    1863       81202 :     GEN v = ZX_nv_mod_tree(to_ZX(gel(C, i), w), xa, T);
    1864      359450 :     for (j = 1; j <= n; j++)
    1865      278255 :       gmael(V, j, i) = gel(v,j);
    1866             :   }
    1867        4046 :   return gerepilecopy(av, V);
    1868             : }
    1869             : 
    1870             : GEN
    1871         431 : ZXM_nv_mod_tree(GEN M, GEN xa, GEN T, long w)
    1872             : {
    1873         431 :   pari_sp av = avma;
    1874         431 :   long i, j, l = lg(M), n = lg(xa)-1;
    1875         431 :   GEN V = cgetg(n+1, t_VEC);
    1876        2086 :   for (j=1; j <= n; j++)
    1877        1655 :     gel(V, j) = cgetg(l, t_MAT);
    1878        4210 :   for (i=1; i < l; i++)
    1879             :   {
    1880        3779 :     GEN v = ZXC_nv_mod_tree(gel(M, i), xa, T, w);
    1881       15312 :     for (j=1; j <= n; j++)
    1882       11533 :       gmael(V, j, i) = gel(v,j);
    1883             :   }
    1884         431 :   return gerepilecopy(av, V);
    1885             : }
    1886             : 
    1887             : GEN
    1888     1171119 : ZV_nv_mod_tree(GEN B, GEN A, GEN T)
    1889             : {
    1890             :   pari_sp av;
    1891     1171119 :   long i, j, l = lg(B), n = lg(A)-1;
    1892     1171119 :   GEN V = cgetg(n+1, t_VEC);
    1893     5806834 :   for (j=1; j <= n; j++) gel(V, j) = cgetg(l, t_VECSMALL);
    1894     1171020 :   av = avma;
    1895    42656457 :   for (i=1; i < l; i++)
    1896             :   {
    1897    41489982 :     GEN v = Z_ZV_mod_tree(gel(B, i), A, T);
    1898   219528285 :     for (j=1; j <= n; j++) mael(V, j, i) = v[j];
    1899    41460961 :     set_avma(av);
    1900             :   }
    1901     1166475 :   return V;
    1902             : }
    1903             : 
    1904             : static GEN
    1905      176828 : ZM_nv_mod_tree_t(GEN M, GEN xa, GEN T, long 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      865007 :   for (j=1; j <= n; j++) gel(V, j) = cgetg(l, t);
    1911     1347701 :   for (i=1; i < l; i++)
    1912             :   {
    1913     1170885 :     GEN v = ZV_nv_mod_tree(gel(M, i), xa, T);
    1914     5806389 :     for (j=1; j <= n; j++) gmael(V, j, i) = gel(v,j);
    1915             :   }
    1916      176816 :   return gerepilecopy(av, V);
    1917             : }
    1918             : 
    1919             : GEN
    1920      171371 : ZM_nv_mod_tree(GEN M, GEN xa, GEN T)
    1921      171371 : { return ZM_nv_mod_tree_t(M, xa, T, t_MAT); }
    1922             : 
    1923             : GEN
    1924        5457 : ZVV_nv_mod_tree(GEN M, GEN xa, GEN T)
    1925        5457 : { return ZM_nv_mod_tree_t(M, xa, T, t_VEC); }
    1926             : 
    1927             : static GEN
    1928     2493378 : ZV_sqr(GEN z)
    1929             : {
    1930     2493378 :   long i,l = lg(z);
    1931     2493378 :   GEN x = cgetg(l, t_VEC);
    1932     2493380 :   if (typ(z)==t_VECSMALL)
    1933     5833037 :     for (i=1; i<l; i++) gel(x,i) = sqru(z[i]);
    1934             :   else
    1935     4458569 :     for (i=1; i<l; i++) gel(x,i) = sqri(gel(z,i));
    1936     2493366 :   return x;
    1937             : }
    1938             : 
    1939             : static GEN
    1940    12819404 : ZT_sqr(GEN x)
    1941             : {
    1942    12819404 :   if (typ(x) == t_INT) return sqri(x);
    1943    16787114 :   pari_APPLY_type(t_VEC, ZT_sqr(gel(x,i)))
    1944             : }
    1945             : 
    1946             : static GEN
    1947     2493374 : ZV_invdivexact(GEN y, GEN x)
    1948             : {
    1949     2493374 :   long i, l = lg(y);
    1950     2493374 :   GEN z = cgetg(l,t_VEC);
    1951     2493371 :   if (typ(x)==t_VECSMALL)
    1952     5832599 :     for (i=1; i<l; i++)
    1953             :     {
    1954     4646279 :       pari_sp av = avma;
    1955     4646279 :       ulong a = Fl_inv(umodiu(diviuexact(gel(y,i),x[i]), x[i]), x[i]);
    1956     4646606 :       set_avma(av); gel(z,i) = utoi(a);
    1957             :     }
    1958             :   else
    1959     4458585 :     for (i=1; i<l; i++)
    1960     3151532 :       gel(z,i) = Fp_inv(diviiexact(gel(y,i), gel(x,i)), gel(x,i));
    1961     2493373 :   return z;
    1962             : }
    1963             : 
    1964             : /* P t_VECSMALL or t_VEC of t_INT  */
    1965             : GEN
    1966     2493372 : ZV_chinesetree(GEN P, GEN T)
    1967             : {
    1968     2493372 :   GEN T2 = ZT_sqr(T), P2 = ZV_sqr(P);
    1969     2493371 :   GEN mod = gmael(T,lg(T)-1,1);
    1970     2493371 :   return ZV_invdivexact(Z_ZV_mod_tree(mod, P2, T2), P);
    1971             : }
    1972             : 
    1973             : static GEN
    1974      957738 : gc_chinese(pari_sp av, GEN T, GEN a, GEN *pt_mod)
    1975             : {
    1976      957738 :   if (!pt_mod)
    1977       12377 :     return gerepileupto(av, a);
    1978             :   else
    1979             :   {
    1980      945361 :     GEN mod = gmael(T, lg(T)-1, 1);
    1981      945361 :     gerepileall(av, 2, &a, &mod);
    1982      945361 :     *pt_mod = mod;
    1983      945361 :     return a;
    1984             :   }
    1985             : }
    1986             : 
    1987             : GEN
    1988      154225 : ZV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    1989             : {
    1990      154225 :   pari_sp av = avma;
    1991      154225 :   GEN T = ZV_producttree(P);
    1992      154225 :   GEN R = ZV_chinesetree(P, T);
    1993      154225 :   GEN a = ZV_chinese_tree(A, P, T, R);
    1994      154225 :   GEN mod = gmael(T, lg(T)-1, 1);
    1995      154225 :   GEN ca = Fp_center(a, mod, shifti(mod,-1));
    1996      154225 :   return gc_chinese(av, T, ca, pt_mod);
    1997             : }
    1998             : 
    1999             : GEN
    2000        5141 : ZV_chinese(GEN A, GEN P, GEN *pt_mod)
    2001             : {
    2002        5141 :   pari_sp av = avma;
    2003        5141 :   GEN T = ZV_producttree(P);
    2004        5141 :   GEN R = ZV_chinesetree(P, T);
    2005        5141 :   GEN a = ZV_chinese_tree(A, P, T, R);
    2006        5141 :   return gc_chinese(av, T, a, pt_mod);
    2007             : }
    2008             : 
    2009             : GEN
    2010      217086 : nxV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2011             : {
    2012      217086 :   pari_sp av = avma;
    2013      217086 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2014      217086 :   GEN a = nxV_polint_center_tree(A, P, T, R, m2);
    2015      217085 :   return gerepileupto(av, a);
    2016             : }
    2017             : 
    2018             : GEN
    2019      415254 : nxV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2020             : {
    2021      415254 :   pari_sp av = avma;
    2022      415254 :   GEN T = ZV_producttree(P);
    2023      415254 :   GEN R = ZV_chinesetree(P, T);
    2024      415255 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2025      415255 :   GEN a = nxV_polint_center_tree(A, P, T, R, m2);
    2026      415255 :   return gc_chinese(av, T, a, pt_mod);
    2027             : }
    2028             : 
    2029             : GEN
    2030       10263 : ncV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2031             : {
    2032       10263 :   pari_sp av = avma;
    2033       10263 :   GEN T = ZV_producttree(P);
    2034       10263 :   GEN R = ZV_chinesetree(P, T);
    2035       10263 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2036       10263 :   GEN a = ncV_polint_center_tree(A, P, T, R, m2);
    2037       10263 :   return gc_chinese(av, T, a, pt_mod);
    2038             : }
    2039             : 
    2040             : GEN
    2041        5457 : ncV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2042             : {
    2043        5457 :   pari_sp av = avma;
    2044        5457 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2045        5457 :   GEN a = ncV_polint_center_tree(A, P, T, R, m2);
    2046        5457 :   return gerepileupto(av, a);
    2047             : }
    2048             : 
    2049             : GEN
    2050           0 : nmV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2051             : {
    2052           0 :   pari_sp av = avma;
    2053           0 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2054           0 :   GEN a = nmV_polint_center_tree(A, P, T, R, m2);
    2055           0 :   return gerepileupto(av, a);
    2056             : }
    2057             : 
    2058             : GEN
    2059      109259 : nmV_chinese_center_tree_seq(GEN A, GEN P, GEN T, GEN R)
    2060             : {
    2061      109259 :   pari_sp av = avma;
    2062      109259 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2063      109258 :   GEN a = nmV_polint_center_tree_seq(A, P, T, R, m2);
    2064      109260 :   return gerepileupto(av, a);
    2065             : }
    2066             : 
    2067             : GEN
    2068      372764 : nmV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2069             : {
    2070      372764 :   pari_sp av = avma;
    2071      372764 :   GEN T = ZV_producttree(P);
    2072      372764 :   GEN R = ZV_chinesetree(P, T);
    2073      372764 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2074      372764 :   GEN a = nmV_polint_center_tree(A, P, T, R, m2);
    2075      372764 :   return gc_chinese(av, T, a, pt_mod);
    2076             : }
    2077             : 
    2078             : GEN
    2079           0 : nxCV_chinese_center_tree(GEN A, GEN P, GEN T, GEN R)
    2080             : {
    2081           0 :   pari_sp av = avma;
    2082           0 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2083           0 :   GEN a = nxCV_polint_center_tree(A, P, T, R, m2);
    2084           0 :   return gerepileupto(av, a);
    2085             : }
    2086             : 
    2087             : GEN
    2088           0 : nxCV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2089             : {
    2090           0 :   pari_sp av = avma;
    2091           0 :   GEN T = ZV_producttree(P);
    2092           0 :   GEN R = ZV_chinesetree(P, T);
    2093           0 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2094           0 :   GEN a = nxCV_polint_center_tree(A, P, T, R, m2);
    2095           0 :   return gc_chinese(av, T, a, pt_mod);
    2096             : }
    2097             : 
    2098             : GEN
    2099         431 : nxMV_chinese_center_tree_seq(GEN A, GEN P, GEN T, GEN R)
    2100             : {
    2101         431 :   pari_sp av = avma;
    2102         431 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2103         431 :   GEN a = nxMV_polint_center_tree_seq(A, P, T, R, m2);
    2104         431 :   return gerepileupto(av, a);
    2105             : }
    2106             : 
    2107             : GEN
    2108          90 : nxMV_chinese_center(GEN A, GEN P, GEN *pt_mod)
    2109             : {
    2110          90 :   pari_sp av = avma;
    2111          90 :   GEN T = ZV_producttree(P);
    2112          90 :   GEN R = ZV_chinesetree(P, T);
    2113          90 :   GEN m2 = shifti(gmael(T, lg(T)-1, 1), -1);
    2114          90 :   GEN a = nxMV_polint_center_tree(A, P, T, R, m2);
    2115          90 :   return gc_chinese(av, T, a, pt_mod);
    2116             : }
    2117             : 
    2118             : /**********************************************************************
    2119             :  **                    Powering  over (Z/NZ)^*, small N              **
    2120             :  **********************************************************************/
    2121             : 
    2122             : /* 2^n mod p; assume n > 1 */
    2123             : static ulong
    2124    11831544 : Fl_2powu_pre(ulong n, ulong p, ulong pi)
    2125             : {
    2126    11831544 :   ulong y = 2;
    2127    11831544 :   int j = 1+bfffo(n);
    2128             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
    2129    11831544 :   n<<=j; j = BITS_IN_LONG-j; /* first bit is now implicit */
    2130   519390688 :   for (; j; n<<=1,j--)
    2131             :   {
    2132   507608401 :     y = Fl_sqr_pre(y,p,pi);
    2133   507619544 :     if (n & HIGHBIT) y = Fl_double(y, p);
    2134             :   }
    2135    11782287 :   return y;
    2136             : }
    2137             : 
    2138             : /* 2^n mod p; assume n > 1 and !(p & HIGHMASK) */
    2139             : static ulong
    2140     4303812 : Fl_2powu(ulong n, ulong p)
    2141             : {
    2142     4303812 :   ulong y = 2;
    2143     4303812 :   int j = 1+bfffo(n);
    2144             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
    2145     4303812 :   n<<=j; j = BITS_IN_LONG-j; /* first bit is now implicit */
    2146    25442977 :   for (; j; n<<=1,j--)
    2147             :   {
    2148    21139092 :     y = (y*y) % p;
    2149    21139092 :     if (n & HIGHBIT) y = Fl_double(y, p);
    2150             :   }
    2151     4303885 :   return y;
    2152             : }
    2153             : 
    2154             : /* allow pi = 0 */
    2155             : ulong
    2156   150477043 : Fl_powu_pre(ulong x, ulong n0, ulong p, ulong pi)
    2157             : {
    2158             :   ulong y, z, n;
    2159   150477043 :   if (!pi) return Fl_powu(x, n0, p);
    2160   148036709 :   if (n0 <= 1)
    2161             :   { /* frequent special cases */
    2162    10106881 :     if (n0 == 1) return x;
    2163      105077 :     if (n0 == 0) return 1;
    2164             :   }
    2165   137929845 :   if (x <= 2)
    2166             :   {
    2167    12090084 :     if (x == 2) return Fl_2powu_pre(n0, p, pi);
    2168      257742 :     return x; /* 0 or 1 */
    2169             :   }
    2170   125839761 :   y = 1; z = x; n = n0;
    2171             :   for(;;)
    2172             :   {
    2173   643323134 :     if (n&1) y = Fl_mul_pre(y,z,p,pi);
    2174   643233732 :     n>>=1; if (!n) return y;
    2175   517121412 :     z = Fl_sqr_pre(z,p,pi);
    2176             :   }
    2177             : }
    2178             : 
    2179             : ulong
    2180   139640594 : Fl_powu(ulong x, ulong n0, ulong p)
    2181             : {
    2182             :   ulong y, z, n;
    2183   139640594 :   if (n0 <= 2)
    2184             :   { /* frequent special cases */
    2185    65783781 :     if (n0 == 2) return Fl_sqr(x,p);
    2186    32191049 :     if (n0 == 1) return x;
    2187     1918104 :     if (n0 == 0) return 1;
    2188             :   }
    2189    73830936 :   if (x <= 1) return x; /* 0 or 1 */
    2190    73396866 :   if (p & HIGHMASK)
    2191     7900836 :     return Fl_powu_pre(x, n0, p, get_Fl_red(p));
    2192    65496030 :   if (x == 2) return Fl_2powu(n0, p);
    2193    61192221 :   y = 1; z = x; n = n0;
    2194             :   for(;;)
    2195             :   {
    2196   263002430 :     if (n&1) y = (y*z) % p;
    2197   263002430 :     n>>=1; if (!n) return y;
    2198   201810209 :     z = (z*z) % p;
    2199             :   }
    2200             : }
    2201             : 
    2202             : /* Reduce data dependency to maximize internal parallelism; allow pi = 0 */
    2203             : GEN
    2204    12785912 : Fl_powers_pre(ulong x, long n, ulong p, ulong pi)
    2205             : {
    2206             :   long i, k;
    2207    12785912 :   GEN z = cgetg(n + 2, t_VECSMALL);
    2208    12779654 :   z[1] = 1; if (n == 0) return z;
    2209    12779654 :   z[2] = x;
    2210    12779654 :   if (pi)
    2211             :   {
    2212    90085621 :     for (i = 3, k=2; i <= n; i+=2, k++)
    2213             :     {
    2214    77514234 :       z[i] = Fl_sqr_pre(z[k], p, pi);
    2215    77515193 :       z[i+1] = Fl_mul_pre(z[k], z[k+1], p, pi);
    2216             :     }
    2217    12571387 :     if (i==n+1) z[i] = Fl_sqr_pre(z[k], p, pi);
    2218             :   }
    2219      213042 :   else if (p & HIGHMASK)
    2220             :   {
    2221           0 :     for (i = 3, k=2; i <= n; i+=2, k++)
    2222             :     {
    2223           0 :       z[i] = Fl_sqr(z[k], p);
    2224           0 :       z[i+1] = Fl_mul(z[k], z[k+1], p);
    2225             :     }
    2226           0 :     if (i==n+1) z[i] = Fl_sqr(z[k], p);
    2227             :   }
    2228             :   else
    2229   400523781 :     for (i = 2; i <= n; i++) z[i+1] = (z[i] * x) % p;
    2230    12785951 :   return z;
    2231             : }
    2232             : 
    2233             : GEN
    2234      296036 : Fl_powers(ulong x, long n, ulong p)
    2235             : {
    2236      296036 :   return Fl_powers_pre(x, n, p, (p & HIGHMASK)? get_Fl_red(p): 0);
    2237             : }
    2238             : 
    2239             : /**********************************************************************
    2240             :  **                    Powering  over (Z/NZ)^*, large N              **
    2241             :  **********************************************************************/
    2242             : typedef struct muldata {
    2243             :   GEN (*sqr)(void * E, GEN x);
    2244             :   GEN (*mul)(void * E, GEN x, GEN y);
    2245             :   GEN (*mul2)(void * E, GEN x);
    2246             : } muldata;
    2247             : 
    2248             : /* modified Barrett reduction with one fold */
    2249             : /* See Fast Modular Reduction, W. Hasenplaugh, G. Gaubatz, V. Gopal, ARITH 18 */
    2250             : 
    2251             : static GEN
    2252       15115 : Fp_invmBarrett(GEN p, long s)
    2253             : {
    2254       15115 :   GEN R, Q = dvmdii(int2n(3*s),p,&R);
    2255       15115 :   return mkvec2(Q,R);
    2256             : }
    2257             : 
    2258             : /* a <= (N-1)^2, 2^(2s-2) <= N < 2^(2s). Return 0 <= r < N such that
    2259             :  * a = r (mod N) */
    2260             : static GEN
    2261     9212146 : Fp_rem_mBarrett(GEN a, GEN B, long s, GEN N)
    2262             : {
    2263     9212146 :   pari_sp av = avma;
    2264     9212146 :   GEN P = gel(B, 1), Q = gel(B, 2); /* 2^(3s) = P N + Q, 0 <= Q < N */
    2265     9212146 :   long t = expi(P)+1; /* 2^(t-1) <= P < 2^t */
    2266     9212146 :   GEN u = shifti(a, -3*s), v = remi2n(a, 3*s); /* a = 2^(3s)u + v */
    2267     9212146 :   GEN A = addii(v, mulii(Q,u)); /* 0 <= A < 2^(3s+1) */
    2268     9212146 :   GEN q = shifti(mulii(shifti(A, t-3*s), P), -t); /* A/N - 4 < q <= A/N */
    2269     9212146 :   GEN r = subii(A, mulii(q, N));
    2270     9212146 :   GEN sr= subii(r,N);     /* 0 <= r < 4*N */
    2271     9212146 :   if (signe(sr)<0) return gerepileuptoint(av, r);
    2272     5028278 :   r=sr; sr = subii(r,N);  /* 0 <= r < 3*N */
    2273     5028278 :   if (signe(sr)<0) return gerepileuptoint(av, r);
    2274      186063 :   r=sr; sr = subii(r,N);  /* 0 <= r < 2*N */
    2275      186063 :   return gerepileuptoint(av, signe(sr)>=0 ? sr:r);
    2276             : }
    2277             : 
    2278             : /* Montgomery reduction */
    2279             : 
    2280             : INLINE ulong
    2281      671609 : init_montdata(GEN N) { return (ulong) -invmod2BIL(mod2BIL(N)); }
    2282             : 
    2283             : struct montred
    2284             : {
    2285             :   GEN N;
    2286             :   ulong inv;
    2287             : };
    2288             : 
    2289             : /* Montgomery reduction */
    2290             : static GEN
    2291    67902770 : _sqr_montred(void * E, GEN x)
    2292             : {
    2293    67902770 :   struct montred * D = (struct montred *) E;
    2294    67902770 :   return red_montgomery(sqri(x), D->N, D->inv);
    2295             : }
    2296             : 
    2297             : /* Montgomery reduction */
    2298             : static GEN
    2299     6957141 : _mul_montred(void * E, GEN x, GEN y)
    2300             : {
    2301     6957141 :   struct montred * D = (struct montred *) E;
    2302     6957141 :   return red_montgomery(mulii(x, y), D->N, D->inv);
    2303             : }
    2304             : 
    2305             : static GEN
    2306    11063105 : _mul2_montred(void * E, GEN x)
    2307             : {
    2308    11063105 :   struct montred * D = (struct montred *) E;
    2309    11063105 :   GEN z = shifti(_sqr_montred(E, x), 1);
    2310    11060284 :   long l = lgefint(D->N);
    2311    11698370 :   while (lgefint(z) > l) z = subii(z, D->N);
    2312    11060423 :   return z;
    2313             : }
    2314             : 
    2315             : static GEN
    2316    23215879 : _sqr_remii(void* N, GEN x)
    2317    23215879 : { return remii(sqri(x), (GEN) N); }
    2318             : 
    2319             : static GEN
    2320     1514005 : _mul_remii(void* N, GEN x, GEN y)
    2321     1514005 : { return remii(mulii(x, y), (GEN) N); }
    2322             : 
    2323             : static GEN
    2324     3201174 : _mul2_remii(void* N, GEN x)
    2325     3201174 : { return Fp_double(_sqr_remii(N, x), (GEN)N); }
    2326             : 
    2327             : struct redbarrett
    2328             : {
    2329             :   GEN iM, N;
    2330             :   long s;
    2331             : };
    2332             : 
    2333             : static GEN
    2334     8437776 : _sqr_remiibar(void *E, GEN x)
    2335             : {
    2336     8437776 :   struct redbarrett * D = (struct redbarrett *) E;
    2337     8437776 :   return Fp_rem_mBarrett(sqri(x), D->iM, D->s, D->N);
    2338             : }
    2339             : 
    2340             : static GEN
    2341      774370 : _mul_remiibar(void *E, GEN x, GEN y)
    2342             : {
    2343      774370 :   struct redbarrett * D = (struct redbarrett *) E;
    2344      774370 :   return Fp_rem_mBarrett(mulii(x, y), D->iM, D->s, D->N);
    2345             : }
    2346             : 
    2347             : static GEN
    2348     2079460 : _mul2_remiibar(void *E, GEN x)
    2349             : {
    2350     2079460 :   struct redbarrett * D = (struct redbarrett *) E;
    2351     2079460 :   return Fp_double(_sqr_remiibar(E, x), D->N);
    2352             : }
    2353             : 
    2354             : static long
    2355      866708 : Fp_select_red(GEN *y, ulong k, GEN N, long lN, muldata *D, void **pt_E)
    2356             : {
    2357      866708 :   if (lN >= Fp_POW_BARRETT_LIMIT && (k==0 || ((double)k)*expi(*y) > 2 + expi(N)))
    2358             :   {
    2359       15115 :     struct redbarrett * E = (struct redbarrett *) stack_malloc(sizeof(struct redbarrett));
    2360       15115 :     D->sqr = &_sqr_remiibar;
    2361       15115 :     D->mul = &_mul_remiibar;
    2362       15115 :     D->mul2 = &_mul2_remiibar;
    2363       15115 :     E->N = N;
    2364       15115 :     E->s = 1+(expi(N)>>1);
    2365       15115 :     E->iM = Fp_invmBarrett(N, E->s);
    2366       15115 :     *pt_E = (void*) E;
    2367       15115 :     return 0;
    2368             :   }
    2369      851593 :   else if (mod2(N) && lN < Fp_POW_REDC_LIMIT)
    2370             :   {
    2371      671610 :     struct montred * E = (struct montred *) stack_malloc(sizeof(struct montred));
    2372      671607 :     *y = remii(shifti(*y, bit_accuracy(lN)), N);
    2373      671609 :     D->sqr = &_sqr_montred;
    2374      671609 :     D->mul = &_mul_montred;
    2375      671609 :     D->mul2 = &_mul2_montred;
    2376      671609 :     E->N = N;
    2377      671609 :     E->inv = init_montdata(N);
    2378      671608 :     *pt_E = (void*) E;
    2379      671608 :     return 1;
    2380             :   }
    2381             :   else
    2382             :   {
    2383      179986 :     D->sqr = &_sqr_remii;
    2384      179986 :     D->mul = &_mul_remii;
    2385      179986 :     D->mul2 = &_mul2_remii;
    2386      179986 :     *pt_E = (void*) N;
    2387      179986 :     return 0;
    2388             :   }
    2389             : }
    2390             : 
    2391             : GEN
    2392     3060645 : Fp_powu(GEN A, ulong k, GEN N)
    2393             : {
    2394     3060645 :   long lN = lgefint(N);
    2395             :   int base_is_2, use_montgomery;
    2396             :   muldata D;
    2397             :   void *E;
    2398             :   pari_sp av;
    2399             : 
    2400     3060645 :   if (lN == 3) {
    2401     1608416 :     ulong n = uel(N,2);
    2402     1608416 :     return utoi( Fl_powu(umodiu(A, n), k, n) );
    2403             :   }
    2404     1452229 :   if (k <= 2)
    2405             :   { /* frequent special cases */
    2406      853874 :     if (k == 2) return Fp_sqr(A,N);
    2407      301238 :     if (k == 1) return A;
    2408           0 :     if (k == 0) return gen_1;
    2409             :   }
    2410      598355 :   av = avma; A = modii(A,N);
    2411      598355 :   base_is_2 = 0;
    2412      598355 :   if (lgefint(A) == 3) switch(A[2])
    2413             :   {
    2414         887 :     case 1: set_avma(av); return gen_1;
    2415       33985 :     case 2:  base_is_2 = 1; break;
    2416             :   }
    2417             : 
    2418             :   /* TODO: Move this out of here and use for general modular computations */
    2419      597468 :   use_montgomery = Fp_select_red(&A, k, N, lN, &D, &E);
    2420      597468 :   if (base_is_2)
    2421       33985 :     A = gen_powu_fold_i(A, k, E, D.sqr, D.mul2);
    2422             :   else
    2423      563483 :     A = gen_powu_i(A, k, E, D.sqr, D.mul);
    2424      597468 :   if (use_montgomery)
    2425             :   {
    2426      500898 :     A = red_montgomery(A, N, ((struct montred *) E)->inv);
    2427      500898 :     if (cmpii(A, N) >= 0) A = subii(A,N);
    2428             :   }
    2429      597468 :   return gerepileuptoint(av, A);
    2430             : }
    2431             : 
    2432             : GEN
    2433       29436 : Fp_pows(GEN A, long k, GEN N)
    2434             : {
    2435       29436 :   if (lgefint(N) == 3) {
    2436       14978 :     ulong n = N[2];
    2437       14978 :     ulong a = umodiu(A, n);
    2438       14978 :     if (k < 0) {
    2439         133 :       a = Fl_inv(a, n);
    2440         133 :       k = -k;
    2441             :     }
    2442       14978 :     return utoi( Fl_powu(a, (ulong)k, n) );
    2443             :   }
    2444       14458 :   if (k < 0) { A = Fp_inv(A, N); k = -k; };
    2445       14458 :   return Fp_powu(A, (ulong)k, N);
    2446             : }
    2447             : 
    2448             : /* A^K mod N */
    2449             : GEN
    2450    36197890 : Fp_pow(GEN A, GEN K, GEN N)
    2451             : {
    2452             :   pari_sp av;
    2453    36197890 :   long s, lN = lgefint(N), sA, sy;
    2454             :   int base_is_2, use_montgomery;
    2455             :   GEN y;
    2456             :   muldata D;
    2457             :   void *E;
    2458             : 
    2459    36197890 :   s = signe(K);
    2460    36197890 :   if (!s) return dvdii(A,N)? gen_0: gen_1;
    2461    35167083 :   if (lN == 3 && lgefint(K) == 3)
    2462             :   {
    2463    34453885 :     ulong n = N[2], a = umodiu(A, n);
    2464    34454206 :     if (s < 0) a = Fl_inv(a, n);
    2465    34454294 :     if (a <= 1) return utoi(a); /* 0 or 1 */
    2466    30921567 :     return utoi(Fl_powu(a, uel(K,2), n));
    2467             :   }
    2468             : 
    2469      713198 :   av = avma;
    2470      713198 :   if (s < 0) y = Fp_inv(A,N);
    2471             :   else
    2472             :   {
    2473      711262 :     y = modii(A,N);
    2474      711487 :     if (!signe(y)) { set_avma(av); return gen_0; }
    2475             :   }
    2476      713421 :   if (lgefint(K) == 3) return gerepileuptoint(av, Fp_powu(y, K[2], N));
    2477             : 
    2478      269451 :   base_is_2 = 0;
    2479      269451 :   sy = abscmpii(y, shifti(N,-1)) > 0;
    2480      269440 :   if (sy) y = subii(N,y);
    2481      269445 :   sA = sy && mod2(K);
    2482      269445 :   if (lgefint(y) == 3) switch(y[2])
    2483             :   {
    2484         207 :     case 1:  set_avma(av); return sA ? subis(N,1): gen_1;
    2485      145810 :     case 2:  base_is_2 = 1; break;
    2486             :   }
    2487             : 
    2488             :   /* TODO: Move this out of here and use for general modular computations */
    2489      269238 :   use_montgomery = Fp_select_red(&y, 0UL, N, lN, &D, &E);
    2490      269248 :   if (base_is_2)
    2491      145822 :     y = gen_pow_fold_i(y, K, E, D.sqr, D.mul2);
    2492             :   else
    2493      123426 :     y = gen_pow_i(y, K, E, D.sqr, D.mul);
    2494      269259 :   if (use_montgomery)
    2495             :   {
    2496      170717 :     y = red_montgomery(y, N, ((struct montred *) E)->inv);
    2497      170718 :     if (cmpii(y,N) >= 0) y = subii(y,N);
    2498             :   }
    2499      269257 :   if (sA) y = subii(N, y);
    2500      269257 :   return gerepileuptoint(av,y);
    2501             : }
    2502             : 
    2503             : static GEN
    2504    14128250 : _Fp_mul(void *E, GEN x, GEN y) { return Fp_mul(x,y,(GEN)E); }
    2505             : static GEN
    2506     8134253 : _Fp_sqr(void *E, GEN x) { return Fp_sqr(x,(GEN)E); }
    2507             : static GEN
    2508       47162 : _Fp_one(void *E) { (void) E; return gen_1; }
    2509             : 
    2510             : GEN
    2511         105 : Fp_pow_init(GEN x, GEN n, long k, GEN p)
    2512         105 : { return gen_pow_init(x, n, k, (void*)p, &_Fp_sqr, &_Fp_mul); }
    2513             : 
    2514             : GEN
    2515       43694 : Fp_pow_table(GEN R, GEN n, GEN p)
    2516       43694 : { return gen_pow_table(R, n, (void*)p, &_Fp_one, &_Fp_mul); }
    2517             : 
    2518             : GEN
    2519        5931 : Fp_powers(GEN x, long n, GEN p)
    2520             : {
    2521        5931 :   if (lgefint(p) == 3)
    2522        2463 :     return Flv_to_ZV(Fl_powers(umodiu(x, uel(p, 2)), n, uel(p, 2)));
    2523        3468 :   return gen_powers(x, n, 1, (void*)p, _Fp_sqr, _Fp_mul, _Fp_one);
    2524             : }
    2525             : 
    2526             : GEN
    2527         497 : FpV_prod(GEN V, GEN p) { return gen_product(V, (void *)p, &_Fp_mul); }
    2528             : 
    2529             : static GEN
    2530    27865747 : _Fp_pow(void *E, GEN x, GEN n) { return Fp_pow(x,n,(GEN)E); }
    2531             : static GEN
    2532         147 : _Fp_rand(void *E) { return addiu(randomi(subiu((GEN)E,1)),1); }
    2533             : 
    2534             : static GEN Fp_easylog(void *E, GEN a, GEN g, GEN ord);
    2535             : static const struct bb_group Fp_star={_Fp_mul,_Fp_pow,_Fp_rand,hash_GEN,
    2536             :                                       equalii,equali1,Fp_easylog};
    2537             : 
    2538             : static GEN
    2539      901624 : _Fp_red(void *E, GEN x) { return Fp_red(x, (GEN)E); }
    2540             : static GEN
    2541     1205873 : _Fp_add(void *E, GEN x, GEN y) { (void) E; return addii(x,y); }
    2542             : static GEN
    2543     1115040 : _Fp_neg(void *E, GEN x) { (void) E; return negi(x); }
    2544             : static GEN
    2545      585377 : _Fp_rmul(void *E, GEN x, GEN y) { (void) E; return mulii(x,y); }
    2546             : static GEN
    2547       34435 : _Fp_inv(void *E, GEN x) { return Fp_inv(x,(GEN)E); }
    2548             : static int
    2549      261718 : _Fp_equal0(GEN x) { return signe(x)==0; }
    2550             : static GEN
    2551       19126 : _Fp_s(void *E, long x) { (void) E; return stoi(x); }
    2552             : 
    2553             : static const struct bb_field Fp_field={_Fp_red,_Fp_add,_Fp_rmul,_Fp_neg,
    2554             :                                         _Fp_inv,_Fp_equal0,_Fp_s};
    2555             : 
    2556        6967 : const struct bb_field *get_Fp_field(void **E, GEN p)
    2557        6967 : { *E = (void*)p; return &Fp_field; }
    2558             : 
    2559             : /*********************************************************************/
    2560             : /**               ORDER of INTEGERMOD x  in  (Z/nZ)*                **/
    2561             : /*********************************************************************/
    2562             : ulong
    2563      482515 : Fl_order(ulong a, ulong o, ulong p)
    2564             : {
    2565      482515 :   pari_sp av = avma;
    2566             :   GEN m, P, E;
    2567             :   long i;
    2568      482515 :   if (a==1) return 1;
    2569      439601 :   if (!o) o = p-1;
    2570      439601 :   m = factoru(o);
    2571      439601 :   P = gel(m,1);
    2572      439601 :   E = gel(m,2);
    2573     1252877 :   for (i = lg(P)-1; i; i--)
    2574             :   {
    2575      813276 :     ulong j, l = P[i], e = E[i], t = o / upowuu(l,e), y = Fl_powu(a, t, p);
    2576      813276 :     if (y == 1) o = t;
    2577      773868 :     else for (j = 1; j < e; j++)
    2578             :     {
    2579      385722 :       y = Fl_powu(y, l, p);
    2580      385722 :       if (y == 1) { o = t *  upowuu(l, j); break; }
    2581             :     }
    2582             :   }
    2583      439601 :   return gc_ulong(av, o);
    2584             : }
    2585             : 
    2586             : /*Find the exact order of a assuming a^o==1*/
    2587             : GEN
    2588       73985 : Fp_order(GEN a, GEN o, GEN p) {
    2589       73985 :   if (lgefint(p) == 3 && (!o || typ(o) == t_INT))
    2590             :   {
    2591          21 :     ulong pp = p[2], oo = (o && lgefint(o)==3)? uel(o,2): pp-1;
    2592          21 :     return utoi( Fl_order(umodiu(a, pp), oo, pp) );
    2593             :   }
    2594       73964 :   return gen_order(a, o, (void*)p, &Fp_star);
    2595             : }
    2596             : GEN
    2597          70 : Fp_factored_order(GEN a, GEN o, GEN p)
    2598          70 : { return gen_factored_order(a, o, (void*)p, &Fp_star); }
    2599             : 
    2600             : /* return order of a mod p^e, e > 0, pe = p^e */
    2601             : static GEN
    2602          70 : Zp_order(GEN a, GEN p, long e, GEN pe)
    2603             : {
    2604             :   GEN ap, op;
    2605          70 :   if (absequaliu(p, 2))
    2606             :   {
    2607          56 :     if (e == 1) return gen_1;
    2608          56 :     if (e == 2) return mod4(a) == 1? gen_1: gen_2;
    2609          49 :     if (mod4(a) == 1) op = gen_1; else { op = gen_2; a = Fp_sqr(a, pe); }
    2610             :   } else {
    2611          14 :     ap = (e == 1)? a: remii(a,p);
    2612          14 :     op = Fp_order(ap, subiu(p,1), p);
    2613          14 :     if (e == 1) return op;
    2614           0 :     a = Fp_pow(a, op, pe); /* 1 mod p */
    2615             :   }
    2616          49 :   if (equali1(a)) return op;
    2617           7 :   return mulii(op, powiu(p, e - Z_pval(subiu(a,1), p)));
    2618             : }
    2619             : 
    2620             : GEN
    2621          63 : znorder(GEN x, GEN o)
    2622             : {
    2623          63 :   pari_sp av = avma;
    2624             :   GEN b, a;
    2625             : 
    2626          63 :   if (typ(x) != t_INTMOD) pari_err_TYPE("znorder [t_INTMOD expected]",x);
    2627          56 :   b = gel(x,1); a = gel(x,2);
    2628          56 :   if (!equali1(gcdii(a,b))) pari_err_COPRIME("znorder", a,b);
    2629          49 :   if (!o)
    2630             :   {
    2631          35 :     GEN fa = Z_factor(b), P = gel(fa,1), E = gel(fa,2);
    2632          35 :     long i, l = lg(P);
    2633          35 :     o = gen_1;
    2634          70 :     for (i = 1; i < l; i++)
    2635             :     {
    2636          35 :       GEN p = gel(P,i);
    2637          35 :       long e = itos(gel(E,i));
    2638             : 
    2639          35 :       if (l == 2)
    2640          35 :         o = Zp_order(a, p, e, b);
    2641             :       else {
    2642           0 :         GEN pe = powiu(p,e);
    2643           0 :         o = lcmii(o, Zp_order(remii(a,pe), p, e, pe));
    2644             :       }
    2645             :     }
    2646          35 :     return gerepileuptoint(av, o);
    2647             :   }
    2648          14 :   return Fp_order(a, o, b);
    2649             : }
    2650             : 
    2651             : /*********************************************************************/
    2652             : /**               DISCRETE LOGARITHM  in  (Z/nZ)*                   **/
    2653             : /*********************************************************************/
    2654             : static GEN
    2655       60294 : Fp_log_halfgcd(ulong bnd, GEN C, GEN g, GEN p)
    2656             : {
    2657       60294 :   pari_sp av = avma;
    2658             :   GEN h1, h2, F, G;
    2659       60294 :   if (!Fp_ratlift(g,p,C,shifti(C,-1),&h1,&h2)) return gc_NULL(av);
    2660       36335 :   if ((F = Z_issmooth_fact(h1, bnd)) && (G = Z_issmooth_fact(h2, bnd)))
    2661             :   {
    2662         126 :     GEN M = cgetg(3, t_MAT);
    2663         126 :     gel(M,1) = vecsmall_concat(gel(F, 1),gel(G, 1));
    2664         126 :     gel(M,2) = vecsmall_concat(gel(F, 2),zv_neg_inplace(gel(G, 2)));
    2665         126 :     return gerepileupto(av, M);
    2666             :   }
    2667       36209 :   return gc_NULL(av);
    2668             : }
    2669             : 
    2670             : static GEN
    2671       60294 : Fp_log_find_rel(GEN b, ulong bnd, GEN C, GEN p, GEN *g, long *e)
    2672             : {
    2673             :   GEN rel;
    2674       60294 :   do { (*e)++; *g = Fp_mul(*g, b, p); rel = Fp_log_halfgcd(bnd, C, *g, p); }
    2675       60294 :   while (!rel);
    2676         126 :   return rel;
    2677             : }
    2678             : 
    2679             : struct Fp_log_rel
    2680             : {
    2681             :   GEN rel;
    2682             :   ulong prmax;
    2683             :   long nbrel, nbmax, nbgen;
    2684             : };
    2685             : 
    2686             : static long
    2687       59731 : tr(long i) { return odd(i) ? (i+1)>>1: -(i>>1); }
    2688             : 
    2689             : static long
    2690      169813 : rt(long i) { return i>0 ? 2*i-1: -2*i; }
    2691             : 
    2692             : /* add u^e */
    2693             : static void
    2694        2163 : addifsmooth1(struct Fp_log_rel *r, GEN z, long u, long e)
    2695             : {
    2696        2163 :   pari_sp av = avma;
    2697        2163 :   long off = r->prmax+1;
    2698        2163 :   GEN F = cgetg(3, t_MAT);
    2699        2163 :   gel(F,1) = vecsmall_append(gel(z,1), off+rt(u));
    2700        2163 :   gel(F,2) = vecsmall_append(gel(z,2), e);
    2701        2163 :   gel(r->rel,++r->nbrel) = gerepileupto(av, F);
    2702        2163 : }
    2703             : 
    2704             : /* add u^-1 v^-1 */
    2705             : static void
    2706       83825 : addifsmooth2(struct Fp_log_rel *r, GEN z, long u, long v)
    2707             : {
    2708       83825 :   pari_sp av = avma;
    2709       83825 :   long off = r->prmax+1;
    2710       83825 :   GEN P = mkvecsmall2(off+rt(u),off+rt(v)), E = mkvecsmall2(-1,-1);
    2711       83825 :   GEN F = cgetg(3, t_MAT);
    2712       83825 :   gel(F,1) = vecsmall_concat(gel(z,1), P);
    2713       83825 :   gel(F,2) = vecsmall_concat(gel(z,2), E);
    2714       83825 :   gel(r->rel,++r->nbrel) = gerepileupto(av, F);
    2715       83825 : }
    2716             : 
    2717             : /* Let p=C^2+c
    2718             :  * Solve h = (C+x)*(C+a)-p = 0 [mod l]
    2719             :  * h= -c+x*(C+a)+C*a = 0  [mod l]
    2720             :  * x = (c-C*a)/(C+a) [mod l]
    2721             :  * h = -c+C*(x+a)+a*x */
    2722             : GEN
    2723       30247 : Fp_log_sieve_worker(long a, long prmax, GEN C, GEN c, GEN Ci, GEN ci, GEN pi, GEN sz)
    2724             : {
    2725       30247 :   pari_sp ltop = avma;
    2726       30247 :   long i, j, th, n = lg(pi)-1, rel = 1, ab = labs(a), ae;
    2727       30247 :   GEN sieve = zero_zv(2*ab+2)+1+ab;
    2728       30254 :   GEN L = cgetg(1+2*ab+2, t_VEC);
    2729       30251 :   pari_sp av = avma;
    2730       30251 :   GEN z, h = addis(C,a);
    2731       30234 :   if ((z = Z_issmooth_fact(h, prmax)))
    2732             :   {
    2733        2169 :     gel(L, rel++) = mkvec2(z, mkvecsmall3(1, a, -1));
    2734        2168 :     av = avma;
    2735             :   }
    2736    12476360 :   for (i=1; i<=n; i++)
    2737             :   {
    2738    12447929 :     ulong li = pi[i], s = sz[i], al = smodss(a,li);
    2739    12431060 :     ulong iv = Fl_invsafe(Fl_add(Ci[i],al,li),li);
    2740             :     long u;
    2741    12738534 :     if (!iv) continue;
    2742    12424333 :     u = Fl_add(Fl_mul(Fl_sub(ci[i],Fl_mul(Ci[i],al,li),li), iv ,li),ab%li,li)-ab;
    2743    46223278 :     for(j = u; j<=ab; j+=li) sieve[j] += s;
    2744             :   }
    2745       28431 :   if (a)
    2746             :   {
    2747       30145 :     long e = expi(mulis(C,a));
    2748       30168 :     th = e - expu(e) - 1;
    2749          54 :   } else th = -1;
    2750       30243 :   ae = a>=0 ? ab-1: ab;
    2751    15520273 :   for (j = 1-ab; j <= ae; j++)
    2752    15489941 :     if (sieve[j]>=th)
    2753             :     {
    2754      108890 :       GEN h = absi(addis(subii(mulis(C,a+j),c), a*j));
    2755      108758 :       if ((z = Z_issmooth_fact(h, prmax)))
    2756             :       {
    2757      106491 :         gel(L, rel++) = mkvec2(z, mkvecsmall3(2, a, j));
    2758      106576 :         av = avma;
    2759        2294 :       } else set_avma(av);
    2760             :     }
    2761             :   /* j = a */
    2762       30332 :   if (sieve[a]>=th)
    2763             :   {
    2764         448 :     GEN h = absi(addiu(subii(mulis(C,2*a),c), a*a));
    2765         448 :     if ((z = Z_issmooth_fact(h, prmax)))
    2766         364 :       gel(L, rel++) = mkvec2(z, mkvecsmall3(1, a, -2));
    2767             :   }
    2768       30332 :   setlg(L, rel); return gerepilecopy(ltop, L);
    2769             : }
    2770             : 
    2771             : static long
    2772          63 : Fp_log_sieve(struct Fp_log_rel *r, GEN C, GEN c, GEN Ci, GEN ci, GEN pi, GEN sz)
    2773             : {
    2774             :   struct pari_mt pt;
    2775          63 :   long i, j, nb = 0;
    2776          63 :   GEN worker = snm_closure(is_entry("_Fp_log_sieve_worker"),
    2777             :                mkvecn(7, utoi(r->prmax), C, c, Ci, ci, pi, sz));
    2778          63 :   long running, pending = 0;
    2779          63 :   GEN W = zerovec(r->nbgen);
    2780          63 :   mt_queue_start_lim(&pt, worker, r->nbgen);
    2781       30459 :   for (i = 0; (running = (i < r->nbgen)) || pending; i++)
    2782             :   {
    2783             :     GEN done;
    2784             :     long idx;
    2785       30396 :     mt_queue_submit(&pt, i, running ? mkvec(stoi(tr(i))): NULL);
    2786       30396 :     done = mt_queue_get(&pt, &idx, &pending);
    2787       30396 :     if (!done || lg(done)==1) continue;
    2788       27636 :     gel(W, idx+1) = done;
    2789       27636 :     nb += lg(done)-1;
    2790       27636 :     if (DEBUGLEVEL && (i&127)==0)
    2791           0 :       err_printf("%ld%% ",100*nb/r->nbmax);
    2792             :   }
    2793          63 :   mt_queue_end(&pt);
    2794       26362 :   for(j = 1; j <= r->nbgen && r->nbrel < r->nbmax; j++)
    2795             :   {
    2796             :     long ll, m;
    2797       26299 :     GEN L = gel(W,j);
    2798       26299 :     if (isintzero(L)) continue;
    2799       23681 :     ll = lg(L);
    2800      109669 :     for (m=1; m<ll && r->nbrel < r->nbmax ; m++)
    2801             :     {
    2802       85988 :       GEN Lm = gel(L,m), h = gel(Lm, 1), v = gel(Lm, 2);
    2803       85988 :       if (v[1] == 1)
    2804        2163 :         addifsmooth1(r, h, v[2], v[3]);
    2805             :       else
    2806       83825 :         addifsmooth2(r, h, v[2], v[3]);
    2807             :     }
    2808             :   }
    2809          63 :   return j;
    2810             : }
    2811             : 
    2812             : static GEN
    2813         837 : ECP_psi(GEN x, GEN y)
    2814             : {
    2815         837 :   long prec = realprec(x);
    2816         837 :   GEN lx = glog(x, prec), ly = glog(y, prec);
    2817         837 :   GEN u = gdiv(lx, ly);
    2818         837 :   return gpow(u, gneg(u),prec);
    2819             : }
    2820             : 
    2821             : struct computeG
    2822             : {
    2823             :   GEN C;
    2824             :   long bnd, nbi;
    2825             : };
    2826             : 
    2827             : static GEN
    2828         837 : _computeG(void *E, GEN gen)
    2829             : {
    2830         837 :   struct computeG * d = (struct computeG *) E;
    2831         837 :   GEN ps = ECP_psi(gmul(gen,d->C), stoi(d->bnd));
    2832         837 :   return gsub(gmul(gsqr(gen),ps),gmulgs(gaddgs(gen,d->nbi),3));
    2833             : }
    2834             : 
    2835             : static long
    2836          63 : compute_nbgen(GEN C, long bnd, long nbi)
    2837             : {
    2838             :   struct computeG d;
    2839          63 :   d.C = shifti(C, 1);
    2840          63 :   d.bnd = bnd;
    2841          63 :   d.nbi = nbi;
    2842          63 :   return itos(ground(zbrent((void*)&d, _computeG, gen_2, stoi(bnd), DEFAULTPREC)));
    2843             : }
    2844             : 
    2845             : static GEN
    2846        1714 : _psi(void*E, GEN y)
    2847             : {
    2848        1714 :   GEN lx = (GEN) E;
    2849        1714 :   long prec = realprec(lx);
    2850        1714 :   GEN ly = glog(y, prec);
    2851        1714 :   GEN u = gdiv(lx, ly);
    2852        1714 :   return gsub(gdiv(y ,ly), gpow(u, u, prec));
    2853             : }
    2854             : 
    2855             : static GEN
    2856          63 : opt_param(GEN x, long prec)
    2857             : {
    2858          63 :   return zbrent((void*)glog(x,prec), _psi, gen_2, x, prec);
    2859             : }
    2860             : 
    2861             : static GEN
    2862          63 : check_kernel(long nbg, long N, long prmax, GEN C, GEN M, GEN p, GEN m)
    2863             : {
    2864          63 :   pari_sp av = avma;
    2865          63 :   long lM = lg(M)-1, nbcol = lM;
    2866          63 :   long tbs = maxss(1, expu(nbg/expi(m)));
    2867             :   for (;;)
    2868          42 :   {
    2869         105 :     GEN K = FpMs_leftkernel_elt_col(M, nbcol, N, m);
    2870             :     GEN tab;
    2871         105 :     long i, f=0;
    2872         105 :     long l = lg(K), lm = lgefint(m);
    2873         105 :     GEN idx = diviiexact(subiu(p,1),m), g;
    2874             :     pari_timer ti;
    2875         105 :     if (DEBUGLEVEL) timer_start(&ti);
    2876         210 :     for(i=1; i<l; i++)
    2877         210 :       if (signe(gel(K,i)))
    2878         105 :         break;
    2879         105 :     g = Fp_pow(utoi(i), idx, p);
    2880         105 :     tab = Fp_pow_init(g, p, tbs, p);
    2881         105 :     K = FpC_Fp_mul(K, Fp_inv(gel(K,i), m), m);
    2882      121520 :     for(i=1; i<l; i++)
    2883             :     {
    2884      121415 :       GEN k = gel(K,i);
    2885      121415 :       GEN j = i<=prmax ? utoi(i): addis(C,tr(i-(prmax+1)));
    2886      121415 :       if (signe(k)==0 || !equalii(Fp_pow_table(tab, k, p), Fp_pow(j, idx, p)))
    2887       82369 :         gel(K,i) = cgetineg(lm);
    2888             :       else
    2889       39046 :         f++;
    2890             :     }
    2891         105 :     if (DEBUGLEVEL) timer_printf(&ti,"found %ld/%ld logs", f, nbg);
    2892         105 :     if(f > (nbg>>1)) return gerepileupto(av, K);
    2893       10024 :     for(i=1; i<=nbcol; i++)
    2894             :     {
    2895        9982 :       long a = 1+random_Fl(lM);
    2896        9982 :       swap(gel(M,a),gel(M,i));
    2897             :     }
    2898          42 :     if (4*nbcol>5*nbg) nbcol = nbcol*9/10;
    2899             :   }
    2900             : }
    2901             : 
    2902             : static GEN
    2903         126 : Fp_log_find_ind(GEN a, GEN K, long prmax, GEN C, GEN p, GEN m)
    2904             : {
    2905         126 :   pari_sp av=avma;
    2906         126 :   GEN aa = gen_1;
    2907         126 :   long AV = 0;
    2908             :   for(;;)
    2909           0 :   {
    2910         126 :     GEN A = Fp_log_find_rel(a, prmax, C, p, &aa, &AV);
    2911         126 :     GEN F = gel(A,1), E = gel(A,2);
    2912         126 :     GEN Ao = gen_0;
    2913         126 :     long i, l = lg(F);
    2914         769 :     for(i=1; i<l; i++)
    2915             :     {
    2916         643 :       GEN Ki = gel(K,F[i]);
    2917         643 :       if (signe(Ki)<0) break;
    2918         643 :       Ao = addii(Ao, mulis(Ki, E[i]));
    2919             :     }
    2920         126 :     if (i==l) return Fp_divu(Ao, AV, m);
    2921           0 :     aa = gerepileuptoint(av, aa);
    2922             :   }
    2923             : }
    2924             : 
    2925             : static GEN
    2926          63 : Fp_log_index(GEN a, GEN b, GEN m, GEN p)
    2927             : {
    2928          63 :   pari_sp av = avma, av2;
    2929          63 :   long i, j, nbi, nbr = 0, nbrow, nbg;
    2930             :   GEN C, c, Ci, ci, pi, pr, sz, l, Ao, Bo, K, d, p_1;
    2931             :   pari_timer ti;
    2932             :   struct Fp_log_rel r;
    2933          63 :   ulong bnds = itou(roundr_safe(opt_param(sqrti(p),DEFAULTPREC)));
    2934          63 :   ulong bnd = 4*bnds;
    2935          63 :   if (!bnds || cmpii(sqru(bnds),m)>=0) return NULL;
    2936             : 
    2937          63 :   p_1 = subiu(p,1);
    2938          63 :   if (!is_pm1(gcdii(m,diviiexact(p_1,m))))
    2939           0 :     m = diviiexact(p_1, Z_ppo(p_1, m));
    2940          63 :   pr = primes_upto_zv(bnd);
    2941          63 :   nbi = lg(pr)-1;
    2942          63 :   C = sqrtremi(p, &c);
    2943          63 :   av2 = avma;
    2944       12796 :   for (i = 1; i <= nbi; ++i)
    2945             :   {
    2946       12733 :     ulong lp = pr[i];
    2947       26894 :     while (lp <= bnd)
    2948             :     {
    2949       14161 :       nbr++;
    2950       14161 :       lp *= pr[i];
    2951             :     }
    2952             :   }
    2953          63 :   pi = cgetg(nbr+1,t_VECSMALL);
    2954          63 :   Ci = cgetg(nbr+1,t_VECSMALL);
    2955          63 :   ci = cgetg(nbr+1,t_VECSMALL);
    2956          63 :   sz = cgetg(nbr+1,t_VECSMALL);
    2957       12796 :   for (i = 1, j = 1; i <= nbi; ++i)
    2958             :   {
    2959       12733 :     ulong lp = pr[i], sp = expu(2*lp-1);
    2960       26894 :     while (lp <= bnd)
    2961             :     {
    2962       14161 :       pi[j] = lp;
    2963       14161 :       Ci[j] = umodiu(C, lp);
    2964       14161 :       ci[j] = umodiu(c, lp);
    2965       14161 :       sz[j] = sp;
    2966       14161 :       lp *= pr[i];
    2967       14161 :       j++;
    2968             :     }
    2969             :   }
    2970          63 :   r.nbrel = 0;
    2971          63 :   r.nbgen = compute_nbgen(C, bnd, nbi);
    2972          63 :   r.nbmax = 2*(nbi+r.nbgen);
    2973          63 :   r.rel = cgetg(r.nbmax+1,t_VEC);
    2974          63 :   r.prmax = pr[nbi];
    2975          63 :   if (DEBUGLEVEL)
    2976             :   {
    2977           0 :     err_printf("bnd=%lu Size FB=%ld extra gen=%ld \n", bnd, nbi, r.nbgen);
    2978           0 :     timer_start(&ti);
    2979             :   }
    2980          63 :   nbg = Fp_log_sieve(&r, C, c, Ci, ci, pi, sz);
    2981          63 :   nbrow = r.prmax + nbg;
    2982          63 :   if (DEBUGLEVEL)
    2983             :   {
    2984           0 :     err_printf("\n");
    2985           0 :     timer_printf(&ti," %ld relations, %ld generators", r.nbrel, nbi+nbg);
    2986             :   }
    2987          63 :   setlg(r.rel,r.nbrel+1);
    2988          63 :   r.rel = gerepilecopy(av2, r.rel);
    2989          63 :   K = check_kernel(nbi+nbrow-r.prmax, nbrow, r.prmax, C, r.rel, p, m);
    2990          63 :   if (DEBUGLEVEL) timer_start(&ti);
    2991          63 :   Ao = Fp_log_find_ind(a, K, r.prmax, C, p, m);
    2992          63 :   if (DEBUGLEVEL) timer_printf(&ti," log element");
    2993          63 :   Bo = Fp_log_find_ind(b, K, r.prmax, C, p, m);
    2994          63 :   if (DEBUGLEVEL) timer_printf(&ti," log generator");
    2995          63 :   d = gcdii(Ao,Bo);
    2996          63 :   l = Fp_div(diviiexact(Ao, d) ,diviiexact(Bo, d), m);
    2997          63 :   if (!equalii(a,Fp_pow(b,l,p))) pari_err_BUG("Fp_log_index");
    2998          63 :   return gerepileuptoint(av, l);
    2999             : }
    3000             : 
    3001             : static int
    3002     4663599 : Fp_log_use_index(long e, long p)
    3003             : {
    3004     4663599 :   return (e >= 27 && 20*(p+6)<=e*e);
    3005             : }
    3006             : 
    3007             : /* Trivial cases a = 1, -1. Return x s.t. g^x = a or [] if no such x exist */
    3008             : static GEN
    3009     8463544 : Fp_easylog(void *E, GEN a, GEN g, GEN ord)
    3010             : {
    3011     8463544 :   pari_sp av = avma;
    3012     8463544 :   GEN p = (GEN)E;
    3013             :   /* assume a reduced mod p, p not necessarily prime */
    3014     8463544 :   if (equali1(a)) return gen_0;
    3015             :   /* p > 2 */
    3016     5439695 :   if (equalii(subiu(p,1), a))  /* -1 */
    3017             :   {
    3018             :     pari_sp av2;
    3019             :     GEN t;
    3020     1323159 :     ord = get_arith_Z(ord);
    3021     1323159 :     if (mpodd(ord)) { set_avma(av); return cgetg(1, t_VEC); } /* no solution */
    3022     1323145 :     t = shifti(ord,-1); /* only possible solution */
    3023     1323145 :     av2 = avma;
    3024     1323145 :     if (!equalii(Fp_pow(g, t, p), a)) { set_avma(av); return cgetg(1, t_VEC); }
    3025     1323118 :     set_avma(av2); return gerepileuptoint(av, t);
    3026             :   }
    3027     4116536 :   if (typ(ord)==t_INT && BPSW_psp(p) && Fp_log_use_index(expi(ord),expi(p)))
    3028          63 :     return Fp_log_index(a, g, ord, p);
    3029     4116473 :   return gc_NULL(av); /* not easy */
    3030             : }
    3031             : 
    3032             : GEN
    3033     3926307 : Fp_log(GEN a, GEN g, GEN ord, GEN p)
    3034             : {
    3035     3926307 :   GEN v = get_arith_ZZM(ord);
    3036     3926284 :   GEN F = gmael(v,2,1);
    3037     3926284 :   long lF = lg(F)-1, lmax;
    3038     3926284 :   if (lF == 0) return equali1(a)? gen_0: cgetg(1, t_VEC);
    3039     3926256 :   lmax = expi(gel(F,lF));
    3040     3926254 :   if (BPSW_psp(p) && Fp_log_use_index(lmax,expi(p)))
    3041          91 :     v = mkvec2(gel(v,1),ZM_famat_limit(gel(v,2),int2n(27)));
    3042     3926249 :   return gen_PH_log(a,g,v,(void*)p,&Fp_star);
    3043             : }
    3044             : 
    3045             : /* assume !(p & HIGHMASK) */
    3046             : static ulong
    3047      132629 : Fl_log_naive(ulong a, ulong g, ulong ord, ulong p)
    3048             : {
    3049      132629 :   ulong i, h=1;
    3050      364848 :   for (i = 0; i < ord; i++, h = (h * g) % p)
    3051      364848 :     if (a==h) return i;
    3052           0 :   return ~0UL;
    3053             : }
    3054             : 
    3055             : static ulong
    3056       25088 : Fl_log_naive_pre(ulong a, ulong g, ulong ord, ulong p, ulong pi)
    3057             : {
    3058       25088 :   ulong i, h=1;
    3059       64285 :   for (i = 0; i < ord; i++, h = Fl_mul_pre(h, g, p, pi))
    3060       64285 :     if (a==h) return i;
    3061           0 :   return ~0UL;
    3062             : }
    3063             : 
    3064             : static ulong
    3065           0 : Fl_log_Fp(ulong a, ulong g, ulong ord, ulong p)
    3066             : {
    3067           0 :   pari_sp av = avma;
    3068           0 :   GEN r = Fp_log(utoi(a),utoi(g),utoi(ord),utoi(p));
    3069           0 :   return gc_ulong(av, typ(r)==t_INT ? itou(r): ~0UL);
    3070             : }
    3071             : 
    3072             : /* allow pi = 0 */
    3073             : ulong
    3074       25487 : Fl_log_pre(ulong a, ulong g, ulong ord, ulong p, ulong pi)
    3075             : {
    3076       25487 :   if (!pi) return Fl_log(a, g, ord, p);
    3077       25088 :   if (ord <= 200) return Fl_log_naive_pre(a, g, ord, p, pi);
    3078           0 :   return Fl_log_Fp(a, g, ord, p);
    3079             : }
    3080             : 
    3081             : ulong
    3082      132629 : Fl_log(ulong a, ulong g, ulong ord, ulong p)
    3083             : {
    3084      132629 :   if (ord <= 200)
    3085           0 :     return (p&HIGHMASK)? Fl_log_naive_pre(a, g, ord, p, get_Fl_red(p))
    3086      132629 :                        : Fl_log_naive(a, g, ord, p);
    3087           0 :   return Fl_log_Fp(a, g, ord, p);
    3088             : }
    3089             : 
    3090             : /* find x such that h = g^x mod N > 1, N = prod_{i <= l} P[i]^E[i], P[i] prime.
    3091             :  * PHI[l] = eulerphi(N / P[l]^E[l]).   Destroys P/E */
    3092             : static GEN
    3093         126 : znlog_rec(GEN h, GEN g, GEN N, GEN P, GEN E, GEN PHI)
    3094             : {
    3095         126 :   long l = lg(P) - 1, e = E[l];
    3096         126 :   GEN p = gel(P, l), phi = gel(PHI,l), pe = e == 1? p: powiu(p, e);
    3097             :   GEN a,b, hp,gp, hpe,gpe, ogpe; /* = order(g mod p^e) | p^(e-1)(p-1) */
    3098             : 
    3099         126 :   if (l == 1) {
    3100          98 :     hpe = h;
    3101          98 :     gpe = g;
    3102             :   } else {
    3103          28 :     hpe = modii(h, pe);
    3104          28 :     gpe = modii(g, pe);
    3105             :   }
    3106         126 :   if (e == 1) {
    3107          42 :     hp = hpe;
    3108          42 :     gp = gpe;
    3109             :   } else {
    3110          84 :     hp = remii(hpe, p);
    3111          84 :     gp = remii(gpe, p);
    3112             :   }
    3113         126 :   if (hp == gen_0 || gp == gen_0) return NULL;
    3114         105 :   if (absequaliu(p, 2))
    3115             :   {
    3116          35 :     GEN n = int2n(e);
    3117          35 :     ogpe = Zp_order(gpe, gen_2, e, n);
    3118          35 :     a = Fp_log(hpe, gpe, ogpe, n);
    3119          35 :     if (typ(a) != t_INT) return NULL;
    3120             :   }
    3121             :   else
    3122             :   { /* Avoid black box groups: (Z/p^2)^* / (Z/p)^* ~ (Z/pZ, +), where DL
    3123             :        is trivial */
    3124             :     /* [order(gp), factor(order(gp))] */
    3125          70 :     GEN v = Fp_factored_order(gp, subiu(p,1), p);
    3126          70 :     GEN ogp = gel(v,1);
    3127          70 :     if (!equali1(Fp_pow(hp, ogp, p))) return NULL;
    3128          70 :     a = Fp_log(hp, gp, v, p);
    3129          70 :     if (typ(a) != t_INT) return NULL;
    3130          70 :     if (e == 1) ogpe = ogp;
    3131             :     else
    3132             :     { /* find a s.t. g^a = h (mod p^e), p odd prime, e > 0, (h,p) = 1 */
    3133             :       /* use p-adic log: O(log p + e) mul*/
    3134             :       long vpogpe, vpohpe;
    3135             : 
    3136          28 :       hpe = Fp_mul(hpe, Fp_pow(gpe, negi(a), pe), pe);
    3137          28 :       gpe = Fp_pow(gpe, ogp, pe);
    3138             :       /* g,h = 1 mod p; compute b s.t. h = g^b */
    3139             : 
    3140             :       /* v_p(order g mod pe) */
    3141          28 :       vpogpe = equali1(gpe)? 0: e - Z_pval(subiu(gpe,1), p);
    3142             :       /* v_p(order h mod pe) */
    3143          28 :       vpohpe = equali1(hpe)? 0: e - Z_pval(subiu(hpe,1), p);
    3144          28 :       if (vpohpe > vpogpe) return NULL;
    3145             : 
    3146          28 :       ogpe = mulii(ogp, powiu(p, vpogpe)); /* order g mod p^e */
    3147          28 :       if (is_pm1(gpe)) return is_pm1(hpe)? a: NULL;
    3148          28 :       b = gdiv(Qp_log(cvtop(hpe, p, e)), Qp_log(cvtop(gpe, p, e)));
    3149          28 :       a = addii(a, mulii(ogp, padic_to_Q(b)));
    3150             :     }
    3151             :   }
    3152             :   /* gp^a = hp => x = a mod ogpe => generalized Pohlig-Hellman strategy */
    3153          91 :   if (l == 1) return a;
    3154             : 
    3155          28 :   N = diviiexact(N, pe); /* make N coprime to p */
    3156          28 :   h = Fp_mul(h, Fp_pow(g, modii(negi(a), phi), N), N);
    3157          28 :   g = Fp_pow(g, modii(ogpe, phi), N);
    3158          28 :   setlg(P, l); /* remove last element */
    3159          28 :   setlg(E, l);
    3160          28 :   b = znlog_rec(h, g, N, P, E, PHI);
    3161          28 :   if (!b) return NULL;
    3162          28 :   return addmulii(a, b, ogpe);
    3163             : }
    3164             : 
    3165             : static GEN
    3166          98 : get_PHI(GEN P, GEN E)
    3167             : {
    3168          98 :   long i, l = lg(P);
    3169          98 :   GEN PHI = cgetg(l, t_VEC);
    3170          98 :   gel(PHI,1) = gen_1;
    3171         126 :   for (i=1; i<l-1; i++)
    3172             :   {
    3173          28 :     GEN t, p = gel(P,i);
    3174          28 :     long e = E[i];
    3175          28 :     t = mulii(powiu(p, e-1), subiu(p,1));
    3176          28 :     if (i > 1) t = mulii(t, gel(PHI,i));
    3177          28 :     gel(PHI,i+1) = t;
    3178             :   }
    3179          98 :   return PHI;
    3180             : }
    3181             : 
    3182             : GEN
    3183         238 : znlog(GEN h, GEN g, GEN o)
    3184             : {
    3185         238 :   pari_sp av = avma;
    3186             :   GEN N, fa, P, E, x;
    3187         238 :   switch (typ(g))
    3188             :   {
    3189          28 :     case t_PADIC:
    3190             :     {
    3191          28 :       GEN p = gel(g,2);
    3192          28 :       long v = valp(g);
    3193          28 :       if (v < 0) pari_err_DIM("znlog");
    3194          28 :       if (v > 0) {
    3195           0 :         long k = gvaluation(h, p);
    3196           0 :         if (k % v) return cgetg(1,t_VEC);
    3197           0 :         k /= v;
    3198           0 :         if (!gequal(h, gpowgs(g,k))) { set_avma(av); return cgetg(1,t_VEC); }
    3199           0 :         return gc_stoi(av, k);
    3200             :       }
    3201          28 :       N = gel(g,3);
    3202          28 :       g = Rg_to_Fp(g, N);
    3203          28 :       break;
    3204             :     }
    3205         203 :     case t_INTMOD:
    3206         203 :       N = gel(g,1);
    3207         203 :       g = gel(g,2); break;
    3208           7 :     default: pari_err_TYPE("znlog", g);
    3209             :       return NULL; /* LCOV_EXCL_LINE */
    3210             :   }
    3211         231 :   if (equali1(N)) { set_avma(av); return gen_0; }
    3212         231 :   h = Rg_to_Fp(h, N);
    3213         224 :   if (o) return gerepileupto(av, Fp_log(h, g, o, N));
    3214          98 :   fa = Z_factor(N);
    3215          98 :   P = gel(fa,1);
    3216          98 :   E = vec_to_vecsmall(gel(fa,2));
    3217          98 :   x = znlog_rec(h, g, N, P, E, get_PHI(P,E));
    3218          98 :   if (!x) { set_avma(av); return cgetg(1,t_VEC); }
    3219          63 :   return gerepileuptoint(av, x);
    3220             : }
    3221             : 
    3222             : GEN
    3223      173327 : Fp_sqrtn(GEN a, GEN n, GEN p, GEN *zeta)
    3224             : {
    3225      173327 :   if (lgefint(p)==3)
    3226             :   {
    3227      172893 :     long nn = itos_or_0(n);
    3228      172893 :     if (nn)
    3229             :     {
    3230      172893 :       ulong pp = p[2];
    3231             :       ulong uz;
    3232      172893 :       ulong r = Fl_sqrtn(umodiu(a,pp),nn,pp, zeta ? &uz:NULL);
    3233      172872 :       if (r==ULONG_MAX) return NULL;
    3234      172816 :       if (zeta) *zeta = utoi(uz);
    3235      172816 :       return utoi(r);
    3236             :     }
    3237             :   }
    3238         434 :   a = modii(a,p);
    3239         434 :   if (!signe(a))
    3240             :   {
    3241           0 :     if (zeta) *zeta = gen_1;
    3242           0 :     if (signe(n) < 0) pari_err_INV("Fp_sqrtn", mkintmod(gen_0,p));
    3243           0 :     return gen_0;
    3244             :   }
    3245         434 :   if (absequaliu(n,2))
    3246             :   {
    3247         238 :     if (zeta) *zeta = subiu(p,1);
    3248         238 :     return signe(n) > 0 ? Fp_sqrt(a,p): Fp_sqrt(Fp_inv(a, p),p);
    3249             :   }
    3250         196 :   return gen_Shanks_sqrtn(a,n,subiu(p,1),zeta,(void*)p,&Fp_star);
    3251             : }
    3252             : 
    3253             : /*********************************************************************/
    3254             : /**                              FACTORIAL                          **/
    3255             : /*********************************************************************/
    3256             : GEN
    3257       90605 : mulu_interval_step(ulong a, ulong b, ulong step)
    3258             : {
    3259       90605 :   pari_sp av = avma;
    3260             :   ulong k, l, N, n;
    3261             :   long lx;
    3262             :   GEN x;
    3263             : 
    3264       90605 :   if (!a) return gen_0;
    3265       90605 :   if (step == 1) return mulu_interval(a, b);
    3266       90605 :   n = 1 + (b-a) / step;
    3267       90605 :   b -= (b-a) % step;
    3268       90605 :   if (n < 61)
    3269             :   {
    3270       89220 :     if (n == 1) return utoipos(a);
    3271       68678 :     x = muluu(a,a+step); if (n == 2) return x;
    3272      538885 :     for (k=a+2*step; k<=b; k+=step) x = mului(k,x);
    3273       53809 :     return gerepileuptoint(av, x);
    3274             :   }
    3275             :   /* step | b-a */
    3276        1385 :   lx = 1; x = cgetg(2 + n/2, t_VEC);
    3277        1384 :   N = b + a;
    3278        1384 :   for (k = a;; k += step)
    3279             :   {
    3280      227455 :     l = N - k; if (l <= k) break;
    3281      226071 :     gel(x,lx++) = muluu(k,l);
    3282             :   }
    3283        1384 :   if (l == k) gel(x,lx++) = utoipos(k);
    3284        1384 :   setlg(x, lx);
    3285        1384 :   return gerepileuptoint(av, ZV_prod(x));
    3286             : }
    3287             : /* return a * (a+1) * ... * b. Assume a <= b  [ note: factoring out powers of 2
    3288             :  * first is slower ... ] */
    3289             : GEN
    3290      158920 : mulu_interval(ulong a, ulong b)
    3291             : {
    3292      158920 :   pari_sp av = avma;
    3293             :   ulong k, l, N, n;
    3294             :   long lx;
    3295             :   GEN x;
    3296             : 
    3297      158920 :   if (!a) return gen_0;
    3298      158920 :   n = b - a + 1;
    3299      158920 :   if (n < 61)
    3300             :   {
    3301      158194 :     if (n == 1) return utoipos(a);
    3302      107878 :     x = muluu(a,a+1); if (n == 2) return x;
    3303      403909 :     for (k=a+2; k<b; k++) x = mului(k,x);
    3304             :     /* avoid k <= b: broken if b = ULONG_MAX */
    3305       93777 :     return gerepileuptoint(av, mului(b,x));
    3306             :   }
    3307         726 :   lx = 1; x = cgetg(2 + n/2, t_VEC);
    3308         728 :   N = b + a;
    3309         728 :   for (k = a;; k++)
    3310             :   {
    3311       27491 :     l = N - k; if (l <= k) break;
    3312       26762 :     gel(x,lx++) = muluu(k,l);
    3313             :   }
    3314         729 :   if (l == k) gel(x,lx++) = utoipos(k);
    3315         729 :   setlg(x, lx);
    3316         730 :   return gerepileuptoint(av, ZV_prod(x));
    3317             : }
    3318             : GEN
    3319         560 : muls_interval(long a, long b)
    3320             : {
    3321         560 :   pari_sp av = avma;
    3322         560 :   long lx, k, l, N, n = b - a + 1;
    3323             :   GEN x;
    3324             : 
    3325         560 :   if (a <= 0 && b >= 0) return gen_0;
    3326         287 :   if (n < 61)
    3327             :   {
    3328         287 :     x = stoi(a);
    3329         511 :     for (k=a+1; k<=b; k++) x = mulsi(k,x);
    3330         287 :     return gerepileuptoint(av, x);
    3331             :   }
    3332           0 :   lx = 1; x = cgetg(2 + n/2, t_VEC);
    3333           0 :   N = b + a;
    3334           0 :   for (k = a;; k++)
    3335             :   {
    3336           0 :     l = N - k; if (l <= k) break;
    3337           0 :     gel(x,lx++) = mulss(k,l);
    3338             :   }
    3339           0 :   if (l == k) gel(x,lx++) = stoi(k);
    3340           0 :   setlg(x, lx);
    3341           0 :   return gerepileuptoint(av, ZV_prod(x));
    3342             : }
    3343             : 
    3344             : GEN
    3345         105 : mpprimorial(long n)
    3346             : {
    3347         105 :   pari_sp av = avma;
    3348         105 :   if (n <= 12) switch(n)
    3349             :   {
    3350          14 :     case 0: case 1: return gen_1;
    3351           7 :     case 2: return gen_2;
    3352          14 :     case 3: case 4: return utoipos(6);
    3353          14 :     case 5: case 6: return utoipos(30);
    3354          28 :     case 7: case 8: case 9: case 10: return utoipos(210);
    3355          14 :     case 11: case 12: return utoipos(2310);
    3356           7 :     default: pari_err_DOMAIN("primorial", "argument","<",gen_0,stoi(n));
    3357             :   }
    3358           7 :   return gerepileuptoint(av, zv_prod_Z(primes_upto_zv(n)));
    3359             : }
    3360             : 
    3361             : GEN
    3362      496578 : mpfact(long n)
    3363             : {
    3364      496578 :   pari_sp av = avma;
    3365             :   GEN a, v;
    3366             :   long k;
    3367      496578 :   if (n <= 12) switch(n)
    3368             :   {
    3369      428654 :     case 0: case 1: return gen_1;
    3370       24338 :     case 2: return gen_2;
    3371        3388 :     case 3: return utoipos(6);
    3372        4145 :     case 4: return utoipos(24);
    3373        2887 :     case 5: return utoipos(120);
    3374        2556 :     case 6: return utoipos(720);
    3375        2448 :     case 7: return utoipos(5040);
    3376        2437 :     case 8: return utoipos(40320);
    3377        2458 :     case 9: return utoipos(362880);
    3378        2694 :     case 10:return utoipos(3628800);
    3379        1409 :     case 11:return utoipos(39916800);
    3380         577 :     case 12:return utoipos(479001600);
    3381           0 :     default: pari_err_DOMAIN("factorial", "argument","<",gen_0,stoi(n));
    3382             :   }
    3383       18587 :   v = cgetg(expu(n) + 2, t_VEC);
    3384       18534 :   for (k = 1;; k++)
    3385       86814 :   {
    3386      105348 :     long m = n >> (k-1), l;
    3387      105348 :     if (m <= 2) break;
    3388       86761 :     l = (1 + (n >> k)) | 1;
    3389             :     /* product of odd numbers in ]n / 2^k, n / 2^(k-1)] */
    3390       86761 :     a = mulu_interval_step(l, m, 2);
    3391       86777 :     gel(v,k) = k == 1? a: powiu(a, k);
    3392             :   }
    3393       86854 :   a = gel(v,--k); while (--k) a = mulii(a, gel(v,k));
    3394       18582 :   a = shifti(a, factorial_lval(n, 2));
    3395       18577 :   return gerepileuptoint(av, a);
    3396             : }
    3397             : 
    3398             : ulong
    3399       56642 : factorial_Fl(long n, ulong p)
    3400             : {
    3401             :   long k;
    3402             :   ulong v;
    3403       56642 :   if (p <= (ulong)n) return 0;
    3404       56642 :   v = Fl_powu(2, factorial_lval(n, 2), p);
    3405       56672 :   for (k = 1;; k++)
    3406      142146 :   {
    3407      198818 :     long m = n >> (k-1), l, i;
    3408      198818 :     ulong a = 1;
    3409      198818 :     if (m <= 2) break;
    3410      142161 :     l = (1 + (n >> k)) | 1;
    3411             :     /* product of odd numbers in ]n / 2^k, 2 / 2^(k-1)] */
    3412      778199 :     for (i=l; i<=m; i+=2)
    3413      636059 :       a = Fl_mul(a, i, p);
    3414      142140 :     v = Fl_mul(v, k == 1? a: Fl_powu(a, k, p), p);
    3415             :   }
    3416       56657 :   return v;
    3417             : }
    3418             : 
    3419             : GEN
    3420         158 : factorial_Fp(long n, GEN p)
    3421             : {
    3422         158 :   pari_sp av = avma;
    3423             :   long k;
    3424         158 :   GEN v = Fp_powu(gen_2, factorial_lval(n, 2), p);
    3425         158 :   for (k = 1;; k++)
    3426         344 :   {
    3427         502 :     long m = n >> (k-1), l, i;
    3428         502 :     GEN a = gen_1;
    3429         502 :     if (m <= 2) break;
    3430         344 :     l = (1 + (n >> k)) | 1;
    3431             :     /* product of odd numbers in ]n / 2^k, 2 / 2^(k-1)] */
    3432         850 :     for (i=l; i<=m; i+=2)
    3433         506 :       a = Fp_mulu(a, i, p);
    3434         344 :     v = Fp_mul(v, k == 1? a: Fp_powu(a, k, p), p);
    3435         344 :     v = gerepileuptoint(av, v);
    3436             :   }
    3437         158 :   return v;
    3438             : }
    3439             : 
    3440             : /*******************************************************************/
    3441             : /**                      LUCAS & FIBONACCI                        **/
    3442             : /*******************************************************************/
    3443             : static void
    3444          56 : lucas(ulong n, GEN *a, GEN *b)
    3445             : {
    3446             :   GEN z, t, zt;
    3447          56 :   if (!n) { *a = gen_2; *b = gen_1; return; }
    3448          49 :   lucas(n >> 1, &z, &t); zt = mulii(z, t);
    3449          49 :   switch(n & 3) {
    3450          14 :     case  0: *a = subiu(sqri(z),2); *b = subiu(zt,1); break;
    3451          14 :     case  1: *a = subiu(zt,1);      *b = addiu(sqri(t),2); break;
    3452           7 :     case  2: *a = addiu(sqri(z),2); *b = addiu(zt,1); break;
    3453          14 :     case  3: *a = addiu(zt,1);      *b = subiu(sqri(t),2);
    3454             :   }
    3455             : }
    3456             : 
    3457             : GEN
    3458           7 : fibo(long n)
    3459             : {
    3460           7 :   pari_sp av = avma;
    3461             :   GEN a, b;
    3462           7 :   if (!n) return gen_0;
    3463           7 :   lucas((ulong)(labs(n)-1), &a, &b);
    3464           7 :   a = diviuexact(addii(shifti(a,1),b), 5);
    3465           7 :   if (n < 0 && !odd(n)) setsigne(a, -1);
    3466           7 :   return gerepileuptoint(av, a);
    3467             : }
    3468             : 
    3469             : /*******************************************************************/
    3470             : /*                      CONTINUED FRACTIONS                        */
    3471             : /*******************************************************************/
    3472             : static GEN
    3473     3136994 : icopy_lg(GEN x, long l)
    3474             : {
    3475     3136994 :   long lx = lgefint(x);
    3476             :   GEN y;
    3477             : 
    3478     3136994 :   if (lx >= l) return icopy(x);
    3479          49 :   y = cgeti(l); affii(x, y); return y;
    3480             : }
    3481             : 
    3482             : /* continued fraction of a/b. If y != NULL, stop when partial quotients
    3483             :  * differ from y */
    3484             : static GEN
    3485     3137344 : Qsfcont(GEN a, GEN b, GEN y, ulong k)
    3486             : {
    3487             :   GEN  z, c;
    3488     3137344 :   ulong i, l, ly = lgefint(b);
    3489             : 
    3490             :   /* times 1 / log2( (1+sqrt(5)) / 2 )  */
    3491     3137344 :   l = (ulong)(3 + bit_accuracy_mul(ly, 1.44042009041256));
    3492     3137344 :   if (k > 0 && k+1 > 0 && l > k+1) l = k+1; /* beware overflow */
    3493     3137344 :   if (l > LGBITS) l = LGBITS;
    3494             : 
    3495     3137344 :   z = cgetg(l,t_VEC);
    3496     3137344 :   l--;
    3497     3137344 :   if (y) {
    3498         350 :     pari_sp av = avma;
    3499         350 :     if (l >= (ulong)lg(y)) l = lg(y)-1;
    3500       25209 :     for (i = 1; i <= l; i++)
    3501             :     {
    3502       24985 :       GEN q = gel(y,i);
    3503       24985 :       gel(z,i) = q;
    3504       24985 :       c = b; if (!gequal1(q)) c = mulii(q, b);
    3505       24985 :       c = subii(a, c);
    3506       24985 :       if (signe(c) < 0)
    3507             :       { /* partial quotient too large */
    3508          96 :         c = addii(c, b);
    3509          96 :         if (signe(c) >= 0) i++; /* by 1 */
    3510          96 :         break;
    3511             :       }
    3512       24889 :       if (cmpii(c, b) >= 0)
    3513             :       { /* partial quotient too small */
    3514          30 :         c = subii(c, b);
    3515          30 :         if (cmpii(c, b) < 0) {
    3516             :           /* by 1. If next quotient is 1 in y, add 1 */
    3517          12 :           if (i < l && equali1(gel(y,i+1))) gel(z,i) = addiu(q,1);
    3518          12 :           i++;
    3519             :         }
    3520          30 :         break;
    3521             :       }
    3522       24859 :       if ((i & 0xff) == 0) gerepileall(av, 2, &b, &c);
    3523       24859 :       a = b; b = c;
    3524             :     }
    3525             :   } else {
    3526     3136994 :     a = icopy_lg(a, ly);
    3527     3136994 :     b = icopy(b);
    3528    24524282 :     for (i = 1; i <= l; i++)
    3529             :     {
    3530    24523964 :       gel(z,i) = truedvmdii(a,b,&c);
    3531    24523964 :       if (c == gen_0) { i++; break; }
    3532    21387288 :       affii(c, a); cgiv(c); c = a;
    3533    21387288 :       a = b; b = c;
    3534             :     }
    3535             :   }
    3536     3137344 :   i--;
    3537     3137344 :   if (i > 1 && gequal1(gel(z,i)))
    3538             :   {
    3539         101 :     cgiv(gel(z,i)); --i;
    3540         101 :     gel(z,i) = addui(1, gel(z,i)); /* unclean: leave old z[i] on stack */
    3541             :   }
    3542     3137344 :   setlg(z,i+1); return z;
    3543             : }
    3544             : 
    3545             : static GEN
    3546           0 : sersfcont(GEN a, GEN b, long k)
    3547             : {
    3548           0 :   long i, l = typ(a) == t_POL? lg(a): 3;
    3549             :   GEN y, c;
    3550           0 :   if (lg(b) > l) l = lg(b);
    3551           0 :   if (k > 0 && l > k+1) l = k+1;
    3552           0 :   y = cgetg(l,t_VEC);
    3553           0 :   for (i=1; i<l; i++)
    3554             :   {
    3555           0 :     gel(y,i) = poldivrem(a,b,&c);
    3556           0 :     if (gequal0(c)) { i++; break; }
    3557           0 :     a = b; b = c;
    3558             :   }
    3559           0 :   setlg(y, i); return y;
    3560             : }
    3561             : 
    3562             : GEN
    3563     3142307 : gboundcf(GEN x, long k)
    3564             : {
    3565             :   pari_sp av;
    3566     3142307 :   long tx = typ(x), e;
    3567             :   GEN y, a, b, c;
    3568             : 
    3569     3142307 :   if (k < 0) pari_err_DOMAIN("gboundcf","nmax","<",gen_0,stoi(k));
    3570     3142300 :   if (is_scalar_t(tx))
    3571             :   {
    3572     3142300 :     if (gequal0(x)) return mkvec(gen_0);
    3573     3142181 :     switch(tx)
    3574             :     {
    3575        5180 :       case t_INT: return mkveccopy(x);
    3576         357 :       case t_REAL:
    3577         357 :         av = avma;
    3578         357 :         c = mantissa_real(x,&e);
    3579         357 :         if (e < 0) pari_err_PREC("gboundcf");
    3580         350 :         y = int2n(e);
    3581         350 :         a = Qsfcont(c,y, NULL, k);
    3582         350 :         b = addsi(signe(x), c);
    3583         350 :         return gerepilecopy(av, Qsfcont(b,y, a, k));
    3584             : 
    3585     3136644 :       case t_FRAC:
    3586     3136644 :         av = avma;
    3587     3136644 :         return gerepileupto(av, Qsfcont(gel(x,1),gel(x,2), NULL, k));
    3588             :     }
    3589           0 :     pari_err_TYPE("gboundcf",x);
    3590             :   }
    3591             : 
    3592           0 :   switch(tx)
    3593             :   {
    3594           0 :     case t_POL: return mkveccopy(x);
    3595           0 :     case t_SER:
    3596           0 :       av = avma;
    3597           0 :       return gerepileupto(av, gboundcf(ser2rfrac_i(x), k));
    3598           0 :     case t_RFRAC:
    3599           0 :       av = avma;
    3600           0 :       return gerepilecopy(av, sersfcont(gel(x,1), gel(x,2), k));
    3601             :   }
    3602           0 :   pari_err_TYPE("gboundcf",x);
    3603             :   return NULL; /* LCOV_EXCL_LINE */
    3604             : }
    3605             : 
    3606             : static GEN
    3607          14 : sfcont2(GEN b, GEN x, long k)
    3608             : {
    3609          14 :   pari_sp av = avma;
    3610          14 :   long lb = lg(b), tx = typ(x), i;
    3611             :   GEN y,p1;
    3612             : 
    3613          14 :   if (k)
    3614             :   {
    3615           7 :     if (k >= lb) pari_err_DIM("contfrac [too few denominators]");
    3616           0 :     lb = k+1;
    3617             :   }
    3618           7 :   y = cgetg(lb,t_VEC);
    3619           7 :   if (lb==1) return y;
    3620           7 :   if (is_scalar_t(tx))
    3621             :   {
    3622           7 :     if (!is_intreal_t(tx) && tx != t_FRAC) pari_err_TYPE("sfcont2",x);
    3623             :   }
    3624           0 :   else if (tx == t_SER) x = ser2rfrac_i(x);
    3625             : 
    3626           7 :   if (!gequal1(gel(b,1))) x = gmul(gel(b,1),x);
    3627           7 :   for (i = 1;;)
    3628             :   {
    3629          35 :     if (tx == t_REAL)
    3630             :     {
    3631          35 :       long e = expo(x);
    3632          35 :       if (e > 0 && nbits2prec(e+1) > realprec(x)) break;
    3633          35 :       gel(y,i) = floorr(x);
    3634          35 :       p1 = subri(x, gel(y,i));
    3635             :     }
    3636             :     else
    3637             :     {
    3638           0 :       gel(y,i) = gfloor(x);
    3639           0 :       p1 = gsub(x, gel(y,i));
    3640             :     }
    3641          35 :     if (++i >= lb) break;
    3642          28 :     if (gequal0(p1)) break;
    3643          28 :     x = gdiv(gel(b,i),p1);
    3644             :   }
    3645           7 :   setlg(y,i);
    3646           7 :   return gerepilecopy(av,y);
    3647             : }
    3648             : 
    3649             : GEN
    3650         126 : gcf(GEN x) { return gboundcf(x,0); }
    3651             : GEN
    3652           0 : gcf2(GEN b, GEN x) { return contfrac0(x,b,0); }
    3653             : GEN
    3654          49 : contfrac0(GEN x, GEN b, long nmax)
    3655             : {
    3656             :   long tb;
    3657             : 
    3658          49 :   if (!b) return gboundcf(x,nmax);
    3659          28 :   tb = typ(b);
    3660          28 :   if (tb == t_INT) return gboundcf(x,itos(b));
    3661          21 :   if (! is_vec_t(tb)) pari_err_TYPE("contfrac0",b);
    3662          21 :   if (nmax < 0) pari_err_DOMAIN("contfrac","nmax","<",gen_0,stoi(nmax));
    3663          14 :   return sfcont2(b,x,nmax);
    3664             : }
    3665             : 
    3666             : GEN
    3667         266 : contfracpnqn(GEN x, long n)
    3668             : {
    3669         266 :   pari_sp av = avma;
    3670         266 :   long i, lx = lg(x);
    3671             :   GEN M,A,B, p0,p1, q0,q1;
    3672             : 
    3673         266 :   if (lx == 1)
    3674             :   {
    3675          28 :     if (! is_matvec_t(typ(x))) pari_err_TYPE("pnqn",x);
    3676          21 :     if (n >= 0) return cgetg(1,t_MAT);
    3677           7 :     return matid(2);
    3678             :   }
    3679         238 :   switch(typ(x))
    3680             :   {
    3681         196 :     case t_VEC: case t_COL: A = x; B = NULL; break;
    3682          42 :     case t_MAT:
    3683          42 :       switch(lgcols(x))
    3684             :       {
    3685           0 :         case 2: A = row(x,1); B = NULL; break;
    3686          35 :         case 3: A = row(x,2); B = row(x,1); break;
    3687           7 :         default: pari_err_DIM("pnqn [ nbrows != 1,2 ]");
    3688             :                  return NULL; /*LCOV_EXCL_LINE*/
    3689             :       }
    3690          35 :       break;
    3691           0 :     default: pari_err_TYPE("pnqn",x);
    3692             :       return NULL; /*LCOV_EXCL_LINE*/
    3693             :   }
    3694         231 :   p1 = gel(A,1);
    3695         231 :   q1 = B? gel(B,1): gen_1; /* p[0], q[0] */
    3696         231 :   if (n >= 0)
    3697             :   {
    3698         196 :     lx = minss(lx, n+2);
    3699         196 :     if (lx == 2) return gerepilecopy(av, mkmat(mkcol2(p1,q1)));
    3700             :   }
    3701          35 :   else if (lx == 2)
    3702           7 :     return gerepilecopy(av, mkmat2(mkcol2(p1,q1), mkcol2(gen_1,gen_0)));
    3703             :   /* lx >= 3 */
    3704         119 :   p0 = gen_1;
    3705         119 :   q0 = gen_0; /* p[-1], q[-1] */
    3706         119 :   M = cgetg(lx, t_MAT);
    3707         119 :   gel(M,1) = mkcol2(p1,q1);
    3708         399 :   for (i=2; i<lx; i++)
    3709             :   {
    3710         280 :     GEN a = gel(A,i), p2,q2;
    3711         280 :     if (B) {
    3712          84 :       GEN b = gel(B,i);
    3713          84 :       p0 = gmul(b,p0);
    3714          84 :       q0 = gmul(b,q0);
    3715             :     }
    3716         280 :     p2 = gadd(gmul(a,p1),p0); p0=p1; p1=p2;
    3717         280 :     q2 = gadd(gmul(a,q1),q0); q0=q1; q1=q2;
    3718         280 :     gel(M,i) = mkcol2(p1,q1);
    3719             :   }
    3720         119 :   if (n < 0) M = mkmat2(gel(M,lx-1), gel(M,lx-2));
    3721         119 :   return gerepilecopy(av, M);
    3722             : }
    3723             : GEN
    3724           0 : pnqn(GEN x) { return contfracpnqn(x,-1); }
    3725             : /* x = [a0, ..., an] from gboundcf, n >= 0;
    3726             :  * return [[p0, ..., pn], [q0,...,qn]] */
    3727             : GEN
    3728      894782 : ZV_allpnqn(GEN x)
    3729             : {
    3730      894782 :   long i, lx = lg(x);
    3731      894782 :   GEN p0, p1, q0, q1, p2, q2, P,Q, v = cgetg(3,t_VEC);
    3732             : 
    3733      894782 :   gel(v,1) = P = cgetg(lx, t_VEC);
    3734      894782 :   gel(v,2) = Q = cgetg(lx, t_VEC);
    3735      894782 :   p0 = gen_1; q0 = gen_0;
    3736      894782 :   gel(P, 1) = p1 = gel(x,1); gel(Q, 1) = q1 = gen_1;
    3737     3106138 :   for (i=2; i<lx; i++)
    3738             :   {
    3739     2211356 :     GEN a = gel(x,i);
    3740     2211356 :     gel(P, i) = p2 = addmulii(p0, a, p1); p0 = p1; p1 = p2;
    3741     2211356 :     gel(Q, i) = q2 = addmulii(q0, a, q1); q0 = q1; q1 = q2;
    3742             :   }
    3743      894782 :   return v;
    3744             : }
    3745             : 
    3746             : /* write Mod(x,N) as a/b, gcd(a,b) = 1, b <= B (no condition if B = NULL) */
    3747             : static GEN
    3748          42 : mod_to_frac(GEN x, GEN N, GEN B)
    3749             : {
    3750             :   GEN a, b, A;
    3751          42 :   if (B) A = divii(shifti(N, -1), B);
    3752             :   else
    3753             :   {
    3754          14 :     A = sqrti(shifti(N, -1));
    3755          14 :     B = A;
    3756             :   }
    3757          42 :   if (!Fp_ratlift(x, N, A,B,&a,&b) || !equali1( gcdii(a,b) )) return NULL;
    3758          28 :   return equali1(b)? a: mkfrac(a,b);
    3759             : }
    3760             : 
    3761             : static GEN
    3762         112 : mod_to_rfrac(GEN x, GEN N, long B)
    3763             : {
    3764             :   GEN a, b;
    3765         112 :   long A, d = degpol(N);
    3766         112 :   if (B >= 0) A = d-1 - B;
    3767             :   else
    3768             :   {
    3769          42 :     B = d >> 1;
    3770          42 :     A = odd(d)? B : B-1;
    3771             :   }
    3772         112 :   if (varn(N) != varn(x)) x = scalarpol(x, varn(N));
    3773         112 :   if (!RgXQ_ratlift(x, N, A, B, &a,&b) || degpol(RgX_gcd(a,b)) > 0) return NULL;
    3774          91 :   return gdiv(a,b);
    3775             : }
    3776             : 
    3777             : /* k > 0 t_INT, x a t_FRAC, returns the convergent a/b
    3778             :  * of the continued fraction of x with b <= k maximal */
    3779             : static GEN
    3780           7 : bestappr_frac(GEN x, GEN k)
    3781             : {
    3782             :   pari_sp av;
    3783             :   GEN p0, p1, p, q0, q1, q, a, y;
    3784             : 
    3785           7 :   if (cmpii(gel(x,2),k) <= 0) return gcopy(x);
    3786           0 :   av = avma; y = x;
    3787           0 :   p1 = gen_1; p0 = truedvmdii(gel(x,1), gel(x,2), &a); /* = floor(x) */
    3788           0 :   q1 = gen_0; q0 = gen_1;
    3789           0 :   x = mkfrac(a, gel(x,2)); /* = frac(x); now 0<= x < 1 */
    3790             :   for(;;)
    3791             :   {
    3792           0 :     x = ginv(x); /* > 1 */
    3793           0 :     a = typ(x)==t_INT? x: divii(gel(x,1), gel(x,2));
    3794           0 :     if (cmpii(a,k) > 0)
    3795             :     { /* next partial quotient will overflow limits */
    3796             :       GEN n, d;
    3797           0 :       a = divii(subii(k, q1), q0);
    3798           0 :       p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3799           0 :       q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3800             :       /* compare |y-p0/q0|, |y-p1/q1| */
    3801           0 :       n = gel(y,1);
    3802           0 :       d = gel(y,2);
    3803           0 :       if (abscmpii(mulii(q1, subii(mulii(q0,n), mulii(d,p0))),
    3804             :                    mulii(q0, subii(mulii(q1,n), mulii(d,p1)))) < 0)
    3805           0 :                    { p1 = p0; q1 = q0; }
    3806           0 :       break;
    3807             :     }
    3808           0 :     p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3809           0 :     q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3810             : 
    3811           0 :     if (cmpii(q0,k) > 0) break;
    3812           0 :     x = gsub(x,a); /* 0 <= x < 1 */
    3813           0 :     if (typ(x) == t_INT) { p1 = p0; q1 = q0; break; } /* x = 0 */
    3814             : 
    3815             :   }
    3816           0 :   return gerepileupto(av, gdiv(p1,q1));
    3817             : }
    3818             : /* k > 0 t_INT, x != 0 a t_REAL, returns the convergent a/b
    3819             :  * of the continued fraction of x with b <= k maximal */
    3820             : static GEN
    3821     1249729 : bestappr_real(GEN x, GEN k)
    3822             : {
    3823     1249729 :   pari_sp av = avma;
    3824     1249729 :   GEN kr, p0, p1, p, q0, q1, q, a, y = x;
    3825             : 
    3826     1249729 :   p1 = gen_1; a = p0 = floorr(x);
    3827     1249628 :   q1 = gen_0; q0 = gen_1;
    3828     1249628 :   x = subri(x,a); /* 0 <= x < 1 */
    3829     1249687 :   if (!signe(x)) { cgiv(x); return a; }
    3830     1133865 :   kr = itor(k, realprec(x));
    3831             :   for(;;)
    3832     1224117 :   {
    3833             :     long d;
    3834     2358021 :     x = invr(x); /* > 1 */
    3835     2357800 :     if (cmprr(x,kr) > 0)
    3836             :     { /* next partial quotient will overflow limits */
    3837     1111888 :       a = divii(subii(k, q1), q0);
    3838     1111897 :       p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3839     1111909 :       q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3840             :       /* compare |y-p0/q0|, |y-p1/q1| */
    3841     1111883 :       if (abscmprr(mulir(q1, subri(mulir(q0,y), p0)),
    3842             :                    mulir(q0, subri(mulir(q1,y), p1))) < 0)
    3843      125076 :                    { p1 = p0; q1 = q0; }
    3844     1111924 :       break;
    3845             :     }
    3846     1246013 :     d = nbits2prec(expo(x) + 1);
    3847     1246013 :     if (d > realprec(x)) { p1 = p0; q1 = q0; break; } /* original x was ~ 0 */
    3848             : 
    3849     1245823 :     a = truncr(x); /* truncr(x) will NOT raise e_PREC */
    3850     1245784 :     p = addii(mulii(a,p0), p1); p1=p0; p0=p;
    3851     1245802 :     q = addii(mulii(a,q0), q1); q1=q0; q0=q;
    3852             : 
    3853     1245798 :     if (cmpii(q0,k) > 0) break;
    3854     1239543 :     x = subri(x,a); /* 0 <= x < 1 */
    3855     1239557 :     if (!signe(x)) { p1 = p0; q1 = q0; break; }
    3856             :   }
    3857     1133809 :   if (signe(q1) < 0) { togglesign_safe(&p1); togglesign_safe(&q1); }
    3858     1133809 :   return gerepilecopy(av, equali1(q1)? p1: mkfrac(p1,q1));
    3859             : }
    3860             : 
    3861             : /* k t_INT or NULL */
    3862             : static GEN
    3863     2250416 : bestappr_Q(GEN x, GEN k)
    3864             : {
    3865     2250416 :   long lx, tx = typ(x), i;
    3866             :   GEN a, y;
    3867             : 
    3868     2250416 :   switch(tx)
    3869             :   {
    3870         154 :     case t_INT: return icopy(x);
    3871           7 :     case t_FRAC: return k? bestappr_frac(x, k): gcopy(x);
    3872     1505853 :     case t_REAL:
    3873     1505853 :       if (!signe(x)) return gen_0;
    3874             :       /* i <= e iff nbits2lg(e+1) > lg(x) iff floorr(x) fails */
    3875     1249713 :       i = bit_prec(x); if (i <= expo(x)) return NULL;
    3876     1249730 :       return bestappr_real(x, k? k: int2n(i));
    3877             : 
    3878          28 :     case t_INTMOD: {
    3879          28 :       pari_sp av = avma;
    3880          28 :       a = mod_to_frac(gel(x,2), gel(x,1), k); if (!a) return NULL;
    3881          21 :       return gerepilecopy(av, a);
    3882             :     }
    3883          14 :     case t_PADIC: {
    3884          14 :       pari_sp av = avma;
    3885          14 :       long v = valp(x);
    3886          14 :       a = mod_to_frac(gel(x,4), gel(x,3), k); if (!a) return NULL;
    3887           7 :       if (v) a = gmul(a, powis(gel(x,2), v));
    3888           7 :       return gerepilecopy(av, a);
    3889             :     }
    3890             : 
    3891        5453 :     case t_COMPLEX: {
    3892        5453 :       pari_sp av = avma;
    3893        5453 :       y = cgetg(3, t_COMPLEX);
    3894        5453 :       gel(y,2) = bestappr(gel(x,2), k);
    3895        5453 :       gel(y,1) = bestappr(gel(x,1), k);
    3896        5453 :       if (gequal0(gel(y,2))) return gerepileupto(av, gel(y,1));
    3897          91 :       return y;
    3898             :     }
    3899           0 :     case t_SER:
    3900           0 :       if (ser_isexactzero(x)) return gcopy(x);
    3901             :       /* fall through */
    3902             :     case t_POLMOD: case t_POL: case t_RFRAC:
    3903             :     case t_VEC: case t_COL: case t_MAT:
    3904      738907 :       y = cgetg_copy(x, &lx);
    3905      738941 :       if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    3906     2884671 :       for (; i<lx; i++)
    3907             :       {
    3908     2145732 :         a = bestappr_Q(gel(x,i),k); if (!a) return NULL;
    3909     2145730 :         gel(y,i) = a;
    3910             :       }
    3911      738939 :       if (tx == t_POL) return normalizepol(y);
    3912      738925 :       if (tx == t_SER) return normalizeser(y);
    3913      738925 :       return y;
    3914             :   }
    3915           0 :   pari_err_TYPE("bestappr_Q",x);
    3916             :   return NULL; /* LCOV_EXCL_LINE */
    3917             : }
    3918             : 
    3919             : static GEN
    3920          98 : bestappr_ser(GEN x, long B)
    3921             : {
    3922          98 :   long dN, v = valser(x), lx = lg(x);
    3923             :   GEN t;
    3924          98 :   x = normalizepol(ser2pol_i(x, lx));
    3925          98 :   dN = lx-2;
    3926          98 :   if (v > 0)
    3927             :   {
    3928          21 :     x = RgX_shift_shallow(x, v);
    3929          21 :     dN += v;
    3930             :   }
    3931          77 :   else if (v < 0)
    3932             :   {
    3933          14 :     if (B >= 0) B = maxss(B+v, 0);
    3934             :   }
    3935          98 :   t = mod_to_rfrac(x, pol_xn(dN, varn(x)), B);
    3936          98 :   if (!t) return NULL;
    3937          77 :   if (v < 0)
    3938             :   {
    3939             :     GEN a, b;
    3940             :     long vx;
    3941          14 :     if (typ(t) == t_POL) return RgX_mulXn(t, v);
    3942             :     /* t_RFRAC */
    3943          14 :     vx = varn(x);
    3944          14 :     a = gel(t,1);
    3945          14 :     b = gel(t,2);
    3946          14 :     v -= RgX_valrem(b, &b);
    3947          14 :     if (typ(a) == t_POL && varn(a) == vx) v += RgX_valrem(a, &a);
    3948          14 :     if (v < 0) b = RgX_shift_shallow(b, -v);
    3949           0 :     else if (v > 0) {
    3950           0 :       if (typ(a) != t_POL || varn(a) != vx) a = scalarpol_shallow(a, vx);
    3951           0 :       a = RgX_shift_shallow(a, v);
    3952             :     }
    3953          14 :     t = mkrfraccopy(a, b);
    3954             :   }
    3955          77 :   return t;
    3956             : }
    3957             : static GEN
    3958          42 : gc_empty(pari_sp av) { set_avma(av); return cgetg(1, t_VEC); }
    3959             : static GEN
    3960         112 : _gc_upto(pari_sp av, GEN x) { return x? gerepileupto(av, x): NULL; }
    3961             : 
    3962             : static GEN bestappr_RgX(GEN x, long B);
    3963             : /* B >= 0 or < 0 [omit condition on B].
    3964             :  * Look for coprime t_POL a,b, deg(b)<=B, such that a/b ~ x */
    3965             : static GEN
    3966         119 : bestappr_RgX(GEN x, long B)
    3967             : {
    3968             :   pari_sp av;
    3969         119 :   switch(typ(x))
    3970             :   {
    3971           0 :     case t_INT: case t_REAL: case t_INTMOD: case t_FRAC: case t_FFELT:
    3972             :     case t_COMPLEX: case t_PADIC: case t_QUAD: case t_POL:
    3973           0 :       return gcopy(x);
    3974          14 :     case t_RFRAC:
    3975          14 :       if (B < 0 || degpol(gel(x,2)) <= B) return gcopy(x);
    3976           7 :       av = avma; return _gc_upto(av, bestappr_ser(rfrac_to_ser_i(x, 2*B+1), B));
    3977          14 :     case t_POLMOD:
    3978          14 :       av = avma; return _gc_upto(av, mod_to_rfrac(gel(x,2), gel(x,1), B));
    3979          91 :     case t_SER:
    3980          91 :       av = avma; return _gc_upto(av, bestappr_ser(x, B));
    3981           0 :     case t_VEC: case t_COL: case t_MAT: {
    3982             :       long i, lx;
    3983           0 :       GEN y = cgetg_copy(x, &lx);
    3984           0 :       for (i = 1; i < lx; i++)
    3985             :       {
    3986           0 :         GEN t = bestappr_RgX(gel(x,i),B); if (!t) return NULL;
    3987           0 :         gel(y,i) = t;
    3988             :       }
    3989           0 :       return y;
    3990             :     }
    3991             :   }
    3992           0 :   pari_err_TYPE("bestappr_RgX",x);
    3993             :   return NULL; /* LCOV_EXCL_LINE */
    3994             : }
    3995             : 
    3996             : /* allow k = NULL: maximal accuracy */
    3997             : GEN
    3998      104687 : bestappr(GEN x, GEN k)
    3999             : {
    4000      104687 :   pari_sp av = avma;
    4001      104687 :   if (k) { /* replace by floor(k) */
    4002      104365 :     switch(typ(k))
    4003             :     {
    4004       33026 :       case t_INT:
    4005       33026 :         break;
    4006       71339 :       case t_REAL: case t_FRAC:
    4007       71339 :         k = floor_safe(k); /* left on stack for efficiency */
    4008       71338 :         if (!signe(k)) k = gen_1;
    4009       71338 :         break;
    4010           0 :       default:
    4011           0 :         pari_err_TYPE("bestappr [bound type]", k);
    4012           0 :         break;
    4013             :     }
    4014             :   }
    4015      104686 :   x = bestappr_Q(x, k);
    4016      104689 :   return x? x: gc_empty(av);
    4017             : }
    4018             : GEN
    4019         119 : bestapprPade(GEN x, long B)
    4020             : {
    4021         119 :   pari_sp av = avma;
    4022         119 :   GEN t = bestappr_RgX(x, B);
    4023         119 :   return t? t: gc_empty(av);
    4024             : }
    4025             : 
    4026             : static GEN
    4027          49 : serPade(GEN S, long p, long q)
    4028             : {
    4029          49 :   pari_sp av = avma;
    4030          49 :   long va, v, t = typ(S);
    4031          49 :   if (t!=t_SER && t!=t_POL && t!=t_RFRAC) pari_err_TYPE("bestapprPade", S);
    4032          49 :   va = gvar(S); v = gvaluation(S, pol_x(va));
    4033          49 :   if (p < 0) pari_err_DOMAIN("bestapprPade", "p", "<", gen_0, stoi(p));
    4034          49 :   if (q < 0) pari_err_DOMAIN("bestapprPade", "q", "<", gen_0, stoi(q));
    4035          49 :   if (v == LONG_MAX) return gc_empty(av);
    4036          42 :   S = gadd(S, zeroser(va, p + q + 1 + v));
    4037          42 :   return gerepileupto(av, bestapprPade(S, q));
    4038             : }
    4039             : 
    4040             : GEN
    4041         126 : bestapprPade0(GEN x, long p, long q)
    4042             : {
    4043          77 :   return (p >= 0 && q >= 0)? serPade(x, p, q)
    4044         203 :                            : bestapprPade(x, p >= 0? p: q);
    4045             : }

Generated by: LCOV version 1.16