Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - FpXX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23328-a3379c31c) Lines: 865 1024 84.5 %
Date: 2018-12-09 05:41:42 Functions: 102 116 87.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2012  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 FpX */
      18             : 
      19             : /*******************************************************************/
      20             : /*                                                                 */
      21             : /*                             FpXX                                */
      22             : /*                                                                 */
      23             : /*******************************************************************/
      24             : /*Polynomials whose coefficients are either polynomials or integers*/
      25             : 
      26             : static ulong
      27      216063 : to_FlxqX(GEN P, GEN Q, GEN T, GEN p, GEN *pt_P, GEN *pt_Q, GEN *pt_T)
      28             : {
      29      216063 :   ulong pp = uel(p,2);
      30      216063 :   long v = get_FpX_var(T);
      31      216063 :   *pt_P = ZXX_to_FlxX(P, pp, v);
      32      216063 :   if (pt_Q) *pt_Q = ZXX_to_FlxX(Q, pp, v);
      33      216063 :   *pt_T = ZXT_to_FlxT(T, pp);
      34      216063 :   return pp;
      35             : }
      36             : 
      37             : static GEN
      38           0 : ZXX_copy(GEN a) { return gcopy(a); }
      39             : 
      40             : GEN
      41       32690 : FpXX_red(GEN z, GEN p)
      42             : {
      43             :   GEN res;
      44       32690 :   long i, l = lg(z);
      45       32690 :   res = cgetg(l,t_POL); res[1] = z[1];
      46      212667 :   for (i=2; i<l; i++)
      47             :   {
      48      179977 :     GEN zi = gel(z,i), c;
      49      179977 :     if (typ(zi)==t_INT)
      50        1799 :       c = modii(zi,p);
      51             :     else
      52             :     {
      53      178178 :       pari_sp av = avma;
      54      178178 :       c = FpX_red(zi,p);
      55      178178 :       switch(lg(c)) {
      56          14 :         case 2: set_avma(av); c = gen_0; break;
      57       16198 :         case 3: c = gerepilecopy(av, gel(c,2)); break;
      58             :       }
      59             :     }
      60      179977 :     gel(res,i) = c;
      61             :   }
      62       32690 :   return FpXX_renormalize(res,lg(res));
      63             : }
      64             : GEN
      65      397527 : FpXX_add(GEN x, GEN y, GEN p)
      66             : {
      67             :   long i,lz;
      68             :   GEN z;
      69      397527 :   long lx=lg(x);
      70      397527 :   long ly=lg(y);
      71      397527 :   if (ly>lx) swapspec(x,y, lx,ly);
      72      397527 :   lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
      73      397527 :   for (i=2; i<ly; i++) gel(z,i) = Fq_add(gel(x,i), gel(y,i), NULL, p);
      74      397527 :   for (   ; i<lx; i++) gel(z,i) = gcopy(gel(x,i));
      75      397527 :   return FpXX_renormalize(z, lz);
      76             : }
      77             : GEN
      78       13294 : FpXX_sub(GEN x, GEN y, GEN p)
      79             : {
      80             :   long i,lz;
      81             :   GEN z;
      82       13294 :   long lx=lg(x);
      83       13294 :   long ly=lg(y);
      84       13294 :   if (ly <= lx)
      85             :   {
      86       11579 :     lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
      87       11579 :     for (i=2; i<ly; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
      88       11579 :     for (   ; i<lx; i++) gel(z,i) = gcopy(gel(x,i));
      89             :   }
      90             :   else
      91             :   {
      92        1715 :     lz = ly; z = cgetg(lz, t_POL); z[1]=x[1];
      93        1715 :     for (i=2; i<lx; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
      94        1715 :     for (   ; i<ly; i++) gel(z,i) = Fq_neg(gel(y,i), NULL, p);
      95             :   }
      96       13294 :   return FpXX_renormalize(z, lz);
      97             : }
      98             : 
      99             : static GEN
     100      117049 : FpXX_subspec(GEN x, GEN y, GEN p, long nx, long ny)
     101             : {
     102             :   long i,lz;
     103             :   GEN z;
     104      117049 :   if (ny <= nx)
     105             :   {
     106      117049 :     lz = nx+2; z = cgetg(lz, t_POL)+2;
     107      117049 :     for (i=0; i<ny; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
     108      117049 :     for (   ; i<nx; i++) gel(z,i) = gcopy(gel(x,i));
     109             :   }
     110             :   else
     111             :   {
     112           0 :     lz = ny+2; z = cgetg(lz, t_POL)+2;
     113           0 :     for (i=0; i<nx; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
     114           0 :     for (   ; i<ny; i++) gel(z,i) = Fq_neg(gel(y,i), NULL, p);
     115             :   }
     116      117049 :   return FpXX_renormalize(z-2, lz);
     117             : }
     118             : 
     119             : GEN
     120         471 : FpXX_neg(GEN x, GEN p)
     121             : {
     122         471 :   long i, lx = lg(x);
     123         471 :   GEN y = cgetg(lx,t_POL);
     124         471 :   y[1] = x[1];
     125         471 :   for(i=2; i<lx; i++) gel(y,i) = Fq_neg(gel(x,i), NULL, p);
     126         471 :   return FpXX_renormalize(y, lx);
     127             : }
     128             : 
     129             : GEN
     130       64332 : FpXX_Fp_mul(GEN P, GEN u, GEN p)
     131             : {
     132             :   long i, lP;
     133       64332 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     134      474857 :   for(i=2; i<lP; i++)
     135             :   {
     136      410525 :     GEN x = gel(P,i);
     137      410525 :     gel(res,i) = typ(x)==t_INT? Fp_mul(x,u,p): FpX_Fp_mul(x,u,p);
     138             :   }
     139       64332 :   return FpXX_renormalize(res,lP);
     140             : }
     141             : 
     142             : GEN
     143       50368 : FpXX_mulu(GEN P, ulong u, GEN p)
     144             : {
     145             :   long i, lP;
     146       50368 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     147      269214 :   for(i=2; i<lP; i++)
     148             :   {
     149      218846 :     GEN x = gel(P,i);
     150      218846 :     gel(res,i) = typ(x)==t_INT? Fp_mulu(x,u,p): FpX_mulu(x,u,p);
     151             :   }
     152       50368 :   return FpXX_renormalize(res,lP);
     153             : }
     154             : 
     155             : GEN
     156           0 : FpXX_halve(GEN P, GEN p)
     157             : {
     158             :   long i, lP;
     159           0 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     160           0 :   for(i=2; i<lP; i++)
     161             :   {
     162           0 :     GEN x = gel(P,i);
     163           0 :     gel(res,i) = typ(x)==t_INT? Fp_halve(x,p): FpX_halve(x,p);
     164             :   }
     165           0 :   return FpXX_renormalize(res,lP);
     166             : }
     167             : 
     168             : GEN
     169        9537 : FpXX_deriv(GEN P, GEN p)
     170             : {
     171        9537 :   long i, l = lg(P)-1;
     172             :   GEN res;
     173             : 
     174        9537 :   if (l < 3) return pol_0(varn(P));
     175        9320 :   res = cgetg(l, t_POL);
     176        9320 :   res[1] = P[1];
     177       50454 :   for (i=2; i<l ; i++)
     178             :   {
     179       41134 :     GEN x = gel(P,i+1);
     180       41134 :     gel(res,i) = typ(x)==t_INT? Fp_mulu(x,i-1,p): FpX_mulu(x,i-1,p);
     181             :   }
     182        9320 :   return FpXX_renormalize(res, l);
     183             : }
     184             : 
     185             : GEN
     186           0 : FpXX_integ(GEN P, GEN p)
     187             : {
     188           0 :   long i, l = lg(P);
     189             :   GEN res;
     190             : 
     191           0 :   if (l == 2) return pol_0(varn(P));
     192           0 :   res = cgetg(l+1, t_POL);
     193           0 :   res[1] = P[1];
     194           0 :   gel(res,2) = gen_0;
     195           0 :   for (i=3; i<=l ; i++)
     196             :   {
     197           0 :     GEN x = gel(P,i-1);
     198           0 :     if (signe(x))
     199             :     {
     200           0 :       GEN i1 = Fp_inv(utoi(i-2), p);
     201           0 :       gel(res,i) = typ(x)==t_INT? Fp_mul(x,i1,p): FpX_Fp_mul(x,i1,p);
     202             :     } else
     203           0 :       gel(res,i) = gen_0;
     204             :   }
     205           0 :   return FpXX_renormalize(res, l+1);
     206             : }
     207             : 
     208             : /*******************************************************************/
     209             : /*                                                                 */
     210             : /*                             (Fp[X]/(Q))[Y]                      */
     211             : /*                                                                 */
     212             : /*******************************************************************/
     213             : 
     214             : static GEN
     215      393125 : get_FpXQX_red(GEN T, GEN *B)
     216             : {
     217      393125 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
     218       60120 :   *B = gel(T,1); return gel(T,2);
     219             : }
     220             : 
     221             : GEN
     222          16 : random_FpXQX(long d1, long v, GEN T, GEN p)
     223             : {
     224          16 :   long dT = get_FpX_degree(T), vT = get_FpX_var(T);
     225          16 :   long i, d = d1+2;
     226          16 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     227          16 :   for (i=2; i<d; i++) gel(y,i) = random_FpX(dT, vT, p);
     228          16 :   return FpXQX_renormalize(y,d);
     229             : }
     230             : 
     231             : /*Not stack clean*/
     232             : GEN
     233      671810 : Kronecker_to_FpXQX(GEN Z, GEN T, GEN p)
     234             : {
     235      671810 :   long i,j,lx,l, N = (get_FpX_degree(T)<<1) + 1;
     236      671810 :   GEN x, t = cgetg(N,t_POL), z = FpX_red(Z, p);
     237      671810 :   t[1] = evalvarn(get_FpX_var(T));
     238      671810 :   l = lg(z); lx = (l-2) / (N-2);
     239      671810 :   x = cgetg(lx+3,t_POL);
     240      671810 :   x[1] = z[1];
     241    14472104 :   for (i=2; i<lx+2; i++)
     242             :   {
     243    13800294 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     244    13800294 :     z += (N-2);
     245    13800294 :     gel(x,i) = FpX_rem(FpX_renormalize(t,N), T,p);
     246             :   }
     247      671810 :   N = (l-2) % (N-2) + 2;
     248      671810 :   for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     249      671810 :   gel(x,i) = FpX_rem(FpX_renormalize(t,N), T,p);
     250      671810 :   return FpXQX_renormalize(x, i+1);
     251             : }
     252             : 
     253             : /* shallow, n = deg(T) */
     254             : GEN
     255      100807 : Kronecker_to_ZXX(GEN z, long n, long v)
     256             : {
     257      100807 :   long i,j,lx,l, N = (n<<1)+1;
     258             :   GEN x, t;
     259      100807 :   l = lg(z); lx = (l-2) / (N-2);
     260      100807 :   x = cgetg(lx+3,t_POL);
     261      100807 :   x[1] = z[1];
     262     1711927 :   for (i=2; i<lx+2; i++)
     263             :   {
     264     1611120 :     t = cgetg(N,t_POL); t[1] = evalvarn(v);
     265     1611120 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     266     1611120 :     z += (N-2);
     267     1611120 :     gel(x,i) = ZX_renormalize(t,N);
     268             :   }
     269      100807 :   N = (l-2) % (N-2) + 2;
     270      100807 :   t = cgetg(N,t_POL); t[1] = evalvarn(v);
     271      100807 :   for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     272      100807 :   gel(x,i) = ZX_renormalize(t,N);
     273      100807 :   return ZXX_renormalize(x, i+1);
     274             : }
     275             : /* shallow */
     276             : GEN
     277      159432 : ZXX_mul_Kronecker(GEN x, GEN y, long n)
     278      159432 : { return ZX_mul(ZXX_to_Kronecker(x,n), ZXX_to_Kronecker(y,n)); }
     279             : 
     280             : GEN
     281        2191 : ZXX_sqr_Kronecker(GEN x, long n)
     282        2191 : { return ZX_sqr(ZXX_to_Kronecker(x,n)); }
     283             : 
     284             : GEN
     285      228722 : FpXQX_red(GEN z, GEN T, GEN p)
     286             : {
     287      228722 :   long i, l = lg(z);
     288      228722 :   GEN res = cgetg(l,t_POL); res[1] = z[1];
     289     4986938 :   for(i=2;i<l;i++)
     290     4758216 :     if (typ(gel(z,i)) == t_INT)
     291      116316 :       gel(res,i) = modii(gel(z,i),p);
     292             :     else
     293     4641900 :       gel(res,i) = FpXQ_red(gel(z,i),T,p);
     294      228722 :   return FpXQX_renormalize(res,l);
     295             : }
     296             : 
     297             : /* z in Z[X], return z * Mod(1,p), normalized*/
     298             : GEN
     299          91 : FpXQX_to_mod(GEN z, GEN T, GEN p)
     300             : {
     301          91 :   long i,l = lg(z);
     302          91 :   GEN x = cgetg(l, t_POL);
     303          91 :   x[1] = z[1];
     304          91 :   if (l == 2) return x;
     305          91 :   T = FpX_to_mod(T, p);
     306        1204 :   for (i=2; i<l; i++)
     307             :   {
     308        1113 :     GEN zi = gel(z,i);
     309        1113 :     gel(x,i) = typ(zi) == t_POL? mkpolmod(FpX_to_mod(zi, p), T): icopy(zi);
     310             :   }
     311          91 :   return normalizepol_lg(x,l);
     312             : }
     313             : 
     314             : static int
     315     1228273 : ZXX_is_ZX_spec(GEN a,long na)
     316             : {
     317             :   long i;
     318     1334056 :   for(i=0;i<na;i++)
     319     1319187 :     if(typ(gel(a,i))!=t_INT) return 0;
     320       14869 :   return 1;
     321             : }
     322             : 
     323             : static int
     324      141065 : ZXX_is_ZX(GEN a) { return ZXX_is_ZX_spec(a+2,lgpol(a)); }
     325             : 
     326             : static GEN
     327       45794 : FpXX_FpX_mulspec(GEN P, GEN U, GEN p, long v, long lU)
     328             : {
     329       45794 :   long i, lP =lg(P);
     330             :   GEN res;
     331       45794 :   res = cgetg(lP, t_POL); res[1] = P[1];
     332     1788849 :   for(i=2; i<lP; i++)
     333             :   {
     334     1743055 :     GEN Pi = gel(P,i);
     335     3479522 :     gel(res,i) = typ(Pi)==t_INT? FpX_Fp_mulspec(U, Pi, p, lU):
     336     1736467 :                                  FpX_mulspec(U, Pi+2, p, lU, lgpol(Pi));
     337     1743055 :     setvarn(gel(res,i),v);
     338             :   }
     339       45794 :   return FpXQX_renormalize(res,lP);
     340             : }
     341             : 
     342             : GEN
     343       36650 : FpXX_FpX_mul(GEN P, GEN U, GEN p)
     344       36650 : { return FpXX_FpX_mulspec(P,U+2,p,varn(U),lgpol(U)); }
     345             : 
     346             : static GEN
     347        9144 : FpXY_FpY_mulspec(GEN x, GEN y, GEN T, GEN p, long lx, long ly)
     348             : {
     349        9144 :   pari_sp av = avma;
     350        9144 :   long v = fetch_var();
     351        9144 :   GEN z = RgXY_swapspec(x,get_FpX_degree(T)-1,v,lx);
     352        9144 :   z = FpXX_FpX_mulspec(z,y,p,v,ly);
     353        9144 :   z = RgXY_swapspec(z+2,lx+ly+3,get_FpX_var(T),lgpol(z));
     354        9144 :   (void)delete_var(); return gerepilecopy(av,z);
     355             : }
     356             : 
     357             : static GEN
     358      543604 : FpXQX_mulspec(GEN x, GEN y, GEN T, GEN p, long lx, long ly)
     359             : {
     360      543604 :   pari_sp av = avma;
     361             :   GEN z, kx, ky;
     362             :   long n;
     363      543604 :   if (ZXX_is_ZX_spec(y,ly))
     364             :   {
     365        6688 :     if (ZXX_is_ZX_spec(x,lx))
     366        2010 :       return FpX_mulspec(x,y,p,lx,ly);
     367             :     else
     368        4678 :       return FpXY_FpY_mulspec(x,y,T,p,lx,ly);
     369      536916 :   } else if (ZXX_is_ZX_spec(x,lx))
     370        4466 :       return FpXY_FpY_mulspec(y,x,T,p,ly,lx);
     371      532450 :   n = get_FpX_degree(T);
     372      532450 :   kx = ZXX_to_Kronecker_spec(x, lx, n);
     373      532450 :   ky = ZXX_to_Kronecker_spec(y, ly, n);
     374      532450 :   z = Kronecker_to_FpXQX(ZX_mul(ky,kx), T, p);
     375      532450 :   return gerepileupto(av, z);
     376             : }
     377             : 
     378             : GEN
     379      306223 : FpXQX_mul(GEN x, GEN y, GEN T, GEN p)
     380             : {
     381      306223 :   GEN z = FpXQX_mulspec(x+2,y+2,T,p,lgpol(x),lgpol(y));
     382      306223 :   setvarn(z,varn(x)); return z;
     383             : }
     384             : 
     385             : GEN
     386      141033 : FpXQX_sqr(GEN x, GEN T, GEN p)
     387             : {
     388      141033 :   pari_sp av = avma;
     389             :   GEN z, kx;
     390      141033 :   if (ZXX_is_ZX(x)) return ZX_sqr(x);
     391      139328 :   kx= ZXX_to_Kronecker(x, get_FpX_degree(T));
     392      139328 :   z = Kronecker_to_FpXQX(ZX_sqr(kx), T, p);
     393      139328 :   return gerepileupto(av, z);
     394             : }
     395             : 
     396             : GEN
     397      240234 : FpXQX_FpXQ_mul(GEN P, GEN U, GEN T, GEN p)
     398             : {
     399             :   long i, lP;
     400             :   GEN res;
     401      240234 :   res = cgetg_copy(P, &lP); res[1] = P[1];
     402     1113248 :   for(i=2; i<lP; i++)
     403     1652358 :     gel(res,i) = typ(gel(P,i))==t_INT? FpX_Fp_mul(U, gel(P,i), p):
     404      779344 :                                        FpXQ_mul(U, gel(P,i), T,p);
     405      240234 :   return FpXQX_renormalize(res,lP);
     406             : }
     407             : 
     408             : /* x and y in Z[Y][X]. Assume T irreducible mod p */
     409             : static GEN
     410      125846 : FpXQX_divrem_basecase(GEN x, GEN y, GEN T, GEN p, GEN *pr)
     411             : {
     412             :   long vx, dx, dy, dy1, dz, i, j, sx, lr;
     413             :   pari_sp av0, av, tetpil;
     414             :   GEN z,p1,rem,lead;
     415             : 
     416      125846 :   if (!signe(y)) pari_err_INV("FpX_divrem",y);
     417      125846 :   vx=varn(x); dy=degpol(y); dx=degpol(x);
     418      125846 :   if (dx < dy)
     419             :   {
     420         143 :     if (pr)
     421             :     {
     422         135 :       av0 = avma; x = FpXQX_red(x, T, p);
     423         135 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: pol_0(vx); }
     424         135 :       if (pr == ONLY_REM) return x;
     425         135 :       *pr = x;
     426             :     }
     427         143 :     return pol_0(vx);
     428             :   }
     429      125703 :   lead = leading_coeff(y);
     430      125703 :   if (!dy) /* y is constant */
     431             :   {
     432         829 :     if (pr && pr != ONLY_DIVIDES)
     433             :     {
     434         571 :       if (pr == ONLY_REM) return pol_0(vx);
     435           4 :       *pr = pol_0(vx);
     436             :     }
     437         262 :     if (gequal1(lead)) return FpXQX_red(x,T,p);
     438         246 :     av0 = avma; x = FqX_Fq_mul(x, Fq_inv(lead, T,p), T,p);
     439         246 :     return gerepileupto(av0,x);
     440             :   }
     441      124874 :   av0 = avma; dz = dx-dy;
     442      124874 :   lead = gequal1(lead)? NULL: gclone(Fq_inv(lead,T,p));
     443      124874 :   set_avma(av0);
     444      124874 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
     445      124874 :   x += 2; y += 2; z += 2;
     446      124874 :   for (dy1=dy-1; dy1>=0 && !signe(gel(y, dy1)); dy1--);
     447             : 
     448      124874 :   p1 = gel(x,dx); av = avma;
     449      124874 :   gel(z,dz) = lead? gerepileupto(av, Fq_mul(p1,lead, T, p)): gcopy(p1);
     450      400271 :   for (i=dx-1; i>=dy; i--)
     451             :   {
     452      275397 :     av=avma; p1=gel(x,i);
     453      924064 :     for (j=i-dy1; j<=i && j<=dz; j++)
     454      648667 :       p1 = Fq_sub(p1, Fq_mul(gel(z,j),gel(y,i-j),NULL,p),NULL,p);
     455      275397 :     if (lead) p1 = Fq_mul(p1, lead, NULL,p);
     456      275397 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Fq_red(p1,T,p));
     457             :   }
     458      124874 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
     459             : 
     460      121773 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     461      135918 :   for (sx=0; ; i--)
     462             :   {
     463      150063 :     p1 = gel(x,i);
     464      532008 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     465      396090 :       p1 = Fq_sub(p1, Fq_mul(gel(z,j),gel(y,i-j),NULL,p),NULL,p);
     466      135918 :     tetpil=avma; p1 = Fq_red(p1, T, p); if (signe(p1)) { sx = 1; break; }
     467       15933 :     if (!i) break;
     468       14145 :     set_avma(av);
     469             :   }
     470      121773 :   if (pr == ONLY_DIVIDES)
     471             :   {
     472           0 :     if (lead) gunclone(lead);
     473           0 :     if (sx) return gc_NULL(av0);
     474           0 :     set_avma((pari_sp)rem); return z-2;
     475             :   }
     476      121773 :   lr=i+3; rem -= lr;
     477      121773 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     478      121773 :   rem[1] = z[-1];
     479      121773 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
     480      121773 :   rem += 2; gel(rem,i) = p1;
     481      825971 :   for (i--; i>=0; i--)
     482             :   {
     483      704198 :     av=avma; p1 = gel(x,i);
     484     2285285 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     485     1581087 :       p1 = Fq_sub(p1, Fq_mul(gel(z,j),gel(y,i-j), NULL,p), NULL,p);
     486      704198 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Fq_red(p1, T, p));
     487             :   }
     488      121773 :   rem -= 2;
     489      121773 :   if (lead) gunclone(lead);
     490      121773 :   if (!sx) (void)FpXQX_renormalize(rem, lr);
     491      121773 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     492        7176 :   *pr = rem; return z-2;
     493             : }
     494             : 
     495             : static GEN
     496          76 : FpXQX_halfgcd_basecase(GEN a, GEN b, GEN T, GEN p)
     497             : {
     498          76 :   pari_sp av=avma;
     499             :   GEN u,u1,v,v1;
     500          76 :   long vx = varn(a);
     501          76 :   long n = lgpol(a)>>1;
     502          76 :   u1 = v = pol_0(vx);
     503          76 :   u = v1 = pol_1(vx);
     504         903 :   while (lgpol(b)>n)
     505             :   {
     506         751 :     GEN r, q = FpXQX_divrem(a,b, T, p, &r);
     507         751 :     a = b; b = r; swap(u,u1); swap(v,v1);
     508         751 :     u1 = FpXX_sub(u1, FpXQX_mul(u, q, T, p), p);
     509         751 :     v1 = FpXX_sub(v1, FpXQX_mul(v, q ,T, p), p);
     510         751 :     if (gc_needed(av,2))
     511             :     {
     512           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQX_halfgcd (d = %ld)",degpol(b));
     513           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
     514             :     }
     515             :   }
     516          76 :   return gerepilecopy(av, mkmat2(mkcol2(u,u1), mkcol2(v,v1)));
     517             : }
     518             : static GEN
     519         136 : FpXQX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN T, GEN p)
     520             : {
     521         136 :   return FpXX_add(FpXQX_mul(u, x, T, p),FpXQX_mul(v, y, T, p), p);
     522             : }
     523             : 
     524             : static GEN
     525          68 : FpXQXM_FpXQX_mul2(GEN M, GEN x, GEN y, GEN T, GEN p)
     526             : {
     527          68 :   GEN res = cgetg(3, t_COL);
     528          68 :   gel(res, 1) = FpXQX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, T, p);
     529          68 :   gel(res, 2) = FpXQX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, T, p);
     530          68 :   return res;
     531             : }
     532             : 
     533             : static GEN
     534          56 : FpXQXM_mul2(GEN A, GEN B, GEN T, GEN p)
     535             : {
     536          56 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
     537          56 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
     538          56 :   GEN M1 = FpXQX_mul(FpXX_add(A11,A22, p), FpXX_add(B11,B22, p), T, p);
     539          56 :   GEN M2 = FpXQX_mul(FpXX_add(A21,A22, p), B11, T, p);
     540          56 :   GEN M3 = FpXQX_mul(A11, FpXX_sub(B12,B22, p), T, p);
     541          56 :   GEN M4 = FpXQX_mul(A22, FpXX_sub(B21,B11, p), T, p);
     542          56 :   GEN M5 = FpXQX_mul(FpXX_add(A11,A12, p), B22, T, p);
     543          56 :   GEN M6 = FpXQX_mul(FpXX_sub(A21,A11, p), FpXX_add(B11,B12, p), T, p);
     544          56 :   GEN M7 = FpXQX_mul(FpXX_sub(A12,A22, p), FpXX_add(B21,B22, p), T, p);
     545          56 :   GEN T1 = FpXX_add(M1,M4, p), T2 = FpXX_sub(M7,M5, p);
     546          56 :   GEN T3 = FpXX_sub(M1,M2, p), T4 = FpXX_add(M3,M6, p);
     547          56 :   retmkmat2(mkcol2(FpXX_add(T1,T2, p), FpXX_add(M2,M4, p)),
     548             :             mkcol2(FpXX_add(M3,M5, p), FpXX_add(T3,T4, p)));
     549             : }
     550             : /* Return [0,1;1,-q]*M */
     551             : static GEN
     552          56 : FpXQX_FpXQXM_qmul(GEN q, GEN M, GEN T, GEN p)
     553             : {
     554          56 :   GEN u, v, res = cgetg(3, t_MAT);
     555          56 :   u = FpXX_sub(gcoeff(M,1,1), FpXQX_mul(gcoeff(M,2,1), q, T, p), p);
     556          56 :   gel(res,1) = mkcol2(gcoeff(M,2,1), u);
     557          56 :   v = FpXX_sub(gcoeff(M,1,2), FpXQX_mul(gcoeff(M,2,2), q, T, p), p);
     558          56 :   gel(res,2) = mkcol2(gcoeff(M,2,2), v);
     559          56 :   return res;
     560             : }
     561             : 
     562             : static GEN
     563           0 : matid2_FpXQXM(long v)
     564             : {
     565           0 :   retmkmat2(mkcol2(pol_1(v),pol_0(v)),
     566             :             mkcol2(pol_0(v),pol_1(v)));
     567             : }
     568             : 
     569             : static GEN
     570          56 : FpXQX_halfgcd_split(GEN x, GEN y, GEN T, GEN p)
     571             : {
     572          56 :   pari_sp av=avma;
     573             :   GEN R, S, V;
     574             :   GEN y1, r, q;
     575          56 :   long l = lgpol(x), n = l>>1, k;
     576          56 :   if (lgpol(y)<=n) return matid2_FpXQXM(varn(x));
     577          56 :   R = FpXQX_halfgcd(RgX_shift_shallow(x,-n),RgX_shift_shallow(y,-n), T, p);
     578          56 :   V = FpXQXM_FpXQX_mul2(R,x,y, T, p); y1 = gel(V,2);
     579          56 :   if (lgpol(y1)<=n) return gerepilecopy(av, R);
     580          56 :   q = FpXQX_divrem(gel(V,1), y1, T, p, &r);
     581          56 :   k = 2*n-degpol(y1);
     582          56 :   S = FpXQX_halfgcd(RgX_shift_shallow(y1,-k), RgX_shift_shallow(r,-k), T, p);
     583          56 :   return gerepileupto(av, FpXQXM_mul2(S,FpXQX_FpXQXM_qmul(q,R, T, p), T, p));
     584             : }
     585             : 
     586             : /* Return M in GL_2(Fp[X]) such that:
     587             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
     588             : */
     589             : 
     590             : static GEN
     591         132 : FpXQX_halfgcd_i(GEN x, GEN y, GEN T, GEN p)
     592             : {
     593         132 :   if (lg(x)<=FpXQX_HALFGCD_LIMIT) return FpXQX_halfgcd_basecase(x, y, T, p);
     594          56 :   return FpXQX_halfgcd_split(x, y, T, p);
     595             : }
     596             : 
     597             : GEN
     598         132 : FpXQX_halfgcd(GEN x, GEN y, GEN T, GEN p)
     599             : {
     600         132 :   pari_sp av = avma;
     601             :   GEN M,q,r;
     602         132 :   if (lgefint(p)==3)
     603             :   {
     604           0 :     ulong pp = to_FlxqX(x, y, T, p, &x, &y, &T);
     605           0 :     M = FlxXM_to_ZXXM(FlxqX_halfgcd(x, y, T, pp));
     606             :   }
     607             :   else
     608             :   {
     609         132 :     if (!signe(x))
     610             :     {
     611           0 :       long v = varn(x);
     612           0 :       retmkmat2(mkcol2(pol_0(v),pol_1(v)),
     613             :                 mkcol2(pol_1(v),pol_0(v)));
     614             :     }
     615         132 :     if (degpol(y)<degpol(x)) return FpXQX_halfgcd_i(x, y, T, p);
     616           8 :     q = FpXQX_divrem(y, x, T, p, &r);
     617           8 :     M = FpXQX_halfgcd_i(x, r, T, p);
     618           8 :     gcoeff(M,1,1) = FpXX_sub(gcoeff(M,1,1), FpXQX_mul(q, gcoeff(M,1,2), T, p), p);
     619           8 :     gcoeff(M,2,1) = FpXX_sub(gcoeff(M,2,1), FpXQX_mul(q, gcoeff(M,2,2), T, p), p);
     620             :   }
     621           8 :   return gerepilecopy(av, M);
     622             : }
     623             : 
     624             : static GEN
     625        3291 : FpXQX_gcd_basecase(GEN a, GEN b, GEN T, GEN p)
     626             : {
     627        3291 :   pari_sp av = avma, av0=avma;
     628       22786 :   while (signe(b))
     629             :   {
     630             :     GEN c;
     631       16204 :     if (gc_needed(av0,2))
     632             :     {
     633           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQX_gcd (d = %ld)",degpol(b));
     634           0 :       gerepileall(av0,2, &a,&b);
     635             :     }
     636       16204 :     av = avma; c = FpXQX_rem(a, b, T, p); a=b; b=c;
     637             :   }
     638        3291 :   set_avma(av); return a;
     639             : }
     640             : 
     641             : GEN
     642       12892 : FpXQX_gcd(GEN x, GEN y, GEN T, GEN p)
     643             : {
     644       12892 :   pari_sp av = avma;
     645       12892 :   if (lgefint(p) == 3)
     646             :   {
     647             :     GEN Pl, Ql, Tl, U;
     648        9524 :     ulong pp = to_FlxqX(x, y, T, p, &Pl, &Ql, &Tl);
     649        9524 :     U  = FlxqX_gcd(Pl, Ql, Tl, pp);
     650        9524 :     return gerepileupto(av, FlxX_to_ZXX(U));
     651             :   }
     652        3368 :   x = FpXQX_red(x, T, p);
     653        3368 :   y = FpXQX_red(y, T, p);
     654        3368 :   if (!signe(x)) return gerepileupto(av, y);
     655        6594 :   while (lg(y)>FpXQX_GCD_LIMIT)
     656             :   {
     657             :     GEN c;
     658          12 :     if (lgpol(y)<=(lgpol(x)>>1))
     659             :     {
     660           0 :       GEN r = FpXQX_rem(x, y, T, p);
     661           0 :       x = y; y = r;
     662             :     }
     663          12 :     c = FpXQXM_FpXQX_mul2(FpXQX_halfgcd(x,y, T, p), x, y, T, p);
     664          12 :     x = gel(c,1); y = gel(c,2);
     665          12 :     gerepileall(av,2,&x,&y);
     666             :   }
     667        3291 :   return gerepileupto(av, FpXQX_gcd_basecase(x, y, T, p));
     668             : }
     669             : 
     670             : static GEN
     671           0 : FpXQX_extgcd_basecase(GEN a, GEN b, GEN T, GEN p, GEN *ptu, GEN *ptv)
     672             : {
     673           0 :   pari_sp av=avma;
     674             :   GEN u,v,d,d1,v1;
     675           0 :   long vx = varn(a);
     676           0 :   d = a; d1 = b;
     677           0 :   v = pol_0(vx); v1 = pol_1(vx);
     678           0 :   while (signe(d1))
     679             :   {
     680           0 :     GEN r, q = FpXQX_divrem(d, d1, T, p, &r);
     681           0 :     v = FpXX_sub(v,FpXQX_mul(q,v1,T, p),p);
     682           0 :     u=v; v=v1; v1=u;
     683           0 :     u=r; d=d1; d1=u;
     684           0 :     if (gc_needed(av,2))
     685             :     {
     686           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQX_extgcd (d = %ld)",degpol(d));
     687           0 :       gerepileall(av,5, &d,&d1,&u,&v,&v1);
     688             :     }
     689             :   }
     690           0 :   if (ptu) *ptu = FpXQX_div(FpXX_sub(d,FpXQX_mul(b,v, T, p), p), a, T, p);
     691           0 :   *ptv = v; return d;
     692             : }
     693             : 
     694             : static GEN
     695           0 : FpXQX_extgcd_halfgcd(GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv)
     696             : {
     697           0 :   pari_sp av=avma;
     698           0 :   GEN u,v,R = matid2_FpXQXM(varn(x));
     699           0 :   while (lg(y)>FpXQX_EXTGCD_LIMIT)
     700             :   {
     701             :     GEN M, c;
     702           0 :     if (lgpol(y)<=(lgpol(x)>>1))
     703             :     {
     704           0 :       GEN r, q = FpXQX_divrem(x, y, T, p, &r);
     705           0 :       x = y; y = r;
     706           0 :       R = FpXQX_FpXQXM_qmul(q, R, T, p);
     707             :     }
     708           0 :     M = FpXQX_halfgcd(x,y, T, p);
     709           0 :     c = FpXQXM_FpXQX_mul2(M, x,y, T, p);
     710           0 :     R = FpXQXM_mul2(M, R, T, p);
     711           0 :     x = gel(c,1); y = gel(c,2);
     712           0 :     gerepileall(av,3,&x,&y,&R);
     713             :   }
     714           0 :   y = FpXQX_extgcd_basecase(x,y, T, p, &u,&v);
     715           0 :   if (ptu) *ptu = FpXQX_addmulmul(u,v,gcoeff(R,1,1),gcoeff(R,2,1), T, p);
     716           0 :   *ptv = FpXQX_addmulmul(u,v,gcoeff(R,1,2),gcoeff(R,2,2), T, p);
     717           0 :   return y;
     718             : }
     719             : 
     720             : /* x and y in Z[Y][X], return lift(gcd(x mod T,p, y mod T,p)). Set u and v st
     721             :  * ux + vy = gcd (mod T,p) */
     722             : GEN
     723        6125 : FpXQX_extgcd(GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv)
     724             : {
     725             :   GEN d;
     726        6125 :   pari_sp ltop=avma;
     727        6125 :   if (lgefint(p) == 3)
     728             :   {
     729             :     GEN Pl, Ql, Tl, Dl;
     730        6125 :     ulong pp = to_FlxqX(x, y, T, p, &Pl, &Ql, &Tl);
     731        6125 :     Dl = FlxqX_extgcd(Pl, Ql, Tl, pp, ptu, ptv);
     732        6125 :     if (ptu) *ptu = FlxX_to_ZXX(*ptu);
     733        6125 :     *ptv = FlxX_to_ZXX(*ptv);
     734        6125 :     d = FlxX_to_ZXX(Dl);
     735             :   }
     736             :   else
     737             :   {
     738           0 :     x = FpXQX_red(x, T, p);
     739           0 :     y = FpXQX_red(y, T, p);
     740           0 :     if (lg(y)>FpXQX_EXTGCD_LIMIT)
     741           0 :       d = FpXQX_extgcd_halfgcd(x, y, T, p, ptu, ptv);
     742             :     else
     743           0 :       d = FpXQX_extgcd_basecase(x, y, T, p, ptu, ptv);
     744             :   }
     745        6125 :   gerepileall(ltop,ptu?3:2,&d,ptv,ptu);
     746        6125 :   return d;
     747             : }
     748             : 
     749             : GEN
     750          68 : FpXQX_dotproduct(GEN x, GEN y, GEN T, GEN p)
     751             : {
     752          68 :   long i, l = minss(lg(x), lg(y));
     753             :   pari_sp av;
     754             :   GEN c;
     755          68 :   if (l == 2) return gen_0;
     756          68 :   av = avma; c = gmul(gel(x,2),gel(y,2));
     757          68 :   for (i=3; i<l; i++) c = gadd(c, gmul(gel(x,i),gel(y,i)));
     758          68 :   return gerepileupto(av, Fq_red(c,T,p));
     759             : }
     760             : 
     761             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
     762             : GEN
     763          63 : FpXQX_resultant(GEN a, GEN b, GEN T, GEN p)
     764             : {
     765             :   long da,db,dc;
     766             :   pari_sp av;
     767          63 :   long vT = get_FpX_var(T);
     768          63 :   GEN c,lb, res = pol_1(vT);
     769             : 
     770          63 :   if (!signe(a) || !signe(b)) return pol_0(vT);
     771          63 :   if (lgefint(p) == 3)
     772             :   {
     773          35 :     pari_sp av = avma;
     774             :     GEN Pl, Ql, Tl, R;
     775          35 :     ulong pp = to_FlxqX(a, b, T, p, &Pl, &Ql, &Tl);
     776          35 :     R = FlxqX_resultant(Pl, Ql, Tl, pp);
     777          35 :     return gerepileupto(av, Flx_to_ZX(R));
     778             :   }
     779             : 
     780          28 :   da = degpol(a);
     781          28 :   db = degpol(b);
     782          28 :   if (db > da)
     783             :   {
     784          14 :     swapspec(a,b, da,db);
     785          14 :     if (both_odd(da,db)) res = FpX_neg(res, p);
     786             :   }
     787          28 :   if (!da) return pol_1(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
     788          28 :   av = avma;
     789         126 :   while (db)
     790             :   {
     791          70 :     lb = gel(b,db+2);
     792          70 :     c = FpXQX_rem(a,b, T,p);
     793          70 :     a = b; b = c; dc = degpol(c);
     794          70 :     if (dc < 0) { set_avma(av); return pol_0(vT); }
     795             : 
     796          70 :     if (both_odd(da,db)) res = FpX_neg(res, p);
     797          70 :     if (!equali1(lb)) res = FpXQ_mul(res, FpXQ_powu(lb, da - dc, T, p), T, p);
     798          70 :     if (gc_needed(av,2))
     799             :     {
     800           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQX_resultant (da = %ld)",da);
     801           0 :       gerepileall(av,3, &a,&b,&res);
     802             :     }
     803          70 :     da = db; /* = degpol(a) */
     804          70 :     db = dc; /* = degpol(b) */
     805             :   }
     806          28 :   res = FpXQ_mul(res, FpXQ_powu(gel(b,2), da, T, p), T, p);
     807          28 :   return gerepileupto(av, res);
     808             : }
     809             : 
     810             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
     811             : GEN
     812          35 : FpXQX_disc(GEN P, GEN T, GEN p)
     813             : {
     814          35 :   pari_sp av = avma;
     815          35 :   GEN L, dP = FpXX_deriv(P, p), D = FpXQX_resultant(P, dP, T, p);
     816             :   long dd;
     817          35 :   if (!signe(D)) return pol_0(get_FpX_var(T));
     818          35 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
     819          35 :   L = leading_coeff(P);
     820          35 :   if (dd && !gequal1(L))
     821           0 :     D = (dd == -1)? FpXQ_div(D,L,T,p): FpXQ_mul(D, FpXQ_powu(L, dd, T, p), T, p);
     822          35 :   if (degpol(P) & 2) D = FpX_neg(D, p);
     823          35 :   return gerepileupto(av, D);
     824             : }
     825             : 
     826             : /***********************************************************************/
     827             : /**                                                                   **/
     828             : /**                       Barrett reduction                           **/
     829             : /**                                                                   **/
     830             : /***********************************************************************/
     831             : 
     832             : /* Return new lgpol */
     833             : static long
     834      239509 : ZXX_lgrenormalizespec(GEN x, long lx)
     835             : {
     836             :   long i;
     837      240019 :   for (i = lx-1; i>=0; i--)
     838      240019 :     if (signe(gel(x,i))) break;
     839      239509 :   return i+1;
     840             : }
     841             : 
     842             : static GEN
     843        2865 : FpXQX_invBarrett_basecase(GEN S, GEN T, GEN p)
     844             : {
     845        2865 :   long i, l=lg(S)-1, lr = l-1, k;
     846        2865 :   GEN r=cgetg(lr, t_POL); r[1]=S[1];
     847        2865 :   gel(r,2) = gen_1;
     848       22687 :   for (i=3; i<lr; i++)
     849             :   {
     850       19822 :     pari_sp av = avma;
     851       19822 :     GEN u = gel(S,l-i+2);
     852      185711 :     for (k=3; k<i; k++)
     853      165889 :       u = Fq_add(u, Fq_mul(gel(S,l-i+k), gel(r,k), NULL, p), NULL, p);
     854       19822 :     gel(r,i) = gerepileupto(av, Fq_red(Fq_neg(u, NULL, p), T, p));
     855             :   }
     856        2865 :   return FpXQX_renormalize(r,lr);
     857             : }
     858             : 
     859             : INLINE GEN
     860      236044 : FpXX_recipspec(GEN x, long l, long n)
     861             : {
     862      236044 :   return RgX_recipspec_shallow(x, l, n);
     863             : }
     864             : 
     865             : static GEN
     866         182 : FpXQX_invBarrett_Newton(GEN S, GEN T, GEN p)
     867             : {
     868         182 :   pari_sp av = avma;
     869         182 :   long nold, lx, lz, lq, l = degpol(S), i, lQ;
     870         182 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
     871         182 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
     872         182 :   for (i=0;i<l;i++) gel(x,i) = gen_0;
     873         182 :   q = RgX_recipspec_shallow(S+2,l+1,l+1); lQ = lgpol(q); q+=2;
     874             :   /* We work on _spec_ FpX's, all the l[xzq] below are lgpol's */
     875             : 
     876             :   /* initialize */
     877         182 :   gel(x,0) = Fq_inv(gel(q,0), T, p);
     878         182 :   if (lQ>1) gel(q,1) = Fq_red(gel(q,1), T, p);
     879         182 :   if (lQ>1 && signe(gel(q,1)))
     880         182 :   {
     881         182 :     GEN u = gel(q, 1);
     882         182 :     if (!gequal1(gel(x,0))) u = Fq_mul(u, Fq_sqr(gel(x,0), T, p), T, p);
     883         182 :     gel(x,1) = Fq_neg(u, T, p); lx = 2;
     884             :   }
     885             :   else
     886           0 :     lx = 1;
     887         182 :   nold = 1;
     888        1519 :   for (; mask > 1; )
     889             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
     890        1155 :     long i, lnew, nnew = nold << 1;
     891             : 
     892        1155 :     if (mask & 1) nnew--;
     893        1155 :     mask >>= 1;
     894             : 
     895        1155 :     lnew = nnew + 1;
     896        1155 :     lq = ZXX_lgrenormalizespec(q, minss(lQ,lnew));
     897        1155 :     z = FpXQX_mulspec(x, q, T, p, lx, lq); /* FIXME: high product */
     898        1155 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
     899        1155 :     z += 2;
     900             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
     901        1155 :     for (i = nold; i < lz; i++) if (signe(gel(z,i))) break;
     902        1155 :     nold = nnew;
     903        1155 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
     904             : 
     905             :     /* z + i represents (x*q - 1) / t^i */
     906        1155 :     lz = ZXX_lgrenormalizespec (z+i, lz-i);
     907        1155 :     z = FpXQX_mulspec(x, z+i, T, p, lx, lz); /* FIXME: low product */
     908        1155 :     lz = lgpol(z); z += 2;
     909        1155 :     if (lz > lnew-i) lz = ZXX_lgrenormalizespec(z, lnew-i);
     910             : 
     911        1155 :     lx = lz+ i;
     912        1155 :     y  = x + i; /* x -= z * t^i, in place */
     913        1155 :     for (i = 0; i < lz; i++) gel(y,i) = Fq_neg(gel(z,i), T, p);
     914             :   }
     915         182 :   x -= 2; setlg(x, lx + 2); x[1] = S[1];
     916         182 :   return gerepilecopy(av, x);
     917             : }
     918             : 
     919             : /* 1/polrecip(S)+O(x^(deg(S)-1)) */
     920             : GEN
     921        3068 : FpXQX_invBarrett(GEN S, GEN T, GEN p)
     922             : {
     923        3068 :   pari_sp ltop = avma;
     924        3068 :   long l = lg(S);
     925             :   GEN r;
     926        3068 :   if (l<5) return pol_0(varn(S));
     927        3047 :   if (l<=FpXQX_INVBARRETT_LIMIT)
     928             :   {
     929        2865 :     GEN c = gel(S,l-1), ci=gen_1;
     930        2865 :     if (!gequal1(c))
     931             :     {
     932        1862 :       ci = Fq_inv(c, T, p);
     933        1862 :       S = FqX_Fq_mul(S, ci, T, p);
     934        1862 :       r = FpXQX_invBarrett_basecase(S, T, p);
     935        1862 :       r = FqX_Fq_mul(r, ci, T, p);
     936             :     } else
     937        1003 :       r = FpXQX_invBarrett_basecase(S, T, p);
     938             :   }
     939             :   else
     940         182 :     r = FpXQX_invBarrett_Newton(S, T, p);
     941        3047 :   return gerepileupto(ltop, r);
     942             : }
     943             : 
     944             : GEN
     945        9169 : FpXQX_get_red(GEN S, GEN T, GEN p)
     946             : {
     947        9169 :   if (typ(S)==t_POL && lg(S)>FpXQX_BARRETT_LIMIT)
     948         752 :     retmkvec2(FpXQX_invBarrett(S,T,p),S);
     949        8417 :   return S;
     950             : }
     951             : 
     952             : /* Compute x mod S where 2 <= degpol(S) <= l+1 <= 2*(degpol(S)-1)
     953             :  * and mg is the Barrett inverse of S. */
     954             : static GEN
     955      118022 : FpXQX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN S, GEN T, GEN p, GEN *pr)
     956             : {
     957             :   GEN q, r;
     958      118022 :   long lt = degpol(S); /*We discard the leading term*/
     959             :   long ld, lm, lT, lmg;
     960      118022 :   ld = l-lt;
     961      118022 :   lm = minss(ld, lgpol(mg));
     962      118022 :   lT  = ZXX_lgrenormalizespec(S+2,lt);
     963      118022 :   lmg = ZXX_lgrenormalizespec(mg+2,lm);
     964      118022 :   q = FpXX_recipspec(x+lt,ld,ld);                 /* q = rec(x)     lq<=ld*/
     965      118022 :   q = FpXQX_mulspec(q+2,mg+2,T,p,lgpol(q),lmg);    /* q = rec(x) * mg lq<=ld+lm*/
     966      118022 :   q = FpXX_recipspec(q+2,minss(ld,lgpol(q)),ld);  /* q = rec (rec(x) * mg) lq<=ld*/
     967      118022 :   if (!pr) return q;
     968      117049 :   r = FpXQX_mulspec(q+2,S+2,T,p,lgpol(q),lT);      /* r = q*pol        lr<=ld+lt*/
     969      117049 :   r = FpXX_subspec(x,r+2,p,lt,minss(lt,lgpol(r))); /* r = x - r   lr<=lt */
     970      117049 :   if (pr == ONLY_REM) return r;
     971       68344 :   *pr = r; return q;
     972             : }
     973             : 
     974             : static GEN
     975       50088 : FpXQX_divrem_Barrett(GEN x, GEN mg, GEN S, GEN T, GEN p, GEN *pr)
     976             : {
     977       50088 :   GEN q = NULL, r = FpXQX_red(x, T, p);
     978       50088 :   long l = lgpol(r), lt = degpol(S), lm = 2*lt-1, v = varn(S);
     979             :   long i;
     980       50088 :   if (l <= lt)
     981             :   {
     982           0 :     if (pr == ONLY_REM) return r;
     983           0 :     if (pr == ONLY_DIVIDES) return signe(r)? NULL: pol_0(v);
     984           0 :     if (pr) *pr = r;
     985           0 :     return pol_0(v);
     986             :   }
     987       50088 :   if (lt <= 1)
     988          21 :     return FpXQX_divrem_basecase(r,S,T,p,pr);
     989       50067 :   if (pr != ONLY_REM && l>lm)
     990             :   {
     991        1341 :     q = cgetg(l-lt+2, t_POL);
     992        1341 :     for (i=0;i<l-lt;i++) gel(q+2,i) = gen_0;
     993             :   }
     994      168170 :   while (l>lm)
     995             :   {
     996       68036 :     GEN zr, zq = FpXQX_divrem_Barrettspec(r+2+l-lm,lm,mg,S,T,p,&zr);
     997       68036 :     long lz = lgpol(zr);
     998       68036 :     if (pr != ONLY_REM)
     999             :     {
    1000       34893 :       long lq = lgpol(zq);
    1001       34893 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1002             :     }
    1003       68036 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
    1004       68036 :     l = l-lm+lz;
    1005             :   }
    1006       50067 :   if (pr == ONLY_REM)
    1007             :   {
    1008       48705 :     if (l > lt)
    1009       48705 :       r = FpXQX_divrem_Barrettspec(r+2,l,mg,S,T,p,ONLY_REM);
    1010             :     else
    1011           0 :       r = FpXQX_renormalize(r, l+2);
    1012       48705 :     setvarn(r, v); return r;
    1013             :   }
    1014        1362 :   if (l > lt)
    1015             :   {
    1016        1281 :     GEN zq = FpXQX_divrem_Barrettspec(r+2,l,mg,S,T,p,pr ? &r: NULL);
    1017        1281 :     if (!q) q = zq;
    1018             :     else
    1019             :     {
    1020        1260 :       long lq = lgpol(zq);
    1021        1260 :       for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
    1022             :     }
    1023             :   }
    1024          81 :   else if (pr)
    1025          81 :     r = FpX_renormalize(r, l+2);
    1026        1362 :   setvarn(q, v); q = FpXQX_renormalize(q, lg(q));
    1027        1362 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
    1028        1362 :   if (pr) { setvarn(r, v); *pr = r; }
    1029        1362 :   return q;
    1030             : }
    1031             : 
    1032             : GEN
    1033      202716 : FpXQX_divrem(GEN x, GEN S, GEN T, GEN p, GEN *pr)
    1034             : {
    1035      202716 :   GEN B, y = get_FpXQX_red(S, &B);
    1036      202716 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1037      202716 :   if (pr==ONLY_REM) return FpXQX_rem(x, y, T, p);
    1038      202716 :   if (lgefint(p) == 3)
    1039             :   {
    1040             :     GEN a, b, t, z;
    1041      190672 :     pari_sp tetpil, av = avma;
    1042      190672 :     ulong pp = to_FlxqX(x, y, T, p, &a, &b, &t);
    1043      190672 :     z = FlxqX_divrem(a, b, t, pp, pr);
    1044      190672 :     if (pr == ONLY_DIVIDES && !z) return gc_NULL(av);
    1045      190672 :     tetpil=avma;
    1046      190672 :     z = FlxX_to_ZXX(z);
    1047      190672 :     if (pr && pr != ONLY_DIVIDES && pr != ONLY_REM)
    1048      189132 :       *pr = FlxX_to_ZXX(*pr);
    1049        1540 :     else return gerepile(av, tetpil, z);
    1050      189132 :     gerepileallsp(av,tetpil,2, pr, &z);
    1051      189132 :     return z;
    1052             :   }
    1053       12044 :   if (!B && d+3 < FpXQX_DIVREM_BARRETT_LIMIT)
    1054       10664 :     return FpXQX_divrem_basecase(x,y,T,p,pr);
    1055             :   else
    1056             :   {
    1057        1380 :     pari_sp av=avma;
    1058        1380 :     GEN mg = B? B: FpXQX_invBarrett(y, T, p);
    1059        1380 :     GEN q = FpXQX_divrem_Barrett(x,mg,y,T,p,pr);
    1060        1380 :     if (!q) return gc_NULL(av);
    1061        1380 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q);
    1062         393 :     gerepileall(av,2,&q,pr);
    1063         393 :     return q;
    1064             :   }
    1065             : }
    1066             : 
    1067             : GEN
    1068      190393 : FpXQX_rem(GEN x, GEN S, GEN T, GEN p)
    1069             : {
    1070      190393 :   GEN B, y = get_FpXQX_red(S, &B);
    1071      190393 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1072      190393 :   if (d < 0) return FpXQX_red(x, T, p);
    1073      173576 :   if (lgefint(p) == 3)
    1074             :   {
    1075        9707 :     pari_sp av = avma;
    1076             :     GEN a, b, t, z;
    1077        9707 :     ulong pp = to_FlxqX(x, y, T, p, &a, &b, &t);
    1078        9707 :     z = FlxqX_rem(a, b, t, pp);
    1079        9707 :     z = FlxX_to_ZXX(z);
    1080        9707 :     return gerepileupto(av, z);
    1081             :   }
    1082      163869 :   if (!B && d+3 < FpXQX_REM_BARRETT_LIMIT)
    1083      115161 :     return FpXQX_divrem_basecase(x,y, T, p, ONLY_REM);
    1084             :   else
    1085             :   {
    1086       48708 :     pari_sp av=avma;
    1087       48708 :     GEN mg = B? B: FpXQX_invBarrett(y, T, p);
    1088       48708 :     GEN r = FpXQX_divrem_Barrett(x, mg, y, T, p, ONLY_REM);
    1089       48708 :     return gerepileupto(av, r);
    1090             :   }
    1091             : }
    1092             : 
    1093             : /* x + y*z mod p */
    1094             : INLINE GEN
    1095       11872 : Fq_addmul(GEN x, GEN y, GEN z, GEN T, GEN p)
    1096             : {
    1097             :   pari_sp av;
    1098       11872 :   if (!signe(y) || !signe(z)) return Fq_red(x, T, p);
    1099       11760 :   if (!signe(x)) return Fq_mul(z,y, T, p);
    1100       11746 :   av = avma;
    1101       11746 :   return gerepileupto(av, Fq_add(x, Fq_mul(y, z, T, p), T, p));
    1102             : }
    1103             : 
    1104             : GEN
    1105        5936 : FpXQX_div_by_X_x(GEN a, GEN x, GEN T, GEN p, GEN *r)
    1106             : {
    1107        5936 :   long l = lg(a)-1, i;
    1108        5936 :   GEN z = cgetg(l, t_POL);
    1109        5936 :   z[1] = evalsigne(1) | evalvarn(0);
    1110        5936 :   gel(z, l-1) = gel(a,l);
    1111       17808 :   for (i=l-2; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1112       11872 :     gel(z, i) = Fq_addmul(gel(a,i+1), x, gel(z,i+1), T, p);
    1113        5936 :   if (r) *r = Fq_addmul(gel(a,2), x, gel(z,2), T, p);
    1114        5936 :   return z;
    1115             : }
    1116             : 
    1117             : struct _FpXQXQ {
    1118             :   GEN T, S;
    1119             :   GEN p;
    1120             : };
    1121             : 
    1122        2766 : static GEN _FpXQX_mul(void *data, GEN a,GEN b)
    1123             : {
    1124        2766 :   struct _FpXQXQ *d=(struct _FpXQXQ*)data;
    1125        2766 :   return FpXQX_mul(a,b,d->T,d->p);
    1126             : }
    1127             : 
    1128        1925 : static GEN _FpXQX_sqr(void *data, GEN a)
    1129             : {
    1130        1925 :   struct _FpXQXQ *d=(struct _FpXQXQ*)data;
    1131        1925 :   return FpXQX_sqr(a, d->T, d->p);
    1132             : }
    1133             : 
    1134             : GEN
    1135          35 : FpXQX_powu(GEN x, ulong n, GEN T, GEN p)
    1136             : {
    1137             :   struct _FpXQXQ D;
    1138          35 :   if (n==0) return pol_1(varn(x));
    1139          35 :   D.T = T; D.p = p;
    1140          35 :   return gen_powu(x, n, (void *)&D, _FpXQX_sqr, _FpXQX_mul);
    1141             : }
    1142             : 
    1143             : GEN
    1144         158 : FpXQXV_prod(GEN V, GEN T, GEN p)
    1145             : {
    1146         158 :   if (lgefint(p) == 3)
    1147             :   {
    1148          12 :     pari_sp av = avma;
    1149          12 :     ulong pp = p[2];
    1150          12 :     GEN Tl = ZXT_to_FlxT(T, pp);
    1151          12 :     GEN Vl = ZXXV_to_FlxXV(V, pp, get_FpX_var(T));
    1152          12 :     Tl = FlxqXV_prod(Vl, Tl, pp);
    1153          12 :     return gerepileupto(av, FlxX_to_ZXX(Tl));
    1154             :   }
    1155             :   else
    1156             :   {
    1157             :     struct _FpXQXQ d;
    1158         146 :     d.T=T; d.p=p;
    1159         146 :     return gen_product(V, (void*)&d, &_FpXQX_mul);
    1160             :   }
    1161             : }
    1162             : 
    1163             : static GEN
    1164        9905 : _FpXQX_divrem(void * E, GEN x, GEN y, GEN *r)
    1165             : {
    1166        9905 :   struct _FpXQXQ *d = (struct _FpXQXQ *) E;
    1167        9905 :   return FpXQX_divrem(x, y, d->T, d->p, r);
    1168             : }
    1169             : 
    1170             : static GEN
    1171       34853 : _FpXQX_add(void * E, GEN x, GEN y)
    1172             : {
    1173       34853 :   struct _FpXQXQ *d = (struct _FpXQXQ *) E;
    1174       34853 :   return FpXX_add(x, y, d->p);
    1175             : }
    1176             : 
    1177             : static GEN
    1178        4088 : _FpXQX_sub(void * E, GEN x, GEN y) {
    1179        4088 :   struct _FpXQXQ *d = (struct _FpXQXQ*) E;
    1180        4088 :   return FpXX_sub(x,y, d->p);
    1181             : }
    1182             : 
    1183             : static struct bb_ring FpXQX_ring = { _FpXQX_add, _FpXQX_mul, _FpXQX_sqr };
    1184             : 
    1185             : GEN
    1186         588 : FpXQX_digits(GEN x, GEN B, GEN T, GEN p)
    1187             : {
    1188         588 :   pari_sp av = avma;
    1189         588 :   long d = degpol(B), n = (lgpol(x)+d-1)/d;
    1190             :   GEN z;
    1191             :   struct _FpXQXQ D;
    1192         588 :   D.T = T; D.p = p;
    1193         588 :   z = gen_digits(x, B, n, (void *)&D, &FpXQX_ring, _FpXQX_divrem);
    1194         588 :   return gerepileupto(av, z);
    1195             : }
    1196             : 
    1197             : GEN
    1198         189 : FpXQXV_FpXQX_fromdigits(GEN x, GEN B, GEN T, GEN p)
    1199             : {
    1200         189 :   pari_sp av = avma;
    1201             :   struct _FpXQXQ D;
    1202             :   GEN z;
    1203         189 :   D.T = T; D.p = p;
    1204         189 :   z = gen_fromdigits(x,B,(void *)&D, &FpXQX_ring);
    1205         189 :   return gerepileupto(av, z);
    1206             : }
    1207             : 
    1208             : /* Q an FpXY (t_POL with FpX coeffs), evaluate at X = x */
    1209             : GEN
    1210       48083 : FpXY_evalx(GEN Q, GEN x, GEN p)
    1211             : {
    1212       48083 :   long i, lb = lg(Q);
    1213             :   GEN z;
    1214       48083 :   z = cgetg(lb, t_POL); z[1] = Q[1];
    1215      436296 :   for (i=2; i<lb; i++)
    1216             :   {
    1217      388213 :     GEN q = gel(Q,i);
    1218      388213 :     gel(z,i) = typ(q) == t_INT? modii(q,p): FpX_eval(q, x, p);
    1219             :   }
    1220       48083 :   return FpX_renormalize(z, lb);
    1221             : }
    1222             : /* Q an FpXY, evaluate at Y = y */
    1223             : GEN
    1224       18834 : FpXY_evaly(GEN Q, GEN y, GEN p, long vx)
    1225             : {
    1226       18834 :   pari_sp av = avma;
    1227       18834 :   long i, lb = lg(Q);
    1228             :   GEN z;
    1229       18834 :   if (!signe(Q)) return pol_0(vx);
    1230       18806 :   if (lb == 3 || !signe(y)) {
    1231          84 :     z = gel(Q, 2);
    1232          84 :     return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1233             :   }
    1234       18722 :   z = gel(Q, lb-1);
    1235       18722 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1236       18722 :   for (i=lb-2; i>=2; i--) z = Fq_add(gel(Q,i), FpX_Fp_mul(z, y, p), NULL, p);
    1237       18722 :   return gerepileupto(av, z);
    1238             : }
    1239             : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
    1240             : GEN
    1241       13909 : FpXY_eval(GEN Q, GEN y, GEN x, GEN p)
    1242             : {
    1243       13909 :   pari_sp av = avma;
    1244       13909 :   return gerepileuptoint(av, FpX_eval(FpXY_evalx(Q, x, p), y, p));
    1245             : }
    1246             : 
    1247             : GEN
    1248        2825 : FpXY_FpXQV_evalx(GEN P, GEN x, GEN T, GEN p)
    1249             : {
    1250        2825 :   long i, lP = lg(P);
    1251        2825 :   GEN res = cgetg(lP,t_POL);
    1252        2825 :   res[1] = P[1];
    1253       32908 :   for(i=2; i<lP; i++)
    1254       60166 :     gel(res,i) = typ(gel(P,i))==t_INT? icopy(gel(P,i)):
    1255       30083 :                                        FpX_FpXQV_eval(gel(P,i), x, T, p);
    1256        2825 :   return FlxX_renormalize(res, lP);
    1257             : }
    1258             : 
    1259             : GEN
    1260         147 : FpXY_FpXQ_evalx(GEN P, GEN x, GEN T, GEN p)
    1261             : {
    1262         147 :   pari_sp av = avma;
    1263         147 :   long n = brent_kung_optpow(get_FpX_degree(T)-1,lgpol(P),1);
    1264         147 :   GEN xp = FpXQ_powers(x, n, T, p);
    1265         147 :   return gerepileupto(av, FpXY_FpXQV_evalx(P, xp, T, p));
    1266             : }
    1267             : 
    1268             : /*******************************************************************/
    1269             : /*                                                                 */
    1270             : /*                       (Fp[X]/T(X))[Y] / S(Y)                    */
    1271             : /*                                                                 */
    1272             : /*******************************************************************/
    1273             : 
    1274             : /*Preliminary implementation to speed up FpX_ffisom*/
    1275             : typedef struct {
    1276             :   GEN S, T, p;
    1277             : } FpXYQQ_muldata;
    1278             : 
    1279             : /* reduce x in Fp[X, Y] in the algebra Fp[X,Y]/ (S(X),T(Y)) */
    1280             : static GEN
    1281         476 : FpXYQQ_redswap(GEN x, GEN S, GEN T, GEN p)
    1282             : {
    1283         476 :   pari_sp ltop=avma;
    1284         476 :   long n = get_FpX_degree(S);
    1285         476 :   long m = get_FpX_degree(T);
    1286         476 :   long v = get_FpX_var(T);
    1287         476 :   GEN V = RgXY_swap(x,m,v);
    1288         476 :   V = FpXQX_red(V,S,p);
    1289         476 :   V = RgXY_swap(V,n,v);
    1290         476 :   return gerepilecopy(ltop,V);
    1291             : }
    1292             : static GEN
    1293         280 : FpXYQQ_sqr(void *data, GEN x)
    1294             : {
    1295         280 :   FpXYQQ_muldata *D = (FpXYQQ_muldata*)data;
    1296         280 :   return FpXYQQ_redswap(FpXQX_sqr(x, D->T, D->p),D->S,D->T,D->p);
    1297             : 
    1298             : }
    1299             : static GEN
    1300         196 : FpXYQQ_mul(void *data, GEN x, GEN y)
    1301             : {
    1302         196 :   FpXYQQ_muldata *D = (FpXYQQ_muldata*)data;
    1303         196 :   return FpXYQQ_redswap(FpXQX_mul(x,y, D->T, D->p),D->S,D->T,D->p);
    1304             : }
    1305             : 
    1306             : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
    1307             : GEN
    1308         182 : FpXYQQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    1309             : {
    1310         182 :   pari_sp av = avma;
    1311             :   FpXYQQ_muldata D;
    1312             :   GEN y;
    1313         182 :   if (lgefint(p) == 3)
    1314             :   {
    1315           0 :     ulong pp = to_FlxqX(x, NULL, T, p, &x, NULL, &T);
    1316           0 :     S = ZX_to_Flx(S, pp);
    1317           0 :     y = FlxX_to_ZXX( FlxYqq_pow(x, n, S, T, pp) );
    1318             :   }
    1319             :   else
    1320             :   {
    1321         182 :     D.S = S;
    1322         182 :     D.T = T;
    1323         182 :     D.p = p;
    1324         182 :     y = gen_pow(x, n, (void*)&D, &FpXYQQ_sqr, &FpXYQQ_mul);
    1325             :   }
    1326         182 :   return gerepileupto(av, y);
    1327             : }
    1328             : 
    1329             : GEN
    1330       32647 : FpXQXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p) {
    1331       32647 :   return FpXQX_rem(FpXQX_mul(x, y, T, p), S, T, p);
    1332             : }
    1333             : 
    1334             : GEN
    1335      137897 : FpXQXQ_sqr(GEN x, GEN S, GEN T, GEN p) {
    1336      137897 :   return FpXQX_rem(FpXQX_sqr(x, T, p), S, T, p);
    1337             : }
    1338             : 
    1339             : /* Inverse of x in Z/pZ[X]/(pol) or NULL if inverse doesn't exist
    1340             :  * return lift(1 / (x mod (p,pol))) */
    1341             : GEN
    1342          21 : FpXQXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
    1343             : {
    1344          21 :   GEN V, z = FpXQX_extgcd(get_FpXQX_mod(S), x, T, p, NULL, &V);
    1345          21 :   if (degpol(z)) return NULL;
    1346          21 :   z = gel(z,2);
    1347          21 :   z = typ(z)==t_INT ? Fp_invsafe(z,p) : FpXQ_invsafe(z,T,p);
    1348          21 :   if (!z) return NULL;
    1349          21 :   return typ(z)==t_INT ? FpXX_Fp_mul(V, z, p): FpXQX_FpXQ_mul(V, z, T, p);
    1350             : }
    1351             : 
    1352             : GEN
    1353          21 : FpXQXQ_inv(GEN x, GEN S, GEN T,GEN p)
    1354             : {
    1355          21 :   pari_sp av = avma;
    1356          21 :   GEN U = FpXQXQ_invsafe(x, S, T, p);
    1357          21 :   if (!U) pari_err_INV("FpXQXQ_inv",x);
    1358          21 :   return gerepileupto(av, U);
    1359             : }
    1360             : 
    1361             : GEN
    1362           0 : FpXQXQ_div(GEN x,GEN y,GEN S, GEN T,GEN p)
    1363             : {
    1364           0 :   pari_sp av = avma;
    1365           0 :   return gerepileupto(av, FpXQXQ_mul(x, FpXQXQ_inv(y,S,T,p),S,T,p));
    1366             : }
    1367             : 
    1368             : static GEN
    1369       36673 : _FpXQXQ_cmul(void *data, GEN P, long a, GEN x) {
    1370       36673 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1371       36673 :   GEN y = gel(P,a+2);
    1372       73323 :   return typ(y)==t_INT ? FpXX_Fp_mul(x,y, d->p):
    1373       36650 :                          FpXX_FpX_mul(x,y,d->p);
    1374             : }
    1375             : static GEN
    1376       12427 : _FpXQXQ_red(void *data, GEN x) {
    1377       12427 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1378       12427 :   return FpXQX_red(x, d->T, d->p);
    1379             : }
    1380             : static GEN
    1381       30537 : _FpXQXQ_mul(void *data, GEN x, GEN y) {
    1382       30537 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1383       30537 :   return FpXQXQ_mul(x,y, d->S,d->T, d->p);
    1384             : }
    1385             : static GEN
    1386      137897 : _FpXQXQ_sqr(void *data, GEN x) {
    1387      137897 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1388      137897 :   return FpXQXQ_sqr(x, d->S,d->T, d->p);
    1389             : }
    1390             : 
    1391             : static GEN
    1392        9736 : _FpXQXQ_one(void *data) {
    1393        9736 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1394        9736 :   return pol_1(get_FpXQX_var(d->S));
    1395             : }
    1396             : 
    1397             : static GEN
    1398          68 : _FpXQXQ_zero(void *data) {
    1399          68 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1400          68 :   return pol_0(get_FpXQX_var(d->S));
    1401             : }
    1402             : 
    1403             : static struct bb_algebra FpXQXQ_algebra = { _FpXQXQ_red, _FpXQX_add,
    1404             :        _FpXQX_sub, _FpXQXQ_mul, _FpXQXQ_sqr, _FpXQXQ_one, _FpXQXQ_zero };
    1405             : 
    1406             : const struct bb_algebra *
    1407         628 : get_FpXQXQ_algebra(void **E, GEN S, GEN T, GEN p)
    1408             : {
    1409         628 :   GEN z = new_chunk(sizeof(struct _FpXQXQ));
    1410         628 :   struct _FpXQXQ *e = (struct _FpXQXQ *) z;
    1411         628 :   e->T = FpX_get_red(T, p);
    1412         628 :   e->S = FpXQX_get_red(S, e->T, p);
    1413         628 :   e->p  = p; *E = (void*)e;
    1414         628 :   return &FpXQXQ_algebra;
    1415             : }
    1416             : 
    1417             : static struct bb_algebra FpXQX_algebra = { _FpXQXQ_red, _FpXQX_add,
    1418             :        _FpXQX_sub, _FpXQX_mul, _FpXQX_sqr, _FpXQXQ_one, _FpXQXQ_zero };
    1419             : 
    1420             : const struct bb_algebra *
    1421          42 : get_FpXQX_algebra(void **E, GEN T, GEN p, long v)
    1422             : {
    1423          42 :   GEN z = new_chunk(sizeof(struct _FpXQXQ));
    1424          42 :   struct _FpXQXQ *e = (struct _FpXQXQ *) z;
    1425          42 :   e->T = FpX_get_red(T, p);
    1426          42 :   e->S = pol_x(v);
    1427          42 :   e->p  = p; *E = (void*)e;
    1428          42 :   return &FpXQX_algebra;
    1429             : }
    1430             : 
    1431             : /* x over Fq, return lift(x^n) mod S */
    1432             : GEN
    1433        1431 : FpXQXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    1434             : {
    1435        1431 :   pari_sp ltop = avma;
    1436             :   GEN y;
    1437             :   struct _FpXQXQ D;
    1438        1431 :   long s = signe(n);
    1439        1431 :   if (!s) return pol_1(varn(x));
    1440        1431 :   if (is_pm1(n)) /* +/- 1 */
    1441           0 :     return (s < 0)? FpXQXQ_inv(x,S,T,p): ZXX_copy(x);
    1442        1431 :   if (lgefint(p) == 3)
    1443             :   {
    1444           0 :     ulong pp = to_FlxqX(x, S, T, p, &x, &S, &T);
    1445           0 :     GEN z = FlxqXQ_pow(x, n, S, T, pp);
    1446           0 :     y = FlxX_to_ZXX(z);
    1447             :   }
    1448             :   else
    1449             :   {
    1450        1431 :     T = FpX_get_red(T, p);
    1451        1431 :     S = FpXQX_get_red(S, T, p);
    1452        1431 :     D.S = S; D.T = T; D.p = p;
    1453        1431 :     if (s < 0) x = FpXQXQ_inv(x,S,T,p);
    1454        1431 :     y = gen_pow(x, n, (void*)&D,&_FpXQXQ_sqr,&_FpXQXQ_mul);
    1455             :   }
    1456        1431 :   return gerepileupto(ltop, y);
    1457             : }
    1458             : 
    1459             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    1460             : GEN
    1461        1403 : FpXQXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
    1462             : {
    1463             :   struct _FpXQXQ D;
    1464        1403 :   int use_sqr = 2*degpol(x) >= get_FpXQX_degree(S);
    1465        1403 :   T = FpX_get_red(T, p);
    1466        1403 :   S = FpXQX_get_red(S, T, p);
    1467        1403 :   D.S = S; D.T = T; D.p = p;
    1468        1403 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FpXQXQ_sqr, &_FpXQXQ_mul,&_FpXQXQ_one);
    1469             : }
    1470             : 
    1471             : /* Let v a linear form, return the linear form z->v(tau*z)
    1472             :    that is, v*(M_tau) */
    1473             : 
    1474             : INLINE GEN
    1475          48 : FpXQX_recipspec(GEN x, long l, long n)
    1476             : {
    1477          48 :   return RgX_recipspec_shallow(x, l, n);
    1478             : }
    1479             : 
    1480             : static GEN
    1481          16 : FpXQXQ_transmul_init(GEN tau, GEN S, GEN T, GEN p)
    1482             : {
    1483             :   GEN bht;
    1484          16 :   GEN h, Sp = get_FpXQX_red(S, &h);
    1485          16 :   long n = degpol(Sp), vT = varn(Sp);
    1486          16 :   GEN ft = FpXQX_recipspec(Sp+2, n+1, n+1);
    1487          16 :   GEN bt = FpXQX_recipspec(tau+2, lgpol(tau), n);
    1488          16 :   setvarn(ft, vT); setvarn(bt, vT);
    1489          16 :   if (h)
    1490           0 :     bht = FpXQXn_mul(bt, h, n-1, T, p);
    1491             :   else
    1492             :   {
    1493          16 :     GEN bh = FpXQX_div(RgX_shift_shallow(tau, n-1), S, T, p);
    1494          16 :     bht = FpXQX_recipspec(bh+2, lgpol(bh), n-1);
    1495          16 :     setvarn(bht, vT);
    1496             :   }
    1497          16 :   return mkvec3(bt, bht, ft);
    1498             : }
    1499             : 
    1500             : static GEN
    1501          40 : FpXQXQ_transmul(GEN tau, GEN a, long n, GEN T, GEN p)
    1502             : {
    1503          40 :   pari_sp ltop = avma;
    1504             :   GEN t1, t2, t3, vec;
    1505          40 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    1506          40 :   if (signe(a)==0) return pol_0(varn(a));
    1507          40 :   t2 = RgX_shift_shallow(FpXQX_mul(bt, a, T, p),1-n);
    1508          40 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    1509          32 :   t1 = RgX_shift_shallow(FpXQX_mul(ft, a, T, p),-n);
    1510          32 :   t3 = FpXQXn_mul(t1, bht, n-1, T, p);
    1511          32 :   vec = FpXX_sub(t2, RgX_shift_shallow(t3, 1), p);
    1512          32 :   return gerepileupto(ltop, vec);
    1513             : }
    1514             : 
    1515             : static GEN
    1516           8 : polxn_FpXX(long n, long v, long vT)
    1517             : {
    1518           8 :   long i, a = n+2;
    1519           8 :   GEN p = cgetg(a+1, t_POL);
    1520           8 :   p[1] = evalsigne(1)|evalvarn(v);
    1521           8 :   for (i = 2; i < a; i++) gel(p,i) = pol_0(vT);
    1522           8 :   gel(p,a) = pol_1(vT); return p;
    1523             : }
    1524             : 
    1525             : GEN
    1526           8 : FpXQXQ_minpoly(GEN x, GEN S, GEN T, GEN p)
    1527             : {
    1528           8 :   pari_sp ltop = avma;
    1529             :   long vS, vT, n;
    1530             :   GEN v_x, g, tau;
    1531           8 :   vS = get_FpXQX_var(S);
    1532           8 :   vT = get_FpX_var(T);
    1533           8 :   n = get_FpXQX_degree(S);
    1534           8 :   g = pol_1(vS);
    1535           8 :   tau = pol_1(vS);
    1536           8 :   S = FpXQX_get_red(S, T, p);
    1537           8 :   v_x = FpXQXQ_powers(x, usqrt(2*n), S, T, p);
    1538          24 :   while(signe(tau) != 0)
    1539             :   {
    1540             :     long i, j, m, k1;
    1541             :     GEN M, v, tr;
    1542             :     GEN g_prime, c;
    1543           8 :     if (degpol(g) == n) { tau = pol_1(vS); g = pol_1(vS); }
    1544           8 :     v = random_FpXQX(n, vS, T, p);
    1545           8 :     tr = FpXQXQ_transmul_init(tau, S, T, p);
    1546           8 :     v = FpXQXQ_transmul(tr, v, n, T, p);
    1547           8 :     m = 2*(n-degpol(g));
    1548           8 :     k1 = usqrt(m);
    1549           8 :     tr = FpXQXQ_transmul_init(gel(v_x,k1+1), S, T, p);
    1550           8 :     c = cgetg(m+2,t_POL);
    1551           8 :     c[1] = evalsigne(1)|evalvarn(vS);
    1552          40 :     for (i=0; i<m; i+=k1)
    1553             :     {
    1554          32 :       long mj = minss(m-i, k1);
    1555         100 :       for (j=0; j<mj; j++)
    1556          68 :         gel(c,m+1-(i+j)) = FpXQX_dotproduct(v, gel(v_x,j+1), T, p);
    1557          32 :       v = FpXQXQ_transmul(tr, v, n, T, p);
    1558             :     }
    1559           8 :     c = FpXX_renormalize(c, m+2);
    1560             :     /* now c contains <v,x^i> , i = 0..m-1  */
    1561           8 :     M = FpXQX_halfgcd(polxn_FpXX(m, vS, vT), c, T, p);
    1562           8 :     g_prime = gmael(M, 2, 2);
    1563           8 :     if (degpol(g_prime) < 1) continue;
    1564           8 :     g = FpXQX_mul(g, g_prime, T, p);
    1565           8 :     tau = FpXQXQ_mul(tau, FpXQX_FpXQXQV_eval(g_prime, v_x, S, T, p), S, T, p);
    1566             :   }
    1567           8 :   g = FpXQX_normalize(g,T, p);
    1568           8 :   return gerepilecopy(ltop,g);
    1569             : }
    1570             : 
    1571             : GEN
    1572           0 : FpXQXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
    1573             : {
    1574           0 :   return RgXV_to_RgM(FpXQXQ_powers(y,m-1,S,T,p),n);
    1575             : }
    1576             : 
    1577             : GEN
    1578        2636 : FpXQX_FpXQXQV_eval(GEN P, GEN V, GEN S, GEN T, GEN p)
    1579             : {
    1580             :   struct _FpXQXQ D;
    1581        2636 :   T = FpX_get_red(T, p);
    1582        2636 :   S = FpXQX_get_red(S, T, p);
    1583        2636 :   D.S=S; D.T=T; D.p=p;
    1584        2636 :   return gen_bkeval_powers(P, degpol(P), V, (void*)&D, &FpXQXQ_algebra,
    1585             :                                                    _FpXQXQ_cmul);
    1586             : }
    1587             : 
    1588             : GEN
    1589         353 : FpXQX_FpXQXQ_eval(GEN Q, GEN x, GEN S, GEN T, GEN p)
    1590             : {
    1591             :   struct _FpXQXQ D;
    1592         353 :   int use_sqr = 2*degpol(x) >= get_FpXQX_degree(S);
    1593         353 :   T = FpX_get_red(T, p);
    1594         353 :   S = FpXQX_get_red(S, T, p);
    1595         353 :   D.S=S; D.T=T; D.p=p;
    1596         353 :   return gen_bkeval(Q, degpol(Q), x, use_sqr, (void*)&D, &FpXQXQ_algebra,
    1597             :       _FpXQXQ_cmul);
    1598             : }
    1599             : 
    1600             : static GEN
    1601         245 : FpXQXQ_autpow_sqr(void * E, GEN x)
    1602             : {
    1603         245 :   struct _FpXQXQ *D = (struct _FpXQXQ *)E;
    1604         245 :   GEN S = D->S, T = D->T, p = D->p;
    1605         245 :   GEN phi = gel(x,1), S1 = gel(x,2);
    1606         245 :   long n = brent_kung_optpow(get_FpX_degree(T)-1,lgpol(S1)+1,1);
    1607         245 :   GEN V = FpXQ_powers(phi, n, T, p);
    1608         245 :   GEN phi2 = FpX_FpXQV_eval(phi, V, T, p);
    1609         245 :   GEN Sphi = FpXY_FpXQV_evalx(S1, V, T, p);
    1610         245 :   GEN S2 = FpXQX_FpXQXQ_eval(Sphi, S1, S, T, p);
    1611         245 :   return mkvec2(phi2, S2);
    1612             : }
    1613             : 
    1614             : static GEN
    1615          85 : FpXQXQ_autpow_mul(void * E, GEN x, GEN y)
    1616             : {
    1617          85 :   struct _FpXQXQ *D = (struct _FpXQXQ *)E;
    1618          85 :   GEN S = D->S, T = D->T, p = D->p;
    1619          85 :   GEN phi1 = gel(x,1), S1 = gel(x,2);
    1620          85 :   GEN phi2 = gel(y,1), S2 = gel(y,2);
    1621          85 :   long n = brent_kung_optpow(get_FpX_degree(T)-1, lgpol(S1)+1, 1);
    1622          85 :   GEN V = FpXQ_powers(phi2, n, T, p);
    1623          85 :   GEN phi3 = FpX_FpXQV_eval(phi1, V, T, p);
    1624          85 :   GEN Sphi = FpXY_FpXQV_evalx(S1, V, T, p);
    1625          85 :   GEN S3 = FpXQX_FpXQXQ_eval(Sphi, S2, S, T, p);
    1626          85 :   return mkvec2(phi3, S3);
    1627             : }
    1628             : 
    1629             : GEN
    1630         231 : FpXQXQ_autpow(GEN aut, long n, GEN S, GEN T, GEN p)
    1631             : {
    1632             :   struct _FpXQXQ D;
    1633         231 :   T = FpX_get_red(T, p);
    1634         231 :   S = FpXQX_get_red(S, T, p);
    1635         231 :   D.S=S; D.T=T; D.p=p;
    1636         231 :   return gen_powu(aut,n,&D,FpXQXQ_autpow_sqr,FpXQXQ_autpow_mul);
    1637             : }
    1638             : 
    1639             : static GEN
    1640           1 : FpXQXQ_auttrace_mul(void *E, GEN x, GEN y)
    1641             : {
    1642           1 :   struct _FpXQXQ *D = (struct _FpXQXQ *)E;
    1643           1 :   GEN S = D->S, T = D->T;
    1644           1 :   GEN p = D->p;
    1645           1 :   GEN S1 = gel(x,1), a1 = gel(x,2);
    1646           1 :   GEN S2 = gel(y,1), a2 = gel(y,2);
    1647           1 :   long n = brent_kung_optpow(maxss(degpol(S1),degpol(a1)),2,1);
    1648           1 :   GEN V = FpXQXQ_powers(S2, n, S, T, p);
    1649           1 :   GEN S3 = FpXQX_FpXQXQV_eval(S1, V, S, T, p);
    1650           1 :   GEN aS = FpXQX_FpXQXQV_eval(a1, V, S, T, p);
    1651           1 :   GEN a3 = FpXX_add(aS, a2, p);
    1652           1 :   return mkvec2(S3, a3);
    1653             : }
    1654             : 
    1655             : static GEN
    1656           1 : FpXQXQ_auttrace_sqr(void *E, GEN x)
    1657           1 : { return FpXQXQ_auttrace_mul(E, x, x); }
    1658             : 
    1659             : GEN
    1660           8 : FpXQXQ_auttrace(GEN aut, long n, GEN S, GEN T, GEN p)
    1661             : {
    1662             :   struct _FpXQXQ D;
    1663           8 :   T = FpX_get_red(T, p);
    1664           8 :   S = FpXQX_get_red(S, T, p);
    1665           8 :   D.S=S; D.T=T; D.p=p;
    1666           8 :   return gen_powu(aut,n,&D,FpXQXQ_auttrace_sqr,FpXQXQ_auttrace_mul);
    1667             : }
    1668             : 
    1669             : static GEN
    1670        1174 : FpXQXQ_autsum_mul(void *E, GEN x, GEN y)
    1671             : {
    1672        1174 :   struct _FpXQXQ *D = (struct _FpXQXQ *) E;
    1673        1174 :   GEN S = D->S, T = D->T, p = D->p;
    1674        1174 :   GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
    1675        1174 :   GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
    1676        1174 :   long n2 = brent_kung_optpow(get_FpX_degree(T)-1, lgpol(S1)+lgpol(a1)+1, 1);
    1677        1174 :   GEN V2 = FpXQ_powers(phi2, n2, T, p);
    1678        1174 :   GEN phi3 = FpX_FpXQV_eval(phi1, V2, T, p);
    1679        1174 :   GEN Sphi = FpXY_FpXQV_evalx(S1, V2, T, p);
    1680        1174 :   GEN aphi = FpXY_FpXQV_evalx(a1, V2, T, p);
    1681        1174 :   long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
    1682        1174 :   GEN V = FpXQXQ_powers(S2, n, S, T, p);
    1683        1174 :   GEN S3 = FpXQX_FpXQXQV_eval(Sphi, V, S, T, p);
    1684        1174 :   GEN aS = FpXQX_FpXQXQV_eval(aphi, V, S, T, p);
    1685        1174 :   GEN a3 = FpXQXQ_mul(aS, a2, S, T, p);
    1686        1174 :   return mkvec3(phi3, S3, a3);
    1687             : }
    1688             : 
    1689             : static GEN
    1690        1134 : FpXQXQ_autsum_sqr(void * T, GEN x)
    1691        1134 : { return FpXQXQ_autsum_mul(T,x,x); }
    1692             : 
    1693             : GEN
    1694        1120 : FpXQXQ_autsum(GEN aut, long n, GEN S, GEN T, GEN p)
    1695             : {
    1696             :   struct _FpXQXQ D;
    1697        1120 :   T = FpX_get_red(T, p);
    1698        1120 :   S = FpXQX_get_red(S, T, p);
    1699        1120 :   D.S=S; D.T=T; D.p=p;
    1700        1120 :   return gen_powu(aut,n,&D,FpXQXQ_autsum_sqr,FpXQXQ_autsum_mul);
    1701             : }
    1702             : 
    1703             : GEN
    1704          32 : FpXQXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
    1705             : {
    1706          32 :   pari_sp av = avma;
    1707             :   GEN z, kx, ky;
    1708             :   long d;
    1709          32 :   if (ZXX_is_ZX(y) && ZXX_is_ZX(x))
    1710           0 :     return FpXn_mul(x,y,n,p);
    1711          32 :   d = get_FpX_degree(T);
    1712          32 :   kx = ZXX_to_Kronecker(x, d);
    1713          32 :   ky = ZXX_to_Kronecker(y, d);
    1714          32 :   z = Kronecker_to_FpXQX(ZXn_mul(ky,kx,(2*d-1)*n), T, p);
    1715          32 :   return gerepileupto(av, z);
    1716             : }
    1717             : 
    1718             : GEN
    1719           0 : FpXQXn_sqr(GEN x, long n, GEN T, GEN p)
    1720             : {
    1721           0 :   pari_sp av = avma;
    1722             :   GEN z, kx;
    1723             :   long d;
    1724           0 :   if (ZXX_is_ZX(x)) return ZXn_sqr(x, n);
    1725           0 :   d = get_FpX_degree(T);
    1726           0 :   kx= ZXX_to_Kronecker(x, d);
    1727           0 :   z = Kronecker_to_FpXQX(ZXn_sqr(kx, (2*d-1)*n), T, p);
    1728           0 :   return gerepileupto(av, z);
    1729             : }
    1730             : 
    1731             : INLINE GEN
    1732           0 : FpXXn_red(GEN a, long n)
    1733           0 : { return RgXn_red_shallow(a, n); }
    1734             : 
    1735             : GEN
    1736           0 : FpXQXn_exp(GEN h, long e, GEN T, GEN p)
    1737             : {
    1738           0 :   pari_sp av = avma, av2;
    1739           0 :   long v = varn(h), n=1;
    1740           0 :   GEN f = pol_1(v), g = pol_1(v);
    1741           0 :   ulong mask = quadratic_prec_mask(e);
    1742           0 :   av2 = avma;
    1743           0 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    1744           0 :     pari_err_DOMAIN("FpXQXn_exp","valuation", "<", gen_1, h);
    1745           0 :   for (;mask>1;)
    1746             :   {
    1747             :     GEN q, w;
    1748           0 :     long n2 = n;
    1749           0 :     n<<=1; if (mask & 1) n--;
    1750           0 :     mask >>= 1;
    1751           0 :     g = FpXX_sub(FpXX_mulu(g,2,p), FpXQXn_mul(f, FpXQXn_sqr(g, n2, T, p), n2, T, p), p);
    1752           0 :     q = FpXX_deriv(FpXXn_red(h,n2), p);
    1753           0 :     w = FpXX_add(q, FpXQXn_mul(g, FpXX_sub(FpXX_deriv(f, p), FpXQXn_mul(f,q,n-1, T, p), p),n-1, T, p), p);
    1754           0 :     f = FpXX_add(f, FpXQXn_mul(f, FpXX_sub(FpXXn_red(h, n), FpXX_integ(w, p), p), n, T, p), p);
    1755           0 :     if (gc_needed(av2,2))
    1756             :     {
    1757           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQXn_exp, e = %ld", n);
    1758           0 :       gerepileall(av2, 2, &f, &g);
    1759             :     }
    1760             :   }
    1761           0 :   return gerepileupto(av, f);
    1762             : }
    1763             : 
    1764             : /* (f*g) \/ x^n */
    1765             : static GEN
    1766           0 : FpXQX_mulhigh_i(GEN f, GEN g, long n, GEN T, GEN p)
    1767             : {
    1768           0 :   return RgX_shift_shallow(FpXQX_mul(f,g,T, p),-n);
    1769             : }
    1770             : 
    1771             : static GEN
    1772           0 : FpXQXn_mulhigh(GEN f, GEN g, long n2, long n, GEN T, GEN p)
    1773             : {
    1774           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    1775           0 :   return FpXX_add(FpXQX_mulhigh_i(fl, g, n2, T, p), FpXQXn_mul(fh, g, n - n2, T, p), p);
    1776             : }
    1777             : 
    1778             : GEN
    1779           0 : FpXQXn_inv(GEN f, long e, GEN T, GEN p)
    1780             : {
    1781           0 :   pari_sp av = avma, av2;
    1782             :   ulong mask;
    1783             :   GEN W, a;
    1784           0 :   long v = varn(f), n = 1;
    1785             : 
    1786           0 :   if (!signe(f)) pari_err_INV("FpXXn_inv",f);
    1787           0 :   a = Fq_inv(gel(f,2), T, p);
    1788           0 :   if (e == 1) return scalarpol(a, v);
    1789           0 :   else if (e == 2)
    1790             :   {
    1791             :     GEN b;
    1792           0 :     if (degpol(f) <= 0) return scalarpol(a, v);
    1793           0 :     b = Fq_neg(gel(f,3),T,p);
    1794           0 :     if (signe(b)==0) return scalarpol(a, v);
    1795           0 :     if (!is_pm1(a)) b = Fq_mul(b, Fq_sqr(a, T, p), T, p);
    1796           0 :     W = deg1pol_shallow(b, a, v);
    1797           0 :     return gerepilecopy(av, W);
    1798             :   }
    1799           0 :   W = scalarpol_shallow(Fq_inv(gel(f,2), T, p),v);
    1800           0 :   mask = quadratic_prec_mask(e);
    1801           0 :   av2 = avma;
    1802           0 :   for (;mask>1;)
    1803             :   {
    1804             :     GEN u, fr;
    1805           0 :     long n2 = n;
    1806           0 :     n<<=1; if (mask & 1) n--;
    1807           0 :     mask >>= 1;
    1808           0 :     fr = FpXXn_red(f, n);
    1809           0 :     u = FpXQXn_mul(W, FpXQXn_mulhigh(fr, W, n2, n, T, p), n-n2, T, p);
    1810           0 :     W = FpXX_sub(W, RgX_shift_shallow(u, n2), p);
    1811           0 :     if (gc_needed(av2,2))
    1812             :     {
    1813           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FpXQXn_inv, e = %ld", n);
    1814           0 :       W = gerepileupto(av2, W);
    1815             :     }
    1816             :   }
    1817           0 :   return gerepileupto(av, W);
    1818             : }

Generated by: LCOV version 1.13