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 21348-d75f58f) Lines: 1453 1608 90.4 %
Date: 2017-11-20 06:21:05 Functions: 170 183 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     2281936 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     2281936 :   long pold=1, rold=n*(d-1);
      32    12672233 :   for(p=2; p<=d; p++)
      33             :   {
      34    10390297 :     r = m*(p-1) + n*((d-1)/p);
      35    10390297 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     2281936 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41    10110508 : 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    10110508 :   pari_sp av = avma;
      45             :   long i;
      46    10110508 :   GEN z = cmul(E,P,a,ff->one(E));
      47    10110542 :   if (!z) z = gen_0;
      48    62650704 :   for (i=1; i<=n; i++)
      49             :   {
      50    52540224 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    52540868 :     if (t) {
      52    51196935 :       z = ff->add(E, z, t);
      53    51195850 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56    10110480 :   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     6506470 : 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     6506470 :   pari_sp av = avma;
      69     6506470 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     6506470 :   if (d < 0) return ff->zero(E);
      73     6006398 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     2415625 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     2415624 :   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     2415624 :   d -= l;
      81     2415624 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     6519738 :   while (d >= l-1)
      83             :   {
      84     1688496 :     d -= l-1;
      85     1688496 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86     1688461 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87     1688488 :     if (gc_needed(av,2))
      88          61 :       z = gerepileupto(av, z);
      89             :   }
      90     2415620 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91     2415624 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92     2415625 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96     1070372 : 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     1070372 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102     1070372 :   if (d < 0) return ff->zero(E);
     103     1070225 :   rtd = (long) sqrt((double)d);
     104     1070225 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105     1070219 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106     1070221 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      762457 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    20844065 : _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      627104 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      203702 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      777808 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       18249 : _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      382151 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      382151 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      139440 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      139440 :   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       96534 : get_Rg_algebra(void)
     158             : {
     159       96534 :   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     3970279 : RgX_equal(GEN x, GEN y)
     188             : {
     189     3970279 :   long i = lg(x);
     190             : 
     191     3970279 :   if (i != lg(y)) return 0;
     192    21381585 :   for (i--; i > 1; i--)
     193    17469413 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194     3912172 :   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     2166604 : Rg_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203     2166604 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     204     2166604 :   if (RgX_type_is_composite(tx))
     205      376005 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206     2166604 :   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         119 :     case t_FFELT: return FF_1(T);
     211     2166387 :     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      128940 : Rg_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220      128940 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     221      128940 :   if (RgX_type_is_composite(tx))
     222       24535 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223      128940 :   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         252 :     case t_FFELT: return FF_zero(T);
     228      128429 :     default: return gen_0;
     229             :   }
     230             : }
     231             : 
     232             : GEN
     233        2576 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     234             : {
     235        2576 :   long i, n = degpol(P);
     236             :   GEN z, dz, dP;
     237        2576 :   if (n < 0) return gen_0;
     238        2576 :   P = Q_remove_denom(P, &dP);
     239        2576 :   z = gel(P,2); if (n == 0) return icopy(z);
     240        1386 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     241        1386 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     242        1386 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     243        1386 :   dz = mul_denom(dP, dV);
     244        1386 :   return dz? RgX_Rg_div(z, dz): z;
     245             : }
     246             : 
     247             : /* Return P(h * x), not memory clean */
     248             : GEN
     249        5026 : RgX_unscale(GEN P, GEN h)
     250             : {
     251        5026 :   long i, l = lg(P);
     252        5026 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253        5026 :   Q[1] = P[1];
     254        5026 :   if (l == 2) return Q;
     255        5026 :   gel(Q,2) = gcopy(gel(P,2));
     256       26103 :   for (i=3; i<l; i++)
     257             :   {
     258       21077 :     hi = gmul(hi,h);
     259       21077 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261        5026 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      319858 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      319858 :   long i, l = lg(P);
     268      319858 :   GEN Q = cgetg(l, t_POL);
     269      319858 :   Q[1] = P[1];
     270      319858 :   if (l == 2) return Q;
     271      319858 :   gel(Q,2) = gel(P,2);
     272      319858 :   if (l == 3) return Q;
     273      319858 :   if (h == -1)
     274      309302 :     for (i = 3; i < l; i++)
     275             :     {
     276      301160 :       gel(Q,i) = negi(gel(P,i));
     277      301160 :       if (++i == l) break;
     278      297913 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      308469 :     gel(Q,3) = mulis(gel(P,3), h);
     284      308469 :     hi = sqrs(h);
     285      653912 :     for (i = 4; i < l; i++)
     286             :     {
     287      345443 :       gel(Q,i) = mulii(gel(P,i), hi);
     288      345443 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      319858 :   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       19050 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       19050 :   long i, ni = n, l = lg(P);
     319       19050 :   GEN Q = cgetg(l, t_POL);
     320       19050 :   Q[1] = P[1];
     321       19050 :   if (l == 2) return Q;
     322       19050 :   gel(Q,2) = gel(P,2);
     323       19050 :   if (l == 3) return Q;
     324       19050 :   gel(Q,3) = shifti(gel(P,3), ni);
     325       77767 :   for (i=4; i<l; i++)
     326             :   {
     327       58717 :     ni += n;
     328       58717 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       19050 :   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         231 : RgXV_unscale(GEN v, GEN h)
     356             : {
     357             :   long i, l;
     358             :   GEN w;
     359         231 :   if (!h || isint1(h)) return v;
     360         175 :   w = cgetg_copy(v, &l);
     361         175 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     362         175 :   return w;
     363             : }
     364             : 
     365             : /* Return h^degpol(P) P(x / h), not memory clean */
     366             : GEN
     367        1806 : RgX_rescale(GEN P, GEN h)
     368             : {
     369        1806 :   long i, l = lg(P);
     370        1806 :   GEN Q = cgetg(l,t_POL), hi = h;
     371        1806 :   Q[l-1] = P[l-1];
     372        8491 :   for (i=l-2; i>=2; i--)
     373             :   {
     374        8491 :     gel(Q,i) = gmul(gel(P,i), hi);
     375        8491 :     if (i == 2) break;
     376        6685 :     hi = gmul(hi,h);
     377             :   }
     378        1806 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      114544 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      114544 :   long i,id, dy, dx = degpol(x0);
     387      114544 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       64281 :   dy = dx/d;
     389       64281 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       64281 :   z = y + 2;
     391       64281 :   x = x0+ 2;
     392       64281 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       64281 :   return y;
     394             : }
     395             : 
     396             : /* return x0(X^d) */
     397             : GEN
     398      405786 : RgX_inflate(GEN x0, long d)
     399             : {
     400      405786 :   long i, id, dy, dx = degpol(x0);
     401      405786 :   GEN x = x0 + 2, z, y;
     402      405786 :   if (dx <= 0) return leafcopy(x0);
     403      401803 :   dy = dx*d;
     404      401803 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     405      401804 :   z = y + 2;
     406      401804 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     407      401804 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     408      401804 :   return y;
     409             : }
     410             : 
     411             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     412             : GEN
     413     1048113 : RgX_translate(GEN P, GEN c)
     414             : {
     415     1048113 :   pari_sp av = avma;
     416             :   GEN Q, *R;
     417             :   long i, k, n;
     418             : 
     419     1048113 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     420     1046346 :   Q = leafcopy(P);
     421     1046346 :   R = (GEN*)(Q+2); n = degpol(P);
     422     1046346 :   if (gequal1(c))
     423             :   {
     424        8666 :     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     1044393 :   else if (gequalm1(c))
     435             :   {
     436      141078 :     for (i=1; i<=n; i++)
     437             :     {
     438      119924 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     439      119924 :       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     3535504 :     for (i=1; i<=n; i++)
     449             :     {
     450     2512265 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     451     2512265 :       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     1046346 :   return gerepilecopy(av, Q);
     459             : }
     460             : 
     461             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     462             : GEN
     463      378289 : ZX_translate(GEN P, GEN c)
     464             : {
     465      378289 :   pari_sp av = avma;
     466             :   GEN Q, *R;
     467             :   long i, k, n;
     468             : 
     469      378289 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     470      378254 :   Q = leafcopy(P);
     471      378254 :   R = (GEN*)(Q+2); n = degpol(P);
     472      378254 :   if (equali1(c))
     473             :   {
     474     2380100 :     for (i=1; i<=n; i++)
     475             :     {
     476     2105182 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     477     2105182 :       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      103336 :   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      772913 :     for (i=1; i<=n; i++)
     499             :     {
     500      669598 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], mulii(c, R[k+1]));
     501      669598 :       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      378254 :   return gerepilecopy(av, Q);
     509             : }
     510             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     511             : GEN
     512        6076 : RgXQX_translate(GEN P, GEN c, GEN T)
     513             : {
     514        6076 :   pari_sp av = avma;
     515             :   GEN Q, *R;
     516             :   long i, k, n;
     517             : 
     518        6076 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     519        6076 :   Q = leafcopy(P);
     520        6076 :   R = (GEN*)(Q+2); n = degpol(P);
     521       34965 :   for (i=1; i<=n; i++)
     522             :   {
     523      142198 :     for (k=n-i; k<n; k++)
     524             :     {
     525      113309 :       pari_sp av2 = avma;
     526      113309 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     527             :     }
     528       28889 :     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        6076 :   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       18211 : QXQ_to_mod_copy(GEN x, GEN T)
     547             : {
     548             :   long d;
     549       18211 :   switch(typ(x))
     550             :   {
     551        7252 :     case t_INT:  return icopy(x);
     552         805 :     case t_FRAC: return gcopy(x);
     553             :     case t_POL:
     554       10154 :       d = degpol(x);
     555       10154 :       if (d < 0) return gen_0;
     556        9832 :       if (d == 0) return gcopy(gel(x,2));
     557        9650 :       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      858856 : QXQ_to_mod(GEN x, GEN T)
     565             : {
     566             :   long d;
     567      858856 :   switch(typ(x))
     568             :   {
     569             :     case t_INT:
     570      399455 :     case t_FRAC: return x;
     571             :     case t_POL:
     572      459401 :       d = degpol(x);
     573      459401 :       if (d < 0) return gen_0;
     574      347877 :       if (d == 0) return gel(x,2);
     575      331344 :       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        1967 : QXQX_to_mod(GEN z, GEN T)
     584             : {
     585        1967 :   long i,l = lg(z);
     586        1967 :   GEN x = cgetg(l,t_POL);
     587        1967 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     588        1967 :   x[1] = z[1]; return normalizepol_lg(x,l);
     589             : }
     590             : /* pure shallow version */
     591             : GEN
     592      121303 : QXQX_to_mod_shallow(GEN z, GEN T)
     593             : {
     594      121303 :   long i,l = lg(z);
     595      121303 :   GEN x = cgetg(l,t_POL);
     596      121303 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     597      121303 :   x[1] = z[1]; return normalizepol_lg(x,l);
     598             : }
     599             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     600             : GEN
     601         609 : QXQXV_to_mod(GEN V, GEN T)
     602             : {
     603         609 :   long i, l = lg(V);
     604         609 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     605         609 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     606         609 :   return z;
     607             : }
     608             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     609             : GEN
     610        4726 : QXQV_to_mod(GEN V, GEN T)
     611             : {
     612        4726 :   long i, l = lg(V);
     613        4726 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     614        4726 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     615        4726 :   return z;
     616             : }
     617             : 
     618             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     619             : GEN
     620       17087 : QXQC_to_mod_shallow(GEN V, GEN T)
     621             : {
     622       17087 :   long i, l = lg(V);
     623       17087 :   GEN z = cgetg(l, t_COL);
     624       17087 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     625       17087 :   return z;
     626             : }
     627             : 
     628             : GEN
     629        5964 : QXQM_to_mod_shallow(GEN V, GEN T)
     630             : {
     631        5964 :   long i, l = lg(V);
     632        5964 :   GEN z = cgetg(l, t_MAT);
     633        5964 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     634        5964 :   return z;
     635             : }
     636             : 
     637             : GEN
     638      751371 : RgX_renormalize_lg(GEN x, long lx)
     639             : {
     640             :   long i;
     641     2047490 :   for (i = lx-1; i>1; i--)
     642     1916303 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     643      751371 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     644      751371 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     645             : }
     646             : 
     647             : GEN
     648      433109 : RgV_to_RgX(GEN x, long v)
     649             : {
     650      433109 :   long i, k = lg(x);
     651             :   GEN p;
     652             : 
     653      433109 :   while (--k && gequal0(gel(x,k)));
     654      433108 :   if (!k) return pol_0(v);
     655      431911 :   i = k+2; p = cgetg(i,t_POL);
     656      431911 :   p[1] = evalsigne(1) | evalvarn(v);
     657      431911 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     658      431911 :   return p;
     659             : }
     660             : GEN
     661      149566 : RgV_to_RgX_reverse(GEN x, long v)
     662             : {
     663      149566 :   long j, k, l = lg(x);
     664             :   GEN p;
     665             : 
     666      149566 :   for (k = 1; k < l; k++)
     667      149566 :     if (!gequal0(gel(x,k))) break;
     668      149566 :   if (k == l) return pol_0(v);
     669      149566 :   k -= 1;
     670      149566 :   l -= k;
     671      149566 :   x += k;
     672      149566 :   p = cgetg(l+1,t_POL);
     673      149566 :   p[1] = evalsigne(1) | evalvarn(v);
     674      149566 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     675      149566 :   return p;
     676             : }
     677             : 
     678             : /* return the (N-dimensional) vector of coeffs of p */
     679             : GEN
     680     4091962 : RgX_to_RgC(GEN x, long N)
     681             : {
     682             :   long i, l;
     683             :   GEN z;
     684     4091962 :   l = lg(x)-1; x++;
     685     4091962 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     686     4091962 :   z = cgetg(N+1,t_COL);
     687     4091962 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     688     4091962 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     689     4091962 :   return z;
     690             : }
     691             : GEN
     692      677132 : Rg_to_RgC(GEN x, long N)
     693             : {
     694      677132 :   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       47043 : RgM_to_RgXV(GEN x, long v)
     700       47043 : { 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      102938 : RgV_to_RgM(GEN x, long n)
     706      102938 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     707             : 
     708             : GEN
     709        2678 : RgXV_to_RgM(GEN x, long n)
     710        2678 : { 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       15736 : RgM_to_RgXX(GEN x, long v,long w)
     715             : {
     716       15736 :   long j, lx = lg(x);
     717       15736 :   GEN y = cgetg(lx+1, t_POL);
     718       15736 :   y[1] = evalsigne(1) | evalvarn(v);
     719       15736 :   y++;
     720       15736 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     721       15736 :   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          77 : RgXX_to_RgM(GEN v, long n)
     728             : {
     729          77 :   long j, N = lg(v)-1;
     730          77 :   GEN y = cgetg(N, t_MAT);
     731          77 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     732          77 :   return y;
     733             : }
     734             : 
     735             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     736             : GEN
     737       13414 : RgXY_swapspec(GEN x, long n, long w, long nx)
     738             : {
     739       13414 :   long j, ly = n+3;
     740       13414 :   GEN y = cgetg(ly, t_POL);
     741       13414 :   y[1] = evalsigne(1);
     742      200810 :   for (j=2; j<ly; j++)
     743             :   {
     744             :     long k;
     745      187396 :     GEN a = cgetg(nx+2,t_POL);
     746      187396 :     a[1] = evalsigne(1) | evalvarn(w);
     747     1016650 :     for (k=0; k<nx; k++)
     748             :     {
     749      829254 :       GEN xk = gel(x,k);
     750      829254 :       if (typ(xk)==t_POL)
     751      743679 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     752             :       else
     753       85575 :         gel(a,k+2) = j==2 ? xk: gen_0;
     754             :     }
     755      187396 :     gel(y,j) = normalizepol_lg(a, nx+2);
     756             :   }
     757       13414 :   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         784 : RgXY_swap(GEN x, long n, long w)
     763             : {
     764         784 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     765         784 :   setvarn(z, varn(x)); return z;
     766             : }
     767             : 
     768             : long
     769          99 : RgXY_degreex(GEN b)
     770             : {
     771          99 :   long deg = -1, i;
     772          99 :   if (!signe(b)) return -1;
     773         612 :   for (i = 2; i < lg(b); ++i)
     774             :   {
     775         513 :     GEN bi = gel(b, i);
     776         513 :     if (typ(bi) == t_POL)
     777         484 :       deg = maxss(deg, degpol(bi));
     778             :   }
     779          99 :   return deg;
     780             : }
     781             : 
     782             : /* return (x % X^n). Shallow */
     783             : GEN
     784     1392974 : RgXn_red_shallow(GEN a, long n)
     785             : {
     786     1392974 :   long i, L = n+2, l = lg(a);
     787             :   GEN  b;
     788     1392974 :   if (L >= l) return a; /* deg(x) < n */
     789     1355783 :   b = cgetg(L, t_POL); b[1] = a[1];
     790     1355783 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     791     1355783 :   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    57761036 : RgX_shift_shallow(GEN a, long n)
     801             : {
     802    57761036 :   long i, l = lg(a);
     803             :   GEN  b;
     804    57761036 :   if (l == 2 || !n) return a;
     805    42569907 :   l += n;
     806    42569907 :   if (n < 0)
     807             :   {
     808    37596767 :     if (l <= 2) return pol_0(varn(a));
     809    37577769 :     b = cgetg(l, t_POL); b[1] = a[1];
     810    37577775 :     a -= n;
     811    37577775 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     812             :   } else {
     813     4973140 :     b = cgetg(l, t_POL); b[1] = a[1];
     814     4973135 :     a -= n; n += 2;
     815     4973135 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     816     4973135 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     817             :   }
     818    42550910 :   return b;
     819             : }
     820             : /* return (x * X^n). */
     821             : GEN
     822     3402844 : RgX_shift(GEN a, long n)
     823             : {
     824     3402844 :   long i, l = lg(a);
     825             :   GEN  b;
     826     3402844 :   if (l == 2 || !n) return RgX_copy(a);
     827     3402585 :   l += n;
     828     3402585 :   if (n < 0)
     829             :   {
     830         623 :     if (l <= 2) return pol_0(varn(a));
     831         581 :     b = cgetg(l, t_POL); b[1] = a[1];
     832         581 :     a -= n;
     833         581 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     834             :   } else {
     835     3401962 :     b = cgetg(l, t_POL); b[1] = a[1];
     836     3401962 :     a -= n; n += 2;
     837     3401962 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     838     3401962 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     839             :   }
     840     3402543 :   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         427 :     return pol_0(varn(P));
     850      314846 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     851     2091390 :   for(i=0; i<p; i++)
     852             :   {
     853     1776544 :     long s = 2+(i+k)%p;
     854     1776544 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     855             :   }
     856      314846 :   return RgX_renormalize(r);
     857             : }
     858             : 
     859             : GEN
     860     2859216 : RgX_mulXn(GEN x, long d)
     861             : {
     862             :   pari_sp av;
     863             :   GEN z;
     864             :   long v;
     865     2859216 :   if (d >= 0) return RgX_shift(x, d);
     866     1366780 :   d = -d;
     867     1366780 :   v = RgX_val(x);
     868     1366780 :   if (v >= d) return RgX_shift(x, -d);
     869     1366773 :   av = avma;
     870     1366773 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     871     1366773 :   return gerepileupto(av, z);
     872             : }
     873             : 
     874             : long
     875     2109601 : RgX_val(GEN x)
     876             : {
     877     2109601 :   long i, lx = lg(x);
     878     2109601 :   if (lx == 2) return LONG_MAX;
     879     2132778 :   for (i = 2; i < lx; i++)
     880     2132764 :     if (!isexactzero(gel(x,i))) break;
     881     2109559 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     882     2109545 :   return i - 2;
     883             : }
     884             : long
     885    42485518 : RgX_valrem(GEN x, GEN *Z)
     886             : {
     887    42485518 :   long v, i, lx = lg(x);
     888    42485518 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     889    81819420 :   for (i = 2; i < lx; i++)
     890    81819420 :     if (!isexactzero(gel(x,i))) break;
     891             :   /* possible with non-rational zeros */
     892    42485518 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     893    42485518 :   v = i - 2;
     894    42485518 :   *Z = RgX_shift_shallow(x, -v);
     895    42485518 :   return v;
     896             : }
     897             : long
     898       10082 : RgX_valrem_inexact(GEN x, GEN *Z)
     899             : {
     900             :   long v;
     901       10082 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     902       10460 :   for (v = 0;; v++)
     903       10460 :     if (!gequal0(gel(x,2+v))) break;
     904         385 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     905       10075 :   return v;
     906             : }
     907             : 
     908             : GEN
     909       82362 : RgXQC_red(GEN x, GEN T)
     910       82362 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
     911             : 
     912             : GEN
     913         546 : RgXQV_red(GEN x, GEN T)
     914         546 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
     915             : 
     916             : GEN
     917       12376 : RgXQM_red(GEN x, GEN T)
     918       12376 : { 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      346450 : RgXQX_red(GEN P, GEN T)
     928             : {
     929      346450 :   long i, l = lg(P);
     930      346450 :   GEN Q = cgetg(l, t_POL);
     931      346450 :   Q[1] = P[1];
     932      346450 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     933      346450 :   return normalizepol_lg(Q, l);
     934             : }
     935             : 
     936             : GEN
     937      222613 : RgX_deriv(GEN x)
     938             : {
     939      222613 :   long i,lx = lg(x)-1;
     940             :   GEN y;
     941             : 
     942      222613 :   if (lx<3) return pol_0(varn(x));
     943      194977 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     944      194977 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     945      194977 :   y[1] = x[1]; return normalizepol_lg(y,i);
     946             : }
     947             : 
     948             : GEN
     949      437578 : RgX_recipspec_shallow(GEN x, long l, long n)
     950             : {
     951             :   long i;
     952      437578 :   GEN z=cgetg(n+2,t_POL)+2;
     953    15162659 :   for(i=0; i<l; i++)
     954    14725081 :     gel(z,n-i-1) = gel(x,i);
     955      525606 :   for(   ; i<n; i++)
     956       88028 :     gel(z, n-i-1) = gen_0;
     957      437578 :   return normalizepol_lg(z-2,n+2);
     958             : }
     959             : 
     960             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     961             : GEN
     962        2149 : RgX_recip(GEN x)
     963             : {
     964             :   long lx, i, j;
     965        2149 :   GEN y = cgetg_copy(x, &lx);
     966        2149 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     967        2149 :   return normalizepol_lg(y,lx);
     968             : }
     969             : /* shallow version */
     970             : GEN
     971      330084 : RgX_recip_shallow(GEN x)
     972             : {
     973             :   long lx, i, j;
     974      330084 :   GEN y = cgetg_copy(x, &lx);
     975      330093 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     976      330093 :   return y;
     977             : }
     978             : /*******************************************************************/
     979             : /*                                                                 */
     980             : /*                      ADDITION / SUBTRACTION                     */
     981             : /*                                                                 */
     982             : /*******************************************************************/
     983             : /* same variable */
     984             : GEN
     985    17210075 : RgX_add(GEN x, GEN y)
     986             : {
     987    17210075 :   long i, lx = lg(x), ly = lg(y);
     988             :   GEN z;
     989    17210075 :   if (ly <= lx) {
     990    15497512 :     z = cgetg(lx,t_POL); z[1] = x[1];
     991    15497512 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     992    15497512 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
     993    15497512 :     z = normalizepol_lg(z, lx);
     994             :   } else {
     995     1712563 :     z = cgetg(ly,t_POL); z[1] = y[1];
     996     1712563 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     997     1712563 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
     998     1712563 :     z = normalizepol_lg(z, ly);
     999             :   }
    1000    17210075 :   return z;
    1001             : }
    1002             : GEN
    1003    11675751 : RgX_sub(GEN x, GEN y)
    1004             : {
    1005    11675751 :   long i, lx = lg(x), ly = lg(y);
    1006             :   GEN z;
    1007    11675751 :   if (ly <= lx) {
    1008     9171674 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1009     9171674 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1010     9171674 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1011     9171674 :     z = normalizepol_lg(z, lx);
    1012             :   } else {
    1013     2504077 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1014     2504077 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1015     2504077 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1016     2504077 :     z = normalizepol_lg(z, ly);
    1017             :   }
    1018    11675751 :   return z;
    1019             : }
    1020             : GEN
    1021     1038865 : RgX_neg(GEN x)
    1022             : {
    1023     1038865 :   long i, lx = lg(x);
    1024     1038865 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1025     1038865 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1026     1038865 :   return y;
    1027             : }
    1028             : 
    1029             : GEN
    1030    11740309 : RgX_Rg_add(GEN y, GEN x)
    1031             : {
    1032             :   GEN z;
    1033    11740309 :   long lz = lg(y), i;
    1034    11740309 :   if (lz == 2) return scalarpol(x,varn(y));
    1035    10043933 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1036    10043933 :   gel(z,2) = gadd(gel(y,2),x);
    1037    10043933 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1038             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1039             :    * an inexact 0 */
    1040    10043933 :   return normalizepol_lg(z,lz);
    1041             : }
    1042             : GEN
    1043        2422 : RgX_Rg_add_shallow(GEN y, GEN x)
    1044             : {
    1045             :   GEN z;
    1046        2422 :   long lz = lg(y), i;
    1047        2422 :   if (lz == 2) return scalarpol(x,varn(y));
    1048        2422 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1049        2422 :   gel(z,2) = gadd(gel(y,2),x);
    1050        2422 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1051        2422 :   return z = normalizepol_lg(z,lz);
    1052             : }
    1053             : GEN
    1054       32901 : RgX_Rg_sub(GEN y, GEN x)
    1055             : {
    1056             :   GEN z;
    1057       32901 :   long lz = lg(y), i;
    1058       32901 :   if (lz == 2)
    1059             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1060        3864 :     long v = varn(y);
    1061        3864 :     if (isrationalzero(x)) return pol_0(v);
    1062          14 :     z = cgetg(3,t_POL);
    1063          28 :     z[1] = gequal0(x)? evalvarn(v)
    1064          14 :                    : evalvarn(v) | evalsigne(1);
    1065          14 :     gel(z,2) = gneg(x); return z;
    1066             :   }
    1067       29037 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1068       29037 :   gel(z,2) = gsub(gel(y,2),x);
    1069       29037 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1070       29037 :   return z = normalizepol_lg(z,lz);
    1071             : }
    1072             : GEN
    1073      380343 : Rg_RgX_sub(GEN x, GEN y)
    1074             : {
    1075             :   GEN z;
    1076      380343 :   long lz = lg(y), i;
    1077      380343 :   if (lz == 2) return scalarpol(x,varn(y));
    1078      379328 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1079      379328 :   gel(z,2) = gsub(x, gel(y,2));
    1080      379328 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1081      379328 :   return z = normalizepol_lg(z,lz);
    1082             : }
    1083             : /*******************************************************************/
    1084             : /*                                                                 */
    1085             : /*                  KARATSUBA MULTIPLICATION                       */
    1086             : /*                                                                 */
    1087             : /*******************************************************************/
    1088             : #if 0
    1089             : /* to debug Karatsuba-like routines */
    1090             : GEN
    1091             : zx_debug_spec(GEN x, long nx)
    1092             : {
    1093             :   GEN z = cgetg(nx+2,t_POL);
    1094             :   long i;
    1095             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1096             :   z[1] = evalsigne(1); return z;
    1097             : }
    1098             : 
    1099             : GEN
    1100             : RgX_debug_spec(GEN x, long nx)
    1101             : {
    1102             :   GEN z = cgetg(nx+2,t_POL);
    1103             :   long i;
    1104             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1105             :   z[1] = evalsigne(1); return z;
    1106             : }
    1107             : #endif
    1108             : 
    1109             : /* generic multiplication */
    1110             : GEN
    1111     2909948 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1112             : {
    1113             :   GEN z, t;
    1114             :   long i;
    1115     2909948 :   if (nx == ny) {
    1116      656172 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1117      656172 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1118      656172 :     return normalizepol_lg(z, nx+2);
    1119             :   }
    1120     2253776 :   if (ny < nx) {
    1121     2108755 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1122     2108755 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1123     2108755 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1124     2108755 :     return normalizepol_lg(z, nx+2);
    1125             :   } else {
    1126      145021 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1127      145021 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1128      145021 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1129      145021 :     return normalizepol_lg(z, ny+2);
    1130             :   }
    1131             : }
    1132             : GEN
    1133      187682 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1134             : {
    1135             :   GEN z, t;
    1136             :   long i;
    1137      187682 :   if (nx == ny) {
    1138        2050 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1139        2050 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1140        2050 :     return normalizepol_lg(z, nx+2);
    1141             :   }
    1142      185632 :   if (ny < nx) {
    1143      184337 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1144      184337 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1145      184337 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1146      184337 :     return normalizepol_lg(z, nx+2);
    1147             :   } else {
    1148        1295 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1149        1295 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1150        1295 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1151        1295 :     return normalizepol_lg(z, ny+2);
    1152             :   }
    1153             : }
    1154             : 
    1155             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1156             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1157             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1158             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1159             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1160             : static GEN
    1161    19700158 : RgXspec_kill0(GEN x, long lx)
    1162             : {
    1163    19700158 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1164             :   long i;
    1165    76683465 :   for (i=0; i <lx; i++)
    1166             :   {
    1167    56983307 :     GEN c = gel(x,i);
    1168    56983307 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1169             :   }
    1170    19700158 :   return z;
    1171             : }
    1172             : 
    1173             : INLINE GEN
    1174    42777420 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1175             : {
    1176    42777420 :   pari_sp av = avma;
    1177    42777420 :   GEN s = NULL;
    1178             :   long i;
    1179             : 
    1180   174518032 :   for (i=a; i<b; i++)
    1181   131740612 :     if (gel(y,i) && gel(x,-i))
    1182             :     {
    1183    81128314 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1184    81128314 :       s = s? gadd(s, t): t;
    1185             :     }
    1186    42777420 :   return s? gerepileupto(av, s): gen_0;
    1187             : }
    1188             : 
    1189             : /* assume nx >= ny > 0, return x * y * t^v */
    1190             : static GEN
    1191     8158837 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1192             : {
    1193             :   long i, lz, nz;
    1194             :   GEN z;
    1195             : 
    1196     8158837 :   x = RgXspec_kill0(x,nx);
    1197     8158837 :   y = RgXspec_kill0(y,ny);
    1198     8158837 :   lz = nx + ny + 1; nz = lz-2;
    1199     8158837 :   lz += v;
    1200     8158837 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1201     8158837 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1202     8158837 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1203     8158837 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1204     8158837 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1205     8158837 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1206             : }
    1207             : 
    1208             : /* return (x * X^d) + y. Assume d > 0 */
    1209             : GEN
    1210     2021073 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1211             : {
    1212             :   GEN x, y, xd, yd, zd;
    1213             :   long a, lz, nx, ny;
    1214             : 
    1215     2021073 :   if (!signe(x0)) return y0;
    1216     1999364 :   ny = lgpol(y0);
    1217     1999364 :   nx = lgpol(x0);
    1218     1999364 :   zd = (GEN)avma;
    1219     1999364 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1220     1999364 :   if (a <= 0)
    1221             :   {
    1222      164147 :     lz = nx+d+2;
    1223      164147 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1224      164147 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1225      164147 :     x = zd + a;
    1226      164147 :     while (zd > x) gel(--zd,0) = gen_0;
    1227             :   }
    1228             :   else
    1229             :   {
    1230     1835217 :     xd = new_chunk(d); yd = y+d;
    1231     1835217 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1232     1835217 :     lz = (a>nx)? ny+2: lg(x)+d;
    1233     1835217 :     x += 2; while (xd > x) *--zd = *--xd;
    1234             :   }
    1235     1999364 :   while (yd > y) *--zd = *--yd;
    1236     1999364 :   *--zd = x0[1];
    1237     1999364 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1238             : }
    1239             : GEN
    1240      478438 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1241             : {
    1242             :   GEN x, y, xd, yd, zd;
    1243             :   long a, lz, nx, ny;
    1244             : 
    1245      478438 :   if (!signe(x0)) return RgX_copy(y0);
    1246      477994 :   nx = lgpol(x0);
    1247      477994 :   ny = lgpol(y0);
    1248      477994 :   zd = (GEN)avma;
    1249      477994 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1250      477994 :   if (a <= 0)
    1251             :   {
    1252      290312 :     lz = nx+d+2;
    1253      290312 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1254      290312 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1255      290312 :     x = zd + a;
    1256      290312 :     while (zd > x) gel(--zd,0) = gen_0;
    1257             :   }
    1258             :   else
    1259             :   {
    1260      187682 :     xd = new_chunk(d); yd = y+d;
    1261      187682 :     x = RgX_addspec(x,yd, nx,a);
    1262      187682 :     lz = (a>nx)? ny+2: lg(x)+d;
    1263      187682 :     x += 2; while (xd > x) *--zd = *--xd;
    1264             :   }
    1265      477994 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1266      477994 :   *--zd = x0[1];
    1267      477994 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1268             : }
    1269             : 
    1270             : /* return x * y mod t^n */
    1271             : static GEN
    1272     1688412 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1273             : {
    1274     1688412 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1275             :   GEN z;
    1276     1688412 :   if (lx < 0) return pol_0(varn(x));
    1277     1688412 :   if (ly < 0) return pol_0(varn(x));
    1278     1688412 :   z = cgetg(lz, t_POL) + 2;
    1279     1688412 :   x+=2; if (lx > n) lx = n;
    1280     1688412 :   y+=2; if (ly > n) ly = n;
    1281     1688412 :   z[-1] = x[-1];
    1282     1688412 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1283     1688412 :   x = RgXspec_kill0(x, lx);
    1284     1688412 :   y = RgXspec_kill0(y, ly);
    1285             :   /* x:y:z [i] = term of degree i */
    1286     1688412 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1287     1688412 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1288     1688412 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1289     1688412 :   return normalizepol_lg(z - 2, lz);
    1290             : }
    1291             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1292             : static GEN
    1293     1729089 : RgXn_mul2(GEN f, GEN g, long n)
    1294             : {
    1295     1729089 :   pari_sp av = avma;
    1296             :   GEN fe,fo, ge,go, l,h,m;
    1297             :   long n0, n1;
    1298     1729089 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1299     1698541 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1300       10129 :   n0 = n>>1; n1 = n-n0;
    1301       10129 :   RgX_even_odd(f, &fe, &fo);
    1302       10129 :   RgX_even_odd(g, &ge, &go);
    1303       10129 :   l = RgXn_mul(fe,ge,n1);
    1304       10129 :   h = RgXn_mul(fo,go,n0);
    1305       10129 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1306             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1307             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1308       10129 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1309             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1310       10129 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1311       10129 :   m = RgX_inflate(m,2);
    1312             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1313       10129 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1314       10129 :   h = RgX_inflate(h,2);
    1315       10129 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1316       10129 :   return gerepileupto(av, h);
    1317             : }
    1318             : /* (f*g) \/ x^n */
    1319             : static GEN
    1320        2540 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1321             : {
    1322        2540 :   long d = degpol(f)+degpol(g) + 1 - n;
    1323             :   GEN h;
    1324        2540 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1325        1046 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1326             :                                  RgX_recip_shallow(g), d));
    1327        1046 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1328             : }
    1329             : 
    1330             : /* (f*g) \/ x^n */
    1331             : static GEN
    1332           0 : RgX_sqrhigh_i2(GEN f, long n)
    1333             : {
    1334           0 :   long d = 2*degpol(f)+ 1 - n;
    1335             :   GEN h;
    1336           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1337           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1338           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1339             : }
    1340             : 
    1341             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1342             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1343             :  * Only c, c0, c1, c2 are genuine GEN.
    1344             :  */
    1345             : GEN
    1346     8694052 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1347             : {
    1348             :   GEN a0, c, c0;
    1349     8694052 :   long n0, n0a, i, v = 0;
    1350             :   pari_sp av;
    1351             : 
    1352     8694052 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1353     8694052 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1354     8694052 :   if (na < nb) swapspec(a,b, na,nb);
    1355     8694052 :   if (!nb) return pol_0(0);
    1356             : 
    1357     8627107 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1358      468270 :   RgX_shift_inplace_init(v);
    1359      468270 :   i = (na>>1); n0 = na-i; na = i;
    1360      468270 :   av = avma; a0 = a+n0; n0a = n0;
    1361      468270 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1362             : 
    1363      468270 :   if (nb > n0)
    1364             :   {
    1365             :     GEN b0,c1,c2;
    1366             :     long n0b;
    1367             : 
    1368      466970 :     nb -= n0; b0 = b+n0; n0b = n0;
    1369      466970 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1370      466970 :     c = RgX_mulspec(a,b,n0a,n0b);
    1371      466970 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1372             : 
    1373      466970 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1374      466970 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1375             : 
    1376      466970 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1377      466970 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1378      466970 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1379             :   }
    1380             :   else
    1381             :   {
    1382        1300 :     c = RgX_mulspec(a,b,n0a,nb);
    1383        1300 :     c0 = RgX_mulspec(a0,b,na,nb);
    1384             :   }
    1385      468270 :   c0 = RgX_addmulXn(c0,c,n0);
    1386      468270 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1387             : }
    1388             : 
    1389             : INLINE GEN
    1390       32266 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1391             : {
    1392       32266 :   pari_sp av = avma;
    1393       32266 :   GEN s = NULL;
    1394       32266 :   long j, l = (i+1)>>1;
    1395       96502 :   for (j=a; j<l; j++)
    1396             :   {
    1397       64236 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1398       64236 :     if (xj && xx)
    1399             :     {
    1400       56942 :       GEN t = gmul(xj, xx);
    1401       56942 :       s = s? gadd(s, t): t;
    1402             :     }
    1403             :   }
    1404       32266 :   if (s) s = gshift(s,1);
    1405       32266 :   if ((i&1) == 0)
    1406             :   {
    1407       18963 :     GEN t = gel(x, i>>1);
    1408       18963 :     if (t) {
    1409       16653 :       t = gsqr(t);
    1410       16653 :       s = s? gadd(s, t): t;
    1411             :     }
    1412             :   }
    1413       32266 :   return s? gerepileupto(av,s): gen_0;
    1414             : }
    1415             : static GEN
    1416        5660 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1417             : {
    1418             :   long i, lz, nz;
    1419             :   GEN z;
    1420             : 
    1421        5660 :   if (!nx) return pol_0(0);
    1422        5660 :   x = RgXspec_kill0(x,nx);
    1423        5660 :   lz = (nx << 1) + 1, nz = lz-2;
    1424        5660 :   lz += v;
    1425        5660 :   z = cgetg(lz,t_POL) + 2;
    1426        5660 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1427        5660 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1428        5660 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1429        5660 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1430             : }
    1431             : /* return x^2 mod t^n */
    1432             : static GEN
    1433           0 : RgXn_sqr_basecase(GEN x, long n)
    1434             : {
    1435           0 :   long i, lz = n+2, lx = lgpol(x);
    1436             :   GEN z;
    1437           0 :   if (lx < 0) return pol_0(varn(x));
    1438           0 :   z = cgetg(lz, t_POL);
    1439           0 :   z[1] = x[1];
    1440           0 :   x+=2; if (lx > n) lx = n;
    1441           0 :   x = RgXspec_kill0(x,lx);
    1442           0 :   z+=2;/* x:z [i] = term of degree i */
    1443           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1444           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1445           0 :   z -= 2; return normalizepol_lg(z, lz);
    1446             : }
    1447             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1448             : static GEN
    1449         238 : RgXn_sqr2(GEN f, long n)
    1450             : {
    1451         238 :   pari_sp av = avma;
    1452             :   GEN fe,fo, l,h,m;
    1453             :   long n0, n1;
    1454         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1455           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1456           0 :   n0 = n>>1; n1 = n-n0;
    1457           0 :   RgX_even_odd(f, &fe, &fo);
    1458           0 :   l = RgXn_sqr(fe,n1);
    1459           0 :   h = RgXn_sqr(fo,n0);
    1460           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1461             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1462             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1463           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1464             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1465           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1466           0 :   m = RgX_inflate(m,2);
    1467             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1468           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1469           0 :   h = RgX_inflate(h,2);
    1470           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1471           0 :   return gerepileupto(av, h);
    1472             : }
    1473             : GEN
    1474        5699 : RgX_sqrspec(GEN a, long na)
    1475             : {
    1476             :   GEN a0, c, c0, c1;
    1477        5699 :   long n0, n0a, i, v = 0;
    1478             :   pari_sp av;
    1479             : 
    1480        5699 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1481        5699 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1482          39 :   RgX_shift_inplace_init(v);
    1483          39 :   i = (na>>1); n0 = na-i; na = i;
    1484          39 :   av = avma; a0 = a+n0; n0a = n0;
    1485          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1486             : 
    1487          39 :   c = RgX_sqrspec(a,n0a);
    1488          39 :   c0 = RgX_sqrspec(a0,na);
    1489          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1490          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1491          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1492          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1493             : }
    1494             : 
    1495             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1496             : GEN
    1497      579499 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1498             : {
    1499      579499 :   GEN z = RgX_mul(A, B);
    1500      579499 :   if (a < b)
    1501        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1502      574053 :   else if (a > b)
    1503      349047 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1504             :   else
    1505      225006 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1506      579499 :   return z;
    1507             : }
    1508             : 
    1509             : GEN
    1510     7290503 : RgX_mul_i(GEN x, GEN y)
    1511             : {
    1512     7290503 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1513     7290503 :   setvarn(z, varn(x)); return z;
    1514             : }
    1515             : 
    1516             : GEN
    1517        5621 : RgX_sqr_i(GEN x)
    1518             : {
    1519        5621 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1520        5621 :   setvarn(z,varn(x)); return z;
    1521             : }
    1522             : 
    1523             : /*******************************************************************/
    1524             : /*                                                                 */
    1525             : /*                               DIVISION                          */
    1526             : /*                                                                 */
    1527             : /*******************************************************************/
    1528             : GEN
    1529      433746 : RgX_Rg_divexact(GEN x, GEN y) {
    1530             :   long i, lx;
    1531             :   GEN z;
    1532      433746 :   if (typ(y) == t_INT && is_pm1(y))
    1533       27767 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1534      405979 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1535      405979 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1536      405979 :   return z;
    1537             : }
    1538             : GEN
    1539    22600750 : RgX_Rg_div(GEN x, GEN y) {
    1540             :   long i, lx;
    1541    22600750 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1542    22600750 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1543    22600750 :   return normalizepol_lg(z, lx);
    1544             : }
    1545             : GEN
    1546        1939 : RgX_normalize(GEN x)
    1547             : {
    1548        1939 :   GEN d = NULL;
    1549        1939 :   long i, n = lg(x)-1;
    1550        1939 :   for (i = n; i > 1; i--)
    1551             :   {
    1552        1939 :     d = gel(x,i);
    1553        1939 :     if (!gequal0(d)) break;
    1554             :   }
    1555        1939 :   if (i == 1) return pol_0(varn(x));
    1556        1939 :   if (i == n && isint1(d)) return x;
    1557         595 :   return normalizepol_lg(RgX_Rg_div(x, d), i+1);
    1558             : }
    1559             : GEN
    1560        2457 : RgX_divs(GEN x, long y) {
    1561             :   long i, lx;
    1562        2457 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1563        2457 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1564        2457 :   return normalizepol_lg(z, lx);
    1565             : }
    1566             : GEN
    1567       39189 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1568             : {
    1569       39189 :   long l = lg(a), i;
    1570       39189 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1571       39189 :   z[1] = a[1];
    1572       39189 :   a0 = a + l-1;
    1573       39189 :   z0 = z + l-2; *z0 = *a0--;
    1574      779374 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1575             :   {
    1576      740185 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1577      740185 :     gel(z0,0) = t;
    1578             :   }
    1579       39189 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1580       39189 :   return z;
    1581             : }
    1582             : /* Polynomial division x / y:
    1583             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1584             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1585             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1586             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1587             : static GEN
    1588    11678210 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1589             : {
    1590             :   pari_sp avy, av, av1;
    1591             :   long dx,dy,dz,i,j,sx,lr;
    1592             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1593             :   GEN (*f)(GEN,GEN);
    1594             : 
    1595    11678210 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1596             : 
    1597    11678210 :   dy = degpol(y);
    1598    11678210 :   y_lead = gel(y,dy+2);
    1599    11678210 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1600             :   {
    1601           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1602           0 :     for (dy--; dy>=0; dy--)
    1603             :     {
    1604           0 :       y_lead = gel(y,dy+2);
    1605           0 :       if (!gequal0(y_lead)) break;
    1606             :     }
    1607             :   }
    1608    11678210 :   if (!dy) /* y is constant */
    1609             :   {
    1610       14375 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1611       14375 :     z = RgX_Rg_div(x, y_lead);
    1612       14375 :     if (pr == ONLY_DIVIDES) return z;
    1613       12744 :     if (pr) *pr = pol_0(varn(x));
    1614       12744 :     return z;
    1615             :   }
    1616    11663835 :   dx = degpol(x);
    1617    11663835 :   if (dx < dy)
    1618             :   {
    1619      532883 :     if (pr == ONLY_REM) return RgX_copy(x);
    1620      339330 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1621      339309 :     z = pol_0(varn(x));
    1622      339309 :     if (pr) *pr = RgX_copy(x);
    1623      339309 :     return z;
    1624             :   }
    1625             : 
    1626             :   /* x,y in R[X], y non constant */
    1627    11130952 :   av = avma;
    1628    11130952 :   p = NULL;
    1629    11130952 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1630             :   {
    1631      112952 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1632      112952 :     if (!z) { avma = av; return NULL; }
    1633      112952 :     z = FpX_to_mod(z, p);
    1634      112952 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1635       60879 :       return gerepileupto(av, z);
    1636       52073 :     *pr = FpX_to_mod(*pr, p);
    1637       52073 :     gerepileall(av, 2, pr, &z);
    1638       52073 :     return z;
    1639             :   }
    1640    11018000 :   switch(typ(y_lead))
    1641             :   {
    1642             :     case t_REAL:
    1643           0 :       y_lead = ginv(y_lead);
    1644           0 :       f = gmul; mod = NULL;
    1645           0 :       break;
    1646             :     case t_INTMOD:
    1647        1378 :     case t_POLMOD: y_lead = ginv(y_lead);
    1648        1378 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1649        1378 :       break;
    1650    11016622 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1651    11016622 :       f = gdiv; mod = NULL;
    1652             :   }
    1653             : 
    1654    11018000 :   if (y_lead == NULL)
    1655     9243409 :     p2 = gel(x,dx+2);
    1656             :   else {
    1657             :     for(;;) {
    1658     1774591 :       p2 = f(gel(x,dx+2),y_lead);
    1659     1774591 :       p2 = simplify_shallow(p2);
    1660     1774591 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1661           0 :     }
    1662     1774591 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1663             :     {
    1664           0 :       if (pr == ONLY_DIVIDES) {
    1665           0 :         avma = av;
    1666           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1667             :       }
    1668           0 :       if (pr == ONLY_REM)
    1669             :       {
    1670           0 :         if (dx < 0)
    1671           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1672             :         else
    1673             :         {
    1674             :           GEN t;
    1675           0 :           avma = av;
    1676           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1677           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1678           0 :           return t;
    1679             :         }
    1680             :       }
    1681           0 :       if (pr) /* cf ONLY_REM above */
    1682             :       {
    1683           0 :         if (dx < 0)
    1684             :         {
    1685           0 :           p2 = gclone(p2);
    1686           0 :           avma = av;
    1687           0 :           z = pol_0(varn(x));
    1688           0 :           x = scalarpol(p2, varn(x));
    1689           0 :           gunclone(p2);
    1690             :         }
    1691             :         else
    1692             :         {
    1693             :           GEN t;
    1694           0 :           avma = av;
    1695           0 :           z = pol_0(varn(x));
    1696           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1697           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1698           0 :           x = t;
    1699             :         }
    1700           0 :         *pr = x;
    1701             :       }
    1702             :       else
    1703             :       {
    1704           0 :         avma = av;
    1705           0 :         z = pol_0(varn(x));
    1706             :       }
    1707           0 :       return z;
    1708             :     }
    1709             :   }
    1710             :   /* dx >= dy */
    1711    11018000 :   avy = avma;
    1712    11018000 :   dz = dx-dy;
    1713    11018000 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1714    11018000 :   x += 2;
    1715    11018000 :   z += 2;
    1716    11018000 :   y += 2;
    1717    11018000 :   gel(z,dz) = gcopy(p2);
    1718             : 
    1719    26951459 :   for (i=dx-1; i>=dy; i--)
    1720             :   {
    1721    15933459 :     av1=avma; p1=gel(x,i);
    1722    15933459 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1723    15933459 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1724             : 
    1725    15933459 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1726             :     else
    1727     6467761 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1728    15933459 :     gel(z,i-dy) = p1;
    1729             :   }
    1730    11018000 :   if (!pr) return gerepileupto(av,z-2);
    1731             : 
    1732     4540925 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1733     4878199 :   for (sx=0; ; i--)
    1734             :   {
    1735     4878199 :     p1 = gel(x,i);
    1736             :     /* we always enter this loop at least once */
    1737     4878199 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1738     4878199 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1739     4878199 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1740     3400685 :     if (!isexactzero(p1)) break;
    1741     3390895 :     if (!i) break;
    1742      337274 :     avma=av1;
    1743      337274 :   }
    1744     4540925 :   if (pr == ONLY_DIVIDES)
    1745             :   {
    1746        1302 :     if (sx) { avma=av; return NULL; }
    1747        1295 :     avma = (pari_sp)rem;
    1748        1295 :     return gerepileupto(av,z-2);
    1749             :   }
    1750     4539623 :   lr=i+3; rem -= lr;
    1751     4539623 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1752     4477530 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1753     4539623 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1754     4539623 :   rem[1] = z[-1];
    1755     4539623 :   rem += 2;
    1756     4539623 :   gel(rem,i) = p1;
    1757     5567336 :   for (i--; i>=0; i--)
    1758             :   {
    1759     1027713 :     av1=avma; p1 = gel(x,i);
    1760     1027713 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1761     1027713 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1762     1027713 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1763             :   }
    1764     4539623 :   rem -= 2;
    1765     4539623 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1766     4539623 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1767     3986986 :   z -= 2;
    1768             :   {
    1769     3986986 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1770     3986986 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1771             :   }
    1772             : }
    1773             : 
    1774             : GEN
    1775    10932020 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1776             : {
    1777    10932020 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1778    10932020 :   return RgX_divrem_i(x, y, pr);
    1779             : }
    1780             : 
    1781             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1782             : GEN
    1783       57247 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1784             : {
    1785             :   long vx, dx, dy, dz, i, j, sx, lr;
    1786             :   pari_sp av0, av, tetpil;
    1787             :   GEN z,p1,rem,lead;
    1788             : 
    1789       57247 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1790       57247 :   vx = varn(x);
    1791       57247 :   dx = degpol(x);
    1792       57247 :   dy = degpol(y);
    1793       57247 :   if (dx < dy)
    1794             :   {
    1795       15848 :     if (pr)
    1796             :     {
    1797       15848 :       av0 = avma; x = RgXQX_red(x, T);
    1798       15848 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1799       15848 :       if (pr == ONLY_REM) return x;
    1800           0 :       *pr = x;
    1801             :     }
    1802           0 :     return pol_0(vx);
    1803             :   }
    1804       41399 :   lead = leading_coeff(y);
    1805       41399 :   if (!dy) /* y is constant */
    1806             :   {
    1807         546 :     if (pr && pr != ONLY_DIVIDES)
    1808             :     {
    1809           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1810           0 :       *pr = pol_0(vx);
    1811             :     }
    1812         546 :     if (gequal1(lead)) return RgX_copy(x);
    1813           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1814           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1815             :   }
    1816       40853 :   av0 = avma; dz = dx-dy;
    1817       40853 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1818       40853 :   avma = av0;
    1819       40853 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1820       40853 :   x += 2; y += 2; z += 2;
    1821             : 
    1822       40853 :   p1 = gel(x,dx); av = avma;
    1823       40853 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1824      271604 :   for (i=dx-1; i>=dy; i--)
    1825             :   {
    1826      230751 :     av=avma; p1=gel(x,i);
    1827      230751 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1828      230751 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1829      230751 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1830             :   }
    1831       40853 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1832             : 
    1833       39607 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1834       64008 :   for (sx=0; ; i--)
    1835             :   {
    1836       64008 :     p1 = gel(x,i);
    1837       64008 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1838       64008 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1839       28539 :     if (!i) break;
    1840       24401 :     avma=av;
    1841       24401 :   }
    1842       39607 :   if (pr == ONLY_DIVIDES)
    1843             :   {
    1844        1716 :     if (lead) gunclone(lead);
    1845        1716 :     if (sx) { avma=av0; return NULL; }
    1846        1646 :     avma = (pari_sp)rem; return z-2;
    1847             :   }
    1848       37891 :   lr=i+3; rem -= lr;
    1849       37891 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1850       37891 :   rem[1] = z[-1];
    1851       37891 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1852       37891 :   rem += 2; gel(rem,i) = p1;
    1853      106715 :   for (i--; i>=0; i--)
    1854             :   {
    1855       68824 :     av=avma; p1 = gel(x,i);
    1856      260585 :     for (j=0; j<=i && j<=dz; j++)
    1857      191761 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1858       68824 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1859             :   }
    1860       37891 :   rem -= 2;
    1861       37891 :   if (lead) gunclone(lead);
    1862       37891 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1863       37891 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1864          84 :   *pr = rem; return z-2;
    1865             : }
    1866             : 
    1867             : /*******************************************************************/
    1868             : /*                                                                 */
    1869             : /*                        PSEUDO-DIVISION                          */
    1870             : /*                                                                 */
    1871             : /*******************************************************************/
    1872             : INLINE GEN
    1873      445395 : rem(GEN c, GEN T)
    1874             : {
    1875      445395 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1876      445395 :   return c;
    1877             : }
    1878             : 
    1879             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1880             : int
    1881        5919 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1882             : {
    1883             :   long dx, dy, dz, i, p, T_ismonic;
    1884        5919 :   pari_sp av = avma, av2;
    1885             :   GEN y_lead;
    1886             : 
    1887        5919 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1888        5919 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1889        5919 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1890             :   /* if monic, no point in using pseudo-division */
    1891        5919 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1892        3567 :   T_ismonic = gequal1(leading_coeff(T));
    1893        3567 :   dx = degpol(x);
    1894        3567 :   if (dx < dy) return !signe(x);
    1895        3567 :   (void)new_chunk(2);
    1896        3567 :   x = RgX_recip_shallow(x)+2;
    1897        3567 :   y = RgX_recip_shallow(y)+2;
    1898             :   /* pay attention to sparse divisors */
    1899        7316 :   for (i = 1; i <= dy; i++)
    1900        3749 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1901        3567 :   dz = dx-dy; p = dz+1;
    1902        3567 :   av2 = avma;
    1903             :   for (;;)
    1904             :   {
    1905       32213 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1906       32213 :     x0 = gneg(x0); p--;
    1907       32213 :     m = gcdii(cx, y0);
    1908       32213 :     if (!equali1(m))
    1909             :     {
    1910       31205 :       x0 = gdiv(x0, m);
    1911       31205 :       y0 = diviiexact(y0, m);
    1912       31205 :       if (equali1(y0)) y0 = NULL;
    1913             :     }
    1914       65616 :     for (i=1; i<=dy; i++)
    1915             :     {
    1916       33403 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1917       33403 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1918       33403 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1919       33403 :       gel(x,i) = c;
    1920             :     }
    1921      382876 :     for (   ; i<=dx; i++)
    1922             :     {
    1923      350663 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1924      350663 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1925      350663 :       gel(x,i) = c;
    1926             :     }
    1927       35871 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1928       32213 :     if (dx < dy) break;
    1929       28646 :     if (gc_needed(av2,1))
    1930             :     {
    1931           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1932           0 :       gerepilecoeffs(av2,x,dx+1);
    1933             :     }
    1934       28646 :   }
    1935        3567 :   avma = av; return (dx < 0);
    1936             : }
    1937             : 
    1938             : /* T either NULL or a t_POL. */
    1939             : GEN
    1940       24933 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1941             : {
    1942       24933 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1943       24933 :   pari_sp av = avma, av2;
    1944             :   GEN y_lead;
    1945             : 
    1946       24933 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1947       24933 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1948             :   /* if monic, no point in using pseudo-division */
    1949       24933 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1950       21426 :   dx = degpol(x);
    1951       21426 :   if (dx < dy) return RgX_copy(x);
    1952       21426 :   (void)new_chunk(2);
    1953       21426 :   x = RgX_recip_shallow(x)+2;
    1954       21426 :   y = RgX_recip_shallow(y)+2;
    1955             :   /* pay attention to sparse divisors */
    1956       66432 :   for (i = 1; i <= dy; i++)
    1957       45006 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1958       21426 :   dz = dx-dy; p = dz+1;
    1959       21426 :   av2 = avma;
    1960             :   for (;;)
    1961             :   {
    1962       81121 :     gel(x,0) = gneg(gel(x,0)); p--;
    1963      257635 :     for (i=1; i<=dy; i++)
    1964             :     {
    1965      176514 :       GEN c = gmul(y_lead, gel(x,i));
    1966      176514 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1967      176514 :       gel(x,i) = rem(c, T);
    1968             :     }
    1969      285320 :     for (   ; i<=dx; i++)
    1970             :     {
    1971      204199 :       GEN c = gmul(y_lead, gel(x,i));
    1972      204199 :       gel(x,i) = rem(c, T);
    1973             :     }
    1974       88597 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1975       81121 :     if (dx < dy) break;
    1976       59695 :     if (gc_needed(av2,1))
    1977             :     {
    1978           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1979           0 :       gerepilecoeffs(av2,x,dx+1);
    1980             :     }
    1981       59695 :   }
    1982       21426 :   if (dx < 0) return pol_0(vx);
    1983       19389 :   lx = dx+3; x -= 2;
    1984       19389 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1985       19389 :   x[1] = evalsigne(1) | evalvarn(vx);
    1986       19389 :   x = RgX_recip_shallow(x);
    1987       19389 :   if (p)
    1988             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1989        1176 :     GEN t = y_lead;
    1990        1176 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1991           0 :       t = RgXQ_powu(t, p, T);
    1992             :     else
    1993        1176 :       t = gpowgs(t, p);
    1994        4060 :     for (i=2; i<lx; i++)
    1995             :     {
    1996        2884 :       GEN c = gmul(gel(x,i), t);
    1997        2884 :       gel(x,i) = rem(c,T);
    1998             :     }
    1999        1176 :     if (!T) return gerepileupto(av, x);
    2000             :   }
    2001       18213 :   return gerepilecopy(av, x);
    2002             : }
    2003             : 
    2004             : GEN
    2005       24933 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2006             : 
    2007             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2008             : GEN
    2009        5319 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2010             : {
    2011        5319 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2012        5319 :   pari_sp av = avma, av2;
    2013             :   GEN z, r, ypow, y_lead;
    2014             : 
    2015        5319 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2016        5319 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2017        5319 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2018        4773 :   dx = degpol(x);
    2019        4773 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2020        4773 :   if (dx == dy)
    2021             :   {
    2022          28 :     GEN x_lead = gel(x,lg(x)-1);
    2023          28 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2024          28 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2025          28 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2026          28 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2027             :   }
    2028        4745 :   (void)new_chunk(2);
    2029        4745 :   x = RgX_recip_shallow(x)+2;
    2030        4745 :   y = RgX_recip_shallow(y)+2;
    2031             :   /* pay attention to sparse divisors */
    2032       23468 :   for (i = 1; i <= dy; i++)
    2033       18723 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2034        4745 :   dz = dx-dy; p = dz+1;
    2035        4745 :   lz = dz+3;
    2036        4745 :   z = cgetg(lz, t_POL);
    2037        4745 :   z[1] = evalsigne(1) | evalvarn(vx);
    2038        4745 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2039        4745 :   ypow = new_chunk(dz+1);
    2040        4745 :   gel(ypow,0) = gen_1;
    2041        4745 :   gel(ypow,1) = y_lead;
    2042        8021 :   for (i=2; i<=dz; i++)
    2043             :   {
    2044        3276 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2045        3276 :     gel(ypow,i) = rem(c,T);
    2046             :   }
    2047        4745 :   av2 = avma;
    2048        4745 :   for (iz=2;;)
    2049             :   {
    2050       10062 :     p--;
    2051       10062 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2052       44489 :     for (i=1; i<=dy; i++)
    2053             :     {
    2054       34427 :       GEN c = gmul(y_lead, gel(x,i));
    2055       34427 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2056       34427 :       gel(x,i) = rem(c, T);
    2057             :     }
    2058       24095 :     for (   ; i<=dx; i++)
    2059             :     {
    2060       14033 :       GEN c = gmul(y_lead, gel(x,i));
    2061       14033 :       gel(x,i) = rem(c,T);
    2062             :     }
    2063       10062 :     x++; dx--;
    2064       10062 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2065       10062 :     if (dx < dy) break;
    2066        5317 :     if (gc_needed(av2,1))
    2067             :     {
    2068           0 :       GEN X = x-2;
    2069           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2070           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2071           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2072             :     }
    2073        5317 :   }
    2074        4745 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2075        4745 :   if (dx < 0)
    2076          98 :     x = pol_0(vx);
    2077             :   else
    2078             :   {
    2079        4647 :     lx = dx+3; x -= 2;
    2080        4647 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2081        4647 :     x[1] = evalsigne(1) | evalvarn(vx);
    2082        4647 :     x = RgX_recip_shallow(x);
    2083             :   }
    2084        4745 :   z = RgX_recip_shallow(z);
    2085        4745 :   r = x;
    2086        4745 :   if (p)
    2087             :   {
    2088        1674 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2089        1674 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2090             :   }
    2091        4745 :   gerepileall(av, 2, &z, &r);
    2092        4745 :   *ptr = r; return z;
    2093             : }
    2094             : GEN
    2095        5158 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2096        5158 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2097             : 
    2098             : GEN
    2099       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2100             : {
    2101       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2102             : }
    2103             : GEN
    2104    69850077 : RgX_Rg_mul(GEN y, GEN x) {
    2105             :   long i, ly;
    2106    69850077 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2107    69850077 :   if (ly == 2) return z;
    2108    69700615 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2109    69700608 :   return normalizepol_lg(z,ly);
    2110             : }
    2111             : GEN
    2112       14217 : RgX_muls(GEN y, long x) {
    2113             :   long i, ly;
    2114       14217 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2115       14217 :   if (ly == 2) return z;
    2116       14182 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2117       14182 :   return normalizepol_lg(z,ly);
    2118             : }
    2119             : GEN
    2120        4417 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2121             : {
    2122        4417 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2123             : }
    2124             : GEN
    2125          56 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2126             : {
    2127          56 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2128             : }
    2129             : 
    2130             : GEN
    2131        1792 : RgXQX_sqr(GEN x, GEN T)
    2132             : {
    2133        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2134             : }
    2135             : 
    2136             : GEN
    2137         448 : RgXQX_powers(GEN P, long n, GEN T)
    2138             : {
    2139         448 :   GEN v = cgetg(n+2, t_VEC);
    2140             :   long i;
    2141         448 :   gel(v, 1) = pol_1(varn(T));
    2142         448 :   if (n==0) return v;
    2143         448 :   gel(v, 2) = gcopy(P);
    2144         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2145         448 :   return v;
    2146             : }
    2147             : 
    2148             : static GEN
    2149       65345 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2150             : static GEN
    2151           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2152             : static GEN
    2153      253610 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2154             : static GEN
    2155      104493 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2156             : static GEN
    2157      111013 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2158             : static GEN
    2159      105910 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2160             : static GEN
    2161         105 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2162             : static GEN
    2163       70826 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2164             : 
    2165             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2166             :               _mul, _sqr, _one, _zero };
    2167             : 
    2168             : GEN
    2169           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2170             : {
    2171           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2172             : }
    2173             : 
    2174             : GEN
    2175       45052 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2176             : {
    2177       45052 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2178       45052 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2179             : }
    2180             : 
    2181             : /* mod X^n */
    2182             : struct modXn {
    2183             :   long v; /* varn(X) */
    2184             :   long n;
    2185             : } ;
    2186             : static GEN
    2187        1876 : _sqrXn(void *data, GEN x) {
    2188        1876 :   struct modXn *S = (struct modXn*)data;
    2189        1876 :   return RgXn_sqr(x, S->n);
    2190             : }
    2191             : static GEN
    2192        1211 : _mulXn(void *data, GEN x, GEN y) {
    2193        1211 :   struct modXn *S = (struct modXn*)data;
    2194        1211 :   return RgXn_mul(x,y, S->n);
    2195             : }
    2196             : static GEN
    2197        1477 : _oneXn(void *data) {
    2198        1477 :   struct modXn *S = (struct modXn*)data;
    2199        1477 :   return pol_1(S->v);
    2200             : }
    2201             : static GEN
    2202           0 : _zeroXn(void *data) {
    2203           0 :   struct modXn *S = (struct modXn*)data;
    2204           0 :   return pol_0(S->v);
    2205             : }
    2206             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2207             :                                           _oneXn, _zeroXn };
    2208             : 
    2209             : GEN
    2210         357 : RgXn_powers(GEN x, long m, long n)
    2211             : {
    2212         357 :   long d = degpol(x);
    2213         357 :   int use_sqr = (d<<1) >= n;
    2214             :   struct modXn S;
    2215         357 :   S.v = varn(x); S.n = n;
    2216         357 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2217             : }
    2218             : 
    2219             : GEN
    2220        1575 : RgXn_powu_i(GEN x, ulong m, long n)
    2221             : {
    2222             :   struct modXn S;
    2223        1575 :   S.v = varn(x); S.n = n;
    2224        1575 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2225             : }
    2226             : GEN
    2227           0 : RgXn_powu(GEN x, ulong m, long n)
    2228             : {
    2229             :   struct modXn S;
    2230           0 :   S.v = varn(x); S.n = n;
    2231           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2232             : }
    2233             : 
    2234             : GEN
    2235         714 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2236             : {
    2237             :   struct modXn S;
    2238         714 :   S.v = varn(gel(x,2)); S.n = n;
    2239         714 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2240             : }
    2241             : 
    2242             : GEN
    2243           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2244             : {
    2245           0 :   int use_sqr = 2*degpol(x) >= n;
    2246             :   struct modXn S;
    2247           0 :   S.v = varn(x); S.n = n;
    2248           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2249             : }
    2250             : 
    2251             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2252             : GEN
    2253        1778 : RgXn_eval(GEN Q, GEN x, long n)
    2254             : {
    2255        1778 :   long d = degpol(x);
    2256             :   int use_sqr;
    2257             :   struct modXn S;
    2258        1778 :   if (d == 1 && isrationalzero(gel(x,2)))
    2259             :   {
    2260        1771 :     GEN y = RgX_unscale(Q, gel(x,3));
    2261        1771 :     setvarn(y, varn(x)); return y;
    2262             :   }
    2263           7 :   S.v = varn(x);
    2264           7 :   S.n = n;
    2265           7 :   use_sqr = (d<<1) >= n;
    2266           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2267             : }
    2268             : 
    2269             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2270             : static GEN
    2271       34628 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2272             : {
    2273       34628 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2274       34628 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2275             : }
    2276             : 
    2277             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2278             : static GEN
    2279           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2280             : {
    2281           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2282           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2283             : }
    2284             : 
    2285             : GEN
    2286      125090 : RgXn_inv(GEN f, long e)
    2287             : {
    2288      125090 :   pari_sp av = avma, av2;
    2289             :   ulong mask;
    2290             :   GEN W, a;
    2291      125090 :   long v = varn(f), n = 1;
    2292             : 
    2293      125090 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2294      125090 :   a = ginv(gel(f,2));
    2295      125090 :   if (e == 1) return scalarpol(a, v);
    2296      125069 :   else if (e == 2)
    2297             :   {
    2298             :     GEN b;
    2299      111104 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2300       91826 :     b = gneg(b);
    2301       91826 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2302       91826 :     W = deg1pol_shallow(b, a, v);
    2303       91826 :     return gerepilecopy(av, W);
    2304             :   }
    2305       13965 :   W = scalarpol_shallow(ginv(gel(f,2)),v);
    2306       13965 :   mask = quadratic_prec_mask(e);
    2307       13965 :   av2 = avma;
    2308       62558 :   for (;mask>1;)
    2309             :   {
    2310             :     GEN u, fr;
    2311       34628 :     long n2 = n;
    2312       34628 :     n<<=1; if (mask & 1) n--;
    2313       34628 :     mask >>= 1;
    2314       34628 :     fr = RgXn_red_shallow(f, n);
    2315       34628 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2316       34628 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2317       34628 :     if (gc_needed(av2,2))
    2318             :     {
    2319           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2320           0 :       W = gerepileupto(av2, W);
    2321             :     }
    2322             :   }
    2323       13965 :   return gerepileupto(av, W);
    2324             : }
    2325             : 
    2326             : GEN
    2327       12810 : RgXn_exp(GEN h, long e)
    2328             : {
    2329       12810 :   pari_sp av = avma, av2;
    2330       12810 :   long v = varn(h), n=1;
    2331       12810 :   GEN f = pol_1(v), g = pol_1(v);
    2332       12810 :   ulong mask = quadratic_prec_mask(e);
    2333       12810 :   av2 = avma;
    2334       12810 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2335           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2336       39606 :   for (;mask>1;)
    2337             :   {
    2338             :     GEN q, w;
    2339       13986 :     long n2 = n;
    2340       13986 :     n<<=1; if (mask & 1) n--;
    2341       13986 :     mask >>= 1;
    2342       13986 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2343       13986 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2344       13986 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2345       13986 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2346       13986 :     if (gc_needed(av2,2))
    2347             :     {
    2348           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2349           0 :       gerepileall(av2, 2, &f, &g);
    2350             :     }
    2351             :   }
    2352       12810 :   return gerepileupto(av, f);
    2353             : }
    2354             : 
    2355             : GEN
    2356          91 : RgXn_reverse(GEN f, long e)
    2357             : {
    2358          91 :   pari_sp av = avma, av2;
    2359             :   ulong mask;
    2360             :   GEN fi, a, df, W, an;
    2361          91 :   long v = varn(f), n=1;
    2362          91 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2363           0 :     pari_err_INV("serreverse",f);
    2364          91 :   fi = ginv(gel(f,3));
    2365          91 :   a = deg1pol_shallow(fi,gen_0,v);
    2366          91 :   if (e <= 2) return gerepilecopy(av, a);
    2367          91 :   W = scalarpol(fi,v);
    2368          91 :   df = RgX_deriv(f);
    2369          91 :   mask = quadratic_prec_mask(e);
    2370          91 :   av2 = avma;
    2371         539 :   for (;mask>1;)
    2372             :   {
    2373             :     GEN u, fa, fr;
    2374         357 :     long n2 = n, rt;
    2375         357 :     n<<=1; if (mask & 1) n--;
    2376         357 :     mask >>= 1;
    2377         357 :     fr = RgXn_red_shallow(f, n);
    2378         357 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2379         357 :     an = RgXn_powers(a, rt, n);
    2380         357 :     if (n>1)
    2381             :     {
    2382         357 :       long n4 = (n2+1)>>1;
    2383         357 :       GEN dfr = RgXn_red_shallow(df, n2);
    2384         357 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2385         357 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2386         357 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2387             :     }
    2388         357 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2389         357 :     fa = RgX_shift(fa, -n2);
    2390         357 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2391         357 :     if (gc_needed(av2,2))
    2392             :     {
    2393           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2394           0 :       gerepileall(av2, 2, &a, &W);
    2395             :     }
    2396             :   }
    2397          91 :   return gerepileupto(av, a);
    2398             : }
    2399             : 
    2400             : GEN
    2401           0 : RgXn_sqrt(GEN h, long e)
    2402             : {
    2403           0 :   pari_sp av = avma, av2;
    2404           0 :   long v = varn(h), n = 1;
    2405           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2406           0 :   ulong mask = quadratic_prec_mask(e);
    2407           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2408           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2409           0 :   av2 = avma;
    2410             :   while(1)
    2411             :   {
    2412           0 :     long n2 = n, m;
    2413             :     GEN g;
    2414           0 :     n<<=1; if (mask & 1) n--;
    2415           0 :     mask >>= 1;
    2416           0 :     m = n-n2;
    2417           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2418           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2419           0 :     if (mask==1) return gerepileupto(av, f);
    2420           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2421           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2422           0 :     if (gc_needed(av2,2))
    2423             :     {
    2424           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2425           0 :       gerepileall(av2, 2, &f, &df);
    2426             :     }
    2427           0 :   }
    2428             : }
    2429             : 
    2430             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2431             : GEN
    2432      209134 : RgXQ_powu(GEN x, ulong n, GEN T)
    2433             : {
    2434             :   pari_sp av;
    2435             :   GEN y;
    2436             : 
    2437      209134 :   if (!n) return pol_1(varn(x));
    2438      207559 :   if (n == 1) return RgX_copy(x);
    2439      143435 :   av = avma;
    2440      143435 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2441      143436 :   return gerepileupto(av, y);
    2442             : }
    2443             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2444             : GEN
    2445       18136 : RgXQ_pow(GEN x, GEN n, GEN T)
    2446             : {
    2447             :   pari_sp av;
    2448       18136 :   long s = signe(n);
    2449             :   GEN y;
    2450             : 
    2451       18136 :   if (!s) return pol_1(varn(x));
    2452       18136 :   if (is_pm1(n) == 1)
    2453           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2454       18136 :   av = avma;
    2455       18136 :   if (s < 0) x = RgXQ_inv(x, T);
    2456       18136 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2457       18136 :   return gerepileupto(av, y);
    2458             : }
    2459             : 
    2460             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2461             : GEN
    2462        2401 : RgXQ_powers(GEN x, long l, GEN T)
    2463             : {
    2464        2401 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2465        2401 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2466             : }
    2467             : 
    2468             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2469             : GEN
    2470        1939 : QXQ_powers(GEN a, long n, GEN T)
    2471             : {
    2472        1939 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2473             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2474        1939 :   if (den)
    2475             :   { /* restore denominators */
    2476        1302 :     GEN d = den;
    2477             :     long i;
    2478        1302 :     gel(v,2) = a;
    2479        3913 :     for (i=3; i<=n+1; i++) {
    2480        2611 :       d = mulii(d,den);
    2481        2611 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2482             :     }
    2483             :   }
    2484        1939 :   return v;
    2485             : }
    2486             : 
    2487             : static GEN
    2488        1253 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2489             : {
    2490        1253 :   long l, i, m = 0;
    2491             :   GEN dz, z;
    2492        1253 :   GEN V = cgetg_copy(v, &l);
    2493        4011 :   for (i = imin; i < l; i++)
    2494             :   {
    2495        2758 :     GEN c = gel(v, i);
    2496        2758 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2497             :   }
    2498        1253 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2499        1253 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2500        4011 :   for (i = imin; i < l; i++)
    2501             :   {
    2502        2758 :     GEN c = gel(v,i);
    2503        2758 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2504        2758 :     gel(V,i) = c;
    2505             :   }
    2506        1253 :   return V;
    2507             : }
    2508             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2509             : GEN
    2510        1190 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2511        1190 : { return do_QXQ_eval(v, 1, a, T); }
    2512             : GEN
    2513          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2514          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2515             : 
    2516             : GEN
    2517         287 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2518             : {
    2519         287 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2520             : }
    2521             : 
    2522             : GEN
    2523          56 : RgXQ_minpoly_naive(GEN y, GEN P)
    2524             : {
    2525          56 :   pari_sp ltop=avma;
    2526          56 :   long n=lgpol(P);
    2527          56 :   GEN M=ker(RgXQ_matrix_pow(y,n,n,P));
    2528          56 :   M=content(RgM_to_RgXV(M,varn(P)));
    2529          56 :   return gerepileupto(ltop,M);
    2530             : }
    2531             : 
    2532             : GEN
    2533       34738 : RgXQ_norm(GEN x, GEN T)
    2534             : {
    2535             :   pari_sp av;
    2536       34738 :   long dx = degpol(x);
    2537             :   GEN L, y;
    2538             : 
    2539       34738 :   av = avma; y = resultant(T, x);
    2540       34738 :   L = leading_coeff(T);
    2541       34738 :   if (gequal1(L) || !signe(x)) return y;
    2542           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2543             : }
    2544             : 
    2545             : GEN
    2546      104740 : RgX_blocks(GEN P, long n, long m)
    2547             : {
    2548      104740 :   GEN z = cgetg(m+1,t_VEC);
    2549      104740 :   long i,j, k=2, l = lg(P);
    2550      507392 :   for(i=1; i<=m; i++)
    2551             :   {
    2552      402652 :     GEN zi = cgetg(n+2,t_POL);
    2553      402652 :     zi[1] = P[1];
    2554      402652 :     gel(z,i) = zi;
    2555     2377553 :     for(j=2; j<n+2; j++)
    2556     1974901 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2557      402652 :     zi = RgX_renormalize_lg(zi, n+2);
    2558             :   }
    2559      104740 :   return z;
    2560             : }
    2561             : 
    2562             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2563             : void
    2564       46546 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2565             : {
    2566       46546 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2567             :   GEN p0, p1;
    2568             : 
    2569       93096 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2570             : 
    2571       46548 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2572       46548 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2573       46547 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2574     2037716 :   for (i=0; i<n1; i++)
    2575             :   {
    2576     1991166 :     p0[2+i] = p[2+(i<<1)];
    2577     1991166 :     p1[2+i] = p[3+(i<<1)];
    2578             :   }
    2579       46550 :   if (n1 != n0)
    2580       29819 :     p0[2+i] = p[2+(i<<1)];
    2581       46550 :   *pe = normalizepol(p0);
    2582       46545 :   *po = normalizepol(p1);
    2583             : }
    2584             : 
    2585             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2586             : GEN
    2587       40614 : RgX_splitting(GEN p, long k)
    2588             : {
    2589       40614 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2590             :   GEN r;
    2591             : 
    2592       40614 :   m = n/k;
    2593       40614 :   r = cgetg(k+1,t_VEC);
    2594      223930 :   for(i=1; i<=k; i++)
    2595             :   {
    2596      183316 :     gel(r,i) = cgetg(m+3, t_POL);
    2597      183316 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2598             :   }
    2599      552426 :   for (j=1, i=0, l=2; i<=n; i++)
    2600             :   {
    2601      511812 :     gmael(r,j,l) = gel(p,2+i);
    2602      511812 :     if (j==k) { j=1; l++; } else j++;
    2603             :   }
    2604      223930 :   for(i=1; i<=k; i++)
    2605      183316 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2606       40614 :   return r;
    2607             : }
    2608             : 
    2609             : /*******************************************************************/
    2610             : /*                                                                 */
    2611             : /*                        Kronecker form                           */
    2612             : /*                                                                 */
    2613             : /*******************************************************************/
    2614             : 
    2615             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2616             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2617             :  * normalized coefficients */
    2618             : GEN
    2619      124923 : Kronecker_to_mod(GEN z, GEN T)
    2620             : {
    2621      124923 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2622      124923 :   GEN x, t = cgetg(N,t_POL);
    2623      124923 :   t[1] = T[1];
    2624      124923 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2625      124923 :   x[1] = z[1];
    2626      124923 :   T = RgX_copy(T);
    2627     1032521 :   for (i=2; i<lx+2; i++, z+= N-2)
    2628             :   {
    2629      907598 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2630      907598 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2631             :   }
    2632      124923 :   N = (l-2) % (N-2) + 2;
    2633      124923 :   for (j=2; j<N; j++) t[j] = z[j];
    2634      124923 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2635      124923 :   return normalizepol_lg(x, i+1);
    2636             : }
    2637             : 
    2638             : /*******************************************************************/
    2639             : /*                                                                 */
    2640             : /*                        Domain detection                         */
    2641             : /*                                                                 */
    2642             : /*******************************************************************/
    2643             : 
    2644             : static GEN
    2645      255459 : zero_FpX_mod(GEN p, long v)
    2646             : {
    2647      255459 :   GEN r = cgetg(3,t_POL);
    2648      255459 :   r[1] = evalvarn(v);
    2649      255459 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2650      255459 :   return r;
    2651             : }
    2652             : 
    2653             : static GEN
    2654      404522 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2655             : {
    2656      404522 :   pari_sp av = avma;
    2657             :   GEN r;
    2658      404522 :   if (lgefint(p) == 3)
    2659             :   {
    2660      344930 :     ulong pp = uel(p, 2);
    2661      344930 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2662             :                                   RgX_to_Flx(y, pp), pp));
    2663             :   }
    2664             :   else
    2665       59592 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2666      404522 :   if (signe(r)==0)
    2667      161729 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2668      242793 :   return gerepileupto(av, FpX_to_mod(r, p));
    2669             : }
    2670             : 
    2671             : static GEN
    2672           7 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2673             : {
    2674           7 :   GEN r = cgetg(3,t_POL);
    2675           7 :   r[1] = evalvarn(v);
    2676           7 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2677           7 :   return r;
    2678             : }
    2679             : 
    2680             : static GEN
    2681          63 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2682             : {
    2683          63 :   pari_sp av = avma;
    2684          63 :   GEN T = RgX_to_FpX(pol, p);
    2685          63 :   long dT = degpol(T);
    2686          63 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2687          63 :   GEN ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2688          63 :   GEN r = FpX_mul(kx, ky, p);
    2689          63 :   if (signe(r)==0)
    2690           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2691          63 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2692             : }
    2693             : 
    2694             : static GEN
    2695      309175 : RgX_liftred(GEN x, GEN T)
    2696      309175 : { return RgXQX_red(liftpol_shallow(x), T); }
    2697             : 
    2698             : static GEN
    2699      120944 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2700             : {
    2701      120944 :   pari_sp av = avma;
    2702      120944 :   long dT = degpol(T);
    2703      120944 :   GEN r = QX_mul(ZXX_to_Kronecker(RgX_liftred(x, T), dT),
    2704             :                  ZXX_to_Kronecker(RgX_liftred(y, T), dT));
    2705      120944 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2706             : }
    2707             : 
    2708             : static GEN
    2709        1253 : RgX_sqr_FpX(GEN x, GEN p)
    2710             : {
    2711        1253 :   pari_sp av = avma;
    2712             :   GEN r;
    2713        1253 :   if (lgefint(p) == 3)
    2714             :   {
    2715        1049 :     ulong pp = uel(p, 2);
    2716        1049 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2717             :   }
    2718             :   else
    2719         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2720        1253 :   if (signe(r)==0)
    2721          98 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2722        1155 :   return gerepileupto(av, FpX_to_mod(r, p));
    2723             : }
    2724             : 
    2725             : static GEN
    2726         147 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2727             : {
    2728         147 :   pari_sp av = avma;
    2729         147 :   GEN T = RgX_to_FpX(pol, p);
    2730         147 :   long dT = degpol(T);
    2731         147 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2732         147 :   GEN r = FpX_sqr(kx, p);
    2733         147 :   if (signe(r)==0)
    2734           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2735         147 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2736             : }
    2737             : 
    2738             : static GEN
    2739        3769 : RgX_sqr_QXQX(GEN x, GEN T)
    2740             : {
    2741        3769 :   pari_sp av = avma;
    2742        3769 :   long dT = degpol(T);
    2743        3769 :   GEN r = QX_sqr(ZXX_to_Kronecker(RgX_liftred(x, T), dT));
    2744        3769 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2745             : }
    2746             : 
    2747             : static GEN
    2748      327754 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2749             : {
    2750      327754 :   pari_sp av = avma;
    2751             :   GEN r;
    2752      327754 :   if (lgefint(p) == 3)
    2753             :   {
    2754      321195 :     ulong pp = uel(p, 2);
    2755      321195 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2756             :                                   RgX_to_Flx(y, pp), pp));
    2757             :   }
    2758             :   else
    2759        6559 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2760      327754 :   if (signe(r)==0)
    2761       93632 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2762      234122 :   return gerepileupto(av, FpX_to_mod(r, p));
    2763             : }
    2764             : 
    2765             : static GEN
    2766       31759 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    2767             : {
    2768       31759 :   pari_sp av = avma;
    2769             :   GEN r;
    2770       31759 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    2771       31759 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    2772             : }
    2773             : static GEN
    2774          42 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2775             : {
    2776          42 :   pari_sp av = avma;
    2777             :   GEN r;
    2778          42 :   GEN T = RgX_to_FpX(pol, p);
    2779          42 :   if (lgefint(p) == 3)
    2780             :   {
    2781          42 :     ulong pp = uel(p, 2);
    2782          42 :     GEN Tp = ZX_to_Flx(T, pp);
    2783          42 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2784             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2785             :   }
    2786             :   else
    2787           0 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2788          42 :   if (signe(r)==0)
    2789           7 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2790          35 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2791             : }
    2792             : 
    2793             : #define code(t1,t2) ((t1 << 6) | t2)
    2794             : static GEN
    2795    40640531 : RgX_mul_fast(GEN x, GEN y)
    2796             : {
    2797             :   GEN p, pol;
    2798             :   long pa;
    2799    40640531 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2800    40640481 :   switch(t)
    2801             :   {
    2802    30060945 :     case t_INT:    return ZX_mul(x,y);
    2803      931067 :     case t_FRAC:   return QX_mul(x,y);
    2804      100808 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2805      404522 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2806             :     case code(t_POLMOD, t_INT):
    2807             :     case code(t_POLMOD, t_FRAC):
    2808      120944 :                    return RgX_mul_QXQX(x, y, pol);
    2809             :     case code(t_POLMOD, t_INTMOD):
    2810          63 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2811     9022132 :     default:       return NULL;
    2812             :   }
    2813             : }
    2814             : static GEN
    2815     2832109 : RgX_sqr_fast(GEN x)
    2816             : {
    2817             :   GEN p, pol;
    2818             :   long pa;
    2819     2832109 :   long t = RgX_type(x,&p,&pol,&pa);
    2820     2832233 :   switch(t)
    2821             :   {
    2822     2774039 :     case t_INT:    return ZX_sqr(x);
    2823       45150 :     case t_FRAC:   return QX_sqr(x);
    2824        2254 :     case t_FFELT:  return FFX_sqr(x, pol);
    2825        1253 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2826             :     case code(t_POLMOD, t_INT):
    2827             :     case code(t_POLMOD, t_FRAC):
    2828        3769 :                    return RgX_sqr_QXQX(x, pol);
    2829             :     case code(t_POLMOD, t_INTMOD):
    2830         147 :                    return RgX_sqr_FpXQX(x, pol, p);
    2831        5621 :     default:       return NULL;
    2832             :   }
    2833             : }
    2834             : 
    2835             : static GEN
    2836     6475803 : RgX_rem_fast(GEN x, GEN y)
    2837             : {
    2838             :   GEN p, pol;
    2839             :   long pa;
    2840     6475803 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2841     6475960 :   switch(t)
    2842             :   {
    2843     4286221 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    2844     1094704 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    2845        1897 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2846      327754 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2847             :     case code(t_POLMOD, t_INT):
    2848             :     case code(t_POLMOD, t_FRAC):
    2849       31759 :                    return RgX_rem_QXQX(x, y, pol);
    2850             :     case code(t_POLMOD, t_INTMOD):
    2851          42 :                    return RgX_rem_FpXQX(x, y, pol, p);
    2852      733583 :     default:       return NULL;
    2853             :   }
    2854             : }
    2855             : 
    2856             : #undef code
    2857             : 
    2858             : GEN
    2859    36858868 : RgX_mul(GEN x, GEN y)
    2860             : {
    2861    36858868 :   GEN z = RgX_mul_fast(x,y);
    2862    36858789 :   if (!z) z = RgX_mul_i(x,y);
    2863    36858804 :   return z;
    2864             : }
    2865             : 
    2866             : GEN
    2867     2816034 : RgX_sqr(GEN x)
    2868             : {
    2869     2816034 :   GEN z = RgX_sqr_fast(x);
    2870     2815689 :   if (!z) z = RgX_sqr_i(x);
    2871     2815764 :   return z;
    2872             : }
    2873             : 
    2874             : GEN
    2875     6475824 : RgX_rem(GEN x, GEN y)
    2876             : {
    2877     6475824 :   GEN z = RgX_rem_fast(x, y);
    2878     6475854 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    2879     6475865 :   return z;
    2880             : }
    2881             : 
    2882             : GEN
    2883     3747057 : RgXn_mul(GEN f, GEN g, long n)
    2884             : {
    2885     3747057 :   pari_sp av = avma;
    2886     3747057 :   GEN h = RgX_mul_fast(f,g);
    2887     3747057 :   if (!h) return RgXn_mul2(f,g,n);
    2888     2017968 :   if (degpol(h) < n) return h;
    2889     1295553 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    2890             : }
    2891             : 
    2892             : GEN
    2893       16065 : RgXn_sqr(GEN f, long n)
    2894             : {
    2895       16065 :   pari_sp av = avma;
    2896       16065 :   GEN g = RgX_sqr_fast(f);
    2897       16065 :   if (!g) return RgXn_sqr2(f,n);
    2898       15827 :   if (degpol(g) < n) return g;
    2899         763 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    2900             : }
    2901             : 
    2902             : /* (f*g) \/ x^n */
    2903             : GEN
    2904       34628 : RgX_mulhigh_i(GEN f, GEN g, long n)
    2905             : {
    2906       34628 :   GEN h = RgX_mul_fast(f,g);
    2907       34628 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    2908             : }
    2909             : 
    2910             : /* (f*g) \/ x^n */
    2911             : GEN
    2912           0 : RgX_sqrhigh_i(GEN f, long n)
    2913             : {
    2914           0 :   GEN h = RgX_sqr_fast(f);
    2915           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    2916             : }

Generated by: LCOV version 1.11