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 21739-44c3c17) Lines: 1457 1612 90.4 %
Date: 2018-01-20 06:18:48 Functions: 171 184 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /*******************************************************************/
      18             : /*                                                                 */
      19             : /*                         GENERIC                                 */
      20             : /*                                                                 */
      21             : /*******************************************************************/
      22             : 
      23             : /* Return optimal parameter l for the evaluation of n/m polynomials of degree d
      24             :    Fractional values can be used if the evaluations are done with different
      25             :    accuracies, and thus have different weights.
      26             :  */
      27             : long
      28     2285632 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     2285632 :   long pold=1, rold=n*(d-1);
      32    12822118 :   for(p=2; p<=d; p++)
      33             :   {
      34    10536486 :     r = m*(p-1) + n*((d-1)/p);
      35    10536486 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     2285632 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41    10165262 : 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    10165262 :   pari_sp av = avma;
      45             :   long i;
      46    10165262 :   GEN z = cmul(E,P,a,ff->one(E));
      47    10165282 :   if (!z) z = gen_0;
      48    63788452 :   for (i=1; i<=n; i++)
      49             :   {
      50    53623207 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    53623945 :     if (t) {
      52    51845009 :       z = ff->add(E, z, t);
      53    51843962 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56    10165245 :   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     6479413 : 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     6479413 :   pari_sp av = avma;
      69     6479413 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     6479413 :   if (d < 0) return ff->zero(E);
      73     5984444 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     2428028 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     2428028 :   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     2428028 :   d -= l;
      81     2428028 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     6608840 :   while (d >= l-1)
      83             :   {
      84     1752789 :     d -= l-1;
      85     1752789 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86     1752758 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87     1752783 :     if (gc_needed(av,2))
      88          61 :       z = gerepileupto(av, z);
      89             :   }
      90     2428025 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91     2428027 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92     2428025 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96     1077031 : 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     1077031 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102     1077031 :   if (d < 0) return ff->zero(E);
     103     1076744 :   rtd = (long) sqrt((double)d);
     104     1076744 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105     1076742 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106     1076741 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      877523 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    21638488 : _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      749653 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      243063 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      895107 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       14588 : _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      367381 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      367381 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      129927 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      129927 :   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      113075 : get_Rg_algebra(void)
     158             : {
     159      113075 :   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     4010872 : RgX_equal(GEN x, GEN y)
     188             : {
     189     4010872 :   long i = lg(x);
     190             : 
     191     4010872 :   if (i != lg(y)) return 0;
     192    21606320 :   for (i--; i > 1; i--)
     193    17653555 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194     3952765 :   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     2062045 : Rg_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203     2062045 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     204     2062045 :   if (RgX_type_is_composite(tx))
     205      376208 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206     2062045 :   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     2061828 :     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      128975 : Rg_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220      128975 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     221      128975 :   if (RgX_type_is_composite(tx))
     222       25067 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223      128975 :   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      128464 :     default: return gen_0;
     229             :   }
     230             : }
     231             : 
     232             : GEN
     233        2590 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     234             : {
     235        2590 :   long i, n = degpol(P);
     236             :   GEN z, dz, dP;
     237        2590 :   if (n < 0) return gen_0;
     238        2590 :   P = Q_remove_denom(P, &dP);
     239        2590 :   z = gel(P,2); if (n == 0) return icopy(z);
     240        1393 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     241        1393 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     242        1393 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     243        1393 :   dz = mul_denom(dP, dV);
     244        1393 :   return dz? RgX_Rg_div(z, dz): z;
     245             : }
     246             : 
     247             : /* Return P(h * x), not memory clean */
     248             : GEN
     249        5628 : RgX_unscale(GEN P, GEN h)
     250             : {
     251        5628 :   long i, l = lg(P);
     252        5628 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253        5628 :   Q[1] = P[1];
     254        5628 :   if (l == 2) return Q;
     255        5600 :   gel(Q,2) = gcopy(gel(P,2));
     256       27433 :   for (i=3; i<l; i++)
     257             :   {
     258       21833 :     hi = gmul(hi,h);
     259       21833 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261        5600 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      322392 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      322392 :   long i, l = lg(P);
     268      322392 :   GEN Q = cgetg(l, t_POL);
     269      322392 :   Q[1] = P[1];
     270      322392 :   if (l == 2) return Q;
     271      322392 :   gel(Q,2) = gel(P,2);
     272      322392 :   if (l == 3) return Q;
     273      322392 :   if (h == -1)
     274      308511 :     for (i = 3; i < l; i++)
     275             :     {
     276      300614 :       gel(Q,i) = negi(gel(P,i));
     277      300614 :       if (++i == l) break;
     278      297367 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      311248 :     gel(Q,3) = mulis(gel(P,3), h);
     284      311248 :     hi = sqrs(h);
     285      659407 :     for (i = 4; i < l; i++)
     286             :     {
     287      348159 :       gel(Q,i) = mulii(gel(P,i), hi);
     288      348159 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      322392 :   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       18553 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       18553 :   long i, ni = n, l = lg(P);
     319       18553 :   GEN Q = cgetg(l, t_POL);
     320       18553 :   Q[1] = P[1];
     321       18553 :   if (l == 2) return Q;
     322       18553 :   gel(Q,2) = gel(P,2);
     323       18553 :   if (l == 3) return Q;
     324       18553 :   gel(Q,3) = shifti(gel(P,3), ni);
     325       75569 :   for (i=4; i<l; i++)
     326             :   {
     327       57016 :     ni += n;
     328       57016 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       18553 :   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        1855 : RgX_rescale(GEN P, GEN h)
     368             : {
     369        1855 :   long i, l = lg(P);
     370        1855 :   GEN Q = cgetg(l,t_POL), hi = h;
     371        1855 :   Q[l-1] = P[l-1];
     372        8694 :   for (i=l-2; i>=2; i--)
     373             :   {
     374        8694 :     gel(Q,i) = gmul(gel(P,i), hi);
     375        8694 :     if (i == 2) break;
     376        6839 :     hi = gmul(hi,h);
     377             :   }
     378        1855 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      115263 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      115263 :   long i,id, dy, dx = degpol(x0);
     387      115263 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       65105 :   dy = dx/d;
     389       65105 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       65105 :   z = y + 2;
     391       65105 :   x = x0+ 2;
     392       65105 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       65105 :   return y;
     394             : }
     395             : 
     396             : /* return x0(X^d) */
     397             : GEN
     398      379775 : RgX_inflate(GEN x0, long d)
     399             : {
     400      379775 :   long i, id, dy, dx = degpol(x0);
     401      379775 :   GEN x = x0 + 2, z, y;
     402      379775 :   if (dx <= 0) return leafcopy(x0);
     403      377689 :   dy = dx*d;
     404      377689 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     405      377689 :   z = y + 2;
     406      377689 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     407      377689 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     408      377689 :   return y;
     409             : }
     410             : 
     411             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     412             : GEN
     413     1048904 : RgX_translate(GEN P, GEN c)
     414             : {
     415     1048904 :   pari_sp av = avma;
     416             :   GEN Q, *R;
     417             :   long i, k, n;
     418             : 
     419     1048904 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     420     1047417 :   Q = leafcopy(P);
     421     1047417 :   R = (GEN*)(Q+2); n = degpol(P);
     422     1047417 :   if (equali1(c))
     423             :   {
     424        8652 :     for (i=1; i<=n; i++)
     425             :     {
     426        6713 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], R[k+1]);
     427        6713 :       if (gc_needed(av,2))
     428             :       {
     429           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(1), i = %ld/%ld", i,n);
     430           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     431             :       }
     432             :     }
     433             :   }
     434     1045478 :   else if (equalim1(c))
     435             :   {
     436      141162 :     for (i=1; i<=n; i++)
     437             :     {
     438      120008 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     439      120008 :       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     3538604 :     for (i=1; i<=n; i++)
     449             :     {
     450     2514280 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     451     2514280 :       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     1047417 :   return gerepilecopy(av, Q);
     459             : }
     460             : 
     461             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     462             : GEN
     463      377372 : ZX_translate(GEN P, GEN c)
     464             : {
     465      377372 :   pari_sp av = avma;
     466             :   GEN Q, *R;
     467             :   long i, k, n;
     468             : 
     469      377372 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     470      377337 :   Q = leafcopy(P);
     471      377337 :   R = (GEN*)(Q+2); n = degpol(P);
     472      377337 :   if (equali1(c))
     473             :   {
     474     2375515 :     for (i=1; i<=n; i++)
     475             :     {
     476     2101514 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     477     2101514 :       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      377337 :   return gerepilecopy(av, Q);
     509             : }
     510             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     511             : GEN
     512        6083 : RgXQX_translate(GEN P, GEN c, GEN T)
     513             : {
     514        6083 :   pari_sp av = avma;
     515             :   GEN Q, *R;
     516             :   long i, k, n;
     517             : 
     518        6083 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     519        6083 :   Q = leafcopy(P);
     520        6083 :   R = (GEN*)(Q+2); n = degpol(P);
     521       34993 :   for (i=1; i<=n; i++)
     522             :   {
     523      142261 :     for (k=n-i; k<n; k++)
     524             :     {
     525      113351 :       pari_sp av2 = avma;
     526      113351 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     527             :     }
     528       28910 :     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        6083 :   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       18232 : QXQ_to_mod_copy(GEN x, GEN T)
     547             : {
     548             :   long d;
     549       18232 :   switch(typ(x))
     550             :   {
     551        7273 :     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      849546 : QXQ_to_mod(GEN x, GEN T)
     565             : {
     566             :   long d;
     567      849546 :   switch(typ(x))
     568             :   {
     569             :     case t_INT:
     570      399455 :     case t_FRAC: return x;
     571             :     case t_POL:
     572      450091 :       d = degpol(x);
     573      450091 :       if (d < 0) return gen_0;
     574      343621 :       if (d == 0) return gel(x,2);
     575      327585 :       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        1974 : QXQX_to_mod(GEN z, GEN T)
     584             : {
     585        1974 :   long i,l = lg(z);
     586        1974 :   GEN x = cgetg(l,t_POL);
     587        1974 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     588        1974 :   x[1] = z[1]; return normalizepol_lg(x,l);
     589             : }
     590             : /* pure shallow version */
     591             : GEN
     592      121324 : QXQX_to_mod_shallow(GEN z, GEN T)
     593             : {
     594      121324 :   long i,l = lg(z);
     595      121324 :   GEN x = cgetg(l,t_POL);
     596      121324 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     597      121324 :   x[1] = z[1]; return normalizepol_lg(x,l);
     598             : }
     599             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     600             : GEN
     601         616 : QXQXV_to_mod(GEN V, GEN T)
     602             : {
     603         616 :   long i, l = lg(V);
     604         616 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     605         616 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     606         616 :   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       16667 : QXQC_to_mod_shallow(GEN V, GEN T)
     621             : {
     622       16667 :   long i, l = lg(V);
     623       16667 :   GEN z = cgetg(l, t_COL);
     624       16667 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     625       16667 :   return z;
     626             : }
     627             : 
     628             : GEN
     629        5901 : QXQM_to_mod_shallow(GEN V, GEN T)
     630             : {
     631        5901 :   long i, l = lg(V);
     632        5901 :   GEN z = cgetg(l, t_MAT);
     633        5901 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     634        5901 :   return z;
     635             : }
     636             : 
     637             : GEN
     638      752694 : RgX_renormalize_lg(GEN x, long lx)
     639             : {
     640             :   long i;
     641     2049268 :   for (i = lx-1; i>1; i--)
     642     1918032 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     643      752694 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     644      752694 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     645             : }
     646             : 
     647             : GEN
     648      448575 : RgV_to_RgX(GEN x, long v)
     649             : {
     650      448575 :   long i, k = lg(x);
     651             :   GEN p;
     652             : 
     653      448575 :   while (--k && gequal0(gel(x,k)));
     654      448574 :   if (!k) return pol_0(v);
     655      443667 :   i = k+2; p = cgetg(i,t_POL);
     656      443666 :   p[1] = evalsigne(1) | evalvarn(v);
     657      443666 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     658      443666 :   return p;
     659             : }
     660             : GEN
     661      152919 : RgV_to_RgX_reverse(GEN x, long v)
     662             : {
     663      152919 :   long j, k, l = lg(x);
     664             :   GEN p;
     665             : 
     666      152919 :   for (k = 1; k < l; k++)
     667      152919 :     if (!gequal0(gel(x,k))) break;
     668      152919 :   if (k == l) return pol_0(v);
     669      152919 :   k -= 1;
     670      152919 :   l -= k;
     671      152919 :   x += k;
     672      152919 :   p = cgetg(l+1,t_POL);
     673      152919 :   p[1] = evalsigne(1) | evalvarn(v);
     674      152919 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     675      152919 :   return p;
     676             : }
     677             : 
     678             : /* return the (N-dimensional) vector of coeffs of p */
     679             : GEN
     680     6563945 : RgX_to_RgC(GEN x, long N)
     681             : {
     682             :   long i, l;
     683             :   GEN z;
     684     6563945 :   l = lg(x)-1; x++;
     685     6563945 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     686     6563945 :   z = cgetg(N+1,t_COL);
     687     6563945 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     688     6563945 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     689     6563945 :   return z;
     690             : }
     691             : GEN
     692      677342 : Rg_to_RgC(GEN x, long N)
     693             : {
     694      677342 :   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       47148 : RgM_to_RgXV(GEN x, long v)
     700       47148 : { 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      103001 : RgV_to_RgM(GEN x, long n)
     706      103001 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     707             : 
     708             : GEN
     709        2839 : RgXV_to_RgM(GEN x, long n)
     710        2839 : { 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       15897 : RgM_to_RgXX(GEN x, long v,long w)
     715             : {
     716       15897 :   long j, lx = lg(x);
     717       15897 :   GEN y = cgetg(lx+1, t_POL);
     718       15897 :   y[1] = evalsigne(1) | evalvarn(v);
     719       15897 :   y++;
     720       15897 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     721       15897 :   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          98 : RgXX_to_RgM(GEN v, long n)
     728             : {
     729          98 :   long j, N = lg(v)-1;
     730          98 :   GEN y = cgetg(N, t_MAT);
     731          98 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     732          98 :   return y;
     733             : }
     734             : 
     735             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     736             : GEN
     737       13904 : RgXY_swapspec(GEN x, long n, long w, long nx)
     738             : {
     739       13904 :   long j, ly = n+3;
     740       13904 :   GEN y = cgetg(ly, t_POL);
     741       13904 :   y[1] = evalsigne(1);
     742      206571 :   for (j=2; j<ly; j++)
     743             :   {
     744             :     long k;
     745      192667 :     GEN a = cgetg(nx+2,t_POL);
     746      192667 :     a[1] = evalsigne(1) | evalvarn(w);
     747     1069234 :     for (k=0; k<nx; k++)
     748             :     {
     749      876567 :       GEN xk = gel(x,k);
     750      876567 :       if (typ(xk)==t_POL)
     751      791237 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     752             :       else
     753       85330 :         gel(a,k+2) = j==2 ? xk: gen_0;
     754             :     }
     755      192667 :     gel(y,j) = normalizepol_lg(a, nx+2);
     756             :   }
     757       13904 :   return normalizepol_lg(y,ly);
     758             : }
     759             : 
     760             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     761             : GEN
     762         952 : RgXY_swap(GEN x, long n, long w)
     763             : {
     764         952 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     765         952 :   setvarn(z, varn(x)); return z;
     766             : }
     767             : 
     768             : long
     769          92 : RgXY_degreex(GEN b)
     770             : {
     771          92 :   long deg = -1, i;
     772          92 :   if (!signe(b)) return -1;
     773         584 :   for (i = 2; i < lg(b); ++i)
     774             :   {
     775         492 :     GEN bi = gel(b, i);
     776         492 :     if (typ(bi) == t_POL)
     777         470 :       deg = maxss(deg, degpol(bi));
     778             :   }
     779          92 :   return deg;
     780             : }
     781             : 
     782             : /* return (x % X^n). Shallow */
     783             : GEN
     784     1567729 : RgXn_red_shallow(GEN a, long n)
     785             : {
     786     1567729 :   long i, L = n+2, l = lg(a);
     787             :   GEN  b;
     788     1567729 :   if (L >= l) return a; /* deg(x) < n */
     789     1528718 :   b = cgetg(L, t_POL); b[1] = a[1];
     790     1528718 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     791     1528718 :   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    57789019 : RgX_shift_shallow(GEN a, long n)
     801             : {
     802    57789019 :   long i, l = lg(a);
     803             :   GEN  b;
     804    57789019 :   if (l == 2 || !n) return a;
     805    42592782 :   l += n;
     806    42592782 :   if (n < 0)
     807             :   {
     808    37598801 :     if (l <= 2) return pol_0(varn(a));
     809    37579719 :     b = cgetg(l, t_POL); b[1] = a[1];
     810    37579720 :     a -= n;
     811    37579720 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     812             :   } else {
     813     4993981 :     b = cgetg(l, t_POL); b[1] = a[1];
     814     4993980 :     a -= n; n += 2;
     815     4993980 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     816     4993980 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     817             :   }
     818    42573700 :   return b;
     819             : }
     820             : /* return (x * X^n). */
     821             : GEN
     822     3403236 : RgX_shift(GEN a, long n)
     823             : {
     824     3403236 :   long i, l = lg(a);
     825             :   GEN  b;
     826     3403236 :   if (l == 2 || !n) return RgX_copy(a);
     827     3402970 :   l += n;
     828     3402970 :   if (n < 0)
     829             :   {
     830         728 :     if (l <= 2) return pol_0(varn(a));
     831         686 :     b = cgetg(l, t_POL); b[1] = a[1];
     832         686 :     a -= n;
     833         686 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     834             :   } else {
     835     3402242 :     b = cgetg(l, t_POL); b[1] = a[1];
     836     3402242 :     a -= n; n += 2;
     837     3402242 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     838     3402242 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     839             :   }
     840     3402928 :   return b;
     841             : }
     842             : 
     843             : GEN
     844      315273 : RgX_rotate_shallow(GEN P, long k, long p)
     845             : {
     846      315273 :   long i, l = lgpol(P);
     847             :   GEN r;
     848      315273 :   if (signe(P)==0)
     849         476 :     return pol_0(varn(P));
     850      314797 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     851     2090746 :   for(i=0; i<p; i++)
     852             :   {
     853     1775949 :     long s = 2+(i+k)%p;
     854     1775949 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     855             :   }
     856      314797 :   return RgX_renormalize(r);
     857             : }
     858             : 
     859             : GEN
     860     2859860 : RgX_mulXn(GEN x, long d)
     861             : {
     862             :   pari_sp av;
     863             :   GEN z;
     864             :   long v;
     865     2859860 :   if (d >= 0) return RgX_shift(x, d);
     866     1367424 :   d = -d;
     867     1367424 :   v = RgX_val(x);
     868     1367424 :   if (v >= d) return RgX_shift(x, -d);
     869     1367417 :   av = avma;
     870     1367417 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     871     1367417 :   return gerepileupto(av, z);
     872             : }
     873             : 
     874             : long
     875     2111477 : RgX_val(GEN x)
     876             : {
     877     2111477 :   long i, lx = lg(x);
     878     2111477 :   if (lx == 2) return LONG_MAX;
     879     2134052 :   for (i = 2; i < lx; i++)
     880     2134038 :     if (!isexactzero(gel(x,i))) break;
     881     2111435 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     882     2111421 :   return i - 2;
     883             : }
     884             : long
     885    42488993 : RgX_valrem(GEN x, GEN *Z)
     886             : {
     887    42488993 :   long v, i, lx = lg(x);
     888    42488993 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     889    81824773 :   for (i = 2; i < lx; i++)
     890    81824773 :     if (!isexactzero(gel(x,i))) break;
     891             :   /* possible with non-rational zeros */
     892    42488993 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     893    42488993 :   v = i - 2;
     894    42488993 :   *Z = RgX_shift_shallow(x, -v);
     895    42488993 :   return v;
     896             : }
     897             : long
     898       10117 : RgX_valrem_inexact(GEN x, GEN *Z)
     899             : {
     900             :   long v;
     901       10117 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     902       10495 :   for (v = 0;; v++)
     903       10495 :     if (!gequal0(gel(x,2+v))) break;
     904         385 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     905       10110 :   return v;
     906             : }
     907             : 
     908             : GEN
     909       80521 : RgXQC_red(GEN x, GEN T)
     910       80521 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
     911             : 
     912             : GEN
     913         665 : RgXQV_red(GEN x, GEN T)
     914         665 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
     915             : 
     916             : GEN
     917       12264 : RgXQM_red(GEN x, GEN T)
     918       12264 : { 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      341410 : RgXQX_red(GEN P, GEN T)
     928             : {
     929      341410 :   long i, l = lg(P);
     930      341410 :   GEN Q = cgetg(l, t_POL);
     931      341410 :   Q[1] = P[1];
     932      341410 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     933      341410 :   return normalizepol_lg(Q, l);
     934             : }
     935             : 
     936             : GEN
     937      224635 : RgX_deriv(GEN x)
     938             : {
     939      224635 :   long i,lx = lg(x)-1;
     940             :   GEN y;
     941             : 
     942      224635 :   if (lx<3) return pol_0(varn(x));
     943      196957 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     944      196957 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     945      196957 :   y[1] = x[1]; return normalizepol_lg(y,i);
     946             : }
     947             : 
     948             : GEN
     949      432825 : RgX_recipspec_shallow(GEN x, long l, long n)
     950             : {
     951             :   long i;
     952      432825 :   GEN z=cgetg(n+2,t_POL)+2;
     953    14114200 :   for(i=0; i<l; i++)
     954    13681372 :     gel(z,n-i-1) = gel(x,i);
     955      521045 :   for(   ; i<n; i++)
     956       88217 :     gel(z, n-i-1) = gen_0;
     957      432828 :   return normalizepol_lg(z-2,n+2);
     958             : }
     959             : 
     960             : GEN
     961         315 : RgXn_recip_shallow(GEN P, long n)
     962             : {
     963         315 :   GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
     964         315 :   setvarn(Q, varn(P));
     965         315 :   return Q;
     966             : }
     967             : 
     968             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     969             : GEN
     970        2170 : RgX_recip(GEN x)
     971             : {
     972             :   long lx, i, j;
     973        2170 :   GEN y = cgetg_copy(x, &lx);
     974        2170 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     975        2170 :   return normalizepol_lg(y,lx);
     976             : }
     977             : /* shallow version */
     978             : GEN
     979      317105 : RgX_recip_shallow(GEN x)
     980             : {
     981             :   long lx, i, j;
     982      317105 :   GEN y = cgetg_copy(x, &lx);
     983      317123 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     984      317123 :   return y;
     985             : }
     986             : /*******************************************************************/
     987             : /*                                                                 */
     988             : /*                      ADDITION / SUBTRACTION                     */
     989             : /*                                                                 */
     990             : /*******************************************************************/
     991             : /* same variable */
     992             : GEN
     993    17160870 : RgX_add(GEN x, GEN y)
     994             : {
     995    17160870 :   long i, lx = lg(x), ly = lg(y);
     996             :   GEN z;
     997    17160870 :   if (ly <= lx) {
     998    15422981 :     z = cgetg(lx,t_POL); z[1] = x[1];
     999    15422981 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1000    15422981 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1001    15422981 :     z = normalizepol_lg(z, lx);
    1002             :   } else {
    1003     1737889 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1004     1737889 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1005     1737889 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1006     1737889 :     z = normalizepol_lg(z, ly);
    1007             :   }
    1008    17160870 :   return z;
    1009             : }
    1010             : GEN
    1011    11671907 : RgX_sub(GEN x, GEN y)
    1012             : {
    1013    11671907 :   long i, lx = lg(x), ly = lg(y);
    1014             :   GEN z;
    1015    11671907 :   if (ly <= lx) {
    1016     9168314 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1017     9168314 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1018     9168314 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1019     9168314 :     z = normalizepol_lg(z, lx);
    1020             :   } else {
    1021     2503593 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1022     2503593 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1023     2503593 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1024     2503593 :     z = normalizepol_lg(z, ly);
    1025             :   }
    1026    11671907 :   return z;
    1027             : }
    1028             : GEN
    1029     1035985 : RgX_neg(GEN x)
    1030             : {
    1031     1035985 :   long i, lx = lg(x);
    1032     1035985 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1033     1035985 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1034     1035985 :   return y;
    1035             : }
    1036             : 
    1037             : GEN
    1038    11532269 : RgX_Rg_add(GEN y, GEN x)
    1039             : {
    1040             :   GEN z;
    1041    11532269 :   long lz = lg(y), i;
    1042    11532269 :   if (lz == 2) return scalarpol(x,varn(y));
    1043     9835872 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1044     9835872 :   gel(z,2) = gadd(gel(y,2),x);
    1045     9835872 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1046             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1047             :    * an inexact 0 */
    1048     9835872 :   return normalizepol_lg(z,lz);
    1049             : }
    1050             : GEN
    1051        2422 : RgX_Rg_add_shallow(GEN y, GEN x)
    1052             : {
    1053             :   GEN z;
    1054        2422 :   long lz = lg(y), i;
    1055        2422 :   if (lz == 2) return scalarpol(x,varn(y));
    1056        2422 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1057        2422 :   gel(z,2) = gadd(gel(y,2),x);
    1058        2422 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1059        2422 :   return z = normalizepol_lg(z,lz);
    1060             : }
    1061             : GEN
    1062       32915 : RgX_Rg_sub(GEN y, GEN x)
    1063             : {
    1064             :   GEN z;
    1065       32915 :   long lz = lg(y), i;
    1066       32915 :   if (lz == 2)
    1067             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1068        3864 :     long v = varn(y);
    1069        3864 :     if (isrationalzero(x)) return pol_0(v);
    1070          14 :     z = cgetg(3,t_POL);
    1071          28 :     z[1] = gequal0(x)? evalvarn(v)
    1072          14 :                    : evalvarn(v) | evalsigne(1);
    1073          14 :     gel(z,2) = gneg(x); return z;
    1074             :   }
    1075       29051 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1076       29051 :   gel(z,2) = gsub(gel(y,2),x);
    1077       29051 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1078       29051 :   return z = normalizepol_lg(z,lz);
    1079             : }
    1080             : GEN
    1081      380600 : Rg_RgX_sub(GEN x, GEN y)
    1082             : {
    1083             :   GEN z;
    1084      380600 :   long lz = lg(y), i;
    1085      380600 :   if (lz == 2) return scalarpol(x,varn(y));
    1086      379585 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1087      379585 :   gel(z,2) = gsub(x, gel(y,2));
    1088      379585 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1089      379585 :   return z = normalizepol_lg(z,lz);
    1090             : }
    1091             : /*******************************************************************/
    1092             : /*                                                                 */
    1093             : /*                  KARATSUBA MULTIPLICATION                       */
    1094             : /*                                                                 */
    1095             : /*******************************************************************/
    1096             : #if 0
    1097             : /* to debug Karatsuba-like routines */
    1098             : GEN
    1099             : zx_debug_spec(GEN x, long nx)
    1100             : {
    1101             :   GEN z = cgetg(nx+2,t_POL);
    1102             :   long i;
    1103             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1104             :   z[1] = evalsigne(1); return z;
    1105             : }
    1106             : 
    1107             : GEN
    1108             : RgX_debug_spec(GEN x, long nx)
    1109             : {
    1110             :   GEN z = cgetg(nx+2,t_POL);
    1111             :   long i;
    1112             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1113             :   z[1] = evalsigne(1); return z;
    1114             : }
    1115             : #endif
    1116             : 
    1117             : /* generic multiplication */
    1118             : GEN
    1119     2905121 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1120             : {
    1121             :   GEN z, t;
    1122             :   long i;
    1123     2905121 :   if (nx == ny) {
    1124      656688 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1125      656688 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1126      656688 :     return normalizepol_lg(z, nx+2);
    1127             :   }
    1128     2248433 :   if (ny < nx) {
    1129     2108067 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1130     2108067 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1131     2108067 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1132     2108067 :     return normalizepol_lg(z, nx+2);
    1133             :   } else {
    1134      140366 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1135      140366 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1136      140366 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1137      140366 :     return normalizepol_lg(z, ny+2);
    1138             :   }
    1139             : }
    1140             : GEN
    1141      179975 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1142             : {
    1143             :   GEN z, t;
    1144             :   long i;
    1145      179975 :   if (nx == ny) {
    1146         650 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1147         650 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1148         650 :     return normalizepol_lg(z, nx+2);
    1149             :   }
    1150      179325 :   if (ny < nx) {
    1151      179017 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1152      179017 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1153      179017 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1154      179017 :     return normalizepol_lg(z, nx+2);
    1155             :   } else {
    1156         308 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1157         308 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1158         308 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1159         308 :     return normalizepol_lg(z, ny+2);
    1160             :   }
    1161             : }
    1162             : 
    1163             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1164             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1165             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1166             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1167             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1168             : static GEN
    1169    19382293 : RgXspec_kill0(GEN x, long lx)
    1170             : {
    1171    19382293 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1172             :   long i;
    1173    73996293 :   for (i=0; i <lx; i++)
    1174             :   {
    1175    54614000 :     GEN c = gel(x,i);
    1176    54614000 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1177             :   }
    1178    19382293 :   return z;
    1179             : }
    1180             : 
    1181             : INLINE GEN
    1182    41455468 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1183             : {
    1184    41455468 :   pari_sp av = avma;
    1185    41455468 :   GEN s = NULL;
    1186             :   long i;
    1187             : 
    1188   146931196 :   for (i=a; i<b; i++)
    1189   105475728 :     if (gel(y,i) && gel(x,-i))
    1190             :     {
    1191    60464235 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1192    60464235 :       s = s? gadd(s, t): t;
    1193             :     }
    1194    41455468 :   return s? gerepileupto(av, s): gen_0;
    1195             : }
    1196             : 
    1197             : /* assume nx >= ny > 0, return x * y * t^v */
    1198             : static GEN
    1199     8011045 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1200             : {
    1201             :   long i, lz, nz;
    1202             :   GEN z;
    1203             : 
    1204     8011045 :   x = RgXspec_kill0(x,nx);
    1205     8011045 :   y = RgXspec_kill0(y,ny);
    1206     8011045 :   lz = nx + ny + 1; nz = lz-2;
    1207     8011045 :   lz += v;
    1208     8011045 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1209     8011045 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1210     8011045 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1211     8011045 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1212     8011045 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1213     8011045 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1214             : }
    1215             : 
    1216             : /* return (x * X^d) + y. Assume d > 0 */
    1217             : GEN
    1218     2014500 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1219             : {
    1220             :   GEN x, y, xd, yd, zd;
    1221             :   long a, lz, nx, ny;
    1222             : 
    1223     2014500 :   if (!signe(x0)) return y0;
    1224     1994632 :   ny = lgpol(y0);
    1225     1994632 :   nx = lgpol(x0);
    1226     1994632 :   zd = (GEN)avma;
    1227     1994632 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1228     1994632 :   if (a <= 0)
    1229             :   {
    1230      164242 :     lz = nx+d+2;
    1231      164242 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1232      164242 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1233      164242 :     x = zd + a;
    1234      164242 :     while (zd > x) gel(--zd,0) = gen_0;
    1235             :   }
    1236             :   else
    1237             :   {
    1238     1830390 :     xd = new_chunk(d); yd = y+d;
    1239     1830390 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1240     1830390 :     lz = (a>nx)? ny+2: lg(x)+d;
    1241     1830390 :     x += 2; while (xd > x) *--zd = *--xd;
    1242             :   }
    1243     1994632 :   while (yd > y) *--zd = *--yd;
    1244     1994632 :   *--zd = x0[1];
    1245     1994632 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1246             : }
    1247             : GEN
    1248      470465 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1249             : {
    1250             :   GEN x, y, xd, yd, zd;
    1251             :   long a, lz, nx, ny;
    1252             : 
    1253      470465 :   if (!signe(x0)) return RgX_copy(y0);
    1254      470287 :   nx = lgpol(x0);
    1255      470287 :   ny = lgpol(y0);
    1256      470287 :   zd = (GEN)avma;
    1257      470287 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1258      470287 :   if (a <= 0)
    1259             :   {
    1260      290312 :     lz = nx+d+2;
    1261      290312 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1262      290312 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1263      290312 :     x = zd + a;
    1264      290312 :     while (zd > x) gel(--zd,0) = gen_0;
    1265             :   }
    1266             :   else
    1267             :   {
    1268      179975 :     xd = new_chunk(d); yd = y+d;
    1269      179975 :     x = RgX_addspec(x,yd, nx,a);
    1270      179975 :     lz = (a>nx)? ny+2: lg(x)+d;
    1271      179975 :     x += 2; while (xd > x) *--zd = *--xd;
    1272             :   }
    1273      470287 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1274      470287 :   *--zd = x0[1];
    1275      470287 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1276             : }
    1277             : 
    1278             : /* return x * y mod t^n */
    1279             : static GEN
    1280     1677184 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1281             : {
    1282     1677184 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1283             :   GEN z;
    1284     1677184 :   if (lx < 0) return pol_0(varn(x));
    1285     1677184 :   if (ly < 0) return pol_0(varn(x));
    1286     1677184 :   z = cgetg(lz, t_POL) + 2;
    1287     1677184 :   x+=2; if (lx > n) lx = n;
    1288     1677184 :   y+=2; if (ly > n) ly = n;
    1289     1677184 :   z[-1] = x[-1];
    1290     1677184 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1291     1677184 :   x = RgXspec_kill0(x, lx);
    1292     1677184 :   y = RgXspec_kill0(y, ly);
    1293             :   /* x:y:z [i] = term of degree i */
    1294     1677184 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1295     1677184 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1296     1677184 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1297     1677184 :   return normalizepol_lg(z - 2, lz);
    1298             : }
    1299             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1300             : static GEN
    1301     1714487 : RgXn_mul2(GEN f, GEN g, long n)
    1302             : {
    1303     1714487 :   pari_sp av = avma;
    1304             :   GEN fe,fo, ge,go, l,h,m;
    1305             :   long n0, n1;
    1306     1714487 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1307     1679340 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1308        2156 :   n0 = n>>1; n1 = n-n0;
    1309        2156 :   RgX_even_odd(f, &fe, &fo);
    1310        2156 :   RgX_even_odd(g, &ge, &go);
    1311        2156 :   l = RgXn_mul(fe,ge,n1);
    1312        2156 :   h = RgXn_mul(fo,go,n0);
    1313        2156 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1314             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1315             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1316        2156 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1317             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1318        2156 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1319        2156 :   m = RgX_inflate(m,2);
    1320             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1321        2156 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1322        2156 :   h = RgX_inflate(h,2);
    1323        2156 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1324        2156 :   return gerepileupto(av, h);
    1325             : }
    1326             : /* (f*g) \/ x^n */
    1327             : static GEN
    1328        1434 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1329             : {
    1330        1434 :   long d = degpol(f)+degpol(g) + 1 - n;
    1331             :   GEN h;
    1332        1434 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1333        1046 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1334             :                                  RgX_recip_shallow(g), d));
    1335        1046 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1336             : }
    1337             : 
    1338             : /* (f*g) \/ x^n */
    1339             : static GEN
    1340           0 : RgX_sqrhigh_i2(GEN f, long n)
    1341             : {
    1342           0 :   long d = 2*degpol(f)+ 1 - n;
    1343             :   GEN h;
    1344           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1345           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1346           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1347             : }
    1348             : 
    1349             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1350             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1351             :  * Only c, c0, c1, c2 are genuine GEN.
    1352             :  */
    1353             : GEN
    1354     8544118 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1355             : {
    1356             :   GEN a0, c, c0;
    1357     8544118 :   long n0, n0a, i, v = 0;
    1358             :   pari_sp av;
    1359             : 
    1360     8544118 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1361     8544118 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1362     8544118 :   if (na < nb) swapspec(a,b, na,nb);
    1363     8544118 :   if (!nb) return pol_0(0);
    1364             : 
    1365     8479315 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1366      468270 :   RgX_shift_inplace_init(v);
    1367      468270 :   i = (na>>1); n0 = na-i; na = i;
    1368      468270 :   av = avma; a0 = a+n0; n0a = n0;
    1369      468270 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1370             : 
    1371      468270 :   if (nb > n0)
    1372             :   {
    1373             :     GEN b0,c1,c2;
    1374             :     long n0b;
    1375             : 
    1376      466970 :     nb -= n0; b0 = b+n0; n0b = n0;
    1377      466970 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1378      466970 :     c = RgX_mulspec(a,b,n0a,n0b);
    1379      466970 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1380             : 
    1381      466970 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1382      466970 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1383             : 
    1384      466970 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1385      466970 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1386      466970 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1387             :   }
    1388             :   else
    1389             :   {
    1390        1300 :     c = RgX_mulspec(a,b,n0a,nb);
    1391        1300 :     c0 = RgX_mulspec(a0,b,na,nb);
    1392             :   }
    1393      468270 :   c0 = RgX_addmulXn(c0,c,n0);
    1394      468270 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1395             : }
    1396             : 
    1397             : INLINE GEN
    1398       32735 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1399             : {
    1400       32735 :   pari_sp av = avma;
    1401       32735 :   GEN s = NULL;
    1402       32735 :   long j, l = (i+1)>>1;
    1403       97097 :   for (j=a; j<l; j++)
    1404             :   {
    1405       64362 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1406       64362 :     if (xj && xx)
    1407             :     {
    1408       57068 :       GEN t = gmul(xj, xx);
    1409       57068 :       s = s? gadd(s, t): t;
    1410             :     }
    1411             :   }
    1412       32735 :   if (s) s = gshift(s,1);
    1413       32735 :   if ((i&1) == 0)
    1414             :   {
    1415       19285 :     GEN t = gel(x, i>>1);
    1416       19285 :     if (t) {
    1417       16975 :       t = gsqr(t);
    1418       16975 :       s = s? gadd(s, t): t;
    1419             :     }
    1420             :   }
    1421       32735 :   return s? gerepileupto(av,s): gen_0;
    1422             : }
    1423             : static GEN
    1424        5835 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1425             : {
    1426             :   long i, lz, nz;
    1427             :   GEN z;
    1428             : 
    1429        5835 :   if (!nx) return pol_0(0);
    1430        5835 :   x = RgXspec_kill0(x,nx);
    1431        5835 :   lz = (nx << 1) + 1, nz = lz-2;
    1432        5835 :   lz += v;
    1433        5835 :   z = cgetg(lz,t_POL) + 2;
    1434        5835 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1435        5835 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1436        5835 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1437        5835 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1438             : }
    1439             : /* return x^2 mod t^n */
    1440             : static GEN
    1441           0 : RgXn_sqr_basecase(GEN x, long n)
    1442             : {
    1443           0 :   long i, lz = n+2, lx = lgpol(x);
    1444             :   GEN z;
    1445           0 :   if (lx < 0) return pol_0(varn(x));
    1446           0 :   z = cgetg(lz, t_POL);
    1447           0 :   z[1] = x[1];
    1448           0 :   x+=2; if (lx > n) lx = n;
    1449           0 :   x = RgXspec_kill0(x,lx);
    1450           0 :   z+=2;/* x:z [i] = term of degree i */
    1451           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1452           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1453           0 :   z -= 2; return normalizepol_lg(z, lz);
    1454             : }
    1455             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1456             : static GEN
    1457         238 : RgXn_sqr2(GEN f, long n)
    1458             : {
    1459         238 :   pari_sp av = avma;
    1460             :   GEN fe,fo, l,h,m;
    1461             :   long n0, n1;
    1462         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1463           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1464           0 :   n0 = n>>1; n1 = n-n0;
    1465           0 :   RgX_even_odd(f, &fe, &fo);
    1466           0 :   l = RgXn_sqr(fe,n1);
    1467           0 :   h = RgXn_sqr(fo,n0);
    1468           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1469             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1470             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1471           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1472             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1473           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1474           0 :   m = RgX_inflate(m,2);
    1475             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1476           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1477           0 :   h = RgX_inflate(h,2);
    1478           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1479           0 :   return gerepileupto(av, h);
    1480             : }
    1481             : GEN
    1482        5874 : RgX_sqrspec(GEN a, long na)
    1483             : {
    1484             :   GEN a0, c, c0, c1;
    1485        5874 :   long n0, n0a, i, v = 0;
    1486             :   pari_sp av;
    1487             : 
    1488        5874 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1489        5874 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1490          39 :   RgX_shift_inplace_init(v);
    1491          39 :   i = (na>>1); n0 = na-i; na = i;
    1492          39 :   av = avma; a0 = a+n0; n0a = n0;
    1493          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1494             : 
    1495          39 :   c = RgX_sqrspec(a,n0a);
    1496          39 :   c0 = RgX_sqrspec(a0,na);
    1497          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1498          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1499          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1500          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1501             : }
    1502             : 
    1503             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1504             : GEN
    1505      580021 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1506             : {
    1507      580021 :   GEN z = RgX_mul(A, B);
    1508      580021 :   if (a < b)
    1509        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1510      574575 :   else if (a > b)
    1511      349479 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1512             :   else
    1513      225096 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1514      580021 :   return z;
    1515             : }
    1516             : 
    1517             : GEN
    1518     7140569 : RgX_mul_i(GEN x, GEN y)
    1519             : {
    1520     7140569 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1521     7140569 :   setvarn(z, varn(x)); return z;
    1522             : }
    1523             : 
    1524             : GEN
    1525        5796 : RgX_sqr_i(GEN x)
    1526             : {
    1527        5796 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1528        5796 :   setvarn(z,varn(x)); return z;
    1529             : }
    1530             : 
    1531             : /*******************************************************************/
    1532             : /*                                                                 */
    1533             : /*                               DIVISION                          */
    1534             : /*                                                                 */
    1535             : /*******************************************************************/
    1536             : GEN
    1537      433473 : RgX_Rg_divexact(GEN x, GEN y) {
    1538             :   long i, lx;
    1539             :   GEN z;
    1540      433473 :   if (typ(y) == t_INT && is_pm1(y))
    1541       27844 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1542      405629 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1543      405629 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1544      405629 :   return z;
    1545             : }
    1546             : GEN
    1547    22607246 : RgX_Rg_div(GEN x, GEN y) {
    1548             :   long i, lx;
    1549    22607246 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1550    22607246 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1551    22607246 :   return normalizepol_lg(z, lx);
    1552             : }
    1553             : GEN
    1554        1939 : RgX_normalize(GEN x)
    1555             : {
    1556        1939 :   GEN d = NULL;
    1557        1939 :   long i, n = lg(x)-1;
    1558        1939 :   for (i = n; i > 1; i--)
    1559             :   {
    1560        1939 :     d = gel(x,i);
    1561        1939 :     if (!gequal0(d)) break;
    1562             :   }
    1563        1939 :   if (i == 1) return pol_0(varn(x));
    1564        1939 :   if (i == n && isint1(d)) return x;
    1565         595 :   return normalizepol_lg(RgX_Rg_div(x, d), i+1);
    1566             : }
    1567             : GEN
    1568        2457 : RgX_divs(GEN x, long y) {
    1569             :   long i, lx;
    1570        2457 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1571        2457 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1572        2457 :   return normalizepol_lg(z, lx);
    1573             : }
    1574             : GEN
    1575       39231 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1576             : {
    1577       39231 :   long l = lg(a), i;
    1578       39231 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1579       39231 :   z[1] = a[1];
    1580       39231 :   a0 = a + l-1;
    1581       39231 :   z0 = z + l-2; *z0 = *a0--;
    1582      779521 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1583             :   {
    1584      740290 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1585      740290 :     gel(z0,0) = t;
    1586             :   }
    1587       39231 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1588       39231 :   return z;
    1589             : }
    1590             : /* Polynomial division x / y:
    1591             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1592             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1593             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1594             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1595             : static GEN
    1596    11679344 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1597             : {
    1598             :   pari_sp avy, av, av1;
    1599             :   long dx,dy,dz,i,j,sx,lr;
    1600             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1601             :   GEN (*f)(GEN,GEN);
    1602             : 
    1603    11679344 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1604             : 
    1605    11679344 :   dy = degpol(y);
    1606    11679344 :   y_lead = gel(y,dy+2);
    1607    11679344 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1608             :   {
    1609           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1610           0 :     for (dy--; dy>=0; dy--)
    1611             :     {
    1612           0 :       y_lead = gel(y,dy+2);
    1613           0 :       if (!gequal0(y_lead)) break;
    1614             :     }
    1615             :   }
    1616    11679344 :   if (!dy) /* y is constant */
    1617             :   {
    1618       14298 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1619       14298 :     z = RgX_Rg_div(x, y_lead);
    1620       14298 :     if (pr == ONLY_DIVIDES) return z;
    1621       12646 :     if (pr) *pr = pol_0(varn(x));
    1622       12646 :     return z;
    1623             :   }
    1624    11665046 :   dx = degpol(x);
    1625    11665046 :   if (dx < dy)
    1626             :   {
    1627      533152 :     if (pr == ONLY_REM) return RgX_copy(x);
    1628      339400 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1629      339379 :     z = pol_0(varn(x));
    1630      339379 :     if (pr) *pr = RgX_copy(x);
    1631      339379 :     return z;
    1632             :   }
    1633             : 
    1634             :   /* x,y in R[X], y non constant */
    1635    11131894 :   av = avma;
    1636    11131894 :   p = NULL;
    1637    11131894 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1638             :   {
    1639      112952 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1640      112952 :     if (!z) { avma = av; return NULL; }
    1641      112952 :     z = FpX_to_mod(z, p);
    1642      112952 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1643       60879 :       return gerepileupto(av, z);
    1644       52073 :     *pr = FpX_to_mod(*pr, p);
    1645       52073 :     gerepileall(av, 2, pr, &z);
    1646       52073 :     return z;
    1647             :   }
    1648    11018942 :   switch(typ(y_lead))
    1649             :   {
    1650             :     case t_REAL:
    1651           0 :       y_lead = ginv(y_lead);
    1652           0 :       f = gmul; mod = NULL;
    1653           0 :       break;
    1654             :     case t_INTMOD:
    1655        1378 :     case t_POLMOD: y_lead = ginv(y_lead);
    1656        1378 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1657        1378 :       break;
    1658    11017564 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1659    11017564 :       f = gdiv; mod = NULL;
    1660             :   }
    1661             : 
    1662    11018942 :   if (y_lead == NULL)
    1663     9244288 :     p2 = gel(x,dx+2);
    1664             :   else {
    1665             :     for(;;) {
    1666     1774654 :       p2 = f(gel(x,dx+2),y_lead);
    1667     1774654 :       p2 = simplify_shallow(p2);
    1668     1774654 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1669           0 :     }
    1670     1774654 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1671             :     {
    1672           0 :       if (pr == ONLY_DIVIDES) {
    1673           0 :         avma = av;
    1674           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1675             :       }
    1676           0 :       if (pr == ONLY_REM)
    1677             :       {
    1678           0 :         if (dx < 0)
    1679           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1680             :         else
    1681             :         {
    1682             :           GEN t;
    1683           0 :           avma = av;
    1684           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1685           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1686           0 :           return t;
    1687             :         }
    1688             :       }
    1689           0 :       if (pr) /* cf ONLY_REM above */
    1690             :       {
    1691           0 :         if (dx < 0)
    1692             :         {
    1693           0 :           p2 = gclone(p2);
    1694           0 :           avma = av;
    1695           0 :           z = pol_0(varn(x));
    1696           0 :           x = scalarpol(p2, varn(x));
    1697           0 :           gunclone(p2);
    1698             :         }
    1699             :         else
    1700             :         {
    1701             :           GEN t;
    1702           0 :           avma = av;
    1703           0 :           z = pol_0(varn(x));
    1704           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1705           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1706           0 :           x = t;
    1707             :         }
    1708           0 :         *pr = x;
    1709             :       }
    1710             :       else
    1711             :       {
    1712           0 :         avma = av;
    1713           0 :         z = pol_0(varn(x));
    1714             :       }
    1715           0 :       return z;
    1716             :     }
    1717             :   }
    1718             :   /* dx >= dy */
    1719    11018942 :   avy = avma;
    1720    11018942 :   dz = dx-dy;
    1721    11018942 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1722    11018942 :   x += 2;
    1723    11018942 :   z += 2;
    1724    11018942 :   y += 2;
    1725    11018942 :   gel(z,dz) = gcopy(p2);
    1726             : 
    1727    26954647 :   for (i=dx-1; i>=dy; i--)
    1728             :   {
    1729    15935705 :     av1=avma; p1=gel(x,i);
    1730    15935705 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1731    15935705 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1732             : 
    1733    15935705 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1734             :     else
    1735     6470007 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1736    15935705 :     gel(z,i-dy) = p1;
    1737             :   }
    1738    11018942 :   if (!pr) return gerepileupto(av,z-2);
    1739             : 
    1740     4541517 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1741     4878805 :   for (sx=0; ; i--)
    1742             :   {
    1743     4878805 :     p1 = gel(x,i);
    1744             :     /* we always enter this loop at least once */
    1745     4878805 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1746     4878805 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1747     4878805 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1748     3400755 :     if (!isexactzero(p1)) break;
    1749     3390965 :     if (!i) break;
    1750      337288 :     avma=av1;
    1751      337288 :   }
    1752     4541517 :   if (pr == ONLY_DIVIDES)
    1753             :   {
    1754        1344 :     if (sx) { avma=av; return NULL; }
    1755        1337 :     avma = (pari_sp)rem;
    1756        1337 :     return gerepileupto(av,z-2);
    1757             :   }
    1758     4540173 :   lr=i+3; rem -= lr;
    1759     4540173 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1760     4478073 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1761     4540173 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1762     4540173 :   rem[1] = z[-1];
    1763     4540173 :   rem += 2;
    1764     4540173 :   gel(rem,i) = p1;
    1765     5568087 :   for (i--; i>=0; i--)
    1766             :   {
    1767     1027914 :     av1=avma; p1 = gel(x,i);
    1768     1027914 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1769     1027914 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1770     1027914 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1771             :   }
    1772     4540173 :   rem -= 2;
    1773     4540173 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1774     4540173 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1775     3987242 :   z -= 2;
    1776             :   {
    1777     3987242 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1778     3987242 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1779             :   }
    1780             : }
    1781             : 
    1782             : GEN
    1783    10932661 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1784             : {
    1785    10932661 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1786    10932661 :   return RgX_divrem_i(x, y, pr);
    1787             : }
    1788             : 
    1789             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1790             : GEN
    1791       57268 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1792             : {
    1793             :   long vx, dx, dy, dz, i, j, sx, lr;
    1794             :   pari_sp av0, av, tetpil;
    1795             :   GEN z,p1,rem,lead;
    1796             : 
    1797       57268 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1798       57268 :   vx = varn(x);
    1799       57268 :   dx = degpol(x);
    1800       57268 :   dy = degpol(y);
    1801       57268 :   if (dx < dy)
    1802             :   {
    1803       15862 :     if (pr)
    1804             :     {
    1805       15862 :       av0 = avma; x = RgXQX_red(x, T);
    1806       15862 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1807       15862 :       if (pr == ONLY_REM) return x;
    1808           0 :       *pr = x;
    1809             :     }
    1810           0 :     return pol_0(vx);
    1811             :   }
    1812       41406 :   lead = leading_coeff(y);
    1813       41406 :   if (!dy) /* y is constant */
    1814             :   {
    1815         546 :     if (pr && pr != ONLY_DIVIDES)
    1816             :     {
    1817           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1818           0 :       *pr = pol_0(vx);
    1819             :     }
    1820         546 :     if (gequal1(lead)) return RgX_copy(x);
    1821           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1822           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1823             :   }
    1824       40860 :   av0 = avma; dz = dx-dy;
    1825       40860 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1826       40860 :   avma = av0;
    1827       40860 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1828       40860 :   x += 2; y += 2; z += 2;
    1829             : 
    1830       40860 :   p1 = gel(x,dx); av = avma;
    1831       40860 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1832      271618 :   for (i=dx-1; i>=dy; i--)
    1833             :   {
    1834      230758 :     av=avma; p1=gel(x,i);
    1835      230758 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1836      230758 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1837      230758 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1838             :   }
    1839       40860 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1840             : 
    1841       39614 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1842       64015 :   for (sx=0; ; i--)
    1843             :   {
    1844       64015 :     p1 = gel(x,i);
    1845       64015 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1846       64015 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1847       28539 :     if (!i) break;
    1848       24401 :     avma=av;
    1849       24401 :   }
    1850       39614 :   if (pr == ONLY_DIVIDES)
    1851             :   {
    1852        1716 :     if (lead) gunclone(lead);
    1853        1716 :     if (sx) { avma=av0; return NULL; }
    1854        1646 :     avma = (pari_sp)rem; return z-2;
    1855             :   }
    1856       37898 :   lr=i+3; rem -= lr;
    1857       37898 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1858       37898 :   rem[1] = z[-1];
    1859       37898 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1860       37898 :   rem += 2; gel(rem,i) = p1;
    1861      106729 :   for (i--; i>=0; i--)
    1862             :   {
    1863       68831 :     av=avma; p1 = gel(x,i);
    1864      260599 :     for (j=0; j<=i && j<=dz; j++)
    1865      191768 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1866       68831 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1867             :   }
    1868       37898 :   rem -= 2;
    1869       37898 :   if (lead) gunclone(lead);
    1870       37898 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1871       37898 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1872          84 :   *pr = rem; return z-2;
    1873             : }
    1874             : 
    1875             : /*******************************************************************/
    1876             : /*                                                                 */
    1877             : /*                        PSEUDO-DIVISION                          */
    1878             : /*                                                                 */
    1879             : /*******************************************************************/
    1880             : INLINE GEN
    1881      446046 : rem(GEN c, GEN T)
    1882             : {
    1883      446046 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1884      446046 :   return c;
    1885             : }
    1886             : 
    1887             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1888             : int
    1889        5919 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1890             : {
    1891             :   long dx, dy, dz, i, p, T_ismonic;
    1892        5919 :   pari_sp av = avma, av2;
    1893             :   GEN y_lead;
    1894             : 
    1895        5919 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1896        5919 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1897        5919 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1898             :   /* if monic, no point in using pseudo-division */
    1899        5919 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1900        3567 :   T_ismonic = gequal1(leading_coeff(T));
    1901        3567 :   dx = degpol(x);
    1902        3567 :   if (dx < dy) return !signe(x);
    1903        3567 :   (void)new_chunk(2);
    1904        3567 :   x = RgX_recip_shallow(x)+2;
    1905        3567 :   y = RgX_recip_shallow(y)+2;
    1906             :   /* pay attention to sparse divisors */
    1907        7316 :   for (i = 1; i <= dy; i++)
    1908        3749 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1909        3567 :   dz = dx-dy; p = dz+1;
    1910        3567 :   av2 = avma;
    1911             :   for (;;)
    1912             :   {
    1913       32213 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1914       32213 :     x0 = gneg(x0); p--;
    1915       32213 :     m = gcdii(cx, y0);
    1916       32213 :     if (!equali1(m))
    1917             :     {
    1918       31205 :       x0 = gdiv(x0, m);
    1919       31205 :       y0 = diviiexact(y0, m);
    1920       31205 :       if (equali1(y0)) y0 = NULL;
    1921             :     }
    1922       65616 :     for (i=1; i<=dy; i++)
    1923             :     {
    1924       33403 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1925       33403 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1926       33403 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1927       33403 :       gel(x,i) = c;
    1928             :     }
    1929      382876 :     for (   ; i<=dx; i++)
    1930             :     {
    1931      350663 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1932      350663 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1933      350663 :       gel(x,i) = c;
    1934             :     }
    1935       35871 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1936       32213 :     if (dx < dy) break;
    1937       28646 :     if (gc_needed(av2,1))
    1938             :     {
    1939           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1940           0 :       gerepilecoeffs(av2,x,dx+1);
    1941             :     }
    1942       28646 :   }
    1943        3567 :   avma = av; return (dx < 0);
    1944             : }
    1945             : 
    1946             : /* T either NULL or a t_POL. */
    1947             : GEN
    1948       25003 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1949             : {
    1950       25003 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1951       25003 :   pari_sp av = avma, av2;
    1952             :   GEN y_lead;
    1953             : 
    1954       25003 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1955       25003 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1956             :   /* if monic, no point in using pseudo-division */
    1957       25003 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1958       21475 :   dx = degpol(x);
    1959       21475 :   if (dx < dy) return RgX_copy(x);
    1960       21475 :   (void)new_chunk(2);
    1961       21475 :   x = RgX_recip_shallow(x)+2;
    1962       21475 :   y = RgX_recip_shallow(y)+2;
    1963             :   /* pay attention to sparse divisors */
    1964       66614 :   for (i = 1; i <= dy; i++)
    1965       45139 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1966       21475 :   dz = dx-dy; p = dz+1;
    1967       21475 :   av2 = avma;
    1968             :   for (;;)
    1969             :   {
    1970       81261 :     gel(x,0) = gneg(gel(x,0)); p--;
    1971      258125 :     for (i=1; i<=dy; i++)
    1972             :     {
    1973      176864 :       GEN c = gmul(y_lead, gel(x,i));
    1974      176864 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1975      176864 :       gel(x,i) = rem(c, T);
    1976             :     }
    1977      285649 :     for (   ; i<=dx; i++)
    1978             :     {
    1979      204388 :       GEN c = gmul(y_lead, gel(x,i));
    1980      204388 :       gel(x,i) = rem(c, T);
    1981             :     }
    1982       88744 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1983       81261 :     if (dx < dy) break;
    1984       59786 :     if (gc_needed(av2,1))
    1985             :     {
    1986           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1987           0 :       gerepilecoeffs(av2,x,dx+1);
    1988             :     }
    1989       59786 :   }
    1990       21475 :   if (dx < 0) return pol_0(vx);
    1991       19438 :   lx = dx+3; x -= 2;
    1992       19438 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1993       19438 :   x[1] = evalsigne(1) | evalvarn(vx);
    1994       19438 :   x = RgX_recip_shallow(x);
    1995       19438 :   if (p)
    1996             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1997        1183 :     GEN t = y_lead;
    1998        1183 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1999           0 :       t = RgXQ_powu(t, p, T);
    2000             :     else
    2001        1183 :       t = gpowgs(t, p);
    2002        4095 :     for (i=2; i<lx; i++)
    2003             :     {
    2004        2912 :       GEN c = gmul(gel(x,i), t);
    2005        2912 :       gel(x,i) = rem(c,T);
    2006             :     }
    2007        1183 :     if (!T) return gerepileupto(av, x);
    2008             :   }
    2009       18255 :   return gerepilecopy(av, x);
    2010             : }
    2011             : 
    2012             : GEN
    2013       25003 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2014             : 
    2015             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2016             : GEN
    2017        5333 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2018             : {
    2019        5333 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2020        5333 :   pari_sp av = avma, av2;
    2021             :   GEN z, r, ypow, y_lead;
    2022             : 
    2023        5333 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2024        5333 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2025        5333 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2026        4787 :   dx = degpol(x);
    2027        4787 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2028        4787 :   if (dx == dy)
    2029             :   {
    2030          28 :     GEN x_lead = gel(x,lg(x)-1);
    2031          28 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2032          28 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2033          28 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2034          28 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2035             :   }
    2036        4759 :   (void)new_chunk(2);
    2037        4759 :   x = RgX_recip_shallow(x)+2;
    2038        4759 :   y = RgX_recip_shallow(y)+2;
    2039             :   /* pay attention to sparse divisors */
    2040       23503 :   for (i = 1; i <= dy; i++)
    2041       18744 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2042        4759 :   dz = dx-dy; p = dz+1;
    2043        4759 :   lz = dz+3;
    2044        4759 :   z = cgetg(lz, t_POL);
    2045        4759 :   z[1] = evalsigne(1) | evalvarn(vx);
    2046        4759 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2047        4759 :   ypow = new_chunk(dz+1);
    2048        4759 :   gel(ypow,0) = gen_1;
    2049        4759 :   gel(ypow,1) = y_lead;
    2050        8035 :   for (i=2; i<=dz; i++)
    2051             :   {
    2052        3276 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2053        3276 :     gel(ypow,i) = rem(c,T);
    2054             :   }
    2055        4759 :   av2 = avma;
    2056        4759 :   for (iz=2;;)
    2057             :   {
    2058       10090 :     p--;
    2059       10090 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2060       44559 :     for (i=1; i<=dy; i++)
    2061             :     {
    2062       34469 :       GEN c = gmul(y_lead, gel(x,i));
    2063       34469 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2064       34469 :       gel(x,i) = rem(c, T);
    2065             :     }
    2066       24137 :     for (   ; i<=dx; i++)
    2067             :     {
    2068       14047 :       GEN c = gmul(y_lead, gel(x,i));
    2069       14047 :       gel(x,i) = rem(c,T);
    2070             :     }
    2071       10090 :     x++; dx--;
    2072       10090 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2073       10090 :     if (dx < dy) break;
    2074        5331 :     if (gc_needed(av2,1))
    2075             :     {
    2076           0 :       GEN X = x-2;
    2077           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2078           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2079           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2080             :     }
    2081        5331 :   }
    2082        4759 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2083        4759 :   if (dx < 0)
    2084          98 :     x = pol_0(vx);
    2085             :   else
    2086             :   {
    2087        4661 :     lx = dx+3; x -= 2;
    2088        4661 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2089        4661 :     x[1] = evalsigne(1) | evalvarn(vx);
    2090        4661 :     x = RgX_recip_shallow(x);
    2091             :   }
    2092        4759 :   z = RgX_recip_shallow(z);
    2093        4759 :   r = x;
    2094        4759 :   if (p)
    2095             :   {
    2096        1674 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2097        1674 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2098             :   }
    2099        4759 :   gerepileall(av, 2, &z, &r);
    2100        4759 :   *ptr = r; return z;
    2101             : }
    2102             : GEN
    2103        5172 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2104        5172 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2105             : 
    2106             : GEN
    2107       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2108             : {
    2109       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2110             : }
    2111             : GEN
    2112    67576619 : RgX_Rg_mul(GEN y, GEN x) {
    2113             :   long i, ly;
    2114    67576619 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2115    67576619 :   if (ly == 2) return z;
    2116    67518225 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2117    67518218 :   return normalizepol_lg(z,ly);
    2118             : }
    2119             : GEN
    2120       14217 : RgX_muls(GEN y, long x) {
    2121             :   long i, ly;
    2122       14217 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2123       14217 :   if (ly == 2) return z;
    2124       14182 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2125       14182 :   return normalizepol_lg(z,ly);
    2126             : }
    2127             : GEN
    2128        4417 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2129             : {
    2130        4417 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2131             : }
    2132             : GEN
    2133          56 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2134             : {
    2135          56 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2136             : }
    2137             : 
    2138             : GEN
    2139        1792 : RgXQX_sqr(GEN x, GEN T)
    2140             : {
    2141        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2142             : }
    2143             : 
    2144             : GEN
    2145         448 : RgXQX_powers(GEN P, long n, GEN T)
    2146             : {
    2147         448 :   GEN v = cgetg(n+2, t_VEC);
    2148             :   long i;
    2149         448 :   gel(v, 1) = pol_1(varn(T));
    2150         448 :   if (n==0) return v;
    2151         448 :   gel(v, 2) = gcopy(P);
    2152         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2153         448 :   return v;
    2154             : }
    2155             : 
    2156             : static GEN
    2157       65380 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2158             : static GEN
    2159           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2160             : static GEN
    2161      210069 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2162             : static GEN
    2163       86637 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2164             : static GEN
    2165      111076 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2166             : static GEN
    2167      106435 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2168             : static GEN
    2169         105 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2170             : static GEN
    2171       70868 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2172             : 
    2173             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2174             :               _mul, _sqr, _one, _zero };
    2175             : 
    2176             : GEN
    2177           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2178             : {
    2179           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2180             : }
    2181             : 
    2182             : GEN
    2183       45080 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2184             : {
    2185       45080 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2186       45080 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2187             : }
    2188             : 
    2189             : /* mod X^n */
    2190             : struct modXn {
    2191             :   long v; /* varn(X) */
    2192             :   long n;
    2193             : } ;
    2194             : static GEN
    2195        1897 : _sqrXn(void *data, GEN x) {
    2196        1897 :   struct modXn *S = (struct modXn*)data;
    2197        1897 :   return RgXn_sqr(x, S->n);
    2198             : }
    2199             : static GEN
    2200        1218 : _mulXn(void *data, GEN x, GEN y) {
    2201        1218 :   struct modXn *S = (struct modXn*)data;
    2202        1218 :   return RgXn_mul(x,y, S->n);
    2203             : }
    2204             : static GEN
    2205        1477 : _oneXn(void *data) {
    2206        1477 :   struct modXn *S = (struct modXn*)data;
    2207        1477 :   return pol_1(S->v);
    2208             : }
    2209             : static GEN
    2210           0 : _zeroXn(void *data) {
    2211           0 :   struct modXn *S = (struct modXn*)data;
    2212           0 :   return pol_0(S->v);
    2213             : }
    2214             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2215             :                                           _oneXn, _zeroXn };
    2216             : 
    2217             : GEN
    2218         357 : RgXn_powers(GEN x, long m, long n)
    2219             : {
    2220         357 :   long d = degpol(x);
    2221         357 :   int use_sqr = (d<<1) >= n;
    2222             :   struct modXn S;
    2223         357 :   S.v = varn(x); S.n = n;
    2224         357 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2225             : }
    2226             : 
    2227             : GEN
    2228        1617 : RgXn_powu_i(GEN x, ulong m, long n)
    2229             : {
    2230             :   struct modXn S;
    2231        1617 :   S.v = varn(x); S.n = n;
    2232        1617 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2233             : }
    2234             : GEN
    2235           0 : RgXn_powu(GEN x, ulong m, long n)
    2236             : {
    2237             :   struct modXn S;
    2238           0 :   S.v = varn(x); S.n = n;
    2239           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2240             : }
    2241             : 
    2242             : GEN
    2243         714 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2244             : {
    2245             :   struct modXn S;
    2246         714 :   S.v = varn(gel(x,2)); S.n = n;
    2247         714 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2248             : }
    2249             : 
    2250             : GEN
    2251           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2252             : {
    2253           0 :   int use_sqr = 2*degpol(x) >= n;
    2254             :   struct modXn S;
    2255           0 :   S.v = varn(x); S.n = n;
    2256           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2257             : }
    2258             : 
    2259             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2260             : GEN
    2261        2016 : RgXn_eval(GEN Q, GEN x, long n)
    2262             : {
    2263        2016 :   long d = degpol(x);
    2264             :   int use_sqr;
    2265             :   struct modXn S;
    2266        2016 :   if (d == 1 && isrationalzero(gel(x,2)))
    2267             :   {
    2268        2009 :     GEN y = RgX_unscale(Q, gel(x,3));
    2269        2009 :     setvarn(y, varn(x)); return y;
    2270             :   }
    2271           7 :   S.v = varn(x);
    2272           7 :   S.n = n;
    2273           7 :   use_sqr = (d<<1) >= n;
    2274           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2275             : }
    2276             : 
    2277             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2278             : static GEN
    2279       34929 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2280             : {
    2281       34929 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2282       34929 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2283             : }
    2284             : 
    2285             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2286             : static GEN
    2287           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2288             : {
    2289           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2290           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2291             : }
    2292             : 
    2293             : GEN
    2294      125664 : RgXn_inv(GEN f, long e)
    2295             : {
    2296      125664 :   pari_sp av = avma, av2;
    2297             :   ulong mask;
    2298             :   GEN W, a;
    2299      125664 :   long v = varn(f), n = 1;
    2300             : 
    2301      125664 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2302      125664 :   a = ginv(gel(f,2));
    2303      125664 :   if (e == 1) return scalarpol(a, v);
    2304      125650 :   else if (e == 2)
    2305             :   {
    2306             :     GEN b;
    2307      111538 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2308       92092 :     b = gneg(b);
    2309       92092 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2310       92092 :     W = deg1pol_shallow(b, a, v);
    2311       92092 :     return gerepilecopy(av, W);
    2312             :   }
    2313       14112 :   W = scalarpol_shallow(ginv(gel(f,2)),v);
    2314       14112 :   mask = quadratic_prec_mask(e);
    2315       14112 :   av2 = avma;
    2316       63153 :   for (;mask>1;)
    2317             :   {
    2318             :     GEN u, fr;
    2319       34929 :     long n2 = n;
    2320       34929 :     n<<=1; if (mask & 1) n--;
    2321       34929 :     mask >>= 1;
    2322       34929 :     fr = RgXn_red_shallow(f, n);
    2323       34929 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2324       34929 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2325       34929 :     if (gc_needed(av2,2))
    2326             :     {
    2327           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2328           0 :       W = gerepileupto(av2, W);
    2329             :     }
    2330             :   }
    2331       14112 :   return gerepileupto(av, W);
    2332             : }
    2333             : 
    2334             : GEN
    2335       12810 : RgXn_exp(GEN h, long e)
    2336             : {
    2337       12810 :   pari_sp av = avma, av2;
    2338       12810 :   long v = varn(h), n=1;
    2339       12810 :   GEN f = pol_1(v), g = pol_1(v);
    2340       12810 :   ulong mask = quadratic_prec_mask(e);
    2341       12810 :   av2 = avma;
    2342       12810 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2343           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2344       39606 :   for (;mask>1;)
    2345             :   {
    2346             :     GEN q, w;
    2347       13986 :     long n2 = n;
    2348       13986 :     n<<=1; if (mask & 1) n--;
    2349       13986 :     mask >>= 1;
    2350       13986 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2351       13986 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2352       13986 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2353       13986 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2354       13986 :     if (gc_needed(av2,2))
    2355             :     {
    2356           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2357           0 :       gerepileall(av2, 2, &f, &g);
    2358             :     }
    2359             :   }
    2360       12810 :   return gerepileupto(av, f);
    2361             : }
    2362             : 
    2363             : GEN
    2364          91 : RgXn_reverse(GEN f, long e)
    2365             : {
    2366          91 :   pari_sp av = avma, av2;
    2367             :   ulong mask;
    2368             :   GEN fi, a, df, W, an;
    2369          91 :   long v = varn(f), n=1;
    2370          91 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2371           0 :     pari_err_INV("serreverse",f);
    2372          91 :   fi = ginv(gel(f,3));
    2373          91 :   a = deg1pol_shallow(fi,gen_0,v);
    2374          91 :   if (e <= 2) return gerepilecopy(av, a);
    2375          91 :   W = scalarpol(fi,v);
    2376          91 :   df = RgX_deriv(f);
    2377          91 :   mask = quadratic_prec_mask(e);
    2378          91 :   av2 = avma;
    2379         539 :   for (;mask>1;)
    2380             :   {
    2381             :     GEN u, fa, fr;
    2382         357 :     long n2 = n, rt;
    2383         357 :     n<<=1; if (mask & 1) n--;
    2384         357 :     mask >>= 1;
    2385         357 :     fr = RgXn_red_shallow(f, n);
    2386         357 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2387         357 :     an = RgXn_powers(a, rt, n);
    2388         357 :     if (n>1)
    2389             :     {
    2390         357 :       long n4 = (n2+1)>>1;
    2391         357 :       GEN dfr = RgXn_red_shallow(df, n2);
    2392         357 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2393         357 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2394         357 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2395             :     }
    2396         357 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2397         357 :     fa = RgX_shift(fa, -n2);
    2398         357 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2399         357 :     if (gc_needed(av2,2))
    2400             :     {
    2401           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2402           0 :       gerepileall(av2, 2, &a, &W);
    2403             :     }
    2404             :   }
    2405          91 :   return gerepileupto(av, a);
    2406             : }
    2407             : 
    2408             : GEN
    2409           0 : RgXn_sqrt(GEN h, long e)
    2410             : {
    2411           0 :   pari_sp av = avma, av2;
    2412           0 :   long v = varn(h), n = 1;
    2413           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2414           0 :   ulong mask = quadratic_prec_mask(e);
    2415           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2416           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2417           0 :   av2 = avma;
    2418             :   while(1)
    2419             :   {
    2420           0 :     long n2 = n, m;
    2421             :     GEN g;
    2422           0 :     n<<=1; if (mask & 1) n--;
    2423           0 :     mask >>= 1;
    2424           0 :     m = n-n2;
    2425           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2426           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2427           0 :     if (mask==1) return gerepileupto(av, f);
    2428           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2429           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2430           0 :     if (gc_needed(av2,2))
    2431             :     {
    2432           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2433           0 :       gerepileall(av2, 2, &f, &df);
    2434             :     }
    2435           0 :   }
    2436             : }
    2437             : 
    2438             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2439             : GEN
    2440      181612 : RgXQ_powu(GEN x, ulong n, GEN T)
    2441             : {
    2442             :   pari_sp av;
    2443             :   GEN y;
    2444             : 
    2445      181612 :   if (!n) return pol_1(varn(x));
    2446      180037 :   if (n == 1) return RgX_copy(x);
    2447      122160 :   av = avma;
    2448      122160 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2449      122162 :   return gerepileupto(av, y);
    2450             : }
    2451             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2452             : GEN
    2453       18087 : RgXQ_pow(GEN x, GEN n, GEN T)
    2454             : {
    2455             :   pari_sp av;
    2456       18087 :   long s = signe(n);
    2457             :   GEN y;
    2458             : 
    2459       18087 :   if (!s) return pol_1(varn(x));
    2460       18087 :   if (is_pm1(n) == 1)
    2461           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2462       18087 :   av = avma;
    2463       18087 :   if (s < 0) x = RgXQ_inv(x, T);
    2464       18087 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2465       18087 :   return gerepileupto(av, y);
    2466             : }
    2467             : 
    2468             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2469             : GEN
    2470        2863 : RgXQ_powers(GEN x, long l, GEN T)
    2471             : {
    2472        2863 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2473        2863 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2474             : }
    2475             : 
    2476             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2477             : GEN
    2478        1946 : QXQ_powers(GEN a, long n, GEN T)
    2479             : {
    2480        1946 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2481             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2482        1946 :   if (den)
    2483             :   { /* restore denominators */
    2484        1309 :     GEN d = den;
    2485             :     long i;
    2486        1309 :     gel(v,2) = a;
    2487        3920 :     for (i=3; i<=n+1; i++) {
    2488        2611 :       d = mulii(d,den);
    2489        2611 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2490             :     }
    2491             :   }
    2492        1946 :   return v;
    2493             : }
    2494             : 
    2495             : static GEN
    2496        1260 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2497             : {
    2498        1260 :   long l, i, m = 0;
    2499             :   GEN dz, z;
    2500        1260 :   GEN V = cgetg_copy(v, &l);
    2501        4032 :   for (i = imin; i < l; i++)
    2502             :   {
    2503        2772 :     GEN c = gel(v, i);
    2504        2772 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2505             :   }
    2506        1260 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2507        1260 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2508        4032 :   for (i = imin; i < l; i++)
    2509             :   {
    2510        2772 :     GEN c = gel(v,i);
    2511        2772 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2512        2772 :     gel(V,i) = c;
    2513             :   }
    2514        1260 :   return V;
    2515             : }
    2516             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2517             : GEN
    2518        1197 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2519        1197 : { return do_QXQ_eval(v, 1, a, T); }
    2520             : GEN
    2521          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2522          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2523             : 
    2524             : GEN
    2525         287 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2526             : {
    2527         287 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2528             : }
    2529             : 
    2530             : GEN
    2531          56 : RgXQ_minpoly_naive(GEN y, GEN P)
    2532             : {
    2533          56 :   pari_sp ltop=avma;
    2534          56 :   long n=lgpol(P);
    2535          56 :   GEN M=ker(RgXQ_matrix_pow(y,n,n,P));
    2536          56 :   M=content(RgM_to_RgXV(M,varn(P)));
    2537          56 :   return gerepileupto(ltop,M);
    2538             : }
    2539             : 
    2540             : GEN
    2541       34738 : RgXQ_norm(GEN x, GEN T)
    2542             : {
    2543             :   pari_sp av;
    2544       34738 :   long dx = degpol(x);
    2545             :   GEN L, y;
    2546             : 
    2547       34738 :   av = avma; y = resultant(T, x);
    2548       34738 :   L = leading_coeff(T);
    2549       34738 :   if (gequal1(L) || !signe(x)) return y;
    2550           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2551             : }
    2552             : 
    2553             : GEN
    2554      105041 : RgX_blocks(GEN P, long n, long m)
    2555             : {
    2556      105041 :   GEN z = cgetg(m+1,t_VEC);
    2557      105041 :   long i,j, k=2, l = lg(P);
    2558      508295 :   for(i=1; i<=m; i++)
    2559             :   {
    2560      403254 :     GEN zi = cgetg(n+2,t_POL);
    2561      403254 :     zi[1] = P[1];
    2562      403254 :     gel(z,i) = zi;
    2563     2379177 :     for(j=2; j<n+2; j++)
    2564     1975923 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2565      403254 :     zi = RgX_renormalize_lg(zi, n+2);
    2566             :   }
    2567      105041 :   return z;
    2568             : }
    2569             : 
    2570             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2571             : void
    2572       28840 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2573             : {
    2574       28840 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2575             :   GEN p0, p1;
    2576             : 
    2577       57681 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2578             : 
    2579       28840 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2580       28840 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2581       28842 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2582      800051 :   for (i=0; i<n1; i++)
    2583             :   {
    2584      771209 :     p0[2+i] = p[2+(i<<1)];
    2585      771209 :     p1[2+i] = p[3+(i<<1)];
    2586             :   }
    2587       28842 :   if (n1 != n0)
    2588       20380 :     p0[2+i] = p[2+(i<<1)];
    2589       28842 :   *pe = normalizepol(p0);
    2590       28841 :   *po = normalizepol(p1);
    2591             : }
    2592             : 
    2593             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2594             : GEN
    2595       40670 : RgX_splitting(GEN p, long k)
    2596             : {
    2597       40670 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2598             :   GEN r;
    2599             : 
    2600       40670 :   m = n/k;
    2601       40670 :   r = cgetg(k+1,t_VEC);
    2602      224154 :   for(i=1; i<=k; i++)
    2603             :   {
    2604      183484 :     gel(r,i) = cgetg(m+3, t_POL);
    2605      183484 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2606             :   }
    2607      553245 :   for (j=1, i=0, l=2; i<=n; i++)
    2608             :   {
    2609      512575 :     gmael(r,j,l) = gel(p,2+i);
    2610      512575 :     if (j==k) { j=1; l++; } else j++;
    2611             :   }
    2612      224154 :   for(i=1; i<=k; i++)
    2613      183484 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2614       40670 :   return r;
    2615             : }
    2616             : 
    2617             : /*******************************************************************/
    2618             : /*                                                                 */
    2619             : /*                        Kronecker form                           */
    2620             : /*                                                                 */
    2621             : /*******************************************************************/
    2622             : 
    2623             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2624             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2625             :  * normalized coefficients */
    2626             : GEN
    2627      122375 : Kronecker_to_mod(GEN z, GEN T)
    2628             : {
    2629      122375 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2630      122375 :   GEN x, t = cgetg(N,t_POL);
    2631      122375 :   t[1] = T[1];
    2632      122375 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2633      122375 :   x[1] = z[1];
    2634      122375 :   T = RgX_copy(T);
    2635     1041432 :   for (i=2; i<lx+2; i++, z+= N-2)
    2636             :   {
    2637      919057 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2638      919057 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2639             :   }
    2640      122375 :   N = (l-2) % (N-2) + 2;
    2641      122375 :   for (j=2; j<N; j++) t[j] = z[j];
    2642      122375 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2643      122375 :   return normalizepol_lg(x, i+1);
    2644             : }
    2645             : 
    2646             : /*******************************************************************/
    2647             : /*                                                                 */
    2648             : /*                        Domain detection                         */
    2649             : /*                                                                 */
    2650             : /*******************************************************************/
    2651             : 
    2652             : static GEN
    2653      271783 : zero_FpX_mod(GEN p, long v)
    2654             : {
    2655      271783 :   GEN r = cgetg(3,t_POL);
    2656      271783 :   r[1] = evalvarn(v);
    2657      271783 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2658      271783 :   return r;
    2659             : }
    2660             : 
    2661             : static GEN
    2662      420846 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2663             : {
    2664      420846 :   pari_sp av = avma;
    2665             :   GEN r;
    2666      420846 :   if (lgefint(p) == 3)
    2667             :   {
    2668      353092 :     ulong pp = uel(p, 2);
    2669      353092 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2670             :                                   RgX_to_Flx(y, pp), pp));
    2671             :   }
    2672             :   else
    2673       67754 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2674      420846 :   if (signe(r)==0)
    2675      178053 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2676      242793 :   return gerepileupto(av, FpX_to_mod(r, p));
    2677             : }
    2678             : 
    2679             : static GEN
    2680           7 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2681             : {
    2682           7 :   GEN r = cgetg(3,t_POL);
    2683           7 :   r[1] = evalvarn(v);
    2684           7 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2685           7 :   return r;
    2686             : }
    2687             : 
    2688             : static GEN
    2689          56 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2690             : {
    2691          56 :   pari_sp av = avma;
    2692          56 :   GEN T = RgX_to_FpX(pol, p);
    2693          56 :   long dT = degpol(T);
    2694          56 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2695          56 :   GEN ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2696          56 :   GEN r = FpX_mul(kx, ky, p);
    2697          56 :   if (signe(r)==0)
    2698           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2699          56 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2700             : }
    2701             : 
    2702             : static GEN
    2703      304121 : RgX_liftred(GEN x, GEN T)
    2704      304121 : { return RgXQX_red(liftpol_shallow(x), T); }
    2705             : 
    2706             : static GEN
    2707      118403 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2708             : {
    2709      118403 :   pari_sp av = avma;
    2710      118403 :   long dT = degpol(T);
    2711      118403 :   GEN r = QX_mul(ZXX_to_Kronecker(RgX_liftred(x, T), dT),
    2712             :                  ZXX_to_Kronecker(RgX_liftred(y, T), dT));
    2713      118403 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2714             : }
    2715             : 
    2716             : static GEN
    2717        1260 : RgX_sqr_FpX(GEN x, GEN p)
    2718             : {
    2719        1260 :   pari_sp av = avma;
    2720             :   GEN r;
    2721        1260 :   if (lgefint(p) == 3)
    2722             :   {
    2723        1056 :     ulong pp = uel(p, 2);
    2724        1056 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2725             :   }
    2726             :   else
    2727         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2728        1260 :   if (signe(r)==0)
    2729          98 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2730        1162 :   return gerepileupto(av, FpX_to_mod(r, p));
    2731             : }
    2732             : 
    2733             : static GEN
    2734         147 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2735             : {
    2736         147 :   pari_sp av = avma;
    2737         147 :   GEN T = RgX_to_FpX(pol, p);
    2738         147 :   long dT = degpol(T);
    2739         147 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2740         147 :   GEN r = FpX_sqr(kx, p);
    2741         147 :   if (signe(r)==0)
    2742           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2743         147 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2744             : }
    2745             : 
    2746             : static GEN
    2747        3769 : RgX_sqr_QXQX(GEN x, GEN T)
    2748             : {
    2749        3769 :   pari_sp av = avma;
    2750        3769 :   long dT = degpol(T);
    2751        3769 :   GEN r = QX_sqr(ZXX_to_Kronecker(RgX_liftred(x, T), dT));
    2752        3769 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2753             : }
    2754             : 
    2755             : static GEN
    2756      327691 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2757             : {
    2758      327691 :   pari_sp av = avma;
    2759             :   GEN r;
    2760      327691 :   if (lgefint(p) == 3)
    2761             :   {
    2762      321132 :     ulong pp = uel(p, 2);
    2763      321132 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2764             :                                   RgX_to_Flx(y, pp), pp));
    2765             :   }
    2766             :   else
    2767        6559 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2768      327691 :   if (signe(r)==0)
    2769       93632 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2770      234059 :   return gerepileupto(av, FpX_to_mod(r, p));
    2771             : }
    2772             : 
    2773             : static GEN
    2774       31773 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    2775             : {
    2776       31773 :   pari_sp av = avma;
    2777             :   GEN r;
    2778       31773 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    2779       31773 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    2780             : }
    2781             : static GEN
    2782          35 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2783             : {
    2784          35 :   pari_sp av = avma;
    2785             :   GEN r;
    2786          35 :   GEN T = RgX_to_FpX(pol, p);
    2787          35 :   if (lgefint(p) == 3)
    2788             :   {
    2789          35 :     ulong pp = uel(p, 2);
    2790          35 :     GEN Tp = ZX_to_Flx(T, pp);
    2791          35 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2792             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2793             :   }
    2794             :   else
    2795           0 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2796          35 :   if (signe(r)==0)
    2797           7 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2798          28 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2799             : }
    2800             : 
    2801             : #define code(t1,t2) ((t1 << 6) | t2)
    2802             : static GEN
    2803    40373496 : RgX_mul_fast(GEN x, GEN y)
    2804             : {
    2805             :   GEN p, pol;
    2806             :   long pa;
    2807    40373496 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2808    40373438 :   switch(t)
    2809             :   {
    2810    29978878 :     case t_INT:    return ZX_mul(x,y);
    2811      896879 :     case t_FRAC:   return QX_mul(x,y);
    2812      101886 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2813      420846 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2814             :     case code(t_POLMOD, t_INT):
    2815             :     case code(t_POLMOD, t_FRAC):
    2816      118403 :                    return RgX_mul_QXQX(x, y, pol);
    2817             :     case code(t_POLMOD, t_INTMOD):
    2818          56 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2819     8856490 :     default:       return NULL;
    2820             :   }
    2821             : }
    2822             : static GEN
    2823     1760056 : RgX_sqr_fast(GEN x)
    2824             : {
    2825             :   GEN p, pol;
    2826             :   long pa;
    2827     1760056 :   long t = RgX_type(x,&p,&pol,&pa);
    2828     1760200 :   switch(t)
    2829             :   {
    2830     1714921 :     case t_INT:    return ZX_sqr(x);
    2831       32046 :     case t_FRAC:   return QX_sqr(x);
    2832        2261 :     case t_FFELT:  return FFX_sqr(x, pol);
    2833        1260 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2834             :     case code(t_POLMOD, t_INT):
    2835             :     case code(t_POLMOD, t_FRAC):
    2836        3769 :                    return RgX_sqr_QXQX(x, pol);
    2837             :     case code(t_POLMOD, t_INTMOD):
    2838         147 :                    return RgX_sqr_FpXQX(x, pol, p);
    2839        5796 :     default:       return NULL;
    2840             :   }
    2841             : }
    2842             : 
    2843             : static GEN
    2844     5427651 : RgX_rem_fast(GEN x, GEN y)
    2845             : {
    2846             :   GEN p, pol;
    2847             :   long pa;
    2848     5427651 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2849     5427781 :   switch(t)
    2850             :   {
    2851     3275903 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    2852     1056420 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    2853        1883 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2854      327691 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2855             :     case code(t_POLMOD, t_INT):
    2856             :     case code(t_POLMOD, t_FRAC):
    2857       31773 :                    return RgX_rem_QXQX(x, y, pol);
    2858             :     case code(t_POLMOD, t_INTMOD):
    2859          35 :                    return RgX_rem_FpXQX(x, y, pol, p);
    2860      734076 :     default:       return NULL;
    2861             :   }
    2862             : }
    2863             : 
    2864             : #undef code
    2865             : 
    2866             : GEN
    2867    36385227 : RgX_mul(GEN x, GEN y)
    2868             : {
    2869    36385227 :   GEN z = RgX_mul_fast(x,y);
    2870    36385217 :   if (!z) z = RgX_mul_i(x,y);
    2871    36385237 :   return z;
    2872             : }
    2873             : 
    2874             : GEN
    2875     1743986 : RgX_sqr(GEN x)
    2876             : {
    2877     1743986 :   GEN z = RgX_sqr_fast(x);
    2878     1743425 :   if (!z) z = RgX_sqr_i(x);
    2879     1743507 :   return z;
    2880             : }
    2881             : 
    2882             : GEN
    2883     5427666 : RgX_rem(GEN x, GEN y)
    2884             : {
    2885     5427666 :   GEN z = RgX_rem_fast(x, y);
    2886     5427795 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    2887     5427807 :   return z;
    2888             : }
    2889             : 
    2890             : GEN
    2891     3953340 : RgXn_mul(GEN f, GEN g, long n)
    2892             : {
    2893     3953340 :   pari_sp av = avma;
    2894     3953340 :   GEN h = RgX_mul_fast(f,g);
    2895     3953340 :   if (!h) return RgXn_mul2(f,g,n);
    2896     2238853 :   if (degpol(h) < n) return h;
    2897     1466808 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    2898             : }
    2899             : 
    2900             : GEN
    2901       16086 : RgXn_sqr(GEN f, long n)
    2902             : {
    2903       16086 :   pari_sp av = avma;
    2904       16086 :   GEN g = RgX_sqr_fast(f);
    2905       16086 :   if (!g) return RgXn_sqr2(f,n);
    2906       15848 :   if (degpol(g) < n) return g;
    2907         770 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    2908             : }
    2909             : 
    2910             : /* (f*g) \/ x^n */
    2911             : GEN
    2912       34929 : RgX_mulhigh_i(GEN f, GEN g, long n)
    2913             : {
    2914       34929 :   GEN h = RgX_mul_fast(f,g);
    2915       34929 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    2916             : }
    2917             : 
    2918             : /* (f*g) \/ x^n */
    2919             : GEN
    2920           0 : RgX_sqrhigh_i(GEN f, long n)
    2921             : {
    2922           0 :   GEN h = RgX_sqr_fast(f);
    2923           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    2924             : }

Generated by: LCOV version 1.11