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

Generated by: LCOV version 1.13