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 - Flx.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.0 lcov report (development 29732-95c6201d93) Lines: 2402 2712 88.6 %
Date: 2024-11-21 09:08:54 Functions: 296 342 86.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2004  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             : /* Not so fast arithmetic with polynomials with small coefficients. */
      19             : 
      20             : static GEN
      21   980281095 : get_Flx_red(GEN T, GEN *B)
      22             : {
      23   980281095 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
      24      681148 :   *B = gel(T,1); return gel(T,2);
      25             : }
      26             : 
      27             : /***********************************************************************/
      28             : /**                              Flx                                  **/
      29             : /***********************************************************************/
      30             : /* Flx objects are defined as follows:
      31             :  * Let l an ulong. An Flx is a t_VECSMALL:
      32             :  * x[0] = codeword
      33             :  * x[1] = evalvarn(variable number)  (signe is not stored).
      34             :  * x[2] = a_0 x[3] = a_1, etc. with 0 <= a_i < l
      35             :  *
      36             :  * signe(x) is not valid. Use degpol(x)>0 instead. */
      37             : /***********************************************************************/
      38             : /**                      Conversion from Flx                          **/
      39             : /***********************************************************************/
      40             : 
      41             : GEN
      42    37063937 : Flx_to_ZX(GEN z)
      43             : {
      44    37063937 :   long i, l = lg(z);
      45    37063937 :   GEN x = cgetg(l,t_POL);
      46   241970616 :   for (i=2; i<l; i++) gel(x,i) = utoi(z[i]);
      47    37050817 :   x[1] = evalsigne(l-2!=0)| z[1]; return x;
      48             : }
      49             : 
      50             : GEN
      51       71517 : Flx_to_FlxX(GEN z, long sv)
      52             : {
      53       71517 :   long i, l = lg(z);
      54       71517 :   GEN x = cgetg(l,t_POL);
      55      278630 :   for (i=2; i<l; i++) gel(x,i) = Fl_to_Flx(z[i], sv);
      56       71516 :   x[1] = evalsigne(l-2!=0)| z[1]; return x;
      57             : }
      58             : 
      59             : /* same as Flx_to_ZX, in place */
      60             : GEN
      61    36781374 : Flx_to_ZX_inplace(GEN z)
      62             : {
      63    36781374 :   long i, l = lg(z);
      64   228556539 :   for (i=2; i<l; i++) gel(z,i) = utoi(z[i]);
      65    36773411 :   settyp(z, t_POL); z[1]=evalsigne(l-2!=0)|z[1]; return z;
      66             : }
      67             : 
      68             : /*Flx_to_Flv=zx_to_zv*/
      69             : GEN
      70    63883543 : Flx_to_Flv(GEN x, long N)
      71             : {
      72    63883543 :   GEN z = cgetg(N+1,t_VECSMALL);
      73    63877545 :   long i, l = lg(x)-1;
      74    63877545 :   x++;
      75   695556073 :   for (i=1; i<l ; i++) z[i]=x[i];
      76   325067559 :   for (   ; i<=N; i++) z[i]=0;
      77    63877545 :   return z;
      78             : }
      79             : 
      80             : /*Flv_to_Flx=zv_to_zx*/
      81             : GEN
      82    26922197 : Flv_to_Flx(GEN x, long sv)
      83             : {
      84    26922197 :   long i, l=lg(x)+1;
      85    26922197 :   GEN z = cgetg(l,t_VECSMALL); z[1]=sv;
      86    26917739 :   x--;
      87   292347054 :   for (i=2; i<l ; i++) z[i]=x[i];
      88    26917739 :   return Flx_renormalize(z,l);
      89             : }
      90             : 
      91             : /*Flm_to_FlxV=zm_to_zxV*/
      92             : GEN
      93        2282 : Flm_to_FlxV(GEN x, long sv)
      94        6216 : { pari_APPLY_type(t_VEC, Flv_to_Flx(gel(x,i), sv)) }
      95             : 
      96             : /*FlxC_to_ZXC=zxC_to_ZXC*/
      97             : GEN
      98      109530 : FlxC_to_ZXC(GEN x)
      99      563775 : { pari_APPLY_type(t_COL, Flx_to_ZX(gel(x,i))) }
     100             : 
     101             : /*FlxC_to_ZXC=zxV_to_ZXV*/
     102             : GEN
     103      600410 : FlxV_to_ZXV(GEN x)
     104     2429208 : { pari_APPLY_type(t_VEC, Flx_to_ZX(gel(x,i))) }
     105             : 
     106             : void
     107     2923739 : FlxV_to_ZXV_inplace(GEN v)
     108             : {
     109             :   long i;
     110     7768888 :   for(i=1;i<lg(v);i++) gel(v,i)= Flx_to_ZX(gel(v,i));
     111     2923652 : }
     112             : 
     113             : /*FlxM_to_ZXM=zxM_to_ZXM*/
     114             : GEN
     115        4605 : FlxM_to_ZXM(GEN x)
     116       15893 : { pari_APPLY_same(FlxC_to_ZXC(gel(x,i))) }
     117             : 
     118             : GEN
     119      396559 : FlxV_to_FlxX(GEN x, long v)
     120             : {
     121      396559 :   long i, l = lg(x)+1;
     122      396559 :   GEN z = cgetg(l,t_POL); z[1] = evalvarn(v);
     123      396559 :   x--;
     124     4970439 :   for (i=2; i<l ; i++) gel(z,i) = gel(x,i);
     125      396559 :   return FlxX_renormalize(z,l);
     126             : }
     127             : 
     128             : GEN
     129           0 : FlxM_to_FlxXV(GEN x, long v)
     130           0 : { pari_APPLY_type(t_COL, FlxV_to_FlxX(gel(x,i), v)) }
     131             : 
     132             : GEN
     133           0 : FlxM_Flx_add_shallow(GEN x, GEN y, ulong p)
     134             : {
     135           0 :   long l = lg(x), i, j;
     136           0 :   GEN z = cgetg(l,t_MAT);
     137             : 
     138           0 :   if (l==1) return z;
     139           0 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     140           0 :   for (i=1; i<l; i++)
     141             :   {
     142           0 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     143           0 :     gel(z,i) = zi;
     144           0 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     145           0 :     gel(zi,i) = Flx_add(gel(zi,i), y, p);
     146             :   }
     147           0 :   return z;
     148             : }
     149             : 
     150             : /***********************************************************************/
     151             : /**                      Conversion to Flx                            **/
     152             : /***********************************************************************/
     153             : /* Take an integer and return a scalar polynomial mod p,  with evalvarn=vs */
     154             : GEN
     155    19846797 : Fl_to_Flx(ulong x, long sv) { return x? mkvecsmall2(sv, x): pol0_Flx(sv); }
     156             : 
     157             : /* a X^d */
     158             : GEN
     159      911957 : monomial_Flx(ulong a, long d, long vs)
     160             : {
     161             :   GEN P;
     162      911957 :   if (a==0) return pol0_Flx(vs);
     163      911957 :   P = const_vecsmall(d+2, 0);
     164      911964 :   P[1] = vs; P[d+2] = a; return P;
     165             : }
     166             : 
     167             : GEN
     168     2673322 : Z_to_Flx(GEN x, ulong p, long sv)
     169             : {
     170     2673322 :   long u = umodiu(x,p);
     171     2673309 :   return u? mkvecsmall2(sv, u): pol0_Flx(sv);
     172             : }
     173             : 
     174             : /* return x[0 .. dx] mod p as t_VECSMALL. Assume x a t_POL*/
     175             : GEN
     176   167538052 : ZX_to_Flx(GEN x, ulong p)
     177             : {
     178   167538052 :   long i, lx = lg(x);
     179   167538052 :   GEN a = cgetg(lx, t_VECSMALL);
     180   167490775 :   a[1]=((ulong)x[1])&VARNBITS;
     181  1110158155 :   for (i=2; i<lx; i++) a[i] = umodiu(gel(x,i), p);
     182   167498172 :   return Flx_renormalize(a,lx);
     183             : }
     184             : 
     185             : /* return x[0 .. dx] mod p as t_VECSMALL. Assume x a t_POL*/
     186             : GEN
     187     6068351 : zx_to_Flx(GEN x, ulong p)
     188             : {
     189     6068351 :   long i, lx = lg(x);
     190     6068351 :   GEN a = cgetg(lx, t_VECSMALL);
     191     6063034 :   a[1] = x[1];
     192    18618519 :   for (i=2; i<lx; i++) uel(a,i) = umodsu(x[i], p);
     193     6062894 :   return Flx_renormalize(a,lx);
     194             : }
     195             : 
     196             : ulong
     197    74783364 : Rg_to_Fl(GEN x, ulong p)
     198             : {
     199    74783364 :   switch(typ(x))
     200             :   {
     201    47740767 :     case t_INT: return umodiu(x, p);
     202      457594 :     case t_FRAC: {
     203      457594 :       ulong z = umodiu(gel(x,1), p);
     204      457598 :       if (!z) return 0;
     205      446227 :       return Fl_div(z, umodiu(gel(x,2), p), p);
     206             :     }
     207      205954 :     case t_PADIC: return padic_to_Fl(x, p);
     208    26379059 :     case t_INTMOD: {
     209    26379059 :       GEN q = gel(x,1), a = gel(x,2);
     210    26379059 :       if (absequaliu(q, p)) return itou(a);
     211           0 :       if (!dvdiu(q,p)) pari_err_MODULUS("Rg_to_Fl", q, utoipos(p));
     212           0 :       return umodiu(a, p);
     213             :     }
     214           0 :     default: pari_err_TYPE("Rg_to_Fl",x);
     215             :       return 0; /* LCOV_EXCL_LINE */
     216             :   }
     217             : }
     218             : 
     219             : ulong
     220     1706855 : Rg_to_F2(GEN x)
     221             : {
     222     1706855 :   switch(typ(x))
     223             :   {
     224      274242 :     case t_INT: return mpodd(x);
     225           0 :     case t_FRAC:
     226           0 :       if (!mpodd(gel(x,2))) (void)Fl_inv(0,2); /* error */
     227           0 :       return mpodd(gel(x,1));
     228           0 :     case t_PADIC:
     229           0 :       if (!absequaliu(gel(x,2),2)) pari_err_OP("",x, mkintmodu(1,2));
     230           0 :       if (valp(x) < 0) (void)Fl_inv(0,2);
     231           0 :       return valp(x) & 1;
     232     1432613 :     case t_INTMOD: {
     233     1432613 :       GEN q = gel(x,1), a = gel(x,2);
     234     1432613 :       if (mpodd(q)) pari_err_MODULUS("Rg_to_F2", q, gen_2);
     235     1432613 :       return mpodd(a);
     236             :     }
     237           0 :     default: pari_err_TYPE("Rg_to_F2",x);
     238             :       return 0; /* LCOV_EXCL_LINE */
     239             :   }
     240             : }
     241             : 
     242             : GEN
     243     2826392 : RgX_to_Flx(GEN x, ulong p)
     244             : {
     245     2826392 :   long i, lx = lg(x);
     246     2826392 :   GEN a = cgetg(lx, t_VECSMALL);
     247     2826392 :   a[1]=((ulong)x[1])&VARNBITS;
     248    22238943 :   for (i=2; i<lx; i++) a[i] = Rg_to_Fl(gel(x,i), p);
     249     2826392 :   return Flx_renormalize(a,lx);
     250             : }
     251             : 
     252             : GEN
     253           7 : RgXV_to_FlxV(GEN x, ulong p)
     254         175 : { pari_APPLY_type(t_VEC, RgX_to_Flx(gel(x,i), p)) }
     255             : 
     256             : /* If x is a POLMOD, assume modulus is a multiple of T. */
     257             : GEN
     258     3565067 : Rg_to_Flxq(GEN x, GEN T, ulong p)
     259             : {
     260     3565067 :   long ta, tx = typ(x), v = get_Flx_var(T);
     261             :   ulong pi;
     262             :   GEN a, b;
     263     3565067 :   if (is_const_t(tx))
     264             :   {
     265     3314547 :     if (tx == t_FFELT) return FF_to_Flxq(x);
     266     2583539 :     return Fl_to_Flx(Rg_to_Fl(x, p), v);
     267             :   }
     268      250519 :   switch(tx)
     269             :   {
     270        8576 :     case t_POLMOD:
     271        8576 :       b = gel(x,1);
     272        8576 :       a = gel(x,2); ta = typ(a);
     273        8576 :       if (is_const_t(ta)) return Fl_to_Flx(Rg_to_Fl(a, p), v);
     274        8422 :       b = RgX_to_Flx(b, p); if (b[1] != v) break;
     275        8422 :       a = RgX_to_Flx(a, p); if (Flx_equal(b,T)) return a;
     276           0 :       pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
     277           0 :       if (lgpol(Flx_rem_pre(b,T,p,pi))==0) return Flx_rem_pre(a, T, p, pi);
     278           0 :       break;
     279      241943 :     case t_POL:
     280      241943 :       x = RgX_to_Flx(x,p);
     281      241943 :       if (x[1] != v) break;
     282      241943 :       return Flx_rem(x, T, p);
     283           0 :     case t_RFRAC:
     284           0 :       a = Rg_to_Flxq(gel(x,1), T,p);
     285           0 :       b = Rg_to_Flxq(gel(x,2), T,p);
     286           0 :       return Flxq_div(a,b, T,p);
     287             :   }
     288           0 :   pari_err_TYPE("Rg_to_Flxq",x);
     289             :   return NULL; /* LCOV_EXCL_LINE */
     290             : }
     291             : 
     292             : /***********************************************************************/
     293             : /**                   Basic operation on Flx                          **/
     294             : /***********************************************************************/
     295             : /* = zx_renormalize. Similar to normalizepol, in place */
     296             : GEN
     297  2135538724 : Flx_renormalize(GEN /*in place*/ x, long lx)
     298             : {
     299             :   long i;
     300  2394566848 :   for (i = lx-1; i>1; i--)
     301  2299035827 :     if (x[i]) break;
     302  2135538724 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     303  2134442003 :   setlg(x, i+1); return x;
     304             : }
     305             : 
     306             : GEN
     307     1875707 : Flx_red(GEN z, ulong p)
     308             : {
     309     1875707 :   long i, l = lg(z);
     310     1875707 :   GEN x = cgetg(l, t_VECSMALL);
     311     1875523 :   x[1] = z[1];
     312    33492335 :   for (i=2; i<l; i++) x[i] = uel(z,i)%p;
     313     1875523 :   return Flx_renormalize(x,l);
     314             : }
     315             : 
     316             : int
     317    28983107 : Flx_equal(GEN V, GEN W)
     318             : {
     319    28983107 :   long l = lg(V);
     320    28983107 :   if (lg(W) != l) return 0;
     321    30058526 :   while (--l > 1) /* do not compare variables, V[1] */
     322    28804642 :     if (V[l] != W[l]) return 0;
     323     1253884 :   return 1;
     324             : }
     325             : 
     326             : GEN
     327     2663572 : random_Flx(long d1, long vs, ulong p)
     328             : {
     329     2663572 :   long i, d = d1+2;
     330     2663572 :   GEN y = cgetg(d,t_VECSMALL); y[1] = vs;
     331    18388493 :   for (i=2; i<d; i++) y[i] = random_Fl(p);
     332     2663738 :   return Flx_renormalize(y,d);
     333             : }
     334             : 
     335             : static GEN
     336     7204967 : Flx_addspec(GEN x, GEN y, ulong p, long lx, long ly)
     337             : {
     338             :   long i,lz;
     339             :   GEN z;
     340             : 
     341     7204967 :   if (ly>lx) swapspec(x,y, lx,ly);
     342     7204967 :   lz = lx+2; z = cgetg(lz, t_VECSMALL);
     343   107914501 :   for (i=0; i<ly; i++) z[i+2] = Fl_add(x[i], y[i], p);
     344    91314517 :   for (   ; i<lx; i++) z[i+2] = x[i];
     345     7204967 :   z[1] = 0; return Flx_renormalize(z, lz);
     346             : }
     347             : 
     348             : GEN
     349    63669298 : Flx_add(GEN x, GEN y, ulong p)
     350             : {
     351             :   long i,lz;
     352             :   GEN z;
     353    63669298 :   long lx=lg(x);
     354    63669298 :   long ly=lg(y);
     355    63669298 :   if (ly>lx) swapspec(x,y, lx,ly);
     356    63669298 :   lz = lx; z = cgetg(lz, t_VECSMALL); z[1]=x[1];
     357   577069536 :   for (i=2; i<ly; i++) z[i] = Fl_add(x[i], y[i], p);
     358   132940496 :   for (   ; i<lx; i++) z[i] = x[i];
     359    63646902 :   return Flx_renormalize(z, lz);
     360             : }
     361             : 
     362             : GEN
     363     9687048 : Flx_Fl_add(GEN y, ulong x, ulong p)
     364             : {
     365             :   GEN z;
     366             :   long lz, i;
     367     9687048 :   if (!lgpol(y))
     368      228768 :     return Fl_to_Flx(x,y[1]);
     369     9459498 :   lz=lg(y);
     370     9459498 :   z=cgetg(lz,t_VECSMALL);
     371     9458594 :   z[1]=y[1];
     372     9458594 :   z[2] = Fl_add(y[2],x,p);
     373    46071073 :   for(i=3;i<lz;i++)
     374    36612859 :     z[i] = y[i];
     375     9458214 :   if (lz==3) z = Flx_renormalize(z,lz);
     376     9457940 :   return z;
     377             : }
     378             : 
     379             : static GEN
     380      896000 : Flx_subspec(GEN x, GEN y, ulong p, long lx, long ly)
     381             : {
     382             :   long i,lz;
     383             :   GEN z;
     384             : 
     385      896000 :   if (ly <= lx)
     386             :   {
     387      896859 :     lz = lx+2; z = cgetg(lz, t_VECSMALL);
     388    53765514 :     for (i=0; i<ly; i++) z[i+2] = Fl_sub(x[i],y[i],p);
     389     1447317 :     for (   ; i<lx; i++) z[i+2] = x[i];
     390             :   }
     391             :   else
     392             :   {
     393           0 :     lz = ly+2; z = cgetg(lz, t_VECSMALL);
     394           0 :     for (i=0; i<lx; i++) z[i+2] = Fl_sub(x[i],y[i],p);
     395           0 :     for (   ; i<ly; i++) z[i+2] = Fl_neg(y[i],p);
     396             :   }
     397      896625 :   z[1] = 0; return Flx_renormalize(z, lz);
     398             : }
     399             : 
     400             : GEN
     401   139450490 : Flx_sub(GEN x, GEN y, ulong p)
     402             : {
     403   139450490 :   long i,lz,lx = lg(x), ly = lg(y);
     404             :   GEN z;
     405             : 
     406   139450490 :   if (ly <= lx)
     407             :   {
     408    87903167 :     lz = lx; z = cgetg(lz, t_VECSMALL);
     409   460163816 :     for (i=2; i<ly; i++) z[i] = Fl_sub(x[i],y[i],p);
     410   176054697 :     for (   ; i<lx; i++) z[i] = x[i];
     411             :   }
     412             :   else
     413             :   {
     414    51547323 :     lz = ly; z = cgetg(lz, t_VECSMALL);
     415   267816412 :     for (i=2; i<lx; i++) z[i] = Fl_sub(x[i],y[i],p);
     416   238640123 :     for (   ; i<ly; i++) z[i] = y[i]? (long)(p - y[i]): y[i];
     417             :   }
     418   139442420 :   z[1]=x[1]; return Flx_renormalize(z, lz);
     419             : }
     420             : 
     421             : GEN
     422      151009 : Flx_Fl_sub(GEN y, ulong x, ulong p)
     423             : {
     424             :   GEN z;
     425      151009 :   long lz = lg(y), i;
     426      151009 :   if (lz==2)
     427         513 :     return Fl_to_Flx(Fl_neg(x, p),y[1]);
     428      150496 :   z = cgetg(lz, t_VECSMALL);
     429      150496 :   z[1] = y[1];
     430      150496 :   z[2] = Fl_sub(uel(y,2), x, p);
     431      748438 :   for(i=3; i<lz; i++)
     432      597942 :     z[i] = y[i];
     433      150496 :   if (lz==3) z = Flx_renormalize(z,lz);
     434      150496 :   return z;
     435             : }
     436             : 
     437             : static GEN
     438     3451806 : Flx_negspec(GEN x, ulong p, long l)
     439             : {
     440             :   long i;
     441     3451806 :   GEN z = cgetg(l+2, t_VECSMALL) + 2;
     442    21232181 :   for (i=0; i<l; i++) z[i] = Fl_neg(x[i], p);
     443     3451878 :   return z-2;
     444             : }
     445             : 
     446             : GEN
     447     3451796 : Flx_neg(GEN x, ulong p)
     448             : {
     449     3451796 :   GEN z = Flx_negspec(x+2, p, lgpol(x));
     450     3451949 :   z[1] = x[1];
     451     3451949 :   return z;
     452             : }
     453             : 
     454             : GEN
     455     1766287 : Flx_neg_inplace(GEN x, ulong p)
     456             : {
     457     1766287 :   long i, l = lg(x);
     458    53018517 :   for (i=2; i<l; i++)
     459    51252230 :     if (x[i]) x[i] = p - x[i];
     460     1766287 :   return x;
     461             : }
     462             : 
     463             : GEN
     464     2434345 : Flx_double(GEN y, ulong p)
     465             : {
     466             :   long i, l;
     467     2434345 :   GEN z = cgetg_copy(y, &l); z[1] = y[1];
     468    20625128 :   for(i=2; i<l; i++) z[i] = Fl_double(y[i], p);
     469     2434345 :   return Flx_renormalize(z, l);
     470             : }
     471             : GEN
     472     1043249 : Flx_triple(GEN y, ulong p)
     473             : {
     474             :   long i, l;
     475     1043249 :   GEN z = cgetg_copy(y, &l); z[1] = y[1];
     476     8299121 :   for(i=2; i<l; i++) z[i] = Fl_triple(y[i], p);
     477     1043249 :   return Flx_renormalize(z, l);
     478             : }
     479             : 
     480             : GEN
     481    18845144 : Flx_Fl_mul_pre(GEN y, ulong x, ulong p, ulong pi)
     482             : {
     483             :   GEN z;
     484             :   long i, l;
     485    18845144 :   if (!x) return pol0_Flx(y[1]);
     486    18064128 :   z = cgetg_copy(y, &l); z[1] = y[1];
     487    18064178 :   if (pi==0)
     488             :   {
     489    15906183 :     if (HIGHWORD(x | p))
     490           0 :       for(i=2; i<l; i++) z[i] = Fl_mul(uel(y,i), x, p);
     491             :     else
     492    95034419 :       for(i=2; i<l; i++) z[i] = (uel(y,i) * x) % p;
     493             :   } else
     494    17896872 :       for(i=2; i<l; i++) z[i] = Fl_mul_pre(uel(y,i), x, p, pi);
     495    18065181 :   return Flx_renormalize(z, l);
     496             : }
     497             : 
     498             : GEN
     499     7115563 : Flx_Fl_mul(GEN x, ulong y, ulong p)
     500     7115563 : { return Flx_Fl_mul_pre(x, y, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
     501             : 
     502             : GEN
     503           0 : Flx_convol(GEN x, GEN y, ulong p)
     504             : {
     505           0 :   long lx = lg(x), ly = lg(y), i;
     506             :   GEN z;
     507           0 :   if (lx < ly) swapspec(x,y, lx,ly);
     508           0 :   z = cgetg(ly,t_VECSMALL); z[1] = x[1];
     509           0 :   for (i=2; i<ly; i++) uel(z,i) = Fl_mul(uel(x,i),uel(y,i), p);
     510           0 :   return Flx_renormalize(z, ly);
     511             : }
     512             : 
     513             : GEN
     514    11942517 : Flx_Fl_mul_to_monic(GEN y, ulong x, ulong p)
     515             : {
     516             :   GEN z;
     517             :   long i, l;
     518    11942517 :   z = cgetg_copy(y, &l); z[1] = y[1];
     519    11939470 :   if (HIGHWORD(x | p))
     520     5398621 :     for(i=2; i<l-1; i++) z[i] = Fl_mul(y[i], x, p);
     521             :   else
     522    26788795 :     for(i=2; i<l-1; i++) z[i] = (y[i] * x) % p;
     523    11939464 :   z[l-1] = 1; return z;
     524             : }
     525             : 
     526             : /* Return a*x^n if n>=0 and a\x^(-n) if n<0 */
     527             : GEN
     528    26728887 : Flx_shift(GEN a, long n)
     529             : {
     530    26728887 :   long i, l = lg(a);
     531             :   GEN  b;
     532    26728887 :   if (l==2 || !n) return Flx_copy(a);
     533    26387439 :   if (l+n<=2) return pol0_Flx(a[1]);
     534    26173104 :   b = cgetg(l+n, t_VECSMALL);
     535    26171029 :   b[1] = a[1];
     536    26171029 :   if (n < 0)
     537    71621206 :     for (i=2-n; i<l; i++) b[i+n] = a[i];
     538             :   else
     539             :   {
     540    50749663 :     for (i=0; i<n; i++) b[2+i] = 0;
     541   147743505 :     for (i=2; i<l; i++) b[i+n] = a[i];
     542             :   }
     543    26171029 :   return b;
     544             : }
     545             : 
     546             : GEN
     547    62110864 : Flx_normalize(GEN z, ulong p)
     548             : {
     549    62110864 :   long l = lg(z)-1;
     550    62110864 :   ulong p1 = z[l]; /* leading term */
     551    62110864 :   if (p1 == 1) return z;
     552    11920092 :   return Flx_Fl_mul_to_monic(z, Fl_inv(p1,p), p);
     553             : }
     554             : 
     555             : /* return (x * X^d) + y. Assume d > 0, shallow if x == 0*/
     556             : static GEN
     557     3701783 : Flx_addshift(GEN x, GEN y, ulong p, long d)
     558             : {
     559     3701783 :   GEN xd,yd,zd = (GEN)avma;
     560     3701783 :   long a,lz,ny = lgpol(y), nx = lgpol(x);
     561     3701783 :   long vs = x[1];
     562     3701783 :   if (nx == 0) return y;
     563     3699931 :   x += 2; y += 2; a = ny-d;
     564     3699931 :   if (a <= 0)
     565             :   {
     566       85126 :     lz = (a>nx)? ny+2: nx+d+2;
     567       85126 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
     568     1733197 :     while (xd > x) *--zd = *--xd;
     569       85126 :     x = zd + a;
     570      166548 :     while (zd > x) *--zd = 0;
     571             :   }
     572             :   else
     573             :   {
     574     3614805 :     xd = new_chunk(d); yd = y+d;
     575     3614805 :     x = Flx_addspec(x,yd,p, nx,a);
     576     3614805 :     lz = (a>nx)? ny+2: lg(x)+d;
     577   134401229 :     x += 2; while (xd > x) *--zd = *--xd;
     578             :   }
     579    61062442 :   while (yd > y) *--zd = *--yd;
     580     3699931 :   *--zd = vs;
     581     3699931 :   *--zd = evaltyp(t_VECSMALL) | evallg(lz); return zd;
     582             : }
     583             : 
     584             : /* shift polynomial + gerepile */
     585             : /* Do not set evalvarn*/
     586             : static GEN
     587   633829026 : Flx_shiftip(pari_sp av, GEN x, long v)
     588             : {
     589   633829026 :   long i, lx = lg(x), ly;
     590             :   GEN y;
     591   633829026 :   if (!v || lx==2) return gerepileuptoleaf(av, x);
     592   175684706 :   ly = lx + v; /* result length */
     593   175684706 :   (void)new_chunk(ly); /* check that result fits */
     594   175584278 :   x += lx; y = (GEN)av;
     595  1242415412 :   for (i = 2; i<lx; i++) *--y = *--x;
     596   707380960 :   for (i = 0; i< v; i++) *--y = 0;
     597   175584278 :   y -= 2; y[0] = evaltyp(t_VECSMALL) | evallg(ly);
     598   175733957 :   return gc_const((pari_sp)y, y);
     599             : }
     600             : 
     601             : static long
     602  2317569373 : get_Fl_threshold(ulong p, long mul, long mul2)
     603             : {
     604  2317569373 :   return SMALL_ULONG(p) ? mul: mul2;
     605             : }
     606             : 
     607             : #define BITS_IN_QUARTULONG (BITS_IN_HALFULONG >> 1)
     608             : #define QUARTMASK ((1UL<<BITS_IN_QUARTULONG)-1UL)
     609             : #define LLQUARTWORD(x) ((x) & QUARTMASK)
     610             : #define HLQUARTWORD(x) (((x) >> BITS_IN_QUARTULONG) & QUARTMASK)
     611             : #define LHQUARTWORD(x) (((x) >> (2*BITS_IN_QUARTULONG)) & QUARTMASK)
     612             : #define HHQUARTWORD(x) (((x) >> (3*BITS_IN_QUARTULONG)) & QUARTMASK)
     613             : INLINE long
     614     8384508 : maxbitcoeffpol(ulong p, long n)
     615             : {
     616     8384508 :   GEN z = muliu(sqru(p - 1), n);
     617     8381369 :   long b = expi(z) + 1;
     618             :   /* only do expensive bit-packing if it saves at least 1 limb */
     619     8382188 :   if (b <= BITS_IN_QUARTULONG)
     620             :   {
     621      926286 :     if (nbits2nlong(n*b) == (n + 3)>>2)
     622      107330 :       b = BITS_IN_QUARTULONG;
     623             :   }
     624     7455902 :   else if (b <= BITS_IN_HALFULONG)
     625             :   {
     626     1549076 :     if (nbits2nlong(n*b) == (n + 1)>>1)
     627        5590 :       b = BITS_IN_HALFULONG;
     628             :   }
     629             :   else
     630             :   {
     631     5906826 :     long l = lgefint(z) - 2;
     632     5906826 :     if (nbits2nlong(n*b) == n*l)
     633      307181 :       b = l*BITS_IN_LONG;
     634             :   }
     635     8381963 :   return b;
     636             : }
     637             : 
     638             : INLINE ulong
     639  3404311068 : Flx_mullimb_ok(GEN x, GEN y, ulong p, long a, long b)
     640             : { /* Assume OK_ULONG*/
     641  3404311068 :   ulong p1 = 0;
     642             :   long i;
     643 16194054513 :   for (i=a; i<b; i++)
     644 12789743445 :     if (y[i])
     645             :     {
     646 10734706194 :       p1 += y[i] * x[-i];
     647 10734706194 :       if (p1 & HIGHBIT) p1 %= p;
     648             :     }
     649  3404311068 :   return p1 % p;
     650             : }
     651             : 
     652             : INLINE ulong
     653  1151189194 : Flx_mullimb(GEN x, GEN y, ulong p, ulong pi, long a, long b)
     654             : {
     655  1151189194 :   ulong p1 = 0;
     656             :   long i;
     657  3619802214 :   for (i=a; i<b; i++)
     658  2468751201 :     if (y[i])
     659  2444385688 :       p1 = Fl_addmul_pre(p1, y[i], x[-i], p, pi);
     660  1151051013 :   return p1;
     661             : }
     662             : 
     663             : /* assume nx >= ny > 0 */
     664             : static GEN
     665   344045507 : Flx_mulspec_basecase(GEN x, GEN y, ulong p, ulong pi, long nx, long ny)
     666             : {
     667             :   long i,lz,nz;
     668             :   GEN z;
     669             : 
     670   344045507 :   lz = nx+ny+1; nz = lz-2;
     671   344045507 :   z = cgetg(lz, t_VECSMALL) + 2; /* x:y:z [i] = term of degree i */
     672   343838692 :   if (!pi)
     673             :   {
     674  1150805159 :     for (i=0; i<ny; i++)z[i] = Flx_mullimb_ok(x+i,y,p,0,i+1);
     675   736524599 :     for (  ; i<nx; i++) z[i] = Flx_mullimb_ok(x+i,y,p,0,ny);
     676   896426032 :     for (  ; i<nz; i++) z[i] = Flx_mullimb_ok(x+i,y,p,i-nx+1,ny);
     677             :   }
     678             :   else
     679             :   {
     680   306276365 :     for (i=0; i<ny; i++)z[i] = Flx_mullimb(x+i,y,p,pi,0,i+1);
     681   213409662 :     for (  ; i<nx; i++) z[i] = Flx_mullimb(x+i,y,p,pi,0,ny);
     682   218403032 :     for (  ; i<nz; i++) z[i] = Flx_mullimb(x+i,y,p,pi,i-nx+1,ny);
     683             :   }
     684   343710648 :   z -= 2; return Flx_renormalize(z, lz);
     685             : }
     686             : 
     687             : static GEN
     688       12301 : int_to_Flx(GEN z, ulong p)
     689             : {
     690       12301 :   long i, l = lgefint(z);
     691       12301 :   GEN x = cgetg(l, t_VECSMALL);
     692     1059375 :   for (i=2; i<l; i++) x[i] = uel(z,i)%p;
     693       12292 :   return Flx_renormalize(x, l);
     694             : }
     695             : 
     696             : INLINE GEN
     697       10028 : Flx_mulspec_mulii(GEN a, GEN b, ulong p, long na, long nb)
     698             : {
     699       10028 :   GEN z=muliispec(a,b,na,nb);
     700       10039 :   return int_to_Flx(z,p);
     701             : }
     702             : 
     703             : static GEN
     704      509899 : Flx_to_int_halfspec(GEN a, long na)
     705             : {
     706             :   long j;
     707      509899 :   long n = (na+1)>>1UL;
     708      509899 :   GEN V = cgetipos(2+n);
     709             :   GEN w;
     710     1438556 :   for (w = int_LSW(V), j=0; j+1<na; j+=2, w=int_nextW(w))
     711      928658 :     *w = a[j]|(a[j+1]<<BITS_IN_HALFULONG);
     712      509898 :   if (j<na)
     713      340551 :     *w = a[j];
     714      509898 :   return V;
     715             : }
     716             : 
     717             : static GEN
     718      594101 : int_to_Flx_half(GEN z, ulong p)
     719             : {
     720             :   long i;
     721      594101 :   long lx = (lgefint(z)-2)*2+2;
     722      594101 :   GEN w, x = cgetg(lx, t_VECSMALL);
     723     2022808 :   for (w = int_LSW(z), i=2; i<lx; i+=2, w=int_nextW(w))
     724             :   {
     725     1428707 :     x[i]   = LOWWORD((ulong)*w)%p;
     726     1428707 :     x[i+1] = HIGHWORD((ulong)*w)%p;
     727             :   }
     728      594101 :   return Flx_renormalize(x, lx);
     729             : }
     730             : 
     731             : static GEN
     732        5454 : Flx_mulspec_halfmulii(GEN a, GEN b, ulong p, long na, long nb)
     733             : {
     734        5454 :   GEN A = Flx_to_int_halfspec(a,na);
     735        5454 :   GEN B = Flx_to_int_halfspec(b,nb);
     736        5454 :   GEN z = mulii(A,B);
     737        5454 :   return int_to_Flx_half(z,p);
     738             : }
     739             : 
     740             : static GEN
     741      204440 : Flx_to_int_quartspec(GEN a, long na)
     742             : {
     743             :   long j;
     744      204440 :   long n = (na+3)>>2UL;
     745      204440 :   GEN V = cgetipos(2+n);
     746             :   GEN w;
     747     4377359 :   for (w = int_LSW(V), j=0; j+3<na; j+=4, w=int_nextW(w))
     748     4172916 :     *w = a[j]|(a[j+1]<<BITS_IN_QUARTULONG)|(a[j+2]<<(2*BITS_IN_QUARTULONG))|(a[j+3]<<(3*BITS_IN_QUARTULONG));
     749      204443 :   switch (na-j)
     750             :   {
     751      116413 :   case 3:
     752      116413 :     *w = a[j]|(a[j+1]<<BITS_IN_QUARTULONG)|(a[j+2]<<(2*BITS_IN_QUARTULONG));
     753      116413 :     break;
     754       34416 :   case 2:
     755       34416 :     *w = a[j]|(a[j+1]<<BITS_IN_QUARTULONG);
     756       34416 :     break;
     757       27225 :   case 1:
     758       27225 :     *w = a[j];
     759       27225 :     break;
     760       26390 :   case 0:
     761       26390 :     break;
     762             :   }
     763      204443 :   return V;
     764             : }
     765             : 
     766             : static GEN
     767      107334 : int_to_Flx_quart(GEN z, ulong p)
     768             : {
     769             :   long i;
     770      107334 :   long lx = (lgefint(z)-2)*4+2;
     771      107334 :   GEN w, x = cgetg(lx, t_VECSMALL);
     772     4873565 :   for (w = int_LSW(z), i=2; i<lx; i+=4, w=int_nextW(w))
     773             :   {
     774     4766231 :     x[i]   = LLQUARTWORD((ulong)*w)%p;
     775     4766231 :     x[i+1] = HLQUARTWORD((ulong)*w)%p;
     776     4766231 :     x[i+2] = LHQUARTWORD((ulong)*w)%p;
     777     4766231 :     x[i+3] = HHQUARTWORD((ulong)*w)%p;
     778             :   }
     779      107334 :   return Flx_renormalize(x, lx);
     780             : }
     781             : 
     782             : static GEN
     783       97109 : Flx_mulspec_quartmulii(GEN a, GEN b, ulong p, long na, long nb)
     784             : {
     785       97109 :   GEN A = Flx_to_int_quartspec(a,na);
     786       97113 :   GEN B = Flx_to_int_quartspec(b,nb);
     787       97113 :   GEN z = mulii(A,B);
     788       97113 :   return int_to_Flx_quart(z,p);
     789             : }
     790             : 
     791             : /*Eval x in 2^(k*BIL) in linear time, k==2 or 3*/
     792             : static GEN
     793      581664 : Flx_eval2BILspec(GEN x, long k, long l)
     794             : {
     795      581664 :   long i, lz = k*l, ki;
     796      581664 :   GEN pz = cgetipos(2+lz);
     797    16356482 :   for (i=0; i < lz; i++)
     798    15774818 :     *int_W(pz,i) = 0UL;
     799     8469073 :   for (i=0, ki=0; i<l; i++, ki+=k)
     800     7887409 :     *int_W(pz,ki) = x[i];
     801      581664 :   return int_normalize(pz,0);
     802             : }
     803             : 
     804             : static GEN
     805      297818 : Z_mod2BIL_Flx_2(GEN x, long d, ulong p)
     806             : {
     807      297818 :   long i, offset, lm = lgefint(x)-2, l = d+3;
     808      297818 :   ulong pi = get_Fl_red(p);
     809      297818 :   GEN pol = cgetg(l, t_VECSMALL);
     810      297818 :   pol[1] = 0;
     811     8004016 :   for (i=0, offset=0; offset+1 < lm; i++, offset += 2)
     812     7706198 :     pol[i+2] = remll_pre(*int_W(x,offset+1), *int_W(x,offset), p, pi);
     813      297818 :   if (offset < lm)
     814      224975 :     pol[i+2] = (*int_W(x,offset)) % p;
     815      297818 :   return Flx_renormalize(pol,l);
     816             : }
     817             : 
     818             : static GEN
     819           0 : Z_mod2BIL_Flx_3(GEN x, long d, ulong p)
     820             : {
     821           0 :   long i, offset, lm = lgefint(x)-2, l = d+3;
     822           0 :   ulong pi = get_Fl_red(p);
     823           0 :   GEN pol = cgetg(l, t_VECSMALL);
     824           0 :   pol[1] = 0;
     825           0 :   for (i=0, offset=0; offset+2 < lm; i++, offset += 3)
     826           0 :     pol[i+2] = remlll_pre(*int_W(x,offset+2), *int_W(x,offset+1),
     827           0 :                           *int_W(x,offset), p, pi);
     828           0 :   if (offset+1 < lm)
     829           0 :     pol[i+2] = remll_pre(*int_W(x,offset+1), *int_W(x,offset), p, pi);
     830           0 :   else if (offset < lm)
     831           0 :     pol[i+2] = (*int_W(x,offset)) % p;
     832           0 :   return Flx_renormalize(pol,l);
     833             : }
     834             : 
     835             : static GEN
     836      294888 : Z_mod2BIL_Flx(GEN x, long bs, long d, ulong p)
     837             : {
     838      294888 :   return bs==2 ? Z_mod2BIL_Flx_2(x, d, p): Z_mod2BIL_Flx_3(x, d, p);
     839             : }
     840             : 
     841             : static GEN
     842      283387 : Flx_mulspec_mulii_inflate(GEN x, GEN y, long N, ulong p, long nx, long ny)
     843             : {
     844      283387 :   pari_sp av = avma;
     845      283387 :   GEN z = mulii(Flx_eval2BILspec(x,N,nx), Flx_eval2BILspec(y,N,ny));
     846      283387 :   return gerepileupto(av, Z_mod2BIL_Flx(z, N, nx+ny-2, p));
     847             : }
     848             : 
     849             : static GEN
     850    20848660 : kron_pack_Flx_spec_bits(GEN x, long b, long l) {
     851             :   GEN y;
     852             :   long i;
     853    20848660 :   if (l == 0)
     854     3428683 :     return gen_0;
     855    17419977 :   y = cgetg(l + 1, t_VECSMALL);
     856   815258115 :   for(i = 1; i <= l; i++)
     857   797841624 :     y[i] = x[l - i];
     858    17416491 :   return nv_fromdigits_2k(y, b);
     859             : }
     860             : 
     861             : /* assume b < BITS_IN_LONG */
     862             : static GEN
     863     5685726 : kron_unpack_Flx_bits_narrow(GEN z, long b, ulong p) {
     864     5685726 :   GEN v = binary_2k_nv(z, b), x;
     865     5685754 :   long i, l = lg(v) + 1;
     866     5685754 :   x = cgetg(l, t_VECSMALL);
     867   623546392 :   for (i = 2; i < l; i++)
     868   617860530 :     x[i] = v[l - i] % p;
     869     5685862 :   return Flx_renormalize(x, l);
     870             : }
     871             : 
     872             : static GEN
     873     5539939 : kron_unpack_Flx_bits_wide(GEN z, long b, ulong p, ulong pi) {
     874     5539939 :   GEN v = binary_2k(z, b), x, y;
     875     5540089 :   long i, l = lg(v) + 1, ly;
     876     5540089 :   x = cgetg(l, t_VECSMALL);
     877   232912720 :   for (i = 2; i < l; i++) {
     878   227376184 :     y = gel(v, l - i);
     879   227376184 :     ly = lgefint(y);
     880   227376184 :     switch (ly) {
     881     6286867 :     case 2: x[i] = 0; break;
     882    29289225 :     case 3: x[i] = *int_W_lg(y, 0, ly) % p; break;
     883   175933570 :     case 4: x[i] = remll_pre(*int_W_lg(y, 1, ly), *int_W_lg(y, 0, ly), p, pi); break;
     884    31732711 :     case 5: x[i] = remlll_pre(*int_W_lg(y, 2, ly), *int_W_lg(y, 1, ly),
     885    15866522 :                               *int_W_lg(y, 0, ly), p, pi); break;
     886           0 :     default: x[i] = umodiu(gel(v, l - i), p);
     887             :     }
     888             :   }
     889     5536536 :   return Flx_renormalize(x, l);
     890             : }
     891             : 
     892             : static GEN
     893     7272284 : Flx_mulspec_Kronecker(GEN A, GEN B, long b, ulong p, long lA, long lB)
     894             : {
     895             :   GEN C, D;
     896     7272284 :   pari_sp av = avma;
     897     7272284 :   A =  kron_pack_Flx_spec_bits(A, b, lA);
     898     7278557 :   B =  kron_pack_Flx_spec_bits(B, b, lB);
     899     7278652 :   C = gerepileuptoint(av, mulii(A, B));
     900     7277452 :   if (b < BITS_IN_LONG)
     901     2111430 :     D =  kron_unpack_Flx_bits_narrow(C, b, p);
     902             :   else
     903             :   {
     904     5166022 :     ulong pi = get_Fl_red(p);
     905     5164949 :     D = kron_unpack_Flx_bits_wide(C, b, p, pi);
     906             :   }
     907     7275019 :   return D;
     908             : }
     909             : 
     910             : static GEN
     911      688615 : Flx_sqrspec_Kronecker(GEN A, long b, ulong p, long lA)
     912             : {
     913             :   GEN C, D;
     914      688615 :   A =  kron_pack_Flx_spec_bits(A, b, lA);
     915      688697 :   C = sqri(A);
     916      688701 :   if (b < BITS_IN_LONG)
     917      480527 :     D =  kron_unpack_Flx_bits_narrow(C, b, p);
     918             :   else
     919             :   {
     920      208174 :     ulong pi = get_Fl_red(p);
     921      208170 :     D = kron_unpack_Flx_bits_wide(C, b, p, pi);
     922             :   }
     923      688674 :   return D;
     924             : }
     925             : 
     926             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
     927             :  * b+2 were sent instead. na, nb = number of terms of a, b.
     928             :  * Only c, c0, c1, c2 are genuine GEN.
     929             :  */
     930             : static GEN
     931   381537228 : Flx_mulspec(GEN a, GEN b, ulong p, ulong pi, long na, long nb)
     932             : {
     933             :   GEN a0,c,c0;
     934   381537228 :   long n0, n0a, i, v = 0;
     935             :   pari_sp av;
     936             : 
     937   487338237 :   while (na && !a[0]) { a++; na--; v++; }
     938   569048086 :   while (nb && !b[0]) { b++; nb--; v++; }
     939   381537228 :   if (na < nb) swapspec(a,b, na,nb);
     940   381537228 :   if (!nb) return pol0_Flx(0);
     941             : 
     942   353290284 :   av = avma;
     943   353290284 :   if (nb >= get_Fl_threshold(p, Flx_MUL_MULII_LIMIT, Flx_MUL2_MULII_LIMIT))
     944             :   {
     945     7672108 :     long m = maxbitcoeffpol(p,nb);
     946     7667898 :     switch (m)
     947             :     {
     948       97109 :     case BITS_IN_QUARTULONG:
     949       97109 :       return Flx_shiftip(av,Flx_mulspec_quartmulii(a,b,p,na,nb), v);
     950        5454 :     case BITS_IN_HALFULONG:
     951        5454 :       return Flx_shiftip(av,Flx_mulspec_halfmulii(a,b,p,na,nb), v);
     952       10028 :     case BITS_IN_LONG:
     953       10028 :       return Flx_shiftip(av,Flx_mulspec_mulii(a,b,p,na,nb), v);
     954      283387 :     case 2*BITS_IN_LONG:
     955      283387 :       return Flx_shiftip(av,Flx_mulspec_mulii_inflate(a,b,2,p,na,nb), v);
     956           0 :     case 3*BITS_IN_LONG:
     957           0 :       return Flx_shiftip(av,Flx_mulspec_mulii_inflate(a,b,3,p,na,nb), v);
     958     7271920 :     default:
     959     7271920 :       return Flx_shiftip(av,Flx_mulspec_Kronecker(a,b,m,p,na,nb), v);
     960             :     }
     961             :   }
     962   345786950 :   if (nb < get_Fl_threshold(p, Flx_MUL_KARATSUBA_LIMIT, Flx_MUL2_KARATSUBA_LIMIT))
     963   343977775 :     return Flx_shiftip(av,Flx_mulspec_basecase(a,b,p,pi,na,nb), v);
     964     1820301 :   i=(na>>1); n0=na-i; na=i;
     965     1820301 :   a0=a+n0; n0a=n0;
     966     2597466 :   while (n0a && !a[n0a-1]) n0a--;
     967             : 
     968     1820301 :   if (nb > n0)
     969             :   {
     970             :     GEN b0,c1,c2;
     971             :     long n0b;
     972             : 
     973     1766287 :     nb -= n0; b0 = b+n0; n0b = n0;
     974     2853259 :     while (n0b && !b[n0b-1]) n0b--;
     975     1766287 :     c =  Flx_mulspec(a,b,p,pi,n0a,n0b);
     976     1766287 :     c0 = Flx_mulspec(a0,b0,p,pi,na,nb);
     977             : 
     978     1766287 :     c2 = Flx_addspec(a0,a,p,na,n0a);
     979     1766287 :     c1 = Flx_addspec(b0,b,p,nb,n0b);
     980             : 
     981     1766287 :     c1 = Flx_mul_pre(c1,c2,p,pi);
     982     1766287 :     c2 = Flx_add(c0,c,p);
     983             : 
     984     1766287 :     c2 = Flx_neg_inplace(c2,p);
     985     1766287 :     c2 = Flx_add(c1,c2,p);
     986     1766287 :     c0 = Flx_addshift(c0,c2 ,p, n0);
     987             :   }
     988             :   else
     989             :   {
     990       54014 :     c  = Flx_mulspec(a,b,p,pi,n0a,nb);
     991       54014 :     c0 = Flx_mulspec(a0,b,p,pi,na,nb);
     992             :   }
     993     1820301 :   c0 = Flx_addshift(c0,c,p,n0);
     994     1820301 :   return Flx_shiftip(av,c0, v);
     995             : }
     996             : 
     997             : GEN
     998   375894272 : Flx_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     999             : {
    1000   375894272 :   GEN z = Flx_mulspec(x+2,y+2,p, pi, lgpol(x),lgpol(y));
    1001   376034720 :   z[1] = x[1]; return z;
    1002             : }
    1003             : GEN
    1004    28047093 : Flx_mul(GEN x, GEN y, ulong p)
    1005    28047093 : { return Flx_mul_pre(x, y, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1006             : 
    1007             : static GEN
    1008   281085830 : Flx_sqrspec_basecase(GEN x, ulong p, ulong pi, long nx)
    1009             : {
    1010             :   long i, lz, nz;
    1011             :   ulong p1;
    1012             :   GEN z;
    1013             : 
    1014   281085830 :   if (!nx) return pol0_Flx(0);
    1015   281085830 :   lz = (nx << 1) + 1, nz = lz-2;
    1016   281085830 :   z = cgetg(lz, t_VECSMALL) + 2;
    1017   280561574 :   if (!pi)
    1018             :   {
    1019   215463254 :     z[0] = x[0]*x[0]%p;
    1020   921659153 :     for (i=1; i<nx; i++)
    1021             :     {
    1022   706309141 :       p1 = Flx_mullimb_ok(x+i,x,p,0, (i+1)>>1);
    1023   706195899 :       p1 <<= 1;
    1024   706195899 :       if ((i&1) == 0) p1 += x[i>>1] * x[i>>1];
    1025   706195899 :       z[i] = p1 % p;
    1026             :     }
    1027   925263578 :     for (  ; i<nz; i++)
    1028             :     {
    1029   709345118 :       p1 = Flx_mullimb_ok(x+i,x,p,i-nx+1, (i+1)>>1);
    1030   709913566 :       p1 <<= 1;
    1031   709913566 :       if ((i&1) == 0) p1 += x[i>>1] * x[i>>1];
    1032   709913566 :       z[i] = p1 % p;
    1033             :     }
    1034             :   }
    1035             :   else
    1036             :   {
    1037    65098320 :     z[0] = Fl_sqr_pre(x[0], p, pi);
    1038   409535818 :     for (i=1; i<nx; i++)
    1039             :     {
    1040   344548219 :       p1 = Flx_mullimb(x+i,x,p,pi,0, (i+1)>>1);
    1041   344799804 :       p1 = Fl_add(p1, p1, p);
    1042   344353395 :       if ((i&1) == 0) p1 = Fl_add(p1, Fl_sqr_pre(x[i>>1], p, pi), p);
    1043   344381082 :       z[i] = p1;
    1044             :     }
    1045   409670657 :     for (  ; i<nz; i++)
    1046             :     {
    1047   344576899 :       p1 = Flx_mullimb(x+i,x,p,pi,i-nx+1, (i+1)>>1);
    1048   345285866 :       p1 = Fl_add(p1, p1, p);
    1049   344926967 :       if ((i&1) == 0) p1 = Fl_add(p1, Fl_sqr_pre(x[i>>1], p, pi), p);
    1050   344683058 :       z[i] = p1;
    1051             :     }
    1052             :   }
    1053   281012218 :   z -= 2; return Flx_renormalize(z, lz);
    1054             : }
    1055             : 
    1056             : static GEN
    1057        2265 : Flx_sqrspec_sqri(GEN a, ulong p, long na)
    1058             : {
    1059        2265 :   GEN z=sqrispec(a,na);
    1060        2264 :   return int_to_Flx(z,p);
    1061             : }
    1062             : 
    1063             : static GEN
    1064         136 : Flx_sqrspec_halfsqri(GEN a, ulong p, long na)
    1065             : {
    1066         136 :   GEN z = sqri(Flx_to_int_halfspec(a,na));
    1067         136 :   return int_to_Flx_half(z,p);
    1068             : }
    1069             : 
    1070             : static GEN
    1071       10221 : Flx_sqrspec_quartsqri(GEN a, ulong p, long na)
    1072             : {
    1073       10221 :   GEN z = sqri(Flx_to_int_quartspec(a,na));
    1074       10221 :   return int_to_Flx_quart(z,p);
    1075             : }
    1076             : 
    1077             : static GEN
    1078       11501 : Flx_sqrspec_sqri_inflate(GEN x, long N, ulong p, long nx)
    1079             : {
    1080       11501 :   pari_sp av = avma;
    1081       11501 :   GEN  z = sqri(Flx_eval2BILspec(x,N,nx));
    1082       11501 :   return gerepileupto(av, Z_mod2BIL_Flx(z, N, (nx-1)*2, p));
    1083             : }
    1084             : 
    1085             : static GEN
    1086   281287801 : Flx_sqrspec(GEN a, ulong p, ulong pi, long na)
    1087             : {
    1088             :   GEN a0, c, c0;
    1089   281287801 :   long n0, n0a, i, v = 0, m;
    1090             :   pari_sp av;
    1091             : 
    1092   402420648 :   while (na && !a[0]) { a++; na--; v += 2; }
    1093   281287801 :   if (!na) return pol0_Flx(0);
    1094             : 
    1095   281040374 :   av = avma;
    1096   281040374 :   if (na >= get_Fl_threshold(p, Flx_SQR_SQRI_LIMIT, Flx_SQR2_SQRI_LIMIT))
    1097             :   {
    1098      712722 :     m = maxbitcoeffpol(p,na);
    1099      712731 :     switch(m)
    1100             :     {
    1101       10221 :     case BITS_IN_QUARTULONG:
    1102       10221 :       return Flx_shiftip(av, Flx_sqrspec_quartsqri(a,p,na), v);
    1103         136 :     case BITS_IN_HALFULONG:
    1104         136 :       return Flx_shiftip(av, Flx_sqrspec_halfsqri(a,p,na), v);
    1105        2265 :     case BITS_IN_LONG:
    1106        2265 :       return Flx_shiftip(av, Flx_sqrspec_sqri(a,p,na), v);
    1107       11501 :     case 2*BITS_IN_LONG:
    1108       11501 :       return Flx_shiftip(av, Flx_sqrspec_sqri_inflate(a,2,p,na), v);
    1109           0 :     case 3*BITS_IN_LONG:
    1110           0 :       return Flx_shiftip(av, Flx_sqrspec_sqri_inflate(a,3,p,na), v);
    1111      688608 :     default:
    1112      688608 :       return Flx_shiftip(av, Flx_sqrspec_Kronecker(a,m,p,na), v);
    1113             :     }
    1114             :   }
    1115   280793953 :   if (na < get_Fl_threshold(p, Flx_SQR_KARATSUBA_LIMIT, Flx_SQR2_KARATSUBA_LIMIT))
    1116   280737917 :     return Flx_shiftip(av, Flx_sqrspec_basecase(a,p,pi,na), v);
    1117       57607 :   i=(na>>1); n0=na-i; na=i;
    1118       57607 :   a0=a+n0; n0a=n0;
    1119       72398 :   while (n0a && !a[n0a-1]) n0a--;
    1120             : 
    1121       57607 :   c = Flx_sqrspec(a,p,pi,n0a);
    1122       57607 :   c0= Flx_sqrspec(a0,p,pi,na);
    1123       57607 :   if (p == 2) n0 *= 2;
    1124             :   else
    1125             :   {
    1126       57588 :     GEN c1, t = Flx_addspec(a0,a,p,na,n0a);
    1127       57588 :     t = Flx_sqr_pre(t,p,pi);
    1128       57588 :     c1= Flx_add(c0,c, p);
    1129       57588 :     c1= Flx_sub(t, c1, p);
    1130       57588 :     c0 = Flx_addshift(c0,c1,p,n0);
    1131             :   }
    1132       57607 :   c0 = Flx_addshift(c0,c,p,n0);
    1133       57607 :   return Flx_shiftip(av,c0,v);
    1134             : }
    1135             : 
    1136             : GEN
    1137   281041571 : Flx_sqr_pre(GEN x, ulong p, ulong pi)
    1138             : {
    1139   281041571 :   GEN z = Flx_sqrspec(x+2,p, pi, lgpol(x));
    1140   282220266 :   z[1] = x[1]; return z;
    1141             : }
    1142             : GEN
    1143      356052 : Flx_sqr(GEN x, ulong p)
    1144      356052 : { return Flx_sqr_pre(x, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1145             : 
    1146             : GEN
    1147        7893 : Flx_powu_pre(GEN x, ulong n, ulong p, ulong pi)
    1148             : {
    1149        7893 :   GEN y = pol1_Flx(x[1]), z;
    1150             :   ulong m;
    1151        7893 :   if (n == 0) return y;
    1152        7893 :   m = n; z = x;
    1153             :   for (;;)
    1154             :   {
    1155       30433 :     if (m&1UL) y = Flx_mul_pre(y,z, p, pi);
    1156       30427 :     m >>= 1; if (!m) return y;
    1157       22539 :     z = Flx_sqr_pre(z, p, pi);
    1158             :   }
    1159             : }
    1160             : GEN
    1161           0 : Flx_powu(GEN x, ulong n, ulong p)
    1162             : {
    1163           0 :   if (n == 0) return pol1_Flx(x[1]);
    1164           0 :   return Flx_powu_pre(x, n, p, SMALL_ULONG(p)? 0: get_Fl_red(p));
    1165             : }
    1166             : 
    1167             : GEN
    1168       14265 : Flx_halve(GEN y, ulong p)
    1169             : {
    1170             :   GEN z;
    1171             :   long i, l;
    1172       14265 :   z = cgetg_copy(y, &l); z[1] = y[1];
    1173       59438 :   for(i=2; i<l; i++) uel(z,i) = Fl_halve(uel(y,i), p);
    1174       14265 :   return z;
    1175             : }
    1176             : 
    1177             : static GEN
    1178     7116487 : Flx_recipspec(GEN x, long l, long n)
    1179             : {
    1180             :   long i;
    1181     7116487 :   GEN z=cgetg(n+2,t_VECSMALL)+2;
    1182   115537710 :   for(i=0; i<l; i++)
    1183   108422817 :     z[n-i-1] = x[i];
    1184    15560141 :   for(   ; i<n; i++)
    1185     8445248 :     z[n-i-1] = 0;
    1186     7114893 :   return Flx_renormalize(z-2,n+2);
    1187             : }
    1188             : 
    1189             : GEN
    1190           0 : Flx_recip(GEN x)
    1191             : {
    1192           0 :   GEN z=Flx_recipspec(x+2,lgpol(x),lgpol(x));
    1193           0 :   z[1]=x[1];
    1194           0 :   return z;
    1195             : }
    1196             : 
    1197             : /* Return h^degpol(P) P(x / h) */
    1198             : GEN
    1199        1117 : Flx_rescale(GEN P, ulong h, ulong p)
    1200             : {
    1201        1117 :   long i, l = lg(P);
    1202        1117 :   GEN Q = cgetg(l,t_VECSMALL);
    1203        1117 :   ulong hi = h;
    1204        1117 :   Q[l-1] = P[l-1];
    1205       12538 :   for (i=l-2; i>=2; i--)
    1206             :   {
    1207       12538 :     Q[i] = Fl_mul(P[i], hi, p);
    1208       12538 :     if (i == 2) break;
    1209       11421 :     hi = Fl_mul(hi,h, p);
    1210             :   }
    1211        1117 :   Q[1] = P[1]; return Q;
    1212             : }
    1213             : 
    1214             : /* x/polrecip(P)+O(x^n); allow pi = 0 */
    1215             : static GEN
    1216      134215 : Flx_invBarrett_basecase(GEN T, ulong p, ulong pi)
    1217             : {
    1218      134215 :   long i, l=lg(T)-1, lr=l-1, k;
    1219      134215 :   GEN r=cgetg(lr,t_VECSMALL); r[1] = T[1];
    1220      134215 :   r[2] = 1;
    1221      134215 :   if (!pi)
    1222      764068 :     for (i=3;i<lr;i++)
    1223             :     {
    1224      757076 :       ulong u = uel(T, l-i+2);
    1225    45377003 :       for (k=3; k<i; k++)
    1226    44619927 :         { u += uel(T,l-i+k) * uel(r, k); if (u & HIGHBIT) u %= p; }
    1227      757076 :       r[i] = Fl_neg(u % p, p);
    1228             :     }
    1229             :   else
    1230     2109570 :     for (i=3;i<lr;i++)
    1231             :     {
    1232     1982348 :       ulong u = Fl_neg(uel(T,l-i+2), p);
    1233    59521586 :       for (k=3; k<i; k++)
    1234             :       {
    1235    57539239 :         ulong t = Fl_neg(uel(T,l-i+k), p);
    1236    57539239 :         u = Fl_addmul_pre(u, t, uel(r,k), p, pi);
    1237             :       }
    1238     1982347 :       r[i] = u;
    1239             :     }
    1240      134214 :   return Flx_renormalize(r,lr);
    1241             : }
    1242             : 
    1243             : /* Return new lgpol */
    1244             : static long
    1245     2130450 : Flx_lgrenormalizespec(GEN x, long lx)
    1246             : {
    1247             :   long i;
    1248     7434820 :   for (i = lx-1; i>=0; i--)
    1249     7433974 :     if (x[i]) break;
    1250     2130450 :   return i+1;
    1251             : }
    1252             : /* allow pi = 0 */
    1253             : static GEN
    1254       23116 : Flx_invBarrett_Newton(GEN T, ulong p, ulong pi)
    1255             : {
    1256       23116 :   long nold, lx, lz, lq, l = degpol(T), lQ;
    1257       23116 :   GEN q, y, z, x = zero_zv(l+1) + 2;
    1258       23116 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
    1259             :   pari_sp av;
    1260             : 
    1261       23116 :   y = T+2;
    1262       23116 :   q = Flx_recipspec(y,l+1,l+1); lQ = lgpol(q); q+=2;
    1263       23116 :   av = avma;
    1264             :   /* We work on _spec_ Flx's, all the l[xzq12] below are lgpol's */
    1265             : 
    1266             :   /* initialize */
    1267       23116 :   x[0] = Fl_inv(q[0], p);
    1268       23116 :   if (lQ>1 && q[1])
    1269        5109 :   {
    1270        5109 :     ulong u = q[1];
    1271        5109 :     if (x[0] != 1) u = Fl_mul(u, Fl_sqr(x[0],p), p);
    1272        5109 :     x[1] = p - u; lx = 2;
    1273             :   }
    1274             :   else
    1275       18007 :     lx = 1;
    1276       23116 :   nold = 1;
    1277      158719 :   for (; mask > 1; set_avma(av))
    1278             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
    1279      135607 :     long i, lnew, nnew = nold << 1;
    1280             : 
    1281      135607 :     if (mask & 1) nnew--;
    1282      135607 :     mask >>= 1;
    1283             : 
    1284      135607 :     lnew = nnew + 1;
    1285      135607 :     lq = Flx_lgrenormalizespec(q, minss(lQ, lnew));
    1286      135608 :     z = Flx_mulspec(x, q, p, pi, lx, lq); /* FIXME: high product */
    1287      135604 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
    1288      135603 :     z += 2;
    1289             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
    1290      290688 :     for (i = nold; i < lz; i++) if (z[i]) break;
    1291      135603 :     nold = nnew;
    1292      135603 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
    1293             : 
    1294             :     /* z + i represents (x*q - 1) / t^i */
    1295      100753 :     lz = Flx_lgrenormalizespec (z+i, lz-i);
    1296      100754 :     z = Flx_mulspec(x, z+i, p, pi, lx, lz); /* FIXME: low product */
    1297      100753 :     lz = lgpol(z); z += 2;
    1298      100753 :     if (lz > lnew-i) lz = Flx_lgrenormalizespec(z, lnew-i);
    1299             : 
    1300      100753 :     lx = lz+ i;
    1301      100753 :     y  = x + i; /* x -= z * t^i, in place */
    1302      915329 :     for (i = 0; i < lz; i++) y[i] = Fl_neg(z[i], p);
    1303             :   }
    1304       23116 :   x -= 2; setlg(x, lx + 2); x[1] = T[1];
    1305       23116 :   return x;
    1306             : }
    1307             : 
    1308             : /* allow pi = 0 */
    1309             : static GEN
    1310      158721 : Flx_invBarrett_pre(GEN T, ulong p, ulong pi)
    1311             : {
    1312      158721 :   pari_sp ltop = avma;
    1313      158721 :   long l = lgpol(T);
    1314             :   GEN r;
    1315      158721 :   if (l < 3) return pol0_Flx(T[1]);
    1316      157331 :   if (l < get_Fl_threshold(p, Flx_INVBARRETT_LIMIT, Flx_INVBARRETT2_LIMIT))
    1317             :   {
    1318      134215 :     ulong c = T[l+1];
    1319      134215 :     if (c != 1)
    1320             :     {
    1321       98118 :       ulong ci = Fl_inv(c,p);
    1322       98118 :       T = Flx_Fl_mul_pre(T, ci, p, pi);
    1323       98118 :       r = Flx_invBarrett_basecase(T, p, pi);
    1324       98118 :       r = Flx_Fl_mul_pre(r, ci, p, pi);
    1325             :     }
    1326             :     else
    1327       36097 :       r = Flx_invBarrett_basecase(T, p, pi);
    1328             :   }
    1329             :   else
    1330       23116 :     r = Flx_invBarrett_Newton(T, p, pi);
    1331      157330 :   return gerepileuptoleaf(ltop, r);
    1332             : }
    1333             : GEN
    1334           0 : Flx_invBarrett(GEN T, ulong p)
    1335           0 : { return Flx_invBarrett_pre(T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1336             : 
    1337             : /* allow pi = 0 */
    1338             : GEN
    1339   100918048 : Flx_get_red_pre(GEN T, ulong p, ulong pi)
    1340             : {
    1341   100918048 :   if (typ(T)!=t_VECSMALL
    1342   100881975 :     || lgpol(T) < get_Fl_threshold(p, Flx_BARRETT_LIMIT,
    1343             :                                        Flx_BARRETT2_LIMIT))
    1344   100900712 :     return T;
    1345        7611 :   retmkvec2(Flx_invBarrett_pre(T, p, pi),T);
    1346             : }
    1347             : GEN
    1348    14032618 : Flx_get_red(GEN T, ulong p)
    1349             : {
    1350    14032618 :   if (typ(T)!=t_VECSMALL
    1351    14032522 :     || lgpol(T) < get_Fl_threshold(p, Flx_BARRETT_LIMIT,
    1352             :                                        Flx_BARRETT2_LIMIT))
    1353    14026911 :     return T;
    1354        5194 :   retmkvec2(Flx_invBarrett_pre(T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)),T);
    1355             : }
    1356             : 
    1357             : /* separate from Flx_divrem for maximal speed. */
    1358             : static GEN
    1359   790799208 : Flx_rem_basecase(GEN x, GEN y, ulong p, ulong pi)
    1360             : {
    1361             :   pari_sp av;
    1362             :   GEN z, c;
    1363             :   long dx,dy,dy1,dz,i,j;
    1364             :   ulong p1,inv;
    1365   790799208 :   long vs=x[1];
    1366             : 
    1367   790799208 :   dy = degpol(y); if (!dy) return pol0_Flx(x[1]);
    1368   755288667 :   dx = degpol(x);
    1369   755206897 :   dz = dx-dy; if (dz < 0) return Flx_copy(x);
    1370   755206897 :   x += 2; y += 2;
    1371   755206897 :   inv = y[dy];
    1372   755206897 :   if (inv != 1UL) inv = Fl_inv(inv,p);
    1373   909743869 :   for (dy1=dy-1; dy1>=0 && !y[dy1]; dy1--);
    1374             : 
    1375   756599369 :   c = cgetg(dy+3, t_VECSMALL); c[1]=vs; c += 2; av=avma;
    1376   755348798 :   z = cgetg(dz+3, t_VECSMALL); z[1]=vs; z += 2;
    1377             : 
    1378   753741949 :   if (!pi)
    1379             :   {
    1380   481952113 :     z[dz] = (inv*x[dx]) % p;
    1381  1811867382 :     for (i=dx-1; i>=dy; --i)
    1382             :     {
    1383  1329915269 :       p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */
    1384 10553242141 :       for (j=i-dy1; j<=i && j<=dz; j++)
    1385             :       {
    1386  9223326872 :         p1 += z[j]*y[i-j];
    1387  9223326872 :         if (p1 & HIGHBIT) p1 %= p;
    1388             :       }
    1389  1329915269 :       p1 %= p;
    1390  1329915269 :       z[i-dy] = p1? ((p - p1)*inv) % p: 0;
    1391             :     }
    1392  3291485931 :     for (i=0; i<dy; i++)
    1393             :     {
    1394  2809762997 :       p1 = z[0]*y[i];
    1395 14565273629 :       for (j=maxss(1,i-dy1); j<=i && j<=dz; j++)
    1396             :       {
    1397 11755510632 :         p1 += z[j]*y[i-j];
    1398 11755510632 :         if (p1 & HIGHBIT) p1 %= p;
    1399             :       }
    1400  2809892683 :       c[i] = Fl_sub(x[i], p1%p, p);
    1401             :     }
    1402             :   }
    1403             :   else
    1404             :   {
    1405   271789836 :     z[dz] = Fl_mul_pre(inv, x[dx], p, pi);
    1406   824411514 :     for (i=dx-1; i>=dy; --i)
    1407             :     {
    1408   552348402 :       p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */
    1409  2326456508 :       for (j=i-dy1; j<=i && j<=dz; j++)
    1410  1773428220 :         p1 = Fl_addmul_pre(p1, z[j], y[i - j], p, pi);
    1411   553028288 :       z[i-dy] = p1? Fl_mul_pre(p - p1, inv, p, pi): 0;
    1412             :     }
    1413  2000013532 :     for (i=0; i<dy; i++)
    1414             :     {
    1415  1728825663 :       p1 = Fl_mul_pre(z[0],y[i],p,pi);
    1416  4644706642 :       for (j=maxss(1,i-dy1); j<=i && j<=dz; j++)
    1417  2908312867 :         p1 = Fl_addmul_pre(p1, z[j], y[i - j], p, pi);
    1418  1714354450 :       c[i] = Fl_sub(x[i], p1, p);
    1419             :     }
    1420             :   }
    1421   921747116 :   i = dy-1; while (i>=0 && !c[i]) i--;
    1422   752910803 :   set_avma(av); return Flx_renormalize(c-2, i+3);
    1423             : }
    1424             : 
    1425             : /* as FpX_divrem but working only on ulong types.
    1426             :  * if relevant, *pr is the last object on stack */
    1427             : static GEN
    1428    63145165 : Flx_divrem_basecase(GEN x, GEN y, ulong p, ulong pi, GEN *pr)
    1429             : {
    1430             :   GEN z,q,c;
    1431             :   long dx,dy,dy1,dz,i,j;
    1432             :   ulong p1,inv;
    1433    63145165 :   long sv=x[1];
    1434             : 
    1435    63145165 :   dy = degpol(y);
    1436    63142840 :   if (dy<0) pari_err_INV("Flx_divrem",y);
    1437    63142989 :   if (pr == ONLY_REM) return Flx_rem_basecase(x, y, p, pi);
    1438    63142591 :   if (!dy)
    1439             :   {
    1440     7621434 :     if (pr && pr != ONLY_DIVIDES) *pr = pol0_Flx(sv);
    1441     7621419 :     if (y[2] == 1UL) return Flx_copy(x);
    1442     5429752 :     return Flx_Fl_mul_pre(x, Fl_inv(y[2], p), p, pi);
    1443             :   }
    1444    55521157 :   dx = degpol(x);
    1445    55524307 :   dz = dx-dy;
    1446    55524307 :   if (dz < 0)
    1447             :   {
    1448     1025750 :     q = pol0_Flx(sv);
    1449     1025745 :     if (pr && pr != ONLY_DIVIDES) *pr = Flx_copy(x);
    1450     1025745 :     return q;
    1451             :   }
    1452    54498557 :   x += 2;
    1453    54498557 :   y += 2;
    1454    54498557 :   z = cgetg(dz + 3, t_VECSMALL); z[1] = sv; z += 2;
    1455    54497487 :   inv = uel(y, dy);
    1456    54497487 :   if (inv != 1UL) inv = Fl_inv(inv,p);
    1457    80201823 :   for (dy1=dy-1; dy1>=0 && !y[dy1]; dy1--);
    1458             : 
    1459    54499895 :   if (SMALL_ULONG(p))
    1460             :   {
    1461    52627131 :     z[dz] = (inv*x[dx]) % p;
    1462   133452898 :     for (i=dx-1; i>=dy; --i)
    1463             :     {
    1464    80825767 :       p1 = p - x[i]; /* compute -p1 instead of p1 (pb with ulongs otherwise) */
    1465   259457998 :       for (j=i-dy1; j<=i && j<=dz; j++)
    1466             :       {
    1467   178632231 :         p1 += z[j]*y[i-j];
    1468   178632231 :         if (p1 & HIGHBIT) p1 %= p;
    1469             :       }
    1470    80825767 :       p1 %= p;
    1471    80825767 :       z[i-dy] = p1? (long) ((p - p1)*inv) % p: 0;
    1472             :     }
    1473             :   }
    1474             :   else
    1475             :   {
    1476     1872764 :     z[dz] = Fl_mul(inv, x[dx], p);
    1477     9233795 :     for (i=dx-1; i>=dy; --i)
    1478             :     { /* compute -p1 instead of p1 (pb with ulongs otherwise) */
    1479     7361002 :       p1 = p - uel(x,i);
    1480    26337863 :       for (j=i-dy1; j<=i && j<=dz; j++)
    1481    18976862 :         p1 = Fl_add(p1, Fl_mul(z[j],y[i-j],p), p);
    1482     7361001 :       z[i-dy] = p1? Fl_mul(p - p1, inv, p): 0;
    1483             :     }
    1484             :   }
    1485    54499924 :   q = Flx_renormalize(z-2, dz+3);
    1486    54498800 :   if (!pr) return q;
    1487             : 
    1488    27396912 :   c = cgetg(dy + 3, t_VECSMALL); c[1] = sv; c += 2;
    1489    27399409 :   if (SMALL_ULONG(p))
    1490             :   {
    1491   233872946 :     for (i=0; i<dy; i++)
    1492             :     {
    1493   208107426 :       p1 = (ulong)z[0]*y[i];
    1494   487388608 :       for (j=maxss(1,i-dy1); j<=i && j<=dz; j++)
    1495             :       {
    1496   279281182 :         p1 += (ulong)z[j]*y[i-j];
    1497   279281182 :         if (p1 & HIGHBIT) p1 %= p;
    1498             :       }
    1499   208107059 :       c[i] = Fl_sub(x[i], p1%p, p);
    1500             :     }
    1501             :   }
    1502             :   else
    1503             :   {
    1504    16034275 :     for (i=0; i<dy; i++)
    1505             :     {
    1506    14400974 :       p1 = Fl_mul(z[0],y[i],p);
    1507    50241351 :       for (j=maxss(1,i-dy1); j<=i && j<=dz; j++)
    1508    35840374 :         p1 = Fl_add(p1, Fl_mul(z[j],y[i-j],p), p);
    1509    14400977 :       c[i] = Fl_sub(x[i], p1, p);
    1510             :     }
    1511             :   }
    1512    36866034 :   i=dy-1; while (i>=0 && !c[i]) i--;
    1513    27398821 :   c = Flx_renormalize(c-2, i+3);
    1514    27399510 :   if (pr == ONLY_DIVIDES)
    1515         428 :   { if (lg(c) != 2) return NULL; }
    1516             :   else
    1517    27399082 :     *pr = c;
    1518    27399370 :   return q;
    1519             : }
    1520             : 
    1521             : /* Compute x mod T where 2 <= degpol(T) <= l+1 <= 2*(degpol(T)-1)
    1522             :  * and mg is the Barrett inverse of T. */
    1523             : static GEN
    1524      904370 : Flx_divrem_Barrettspec(GEN x, long l, GEN mg, GEN T, ulong p, ulong pi, GEN *pr)
    1525             : {
    1526             :   GEN q, r;
    1527      904370 :   long lt = degpol(T); /*We discard the leading term*/
    1528             :   long ld, lm, lT, lmg;
    1529      904341 :   ld = l-lt;
    1530      904341 :   lm = minss(ld, lgpol(mg));
    1531      904712 :   lT  = Flx_lgrenormalizespec(T+2,lt);
    1532      904860 :   lmg = Flx_lgrenormalizespec(mg+2,lm);
    1533      904708 :   q = Flx_recipspec(x+lt,ld,ld);               /* q = rec(x)      lz<=ld*/
    1534      903888 :   q = Flx_mulspec(q+2,mg+2,p,pi,lgpol(q),lmg); /* q = rec(x) * mg lz<=ld+lm*/
    1535      904719 :   q = Flx_recipspec(q+2,minss(ld,lgpol(q)),ld);/* q = rec (rec(x) * mg) lz<=ld*/
    1536      903831 :   if (!pr) return q;
    1537      896140 :   r = Flx_mulspec(q+2,T+2,p,pi,lgpol(q),lT);   /* r = q*pol      lz<=ld+lt*/
    1538      896986 :   r = Flx_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* r = x - q*pol lz<=lt */
    1539      896725 :   if (pr == ONLY_REM) return r;
    1540      427870 :   *pr = r; return q;
    1541             : }
    1542             : 
    1543             : static GEN
    1544      604032 : Flx_divrem_Barrett(GEN x, GEN mg, GEN T, ulong p, ulong pi, GEN *pr)
    1545             : {
    1546      604032 :   GEN q = NULL, r = Flx_copy(x);
    1547      604049 :   long l = lgpol(x), lt = degpol(T), lm = 2*lt-1, v = T[1];
    1548             :   long i;
    1549      604047 :   if (l <= lt)
    1550             :   {
    1551           0 :     if (pr == ONLY_REM) return Flx_copy(x);
    1552           0 :     if (pr == ONLY_DIVIDES) return lgpol(x)? NULL: pol0_Flx(v);
    1553           0 :     if (pr) *pr = Flx_copy(x);
    1554           0 :     return pol0_Flx(v);
    1555             :   }
    1556      604047 :   if (lt <= 1)
    1557        1390 :     return Flx_divrem_basecase(x,T,p,pi,pr);
    1558      602657 :   if (pr != ONLY_REM && l>lm)
    1559       28900 :   { q = zero_zv(l-lt+1); q[1] = T[1]; }
    1560      905972 :   while (l>lm)
    1561             :   {
    1562      303354 :     GEN zr, zq = Flx_divrem_Barrettspec(r+2+l-lm,lm,mg,T,p,pi,&zr);
    1563      303357 :     long lz = lgpol(zr);
    1564      303315 :     if (pr != ONLY_REM)
    1565             :     {
    1566       57981 :       long lq = lgpol(zq);
    1567      872378 :       for(i=0; i<lq; i++) q[2+l-lm+i] = zq[2+i];
    1568             :     }
    1569     4391433 :     for(i=0; i<lz; i++)   r[2+l-lm+i] = zr[2+i];
    1570      303315 :     l = l-lm+lz;
    1571             :   }
    1572      602618 :   if (pr == ONLY_REM)
    1573             :   {
    1574      468912 :     if (l > lt)
    1575      468870 :       r = Flx_divrem_Barrettspec(r+2,l,mg,T,p,pi,ONLY_REM);
    1576             :     else
    1577          42 :       r = Flx_renormalize(r, l+2);
    1578      468897 :     r[1] = v; return r;
    1579             :   }
    1580      133706 :   if (l > lt)
    1581             :   {
    1582      132170 :     GEN zq = Flx_divrem_Barrettspec(r+2,l,mg,T,p,pi, pr ? &r: NULL);
    1583      132170 :     if (!q) q = zq;
    1584             :     else
    1585             :     {
    1586       27326 :       long lq = lgpol(zq);
    1587      158629 :       for(i=0; i<lq; i++) q[2+i] = zq[2+i];
    1588             :     }
    1589             :   }
    1590        1536 :   else if (pr)
    1591        1535 :     r = Flx_renormalize(r, l+2);
    1592      133706 :   q[1] = v; q = Flx_renormalize(q, lg(q));
    1593      133744 :   if (pr == ONLY_DIVIDES) return lgpol(r)? NULL: q;
    1594      133744 :   if (pr) { r[1] = v; *pr = r; }
    1595      133744 :   return q;
    1596             : }
    1597             : 
    1598             : /* allow pi = 0 (SMALL_ULONG) */
    1599             : GEN
    1600    80563576 : Flx_divrem_pre(GEN x, GEN T, ulong p, ulong pi, GEN *pr)
    1601             : {
    1602             :   GEN B, y;
    1603             :   long dy, dx, d;
    1604    80563576 :   if (pr==ONLY_REM) return Flx_rem_pre(x, T, p, pi);
    1605    63268829 :   y = get_Flx_red(T, &B);
    1606    63280605 :   dy = degpol(y); dx = degpol(x); d = dx-dy;
    1607    63276530 :   if (!B && d+3 < get_Fl_threshold(p, Flx_DIVREM_BARRETT_LIMIT,Flx_DIVREM2_BARRETT_LIMIT))
    1608    63142587 :     return Flx_divrem_basecase(x,y,p,pi,pr);
    1609             :   else
    1610             :   {
    1611      134736 :     pari_sp av = avma;
    1612      134736 :     GEN mg = B? B: Flx_invBarrett_pre(y, p, pi);
    1613      134736 :     GEN q1 = Flx_divrem_Barrett(x,mg,y,p,pi,pr);
    1614      134736 :     if (!q1) return gc_NULL(av);
    1615      134736 :     if (!pr || pr==ONLY_DIVIDES) return gerepileuptoleaf(av, q1);
    1616      126445 :     return gc_all(av, 2, &q1, pr);
    1617             :   }
    1618             : }
    1619             : GEN
    1620    30359345 : Flx_divrem(GEN x, GEN T, ulong p, GEN *pr)
    1621    30359345 : { return Flx_divrem_pre(x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p), pr); }
    1622             : 
    1623             : GEN
    1624   915497588 : Flx_rem_pre(GEN x, GEN T, ulong p, ulong pi)
    1625             : {
    1626   915497588 :   GEN B, y = get_Flx_red(T, &B);
    1627   915357757 :   long d = degpol(x) - degpol(y);
    1628   915122635 :   if (d < 0) return Flx_copy(x);
    1629   791325411 :   if (!B && d+3 < get_Fl_threshold(p, Flx_REM_BARRETT_LIMIT,Flx_REM2_BARRETT_LIMIT))
    1630   790784942 :     return Flx_rem_basecase(x,y,p, pi);
    1631             :   else
    1632             :   {
    1633      469299 :     pari_sp av=avma;
    1634      469299 :     GEN mg = B ? B: Flx_invBarrett_pre(y, p, pi);
    1635      469298 :     GEN r  = Flx_divrem_Barrett(x, mg, y, p, pi, ONLY_REM);
    1636      469295 :     return gerepileuptoleaf(av, r);
    1637             :   }
    1638             : }
    1639             : GEN
    1640    41757086 : Flx_rem(GEN x, GEN T, ulong p)
    1641    41757086 : { return Flx_rem_pre(x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1642             : 
    1643             : /* reduce T mod (X^n - 1, p). Shallow function */
    1644             : GEN
    1645     5097419 : Flx_mod_Xnm1(GEN T, ulong n, ulong p)
    1646             : {
    1647     5097419 :   long i, j, L = lg(T), l = n+2;
    1648             :   GEN S;
    1649     5097419 :   if (L <= l || n & ~LGBITS) return T;
    1650        3450 :   S = cgetg(l, t_VECSMALL);
    1651        3450 :   S[1] = T[1];
    1652       14013 :   for (i = 2; i < l; i++) S[i] = T[i];
    1653        9420 :   for (j = 2; i < L; i++) {
    1654        5970 :     S[j] = Fl_add(S[j], T[i], p);
    1655        5970 :     if (++j == l) j = 2;
    1656             :   }
    1657        3450 :   return Flx_renormalize(S, l);
    1658             : }
    1659             : /* reduce T mod (X^n + 1, p). Shallow function */
    1660             : GEN
    1661       30282 : Flx_mod_Xn1(GEN T, ulong n, ulong p)
    1662             : {
    1663       30282 :   long i, j, L = lg(T), l = n+2;
    1664             :   GEN S;
    1665       30282 :   if (L <= l || n & ~LGBITS) return T;
    1666        2682 :   S = cgetg(l, t_VECSMALL);
    1667        2682 :   S[1] = T[1];
    1668       11347 :   for (i = 2; i < l; i++) S[i] = T[i];
    1669        6974 :   for (j = 2; i < L; i++) {
    1670        4292 :     S[j] = Fl_sub(S[j], T[i], p);
    1671        4292 :     if (++j == l) j = 2;
    1672             :   }
    1673        2682 :   return Flx_renormalize(S, l);
    1674             : }
    1675             : 
    1676             : struct _Flxq {
    1677             :   GEN aut, T;
    1678             :   ulong p, pi;
    1679             : };
    1680             : /* allow pi = 0 */
    1681             : static void
    1682    73242130 : set_Flxq_pre(struct _Flxq *D, GEN T, ulong p, ulong pi)
    1683             : {
    1684    73242130 :   D->p = p;
    1685    73242130 :   D->pi = pi;
    1686    73242130 :   D->T = Flx_get_red_pre(T, p, pi);
    1687    73237648 : }
    1688             : static void
    1689       73628 : set_Flxq(struct _Flxq *D, GEN T, ulong p)
    1690       73628 : { set_Flxq_pre(D, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    1691             : 
    1692             : static GEN
    1693           0 : _Flx_divrem(void * E, GEN x, GEN y, GEN *r)
    1694             : {
    1695           0 :   struct _Flxq *D = (struct _Flxq*) E;
    1696           0 :   return Flx_divrem_pre(x, y, D->p, D->pi, r);
    1697             : }
    1698             : static GEN
    1699      578752 : _Flx_add(void * E, GEN x, GEN y) {
    1700      578752 :   struct _Flxq *D = (struct _Flxq*) E;
    1701      578752 :   return Flx_add(x, y, D->p);
    1702             : }
    1703             : static GEN
    1704    10475971 : _Flx_mul(void *E, GEN x, GEN y) {
    1705    10475971 :   struct _Flxq *D = (struct _Flxq*) E;
    1706    10475971 :   return Flx_mul_pre(x, y, D->p, D->pi);
    1707             : }
    1708             : static GEN
    1709           0 : _Flx_sqr(void *E, GEN x) {
    1710           0 :   struct _Flxq *D = (struct _Flxq*) E;
    1711           0 :   return Flx_sqr_pre(x, D->p, D->pi);
    1712             : }
    1713             : 
    1714             : static struct bb_ring Flx_ring = { _Flx_add,_Flx_mul,_Flx_sqr };
    1715             : 
    1716             : GEN
    1717           0 : Flx_digits(GEN x, GEN T, ulong p)
    1718             : {
    1719             :   struct _Flxq D;
    1720           0 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
    1721           0 :   D.p = p; D.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1722           0 :   return gen_digits(x,T,n,(void *)&D, &Flx_ring, _Flx_divrem);
    1723             : }
    1724             : 
    1725             : GEN
    1726           0 : FlxV_Flx_fromdigits(GEN x, GEN T, ulong p)
    1727             : {
    1728             :   struct _Flxq D;
    1729           0 :   D.p = p; D.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1730           0 :   return gen_fromdigits(x,T,(void *)&D, &Flx_ring);
    1731             : }
    1732             : 
    1733             : long
    1734     4153618 : Flx_val(GEN x)
    1735             : {
    1736     4153618 :   long i, l=lg(x);
    1737     4153618 :   if (l==2)  return LONG_MAX;
    1738     4162537 :   for (i=2; i<l && x[i]==0; i++) /*empty*/;
    1739     4153618 :   return i-2;
    1740             : }
    1741             : long
    1742    26276790 : Flx_valrem(GEN x, GEN *Z)
    1743             : {
    1744    26276790 :   long v, i, l=lg(x);
    1745             :   GEN y;
    1746    26276790 :   if (l==2) { *Z = Flx_copy(x); return LONG_MAX; }
    1747    28444890 :   for (i=2; i<l && x[i]==0; i++) /*empty*/;
    1748    26276790 :   v = i-2;
    1749    26276790 :   if (v == 0) { *Z = x; return 0; }
    1750     1017001 :   l -= v;
    1751     1017001 :   y = cgetg(l, t_VECSMALL); y[1] = x[1];
    1752     2612155 :   for (i=2; i<l; i++) y[i] = x[i+v];
    1753     1017902 :   *Z = y; return v;
    1754             : }
    1755             : 
    1756             : GEN
    1757    21063195 : Flx_deriv(GEN z, ulong p)
    1758             : {
    1759    21063195 :   long i,l = lg(z)-1;
    1760             :   GEN x;
    1761    21063195 :   if (l < 2) l = 2;
    1762    21063195 :   x = cgetg(l, t_VECSMALL); x[1] = z[1]; z++;
    1763    21061443 :   if (HIGHWORD(l | p))
    1764    56931240 :     for (i=2; i<l; i++) x[i] = Fl_mul((ulong)i-1, z[i], p);
    1765             :   else
    1766    85289145 :     for (i=2; i<l; i++) x[i] = ((i-1) * z[i]) % p;
    1767    21062440 :   return Flx_renormalize(x,l);
    1768             : }
    1769             : 
    1770             : static GEN
    1771      422518 : Flx_integXn(GEN x, long n, ulong p)
    1772             : {
    1773      422518 :   long i, lx = lg(x);
    1774             :   GEN y;
    1775      422518 :   if (lx == 2) return Flx_copy(x);
    1776      412705 :   y = cgetg(lx, t_VECSMALL); y[1] = x[1];
    1777     2096140 :   for (i=2; i<lx; i++)
    1778             :   {
    1779     1682893 :     ulong xi = uel(x,i);
    1780     1682893 :     if (xi == 0)
    1781       13339 :       uel(y,i) = 0;
    1782             :     else
    1783             :     {
    1784     1669554 :       ulong j = n+i-1;
    1785     1669554 :       ulong d = ugcd(j, xi);
    1786     1669502 :       if (d==1)
    1787     1018083 :         uel(y,i) = Fl_div(xi, j, p);
    1788             :       else
    1789      651419 :         uel(y,i) = Fl_div(xi/d, j/d, p);
    1790             :     }
    1791             :   }
    1792      413247 :   return Flx_renormalize(y, lx);;
    1793             : }
    1794             : 
    1795             : GEN
    1796           0 : Flx_integ(GEN x, ulong p)
    1797             : {
    1798           0 :   long i, lx = lg(x);
    1799             :   GEN y;
    1800           0 :   if (lx == 2) return Flx_copy(x);
    1801           0 :   y = cgetg(lx+1, t_VECSMALL); y[1] = x[1];
    1802           0 :   uel(y,2) = 0;
    1803           0 :   for (i=3; i<=lx; i++)
    1804           0 :     uel(y,i) = uel(x,i-1) ? Fl_div(uel(x,i-1), (i-2)%p, p): 0UL;
    1805           0 :   return Flx_renormalize(y, lx+1);;
    1806             : }
    1807             : 
    1808             : /* assume p prime */
    1809             : GEN
    1810       13447 : Flx_diff1(GEN P, ulong p)
    1811             : {
    1812       13447 :   return Flx_sub(Flx_translate1(P, p), P, p);
    1813             : }
    1814             : 
    1815             : GEN
    1816      419200 : Flx_deflate(GEN x0, long d)
    1817             : {
    1818             :   GEN z, y, x;
    1819      419200 :   long i,id, dy, dx = degpol(x0);
    1820      419200 :   if (d == 1 || dx <= 0) return Flx_copy(x0);
    1821      355946 :   dy = dx/d;
    1822      355946 :   y = cgetg(dy+3, t_VECSMALL); y[1] = x0[1];
    1823      355946 :   z = y + 2;
    1824      355946 :   x = x0+ 2;
    1825     1156850 :   for (i=id=0; i<=dy; i++,id+=d) z[i] = x[id];
    1826      355946 :   return y;
    1827             : }
    1828             : 
    1829             : GEN
    1830      157974 : Flx_inflate(GEN x0, long d)
    1831             : {
    1832      157974 :   long i, id, dy, dx = degpol(x0);
    1833      157975 :   GEN x = x0 + 2, z, y;
    1834      157975 :   if (dx <= 0) return Flx_copy(x0);
    1835      156902 :   dy = dx*d;
    1836      156902 :   y = cgetg(dy+3, t_VECSMALL); y[1] = x0[1];
    1837      156903 :   z = y + 2;
    1838     8807142 :   for (i=0; i<=dy; i++) z[i] = 0;
    1839     4288252 :   for (i=id=0; i<=dx; i++,id+=d) z[id] = x[i];
    1840      156903 :   return y;
    1841             : }
    1842             : 
    1843             : /* write p(X) = a_0(X^k) + X*a_1(X^k) + ... + X^(k-1)*a_{k-1}(X^k) */
    1844             : GEN
    1845      147495 : Flx_splitting(GEN p, long k)
    1846             : {
    1847      147495 :   long n = degpol(p), v = p[1], m, i, j, l;
    1848             :   GEN r;
    1849             : 
    1850      147492 :   m = n/k;
    1851      147492 :   r = cgetg(k+1,t_VEC);
    1852      679779 :   for(i=1; i<=k; i++)
    1853             :   {
    1854      532293 :     gel(r,i) = cgetg(m+3, t_VECSMALL);
    1855      532283 :     mael(r,i,1) = v;
    1856             :   }
    1857     4480072 :   for (j=1, i=0, l=2; i<=n; i++)
    1858             :   {
    1859     4332586 :     mael(r,j,l) = p[2+i];
    1860     4332586 :     if (j==k) { j=1; l++; } else j++;
    1861             :   }
    1862      679792 :   for(i=1; i<=k; i++)
    1863      532312 :     gel(r,i) = Flx_renormalize(gel(r,i),i<j?l+1:l);
    1864      147480 :   return r;
    1865             : }
    1866             : 
    1867             : /* ux + vy */
    1868             : static GEN
    1869      430128 : Flx_addmulmul(GEN u, GEN v, GEN x, GEN y, ulong p, ulong pi)
    1870      430128 : { return Flx_add(Flx_mul_pre(u,x, p,pi), Flx_mul_pre(v,y, p,pi), p); }
    1871             : 
    1872             : static GEN
    1873       24752 : FlxM_Flx_mul2(GEN M, GEN x, GEN y, ulong p, ulong pi)
    1874             : {
    1875       24752 :   GEN res = cgetg(3, t_COL);
    1876       24752 :   gel(res, 1) = Flx_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, p, pi);
    1877       24752 :   gel(res, 2) = Flx_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, p, pi);
    1878       24752 :   return res;
    1879             : }
    1880             : 
    1881             : #if 0
    1882             : static GEN
    1883             : FlxM_mul2_old(GEN M, GEN N, ulong p)
    1884             : {
    1885             :   GEN res = cgetg(3, t_MAT);
    1886             :   gel(res, 1) = FlxM_Flx_mul2(M,gcoeff(N,1,1),gcoeff(N,2,1),p);
    1887             :   gel(res, 2) = FlxM_Flx_mul2(M,gcoeff(N,1,2),gcoeff(N,2,2),p);
    1888             :   return res;
    1889             : }
    1890             : #endif
    1891             : /* A,B are 2x2 matrices, Flx entries. Return A x B using Strassen 7M formula */
    1892             : static GEN
    1893        6517 : FlxM_mul2(GEN A, GEN B, ulong p, ulong pi)
    1894             : {
    1895        6517 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
    1896        6517 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
    1897        6517 :   GEN M1 = Flx_mul_pre(Flx_add(A11,A22, p), Flx_add(B11,B22, p), p, pi);
    1898        6517 :   GEN M2 = Flx_mul_pre(Flx_add(A21,A22, p), B11, p, pi);
    1899        6517 :   GEN M3 = Flx_mul_pre(A11, Flx_sub(B12,B22, p), p, pi);
    1900        6517 :   GEN M4 = Flx_mul_pre(A22, Flx_sub(B21,B11, p), p, pi);
    1901        6517 :   GEN M5 = Flx_mul_pre(Flx_add(A11,A12, p), B22, p, pi);
    1902        6517 :   GEN M6 = Flx_mul_pre(Flx_sub(A21,A11, p), Flx_add(B11,B12, p), p, pi);
    1903        6517 :   GEN M7 = Flx_mul_pre(Flx_sub(A12,A22, p), Flx_add(B21,B22, p), p, pi);
    1904        6517 :   GEN T1 = Flx_add(M1,M4, p), T2 = Flx_sub(M7,M5, p);
    1905        6517 :   GEN T3 = Flx_sub(M1,M2, p), T4 = Flx_add(M3,M6, p);
    1906        6517 :   retmkmat22(Flx_add(T1,T2, p), Flx_add(M3,M5, p),
    1907             :              Flx_add(M2,M4, p), Flx_add(T3,T4, p));
    1908             : }
    1909             : 
    1910             : /* Return [0,1;1,-q]*M */
    1911             : static GEN
    1912        6345 : Flx_FlxM_qmul(GEN q, GEN M, ulong p, ulong pi)
    1913             : {
    1914        6345 :   GEN u = Flx_mul_pre(gcoeff(M,2,1), q, p, pi);
    1915        6345 :   GEN v = Flx_mul_pre(gcoeff(M,2,2), q, p, pi);
    1916        6345 :   retmkmat22(gcoeff(M,2,1), gcoeff(M,2,2),
    1917             :     Flx_sub(gcoeff(M,1,1), u, p), Flx_sub(gcoeff(M,1,2), v, p));
    1918             : }
    1919             : 
    1920             : static GEN
    1921         895 : matid2_FlxM(long v)
    1922         895 : { retmkmat22(pol1_Flx(v),pol0_Flx(v),pol0_Flx(v),pol1_Flx(v)); }
    1923             : 
    1924             : static GEN
    1925          13 : matJ2_FlxM(long v)
    1926          13 : { retmkmat22(pol0_Flx(v),pol1_Flx(v),pol1_Flx(v),pol0_Flx(v)); }
    1927             : 
    1928             : struct Flx_res
    1929             : {
    1930             :    ulong res, lc;
    1931             :    long deg0, deg1, off;
    1932             : };
    1933             : 
    1934             : INLINE void
    1935        9405 : Flx_halfres_update_pre(long da, long db, long dr, ulong p, ulong pi, struct Flx_res *res)
    1936             : {
    1937        9405 :   if (dr >= 0)
    1938             :   {
    1939        9405 :     if (res->lc != 1)
    1940             :     {
    1941        7596 :       if (pi)
    1942             :       {
    1943        3127 :         res->lc  = Fl_powu_pre(res->lc, da - dr, p, pi);
    1944        3127 :         res->res = Fl_mul_pre(res->res, res->lc, p, pi);
    1945             :       } else
    1946             :       {
    1947        4469 :         res->lc  = Fl_powu(res->lc, da - dr, p);
    1948        4469 :         res->res = Fl_mul(res->res, res->lc, p);
    1949             :       }
    1950             :     }
    1951        9405 :     if (both_odd(da + res->off, db + res->off))
    1952          63 :       res->res = Fl_neg(res->res, p);
    1953             :   } else
    1954             :   {
    1955           0 :     if (db == 0)
    1956             :     {
    1957           0 :       if (res->lc != 1)
    1958             :       {
    1959           0 :         if (pi)
    1960             :         {
    1961           0 :           res->lc  = Fl_powu_pre(res->lc, da, p, pi);
    1962           0 :           res->res = Fl_mul_pre(res->res, res->lc, p, pi);
    1963             :         } else
    1964             :         {
    1965           0 :           res->lc  = Fl_powu(res->lc, da, p);
    1966           0 :           res->res = Fl_mul(res->res, res->lc, p);
    1967             :         }
    1968             :       }
    1969             :     } else
    1970           0 :       res->res = 0;
    1971             :   }
    1972        9405 : }
    1973             : 
    1974             : static GEN
    1975     1116625 : Flx_halfres_basecase(GEN a, GEN b, ulong p, ulong pi, GEN *pa, GEN *pb, struct Flx_res *res)
    1976             : {
    1977     1116625 :   pari_sp av = avma;
    1978             :   GEN u, u1, v, v1, M;
    1979     1116625 :   long vx = a[1], n = lgpol(a)>>1;
    1980     1116623 :   u1 = v = pol0_Flx(vx);
    1981     1116614 :   u = v1 = pol1_Flx(vx);
    1982     6980126 :   while (lgpol(b)>n)
    1983             :   {
    1984             :     GEN r, q;
    1985     5863522 :     q = Flx_divrem_pre(a,b,p,pi, &r);
    1986     5863518 :     if (res)
    1987             :     {
    1988        8362 :       long da = degpol(a), db=degpol(b), dr = degpol(r);
    1989        8362 :       res->lc = b[db+2];
    1990        8362 :       if (dr >= n)
    1991        7133 :         Flx_halfres_update_pre(da, db, dr, p, pi, res);
    1992             :       else
    1993             :       {
    1994        1229 :         res->deg0 = da;
    1995        1229 :         res->deg1 = db;
    1996             :       }
    1997             :     }
    1998     5863518 :     a = b; b = r; swap(u,u1); swap(v,v1);
    1999     5863518 :     u1 = Flx_sub(u1, Flx_mul(u, q, p), p);
    2000     5863454 :     v1 = Flx_sub(v1, Flx_mul(v, q, p), p);
    2001     5863515 :     if (gc_needed(av,2))
    2002             :     {
    2003           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_halfgcd (d = %ld)",degpol(b));
    2004           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
    2005             :     }
    2006             :   }
    2007     1116470 :   M = mkmat22(u,v,u1,v1); *pa = a; *pb = b;
    2008     1116584 :   return gc_all(av,3, &M, pa, pb);
    2009             : }
    2010             : 
    2011             : static GEN Flx_halfres_i(GEN x, GEN y, ulong p, ulong pi, GEN *a, GEN *b, struct Flx_res *res);
    2012             : 
    2013             : static GEN
    2014       19282 : Flx_halfres_split(GEN x, GEN y, ulong p, ulong pi, GEN *a, GEN *b, struct Flx_res *res)
    2015             : {
    2016       19282 :   pari_sp av = avma;
    2017             :   GEN R, S, T, V1, V2;
    2018             :   GEN x1, y1, r, q;
    2019       19282 :   long l = lgpol(x), n = l>>1, k;
    2020       19282 :   if (lgpol(y) <= n)
    2021         855 :     { *a = Flx_copy(x); *b = Flx_copy(y); return matid2_FlxM(x[1]); }
    2022       18427 :   if (res)
    2023             :   {
    2024        3263 :      res->lc = Flx_lead(y);
    2025        3263 :      res->deg0 -= n;
    2026        3263 :      res->deg1 -= n;
    2027        3263 :      res->off += n;
    2028             :   }
    2029       18427 :   R = Flx_halfres_i(Flx_shift(x,-n),Flx_shift(y,-n),p,pi,a,b,res);
    2030       18426 :   if (res)
    2031             :   {
    2032        3262 :     res->off -= n;
    2033        3262 :     res->deg0 += n;
    2034        3262 :     res->deg1 += n;
    2035             :   }
    2036       18426 :   V1 = FlxM_Flx_mul2(R, Flxn_red(x,n), Flxn_red(y,n), p, pi);
    2037       18427 :   x1 = Flx_add(Flx_shift(*a,n), gel(V1,1), p);
    2038       18427 :   y1 = Flx_add(Flx_shift(*b,n), gel(V1,2), p);
    2039       18426 :   if (lgpol(y1) <= n)
    2040       12102 :     { *a = x1; *b = y1; return gc_all(av, 3, &R, a, b); }
    2041        6325 :   k = 2*n-degpol(y1);
    2042        6325 :   q = Flx_divrem_pre(x1, y1, p, pi, &r);
    2043        6325 :   if (res)
    2044             :   {
    2045        1043 :     long dx1 = degpol(x1), dy1 = degpol(y1), dr = degpol(r);
    2046        1043 :     if (dy1 < degpol(y))
    2047         185 :       Flx_halfres_update_pre(res->deg0, res->deg1, dy1, p, pi, res);
    2048        1043 :     res->lc = uel(y1, dy1+2);
    2049        1043 :     res->deg0 = dx1;
    2050        1043 :     res->deg1 = dy1;
    2051        1043 :     if (dr >= n)
    2052             :     {
    2053        1043 :       Flx_halfres_update_pre(dx1, dy1, dr, p, pi, res);
    2054        1043 :       res->deg0 = dy1;
    2055        1043 :       res->deg1 = dr;
    2056             :     }
    2057        1043 :     res->deg0 -= k;
    2058        1043 :     res->deg1 -= k;
    2059        1043 :     res->off += k;
    2060             :   }
    2061        6325 :   S = Flx_halfres_i(Flx_shift(y1,-k), Flx_shift(r,-k), p, pi, a, b, res);
    2062        6325 :   if (res)
    2063             :   {
    2064        1043 :     res->deg0 += k;
    2065        1043 :     res->deg1 += k;
    2066        1043 :     res->off -= k;
    2067             :   }
    2068        6325 :   T = FlxM_mul2(S, Flx_FlxM_qmul(q, R, p,pi), p, pi);
    2069        6325 :   V2 = FlxM_Flx_mul2(S, Flxn_red(y1,k), Flxn_red(r,k), p, pi);
    2070        6325 :   *a = Flx_add(Flx_shift(*a,k), gel(V2,1), p);
    2071        6325 :   *b = Flx_add(Flx_shift(*b,k), gel(V2,2), p);
    2072        6325 :   return gc_all(av, 3, &T, a, b);
    2073             : }
    2074             : 
    2075             : static GEN
    2076     1135907 : Flx_halfres_i(GEN x, GEN y, ulong p, ulong pi, GEN *a, GEN *b, struct Flx_res *res)
    2077             : {
    2078     1135907 :   if (lgpol(x) < get_Fl_threshold(p, Flx_HALFGCD_LIMIT, Flx_HALFGCD2_LIMIT))
    2079     1116625 :     return Flx_halfres_basecase(x, y, p, pi, a, b, res);
    2080       19282 :   return Flx_halfres_split(x, y, p, pi, a, b, res);
    2081             : }
    2082             : 
    2083             : static GEN
    2084     1110111 : Flx_halfgcd_all_i(GEN x, GEN y, ulong p, ulong pi, GEN *pa, GEN *pb)
    2085             : {
    2086             :   GEN a, b, R;
    2087     1110111 :   R = Flx_halfres_i(x, y, p, pi, &a, &b, NULL);
    2088     1110124 :   if (pa) *pa = a;
    2089     1110124 :   if (pb) *pb = b;
    2090     1110124 :   return R;
    2091             : }
    2092             : 
    2093             : /* Return M in GL_2(Fl[X]) such that:
    2094             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
    2095             : */
    2096             : 
    2097             : GEN
    2098     1110120 : Flx_halfgcd_all_pre(GEN x, GEN y, ulong p, ulong pi, GEN *a, GEN *b)
    2099             : {
    2100             :   pari_sp av;
    2101             :   GEN R, q ,r;
    2102     1110120 :   long lx = lgpol(x), ly = lgpol(y);
    2103     1110111 :   if (!lx)
    2104             :   {
    2105           0 :     if (a) *a = Flx_copy(y);
    2106           0 :     if (b) *b = Flx_copy(x);
    2107           0 :     return matJ2_FlxM(x[1]);
    2108             :   }
    2109     1110111 :   if (ly < lx) return Flx_halfgcd_all_i(x, y, p, pi, a, b);
    2110        8542 :   av = avma;
    2111        8542 :   q = Flx_divrem(y,x,p,&r);
    2112        8542 :   R = Flx_halfgcd_all_i(x, r, p, pi, a, b);
    2113        8542 :   gcoeff(R,1,1) = Flx_sub(gcoeff(R,1,1), Flx_mul_pre(q,gcoeff(R,1,2), p,pi), p);
    2114        8542 :   gcoeff(R,2,1) = Flx_sub(gcoeff(R,2,1), Flx_mul_pre(q,gcoeff(R,2,2), p,pi), p);
    2115        8542 :   return !a && b ? gc_all(av, 2, &R, b): gc_all(av, 1+!!a+!!b, &R, a, b);
    2116             : }
    2117             : 
    2118             : GEN
    2119         154 : Flx_halfgcd_all(GEN x, GEN y, ulong p, GEN *a, GEN *b)
    2120         154 : { return Flx_halfgcd_all_pre(x, y, p, SMALL_ULONG(p)? 0: get_Fl_red(p), a, b); }
    2121             : 
    2122             : GEN
    2123      845171 : Flx_halfgcd_pre(GEN x, GEN y, ulong p, ulong pi)
    2124      845171 : { return Flx_halfgcd_all_pre(x, y, p, pi, NULL, NULL); }
    2125             : 
    2126             : GEN
    2127           0 : Flx_halfgcd(GEN x, GEN y, ulong p)
    2128           0 : { return Flx_halfgcd_pre(x, y, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2129             : 
    2130             : /*Do not garbage collect*/
    2131             : static GEN
    2132    82946015 : Flx_gcd_basecase(GEN a, GEN b, ulong p, ulong pi)
    2133             : {
    2134    82946015 :   pari_sp av = avma;
    2135    82946015 :   ulong iter = 0;
    2136    82946015 :   if (lg(b) > lg(a)) swap(a, b);
    2137   286246987 :   while (lgpol(b))
    2138             :   {
    2139   202890287 :     GEN c = Flx_rem_pre(a,b,p,pi);
    2140   203300972 :     iter++; a = b; b = c;
    2141   203300972 :     if (gc_needed(av,2))
    2142             :     {
    2143           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_gcd (d = %ld)",degpol(c));
    2144           0 :       gerepileall(av,2, &a,&b);
    2145             :     }
    2146             :   }
    2147    82884035 :   return iter < 2 ? Flx_copy(a) : a;
    2148             : }
    2149             : 
    2150             : GEN
    2151    84584975 : Flx_gcd_pre(GEN x, GEN y, ulong p, ulong pi)
    2152             : {
    2153    84584975 :   pari_sp av = avma;
    2154             :   long lim;
    2155    84584975 :   if (!lgpol(x)) return Flx_copy(y);
    2156    82946277 :   lim = get_Fl_threshold(p, Flx_GCD_LIMIT, Flx_GCD2_LIMIT);
    2157    82953320 :   while (lgpol(y) >= lim)
    2158             :   {
    2159         150 :     if (lgpol(y)<=(lgpol(x)>>1))
    2160             :     {
    2161           0 :       GEN r = Flx_rem_pre(x, y, p, pi);
    2162           0 :       x = y; y = r;
    2163             :     }
    2164         150 :     (void) Flx_halfgcd_all_pre(x, y, p, pi, &x, &y);
    2165         150 :     if (gc_needed(av,2))
    2166             :     {
    2167           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_gcd (y = %ld)",degpol(y));
    2168           0 :       gerepileall(av,2,&x,&y);
    2169             :     }
    2170             :   }
    2171    82942589 :   return gerepileuptoleaf(av, Flx_gcd_basecase(x,y,p,pi));
    2172             : }
    2173             : GEN
    2174    32486462 : Flx_gcd(GEN x, GEN y, ulong p)
    2175    32486462 : { return Flx_gcd_pre(x, y, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2176             : 
    2177             : int
    2178     8524473 : Flx_is_squarefree(GEN z, ulong p)
    2179             : {
    2180     8524473 :   pari_sp av = avma;
    2181     8524473 :   GEN d = Flx_gcd(z, Flx_deriv(z,p) , p);
    2182     8524370 :   return gc_bool(av, degpol(d) == 0);
    2183             : }
    2184             : 
    2185             : static long
    2186      126398 : Flx_is_smooth_squarefree(GEN f, long r, ulong p, ulong pi)
    2187             : {
    2188      126398 :   pari_sp av = avma;
    2189             :   long i;
    2190      126398 :   GEN sx = polx_Flx(f[1]), a = sx;
    2191      530099 :   for(i=1;;i++)
    2192             :   {
    2193      530099 :     if (degpol(f)<=r) return gc_long(av,1);
    2194      508061 :     a = Flxq_powu_pre(Flx_rem_pre(a,f,p,pi), p, f, p, pi);
    2195      508009 :     if (Flx_equal(a, sx)) return gc_long(av,1);
    2196      504356 :     if (i==r) return gc_long(av,0);
    2197      403459 :     f = Flx_div_pre(f, Flx_gcd_pre(Flx_sub(a,sx,p),f,p,pi),p,pi);
    2198             :   }
    2199             : }
    2200             : 
    2201             : static long
    2202        8365 : Flx_is_l_pow(GEN x, ulong p)
    2203             : {
    2204        8365 :   ulong i, lx = lgpol(x);
    2205       16603 :   for (i=1; i<lx; i++)
    2206       14909 :     if (x[i+2] && i%p) return 0;
    2207        1694 :   return 1;
    2208             : }
    2209             : 
    2210             : int
    2211      126412 : Flx_is_smooth_pre(GEN g, long r, ulong p, ulong pi)
    2212             : {
    2213             :   while (1)
    2214        8365 :   {
    2215      126412 :     GEN f = Flx_gcd_pre(g, Flx_deriv(g, p), p, pi);
    2216      126236 :     if (!Flx_is_smooth_squarefree(Flx_div_pre(g, f, p, pi), r, p, pi))
    2217      100909 :       return 0;
    2218       25508 :     if (degpol(f)==0) return 1;
    2219        8353 :     g = Flx_is_l_pow(f,p) ? Flx_deflate(f, p): f;
    2220             :   }
    2221             : }
    2222             : int
    2223       74256 : Flx_is_smooth(GEN g, long r, ulong p)
    2224       74256 : { return Flx_is_smooth_pre(g, r, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2225             : 
    2226             : static GEN
    2227     6764324 : Flx_extgcd_basecase(GEN a, GEN b, ulong p, ulong pi, GEN *ptu, GEN *ptv)
    2228             : {
    2229     6764324 :   pari_sp av=avma;
    2230             :   GEN u,v,u1,v1;
    2231     6764324 :   long vx = a[1];
    2232     6764324 :   v = pol0_Flx(vx); v1 = pol1_Flx(vx);
    2233     6764169 :   if (ptu) { u = pol1_Flx(vx); u1 = pol0_Flx(vx); }
    2234    29755069 :   while (lgpol(b))
    2235             :   {
    2236    22989897 :     GEN r, q = Flx_divrem_pre(a,b,p,pi, &r);
    2237    22991245 :     a = b; b = r;
    2238    22991245 :     if (ptu)
    2239             :     {
    2240     2475868 :       swap(u,u1);
    2241     2475868 :       u1 = Flx_sub(u1, Flx_mul_pre(u, q, p, pi), p);
    2242             :     }
    2243    22991246 :     swap(v,v1);
    2244    22991246 :     v1 = Flx_sub(v1, Flx_mul_pre(v, q, p, pi), p);
    2245    22990888 :     if (gc_needed(av,2))
    2246             :     {
    2247           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_extgcd (d = %ld)",degpol(a));
    2248           0 :       gerepileall(av,ptu ? 6: 4, &a,&b,&v,&v1,&u,&u1);
    2249             :     }
    2250             :   }
    2251     6764199 :   if (ptu) *ptu = u;
    2252     6764199 :   *ptv = v;
    2253     6764199 :   return a;
    2254             : }
    2255             : 
    2256             : static GEN
    2257      152021 : Flx_extgcd_halfgcd(GEN x, GEN y, ulong p, ulong pi, GEN *ptu, GEN *ptv)
    2258             : {
    2259             :   GEN u, v;
    2260      152021 :   long lim = get_Fl_threshold(p, Flx_EXTGCD_LIMIT, Flx_EXTGCD2_LIMIT);
    2261      152021 :   GEN V = cgetg(expu(lgpol(y))+2,t_VEC);
    2262      152021 :   long i, n = 0, vs = x[1];
    2263      415022 :   while (lgpol(y) >= lim)
    2264             :   {
    2265      263000 :     if (lgpol(y)<=(lgpol(x)>>1))
    2266             :     {
    2267          26 :       GEN r, q = Flx_divrem_pre(x, y, p, pi, &r);
    2268          26 :       x = y; y = r;
    2269          26 :       gel(V,++n) = mkmat22(pol0_Flx(vs),pol1_Flx(vs),pol1_Flx(vs),Flx_neg(q,p));
    2270             :     } else
    2271      262974 :       gel(V,++n) = Flx_halfgcd_all_pre(x, y, p, pi, &x, &y);
    2272             :   }
    2273      152020 :   y = Flx_extgcd_basecase(x,y,p,pi,&u,&v);
    2274      262998 :   for (i = n; i>1; i--)
    2275             :   {
    2276      110979 :     GEN R = gel(V,i);
    2277      110979 :     GEN u1 = Flx_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), p, pi);
    2278      110979 :     GEN v1 = Flx_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), p, pi);
    2279      110979 :     u = u1; v = v1;
    2280             :   }
    2281             :   {
    2282      152019 :     GEN R = gel(V,1);
    2283      152019 :     if (ptu)
    2284        6543 :       *ptu = Flx_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), p, pi);
    2285      152019 :     *ptv   = Flx_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), p, pi);
    2286             :   }
    2287      152022 :   return y;
    2288             : }
    2289             : 
    2290             : /* x and y in Z[X], return lift(gcd(x mod p, y mod p)). Set u and v st
    2291             :  * ux + vy = gcd (mod p) */
    2292             : GEN
    2293     6764327 : Flx_extgcd_pre(GEN x, GEN y, ulong p, ulong pi, GEN *ptu, GEN *ptv)
    2294             : {
    2295     6764327 :   pari_sp av = avma;
    2296             :   GEN d;
    2297     6764327 :   long lim = get_Fl_threshold(p, Flx_EXTGCD_LIMIT, Flx_EXTGCD2_LIMIT);
    2298     6764328 :   if (lgpol(y) >= lim)
    2299      152021 :     d = Flx_extgcd_halfgcd(x, y, p, pi, ptu, ptv);
    2300             :   else
    2301     6612301 :     d = Flx_extgcd_basecase(x, y, p, pi, ptu, ptv);
    2302     6764203 :   return gc_all(av, ptu?3:2, &d, ptv, ptu);
    2303             : }
    2304             : GEN
    2305      852682 : Flx_extgcd(GEN x, GEN y, ulong p, GEN *ptu, GEN *ptv)
    2306      852682 : { return Flx_extgcd_pre(x, y, p, SMALL_ULONG(p)? 0: get_Fl_red(p), ptu, ptv); }
    2307             : 
    2308             : static GEN
    2309        1044 : Flx_halfres_pre(GEN x, GEN y, ulong p, ulong pi, GEN *a, GEN *b, ulong *r)
    2310             : {
    2311             :   struct Flx_res res;
    2312             :   GEN R;
    2313             :   long dB;
    2314             : 
    2315        1044 :   res.res  = *r;
    2316        1044 :   res.lc   = Flx_lead(y);
    2317        1044 :   res.deg0 = degpol(x);
    2318        1044 :   res.deg1 = degpol(y);
    2319        1044 :   res.off = 0;
    2320        1044 :   R = Flx_halfres_i(x, y, p, pi, a, b, &res);
    2321        1044 :   dB = degpol(*b);
    2322        1044 :   if (dB < degpol(y))
    2323        1044 :     Flx_halfres_update_pre(res.deg0, res.deg1, dB, p, pi, &res);
    2324        1044 :   *r = res.res;
    2325        1044 :   return R;
    2326             : }
    2327             : 
    2328             : static ulong
    2329    10184435 : Flx_resultant_basecase_pre(GEN a, GEN b, ulong p, ulong pi)
    2330             : {
    2331             :   pari_sp av;
    2332             :   long da,db,dc;
    2333    10184435 :   ulong lb, res = 1UL;
    2334             :   GEN c;
    2335             : 
    2336    10184435 :   da = degpol(a);
    2337    10184325 :   db = degpol(b);
    2338    10184513 :   if (db > da)
    2339             :   {
    2340           0 :     swapspec(a,b, da,db);
    2341           0 :     if (both_odd(da,db)) res = p-res;
    2342             :   }
    2343    10184513 :   else if (!da) return 1; /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    2344    10184513 :   av = avma;
    2345   106821119 :   while (db)
    2346             :   {
    2347    96655923 :     lb = b[db+2];
    2348    96655923 :     c = Flx_rem_pre(a,b, p,pi);
    2349    96303609 :     a = b; b = c; dc = degpol(c);
    2350    96281683 :     if (dc < 0) return gc_long(av,0);
    2351             : 
    2352    96276285 :     if (both_odd(da,db)) res = p - res;
    2353    96269110 :     if (lb != 1) res = Fl_mul(res, Fl_powu_pre(lb, da - dc, p, pi), p);
    2354    96635435 :     if (gc_needed(av,2))
    2355             :     {
    2356           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant (da = %ld)",da);
    2357           0 :       gerepileall(av,2, &a,&b);
    2358             :     }
    2359    96636606 :     da = db; /* = degpol(a) */
    2360    96636606 :     db = dc; /* = degpol(b) */
    2361             :   }
    2362    10165196 :   return gc_ulong(av, Fl_mul(res, Fl_powu_pre(b[2], da, p, pi), p));
    2363             : }
    2364             : 
    2365             : ulong
    2366    10186409 : Flx_resultant_pre(GEN x, GEN y, ulong p, ulong pi)
    2367             : {
    2368    10186409 :   pari_sp av = avma;
    2369             :   long lim;
    2370    10186409 :   ulong res = 1;
    2371    10186409 :   long dx = degpol(x), dy = degpol(y);
    2372    10185958 :   if (dx < 0 || dy < 0) return 0;
    2373    10184516 :   if (dx < dy)
    2374             :   {
    2375     1065958 :     swap(x,y);
    2376     1065958 :     if (both_odd(dx, dy))
    2377        1906 :       res = Fl_neg(res, p);
    2378             :   }
    2379    10184516 :   lim = get_Fl_threshold(p, Flx_GCD_LIMIT, Flx_GCD2_LIMIT);
    2380    10185400 :   while (lgpol(y) >= lim)
    2381             :   {
    2382         852 :     if (lgpol(y)<=(lgpol(x)>>1))
    2383             :     {
    2384           0 :       GEN r = Flx_rem_pre(x, y, p, pi);
    2385           0 :       long dx = degpol(x), dy = degpol(y), dr = degpol(r);
    2386           0 :       ulong ly = y[dy+2];
    2387           0 :       if (ly != 1) res = Fl_mul(res, Fl_powu_pre(ly, dx - dr, p, pi), p);
    2388           0 :       if (both_odd(dx, dy))
    2389           0 :         res = Fl_neg(res, p);
    2390           0 :       x = y; y = r;
    2391             :     }
    2392         852 :     (void) Flx_halfres_pre(x, y, p, pi, &x, &y, &res);
    2393         852 :     if (gc_needed(av,2))
    2394             :     {
    2395           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_res (y = %ld)",degpol(y));
    2396           0 :       gerepileall(av,2,&x,&y);
    2397             :     }
    2398             :   }
    2399    10184451 :   return gc_ulong(av, Fl_mul(res, Flx_resultant_basecase_pre(x, y, p, pi), p));
    2400             : }
    2401             : 
    2402             : ulong
    2403     4654465 : Flx_resultant(GEN a, GEN b, ulong p)
    2404     4654465 : { return Flx_resultant_pre(a, b, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2405             : 
    2406             : /* If resultant is 0, *ptU and *ptV are not set */
    2407             : static ulong
    2408          53 : Flx_extresultant_basecase(GEN a, GEN b, ulong p, ulong pi, GEN *ptU, GEN *ptV)
    2409             : {
    2410          53 :   GEN z,q,u,v, x = a, y = b;
    2411          53 :   ulong lb, res = 1UL;
    2412          53 :   pari_sp av = avma;
    2413             :   long dx, dy, dz;
    2414          53 :   long vs = a[1];
    2415             : 
    2416          53 :   u = pol0_Flx(vs);
    2417          53 :   v = pol1_Flx(vs); /* v = 1 */
    2418          53 :   dx = degpol(x);
    2419          53 :   dy = degpol(y);
    2420         764 :   while (dy)
    2421             :   { /* b u = x (a), b v = y (a) */
    2422         711 :     lb = y[dy+2];
    2423         711 :     q = Flx_divrem_pre(x,y, p, pi, &z);
    2424         711 :     x = y; y = z; /* (x,y) = (y, x - q y) */
    2425         711 :     dz = degpol(z); if (dz < 0) return gc_ulong(av,0);
    2426         711 :     z = Flx_sub(u, Flx_mul_pre(q,v, p, pi), p);
    2427         711 :     u = v; v = z; /* (u,v) = (v, u - q v) */
    2428             : 
    2429         711 :     if (both_odd(dx,dy)) res = p - res;
    2430         711 :     if (lb != 1) res = Fl_mul(res, Fl_powu_pre(lb, dx-dz, p, pi), p);
    2431         711 :     dx = dy; /* = degpol(x) */
    2432         711 :     dy = dz; /* = degpol(y) */
    2433             :   }
    2434          53 :   res = Fl_mul(res, Fl_powu_pre(y[2], dx, p, pi), p);
    2435          53 :   lb = Fl_mul(res, Fl_inv(y[2],p), p);
    2436          53 :   v = gerepileuptoleaf(av, Flx_Fl_mul_pre(v, lb, p, pi));
    2437          53 :   av = avma;
    2438          53 :   u = Flx_sub(Fl_to_Flx(res,vs), Flx_mul_pre(b,v,p,pi), p);
    2439          53 :   u = gerepileuptoleaf(av, Flx_div_pre(u,a,p,pi)); /* = (res - b v) / a */
    2440          53 :   *ptU = u;
    2441          53 :   *ptV = v; return res;
    2442             : }
    2443             : 
    2444             : ulong
    2445          53 : Flx_extresultant_pre(GEN x, GEN y, ulong p, ulong pi, GEN *ptU, GEN *ptV)
    2446             : {
    2447          53 :   pari_sp av=avma;
    2448             :   GEN u, v, R;
    2449          53 :   long lim = get_Fl_threshold(p, Flx_EXTGCD_LIMIT, Flx_EXTGCD2_LIMIT);
    2450          53 :   ulong res = 1, res1;
    2451          53 :   long dx = degpol(x), dy = degpol(y);
    2452          53 :   if (dy > dx)
    2453             :   {
    2454          13 :     swap(x,y); lswap(dx,dy);
    2455          13 :     if (both_odd(dx,dy)) res = p-res;
    2456          13 :     R = matJ2_FlxM(x[1]);
    2457          40 :   } else R = matid2_FlxM(x[1]);
    2458          53 :   if (dy < 0) return 0;
    2459         245 :   while (lgpol(y) >= lim)
    2460             :   {
    2461             :     GEN M;
    2462         192 :     if (lgpol(y)<=(lgpol(x)>>1))
    2463             :     {
    2464          20 :       GEN r, q = Flx_divrem_pre(x, y, p, pi, &r);
    2465          20 :       long dx = degpol(x), dy = degpol(y), dr = degpol(r);
    2466          20 :       ulong ly = y[dy+2];
    2467          20 :       if (ly != 1) res = Fl_mul(res, Fl_powu_pre(ly, dx - dr, p, pi), p);
    2468          20 :       if (both_odd(dx, dy))
    2469           0 :         res = Fl_neg(res, p);
    2470          20 :       x = y; y = r;
    2471          20 :       R = Flx_FlxM_qmul(q, R, p,pi);
    2472             :     }
    2473         192 :     M = Flx_halfres_pre(x, y, p, pi, &x, &y, &res);
    2474         192 :     if (!res) return gc_ulong(av, 0);
    2475         192 :     R = FlxM_mul2(M, R, p, pi);
    2476         192 :     gerepileall(av,3,&x,&y,&R);
    2477             :   }
    2478          53 :   res1 = Flx_extresultant_basecase(x,y,p,pi,&u,&v);
    2479          53 :   if (!res1) return gc_ulong(av, 0);
    2480          53 :   *ptU = Flx_Fl_mul_pre(Flx_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), p, pi), res, p, pi);
    2481          53 :   *ptV = Flx_Fl_mul_pre(Flx_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), p, pi), res, p, pi);
    2482          53 :   gerepileall(av, 2, ptU, ptV);
    2483          53 :   return Fl_mul(res1,res,p);
    2484             : }
    2485             : 
    2486             : ulong
    2487          53 : Flx_extresultant(GEN a, GEN b, ulong p, GEN *ptU, GEN *ptV)
    2488          53 : { return Flx_extresultant_pre(a, b, p, SMALL_ULONG(p)? 0: get_Fl_red(p), ptU, ptV); }
    2489             : 
    2490             : /* allow pi = 0 (SMALL_ULONG) */
    2491             : ulong
    2492    43718954 : Flx_eval_powers_pre(GEN x, GEN y, ulong p, ulong pi)
    2493             : {
    2494    43718954 :   ulong l0, l1, h0, h1, v1,  i = 1, lx = lg(x)-1;
    2495             : 
    2496    43718954 :   if (lx == 1) return 0;
    2497    40965615 :   x++;
    2498    40965615 :   if (pi)
    2499             :   {
    2500             :     LOCAL_OVERFLOW;
    2501             :     LOCAL_HIREMAINDER;
    2502    40900727 :     l1 = mulll(uel(x,i), uel(y,i)); h1 = hiremainder; v1 = 0;
    2503    97670351 :     while (++i < lx)
    2504             :     {
    2505    56769624 :       l0 = mulll(uel(x,i), uel(y,i)); h0 = hiremainder;
    2506    56769624 :       l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
    2507             :     }
    2508       81118 :     return v1? remlll_pre(v1, h1, l1, p, pi)
    2509    40981845 :              : remll_pre(h1, l1, p, pi);
    2510             :   }
    2511             :   else
    2512             :   {
    2513       64888 :     l1 = x[i] * y[i];
    2514    30927105 :     while (++i < lx) { l1 += x[i] * y[i]; if (l1 & HIGHBIT) l1 %= p; }
    2515       64888 :     return l1 % p;
    2516             :   }
    2517             : }
    2518             : 
    2519             : /* allow pi = 0 (SMALL_ULONG) */
    2520             : ulong
    2521   100617610 : Flx_eval_pre(GEN x, ulong y, ulong p, ulong pi)
    2522             : {
    2523   100617610 :   long i, n = degpol(x);
    2524             :   ulong t;
    2525   100617548 :   if (n <= 0) return n? 0: x[2];
    2526    32926876 :   if (n > 15)
    2527             :   {
    2528      180365 :     pari_sp av = avma;
    2529      180365 :     GEN v = Fl_powers_pre(y, n, p, pi);
    2530      180371 :     return gc_ulong(av, Flx_eval_powers_pre(x, v, p, pi));
    2531             :   }
    2532    32746511 :   i = n+2; t = x[i];
    2533    32746511 :   if (pi)
    2534             :   {
    2535   123084022 :     for (i--; i>=2; i--) t = Fl_addmul_pre(uel(x, i), t, y, p, pi);
    2536    31644319 :     return t;
    2537             :   }
    2538     2671145 :   for (i--; i>=2; i--) t = (t * y + x[i]) % p;
    2539     1115557 :   return t %= p;
    2540             : }
    2541             : ulong
    2542    20389717 : Flx_eval(GEN x, ulong y, ulong p)
    2543    20389717 : { return Flx_eval_pre(x, y, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2544             : 
    2545             : ulong
    2546        3255 : Flv_prod_pre(GEN x, ulong p, ulong pi)
    2547             : {
    2548        3255 :   pari_sp ltop = avma;
    2549             :   GEN v;
    2550        3255 :   long i,k,lx = lg(x);
    2551        3255 :   if (lx == 1) return 1UL;
    2552        3255 :   if (lx == 2) return uel(x,1);
    2553        3003 :   v = cgetg(1+(lx << 1), t_VECSMALL);
    2554        3003 :   k = 1;
    2555       28593 :   for (i=1; i<lx-1; i+=2)
    2556       25590 :     uel(v,k++) = Fl_mul_pre(uel(x,i), uel(x,i+1), p, pi);
    2557        3003 :   if (i < lx) uel(v,k++) = uel(x,i);
    2558       13529 :   while (k > 2)
    2559             :   {
    2560       10526 :     lx = k; k = 1;
    2561       36116 :     for (i=1; i<lx-1; i+=2)
    2562       25590 :       uel(v,k++) = Fl_mul_pre(uel(v,i), uel(v,i+1), p, pi);
    2563       10526 :     if (i < lx) uel(v,k++) = uel(v,i);
    2564             :   }
    2565        3003 :   return gc_ulong(ltop, uel(v,1));
    2566             : }
    2567             : 
    2568             : ulong
    2569           0 : Flv_prod(GEN v, ulong p)
    2570             : {
    2571           0 :   return Flv_prod_pre(v, p, get_Fl_red(p));
    2572             : }
    2573             : 
    2574             : GEN
    2575           0 : FlxV_prod(GEN V, ulong p)
    2576             : {
    2577             :   struct _Flxq D;
    2578           0 :   D.T = NULL; D.aut = NULL; D.p = p; D.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2579           0 :   return gen_product(V, (void *)&D, &_Flx_mul);
    2580             : }
    2581             : 
    2582             : /* compute prod (x - a[i]) */
    2583             : GEN
    2584      737461 : Flv_roots_to_pol(GEN a, ulong p, long vs)
    2585             : {
    2586             :   struct _Flxq D;
    2587      737461 :   long i,k,lx = lg(a);
    2588             :   GEN p1;
    2589      737461 :   if (lx == 1) return pol1_Flx(vs);
    2590      737461 :   p1 = cgetg(lx, t_VEC);
    2591    11899424 :   for (k=1,i=1; i<lx-1; i+=2)
    2592    11160520 :     gel(p1,k++) = mkvecsmall4(vs, Fl_mul(a[i], a[i+1], p),
    2593    11162280 :                               Fl_neg(Fl_add(a[i],a[i+1],p),p), 1);
    2594      737144 :   if (i < lx)
    2595       58112 :     gel(p1,k++) = mkvecsmall3(vs, Fl_neg(a[i],p), 1);
    2596      737141 :   D.T = NULL; D.aut = NULL; D.p = p; D.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2597      737136 :   setlg(p1, k); return gen_product(p1, (void *)&D, _Flx_mul);
    2598             : }
    2599             : 
    2600             : /* set v[i] = w[i]^{-1}; may be called with w = v, suitable for "large" p */
    2601             : INLINE void
    2602    18883348 : Flv_inv_pre_indir(GEN w, GEN v, ulong p, ulong pi)
    2603             : {
    2604    18883348 :   pari_sp av = avma;
    2605    18883348 :   long n = lg(w), i;
    2606             :   ulong u;
    2607             :   GEN c;
    2608             : 
    2609    18883348 :   if (n == 1) return;
    2610    18883348 :   c = cgetg(n, t_VECSMALL); c[1] = w[1];
    2611    80319758 :   for (i = 2; i < n; ++i) c[i] = Fl_mul_pre(w[i], c[i-1], p, pi);
    2612    19047797 :   i = n-1; u = Fl_inv(c[i], p);
    2613    80622375 :   for ( ; i > 1; --i)
    2614             :   {
    2615    61542431 :     ulong t = Fl_mul_pre(u, c[i-1], p, pi);
    2616    61521225 :     u = Fl_mul_pre(u, w[i], p, pi); v[i] = t;
    2617             :   }
    2618    19079944 :   v[1] = u; set_avma(av);
    2619             : }
    2620             : 
    2621             : void
    2622    18277880 : Flv_inv_pre_inplace(GEN v, ulong p, ulong pi) { Flv_inv_pre_indir(v,v, p, pi); }
    2623             : 
    2624             : GEN
    2625       10850 : Flv_inv_pre(GEN w, ulong p, ulong pi)
    2626       10850 : { GEN v = cgetg(lg(w), t_VECSMALL); Flv_inv_pre_indir(w, v, p, pi); return v; }
    2627             : 
    2628             : /* set v[i] = w[i]^{-1}; may be called with w = v, suitable for SMALL_ULONG p */
    2629             : INLINE void
    2630       49734 : Flv_inv_indir(GEN w, GEN v, ulong p)
    2631             : {
    2632       49734 :   pari_sp av = avma;
    2633       49734 :   long n = lg(w), i;
    2634             :   ulong u;
    2635             :   GEN c;
    2636             : 
    2637       49734 :   if (n == 1) return;
    2638       49734 :   c = cgetg(n, t_VECSMALL); c[1] = w[1];
    2639     1718581 :   for (i = 2; i < n; ++i) c[i] = Fl_mul(w[i], c[i-1], p);
    2640       49739 :   i = n-1; u = Fl_inv(c[i], p);
    2641     1718557 :   for ( ; i > 1; --i)
    2642             :   {
    2643     1668822 :     ulong t = Fl_mul(u, c[i-1], p);
    2644     1668820 :     u = Fl_mul(u, w[i], p); v[i] = t;
    2645             :   }
    2646       49735 :   v[1] = u; set_avma(av);
    2647             : }
    2648             : static void
    2649      635561 : Flv_inv_i(GEN v, GEN w, ulong p)
    2650             : {
    2651      635561 :   if (SMALL_ULONG(p)) Flv_inv_indir(w, v, p);
    2652      585827 :   else Flv_inv_pre_indir(w, v, p, get_Fl_red(p));
    2653      635562 : }
    2654             : void
    2655       12017 : Flv_inv_inplace(GEN v, ulong p) { Flv_inv_i(v, v, p); }
    2656             : GEN
    2657      623547 : Flv_inv(GEN w, ulong p)
    2658      623547 : { GEN v = cgetg(lg(w), t_VECSMALL); Flv_inv_i(v, w, p); return v; }
    2659             : 
    2660             : GEN
    2661    33001896 : Flx_div_by_X_x(GEN a, ulong x, ulong p, ulong *rem)
    2662             : {
    2663    33001896 :   long l = lg(a), i;
    2664             :   GEN a0, z0, z;
    2665    33001896 :   if (l <= 3)
    2666             :   {
    2667           0 :     if (rem) *rem = l == 2? 0: a[2];
    2668           0 :     return zero_Flx(a[1]);
    2669             :   }
    2670    33001896 :   z = cgetg(l-1,t_VECSMALL); z[1] = a[1];
    2671    32858971 :   a0 = a + l-1;
    2672    32858971 :   z0 = z + l-2; *z0 = *a0--;
    2673    32858971 :   if (SMALL_ULONG(p))
    2674             :   {
    2675    79648781 :     for (i=l-3; i>1; i--) /* z[i] = (a[i+1] + x*z[i+1]) % p */
    2676             :     {
    2677    59021084 :       ulong t = (*a0-- + x *  *z0--) % p;
    2678    59021084 :       *z0 = (long)t;
    2679             :     }
    2680    20627697 :     if (rem) *rem = (*a0 + x *  *z0) % p;
    2681             :   }
    2682             :   else
    2683             :   {
    2684    48259495 :     for (i=l-3; i>1; i--)
    2685             :     {
    2686    36005446 :       ulong t = Fl_add((ulong)*a0--, Fl_mul(x, *z0--, p), p);
    2687    36028221 :       *z0 = (long)t;
    2688             :     }
    2689    12254049 :     if (rem) *rem = Fl_add((ulong)*a0, Fl_mul(x, *z0, p), p);
    2690             :   }
    2691    32881259 :   return z;
    2692             : }
    2693             : 
    2694             : /* xa, ya = t_VECSMALL */
    2695             : static GEN
    2696      624748 : Flv_producttree(GEN xa, GEN s, ulong p, ulong pi, long vs)
    2697             : {
    2698      624748 :   long n = lg(xa)-1;
    2699      624748 :   long m = n==1 ? 1: expu(n-1)+1;
    2700      624747 :   long i, j, k, ls = lg(s);
    2701      624747 :   GEN T = cgetg(m+1, t_VEC);
    2702      624740 :   GEN t = cgetg(ls, t_VEC);
    2703     7829342 :   for (j=1, k=1; j<ls; k+=s[j++])
    2704     7204420 :     gel(t, j) = s[j] == 1 ?
    2705     7204601 :              mkvecsmall3(vs, Fl_neg(xa[k], p), 1):
    2706     1515079 :              mkvecsmall4(vs, Fl_mul(xa[k], xa[k+1], p),
    2707     1515082 :                  Fl_neg(Fl_add(xa[k],xa[k+1],p),p), 1);
    2708      624741 :   gel(T,1) = t;
    2709     2355315 :   for (i=2; i<=m; i++)
    2710             :   {
    2711     1730669 :     GEN u = gel(T, i-1);
    2712     1730669 :     long n = lg(u)-1;
    2713     1730669 :     GEN t = cgetg(((n+1)>>1)+1, t_VEC);
    2714     8309543 :     for (j=1, k=1; k<n; j++, k+=2)
    2715     6578969 :       gel(t, j) = Flx_mul_pre(gel(u, k), gel(u, k+1), p, pi);
    2716     1730574 :     gel(T, i) = t;
    2717             :   }
    2718      624646 :   return T;
    2719             : }
    2720             : 
    2721             : static GEN
    2722      665053 : Flx_Flv_multieval_tree(GEN P, GEN xa, GEN T, ulong p, ulong pi)
    2723             : {
    2724             :   long i,j,k;
    2725      665053 :   long m = lg(T)-1;
    2726      665053 :   GEN R = cgetg(lg(xa), t_VECSMALL);
    2727      665051 :   GEN Tp = cgetg(m+1, t_VEC), t;
    2728      665041 :   gel(Tp, m) = mkvec(P);
    2729     2581044 :   for (i=m-1; i>=1; i--)
    2730             :   {
    2731     1916004 :     GEN u = gel(T, i), v = gel(Tp, i+1);
    2732     1916004 :     long n = lg(u)-1;
    2733     1916004 :     t = cgetg(n+1, t_VEC);
    2734     9525275 :     for (j=1, k=1; k<n; j++, k+=2)
    2735             :     {
    2736     7609304 :       gel(t, k)   = Flx_rem_pre(gel(v, j), gel(u, k), p, pi);
    2737     7609283 :       gel(t, k+1) = Flx_rem_pre(gel(v, j), gel(u, k+1), p, pi);
    2738             :     }
    2739     1915971 :     gel(Tp, i) = t;
    2740             :   }
    2741             :   {
    2742      665040 :     GEN u = gel(T, i+1), v = gel(Tp, i+1);
    2743      665040 :     long n = lg(u)-1;
    2744     8942389 :     for (j=1, k=1; j<=n; j++)
    2745             :     {
    2746     8277323 :       long c, d = degpol(gel(u,j));
    2747    18319020 :       for (c=1; c<=d; c++, k++) R[k] = Flx_eval_pre(gel(v, j), xa[k], p, pi);
    2748             :     }
    2749      665066 :     return gc_const((pari_sp)R, R);
    2750             :   }
    2751             : }
    2752             : 
    2753             : static GEN
    2754     1386147 : FlvV_polint_tree(GEN T, GEN R, GEN s, GEN xa, GEN ya, ulong p, ulong pi, long vs)
    2755             : {
    2756     1386147 :   pari_sp av = avma;
    2757     1386147 :   long m = lg(T)-1;
    2758     1386147 :   long i, j, k, ls = lg(s);
    2759     1386147 :   GEN Tp = cgetg(m+1, t_VEC);
    2760     1385780 :   GEN t = cgetg(ls, t_VEC);
    2761    24926916 :   for (j=1, k=1; j<ls; k+=s[j++])
    2762    23541243 :     if (s[j]==2)
    2763             :     {
    2764     6935829 :       ulong a = Fl_mul(ya[k], R[k], p);
    2765     6935416 :       ulong b = Fl_mul(ya[k+1], R[k+1], p);
    2766     6942157 :       gel(t, j) = mkvecsmall3(vs, Fl_neg(Fl_add(Fl_mul(xa[k], b, p ),
    2767     6935824 :                   Fl_mul(xa[k+1], a, p), p), p), Fl_add(a, b, p));
    2768     6939765 :       gel(t, j) = Flx_renormalize(gel(t, j), 4);
    2769             :     }
    2770             :     else
    2771    16605414 :       gel(t, j) = Fl_to_Flx(Fl_mul(ya[k], R[k], p), vs);
    2772     1385673 :   gel(Tp, 1) = t;
    2773     6386480 :   for (i=2; i<=m; i++)
    2774             :   {
    2775     5000708 :     GEN u = gel(T, i-1);
    2776     5000708 :     GEN t = cgetg(lg(gel(T,i)), t_VEC);
    2777     4997714 :     GEN v = gel(Tp, i-1);
    2778     4997714 :     long n = lg(v)-1;
    2779    27099101 :     for (j=1, k=1; k<n; j++, k+=2)
    2780    22083896 :       gel(t, j) = Flx_add(Flx_mul_pre(gel(u, k), gel(v, k+1), p, pi),
    2781    22098294 :                           Flx_mul_pre(gel(u, k+1), gel(v, k), p, pi), p);
    2782     5000807 :     gel(Tp, i) = t;
    2783             :   }
    2784     1385772 :   return gerepileuptoleaf(av, gmael(Tp,m,1));
    2785             : }
    2786             : 
    2787             : GEN
    2788           0 : Flx_Flv_multieval(GEN P, GEN xa, ulong p)
    2789             : {
    2790           0 :   pari_sp av = avma;
    2791           0 :   GEN s = producttree_scheme(lg(xa)-1);
    2792           0 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2793           0 :   GEN T = Flv_producttree(xa, s, p, pi, P[1]);
    2794           0 :   return gerepileuptoleaf(av, Flx_Flv_multieval_tree(P, xa, T, p, pi));
    2795             : }
    2796             : 
    2797             : static GEN
    2798        2471 : FlxV_Flv_multieval_tree(GEN x, GEN xa, GEN T, ulong p, ulong pi)
    2799       45248 : { pari_APPLY_same(Flx_Flv_multieval_tree(gel(x,i), xa, T, p, pi)) }
    2800             : 
    2801             : GEN
    2802        2471 : FlxV_Flv_multieval(GEN P, GEN xa, ulong p)
    2803             : {
    2804        2471 :   pari_sp av = avma;
    2805        2471 :   GEN s = producttree_scheme(lg(xa)-1);
    2806        2471 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2807        2471 :   GEN T = Flv_producttree(xa, s, p, pi, P[1]);
    2808        2471 :   return gerepileupto(av, FlxV_Flv_multieval_tree(P, xa, T, p, pi));
    2809             : }
    2810             : 
    2811             : GEN
    2812      368201 : Flv_polint(GEN xa, GEN ya, ulong p, long vs)
    2813             : {
    2814      368201 :   pari_sp av = avma;
    2815      368201 :   GEN s = producttree_scheme(lg(xa)-1);
    2816      368213 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2817      368213 :   GEN T = Flv_producttree(xa, s, p, pi, vs);
    2818      368212 :   long m = lg(T)-1;
    2819      368212 :   GEN P = Flx_deriv(gmael(T, m, 1), p);
    2820      368212 :   GEN R = Flv_inv(Flx_Flv_multieval_tree(P, xa, T, p, pi), p);
    2821      368210 :   return gerepileuptoleaf(av, FlvV_polint_tree(T, R, s, xa, ya, p, pi, vs));
    2822             : }
    2823             : 
    2824             : GEN
    2825      101077 : Flv_Flm_polint(GEN xa, GEN ya, ulong p, long vs)
    2826             : {
    2827      101077 :   pari_sp av = avma;
    2828      101077 :   GEN s = producttree_scheme(lg(xa)-1);
    2829      101078 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2830      101078 :   GEN T = Flv_producttree(xa, s, p, pi, vs);
    2831      101076 :   long i, m = lg(T)-1, l = lg(ya)-1;
    2832      101076 :   GEN P = Flx_deriv(gmael(T, m, 1), p);
    2833      101077 :   GEN R = Flv_inv(Flx_Flv_multieval_tree(P, xa, T, p, pi), p);
    2834      101076 :   GEN M = cgetg(l+1, t_VEC);
    2835     1118857 :   for (i=1; i<=l; i++)
    2836     1017782 :     gel(M,i) = FlvV_polint_tree(T, R, s, xa, gel(ya,i), p, pi, vs);
    2837      101075 :   return gerepileupto(av, M);
    2838             : }
    2839             : 
    2840             : GEN
    2841      152987 : Flv_invVandermonde(GEN L, ulong den, ulong p)
    2842             : {
    2843      152987 :   pari_sp av = avma;
    2844      152987 :   long i, n = lg(L);
    2845             :   GEN M, R;
    2846      152987 :   GEN s = producttree_scheme(n-1);
    2847      152987 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2848      152987 :   GEN tree = Flv_producttree(L, s, p, pi, 0);
    2849      152987 :   long m = lg(tree)-1;
    2850      152987 :   GEN T = gmael(tree, m, 1);
    2851      152987 :   R = Flv_inv(Flx_Flv_multieval_tree(Flx_deriv(T, p), L, tree, p, pi), p);
    2852      152987 :   if (den!=1) R = Flv_Fl_mul(R, den, p);
    2853      152987 :   M = cgetg(n, t_MAT);
    2854      600481 :   for (i = 1; i < n; i++)
    2855             :   {
    2856      447494 :     GEN P = Flx_Fl_mul(Flx_div_by_X_x(T, uel(L,i), p, NULL), uel(R,i), p);
    2857      447494 :     gel(M,i) = Flx_to_Flv(P, n-1);
    2858             :   }
    2859      152987 :   return gerepilecopy(av, M);
    2860             : }
    2861             : 
    2862             : /***********************************************************************/
    2863             : /**                               Flxq                                **/
    2864             : /***********************************************************************/
    2865             : /* Flxq objects are Flx modulo another Flx called q. */
    2866             : 
    2867             : /* Product of y and x in Z/pZ[X]/(T), as t_VECSMALL. */
    2868             : GEN
    2869   193190573 : Flxq_mul_pre(GEN x,GEN y,GEN T,ulong p,ulong pi)
    2870   193190573 : { return Flx_rem_pre(Flx_mul_pre(x,y,p,pi),T,p,pi); }
    2871             : GEN
    2872    13180606 : Flxq_mul(GEN x,GEN y,GEN T,ulong p)
    2873    13180606 : { return Flxq_mul_pre(x,y,T,p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2874             : 
    2875             : GEN
    2876   280087832 : Flxq_sqr_pre(GEN x,GEN T,ulong p,ulong pi)
    2877   280087832 : { return Flx_rem_pre(Flx_sqr_pre(x, p,pi), T, p,pi); }
    2878             : /* Square of y in Z/pZ[X]/(T), as t_VECSMALL. */
    2879             : GEN
    2880     2746662 : Flxq_sqr(GEN x,GEN T,ulong p)
    2881     2746662 : { return Flxq_sqr_pre(x,T,p,SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2882             : 
    2883             : static GEN
    2884     1773388 : _Flxq_red(void *E, GEN x)
    2885     1773388 : { struct _Flxq *s = (struct _Flxq *)E;
    2886     1773388 :   return Flx_rem_pre(x, s->T, s->p, s->pi); }
    2887             : #if 0
    2888             : static GEN
    2889             : _Flx_sub(void *E, GEN x, GEN y)
    2890             : { struct _Flxq *s = (struct _Flxq *)E;
    2891             :   return Flx_sub(x,y,s->p); }
    2892             : #endif
    2893             : static GEN
    2894   272656630 : _Flxq_sqr(void *data, GEN x)
    2895             : {
    2896   272656630 :   struct _Flxq *D = (struct _Flxq*)data;
    2897   272656630 :   return Flxq_sqr_pre(x, D->T, D->p, D->pi);
    2898             : }
    2899             : static GEN
    2900   151718907 : _Flxq_mul(void *data, GEN x, GEN y)
    2901             : {
    2902   151718907 :   struct _Flxq *D = (struct _Flxq*)data;
    2903   151718907 :   return Flxq_mul_pre(x,y, D->T, D->p, D->pi);
    2904             : }
    2905             : static GEN
    2906    22682906 : _Flxq_one(void *data)
    2907             : {
    2908    22682906 :   struct _Flxq *D = (struct _Flxq*)data;
    2909    22682906 :   return pol1_Flx(get_Flx_var(D->T));
    2910             : }
    2911             : 
    2912             : static GEN
    2913    22866310 : _Flxq_powu_i(struct _Flxq *D, GEN x, ulong n)
    2914    22866310 : { return gen_powu_i(x, n, (void*)D, &_Flxq_sqr, &_Flxq_mul); }
    2915             : static GEN
    2916          68 : _Flxq_powu(struct _Flxq *D, GEN x, ulong n)
    2917          68 : { pari_sp av = avma; return gerepileuptoleaf(av, _Flxq_powu_i(D, x, n)); }
    2918             : /* n-Power of x in Z/pZ[X]/(T), as t_VECSMALL. */
    2919             : GEN
    2920    23909012 : Flxq_powu_pre(GEN x, ulong n, GEN T, ulong p, ulong pi)
    2921             : {
    2922             :   pari_sp av;
    2923             :   struct _Flxq D;
    2924    23909012 :   switch(n)
    2925             :   {
    2926           0 :     case 0: return pol1_Flx(get_Flx_var(T));
    2927      143937 :     case 1: return Flx_copy(x);
    2928      899112 :     case 2: return Flxq_sqr_pre(x, T, p, pi);
    2929             :   }
    2930    22865963 :   av = avma; set_Flxq_pre(&D, T, p, pi);
    2931    22866033 :   return gerepileuptoleaf(av, _Flxq_powu_i(&D, x, n));
    2932             : }
    2933             : GEN
    2934      486934 : Flxq_powu(GEN x, ulong n, GEN T, ulong p)
    2935      486934 : { return Flxq_powu_pre(x, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2936             : 
    2937             : /* n-Power of x in Z/pZ[X]/(T), as t_VECSMALL. */
    2938             : GEN
    2939    27485140 : Flxq_pow_pre(GEN x, GEN n, GEN T, ulong p, ulong pi)
    2940             : {
    2941    27485140 :   pari_sp av = avma;
    2942             :   struct _Flxq D;
    2943             :   GEN y;
    2944    27485140 :   long s = signe(n);
    2945    27485140 :   if (!s) return pol1_Flx(get_Flx_var(T));
    2946    27249334 :   if (s < 0) x = Flxq_inv_pre(x,T,p,pi);
    2947    27249334 :   if (is_pm1(n)) return s < 0 ? x : Flx_copy(x);
    2948    26073145 :   set_Flxq_pre(&D, T, p, pi);
    2949    26073186 :   y = gen_pow_i(x, n, (void*)&D, &_Flxq_sqr, &_Flxq_mul);
    2950    26073072 :   return gerepileuptoleaf(av, y);
    2951             : }
    2952             : GEN
    2953      915489 : Flxq_pow(GEN x, GEN n, GEN T, ulong p)
    2954      915489 : { return Flxq_pow_pre(x, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2955             : 
    2956             : GEN
    2957          28 : Flxq_pow_init_pre(GEN x, GEN n, long k, GEN T, ulong p, ulong pi)
    2958             : {
    2959          28 :   struct _Flxq D; set_Flxq_pre(&D, T, p, pi);
    2960          28 :   return gen_pow_init(x, n, k, (void*)&D, &_Flxq_sqr, &_Flxq_mul);
    2961             : }
    2962             : GEN
    2963           0 : Flxq_pow_init(GEN x, GEN n, long k, GEN T, ulong p)
    2964           0 : { return Flxq_pow_init_pre(x, n, k, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2965             : 
    2966             : GEN
    2967        4393 : Flxq_pow_table_pre(GEN R, GEN n, GEN T, ulong p, ulong pi)
    2968             : {
    2969        4393 :   struct _Flxq D; set_Flxq_pre(&D, T, p, pi);
    2970        4393 :   return gen_pow_table(R, n, (void*)&D, &_Flxq_one, &_Flxq_mul);
    2971             : }
    2972             : GEN
    2973           0 : Flxq_pow_table(GEN R, GEN n, GEN T, ulong p)
    2974           0 : { return Flxq_pow_table_pre(R, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2975             : 
    2976             : /* Inverse of x in Z/lZ[X]/(T) or NULL if inverse doesn't exist
    2977             :  * not stack clean. */
    2978             : GEN
    2979     5911661 : Flxq_invsafe_pre(GEN x, GEN T, ulong p, ulong pi)
    2980             : {
    2981     5911661 :   GEN V, z = Flx_extgcd_pre(get_Flx_mod(T), x, p, pi, NULL, &V);
    2982             :   ulong iz;
    2983     5911745 :   if (degpol(z)) return NULL;
    2984     5911070 :   iz = Fl_inv(uel(z,2), p);
    2985     5911063 :   return Flx_Fl_mul_pre(V, iz, p, pi);
    2986             : }
    2987             : GEN
    2988      656243 : Flxq_invsafe(GEN x, GEN T, ulong p)
    2989      656243 : { return Flxq_invsafe_pre(x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    2990             : 
    2991             : GEN
    2992     4809744 : Flxq_inv_pre(GEN x, GEN T, ulong p, ulong pi)
    2993             : {
    2994     4809744 :   pari_sp av=avma;
    2995     4809744 :   GEN U = Flxq_invsafe_pre(x, T, p, pi);
    2996     4809740 :   if (!U) pari_err_INV("Flxq_inv",Flx_to_ZX(x));
    2997     4809712 :   return gerepileuptoleaf(av, U);
    2998             : }
    2999             : GEN
    3000      345517 : Flxq_inv(GEN x, GEN T, ulong p)
    3001      345517 : { return Flxq_inv_pre(x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3002             : 
    3003             : GEN
    3004     2414484 : Flxq_div_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
    3005             : {
    3006     2414484 :   pari_sp av = avma;
    3007     2414484 :   return gerepileuptoleaf(av, Flxq_mul_pre(x,Flxq_inv_pre(y,T,p,pi),T,p,pi));
    3008             : }
    3009             : GEN
    3010      237416 : Flxq_div(GEN x, GEN y, GEN T, ulong p)
    3011      237416 : { return Flxq_div_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3012             : 
    3013             : GEN
    3014    22684058 : Flxq_powers_pre(GEN x, long l, GEN T, ulong p, ulong pi)
    3015             : {
    3016    22684058 :   int use_sqr = 2*degpol(x) >= get_Flx_degree(T);
    3017    22680958 :   struct _Flxq D; set_Flxq_pre(&D, T, p, pi);
    3018    22678987 :   return gen_powers(x, l, use_sqr, (void*)&D, &_Flxq_sqr, &_Flxq_mul, &_Flxq_one);
    3019             : }
    3020             : GEN
    3021      232197 : Flxq_powers(GEN x, long l, GEN T, ulong p)
    3022      232197 : { return Flxq_powers_pre(x, l, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3023             : 
    3024             : GEN
    3025      170496 : Flxq_matrix_pow_pre(GEN y, long n, long m, GEN P, ulong l, ulong li)
    3026      170496 : { return FlxV_to_Flm(Flxq_powers_pre(y,m-1,P,l,li),n); }
    3027             : GEN
    3028         399 : Flxq_matrix_pow(GEN y, long n, long m, GEN P, ulong l)
    3029         399 : { return Flxq_matrix_pow_pre(y, n, m, P, l, SMALL_ULONG(l)? 0: get_Fl_red(l)); }
    3030             : 
    3031             : GEN
    3032    13667517 : Flx_Frobenius_pre(GEN T, ulong p, ulong pi)
    3033    13667517 : { return Flxq_powu_pre(polx_Flx(get_Flx_var(T)), p, T, p, pi); }
    3034             : GEN
    3035       86486 : Flx_Frobenius(GEN T, ulong p)
    3036       86486 : { return Flx_Frobenius_pre(T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3037             : 
    3038             : GEN
    3039       86455 : Flx_matFrobenius_pre(GEN T, ulong p, ulong pi)
    3040             : {
    3041       86455 :   long n = get_Flx_degree(T);
    3042       86455 :   return Flxq_matrix_pow_pre(Flx_Frobenius_pre(T, p, pi), n, n, T, p, pi);
    3043             : }
    3044             : GEN
    3045           0 : Flx_matFrobenius(GEN T, ulong p)
    3046           0 : { return Flx_matFrobenius_pre(T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3047             : 
    3048             : static GEN
    3049    13019192 : Flx_blocks_Flm(GEN P, long n, long m)
    3050             : {
    3051    13019192 :   GEN z = cgetg(m+1,t_MAT);
    3052    13018905 :   long i,j, k=2, l = lg(P);
    3053    38577117 :   for(i=1; i<=m; i++)
    3054             :   {
    3055    25561948 :     GEN zi = cgetg(n+1,t_VECSMALL);
    3056    25558212 :     gel(z,i) = zi;
    3057   113469859 :     for(j=1; j<=n; j++)
    3058    87911647 :       uel(zi, j) = k==l ? 0 : uel(P,k++);
    3059             :   }
    3060    13015169 :   return z;
    3061             : }
    3062             : 
    3063             : GEN
    3064      516072 : Flx_blocks(GEN P, long n, long m)
    3065             : {
    3066      516072 :   GEN z = cgetg(m+1,t_VEC);
    3067      515758 :   long i,j, k=2, l = lg(P);
    3068     1545179 :   for(i=1; i<=m; i++)
    3069             :   {
    3070     1029810 :     GEN zi = cgetg(n+2,t_VECSMALL);
    3071     1028951 :     zi[1] = P[1];
    3072     1028951 :     gel(z,i) = zi;
    3073     6456185 :     for(j=2; j<n+2; j++)
    3074     5427234 :       uel(zi, j) = k==l ? 0 : uel(P,k++);
    3075     1028951 :     zi = Flx_renormalize(zi, n+2);
    3076             :   }
    3077      515369 :   return z;
    3078             : }
    3079             : 
    3080             : static GEN
    3081    13020066 : FlxV_to_Flm_lg(GEN x, long m, long n)
    3082             : {
    3083             :   long i;
    3084    13020066 :   GEN y = cgetg(n+1, t_MAT);
    3085    59191295 :   for (i=1; i<=n; i++) gel(y,i) = Flx_to_Flv(gel(x,i), m);
    3086    13017237 :   return y;
    3087             : }
    3088             : 
    3089             : /* allow pi = 0 (SMALL_ULONG) */
    3090             : GEN
    3091    13218099 : Flx_FlxqV_eval_pre(GEN Q, GEN x, GEN T, ulong p, ulong pi)
    3092             : {
    3093    13218099 :   pari_sp btop, av = avma;
    3094    13218099 :   long sv = get_Flx_var(T), m = get_Flx_degree(T);
    3095    13218334 :   long i, l = lg(x)-1, lQ = lgpol(Q), n,  d;
    3096             :   GEN A, B, C, S, g;
    3097    13219074 :   if (lQ == 0) return pol0_Flx(sv);
    3098    13020940 :   if (lQ <= l)
    3099             :   {
    3100     5803836 :     n = l;
    3101     5803836 :     d = 1;
    3102             :   }
    3103             :   else
    3104             :   {
    3105     7217104 :     n = l-1;
    3106     7217104 :     d = (lQ+n-1)/n;
    3107             :   }
    3108    13020940 :   A = FlxV_to_Flm_lg(x, m, n);
    3109    13019064 :   B = Flx_blocks_Flm(Q, n, d);
    3110    13018213 :   C = gerepileupto(av, Flm_mul(A, B, p));
    3111    13021386 :   g = gel(x, l);
    3112    13021386 :   if (pi && SMALL_ULONG(p)) pi = 0;
    3113    13021386 :   T = Flx_get_red_pre(T, p, pi);
    3114    13021044 :   btop = avma;
    3115    13021044 :   S = Flv_to_Flx(gel(C, d), sv);
    3116    25566803 :   for (i = d-1; i>0; i--)
    3117             :   {
    3118    12546966 :     S = Flx_add(Flxq_mul_pre(S, g, T, p, pi), Flv_to_Flx(gel(C,i), sv), p);
    3119    12546517 :     if (gc_needed(btop,1))
    3120           0 :       S = gerepileuptoleaf(btop, S);
    3121             :   }
    3122    13019837 :   return gerepileuptoleaf(av, S);
    3123             : }
    3124             : GEN
    3125        5103 : Flx_FlxqV_eval(GEN Q, GEN x, GEN T, ulong p)
    3126        5103 : { return Flx_FlxqV_eval_pre(Q, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3127             : 
    3128             : /* allow pi = 0 (SMALL_ULONG) */
    3129             : GEN
    3130     2627291 : Flx_Flxq_eval_pre(GEN Q, GEN x, GEN T, ulong p, ulong pi)
    3131             : {
    3132     2627291 :   pari_sp av = avma;
    3133             :   GEN z, V;
    3134     2627291 :   long d = degpol(Q), rtd;
    3135     2627286 :   if (d < 0) return pol0_Flx(get_Flx_var(T));
    3136     2627195 :   rtd = (long) sqrt((double)d);
    3137     2627195 :   T = Flx_get_red_pre(T, p, pi);
    3138     2627211 :   V = Flxq_powers_pre(x, rtd, T, p, pi);
    3139     2627242 :   z = Flx_FlxqV_eval_pre(Q, V, T, p, pi);
    3140     2627228 :   return gerepileupto(av, z);
    3141             : }
    3142             : GEN
    3143      786387 : Flx_Flxq_eval(GEN Q, GEN x, GEN T, ulong p)
    3144      786387 : { return Flx_Flxq_eval_pre(Q, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3145             : 
    3146             : /* allow pi = 0 (SMALL_ULONG) */
    3147             : GEN
    3148           0 : FlxC_FlxqV_eval_pre(GEN x, GEN v, GEN T, ulong p, ulong pi)
    3149           0 : { pari_APPLY_type(t_COL, Flx_FlxqV_eval_pre(gel(x,i), v, T, p, pi)) }
    3150             : GEN
    3151           0 : FlxC_FlxqV_eval(GEN x, GEN v, GEN T, ulong p)
    3152           0 : { return FlxC_FlxqV_eval_pre(x, v, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3153             : 
    3154             : /* allow pi = 0 (SMALL_ULONG) */
    3155             : GEN
    3156           0 : FlxC_Flxq_eval_pre(GEN x, GEN F, GEN T, ulong p, ulong pi)
    3157             : {
    3158           0 :   long d = brent_kung_optpow(get_Flx_degree(T)-1,lg(x)-1,1);
    3159           0 :   GEN Fp = Flxq_powers_pre(F, d, T, p, pi);
    3160           0 :   return FlxC_FlxqV_eval_pre(x, Fp, T, p, pi);
    3161             : }
    3162             : GEN
    3163           0 : FlxC_Flxq_eval(GEN x, GEN F, GEN T, ulong p)
    3164           0 : { return FlxC_Flxq_eval_pre(x, F, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3165             : 
    3166             : #if 0
    3167             : static struct bb_algebra Flxq_algebra = { _Flxq_red, _Flx_add, _Flx_sub,
    3168             :               _Flxq_mul, _Flxq_sqr, _Flxq_one, _Flxq_zero};
    3169             : #endif
    3170             : 
    3171             : static GEN
    3172      386204 : Flxq_autpow_sqr(void *E, GEN x)
    3173             : {
    3174      386204 :   struct _Flxq *D = (struct _Flxq*)E;
    3175      386204 :   return Flx_Flxq_eval_pre(x, x, D->T, D->p, D->pi);
    3176             : }
    3177             : static GEN
    3178       20730 : Flxq_autpow_msqr(void *E, GEN x)
    3179             : {
    3180       20730 :   struct _Flxq *D = (struct _Flxq*)E;
    3181       20730 :   return Flx_FlxqV_eval_pre(Flxq_autpow_sqr(E, x), D->aut, D->T, D->p, D->pi);
    3182             : }
    3183             : 
    3184             : GEN
    3185      304372 : Flxq_autpow_pre(GEN x, ulong n, GEN T, ulong p, ulong pi)
    3186             : {
    3187      304372 :   pari_sp av = avma;
    3188             :   struct _Flxq D;
    3189             :   long d;
    3190      304372 :   if (n==0) return Flx_rem_pre(polx_Flx(x[1]), T, p, pi);
    3191      304365 :   if (n==1) return Flx_rem_pre(x, T, p, pi);
    3192      303875 :   set_Flxq_pre(&D, T, p, pi);
    3193      303875 :   d = brent_kung_optpow(get_Flx_degree(T), hammingl(n)-1, 1);
    3194      303875 :   D.aut = Flxq_powers_pre(x, d, T, p, D.pi);
    3195      303875 :   x = gen_powu_fold_i(x,n,(void*)&D,Flxq_autpow_sqr,Flxq_autpow_msqr);
    3196      303875 :   return gerepilecopy(av, x);
    3197             : }
    3198             : GEN
    3199           7 : Flxq_autpow(GEN x, ulong n, GEN T, ulong p)
    3200           7 : { return Flxq_autpow_pre(x, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3201             : 
    3202             : GEN
    3203        1667 : Flxq_autpowers(GEN x, ulong l, GEN T, ulong p)
    3204             : {
    3205        1667 :   long d, vT = get_Flx_var(T), dT = get_Flx_degree(T);
    3206             :   ulong i, pi;
    3207        1667 :   pari_sp av = avma;
    3208        1667 :   GEN xp, V = cgetg(l+2,t_VEC);
    3209        1667 :   gel(V,1) = polx_Flx(vT); if (l==0) return V;
    3210        1667 :   gel(V,2) = gcopy(x); if (l==1) return V;
    3211        1667 :   pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    3212        1667 :   T = Flx_get_red_pre(T, p, pi);
    3213        1667 :   d = brent_kung_optpow(dT-1, l-1, 1);
    3214        1667 :   xp = Flxq_powers_pre(x, d, T, p, pi);
    3215        6998 :   for(i = 3; i < l+2; i++)
    3216        5331 :     gel(V,i) = Flx_FlxqV_eval_pre(gel(V,i-1), xp, T, p, pi);
    3217        1667 :   return gerepilecopy(av, V);
    3218             : }
    3219             : 
    3220             : static GEN
    3221      588806 : Flxq_autsum_mul(void *E, GEN x, GEN y)
    3222             : {
    3223      588806 :   struct _Flxq *D = (struct _Flxq*)E;
    3224      588806 :   GEN T = D->T;
    3225      588806 :   ulong p = D->p, pi = D->pi;
    3226      588806 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    3227      588806 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    3228      588806 :   ulong d = brent_kung_optpow(maxss(degpol(phi1),degpol(a1)),2,1);
    3229      588806 :   GEN V2 = Flxq_powers_pre(phi2, d, T, p, pi);
    3230      588806 :   GEN phi3 = Flx_FlxqV_eval_pre(phi1, V2, T, p, pi);
    3231      588806 :   GEN aphi = Flx_FlxqV_eval_pre(a1, V2, T, p, pi);
    3232      588806 :   GEN a3 = Flxq_mul_pre(aphi, a2, T, p, pi);
    3233      588806 :   return mkvec2(phi3, a3);
    3234             : }
    3235             : static GEN
    3236      379955 : Flxq_autsum_sqr(void *E, GEN x)
    3237      379955 : { return Flxq_autsum_mul(E, x, x); }
    3238             : 
    3239             : static GEN
    3240      315826 : Flxq_autsum_pre(GEN x, ulong n, GEN T, ulong p, ulong pi)
    3241             : {
    3242      315826 :   pari_sp av = avma;
    3243      315826 :   struct _Flxq D; set_Flxq_pre(&D, T, p, pi);
    3244      315826 :   x = gen_powu_i(x,n,(void*)&D,Flxq_autsum_sqr,Flxq_autsum_mul);
    3245      315826 :   return gerepilecopy(av, x);
    3246             : }
    3247             : GEN
    3248           0 : Flxq_autsum(GEN x, ulong n, GEN T, ulong p)
    3249           0 : { return Flxq_autsum_pre(x, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3250             : 
    3251             : static GEN
    3252      762406 : Flxq_auttrace_mul(void *E, GEN x, GEN y)
    3253             : {
    3254      762406 :   struct _Flxq *D = (struct _Flxq*)E;
    3255      762406 :   GEN T = D->T;
    3256      762406 :   ulong p = D->p, pi = D->pi;
    3257      762406 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    3258      762406 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    3259      762406 :   ulong d = brent_kung_optpow(maxss(degpol(phi1),degpol(a1)),2,1);
    3260      762416 :   GEN V1 = Flxq_powers_pre(phi1, d, T, p, pi);
    3261      762381 :   GEN phi3 = Flx_FlxqV_eval_pre(phi2, V1, T, p, pi);
    3262      762388 :   GEN aphi = Flx_FlxqV_eval_pre(a2, V1, T, p, pi);
    3263      762395 :   GEN a3 = Flx_add(a1, aphi, p);
    3264      762400 :   return mkvec2(phi3, a3);
    3265             : }
    3266             : 
    3267             : static GEN
    3268      635478 : Flxq_auttrace_sqr(void *E, GEN x)
    3269      635478 : { return Flxq_auttrace_mul(E, x, x); }
    3270             : 
    3271             : GEN
    3272      934749 : Flxq_auttrace_pre(GEN x, ulong n, GEN T, ulong p, ulong pi)
    3273             : {
    3274      934749 :   pari_sp av = avma;
    3275             :   struct _Flxq D;
    3276      934749 :   set_Flxq_pre(&D, T, p, pi);
    3277      934748 :   x = gen_powu_i(x,n,(void*)&D,Flxq_auttrace_sqr,Flxq_auttrace_mul);
    3278      934725 :   return gerepilecopy(av, x);
    3279             : }
    3280             : GEN
    3281           0 : Flxq_auttrace(GEN x, ulong n, GEN T, ulong p)
    3282           0 : { return Flxq_auttrace_pre(x, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3283             : 
    3284             : static long
    3285      851196 : bounded_order(ulong p, GEN b, long k)
    3286             : {
    3287      851196 :   GEN a = modii(utoipos(p), b);
    3288             :   long i;
    3289     2080430 :   for(i = 1; i < k; i++)
    3290             :   {
    3291     1613617 :     if (equali1(a)) return i;
    3292     1229235 :     a = modii(muliu(a,p),b);
    3293             :   }
    3294      466813 :   return 0;
    3295             : }
    3296             : 
    3297             : /* n = (p^d-a)\b
    3298             :  * b = bb*p^vb
    3299             :  * p^k = 1 [bb]
    3300             :  * d = m*k+r+vb
    3301             :  * u = (p^k-1)/bb;
    3302             :  * v = (p^(r+vb)-a)/b;
    3303             :  * w = (p^(m*k)-1)/(p^k-1)
    3304             :  * n = p^r*w*u+v
    3305             :  * w*u = p^vb*(p^(m*k)-1)/b
    3306             :  * n = p^(r+vb)*(p^(m*k)-1)/b+(p^(r+vb)-a)/b */
    3307             : static GEN
    3308    26735105 : Flxq_pow_Frobenius(GEN x, GEN n, GEN aut, GEN T, ulong p, ulong pi)
    3309             : {
    3310    26735105 :   pari_sp av=avma;
    3311    26735105 :   long d = get_Flx_degree(T);
    3312    26735099 :   GEN an = absi_shallow(n), z, q;
    3313    26735112 :   if (abscmpiu(an,p)<0 || cmpis(an,d)<=0) return Flxq_pow_pre(x, n, T, p, pi);
    3314      851563 :   q = powuu(p, d);
    3315      851562 :   if (dvdii(q, n))
    3316             :   {
    3317         332 :     long vn = logint(an, utoipos(p));
    3318         332 :     GEN autvn = vn==1 ? aut: Flxq_autpow_pre(aut,vn,T,p,pi);
    3319         332 :     z = Flx_Flxq_eval_pre(x,autvn,T,p,pi);
    3320             :   } else
    3321             :   {
    3322      851228 :     GEN b = diviiround(q, an), a = subii(q, mulii(an,b));
    3323             :     GEN bb, u, v, autk;
    3324      851229 :     long vb = Z_lvalrem(b,p,&bb);
    3325      851230 :     long m, r, k = is_pm1(bb)? 1: bounded_order(p,bb,d);
    3326      851229 :     if (!k || d-vb < k) return Flxq_pow_pre(x,n, T,p,pi);
    3327      384409 :     m = (d-vb)/k; r = (d-vb)%k;
    3328      384409 :     u = diviiexact(subiu(powuu(p,k),1),bb);
    3329      384409 :     v = diviiexact(subii(powuu(p,r+vb),a),b);
    3330      384409 :     autk = k==1 ? aut: Flxq_autpow_pre(aut,k,T,p,pi);
    3331      384409 :     if (r)
    3332             :     {
    3333       94552 :       GEN autr = r==1 ? aut: Flxq_autpow_pre(aut,r,T,p,pi);
    3334       94552 :       z = Flx_Flxq_eval_pre(x,autr,T,p,pi);
    3335      289857 :     } else z = x;
    3336      384409 :     if (m > 1) z = gel(Flxq_autsum_pre(mkvec2(autk, z), m, T, p, pi), 2);
    3337      384409 :     if (!is_pm1(u)) z = Flxq_pow_pre(z, u, T, p, pi);
    3338      384409 :     if (signe(v)) z = Flxq_mul_pre(z, Flxq_pow_pre(x, v, T, p, pi), T, p, pi);
    3339             :   }
    3340      384741 :   return gerepileupto(av,signe(n)>0 ? z : Flxq_inv_pre(z,T,p,pi));
    3341             : }
    3342             : 
    3343             : static GEN
    3344    26727725 : _Flxq_pow(void *data, GEN x, GEN n)
    3345             : {
    3346    26727725 :   struct _Flxq *D = (struct _Flxq*)data;
    3347    26727725 :   return Flxq_pow_Frobenius(x, n, D->aut, D->T, D->p, D->pi);
    3348             : }
    3349             : 
    3350             : static GEN
    3351      330223 : _Flxq_rand(void *data)
    3352             : {
    3353      330223 :   pari_sp av=avma;
    3354      330223 :   struct _Flxq *D = (struct _Flxq*)data;
    3355             :   GEN z;
    3356             :   do
    3357             :   {
    3358      331622 :     set_avma(av);
    3359      331621 :     z = random_Flx(get_Flx_degree(D->T),get_Flx_var(D->T),D->p);
    3360      331623 :   } while (lgpol(z)==0);
    3361      330224 :   return z;
    3362             : }
    3363             : 
    3364             : /* discrete log in FpXQ for a in Fp^*, g in FpXQ^* of order ord */
    3365             : static GEN
    3366       35419 : Fl_Flxq_log(ulong a, GEN g, GEN o, GEN T, ulong p)
    3367             : {
    3368       35419 :   pari_sp av = avma;
    3369             :   GEN q,n_q,ord,ordp, op;
    3370             : 
    3371       35419 :   if (a == 1UL) return gen_0;
    3372             :   /* p > 2 */
    3373             : 
    3374       35419 :   ordp = utoi(p - 1);
    3375       35419 :   ord  = get_arith_Z(o);
    3376       35419 :   if (!ord) ord = T? subiu(powuu(p, get_FpX_degree(T)), 1): ordp;
    3377       35419 :   if (a == p - 1) /* -1 */
    3378        7732 :     return gerepileuptoint(av, shifti(ord,-1));
    3379       27687 :   ordp = gcdii(ordp, ord);
    3380       27687 :   op = typ(o)==t_MAT ? famat_Z_gcd(o, ordp) : ordp;
    3381             : 
    3382       27687 :   q = NULL;
    3383       27687 :   if (T)
    3384             :   { /* we want < g > = Fp^* */
    3385       27687 :     if (!equalii(ord,ordp)) {
    3386       11906 :       q = diviiexact(ord,ordp);
    3387       11906 :       g = Flxq_pow(g,q,T,p);
    3388             :     }
    3389             :   }
    3390       27687 :   n_q = Fp_log(utoi(a), utoipos(uel(g,2)), op, utoipos(p));
    3391       27687 :   if (lg(n_q)==1) return gerepileuptoleaf(av, n_q);
    3392       27687 :   if (q) n_q = mulii(q, n_q);
    3393       27687 :   return gerepileuptoint(av, n_q);
    3394             : }
    3395             : 
    3396             : static GEN
    3397      691299 : Flxq_easylog(void* E, GEN a, GEN g, GEN ord)
    3398             : {
    3399      691299 :   struct _Flxq *f = (struct _Flxq *)E;
    3400      691299 :   GEN T = f->T;
    3401      691299 :   ulong p = f->p;
    3402      691299 :   long d = get_Flx_degree(T);
    3403      691299 :   if (Flx_equal1(a)) return gen_0;
    3404      531495 :   if (Flx_equal(a,g)) return gen_1;
    3405      174269 :   if (!degpol(a))
    3406       35419 :     return Fl_Flxq_log(uel(a,2), g, ord, T, p);
    3407      138850 :   if (typ(ord)!=t_INT || d <= 4 || d == 6 || abscmpiu(ord,1UL<<27)<0)
    3408      138822 :     return NULL;
    3409          28 :   return Flxq_log_index(a, g, ord, T, p);
    3410             : }
    3411             : 
    3412             : static const struct bb_group Flxq_star={_Flxq_mul,_Flxq_pow,_Flxq_rand,hash_GEN,Flx_equal,Flx_equal1,Flxq_easylog};
    3413             : 
    3414             : const struct bb_group *
    3415      431803 : get_Flxq_star(void **E, GEN T, ulong p)
    3416             : {
    3417      431803 :   struct _Flxq *e = (struct _Flxq *) stack_malloc(sizeof(struct _Flxq));
    3418      431803 :   e->T = T; e->p  = p; e->pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    3419      431803 :   e->aut =  Flx_Frobenius_pre(T, p, e->pi);
    3420      431804 :   *E = (void*)e; return &Flxq_star;
    3421             : }
    3422             : 
    3423             : GEN
    3424       96584 : Flxq_order(GEN a, GEN ord, GEN T, ulong p)
    3425             : {
    3426             :   void *E;
    3427       96584 :   const struct bb_group *S = get_Flxq_star(&E,T,p);
    3428       96584 :   return gen_order(a,ord,E,S);
    3429             : }
    3430             : 
    3431             : GEN
    3432      164469 : Flxq_log(GEN a, GEN g, GEN ord, GEN T, ulong p)
    3433             : {
    3434             :   void *E;
    3435      164469 :   pari_sp av = avma;
    3436      164469 :   const struct bb_group *S = get_Flxq_star(&E,T,p);
    3437      164469 :   GEN v = get_arith_ZZM(ord), F = gmael(v,2,1);
    3438      164469 :   if (lg(F) > 1 && Flxq_log_use_index(veclast(F), T, p))
    3439       24381 :     v = mkvec2(gel(v, 1), ZM_famat_limit(gel(v, 2), int2n(27)));
    3440      164469 :   return gerepileuptoleaf(av, gen_PH_log(a, g, v, E, S));
    3441             : }
    3442             : 
    3443             : GEN
    3444      177035 : Flxq_sqrtn(GEN a, GEN n, GEN T, ulong p, GEN *zeta)
    3445             : {
    3446      177035 :   if (!lgpol(a))
    3447             :   {
    3448        6286 :     if (signe(n) < 0) pari_err_INV("Flxq_sqrtn",a);
    3449        6279 :     if (zeta)
    3450           0 :       *zeta=pol1_Flx(get_Flx_var(T));
    3451        6279 :     return pol0_Flx(get_Flx_var(T));
    3452             :   }
    3453             :   else
    3454             :   {
    3455             :     void *E;
    3456      170750 :     pari_sp av = avma;
    3457      170750 :     const struct bb_group *S = get_Flxq_star(&E,T,p);
    3458      170751 :     GEN o = subiu(powuu(p,get_Flx_degree(T)), 1);
    3459      170751 :     GEN s = gen_Shanks_sqrtn(a,n,o,zeta,E,S);
    3460      170752 :     if (!s) return gc_NULL(av);
    3461      170654 :     return gc_all(av, zeta?2:1, &s, zeta);
    3462             :   }
    3463             : }
    3464             : 
    3465             : GEN
    3466      224774 : Flxq_sqrt_pre(GEN z, GEN T, ulong p, ulong pi)
    3467             : {
    3468      224774 :   pari_sp av = avma;
    3469      224774 :   if (p==2)
    3470             :   {
    3471           0 :     GEN r = F2xq_sqrt(Flx_to_F2x(z), Flx_to_F2x(get_Flx_mod(T)));
    3472           0 :     return gerepileupto(av, F2x_to_Flx(r));
    3473             :   }
    3474      224774 :   if (get_Flx_degree(T)==2)
    3475             :   {
    3476       67842 :     GEN P = get_Flx_mod(T), s;
    3477       67842 :     ulong c = uel(P,2), b = uel(P,3), a = uel(P,4);
    3478       67842 :     ulong y = degpol(z)<1 ? 0: uel(z,3);
    3479       67842 :     if (a==1 && b==0)
    3480       14890 :     {
    3481       15670 :       ulong x = degpol(z)<1 ? Flx_constant(z): uel(z,2);
    3482       15670 :       GEN r = Fl2_sqrt_pre(mkvecsmall2(x, y), Fl_neg(c, p), p, pi);
    3483       15670 :       if (!r) return gc_NULL(av);
    3484       14890 :       s = mkvecsmall3(P[1], uel(r,1), uel(r,2));
    3485             :     }
    3486             :     else
    3487             :     {
    3488       52172 :       ulong b2 = Fl_halve(b, p), t = Fl_div(b2, a, p);
    3489       52172 :       ulong D = Fl_sub(Fl_sqr(b2, p), Fl_mul(a, c, p), p);
    3490       52172 :       ulong x = degpol(z)<1 ? Flx_constant(z): Fl_sub(uel(z,2), Fl_mul(uel(z,3), t, p), p);
    3491       52172 :       GEN r = Fl2_sqrt_pre(mkvecsmall2(x, y), D, p, pi);
    3492       52172 :       if (!r) return gc_NULL(av);
    3493       49778 :       s = mkvecsmall3(P[1], Fl_add(uel(r,1), Fl_mul(uel(r,2),t,p), p), uel(r,2));
    3494             :     }
    3495       64668 :     return gerepileuptoleaf(av, Flx_renormalize(s, 4));
    3496             :   }
    3497             :   else
    3498      156932 :     return Flxq_sqrtn(z, gen_2, T, p, NULL);
    3499             : }
    3500             : 
    3501             : GEN
    3502      224774 : Flxq_sqrt(GEN a, GEN T, ulong p)
    3503      224774 : { return Flxq_sqrt_pre(a, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3504             : 
    3505             : /* assume T irreducible mod p */
    3506             : int
    3507      399304 : Flxq_issquare(GEN x, GEN T, ulong p)
    3508             : {
    3509      399304 :   if (lgpol(x) == 0 || p == 2) return 1;
    3510      392773 :   return krouu(Flxq_norm(x,T,p), p) == 1;
    3511             : }
    3512             : 
    3513             : /* assume T irreducible mod p */
    3514             : int
    3515           0 : Flxq_is2npower(GEN x, long n, GEN T, ulong p)
    3516             : {
    3517             :   pari_sp av;
    3518             :   GEN m;
    3519           0 :   if (n==1) return Flxq_issquare(x, T, p);
    3520           0 :   if (lgpol(x) == 0 || p == 2) return 1;
    3521           0 :   av = avma;
    3522           0 :   m = shifti(subiu(powuu(p, get_Flx_degree(T)), 1), -n);
    3523           0 :   return gc_bool(av, Flx_equal1(Flxq_pow(x, m, T, p)));
    3524             : }
    3525             : 
    3526             : GEN
    3527      113589 : Flxq_lroot_fast_pre(GEN a, GEN sqx, GEN T, long p, ulong pi)
    3528             : {
    3529      113589 :   pari_sp av=avma;
    3530      113589 :   GEN A = Flx_splitting(a,p);
    3531      113589 :   return gerepileuptoleaf(av, FlxqV_dotproduct_pre(A,sqx,T,p,pi));
    3532             : }
    3533             : GEN
    3534           0 : Flxq_lroot_fast(GEN a, GEN sqx, GEN T, long p)
    3535           0 : { return Flxq_lroot_fast_pre(a, sqx, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3536             : 
    3537             : GEN
    3538       25053 : Flxq_lroot_pre(GEN a, GEN T, long p, ulong pi)
    3539             : {
    3540       25053 :   pari_sp av=avma;
    3541       25053 :   long n = get_Flx_degree(T), d = degpol(a);
    3542             :   GEN sqx, V;
    3543       25053 :   if (n==1) return leafcopy(a);
    3544       25053 :   if (n==2) return Flxq_powu_pre(a, p, T, p, pi);
    3545       25053 :   sqx = Flxq_autpow_pre(Flx_Frobenius_pre(T, p, pi), n-1, T, p, pi);
    3546       25053 :   if (d==1 && a[2]==0 && a[3]==1) return gerepileuptoleaf(av, sqx);
    3547           0 :   if (d>=p)
    3548             :   {
    3549           0 :     V = Flxq_powers_pre(sqx,p-1,T,p,pi);
    3550           0 :     return gerepileuptoleaf(av, Flxq_lroot_fast_pre(a,V,T,p,pi));
    3551             :   } else
    3552           0 :     return gerepileuptoleaf(av, Flx_Flxq_eval_pre(a,sqx,T,p,pi));
    3553             : }
    3554             : GEN
    3555           0 : Flxq_lroot(GEN a, GEN T, long p)
    3556           0 : { return Flxq_lroot_pre(a, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3557             : 
    3558             : ulong
    3559      438054 : Flxq_norm(GEN x, GEN TB, ulong p)
    3560             : {
    3561      438054 :   GEN T = get_Flx_mod(TB);
    3562      438054 :   ulong y = Flx_resultant(T, x, p), L = Flx_lead(T);
    3563      438054 :   if (L==1 || lgpol(x)==0) return y;
    3564           0 :   return Fl_div(y, Fl_powu(L, (ulong)degpol(x), p), p);
    3565             : }
    3566             : 
    3567             : ulong
    3568        4402 : Flxq_trace(GEN x, GEN TB, ulong p)
    3569             : {
    3570        4402 :   pari_sp av = avma;
    3571             :   ulong t;
    3572        4402 :   GEN T = get_Flx_mod(TB);
    3573        4402 :   long n = degpol(T)-1;
    3574        4402 :   GEN z = Flxq_mul(x, Flx_deriv(T, p), TB, p);
    3575        4402 :   t = degpol(z)<n ? 0 : Fl_div(z[2+n],T[3+n],p);
    3576        4402 :   return gc_ulong(av, t);
    3577             : }
    3578             : 
    3579             : /*x must be reduced*/
    3580             : GEN
    3581        3632 : Flxq_charpoly(GEN x, GEN TB, ulong p)
    3582             : {
    3583        3632 :   pari_sp ltop=avma;
    3584        3632 :   GEN T = get_Flx_mod(TB);
    3585        3632 :   long vs = evalvarn(fetch_var());
    3586        3632 :   GEN xm1 = deg1pol_shallow(pol1_Flx(x[1]),Flx_neg(x,p),vs);
    3587        3632 :   GEN r = Flx_FlxY_resultant(T, xm1, p);
    3588        3632 :   r[1] = x[1];
    3589        3632 :   (void)delete_var(); return gerepileupto(ltop, r);
    3590             : }
    3591             : 
    3592             : /* Computing minimal polynomial :                         */
    3593             : /* cf Shoup 'Efficient Computation of Minimal Polynomials */
    3594             : /*          in Algebraic Extensions of Finite Fields'     */
    3595             : 
    3596             : /* Let v a linear form, return the linear form z->v(tau*z)
    3597             :    that is, v*(M_tau) */
    3598             : 
    3599             : static GEN
    3600     1690312 : Flxq_transmul_init(GEN tau, GEN T, ulong p, ulong pi)
    3601             : {
    3602             :   GEN bht;
    3603     1690312 :   GEN h, Tp = get_Flx_red(T, &h);
    3604     1690310 :   long n = degpol(Tp), vT = Tp[1];
    3605     1690298 :   GEN ft = Flx_recipspec(Tp+2, n+1, n+1);
    3606     1690291 :   GEN bt = Flx_recipspec(tau+2, lgpol(tau), n);
    3607     1690283 :   ft[1] = vT; bt[1] = vT;
    3608     1690283 :   if (h)
    3609        2688 :     bht = Flxn_mul_pre(bt, h, n-1, p, pi);
    3610             :   else
    3611             :   {
    3612     1687595 :     GEN bh = Flx_div_pre(Flx_shift(tau, n-1), T, p, pi);
    3613     1687608 :     bht = Flx_recipspec(bh+2, lgpol(bh), n-1);
    3614     1687602 :     bht[1] = vT;
    3615             :   }
    3616     1690290 :   return mkvec3(bt, bht, ft);
    3617             : }
    3618             : 
    3619             : static GEN
    3620     4080638 : Flxq_transmul(GEN tau, GEN a, long n, ulong p, ulong pi)
    3621             : {
    3622     4080638 :   pari_sp ltop = avma;
    3623             :   GEN t1, t2, t3, vec;
    3624     4080638 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    3625     4080638 :   if (lgpol(a)==0) return pol0_Flx(a[1]);
    3626     4050103 :   t2  = Flx_shift(Flx_mul_pre(bt, a, p, pi),1-n);
    3627     4049744 :   if (lgpol(bht)==0) return gerepileuptoleaf(ltop, t2);
    3628     3060092 :   t1  = Flx_shift(Flx_mul_pre(ft, a, p, pi),-n);
    3629     3060093 :   t3  = Flxn_mul_pre(t1, bht, n-1, p, pi);
    3630     3060146 :   vec = Flx_sub(t2, Flx_shift(t3, 1), p);
    3631     3060224 :   return gerepileuptoleaf(ltop, vec);
    3632             : }
    3633             : 
    3634             : GEN
    3635      783821 : Flxq_minpoly_pre(GEN x, GEN T, ulong p, ulong pi)
    3636             : {
    3637      783821 :   pari_sp ltop = avma;
    3638      783821 :   long vT = get_Flx_var(T), n = get_Flx_degree(T);
    3639             :   GEN v_x;
    3640      783816 :   GEN g = pol1_Flx(vT), tau = pol1_Flx(vT);
    3641      783794 :   T = Flx_get_red_pre(T, p, pi);
    3642      783792 :   v_x = Flxq_powers_pre(x, usqrt(2*n), T, p, pi);
    3643     1628944 :   while (lgpol(tau) != 0)
    3644             :   {
    3645             :     long i, j, m, k1;
    3646             :     GEN M, v, tr, g_prime, c;
    3647      845148 :     if (degpol(g) == n) { tau = pol1_Flx(vT); g = pol1_Flx(vT); }
    3648      845149 :     v = random_Flx(n, vT, p);
    3649      845162 :     tr = Flxq_transmul_init(tau, T, p, pi);
    3650      845145 :     v = Flxq_transmul(tr, v, n, p, pi);
    3651      845162 :     m = 2*(n-degpol(g));
    3652      845161 :     k1 = usqrt(m);
    3653      845163 :     tr = Flxq_transmul_init(gel(v_x,k1+1), T, p, pi);
    3654      845157 :     c = cgetg(m+2,t_VECSMALL);
    3655      845101 :     c[1] = vT;
    3656     4080482 :     for (i=0; i<m; i+=k1)
    3657             :     {
    3658     3235321 :       long mj = minss(m-i, k1);
    3659    12642500 :       for (j=0; j<mj; j++)
    3660     9406894 :         uel(c,m+1-(i+j)) = Flx_dotproduct_pre(v, gel(v_x,j+1), p, pi);
    3661     3235606 :       v = Flxq_transmul(tr, v, n, p, pi);
    3662             :     }
    3663      845161 :     c = Flx_renormalize(c, m+2);
    3664             :     /* now c contains <v,x^i> , i = 0..m-1  */
    3665      845163 :     M = Flx_halfgcd_pre(monomial_Flx(1, m, vT), c, p, pi);
    3666      845176 :     g_prime = gmael(M, 2, 2);
    3667      845176 :     if (degpol(g_prime) < 1) continue;
    3668      833478 :     g = Flx_mul_pre(g, g_prime, p, pi);
    3669      833454 :     tau = Flxq_mul_pre(tau, Flx_FlxqV_eval_pre(g_prime, v_x, T,p,pi), T,p,pi);
    3670             :   }
    3671      783770 :   g = Flx_normalize(g,p);
    3672      783805 :   return gerepileuptoleaf(ltop,g);
    3673             : }
    3674             : GEN
    3675       44415 : Flxq_minpoly(GEN x, GEN T, ulong p)
    3676       44415 : { return Flxq_minpoly_pre(x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3677             : 
    3678             : GEN
    3679          20 : Flxq_conjvec(GEN x, GEN T, ulong p)
    3680             : {
    3681          20 :   long i, l = 1+get_Flx_degree(T);
    3682          20 :   GEN z = cgetg(l,t_COL);
    3683          20 :   struct _Flxq D; set_Flxq(&D, T, p);
    3684          20 :   gel(z,1) = Flx_copy(x);
    3685          88 :   for (i=2; i<l; i++) gel(z,i) = _Flxq_powu(&D, gel(z,i-1), p);
    3686          20 :   return z;
    3687             : }
    3688             : 
    3689             : GEN
    3690        7187 : gener_Flxq(GEN T, ulong p, GEN *po)
    3691             : {
    3692        7187 :   long i, j, vT = get_Flx_var(T), f = get_Flx_degree(T);
    3693             :   ulong p_1, pi;
    3694             :   GEN g, L, L2, o, q, F;
    3695             :   pari_sp av0, av;
    3696             : 
    3697        7187 :   if (f == 1) {
    3698             :     GEN fa;
    3699          28 :     o = utoipos(p-1);
    3700          28 :     fa = Z_factor(o);
    3701          28 :     L = gel(fa,1);
    3702          28 :     L = vecslice(L, 2, lg(L)-1); /* remove 2 for efficiency */
    3703          28 :     g = Fl_to_Flx(pgener_Fl_local(p, vec_to_vecsmall(L)), vT);
    3704          28 :     if (po) *po = mkvec2(o, fa);
    3705          28 :     return g;
    3706             :   }
    3707             : 
    3708        7159 :   av0 = avma; p_1 = p - 1;
    3709        7159 :   q = diviuexact(subiu(powuu(p,f), 1), p_1);
    3710             : 
    3711        7159 :   L = cgetg(1, t_VECSMALL);
    3712        7159 :   if (p > 3)
    3713             :   {
    3714        2357 :     ulong t = p_1 >> vals(p_1);
    3715        2357 :     GEN P = gel(factoru(t), 1);
    3716        2357 :     L = cgetg_copy(P, &i);
    3717        3773 :     while (--i) L[i] = p_1 / P[i];
    3718             :   }
    3719        7159 :   o = factor_pn_1(utoipos(p),f);
    3720        7159 :   L2 = leafcopy( gel(o, 1) );
    3721       19170 :   for (i = j = 1; i < lg(L2); i++)
    3722             :   {
    3723       12011 :     if (umodui(p_1, gel(L2,i)) == 0) continue;
    3724        6474 :     gel(L2,j++) = diviiexact(q, gel(L2,i));
    3725             :   }
    3726        7159 :   setlg(L2, j); pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    3727        7159 :   F = Flx_Frobenius_pre(T, p, pi);
    3728       17675 :   for (av = avma;; set_avma(av))
    3729       10516 :   {
    3730             :     GEN tt;
    3731       17675 :     g = random_Flx(f, vT, p);
    3732       17675 :     if (degpol(g) < 1) continue;
    3733       12041 :     if (p == 2) tt = g;
    3734             :     else
    3735             :     {
    3736        8842 :       ulong t = Flxq_norm(g, T, p);
    3737        8842 :       if (t == 1 || !is_gener_Fl(t, p, p_1, L)) continue;
    3738        4752 :       tt = Flxq_powu_pre(g, p_1>>1, T, p, pi);
    3739             :     }
    3740       14530 :     for (i = 1; i < j; i++)
    3741             :     {
    3742        7371 :       GEN a = Flxq_pow_Frobenius(tt, gel(L2,i), F, T, p, pi);
    3743        7371 :       if (!degpol(a) && uel(a,2) == p_1) break;
    3744             :     }
    3745        7951 :     if (i == j) break;
    3746             :   }
    3747        7159 :   if (!po)
    3748             :   {
    3749         187 :     set_avma((pari_sp)g);
    3750         187 :     g = gerepileuptoleaf(av0, g);
    3751             :   }
    3752             :   else {
    3753        6972 :     *po = mkvec2(subiu(powuu(p,f), 1), o);
    3754        6972 :     gerepileall(av0, 2, &g, po);
    3755             :   }
    3756        7159 :   return g;
    3757             : }
    3758             : 
    3759             : static GEN
    3760      553759 : _Flxq_neg(void *E, GEN x)
    3761      553759 : { struct _Flxq *s = (struct _Flxq *)E;
    3762      553759 :   return Flx_neg(x,s->p); }
    3763             : 
    3764             : static GEN
    3765     1577269 : _Flxq_rmul(void *E, GEN x, GEN y)
    3766     1577269 : { struct _Flxq *s = (struct _Flxq *)E;
    3767     1577269 :   return Flx_mul_pre(x,y,s->p,s->pi); }
    3768             : 
    3769             : static GEN
    3770       18980 : _Flxq_inv(void *E, GEN x)
    3771       18980 : { struct _Flxq *s = (struct _Flxq *)E;
    3772       18980 :   return Flxq_inv(x,s->T,s->p); }
    3773             : 
    3774             : static int
    3775      165726 : _Flxq_equal0(GEN x) { return lgpol(x)==0; }
    3776             : 
    3777             : static GEN
    3778       25157 : _Flxq_s(void *E, long x)
    3779       25157 : { struct _Flxq *s = (struct _Flxq *)E;
    3780       25157 :   ulong u = x<0 ? s->p+x: (ulong)x;
    3781       25157 :   return Fl_to_Flx(u, get_Flx_var(s->T));
    3782             : }
    3783             : 
    3784             : static const struct bb_field Flxq_field={_Flxq_red,_Flx_add,_Flxq_rmul,_Flxq_neg,
    3785             :                                          _Flxq_inv,_Flxq_equal0,_Flxq_s};
    3786             : 
    3787       73608 : const struct bb_field *get_Flxq_field(void **E, GEN T, ulong p)
    3788             : {
    3789       73608 :   GEN z = new_chunk(sizeof(struct _Flxq));
    3790       73608 :   set_Flxq((struct _Flxq *)z, T, p); *E = (void*)z; return &Flxq_field;
    3791             : }
    3792             : 
    3793             : /***********************************************************************/
    3794             : /**                               Flxn                                **/
    3795             : /***********************************************************************/
    3796             : 
    3797             : GEN
    3798       54248 : Flx_invLaplace(GEN x, ulong p)
    3799             : {
    3800       54248 :   long i, d = degpol(x);
    3801             :   ulong t;
    3802             :   GEN y;
    3803       54247 :   if (d <= 1) return Flx_copy(x);
    3804       54247 :   t = Fl_inv(factorial_Fl(d, p), p);
    3805       54291 :   y = cgetg(d+3, t_VECSMALL);
    3806       54252 :   y[1] = x[1];
    3807     1326183 :   for (i=d; i>=2; i--)
    3808             :   {
    3809     1271896 :     uel(y,i+2) = Fl_mul(uel(x,i+2), t, p);
    3810     1271889 :     t = Fl_mul(t, i, p);
    3811             :   }
    3812       54287 :   uel(y,3) = uel(x,3);
    3813       54287 :   uel(y,2) = uel(x,2);
    3814       54287 :   return y;
    3815             : }
    3816             : 
    3817             : GEN
    3818       27280 : Flx_Laplace(GEN x, ulong p)
    3819             : {
    3820       27280 :   long i, d = degpol(x);
    3821       27278 :   ulong t = 1;
    3822             :   GEN y;
    3823       27278 :   if (d <= 1) return Flx_copy(x);
    3824       27278 :   y = cgetg(d+3, t_VECSMALL);
    3825       27267 :   y[1] = x[1];
    3826       27267 :   uel(y,2) = uel(x,2);
    3827       27267 :   uel(y,3) = uel(x,3);
    3828      755338 :   for (i=2; i<=d; i++)
    3829             :   {
    3830      728041 :     t = Fl_mul(t, i%p, p);
    3831      728075 :     uel(y,i+2) = Fl_mul(uel(x,i+2), t, p);
    3832             :   }
    3833       27297 :   return y;
    3834             : }
    3835             : 
    3836             : GEN
    3837     6228019 : Flxn_red(GEN a, long n)
    3838             : {
    3839     6228019 :   long i, L, l = lg(a);
    3840             :   GEN  b;
    3841     6228019 :   if (l == 2 || !n) return zero_Flx(a[1]);
    3842     5838800 :   L = n+2; if (L > l) L = l;
    3843     5838800 :   b = cgetg(L, t_VECSMALL); b[1] = a[1];
    3844    58558196 :   for (i=2; i<L; i++) b[i] = a[i];
    3845     5836385 :   return Flx_renormalize(b,L);
    3846             : }
    3847             : 
    3848             : GEN
    3849     5061424 : Flxn_mul_pre(GEN a, GEN b, long n, ulong p, ulong pi)
    3850     5061424 : { return Flxn_red(Flx_mul_pre(a, b, p, pi), n); }
    3851             : GEN
    3852       75253 : Flxn_mul(GEN a, GEN b, long n, ulong p)
    3853       75253 : { return Flxn_mul_pre(a, b, n, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3854             : 
    3855             : GEN
    3856           0 : Flxn_sqr_pre(GEN a, long n, ulong p, ulong pi)
    3857           0 : { return Flxn_red(Flx_sqr_pre(a, p, pi), n); }
    3858             : GEN
    3859           0 : Flxn_sqr(GEN a, long n, ulong p)
    3860           0 : { return Flxn_sqr_pre(a, n, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3861             : 
    3862             : /* (f*g) \/ x^n */
    3863             : static GEN
    3864      937377 : Flx_mulhigh_i(GEN f, GEN g, long n, ulong p, ulong pi)
    3865      937377 : { return Flx_shift(Flx_mul_pre(f, g, p, pi),-n); }
    3866             : 
    3867             : static GEN
    3868      515955 : Flxn_mulhigh(GEN f, GEN g, long n2, long n, ulong p, ulong pi)
    3869             : {
    3870      515955 :   GEN F = Flx_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    3871      515534 :   return Flx_add(Flx_mulhigh_i(fl, g, n2, p, pi),
    3872             :                  Flxn_mul_pre(fh, g, n - n2, p, pi), p);
    3873             : }
    3874             : 
    3875             : /* g==NULL -> assume g==1 */
    3876             : GEN
    3877       55054 : Flxn_div_pre(GEN g, GEN f, long e, ulong p, ulong pi)
    3878             : {
    3879       55054 :   pari_sp av = avma, av2;
    3880             :   ulong mask;
    3881             :   GEN W;
    3882       55054 :   long n = 1;
    3883       55054 :   if (lg(f) <= 2) pari_err_INV("Flxn_inv",f);
    3884       55054 :   W = Fl_to_Flx(Fl_inv(uel(f,2),p), f[1]);
    3885       55077 :   mask = quadratic_prec_mask(e);
    3886       55077 :   av2 = avma;
    3887      258357 :   for (;mask>1;)
    3888             :   {
    3889             :     GEN u, fr;
    3890      203259 :     long n2 = n;
    3891      203259 :     n<<=1; if (mask & 1) n--;
    3892      203259 :     mask >>= 1;
    3893      203259 :     fr = Flxn_red(f, n);
    3894      203068 :     if (mask>1 || !g)
    3895             :     {
    3896      149060 :       u = Flxn_mul_pre(W, Flxn_mulhigh(fr, W, n2, n, p, pi), n-n2, p, pi);
    3897      149446 :       W = Flx_sub(W, Flx_shift(u, n2), p);
    3898             :     } else
    3899             :     {
    3900       54008 :       GEN y = Flxn_mul_pre(g, W, n, p, pi), yt =  Flxn_red(y, n-n2);
    3901       53987 :       u = Flxn_mul_pre(yt, Flxn_mulhigh(fr,  W, n2, n, p, pi), n-n2, p, pi);
    3902       53991 :       W = Flx_sub(y, Flx_shift(u, n2), p);
    3903             :     }
    3904      203275 :     if (gc_needed(av2,2))
    3905             :     {
    3906           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Flxn_div, e = %ld", n);
    3907           0 :       W = gerepileupto(av2, W);
    3908             :     }
    3909             :   }
    3910       55098 :   return gerepileupto(av, W);
    3911             : }
    3912             : GEN
    3913       55020 : Flxn_div(GEN g, GEN f, long e, ulong p)
    3914       55020 : { return Flxn_div_pre(g, f, e, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    3915             : 
    3916             : GEN
    3917        1030 : Flxn_inv(GEN f, long e, ulong p)
    3918        1030 : { return Flxn_div(NULL, f, e, p); }
    3919             : 
    3920             : GEN
    3921      109342 : Flxn_expint(GEN h, long e, ulong p)
    3922             : {
    3923      109342 :   pari_sp av = avma, av2;
    3924      109342 :   long v = h[1], n=1;
    3925      109342 :   GEN f = pol1_Flx(v), g = pol1_Flx(v);
    3926      109312 :   ulong mask = quadratic_prec_mask(e), pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    3927      109316 :   av2 = avma;
    3928      422578 :   for (;mask>1;)
    3929             :   {
    3930             :     GEN u, w;
    3931      422521 :     long n2 = n;
    3932      422521 :     n<<=1; if (mask & 1) n--;
    3933      422521 :     mask >>= 1;
    3934      422521 :     u = Flxn_mul_pre(g, Flx_mulhigh_i(f, Flxn_red(h, n2-1), n2-1, p,pi), n-n2, p,pi);
    3935      422495 :     u = Flx_add(u, Flx_shift(Flxn_red(h, n-1), 1-n2), p);
    3936      422508 :     w = Flxn_mul_pre(f, Flx_integXn(u, n2-1, p), n-n2, p, pi);
    3937      422458 :     f = Flx_add(f, Flx_shift(w, n2), p);
    3938      422617 :     if (mask<=1) break;
    3939      313283 :     u = Flxn_mul_pre(g, Flxn_mulhigh(f, g, n2, n, p, pi), n-n2, p, pi);
    3940      313242 :     g = Flx_sub(g, Flx_shift(u, n2), p);
    3941      313262 :     if (gc_needed(av2,2))
    3942             :     {
    3943           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flxn_exp, e = %ld", n);
    3944           0 :       gerepileall(av2, 2, &f, &g);
    3945             :     }
    3946             :   }
    3947      109391 :   return gerepileupto(av, f);
    3948             : }
    3949             : 
    3950             : GEN
    3951           0 : Flxn_exp(GEN h, long e, ulong p)
    3952             : {
    3953           0 :   if (degpol(h)<1 || uel(h,2)!=0)
    3954           0 :     pari_err_DOMAIN("Flxn_exp","valuation", "<", gen_1, h);
    3955           0 :   return Flxn_expint(Flx_deriv(h, p), e, p);
    3956             : }
    3957             : 
    3958             : INLINE GEN
    3959      217014 : Flxn_recip(GEN x, long n)
    3960             : {
    3961      217014 :   GEN z=Flx_recipspec(x+2,lgpol(x),n);
    3962      216830 :   z[1]=x[1];
    3963      216830 :   return z;
    3964             : }
    3965             : 
    3966             : GEN
    3967       53998 : Flx_Newton(GEN P, long n, ulong p)
    3968             : {
    3969       53998 :   pari_sp av = avma;
    3970       53998 :   long d = degpol(P);
    3971       53992 :   GEN dP = Flxn_recip(Flx_deriv(P, p), d);
    3972       53878 :   GEN Q = Flxn_div(dP, Flxn_recip(P, d+1), n, p);
    3973       53952 :   return gerepileuptoleaf(av, Q);
    3974             : }
    3975             : 
    3976             : GEN
    3977      109345 : Flx_fromNewton(GEN P, ulong p)
    3978             : {
    3979      109345 :   pari_sp av = avma;
    3980      109345 :   ulong n = Flx_constant(P)+1;
    3981      109345 :   GEN z = Flx_neg(Flx_shift(P, -1), p);
    3982      109342 :   GEN Q = Flxn_recip(Flxn_expint(z, n, p), n);
    3983      109319 :   return gerepileuptoleaf(av, Q);
    3984             : }
    3985             : 
    3986             : static void
    3987       12514 : init_invlaplace(long d, ulong p, GEN *pt_P, GEN *pt_V)
    3988             : {
    3989             :   long i;
    3990             :   ulong e;
    3991       12514 :   GEN P = cgetg(d+1, t_VECSMALL);
    3992       12514 :   GEN V = cgetg(d+1, t_VECSMALL);
    3993     1396581 :   for (i=1, e=1; i<=d; i++, e++)
    3994             :   {
    3995     1384067 :     if (e==p)
    3996             :     {
    3997      459153 :       e = 0;
    3998      459153 :       V[i] = u_lvalrem(i, p, &uel(P,i));
    3999             :     } else
    4000             :     {
    4001      924914 :       V[i] = 0; uel(P,i) = i;
    4002             :     }
    4003             :   }
    4004       12514 :   *pt_P = P; *pt_V = V;
    4005       12514 : }
    4006             : 
    4007             : /* return p^val * FpX_invLaplace(1+x+...x^(n-1), q), with q a power of p and
    4008             :  * val large enough to compensate for the power of p in the factorials */
    4009             : 
    4010             : static GEN
    4011         497 : ZpX_invLaplace_init(long n, GEN q, ulong p, long v, long sv)
    4012             : {
    4013         497 :   pari_sp av = avma;
    4014         497 :   long i, d = n-1, w;
    4015             :   GEN y, W, E, t;
    4016         497 :   init_invlaplace(d, p, &E, &W);
    4017         497 :   t = Fp_inv(FpV_prod(Flv_to_ZV(E), q), q);
    4018         497 :   w = zv_sum(W);
    4019         497 :   if (v > w) t = Fp_mul(t, powuu(p, v-w), q);
    4020         497 :   y = cgetg(d+3,t_POL);
    4021         497 :   y[1] = evalsigne(1) | sv;
    4022       28882 :   for (i=d; i>=1; i--)
    4023             :   {
    4024       28385 :     gel(y,i+2) = t;
    4025       28385 :     t = Fp_mulu(t, uel(E,i), q);
    4026       28385 :     if (uel(W,i)) t = Fp_mul(t, powuu(p, uel(W,i)), q);
    4027             :   }
    4028         497 :   gel(y,2) = t;
    4029         497 :   return gerepilecopy(av, ZX_renormalize(y, d+3));
    4030             : }
    4031             : 
    4032             : GEN
    4033       27490 : Flx_composedsum(GEN P, GEN Q, ulong p)
    4034             : {
    4035       27490 :   pari_sp av = avma;
    4036       27490 :   long n = 1 + degpol(P)*degpol(Q);
    4037       27486 :   ulong lead = Fl_mul(Fl_powu(Flx_lead(P), degpol(Q), p),
    4038       27486 :                       Fl_powu(Flx_lead(Q), degpol(P), p), p);
    4039             :   GEN R;
    4040       27497 :   if (p >= (ulong)n)
    4041             :   {
    4042       27000 :     GEN Pl = Flx_invLaplace(Flx_Newton(P,n,p), p);
    4043       27003 :     GEN Ql = Flx_invLaplace(Flx_Newton(Q,n,p), p);
    4044       27001 :     GEN L  = Flx_Laplace(Flxn_mul(Pl, Ql, n, p), p);
    4045       27001 :     R = Flx_fromNewton(L, p);
    4046             :   } else
    4047             :   {
    4048         497 :     long v = factorial_lval(n-1, p);
    4049         497 :     long w = 1 + ulogint(n-1, p);
    4050         497 :     GEN pv = powuu(p, v);
    4051         497 :     GEN qf = powuu(p, w), q = mulii(pv, qf), q2 = mulii(q, pv);
    4052         497 :     GEN iL = ZpX_invLaplace_init(n, q, p, v, P[1]);
    4053         497 :     GEN Pl = FpX_convol(iL, FpX_Newton(Flx_to_ZX(P), n, qf), q);
    4054         497 :     GEN Ql = FpX_convol(iL, FpX_Newton(Flx_to_ZX(Q), n, qf), q);
    4055         497 :     GEN Ln = ZX_Z_divexact(FpXn_mul(Pl, Ql, n, q2), pv);
    4056         497 :     GEN L  = ZX_Z_divexact(FpX_Laplace(Ln, q), pv);
    4057         497 :     R = ZX_to_Flx(FpX_fromNewton(L, qf), p);
    4058             :   }
    4059       27478 :   return gerepileuptoleaf(av, Flx_Fl_mul(R, lead, p));
    4060             : }
    4061             : 
    4062             : static GEN
    4063        3826 : _Flx_composedsum(void *E, GEN a, GEN b)
    4064        3826 : { return Flx_composedsum(a, b, (ulong)E); }
    4065             : 
    4066             : GEN
    4067       28908 : FlxV_composedsum(GEN V, ulong p)
    4068       28908 : { return gen_product(V, (void *)p, &_Flx_composedsum); }
    4069             : 
    4070             : GEN
    4071           0 : Flx_composedprod(GEN P, GEN Q, ulong p)
    4072             : {
    4073           0 :   pari_sp av = avma;
    4074           0 :   long n = 1+ degpol(P)*degpol(Q);
    4075           0 :   ulong lead = Fl_mul(Fl_powu(Flx_lead(P), degpol(Q), p),
    4076           0 :                       Fl_powu(Flx_lead(Q), degpol(P), p), p);
    4077             :   GEN R;
    4078           0 :   if (p >= (ulong)n)
    4079             :   {
    4080           0 :     GEN L = Flx_convol(Flx_Newton(P,n,p), Flx_Newton(Q,n,p), p);
    4081           0 :     R = Flx_fromNewton(L, p);
    4082             :   } else
    4083             :   {
    4084           0 :     long w = 1 + ulogint(n, p);
    4085           0 :     GEN qf = powuu(p, w);
    4086           0 :     GEN Pl = FpX_convol(FpX_Newton(Flx_to_ZX(P), n, qf), FpX_Newton(Flx_to_ZX(Q), n, qf), qf);
    4087           0 :     R = ZX_to_Flx(FpX_fromNewton(Pl, qf), p);
    4088             :   }
    4089           0 :   return gerepileuptoleaf(av, Flx_Fl_mul(R, lead, p));
    4090             : 
    4091             : }
    4092             : 
    4093             : /* (x+1)^n mod p; assume 2 <= n < 2p prime */
    4094             : static GEN
    4095           0 : Fl_Xp1_powu(ulong n, ulong p, long v)
    4096             : {
    4097           0 :   ulong k, d = (n + 1) >> 1;
    4098           0 :   GEN C, V = identity_zv(d);
    4099             : 
    4100           0 :   Flv_inv_inplace(V, p); /* could restrict to odd integers in [3,d] */
    4101           0 :   C = cgetg(n+3, t_VECSMALL);
    4102           0 :   C[1] = v;
    4103           0 :   uel(C,2) = 1UL;
    4104           0 :   uel(C,3) = n%p;
    4105           0 :   uel(C,4) = Fl_mul(odd(n)? n: n-1, n >> 1, p);
    4106             :     /* binom(n,k) = binom(n,k-1) * (n-k+1) / k */
    4107           0 :   if (SMALL_ULONG(p))
    4108           0 :     for (k = 3; k <= d; k++)
    4109           0 :       uel(C,k+2) = Fl_mul(Fl_mul(n-k+1, uel(C,k+1), p), uel(V,k), p);
    4110             :   else
    4111             :   {
    4112           0 :     ulong pi  = get_Fl_red(p);
    4113           0 :     for (k = 3; k <= d; k++)
    4114           0 :       uel(C,k+2) = Fl_mul_pre(Fl_mul(n-k+1, uel(C,k+1), p), uel(V,k), p, pi);
    4115             :   }
    4116           0 :   for (   ; k <= n; k++) uel(C,2+k) = uel(C,2+n-k);
    4117           0 :   return C; /* normalized */
    4118             : }
    4119             : 
    4120             : /* p arbitrary */
    4121             : GEN
    4122       28201 : Flx_translate1_basecase(GEN P, ulong p)
    4123             : {
    4124       28201 :   GEN R = Flx_copy(P);
    4125       28201 :   long i, k, n = degpol(P);
    4126      654753 :   for (i = 1; i <= n; i++)
    4127    14846523 :     for (k = n-i; k < n; k++) uel(R,k+2) = Fl_add(uel(R,k+2), uel(R,k+3), p);
    4128       28201 :   return R;
    4129             : }
    4130             : 
    4131             : static int
    4132       41366 : translate_basecase(long n, ulong p)
    4133             : {
    4134             : #ifdef LONG_IS_64BIT
    4135       36072 :   if (p <= 19) return n < 40;
    4136       29910 :   if (p < 1UL<<30) return n < 58;
    4137           0 :   if (p < 1UL<<59) return n < 100;
    4138           0 :   if (p < 1UL<<62) return n < 120;
    4139           0 :   if (p < 1UL<<63) return n < 240;
    4140           0 :   return n < 250;
    4141             : #else
    4142        5294 :   if (p <= 13) return n < 18;
    4143        4136 :   if (p <= 17) return n < 22;
    4144        4078 :   if (p <= 29) return n < 39;
    4145        3886 :   if (p <= 67) return n < 69;
    4146        3667 :   if (p < 1UL<< 15) return n < 80;
    4147        2047 :   if (p < 1UL<< 16) return n < 100;
    4148           0 :   if (p < 1UL<< 28) return n < 300;
    4149           0 :   return n < 650;
    4150             : #endif
    4151             : }
    4152             : /* assume p prime */
    4153             : GEN
    4154       16107 : Flx_translate1(GEN P, ulong p)
    4155             : {
    4156       16107 :   long d, n = degpol(P);
    4157             :   GEN R, Q, S;
    4158       16107 :   if (translate_basecase(n, p)) return Flx_translate1_basecase(P, p);
    4159             :   /* n > 0 */
    4160        1148 :   d = n >> 1;
    4161        1148 :   if ((ulong)n < p)
    4162             :   {
    4163           0 :     R = Flx_translate1(Flxn_red(P, d), p);
    4164           0 :     Q = Flx_translate1(Flx_shift(P, -d), p);
    4165           0 :     S = Fl_Xp1_powu(d, p, P[1]);
    4166           0 :     return Flx_add(Flx_mul(Q, S, p), R, p);
    4167             :   }
    4168             :   else
    4169             :   {
    4170             :     ulong q;
    4171        1148 :     if ((ulong)d > p) (void)ulogintall(d, p, &q); else q = p;
    4172        1148 :     R = Flx_translate1(Flxn_red(P, q), p);
    4173        1148 :     Q = Flx_translate1(Flx_shift(P, -q), p);
    4174        1148 :     S = Flx_add(Flx_shift(Q, q), Q, p);
    4175        1148 :     return Flx_add(S, R, p); /* P(x+1) = Q(x+1) (x^q+1) + R(x+1) */
    4176             :   }
    4177             : }
    4178             : 
    4179             : static GEN
    4180       12017 : zl_Xp1_powu(ulong n, ulong p, ulong q, long e, long vs)
    4181             : {
    4182       12017 :   ulong k, d = n >> 1, c, v = 0;
    4183       12017 :   GEN C, V, W, U = upowers(p, e-1);
    4184       12017 :   init_invlaplace(d, p, &V, &W);
    4185       12017 :   Flv_inv_inplace(V, q);
    4186       12017 :   C = cgetg(n+3, t_VECSMALL);
    4187       12017 :   C[1] = vs;
    4188       12017 :   uel(C,2) = 1UL;
    4189       12017 :   uel(C,3) = n%q;
    4190       12017 :   v = u_lvalrem(n, p, &c);
    4191     1355682 :   for (k = 2; k <= d; k++)
    4192             :   {
    4193             :     ulong w;
    4194     1343665 :     v += u_lvalrem(n-k+1, p, &w) - W[k];
    4195     1343665 :     c = Fl_mul(Fl_mul(w%q, c, q), uel(V,k), q);
    4196     1343665 :     uel(C,2+k) = v >= (ulong)e ? 0: v==0 ? c : Fl_mul(c, uel(U, v+1), q);
    4197             :   }
    4198     1374521 :   for (   ; k <= n; k++) uel(C,2+k) = uel(C,2+n-k);
    4199       12017 :   return C; /* normalized */
    4200             : }
    4201             : 
    4202             : GEN
    4203       25259 : zlx_translate1(GEN P, ulong p, long e)
    4204             : {
    4205       25259 :   ulong d, q = upowuu(p,e), n = degpol(P);
    4206             :   GEN R, Q, S;
    4207       25259 :   if (translate_basecase(n, q)) return Flx_translate1_basecase(P, q);
    4208             :   /* n > 0 */
    4209       12017 :   d = n >> 1;
    4210       12017 :   R = zlx_translate1(Flxn_red(P, d), p, e);
    4211       12017 :   Q = zlx_translate1(Flx_shift(P, -d), p, e);
    4212       12017 :   S = zl_Xp1_powu(d, p, q, e, P[1]);
    4213       12017 :   return Flx_add(Flx_mul(Q, S, q), R, q);
    4214             : }
    4215             : 
    4216             : /***********************************************************************/
    4217             : /**                               Fl2                                 **/
    4218             : /***********************************************************************/
    4219             : /* Fl2 objects are Flv of length 2 [a,b] representing a+bsqrt(D) for
    4220             :  * a nonsquare D. */
    4221             : 
    4222             : INLINE GEN
    4223     7191105 : mkF2(ulong a, ulong b) { return mkvecsmall2(a,b); }
    4224             : 
    4225             : /* allow pi = 0 */
    4226             : GEN
    4227     1916282 : Fl2_mul_pre(GEN x, GEN y, ulong D, ulong p, ulong pi)
    4228             : {
    4229             :   ulong xaya, xbyb, Db2, mid, z1, z2;
    4230     1916282 :   ulong x1 = x[1], x2 = x[2], y1 = y[1], y2 = y[2];
    4231     1916282 :   if (pi)
    4232             :   {
    4233     1916298 :     xaya = Fl_mul_pre(x1,y1,p,pi);
    4234     1916750 :     if (x2==0 && y2==0) return mkF2(xaya,0);
    4235     1847780 :     if (x2==0) return mkF2(xaya,Fl_mul_pre(x1,y2,p,pi));
    4236     1823243 :     if (y2==0) return mkF2(xaya,Fl_mul_pre(x2,y1,p,pi));
    4237     1822934 :     xbyb = Fl_mul_pre(x2,y2,p,pi);
    4238     1822773 :     mid = Fl_mul_pre(Fl_add(x1,x2,p), Fl_add(y1,y2,p),p,pi);
    4239     1822985 :     Db2 = Fl_mul_pre(D, xbyb, p,pi);
    4240             :   }
    4241           0 :   else if (p & HIGHMASK)
    4242             :   {
    4243           0 :     xaya = Fl_mul(x1,y1,p);
    4244           0 :     if (x2==0 && y2==0) return mkF2(xaya,0);
    4245           0 :     if (x2==0) return mkF2(xaya,Fl_mul(x1,y2,p));
    4246           0 :     if (y2==0) return mkF2(xaya,Fl_mul(x2,y1,p));
    4247           0 :     xbyb = Fl_mul(x2,y2,p);
    4248           0 :     mid = Fl_mul(Fl_add(x1,x2,p), Fl_add(y1,y2,p),p);
    4249           0 :     Db2 = Fl_mul(D, xbyb, p);
    4250             :   }
    4251             :   else
    4252             :   {
    4253           0 :     xaya = (x1 * y1) % p;
    4254           0 :     if (x2==0 && y2==0) return mkF2(xaya,0);
    4255           0 :     if (x2==0) return mkF2(xaya, (x1 * y2) % p);
    4256           0 :     if (y2==0) return mkF2(xaya, (x2 * y1) % p);
    4257           0 :     xbyb = (x2 * y2) % p;
    4258           0 :     mid = (Fl_add(x1,x2,p) * Fl_add(y1,y2,p)) % p;
    4259           0 :     Db2 = (D * xbyb) % p;
    4260             :   }
    4261     1822870 :   z1 = Fl_add(xaya,Db2,p);
    4262     1822868 :   z2 = Fl_sub(mid,Fl_add(xaya,xbyb,p),p);
    4263     1822893 :   return mkF2(z1,z2);
    4264             : }
    4265             : 
    4266             : /* allow pi = 0 */
    4267             : GEN
    4268     4822896 : Fl2_sqr_pre(GEN x, ulong D, ulong p, ulong pi)
    4269             : {
    4270     4822896 :   ulong a = x[1], b = x[2];
    4271             :   ulong a2, Db2, ab;
    4272     4822896 :   if (pi)
    4273             :   {
    4274     4822912 :     a2 = Fl_sqr_pre(a,p,pi);
    4275     4825154 :     if (b==0) return mkF2(a2,0);
    4276     4612413 :     Db2= Fl_mul_pre(D, Fl_sqr_pre(b,p,pi), p,pi);
    4277     4612407 :     ab = Fl_mul_pre(a,b,p,pi);
    4278             :   }
    4279           0 :   else if (p & HIGHMASK)
    4280             :   {
    4281           0 :     a2 = Fl_sqr(a,p);
    4282           0 :     if (b==0) return mkF2(a2,0);
    4283           0 :     Db2= Fl_mul(D, Fl_sqr(b,p), p);
    4284           0 :     ab = Fl_mul(a,b,p);
    4285             :   }
    4286             :   else
    4287             :   {
    4288           0 :     a2 = (a * a) % p;
    4289           0 :     if (b==0) return mkF2(a2,0);
    4290           0 :     Db2= (D * ((b * b) % p)) % p;
    4291           0 :     ab = (a * b) % p;
    4292             :   }
    4293     4612476 :   return mkF2(Fl_add(a2,Db2,p), Fl_double(ab,p));
    4294             : }
    4295             : 
    4296             : /* allow pi = 0 */
    4297             : ulong
    4298      126079 : Fl2_norm_pre(GEN x, ulong D, ulong p, ulong pi)
    4299             : {
    4300      126079 :   ulong a = x[1], b = x[2], a2;
    4301      126079 :   if (pi)
    4302             :   {
    4303       72287 :     a2 = Fl_sqr_pre(a,p,pi);
    4304       72288 :     return b? Fl_sub(a2, Fl_mul_pre(D, Fl_sqr_pre(b, p,pi), p,pi), p): a2;
    4305             :   }
    4306       53792 :   else if (p & HIGHMASK)
    4307             :   {
    4308           0 :     a2 = Fl_sqr(a,p);
    4309           0 :     return b? Fl_sub(a2, Fl_mul(D, Fl_sqr(b, p), p), p): a2;
    4310             :   }
    4311             :   else
    4312             :   {
    4313       53792 :     a2 = (a * a) % p;
    4314       53792 :     return b? Fl_sub(a2, (D * ((b * b) % p)) % p, p): a2;
    4315             :   }
    4316             : }
    4317             : 
    4318             : /* allow pi = 0 */
    4319             : GEN
    4320      192890 : Fl2_inv_pre(GEN x, ulong D, ulong p, ulong pi)
    4321             : {
    4322      192890 :   ulong a = x[1], b = x[2], n, ni;
    4323      192890 :   if (b == 0) return mkF2(Fl_inv(a,p), 0);
    4324      162176 :   b = Fl_neg(b, p);
    4325      162182 :   if (pi)
    4326             :   {
    4327      162182 :     n = Fl_sub(Fl_sqr_pre(a, p,pi),
    4328             :                Fl_mul_pre(D, Fl_sqr_pre(b, p,pi), p,pi), p);
    4329      162180 :     ni = Fl_inv(n,p);
    4330      162184 :     return mkF2(Fl_mul_pre(a, ni, p,pi), Fl_mul_pre(b, ni, p,pi));
    4331             :   }
    4332           0 :   else if (p & HIGHMASK)
    4333             :   {
    4334           0 :     n = Fl_sub(Fl_sqr(a, p), Fl_mul(D, Fl_sqr(b, p), p), p);
    4335           0 :     ni = Fl_inv(n,p);
    4336           0 :     return mkF2(Fl_mul(a, ni, p), Fl_mul(b, ni, p));
    4337             :   }
    4338             :   else
    4339             :   {
    4340           0 :     n = Fl_sub((a * a) % p, (D * ((b * b) % p)) % p, p);
    4341           0 :     ni = Fl_inv(n,p);
    4342           0 :     return mkF2((a * ni) % p, (b * ni) % p);
    4343             :   }
    4344             : }
    4345             : 
    4346             : int
    4347      439584 : Fl2_equal1(GEN x) { return x[1]==1 && x[2]==0; }
    4348             : 
    4349             : struct _Fl2 {
    4350             :   ulong p, pi, D;
    4351             : };
    4352             : 
    4353             : static GEN
    4354     4822935 : _Fl2_sqr(void *data, GEN x)
    4355             : {
    4356     4822935 :   struct _Fl2 *D = (struct _Fl2*)data;
    4357     4822935 :   return Fl2_sqr_pre(x, D->D, D->p, D->pi);
    4358             : }
    4359             : static GEN
    4360     1888186 : _Fl2_mul(void *data, GEN x, GEN y)
    4361             : {
    4362     1888186 :   struct _Fl2 *D = (struct _Fl2*)data;
    4363     1888186 :   return Fl2_mul_pre(x,y, D->D, D->p, D->pi);
    4364             : }
    4365             : 
    4366             : /* n-Power of x in Z/pZ[X]/(T), as t_VECSMALL; allow pi = 0 */
    4367             : GEN
    4368      656880 : Fl2_pow_pre(GEN x, GEN n, ulong D, ulong p, ulong pi)
    4369             : {
    4370      656880 :   pari_sp av = avma;
    4371             :   struct _Fl2 d;
    4372             :   GEN y;
    4373      656880 :   long s = signe(n);
    4374      656880 :   if (!s) return mkF2(1,0);
    4375      582195 :   if (s < 0)
    4376      192889 :     x = Fl2_inv_pre(x,D,p,pi);
    4377      582191 :   if (is_pm1(n)) return s < 0 ? x : zv_copy(x);
    4378      428882 :   d.p = p; d.pi = pi; d.D=D;
    4379      428882 :   y = gen_pow_i(x, n, (void*)&d, &_Fl2_sqr, &_Fl2_mul);
    4380      428904 :   return gerepileuptoleaf(av, y);
    4381             : }
    4382             : 
    4383             : static GEN
    4384      656865 : _Fl2_pow(void *data, GEN x, GEN n)
    4385             : {
    4386      656865 :   struct _Fl2 *D = (struct _Fl2*)data;
    4387      656865 :   return Fl2_pow_pre(x, n, D->D, D->p, D->pi);
    4388             : }
    4389             : 
    4390             : static GEN
    4391      111163 : _Fl2_rand(void *data)
    4392             : {
    4393      111163 :   struct _Fl2 *D = (struct _Fl2*)data;
    4394      111163 :   ulong a = random_Fl(D->p), b=random_Fl(D->p-1)+1;
    4395      111164 :   return mkF2(a,b);
    4396             : }
    4397             : 
    4398             : GEN
    4399       67842 : Fl2_sqrt_pre(GEN z, ulong D, ulong p, ulong pi)
    4400             : {
    4401       67842 :   ulong a = uel(z,1), b = uel(z,2), as2, u, v, s;
    4402       67842 :   ulong y = Fl_2gener_pre_i(D, p, pi);
    4403       67842 :   if (b == 0)
    4404       19476 :     return krouu(a, p)==1 ? mkF2(Fl_sqrt_pre_i(a, y, p, pi), 0)
    4405       19476 :                           : mkF2(0, Fl_sqrt_pre_i(Fl_div(a, D, p), y, p, pi));
    4406       54622 :   s = Fl_sqrt_pre_i(Fl2_norm_pre(z, D, p, pi), y, p, pi);
    4407       54622 :   if (s==~0UL) return NULL;
    4408       51448 :   as2 = Fl_halve(Fl_add(a, s, p), p);
    4409       51448 :   if (krouu(as2, p)==-1) as2 = Fl_sub(as2, s, p);
    4410       51448 :   u = Fl_sqrt_pre_i(as2, y, p, pi);
    4411       51448 :   v = Fl_div(b, Fl_double(u, p), p);
    4412       51448 :   return mkF2(u,v);
    4413             : }
    4414             : 
    4415             : static const struct bb_group Fl2_star={_Fl2_mul, _Fl2_pow, _Fl2_rand,
    4416             :        hash_GEN, zv_equal, Fl2_equal1, NULL};
    4417             : 
    4418             : /* allow pi = 0 */
    4419             : GEN
    4420       74687 : Fl2_sqrtn_pre(GEN a, GEN n, ulong D, ulong p, ulong pi, GEN *zeta)
    4421             : {
    4422             :   struct _Fl2 E;
    4423             :   GEN o;
    4424       74687 :   if (a[1]==0 && a[2]==0)
    4425             :   {
    4426           0 :     if (signe(n) < 0) pari_err_INV("Flxq_sqrtn",a);
    4427           0 :     if (zeta) *zeta=mkF2(1,0);
    4428           0 :     return zv_copy(a);
    4429             :   }
    4430       74687 :   E.p=p; E.pi = pi; E.D = D;
    4431       74687 :   o = subiu(powuu(p,2), 1);
    4432       74682 :   return gen_Shanks_sqrtn(a,n,o,zeta,(void*)&E,&Fl2_star);
    4433             : }
    4434             : 
    4435             : /* allow pi = 0 */
    4436             : GEN
    4437       10402 : Flx_Fl2_eval_pre(GEN x, GEN y, ulong D, ulong p, ulong pi)
    4438             : {
    4439             :   GEN p1;
    4440       10402 :   long i = lg(x)-1;
    4441       10402 :   if (i <= 2)
    4442        2065 :     return mkF2(i == 2? x[2]: 0, 0);
    4443        8337 :   p1 = mkF2(x[i], 0);
    4444       36456 :   for (i--; i>=2; i--)
    4445             :   {
    4446       28119 :     p1 = Fl2_mul_pre(p1, y, D, p, pi);
    4447       28119 :     uel(p1,1) = Fl_add(uel(p1,1), uel(x,i), p);
    4448             :   }
    4449        8337 :   return p1;
    4450             : }
    4451             : 
    4452             : /***********************************************************************/
    4453             : /**                               FlxV                                **/
    4454             : /***********************************************************************/
    4455             : /* FlxV are t_VEC with Flx coefficients. */
    4456             : 
    4457             : GEN
    4458       34482 : FlxV_Flc_mul(GEN V, GEN W, ulong p)
    4459             : {
    4460       34482 :   pari_sp ltop=avma;
    4461             :   long i;
    4462       34482 :   GEN z = Flx_Fl_mul(gel(V,1),W[1],p);
    4463      257058 :   for(i=2;i<lg(V);i++)
    4464      222576 :     z=Flx_add(z,Flx_Fl_mul(gel(V,i),W[i],p),p);
    4465       34482 :   return gerepileuptoleaf(ltop,z);
    4466             : }
    4467             : 
    4468             : GEN
    4469           0 : ZXV_to_FlxV(GEN x, ulong p)
    4470           0 : { pari_APPLY_type(t_VEC, ZX_to_Flx(gel(x,i), p)) }
    4471             : 
    4472             : GEN
    4473     3782718 : ZXT_to_FlxT(GEN x, ulong p)
    4474             : {
    4475     3782718 :   if (typ(x) == t_POL)
    4476     3723924 :     return ZX_to_Flx(x, p);
    4477             :   else
    4478      193012 :     pari_APPLY_type(t_VEC, ZXT_to_FlxT(gel(x,i), p))
    4479             : }
    4480             : 
    4481             : GEN
    4482      171714 : FlxV_to_Flm(GEN x, long n)
    4483      926411 : { pari_APPLY_type(t_MAT, Flx_to_Flv(gel(x,i), n)) }
    4484             : 
    4485             : GEN
    4486           0 : FlxV_red(GEN x, ulong p)
    4487           0 : { pari_APPLY_type(t_VEC, Flx_red(gel(x,i), p)) }
    4488             : 
    4489             : GEN
    4490      295611 : FlxT_red(GEN x, ulong p)
    4491             : {
    4492      295611 :   if (typ(x) == t_VECSMALL)
    4493      198889 :     return Flx_red(x, p);
    4494             :   else
    4495      324319 :     pari_APPLY_type(t_VEC, FlxT_red(gel(x,i), p))
    4496             : }
    4497             : 
    4498             : GEN
    4499      113589 : FlxqV_dotproduct_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
    4500             : {
    4501      113589 :   long i, lx = lg(x);
    4502             :   pari_sp av;
    4503             :   GEN c;
    4504      113589 :   if (lx == 1) return pol0_Flx(get_Flx_var(T));
    4505      113589 :   av = avma; c = Flx_mul_pre(gel(x,1),gel(y,1), p, pi);
    4506      464499 :   for (i=2; i<lx; i++) c = Flx_add(c, Flx_mul_pre(gel(x,i),gel(y,i), p, pi), p);
    4507      113589 :   return gerepileuptoleaf(av, Flx_rem_pre(c,T,p,pi));
    4508             : }
    4509             : GEN
    4510           0 : FlxqV_dotproduct(GEN x, GEN y, GEN T, ulong p)
    4511           0 : { return FlxqV_dotproduct_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
    4512             : 
    4513             : GEN
    4514        2456 : FlxqX_dotproduct(GEN x, GEN y, GEN T, ulong p)
    4515             : {
    4516        2456 :   long i, l = minss(lg(x), lg(y));
    4517             :   ulong pi;
    4518             :   pari_sp av;
    4519             :   GEN c;
    4520        2456 :   if (l == 2) return pol0_Flx(get_Flx_var(T));
    4521        2387 :   av = avma; pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    4522        2387 :   c = Flx_mul_pre(gel(x,2),gel(y,2), p, pi);
    4523        7169 :   for (i=3; i<l; i++) c = Flx_add(c, Flx_mul_pre(gel(x,i),gel(y,i), p, pi), p);
    4524        2387 :   return gerepileuptoleaf(av, Flx_rem_pre(c,T,p,pi));
    4525             : }
    4526             : 
    4527             : /* allow pi = 0 */
    4528             : GEN
    4529      250784 : FlxC_eval_powers_pre(GEN z, GEN x, ulong p, ulong pi)
    4530             : {
    4531      250784 :   long i, l = lg(z);
    4532      250784 :   GEN y = cgetg(l, t_VECSMALL);
    4533    12740527 :   for (i=1; i<l; i++) uel(y,i) = Flx_eval_powers_pre(gel(z,i), x, p, pi);
    4534      250786 :   return y;
    4535             : }
    4536             : 
    4537             : /***********************************************************************/
    4538             : /**                               FlxM                                **/
    4539             : /***********************************************************************/
    4540             : /* allow pi = 0 */
    4541             : GEN
    4542       19404 : FlxM_eval_powers_pre(GEN z, GEN x, ulong p, ulong pi)
    4543             : {
    4544       19404 :   long i, l = lg(z);
    4545       19404 :   GEN y = cgetg(l, t_MAT);
    4546      270188 :   for (i=1; i<l; i++) gel(y,i) = FlxC_eval_powers_pre(gel(z,i), x, p, pi);
    4547       19404 :   return y;
    4548             : }
    4549             : 
    4550             : GEN
    4551           0 : zero_FlxC(long n, long sv)
    4552             : {
    4553           0 :   GEN x = cgetg(n + 1, t_COL), z = zero_Flx(sv);
    4554             :   long i;
    4555           0 :   for (i = 1; i <= n; i++) gel(x, i) = z;
    4556           0 :   return x;
    4557             : }
    4558             : 
    4559             : GEN
    4560           0 : FlxC_neg(GEN x, ulong p)
    4561           0 : { pari_APPLY_type(t_COL, Flx_neg(gel(x, i), p)) }
    4562             : 
    4563             : GEN
    4564           0 : FlxC_sub(GEN x, GEN y, ulong p)
    4565           0 : { pari_APPLY_type(t_COL, Flx_sub(gel(x, i), gel(y, i), p)) }
    4566             : 
    4567             : GEN
    4568           0 : zero_FlxM(long r, long c, long sv)
    4569             : {
    4570           0 :   GEN x = cgetg(c + 1, t_MAT), z = zero_FlxC(r, sv);
    4571             :   long j;
    4572           0 :   for (j = 1; j <= c; j++) gel(x, j) = z;
    4573           0 :   return x;
    4574             : }
    4575             : 
    4576             : GEN
    4577           0 : FlxM_neg(GEN x, ulong p)
    4578           0 : { pari_APPLY_same(FlxC_neg(gel(x, i), p)) }
    4579             : 
    4580             : GEN
    4581           0 : FlxM_sub(GEN x, GEN y, ulong p)
    4582           0 : { pari_APPLY_same(FlxC_sub(gel(x, i), gel(y,i), p)) }
    4583             : 
    4584             : GEN
    4585           0 : FlxqC_Flxq_mul(GEN x, GEN y, GEN T, ulong p)
    4586           0 : { pari_APPLY_type(t_COL, Flxq_mul(gel(x, i), y, T, p)) }
    4587             : 
    4588             : GEN
    4589           0 : FlxqM_Flxq_mul(GEN x, GEN y, GEN T, ulong p)
    4590           0 : { pari_APPLY_same(FlxqC_Flxq_mul(gel(x, i), y, T, p)) }
    4591             : 
    4592             : static GEN
    4593       58683 : FlxM_pack_ZM(GEN M, GEN (*pack)(GEN, long)) {
    4594             :   long i, j, l, lc;
    4595       58683 :   GEN N = cgetg_copy(M, &l), x;
    4596       58683 :   if (l == 1)
    4597           0 :     return N;
    4598       58683 :   lc = lgcols(M);
    4599      279934 :   for (j = 1; j < l; j++) {
    4600      221251 :     gel(N, j) = cgetg(lc, t_COL);
    4601     1135923 :     for (i = 1; i < lc; i++) {
    4602      914672 :       x = gcoeff(M, i, j);
    4603      914672 :       gcoeff(N, i, j) = pack(x + 2, lgpol(x));
    4604             :     }
    4605             :   }
    4606       58683 :   return N;
    4607             : }
    4608             : 
    4609             : static GEN
    4610      857451 : kron_pack_Flx_spec_half(GEN x, long l) {
    4611      857451 :   if (l == 0) return gen_0;
    4612      498855 :   return Flx_to_int_halfspec(x, l);
    4613             : }
    4614             : 
    4615             : static GEN
    4616       53832 : kron_pack_Flx_spec(GEN x, long l) {
    4617             :   long i;
    4618             :   GEN w, y;
    4619       53832 :   if (l == 0)
    4620       10072 :     return gen_0;
    4621       43760 :   y = cgetipos(l + 2);
    4622      159479 :   for (i = 0, w = int_LSW(y); i < l; i++, w = int_nextW(w))
    4623      115719 :     *w = x[i];
    4624       43760 :   return y;
    4625             : }
    4626             : 
    4627             : static GEN
    4628        3389 : kron_pack_Flx_spec_2(GEN x, long l) { return Flx_eval2BILspec(x, 2, l); }
    4629             : 
    4630             : static GEN
    4631           0 : kron_pack_Flx_spec_3(GEN x, long l) { return Flx_eval2BILspec(x, 3, l); }
    4632             : 
    4633             : static GEN
    4634       42953 : kron_unpack_Flx(GEN z, ulong p)
    4635             : {
    4636       42953 :   long i, l = lgefint(z);
    4637       42953 :   GEN x = cgetg(l, t_VECSMALL), w;
    4638      201969 :   for (w = int_LSW(z), i = 2; i < l; w = int_nextW(w), i++)
    4639      159016 :     x[i] = ((ulong) *w) % p;
    4640       42953 :   return Flx_renormalize(x, l);
    4641             : }
    4642             : 
    4643             : static GEN
    4644        2930 : kron_unpack_Flx_2(GEN x, ulong p) {
    4645        2930 :   long d = (lgefint(x)-1)/2 - 1;
    4646        2930 :   return Z_mod2BIL_Flx_2(x, d, p);
    4647             : }
    4648             : 
    4649             : static GEN
    4650           0 : kron_unpack_Flx_3(GEN x, ulong p) {
    4651           0 :   long d = lgefint(x)/3 - 1;
    4652           0 :   return Z_mod2BIL_Flx_3(x, d, p);
    4653             : }
    4654             : 
    4655             : static GEN
    4656      119615 : FlxM_pack_ZM_bits(GEN M, long b)
    4657             : {
    4658             :   long i, j, l, lc;
    4659      119615 :   GEN N = cgetg_copy(M, &l), x;
    4660      119615 :   if (l == 1)
    4661           0 :     return N;
    4662      119615 :   lc = lgcols(M);
    4663      493928 :   for (j = 1; j < l; j++) {
    4664      374313 :     gel(N, j) = cgetg(lc, t_COL);
    4665     5988006 :     for (i = 1; i < lc; i++) {
    4666     5613693 :       x = gcoeff(M, i, j);
    4667     5613693 :       gcoeff(N, i, j) = kron_pack_Flx_spec_bits(x + 2, b, lgpol(x));
    4668             :     }
    4669             :   }
    4670      119615 :   return N;
    4671             : }
    4672             : 
    4673             : static GEN
    4674       29345 : ZM_unpack_FlxqM(GEN M, GEN T, ulong p, ulong pi, GEN (*unpack)(GEN, ulong))
    4675             : {
    4676       29345 :   long i, j, l, lc, sv = get_Flx_var(T);
    4677       29345 :   GEN N = cgetg_copy(M, &l), x;
    4678       29345 :   if (l == 1)
    4679           0 :     return N;
    4680       29345 :   lc = lgcols(M);
    4681      163939 :   for (j = 1; j < l; j++) {
    4682      134594 :     gel(N, j) = cgetg(lc, t_COL);
    4683      768988 :     for (i = 1; i < lc; i++) {
    4684      634394 :       x = unpack(gcoeff(M, i, j), p);
    4685      634394 :       x[1] = sv;
    4686      634394 :       gcoeff(N, i, j) = Flx_rem_pre(x, T, p, pi);
    4687             :     }
    4688             :   }
    4689       29345 :   return N;
    4690             : }
    4691             : 
    4692             : static GEN
    4693       59848 : ZM_unpack_FlxqM_bits(GEN M, long b, GEN T, ulong p, ulong pi)
    4694             : {
    4695       59848 :   long i, j, l, lc, sv = get_Flx_var(T);
    4696       59848 :   GEN N = cgetg_copy(M, &l), x;
    4697       59848 :   if (l == 1)
    4698           0 :     return N;
    4699       59848 :   lc = lgcols(M);
    4700       59848 :   if (b < BITS_IN_LONG) {
    4701      201798 :     for (j = 1; j < l; j++) {
    4702      143609 :       gel(N, j) = cgetg(lc, t_COL);
    4703     3237399 :       for (i = 1; i < lc; i++) {
    4704     3093790 :         x = kron_unpack_Flx_bits_narrow(gcoeff(M, i, j), b, p);
    4705     3093790 :         x[1] = sv;
    4706     3093790 :         gcoeff(N, i, j) = Flx_rem_pre(x, T, p, pi);
    4707             :       }
    4708             :     }
    4709             :   } else {
    4710        1659 :     ulong pi = get_Fl_red(p);
    4711        9796 :     for (j = 1; j < l; j++) {
    4712        8137 :       gel(N, j) = cgetg(lc, t_COL);
    4713      175265 :       for (i = 1; i < lc; i++) {
    4714      167128 :         x = kron_unpack_Flx_bits_wide(gcoeff(M, i, j), b, p, pi);
    4715      167128 :         x[1] = sv;
    4716      167128 :         gcoeff(N, i, j) = Flx_rem_pre(x, T, p, pi);
    4717             :       }
    4718             :     }
    4719             :   }
    4720       59848 :   return N;
    4721             : }
    4722             : 
    4723             : GEN
    4724       89193 : FlxqM_mul_Kronecker(GEN A, GEN B, GEN T, ulong p)
    4725             : {
    4726       89193 :   pari_sp av = avma;
    4727       89193 :   long b, d = get_Flx_degree(T), n = lg(A) - 1;
    4728             :   GEN C, D, z;
    4729             :   GEN (*pack)(GEN, long), (*unpack)(GEN, ulong);
    4730       89193 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    4731       89193 :   int is_sqr = A==B;
    4732             : 
    4733       89193 :   z = muliu(muliu(sqru(p - 1), d), n);
    4734       89193 :   b = expi(z) + 1;
    4735             :   /* only do expensive bit-packing if it saves at least 1 limb */
    4736       89193 :   if (b <= BITS_IN_HALFULONG)
    4737       84784 :   { if (nbits2nlong(d*b) == (d + 1)/2) b = BITS_IN_HALFULONG; }
    4738             :   else
    4739             :   {
    4740        4409 :     long l = lgefint(z) - 2;
    4741        4409 :     if (nbits2nlong(d*b) == d*l) b = l*BITS_IN_LONG;
    4742             :   }
    4743       89193 :   set_avma(av);
    4744             : 
    4745       89193 :   switch (b) {
    4746       28278 :   case BITS_IN_HALFULONG:
    4747       28278 :     pack = kron_pack_Flx_spec_half;
    4748       28278 :     unpack = int_to_Flx_half;
    4749       28278 :     break;
    4750        1018 :   case BITS_IN_LONG:
    4751        1018 :     pack = kron_pack_Flx_spec;
    4752        1018 :     unpack = kron_unpack_Flx;
    4753        1018 :     break;
    4754          49 :   case 2*BITS_IN_LONG:
    4755          49 :     pack = kron_pack_Flx_spec_2;
    4756          49 :     unpack = kron_unpack_Flx_2;
    4757          49 :     break;
    4758           0 :   case 3*BITS_IN_LONG:
    4759           0 :     pack = kron_pack_Flx_spec_3;
    4760           0 :     unpack = kron_unpack_Flx_3;
    4761           0 :     break;
    4762       59848 :   default:
    4763       59848 :     A = FlxM_pack_ZM_bits(A, b);
    4764       59848 :     B = is_sqr? A: FlxM_pack_ZM_bits(B, b);
    4765       59848 :     C = ZM_mul(A, B);
    4766       59848 :     D = ZM_unpack_FlxqM_bits(C, b, T, p, pi);
    4767       59848 :     return gerepilecopy(av, D);
    4768             :   }
    4769       29345 :   A = FlxM_pack_ZM(A, pack);
    4770       29345 :   B = is_sqr? A: FlxM_pack_ZM(B, pack);
    4771       29345 :   C = ZM_mul(A, B);
    4772       29345 :   D = ZM_unpack_FlxqM(C, T, p, pi, unpack);
    4773       29345 :   return gerepilecopy(av, D);
    4774             : }

Generated by: LCOV version 1.16