Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - FpXX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20459-9710128) Lines: 716 826 86.7 %
Date: 2017-04-28 05:33:48 Functions: 93 101 92.1 %
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      182898 : to_FlxqX(GEN P, GEN Q, GEN T, GEN p, GEN *pt_P, GEN *pt_Q, GEN *pt_T)
      28             : {
      29      182898 :   ulong pp = uel(p,2);
      30      182898 :   long v = get_FpX_var(T);
      31      182898 :   *pt_P = ZXX_to_FlxX(P, pp, v);
      32      182898 :   if (pt_Q) *pt_Q = ZXX_to_FlxX(Q, pp, v);
      33      182898 :   *pt_T = ZXT_to_FlxT(T, pp);
      34      182898 :   return pp;
      35             : }
      36             : 
      37             : static GEN
      38          66 : ZXX_copy(GEN a) { return gcopy(a); }
      39             : 
      40             : GEN
      41       27870 : FpXX_red(GEN z, GEN p)
      42             : {
      43             :   GEN res;
      44       27870 :   long i, l = lg(z);
      45       27870 :   res = cgetg(l,t_POL); res[1] = z[1];
      46      180984 :   for (i=2; i<l; i++)
      47             :   {
      48      153114 :     GEN zi = gel(z,i), c;
      49      153114 :     if (typ(zi)==t_INT)
      50        1542 :       c = modii(zi,p);
      51             :     else
      52             :     {
      53      151572 :       pari_sp av = avma;
      54      151572 :       c = FpX_red(zi,p);
      55      151572 :       switch(lg(c)) {
      56          12 :         case 2: avma = av; c = gen_0; break;
      57       13734 :         case 3: c = gerepilecopy(av, gel(c,2)); break;
      58             :       }
      59             :     }
      60      153114 :     gel(res,i) = c;
      61             :   }
      62       27870 :   return FpXX_renormalize(res,lg(res));
      63             : }
      64             : GEN
      65      338916 : FpXX_add(GEN x, GEN y, GEN p)
      66             : {
      67             :   long i,lz;
      68             :   GEN z;
      69      338916 :   long lx=lg(x);
      70      338916 :   long ly=lg(y);
      71      338916 :   if (ly>lx) swapspec(x,y, lx,ly);
      72      338916 :   lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
      73      338916 :   for (i=2; i<ly; i++) gel(z,i) = Fq_add(gel(x,i), gel(y,i), NULL, p);
      74      338916 :   for (   ; i<lx; i++) gel(z,i) = gcopy(gel(x,i));
      75      338916 :   return FpXX_renormalize(z, lz);
      76             : }
      77             : GEN
      78       10722 : FpXX_sub(GEN x, GEN y, GEN p)
      79             : {
      80             :   long i,lz;
      81             :   GEN z;
      82       10722 :   long lx=lg(x);
      83       10722 :   long ly=lg(y);
      84       10722 :   if (ly <= lx)
      85             :   {
      86        9489 :     lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
      87        9489 :     for (i=2; i<ly; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
      88        9489 :     for (   ; i<lx; i++) gel(z,i) = gcopy(gel(x,i));
      89             :   }
      90             :   else
      91             :   {
      92        1233 :     lz = ly; z = cgetg(lz, t_POL); z[1]=x[1];
      93        1233 :     for (i=2; i<lx; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
      94        1233 :     for (   ; i<ly; i++) gel(z,i) = Fq_neg(gel(y,i), NULL, p);
      95             :   }
      96       10722 :   return FpXX_renormalize(z, lz);
      97             : }
      98             : 
      99             : static GEN
     100       39282 : FpXX_subspec(GEN x, GEN y, GEN p, long nx, long ny)
     101             : {
     102             :   long i,lz;
     103             :   GEN z;
     104       39282 :   if (ny <= nx)
     105             :   {
     106       39282 :     lz = nx+2; z = cgetg(lz, t_POL)+2;
     107       39282 :     for (i=0; i<ny; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
     108       39282 :     for (   ; i<nx; i++) gel(z,i) = gcopy(gel(x,i));
     109             :   }
     110             :   else
     111             :   {
     112           0 :     lz = ny+2; z = cgetg(lz, t_POL)+2;
     113           0 :     for (i=0; i<nx; i++) gel(z,i) = Fq_sub(gel(x,i), gel(y,i), NULL, p);
     114           0 :     for (   ; i<ny; i++) gel(z,i) = Fq_neg(gel(y,i), NULL, p);
     115             :   }
     116       39282 :   return FpXX_renormalize(z-2, lz);
     117             : }
     118             : 
     119             : GEN
     120         390 : FpXX_neg(GEN x, GEN p)
     121             : {
     122         390 :   long i, lx = lg(x);
     123         390 :   GEN y = cgetg(lx,t_POL);
     124         390 :   y[1] = x[1];
     125         390 :   for(i=2; i<lx; i++) gel(y,i) = Fq_neg(gel(x,i), NULL, p);
     126         390 :   return FpXX_renormalize(y, lx);
     127             : }
     128             : 
     129             : GEN
     130       55014 : FpXX_Fp_mul(GEN P, GEN u, GEN p)
     131             : {
     132             :   long i, lP;
     133       55014 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     134      405306 :   for(i=2; i<lP; i++)
     135             :   {
     136      350292 :     GEN x = gel(P,i);
     137      350292 :     gel(res,i) = typ(x)==t_INT? Fp_mul(x,u,p): FpX_Fp_mul(x,u,p);
     138             :   }
     139       55014 :   return FpXX_renormalize(res,lP);
     140             : }
     141             : 
     142             : GEN
     143       44484 : FpXX_mulu(GEN P, ulong u, GEN p)
     144             : {
     145             :   long i, lP;
     146       44484 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     147      237162 :   for(i=2; i<lP; i++)
     148             :   {
     149      192678 :     GEN x = gel(P,i);
     150      192678 :     gel(res,i) = typ(x)==t_INT? Fp_mulu(x,u,p): FpX_mulu(x,u,p);
     151             :   }
     152       44484 :   return FpXX_renormalize(res,lP);
     153             : }
     154             : 
     155             : GEN
     156      230298 : FpXX_deriv(GEN P, GEN p)
     157             : {
     158      230298 :   long i, l = lg(P)-1;
     159             :   GEN res;
     160             : 
     161      230298 :   if (l < 3) return pol_0(varn(P));
     162      226116 :   res = cgetg(l, t_POL);
     163      226116 :   res[1] = P[1];
     164      820944 :   for (i=2; i<l ; i++)
     165             :   {
     166      594828 :     GEN x = gel(P,i+1);
     167      594828 :     gel(res,i) = typ(x)==t_INT? Fp_mulu(x,i-1,p): FpX_mulu(x,i-1,p);
     168             :   }
     169      226116 :   return FpXX_renormalize(res, l);
     170             : }
     171             : 
     172             : /*******************************************************************/
     173             : /*                                                                 */
     174             : /*                             (Fp[X]/(Q))[Y]                      */
     175             : /*                                                                 */
     176             : /*******************************************************************/
     177             : 
     178             : static GEN
     179      320484 : get_FpXQX_red(GEN T, GEN *B)
     180             : {
     181      320484 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
     182       48528 :   *B = gel(T,1); return gel(T,2);
     183             : }
     184             : 
     185             : GEN
     186      122094 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
     187             : 
     188             : long
     189       43566 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
     190             : 
     191             : long
     192        1212 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
     193             : 
     194             : GEN
     195           0 : random_FpXQX(long d1, long v, GEN T, GEN p)
     196             : {
     197           0 :   long dT = get_FpX_degree(T), vT = get_FpX_var(T);
     198           0 :   long i, d = d1+2;
     199           0 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     200           0 :   for (i=2; i<d; i++) gel(y,i) = random_FpX(dT, vT, p);
     201           0 :   return FpXQX_renormalize(y,d);
     202             : }
     203             : 
     204             : /*Not stack clean*/
     205             : GEN
     206      440385 : Kronecker_to_FpXQX(GEN Z, GEN T, GEN p)
     207             : {
     208      440385 :   long i,j,lx,l, N = (get_FpX_degree(T)<<1) + 1;
     209      440385 :   GEN x, t = cgetg(N,t_POL), z = FpX_red(Z, p);
     210      440385 :   t[1] = evalvarn(get_FpX_var(T));
     211      440385 :   l = lg(z); lx = (l-2) / (N-2);
     212      440385 :   x = cgetg(lx+3,t_POL);
     213      440385 :   x[1] = z[1];
     214    11664348 :   for (i=2; i<lx+2; i++)
     215             :   {
     216    11223963 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     217    11223963 :     z += (N-2);
     218    11223963 :     gel(x,i) = FpX_rem(FpX_renormalize(t,N), T,p);
     219             :   }
     220      440385 :   N = (l-2) % (N-2) + 2;
     221      440385 :   for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     222      440385 :   gel(x,i) = FpX_rem(FpX_renormalize(t,N), T,p);
     223      440385 :   return FpXQX_renormalize(x, i+1);
     224             : }
     225             : 
     226             : /* shallow, n = deg(T) */
     227             : GEN
     228       82998 : Kronecker_to_ZXX(GEN z, long n, long v)
     229             : {
     230       82998 :   long i,j,lx,l, N = (n<<1)+1;
     231             :   GEN x, t;
     232       82998 :   l = lg(z); lx = (l-2) / (N-2);
     233       82998 :   x = cgetg(lx+3,t_POL);
     234       82998 :   x[1] = z[1];
     235     1438236 :   for (i=2; i<lx+2; i++)
     236             :   {
     237     1355238 :     t = cgetg(N,t_POL); t[1] = evalvarn(v);
     238     1355238 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     239     1355238 :     z += (N-2);
     240     1355238 :     gel(x,i) = ZX_renormalize(t,N);
     241             :   }
     242       82998 :   N = (l-2) % (N-2) + 2;
     243       82998 :   t = cgetg(N,t_POL); t[1] = evalvarn(v);
     244       82998 :   for (j=2; j<N; j++) gel(t,j) = gel(z,j);
     245       82998 :   gel(x,i) = ZX_renormalize(t,N);
     246       82998 :   return ZXX_renormalize(x, i+1);
     247             : }
     248             : /* shallow */
     249             : GEN
     250      131328 : ZXX_mul_Kronecker(GEN x, GEN y, long n)
     251      131328 : { return ZX_mul(ZXX_to_Kronecker(x,n), ZXX_to_Kronecker(y,n)); }
     252             : 
     253             : GEN
     254        1770 : ZXX_sqr_Kronecker(GEN x, long n)
     255        1770 : { return ZX_sqr(ZXX_to_Kronecker(x,n)); }
     256             : 
     257             : GEN
     258      144126 : FpXQX_red(GEN z, GEN T, GEN p)
     259             : {
     260      144126 :   long i, l = lg(z);
     261      144126 :   GEN res = cgetg(l,t_POL); res[1] = z[1];
     262     1418133 :   for(i=2;i<l;i++)
     263     1274007 :     if (typ(gel(z,i)) == t_INT)
     264       89436 :       gel(res,i) = modii(gel(z,i),p);
     265             :     else
     266     1184571 :       gel(res,i) = FpXQ_red(gel(z,i),T,p);
     267      144126 :   return FpXQX_renormalize(res,l);
     268             : }
     269             : 
     270             : static int
     271      785454 : ZXX_is_ZX_spec(GEN a,long na)
     272             : {
     273             :   long i;
     274      924939 :   for(i=0;i<na;i++)
     275      913344 :     if(typ(gel(a,i))!=t_INT) return 0;
     276       11595 :   return 1;
     277             : }
     278             : 
     279             : static int
     280      113454 : ZXX_is_ZX(GEN a) { return ZXX_is_ZX_spec(a+2,lgpol(a)); }
     281             : 
     282             : static GEN
     283       30075 : FpXX_FpX_mulspec(GEN P, GEN U, GEN p, long v, long lU)
     284             : {
     285       30075 :   long i, lP =lg(P);
     286             :   GEN res;
     287       30075 :   res = cgetg(lP, t_POL); res[1] = P[1];
     288     1274235 :   for(i=2; i<lP; i++)
     289             :   {
     290     1244160 :     GEN Pi = gel(P,i);
     291     2483532 :     gel(res,i) = typ(Pi)==t_INT? FpX_Fp_mulspec(U, Pi, p, lU):
     292     1239372 :                                  FpX_mulspec(U, Pi+2, p, lU, lgpol(Pi));
     293     1244160 :     setvarn(gel(res,i),v);
     294             :   }
     295       30075 :   return FpXQX_renormalize(res,lP);
     296             : }
     297             : 
     298             : GEN
     299       24750 : FpXX_FpX_mul(GEN P, GEN U, GEN p)
     300       24750 : { return FpXX_FpX_mulspec(P,U+2,p,varn(U),lgpol(U)); }
     301             : 
     302             : static GEN
     303        5325 : FpXY_FpY_mulspec(GEN x, GEN y, GEN T, GEN p, long lx, long ly)
     304             : {
     305        5325 :   pari_sp av = avma;
     306        5325 :   long v = fetch_var();
     307        5325 :   GEN z = RgXY_swapspec(x,get_FpX_degree(T)-1,v,lx);
     308        5325 :   z = FpXX_FpX_mulspec(z,y,p,v,ly);
     309        5325 :   z = RgXY_swapspec(z+2,lx+ly+3,get_FpX_var(T),lgpol(z));
     310        5325 :   (void)delete_var(); return gerepilecopy(av,z);
     311             : }
     312             : 
     313             : static GEN
     314      336000 : FpXQX_mulspec(GEN x, GEN y, GEN T, GEN p, long lx, long ly)
     315             : {
     316      336000 :   pari_sp av = avma;
     317             :   GEN z, kx, ky;
     318             :   long n;
     319      336000 :   if (ZXX_is_ZX_spec(y,ly))
     320             :   {
     321        5202 :     if (ZXX_is_ZX_spec(x,lx))
     322        2526 :       return FpX_mulspec(x,y,p,lx,ly);
     323             :     else
     324        2676 :       return FpXY_FpY_mulspec(x,y,T,p,lx,ly);
     325      330798 :   } else if (ZXX_is_ZX_spec(x,lx))
     326        2649 :       return FpXY_FpY_mulspec(y,x,T,p,ly,lx);
     327      328149 :   n = get_FpX_degree(T);
     328      328149 :   kx = ZXX_to_Kronecker_spec(x, lx, n);
     329      328149 :   ky = ZXX_to_Kronecker_spec(y, ly, n);
     330      328149 :   z = Kronecker_to_FpXQX(ZX_mul(ky,kx), T, p);
     331      328149 :   return gerepileupto(av, z);
     332             : }
     333             : 
     334             : GEN
     335      255456 : FpXQX_mul(GEN x, GEN y, GEN T, GEN p)
     336             : {
     337      255456 :   GEN z = FpXQX_mulspec(x+2,y+2,T,p,lgpol(x),lgpol(y));
     338      255456 :   setvarn(z,varn(x)); return z;
     339             : }
     340             : 
     341             : GEN
     342      113454 : FpXQX_sqr(GEN x, GEN T, GEN p)
     343             : {
     344      113454 :   pari_sp av = avma;
     345             :   GEN z, kx;
     346      113454 :   if (ZXX_is_ZX(x)) return ZX_sqr(x);
     347      112236 :   kx= ZXX_to_Kronecker(x, get_FpX_degree(T));
     348      112236 :   z = Kronecker_to_FpXQX(ZX_sqr(kx), T, p);
     349      112236 :   return gerepileupto(av, z);
     350             : }
     351             : 
     352             : GEN
     353      130575 : FpXQX_FpXQ_mul(GEN P, GEN U, GEN T, GEN p)
     354             : {
     355             :   long i, lP;
     356             :   GEN res;
     357      130575 :   res = cgetg_copy(P, &lP); res[1] = P[1];
     358      582822 :   for(i=2; i<lP; i++)
     359      802296 :     gel(res,i) = typ(gel(P,i))==t_INT? FpX_Fp_mul(U, gel(P,i), p):
     360      350049 :                                        FpXQ_mul(U, gel(P,i), T,p);
     361      130575 :   return FpXQX_renormalize(res,lP);
     362             : }
     363             : 
     364             : /* x and y in Z[Y][X]. Assume T irreducible mod p */
     365             : static GEN
     366       99744 : FpXQX_divrem_basecase(GEN x, GEN y, GEN T, GEN p, GEN *pr)
     367             : {
     368             :   long vx, dx, dy, dy1, dz, i, j, sx, lr;
     369             :   pari_sp av0, av, tetpil;
     370             :   GEN z,p1,rem,lead;
     371             : 
     372       99744 :   if (!signe(y)) pari_err_INV("FpX_divrem",y);
     373       99744 :   vx=varn(x); dy=degpol(y); dx=degpol(x);
     374       99744 :   if (dx < dy)
     375             :   {
     376           0 :     if (pr)
     377             :     {
     378           0 :       av0 = avma; x = FpXQX_red(x, T, p);
     379           0 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: pol_0(vx); }
     380           0 :       if (pr == ONLY_REM) return x;
     381           0 :       *pr = x;
     382             :     }
     383           0 :     return pol_0(vx);
     384             :   }
     385       99744 :   lead = leading_coeff(y);
     386       99744 :   if (!dy) /* y is constant */
     387             :   {
     388         276 :     if (pr && pr != ONLY_DIVIDES)
     389             :     {
     390         228 :       if (pr == ONLY_REM) return pol_0(vx);
     391           3 :       *pr = pol_0(vx);
     392             :     }
     393          51 :     if (gequal1(lead)) return FpXQX_red(x,T,p);
     394          39 :     av0 = avma; x = FqX_Fq_mul(x, Fq_inv(lead, T,p), T,p);
     395          39 :     return gerepileupto(av0,x);
     396             :   }
     397       99468 :   av0 = avma; dz = dx-dy;
     398       99468 :   lead = gequal1(lead)? NULL: gclone(Fq_inv(lead,T,p));
     399       99468 :   avma = av0;
     400       99468 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
     401       99468 :   x += 2; y += 2; z += 2;
     402       99468 :   for (dy1=dy-1; dy1>=0 && !signe(gel(y, dy1)); dy1--);
     403             : 
     404       99468 :   p1 = gel(x,dx); av = avma;
     405       99468 :   gel(z,dz) = lead? gerepileupto(av, Fq_mul(p1,lead, T, p)): gcopy(p1);
     406      325128 :   for (i=dx-1; i>=dy; i--)
     407             :   {
     408      225660 :     av=avma; p1=gel(x,i);
     409      776004 :     for (j=i-dy1; j<=i && j<=dz; j++)
     410      550344 :       p1 = Fq_sub(p1, Fq_mul(gel(z,j),gel(y,i-j),NULL,p),NULL,p);
     411      225660 :     if (lead) p1 = Fq_mul(p1, lead, NULL,p);
     412      225660 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Fq_red(p1,T,p));
     413             :   }
     414       99468 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
     415             : 
     416       98694 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     417      106170 :   for (sx=0; ; i--)
     418             :   {
     419      106170 :     p1 = gel(x,i);
     420      437034 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     421      330864 :       p1 = Fq_sub(p1, Fq_mul(gel(z,j),gel(y,i-j),NULL,p),NULL,p);
     422      106170 :     tetpil=avma; p1 = Fq_red(p1, T, p); if (signe(p1)) { sx = 1; break; }
     423        8364 :     if (!i) break;
     424        7476 :     avma=av;
     425        7476 :   }
     426       98694 :   if (pr == ONLY_DIVIDES)
     427             :   {
     428           0 :     if (lead) gunclone(lead);
     429           0 :     if (sx) { avma=av0; return NULL; }
     430           0 :     avma = (pari_sp)rem; return z-2;
     431             :   }
     432       98694 :   lr=i+3; rem -= lr;
     433       98694 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     434       98694 :   rem[1] = z[-1];
     435       98694 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
     436       98694 :   rem += 2; gel(rem,i) = p1;
     437      695862 :   for (i--; i>=0; i--)
     438             :   {
     439      597168 :     av=avma; p1 = gel(x,i);
     440     1958916 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     441     1361748 :       p1 = Fq_sub(p1, Fq_mul(gel(z,j),gel(y,i-j), NULL,p), NULL,p);
     442      597168 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Fq_red(p1, T, p));
     443             :   }
     444       98694 :   rem -= 2;
     445       98694 :   if (lead) gunclone(lead);
     446       98694 :   if (!sx) (void)FpXQX_renormalize(rem, lr);
     447       98694 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     448        5604 :   *pr = rem; return z-2;
     449             : }
     450             : 
     451             : static GEN
     452          51 : FpXQX_halfgcd_basecase(GEN a, GEN b, GEN T, GEN p)
     453             : {
     454          51 :   pari_sp av=avma;
     455             :   GEN u,u1,v,v1;
     456          51 :   long vx = varn(a);
     457          51 :   long n = lgpol(a)>>1;
     458          51 :   u1 = v = pol_0(vx);
     459          51 :   u = v1 = pol_1(vx);
     460         651 :   while (lgpol(b)>n)
     461             :   {
     462         549 :     GEN r, q = FpXQX_divrem(a,b, T, p, &r);
     463         549 :     a = b; b = r; swap(u,u1); swap(v,v1);
     464         549 :     u1 = FpXX_sub(u1, FpXQX_mul(u, q, T, p), p);
     465         549 :     v1 = FpXX_sub(v1, FpXQX_mul(v, q ,T, p), p);
     466         549 :     if (gc_needed(av,2))
     467             :     {
     468           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQX_halfgcd (d = %ld)",degpol(b));
     469           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
     470             :     }
     471             :   }
     472          51 :   return gerepilecopy(av, mkmat2(mkcol2(u,u1), mkcol2(v,v1)));
     473             : }
     474             : static GEN
     475         102 : FpXQX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN T, GEN p)
     476             : {
     477         102 :   return FpXX_add(FpXQX_mul(u, x, T, p),FpXQX_mul(v, y, T, p), p);
     478             : }
     479             : 
     480             : static GEN
     481          51 : FpXQXM_FpXQX_mul2(GEN M, GEN x, GEN y, GEN T, GEN p)
     482             : {
     483          51 :   GEN res = cgetg(3, t_COL);
     484          51 :   gel(res, 1) = FpXQX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, T, p);
     485          51 :   gel(res, 2) = FpXQX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, T, p);
     486          51 :   return res;
     487             : }
     488             : 
     489             : static GEN
     490          42 : FpXQXM_mul2(GEN A, GEN B, GEN T, GEN p)
     491             : {
     492          42 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
     493          42 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
     494          42 :   GEN M1 = FpXQX_mul(FpXX_add(A11,A22, p), FpXX_add(B11,B22, p), T, p);
     495          42 :   GEN M2 = FpXQX_mul(FpXX_add(A21,A22, p), B11, T, p);
     496          42 :   GEN M3 = FpXQX_mul(A11, FpXX_sub(B12,B22, p), T, p);
     497          42 :   GEN M4 = FpXQX_mul(A22, FpXX_sub(B21,B11, p), T, p);
     498          42 :   GEN M5 = FpXQX_mul(FpXX_add(A11,A12, p), B22, T, p);
     499          42 :   GEN M6 = FpXQX_mul(FpXX_sub(A21,A11, p), FpXX_add(B11,B12, p), T, p);
     500          42 :   GEN M7 = FpXQX_mul(FpXX_sub(A12,A22, p), FpXX_add(B21,B22, p), T, p);
     501          42 :   GEN T1 = FpXX_add(M1,M4, p), T2 = FpXX_sub(M7,M5, p);
     502          42 :   GEN T3 = FpXX_sub(M1,M2, p), T4 = FpXX_add(M3,M6, p);
     503          42 :   retmkmat2(mkcol2(FpXX_add(T1,T2, p), FpXX_add(M2,M4, p)),
     504             :             mkcol2(FpXX_add(M3,M5, p), FpXX_add(T3,T4, p)));
     505             : }
     506             : /* Return [0,1;1,-q]*M */
     507             : static GEN
     508          42 : FpXQX_FpXQXM_qmul(GEN q, GEN M, GEN T, GEN p)
     509             : {
     510          42 :   GEN u, v, res = cgetg(3, t_MAT);
     511          42 :   u = FpXX_sub(gcoeff(M,1,1), FpXQX_mul(gcoeff(M,2,1), q, T, p), p);
     512          42 :   gel(res,1) = mkcol2(gcoeff(M,2,1), u);
     513          42 :   v = FpXX_sub(gcoeff(M,1,2), FpXQX_mul(gcoeff(M,2,2), q, T, p), p);
     514          42 :   gel(res,2) = mkcol2(gcoeff(M,2,2), v);
     515          42 :   return res;
     516             : }
     517             : 
     518             : static GEN
     519           0 : matid2_FpXQXM(long v)
     520             : {
     521           0 :   retmkmat2(mkcol2(pol_1(v),pol_0(v)),
     522             :             mkcol2(pol_0(v),pol_1(v)));
     523             : }
     524             : 
     525             : static GEN
     526          42 : FpXQX_halfgcd_split(GEN x, GEN y, GEN T, GEN p)
     527             : {
     528          42 :   pari_sp av=avma;
     529             :   GEN R, S, V;
     530             :   GEN y1, r, q;
     531          42 :   long l = lgpol(x), n = l>>1, k;
     532          42 :   if (lgpol(y)<=n) return matid2_FpXQXM(varn(x));
     533          42 :   R = FpXQX_halfgcd(RgX_shift_shallow(x,-n),RgX_shift_shallow(y,-n), T, p);
     534          42 :   V = FpXQXM_FpXQX_mul2(R,x,y, T, p); y1 = gel(V,2);
     535          42 :   if (lgpol(y1)<=n) return gerepilecopy(av, R);
     536          42 :   q = FpXQX_divrem(gel(V,1), y1, T, p, &r);
     537          42 :   k = 2*n-degpol(y1);
     538          42 :   S = FpXQX_halfgcd(RgX_shift_shallow(y1,-k), RgX_shift_shallow(r,-k), T, p);
     539          42 :   return gerepileupto(av, FpXQXM_mul2(S,FpXQX_FpXQXM_qmul(q,R, T, p), T, p));
     540             : }
     541             : 
     542             : /* Return M in GL_2(Fp[X]) such that:
     543             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
     544             : */
     545             : 
     546             : static GEN
     547          93 : FpXQX_halfgcd_i(GEN x, GEN y, GEN T, GEN p)
     548             : {
     549          93 :   if (lg(x)<=FpXQX_HALFGCD_LIMIT) return FpXQX_halfgcd_basecase(x, y, T, p);
     550          42 :   return FpXQX_halfgcd_split(x, y, T, p);
     551             : }
     552             : 
     553             : GEN
     554          93 : FpXQX_halfgcd(GEN x, GEN y, GEN T, GEN p)
     555             : {
     556          93 :   pari_sp av = avma;
     557             :   GEN M,q,r;
     558          93 :   if (lgefint(p)==3)
     559             :   {
     560           0 :     ulong pp = to_FlxqX(x, y, T, p, &x, &y, &T);
     561           0 :     M = FlxXM_to_ZXXM(FlxqX_halfgcd(x, y, T, pp));
     562             :   }
     563             :   else
     564             :   {
     565          93 :     if (!signe(x))
     566             :     {
     567           0 :       long v = varn(x);
     568           0 :       retmkmat2(mkcol2(pol_0(v),pol_1(v)),
     569             :                 mkcol2(pol_1(v),pol_0(v)));
     570             :     }
     571          93 :     if (degpol(y)<degpol(x)) return FpXQX_halfgcd_i(x, y, T, p);
     572           6 :     q = FpXQX_divrem(y, x, T, p, &r);
     573           6 :     M = FpXQX_halfgcd_i(x, r, T, p);
     574           6 :     gcoeff(M,1,1) = FpXX_sub(gcoeff(M,1,1), FpXQX_mul(q, gcoeff(M,1,2), T, p), p);
     575           6 :     gcoeff(M,2,1) = FpXX_sub(gcoeff(M,2,1), FpXQX_mul(q, gcoeff(M,2,2), T, p), p);
     576             :   }
     577           6 :   return gerepilecopy(av, M);
     578             : }
     579             : 
     580             : static GEN
     581        1092 : FpXQX_gcd_basecase(GEN a, GEN b, GEN T, GEN p)
     582             : {
     583        1092 :   pari_sp av = avma, av0=avma;
     584       13287 :   while (signe(b))
     585             :   {
     586             :     GEN c;
     587       11103 :     if (gc_needed(av0,2))
     588             :     {
     589           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQX_gcd (d = %ld)",degpol(b));
     590           0 :       gerepileall(av0,2, &a,&b);
     591             :     }
     592       11103 :     av = avma; c = FpXQX_rem(a, b, T, p); a=b; b=c;
     593             :   }
     594        1092 :   avma = av; return a;
     595             : }
     596             : 
     597             : GEN
     598        8460 : FpXQX_gcd(GEN x, GEN y, GEN T, GEN p)
     599             : {
     600        8460 :   pari_sp av = avma;
     601        8460 :   if (lgefint(p) == 3)
     602             :   {
     603             :     GEN Pl, Ql, Tl, U;
     604        7320 :     ulong pp = to_FlxqX(x, y, T, p, &Pl, &Ql, &Tl);
     605        7320 :     U  = FlxqX_gcd(Pl, Ql, Tl, pp);
     606        7320 :     return gerepileupto(av, FlxX_to_ZXX(U));
     607             :   }
     608        1140 :   x = FpXQX_red(x, T, p);
     609        1140 :   y = FpXQX_red(y, T, p);
     610        1140 :   if (!signe(x)) return gerepileupto(av, y);
     611        2193 :   while (lg(y)>FpXQX_GCD_LIMIT)
     612             :   {
     613             :     GEN c;
     614           9 :     if (lgpol(y)<=(lgpol(x)>>1))
     615             :     {
     616           0 :       GEN r = FpXQX_rem(x, y, T, p);
     617           0 :       x = y; y = r;
     618             :     }
     619           9 :     c = FpXQXM_FpXQX_mul2(FpXQX_halfgcd(x,y, T, p), x, y, T, p);
     620           9 :     x = gel(c,1); y = gel(c,2);
     621           9 :     gerepileall(av,2,&x,&y);
     622             :   }
     623        1092 :   return gerepileupto(av, FpXQX_gcd_basecase(x, y, T, p));
     624             : }
     625             : 
     626             : static GEN
     627           0 : FpXQX_extgcd_basecase(GEN a, GEN b, GEN T, GEN p, GEN *ptu, GEN *ptv)
     628             : {
     629           0 :   pari_sp av=avma;
     630             :   GEN u,v,d,d1,v1;
     631           0 :   long vx = varn(a);
     632           0 :   d = a; d1 = b;
     633           0 :   v = pol_0(vx); v1 = pol_1(vx);
     634           0 :   while (signe(d1))
     635             :   {
     636           0 :     GEN r, q = FpXQX_divrem(d, d1, T, p, &r);
     637           0 :     v = FpXX_sub(v,FpXQX_mul(q,v1,T, p),p);
     638           0 :     u=v; v=v1; v1=u;
     639           0 :     u=r; d=d1; d1=u;
     640           0 :     if (gc_needed(av,2))
     641             :     {
     642           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpXQX_extgcd (d = %ld)",degpol(d));
     643           0 :       gerepileall(av,5, &d,&d1,&u,&v,&v1);
     644             :     }
     645             :   }
     646           0 :   if (ptu) *ptu = FpXQX_div(FpXX_sub(d,FpXQX_mul(b,v, T, p), p), a, T, p);
     647           0 :   *ptv = v; return d;
     648             : }
     649             : 
     650             : static GEN
     651           0 : FpXQX_extgcd_halfgcd(GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv)
     652             : {
     653           0 :   pari_sp av=avma;
     654           0 :   GEN u,v,R = matid2_FpXQXM(varn(x));
     655           0 :   while (lg(y)>FpXQX_EXTGCD_LIMIT)
     656             :   {
     657             :     GEN M, c;
     658           0 :     if (lgpol(y)<=(lgpol(x)>>1))
     659             :     {
     660           0 :       GEN r, q = FpXQX_divrem(x, y, T, p, &r);
     661           0 :       x = y; y = r;
     662           0 :       R = FpXQX_FpXQXM_qmul(q, R, T, p);
     663             :     }
     664           0 :     M = FpXQX_halfgcd(x,y, T, p);
     665           0 :     c = FpXQXM_FpXQX_mul2(M, x,y, T, p);
     666           0 :     R = FpXQXM_mul2(M, R, T, p);
     667           0 :     x = gel(c,1); y = gel(c,2);
     668           0 :     gerepileall(av,3,&x,&y,&R);
     669             :   }
     670           0 :   y = FpXQX_extgcd_basecase(x,y, T, p, &u,&v);
     671           0 :   if (ptu) *ptu = FpXQX_addmulmul(u,v,gcoeff(R,1,1),gcoeff(R,2,1), T, p);
     672           0 :   *ptv = FpXQX_addmulmul(u,v,gcoeff(R,1,2),gcoeff(R,2,2), T, p);
     673           0 :   return y;
     674             : }
     675             : 
     676             : /* x and y in Z[Y][X], return lift(gcd(x mod T,p, y mod T,p)). Set u and v st
     677             :  * ux + vy = gcd (mod T,p) */
     678             : GEN
     679        4962 : FpXQX_extgcd(GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv)
     680             : {
     681             :   GEN d;
     682        4962 :   pari_sp ltop=avma;
     683        4962 :   if (lgefint(p) == 3)
     684             :   {
     685             :     GEN Pl, Ql, Tl, Dl;
     686        4962 :     ulong pp = to_FlxqX(x, y, T, p, &Pl, &Ql, &Tl);
     687        4962 :     Dl = FlxqX_extgcd(Pl, Ql, Tl, pp, ptu, ptv);
     688        4962 :     if (ptu) *ptu = FlxX_to_ZXX(*ptu);
     689        4962 :     *ptv = FlxX_to_ZXX(*ptv);
     690        4962 :     d = FlxX_to_ZXX(Dl);
     691             :   }
     692             :   else
     693             :   {
     694           0 :     x = FpXQX_red(x, T, p);
     695           0 :     y = FpXQX_red(y, T, p);
     696           0 :     if (lg(y)>FpXQX_EXTGCD_LIMIT)
     697           0 :       d = FpXQX_extgcd_halfgcd(x, y, T, p, ptu, ptv);
     698             :     else
     699           0 :       d = FpXQX_extgcd_basecase(x, y, T, p, ptu, ptv);
     700             :   }
     701        4962 :   gerepileall(ltop,ptu?3:2,&d,ptv,ptu);
     702        4962 :   return d;
     703             : }
     704             : 
     705             : /***********************************************************************/
     706             : /**                                                                   **/
     707             : /**                       Barrett reduction                           **/
     708             : /**                                                                   **/
     709             : /***********************************************************************/
     710             : 
     711             : /* Return new lgpol */
     712             : static long
     713       81534 : ZXX_lgrenormalizespec(GEN x, long lx)
     714             : {
     715             :   long i;
     716       81954 :   for (i = lx-1; i>=0; i--)
     717       81954 :     if (signe(gel(x,i))) break;
     718       81534 :   return i+1;
     719             : }
     720             : 
     721             : static GEN
     722         774 : FpXQX_invBarrett_basecase(GEN S, GEN T, GEN p)
     723             : {
     724         774 :   long i, l=lg(S)-1, lr = l-1, k;
     725         774 :   GEN r=cgetg(lr, t_POL); r[1]=S[1];
     726         774 :   gel(r,2) = gen_1;
     727       14280 :   for (i=3; i<lr; i++)
     728             :   {
     729       13506 :     pari_sp av = avma;
     730       13506 :     GEN u = gel(S,l-i+2);
     731      144390 :     for (k=3; k<i; k++)
     732      130884 :       u = Fq_add(u, Fq_mul(gel(S,l-i+k), gel(r,k), NULL, p), NULL, p);
     733       13506 :     gel(r,i) = gerepileupto(av, Fq_red(Fq_neg(u, NULL, p), T, p));
     734             :   }
     735         774 :   return FpXQX_renormalize(r,lr);
     736             : }
     737             : 
     738             : INLINE GEN
     739       78564 : FpXQX_recipspec(GEN x, long l, long n)
     740             : {
     741       78564 :   return RgX_recipspec_shallow(x, l, n);
     742             : }
     743             : 
     744             : static GEN
     745         156 : FpXQX_invBarrett_Newton(GEN S, GEN T, GEN p)
     746             : {
     747         156 :   pari_sp av = avma;
     748         156 :   long nold, lx, lz, lq, l = degpol(S), i, lQ;
     749         156 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
     750         156 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
     751         156 :   for (i=0;i<l;i++) gel(x,i) = gen_0;
     752         156 :   q = RgX_recipspec_shallow(S+2,l+1,l+1); lQ = lgpol(q); q+=2;
     753             :   /* We work on _spec_ FpX's, all the l[xzq] below are lgpol's */
     754             : 
     755             :   /* initialize */
     756         156 :   gel(x,0) = Fq_inv(gel(q,0), T, p);
     757         156 :   if (lQ>1) gel(q,1) = Fq_red(gel(q,1), T, p);
     758         156 :   if (lQ>1 && signe(gel(q,1)))
     759         156 :   {
     760         156 :     GEN u = gel(q, 1);
     761         156 :     if (!gequal1(gel(x,0))) u = Fq_mul(u, Fq_sqr(gel(x,0), T, p), T, p);
     762         156 :     gel(x,1) = Fq_neg(u, T, p); lx = 2;
     763             :   }
     764             :   else
     765           0 :     lx = 1;
     766         156 :   nold = 1;
     767        1302 :   for (; mask > 1; )
     768             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
     769         990 :     long i, lnew, nnew = nold << 1;
     770             : 
     771         990 :     if (mask & 1) nnew--;
     772         990 :     mask >>= 1;
     773             : 
     774         990 :     lnew = nnew + 1;
     775         990 :     lq = ZXX_lgrenormalizespec(q, minss(lQ,lnew));
     776         990 :     z = FpXQX_mulspec(x, q, T, p, lx, lq); /* FIXME: high product */
     777         990 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
     778         990 :     z += 2;
     779             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
     780         990 :     for (i = nold; i < lz; i++) if (signe(gel(z,i))) break;
     781         990 :     nold = nnew;
     782         990 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
     783             : 
     784             :     /* z + i represents (x*q - 1) / t^i */
     785         990 :     lz = ZXX_lgrenormalizespec (z+i, lz-i);
     786         990 :     z = FpXQX_mulspec(x, z+i, T, p, lx, lz); /* FIXME: low product */
     787         990 :     lz = lgpol(z); z += 2;
     788         990 :     if (lz > lnew-i) lz = ZXX_lgrenormalizespec(z, lnew-i);
     789             : 
     790         990 :     lx = lz+ i;
     791         990 :     y  = x + i; /* x -= z * t^i, in place */
     792         990 :     for (i = 0; i < lz; i++) gel(y,i) = Fq_neg(gel(z,i), T, p);
     793             :   }
     794         156 :   x -= 2; setlg(x, lx + 2); x[1] = S[1];
     795         156 :   return gerepilecopy(av, x);
     796             : }
     797             : 
     798             : /* 1/polrecip(S)+O(x^(deg(S)-1)) */
     799             : GEN
     800         948 : FpXQX_invBarrett(GEN S, GEN T, GEN p)
     801             : {
     802         948 :   pari_sp ltop = avma;
     803         948 :   long l = lg(S);
     804             :   GEN r;
     805         948 :   if (l<5) return pol_0(varn(S));
     806         930 :   if (l<=FpXQX_INVBARRETT_LIMIT)
     807             :   {
     808         774 :     GEN c = gel(S,l-1), ci=gen_1;
     809         774 :     if (!gequal1(c))
     810             :     {
     811           0 :       ci = Fq_inv(c, T, p);
     812           0 :       S = FqX_Fq_mul(S, ci, T, p);
     813           0 :       r = FpXQX_invBarrett_basecase(S, T, p);
     814           0 :       r = FqX_Fq_mul(r, ci, T, p);
     815             :     } else
     816         774 :       r = FpXQX_invBarrett_basecase(S, T, p);
     817             :   }
     818             :   else
     819         156 :     r = FpXQX_invBarrett_Newton(S, T, p);
     820         930 :   return gerepileupto(ltop, r);
     821             : }
     822             : 
     823             : GEN
     824        6924 : FpXQX_get_red(GEN S, GEN T, GEN p)
     825             : {
     826        6924 :   if (typ(S)==t_POL && lg(S)>FpXQX_BARRETT_LIMIT)
     827         588 :     retmkvec2(FpXQX_invBarrett(S,T,p),S);
     828        6336 :   return S;
     829             : }
     830             : 
     831             : /* Compute x mod S where 2 <= degpol(S) <= l+1 <= 2*(degpol(S)-1)
     832             :  * and mg is the Barrett inverse of S. */
     833             : static GEN
     834       39282 : FpXQX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN S, GEN T, GEN p, GEN *pr)
     835             : {
     836             :   GEN q, r;
     837       39282 :   long lt = degpol(S); /*We discard the leading term*/
     838             :   long ld, lm, lT, lmg;
     839       39282 :   ld = l-lt;
     840       39282 :   lm = minss(ld, lgpol(mg));
     841       39282 :   lT  = ZXX_lgrenormalizespec(S+2,lt);
     842       39282 :   lmg = ZXX_lgrenormalizespec(mg+2,lm);
     843       39282 :   q = FpXQX_recipspec(x+lt,ld,ld);                 /* q = rec(x)     lq<=ld*/
     844       39282 :   q = FpXQX_mulspec(q+2,mg+2,T,p,lgpol(q),lmg);    /* q = rec(x) * mg lq<=ld+lm*/
     845       39282 :   q = FpXQX_recipspec(q+2,minss(ld,lgpol(q)),ld);  /* q = rec (rec(x) * mg) lq<=ld*/
     846       39282 :   if (!pr) return q;
     847       39282 :   r = FpXQX_mulspec(q+2,S+2,T,p,lgpol(q),lT);      /* r = q*pol        lr<=ld+lt*/
     848       39282 :   r = FpXX_subspec(x,r+2,p,lt,minss(lt,lgpol(r))); /* r = x - r   lr<=lt */
     849       39282 :   if (pr == ONLY_REM) return r;
     850       39282 :   *pr = r; return q;
     851             : }
     852             : 
     853             : static GEN
     854       38856 : FpXQX_divrem_Barrett_noGC(GEN x, GEN mg, GEN S, GEN T, GEN p, GEN *pr)
     855             : {
     856       38856 :   long l = lgpol(x), lt = degpol(S), lm = 2*lt-1;
     857       38856 :   GEN q = NULL, r;
     858             :   long i;
     859       38856 :   if (l <= lt)
     860             :   {
     861           0 :     if (pr == ONLY_REM) return ZXX_copy(x);
     862           0 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
     863           0 :     if (pr) *pr =  ZXX_copy(x);
     864           0 :     return pol_0(varn(x));
     865             :   }
     866       38856 :   if (lt <= 1)
     867          18 :     return FpXQX_divrem_basecase(x,S,T,p,pr);
     868       38838 :   if (pr != ONLY_REM && l>lm)
     869             :   {
     870         330 :     q = cgetg(l-lt+2, t_POL);
     871         330 :     for (i=0;i<l-lt;i++) gel(q+2,i) = gen_0;
     872             :   }
     873       38838 :   r = l>lm ? shallowcopy(x): x;
     874       78186 :   while (l>lm)
     875             :   {
     876         510 :     GEN zr, zq = FpXQX_divrem_Barrettspec(r+2+l-lm,lm,mg,S,T,p,&zr);
     877         510 :     long lz = lgpol(zr);
     878         510 :     if (pr != ONLY_REM)
     879             :     {
     880         438 :       long lq = lgpol(zq);
     881         438 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
     882             :     }
     883         510 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
     884         510 :     l = l-lm+lz;
     885             :   }
     886       38838 :   if (pr != ONLY_REM)
     887             :   {
     888         342 :     if (l > lt)
     889             :     {
     890         276 :       GEN zq = FpXQX_divrem_Barrettspec(r+2,l,mg,S,T,p,&r);
     891         276 :       if (!q) q = zq;
     892             :       else
     893             :       {
     894         264 :         long lq = lgpol(zq);
     895         264 :         for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
     896             :       }
     897             :     }
     898             :     else
     899          66 :     { setlg(r, l+2); r = ZXX_copy(r); }
     900             :   }
     901             :   else
     902             :   {
     903       38496 :     if (l > lt)
     904       38496 :       (void) FpXQX_divrem_Barrettspec(r+2,l,mg,S,T,p,&r);
     905             :     else
     906           0 :     { setlg(r, l+2); r = ZXX_copy(r); }
     907       38496 :     r[1] = x[1]; return FpXQX_renormalize(r, lg(r));
     908             :   }
     909         342 :   if (pr) { r[1] = x[1]; r = FpXQX_renormalize(r, lg(r)); }
     910         342 :   q[1] = x[1]; q = FpXQX_renormalize(q, lg(q));
     911         342 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
     912         342 :   if (pr) *pr = r;
     913         342 :   return q;
     914             : }
     915             : 
     916             : GEN
     917      169011 : FpXQX_divrem(GEN x, GEN S, GEN T, GEN p, GEN *pr)
     918             : {
     919      169011 :   GEN B, y = get_FpXQX_red(S, &B);
     920      169011 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
     921      169011 :   if (pr==ONLY_REM) return FpXQX_rem(x, y, T, p);
     922      169011 :   if (lgefint(p) == 3)
     923             :   {
     924             :     GEN a, b, t, z;
     925      162240 :     pari_sp tetpil, av = avma;
     926      162240 :     ulong pp = to_FlxqX(x, y, T, p, &a, &b, &t);
     927      162240 :     z = FlxqX_divrem(a, b, t, pp, pr);
     928      162240 :     if (pr == ONLY_DIVIDES && !z) { avma = av; return NULL; }
     929      162162 :     tetpil=avma;
     930      162162 :     z = FlxX_to_ZXX(z);
     931      162162 :     if (pr && pr != ONLY_DIVIDES && pr != ONLY_REM)
     932      161244 :       *pr = FlxX_to_ZXX(*pr);
     933         918 :     else return gerepile(av, tetpil, z);
     934      161244 :     gerepileallsp(av,tetpil,2, pr, &z);
     935      161244 :     return z;
     936             :   }
     937        6771 :   if (!B && d+3 < FpXQX_DIVREM_BARRETT_LIMIT)
     938        6414 :     return FpXQX_divrem_basecase(x,y,T,p,pr);
     939             :   else
     940             :   {
     941         357 :     pari_sp av=avma;
     942         357 :     GEN mg = B? B: FpXQX_invBarrett(y, T, p);
     943         357 :     GEN q = FpXQX_divrem_Barrett_noGC(x,mg,y,T,p,pr);
     944         357 :     if (!q) {avma=av; return NULL;}
     945         357 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q);
     946         321 :     gerepileall(av,2,&q,pr);
     947         321 :     return q;
     948             :   }
     949             : }
     950             : 
     951             : GEN
     952      151473 : FpXQX_rem(GEN x, GEN S, GEN T, GEN p)
     953             : {
     954      151473 :   GEN B, y = get_FpXQX_red(S, &B);
     955      151473 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
     956      151473 :   if (d < 0) return FpXQX_red(x, T, p);
     957      140187 :   if (lgefint(p) == 3)
     958             :   {
     959        8376 :     pari_sp av = avma;
     960             :     GEN a, b, t, z;
     961        8376 :     ulong pp = to_FlxqX(x, y, T, p, &a, &b, &t);
     962        8376 :     z = FlxqX_rem(a, b, t, pp);
     963        8376 :     z = FlxX_to_ZXX(z);
     964        8376 :     return gerepileupto(av, z);
     965             :   }
     966      131811 :   if (!B && d+3 < FpXQX_REM_BARRETT_LIMIT)
     967       93312 :     return FpXQX_divrem_basecase(x,y, T, p, ONLY_REM);
     968             :   else
     969             :   {
     970       38499 :     pari_sp av=avma;
     971       38499 :     GEN mg = B? B: FpXQX_invBarrett(y, T, p);
     972       38499 :     GEN r = FpXQX_divrem_Barrett_noGC(x, mg, y, T, p, ONLY_REM);
     973       38499 :     return gerepileupto(av, r);
     974             :   }
     975             : }
     976             : 
     977             : /* x + y*z mod p */
     978             : INLINE GEN
     979       10212 : Fq_addmul(GEN x, GEN y, GEN z, GEN T, GEN p)
     980             : {
     981             :   pari_sp av;
     982       10212 :   if (!signe(y) || !signe(z)) return Fq_red(x, T, p);
     983       10098 :   if (!signe(x)) return Fq_mul(z,y, T, p);
     984       10086 :   av = avma;
     985       10086 :   return gerepileupto(av, Fq_add(x, Fq_mul(y, z, T, p), T, p));
     986             : }
     987             : 
     988             : GEN
     989        5106 : FpXQX_div_by_X_x(GEN a, GEN x, GEN T, GEN p, GEN *r)
     990             : {
     991        5106 :   long l = lg(a)-1, i;
     992        5106 :   GEN z = cgetg(l, t_POL);
     993        5106 :   z[1] = evalsigne(1) | evalvarn(0);
     994        5106 :   gel(z, l-1) = gel(a,l);
     995       15318 :   for (i=l-2; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
     996       10212 :     gel(z, i) = Fq_addmul(gel(a,i+1), x, gel(z,i+1), T, p);
     997        5106 :   if (r) *r = Fq_addmul(gel(a,2), x, gel(z,2), T, p);
     998        5106 :   return z;
     999             : }
    1000             : 
    1001             : struct _FpXQXQ {
    1002             :   GEN T, S;
    1003             :   GEN p;
    1004             : };
    1005             : 
    1006        2178 : static GEN _FpXQX_mul(void *data, GEN a,GEN b)
    1007             : {
    1008        2178 :   struct _FpXQXQ *d=(struct _FpXQXQ*)data;
    1009        2178 :   return FpXQX_mul(a,b,d->T,d->p);
    1010             : }
    1011             : 
    1012        1836 : static GEN _FpXQX_sqr(void *data, GEN a)
    1013             : {
    1014        1836 :   struct _FpXQXQ *d=(struct _FpXQXQ*)data;
    1015        1836 :   return FpXQX_sqr(a, d->T, d->p);
    1016             : }
    1017             : 
    1018             : GEN
    1019         156 : FpXQX_powu(GEN x, ulong n, GEN T, GEN p)
    1020             : {
    1021             :   struct _FpXQXQ D;
    1022         156 :   if (n==0) return pol_1(varn(x));
    1023         156 :   D.T = T; D.p = p;
    1024         156 :   return gen_powu(x, n, (void *)&D, _FpXQX_sqr, _FpXQX_mul);
    1025             : }
    1026             : 
    1027             : GEN
    1028          54 : FpXQXV_prod(GEN V, GEN T, GEN p)
    1029             : {
    1030          54 :   if (lgefint(p) == 3)
    1031             :   {
    1032          12 :     pari_sp av = avma;
    1033          12 :     ulong pp = p[2];
    1034          12 :     GEN Tl = ZXT_to_FlxT(T, pp);
    1035          12 :     GEN Vl = ZXXV_to_FlxXV(V, pp, get_FpX_var(T));
    1036          12 :     Tl = FlxqXV_prod(Vl, Tl, pp);
    1037          12 :     return gerepileupto(av, FlxX_to_ZXX(Tl));
    1038             :   }
    1039             :   else
    1040             :   {
    1041             :     struct _FpXQXQ d;
    1042          42 :     d.T=T; d.p=p;
    1043          42 :     return gen_product(V, (void*)&d, &_FpXQX_mul);
    1044             :   }
    1045             : }
    1046             : 
    1047             : static GEN
    1048        8490 : _FpXQX_divrem(void * E, GEN x, GEN y, GEN *r)
    1049             : {
    1050        8490 :   struct _FpXQXQ *d = (struct _FpXQXQ *) E;
    1051        8490 :   return FpXQX_divrem(x, y, d->T, d->p, r);
    1052             : }
    1053             : 
    1054             : static GEN
    1055       23568 : _FpXQX_add(void * E, GEN x, GEN y)
    1056             : {
    1057       23568 :   struct _FpXQXQ *d = (struct _FpXQXQ *) E;
    1058       23568 :   return FpXX_add(x, y, d->p);
    1059             : }
    1060             : 
    1061             : static GEN
    1062        3504 : _FpXQX_sub(void * E, GEN x, GEN y) {
    1063        3504 :   struct _FpXQXQ *d = (struct _FpXQXQ*) E;
    1064        3504 :   return FpXX_sub(x,y, d->p);
    1065             : }
    1066             : 
    1067             : static struct bb_ring FpXQX_ring = { _FpXQX_add, _FpXQX_mul, _FpXQX_sqr };
    1068             : 
    1069             : GEN
    1070         504 : FpXQX_digits(GEN x, GEN B, GEN T, GEN p)
    1071             : {
    1072         504 :   pari_sp av = avma;
    1073         504 :   long d = degpol(B), n = (lgpol(x)+d-1)/d;
    1074             :   GEN z;
    1075             :   struct _FpXQXQ D;
    1076         504 :   D.T = T; D.p = p;
    1077         504 :   z = gen_digits(x, B, n, (void *)&D, &FpXQX_ring, _FpXQX_divrem);
    1078         504 :   return gerepileupto(av, z);
    1079             : }
    1080             : 
    1081             : GEN
    1082         162 : FpXQXV_FpXQX_fromdigits(GEN x, GEN B, GEN T, GEN p)
    1083             : {
    1084         162 :   pari_sp av = avma;
    1085             :   struct _FpXQXQ D;
    1086             :   GEN z;
    1087         162 :   D.T = T; D.p = p;
    1088         162 :   z = gen_fromdigits(x,B,(void *)&D, &FpXQX_ring);
    1089         162 :   return gerepileupto(av, z);
    1090             : }
    1091             : 
    1092             : /* Q an FpXY (t_POL with FpX coeffs), evaluate at X = x */
    1093             : GEN
    1094       43014 : FpXY_evalx(GEN Q, GEN x, GEN p)
    1095             : {
    1096       43014 :   long i, lb = lg(Q);
    1097             :   GEN z;
    1098       43014 :   z = cgetg(lb, t_POL); z[1] = Q[1];
    1099      372798 :   for (i=2; i<lb; i++)
    1100             :   {
    1101      329784 :     GEN q = gel(Q,i);
    1102      329784 :     gel(z,i) = typ(q) == t_INT? modii(q,p): FpX_eval(q, x, p);
    1103             :   }
    1104       43014 :   return FpX_renormalize(z, lb);
    1105             : }
    1106             : /* Q an FpXY, evaluate at Y = y */
    1107             : GEN
    1108       14304 : FpXY_evaly(GEN Q, GEN y, GEN p, long vx)
    1109             : {
    1110       14304 :   pari_sp av = avma;
    1111       14304 :   long i, lb = lg(Q);
    1112             :   GEN z;
    1113       14304 :   if (!signe(Q)) return pol_0(vx);
    1114       14304 :   if (lb == 3 || !signe(y)) {
    1115          18 :     z = gel(Q, 2);
    1116          18 :     return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1117             :   }
    1118       14286 :   z = gel(Q, lb-1);
    1119       14286 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1120       14286 :   for (i=lb-2; i>=2; i--) z = Fq_add(gel(Q,i), FpX_Fp_mul(z, y, p), NULL, p);
    1121       14286 :   return gerepileupto(av, z);
    1122             : }
    1123             : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
    1124             : GEN
    1125       10626 : FpXY_eval(GEN Q, GEN y, GEN x, GEN p)
    1126             : {
    1127       10626 :   pari_sp av = avma;
    1128       10626 :   return gerepileuptoint(av, FpX_eval(FpXY_evalx(Q, x, p), y, p));
    1129             : }
    1130             : 
    1131             : GEN
    1132        2310 : FpXY_FpXQV_evalx(GEN P, GEN x, GEN T, GEN p)
    1133             : {
    1134        2310 :   long i, lP = lg(P);
    1135        2310 :   GEN res = cgetg(lP,t_POL);
    1136        2310 :   res[1] = P[1];
    1137       27714 :   for(i=2; i<lP; i++)
    1138       50490 :     gel(res,i) = typ(gel(P,i))==t_INT? icopy(gel(P,i)):
    1139       25086 :                                        FpX_FpXQV_eval(gel(P,i), x, T, p);
    1140        2310 :   return FlxX_renormalize(res, lP);
    1141             : }
    1142             : 
    1143             : GEN
    1144         126 : FpXY_FpXQ_evalx(GEN P, GEN x, GEN T, GEN p)
    1145             : {
    1146         126 :   pari_sp av = avma;
    1147         126 :   long n = brent_kung_optpow(get_FpX_degree(T)-1,lgpol(P),1);
    1148         126 :   GEN xp = FpXQ_powers(x, n, T, p);
    1149         126 :   return gerepileupto(av, FpXY_FpXQV_evalx(P, xp, T, p));
    1150             : }
    1151             : 
    1152             : /*******************************************************************/
    1153             : /*                                                                 */
    1154             : /*                       (Fp[X]/T(X))[Y] / S(Y)                    */
    1155             : /*                                                                 */
    1156             : /*******************************************************************/
    1157             : 
    1158             : /*Preliminary implementation to speed up FpX_ffisom*/
    1159             : typedef struct {
    1160             :   GEN S, T, p;
    1161             : } FpXYQQ_muldata;
    1162             : 
    1163             : /* reduce x in Fp[X, Y] in the algebra Fp[X,Y]/ (S(X),T(Y)) */
    1164             : static GEN
    1165          96 : FpXYQQ_redswap(GEN x, GEN S, GEN T, GEN p)
    1166             : {
    1167          96 :   pari_sp ltop=avma;
    1168          96 :   long n = get_FpX_degree(S);
    1169          96 :   long m = get_FpX_degree(T);
    1170          96 :   long v = get_FpX_var(T);
    1171          96 :   GEN V = RgXY_swap(x,m,v);
    1172          96 :   V = FpXQX_red(V,S,p);
    1173          96 :   V = RgXY_swap(V,n,v);
    1174          96 :   return gerepilecopy(ltop,V);
    1175             : }
    1176             : static GEN
    1177          60 : FpXYQQ_sqr(void *data, GEN x)
    1178             : {
    1179          60 :   FpXYQQ_muldata *D = (FpXYQQ_muldata*)data;
    1180          60 :   return FpXYQQ_redswap(FpXQX_sqr(x, D->T, D->p),D->S,D->T,D->p);
    1181             : 
    1182             : }
    1183             : static GEN
    1184          36 : FpXYQQ_mul(void *data, GEN x, GEN y)
    1185             : {
    1186          36 :   FpXYQQ_muldata *D = (FpXYQQ_muldata*)data;
    1187          36 :   return FpXYQQ_redswap(FpXQX_mul(x,y, D->T, D->p),D->S,D->T,D->p);
    1188             : }
    1189             : 
    1190             : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
    1191             : GEN
    1192          24 : FpXYQQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    1193             : {
    1194          24 :   pari_sp av = avma;
    1195             :   FpXYQQ_muldata D;
    1196             :   GEN y;
    1197          24 :   if (lgefint(p) == 3)
    1198             :   {
    1199           0 :     ulong pp = to_FlxqX(x, NULL, T, p, &x, NULL, &T);
    1200           0 :     S = ZX_to_Flx(S, pp);
    1201           0 :     y = FlxX_to_ZXX( FlxYqq_pow(x, n, S, T, pp) );
    1202             :   }
    1203             :   else
    1204             :   {
    1205          24 :     D.S = S;
    1206          24 :     D.T = T;
    1207          24 :     D.p = p;
    1208          24 :     y = gen_pow(x, n, (void*)&D, &FpXYQQ_sqr, &FpXYQQ_mul);
    1209             :   }
    1210          24 :   return gerepileupto(av, y);
    1211             : }
    1212             : 
    1213             : GEN
    1214       26796 : FpXQXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p) {
    1215       26796 :   return FpXQX_rem(FpXQX_mul(x, y, T, p), S, T, p);
    1216             : }
    1217             : 
    1218             : GEN
    1219      111156 : FpXQXQ_sqr(GEN x, GEN S, GEN T, GEN p) {
    1220      111156 :   return FpXQX_rem(FpXQX_sqr(x, T, p), S, T, p);
    1221             : }
    1222             : 
    1223             : /* Inverse of x in Z/pZ[X]/(pol) or NULL if inverse doesn't exist
    1224             :  * return lift(1 / (x mod (p,pol))) */
    1225             : GEN
    1226          18 : FpXQXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
    1227             : {
    1228          18 :   GEN V, z = FpXQX_extgcd(get_FpXQX_mod(S), x, T, p, NULL, &V);
    1229          18 :   if (degpol(z)) return NULL;
    1230          18 :   z = gel(z,2);
    1231          18 :   z = typ(z)==t_INT ? Fp_invsafe(z,p) : FpXQ_invsafe(z,T,p);
    1232          18 :   if (!z) return NULL;
    1233          18 :   return typ(z)==t_INT ? FpXX_Fp_mul(V, z, p): FpXQX_FpXQ_mul(V, z, T, p);
    1234             : }
    1235             : 
    1236             : GEN
    1237          18 : FpXQXQ_inv(GEN x, GEN S, GEN T,GEN p)
    1238             : {
    1239          18 :   pari_sp av = avma;
    1240          18 :   GEN U = FpXQXQ_invsafe(x, S, T, p);
    1241          18 :   if (!U) pari_err_INV("FpXQXQ_inv",x);
    1242          18 :   return gerepileupto(av, U);
    1243             : }
    1244             : 
    1245             : GEN
    1246           0 : FpXQXQ_div(GEN x,GEN y,GEN S, GEN T,GEN p)
    1247             : {
    1248           0 :   pari_sp av = avma;
    1249           0 :   return gerepileupto(av, FpXQXQ_mul(x, FpXQXQ_inv(y,S,T,p),S,T,p));
    1250             : }
    1251             : 
    1252             : static GEN
    1253       24750 : _FpXQXQ_cmul(void *data, GEN P, long a, GEN x) {
    1254       24750 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1255       24750 :   GEN y = gel(P,a+2);
    1256       49500 :   return typ(y)==t_INT ? FpXX_Fp_mul(x,y, d->p):
    1257       24750 :                          FpXX_FpX_mul(x,y,d->p);
    1258             : }
    1259             : static GEN
    1260        9606 : _FpXQXQ_red(void *data, GEN x) {
    1261        9606 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1262        9606 :   return FpXQX_red(x, d->T, d->p);
    1263             : }
    1264             : static GEN
    1265       25050 : _FpXQXQ_mul(void *data, GEN x, GEN y) {
    1266       25050 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1267       25050 :   return FpXQXQ_mul(x,y, d->S,d->T, d->p);
    1268             : }
    1269             : static GEN
    1270      111156 : _FpXQXQ_sqr(void *data, GEN x) {
    1271      111156 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1272      111156 :   return FpXQXQ_sqr(x, d->S,d->T, d->p);
    1273             : }
    1274             : 
    1275             : static GEN
    1276        7110 : _FpXQXQ_one(void *data) {
    1277        7110 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1278        7110 :   return pol_1(get_FpXQX_var(d->S));
    1279             : }
    1280             : 
    1281             : static GEN
    1282          54 : _FpXQXQ_zero(void *data) {
    1283          54 :   struct _FpXQXQ *d = (struct _FpXQXQ*) data;
    1284          54 :   return pol_0(get_FpXQX_var(d->S));
    1285             : }
    1286             : 
    1287             : static struct bb_algebra FpXQXQ_algebra = { _FpXQXQ_red, _FpXQX_add,
    1288             :        _FpXQX_sub, _FpXQXQ_mul, _FpXQXQ_sqr, _FpXQXQ_one, _FpXQXQ_zero };
    1289             : 
    1290             : const struct bb_algebra *
    1291         420 : get_FpXQXQ_algebra(void **E, GEN S, GEN T, GEN p)
    1292             : {
    1293         420 :   GEN z = new_chunk(sizeof(struct _FpXQXQ));
    1294         420 :   struct _FpXQXQ *e = (struct _FpXQXQ *) z;
    1295         420 :   e->T = FpX_get_red(T, p);
    1296         420 :   e->S = FpXQX_get_red(S, e->T, p);
    1297         420 :   e->p  = p; *E = (void*)e;
    1298         420 :   return &FpXQXQ_algebra;
    1299             : }
    1300             : 
    1301             : static struct bb_algebra FpXQX_algebra = { _FpXQXQ_red, _FpXQX_add,
    1302             :        _FpXQX_sub, _FpXQX_mul, _FpXQX_sqr, _FpXQXQ_one, _FpXQXQ_zero };
    1303             : 
    1304             : const struct bb_algebra *
    1305          36 : get_FpXQX_algebra(void **E, GEN T, GEN p, long v)
    1306             : {
    1307          36 :   GEN z = new_chunk(sizeof(struct _FpXQXQ));
    1308          36 :   struct _FpXQXQ *e = (struct _FpXQXQ *) z;
    1309          36 :   e->T = FpX_get_red(T, p);
    1310          36 :   e->S = pol_x(v);
    1311          36 :   e->p  = p; *E = (void*)e;
    1312          36 :   return &FpXQX_algebra;
    1313             : }
    1314             : 
    1315             : /* x over Fq, return lift(x^n) mod S */
    1316             : GEN
    1317        1134 : FpXQXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    1318             : {
    1319        1134 :   pari_sp ltop = avma;
    1320             :   GEN y;
    1321             :   struct _FpXQXQ D;
    1322        1134 :   long s = signe(n);
    1323        1134 :   if (!s) return pol_1(varn(x));
    1324        1134 :   if (is_pm1(n)) /* +/- 1 */
    1325           0 :     return (s < 0)? FpXQXQ_inv(x,S,T,p): ZXX_copy(x);
    1326        1134 :   if (lgefint(p) == 3)
    1327             :   {
    1328           0 :     ulong pp = to_FlxqX(x, S, T, p, &x, &S, &T);
    1329           0 :     GEN z = FlxqXQ_pow(x, n, S, T, pp);
    1330           0 :     y = FlxX_to_ZXX(z);
    1331             :   }
    1332             :   else
    1333             :   {
    1334        1134 :     T = FpX_get_red(T, p);
    1335        1134 :     S = FpXQX_get_red(S, T, p);
    1336        1134 :     D.S = S; D.T = T; D.p = p;
    1337        1134 :     if (s < 0) x = FpXQXQ_inv(x,S,T,p);
    1338        1134 :     y = gen_pow(x, n, (void*)&D,&_FpXQXQ_sqr,&_FpXQXQ_mul);
    1339             :   }
    1340        1134 :   return gerepileupto(ltop, y);
    1341             : }
    1342             : 
    1343             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    1344             : GEN
    1345        1074 : FpXQXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
    1346             : {
    1347             :   struct _FpXQXQ D;
    1348        1074 :   int use_sqr = 2*degpol(x) >= get_FpXQX_degree(S);
    1349        1074 :   T = FpX_get_red(T, p);
    1350        1074 :   S = FpXQX_get_red(S, T, p);
    1351        1074 :   D.S = S; D.T = T; D.p = p;
    1352        1074 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FpXQXQ_sqr, &_FpXQXQ_mul,&_FpXQXQ_one);
    1353             : }
    1354             : 
    1355             : GEN
    1356          48 : FpXQXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
    1357             : {
    1358          48 :   return RgXV_to_RgM(FpXQXQ_powers(y,m-1,S,T,p),n);
    1359             : }
    1360             : 
    1361             : GEN
    1362        2052 : FpXQX_FpXQXQV_eval(GEN P, GEN V, GEN S, GEN T, GEN p)
    1363             : {
    1364             :   struct _FpXQXQ D;
    1365        2052 :   T = FpX_get_red(T, p);
    1366        2052 :   S = FpXQX_get_red(S, T, p);
    1367        2052 :   D.S=S; D.T=T; D.p=p;
    1368        2052 :   return gen_bkeval_powers(P, degpol(P), V, (void*)&D, &FpXQXQ_algebra,
    1369             :                                                    _FpXQXQ_cmul);
    1370             : }
    1371             : 
    1372             : GEN
    1373         132 : FpXQX_FpXQXQ_eval(GEN Q, GEN x, GEN S, GEN T, GEN p)
    1374             : {
    1375             :   struct _FpXQXQ D;
    1376         132 :   int use_sqr = 2*degpol(x) >= get_FpXQX_degree(S);
    1377         132 :   T = FpX_get_red(T, p);
    1378         132 :   S = FpXQX_get_red(S, T, p);
    1379         132 :   D.S=S; D.T=T; D.p=p;
    1380         132 :   return gen_bkeval(Q, degpol(Q), x, use_sqr, (void*)&D, &FpXQXQ_algebra,
    1381             :       _FpXQXQ_cmul);
    1382             : }
    1383             : 
    1384             : static GEN
    1385         120 : FpXQXQ_autpow_sqr(void * E, GEN x)
    1386             : {
    1387         120 :   struct _FpXQXQ *D = (struct _FpXQXQ *)E;
    1388         120 :   GEN T = D->T, p = D->p;
    1389         120 :   GEN phi = gel(x,1), S = gel(x,2);
    1390         120 :   long n = brent_kung_optpow(get_FpX_degree(T)-1,lgpol(S)+1,1);
    1391         120 :   GEN V = FpXQ_powers(phi, n, T, p);
    1392         120 :   GEN phi2 = FpX_FpXQV_eval(phi, V, T, p);
    1393         120 :   GEN Sphi = FpXY_FpXQV_evalx(S, V, T, p);
    1394         120 :   GEN S2 = FpXQX_FpXQXQ_eval(Sphi, S, D->S, T, p);
    1395         120 :   return mkvec2(phi2, S2);
    1396             : }
    1397             : 
    1398             : static GEN
    1399          12 : FpXQXQ_autpow_mul(void * E, GEN x, GEN y)
    1400             : {
    1401          12 :   struct _FpXQXQ *D = (struct _FpXQXQ *)E;
    1402          12 :   GEN T = D->T, p = D->p;
    1403          12 :   GEN phi1 = gel(x,1), S1 = gel(x,2);
    1404          12 :   GEN phi2 = gel(y,1), S2 = gel(y,2);
    1405          12 :   long n = brent_kung_optpow(get_FpX_degree(T)-1, lgpol(S1)+1, 1);
    1406          12 :   GEN V = FpXQ_powers(phi2, n, T, p);
    1407          12 :   GEN phi3 = FpX_FpXQV_eval(phi1, V, T, p);
    1408          12 :   GEN Sphi = FpXY_FpXQV_evalx(S1, V, T, p);
    1409          12 :   GEN S3 = FpXQX_FpXQXQ_eval(Sphi, S2, D->S, T, p);
    1410          12 :   return mkvec2(phi3, S3);
    1411             : }
    1412             : 
    1413             : GEN
    1414         120 : FpXQXQV_autpow(GEN aut, long n, GEN S, GEN T, GEN p)
    1415             : {
    1416             :   struct _FpXQXQ D;
    1417         120 :   T = FpX_get_red(T, p);
    1418         120 :   S = FpXQX_get_red(S, T, p);
    1419         120 :   D.S=S; D.T=T; D.p=p;
    1420         120 :   return gen_powu(aut,n,&D,FpXQXQ_autpow_sqr,FpXQXQ_autpow_mul);
    1421             : }
    1422             : 
    1423             : static GEN
    1424           0 : FpXQXQ_auttrace_mul(void *E, GEN x, GEN y)
    1425             : {
    1426           0 :   struct _FpXQXQ *D = (struct _FpXQXQ *) E;
    1427           0 :   GEN T = D->T, p = D->p;
    1428           0 :   GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
    1429           0 :   GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
    1430           0 :   long n2 = brent_kung_optpow(get_FpX_degree(T)-1, lgpol(S1)+lgpol(a1)+1, 1);
    1431           0 :   GEN V2 = FpXQ_powers(phi2, n2, T, p);
    1432           0 :   GEN phi3 = FpX_FpXQV_eval(phi1, V2, T, p);
    1433           0 :   GEN Sphi = FpXY_FpXQV_evalx(S1, V2, T, p);
    1434           0 :   GEN aphi = FpXY_FpXQV_evalx(a1, V2, T, p);
    1435           0 :   long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
    1436           0 :   GEN V = FpXQXQ_powers(S2, n, D->S, T, p);
    1437           0 :   GEN S3 = FpXQX_FpXQXQV_eval(Sphi, V, D->S, T, p);
    1438           0 :   GEN aS = FpXQX_FpXQXQV_eval(aphi, V, D->S, T, p);
    1439           0 :   GEN a3 = FpXX_add(aS, a2, p);
    1440           0 :   return mkvec3(phi3, S3, a3);
    1441             : }
    1442             : 
    1443             : static GEN
    1444           0 : FpXQXQ_auttrace_sqr(void *E, GEN x)
    1445           0 : { return FpXQXQ_auttrace_mul(E, x, x); }
    1446             : 
    1447             : GEN
    1448           0 : FpXQXQV_auttrace(GEN aut, long n, GEN S, GEN T, GEN p)
    1449             : {
    1450             :   struct _FpXQXQ D;
    1451           0 :   T = FpX_get_red(T, p);
    1452           0 :   S = FpXQX_get_red(S, T, p);
    1453           0 :   D.S=S; D.T=T; D.p=p;
    1454           0 :   return gen_powu(aut,n,&D,FpXQXQ_auttrace_sqr,FpXQXQ_auttrace_mul);
    1455             : }
    1456             : 
    1457             : static GEN
    1458        1026 : FpXQXQ_autsum_mul(void *E, GEN x, GEN y)
    1459             : {
    1460        1026 :   struct _FpXQXQ *D = (struct _FpXQXQ *) E;
    1461        1026 :   GEN T = D->T, p = D->p;
    1462        1026 :   GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
    1463        1026 :   GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
    1464        1026 :   long n2 = brent_kung_optpow(get_FpX_degree(T)-1, lgpol(S1)+lgpol(a1)+1, 1);
    1465        1026 :   GEN V2 = FpXQ_powers(phi2, n2, T, p);
    1466        1026 :   GEN phi3 = FpX_FpXQV_eval(phi1, V2, T, p);
    1467        1026 :   GEN Sphi = FpXY_FpXQV_evalx(S1, V2, T, p);
    1468        1026 :   GEN aphi = FpXY_FpXQV_evalx(a1, V2, T, p);
    1469        1026 :   long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
    1470        1026 :   GEN V = FpXQXQ_powers(S2, n, D->S, T, p);
    1471        1026 :   GEN S3 = FpXQX_FpXQXQV_eval(Sphi, V, D->S, T, p);
    1472        1026 :   GEN aS = FpXQX_FpXQXQV_eval(aphi, V, D->S, T, p);
    1473        1026 :   GEN a3 = FpXQXQ_mul(aS,a2,D->S, T, p);
    1474        1026 :   return mkvec3(phi3, S3, a3);
    1475             : }
    1476             : 
    1477             : static GEN
    1478         960 : FpXQXQ_autsum_sqr(void * T, GEN x)
    1479         960 : { return FpXQXQ_autsum_mul(T,x,x); }
    1480             : 
    1481             : GEN
    1482         960 : FpXQXQV_autsum(GEN aut, long n, GEN S, GEN T, GEN p)
    1483             : {
    1484             :   struct _FpXQXQ D;
    1485         960 :   T = FpX_get_red(T, p);
    1486         960 :   S = FpXQX_get_red(S, T, p);
    1487         960 :   D.S=S; D.T=T; D.p=p;
    1488         960 :   return gen_powu(aut,n,&D,FpXQXQ_autsum_sqr,FpXQXQ_autsum_mul);
    1489             : }

Generated by: LCOV version 1.11