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.10.0 lcov report (development 22303-eb3e11d) Lines: 1452 1607 90.4 %
Date: 2018-04-21 06:16:28 Functions: 171 184 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /*******************************************************************/
      18             : /*                                                                 */
      19             : /*                         GENERIC                                 */
      20             : /*                                                                 */
      21             : /*******************************************************************/
      22             : 
      23             : /* Return optimal parameter l for the evaluation of n/m polynomials of degree d
      24             :    Fractional values can be used if the evaluations are done with different
      25             :    accuracies, and thus have different weights.
      26             :  */
      27             : long
      28     3098541 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     3098541 :   long pold=1, rold=n*(d-1);
      32    18108554 :   for(p=2; p<=d; p++)
      33             :   {
      34    15010013 :     r = m*(p-1) + n*((d-1)/p);
      35    15010013 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     3098541 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41    12183215 : 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    12183215 :   pari_sp av = avma;
      45             :   long i;
      46    12183215 :   GEN z = cmul(E,P,a,ff->one(E));
      47    12183229 :   if (!z) z = gen_0;
      48    71669832 :   for (i=1; i<=n; i++)
      49             :   {
      50    59486624 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    59486775 :     if (t) {
      52    57707836 :       z = ff->add(E, z, t);
      53    57707450 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56    12183208 :   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     7576967 : 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     7576967 :   pari_sp av = avma;
      69     7576967 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     7576967 :   if (d < 0) return ff->zero(E);
      73     7077276 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     2977047 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     2977047 :   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     2977047 :   d -= l;
      81     2977047 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     8082997 :   while (d >= l-1)
      83             :   {
      84     2128906 :     d -= l-1;
      85     2128906 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86     2128893 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87     2128903 :     if (gc_needed(av,2))
      88          61 :       z = gerepileupto(av, z);
      89             :   }
      90     2977045 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91     2977042 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92     2977046 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96     1393704 : 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     1393704 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102     1393704 :   if (d < 0) return ff->zero(E);
     103     1393417 :   rtd = (long) sqrt((double)d);
     104     1393417 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105     1393418 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106     1393416 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      881611 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    21659369 : _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      753146 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      244218 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      899195 : _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      368781 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      368781 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      130655 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      130655 :   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        2814 : RgXV_RgV_eval(GEN Q, GEN x)
     145             : {
     146        2814 :   long i, l = lg(Q), vQ = gvar(Q);
     147        2814 :   GEN v = cgetg(l, t_VEC);
     148      236124 :   for (i = 1; i < l; i++)
     149             :   {
     150      233310 :     GEN Qi = gel(Q, i);
     151      233310 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     152             :   }
     153        2814 :   return v;
     154             : }
     155             : 
     156             : const struct bb_algebra *
     157      113467 : get_Rg_algebra(void)
     158             : {
     159      113467 :   return &Rg_algebra;
     160             : }
     161             : 
     162             : static struct bb_ring Rg_ring = {  _gen_add, _gen_mul, _gen_sqr };
     163             : 
     164             : static GEN
     165        9058 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
     166             : {
     167             :   (void) E;
     168        9058 :   return RgX_divrem(x, y, r);
     169             : }
     170             : 
     171             : GEN
     172        2219 : RgX_digits(GEN x, GEN T)
     173             : {
     174        2219 :   pari_sp av = avma;
     175        2219 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     176        2219 :   GEN z = gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
     177        2219 :   return gerepileupto(av, z);
     178             : }
     179             : 
     180             : /*******************************************************************/
     181             : /*                                                                 */
     182             : /*                         RgX                                     */
     183             : /*                                                                 */
     184             : /*******************************************************************/
     185             : 
     186             : long
     187     3767249 : RgX_equal(GEN x, GEN y)
     188             : {
     189     3767249 :   long i = lg(x);
     190             : 
     191     3767249 :   if (i != lg(y)) return 0;
     192    19931142 :   for (i--; i > 1; i--)
     193    16222539 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194     3708603 :   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     2325900 : Rg_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203     2325900 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     204     2325900 :   if (RgX_type_is_composite(tx))
     205      376684 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206     2325900 :   switch(tx)
     207             :   {
     208          91 :     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     2325165 :     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      129171 : Rg_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220      129171 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     221      129171 :   if (RgX_type_is_composite(tx))
     222       25151 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223      129171 :   switch(tx)
     224             :   {
     225         259 :     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      128702 :     default: return gen_0;
     229             :   }
     230             : }
     231             : 
     232             : GEN
     233        2674 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     234             : {
     235        2674 :   long i, n = degpol(P);
     236             :   GEN z, dz, dP;
     237        2674 :   if (n < 0) return gen_0;
     238        2674 :   P = Q_remove_denom(P, &dP);
     239        2674 :   z = gel(P,2); if (n == 0) return icopy(z);
     240        1442 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     241        1442 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     242        1442 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     243        1442 :   dz = mul_denom(dP, dV);
     244        1442 :   return dz? RgX_Rg_div(z, dz): z;
     245             : }
     246             : 
     247             : /* Return P(h * x), not memory clean */
     248             : GEN
     249       10535 : RgX_unscale(GEN P, GEN h)
     250             : {
     251       10535 :   long i, l = lg(P);
     252       10535 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253       10535 :   Q[1] = P[1];
     254       10535 :   if (l == 2) return Q;
     255       10507 :   gel(Q,2) = gcopy(gel(P,2));
     256       34853 :   for (i=3; i<l; i++)
     257             :   {
     258       24346 :     hi = gmul(hi,h);
     259       24346 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261       10507 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      322931 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      322931 :   long i, l = lg(P);
     268      322931 :   GEN Q = cgetg(l, t_POL);
     269      322931 :   Q[1] = P[1];
     270      322931 :   if (l == 2) return Q;
     271      322931 :   gel(Q,2) = gel(P,2);
     272      322931 :   if (l == 3) return Q;
     273      322931 :   if (h == -1)
     274      309085 :     for (i = 3; i < l; i++)
     275             :     {
     276      301020 :       gel(Q,i) = negi(gel(P,i));
     277      301020 :       if (++i == l) break;
     278      297710 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      311556 :     gel(Q,3) = mulis(gel(P,3), h);
     284      311556 :     hi = sqrs(h);
     285      661430 :     for (i = 4; i < l; i++)
     286             :     {
     287      349874 :       gel(Q,i) = mulii(gel(P,i), hi);
     288      349874 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      322931 :   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        7336 : ZX_unscale(GEN P, GEN h)
     296             : {
     297             :   long i, l;
     298             :   GEN Q, hi;
     299        7336 :   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       19099 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       19099 :   long i, ni = n, l = lg(P);
     319       19099 :   GEN Q = cgetg(l, t_POL);
     320       19099 :   Q[1] = P[1];
     321       19099 :   if (l == 2) return Q;
     322       19099 :   gel(Q,2) = gel(P,2);
     323       19099 :   if (l == 3) return Q;
     324       19099 :   gel(Q,3) = shifti(gel(P,3), ni);
     325       77333 :   for (i=4; i<l; i++)
     326             :   {
     327       58234 :     ni += n;
     328       58234 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       19099 :   return Q;
     331             : }
     332             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     333             : GEN
     334        1176 : ZX_unscale_div(GEN P, GEN h)
     335             : {
     336        1176 :   long i, l = lg(P);
     337        1176 :   GEN hi, Q = cgetg(l, t_POL);
     338        1176 :   Q[1] = P[1];
     339        1176 :   if (l == 2) return Q;
     340        1176 :   gel(Q,2) = diviiexact(gel(P,2), h);
     341        1176 :   if (l == 3) return Q;
     342        1176 :   gel(Q,3) = gel(P,3);
     343        1176 :   if (l == 4) return Q;
     344        1176 :   hi = h;
     345        1176 :   gel(Q,4) = mulii(gel(P,4), hi);
     346        5194 :   for (i=5; i<l; i++)
     347             :   {
     348        4018 :     hi = mulii(hi,h);
     349        4018 :     gel(Q,i) = mulii(gel(P,i), hi);
     350             :   }
     351        1176 :   return Q;
     352             : }
     353             : 
     354             : GEN
     355         357 : RgXV_unscale(GEN v, GEN h)
     356             : {
     357             :   long i, l;
     358             :   GEN w;
     359         357 :   if (!h || isint1(h)) return v;
     360         301 :   w = cgetg_copy(v, &l);
     361         301 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     362         301 :   return w;
     363             : }
     364             : 
     365             : /* Return h^degpol(P) P(x / h), not memory clean */
     366             : GEN
     367        1925 : RgX_rescale(GEN P, GEN h)
     368             : {
     369        1925 :   long i, l = lg(P);
     370        1925 :   GEN Q = cgetg(l,t_POL), hi = h;
     371        1925 :   Q[l-1] = P[l-1];
     372        8869 :   for (i=l-2; i>=2; i--)
     373             :   {
     374        8869 :     gel(Q,i) = gmul(gel(P,i), hi);
     375        8869 :     if (i == 2) break;
     376        6944 :     hi = gmul(hi,h);
     377             :   }
     378        1925 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      123954 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      123954 :   long i,id, dy, dx = degpol(x0);
     387      123954 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       68743 :   dy = dx/d;
     389       68743 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       68743 :   z = y + 2;
     391       68743 :   x = x0+ 2;
     392       68743 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       68743 :   return y;
     394             : }
     395             : 
     396             : /* return x0(X^d) */
     397             : GEN
     398      397261 : RgX_inflate(GEN x0, long d)
     399             : {
     400      397261 :   long i, id, dy, dx = degpol(x0);
     401      397262 :   GEN x = x0 + 2, z, y;
     402      397262 :   if (dx <= 0) return leafcopy(x0);
     403      394819 :   dy = dx*d;
     404      394819 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     405      394818 :   z = y + 2;
     406      394818 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     407      394818 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     408      394818 :   return y;
     409             : }
     410             : 
     411             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     412             : GEN
     413     1181508 : RgX_translate(GEN P, GEN c)
     414             : {
     415     1181508 :   pari_sp av = avma;
     416             :   GEN Q, *R;
     417             :   long i, k, n;
     418             : 
     419     1181508 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     420     1179965 :   Q = leafcopy(P);
     421     1179965 :   R = (GEN*)(Q+2); n = degpol(P);
     422     1179965 :   if (equali1(c))
     423             :   {
     424        8652 :     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     1178026 :   else if (equalim1(c))
     435             :   {
     436      141813 :     for (i=1; i<=n; i++)
     437             :     {
     438      120547 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     439      120547 :       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     3980832 :     for (i=1; i<=n; i++)
     449             :     {
     450     2824072 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     451     2824072 :       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     1179965 :   return gerepilecopy(av, Q);
     459             : }
     460             : 
     461             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     462             : GEN
     463      381992 : ZX_translate(GEN P, GEN c)
     464             : {
     465      381992 :   pari_sp av = avma;
     466             :   GEN Q, *R;
     467             :   long i, k, n;
     468             : 
     469      381992 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     470      381957 :   Q = leafcopy(P);
     471      381957 :   R = (GEN*)(Q+2); n = degpol(P);
     472      381957 :   if (equali1(c))
     473             :   {
     474     2401940 :     for (i=1; i<=n; i++)
     475             :     {
     476     2123403 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     477     2123403 :       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      103420 :   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      773487 :     for (i=1; i<=n; i++)
     499             :     {
     500      670088 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], mulii(c, R[k+1]));
     501      670088 :       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      381957 :   return gerepilecopy(av, Q);
     509             : }
     510             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     511             : GEN
     512        6146 : RgXQX_translate(GEN P, GEN c, GEN T)
     513             : {
     514        6146 :   pari_sp av = avma;
     515             :   GEN Q, *R;
     516             :   long i, k, n;
     517             : 
     518        6146 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     519        6125 :   Q = leafcopy(P);
     520        6125 :   R = (GEN*)(Q+2); n = degpol(P);
     521       34993 :   for (i=1; i<=n; i++)
     522             :   {
     523      141876 :     for (k=n-i; k<n; k++)
     524             :     {
     525      113008 :       pari_sp av2 = avma;
     526      113008 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     527             :     }
     528       28868 :     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        6125 :   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       23825 : QXQ_to_mod_copy(GEN x, GEN T)
     547             : {
     548             :   long d;
     549       23825 :   switch(typ(x))
     550             :   {
     551        9485 :     case t_INT:  return icopy(x);
     552         805 :     case t_FRAC: return gcopy(x);
     553             :     case t_POL:
     554       13535 :       d = degpol(x);
     555       13535 :       if (d < 0) return gen_0;
     556       12996 :       if (d == 0) return gcopy(gel(x,2));
     557       12625 :       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      914169 : QXQ_to_mod(GEN x, GEN T)
     565             : {
     566             :   long d;
     567      914169 :   switch(typ(x))
     568             :   {
     569             :     case t_INT:
     570      401310 :     case t_FRAC: return x;
     571             :     case t_POL:
     572      512859 :       d = degpol(x);
     573      512859 :       if (d < 0) return gen_0;
     574      385501 :       if (d == 0) return gel(x,2);
     575      368577 :       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        3087 : QXQX_to_mod(GEN z, GEN T)
     584             : {
     585        3087 :   long i,l = lg(z);
     586        3087 :   GEN x = cgetg(l,t_POL);
     587        3087 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     588        3087 :   x[1] = z[1]; return normalizepol_lg(x,l);
     589             : }
     590             : /* pure shallow version */
     591             : GEN
     592      122132 : QXQX_to_mod_shallow(GEN z, GEN T)
     593             : {
     594      122132 :   long i,l = lg(z);
     595      122132 :   GEN x = cgetg(l,t_POL);
     596      122132 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     597      122132 :   x[1] = z[1]; return normalizepol_lg(x,l);
     598             : }
     599             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     600             : GEN
     601        1176 : QXQXV_to_mod(GEN V, GEN T)
     602             : {
     603        1176 :   long i, l = lg(V);
     604        1176 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     605        1176 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     606        1176 :   return z;
     607             : }
     608             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     609             : GEN
     610        4880 : QXQV_to_mod(GEN V, GEN T)
     611             : {
     612        4880 :   long i, l = lg(V);
     613        4880 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     614        4880 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     615        4880 :   return z;
     616             : }
     617             : 
     618             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     619             : GEN
     620       17976 : QXQC_to_mod_shallow(GEN V, GEN T)
     621             : {
     622       17976 :   long i, l = lg(V);
     623       17976 :   GEN z = cgetg(l, t_COL);
     624       17976 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     625       17976 :   return z;
     626             : }
     627             : 
     628             : GEN
     629        6090 : QXQM_to_mod_shallow(GEN V, GEN T)
     630             : {
     631        6090 :   long i, l = lg(V);
     632        6090 :   GEN z = cgetg(l, t_MAT);
     633        6090 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     634        6090 :   return z;
     635             : }
     636             : 
     637             : GEN
     638      763309 : RgX_renormalize_lg(GEN x, long lx)
     639             : {
     640             :   long i;
     641     2073344 :   for (i = lx-1; i>1; i--)
     642     1937060 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     643      763309 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     644      763309 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     645             : }
     646             : 
     647             : GEN
     648      456903 : RgV_to_RgX(GEN x, long v)
     649             : {
     650      456903 :   long i, k = lg(x);
     651             :   GEN p;
     652             : 
     653      456903 :   while (--k && gequal0(gel(x,k)));
     654      456907 :   if (!k) return pol_0(v);
     655      451958 :   i = k+2; p = cgetg(i,t_POL);
     656      451958 :   p[1] = evalsigne(1) | evalvarn(v);
     657      451958 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     658      451958 :   return p;
     659             : }
     660             : GEN
     661      159639 : RgV_to_RgX_reverse(GEN x, long v)
     662             : {
     663      159639 :   long j, k, l = lg(x);
     664             :   GEN p;
     665             : 
     666      159639 :   for (k = 1; k < l; k++)
     667      159639 :     if (!gequal0(gel(x,k))) break;
     668      159639 :   if (k == l) return pol_0(v);
     669      159639 :   k -= 1;
     670      159639 :   l -= k;
     671      159639 :   x += k;
     672      159639 :   p = cgetg(l+1,t_POL);
     673      159639 :   p[1] = evalsigne(1) | evalvarn(v);
     674      159639 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     675      159639 :   return p;
     676             : }
     677             : 
     678             : /* return the (N-dimensional) vector of coeffs of p */
     679             : GEN
     680     6617176 : RgX_to_RgC(GEN x, long N)
     681             : {
     682             :   long i, l;
     683             :   GEN z;
     684     6617176 :   l = lg(x)-1; x++;
     685     6617176 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     686     6617176 :   z = cgetg(N+1,t_COL);
     687     6617176 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     688     6617176 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     689     6617176 :   return z;
     690             : }
     691             : GEN
     692      679516 : Rg_to_RgC(GEN x, long N)
     693             : {
     694      679516 :   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       48104 : RgM_to_RgXV(GEN x, long v)
     700       48104 : { 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      103712 : RgV_to_RgM(GEN x, long n)
     706      103712 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     707             : 
     708             : GEN
     709        3765 : RgXV_to_RgM(GEN x, long n)
     710        3765 : { 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       16352 : RgM_to_RgXX(GEN x, long v,long w)
     715             : {
     716       16352 :   long j, lx = lg(x);
     717       16352 :   GEN y = cgetg(lx+1, t_POL);
     718       16352 :   y[1] = evalsigne(1) | evalvarn(v);
     719       16352 :   y++;
     720       16352 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     721       16352 :   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       17172 : RgXY_swapspec(GEN x, long n, long w, long nx)
     738             : {
     739       17172 :   long j, ly = n+3;
     740       17172 :   GEN y = cgetg(ly, t_POL);
     741       17172 :   y[1] = evalsigne(1);
     742      226670 :   for (j=2; j<ly; j++)
     743             :   {
     744             :     long k;
     745      209498 :     GEN a = cgetg(nx+2,t_POL);
     746      209498 :     a[1] = evalsigne(1) | evalvarn(w);
     747     1091032 :     for (k=0; k<nx; k++)
     748             :     {
     749      881534 :       GEN xk = gel(x,k);
     750      881534 :       if (typ(xk)==t_POL)
     751      808006 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     752             :       else
     753       73528 :         gel(a,k+2) = j==2 ? xk: gen_0;
     754             :     }
     755      209498 :     gel(y,j) = normalizepol_lg(a, nx+2);
     756             :   }
     757       17172 :   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          92 : RgXY_degreex(GEN b)
     770             : {
     771          92 :   long deg = -1, i;
     772          92 :   if (!signe(b)) return -1;
     773         584 :   for (i = 2; i < lg(b); ++i)
     774             :   {
     775         492 :     GEN bi = gel(b, i);
     776         492 :     if (typ(bi) == t_POL)
     777         470 :       deg = maxss(deg, degpol(bi));
     778             :   }
     779          92 :   return deg;
     780             : }
     781             : 
     782             : /* return (x % X^n). Shallow */
     783             : GEN
     784     1575470 : RgXn_red_shallow(GEN a, long n)
     785             : {
     786     1575470 :   long i, L = n+2, l = lg(a);
     787             :   GEN  b;
     788     1575470 :   if (L >= l) return a; /* deg(x) < n */
     789     1534345 :   b = cgetg(L, t_POL); b[1] = a[1];
     790     1534345 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     791     1534345 :   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    59267130 : RgX_shift_shallow(GEN a, long n)
     801             : {
     802    59267130 :   long i, l = lg(a);
     803             :   GEN  b;
     804    59267130 :   if (l == 2 || !n) return a;
     805    43452363 :   l += n;
     806    43452363 :   if (n < 0)
     807             :   {
     808    37759116 :     if (l <= 2) return pol_0(varn(a));
     809    37739663 :     b = cgetg(l, t_POL); b[1] = a[1];
     810    37739663 :     a -= n;
     811    37739663 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     812             :   } else {
     813     5693247 :     b = cgetg(l, t_POL); b[1] = a[1];
     814     5693247 :     a -= n; n += 2;
     815     5693247 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     816     5693247 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     817             :   }
     818    43432910 :   return b;
     819             : }
     820             : /* return (x * X^n). */
     821             : GEN
     822     3443654 : RgX_shift(GEN a, long n)
     823             : {
     824     3443654 :   long i, l = lg(a);
     825             :   GEN  b;
     826     3443654 :   if (l == 2 || !n) return RgX_copy(a);
     827     3443388 :   l += n;
     828     3443388 :   if (n < 0)
     829             :   {
     830         805 :     if (l <= 2) return pol_0(varn(a));
     831         763 :     b = cgetg(l, t_POL); b[1] = a[1];
     832         763 :     a -= n;
     833         763 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     834             :   } else {
     835     3442583 :     b = cgetg(l, t_POL); b[1] = a[1];
     836     3442583 :     a -= n; n += 2;
     837     3442583 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     838     3442583 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     839             :   }
     840     3443346 :   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     2859839 : RgX_mulXn(GEN x, long d)
     861             : {
     862             :   pari_sp av;
     863             :   GEN z;
     864             :   long v;
     865     2859839 :   if (d >= 0) return RgX_shift(x, d);
     866     1367403 :   d = -d;
     867     1367403 :   v = RgX_val(x);
     868     1367403 :   if (v >= d) return RgX_shift(x, -d);
     869     1367396 :   av = avma;
     870     1367396 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     871     1367396 :   return gerepileupto(av, z);
     872             : }
     873             : 
     874             : long
     875     2219123 : RgX_val(GEN x)
     876             : {
     877     2219123 :   long i, lx = lg(x);
     878     2219123 :   if (lx == 2) return LONG_MAX;
     879     2251337 :   for (i = 2; i < lx; i++)
     880     2251323 :     if (!isexactzero(gel(x,i))) break;
     881     2219081 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     882     2219067 :   return i - 2;
     883             : }
     884             : long
     885    43170809 : RgX_valrem(GEN x, GEN *Z)
     886             : {
     887    43170809 :   long v, i, lx = lg(x);
     888    43170809 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     889    82699263 :   for (i = 2; i < lx; i++)
     890    82699263 :     if (!isexactzero(gel(x,i))) break;
     891             :   /* possible with non-rational zeros */
     892    43170809 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     893    43170809 :   v = i - 2;
     894    43170809 :   *Z = RgX_shift_shallow(x, -v);
     895    43170809 :   return v;
     896             : }
     897             : long
     898       14220 : RgX_valrem_inexact(GEN x, GEN *Z)
     899             : {
     900             :   long v;
     901       14220 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     902       14584 :   for (v = 0;; v++)
     903       14584 :     if (!gequal0(gel(x,2+v))) break;
     904         371 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     905       14213 :   return v;
     906             : }
     907             : 
     908             : GEN
     909       84602 : RgXQC_red(GEN x, GEN T)
     910       84602 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
     911             : 
     912             : GEN
     913         903 : RgXQV_red(GEN x, GEN T)
     914         903 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
     915             : 
     916             : GEN
     917       12040 : RgXQM_red(GEN x, GEN T)
     918       12040 : { 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      345068 : RgXQX_red(GEN P, GEN T)
     928             : {
     929      345068 :   long i, l = lg(P);
     930      345068 :   GEN Q = cgetg(l, t_POL);
     931      345068 :   Q[1] = P[1];
     932      345068 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     933      345068 :   return normalizepol_lg(Q, l);
     934             : }
     935             : 
     936             : GEN
     937      235411 : RgX_deriv(GEN x)
     938             : {
     939      235411 :   long i,lx = lg(x)-1;
     940             :   GEN y;
     941             : 
     942      235411 :   if (lx<3) return pol_0(varn(x));
     943      207740 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     944      207740 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     945      207740 :   y[1] = x[1]; return normalizepol_lg(y,i);
     946             : }
     947             : 
     948             : GEN
     949      492744 : RgX_recipspec_shallow(GEN x, long l, long n)
     950             : {
     951             :   long i;
     952      492744 :   GEN z=cgetg(n+2,t_POL)+2;
     953    15908301 :   for(i=0; i<l; i++)
     954    15415556 :     gel(z,n-i-1) = gel(x,i);
     955      619756 :   for(   ; i<n; i++)
     956      127011 :     gel(z, n-i-1) = gen_0;
     957      492745 :   return normalizepol_lg(z-2,n+2);
     958             : }
     959             : 
     960             : GEN
     961       40712 : RgXn_recip_shallow(GEN P, long n)
     962             : {
     963       40712 :   GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
     964       40712 :   setvarn(Q, varn(P));
     965       40712 :   return Q;
     966             : }
     967             : 
     968             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     969             : GEN
     970        2093 : RgX_recip(GEN x)
     971             : {
     972             :   long lx, i, j;
     973        2093 :   GEN y = cgetg_copy(x, &lx);
     974        2093 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     975        2093 :   return normalizepol_lg(y,lx);
     976             : }
     977             : /* shallow version */
     978             : GEN
     979      374459 : RgX_recip_shallow(GEN x)
     980             : {
     981             :   long lx, i, j;
     982      374459 :   GEN y = cgetg_copy(x, &lx);
     983      374470 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     984      374470 :   return y;
     985             : }
     986             : /*******************************************************************/
     987             : /*                                                                 */
     988             : /*                      ADDITION / SUBTRACTION                     */
     989             : /*                                                                 */
     990             : /*******************************************************************/
     991             : /* same variable */
     992             : GEN
     993    17221642 : RgX_add(GEN x, GEN y)
     994             : {
     995    17221642 :   long i, lx = lg(x), ly = lg(y);
     996             :   GEN z;
     997    17221642 :   if (ly <= lx) {
     998    15483405 :     z = cgetg(lx,t_POL); z[1] = x[1];
     999    15483405 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1000    15483405 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1001    15483405 :     z = normalizepol_lg(z, lx);
    1002             :   } else {
    1003     1738237 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1004     1738237 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1005     1738237 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1006     1738237 :     z = normalizepol_lg(z, ly);
    1007             :   }
    1008    17221642 :   return z;
    1009             : }
    1010             : GEN
    1011    12364417 : RgX_sub(GEN x, GEN y)
    1012             : {
    1013    12364417 :   long i, lx = lg(x), ly = lg(y);
    1014             :   GEN z;
    1015    12364417 :   if (ly <= lx) {
    1016     9440786 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1017     9440786 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1018     9440786 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1019     9440786 :     z = normalizepol_lg(z, lx);
    1020             :   } else {
    1021     2923631 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1022     2923631 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1023     2923631 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1024     2923631 :     z = normalizepol_lg(z, ly);
    1025             :   }
    1026    12364417 :   return z;
    1027             : }
    1028             : GEN
    1029     1062859 : RgX_neg(GEN x)
    1030             : {
    1031     1062859 :   long i, lx = lg(x);
    1032     1062859 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1033     1062859 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1034     1062859 :   return y;
    1035             : }
    1036             : 
    1037             : GEN
    1038    11699271 : RgX_Rg_add(GEN y, GEN x)
    1039             : {
    1040             :   GEN z;
    1041    11699271 :   long lz = lg(y), i;
    1042    11699271 :   if (lz == 2) return scalarpol(x,varn(y));
    1043     9884756 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1044     9884756 :   gel(z,2) = gadd(gel(y,2),x);
    1045     9884756 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1046             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1047             :    * an inexact 0 */
    1048     9884756 :   return normalizepol_lg(z,lz);
    1049             : }
    1050             : GEN
    1051        2548 : RgX_Rg_add_shallow(GEN y, GEN x)
    1052             : {
    1053             :   GEN z;
    1054        2548 :   long lz = lg(y), i;
    1055        2548 :   if (lz == 2) return scalarpol(x,varn(y));
    1056        2548 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1057        2548 :   gel(z,2) = gadd(gel(y,2),x);
    1058        2548 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1059        2548 :   return z = normalizepol_lg(z,lz);
    1060             : }
    1061             : GEN
    1062       33389 : RgX_Rg_sub(GEN y, GEN x)
    1063             : {
    1064             :   GEN z;
    1065       33389 :   long lz = lg(y), i;
    1066       33389 :   if (lz == 2)
    1067             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1068        3864 :     long v = varn(y);
    1069        3864 :     if (isrationalzero(x)) return pol_0(v);
    1070          14 :     z = cgetg(3,t_POL);
    1071          28 :     z[1] = gequal0(x)? evalvarn(v)
    1072          14 :                    : evalvarn(v) | evalsigne(1);
    1073          14 :     gel(z,2) = gneg(x); return z;
    1074             :   }
    1075       29525 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1076       29525 :   gel(z,2) = gsub(gel(y,2),x);
    1077       29525 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1078       29525 :   return z = normalizepol_lg(z,lz);
    1079             : }
    1080             : GEN
    1081      464290 : Rg_RgX_sub(GEN x, GEN y)
    1082             : {
    1083             :   GEN z;
    1084      464290 :   long lz = lg(y), i;
    1085      464290 :   if (lz == 2) return scalarpol(x,varn(y));
    1086      463275 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1087      463275 :   gel(z,2) = gsub(x, gel(y,2));
    1088      463275 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1089      463275 :   return z = normalizepol_lg(z,lz);
    1090             : }
    1091             : /*******************************************************************/
    1092             : /*                                                                 */
    1093             : /*                  KARATSUBA MULTIPLICATION                       */
    1094             : /*                                                                 */
    1095             : /*******************************************************************/
    1096             : #if 0
    1097             : /* to debug Karatsuba-like routines */
    1098             : GEN
    1099             : zx_debug_spec(GEN x, long nx)
    1100             : {
    1101             :   GEN z = cgetg(nx+2,t_POL);
    1102             :   long i;
    1103             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1104             :   z[1] = evalsigne(1); return z;
    1105             : }
    1106             : 
    1107             : GEN
    1108             : RgX_debug_spec(GEN x, long nx)
    1109             : {
    1110             :   GEN z = cgetg(nx+2,t_POL);
    1111             :   long i;
    1112             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1113             :   z[1] = evalsigne(1); return z;
    1114             : }
    1115             : #endif
    1116             : 
    1117             : /* generic multiplication */
    1118             : GEN
    1119     3062835 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1120             : {
    1121             :   GEN z, t;
    1122             :   long i;
    1123     3062835 :   if (nx == ny) {
    1124      688913 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1125      688913 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1126      688913 :     return normalizepol_lg(z, nx+2);
    1127             :   }
    1128     2373922 :   if (ny < nx) {
    1129     2230342 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1130     2230342 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1131     2230342 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1132     2230342 :     return normalizepol_lg(z, nx+2);
    1133             :   } else {
    1134      143580 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1135      143580 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1136      143580 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1137      143580 :     return normalizepol_lg(z, ny+2);
    1138             :   }
    1139             : }
    1140             : GEN
    1141      185372 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1142             : {
    1143             :   GEN z, t;
    1144             :   long i;
    1145      185372 :   if (nx == ny) {
    1146        2162 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1147        2162 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1148        2162 :     return normalizepol_lg(z, nx+2);
    1149             :   }
    1150      183210 :   if (ny < nx) {
    1151      182818 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1152      182818 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1153      182818 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1154      182818 :     return normalizepol_lg(z, nx+2);
    1155             :   } else {
    1156         392 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1157         392 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1158         392 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1159         392 :     return normalizepol_lg(z, ny+2);
    1160             :   }
    1161             : }
    1162             : 
    1163             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1164             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1165             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1166             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1167             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1168             : static GEN
    1169    20193601 : RgXspec_kill0(GEN x, long lx)
    1170             : {
    1171    20193601 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1172             :   long i;
    1173    77848872 :   for (i=0; i <lx; i++)
    1174             :   {
    1175    57655271 :     GEN c = gel(x,i);
    1176    57655271 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1177             :   }
    1178    20193601 :   return z;
    1179             : }
    1180             : 
    1181             : INLINE GEN
    1182    43330623 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1183             : {
    1184    43330623 :   pari_sp av = avma;
    1185    43330623 :   GEN s = NULL;
    1186             :   long i;
    1187             : 
    1188   163190580 :   for (i=a; i<b; i++)
    1189   119859957 :     if (gel(y,i) && gel(x,-i))
    1190             :     {
    1191    71555835 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1192    71555835 :       s = s? gadd(s, t): t;
    1193             :     }
    1194    43330623 :   return s? gerepileupto(av, s): gen_0;
    1195             : }
    1196             : 
    1197             : /* assume nx >= ny > 0, return x * y * t^v */
    1198             : static GEN
    1199     8240145 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1200             : {
    1201             :   long i, lz, nz;
    1202             :   GEN z;
    1203             : 
    1204     8240145 :   x = RgXspec_kill0(x,nx);
    1205     8240145 :   y = RgXspec_kill0(y,ny);
    1206     8240145 :   lz = nx + ny + 1; nz = lz-2;
    1207     8240145 :   lz += v;
    1208     8240145 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1209     8240145 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1210     8240145 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1211     8240145 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1212     8240145 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1213     8240145 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1214             : }
    1215             : 
    1216             : /* return (x * X^d) + y. Assume d > 0 */
    1217             : GEN
    1218     2186751 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1219             : {
    1220             :   GEN x, y, xd, yd, zd;
    1221             :   long a, lz, nx, ny;
    1222             : 
    1223     2186751 :   if (!signe(x0)) return y0;
    1224     2166253 :   ny = lgpol(y0);
    1225     2166253 :   nx = lgpol(x0);
    1226     2166253 :   zd = (GEN)avma;
    1227     2166253 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1228     2166253 :   if (a <= 0)
    1229             :   {
    1230      182751 :     lz = nx+d+2;
    1231      182751 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1232      182751 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1233      182751 :     x = zd + a;
    1234      182751 :     while (zd > x) gel(--zd,0) = gen_0;
    1235             :   }
    1236             :   else
    1237             :   {
    1238     1983502 :     xd = new_chunk(d); yd = y+d;
    1239     1983502 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1240     1983502 :     lz = (a>nx)? ny+2: lg(x)+d;
    1241     1983502 :     x += 2; while (xd > x) *--zd = *--xd;
    1242             :   }
    1243     2166253 :   while (yd > y) *--zd = *--yd;
    1244     2166253 :   *--zd = x0[1];
    1245     2166253 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1246             : }
    1247             : GEN
    1248      475918 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1249             : {
    1250             :   GEN x, y, xd, yd, zd;
    1251             :   long a, lz, nx, ny;
    1252             : 
    1253      475918 :   if (!signe(x0)) return RgX_copy(y0);
    1254      475733 :   nx = lgpol(x0);
    1255      475733 :   ny = lgpol(y0);
    1256      475733 :   zd = (GEN)avma;
    1257      475733 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1258      475733 :   if (a <= 0)
    1259             :   {
    1260      290361 :     lz = nx+d+2;
    1261      290361 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1262      290361 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1263      290361 :     x = zd + a;
    1264      290361 :     while (zd > x) gel(--zd,0) = gen_0;
    1265             :   }
    1266             :   else
    1267             :   {
    1268      185372 :     xd = new_chunk(d); yd = y+d;
    1269      185372 :     x = RgX_addspec(x,yd, nx,a);
    1270      185372 :     lz = (a>nx)? ny+2: lg(x)+d;
    1271      185372 :     x += 2; while (xd > x) *--zd = *--xd;
    1272             :   }
    1273      475733 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1274      475733 :   *--zd = x0[1];
    1275      475733 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1276             : }
    1277             : 
    1278             : /* return x * y mod t^n */
    1279             : static GEN
    1280     1853752 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1281             : {
    1282     1853752 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1283             :   GEN z;
    1284     1853752 :   if (lx < 0) return pol_0(varn(x));
    1285     1853752 :   if (ly < 0) return pol_0(varn(x));
    1286     1853752 :   z = cgetg(lz, t_POL) + 2;
    1287     1853752 :   x+=2; if (lx > n) lx = n;
    1288     1853752 :   y+=2; if (ly > n) ly = n;
    1289     1853752 :   z[-1] = x[-1];
    1290     1853752 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1291     1853752 :   x = RgXspec_kill0(x, lx);
    1292     1853752 :   y = RgXspec_kill0(y, ly);
    1293             :   /* x:y:z [i] = term of degree i */
    1294     1853752 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1295     1853752 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1296     1853752 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1297     1853752 :   return normalizepol_lg(z - 2, lz);
    1298             : }
    1299             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1300             : static GEN
    1301     1912062 : RgXn_mul2(GEN f, GEN g, long n)
    1302             : {
    1303     1912062 :   pari_sp av = avma;
    1304             :   GEN fe,fo, ge,go, l,h,m;
    1305             :   long n0, n1;
    1306     1912062 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1307     1860654 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1308        6902 :   n0 = n>>1; n1 = n-n0;
    1309        6902 :   RgX_even_odd(f, &fe, &fo);
    1310        6902 :   RgX_even_odd(g, &ge, &go);
    1311        6902 :   l = RgXn_mul(fe,ge,n1);
    1312        6902 :   h = RgXn_mul(fo,go,n0);
    1313        6902 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1314             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1315             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1316        6902 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1317             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1318        6902 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1319        6902 :   m = RgX_inflate(m,2);
    1320             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1321        6902 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1322        6902 :   h = RgX_inflate(h,2);
    1323        6902 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1324        6902 :   return gerepileupto(av, h);
    1325             : }
    1326             : /* (f*g) \/ x^n */
    1327             : static GEN
    1328        1700 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1329             : {
    1330        1700 :   long d = degpol(f)+degpol(g) + 1 - n;
    1331             :   GEN h;
    1332        1700 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1333        1207 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1334             :                                  RgX_recip_shallow(g), d));
    1335        1207 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1336             : }
    1337             : 
    1338             : /* (f*g) \/ x^n */
    1339             : static GEN
    1340           0 : RgX_sqrhigh_i2(GEN f, long n)
    1341             : {
    1342           0 :   long d = 2*degpol(f)+ 1 - n;
    1343             :   GEN h;
    1344           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1345           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1346           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1347             : }
    1348             : 
    1349             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1350             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1351             :  * Only c, c0, c1, c2 are genuine GEN.
    1352             :  */
    1353             : GEN
    1354     8774359 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1355             : {
    1356             :   GEN a0, c, c0;
    1357     8774359 :   long n0, n0a, i, v = 0;
    1358             :   pari_sp av;
    1359             : 
    1360     8774359 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1361     8774359 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1362     8774359 :   if (na < nb) swapspec(a,b, na,nb);
    1363     8774359 :   if (!nb) return pol_0(0);
    1364             : 
    1365     8709122 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1366      468977 :   RgX_shift_inplace_init(v);
    1367      468977 :   i = (na>>1); n0 = na-i; na = i;
    1368      468977 :   av = avma; a0 = a+n0; n0a = n0;
    1369      468977 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1370             : 
    1371      468977 :   if (nb > n0)
    1372             :   {
    1373             :     GEN b0,c1,c2;
    1374             :     long n0b;
    1375             : 
    1376      467675 :     nb -= n0; b0 = b+n0; n0b = n0;
    1377      467675 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1378      467675 :     c = RgX_mulspec(a,b,n0a,n0b);
    1379      467675 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1380             : 
    1381      467675 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1382      467675 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1383             : 
    1384      467675 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1385      467675 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1386      467675 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1387             :   }
    1388             :   else
    1389             :   {
    1390        1302 :     c = RgX_mulspec(a,b,n0a,nb);
    1391        1302 :     c0 = RgX_mulspec(a0,b,na,nb);
    1392             :   }
    1393      468977 :   c0 = RgX_addmulXn(c0,c,n0);
    1394      468977 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1395             : }
    1396             : 
    1397             : INLINE GEN
    1398       32595 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1399             : {
    1400       32595 :   pari_sp av = avma;
    1401       32595 :   GEN s = NULL;
    1402       32595 :   long j, l = (i+1)>>1;
    1403       96859 :   for (j=a; j<l; j++)
    1404             :   {
    1405       64264 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1406       64264 :     if (xj && xx)
    1407             :     {
    1408       56970 :       GEN t = gmul(xj, xx);
    1409       56970 :       s = s? gadd(s, t): t;
    1410             :     }
    1411             :   }
    1412       32595 :   if (s) s = gshift(s,1);
    1413       32595 :   if ((i&1) == 0)
    1414             :   {
    1415       19201 :     GEN t = gel(x, i>>1);
    1416       19201 :     if (t) {
    1417       16891 :       t = gsqr(t);
    1418       16891 :       s = s? gadd(s, t): t;
    1419             :     }
    1420             :   }
    1421       32595 :   return s? gerepileupto(av,s): gen_0;
    1422             : }
    1423             : static GEN
    1424        5807 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1425             : {
    1426             :   long i, lz, nz;
    1427             :   GEN z;
    1428             : 
    1429        5807 :   if (!nx) return pol_0(0);
    1430        5807 :   x = RgXspec_kill0(x,nx);
    1431        5807 :   lz = (nx << 1) + 1, nz = lz-2;
    1432        5807 :   lz += v;
    1433        5807 :   z = cgetg(lz,t_POL) + 2;
    1434        5807 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1435        5807 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1436        5807 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1437        5807 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1438             : }
    1439             : /* return x^2 mod t^n */
    1440             : static GEN
    1441           0 : RgXn_sqr_basecase(GEN x, long n)
    1442             : {
    1443           0 :   long i, lz = n+2, lx = lgpol(x);
    1444             :   GEN z;
    1445           0 :   if (lx < 0) return pol_0(varn(x));
    1446           0 :   z = cgetg(lz, t_POL);
    1447           0 :   z[1] = x[1];
    1448           0 :   x+=2; if (lx > n) lx = n;
    1449           0 :   x = RgXspec_kill0(x,lx);
    1450           0 :   z+=2;/* x:z [i] = term of degree i */
    1451           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1452           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1453           0 :   z -= 2; return normalizepol_lg(z, lz);
    1454             : }
    1455             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1456             : static GEN
    1457         238 : RgXn_sqr2(GEN f, long n)
    1458             : {
    1459         238 :   pari_sp av = avma;
    1460             :   GEN fe,fo, l,h,m;
    1461             :   long n0, n1;
    1462         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1463           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1464           0 :   n0 = n>>1; n1 = n-n0;
    1465           0 :   RgX_even_odd(f, &fe, &fo);
    1466           0 :   l = RgXn_sqr(fe,n1);
    1467           0 :   h = RgXn_sqr(fo,n0);
    1468           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1469             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1470             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1471           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1472             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1473           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1474           0 :   m = RgX_inflate(m,2);
    1475             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1476           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1477           0 :   h = RgX_inflate(h,2);
    1478           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1479           0 :   return gerepileupto(av, h);
    1480             : }
    1481             : GEN
    1482        5846 : RgX_sqrspec(GEN a, long na)
    1483             : {
    1484             :   GEN a0, c, c0, c1;
    1485        5846 :   long n0, n0a, i, v = 0;
    1486             :   pari_sp av;
    1487             : 
    1488        5846 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1489        5846 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1490          39 :   RgX_shift_inplace_init(v);
    1491          39 :   i = (na>>1); n0 = na-i; na = i;
    1492          39 :   av = avma; a0 = a+n0; n0a = n0;
    1493          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1494             : 
    1495          39 :   c = RgX_sqrspec(a,n0a);
    1496          39 :   c0 = RgX_sqrspec(a0,na);
    1497          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1498          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1499          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1500          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1501             : }
    1502             : 
    1503             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1504             : GEN
    1505      634070 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1506             : {
    1507      634070 :   GEN z = RgX_mul(A, B);
    1508      634070 :   if (a < b)
    1509        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1510      628624 :   else if (a > b)
    1511      377964 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1512             :   else
    1513      250660 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1514      634070 :   return z;
    1515             : }
    1516             : 
    1517             : GEN
    1518     7368691 : RgX_mul_i(GEN x, GEN y)
    1519             : {
    1520     7368691 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1521     7368691 :   setvarn(z, varn(x)); return z;
    1522             : }
    1523             : 
    1524             : GEN
    1525        5768 : RgX_sqr_i(GEN x)
    1526             : {
    1527        5768 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1528        5768 :   setvarn(z,varn(x)); return z;
    1529             : }
    1530             : 
    1531             : /*******************************************************************/
    1532             : /*                                                                 */
    1533             : /*                               DIVISION                          */
    1534             : /*                                                                 */
    1535             : /*******************************************************************/
    1536             : GEN
    1537      413411 : RgX_Rg_divexact(GEN x, GEN y) {
    1538             :   long i, lx;
    1539             :   GEN z;
    1540      413411 :   if (typ(y) == t_INT && is_pm1(y))
    1541       27830 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1542      385581 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1543      385581 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1544      385581 :   return z;
    1545             : }
    1546             : GEN
    1547    22618559 : RgX_Rg_div(GEN x, GEN y) {
    1548             :   long i, lx;
    1549    22618559 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1550    22618559 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1551    22618559 :   return normalizepol_lg(z, lx);
    1552             : }
    1553             : GEN
    1554       11585 : RgX_normalize(GEN x)
    1555             : {
    1556       11585 :   GEN z, d = NULL;
    1557       11585 :   long i, n = lg(x)-1;
    1558       11585 :   for (i = n; i > 1; i--) { d = gel(x,i); if (!gequal0(d)) break; }
    1559       11585 :   if (i == 1) return pol_0(varn(x));
    1560       11585 :   if (i == n && isint1(d)) return x;
    1561        5026 :   n = i; z = cgetg(n+1, t_POL); z[1] = x[1];
    1562        5026 :   for (i=2; i<n; i++) gel(z,i) = gdiv(gel(x,i),d);
    1563        5026 :   gel(z,n) = Rg_get_1(d); return z;
    1564             : }
    1565             : GEN
    1566        2457 : RgX_divs(GEN x, long y) {
    1567             :   long i, lx;
    1568        2457 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1569        2457 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1570        2457 :   return normalizepol_lg(z, lx);
    1571             : }
    1572             : GEN
    1573       40724 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1574             : {
    1575       40724 :   long l = lg(a), i;
    1576       40724 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1577       40724 :   z[1] = a[1];
    1578       40724 :   a0 = a + l-1;
    1579       40724 :   z0 = z + l-2; *z0 = *a0--;
    1580      785130 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1581             :   {
    1582      744406 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1583      744406 :     gel(z0,0) = t;
    1584             :   }
    1585       40724 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1586       40724 :   return z;
    1587             : }
    1588             : /* Polynomial division x / y:
    1589             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1590             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1591             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1592             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1593             : static GEN
    1594    11902843 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1595             : {
    1596             :   pari_sp avy, av, av1;
    1597             :   long dx,dy,dz,i,j,sx,lr;
    1598             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1599             :   GEN (*f)(GEN,GEN);
    1600             : 
    1601    11902843 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1602             : 
    1603    11902843 :   dy = degpol(y);
    1604    11902843 :   y_lead = gel(y,dy+2);
    1605    11902843 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1606             :   {
    1607           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1608           0 :     for (dy--; dy>=0; dy--)
    1609             :     {
    1610           0 :       y_lead = gel(y,dy+2);
    1611           0 :       if (!gequal0(y_lead)) break;
    1612             :     }
    1613             :   }
    1614    11902843 :   if (!dy) /* y is constant */
    1615             :   {
    1616       15481 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1617       15481 :     z = RgX_Rg_div(x, y_lead);
    1618       15481 :     if (pr == ONLY_DIVIDES) return z;
    1619       13829 :     if (pr) *pr = pol_0(varn(x));
    1620       13829 :     return z;
    1621             :   }
    1622    11887362 :   dx = degpol(x);
    1623    11887362 :   if (dx < dy)
    1624             :   {
    1625      601819 :     if (pr == ONLY_REM) return RgX_copy(x);
    1626      339337 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1627      339316 :     z = pol_0(varn(x));
    1628      339316 :     if (pr) *pr = RgX_copy(x);
    1629      339316 :     return z;
    1630             :   }
    1631             : 
    1632             :   /* x,y in R[X], y non constant */
    1633    11285543 :   av = avma;
    1634    11285543 :   p = NULL;
    1635    11285543 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1636             :   {
    1637      112952 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1638      112952 :     if (!z) { avma = av; return NULL; }
    1639      112952 :     z = FpX_to_mod(z, p);
    1640      112952 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1641       60879 :       return gerepileupto(av, z);
    1642       52073 :     *pr = FpX_to_mod(*pr, p);
    1643       52073 :     gerepileall(av, 2, pr, &z);
    1644       52073 :     return z;
    1645             :   }
    1646    11172591 :   switch(typ(y_lead))
    1647             :   {
    1648             :     case t_REAL:
    1649           0 :       y_lead = ginv(y_lead);
    1650           0 :       f = gmul; mod = NULL;
    1651           0 :       break;
    1652             :     case t_INTMOD:
    1653        1336 :     case t_POLMOD: y_lead = ginv(y_lead);
    1654        1336 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1655        1336 :       break;
    1656    11171255 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1657    11171255 :       f = gdiv; mod = NULL;
    1658             :   }
    1659             : 
    1660    11172591 :   if (y_lead == NULL)
    1661     9397818 :     p2 = gel(x,dx+2);
    1662             :   else {
    1663             :     for(;;) {
    1664     1774773 :       p2 = f(gel(x,dx+2),y_lead);
    1665     1774773 :       p2 = simplify_shallow(p2);
    1666     1774773 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1667           0 :     }
    1668     1774773 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1669             :     {
    1670           0 :       if (pr == ONLY_DIVIDES) {
    1671           0 :         avma = av;
    1672           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1673             :       }
    1674           0 :       if (pr == ONLY_REM)
    1675             :       {
    1676           0 :         if (dx < 0)
    1677           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1678             :         else
    1679             :         {
    1680             :           GEN t;
    1681           0 :           avma = av;
    1682           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1683           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1684           0 :           return t;
    1685             :         }
    1686             :       }
    1687           0 :       if (pr) /* cf ONLY_REM above */
    1688             :       {
    1689           0 :         if (dx < 0)
    1690             :         {
    1691           0 :           p2 = gclone(p2);
    1692           0 :           avma = av;
    1693           0 :           z = pol_0(varn(x));
    1694           0 :           x = scalarpol(p2, varn(x));
    1695           0 :           gunclone(p2);
    1696             :         }
    1697             :         else
    1698             :         {
    1699             :           GEN t;
    1700           0 :           avma = av;
    1701           0 :           z = pol_0(varn(x));
    1702           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1703           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1704           0 :           x = t;
    1705             :         }
    1706           0 :         *pr = x;
    1707             :       }
    1708             :       else
    1709             :       {
    1710           0 :         avma = av;
    1711           0 :         z = pol_0(varn(x));
    1712             :       }
    1713           0 :       return z;
    1714             :     }
    1715             :   }
    1716             :   /* dx >= dy */
    1717    11172591 :   avy = avma;
    1718    11172591 :   dz = dx-dy;
    1719    11172591 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1720    11172591 :   x += 2;
    1721    11172591 :   z += 2;
    1722    11172591 :   y += 2;
    1723    11172591 :   gel(z,dz) = gcopy(p2);
    1724             : 
    1725    27421808 :   for (i=dx-1; i>=dy; i--)
    1726             :   {
    1727    16249217 :     av1=avma; p1=gel(x,i);
    1728    16249217 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1729    16249217 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1730             : 
    1731    16249217 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1732             :     else
    1733     6783519 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1734    16249217 :     gel(z,i-dy) = p1;
    1735             :   }
    1736    11172591 :   if (!pr) return gerepileupto(av,z-2);
    1737             : 
    1738     4695047 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1739     5032328 :   for (sx=0; ; i--)
    1740             :   {
    1741     5032328 :     p1 = gel(x,i);
    1742             :     /* we always enter this loop at least once */
    1743     5032328 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1744     5032328 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1745     5032328 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1746     3403787 :     if (!isexactzero(p1)) break;
    1747     3390944 :     if (!i) break;
    1748      337281 :     avma=av1;
    1749      337281 :   }
    1750     4695047 :   if (pr == ONLY_DIVIDES)
    1751             :   {
    1752        1344 :     if (sx) { avma=av; return NULL; }
    1753        1337 :     avma = (pari_sp)rem;
    1754        1337 :     return gerepileupto(av,z-2);
    1755             :   }
    1756     4693703 :   lr=i+3; rem -= lr;
    1757     4693703 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1758     4631603 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1759     4693703 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1760     4693703 :   rem[1] = z[-1];
    1761     4693703 :   rem += 2;
    1762     4693703 :   gel(rem,i) = p1;
    1763     5797073 :   for (i--; i>=0; i--)
    1764             :   {
    1765     1103370 :     av1=avma; p1 = gel(x,i);
    1766     1103370 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1767     1103370 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1768     1103370 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1769             :   }
    1770     4693703 :   rem -= 2;
    1771     4693703 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1772     4693703 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1773     4038489 :   z -= 2;
    1774             :   {
    1775     4038489 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1776     4038489 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1777             :   }
    1778             : }
    1779             : 
    1780             : GEN
    1781    10985147 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1782             : {
    1783    10985147 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1784    10985147 :   return RgX_divrem_i(x, y, pr);
    1785             : }
    1786             : 
    1787             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1788             : GEN
    1789       57951 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1790             : {
    1791             :   long vx, dx, dy, dz, i, j, sx, lr;
    1792             :   pari_sp av0, av, tetpil;
    1793             :   GEN z,p1,rem,lead;
    1794             : 
    1795       57951 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1796       57951 :   vx = varn(x);
    1797       57951 :   dx = degpol(x);
    1798       57951 :   dy = degpol(y);
    1799       57951 :   if (dx < dy)
    1800             :   {
    1801       15904 :     if (pr)
    1802             :     {
    1803       15904 :       av0 = avma; x = RgXQX_red(x, T);
    1804       15904 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1805       15904 :       if (pr == ONLY_REM) return x;
    1806           0 :       *pr = x;
    1807             :     }
    1808           0 :     return pol_0(vx);
    1809             :   }
    1810       42047 :   lead = leading_coeff(y);
    1811       42047 :   if (!dy) /* y is constant */
    1812             :   {
    1813         546 :     if (pr && pr != ONLY_DIVIDES)
    1814             :     {
    1815           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1816           0 :       *pr = pol_0(vx);
    1817             :     }
    1818         546 :     if (gequal1(lead)) return RgX_copy(x);
    1819           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1820           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1821             :   }
    1822       41501 :   av0 = avma; dz = dx-dy;
    1823       41501 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1824       41501 :   avma = av0;
    1825       41501 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1826       41501 :   x += 2; y += 2; z += 2;
    1827             : 
    1828       41501 :   p1 = gel(x,dx); av = avma;
    1829       41501 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1830      274465 :   for (i=dx-1; i>=dy; i--)
    1831             :   {
    1832      232964 :     av=avma; p1=gel(x,i);
    1833      232964 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1834      232964 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1835      232964 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1836             :   }
    1837       41501 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1838             : 
    1839       40255 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1840       66140 :   for (sx=0; ; i--)
    1841             :   {
    1842       66140 :     p1 = gel(x,i);
    1843       66140 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1844       66140 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1845       30577 :     if (!i) break;
    1846       25885 :     avma=av;
    1847       25885 :   }
    1848       40255 :   if (pr == ONLY_DIVIDES)
    1849             :   {
    1850        2242 :     if (lead) gunclone(lead);
    1851        2242 :     if (sx) { avma=av0; return NULL; }
    1852        2172 :     avma = (pari_sp)rem; return z-2;
    1853             :   }
    1854       38013 :   lr=i+3; rem -= lr;
    1855       38013 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1856       38013 :   rem[1] = z[-1];
    1857       38013 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1858       38013 :   rem += 2; gel(rem,i) = p1;
    1859      106861 :   for (i--; i>=0; i--)
    1860             :   {
    1861       68848 :     av=avma; p1 = gel(x,i);
    1862      260479 :     for (j=0; j<=i && j<=dz; j++)
    1863      191631 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1864       68848 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1865             :   }
    1866       38013 :   rem -= 2;
    1867       38013 :   if (lead) gunclone(lead);
    1868       38013 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1869       38013 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1870          91 :   *pr = rem; return z-2;
    1871             : }
    1872             : 
    1873             : /*******************************************************************/
    1874             : /*                                                                 */
    1875             : /*                        PSEUDO-DIVISION                          */
    1876             : /*                                                                 */
    1877             : /*******************************************************************/
    1878             : INLINE GEN
    1879      446676 : rem(GEN c, GEN T)
    1880             : {
    1881      446676 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1882      446676 :   return c;
    1883             : }
    1884             : 
    1885             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1886             : int
    1887        5968 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1888             : {
    1889             :   long dx, dy, dz, i, p, T_ismonic;
    1890        5968 :   pari_sp av = avma, av2;
    1891             :   GEN y_lead;
    1892             : 
    1893        5968 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1894        5968 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1895        5968 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1896             :   /* if monic, no point in using pseudo-division */
    1897        5968 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1898        3595 :   T_ismonic = gequal1(leading_coeff(T));
    1899        3595 :   dx = degpol(x);
    1900        3595 :   if (dx < dy) return !signe(x);
    1901        3595 :   (void)new_chunk(2);
    1902        3595 :   x = RgX_recip_shallow(x)+2;
    1903        3595 :   y = RgX_recip_shallow(y)+2;
    1904             :   /* pay attention to sparse divisors */
    1905        7372 :   for (i = 1; i <= dy; i++)
    1906        3777 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1907        3595 :   dz = dx-dy; p = dz+1;
    1908        3595 :   av2 = avma;
    1909             :   for (;;)
    1910             :   {
    1911       32353 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1912       32353 :     x0 = gneg(x0); p--;
    1913       32353 :     m = gcdii(cx, y0);
    1914       32353 :     if (!equali1(m))
    1915             :     {
    1916       31303 :       x0 = gdiv(x0, m);
    1917       31303 :       y0 = diviiexact(y0, m);
    1918       31303 :       if (equali1(y0)) y0 = NULL;
    1919             :     }
    1920       65896 :     for (i=1; i<=dy; i++)
    1921             :     {
    1922       33543 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1923       33543 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1924       33543 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1925       33543 :       gel(x,i) = c;
    1926             :     }
    1927      383422 :     for (   ; i<=dx; i++)
    1928             :     {
    1929      351069 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1930      351069 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1931      351069 :       gel(x,i) = c;
    1932             :     }
    1933       36039 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1934       32353 :     if (dx < dy) break;
    1935       28758 :     if (gc_needed(av2,1))
    1936             :     {
    1937           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1938           0 :       gerepilecoeffs(av2,x,dx+1);
    1939             :     }
    1940       28758 :   }
    1941        3595 :   avma = av; return (dx < 0);
    1942             : }
    1943             : 
    1944             : /* T either NULL or a t_POL. */
    1945             : GEN
    1946       25115 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1947             : {
    1948       25115 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1949       25115 :   pari_sp av = avma, av2;
    1950             :   GEN y_lead;
    1951             : 
    1952       25115 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1953       25115 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1954             :   /* if monic, no point in using pseudo-division */
    1955       25115 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1956       21510 :   dx = degpol(x);
    1957       21510 :   if (dx < dy) return RgX_copy(x);
    1958       21510 :   (void)new_chunk(2);
    1959       21510 :   x = RgX_recip_shallow(x)+2;
    1960       21510 :   y = RgX_recip_shallow(y)+2;
    1961             :   /* pay attention to sparse divisors */
    1962       66754 :   for (i = 1; i <= dy; i++)
    1963       45244 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1964       21510 :   dz = dx-dy; p = dz+1;
    1965       21510 :   av2 = avma;
    1966             :   for (;;)
    1967             :   {
    1968       81394 :     gel(x,0) = gneg(gel(x,0)); p--;
    1969      258846 :     for (i=1; i<=dy; i++)
    1970             :     {
    1971      177452 :       GEN c = gmul(y_lead, gel(x,i));
    1972      177452 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1973      177452 :       gel(x,i) = rem(c, T);
    1974             :     }
    1975      285957 :     for (   ; i<=dx; i++)
    1976             :     {
    1977      204563 :       GEN c = gmul(y_lead, gel(x,i));
    1978      204563 :       gel(x,i) = rem(c, T);
    1979             :     }
    1980       88905 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1981       81394 :     if (dx < dy) break;
    1982       59884 :     if (gc_needed(av2,1))
    1983             :     {
    1984           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1985           0 :       gerepilecoeffs(av2,x,dx+1);
    1986             :     }
    1987       59884 :   }
    1988       21510 :   if (dx < 0) return pol_0(vx);
    1989       19473 :   lx = dx+3; x -= 2;
    1990       19473 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1991       19473 :   x[1] = evalsigne(1) | evalvarn(vx);
    1992       19473 :   x = RgX_recip_shallow(x);
    1993       19473 :   if (p)
    1994             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1995        1155 :     GEN t = y_lead;
    1996        1155 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1997           0 :       t = RgXQ_powu(t, p, T);
    1998             :     else
    1999        1155 :       t = gpowgs(t, p);
    2000        3934 :     for (i=2; i<lx; i++)
    2001             :     {
    2002        2779 :       GEN c = gmul(gel(x,i), t);
    2003        2779 :       gel(x,i) = rem(c,T);
    2004             :     }
    2005        1155 :     if (!T) return gerepileupto(av, x);
    2006             :   }
    2007       18318 :   return gerepilecopy(av, x);
    2008             : }
    2009             : 
    2010             : GEN
    2011       25115 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2012             : 
    2013             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2014             : GEN
    2015        5340 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2016             : {
    2017        5340 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2018        5340 :   pari_sp av = avma, av2;
    2019             :   GEN z, r, ypow, y_lead;
    2020             : 
    2021        5340 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2022        5340 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2023        5340 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2024        4787 :   dx = degpol(x);
    2025        4787 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2026        4787 :   if (dx == dy)
    2027             :   {
    2028          28 :     GEN x_lead = gel(x,lg(x)-1);
    2029          28 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2030          28 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2031          28 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2032          28 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2033             :   }
    2034        4759 :   (void)new_chunk(2);
    2035        4759 :   x = RgX_recip_shallow(x)+2;
    2036        4759 :   y = RgX_recip_shallow(y)+2;
    2037             :   /* pay attention to sparse divisors */
    2038       23503 :   for (i = 1; i <= dy; i++)
    2039       18744 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2040        4759 :   dz = dx-dy; p = dz+1;
    2041        4759 :   lz = dz+3;
    2042        4759 :   z = cgetg(lz, t_POL);
    2043        4759 :   z[1] = evalsigne(1) | evalvarn(vx);
    2044        4759 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2045        4759 :   ypow = new_chunk(dz+1);
    2046        4759 :   gel(ypow,0) = gen_1;
    2047        4759 :   gel(ypow,1) = y_lead;
    2048        8035 :   for (i=2; i<=dz; i++)
    2049             :   {
    2050        3276 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2051        3276 :     gel(ypow,i) = rem(c,T);
    2052             :   }
    2053        4759 :   av2 = avma;
    2054        4759 :   for (iz=2;;)
    2055             :   {
    2056       10090 :     p--;
    2057       10090 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2058       44559 :     for (i=1; i<=dy; i++)
    2059             :     {
    2060       34469 :       GEN c = gmul(y_lead, gel(x,i));
    2061       34469 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2062       34469 :       gel(x,i) = rem(c, T);
    2063             :     }
    2064       24137 :     for (   ; i<=dx; i++)
    2065             :     {
    2066       14047 :       GEN c = gmul(y_lead, gel(x,i));
    2067       14047 :       gel(x,i) = rem(c,T);
    2068             :     }
    2069       10090 :     x++; dx--;
    2070       10090 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2071       10090 :     if (dx < dy) break;
    2072        5331 :     if (gc_needed(av2,1))
    2073             :     {
    2074           0 :       GEN X = x-2;
    2075           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2076           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2077           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2078             :     }
    2079        5331 :   }
    2080        4759 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2081        4759 :   if (dx < 0)
    2082          98 :     x = pol_0(vx);
    2083             :   else
    2084             :   {
    2085        4661 :     lx = dx+3; x -= 2;
    2086        4661 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2087        4661 :     x[1] = evalsigne(1) | evalvarn(vx);
    2088        4661 :     x = RgX_recip_shallow(x);
    2089             :   }
    2090        4759 :   z = RgX_recip_shallow(z);
    2091        4759 :   r = x;
    2092        4759 :   if (p)
    2093             :   {
    2094        1674 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2095        1674 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2096             :   }
    2097        4759 :   gerepileall(av, 2, &z, &r);
    2098        4759 :   *ptr = r; return z;
    2099             : }
    2100             : GEN
    2101        5172 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2102        5172 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2103             : 
    2104             : GEN
    2105       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2106             : {
    2107       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2108             : }
    2109             : GEN
    2110    67948582 : RgX_Rg_mul(GEN y, GEN x) {
    2111             :   long i, ly;
    2112    67948582 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2113    67948582 :   if (ly == 2) return z;
    2114    67880143 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2115    67880136 :   return normalizepol_lg(z,ly);
    2116             : }
    2117             : GEN
    2118       14217 : RgX_muls(GEN y, long x) {
    2119             :   long i, ly;
    2120       14217 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2121       14217 :   if (ly == 2) return z;
    2122       14182 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2123       14182 :   return normalizepol_lg(z,ly);
    2124             : }
    2125             : GEN
    2126        4585 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2127             : {
    2128        4585 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2129             : }
    2130             : GEN
    2131          77 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2132             : {
    2133          77 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2134             : }
    2135             : 
    2136             : GEN
    2137        1792 : RgXQX_sqr(GEN x, GEN T)
    2138             : {
    2139        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2140             : }
    2141             : 
    2142             : GEN
    2143         448 : RgXQX_powers(GEN P, long n, GEN T)
    2144             : {
    2145         448 :   GEN v = cgetg(n+2, t_VEC);
    2146             :   long i;
    2147         448 :   gel(v, 1) = pol_1(varn(T));
    2148         448 :   if (n==0) return v;
    2149         448 :   gel(v, 2) = gcopy(P);
    2150         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2151         448 :   return v;
    2152             : }
    2153             : 
    2154             : static GEN
    2155       65695 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2156             : static GEN
    2157           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2158             : static GEN
    2159      251581 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2160             : static GEN
    2161      103067 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2162             : static GEN
    2163      111559 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2164             : static GEN
    2165      107014 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2166             : static GEN
    2167         105 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2168             : static GEN
    2169       71155 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2170             : 
    2171             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2172             :               _mul, _sqr, _one, _zero };
    2173             : 
    2174             : GEN
    2175           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2176             : {
    2177           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2178             : }
    2179             : 
    2180             : GEN
    2181       45248 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2182             : {
    2183       45248 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2184       45248 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2185             : }
    2186             : 
    2187             : /* mod X^n */
    2188             : struct modXn {
    2189             :   long v; /* varn(X) */
    2190             :   long n;
    2191             : } ;
    2192             : static GEN
    2193        1897 : _sqrXn(void *data, GEN x) {
    2194        1897 :   struct modXn *S = (struct modXn*)data;
    2195        1897 :   return RgXn_sqr(x, S->n);
    2196             : }
    2197             : static GEN
    2198        1218 : _mulXn(void *data, GEN x, GEN y) {
    2199        1218 :   struct modXn *S = (struct modXn*)data;
    2200        1218 :   return RgXn_mul(x,y, S->n);
    2201             : }
    2202             : static GEN
    2203        1477 : _oneXn(void *data) {
    2204        1477 :   struct modXn *S = (struct modXn*)data;
    2205        1477 :   return pol_1(S->v);
    2206             : }
    2207             : static GEN
    2208           0 : _zeroXn(void *data) {
    2209           0 :   struct modXn *S = (struct modXn*)data;
    2210           0 :   return pol_0(S->v);
    2211             : }
    2212             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2213             :                                           _oneXn, _zeroXn };
    2214             : 
    2215             : GEN
    2216         357 : RgXn_powers(GEN x, long m, long n)
    2217             : {
    2218         357 :   long d = degpol(x);
    2219         357 :   int use_sqr = (d<<1) >= n;
    2220             :   struct modXn S;
    2221         357 :   S.v = varn(x); S.n = n;
    2222         357 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2223             : }
    2224             : 
    2225             : GEN
    2226        1617 : RgXn_powu_i(GEN x, ulong m, long n)
    2227             : {
    2228             :   struct modXn S;
    2229        1617 :   S.v = varn(x); S.n = n;
    2230        1617 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2231             : }
    2232             : GEN
    2233           0 : RgXn_powu(GEN x, ulong m, long n)
    2234             : {
    2235             :   struct modXn S;
    2236           0 :   S.v = varn(x); S.n = n;
    2237           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2238             : }
    2239             : 
    2240             : GEN
    2241         714 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2242             : {
    2243             :   struct modXn S;
    2244         714 :   S.v = varn(gel(x,2)); S.n = n;
    2245         714 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2246             : }
    2247             : 
    2248             : GEN
    2249           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2250             : {
    2251           0 :   int use_sqr = 2*degpol(x) >= n;
    2252             :   struct modXn S;
    2253           0 :   S.v = varn(x); S.n = n;
    2254           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2255             : }
    2256             : 
    2257             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2258             : GEN
    2259        6713 : RgXn_eval(GEN Q, GEN x, long n)
    2260             : {
    2261        6713 :   long d = degpol(x);
    2262             :   int use_sqr;
    2263             :   struct modXn S;
    2264        6713 :   if (d == 1 && isrationalzero(gel(x,2)))
    2265             :   {
    2266        6706 :     GEN y = RgX_unscale(Q, gel(x,3));
    2267        6706 :     setvarn(y, varn(x)); return y;
    2268             :   }
    2269           7 :   S.v = varn(x);
    2270           7 :   S.n = n;
    2271           7 :   use_sqr = (d<<1) >= n;
    2272           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2273             : }
    2274             : 
    2275             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2276             : static GEN
    2277       36000 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2278             : {
    2279       36000 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2280       36000 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2281             : }
    2282             : 
    2283             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2284             : static GEN
    2285           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2286             : {
    2287           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2288           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2289             : }
    2290             : 
    2291             : GEN
    2292      126497 : RgXn_inv_i(GEN f, long e)
    2293             : {
    2294             :   pari_sp av;
    2295             :   ulong mask;
    2296             :   GEN W, a;
    2297      126497 :   long v = varn(f), n = 1;
    2298             : 
    2299      126497 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2300      126497 :   a = ginv(gel(f,2));
    2301      126497 :   if (e == 1) return scalarpol(a, v);
    2302      126483 :   else if (e == 2)
    2303             :   {
    2304             :     GEN b;
    2305      112056 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2306       92610 :     av = avma; b = gneg(b);
    2307       92610 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2308       92610 :     W = deg1pol_shallow(b, a, v);
    2309       92610 :     return gcopy(W);
    2310             :   }
    2311       14427 :   W = scalarpol_shallow(ginv(gel(f,2)),v);
    2312       14427 :   mask = quadratic_prec_mask(e);
    2313       14427 :   av = avma;
    2314       64854 :   for (;mask>1;)
    2315             :   {
    2316             :     GEN u, fr;
    2317       36000 :     long n2 = n;
    2318       36000 :     n<<=1; if (mask & 1) n--;
    2319       36000 :     mask >>= 1;
    2320       36000 :     fr = RgXn_red_shallow(f, n);
    2321       36000 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2322       36000 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2323       36000 :     if (gc_needed(av,2))
    2324             :     {
    2325           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2326           0 :       W = gerepileupto(av, W);
    2327             :     }
    2328             :   }
    2329       14427 :   return W;
    2330             : }
    2331             : GEN
    2332       81032 : RgXn_inv(GEN f, long e)
    2333       81032 : { pari_sp av = avma; return gerepileupto(av, RgXn_inv_i(f,e)); }
    2334             : 
    2335             : GEN
    2336       12810 : RgXn_exp(GEN h, long e)
    2337             : {
    2338       12810 :   pari_sp av = avma, av2;
    2339       12810 :   long v = varn(h), n=1;
    2340       12810 :   GEN f = pol_1(v), g = pol_1(v);
    2341       12810 :   ulong mask = quadratic_prec_mask(e);
    2342       12810 :   av2 = avma;
    2343       12810 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2344           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2345       39606 :   for (;mask>1;)
    2346             :   {
    2347             :     GEN q, w;
    2348       13986 :     long n2 = n;
    2349       13986 :     n<<=1; if (mask & 1) n--;
    2350       13986 :     mask >>= 1;
    2351       13986 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2352       13986 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2353       13986 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2354       13986 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2355       13986 :     if (gc_needed(av2,2))
    2356             :     {
    2357           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2358           0 :       gerepileall(av2, 2, &f, &g);
    2359             :     }
    2360             :   }
    2361       12810 :   return gerepileupto(av, f);
    2362             : }
    2363             : 
    2364             : GEN
    2365          91 : RgXn_reverse(GEN f, long e)
    2366             : {
    2367          91 :   pari_sp av = avma, av2;
    2368             :   ulong mask;
    2369             :   GEN fi, a, df, W, an;
    2370          91 :   long v = varn(f), n=1;
    2371          91 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2372           0 :     pari_err_INV("serreverse",f);
    2373          91 :   fi = ginv(gel(f,3));
    2374          91 :   a = deg1pol_shallow(fi,gen_0,v);
    2375          91 :   if (e <= 2) return gerepilecopy(av, a);
    2376          91 :   W = scalarpol(fi,v);
    2377          91 :   df = RgX_deriv(f);
    2378          91 :   mask = quadratic_prec_mask(e);
    2379          91 :   av2 = avma;
    2380         539 :   for (;mask>1;)
    2381             :   {
    2382             :     GEN u, fa, fr;
    2383         357 :     long n2 = n, rt;
    2384         357 :     n<<=1; if (mask & 1) n--;
    2385         357 :     mask >>= 1;
    2386         357 :     fr = RgXn_red_shallow(f, n);
    2387         357 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2388         357 :     an = RgXn_powers(a, rt, n);
    2389         357 :     if (n>1)
    2390             :     {
    2391         357 :       long n4 = (n2+1)>>1;
    2392         357 :       GEN dfr = RgXn_red_shallow(df, n2);
    2393         357 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2394         357 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2395         357 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2396             :     }
    2397         357 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2398         357 :     fa = RgX_shift(fa, -n2);
    2399         357 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2400         357 :     if (gc_needed(av2,2))
    2401             :     {
    2402           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2403           0 :       gerepileall(av2, 2, &a, &W);
    2404             :     }
    2405             :   }
    2406          91 :   return gerepileupto(av, a);
    2407             : }
    2408             : 
    2409             : GEN
    2410           0 : RgXn_sqrt(GEN h, long e)
    2411             : {
    2412           0 :   pari_sp av = avma, av2;
    2413           0 :   long v = varn(h), n = 1;
    2414           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2415           0 :   ulong mask = quadratic_prec_mask(e);
    2416           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2417           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2418           0 :   av2 = avma;
    2419             :   while(1)
    2420             :   {
    2421           0 :     long n2 = n, m;
    2422             :     GEN g;
    2423           0 :     n<<=1; if (mask & 1) n--;
    2424           0 :     mask >>= 1;
    2425           0 :     m = n-n2;
    2426           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2427           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2428           0 :     if (mask==1) return gerepileupto(av, f);
    2429           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2430           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2431           0 :     if (gc_needed(av2,2))
    2432             :     {
    2433           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2434           0 :       gerepileall(av2, 2, &f, &df);
    2435             :     }
    2436           0 :   }
    2437             : }
    2438             : 
    2439             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2440             : GEN
    2441      206949 : RgXQ_powu(GEN x, ulong n, GEN T)
    2442             : {
    2443             :   pari_sp av;
    2444             :   GEN y;
    2445             : 
    2446      206949 :   if (!n) return pol_1(varn(x));
    2447      205339 :   if (n == 1) return RgX_copy(x);
    2448      141172 :   av = avma;
    2449      141172 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2450      141168 :   return gerepileupto(av, y);
    2451             : }
    2452             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2453             : GEN
    2454       18681 : RgXQ_pow(GEN x, GEN n, GEN T)
    2455             : {
    2456             :   pari_sp av;
    2457       18681 :   long s = signe(n);
    2458             :   GEN y;
    2459             : 
    2460       18681 :   if (!s) return pol_1(varn(x));
    2461       18681 :   if (is_pm1(n) == 1)
    2462           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2463       18681 :   av = avma;
    2464       18681 :   if (s < 0) x = RgXQ_inv(x, T);
    2465       18681 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2466       18681 :   return gerepileupto(av, y);
    2467             : }
    2468             : 
    2469             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2470             : GEN
    2471        3036 : RgXQ_powers(GEN x, long l, GEN T)
    2472             : {
    2473        3036 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2474        3036 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2475             : }
    2476             : 
    2477             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2478             : GEN
    2479        2091 : QXQ_powers(GEN a, long n, GEN T)
    2480             : {
    2481        2091 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2482             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2483        2091 :   if (den)
    2484             :   { /* restore denominators */
    2485        1371 :     GEN d = den;
    2486             :     long i;
    2487        1371 :     gel(v,2) = a;
    2488        4099 :     for (i=3; i<=n+1; i++) {
    2489        2728 :       d = mulii(d,den);
    2490        2728 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2491             :     }
    2492             :   }
    2493        2091 :   return v;
    2494             : }
    2495             : 
    2496             : static GEN
    2497        1295 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2498             : {
    2499        1295 :   long l, i, m = 0;
    2500             :   GEN dz, z;
    2501        1295 :   GEN V = cgetg_copy(v, &l);
    2502        4151 :   for (i = imin; i < l; i++)
    2503             :   {
    2504        2856 :     GEN c = gel(v, i);
    2505        2856 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2506             :   }
    2507        1295 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2508        1295 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2509        4151 :   for (i = imin; i < l; i++)
    2510             :   {
    2511        2856 :     GEN c = gel(v,i);
    2512        2856 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2513        2856 :     gel(V,i) = c;
    2514             :   }
    2515        1295 :   return V;
    2516             : }
    2517             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2518             : GEN
    2519        1232 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2520        1232 : { return do_QXQ_eval(v, 1, a, T); }
    2521             : GEN
    2522          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2523          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2524             : 
    2525             : GEN
    2526         287 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2527             : {
    2528         287 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2529             : }
    2530             : 
    2531             : GEN
    2532       34748 : RgXQ_norm(GEN x, GEN T)
    2533             : {
    2534             :   pari_sp av;
    2535       34748 :   long dx = degpol(x);
    2536             :   GEN L, y;
    2537             : 
    2538       34748 :   av = avma; y = resultant(T, x);
    2539       34748 :   L = leading_coeff(T);
    2540       34748 :   if (gequal1(L) || !signe(x)) return y;
    2541           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2542             : }
    2543             : 
    2544             : GEN
    2545      106112 : RgX_blocks(GEN P, long n, long m)
    2546             : {
    2547      106112 :   GEN z = cgetg(m+1,t_VEC);
    2548      106112 :   long i,j, k=2, l = lg(P);
    2549      511508 :   for(i=1; i<=m; i++)
    2550             :   {
    2551      405396 :     GEN zi = cgetg(n+2,t_POL);
    2552      405396 :     zi[1] = P[1];
    2553      405396 :     gel(z,i) = zi;
    2554     2431201 :     for(j=2; j<n+2; j++)
    2555     2025805 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2556      405396 :     zi = RgX_renormalize_lg(zi, n+2);
    2557             :   }
    2558      106112 :   return z;
    2559             : }
    2560             : 
    2561             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2562             : void
    2563       40195 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2564             : {
    2565       40195 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2566             :   GEN p0, p1;
    2567             : 
    2568       80390 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2569             : 
    2570       40181 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2571       40181 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2572       40181 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2573     1687310 :   for (i=0; i<n1; i++)
    2574             :   {
    2575     1647129 :     p0[2+i] = p[2+(i<<1)];
    2576     1647129 :     p1[2+i] = p[3+(i<<1)];
    2577             :   }
    2578       40181 :   if (n1 != n0)
    2579       26133 :     p0[2+i] = p[2+(i<<1)];
    2580       40181 :   *pe = normalizepol(p0);
    2581       40181 :   *po = normalizepol(p1);
    2582             : }
    2583             : 
    2584             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2585             : GEN
    2586       40670 : RgX_splitting(GEN p, long k)
    2587             : {
    2588       40670 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2589             :   GEN r;
    2590             : 
    2591       40670 :   m = n/k;
    2592       40670 :   r = cgetg(k+1,t_VEC);
    2593      224154 :   for(i=1; i<=k; i++)
    2594             :   {
    2595      183484 :     gel(r,i) = cgetg(m+3, t_POL);
    2596      183484 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2597             :   }
    2598      553245 :   for (j=1, i=0, l=2; i<=n; i++)
    2599             :   {
    2600      512575 :     gmael(r,j,l) = gel(p,2+i);
    2601      512575 :     if (j==k) { j=1; l++; } else j++;
    2602             :   }
    2603      224154 :   for(i=1; i<=k; i++)
    2604      183484 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2605       40670 :   return r;
    2606             : }
    2607             : 
    2608             : /*******************************************************************/
    2609             : /*                                                                 */
    2610             : /*                        Kronecker form                           */
    2611             : /*                                                                 */
    2612             : /*******************************************************************/
    2613             : 
    2614             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2615             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2616             :  * normalized coefficients */
    2617             : GEN
    2618      123976 : Kronecker_to_mod(GEN z, GEN T)
    2619             : {
    2620      123976 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2621      123976 :   GEN x, t = cgetg(N,t_POL);
    2622      123976 :   t[1] = T[1];
    2623      123976 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2624      123976 :   x[1] = z[1];
    2625      123976 :   T = RgX_copy(T);
    2626     1158271 :   for (i=2; i<lx+2; i++, z+= N-2)
    2627             :   {
    2628     1034295 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2629     1034295 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2630             :   }
    2631      123976 :   N = (l-2) % (N-2) + 2;
    2632      123976 :   for (j=2; j<N; j++) t[j] = z[j];
    2633      123976 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2634      123976 :   return normalizepol_lg(x, i+1);
    2635             : }
    2636             : 
    2637             : /*******************************************************************/
    2638             : /*                                                                 */
    2639             : /*                        Domain detection                         */
    2640             : /*                                                                 */
    2641             : /*******************************************************************/
    2642             : 
    2643             : static GEN
    2644      210127 : zero_FpX_mod(GEN p, long v)
    2645             : {
    2646      210127 :   GEN r = cgetg(3,t_POL);
    2647      210127 :   r[1] = evalvarn(v);
    2648      210127 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2649      210127 :   return r;
    2650             : }
    2651             : 
    2652             : static GEN
    2653      341067 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2654             : {
    2655      341067 :   pari_sp av = avma;
    2656             :   GEN r;
    2657      341067 :   if (lgefint(p) == 3)
    2658             :   {
    2659      312688 :     ulong pp = uel(p, 2);
    2660      312688 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2661             :                                   RgX_to_Flx(y, pp), pp));
    2662             :   }
    2663             :   else
    2664       28379 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2665      341067 :   if (signe(r)==0)
    2666       98841 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2667      242226 :   return gerepileupto(av, FpX_to_mod(r, p));
    2668             : }
    2669             : 
    2670             : static GEN
    2671           7 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2672             : {
    2673           7 :   GEN r = cgetg(3,t_POL);
    2674           7 :   r[1] = evalvarn(v);
    2675           7 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2676           7 :   return r;
    2677             : }
    2678             : 
    2679             : static GEN
    2680          56 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2681             : {
    2682          56 :   pari_sp av = avma;
    2683          56 :   GEN T = RgX_to_FpX(pol, p);
    2684          56 :   long dT = degpol(T);
    2685          56 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2686          56 :   GEN ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2687          56 :   GEN r = FpX_mul(kx, ky, p);
    2688          56 :   if (signe(r)==0)
    2689           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2690          56 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2691             : }
    2692             : 
    2693             : static GEN
    2694      307569 : RgX_liftred(GEN x, GEN T)
    2695      307569 : { return RgXQX_red(liftpol_shallow(x), T); }
    2696             : 
    2697             : static GEN
    2698      119992 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2699             : {
    2700      119992 :   pari_sp av = avma;
    2701      119992 :   long dT = degpol(T);
    2702      119992 :   GEN r = QX_mul(ZXX_to_Kronecker(RgX_liftred(x, T), dT),
    2703             :                  ZXX_to_Kronecker(RgX_liftred(y, T), dT));
    2704      119992 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2705             : }
    2706             : 
    2707             : static GEN
    2708        1260 : RgX_sqr_FpX(GEN x, GEN p)
    2709             : {
    2710        1260 :   pari_sp av = avma;
    2711             :   GEN r;
    2712        1260 :   if (lgefint(p) == 3)
    2713             :   {
    2714        1056 :     ulong pp = uel(p, 2);
    2715        1056 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2716             :   }
    2717             :   else
    2718         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2719        1260 :   if (signe(r)==0)
    2720          98 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2721        1162 :   return gerepileupto(av, FpX_to_mod(r, p));
    2722             : }
    2723             : 
    2724             : static GEN
    2725         147 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2726             : {
    2727         147 :   pari_sp av = avma;
    2728         147 :   GEN T = RgX_to_FpX(pol, p);
    2729         147 :   long dT = degpol(T);
    2730         147 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2731         147 :   GEN r = FpX_sqr(kx, p);
    2732         147 :   if (signe(r)==0)
    2733           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2734         147 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2735             : }
    2736             : 
    2737             : static GEN
    2738        3781 : RgX_sqr_QXQX(GEN x, GEN T)
    2739             : {
    2740        3781 :   pari_sp av = avma;
    2741        3781 :   long dT = degpol(T);
    2742        3781 :   GEN r = QX_sqr(ZXX_to_Kronecker(RgX_liftred(x, T), dT));
    2743        3781 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2744             : }
    2745             : 
    2746             : static GEN
    2747      348880 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2748             : {
    2749      348880 :   pari_sp av = avma;
    2750             :   GEN r;
    2751      348880 :   if (lgefint(p) == 3)
    2752             :   {
    2753      340249 :     ulong pp = uel(p, 2);
    2754      340249 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2755             :                                   RgX_to_Flx(y, pp), pp));
    2756             :   }
    2757             :   else
    2758        8631 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2759      348880 :   if (signe(r)==0)
    2760      111188 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2761      237692 :   return gerepileupto(av, FpX_to_mod(r, p));
    2762             : }
    2763             : 
    2764             : static GEN
    2765       31902 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    2766             : {
    2767       31902 :   pari_sp av = avma;
    2768             :   GEN r;
    2769       31902 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    2770       31902 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    2771             : }
    2772             : static GEN
    2773          35 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2774             : {
    2775          35 :   pari_sp av = avma;
    2776             :   GEN r;
    2777          35 :   GEN T = RgX_to_FpX(pol, p);
    2778          35 :   if (lgefint(p) == 3)
    2779             :   {
    2780          35 :     ulong pp = uel(p, 2);
    2781          35 :     GEN Tp = ZX_to_Flx(T, pp);
    2782          35 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2783             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2784             :   }
    2785             :   else
    2786           0 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2787          35 :   if (signe(r)==0)
    2788           7 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2789          28 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2790             : }
    2791             : 
    2792             : #define code(t1,t2) ((t1 << 6) | t2)
    2793             : static GEN
    2794    41048049 : RgX_mul_fast(GEN x, GEN y)
    2795             : {
    2796             :   GEN p, pol;
    2797             :   long pa;
    2798    41048049 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2799    41048030 :   switch(t)
    2800             :   {
    2801    30298499 :     case t_INT:    return ZX_mul(x,y);
    2802      904224 :     case t_FRAC:   return QX_mul(x,y);
    2803      101739 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2804      341067 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2805             :     case code(t_POLMOD, t_INT):
    2806             :     case code(t_POLMOD, t_FRAC):
    2807      119992 :                    return RgX_mul_QXQX(x, y, pol);
    2808             :     case code(t_POLMOD, t_INTMOD):
    2809          56 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2810     9282453 :     default:       return NULL;
    2811             :   }
    2812             : }
    2813             : static GEN
    2814     2553998 : RgX_sqr_fast(GEN x)
    2815             : {
    2816             :   GEN p, pol;
    2817             :   long pa;
    2818     2553998 :   long t = RgX_type(x,&p,&pol,&pa);
    2819     2554170 :   switch(t)
    2820             :   {
    2821     2508907 :     case t_INT:    return ZX_sqr(x);
    2822       32046 :     case t_FRAC:   return QX_sqr(x);
    2823        2261 :     case t_FFELT:  return FFX_sqr(x, pol);
    2824        1260 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2825             :     case code(t_POLMOD, t_INT):
    2826             :     case code(t_POLMOD, t_FRAC):
    2827        3781 :                    return RgX_sqr_QXQX(x, pol);
    2828             :     case code(t_POLMOD, t_INTMOD):
    2829         147 :                    return RgX_sqr_FpXQX(x, pol, p);
    2830        5768 :     default:       return NULL;
    2831             :   }
    2832             : }
    2833             : 
    2834             : static GEN
    2835     6286041 : RgX_rem_fast(GEN x, GEN y)
    2836             : {
    2837             :   GEN p, pol;
    2838             :   long pa;
    2839     6286041 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2840     6286260 :   switch(t)
    2841             :   {
    2842     3904827 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    2843     1093644 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    2844        1883 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2845      348880 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2846             :     case code(t_POLMOD, t_INT):
    2847             :     case code(t_POLMOD, t_FRAC):
    2848       31902 :                    return RgX_rem_QXQX(x, y, pol);
    2849             :     case code(t_POLMOD, t_INTMOD):
    2850          35 :                    return RgX_rem_FpXQX(x, y, pol, p);
    2851      905089 :     default:       return NULL;
    2852             :   }
    2853             : }
    2854             : 
    2855             : #undef code
    2856             : 
    2857             : GEN
    2858    36856510 : RgX_mul(GEN x, GEN y)
    2859             : {
    2860    36856510 :   GEN z = RgX_mul_fast(x,y);
    2861    36856439 :   if (!z) z = RgX_mul_i(x,y);
    2862    36856487 :   return z;
    2863             : }
    2864             : 
    2865             : GEN
    2866     2537948 : RgX_sqr(GEN x)
    2867             : {
    2868     2537948 :   GEN z = RgX_sqr_fast(x);
    2869     2537023 :   if (!z) z = RgX_sqr_i(x);
    2870     2537256 :   return z;
    2871             : }
    2872             : 
    2873             : GEN
    2874     6286256 : RgX_rem(GEN x, GEN y)
    2875             : {
    2876     6286256 :   GEN z = RgX_rem_fast(x, y);
    2877     6286165 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    2878     6286252 :   return z;
    2879             : }
    2880             : 
    2881             : GEN
    2882     4155549 : RgXn_mul(GEN f, GEN g, long n)
    2883             : {
    2884     4155549 :   pari_sp av = avma;
    2885     4155549 :   GEN h = RgX_mul_fast(f,g);
    2886     4155549 :   if (!h) return RgXn_mul2(f,g,n);
    2887     2243487 :   if (degpol(h) < n) return h;
    2888     1469405 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    2889             : }
    2890             : 
    2891             : GEN
    2892       16086 : RgXn_sqr(GEN f, long n)
    2893             : {
    2894       16086 :   pari_sp av = avma;
    2895       16086 :   GEN g = RgX_sqr_fast(f);
    2896       16086 :   if (!g) return RgXn_sqr2(f,n);
    2897       15848 :   if (degpol(g) < n) return g;
    2898         770 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    2899             : }
    2900             : 
    2901             : /* (f*g) \/ x^n */
    2902             : GEN
    2903       36000 : RgX_mulhigh_i(GEN f, GEN g, long n)
    2904             : {
    2905       36000 :   GEN h = RgX_mul_fast(f,g);
    2906       36000 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    2907             : }
    2908             : 
    2909             : /* (f*g) \/ x^n */
    2910             : GEN
    2911           0 : RgX_sqrhigh_i(GEN f, long n)
    2912             : {
    2913           0 :   GEN h = RgX_sqr_fast(f);
    2914           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    2915             : }

Generated by: LCOV version 1.11