Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - FpX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 19828-fe528f7) Lines: 1235 1333 92.6 %
Date: 2016-12-07 05:49:12 Functions: 147 153 96.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2007  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /* Not so fast arithmetic with polynomials over Fp */
      18             : 
      19             : static GEN
      20    64544038 : get_FpX_red(GEN T, GEN *B)
      21             : {
      22    64544038 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
      23      120879 :   *B = gel(T,1); return gel(T,2);
      24             : }
      25             : 
      26             : GEN
      27      383268 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
      28             : 
      29             : long
      30     1377326 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
      31             : 
      32             : long
      33     1582298 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
      34             : 
      35             : /***********************************************************************/
      36             : /**                                                                   **/
      37             : /**                              FpX                                  **/
      38             : /**                                                                   **/
      39             : /***********************************************************************/
      40             : 
      41             : /* FpX are polynomials over Z/pZ represented as t_POL with
      42             :  * t_INT coefficients.
      43             :  * 1) Coefficients should belong to {0,...,p-1}, though non-reduced
      44             :  * coefficients should work but be slower.
      45             :  *
      46             :  * 2) p is not assumed to be prime, but it is assumed that impossible divisions
      47             :  *    will not happen.
      48             :  * 3) Theses functions let some garbage on the stack, but are gerepileupto
      49             :  * compatible.
      50             :  */
      51             : 
      52             : static ulong
      53    39994659 : to_Flx(GEN *P, GEN *Q, GEN p)
      54             : {
      55    39994659 :   ulong pp = uel(p,2);
      56    39994659 :   *P = ZX_to_Flx(*P, pp);
      57    39994659 :   *Q = ZX_to_Flx(*Q, pp); return pp;
      58             : }
      59             : 
      60             : static ulong
      61      753079 : to_Flxq(GEN *P, GEN *T, GEN p)
      62             : {
      63      753079 :   ulong pp = uel(p,2);
      64      753079 :   if (P) *P = ZX_to_Flx(*P, pp);
      65      753079 :   *T = ZXT_to_FlxT(*T, pp); return pp;
      66             : }
      67             : 
      68             : GEN
      69        1710 : Z_to_FpX(GEN a, GEN p, long v)
      70             : {
      71        1710 :   pari_sp av = avma;
      72        1710 :   GEN z = cgetg(3, t_POL);
      73        1710 :   GEN x = modii(a, p);
      74        1710 :   if (!signe(x)) { avma =av; return pol_0(v); }
      75        1710 :   z[1] = evalsigne(1) | evalvarn(v);
      76        1710 :   gel(z,2) = x; return z;
      77             : }
      78             : 
      79             : /* z in Z[X], return lift(z * Mod(1,p)), normalized*/
      80             : GEN
      81    65996117 : FpX_red(GEN z, GEN p)
      82             : {
      83    65996117 :   long i, l = lg(z);
      84    65996117 :   GEN x = cgetg(l, t_POL);
      85    66035144 :   for (i=2; i<l; i++) gel(x,i) = modii(gel(z,i),p);
      86    65996081 :   x[1] = z[1]; return FpX_renormalize(x,l);
      87             : }
      88             : GEN
      89      289286 : FpXV_red(GEN z, GEN p)
      90             : {
      91      289286 :   long i,l = lg(z);
      92      289286 :   GEN x = cgetg(l, t_VEC);
      93      289286 :   for (i=1; i<l; i++) gel(x,i) = FpX_red(gel(z,i), p);
      94      289286 :   return x;
      95             : }
      96             : 
      97             : GEN
      98     1092439 : FpXT_red(GEN z, GEN p)
      99             : {
     100     1092439 :   if (typ(z) == t_POL)
     101     1010199 :     return FpX_red(z, p);
     102             :   else
     103             :   {
     104       82240 :     long i,l = lg(z);
     105       82240 :     GEN x = cgetg(l, t_VEC);
     106       82241 :     for (i=1; i<l; i++) gel(x,i) = FpXT_red(gel(z,i), p);
     107       82240 :     return x;
     108             :   }
     109             : }
     110             : 
     111             : GEN
     112      259006 : FpX_normalize(GEN z, GEN p)
     113             : {
     114      259006 :   GEN p1 = leading_coeff(z);
     115      259006 :   if (lg(z) == 2 || equali1(p1)) return z;
     116       45761 :   return FpX_Fp_mul_to_monic(z, Fp_inv(p1,p), p);
     117             : }
     118             : 
     119             : GEN
     120      665539 : FpX_center(GEN T, GEN p, GEN pov2)
     121             : {
     122      665539 :   long i, l = lg(T);
     123      665539 :   GEN P = cgetg(l,t_POL);
     124      665539 :   for(i=2; i<l; i++) gel(P,i) = Fp_center(gel(T,i), p, pov2);
     125      665539 :   P[1] = T[1]; return P;
     126             : }
     127             : 
     128             : GEN
     129     8071652 : FpX_add(GEN x,GEN y,GEN p)
     130             : {
     131     8071652 :   long lx = lg(x), ly = lg(y), i;
     132             :   GEN z;
     133     8071652 :   if (lx < ly) swapspec(x,y, lx,ly);
     134     8071652 :   z = cgetg(lx,t_POL); z[1] = x[1];
     135     8071652 :   for (i=2; i<ly; i++) gel(z,i) = Fp_add(gel(x,i),gel(y,i), p);
     136     8071652 :   for (   ; i<lx; i++) gel(z,i) = modii(gel(x,i), p);
     137     8071652 :   z = ZX_renormalize(z, lx);
     138     8071652 :   if (!lgpol(z)) { avma = (pari_sp)(z + lx); return pol_0(varn(x)); }
     139     7793685 :   return z;
     140             : }
     141             : 
     142             : static GEN
     143        6584 : Fp_red_FpX(GEN x, GEN p, long v)
     144             : {
     145             :   GEN z;
     146        6584 :   if (!signe(x)) return pol_0(v);
     147         442 :   z = cgetg(3, t_POL);
     148         442 :   gel(z,2) = Fp_red(x,p);
     149         442 :   z[1] = evalvarn(v);
     150         442 :   return FpX_renormalize(z, 3);
     151             : }
     152             : 
     153             : static GEN
     154          13 : Fp_neg_FpX(GEN x, GEN p, long v)
     155             : {
     156             :   GEN z;
     157          13 :   if (!signe(x)) return pol_0(v);
     158           1 :   z = cgetg(3, t_POL);
     159           1 :   gel(z,2) = Fp_neg(x,p);
     160           1 :   z[1] = evalvarn(v);
     161           1 :   return FpX_renormalize(z, 3);
     162             : }
     163             : 
     164             : GEN
     165      550258 : FpX_Fp_add(GEN y,GEN x,GEN p)
     166             : {
     167      550258 :   long i, lz = lg(y);
     168             :   GEN z;
     169      550258 :   if (lz == 2) return Fp_red_FpX(x,p,varn(y));
     170      543674 :   z = cgetg(lz,t_POL); z[1] = y[1];
     171      543674 :   gel(z,2) = Fp_add(gel(y,2),x, p);
     172      543674 :   if (lz == 3) z = FpX_renormalize(z,lz);
     173             :   else
     174      496235 :     for(i=3;i<lz;i++) gel(z,i) = icopy(gel(y,i));
     175      543674 :   return z;
     176             : }
     177             : GEN
     178           0 : FpX_Fp_add_shallow(GEN y,GEN x,GEN p)
     179             : {
     180           0 :   long i, lz = lg(y);
     181             :   GEN z;
     182           0 :   if (lz == 2) return scalar_ZX_shallow(x,varn(y));
     183           0 :   z = cgetg(lz,t_POL); z[1] = y[1];
     184           0 :   gel(z,2) = Fp_add(gel(y,2),x, p);
     185           0 :   if (lz == 3) z = FpX_renormalize(z,lz);
     186             :   else
     187           0 :     for(i=3;i<lz;i++) gel(z,i) = gel(y,i);
     188           0 :   return z;
     189             : }
     190             : GEN
     191      299292 : FpX_Fp_sub(GEN y,GEN x,GEN p)
     192             : {
     193      299292 :   long i, lz = lg(y);
     194             :   GEN z;
     195      299292 :   if (lz == 2) return Fp_neg_FpX(x,p,varn(y));
     196      299279 :   z = cgetg(lz,t_POL); z[1] = y[1];
     197      299279 :   gel(z,2) = Fp_sub(gel(y,2),x, p);
     198      299279 :   if (lz == 3) z = FpX_renormalize(z,lz);
     199             :   else
     200      121997 :     for(i=3;i<lz;i++) gel(z,i) = icopy(gel(y,i));
     201      299279 :   return z;
     202             : }
     203             : GEN
     204        1559 : FpX_Fp_sub_shallow(GEN y,GEN x,GEN p)
     205             : {
     206        1559 :   long i, lz = lg(y);
     207             :   GEN z;
     208        1559 :   if (lz == 2) return Fp_neg_FpX(x,p,varn(y));
     209        1559 :   z = cgetg(lz,t_POL); z[1] = y[1];
     210        1559 :   gel(z,2) = Fp_sub(gel(y,2),x, p);
     211        1559 :   if (lz == 3) z = FpX_renormalize(z,lz);
     212             :   else
     213        1405 :     for(i=3;i<lz;i++) gel(z,i) = gel(y,i);
     214        1559 :   return z;
     215             : }
     216             : 
     217             : GEN
     218       73691 : FpX_neg(GEN x,GEN p)
     219             : {
     220       73691 :   long i, lx = lg(x);
     221       73691 :   GEN y = cgetg(lx,t_POL);
     222       73691 :   y[1] = x[1];
     223       73691 :   for(i=2; i<lx; i++) gel(y,i) = Fp_neg(gel(x,i), p);
     224       73691 :   return ZX_renormalize(y, lx);
     225             : }
     226             : 
     227             : static GEN
     228     6578613 : FpX_subspec(GEN x,GEN y,GEN p, long nx, long ny)
     229             : {
     230             :   long i, lz;
     231             :   GEN z;
     232     6578613 :   if (nx >= ny)
     233             :   {
     234     4834822 :     lz = nx+2;
     235     4834822 :     z = cgetg(lz,t_POL); z[1] = 0; z += 2;
     236     4836249 :     for (i=0; i<ny; i++) gel(z,i) = Fp_sub(gel(x,i),gel(y,i), p);
     237     4834823 :     for (   ; i<nx; i++) gel(z,i) = modii(gel(x,i), p);
     238             :   }
     239             :   else
     240             :   {
     241     1743791 :     lz = ny+2;
     242     1743791 :     z = cgetg(lz,t_POL); z[1] = 0; z += 2;
     243     1743791 :     for (i=0; i<nx; i++) gel(z,i) = Fp_sub(gel(x,i),gel(y,i), p);
     244     1743790 :     for (   ; i<ny; i++) gel(z,i) = Fp_neg(gel(y,i), p);
     245             :   }
     246     6578613 :   z = FpX_renormalize(z-2, lz);
     247     6578614 :   if (!lgpol(z)) { avma = (pari_sp)(z + lz); return pol_0(0); }
     248     6463125 :   return z;
     249             : }
     250             : 
     251             : GEN
     252     6480902 : FpX_sub(GEN x,GEN y,GEN p)
     253             : {
     254     6480902 :   GEN z = FpX_subspec(x+2,y+2,p,lgpol(x),lgpol(y));
     255     6480902 :   setvarn(z, varn(x));
     256     6480902 :   return z;
     257             : }
     258             : 
     259             : GEN
     260        8426 : Fp_FpX_sub(GEN x, GEN y, GEN p)
     261             : {
     262        8426 :   long ly = lg(y), i;
     263             :   GEN z;
     264        8426 :   if (ly <= 3) {
     265         216 :     z = cgetg(3, t_POL);
     266         216 :     x = (ly == 3)? Fp_sub(x, gel(y,2), p): modii(x, p);
     267         216 :     if (!signe(x)) { avma = (pari_sp)(z + 3); return pol_0(varn(y)); }
     268         187 :     z[1] = evalsigne(1)|y[1]; gel(z,2) = x; return z;
     269             :   }
     270        8210 :   z = cgetg(ly,t_POL);
     271        8210 :   gel(z,2) = Fp_sub(x, gel(y,2), p);
     272        8210 :   for (i = 3; i < ly; i++) gel(z,i) = Fp_neg(gel(y,i), p);
     273        8210 :   z = ZX_renormalize(z, ly);
     274        8210 :   if (!lgpol(z)) { avma = (pari_sp)(z + ly); return pol_0(varn(x)); }
     275        8210 :   z[1] = y[1]; return z;
     276             : }
     277             : 
     278             : GEN
     279    46013198 : FpX_mul(GEN x,GEN y,GEN p) { return FpX_red(ZX_mul(x, y), p); }
     280             : 
     281             : GEN
     282     1653183 : FpX_mulspec(GEN a, GEN b, GEN p, long na, long nb)
     283     1653183 : { return FpX_red(ZX_mulspec(a, b, na, nb), p); }
     284             : 
     285             : GEN
     286     3146742 : FpX_sqr(GEN x,GEN p) { return FpX_red(ZX_sqr(x), p); }
     287             : 
     288             : GEN
     289     1183978 : FpX_mulu(GEN y, ulong x,GEN p)
     290             : {
     291             :   GEN z;
     292             :   long i, l;
     293     1183978 :   x = umodui(x, p);
     294     1183978 :   if (!x) return zeropol(varn(y));
     295     1183712 :   z = cgetg_copy(y, &l); z[1] = y[1];
     296     1183712 :   for(i=2; i<l; i++) gel(z,i) = Fp_mulu(gel(y,i), x, p);
     297     1183712 :   return z;
     298             : }
     299             : 
     300             : GEN
     301     3498153 : FpX_Fp_mulspec(GEN y,GEN x,GEN p,long ly)
     302             : {
     303             :   GEN z;
     304             :   long i;
     305     3498153 :   if (!signe(x)) return pol_0(0);
     306     3243144 :   z = cgetg(ly+2,t_POL); z[1] = evalsigne(1);
     307     3243144 :   for(i=0; i<ly; i++) gel(z,i+2) = Fp_mul(gel(y,i), x, p);
     308     3243144 :   return ZX_renormalize(z, ly+2);
     309             : }
     310             : 
     311             : GEN
     312     3492469 : FpX_Fp_mul(GEN y,GEN x,GEN p)
     313             : {
     314     3492469 :   GEN z = FpX_Fp_mulspec(y+2,x,p,lgpol(y));
     315     3492469 :   setvarn(z, varn(y)); return z;
     316             : }
     317             : 
     318             : GEN
     319       45761 : FpX_Fp_mul_to_monic(GEN y,GEN x,GEN p)
     320             : {
     321             :   GEN z;
     322             :   long i, l;
     323       45761 :   z = cgetg_copy(y, &l); z[1] = y[1];
     324       45761 :   for(i=2; i<l-1; i++) gel(z,i) = Fp_mul(gel(y,i), x, p);
     325       45761 :   gel(z,l-1) = gen_1; return z;
     326             : }
     327             : 
     328             : struct _FpXQ {
     329             :   GEN T, p, aut;
     330             : };
     331             : 
     332             : static GEN
     333       55076 : _FpX_sqr(void *data, GEN x)
     334             : {
     335       55076 :   struct _FpXQ *D = (struct _FpXQ*)data;
     336       55076 :   return FpX_sqr(x, D->p);
     337             : }
     338             : static GEN
     339       69261 : _FpX_mul(void *data, GEN x, GEN y)
     340             : {
     341       69261 :   struct _FpXQ *D = (struct _FpXQ*)data;
     342       69261 :   return FpX_mul(x,y, D->p);
     343             : }
     344             : 
     345             : GEN
     346      258181 : FpX_powu(GEN x, ulong n, GEN p)
     347             : {
     348             :   struct _FpXQ D;
     349      258181 :   if (n==0) return pol_1(varn(x));
     350       24549 :   D.p = p;
     351       24549 :   return gen_powu(x, n, (void *)&D, _FpX_sqr, _FpX_mul);
     352             : }
     353             : 
     354             : GEN
     355        1090 : FpX_halve(GEN y, GEN p)
     356             : {
     357             :   GEN z;
     358             :   long i, l;
     359        1090 :   z = cgetg_copy(y, &l); z[1] = y[1];
     360        1090 :   for(i=2; i<l; i++) gel(z,i) = Fp_halve(gel(y,i), p);
     361        1090 :   return z;
     362             : }
     363             : 
     364             : static GEN
     365    61588780 : FpX_divrem_basecase(GEN x, GEN y, GEN p, GEN *pr)
     366             : {
     367             :   long vx, dx, dy, dy1, dz, i, j, sx, lr;
     368             :   pari_sp av0, av;
     369             :   GEN z,p1,rem,lead;
     370             : 
     371    61588780 :   if (!signe(y)) pari_err_INV("FpX_divrem",y);
     372    61588780 :   vx = varn(x);
     373    61588780 :   dy = degpol(y);
     374    61588780 :   dx = degpol(x);
     375    61588780 :   if (dx < dy)
     376             :   {
     377       64275 :     if (pr)
     378             :     {
     379       64260 :       av0 = avma; x = FpX_red(x, p);
     380       64260 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: pol_0(vx); }
     381       64218 :       if (pr == ONLY_REM) return x;
     382       64218 :       *pr = x;
     383             :     }
     384       64233 :     return pol_0(vx);
     385             :   }
     386    61524505 :   lead = leading_coeff(y);
     387    61524505 :   if (!dy) /* y is constant */
     388             :   {
     389      291010 :     if (pr && pr != ONLY_DIVIDES)
     390             :     {
     391      278632 :       if (pr == ONLY_REM) return pol_0(vx);
     392      246645 :       *pr = pol_0(vx);
     393             :     }
     394      259023 :     av0 = avma;
     395      259023 :     if (equali1(lead)) return FpX_red(x, p);
     396      256336 :     else return gerepileupto(av0, FpX_Fp_mul(x, Fp_inv(lead,p), p));
     397             :   }
     398    61233495 :   av0 = avma; dz = dx-dy;
     399    61233495 :   if (lgefint(p) == 3)
     400             :   { /* assume ab != 0 mod p */
     401    39496395 :     ulong pp = to_Flx(&x, &y, p);
     402    39496395 :     z = Flx_divrem(x, y, pp, pr);
     403    39496395 :     avma = av0; /* HACK: assume pr last on stack, then z */
     404    39496395 :     if (!z) return NULL;
     405    39496360 :     z = leafcopy(z);
     406    39496360 :     if (pr && pr != ONLY_DIVIDES && pr != ONLY_REM)
     407             :     {
     408     2049905 :       *pr = leafcopy(*pr);
     409     2049905 :       *pr = Flx_to_ZX_inplace(*pr);
     410             :     }
     411    39496360 :     return Flx_to_ZX_inplace(z);
     412             :   }
     413    21737100 :   lead = equali1(lead)? NULL: gclone(Fp_inv(lead,p));
     414    21737037 :   avma = av0;
     415    21737037 :   z=cgetg(dz+3,t_POL); z[1] = x[1];
     416    21737037 :   x += 2; y += 2; z += 2;
     417    21737037 :   for (dy1=dy-1; dy1>=0 && !signe(gel(y, dy1)); dy1--);
     418             : 
     419    21737037 :   p1 = gel(x,dx); av = avma;
     420    21737037 :   gel(z,dz) = lead? gerepileuptoint(av, Fp_mul(p1,lead, p)): icopy(p1);
     421    50890722 :   for (i=dx-1; i>=dy; i--)
     422             :   {
     423    29153685 :     av=avma; p1=gel(x,i);
     424   314181621 :     for (j=i-dy1; j<=i && j<=dz; j++)
     425   285027936 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     426    29153685 :     if (lead) p1 = mulii(p1,lead);
     427    29153685 :     gel(z,i-dy) = gerepileuptoint(av,modii(p1, p));
     428             :   }
     429    21737037 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
     430             : 
     431    21719158 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     432    22475625 :   for (sx=0; ; i--)
     433             :   {
     434    22475625 :     p1 = gel(x,i);
     435    73570716 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     436    51095091 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     437    22475625 :     p1 = modii(p1,p); if (signe(p1)) { sx = 1; break; }
     438      824310 :     if (!i) break;
     439      756467 :     avma=av;
     440      756467 :   }
     441    21719158 :   if (pr == ONLY_DIVIDES)
     442             :   {
     443           0 :     if (lead) gunclone(lead);
     444           0 :     if (sx) { avma=av0; return NULL; }
     445           0 :     avma = (pari_sp)rem; return z-2;
     446             :   }
     447    21719158 :   lr=i+3; rem -= lr;
     448    21719158 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     449    21719158 :   rem[1] = z[-1];
     450    21719158 :   p1 = gerepileuptoint((pari_sp)rem, p1);
     451    21719158 :   rem += 2; gel(rem,i) = p1;
     452    81715604 :   for (i--; i>=0; i--)
     453             :   {
     454    59996446 :     av=avma; p1 = gel(x,i);
     455   421312851 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     456   361316405 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     457    59996446 :     gel(rem,i) = gerepileuptoint(av, modii(p1,p));
     458             :   }
     459    21719158 :   rem -= 2;
     460    21719158 :   if (lead) gunclone(lead);
     461    21719158 :   if (!sx) (void)FpX_renormalize(rem, lr);
     462    21719158 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     463      527342 :   *pr = rem; return z-2;
     464             : }
     465             : 
     466             : GEN
     467       19369 : FpX_div_by_X_x(GEN a, GEN x, GEN p, GEN *r)
     468             : {
     469       19369 :   long l = lg(a)-1, i;
     470       19369 :   GEN z = cgetg(l, t_POL);
     471       19369 :   z[1] = evalsigne(1) | evalvarn(0);
     472       19369 :   gel(z, l-1) = gel(a,l);
     473      628299 :   for (i=l-2; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
     474      608930 :     gel(z, i) = Fp_addmul(gel(a,i+1), x, gel(z,i+1), p);
     475       19369 :   if (r) *r = Fp_addmul(gel(a,2), x, gel(z,2), p);
     476       19369 :   return z;
     477             : }
     478             : 
     479             : static GEN
     480       69706 : _FpX_divrem(void * E, GEN x, GEN y, GEN *r)
     481             : {
     482       69706 :   struct _FpXQ *D = (struct _FpXQ*) E;
     483       69706 :   return FpX_divrem(x, y, D->p, r);
     484             : }
     485             : static GEN
     486       10241 : _FpX_add(void * E, GEN x, GEN y) {
     487       10241 :   struct _FpXQ *D = (struct _FpXQ*) E;
     488       10241 :   return FpX_add(x, y, D->p);
     489             : }
     490             : 
     491             : static struct bb_ring FpX_ring = { _FpX_add,_FpX_mul,_FpX_sqr };
     492             : 
     493             : GEN
     494        5642 : FpX_digits(GEN x, GEN T, GEN p)
     495             : {
     496        5642 :   pari_sp av = avma;
     497             :   struct _FpXQ D;
     498        5642 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     499             :   GEN z;
     500        5642 :   D.p = p;
     501        5642 :   z = gen_digits(x,T,n,(void *)&D, &FpX_ring, _FpX_divrem);
     502        5642 :   return gerepileupto(av, z);
     503             : }
     504             : 
     505             : GEN
     506        2296 : FpX_fromdigits(GEN x, GEN T, GEN p)
     507             : {
     508        2296 :   pari_sp av = avma;
     509             :   struct _FpXQ D;
     510             :   GEN z;
     511        2296 :   D.p = p;
     512        2296 :   z = gen_fromdigits(x,T,(void *)&D, &FpX_ring);
     513        2296 :   return gerepileupto(av, z);
     514             : }
     515             : 
     516             : long
     517       23779 : FpX_valrem(GEN x, GEN t, GEN p, GEN *py)
     518             : {
     519       23779 :   pari_sp av=avma;
     520             :   long k;
     521             :   GEN r, y;
     522             : 
     523       68957 :   for (k=0; ; k++)
     524             :   {
     525       68957 :     y = FpX_divrem(x, t, p, &r);
     526       68957 :     if (signe(r)) break;
     527       45178 :     x = y;
     528       45178 :   }
     529       23779 :   *py = gerepilecopy(av,x);
     530       23779 :   return k;
     531             : }
     532             : 
     533             : static GEN
     534         191 : FpX_halfgcd_basecase(GEN a, GEN b, GEN p)
     535             : {
     536         191 :   pari_sp av=avma;
     537             :   GEN u,u1,v,v1;
     538         191 :   long vx = varn(a);
     539         191 :   long n = lgpol(a)>>1;
     540         191 :   u1 = v = pol_0(vx);
     541         191 :   u = v1 = pol_1(vx);
     542        3577 :   while (lgpol(b)>n)
     543             :   {
     544        3195 :     GEN r, q = FpX_divrem(a,b,p, &r);
     545        3195 :     a = b; b = r; swap(u,u1); swap(v,v1);
     546        3195 :     u1 = FpX_sub(u1, FpX_mul(u, q, p), p);
     547        3195 :     v1 = FpX_sub(v1, FpX_mul(v, q ,p), p);
     548        3195 :     if (gc_needed(av,2))
     549             :     {
     550           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_halfgcd (d = %ld)",degpol(b));
     551           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
     552             :     }
     553             :   }
     554         191 :   return gerepilecopy(av, mkmat2(mkcol2(u,u1), mkcol2(v,v1)));
     555             : }
     556             : static GEN
     557         346 : FpX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN p)
     558             : {
     559         346 :   return FpX_add(FpX_mul(u, x, p),FpX_mul(v, y, p), p);
     560             : }
     561             : 
     562             : static GEN
     563         169 : FpXM_FpX_mul2(GEN M, GEN x, GEN y, GEN p)
     564             : {
     565         169 :   GEN res = cgetg(3, t_COL);
     566         169 :   gel(res, 1) = FpX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, p);
     567         169 :   gel(res, 2) = FpX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, p);
     568         169 :   return res;
     569             : }
     570             : 
     571             : static GEN
     572         169 : FpXM_mul2(GEN A, GEN B, GEN p)
     573             : {
     574         169 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
     575         169 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
     576         169 :   GEN M1 = FpX_mul(FpX_add(A11,A22, p), FpX_add(B11,B22, p), p);
     577         169 :   GEN M2 = FpX_mul(FpX_add(A21,A22, p), B11, p);
     578         169 :   GEN M3 = FpX_mul(A11, FpX_sub(B12,B22, p), p);
     579         169 :   GEN M4 = FpX_mul(A22, FpX_sub(B21,B11, p), p);
     580         169 :   GEN M5 = FpX_mul(FpX_add(A11,A12, p), B22, p);
     581         169 :   GEN M6 = FpX_mul(FpX_sub(A21,A11, p), FpX_add(B11,B12, p), p);
     582         169 :   GEN M7 = FpX_mul(FpX_sub(A12,A22, p), FpX_add(B21,B22, p), p);
     583         169 :   GEN T1 = FpX_add(M1,M4, p), T2 = FpX_sub(M7,M5, p);
     584         169 :   GEN T3 = FpX_sub(M1,M2, p), T4 = FpX_add(M3,M6, p);
     585         169 :   retmkmat2(mkcol2(FpX_add(T1,T2, p), FpX_add(M2,M4, p)),
     586             :             mkcol2(FpX_add(M3,M5, p), FpX_add(T3,T4, p)));
     587             : }
     588             : 
     589             : /* Return [0,1;1,-q]*M */
     590             : static GEN
     591         165 : FpX_FpXM_qmul(GEN q, GEN M, GEN p)
     592             : {
     593         165 :   GEN u, v, res = cgetg(3, t_MAT);
     594         165 :   u = FpX_sub(gcoeff(M,1,1), FpX_mul(gcoeff(M,2,1), q, p), p);
     595         165 :   gel(res,1) = mkcol2(gcoeff(M,2,1), u);
     596         165 :   v = FpX_sub(gcoeff(M,1,2), FpX_mul(gcoeff(M,2,2), q, p), p);
     597         165 :   gel(res,2) = mkcol2(gcoeff(M,2,2), v);
     598         165 :   return res;
     599             : }
     600             : 
     601             : static GEN
     602           4 : matid2_FpXM(long v)
     603             : {
     604           4 :   retmkmat2(mkcol2(pol_1(v),pol_0(v)),
     605             :             mkcol2(pol_0(v),pol_1(v)));
     606             : }
     607             : 
     608             : static GEN
     609         165 : FpX_halfgcd_split(GEN x, GEN y, GEN p)
     610             : {
     611         165 :   pari_sp av=avma;
     612             :   GEN R, S, V;
     613             :   GEN y1, r, q;
     614         165 :   long l = lgpol(x), n = l>>1, k;
     615         165 :   if (lgpol(y)<=n) return matid2_FpXM(varn(x));
     616         165 :   R = FpX_halfgcd(RgX_shift_shallow(x,-n),RgX_shift_shallow(y,-n),p);
     617         165 :   V = FpXM_FpX_mul2(R,x,y,p); y1 = gel(V,2);
     618         165 :   if (lgpol(y1)<=n) return gerepilecopy(av, R);
     619         165 :   q = FpX_divrem(gel(V,1), y1, p, &r);
     620         165 :   k = 2*n-degpol(y1);
     621         165 :   S = FpX_halfgcd(RgX_shift_shallow(y1,-k), RgX_shift_shallow(r,-k),p);
     622         165 :   return gerepileupto(av, FpXM_mul2(S,FpX_FpXM_qmul(q,R,p),p));
     623             : }
     624             : 
     625             : /* Return M in GL_2(Fp[X]) such that:
     626             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
     627             : */
     628             : 
     629             : static GEN
     630         356 : FpX_halfgcd_i(GEN x, GEN y, GEN p)
     631             : {
     632         356 :   if (lg(x)<=FpX_HALFGCD_LIMIT) return FpX_halfgcd_basecase(x,y,p);
     633         165 :   return FpX_halfgcd_split(x,y,p);
     634             : }
     635             : 
     636             : GEN
     637         468 : FpX_halfgcd(GEN x, GEN y, GEN p)
     638             : {
     639         468 :   pari_sp av = avma;
     640             :   GEN M,q,r;
     641         468 :   if (lgefint(p)==3)
     642             :   {
     643         112 :     ulong pp = to_Flx(&x, &y, p);
     644         112 :     M = FlxM_to_ZXM(Flx_halfgcd(x, y, pp));
     645             :   }
     646             :   else
     647             :   {
     648         356 :     if (!signe(x))
     649             :     {
     650           0 :       long v = varn(x);
     651           0 :       retmkmat2(mkcol2(pol_0(v),pol_1(v)),
     652             :                 mkcol2(pol_1(v),pol_0(v)));
     653             :     }
     654         356 :     if (degpol(y)<degpol(x)) return FpX_halfgcd_i(x,y,p);
     655          11 :     q = FpX_divrem(y,x,p,&r);
     656          11 :     M = FpX_halfgcd_i(x,r,p);
     657          11 :     gcoeff(M,1,1) = FpX_sub(gcoeff(M,1,1), FpX_mul(q, gcoeff(M,1,2), p), p);
     658          11 :     gcoeff(M,2,1) = FpX_sub(gcoeff(M,2,1), FpX_mul(q, gcoeff(M,2,2), p), p);
     659             :   }
     660         123 :   return gerepilecopy(av, M);
     661             : }
     662             : 
     663             : static GEN
     664       52006 : FpX_gcd_basecase(GEN a, GEN b, GEN p)
     665             : {
     666       52006 :   pari_sp av = avma, av0=avma;
     667      696605 :   while (signe(b))
     668             :   {
     669             :     GEN c;
     670      592656 :     if (gc_needed(av0,2))
     671             :     {
     672           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_gcd (d = %ld)",degpol(b));
     673           0 :       gerepileall(av0,2, &a,&b);
     674             :     }
     675      592656 :     av = avma; c = FpX_rem(a,b,p); a=b; b=c;
     676             :   }
     677       51943 :   avma = av; return a;
     678             : }
     679             : 
     680             : GEN
     681      440417 : FpX_gcd(GEN x, GEN y, GEN p)
     682             : {
     683      440417 :   pari_sp av = avma;
     684      440417 :   if (lgefint(p)==3)
     685             :   {
     686             :     ulong pp;
     687      388230 :     (void)new_chunk((lg(x) + lg(y)) << 2); /* scratch space */
     688      388230 :     pp = to_Flx(&x, &y, p);
     689      388230 :     x = Flx_gcd(x, y, pp);
     690      388230 :     avma = av; return Flx_to_ZX(x);
     691             :   }
     692       52187 :   x = FpX_red(x, p);
     693       52187 :   y = FpX_red(y, p);
     694       52187 :   if (!signe(x)) return gerepileupto(av, y);
     695      104012 :   while (lg(y)>FpX_GCD_LIMIT)
     696             :   {
     697             :     GEN c;
     698           0 :     if (lgpol(y)<=(lgpol(x)>>1))
     699             :     {
     700           0 :       GEN r = FpX_rem(x, y, p);
     701           0 :       x = y; y = r;
     702             :     }
     703           0 :     c = FpXM_FpX_mul2(FpX_halfgcd(x,y, p), x, y, p);
     704           0 :     x = gel(c,1); y = gel(c,2);
     705           0 :     gerepileall(av,2,&x,&y);
     706             :   }
     707       52006 :   return gerepileupto(av, FpX_gcd_basecase(x,y,p));
     708             : }
     709             : 
     710             : /*Return 1 if gcd can be computed
     711             :  * else return a factor of p*/
     712             : 
     713             : GEN
     714           0 : FpX_gcd_check(GEN x, GEN y, GEN p)
     715             : {
     716             :   GEN a,b,c;
     717           0 :   pari_sp av=avma;
     718             : 
     719           0 :   a = FpX_red(x, p);
     720           0 :   b = FpX_red(y, p);
     721           0 :   while (signe(b))
     722             :   {
     723           0 :     GEN lead = leading_coeff(b);
     724           0 :     GEN g = gcdii(lead,p);
     725           0 :     if (!equali1(g)) return gerepileuptoint(av,g);
     726           0 :     c = FpX_rem(a,b,p); a=b; b=c;
     727             :   }
     728           0 :   avma = av; return gen_1;
     729             : }
     730             : 
     731             : static GEN
     732      246645 : FpX_extgcd_basecase(GEN a, GEN b, GEN p, GEN *ptu, GEN *ptv)
     733             : {
     734      246645 :   pari_sp av=avma;
     735             :   GEN u,v,d,d1,v1;
     736      246645 :   long vx = varn(a);
     737      246645 :   d = a; d1 = b;
     738      246645 :   v = pol_0(vx); v1 = pol_1(vx);
     739     1081986 :   while (signe(d1))
     740             :   {
     741      588696 :     GEN r, q = FpX_divrem(d,d1,p, &r);
     742      588696 :     v = FpX_sub(v,FpX_mul(q,v1,p),p);
     743      588696 :     u=v; v=v1; v1=u;
     744      588696 :     u=r; d=d1; d1=u;
     745      588696 :     if (gc_needed(av,2))
     746             :     {
     747           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_extgcd (d = %ld)",degpol(d));
     748           0 :       gerepileall(av,5, &d,&d1,&u,&v,&v1);
     749             :     }
     750             :   }
     751      246645 :   if (ptu) *ptu = FpX_div(FpX_sub(d,FpX_mul(b,v,p),p),a,p);
     752      246645 :   *ptv = v; return d;
     753             : }
     754             : 
     755             : static GEN
     756           4 : FpX_extgcd_halfgcd(GEN x, GEN y, GEN p, GEN *ptu, GEN *ptv)
     757             : {
     758           4 :   pari_sp av=avma;
     759           4 :   GEN u,v,R = matid2_FpXM(varn(x));
     760          12 :   while (lg(y)>FpX_EXTGCD_LIMIT)
     761             :   {
     762             :     GEN M, c;
     763           4 :     if (lgpol(y)<=(lgpol(x)>>1))
     764             :     {
     765           0 :       GEN r, q = FpX_divrem(x, y, p, &r);
     766           0 :       x = y; y = r;
     767           0 :       R = FpX_FpXM_qmul(q, R, p);
     768             :     }
     769           4 :     M = FpX_halfgcd(x,y, p);
     770           4 :     c = FpXM_FpX_mul2(M, x,y, p);
     771           4 :     R = FpXM_mul2(M, R, p);
     772           4 :     x = gel(c,1); y = gel(c,2);
     773           4 :     gerepileall(av,3,&x,&y,&R);
     774             :   }
     775           4 :   y = FpX_extgcd_basecase(x,y,p,&u,&v);
     776           4 :   if (ptu) *ptu = FpX_addmulmul(u,v,gcoeff(R,1,1),gcoeff(R,2,1),p);
     777           4 :   *ptv = FpX_addmulmul(u,v,gcoeff(R,1,2),gcoeff(R,2,2),p);
     778           4 :   return y;
     779             : }
     780             : 
     781             : /* x and y in Z[X], return lift(gcd(x mod p, y mod p)). Set u and v st
     782             :  * ux + vy = gcd (mod p) */
     783             : GEN
     784      356567 : FpX_extgcd(GEN x, GEN y, GEN p, GEN *ptu, GEN *ptv)
     785             : {
     786             :   GEN d;
     787      356567 :   pari_sp ltop=avma;
     788      356567 :   if (lgefint(p)==3)
     789             :   {
     790      109922 :     ulong pp = to_Flx(&x, &y, p);
     791      109922 :     d = Flx_extgcd(x,y, pp, ptu,ptv);
     792      109922 :     d = Flx_to_ZX(d);
     793      109922 :     if (ptu) *ptu=Flx_to_ZX(*ptu);
     794      109922 :     *ptv=Flx_to_ZX(*ptv);
     795             :   }
     796             :   else
     797             :   {
     798      246645 :     x = FpX_red(x, p);
     799      246645 :     y = FpX_red(y, p);
     800      246645 :     if (lg(y)>FpX_EXTGCD_LIMIT)
     801           4 :       d = FpX_extgcd_halfgcd(x, y, p, ptu, ptv);
     802             :     else
     803      246641 :       d = FpX_extgcd_basecase(x, y, p, ptu, ptv);
     804             :   }
     805      356567 :   gerepileall(ltop,ptu?3:2,&d,ptv,ptu);
     806      356567 :   return d;
     807             : }
     808             : 
     809             : GEN
     810       13377 : FpX_rescale(GEN P, GEN h, GEN p)
     811             : {
     812       13377 :   long i, l = lg(P);
     813       13377 :   GEN Q = cgetg(l,t_POL), hi = h;
     814       13377 :   Q[l-1] = P[l-1];
     815       61054 :   for (i=l-2; i>=2; i--)
     816             :   {
     817       61054 :     gel(Q,i) = Fp_mul(gel(P,i), hi, p);
     818       61054 :     if (i == 2) break;
     819       47677 :     hi = Fp_mul(hi,h, p);
     820             :   }
     821       13377 :   Q[1] = P[1]; return Q;
     822             : }
     823             : 
     824             : GEN
     825      635232 : FpX_deriv(GEN x, GEN p) { return FpX_red(ZX_deriv(x), p); }
     826             : 
     827             : int
     828        2065 : FpX_is_squarefree(GEN f, GEN p)
     829             : {
     830        2065 :   pari_sp av = avma;
     831        2065 :   GEN z = FpX_gcd(f,FpX_deriv(f,p),p);
     832        2065 :   avma = av;
     833        2065 :   return degpol(z)==0;
     834             : }
     835             : 
     836             : GEN
     837       19165 : random_FpX(long d1, long v, GEN p)
     838             : {
     839       19165 :   long i, d = d1+2;
     840       19165 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     841       19165 :   for (i=2; i<d; i++) gel(y,i) = randomi(p);
     842       19165 :   return FpX_renormalize(y,d);
     843             : }
     844             : 
     845             : GEN
     846         566 : FpX_dotproduct(GEN x, GEN y, GEN p)
     847             : {
     848         566 :   long i, l = minss(lg(x), lg(y));
     849             :   pari_sp av;
     850             :   GEN c;
     851         566 :   if (l == 2) return gen_0;
     852         566 :   av = avma; c = mulii(gel(x,2),gel(y,2));
     853         566 :   for (i=3; i<l; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     854         566 :   return gerepileuptoint(av, modii(c,p));
     855             : }
     856             : 
     857             : /* Evaluation in Fp
     858             :  * x a ZX and y an Fp, return x(y) mod p
     859             :  *
     860             :  * If p is very large (several longs) and x has small coefficients(<<p),
     861             :  * then Brent & Kung algorithm is faster. */
     862             : GEN
     863      370430 : FpX_eval(GEN x,GEN y,GEN p)
     864             : {
     865             :   pari_sp av;
     866             :   GEN p1,r,res;
     867      370430 :   long j, i=lg(x)-1;
     868      370430 :   if (i<=2 || !signe(y))
     869      153923 :     return (i==1)? gen_0: modii(gel(x,2),p);
     870      216507 :   res=cgeti(lgefint(p));
     871      216507 :   av=avma; p1=gel(x,i);
     872             :   /* specific attention to sparse polynomials (see poleval)*/
     873             :   /*You've guessed it! It's a copy-paste(tm)*/
     874     1031412 :   for (i--; i>=2; i=j-1)
     875             :   {
     876     1277098 :     for (j=i; !signe(gel(x,j)); j--)
     877      462193 :       if (j==2)
     878             :       {
     879       25141 :         if (i!=j) y = Fp_powu(y,i-j+1,p);
     880       25141 :         p1=mulii(p1,y);
     881       25141 :         goto fppoleval;/*sorry break(2) no implemented*/
     882             :       }
     883      814905 :     r = (i==j)? y: Fp_powu(y,i-j+1,p);
     884      814905 :     p1 = Fp_addmul(gel(x,j), p1, r, p);
     885      814905 :     if ((i & 7) == 0) { affii(p1, res); p1 = res; avma = av; }
     886             :   }
     887             :  fppoleval:
     888      216507 :   modiiz(p1,p,res);
     889      216507 :   avma = av; return res;
     890             : }
     891             : 
     892             : /* Tz=Tx*Ty where Tx and Ty coprime
     893             :  * return lift(chinese(Mod(x*Mod(1,p),Tx*Mod(1,p)),Mod(y*Mod(1,p),Ty*Mod(1,p))))
     894             :  * if Tz is NULL it is computed
     895             :  * As we do not return it, and the caller will frequently need it,
     896             :  * it must compute it and pass it.
     897             :  */
     898             : GEN
     899         665 : FpX_chinese_coprime(GEN x,GEN y,GEN Tx,GEN Ty,GEN Tz,GEN p)
     900             : {
     901         665 :   pari_sp av = avma;
     902             :   GEN ax,p1;
     903         665 :   ax = FpX_mul(FpXQ_inv(Tx,Ty,p), Tx,p);
     904         665 :   p1 = FpX_mul(ax, FpX_sub(y,x,p),p);
     905         665 :   p1 = FpX_add(x,p1,p);
     906         665 :   if (!Tz) Tz=FpX_mul(Tx,Ty,p);
     907         665 :   p1 = FpX_rem(p1,Tz,p);
     908         665 :   return gerepileupto(av,p1);
     909             : }
     910             : 
     911             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
     912             : GEN
     913        5878 : FpX_resultant(GEN a, GEN b, GEN p)
     914             : {
     915             :   long da,db,dc;
     916             :   pari_sp av;
     917        5878 :   GEN c,lb, res = gen_1;
     918             : 
     919        5878 :   if (!signe(a) || !signe(b)) return gen_0;
     920        5878 :   da = degpol(a);
     921        5878 :   db = degpol(b);
     922        5878 :   if (db > da)
     923             :   {
     924           0 :     swapspec(a,b, da,db);
     925           0 :     if (both_odd(da,db)) res = subii(p, res);
     926             :   }
     927        5878 :   if (!da) return gen_1; /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
     928        5878 :   av = avma;
     929       33799 :   while (db)
     930             :   {
     931       22050 :     lb = gel(b,db+2);
     932       22050 :     c = FpX_rem(a,b, p);
     933       22050 :     a = b; b = c; dc = degpol(c);
     934       22050 :     if (dc < 0) { avma = av; return NULL; }
     935             : 
     936       22043 :     if (both_odd(da,db)) res = subii(p, res);
     937       22043 :     if (!equali1(lb)) res = Fp_mul(res, Fp_powu(lb, da - dc, p), p);
     938       22043 :     if (gc_needed(av,2))
     939             :     {
     940           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_resultant (da = %ld)",da);
     941           0 :       gerepileall(av,3, &a,&b,&res);
     942             :     }
     943       22043 :     da = db; /* = degpol(a) */
     944       22043 :     db = dc; /* = degpol(b) */
     945             :   }
     946        5871 :   res = Fp_mul(res, Fp_powu(gel(b,2), da, p), p);
     947        5871 :   return gerepileuptoint(av, res);
     948             : }
     949             : 
     950             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
     951             : GEN
     952          28 : FpX_disc(GEN P, GEN p)
     953             : {
     954          28 :   pari_sp av = avma;
     955          28 :   GEN L, dP = FpX_deriv(P,p), D = FpX_resultant(P, dP, p);
     956             :   long dd;
     957          28 :   if (!D || !signe(D)) return gen_0;
     958          21 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
     959          21 :   L = leading_coeff(P);
     960          21 :   if (dd && !equali1(L))
     961           7 :     D = (dd == -1)? Fp_div(D,L,p): Fp_mul(D, Fp_powu(L, dd, p), p);
     962          21 :   if (degpol(P) & 2) D = Fp_neg(D ,p);
     963          21 :   return gerepileuptoint(av, D);
     964             : }
     965             : 
     966             : GEN
     967       18650 : FpXV_prod(GEN V, GEN p)
     968             : {
     969             :   struct _FpXQ D;
     970       18650 :   D.p = p;
     971       18650 :   return gen_product(V, (void *)&D, &_FpX_mul);
     972             : }
     973             : 
     974             : GEN
     975        5306 : FpV_roots_to_pol(GEN V, GEN p, long v)
     976             : {
     977        5306 :   pari_sp ltop=avma;
     978             :   long i;
     979        5306 :   GEN g=cgetg(lg(V),t_VEC);
     980       40712 :   for(i=1;i<lg(V);i++)
     981       35406 :     gel(g,i) = deg1pol_shallow(gen_1,modii(negi(gel(V,i)),p),v);
     982        5306 :   return gerepileupto(ltop,FpXV_prod(g,p));
     983             : }
     984             : 
     985             : /* invert all elements of x mod p using Montgomery's multi-inverse trick.
     986             :  * Not stack-clean. */
     987             : GEN
     988        4160 : FpV_inv(GEN x, GEN p)
     989             : {
     990        4160 :   long i, lx = lg(x);
     991        4160 :   GEN u, y = cgetg(lx, t_VEC);
     992             : 
     993        4160 :   gel(y,1) = gel(x,1);
     994        4160 :   for (i=2; i<lx; i++) gel(y,i) = Fp_mul(gel(y,i-1), gel(x,i), p);
     995             : 
     996        4160 :   u = Fp_inv(gel(y,--i), p);
     997      328141 :   for ( ; i > 1; i--)
     998             :   {
     999      323981 :     gel(y,i) = Fp_mul(u, gel(y,i-1), p);
    1000      323981 :     u = Fp_mul(u, gel(x,i), p); /* u = 1 / (x[1] ... x[i-1]) */
    1001             :   }
    1002        4160 :   gel(y,1) = u; return y;
    1003             : }
    1004             : GEN
    1005           0 : FqV_inv(GEN x, GEN T, GEN p)
    1006             : {
    1007           0 :   long i, lx = lg(x);
    1008           0 :   GEN u, y = cgetg(lx, t_VEC);
    1009             : 
    1010           0 :   gel(y,1) = gel(x,1);
    1011           0 :   for (i=2; i<lx; i++) gel(y,i) = Fq_mul(gel(y,i-1), gel(x,i), T,p);
    1012             : 
    1013           0 :   u = Fq_inv(gel(y,--i), T,p);
    1014           0 :   for ( ; i > 1; i--)
    1015             :   {
    1016           0 :     gel(y,i) = Fq_mul(u, gel(y,i-1), T,p);
    1017           0 :     u = Fq_mul(u, gel(x,i), T,p); /* u = 1 / (x[1] ... x[i-1]) */
    1018             :   }
    1019           0 :   gel(y,1) = u; return y;
    1020             : }
    1021             : 
    1022             : /***********************************************************************/
    1023             : /**                                                                   **/
    1024             : /**                      Barrett reduction                            **/
    1025             : /**                                                                   **/
    1026             : /***********************************************************************/
    1027             : 
    1028             : static GEN
    1029        1490 : FpX_invBarrett_basecase(GEN T, GEN p)
    1030             : {
    1031        1490 :   long i, l=lg(T)-1, lr = l-1, k;
    1032        1490 :   GEN r=cgetg(lr, t_POL); r[1]=T[1];
    1033        1490 :   gel(r,2) = gen_1;
    1034       85740 :   for (i=3; i<lr; i++)
    1035             :   {
    1036       84250 :     pari_sp av = avma;
    1037       84250 :     GEN u = gel(T,l-i+2);
    1038     2674917 :     for (k=3; k<i; k++)
    1039     2590667 :       u = addii(u, mulii(gel(T,l-i+k), gel(r,k)));
    1040       84250 :     gel(r,i) = gerepileupto(av, modii(negi(u), p));
    1041             :   }
    1042        1490 :   return FpX_renormalize(r,lr);
    1043             : }
    1044             : 
    1045             : /* Return new lgpol */
    1046             : static long
    1047      206011 : ZX_lgrenormalizespec(GEN x, long lx)
    1048             : {
    1049             :   long i;
    1050      252928 :   for (i = lx-1; i>=0; i--)
    1051      252928 :     if (signe(gel(x,i))) break;
    1052      206011 :   return i+1;
    1053             : }
    1054             : 
    1055             : INLINE GEN
    1056      196031 : FpX_recipspec(GEN x, long l, long n)
    1057             : {
    1058      196031 :   return RgX_recipspec_shallow(x, l, n);
    1059             : }
    1060             : 
    1061             : static GEN
    1062         518 : FpX_invBarrett_Newton(GEN T, GEN p)
    1063             : {
    1064         518 :   pari_sp av = avma;
    1065         518 :   long nold, lx, lz, lq, l = degpol(T), i, lQ;
    1066         518 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
    1067         518 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
    1068         518 :   for (i=0;i<l;i++) gel(x,i) = gen_0;
    1069         518 :   q = FpX_recipspec(T+2,l+1,l+1); lQ = lgpol(q); q+=2;
    1070             :   /* We work on _spec_ FpX's, all the l[xzq] below are lgpol's */
    1071             : 
    1072             :   /* initialize */
    1073         518 :   gel(x,0) = Fp_inv(gel(q,0), p);
    1074         518 :   if (lQ>1) gel(q,1) = Fp_red(gel(q,1), p);
    1075         518 :   if (lQ>1 && signe(gel(q,1)))
    1076         493 :   {
    1077         467 :     GEN u = gel(q, 1);
    1078         467 :     if (!equali1(gel(x,0))) u = Fp_mul(u, Fp_sqr(gel(x,0), p), p);
    1079         467 :     gel(x,1) = Fp_neg(u, p); lx = 2;
    1080             :   }
    1081             :   else
    1082          51 :     lx = 1;
    1083         544 :   nold = 1;
    1084        4758 :   for (; mask > 1; )
    1085             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
    1086        3696 :     long i, lnew, nnew = nold << 1;
    1087             : 
    1088        3696 :     if (mask & 1) nnew--;
    1089        3696 :     mask >>= 1;
    1090             : 
    1091        3696 :     lnew = nnew + 1;
    1092        3696 :     lq = ZX_lgrenormalizespec(q, minss(lQ,lnew));
    1093        3696 :     z = FpX_mulspec(x, q, p, lx, lq); /* FIXME: high product */
    1094        3696 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
    1095        3696 :     z += 2;
    1096             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
    1097        3696 :     for (i = nold; i < lz; i++) if (signe(gel(z,i))) break;
    1098        3696 :     nold = nnew;
    1099        3696 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
    1100             : 
    1101             :     /* z + i represents (x*q - 1) / t^i */
    1102        3473 :     lz = ZX_lgrenormalizespec (z+i, lz-i);
    1103        3472 :     z = FpX_mulspec(x, z+i, p, lx, lz); /* FIXME: low product */
    1104        3473 :     lz = lgpol(z); z += 2;
    1105        3473 :     if (lz > lnew-i) lz = ZX_lgrenormalizespec(z, lnew-i);
    1106             : 
    1107        3473 :     lx = lz+ i;
    1108        3473 :     y  = x + i; /* x -= z * t^i, in place */
    1109        3473 :     for (i = 0; i < lz; i++) gel(y,i) = Fp_neg(gel(z,i), p);
    1110             :   }
    1111         518 :   x -= 2; setlg(x, lx + 2); x[1] = T[1];
    1112         518 :   return gerepilecopy(av, x);
    1113             : }
    1114             : 
    1115             : /* 1/polrecip(T)+O(x^(deg(T)-1)) */
    1116             : GEN
    1117        2040 : FpX_invBarrett(GEN T, GEN p)
    1118             : {
    1119        2040 :   pari_sp ltop = avma;
    1120        2040 :   long l = lg(T);
    1121             :   GEN r;
    1122        2040 :   if (l<5) return pol_0(varn(T));
    1123        2008 :   if (l<=FpX_INVBARRETT_LIMIT)
    1124             :   {
    1125        1490 :     GEN c = gel(T,l-1), ci=gen_1;
    1126        1490 :     if (!equali1(c))
    1127             :     {
    1128           0 :       ci = Fp_inv(c, p);
    1129           0 :       T = FpX_Fp_mul(T, ci, p);
    1130           0 :       r = FpX_invBarrett_basecase(T, p);
    1131           0 :       r = FpX_Fp_mul(r, ci, p);
    1132             :     } else
    1133        1490 :       r = FpX_invBarrett_basecase(T, p);
    1134             :   }
    1135             :   else
    1136         518 :     r = FpX_invBarrett_Newton(T, p);
    1137        2008 :   return gerepileupto(ltop, r);
    1138             : }
    1139             : 
    1140             : GEN
    1141      420248 : FpX_get_red(GEN T, GEN p)
    1142             : {
    1143      420248 :   if (typ(T)==t_POL && lg(T)>FpX_BARRETT_LIMIT)
    1144        1719 :     retmkvec2(FpX_invBarrett(T,p),T);
    1145      418529 :   return T;
    1146             : }
    1147             : 
    1148             : /* Compute x mod T where 2 <= degpol(T) <= l+1 <= 2*(degpol(T)-1)
    1149             :  * and mg is the Barrett inverse of T. */
    1150             : static GEN
    1151       97712 : FpX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN T, GEN p, GEN *pr)
    1152             : {
    1153             :   GEN q, r;
    1154       97712 :   long lt = degpol(T); /*We discard the leading term*/
    1155             :   long ld, lm, lT, lmg;
    1156       97712 :   ld = l-lt;
    1157       97712 :   lm = minss(ld, lgpol(mg));
    1158       97712 :   lT  = ZX_lgrenormalizespec(T+2,lt);
    1159       97712 :   lmg = ZX_lgrenormalizespec(mg+2,lm);
    1160       97712 :   q = FpX_recipspec(x+lt,ld,ld);              /* q = rec(x)     lq<=ld*/
    1161       97712 :   q = FpX_mulspec(q+2,mg+2,p,lgpol(q),lmg);    /* q = rec(x) * mg lq<=ld+lm*/
    1162       97712 :   q = FpX_recipspec(q+2,minss(ld,lgpol(q)),ld);/* q = rec (rec(x) * mg) lq<=ld*/
    1163       97712 :   if (!pr) return q;
    1164       97712 :   r = FpX_mulspec(q+2,T+2,p,lgpol(q),lT);      /* r = q*pol        lr<=ld+lt*/
    1165       97710 :   r = FpX_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* r = x - r   lr<=lt */
    1166       97712 :   if (pr == ONLY_REM) return r;
    1167         590 :   *pr = r; return q;
    1168             : }
    1169             : 
    1170             : static GEN
    1171       97316 : FpX_divrem_Barrett_noGC(GEN x, GEN mg, GEN T, GEN p, GEN *pr)
    1172             : {
    1173       97316 :   GEN q = NULL, r = FpX_red(x, p);
    1174       97316 :   long l = lgpol(r), lt = degpol(T), lm = 2*lt-1;
    1175             :   long i;
    1176       97316 :   if (l <= lt)
    1177             :   {
    1178           0 :     if (pr == ONLY_REM) return r;
    1179           0 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1180           0 :     if (pr) *pr = r;
    1181           0 :     return pol_0(varn(T));
    1182             :   }
    1183       97316 :   if (lt <= 1)
    1184          32 :     return FpX_divrem_basecase(r,T,p,pr);
    1185       97284 :   if (pr != ONLY_REM && l>lm)
    1186             :   {
    1187         128 :     q = cgetg(l-lt+2, t_POL);
    1188         128 :     for (i=0;i<l-lt;i++) gel(q+2,i) = gen_0;
    1189             :   }
    1190      194996 :   while (l>lm)
    1191             :   {
    1192         428 :     GEN zr, zq = FpX_divrem_Barrettspec(r+2+l-lm,lm,mg,T,p,&zr);
    1193         428 :     long lz = lgpol(zr);
    1194         428 :     if (pr != ONLY_REM)
    1195             :     {
    1196         268 :       long lq = lgpol(zq);
    1197         268 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1198             :     }
    1199         428 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
    1200         428 :     l = l-lm+lz;
    1201             :   }
    1202       97284 :   if (pr != ONLY_REM)
    1203             :   {
    1204         162 :     if (l > lt)
    1205             :     {
    1206         162 :       GEN zq = FpX_divrem_Barrettspec(r+2,l,mg,T,p,&r);
    1207         162 :       if (!q) q = zq;
    1208             :       else
    1209             :       {
    1210         128 :         long lq = lgpol(zq);
    1211         128 :         for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
    1212             :       }
    1213             :     }
    1214             :     else
    1215           0 :       r = FpX_renormalize(r, l+2);
    1216             :   }
    1217             :   else
    1218             :   {
    1219       97122 :     if (l > lt)
    1220       97122 :       r = FpX_divrem_Barrettspec(r+2, l, mg, T, p, ONLY_REM);
    1221             :     else
    1222           0 :       r = FpX_renormalize(r, l+2);
    1223       97122 :     r[1] = x[1]; return FpX_renormalize(r, lg(r));
    1224             :   }
    1225         162 :   if (pr) { r[1] = x[1]; r = FpX_renormalize(r, lg(r)); }
    1226         162 :   q[1] = x[1]; q = FpX_renormalize(q, lg(q));
    1227         162 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
    1228         162 :   if (pr) *pr = r;
    1229         162 :   return q;
    1230             : }
    1231             : 
    1232             : GEN
    1233     3630529 : FpX_divrem(GEN x, GEN T, GEN p, GEN *pr)
    1234             : {
    1235     3630529 :   GEN B, y = get_FpX_red(T, &B);
    1236     3630529 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1237     3630529 :   if (pr==ONLY_REM) return FpX_rem(x, y, p);
    1238     3630529 :   if (!B && d+3 < FpX_DIVREM_BARRETT_LIMIT)
    1239     3629532 :     return FpX_divrem_basecase(x,y,p,pr);
    1240         997 :   else if (lgefint(p)==3)
    1241             :   {
    1242         807 :     pari_sp av = avma;
    1243         807 :     ulong pp = to_Flxq(&x, &T, p);
    1244         807 :     GEN z = Flx_divrem(x, T, pp, pr);
    1245         807 :     if (!z) return NULL;
    1246         807 :     if (!pr || pr == ONLY_DIVIDES)
    1247           8 :       return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    1248         799 :     z = Flx_to_ZX(z);
    1249         799 :     *pr = Flx_to_ZX(*pr);
    1250         799 :     gerepileall(av, 2, &z, pr);
    1251         799 :     return z;
    1252             :   } else
    1253             :   {
    1254         190 :     pari_sp av=avma;
    1255         190 :     GEN mg = B? B: FpX_invBarrett(y, p);
    1256         190 :     GEN q1 = FpX_divrem_Barrett_noGC(x,mg,y,p,pr);
    1257         190 :     if (!q1) {avma=av; return NULL;}
    1258         190 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q1);
    1259         190 :     gerepileall(av,2,&q1,pr);
    1260         190 :     return q1;
    1261             :   }
    1262             : }
    1263             : 
    1264             : GEN
    1265    60913479 : FpX_rem(GEN x, GEN T, GEN p)
    1266             : {
    1267    60913479 :   GEN B, y = get_FpX_red(T, &B);
    1268    60913479 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1269    60913478 :   if (d < 0) return FpX_red(x,p);
    1270    58067744 :   if (!B && d+3 < FpX_REM_BARRETT_LIMIT)
    1271    57959216 :     return FpX_divrem_basecase(x,y,p,ONLY_REM);
    1272      108528 :   else if (lgefint(p)==3)
    1273             :   {
    1274       11402 :     pari_sp av = avma;
    1275       11402 :     ulong pp = to_Flxq(&x, &T, p);
    1276       11402 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, Flx_rem(x, T, pp)));
    1277             :   } else
    1278             :   {
    1279       97126 :     pari_sp av = avma;
    1280       97126 :     GEN mg = B? B: FpX_invBarrett(y, p);
    1281       97126 :     return gerepileupto(av, FpX_divrem_Barrett_noGC(x, mg, y, p, ONLY_REM));
    1282             :   }
    1283             : }
    1284             : 
    1285             : static GEN
    1286        8087 : deg2pol_shallow(GEN x2, GEN x1, GEN x0, long v)
    1287             : {
    1288        8087 :   GEN x = cgetg(5,t_POL);
    1289        8087 :   x[1] = evalsigne(1) | evalvarn(v);
    1290        8087 :   gel(x,2) = x0;
    1291        8087 :   gel(x,3) = x1;
    1292        8087 :   gel(x,4) = x2;
    1293        8087 :   return normalizepol_lg(x,5);
    1294             : }
    1295             : 
    1296             : static GEN
    1297        1737 : FpV_producttree(GEN xa, GEN s, GEN p, long vs)
    1298             : {
    1299        1737 :   long n = lg(xa)-1;
    1300        1737 :   long m = n==1 ? 1: expu(n-1)+1;
    1301        1737 :   long i, j, k, ls = lg(s);
    1302        1737 :   GEN T = cgetg(m+1, t_VEC);
    1303        1737 :   GEN t = cgetg(ls, t_VEC);
    1304       12743 :   for (j=1, k=1; j<ls; k+=s[j++])
    1305       22012 :     gel(t, j) = s[j] == 1 ?
    1306       19093 :              deg1pol(gen_1, Fp_neg(gel(xa,k), p), vs):
    1307       32348 :              deg2pol_shallow(gen_1,
    1308       16174 :                Fp_neg(Fp_add(gel(xa,k), gel(xa,k+1), p), p),
    1309       16174 :                Fp_mul(gel(xa,k), gel(xa,k+1), p), vs);
    1310        1737 :   gel(T,1) = t;
    1311        4825 :   for (i=2; i<=m; i++)
    1312             :   {
    1313        3088 :     GEN u = gel(T, i-1);
    1314        3088 :     long n = lg(u)-1;
    1315        3088 :     GEN t = cgetg(((n+1)>>1)+1, t_VEC);
    1316       12357 :     for (j=1, k=1; k<n; j++, k+=2)
    1317        9269 :       gel(t, j) = FpX_mul(gel(u, k), gel(u, k+1), p);
    1318        3088 :     gel(T, i) = t;
    1319             :   }
    1320        1737 :   return T;
    1321             : }
    1322             : 
    1323             : static GEN
    1324        1737 : FpX_FpV_multieval_tree(GEN P, GEN xa, GEN T, GEN p)
    1325             : {
    1326        1737 :   pari_sp av = avma;
    1327             :   long i,j,k;
    1328        1737 :   long m = lg(T)-1;
    1329             :   GEN t;
    1330        1737 :   GEN Tp = cgetg(m+1, t_VEC);
    1331        1737 :   gel(Tp, m) = mkvec(P);
    1332        4825 :   for (i=m-1; i>=1; i--)
    1333             :   {
    1334        3088 :     GEN u = gel(T, i);
    1335        3088 :     GEN v = gel(Tp, i+1);
    1336        3088 :     long n = lg(u)-1;
    1337        3088 :     t = cgetg(n+1, t_VEC);
    1338       12357 :     for (j=1, k=1; k<n; j++, k+=2)
    1339             :     {
    1340        9269 :       gel(t, k)   = FpX_rem(gel(v, j), gel(u, k), p);
    1341        9269 :       gel(t, k+1) = FpX_rem(gel(v, j), gel(u, k+1), p);
    1342             :     }
    1343        3088 :     gel(Tp, i) = t;
    1344             :   }
    1345             :   {
    1346        1737 :     GEN R = cgetg(lg(xa), t_VEC);
    1347        1737 :     GEN u = gel(T, i+1);
    1348        1737 :     GEN v = gel(Tp, i+1);
    1349        1737 :     long n = lg(u)-1;
    1350       12743 :     for (j=1, k=1; j<=n; j++)
    1351             :     {
    1352       11006 :       long c, d = degpol(gel(u,j));
    1353       30099 :       for (c=1; c<=d; c++, k++)
    1354       19093 :         gel(R,k) = FpX_eval(gel(v, j), gel(xa,k), p);
    1355             :     }
    1356        1737 :     return gerepileupto(av, R);
    1357             :   }
    1358             : }
    1359             : 
    1360             : static GEN
    1361           1 : FpVV_polint_tree(GEN T, GEN R, GEN s, GEN xa, GEN ya, GEN p, long vs)
    1362             : {
    1363           1 :   pari_sp av = avma;
    1364           1 :   long m = lg(T)-1;
    1365           1 :   long i, j, k, ls = lg(s);
    1366           1 :   GEN Tp = cgetg(m+1, t_VEC);
    1367           1 :   GEN t = cgetg(ls, t_VEC);
    1368           3 :   for (j=1, k=1; j<ls; k+=s[j++])
    1369           2 :     if (s[j]==2)
    1370             :     {
    1371           2 :       GEN a = Fp_mul(gel(ya,k), gel(R,k), p);
    1372           2 :       GEN b = Fp_mul(gel(ya,k+1), gel(R,k+1), p);
    1373           6 :       gel(t, j) = deg1pol(Fp_add(a, b, p),
    1374           2 :               Fp_neg(Fp_add(Fp_mul(gel(xa,k), b, p ),
    1375           2 :               Fp_mul(gel(xa,k+1), a, p), p), p), vs);
    1376             :     }
    1377             :     else
    1378           0 :       gel(t, j) = scalarpol(Fp_mul(gel(ya,k), gel(R,k), p), vs);
    1379           1 :   gel(Tp, 1) = t;
    1380           2 :   for (i=2; i<=m; i++)
    1381             :   {
    1382           1 :     GEN u = gel(T, i-1);
    1383           1 :     GEN t = cgetg(lg(gel(T,i)), t_VEC);
    1384           1 :     GEN v = gel(Tp, i-1);
    1385           1 :     long n = lg(v)-1;
    1386           2 :     for (j=1, k=1; k<n; j++, k+=2)
    1387           3 :       gel(t, j) = FpX_add(ZX_mul(gel(u, k), gel(v, k+1)),
    1388           2 :                           ZX_mul(gel(u, k+1), gel(v, k)), p);
    1389           1 :     gel(Tp, i) = t;
    1390             :   }
    1391           1 :   return gerepilecopy(av, gmael(Tp,m,1));
    1392             : }
    1393             : 
    1394             : GEN
    1395           0 : FpX_FpV_multieval(GEN P, GEN xa, GEN p)
    1396             : {
    1397           0 :   pari_sp av = avma;
    1398           0 :   GEN s = producttree_scheme(lg(xa)-1);
    1399           0 :   GEN T = FpV_producttree(xa, s, p, varn(P));
    1400           0 :   return gerepileupto(av, FpX_FpV_multieval_tree(P, xa, T, p));
    1401             : }
    1402             : 
    1403             : GEN
    1404           1 : FpV_polint(GEN xa, GEN ya, GEN p, long vs)
    1405             : {
    1406           1 :   pari_sp av = avma;
    1407           1 :   GEN s = producttree_scheme(lg(xa)-1);
    1408           1 :   GEN T = FpV_producttree(xa, s, p, vs);
    1409           1 :   long m = lg(T)-1;
    1410           1 :   GEN P = FpX_deriv(gmael(T, m, 1), p);
    1411           1 :   GEN R = FpV_inv(FpX_FpV_multieval_tree(P, xa, T, p), p);
    1412           1 :   return gerepileupto(av, FpVV_polint_tree(T, R, s, xa, ya, p, vs));
    1413             : }
    1414             : 
    1415             : GEN
    1416           0 : FpV_FpM_polint(GEN xa, GEN ya, GEN p, long vs)
    1417             : {
    1418           0 :   pari_sp av = avma;
    1419           0 :   GEN s = producttree_scheme(lg(xa)-1);
    1420           0 :   GEN T = FpV_producttree(xa, s, p, vs);
    1421           0 :   long i, m = lg(T)-1, l = lg(ya)-1;
    1422           0 :   GEN P = FpX_deriv(gmael(T, m, 1), p);
    1423           0 :   GEN R = FpV_inv(FpX_FpV_multieval_tree(P, xa, T, p), p);
    1424           0 :   GEN M = cgetg(l+1, t_VEC);
    1425           0 :   for (i=1; i<=l; i++)
    1426           0 :     gel(M,i) = FpVV_polint_tree(T, R, s, xa, gel(ya,i), p, vs);
    1427           0 :   return gerepileupto(av, M);
    1428             : }
    1429             : 
    1430             : GEN
    1431        1736 : FpV_invVandermonde(GEN L, GEN den, GEN p)
    1432             : {
    1433        1736 :   pari_sp av = avma;
    1434        1736 :   long i, n = lg(L);
    1435             :   GEN M, R;
    1436        1736 :   GEN s = producttree_scheme(n-1);
    1437        1736 :   GEN tree = FpV_producttree(L, s, p, 0);
    1438        1736 :   long m = lg(tree)-1;
    1439        1736 :   GEN T = gmael(tree, m, 1);
    1440        1736 :   R = FpV_inv(FpX_FpV_multieval_tree(FpX_deriv(T, p), L, tree, p), p);
    1441        1736 :   if (den) R = FpC_Fp_mul(R, den, p);
    1442        1736 :   M = cgetg(n, t_MAT);
    1443       20825 :   for (i = 1; i < n; i++)
    1444             :   {
    1445       19089 :     GEN P = FpX_Fp_mul(FpX_div_by_X_x(T, gel(L,i), p, NULL), gel(R,i), p);
    1446       19089 :     gel(M,i) = RgX_to_RgC(P, n-1);
    1447             :   }
    1448        1736 :   return gerepilecopy(av, M);
    1449             : }
    1450             : 
    1451             : /***********************************************************************/
    1452             : /**                                                                   **/
    1453             : /**                              FpXQ                                 **/
    1454             : /**                                                                   **/
    1455             : /***********************************************************************/
    1456             : 
    1457             : /* FpXQ are elements of Fp[X]/(T), represented by FpX*/
    1458             : 
    1459             : GEN
    1460     2639212 : FpXQ_red(GEN x, GEN T, GEN p)
    1461             : {
    1462     2639212 :   GEN z = FpX_red(x,p);
    1463     2639212 :   return FpX_rem(z, T,p);
    1464             : }
    1465             : 
    1466             : GEN
    1467    39904890 : FpXQ_mul(GEN x,GEN y,GEN T,GEN p)
    1468             : {
    1469    39904890 :   GEN z = FpX_mul(x,y,p);
    1470    39904891 :   return FpX_rem(z, T, p);
    1471             : }
    1472             : 
    1473             : GEN
    1474     3057452 : FpXQ_sqr(GEN x, GEN T, GEN p)
    1475             : {
    1476     3057452 :   GEN z = FpX_sqr(x,p);
    1477     3057452 :   return FpX_rem(z, T, p);
    1478             : }
    1479             : 
    1480             : /* Inverse of x in Z/pZ[X]/(pol) or NULL if inverse doesn't exist
    1481             :  * return lift(1 / (x mod (p,pol))) */
    1482             : GEN
    1483      285874 : FpXQ_invsafe(GEN x, GEN y, GEN p)
    1484             : {
    1485      285874 :   GEN V, z = FpX_extgcd(get_FpX_mod(y), x, p, NULL, &V);
    1486      285874 :   if (degpol(z)) return NULL;
    1487      285874 :   z = Fp_invsafe(gel(z,2), p);
    1488      285874 :   if (!z) return NULL;
    1489      285874 :   return FpX_Fp_mul(V, z, p);
    1490             : }
    1491             : 
    1492             : GEN
    1493      285860 : FpXQ_inv(GEN x,GEN T,GEN p)
    1494             : {
    1495      285860 :   pari_sp av = avma;
    1496      285860 :   GEN U = FpXQ_invsafe(x, T, p);
    1497      285860 :   if (!U) pari_err_INV("FpXQ_inv",x);
    1498      285860 :   return gerepileupto(av, U);
    1499             : }
    1500             : 
    1501             : GEN
    1502      228360 : FpXQ_div(GEN x,GEN y,GEN T,GEN p)
    1503             : {
    1504      228360 :   pari_sp av = avma;
    1505      228360 :   return gerepileupto(av, FpXQ_mul(x,FpXQ_inv(y,T,p),T,p));
    1506             : }
    1507             : 
    1508             : static GEN
    1509     1052847 : _FpXQ_add(void *data, GEN x, GEN y)
    1510             : {
    1511             :   (void) data;
    1512     1052847 :   return ZX_add(x, y);
    1513             : }
    1514             : static GEN
    1515       57554 : _FpXQ_sub(void *data, GEN x, GEN y)
    1516             : {
    1517             :   (void) data;
    1518       57554 :   return ZX_sub(x, y);
    1519             : }
    1520             : static GEN
    1521     1181040 : _FpXQ_cmul(void *data, GEN P, long a, GEN x)
    1522             : {
    1523             :   (void) data;
    1524     1181040 :   return ZX_Z_mul(x, gel(P,a+2));
    1525             : }
    1526             : static GEN
    1527     2589431 : _FpXQ_sqr(void *data, GEN x)
    1528             : {
    1529     2589431 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1530     2589431 :   return FpXQ_sqr(x, D->T, D->p);
    1531             : }
    1532             : static GEN
    1533      868943 : _FpXQ_mul(void *data, GEN x, GEN y)
    1534             : {
    1535      868943 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1536      868943 :   return FpXQ_mul(x,y, D->T, D->p);
    1537             : }
    1538             : static GEN
    1539        3537 : _FpXQ_zero(void *data)
    1540             : {
    1541        3537 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1542        3537 :   return pol_0(get_FpX_var(D->T));
    1543             : }
    1544             : static GEN
    1545      308392 : _FpXQ_one(void *data)
    1546             : {
    1547      308392 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1548      308392 :   return pol_1(get_FpX_var(D->T));
    1549             : }
    1550             : static GEN
    1551      352469 : _FpXQ_red(void *data, GEN x)
    1552             : {
    1553      352469 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1554      352469 :   return FpX_red(x,D->p);
    1555             : }
    1556             : 
    1557             : static struct bb_algebra FpXQ_algebra = { _FpXQ_red, _FpXQ_add, _FpXQ_sub,
    1558             :        _FpXQ_mul, _FpXQ_sqr, _FpXQ_one, _FpXQ_zero };
    1559             : 
    1560             : const struct bb_algebra *
    1561       13559 : get_FpXQ_algebra(void **E, GEN T, GEN p)
    1562             : {
    1563       13559 :   GEN z = new_chunk(sizeof(struct _FpXQ));
    1564       13559 :   struct _FpXQ *e = (struct _FpXQ *) z;
    1565       13559 :   e->T = FpX_get_red(T, p);
    1566       13559 :   e->p  = p; *E = (void*)e;
    1567       13559 :   return &FpXQ_algebra;
    1568             : }
    1569             : 
    1570             : static struct bb_algebra FpX_algebra = { _FpXQ_red, _FpXQ_add, _FpXQ_sub,
    1571             :        _FpX_mul, _FpX_sqr, _FpXQ_one, _FpXQ_zero };
    1572             : 
    1573             : const struct bb_algebra *
    1574           0 : get_FpX_algebra(void **E, GEN p, long v)
    1575             : {
    1576           0 :   GEN z = new_chunk(sizeof(struct _FpXQ));
    1577           0 :   struct _FpXQ *e = (struct _FpXQ *) z;
    1578           0 :   e->T = pol_x(v);
    1579           0 :   e->p  = p; *E = (void*)e;
    1580           0 :   return &FpX_algebra;
    1581             : }
    1582             : 
    1583             : /* x,pol in Z[X], p in Z, n in Z, compute lift(x^n mod (p, pol)) */
    1584             : GEN
    1585      514881 : FpXQ_pow(GEN x, GEN n, GEN T, GEN p)
    1586             : {
    1587             :   struct _FpXQ D;
    1588             :   pari_sp av;
    1589      514881 :   long s = signe(n);
    1590             :   GEN y;
    1591      514881 :   if (!s) return pol_1(varn(x));
    1592      513169 :   if (is_pm1(n)) /* +/- 1 */
    1593        9080 :     return (s < 0)? FpXQ_inv(x,T,p): FpXQ_red(x,T,p);
    1594      504089 :   av = avma;
    1595      504089 :   if (!is_bigint(p))
    1596             :   {
    1597      342322 :     ulong pp = to_Flxq(&x, &T, p);
    1598      342322 :     y = Flxq_pow(x, n, T, pp);
    1599      342322 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, y));
    1600             :   }
    1601      161767 :   if (s < 0) x = FpXQ_inv(x,T,p);
    1602      161767 :   D.p = p; D.T = FpX_get_red(T,p);
    1603      161767 :   y = gen_pow(x, n, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul);
    1604      161767 :   return gerepileupto(av, y);
    1605             : }
    1606             : 
    1607             : GEN /*Assume n is very small*/
    1608       62860 : FpXQ_powu(GEN x, ulong n, GEN T, GEN p)
    1609             : {
    1610             :   struct _FpXQ D;
    1611             :   pari_sp av;
    1612             :   GEN y;
    1613       62860 :   if (!n) return pol_1(varn(x));
    1614       62860 :   if (n==1) return FpXQ_red(x,T,p);
    1615       32669 :   av = avma;
    1616       32669 :   if (!is_bigint(p))
    1617             :   {
    1618       32350 :     ulong pp = to_Flxq(&x, &T, p);
    1619       32350 :     y = Flxq_powu(x, n, T, pp);
    1620       32350 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, y));
    1621             :   }
    1622         319 :   D.T = FpX_get_red(T, p); D.p = p;
    1623         319 :   y = gen_powu(x, n, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul);
    1624         319 :   return gerepileupto(av, y);
    1625             : }
    1626             : 
    1627             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    1628             : GEN
    1629      184728 : FpXQ_powers(GEN x, long l, GEN T, GEN p)
    1630             : {
    1631             :   struct _FpXQ D;
    1632             :   int use_sqr;
    1633      184728 :   if (l>2 && lgefint(p) == 3) {
    1634      152734 :     pari_sp av = avma;
    1635      152734 :     ulong pp = to_Flxq(&x, &T, p);
    1636      152734 :     GEN z = FlxV_to_ZXV(Flxq_powers(x, l, T, pp));
    1637      152734 :     return gerepileupto(av, z);
    1638             :   }
    1639       31994 :   use_sqr = 2*degpol(x)>=get_FpX_degree(T);
    1640       31994 :   D.T = FpX_get_red(T,p); D.p = p;
    1641       31994 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul,&_FpXQ_one);
    1642             : }
    1643             : 
    1644             : GEN
    1645        3876 : FpXQ_matrix_pow(GEN y, long n, long m, GEN P, GEN l)
    1646             : {
    1647        3876 :   return RgXV_to_RgM(FpXQ_powers(y,m-1,P,l),n);
    1648             : }
    1649             : 
    1650             : GEN
    1651      156140 : FpX_Frobenius(GEN T, GEN p)
    1652             : {
    1653      156140 :   return FpXQ_pow(pol_x(get_FpX_var(T)), p, T, p);
    1654             : }
    1655             : 
    1656             : GEN
    1657        2581 : FpX_matFrobenius(GEN T, GEN p)
    1658             : {
    1659        2581 :   long n = get_FpX_degree(T);
    1660        2581 :   return FpXQ_matrix_pow(FpX_Frobenius(T, p), n, n, T, p);
    1661             : }
    1662             : 
    1663             : GEN
    1664      125351 : FpX_FpXQV_eval(GEN Q, GEN x, GEN T, GEN p)
    1665             : {
    1666             :   struct _FpXQ D;
    1667      125351 :   D.T = FpX_get_red(T,p); D.p = p;
    1668      125351 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&D,&FpXQ_algebra,_FpXQ_cmul);
    1669             : }
    1670             : 
    1671             : GEN
    1672      169869 : FpX_FpXQ_eval(GEN Q, GEN x, GEN T, GEN p)
    1673             : {
    1674             :   struct _FpXQ D;
    1675             :   int use_sqr;
    1676      169869 :   if (lgefint(p) == 3)
    1677             :   {
    1678      166619 :     pari_sp av = avma;
    1679      166619 :     ulong pp = to_Flxq(&x, &T, p);
    1680      166619 :     GEN z = Flx_Flxq_eval(ZX_to_Flx(Q, pp), x, T, pp);
    1681      166619 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    1682             :   }
    1683        3250 :   use_sqr = 2*degpol(x) >= get_FpX_degree(T);
    1684        3250 :   D.T = FpX_get_red(T,p); D.p = p;
    1685        3250 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&D,&FpXQ_algebra,_FpXQ_cmul);
    1686             : }
    1687             : 
    1688             : GEN
    1689         854 : FpXC_FpXQV_eval(GEN P, GEN x, GEN T, GEN p)
    1690             : {
    1691         854 :   long i, l = lg(P);
    1692         854 :   GEN res = cgetg(l, t_COL);
    1693        3682 :   for (i=1; i<l; i++)
    1694        2828 :     gel(res,i) = FpX_FpXQV_eval(gel(P,i), x, T, p);
    1695         854 :   return res;
    1696             : }
    1697             : 
    1698             : GEN
    1699         308 : FpXM_FpXQV_eval(GEN Q, GEN x, GEN T, GEN p)
    1700             : {
    1701         308 :   long i, l = lg(Q);
    1702         308 :   GEN y = cgetg(l, t_MAT);
    1703        1162 :   for (i=1; i<l; i++)
    1704         854 :     gel(y,i) = FpXC_FpXQV_eval(gel(Q,i), x, T, p);
    1705         308 :   return y;
    1706             : }
    1707             : 
    1708             : GEN
    1709         609 : FpXQ_autpowers(GEN aut, long f, GEN T, GEN p)
    1710             : {
    1711         609 :   pari_sp av = avma;
    1712         609 :   long n = get_FpX_degree(T);
    1713         609 :   long i, nautpow = brent_kung_optpow(n-1,f-2,1);
    1714         609 :   long v = get_FpX_var(T);
    1715             :   GEN autpow, V;
    1716         609 :   T = FpX_get_red(T, p);
    1717         609 :   autpow = FpXQ_powers(aut, nautpow,T,p);
    1718         609 :   V = cgetg(f + 2, t_VEC);
    1719         609 :   gel(V,1) = pol_x(v); if (f==0) return gerepileupto(av, V);
    1720         609 :   gel(V,2) = gcopy(aut);
    1721        3129 :   for (i = 3; i <= f+1; i++)
    1722        2520 :     gel(V,i) = FpX_FpXQV_eval(gel(V,i-1),autpow,T,p);
    1723         609 :   return gerepileupto(av, V);
    1724             : }
    1725             : 
    1726             : static GEN
    1727         542 : FpXQ_autpow_sqr(void *E, GEN x)
    1728             : {
    1729         542 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1730         542 :   return FpX_FpXQ_eval(x, x, D->T, D->p);
    1731             : }
    1732             : 
    1733             : static GEN
    1734           7 : FpXQ_autpow_mul(void *E, GEN x, GEN y)
    1735             : {
    1736           7 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1737           7 :   return FpX_FpXQ_eval(x, y, D->T, D->p);
    1738             : }
    1739             : 
    1740             : GEN
    1741         514 : FpXQ_autpow(GEN x, ulong n, GEN T, GEN p)
    1742             : {
    1743             :   struct _FpXQ D;
    1744         514 :   D.T = FpX_get_red(T, p); D.p = p;
    1745         514 :   if (n==0) return pol_x(varn(x));
    1746         514 :   if (n==1) return ZX_copy(x);
    1747         514 :   return gen_powu(x,n,(void*)&D,FpXQ_autpow_sqr,FpXQ_autpow_mul);
    1748             : }
    1749             : 
    1750             : static GEN
    1751           7 : FpXQ_auttrace_mul(void *E, GEN x, GEN y)
    1752             : {
    1753           7 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1754           7 :   GEN T = D->T, p = D->p;
    1755           7 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1756           7 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1757           7 :   ulong d = brent_kung_optpow(maxss(degpol(phi2),degpol(a2)),2,1);
    1758           7 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1759           7 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1760           7 :   GEN aphi = FpX_FpXQV_eval(a2, V1, T, p);
    1761           7 :   GEN a3 = FpX_add(a1, aphi, p);
    1762           7 :   return mkvec2(phi3, a3);
    1763             : }
    1764             : 
    1765             : static GEN
    1766           7 : FpXQ_auttrace_sqr(void *E, GEN x)
    1767           7 : { return FpXQ_auttrace_mul(E, x, x); }
    1768             : 
    1769             : GEN
    1770          14 : FpXQ_auttrace(GEN x, ulong n, GEN T, GEN p)
    1771             : {
    1772             :   struct _FpXQ D;
    1773          14 :   D.T = FpX_get_red(T, p); D.p = p;
    1774          14 :   return gen_powu(x,n,(void*)&D,FpXQ_auttrace_sqr,FpXQ_auttrace_mul);
    1775             : }
    1776             : 
    1777             : static GEN
    1778        1551 : FpXQ_autsum_mul(void *E, GEN x, GEN y)
    1779             : {
    1780        1551 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1781        1551 :   GEN T = D->T, p = D->p;
    1782        1551 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1783        1551 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1784        1551 :   ulong d = brent_kung_optpow(maxss(degpol(phi2),degpol(a2)),2,1);
    1785        1551 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1786        1551 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1787        1551 :   GEN aphi = FpX_FpXQV_eval(a2, V1, T, p);
    1788        1551 :   GEN a3 = FpXQ_mul(a1, aphi, T, p);
    1789        1551 :   return mkvec2(phi3, a3);
    1790             : }
    1791             : static GEN
    1792         809 : FpXQ_autsum_sqr(void *E, GEN x)
    1793         809 : { return FpXQ_autsum_mul(E, x, x); }
    1794             : 
    1795             : GEN
    1796         795 : FpXQ_autsum(GEN x, ulong n, GEN T, GEN p)
    1797             : {
    1798             :   struct _FpXQ D;
    1799         795 :   D.T = FpX_get_red(T, p); D.p = p;
    1800         795 :   return gen_powu(x,n,(void*)&D,FpXQ_autsum_sqr,FpXQ_autsum_mul);
    1801             : }
    1802             : 
    1803             : static GEN
    1804         308 : FpXQM_autsum_mul(void *E, GEN x, GEN y)
    1805             : {
    1806         308 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1807         308 :   GEN T = D->T, p = D->p;
    1808         308 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1809         308 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1810         308 :   long g = lg(a2)-1, dT = get_FpX_degree(T);
    1811         308 :   ulong d = brent_kung_optpow(dT-1, g*g+1, 1);
    1812         308 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1813         308 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1814         308 :   GEN aphi = FpXM_FpXQV_eval(a2, V1, T, p);
    1815         308 :   GEN a3 = FqM_mul(a1, aphi, T, p);
    1816         308 :   return mkvec2(phi3, a3);
    1817             : }
    1818             : static GEN
    1819         210 : FpXQM_autsum_sqr(void *E, GEN x)
    1820         210 : { return FpXQM_autsum_mul(E, x, x); }
    1821             : 
    1822             : GEN
    1823         140 : FpXQM_autsum(GEN x, ulong n, GEN T, GEN p)
    1824             : {
    1825             :   struct _FpXQ D;
    1826         140 :   D.T = FpX_get_red(T, p); D.p = p;
    1827         140 :   return gen_powu(x, n, (void*)&D, FpXQM_autsum_sqr, FpXQM_autsum_mul);
    1828             : }
    1829             : 
    1830             : static long
    1831        5382 : bounded_order(GEN p, GEN b, long k)
    1832             : {
    1833             :   long i;
    1834        5382 :   GEN a=modii(p,b);
    1835       10848 :   for(i=1;i<k;i++)
    1836             :   {
    1837        6698 :     if (equali1(a))
    1838        1232 :       return i;
    1839        5466 :     a = Fp_mul(a,p,b);
    1840             :   }
    1841        4150 :   return 0;
    1842             : }
    1843             : 
    1844             : /*
    1845             :   n = (p^d-a)\b
    1846             :   b = bb*p^vb
    1847             :   p^k = 1 [bb]
    1848             :   d = m*k+r+vb
    1849             :   u = (p^k-1)/bb;
    1850             :   v = (p^(r+vb)-a)/b;
    1851             :   w = (p^(m*k)-1)/(p^k-1)
    1852             :   n = p^r*w*u+v
    1853             :   w*u = p^vb*(p^(m*k)-1)/b
    1854             :   n = p^(r+vb)*(p^(m*k)-1)/b+(p^(r+vb)-a)/b
    1855             : */
    1856             : 
    1857             : static GEN
    1858      150882 : FpXQ_pow_Frobenius(GEN x, GEN n, GEN aut, GEN T, GEN p)
    1859             : {
    1860      150882 :   pari_sp av=avma;
    1861      150882 :   long d = get_FpX_degree(T);
    1862      150882 :   GEN an = absi(n), z, q;
    1863      150882 :   if (cmpii(an,p)<0 || cmpis(an,d)<=0)
    1864      145500 :     return FpXQ_pow(x, n, T, p);
    1865        5382 :   q = powiu(p, d);
    1866        5382 :   if (dvdii(q, n))
    1867             :   {
    1868           0 :     long vn = logint(an,p);
    1869           0 :     GEN autvn = vn==1 ? aut: FpXQ_autpow(aut,vn,T,p);
    1870           0 :     z = FpX_FpXQ_eval(x,autvn,T,p);
    1871             :   } else
    1872             :   {
    1873        5382 :     GEN b = diviiround(q, an), a = subii(q, mulii(an,b));
    1874             :     GEN bb, u, v, autk;
    1875        5382 :     long vb = Z_pvalrem(b,p,&bb);
    1876        5382 :     long m, r, k = is_pm1(bb) ? 1 : bounded_order(p,bb,d);
    1877        5382 :     if (!k || d-vb<k) return FpXQ_pow(x,n, T, p);
    1878        1232 :     m = (d-vb)/k; r = (d-vb)%k;
    1879        1232 :     u = diviiexact(subis(powiu(p,k),1),bb);
    1880        1232 :     v = diviiexact(subii(powiu(p,r+vb),a),b);
    1881        1232 :     autk = k==1 ? aut: FpXQ_autpow(aut,k,T,p);
    1882        1232 :     if (r)
    1883             :     {
    1884         444 :       GEN autr = r==1 ? aut: FpXQ_autpow(aut,r,T,p);
    1885         444 :       z = FpX_FpXQ_eval(x,autr,T,p);
    1886         788 :     } else z = x;
    1887        1232 :     if (m > 1) z = gel(FpXQ_autsum(mkvec2(autk, z), m, T, p), 2);
    1888        1232 :     if (!is_pm1(u)) z = FpXQ_pow(z, u, T, p);
    1889        1232 :     if (signe(v)) z = FpXQ_mul(z, FpXQ_pow(x, v, T, p), T, p);
    1890             :   }
    1891        1232 :   return gerepileupto(av,signe(n)>0 ? z : FpXQ_inv(z,T,p));
    1892             : }
    1893             : 
    1894             : /* assume T irreducible mod p */
    1895             : int
    1896        3258 : FpXQ_issquare(GEN x, GEN T, GEN p)
    1897             : {
    1898             :   pari_sp av;
    1899             :   long res;
    1900        3258 :   if (lg(x) == 2 || absequalui(2, p)) return 1;
    1901        3258 :   if (lg(x) == 3) return Fq_issquare(gel(x,2), T, p);
    1902             :   /* Ng = g^((q-1)/(p-1)) */
    1903        3209 :   av = avma; res = kronecker(FpXQ_norm(x,T,p), p) == 1;
    1904        3209 :   avma = av; return res;
    1905             : }
    1906             : int
    1907       90797 : Fp_issquare(GEN x, GEN p)
    1908             : {
    1909       90797 :   if (absequalui(2, p)) return 1;
    1910       90797 :   return kronecker(x, p) == 1;
    1911             : }
    1912             : /* assume T irreducible mod p */
    1913             : int
    1914       90650 : Fq_issquare(GEN x, GEN T, GEN p)
    1915             : {
    1916       90650 :   if (typ(x) != t_INT) return FpXQ_issquare(x, T, p);
    1917       90615 :   return (T && ! odd(get_FpX_degree(T))) || Fp_issquare(x, p);
    1918             : }
    1919             : 
    1920             : long
    1921         133 : Fq_ispower(GEN x, GEN K, GEN T, GEN p)
    1922             : {
    1923         133 :   pari_sp av = avma;
    1924             :   long d;
    1925             :   GEN Q;
    1926         133 :   if (!T) return Fp_ispower(x,K,p);
    1927         112 :   d = get_FpX_degree(T);
    1928         112 :   if (!umodui(d, K)) return 1;
    1929          63 :   Q = subiu(powiu(p,d), 1);
    1930          63 :   Q = diviiexact(Q, gcdii(Q, K));
    1931          63 :   d = gequal1(Fq_pow(x, Q, T,p));
    1932          63 :   avma = av; return d;
    1933             : }
    1934             : 
    1935             : /* discrete log in FpXQ for a in Fp^*, g in FpXQ^* of order ord */
    1936             : GEN
    1937        5626 : Fp_FpXQ_log(GEN a, GEN g, GEN o, GEN T, GEN p)
    1938             : {
    1939        5626 :   pari_sp av = avma;
    1940             :   GEN q,n_q,ord,ordp, op;
    1941             : 
    1942        5626 :   if (equali1(a)) return gen_0;
    1943             :   /* p > 2 */
    1944             : 
    1945        3643 :   ordp = subis(p, 1); /* even */
    1946        3643 :   ord  = get_arith_Z(o);
    1947        3615 :   if (!ord) ord = T? subis(powiu(p, get_FpX_degree(T)), 1): ordp;
    1948        3615 :   if (equalii(a, ordp)) /* -1 */
    1949        2177 :     return gerepileuptoint(av, shifti(ord,-1));
    1950        1438 :   ordp = gcdii(ordp,ord);
    1951        1438 :   op = typ(o)==t_MAT ? famat_Z_gcd(o,ordp) : ordp;
    1952             : 
    1953        1438 :   q = NULL;
    1954        1438 :   if (T)
    1955             :   { /* we want < g > = Fp^* */
    1956        1438 :     if (!equalii(ord,ordp)) {
    1957        1428 :       q = diviiexact(ord,ordp);
    1958        1428 :       g = FpXQ_pow(g,q,T,p);
    1959             :     }
    1960        1438 :     g = constant_coeff(g);
    1961             :   }
    1962        1438 :   n_q = Fp_log(a,g,op,p);
    1963        1438 :   if (lg(n_q)==1) return gerepileuptoleaf(av, n_q);
    1964        1438 :   if (q) n_q = mulii(q, n_q);
    1965        1438 :   return gerepileuptoint(av, n_q);
    1966             : }
    1967             : 
    1968             : static GEN
    1969      150173 : _FpXQ_pow(void *data, GEN x, GEN n)
    1970             : {
    1971      150173 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1972      150173 :   return FpXQ_pow_Frobenius(x,n, D->aut, D->T, D->p);
    1973             : }
    1974             : 
    1975             : static GEN
    1976        3384 : _FpXQ_rand(void *data)
    1977             : {
    1978        3384 :   pari_sp av=avma;
    1979        3384 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1980             :   GEN z;
    1981             :   do
    1982             :   {
    1983        3384 :     avma=av;
    1984        3384 :     z=random_FpX(get_FpX_degree(D->T),get_FpX_var(D->T),D->p);
    1985        3384 :   } while (!signe(z));
    1986        3384 :   return z;
    1987             : }
    1988             : 
    1989             : static GEN
    1990        1443 : _FpXQ_easylog(void *E, GEN a, GEN g, GEN ord)
    1991             : {
    1992        1443 :   struct _FpXQ *s=(struct _FpXQ*) E;
    1993        1443 :   if (degpol(a)) return NULL;
    1994        1426 :   return Fp_FpXQ_log(constant_coeff(a),g,ord,s->T,s->p);
    1995             : }
    1996             : 
    1997             : static const struct bb_group FpXQ_star={_FpXQ_mul,_FpXQ_pow,_FpXQ_rand,hash_GEN,ZX_equal,ZX_equal1,_FpXQ_easylog};
    1998             : 
    1999             : const struct bb_group *
    2000        1782 : get_FpXQ_star(void **E, GEN T, GEN p)
    2001             : {
    2002        1782 :   struct _FpXQ *e = (struct _FpXQ *) stack_malloc(sizeof(struct _FpXQ));
    2003        1782 :   e->T = T; e->p  = p; e->aut =  FpX_Frobenius(T, p);
    2004        1782 :   *E = (void*)e; return &FpXQ_star;
    2005             : }
    2006             : 
    2007             : GEN
    2008          29 : FpXQ_order(GEN a, GEN ord, GEN T, GEN p)
    2009             : {
    2010          29 :   if (lgefint(p)==3)
    2011             :   {
    2012           0 :     pari_sp av=avma;
    2013           0 :     ulong pp = to_Flxq(&a, &T, p);
    2014           0 :     GEN z = Flxq_order(a, ord, T, pp);
    2015           0 :     return gerepileuptoint(av,z);
    2016             :   }
    2017             :   else
    2018             :   {
    2019             :     void *E;
    2020          29 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2021          29 :     return gen_order(a,ord,E,S);
    2022             :   }
    2023             : }
    2024             : 
    2025             : GEN
    2026       71767 : FpXQ_log(GEN a, GEN g, GEN ord, GEN T, GEN p)
    2027             : {
    2028       71767 :   pari_sp av=avma;
    2029       71767 :   if (lgefint(p)==3)
    2030             :   {
    2031       71723 :     if (uel(p,2) == 2)
    2032             :     {
    2033       40817 :       GEN z = F2xq_log(ZX_to_F2x(a), ZX_to_F2x(g), ord,
    2034             :                                      ZX_to_F2x(get_FpX_mod(T)));
    2035       40817 :       return gerepileuptoleaf(av, z);
    2036             :     }
    2037             :     else
    2038             :     {
    2039       30906 :       ulong pp = to_Flxq(&a, &T, p);
    2040       30906 :       GEN z = Flxq_log(a, ZX_to_Flx(g, pp), ord, T, pp);
    2041       30906 :       return gerepileuptoleaf(av, z);
    2042             :     }
    2043             :   }
    2044             :   else
    2045             :   {
    2046             :     void *E;
    2047          44 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2048          44 :     GEN z = gen_PH_log(a,g,ord,E,S);
    2049          16 :     return gerepileuptoleaf(av, z);
    2050             :   }
    2051             : }
    2052             : 
    2053             : GEN
    2054      245819 : Fq_log(GEN a, GEN g, GEN ord, GEN T, GEN p)
    2055             : {
    2056      245819 :   if (!T) return Fp_log(a,g,ord,p);
    2057       75923 :   if (typ(g) == t_INT)
    2058             :   {
    2059           0 :     if (typ(a) == t_POL)
    2060             :     {
    2061           0 :       if (degpol(a)) return cgetg(1,t_VEC);
    2062           0 :       a = gel(a,2);
    2063             :     }
    2064           0 :     return Fp_log(a,g,ord,p);
    2065             :   }
    2066       75923 :   return typ(a) == t_INT? Fp_FpXQ_log(a,g,ord,T,p): FpXQ_log(a,g,ord,T,p);
    2067             : }
    2068             : 
    2069             : GEN
    2070        8436 : FpXQ_sqrtn(GEN a, GEN n, GEN T, GEN p, GEN *zeta)
    2071             : {
    2072        8436 :   pari_sp av = avma;
    2073             :   GEN z;
    2074        8436 :   if (!signe(a))
    2075             :   {
    2076          70 :     long v=varn(a);
    2077          70 :     if (signe(n) < 0) pari_err_INV("FpXQ_sqrtn",a);
    2078          63 :     if (zeta) *zeta=pol_1(v);
    2079          63 :     return pol_0(v);
    2080             :   }
    2081        8366 :   if (lgefint(p)==3)
    2082             :   {
    2083        6657 :     if (uel(p,2) == 2)
    2084             :     {
    2085         791 :       z = F2xq_sqrtn(ZX_to_F2x(a), n, ZX_to_F2x(get_Flx_mod(T)), zeta);
    2086         791 :       if (!z) return NULL;
    2087         791 :       z = F2x_to_ZX(z);
    2088         791 :       if (!zeta) return gerepileuptoleaf(av, z);
    2089           7 :       *zeta=F2x_to_ZX(*zeta);
    2090             :     } else
    2091             :     {
    2092        5866 :       ulong pp = to_Flxq(&a, &T, p);
    2093        5866 :       z = Flxq_sqrtn(a, n, T, pp, zeta);
    2094        5866 :       if (!z) return NULL;
    2095        3549 :       if (!zeta) return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    2096          70 :       z = Flx_to_ZX(z);
    2097          70 :       *zeta=Flx_to_ZX(*zeta);
    2098             :     }
    2099             :   }
    2100             :   else
    2101             :   {
    2102             :     void *E;
    2103        1709 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2104        1709 :     GEN o = addis(powiu(p,get_FpX_degree(T)),-1);
    2105        1709 :     z = gen_Shanks_sqrtn(a,n,o,zeta,E,S);
    2106        3417 :     if (!z) return NULL;
    2107        1667 :     if (!zeta) return gerepileupto(av, z);
    2108             :   }
    2109          78 :   gerepileall(av, 2, &z,zeta);
    2110          78 :   return z;
    2111             : }
    2112             : 
    2113             : GEN
    2114        7340 : FpXQ_sqrt(GEN a, GEN T, GEN p)
    2115             : {
    2116        7340 :   return FpXQ_sqrtn(a, gen_2, T, p, NULL);
    2117             : }
    2118             : 
    2119             : GEN
    2120        3210 : FpXQ_norm(GEN x, GEN TB, GEN p)
    2121             : {
    2122        3210 :   pari_sp av = avma;
    2123        3210 :   GEN T = get_FpX_mod(TB);
    2124        3210 :   GEN y = FpX_resultant(T, x, p);
    2125        3210 :   GEN L = leading_coeff(T);
    2126        3210 :   if (gequal1(L) || signe(x)==0) return y;
    2127           0 :   return gerepileupto(av, Fp_div(y, Fp_pows(L, degpol(x), p), p));
    2128             : }
    2129             : 
    2130             : GEN
    2131       20785 : FpXQ_trace(GEN x, GEN TB, GEN p)
    2132             : {
    2133       20785 :   pari_sp av = avma;
    2134       20785 :   GEN T = get_FpX_mod(TB);
    2135       20785 :   GEN dT = FpX_deriv(T,p);
    2136       20785 :   long n = degpol(dT);
    2137       20785 :   GEN z = FpXQ_mul(x, dT, TB, p);
    2138       20785 :   if (degpol(z)<n) { avma = av; return gen_0; }
    2139       19609 :   return gerepileuptoint(av, Fp_div(gel(z,2+n), gel(T,3+n),p));
    2140             : }
    2141             : 
    2142             : GEN
    2143           1 : FpXQ_charpoly(GEN x, GEN T, GEN p)
    2144             : {
    2145           1 :   pari_sp ltop=avma;
    2146           1 :   long vT, v = fetch_var();
    2147             :   GEN R;
    2148           1 :   T = leafcopy(get_FpX_mod(T));
    2149           1 :   vT = varn(T); setvarn(T, v);
    2150           1 :   x = leafcopy(x); setvarn(x, v);
    2151           1 :   R = FpX_FpXY_resultant(T, deg1pol_shallow(gen_1,FpX_neg(x,p),vT),p);
    2152           1 :   (void)delete_var(); return gerepileupto(ltop,R);
    2153             : }
    2154             : 
    2155             : /* Computing minimal polynomial :                         */
    2156             : /* cf Shoup 'Efficient Computation of Minimal Polynomials */
    2157             : /*          in Algebraic Extensions of Finite Fields'     */
    2158             : 
    2159             : static GEN
    2160         108 : FpXn_mul(GEN a, GEN b, long n, GEN p)
    2161             : {
    2162         108 :   return FpX_red(RgXn_red_shallow(ZX_mul(a, b), n), p);
    2163             : }
    2164             : 
    2165             : /* Let v a linear form, return the linear form z->v(tau*z)
    2166             :    that is, v*(M_tau) */
    2167             : 
    2168             : static GEN
    2169          30 : FpXQ_transmul_init(GEN tau, GEN T, GEN p)
    2170             : {
    2171             :   GEN bht;
    2172          30 :   GEN h, Tp = get_FpX_red(T, &h);
    2173          30 :   long n = degpol(Tp), vT = varn(Tp);
    2174          30 :   GEN ft = FpX_recipspec(Tp+2, n+1, n+1);
    2175          30 :   GEN bt = FpX_recipspec(tau+2, lgpol(tau), n);
    2176          30 :   setvarn(ft, vT); setvarn(bt, vT);
    2177          30 :   if (h)
    2178           0 :     bht = FpXn_mul(bt, h, n-1, p);
    2179             :   else
    2180             :   {
    2181          30 :     GEN bh = FpX_div(RgX_shift_shallow(tau, n-1), T, p);
    2182          30 :     bht = FpX_recipspec(bh+2, lgpol(bh), n-1);
    2183          30 :     setvarn(bht, vT);
    2184             :   }
    2185          30 :   return mkvec3(bt, bht, ft);
    2186             : }
    2187             : 
    2188             : static GEN
    2189         123 : FpXQ_transmul(GEN tau, GEN a, long n, GEN p)
    2190             : {
    2191         123 :   pari_sp ltop = avma;
    2192             :   GEN t1, t2, t3, vec;
    2193         123 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    2194         123 :   if (signe(a)==0) return pol_0(varn(a));
    2195         123 :   t2 = RgX_shift_shallow(FpX_mul(bt, a, p),1-n);
    2196         123 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    2197         108 :   t1 = RgX_shift_shallow(FpX_mul(ft, a, p),-n);
    2198         108 :   t3 = FpXn_mul(t1, bht, n-1, p);
    2199         108 :   vec = FpX_sub(t2, RgX_shift_shallow(t3, 1), p);
    2200         108 :   return gerepileupto(ltop, vec);
    2201             : }
    2202             : 
    2203             : GEN
    2204        3277 : FpXQ_minpoly(GEN x, GEN T, GEN p)
    2205             : {
    2206        3277 :   pari_sp ltop = avma;
    2207             :   long vT, n;
    2208             :   GEN v_x, g, tau;
    2209        3277 :   if (lgefint(p)==3)
    2210             :   {
    2211        3262 :     ulong pp = to_Flxq(&x, &T, p);
    2212        3262 :     GEN g = Flxq_minpoly(x, T, pp);
    2213        3262 :     return gerepileupto(ltop, Flx_to_ZX(g));
    2214             :   }
    2215          15 :   vT = get_FpX_var(T);
    2216          15 :   n = get_FpX_degree(T);
    2217          15 :   g = pol_1(vT);
    2218          15 :   tau = pol_1(vT);
    2219          15 :   T = FpX_get_red(T, p);
    2220          15 :   x = FpXQ_red(x, T, p);
    2221          15 :   v_x = FpXQ_powers(x, usqrt(2*n), T, p);
    2222          45 :   while(signe(tau) != 0)
    2223             :   {
    2224             :     long i, j, m, k1;
    2225             :     GEN M, v, tr;
    2226             :     GEN g_prime, c;
    2227          15 :     if (degpol(g) == n) { tau = pol_1(vT); g = pol_1(vT); }
    2228          15 :     v = random_FpX(n, vT, p);
    2229          15 :     tr = FpXQ_transmul_init(tau, T, p);
    2230          15 :     v = FpXQ_transmul(tr, v, n, p);
    2231          15 :     m = 2*(n-degpol(g));
    2232          15 :     k1 = usqrt(m);
    2233          15 :     tr = FpXQ_transmul_init(gel(v_x,k1+1), T, p);
    2234          15 :     c = cgetg(m+2,t_POL);
    2235          15 :     c[1] = evalsigne(1)|evalvarn(vT);
    2236         123 :     for (i=0; i<m; i+=k1)
    2237             :     {
    2238         108 :       long mj = minss(m-i, k1);
    2239         674 :       for (j=0; j<mj; j++)
    2240         566 :         gel(c,m+1-(i+j)) = FpX_dotproduct(v, gel(v_x,j+1), p);
    2241         108 :       v = FpXQ_transmul(tr, v, n, p);
    2242             :     }
    2243          15 :     c = FpX_renormalize(c, m+2);
    2244             :     /* now c contains <v,x^i> , i = 0..m-1  */
    2245          15 :     M = FpX_halfgcd(pol_xn(m, vT), c, p);
    2246          15 :     g_prime = gmael(M, 2, 2);
    2247          15 :     if (degpol(g_prime) < 1) continue;
    2248          15 :     g = FpX_mul(g, g_prime, p);
    2249          15 :     tau = FpXQ_mul(tau, FpX_FpXQV_eval(g_prime, v_x, T, p), T, p);
    2250             :   }
    2251          15 :   g = FpX_normalize(g,p);
    2252          15 :   return gerepilecopy(ltop,g);
    2253             : }
    2254             : 
    2255             : GEN
    2256           8 : FpXQ_conjvec(GEN x, GEN T, GEN p)
    2257             : {
    2258           8 :   pari_sp av=avma;
    2259             :   long i;
    2260           8 :   long n = get_FpX_degree(T), v = varn(x);
    2261           8 :   GEN M = FpX_matFrobenius(T, p);
    2262           8 :   GEN z = cgetg(n+1,t_COL);
    2263           8 :   gel(z,1) = RgX_to_RgC(x,n);
    2264           8 :   for (i=2; i<=n; i++) gel(z,i) = FpM_FpC_mul(M,gel(z,i-1),p);
    2265           8 :   gel(z,1) = x;
    2266           8 :   for (i=2; i<=n; i++) gel(z,i) = RgV_to_RgX(gel(z,i),v);
    2267           8 :   return gerepilecopy(av,z);
    2268             : }
    2269             : 
    2270             : /* p prime, p_1 = p-1, q = p^deg T, Lp = cofactors of some prime divisors
    2271             :  * l_p of p-1, Lq = cofactors of some prime divisors l_q of q-1, return a
    2272             :  * g in Fq such that
    2273             :  * - Ng generates all l_p-Sylows of Fp^*
    2274             :  * - g generates all l_q-Sylows of Fq^* */
    2275             : static GEN
    2276        1465 : gener_FpXQ_i(GEN T, GEN p, GEN p_1, GEN Lp, GEN Lq)
    2277             : {
    2278             :   pari_sp av;
    2279        1465 :   long vT = varn(T), f = degpol(T), l = lg(Lq);
    2280        1465 :   GEN F = FpX_Frobenius(T, p);
    2281        1465 :   int p_is_2 = is_pm1(p_1);
    2282        3140 :   for (av = avma;; avma = av)
    2283             :   {
    2284        3140 :     GEN t, g = random_FpX(f, vT, p);
    2285             :     long i;
    2286        3140 :     if (degpol(g) < 1) continue;
    2287        3028 :     if (p_is_2)
    2288         392 :       t = g;
    2289             :     else
    2290             :     {
    2291        2636 :       t = FpX_resultant(T, g, p); /* Ng = g^((q-1)/(p-1)), assuming T monic */
    2292        2636 :       if (kronecker(t, p) == 1) continue;
    2293        1304 :       if (lg(Lp) > 1 && !is_gener_Fp(t, p, p_1, Lp)) continue;
    2294        1298 :       t = FpXQ_pow(g, shifti(p_1,-1), T, p);
    2295             :     }
    2296        2174 :     for (i = 1; i < l; i++)
    2297             :     {
    2298         709 :       GEN a = FpXQ_pow_Frobenius(t, gel(Lq,i), F, T, p);
    2299         709 :       if (!degpol(a) && equalii(gel(a,2), p_1)) break;
    2300             :     }
    2301        3155 :     if (i == l) return g;
    2302        1675 :   }
    2303             : }
    2304             : 
    2305             : GEN
    2306        6841 : gener_FpXQ(GEN T, GEN p, GEN *po)
    2307             : {
    2308        6841 :   long i, j, f = get_FpX_degree(T);
    2309             :   GEN g, Lp, Lq, p_1, q_1, N, o;
    2310        6841 :   pari_sp av = avma;
    2311             : 
    2312        6841 :   p_1 = subiu(p,1);
    2313        6841 :   if (f == 1) {
    2314             :     GEN Lp, fa;
    2315           7 :     o = p_1;
    2316           7 :     fa = Z_factor(o);
    2317           7 :     Lp = gel(fa,1);
    2318           7 :     Lp = vecslice(Lp, 2, lg(Lp)-1); /* remove 2 for efficiency */
    2319             : 
    2320           7 :     g = cgetg(3, t_POL);
    2321           7 :     g[1] = evalsigne(1) | evalvarn(get_FpX_var(T));
    2322           7 :     gel(g,2) = pgener_Fp_local(p, Lp);
    2323           7 :     if (po) *po = mkvec2(o, fa);
    2324           7 :     return g;
    2325             :   }
    2326        6834 :   if (lgefint(p) == 3)
    2327             :   {
    2328        6811 :     ulong pp = to_Flxq(NULL, &T, p);
    2329        6811 :     g = gener_Flxq(T, pp, po);
    2330        6811 :     if (!po) return Flx_to_ZX_inplace(gerepileuptoleaf(av, g));
    2331        6811 :     g = Flx_to_ZX(g);
    2332        6811 :     gerepileall(av, 2, &g, po);
    2333        6811 :     return g;
    2334             :   }
    2335             :   /* p now odd */
    2336          23 :   q_1 = subiu(powiu(p,f), 1);
    2337          23 :   N = diviiexact(q_1, p_1);
    2338          23 :   Lp = odd_prime_divisors(p_1);
    2339          23 :   for (i=lg(Lp)-1; i; i--) gel(Lp,i) = diviiexact(p_1, gel(Lp,i));
    2340          23 :   o = factor_pn_1(p,f);
    2341          23 :   Lq = leafcopy( gel(o, 1) );
    2342         199 :   for (i = j = 1; i < lg(Lq); i++)
    2343             :   {
    2344         176 :     if (remii(p_1, gel(Lq,i)) == gen_0) continue;
    2345         106 :     gel(Lq,j++) = diviiexact(N, gel(Lq,i));
    2346             :   }
    2347          23 :   setlg(Lq, j);
    2348          23 :   g = gener_FpXQ_i(get_FpX_mod(T), p, p_1, Lp, Lq);
    2349          23 :   if (!po) g = gerepilecopy(av, g);
    2350             :   else {
    2351           7 :     *po = mkvec2(q_1, o);
    2352           7 :     gerepileall(av, 2, &g, po);
    2353             :   }
    2354          23 :   return g;
    2355             : }
    2356             : 
    2357             : #if 0 /* generic version: slower */
    2358             : GEN
    2359             : gener_FpXQ2(GEN T, GEN p, GEN *po)
    2360             : {
    2361             :   pari_sp av = avma;
    2362             :   void *E;
    2363             :   long f = get_FpX_degree(T);
    2364             :   GEN g, o = factor_pn_1(p,f);
    2365             :   const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2366             :   g = gen_gener(o,E,S);
    2367             :   if (!po) g = gerepilecopy(av, g);
    2368             :   else {
    2369             :     *po = mkvec2(powiu(p,f), o);
    2370             :     gerepileall(av, 2, &g, po);
    2371             :   }
    2372             :   return g;
    2373             : }
    2374             : #endif
    2375             : 
    2376             : GEN
    2377        1442 : gener_FpXQ_local(GEN T, GEN p, GEN L)
    2378             : {
    2379        1442 :   GEN Lp, Lq, p_1 = subiu(p,1), q_1, N, Q;
    2380        1442 :   long f, i, ip, iq, l = lg(L);
    2381        1442 :   T = get_FpX_mod(T);
    2382        1442 :   f = degpol(T);
    2383        1442 :   q_1 = subiu(powiu(p,f), 1);
    2384        1442 :   N = diviiexact(q_1, p_1);
    2385             : 
    2386        1442 :   Q = is_pm1(p_1)? gen_1: shifti(p_1,-1);
    2387        1442 :   Lp = cgetg(l, t_VEC); ip = 1;
    2388        1442 :   Lq = cgetg(l, t_VEC); iq = 1;
    2389        2114 :   for (i=1; i < l; i++)
    2390             :   {
    2391         672 :     GEN a, b, ell = gel(L,i);
    2392         672 :     if (absequaliu(ell,2)) continue;
    2393         392 :     a = dvmdii(Q, ell, &b);
    2394         392 :     if (b == gen_0)
    2395          21 :       gel(Lp,ip++) = a;
    2396             :     else
    2397         371 :       gel(Lq,iq++) = diviiexact(N,ell);
    2398             :   }
    2399        1442 :   setlg(Lp, ip);
    2400        1442 :   setlg(Lq, iq);
    2401        1442 :   return gener_FpXQ_i(T, p, p_1, Lp, Lq);
    2402             : }

Generated by: LCOV version 1.11