Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - FlxX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.2 lcov report (development 29115-f22e516b23) Lines: 1195 1455 82.1 %
Date: 2024-05-18 08:06:58 Functions: 149 194 76.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2019  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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : /***********************************************************************/
      19             : /**                                                                   **/
      20             : /**                               FlxX                                **/
      21             : /**                                                                   **/
      22             : /***********************************************************************/
      23             : 
      24             : /* FlxX are t_POL with Flx coefficients.
      25             :  * Normally the variable ordering should be respected.*/
      26             : 
      27             : /*Similar to normalizepol, in place*/
      28             : /*FlxX_renormalize=zxX_renormalize */
      29             : GEN
      30    15007869 : FlxX_renormalize(GEN /*in place*/ x, long lx)
      31             : {
      32             :   long i;
      33    19815648 :   for (i = lx-1; i>1; i--)
      34    19037500 :     if (lgpol(gel(x,i))) break;
      35    15007910 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
      36    15008555 :   setlg(x, i+1); setsigne(x, i!=1); return x;
      37             : }
      38             : 
      39             : GEN
      40      760432 : pol1_FlxX(long v, long sv)
      41             : {
      42      760432 :   GEN z = cgetg(3, t_POL);
      43      760431 :   z[1] = evalsigne(1) | evalvarn(v);
      44      760431 :   gel(z,2) = pol1_Flx(sv); return z;
      45             : }
      46             : 
      47             : GEN
      48      690442 : polx_FlxX(long v, long sv)
      49             : {
      50      690442 :   GEN z = cgetg(4, t_POL);
      51      690442 :   z[1] = evalsigne(1) | evalvarn(v);
      52      690442 :   gel(z,2) = pol0_Flx(sv);
      53      690442 :   gel(z,3) = pol1_Flx(sv); return z;
      54             : }
      55             : 
      56             : long
      57     2870654 : FlxY_degreex(GEN b)
      58             : {
      59     2870654 :   long deg = 0, i;
      60     2870654 :   if (!signe(b)) return -1;
      61    10348049 :   for (i = 2; i < lg(b); ++i)
      62     7477287 :     deg = maxss(deg, degpol(gel(b, i)));
      63     2870762 :   return deg;
      64             : }
      65             : 
      66             : /*Lift coefficient of B to constant Flx, to give a FlxY*/
      67             : GEN
      68        2472 : Fly_to_FlxY(GEN B, long sv)
      69             : {
      70        2472 :   long lb=lg(B);
      71             :   long i;
      72        2472 :   GEN b=cgetg(lb,t_POL);
      73        2473 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
      74       64251 :   for (i=2; i<lb; i++)
      75       61783 :     gel(b,i) = Fl_to_Flx(B[i], sv);
      76        2468 :   return FlxX_renormalize(b, lb);
      77             : }
      78             : 
      79             : GEN
      80     2666815 : zxX_to_FlxX(GEN B, ulong p)
      81             : {
      82     2666815 :   long i, lb = lg(B);
      83     2666815 :   GEN b = cgetg(lb,t_POL);
      84     8694301 :   for (i=2; i<lb; i++)
      85     6033718 :     gel(b,i) = zx_to_Flx(gel(B,i), p);
      86     2660583 :   b[1] = B[1]; return FlxX_renormalize(b, lb);
      87             : }
      88             : 
      89             : GEN
      90     2767990 : FlxX_to_ZXX(GEN B)
      91             : {
      92     2767990 :   long i, lb = lg(B);
      93     2767990 :   GEN b = cgetg(lb,t_POL);
      94     8881517 :   for (i=2; i<lb; i++)
      95             :   {
      96     6114657 :     GEN c = gel(B,i);
      97     6114657 :     switch(lgpol(c))
      98             :     {
      99      268901 :       case 0:  c = gen_0; break;
     100      796584 :       case 1:  c = utoi(c[2]); break;
     101     5049222 :       default: c = Flx_to_ZX(c); break;
     102             :     }
     103     6113428 :     gel(b,i) = c;
     104             :   }
     105     2766860 :   b[1] = B[1]; return b;
     106             : }
     107             : 
     108             : /* Note: v is used _only_ for the t_INT. It must match
     109             :  * the variable of any t_POL coefficients. */
     110             : GEN
     111     2809543 : ZXX_to_FlxX(GEN B, ulong p, long v)
     112             : {
     113     2809543 :   long lb=lg(B);
     114             :   long i;
     115     2809543 :   GEN b=cgetg(lb,t_POL);
     116     2809581 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
     117    13751722 :   for (i=2; i<lb; i++)
     118    10943237 :     switch (typ(gel(B,i)))
     119             :     {
     120     2136689 :     case t_INT:
     121     2136689 :       gel(b,i) = Z_to_Flx(gel(B,i), p, evalvarn(v));
     122     2136625 :       break;
     123     8807929 :     case t_POL:
     124     8807929 :       gel(b,i) = ZX_to_Flx(gel(B,i), p);
     125     8806897 :       break;
     126             :     }
     127    13750626 :   return FlxX_renormalize(b, lb);
     128             : }
     129             : 
     130             : GEN
     131           0 : ZXXV_to_FlxXV(GEN x, ulong p, long v)
     132           0 : { pari_APPLY_type(t_VEC, ZXX_to_FlxX(gel(x,i), p, v)) }
     133             : 
     134             : GEN
     135         512 : ZXXT_to_FlxXT(GEN x, ulong p, long v)
     136             : {
     137         512 :   if (typ(x) == t_POL)
     138         491 :     return ZXX_to_FlxX(x, p, v);
     139             :   else
     140          63 :     pari_APPLY_type(t_VEC, ZXXT_to_FlxXT(gel(x,i), p, v))
     141             : }
     142             : 
     143             : GEN
     144      323646 : FlxX_to_FlxC(GEN x, long N, long sv)
     145             : {
     146             :   long i, l;
     147             :   GEN z;
     148      323646 :   l = lg(x)-1; x++;
     149      323646 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     150      323646 :   z = cgetg(N+1,t_COL);
     151     2435020 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     152     3477639 :   for (   ; i<=N; i++) gel(z,i) = pol0_Flx(sv);
     153      323646 :   return z;
     154             : }
     155             : 
     156             : /* matrix whose entries are given by the coeffs of the polynomial v in
     157             :  * two variables (considered as degree n polynomials) */
     158             : GEN
     159       54587 : FlxX_to_Flm(GEN v, long n)
     160             : {
     161       54587 :   long j, N = lg(v)-1;
     162       54587 :   GEN y = cgetg(N, t_MAT);
     163       54587 :   v++;
     164      188059 :   for (j=1; j<N; j++) gel(y,j) = Flx_to_Flv(gel(v,j), n);
     165       54587 :   return y;
     166             : }
     167             : 
     168             : GEN
     169      363803 : FlxX_to_Flx(GEN f)
     170             : {
     171      363803 :   long i, l = lg(f);
     172      363803 :   GEN V = cgetg(l, t_VECSMALL);
     173      363802 :   V[1] = ((ulong)f[1])&VARNBITS;
     174     3496276 :   for(i=2; i<l; i++)
     175     3132474 :     V[i] = lgpol(gel(f,i)) ? mael(f,i,2): 0L;
     176      363802 :   return V;
     177             : }
     178             : 
     179             : GEN
     180      219695 : Flm_to_FlxX(GEN x, long v,long w)
     181             : {
     182      219695 :   long j, lx = lg(x);
     183      219695 :   GEN y = cgetg(lx+1, t_POL);
     184      219697 :   y[1]=evalsigne(1) | v;
     185      219697 :   y++;
     186      808740 :   for (j=1; j<lx; j++) gel(y,j) = Flv_to_Flx(gel(x,j), w);
     187      219674 :   return FlxX_renormalize(--y, lx+1);
     188             : }
     189             : 
     190             : /* P(X,Y) --> P(Y,X), n is the degree in Y */
     191             : GEN
     192      181452 : FlxX_swap(GEN x, long n, long ws)
     193             : {
     194      181452 :   long j, lx = lg(x), ly = n+3;
     195      181452 :   GEN y = cgetg(ly, t_POL);
     196      181454 :   y[1] = x[1];
     197     1165639 :   for (j=2; j<ly; j++)
     198             :   {
     199             :     long k;
     200      984194 :     GEN p1 = cgetg(lx, t_VECSMALL);
     201      984182 :     p1[1] = ws;
     202    11195981 :     for (k=2; k<lx; k++)
     203    10211799 :       if (j<lg(gel(x,k)))
     204     7181781 :         p1[k] = mael(x,k,j);
     205             :       else
     206     3030018 :         p1[k] = 0;
     207      984182 :     gel(y,j) = Flx_renormalize(p1,lx);
     208             :   }
     209      181445 :   return FlxX_renormalize(y,ly);
     210             : }
     211             : 
     212             : static GEN
     213     4352820 : zxX_to_Kronecker_spec(GEN P, long lp, long n)
     214             : { /* P(X) = sum Pi(Y) * X^i, return P( Y^(2n-1) ) */
     215     4352820 :   long i, j, k, l, N = (n<<1) + 1;
     216     4352820 :   GEN y = cgetg((N-2)*lp + 2, t_VECSMALL) + 2;
     217    28723160 :   for (k=i=0; i<lp; i++)
     218             :   {
     219    28687018 :     GEN c = gel(P,i);
     220    28687018 :     l = lg(c);
     221    28687018 :     if (l-3 >= n)
     222           0 :       pari_err_BUG("zxX_to_Kronecker, P is not reduced mod Q");
     223   167357844 :     for (j=2; j < l; j++) y[k++] = c[j];
     224    28686991 :     if (i == lp-1) break;
     225   217360423 :     for (   ; j < N; j++) y[k++] = 0;
     226             :   }
     227     4352770 :   y -= 2;
     228     4352770 :   y[1] = 0; setlg(y, k+2); return y;
     229             : }
     230             : 
     231             : GEN
     232     3772138 : zxX_to_Kronecker(GEN P, GEN Q)
     233             : {
     234     3772138 :   GEN z = zxX_to_Kronecker_spec(P+2, lg(P)-2, degpol(Q));
     235     3772147 :   z[1] = P[1]; return z;
     236             : }
     237             : 
     238             : GEN
     239      346184 : FlxX_add(GEN x, GEN y, ulong p)
     240             : {
     241             :   long i,lz;
     242             :   GEN z;
     243      346184 :   long lx=lg(x);
     244      346184 :   long ly=lg(y);
     245      346184 :   if (ly>lx) swapspec(x,y, lx,ly);
     246      346184 :   lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
     247     3902500 :   for (i=2; i<ly; i++) gel(z,i) = Flx_add(gel(x,i), gel(y,i), p);
     248     2136318 :   for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     249      346185 :   return FlxX_renormalize(z, lz);
     250             : }
     251             : 
     252             : GEN
     253         245 : FlxX_Flx_add(GEN y, GEN x, ulong p)
     254             : {
     255         245 :   long i, lz = lg(y);
     256             :   GEN z;
     257         245 :   if (signe(y) == 0) return scalarpol(x, varn(y));
     258         245 :   z = cgetg(lz,t_POL); z[1] = y[1];
     259         245 :   gel(z,2) = Flx_add(gel(y,2), x, p);
     260         245 :   if (lz == 3) z = FlxX_renormalize(z,lz);
     261             :   else
     262        1071 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     263         245 :   return z;
     264             : }
     265             : 
     266             : GEN
     267       11705 : FlxX_Flx_sub(GEN y, GEN x, ulong p)
     268             : {
     269       11705 :   long i, lz = lg(y);
     270             :   GEN z;
     271       11705 :   if (signe(y) == 0) return scalarpol(x, varn(y));
     272       11705 :   z = cgetg(lz,t_POL); z[1] = y[1];
     273       11705 :   gel(z,2) = Flx_sub(gel(y,2), x, p);
     274       11705 :   if (lz == 3) z = FlxX_renormalize(z,lz);
     275             :   else
     276       84309 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     277       11705 :   return z;
     278             : }
     279             : 
     280             : GEN
     281        2636 : FlxX_neg(GEN x, ulong p)
     282             : {
     283        2636 :   long i, lx=lg(x);
     284        2636 :   GEN z = cgetg(lx, t_POL);
     285        2636 :   z[1]=x[1];
     286       45407 :   for (i=2; i<lx; i++) gel(z,i) = Flx_neg(gel(x,i), p);
     287        2636 :   return z;
     288             : }
     289             : 
     290             : GEN
     291         761 : FlxX_Fl_mul(GEN x, ulong y, ulong p)
     292             : {
     293         761 :   long i, lx=lg(x);
     294         761 :   GEN z = cgetg(lx, t_POL);
     295         761 :   z[1]=x[1];
     296        3667 :   for (i=2; i<lx; i++) gel(z,i) = Flx_Fl_mul(gel(x,i), y, p);
     297         761 :   return FlxX_renormalize(z, lx);
     298             : }
     299             : 
     300             : GEN
     301           0 : FlxX_triple(GEN x, ulong p)
     302             : {
     303           0 :   long i, lx=lg(x);
     304           0 :   GEN z = cgetg(lx, t_POL);
     305           0 :   z[1]=x[1];
     306           0 :   for (i=2; i<lx; i++) gel(z,i) = Flx_triple(gel(x,i), p);
     307           0 :   return FlxX_renormalize(z, lx);
     308             : }
     309             : 
     310             : GEN
     311         761 : FlxX_double(GEN x, ulong p)
     312             : {
     313         761 :   long i, lx=lg(x);
     314         761 :   GEN z = cgetg(lx, t_POL);
     315         761 :   z[1]=x[1];
     316        6088 :   for (i=2; i<lx; i++) gel(z,i) = Flx_double(gel(x,i), p);
     317         761 :   return FlxX_renormalize(z, lx);
     318             : }
     319             : 
     320             : GEN
     321       82224 : FlxX_deriv(GEN z, ulong p)
     322             : {
     323       82224 :   long i,l = lg(z)-1;
     324             :   GEN x;
     325       82224 :   if (l < 2) l = 2;
     326       82224 :   x = cgetg(l, t_POL); x[1] = z[1];
     327      892607 :   for (i=2; i<l; i++) gel(x,i) = Flx_mulu(gel(z,i+1), (ulong) i-1, p);
     328       82225 :   return FlxX_renormalize(x,l);
     329             : }
     330             : 
     331             : GEN
     332           0 : FlxX_translate1(GEN P, long p, long n)
     333             : {
     334             :   GEN Q;
     335           0 :   long i, l, ws, lP = lgpol(P);
     336           0 :   if (!lP) return gcopy(P);
     337           0 :   ws = mael(P,2,1);
     338           0 :   Q = FlxX_swap(P, n, ws);
     339           0 :   l = lg(Q);
     340           0 :   for (i=2; i<l; i++) gel(Q, i) = Flx_translate1(gel(Q, i), p);
     341           0 :   return FlxX_swap(Q, lP, ws);
     342             : }
     343             : 
     344             : GEN
     345           0 : zlxX_translate1(GEN P, long p, long e, long n)
     346             : {
     347             :   GEN Q;
     348           0 :   long i, l, ws, lP = lgpol(P);
     349           0 :   if (!lP) return gcopy(P);
     350           0 :   ws = mael(P,2,1);
     351           0 :   Q = FlxX_swap(P, n, ws);
     352           0 :   l = lg(Q);
     353           0 :   for (i=2; i<l; i++) gel(Q, i) = zlx_translate1(gel(Q, i), p, e);
     354           0 :   return FlxX_swap(Q, lP, ws);
     355             : }
     356             : 
     357             : static GEN
     358      109012 : FlxX_subspec(GEN x, GEN y, ulong p, long lx, long ly)
     359             : {
     360             :   long i,lz;
     361             :   GEN z;
     362             : 
     363      109012 :   if (ly <= lx)
     364             :   {
     365      109012 :     lz = lx+2; z = cgetg(lz, t_POL);
     366     3324116 :     for (i=0; i<ly; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
     367      110789 :     for (   ; i<lx; i++) gel(z,i+2) = Flx_copy(gel(x,i));
     368             :   }
     369             :   else
     370             :   {
     371           0 :     lz = ly+2; z = cgetg(lz, t_POL);
     372           0 :     for (i=0; i<lx; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
     373           0 :     for (   ; i<ly; i++) gel(z,i+2) = Flx_neg(gel(y,i),p);
     374             :   }
     375      109012 :   z[1] = 0; return FlxX_renormalize(z, lz);
     376             : }
     377             : 
     378             : GEN
     379      623622 : FlxX_sub(GEN x, GEN y, ulong p)
     380             : {
     381             :   long lx,ly,i,lz;
     382             :   GEN z;
     383      623622 :   lx = lg(x); ly = lg(y);
     384      623622 :   lz=maxss(lx,ly);
     385      623623 :   z = cgetg(lz,t_POL);
     386      623619 :   if (lx >= ly)
     387             :   {
     388      110105 :     z[1] = x[1];
     389      638402 :     for (i=2; i<ly; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     390      388084 :     for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     391      110105 :     if (lx==ly) z = FlxX_renormalize(z, lz);
     392             :   }
     393             :   else
     394             :   {
     395      513514 :     z[1] = y[1];
     396     1299731 :     for (i=2; i<lx; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     397     1380159 :     for (   ; i<ly; i++) gel(z,i) = Flx_neg(gel(y,i),p);
     398             :   }
     399      623577 :   if (!lgpol(z)) { set_avma((pari_sp)(z + lz)); z = pol_0(varn(x)); }
     400      623609 :   return z;
     401             : }
     402             : 
     403             : GEN
     404           0 : FlxX_Flx_mul(GEN P, GEN U, ulong p)
     405             : {
     406           0 :   long i, lP = lg(P);
     407           0 :   GEN res = cgetg(lP,t_POL);
     408           0 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
     409           0 :   res[1] = P[1];
     410           0 :   for(i=2; i<lP; i++) gel(res,i) = Flx_mul_pre(U,gel(P,i), p, pi);
     411           0 :   return FlxX_renormalize(res, lP);
     412             : }
     413             : 
     414             : GEN
     415     2307144 : FlxY_evalx_pre(GEN Q, ulong x, ulong p, ulong pi)
     416             : {
     417     2307144 :   long i, lb = lg(Q);
     418             :   GEN z;
     419     2307144 :   z = cgetg(lb,t_VECSMALL); z[1] = evalvarn(varn(Q));
     420    19979922 :   for (i=2; i<lb; i++) z[i] = Flx_eval_pre(gel(Q,i), x, p, pi);
     421     2308325 :   return Flx_renormalize(z, lb);
     422             : }
     423             : GEN
     424         231 : FlxY_evalx(GEN Q, ulong x, ulong p)
     425         231 : { return FlxY_evalx_pre(Q, x, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     426             : 
     427             : GEN
     428           0 : FlxY_Flx_translate(GEN P, GEN c, ulong p)
     429             : {
     430           0 :   pari_sp av = avma;
     431           0 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
     432             :   GEN Q;
     433             :   long i, k, n;
     434             : 
     435           0 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     436           0 :   Q = leafcopy(P); n = degpol(P);
     437           0 :   for (i=1; i<=n; i++)
     438             :   {
     439           0 :     for (k=n-i; k<n; k++)
     440           0 :       gel(Q,2+k) = Flx_add(gel(Q,2+k), Flx_mul_pre(gel(Q,2+k+1), c, p, pi), p);
     441           0 :     if (gc_needed(av,2))
     442             :     {
     443           0 :       if(DEBUGMEM>1)
     444           0 :         pari_warn(warnmem,"FlxY_Flx_translate, i = %ld/%ld", i,n);
     445           0 :       Q = gerepilecopy(av, Q);
     446             :     }
     447             :   }
     448           0 :   return gerepilecopy(av, Q);
     449             : }
     450             : 
     451             : /* allow pi = 0 */
     452             : GEN
     453    10152732 : FlxY_evalx_powers_pre(GEN pol, GEN ypowers, ulong p, ulong pi)
     454             : {
     455    10152732 :   long i, len = lg(pol);
     456    10152732 :   GEN res = cgetg(len, t_VECSMALL);
     457    10149588 :   res[1] = pol[1] & VARNBITS;
     458    34436831 :   for (i = 2; i < len; ++i)
     459    24279098 :     res[i] = Flx_eval_powers_pre(gel(pol, i), ypowers, p, pi);
     460    10157733 :   return Flx_renormalize(res, len);
     461             : }
     462             : 
     463             : /* allow pi = 0 */
     464             : ulong
     465     6812288 : FlxY_eval_powers_pre(GEN pol, GEN ypowers, GEN xpowers, ulong p, ulong pi)
     466             : {
     467     6812288 :   pari_sp av = avma;
     468     6812288 :   GEN t = FlxY_evalx_powers_pre(pol, ypowers, p, pi);
     469     6808610 :   return gc_ulong(av, Flx_eval_powers_pre(t, xpowers, p, pi));
     470             : }
     471             : 
     472             : GEN
     473      137202 : FlxY_FlxqV_evalx_pre(GEN P, GEN x, GEN T, ulong p, ulong pi)
     474             : {
     475      137202 :   long i, lP = lg(P);
     476      137202 :   GEN res = cgetg(lP,t_POL);
     477      137202 :   res[1] = P[1];
     478      931519 :   for(i=2; i<lP; i++)
     479      794317 :     gel(res,i) = Flx_FlxqV_eval_pre(gel(P,i), x, T, p, pi);
     480      137202 :   return FlxX_renormalize(res, lP);
     481             : }
     482             : GEN
     483           0 : FlxY_FlxqV_evalx(GEN P, GEN x, GEN T, ulong p)
     484           0 : { return FlxY_FlxqV_evalx_pre(P, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     485             : 
     486             : GEN
     487           0 : FlxY_Flxq_evalx_pre(GEN P, GEN x, GEN T, ulong p, ulong pi)
     488             : {
     489           0 :   pari_sp av = avma;
     490           0 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(P),1);
     491           0 :   GEN xp = Flxq_powers_pre(x, n, T, p, pi);
     492           0 :   return gerepileupto(av, FlxY_FlxqV_evalx_pre(P, xp, T, p, pi));
     493             : }
     494             : GEN
     495           0 : FlxY_Flxq_evalx(GEN P, GEN x, GEN T, ulong p)
     496           0 : { return FlxY_Flxq_evalx_pre(P, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     497             : 
     498             : GEN
     499        7391 : FlxY_Flx_div(GEN x, GEN y, ulong p)
     500             : {
     501             :   long i, l;
     502             :   GEN z;
     503        7391 :   if (degpol(y) == 0)
     504             :   {
     505        5474 :     ulong t = uel(y,2);
     506        5474 :     if (t == 1) return x;
     507           0 :     t = Fl_inv(t, p);
     508           0 :     z = cgetg_copy(x, &l); z[1] = x[1];
     509           0 :     for (i=2; i<l; i++) gel(z,i) = Flx_Fl_mul(gel(x,i),t,p);
     510             :   }
     511             :   else
     512             :   {
     513        1917 :     ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
     514        1917 :     z = cgetg_copy(x, &l); z[1] = x[1];
     515        3834 :     for (i=2; i<l; i++) gel(z,i) = Flx_div_pre(gel(x,i),y,p,pi);
     516             :   }
     517        1917 :   return z;
     518             : }
     519             : 
     520             : GEN
     521       71520 : FlxX_shift(GEN a, long n, long vs)
     522             : {
     523       71520 :   long i, l = lg(a);
     524             :   GEN  b;
     525       71520 :   if (l == 2 || !n) return a;
     526       64664 :   l += n;
     527       64664 :   if (n < 0)
     528             :   {
     529       35763 :     if (l <= 2) return pol_0(varn(a));
     530       29531 :     b = cgetg(l, t_POL); b[1] = a[1];
     531       29530 :     a -= n;
     532      611123 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     533             :   } else {
     534       28901 :     b = cgetg(l, t_POL); b[1] = a[1];
     535       28904 :     a -= n; n += 2;
     536      503103 :     for (i=2; i<n; i++) gel(b,i) = pol0_Flx(vs);
     537      363860 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     538             :   }
     539       58433 :   return b;
     540             : }
     541             : 
     542             : GEN
     543       13026 : FlxX_blocks(GEN P, long n, long m, long vs)
     544             : {
     545       13026 :   GEN z = cgetg(m+1,t_VEC);
     546       13026 :   long i,j, k=2, l = lg(P);
     547       39081 :   for(i=1; i<=m; i++)
     548             :   {
     549       26053 :     GEN zi = cgetg(n+2,t_POL);
     550       26052 :     zi[1] = P[1];
     551       26052 :     gel(z,i) = zi;
     552      159478 :     for(j=2; j<n+2; j++)
     553      133439 :       gel(zi, j) = k==l ? pol0_Flx(vs) : gel(P,k++);
     554       26039 :     zi = FlxX_renormalize(zi, n+2);
     555             :   }
     556       13028 :   return z;
     557             : }
     558             : 
     559             : static GEN
     560      233054 : FlxX_recipspec(GEN x, long l, long n, long vs)
     561             : {
     562             :   long i;
     563      233054 :   GEN z = cgetg(n+2,t_POL);
     564      233054 :   z[1] = 0; z += 2;
     565     5242771 :   for(i=0; i<l; i++)
     566     5009717 :     gel(z,n-i-1) = Flx_copy(gel(x,i));
     567      240336 :   for(   ; i<n; i++)
     568        7282 :     gel(z,n-i-1) = pol0_Flx(vs);
     569      233054 :   return FlxX_renormalize(z-2,n+2);
     570             : }
     571             : 
     572             : GEN
     573        2390 : FlxX_invLaplace(GEN x, ulong p)
     574             : {
     575        2390 :   long i, d = degpol(x);
     576             :   GEN y;
     577             :   ulong t;
     578        2390 :   if (d <= 1) return gcopy(x);
     579        2383 :   t = Fl_inv(factorial_Fl(d, p), p);
     580        2383 :   y = cgetg(d+3, t_POL);
     581        2383 :   y[1] = x[1];
     582       47429 :   for (i=d; i>=2; i--)
     583             :   {
     584       45046 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     585       45046 :     t = Fl_mul(t, i, p);
     586             :   }
     587        2383 :   gel(y,3) = Flx_copy(gel(x,3));
     588        2383 :   gel(y,2) = Flx_copy(gel(x,2));
     589        2383 :   return FlxX_renormalize(y, d+3);
     590             : }
     591             : 
     592             : GEN
     593        1195 : FlxX_Laplace(GEN x, ulong p)
     594             : {
     595        1195 :   long i, d = degpol(x);
     596        1195 :   ulong t = 1;
     597             :   GEN y;
     598        1195 :   if (d <= 1) return gcopy(x);
     599        1195 :   y = cgetg(d+3, t_POL);
     600        1195 :   y[1] = x[1];
     601        1195 :   gel(y,2) = Flx_copy(gel(x,2));
     602        1195 :   gel(y,3) = Flx_copy(gel(x,3));
     603       23724 :   for (i=2; i<=d; i++)
     604             :   {
     605       22529 :     t = Fl_mul(t, i%p, p);
     606       22529 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     607             :   }
     608        1195 :   return FlxX_renormalize(y, d+3);
     609             : }
     610             : 
     611             : /***********************************************************************/
     612             : /**                                                                   **/
     613             : /**                               FlxXV                               **/
     614             : /**                                                                   **/
     615             : /***********************************************************************/
     616             : 
     617             : GEN
     618           0 : FlxXC_sub(GEN x, GEN y, ulong p)
     619           0 : { pari_APPLY_same(FlxX_sub(gel(x, i), gel(y,i), p)) }
     620             : 
     621             : static GEN
     622      139211 : FlxXV_to_FlxM_lg(GEN x, long m, long n, long sv)
     623             : {
     624             :   long i;
     625      139211 :   GEN y = cgetg(n+1, t_MAT);
     626      462857 :   for (i=1; i<=n; i++) gel(y,i) = FlxX_to_FlxC(gel(x,i), m, sv);
     627      139211 :   return y;
     628             : }
     629             : 
     630             : GEN
     631           0 : FlxXV_to_FlxM(GEN v, long n, long sv)
     632           0 : { return FlxXV_to_FlxM_lg(v, n, lg(v)-1, sv); }
     633             : 
     634             : GEN
     635       12446 : FlxXC_to_ZXXC(GEN x)
     636       62377 : { pari_APPLY_type(t_COL, FlxX_to_ZXX(gel(x,i))) }
     637             : 
     638             : GEN
     639           0 : FlxXM_to_ZXXM(GEN x)
     640           0 : { pari_APPLY_same(FlxXC_to_ZXXC(gel(x,i))) }
     641             : 
     642             : /***********************************************************************/
     643             : /**                                                                   **/
     644             : /**                               FlxqX                               **/
     645             : /**                                                                   **/
     646             : /***********************************************************************/
     647             : 
     648             : static GEN
     649     3588425 : get_FlxqX_red(GEN T, GEN *B)
     650             : {
     651     3588425 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
     652      139588 :   *B = gel(T,1); return gel(T,2);
     653             : }
     654             : 
     655             : GEN
     656      151778 : RgX_to_FlxqX(GEN x, GEN T, ulong p)
     657             : {
     658      151778 :   long i, l = lg(x);
     659      151778 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     660     2327891 :   for (i = 2; i < l; i++)
     661     2176117 :     gel(z,i) = Rg_to_Flxq(gel(x,i), T, p);
     662      151774 :   return FlxX_renormalize(z, l);
     663             : }
     664             : 
     665             : /* FlxqX are t_POL with Flxq coefficients.
     666             :  * Normally the variable ordering should be respected.*/
     667             : 
     668             : GEN
     669         634 : random_FlxqX(long d1, long v, GEN T, ulong p)
     670             : {
     671         634 :   long dT = get_Flx_degree(T), vT = get_Flx_var(T);
     672         634 :   long i, d = d1+2;
     673         634 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     674        3374 :   for (i=2; i<d; i++) gel(y,i) = random_Flx(dT, vT, p);
     675         634 :   return FlxX_renormalize(y,d);
     676             : }
     677             : 
     678             : /*Not stack clean*/
     679             : GEN
     680     2354175 : Kronecker_to_FlxqX_pre(GEN z, GEN T, ulong p, ulong pi)
     681             : {
     682     2354175 :   long i,j,lx,l, N = (get_Flx_degree(T)<<1) + 1;
     683     2354174 :   GEN x, t = cgetg(N,t_VECSMALL);
     684     2354168 :   t[1] = get_Flx_var(T);
     685     2354168 :   l = lg(z); lx = (l-2) / (N-2);
     686     2354168 :   x = cgetg(lx+3,t_POL);
     687     2354163 :   x[1] = z[1];
     688    28772126 :   for (i=2; i<lx+2; i++)
     689             :   {
     690   367906060 :     for (j=2; j<N; j++) t[j] = z[j];
     691    26417983 :     z += (N-2);
     692    26417983 :     gel(x,i) = Flx_rem_pre(Flx_renormalize(t,N), T,p,pi);
     693             :   }
     694     2354143 :   N = (l-2) % (N-2) + 2;
     695     7477264 :   for (j=2; j<N; j++) t[j] = z[j];
     696     2354143 :   gel(x,i) = Flx_rem_pre(Flx_renormalize(t,N), T,p,pi);
     697     2354102 :   return FlxX_renormalize(x, i+1);
     698             : }
     699             : GEN
     700           0 : Kronecker_to_FlxqX(GEN z, GEN T, ulong p)
     701           0 : { return Kronecker_to_FlxqX_pre(z, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     702             : 
     703             : GEN
     704     1259048 : FlxqX_red_pre(GEN z, GEN T, ulong p, ulong pi)
     705             : {
     706             :   GEN res;
     707     1259048 :   long i, l = lg(z);
     708     1259048 :   res = cgetg(l,t_POL); res[1] = z[1];
     709    12999787 :   for(i=2;i<l;i++) gel(res,i) = Flx_rem_pre(gel(z,i),T,p,pi);
     710     1259013 :   return FlxX_renormalize(res,l);
     711             : }
     712             : GEN
     713           0 : FlxqX_red(GEN z, GEN T, ulong p)
     714           0 : { return FlxqX_red_pre(z, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     715             : 
     716             : static GEN
     717      290346 : FlxqX_mulspec(GEN x, GEN y, GEN T, ulong p, ulong pi, long lx, long ly)
     718             : {
     719      290346 :   pari_sp av = avma;
     720             :   GEN z,kx,ky;
     721      290346 :   long dT =  get_Flx_degree(T);
     722      290346 :   kx= zxX_to_Kronecker_spec(x,lx,dT);
     723      290346 :   ky= zxX_to_Kronecker_spec(y,ly,dT);
     724      290346 :   z = Flx_mul_pre(ky, kx, p, pi);
     725      290346 :   z = Kronecker_to_FlxqX_pre(z,T,p,pi);
     726      290346 :   return gerepileupto(av, z);
     727             : }
     728             : 
     729             : GEN
     730     1708297 : FlxqX_mul_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
     731             : {
     732     1708297 :   pari_sp av = avma;
     733     1708297 :   GEN z, kx, ky, Tm = get_Flx_mod(T);
     734     1708295 :   kx= zxX_to_Kronecker(x, Tm);
     735     1708311 :   ky= zxX_to_Kronecker(y, Tm);
     736     1708314 :   z = Flx_mul_pre(ky, kx, p, pi);
     737     1708283 :   z = Kronecker_to_FlxqX_pre(z, T, p, pi);
     738     1708245 :   return gerepileupto(av, z);
     739             : }
     740             : GEN
     741       63698 : FlxqX_mul(GEN x, GEN y, GEN T, ulong p)
     742       63698 : { return FlxqX_mul_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     743             : 
     744             : GEN
     745      355558 : FlxqX_sqr_pre(GEN x, GEN T, ulong p, ulong pi)
     746             : {
     747      355558 :   pari_sp av = avma;
     748             :   GEN z,kx;
     749      355558 :   kx= zxX_to_Kronecker(x,get_Flx_mod(T));
     750      355557 :   z = Flx_sqr_pre(kx, p, pi);
     751      355557 :   z = Kronecker_to_FlxqX_pre(z,T,p,pi);
     752      355556 :   return gerepileupto(av, z);
     753             : }
     754             : GEN
     755        1260 : FlxqX_sqr(GEN x, GEN T, ulong p)
     756        1260 : { return FlxqX_sqr_pre(x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     757             : 
     758             : GEN
     759      154791 : FlxqX_Flxq_mul_pre(GEN P, GEN U, GEN T, ulong p, ulong pi)
     760             : {
     761      154791 :   long i, lP = lg(P);
     762      154791 :   GEN res = cgetg(lP,t_POL);
     763      154792 :   res[1] = P[1];
     764      553120 :   for(i=2; i<lP; i++) gel(res,i) = Flxq_mul_pre(U,gel(P,i), T,p,pi);
     765      154769 :   return FlxX_renormalize(res, lP);
     766             : }
     767             : GEN
     768           0 : FlxqX_Flxq_mul(GEN P, GEN U, GEN T, ulong p)
     769           0 : { return FlxqX_Flxq_mul_pre(P, U, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     770             : 
     771             : GEN
     772      633397 : FlxqX_Flxq_mul_to_monic_pre(GEN P, GEN U, GEN T, ulong p, ulong pi)
     773             : {
     774      633397 :   long i, lP = lg(P);
     775      633397 :   GEN res = cgetg(lP,t_POL);
     776      633390 :   res[1] = P[1];
     777     3446548 :   for(i=2; i<lP-1; i++) gel(res,i) = Flxq_mul_pre(U,gel(P,i), T,p,pi);
     778      632998 :   gel(res,lP-1) = pol1_Flx(get_Flx_var(T));
     779      633415 :   return FlxX_renormalize(res, lP);
     780             : }
     781             : GEN
     782           0 : FlxqX_Flxq_mul_to_monic(GEN P, GEN U, GEN T, ulong p)
     783             : {
     784           0 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
     785           0 :   return FlxqX_Flxq_mul_to_monic_pre(P, U, T, p, pi);
     786             : }
     787             : 
     788             : GEN
     789      189644 : FlxqX_normalize_pre(GEN z, GEN T, ulong p, ulong pi)
     790             : {
     791      189644 :   GEN p1 = leading_coeff(z);
     792      189644 :   if (!lgpol(z) || (!degpol(p1) && p1[1] == 1)) return z;
     793      189623 :   return FlxqX_Flxq_mul_to_monic_pre(z, Flxq_inv_pre(p1,T,p,pi), T,p,pi);
     794             : }
     795             : GEN
     796         133 : FlxqX_normalize(GEN z, GEN T, ulong p)
     797         133 : { return FlxqX_normalize_pre(z, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     798             : 
     799             : struct _FlxqX {ulong p, pi; GEN T;};
     800      538707 : static GEN _FlxqX_mul(void *data,GEN a,GEN b)
     801             : {
     802      538707 :   struct _FlxqX *d=(struct _FlxqX*)data;
     803      538707 :   return FlxqX_mul_pre(a,b,d->T,d->p,d->pi);
     804             : }
     805       10283 : static GEN _FlxqX_sqr(void *data,GEN a)
     806             : {
     807       10283 :   struct _FlxqX *d=(struct _FlxqX*)data;
     808       10283 :   return FlxqX_sqr_pre(a,d->T,d->p,d->pi);
     809             : }
     810             : 
     811             : GEN
     812       10248 : FlxqX_powu_pre(GEN V, ulong n, GEN T, ulong p, ulong pi)
     813             : {
     814       10248 :   struct _FlxqX d; d.p = p; d.pi = pi; d.T = T;
     815       10248 :   return gen_powu(V, n, (void*)&d, &_FlxqX_sqr, &_FlxqX_mul);
     816             : }
     817             : GEN
     818           0 : FlxqX_powu(GEN V, ulong n, GEN T, ulong p)
     819           0 : { return FlxqX_powu_pre(V, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     820             : 
     821             : /* x and y in Z[Y][X]. Assume T irreducible mod p */
     822             : static GEN
     823     3140661 : FlxqX_divrem_basecase(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *pr)
     824             : {
     825             :   long vx, dx, dy, dz, i, j, sx, lr;
     826             : 
     827             :   pari_sp av0, av, tetpil;
     828             :   GEN z,p1,rem,lead;
     829             : 
     830     3140661 :   if (!signe(y)) pari_err_INV("FlxqX_divrem",y);
     831     3140661 :   vx=varn(x); dy=degpol(y); dx=degpol(x);
     832     3140665 :   if (dx < dy)
     833             :   {
     834       21698 :     if (pr)
     835             :     {
     836       21395 :       av0 = avma; x = FlxqX_red_pre(x, T, p, pi);
     837       21395 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: pol_0(vx); }
     838       21395 :       if (pr == ONLY_REM) return x;
     839       21395 :       *pr = x;
     840             :     }
     841       21698 :     return pol_0(vx);
     842             :   }
     843     3118967 :   lead = leading_coeff(y);
     844     3118978 :   if (!dy) /* y is constant */
     845             :   {
     846      325325 :     if (pr && pr != ONLY_DIVIDES)
     847             :     {
     848      320666 :       if (pr == ONLY_REM) return pol_0(vx);
     849      133511 :       *pr = pol_0(vx);
     850             :     }
     851      138170 :     if (Flx_equal1(lead)) return gcopy(x);
     852      133987 :     av0 = avma; x = FlxqX_Flxq_mul_pre(x,Flxq_inv(lead,T,p),T,p,pi);
     853      133982 :     return gerepileupto(av0,x);
     854             :   }
     855     2793653 :   av0 = avma; dz = dx-dy;
     856     2793653 :   lead = Flx_equal1(lead)? NULL: gclone(Flxq_inv_pre(lead,T,p,pi));
     857     2793691 :   set_avma(av0);
     858     2793685 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
     859     2793694 :   x += 2; y += 2; z += 2;
     860             : 
     861     2793694 :   p1 = gel(x,dx); av = avma;
     862     2793694 :   gel(z,dz) = lead? gerepileupto(av, Flxq_mul_pre(p1,lead, T,p,pi)): gcopy(p1);
     863     6446421 :   for (i=dx-1; i>=dy; i--)
     864             :   {
     865     3652771 :     av=avma; p1=gel(x,i);
     866    11930233 :     for (j=i-dy+1; j<=i && j<=dz; j++)
     867     8277649 :       p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi),p);
     868     3652584 :     if (lead) p1 = Flx_mul_pre(p1, lead,p,pi);
     869     3652585 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Flx_rem_pre(p1,T,p,pi));
     870             :   }
     871     2793650 :   if (!pr) { guncloneNULL(lead); return z-2; }
     872             : 
     873     2599718 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     874     2908703 :   for (sx=0; ; i--)
     875             :   {
     876     2908703 :     p1 = gel(x,i);
     877     8582540 :     for (j=0; j<=i && j<=dz; j++)
     878     5674030 :       p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi),p);
     879     2908510 :     tetpil=avma; p1 = Flx_rem_pre(p1, T,p,pi); if (lgpol(p1)) { sx = 1; break; }
     880      381313 :     if (!i) break;
     881      308945 :     set_avma(av);
     882             :   }
     883     2599572 :   if (pr == ONLY_DIVIDES)
     884             :   {
     885           0 :     guncloneNULL(lead);
     886           0 :     if (sx) return gc_NULL(av0);
     887           0 :     return gc_const((pari_sp)rem, z-2);
     888             :   }
     889     2599572 :   lr=i+3; rem -= lr;
     890     2599572 :   rem[0] = evaltyp(t_POL) | _evallg(lr);
     891     2599572 :   rem[1] = z[-1];
     892     2599572 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
     893     2599735 :   rem += 2; gel(rem,i) = p1;
     894    16284284 :   for (i--; i>=0; i--)
     895             :   {
     896    13684544 :     av=avma; p1 = gel(x,i);
     897    44710600 :     for (j=0; j<=i && j<=dz; j++)
     898    31032284 :       p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi), p);
     899    13678316 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Flx_rem_pre(p1, T,p,pi));
     900             :   }
     901     2599740 :   rem -= 2;
     902     2599740 :   guncloneNULL(lead);
     903     2599752 :   if (!sx) (void)FlxX_renormalize(rem, lr);
     904     2599759 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     905     1219168 :   *pr = rem; return z-2;
     906             : }
     907             : 
     908             : static GEN
     909        1340 : FlxqX_invBarrett_basecase(GEN T, GEN Q, ulong p, ulong pi)
     910             : {
     911        1340 :   long i, l=lg(T)-1, lr = l-1, k;
     912        1340 :   long sv=Q[1];
     913        1340 :   GEN r=cgetg(lr,t_POL); r[1]=T[1];
     914        1340 :   gel(r,2) = pol1_Flx(sv);
     915       15134 :   for (i=3;i<lr;i++)
     916             :   {
     917       13794 :     pari_sp ltop=avma;
     918       13794 :     GEN u = Flx_neg(gel(T,l-i+2),p);
     919       99301 :     for (k=3;k<i;k++)
     920       85507 :       u = Flx_sub(u, Flxq_mul_pre(gel(T,l-i+k), gel(r,k), Q, p, pi), p);
     921       13794 :     gel(r,i) = gerepileupto(ltop, u);
     922             :   }
     923        1340 :   r = FlxX_renormalize(r,lr);
     924        1340 :   return r;
     925             : }
     926             : 
     927             : /* Return new lgpol */
     928             : static long
     929      325174 : FlxX_lgrenormalizespec(GEN x, long lx)
     930             : {
     931             :   long i;
     932      356282 :   for (i = lx-1; i>=0; i--)
     933      356282 :     if (lgpol(gel(x,i))) break;
     934      325174 :   return i+1;
     935             : }
     936             : 
     937             : static GEN
     938        6697 : FlxqX_invBarrett_Newton(GEN S, GEN T, ulong p, ulong pi)
     939             : {
     940        6697 :   pari_sp av = avma;
     941        6697 :   long nold, lx, lz, lq, l = degpol(S), i, lQ;
     942        6697 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
     943        6697 :   long dT = get_Flx_degree(T), vT = get_Flx_var(T);
     944        6697 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
     945      273192 :   for (i=0;i<l;i++) gel(x,i) = pol0_Flx(vT);
     946        6697 :   q = FlxX_recipspec(S+2,l+1,l+1,dT);
     947        6697 :   lQ = lgpol(q); q+=2;
     948             :   /* We work on _spec_ FlxX's, all the l[xzq] below are lgpol's */
     949             : 
     950             :   /* initialize */
     951        6697 :   gel(x,0) = Flxq_inv_pre(gel(q,0),T, p, pi);
     952        6697 :   if (lQ>1 && degpol(gel(q,1)) >= dT)
     953           0 :     gel(q,1) = Flx_rem_pre(gel(q,1), T, p, pi);
     954        6697 :   if (lQ>1 && lgpol(gel(q,1)))
     955        5686 :   {
     956        5686 :     GEN u = gel(q, 1);
     957        5686 :     if (!Flx_equal1(gel(x,0)))
     958           0 :       u = Flxq_mul_pre(u, Flxq_sqr_pre(gel(x,0), T,p,pi), T,p,pi);
     959        5686 :     gel(x,1) = Flx_neg(u, p); lx = 2;
     960             :   }
     961             :   else
     962        1011 :     lx = 1;
     963        6697 :   nold = 1;
     964       43173 :   for (; mask > 1; )
     965             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
     966       36476 :     long i, lnew, nnew = nold << 1;
     967             : 
     968       36476 :     if (mask & 1) nnew--;
     969       36476 :     mask >>= 1;
     970             : 
     971       36476 :     lnew = nnew + 1;
     972       36476 :     lq = FlxX_lgrenormalizespec(q, minss(lQ,lnew));
     973       36476 :     z = FlxqX_mulspec(x, q, T,p,pi, lx, lq); /* FIXME: high product */
     974       36476 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
     975       36476 :     z += 2;
     976             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
     977       74564 :     for (i = nold; i < lz; i++) if (lgpol(gel(z,i))) break;
     978       36476 :     nold = nnew;
     979       36476 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
     980             : 
     981             :     /* z + i represents (x*q - 1) / t^i */
     982       35573 :     lz = FlxX_lgrenormalizespec (z+i, lz-i);
     983       35573 :     z = FlxqX_mulspec(x, z+i, T,p,pi, lx, lz); /* FIXME: low product */
     984       35573 :     lz = lgpol(z); z += 2;
     985       35573 :     if (lz > lnew-i) lz = FlxX_lgrenormalizespec(z, lnew-i);
     986             : 
     987       35573 :     lx = lz+ i;
     988       35573 :     y  = x + i; /* x -= z * t^i, in place */
     989      278745 :     for (i = 0; i < lz; i++) gel(y,i) = Flx_neg(gel(z,i), p);
     990             :   }
     991        6697 :   x -= 2; setlg(x, lx + 2); x[1] = S[1];
     992        6697 :   return gerepilecopy(av, x);
     993             : }
     994             : 
     995             : GEN
     996        8037 : FlxqX_invBarrett_pre(GEN T, GEN Q, ulong p, ulong pi)
     997             : {
     998        8037 :   pari_sp ltop=avma;
     999        8037 :   long l=lg(T), v = varn(T);
    1000             :   GEN r;
    1001        8037 :   GEN c = gel(T,l-1);
    1002        8037 :   if (l<5) return pol_0(v);
    1003        8037 :   if (l<=FlxqX_INVBARRETT_LIMIT)
    1004             :   {
    1005        1340 :     if (!Flx_equal1(c))
    1006             :     {
    1007           0 :       GEN ci = Flxq_inv_pre(c,Q,p,pi);
    1008           0 :       T = FlxqX_Flxq_mul_pre(T, ci, Q, p, pi);
    1009           0 :       r = FlxqX_invBarrett_basecase(T,Q,p,pi);
    1010           0 :       r = FlxqX_Flxq_mul_pre(r,ci,Q,p,pi);
    1011             :     } else
    1012        1340 :       r = FlxqX_invBarrett_basecase(T,Q,p,pi);
    1013             :   } else
    1014        6697 :     r = FlxqX_invBarrett_Newton(T,Q,p,pi);
    1015        8037 :   return gerepileupto(ltop, r);
    1016             : }
    1017             : GEN
    1018          76 : FlxqX_invBarrett(GEN T, GEN Q, ulong p)
    1019          76 : { return FlxqX_invBarrett_pre(T, Q, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1020             : 
    1021             : GEN
    1022      493118 : FlxqX_get_red_pre(GEN S, GEN T, ulong p, ulong pi)
    1023             : {
    1024      493118 :   if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
    1025        6588 :     retmkvec2(FlxqX_invBarrett_pre(S, T, p, pi), S);
    1026      486530 :   return S;
    1027             : }
    1028             : GEN
    1029         371 : FlxqX_get_red(GEN S, GEN T, ulong p)
    1030             : {
    1031         371 :   if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
    1032          76 :     retmkvec2(FlxqX_invBarrett(S, T, p), S);
    1033         295 :   return S;
    1034             : }
    1035             : 
    1036             : /* Compute x mod S where 2 <= degpol(S) <= l+1 <= 2*(degpol(S)-1)
    1037             :  *  * and mg is the Barrett inverse of S. */
    1038             : static GEN
    1039      109285 : FlxqX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN S, GEN T, ulong p,
    1040             :   ulong pi, GEN *pr)
    1041             : {
    1042             :   GEN q, r;
    1043      109285 :   long lt = degpol(S); /*We discard the leading term*/
    1044             :   long ld, lm, lT, lmg;
    1045      109285 :   ld = l-lt;
    1046      109285 :   lm = minss(ld, lgpol(mg));
    1047      109285 :   lT  = FlxX_lgrenormalizespec(S+2,lt);
    1048      109285 :   lmg = FlxX_lgrenormalizespec(mg+2,lm);
    1049      109285 :   q = FlxX_recipspec(x+lt,ld,ld,0);               /* = rec(x)     lq<=ld*/
    1050      109285 :   q = FlxqX_mulspec(q+2,mg+2,T,p,pi,lgpol(q),lmg); /* = rec(x)*mg lq<=ld+lm*/
    1051      109285 :   q = FlxX_recipspec(q+2,minss(ld,lgpol(q)),ld,0); /* = rec(rec(x)*mg) lq<=ld*/
    1052      109285 :   if (!pr) return q;
    1053      109012 :   r = FlxqX_mulspec(q+2,S+2,T,p,pi,lgpol(q),lT);  /* = q*pol   lr<=ld+lt*/
    1054      109012 :   r = FlxX_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* = x - r   lr<=lt */
    1055      109012 :   if (pr == ONLY_REM) return r;
    1056        7667 :   *pr = r; return q;
    1057             : }
    1058             : 
    1059             : static GEN
    1060      102410 : FlxqX_divrem_Barrett(GEN x, GEN mg, GEN S, GEN T, ulong p, ulong pi, GEN *pr)
    1061             : {
    1062      102410 :   GEN q = NULL, r = FlxqX_red_pre(x, T, p, pi);
    1063      102410 :   long l = lgpol(r), lt = degpol(S), lm = 2*lt-1, v = varn(S);
    1064             :   long i;
    1065      102410 :   if (l <= lt)
    1066             :   {
    1067           0 :     if (pr == ONLY_REM) return r;
    1068           0 :     if (pr == ONLY_DIVIDES) return signe(r)? NULL: pol_0(v);
    1069           0 :     if (pr) *pr = r;
    1070           0 :     return pol_0(v);
    1071             :   }
    1072      102410 :   if (lt <= 1)
    1073           0 :     return FlxqX_divrem_basecase(x,S,T,p,pi,pr);
    1074      102410 :   if (pr != ONLY_REM && l>lm)
    1075             :   {
    1076         750 :     long vT = get_Flx_var(T);
    1077         750 :     q = cgetg(l-lt+2, t_POL); q[1] = S[1];
    1078       53441 :     for (i=0;i<l-lt;i++) gel(q+2,i) = pol0_Flx(vT);
    1079             :   }
    1080      109367 :   while (l>lm)
    1081             :   {
    1082        6957 :     GEN zr, zq = FlxqX_divrem_Barrettspec(r+2+l-lm,lm,mg,S,T,p,pi,&zr);
    1083        6957 :     long lz = lgpol(zr);
    1084        6957 :     if (pr != ONLY_REM)
    1085             :     {
    1086        2568 :       long lq = lgpol(zq);
    1087       48051 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1088             :     }
    1089       69735 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
    1090        6957 :     l = l-lm+lz;
    1091             :   }
    1092      102410 :   if (pr == ONLY_REM)
    1093             :   {
    1094      101345 :     if (l > lt)
    1095      101345 :       r = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,pi,ONLY_REM);
    1096             :     else
    1097           0 :       r = FlxX_renormalize(r, l+2);
    1098      101345 :     setvarn(r, v); return r;
    1099             :   }
    1100        1065 :   if (l > lt)
    1101             :   {
    1102         983 :     GEN zq = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,pi,pr? &r: NULL);
    1103         983 :     if (!q) q = zq;
    1104             :     else
    1105             :     {
    1106         668 :       long lq = lgpol(zq);
    1107        5292 :       for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
    1108             :     }
    1109             :   }
    1110          82 :   else if (pr)
    1111          82 :     r = FlxX_renormalize(r, l+2);
    1112        1065 :   setvarn(q, v); q = FlxX_renormalize(q, lg(q));
    1113        1065 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
    1114        1065 :   if (pr) { setvarn(r, v); *pr = r; }
    1115        1065 :   return q;
    1116             : }
    1117             : 
    1118             : GEN
    1119     1573941 : FlxqX_divrem_pre(GEN x, GEN S, GEN T, ulong p, long pi, GEN *pr)
    1120             : {
    1121             :   GEN B, y;
    1122             :   long dy, dx, d;
    1123     1573941 :   if (pr==ONLY_REM) return FlxqX_rem_pre(x, S, T, p, pi);
    1124     1573941 :   y = get_FlxqX_red(S, &B);
    1125     1573928 :   dy = degpol(y); dx = degpol(x); d = dx-dy;
    1126     1573903 :   if (!B && d+3 < FlxqX_DIVREM_BARRETT_LIMIT)
    1127     1572838 :     return FlxqX_divrem_basecase(x,y,T,p,pi,pr);
    1128             :   else
    1129             :   {
    1130        1065 :     pari_sp av = avma;
    1131        1065 :     GEN mg = B? B: FlxqX_invBarrett_pre(y, T, p, pi);
    1132        1065 :     GEN q = FlxqX_divrem_Barrett(x,mg,y,T,p,pi,pr);
    1133        1065 :     if (!q) return gc_NULL(av);
    1134        1065 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q);
    1135         792 :     return gc_all(av, 2, &q, pr);
    1136             :   }
    1137             : }
    1138             : GEN
    1139     1072328 : FlxqX_divrem(GEN x, GEN S, GEN T, ulong p, GEN *pr)
    1140             : {
    1141     1072328 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1142     1072328 :   return FlxqX_divrem_pre(x, S, T, p, pi, pr);
    1143             : }
    1144             : 
    1145             : GEN
    1146     2013893 : FlxqX_rem_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
    1147             : {
    1148     2013893 :   GEN B, y = get_FlxqX_red(S, &B);
    1149     2013891 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1150     2013890 :   if (d < 0) return FlxqX_red_pre(x, T, p, pi);
    1151     1669105 :   if (!B && d+3 < FlxqX_REM_BARRETT_LIMIT)
    1152     1567760 :     return FlxqX_divrem_basecase(x,y, T, p, pi, ONLY_REM);
    1153             :   else
    1154             :   {
    1155      101345 :     pari_sp av=avma;
    1156      101345 :     GEN mg = B? B: FlxqX_invBarrett_pre(y, T, p, pi);
    1157      101345 :     GEN r = FlxqX_divrem_Barrett(x, mg, y, T, p, pi, ONLY_REM);
    1158      101345 :     return gerepileupto(av, r);
    1159             :   }
    1160             : }
    1161             : GEN
    1162        5949 : FlxqX_rem(GEN x, GEN S, GEN T, ulong p)
    1163        5949 : { return FlxqX_rem_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1164             : 
    1165             : /* x + y*z mod p */
    1166             : INLINE GEN
    1167      107400 : Flxq_addmul_pre(GEN x, GEN y, GEN z, GEN T, ulong p, ulong pi)
    1168             : {
    1169             :   pari_sp av;
    1170      107400 :   if (!lgpol(y) || !lgpol(z)) return Flx_rem_pre(x, T, p, pi);
    1171      105935 :   if (!lgpol(x)) return Flxq_mul_pre(z, y, T, p, pi);
    1172      105849 :   av = avma;
    1173      105849 :   return gerepileupto(av, Flx_add(x, Flxq_mul_pre(y, z, T, p, pi), p));
    1174             : }
    1175             : 
    1176             : GEN
    1177       53700 : FlxqX_div_by_X_x_pre(GEN a, GEN x, GEN T, ulong p, ulong pi, GEN *r)
    1178             : {
    1179       53700 :   long l = lg(a), i;
    1180             :   GEN z;
    1181       53700 :   if (l <= 3)
    1182             :   {
    1183           0 :     if (r) *r = l == 2? pol0_Flx(get_Flx_var(T)): Flx_copy(gel(a,2));
    1184           0 :     return pol_0(varn(a));
    1185             :   }
    1186       53700 :   l--; z = cgetg(l, t_POL); z[1] = a[1];
    1187       53700 :   gel(z, l-1) = gel(a,l);
    1188      161100 :   for (i=l-2; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1189      107400 :     gel(z, i) = Flxq_addmul_pre(gel(a,i+1), x, gel(z,i+1), T, p, pi);
    1190       53700 :   if (r) *r = Flxq_addmul_pre(gel(a,2), x, gel(z,2), T, p, pi);
    1191       53700 :   return z;
    1192             : }
    1193             : 
    1194             : GEN
    1195       53700 : FlxqX_div_by_X_x(GEN a, GEN x, GEN T, ulong p, GEN *r)
    1196       53700 : { return FlxqX_div_by_X_x_pre(a, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p), r); }
    1197             : 
    1198             : static GEN
    1199       13580 : FlxqX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN T, ulong p, ulong pi)
    1200             : {
    1201       13580 :   return FlxX_add(FlxqX_mul_pre(u, x, T, p, pi),
    1202             :                   FlxqX_mul_pre(v, y, T, p, pi), p);
    1203             : }
    1204             : 
    1205             : static GEN
    1206        6651 : FlxqXM_FlxqX_mul2(GEN M, GEN x, GEN y, GEN T, ulong p, ulong pi)
    1207             : {
    1208        6651 :   GEN res = cgetg(3, t_COL);
    1209        6651 :   gel(res, 1) = FlxqX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, T, p, pi);
    1210        6651 :   gel(res, 2) = FlxqX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, T, p, pi);
    1211        6651 :   return res;
    1212             : }
    1213             : 
    1214             : static GEN
    1215        3289 : FlxqXM_mul2(GEN A, GEN B, GEN T, ulong p, ulong pi)
    1216             : {
    1217        3289 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
    1218        3289 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
    1219        3289 :   GEN M1 = FlxqX_mul_pre(FlxX_add(A11,A22, p), FlxX_add(B11,B22, p), T, p, pi);
    1220        3289 :   GEN M2 = FlxqX_mul_pre(FlxX_add(A21,A22, p), B11, T, p, pi);
    1221        3289 :   GEN M3 = FlxqX_mul_pre(A11, FlxX_sub(B12,B22, p), T, p, pi);
    1222        3289 :   GEN M4 = FlxqX_mul_pre(A22, FlxX_sub(B21,B11, p), T, p, pi);
    1223        3289 :   GEN M5 = FlxqX_mul_pre(FlxX_add(A11,A12, p), B22, T, p, pi);
    1224        3289 :   GEN M6 = FlxqX_mul_pre(FlxX_sub(A21,A11, p), FlxX_add(B11,B12, p), T, p, pi);
    1225        3289 :   GEN M7 = FlxqX_mul_pre(FlxX_sub(A12,A22, p), FlxX_add(B21,B22, p), T, p, pi);
    1226        3289 :   GEN T1 = FlxX_add(M1,M4, p), T2 = FlxX_sub(M7,M5, p);
    1227        3289 :   GEN T3 = FlxX_sub(M1,M2, p), T4 = FlxX_add(M3,M6, p);
    1228        3289 :   retmkmat22(FlxX_add(T1,T2, p), FlxX_add(M3,M5, p),
    1229             :              FlxX_add(M2,M4, p), FlxX_add(T3,T4, p));
    1230             : }
    1231             : 
    1232             : /* Return [0,1;1,-q]*M */
    1233             : static GEN
    1234        3289 : FlxqX_FlxqXM_qmul(GEN q, GEN M, GEN T, ulong p, ulong pi)
    1235             : {
    1236        3289 :   GEN u = FlxqX_mul_pre(gcoeff(M,2,1), q, T,p,pi);
    1237        3289 :   GEN v = FlxqX_mul_pre(gcoeff(M,2,2), q, T,p,pi);
    1238        3289 :   retmkmat22(gcoeff(M,2,1), gcoeff(M,2,2),
    1239             :     FlxX_sub(gcoeff(M,1,1), u, p), FlxX_sub(gcoeff(M,1,2), v, p));
    1240             : }
    1241             : 
    1242             : static GEN
    1243           0 : matid2_FlxXM(long v, long sv)
    1244           0 : { retmkmat22(pol1_FlxX(v, sv),pol_0(v),pol_0(v),pol1_FlxX(v, sv)); }
    1245             : 
    1246             : static GEN
    1247           0 : matJ2_FlxXM(long v, long sv)
    1248           0 : { retmkmat22(pol_0(v),pol1_FlxX(v, sv),pol1_FlxX(v, sv),pol_0(v)); }
    1249             : 
    1250             : struct FlxqX_res
    1251             : {
    1252             :    GEN res, lc;
    1253             :    long deg0, deg1, off;
    1254             : };
    1255             : 
    1256             : INLINE void
    1257           0 : FlxqX_halfres_update(long da, long db, long dr, GEN T, ulong p, ulong pi, struct FlxqX_res *res)
    1258             : {
    1259           0 :   if (dr >= 0)
    1260             :   {
    1261           0 :     if (!Flx_equal1(res->lc))
    1262             :     {
    1263           0 :       res->lc  = Flxq_powu_pre(res->lc, da - dr, T, p, pi);
    1264           0 :       res->res = Flxq_mul_pre(res->res, res->lc, T, p, pi);
    1265             :     }
    1266           0 :     if (both_odd(da + res->off, db + res->off))
    1267           0 :       res->res = Flx_neg(res->res, p);
    1268             :   } else
    1269             :   {
    1270           0 :     if (db == 0)
    1271             :     {
    1272           0 :       if (!Flx_equal1(res->lc))
    1273             :       {
    1274           0 :           res->lc  = Flxq_powu_pre(res->lc, da, T, p, pi);
    1275           0 :           res->res = Flxq_mul_pre(res->res, res->lc, T, p, pi);
    1276             :       }
    1277             :     } else
    1278           0 :       res->res = pol0_Flx(get_Flx_var(T));
    1279             :   }
    1280           0 : }
    1281             : 
    1282             : static GEN
    1283        4115 : FlxqX_halfres_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi, GEN *pa, GEN *pb, struct FlxqX_res *res)
    1284             : {
    1285        4115 :   pari_sp av=avma;
    1286             :   GEN u,u1,v,v1, M;
    1287        4115 :   long vx = varn(a), vT = get_Flx_var(T), n = lgpol(a)>>1;
    1288        4115 :   u1 = v = pol_0(vx);
    1289        4115 :   u = v1 = pol1_FlxX(vx, vT);
    1290       22796 :   while (lgpol(b)>n)
    1291             :   {
    1292             :     GEN r, q;
    1293       18681 :     q = FlxqX_divrem(a,b, T, p, &r);
    1294       18681 :     if (res)
    1295             :     {
    1296           0 :       long da = degpol(a), db = degpol(b), dr = degpol(r);
    1297           0 :       res->lc = gel(b,db+2);
    1298           0 :       if (dr >= n)
    1299           0 :         FlxqX_halfres_update(da, db, dr, T, p, pi, res);
    1300             :       else
    1301             :       {
    1302           0 :         res->deg0 = da;
    1303           0 :         res->deg1 = db;
    1304             :       }
    1305             :     }
    1306       18681 :     a = b; b = r; swap(u,u1); swap(v,v1);
    1307       18681 :     u1 = FlxX_sub(u1, FlxqX_mul_pre(u, q, T, p, pi), p);
    1308       18681 :     v1 = FlxX_sub(v1, FlxqX_mul_pre(v, q, T, p, pi), p);
    1309       18681 :     if (gc_needed(av,2))
    1310             :     {
    1311           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_halfgcd (d = %ld)",degpol(b));
    1312           0 :       gerepileall(av,res ? 8: 6, &a,&b,&u1,&v1,&u,&v,&res->res,&res->lc);
    1313             :     }
    1314             :   }
    1315        4115 :   M = mkmat22(u,v,u1,v1); *pa = a; *pb = b;
    1316        4115 :   return gc_all(av, res ? 5: 3, &M, pa, pb, &res->res, &res->lc);
    1317             : }
    1318             : 
    1319             : static GEN FlxqX_halfres_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res);
    1320             : 
    1321             : static GEN
    1322        3362 : FlxqX_halfres_split(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res)
    1323             : {
    1324        3362 :   pari_sp av = avma;
    1325             :   GEN Q, R, S, V1, V2;
    1326             :   GEN x1, y1, r, q;
    1327        3362 :   long l = lgpol(x), n = l>>1, k, vT = get_Flx_var(T);
    1328        3362 :   if (lgpol(y) <= n)
    1329           0 :     { *a = RgX_copy(x); *b = RgX_copy(y); return matid2_FlxXM(varn(x), vT); }
    1330        3362 :   if (res)
    1331             :   {
    1332           0 :      res->lc = leading_coeff(y);
    1333           0 :      res->deg0 -= n;
    1334           0 :      res->deg1 -= n;
    1335           0 :      res->off += n;
    1336             :   }
    1337        3362 :   R = FlxqX_halfres_i(FlxX_shift(x,-n, vT),FlxX_shift(y,-n, vT), T, p, pi, a, b, res);
    1338        3362 :   if (res)
    1339             :   {
    1340           0 :     res->off -= n;
    1341           0 :     res->deg0 += n;
    1342           0 :     res->deg1 += n;
    1343             :   }
    1344        3362 :   V1 = FlxqXM_FlxqX_mul2(R, Flxn_red(x,n), Flxn_red(y,n), T, p, pi);
    1345        3362 :   x1 = FlxX_add(FlxX_shift(*a,n,vT), gel(V1,1), p);
    1346        3362 :   y1 = FlxX_add(FlxX_shift(*b,n,vT), gel(V1,2), p);
    1347        3362 :   if (lgpol(y1) <= n)
    1348             :   {
    1349          73 :     *a = x1; *b = y1;
    1350          73 :     return gc_all(av, res ? 5: 3, &R, a, b, &res->res, &res->lc);
    1351             :   }
    1352        3289 :   k = 2*n-degpol(y1);
    1353        3289 :   q = FlxqX_divrem(x1, y1, T, p, &r);
    1354        3289 :   if (res)
    1355             :   {
    1356           0 :     long dx1 = degpol(x1), dy1 = degpol(y1), dr = degpol(r);
    1357           0 :     if (dy1 < degpol(y))
    1358           0 :       FlxqX_halfres_update(res->deg0, res->deg1, dy1, T, p, pi, res);
    1359           0 :     res->lc = leading_coeff(y1);
    1360           0 :     res->deg0 = dx1;
    1361           0 :     res->deg1 = dy1;
    1362           0 :     if (dr >= n)
    1363             :     {
    1364           0 :       FlxqX_halfres_update(dx1, dy1, dr, T, p, pi, res);
    1365           0 :       res->deg0 = dy1;
    1366           0 :       res->deg1 = dr;
    1367             :     }
    1368           0 :     res->deg0 -= k;
    1369           0 :     res->deg1 -= k;
    1370           0 :     res->off += k;
    1371             :   }
    1372        3289 :   S = FlxqX_halfres_i(FlxX_shift(y1,-k, vT), FlxX_shift(r,-k, vT), T, p, pi, a, b, res);
    1373        3289 :   if (res)
    1374             :   {
    1375           0 :     res->deg0 += k;
    1376           0 :     res->deg1 += k;
    1377           0 :     res->off -= k;
    1378             :   }
    1379        3289 :   Q = FlxqXM_mul2(S, FlxqX_FlxqXM_qmul(q, R, T, p, pi), T, p, pi);
    1380        3289 :   V2 = FlxqXM_FlxqX_mul2(S, FlxXn_red(y1,k), FlxXn_red(r,k), T, p, pi);
    1381        3289 :   *a = FlxX_add(FlxX_shift(*a,k,vT), gel(V2,1), p);
    1382        3289 :   *b = FlxX_add(FlxX_shift(*b,k,vT), gel(V2,2), p);
    1383        3289 :   return gc_all(av, res ? 5: 3, &Q, a, b, &res->res, &res->lc);
    1384             : }
    1385             : 
    1386             : static GEN
    1387        7477 : FlxqX_halfres_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res)
    1388             : {
    1389        7477 :   if (lgpol(x) < FlxqX_HALFGCD_LIMIT)
    1390        4115 :     return FlxqX_halfres_basecase(x, y, T, p, pi, a, b, res);
    1391        3362 :   return FlxqX_halfres_split(x, y, T, p, pi, a, b, res);
    1392             : }
    1393             : 
    1394             : static GEN
    1395         826 : FlxqX_halfgcd_all_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *pa, GEN *pb)
    1396             : {
    1397             :   GEN a, b;
    1398         826 :   GEN R = FlxqX_halfres_i(x, y, T, p, pi, &a, &b, NULL);
    1399         826 :   if (pa) *pa = a;
    1400         826 :   if (pb) *pb = b;
    1401         826 :   return R;
    1402             : }
    1403             : 
    1404             : /* Return M in GL_2(Fp[X]/(T)[Y]) such that:
    1405             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
    1406             : */
    1407             : 
    1408             : GEN
    1409         826 : FlxqX_halfgcd_all_pre(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b)
    1410             : {
    1411         826 :   pari_sp av = avma;
    1412             :   GEN R,q,r;
    1413         826 :   if (!signe(x))
    1414             :   {
    1415           0 :     if (a) *a = RgX_copy(y);
    1416           0 :     if (b) *b = RgX_copy(x);
    1417           0 :     return matJ2_FlxXM(varn(x),get_Flx_var(T));
    1418             :   }
    1419         826 :   if (degpol(y)<degpol(x)) return FlxqX_halfgcd_all_i(x, y, T, p, pi, a, b);
    1420         186 :   q = FlxqX_divrem_pre(y, x, T, p, pi, &r);
    1421         186 :   R = FlxqX_halfgcd_all_i(x, r, T, p, pi, a, b);
    1422         186 :   gcoeff(R,1,1) = FlxX_sub(gcoeff(R,1,1),
    1423         186 :                            FlxqX_mul_pre(q, gcoeff(R,1,2), T, p, pi), p);
    1424         186 :   gcoeff(R,2,1) = FlxX_sub(gcoeff(R,2,1),
    1425         186 :                            FlxqX_mul_pre(q, gcoeff(R,2,2), T, p, pi), p);
    1426         186 :   return !a && b ? gc_all(av, 2, &R, b): gc_all(av, 1+!!a+!!b, &R, a, b);
    1427             : }
    1428             : GEN
    1429           7 : FlxqX_halfgcd_all(GEN x, GEN y, GEN T, ulong p, GEN *a, GEN *b)
    1430           7 : { return FlxqX_halfgcd_all_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p), a, b); }
    1431             : 
    1432             : GEN
    1433         302 : FlxqX_halfgcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
    1434         302 : { return FlxqX_halfgcd_all_pre(x, y, T, p, pi, NULL, NULL); }
    1435             : 
    1436             : GEN
    1437           0 : FlxqX_halfgcd(GEN x, GEN y, GEN T, ulong p)
    1438           0 : { return FlxqX_halfgcd_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1439             : 
    1440             : static GEN
    1441      166756 : FlxqX_gcd_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi)
    1442             : {
    1443      166756 :   pari_sp av = avma, av0=avma;
    1444     1075689 :   while (signe(b))
    1445             :   {
    1446             :     GEN c;
    1447      908933 :     if (gc_needed(av0,2))
    1448             :     {
    1449           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (d = %ld)",degpol(b));
    1450           0 :       gerepileall(av0,2, &a,&b);
    1451             :     }
    1452      908933 :     av = avma; c = FlxqX_rem_pre(a, b, T, p, pi); a=b; b=c;
    1453             :   }
    1454      166756 :   return gc_const(av, a);
    1455             : }
    1456             : 
    1457             : GEN
    1458      175880 : FlxqX_gcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
    1459             : {
    1460      175880 :   pari_sp av = avma;
    1461      175880 :   x = FlxqX_red_pre(x, T, p, pi);
    1462      175880 :   y = FlxqX_red_pre(y, T, p, pi);
    1463      175880 :   if (!signe(x)) return gerepileupto(av, y);
    1464      167134 :   while (lgpol(y)>=FlxqX_GCD_LIMIT)
    1465             :   {
    1466         378 :     if (lgpol(y)<=(lgpol(x)>>1))
    1467             :     {
    1468           0 :       GEN r = FlxqX_rem_pre(x, y, T, p, pi);
    1469           0 :       x = y; y = r;
    1470             :     }
    1471         378 :     (void) FlxqX_halfgcd_all_pre(x,y, T, p, pi, &x, &y);
    1472         378 :     if (gc_needed(av,2))
    1473             :     {
    1474           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (y = %ld)",degpol(y));
    1475           0 :       gerepileall(av,2,&x,&y);
    1476             :     }
    1477             :   }
    1478      166756 :   return gerepileupto(av, FlxqX_gcd_basecase(x, y, T, p, pi));
    1479             : }
    1480             : GEN
    1481       23436 : FlxqX_gcd(GEN x, GEN y, GEN T, ulong p)
    1482       23436 : { return FlxqX_gcd_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1483             : 
    1484             : static GEN
    1485      133518 : FlxqX_extgcd_basecase(GEN a, GEN b, GEN T, ulong p,ulong pi, GEN *ptu, GEN *ptv)
    1486             : {
    1487      133518 :   pari_sp av=avma;
    1488             :   GEN u,v,d,d1,v1;
    1489      133518 :   long vx = varn(a);
    1490      133518 :   d = a; d1 = b;
    1491      133518 :   v = pol_0(vx); v1 = pol1_FlxX(vx, get_Flx_var(T));
    1492      469852 :   while (signe(d1))
    1493             :   {
    1494      336329 :     GEN r, q = FlxqX_divrem_pre(d, d1, T, p, pi, &r);
    1495      336338 :     v = FlxX_sub(v, FlxqX_mul_pre(q,v1,T, p, pi),p);
    1496      336336 :     u=v; v=v1; v1=u;
    1497      336336 :     u=r; d=d1; d1=u;
    1498      336336 :     if (gc_needed(av,2))
    1499             :     {
    1500           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_extgcd (d = %ld)",degpol(d));
    1501           0 :       gerepileall(av,5, &d,&d1,&u,&v,&v1);
    1502             :     }
    1503             :   }
    1504      133523 :   if (ptu)
    1505      133509 :     *ptu = FlxqX_div_pre(FlxX_sub(d,FlxqX_mul_pre(b,v, T,p,pi), p), a, T,p,pi);
    1506      133519 :   *ptv = v; return d;
    1507             : }
    1508             : 
    1509             : static GEN
    1510         130 : FlxqX_extgcd_halfgcd(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *ptu, GEN *ptv)
    1511             : {
    1512             :   GEN u,v;
    1513         130 :   GEN V = cgetg(expu(lgpol(y))+2,t_VEC);
    1514         130 :   long i, n = 0, vs = varn(x), vT = get_Flx_var(T);
    1515         269 :   while (lgpol(y) >= FlxqX_EXTGCD_LIMIT)
    1516             :   {
    1517         139 :     if (lgpol(y)<=(lgpol(x)>>1))
    1518             :     {
    1519           0 :       GEN r, q = FlxqX_divrem_pre(x, y, T, p, pi, &r);
    1520           0 :       x = y; y = r;
    1521           0 :       gel(V,++n) = mkmat22(pol_0(vs),pol1_FlxX(vs,vT),pol1_FlxX(vs,vT),FlxX_neg(q,p));
    1522             :     } else
    1523         139 :       gel(V,++n) = FlxqX_halfgcd_all_pre(x, y, T, p, pi, &x, &y);
    1524             :   }
    1525         130 :   y = FlxqX_extgcd_basecase(x,y, T, p, pi, &u,&v);
    1526         139 :   for (i = n; i>1; i--)
    1527             :   {
    1528           9 :     GEN R = gel(V,i);
    1529           9 :     GEN u1 = FlxqX_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), T, p, pi);
    1530           9 :     GEN v1 = FlxqX_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), T, p, pi);
    1531           9 :     u = u1; v = v1;
    1532             :   }
    1533             :   {
    1534         130 :     GEN R = gel(V,1);
    1535         130 :     if (ptu)
    1536         130 :       *ptu = FlxqX_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), T, p, pi);
    1537         130 :     *ptv   = FlxqX_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), T, p, pi);
    1538             :   }
    1539         130 :   return y;
    1540             : }
    1541             : 
    1542             : /* x and y in Z[Y][X], return lift(gcd(x mod T,p, y mod T,p)). Set u and v st
    1543             :  * ux + vy = gcd (mod T,p) */
    1544             : GEN
    1545      133518 : FlxqX_extgcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *ptu, GEN *ptv)
    1546             : {
    1547      133518 :   pari_sp av = avma;
    1548             :   GEN d;
    1549      133518 :   x = FlxqX_red_pre(x, T, p, pi);
    1550      133516 :   y = FlxqX_red_pre(y, T, p, pi);
    1551      133516 :   if (lgpol(y)>=FlxqX_EXTGCD_LIMIT)
    1552         130 :     d = FlxqX_extgcd_halfgcd(x, y, T, p, pi, ptu, ptv);
    1553             :   else
    1554      133386 :     d = FlxqX_extgcd_basecase(x, y, T, p, pi, ptu, ptv);
    1555      133519 :   return gc_all(av, ptu?3:2, &d, ptv, ptu);
    1556             : }
    1557             : GEN
    1558      133504 : FlxqX_extgcd(GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv)
    1559             : {
    1560      133504 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1561      133504 :   return FlxqX_extgcd_pre(x, y, T, p, pi, ptu, ptv);
    1562             : }
    1563             : 
    1564             : static GEN
    1565      355011 : FlxqX_saferem(GEN P, GEN Q, GEN T, ulong p, ulong pi)
    1566             : {
    1567      355011 :   GEN U = Flxq_invsafe_pre(leading_coeff(Q), T, p, pi);
    1568      355023 :   if (!U) return NULL;
    1569      355023 :   Q = FlxqX_Flxq_mul_to_monic_pre(Q,U,T,p,pi);
    1570      355039 :   return FlxqX_rem_pre(P,Q,T,p,pi);
    1571             : }
    1572             : 
    1573             : GEN
    1574       88755 : FlxqX_safegcd(GEN P, GEN Q, GEN T, ulong p)
    1575             : {
    1576       88755 :   pari_sp av = avma;
    1577             :   ulong pi;
    1578             :   GEN U;
    1579       88755 :   if (!signe(P)) return gcopy(Q);
    1580       88755 :   if (!signe(Q)) return gcopy(P);
    1581       88755 :   pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1582       88755 :   T = Flx_get_red_pre(T,p,pi);
    1583             :   for(;;)
    1584             :   {
    1585      308816 :     P = FlxqX_saferem(P,Q,T,p,pi);
    1586      308816 :     if (!P) return gc_NULL(av);
    1587      308816 :     if (!signe(P)) break;
    1588      220061 :     if (gc_needed(av, 1))
    1589             :     {
    1590           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_safegcd");
    1591           0 :       gerepileall(av, 2, &P,&Q);
    1592             :     }
    1593      220061 :     swap(P, Q);
    1594             :   }
    1595       88755 :   U = Flxq_invsafe_pre(leading_coeff(Q), T, p, pi);
    1596       88756 :   if (!U) return gc_NULL(av);
    1597       88756 :   Q = FlxqX_Flxq_mul_to_monic_pre(Q,U,T,p,pi);
    1598       88756 :   return gerepileupto(av, Q);
    1599             : }
    1600             : 
    1601             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
    1602             : GEN
    1603        8584 : FlxqX_saferesultant(GEN a, GEN b, GEN T, ulong p)
    1604             : {
    1605        8584 :   long vT = get_Flx_var(T), da,db,dc;
    1606             :   ulong pi;
    1607             :   pari_sp av;
    1608        8584 :   GEN c,lb, res = pol1_Flx(vT);
    1609             : 
    1610        8585 :   if (!signe(a) || !signe(b)) return pol0_Flx(vT);
    1611             : 
    1612        8585 :   da = degpol(a);
    1613        8584 :   db = degpol(b);
    1614        8584 :   if (db > da)
    1615             :   {
    1616           0 :     swapspec(a,b, da,db);
    1617           0 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1618             :   }
    1619        8584 :   if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    1620        8584 :   pi = SMALL_ULONG(p)? 0: get_Fl_red(p); av = avma;
    1621       54759 :   while (db)
    1622             :   {
    1623       46196 :     lb = gel(b,db+2);
    1624       46196 :     c = FlxqX_saferem(a,b, T,p,pi);
    1625       46262 :     if (!c) return gc_NULL(av);
    1626       46262 :     a = b; b = c; dc = degpol(c);
    1627       46258 :     if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
    1628             : 
    1629       46244 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1630       46243 :     if (!Flx_equal1(lb))
    1631       46198 :       res = Flxq_mul_pre(res, Flxq_powu_pre(lb, da - dc, T, p, pi), T, p, pi);
    1632       46175 :     if (gc_needed(av,2))
    1633             :     {
    1634           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
    1635           0 :       gerepileall(av,3, &a,&b,&res);
    1636             :     }
    1637       46175 :     da = db; /* = degpol(a) */
    1638       46175 :     db = dc; /* = degpol(b) */
    1639             :   }
    1640        8563 :   res = Flxq_mul_pre(res, Flxq_powu_pre(gel(b,2), da, T, p, pi), T, p, pi);
    1641        8570 :   return gerepileupto(av, res);
    1642             : }
    1643             : 
    1644             : static GEN
    1645           0 : FlxqX_halfres(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, GEN *r)
    1646             : {
    1647             :   struct FlxqX_res res;
    1648             :   GEN V;
    1649             :   long dB;
    1650             : 
    1651           0 :   res.res  = *r;
    1652           0 :   res.lc   = leading_coeff(y);
    1653           0 :   res.deg0 = degpol(x);
    1654           0 :   res.deg1 = degpol(y);
    1655           0 :   res.off = 0;
    1656           0 :   V = FlxqX_halfres_i(x, y, T, p, pi, a, b, &res);
    1657           0 :   dB = degpol(*b);
    1658           0 :   if (dB < degpol(y))
    1659           0 :     FlxqX_halfres_update(res.deg0, res.deg1, dB, T, p, pi, &res);
    1660           0 :   *r = res.res;
    1661           0 :   return V;
    1662             : }
    1663             : 
    1664             : static GEN
    1665          56 : FlxqX_resultant_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi)
    1666             : {
    1667          56 :   pari_sp av = avma;
    1668          56 :   long vT = get_Flx_var(T), da,db,dc;
    1669          56 :   GEN c,lb, res = pol1_Flx(vT);
    1670             : 
    1671          56 :   if (!signe(a) || !signe(b)) return pol0_Flx(vT);
    1672             : 
    1673          56 :   da = degpol(a);
    1674          56 :   db = degpol(b);
    1675          56 :   if (db > da)
    1676             :   {
    1677           0 :     swapspec(a,b, da,db);
    1678           0 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1679             :   }
    1680          56 :   if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    1681         147 :   while (db)
    1682             :   {
    1683          91 :     lb = gel(b,db+2);
    1684          91 :     c = FlxqX_rem_pre(a,b, T,p,pi);
    1685          91 :     a = b; b = c; dc = degpol(c);
    1686          91 :     if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
    1687             : 
    1688          91 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1689          91 :     if (!Flx_equal1(lb))
    1690          63 :       res = Flxq_mul_pre(res, Flxq_powu_pre(lb, da - dc, T,p,pi), T,p,pi);
    1691          91 :     if (gc_needed(av,2))
    1692             :     {
    1693           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
    1694           0 :       gerepileall(av,3, &a,&b,&res);
    1695             :     }
    1696          91 :     da = db; /* = degpol(a) */
    1697          91 :     db = dc; /* = degpol(b) */
    1698             :   }
    1699          56 :   res = Flxq_mul_pre(res, Flxq_powu_pre(gel(b,2), da, T,p,pi), T,p,pi);
    1700          56 :   return gerepileupto(av, res);
    1701             : }
    1702             : 
    1703             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
    1704             : GEN
    1705          56 : FlxqX_resultant_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
    1706             : {
    1707          56 :   pari_sp av = avma;
    1708          56 :   long dx, dy, vT = get_Flx_var(T);
    1709          56 :   GEN res = pol1_Flx(vT);
    1710          56 :   if (!signe(x) || !signe(y)) return pol0_Flx(vT);
    1711          56 :   dx = degpol(x); dy = degpol(y);
    1712          56 :   if (dx < dy)
    1713             :   {
    1714          21 :     swap(x,y);
    1715          21 :     if (both_odd(dx, dy))
    1716           0 :       res = Flx_neg(res, p);
    1717             :   }
    1718          56 :   while (lgpol(y) >= FlxqX_GCD_LIMIT)
    1719             :   {
    1720           0 :     if (lgpol(y)<=(lgpol(x)>>1))
    1721             :     {
    1722           0 :       GEN r = FlxqX_rem_pre(x, y, T, p, pi);
    1723           0 :       long dx = degpol(x), dy = degpol(y), dr = degpol(r);
    1724           0 :       GEN ly = gel(y,dy+2);
    1725           0 :       if (!Flx_equal1(ly))
    1726           0 :         res = Flxq_mul_pre(res, Flxq_powu_pre(ly, dx - dr, T, p, pi), T, p, pi);
    1727           0 :       if (both_odd(dx, dy))
    1728           0 :         res = Flx_neg(res, p);
    1729           0 :       x = y; y = r;
    1730             :     }
    1731           0 :     (void) FlxqX_halfres(x, y, T, p, pi, &x, &y, &res);
    1732           0 :     if (gc_needed(av,2))
    1733             :     {
    1734           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (y = %ld)",degpol(y));
    1735           0 :       gerepileall(av,3,&x,&y,&res);
    1736             :     }
    1737             :   }
    1738          56 :   res = Flxq_mul_pre(res, FlxqX_resultant_basecase(x, y, T, p, pi), T, p, pi);
    1739          56 :   return gerepileupto(av, res);
    1740             : }
    1741             : GEN
    1742          56 : FlxqX_resultant(GEN x, GEN y, GEN T, ulong p)
    1743          56 : { return FlxqX_resultant_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1744             : 
    1745             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
    1746             : GEN
    1747          14 : FlxqX_disc(GEN P, GEN T, ulong p)
    1748             : {
    1749          14 :   pari_sp av = avma;
    1750          14 :   GEN L, dP = FlxX_deriv(P, p), D = FlxqX_resultant(P, dP, T, p);
    1751             :   long dd;
    1752          14 :   if (!lgpol(D)) return pol0_Flx(get_Flx_var(T));
    1753          14 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
    1754          14 :   L = leading_coeff(P);
    1755          14 :   if (dd && !Flx_equal1(L))
    1756             :   {
    1757           0 :     ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1758           0 :     D = (dd == -1)? Flxq_div_pre(D,L,T,p,pi)
    1759           0 :                   : Flxq_mul_pre(D, Flxq_powu_pre(L, dd, T,p,pi), T,p,pi); }
    1760          14 :   if (degpol(P) & 2) D = Flx_neg(D, p);
    1761          14 :   return gerepileupto(av, D);
    1762             : }
    1763             : 
    1764             : INLINE GEN
    1765        5975 : FlxXn_recip(GEN x, long n, long v)
    1766        5975 : { return FlxX_recipspec(x+2, minss(lgpol(x), n), n, v); }
    1767             : 
    1768             : GEN
    1769        2390 : FlxqX_Newton_pre(GEN P, long n, GEN T, ulong p, ulong pi)
    1770             : {
    1771        2390 :   pari_sp av = avma;
    1772        2390 :   long d = degpol(P), vT = get_Flx_var(T);
    1773        2390 :   GEN dP = FlxXn_recip(FlxX_deriv(P, p), d, vT);
    1774        2390 :   GEN Q = FlxqXn_mul_pre(FlxqXn_inv_pre(FlxXn_recip(P, d+1, vT), n, T,p,pi),
    1775             :                          dP, n, T, p, pi);
    1776        2390 :   return gerepilecopy(av, Q);
    1777             : }
    1778             : GEN
    1779           0 : FlxqX_Newton(GEN P, long n, GEN T, ulong p)
    1780           0 : { return FlxqX_Newton_pre(P, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1781             : 
    1782             : GEN
    1783        1195 : FlxqX_fromNewton_pre(GEN P, GEN T, ulong p, ulong pi)
    1784             : {
    1785        1195 :   pari_sp av = avma;
    1786        1195 :   long vT = get_Flx_var(T);
    1787        1195 :   long n = Flx_constant(constant_coeff(P))+1;
    1788        1195 :   GEN z = FlxX_neg(FlxX_shift(P, -1, vT), p);
    1789        1195 :   GEN Q = FlxXn_recip(FlxqXn_expint_pre(z, n, T, p, pi), n, vT);
    1790        1195 :   return gerepilecopy(av, Q);
    1791             : }
    1792             : GEN
    1793           0 : FlxqX_fromNewton(GEN P, GEN T, ulong p)
    1794           0 : { return FlxqX_fromNewton_pre(P, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1795             : 
    1796             : GEN
    1797        1195 : FlxqX_composedsum(GEN P, GEN Q, GEN T, ulong p)
    1798             : {
    1799        1195 :   pari_sp av = avma;
    1800        1195 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1801        1195 :   long n = 1+ degpol(P)*degpol(Q);
    1802        1195 :   GEN Pl = FlxX_invLaplace(FlxqX_Newton_pre(P,n, T,p,pi), p);
    1803        1195 :   GEN Ql = FlxX_invLaplace(FlxqX_Newton_pre(Q,n, T,p,pi), p);
    1804        1195 :   GEN L = FlxX_Laplace(FlxqXn_mul_pre(Pl, Ql, n, T,p,pi), p);
    1805        1195 :   GEN R = FlxqX_fromNewton_pre(L, T, p, pi);
    1806        1195 :   GEN lead = Flxq_mul_pre(Flxq_powu_pre(leading_coeff(P),degpol(Q), T,p,pi),
    1807        1195 :                           Flxq_powu_pre(leading_coeff(Q),degpol(P), T,p,pi),
    1808             :                           T, p, pi);
    1809        1195 :   return gerepileupto(av, FlxqX_Flxq_mul_pre(R, lead, T, p, pi));
    1810             : }
    1811             : 
    1812             : GEN
    1813      223117 : FlxqXV_prod(GEN V, GEN T, ulong p)
    1814             : {
    1815      223117 :   struct _FlxqX d; d.p=p; d.T=T; d.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1816      223117 :   return gen_product(V, (void*)&d, &_FlxqX_mul);
    1817             : }
    1818             : 
    1819             : static GEN
    1820       32178 : FlxqV_roots_to_deg1(GEN x, GEN T, ulong p, long v)
    1821             : {
    1822       32178 :   long sv = get_Flx_var(T);
    1823      124477 :   pari_APPLY_same(deg1pol_shallow(pol1_Flx(sv),Flx_neg(gel(x,i),p),v))
    1824             : }
    1825             : 
    1826             : GEN
    1827       32178 : FlxqV_roots_to_pol(GEN V, GEN T, ulong p, long v)
    1828             : {
    1829       32178 :   pari_sp ltop = avma;
    1830       32178 :   GEN W = FlxqV_roots_to_deg1(V, T, p, v);
    1831       32175 :   return gerepileupto(ltop, FlxqXV_prod(W, T, p));
    1832             : }
    1833             : 
    1834             : /*******************************************************************/
    1835             : /*                                                                 */
    1836             : /*                       (Fl[X]/T(X))[Y] / S(Y)                    */
    1837             : /*                                                                 */
    1838             : /*******************************************************************/
    1839             : 
    1840             : GEN
    1841      469863 : FlxqXQ_mul_pre(GEN x, GEN y, GEN S, GEN T, ulong p, ulong pi)
    1842      469863 : { return FlxqX_rem_pre(FlxqX_mul_pre(x,y,T,p,pi),S,T,p,pi); }
    1843             : GEN
    1844        1947 : FlxqXQ_mul(GEN x, GEN y, GEN S, GEN T, ulong p)
    1845        1947 : { return FlxqXQ_mul_pre(x, y, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1846             : 
    1847             : GEN
    1848      263593 : FlxqXQ_sqr_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
    1849      263593 : { return FlxqX_rem_pre(FlxqX_sqr_pre(x,T,p,pi),S,T,p,pi); }
    1850             : GEN
    1851           0 : FlxqXQ_sqr(GEN x, GEN S, GEN T, ulong p)
    1852           0 : { return FlxqXQ_sqr_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1853             : 
    1854             : GEN
    1855          14 : FlxqXQ_invsafe_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
    1856             : {
    1857          14 :   GEN V, z = FlxqX_extgcd_pre(get_FlxqX_mod(S), x, T, p, pi, NULL, &V);
    1858          14 :   if (degpol(z)) return NULL;
    1859          14 :   z = Flxq_invsafe_pre(gel(z,2),T,p,pi);
    1860          14 :   if (!z) return NULL;
    1861          14 :   return FlxqX_Flxq_mul_pre(V, z, T, p, pi);
    1862             : }
    1863             : GEN
    1864           0 : FlxqXQ_invsafe(GEN x, GEN S, GEN T, ulong p)
    1865           0 : { return FlxqXQ_invsafe_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1866             : 
    1867             : GEN
    1868          14 : FlxqXQ_inv_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
    1869             : {
    1870          14 :   pari_sp av = avma;
    1871          14 :   GEN U = FlxqXQ_invsafe_pre(x, S, T, p, pi);
    1872          14 :   if (!U) pari_err_INV("FlxqXQ_inv",x);
    1873          14 :   return gerepileupto(av, U);
    1874             : }
    1875             : GEN
    1876          14 : FlxqXQ_inv(GEN x, GEN S, GEN T,ulong p)
    1877          14 : { return FlxqXQ_inv_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1878             : 
    1879             : GEN
    1880           0 : FlxqXQ_div_pre(GEN x, GEN y, GEN S, GEN T, ulong p, ulong pi)
    1881           0 : { return FlxqXQ_mul_pre(x, FlxqXQ_inv_pre(y,S,T,p,pi),S,T,p,pi); }
    1882             : GEN
    1883           0 : FlxqXQ_div(GEN x, GEN y, GEN S, GEN T, ulong p)
    1884           0 : { return FlxqXQ_div_pre(x, y, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1885             : 
    1886             : struct _FlxqXQ {
    1887             :   GEN T, S;
    1888             :   ulong p, pi;
    1889             : };
    1890             : static GEN
    1891           0 : _FlxqXQ_add(void *data, GEN x, GEN y) {
    1892           0 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1893           0 :   return FlxX_add(x,y, d->p);
    1894             : }
    1895             : static GEN
    1896        5526 : _FlxqXQ_sub(void *data, GEN x, GEN y) {
    1897        5526 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1898        5526 :   return FlxX_sub(x,y, d->p);
    1899             : }
    1900             : #if 0
    1901             : static GEN
    1902             : _FlxqXQ_cmul(void *data, GEN P, long a, GEN x) {
    1903             :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1904             :   return FlxX_Flx_mul(x,gel(P,a+2), d->p);
    1905             : }
    1906             : #endif
    1907             : static GEN
    1908        6044 : _FlxqXQ_red(void *data, GEN x) {
    1909        6044 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1910        6044 :   return FlxqX_red_pre(x, d->T, d->p, d->pi);
    1911             : }
    1912             : static GEN
    1913      179880 : _FlxqXQ_mul(void *data, GEN x, GEN y) {
    1914      179880 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1915      179880 :   return FlxqXQ_mul_pre(x,y, d->S,d->T, d->p, d->pi);
    1916             : }
    1917             : static GEN
    1918      263152 : _FlxqXQ_sqr(void *data, GEN x) {
    1919      263152 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1920      263152 :   return FlxqXQ_sqr_pre(x, d->S,d->T, d->p, d->pi);
    1921             : }
    1922             : 
    1923             : static GEN
    1924      111704 : _FlxqXQ_one(void *data) {
    1925      111704 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1926      111704 :   return pol1_FlxX(get_FlxqX_var(d->S),get_Flx_var(d->T));
    1927             : }
    1928             : 
    1929             : static GEN
    1930         316 : _FlxqXQ_zero(void *data) {
    1931         316 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1932         316 :   return pol_0(get_FlxqX_var(d->S));
    1933             : }
    1934             : 
    1935             : static struct bb_algebra FlxqXQ_algebra = { _FlxqXQ_red, _FlxqXQ_add,
    1936             :        _FlxqXQ_sub, _FlxqXQ_mul, _FlxqXQ_sqr, _FlxqXQ_one, _FlxqXQ_zero };
    1937             : 
    1938             : const struct bb_algebra *
    1939         761 : get_FlxqXQ_algebra(void **E, GEN S, GEN T, ulong p)
    1940             : {
    1941         761 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1942         761 :   GEN z = new_chunk(sizeof(struct _FlxqXQ));
    1943         761 :   struct _FlxqXQ *e = (struct _FlxqXQ *) z;
    1944         761 :   e->T = Flx_get_red_pre(T, p, pi);
    1945         761 :   e->S = FlxqX_get_red_pre(S, e->T, p, pi);
    1946         761 :   e->p = p;
    1947         761 :   e->pi= pi; *E = (void*)e;
    1948         761 :   return &FlxqXQ_algebra;
    1949             : }
    1950             : 
    1951             : /* x over Fq, return lift(x^n) mod S */
    1952             : GEN
    1953          91 : FlxqXQ_pow_pre(GEN x, GEN n, GEN S, GEN T, ulong p, ulong pi)
    1954             : {
    1955          91 :   pari_sp av = avma;
    1956             :   struct _FlxqXQ D;
    1957          91 :   long s = signe(n);
    1958          91 :   if (!s) return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1959          91 :   if (s < 0) x = FlxqXQ_inv_pre(x,S,T,p,pi);
    1960          91 :   if (is_pm1(n)) return s < 0 ? x : gcopy(x);
    1961          91 :   if (degpol(x) >= get_FlxqX_degree(S)) x = FlxqX_rem_pre(x,S,T,p,pi);
    1962          91 :   T = Flx_get_red_pre(T, p, pi);
    1963          91 :   S = FlxqX_get_red_pre(S, T, p, pi);
    1964          91 :   D.S = S; D.T = T; D.p = p; D.pi = pi;
    1965          91 :   x = gen_pow_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1966          91 :   return gerepilecopy(av, x);
    1967             : }
    1968             : GEN
    1969          35 : FlxqXQ_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    1970          35 : { return FlxqXQ_pow_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1971             : 
    1972             : /* x over Fq, return lift(x^n) mod S */
    1973             : GEN
    1974       82122 : FlxqXQ_powu_pre(GEN x, ulong n, GEN S, GEN T, ulong p, ulong pi)
    1975             : {
    1976       82122 :   pari_sp av = avma;
    1977             :   struct _FlxqXQ D;
    1978       82122 :   switch(n)
    1979             :   {
    1980           0 :     case 0: return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1981        7683 :     case 1: return gcopy(x);
    1982         441 :     case 2: return FlxqXQ_sqr_pre(x, S, T, p, pi);
    1983             :   }
    1984       73998 :   T = Flx_get_red_pre(T, p, pi);
    1985       73998 :   S = FlxqX_get_red_pre(S, T, p, pi);
    1986       73998 :   D.S = S; D.T = T; D.p = p; D.pi = pi;
    1987       73998 :   x = gen_powu_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1988       73998 :   return gerepilecopy(av, x);
    1989             : }
    1990             : GEN
    1991           0 : FlxqXQ_powu(GEN x, ulong n, GEN S, GEN T, ulong p)
    1992           0 : { return FlxqXQ_powu_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1993             : 
    1994             : GEN
    1995      108669 : FlxqXQ_powers_pre(GEN x, long l, GEN S, GEN T, ulong p, ulong pi)
    1996             : {
    1997             :   struct _FlxqXQ D;
    1998      108669 :   int use_sqr = 2*degpol(x) >= get_FlxqX_degree(S);
    1999      108669 :   T = Flx_get_red_pre(T, p, pi);
    2000      108669 :   S = FlxqX_get_red_pre(S, T, p, pi);
    2001      108669 :   D.S = S; D.T = T; D.p = p; D.pi = pi;
    2002      108669 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul,&_FlxqXQ_one);
    2003             : }
    2004             : GEN
    2005           0 : FlxqXQ_powers(GEN x, long l, GEN S, GEN T, ulong p)
    2006           0 : { return FlxqXQ_powers_pre(x, l, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2007             : 
    2008             : /* Let v a linear form, return the linear form z->v(tau*z)
    2009             :    that is, v*(M_tau) */
    2010             : static GEN
    2011         604 : FlxqXQ_transmul_init(GEN tau, GEN S, GEN T, ulong p, ulong pi)
    2012             : {
    2013             :   GEN bht;
    2014         604 :   GEN h, Sp = get_FlxqX_red(S, &h);
    2015         604 :   long n = degpol(Sp), vS = varn(Sp), vT = get_Flx_var(T);
    2016         604 :   GEN ft = FlxX_recipspec(Sp+2, n+1, n+1, vT);
    2017         604 :   GEN bt = FlxX_recipspec(tau+2, lgpol(tau), n, vT);
    2018         604 :   setvarn(ft, vS); setvarn(bt, vS);
    2019         604 :   if (h)
    2020           0 :     bht = FlxqXn_mul_pre(bt, h, n-1, T, p, pi);
    2021             :   else
    2022             :   {
    2023         604 :     GEN bh = FlxqX_div_pre(FlxX_shift(tau, n-1, vT), S, T, p, pi);
    2024         604 :     bht = FlxX_recipspec(bh+2, lgpol(bh), n-1, vT);
    2025         604 :     setvarn(bht, vS);
    2026             :   }
    2027         604 :   return mkvec3(bt, bht, ft);
    2028             : }
    2029             : 
    2030             : static GEN
    2031        1265 : FlxqXQ_transmul(GEN tau, GEN a, long n, GEN T, ulong p, ulong pi)
    2032             : {
    2033        1265 :   pari_sp ltop = avma;
    2034             :   GEN t1, t2, t3, vec;
    2035        1265 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    2036        1265 :   long vT = get_Flx_var(T);
    2037        1265 :   if (signe(a)==0) return pol_0(varn(a));
    2038        1210 :   t2 = FlxX_shift(FlxqX_mul_pre(bt, a, T, p, pi),1-n,vT);
    2039        1210 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    2040         816 :   t1 = FlxX_shift(FlxqX_mul_pre(ft, a, T, p, pi),-n,vT);
    2041         816 :   t3 = FlxqXn_mul_pre(t1, bht, n-1, T, p, pi);
    2042         816 :   vec = FlxX_sub(t2, FlxX_shift(t3, 1, vT), p);
    2043         816 :   return gerepileupto(ltop, vec);
    2044             : }
    2045             : 
    2046             : static GEN
    2047         302 : polxn_FlxX(long n, long v, long vT)
    2048             : {
    2049         302 :   long i, a = n+2;
    2050         302 :   GEN p = cgetg(a+1, t_POL);
    2051         302 :   p[1] = evalsigne(1)|evalvarn(v);
    2052        2542 :   for (i = 2; i < a; i++) gel(p,i) = pol0_Flx(vT);
    2053         302 :   gel(p,a) = pol1_Flx(vT); return p;
    2054             : }
    2055             : 
    2056             : GEN
    2057         219 : FlxqXQ_minpoly_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
    2058             : {
    2059         219 :   pari_sp ltop = avma;
    2060             :   long vS, vT, n;
    2061             :   GEN v_x, g, tau;
    2062         219 :   vS = get_FlxqX_var(S);
    2063         219 :   vT = get_Flx_var(T);
    2064         219 :   n = get_FlxqX_degree(S);
    2065         219 :   g = pol1_FlxX(vS,vT);
    2066         219 :   tau = pol1_FlxX(vS,vT);
    2067         219 :   S = FlxqX_get_red_pre(S, T, p, pi);
    2068         219 :   v_x = FlxqXQ_powers_pre(x, usqrt(2*n), S, T, p, pi);
    2069         521 :   while(signe(tau) != 0)
    2070             :   {
    2071             :     long i, j, m, k1;
    2072             :     GEN M, v, tr;
    2073             :     GEN g_prime, c;
    2074         302 :     if (degpol(g) == n) { tau = pol1_FlxX(vS, vT); g = pol1_FlxX(vS, vT); }
    2075         302 :     v = random_FlxqX(n, vS, T, p);
    2076         302 :     tr = FlxqXQ_transmul_init(tau, S, T, p, pi);
    2077         302 :     v = FlxqXQ_transmul(tr, v, n, T, p, pi);
    2078         302 :     m = 2*(n-degpol(g));
    2079         302 :     k1 = usqrt(m);
    2080         302 :     tr = FlxqXQ_transmul_init(gel(v_x,k1+1), S, T, p, pi);
    2081         302 :     c = cgetg(m+2,t_POL);
    2082         302 :     c[1] = evalsigne(1)|evalvarn(vS);
    2083        1265 :     for (i=0; i<m; i+=k1)
    2084             :     {
    2085         963 :       long mj = minss(m-i, k1);
    2086        3203 :       for (j=0; j<mj; j++)
    2087        2240 :         gel(c,m+1-(i+j)) = FlxqX_dotproduct(v, gel(v_x,j+1), T, p);
    2088         963 :       v = FlxqXQ_transmul(tr, v, n, T, p, pi);
    2089             :     }
    2090         302 :     c = FlxX_renormalize(c, m+2);
    2091             :     /* now c contains <v,x^i> , i = 0..m-1  */
    2092         302 :     M = FlxqX_halfgcd_pre(polxn_FlxX(m, vS, vT), c, T, p, pi);
    2093         302 :     g_prime = gmael(M, 2, 2);
    2094         302 :     if (degpol(g_prime) < 1) continue;
    2095         261 :     g = FlxqX_mul_pre(g, g_prime, T, p, pi);
    2096         261 :     tau = FlxqXQ_mul_pre(tau, FlxqX_FlxqXQV_eval_pre(g_prime, v_x, S, T,p,pi),
    2097             :                          S, T, p,pi);
    2098             :   }
    2099         219 :   g = FlxqX_normalize_pre(g,T,p,pi);
    2100         219 :   return gerepilecopy(ltop,g);
    2101             : }
    2102             : GEN
    2103          62 : FlxqXQ_minpoly(GEN x, GEN S, GEN T, ulong p)
    2104          62 : { return FlxqXQ_minpoly_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2105             : 
    2106             : GEN
    2107           0 : FlxqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, ulong p)
    2108           0 : { return FlxXV_to_FlxM(FlxqXQ_powers(y,m-1,S,T,p), n, get_Flx_var(T)); }
    2109             : 
    2110             : static GEN
    2111      139211 : FlxX_blocks_FlxM(GEN P, long n, long m, long v)
    2112             : {
    2113      139211 :   GEN z = cgetg(m+1,t_MAT);
    2114      139211 :   long i,j, k=2, l = lg(P);
    2115      535723 :   for(i=1; i<=m; i++)
    2116             :   {
    2117      396512 :     GEN zi = cgetg(n+1,t_COL);
    2118      396512 :     gel(z,i) = zi;
    2119     1269987 :     for(j=1; j<=n; j++)
    2120      873475 :       gel(zi, j) = k==l ? pol0_Flx(v) : gel(P,k++);
    2121             :   }
    2122      139211 :   return z;
    2123             : }
    2124             : 
    2125             : GEN
    2126      139211 : FlxqX_FlxqXQV_eval_pre(GEN Q, GEN x, GEN S, GEN T, ulong p, ulong pi)
    2127             : {
    2128      139211 :   pari_sp btop, av = avma;
    2129      139211 :   long v = get_FlxqX_var(S), m = get_FlxqX_degree(S);
    2130      139211 :   long vT = get_Flx_var(T);
    2131      139211 :   long i, l = lg(x)-1, lQ = lgpol(Q), n,  d;
    2132             :   GEN A, B, C, R, g;
    2133      139211 :   if (lQ == 0) return pol_0(v);
    2134      139211 :   if (lQ <= l)
    2135             :   {
    2136       45221 :     n = l;
    2137       45221 :     d = 1;
    2138             :   }
    2139             :   else
    2140             :   {
    2141       93990 :     n = l-1;
    2142       93990 :     d = (lQ+n-1)/n;
    2143             :   }
    2144      139211 :   A = FlxXV_to_FlxM_lg(x, m, n, vT);
    2145      139211 :   B = FlxX_blocks_FlxM(Q, n, d, vT);
    2146      139211 :   C = gerepileupto(av, FlxqM_mul(A, B, T, p));
    2147      139211 :   g = gel(x, l);
    2148      139211 :   T = Flx_get_red_pre(T, p, pi);
    2149      139211 :   S = FlxqX_get_red_pre(S, T, p, pi);
    2150      139211 :   btop = avma;
    2151      139211 :   R = FlxV_to_FlxX(gel(C, d), v);
    2152      396512 :   for (i = d-1; i>0; i--)
    2153             :   {
    2154      257301 :     R = FlxX_add(FlxqXQ_mul_pre(R, g, S, T,p,pi), FlxV_to_FlxX(gel(C,i), v), p);
    2155      257301 :     if (gc_needed(btop,1))
    2156           6 :       R = gerepileupto(btop, R);
    2157             :   }
    2158      139211 :   return gerepilecopy(av, R);
    2159             : }
    2160             : GEN
    2161           0 : FlxqX_FlxqXQV_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    2162           0 : { return FlxqX_FlxqXQV_eval_pre(Q,x,S,T,p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2163             : 
    2164             : GEN
    2165       76756 : FlxqX_FlxqXQ_eval_pre(GEN Q, GEN x, GEN S, GEN T, ulong p, ulong pi)
    2166             : {
    2167       76756 :   pari_sp av = avma;
    2168             :   GEN z, V;
    2169       76756 :   long d = degpol(Q), rtd;
    2170       76756 :   if (d < 0) return pol_0(get_FlxqX_var(S));
    2171       76756 :   rtd = (long) sqrt((double)d);
    2172       76756 :   T = Flx_get_red_pre(T, p, pi);
    2173       76756 :   S = FlxqX_get_red_pre(S, T, p, pi);
    2174       76756 :   V = FlxqXQ_powers_pre(x, rtd, S, T, p, pi);
    2175       76756 :   z = FlxqX_FlxqXQV_eval_pre(Q, V, S, T, p, pi);
    2176       76756 :   return gerepileupto(av, z);
    2177             : }
    2178             : GEN
    2179           0 : FlxqX_FlxqXQ_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    2180           0 : { return FlxqX_FlxqXQ_eval_pre(Q, x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2181             : 
    2182             : GEN
    2183           0 : FlxqXC_FlxqXQV_eval_pre(GEN x, GEN v, GEN S, GEN T, ulong p, ulong pi)
    2184           0 : { pari_APPLY_type(t_COL, FlxqX_FlxqXQV_eval_pre(gel(x,i), v, S, T, p, pi)) }
    2185             : GEN
    2186           0 : FlxqXC_FlxqXQV_eval(GEN x, GEN v, GEN S, GEN T, ulong p)
    2187           0 : { return FlxqXC_FlxqXQV_eval_pre(x, v, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2188             : 
    2189             : GEN
    2190           0 : FlxqXC_FlxqXQ_eval(GEN x, GEN F, GEN S, GEN T, ulong p)
    2191             : {
    2192           0 :   long d = brent_kung_optpow(get_FlxqX_degree(S)-1,lg(x)-1,1);
    2193           0 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2194           0 :   GEN Fp = FlxqXQ_powers_pre(F, d, S, T, p, pi);
    2195           0 :   return FlxqXC_FlxqXQV_eval_pre(x, Fp, S, T, p, pi);
    2196             : }
    2197             : 
    2198             : static GEN
    2199       72659 : FlxqXQ_autpow_sqr(void * E, GEN x)
    2200             : {
    2201       72659 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    2202       72659 :   GEN S = D->S, T = D->T;
    2203       72659 :   ulong p = D->p, pi = D->pi;
    2204       72659 :   GEN phi = gel(x,1), S1 = gel(x,2);
    2205       72659 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    2206       72659 :   GEN V = Flxq_powers_pre(phi, n, T, p, pi);
    2207       72659 :   GEN phi2 = Flx_FlxqV_eval_pre(phi, V, T, p, pi);
    2208       72659 :   GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V, T, p, pi);
    2209       72659 :   GEN S2 = FlxqX_FlxqXQ_eval_pre(Sphi, S1, S, T, p, pi);
    2210       72659 :   return mkvec2(phi2, S2);
    2211             : }
    2212             : 
    2213             : static GEN
    2214        3735 : FlxqXQ_autpow_mul(void * E, GEN x, GEN y)
    2215             : {
    2216        3735 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    2217        3735 :   GEN S = D->S, T = D->T;
    2218        3735 :   ulong p = D->p, pi = D->pi;
    2219        3735 :   GEN phi1 = gel(x,1), S1 = gel(x,2);
    2220        3735 :   GEN phi2 = gel(y,1), S2 = gel(y,2);
    2221        3735 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    2222        3735 :   GEN V = Flxq_powers_pre(phi2, n, T, p, pi);
    2223        3735 :   GEN phi3 = Flx_FlxqV_eval_pre(phi1, V, T, p, pi);
    2224        3735 :   GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V, T, p, pi);
    2225        3735 :   GEN S3 = FlxqX_FlxqXQ_eval_pre(Sphi, S2, S, T, p, pi);
    2226        3735 :   return mkvec2(phi3, S3);
    2227             : }
    2228             : 
    2229             : GEN
    2230       69296 : FlxqXQ_autpow_pre(GEN aut, long n, GEN S, GEN T, ulong p, ulong pi)
    2231             : {
    2232       69296 :   pari_sp av = avma;
    2233             :   struct _FlxqXQ D;
    2234       69296 :   T = Flx_get_red_pre(T, p, pi);
    2235       69296 :   S = FlxqX_get_red_pre(S, T, p, pi);
    2236       69296 :   D.S = S; D.T = T; D.p = p; D.pi = pi;
    2237       69296 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autpow_sqr,FlxqXQ_autpow_mul);
    2238       69296 :   return gerepilecopy(av, aut);
    2239             : }
    2240             : GEN
    2241           0 : FlxqXQ_autpow(GEN aut, long n, GEN S, GEN T, ulong p)
    2242           0 : { return FlxqXQ_autpow_pre(aut, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2243             : 
    2244             : static GEN
    2245       30404 : FlxqXQ_autsum_mul(void *E, GEN x, GEN y)
    2246             : {
    2247       30404 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    2248       30404 :   GEN S = D->S, T = D->T;
    2249       30404 :   ulong p = D->p, pi = D->pi;
    2250       30404 :   GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
    2251       30404 :   GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
    2252       30404 :   long n2 = brent_kung_optpow(get_Flx_degree(T)-1, lgpol(S1)+lgpol(a1)+1,1);
    2253       30404 :   GEN V2 = Flxq_powers_pre(phi2, n2, T, p, pi);
    2254       30404 :   GEN phi3 = Flx_FlxqV_eval_pre(phi1, V2, T, p, pi);
    2255       30404 :   GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V2, T, p, pi);
    2256       30404 :   GEN aphi = FlxY_FlxqV_evalx_pre(a1, V2, T, p, pi);
    2257       30404 :   long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
    2258       30404 :   GEN V = FlxqXQ_powers_pre(S2, n, S, T, p, pi);
    2259       30404 :   GEN S3 = FlxqX_FlxqXQV_eval_pre(Sphi, V, S, T, p, pi);
    2260       30404 :   GEN aS = FlxqX_FlxqXQV_eval_pre(aphi, V, S, T, p, pi);
    2261       30404 :   GEN a3 = FlxqXQ_mul_pre(aS, a2, S, T, p, pi);
    2262       30404 :   return mkvec3(phi3, S3, a3);
    2263             : }
    2264             : 
    2265             : static GEN
    2266       18817 : FlxqXQ_autsum_sqr(void * T, GEN x)
    2267       18817 : { return FlxqXQ_autsum_mul(T, x, x); }
    2268             : 
    2269             : GEN
    2270       12168 : FlxqXQ_autsum_pre(GEN aut, long n, GEN S, GEN T, ulong p, ulong pi)
    2271             : {
    2272       12168 :   pari_sp av = avma;
    2273             :   struct _FlxqXQ D;
    2274       12168 :   T = Flx_get_red_pre(T, p, pi);
    2275       12168 :   S = FlxqX_get_red_pre(S, T, p, pi);
    2276       12168 :   D.S=S; D.T=T; D.p=p; D.pi=pi;
    2277       12168 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autsum_sqr,FlxqXQ_autsum_mul);
    2278       12168 :   return gerepilecopy(av, aut);
    2279             : }
    2280             : GEN
    2281           0 : FlxqXQ_autsum(GEN aut, long n, GEN S, GEN T, ulong p)
    2282           0 : { return FlxqXQ_autsum_pre(aut, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2283             : 
    2284             : static GEN
    2285          27 : FlxqXQ_auttrace_mul(void *E, GEN x, GEN y)
    2286             : {
    2287          27 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    2288          27 :   GEN S = D->S, T = D->T;
    2289          27 :   ulong p = D->p, pi = D->pi;
    2290          27 :   GEN S1 = gel(x,1), a1 = gel(x,2);
    2291          27 :   GEN S2 = gel(y,1), a2 = gel(y,2);
    2292          27 :   long n = brent_kung_optpow(maxss(degpol(S1),degpol(a1)),2,1);
    2293          27 :   GEN V = FlxqXQ_powers_pre(S2, n, S, T, p, pi);
    2294          27 :   GEN S3 = FlxqX_FlxqXQV_eval_pre(S1, V, S, T, p, pi);
    2295          27 :   GEN aS = FlxqX_FlxqXQV_eval_pre(a1, V, S, T, p, pi);
    2296          27 :   GEN a3 = FlxX_add(aS, a2, p);
    2297          27 :   return mkvec2(S3, a3);
    2298             : }
    2299             : 
    2300             : static GEN
    2301          20 : FlxqXQ_auttrace_sqr(void *E, GEN x)
    2302          20 : { return FlxqXQ_auttrace_mul(E, x, x); }
    2303             : 
    2304             : GEN
    2305         332 : FlxqXQ_auttrace_pre(GEN x, ulong n, GEN S, GEN T, ulong p, ulong pi)
    2306             : {
    2307         332 :   pari_sp av = avma;
    2308             :   struct _FlxqXQ D;
    2309         332 :   T = Flx_get_red_pre(T, p, pi);
    2310         332 :   S = FlxqX_get_red_pre(S, T, p, pi);
    2311         332 :   D.S=S; D.T=T; D.p=p; D.pi = pi;
    2312         332 :   x = gen_powu_i(x,n,(void*)&D,FlxqXQ_auttrace_sqr,FlxqXQ_auttrace_mul);
    2313         332 :   return gerepilecopy(av, x);
    2314             : }
    2315             : GEN
    2316           0 : FlxqXQ_auttrace(GEN x, ulong n, GEN S, GEN T, ulong p)
    2317           0 : { return FlxqXQ_auttrace_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2318             : 
    2319             : /*******************************************************************/
    2320             : /*                                                                 */
    2321             : /*                      FlxYqQ                                     */
    2322             : /*                                                                 */
    2323             : /*******************************************************************/
    2324             : 
    2325             : /*Preliminary implementation to speed up FpX_ffisom*/
    2326             : typedef struct {
    2327             :   GEN S, T;
    2328             :   ulong p, pi;
    2329             : } FlxYqq_muldata;
    2330             : 
    2331             : /* reduce x in Fl[X, Y] in the algebra Fl[X, Y]/ (P(X),Q(Y)) */
    2332             : static GEN
    2333       90730 : FlxYqq_redswap(GEN x, GEN S, GEN T, ulong p, ulong pi)
    2334             : {
    2335       90730 :   pari_sp ltop=avma;
    2336       90730 :   long n = get_Flx_degree(S);
    2337       90730 :   long m = get_Flx_degree(T);
    2338       90730 :   long w = get_Flx_var(T);
    2339       90730 :   GEN V = FlxX_swap(x,m,w);
    2340       90730 :   V = FlxqX_red_pre(V,S,p,pi);
    2341       90728 :   V = FlxX_swap(V,n,w);
    2342       90730 :   return gerepilecopy(ltop,V);
    2343             : }
    2344             : static GEN
    2345       80422 : FlxYqq_sqr(void *data, GEN x)
    2346             : {
    2347       80422 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    2348       80422 :   return FlxYqq_redswap(FlxqX_sqr_pre(x,D->T,D->p,D->pi),D->S,D->T,D->p,D->pi);
    2349             : }
    2350             : 
    2351             : static GEN
    2352       10310 : FlxYqq_mul(void *data, GEN x, GEN y)
    2353             : {
    2354       10310 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    2355       10310 :   return FlxYqq_redswap(FlxqX_mul_pre(x,y, D->T,D->p,D->pi),D->S,D->T,D->p,D->pi);
    2356             : }
    2357             : 
    2358             : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
    2359             : GEN
    2360       39192 : FlxYqq_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    2361             : {
    2362             :   FlxYqq_muldata D;
    2363       39192 :   D.S = S; D.T = T; D.p = p; D.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2364       39192 :   return gen_pow(x, n, (void*)&D, &FlxYqq_sqr, &FlxYqq_mul);
    2365             : }
    2366             : 
    2367             : /*******************************************************************/
    2368             : /*                                                                 */
    2369             : /*                      FlxqXn                                     */
    2370             : /*                                                                 */
    2371             : /*******************************************************************/
    2372             : 
    2373             : GEN
    2374       65473 : FlxXn_red(GEN a, long n)
    2375             : {
    2376       65473 :   long i, L = n+2, l = lg(a);
    2377             :   GEN  b;
    2378       65473 :   if (L >= l) return a; /* deg(x) < n */
    2379       36547 :   b = cgetg(L, t_POL); b[1] = a[1];
    2380      391581 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
    2381       36550 :   return FlxX_renormalize(b,L);
    2382             : }
    2383             : 
    2384             : GEN
    2385       39937 : FlxqXn_mul_pre(GEN a, GEN b, long n, GEN T, ulong p, ulong pi)
    2386       39937 : { return FlxXn_red(FlxqX_mul_pre(a, b, T, p, pi), n); }
    2387             : GEN
    2388           0 : FlxqXn_mul(GEN a, GEN b, long n, GEN T, ulong p)
    2389           0 : { return FlxqXn_mul_pre(a, b, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2390             : 
    2391             : GEN
    2392           0 : FlxqXn_sqr_pre(GEN a, long n, GEN T, ulong p, ulong pi)
    2393           0 : { return FlxXn_red(FlxqX_sqr_pre(a, T, p, pi), n); }
    2394             : GEN
    2395           0 : FlxqXn_sqr(GEN a, long n, GEN T, ulong p)
    2396           0 : { return FlxqXn_sqr_pre(a, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2397             : 
    2398             : /* (f*g) \/ x^n */
    2399             : static GEN
    2400       17767 : FlxqX_mulhigh_i(GEN f, GEN g, long n, GEN T, ulong p, ulong pi)
    2401       17767 : { return FlxX_shift(FlxqX_mul_pre(f, g, T, p, pi), -n , get_Flx_var(T)); }
    2402             : 
    2403             : static GEN
    2404       13026 : FlxqXn_mulhigh(GEN f, GEN g, long n2, long n, GEN T, ulong p, ulong pi)
    2405             : {
    2406       13026 :   long vT = get_Flx_var(T);
    2407       13026 :   GEN F = FlxX_blocks(f, n2, 2, vT), fl = gel(F,1), fh = gel(F,2);
    2408       13028 :   return FlxX_add(FlxqX_mulhigh_i(fl, g, n2, T, p, pi),
    2409             :                   FlxqXn_mul_pre(fh, g, n - n2, T, p, pi), p);
    2410             : }
    2411             : 
    2412             : GEN
    2413        2390 : FlxqXn_inv_pre(GEN f, long e, GEN T, ulong p, ulong pi)
    2414             : {
    2415        2390 :   pari_sp av = avma, av2;
    2416             :   ulong mask;
    2417             :   GEN W, a;
    2418        2390 :   long v = varn(f), n = 1, vT = get_Flx_var(T);
    2419             : 
    2420        2390 :   if (!signe(f)) pari_err_INV("FlxqXn_inv",f);
    2421        2390 :   a = Flxq_inv_pre(gel(f,2), T, p, pi);
    2422        2390 :   if (e == 1) return scalarpol(a, v);
    2423        2390 :   else if (e == 2)
    2424             :   {
    2425             :     GEN b;
    2426           0 :     if (degpol(f) <= 0) return scalarpol(a, v);
    2427           0 :     b = Flx_neg(gel(f,3), p);
    2428           0 :     if (lgpol(b)==0) return scalarpol(a, v);
    2429           0 :     b = Flxq_mul_pre(b, Flxq_sqr_pre(a, T, p, pi), T, p, pi);
    2430           0 :     W = deg1pol_shallow(b, a, v);
    2431           0 :     return gerepilecopy(av, W);
    2432             :   }
    2433        2390 :   W = scalarpol_shallow(Flxq_inv_pre(gel(f,2), T, p, pi), v);
    2434        2390 :   mask = quadratic_prec_mask(e);
    2435        2390 :   av2 = avma;
    2436       11870 :   for (;mask>1;)
    2437             :   {
    2438             :     GEN u, fr;
    2439        9480 :     long n2 = n;
    2440        9480 :     n<<=1; if (mask & 1) n--;
    2441        9480 :     mask >>= 1;
    2442        9480 :     fr = FlxXn_red(f, n);
    2443        9480 :     u = FlxqXn_mul_pre(W, FlxqXn_mulhigh(fr, W, n2, n, T,p,pi), n-n2, T,p,pi);
    2444        9482 :     W = FlxX_sub(W, FlxX_shift(u, n2, vT), p);
    2445        9480 :     if (gc_needed(av2,2))
    2446             :     {
    2447           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_inv, e = %ld", n);
    2448           0 :       W = gerepileupto(av2, W);
    2449             :     }
    2450             :   }
    2451        2390 :   return gerepileupto(av, W);
    2452             : }
    2453             : GEN
    2454           0 : FlxqXn_inv(GEN f, long e, GEN T, ulong p)
    2455           0 : { return FlxqXn_inv_pre(f, e, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2456             : 
    2457             : /* Compute intformal(x^n*S)/x^(n+1) */
    2458             : static GEN
    2459        4741 : FlxX_integXn(GEN x, long n, ulong p)
    2460             : {
    2461        4741 :   long i, lx = lg(x);
    2462             :   GEN y;
    2463        4741 :   if (lx == 2) return gcopy(x);
    2464        3949 :   y = cgetg(lx, t_POL); y[1] = x[1];
    2465       26598 :   for (i=2; i<lx; i++)
    2466             :   {
    2467       22649 :     GEN xi = gel(x,i);
    2468       22649 :     gel(y,i) = Flx_Fl_mul(xi, Fl_inv((n+i-1)%p, p), p);
    2469             :   }
    2470        3949 :   return FlxX_renormalize(y, lx);;
    2471             : }
    2472             : 
    2473             : GEN
    2474        1195 : FlxqXn_expint_pre(GEN h, long e, GEN T, ulong p, ulong pi)
    2475             : {
    2476        1195 :   pari_sp av = avma, av2;
    2477        1195 :   long v = varn(h), n = 1, vT = get_Flx_var(T);
    2478        1195 :   GEN f = pol1_FlxX(v, vT), g = pol1_FlxX(v, vT);
    2479        1195 :   ulong mask = quadratic_prec_mask(e);
    2480        1195 :   av2 = avma;
    2481        4741 :   for (;mask>1;)
    2482             :   {
    2483             :     GEN u, w;
    2484        4741 :     long n2 = n;
    2485        4741 :     n<<=1; if (mask & 1) n--;
    2486        4741 :     mask >>= 1;
    2487        4741 :     u = FlxqXn_mul_pre(g, FlxqX_mulhigh_i(f, FlxXn_red(h, n2-1), n2-1, T,p,pi), n-n2, T,p,pi);
    2488        4741 :     u = FlxX_add(u, FlxX_shift(FlxXn_red(h, n-1), 1-n2, vT), p);
    2489        4741 :     w = FlxqXn_mul_pre(f, FlxX_integXn(u, n2-1, p), n-n2, T, p, pi);
    2490        4741 :     f = FlxX_add(f, FlxX_shift(w, n2, vT), p);
    2491        4741 :     if (mask<=1) break;
    2492        3546 :     u = FlxqXn_mul_pre(g, FlxqXn_mulhigh(f, g, n2, n, T,p,pi), n-n2, T,p,pi);
    2493        3546 :     g = FlxX_sub(g, FlxX_shift(u, n2, vT), p);
    2494        3546 :     if (gc_needed(av2,2))
    2495             :     {
    2496           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_exp, e = %ld", n);
    2497           0 :       gerepileall(av2, 2, &f, &g);
    2498             :     }
    2499             :   }
    2500        1195 :   return gerepileupto(av, f);
    2501             : }
    2502             : GEN
    2503           0 : FlxqXn_expint(GEN h, long e, GEN T, ulong p)
    2504           0 : { return FlxqXn_expint_pre(h, e, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }

Generated by: LCOV version 1.14