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

Generated by: LCOV version 1.11