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 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.11.0 lcov report (development 22851-e834f1b2f) Lines: 1459 1627 89.7 %
Date: 2018-07-16 05:36:59 Functions: 172 187 92.0 %
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     3134796 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     3134796 :   long pold=1, rold=n*(d-1);
      32    18360940 :   for(p=2; p<=d; p++)
      33             :   {
      34    15226144 :     r = m*(p-1) + n*((d-1)/p);
      35    15226144 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     3134796 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41    12311775 : 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    12311775 :   pari_sp av = avma;
      45             :   long i;
      46    12311775 :   GEN z = cmul(E,P,a,ff->one(E));
      47    12311925 :   if (!z) z = gen_0;
      48    71859482 :   for (i=1; i<=n; i++)
      49             :   {
      50    59547880 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    59549455 :     if (t) {
      52    57764623 :       z = ff->add(E, z, t);
      53    57762416 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56    12311602 :   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     7689078 : 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     7689078 :   pari_sp av = avma;
      69     7689078 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     7689078 :   if (d < 0) return ff->zero(E);
      73     7186047 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     3003246 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     3003247 :   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     3003247 :   d -= l;
      81     3003247 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     8129141 :   while (d >= l-1)
      83             :   {
      84     2122645 :     d -= l-1;
      85     2122645 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86     2122553 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87     2122625 :     if (gc_needed(av,2))
      88          61 :       z = gerepileupto(av, z);
      89             :   }
      90     3003246 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91     3003237 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92     3003238 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96     1455641 : 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     1455641 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102     1455641 :   if (d < 0) return ff->zero(E);
     103     1455214 :   rtd = (long) sqrt((double)d);
     104     1455214 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105     1455212 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106     1455218 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      887379 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    21671187 : _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      758522 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      249034 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      905152 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       14602 : _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      369502 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      369502 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      131362 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      131362 :   return gen_bkeval_powers(Q, degpol(Q), x, NULL, &Rg_algebra, _gen_cmul);
     135             : }
     136             : 
     137             : GEN
     138           0 : RgX_Rg_eval_bk(GEN Q, GEN x)
     139             : {
     140           0 :   return gen_bkeval(Q, degpol(Q), x, 1, NULL, &Rg_algebra, _gen_cmul);
     141             : }
     142             : 
     143             : GEN
     144        2884 : RgXV_RgV_eval(GEN Q, GEN x)
     145             : {
     146        2884 :   long i, l = lg(Q), vQ = gvar(Q);
     147        2884 :   GEN v = cgetg(l, t_VEC);
     148      242928 :   for (i = 1; i < l; i++)
     149             :   {
     150      240044 :     GEN Qi = gel(Q, i);
     151      240044 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     152             :   }
     153        2884 :   return v;
     154             : }
     155             : 
     156             : const struct bb_algebra *
     157      114510 : get_Rg_algebra(void)
     158             : {
     159      114510 :   return &Rg_algebra;
     160             : }
     161             : 
     162             : static struct bb_ring Rg_ring = {  _gen_add, _gen_mul, _gen_sqr };
     163             : 
     164             : static GEN
     165       17668 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
     166             : {
     167             :   (void) E;
     168       17668 :   return RgX_divrem(x, y, r);
     169             : }
     170             : 
     171             : GEN
     172        4221 : RgX_digits(GEN x, GEN T)
     173             : {
     174        4221 :   pari_sp av = avma;
     175        4221 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     176        4221 :   GEN z = gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
     177        4221 :   return gerepileupto(av, z);
     178             : }
     179             : 
     180             : /*******************************************************************/
     181             : /*                                                                 */
     182             : /*                         RgX                                     */
     183             : /*                                                                 */
     184             : /*******************************************************************/
     185             : 
     186             : long
     187     4373990 : RgX_equal(GEN x, GEN y)
     188             : {
     189     4373990 :   long i = lg(x);
     190             : 
     191     4373990 :   if (i != lg(y)) return 0;
     192    22357961 :   for (i--; i > 1; i--)
     193    18110609 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194     4247352 :   return 1;
     195             : }
     196             : 
     197             : /* Returns 1 in the base ring over which x is defined */
     198             : /* HACK: this also works for t_SER */
     199             : GEN
     200     2383535 : Rg_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203     2383535 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     204     2383535 :   if (RgX_type_is_composite(tx))
     205      349832 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206     2383535 :   switch(tx)
     207             :   {
     208         161 :     case t_INTMOD: retmkintmod(is_pm1(p)? gen_0: gen_1, icopy(p));
     209           7 :     case t_PADIC: return cvtop(gen_1, p, lx);
     210         637 :     case t_FFELT: return FF_1(T);
     211     2382730 :     default: return gen_1;
     212             :   }
     213             : }
     214             : /* Returns 0 in the base ring over which x is defined */
     215             : /* HACK: this also works for t_SER */
     216             : GEN
     217      201747 : Rg_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220      201747 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     221      201747 :   if (RgX_type_is_composite(tx))
     222       40047 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223      201747 :   switch(tx)
     224             :   {
     225         266 :     case t_INTMOD: retmkintmod(gen_0, icopy(p));
     226           0 :     case t_PADIC: return cvtop(gen_0, p, lx);
     227         210 :     case t_FFELT: return FF_zero(T);
     228      201271 :     default: return gen_0;
     229             :   }
     230             : }
     231             : 
     232             : GEN
     233        3269 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     234             : {
     235        3269 :   long i, n = degpol(P);
     236             :   GEN z, dz, dP;
     237        3269 :   if (n < 0) return gen_0;
     238        3269 :   P = Q_remove_denom(P, &dP);
     239        3269 :   z = gel(P,2); if (n == 0) return icopy(z);
     240        1743 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     241        1743 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     242        1743 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     243        1743 :   dz = mul_denom(dP, dV);
     244        1743 :   return dz? RgX_Rg_div(z, dz): z;
     245             : }
     246             : 
     247             : /* Return P(h * x), not memory clean */
     248             : GEN
     249       10486 : RgX_unscale(GEN P, GEN h)
     250             : {
     251       10486 :   long i, l = lg(P);
     252       10486 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253       10486 :   Q[1] = P[1];
     254       10486 :   if (l == 2) return Q;
     255       10458 :   gel(Q,2) = gcopy(gel(P,2));
     256       35448 :   for (i=3; i<l; i++)
     257             :   {
     258       24990 :     hi = gmul(hi,h);
     259       24990 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261       10458 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      324142 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      324142 :   long i, l = lg(P);
     268      324142 :   GEN Q = cgetg(l, t_POL);
     269      324142 :   Q[1] = P[1];
     270      324142 :   if (l == 2) return Q;
     271      324142 :   gel(Q,2) = gel(P,2);
     272      324142 :   if (l == 3) return Q;
     273      324142 :   if (h == -1)
     274      309120 :     for (i = 3; i < l; i++)
     275             :     {
     276      301118 :       gel(Q,i) = negi(gel(P,i));
     277      301118 :       if (++i == l) break;
     278      297752 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      312774 :     gel(Q,3) = mulis(gel(P,3), h);
     284      312774 :     hi = sqrs(h);
     285      676004 :     for (i = 4; i < l; i++)
     286             :     {
     287      363230 :       gel(Q,i) = mulii(gel(P,i), hi);
     288      363230 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      324142 :   return Q;
     292             : }
     293             : /* P a ZX, h a t_INT. Return P(h * x), not memory clean; optimize for h = -1 */
     294             : GEN
     295        7343 : ZX_unscale(GEN P, GEN h)
     296             : {
     297             :   long i, l;
     298             :   GEN Q, hi;
     299        7343 :   i = itos_or_0(h); if (i) return ZX_z_unscale(P, i);
     300          14 :   l = lg(P); Q = cgetg(l, t_POL);
     301          14 :   Q[1] = P[1];
     302          14 :   if (l == 2) return Q;
     303          14 :   gel(Q,2) = gel(P,2);
     304          14 :   if (l == 3) return Q;
     305          14 :   hi = h;
     306          14 :   gel(Q,3) = mulii(gel(P,3), hi);
     307          91 :   for (i = 4; i < l; i++)
     308             :   {
     309          77 :     hi = mulii(hi,h);
     310          77 :     gel(Q,i) = mulii(gel(P,i), hi);
     311             :   }
     312          14 :   return Q;
     313             : }
     314             : /* P a ZX. Return P(x << n), not memory clean */
     315             : GEN
     316       19671 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       19671 :   long i, ni = n, l = lg(P);
     319       19671 :   GEN Q = cgetg(l, t_POL);
     320       19671 :   Q[1] = P[1];
     321       19671 :   if (l == 2) return Q;
     322       19671 :   gel(Q,2) = gel(P,2);
     323       19671 :   if (l == 3) return Q;
     324       19671 :   gel(Q,3) = shifti(gel(P,3), ni);
     325       78715 :   for (i=4; i<l; i++)
     326             :   {
     327       59044 :     ni += n;
     328       59044 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       19671 :   return Q;
     331             : }
     332             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     333             : GEN
     334        1190 : ZX_unscale_div(GEN P, GEN h)
     335             : {
     336        1190 :   long i, l = lg(P);
     337        1190 :   GEN hi, Q = cgetg(l, t_POL);
     338        1190 :   Q[1] = P[1];
     339        1190 :   if (l == 2) return Q;
     340        1190 :   gel(Q,2) = diviiexact(gel(P,2), h);
     341        1190 :   if (l == 3) return Q;
     342        1190 :   gel(Q,3) = gel(P,3);
     343        1190 :   if (l == 4) return Q;
     344        1190 :   hi = h;
     345        1190 :   gel(Q,4) = mulii(gel(P,4), hi);
     346        5257 :   for (i=5; i<l; i++)
     347             :   {
     348        4067 :     hi = mulii(hi,h);
     349        4067 :     gel(Q,i) = mulii(gel(P,i), hi);
     350             :   }
     351        1190 :   return Q;
     352             : }
     353             : 
     354             : GEN
     355         378 : RgXV_unscale(GEN v, GEN h)
     356             : {
     357             :   long i, l;
     358             :   GEN w;
     359         378 :   if (!h || isint1(h)) return v;
     360         308 :   w = cgetg_copy(v, &l);
     361         308 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     362         308 :   return w;
     363             : }
     364             : 
     365             : /* Return h^degpol(P) P(x / h), not memory clean */
     366             : GEN
     367        1953 : RgX_rescale(GEN P, GEN h)
     368             : {
     369        1953 :   long i, l = lg(P);
     370        1953 :   GEN Q = cgetg(l,t_POL), hi = h;
     371        1953 :   Q[l-1] = P[l-1];
     372        8932 :   for (i=l-2; i>=2; i--)
     373             :   {
     374        8932 :     gel(Q,i) = gmul(gel(P,i), hi);
     375        8932 :     if (i == 2) break;
     376        6979 :     hi = gmul(hi,h);
     377             :   }
     378        1953 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      126288 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      126288 :   long i,id, dy, dx = degpol(x0);
     387      126288 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       70645 :   dy = dx/d;
     389       70645 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       70645 :   z = y + 2;
     391       70645 :   x = x0+ 2;
     392       70645 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       70645 :   return y;
     394             : }
     395             : 
     396             : /* return x0(X^d) */
     397             : GEN
     398      453395 : RgX_inflate(GEN x0, long d)
     399             : {
     400      453395 :   long i, id, dy, dx = degpol(x0);
     401      453395 :   GEN x = x0 + 2, z, y;
     402      453395 :   if (dx <= 0) return leafcopy(x0);
     403      446213 :   dy = dx*d;
     404      446213 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     405      446213 :   z = y + 2;
     406      446213 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     407      446213 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     408      446213 :   return y;
     409             : }
     410             : 
     411             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     412             : GEN
     413     1363432 : RgX_translate(GEN P, GEN c)
     414             : {
     415     1363432 :   pari_sp av = avma;
     416             :   GEN Q, *R;
     417             :   long i, k, n;
     418             : 
     419     1363432 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     420     1361777 :   Q = leafcopy(P);
     421     1361777 :   R = (GEN*)(Q+2); n = degpol(P);
     422     1361777 :   if (equali1(c))
     423             :   {
     424        8862 :     for (i=1; i<=n; i++)
     425             :     {
     426        6713 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], R[k+1]);
     427        6713 :       if (gc_needed(av,2))
     428             :       {
     429           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(1), i = %ld/%ld", i,n);
     430           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     431             :       }
     432             :     }
     433             :   }
     434     1359628 :   else if (equalim1(c))
     435             :   {
     436      142352 :     for (i=1; i<=n; i++)
     437             :     {
     438      120827 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     439      120827 :       if (gc_needed(av,2))
     440             :       {
     441           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(-1), i = %ld/%ld", i,n);
     442           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     443             :       }
     444             :     }
     445             :   }
     446             :   else
     447             :   {
     448     4541164 :     for (i=1; i<=n; i++)
     449             :     {
     450     3203061 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     451     3203061 :       if (gc_needed(av,2))
     452             :       {
     453           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL, i = %ld/%ld", i,n);
     454           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     455             :       }
     456             :     }
     457             :   }
     458     1361777 :   return gerepilecopy(av, Q);
     459             : }
     460             : 
     461             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     462             : GEN
     463      382977 : ZX_translate(GEN P, GEN c)
     464             : {
     465      382977 :   pari_sp av = avma;
     466             :   GEN Q, *R;
     467             :   long i, k, n;
     468             : 
     469      382977 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     470      382942 :   Q = leafcopy(P);
     471      382942 :   R = (GEN*)(Q+2); n = degpol(P);
     472      382942 :   if (equali1(c))
     473             :   {
     474     2406393 :     for (i=1; i<=n; i++)
     475             :     {
     476     2126864 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     477     2126864 :       if (gc_needed(av,2))
     478             :       {
     479           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(1), i = %ld/%ld", i,n);
     480           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     481             :       }
     482             :     }
     483             :   }
     484      103413 :   else if (equalim1(c))
     485             :   {
     486          70 :     for (i=1; i<=n; i++)
     487             :     {
     488          49 :       for (k=n-i; k<n; k++) R[k] = subii(R[k], R[k+1]);
     489          49 :       if (gc_needed(av,2))
     490             :       {
     491           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(-1), i = %ld/%ld", i,n);
     492           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     493             :       }
     494             :     }
     495             :   }
     496             :   else
     497             :   {
     498      773144 :     for (i=1; i<=n; i++)
     499             :     {
     500      669752 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], mulii(c, R[k+1]));
     501      669752 :       if (gc_needed(av,2))
     502             :       {
     503           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate, i = %ld/%ld", i,n);
     504           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     505             :       }
     506             :     }
     507             :   }
     508      382942 :   return gerepilecopy(av, Q);
     509             : }
     510             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     511             : GEN
     512        6426 : RgXQX_translate(GEN P, GEN c, GEN T)
     513             : {
     514        6426 :   pari_sp av = avma;
     515             :   GEN Q, *R;
     516             :   long i, k, n;
     517             : 
     518        6426 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     519        6405 :   Q = leafcopy(P);
     520        6405 :   R = (GEN*)(Q+2); n = degpol(P);
     521       38311 :   for (i=1; i<=n; i++)
     522             :   {
     523      164997 :     for (k=n-i; k<n; k++)
     524             :     {
     525      133091 :       pari_sp av2 = avma;
     526      133091 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     527             :     }
     528       31906 :     if (gc_needed(av,2))
     529             :     {
     530           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     531           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     532             :     }
     533             :   }
     534        6405 :   return gerepilecopy(av, Q);
     535             : }
     536             : 
     537             : /********************************************************************/
     538             : /**                                                                **/
     539             : /**                          CONVERSIONS                           **/
     540             : /**                       (not memory clean)                       **/
     541             : /**                                                                **/
     542             : /********************************************************************/
     543             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     544             :  * but everything else is */
     545             : static GEN
     546       23839 : QXQ_to_mod_copy(GEN x, GEN T)
     547             : {
     548             :   long d;
     549       23839 :   switch(typ(x))
     550             :   {
     551        9618 :     case t_INT:  return icopy(x);
     552         805 :     case t_FRAC: return gcopy(x);
     553             :     case t_POL:
     554       13416 :       d = degpol(x);
     555       13416 :       if (d < 0) return gen_0;
     556       12877 :       if (d == 0) return gcopy(gel(x,2));
     557       12527 :       return mkpolmod(RgX_copy(x), T);
     558           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     559             :              return NULL;/* LCOV_EXCL_LINE */
     560             :   }
     561             : }
     562             : /* pure shallow version */
     563             : static GEN
     564      978862 : QXQ_to_mod(GEN x, GEN T)
     565             : {
     566             :   long d;
     567      978862 :   switch(typ(x))
     568             :   {
     569             :     case t_INT:
     570      433468 :     case t_FRAC: return x;
     571             :     case t_POL:
     572      545394 :       d = degpol(x);
     573      545394 :       if (d < 0) return gen_0;
     574      416790 :       if (d == 0) return gel(x,2);
     575      399557 :       return mkpolmod(x, T);
     576           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     577             :              return NULL;/* LCOV_EXCL_LINE */
     578             :   }
     579             : }
     580             : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod_copy to all coeffs.
     581             :  * Not memory clean because T not copied, but everything else is */
     582             : static GEN
     583        3164 : QXQX_to_mod(GEN z, GEN T)
     584             : {
     585        3164 :   long i,l = lg(z);
     586        3164 :   GEN x = cgetg(l,t_POL);
     587        3164 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     588        3164 :   x[1] = z[1]; return normalizepol_lg(x,l);
     589             : }
     590             : /* pure shallow version */
     591             : GEN
     592      146656 : QXQX_to_mod_shallow(GEN z, GEN T)
     593             : {
     594      146656 :   long i,l = lg(z);
     595      146656 :   GEN x = cgetg(l,t_POL);
     596      146656 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     597      146656 :   x[1] = z[1]; return normalizepol_lg(x,l);
     598             : }
     599             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     600             : GEN
     601        1232 : QXQXV_to_mod(GEN V, GEN T)
     602             : {
     603        1232 :   long i, l = lg(V);
     604        1232 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     605        1232 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     606        1232 :   return z;
     607             : }
     608             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     609             : GEN
     610        4908 : QXQV_to_mod(GEN V, GEN T)
     611             : {
     612        4908 :   long i, l = lg(V);
     613        4908 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     614        4908 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     615        4908 :   return z;
     616             : }
     617             : 
     618             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     619             : GEN
     620       19222 : QXQC_to_mod_shallow(GEN V, GEN T)
     621             : {
     622       19222 :   long i, l = lg(V);
     623       19222 :   GEN z = cgetg(l, t_COL);
     624       19222 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     625       19222 :   return z;
     626             : }
     627             : 
     628             : GEN
     629        6615 : QXQM_to_mod_shallow(GEN V, GEN T)
     630             : {
     631        6615 :   long i, l = lg(V);
     632        6615 :   GEN z = cgetg(l, t_MAT);
     633        6615 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     634        6615 :   return z;
     635             : }
     636             : 
     637             : GEN
     638      778310 : RgX_renormalize_lg(GEN x, long lx)
     639             : {
     640             :   long i;
     641     2128371 :   for (i = lx-1; i>1; i--)
     642     1990729 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     643      778310 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     644      778310 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     645             : }
     646             : 
     647             : GEN
     648      466603 : RgV_to_RgX(GEN x, long v)
     649             : {
     650      466603 :   long i, k = lg(x);
     651             :   GEN p;
     652             : 
     653      466603 :   while (--k && gequal0(gel(x,k)));
     654      466603 :   if (!k) return pol_0(v);
     655      460884 :   i = k+2; p = cgetg(i,t_POL);
     656      460883 :   p[1] = evalsigne(1) | evalvarn(v);
     657      460883 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     658      460883 :   return p;
     659             : }
     660             : GEN
     661      158603 : RgV_to_RgX_reverse(GEN x, long v)
     662             : {
     663      158603 :   long j, k, l = lg(x);
     664             :   GEN p;
     665             : 
     666      158603 :   for (k = 1; k < l; k++)
     667      158603 :     if (!gequal0(gel(x,k))) break;
     668      158603 :   if (k == l) return pol_0(v);
     669      158603 :   k -= 1;
     670      158603 :   l -= k;
     671      158603 :   x += k;
     672      158603 :   p = cgetg(l+1,t_POL);
     673      158603 :   p[1] = evalsigne(1) | evalvarn(v);
     674      158603 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     675      158603 :   return p;
     676             : }
     677             : 
     678             : /* return the (N-dimensional) vector of coeffs of p */
     679             : GEN
     680     6841120 : RgX_to_RgC(GEN x, long N)
     681             : {
     682             :   long i, l;
     683             :   GEN z;
     684     6841120 :   l = lg(x)-1; x++;
     685     6841120 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     686     6841120 :   z = cgetg(N+1,t_COL);
     687     6841120 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     688     6841120 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     689     6841120 :   return z;
     690             : }
     691             : GEN
     692      689708 : Rg_to_RgC(GEN x, long N)
     693             : {
     694      689708 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     695             : }
     696             : 
     697             : /* vector of polynomials (in v) whose coeffs are given by the columns of x */
     698             : GEN
     699       49007 : RgM_to_RgXV(GEN x, long v)
     700       49007 : { pari_APPLY_type(t_VEC, RgV_to_RgX(gel(x,i), v)) }
     701             : 
     702             : /* matrix whose entries are given by the coeffs of the polynomials in
     703             :  * vector v (considered as degree n-1 polynomials) */
     704             : GEN
     705      105756 : RgV_to_RgM(GEN x, long n)
     706      105756 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     707             : 
     708             : GEN
     709        3977 : RgXV_to_RgM(GEN x, long n)
     710        3977 : { pari_APPLY_type(t_MAT, RgX_to_RgC(gel(x,i), n)) }
     711             : 
     712             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     713             : GEN
     714       16359 : RgM_to_RgXX(GEN x, long v,long w)
     715             : {
     716       16359 :   long j, lx = lg(x);
     717       16359 :   GEN y = cgetg(lx+1, t_POL);
     718       16359 :   y[1] = evalsigne(1) | evalvarn(v);
     719       16359 :   y++;
     720       16359 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     721       16359 :   return normalizepol_lg(--y, lx+1);
     722             : }
     723             : 
     724             : /* matrix whose entries are given by the coeffs of the polynomial v in
     725             :  * two variables (considered as degree n-1 polynomials) */
     726             : GEN
     727         308 : RgXX_to_RgM(GEN v, long n)
     728             : {
     729         308 :   long j, N = lg(v)-1;
     730         308 :   GEN y = cgetg(N, t_MAT);
     731         308 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     732         308 :   return y;
     733             : }
     734             : 
     735             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     736             : GEN
     737       18036 : RgXY_swapspec(GEN x, long n, long w, long nx)
     738             : {
     739       18036 :   long j, ly = n+3;
     740       18036 :   GEN y = cgetg(ly, t_POL);
     741       18036 :   y[1] = evalsigne(1);
     742      231499 :   for (j=2; j<ly; j++)
     743             :   {
     744             :     long k;
     745      213463 :     GEN a = cgetg(nx+2,t_POL);
     746      213463 :     a[1] = evalsigne(1) | evalvarn(w);
     747     1132634 :     for (k=0; k<nx; k++)
     748             :     {
     749      919171 :       GEN xk = gel(x,k);
     750      919171 :       if (typ(xk)==t_POL)
     751      843914 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     752             :       else
     753       75257 :         gel(a,k+2) = j==2 ? xk: gen_0;
     754             :     }
     755      213463 :     gel(y,j) = normalizepol_lg(a, nx+2);
     756             :   }
     757       18036 :   return normalizepol_lg(y,ly);
     758             : }
     759             : 
     760             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     761             : GEN
     762         952 : RgXY_swap(GEN x, long n, long w)
     763             : {
     764         952 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     765         952 :   setvarn(z, varn(x)); return z;
     766             : }
     767             : 
     768             : long
     769         162 : RgXY_degreex(GEN b)
     770             : {
     771         162 :   long deg = -1, i;
     772         162 :   if (!signe(b)) return -1;
     773         976 :   for (i = 2; i < lg(b); ++i)
     774             :   {
     775         814 :     GEN bi = gel(b, i);
     776         814 :     if (typ(bi) == t_POL)
     777         792 :       deg = maxss(deg, degpol(bi));
     778             :   }
     779         162 :   return deg;
     780             : }
     781             : 
     782             : /* return (x % X^n). Shallow */
     783             : GEN
     784     2242214 : RgXn_red_shallow(GEN a, long n)
     785             : {
     786     2242214 :   long i, L = n+2, l = lg(a);
     787             :   GEN  b;
     788     2242214 :   if (L >= l) return a; /* deg(x) < n */
     789     1544622 :   b = cgetg(L, t_POL); b[1] = a[1];
     790     1544622 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     791     1544622 :   return normalizepol_lg(b,L);
     792             : }
     793             : 
     794             : GEN
     795         357 : RgXnV_red_shallow(GEN x, long n)
     796         357 : { pari_APPLY_type(t_VEC, RgXn_red_shallow(gel(x,i), n)) }
     797             : 
     798             : /* return (x * X^n). Shallow */
     799             : GEN
     800    59921608 : RgX_shift_shallow(GEN a, long n)
     801             : {
     802    59921608 :   long i, l = lg(a);
     803             :   GEN  b;
     804    59921608 :   if (l == 2 || !n) return a;
     805    43736058 :   l += n;
     806    43736058 :   if (n < 0)
     807             :   {
     808    38008838 :     if (l <= 2) return pol_0(varn(a));
     809    37985605 :     b = cgetg(l, t_POL); b[1] = a[1];
     810    37985608 :     a -= n;
     811    37985608 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     812             :   } else {
     813     5727220 :     b = cgetg(l, t_POL); b[1] = a[1];
     814     5727223 :     a -= n; n += 2;
     815     5727223 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     816     5727223 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     817             :   }
     818    43712831 :   return b;
     819             : }
     820             : /* return (x * X^n). */
     821             : GEN
     822     3472978 : RgX_shift(GEN a, long n)
     823             : {
     824     3472978 :   long i, l = lg(a);
     825             :   GEN  b;
     826     3472978 :   if (l == 2 || !n) return RgX_copy(a);
     827     3472712 :   l += n;
     828     3472712 :   if (n < 0)
     829             :   {
     830         812 :     if (l <= 2) return pol_0(varn(a));
     831         770 :     b = cgetg(l, t_POL); b[1] = a[1];
     832         770 :     a -= n;
     833         770 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     834             :   } else {
     835     3471900 :     b = cgetg(l, t_POL); b[1] = a[1];
     836     3471900 :     a -= n; n += 2;
     837     3471900 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     838     3471900 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     839             :   }
     840     3472670 :   return b;
     841             : }
     842             : 
     843             : GEN
     844      315273 : RgX_rotate_shallow(GEN P, long k, long p)
     845             : {
     846      315273 :   long i, l = lgpol(P);
     847             :   GEN r;
     848      315273 :   if (signe(P)==0)
     849         476 :     return pol_0(varn(P));
     850      314797 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     851     2090746 :   for(i=0; i<p; i++)
     852             :   {
     853     1775949 :     long s = 2+(i+k)%p;
     854     1775949 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     855             :   }
     856      314797 :   return RgX_renormalize(r);
     857             : }
     858             : 
     859             : GEN
     860     2974324 : RgX_mulXn(GEN x, long d)
     861             : {
     862             :   pari_sp av;
     863             :   GEN z;
     864             :   long v;
     865     2974324 :   if (d >= 0) return RgX_shift(x, d);
     866     1463359 :   d = -d;
     867     1463359 :   v = RgX_val(x);
     868     1463359 :   if (v >= d) return RgX_shift(x, -d);
     869     1463345 :   av = avma;
     870     1463345 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     871     1463345 :   return gerepileupto(av, z);
     872             : }
     873             : 
     874             : long
     875     2484313 : RgX_val(GEN x)
     876             : {
     877     2484313 :   long i, lx = lg(x);
     878     2484313 :   if (lx == 2) return LONG_MAX;
     879     2516520 :   for (i = 2; i < lx; i++)
     880     2516478 :     if (!isexactzero(gel(x,i))) break;
     881     2484271 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     882     2484229 :   return i - 2;
     883             : }
     884             : long
     885    43683875 : RgX_valrem(GEN x, GEN *Z)
     886             : {
     887    43683875 :   long v, i, lx = lg(x);
     888    43683875 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     889    83809420 :   for (i = 2; i < lx; i++)
     890    83809420 :     if (!isexactzero(gel(x,i))) break;
     891             :   /* possible with non-rational zeros */
     892    43683875 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     893    43683875 :   v = i - 2;
     894    43683875 :   *Z = RgX_shift_shallow(x, -v);
     895    43683875 :   return v;
     896             : }
     897             : long
     898       14514 : RgX_valrem_inexact(GEN x, GEN *Z)
     899             : {
     900             :   long v;
     901       14514 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     902       14990 :   for (v = 0;; v++)
     903       15473 :     if (!gequal0(gel(x,2+v))) break;
     904       14507 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     905       14507 :   return v;
     906             : }
     907             : 
     908             : GEN
     909       89992 : RgXQC_red(GEN x, GEN T)
     910       89992 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
     911             : 
     912             : GEN
     913         945 : RgXQV_red(GEN x, GEN T)
     914         945 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
     915             : 
     916             : GEN
     917       13090 : RgXQM_red(GEN x, GEN T)
     918       13090 : { pari_APPLY_same(RgXQC_red(gel(x,i), T)) }
     919             : 
     920             : GEN
     921           0 : RgXQM_mul(GEN P, GEN Q, GEN T)
     922             : {
     923           0 :   return RgXQM_red(RgM_mul(P, Q), T);
     924             : }
     925             : 
     926             : GEN
     927      379683 : RgXQX_red(GEN P, GEN T)
     928             : {
     929      379683 :   long i, l = lg(P);
     930      379683 :   GEN Q = cgetg(l, t_POL);
     931      379683 :   Q[1] = P[1];
     932      379683 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     933      379683 :   return normalizepol_lg(Q, l);
     934             : }
     935             : 
     936             : GEN
     937      239202 : RgX_deriv(GEN x)
     938             : {
     939      239202 :   long i,lx = lg(x)-1;
     940             :   GEN y;
     941             : 
     942      239202 :   if (lx<3) return pol_0(varn(x));
     943      211146 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     944      211146 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     945      211146 :   y[1] = x[1]; return normalizepol_lg(y,i);
     946             : }
     947             : 
     948             : GEN
     949      477391 : RgX_recipspec_shallow(GEN x, long l, long n)
     950             : {
     951             :   long i;
     952      477391 :   GEN z = cgetg(n+2,t_POL);
     953      477394 :   z[1] = 0; z += 2;
     954    14513176 :   for(i=0; i<l; i++)
     955    14035782 :     gel(z,n-i-1) = gel(x,i);
     956      604433 :   for(   ; i<n; i++)
     957      127039 :     gel(z, n-i-1) = gen_0;
     958      477394 :   return normalizepol_lg(z-2,n+2);
     959             : }
     960             : 
     961             : GEN
     962       40923 : RgXn_recip_shallow(GEN P, long n)
     963             : {
     964       40923 :   GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
     965       40923 :   setvarn(Q, varn(P));
     966       40923 :   return Q;
     967             : }
     968             : 
     969             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     970             : GEN
     971        2093 : RgX_recip(GEN x)
     972             : {
     973             :   long lx, i, j;
     974        2093 :   GEN y = cgetg_copy(x, &lx);
     975        2093 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     976        2093 :   return normalizepol_lg(y,lx);
     977             : }
     978             : /* shallow version */
     979             : GEN
     980      376353 : RgX_recip_shallow(GEN x)
     981             : {
     982             :   long lx, i, j;
     983      376353 :   GEN y = cgetg_copy(x, &lx);
     984      376503 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     985      376503 :   return y;
     986             : }
     987             : /*******************************************************************/
     988             : /*                                                                 */
     989             : /*                      ADDITION / SUBTRACTION                     */
     990             : /*                                                                 */
     991             : /*******************************************************************/
     992             : /* same variable */
     993             : GEN
     994    17869846 : RgX_add(GEN x, GEN y)
     995             : {
     996    17869846 :   long i, lx = lg(x), ly = lg(y);
     997             :   GEN z;
     998    17869846 :   if (ly <= lx) {
     999    15950575 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1000    15950575 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1001    15950575 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1002    15950575 :     z = normalizepol_lg(z, lx);
    1003             :   } else {
    1004     1919271 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1005     1919271 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1006     1919271 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1007     1919271 :     z = normalizepol_lg(z, ly);
    1008             :   }
    1009    17869846 :   return z;
    1010             : }
    1011             : GEN
    1012    12472164 : RgX_sub(GEN x, GEN y)
    1013             : {
    1014    12472164 :   long i, lx = lg(x), ly = lg(y);
    1015             :   GEN z;
    1016    12472164 :   if (ly <= lx) {
    1017     9516264 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1018     9516264 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1019     9516264 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1020     9516264 :     z = normalizepol_lg(z, lx);
    1021             :   } else {
    1022     2955900 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1023     2955900 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1024     2955900 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1025     2955900 :     z = normalizepol_lg(z, ly);
    1026             :   }
    1027    12472164 :   return z;
    1028             : }
    1029             : GEN
    1030     1091540 : RgX_neg(GEN x)
    1031             : {
    1032     1091540 :   long i, lx = lg(x);
    1033     1091540 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1034     1091540 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1035     1091540 :   return y;
    1036             : }
    1037             : 
    1038             : GEN
    1039    11900410 : RgX_Rg_add(GEN y, GEN x)
    1040             : {
    1041             :   GEN z;
    1042    11900410 :   long lz = lg(y), i;
    1043    11900410 :   if (lz == 2) return scalarpol(x,varn(y));
    1044    10035046 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1045    10035046 :   gel(z,2) = gadd(gel(y,2),x);
    1046    10035046 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1047             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1048             :    * an inexact 0 */
    1049    10035046 :   return normalizepol_lg(z,lz);
    1050             : }
    1051             : GEN
    1052        2548 : RgX_Rg_add_shallow(GEN y, GEN x)
    1053             : {
    1054             :   GEN z;
    1055        2548 :   long lz = lg(y), i;
    1056        2548 :   if (lz == 2) return scalarpol(x,varn(y));
    1057        2548 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1058        2548 :   gel(z,2) = gadd(gel(y,2),x);
    1059        2548 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1060        2548 :   return z = normalizepol_lg(z,lz);
    1061             : }
    1062             : GEN
    1063       33636 : RgX_Rg_sub(GEN y, GEN x)
    1064             : {
    1065             :   GEN z;
    1066       33636 :   long lz = lg(y), i;
    1067       33636 :   if (lz == 2)
    1068             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1069        3864 :     long v = varn(y);
    1070        3864 :     if (isrationalzero(x)) return pol_0(v);
    1071          14 :     z = cgetg(3,t_POL);
    1072          28 :     z[1] = gequal0(x)? evalvarn(v)
    1073          14 :                    : evalvarn(v) | evalsigne(1);
    1074          14 :     gel(z,2) = gneg(x); return z;
    1075             :   }
    1076       29772 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1077       29772 :   gel(z,2) = gsub(gel(y,2),x);
    1078       29772 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1079       29772 :   return z = normalizepol_lg(z,lz);
    1080             : }
    1081             : GEN
    1082      466873 : Rg_RgX_sub(GEN x, GEN y)
    1083             : {
    1084             :   GEN z;
    1085      466873 :   long lz = lg(y), i;
    1086      466873 :   if (lz == 2) return scalarpol(x,varn(y));
    1087      465858 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1088      465858 :   gel(z,2) = gsub(x, gel(y,2));
    1089      465858 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1090      465858 :   return z = normalizepol_lg(z,lz);
    1091             : }
    1092             : /*******************************************************************/
    1093             : /*                                                                 */
    1094             : /*                  KARATSUBA MULTIPLICATION                       */
    1095             : /*                                                                 */
    1096             : /*******************************************************************/
    1097             : #if 0
    1098             : /* to debug Karatsuba-like routines */
    1099             : GEN
    1100             : zx_debug_spec(GEN x, long nx)
    1101             : {
    1102             :   GEN z = cgetg(nx+2,t_POL);
    1103             :   long i;
    1104             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1105             :   z[1] = evalsigne(1); return z;
    1106             : }
    1107             : 
    1108             : GEN
    1109             : RgX_debug_spec(GEN x, long nx)
    1110             : {
    1111             :   GEN z = cgetg(nx+2,t_POL);
    1112             :   long i;
    1113             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1114             :   z[1] = evalsigne(1); return z;
    1115             : }
    1116             : #endif
    1117             : 
    1118             : /* generic multiplication */
    1119             : GEN
    1120     3189384 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1121             : {
    1122             :   GEN z, t;
    1123             :   long i;
    1124     3189384 :   if (nx == ny) {
    1125      743103 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1126      743103 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1127      743103 :     return normalizepol_lg(z, nx+2);
    1128             :   }
    1129     2446281 :   if (ny < nx) {
    1130     2295877 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1131     2295877 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1132     2295877 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1133     2295877 :     return normalizepol_lg(z, nx+2);
    1134             :   } else {
    1135      150404 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1136      150404 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1137      150404 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1138      150404 :     return normalizepol_lg(z, ny+2);
    1139             :   }
    1140             : }
    1141             : GEN
    1142      201476 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1143             : {
    1144             :   GEN z, t;
    1145             :   long i;
    1146      201476 :   if (nx == ny) {
    1147       10324 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1148       10324 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1149       10324 :     return normalizepol_lg(z, nx+2);
    1150             :   }
    1151      191152 :   if (ny < nx) {
    1152      189451 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1153      189451 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1154      189451 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1155      189451 :     return normalizepol_lg(z, nx+2);
    1156             :   } else {
    1157        1701 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1158        1701 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1159        1701 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1160        1701 :     return normalizepol_lg(z, ny+2);
    1161             :   }
    1162             : }
    1163             : 
    1164             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1165             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1166             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1167             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1168             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1169             : static GEN
    1170    20947378 : RgXspec_kill0(GEN x, long lx)
    1171             : {
    1172    20947378 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1173             :   long i;
    1174    83863355 :   for (i=0; i <lx; i++)
    1175             :   {
    1176    62915977 :     GEN c = gel(x,i);
    1177    62915977 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1178             :   }
    1179    20947378 :   return z;
    1180             : }
    1181             : 
    1182             : INLINE GEN
    1183    46269616 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1184             : {
    1185    46269616 :   pari_sp av = avma;
    1186    46269616 :   GEN s = NULL;
    1187             :   long i;
    1188             : 
    1189   205176416 :   for (i=a; i<b; i++)
    1190   158906800 :     if (gel(y,i) && gel(x,-i))
    1191             :     {
    1192    83292640 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1193    83292640 :       s = s? gadd(s, t): t;
    1194             :     }
    1195    46269616 :   return s? gerepileupto(av, s): gen_0;
    1196             : }
    1197             : 
    1198             : /* assume nx >= ny > 0, return x * y * t^v */
    1199             : static GEN
    1200     8252984 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1201             : {
    1202             :   long i, lz, nz;
    1203             :   GEN z;
    1204             : 
    1205     8252984 :   x = RgXspec_kill0(x,nx);
    1206     8252984 :   y = RgXspec_kill0(y,ny);
    1207     8252984 :   lz = nx + ny + 1; nz = lz-2;
    1208     8252984 :   lz += v;
    1209     8252984 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1210     8252984 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1211     8252984 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1212     8252984 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1213     8252984 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1214     8252984 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1215             : }
    1216             : 
    1217             : /* return (x * X^d) + y. Assume d > 0 */
    1218             : GEN
    1219     2318163 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1220             : {
    1221             :   GEN x, y, xd, yd, zd;
    1222             :   long a, lz, nx, ny;
    1223             : 
    1224     2318163 :   if (!signe(x0)) return y0;
    1225     2294214 :   ny = lgpol(y0);
    1226     2294214 :   nx = lgpol(x0);
    1227     2294214 :   zd = (GEN)avma;
    1228     2294214 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1229     2294214 :   if (a <= 0)
    1230             :   {
    1231      184323 :     lz = nx+d+2;
    1232      184323 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1233      184323 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1234      184323 :     x = zd + a;
    1235      184323 :     while (zd > x) gel(--zd,0) = gen_0;
    1236             :   }
    1237             :   else
    1238             :   {
    1239     2109891 :     xd = new_chunk(d); yd = y+d;
    1240     2109891 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1241     2109891 :     lz = (a>nx)? ny+2: lg(x)+d;
    1242     2109891 :     x += 2; while (xd > x) *--zd = *--xd;
    1243             :   }
    1244     2294214 :   while (yd > y) *--zd = *--yd;
    1245     2294214 :   *--zd = x0[1];
    1246     2294214 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1247             : }
    1248             : GEN
    1249      493436 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1250             : {
    1251             :   GEN x, y, xd, yd, zd;
    1252             :   long a, lz, nx, ny;
    1253             : 
    1254      493436 :   if (!signe(x0)) return RgX_copy(y0);
    1255      492859 :   nx = lgpol(x0);
    1256      492859 :   ny = lgpol(y0);
    1257      492859 :   zd = (GEN)avma;
    1258      492859 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1259      492859 :   if (a <= 0)
    1260             :   {
    1261      291383 :     lz = nx+d+2;
    1262      291383 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1263      291383 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1264      291383 :     x = zd + a;
    1265      291383 :     while (zd > x) gel(--zd,0) = gen_0;
    1266             :   }
    1267             :   else
    1268             :   {
    1269      201476 :     xd = new_chunk(d); yd = y+d;
    1270      201476 :     x = RgX_addspec(x,yd, nx,a);
    1271      201476 :     lz = (a>nx)? ny+2: lg(x)+d;
    1272      201476 :     x += 2; while (xd > x) *--zd = *--xd;
    1273             :   }
    1274      492859 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1275      492859 :   *--zd = x0[1];
    1276      492859 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1277             : }
    1278             : 
    1279             : /* return x * y mod t^n */
    1280             : static GEN
    1281     2217784 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1282             : {
    1283     2217784 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1284             :   GEN z;
    1285     2217784 :   if (lx < 0) return pol_0(varn(x));
    1286     2217784 :   if (ly < 0) return pol_0(varn(x));
    1287     2217784 :   z = cgetg(lz, t_POL) + 2;
    1288     2217784 :   x+=2; if (lx > n) lx = n;
    1289     2217784 :   y+=2; if (ly > n) ly = n;
    1290     2217784 :   z[-1] = x[-1];
    1291     2217784 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1292     2217784 :   x = RgXspec_kill0(x, lx);
    1293     2217784 :   y = RgXspec_kill0(y, ly);
    1294             :   /* x:y:z [i] = term of degree i */
    1295     2217784 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1296     2217784 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1297     2217784 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1298     2217784 :   return normalizepol_lg(z - 2, lz);
    1299             : }
    1300             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1301             : static GEN
    1302     2301567 : RgXn_mul2(GEN f, GEN g, long n)
    1303             : {
    1304     2301567 :   pari_sp av = avma;
    1305             :   GEN fe,fo, ge,go, l,h,m;
    1306             :   long n0, n1;
    1307     2301567 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1308     2242102 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1309       24318 :   n0 = n>>1; n1 = n-n0;
    1310       24318 :   RgX_even_odd(f, &fe, &fo);
    1311       24318 :   RgX_even_odd(g, &ge, &go);
    1312       24318 :   l = RgXn_mul(fe,ge,n1);
    1313       24318 :   h = RgXn_mul(fo,go,n0);
    1314       24318 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1315             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1316             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1317       24318 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1318             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1319       24318 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1320       24318 :   m = RgX_inflate(m,2);
    1321             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1322       24318 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1323       24318 :   h = RgX_inflate(h,2);
    1324       24318 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1325       24318 :   return gerepileupto(av, h);
    1326             : }
    1327             : /* (f*g) \/ x^n */
    1328             : static GEN
    1329        2309 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1330             : {
    1331        2309 :   long d = degpol(f)+degpol(g) + 1 - n;
    1332             :   GEN h;
    1333        2309 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1334        1564 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1335             :                                  RgX_recip_shallow(g), d));
    1336        1564 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1337             : }
    1338             : 
    1339             : /* (f*g) \/ x^n */
    1340             : static GEN
    1341           0 : RgX_sqrhigh_i2(GEN f, long n)
    1342             : {
    1343           0 :   long d = 2*degpol(f)+ 1 - n;
    1344             :   GEN h;
    1345           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1346           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1347           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1348             : }
    1349             : 
    1350             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1351             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1352             :  * Only c, c0, c1, c2 are genuine GEN.
    1353             :  */
    1354             : GEN
    1355     8789806 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1356             : {
    1357             :   GEN a0, c, c0;
    1358     8789806 :   long n0, n0a, i, v = 0;
    1359             :   pari_sp av;
    1360             : 
    1361     8789806 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1362     8789806 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1363     8789806 :   if (na < nb) swapspec(a,b, na,nb);
    1364     8789806 :   if (!nb) return pol_0(0);
    1365             : 
    1366     8722063 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1367      469079 :   RgX_shift_inplace_init(v);
    1368      469079 :   i = (na>>1); n0 = na-i; na = i;
    1369      469079 :   av = avma; a0 = a+n0; n0a = n0;
    1370      469079 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1371             : 
    1372      469079 :   if (nb > n0)
    1373             :   {
    1374             :     GEN b0,c1,c2;
    1375             :     long n0b;
    1376             : 
    1377      467755 :     nb -= n0; b0 = b+n0; n0b = n0;
    1378      467755 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1379      467755 :     c = RgX_mulspec(a,b,n0a,n0b);
    1380      467755 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1381             : 
    1382      467755 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1383      467755 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1384             : 
    1385      467755 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1386      467755 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1387      467755 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1388             :   }
    1389             :   else
    1390             :   {
    1391        1324 :     c = RgX_mulspec(a,b,n0a,nb);
    1392        1324 :     c0 = RgX_mulspec(a0,b,na,nb);
    1393             :   }
    1394      469079 :   c0 = RgX_addmulXn(c0,c,n0);
    1395      469079 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1396             : }
    1397             : 
    1398             : INLINE GEN
    1399       33134 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1400             : {
    1401       33134 :   pari_sp av = avma;
    1402       33134 :   GEN s = NULL;
    1403       33134 :   long j, l = (i+1)>>1;
    1404       98812 :   for (j=a; j<l; j++)
    1405             :   {
    1406       65678 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1407       65678 :     if (xj && xx)
    1408             :     {
    1409       58356 :       GEN t = gmul(xj, xx);
    1410       58356 :       s = s? gadd(s, t): t;
    1411             :     }
    1412             :   }
    1413       33134 :   if (s) s = gshift(s,1);
    1414       33134 :   if ((i&1) == 0)
    1415             :   {
    1416       19488 :     GEN t = gel(x, i>>1);
    1417       19488 :     if (t) {
    1418       17171 :       t = gsqr(t);
    1419       17171 :       s = s? gadd(s, t): t;
    1420             :     }
    1421             :   }
    1422       33134 :   return s? gerepileupto(av,s): gen_0;
    1423             : }
    1424             : static GEN
    1425        5842 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1426             : {
    1427             :   long i, lz, nz;
    1428             :   GEN z;
    1429             : 
    1430        5842 :   if (!nx) return pol_0(0);
    1431        5842 :   x = RgXspec_kill0(x,nx);
    1432        5842 :   lz = (nx << 1) + 1, nz = lz-2;
    1433        5842 :   lz += v;
    1434        5842 :   z = cgetg(lz,t_POL) + 2;
    1435        5842 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1436        5842 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1437        5842 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1438        5842 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1439             : }
    1440             : /* return x^2 mod t^n */
    1441             : static GEN
    1442           0 : RgXn_sqr_basecase(GEN x, long n)
    1443             : {
    1444           0 :   long i, lz = n+2, lx = lgpol(x);
    1445             :   GEN z;
    1446           0 :   if (lx < 0) return pol_0(varn(x));
    1447           0 :   z = cgetg(lz, t_POL);
    1448           0 :   z[1] = x[1];
    1449           0 :   x+=2; if (lx > n) lx = n;
    1450           0 :   x = RgXspec_kill0(x,lx);
    1451           0 :   z+=2;/* x:z [i] = term of degree i */
    1452           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1453           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1454           0 :   z -= 2; return normalizepol_lg(z, lz);
    1455             : }
    1456             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1457             : static GEN
    1458         238 : RgXn_sqr2(GEN f, long n)
    1459             : {
    1460         238 :   pari_sp av = avma;
    1461             :   GEN fe,fo, l,h,m;
    1462             :   long n0, n1;
    1463         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1464           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1465           0 :   n0 = n>>1; n1 = n-n0;
    1466           0 :   RgX_even_odd(f, &fe, &fo);
    1467           0 :   l = RgXn_sqr(fe,n1);
    1468           0 :   h = RgXn_sqr(fo,n0);
    1469           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1470             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1471             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1472           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1473             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1474           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1475           0 :   m = RgX_inflate(m,2);
    1476             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1477           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1478           0 :   h = RgX_inflate(h,2);
    1479           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1480           0 :   return gerepileupto(av, h);
    1481             : }
    1482             : GEN
    1483        5881 : RgX_sqrspec(GEN a, long na)
    1484             : {
    1485             :   GEN a0, c, c0, c1;
    1486        5881 :   long n0, n0a, i, v = 0;
    1487             :   pari_sp av;
    1488             : 
    1489        5881 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1490        5881 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1491          39 :   RgX_shift_inplace_init(v);
    1492          39 :   i = (na>>1); n0 = na-i; na = i;
    1493          39 :   av = avma; a0 = a+n0; n0a = n0;
    1494          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1495             : 
    1496          39 :   c = RgX_sqrspec(a,n0a);
    1497          39 :   c0 = RgX_sqrspec(a0,na);
    1498          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1499          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1500          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1501          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1502             : }
    1503             : 
    1504             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1505             : GEN
    1506      689475 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1507             : {
    1508      689475 :   GEN z = RgX_mul(A, B);
    1509      689475 :   if (a < b)
    1510        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1511      684029 :   else if (a > b)
    1512      432245 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1513             :   else
    1514      251784 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1515      689475 :   return z;
    1516             : }
    1517             : 
    1518             : GEN
    1519     7383854 : RgX_mul_i(GEN x, GEN y)
    1520             : {
    1521     7383854 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1522     7383854 :   setvarn(z, varn(x)); return z;
    1523             : }
    1524             : 
    1525             : GEN
    1526        5803 : RgX_sqr_i(GEN x)
    1527             : {
    1528        5803 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1529        5803 :   setvarn(z,varn(x)); return z;
    1530             : }
    1531             : 
    1532             : /*******************************************************************/
    1533             : /*                                                                 */
    1534             : /*                               DIVISION                          */
    1535             : /*                                                                 */
    1536             : /*******************************************************************/
    1537             : GEN
    1538      420236 : RgX_Rg_divexact(GEN x, GEN y) {
    1539             :   long i, lx;
    1540             :   GEN z;
    1541      420236 :   if (typ(y) == t_INT && is_pm1(y))
    1542       28124 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1543      392112 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1544      392112 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1545      392112 :   return z;
    1546             : }
    1547             : GEN
    1548    22878002 : RgX_Rg_div(GEN x, GEN y) {
    1549             :   long i, lx;
    1550    22878002 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1551    22878002 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1552    22878002 :   return normalizepol_lg(z, lx);
    1553             : }
    1554             : GEN
    1555       12698 : RgX_normalize(GEN x)
    1556             : {
    1557       12698 :   GEN z, d = NULL;
    1558       12698 :   long i, n = lg(x)-1;
    1559       12698 :   for (i = n; i > 1; i--) { d = gel(x,i); if (!gequal0(d)) break; }
    1560       12698 :   if (i == 1) return pol_0(varn(x));
    1561       12698 :   if (i == n && isint1(d)) return x;
    1562        5621 :   n = i; z = cgetg(n+1, t_POL); z[1] = x[1];
    1563        5621 :   for (i=2; i<n; i++) gel(z,i) = gdiv(gel(x,i),d);
    1564        5621 :   gel(z,n) = Rg_get_1(d); return z;
    1565             : }
    1566             : GEN
    1567        2597 : RgX_divs(GEN x, long y) {
    1568             :   long i, lx;
    1569        2597 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1570        2597 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1571        2597 :   return normalizepol_lg(z, lx);
    1572             : }
    1573             : GEN
    1574       41088 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1575             : {
    1576       41088 :   long l = lg(a), i;
    1577       41088 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1578       41088 :   z[1] = a[1];
    1579       41088 :   a0 = a + l-1;
    1580       41088 :   z0 = z + l-2; *z0 = *a0--;
    1581      788028 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1582             :   {
    1583      746940 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1584      746940 :     gel(z0,0) = t;
    1585             :   }
    1586       41088 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1587       41088 :   return z;
    1588             : }
    1589             : /* Polynomial division x / y:
    1590             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1591             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1592             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1593             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1594             : static GEN
    1595    12174356 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1596             : {
    1597             :   pari_sp avy, av, av1;
    1598             :   long dx,dy,dz,i,j,sx,lr;
    1599             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1600             :   GEN (*f)(GEN,GEN);
    1601             : 
    1602    12174356 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1603             : 
    1604    12174356 :   dy = degpol(y);
    1605    12174356 :   y_lead = gel(y,dy+2);
    1606    12174356 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1607             :   {
    1608           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1609           0 :     for (dy--; dy>=0; dy--)
    1610             :     {
    1611           0 :       y_lead = gel(y,dy+2);
    1612           0 :       if (!gequal0(y_lead)) break;
    1613             :     }
    1614             :   }
    1615    12174356 :   if (!dy) /* y is constant */
    1616             :   {
    1617       15614 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1618       15614 :     z = RgX_Rg_div(x, y_lead);
    1619       15614 :     if (pr == ONLY_DIVIDES) return z;
    1620       13864 :     if (pr) *pr = pol_0(varn(x));
    1621       13864 :     return z;
    1622             :   }
    1623    12158742 :   dx = degpol(x);
    1624    12158742 :   if (dx < dy)
    1625             :   {
    1626      637796 :     if (pr == ONLY_REM) return RgX_copy(x);
    1627      371691 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1628      371670 :     z = pol_0(varn(x));
    1629      371670 :     if (pr) *pr = RgX_copy(x);
    1630      371670 :     return z;
    1631             :   }
    1632             : 
    1633             :   /* x,y in R[X], y non constant */
    1634    11520946 :   av = avma;
    1635    11520946 :   p = NULL;
    1636    11520946 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1637             :   {
    1638      225904 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1639      225904 :     if (!z) { avma = av; return NULL; }
    1640      225904 :     z = FpX_to_mod(z, p);
    1641      225904 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1642      121681 :       return gerepileupto(av, z);
    1643      104223 :     *pr = FpX_to_mod(*pr, p);
    1644      104223 :     gerepileall(av, 2, pr, &z);
    1645      104223 :     return z;
    1646             :   }
    1647    11295042 :   switch(typ(y_lead))
    1648             :   {
    1649             :     case t_REAL:
    1650           0 :       y_lead = ginv(y_lead);
    1651           0 :       f = gmul; mod = NULL;
    1652           0 :       break;
    1653             :     case t_INTMOD:
    1654        2001 :     case t_POLMOD: y_lead = ginv(y_lead);
    1655        2001 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1656        2001 :       break;
    1657    11293041 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1658    11293041 :       f = gdiv; mod = NULL;
    1659             :   }
    1660             : 
    1661    11295042 :   if (y_lead == NULL)
    1662     9517707 :     p2 = gel(x,dx+2);
    1663             :   else {
    1664             :     for(;;) {
    1665     1777335 :       p2 = f(gel(x,dx+2),y_lead);
    1666     1777335 :       p2 = simplify_shallow(p2);
    1667     1777335 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1668             :     }
    1669     1777335 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1670             :     {
    1671           0 :       if (pr == ONLY_DIVIDES) {
    1672           0 :         avma = av;
    1673           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1674             :       }
    1675           0 :       if (pr == ONLY_REM)
    1676             :       {
    1677           0 :         if (dx < 0)
    1678           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1679             :         else
    1680             :         {
    1681             :           GEN t;
    1682           0 :           avma = av;
    1683           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1684           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1685           0 :           return t;
    1686             :         }
    1687             :       }
    1688           0 :       if (pr) /* cf ONLY_REM above */
    1689             :       {
    1690           0 :         if (dx < 0)
    1691             :         {
    1692           0 :           p2 = gclone(p2);
    1693           0 :           avma = av;
    1694           0 :           z = pol_0(varn(x));
    1695           0 :           x = scalarpol(p2, varn(x));
    1696           0 :           gunclone(p2);
    1697             :         }
    1698             :         else
    1699             :         {
    1700             :           GEN t;
    1701           0 :           avma = av;
    1702           0 :           z = pol_0(varn(x));
    1703           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1704           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1705           0 :           x = t;
    1706             :         }
    1707           0 :         *pr = x;
    1708             :       }
    1709             :       else
    1710             :       {
    1711           0 :         avma = av;
    1712           0 :         z = pol_0(varn(x));
    1713             :       }
    1714           0 :       return z;
    1715             :     }
    1716             :   }
    1717             :   /* dx >= dy */
    1718    11295042 :   avy = avma;
    1719    11295042 :   dz = dx-dy;
    1720    11295042 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1721    11295042 :   x += 2;
    1722    11295042 :   z += 2;
    1723    11295042 :   y += 2;
    1724    11295042 :   gel(z,dz) = gcopy(p2);
    1725             : 
    1726    27751072 :   for (i=dx-1; i>=dy; i--)
    1727             :   {
    1728    16456030 :     av1=avma; p1=gel(x,i);
    1729    16456030 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1730    16456030 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1731             : 
    1732    16456030 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1733             :     else
    1734     6911127 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1735    16456030 :     gel(z,i-dy) = p1;
    1736             :   }
    1737    11295042 :   if (!pr) return gerepileupto(av,z-2);
    1738             : 
    1739     4730663 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1740     5076484 :   for (sx=0; ; i--)
    1741             :   {
    1742     5422305 :     p1 = gel(x,i);
    1743             :     /* we always enter this loop at least once */
    1744     5076484 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1745     5076484 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1746     5076484 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1747     3414093 :     if (!isexactzero(p1)) break;
    1748     3401157 :     if (!i) break;
    1749      345821 :     avma=av1;
    1750             :   }
    1751     4730663 :   if (pr == ONLY_DIVIDES)
    1752             :   {
    1753        1568 :     if (sx) { avma=av; return NULL; }
    1754        1561 :     avma = (pari_sp)rem;
    1755        1561 :     return gerepileupto(av,z-2);
    1756             :   }
    1757     4729095 :   lr=i+3; rem -= lr;
    1758     4729095 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1759     4665840 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1760     4729095 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1761     4729095 :   rem[1] = z[-1];
    1762     4729095 :   rem += 2;
    1763     4729095 :   gel(rem,i) = p1;
    1764     5874546 :   for (i--; i>=0; i--)
    1765             :   {
    1766     1145451 :     av1=avma; p1 = gel(x,i);
    1767     1145451 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1768     1145451 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1769     1145451 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1770             :   }
    1771     4729095 :   rem -= 2;
    1772     4729095 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1773     4729095 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1774     4070966 :   z -= 2;
    1775             :   {
    1776     4070966 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1777     4070966 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1778             :   }
    1779             : }
    1780             : 
    1781             : GEN
    1782    11250122 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1783             : {
    1784    11250122 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1785    11250122 :   return RgX_divrem_i(x, y, pr);
    1786             : }
    1787             : 
    1788             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1789             : GEN
    1790       69308 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1791             : {
    1792             :   long vx, dx, dy, dz, i, j, sx, lr;
    1793             :   pari_sp av0, av, tetpil;
    1794             :   GEN z,p1,rem,lead;
    1795             : 
    1796       69308 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1797       69308 :   vx = varn(x);
    1798       69308 :   dx = degpol(x);
    1799       69308 :   dy = degpol(y);
    1800       69308 :   if (dx < dy)
    1801             :   {
    1802       22855 :     if (pr)
    1803             :     {
    1804       22855 :       av0 = avma; x = RgXQX_red(x, T);
    1805       22855 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1806       22855 :       if (pr == ONLY_REM) return x;
    1807           0 :       *pr = x;
    1808             :     }
    1809           0 :     return pol_0(vx);
    1810             :   }
    1811       46453 :   lead = leading_coeff(y);
    1812       46453 :   if (!dy) /* y is constant */
    1813             :   {
    1814         546 :     if (pr && pr != ONLY_DIVIDES)
    1815             :     {
    1816           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1817           0 :       *pr = pol_0(vx);
    1818             :     }
    1819         546 :     if (gequal1(lead)) return RgX_copy(x);
    1820           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1821           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1822             :   }
    1823       45907 :   av0 = avma; dz = dx-dy;
    1824       45907 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1825       45907 :   avma = av0;
    1826       45907 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1827       45907 :   x += 2; y += 2; z += 2;
    1828             : 
    1829       45907 :   p1 = gel(x,dx); av = avma;
    1830       45907 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1831      288414 :   for (i=dx-1; i>=dy; i--)
    1832             :   {
    1833      242507 :     av=avma; p1=gel(x,i);
    1834      242507 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1835      242507 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1836      242507 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1837             :   }
    1838       45907 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1839             : 
    1840       44661 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1841       70861 :   for (sx=0; ; i--)
    1842             :   {
    1843       97061 :     p1 = gel(x,i);
    1844       70861 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1845       70861 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1846       30920 :     if (!i) break;
    1847       26200 :     avma=av;
    1848             :   }
    1849       44661 :   if (pr == ONLY_DIVIDES)
    1850             :   {
    1851        2270 :     if (lead) gunclone(lead);
    1852        2270 :     if (sx) { avma=av0; return NULL; }
    1853        2172 :     avma = (pari_sp)rem; return z-2;
    1854             :   }
    1855       42391 :   lr=i+3; rem -= lr;
    1856       42391 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1857       42391 :   rem[1] = z[-1];
    1858       42391 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1859       42391 :   rem += 2; gel(rem,i) = p1;
    1860      116051 :   for (i--; i>=0; i--)
    1861             :   {
    1862       73660 :     av=avma; p1 = gel(x,i);
    1863      273526 :     for (j=0; j<=i && j<=dz; j++)
    1864      199866 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1865       73660 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1866             :   }
    1867       42391 :   rem -= 2;
    1868       42391 :   if (lead) gunclone(lead);
    1869       42391 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1870       42391 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1871          91 :   *pr = rem; return z-2;
    1872             : }
    1873             : 
    1874             : /*******************************************************************/
    1875             : /*                                                                 */
    1876             : /*                        PSEUDO-DIVISION                          */
    1877             : /*                                                                 */
    1878             : /*******************************************************************/
    1879             : INLINE GEN
    1880      462147 : rem(GEN c, GEN T)
    1881             : {
    1882      462147 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1883      462147 :   return c;
    1884             : }
    1885             : 
    1886             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1887             : int
    1888        6136 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1889             : {
    1890             :   long dx, dy, dz, i, p, T_ismonic;
    1891        6136 :   pari_sp av = avma, av2;
    1892             :   GEN y_lead;
    1893             : 
    1894        6136 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1895        6136 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1896        6136 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1897             :   /* if monic, no point in using pseudo-division */
    1898        6136 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1899        3735 :   T_ismonic = gequal1(leading_coeff(T));
    1900        3735 :   dx = degpol(x);
    1901        3735 :   if (dx < dy) return !signe(x);
    1902        3735 :   (void)new_chunk(2);
    1903        3735 :   x = RgX_recip_shallow(x)+2;
    1904        3735 :   y = RgX_recip_shallow(y)+2;
    1905             :   /* pay attention to sparse divisors */
    1906        7652 :   for (i = 1; i <= dy; i++)
    1907        3917 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1908        3735 :   dz = dx-dy; p = dz+1;
    1909        3735 :   av2 = avma;
    1910             :   for (;;)
    1911       30634 :   {
    1912       34369 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1913       34369 :     x0 = gneg(x0); p--;
    1914       34369 :     m = gcdii(cx, y0);
    1915       34369 :     if (!equali1(m))
    1916             :     {
    1917       33319 :       x0 = gdiv(x0, m);
    1918       33319 :       y0 = diviiexact(y0, m);
    1919       33319 :       if (equali1(y0)) y0 = NULL;
    1920             :     }
    1921       69928 :     for (i=1; i<=dy; i++)
    1922             :     {
    1923       35559 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1924       35559 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1925       35559 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1926       35559 :       gel(x,i) = c;
    1927             :     }
    1928      399662 :     for (   ; i<=dx; i++)
    1929             :     {
    1930      365293 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1931      365293 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1932      365293 :       gel(x,i) = c;
    1933             :     }
    1934       38097 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1935       34369 :     if (dx < dy) break;
    1936       30634 :     if (gc_needed(av2,1))
    1937             :     {
    1938           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1939           0 :       gerepilecoeffs(av2,x,dx+1);
    1940             :     }
    1941             :   }
    1942        3735 :   avma = av; return (dx < 0);
    1943             : }
    1944             : 
    1945             : /* T either NULL or a t_POL. */
    1946             : GEN
    1947       27752 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1948             : {
    1949       27752 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1950       27752 :   pari_sp av = avma, av2;
    1951             :   GEN y_lead;
    1952             : 
    1953       27752 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1954       27752 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1955             :   /* if monic, no point in using pseudo-division */
    1956       27752 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1957       24483 :   dx = degpol(x);
    1958       24483 :   if (dx < dy) return RgX_copy(x);
    1959       24483 :   (void)new_chunk(2);
    1960       24483 :   x = RgX_recip_shallow(x)+2;
    1961       24483 :   y = RgX_recip_shallow(y)+2;
    1962             :   /* pay attention to sparse divisors */
    1963       74709 :   for (i = 1; i <= dy; i++)
    1964       50226 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1965       24483 :   dz = dx-dy; p = dz+1;
    1966       24483 :   av2 = avma;
    1967             :   for (;;)
    1968             :   {
    1969      148251 :     gel(x,0) = gneg(gel(x,0)); p--;
    1970      273342 :     for (i=1; i<=dy; i++)
    1971             :     {
    1972      186975 :       GEN c = gmul(y_lead, gel(x,i));
    1973      186975 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1974      186975 :       gel(x,i) = rem(c, T);
    1975             :     }
    1976      295373 :     for (   ; i<=dx; i++)
    1977             :     {
    1978      209006 :       GEN c = gmul(y_lead, gel(x,i));
    1979      209006 :       gel(x,i) = rem(c, T);
    1980             :     }
    1981       95488 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1982       86367 :     if (dx < dy) break;
    1983       61884 :     if (gc_needed(av2,1))
    1984             :     {
    1985           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1986           0 :       gerepilecoeffs(av2,x,dx+1);
    1987             :     }
    1988             :   }
    1989       24483 :   if (dx < 0) return pol_0(vx);
    1990       22446 :   lx = dx+3; x -= 2;
    1991       22446 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1992       22446 :   x[1] = evalsigne(1) | evalvarn(vx);
    1993       22446 :   x = RgX_recip_shallow(x);
    1994       22446 :   if (p)
    1995             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1996        2506 :     GEN t = y_lead;
    1997        2506 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1998           0 :       t = RgXQ_powu(t, p, T);
    1999             :     else
    2000        2506 :       t = gpowgs(t, p);
    2001        6825 :     for (i=2; i<lx; i++)
    2002             :     {
    2003        4319 :       GEN c = gmul(gel(x,i), t);
    2004        4319 :       gel(x,i) = rem(c,T);
    2005             :     }
    2006        2506 :     if (!T) return gerepileupto(av, x);
    2007             :   }
    2008       19940 :   return gerepilecopy(av, x);
    2009             : }
    2010             : 
    2011             : GEN
    2012       27752 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2013             : 
    2014             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2015             : GEN
    2016        5333 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2017             : {
    2018        5333 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2019        5333 :   pari_sp av = avma, av2;
    2020             :   GEN z, r, ypow, y_lead;
    2021             : 
    2022        5333 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2023        5333 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2024        5333 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2025        4780 :   dx = degpol(x);
    2026        4780 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2027        4780 :   if (dx == dy)
    2028             :   {
    2029          28 :     GEN x_lead = gel(x,lg(x)-1);
    2030          28 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2031          28 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2032          28 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2033          28 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2034             :   }
    2035        4752 :   (void)new_chunk(2);
    2036        4752 :   x = RgX_recip_shallow(x)+2;
    2037        4752 :   y = RgX_recip_shallow(y)+2;
    2038             :   /* pay attention to sparse divisors */
    2039       23489 :   for (i = 1; i <= dy; i++)
    2040       18737 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2041        4752 :   dz = dx-dy; p = dz+1;
    2042        4752 :   lz = dz+3;
    2043        4752 :   z = cgetg(lz, t_POL);
    2044        4752 :   z[1] = evalsigne(1) | evalvarn(vx);
    2045        4752 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2046        4752 :   ypow = new_chunk(dz+1);
    2047        4752 :   gel(ypow,0) = gen_1;
    2048        4752 :   gel(ypow,1) = y_lead;
    2049        8028 :   for (i=2; i<=dz; i++)
    2050             :   {
    2051        3276 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2052        3276 :     gel(ypow,i) = rem(c,T);
    2053             :   }
    2054        4752 :   av2 = avma;
    2055        4752 :   for (iz=2;;)
    2056             :   {
    2057       15400 :     p--;
    2058       10076 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2059       44531 :     for (i=1; i<=dy; i++)
    2060             :     {
    2061       34455 :       GEN c = gmul(y_lead, gel(x,i));
    2062       34455 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2063       34455 :       gel(x,i) = rem(c, T);
    2064             :     }
    2065       24116 :     for (   ; i<=dx; i++)
    2066             :     {
    2067       14040 :       GEN c = gmul(y_lead, gel(x,i));
    2068       14040 :       gel(x,i) = rem(c,T);
    2069             :     }
    2070       10076 :     x++; dx--;
    2071       10076 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2072       10076 :     if (dx < dy) break;
    2073        5324 :     if (gc_needed(av2,1))
    2074             :     {
    2075           0 :       GEN X = x-2;
    2076           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2077           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2078           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2079             :     }
    2080             :   }
    2081        4752 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2082        4752 :   if (dx < 0)
    2083          98 :     x = pol_0(vx);
    2084             :   else
    2085             :   {
    2086        4654 :     lx = dx+3; x -= 2;
    2087        4654 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2088        4654 :     x[1] = evalsigne(1) | evalvarn(vx);
    2089        4654 :     x = RgX_recip_shallow(x);
    2090             :   }
    2091        4752 :   z = RgX_recip_shallow(z);
    2092        4752 :   r = x;
    2093        4752 :   if (p)
    2094             :   {
    2095        1674 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2096        1674 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2097             :   }
    2098        4752 :   gerepileall(av, 2, &z, &r);
    2099        4752 :   *ptr = r; return z;
    2100             : }
    2101             : GEN
    2102        5165 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2103        5165 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2104             : 
    2105             : GEN
    2106       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2107             : {
    2108       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2109             : }
    2110             : GEN
    2111    68922154 : RgX_Rg_mul(GEN y, GEN x) {
    2112             :   long i, ly;
    2113    68922154 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2114    68922154 :   if (ly == 2) return z;
    2115    68853505 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2116    68853498 :   return normalizepol_lg(z,ly);
    2117             : }
    2118             : GEN
    2119       14427 : RgX_muls(GEN y, long x) {
    2120             :   long i, ly;
    2121       14427 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2122       14427 :   if (ly == 2) return z;
    2123       14392 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2124       14392 :   return normalizepol_lg(z,ly);
    2125             : }
    2126             : GEN
    2127        4613 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2128             : {
    2129        4613 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2130             : }
    2131             : GEN
    2132          77 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2133             : {
    2134          77 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2135             : }
    2136             : 
    2137             : GEN
    2138        1792 : RgXQX_sqr(GEN x, GEN T)
    2139             : {
    2140        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2141             : }
    2142             : 
    2143             : GEN
    2144         448 : RgXQX_powers(GEN P, long n, GEN T)
    2145             : {
    2146         448 :   GEN v = cgetg(n+2, t_VEC);
    2147             :   long i;
    2148         448 :   gel(v, 1) = pol_1(varn(T));
    2149         448 :   if (n==0) return v;
    2150         448 :   gel(v, 2) = gcopy(P);
    2151         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2152         448 :   return v;
    2153             : }
    2154             : 
    2155             : static GEN
    2156      124369 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2157             : static GEN
    2158           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2159             : static GEN
    2160      214928 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2161             : static GEN
    2162       90137 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2163             : static GEN
    2164      226513 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2165             : static GEN
    2166      221627 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2167             : static GEN
    2168         245 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2169             : static GEN
    2170      129906 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2171             : 
    2172             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2173             :               _mul, _sqr, _one, _zero };
    2174             : 
    2175             : GEN
    2176           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2177             : {
    2178           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2179             : }
    2180             : 
    2181             : GEN
    2182      101668 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2183             : {
    2184      101668 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2185      101668 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2186             : }
    2187             : 
    2188             : /* mod X^n */
    2189             : struct modXn {
    2190             :   long v; /* varn(X) */
    2191             :   long n;
    2192             : } ;
    2193             : static GEN
    2194        2240 : _sqrXn(void *data, GEN x) {
    2195        2240 :   struct modXn *S = (struct modXn*)data;
    2196        2240 :   return RgXn_sqr(x, S->n);
    2197             : }
    2198             : static GEN
    2199        1295 : _mulXn(void *data, GEN x, GEN y) {
    2200        1295 :   struct modXn *S = (struct modXn*)data;
    2201        1295 :   return RgXn_mul(x,y, S->n);
    2202             : }
    2203             : static GEN
    2204        1477 : _oneXn(void *data) {
    2205        1477 :   struct modXn *S = (struct modXn*)data;
    2206        1477 :   return pol_1(S->v);
    2207             : }
    2208             : static GEN
    2209           0 : _zeroXn(void *data) {
    2210           0 :   struct modXn *S = (struct modXn*)data;
    2211           0 :   return pol_0(S->v);
    2212             : }
    2213             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2214             :                                           _oneXn, _zeroXn };
    2215             : 
    2216             : GEN
    2217         357 : RgXn_powers(GEN x, long m, long n)
    2218             : {
    2219         357 :   long d = degpol(x);
    2220         357 :   int use_sqr = (d<<1) >= n;
    2221             :   struct modXn S;
    2222         357 :   S.v = varn(x); S.n = n;
    2223         357 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2224             : }
    2225             : 
    2226             : GEN
    2227        1820 : RgXn_powu_i(GEN x, ulong m, long n)
    2228             : {
    2229             :   struct modXn S;
    2230        1820 :   S.v = varn(x); S.n = n;
    2231        1820 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2232             : }
    2233             : GEN
    2234           0 : RgXn_powu(GEN x, ulong m, long n)
    2235             : {
    2236             :   struct modXn S;
    2237           0 :   S.v = varn(x); S.n = n;
    2238           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2239             : }
    2240             : 
    2241             : GEN
    2242         714 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2243             : {
    2244             :   struct modXn S;
    2245         714 :   S.v = varn(gel(x,2)); S.n = n;
    2246         714 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2247             : }
    2248             : 
    2249             : GEN
    2250           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2251             : {
    2252           0 :   int use_sqr = 2*degpol(x) >= n;
    2253             :   struct modXn S;
    2254           0 :   S.v = varn(x); S.n = n;
    2255           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2256             : }
    2257             : 
    2258             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2259             : GEN
    2260        6503 : RgXn_eval(GEN Q, GEN x, long n)
    2261             : {
    2262        6503 :   long d = degpol(x);
    2263             :   int use_sqr;
    2264             :   struct modXn S;
    2265        6503 :   if (d == 1 && isrationalzero(gel(x,2)))
    2266             :   {
    2267        6496 :     GEN y = RgX_unscale(Q, gel(x,3));
    2268        6496 :     setvarn(y, varn(x)); return y;
    2269             :   }
    2270           7 :   S.v = varn(x);
    2271           7 :   S.n = n;
    2272           7 :   use_sqr = (d<<1) >= n;
    2273           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2274             : }
    2275             : 
    2276             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2277             : static GEN
    2278       43420 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2279             : {
    2280       43420 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2281       43420 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2282             : }
    2283             : 
    2284             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2285             : static GEN
    2286           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2287             : {
    2288           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2289           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2290             : }
    2291             : 
    2292             : GEN
    2293      126133 : RgXn_inv_i(GEN f, long e)
    2294             : {
    2295             :   pari_sp av;
    2296             :   ulong mask;
    2297             :   GEN W, a;
    2298      126133 :   long v = varn(f), n = 1;
    2299             : 
    2300      126133 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2301      126133 :   a = ginv(gel(f,2));
    2302      126133 :   if (e == 1) return scalarpol(a, v);
    2303      126105 :   else if (e == 2)
    2304             :   {
    2305             :     GEN b;
    2306      108745 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2307       88690 :     av = avma; b = gneg(b);
    2308       88690 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2309       88690 :     W = deg1pol_shallow(b, a, v);
    2310       88690 :     return gcopy(W);
    2311             :   }
    2312       17360 :   W = scalarpol_shallow(a,v);
    2313       17360 :   mask = quadratic_prec_mask(e);
    2314       17360 :   av = avma;
    2315       78140 :   for (;mask>1;)
    2316             :   {
    2317             :     GEN u, fr;
    2318       43420 :     long n2 = n;
    2319       43420 :     n<<=1; if (mask & 1) n--;
    2320       43420 :     mask >>= 1;
    2321       43420 :     fr = RgXn_red_shallow(f, n);
    2322       43420 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2323       43420 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2324       43420 :     if (gc_needed(av,2))
    2325             :     {
    2326           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2327           0 :       W = gerepileupto(av, W);
    2328             :     }
    2329             :   }
    2330       17360 :   return W;
    2331             : }
    2332             : 
    2333             : static GEN
    2334           0 : RgXn_inv_FpX(GEN x, long e, GEN p)
    2335             : {
    2336           0 :   pari_sp av = avma;
    2337             :   GEN r;
    2338           0 :   if (lgefint(p) == 3)
    2339             :   {
    2340           0 :     ulong pp = uel(p, 2);
    2341           0 :     r = Flx_to_ZX_inplace(Flxn_inv(RgX_to_Flx(x, pp), e, pp));
    2342             :   }
    2343             :   else
    2344           0 :     r = FpXn_inv(RgX_to_FpX(x, p), e, p);
    2345           0 :   return gerepileupto(av, FpX_to_mod(r, p));
    2346             : }
    2347             : 
    2348             : static GEN
    2349           0 : RgXn_inv_FpXQX(GEN x, long n, GEN pol, GEN p)
    2350             : {
    2351           0 :   pari_sp av = avma;
    2352           0 :   GEN r, T = RgX_to_FpX(pol, p);
    2353           0 :   if (signe(T) == 0) pari_err_OP("/", gen_1, x);
    2354           0 :   r = FpXQXn_inv(RgX_to_FpXQX(x, T, p), n, T, p);
    2355           0 :   return gerepileupto(av, FpXQX_to_mod(r, pol, p));
    2356             : }
    2357             : 
    2358             : #define code(t1,t2) ((t1 << 6) | t2)
    2359             : 
    2360             : static GEN
    2361       82936 : RgXn_inv_fast(GEN x, long e)
    2362             : {
    2363             :   GEN p, pol;
    2364             :   long pa;
    2365       82936 :   long t = RgX_type(x,&p,&pol,&pa);
    2366       82936 :   switch(t)
    2367             :   {
    2368           0 :     case t_INTMOD: return RgXn_inv_FpX(x, e, p);
    2369             :     case code(t_POLMOD, t_INTMOD):
    2370           0 :                    return RgXn_inv_FpXQX(x, e, pol, p);
    2371       82936 :     default:       return NULL;
    2372             :   }
    2373             : }
    2374             : #undef code
    2375             : 
    2376             : GEN
    2377       82936 : RgXn_inv(GEN f, long e)
    2378             : {
    2379       82936 :   GEN h = RgXn_inv_fast(f, e);
    2380       82936 :   if (h) return h;
    2381       82936 :   return RgXn_inv_i(f, e);
    2382             : }
    2383             : 
    2384             : GEN
    2385       12957 : RgXn_exp(GEN h, long e)
    2386             : {
    2387       12957 :   pari_sp av = avma, av2;
    2388       12957 :   long v = varn(h), n=1;
    2389       12957 :   GEN f = pol_1(v), g = pol_1(v);
    2390       12957 :   ulong mask = quadratic_prec_mask(e);
    2391       12957 :   av2 = avma;
    2392       12957 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2393           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2394       40110 :   for (;mask>1;)
    2395             :   {
    2396             :     GEN q, w;
    2397       14196 :     long n2 = n;
    2398       14196 :     n<<=1; if (mask & 1) n--;
    2399       14196 :     mask >>= 1;
    2400       14196 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2401       14196 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2402       14196 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2403       14196 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2404       14196 :     if (gc_needed(av2,2))
    2405             :     {
    2406           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2407           0 :       gerepileall(av2, 2, &f, &g);
    2408             :     }
    2409             :   }
    2410       12957 :   return gerepileupto(av, f);
    2411             : }
    2412             : 
    2413             : GEN
    2414          91 : RgXn_reverse(GEN f, long e)
    2415             : {
    2416          91 :   pari_sp av = avma, av2;
    2417             :   ulong mask;
    2418             :   GEN fi, a, df, W, an;
    2419          91 :   long v = varn(f), n=1;
    2420          91 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2421           0 :     pari_err_INV("serreverse",f);
    2422          91 :   fi = ginv(gel(f,3));
    2423          91 :   a = deg1pol_shallow(fi,gen_0,v);
    2424          91 :   if (e <= 2) return gerepilecopy(av, a);
    2425          91 :   W = scalarpol(fi,v);
    2426          91 :   df = RgX_deriv(f);
    2427          91 :   mask = quadratic_prec_mask(e);
    2428          91 :   av2 = avma;
    2429         539 :   for (;mask>1;)
    2430             :   {
    2431             :     GEN u, fa, fr;
    2432         357 :     long n2 = n, rt;
    2433         357 :     n<<=1; if (mask & 1) n--;
    2434         357 :     mask >>= 1;
    2435         357 :     fr = RgXn_red_shallow(f, n);
    2436         357 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2437         357 :     an = RgXn_powers(a, rt, n);
    2438         357 :     if (n>1)
    2439             :     {
    2440         357 :       long n4 = (n2+1)>>1;
    2441         357 :       GEN dfr = RgXn_red_shallow(df, n2);
    2442         357 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2443         357 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2444         357 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2445             :     }
    2446         357 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2447         357 :     fa = RgX_shift(fa, -n2);
    2448         357 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2449         357 :     if (gc_needed(av2,2))
    2450             :     {
    2451           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2452           0 :       gerepileall(av2, 2, &a, &W);
    2453             :     }
    2454             :   }
    2455          91 :   return gerepileupto(av, a);
    2456             : }
    2457             : 
    2458             : GEN
    2459           0 : RgXn_sqrt(GEN h, long e)
    2460             : {
    2461           0 :   pari_sp av = avma, av2;
    2462           0 :   long v = varn(h), n = 1;
    2463           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2464           0 :   ulong mask = quadratic_prec_mask(e);
    2465           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2466           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2467           0 :   av2 = avma;
    2468             :   while(1)
    2469           0 :   {
    2470           0 :     long n2 = n, m;
    2471             :     GEN g;
    2472           0 :     n<<=1; if (mask & 1) n--;
    2473           0 :     mask >>= 1;
    2474           0 :     m = n-n2;
    2475           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2476           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2477           0 :     if (mask==1) return gerepileupto(av, f);
    2478           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2479           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2480           0 :     if (gc_needed(av2,2))
    2481             :     {
    2482           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2483           0 :       gerepileall(av2, 2, &f, &df);
    2484             :     }
    2485             :   }
    2486             : }
    2487             : 
    2488             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2489             : GEN
    2490      181844 : RgXQ_powu(GEN x, ulong n, GEN T)
    2491             : {
    2492             :   pari_sp av;
    2493             :   GEN y;
    2494             : 
    2495      181844 :   if (!n) return pol_1(varn(x));
    2496      180234 :   if (n == 1) return RgX_copy(x);
    2497      122293 :   av = avma;
    2498      122293 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2499      122304 :   return gerepileupto(av, y);
    2500             : }
    2501             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2502             : GEN
    2503       19361 : RgXQ_pow(GEN x, GEN n, GEN T)
    2504             : {
    2505             :   pari_sp av;
    2506       19361 :   long s = signe(n);
    2507             :   GEN y;
    2508             : 
    2509       19361 :   if (!s) return pol_1(varn(x));
    2510       19361 :   if (is_pm1(n) == 1)
    2511           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2512       19361 :   av = avma;
    2513       19361 :   if (s < 0) x = RgXQ_inv(x, T);
    2514       19361 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2515       19361 :   return gerepileupto(av, y);
    2516             : }
    2517             : 
    2518             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2519             : GEN
    2520        3402 : RgXQ_powers(GEN x, long l, GEN T)
    2521             : {
    2522        3402 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2523        3402 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2524             : }
    2525             : 
    2526             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2527             : GEN
    2528        2331 : QXQ_powers(GEN a, long n, GEN T)
    2529             : {
    2530        2331 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2531             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2532        2331 :   if (den)
    2533             :   { /* restore denominators */
    2534        1393 :     GEN d = den;
    2535             :     long i;
    2536        1393 :     gel(v,2) = a;
    2537        4109 :     for (i=3; i<=n+1; i++) {
    2538        2716 :       d = mulii(d,den);
    2539        2716 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2540             :     }
    2541             :   }
    2542        2331 :   return v;
    2543             : }
    2544             : 
    2545             : static GEN
    2546        1589 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2547             : {
    2548        1589 :   long l, i, m = 0;
    2549             :   GEN dz, z;
    2550        1589 :   GEN V = cgetg_copy(v, &l);
    2551        5040 :   for (i = imin; i < l; i++)
    2552             :   {
    2553        3451 :     GEN c = gel(v, i);
    2554        3451 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2555             :   }
    2556        1589 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2557        1589 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2558        5040 :   for (i = imin; i < l; i++)
    2559             :   {
    2560        3451 :     GEN c = gel(v,i);
    2561        3451 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2562        3451 :     gel(V,i) = c;
    2563             :   }
    2564        1589 :   return V;
    2565             : }
    2566             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2567             : GEN
    2568        1526 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2569        1526 : { return do_QXQ_eval(v, 1, a, T); }
    2570             : GEN
    2571          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2572          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2573             : 
    2574             : GEN
    2575         287 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2576             : {
    2577         287 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2578             : }
    2579             : 
    2580             : GEN
    2581       38668 : RgXQ_norm(GEN x, GEN T)
    2582             : {
    2583             :   pari_sp av;
    2584       38668 :   long dx = degpol(x);
    2585             :   GEN L, y;
    2586             : 
    2587       38668 :   av = avma; y = resultant(T, x);
    2588       38668 :   L = leading_coeff(T);
    2589       38668 :   if (gequal1(L) || !signe(x)) return y;
    2590           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2591             : }
    2592             : 
    2593             : GEN
    2594      113532 : RgX_blocks(GEN P, long n, long m)
    2595             : {
    2596      113532 :   GEN z = cgetg(m+1,t_VEC);
    2597      113532 :   long i,j, k=2, l = lg(P);
    2598      533768 :   for(i=1; i<=m; i++)
    2599             :   {
    2600      420236 :     GEN zi = cgetg(n+2,t_POL);
    2601      420236 :     zi[1] = P[1];
    2602      420236 :     gel(z,i) = zi;
    2603     2632899 :     for(j=2; j<n+2; j++)
    2604     2212663 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2605      420236 :     zi = RgX_renormalize_lg(zi, n+2);
    2606             :   }
    2607      113532 :   return z;
    2608             : }
    2609             : 
    2610             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2611             : void
    2612       73795 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2613             : {
    2614       73795 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2615             :   GEN p0, p1;
    2616             : 
    2617       73801 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2618             : 
    2619       73514 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2620       73514 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2621       73516 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2622     4612421 :   for (i=0; i<n1; i++)
    2623             :   {
    2624     4538903 :     p0[2+i] = p[2+(i<<1)];
    2625     4538903 :     p1[2+i] = p[3+(i<<1)];
    2626             :   }
    2627       73518 :   if (n1 != n0)
    2628       41529 :     p0[2+i] = p[2+(i<<1)];
    2629       73518 :   *pe = normalizepol(p0);
    2630       73515 :   *po = normalizepol(p1);
    2631             : }
    2632             : 
    2633             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2634             : GEN
    2635       40670 : RgX_splitting(GEN p, long k)
    2636             : {
    2637       40670 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2638             :   GEN r;
    2639             : 
    2640       40670 :   m = n/k;
    2641       40670 :   r = cgetg(k+1,t_VEC);
    2642      224154 :   for(i=1; i<=k; i++)
    2643             :   {
    2644      183484 :     gel(r,i) = cgetg(m+3, t_POL);
    2645      183484 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2646             :   }
    2647      553245 :   for (j=1, i=0, l=2; i<=n; i++)
    2648             :   {
    2649      512575 :     gmael(r,j,l) = gel(p,2+i);
    2650      512575 :     if (j==k) { j=1; l++; } else j++;
    2651             :   }
    2652      224154 :   for(i=1; i<=k; i++)
    2653      183484 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2654       40670 :   return r;
    2655             : }
    2656             : 
    2657             : /*******************************************************************/
    2658             : /*                                                                 */
    2659             : /*                        Kronecker form                           */
    2660             : /*                                                                 */
    2661             : /*******************************************************************/
    2662             : 
    2663             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2664             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2665             :  * normalized coefficients */
    2666             : GEN
    2667      132055 : Kronecker_to_mod(GEN z, GEN T)
    2668             : {
    2669      132055 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2670      132055 :   GEN x, t = cgetg(N,t_POL);
    2671      132055 :   t[1] = T[1];
    2672      132055 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2673      132055 :   x[1] = z[1];
    2674      132055 :   T = RgX_copy(T);
    2675     1195382 :   for (i=2; i<lx+2; i++, z+= N-2)
    2676             :   {
    2677     1063327 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2678     1063327 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2679             :   }
    2680      132055 :   N = (l-2) % (N-2) + 2;
    2681      132055 :   for (j=2; j<N; j++) t[j] = z[j];
    2682      132055 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2683      132055 :   return normalizepol_lg(x, i+1);
    2684             : }
    2685             : 
    2686             : /*******************************************************************/
    2687             : /*                                                                 */
    2688             : /*                        Domain detection                         */
    2689             : /*                                                                 */
    2690             : /*******************************************************************/
    2691             : 
    2692             : static GEN
    2693      307518 : zero_FpX_mod(GEN p, long v)
    2694             : {
    2695      307518 :   GEN r = cgetg(3,t_POL);
    2696      307518 :   r[1] = evalvarn(v);
    2697      307518 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2698      307518 :   return r;
    2699             : }
    2700             : 
    2701             : static GEN
    2702      494598 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2703             : {
    2704      494598 :   pari_sp av = avma;
    2705             :   GEN r;
    2706      494598 :   if (lgefint(p) == 3)
    2707             :   {
    2708      466219 :     ulong pp = uel(p, 2);
    2709      466219 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2710             :                                   RgX_to_Flx(y, pp), pp));
    2711             :   }
    2712             :   else
    2713       28379 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2714      494598 :   if (signe(r)==0)
    2715      109803 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2716      384795 :   return gerepileupto(av, FpX_to_mod(r, p));
    2717             : }
    2718             : 
    2719             : static GEN
    2720          21 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2721             : {
    2722          21 :   GEN r = cgetg(3,t_POL);
    2723          21 :   r[1] = evalvarn(v);
    2724          21 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2725          21 :   return r;
    2726             : }
    2727             : 
    2728             : static GEN
    2729          77 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2730             : {
    2731          77 :   pari_sp av = avma;
    2732             :   long dT;
    2733             :   GEN kx, ky, r;
    2734          77 :   GEN T = RgX_to_FpX(pol, p);
    2735          77 :   if (signe(T)==0) pari_err_OP("*", x, y);
    2736          70 :   dT = degpol(T);
    2737          70 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2738          70 :   ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2739          70 :   r = FpX_mul(kx, ky, p);
    2740          70 :   if (signe(r)==0)
    2741          14 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2742          56 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2743             : }
    2744             : 
    2745             : static GEN
    2746      335205 : RgX_liftred(GEN x, GEN T)
    2747      335205 : { return RgXQX_red(liftpol_shallow(x), T); }
    2748             : 
    2749             : static GEN
    2750      128077 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2751             : {
    2752      128077 :   pari_sp av = avma;
    2753      128077 :   long dT = degpol(T);
    2754      128077 :   GEN r = QX_mul(ZXX_to_Kronecker(RgX_liftred(x, T), dT),
    2755             :                  ZXX_to_Kronecker(RgX_liftred(y, T), dT));
    2756      128077 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2757             : }
    2758             : 
    2759             : static GEN
    2760        1260 : RgX_sqr_FpX(GEN x, GEN p)
    2761             : {
    2762        1260 :   pari_sp av = avma;
    2763             :   GEN r;
    2764        1260 :   if (lgefint(p) == 3)
    2765             :   {
    2766        1056 :     ulong pp = uel(p, 2);
    2767        1056 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2768             :   }
    2769             :   else
    2770         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2771        1260 :   if (signe(r)==0)
    2772          98 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2773        1162 :   return gerepileupto(av, FpX_to_mod(r, p));
    2774             : }
    2775             : 
    2776             : static GEN
    2777         154 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2778             : {
    2779         154 :   pari_sp av = avma;
    2780             :   long dT;
    2781         154 :   GEN kx, r, T = RgX_to_FpX(pol, p);
    2782         154 :   if (signe(T)==0) pari_err_OP("*",x,x);
    2783         147 :   dT = degpol(T);
    2784         147 :   kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2785         147 :   r = FpX_sqr(kx, p);
    2786         147 :   if (signe(r)==0)
    2787           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2788         147 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2789             : }
    2790             : 
    2791             : static GEN
    2792        3775 : RgX_sqr_QXQX(GEN x, GEN T)
    2793             : {
    2794        3775 :   pari_sp av = avma;
    2795        3775 :   long dT = degpol(T);
    2796        3775 :   GEN r = QX_sqr(ZXX_to_Kronecker(RgX_liftred(x, T), dT));
    2797        3775 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2798             : }
    2799             : 
    2800             : static GEN
    2801      649348 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2802             : {
    2803      649348 :   pari_sp av = avma;
    2804             :   GEN r;
    2805      649348 :   if (lgefint(p) == 3)
    2806             :   {
    2807      640717 :     ulong pp = uel(p, 2);
    2808      640717 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2809             :                                   RgX_to_Flx(y, pp), pp));
    2810             :   }
    2811             :   else
    2812        8631 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2813      649348 :   if (signe(r)==0)
    2814      197617 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2815      451731 :   return gerepileupto(av, FpX_to_mod(r, p));
    2816             : }
    2817             : 
    2818             : static GEN
    2819       37638 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    2820             : {
    2821       37638 :   pari_sp av = avma;
    2822             :   GEN r;
    2823       37638 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    2824       37638 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    2825             : }
    2826             : static GEN
    2827          42 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2828             : {
    2829          42 :   pari_sp av = avma;
    2830             :   GEN r;
    2831          42 :   GEN T = RgX_to_FpX(pol, p);
    2832          42 :   if (signe(T) == 0) pari_err_OP("%", x, y);
    2833          35 :   if (lgefint(p) == 3)
    2834             :   {
    2835          35 :     ulong pp = uel(p, 2);
    2836          35 :     GEN Tp = ZX_to_Flx(T, pp);
    2837          35 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2838             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2839             :   }
    2840             :   else
    2841           0 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2842          35 :   if (signe(r)==0)
    2843           7 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2844          28 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2845             : }
    2846             : 
    2847             : #define code(t1,t2) ((t1 << 6) | t2)
    2848             : static GEN
    2849    39621822 : RgX_mul_fast(GEN x, GEN y)
    2850             : {
    2851             :   GEN p, pol;
    2852             :   long pa;
    2853    39621822 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2854    39621978 :   switch(t)
    2855             :   {
    2856    28239005 :     case t_INT:    return ZX_mul(x,y);
    2857      970752 :     case t_FRAC:   return QX_mul(x,y);
    2858      101739 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2859      494598 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2860             :     case code(t_POLMOD, t_INT):
    2861             :     case code(t_POLMOD, t_FRAC):
    2862      128077 :                    return RgX_mul_QXQX(x, y, pol);
    2863             :     case code(t_POLMOD, t_INTMOD):
    2864          77 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2865     9687730 :     default:       return NULL;
    2866             :   }
    2867             : }
    2868             : static GEN
    2869     1763556 : RgX_sqr_fast(GEN x)
    2870             : {
    2871             :   GEN p, pol;
    2872             :   long pa;
    2873     1763556 :   long t = RgX_type(x,&p,&pol,&pa);
    2874     1764606 :   switch(t)
    2875             :   {
    2876     1718684 :     case t_INT:    return ZX_sqr(x);
    2877       32669 :     case t_FRAC:   return QX_sqr(x);
    2878        2261 :     case t_FFELT:  return FFX_sqr(x, pol);
    2879        1260 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2880             :     case code(t_POLMOD, t_INT):
    2881             :     case code(t_POLMOD, t_FRAC):
    2882        3775 :                    return RgX_sqr_QXQX(x, pol);
    2883             :     case code(t_POLMOD, t_INTMOD):
    2884         154 :                    return RgX_sqr_FpXQX(x, pol, p);
    2885        5803 :     default:       return NULL;
    2886             :   }
    2887             : }
    2888             : 
    2889             : static GEN
    2890     6481259 : RgX_rem_fast(GEN x, GEN y)
    2891             : {
    2892             :   GEN p, pol;
    2893             :   long pa;
    2894     6481259 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2895     6481619 :   switch(t)
    2896             :   {
    2897     3657391 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    2898     1223704 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    2899        1883 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2900      649348 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2901             :     case code(t_POLMOD, t_INT):
    2902             :     case code(t_POLMOD, t_FRAC):
    2903       37638 :                    return RgX_rem_QXQX(x, y, pol);
    2904             :     case code(t_POLMOD, t_INTMOD):
    2905          42 :                    return RgX_rem_FpXQX(x, y, pol, p);
    2906      911613 :     default:       return NULL;
    2907             :   }
    2908             : }
    2909             : 
    2910             : #undef code
    2911             : 
    2912             : GEN
    2913    37111601 : RgX_mul(GEN x, GEN y)
    2914             : {
    2915    37111601 :   GEN z = RgX_mul_fast(x,y);
    2916    37111961 :   if (!z) z = RgX_mul_i(x,y);
    2917    37111961 :   return z;
    2918             : }
    2919             : 
    2920             : GEN
    2921     1746969 : RgX_sqr(GEN x)
    2922             : {
    2923     1746969 :   GEN z = RgX_sqr_fast(x);
    2924     1746493 :   if (!z) z = RgX_sqr_i(x);
    2925     1746500 :   return z;
    2926             : }
    2927             : 
    2928             : GEN
    2929     6481268 : RgX_rem(GEN x, GEN y)
    2930             : {
    2931     6481268 :   GEN z = RgX_rem_fast(x, y);
    2932     6481597 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    2933     6481597 :   return z;
    2934             : }
    2935             : 
    2936             : GEN
    2937     2466803 : RgXn_mul(GEN f, GEN g, long n)
    2938             : {
    2939     2466803 :   pari_sp av = avma;
    2940     2466803 :   GEN h = RgX_mul_fast(f,g);
    2941     2466803 :   if (!h) return RgXn_mul2(f,g,n);
    2942      165236 :   if (degpol(h) < n) return h;
    2943       41440 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    2944             : }
    2945             : 
    2946             : GEN
    2947       16625 : RgXn_sqr(GEN f, long n)
    2948             : {
    2949       16625 :   pari_sp av = avma;
    2950       16625 :   GEN g = RgX_sqr_fast(f);
    2951       16625 :   if (!g) return RgXn_sqr2(f,n);
    2952       16387 :   if (degpol(g) < n) return g;
    2953        1057 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    2954             : }
    2955             : 
    2956             : /* (f*g) \/ x^n */
    2957             : GEN
    2958       43420 : RgX_mulhigh_i(GEN f, GEN g, long n)
    2959             : {
    2960       43420 :   GEN h = RgX_mul_fast(f,g);
    2961       43420 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    2962             : }
    2963             : 
    2964             : /* (f*g) \/ x^n */
    2965             : GEN
    2966           0 : RgX_sqrhigh_i(GEN f, long n)
    2967             : {
    2968           0 :   GEN h = RgX_sqr_fast(f);
    2969           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    2970             : }

Generated by: LCOV version 1.13