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 24782-f7724578b4) Lines: 1518 1689 89.9 %
Date: 2019-12-06 05:56:26 Functions: 181 197 91.9 %
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     4266249 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     4266249 :   long pold=1, rold=n*(d-1);
      32    25282447 :   for(p=2; p<=d; p++)
      33             :   {
      34    21016198 :     r = m*(p-1) + n*((d-1)/p);
      35    21016198 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     4266249 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41     7062049 : 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     7062049 :   pari_sp av = avma;
      45             :   long i;
      46     7062049 :   GEN z = cmul(E,P,a,ff->one(E));
      47     7060632 :   if (!z) z = gen_0;
      48    50623734 :   for (i=1; i<=n; i++)
      49             :   {
      50    43561922 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    43566102 :     if (t) {
      52    32339658 :       z = ff->add(E, z, t);
      53    32323521 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56     7061812 :   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     5875265 : 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     5875265 :   pari_sp av = avma;
      69     5875265 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     5875265 :   if (d < 0) return ff->zero(E);
      73     5557111 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74      644200 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75      644200 :   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      644200 :   d -= l;
      81      644200 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     2149630 :   while (d >= l-1)
      83             :   {
      84      861249 :     d -= l-1;
      85      861249 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86      861095 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87      861227 :     if (gc_needed(av,2))
      88          91 :       z = gerepileupto(av, z);
      89             :   }
      90      644191 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91      644187 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92      644184 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96      422571 : 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      422571 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102      422571 :   if (d < 0) return ff->zero(E);
     103      421913 :   rtd = (long) sqrt((double)d);
     104      421913 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105      421914 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106      421912 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110     1085090 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    13986912 : _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      938296 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      307432 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120     1106856 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       21133 : _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      479423 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      479423 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      155358 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      155358 :   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             : GEN
     157        5040 : RgX_homogenous_evalpow(GEN P, GEN A, GEN B)
     158             : {
     159        5040 :   pari_sp av = avma;
     160             :   long d, i, v;
     161             :   GEN s;
     162        5040 :   if (typ(P)!=t_POL)
     163        1547 :     return mkvec2(P, gen_1);
     164        3493 :   d = degpol(P); v = varn(A);
     165        3493 :   s = scalarpol_shallow(gel(P, d+2), v);
     166       17955 :   for (i = d-1; i >= 0; i--)
     167             :   {
     168       14462 :     s = gadd(gmul(s, A), gmul(gel(B,d+1-i), gel(P,i+2)));
     169       14462 :     if (gc_needed(av,1))
     170             :     {
     171           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_homogenous_eval(%ld)",i);
     172           0 :       s = gerepileupto(av, s);
     173             :     }
     174             :   }
     175        3493 :   s = gerepileupto(av, s);
     176        3493 :   return mkvec2(s, gel(B,d+1));
     177             : }
     178             : 
     179             : GEN
     180        1792 : RgXQX_homogenous_evalpow(GEN P, GEN A, GEN B, GEN T)
     181             : {
     182        1792 :   pari_sp av = avma;
     183        1792 :   long i, d = degpol(P), v = varn(A);
     184             :   GEN s;
     185        1792 :   if (signe(P)==0) return mkvec2(pol_0(v), pol_1(v));
     186        1568 :   s = scalarpol_shallow(gel(P, d+2), v);
     187        5229 :   for (i = d-1; i >= 0; i--)
     188             :   {
     189        3661 :     s = RgX_add(RgXQX_mul(s, A, T), RgXQX_RgXQ_mul(gel(B,d+1-i), gel(P,i+2), T));
     190        3661 :     if (gc_needed(av,1))
     191             :     {
     192           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_homogenous_eval(%ld)",i);
     193           0 :       s = gerepileupto(av, s);
     194             :     }
     195             :   }
     196        1568 :   s = gerepileupto(av, s);
     197        1568 :   return mkvec2(s, gel(B,d+1));
     198             : }
     199             : 
     200             : const struct bb_algebra *
     201      140400 : get_Rg_algebra(void)
     202             : {
     203      140400 :   return &Rg_algebra;
     204             : }
     205             : 
     206             : static struct bb_ring Rg_ring = {  _gen_add, _gen_mul, _gen_sqr };
     207             : 
     208             : static GEN
     209       17766 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
     210             : {
     211             :   (void) E;
     212       17766 :   return RgX_divrem(x, y, r);
     213             : }
     214             : 
     215             : GEN
     216        4354 : RgX_digits(GEN x, GEN T)
     217             : {
     218        4354 :   pari_sp av = avma;
     219        4354 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     220        4354 :   GEN z = gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
     221        4354 :   return gerepileupto(av, z);
     222             : }
     223             : 
     224             : /*******************************************************************/
     225             : /*                                                                 */
     226             : /*                         RgX                                     */
     227             : /*                                                                 */
     228             : /*******************************************************************/
     229             : 
     230             : long
     231    24670400 : RgX_equal(GEN x, GEN y)
     232             : {
     233    24670400 :   long i = lg(x);
     234             : 
     235    24670400 :   if (i != lg(y)) return 0;
     236   106060586 :   for (i--; i > 1; i--)
     237    81519426 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     238    24541160 :   return 1;
     239             : }
     240             : 
     241             : /* Returns 1 in the base ring over which x is defined */
     242             : /* HACK: this also works for t_SER */
     243             : GEN
     244     3543239 : Rg_get_1(GEN x)
     245             : {
     246             :   GEN p, T;
     247     3543239 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     248     3543239 :   if (RgX_type_is_composite(tx))
     249       25011 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     250     3543239 :   switch(tx)
     251             :   {
     252         357 :     case t_INTMOD: retmkintmod(is_pm1(p)? gen_0: gen_1, icopy(p));
     253           7 :     case t_PADIC: return cvtop(gen_1, p, lx);
     254         735 :     case t_FFELT: return FF_1(T);
     255     3542140 :     default: return gen_1;
     256             :   }
     257             : }
     258             : /* Returns 0 in the base ring over which x is defined */
     259             : /* HACK: this also works for t_SER */
     260             : GEN
     261     6972035 : Rg_get_0(GEN x)
     262             : {
     263             :   GEN p, T;
     264     6972035 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     265     6972035 :   if (RgX_type_is_composite(tx))
     266       51044 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     267     6972035 :   switch(tx)
     268             :   {
     269         385 :     case t_INTMOD: retmkintmod(gen_0, icopy(p));
     270           0 :     case t_PADIC: return cvtop(gen_0, p, lx);
     271         231 :     case t_FFELT: return FF_zero(T);
     272     6971419 :     default: return gen_0;
     273             :   }
     274             : }
     275             : 
     276             : GEN
     277        4291 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     278             : {
     279        4291 :   long i, n = degpol(P);
     280             :   GEN z, dz, dP;
     281        4291 :   if (n < 0) return gen_0;
     282        4291 :   P = Q_remove_denom(P, &dP);
     283        4291 :   z = gel(P,2); if (n == 0) return icopy(z);
     284        2345 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     285        2345 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     286        2345 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     287        2345 :   dz = mul_denom(dP, dV);
     288        2345 :   return dz? RgX_Rg_div(z, dz): z;
     289             : }
     290             : 
     291             : /* Return P(h * x), not memory clean */
     292             : GEN
     293       10192 : RgX_unscale(GEN P, GEN h)
     294             : {
     295       10192 :   long i, l = lg(P);
     296       10192 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     297       10192 :   Q[1] = P[1];
     298       10192 :   if (l == 2) return Q;
     299       10164 :   gel(Q,2) = gcopy(gel(P,2));
     300       41454 :   for (i=3; i<l; i++)
     301             :   {
     302       31290 :     hi = gmul(hi,h);
     303       31290 :     gel(Q,i) = gmul(gel(P,i), hi);
     304             :   }
     305       10164 :   return Q;
     306             : }
     307             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     308             : GEN
     309      372294 : ZX_z_unscale(GEN P, long h)
     310             : {
     311      372294 :   long i, l = lg(P);
     312      372294 :   GEN Q = cgetg(l, t_POL);
     313      372294 :   Q[1] = P[1];
     314      372294 :   if (l == 2) return Q;
     315      372294 :   gel(Q,2) = gel(P,2);
     316      372294 :   if (l == 3) return Q;
     317      372294 :   if (h == -1)
     318      321353 :     for (i = 3; i < l; i++)
     319             :     {
     320      310390 :       gel(Q,i) = negi(gel(P,i));
     321      310390 :       if (++i == l) break;
     322      306647 :       gel(Q,i) = gel(P,i);
     323             :     }
     324             :   else
     325             :   {
     326             :     GEN hi;
     327      357588 :     gel(Q,3) = mulis(gel(P,3), h);
     328      357588 :     hi = sqrs(h);
     329     1724576 :     for (i = 4; i < l; i++)
     330             :     {
     331     1366988 :       gel(Q,i) = mulii(gel(P,i), hi);
     332     1366988 :       if (i != l-1) hi = mulis(hi,h);
     333             :     }
     334             :   }
     335      372294 :   return Q;
     336             : }
     337             : /* P a ZX, h a t_INT. Return P(h * x), not memory clean; optimize for h = -1 */
     338             : GEN
     339        7273 : ZX_unscale(GEN P, GEN h)
     340             : {
     341             :   long i, l;
     342             :   GEN Q, hi;
     343        7273 :   i = itos_or_0(h); if (i) return ZX_z_unscale(P, i);
     344           7 :   l = lg(P); Q = cgetg(l, t_POL);
     345           7 :   Q[1] = P[1];
     346           7 :   if (l == 2) return Q;
     347           7 :   gel(Q,2) = gel(P,2);
     348           7 :   if (l == 3) return Q;
     349           7 :   hi = h;
     350           7 :   gel(Q,3) = mulii(gel(P,3), hi);
     351          70 :   for (i = 4; i < l; i++)
     352             :   {
     353          63 :     hi = mulii(hi,h);
     354          63 :     gel(Q,i) = mulii(gel(P,i), hi);
     355             :   }
     356           7 :   return Q;
     357             : }
     358             : /* P a ZX. Return P(x << n), not memory clean */
     359             : GEN
     360       63281 : ZX_unscale2n(GEN P, long n)
     361             : {
     362       63281 :   long i, ni = n, l = lg(P);
     363       63281 :   GEN Q = cgetg(l, t_POL);
     364       63281 :   Q[1] = P[1];
     365       63281 :   if (l == 2) return Q;
     366       63281 :   gel(Q,2) = gel(P,2);
     367       63281 :   if (l == 3) return Q;
     368       63281 :   gel(Q,3) = shifti(gel(P,3), ni);
     369      251210 :   for (i=4; i<l; i++)
     370             :   {
     371      187929 :     ni += n;
     372      187929 :     gel(Q,i) = shifti(gel(P,i), ni);
     373             :   }
     374       63281 :   return Q;
     375             : }
     376             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     377             : GEN
     378        1190 : ZX_unscale_div(GEN P, GEN h)
     379             : {
     380        1190 :   long i, l = lg(P);
     381        1190 :   GEN hi, Q = cgetg(l, t_POL);
     382        1190 :   Q[1] = P[1];
     383        1190 :   if (l == 2) return Q;
     384        1190 :   gel(Q,2) = diviiexact(gel(P,2), h);
     385        1190 :   if (l == 3) return Q;
     386        1190 :   gel(Q,3) = gel(P,3);
     387        1190 :   if (l == 4) return Q;
     388        1190 :   hi = h;
     389        1190 :   gel(Q,4) = mulii(gel(P,4), hi);
     390        5257 :   for (i=5; i<l; i++)
     391             :   {
     392        4067 :     hi = mulii(hi,h);
     393        4067 :     gel(Q,i) = mulii(gel(P,i), hi);
     394             :   }
     395        1190 :   return Q;
     396             : }
     397             : 
     398             : GEN
     399         651 : RgXV_unscale(GEN v, GEN h)
     400             : {
     401             :   long i, l;
     402             :   GEN w;
     403         651 :   if (!h || isint1(h)) return v;
     404         350 :   w = cgetg_copy(v, &l);
     405         350 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     406         350 :   return w;
     407             : }
     408             : 
     409             : /* Return h^degpol(P) P(x / h), not memory clean */
     410             : GEN
     411      479918 : RgX_rescale(GEN P, GEN h)
     412             : {
     413      479918 :   long i, l = lg(P);
     414      479918 :   GEN Q = cgetg(l,t_POL), hi = h;
     415      479918 :   gel(Q,l-1) = gel(P,l-1);
     416     1741903 :   for (i=l-2; i>=2; i--)
     417             :   {
     418     1739656 :     gel(Q,i) = gmul(gel(P,i), hi);
     419     1739656 :     if (i == 2) break;
     420     1261985 :     hi = gmul(hi,h);
     421             :   }
     422      479918 :   Q[1] = P[1]; return Q;
     423             : }
     424             : 
     425             : /* A(X^d) --> A(X) */
     426             : GEN
     427      151498 : RgX_deflate(GEN x0, long d)
     428             : {
     429             :   GEN z, y, x;
     430      151498 :   long i,id, dy, dx = degpol(x0);
     431      151498 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     432       81159 :   dy = dx/d;
     433       81159 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     434       81159 :   z = y + 2;
     435       81159 :   x = x0+ 2;
     436       81159 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     437       81159 :   return y;
     438             : }
     439             : 
     440             : /* F a t_RFRAC */
     441             : long
     442          63 : rfrac_deflate_order(GEN F)
     443             : {
     444          63 :   GEN N = gel(F,1), D = gel(F,2);
     445          63 :   long m = (degpol(D) <= 0)? 0: RgX_deflate_order(D);
     446          63 :   if (m == 1) return 1;
     447          28 :   if (typ(N) == t_POL && varn(N) == varn(D))
     448          14 :     m = cgcd(m, RgX_deflate_order(N));
     449          28 :   return m;
     450             : }
     451             : /* F a t_RFRAC */
     452             : GEN
     453          63 : rfrac_deflate_max(GEN F, long *m)
     454             : {
     455          63 :   *m = rfrac_deflate_order(F);
     456          63 :   return rfrac_deflate(F, *m);
     457             : }
     458             : /* F a t_RFRAC */
     459             : GEN
     460          63 : rfrac_deflate(GEN F, long m)
     461             : {
     462          63 :   GEN N = gel(F,1), D = gel(F,2);
     463          63 :   if (m == 1) return F;
     464          28 :   if (typ(N) == t_POL && varn(N) == varn(D)) N = RgX_deflate(N, m);
     465          28 :   D = RgX_deflate(D, m); return mkrfrac(N, D);
     466             : }
     467             : 
     468             : 
     469             : /* return x0(X^d) */
     470             : GEN
     471      594415 : RgX_inflate(GEN x0, long d)
     472             : {
     473      594415 :   long i, id, dy, dx = degpol(x0);
     474      594415 :   GEN x = x0 + 2, z, y;
     475      594415 :   if (dx <= 0) return leafcopy(x0);
     476      587163 :   dy = dx*d;
     477      587163 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     478      587162 :   z = y + 2;
     479      587162 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     480      587162 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     481      587162 :   return y;
     482             : }
     483             : 
     484             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     485             : static GEN
     486     1454474 : RgX_translate_basecase(GEN P, GEN c)
     487             : {
     488     1454474 :   pari_sp av = avma;
     489             :   GEN Q, R;
     490             :   long i, k, n;
     491             : 
     492     1454474 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     493     1452014 :   Q = leafcopy(P);
     494     1452014 :   R = Q+2; n = degpol(P);
     495     1452014 :   if (isint1(c))
     496             :   {
     497        9086 :     for (i=1; i<=n; i++)
     498             :     {
     499        6713 :       for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gel(R,k+1));
     500        6713 :       if (gc_needed(av,2))
     501             :       {
     502           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate(1), i = %ld/%ld", i,n);
     503           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     504             :       }
     505             :     }
     506             :   }
     507     1449641 :   else if (isintm1(c))
     508             :   {
     509      162081 :     for (i=1; i<=n; i++)
     510             :     {
     511      137816 :       for (k=n-i; k<n; k++) gel(R,k) = gsub(gel(R,k), gel(R,k+1));
     512      137816 :       if (gc_needed(av,2))
     513             :       {
     514           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate(-1), i = %ld/%ld", i,n);
     515           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     516             :       }
     517             :     }
     518             :   }
     519             :   else
     520             :   {
     521     4944384 :     for (i=1; i<=n; i++)
     522             :     {
     523     3519008 :       for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gmul(c, gel(R,k+1)));
     524     3519008 :       if (gc_needed(av,2))
     525             :       {
     526           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate, i = %ld/%ld", i,n);
     527           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     528             :       }
     529             :     }
     530             :   }
     531     1452014 :   return gerepilecopy(av, Q);
     532             : }
     533             : GEN
     534     1454782 : RgX_translate(GEN P, GEN c)
     535             : {
     536     1454782 :   pari_sp av = avma;
     537     1454782 :   long n = degpol(P);
     538     1454782 :   if (n < 40)
     539     1454474 :     return RgX_translate_basecase(P, c);
     540             :   else
     541             :   {
     542         308 :     long d = n >> 1;
     543         308 :     GEN Q = RgX_translate(RgX_shift_shallow(P, -d), c);
     544         308 :     GEN R = RgX_translate(RgXn_red_shallow(P, d), c);
     545         308 :     GEN S = gpowgs(deg1pol_shallow(gen_1, c, varn(P)), d);
     546         308 :     return gerepileupto(av, RgX_add(RgX_mul(Q, S), R));
     547             :   }
     548             : }
     549             : 
     550             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     551             : GEN
     552       21882 : RgXQX_translate(GEN P, GEN c, GEN T)
     553             : {
     554       21882 :   pari_sp av = avma;
     555             :   GEN Q, R;
     556             :   long i, k, n;
     557             : 
     558       21882 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     559       21574 :   Q = leafcopy(P);
     560       21574 :   R = Q+2; n = degpol(P);
     561       76951 :   for (i=1; i<=n; i++)
     562             :   {
     563      248507 :     for (k=n-i; k<n; k++)
     564             :     {
     565      193130 :       pari_sp av2 = avma;
     566      579390 :       gel(R,k) = gerepileupto(av2,
     567      386260 :                    RgX_rem(gadd(gel(R,k), gmul(c, gel(R,k+1))), T));
     568             :     }
     569       55377 :     if (gc_needed(av,2))
     570             :     {
     571           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     572           0 :       Q = gerepilecopy(av, Q); R = Q+2;
     573             :     }
     574             :   }
     575       21574 :   return gerepilecopy(av, Q);
     576             : }
     577             : 
     578             : /********************************************************************/
     579             : /**                                                                **/
     580             : /**                          CONVERSIONS                           **/
     581             : /**                       (not memory clean)                       **/
     582             : /**                                                                **/
     583             : /********************************************************************/
     584             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     585             :  * but everything else is */
     586             : static GEN
     587       31724 : QXQ_to_mod_copy(GEN x, GEN T)
     588             : {
     589             :   long d;
     590       31724 :   switch(typ(x))
     591             :   {
     592       13020 :     case t_INT:  return icopy(x);
     593         805 :     case t_FRAC: return gcopy(x);
     594             :     case t_POL:
     595       17899 :       d = degpol(x);
     596       17899 :       if (d < 0) return gen_0;
     597       17157 :       if (d == 0) return gcopy(gel(x,2));
     598       16751 :       return mkpolmod(RgX_copy(x), T);
     599           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     600             :              return NULL;/* LCOV_EXCL_LINE */
     601             :   }
     602             : }
     603             : /* pure shallow version */
     604             : static GEN
     605     1218561 : QXQ_to_mod(GEN x, GEN T)
     606             : {
     607             :   long d;
     608     1218561 :   switch(typ(x))
     609             :   {
     610             :     case t_INT:
     611      459249 :     case t_FRAC: return x;
     612             :     case t_POL:
     613      759312 :       d = degpol(x);
     614      759312 :       if (d < 0) return gen_0;
     615      580391 :       if (d == 0) return gel(x,2);
     616      556739 :       return mkpolmod(x, T);
     617           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     618             :              return NULL;/* LCOV_EXCL_LINE */
     619             :   }
     620             : }
     621             : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod_copy to all coeffs.
     622             :  * Not memory clean because T not copied, but everything else is */
     623             : static GEN
     624        5873 : QXQX_to_mod(GEN z, GEN T)
     625             : {
     626        5873 :   long i,l = lg(z);
     627        5873 :   GEN x = cgetg(l,t_POL);
     628        5873 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     629        5873 :   x[1] = z[1]; return normalizepol_lg(x,l);
     630             : }
     631             : /* pure shallow version */
     632             : GEN
     633      189220 : QXQX_to_mod_shallow(GEN z, GEN T)
     634             : {
     635      189220 :   long i,l = lg(z);
     636      189220 :   GEN x = cgetg(l,t_POL);
     637      189220 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     638      189220 :   x[1] = z[1]; return normalizepol_lg(x,l);
     639             : }
     640             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     641             : GEN
     642        1764 : QXQXV_to_mod(GEN V, GEN T)
     643             : {
     644        1764 :   long i, l = lg(V);
     645        1764 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     646        1764 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     647        1764 :   return z;
     648             : }
     649             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     650             : GEN
     651        5180 : QXQV_to_mod(GEN V, GEN T)
     652             : {
     653        5180 :   long i, l = lg(V);
     654        5180 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     655        5180 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     656        5180 :   return z;
     657             : }
     658             : 
     659             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     660             : GEN
     661       23926 : QXQC_to_mod_shallow(GEN V, GEN T)
     662             : {
     663       23926 :   long i, l = lg(V);
     664       23926 :   GEN z = cgetg(l, t_COL);
     665       23926 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     666       23926 :   return z;
     667             : }
     668             : 
     669             : GEN
     670        7476 : QXQM_to_mod_shallow(GEN V, GEN T)
     671             : {
     672        7476 :   long i, l = lg(V);
     673        7476 :   GEN z = cgetg(l, t_MAT);
     674        7476 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     675        7476 :   return z;
     676             : }
     677             : 
     678             : GEN
     679      816958 : RgX_renormalize_lg(GEN x, long lx)
     680             : {
     681             :   long i;
     682     2204706 :   for (i = lx-1; i>1; i--)
     683     2059567 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     684      816958 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     685      816958 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     686             : }
     687             : 
     688             : GEN
     689      540706 : RgV_to_RgX(GEN x, long v)
     690             : {
     691      540706 :   long i, k = lg(x);
     692             :   GEN p;
     693             : 
     694      540706 :   while (--k && gequal0(gel(x,k)));
     695      540706 :   if (!k) return pol_0(v);
     696      534174 :   i = k+2; p = cgetg(i,t_POL);
     697      534173 :   p[1] = evalsigne(1) | evalvarn(v);
     698      534173 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     699      534173 :   return p;
     700             : }
     701             : GEN
     702      158980 : RgV_to_RgX_reverse(GEN x, long v)
     703             : {
     704      158980 :   long j, k, l = lg(x);
     705             :   GEN p;
     706             : 
     707      158980 :   for (k = 1; k < l; k++)
     708      158980 :     if (!gequal0(gel(x,k))) break;
     709      158980 :   if (k == l) return pol_0(v);
     710      158980 :   k -= 1;
     711      158980 :   l -= k;
     712      158980 :   x += k;
     713      158980 :   p = cgetg(l+1,t_POL);
     714      158980 :   p[1] = evalsigne(1) | evalvarn(v);
     715      158980 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     716      158980 :   return p;
     717             : }
     718             : 
     719             : /* return the (N-dimensional) vector of coeffs of p */
     720             : GEN
     721     7355587 : RgX_to_RgC(GEN x, long N)
     722             : {
     723             :   long i, l;
     724             :   GEN z;
     725     7355587 :   l = lg(x)-1; x++;
     726     7355587 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     727     7355587 :   z = cgetg(N+1,t_COL);
     728     7355633 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     729     7355633 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     730     7355633 :   return z;
     731             : }
     732             : GEN
     733      767071 : Rg_to_RgC(GEN x, long N)
     734             : {
     735      767071 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     736             : }
     737             : 
     738             : /* vector of polynomials (in v) whose coeffs are given by the columns of x */
     739             : GEN
     740       60807 : RgM_to_RgXV(GEN x, long v)
     741       60807 : { pari_APPLY_type(t_VEC, RgV_to_RgX(gel(x,i), v)) }
     742             : 
     743             : /* matrix whose entries are given by the coeffs of the polynomials in
     744             :  * vector v (considered as degree n-1 polynomials) */
     745             : GEN
     746      136330 : RgV_to_RgM(GEN x, long n)
     747      136330 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     748             : 
     749             : GEN
     750        5685 : RgXV_to_RgM(GEN x, long n)
     751        5685 : { pari_APPLY_type(t_MAT, RgX_to_RgC(gel(x,i), n)) }
     752             : 
     753             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     754             : GEN
     755       17629 : RgM_to_RgXX(GEN x, long v,long w)
     756             : {
     757       17629 :   long j, lx = lg(x);
     758       17629 :   GEN y = cgetg(lx+1, t_POL);
     759       17629 :   y[1] = evalsigne(1) | evalvarn(v);
     760       17629 :   y++;
     761       17629 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     762       17629 :   return normalizepol_lg(--y, lx+1);
     763             : }
     764             : 
     765             : /* matrix whose entries are given by the coeffs of the polynomial v in
     766             :  * two variables (considered as degree n-1 polynomials) */
     767             : GEN
     768         308 : RgXX_to_RgM(GEN v, long n)
     769             : {
     770         308 :   long j, N = lg(v)-1;
     771         308 :   GEN y = cgetg(N, t_MAT);
     772         308 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     773         308 :   return y;
     774             : }
     775             : 
     776             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     777             : GEN
     778       20512 : RgXY_swapspec(GEN x, long n, long w, long nx)
     779             : {
     780       20512 :   long j, ly = n+3;
     781       20512 :   GEN y = cgetg(ly, t_POL);
     782       20512 :   y[1] = evalsigne(1);
     783      256865 :   for (j=2; j<ly; j++)
     784             :   {
     785             :     long k;
     786      236353 :     GEN a = cgetg(nx+2,t_POL);
     787      236353 :     a[1] = evalsigne(1) | evalvarn(w);
     788     1205903 :     for (k=0; k<nx; k++)
     789             :     {
     790      969550 :       GEN xk = gel(x,k);
     791      969550 :       if (typ(xk)==t_POL)
     792      894241 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     793             :       else
     794       75309 :         gel(a,k+2) = j==2 ? xk: gen_0;
     795             :     }
     796      236353 :     gel(y,j) = normalizepol_lg(a, nx+2);
     797             :   }
     798       20512 :   return normalizepol_lg(y,ly);
     799             : }
     800             : 
     801             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     802             : GEN
     803         952 : RgXY_swap(GEN x, long n, long w)
     804             : {
     805         952 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     806         952 :   setvarn(z, varn(x)); return z;
     807             : }
     808             : 
     809             : long
     810         176 : RgXY_degreex(GEN b)
     811             : {
     812         176 :   long deg = -1, i;
     813         176 :   if (!signe(b)) return -1;
     814         990 :   for (i = 2; i < lg(b); ++i)
     815             :   {
     816         814 :     GEN bi = gel(b, i);
     817         814 :     if (typ(bi) == t_POL)
     818         785 :       deg = maxss(deg, degpol(bi));
     819             :   }
     820         176 :   return deg;
     821             : }
     822             : 
     823             : /* return (x % X^n). Shallow */
     824             : GEN
     825     2263729 : RgXn_red_shallow(GEN a, long n)
     826             : {
     827     2263729 :   long i, L = n+2, l = lg(a);
     828             :   GEN  b;
     829     2263729 :   if (L >= l) return a; /* deg(x) < n */
     830     1568001 :   b = cgetg(L, t_POL); b[1] = a[1];
     831     1568001 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     832     1568001 :   return normalizepol_lg(b,L);
     833             : }
     834             : 
     835             : GEN
     836         357 : RgXnV_red_shallow(GEN x, long n)
     837         357 : { pari_APPLY_type(t_VEC, RgXn_red_shallow(gel(x,i), n)) }
     838             : 
     839             : /* return (x * X^n). Shallow */
     840             : GEN
     841    71961229 : RgX_shift_shallow(GEN a, long n)
     842             : {
     843    71961229 :   long i, l = lg(a);
     844             :   GEN  b;
     845    71961229 :   if (l == 2 || !n) return a;
     846    52255685 :   l += n;
     847    52255685 :   if (n < 0)
     848             :   {
     849    43108039 :     if (l <= 2) return pol_0(varn(a));
     850    43081551 :     b = cgetg(l, t_POL); b[1] = a[1];
     851    43081551 :     a -= n;
     852    43081551 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     853             :   } else {
     854     9147646 :     b = cgetg(l, t_POL); b[1] = a[1];
     855     9147646 :     a -= n; n += 2;
     856     9147646 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     857     9147646 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     858             :   }
     859    52229197 :   return b;
     860             : }
     861             : /* return (x * X^n). */
     862             : GEN
     863     1534383 : RgX_shift(GEN a, long n)
     864             : {
     865     1534383 :   long i, l = lg(a);
     866             :   GEN  b;
     867     1534383 :   if (l == 2 || !n) return RgX_copy(a);
     868     1534103 :   l += n;
     869     1534103 :   if (n < 0)
     870             :   {
     871         630 :     if (l <= 2) return pol_0(varn(a));
     872         588 :     b = cgetg(l, t_POL); b[1] = a[1];
     873         588 :     a -= n;
     874         588 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     875             :   } else {
     876     1533473 :     b = cgetg(l, t_POL); b[1] = a[1];
     877     1533473 :     a -= n; n += 2;
     878     1533473 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     879     1533473 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     880             :   }
     881     1534061 :   return b;
     882             : }
     883             : 
     884             : GEN
     885      316113 : RgX_rotate_shallow(GEN P, long k, long p)
     886             : {
     887      316113 :   long i, l = lgpol(P);
     888             :   GEN r;
     889      316113 :   if (signe(P)==0)
     890        1365 :     return pol_0(varn(P));
     891      314748 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     892     2090452 :   for(i=0; i<p; i++)
     893             :   {
     894     1775704 :     long s = 2+(i+k)%p;
     895     1775704 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     896             :   }
     897      314748 :   return RgX_renormalize(r);
     898             : }
     899             : 
     900             : GEN
     901     2974898 : RgX_mulXn(GEN x, long d)
     902             : {
     903             :   pari_sp av;
     904             :   GEN z;
     905             :   long v;
     906     2974898 :   if (d >= 0) return RgX_shift(x, d);
     907     1463408 :   d = -d;
     908     1463408 :   v = RgX_val(x);
     909     1463408 :   if (v >= d) return RgX_shift(x, -d);
     910     1463394 :   av = avma;
     911     1463394 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     912     1463394 :   return gerepileupto(av, z);
     913             : }
     914             : 
     915             : long
     916     2497069 : RgX_val(GEN x)
     917             : {
     918     2497069 :   long i, lx = lg(x);
     919     2497069 :   if (lx == 2) return LONG_MAX;
     920     2530473 :   for (i = 2; i < lx; i++)
     921     2530431 :     if (!isexactzero(gel(x,i))) break;
     922     2496929 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     923     2496887 :   return i - 2;
     924             : }
     925             : long
     926    39008767 : RgX_valrem(GEN x, GEN *Z)
     927             : {
     928    39008767 :   long v, i, lx = lg(x);
     929    39008767 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     930    73806823 :   for (i = 2; i < lx; i++)
     931    73806823 :     if (!isexactzero(gel(x,i))) break;
     932             :   /* possible with non-rational zeros */
     933    39008767 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     934    39008767 :   v = i - 2;
     935    39008767 :   *Z = RgX_shift_shallow(x, -v);
     936    39008767 :   return v;
     937             : }
     938             : long
     939       16471 : RgX_valrem_inexact(GEN x, GEN *Z)
     940             : {
     941             :   long v;
     942       16471 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     943       16954 :   for (v = 0;; v++)
     944       17444 :     if (!gequal0(gel(x,2+v))) break;
     945       16464 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     946       16464 :   return v;
     947             : }
     948             : 
     949             : GEN
     950      109186 : RgXQC_red(GEN x, GEN T)
     951      109186 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
     952             : 
     953             : GEN
     954        1106 : RgXQV_red(GEN x, GEN T)
     955        1106 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
     956             : 
     957             : GEN
     958       14826 : RgXQM_red(GEN x, GEN T)
     959       14826 : { pari_APPLY_same(RgXQC_red(gel(x,i), T)) }
     960             : 
     961             : GEN
     962           0 : RgXQM_mul(GEN P, GEN Q, GEN T)
     963             : {
     964           0 :   return RgXQM_red(RgM_mul(P, Q), T);
     965             : }
     966             : 
     967             : GEN
     968      550545 : RgXQX_red(GEN P, GEN T)
     969             : {
     970      550545 :   long i, l = lg(P);
     971      550545 :   GEN Q = cgetg(l, t_POL);
     972      550545 :   Q[1] = P[1];
     973      550545 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     974      550545 :   return normalizepol_lg(Q, l);
     975             : }
     976             : 
     977             : GEN
     978      226067 : RgX_deriv(GEN x)
     979             : {
     980      226067 :   long i,lx = lg(x)-1;
     981             :   GEN y;
     982             : 
     983      226067 :   if (lx<3) return pol_0(varn(x));
     984      224597 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     985      224597 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     986      224597 :   y[1] = x[1]; return normalizepol_lg(y,i);
     987             : }
     988             : 
     989             : GEN
     990      493650 : RgX_recipspec_shallow(GEN x, long l, long n)
     991             : {
     992             :   long i;
     993      493650 :   GEN z = cgetg(n+2,t_POL);
     994      493655 :   z[1] = 0; z += 2;
     995    15162149 :   for(i=0; i<l; i++)
     996    14668494 :     gel(z,n-i-1) = gel(x,i);
     997      621080 :   for(   ; i<n; i++)
     998      127425 :     gel(z, n-i-1) = gen_0;
     999      493655 :   return normalizepol_lg(z-2,n+2);
    1000             : }
    1001             : 
    1002             : GEN
    1003       47427 : RgXn_recip_shallow(GEN P, long n)
    1004             : {
    1005       47427 :   GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
    1006       47427 :   setvarn(Q, varn(P));
    1007       47427 :   return Q;
    1008             : }
    1009             : 
    1010             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
    1011             : GEN
    1012        2170 : RgX_recip(GEN x)
    1013             : {
    1014             :   long lx, i, j;
    1015        2170 :   GEN y = cgetg_copy(x, &lx);
    1016        2170 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
    1017        2170 :   return normalizepol_lg(y,lx);
    1018             : }
    1019             : /* shallow version */
    1020             : GEN
    1021      460036 : RgX_recip_shallow(GEN x)
    1022             : {
    1023             :   long lx, i, j;
    1024      460036 :   GEN y = cgetg_copy(x, &lx);
    1025      460047 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
    1026      460047 :   return y;
    1027             : }
    1028             : /*******************************************************************/
    1029             : /*                                                                 */
    1030             : /*                      ADDITION / SUBTRACTION                     */
    1031             : /*                                                                 */
    1032             : /*******************************************************************/
    1033             : /* same variable */
    1034             : GEN
    1035    22244968 : RgX_add(GEN x, GEN y)
    1036             : {
    1037    22244968 :   long i, lx = lg(x), ly = lg(y);
    1038             :   GEN z;
    1039    22244968 :   if (ly <= lx) {
    1040    19681304 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1041    19681304 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1042    19681304 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1043    19681304 :     z = normalizepol_lg(z, lx);
    1044             :   } else {
    1045     2563664 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1046     2563664 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1047     2563664 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1048     2563664 :     z = normalizepol_lg(z, ly);
    1049             :   }
    1050    22244968 :   return z;
    1051             : }
    1052             : GEN
    1053    22779167 : RgX_sub(GEN x, GEN y)
    1054             : {
    1055    22779167 :   long i, lx = lg(x), ly = lg(y);
    1056             :   GEN z;
    1057    22779167 :   if (ly <= lx) {
    1058    19039225 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1059    19039225 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1060    19039225 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1061    19039225 :     z = normalizepol_lg(z, lx);
    1062             :   } else {
    1063     3739942 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1064     3739942 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1065     3739942 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1066     3739942 :     z = normalizepol_lg(z, ly);
    1067             :   }
    1068    22779167 :   return z;
    1069             : }
    1070             : GEN
    1071     1385573 : RgX_neg(GEN x)
    1072             : {
    1073     1385573 :   long i, lx = lg(x);
    1074     1385573 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1075     1385573 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1076     1385573 :   return y;
    1077             : }
    1078             : 
    1079             : GEN
    1080    12657696 : RgX_Rg_add(GEN y, GEN x)
    1081             : {
    1082             :   GEN z;
    1083    12657696 :   long lz = lg(y), i;
    1084    12657696 :   if (lz == 2) return scalarpol(x,varn(y));
    1085     9694411 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1086     9694411 :   gel(z,2) = gadd(gel(y,2),x);
    1087     9694411 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1088             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1089             :    * an inexact 0 */
    1090     9694411 :   return normalizepol_lg(z,lz);
    1091             : }
    1092             : GEN
    1093        2879 : RgX_Rg_add_shallow(GEN y, GEN x)
    1094             : {
    1095             :   GEN z;
    1096        2879 :   long lz = lg(y), i;
    1097        2879 :   if (lz == 2) return scalarpol(x,varn(y));
    1098        2879 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1099        2879 :   gel(z,2) = gadd(gel(y,2),x);
    1100        2879 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1101        2879 :   return z = normalizepol_lg(z,lz);
    1102             : }
    1103             : GEN
    1104       33256 : RgX_Rg_sub(GEN y, GEN x)
    1105             : {
    1106             :   GEN z;
    1107       33256 :   long lz = lg(y), i;
    1108       33256 :   if (lz == 2)
    1109             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1110        2576 :     long v = varn(y);
    1111        2576 :     if (isrationalzero(x)) return pol_0(v);
    1112          14 :     z = cgetg(3,t_POL);
    1113          28 :     z[1] = gequal0(x)? evalvarn(v)
    1114          14 :                    : evalvarn(v) | evalsigne(1);
    1115          14 :     gel(z,2) = gneg(x); return z;
    1116             :   }
    1117       30680 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1118       30680 :   gel(z,2) = gsub(gel(y,2),x);
    1119       30680 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1120       30680 :   return z = normalizepol_lg(z,lz);
    1121             : }
    1122             : GEN
    1123      583219 : Rg_RgX_sub(GEN x, GEN y)
    1124             : {
    1125             :   GEN z;
    1126      583219 :   long lz = lg(y), i;
    1127      583219 :   if (lz == 2) return scalarpol(x,varn(y));
    1128      582127 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1129      582127 :   gel(z,2) = gsub(x, gel(y,2));
    1130      582127 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1131      582127 :   return z = normalizepol_lg(z,lz);
    1132             : }
    1133             : /*******************************************************************/
    1134             : /*                                                                 */
    1135             : /*                  KARATSUBA MULTIPLICATION                       */
    1136             : /*                                                                 */
    1137             : /*******************************************************************/
    1138             : #if 0
    1139             : /* to debug Karatsuba-like routines */
    1140             : GEN
    1141             : zx_debug_spec(GEN x, long nx)
    1142             : {
    1143             :   GEN z = cgetg(nx+2,t_POL);
    1144             :   long i;
    1145             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1146             :   z[1] = evalsigne(1); return z;
    1147             : }
    1148             : 
    1149             : GEN
    1150             : RgX_debug_spec(GEN x, long nx)
    1151             : {
    1152             :   GEN z = cgetg(nx+2,t_POL);
    1153             :   long i;
    1154             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1155             :   z[1] = evalsigne(1); return z;
    1156             : }
    1157             : #endif
    1158             : 
    1159             : /* generic multiplication */
    1160             : GEN
    1161     3292424 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1162             : {
    1163             :   GEN z, t;
    1164             :   long i;
    1165     3292424 :   if (nx == ny) {
    1166      678126 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1167      678126 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1168      678126 :     return normalizepol_lg(z, nx+2);
    1169             :   }
    1170     2614298 :   if (ny < nx) {
    1171     2455503 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1172     2455503 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1173     2455503 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1174     2455503 :     return normalizepol_lg(z, nx+2);
    1175             :   } else {
    1176      158795 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1177      158795 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1178      158795 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1179      158795 :     return normalizepol_lg(z, ny+2);
    1180             :   }
    1181             : }
    1182             : GEN
    1183      210814 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1184             : {
    1185             :   GEN z, t;
    1186             :   long i;
    1187      210814 :   if (nx == ny) {
    1188       12425 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1189       12425 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1190       12425 :     return normalizepol_lg(z, nx+2);
    1191             :   }
    1192      198389 :   if (ny < nx) {
    1193      196688 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1194      196688 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1195      196688 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1196      196688 :     return normalizepol_lg(z, nx+2);
    1197             :   } else {
    1198        1701 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1199        1701 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1200        1701 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1201        1701 :     return normalizepol_lg(z, ny+2);
    1202             :   }
    1203             : }
    1204             : 
    1205             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1206             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1207             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1208             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1209             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1210             : static GEN
    1211    21486339 : RgXspec_kill0(GEN x, long lx)
    1212             : {
    1213    21486339 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1214             :   long i;
    1215    87196922 :   for (i=0; i <lx; i++)
    1216             :   {
    1217    65710583 :     GEN c = gel(x,i);
    1218    65710583 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1219             :   }
    1220    21486339 :   return z;
    1221             : }
    1222             : 
    1223             : INLINE GEN
    1224    48205189 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1225             : {
    1226    48205189 :   pari_sp av = avma;
    1227    48205189 :   GEN s = NULL;
    1228             :   long i;
    1229             : 
    1230   228478546 :   for (i=a; i<b; i++)
    1231   180273357 :     if (gel(y,i) && gel(x,-i))
    1232             :     {
    1233   102261974 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1234   102261974 :       s = s? gadd(s, t): t;
    1235             :     }
    1236    48205189 :   return s? gerepileupto(av, s): gen_0;
    1237             : }
    1238             : 
    1239             : /* assume nx >= ny > 0, return x * y * t^v */
    1240             : static GEN
    1241     8545676 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1242             : {
    1243             :   long i, lz, nz;
    1244             :   GEN z;
    1245             : 
    1246     8545676 :   x = RgXspec_kill0(x,nx);
    1247     8545676 :   y = RgXspec_kill0(y,ny);
    1248     8545676 :   lz = nx + ny + 1; nz = lz-2;
    1249     8545676 :   lz += v;
    1250     8545676 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1251     8545676 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1252     8545676 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1253     8545676 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1254     8545676 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1255     8545676 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1256             : }
    1257             : 
    1258             : /* return (x * X^d) + y. Assume d > 0 */
    1259             : GEN
    1260     2593812 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1261             : {
    1262             :   GEN x, y, xd, yd, zd;
    1263             :   long a, lz, nx, ny;
    1264             : 
    1265     2593812 :   if (!signe(x0)) return y0;
    1266     2567405 :   ny = lgpol(y0);
    1267     2567405 :   nx = lgpol(x0);
    1268     2567405 :   zd = (GEN)avma;
    1269     2567405 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1270     2567405 :   if (a <= 0)
    1271             :   {
    1272      217943 :     lz = nx+d+2;
    1273      217943 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1274      217943 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1275      217943 :     x = zd + a;
    1276      217943 :     while (zd > x) gel(--zd,0) = gen_0;
    1277             :   }
    1278             :   else
    1279             :   {
    1280     2349462 :     xd = new_chunk(d); yd = y+d;
    1281     2349462 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1282     2349462 :     lz = (a>nx)? ny+2: lg(x)+d;
    1283     2349462 :     x += 2; while (xd > x) *--zd = *--xd;
    1284             :   }
    1285     2567405 :   while (yd > y) *--zd = *--yd;
    1286     2567405 :   *--zd = x0[1];
    1287     2567405 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1288             : }
    1289             : GEN
    1290      502778 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1291             : {
    1292             :   GEN x, y, xd, yd, zd;
    1293             :   long a, lz, nx, ny;
    1294             : 
    1295      502778 :   if (!signe(x0)) return RgX_copy(y0);
    1296      502204 :   nx = lgpol(x0);
    1297      502204 :   ny = lgpol(y0);
    1298      502204 :   zd = (GEN)avma;
    1299      502204 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1300      502204 :   if (a <= 0)
    1301             :   {
    1302      291390 :     lz = nx+d+2;
    1303      291390 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1304      291390 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1305      291390 :     x = zd + a;
    1306      291390 :     while (zd > x) gel(--zd,0) = gen_0;
    1307             :   }
    1308             :   else
    1309             :   {
    1310      210814 :     xd = new_chunk(d); yd = y+d;
    1311      210814 :     x = RgX_addspec(x,yd, nx,a);
    1312      210814 :     lz = (a>nx)? ny+2: lg(x)+d;
    1313      210814 :     x += 2; while (xd > x) *--zd = *--xd;
    1314             :   }
    1315      502204 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1316      502204 :   *--zd = x0[1];
    1317      502204 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1318             : }
    1319             : 
    1320             : /* return x * y mod t^n */
    1321             : static GEN
    1322     2193645 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1323             : {
    1324     2193645 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1325             :   GEN z;
    1326     2193645 :   if (lx < 0) return pol_0(varn(x));
    1327     2193645 :   if (ly < 0) return pol_0(varn(x));
    1328     2193645 :   z = cgetg(lz, t_POL) + 2;
    1329     2193645 :   x+=2; if (lx > n) lx = n;
    1330     2193645 :   y+=2; if (ly > n) ly = n;
    1331     2193645 :   z[-1] = x[-1];
    1332     2193645 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1333     2193645 :   x = RgXspec_kill0(x, lx);
    1334     2193645 :   y = RgXspec_kill0(y, ly);
    1335             :   /* x:y:z [i] = term of degree i */
    1336     2193645 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1337     2193645 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1338     2193645 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1339     2193645 :   return normalizepol_lg(z - 2, lz);
    1340             : }
    1341             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1342             : static GEN
    1343     2285044 : RgXn_mul2(GEN f, GEN g, long n)
    1344             : {
    1345     2285044 :   pari_sp av = avma;
    1346             :   GEN fe,fo, ge,go, l,h,m;
    1347             :   long n0, n1;
    1348     2285044 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1349     2223591 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1350       29946 :   n0 = n>>1; n1 = n-n0;
    1351       29946 :   RgX_even_odd(f, &fe, &fo);
    1352       29946 :   RgX_even_odd(g, &ge, &go);
    1353       29946 :   l = RgXn_mul(fe,ge,n1);
    1354       29946 :   h = RgXn_mul(fo,go,n0);
    1355       29946 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1356             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1357             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1358       29946 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1359             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1360       29946 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1361       29946 :   m = RgX_inflate(m,2);
    1362             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1363       29946 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1364       29946 :   h = RgX_inflate(h,2);
    1365       29946 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1366       29946 :   return gerepileupto(av, h);
    1367             : }
    1368             : /* (f*g) \/ x^n */
    1369             : static GEN
    1370        3044 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1371             : {
    1372        3044 :   long d = degpol(f)+degpol(g) + 1 - n;
    1373             :   GEN h;
    1374        3044 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1375        1886 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1376             :                                  RgX_recip_shallow(g), d));
    1377        1886 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1378             : }
    1379             : 
    1380             : /* (f*g) \/ x^n */
    1381             : static GEN
    1382           0 : RgX_sqrhigh_i2(GEN f, long n)
    1383             : {
    1384           0 :   long d = 2*degpol(f)+ 1 - n;
    1385             :   GEN h;
    1386           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1387           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1388           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1389             : }
    1390             : 
    1391             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1392             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1393             :  * Only c, c0, c1, c2 are genuine GEN.
    1394             :  */
    1395             : GEN
    1396     9118769 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1397             : {
    1398             :   GEN a0, c, c0;
    1399     9118769 :   long n0, n0a, i, v = 0;
    1400             :   pari_sp av;
    1401             : 
    1402     9118769 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1403     9118769 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1404     9118769 :   if (na < nb) swapspec(a,b, na,nb);
    1405     9118769 :   if (!nb) return pol_0(0);
    1406             : 
    1407     9018469 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1408      472793 :   RgX_shift_inplace_init(v);
    1409      472793 :   i = (na>>1); n0 = na-i; na = i;
    1410      472793 :   av = avma; a0 = a+n0; n0a = n0;
    1411      472793 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1412             : 
    1413      472793 :   if (nb > n0)
    1414             :   {
    1415             :     GEN b0,c1,c2;
    1416             :     long n0b;
    1417             : 
    1418      471481 :     nb -= n0; b0 = b+n0; n0b = n0;
    1419      471481 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1420      471481 :     c = RgX_mulspec(a,b,n0a,n0b);
    1421      471481 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1422             : 
    1423      471481 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1424      471481 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1425             : 
    1426      471481 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1427      471481 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1428      471481 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1429             :   }
    1430             :   else
    1431             :   {
    1432        1312 :     c = RgX_mulspec(a,b,n0a,nb);
    1433        1312 :     c0 = RgX_mulspec(a0,b,na,nb);
    1434             :   }
    1435      472793 :   c0 = RgX_addmulXn(c0,c,n0);
    1436      472793 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1437             : }
    1438             : 
    1439             : INLINE GEN
    1440       41975 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1441             : {
    1442       41975 :   pari_sp av = avma;
    1443       41975 :   GEN s = NULL;
    1444       41975 :   long j, l = (i+1)>>1;
    1445      120652 :   for (j=a; j<l; j++)
    1446             :   {
    1447       78677 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1448       78677 :     if (xj && xx)
    1449             :     {
    1450       71355 :       GEN t = gmul(xj, xx);
    1451       71355 :       s = s? gadd(s, t): t;
    1452             :     }
    1453             :   }
    1454       41975 :   if (s) s = gshift(s,1);
    1455       41975 :   if ((i&1) == 0)
    1456             :   {
    1457       24836 :     GEN t = gel(x, i>>1);
    1458       24836 :     if (t) {
    1459       22519 :       t = gsqr(t);
    1460       22519 :       s = s? gadd(s, t): t;
    1461             :     }
    1462             :   }
    1463       41975 :   return s? gerepileupto(av,s): gen_0;
    1464             : }
    1465             : static GEN
    1466        7697 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1467             : {
    1468             :   long i, lz, nz;
    1469             :   GEN z;
    1470             : 
    1471        7697 :   if (!nx) return pol_0(0);
    1472        7697 :   x = RgXspec_kill0(x,nx);
    1473        7697 :   lz = (nx << 1) + 1, nz = lz-2;
    1474        7697 :   lz += v;
    1475        7697 :   z = cgetg(lz,t_POL) + 2;
    1476        7697 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1477        7697 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1478        7697 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1479        7697 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1480             : }
    1481             : /* return x^2 mod t^n */
    1482             : static GEN
    1483           0 : RgXn_sqr_basecase(GEN x, long n)
    1484             : {
    1485           0 :   long i, lz = n+2, lx = lgpol(x);
    1486             :   GEN z;
    1487           0 :   if (lx < 0) return pol_0(varn(x));
    1488           0 :   z = cgetg(lz, t_POL);
    1489           0 :   z[1] = x[1];
    1490           0 :   x+=2; if (lx > n) lx = n;
    1491           0 :   x = RgXspec_kill0(x,lx);
    1492           0 :   z+=2;/* x:z [i] = term of degree i */
    1493           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1494           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1495           0 :   z -= 2; return normalizepol_lg(z, lz);
    1496             : }
    1497             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1498             : static GEN
    1499         238 : RgXn_sqr2(GEN f, long n)
    1500             : {
    1501         238 :   pari_sp av = avma;
    1502             :   GEN fe,fo, l,h,m;
    1503             :   long n0, n1;
    1504         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1505           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1506           0 :   n0 = n>>1; n1 = n-n0;
    1507           0 :   RgX_even_odd(f, &fe, &fo);
    1508           0 :   l = RgXn_sqr(fe,n1);
    1509           0 :   h = RgXn_sqr(fo,n0);
    1510           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1511             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1512             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1513           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1514             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1515           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1516           0 :   m = RgX_inflate(m,2);
    1517             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1518           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1519           0 :   h = RgX_inflate(h,2);
    1520           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1521           0 :   return gerepileupto(av, h);
    1522             : }
    1523             : GEN
    1524        7736 : RgX_sqrspec(GEN a, long na)
    1525             : {
    1526             :   GEN a0, c, c0, c1;
    1527        7736 :   long n0, n0a, i, v = 0;
    1528             :   pari_sp av;
    1529             : 
    1530        7736 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1531        7736 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1532          39 :   RgX_shift_inplace_init(v);
    1533          39 :   i = (na>>1); n0 = na-i; na = i;
    1534          39 :   av = avma; a0 = a+n0; n0a = n0;
    1535          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1536             : 
    1537          39 :   c = RgX_sqrspec(a,n0a);
    1538          39 :   c0 = RgX_sqrspec(a0,na);
    1539          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1540          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1541          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1542          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1543             : }
    1544             : 
    1545             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1546             : GEN
    1547      730211 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1548             : {
    1549      730211 :   GEN z = RgX_mul(A, B);
    1550      730211 :   if (a < b)
    1551        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1552      724765 :   else if (a > b)
    1553      446681 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1554             :   else
    1555      278084 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1556      730211 :   return z;
    1557             : }
    1558             : 
    1559             : GEN
    1560     7701663 : RgX_mul_i(GEN x, GEN y)
    1561             : {
    1562     7701663 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1563     7701663 :   setvarn(z, varn(x)); return z;
    1564             : }
    1565             : 
    1566             : GEN
    1567        7658 : RgX_sqr_i(GEN x)
    1568             : {
    1569        7658 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1570        7658 :   setvarn(z,varn(x)); return z;
    1571             : }
    1572             : 
    1573             : /*******************************************************************/
    1574             : /*                                                                 */
    1575             : /*                               DIVISION                          */
    1576             : /*                                                                 */
    1577             : /*******************************************************************/
    1578             : GEN
    1579      530691 : RgX_Rg_divexact(GEN x, GEN y) {
    1580      530691 :   long i, lx = lg(x);
    1581             :   GEN z;
    1582      530691 :   if (lx == 2) return gcopy(x);
    1583      496608 :   switch(typ(y))
    1584             :   {
    1585             :     case t_INT:
    1586      476647 :       if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1587      443072 :       break;
    1588        4935 :     case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
    1589             :   }
    1590      458098 :   z = cgetg(lx, t_POL); z[1] = x[1];
    1591      458098 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1592      458098 :   return z;
    1593             : }
    1594             : GEN
    1595    23912932 : RgX_Rg_div(GEN x, GEN y) {
    1596    23912932 :   long i, lx = lg(x);
    1597             :   GEN z;
    1598    23912932 :   if (lx == 2) return gcopy(x);
    1599    23682922 :   switch(typ(y))
    1600             :   {
    1601             :     case t_INT:
    1602    14562795 :       if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1603      843457 :       break;
    1604        4158 :     case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
    1605             :   }
    1606     9959426 :   z = cgetg(lx, t_POL); z[1] = x[1];
    1607     9959426 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1608     9959426 :   return normalizepol_lg(z, lx);
    1609             : }
    1610             : GEN
    1611       13412 : RgX_normalize(GEN x)
    1612             : {
    1613       13412 :   GEN z, d = NULL;
    1614       13412 :   long i, n = lg(x)-1;
    1615       13412 :   for (i = n; i > 1; i--) { d = gel(x,i); if (!gequal0(d)) break; }
    1616       13412 :   if (i == 1) return pol_0(varn(x));
    1617       13412 :   if (i == n && isint1(d)) return x;
    1618        5019 :   n = i; z = cgetg(n+1, t_POL); z[1] = x[1];
    1619        5019 :   for (i=2; i<n; i++) gel(z,i) = gdiv(gel(x,i),d);
    1620        5019 :   gel(z,n) = Rg_get_1(d); return z;
    1621             : }
    1622             : GEN
    1623        2625 : RgX_divs(GEN x, long y) {
    1624             :   long i, lx;
    1625        2625 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1626        2625 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1627        2625 :   return normalizepol_lg(z, lx);
    1628             : }
    1629             : GEN
    1630       54789 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1631             : {
    1632       54789 :   long l = lg(a), i;
    1633       54789 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1634       54789 :   z[1] = a[1];
    1635       54789 :   a0 = a + l-1;
    1636       54789 :   z0 = z + l-2; *z0 = *a0--;
    1637      943670 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1638             :   {
    1639      888881 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1640      888881 :     gel(z0,0) = t;
    1641             :   }
    1642       54789 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1643       54789 :   return z;
    1644             : }
    1645             : /* Polynomial division x / y:
    1646             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1647             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1648             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1649             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1650             : static GEN
    1651    12721727 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1652             : {
    1653             :   pari_sp avy, av, av1;
    1654             :   long dx,dy,dz,i,j,sx,lr;
    1655             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1656             :   GEN (*f)(GEN,GEN);
    1657             : 
    1658    12721727 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1659             : 
    1660    12721727 :   dy = degpol(y);
    1661    12721727 :   y_lead = gel(y,dy+2);
    1662    12721727 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1663             :   {
    1664           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1665           0 :     for (dy--; dy>=0; dy--)
    1666             :     {
    1667           0 :       y_lead = gel(y,dy+2);
    1668           0 :       if (!gequal0(y_lead)) break;
    1669             :     }
    1670             :   }
    1671    12721727 :   if (!dy) /* y is constant */
    1672             :   {
    1673        3079 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1674        3079 :     z = RgX_Rg_div(x, y_lead);
    1675        3079 :     if (pr == ONLY_DIVIDES) return z;
    1676        1196 :     if (pr) *pr = pol_0(varn(x));
    1677        1196 :     return z;
    1678             :   }
    1679    12718648 :   dx = degpol(x);
    1680    12718648 :   if (dx < dy)
    1681             :   {
    1682      682924 :     if (pr == ONLY_REM) return RgX_copy(x);
    1683      371635 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1684      371614 :     z = pol_0(varn(x));
    1685      371614 :     if (pr) *pr = RgX_copy(x);
    1686      371614 :     return z;
    1687             :   }
    1688             : 
    1689             :   /* x,y in R[X], y non constant */
    1690    12035724 :   av = avma;
    1691    12035724 :   p = NULL;
    1692    12035724 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1693             :   {
    1694      225841 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1695      225841 :     if (!z) return gc_NULL(av);
    1696      225841 :     z = FpX_to_mod(z, p);
    1697      225841 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1698      121681 :       return gerepileupto(av, z);
    1699      104160 :     *pr = FpX_to_mod(*pr, p);
    1700      104160 :     gerepileall(av, 2, pr, &z);
    1701      104160 :     return z;
    1702             :   }
    1703    11809883 :   switch(typ(y_lead))
    1704             :   {
    1705             :     case t_REAL:
    1706           0 :       y_lead = ginv(y_lead);
    1707           0 :       f = gmul; mod = NULL;
    1708           0 :       break;
    1709             :     case t_INTMOD:
    1710        1873 :     case t_POLMOD: y_lead = ginv(y_lead);
    1711        1873 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1712        1873 :       break;
    1713    11808010 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1714    11808010 :       f = gdiv; mod = NULL;
    1715             :   }
    1716             : 
    1717    11809883 :   if (y_lead == NULL)
    1718    10025992 :     p2 = gel(x,dx+2);
    1719             :   else {
    1720             :     for(;;) {
    1721     1783891 :       p2 = f(gel(x,dx+2),y_lead);
    1722     1783891 :       p2 = simplify_shallow(p2);
    1723     1783891 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1724             :     }
    1725     1783891 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1726             :     {
    1727           0 :       if (pr == ONLY_DIVIDES) {
    1728           0 :         set_avma(av);
    1729           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1730             :       }
    1731           0 :       if (pr == ONLY_REM)
    1732             :       {
    1733           0 :         if (dx < 0)
    1734           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1735             :         else
    1736             :         {
    1737             :           GEN t;
    1738           0 :           set_avma(av);
    1739           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1740           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1741           0 :           return t;
    1742             :         }
    1743             :       }
    1744           0 :       if (pr) /* cf ONLY_REM above */
    1745             :       {
    1746           0 :         if (dx < 0)
    1747             :         {
    1748           0 :           p2 = gclone(p2);
    1749           0 :           set_avma(av);
    1750           0 :           z = pol_0(varn(x));
    1751           0 :           x = scalarpol(p2, varn(x));
    1752           0 :           gunclone(p2);
    1753             :         }
    1754             :         else
    1755             :         {
    1756             :           GEN t;
    1757           0 :           set_avma(av);
    1758           0 :           z = pol_0(varn(x));
    1759           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1760           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1761           0 :           x = t;
    1762             :         }
    1763           0 :         *pr = x;
    1764             :       }
    1765             :       else
    1766             :       {
    1767           0 :         set_avma(av);
    1768           0 :         z = pol_0(varn(x));
    1769             :       }
    1770           0 :       return z;
    1771             :     }
    1772             :   }
    1773             :   /* dx >= dy */
    1774    11809883 :   avy = avma;
    1775    11809883 :   dz = dx-dy;
    1776    11809883 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1777    11809883 :   x += 2;
    1778    11809883 :   z += 2;
    1779    11809883 :   y += 2;
    1780    11809883 :   gel(z,dz) = gcopy(p2);
    1781             : 
    1782    30716896 :   for (i=dx-1; i>=dy; i--)
    1783             :   {
    1784    18907013 :     av1=avma; p1=gel(x,i);
    1785    18907013 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1786    18907013 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1787             : 
    1788    18907013 :     if (isrationalzero(p1)) { set_avma(av1); p1 = gen_0; }
    1789             :     else
    1790     9357178 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1791    18907013 :     gel(z,i-dy) = p1;
    1792             :   }
    1793    11809883 :   if (!pr) return gerepileupto(av,z-2);
    1794             : 
    1795     5122531 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1796     5471829 :   for (sx=0; ; i--)
    1797             :   {
    1798     5821127 :     p1 = gel(x,i);
    1799             :     /* we always enter this loop at least once */
    1800     5471829 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1801     5471829 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1802     5471829 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1803     3420861 :     if (!isexactzero(p1)) break;
    1804     3405271 :     if (!i) break;
    1805      349298 :     set_avma(av1);
    1806             :   }
    1807     5122531 :   if (pr == ONLY_DIVIDES)
    1808             :   {
    1809        1659 :     if (sx) return gc_NULL(av);
    1810        1652 :     set_avma((pari_sp)rem); return gerepileupto(av,z-2);
    1811             :   }
    1812     5120872 :   lr=i+3; rem -= lr;
    1813     5120872 :   if (avma==av1) { set_avma((pari_sp)rem); p1 = gcopy(p1); }
    1814     5077302 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1815     5120872 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1816     5120872 :   rem[1] = z[-1];
    1817     5120872 :   rem += 2;
    1818     5120872 :   gel(rem,i) = p1;
    1819     6583698 :   for (i--; i>=0; i--)
    1820             :   {
    1821     1462826 :     av1=avma; p1 = gel(x,i);
    1822     1462826 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1823     1462826 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1824     1462826 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1825             :   }
    1826     5120872 :   rem -= 2;
    1827     5120872 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1828     5120872 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1829     4142146 :   z -= 2;
    1830             :   {
    1831     4142146 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1832     4142146 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1833             :   }
    1834             : }
    1835             : 
    1836             : GEN
    1837    11431712 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1838             : {
    1839    11431712 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1840    11431712 :   return RgX_divrem_i(x, y, pr);
    1841             : }
    1842             : 
    1843             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1844             : GEN
    1845      110988 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1846             : {
    1847             :   long vx, dx, dy, dz, i, j, sx, lr;
    1848             :   pari_sp av0, av, tetpil;
    1849             :   GEN z,p1,rem,lead;
    1850             : 
    1851      110988 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1852      110988 :   vx = varn(x);
    1853      110988 :   dx = degpol(x);
    1854      110988 :   dy = degpol(y);
    1855      110988 :   if (dx < dy)
    1856             :   {
    1857       34090 :     if (pr)
    1858             :     {
    1859       34090 :       av0 = avma; x = RgXQX_red(x, T);
    1860       34090 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: gen_0; }
    1861       34090 :       if (pr == ONLY_REM) return x;
    1862           0 :       *pr = x;
    1863             :     }
    1864           0 :     return pol_0(vx);
    1865             :   }
    1866       76898 :   lead = leading_coeff(y);
    1867       76898 :   if (!dy) /* y is constant */
    1868             :   {
    1869         546 :     if (pr && pr != ONLY_DIVIDES)
    1870             :     {
    1871           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1872           0 :       *pr = pol_0(vx);
    1873             :     }
    1874         546 :     if (gequal1(lead)) return RgX_copy(x);
    1875           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1876           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1877             :   }
    1878       76352 :   av0 = avma; dz = dx-dy;
    1879       76352 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1880       76352 :   set_avma(av0);
    1881       76352 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1882       76352 :   x += 2; y += 2; z += 2;
    1883             : 
    1884       76352 :   p1 = gel(x,dx); av = avma;
    1885       76352 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1886      342755 :   for (i=dx-1; i>=dy; i--)
    1887             :   {
    1888      266403 :     av=avma; p1=gel(x,i);
    1889      266403 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1890      266403 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1891      266403 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1892             :   }
    1893       76352 :   if (!pr) { guncloneNULL(lead); return z-2; }
    1894             : 
    1895       75106 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1896      107141 :   for (sx=0; ; i--)
    1897             :   {
    1898      139176 :     p1 = gel(x,i);
    1899      107141 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1900      107141 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1901       39770 :     if (!i) break;
    1902       32035 :     set_avma(av);
    1903             :   }
    1904       75106 :   if (pr == ONLY_DIVIDES)
    1905             :   {
    1906        4732 :     guncloneNULL(lead);
    1907        4732 :     if (sx) return gc_NULL(av0);
    1908        4599 :     set_avma((pari_sp)rem); return z-2;
    1909             :   }
    1910       70374 :   lr=i+3; rem -= lr;
    1911       70374 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1912       70374 :   rem[1] = z[-1];
    1913       70374 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1914       70374 :   rem += 2; gel(rem,i) = p1;
    1915      156320 :   for (i--; i>=0; i--)
    1916             :   {
    1917       85946 :     av=avma; p1 = gel(x,i);
    1918      299359 :     for (j=0; j<=i && j<=dz; j++)
    1919      213413 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1920       85946 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1921             :   }
    1922       70374 :   rem -= 2;
    1923       70374 :   guncloneNULL(lead);
    1924       70374 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1925       70374 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1926         119 :   *pr = rem; return z-2;
    1927             : }
    1928             : 
    1929             : /*******************************************************************/
    1930             : /*                                                                 */
    1931             : /*                        PSEUDO-DIVISION                          */
    1932             : /*                                                                 */
    1933             : /*******************************************************************/
    1934             : INLINE GEN
    1935      533427 : rem(GEN c, GEN T)
    1936             : {
    1937      533427 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1938      533427 :   return c;
    1939             : }
    1940             : 
    1941             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1942             : int
    1943        8487 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1944             : {
    1945             :   long dx, dy, dz, i, p, T_ismonic;
    1946        8487 :   pari_sp av = avma, av2;
    1947             :   GEN y_lead;
    1948             : 
    1949        8487 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1950        8487 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1951        8487 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1952             :   /* if monic, no point in using pseudo-division */
    1953        8487 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1954        5344 :   T_ismonic = gequal1(leading_coeff(T));
    1955        5344 :   dx = degpol(x);
    1956        5344 :   if (dx < dy) return !signe(x);
    1957        5344 :   (void)new_chunk(2);
    1958        5344 :   x = RgX_recip_shallow(x)+2;
    1959        5344 :   y = RgX_recip_shallow(y)+2;
    1960             :   /* pay attention to sparse divisors */
    1961       11192 :   for (i = 1; i <= dy; i++)
    1962        5848 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1963        5344 :   dz = dx-dy; p = dz+1;
    1964        5344 :   av2 = avma;
    1965             :   for (;;)
    1966       33607 :   {
    1967       38951 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1968       38951 :     x0 = gneg(x0); p--;
    1969       38951 :     m = gcdii(cx, y0);
    1970       38951 :     if (!equali1(m))
    1971             :     {
    1972       37626 :       x0 = gdiv(x0, m);
    1973       37626 :       y0 = diviiexact(y0, m);
    1974       37626 :       if (equali1(y0)) y0 = NULL;
    1975             :     }
    1976       81687 :     for (i=1; i<=dy; i++)
    1977             :     {
    1978       42736 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1979       42736 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1980       42736 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1981       42736 :       gel(x,i) = c;
    1982             :     }
    1983      414997 :     for (   ; i<=dx; i++)
    1984             :     {
    1985      376046 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1986      376046 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1987      376046 :       gel(x,i) = c;
    1988             :     }
    1989       44670 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1990       38951 :     if (dx < dy) break;
    1991       33607 :     if (gc_needed(av2,1))
    1992             :     {
    1993           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1994           0 :       gerepilecoeffs(av2,x,dx+1);
    1995             :     }
    1996             :   }
    1997        5344 :   return gc_bool(av, dx < 0);
    1998             : }
    1999             : 
    2000             : /* T either NULL or a t_POL. */
    2001             : GEN
    2002       28587 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    2003             : {
    2004       28587 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    2005       28587 :   pari_sp av = avma, av2;
    2006             :   GEN y_lead;
    2007             : 
    2008       28587 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    2009       28587 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2010             :   /* if monic, no point in using pseudo-division */
    2011       28587 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    2012       25283 :   dx = degpol(x);
    2013       25283 :   if (dx < dy) return RgX_copy(x);
    2014       25276 :   (void)new_chunk(2);
    2015       25276 :   x = RgX_recip_shallow(x)+2;
    2016       25276 :   y = RgX_recip_shallow(y)+2;
    2017             :   /* pay attention to sparse divisors */
    2018       80218 :   for (i = 1; i <= dy; i++)
    2019       54942 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2020       25276 :   dz = dx-dy; p = dz+1;
    2021       25276 :   av2 = avma;
    2022             :   for (;;)
    2023             :   {
    2024      157926 :     gel(x,0) = gneg(gel(x,0)); p--;
    2025      298750 :     for (i=1; i<=dy; i++)
    2026             :     {
    2027      207149 :       GEN c = gmul(y_lead, gel(x,i));
    2028      207149 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    2029      207149 :       gel(x,i) = rem(c, T);
    2030             :     }
    2031      318184 :     for (   ; i<=dx; i++)
    2032             :     {
    2033      226583 :       GEN c = gmul(y_lead, gel(x,i));
    2034      226583 :       gel(x,i) = rem(c, T);
    2035             :     }
    2036      101316 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    2037       91601 :     if (dx < dy) break;
    2038       66325 :     if (gc_needed(av2,1))
    2039             :     {
    2040           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    2041           0 :       gerepilecoeffs(av2,x,dx+1);
    2042             :     }
    2043             :   }
    2044       25276 :   if (dx < 0) return pol_0(vx);
    2045       23757 :   lx = dx+3; x -= 2;
    2046       23757 :   x[0] = evaltyp(t_POL) | evallg(lx);
    2047       23757 :   x[1] = evalsigne(1) | evalvarn(vx);
    2048       23757 :   x = RgX_recip_shallow(x);
    2049       23757 :   if (p)
    2050             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    2051        2797 :     GEN t = y_lead;
    2052        2797 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    2053           0 :       t = RgXQ_powu(t, p, T);
    2054             :     else
    2055        2797 :       t = gpowgs(t, p);
    2056        7778 :     for (i=2; i<lx; i++)
    2057             :     {
    2058        4981 :       GEN c = gmul(gel(x,i), t);
    2059        4981 :       gel(x,i) = rem(c,T);
    2060             :     }
    2061        2797 :     if (!T) return gerepileupto(av, x);
    2062             :   }
    2063       20960 :   return gerepilecopy(av, x);
    2064             : }
    2065             : 
    2066             : GEN
    2067       28587 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2068             : 
    2069             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2070             : GEN
    2071        7754 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2072             : {
    2073        7754 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2074        7754 :   pari_sp av = avma, av2;
    2075             :   GEN z, r, ypow, y_lead;
    2076             : 
    2077        7754 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2078        7754 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2079        7754 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2080        7201 :   dx = degpol(x);
    2081        7201 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2082        7201 :   if (dx == dy)
    2083             :   {
    2084          70 :     GEN x_lead = gel(x,lg(x)-1);
    2085          70 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2086          70 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2087          70 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2088          70 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2089             :   }
    2090        7131 :   (void)new_chunk(2);
    2091        7131 :   x = RgX_recip_shallow(x)+2;
    2092        7131 :   y = RgX_recip_shallow(y)+2;
    2093             :   /* pay attention to sparse divisors */
    2094       36582 :   for (i = 1; i <= dy; i++)
    2095       29451 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2096        7131 :   dz = dx-dy; p = dz+1;
    2097        7131 :   lz = dz+3;
    2098        7131 :   z = cgetg(lz, t_POL);
    2099        7131 :   z[1] = evalsigne(1) | evalvarn(vx);
    2100        7131 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2101        7131 :   ypow = new_chunk(dz+1);
    2102        7131 :   gel(ypow,0) = gen_1;
    2103        7131 :   gel(ypow,1) = y_lead;
    2104       13104 :   for (i=2; i<=dz; i++)
    2105             :   {
    2106        5973 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2107        5973 :     gel(ypow,i) = rem(c,T);
    2108             :   }
    2109        7131 :   av2 = avma;
    2110        7131 :   for (iz=2;;)
    2111             :   {
    2112       22639 :     p--;
    2113       14885 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2114       64689 :     for (i=1; i<=dy; i++)
    2115             :     {
    2116       49804 :       GEN c = gmul(y_lead, gel(x,i));
    2117       49804 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2118       49804 :       gel(x,i) = rem(c, T);
    2119             :     }
    2120       38937 :     for (   ; i<=dx; i++)
    2121             :     {
    2122       24052 :       GEN c = gmul(y_lead, gel(x,i));
    2123       24052 :       gel(x,i) = rem(c,T);
    2124             :     }
    2125       14885 :     x++; dx--;
    2126       14885 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2127       14885 :     if (dx < dy) break;
    2128        7754 :     if (gc_needed(av2,1))
    2129             :     {
    2130           0 :       GEN X = x-2;
    2131           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2132           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2133           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2134             :     }
    2135             :   }
    2136        7131 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2137        7131 :   if (dx < 0)
    2138          98 :     x = pol_0(vx);
    2139             :   else
    2140             :   {
    2141        7033 :     lx = dx+3; x -= 2;
    2142        7033 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2143        7033 :     x[1] = evalsigne(1) | evalvarn(vx);
    2144        7033 :     x = RgX_recip_shallow(x);
    2145             :   }
    2146        7131 :   z = RgX_recip_shallow(z);
    2147        7131 :   r = x;
    2148        7131 :   if (p)
    2149             :   {
    2150        3220 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2151        3220 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2152             :   }
    2153        7131 :   gerepileall(av, 2, &z, &r);
    2154        7131 :   *ptr = r; return z;
    2155             : }
    2156             : GEN
    2157        7558 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2158        7558 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2159             : 
    2160             : GEN
    2161       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2162             : {
    2163       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2164             : }
    2165             : GEN
    2166    72384827 : RgX_Rg_mul(GEN y, GEN x) {
    2167             :   long i, ly;
    2168    72384827 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2169    72384827 :   if (ly == 2) return z;
    2170    72298475 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2171    72298468 :   return normalizepol_lg(z,ly);
    2172             : }
    2173             : GEN
    2174         511 : RgX_muls(GEN y, long x) {
    2175             :   long i, ly;
    2176         511 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2177         511 :   if (ly == 2) return z;
    2178         413 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2179         413 :   return normalizepol_lg(z,ly);
    2180             : }
    2181             : GEN
    2182        4417 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2183             : {
    2184        4417 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2185             : }
    2186             : GEN
    2187          77 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2188             : {
    2189          77 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2190             : }
    2191             : 
    2192             : GEN
    2193        1792 : RgXQX_sqr(GEN x, GEN T)
    2194             : {
    2195        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2196             : }
    2197             : 
    2198             : GEN
    2199         448 : RgXQX_powers(GEN P, long n, GEN T)
    2200             : {
    2201         448 :   GEN v = cgetg(n+2, t_VEC);
    2202             :   long i;
    2203         448 :   gel(v, 1) = pol_1(varn(T));
    2204         448 :   if (n==0) return v;
    2205         448 :   gel(v, 2) = gcopy(P);
    2206         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2207         448 :   return v;
    2208             : }
    2209             : 
    2210             : static GEN
    2211      141459 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2212             : static GEN
    2213           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2214             : static GEN
    2215       57066 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2216             : static GEN
    2217       47877 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2218             : static GEN
    2219      254102 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2220             : static GEN
    2221      251477 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2222             : static GEN
    2223         553 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2224             : static GEN
    2225      153212 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2226             : 
    2227             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2228             :               _mul, _sqr, _one, _zero };
    2229             : 
    2230             : GEN
    2231           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2232             : {
    2233           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2234             : }
    2235             : 
    2236             : GEN
    2237      112475 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2238             : {
    2239      112475 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2240      112475 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2241             : }
    2242             : 
    2243             : /* mod X^n */
    2244             : struct modXn {
    2245             :   long v; /* varn(X) */
    2246             :   long n;
    2247             : } ;
    2248             : static GEN
    2249       11613 : _sqrXn(void *data, GEN x) {
    2250       11613 :   struct modXn *S = (struct modXn*)data;
    2251       11613 :   return RgXn_sqr(x, S->n);
    2252             : }
    2253             : static GEN
    2254        4234 : _mulXn(void *data, GEN x, GEN y) {
    2255        4234 :   struct modXn *S = (struct modXn*)data;
    2256        4234 :   return RgXn_mul(x,y, S->n);
    2257             : }
    2258             : static GEN
    2259        1477 : _oneXn(void *data) {
    2260        1477 :   struct modXn *S = (struct modXn*)data;
    2261        1477 :   return pol_1(S->v);
    2262             : }
    2263             : static GEN
    2264           0 : _zeroXn(void *data) {
    2265           0 :   struct modXn *S = (struct modXn*)data;
    2266           0 :   return pol_0(S->v);
    2267             : }
    2268             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2269             :                                           _oneXn, _zeroXn };
    2270             : 
    2271             : GEN
    2272         357 : RgXn_powers(GEN x, long m, long n)
    2273             : {
    2274         357 :   long d = degpol(x);
    2275         357 :   int use_sqr = (d<<1) >= n;
    2276             :   struct modXn S;
    2277         357 :   S.v = varn(x); S.n = n;
    2278         357 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2279             : }
    2280             : 
    2281             : GEN
    2282        2216 : RgXn_powu_i(GEN x, ulong m, long n)
    2283             : {
    2284             :   struct modXn S;
    2285             :   long v;
    2286        2216 :   if (n == 1) return x;
    2287        2188 :   v = RgX_valrem(x, &x);
    2288        2188 :   if (v) n -= m * v;
    2289        2188 :   S.v = varn(x); S.n = n;
    2290        2188 :   x = gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2291        2188 :   if (v) x = RgX_shift_shallow(x, m * v);
    2292        2188 :   return x;
    2293             : }
    2294             : GEN
    2295           0 : RgXn_powu(GEN x, ulong m, long n)
    2296             : {
    2297             :   pari_sp av;
    2298           0 :   if (n == 1) return gcopy(x);
    2299           0 :   av = avma; return gerepilecopy(av, RgXn_powu_i(x, m, n));
    2300             : }
    2301             : 
    2302             : GEN
    2303         714 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2304             : {
    2305             :   struct modXn S;
    2306         714 :   S.v = varn(gel(x,2)); S.n = n;
    2307         714 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2308             : }
    2309             : 
    2310             : GEN
    2311           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2312             : {
    2313           0 :   int use_sqr = 2*degpol(x) >= n;
    2314             :   struct modXn S;
    2315           0 :   S.v = varn(x); S.n = n;
    2316           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2317             : }
    2318             : 
    2319             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2320             : GEN
    2321        6734 : RgXn_eval(GEN Q, GEN x, long n)
    2322             : {
    2323        6734 :   long d = degpol(x);
    2324             :   int use_sqr;
    2325             :   struct modXn S;
    2326        6734 :   if (d == 1 && isrationalzero(gel(x,2)))
    2327             :   {
    2328        6727 :     GEN y = RgX_unscale(Q, gel(x,3));
    2329        6727 :     setvarn(y, varn(x)); return y;
    2330             :   }
    2331           7 :   S.v = varn(x);
    2332           7 :   S.n = n;
    2333           7 :   use_sqr = (d<<1) >= n;
    2334           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2335             : }
    2336             : 
    2337             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2338             : static GEN
    2339       47994 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2340             : {
    2341       47994 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2342       47994 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2343             : }
    2344             : 
    2345             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2346             : static GEN
    2347           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2348             : {
    2349           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2350           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2351             : }
    2352             : 
    2353             : GEN
    2354      126625 : RgXn_inv_i(GEN f, long e)
    2355             : {
    2356             :   pari_sp av;
    2357             :   ulong mask;
    2358             :   GEN W, a;
    2359      126625 :   long v = varn(f), n = 1;
    2360             : 
    2361      126625 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2362      126625 :   a = ginv(gel(f,2));
    2363      126628 :   if (e == 1) return scalarpol(a, v);
    2364      126600 :   else if (e == 2)
    2365             :   {
    2366             :     GEN b;
    2367      108648 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2368       88592 :     av = avma; b = gneg(b);
    2369       88591 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2370       88590 :     W = deg1pol_shallow(b, a, v);
    2371       88590 :     return gcopy(W);
    2372             :   }
    2373       17952 :   W = scalarpol_shallow(a,v);
    2374       17952 :   mask = quadratic_prec_mask(e);
    2375       17952 :   av = avma;
    2376       82659 :   for (;mask>1;)
    2377             :   {
    2378             :     GEN u, fr;
    2379       46755 :     long n2 = n;
    2380       46755 :     n<<=1; if (mask & 1) n--;
    2381       46755 :     mask >>= 1;
    2382       46755 :     fr = RgXn_red_shallow(f, n);
    2383       46755 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2384       46755 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2385       46755 :     if (gc_needed(av,2))
    2386             :     {
    2387           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2388           0 :       W = gerepileupto(av, W);
    2389             :     }
    2390             :   }
    2391       17952 :   return W;
    2392             : }
    2393             : 
    2394             : static GEN
    2395           0 : RgXn_inv_FpX(GEN x, long e, GEN p)
    2396             : {
    2397           0 :   pari_sp av = avma;
    2398             :   GEN r;
    2399           0 :   if (lgefint(p) == 3)
    2400             :   {
    2401           0 :     ulong pp = uel(p, 2);
    2402           0 :     if (pp == 2)
    2403           0 :       r = F2x_to_ZX(F2xn_inv(RgX_to_F2x(x), e));
    2404             :     else
    2405           0 :       r = Flx_to_ZX_inplace(Flxn_inv(RgX_to_Flx(x, pp), e, pp));
    2406             :   }
    2407             :   else
    2408           0 :     r = FpXn_inv(RgX_to_FpX(x, p), e, p);
    2409           0 :   return gerepileupto(av, FpX_to_mod(r, p));
    2410             : }
    2411             : 
    2412             : static GEN
    2413           0 : RgXn_inv_FpXQX(GEN x, long n, GEN pol, GEN p)
    2414             : {
    2415           0 :   pari_sp av = avma;
    2416           0 :   GEN r, T = RgX_to_FpX(pol, p);
    2417           0 :   if (signe(T) == 0) pari_err_OP("/", gen_1, x);
    2418           0 :   r = FpXQXn_inv(RgX_to_FpXQX(x, T, p), n, T, p);
    2419           0 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2420             : }
    2421             : 
    2422             : #define code(t1,t2) ((t1 << 6) | t2)
    2423             : 
    2424             : static GEN
    2425       83009 : RgXn_inv_fast(GEN x, long e)
    2426             : {
    2427             :   GEN p, pol;
    2428             :   long pa;
    2429       83009 :   long t = RgX_type(x,&p,&pol,&pa);
    2430       83010 :   switch(t)
    2431             :   {
    2432           0 :     case t_INTMOD: return RgXn_inv_FpX(x, e, p);
    2433             :     case code(t_POLMOD, t_INTMOD):
    2434           0 :                    return RgXn_inv_FpXQX(x, e, pol, p);
    2435       83010 :     default:       return NULL;
    2436             :   }
    2437             : }
    2438             : #undef code
    2439             : 
    2440             : GEN
    2441       83009 : RgXn_inv(GEN f, long e)
    2442             : {
    2443       83009 :   GEN h = RgXn_inv_fast(f, e);
    2444       83010 :   if (h) return h;
    2445       83010 :   return RgXn_inv_i(f, e);
    2446             : }
    2447             : 
    2448             : /* Compute intformal(x^n*S)/x^(n+1) */
    2449             : static GEN
    2450       14210 : RgX_integXn(GEN x, long n)
    2451             : {
    2452       14210 :   long i, lx = lg(x);
    2453             :   GEN y;
    2454       14210 :   if (lx == 2) return RgX_copy(x);
    2455       13846 :   y = cgetg(lx, t_POL); y[1] = x[1];
    2456       27713 :   for (i=2; i<lx; i++)
    2457       13867 :     gel(y,i) = gdivgs(gel(x,i), n+i-1);
    2458       13846 :   return RgX_renormalize_lg(y, lx);;
    2459             : }
    2460             : 
    2461             : GEN
    2462       12971 : RgXn_expint(GEN h, long e)
    2463             : {
    2464       12971 :   pari_sp av = avma, av2;
    2465       12971 :   long v = varn(h), n;
    2466       12971 :   GEN f = pol_1(v), g;
    2467             :   ulong mask;
    2468             : 
    2469       12971 :   if (!signe(h)) return f;
    2470       12971 :   g = pol_1(v);
    2471       12971 :   n = 1; mask = quadratic_prec_mask(e);
    2472       12971 :   av2 = avma;
    2473       27181 :   for (;mask>1;)
    2474             :   {
    2475             :     GEN u, w;
    2476       14210 :     long n2 = n;
    2477       14210 :     n<<=1; if (mask & 1) n--;
    2478       14210 :     mask >>= 1;
    2479       14210 :     u = RgXn_mul(g, RgX_mulhigh_i(f, RgXn_red_shallow(h, n2-1), n2-1), n-n2);
    2480       14210 :     u = RgX_add(u, RgX_shift_shallow(RgXn_red_shallow(h, n-1), 1-n2));
    2481       14210 :     w = RgXn_mul(f, RgX_integXn(u, n2-1), n-n2);
    2482       14210 :     f = RgX_add(f, RgX_shift_shallow(w, n2));
    2483       14210 :     if (mask<=1) break;
    2484        1239 :     u = RgXn_mul(g, RgXn_mulhigh(f, g, n2, n), n-n2);
    2485        1239 :     g = RgX_sub(g, RgX_shift_shallow(u, n2));
    2486        1239 :     if (gc_needed(av2,2))
    2487             :     {
    2488           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"RgXn_expint, e = %ld", n);
    2489           0 :       gerepileall(av2, 2, &f, &g);
    2490             :     }
    2491             :   }
    2492       12971 :   return gerepileupto(av, f);
    2493             : }
    2494             : 
    2495             : GEN
    2496           0 : RgXn_exp(GEN h, long e)
    2497             : {
    2498           0 :   long d = degpol(h);
    2499           0 :   if (d < 0) return pol_1(varn(h));
    2500           0 :   if (!d || !gequal0(gel(h,2)))
    2501           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2502           0 :   return RgXn_expint(RgX_deriv(h), e);
    2503             : }
    2504             : 
    2505             : GEN
    2506          91 : RgXn_reverse(GEN f, long e)
    2507             : {
    2508          91 :   pari_sp av = avma, av2;
    2509             :   ulong mask;
    2510             :   GEN fi, a, df, W, an;
    2511          91 :   long v = varn(f), n=1;
    2512          91 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2513           0 :     pari_err_INV("serreverse",f);
    2514          91 :   fi = ginv(gel(f,3));
    2515          91 :   a = deg1pol_shallow(fi,gen_0,v);
    2516          91 :   if (e <= 2) return gerepilecopy(av, a);
    2517          91 :   W = scalarpol(fi,v);
    2518          91 :   df = RgX_deriv(f);
    2519          91 :   mask = quadratic_prec_mask(e);
    2520          91 :   av2 = avma;
    2521         539 :   for (;mask>1;)
    2522             :   {
    2523             :     GEN u, fa, fr;
    2524         357 :     long n2 = n, rt;
    2525         357 :     n<<=1; if (mask & 1) n--;
    2526         357 :     mask >>= 1;
    2527         357 :     fr = RgXn_red_shallow(f, n);
    2528         357 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2529         357 :     an = RgXn_powers(a, rt, n);
    2530         357 :     if (n>1)
    2531             :     {
    2532         357 :       long n4 = (n2+1)>>1;
    2533         357 :       GEN dfr = RgXn_red_shallow(df, n2);
    2534         357 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2535         357 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2536         357 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2537             :     }
    2538         357 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2539         357 :     fa = RgX_shift(fa, -n2);
    2540         357 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2541         357 :     if (gc_needed(av2,2))
    2542             :     {
    2543           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2544           0 :       gerepileall(av2, 2, &a, &W);
    2545             :     }
    2546             :   }
    2547          91 :   return gerepileupto(av, a);
    2548             : }
    2549             : 
    2550             : GEN
    2551           0 : RgXn_sqrt(GEN h, long e)
    2552             : {
    2553           0 :   pari_sp av = avma, av2;
    2554           0 :   long v = varn(h), n = 1;
    2555           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2556           0 :   ulong mask = quadratic_prec_mask(e);
    2557           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2558           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2559           0 :   av2 = avma;
    2560             :   while(1)
    2561           0 :   {
    2562           0 :     long n2 = n, m;
    2563             :     GEN g;
    2564           0 :     n<<=1; if (mask & 1) n--;
    2565           0 :     mask >>= 1;
    2566           0 :     m = n-n2;
    2567           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2568           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2569           0 :     if (mask==1) return gerepileupto(av, f);
    2570           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2571           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2572           0 :     if (gc_needed(av2,2))
    2573             :     {
    2574           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2575           0 :       gerepileall(av2, 2, &f, &df);
    2576             :     }
    2577             :   }
    2578             : }
    2579             : 
    2580             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2581             : GEN
    2582       18916 : RgXQ_powu(GEN x, ulong n, GEN T)
    2583             : {
    2584       18916 :   pari_sp av = avma;
    2585             : 
    2586       18916 :   if (!n) return pol_1(varn(x));
    2587       17095 :   if (n == 1) return RgX_copy(x);
    2588       15169 :   x = gen_powu_i(x, n, (void*)T, &_sqr, &_mul);
    2589       15169 :   return gerepilecopy(av, x);
    2590             : }
    2591             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2592             : GEN
    2593       19108 : RgXQ_pow(GEN x, GEN n, GEN T)
    2594             : {
    2595             :   pari_sp av;
    2596       19108 :   long s = signe(n);
    2597             : 
    2598       19108 :   if (!s) return pol_1(varn(x));
    2599       19108 :   if (is_pm1(n) == 1)
    2600           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2601       19108 :   av = avma;
    2602       19108 :   if (s < 0) x = RgXQ_inv(x, T);
    2603       19108 :   x = gen_pow_i(x, n, (void*)T, &_sqr, &_mul);
    2604       19108 :   return gerepilecopy(av, x);
    2605             : }
    2606             : static GEN
    2607      159049 : _ZXQsqr(void *data, GEN x) { return ZXQ_sqr(x, (GEN)data); }
    2608             : static GEN
    2609       50460 : _ZXQmul(void *data, GEN x, GEN y) { return ZXQ_mul(x,y, (GEN)data); }
    2610             : /* x,T in Z[X], n in N, compute lift(x^n mod T)) */
    2611             : GEN
    2612      163666 : ZXQ_powu(GEN x, ulong n, GEN T)
    2613             : {
    2614      163666 :   pari_sp av = avma;
    2615             : 
    2616      163666 :   if (!n) return pol_1(varn(x));
    2617      163666 :   if (n == 1) return ZX_copy(x);
    2618      107411 :   x = gen_powu_i(x, n, (void*)T, &_ZXQsqr, &_ZXQmul);
    2619      107410 :   return gerepilecopy(av, x);
    2620             : }
    2621             : 
    2622             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2623             : GEN
    2624        5215 : RgXQ_powers(GEN x, long l, GEN T)
    2625             : {
    2626        5215 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2627        5215 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2628             : }
    2629             : 
    2630             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2631             : GEN
    2632        3395 : QXQ_powers(GEN a, long n, GEN T)
    2633             : {
    2634        3395 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2635             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2636        3395 :   if (den)
    2637             :   { /* restore denominators */
    2638        2079 :     GEN d = den;
    2639             :     long i;
    2640        2079 :     gel(v,2) = a;
    2641        6608 :     for (i=3; i<=n+1; i++) {
    2642        4529 :       d = mulii(d,den);
    2643        4529 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2644             :     }
    2645             :   }
    2646        3395 :   return v;
    2647             : }
    2648             : 
    2649             : static GEN
    2650        2009 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2651             : {
    2652        2009 :   long l, i, m = 0;
    2653             :   GEN dz, z;
    2654        2009 :   GEN V = cgetg_copy(v, &l);
    2655        6482 :   for (i = imin; i < l; i++)
    2656             :   {
    2657        4473 :     GEN c = gel(v, i);
    2658        4473 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2659             :   }
    2660        2009 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2661        2009 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2662        6482 :   for (i = imin; i < l; i++)
    2663             :   {
    2664        4473 :     GEN c = gel(v,i);
    2665        4473 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2666        4473 :     gel(V,i) = c;
    2667             :   }
    2668        2009 :   return V;
    2669             : }
    2670             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2671             : GEN
    2672        1946 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2673        1946 : { return do_QXQ_eval(v, 1, a, T); }
    2674             : GEN
    2675          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2676          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2677             : 
    2678             : GEN
    2679         560 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2680             : {
    2681         560 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2682             : }
    2683             : 
    2684             : GEN
    2685       35973 : RgXQ_norm(GEN x, GEN T)
    2686             : {
    2687             :   pari_sp av;
    2688       35973 :   long dx = degpol(x);
    2689             :   GEN L, y;
    2690             : 
    2691       35973 :   av = avma; y = resultant(T, x);
    2692       35973 :   L = leading_coeff(T);
    2693       35973 :   if (gequal1(L) || !signe(x)) return y;
    2694           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2695             : }
    2696             : 
    2697             : GEN
    2698      118212 : RgX_blocks(GEN P, long n, long m)
    2699             : {
    2700      118212 :   GEN z = cgetg(m+1,t_VEC);
    2701      118212 :   long i,j, k=2, l = lg(P);
    2702      547808 :   for(i=1; i<=m; i++)
    2703             :   {
    2704      429596 :     GEN zi = cgetg(n+2,t_POL);
    2705      429596 :     zi[1] = P[1];
    2706      429596 :     gel(z,i) = zi;
    2707     2770459 :     for(j=2; j<n+2; j++)
    2708     2340863 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2709      429596 :     zi = RgX_renormalize_lg(zi, n+2);
    2710             :   }
    2711      118212 :   return z;
    2712             : }
    2713             : 
    2714             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2715             : void
    2716     5796168 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2717             : {
    2718     5796168 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2719             :   GEN p0, p1;
    2720             : 
    2721     5796169 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2722             : 
    2723     5795882 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2724     5795882 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2725     5795883 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2726    24150886 :   for (i=0; i<n1; i++)
    2727             :   {
    2728    18355003 :     p0[2+i] = p[2+(i<<1)];
    2729    18355003 :     p1[2+i] = p[3+(i<<1)];
    2730             :   }
    2731     5795883 :   if (n1 != n0)
    2732     2838502 :     p0[2+i] = p[2+(i<<1)];
    2733     5795883 :   *pe = normalizepol(p0);
    2734     5795883 :   *po = normalizepol(p1);
    2735             : }
    2736             : 
    2737             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2738             : GEN
    2739       40922 : RgX_splitting(GEN p, long k)
    2740             : {
    2741       40922 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2742             :   GEN r;
    2743             : 
    2744       40922 :   m = n/k;
    2745       40922 :   r = cgetg(k+1,t_VEC);
    2746      225666 :   for(i=1; i<=k; i++)
    2747             :   {
    2748      184744 :     gel(r,i) = cgetg(m+3, t_POL);
    2749      184744 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2750             :   }
    2751      554981 :   for (j=1, i=0, l=2; i<=n; i++)
    2752             :   {
    2753      514059 :     gmael(r,j,l) = gel(p,2+i);
    2754      514059 :     if (j==k) { j=1; l++; } else j++;
    2755             :   }
    2756      225666 :   for(i=1; i<=k; i++)
    2757      184744 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2758       40922 :   return r;
    2759             : }
    2760             : 
    2761             : /*******************************************************************/
    2762             : /*                                                                 */
    2763             : /*                        Kronecker form                           */
    2764             : /*                                                                 */
    2765             : /*******************************************************************/
    2766             : 
    2767             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2768             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2769             :  * normalized coefficients */
    2770             : GEN
    2771      176485 : Kronecker_to_mod(GEN z, GEN T)
    2772             : {
    2773      176485 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2774      176485 :   GEN x, t = cgetg(N,t_POL);
    2775      176485 :   t[1] = T[1];
    2776      176485 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2777      176485 :   x[1] = z[1];
    2778      176485 :   T = RgX_copy(T);
    2779     1524128 :   for (i=2; i<lx+2; i++, z+= N-2)
    2780             :   {
    2781     1347643 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2782     1347643 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2783             :   }
    2784      176485 :   N = (l-2) % (N-2) + 2;
    2785      176485 :   for (j=2; j<N; j++) t[j] = z[j];
    2786      176485 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2787      176485 :   return normalizepol_lg(x, i+1);
    2788             : }
    2789             : 
    2790             : /*******************************************************************/
    2791             : /*                                                                 */
    2792             : /*                        Domain detection                         */
    2793             : /*                                                                 */
    2794             : /*******************************************************************/
    2795             : 
    2796             : static GEN
    2797      150725 : zero_FpX_mod(GEN p, long v)
    2798             : {
    2799      150725 :   GEN r = cgetg(3,t_POL);
    2800      150725 :   r[1] = evalvarn(v);
    2801      150725 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2802      150725 :   return r;
    2803             : }
    2804             : 
    2805             : static GEN
    2806      498994 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2807             : {
    2808      498994 :   pari_sp av = avma;
    2809             :   GEN r;
    2810      498994 :   if (lgefint(p) == 3)
    2811             :   {
    2812      468890 :     ulong pp = uel(p, 2);
    2813      468890 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2814             :                                   RgX_to_Flx(y, pp), pp));
    2815             :   }
    2816             :   else
    2817       30104 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2818      498994 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2819      385957 :   return gerepileupto(av, FpX_to_mod(r, p));
    2820             : }
    2821             : 
    2822             : static GEN
    2823         532 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2824             : {
    2825         532 :   GEN r = cgetg(3,t_POL);
    2826         532 :   r[1] = evalvarn(v);
    2827         532 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2828         532 :   return r;
    2829             : }
    2830             : 
    2831             : static GEN
    2832        2037 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2833             : {
    2834        2037 :   pari_sp av = avma;
    2835             :   long dT;
    2836             :   GEN kx, ky, r;
    2837        2037 :   GEN T = RgX_to_FpX(pol, p);
    2838        2037 :   if (signe(T)==0) pari_err_OP("*", x, y);
    2839        2030 :   dT = degpol(T);
    2840        2030 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2841        2030 :   ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2842        2030 :   r = FpX_mul(kx, ky, p);
    2843        2030 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2844        1505 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2845             : }
    2846             : 
    2847             : static GEN
    2848      485956 : RgX_liftred(GEN x, GEN T)
    2849      485956 : { return RgXQX_red(liftpol_shallow(x), T); }
    2850             : 
    2851             : static GEN
    2852      162479 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2853             : {
    2854      162479 :   pari_sp av = avma;
    2855      162479 :   long dT = degpol(T);
    2856      162479 :   GEN r = QX_mul(ZXX_to_Kronecker(RgX_liftred(x, T), dT),
    2857             :                  ZXX_to_Kronecker(RgX_liftred(y, T), dT));
    2858      162479 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2859             : }
    2860             : 
    2861             : static GEN
    2862        1260 : RgX_sqr_FpX(GEN x, GEN p)
    2863             : {
    2864        1260 :   pari_sp av = avma;
    2865             :   GEN r;
    2866        1260 :   if (lgefint(p) == 3)
    2867             :   {
    2868        1056 :     ulong pp = uel(p, 2);
    2869        1056 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2870             :   }
    2871             :   else
    2872         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2873        1260 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2874        1162 :   return gerepileupto(av, FpX_to_mod(r, p));
    2875             : }
    2876             : 
    2877             : static GEN
    2878         196 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2879             : {
    2880         196 :   pari_sp av = avma;
    2881             :   long dT;
    2882         196 :   GEN kx, r, T = RgX_to_FpX(pol, p);
    2883         196 :   if (signe(T)==0) pari_err_OP("*",x,x);
    2884         189 :   dT = degpol(T);
    2885         189 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2886         189 :   r = FpX_sqr(kx, p);
    2887         189 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2888         189 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2889             : }
    2890             : 
    2891             : static GEN
    2892       12312 : RgX_sqr_QXQX(GEN x, GEN T)
    2893             : {
    2894       12312 :   pari_sp av = avma;
    2895       12312 :   long dT = degpol(T);
    2896       12312 :   GEN r = QX_sqr(ZXX_to_Kronecker(RgX_liftred(x, T), dT));
    2897       12312 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2898             : }
    2899             : 
    2900             : static GEN
    2901       67466 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2902             : {
    2903       67466 :   pari_sp av = avma;
    2904             :   GEN r;
    2905       67466 :   if (lgefint(p) == 3)
    2906             :   {
    2907       50676 :     ulong pp = uel(p, 2);
    2908       50676 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2909             :                                   RgX_to_Flx(y, pp), pp));
    2910             :   }
    2911             :   else
    2912       16790 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2913       67466 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2914       29876 :   return gerepileupto(av, FpX_to_mod(r, p));
    2915             : }
    2916             : 
    2917             : static GEN
    2918       74343 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    2919             : {
    2920       74343 :   pari_sp av = avma;
    2921             :   GEN r;
    2922       74343 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    2923       74343 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    2924             : }
    2925             : static GEN
    2926          63 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2927             : {
    2928          63 :   pari_sp av = avma;
    2929             :   GEN r;
    2930          63 :   GEN T = RgX_to_FpX(pol, p);
    2931          63 :   if (signe(T) == 0) pari_err_OP("%", x, y);
    2932          56 :   if (lgefint(p) == 3)
    2933             :   {
    2934          48 :     ulong pp = uel(p, 2);
    2935          48 :     GEN Tp = ZX_to_Flx(T, pp);
    2936          48 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2937             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2938             :   }
    2939             :   else
    2940           8 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2941          56 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2942          49 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2943             : }
    2944             : 
    2945             : #define code(t1,t2) ((t1 << 6) | t2)
    2946             : static GEN
    2947    49076633 : RgX_mul_fast(GEN x, GEN y)
    2948             : {
    2949             :   GEN p, pol;
    2950             :   long pa;
    2951    49076633 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2952    49076633 :   switch(t)
    2953             :   {
    2954    36709010 :     case t_INT:    return ZX_mul(x,y);
    2955     1610292 :     case t_FRAC:   return QX_mul(x,y);
    2956      104070 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2957      498994 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2958             :     case code(t_POLMOD, t_INT):
    2959             :     case code(t_POLMOD, t_FRAC):
    2960      162479 :                    return RgX_mul_QXQX(x, y, pol);
    2961             :     case code(t_POLMOD, t_INTMOD):
    2962        2037 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2963     9989751 :     default:       return NULL;
    2964             :   }
    2965             : }
    2966             : static GEN
    2967      142518 : RgX_sqr_fast(GEN x)
    2968             : {
    2969             :   GEN p, pol;
    2970             :   long pa;
    2971      142518 :   long t = RgX_type(x,&p,&pol,&pa);
    2972      142518 :   switch(t)
    2973             :   {
    2974      107386 :     case t_INT:    return ZX_sqr(x);
    2975       11410 :     case t_FRAC:   return QX_sqr(x);
    2976        2296 :     case t_FFELT:  return FFX_sqr(x, pol);
    2977        1260 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2978             :     case code(t_POLMOD, t_INT):
    2979             :     case code(t_POLMOD, t_FRAC):
    2980       12312 :                    return RgX_sqr_QXQX(x, pol);
    2981             :     case code(t_POLMOD, t_INTMOD):
    2982         196 :                    return RgX_sqr_FpXQX(x, pol, p);
    2983        7658 :     default:       return NULL;
    2984             :   }
    2985             : }
    2986             : 
    2987             : static GEN
    2988     6811505 : RgX_rem_fast(GEN x, GEN y)
    2989             : {
    2990             :   GEN p, pol;
    2991             :   long pa;
    2992     6811505 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2993     6811505 :   switch(t)
    2994             :   {
    2995     3992571 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    2996     1402307 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    2997          84 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2998       67466 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2999             :     case code(t_POLMOD, t_INT):
    3000             :     case code(t_POLMOD, t_FRAC):
    3001       74343 :                    return RgX_rem_QXQX(x, y, pol);
    3002             :     case code(t_POLMOD, t_INTMOD):
    3003          63 :                    return RgX_rem_FpXQX(x, y, pol, p);
    3004     1274671 :     default:       return NULL;
    3005             :   }
    3006             : }
    3007             : 
    3008             : #undef code
    3009             : 
    3010             : GEN
    3011    46574567 : RgX_mul(GEN x, GEN y)
    3012             : {
    3013    46574567 :   GEN z = RgX_mul_fast(x,y);
    3014    46574559 :   if (!z) z = RgX_mul_i(x,y);
    3015    46574559 :   return z;
    3016             : }
    3017             : 
    3018             : GEN
    3019      130667 : RgX_sqr(GEN x)
    3020             : {
    3021      130667 :   GEN z = RgX_sqr_fast(x);
    3022      130660 :   if (!z) z = RgX_sqr_i(x);
    3023      130660 :   return z;
    3024             : }
    3025             : 
    3026             : GEN
    3027     6811505 : RgX_rem(GEN x, GEN y)
    3028             : {
    3029     6811505 :   GEN z = RgX_rem_fast(x, y);
    3030     6811498 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    3031     6811498 :   return z;
    3032             : }
    3033             : 
    3034             : GEN
    3035     2439862 : RgXn_mul(GEN f, GEN g, long n)
    3036             : {
    3037     2439862 :   pari_sp av = avma;
    3038     2439862 :   GEN h = RgX_mul_fast(f,g);
    3039     2439862 :   if (!h) return RgXn_mul2(f,g,n);
    3040      154818 :   if (degpol(h) < n) return h;
    3041       53136 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    3042             : }
    3043             : 
    3044             : GEN
    3045       11851 : RgXn_sqr(GEN f, long n)
    3046             : {
    3047       11851 :   pari_sp av = avma;
    3048       11851 :   GEN g = RgX_sqr_fast(f);
    3049       11851 :   if (!g) return RgXn_sqr2(f,n);
    3050       11613 :   if (degpol(g) < n) return g;
    3051       10521 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    3052             : }
    3053             : 
    3054             : /* (f*g) \/ x^n */
    3055             : GEN
    3056       62204 : RgX_mulhigh_i(GEN f, GEN g, long n)
    3057             : {
    3058       62204 :   GEN h = RgX_mul_fast(f,g);
    3059       62204 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    3060             : }
    3061             : 
    3062             : /* (f*g) \/ x^n */
    3063             : GEN
    3064           0 : RgX_sqrhigh_i(GEN f, long n)
    3065             : {
    3066           0 :   GEN h = RgX_sqr_fast(f);
    3067           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    3068             : }

Generated by: LCOV version 1.13