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

Generated by: LCOV version 1.13