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 21059-cbe0d6a) Lines: 1454 1629 89.3 %
Date: 2017-09-22 06:24:58 Functions: 166 182 91.2 %
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     2293051 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     2293051 :   long pold=1, rold=n*(d-1);
      32    12701119 :   for(p=2; p<=d; p++)
      33             :   {
      34    10408068 :     r = m*(p-1) + n*((d-1)/p);
      35    10408068 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     2293051 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41    10023876 : 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    10023876 :   pari_sp av = avma;
      45             :   long i;
      46    10023876 :   GEN z = cmul(E,P,a,ff->one(E));
      47    10023914 :   if (!z) z = gen_0;
      48    62364861 :   for (i=1; i<=n; i++)
      49             :   {
      50    52340987 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    52341185 :     if (t) {
      52    51004032 :       z = ff->add(E, z, t);
      53    51003613 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56    10023874 :   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     6433863 : 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     6433863 :   pari_sp av = avma;
      69     6433863 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     6433863 :   if (d < 0) return ff->zero(E);
      73     5941704 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     2406867 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     2406867 :   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     2406867 :   d -= l;
      81     2406867 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     6489040 :   while (d >= l-1)
      83             :   {
      84     1675305 :     d -= l-1;
      85     1675305 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86     1675295 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87     1675306 :     if (gc_needed(av,2))
      88          65 :       z = gerepileupto(av, z);
      89             :   }
      90     2406869 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91     2406868 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92     2406867 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96     1061725 : 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     1061725 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102     1061725 :   if (d < 0) return ff->zero(E);
     103     1061578 :   rtd = (long) sqrt((double)d);
     104     1061578 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105     1061575 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106     1061577 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      729963 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    20663605 : _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      607091 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      197507 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      744691 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       14553 : _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      353010 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      353010 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      125720 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      125720 :   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      237538 :   for (i = 1; i < l; i++)
     149             :   {
     150      234724 :     GEN Qi = gel(Q, i);
     151      234724 :     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       92614 : get_Rg_algebra(void)
     158             : {
     159       92614 :   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    23387939 : RgX_equal(GEN x, GEN y)
     188             : {
     189    23387939 :   long i = lg(x);
     190             : 
     191    23387939 :   if (i != lg(y)) return 0;
     192    98055901 :   for (i--; i > 1; i--)
     193    74730031 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194    23325870 :   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      824903 : RgX_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203      824903 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     204      824903 :   if (RgX_type_is_composite(tx))
     205      477061 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206      824903 :   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      824686 :     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      132965 : RgX_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220      132965 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     221      132965 :   if (RgX_type_is_composite(tx))
     222       28161 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223      132965 :   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      132454 :     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        3913 : RgX_unscale(GEN P, GEN h)
     250             : {
     251        3913 :   long i, l = lg(P);
     252        3913 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253        3913 :   Q[1] = P[1];
     254        3913 :   if (l == 2) return Q;
     255        3913 :   gel(Q,2) = gcopy(gel(P,2));
     256       21987 :   for (i=3; i<l; i++)
     257             :   {
     258       18074 :     hi = gmul(hi,h);
     259       18074 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261        3913 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      892633 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      892633 :   long i, l = lg(P);
     268      892633 :   GEN Q = cgetg(l, t_POL);
     269      892633 :   Q[1] = P[1];
     270      892633 :   if (l == 2) return Q;
     271      892633 :   gel(Q,2) = gel(P,2);
     272      892633 :   if (l == 3) return Q;
     273      892633 :   if (h == -1)
     274      306649 :     for (i = 3; i < l; i++)
     275             :     {
     276      299655 :       gel(Q,i) = negi(gel(P,i));
     277      299655 :       if (++i == l) break;
     278      296373 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      882357 :     gel(Q,3) = mulis(gel(P,3), h);
     284      882357 :     hi = sqrs(h);
     285     1830052 :     for (i = 4; i < l; i++)
     286             :     {
     287      947695 :       gel(Q,i) = mulii(gel(P,i), hi);
     288      947695 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      892633 :   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       18210 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       18210 :   long i, ni = n, l = lg(P);
     319       18210 :   GEN Q = cgetg(l, t_POL);
     320       18210 :   Q[1] = P[1];
     321       18210 :   if (l == 2) return Q;
     322       18210 :   gel(Q,2) = gel(P,2);
     323       18210 :   if (l == 3) return Q;
     324       18210 :   gel(Q,3) = shifti(gel(P,3), ni);
     325       74505 :   for (i=4; i<l; i++)
     326             :   {
     327       56295 :     ni += n;
     328       56295 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       18210 :   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         224 : RgXV_unscale(GEN v, GEN h)
     356             : {
     357             :   long i, l;
     358             :   GEN w;
     359         224 :   if (!h || isint1(h)) return v;
     360         168 :   w = cgetg_copy(v, &l);
     361         168 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     362         168 :   return w;
     363             : }
     364             : 
     365             : /* Return h^degpol(P) P(x / h), not memory clean */
     366             : GEN
     367        1456 : RgX_rescale(GEN P, GEN h)
     368             : {
     369        1456 :   long i, l = lg(P);
     370        1456 :   GEN Q = cgetg(l,t_POL), hi = h;
     371        1456 :   Q[l-1] = P[l-1];
     372        8001 :   for (i=l-2; i>=2; i--)
     373             :   {
     374        8001 :     gel(Q,i) = gmul(gel(P,i), hi);
     375        8001 :     if (i == 2) break;
     376        6545 :     hi = gmul(hi,h);
     377             :   }
     378        1456 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      114915 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      114915 :   long i,id, dy, dx = degpol(x0);
     387      114915 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       65877 :   dy = dx/d;
     389       65877 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       65877 :   z = y + 2;
     391       65877 :   x = x0+ 2;
     392       65877 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       65877 :   return y;
     394             : }
     395             : 
     396             : /* return x0(X^d) */
     397             : GEN
     398      240660 : RgX_inflate(GEN x0, long d)
     399             : {
     400      240660 :   long i, id, dy, dx = degpol(x0);
     401      240660 :   GEN x = x0 + 2, z, y;
     402      240660 :   if (dx <= 0) return leafcopy(x0);
     403      239407 :   dy = dx*d;
     404      239407 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     405      239407 :   z = y + 2;
     406      239407 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     407      239407 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     408      239407 :   return y;
     409             : }
     410             : 
     411             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     412             : GEN
     413     1029136 : RgX_translate(GEN P, GEN c)
     414             : {
     415     1029136 :   pari_sp av = avma;
     416             :   GEN Q, *R;
     417             :   long i, k, n;
     418             : 
     419     1029136 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     420     1027432 :   Q = leafcopy(P);
     421     1027432 :   R = (GEN*)(Q+2); n = degpol(P);
     422     1027432 :   if (gequal1(c))
     423             :   {
     424        2072 :     for (i=1; i<=n; i++)
     425             :     {
     426        1799 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], R[k+1]);
     427        1799 :       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     1027159 :   else if (gequalm1(c))
     435             :   {
     436      133959 :     for (i=1; i<=n; i++)
     437             :     {
     438      114660 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     439      114660 :       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     3487141 :     for (i=1; i<=n; i++)
     449             :     {
     450     2479281 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     451     2479281 :       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     1027432 :   return gerepilecopy(av, Q);
     459             : }
     460             : 
     461             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     462             : GEN
     463      377127 : ZX_translate(GEN P, GEN c)
     464             : {
     465      377127 :   pari_sp av = avma;
     466             :   GEN Q, *R;
     467             :   long i, k, n;
     468             : 
     469      377127 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     470      377092 :   Q = leafcopy(P);
     471      377092 :   R = (GEN*)(Q+2); n = degpol(P);
     472      377092 :   if (equali1(c))
     473             :   {
     474     2374178 :     for (i=1; i<=n; i++)
     475             :     {
     476     2100422 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     477     2100422 :       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      377092 :   return gerepilecopy(av, Q);
     509             : }
     510             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     511             : GEN
     512        6076 : RgXQX_translate(GEN P, GEN c, GEN T)
     513             : {
     514        6076 :   pari_sp av = avma;
     515             :   GEN Q, *R;
     516             :   long i, k, n;
     517             : 
     518        6076 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     519        6076 :   Q = leafcopy(P);
     520        6076 :   R = (GEN*)(Q+2); n = degpol(P);
     521       34965 :   for (i=1; i<=n; i++)
     522             :   {
     523      142198 :     for (k=n-i; k<n; k++)
     524             :     {
     525      113309 :       pari_sp av2 = avma;
     526      113309 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     527             :     }
     528       28889 :     if (gc_needed(av,2))
     529             :     {
     530           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     531           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     532             :     }
     533             :   }
     534        6076 :   return gerepilecopy(av, Q);
     535             : }
     536             : 
     537             : /********************************************************************/
     538             : /**                                                                **/
     539             : /**                          CONVERSIONS                           **/
     540             : /**                       (not memory clean)                       **/
     541             : /**                                                                **/
     542             : /********************************************************************/
     543             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     544             :  * but everything else is */
     545             : static GEN
     546       18134 : QXQ_to_mod_copy(GEN x, GEN T)
     547             : {
     548             :   long d;
     549       18134 :   switch(typ(x))
     550             :   {
     551        7238 :     case t_INT:  return icopy(x);
     552         805 :     case t_FRAC: return gcopy(x);
     553             :     case t_POL:
     554       10091 :       d = degpol(x);
     555       10091 :       if (d < 0) return gen_0;
     556        9769 :       if (d == 0) return gcopy(gel(x,2));
     557        9594 :       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      506760 : QXQ_to_mod(GEN x, GEN T)
     565             : {
     566             :   long d;
     567      506760 :   switch(typ(x))
     568             :   {
     569             :     case t_INT:
     570      401090 :     case t_FRAC: return x;
     571             :     case t_POL:
     572      105670 :       d = degpol(x);
     573      105670 :       if (d < 0) return gen_0;
     574       96899 :       if (d == 0) return gel(x,2);
     575       90411 :       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        1939 : QXQX_to_mod(GEN z, GEN T)
     584             : {
     585        1939 :   long i,l = lg(z);
     586        1939 :   GEN x = cgetg(l,t_POL);
     587        1939 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     588        1939 :   x[1] = z[1]; return normalizepol_lg(x,l);
     589             : }
     590             : /* pure shallow version */
     591             : GEN
     592      116832 : QXQX_to_mod_shallow(GEN z, GEN T)
     593             : {
     594      116832 :   long i,l = lg(z);
     595      116832 :   GEN x = cgetg(l,t_POL);
     596      116832 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     597      116832 :   x[1] = z[1]; return normalizepol_lg(x,l);
     598             : }
     599             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     600             : GEN
     601         609 : QXQXV_to_mod(GEN V, GEN T)
     602             : {
     603         609 :   long i, l = lg(V);
     604         609 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     605         609 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     606         609 :   return z;
     607             : }
     608             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     609             : GEN
     610        4726 : QXQV_to_mod(GEN V, GEN T)
     611             : {
     612        4726 :   long i, l = lg(V);
     613        4726 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     614        4726 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     615        4726 :   return z;
     616             : }
     617             : 
     618             : GEN
     619      720375 : RgX_renormalize_lg(GEN x, long lx)
     620             : {
     621             :   long i;
     622     1981377 :   for (i = lx-1; i>1; i--)
     623     1866206 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     624      720375 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     625      720375 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     626             : }
     627             : 
     628             : GEN
     629      488391 : RgV_to_RgX(GEN x, long v)
     630             : {
     631      488391 :   long i, k = lg(x);
     632             :   GEN p;
     633             : 
     634      488391 :   while (--k && gequal0(gel(x,k)));
     635      488391 :   if (!k) return pol_0(v);
     636      469883 :   i = k+2; p = cgetg(i,t_POL);
     637      469883 :   p[1] = evalsigne(1) | evalvarn(v);
     638      469883 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     639      469883 :   return p;
     640             : }
     641             : GEN
     642      149566 : RgV_to_RgX_reverse(GEN x, long v)
     643             : {
     644      149566 :   long j, k, l = lg(x);
     645             :   GEN p;
     646             : 
     647      149566 :   for (k = 1; k < l; k++)
     648      149566 :     if (!gequal0(gel(x,k))) break;
     649      149566 :   if (k == l) return pol_0(v);
     650      149566 :   k -= 1;
     651      149566 :   l -= k;
     652      149566 :   x += k;
     653      149566 :   p = cgetg(l+1,t_POL);
     654      149566 :   p[1] = evalsigne(1) | evalvarn(v);
     655      149566 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     656      149566 :   return p;
     657             : }
     658             : 
     659             : /* return the (N-dimensional) vector of coeffs of p */
     660             : GEN
     661     4082583 : RgX_to_RgC(GEN x, long N)
     662             : {
     663             :   long i, l;
     664             :   GEN z;
     665     4082583 :   l = lg(x)-1; x++;
     666     4082583 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     667     4082583 :   z = cgetg(N+1,t_COL);
     668     4082583 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     669     4082583 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     670     4082583 :   return z;
     671             : }
     672             : GEN
     673      680107 : Rg_to_RgC(GEN x, long N)
     674             : {
     675      680107 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     676             : }
     677             : 
     678             : /* vector of polynomials (in v) whose coeffs are given by the columns of x */
     679             : GEN
     680       48059 : RgM_to_RgXV(GEN x, long v)
     681             : {
     682       48059 :   long j, lx = lg(x);
     683       48059 :   GEN y = cgetg(lx, t_VEC);
     684       48059 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), v);
     685       48059 :   return y;
     686             : }
     687             : 
     688             : /* matrix whose entries are given by the coeffs of the polynomials in
     689             :  * vector v (considered as degree n-1 polynomials) */
     690             : GEN
     691      103589 : RgV_to_RgM(GEN v, long n)
     692             : {
     693      103589 :   long j, N = lg(v);
     694      103589 :   GEN y = cgetg(N, t_MAT);
     695      103589 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j), n);
     696      103589 :   return y;
     697             : }
     698             : GEN
     699        2266 : RgXV_to_RgM(GEN v, long n)
     700             : {
     701        2266 :   long j, N = lg(v);
     702        2266 :   GEN y = cgetg(N, t_MAT);
     703        2266 :   for (j=1; j<N; j++) gel(y,j) = RgX_to_RgC(gel(v,j), n);
     704        2266 :   return y;
     705             : }
     706             : 
     707             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     708             : GEN
     709       15365 : RgM_to_RgXX(GEN x, long v,long w)
     710             : {
     711       15365 :   long j, lx = lg(x);
     712       15365 :   GEN y = cgetg(lx+1, t_POL);
     713       15365 :   y[1] = evalsigne(1) | evalvarn(v);
     714       15365 :   y++;
     715       15365 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     716       15365 :   return normalizepol_lg(--y, lx+1);
     717             : }
     718             : 
     719             : /* matrix whose entries are given by the coeffs of the polynomial v in
     720             :  * two variables (considered as degree n-1 polynomials) */
     721             : GEN
     722          21 : RgXX_to_RgM(GEN v, long n)
     723             : {
     724          21 :   long j, N = lg(v)-1;
     725          21 :   GEN y = cgetg(N, t_MAT);
     726          21 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     727          21 :   return y;
     728             : }
     729             : 
     730             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     731             : GEN
     732       12336 : RgXY_swapspec(GEN x, long n, long w, long nx)
     733             : {
     734       12336 :   long j, ly = n+3;
     735       12336 :   GEN y = cgetg(ly, t_POL);
     736       12336 :   y[1] = evalsigne(1);
     737      192081 :   for (j=2; j<ly; j++)
     738             :   {
     739             :     long k;
     740      179745 :     GEN a = cgetg(nx+2,t_POL);
     741      179745 :     a[1] = evalsigne(1) | evalvarn(w);
     742      957129 :     for (k=0; k<nx; k++)
     743             :     {
     744      777384 :       GEN xk = gel(x,k);
     745      777384 :       if (typ(xk)==t_POL)
     746      691809 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     747             :       else
     748       85575 :         gel(a,k+2) = j==2 ? xk: gen_0;
     749             :     }
     750      179745 :     gel(y,j) = normalizepol_lg(a, nx+2);
     751             :   }
     752       12336 :   return normalizepol_lg(y,ly);
     753             : }
     754             : 
     755             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     756             : GEN
     757         224 : RgXY_swap(GEN x, long n, long w)
     758             : {
     759         224 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     760         224 :   setvarn(z, varn(x)); return z;
     761             : }
     762             : 
     763             : long
     764          92 : RgXY_degreex(GEN b)
     765             : {
     766          92 :   long deg = -1, i;
     767          92 :   if (!signe(b)) return -1;
     768         584 :   for (i = 2; i < lg(b); ++i)
     769             :   {
     770         492 :     GEN bi = gel(b, i);
     771         492 :     if (typ(bi) == t_POL)
     772         470 :       deg = maxss(deg, degpol(bi));
     773             :   }
     774          92 :   return deg;
     775             : }
     776             : 
     777             : /* return (x % X^n). Shallow */
     778             : GEN
     779     1373990 : RgXn_red_shallow(GEN a, long n)
     780             : {
     781     1373990 :   long i, L = n+2, l = lg(a);
     782             :   GEN  b;
     783     1373990 :   if (L >= l) return a; /* deg(x) < n */
     784     1337968 :   b = cgetg(L, t_POL); b[1] = a[1];
     785     1337968 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     786     1337968 :   return normalizepol_lg(b,L);
     787             : }
     788             : 
     789             : GEN
     790         336 : RgXnV_red_shallow(GEN P, long n)
     791             : {
     792         336 :   long i, l = lg(P);
     793         336 :   GEN Q = cgetg(l, t_VEC);
     794         336 :   for (i=1; i<l; i++) gel(Q,i) = RgXn_red_shallow(gel(P,i), n);
     795         336 :   return Q;
     796             : }
     797             : 
     798             : /* return (x * X^n). Shallow */
     799             : GEN
     800    57748558 : RgX_shift_shallow(GEN a, long n)
     801             : {
     802    57748558 :   long i, l = lg(a);
     803             :   GEN  b;
     804    57748558 :   if (l == 2 || !n) return a;
     805    42560858 :   l += n;
     806    42560858 :   if (n < 0)
     807             :   {
     808    37595163 :     if (l <= 2) return pol_0(varn(a));
     809    37576648 :     b = cgetg(l, t_POL); b[1] = a[1];
     810    37576649 :     a -= n;
     811    37576649 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     812             :   } else {
     813     4965695 :     b = cgetg(l, t_POL); b[1] = a[1];
     814     4965696 :     a -= n; n += 2;
     815     4965696 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     816     4965696 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     817             :   }
     818    42542345 :   return b;
     819             : }
     820             : /* return (x * X^n). */
     821             : GEN
     822     3402753 : RgX_shift(GEN a, long n)
     823             : {
     824     3402753 :   long i, l = lg(a);
     825             :   GEN  b;
     826     3402753 :   if (l == 2 || !n) return RgX_copy(a);
     827     3402522 :   l += n;
     828     3402522 :   if (n < 0)
     829             :   {
     830         595 :     if (l <= 2) return pol_0(varn(a));
     831         553 :     b = cgetg(l, t_POL); b[1] = a[1];
     832         553 :     a -= n;
     833         553 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     834             :   } else {
     835     3401927 :     b = cgetg(l, t_POL); b[1] = a[1];
     836     3401927 :     a -= n; n += 2;
     837     3401927 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     838     3401927 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     839             :   }
     840     3402480 :   return b;
     841             : }
     842             : 
     843             : GEN
     844      315273 : RgX_rotate_shallow(GEN P, long k, long p)
     845             : {
     846      315273 :   long i, l = lgpol(P);
     847             :   GEN r;
     848      315273 :   if (signe(P)==0)
     849         427 :     return pol_0(varn(P));
     850      314846 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     851     2091390 :   for(i=0; i<p; i++)
     852             :   {
     853     1776544 :     long s = 2+(i+k)%p;
     854     1776544 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     855             :   }
     856      314846 :   return RgX_renormalize(r);
     857             : }
     858             : 
     859             : GEN
     860     2859209 : RgX_mulXn(GEN x, long d)
     861             : {
     862             :   pari_sp av;
     863             :   GEN z;
     864             :   long v;
     865     2859209 :   if (d >= 0) return RgX_shift(x, d);
     866     1366780 :   d = -d;
     867     1366780 :   v = RgX_val(x);
     868     1366780 :   if (v >= d) return RgX_shift(x, -d);
     869     1366773 :   av = avma;
     870     1366773 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     871     1366773 :   return gerepileupto(av, z);
     872             : }
     873             : 
     874             : long
     875     2095195 : RgX_val(GEN x)
     876             : {
     877     2095195 :   long i, lx = lg(x);
     878     2095195 :   if (lx == 2) return LONG_MAX;
     879     2120773 :   for (i = 2; i < lx; i++)
     880     2120759 :     if (!isexactzero(gel(x,i))) break;
     881     2095153 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     882     2095139 :   return i - 2;
     883             : }
     884             : long
     885    42479736 : RgX_valrem(GEN x, GEN *Z)
     886             : {
     887    42479736 :   long v, i, lx = lg(x);
     888    42479736 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     889    81815752 :   for (i = 2; i < lx; i++)
     890    81815752 :     if (!isexactzero(gel(x,i))) break;
     891             :   /* possible with non-rational zeros */
     892    42479736 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     893    42479736 :   v = i - 2;
     894    42479736 :   *Z = RgX_shift_shallow(x, -v);
     895    42479736 :   return v;
     896             : }
     897             : long
     898        9557 : RgX_valrem_inexact(GEN x, GEN *Z)
     899             : {
     900             :   long v;
     901        9557 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     902        9851 :   for (v = 0;; v++)
     903        9851 :     if (!gequal0(gel(x,2+v))) break;
     904         301 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     905        9550 :   return v;
     906             : }
     907             : 
     908             : GEN
     909           0 : RgXQC_red(GEN P, GEN T)
     910             : {
     911           0 :   long i, l = lg(P);
     912           0 :   GEN Q = cgetg(l, t_COL);
     913           0 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     914           0 :   return Q;
     915             : }
     916             : 
     917             : GEN
     918          56 : RgXQV_red(GEN P, GEN T)
     919             : {
     920          56 :   long i, l = lg(P);
     921          56 :   GEN Q = cgetg(l, t_VEC);
     922          56 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     923          56 :   return Q;
     924             : }
     925             : 
     926             : GEN
     927           0 : RgXQM_red(GEN P, GEN T)
     928             : {
     929           0 :   long i, l = lg(P);
     930           0 :   GEN Q = cgetg(l, t_MAT);
     931           0 :   for (i=1; i<l; i++) gel(Q,i) = RgXQC_red(gel(P,i), T);
     932           0 :   return Q;
     933             : }
     934             : 
     935             : GEN
     936           0 : RgXQM_mul(GEN P, GEN Q, GEN T)
     937             : {
     938           0 :   return RgXQM_red(RgM_mul(P, Q), T);
     939             : }
     940             : 
     941             : GEN
     942       40154 : RgXQX_red(GEN P, GEN T)
     943             : {
     944       40154 :   long i, l = lg(P);
     945       40154 :   GEN Q = cgetg(l, t_POL);
     946       40154 :   Q[1] = P[1];
     947       40154 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     948       40154 :   return normalizepol_lg(Q, l);
     949             : }
     950             : 
     951             : GEN
     952      221899 : RgX_deriv(GEN x)
     953             : {
     954      221899 :   long i,lx = lg(x)-1;
     955             :   GEN y;
     956             : 
     957      221899 :   if (lx<3) return pol_0(varn(x));
     958      194767 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     959      194767 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     960      194767 :   y[1] = x[1]; return normalizepol_lg(y,i);
     961             : }
     962             : 
     963             : GEN
     964      297854 : RgX_recipspec_shallow(GEN x, long l, long n)
     965             : {
     966             :   long i;
     967      297854 :   GEN z=cgetg(n+2,t_POL)+2;
     968    14727260 :   for(i=0; i<l; i++)
     969    14429406 :     gel(z,n-i-1) = gel(x,i);
     970      385630 :   for(   ; i<n; i++)
     971       87776 :     gel(z, n-i-1) = gen_0;
     972      297854 :   return normalizepol_lg(z-2,n+2);
     973             : }
     974             : 
     975             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     976             : GEN
     977        1708 : RgX_recip(GEN x)
     978             : {
     979             :   long lx, i, j;
     980        1708 :   GEN y = cgetg_copy(x, &lx);
     981        1708 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     982        1708 :   return normalizepol_lg(y,lx);
     983             : }
     984             : /* shallow version */
     985             : GEN
     986      328901 : RgX_recip_shallow(GEN x)
     987             : {
     988             :   long lx, i, j;
     989      328901 :   GEN y = cgetg_copy(x, &lx);
     990      328909 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     991      328909 :   return y;
     992             : }
     993             : /*******************************************************************/
     994             : /*                                                                 */
     995             : /*                      ADDITION / SUBTRACTION                     */
     996             : /*                                                                 */
     997             : /*******************************************************************/
     998             : /* same variable */
     999             : GEN
    1000    25283177 : RgX_add(GEN x, GEN y)
    1001             : {
    1002    25283177 :   long i, lx = lg(x), ly = lg(y);
    1003             :   GEN z;
    1004    25283177 :   if (ly <= lx) {
    1005    23394846 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1006    23394846 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1007    23394846 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1008    23394846 :     z = normalizepol_lg(z, lx);
    1009             :   } else {
    1010     1888331 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1011     1888331 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1012     1888331 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1013     1888331 :     z = normalizepol_lg(z, ly);
    1014             :   }
    1015    25283177 :   return z;
    1016             : }
    1017             : GEN
    1018    11408326 : RgX_sub(GEN x, GEN y)
    1019             : {
    1020    11408326 :   long i, lx = lg(x), ly = lg(y);
    1021             :   GEN z;
    1022    11408326 :   if (ly <= lx) {
    1023     8967416 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1024     8967416 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1025     8967416 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1026     8967416 :     z = normalizepol_lg(z, lx);
    1027             :   } else {
    1028     2440910 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1029     2440910 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1030     2440910 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1031     2440910 :     z = normalizepol_lg(z, ly);
    1032             :   }
    1033    11408326 :   return z;
    1034             : }
    1035             : GEN
    1036     1022905 : RgX_neg(GEN x)
    1037             : {
    1038     1022905 :   long i, lx = lg(x);
    1039     1022905 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1040     1022905 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1041     1022905 :   return y;
    1042             : }
    1043             : 
    1044             : GEN
    1045    11279548 : RgX_Rg_add(GEN y, GEN x)
    1046             : {
    1047             :   GEN z;
    1048    11279548 :   long lz = lg(y), i;
    1049    11279548 :   if (lz == 2) return scalarpol(x,varn(y));
    1050     9601527 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1051     9601527 :   gel(z,2) = gadd(gel(y,2),x);
    1052     9601527 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1053             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1054             :    * an inexact 0 */
    1055     9601527 :   return normalizepol_lg(z,lz);
    1056             : }
    1057             : GEN
    1058        2422 : RgX_Rg_add_shallow(GEN y, GEN x)
    1059             : {
    1060             :   GEN z;
    1061        2422 :   long lz = lg(y), i;
    1062        2422 :   if (lz == 2) return scalarpol(x,varn(y));
    1063        2422 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1064        2422 :   gel(z,2) = gadd(gel(y,2),x);
    1065        2422 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1066        2422 :   return z = normalizepol_lg(z,lz);
    1067             : }
    1068             : GEN
    1069       32124 : RgX_Rg_sub(GEN y, GEN x)
    1070             : {
    1071             :   GEN z;
    1072       32124 :   long lz = lg(y), i;
    1073       32124 :   if (lz == 2)
    1074             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1075        3864 :     long v = varn(y);
    1076        3864 :     if (isrationalzero(x)) return pol_0(v);
    1077          14 :     z = cgetg(3,t_POL);
    1078          28 :     z[1] = gequal0(x)? evalvarn(v)
    1079          14 :                    : evalvarn(v) | evalsigne(1);
    1080          14 :     gel(z,2) = gneg(x); return z;
    1081             :   }
    1082       28260 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1083       28260 :   gel(z,2) = gsub(gel(y,2),x);
    1084       28260 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1085       28260 :   return z = normalizepol_lg(z,lz);
    1086             : }
    1087             : GEN
    1088      380057 : Rg_RgX_sub(GEN x, GEN y)
    1089             : {
    1090             :   GEN z;
    1091      380057 :   long lz = lg(y), i;
    1092      380057 :   if (lz == 2) return scalarpol(x,varn(y));
    1093      379042 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1094      379042 :   gel(z,2) = gsub(x, gel(y,2));
    1095      379042 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1096      379042 :   return z = normalizepol_lg(z,lz);
    1097             : }
    1098             : /*******************************************************************/
    1099             : /*                                                                 */
    1100             : /*                  KARATSUBA MULTIPLICATION                       */
    1101             : /*                                                                 */
    1102             : /*******************************************************************/
    1103             : #if 0
    1104             : /* to debug Karatsuba-like routines */
    1105             : GEN
    1106             : zx_debug_spec(GEN x, long nx)
    1107             : {
    1108             :   GEN z = cgetg(nx+2,t_POL);
    1109             :   long i;
    1110             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1111             :   z[1] = evalsigne(1); return z;
    1112             : }
    1113             : 
    1114             : GEN
    1115             : RgX_debug_spec(GEN x, long nx)
    1116             : {
    1117             :   GEN z = cgetg(nx+2,t_POL);
    1118             :   long i;
    1119             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1120             :   z[1] = evalsigne(1); return z;
    1121             : }
    1122             : #endif
    1123             : 
    1124             : /* generic multiplication */
    1125             : GEN
    1126     2942027 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1127             : {
    1128             :   GEN z, t;
    1129             :   long i;
    1130     2942027 :   if (nx == ny) {
    1131      664471 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1132      664471 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1133      664471 :     return normalizepol_lg(z, nx+2);
    1134             :   }
    1135     2277556 :   if (ny < nx) {
    1136     2118135 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1137     2118135 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1138     2118135 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1139     2118135 :     return normalizepol_lg(z, nx+2);
    1140             :   } else {
    1141      159421 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1142      159421 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1143      159421 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1144      159421 :     return normalizepol_lg(z, ny+2);
    1145             :   }
    1146             : }
    1147             : GEN
    1148      195930 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1149             : {
    1150             :   GEN z, t;
    1151             :   long i;
    1152      195930 :   if (nx == ny) {
    1153         300 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1154         300 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1155         300 :     return normalizepol_lg(z, nx+2);
    1156             :   }
    1157      195630 :   if (ny < nx) {
    1158      195630 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1159      195630 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1160      195630 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1161      195630 :     return normalizepol_lg(z, nx+2);
    1162             :   } else {
    1163           0 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1164           0 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1165           0 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1166           0 :     return normalizepol_lg(z, ny+2);
    1167             :   }
    1168             : }
    1169             : 
    1170             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1171             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1172             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1173             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1174             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1175             : static GEN
    1176    19077837 : RgXspec_kill0(GEN x, long lx)
    1177             : {
    1178    19077837 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1179             :   long i;
    1180    72972464 :   for (i=0; i <lx; i++)
    1181             :   {
    1182    53894627 :     GEN c = gel(x,i);
    1183    53894627 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1184             :   }
    1185    19077837 :   return z;
    1186             : }
    1187             : 
    1188             : INLINE GEN
    1189    41077579 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1190             : {
    1191    41077579 :   pari_sp av = avma;
    1192    41077579 :   GEN s = NULL;
    1193             :   long i;
    1194             : 
    1195   142710461 :   for (i=a; i<b; i++)
    1196   101632882 :     if (gel(y,i) && gel(x,-i))
    1197             :     {
    1198    57476913 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1199    57476913 :       s = s? gadd(s, t): t;
    1200             :     }
    1201    41077579 :   return s? gerepileupto(av, s): gen_0;
    1202             : }
    1203             : 
    1204             : /* assume nx >= ny > 0, return x * y * t^v */
    1205             : static GEN
    1206     7895724 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1207             : {
    1208             :   long i, lz, nz;
    1209             :   GEN z;
    1210             : 
    1211     7895724 :   x = RgXspec_kill0(x,nx);
    1212     7895724 :   y = RgXspec_kill0(y,ny);
    1213     7895724 :   lz = nx + ny + 1; nz = lz-2;
    1214     7895724 :   lz += v;
    1215     7895724 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1216     7895724 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1217     7895724 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1218     7895724 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1219     7895724 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1220     7895724 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1221             : }
    1222             : 
    1223             : /* return (x * X^d) + y. Assume d > 0 */
    1224             : GEN
    1225     2017937 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1226             : {
    1227             :   GEN x, y, xd, yd, zd;
    1228             :   long a, lz, nx, ny;
    1229             : 
    1230     2017937 :   if (!signe(x0)) return y0;
    1231     1998811 :   ny = lgpol(y0);
    1232     1998811 :   nx = lgpol(x0);
    1233     1998811 :   zd = (GEN)avma;
    1234     1998811 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1235     1998811 :   if (a <= 0)
    1236             :   {
    1237      163881 :     lz = nx+d+2;
    1238      163881 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1239      163881 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1240      163881 :     x = zd + a;
    1241      163881 :     while (zd > x) gel(--zd,0) = gen_0;
    1242             :   }
    1243             :   else
    1244             :   {
    1245     1834930 :     xd = new_chunk(d); yd = y+d;
    1246     1834930 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1247     1834930 :     lz = (a>nx)? ny+2: lg(x)+d;
    1248     1834930 :     x += 2; while (xd > x) *--zd = *--xd;
    1249             :   }
    1250     1998811 :   while (yd > y) *--zd = *--yd;
    1251     1998811 :   *--zd = x0[1];
    1252     1998811 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1253             : }
    1254             : GEN
    1255      486357 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1256             : {
    1257             :   GEN x, y, xd, yd, zd;
    1258             :   long a, lz, nx, ny;
    1259             : 
    1260      486357 :   if (!signe(x0)) return RgX_copy(y0);
    1261      486242 :   nx = lgpol(x0);
    1262      486242 :   ny = lgpol(y0);
    1263      486242 :   zd = (GEN)avma;
    1264      486242 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1265      486242 :   if (a <= 0)
    1266             :   {
    1267      290312 :     lz = nx+d+2;
    1268      290312 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1269      290312 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1270      290312 :     x = zd + a;
    1271      290312 :     while (zd > x) gel(--zd,0) = gen_0;
    1272             :   }
    1273             :   else
    1274             :   {
    1275      195930 :     xd = new_chunk(d); yd = y+d;
    1276      195930 :     x = RgX_addspec(x,yd, nx,a);
    1277      195930 :     lz = (a>nx)? ny+2: lg(x)+d;
    1278      195930 :     x += 2; while (xd > x) *--zd = *--xd;
    1279             :   }
    1280      486242 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1281      486242 :   *--zd = x0[1];
    1282      486242 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1283             : }
    1284             : 
    1285             : /* return x * y mod t^n */
    1286             : static GEN
    1287     1638572 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1288             : {
    1289     1638572 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1290             :   GEN z;
    1291     1638572 :   if (lx < 0) return pol_0(varn(x));
    1292     1638572 :   if (ly < 0) return pol_0(varn(x));
    1293     1638572 :   z = cgetg(lz, t_POL) + 2;
    1294     1638572 :   x+=2; if (lx > n) lx = n;
    1295     1638572 :   y+=2; if (ly > n) ly = n;
    1296     1638572 :   z[-1] = x[-1];
    1297     1638572 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1298     1638572 :   x = RgXspec_kill0(x, lx);
    1299     1638572 :   y = RgXspec_kill0(y, ly);
    1300             :   /* x:y:z [i] = term of degree i */
    1301     1638572 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1302     1638572 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1303     1638572 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1304     1638572 :   return normalizepol_lg(z - 2, lz);
    1305             : }
    1306             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1307             : static GEN
    1308     1665564 : RgXn_mul2(GEN f, GEN g, long n)
    1309             : {
    1310     1665564 :   pari_sp av = avma;
    1311             :   GEN fe,fo, ge,go, l,h,m;
    1312             :   long n0, n1;
    1313     1665564 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1314     1639503 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1315         931 :   n0 = n>>1; n1 = n-n0;
    1316         931 :   RgX_even_odd(f, &fe, &fo);
    1317         931 :   RgX_even_odd(g, &ge, &go);
    1318         931 :   l = RgXn_mul(fe,ge,n1);
    1319         931 :   h = RgXn_mul(fo,go,n0);
    1320         931 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1321             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1322             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1323         931 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1324             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1325         931 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1326         931 :   m = RgX_inflate(m,2);
    1327             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1328         931 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1329         931 :   h = RgX_inflate(h,2);
    1330         931 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1331         931 :   return gerepileupto(av, h);
    1332             : }
    1333             : /* (f*g) \/ x^n */
    1334             : static GEN
    1335        1322 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1336             : {
    1337        1322 :   long d = degpol(f)+degpol(g) + 1 - n;
    1338             :   GEN h;
    1339        1322 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1340        1046 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1341             :                                  RgX_recip_shallow(g), d));
    1342        1046 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1343             : }
    1344             : 
    1345             : /* (f*g) \/ x^n */
    1346             : static GEN
    1347           0 : RgX_sqrhigh_i2(GEN f, long n)
    1348             : {
    1349           0 :   long d = 2*degpol(f)+ 1 - n;
    1350             :   GEN h;
    1351           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1352           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1353           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1354             : }
    1355             : 
    1356             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1357             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1358             :  * Only c, c0, c1, c2 are genuine GEN.
    1359             :  */
    1360             : GEN
    1361     8445570 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1362             : {
    1363             :   GEN a0, c, c0;
    1364     8445570 :   long n0, n0a, i, v = 0;
    1365             :   pari_sp av;
    1366             : 
    1367     8445570 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1368     8445570 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1369     8445570 :   if (na < nb) swapspec(a,b, na,nb);
    1370     8445570 :   if (!nb) return pol_0(0);
    1371             : 
    1372     8380725 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1373      485001 :   RgX_shift_inplace_init(v);
    1374      485001 :   i = (na>>1); n0 = na-i; na = i;
    1375      485001 :   av = avma; a0 = a+n0; n0a = n0;
    1376      485001 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1377             : 
    1378      485001 :   if (nb > n0)
    1379             :   {
    1380             :     GEN b0,c1,c2;
    1381             :     long n0b;
    1382             : 
    1383      483153 :     nb -= n0; b0 = b+n0; n0b = n0;
    1384      483153 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1385      483153 :     c = RgX_mulspec(a,b,n0a,n0b);
    1386      483153 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1387             : 
    1388      483153 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1389      483153 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1390             : 
    1391      483153 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1392      483153 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1393      483153 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1394             :   }
    1395             :   else
    1396             :   {
    1397        1848 :     c = RgX_mulspec(a,b,n0a,nb);
    1398        1848 :     c0 = RgX_mulspec(a0,b,na,nb);
    1399             :   }
    1400      485001 :   c0 = RgX_addmulXn(c0,c,n0);
    1401      485001 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1402             : }
    1403             : 
    1404             : INLINE GEN
    1405       98569 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1406             : {
    1407       98569 :   pari_sp av = avma;
    1408       98569 :   GEN s = NULL;
    1409       98569 :   long j, l = (i+1)>>1;
    1410      442113 :   for (j=a; j<l; j++)
    1411             :   {
    1412      343544 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1413      343544 :     if (xj && xx)
    1414             :     {
    1415      336012 :       GEN t = gmul(xj, xx);
    1416      336012 :       s = s? gadd(s, t): t;
    1417             :     }
    1418             :   }
    1419       98569 :   if (s) s = gshift(s,1);
    1420       98569 :   if ((i&1) == 0)
    1421             :   {
    1422       53907 :     GEN t = gel(x, i>>1);
    1423       53907 :     if (t) {
    1424       51534 :       t = gsqr(t);
    1425       51534 :       s = s? gadd(s, t): t;
    1426             :     }
    1427             :   }
    1428       98569 :   return s? gerepileupto(av,s): gen_0;
    1429             : }
    1430             : static GEN
    1431        9245 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1432             : {
    1433             :   long i, lz, nz;
    1434             :   GEN z;
    1435             : 
    1436        9245 :   if (!nx) return pol_0(0);
    1437        9245 :   x = RgXspec_kill0(x,nx);
    1438        9245 :   lz = (nx << 1) + 1, nz = lz-2;
    1439        9245 :   lz += v;
    1440        9245 :   z = cgetg(lz,t_POL) + 2;
    1441        9245 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1442        9245 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1443        9245 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1444        9245 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1445             : }
    1446             : /* return x^2 mod t^n */
    1447             : static GEN
    1448           0 : RgXn_sqr_basecase(GEN x, long n)
    1449             : {
    1450           0 :   long i, lz = n+2, lx = lgpol(x);
    1451             :   GEN z;
    1452           0 :   if (lx < 0) return pol_0(varn(x));
    1453           0 :   z = cgetg(lz, t_POL);
    1454           0 :   z[1] = x[1];
    1455           0 :   x+=2; if (lx > n) lx = n;
    1456           0 :   x = RgXspec_kill0(x,lx);
    1457           0 :   z+=2;/* x:z [i] = term of degree i */
    1458           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1459           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1460           0 :   z -= 2; return normalizepol_lg(z, lz);
    1461             : }
    1462             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1463             : static GEN
    1464         238 : RgXn_sqr2(GEN f, long n)
    1465             : {
    1466         238 :   pari_sp av = avma;
    1467             :   GEN fe,fo, l,h,m;
    1468             :   long n0, n1;
    1469         238 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1470           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1471           0 :   n0 = n>>1; n1 = n-n0;
    1472           0 :   RgX_even_odd(f, &fe, &fo);
    1473           0 :   l = RgXn_sqr(fe,n1);
    1474           0 :   h = RgXn_sqr(fo,n0);
    1475           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1476             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1477             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1478           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1479             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1480           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1481           0 :   m = RgX_inflate(m,2);
    1482             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1483           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1484           0 :   h = RgX_inflate(h,2);
    1485           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1486           0 :   return gerepileupto(av, h);
    1487             : }
    1488             : GEN
    1489        9670 : RgX_sqrspec(GEN a, long na)
    1490             : {
    1491             :   GEN a0, c, c0, c1;
    1492        9670 :   long n0, n0a, i, v = 0;
    1493             :   pari_sp av;
    1494             : 
    1495        9670 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1496        9670 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1497         425 :   RgX_shift_inplace_init(v);
    1498         425 :   i = (na>>1); n0 = na-i; na = i;
    1499         425 :   av = avma; a0 = a+n0; n0a = n0;
    1500         425 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1501             : 
    1502         425 :   c = RgX_sqrspec(a,n0a);
    1503         425 :   c0 = RgX_sqrspec(a0,na);
    1504         425 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1505         425 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1506         425 :   c0 = RgX_addmulXn(c0,c,n0);
    1507         425 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1508             : }
    1509             : 
    1510             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1511             : GEN
    1512      574697 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1513             : {
    1514      574697 :   GEN z = RgX_mul(A, B);
    1515      574697 :   if (a < b)
    1516        5446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1517      569251 :   else if (a > b)
    1518      344497 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1519             :   else
    1520      224754 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1521      574697 :   return z;
    1522             : }
    1523             : 
    1524             : GEN
    1525     6991990 : RgX_mul_i(GEN x, GEN y)
    1526             : {
    1527     6991990 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1528     6991990 :   setvarn(z, varn(x)); return z;
    1529             : }
    1530             : 
    1531             : GEN
    1532        8820 : RgX_sqr_i(GEN x)
    1533             : {
    1534        8820 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1535        8820 :   setvarn(z,varn(x)); return z;
    1536             : }
    1537             : 
    1538             : /*******************************************************************/
    1539             : /*                                                                 */
    1540             : /*                               DIVISION                          */
    1541             : /*                                                                 */
    1542             : /*******************************************************************/
    1543             : GEN
    1544      432374 : RgX_Rg_divexact(GEN x, GEN y) {
    1545             :   long i, lx;
    1546             :   GEN z;
    1547      432374 :   if (typ(y) == t_INT && is_pm1(y))
    1548       27711 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1549      404663 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1550      404663 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1551      404663 :   return z;
    1552             : }
    1553             : GEN
    1554    22578834 : RgX_Rg_div(GEN x, GEN y) {
    1555             :   long i, lx;
    1556    22578834 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1557    22578834 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1558    22578834 :   return normalizepol_lg(z, lx);
    1559             : }
    1560             : GEN
    1561        1981 : RgX_normalize(GEN x)
    1562             : {
    1563        1981 :   GEN d = NULL;
    1564        1981 :   long i, n = lg(x)-1;
    1565        1981 :   for (i = n; i > 1; i--)
    1566             :   {
    1567        1981 :     d = gel(x,i);
    1568        1981 :     if (!gequal0(d)) break;
    1569             :   }
    1570        1981 :   if (i == 1) return pol_0(varn(x));
    1571        1981 :   if (i == n && isint1(d)) return x;
    1572         728 :   return normalizepol_lg(RgX_Rg_div(x, d), i+1);
    1573             : }
    1574             : GEN
    1575        2457 : RgX_divs(GEN x, long y) {
    1576             :   long i, lx;
    1577        2457 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1578        2457 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1579        2457 :   return normalizepol_lg(z, lx);
    1580             : }
    1581             : GEN
    1582       39161 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1583             : {
    1584       39161 :   long l = lg(a), i;
    1585       39161 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1586       39161 :   z[1] = a[1];
    1587       39161 :   a0 = a + l-1;
    1588       39161 :   z0 = z + l-2; *z0 = *a0--;
    1589      779374 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1590             :   {
    1591      740213 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1592      740213 :     gel(z0,0) = t;
    1593             :   }
    1594       39161 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1595       39161 :   return z;
    1596             : }
    1597             : /* Polynomial division x / y:
    1598             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1599             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1600             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1601             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1602             : static GEN
    1603    11607183 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1604             : {
    1605             :   pari_sp avy, av, av1;
    1606             :   long dx,dy,dz,i,j,sx,lr;
    1607             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1608             :   GEN (*f)(GEN,GEN);
    1609             : 
    1610    11607183 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1611             : 
    1612    11607183 :   dy = degpol(y);
    1613    11607183 :   y_lead = gel(y,dy+2);
    1614    11607183 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1615             :   {
    1616           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1617           0 :     for (dy--; dy>=0; dy--)
    1618             :     {
    1619           0 :       y_lead = gel(y,dy+2);
    1620           0 :       if (!gequal0(y_lead)) break;
    1621             :     }
    1622             :   }
    1623    11607183 :   if (!dy) /* y is constant */
    1624             :   {
    1625       14893 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1626       14893 :     z = RgX_Rg_div(x, y_lead);
    1627       14893 :     if (pr == ONLY_DIVIDES) return z;
    1628       13262 :     if (pr) *pr = pol_0(varn(x));
    1629       13262 :     return z;
    1630             :   }
    1631    11592290 :   dx = degpol(x);
    1632    11592290 :   if (dx < dy)
    1633             :   {
    1634      531490 :     if (pr == ONLY_REM) return RgX_copy(x);
    1635      339309 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1636      339288 :     z = pol_0(varn(x));
    1637      339288 :     if (pr) *pr = RgX_copy(x);
    1638      339288 :     return z;
    1639             :   }
    1640             : 
    1641             :   /* x,y in R[X], y non constant */
    1642    11060800 :   av = avma;
    1643    11060800 :   p = NULL;
    1644    11060800 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1645             :   {
    1646      112952 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1647      112952 :     if (!z) { avma = av; return NULL; }
    1648      112952 :     z = FpX_to_mod(z, p);
    1649      112952 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1650       60879 :       return gerepileupto(av, z);
    1651       52073 :     *pr = FpX_to_mod(*pr, p);
    1652       52073 :     gerepileall(av, 2, pr, &z);
    1653       52073 :     return z;
    1654             :   }
    1655    10947848 :   switch(typ(y_lead))
    1656             :   {
    1657             :     case t_REAL:
    1658           0 :       y_lead = ginv(y_lead);
    1659           0 :       f = gmul; mod = NULL;
    1660           0 :       break;
    1661             :     case t_INTMOD:
    1662        1371 :     case t_POLMOD: y_lead = ginv(y_lead);
    1663        1371 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1664        1371 :       break;
    1665    10946477 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1666    10946477 :       f = gdiv; mod = NULL;
    1667             :   }
    1668             : 
    1669    10947848 :   if (y_lead == NULL)
    1670     9180901 :     p2 = gel(x,dx+2);
    1671             :   else {
    1672             :     for(;;) {
    1673     1766947 :       p2 = f(gel(x,dx+2),y_lead);
    1674     1766947 :       p2 = simplify_shallow(p2);
    1675     1766947 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1676           0 :     }
    1677     1766947 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1678             :     {
    1679           0 :       if (pr == ONLY_DIVIDES) {
    1680           0 :         avma = av;
    1681           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1682             :       }
    1683           0 :       if (pr == ONLY_REM)
    1684             :       {
    1685           0 :         if (dx < 0)
    1686           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1687             :         else
    1688             :         {
    1689             :           GEN t;
    1690           0 :           avma = av;
    1691           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1692           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1693           0 :           return t;
    1694             :         }
    1695             :       }
    1696           0 :       if (pr) /* cf ONLY_REM above */
    1697             :       {
    1698           0 :         if (dx < 0)
    1699             :         {
    1700           0 :           p2 = gclone(p2);
    1701           0 :           avma = av;
    1702           0 :           z = pol_0(varn(x));
    1703           0 :           x = scalarpol(p2, varn(x));
    1704           0 :           gunclone(p2);
    1705             :         }
    1706             :         else
    1707             :         {
    1708             :           GEN t;
    1709           0 :           avma = av;
    1710           0 :           z = pol_0(varn(x));
    1711           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1712           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1713           0 :           x = t;
    1714             :         }
    1715           0 :         *pr = x;
    1716             :       }
    1717             :       else
    1718             :       {
    1719           0 :         avma = av;
    1720           0 :         z = pol_0(varn(x));
    1721             :       }
    1722           0 :       return z;
    1723             :     }
    1724             :   }
    1725             :   /* dx >= dy */
    1726    10947848 :   avy = avma;
    1727    10947848 :   dz = dx-dy;
    1728    10947848 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1729    10947848 :   x += 2;
    1730    10947848 :   z += 2;
    1731    10947848 :   y += 2;
    1732    10947848 :   gel(z,dz) = gcopy(p2);
    1733             : 
    1734    26562400 :   for (i=dx-1; i>=dy; i--)
    1735             :   {
    1736    15614552 :     av1=avma; p1=gel(x,i);
    1737    15614552 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1738    15614552 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1739             : 
    1740    15614552 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1741             :     else
    1742     6206128 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1743    15614552 :     gel(z,i-dy) = p1;
    1744             :   }
    1745    10947848 :   if (!pr) return gerepileupto(av,z-2);
    1746             : 
    1747     4534648 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1748     4872125 :   for (sx=0; ; i--)
    1749             :   {
    1750     4872125 :     p1 = gel(x,i);
    1751             :     /* we always enter this loop at least once */
    1752     4872125 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1753     4872125 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1754     4872125 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1755     3401039 :     if (!isexactzero(p1)) break;
    1756     3391245 :     if (!i) break;
    1757      337477 :     avma=av1;
    1758      337477 :   }
    1759     4534648 :   if (pr == ONLY_DIVIDES)
    1760             :   {
    1761        1302 :     if (sx) { avma=av; return NULL; }
    1762        1295 :     avma = (pari_sp)rem;
    1763        1295 :     return gerepileupto(av,z-2);
    1764             :   }
    1765     4533346 :   lr=i+3; rem -= lr;
    1766     4533346 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1767     4471029 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1768     4533346 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1769     4533346 :   rem[1] = z[-1];
    1770     4533346 :   rem += 2;
    1771     4533346 :   gel(rem,i) = p1;
    1772     5549028 :   for (i--; i>=0; i--)
    1773             :   {
    1774     1015682 :     av1=avma; p1 = gel(x,i);
    1775     1015682 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1776     1015682 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1777     1015682 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1778             :   }
    1779     4533346 :   rem -= 2;
    1780     4533346 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1781     4533346 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1782     3986580 :   z -= 2;
    1783             :   {
    1784     3986580 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1785     3986580 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1786             :   }
    1787             : }
    1788             : 
    1789             : GEN
    1790    10868236 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1791             : {
    1792    10868236 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1793    10868236 :   return RgX_divrem_i(x, y, pr);
    1794             : }
    1795             : 
    1796             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1797             : GEN
    1798       55618 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1799             : {
    1800             :   long vx, dx, dy, dz, i, j, sx, lr;
    1801             :   pari_sp av0, av, tetpil;
    1802             :   GEN z,p1,rem,lead;
    1803             : 
    1804       55618 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1805       55618 :   vx = varn(x);
    1806       55618 :   dx = degpol(x);
    1807       55618 :   dy = degpol(y);
    1808       55618 :   if (dx < dy)
    1809             :   {
    1810       18727 :     if (pr)
    1811             :     {
    1812       18727 :       av0 = avma; x = RgXQX_red(x, T);
    1813       18727 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1814       18727 :       if (pr == ONLY_REM) return x;
    1815           0 :       *pr = x;
    1816             :     }
    1817           0 :     return pol_0(vx);
    1818             :   }
    1819       36891 :   lead = leading_coeff(y);
    1820       36891 :   if (!dy) /* y is constant */
    1821             :   {
    1822         546 :     if (pr && pr != ONLY_DIVIDES)
    1823             :     {
    1824           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1825           0 :       *pr = pol_0(vx);
    1826             :     }
    1827         546 :     if (gequal1(lead)) return RgX_copy(x);
    1828           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1829           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1830             :   }
    1831       36345 :   av0 = avma; dz = dx-dy;
    1832       36345 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1833       36345 :   avma = av0;
    1834       36345 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1835       36345 :   x += 2; y += 2; z += 2;
    1836             : 
    1837       36345 :   p1 = gel(x,dx); av = avma;
    1838       36345 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1839      223829 :   for (i=dx-1; i>=dy; i--)
    1840             :   {
    1841      187484 :     av=avma; p1=gel(x,i);
    1842      187484 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1843      187484 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1844      187484 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1845             :   }
    1846       36345 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1847             : 
    1848       35099 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1849       59353 :   for (sx=0; ; i--)
    1850             :   {
    1851       59353 :     p1 = gel(x,i);
    1852       59353 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1853       59353 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1854       28322 :     if (!i) break;
    1855       24254 :     avma=av;
    1856       24254 :   }
    1857       35099 :   if (pr == ONLY_DIVIDES)
    1858             :   {
    1859        1688 :     if (lead) gunclone(lead);
    1860        1688 :     if (sx) { avma=av0; return NULL; }
    1861        1618 :     avma = (pari_sp)rem; return z-2;
    1862             :   }
    1863       33411 :   lr=i+3; rem -= lr;
    1864       33411 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1865       33411 :   rem[1] = z[-1];
    1866       33411 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1867       33411 :   rem += 2; gel(rem,i) = p1;
    1868       89166 :   for (i--; i>=0; i--)
    1869             :   {
    1870       55755 :     av=avma; p1 = gel(x,i);
    1871      212873 :     for (j=0; j<=i && j<=dz; j++)
    1872      157118 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1873       55755 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1874             :   }
    1875       33411 :   rem -= 2;
    1876       33411 :   if (lead) gunclone(lead);
    1877       33411 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1878       33411 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1879          77 :   *pr = rem; return z-2;
    1880             : }
    1881             : 
    1882             : /*******************************************************************/
    1883             : /*                                                                 */
    1884             : /*                        PSEUDO-DIVISION                          */
    1885             : /*                                                                 */
    1886             : /*******************************************************************/
    1887             : INLINE GEN
    1888      442924 : rem(GEN c, GEN T)
    1889             : {
    1890      442924 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1891      442924 :   return c;
    1892             : }
    1893             : 
    1894             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1895             : int
    1896        5912 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1897             : {
    1898             :   long dx, dy, dz, i, p, T_ismonic;
    1899        5912 :   pari_sp av = avma, av2;
    1900             :   GEN y_lead;
    1901             : 
    1902        5912 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1903        5912 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1904        5912 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1905             :   /* if monic, no point in using pseudo-division */
    1906        5912 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1907        3567 :   T_ismonic = gequal1(leading_coeff(T));
    1908        3567 :   dx = degpol(x);
    1909        3567 :   if (dx < dy) return !signe(x);
    1910        3567 :   (void)new_chunk(2);
    1911        3567 :   x = RgX_recip_shallow(x)+2;
    1912        3567 :   y = RgX_recip_shallow(y)+2;
    1913             :   /* pay attention to sparse divisors */
    1914        7316 :   for (i = 1; i <= dy; i++)
    1915        3749 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1916        3567 :   dz = dx-dy; p = dz+1;
    1917        3567 :   av2 = avma;
    1918             :   for (;;)
    1919             :   {
    1920       32213 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1921       32213 :     x0 = gneg(x0); p--;
    1922       32213 :     m = gcdii(cx, y0);
    1923       32213 :     if (!equali1(m))
    1924             :     {
    1925       31205 :       x0 = gdiv(x0, m);
    1926       31205 :       y0 = diviiexact(y0, m);
    1927       31205 :       if (equali1(y0)) y0 = NULL;
    1928             :     }
    1929       65616 :     for (i=1; i<=dy; i++)
    1930             :     {
    1931       33403 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1932       33403 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1933       33403 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1934       33403 :       gel(x,i) = c;
    1935             :     }
    1936      382876 :     for (   ; i<=dx; i++)
    1937             :     {
    1938      350663 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1939      350663 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1940      350663 :       gel(x,i) = c;
    1941             :     }
    1942       35871 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1943       32213 :     if (dx < dy) break;
    1944       28646 :     if (gc_needed(av2,1))
    1945             :     {
    1946           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1947           0 :       gerepilecoeffs(av2,x,dx+1);
    1948             :     }
    1949       28646 :   }
    1950        3567 :   avma = av; return (dx < 0);
    1951             : }
    1952             : 
    1953             : /* T either NULL or a t_POL. */
    1954             : GEN
    1955       24898 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1956             : {
    1957       24898 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1958       24898 :   pari_sp av = avma, av2;
    1959             :   GEN y_lead;
    1960             : 
    1961       24898 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1962       24898 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1963             :   /* if monic, no point in using pseudo-division */
    1964       24898 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1965       21454 :   dx = degpol(x);
    1966       21454 :   if (dx < dy) return RgX_copy(x);
    1967       21454 :   (void)new_chunk(2);
    1968       21454 :   x = RgX_recip_shallow(x)+2;
    1969       21454 :   y = RgX_recip_shallow(y)+2;
    1970             :   /* pay attention to sparse divisors */
    1971       66453 :   for (i = 1; i <= dy; i++)
    1972       44999 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1973       21454 :   dz = dx-dy; p = dz+1;
    1974       21454 :   av2 = avma;
    1975             :   for (;;)
    1976             :   {
    1977       81191 :     gel(x,0) = gneg(gel(x,0)); p--;
    1978      257719 :     for (i=1; i<=dy; i++)
    1979             :     {
    1980      176528 :       GEN c = gmul(y_lead, gel(x,i));
    1981      176528 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1982      176528 :       gel(x,i) = rem(c, T);
    1983             :     }
    1984      285516 :     for (   ; i<=dx; i++)
    1985             :     {
    1986      204325 :       GEN c = gmul(y_lead, gel(x,i));
    1987      204325 :       gel(x,i) = rem(c, T);
    1988             :     }
    1989       88653 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1990       81191 :     if (dx < dy) break;
    1991       59737 :     if (gc_needed(av2,1))
    1992             :     {
    1993           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1994           0 :       gerepilecoeffs(av2,x,dx+1);
    1995             :     }
    1996       59737 :   }
    1997       21454 :   if (dx < 0) return pol_0(vx);
    1998       19417 :   lx = dx+3; x -= 2;
    1999       19417 :   x[0] = evaltyp(t_POL) | evallg(lx);
    2000       19417 :   x[1] = evalsigne(1) | evalvarn(vx);
    2001       19417 :   x = RgX_recip_shallow(x);
    2002       19417 :   if (p)
    2003             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    2004        1176 :     GEN t = y_lead;
    2005        1176 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    2006           0 :       t = RgXQ_powu(t, p, T);
    2007             :     else
    2008        1176 :       t = gpowgs(t, p);
    2009        4060 :     for (i=2; i<lx; i++)
    2010             :     {
    2011        2884 :       GEN c = gmul(gel(x,i), t);
    2012        2884 :       gel(x,i) = rem(c,T);
    2013             :     }
    2014        1176 :     if (!T) return gerepileupto(av, x);
    2015             :   }
    2016       18241 :   return gerepilecopy(av, x);
    2017             : }
    2018             : 
    2019             : GEN
    2020       24898 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2021             : 
    2022             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2023             : GEN
    2024        5193 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2025             : {
    2026        5193 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2027        5193 :   pari_sp av = avma, av2;
    2028             :   GEN z, r, ypow, y_lead;
    2029             : 
    2030        5193 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2031        5193 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2032        5193 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2033        4668 :   dx = degpol(x);
    2034        4668 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2035        4668 :   if (dx == dy)
    2036             :   {
    2037          28 :     GEN x_lead = gel(x,lg(x)-1);
    2038          28 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2039          28 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2040          28 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2041          28 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2042             :   }
    2043        4640 :   (void)new_chunk(2);
    2044        4640 :   x = RgX_recip_shallow(x)+2;
    2045        4640 :   y = RgX_recip_shallow(y)+2;
    2046             :   /* pay attention to sparse divisors */
    2047       22215 :   for (i = 1; i <= dy; i++)
    2048       17575 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2049        4640 :   dz = dx-dy; p = dz+1;
    2050        4640 :   lz = dz+3;
    2051        4640 :   z = cgetg(lz, t_POL);
    2052        4640 :   z[1] = evalsigne(1) | evalvarn(vx);
    2053        4640 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2054        4640 :   ypow = new_chunk(dz+1);
    2055        4640 :   gel(ypow,0) = gen_1;
    2056        4640 :   gel(ypow,1) = y_lead;
    2057        7916 :   for (i=2; i<=dz; i++)
    2058             :   {
    2059        3276 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2060        3276 :     gel(ypow,i) = rem(c,T);
    2061             :   }
    2062        4640 :   av2 = avma;
    2063        4640 :   for (iz=2;;)
    2064             :   {
    2065        9852 :     p--;
    2066        9852 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2067       41983 :     for (i=1; i<=dy; i++)
    2068             :     {
    2069       32131 :       GEN c = gmul(y_lead, gel(x,i));
    2070       32131 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2071       32131 :       gel(x,i) = rem(c, T);
    2072             :     }
    2073       23780 :     for (   ; i<=dx; i++)
    2074             :     {
    2075       13928 :       GEN c = gmul(y_lead, gel(x,i));
    2076       13928 :       gel(x,i) = rem(c,T);
    2077             :     }
    2078        9852 :     x++; dx--;
    2079        9852 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2080        9852 :     if (dx < dy) break;
    2081        5212 :     if (gc_needed(av2,1))
    2082             :     {
    2083           0 :       GEN X = x-2;
    2084           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2085           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2086           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2087             :     }
    2088        5212 :   }
    2089        4640 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2090        4640 :   if (dx < 0)
    2091          98 :     x = pol_0(vx);
    2092             :   else
    2093             :   {
    2094        4542 :     lx = dx+3; x -= 2;
    2095        4542 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2096        4542 :     x[1] = evalsigne(1) | evalvarn(vx);
    2097        4542 :     x = RgX_recip_shallow(x);
    2098             :   }
    2099        4640 :   z = RgX_recip_shallow(z);
    2100        4640 :   r = x;
    2101        4640 :   if (p)
    2102             :   {
    2103        1674 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2104        1674 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2105             :   }
    2106        4640 :   gerepileall(av, 2, &z, &r);
    2107        4640 :   *ptr = r; return z;
    2108             : }
    2109             : GEN
    2110        5039 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2111        5039 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2112             : 
    2113             : GEN
    2114       12789 : RgXQX_mul(GEN x, GEN y, GEN T)
    2115             : {
    2116       12789 :   return RgXQX_red(RgX_mul(x,y), T);
    2117             : }
    2118             : GEN
    2119    69246797 : RgX_Rg_mul(GEN y, GEN x) {
    2120             :   long i, ly;
    2121    69246797 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2122    69246797 :   if (ly == 2) return z;
    2123    69101780 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2124    69101773 :   return normalizepol_lg(z,ly);
    2125             : }
    2126             : GEN
    2127       13881 : RgX_muls(GEN y, long x) {
    2128             :   long i, ly;
    2129       13881 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2130       13881 :   if (ly == 2) return z;
    2131       13846 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2132       13846 :   return normalizepol_lg(z,ly);
    2133             : }
    2134             : GEN
    2135        4417 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2136             : {
    2137        4417 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2138             : }
    2139             : GEN
    2140          56 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2141             : {
    2142          56 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2143             : }
    2144             : 
    2145             : GEN
    2146        1792 : RgXQX_sqr(GEN x, GEN T)
    2147             : {
    2148        1792 :   return RgXQX_red(RgX_sqr(x), T);
    2149             : }
    2150             : 
    2151             : GEN
    2152         448 : RgXQX_powers(GEN P, long n, GEN T)
    2153             : {
    2154         448 :   GEN v = cgetg(n+2, t_VEC);
    2155             :   long i;
    2156         448 :   gel(v, 1) = pol_1(varn(T));
    2157         448 :   if (n==0) return v;
    2158         448 :   gel(v, 2) = gcopy(P);
    2159         448 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2160         448 :   return v;
    2161             : }
    2162             : 
    2163             : static GEN
    2164       65219 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2165             : static GEN
    2166           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2167             : static GEN
    2168      248741 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2169             : static GEN
    2170      101911 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2171             : static GEN
    2172      110845 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2173             : static GEN
    2174      105819 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2175             : static GEN
    2176         105 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2177             : static GEN
    2178       70735 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2179             : 
    2180             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2181             :               _mul, _sqr, _one, _zero };
    2182             : 
    2183             : GEN
    2184           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2185             : {
    2186           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2187             : }
    2188             : 
    2189             : GEN
    2190       45052 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2191             : {
    2192       45052 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2193       45052 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2194             : }
    2195             : 
    2196             : /* mod X^n */
    2197             : struct modXn {
    2198             :   long v; /* varn(X) */
    2199             :   long n;
    2200             : } ;
    2201             : static GEN
    2202        1806 : _sqrXn(void *data, GEN x) {
    2203        1806 :   struct modXn *S = (struct modXn*)data;
    2204        1806 :   return RgXn_sqr(x, S->n);
    2205             : }
    2206             : static GEN
    2207        1190 : _mulXn(void *data, GEN x, GEN y) {
    2208        1190 :   struct modXn *S = (struct modXn*)data;
    2209        1190 :   return RgXn_mul(x,y, S->n);
    2210             : }
    2211             : static GEN
    2212        1407 : _oneXn(void *data) {
    2213        1407 :   struct modXn *S = (struct modXn*)data;
    2214        1407 :   return pol_1(S->v);
    2215             : }
    2216             : static GEN
    2217           0 : _zeroXn(void *data) {
    2218           0 :   struct modXn *S = (struct modXn*)data;
    2219           0 :   return pol_0(S->v);
    2220             : }
    2221             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2222             :                                           _oneXn, _zeroXn };
    2223             : 
    2224             : GEN
    2225         336 : RgXn_powers(GEN x, long m, long n)
    2226             : {
    2227         336 :   long d = degpol(x);
    2228         336 :   int use_sqr = (d<<1) >= n;
    2229             :   struct modXn S;
    2230         336 :   S.v = varn(x); S.n = n;
    2231         336 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2232             : }
    2233             : 
    2234             : GEN
    2235        1526 : RgXn_powu_i(GEN x, ulong m, long n)
    2236             : {
    2237             :   struct modXn S;
    2238        1526 :   S.v = varn(x); S.n = n;
    2239        1526 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2240             : }
    2241             : GEN
    2242           0 : RgXn_powu(GEN x, ulong m, long n)
    2243             : {
    2244             :   struct modXn S;
    2245           0 :   S.v = varn(x); S.n = n;
    2246           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2247             : }
    2248             : 
    2249             : GEN
    2250         672 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2251             : {
    2252             :   struct modXn S;
    2253         672 :   S.v = varn(gel(x,2)); S.n = n;
    2254         672 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2255             : }
    2256             : 
    2257             : GEN
    2258           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2259             : {
    2260           0 :   int use_sqr = 2*degpol(x) >= n;
    2261             :   struct modXn S;
    2262           0 :   S.v = varn(x); S.n = n;
    2263           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2264             : }
    2265             : 
    2266             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2267             : GEN
    2268        1799 : RgXn_eval(GEN Q, GEN x, long n)
    2269             : {
    2270        1799 :   long d = degpol(x);
    2271             :   int use_sqr;
    2272             :   struct modXn S;
    2273        1799 :   if (d == 1 && isrationalzero(gel(x,2)))
    2274             :   {
    2275        1792 :     GEN y = RgX_unscale(Q, gel(x,3));
    2276        1792 :     setvarn(y, varn(x)); return y;
    2277             :   }
    2278           7 :   S.v = varn(x);
    2279           7 :   S.n = n;
    2280           7 :   use_sqr = (d<<1) >= n;
    2281           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2282             : }
    2283             : 
    2284             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2285             : static GEN
    2286       33816 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2287             : {
    2288       33816 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2289       33816 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2290             : }
    2291             : 
    2292             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2293             : static GEN
    2294           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2295             : {
    2296           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2297           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2298             : }
    2299             : 
    2300             : GEN
    2301      112602 : RgXn_inv(GEN f, long e)
    2302             : {
    2303      112602 :   pari_sp av = avma, av2;
    2304             :   ulong mask;
    2305             :   GEN W, a;
    2306      112602 :   long v = varn(f), n = 1;
    2307             : 
    2308      112602 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2309      112602 :   a = ginv(gel(f,2));
    2310      112602 :   if (e == 1) return scalarpol(a, v);
    2311      112602 :   else if (e == 2)
    2312             :   {
    2313             :     GEN b;
    2314       98994 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2315       80185 :     b = gneg(b);
    2316       80185 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2317       80185 :     W = deg1pol_shallow(b, a, v);
    2318       80185 :     return gerepilecopy(av, W);
    2319             :   }
    2320       13608 :   W = scalarpol_shallow(ginv(gel(f,2)),v);
    2321       13608 :   mask = quadratic_prec_mask(e);
    2322       13608 :   av2 = avma;
    2323       61032 :   for (;mask>1;)
    2324             :   {
    2325             :     GEN u, fr;
    2326       33816 :     long n2 = n;
    2327       33816 :     n<<=1; if (mask & 1) n--;
    2328       33816 :     mask >>= 1;
    2329       33816 :     fr = RgXn_red_shallow(f, n);
    2330       33816 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2331       33816 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2332       33816 :     if (gc_needed(av2,2))
    2333             :     {
    2334           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2335           0 :       W = gerepileupto(av2, W);
    2336             :     }
    2337             :   }
    2338       13608 :   return gerepileupto(av, W);
    2339             : }
    2340             : 
    2341             : GEN
    2342       12586 : RgXn_exp(GEN h, long e)
    2343             : {
    2344       12586 :   pari_sp av = avma, av2;
    2345       12586 :   long v = varn(h), n=1;
    2346       12586 :   GEN f = pol_1(v), g = pol_1(v);
    2347       12586 :   ulong mask = quadratic_prec_mask(e);
    2348       12586 :   av2 = avma;
    2349       12586 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2350           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2351       38822 :   for (;mask>1;)
    2352             :   {
    2353             :     GEN q, w;
    2354       13650 :     long n2 = n;
    2355       13650 :     n<<=1; if (mask & 1) n--;
    2356       13650 :     mask >>= 1;
    2357       13650 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2358       13650 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2359       13650 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2360       13650 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2361       13650 :     if (gc_needed(av2,2))
    2362             :     {
    2363           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2364           0 :       gerepileall(av2, 2, &f, &g);
    2365             :     }
    2366             :   }
    2367       12586 :   return gerepileupto(av, f);
    2368             : }
    2369             : 
    2370             : GEN
    2371          84 : RgXn_reverse(GEN f, long e)
    2372             : {
    2373          84 :   pari_sp av = avma, av2;
    2374             :   ulong mask;
    2375             :   GEN fi, a, df, W, an;
    2376          84 :   long v = varn(f), n=1;
    2377          84 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2378           0 :     pari_err_INV("serreverse",f);
    2379          84 :   fi = ginv(gel(f,3));
    2380          84 :   a = deg1pol_shallow(fi,gen_0,v);
    2381          84 :   if (e <= 2) return gerepilecopy(av, a);
    2382          84 :   W = scalarpol(fi,v);
    2383          84 :   df = RgX_deriv(f);
    2384          84 :   mask = quadratic_prec_mask(e);
    2385          84 :   av2 = avma;
    2386         504 :   for (;mask>1;)
    2387             :   {
    2388             :     GEN u, fa, fr;
    2389         336 :     long n2 = n, rt;
    2390         336 :     n<<=1; if (mask & 1) n--;
    2391         336 :     mask >>= 1;
    2392         336 :     fr = RgXn_red_shallow(f, n);
    2393         336 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2394         336 :     an = RgXn_powers(a, rt, n);
    2395         336 :     if (n>1)
    2396             :     {
    2397         336 :       long n4 = (n2+1)>>1;
    2398         336 :       GEN dfr = RgXn_red_shallow(df, n2);
    2399         336 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2400         336 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2401         336 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2402             :     }
    2403         336 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2404         336 :     fa = RgX_shift(fa, -n2);
    2405         336 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2406         336 :     if (gc_needed(av2,2))
    2407             :     {
    2408           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2409           0 :       gerepileall(av2, 2, &a, &W);
    2410             :     }
    2411             :   }
    2412          84 :   return gerepileupto(av, a);
    2413             : }
    2414             : 
    2415             : GEN
    2416           0 : RgXn_sqrt(GEN h, long e)
    2417             : {
    2418           0 :   pari_sp av = avma, av2;
    2419           0 :   long v = varn(h), n = 1;
    2420           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2421           0 :   ulong mask = quadratic_prec_mask(e);
    2422           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2423           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2424           0 :   av2 = avma;
    2425             :   while(1)
    2426             :   {
    2427           0 :     long n2 = n, m;
    2428             :     GEN g;
    2429           0 :     n<<=1; if (mask & 1) n--;
    2430           0 :     mask >>= 1;
    2431           0 :     m = n-n2;
    2432           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2433           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2434           0 :     if (mask==1) return gerepileupto(av, f);
    2435           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2436           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2437           0 :     if (gc_needed(av2,2))
    2438             :     {
    2439           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2440           0 :       gerepileall(av2, 2, &f, &df);
    2441             :     }
    2442           0 :   }
    2443             : }
    2444             : 
    2445             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2446             : GEN
    2447      205765 : RgXQ_powu(GEN x, ulong n, GEN T)
    2448             : {
    2449             :   pari_sp av;
    2450             :   GEN y;
    2451             : 
    2452      205765 :   if (!n) return pol_1(varn(x));
    2453      204225 :   if (n == 1) return RgX_copy(x);
    2454      140135 :   av = avma;
    2455      140135 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2456      140141 :   return gerepileupto(av, y);
    2457             : }
    2458             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2459             : GEN
    2460       17877 : RgXQ_pow(GEN x, GEN n, GEN T)
    2461             : {
    2462             :   pari_sp av;
    2463       17877 :   long s = signe(n);
    2464             :   GEN y;
    2465             : 
    2466       17877 :   if (!s) return pol_1(varn(x));
    2467       17877 :   if (is_pm1(n) == 1)
    2468           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2469       17877 :   av = avma;
    2470       17877 :   if (s < 0) x = RgXQ_inv(x, T);
    2471       17877 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2472       17877 :   return gerepileupto(av, y);
    2473             : }
    2474             : 
    2475             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2476             : GEN
    2477        2338 : RgXQ_powers(GEN x, long l, GEN T)
    2478             : {
    2479        2338 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2480        2338 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2481             : }
    2482             : 
    2483             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2484             : GEN
    2485        1904 : QXQ_powers(GEN a, long n, GEN T)
    2486             : {
    2487        1904 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2488             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2489        1904 :   if (den)
    2490             :   { /* restore denominators */
    2491        1274 :     GEN d = den;
    2492             :     long i;
    2493        1274 :     gel(v,2) = a;
    2494        3857 :     for (i=3; i<=n+1; i++) {
    2495        2583 :       d = mulii(d,den);
    2496        2583 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2497             :     }
    2498             :   }
    2499        1904 :   return v;
    2500             : }
    2501             : 
    2502             : static GEN
    2503        1260 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2504             : {
    2505        1260 :   long l, i, m = 0;
    2506             :   GEN dz, z;
    2507        1260 :   GEN V = cgetg_copy(v, &l);
    2508        4032 :   for (i = imin; i < l; i++)
    2509             :   {
    2510        2772 :     GEN c = gel(v, i);
    2511        2772 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2512             :   }
    2513        1260 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2514        1260 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2515        4032 :   for (i = imin; i < l; i++)
    2516             :   {
    2517        2772 :     GEN c = gel(v,i);
    2518        2772 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2519        2772 :     gel(V,i) = c;
    2520             :   }
    2521        1260 :   return V;
    2522             : }
    2523             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2524             : GEN
    2525        1197 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2526        1197 : { return do_QXQ_eval(v, 1, a, T); }
    2527             : GEN
    2528          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2529          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2530             : 
    2531             : GEN
    2532         287 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2533             : {
    2534         287 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2535             : }
    2536             : 
    2537             : GEN
    2538          56 : RgXQ_minpoly_naive(GEN y, GEN P)
    2539             : {
    2540          56 :   pari_sp ltop=avma;
    2541          56 :   long n=lgpol(P);
    2542          56 :   GEN M=ker(RgXQ_matrix_pow(y,n,n,P));
    2543          56 :   M=content(RgM_to_RgXV(M,varn(P)));
    2544          56 :   return gerepileupto(ltop,M);
    2545             : }
    2546             : 
    2547             : GEN
    2548       34794 : RgXQ_norm(GEN x, GEN T)
    2549             : {
    2550             :   pari_sp av;
    2551       34794 :   long dx = degpol(x);
    2552             :   GEN L, y;
    2553             : 
    2554       34794 :   av = avma; y = resultant(T, x);
    2555       34794 :   L = leading_coeff(T);
    2556       34794 :   if (gequal1(L) || !signe(x)) return y;
    2557           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2558             : }
    2559             : 
    2560             : GEN
    2561      103928 : RgX_blocks(GEN P, long n, long m)
    2562             : {
    2563      103928 :   GEN z = cgetg(m+1,t_VEC);
    2564      103928 :   long i,j, k=2, l = lg(P);
    2565      504956 :   for(i=1; i<=m; i++)
    2566             :   {
    2567      401028 :     GEN zi = cgetg(n+2,t_POL);
    2568      401028 :     zi[1] = P[1];
    2569      401028 :     gel(z,i) = zi;
    2570     2373437 :     for(j=2; j<n+2; j++)
    2571     1972409 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2572      401028 :     zi = RgX_renormalize_lg(zi, n+2);
    2573             :   }
    2574      103928 :   return z;
    2575             : }
    2576             : 
    2577             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2578             : void
    2579       27372 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2580             : {
    2581       27372 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2582             :   GEN p0, p1;
    2583             : 
    2584       54745 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2585             : 
    2586       27372 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2587       27372 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2588       27373 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2589      750162 :   for (i=0; i<n1; i++)
    2590             :   {
    2591      722789 :     p0[2+i] = p[2+(i<<1)];
    2592      722789 :     p1[2+i] = p[3+(i<<1)];
    2593             :   }
    2594       27373 :   if (n1 != n0)
    2595       19793 :     p0[2+i] = p[2+(i<<1)];
    2596       27373 :   *pe = normalizepol(p0);
    2597       27373 :   *po = normalizepol(p1);
    2598             : }
    2599             : 
    2600             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2601             : GEN
    2602       40614 : RgX_splitting(GEN p, long k)
    2603             : {
    2604       40614 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2605             :   GEN r;
    2606             : 
    2607       40614 :   m = n/k;
    2608       40614 :   r = cgetg(k+1,t_VEC);
    2609      223930 :   for(i=1; i<=k; i++)
    2610             :   {
    2611      183316 :     gel(r,i) = cgetg(m+3, t_POL);
    2612      183316 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2613             :   }
    2614      552426 :   for (j=1, i=0, l=2; i<=n; i++)
    2615             :   {
    2616      511812 :     gmael(r,j,l) = gel(p,2+i);
    2617      511812 :     if (j==k) { j=1; l++; } else j++;
    2618             :   }
    2619      223930 :   for(i=1; i<=k; i++)
    2620      183316 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2621       40614 :   return r;
    2622             : }
    2623             : 
    2624             : /*******************************************************************/
    2625             : /*                                                                 */
    2626             : /*                        Kronecker form                           */
    2627             : /*                                                                 */
    2628             : /*******************************************************************/
    2629             : 
    2630             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2631             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2632             :  * normalized coefficients */
    2633             : GEN
    2634      118588 : Kronecker_to_mod(GEN z, GEN T)
    2635             : {
    2636      118588 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2637      118588 :   GEN x, t = cgetg(N,t_POL);
    2638      118588 :   t[1] = T[1];
    2639      118588 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2640      118588 :   x[1] = z[1];
    2641      118588 :   T = RgX_copy(T);
    2642     1116829 :   for (i=2; i<lx+2; i++, z+= N-2)
    2643             :   {
    2644      998241 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2645      998241 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2646             :   }
    2647      118588 :   N = (l-2) % (N-2) + 2;
    2648      118588 :   for (j=2; j<N; j++) t[j] = z[j];
    2649      118588 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2650      118588 :   return normalizepol_lg(x, i+1);
    2651             : }
    2652             : 
    2653             : /*******************************************************************/
    2654             : /*                                                                 */
    2655             : /*                        Domain detection                         */
    2656             : /*                                                                 */
    2657             : /*******************************************************************/
    2658             : 
    2659             : static GEN
    2660      142605 : zero_FpX_mod(GEN p, long v)
    2661             : {
    2662      142605 :   GEN r = cgetg(3,t_POL);
    2663      142605 :   r[1] = evalvarn(v);
    2664      142605 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2665      142605 :   return r;
    2666             : }
    2667             : 
    2668             : static GEN
    2669      292662 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2670             : {
    2671      292662 :   pari_sp av = avma;
    2672             :   GEN r;
    2673      292662 :   if (lgefint(p) == 3)
    2674             :   {
    2675      287579 :     ulong pp = uel(p, 2);
    2676      287579 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2677             :                                   RgX_to_Flx(y, pp), pp));
    2678             :   }
    2679             :   else
    2680        5083 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2681      292662 :   if (signe(r)==0)
    2682       50807 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2683      241855 :   return gerepileupto(av, FpX_to_mod(r, p));
    2684             : }
    2685             : 
    2686             : static GEN
    2687           0 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2688             : {
    2689           0 :   GEN r = cgetg(3,t_POL);
    2690           0 :   r[1] = evalvarn(v);
    2691           0 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2692           0 :   return r;
    2693             : }
    2694             : 
    2695             : static GEN
    2696          84 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2697             : {
    2698          84 :   pari_sp av = avma;
    2699          84 :   GEN T = RgX_to_FpX(pol, p);
    2700          84 :   long dT = degpol(T);
    2701          84 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2702          84 :   GEN ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2703          84 :   GEN r = FpX_mul(kx, ky, p);
    2704          84 :   if (signe(r)==0)
    2705           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2706          84 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2707             : }
    2708             : 
    2709             : static GEN
    2710       93378 : RgX_mul_ZXQX(GEN x, GEN y, GEN T)
    2711             : {
    2712       93378 :   pari_sp av = avma;
    2713       93378 :   long dT = degpol(T);
    2714       93378 :   GEN r = ZXX_mul_Kronecker(liftpol_shallow(x), liftpol_shallow(y), dT);
    2715       93378 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2716             : }
    2717             : 
    2718             : static GEN
    2719       22022 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2720             : {
    2721       22022 :   pari_sp av = avma;
    2722       22022 :   long dT = degpol(T);
    2723       22022 :   GEN r = QX_mul(ZXX_to_Kronecker(liftpol_shallow(x), dT),
    2724             :                  ZXX_to_Kronecker(liftpol_shallow(y), dT));
    2725       22022 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2726             : }
    2727             : 
    2728             : static GEN
    2729         651 : RgX_sqr_FpX(GEN x, GEN p)
    2730             : {
    2731         651 :   pari_sp av = avma;
    2732             :   GEN r;
    2733         651 :   if (lgefint(p) == 3)
    2734             :   {
    2735         643 :     ulong pp = uel(p, 2);
    2736         643 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    2737             :   }
    2738             :   else
    2739           8 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    2740         651 :   if (signe(r)==0)
    2741          98 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2742         553 :   return gerepileupto(av, FpX_to_mod(r, p));
    2743             : }
    2744             : 
    2745             : static GEN
    2746         203 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    2747             : {
    2748         203 :   pari_sp av = avma;
    2749         203 :   GEN T = RgX_to_FpX(pol, p);
    2750         203 :   long dT = degpol(T);
    2751         203 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2752         203 :   GEN r = FpX_sqr(kx, p);
    2753         203 :   if (signe(r)==0)
    2754           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    2755         203 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2756             : }
    2757             : 
    2758             : static GEN
    2759        1422 : RgX_sqr_ZXQX(GEN x, GEN T)
    2760             : {
    2761        1422 :   pari_sp av = avma;
    2762        1422 :   long dT = degpol(T);
    2763        1422 :   GEN r = ZXX_sqr_Kronecker(liftpol_shallow(x), dT);
    2764        1422 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2765             : }
    2766             : 
    2767             : static GEN
    2768        1479 : RgX_sqr_QXQX(GEN x, GEN T)
    2769             : {
    2770        1479 :   pari_sp av = avma;
    2771        1479 :   long dT = degpol(T);
    2772        1479 :   GEN r = QX_sqr(ZXX_to_Kronecker(liftpol_shallow(x), dT));
    2773        1479 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2774             : }
    2775             : 
    2776             : static GEN
    2777      312368 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    2778             : {
    2779      312368 :   pari_sp av = avma;
    2780             :   GEN r;
    2781      312368 :   if (lgefint(p) == 3)
    2782             :   {
    2783      311801 :     ulong pp = uel(p, 2);
    2784      311801 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    2785             :                                   RgX_to_Flx(y, pp), pp));
    2786             :   }
    2787             :   else
    2788         567 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2789      312368 :   if (signe(r)==0)
    2790       91700 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    2791      220668 :   return gerepileupto(av, FpX_to_mod(r, p));
    2792             : }
    2793             : 
    2794             : static GEN
    2795       32601 : RgX_rem_QXQX(GEN x, GEN y, GEN pol)
    2796             : {
    2797       32601 :   pari_sp av = avma;
    2798             :   GEN r;
    2799       32601 :   r = RgXQX_rem(liftpol_shallow(x), liftpol_shallow(y), pol);
    2800       32601 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, pol));
    2801             : }
    2802             : static GEN
    2803          35 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2804             : {
    2805          35 :   pari_sp av = avma;
    2806             :   GEN r;
    2807          35 :   GEN T = RgX_to_FpX(pol, p);
    2808          35 :   if (lgefint(p) == 3)
    2809             :   {
    2810          35 :     ulong pp = uel(p, 2);
    2811          35 :     GEN Tp = ZX_to_Flx(T, pp);
    2812          35 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    2813             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    2814             :   }
    2815             :   else
    2816           0 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    2817          35 :   if (signe(r)==0)
    2818           0 :   { avma = av; return zero_FpXQX_mod(p, pol, varn(x)); }
    2819          35 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    2820             : }
    2821             : 
    2822             : #define code(t1,t2) ((t1 << 6) | t2)
    2823             : static GEN
    2824    43408316 : RgX_mul_fast(GEN x, GEN y)
    2825             : {
    2826             :   GEN p, pol;
    2827             :   long pa;
    2828    43408316 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2829    43408253 :   switch(t)
    2830             :   {
    2831    33424532 :     case t_INT:    return ZX_mul(x,y);
    2832      817179 :     case t_FRAC:   return QX_mul(x,y);
    2833       99520 :     case t_FFELT:  return FFX_mul(x, y, pol);
    2834      292662 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    2835             :     case code(t_POLMOD, t_INT):
    2836       93378 :                    return RgX_mul_ZXQX(x, y, pol);
    2837             :     case code(t_POLMOD, t_FRAC):
    2838       22022 :                    return RgX_mul_QXQX(x, y, pol);
    2839             :     case code(t_POLMOD, t_INTMOD):
    2840          84 :                    return RgX_mul_FpXQX(x, y, pol, p);
    2841     8658876 :     default:       return NULL;
    2842             :   }
    2843             : }
    2844             : static GEN
    2845     2833672 : RgX_sqr_fast(GEN x)
    2846             : {
    2847             :   GEN p, pol;
    2848             :   long pa;
    2849     2833672 :   long t = RgX_type(x,&p,&pol,&pa);
    2850     2833505 :   switch(t)
    2851             :   {
    2852     2776963 :     case t_INT:    return ZX_sqr(x);
    2853       41944 :     case t_FRAC:   return QX_sqr(x);
    2854        2023 :     case t_FFELT:  return FFX_sqr(x, pol);
    2855         651 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    2856             :     case code(t_POLMOD, t_INT):
    2857        1422 :                    return RgX_sqr_ZXQX(x, pol);
    2858             :     case code(t_POLMOD, t_FRAC):
    2859        1479 :                    return RgX_sqr_QXQX(x, pol);
    2860             :     case code(t_POLMOD, t_INTMOD):
    2861         203 :                    return RgX_sqr_FpXQX(x, pol, p);
    2862        8820 :     default:       return NULL;
    2863             :   }
    2864             : }
    2865             : 
    2866             : static GEN
    2867     5406812 : RgX_rem_fast(GEN x, GEN y)
    2868             : {
    2869             :   GEN p, pol;
    2870             :   long pa;
    2871     5406812 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2872     5406928 :   switch(t)
    2873             :   {
    2874     3645460 :     case t_INT:    return equali1(leading_term(y)) ? ZX_rem(x,y): NULL;
    2875      678483 :     case t_FRAC:   return RgX_is_ZX(y) && equali1(leading_term(y)) ? QX_ZX_rem(x,y): NULL;
    2876        1883 :     case t_FFELT:  return FFX_rem(x, y, pol);
    2877      312368 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    2878             :     case code(t_POLMOD, t_INT):
    2879             :     case code(t_POLMOD, t_FRAC):
    2880       32601 :                    return RgX_rem_QXQX(x, y, pol);
    2881             :     case code(t_POLMOD, t_INTMOD):
    2882          35 :                    return RgX_rem_FpXQX(x, y, pol, p);
    2883      736098 :     default:       return NULL;
    2884             :   }
    2885             : }
    2886             : 
    2887             : #undef code
    2888             : 
    2889             : GEN
    2890    39695793 : RgX_mul(GEN x, GEN y)
    2891             : {
    2892    39695793 :   GEN z = RgX_mul_fast(x,y);
    2893    39695707 :   if (!z) z = RgX_mul_i(x,y);
    2894    39695728 :   return z;
    2895             : }
    2896             : 
    2897             : GEN
    2898     2817936 : RgX_sqr(GEN x)
    2899             : {
    2900     2817936 :   GEN z = RgX_sqr_fast(x);
    2901     2817458 :   if (!z) z = RgX_sqr_i(x);
    2902     2817532 :   return z;
    2903             : }
    2904             : 
    2905             : GEN
    2906     5406861 : RgX_rem(GEN x, GEN y)
    2907             : {
    2908     5406861 :   GEN z = RgX_rem_fast(x, y);
    2909     5406771 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    2910     5406821 :   return z;
    2911             : }
    2912             : 
    2913             : GEN
    2914     3678730 : RgXn_mul(GEN f, GEN g, long n)
    2915             : {
    2916     3678730 :   pari_sp av = avma;
    2917     3678730 :   GEN h = RgX_mul_fast(f,g);
    2918     3678730 :   if (!h) return RgXn_mul2(f,g,n);
    2919     2013166 :   if (degpol(h) < n) return h;
    2920     1291598 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    2921             : }
    2922             : 
    2923             : GEN
    2924       15757 : RgXn_sqr(GEN f, long n)
    2925             : {
    2926       15757 :   pari_sp av = avma;
    2927       15757 :   GEN g = RgX_sqr_fast(f);
    2928       15757 :   if (!g) return RgXn_sqr2(f,n);
    2929       15519 :   if (degpol(g) < n) return g;
    2930         805 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    2931             : }
    2932             : 
    2933             : /* (f*g) \/ x^n */
    2934             : GEN
    2935       33816 : RgX_mulhigh_i(GEN f, GEN g, long n)
    2936             : {
    2937       33816 :   GEN h = RgX_mul_fast(f,g);
    2938       33816 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    2939             : }
    2940             : 
    2941             : /* (f*g) \/ x^n */
    2942             : GEN
    2943           0 : RgX_sqrhigh_i(GEN f, long n)
    2944             : {
    2945           0 :   GEN h = RgX_sqr_fast(f);
    2946           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    2947             : }

Generated by: LCOV version 1.11