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 - RgX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23332-367b47754) Lines: 1459 1626 89.7 %
Date: 2018-12-10 05:41:52 Functions: 175 190 92.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /*******************************************************************/
      18             : /*                                                                 */
      19             : /*                         GENERIC                                 */
      20             : /*                                                                 */
      21             : /*******************************************************************/
      22             : 
      23             : /* Return optimal parameter l for the evaluation of n/m polynomials of degree d
      24             :    Fractional values can be used if the evaluations are done with different
      25             :    accuracies, and thus have different weights.
      26             :  */
      27             : long
      28     3352409 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     3352409 :   long pold=1, rold=n*(d-1);
      32    18945521 :   for(p=2; p<=d; p++)
      33             :   {
      34    15593112 :     r = m*(p-1) + n*((d-1)/p);
      35    15593112 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     3352409 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41     7159321 : gen_RgXQ_eval_powers(GEN P, GEN V, long a, long n, void *E, const struct bb_algebra *ff,
      42             :                                            GEN cmul(void *E, GEN P, long a, GEN x))
      43             : {
      44     7159321 :   pari_sp av = avma;
      45             :   long i;
      46     7159321 :   GEN z = cmul(E,P,a,ff->one(E));
      47     7159606 :   if (!z) z = gen_0;
      48    50440255 :   for (i=1; i<=n; i++)
      49             :   {
      50    43280960 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    43283003 :     if (t) {
      52    41491389 :       z = ff->add(E, z, t);
      53    41488966 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56     7159295 :   return ff->red(E,z);
      57             : }
      58             : 
      59             : /* Brent & Kung
      60             :  * (Fast algorithms for manipulating formal power series, JACM 25:581-595, 1978)
      61             :  *
      62             :  * V as output by FpXQ_powers(x,l,T,p). For optimal performance, l is as given
      63             :  * by brent_kung_optpow */
      64             : GEN
      65     5958781 : gen_bkeval_powers(GEN P, long d, GEN V, void *E, const struct bb_algebra *ff,
      66             :                                      GEN cmul(void *E, GEN P, long a, GEN x))
      67             : {
      68     5958781 :   pari_sp av = avma;
      69     5958781 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     5958781 :   if (d < 0) return ff->zero(E);
      73     5647391 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74      697953 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75      697953 :   if (DEBUGLEVEL>=8)
      76             :   {
      77           0 :     long cnt = 1 + (d - l) / (l-1);
      78           0 :     err_printf("RgX_RgXQV_eval(%ld/%ld): %ld RgXQ_mul\n", d, l-1, cnt);
      79             :   }
      80      697953 :   d -= l;
      81      697953 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     2209926 :   while (d >= l-1)
      83             :   {
      84      814025 :     d -= l-1;
      85      814025 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86      813974 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87      814011 :     if (gc_needed(av,2))
      88          61 :       z = gerepileupto(av, z);
      89             :   }
      90      697950 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91      697950 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92      697952 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96      451405 : gen_bkeval(GEN Q, long d, GEN x, int use_sqr, void *E, const struct bb_algebra *ff,
      97             :                                       GEN cmul(void *E, GEN P, long a, GEN x))
      98             : {
      99      451405 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102      451405 :   if (d < 0) return ff->zero(E);
     103      451006 :   rtd = (long) sqrt((double)d);
     104      451006 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105      451008 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106      451005 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      903605 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    21775627 : _gen_add(void *E, GEN x, GEN y) { (void)E; return gadd(x, y); }
     113             : static GEN
     114           0 : _gen_sub(void *E, GEN x, GEN y) { (void)E; return gsub(x, y); }
     115             : static GEN
     116      762435 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      250294 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      921455 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       15701 : _gen_zero(void *E) { (void)E; return gen_0; }
     123             : 
     124             : static struct bb_algebra Rg_algebra = { _gen_nored, _gen_add, _gen_sub,
     125             :               _gen_mul, _gen_sqr,_gen_one,_gen_zero };
     126             : 
     127             : static GEN
     128      471912 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      471912 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      144431 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      144431 :   return gen_bkeval_powers(Q, degpol(Q), x, NULL, &Rg_algebra, _gen_cmul);
     135             : }
     136             : 
     137             : GEN
     138           0 : RgX_Rg_eval_bk(GEN Q, GEN x)
     139             : {
     140           0 :   return gen_bkeval(Q, degpol(Q), x, 1, NULL, &Rg_algebra, _gen_cmul);
     141             : }
     142             : 
     143             : GEN
     144        2884 : RgXV_RgV_eval(GEN Q, GEN x)
     145             : {
     146        2884 :   long i, l = lg(Q), vQ = gvar(Q);
     147        2884 :   GEN v = cgetg(l, t_VEC);
     148      242928 :   for (i = 1; i < l; i++)
     149             :   {
     150      240044 :     GEN Qi = gel(Q, i);
     151      240044 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     152             :   }
     153        2884 :   return v;
     154             : }
     155             : 
     156             : const struct bb_algebra *
     157      115175 : get_Rg_algebra(void)
     158             : {
     159      115175 :   return &Rg_algebra;
     160             : }
     161             : 
     162             : static struct bb_ring Rg_ring = {  _gen_add, _gen_mul, _gen_sqr };
     163             : 
     164             : static GEN
     165       17675 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
     166             : {
     167             :   (void) E;
     168       17675 :   return RgX_divrem(x, y, r);
     169             : }
     170             : 
     171             : GEN
     172        4242 : RgX_digits(GEN x, GEN T)
     173             : {
     174        4242 :   pari_sp av = avma;
     175        4242 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     176        4242 :   GEN z = gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
     177        4242 :   return gerepileupto(av, z);
     178             : }
     179             : 
     180             : /*******************************************************************/
     181             : /*                                                                 */
     182             : /*                         RgX                                     */
     183             : /*                                                                 */
     184             : /*******************************************************************/
     185             : 
     186             : long
     187    24480527 : RgX_equal(GEN x, GEN y)
     188             : {
     189    24480527 :   long i = lg(x);
     190             : 
     191    24480527 :   if (i != lg(y)) return 0;
     192   102895170 :   for (i--; i > 1; i--)
     193    78541788 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194    24353382 :   return 1;
     195             : }
     196             : 
     197             : /* Returns 1 in the base ring over which x is defined */
     198             : /* HACK: this also works for t_SER */
     199             : GEN
     200     2870819 : Rg_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203     2870819 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     204     2870819 :   if (RgX_type_is_composite(tx))
     205      350364 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206     2870819 :   switch(tx)
     207             :   {
     208         301 :     case t_INTMOD: retmkintmod(is_pm1(p)? gen_0: gen_1, icopy(p));
     209           7 :     case t_PADIC: return cvtop(gen_1, p, lx);
     210         735 :     case t_FFELT: return FF_1(T);
     211     2869776 :     default: return gen_1;
     212             :   }
     213             : }
     214             : /* Returns 0 in the base ring over which x is defined */
     215             : /* HACK: this also works for t_SER */
     216             : GEN
     217     7032452 : Rg_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220     7032452 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     221     7032452 :   if (RgX_type_is_composite(tx))
     222       75173 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223     7032452 :   switch(tx)
     224             :   {
     225         322 :     case t_INTMOD: retmkintmod(gen_0, icopy(p));
     226           0 :     case t_PADIC: return cvtop(gen_0, p, lx);
     227         231 :     case t_FFELT: return FF_zero(T);
     228     7031899 :     default: return gen_0;
     229             :   }
     230             : }
     231             : 
     232             : GEN
     233        3815 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     234             : {
     235        3815 :   long i, n = degpol(P);
     236             :   GEN z, dz, dP;
     237        3815 :   if (n < 0) return gen_0;
     238        3815 :   P = Q_remove_denom(P, &dP);
     239        3815 :   z = gel(P,2); if (n == 0) return icopy(z);
     240        2107 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     241        2107 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     242        2107 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     243        2107 :   dz = mul_denom(dP, dV);
     244        2107 :   return dz? RgX_Rg_div(z, dz): z;
     245             : }
     246             : 
     247             : /* Return P(h * x), not memory clean */
     248             : GEN
     249       10675 : RgX_unscale(GEN P, GEN h)
     250             : {
     251       10675 :   long i, l = lg(P);
     252       10675 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253       10675 :   Q[1] = P[1];
     254       10675 :   if (l == 2) return Q;
     255       10647 :   gel(Q,2) = gcopy(gel(P,2));
     256       43911 :   for (i=3; i<l; i++)
     257             :   {
     258       33264 :     hi = gmul(hi,h);
     259       33264 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261       10647 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      368955 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      368955 :   long i, l = lg(P);
     268      368955 :   GEN Q = cgetg(l, t_POL);
     269      368955 :   Q[1] = P[1];
     270      368955 :   if (l == 2) return Q;
     271      368955 :   gel(Q,2) = gel(P,2);
     272      368955 :   if (l == 3) return Q;
     273      368955 :   if (h == -1)
     274      312400 :     for (i = 3; i < l; i++)
     275             :     {
     276      303167 :       gel(Q,i) = negi(gel(P,i));
     277      303167 :       if (++i == l) break;
     278      299612 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      356167 :     gel(Q,3) = mulis(gel(P,3), h);
     284      356167 :     hi = sqrs(h);
     285     1720642 :     for (i = 4; i < l; i++)
     286             :     {
     287     1364475 :       gel(Q,i) = mulii(gel(P,i), hi);
     288     1364475 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      368955 :   return Q;
     292             : }
     293             : /* P a ZX, h a t_INT. Return P(h * x), not memory clean; optimize for h = -1 */
     294             : GEN
     295        7343 : ZX_unscale(GEN P, GEN h)
     296             : {
     297             :   long i, l;
     298             :   GEN Q, hi;
     299        7343 :   i = itos_or_0(h); if (i) return ZX_z_unscale(P, i);
     300          14 :   l = lg(P); Q = cgetg(l, t_POL);
     301          14 :   Q[1] = P[1];
     302          14 :   if (l == 2) return Q;
     303          14 :   gel(Q,2) = gel(P,2);
     304          14 :   if (l == 3) return Q;
     305          14 :   hi = h;
     306          14 :   gel(Q,3) = mulii(gel(P,3), hi);
     307          91 :   for (i = 4; i < l; i++)
     308             :   {
     309          77 :     hi = mulii(hi,h);
     310          77 :     gel(Q,i) = mulii(gel(P,i), hi);
     311             :   }
     312          14 :   return Q;
     313             : }
     314             : /* P a ZX. Return P(x << n), not memory clean */
     315             : GEN
     316       61049 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       61049 :   long i, ni = n, l = lg(P);
     319       61049 :   GEN Q = cgetg(l, t_POL);
     320       61049 :   Q[1] = P[1];
     321       61049 :   if (l == 2) return Q;
     322       61049 :   gel(Q,2) = gel(P,2);
     323       61049 :   if (l == 3) return Q;
     324       61049 :   gel(Q,3) = shifti(gel(P,3), ni);
     325      236372 :   for (i=4; i<l; i++)
     326             :   {
     327      175323 :     ni += n;
     328      175323 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       61049 :   return Q;
     331             : }
     332             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     333             : GEN
     334        1190 : ZX_unscale_div(GEN P, GEN h)
     335             : {
     336        1190 :   long i, l = lg(P);
     337        1190 :   GEN hi, Q = cgetg(l, t_POL);
     338        1190 :   Q[1] = P[1];
     339        1190 :   if (l == 2) return Q;
     340        1190 :   gel(Q,2) = diviiexact(gel(P,2), h);
     341        1190 :   if (l == 3) return Q;
     342        1190 :   gel(Q,3) = gel(P,3);
     343        1190 :   if (l == 4) return Q;
     344        1190 :   hi = h;
     345        1190 :   gel(Q,4) = mulii(gel(P,4), hi);
     346        5257 :   for (i=5; i<l; i++)
     347             :   {
     348        4067 :     hi = mulii(hi,h);
     349        4067 :     gel(Q,i) = mulii(gel(P,i), hi);
     350             :   }
     351        1190 :   return Q;
     352             : }
     353             : 
     354             : GEN
     355         385 : RgXV_unscale(GEN v, GEN h)
     356             : {
     357             :   long i, l;
     358             :   GEN w;
     359         385 :   if (!h || isint1(h)) return v;
     360         308 :   w = cgetg_copy(v, &l);
     361         308 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     362         308 :   return w;
     363             : }
     364             : 
     365             : /* Return h^degpol(P) P(x / h), not memory clean */
     366             : GEN
     367        2016 : RgX_rescale(GEN P, GEN h)
     368             : {
     369        2016 :   long i, l = lg(P);
     370        2016 :   GEN Q = cgetg(l,t_POL), hi = h;
     371        2016 :   gel(Q,l-1) = gel(P,l-1);
     372        9282 :   for (i=l-2; i>=2; i--)
     373             :   {
     374        9282 :     gel(Q,i) = gmul(gel(P,i), hi);
     375        9282 :     if (i == 2) break;
     376        7266 :     hi = gmul(hi,h);
     377             :   }
     378        2016 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      140412 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      140412 :   long i,id, dy, dx = degpol(x0);
     387      140412 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       76031 :   dy = dx/d;
     389       76031 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       76031 :   z = y + 2;
     391       76031 :   x = x0+ 2;
     392       76031 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       76031 :   return y;
     394             : }
     395             : 
     396             : /* return x0(X^d) */
     397             : GEN
     398      587401 : RgX_inflate(GEN x0, long d)
     399             : {
     400      587401 :   long i, id, dy, dx = degpol(x0);
     401      587401 :   GEN x = x0 + 2, z, y;
     402      587401 :   if (dx <= 0) return leafcopy(x0);
     403      580198 :   dy = dx*d;
     404      580198 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     405      580198 :   z = y + 2;
     406      580198 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     407      580198 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     408      580198 :   return y;
     409             : }
     410             : 
     411             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     412             : GEN
     413     1373312 : RgX_translate(GEN P, GEN c)
     414             : {
     415     1373312 :   pari_sp av = avma;
     416             :   GEN Q, *R;
     417             :   long i, k, n;
     418             : 
     419     1373312 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     420     1371370 :   Q = leafcopy(P);
     421     1371370 :   R = (GEN*)(Q+2); n = degpol(P);
     422     1371370 :   if (equali1(c))
     423             :   {
     424        8862 :     for (i=1; i<=n; i++)
     425             :     {
     426        6713 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], R[k+1]);
     427        6713 :       if (gc_needed(av,2))
     428             :       {
     429           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(1), i = %ld/%ld", i,n);
     430           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     431             :       }
     432             :     }
     433             :   }
     434     1369221 :   else if (equalim1(c))
     435             :   {
     436      145183 :     for (i=1; i<=n; i++)
     437             :     {
     438      123091 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     439      123091 :       if (gc_needed(av,2))
     440             :       {
     441           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(-1), i = %ld/%ld", i,n);
     442           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     443             :       }
     444             :     }
     445             :   }
     446             :   else
     447             :   {
     448     4582126 :     for (i=1; i<=n; i++)
     449             :     {
     450     3234997 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     451     3234997 :       if (gc_needed(av,2))
     452             :       {
     453           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL, i = %ld/%ld", i,n);
     454           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     455             :       }
     456             :     }
     457             :   }
     458     1371370 :   return gerepilecopy(av, Q);
     459             : }
     460             : 
     461             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     462             : GEN
     463      389801 : ZX_translate(GEN P, GEN c)
     464             : {
     465      389801 :   pari_sp av = avma;
     466             :   GEN Q, *R;
     467             :   long i, k, n;
     468             : 
     469      389801 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     470      389766 :   Q = leafcopy(P);
     471      389766 :   R = (GEN*)(Q+2); n = degpol(P);
     472      389766 :   if (equali1(c))
     473             :   {
     474     2464829 :     for (i=1; i<=n; i++)
     475             :     {
     476     2179383 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     477     2179383 :       if (gc_needed(av,2))
     478             :       {
     479           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(1), i = %ld/%ld", i,n);
     480           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     481             :       }
     482             :     }
     483             :   }
     484      104320 :   else if (equalim1(c))
     485             :   {
     486          70 :     for (i=1; i<=n; i++)
     487             :     {
     488          49 :       for (k=n-i; k<n; k++) R[k] = subii(R[k], R[k+1]);
     489          49 :       if (gc_needed(av,2))
     490             :       {
     491           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(-1), i = %ld/%ld", i,n);
     492           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     493             :       }
     494             :     }
     495             :   }
     496             :   else
     497             :   {
     498      783174 :     for (i=1; i<=n; i++)
     499             :     {
     500      678875 :       for (k=n-i; k<n; k++) R[k] = addmulii_inplace(R[k], c, R[k+1]);
     501      678875 :       if (gc_needed(av,2))
     502             :       {
     503           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate, i = %ld/%ld", i,n);
     504           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     505             :       }
     506             :     }
     507             :   }
     508      389766 :   return gerepilecopy(av, Q);
     509             : }
     510             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     511             : GEN
     512        6454 : RgXQX_translate(GEN P, GEN c, GEN T)
     513             : {
     514        6454 :   pari_sp av = avma;
     515             :   GEN Q, *R;
     516             :   long i, k, n;
     517             : 
     518        6454 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     519        6433 :   Q = leafcopy(P);
     520        6433 :   R = (GEN*)(Q+2); n = degpol(P);
     521       38423 :   for (i=1; i<=n; i++)
     522             :   {
     523      165249 :     for (k=n-i; k<n; k++)
     524             :     {
     525      133259 :       pari_sp av2 = avma;
     526      133259 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     527             :     }
     528       31990 :     if (gc_needed(av,2))
     529             :     {
     530           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     531           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     532             :     }
     533             :   }
     534        6433 :   return gerepilecopy(av, Q);
     535             : }
     536             : 
     537             : /********************************************************************/
     538             : /**                                                                **/
     539             : /**                          CONVERSIONS                           **/
     540             : /**                       (not memory clean)                       **/
     541             : /**                                                                **/
     542             : /********************************************************************/
     543             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     544             :  * but everything else is */
     545             : static GEN
     546       24413 : QXQ_to_mod_copy(GEN x, GEN T)
     547             : {
     548             :   long d;
     549       24413 :   switch(typ(x))
     550             :   {
     551        9982 :     case t_INT:  return icopy(x);
     552         805 :     case t_FRAC: return gcopy(x);
     553             :     case t_POL:
     554       13626 :       d = degpol(x);
     555       13626 :       if (d < 0) return gen_0;
     556       13087 :       if (d == 0) return gcopy(gel(x,2));
     557       12737 :       return mkpolmod(RgX_copy(x), T);
     558           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     559             :              return NULL;/* LCOV_EXCL_LINE */
     560             :   }
     561             : }
     562             : /* pure shallow version */
     563             : static GEN
     564     1147758 : QXQ_to_mod(GEN x, GEN T)
     565             : {
     566             :   long d;
     567     1147758 :   switch(typ(x))
     568             :   {
     569             :     case t_INT:
     570      435617 :     case t_FRAC: return x;
     571             :     case t_POL:
     572      712141 :       d = degpol(x);
     573      712141 :       if (d < 0) return gen_0;
     574      537015 :       if (d == 0) return gel(x,2);
     575      516226 :       return mkpolmod(x, T);
     576           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     577             :              return NULL;/* LCOV_EXCL_LINE */
     578             :   }
     579             : }
     580             : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod_copy to all coeffs.
     581             :  * Not memory clean because T not copied, but everything else is */
     582             : static GEN
     583        3297 : QXQX_to_mod(GEN z, GEN T)
     584             : {
     585        3297 :   long i,l = lg(z);
     586        3297 :   GEN x = cgetg(l,t_POL);
     587        3297 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     588        3297 :   x[1] = z[1]; return normalizepol_lg(x,l);
     589             : }
     590             : /* pure shallow version */
     591             : GEN
     592      151325 : QXQX_to_mod_shallow(GEN z, GEN T)
     593             : {
     594      151325 :   long i,l = lg(z);
     595      151325 :   GEN x = cgetg(l,t_POL);
     596      151325 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     597      151325 :   x[1] = z[1]; return normalizepol_lg(x,l);
     598             : }
     599             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     600             : GEN
     601        1323 : QXQXV_to_mod(GEN V, GEN T)
     602             : {
     603        1323 :   long i, l = lg(V);
     604        1323 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     605        1323 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     606        1323 :   return z;
     607             : }
     608             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     609             : GEN
     610        4936 : QXQV_to_mod(GEN V, GEN T)
     611             : {
     612        4936 :   long i, l = lg(V);
     613        4936 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     614        4936 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     615        4936 :   return z;
     616             : }
     617             : 
     618             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     619             : GEN
     620       23926 : QXQC_to_mod_shallow(GEN V, GEN T)
     621             : {
     622       23926 :   long i, l = lg(V);
     623       23926 :   GEN z = cgetg(l, t_COL);
     624       23926 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     625       23926 :   return z;
     626             : }
     627             : 
     628             : GEN
     629        7476 : QXQM_to_mod_shallow(GEN V, GEN T)
     630             : {
     631        7476 :   long i, l = lg(V);
     632        7476 :   GEN z = cgetg(l, t_MAT);
     633        7476 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     634        7476 :   return z;
     635             : }
     636             : 
     637             : GEN
     638      795012 : RgX_renormalize_lg(GEN x, long lx)
     639             : {
     640             :   long i;
     641     2159509 :   for (i = lx-1; i>1; i--)
     642     2016105 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     643      795012 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     644      795012 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     645             : }
     646             : 
     647             : GEN
     648      488457 : RgV_to_RgX(GEN x, long v)
     649             : {
     650      488457 :   long i, k = lg(x);
     651             :   GEN p;
     652             : 
     653      488457 :   while (--k && gequal0(gel(x,k)));
     654      488458 :   if (!k) return pol_0(v);
     655      482655 :   i = k+2; p = cgetg(i,t_POL);
     656      482655 :   p[1] = evalsigne(1) | evalvarn(v);
     657      482655 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     658      482655 :   return p;
     659             : }
     660             : GEN
     661      158603 : RgV_to_RgX_reverse(GEN x, long v)
     662             : {
     663      158603 :   long j, k, l = lg(x);
     664             :   GEN p;
     665             : 
     666      158603 :   for (k = 1; k < l; k++)
     667      158603 :     if (!gequal0(gel(x,k))) break;
     668      158603 :   if (k == l) return pol_0(v);
     669      158603 :   k -= 1;
     670      158603 :   l -= k;
     671      158603 :   x += k;
     672      158603 :   p = cgetg(l+1,t_POL);
     673      158603 :   p[1] = evalsigne(1) | evalvarn(v);
     674      158603 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     675      158603 :   return p;
     676             : }
     677             : 
     678             : /* return the (N-dimensional) vector of coeffs of p */
     679             : GEN
     680     7017313 : RgX_to_RgC(GEN x, long N)
     681             : {
     682             :   long i, l;
     683             :   GEN z;
     684     7017313 :   l = lg(x)-1; x++;
     685     7017313 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     686     7017313 :   z = cgetg(N+1,t_COL);
     687     7017337 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     688     7017337 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     689     7017337 :   return z;
     690             : }
     691             : GEN
     692      743977 : Rg_to_RgC(GEN x, long N)
     693             : {
     694      743977 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     695             : }
     696             : 
     697             : /* vector of polynomials (in v) whose coeffs are given by the columns of x */
     698             : GEN
     699       53812 : RgM_to_RgXV(GEN x, long v)
     700       53812 : { pari_APPLY_type(t_VEC, RgV_to_RgX(gel(x,i), v)) }
     701             : 
     702             : /* matrix whose entries are given by the coeffs of the polynomials in
     703             :  * vector v (considered as degree n-1 polynomials) */
     704             : GEN
     705      132294 : RgV_to_RgM(GEN x, long n)
     706      132294 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     707             : 
     708             : GEN
     709        4488 : RgXV_to_RgM(GEN x, long n)
     710        4488 : { pari_APPLY_type(t_MAT, RgX_to_RgC(gel(x,i), n)) }
     711             : 
     712             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     713             : GEN
     714       16667 : RgM_to_RgXX(GEN x, long v,long w)
     715             : {
     716       16667 :   long j, lx = lg(x);
     717       16667 :   GEN y = cgetg(lx+1, t_POL);
     718       16667 :   y[1] = evalsigne(1) | evalvarn(v);
     719       16667 :   y++;
     720       16667 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     721       16667 :   return normalizepol_lg(--y, lx+1);
     722             : }
     723             : 
     724             : /* matrix whose entries are given by the coeffs of the polynomial v in
     725             :  * two variables (considered as degree n-1 polynomials) */
     726             : GEN
     727         308 : RgXX_to_RgM(GEN v, long n)
     728             : {
     729         308 :   long j, N = lg(v)-1;
     730         308 :   GEN y = cgetg(N, t_MAT);
     731         308 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     732         308 :   return y;
     733             : }
     734             : 
     735             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     736             : GEN
     737       19240 : RgXY_swapspec(GEN x, long n, long w, long nx)
     738             : {
     739       19240 :   long j, ly = n+3;
     740       19240 :   GEN y = cgetg(ly, t_POL);
     741       19240 :   y[1] = evalsigne(1);
     742      245996 :   for (j=2; j<ly; j++)
     743             :   {
     744             :     long k;
     745      226756 :     GEN a = cgetg(nx+2,t_POL);
     746      226756 :     a[1] = evalsigne(1) | evalvarn(w);
     747     1158842 :     for (k=0; k<nx; k++)
     748             :     {
     749      932086 :       GEN xk = gel(x,k);
     750      932086 :       if (typ(xk)==t_POL)
     751      856829 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     752             :       else
     753       75257 :         gel(a,k+2) = j==2 ? xk: gen_0;
     754             :     }
     755      226756 :     gel(y,j) = normalizepol_lg(a, nx+2);
     756             :   }
     757       19240 :   return normalizepol_lg(y,ly);
     758             : }
     759             : 
     760             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     761             : GEN
     762         952 : RgXY_swap(GEN x, long n, long w)
     763             : {
     764         952 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     765         952 :   setvarn(z, varn(x)); return z;
     766             : }
     767             : 
     768             : long
     769         176 : RgXY_degreex(GEN b)
     770             : {
     771         176 :   long deg = -1, i;
     772         176 :   if (!signe(b)) return -1;
     773         990 :   for (i = 2; i < lg(b); ++i)
     774             :   {
     775         814 :     GEN bi = gel(b, i);
     776         814 :     if (typ(bi) == t_POL)
     777         778 :       deg = maxss(deg, degpol(bi));
     778             :   }
     779         176 :   return deg;
     780             : }
     781             : 
     782             : /* return (x % X^n). Shallow */
     783             : GEN
     784     2245889 : RgXn_red_shallow(GEN a, long n)
     785             : {
     786     2245889 :   long i, L = n+2, l = lg(a);
     787             :   GEN  b;
     788     2245889 :   if (L >= l) return a; /* deg(x) < n */
     789     1548185 :   b = cgetg(L, t_POL); b[1] = a[1];
     790     1548185 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     791     1548185 :   return normalizepol_lg(b,L);
     792             : }
     793             : 
     794             : GEN
     795         357 : RgXnV_red_shallow(GEN x, long n)
     796         357 : { pari_APPLY_type(t_VEC, RgXn_red_shallow(gel(x,i), n)) }
     797             : 
     798             : /* return (x * X^n). Shallow */
     799             : GEN
     800    66805224 : RgX_shift_shallow(GEN a, long n)
     801             : {
     802    66805224 :   long i, l = lg(a);
     803             :   GEN  b;
     804    66805224 :   if (l == 2 || !n) return a;
     805    48684539 :   l += n;
     806    48684539 :   if (n < 0)
     807             :   {
     808    42790568 :     if (l <= 2) return pol_0(varn(a));
     809    42767195 :     b = cgetg(l, t_POL); b[1] = a[1];
     810    42767197 :     a -= n;
     811    42767197 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     812             :   } else {
     813     5893971 :     b = cgetg(l, t_POL); b[1] = a[1];
     814     5893971 :     a -= n; n += 2;
     815     5893971 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     816     5893971 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     817             :   }
     818    48661168 :   return b;
     819             : }
     820             : /* return (x * X^n). */
     821             : GEN
     822     3474848 : RgX_shift(GEN a, long n)
     823             : {
     824     3474848 :   long i, l = lg(a);
     825             :   GEN  b;
     826     3474848 :   if (l == 2 || !n) return RgX_copy(a);
     827     3474582 :   l += n;
     828     3474582 :   if (n < 0)
     829             :   {
     830         812 :     if (l <= 2) return pol_0(varn(a));
     831         770 :     b = cgetg(l, t_POL); b[1] = a[1];
     832         770 :     a -= n;
     833         770 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     834             :   } else {
     835     3473770 :     b = cgetg(l, t_POL); b[1] = a[1];
     836     3473770 :     a -= n; n += 2;
     837     3473770 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     838     3473770 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     839             :   }
     840     3474540 :   return b;
     841             : }
     842             : 
     843             : GEN
     844      316113 : RgX_rotate_shallow(GEN P, long k, long p)
     845             : {
     846      316113 :   long i, l = lgpol(P);
     847             :   GEN r;
     848      316113 :   if (signe(P)==0)
     849         623 :     return pol_0(varn(P));
     850      315490 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     851     2094904 :   for(i=0; i<p; i++)
     852             :   {
     853     1779414 :     long s = 2+(i+k)%p;
     854     1779414 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     855             :   }
     856      315490 :   return RgX_renormalize(r);
     857             : }
     858             : 
     859             : GEN
     860     2974912 : RgX_mulXn(GEN x, long d)
     861             : {
     862             :   pari_sp av;
     863             :   GEN z;
     864             :   long v;
     865     2974912 :   if (d >= 0) return RgX_shift(x, d);
     866     1463359 :   d = -d;
     867     1463359 :   v = RgX_val(x);
     868     1463359 :   if (v >= d) return RgX_shift(x, -d);
     869     1463345 :   av = avma;
     870     1463345 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     871     1463345 :   return gerepileupto(av, z);
     872             : }
     873             : 
     874             : long
     875     2485748 : RgX_val(GEN x)
     876             : {
     877     2485748 :   long i, lx = lg(x);
     878     2485748 :   if (lx == 2) return LONG_MAX;
     879     2518158 :   for (i = 2; i < lx; i++)
     880     2518116 :     if (!isexactzero(gel(x,i))) break;
     881     2485706 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     882     2485664 :   return i - 2;
     883             : }
     884             : long
     885    37990028 : RgX_valrem(GEN x, GEN *Z)
     886             : {
     887    37990028 :   long v, i, lx = lg(x);
     888    37990028 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     889    72346051 :   for (i = 2; i < lx; i++)
     890    72346051 :     if (!isexactzero(gel(x,i))) break;
     891             :   /* possible with non-rational zeros */
     892    37990028 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     893    37990028 :   v = i - 2;
     894    37990028 :   *Z = RgX_shift_shallow(x, -v);
     895    37990028 :   return v;
     896             : }
     897             : long
     898       14535 : RgX_valrem_inexact(GEN x, GEN *Z)
     899             : {
     900             :   long v;
     901       14535 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     902       15032 :   for (v = 0;; v++)
     903       15536 :     if (!gequal0(gel(x,2+v))) break;
     904       14528 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     905       14528 :   return v;
     906             : }
     907             : 
     908             : GEN
     909      109186 : RgXQC_red(GEN x, GEN T)
     910      109186 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
     911             : 
     912             : GEN
     913         945 : RgXQV_red(GEN x, GEN T)
     914         945 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
     915             : 
     916             : GEN
     917       14826 : RgXQM_red(GEN x, GEN T)
     918       14826 : { pari_APPLY_same(RgXQC_red(gel(x,i), T)) }
     919             : 
     920             : GEN
     921           0 : RgXQM_mul(GEN P, GEN Q, GEN T)
     922             : {
     923           0 :   return RgXQM_red(RgM_mul(P, Q), T);
     924             : }
     925             : 
     926             : GEN
     927      414452 : RgXQX_red(GEN P, GEN T)
     928             : {
     929      414452 :   long i, l = lg(P);
     930      414452 :   GEN Q = cgetg(l, t_POL);
     931      414452 :   Q[1] = P[1];
     932      414452 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     933      414452 :   return normalizepol_lg(Q, l);
     934             : }
     935             : 
     936             : GEN
     937      241348 : RgX_deriv(GEN x)
     938             : {
     939      241348 :   long i,lx = lg(x)-1;
     940             :   GEN y;
     941             : 
     942      241348 :   if (lx<3) return pol_0(varn(x));
     943      213292 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     944      213292 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     945      213292 :   y[1] = x[1]; return normalizepol_lg(y,i);
     946             : }
     947             : 
     948             : GEN
     949      478702 : RgX_recipspec_shallow(GEN x, long l, long n)
     950             : {
     951             :   long i;
     952      478702 :   GEN z = cgetg(n+2,t_POL);
     953      478705 :   z[1] = 0; z += 2;
     954    14528166 :   for(i=0; i<l; i++)
     955    14049461 :     gel(z,n-i-1) = gel(x,i);
     956      605744 :   for(   ; i<n; i++)
     957      127039 :     gel(z, n-i-1) = gen_0;
     958      478705 :   return normalizepol_lg(z-2,n+2);
     959             : }
     960             : 
     961             : GEN
     962       42170 : RgXn_recip_shallow(GEN P, long n)
     963             : {
     964       42170 :   GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
     965       42170 :   setvarn(Q, varn(P));
     966       42170 :   return Q;
     967             : }
     968             : 
     969             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     970             : GEN
     971        2170 : RgX_recip(GEN x)
     972             : {
     973             :   long lx, i, j;
     974        2170 :   GEN y = cgetg_copy(x, &lx);
     975        2170 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     976        2170 :   return normalizepol_lg(y,lx);
     977             : }
     978             : /* shallow version */
     979             : GEN
     980      386892 : RgX_recip_shallow(GEN x)
     981             : {
     982             :   long lx, i, j;
     983      386892 :   GEN y = cgetg_copy(x, &lx);
     984      386933 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     985      386933 :   return y;
     986             : }
     987             : /*******************************************************************/
     988             : /*                                                                 */
     989             : /*                      ADDITION / SUBTRACTION                     */
     990             : /*                                                                 */
     991             : /*******************************************************************/
     992             : /* same variable */
     993             : GEN
     994    20127449 : RgX_add(GEN x, GEN y)
     995             : {
     996    20127449 :   long i, lx = lg(x), ly = lg(y);
     997             :   GEN z;
     998    20127449 :   if (ly <= lx) {
     999    17863390 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1000    17863390 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1001    17863390 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1002    17863390 :     z = normalizepol_lg(z, lx);
    1003             :   } else {
    1004     2264059 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1005     2264059 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1006     2264059 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1007     2264059 :     z = normalizepol_lg(z, ly);
    1008             :   }
    1009    20127449 :   return z;
    1010             : }
    1011             : GEN
    1012    21662745 : RgX_sub(GEN x, GEN y)
    1013             : {
    1014    21662745 :   long i, lx = lg(x), ly = lg(y);
    1015             :   GEN z;
    1016    21662745 :   if (ly <= lx) {
    1017    18469420 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1018    18469420 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1019    18469420 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1020    18469420 :     z = normalizepol_lg(z, lx);
    1021             :   } else {
    1022     3193325 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1023     3193325 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1024     3193325 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1025     3193325 :     z = normalizepol_lg(z, ly);
    1026             :   }
    1027    21662745 :   return z;
    1028             : }
    1029             : GEN
    1030     1137666 : RgX_neg(GEN x)
    1031             : {
    1032     1137666 :   long i, lx = lg(x);
    1033     1137666 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1034     1137667 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1035     1137669 :   return y;
    1036             : }
    1037             : 
    1038             : GEN
    1039    12959913 : RgX_Rg_add(GEN y, GEN x)
    1040             : {
    1041             :   GEN z;
    1042    12959913 :   long lz = lg(y), i;
    1043    12959913 :   if (lz == 2) return scalarpol(x,varn(y));
    1044     9986877 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1045     9986876 :   gel(z,2) = gadd(gel(y,2),x);
    1046     9986907 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1047             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1048             :    * an inexact 0 */
    1049     9986879 :   return normalizepol_lg(z,lz);
    1050             : }
    1051             : GEN
    1052        2592 : RgX_Rg_add_shallow(GEN y, GEN x)
    1053             : {
    1054             :   GEN z;
    1055        2592 :   long lz = lg(y), i;
    1056        2592 :   if (lz == 2) return scalarpol(x,varn(y));
    1057        2592 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1058        2592 :   gel(z,2) = gadd(gel(y,2),x);
    1059        2592 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1060        2592 :   return z = normalizepol_lg(z,lz);
    1061             : }
    1062             : GEN
    1063       31439 : RgX_Rg_sub(GEN y, GEN x)
    1064             : {
    1065             :   GEN z;
    1066       31439 :   long lz = lg(y), i;
    1067       31439 :   if (lz == 2)
    1068             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1069        2576 :     long v = varn(y);
    1070        2576 :     if (isrationalzero(x)) return pol_0(v);
    1071          14 :     z = cgetg(3,t_POL);
    1072          28 :     z[1] = gequal0(x)? evalvarn(v)
    1073          14 :                    : evalvarn(v) | evalsigne(1);
    1074          14 :     gel(z,2) = gneg(x); return z;
    1075             :   }
    1076       28863 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1077       28863 :   gel(z,2) = gsub(gel(y,2),x);
    1078       28863 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1079       28863 :   return z = normalizepol_lg(z,lz);
    1080             : }
    1081             : GEN
    1082      480308 : Rg_RgX_sub(GEN x, GEN y)
    1083             : {
    1084             :   GEN z;
    1085      480308 :   long lz = lg(y), i;
    1086      480308 :   if (lz == 2) return scalarpol(x,varn(y));
    1087      479202 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1088      479202 :   gel(z,2) = gsub(x, gel(y,2));
    1089      479202 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1090      479202 :   return z = normalizepol_lg(z,lz);
    1091             : }
    1092             : /*******************************************************************/
    1093             : /*                                                                 */
    1094             : /*                  KARATSUBA MULTIPLICATION                       */
    1095             : /*                                                                 */
    1096             : /*******************************************************************/
    1097             : #if 0
    1098             : /* to debug Karatsuba-like routines */
    1099             : GEN
    1100             : zx_debug_spec(GEN x, long nx)
    1101             : {
    1102             :   GEN z = cgetg(nx+2,t_POL);
    1103             :   long i;
    1104             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1105             :   z[1] = evalsigne(1); return z;
    1106             : }
    1107             : 
    1108             : GEN
    1109             : RgX_debug_spec(GEN x, long nx)
    1110             : {
    1111             :   GEN z = cgetg(nx+2,t_POL);
    1112             :   long i;
    1113             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1114             :   z[1] = evalsigne(1); return z;
    1115             : }
    1116             : #endif
    1117             : 
    1118             : /* generic multiplication */
    1119             : GEN
    1120     3248688 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1121             : {
    1122             :   GEN z, t;
    1123             :   long i;
    1124     3248688 :   if (nx == ny) {
    1125      757608 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1126      757608 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1127      757608 :     return normalizepol_lg(z, nx+2);
    1128             :   }
    1129     2491080 :   if (ny < nx) {
    1130     2338584 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1131     2338584 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1132     2338584 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1133     2338584 :     return normalizepol_lg(z, nx+2);
    1134             :   } else {
    1135      152496 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1136      152496 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1137      152496 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1138      152496 :     return normalizepol_lg(z, ny+2);
    1139             :   }
    1140             : }
    1141             : GEN
    1142      204828 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1143             : {
    1144             :   GEN z, t;
    1145             :   long i;
    1146      204828 :   if (nx == ny) {
    1147       10423 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1148       10423 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1149       10423 :     return normalizepol_lg(z, nx+2);
    1150             :   }
    1151      194405 :   if (ny < nx) {
    1152      192704 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1153      192704 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1154      192704 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1155      192704 :     return normalizepol_lg(z, nx+2);
    1156             :   } else {
    1157        1701 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1158        1701 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1159        1701 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1160        1701 :     return normalizepol_lg(z, ny+2);
    1161             :   }
    1162             : }
    1163             : 
    1164             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1165             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1166             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1167             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1168             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1169             : static GEN
    1170    21087857 : RgXspec_kill0(GEN x, long lx)
    1171             : {
    1172    21087857 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1173             :   long i;
    1174    84700701 :   for (i=0; i <lx; i++)
    1175             :   {
    1176    63612844 :     GEN c = gel(x,i);
    1177    63612844 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1178             :   }
    1179    21087857 :   return z;
    1180             : }
    1181             : 
    1182             : INLINE GEN
    1183    46743650 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1184             : {
    1185    46743650 :   pari_sp av = avma;
    1186    46743650 :   GEN s = NULL;
    1187             :   long i;
    1188             : 
    1189   209802915 :   for (i=a; i<b; i++)
    1190   163059265 :     if (gel(y,i) && gel(x,-i))
    1191             :     {
    1192    86659334 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1193    86659334 :       s = s? gadd(s, t): t;
    1194             :     }
    1195    46743650 :   return s? gerepileupto(av, s): gen_0;
    1196             : }
    1197             : 
    1198             : /* assume nx >= ny > 0, return x * y * t^v */
    1199             : static GEN
    1200     8317046 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1201             : {
    1202             :   long i, lz, nz;
    1203             :   GEN z;
    1204             : 
    1205     8317046 :   x = RgXspec_kill0(x,nx);
    1206     8317046 :   y = RgXspec_kill0(y,ny);
    1207     8317046 :   lz = nx + ny + 1; nz = lz-2;
    1208     8317046 :   lz += v;
    1209     8317046 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1210     8317046 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1211     8317046 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1212     8317046 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1213     8317046 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1214     8317046 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1215             : }
    1216             : 
    1217             : /* return (x * X^d) + y. Assume d > 0 */
    1218             : GEN
    1219     2374273 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1220             : {
    1221             :   GEN x, y, xd, yd, zd;
    1222             :   long a, lz, nx, ny;
    1223             : 
    1224     2374273 :   if (!signe(x0)) return y0;
    1225     2349667 :   ny = lgpol(y0);
    1226     2349667 :   nx = lgpol(x0);
    1227     2349667 :   zd = (GEN)avma;
    1228     2349667 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1229     2349667 :   if (a <= 0)
    1230             :   {
    1231      187477 :     lz = nx+d+2;
    1232      187477 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1233      187477 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1234      187477 :     x = zd + a;
    1235      187477 :     while (zd > x) gel(--zd,0) = gen_0;
    1236             :   }
    1237             :   else
    1238             :   {
    1239     2162190 :     xd = new_chunk(d); yd = y+d;
    1240     2162190 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1241     2162190 :     lz = (a>nx)? ny+2: lg(x)+d;
    1242     2162190 :     x += 2; while (xd > x) *--zd = *--xd;
    1243             :   }
    1244     2349667 :   while (yd > y) *--zd = *--yd;
    1245     2349667 :   *--zd = x0[1];
    1246     2349667 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1247             : }
    1248             : GEN
    1249      496788 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1250             : {
    1251             :   GEN x, y, xd, yd, zd;
    1252             :   long a, lz, nx, ny;
    1253             : 
    1254      496788 :   if (!signe(x0)) return RgX_copy(y0);
    1255      496211 :   nx = lgpol(x0);
    1256      496211 :   ny = lgpol(y0);
    1257      496211 :   zd = (GEN)avma;
    1258      496211 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1259      496211 :   if (a <= 0)
    1260             :   {
    1261      291383 :     lz = nx+d+2;
    1262      291383 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1263      291383 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1264      291383 :     x = zd + a;
    1265      291383 :     while (zd > x) gel(--zd,0) = gen_0;
    1266             :   }
    1267             :   else
    1268             :   {
    1269      204828 :     xd = new_chunk(d); yd = y+d;
    1270      204828 :     x = RgX_addspec(x,yd, nx,a);
    1271      204828 :     lz = (a>nx)? ny+2: lg(x)+d;
    1272      204828 :     x += 2; while (xd > x) *--zd = *--xd;
    1273             :   }
    1274      496211 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1275      496211 :   *--zd = x0[1];
    1276      496211 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1277             : }
    1278             : 
    1279             : /* return x * y mod t^n */
    1280             : static GEN
    1281     2223846 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1282             : {
    1283     2223846 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1284             :   GEN z;
    1285     2223846 :   if (lx < 0) return pol_0(varn(x));
    1286     2223846 :   if (ly < 0) return pol_0(varn(x));
    1287     2223846 :   z = cgetg(lz, t_POL) + 2;
    1288     2223846 :   x+=2; if (lx > n) lx = n;
    1289     2223846 :   y+=2; if (ly > n) ly = n;
    1290     2223846 :   z[-1] = x[-1];
    1291     2223846 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1292     2223846 :   x = RgXspec_kill0(x, lx);
    1293     2223846 :   y = RgXspec_kill0(y, ly);
    1294             :   /* x:y:z [i] = term of degree i */
    1295     2223846 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1296     2223846 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1297     2223846 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1298     2223846 :   return normalizepol_lg(z - 2, lz);
    1299             : }
    1300             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1301             : static GEN
    1302     2309407 : RgXn_mul2(GEN f, GEN g, long n)
    1303             : {
    1304     2309407 :   pari_sp av = avma;
    1305             :   GEN fe,fo, ge,go, l,h,m;
    1306             :   long n0, n1;
    1307     2309407 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1308     2249760 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1309       25914 :   n0 = n>>1; n1 = n-n0;
    1310       25914 :   RgX_even_odd(f, &fe, &fo);
    1311       25914 :   RgX_even_odd(g, &ge, &go);
    1312       25914 :   l = RgXn_mul(fe,ge,n1);
    1313       25914 :   h = RgXn_mul(fo,go,n0);
    1314       25914 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1315             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1316             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1317       25914 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1318             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1319       25914 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1320       25914 :   m = RgX_inflate(m,2);
    1321             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1322       25914 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1323       25914 :   h = RgX_inflate(h,2);
    1324       25914 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1325       25914 :   return gerepileupto(av, h);
    1326             : }
    1327             : /* (f*g) \/ x^n */
    1328             : static GEN
    1329        2470 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1330             : {
    1331        2470 :   long d = degpol(f)+degpol(g) + 1 - n;
    1332             :   GEN h;
    1333        2470 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1334        1669 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1335             :                                  RgX_recip_shallow(g), d));
    1336        1669 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1337             : }
    1338             : 
    1339             : /* (f*g) \/ x^n */
    1340             : static GEN
    1341           0 : RgX_sqrhigh_i2(GEN f, long n)
    1342             : {
    1343           0 :   long d = 2*degpol(f)+ 1 - n;
    1344             :   GEN h;
    1345           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1346           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1347           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1348             : }
    1349             : 
    1350             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1351             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1352             :  * Only c, c0, c1, c2 are genuine GEN.
    1353             :  */
    1354             : GEN
    1355     8888167 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1356             : {
    1357             :   GEN a0, c, c0;
    1358     8888167 :   long n0, n0a, i, v = 0;
    1359             :   pari_sp av;
    1360             : 
    1361     8888167 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1362     8888167 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1363     8888167 :   if (na < nb) swapspec(a,b, na,nb);
    1364     8888167 :   if (!nb) return pol_0(0);
    1365             : 
    1366     8787881 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1367      470835 :   RgX_shift_inplace_init(v);
    1368      470835 :   i = (na>>1); n0 = na-i; na = i;
    1369      470835 :   av = avma; a0 = a+n0; n0a = n0;
    1370      470835 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1371             : 
    1372      470835 :   if (nb > n0)
    1373             :   {
    1374             :     GEN b0,c1,c2;
    1375             :     long n0b;
    1376             : 
    1377      469511 :     nb -= n0; b0 = b+n0; n0b = n0;
    1378      469511 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1379      469511 :     c = RgX_mulspec(a,b,n0a,n0b);
    1380      469511 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1381             : 
    1382      469511 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1383      469511 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1384             : 
    1385      469511 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1386      469511 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1387      469511 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1388             :   }
    1389             :   else
    1390             :   {
    1391        1324 :     c = RgX_mulspec(a,b,n0a,nb);
    1392        1324 :     c0 = RgX_mulspec(a0,b,na,nb);
    1393             :   }
    1394      470835 :   c0 = RgX_addmulXn(c0,c,n0);
    1395      470835 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1396             : }
    1397             : 
    1398             : INLINE GEN
    1399       33673 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1400             : {
    1401       33673 :   pari_sp av = avma;
    1402       33673 :   GEN s = NULL;
    1403       33673 :   long j, l = (i+1)>>1;
    1404       99505 :   for (j=a; j<l; j++)
    1405             :   {
    1406       65832 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1407       65832 :     if (xj && xx)
    1408             :     {
    1409       58510 :       GEN t = gmul(xj, xx);
    1410       58510 :       s = s? gadd(s, t): t;
    1411             :     }
    1412             :   }
    1413       33673 :   if (s) s = gshift(s,1);
    1414       33673 :   if ((i&1) == 0)
    1415             :   {
    1416       19873 :     GEN t = gel(x, i>>1);
    1417       19873 :     if (t) {
    1418       17556 :       t = gsqr(t);
    1419       17556 :       s = s? gadd(s, t): t;
    1420             :     }
    1421             :   }
    1422       33673 :   return s? gerepileupto(av,s): gen_0;
    1423             : }
    1424             : static GEN
    1425        6073 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1426             : {
    1427             :   long i, lz, nz;
    1428             :   GEN z;
    1429             : 
    1430        6073 :   if (!nx) return pol_0(0);
    1431        6073 :   x = RgXspec_kill0(x,nx);
    1432        6073 :   lz = (nx << 1) + 1, nz = lz-2;
    1433        6073 :   lz += v;
    1434        6073 :   z = cgetg(lz,t_POL) + 2;
    1435        6073 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1436        6073 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1437        6073 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1438        6073 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1439             : }
    1440             : /* return x^2 mod t^n */
    1441             : static GEN
    1442           0 : RgXn_sqr_basecase(GEN x, long n)
    1443             : {
    1444           0 :   long i, lz = n+2, lx = lgpol(x);
    1445             :   GEN z;
    1446           0 :   if (lx < 0) return pol_0(varn(x));
    1447           0 :   z = cgetg(lz, t_POL);
    1448           0 :   z[1] = x[1];
    1449           0 :   x+=2; if (lx > n) lx = n;
    1450           0 :   x = RgXspec_kill0(x,lx);
    1451           0 :   z+=2;/* x:z [i] = term of degree i */
    1452           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1453           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1454           0 :   z -= 2; return normalizepol_lg(z, lz);
    1455             : }
    1456             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1457             : static GEN
    1458         238 : RgXn_sqr2(GEN f, long n)
    1459             : {
    1460         238 :   pari_sp av = avma;
    1461             :   GEN fe,fo, l,h,m;
    1462             :   long n0, n1;
    1463         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1464           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1465           0 :   n0 = n>>1; n1 = n-n0;
    1466           0 :   RgX_even_odd(f, &fe, &fo);
    1467           0 :   l = RgXn_sqr(fe,n1);
    1468           0 :   h = RgXn_sqr(fo,n0);
    1469           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1470             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1471             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1472           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1473             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1474           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1475           0 :   m = RgX_inflate(m,2);
    1476             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1477           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1478           0 :   h = RgX_inflate(h,2);
    1479           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1480           0 :   return gerepileupto(av, h);
    1481             : }
    1482             : GEN
    1483        6112 : RgX_sqrspec(GEN a, long na)
    1484             : {
    1485             :   GEN a0, c, c0, c1;
    1486        6112 :   long n0, n0a, i, v = 0;
    1487             :   pari_sp av;
    1488             : 
    1489        6112 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1490        6112 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1491          39 :   RgX_shift_inplace_init(v);
    1492          39 :   i = (na>>1); n0 = na-i; na = i;
    1493          39 :   av = avma; a0 = a+n0; n0a = n0;
    1494          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1495             : 
    1496          39 :   c = RgX_sqrspec(a,n0a);
    1497          39 :   c0 = RgX_sqrspec(a0,na);
    1498          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1499          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1500          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1501          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1502             : }
    1503             : 
    1504             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1505             : GEN
    1506      713120 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1507             : {
    1508      713120 :   GEN z = RgX_mul(A, B);
    1509      713120 :   if (a < b)
    1510        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1511      707674 :   else if (a > b)
    1512      441425 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1513             :   else
    1514      266249 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1515      713120 :   return z;
    1516             : }
    1517             : 
    1518             : GEN
    1519     7476947 : RgX_mul_i(GEN x, GEN y)
    1520             : {
    1521     7476947 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1522     7476947 :   setvarn(z, varn(x)); return z;
    1523             : }
    1524             : 
    1525             : GEN
    1526        6034 : RgX_sqr_i(GEN x)
    1527             : {
    1528        6034 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1529        6034 :   setvarn(z,varn(x)); return z;
    1530             : }
    1531             : 
    1532             : /*******************************************************************/
    1533             : /*                                                                 */
    1534             : /*                               DIVISION                          */
    1535             : /*                                                                 */
    1536             : /*******************************************************************/
    1537             : GEN
    1538      442053 : RgX_Rg_divexact(GEN x, GEN y) {
    1539             :   long i, lx;
    1540             :   GEN z;
    1541      442053 :   if (typ(y) == t_INT && is_pm1(y))
    1542       28850 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1543      413203 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1544      413203 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1545      413203 :   return z;
    1546             : }
    1547             : GEN
    1548    24038674 : RgX_Rg_div(GEN x, GEN y) {
    1549             :   long i, lx;
    1550    24038674 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1551    24038674 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1552    24038674 :   return normalizepol_lg(z, lx);
    1553             : }
    1554             : GEN
    1555       12726 : RgX_normalize(GEN x)
    1556             : {
    1557       12726 :   GEN z, d = NULL;
    1558       12726 :   long i, n = lg(x)-1;
    1559       12726 :   for (i = n; i > 1; i--) { d = gel(x,i); if (!gequal0(d)) break; }
    1560       12726 :   if (i == 1) return pol_0(varn(x));
    1561       12726 :   if (i == n && isint1(d)) return x;
    1562        5719 :   n = i; z = cgetg(n+1, t_POL); z[1] = x[1];
    1563        5719 :   for (i=2; i<n; i++) gel(z,i) = gdiv(gel(x,i),d);
    1564        5719 :   gel(z,n) = Rg_get_1(d); return z;
    1565             : }
    1566             : GEN
    1567        2597 : RgX_divs(GEN x, long y) {
    1568             :   long i, lx;
    1569        2597 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1570        2597 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1571        2597 :   return normalizepol_lg(z, lx);
    1572             : }
    1573             : GEN
    1574       42075 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1575             : {
    1576       42075 :   long l = lg(a), i;
    1577       42075 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1578       42075 :   z[1] = a[1];
    1579       42075 :   a0 = a + l-1;
    1580       42075 :   z0 = z + l-2; *z0 = *a0--;
    1581      798367 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1582             :   {
    1583      756292 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1584      756292 :     gel(z0,0) = t;
    1585             :   }
    1586       42075 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1587       42075 :   return z;
    1588             : }
    1589             : /* Polynomial division x / y:
    1590             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1591             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1592             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1593             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1594             : static GEN
    1595    12368484 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1596             : {
    1597             :   pari_sp avy, av, av1;
    1598             :   long dx,dy,dz,i,j,sx,lr;
    1599             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1600             :   GEN (*f)(GEN,GEN);
    1601             : 
    1602    12368484 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1603             : 
    1604    12368484 :   dy = degpol(y);
    1605    12368484 :   y_lead = gel(y,dy+2);
    1606    12368484 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1607             :   {
    1608           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1609           0 :     for (dy--; dy>=0; dy--)
    1610             :     {
    1611           0 :       y_lead = gel(y,dy+2);
    1612           0 :       if (!gequal0(y_lead)) break;
    1613             :     }
    1614             :   }
    1615    12368484 :   if (!dy) /* y is constant */
    1616             :   {
    1617       53792 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1618       53792 :     z = RgX_Rg_div(x, y_lead);
    1619       53792 :     if (pr == ONLY_DIVIDES) return z;
    1620       52042 :     if (pr) *pr = pol_0(varn(x));
    1621       52042 :     return z;
    1622             :   }
    1623    12314692 :   dx = degpol(x);
    1624    12314692 :   if (dx < dy)
    1625             :   {
    1626      644084 :     if (pr == ONLY_REM) return RgX_copy(x);
    1627      371698 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1628      371677 :     z = pol_0(varn(x));
    1629      371677 :     if (pr) *pr = RgX_copy(x);
    1630      371677 :     return z;
    1631             :   }
    1632             : 
    1633             :   /* x,y in R[X], y non constant */
    1634    11670608 :   av = avma;
    1635    11670608 :   p = NULL;
    1636    11670608 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1637             :   {
    1638      225848 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1639      225848 :     if (!z) return gc_NULL(av);
    1640      225848 :     z = FpX_to_mod(z, p);
    1641      225848 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1642      121681 :       return gerepileupto(av, z);
    1643      104167 :     *pr = FpX_to_mod(*pr, p);
    1644      104167 :     gerepileall(av, 2, pr, &z);
    1645      104167 :     return z;
    1646             :   }
    1647    11444760 :   switch(typ(y_lead))
    1648             :   {
    1649             :     case t_REAL:
    1650           0 :       y_lead = ginv(y_lead);
    1651           0 :       f = gmul; mod = NULL;
    1652           0 :       break;
    1653             :     case t_INTMOD:
    1654        1833 :     case t_POLMOD: y_lead = ginv(y_lead);
    1655        1833 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1656        1833 :       break;
    1657    11442927 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1658    11442927 :       f = gdiv; mod = NULL;
    1659             :   }
    1660             : 
    1661    11444760 :   if (y_lead == NULL)
    1662     9664065 :     p2 = gel(x,dx+2);
    1663             :   else {
    1664             :     for(;;) {
    1665     1780695 :       p2 = f(gel(x,dx+2),y_lead);
    1666     1780695 :       p2 = simplify_shallow(p2);
    1667     1780695 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1668             :     }
    1669     1780695 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1670             :     {
    1671           0 :       if (pr == ONLY_DIVIDES) {
    1672           0 :         set_avma(av);
    1673           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1674             :       }
    1675           0 :       if (pr == ONLY_REM)
    1676             :       {
    1677           0 :         if (dx < 0)
    1678           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1679             :         else
    1680             :         {
    1681             :           GEN t;
    1682           0 :           set_avma(av);
    1683           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1684           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1685           0 :           return t;
    1686             :         }
    1687             :       }
    1688           0 :       if (pr) /* cf ONLY_REM above */
    1689             :       {
    1690           0 :         if (dx < 0)
    1691             :         {
    1692           0 :           p2 = gclone(p2);
    1693           0 :           set_avma(av);
    1694           0 :           z = pol_0(varn(x));
    1695           0 :           x = scalarpol(p2, varn(x));
    1696           0 :           gunclone(p2);
    1697             :         }
    1698             :         else
    1699             :         {
    1700             :           GEN t;
    1701           0 :           set_avma(av);
    1702           0 :           z = pol_0(varn(x));
    1703           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1704           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1705           0 :           x = t;
    1706             :         }
    1707           0 :         *pr = x;
    1708             :       }
    1709             :       else
    1710             :       {
    1711           0 :         set_avma(av);
    1712           0 :         z = pol_0(varn(x));
    1713             :       }
    1714           0 :       return z;
    1715             :     }
    1716             :   }
    1717             :   /* dx >= dy */
    1718    11444760 :   avy = avma;
    1719    11444760 :   dz = dx-dy;
    1720    11444760 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1721    11444760 :   x += 2;
    1722    11444760 :   z += 2;
    1723    11444760 :   y += 2;
    1724    11444760 :   gel(z,dz) = gcopy(p2);
    1725             : 
    1726    28418187 :   for (i=dx-1; i>=dy; i--)
    1727             :   {
    1728    16973427 :     av1=avma; p1=gel(x,i);
    1729    16973427 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1730    16973427 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1731             : 
    1732    16973427 :     if (isrationalzero(p1)) { set_avma(av1); p1 = gen_0; }
    1733             :     else
    1734     7422231 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1735    16973427 :     gel(z,i-dy) = p1;
    1736             :   }
    1737    11444760 :   if (!pr) return gerepileupto(av,z-2);
    1738             : 
    1739     4758518 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1740     5104899 :   for (sx=0; ; i--)
    1741             :   {
    1742     5451280 :     p1 = gel(x,i);
    1743             :     /* we always enter this loop at least once */
    1744     5104899 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1745     5104899 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1746     5104899 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1747     3414936 :     if (!isexactzero(p1)) break;
    1748     3401773 :     if (!i) break;
    1749      346381 :     set_avma(av1);
    1750             :   }
    1751     4758518 :   if (pr == ONLY_DIVIDES)
    1752             :   {
    1753        1568 :     if (sx) return gc_NULL(av);
    1754        1561 :     set_avma((pari_sp)rem); return gerepileupto(av,z-2);
    1755             :   }
    1756     4756950 :   lr=i+3; rem -= lr;
    1757     4756950 :   if (avma==av1) { set_avma((pari_sp)rem); p1 = gcopy(p1); }
    1758     4713800 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1759     4756950 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1760     4756950 :   rem[1] = z[-1];
    1761     4756950 :   rem += 2;
    1762     4756950 :   gel(rem,i) = p1;
    1763     5940336 :   for (i--; i>=0; i--)
    1764             :   {
    1765     1183386 :     av1=avma; p1 = gel(x,i);
    1766     1183386 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1767     1183386 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1768     1183386 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1769             :   }
    1770     4756950 :   rem -= 2;
    1771     4756950 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1772     4756950 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1773     4078393 :   z -= 2;
    1774             :   {
    1775     4078393 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1776     4078393 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1777             :   }
    1778             : }
    1779             : 
    1780             : GEN
    1781    11417541 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1782             : {
    1783    11417541 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1784    11417541 :   return RgX_divrem_i(x, y, pr);
    1785             : }
    1786             : 
    1787             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1788             : GEN
    1789       72115 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1790             : {
    1791             :   long vx, dx, dy, dz, i, j, sx, lr;
    1792             :   pari_sp av0, av, tetpil;
    1793             :   GEN z,p1,rem,lead;
    1794             : 
    1795       72115 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1796       72115 :   vx = varn(x);
    1797       72115 :   dx = degpol(x);
    1798       72115 :   dy = degpol(y);
    1799       72115 :   if (dx < dy)
    1800             :   {
    1801       23086 :     if (pr)
    1802             :     {
    1803       23086 :       av0 = avma; x = RgXQX_red(x, T);
    1804       23086 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: gen_0; }
    1805       23086 :       if (pr == ONLY_REM) return x;
    1806           0 :       *pr = x;
    1807             :     }
    1808           0 :     return pol_0(vx);
    1809             :   }
    1810       49029 :   lead = leading_coeff(y);
    1811       49029 :   if (!dy) /* y is constant */
    1812             :   {
    1813         546 :     if (pr && pr != ONLY_DIVIDES)
    1814             :     {
    1815           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1816           0 :       *pr = pol_0(vx);
    1817             :     }
    1818         546 :     if (gequal1(lead)) return RgX_copy(x);
    1819           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1820           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1821             :   }
    1822       48483 :   av0 = avma; dz = dx-dy;
    1823       48483 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1824       48483 :   set_avma(av0);
    1825       48483 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1826       48483 :   x += 2; y += 2; z += 2;
    1827             : 
    1828       48483 :   p1 = gel(x,dx); av = avma;
    1829       48483 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1830      289387 :   for (i=dx-1; i>=dy; i--)
    1831             :   {
    1832      240904 :     av=avma; p1=gel(x,i);
    1833      240904 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1834      240904 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1835      240904 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1836             :   }
    1837       48483 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1838             : 
    1839       47237 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1840       76027 :   for (sx=0; ; i--)
    1841             :   {
    1842      104817 :     p1 = gel(x,i);
    1843       76027 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1844       76027 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1845       33846 :     if (!i) break;
    1846       28790 :     set_avma(av);
    1847             :   }
    1848       47237 :   if (pr == ONLY_DIVIDES)
    1849             :   {
    1850        2340 :     if (lead) gunclone(lead);
    1851        2340 :     if (sx) return gc_NULL(av0);
    1852        2242 :     set_avma((pari_sp)rem); return z-2;
    1853             :   }
    1854       44897 :   lr=i+3; rem -= lr;
    1855       44897 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1856       44897 :   rem[1] = z[-1];
    1857       44897 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1858       44897 :   rem += 2; gel(rem,i) = p1;
    1859      118375 :   for (i--; i>=0; i--)
    1860             :   {
    1861       73478 :     av=avma; p1 = gel(x,i);
    1862      272847 :     for (j=0; j<=i && j<=dz; j++)
    1863      199369 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1864       73478 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1865             :   }
    1866       44897 :   rem -= 2;
    1867       44897 :   if (lead) gunclone(lead);
    1868       44897 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1869       44897 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1870          98 :   *pr = rem; return z-2;
    1871             : }
    1872             : 
    1873             : /*******************************************************************/
    1874             : /*                                                                 */
    1875             : /*                        PSEUDO-DIVISION                          */
    1876             : /*                                                                 */
    1877             : /*******************************************************************/
    1878             : INLINE GEN
    1879      474054 : rem(GEN c, GEN T)
    1880             : {
    1881      474054 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1882      474054 :   return c;
    1883             : }
    1884             : 
    1885             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1886             : int
    1887        6745 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1888             : {
    1889             :   long dx, dy, dz, i, p, T_ismonic;
    1890        6745 :   pari_sp av = avma, av2;
    1891             :   GEN y_lead;
    1892             : 
    1893        6745 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1894        6745 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1895        6745 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1896             :   /* if monic, no point in using pseudo-division */
    1897        6745 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1898        4015 :   T_ismonic = gequal1(leading_coeff(T));
    1899        4015 :   dx = degpol(x);
    1900        4015 :   if (dx < dy) return !signe(x);
    1901        4015 :   (void)new_chunk(2);
    1902        4015 :   x = RgX_recip_shallow(x)+2;
    1903        4015 :   y = RgX_recip_shallow(y)+2;
    1904             :   /* pay attention to sparse divisors */
    1905        8212 :   for (i = 1; i <= dy; i++)
    1906        4197 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1907        4015 :   dz = dx-dy; p = dz+1;
    1908        4015 :   av2 = avma;
    1909             :   for (;;)
    1910       30634 :   {
    1911       34649 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1912       34649 :     x0 = gneg(x0); p--;
    1913       34649 :     m = gcdii(cx, y0);
    1914       34649 :     if (!equali1(m))
    1915             :     {
    1916       33599 :       x0 = gdiv(x0, m);
    1917       33599 :       y0 = diviiexact(y0, m);
    1918       33599 :       if (equali1(y0)) y0 = NULL;
    1919             :     }
    1920       70488 :     for (i=1; i<=dy; i++)
    1921             :     {
    1922       35839 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1923       35839 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1924       35839 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1925       35839 :       gel(x,i) = c;
    1926             :     }
    1927      399942 :     for (   ; i<=dx; i++)
    1928             :     {
    1929      365293 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1930      365293 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1931      365293 :       gel(x,i) = c;
    1932             :     }
    1933       38657 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1934       34649 :     if (dx < dy) break;
    1935       30634 :     if (gc_needed(av2,1))
    1936             :     {
    1937           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1938           0 :       gerepilecoeffs(av2,x,dx+1);
    1939             :     }
    1940             :   }
    1941        4015 :   return gc_bool(av, dx < 0);
    1942             : }
    1943             : 
    1944             : /* T either NULL or a t_POL. */
    1945             : GEN
    1946       27768 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1947             : {
    1948       27768 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1949       27768 :   pari_sp av = avma, av2;
    1950             :   GEN y_lead;
    1951             : 
    1952       27768 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1953       27768 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1954             :   /* if monic, no point in using pseudo-division */
    1955       27768 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1956       24695 :   dx = degpol(x);
    1957       24695 :   if (dx < dy) return RgX_copy(x);
    1958       24695 :   (void)new_chunk(2);
    1959       24695 :   x = RgX_recip_shallow(x)+2;
    1960       24695 :   y = RgX_recip_shallow(y)+2;
    1961             :   /* pay attention to sparse divisors */
    1962       75960 :   for (i = 1; i <= dy; i++)
    1963       51265 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1964       24695 :   dz = dx-dy; p = dz+1;
    1965       24695 :   av2 = avma;
    1966             :   for (;;)
    1967             :   {
    1968      151229 :     gel(x,0) = gneg(gel(x,0)); p--;
    1969      279598 :     for (i=1; i<=dy; i++)
    1970             :     {
    1971      191636 :       GEN c = gmul(y_lead, gel(x,i));
    1972      191636 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1973      191636 :       gel(x,i) = rem(c, T);
    1974             :     }
    1975      301956 :     for (   ; i<=dx; i++)
    1976             :     {
    1977      213994 :       GEN c = gmul(y_lead, gel(x,i));
    1978      213994 :       gel(x,i) = rem(c, T);
    1979             :     }
    1980       97324 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1981       87962 :     if (dx < dy) break;
    1982       63267 :     if (gc_needed(av2,1))
    1983             :     {
    1984           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1985           0 :       gerepilecoeffs(av2,x,dx+1);
    1986             :     }
    1987             :   }
    1988       24695 :   if (dx < 0) return pol_0(vx);
    1989       22749 :   lx = dx+3; x -= 2;
    1990       22749 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1991       22749 :   x[1] = evalsigne(1) | evalvarn(vx);
    1992       22749 :   x = RgX_recip_shallow(x);
    1993       22749 :   if (p)
    1994             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1995        2563 :     GEN t = y_lead;
    1996        2563 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1997           0 :       t = RgXQ_powu(t, p, T);
    1998             :     else
    1999        2563 :       t = gpowgs(t, p);
    2000        7012 :     for (i=2; i<lx; i++)
    2001             :     {
    2002        4449 :       GEN c = gmul(gel(x,i), t);
    2003        4449 :       gel(x,i) = rem(c,T);
    2004             :     }
    2005        2563 :     if (!T) return gerepileupto(av, x);
    2006             :   }
    2007       20186 :   return gerepilecopy(av, x);
    2008             : }
    2009             : 
    2010             : GEN
    2011       27768 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2012             : 
    2013             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2014             : GEN
    2015        5543 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2016             : {
    2017        5543 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2018        5543 :   pari_sp av = avma, av2;
    2019             :   GEN z, r, ypow, y_lead;
    2020             : 
    2021        5543 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2022        5543 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2023        5543 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2024        5025 :   dx = degpol(x);
    2025        5025 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2026        5025 :   if (dx == dy)
    2027             :   {
    2028          70 :     GEN x_lead = gel(x,lg(x)-1);
    2029          70 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2030          70 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2031          70 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2032          70 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2033             :   }
    2034        4955 :   (void)new_chunk(2);
    2035        4955 :   x = RgX_recip_shallow(x)+2;
    2036        4955 :   y = RgX_recip_shallow(y)+2;
    2037             :   /* pay attention to sparse divisors */
    2038       24343 :   for (i = 1; i <= dy; i++)
    2039       19388 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2040        4955 :   dz = dx-dy; p = dz+1;
    2041        4955 :   lz = dz+3;
    2042        4955 :   z = cgetg(lz, t_POL);
    2043        4955 :   z[1] = evalsigne(1) | evalvarn(vx);
    2044        4955 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2045        4955 :   ypow = new_chunk(dz+1);
    2046        4955 :   gel(ypow,0) = gen_1;
    2047        4955 :   gel(ypow,1) = y_lead;
    2048        8357 :   for (i=2; i<=dz; i++)
    2049             :   {
    2050        3402 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2051        3402 :     gel(ypow,i) = rem(c,T);
    2052             :   }
    2053        4955 :   av2 = avma;
    2054        4955 :   for (iz=2;;)
    2055             :   {
    2056       15981 :     p--;
    2057       10468 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2058       46071 :     for (i=1; i<=dy; i++)
    2059             :     {
    2060       35603 :       GEN c = gmul(y_lead, gel(x,i));
    2061       35603 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2062       35603 :       gel(x,i) = rem(c, T);
    2063             :     }
    2064       24970 :     for (   ; i<=dx; i++)
    2065             :     {
    2066       14502 :       GEN c = gmul(y_lead, gel(x,i));
    2067       14502 :       gel(x,i) = rem(c,T);
    2068             :     }
    2069       10468 :     x++; dx--;
    2070       10468 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2071       10468 :     if (dx < dy) break;
    2072        5513 :     if (gc_needed(av2,1))
    2073             :     {
    2074           0 :       GEN X = x-2;
    2075           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2076           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2077           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2078             :     }
    2079             :   }
    2080        4955 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2081        4955 :   if (dx < 0)
    2082          98 :     x = pol_0(vx);
    2083             :   else
    2084             :   {
    2085        4857 :     lx = dx+3; x -= 2;
    2086        4857 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2087        4857 :     x[1] = evalsigne(1) | evalvarn(vx);
    2088        4857 :     x = RgX_recip_shallow(x);
    2089             :   }
    2090        4955 :   z = RgX_recip_shallow(z);
    2091        4955 :   r = x;
    2092        4955 :   if (p)
    2093             :   {
    2094        1737 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2095        1737 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2096             :   }
    2097        4955 :   gerepileall(av, 2, &z, &r);
    2098        4955 :   *ptr = r; return z;
    2099             : }
    2100             : GEN
    2101        5368 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2102        5368 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2103             : 
    2104             : GEN
    2105       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2106             : {
    2107       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2108             : }
    2109             : GEN
    2110    69956875 : RgX_Rg_mul(GEN y, GEN x) {
    2111             :   long i, ly;
    2112    69956875 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2113    69956875 :   if (ly == 2) return z;
    2114    69871944 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2115    69871937 :   return normalizepol_lg(z,ly);
    2116             : }
    2117             : GEN
    2118       14504 : RgX_muls(GEN y, long x) {
    2119             :   long i, ly;
    2120       14504 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2121       14504 :   if (ly == 2) return z;
    2122       14420 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2123       14420 :   return normalizepol_lg(z,ly);
    2124             : }
    2125             : GEN
    2126        4613 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2127             : {
    2128        4613 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2129             : }
    2130             : GEN
    2131          77 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2132             : {
    2133          77 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2134             : }
    2135             : 
    2136             : GEN
    2137        1792 : RgXQX_sqr(GEN x, GEN T)
    2138             : {
    2139        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2140             : }
    2141             : 
    2142             : GEN
    2143         448 : RgXQX_powers(GEN P, long n, GEN T)
    2144             : {
    2145         448 :   GEN v = cgetg(n+2, t_VEC);
    2146             :   long i;
    2147         448 :   gel(v, 1) = pol_1(varn(T));
    2148         448 :   if (n==0) return v;
    2149         448 :   gel(v, 2) = gcopy(P);
    2150         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2151         448 :   return v;
    2152             : }
    2153             : 
    2154             : static GEN
    2155      125281 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2156             : static GEN
    2157           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2158             : static GEN
    2159       55040 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2160             : static GEN
    2161       40116 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2162             : static GEN
    2163      228061 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2164             : static GEN
    2165      223772 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2166             : static GEN
    2167         294 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2168             : static GEN
    2169      131002 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2170             : 
    2171             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2172             :               _mul, _sqr, _one, _zero };
    2173             : 
    2174             : GEN
    2175           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2176             : {
    2177           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2178             : }
    2179             : 
    2180             : GEN
    2181      102353 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2182             : {
    2183      102353 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2184      102353 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2185             : }
    2186             : 
    2187             : /* mod X^n */
    2188             : struct modXn {
    2189             :   long v; /* varn(X) */
    2190             :   long n;
    2191             : } ;
    2192             : static GEN
    2193        2254 : _sqrXn(void *data, GEN x) {
    2194        2254 :   struct modXn *S = (struct modXn*)data;
    2195        2254 :   return RgXn_sqr(x, S->n);
    2196             : }
    2197             : static GEN
    2198        1295 : _mulXn(void *data, GEN x, GEN y) {
    2199        1295 :   struct modXn *S = (struct modXn*)data;
    2200        1295 :   return RgXn_mul(x,y, S->n);
    2201             : }
    2202             : static GEN
    2203        1477 : _oneXn(void *data) {
    2204        1477 :   struct modXn *S = (struct modXn*)data;
    2205        1477 :   return pol_1(S->v);
    2206             : }
    2207             : static GEN
    2208           0 : _zeroXn(void *data) {
    2209           0 :   struct modXn *S = (struct modXn*)data;
    2210           0 :   return pol_0(S->v);
    2211             : }
    2212             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2213             :                                           _oneXn, _zeroXn };
    2214             : 
    2215             : GEN
    2216         357 : RgXn_powers(GEN x, long m, long n)
    2217             : {
    2218         357 :   long d = degpol(x);
    2219         357 :   int use_sqr = (d<<1) >= n;
    2220             :   struct modXn S;
    2221         357 :   S.v = varn(x); S.n = n;
    2222         357 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2223             : }
    2224             : 
    2225             : GEN
    2226        1834 : RgXn_powu_i(GEN x, ulong m, long n)
    2227             : {
    2228             :   struct modXn S;
    2229        1834 :   S.v = varn(x); S.n = n;
    2230        1834 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2231             : }
    2232             : GEN
    2233           0 : RgXn_powu(GEN x, ulong m, long n)
    2234             : {
    2235             :   struct modXn S;
    2236           0 :   S.v = varn(x); S.n = n;
    2237           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2238             : }
    2239             : 
    2240             : GEN
    2241         714 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2242             : {
    2243             :   struct modXn S;
    2244         714 :   S.v = varn(gel(x,2)); S.n = n;
    2245         714 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2246             : }
    2247             : 
    2248             : GEN
    2249           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2250             : {
    2251           0 :   int use_sqr = 2*degpol(x) >= n;
    2252             :   struct modXn S;
    2253           0 :   S.v = varn(x); S.n = n;
    2254           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2255             : }
    2256             : 
    2257             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2258             : GEN
    2259        6538 : RgXn_eval(GEN Q, GEN x, long n)
    2260             : {
    2261        6538 :   long d = degpol(x);
    2262             :   int use_sqr;
    2263             :   struct modXn S;
    2264        6538 :   if (d == 1 && isrationalzero(gel(x,2)))
    2265             :   {
    2266        6531 :     GEN y = RgX_unscale(Q, gel(x,3));
    2267        6531 :     setvarn(y, varn(x)); return y;
    2268             :   }
    2269           7 :   S.v = varn(x);
    2270           7 :   S.n = n;
    2271           7 :   use_sqr = (d<<1) >= n;
    2272           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2273             : }
    2274             : 
    2275             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2276             : static GEN
    2277       44057 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2278             : {
    2279       44057 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2280       44057 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2281             : }
    2282             : 
    2283             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2284             : static GEN
    2285           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2286             : {
    2287           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2288           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2289             : }
    2290             : 
    2291             : GEN
    2292      126224 : RgXn_inv_i(GEN f, long e)
    2293             : {
    2294             :   pari_sp av;
    2295             :   ulong mask;
    2296             :   GEN W, a;
    2297      126224 :   long v = varn(f), n = 1;
    2298             : 
    2299      126224 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2300      126224 :   a = ginv(gel(f,2));
    2301      126219 :   if (e == 1) return scalarpol(a, v);
    2302      126191 :   else if (e == 2)
    2303             :   {
    2304             :     GEN b;
    2305      108733 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2306       88684 :     av = avma; b = gneg(b);
    2307       88685 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2308       88681 :     W = deg1pol_shallow(b, a, v);
    2309       88683 :     return gcopy(W);
    2310             :   }
    2311       17458 :   W = scalarpol_shallow(a,v);
    2312       17458 :   mask = quadratic_prec_mask(e);
    2313       17458 :   av = avma;
    2314       78973 :   for (;mask>1;)
    2315             :   {
    2316             :     GEN u, fr;
    2317       44057 :     long n2 = n;
    2318       44057 :     n<<=1; if (mask & 1) n--;
    2319       44057 :     mask >>= 1;
    2320       44057 :     fr = RgXn_red_shallow(f, n);
    2321       44057 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2322       44057 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2323       44057 :     if (gc_needed(av,2))
    2324             :     {
    2325           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2326           0 :       W = gerepileupto(av, W);
    2327             :     }
    2328             :   }
    2329       17458 :   return W;
    2330             : }
    2331             : 
    2332             : static GEN
    2333           0 : RgXn_inv_FpX(GEN x, long e, GEN p)
    2334             : {
    2335           0 :   pari_sp av = avma;
    2336             :   GEN r;
    2337           0 :   if (lgefint(p) == 3)
    2338             :   {
    2339           0 :     ulong pp = uel(p, 2);
    2340           0 :     r = Flx_to_ZX_inplace(Flxn_inv(RgX_to_Flx(x, pp), e, pp));
    2341             :   }
    2342             :   else
    2343           0 :     r = FpXn_inv(RgX_to_FpX(x, p), e, p);
    2344           0 :   return gerepileupto(av, FpX_to_mod(r, p));
    2345             : }
    2346             : 
    2347             : static GEN
    2348           0 : RgXn_inv_FpXQX(GEN x, long n, GEN pol, GEN p)
    2349             : {
    2350           0 :   pari_sp av = avma;
    2351           0 :   GEN r, T = RgX_to_FpX(pol, p);
    2352           0 :   if (signe(T) == 0) pari_err_OP("/", gen_1, x);
    2353           0 :   r = FpXQXn_inv(RgX_to_FpXQX(x, T, p), n, T, p);
    2354           0 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2355             : }
    2356             : 
    2357             : #define code(t1,t2) ((t1 << 6) | t2)
    2358             : 
    2359             : static GEN
    2360       83029 : RgXn_inv_fast(GEN x, long e)
    2361             : {
    2362             :   GEN p, pol;
    2363             :   long pa;
    2364       83029 :   long t = RgX_type(x,&p,&pol,&pa);
    2365       83031 :   switch(t)
    2366             :   {
    2367           0 :     case t_INTMOD: return RgXn_inv_FpX(x, e, p);
    2368             :     case code(t_POLMOD, t_INTMOD):
    2369           0 :                    return RgXn_inv_FpXQX(x, e, pol, p);
    2370       83031 :     default:       return NULL;
    2371             :   }
    2372             : }
    2373             : #undef code
    2374             : 
    2375             : GEN
    2376       83029 : RgXn_inv(GEN f, long e)
    2377             : {
    2378       83029 :   GEN h = RgXn_inv_fast(f, e);
    2379       83031 :   if (h) return h;
    2380       83031 :   return RgXn_inv_i(f, e);
    2381             : }
    2382             : 
    2383             : GEN
    2384       12957 : RgXn_exp(GEN h, long e)
    2385             : {
    2386       12957 :   pari_sp av = avma, av2;
    2387       12957 :   long v = varn(h), n=1;
    2388       12957 :   GEN f = pol_1(v), g = pol_1(v);
    2389       12957 :   ulong mask = quadratic_prec_mask(e);
    2390       12957 :   av2 = avma;
    2391       12957 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2392           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2393       40110 :   for (;mask>1;)
    2394             :   {
    2395             :     GEN q, w;
    2396       14196 :     long n2 = n;
    2397       14196 :     n<<=1; if (mask & 1) n--;
    2398       14196 :     mask >>= 1;
    2399       14196 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2400       14196 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2401       14196 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2402       14196 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2403       14196 :     if (gc_needed(av2,2))
    2404             :     {
    2405           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2406           0 :       gerepileall(av2, 2, &f, &g);
    2407             :     }
    2408             :   }
    2409       12957 :   return gerepileupto(av, f);
    2410             : }
    2411             : 
    2412             : GEN
    2413          91 : RgXn_reverse(GEN f, long e)
    2414             : {
    2415          91 :   pari_sp av = avma, av2;
    2416             :   ulong mask;
    2417             :   GEN fi, a, df, W, an;
    2418          91 :   long v = varn(f), n=1;
    2419          91 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2420           0 :     pari_err_INV("serreverse",f);
    2421          91 :   fi = ginv(gel(f,3));
    2422          91 :   a = deg1pol_shallow(fi,gen_0,v);
    2423          91 :   if (e <= 2) return gerepilecopy(av, a);
    2424          91 :   W = scalarpol(fi,v);
    2425          91 :   df = RgX_deriv(f);
    2426          91 :   mask = quadratic_prec_mask(e);
    2427          91 :   av2 = avma;
    2428         539 :   for (;mask>1;)
    2429             :   {
    2430             :     GEN u, fa, fr;
    2431         357 :     long n2 = n, rt;
    2432         357 :     n<<=1; if (mask & 1) n--;
    2433         357 :     mask >>= 1;
    2434         357 :     fr = RgXn_red_shallow(f, n);
    2435         357 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2436         357 :     an = RgXn_powers(a, rt, n);
    2437         357 :     if (n>1)
    2438             :     {
    2439         357 :       long n4 = (n2+1)>>1;
    2440         357 :       GEN dfr = RgXn_red_shallow(df, n2);
    2441         357 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2442         357 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2443         357 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2444             :     }
    2445         357 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2446         357 :     fa = RgX_shift(fa, -n2);
    2447         357 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2448         357 :     if (gc_needed(av2,2))
    2449             :     {
    2450           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2451           0 :       gerepileall(av2, 2, &a, &W);
    2452             :     }
    2453             :   }
    2454          91 :   return gerepileupto(av, a);
    2455             : }
    2456             : 
    2457             : GEN
    2458           0 : RgXn_sqrt(GEN h, long e)
    2459             : {
    2460           0 :   pari_sp av = avma, av2;
    2461           0 :   long v = varn(h), n = 1;
    2462           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2463           0 :   ulong mask = quadratic_prec_mask(e);
    2464           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2465           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2466           0 :   av2 = avma;
    2467             :   while(1)
    2468           0 :   {
    2469           0 :     long n2 = n, m;
    2470             :     GEN g;
    2471           0 :     n<<=1; if (mask & 1) n--;
    2472           0 :     mask >>= 1;
    2473           0 :     m = n-n2;
    2474           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2475           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2476           0 :     if (mask==1) return gerepileupto(av, f);
    2477           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2478           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2479           0 :     if (gc_needed(av2,2))
    2480             :     {
    2481           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2482           0 :       gerepileall(av2, 2, &f, &df);
    2483             :     }
    2484             :   }
    2485             : }
    2486             : 
    2487             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2488             : GEN
    2489       18391 : RgXQ_powu(GEN x, ulong n, GEN T)
    2490             : {
    2491             :   pari_sp av;
    2492             :   GEN y;
    2493             : 
    2494       18391 :   if (!n) return pol_1(varn(x));
    2495       16717 :   if (n == 1) return RgX_copy(x);
    2496       14945 :   av = avma; y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2497       14945 :   return gerepileupto(av, y);
    2498             : }
    2499             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2500             : GEN
    2501       18724 : RgXQ_pow(GEN x, GEN n, GEN T)
    2502             : {
    2503             :   pari_sp av;
    2504       18724 :   long s = signe(n);
    2505             :   GEN y;
    2506             : 
    2507       18724 :   if (!s) return pol_1(varn(x));
    2508       18724 :   if (is_pm1(n) == 1)
    2509           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2510       18724 :   av = avma;
    2511       18724 :   if (s < 0) x = RgXQ_inv(x, T);
    2512       18724 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2513       18724 :   return gerepileupto(av, y);
    2514             : }
    2515             : static GEN
    2516      159042 : _ZXQsqr(void *data, GEN x) { return ZXQ_sqr(x, (GEN)data); }
    2517             : static GEN
    2518       50465 : _ZXQmul(void *data, GEN x, GEN y) { return ZXQ_mul(x,y, (GEN)data); }
    2519             : /* x,T in Z[X], n in N, compute lift(x^n mod T)) */
    2520             : GEN
    2521      163661 : ZXQ_powu(GEN x, ulong n, GEN T)
    2522             : {
    2523             :   pari_sp av;
    2524             :   GEN y;
    2525             : 
    2526      163661 :   if (!n) return pol_1(varn(x));
    2527      163661 :   if (n == 1) return ZX_copy(x);
    2528      107408 :   av = avma; y = gen_powu(x, n, (void*)T, &_ZXQsqr, &_ZXQmul);
    2529      107415 :   return gerepileupto(av, y);
    2530             : }
    2531             : 
    2532             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2533             : GEN
    2534        4011 : RgXQ_powers(GEN x, long l, GEN T)
    2535             : {
    2536        4011 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2537        4011 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2538             : }
    2539             : 
    2540             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2541             : GEN
    2542        2765 : QXQ_powers(GEN a, long n, GEN T)
    2543             : {
    2544        2765 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2545             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2546        2765 :   if (den)
    2547             :   { /* restore denominators */
    2548        1568 :     GEN d = den;
    2549             :     long i;
    2550        1568 :     gel(v,2) = a;
    2551        4606 :     for (i=3; i<=n+1; i++) {
    2552        3038 :       d = mulii(d,den);
    2553        3038 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2554             :     }
    2555             :   }
    2556        2765 :   return v;
    2557             : }
    2558             : 
    2559             : static GEN
    2560        1771 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2561             : {
    2562        1771 :   long l, i, m = 0;
    2563             :   GEN dz, z;
    2564        1771 :   GEN V = cgetg_copy(v, &l);
    2565        5768 :   for (i = imin; i < l; i++)
    2566             :   {
    2567        3997 :     GEN c = gel(v, i);
    2568        3997 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2569             :   }
    2570        1771 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2571        1771 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2572        5768 :   for (i = imin; i < l; i++)
    2573             :   {
    2574        3997 :     GEN c = gel(v,i);
    2575        3997 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2576        3997 :     gel(V,i) = c;
    2577             :   }
    2578        1771 :   return V;
    2579             : }
    2580             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2581             : GEN
    2582        1708 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2583        1708 : { return do_QXQ_eval(v, 1, a, T); }
    2584             : GEN
    2585          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2586          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2587             : 
    2588             : GEN
    2589         371 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2590             : {
    2591         371 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2592             : }
    2593             : 
    2594             : GEN
    2595       40740 : RgXQ_norm(GEN x, GEN T)
    2596             : {
    2597             :   pari_sp av;
    2598       40740 :   long dx = degpol(x);
    2599             :   GEN L, y;
    2600             : 
    2601       40740 :   av = avma; y = resultant(T, x);
    2602       40740 :   L = leading_coeff(T);
    2603       40740 :   if (gequal1(L) || !signe(x)) return y;
    2604           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2605             : }
    2606             : 
    2607             : GEN
    2608      114169 : RgX_blocks(GEN P, long n, long m)
    2609             : {
    2610      114169 :   GEN z = cgetg(m+1,t_VEC);
    2611      114169 :   long i,j, k=2, l = lg(P);
    2612      535679 :   for(i=1; i<=m; i++)
    2613             :   {
    2614      421510 :     GEN zi = cgetg(n+2,t_POL);
    2615      421510 :     zi[1] = P[1];
    2616      421510 :     gel(z,i) = zi;
    2617     2672673 :     for(j=2; j<n+2; j++)
    2618     2251163 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2619      421510 :     zi = RgX_renormalize_lg(zi, n+2);
    2620             :   }
    2621      114169 :   return z;
    2622             : }
    2623             : 
    2624             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2625             : void
    2626       77126 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2627             : {
    2628       77126 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2629             :   GEN p0, p1;
    2630             : 
    2631       77128 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2632             : 
    2633       76841 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2634       76841 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2635       76841 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2636     4833527 :   for (i=0; i<n1; i++)
    2637             :   {
    2638     4756686 :     p0[2+i] = p[2+(i<<1)];
    2639     4756686 :     p1[2+i] = p[3+(i<<1)];
    2640             :   }
    2641       76841 :   if (n1 != n0)
    2642       42553 :     p0[2+i] = p[2+(i<<1)];
    2643       76841 :   *pe = normalizepol(p0);
    2644       76841 :   *po = normalizepol(p1);
    2645             : }
    2646             : 
    2647             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2648             : GEN
    2649       40922 : RgX_splitting(GEN p, long k)
    2650             : {
    2651       40922 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2652             :   GEN r;
    2653             : 
    2654       40922 :   m = n/k;
    2655       40922 :   r = cgetg(k+1,t_VEC);
    2656      225666 :   for(i=1; i<=k; i++)
    2657             :   {
    2658      184744 :     gel(r,i) = cgetg(m+3, t_POL);
    2659      184744 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2660             :   }
    2661      554799 :   for (j=1, i=0, l=2; i<=n; i++)
    2662             :   {
    2663      513877 :     gmael(r,j,l) = gel(p,2+i);
    2664      513877 :     if (j==k) { j=1; l++; } else j++;
    2665             :   }
    2666      225666 :   for(i=1; i<=k; i++)
    2667      184744 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2668       40922 :   return r;
    2669             : }
    2670             : 
    2671             : /*******************************************************************/
    2672             : /*                                                                 */
    2673             : /*                        Kronecker form                           */
    2674             : /*                                                                 */
    2675             : /*******************************************************************/
    2676             : 
    2677             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2678             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2679             :  * normalized coefficients */
    2680             : GEN
    2681      149401 : Kronecker_to_mod(GEN z, GEN T)
    2682             : {
    2683      149401 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2684      149401 :   GEN x, t = cgetg(N,t_POL);
    2685      149401 :   t[1] = T[1];
    2686      149401 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2687      149401 :   x[1] = z[1];
    2688      149401 :   T = RgX_copy(T);
    2689     1468263 :   for (i=2; i<lx+2; i++, z+= N-2)
    2690             :   {
    2691     1318862 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2692     1318862 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2693             :   }
    2694      149401 :   N = (l-2) % (N-2) + 2;
    2695      149401 :   for (j=2; j<N; j++) t[j] = z[j];
    2696      149401 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2697      149401 :   return normalizepol_lg(x, i+1);
    2698             : }
    2699             : 
    2700             : /*******************************************************************/
    2701             : /*                                                                 */
    2702             : /*                        Domain detection                         */
    2703             : /*                                                                 */
    2704             : /*******************************************************************/
    2705             : 
    2706             : static GEN
    2707      149227 : zero_FpX_mod(GEN p, long v)
    2708             : {
    2709      149227 :   GEN r = cgetg(3,t_POL);
    2710      149227 :   r[1] = evalvarn(v);
    2711      149227 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2712      149227 :   return r;
    2713             : }
    2714             : 
    2715             : static GEN
    2716      498084 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2717             : {
    2718      498084 :   pari_sp av = avma;
    2719             :   GEN r;
    2720      498084 :   if (lgefint(p) == 3)
    2721             :   {
    2722      467640 :     ulong pp = uel(p, 2);
    2723      467640 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2724             :                                   RgX_to_Flx(y, pp), pp));
    2725             :   }
    2726             :   else
    2727       30444 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2728      498084 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2729      385691 :   return gerepileupto(av, FpX_to_mod(r, p));
    2730             : }
    2731             : 
    2732             : static GEN
    2733         532 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2734             : {
    2735         532 :   GEN r = cgetg(3,t_POL);
    2736         532 :   r[1] = evalvarn(v);
    2737         532 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2738         532 :   return r;
    2739             : }
    2740             : 
    2741             : static GEN
    2742        1092 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2743             : {
    2744        1092 :   pari_sp av = avma;
    2745             :   long dT;
    2746             :   GEN kx, ky, r;
    2747        1092 :   GEN T = RgX_to_FpX(pol, p);
    2748        1092 :   if (signe(T)==0) pari_err_OP("*", x, y);
    2749        1085 :   dT = degpol(T);
    2750        1085 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2751        1085 :   ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2752        1085 :   r = FpX_mul(kx, ky, p);
    2753        1085 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2754         560 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2755             : }
    2756             : 
    2757             : static GEN
    2758      364745 : RgX_liftred(GEN x, GEN T)
    2759      364745 : { return RgXQX_red(liftpol_shallow(x), T); }
    2760             : 
    2761             : static GEN
    2762      136323 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2763             : {
    2764      136323 :   pari_sp av = avma;
    2765      136323 :   long dT = degpol(T);
    2766      136323 :   GEN r = QX_mul(ZXX_to_Kronecker(RgX_liftred(x, T), dT),
    2767             :                  ZXX_to_Kronecker(RgX_liftred(y, T), dT));
    2768      136323 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2769             : }
    2770             : 
    2771             : static GEN
    2772        1260 : RgX_sqr_FpX(GEN x, GEN p)
    2773             : {
    2774        1260 :   pari_sp av = avma;
    2775             :   GEN r;
    2776        1260 :   if (lgefint(p) == 3)
    2777             :   {
    2778        1056 :     ulong pp = uel(p, 2);
    2779        1056 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2780             :   }
    2781             :   else
    2782         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2783        1260 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2784        1162 :   return gerepileupto(av, FpX_to_mod(r, p));
    2785             : }
    2786             : 
    2787             : static GEN
    2788         196 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2789             : {
    2790         196 :   pari_sp av = avma;
    2791             :   long dT;
    2792         196 :   GEN kx, r, T = RgX_to_FpX(pol, p);
    2793         196 :   if (signe(T)==0) pari_err_OP("*",x,x);
    2794         189 :   dT = degpol(T);
    2795         189 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2796         189 :   r = FpX_sqr(kx, p);
    2797         189 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2798         189 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2799             : }
    2800             : 
    2801             : static GEN
    2802       12329 : RgX_sqr_QXQX(GEN x, GEN T)
    2803             : {
    2804       12329 :   pari_sp av = avma;
    2805       12329 :   long dT = degpol(T);
    2806       12329 :   GEN r = QX_sqr(ZXX_to_Kronecker(RgX_liftred(x, T), dT));
    2807       12329 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2808             : }
    2809             : 
    2810             : static GEN
    2811       60795 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2812             : {
    2813       60795 :   pari_sp av = avma;
    2814             :   GEN r;
    2815       60795 :   if (lgefint(p) == 3)
    2816             :   {
    2817       45437 :     ulong pp = uel(p, 2);
    2818       45437 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2819             :                                   RgX_to_Flx(y, pp), pp));
    2820             :   }
    2821             :   else
    2822       15358 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2823       60795 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2824       24059 :   return gerepileupto(av, FpX_to_mod(r, p));
    2825             : }
    2826             : 
    2827             : static GEN
    2828       39885 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    2829             : {
    2830       39885 :   pari_sp av = avma;
    2831             :   GEN r;
    2832       39885 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    2833       39885 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    2834             : }
    2835             : static GEN
    2836          42 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2837             : {
    2838          42 :   pari_sp av = avma;
    2839             :   GEN r;
    2840          42 :   GEN T = RgX_to_FpX(pol, p);
    2841          42 :   if (signe(T) == 0) pari_err_OP("%", x, y);
    2842          35 :   if (lgefint(p) == 3)
    2843             :   {
    2844          35 :     ulong pp = uel(p, 2);
    2845          35 :     GEN Tp = ZX_to_Flx(T, pp);
    2846          35 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2847             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2848             :   }
    2849             :   else
    2850           0 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2851          35 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2852          28 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2853             : }
    2854             : 
    2855             : #define code(t1,t2) ((t1 << 6) | t2)
    2856             : static GEN
    2857    48212564 : RgX_mul_fast(GEN x, GEN y)
    2858             : {
    2859             :   GEN p, pol;
    2860             :   long pa;
    2861    48212564 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2862    48212564 :   switch(t)
    2863             :   {
    2864    36123550 :     case t_INT:    return ZX_mul(x,y);
    2865     1560530 :     case t_FRAC:   return QX_mul(x,y);
    2866      104161 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2867      498084 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2868             :     case code(t_POLMOD, t_INT):
    2869             :     case code(t_POLMOD, t_FRAC):
    2870      136323 :                    return RgX_mul_QXQX(x, y, pol);
    2871             :     case code(t_POLMOD, t_INTMOD):
    2872        1092 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2873     9788824 :     default:       return NULL;
    2874             :   }
    2875             : }
    2876             : static GEN
    2877      141802 : RgX_sqr_fast(GEN x)
    2878             : {
    2879             :   GEN p, pol;
    2880             :   long pa;
    2881      141802 :   long t = RgX_type(x,&p,&pol,&pa);
    2882      141802 :   switch(t)
    2883             :   {
    2884       86962 :     case t_INT:    return ZX_sqr(x);
    2885       32725 :     case t_FRAC:   return QX_sqr(x);
    2886        2296 :     case t_FFELT:  return FFX_sqr(x, pol);
    2887        1260 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2888             :     case code(t_POLMOD, t_INT):
    2889             :     case code(t_POLMOD, t_FRAC):
    2890       12329 :                    return RgX_sqr_QXQX(x, pol);
    2891             :     case code(t_POLMOD, t_INTMOD):
    2892         196 :                    return RgX_sqr_FpXQX(x, pol, p);
    2893        6034 :     default:       return NULL;
    2894             :   }
    2895             : }
    2896             : 
    2897             : static GEN
    2898     6132659 : RgX_rem_fast(GEN x, GEN y)
    2899             : {
    2900             :   GEN p, pol;
    2901             :   long pa;
    2902     6132659 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2903     6132659 :   switch(t)
    2904             :   {
    2905     3879025 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    2906     1217250 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    2907          84 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2908       60795 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2909             :     case code(t_POLMOD, t_INT):
    2910             :     case code(t_POLMOD, t_FRAC):
    2911       39885 :                    return RgX_rem_QXQX(x, y, pol);
    2912             :     case code(t_POLMOD, t_INTMOD):
    2913          42 :                    return RgX_rem_FpXQX(x, y, pol, p);
    2914      935578 :     default:       return NULL;
    2915             :   }
    2916             : }
    2917             : 
    2918             : #undef code
    2919             : 
    2920             : GEN
    2921    45691127 : RgX_mul(GEN x, GEN y)
    2922             : {
    2923    45691127 :   GEN z = RgX_mul_fast(x,y);
    2924    45691121 :   if (!z) z = RgX_mul_i(x,y);
    2925    45691121 :   return z;
    2926             : }
    2927             : 
    2928             : GEN
    2929      125163 : RgX_sqr(GEN x)
    2930             : {
    2931      125163 :   GEN z = RgX_sqr_fast(x);
    2932      125156 :   if (!z) z = RgX_sqr_i(x);
    2933      125156 :   return z;
    2934             : }
    2935             : 
    2936             : GEN
    2937     6132659 : RgX_rem(GEN x, GEN y)
    2938             : {
    2939     6132659 :   GEN z = RgX_rem_fast(x, y);
    2940     6132652 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    2941     6132652 :   return z;
    2942             : }
    2943             : 
    2944             : GEN
    2945     2477380 : RgXn_mul(GEN f, GEN g, long n)
    2946             : {
    2947     2477380 :   pari_sp av = avma;
    2948     2477380 :   GEN h = RgX_mul_fast(f,g);
    2949     2477380 :   if (!h) return RgXn_mul2(f,g,n);
    2950      167973 :   if (degpol(h) < n) return h;
    2951       44093 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    2952             : }
    2953             : 
    2954             : GEN
    2955       16639 : RgXn_sqr(GEN f, long n)
    2956             : {
    2957       16639 :   pari_sp av = avma;
    2958       16639 :   GEN g = RgX_sqr_fast(f);
    2959       16639 :   if (!g) return RgXn_sqr2(f,n);
    2960       16401 :   if (degpol(g) < n) return g;
    2961        1071 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    2962             : }
    2963             : 
    2964             : /* (f*g) \/ x^n */
    2965             : GEN
    2966       44057 : RgX_mulhigh_i(GEN f, GEN g, long n)
    2967             : {
    2968       44057 :   GEN h = RgX_mul_fast(f,g);
    2969       44057 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    2970             : }
    2971             : 
    2972             : /* (f*g) \/ x^n */
    2973             : GEN
    2974           0 : RgX_sqrhigh_i(GEN f, long n)
    2975             : {
    2976           0 :   GEN h = RgX_sqr_fast(f);
    2977           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    2978             : }

Generated by: LCOV version 1.13