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.1 lcov report (development 24302-468db01a7) Lines: 1476 1638 90.1 %
Date: 2019-09-18 05:55:38 Functions: 177 192 92.2 %
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     3826631 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     3826631 :   long pold=1, rold=n*(d-1);
      32    24381023 :   for(p=2; p<=d; p++)
      33             :   {
      34    20554392 :     r = m*(p-1) + n*((d-1)/p);
      35    20554392 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     3826631 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41     7034470 : 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     7034470 :   pari_sp av = avma;
      45             :   long i;
      46     7034470 :   GEN z = cmul(E,P,a,ff->one(E));
      47     7035080 :   if (!z) z = gen_0;
      48    49460933 :   for (i=1; i<=n; i++)
      49             :   {
      50    42426610 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    42427003 :     if (t) {
      52    31580366 :       z = ff->add(E, z, t);
      53    31568027 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56     7034323 :   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     5890718 : 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     5890718 :   pari_sp av = avma;
      69     5890718 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     5890718 :   if (d < 0) return ff->zero(E);
      73     5572163 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74      651964 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75      651964 :   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      651964 :   d -= l;
      81      651964 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     2114651 :   while (d >= l-1)
      83             :   {
      84      810736 :     d -= l-1;
      85      810736 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86      810645 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87      810721 :     if (gc_needed(av,2))
      88          92 :       z = gerepileupto(av, z);
      89             :   }
      90      651960 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91      651958 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92      651965 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96      406154 : 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      406154 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102      406154 :   if (d < 0) return ff->zero(E);
     103      405496 :   rtd = (long) sqrt((double)d);
     104      405496 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105      405497 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106      405507 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      929399 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    12827024 : _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      782451 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      257133 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      948072 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       21035 : _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      476553 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      476553 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      154406 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      154406 :   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        2905 : RgXV_RgV_eval(GEN Q, GEN x)
     145             : {
     146        2905 :   long i, l = lg(Q), vQ = gvar(Q);
     147        2905 :   GEN v = cgetg(l, t_VEC);
     148      243236 :   for (i = 1; i < l; i++)
     149             :   {
     150      240331 :     GEN Qi = gel(Q, i);
     151      240331 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     152             :   }
     153        2905 :   return v;
     154             : }
     155             : 
     156             : const struct bb_algebra *
     157      118780 : get_Rg_algebra(void)
     158             : {
     159      118780 :   return &Rg_algebra;
     160             : }
     161             : 
     162             : static struct bb_ring Rg_ring = {  _gen_add, _gen_mul, _gen_sqr };
     163             : 
     164             : static GEN
     165       17766 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
     166             : {
     167             :   (void) E;
     168       17766 :   return RgX_divrem(x, y, r);
     169             : }
     170             : 
     171             : GEN
     172        4354 : RgX_digits(GEN x, GEN T)
     173             : {
     174        4354 :   pari_sp av = avma;
     175        4354 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     176        4354 :   GEN z = gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
     177        4354 :   return gerepileupto(av, z);
     178             : }
     179             : 
     180             : /*******************************************************************/
     181             : /*                                                                 */
     182             : /*                         RgX                                     */
     183             : /*                                                                 */
     184             : /*******************************************************************/
     185             : 
     186             : long
     187    24556015 : RgX_equal(GEN x, GEN y)
     188             : {
     189    24556015 :   long i = lg(x);
     190             : 
     191    24556015 :   if (i != lg(y)) return 0;
     192   103459401 :   for (i--; i > 1; i--)
     193    79031881 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194    24427520 :   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     2893075 : Rg_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203     2893075 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     204     2893074 :   if (RgX_type_is_composite(tx))
     205       13545 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206     2893074 :   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     2892031 :     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     6971482 : Rg_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220     6971482 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     221     6971482 :   if (RgX_type_is_composite(tx))
     222       50526 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223     6971482 :   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     6970929 :     default: return gen_0;
     229             :   }
     230             : }
     231             : 
     232             : GEN
     233        4291 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     234             : {
     235        4291 :   long i, n = degpol(P);
     236             :   GEN z, dz, dP;
     237        4291 :   if (n < 0) return gen_0;
     238        4291 :   P = Q_remove_denom(P, &dP);
     239        4291 :   z = gel(P,2); if (n == 0) return icopy(z);
     240        2345 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     241        2345 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     242        2345 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     243        2345 :   dz = mul_denom(dP, dV);
     244        2345 :   return dz? RgX_Rg_div(z, dz): z;
     245             : }
     246             : 
     247             : /* Return P(h * x), not memory clean */
     248             : GEN
     249       10787 : RgX_unscale(GEN P, GEN h)
     250             : {
     251       10787 :   long i, l = lg(P);
     252       10787 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253       10787 :   Q[1] = P[1];
     254       10787 :   if (l == 2) return Q;
     255       10759 :   gel(Q,2) = gcopy(gel(P,2));
     256       44478 :   for (i=3; i<l; i++)
     257             :   {
     258       33719 :     hi = gmul(hi,h);
     259       33719 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261       10759 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      371958 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      371958 :   long i, l = lg(P);
     268      371958 :   GEN Q = cgetg(l, t_POL);
     269      371958 :   Q[1] = P[1];
     270      371958 :   if (l == 2) return Q;
     271      371958 :   gel(Q,2) = gel(P,2);
     272      371958 :   if (l == 3) return Q;
     273      371958 :   if (h == -1)
     274      318889 :     for (i = 3; i < l; i++)
     275             :     {
     276      308284 :       gel(Q,i) = negi(gel(P,i));
     277      308284 :       if (++i == l) break;
     278      304617 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      357686 :     gel(Q,3) = mulis(gel(P,3), h);
     284      357686 :     hi = sqrs(h);
     285     1725234 :     for (i = 4; i < l; i++)
     286             :     {
     287     1367548 :       gel(Q,i) = mulii(gel(P,i), hi);
     288     1367548 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      371958 :   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        7378 : ZX_unscale(GEN P, GEN h)
     296             : {
     297             :   long i, l;
     298             :   GEN Q, hi;
     299        7378 :   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       63045 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       63045 :   long i, ni = n, l = lg(P);
     319       63045 :   GEN Q = cgetg(l, t_POL);
     320       63045 :   Q[1] = P[1];
     321       63045 :   if (l == 2) return Q;
     322       63045 :   gel(Q,2) = gel(P,2);
     323       63045 :   if (l == 3) return Q;
     324       63045 :   gel(Q,3) = shifti(gel(P,3), ni);
     325      245809 :   for (i=4; i<l; i++)
     326             :   {
     327      182764 :     ni += n;
     328      182764 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       63045 :   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         651 : RgXV_unscale(GEN v, GEN h)
     356             : {
     357             :   long i, l;
     358             :   GEN w;
     359         651 :   if (!h || isint1(h)) return v;
     360         350 :   w = cgetg_copy(v, &l);
     361         350 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     362         350 :   return w;
     363             : }
     364             : 
     365             : /* Return h^degpol(P) P(x / h), not memory clean */
     366             : GEN
     367       12719 : RgX_rescale(GEN P, GEN h)
     368             : {
     369       12719 :   long i, l = lg(P);
     370       12719 :   GEN Q = cgetg(l,t_POL), hi = h;
     371       12719 :   gel(Q,l-1) = gel(P,l-1);
     372      103670 :   for (i=l-2; i>=2; i--)
     373             :   {
     374      102473 :     gel(Q,i) = gmul(gel(P,i), hi);
     375      102473 :     if (i == 2) break;
     376       90951 :     hi = gmul(hi,h);
     377             :   }
     378       12719 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      147910 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      147910 :   long i,id, dy, dx = degpol(x0);
     387      147910 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       78176 :   dy = dx/d;
     389       78176 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       78176 :   z = y + 2;
     391       78176 :   x = x0+ 2;
     392       78176 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       78176 :   return y;
     394             : }
     395             : 
     396             : /* F a t_RFRAC */
     397             : long
     398          63 : rfrac_deflate_order(GEN F)
     399             : {
     400          63 :   GEN N = gel(F,1), D = gel(F,2);
     401          63 :   long m = (degpol(D) <= 0)? 0: RgX_deflate_order(D);
     402          63 :   if (m == 1) return 1;
     403          28 :   if (typ(N) == t_POL && varn(N) == varn(D))
     404          14 :     m = cgcd(m, RgX_deflate_order(N));
     405          28 :   return m;
     406             : }
     407             : /* F a t_RFRAC */
     408             : GEN
     409          63 : rfrac_deflate_max(GEN F, long *m)
     410             : {
     411          63 :   *m = rfrac_deflate_order(F);
     412          63 :   return rfrac_deflate(F, *m);
     413             : }
     414             : /* F a t_RFRAC */
     415             : GEN
     416          63 : rfrac_deflate(GEN F, long m)
     417             : {
     418          63 :   GEN N = gel(F,1), D = gel(F,2);
     419          63 :   if (m == 1) return F;
     420          28 :   if (typ(N) == t_POL && varn(N) == varn(D)) N = RgX_deflate(N, m);
     421          28 :   D = RgX_deflate(D, m); return mkrfrac(N, D);
     422             : }
     423             : 
     424             : 
     425             : /* return x0(X^d) */
     426             : GEN
     427      593461 : RgX_inflate(GEN x0, long d)
     428             : {
     429      593461 :   long i, id, dy, dx = degpol(x0);
     430      593461 :   GEN x = x0 + 2, z, y;
     431      593461 :   if (dx <= 0) return leafcopy(x0);
     432      586202 :   dy = dx*d;
     433      586202 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     434      586204 :   z = y + 2;
     435      586204 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     436      586204 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     437      586204 :   return y;
     438             : }
     439             : 
     440             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     441             : GEN
     442     1417422 : RgX_translate(GEN P, GEN c)
     443             : {
     444     1417422 :   pari_sp av = avma;
     445             :   GEN Q, R;
     446             :   long i, k, n;
     447             : 
     448     1417422 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     449     1414969 :   Q = leafcopy(P);
     450     1414969 :   R = Q+2; n = degpol(P);
     451     1414969 :   if (isint1(c))
     452             :   {
     453        9023 :     for (i=1; i<=n; i++)
     454             :     {
     455        6657 :       for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gel(R,k+1));
     456        6657 :       if (gc_needed(av,2))
     457             :       {
     458           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(1), i = %ld/%ld", i,n);
     459           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     460             :       }
     461             :     }
     462             :   }
     463     1412603 :   else if (isintm1(c))
     464             :   {
     465      150588 :     for (i=1; i<=n; i++)
     466             :     {
     467      127216 :       for (k=n-i; k<n; k++) gel(R,k) = gsub(gel(R,k), gel(R,k+1));
     468      127216 :       if (gc_needed(av,2))
     469             :       {
     470           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(-1), i = %ld/%ld", i,n);
     471           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     472             :       }
     473             :     }
     474             :   }
     475             :   else
     476             :   {
     477     4778866 :     for (i=1; i<=n; i++)
     478             :     {
     479     3389635 :       for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gmul(c, gel(R,k+1)));
     480     3389635 :       if (gc_needed(av,2))
     481             :       {
     482           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL, i = %ld/%ld", i,n);
     483           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     484             :       }
     485             :     }
     486             :   }
     487     1414969 :   return gerepilecopy(av, Q);
     488             : }
     489             : 
     490             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     491             : GEN
     492       21798 : RgXQX_translate(GEN P, GEN c, GEN T)
     493             : {
     494       21798 :   pari_sp av = avma;
     495             :   GEN Q, R;
     496             :   long i, k, n;
     497             : 
     498       21798 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     499       21490 :   Q = leafcopy(P);
     500       21490 :   R = Q+2; n = degpol(P);
     501       74501 :   for (i=1; i<=n; i++)
     502             :   {
     503      222565 :     for (k=n-i; k<n; k++)
     504             :     {
     505      169554 :       pari_sp av2 = avma;
     506      508662 :       gel(R,k) = gerepileupto(av2,
     507      339108 :                    RgX_rem(gadd(gel(R,k), gmul(c, gel(R,k+1))), T));
     508             :     }
     509       53011 :     if (gc_needed(av,2))
     510             :     {
     511           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     512           0 :       Q = gerepilecopy(av, Q); R = Q+2;
     513             :     }
     514             :   }
     515       21490 :   return gerepilecopy(av, Q);
     516             : }
     517             : 
     518             : /********************************************************************/
     519             : /**                                                                **/
     520             : /**                          CONVERSIONS                           **/
     521             : /**                       (not memory clean)                       **/
     522             : /**                                                                **/
     523             : /********************************************************************/
     524             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     525             :  * but everything else is */
     526             : static GEN
     527       25148 : QXQ_to_mod_copy(GEN x, GEN T)
     528             : {
     529             :   long d;
     530       25148 :   switch(typ(x))
     531             :   {
     532       10311 :     case t_INT:  return icopy(x);
     533         805 :     case t_FRAC: return gcopy(x);
     534             :     case t_POL:
     535       14032 :       d = degpol(x);
     536       14032 :       if (d < 0) return gen_0;
     537       13472 :       if (d == 0) return gcopy(gel(x,2));
     538       13122 :       return mkpolmod(RgX_copy(x), T);
     539           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     540             :              return NULL;/* LCOV_EXCL_LINE */
     541             :   }
     542             : }
     543             : /* pure shallow version */
     544             : static GEN
     545     1154590 : QXQ_to_mod(GEN x, GEN T)
     546             : {
     547             :   long d;
     548     1154590 :   switch(typ(x))
     549             :   {
     550             :     case t_INT:
     551      439453 :     case t_FRAC: return x;
     552             :     case t_POL:
     553      715137 :       d = degpol(x);
     554      715137 :       if (d < 0) return gen_0;
     555      539983 :       if (d == 0) return gel(x,2);
     556      518473 :       return mkpolmod(x, T);
     557           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     558             :              return NULL;/* LCOV_EXCL_LINE */
     559             :   }
     560             : }
     561             : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod_copy to all coeffs.
     562             :  * Not memory clean because T not copied, but everything else is */
     563             : static GEN
     564        3416 : QXQX_to_mod(GEN z, GEN T)
     565             : {
     566        3416 :   long i,l = lg(z);
     567        3416 :   GEN x = cgetg(l,t_POL);
     568        3416 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     569        3416 :   x[1] = z[1]; return normalizepol_lg(x,l);
     570             : }
     571             : /* pure shallow version */
     572             : GEN
     573      153908 : QXQX_to_mod_shallow(GEN z, GEN T)
     574             : {
     575      153908 :   long i,l = lg(z);
     576      153908 :   GEN x = cgetg(l,t_POL);
     577      153908 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     578      153908 :   x[1] = z[1]; return normalizepol_lg(x,l);
     579             : }
     580             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     581             : GEN
     582        1372 : QXQXV_to_mod(GEN V, GEN T)
     583             : {
     584        1372 :   long i, l = lg(V);
     585        1372 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     586        1372 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     587        1372 :   return z;
     588             : }
     589             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     590             : GEN
     591        5202 : QXQV_to_mod(GEN V, GEN T)
     592             : {
     593        5202 :   long i, l = lg(V);
     594        5202 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     595        5202 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     596        5202 :   return z;
     597             : }
     598             : 
     599             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     600             : GEN
     601       23926 : QXQC_to_mod_shallow(GEN V, GEN T)
     602             : {
     603       23926 :   long i, l = lg(V);
     604       23926 :   GEN z = cgetg(l, t_COL);
     605       23926 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     606       23926 :   return z;
     607             : }
     608             : 
     609             : GEN
     610        7476 : QXQM_to_mod_shallow(GEN V, GEN T)
     611             : {
     612        7476 :   long i, l = lg(V);
     613        7476 :   GEN z = cgetg(l, t_MAT);
     614        7476 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     615        7476 :   return z;
     616             : }
     617             : 
     618             : GEN
     619      800975 : RgX_renormalize_lg(GEN x, long lx)
     620             : {
     621             :   long i;
     622     2188544 :   for (i = lx-1; i>1; i--)
     623     2043643 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     624      800975 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     625      800975 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     626             : }
     627             : 
     628             : GEN
     629      524048 : RgV_to_RgX(GEN x, long v)
     630             : {
     631      524048 :   long i, k = lg(x);
     632             :   GEN p;
     633             : 
     634      524048 :   while (--k && gequal0(gel(x,k)));
     635      524048 :   if (!k) return pol_0(v);
     636      518098 :   i = k+2; p = cgetg(i,t_POL);
     637      518099 :   p[1] = evalsigne(1) | evalvarn(v);
     638      518099 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     639      518099 :   return p;
     640             : }
     641             : GEN
     642      158603 : RgV_to_RgX_reverse(GEN x, long v)
     643             : {
     644      158603 :   long j, k, l = lg(x);
     645             :   GEN p;
     646             : 
     647      158603 :   for (k = 1; k < l; k++)
     648      158603 :     if (!gequal0(gel(x,k))) break;
     649      158603 :   if (k == l) return pol_0(v);
     650      158603 :   k -= 1;
     651      158603 :   l -= k;
     652      158603 :   x += k;
     653      158603 :   p = cgetg(l+1,t_POL);
     654      158603 :   p[1] = evalsigne(1) | evalvarn(v);
     655      158603 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     656      158603 :   return p;
     657             : }
     658             : 
     659             : /* return the (N-dimensional) vector of coeffs of p */
     660             : GEN
     661     7113501 : RgX_to_RgC(GEN x, long N)
     662             : {
     663             :   long i, l;
     664             :   GEN z;
     665     7113501 :   l = lg(x)-1; x++;
     666     7113501 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     667     7113501 :   z = cgetg(N+1,t_COL);
     668     7113654 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     669     7113654 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     670     7113654 :   return z;
     671             : }
     672             : GEN
     673      760528 : Rg_to_RgC(GEN x, long N)
     674             : {
     675      760528 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     676             : }
     677             : 
     678             : /* vector of polynomials (in v) whose coeffs are given by the columns of x */
     679             : GEN
     680       58252 : RgM_to_RgXV(GEN x, long v)
     681       58252 : { pari_APPLY_type(t_VEC, RgV_to_RgX(gel(x,i), v)) }
     682             : 
     683             : /* matrix whose entries are given by the coeffs of the polynomials in
     684             :  * vector v (considered as degree n-1 polynomials) */
     685             : GEN
     686      135492 : RgV_to_RgM(GEN x, long n)
     687      135492 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     688             : 
     689             : GEN
     690        5622 : RgXV_to_RgM(GEN x, long n)
     691        5622 : { pari_APPLY_type(t_MAT, RgX_to_RgC(gel(x,i), n)) }
     692             : 
     693             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     694             : GEN
     695       17405 : RgM_to_RgXX(GEN x, long v,long w)
     696             : {
     697       17405 :   long j, lx = lg(x);
     698       17405 :   GEN y = cgetg(lx+1, t_POL);
     699       17405 :   y[1] = evalsigne(1) | evalvarn(v);
     700       17405 :   y++;
     701       17405 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     702       17405 :   return normalizepol_lg(--y, lx+1);
     703             : }
     704             : 
     705             : /* matrix whose entries are given by the coeffs of the polynomial v in
     706             :  * two variables (considered as degree n-1 polynomials) */
     707             : GEN
     708         308 : RgXX_to_RgM(GEN v, long n)
     709             : {
     710         308 :   long j, N = lg(v)-1;
     711         308 :   GEN y = cgetg(N, t_MAT);
     712         308 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     713         308 :   return y;
     714             : }
     715             : 
     716             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     717             : GEN
     718       19572 : RgXY_swapspec(GEN x, long n, long w, long nx)
     719             : {
     720       19572 :   long j, ly = n+3;
     721       19572 :   GEN y = cgetg(ly, t_POL);
     722       19572 :   y[1] = evalsigne(1);
     723      248426 :   for (j=2; j<ly; j++)
     724             :   {
     725             :     long k;
     726      228854 :     GEN a = cgetg(nx+2,t_POL);
     727      228854 :     a[1] = evalsigne(1) | evalvarn(w);
     728     1169632 :     for (k=0; k<nx; k++)
     729             :     {
     730      940778 :       GEN xk = gel(x,k);
     731      940778 :       if (typ(xk)==t_POL)
     732      865521 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     733             :       else
     734       75257 :         gel(a,k+2) = j==2 ? xk: gen_0;
     735             :     }
     736      228854 :     gel(y,j) = normalizepol_lg(a, nx+2);
     737             :   }
     738       19572 :   return normalizepol_lg(y,ly);
     739             : }
     740             : 
     741             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     742             : GEN
     743         952 : RgXY_swap(GEN x, long n, long w)
     744             : {
     745         952 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     746         952 :   setvarn(z, varn(x)); return z;
     747             : }
     748             : 
     749             : long
     750         176 : RgXY_degreex(GEN b)
     751             : {
     752         176 :   long deg = -1, i;
     753         176 :   if (!signe(b)) return -1;
     754         990 :   for (i = 2; i < lg(b); ++i)
     755             :   {
     756         814 :     GEN bi = gel(b, i);
     757         814 :     if (typ(bi) == t_POL)
     758         785 :       deg = maxss(deg, degpol(bi));
     759             :   }
     760         176 :   return deg;
     761             : }
     762             : 
     763             : /* return (x % X^n). Shallow */
     764             : GEN
     765     2268934 : RgXn_red_shallow(GEN a, long n)
     766             : {
     767     2268934 :   long i, L = n+2, l = lg(a);
     768             :   GEN  b;
     769     2268934 :   if (L >= l) return a; /* deg(x) < n */
     770     1570064 :   b = cgetg(L, t_POL); b[1] = a[1];
     771     1570064 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     772     1570064 :   return normalizepol_lg(b,L);
     773             : }
     774             : 
     775             : GEN
     776         357 : RgXnV_red_shallow(GEN x, long n)
     777         357 : { pari_APPLY_type(t_VEC, RgXn_red_shallow(gel(x,i), n)) }
     778             : 
     779             : /* return (x * X^n). Shallow */
     780             : GEN
     781    70681185 : RgX_shift_shallow(GEN a, long n)
     782             : {
     783    70681185 :   long i, l = lg(a);
     784             :   GEN  b;
     785    70681185 :   if (l == 2 || !n) return a;
     786    51481702 :   l += n;
     787    51481702 :   if (n < 0)
     788             :   {
     789    42954426 :     if (l <= 2) return pol_0(varn(a));
     790    42929076 :     b = cgetg(l, t_POL); b[1] = a[1];
     791    42929078 :     a -= n;
     792    42929078 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     793             :   } else {
     794     8527276 :     b = cgetg(l, t_POL); b[1] = a[1];
     795     8527276 :     a -= n; n += 2;
     796     8527276 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     797     8527276 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     798             :   }
     799    51456354 :   return b;
     800             : }
     801             : /* return (x * X^n). */
     802             : GEN
     803     1580052 : RgX_shift(GEN a, long n)
     804             : {
     805     1580052 :   long i, l = lg(a);
     806             :   GEN  b;
     807     1580052 :   if (l == 2 || !n) return RgX_copy(a);
     808     1579772 :   l += n;
     809     1579772 :   if (n < 0)
     810             :   {
     811         840 :     if (l <= 2) return pol_0(varn(a));
     812         798 :     b = cgetg(l, t_POL); b[1] = a[1];
     813         798 :     a -= n;
     814         798 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     815             :   } else {
     816     1578932 :     b = cgetg(l, t_POL); b[1] = a[1];
     817     1578932 :     a -= n; n += 2;
     818     1578932 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     819     1578932 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     820             :   }
     821     1579730 :   return b;
     822             : }
     823             : 
     824             : GEN
     825      316113 : RgX_rotate_shallow(GEN P, long k, long p)
     826             : {
     827      316113 :   long i, l = lgpol(P);
     828             :   GEN r;
     829      316113 :   if (signe(P)==0)
     830         623 :     return pol_0(varn(P));
     831      315490 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     832     2094904 :   for(i=0; i<p; i++)
     833             :   {
     834     1779414 :     long s = 2+(i+k)%p;
     835     1779414 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     836             :   }
     837      315490 :   return RgX_renormalize(r);
     838             : }
     839             : 
     840             : GEN
     841     2974975 : RgX_mulXn(GEN x, long d)
     842             : {
     843             :   pari_sp av;
     844             :   GEN z;
     845             :   long v;
     846     2974975 :   if (d >= 0) return RgX_shift(x, d);
     847     1463394 :   d = -d;
     848     1463394 :   v = RgX_val(x);
     849     1463394 :   if (v >= d) return RgX_shift(x, -d);
     850     1463380 :   av = avma;
     851     1463380 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     852     1463380 :   return gerepileupto(av, z);
     853             : }
     854             : 
     855             : long
     856     2488722 : RgX_val(GEN x)
     857             : {
     858     2488722 :   long i, lx = lg(x);
     859     2488722 :   if (lx == 2) return LONG_MAX;
     860     2521006 :   for (i = 2; i < lx; i++)
     861     2520964 :     if (!isexactzero(gel(x,i))) break;
     862     2488582 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     863     2488540 :   return i - 2;
     864             : }
     865             : long
     866    38460071 : RgX_valrem(GEN x, GEN *Z)
     867             : {
     868    38460071 :   long v, i, lx = lg(x);
     869    38460071 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     870    73038394 :   for (i = 2; i < lx; i++)
     871    73038394 :     if (!isexactzero(gel(x,i))) break;
     872             :   /* possible with non-rational zeros */
     873    38460071 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     874    38460071 :   v = i - 2;
     875    38460071 :   *Z = RgX_shift_shallow(x, -v);
     876    38460071 :   return v;
     877             : }
     878             : long
     879       14977 : RgX_valrem_inexact(GEN x, GEN *Z)
     880             : {
     881             :   long v;
     882       14977 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     883       15453 :   for (v = 0;; v++)
     884       15936 :     if (!gequal0(gel(x,2+v))) break;
     885       14970 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     886       14970 :   return v;
     887             : }
     888             : 
     889             : GEN
     890      109186 : RgXQC_red(GEN x, GEN T)
     891      109186 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
     892             : 
     893             : GEN
     894        1106 : RgXQV_red(GEN x, GEN T)
     895        1106 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
     896             : 
     897             : GEN
     898       14826 : RgXQM_red(GEN x, GEN T)
     899       14826 : { pari_APPLY_same(RgXQC_red(gel(x,i), T)) }
     900             : 
     901             : GEN
     902           0 : RgXQM_mul(GEN P, GEN Q, GEN T)
     903             : {
     904           0 :   return RgXQM_red(RgM_mul(P, Q), T);
     905             : }
     906             : 
     907             : GEN
     908      424499 : RgXQX_red(GEN P, GEN T)
     909             : {
     910      424499 :   long i, l = lg(P);
     911      424499 :   GEN Q = cgetg(l, t_POL);
     912      424499 :   Q[1] = P[1];
     913      424499 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     914      424498 :   return normalizepol_lg(Q, l);
     915             : }
     916             : 
     917             : GEN
     918      248181 : RgX_deriv(GEN x)
     919             : {
     920      248181 :   long i,lx = lg(x)-1;
     921             :   GEN y;
     922             : 
     923      248181 :   if (lx<3) return pol_0(varn(x));
     924      220083 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     925      220083 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     926      220083 :   y[1] = x[1]; return normalizepol_lg(y,i);
     927             : }
     928             : 
     929             : GEN
     930      530813 : RgX_recipspec_shallow(GEN x, long l, long n)
     931             : {
     932             :   long i;
     933      530813 :   GEN z = cgetg(n+2,t_POL);
     934      530822 :   z[1] = 0; z += 2;
     935    17454878 :   for(i=0; i<l; i++)
     936    16924056 :     gel(z,n-i-1) = gel(x,i);
     937      658171 :   for(   ; i<n; i++)
     938      127349 :     gel(z, n-i-1) = gen_0;
     939      530822 :   return normalizepol_lg(z-2,n+2);
     940             : }
     941             : 
     942             : GEN
     943       45452 : RgXn_recip_shallow(GEN P, long n)
     944             : {
     945       45452 :   GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
     946       45452 :   setvarn(Q, varn(P));
     947       45452 :   return Q;
     948             : }
     949             : 
     950             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     951             : GEN
     952        2170 : RgX_recip(GEN x)
     953             : {
     954             :   long lx, i, j;
     955        2170 :   GEN y = cgetg_copy(x, &lx);
     956        2170 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     957        2170 :   return normalizepol_lg(y,lx);
     958             : }
     959             : /* shallow version */
     960             : GEN
     961      434680 : RgX_recip_shallow(GEN x)
     962             : {
     963             :   long lx, i, j;
     964      434680 :   GEN y = cgetg_copy(x, &lx);
     965      434708 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     966      434708 :   return y;
     967             : }
     968             : /*******************************************************************/
     969             : /*                                                                 */
     970             : /*                      ADDITION / SUBTRACTION                     */
     971             : /*                                                                 */
     972             : /*******************************************************************/
     973             : /* same variable */
     974             : GEN
     975    20470073 : RgX_add(GEN x, GEN y)
     976             : {
     977    20470073 :   long i, lx = lg(x), ly = lg(y);
     978             :   GEN z;
     979    20470073 :   if (ly <= lx) {
     980    18123461 :     z = cgetg(lx,t_POL); z[1] = x[1];
     981    18123461 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     982    18123461 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
     983    18123461 :     z = normalizepol_lg(z, lx);
     984             :   } else {
     985     2346612 :     z = cgetg(ly,t_POL); z[1] = y[1];
     986     2346612 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     987     2346612 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
     988     2346612 :     z = normalizepol_lg(z, ly);
     989             :   }
     990    20470073 :   return z;
     991             : }
     992             : GEN
     993    22327625 : RgX_sub(GEN x, GEN y)
     994             : {
     995    22327625 :   long i, lx = lg(x), ly = lg(y);
     996             :   GEN z;
     997    22327625 :   if (ly <= lx) {
     998    18882957 :     z = cgetg(lx,t_POL); z[1] = x[1];
     999    18882957 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1000    18882957 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1001    18882957 :     z = normalizepol_lg(z, lx);
    1002             :   } else {
    1003     3444668 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1004     3444668 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1005     3444668 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1006     3444668 :     z = normalizepol_lg(z, ly);
    1007             :   }
    1008    22327625 :   return z;
    1009             : }
    1010             : GEN
    1011     1299089 : RgX_neg(GEN x)
    1012             : {
    1013     1299089 :   long i, lx = lg(x);
    1014     1299089 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1015     1299091 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1016     1299089 :   return y;
    1017             : }
    1018             : 
    1019             : GEN
    1020    12592868 : RgX_Rg_add(GEN y, GEN x)
    1021             : {
    1022             :   GEN z;
    1023    12592868 :   long lz = lg(y), i;
    1024    12592868 :   if (lz == 2) return scalarpol(x,varn(y));
    1025     9610926 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1026     9610926 :   gel(z,2) = gadd(gel(y,2),x);
    1027     9610927 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1028             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1029             :    * an inexact 0 */
    1030     9610926 :   return normalizepol_lg(z,lz);
    1031             : }
    1032             : GEN
    1033        2837 : RgX_Rg_add_shallow(GEN y, GEN x)
    1034             : {
    1035             :   GEN z;
    1036        2837 :   long lz = lg(y), i;
    1037        2837 :   if (lz == 2) return scalarpol(x,varn(y));
    1038        2837 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1039        2837 :   gel(z,2) = gadd(gel(y,2),x);
    1040        2837 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1041        2837 :   return z = normalizepol_lg(z,lz);
    1042             : }
    1043             : GEN
    1044       32748 : RgX_Rg_sub(GEN y, GEN x)
    1045             : {
    1046             :   GEN z;
    1047       32748 :   long lz = lg(y), i;
    1048       32748 :   if (lz == 2)
    1049             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1050        2576 :     long v = varn(y);
    1051        2576 :     if (isrationalzero(x)) return pol_0(v);
    1052          14 :     z = cgetg(3,t_POL);
    1053          28 :     z[1] = gequal0(x)? evalvarn(v)
    1054          14 :                    : evalvarn(v) | evalsigne(1);
    1055          14 :     gel(z,2) = gneg(x); return z;
    1056             :   }
    1057       30172 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1058       30172 :   gel(z,2) = gsub(gel(y,2),x);
    1059       30172 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1060       30172 :   return z = normalizepol_lg(z,lz);
    1061             : }
    1062             : GEN
    1063      532168 : Rg_RgX_sub(GEN x, GEN y)
    1064             : {
    1065             :   GEN z;
    1066      532168 :   long lz = lg(y), i;
    1067      532168 :   if (lz == 2) return scalarpol(x,varn(y));
    1068      531062 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1069      531062 :   gel(z,2) = gsub(x, gel(y,2));
    1070      531062 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1071      531062 :   return z = normalizepol_lg(z,lz);
    1072             : }
    1073             : /*******************************************************************/
    1074             : /*                                                                 */
    1075             : /*                  KARATSUBA MULTIPLICATION                       */
    1076             : /*                                                                 */
    1077             : /*******************************************************************/
    1078             : #if 0
    1079             : /* to debug Karatsuba-like routines */
    1080             : GEN
    1081             : zx_debug_spec(GEN x, long nx)
    1082             : {
    1083             :   GEN z = cgetg(nx+2,t_POL);
    1084             :   long i;
    1085             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1086             :   z[1] = evalsigne(1); return z;
    1087             : }
    1088             : 
    1089             : GEN
    1090             : RgX_debug_spec(GEN x, long nx)
    1091             : {
    1092             :   GEN z = cgetg(nx+2,t_POL);
    1093             :   long i;
    1094             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1095             :   z[1] = evalsigne(1); return z;
    1096             : }
    1097             : #endif
    1098             : 
    1099             : /* generic multiplication */
    1100             : GEN
    1101     3392974 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1102             : {
    1103             :   GEN z, t;
    1104             :   long i;
    1105     3392974 :   if (nx == ny) {
    1106      772008 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1107      772008 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1108      772008 :     return normalizepol_lg(z, nx+2);
    1109             :   }
    1110     2620966 :   if (ny < nx) {
    1111     2466034 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1112     2466034 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1113     2466034 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1114     2466034 :     return normalizepol_lg(z, nx+2);
    1115             :   } else {
    1116      154932 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1117      154932 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1118      154932 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1119      154932 :     return normalizepol_lg(z, ny+2);
    1120             :   }
    1121             : }
    1122             : GEN
    1123      209438 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1124             : {
    1125             :   GEN z, t;
    1126             :   long i;
    1127      209438 :   if (nx == ny) {
    1128       12425 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1129       12425 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1130       12425 :     return normalizepol_lg(z, nx+2);
    1131             :   }
    1132      197013 :   if (ny < nx) {
    1133      195312 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1134      195312 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1135      195312 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1136      195312 :     return normalizepol_lg(z, nx+2);
    1137             :   } else {
    1138        1701 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1139        1701 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1140        1701 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1141        1701 :     return normalizepol_lg(z, ny+2);
    1142             :   }
    1143             : }
    1144             : 
    1145             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1146             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1147             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1148             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1149             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1150             : static GEN
    1151    21264980 : RgXspec_kill0(GEN x, long lx)
    1152             : {
    1153    21264980 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1154             :   long i;
    1155    86323975 :   for (i=0; i <lx; i++)
    1156             :   {
    1157    65058995 :     GEN c = gel(x,i);
    1158    65058995 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1159             :   }
    1160    21264980 :   return z;
    1161             : }
    1162             : 
    1163             : INLINE GEN
    1164    47732224 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1165             : {
    1166    47732224 :   pari_sp av = avma;
    1167    47732224 :   GEN s = NULL;
    1168             :   long i;
    1169             : 
    1170   227072222 :   for (i=a; i<b; i++)
    1171   179339998 :     if (gel(y,i) && gel(x,-i))
    1172             :     {
    1173   101329056 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1174   101329056 :       s = s? gadd(s, t): t;
    1175             :     }
    1176    47732224 :   return s? gerepileupto(av, s): gen_0;
    1177             : }
    1178             : 
    1179             : /* assume nx >= ny > 0, return x * y * t^v */
    1180             : static GEN
    1181     8465582 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1182             : {
    1183             :   long i, lz, nz;
    1184             :   GEN z;
    1185             : 
    1186     8465582 :   x = RgXspec_kill0(x,nx);
    1187     8465582 :   y = RgXspec_kill0(y,ny);
    1188     8465582 :   lz = nx + ny + 1; nz = lz-2;
    1189     8465582 :   lz += v;
    1190     8465582 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1191     8465582 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1192     8465582 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1193     8465582 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1194     8465582 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1195     8465582 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1196             : }
    1197             : 
    1198             : /* return (x * X^d) + y. Assume d > 0 */
    1199             : GEN
    1200     2527941 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1201             : {
    1202             :   GEN x, y, xd, yd, zd;
    1203             :   long a, lz, nx, ny;
    1204             : 
    1205     2527941 :   if (!signe(x0)) return y0;
    1206     2501878 :   ny = lgpol(y0);
    1207     2501878 :   nx = lgpol(x0);
    1208     2501878 :   zd = (GEN)avma;
    1209     2501878 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1210     2501878 :   if (a <= 0)
    1211             :   {
    1212      203495 :     lz = nx+d+2;
    1213      203495 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1214      203495 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1215      203495 :     x = zd + a;
    1216      203495 :     while (zd > x) gel(--zd,0) = gen_0;
    1217             :   }
    1218             :   else
    1219             :   {
    1220     2298383 :     xd = new_chunk(d); yd = y+d;
    1221     2298383 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1222     2298383 :     lz = (a>nx)? ny+2: lg(x)+d;
    1223     2298383 :     x += 2; while (xd > x) *--zd = *--xd;
    1224             :   }
    1225     2501878 :   while (yd > y) *--zd = *--yd;
    1226     2501878 :   *--zd = x0[1];
    1227     2501878 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1228             : }
    1229             : GEN
    1230      501398 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1231             : {
    1232             :   GEN x, y, xd, yd, zd;
    1233             :   long a, lz, nx, ny;
    1234             : 
    1235      501398 :   if (!signe(x0)) return RgX_copy(y0);
    1236      500821 :   nx = lgpol(x0);
    1237      500821 :   ny = lgpol(y0);
    1238      500821 :   zd = (GEN)avma;
    1239      500821 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1240      500821 :   if (a <= 0)
    1241             :   {
    1242      291383 :     lz = nx+d+2;
    1243      291383 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1244      291383 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1245      291383 :     x = zd + a;
    1246      291383 :     while (zd > x) gel(--zd,0) = gen_0;
    1247             :   }
    1248             :   else
    1249             :   {
    1250      209438 :     xd = new_chunk(d); yd = y+d;
    1251      209438 :     x = RgX_addspec(x,yd, nx,a);
    1252      209438 :     lz = (a>nx)? ny+2: lg(x)+d;
    1253      209438 :     x += 2; while (xd > x) *--zd = *--xd;
    1254             :   }
    1255      500821 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1256      500821 :   *--zd = x0[1];
    1257      500821 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1258             : }
    1259             : 
    1260             : /* return x * y mod t^n */
    1261             : static GEN
    1262     2163602 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1263             : {
    1264     2163602 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1265             :   GEN z;
    1266     2163602 :   if (lx < 0) return pol_0(varn(x));
    1267     2163602 :   if (ly < 0) return pol_0(varn(x));
    1268     2163602 :   z = cgetg(lz, t_POL) + 2;
    1269     2163602 :   x+=2; if (lx > n) lx = n;
    1270     2163602 :   y+=2; if (ly > n) ly = n;
    1271     2163602 :   z[-1] = x[-1];
    1272     2163602 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1273     2163602 :   x = RgXspec_kill0(x, lx);
    1274     2163602 :   y = RgXspec_kill0(y, ly);
    1275             :   /* x:y:z [i] = term of degree i */
    1276     2163602 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1277     2163602 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1278     2163602 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1279     2163602 :   return normalizepol_lg(z - 2, lz);
    1280             : }
    1281             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1282             : static GEN
    1283     2253580 : RgXn_mul2(GEN f, GEN g, long n)
    1284             : {
    1285     2253580 :   pari_sp av = avma;
    1286             :   GEN fe,fo, ge,go, l,h,m;
    1287             :   long n0, n1;
    1288     2253580 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1289     2193548 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1290       29946 :   n0 = n>>1; n1 = n-n0;
    1291       29946 :   RgX_even_odd(f, &fe, &fo);
    1292       29946 :   RgX_even_odd(g, &ge, &go);
    1293       29946 :   l = RgXn_mul(fe,ge,n1);
    1294       29946 :   h = RgXn_mul(fo,go,n0);
    1295       29946 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1296             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1297             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1298       29946 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1299             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1300       29946 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1301       29946 :   m = RgX_inflate(m,2);
    1302             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1303       29946 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1304       29946 :   h = RgX_inflate(h,2);
    1305       29946 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1306       29946 :   return gerepileupto(av, h);
    1307             : }
    1308             : /* (f*g) \/ x^n */
    1309             : static GEN
    1310        3044 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1311             : {
    1312        3044 :   long d = degpol(f)+degpol(g) + 1 - n;
    1313             :   GEN h;
    1314        3044 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1315        1886 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1316             :                                  RgX_recip_shallow(g), d));
    1317        1886 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1318             : }
    1319             : 
    1320             : /* (f*g) \/ x^n */
    1321             : static GEN
    1322           0 : RgX_sqrhigh_i2(GEN f, long n)
    1323             : {
    1324           0 :   long d = 2*degpol(f)+ 1 - n;
    1325             :   GEN h;
    1326           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1327           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1328           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1329             : }
    1330             : 
    1331             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1332             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1333             :  * Only c, c0, c1, c2 are genuine GEN.
    1334             :  */
    1335             : GEN
    1336     9037302 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1337             : {
    1338             :   GEN a0, c, c0;
    1339     9037302 :   long n0, n0a, i, v = 0;
    1340             :   pari_sp av;
    1341             : 
    1342     9037302 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1343     9037302 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1344     9037302 :   if (na < nb) swapspec(a,b, na,nb);
    1345     9037302 :   if (!nb) return pol_0(0);
    1346             : 
    1347     8936995 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1348      471413 :   RgX_shift_inplace_init(v);
    1349      471413 :   i = (na>>1); n0 = na-i; na = i;
    1350      471413 :   av = avma; a0 = a+n0; n0a = n0;
    1351      471413 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1352             : 
    1353      471413 :   if (nb > n0)
    1354             :   {
    1355             :     GEN b0,c1,c2;
    1356             :     long n0b;
    1357             : 
    1358      470040 :     nb -= n0; b0 = b+n0; n0b = n0;
    1359      470040 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1360      470040 :     c = RgX_mulspec(a,b,n0a,n0b);
    1361      470040 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1362             : 
    1363      470040 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1364      470040 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1365             : 
    1366      470040 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1367      470040 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1368      470040 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1369             :   }
    1370             :   else
    1371             :   {
    1372        1373 :     c = RgX_mulspec(a,b,n0a,nb);
    1373        1373 :     c0 = RgX_mulspec(a0,b,na,nb);
    1374             :   }
    1375      471413 :   c0 = RgX_addmulXn(c0,c,n0);
    1376      471413 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1377             : }
    1378             : 
    1379             : INLINE GEN
    1380       34870 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1381             : {
    1382       34870 :   pari_sp av = avma;
    1383       34870 :   GEN s = NULL;
    1384       34870 :   long j, l = (i+1)>>1;
    1385      101073 :   for (j=a; j<l; j++)
    1386             :   {
    1387       66203 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1388       66203 :     if (xj && xx)
    1389             :     {
    1390       58881 :       GEN t = gmul(xj, xx);
    1391       58881 :       s = s? gadd(s, t): t;
    1392             :     }
    1393             :   }
    1394       34870 :   if (s) s = gshift(s,1);
    1395       34870 :   if ((i&1) == 0)
    1396             :   {
    1397       20741 :     GEN t = gel(x, i>>1);
    1398       20741 :     if (t) {
    1399       18424 :       t = gsqr(t);
    1400       18424 :       s = s? gadd(s, t): t;
    1401             :     }
    1402             :   }
    1403       34870 :   return s? gerepileupto(av,s): gen_0;
    1404             : }
    1405             : static GEN
    1406        6612 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1407             : {
    1408             :   long i, lz, nz;
    1409             :   GEN z;
    1410             : 
    1411        6612 :   if (!nx) return pol_0(0);
    1412        6612 :   x = RgXspec_kill0(x,nx);
    1413        6612 :   lz = (nx << 1) + 1, nz = lz-2;
    1414        6612 :   lz += v;
    1415        6612 :   z = cgetg(lz,t_POL) + 2;
    1416        6612 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1417        6612 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1418        6612 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1419        6612 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1420             : }
    1421             : /* return x^2 mod t^n */
    1422             : static GEN
    1423           0 : RgXn_sqr_basecase(GEN x, long n)
    1424             : {
    1425           0 :   long i, lz = n+2, lx = lgpol(x);
    1426             :   GEN z;
    1427           0 :   if (lx < 0) return pol_0(varn(x));
    1428           0 :   z = cgetg(lz, t_POL);
    1429           0 :   z[1] = x[1];
    1430           0 :   x+=2; if (lx > n) lx = n;
    1431           0 :   x = RgXspec_kill0(x,lx);
    1432           0 :   z+=2;/* x:z [i] = term of degree i */
    1433           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1434           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1435           0 :   z -= 2; return normalizepol_lg(z, lz);
    1436             : }
    1437             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1438             : static GEN
    1439         238 : RgXn_sqr2(GEN f, long n)
    1440             : {
    1441         238 :   pari_sp av = avma;
    1442             :   GEN fe,fo, l,h,m;
    1443             :   long n0, n1;
    1444         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1445           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1446           0 :   n0 = n>>1; n1 = n-n0;
    1447           0 :   RgX_even_odd(f, &fe, &fo);
    1448           0 :   l = RgXn_sqr(fe,n1);
    1449           0 :   h = RgXn_sqr(fo,n0);
    1450           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1451             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1452             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1453           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1454             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1455           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1456           0 :   m = RgX_inflate(m,2);
    1457             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1458           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1459           0 :   h = RgX_inflate(h,2);
    1460           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1461           0 :   return gerepileupto(av, h);
    1462             : }
    1463             : GEN
    1464        6651 : RgX_sqrspec(GEN a, long na)
    1465             : {
    1466             :   GEN a0, c, c0, c1;
    1467        6651 :   long n0, n0a, i, v = 0;
    1468             :   pari_sp av;
    1469             : 
    1470        6651 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1471        6651 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1472          39 :   RgX_shift_inplace_init(v);
    1473          39 :   i = (na>>1); n0 = na-i; na = i;
    1474          39 :   av = avma; a0 = a+n0; n0a = n0;
    1475          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1476             : 
    1477          39 :   c = RgX_sqrspec(a,n0a);
    1478          39 :   c0 = RgX_sqrspec(a0,na);
    1479          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1480          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1481          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1482          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1483             : }
    1484             : 
    1485             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1486             : GEN
    1487      749409 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1488             : {
    1489      749409 :   GEN z = RgX_mul(A, B);
    1490      749409 :   if (a < b)
    1491        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1492      743963 :   else if (a > b)
    1493      449852 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1494             :   else
    1495      294111 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1496      749409 :   return z;
    1497             : }
    1498             : 
    1499             : GEN
    1500     7624397 : RgX_mul_i(GEN x, GEN y)
    1501             : {
    1502     7624397 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1503     7624397 :   setvarn(z, varn(x)); return z;
    1504             : }
    1505             : 
    1506             : GEN
    1507        6573 : RgX_sqr_i(GEN x)
    1508             : {
    1509        6573 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1510        6573 :   setvarn(z,varn(x)); return z;
    1511             : }
    1512             : 
    1513             : /*******************************************************************/
    1514             : /*                                                                 */
    1515             : /*                               DIVISION                          */
    1516             : /*                                                                 */
    1517             : /*******************************************************************/
    1518             : GEN
    1519      455076 : RgX_Rg_divexact(GEN x, GEN y) {
    1520      455076 :   long i, lx = lg(x);
    1521             :   GEN z;
    1522      455076 :   if (lx == 2) return gcopy(x);
    1523      422309 :   switch(typ(y))
    1524             :   {
    1525             :     case t_INT:
    1526      401968 :       if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1527      370062 :       break;
    1528        5985 :     case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
    1529             :   }
    1530      384418 :   z = cgetg(lx, t_POL); z[1] = x[1];
    1531      384418 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1532      384418 :   return z;
    1533             : }
    1534             : GEN
    1535    23768611 : RgX_Rg_div(GEN x, GEN y) {
    1536    23768611 :   long i, lx = lg(x);
    1537             :   GEN z;
    1538    23768611 :   if (lx == 2) return gcopy(x);
    1539    23538909 :   switch(typ(y))
    1540             :   {
    1541             :     case t_INT:
    1542    14425804 :       if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1543      694048 :       break;
    1544        4270 :     case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
    1545             :   }
    1546     9802883 :   z = cgetg(lx, t_POL); z[1] = x[1];
    1547     9802883 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1548     9802883 :   return normalizepol_lg(z, lx);
    1549             : }
    1550             : GEN
    1551       13223 : RgX_normalize(GEN x)
    1552             : {
    1553       13223 :   GEN z, d = NULL;
    1554       13223 :   long i, n = lg(x)-1;
    1555       13223 :   for (i = n; i > 1; i--) { d = gel(x,i); if (!gequal0(d)) break; }
    1556       13223 :   if (i == 1) return pol_0(varn(x));
    1557       13223 :   if (i == n && isint1(d)) return x;
    1558        4893 :   n = i; z = cgetg(n+1, t_POL); z[1] = x[1];
    1559        4893 :   for (i=2; i<n; i++) gel(z,i) = gdiv(gel(x,i),d);
    1560        4893 :   gel(z,n) = Rg_get_1(d); return z;
    1561             : }
    1562             : GEN
    1563        2625 : RgX_divs(GEN x, long y) {
    1564             :   long i, lx;
    1565        2625 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1566        2625 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1567        2625 :   return normalizepol_lg(z, lx);
    1568             : }
    1569             : GEN
    1570       50858 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1571             : {
    1572       50858 :   long l = lg(a), i;
    1573       50858 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1574       50858 :   z[1] = a[1];
    1575       50858 :   a0 = a + l-1;
    1576       50858 :   z0 = z + l-2; *z0 = *a0--;
    1577      872740 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1578             :   {
    1579      821882 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1580      821882 :     gel(z0,0) = t;
    1581             :   }
    1582       50858 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1583       50858 :   return z;
    1584             : }
    1585             : /* Polynomial division x / y:
    1586             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1587             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1588             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1589             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1590             : static GEN
    1591    12453746 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1592             : {
    1593             :   pari_sp avy, av, av1;
    1594             :   long dx,dy,dz,i,j,sx,lr;
    1595             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1596             :   GEN (*f)(GEN,GEN);
    1597             : 
    1598    12453746 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1599             : 
    1600    12453746 :   dy = degpol(y);
    1601    12453746 :   y_lead = gel(y,dy+2);
    1602    12453746 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1603             :   {
    1604           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1605           0 :     for (dy--; dy>=0; dy--)
    1606             :     {
    1607           0 :       y_lead = gel(y,dy+2);
    1608           0 :       if (!gequal0(y_lead)) break;
    1609             :     }
    1610             :   }
    1611    12453746 :   if (!dy) /* y is constant */
    1612             :   {
    1613        3016 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1614        3016 :     z = RgX_Rg_div(x, y_lead);
    1615        3016 :     if (pr == ONLY_DIVIDES) return z;
    1616        1196 :     if (pr) *pr = pol_0(varn(x));
    1617        1196 :     return z;
    1618             :   }
    1619    12450730 :   dx = degpol(x);
    1620    12450730 :   if (dx < dy)
    1621             :   {
    1622      663244 :     if (pr == ONLY_REM) return RgX_copy(x);
    1623      371705 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1624      371684 :     z = pol_0(varn(x));
    1625      371684 :     if (pr) *pr = RgX_copy(x);
    1626      371684 :     return z;
    1627             :   }
    1628             : 
    1629             :   /* x,y in R[X], y non constant */
    1630    11787486 :   av = avma;
    1631    11787486 :   p = NULL;
    1632    11787486 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1633             :   {
    1634      225841 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1635      225841 :     if (!z) return gc_NULL(av);
    1636      225841 :     z = FpX_to_mod(z, p);
    1637      225841 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1638      121681 :       return gerepileupto(av, z);
    1639      104160 :     *pr = FpX_to_mod(*pr, p);
    1640      104160 :     gerepileall(av, 2, pr, &z);
    1641      104160 :     return z;
    1642             :   }
    1643    11561645 :   switch(typ(y_lead))
    1644             :   {
    1645             :     case t_REAL:
    1646           0 :       y_lead = ginv(y_lead);
    1647           0 :       f = gmul; mod = NULL;
    1648           0 :       break;
    1649             :     case t_INTMOD:
    1650        2246 :     case t_POLMOD: y_lead = ginv(y_lead);
    1651        2246 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1652        2246 :       break;
    1653    11559399 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1654    11559399 :       f = gdiv; mod = NULL;
    1655             :   }
    1656             : 
    1657    11561645 :   if (y_lead == NULL)
    1658     9777575 :     p2 = gel(x,dx+2);
    1659             :   else {
    1660             :     for(;;) {
    1661     1784070 :       p2 = f(gel(x,dx+2),y_lead);
    1662     1784070 :       p2 = simplify_shallow(p2);
    1663     1784070 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1664             :     }
    1665     1784070 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1666             :     {
    1667           0 :       if (pr == ONLY_DIVIDES) {
    1668           0 :         set_avma(av);
    1669           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1670             :       }
    1671           0 :       if (pr == ONLY_REM)
    1672             :       {
    1673           0 :         if (dx < 0)
    1674           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1675             :         else
    1676             :         {
    1677             :           GEN t;
    1678           0 :           set_avma(av);
    1679           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1680           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1681           0 :           return t;
    1682             :         }
    1683             :       }
    1684           0 :       if (pr) /* cf ONLY_REM above */
    1685             :       {
    1686           0 :         if (dx < 0)
    1687             :         {
    1688           0 :           p2 = gclone(p2);
    1689           0 :           set_avma(av);
    1690           0 :           z = pol_0(varn(x));
    1691           0 :           x = scalarpol(p2, varn(x));
    1692           0 :           gunclone(p2);
    1693             :         }
    1694             :         else
    1695             :         {
    1696             :           GEN t;
    1697           0 :           set_avma(av);
    1698           0 :           z = pol_0(varn(x));
    1699           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1700           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1701           0 :           x = t;
    1702             :         }
    1703           0 :         *pr = x;
    1704             :       }
    1705             :       else
    1706             :       {
    1707           0 :         set_avma(av);
    1708           0 :         z = pol_0(varn(x));
    1709             :       }
    1710           0 :       return z;
    1711             :     }
    1712             :   }
    1713             :   /* dx >= dy */
    1714    11561645 :   avy = avma;
    1715    11561645 :   dz = dx-dy;
    1716    11561645 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1717    11561645 :   x += 2;
    1718    11561645 :   z += 2;
    1719    11561645 :   y += 2;
    1720    11561645 :   gel(z,dz) = gcopy(p2);
    1721             : 
    1722    28846251 :   for (i=dx-1; i>=dy; i--)
    1723             :   {
    1724    17284606 :     av1=avma; p1=gel(x,i);
    1725    17284606 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1726    17284606 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1727             : 
    1728    17284606 :     if (isrationalzero(p1)) { set_avma(av1); p1 = gen_0; }
    1729             :     else
    1730     7732990 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1731    17284606 :     gel(z,i-dy) = p1;
    1732             :   }
    1733    11561645 :   if (!pr) return gerepileupto(av,z-2);
    1734             : 
    1735     4877209 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1736     5236890 :   for (sx=0; ; i--)
    1737             :   {
    1738     5596571 :     p1 = gel(x,i);
    1739             :     /* we always enter this loop at least once */
    1740     5236890 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1741     5236890 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1742     5236890 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1743     3430664 :     if (!isexactzero(p1)) break;
    1744     3416165 :     if (!i) break;
    1745      359681 :     set_avma(av1);
    1746             :   }
    1747     4877209 :   if (pr == ONLY_DIVIDES)
    1748             :   {
    1749        1659 :     if (sx) return gc_NULL(av);
    1750        1652 :     set_avma((pari_sp)rem); return gerepileupto(av,z-2);
    1751             :   }
    1752     4875550 :   lr=i+3; rem -= lr;
    1753     4875550 :   if (avma==av1) { set_avma((pari_sp)rem); p1 = gcopy(p1); }
    1754     4831938 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1755     4875550 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1756     4875550 :   rem[1] = z[-1];
    1757     4875550 :   rem += 2;
    1758     4875550 :   gel(rem,i) = p1;
    1759     6191642 :   for (i--; i>=0; i--)
    1760             :   {
    1761     1316092 :     av1=avma; p1 = gel(x,i);
    1762     1316092 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1763     1316092 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1764     1316092 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1765             :   }
    1766     4875550 :   rem -= 2;
    1767     4875550 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1768     4875550 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1769     4113182 :   z -= 2;
    1770             :   {
    1771     4113182 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1772     4113182 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1773             :   }
    1774             : }
    1775             : 
    1776             : GEN
    1777    11399839 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1778             : {
    1779    11399839 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1780    11399839 :   return RgX_divrem_i(x, y, pr);
    1781             : }
    1782             : 
    1783             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1784             : GEN
    1785       74130 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1786             : {
    1787             :   long vx, dx, dy, dz, i, j, sx, lr;
    1788             :   pari_sp av0, av, tetpil;
    1789             :   GEN z,p1,rem,lead;
    1790             : 
    1791       74130 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1792       74130 :   vx = varn(x);
    1793       74130 :   dx = degpol(x);
    1794       74130 :   dy = degpol(y);
    1795       74130 :   if (dx < dy)
    1796             :   {
    1797       24213 :     if (pr)
    1798             :     {
    1799       24213 :       av0 = avma; x = RgXQX_red(x, T);
    1800       24213 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: gen_0; }
    1801       24213 :       if (pr == ONLY_REM) return x;
    1802           0 :       *pr = x;
    1803             :     }
    1804           0 :     return pol_0(vx);
    1805             :   }
    1806       49917 :   lead = leading_coeff(y);
    1807       49917 :   if (!dy) /* y is constant */
    1808             :   {
    1809         546 :     if (pr && pr != ONLY_DIVIDES)
    1810             :     {
    1811           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1812           0 :       *pr = pol_0(vx);
    1813             :     }
    1814         546 :     if (gequal1(lead)) return RgX_copy(x);
    1815           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1816           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1817             :   }
    1818       49371 :   av0 = avma; dz = dx-dy;
    1819       49371 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1820       49371 :   set_avma(av0);
    1821       49371 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1822       49371 :   x += 2; y += 2; z += 2;
    1823             : 
    1824       49371 :   p1 = gel(x,dx); av = avma;
    1825       49371 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1826      292939 :   for (i=dx-1; i>=dy; i--)
    1827             :   {
    1828      243568 :     av=avma; p1=gel(x,i);
    1829      243568 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1830      243568 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1831      243568 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1832             :   }
    1833       49371 :   if (!pr) { guncloneNULL(lead); return z-2; }
    1834             : 
    1835       48125 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1836       78147 :   for (sx=0; ; i--)
    1837             :   {
    1838      108169 :     p1 = gel(x,i);
    1839       78147 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1840       78147 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1841       35644 :     if (!i) break;
    1842       30022 :     set_avma(av);
    1843             :   }
    1844       48125 :   if (pr == ONLY_DIVIDES)
    1845             :   {
    1846        2619 :     guncloneNULL(lead);
    1847        2619 :     if (sx) return gc_NULL(av0);
    1848        2514 :     set_avma((pari_sp)rem); return z-2;
    1849             :   }
    1850       45506 :   lr=i+3; rem -= lr;
    1851       45506 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1852       45506 :   rem[1] = z[-1];
    1853       45506 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1854       45506 :   rem += 2; gel(rem,i) = p1;
    1855      120132 :   for (i--; i>=0; i--)
    1856             :   {
    1857       74626 :     av=avma; p1 = gel(x,i);
    1858      277026 :     for (j=0; j<=i && j<=dz; j++)
    1859      202400 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1860       74626 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1861             :   }
    1862       45506 :   rem -= 2;
    1863       45506 :   guncloneNULL(lead);
    1864       45506 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1865       45506 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1866         119 :   *pr = rem; return z-2;
    1867             : }
    1868             : 
    1869             : /*******************************************************************/
    1870             : /*                                                                 */
    1871             : /*                        PSEUDO-DIVISION                          */
    1872             : /*                                                                 */
    1873             : /*******************************************************************/
    1874             : INLINE GEN
    1875      516224 : rem(GEN c, GEN T)
    1876             : {
    1877      516224 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1878      516224 :   return c;
    1879             : }
    1880             : 
    1881             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1882             : int
    1883        8344 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1884             : {
    1885             :   long dx, dy, dz, i, p, T_ismonic;
    1886        8344 :   pari_sp av = avma, av2;
    1887             :   GEN y_lead;
    1888             : 
    1889        8344 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1890        8344 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1891        8344 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1892             :   /* if monic, no point in using pseudo-division */
    1893        8344 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1894        5341 :   T_ismonic = gequal1(leading_coeff(T));
    1895        5341 :   dx = degpol(x);
    1896        5341 :   if (dx < dy) return !signe(x);
    1897        5341 :   (void)new_chunk(2);
    1898        5341 :   x = RgX_recip_shallow(x)+2;
    1899        5341 :   y = RgX_recip_shallow(y)+2;
    1900             :   /* pay attention to sparse divisors */
    1901       11202 :   for (i = 1; i <= dy; i++)
    1902        5861 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1903        5341 :   dz = dx-dy; p = dz+1;
    1904        5341 :   av2 = avma;
    1905             :   for (;;)
    1906       33633 :   {
    1907       38974 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1908       38974 :     x0 = gneg(x0); p--;
    1909       38974 :     m = gcdii(cx, y0);
    1910       38974 :     if (!equali1(m))
    1911             :     {
    1912       37650 :       x0 = gdiv(x0, m);
    1913       37650 :       y0 = diviiexact(y0, m);
    1914       37650 :       if (equali1(y0)) y0 = NULL;
    1915             :     }
    1916       81865 :     for (i=1; i<=dy; i++)
    1917             :     {
    1918       42891 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1919       42891 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1920       42891 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1921       42891 :       gel(x,i) = c;
    1922             :     }
    1923      415284 :     for (   ; i<=dx; i++)
    1924             :     {
    1925      376310 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1926      376310 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1927      376310 :       gel(x,i) = c;
    1928             :     }
    1929       44669 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1930       38974 :     if (dx < dy) break;
    1931       33633 :     if (gc_needed(av2,1))
    1932             :     {
    1933           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1934           0 :       gerepilecoeffs(av2,x,dx+1);
    1935             :     }
    1936             :   }
    1937        5341 :   return gc_bool(av, dx < 0);
    1938             : }
    1939             : 
    1940             : /* T either NULL or a t_POL. */
    1941             : GEN
    1942       28188 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1943             : {
    1944       28188 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1945       28188 :   pari_sp av = avma, av2;
    1946             :   GEN y_lead;
    1947             : 
    1948       28188 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1949       28188 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1950             :   /* if monic, no point in using pseudo-division */
    1951       28188 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1952       24800 :   dx = degpol(x);
    1953       24800 :   if (dx < dy) return RgX_copy(x);
    1954       24800 :   (void)new_chunk(2);
    1955       24800 :   x = RgX_recip_shallow(x)+2;
    1956       24800 :   y = RgX_recip_shallow(y)+2;
    1957             :   /* pay attention to sparse divisors */
    1958       78486 :   for (i = 1; i <= dy; i++)
    1959       53686 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1960       24800 :   dz = dx-dy; p = dz+1;
    1961       24800 :   av2 = avma;
    1962             :   for (;;)
    1963             :   {
    1964      152532 :     gel(x,0) = gneg(gel(x,0)); p--;
    1965      287622 :     for (i=1; i<=dy; i++)
    1966             :     {
    1967      198956 :       GEN c = gmul(y_lead, gel(x,i));
    1968      198956 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1969      198956 :       gel(x,i) = rem(c, T);
    1970             :     }
    1971      306261 :     for (   ; i<=dx; i++)
    1972             :     {
    1973      217595 :       GEN c = gmul(y_lead, gel(x,i));
    1974      217595 :       gel(x,i) = rem(c, T);
    1975             :     }
    1976       97683 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1977       88666 :     if (dx < dy) break;
    1978       63866 :     if (gc_needed(av2,1))
    1979             :     {
    1980           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1981           0 :       gerepilecoeffs(av2,x,dx+1);
    1982             :     }
    1983             :   }
    1984       24800 :   if (dx < 0) return pol_0(vx);
    1985       23386 :   lx = dx+3; x -= 2;
    1986       23386 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1987       23386 :   x[1] = evalsigne(1) | evalvarn(vx);
    1988       23386 :   x = RgX_recip_shallow(x);
    1989       23386 :   if (p)
    1990             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1991        2668 :     GEN t = y_lead;
    1992        2668 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1993           0 :       t = RgXQ_powu(t, p, T);
    1994             :     else
    1995        2668 :       t = gpowgs(t, p);
    1996        7447 :     for (i=2; i<lx; i++)
    1997             :     {
    1998        4779 :       GEN c = gmul(gel(x,i), t);
    1999        4779 :       gel(x,i) = rem(c,T);
    2000             :     }
    2001        2668 :     if (!T) return gerepileupto(av, x);
    2002             :   }
    2003       20718 :   return gerepilecopy(av, x);
    2004             : }
    2005             : 
    2006             : GEN
    2007       28188 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2008             : 
    2009             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2010             : GEN
    2011        7769 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2012             : {
    2013        7769 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2014        7769 :   pari_sp av = avma, av2;
    2015             :   GEN z, r, ypow, y_lead;
    2016             : 
    2017        7769 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2018        7769 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2019        7769 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2020        7223 :   dx = degpol(x);
    2021        7223 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2022        7223 :   if (dx == dy)
    2023             :   {
    2024          70 :     GEN x_lead = gel(x,lg(x)-1);
    2025          70 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2026          70 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2027          70 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2028          70 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2029             :   }
    2030        7153 :   (void)new_chunk(2);
    2031        7153 :   x = RgX_recip_shallow(x)+2;
    2032        7153 :   y = RgX_recip_shallow(y)+2;
    2033             :   /* pay attention to sparse divisors */
    2034       36628 :   for (i = 1; i <= dy; i++)
    2035       29475 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2036        7153 :   dz = dx-dy; p = dz+1;
    2037        7153 :   lz = dz+3;
    2038        7153 :   z = cgetg(lz, t_POL);
    2039        7153 :   z[1] = evalsigne(1) | evalvarn(vx);
    2040        7153 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2041        7153 :   ypow = new_chunk(dz+1);
    2042        7153 :   gel(ypow,0) = gen_1;
    2043        7153 :   gel(ypow,1) = y_lead;
    2044       13131 :   for (i=2; i<=dz; i++)
    2045             :   {
    2046        5978 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2047        5978 :     gel(ypow,i) = rem(c,T);
    2048             :   }
    2049        7153 :   av2 = avma;
    2050        7153 :   for (iz=2;;)
    2051             :   {
    2052       22743 :     p--;
    2053       14948 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2054       64831 :     for (i=1; i<=dy; i++)
    2055             :     {
    2056       49883 :       GEN c = gmul(y_lead, gel(x,i));
    2057       49883 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2058       49883 :       gel(x,i) = rem(c, T);
    2059             :     }
    2060       39033 :     for (   ; i<=dx; i++)
    2061             :     {
    2062       24085 :       GEN c = gmul(y_lead, gel(x,i));
    2063       24085 :       gel(x,i) = rem(c,T);
    2064             :     }
    2065       14948 :     x++; dx--;
    2066       14948 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2067       14948 :     if (dx < dy) break;
    2068        7795 :     if (gc_needed(av2,1))
    2069             :     {
    2070           0 :       GEN X = x-2;
    2071           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2072           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2073           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2074             :     }
    2075             :   }
    2076        7153 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2077        7153 :   if (dx < 0)
    2078          98 :     x = pol_0(vx);
    2079             :   else
    2080             :   {
    2081        7055 :     lx = dx+3; x -= 2;
    2082        7055 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2083        7055 :     x[1] = evalsigne(1) | evalvarn(vx);
    2084        7055 :     x = RgX_recip_shallow(x);
    2085             :   }
    2086        7153 :   z = RgX_recip_shallow(z);
    2087        7153 :   r = x;
    2088        7153 :   if (p)
    2089             :   {
    2090        3207 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2091        3207 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2092             :   }
    2093        7153 :   gerepileall(av, 2, &z, &r);
    2094        7153 :   *ptr = r; return z;
    2095             : }
    2096             : GEN
    2097        7573 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2098        7573 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2099             : 
    2100             : GEN
    2101       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2102             : {
    2103       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2104             : }
    2105             : GEN
    2106    70099413 : RgX_Rg_mul(GEN y, GEN x) {
    2107             :   long i, ly;
    2108    70099413 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2109    70099413 :   if (ly == 2) return z;
    2110    70013796 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2111    70013789 :   return normalizepol_lg(z,ly);
    2112             : }
    2113             : GEN
    2114       14714 : RgX_muls(GEN y, long x) {
    2115             :   long i, ly;
    2116       14714 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2117       14714 :   if (ly == 2) return z;
    2118       14616 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2119       14616 :   return normalizepol_lg(z,ly);
    2120             : }
    2121             : GEN
    2122        4755 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2123             : {
    2124        4755 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2125             : }
    2126             : GEN
    2127          77 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2128             : {
    2129          77 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2130             : }
    2131             : 
    2132             : GEN
    2133        1792 : RgXQX_sqr(GEN x, GEN T)
    2134             : {
    2135        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2136             : }
    2137             : 
    2138             : GEN
    2139         448 : RgXQX_powers(GEN P, long n, GEN T)
    2140             : {
    2141         448 :   GEN v = cgetg(n+2, t_VEC);
    2142             :   long i;
    2143         448 :   gel(v, 1) = pol_1(varn(T));
    2144         448 :   if (n==0) return v;
    2145         448 :   gel(v, 2) = gcopy(P);
    2146         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2147         448 :   return v;
    2148             : }
    2149             : 
    2150             : static GEN
    2151      144465 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2152             : static GEN
    2153           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2154             : static GEN
    2155       56398 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2156             : static GEN
    2157       46663 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2158             : static GEN
    2159      262176 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2160             : static GEN
    2161      260097 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2162             : static GEN
    2163         553 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2164             : static GEN
    2165      156540 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2166             : 
    2167             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2168             :               _mul, _sqr, _one, _zero };
    2169             : 
    2170             : GEN
    2171           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2172             : {
    2173           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2174             : }
    2175             : 
    2176             : GEN
    2177      117543 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2178             : {
    2179      117543 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2180      117543 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2181             : }
    2182             : 
    2183             : /* mod X^n */
    2184             : struct modXn {
    2185             :   long v; /* varn(X) */
    2186             :   long n;
    2187             : } ;
    2188             : static GEN
    2189       11613 : _sqrXn(void *data, GEN x) {
    2190       11613 :   struct modXn *S = (struct modXn*)data;
    2191       11613 :   return RgXn_sqr(x, S->n);
    2192             : }
    2193             : static GEN
    2194        4234 : _mulXn(void *data, GEN x, GEN y) {
    2195        4234 :   struct modXn *S = (struct modXn*)data;
    2196        4234 :   return RgXn_mul(x,y, S->n);
    2197             : }
    2198             : static GEN
    2199        1477 : _oneXn(void *data) {
    2200        1477 :   struct modXn *S = (struct modXn*)data;
    2201        1477 :   return pol_1(S->v);
    2202             : }
    2203             : static GEN
    2204           0 : _zeroXn(void *data) {
    2205           0 :   struct modXn *S = (struct modXn*)data;
    2206           0 :   return pol_0(S->v);
    2207             : }
    2208             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2209             :                                           _oneXn, _zeroXn };
    2210             : 
    2211             : GEN
    2212         357 : RgXn_powers(GEN x, long m, long n)
    2213             : {
    2214         357 :   long d = degpol(x);
    2215         357 :   int use_sqr = (d<<1) >= n;
    2216             :   struct modXn S;
    2217         357 :   S.v = varn(x); S.n = n;
    2218         357 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2219             : }
    2220             : 
    2221             : GEN
    2222        2216 : RgXn_powu_i(GEN x, ulong m, long n)
    2223             : {
    2224             :   struct modXn S;
    2225             :   long v;
    2226        2216 :   if (n == 1) return x;
    2227        2188 :   v = RgX_valrem(x, &x);
    2228        2188 :   if (v) n -= m * v;
    2229        2188 :   S.v = varn(x); S.n = n;
    2230        2188 :   x = gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2231        2188 :   if (v) x = RgX_shift_shallow(x, m * v);
    2232        2188 :   return x;
    2233             : }
    2234             : GEN
    2235           0 : RgXn_powu(GEN x, ulong m, long n)
    2236             : {
    2237             :   pari_sp av;
    2238           0 :   if (n == 1) return gcopy(x);
    2239           0 :   av = avma; return gerepilecopy(av, RgXn_powu_i(x, m, n));
    2240             : }
    2241             : 
    2242             : GEN
    2243         714 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2244             : {
    2245             :   struct modXn S;
    2246         714 :   S.v = varn(gel(x,2)); S.n = n;
    2247         714 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2248             : }
    2249             : 
    2250             : GEN
    2251           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2252             : {
    2253           0 :   int use_sqr = 2*degpol(x) >= n;
    2254             :   struct modXn S;
    2255           0 :   S.v = varn(x); S.n = n;
    2256           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2257             : }
    2258             : 
    2259             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2260             : GEN
    2261        6398 : RgXn_eval(GEN Q, GEN x, long n)
    2262             : {
    2263        6398 :   long d = degpol(x);
    2264             :   int use_sqr;
    2265             :   struct modXn S;
    2266        6398 :   if (d == 1 && isrationalzero(gel(x,2)))
    2267             :   {
    2268        6391 :     GEN y = RgX_unscale(Q, gel(x,3));
    2269        6391 :     setvarn(y, varn(x)); return y;
    2270             :   }
    2271           7 :   S.v = varn(x);
    2272           7 :   S.n = n;
    2273           7 :   use_sqr = (d<<1) >= n;
    2274           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2275             : }
    2276             : 
    2277             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2278             : static GEN
    2279       47007 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2280             : {
    2281       47007 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2282       47007 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2283             : }
    2284             : 
    2285             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2286             : static GEN
    2287           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2288             : {
    2289           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2290           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2291             : }
    2292             : 
    2293             : GEN
    2294      126934 : RgXn_inv_i(GEN f, long e)
    2295             : {
    2296             :   pari_sp av;
    2297             :   ulong mask;
    2298             :   GEN W, a;
    2299      126934 :   long v = varn(f), n = 1;
    2300             : 
    2301      126934 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2302      126934 :   a = ginv(gel(f,2));
    2303      126933 :   if (e == 1) return scalarpol(a, v);
    2304      126905 :   else if (e == 2)
    2305             :   {
    2306             :     GEN b;
    2307      108827 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2308       88773 :     av = avma; b = gneg(b);
    2309       88773 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2310       88772 :     W = deg1pol_shallow(b, a, v);
    2311       88772 :     return gcopy(W);
    2312             :   }
    2313       18078 :   W = scalarpol_shallow(a,v);
    2314       18078 :   mask = quadratic_prec_mask(e);
    2315       18078 :   av = avma;
    2316       83163 :   for (;mask>1;)
    2317             :   {
    2318             :     GEN u, fr;
    2319       47007 :     long n2 = n;
    2320       47007 :     n<<=1; if (mask & 1) n--;
    2321       47007 :     mask >>= 1;
    2322       47007 :     fr = RgXn_red_shallow(f, n);
    2323       47007 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2324       47007 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2325       47007 :     if (gc_needed(av,2))
    2326             :     {
    2327           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2328           0 :       W = gerepileupto(av, W);
    2329             :     }
    2330             :   }
    2331       18078 :   return W;
    2332             : }
    2333             : 
    2334             : static GEN
    2335           0 : RgXn_inv_FpX(GEN x, long e, GEN p)
    2336             : {
    2337           0 :   pari_sp av = avma;
    2338             :   GEN r;
    2339           0 :   if (lgefint(p) == 3)
    2340             :   {
    2341           0 :     ulong pp = uel(p, 2);
    2342           0 :     if (pp == 2)
    2343           0 :       r = F2x_to_ZX(F2xn_inv(RgX_to_F2x(x), e));
    2344             :     else
    2345           0 :       r = Flx_to_ZX_inplace(Flxn_inv(RgX_to_Flx(x, pp), e, pp));
    2346             :   }
    2347             :   else
    2348           0 :     r = FpXn_inv(RgX_to_FpX(x, p), e, p);
    2349           0 :   return gerepileupto(av, FpX_to_mod(r, p));
    2350             : }
    2351             : 
    2352             : static GEN
    2353           0 : RgXn_inv_FpXQX(GEN x, long n, GEN pol, GEN p)
    2354             : {
    2355           0 :   pari_sp av = avma;
    2356           0 :   GEN r, T = RgX_to_FpX(pol, p);
    2357           0 :   if (signe(T) == 0) pari_err_OP("/", gen_1, x);
    2358           0 :   r = FpXQXn_inv(RgX_to_FpXQX(x, T, p), n, T, p);
    2359           0 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2360             : }
    2361             : 
    2362             : #define code(t1,t2) ((t1 << 6) | t2)
    2363             : 
    2364             : static GEN
    2365       83317 : RgXn_inv_fast(GEN x, long e)
    2366             : {
    2367             :   GEN p, pol;
    2368             :   long pa;
    2369       83317 :   long t = RgX_type(x,&p,&pol,&pa);
    2370       83319 :   switch(t)
    2371             :   {
    2372           0 :     case t_INTMOD: return RgXn_inv_FpX(x, e, p);
    2373             :     case code(t_POLMOD, t_INTMOD):
    2374           0 :                    return RgXn_inv_FpXQX(x, e, pol, p);
    2375       83319 :     default:       return NULL;
    2376             :   }
    2377             : }
    2378             : #undef code
    2379             : 
    2380             : GEN
    2381       83317 : RgXn_inv(GEN f, long e)
    2382             : {
    2383       83317 :   GEN h = RgXn_inv_fast(f, e);
    2384       83319 :   if (h) return h;
    2385       83319 :   return RgXn_inv_i(f, e);
    2386             : }
    2387             : 
    2388             : GEN
    2389       12971 : RgXn_exp(GEN h, long e)
    2390             : {
    2391       12971 :   pari_sp av = avma, av2;
    2392       12971 :   long v = varn(h), n=1;
    2393       12971 :   GEN f = pol_1(v), g = pol_1(v);
    2394       12971 :   ulong mask = quadratic_prec_mask(e);
    2395       12971 :   av2 = avma;
    2396       12971 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2397           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2398       40152 :   for (;mask>1;)
    2399             :   {
    2400             :     GEN q, w;
    2401       14210 :     long n2 = n;
    2402       14210 :     n<<=1; if (mask & 1) n--;
    2403       14210 :     mask >>= 1;
    2404       14210 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2405       14210 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2406       14210 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2407       14210 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2408       14210 :     if (gc_needed(av2,2))
    2409             :     {
    2410           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2411           0 :       gerepileall(av2, 2, &f, &g);
    2412             :     }
    2413             :   }
    2414       12971 :   return gerepileupto(av, f);
    2415             : }
    2416             : 
    2417             : GEN
    2418          91 : RgXn_reverse(GEN f, long e)
    2419             : {
    2420          91 :   pari_sp av = avma, av2;
    2421             :   ulong mask;
    2422             :   GEN fi, a, df, W, an;
    2423          91 :   long v = varn(f), n=1;
    2424          91 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2425           0 :     pari_err_INV("serreverse",f);
    2426          91 :   fi = ginv(gel(f,3));
    2427          91 :   a = deg1pol_shallow(fi,gen_0,v);
    2428          91 :   if (e <= 2) return gerepilecopy(av, a);
    2429          91 :   W = scalarpol(fi,v);
    2430          91 :   df = RgX_deriv(f);
    2431          91 :   mask = quadratic_prec_mask(e);
    2432          91 :   av2 = avma;
    2433         539 :   for (;mask>1;)
    2434             :   {
    2435             :     GEN u, fa, fr;
    2436         357 :     long n2 = n, rt;
    2437         357 :     n<<=1; if (mask & 1) n--;
    2438         357 :     mask >>= 1;
    2439         357 :     fr = RgXn_red_shallow(f, n);
    2440         357 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2441         357 :     an = RgXn_powers(a, rt, n);
    2442         357 :     if (n>1)
    2443             :     {
    2444         357 :       long n4 = (n2+1)>>1;
    2445         357 :       GEN dfr = RgXn_red_shallow(df, n2);
    2446         357 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2447         357 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2448         357 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2449             :     }
    2450         357 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2451         357 :     fa = RgX_shift(fa, -n2);
    2452         357 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2453         357 :     if (gc_needed(av2,2))
    2454             :     {
    2455           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2456           0 :       gerepileall(av2, 2, &a, &W);
    2457             :     }
    2458             :   }
    2459          91 :   return gerepileupto(av, a);
    2460             : }
    2461             : 
    2462             : GEN
    2463           0 : RgXn_sqrt(GEN h, long e)
    2464             : {
    2465           0 :   pari_sp av = avma, av2;
    2466           0 :   long v = varn(h), n = 1;
    2467           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2468           0 :   ulong mask = quadratic_prec_mask(e);
    2469           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2470           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2471           0 :   av2 = avma;
    2472             :   while(1)
    2473           0 :   {
    2474           0 :     long n2 = n, m;
    2475             :     GEN g;
    2476           0 :     n<<=1; if (mask & 1) n--;
    2477           0 :     mask >>= 1;
    2478           0 :     m = n-n2;
    2479           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2480           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2481           0 :     if (mask==1) return gerepileupto(av, f);
    2482           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2483           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2484           0 :     if (gc_needed(av2,2))
    2485             :     {
    2486           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2487           0 :       gerepileall(av2, 2, &f, &df);
    2488             :     }
    2489             :   }
    2490             : }
    2491             : 
    2492             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2493             : GEN
    2494       18706 : RgXQ_powu(GEN x, ulong n, GEN T)
    2495             : {
    2496       18706 :   pari_sp av = avma;
    2497             : 
    2498       18706 :   if (!n) return pol_1(varn(x));
    2499       16983 :   if (n == 1) return RgX_copy(x);
    2500       15162 :   x = gen_powu_i(x, n, (void*)T, &_sqr, &_mul);
    2501       15162 :   return gerepilecopy(av, x);
    2502             : }
    2503             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2504             : GEN
    2505       18815 : RgXQ_pow(GEN x, GEN n, GEN T)
    2506             : {
    2507             :   pari_sp av;
    2508       18815 :   long s = signe(n);
    2509             : 
    2510       18815 :   if (!s) return pol_1(varn(x));
    2511       18815 :   if (is_pm1(n) == 1)
    2512           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2513       18815 :   av = avma;
    2514       18815 :   if (s < 0) x = RgXQ_inv(x, T);
    2515       18815 :   x = gen_pow_i(x, n, (void*)T, &_sqr, &_mul);
    2516       18815 :   return gerepilecopy(av, x);
    2517             : }
    2518             : static GEN
    2519      159039 : _ZXQsqr(void *data, GEN x) { return ZXQ_sqr(x, (GEN)data); }
    2520             : static GEN
    2521       50461 : _ZXQmul(void *data, GEN x, GEN y) { return ZXQ_mul(x,y, (GEN)data); }
    2522             : /* x,T in Z[X], n in N, compute lift(x^n mod T)) */
    2523             : GEN
    2524      163664 : ZXQ_powu(GEN x, ulong n, GEN T)
    2525             : {
    2526      163664 :   pari_sp av = avma;
    2527             : 
    2528      163664 :   if (!n) return pol_1(varn(x));
    2529      163664 :   if (n == 1) return ZX_copy(x);
    2530      107408 :   x = gen_powu_i(x, n, (void*)T, &_ZXQsqr, &_ZXQmul);
    2531      107400 :   return gerepilecopy(av, x);
    2532             : }
    2533             : 
    2534             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2535             : GEN
    2536        4837 : RgXQ_powers(GEN x, long l, GEN T)
    2537             : {
    2538        4837 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2539        4837 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2540             : }
    2541             : 
    2542             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2543             : GEN
    2544        3381 : QXQ_powers(GEN a, long n, GEN T)
    2545             : {
    2546        3381 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2547             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2548        3381 :   if (den)
    2549             :   { /* restore denominators */
    2550        2072 :     GEN d = den;
    2551             :     long i;
    2552        2072 :     gel(v,2) = a;
    2553        6559 :     for (i=3; i<=n+1; i++) {
    2554        4487 :       d = mulii(d,den);
    2555        4487 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2556             :     }
    2557             :   }
    2558        3381 :   return v;
    2559             : }
    2560             : 
    2561             : static GEN
    2562        2009 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2563             : {
    2564        2009 :   long l, i, m = 0;
    2565             :   GEN dz, z;
    2566        2009 :   GEN V = cgetg_copy(v, &l);
    2567        6482 :   for (i = imin; i < l; i++)
    2568             :   {
    2569        4473 :     GEN c = gel(v, i);
    2570        4473 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2571             :   }
    2572        2009 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2573        2009 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2574        6482 :   for (i = imin; i < l; i++)
    2575             :   {
    2576        4473 :     GEN c = gel(v,i);
    2577        4473 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2578        4473 :     gel(V,i) = c;
    2579             :   }
    2580        2009 :   return V;
    2581             : }
    2582             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2583             : GEN
    2584        1946 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2585        1946 : { return do_QXQ_eval(v, 1, a, T); }
    2586             : GEN
    2587          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2588          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2589             : 
    2590             : GEN
    2591         553 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2592             : {
    2593         553 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2594             : }
    2595             : 
    2596             : GEN
    2597       42684 : RgXQ_norm(GEN x, GEN T)
    2598             : {
    2599             :   pari_sp av;
    2600       42684 :   long dx = degpol(x);
    2601             :   GEN L, y;
    2602             : 
    2603       42684 :   av = avma; y = resultant(T, x);
    2604       42684 :   L = leading_coeff(T);
    2605       42684 :   if (gequal1(L) || !signe(x)) return y;
    2606           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2607             : }
    2608             : 
    2609             : GEN
    2610      117119 : RgX_blocks(GEN P, long n, long m)
    2611             : {
    2612      117119 :   GEN z = cgetg(m+1,t_VEC);
    2613      117119 :   long i,j, k=2, l = lg(P);
    2614      544529 :   for(i=1; i<=m; i++)
    2615             :   {
    2616      427410 :     GEN zi = cgetg(n+2,t_POL);
    2617      427410 :     zi[1] = P[1];
    2618      427410 :     gel(z,i) = zi;
    2619     2764901 :     for(j=2; j<n+2; j++)
    2620     2337491 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2621      427410 :     zi = RgX_renormalize_lg(zi, n+2);
    2622             :   }
    2623      117119 :   return z;
    2624             : }
    2625             : 
    2626             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2627             : void
    2628       85336 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2629             : {
    2630       85336 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2631             :   GEN p0, p1;
    2632             : 
    2633       85337 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2634             : 
    2635       85050 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2636       85050 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2637       85051 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2638     5572744 :   for (i=0; i<n1; i++)
    2639             :   {
    2640     5487692 :     p0[2+i] = p[2+(i<<1)];
    2641     5487692 :     p1[2+i] = p[3+(i<<1)];
    2642             :   }
    2643       85052 :   if (n1 != n0)
    2644       45030 :     p0[2+i] = p[2+(i<<1)];
    2645       85052 :   *pe = normalizepol(p0);
    2646       85051 :   *po = normalizepol(p1);
    2647             : }
    2648             : 
    2649             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2650             : GEN
    2651       40922 : RgX_splitting(GEN p, long k)
    2652             : {
    2653       40922 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2654             :   GEN r;
    2655             : 
    2656       40922 :   m = n/k;
    2657       40922 :   r = cgetg(k+1,t_VEC);
    2658      225666 :   for(i=1; i<=k; i++)
    2659             :   {
    2660      184744 :     gel(r,i) = cgetg(m+3, t_POL);
    2661      184744 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2662             :   }
    2663      554799 :   for (j=1, i=0, l=2; i<=n; i++)
    2664             :   {
    2665      513877 :     gmael(r,j,l) = gel(p,2+i);
    2666      513877 :     if (j==k) { j=1; l++; } else j++;
    2667             :   }
    2668      225666 :   for(i=1; i<=k; i++)
    2669      184744 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2670       40922 :   return r;
    2671             : }
    2672             : 
    2673             : /*******************************************************************/
    2674             : /*                                                                 */
    2675             : /*                        Kronecker form                           */
    2676             : /*                                                                 */
    2677             : /*******************************************************************/
    2678             : 
    2679             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2680             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2681             :  * normalized coefficients */
    2682             : GEN
    2683      152796 : Kronecker_to_mod(GEN z, GEN T)
    2684             : {
    2685      152796 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2686      152796 :   GEN x, t = cgetg(N,t_POL);
    2687      152796 :   t[1] = T[1];
    2688      152796 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2689      152796 :   x[1] = z[1];
    2690      152796 :   T = RgX_copy(T);
    2691     1485714 :   for (i=2; i<lx+2; i++, z+= N-2)
    2692             :   {
    2693     1332918 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2694     1332918 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2695             :   }
    2696      152796 :   N = (l-2) % (N-2) + 2;
    2697      152796 :   for (j=2; j<N; j++) t[j] = z[j];
    2698      152796 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2699      152796 :   return normalizepol_lg(x, i+1);
    2700             : }
    2701             : 
    2702             : /*******************************************************************/
    2703             : /*                                                                 */
    2704             : /*                        Domain detection                         */
    2705             : /*                                                                 */
    2706             : /*******************************************************************/
    2707             : 
    2708             : static GEN
    2709      150739 : zero_FpX_mod(GEN p, long v)
    2710             : {
    2711      150739 :   GEN r = cgetg(3,t_POL);
    2712      150739 :   r[1] = evalvarn(v);
    2713      150739 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2714      150739 :   return r;
    2715             : }
    2716             : 
    2717             : static GEN
    2718      498994 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2719             : {
    2720      498994 :   pari_sp av = avma;
    2721             :   GEN r;
    2722      498994 :   if (lgefint(p) == 3)
    2723             :   {
    2724      468890 :     ulong pp = uel(p, 2);
    2725      468890 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2726             :                                   RgX_to_Flx(y, pp), pp));
    2727             :   }
    2728             :   else
    2729       30104 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2730      498994 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2731      385957 :   return gerepileupto(av, FpX_to_mod(r, p));
    2732             : }
    2733             : 
    2734             : static GEN
    2735         532 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2736             : {
    2737         532 :   GEN r = cgetg(3,t_POL);
    2738         532 :   r[1] = evalvarn(v);
    2739         532 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2740         532 :   return r;
    2741             : }
    2742             : 
    2743             : static GEN
    2744        2037 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2745             : {
    2746        2037 :   pari_sp av = avma;
    2747             :   long dT;
    2748             :   GEN kx, ky, r;
    2749        2037 :   GEN T = RgX_to_FpX(pol, p);
    2750        2037 :   if (signe(T)==0) pari_err_OP("*", x, y);
    2751        2030 :   dT = degpol(T);
    2752        2030 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2753        2030 :   ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2754        2030 :   r = FpX_mul(kx, ky, p);
    2755        2030 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2756        1505 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2757             : }
    2758             : 
    2759             : static GEN
    2760      369491 : RgX_liftred(GEN x, GEN T)
    2761      369491 : { return RgXQX_red(liftpol_shallow(x), T); }
    2762             : 
    2763             : static GEN
    2764      138773 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2765             : {
    2766      138773 :   pari_sp av = avma;
    2767      138773 :   long dT = degpol(T);
    2768      138773 :   GEN r = QX_mul(ZXX_to_Kronecker(RgX_liftred(x, T), dT),
    2769             :                  ZXX_to_Kronecker(RgX_liftred(y, T), dT));
    2770      138773 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2771             : }
    2772             : 
    2773             : static GEN
    2774        1260 : RgX_sqr_FpX(GEN x, GEN p)
    2775             : {
    2776        1260 :   pari_sp av = avma;
    2777             :   GEN r;
    2778        1260 :   if (lgefint(p) == 3)
    2779             :   {
    2780        1056 :     ulong pp = uel(p, 2);
    2781        1056 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2782             :   }
    2783             :   else
    2784         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2785        1260 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2786        1162 :   return gerepileupto(av, FpX_to_mod(r, p));
    2787             : }
    2788             : 
    2789             : static GEN
    2790         196 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2791             : {
    2792         196 :   pari_sp av = avma;
    2793             :   long dT;
    2794         196 :   GEN kx, r, T = RgX_to_FpX(pol, p);
    2795         196 :   if (signe(T)==0) pari_err_OP("*",x,x);
    2796         189 :   dT = degpol(T);
    2797         189 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2798         189 :   r = FpX_sqr(kx, p);
    2799         189 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2800         189 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2801             : }
    2802             : 
    2803             : static GEN
    2804       12329 : RgX_sqr_QXQX(GEN x, GEN T)
    2805             : {
    2806       12329 :   pari_sp av = avma;
    2807       12329 :   long dT = degpol(T);
    2808       12329 :   GEN r = QX_sqr(ZXX_to_Kronecker(RgX_liftred(x, T), dT));
    2809       12329 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2810             : }
    2811             : 
    2812             : static GEN
    2813       67480 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2814             : {
    2815       67480 :   pari_sp av = avma;
    2816             :   GEN r;
    2817       67480 :   if (lgefint(p) == 3)
    2818             :   {
    2819       50690 :     ulong pp = uel(p, 2);
    2820       50690 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2821             :                                   RgX_to_Flx(y, pp), pp));
    2822             :   }
    2823             :   else
    2824       16790 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2825       67480 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2826       29876 :   return gerepileupto(av, FpX_to_mod(r, p));
    2827             : }
    2828             : 
    2829             : static GEN
    2830       39808 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    2831             : {
    2832       39808 :   pari_sp av = avma;
    2833             :   GEN r;
    2834       39808 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    2835       39808 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    2836             : }
    2837             : static GEN
    2838          63 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2839             : {
    2840          63 :   pari_sp av = avma;
    2841             :   GEN r;
    2842          63 :   GEN T = RgX_to_FpX(pol, p);
    2843          63 :   if (signe(T) == 0) pari_err_OP("%", x, y);
    2844          56 :   if (lgefint(p) == 3)
    2845             :   {
    2846          48 :     ulong pp = uel(p, 2);
    2847          48 :     GEN Tp = ZX_to_Flx(T, pp);
    2848          48 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2849             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2850             :   }
    2851             :   else
    2852           8 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2853          56 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2854          49 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2855             : }
    2856             : 
    2857             : #define code(t1,t2) ((t1 << 6) | t2)
    2858             : static GEN
    2859    48557156 : RgX_mul_fast(GEN x, GEN y)
    2860             : {
    2861             :   GEN p, pol;
    2862             :   long pa;
    2863    48557156 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2864    48557157 :   switch(t)
    2865             :   {
    2866    36360761 :     case t_INT:    return ZX_mul(x,y);
    2867     1571410 :     case t_FRAC:   return QX_mul(x,y);
    2868      104161 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2869      498994 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2870             :     case code(t_POLMOD, t_INT):
    2871             :     case code(t_POLMOD, t_FRAC):
    2872      138773 :                    return RgX_mul_QXQX(x, y, pol);
    2873             :     case code(t_POLMOD, t_INTMOD):
    2874        2037 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2875     9881021 :     default:       return NULL;
    2876             :   }
    2877             : }
    2878             : static GEN
    2879      153231 : RgX_sqr_fast(GEN x)
    2880             : {
    2881             :   GEN p, pol;
    2882             :   long pa;
    2883      153231 :   long t = RgX_type(x,&p,&pol,&pa);
    2884      153231 :   switch(t)
    2885             :   {
    2886      119195 :     case t_INT:    return ZX_sqr(x);
    2887       11382 :     case t_FRAC:   return QX_sqr(x);
    2888        2296 :     case t_FFELT:  return FFX_sqr(x, pol);
    2889        1260 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2890             :     case code(t_POLMOD, t_INT):
    2891             :     case code(t_POLMOD, t_FRAC):
    2892       12329 :                    return RgX_sqr_QXQX(x, pol);
    2893             :     case code(t_POLMOD, t_INTMOD):
    2894         196 :                    return RgX_sqr_FpXQX(x, pol, p);
    2895        6573 :     default:       return NULL;
    2896             :   }
    2897             : }
    2898             : 
    2899             : static GEN
    2900     6314319 : RgX_rem_fast(GEN x, GEN y)
    2901             : {
    2902             :   GEN p, pol;
    2903             :   long pa;
    2904     6314319 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2905     6314319 :   switch(t)
    2906             :   {
    2907     3933368 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    2908     1234974 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    2909          84 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2910       67480 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2911             :     case code(t_POLMOD, t_INT):
    2912             :     case code(t_POLMOD, t_FRAC):
    2913       39808 :                    return RgX_rem_QXQX(x, y, pol);
    2914             :     case code(t_POLMOD, t_INTMOD):
    2915          63 :                    return RgX_rem_FpXQX(x, y, pol, p);
    2916     1038542 :     default:       return NULL;
    2917             :   }
    2918             : }
    2919             : 
    2920             : #undef code
    2921             : 
    2922             : GEN
    2923    46075459 : RgX_mul(GEN x, GEN y)
    2924             : {
    2925    46075459 :   GEN z = RgX_mul_fast(x,y);
    2926    46075452 :   if (!z) z = RgX_mul_i(x,y);
    2927    46075452 :   return z;
    2928             : }
    2929             : 
    2930             : GEN
    2931      127205 : RgX_sqr(GEN x)
    2932             : {
    2933      127205 :   GEN z = RgX_sqr_fast(x);
    2934      127198 :   if (!z) z = RgX_sqr_i(x);
    2935      127198 :   return z;
    2936             : }
    2937             : 
    2938             : GEN
    2939     6314319 : RgX_rem(GEN x, GEN y)
    2940             : {
    2941     6314319 :   GEN z = RgX_rem_fast(x, y);
    2942     6314312 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    2943     6314312 :   return z;
    2944             : }
    2945             : 
    2946             : GEN
    2947     2434690 : RgXn_mul(GEN f, GEN g, long n)
    2948             : {
    2949     2434690 :   pari_sp av = avma;
    2950     2434690 :   GEN h = RgX_mul_fast(f,g);
    2951     2434690 :   if (!h) return RgXn_mul2(f,g,n);
    2952      181110 :   if (degpol(h) < n) return h;
    2953       53059 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    2954             : }
    2955             : 
    2956             : GEN
    2957       26026 : RgXn_sqr(GEN f, long n)
    2958             : {
    2959       26026 :   pari_sp av = avma;
    2960       26026 :   GEN g = RgX_sqr_fast(f);
    2961       26026 :   if (!g) return RgXn_sqr2(f,n);
    2962       25788 :   if (degpol(g) < n) return g;
    2963       10486 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    2964             : }
    2965             : 
    2966             : /* (f*g) \/ x^n */
    2967             : GEN
    2968       47007 : RgX_mulhigh_i(GEN f, GEN g, long n)
    2969             : {
    2970       47007 :   GEN h = RgX_mul_fast(f,g);
    2971       47007 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    2972             : }
    2973             : 
    2974             : /* (f*g) \/ x^n */
    2975             : GEN
    2976           0 : RgX_sqrhigh_i(GEN f, long n)
    2977             : {
    2978           0 :   GEN h = RgX_sqr_fast(f);
    2979           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    2980             : }

Generated by: LCOV version 1.13