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 to exceed 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 - headers - pariinl.h (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25524-6af9191f57) Lines: 1342 1493 89.9 %
Date: 2020-07-03 06:00:21 Functions: 582 652 89.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2010  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             : /*********************************************************************/
      15             : /*                       MALLOC/FREE WRAPPERS                        */
      16             : /*********************************************************************/
      17             : #define BLOCK_SIGALRM_START          \
      18             : {                                    \
      19             :   int block=PARI_SIGINT_block;       \
      20             :   PARI_SIGINT_block = 2;             \
      21             :   MT_SIGINT_BLOCK(block);
      22             : 
      23             : #define BLOCK_SIGINT_START           \
      24             : {                                    \
      25             :   int block=PARI_SIGINT_block;       \
      26             :   PARI_SIGINT_block = 1;             \
      27             :   MT_SIGINT_BLOCK(block);
      28             : 
      29             : #define BLOCK_SIGINT_END             \
      30             :   PARI_SIGINT_block = block;         \
      31             :   MT_SIGINT_UNBLOCK(block);          \
      32             :   if (!block && PARI_SIGINT_pending) \
      33             :   {                                  \
      34             :     int sig = PARI_SIGINT_pending;   \
      35             :     PARI_SIGINT_pending = 0;         \
      36             :     raise(sig);                      \
      37             :   }                                  \
      38             : }
      39             : 
      40             : /*******************************************************************/
      41             : /*                                                                 */
      42             : /*                          CONSTRUCTORS                           */
      43             : /*                                                                 */
      44             : /*******************************************************************/
      45             : #define retmkfrac(x,y)\
      46             :   do { GEN _v = cgetg(3, t_FRAC);\
      47             :        gel(_v,1) = (x);\
      48             :        gel(_v,2) = (y); return _v; } while(0)
      49             : #define retmkrfrac(x,y)\
      50             :   do { GEN _v = cgetg(3, t_RFRAC);\
      51             :        gel(_v,1) = (x);\
      52             :        gel(_v,2) = (y); return _v; } while(0)
      53             : #define retmkintmod(x,y)\
      54             :   do { GEN _v = cgetg(3, t_INTMOD);\
      55             :        gel(_v,1) = (y);\
      56             :        gel(_v,2) = (x); return _v; } while(0)
      57             : #define retmkcomplex(x,y)\
      58             :   do { GEN _v = cgetg(3, t_COMPLEX);\
      59             :        gel(_v,1) = (x);\
      60             :        gel(_v,2) = (y); return _v; } while(0)
      61             : #define retmkpolmod(x,y)\
      62             :   do { GEN _v = cgetg(3, t_POLMOD);\
      63             :        gel(_v,1) = (y);\
      64             :        gel(_v,2) = (x); return _v; } while(0)
      65             : #define retmkvec(x)\
      66             :   do { GEN _v = cgetg(2, t_VEC);\
      67             :        gel(_v,1) = (x); return _v; } while(0)
      68             : #define retmkvec2(x,y)\
      69             :   do { GEN _v = cgetg(3, t_VEC);\
      70             :        gel(_v,1) = (x);\
      71             :        gel(_v,2) = (y); return _v; } while(0)
      72             : #define retmkvec3(x,y,z)\
      73             :   do { GEN _v = cgetg(4, t_VEC);\
      74             :        gel(_v,1) = (x);\
      75             :        gel(_v,2) = (y);\
      76             :        gel(_v,3) = (z); return _v; } while(0)
      77             : #define retmkqfi(x,y,z)\
      78             :   do { GEN _v = cgetg(4, t_QFI);\
      79             :        gel(_v,1) = (x);\
      80             :        gel(_v,2) = (y);\
      81             :        gel(_v,3) = (z); return _v; } while(0)
      82             : #define retmkqfr(x,y,z,d)\
      83             :   do { GEN _v = cgetg(5, t_QFR);\
      84             :        gel(_v,1) = (x);\
      85             :        gel(_v,2) = (y);\
      86             :        gel(_v,3) = (z);\
      87             :        gel(_v,4) = (d); return _v; } while(0)
      88             : #define retmkquad(x,y,z)\
      89             :   do { GEN _v = cgetg(4, t_QUAD);\
      90             :        gel(_v,1) = (x);\
      91             :        gel(_v,2) = (y);\
      92             :        gel(_v,3) = (z); return _v; } while(0)
      93             : #define retmkvec4(x,y,z,t)\
      94             :   do { GEN _v = cgetg(5, t_VEC);\
      95             :        gel(_v,1) = (x);\
      96             :        gel(_v,2) = (y);\
      97             :        gel(_v,3) = (z);\
      98             :        gel(_v,4) = (t); return _v; } while(0)
      99             : #define retmkvec5(x,y,z,t,u)\
     100             :   do { GEN _v = cgetg(6, t_VEC);\
     101             :        gel(_v,1) = (x);\
     102             :        gel(_v,2) = (y);\
     103             :        gel(_v,3) = (z);\
     104             :        gel(_v,4) = (t);\
     105             :        gel(_v,5) = (u); return _v; } while(0)
     106             : #define retmkcol(x)\
     107             :   do { GEN _v = cgetg(2, t_COL);\
     108             :        gel(_v,1) = (x); return _v; } while(0)
     109             : #define retmkcol2(x,y)\
     110             :   do { GEN _v = cgetg(3, t_COL);\
     111             :        gel(_v,1) = (x);\
     112             :        gel(_v,2) = (y); return _v; } while(0)
     113             : #define retmkcol3(x,y,z)\
     114             :   do { GEN _v = cgetg(4, t_COL);\
     115             :        gel(_v,1) = (x);\
     116             :        gel(_v,2) = (y);\
     117             :        gel(_v,3) = (z); return _v; } while(0)
     118             : #define retmkcol4(x,y,z,t)\
     119             :   do { GEN _v = cgetg(5, t_COL);\
     120             :        gel(_v,1) = (x);\
     121             :        gel(_v,2) = (y);\
     122             :        gel(_v,3) = (z);\
     123             :        gel(_v,4) = (t); return _v; } while(0)
     124             : #define retmkcol5(x,y,z,t,u)\
     125             :   do { GEN _v = cgetg(6, t_COL);\
     126             :        gel(_v,1) = (x);\
     127             :        gel(_v,2) = (y);\
     128             :        gel(_v,3) = (z);\
     129             :        gel(_v,4) = (t);\
     130             :        gel(_v,5) = (u); return _v; } while(0)
     131             : #define retmkcol6(x,y,z,t,u,v)\
     132             :   do { GEN _v = cgetg(7, t_COL);\
     133             :        gel(_v,1) = (x);\
     134             :        gel(_v,2) = (y);\
     135             :        gel(_v,3) = (z);\
     136             :        gel(_v,4) = (t);\
     137             :        gel(_v,5) = (u);\
     138             :        gel(_v,6) = (v); return _v; } while(0)
     139             : #define retmkmat(x)\
     140             :   do { GEN _v = cgetg(2, t_MAT);\
     141             :        gel(_v,1) = (x); return _v; } while(0)
     142             : #define retmkmat2(x,y)\
     143             :   do { GEN _v = cgetg(3, t_MAT);\
     144             :        gel(_v,1) = (x);\
     145             :        gel(_v,2) = (y); return _v; } while(0)
     146             : #define retmkmat3(x,y,z)\
     147             :   do { GEN _v = cgetg(4, t_MAT);\
     148             :        gel(_v,1) = (x);\
     149             :        gel(_v,2) = (y);\
     150             :        gel(_v,3) = (z); return _v; } while(0)
     151             : #define retmkmat4(x,y,z,t)\
     152             :   do { GEN _v = cgetg(5, t_MAT);\
     153             :        gel(_v,1) = (x);\
     154             :        gel(_v,2) = (y);\
     155             :        gel(_v,3) = (z);\
     156             :        gel(_v,4) = (t); return _v; } while(0)
     157             : #define retmkmat5(x,y,z,t,u)\
     158             :   do { GEN _v = cgetg(6, t_MAT);\
     159             :        gel(_v,1) = (x);\
     160             :        gel(_v,2) = (y);\
     161             :        gel(_v,3) = (z);\
     162             :        gel(_v,4) = (t);\
     163             :        gel(_v,5) = (u); return _v; } while(0)
     164             : 
     165             : INLINE GEN
     166     3087257 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
     167             : INLINE GEN
     168        1666 : mkintmodu(ulong x, ulong y) {
     169        1666 :   GEN v = cgetg(3,t_INTMOD);
     170        1666 :   gel(v,1) = utoipos(y);
     171        1666 :   gel(v,2) = utoi(x); return v;
     172             : }
     173             : INLINE GEN
     174     3392049 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
     175             : INLINE GEN
     176    28576074 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
     177             : INLINE GEN
     178       32676 : mkfracss(long x, long y) { retmkfrac(stoi(x),utoipos(y)); }
     179             : /* q = n/d a t_FRAC or t_INT; recover (n,d) */
     180             : INLINE void
     181       14119 : Qtoss(GEN q, long *n, long *d)
     182             : {
     183       14119 :   if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
     184        2891 :   else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
     185       14119 : }
     186             : INLINE GEN
     187    68194235 : sstoQ(long n, long d)
     188             : {
     189             :   ulong r;
     190             :   long g, q;
     191    68194235 :   if (!n)
     192             :   {
     193      100464 :     if (!d) pari_err_INV("sstoQ",gen_0);
     194      100464 :     return gen_0;
     195             :   }
     196    68093771 :   if (d < 0) { d = -d; n = -n; }
     197    68093771 :   if (d == 1) return stoi(n);
     198    68081416 :   q = udivuu_rem(labs(n),d,&r);
     199    68081416 :   if (!r) return n > 0? utoipos(q): utoineg(q);
     200     3582793 :   g = ugcd(d,r); /* gcd(n,d) */
     201     3582793 :   if (g != 1) { n /= g; d /= g; }
     202     3582793 :   retmkfrac(stoi(n), utoi(d));
     203             : }
     204             : 
     205             : INLINE GEN
     206     2375845 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
     207             : INLINE GEN
     208        1554 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
     209        1554 :   gel(v,1) = x; gel(v,2) = y; return v; }
     210             : INLINE GEN
     211           7 : mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
     212           7 :   gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
     213             : INLINE GEN
     214    21205511 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
     215             : INLINE GEN
     216      457352 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
     217             : INLINE GEN
     218      283361 : cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
     219             : INLINE GEN
     220         105 : mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
     221         105 :   gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
     222             : /* vecsmall */
     223             : INLINE GEN
     224    69598650 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
     225             : INLINE GEN
     226   115812612 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
     227   115804696 :   v[1]=x; v[2]=y; return v; }
     228             : INLINE GEN
     229    57927737 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
     230    57921465 :   v[1]=x; v[2]=y; v[3]=z; return v; }
     231             : INLINE GEN
     232    11984640 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
     233    11966912 :   v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
     234             : INLINE GEN
     235      469860 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
     236      469857 :   v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
     237             : 
     238             : INLINE GEN
     239      158116 : mkqfi(GEN x, GEN y, GEN z) { retmkqfi(x,y,z); }
     240             : /* vec */
     241             : INLINE GEN
     242    32603387 : mkvec(GEN x) { retmkvec(x); }
     243             : INLINE GEN
     244   109383865 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
     245             : INLINE GEN
     246    67490469 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
     247             : INLINE GEN
     248     7356949 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
     249             : INLINE GEN
     250    12207020 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
     251             : INLINE GEN
     252         105 : mkvecs(long x) { retmkvec(stoi(x)); }
     253             : INLINE GEN
     254       26041 : mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
     255             : INLINE GEN
     256      492752 : mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
     257             : INLINE GEN
     258          21 : mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
     259             : INLINE GEN
     260       35756 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
     261             : INLINE GEN
     262       85056 : mkvec2copy(GEN x, GEN y) {
     263       85056 :   GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
     264             : /* col */
     265             : INLINE GEN
     266     3024983 : mkcol(GEN x) { retmkcol(x); }
     267             : INLINE GEN
     268    36410723 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
     269             : INLINE GEN
     270       42707 : mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
     271             : INLINE GEN
     272           0 : mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
     273             : INLINE GEN
     274         106 : mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
     275             : INLINE GEN
     276        9586 : mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
     277             : INLINE GEN
     278         273 : mkcols(long x) { retmkcol(stoi(x)); }
     279             : INLINE GEN
     280     2760800 : mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
     281             : INLINE GEN
     282           0 : mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
     283             : INLINE GEN
     284           0 : mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
     285             : INLINE GEN
     286       23865 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
     287             : /* mat */
     288             : INLINE GEN
     289     2514414 : mkmat(GEN x) { retmkmat(x); }
     290             : INLINE GEN
     291    32853581 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
     292             : INLINE GEN
     293        2329 : mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
     294             : INLINE GEN
     295           0 : mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
     296             : INLINE GEN
     297           0 : mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
     298             : INLINE GEN
     299       25764 : mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
     300             : INLINE GEN
     301           0 : mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
     302             : INLINE GEN
     303       89699 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
     304             : INLINE GEN
     305       17871 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
     306             : INLINE long
     307      119751 : inf_get_sign(GEN x) { return signe(gel(x,1)); }
     308             : INLINE GEN
     309       12432 : mkmat22s(long a, long b, long c, long d) {retmkmat2(mkcol2s(a,c),mkcol2s(b,d));}
     310             : INLINE GEN
     311     4623550 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
     312             : 
     313             : /* pol */
     314             : INLINE GEN
     315     1013594 : pol_x(long v) {
     316     1013594 :   GEN p = cgetg(4, t_POL);
     317     1013595 :   p[1] = evalsigne(1)|evalvarn(v);
     318     1013595 :   gel(p,2) = gen_0;
     319     1013595 :   gel(p,3) = gen_1; return p;
     320             : }
     321             : /* x^n, assume n >= 0 */
     322             : INLINE GEN
     323     1700420 : pol_xn(long n, long v) {
     324     1700420 :   long i, a = n+2;
     325     1700420 :   GEN p = cgetg(a+1, t_POL);
     326     1700420 :   p[1] = evalsigne(1)|evalvarn(v);
     327     4335213 :   for (i = 2; i < a; i++) gel(p,i) = gen_0;
     328     1700420 :   gel(p,a) = gen_1; return p;
     329             : }
     330             : /* x^n, no assumption on n */
     331             : INLINE GEN
     332         287 : pol_xnall(long n, long v)
     333             : {
     334         287 :   if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
     335         280 :   return pol_xn(n, v);
     336             : }
     337             : /* x^n, assume n >= 0 */
     338             : INLINE GEN
     339         210 : polxn_Flx(long n, long sv) {
     340         210 :   long i, a = n+2;
     341         210 :   GEN p = cgetg(a+1, t_VECSMALL);
     342         210 :   p[1] = sv;
     343        1022 :   for (i = 2; i < a; i++) p[i] = 0;
     344         210 :   p[a] = 1; return p;
     345             : }
     346             : INLINE GEN
     347     1840069 : pol_1(long v) {
     348     1840069 :   GEN p = cgetg(3, t_POL);
     349     1840067 :   p[1] = evalsigne(1)|evalvarn(v);
     350     1840067 :   gel(p,2) = gen_1; return p;
     351             : }
     352             : INLINE GEN
     353    30307193 : pol_0(long v)
     354             : {
     355    30307193 :   GEN x = cgetg(2,t_POL);
     356    30307185 :   x[1] = evalvarn(v); return x;
     357             : }
     358             : #define retconst_vec(n,x)\
     359             :   do { long _i, _n = (n);\
     360             :        GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
     361             :        for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
     362             :        return _v; } while(0)
     363             : INLINE GEN
     364   131084646 : const_vec(long n, GEN x) { retconst_vec(n, x); }
     365             : #define retconst_col(n,x)\
     366             :   do { long _i, _n = (n);\
     367             :        GEN _v = cgetg(_n+1, t_COL), _x = (x);\
     368             :        for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
     369             :        return _v; } while(0)
     370             : INLINE GEN
     371    35749795 : const_col(long n, GEN x) { retconst_col(n, x); }
     372             : INLINE GEN
     373    10952918 : const_vecsmall(long n, long c)
     374             : {
     375             :   long i;
     376    10952918 :   GEN V = cgetg(n+1,t_VECSMALL);
     377   366965815 :   for(i=1;i<=n;i++) V[i] = c;
     378    10952899 :   return V;
     379             : }
     380             : 
     381             : /***   ZERO   ***/
     382             : /* O(p^e) */
     383             : INLINE GEN
     384      362811 : zeropadic(GEN p, long e)
     385             : {
     386      362811 :   GEN y = cgetg(5,t_PADIC);
     387      362811 :   gel(y,4) = gen_0;
     388      362811 :   gel(y,3) = gen_1;
     389      362811 :   gel(y,2) = icopy(p);
     390      362811 :   y[1] = evalvalp(e) | _evalprecp(0);
     391      362811 :   return y;
     392             : }
     393             : INLINE GEN
     394         966 : zeropadic_shallow(GEN p, long e)
     395             : {
     396         966 :   GEN y = cgetg(5,t_PADIC);
     397         966 :   gel(y,4) = gen_0;
     398         966 :   gel(y,3) = gen_1;
     399         966 :   gel(y,2) = p;
     400         966 :   y[1] = evalvalp(e) | _evalprecp(0);
     401         966 :   return y;
     402             : }
     403             : /* O(pol_x(v)^e) */
     404             : INLINE GEN
     405      136234 : zeroser(long v, long e)
     406             : {
     407      136234 :   GEN x = cgetg(2, t_SER);
     408      136234 :   x[1] = evalvalp(e) | evalvarn(v); return x;
     409             : }
     410             : INLINE int
     411     6147703 : ser_isexactzero(GEN x)
     412             : {
     413     6147703 :   if (!signe(x)) switch(lg(x))
     414             :   {
     415      131075 :     case 2: return 1;
     416        1267 :     case 3: return isexactzero(gel(x,2));
     417             :   }
     418     6015361 :   return 0;
     419             : }
     420             : /* 0 * pol_x(v) */
     421             : INLINE GEN
     422    14216377 : zeropol(long v) { return pol_0(v); }
     423             : /* vector(n) */
     424             : INLINE GEN
     425    41007441 : zerocol(long n)
     426             : {
     427    41007441 :   GEN y = cgetg(n+1,t_COL);
     428   294879395 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     429    41007635 :   return y;
     430             : }
     431             : /* vectorv(n) */
     432             : INLINE GEN
     433    19644992 : zerovec(long n)
     434             : {
     435    19644992 :   GEN y = cgetg(n+1,t_VEC);
     436   146268331 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     437    19644992 :   return y;
     438             : }
     439             : /* matrix(m, n) */
     440             : INLINE GEN
     441       25773 : zeromat(long m, long n)
     442             : {
     443       25773 :   GEN y = cgetg(n+1,t_MAT);
     444       25773 :   GEN v = zerocol(m);
     445       95845 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     446       25773 :   return y;
     447             : }
     448             : /* = zero_zx, sv is a evalvarn()*/
     449             : INLINE GEN
     450      948283 : zero_Flx(long sv) { return pol0_Flx(sv); }
     451             : INLINE GEN
     452    34134547 : zero_Flv(long n)
     453             : {
     454    34134547 :   GEN y = cgetg(n+1,t_VECSMALL);
     455   626644255 :   long i; for (i=1; i<=n; i++) y[i] = 0;
     456    34135495 :   return y;
     457             : }
     458             : /* matrix(m, n) */
     459             : INLINE GEN
     460     1116373 : zero_Flm(long m, long n)
     461             : {
     462     1116373 :   GEN y = cgetg(n+1,t_MAT);
     463     1116366 :   GEN v = zero_Flv(m);
     464     7716879 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     465     1116363 :   return y;
     466             : }
     467             : /* matrix(m, n) */
     468             : INLINE GEN
     469      113228 : zero_Flm_copy(long m, long n)
     470             : {
     471      113228 :   GEN y = cgetg(n+1,t_MAT);
     472     1650772 :   long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
     473      113222 :   return y;
     474             : }
     475             : 
     476             : INLINE GEN
     477      855347 : zero_F2v(long m)
     478             : {
     479      855347 :   long l = nbits2nlong(m);
     480      855347 :   GEN v  = zero_Flv(l+1);
     481      855347 :   v[1] = m;
     482      855347 :   return v;
     483             : }
     484             : 
     485             : INLINE GEN
     486           0 : zero_F2m(long m, long n)
     487             : {
     488             :   long i;
     489           0 :   GEN M = cgetg(n+1, t_MAT);
     490           0 :   GEN v = zero_F2v(m);
     491           0 :   for (i = 1; i <= n; i++)
     492           0 :     gel(M,i) = v;
     493           0 :   return M;
     494             : }
     495             : 
     496             : 
     497             : INLINE GEN
     498      304237 : zero_F2m_copy(long m, long n)
     499             : {
     500             :   long i;
     501      304237 :   GEN M = cgetg(n+1, t_MAT);
     502      797767 :   for (i = 1; i <= n; i++)
     503      493530 :     gel(M,i)= zero_F2v(m);
     504      304237 :   return M;
     505             : }
     506             : 
     507             : /* matrix(m, n) */
     508             : INLINE GEN
     509     5473988 : zeromatcopy(long m, long n)
     510             : {
     511     5473988 :   GEN y = cgetg(n+1,t_MAT);
     512    21889811 :   long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
     513     5473988 :   return y;
     514             : }
     515             : 
     516             : INLINE GEN
     517       17640 : zerovec_block(long len)
     518             : {
     519             :   long i;
     520       17640 :   GEN blk = cgetg_block(len + 1, t_VEC);
     521      582120 :   for (i = 1; i <= len; ++i)
     522      564480 :     gel(blk, i) = gen_0;
     523       17640 :   return blk;
     524             : }
     525             : 
     526             : /* i-th vector in the standard basis */
     527             : INLINE GEN
     528     1333250 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
     529             : INLINE GEN
     530      363534 : vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
     531             : INLINE GEN
     532          42 : F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
     533             : INLINE GEN
     534       27542 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
     535             : INLINE GEN
     536      479236 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
     537             : 
     538             : INLINE GEN
     539    13055215 : shallowcopy(GEN x)
     540    13055215 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
     541             : 
     542             : /* routines for naive growarrays */
     543             : INLINE GEN
     544    12381007 : vectrunc_init(long l)
     545             : {
     546    12381007 :   GEN z = new_chunk(l);
     547    12380822 :   z[0] = evaltyp(t_VEC) | _evallg(1); return z;
     548             : }
     549             : INLINE GEN
     550      347564 : coltrunc_init(long l)
     551             : {
     552      347564 :   GEN z = new_chunk(l);
     553      347564 :   z[0] = evaltyp(t_COL) | _evallg(1); return z;
     554             : }
     555             : INLINE void
     556   266859205 : lg_increase(GEN x) { x[0]++; }
     557             : INLINE void
     558    51326061 : vectrunc_append(GEN x, GEN t) { gel(x, lg(x)) = t; lg_increase(x); }
     559             : INLINE void
     560       10416 : vectrunc_append_batch(GEN x, GEN y)
     561             : {
     562       10416 :   long i, l = lg(x), ly = lg(y);
     563       10416 :   GEN z = x + l-1;
     564       34482 :   for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
     565       10416 :   setlg(x, l+ly-1);
     566       10416 : }
     567             : INLINE GEN
     568    88206019 : vecsmalltrunc_init(long l)
     569             : {
     570    88206019 :   GEN z = new_chunk(l);
     571    88206019 :   z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
     572             : }
     573             : INLINE void
     574    33719145 : vecsmalltrunc_append(GEN x, long t) { x[ lg(x) ] = t; lg_increase(x); }
     575             : 
     576             : /*******************************************************************/
     577             : /*                                                                 */
     578             : /*                    STRING HASH FUNCTIONS                        */
     579             : /*                                                                 */
     580             : /*******************************************************************/
     581             : INLINE ulong
     582     2138932 : hash_str(const char *str)
     583             : {
     584     2138932 :   ulong hash = 5381UL, c;
     585    21426563 :   while ( (c = (ulong)*str++) )
     586    19287631 :     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     587     2138932 :   return hash;
     588             : }
     589             : INLINE ulong
     590     9962507 : hash_str_len(const char *str, long len)
     591             : {
     592     9962507 :   ulong hash = 5381UL;
     593             :   long i;
     594    75262715 :   for (i = 0; i < len; i++)
     595             :   {
     596    65300208 :     ulong c = (ulong)*str++;
     597    65300208 :     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     598             :   }
     599     9962507 :   return hash;
     600             : }
     601             : 
     602             : /*******************************************************************/
     603             : /*                                                                 */
     604             : /*                        VEC / COL / VECSMALL                     */
     605             : /*                                                                 */
     606             : /*******************************************************************/
     607             : /* shallow*/
     608             : INLINE GEN
     609           0 : vec_shorten(GEN v, long n)
     610             : {
     611             :   long i;
     612           0 :   GEN V = cgetg(n+1,t_VEC);
     613           0 :   for(i=1;i<=n;i++) gel(V,i) = gel(v,i);
     614           0 :   return V;
     615             : }
     616             : /* shallow*/
     617             : INLINE GEN
     618       13724 : vec_lengthen(GEN v, long n)
     619             : {
     620             :   long i;
     621       13724 :   long l=lg(v);
     622       13724 :   GEN V = cgetg(n+1,t_VEC);
     623      441486 :   for(i=1;i<l;i++) gel(V,i) = gel(v,i);
     624       13724 :   return V;
     625             : }
     626             : /* shallow*/
     627             : INLINE GEN
     628       95955 : vec_append(GEN V, GEN s)
     629             : {
     630       95955 :   long i, l2 = lg(V);
     631       95955 :   GEN res = cgetg(l2+1, typ(V));
     632      860080 :   for (i = 1; i < l2; ++i) gel(res, i) = gel(V,i);
     633       95955 :   gel(res,l2) = s; return res;
     634             : }
     635             : /* shallow*/
     636             : INLINE GEN
     637       40694 : vec_prepend(GEN v, GEN s)
     638             : {
     639       40694 :   long i, l = lg(v);
     640       40694 :   GEN w = cgetg(l+1, typ(v));
     641       40694 :   gel(w,1) = s;
     642       77285 :   for (i = 2; i <= l; ++i) gel(w,i) = gel(v,i-1);
     643       40694 :   return w;
     644             : }
     645             : /* shallow*/
     646             : INLINE GEN
     647           0 : vec_setconst(GEN v, GEN x)
     648             : {
     649           0 :   long i, l = lg(v);
     650           0 :   for (i = 1; i < l; i++) gel(v,i) = x;
     651           0 :   return v;
     652             : }
     653             : INLINE GEN
     654       42753 : vecsmall_shorten(GEN v, long n)
     655             : {
     656             :   long i;
     657       42753 :   GEN V = cgetg(n+1,t_VECSMALL);
     658      568260 :   for(i=1;i<=n;i++) V[i] = v[i];
     659       42751 :   return V;
     660             : }
     661             : INLINE GEN
     662           0 : vecsmall_lengthen(GEN v, long n)
     663             : {
     664           0 :   long i, l = lg(v);
     665           0 :   GEN V = cgetg(n+1,t_VECSMALL);
     666           0 :   for(i=1;i<l;i++) V[i] = v[i];
     667           0 :   return V;
     668             : }
     669             : 
     670             : INLINE GEN
     671     3331462 : vec_to_vecsmall(GEN x)
     672    16219399 : { pari_APPLY_long(itos(gel(x,i))) }
     673             : INLINE GEN
     674      142709 : vecsmall_to_vec(GEN x)
     675     2537173 : { pari_APPLY_type(t_VEC, stoi(x[i])) }
     676             : INLINE GEN
     677        2387 : vecsmall_to_vec_inplace(GEN z)
     678             : {
     679        2387 :   long i, l = lg(z);
     680       70399 :   for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
     681        2387 :   settyp(z, t_VEC); return z;
     682             : }
     683             : INLINE GEN
     684     5338818 : vecsmall_to_col(GEN x)
     685    52643902 : { pari_APPLY_type(t_COL, stoi(x[i])) }
     686             : 
     687             : INLINE int
     688     6148433 : vecsmall_lexcmp(GEN x, GEN y)
     689             : {
     690             :   long lx,ly,l,i;
     691     6148433 :   lx = lg(x);
     692     6148433 :   ly = lg(y); l = minss(lx,ly);
     693    32009135 :   for (i=1; i<l; i++)
     694    30333058 :     if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
     695     1676077 :   if (lx == ly) return 0;
     696       30577 :   return (lx < ly)? -1 : 1;
     697             : }
     698             : 
     699             : INLINE int
     700    85249612 : vecsmall_prefixcmp(GEN x, GEN y)
     701             : {
     702    85249612 :   long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
     703   394179177 :   for (i=1; i<l; i++)
     704   374535735 :     if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
     705    19643442 :   return 0;
     706             : }
     707             : 
     708             : /*Can be used on t_VEC, but coeffs not gcopy-ed*/
     709             : INLINE GEN
     710       22323 : vecsmall_prepend(GEN V, long s)
     711             : {
     712       22323 :   long i, l2 = lg(V);
     713       22323 :   GEN res = cgetg(l2+1, typ(V));
     714       22323 :   res[1] = s;
     715       70875 :   for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
     716       22323 :   return res;
     717             : }
     718             : 
     719             : INLINE GEN
     720     2956699 : vecsmall_append(GEN V, long s)
     721             : {
     722     2956699 :   long i, l2 = lg(V);
     723     2956699 :   GEN res = cgetg(l2+1, t_VECSMALL);
     724     4417798 :   for (i = 1; i < l2; ++i) res[i] = V[i];
     725     2956699 :   res[l2] = s; return res;
     726             : }
     727             : 
     728             : INLINE GEN
     729      823125 : vecsmall_concat(GEN u, GEN v)
     730             : {
     731      823125 :   long i, l1 = lg(u)-1, l2 = lg(v)-1;
     732      823125 :   GEN res = cgetg(l1+l2+1, t_VECSMALL);
     733     6243716 :   for (i = 1; i <= l1; ++i) res[i]    = u[i];
     734     7007144 :   for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
     735      823125 :   return res;
     736             : }
     737             : 
     738             : /* return the number of indices where u and v are equal */
     739             : INLINE long
     740           0 : vecsmall_coincidence(GEN u, GEN v)
     741             : {
     742           0 :   long i, s = 0, l = minss(lg(u),lg(v));
     743           0 :   for(i=1; i<l; i++)
     744           0 :     if(u[i] == v[i]) s++;
     745           0 :   return s;
     746             : }
     747             : 
     748             : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
     749             : INLINE long
     750           0 : vecsmall_isin(GEN v, long x)
     751             : {
     752           0 :   long i, l = lg(v);
     753           0 :   for (i = 1; i < l; i++)
     754           0 :     if (v[i] == x) return i;
     755           0 :   return 0;
     756             : }
     757             : 
     758             : INLINE long
     759          84 : vecsmall_pack(GEN V, long base, long mod)
     760             : {
     761          84 :   long i, s = 0;
     762         273 :   for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
     763          84 :   return s;
     764             : }
     765             : 
     766             : INLINE long
     767          14 : vecsmall_indexmax(GEN x)
     768             : {
     769          14 :   long i, i0 = 1, t = x[1], lx = lg(x);
     770          56 :   for (i=2; i<lx; i++)
     771          42 :     if (x[i] > t) t = x[i0=i];
     772          14 :   return i0;
     773             : }
     774             : 
     775             : INLINE long
     776      375265 : vecsmall_max(GEN x)
     777             : {
     778      375265 :   long i, t = x[1], lx = lg(x);
     779      963765 :   for (i=2; i<lx; i++)
     780      588500 :     if (x[i] > t) t = x[i];
     781      375265 :   return t;
     782             : }
     783             : 
     784             : INLINE long
     785          14 : vecsmall_indexmin(GEN x)
     786             : {
     787          14 :   long i, i0 = 1, t = x[1], lx =lg(x);
     788          56 :   for (i=2; i<lx; i++)
     789          42 :     if (x[i] < t) t = x[i0=i];
     790          14 :   return i0;
     791             : }
     792             : 
     793             : INLINE long
     794        1421 : vecsmall_min(GEN x)
     795             : {
     796        1421 :   long i, t = x[1], lx =lg(x);
     797        9947 :   for (i=2; i<lx; i++)
     798        8526 :     if (x[i] < t) t = x[i];
     799        1421 :   return t;
     800             : }
     801             : 
     802             : INLINE int
     803     5305934 : ZV_isscalar(GEN x)
     804             : {
     805     5305934 :   long l = lg(x);
     806    11285490 :   while (--l > 1)
     807     9958473 :     if (signe(gel(x, l))) return 0;
     808     1327017 :   return 1;
     809             : }
     810             : INLINE int
     811    24180783 : QV_isscalar(GEN x)
     812             : {
     813    24180783 :   long lx = lg(x),i;
     814    26668509 :   for (i=2; i<lx; i++)
     815    25892763 :     if (!isintzero(gel(x, i))) return 0;
     816      775746 :   return 1;
     817             : }
     818             : INLINE int
     819        4314 : RgV_isscalar(GEN x)
     820             : {
     821        4314 :   long lx = lg(x),i;
     822        4461 :   for (i=2; i<lx; i++)
     823        4454 :     if (!gequal0(gel(x, i))) return 0;
     824           7 :   return 1;
     825             : }
     826             : INLINE int
     827           0 : RgX_isscalar(GEN x)
     828             : {
     829             :   long i;
     830           0 :   for (i=lg(x)-1; i>2; i--)
     831           0 :     if (!gequal0(gel(x, i))) return 0;
     832           0 :   return 1;
     833             : }
     834             : INLINE long
     835    24805489 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
     836             : 
     837             : INLINE int
     838        9492 : RgX_is_rational(GEN x)
     839             : {
     840             :   long i;
     841       51483 :   for (i = lg(x)-1; i > 1; i--)
     842       47584 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     843        3899 :   return 1;
     844             : }
     845             : INLINE int
     846     5208430 : RgX_is_ZX(GEN x)
     847             : {
     848             :   long i;
     849    26005050 :   for (i = lg(x)-1; i > 1; i--)
     850    20849148 :     if (typ(gel(x,i)) != t_INT) return 0;
     851     5155902 :   return 1;
     852             : }
     853             : INLINE int
     854      256690 : RgX_is_QX(GEN x)
     855             : {
     856      256690 :   long k = lg(x)-1;
     857      949133 :   for ( ; k>1; k--)
     858      692723 :     if (!is_rational_t(typ(gel(x,k)))) return 0;
     859      256410 :   return 1;
     860             : }
     861             : INLINE int
     862     2338977 : RgX_is_monomial(GEN x)
     863             : {
     864             :   long i;
     865     2338977 :   if (!signe(x)) return 0;
     866     5846503 :   for (i=lg(x)-2; i>1; i--)
     867     3557040 :     if (!isexactzero(gel(x,i))) return 0;
     868     2289463 :   return 1;
     869             : }
     870             : INLINE int
     871    27695216 : RgV_is_ZV(GEN x)
     872             : {
     873             :   long i;
     874   113242043 :   for (i = lg(x)-1; i > 0; i--)
     875    85550131 :     if (typ(gel(x,i)) != t_INT) return 0;
     876    27691912 :   return 1;
     877             : }
     878             : INLINE int
     879       24038 : RgV_is_QV(GEN x)
     880             : {
     881             :   long i;
     882      108969 :   for (i = lg(x)-1; i > 0; i--)
     883       86625 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     884       22344 :   return 1;
     885             : }
     886             : INLINE long
     887       24241 : RgV_isin_i(GEN v, GEN x, long n)
     888             : {
     889             :   long i;
     890      513345 :   for (i = 1; i <= n; i++)
     891      513086 :     if (gequal(gel(v,i), x)) return i;
     892         259 :   return 0;
     893             : }
     894             : INLINE long
     895       24241 : RgV_isin(GEN v, GEN x) { return RgV_isin_i(v, x, lg(v)-1); }
     896             : 
     897             : /********************************************************************/
     898             : /**                                                                **/
     899             : /**            Dynamic arrays implementation                       **/
     900             : /**                                                                **/
     901             : /********************************************************************/
     902             : INLINE void **
     903   470214851 : pari_stack_base(pari_stack *s) { return s->data; }
     904             : 
     905             : INLINE void
     906     2741986 : pari_stack_init(pari_stack *s, size_t size, void **data)
     907             : {
     908     2741986 :   s->data = data;
     909     2741986 :   *data = NULL;
     910     2741986 :   s->n = 0;
     911     2741986 :   s->alloc = 0;
     912     2741986 :   s->size = size;
     913     2741986 : }
     914             : 
     915             : INLINE void
     916   467694197 : pari_stack_alloc(pari_stack *s, long nb)
     917             : {
     918   467694197 :   void **sdat = pari_stack_base(s);
     919   467606118 :   long alloc = s->alloc;
     920   467606118 :   if (s->n+nb <= alloc) return;
     921     1043935 :   if (!alloc)
     922      978399 :     alloc = nb;
     923             :   else
     924             :   {
     925      133973 :     while (s->n+nb > alloc) alloc <<= 1;
     926             :   }
     927     1043935 :   pari_realloc_ip(sdat,alloc*s->size);
     928     1050377 :   s->alloc = alloc;
     929             : }
     930             : 
     931             : INLINE long
     932   384711771 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
     933             : 
     934             : INLINE void
     935     2554507 : pari_stack_delete(pari_stack *s)
     936             : {
     937     2554507 :   void **sdat = pari_stack_base(s);
     938     2550711 :   if (*sdat) pari_free(*sdat);
     939     2565279 : }
     940             : 
     941             : INLINE void
     942        5060 : pari_stack_pushp(pari_stack *s, void *u)
     943             : {
     944        5060 :   long n = pari_stack_new(s);
     945        5060 :   void **sdat =(void**) *pari_stack_base(s);
     946        5060 :   sdat[n] = u;
     947        5060 : }
     948             : 
     949             : /*******************************************************************/
     950             : /*                                                                 */
     951             : /*                            EXTRACT                              */
     952             : /*                                                                 */
     953             : /*******************************************************************/
     954             : INLINE GEN
     955   353653782 : vecslice(GEN A, long y1, long y2)
     956             : {
     957   353653782 :   long i,lB = y2 - y1 + 2;
     958   353653782 :   GEN B = cgetg(lB, typ(A));
     959  1639996938 :   for (i=1; i<lB; i++) B[i] = A[y1-1+i];
     960   353645909 :   return B;
     961             : }
     962             : INLINE GEN
     963      994112 : vecslicepermute(GEN A, GEN p, long y1, long y2)
     964             : {
     965      994112 :   long i,lB = y2 - y1 + 2;
     966      994112 :   GEN B = cgetg(lB, typ(A));
     967    17691018 :   for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
     968      994111 :   return B;
     969             : }
     970             : /* rowslice(rowpermute(A,p), x1, x2) */
     971             : INLINE GEN
     972       45740 : rowslicepermute(GEN x, GEN p, long j1, long j2)
     973      214292 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
     974             : 
     975             : INLINE GEN
     976    30031707 : rowslice(GEN x, long j1, long j2)
     977   359947295 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
     978             : 
     979             : INLINE GEN
     980     5125973 : matslice(GEN A, long x1, long x2, long y1, long y2)
     981     5125973 : { return rowslice(vecslice(A, y1, y2), x1, x2); }
     982             : 
     983             : /* shallow, remove coeff of index j */
     984             : INLINE GEN
     985        4368 : rowsplice(GEN x, long j)
     986       11452 : { pari_APPLY_same(vecsplice(gel(x,i), j)) }
     987             : 
     988             : /* shallow, remove coeff of index j */
     989             : INLINE GEN
     990      274844 : vecsplice(GEN a, long j)
     991             : {
     992      274844 :   long i, k, l = lg(a);
     993             :   GEN b;
     994      274844 :   if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
     995      274844 :   b = cgetg(l-1, typ(a));
     996     1273934 :   for (i = k = 1; i < l; i++)
     997      999090 :     if (i != j) gel(b, k++) = gel(a,i);
     998      274844 :   return b;
     999             : }
    1000             : /* shallow */
    1001             : INLINE GEN
    1002        1036 : RgM_minor(GEN a, long i, long j)
    1003             : {
    1004        1036 :   GEN b = vecsplice(a, j);
    1005        1036 :   long k, l = lg(b);
    1006        4242 :   for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
    1007        1036 :   return b;
    1008             : }
    1009             : 
    1010             : /* A[x0,] */
    1011             : INLINE GEN
    1012      390085 : row(GEN x, long j)
    1013     4018513 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
    1014             : INLINE GEN
    1015     4174031 : Flm_row(GEN x, long j)
    1016   105098804 : { pari_APPLY_ulong((ulong)coeff(x, j, i)) }
    1017             : /* A[x0,] */
    1018             : INLINE GEN
    1019      204834 : rowcopy(GEN x, long j)
    1020     2097809 : { pari_APPLY_type(t_VEC, gcopy(gcoeff(x, j, i))) }
    1021             : /* A[x0, x1..x2] */
    1022             : INLINE GEN
    1023         973 : row_i(GEN A, long x0, long x1, long x2)
    1024             : {
    1025         973 :   long i, lB = x2 - x1 + 2;
    1026         973 :   GEN B  = cgetg(lB, t_VEC);
    1027        2947 :   for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
    1028         973 :   return B;
    1029             : }
    1030             : 
    1031             : INLINE GEN
    1032     5971743 : vecreverse(GEN A)
    1033             : {
    1034             :   long i, l;
    1035     5971743 :   GEN B = cgetg_copy(A, &l);
    1036    14497634 :   for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
    1037     5971743 :   return B;
    1038             : }
    1039             : 
    1040             : INLINE GEN
    1041         679 : vecsmall_reverse(GEN A)
    1042             : {
    1043             :   long i, l;
    1044         679 :   GEN B = cgetg_copy(A, &l);
    1045        2233 :   for (i=1; i<l; i++) B[i] = A[l-i];
    1046         679 :   return B;
    1047             : }
    1048             : 
    1049             : INLINE void
    1050         428 : vecreverse_inplace(GEN y)
    1051             : {
    1052         428 :   long l = lg(y), lim = l>>1, i;
    1053        1606 :   for (i = 1; i <= lim; i++)
    1054             :   {
    1055        1178 :     GEN z = gel(y,i);
    1056        1178 :     gel(y,i)    = gel(y,l-i);
    1057        1178 :     gel(y,l-i) = z;
    1058             :   }
    1059         428 : }
    1060             : 
    1061             : INLINE GEN
    1062    35270802 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
    1063             : 
    1064             : INLINE GEN
    1065     6907785 : vecpermute(GEN A, GEN x)
    1066    80577273 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
    1067             : 
    1068             : INLINE GEN
    1069     3481618 : rowpermute(GEN x, GEN p)
    1070    38419720 : { pari_APPLY_same(typ(gel(x,i)) == t_VECSMALL ? vecsmallpermute(gel(x, i), p)
    1071             :                                               : vecpermute(gel(x, i), p))
    1072             : }
    1073             : /*******************************************************************/
    1074             : /*                                                                 */
    1075             : /*                          PERMUTATIONS                           */
    1076             : /*                                                                 */
    1077             : /*******************************************************************/
    1078             : INLINE GEN
    1079      825072 : identity_zv(long n)
    1080             : {
    1081      825072 :   GEN v = cgetg(n+1, t_VECSMALL);
    1082             :   long i;
    1083    13718227 :   for (i = 1; i <= n; i++) v[i] = i;
    1084      825064 :   return v;
    1085             : }
    1086             : INLINE GEN
    1087        3591 : identity_ZV(long n)
    1088             : {
    1089        3591 :   GEN v = cgetg(n+1, t_VEC);
    1090             :   long i;
    1091       44688 :   for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
    1092        3591 :   return v;
    1093             : }
    1094             : /* identity permutation */
    1095             : INLINE GEN
    1096      824539 : identity_perm(long n) { return identity_zv(n); }
    1097             : 
    1098             : /* assume d <= n */
    1099             : INLINE GEN
    1100       98483 : cyclic_perm(long n, long d)
    1101             : {
    1102       98483 :   GEN perm = cgetg(n+1, t_VECSMALL);
    1103             :   long i;
    1104      504098 :   for (i = 1; i <= n-d; i++) perm[i] = i+d;
    1105      225029 :   for (     ; i <= n;   i++) perm[i] = i-n+d;
    1106       98483 :   return perm;
    1107             : }
    1108             : 
    1109             : /* Multiply (compose) two permutations */
    1110             : INLINE GEN
    1111    36142977 : perm_mul(GEN s, GEN x)
    1112   494255391 : { pari_APPLY_long(s[x[i]]) }
    1113             : 
    1114             : /* Compute the inverse (reciprocal) of a permutation. */
    1115             : INLINE GEN
    1116     1222224 : perm_inv(GEN x)
    1117             : {
    1118             :   long i, lx;
    1119     1222224 :   GEN y = cgetg_copy(x, &lx);
    1120    16789762 :   for (i=1; i<lx; i++) y[ x[i] ] = i;
    1121     1222224 :   return y;
    1122             : }
    1123             : /* Return s*t*s^-1 */
    1124             : INLINE GEN
    1125      417109 : perm_conj(GEN s, GEN t)
    1126             : {
    1127             :   long i, l;
    1128      417109 :   GEN v = cgetg_copy(s, &l);
    1129     6836291 :   for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
    1130      417109 :   return v;
    1131             : }
    1132             : 
    1133             : INLINE void
    1134   294599868 : pari_free(void *pointer)
    1135             : {
    1136   294599868 :   BLOCK_SIGINT_START;
    1137   294617440 :   free(pointer);
    1138   294617440 :   BLOCK_SIGINT_END;
    1139   294620882 : }
    1140             : INLINE void*
    1141   505131536 : pari_malloc(size_t size)
    1142             : {
    1143   505131536 :   if (size)
    1144             :   {
    1145             :     char *tmp;
    1146   505131545 :     BLOCK_SIGINT_START;
    1147   505132158 :     tmp = (char*)malloc(size);
    1148   505132158 :     BLOCK_SIGINT_END;
    1149   505130815 :     if (!tmp) pari_err(e_MEM);
    1150   505134426 :     return tmp;
    1151             :   }
    1152           0 :   return NULL;
    1153             : }
    1154             : INLINE void*
    1155        1633 : pari_realloc(void *pointer, size_t size)
    1156             : {
    1157             :   char *tmp;
    1158             : 
    1159        1633 :   BLOCK_SIGINT_START;
    1160        1633 :   if (!pointer) tmp = (char *) malloc(size);
    1161        1633 :   else tmp = (char *) realloc(pointer,size);
    1162        1633 :   BLOCK_SIGINT_END;
    1163        1633 :   if (!tmp) pari_err(e_MEM);
    1164        1633 :   return tmp;
    1165             : }
    1166             : INLINE void
    1167     1059565 : pari_realloc_ip(void **pointer, size_t size)
    1168             : {
    1169             :   char *tmp;
    1170     1059565 :   BLOCK_SIGINT_START;
    1171     1058559 :   if (!*pointer) tmp = (char *) malloc(size);
    1172       69706 :   else tmp = (char *) realloc(*pointer,size);
    1173     1058559 :   if (!tmp) pari_err(e_MEM);
    1174     1058559 :   *pointer = tmp;
    1175     1058559 :   BLOCK_SIGINT_END;
    1176     1055361 : }
    1177             : 
    1178             : INLINE void*
    1179       39286 : pari_calloc(size_t size)
    1180             : {
    1181       39286 :   void *t = pari_malloc(size);
    1182       39286 :   memset(t, 0, size); return t;
    1183             : }
    1184             : INLINE GEN
    1185        7070 : cgetalloc(long t, size_t l)
    1186             : { /* evallg may raise e_OVERFLOW, which would leak x */
    1187        7070 :   ulong x0 = evaltyp(t) | evallg(l);
    1188        7070 :   GEN x = (GEN)pari_malloc(l * sizeof(long));
    1189        7070 :   x[0] = x0; return x;
    1190             : }
    1191             : 
    1192             : /*******************************************************************/
    1193             : /*                                                                 */
    1194             : /*                       GARBAGE COLLECTION                        */
    1195             : /*                                                                 */
    1196             : /*******************************************************************/
    1197             : /* copy integer x as if we had set_avma(av) */
    1198             : INLINE GEN
    1199  4644134104 : icopy_avma(GEN x, pari_sp av)
    1200             : {
    1201  4644134104 :   long i = lgefint(x), lx = i;
    1202  4644134104 :   GEN y = ((GEN)av) - i;
    1203 24663180198 :   while (--i > 0) y[i] = x[i];
    1204  4644134104 :   y[0] = evaltyp(t_INT)|evallg(lx);
    1205  4646919395 :   return y;
    1206             : }
    1207             : /* copy leaf x as if we had set_avma(av) */
    1208             : INLINE GEN
    1209   212543088 : leafcopy_avma(GEN x, pari_sp av)
    1210             : {
    1211   212543088 :   long i = lg(x);
    1212   212543088 :   GEN y = ((GEN)av) - i;
    1213  1607878843 :   while (--i > 0) y[i] = x[i];
    1214   212543088 :   y[0] = x[0] & (~CLONEBIT);
    1215   212543088 :   return y;
    1216             : }
    1217             : INLINE GEN
    1218   602483063 : gerepileuptoleaf(pari_sp av, GEN x)
    1219             : {
    1220             :   long lx;
    1221             :   GEN q;
    1222             : 
    1223   602483063 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1224   602245954 :   lx = lg(x);
    1225   602245954 :   q = ((GEN)av) - lx;
    1226   602245954 :   set_avma((pari_sp)q);
    1227  6558981863 :   while (--lx >= 0) q[lx] = x[lx];
    1228   602097773 :   return q;
    1229             : }
    1230             : INLINE GEN
    1231  1767419763 : gerepileuptoint(pari_sp av, GEN x)
    1232             : {
    1233  1767419763 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1234  1551277554 :   set_avma((pari_sp)icopy_avma(x, av));
    1235  1553352977 :   return (GEN)avma;
    1236             : }
    1237             : INLINE GEN
    1238   801815080 : gerepileupto(pari_sp av, GEN x)
    1239             : {
    1240   801815080 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1241   716596333 :   switch(typ(x))
    1242             :   { /* non-default = !is_recursive_t(tq) */
    1243   217884938 :     case t_INT: return gerepileuptoint(av, x);
    1244   138275058 :     case t_REAL:
    1245             :     case t_STR:
    1246   138275058 :     case t_VECSMALL: return gerepileuptoleaf(av,x);
    1247   360436337 :     default:
    1248             :       /* NB: x+i --> ((long)x) + i*sizeof(long) */
    1249   360436337 :       return gerepile(av, (pari_sp) (x+lg(x)), x);
    1250             :   }
    1251             : }
    1252             : 
    1253             : /* gerepileupto(av, gcopy(x)) */
    1254             : INLINE GEN
    1255    72031482 : gerepilecopy(pari_sp av, GEN x)
    1256             : {
    1257    72031482 :   if (is_recursive_t(typ(x)))
    1258             :   {
    1259    67443782 :     GENbin *p = copy_bin(x);
    1260    67444833 :     set_avma(av); return bin_copy(p);
    1261             :   }
    1262             :   else
    1263             :   {
    1264     4587812 :     set_avma(av);
    1265     4587819 :     if (x < (GEN)av) {
    1266     3092296 :       if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
    1267     3092296 :       x = leafcopy_avma(x, av);
    1268     3092302 :       set_avma((pari_sp)x);
    1269             :     } else
    1270     1495523 :       x = leafcopy(x);
    1271     4587828 :     return x;
    1272             :   }
    1273             : }
    1274             : 
    1275             : INLINE void
    1276    24485311 : guncloneNULL(GEN x) { if (x) gunclone(x); }
    1277             : INLINE void
    1278      193493 : guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
    1279             : 
    1280             : /* Takes an array of pointers to GENs, of length n. Copies all
    1281             :  * objects to contiguous locations and cleans up the stack between
    1282             :  * av and avma. */
    1283             : INLINE void
    1284      193502 : gerepilemany(pari_sp av, GEN* gptr[], int n)
    1285             : {
    1286             :   int i;
    1287      767569 :   for (i=0; i<n; i++) *gptr[i] = (GEN)copy_bin(*gptr[i]);
    1288      193502 :   set_avma(av);
    1289      767569 :   for (i=0; i<n; i++) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
    1290      193502 : }
    1291             : 
    1292             : INLINE void
    1293    92717239 : gerepileall(pari_sp av, int n, ...)
    1294             : {
    1295             :   int i;
    1296    92717239 :   va_list a; va_start(a, n);
    1297    92717239 :   if (n < 10)
    1298             :   {
    1299             :     GEN *gptr[10];
    1300   302675892 :     for (i=0; i<n; i++)
    1301   209958485 :     { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
    1302    92717407 :     set_avma(av);
    1303   302676150 :     for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
    1304             : 
    1305             :   }
    1306             :   else
    1307             :   {
    1308           2 :     GEN **gptr = (GEN**)  pari_malloc(n*sizeof(GEN*));
    1309           0 :     for (i=0; i<n; i++)
    1310           0 :     { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
    1311           0 :     set_avma(av);
    1312           0 :     for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
    1313           0 :     pari_free(gptr);
    1314             :   }
    1315    92717569 :   va_end(a);
    1316    92717569 : }
    1317             : 
    1318             : INLINE void
    1319      625680 : gerepilecoeffs(pari_sp av, GEN x, int n)
    1320             : {
    1321             :   int i;
    1322     8162028 :   for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
    1323      625680 :   set_avma(av);
    1324     8162028 :   for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
    1325      625680 : }
    1326             : 
    1327             : /* p from copy_bin. Copy p->x back to stack, then destroy p */
    1328             : INLINE GEN
    1329   285548622 : bin_copy(GENbin *p)
    1330             : {
    1331             :   GEN x, y, base;
    1332             :   long dx, len;
    1333             : 
    1334   285548622 :   x   = p->x; if (!x) { pari_free(p); return gen_0; }
    1335   263285992 :   len = p->len;
    1336   263285992 :   base= p->base; dx = x - base;
    1337   263285992 :   y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
    1338   263285646 :   y += dx;
    1339   263285646 :   p->rebase(y, ((ulong)y-(ulong)x));
    1340   263285879 :   pari_free(p); return y;
    1341             : }
    1342             : 
    1343             : INLINE GEN
    1344   548834913 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
    1345             : 
    1346             : INLINE void
    1347    81331888 : cgiv(GEN x)
    1348             : {
    1349    81331888 :   pari_sp av = (pari_sp)(x+lg(x));
    1350    81331888 :   if (isonstack((GEN)av)) set_avma(av);
    1351    81330934 : }
    1352             : 
    1353             : INLINE void
    1354     6552348 : killblock(GEN x) { gunclone(x); }
    1355             : 
    1356             : INLINE int
    1357   240100341 : is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
    1358             : 
    1359             : /*******************************************************************/
    1360             : /*                                                                 */
    1361             : /*                    CONVERSION / ASSIGNMENT                      */
    1362             : /*                                                                 */
    1363             : /*******************************************************************/
    1364             : /* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
    1365             : INLINE GEN
    1366     4171809 : cxcompotor(GEN z, long prec)
    1367             : {
    1368     4171809 :   switch(typ(z))
    1369             :   {
    1370     2466515 :     case t_INT:  return itor(z, prec);
    1371      136689 :     case t_FRAC: return fractor(z, prec);
    1372     1568605 :     case t_REAL: return rtor(z, prec);
    1373           0 :     default: pari_err_TYPE("cxcompotor",z);
    1374             :              return NULL; /* LCOV_EXCL_LINE */
    1375             :   }
    1376             : }
    1377             : INLINE GEN
    1378     2073008 : cxtofp(GEN x, long prec)
    1379     2073008 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
    1380             : 
    1381             : INLINE GEN
    1382      273403 : cxtoreal(GEN q)
    1383      273403 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
    1384             : 
    1385             : INLINE double
    1386    31548937 : gtodouble(GEN x)
    1387             : {
    1388    31548937 :   if (typ(x)!=t_REAL) {
    1389     5743780 :     pari_sp av = avma;
    1390     5743780 :     x = gtofp(x, DEFAULTPREC);
    1391     5743173 :     if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
    1392     5743173 :     set_avma(av);
    1393             :   }
    1394    31548246 :   return rtodbl(x);
    1395             : }
    1396             : INLINE long
    1397    73470098 : gtos(GEN x) {
    1398    73470098 :   if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
    1399    73470091 :   return itos(x);
    1400             : }
    1401             : 
    1402             : INLINE ulong
    1403       88591 : gtou(GEN x) {
    1404       88591 :   if (typ(x) != t_INT || signe(x)<0)
    1405           0 :     pari_err_TYPE("gtou [integer >=0 expected]",x);
    1406       88591 :   return itou(x);
    1407             : }
    1408             : 
    1409             : INLINE GEN
    1410    37933895 : absfrac(GEN x)
    1411             : {
    1412    37933895 :   GEN y = cgetg(3, t_FRAC);
    1413    37933895 :   gel(y,1) = absi(gel(x,1));
    1414    37933895 :   gel(y,2) = icopy(gel(x,2)); return y;
    1415             : }
    1416             : INLINE GEN
    1417       38059 : absfrac_shallow(GEN x)
    1418       38059 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
    1419             : INLINE GEN
    1420    10554785 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
    1421             : INLINE GEN
    1422      139489 : Q_abs_shallow(GEN x)
    1423      139489 : { return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
    1424             : INLINE GEN
    1425       12712 : R_abs_shallow(GEN x)
    1426       12712 : { return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
    1427             : INLINE GEN
    1428           0 : R_abs(GEN x)
    1429           0 : { return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
    1430             : 
    1431             : /* Force z to be of type real/complex with floating point components */
    1432             : INLINE GEN
    1433    36545217 : gtofp(GEN z, long prec)
    1434             : {
    1435    36545217 :   switch(typ(z))
    1436             :   {
    1437    22484078 :     case t_INT:  return itor(z, prec);
    1438     4066233 :     case t_FRAC: return fractor(z, prec);
    1439     8169027 :     case t_REAL: return rtor(z, prec);
    1440     1827635 :     case t_COMPLEX: {
    1441     1827635 :       GEN a = gel(z,1), b = gel(z,2);
    1442     1827635 :       if (isintzero(b)) return cxcompotor(a, prec);
    1443     1827635 :       if (isintzero(a)) {
    1444        3501 :         GEN y = cgetg(3, t_COMPLEX);
    1445        3501 :         b = cxcompotor(b, prec);
    1446        3501 :         gel(y,1) = real_0_bit(expo(b) - prec2nbits(prec));
    1447        3501 :         gel(y,2) = b; return y;
    1448             :       }
    1449     1824134 :       return cxtofp(z, prec);
    1450             :     }
    1451           0 :     case t_QUAD: return quadtofp(z, prec);
    1452           0 :     default: pari_err_TYPE("gtofp",z);
    1453             :              return NULL; /* LCOV_EXCL_LINE */
    1454             :   }
    1455             : }
    1456             : /* Force z to be of type real / int */
    1457             : INLINE GEN
    1458       22428 : gtomp(GEN z, long prec)
    1459             : {
    1460       22428 :   switch(typ(z))
    1461             :   {
    1462          42 :     case t_INT:  return z;
    1463       22386 :     case t_FRAC: return fractor(z, prec);
    1464           0 :     case t_REAL: return rtor(z, prec);
    1465           0 :     case t_QUAD: z = quadtofp(z, prec);
    1466           0 :                  if (typ(z) == t_REAL) return z;
    1467           0 :     default: pari_err_TYPE("gtomp",z);
    1468             :              return NULL; /* LCOV_EXCL_LINE */
    1469             :   }
    1470             : }
    1471             : 
    1472             : INLINE GEN
    1473      985707 : RgX_gtofp(GEN x, long prec)
    1474             : {
    1475             :   long l;
    1476      985707 :   GEN y = cgetg_copy(x, &l);
    1477     6008159 :   while (--l > 1) gel(y,l) = gtofp(gel(x,l), prec);
    1478      985707 :   y[1] = x[1]; return y;
    1479             : }
    1480             : INLINE GEN
    1481     1277917 : RgC_gtofp(GEN x, long prec)
    1482    15216182 : { pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
    1483             : 
    1484             : INLINE GEN
    1485       12215 : RgV_gtofp(GEN x, long prec)
    1486       37555 : { pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
    1487             : 
    1488             : INLINE GEN
    1489      142642 : RgM_gtofp(GEN x, long prec)
    1490     1175330 : { pari_APPLY_same(RgC_gtofp(gel(x,i), prec)) }
    1491             : 
    1492             : INLINE GEN
    1493         574 : RgC_gtomp(GEN x, long prec)
    1494       23002 : { pari_APPLY_type(t_COL, gtomp(gel(x,i), prec)) }
    1495             : 
    1496             : INLINE GEN
    1497          21 : RgM_gtomp(GEN x, long prec)
    1498         595 : { pari_APPLY_same(RgC_gtomp(gel(x,i), prec)) }
    1499             : 
    1500             : INLINE GEN
    1501       11270 : RgX_fpnorml2(GEN x, long prec)
    1502             : {
    1503       11270 :   pari_sp av = avma;
    1504       11270 :   return gerepileupto(av, gnorml2(RgX_gtofp(x, prec)));
    1505             : }
    1506             : INLINE GEN
    1507      232946 : RgC_fpnorml2(GEN x, long prec)
    1508             : {
    1509      232946 :   pari_sp av = avma;
    1510      232946 :   return gerepileupto(av, gnorml2(RgC_gtofp(x, prec)));
    1511             : }
    1512             : INLINE GEN
    1513        7262 : RgM_fpnorml2(GEN x, long prec)
    1514             : {
    1515        7262 :   pari_sp av = avma;
    1516        7262 :   return gerepileupto(av, gnorml2(RgM_gtofp(x, prec)));
    1517             : }
    1518             : 
    1519             : /* y a t_REAL */
    1520             : INLINE void
    1521      216244 : affgr(GEN x, GEN y)
    1522             : {
    1523             :   pari_sp av;
    1524      216244 :   switch(typ(x)) {
    1525        5992 :     case t_INT:  affir(x,y); break;
    1526      210252 :     case t_REAL: affrr(x,y); break;
    1527           0 :     case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
    1528           0 :     case t_QUAD: av = avma; affgr(quadtofp(x,realprec(y)), y); set_avma(av); break;
    1529           0 :     default: pari_err_TYPE2("=",x,y);
    1530             :   }
    1531      216244 : }
    1532             : 
    1533             : INLINE GEN
    1534      243064 : affc_fixlg(GEN x, GEN res)
    1535             : {
    1536      243064 :   if (typ(x) == t_COMPLEX)
    1537             :   {
    1538      210658 :     affrr_fixlg(gel(x,1), gel(res,1));
    1539      210658 :     affrr_fixlg(gel(x,2), gel(res,2));
    1540             :   }
    1541             :   else
    1542             :   {
    1543       32406 :     set_avma((pari_sp)(res+3));
    1544       32405 :     res = cgetr(realprec(gel(res,1)));
    1545       32408 :     affrr_fixlg(x, res);
    1546             :   }
    1547      243069 :   return res;
    1548             : }
    1549             : 
    1550             : INLINE GEN
    1551           0 : trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
    1552             : 
    1553             : /*******************************************************************/
    1554             : /*                                                                 */
    1555             : /*                          LENGTH CONVERSIONS                     */
    1556             : /*                                                                 */
    1557             : /*******************************************************************/
    1558             : INLINE long
    1559       36018 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
    1560             : INLINE long
    1561       27749 : ndec2prec(long x) { return 2 + ndec2nlong(x); }
    1562             : INLINE long
    1563        8269 : ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
    1564             : /* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
    1565             :  * to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
    1566             :  *   addl, shrl instead of subl, sarl, addl */
    1567             : INLINE long
    1568     6578337 : nbits2nlong(long x) {
    1569     6578337 :   return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1570             : }
    1571             : 
    1572             : INLINE long
    1573   339223865 : nbits2extraprec(long x) {
    1574   339223865 :   return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1575             : }
    1576             : 
    1577             : /* Fast implementation of 2 + nbits2nlong(x) */
    1578             : INLINE long
    1579   122559653 : nbits2prec(long x) {
    1580   122559653 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1581             : }
    1582             : INLINE long
    1583   182513680 : nbits2lg(long x) {
    1584   182513680 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1585             : }
    1586             : /* ceil(x / sizeof(long)) */
    1587             : INLINE long
    1588    16517028 : nchar2nlong(long x) {
    1589    16517028 :   return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
    1590             : }
    1591             : INLINE long
    1592  1472820931 : prec2nbits(long x) { return (x-2) * BITS_IN_LONG; }
    1593             : INLINE double
    1594     2865365 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
    1595             : INLINE double
    1596      272072 : prec2nbits_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
    1597             : INLINE long
    1598    52102323 : bit_prec(GEN x) { return prec2nbits(realprec(x)); }
    1599             : INLINE long
    1600  1302440925 : bit_accuracy(long x) { return prec2nbits(x); }
    1601             : INLINE long
    1602       12553 : prec2ndec(long x) { return (long)prec2nbits_mul(x, LOG10_2); }
    1603             : INLINE long
    1604         186 : nbits2ndec(long x) { return (long)(x * LOG10_2); }
    1605             : INLINE long
    1606        3315 : precdbl(long x) {return (x - 1) << 1;}
    1607             : INLINE long
    1608  3965170943 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
    1609             : INLINE long
    1610  3914649425 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
    1611             : 
    1612             : /*********************************************************************/
    1613             : /**                                                                 **/
    1614             : /**                      OPERATIONS MODULO m                        **/
    1615             : /**                                                                 **/
    1616             : /*********************************************************************/
    1617             : /* Assume m > 0, more efficient if 0 <= a, b < m */
    1618             : 
    1619             : INLINE GEN
    1620    36924486 : Fp_red(GEN a, GEN m) { return modii(a, m); }
    1621             : INLINE GEN
    1622    84859568 : Fp_add(GEN a, GEN b, GEN m)
    1623             : {
    1624    84859568 :   pari_sp av=avma;
    1625    84859568 :   GEN p = addii(a,b);
    1626    83163068 :   long s = signe(p);
    1627    83163068 :   if (!s) return p; /* = gen_0 */
    1628    72451187 :   if (s > 0) /* general case */
    1629             :   {
    1630    72456755 :     GEN t = subii(p, m);
    1631    72179465 :     s = signe(t);
    1632    72179465 :     if (!s) return gc_const(av, gen_0);
    1633    64782955 :     if (s < 0) return gc_const((pari_sp)p, p);
    1634    29050850 :     if (cmpii(t, m) < 0) return gerepileuptoint(av, t); /* general case ! */
    1635     1069670 :     p = remii(t, m);
    1636             :   }
    1637             :   else
    1638      226517 :     p = modii(p, m);
    1639     1296187 :   return gerepileuptoint(av, p);
    1640             : }
    1641             : INLINE GEN
    1642    65431566 : Fp_sub(GEN a, GEN b, GEN m)
    1643             : {
    1644    65431566 :   pari_sp av=avma;
    1645    65431566 :   GEN p = subii(a,b);
    1646    62911534 :   long s = signe(p);
    1647    62911534 :   if (!s) return p; /* = gen_0 */
    1648    55247935 :   if (s > 0)
    1649             :   {
    1650    25407229 :     if (cmpii(p, m) < 0) return p; /* general case ! */
    1651     1047631 :     p = remii(p, m);
    1652             :   }
    1653             :   else
    1654             :   {
    1655    29840706 :     GEN t = addii(p, m);
    1656    31612607 :     if (!s) return gc_const(av, gen_0);
    1657    31668821 :     if (s > 0) return gerepileuptoint(av, t); /* general case ! */
    1658    31668821 :     p = modii(t, m);
    1659             :   }
    1660    33045052 :   return gerepileuptoint(av, p);
    1661             : }
    1662             : INLINE GEN
    1663    14244741 : Fp_neg(GEN b, GEN m)
    1664             : {
    1665    14244741 :   pari_sp av = avma;
    1666    14244741 :   long s = signe(b);
    1667             :   GEN p;
    1668    14244741 :   if (!s) return gen_0;
    1669    11057929 :   if (s > 0)
    1670             :   {
    1671    10866642 :     p = subii(m, b);
    1672    10863693 :     if (signe(p) >= 0) return p; /* general case ! */
    1673      131108 :     p = modii(p, m);
    1674             :   } else
    1675      192494 :     p = remii(negi(b), m);
    1676      323656 :   return gerepileuptoint(av, p);
    1677             : }
    1678             : 
    1679             : INLINE GEN
    1680       63490 : Fp_halve(GEN a, GEN p)
    1681             : {
    1682       63490 :   if (mpodd(a)) a = addii(a,p);
    1683       63490 :   return shifti(a,-1);
    1684             : }
    1685             : 
    1686             : /* assume 0 <= u < p and ps2 = p>>1 */
    1687             : INLINE GEN
    1688    39054496 : Fp_center(GEN u, GEN p, GEN ps2)
    1689    39054496 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
    1690             : /* same without copy */
    1691             : INLINE GEN
    1692     4986666 : Fp_center_i(GEN u, GEN p, GEN ps2)
    1693     4986666 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
    1694             : 
    1695             : /* x + y*z mod p */
    1696             : INLINE GEN
    1697     5105027 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
    1698             : {
    1699             :   pari_sp av;
    1700     5105027 :   if (!signe(y) || !signe(z)) return Fp_red(x, p);
    1701     4826918 :   if (!signe(x)) return Fp_mul(z,y, p);
    1702     4516972 :   av = avma;
    1703     4516972 :   return gerepileuptoint(av, modii(addii(x, mulii(y,z)), p));
    1704             : }
    1705             : 
    1706             : INLINE GEN
    1707    94543397 : Fp_mul(GEN a, GEN b, GEN m)
    1708             : {
    1709    94543397 :   pari_sp av=avma;
    1710             :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1711    94543397 :   (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
    1712    94525325 :   p = mulii(a,b);
    1713    94627755 :   set_avma(av); return modii(p,m);
    1714             : }
    1715             : INLINE GEN
    1716    33770114 : Fp_sqr(GEN a, GEN m)
    1717             : {
    1718    33770114 :   pari_sp av=avma;
    1719             :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1720    33770114 :   (void)new_chunk((lg(a)+lg(m))<<1);
    1721    33462700 :   p = sqri(a);
    1722    30751066 :   set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
    1723             : }
    1724             : INLINE GEN
    1725    50870628 : Fp_mulu(GEN a, ulong b, GEN m)
    1726             : {
    1727    50870628 :   long l = lgefint(m);
    1728    50870628 :   if (l == 3)
    1729             :   {
    1730    29609005 :     ulong mm = m[2];
    1731    29609005 :     return utoi( Fl_mul(umodiu(a, mm), b, mm) );
    1732             :   } else {
    1733    21261623 :     pari_sp av = avma;
    1734             :     GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1735    21261623 :     (void)new_chunk(lg(a)+1+(l<<1));
    1736    21178474 :     p = muliu(a,b);
    1737    19363841 :     set_avma(av); return modii(p,m);
    1738             :   }
    1739             : }
    1740             : INLINE GEN
    1741       17290 : Fp_muls(GEN a, long b, GEN m)
    1742             : {
    1743       17290 :   long l = lgefint(m);
    1744       17290 :   if (l == 3)
    1745             :   {
    1746        3058 :     ulong mm = m[2];
    1747        3058 :     if (b < 0)
    1748             :     {
    1749        3058 :       ulong t = Fl_mul(umodiu(a, mm), -b, mm);
    1750        3058 :       return t? utoipos(mm - t): gen_0;
    1751             :     }
    1752             :     else
    1753           0 :       return utoi( Fl_mul(umodiu(a, mm), b, mm) );
    1754             :   } else {
    1755       14232 :     pari_sp av = avma;
    1756             :     GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1757       14232 :     (void)new_chunk(lg(a)+1+(l<<1));
    1758       14232 :     p = mulis(a,b);
    1759       14232 :     set_avma(av); return modii(p,m);
    1760             :   }
    1761             : }
    1762             : 
    1763             : INLINE GEN
    1764    12677097 : Fp_inv(GEN a, GEN m)
    1765             : {
    1766             :   GEN res;
    1767    12677097 :   if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
    1768    12676579 :   return res;
    1769             : }
    1770             : INLINE GEN
    1771      333616 : Fp_invsafe(GEN a, GEN m)
    1772             : {
    1773             :   GEN res;
    1774      333616 :   if (! invmod(a,m,&res)) return NULL;
    1775      333616 :   return res;
    1776             : }
    1777             : INLINE GEN
    1778     1500046 : Fp_div(GEN a, GEN b, GEN m)
    1779             : {
    1780     1500046 :   pari_sp av = avma;
    1781             :   GEN p;
    1782     1500046 :   if (lgefint(b) == 3)
    1783             :   {
    1784      527058 :     a = Fp_divu(a, b[2], m);
    1785      527058 :     if (signe(b) < 0) a = Fp_neg(a, m);
    1786      527058 :     return a;
    1787             :   }
    1788             :   /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1789      972988 :   (void)new_chunk(lg(a)+(lg(m)<<1));
    1790      972988 :   p = mulii(a, Fp_inv(b,m));
    1791      972988 :   set_avma(av); return modii(p,m);
    1792             : }
    1793             : INLINE GEN
    1794     1690781 : Fp_divu(GEN x, ulong a, GEN p)
    1795             : {
    1796     1690781 :   pari_sp av = avma;
    1797             :   ulong b;
    1798     1690781 :   if (lgefint(p) == 3)
    1799             :   {
    1800     1394447 :     ulong pp = p[2], xp = umodiu(x, pp);
    1801     1394447 :     return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
    1802             :   }
    1803      296334 :   x = Fp_red(x, p);
    1804      296334 :   b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
    1805      296336 :   return gerepileuptoint(av, diviuexact(addmuliu(x, p, b), a));
    1806             : }
    1807             : 
    1808             : INLINE GEN
    1809     1032255 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
    1810             : 
    1811             : INLINE GEN
    1812     1748325 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1813             : 
    1814             : INLINE long
    1815     2251480 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
    1816             : 
    1817             : INLINE long
    1818     1899771 : get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
    1819             : 
    1820             : INLINE GEN
    1821         399 : get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1822             : 
    1823             : INLINE long
    1824      376152 : get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1825             : 
    1826             : INLINE long
    1827      201446 : get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1828             : 
    1829             : INLINE GEN
    1830     8228559 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1831             : 
    1832             : INLINE long
    1833    21745856 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
    1834             : 
    1835             : INLINE long
    1836    32582818 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1837             : 
    1838             : INLINE GEN
    1839        5891 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1840             : 
    1841             : INLINE long
    1842      231216 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1843             : 
    1844             : INLINE long
    1845      287008 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1846             : 
    1847             : INLINE GEN
    1848     1622469 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1849             : 
    1850             : INLINE long
    1851     4080573 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1852             : 
    1853             : INLINE long
    1854     3030365 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1855             : 
    1856             : INLINE GEN
    1857      146604 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1858             : 
    1859             : INLINE long
    1860       34892 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1861             : 
    1862             : INLINE long
    1863        2259 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1864             : 
    1865             : /*******************************************************************/
    1866             : /*                                                                 */
    1867             : /*                        ADDMULII / SUBMULII                      */
    1868             : /*                                                                 */
    1869             : /*******************************************************************/
    1870             : /* x - y*z */
    1871             : INLINE GEN
    1872      804693 : submulii(GEN x, GEN y, GEN z)
    1873             : {
    1874      804693 :   long lx = lgefint(x), ly, lz;
    1875             :   pari_sp av;
    1876             :   GEN t;
    1877      804693 :   if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
    1878      698805 :   ly = lgefint(y);
    1879      698805 :   if (ly == 2) return icopy(x);
    1880      559230 :   lz = lgefint(z);
    1881      559230 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1882      559230 :   t = mulii(z, y);
    1883      559230 :   set_avma(av); return subii(x,t);
    1884             : }
    1885             : /* y*z - x */
    1886             : INLINE GEN
    1887     1350654 : mulsubii(GEN y, GEN z, GEN x)
    1888             : {
    1889     1350654 :   long lx = lgefint(x), ly, lz;
    1890             :   pari_sp av;
    1891             :   GEN t;
    1892     1350654 :   if (lx == 2) return mulii(z,y);
    1893      843331 :   ly = lgefint(y);
    1894      843331 :   if (ly == 2) return negi(x);
    1895      772689 :   lz = lgefint(z);
    1896      772689 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1897      772689 :   t = mulii(z, y);
    1898      772689 :   set_avma(av); return subii(t,x);
    1899             : }
    1900             : 
    1901             : /* x - u*y */
    1902             : INLINE GEN
    1903        7595 : submuliu(GEN x, GEN y, ulong u)
    1904             : {
    1905             :   pari_sp av;
    1906        7595 :   long ly = lgefint(y);
    1907        7595 :   if (ly == 2) return icopy(x);
    1908        7595 :   av = avma;
    1909        7595 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1910        7595 :   y = mului(u,y);
    1911        7595 :   set_avma(av); return subii(x, y);
    1912             : }
    1913             : /* x + u*y */
    1914             : INLINE GEN
    1915      304292 : addmuliu(GEN x, GEN y, ulong u)
    1916             : {
    1917             :   pari_sp av;
    1918      304292 :   long ly = lgefint(y);
    1919      304292 :   if (ly == 2) return icopy(x);
    1920      304292 :   av = avma;
    1921      304292 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1922      304294 :   y = mului(u,y);
    1923      304291 :   set_avma(av); return addii(x, y);
    1924             : }
    1925             : /* x - u*y */
    1926             : INLINE GEN
    1927   159633838 : submuliu_inplace(GEN x, GEN y, ulong u)
    1928             : {
    1929             :   pari_sp av;
    1930   159633838 :   long ly = lgefint(y);
    1931   159633838 :   if (ly == 2) return x;
    1932   132937300 :   av = avma;
    1933   132937300 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1934   132937314 :   y = mului(u,y);
    1935   132937250 :   set_avma(av); return subii(x, y);
    1936             : }
    1937             : /* x + u*y */
    1938             : INLINE GEN
    1939   157392172 : addmuliu_inplace(GEN x, GEN y, ulong u)
    1940             : {
    1941             :   pari_sp av;
    1942   157392172 :   long ly = lgefint(y);
    1943   157392172 :   if (ly == 2) return x;
    1944   132334583 :   av = avma;
    1945   132334583 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1946   132334594 :   y = mului(u,y);
    1947   132334523 :   set_avma(av); return addii(x, y);
    1948             : }
    1949             : /* ux + vy */
    1950             : INLINE GEN
    1951    23988403 : lincombii(GEN u, GEN v, GEN x, GEN y)
    1952             : {
    1953    23988403 :   long lx = lgefint(x), ly;
    1954             :   GEN p1, p2;
    1955             :   pari_sp av;
    1956    23988403 :   if (lx == 2) return mulii(v,y);
    1957    16729921 :   ly = lgefint(y);
    1958    16729921 :   if (ly == 2) return mulii(u,x);
    1959    15488842 :   av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
    1960    15489702 :   p1 = mulii(u,x);
    1961    15489542 :   p2 = mulii(v,y);
    1962    15489549 :   set_avma(av); return addii(p1,p2);
    1963             : }
    1964             : 
    1965             : /*******************************************************************/
    1966             : /*                                                                 */
    1967             : /*                          GEN SUBTYPES                           */
    1968             : /*                                                                 */
    1969             : /*******************************************************************/
    1970             : 
    1971             : INLINE int
    1972  1443738030 : is_const_t(long t) { return (t < t_POLMOD); }
    1973             : INLINE int
    1974        6819 : is_extscalar_t(long t) { return (t <= t_POL); }
    1975             : INLINE int
    1976     1240612 : is_intreal_t(long t) { return (t <= t_REAL); }
    1977             : INLINE int
    1978   421473650 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
    1979             : INLINE int
    1980    71732160 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
    1981             : INLINE int
    1982     1565400 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
    1983             : INLINE int
    1984     2817880 : is_qfb_t(long t) { return (t == t_QFI || t == t_QFR); }
    1985             : INLINE int
    1986      803761 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
    1987             : INLINE int
    1988  3919812715 : is_recursive_t(long t) { return lontyp[t]; }
    1989             : INLINE int
    1990   177471122 : is_scalar_t(long t) { return (t < t_POL); }
    1991             : INLINE int
    1992     1035630 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
    1993             : 
    1994             : /*******************************************************************/
    1995             : /*                                                                 */
    1996             : /*                         TRANSCENDENTAL                          */
    1997             : /*                                                                 */
    1998             : /*******************************************************************/
    1999             : INLINE GEN
    2000    22608089 : sqrtr(GEN x) {
    2001    22608089 :   long s = signe(x);
    2002    22608089 :   if (s == 0) return real_0_bit(expo(x) >> 1);
    2003    22574846 :   if (s >= 0) return sqrtr_abs(x);
    2004       42198 :   retmkcomplex(gen_0, sqrtr_abs(x));
    2005             : }
    2006             : INLINE GEN
    2007           0 : cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
    2008             : INLINE GEN
    2009           0 : cbrtr(GEN x) {
    2010           0 :   long s = signe(x);
    2011             :   GEN r;
    2012           0 :   if (s == 0) return real_0_bit(expo(x) / 3);
    2013           0 :   r = cbrtr_abs(x);
    2014           0 :   if (s < 0) togglesign(r);
    2015           0 :   return r;
    2016             : }
    2017             : INLINE GEN
    2018      491106 : sqrtnr(GEN x, long n) {
    2019      491106 :   long s = signe(x);
    2020             :   GEN r;
    2021      491106 :   if (s == 0) return real_0_bit(expo(x) / n);
    2022      491106 :   r = sqrtnr_abs(x, n);
    2023      491106 :   if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
    2024      491106 :   return r;
    2025             : }
    2026             : INLINE long
    2027      260220 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
    2028             : INLINE ulong
    2029     2007176 : ulogint(ulong B, ulong y)
    2030             : {
    2031             :   ulong r;
    2032             :   long e;
    2033     2007176 :   if (y == 2) return expu(B);
    2034     1988262 :   r = y;
    2035     1988262 :   for (e=1;; e++)
    2036             :   { /* here, r = y^e, r2 = y^(e-1) */
    2037     6413428 :     if (r >= B) return r == B? e: e-1;
    2038     4425161 :     r = umuluu_or_0(y, r);
    2039     4425164 :     if (!r) return e;
    2040             :   }
    2041             : }
    2042             : 
    2043             : /*******************************************************************/
    2044             : /*                                                                 */
    2045             : /*                         MISCELLANEOUS                           */
    2046             : /*                                                                 */
    2047             : /*******************************************************************/
    2048     1240535 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
    2049   768288114 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
    2050    10784640 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
    2051     1700138 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
    2052   765061220 : INLINE int equali1(GEN n)
    2053   765061220 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
    2054    61430643 : INLINE int equalim1(GEN n)
    2055    61430643 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
    2056             : /* works only for POSITIVE integers */
    2057  1221476616 : INLINE int is_pm1(GEN n)
    2058  1221476616 : { return lgefint(n) == 3 && n[2] == 1; }
    2059   271638535 : INLINE int is_bigint(GEN n)
    2060   271638535 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
    2061             : 
    2062  2108472630 : INLINE int odd(long x) { return x & 1; }
    2063    29037986 : INLINE int both_odd(long x, long y) { return x & y & 1; }
    2064             : 
    2065             : INLINE int
    2066  3595830975 : isonstack(GEN x)
    2067  3595830975 : { return ((pari_sp)x >= pari_mainstack->bot
    2068  3595830975 :        && (pari_sp)x <  pari_mainstack->top); }
    2069             : 
    2070             : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
    2071             : INLINE double
    2072    24599796 : dbllog2r(GEN x)
    2073    24599796 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
    2074             : 
    2075             : INLINE GEN
    2076      457104 : mul_content(GEN cx, GEN cy)
    2077             : {
    2078      457104 :   if (!cx) return cy;
    2079      154935 :   if (!cy) return cx;
    2080      103648 :   return gmul(cx,cy);
    2081             : }
    2082             : INLINE GEN
    2083       11011 : inv_content(GEN c) { return c? ginv(c): NULL; }
    2084             : INLINE GEN
    2085     2829512 : mul_denom(GEN dx, GEN dy)
    2086             : {
    2087     2829512 :   if (!dx) return dy;
    2088     1259983 :   if (!dy) return dx;
    2089      406695 :   return mulii(dx,dy);
    2090             : }
    2091             : 
    2092             : /* POLYNOMIALS */
    2093             : INLINE GEN
    2094      834200 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
    2095             : INLINE GEN
    2096    50868166 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
    2097             : INLINE ulong
    2098      503789 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
    2099             : INLINE ulong
    2100       15174 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
    2101             : INLINE long
    2102  2654719099 : degpol(GEN x) { return lg(x)-3; }
    2103             : INLINE long
    2104  1441277119 : lgpol(GEN x) { return lg(x)-2; }
    2105             : INLINE long
    2106   138246257 : lgcols(GEN x) { return lg(gel(x,1)); }
    2107             : INLINE long
    2108    38815093 : nbrows(GEN x) { return lg(gel(x,1))-1; }
    2109             : INLINE GEN
    2110           0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
    2111             : 
    2112             : INLINE GEN
    2113      643952 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
    2114             : INLINE GEN
    2115      295345 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
    2116             : 
    2117             : INLINE GEN
    2118    92059395 : RgX_copy(GEN x)
    2119             : {
    2120             :   long lx, i;
    2121    92059395 :   GEN y = cgetg_copy(x, &lx); y[1] = x[1];
    2122   335914996 :   for (i = 2; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    2123    92059398 :   return y;
    2124             : }
    2125             : /* have to use ulong to avoid silly warnings from gcc "assuming signed
    2126             :  * overflow does not occur" */
    2127             : INLINE GEN
    2128      851669 : RgX_coeff(GEN x, long n)
    2129             : {
    2130      851669 :   ulong l = lg(x);
    2131      851669 :   return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
    2132             : }
    2133             : INLINE GEN
    2134      369316 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
    2135             : INLINE GEN
    2136     6812185 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
    2137             : INLINE GEN
    2138        1792 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
    2139             : INLINE GEN
    2140      104090 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
    2141             : INLINE GEN
    2142     1085693 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
    2143             : INLINE GEN
    2144     5534757 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
    2145             : INLINE GEN
    2146      826436 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
    2147             : INLINE GEN
    2148           0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
    2149             : INLINE GEN
    2150    53631708 : pol0_Flx(long sv) { return mkvecsmall(sv); }
    2151             : INLINE GEN
    2152    16777701 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
    2153             : INLINE GEN
    2154    18614964 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
    2155             : INLINE GEN
    2156           0 : zero_zx(long sv) { return zero_Flx(sv); }
    2157             : INLINE GEN
    2158           0 : polx_zx(long sv) { return polx_Flx(sv); }
    2159             : INLINE GEN
    2160           0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
    2161             : INLINE GEN
    2162           0 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
    2163             : INLINE GEN
    2164        1341 : zero_F2x(long sv) { return zero_Flx(sv); }
    2165             : INLINE GEN
    2166    11020821 : pol0_F2x(long sv) { return pol0_Flx(sv); }
    2167             : INLINE GEN
    2168     2803662 : pol1_F2x(long sv) { return pol1_Flx(sv); }
    2169             : INLINE GEN
    2170      690772 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
    2171             : INLINE int
    2172      518500 : F2x_equal1(GEN x) { return Flx_equal1(x); }
    2173             : INLINE int
    2174     2967631 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
    2175             : INLINE GEN
    2176    53476309 : F2x_copy(GEN x) { return leafcopy(x); }
    2177             : INLINE GEN
    2178        3360 : F2v_copy(GEN x) { return leafcopy(x); }
    2179             : INLINE GEN
    2180     2277171 : Flv_copy(GEN x) { return leafcopy(x); }
    2181             : INLINE GEN
    2182    93048751 : Flx_copy(GEN x) { return leafcopy(x); }
    2183             : INLINE GEN
    2184     1887683 : vecsmall_copy(GEN x) { return leafcopy(x); }
    2185             : INLINE int
    2186     4509160 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
    2187             : INLINE int
    2188       18616 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
    2189             : INLINE int
    2190     6514462 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
    2191             : 
    2192             : INLINE GEN
    2193    60281487 : ZX_renormalize(GEN x, long lx)    { return ZXX_renormalize(x,lx); }
    2194             : INLINE GEN
    2195    72572162 : FpX_renormalize(GEN x, long lx)   { return ZXX_renormalize(x,lx); }
    2196             : INLINE GEN
    2197      674459 : FpXX_renormalize(GEN x, long lx)  { return ZXX_renormalize(x,lx); }
    2198             : INLINE GEN
    2199     1347623 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
    2200             : INLINE GEN
    2201   107115788 : F2x_renormalize(GEN x, long lx)   { return Flx_renormalize(x,lx); }
    2202             : INLINE GEN
    2203       32620 : F2v_to_F2x(GEN x, long sv) {
    2204       32620 :   GEN y = leafcopy(x);
    2205       32620 :   y[1] = sv; F2x_renormalize(y, lg(y)); return y;
    2206             : }
    2207             : 
    2208             : INLINE long
    2209         126 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
    2210             : 
    2211             : INLINE long
    2212        3927 : gval(GEN x, long v)
    2213        3927 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
    2214             : 
    2215             : INLINE void
    2216      473700 : RgX_shift_inplace_init(long v)
    2217      473700 : { if (v) (void)cgetg(v, t_VECSMALL); }
    2218             : /* shift polynomial in place. assume v free cells have been left before x */
    2219             : INLINE GEN
    2220      473700 : RgX_shift_inplace(GEN x, long v)
    2221             : {
    2222             :   long i, lx;
    2223             :   GEN z;
    2224      473700 :   if (!v) return x;
    2225      253227 :   lx = lg(x);
    2226      253227 :   if (lx == 2) return x;
    2227      253227 :   z = x + lx;
    2228             :   /* stackdummy's from normalizepol */
    2229      253227 :   while (lg(z) != v) z += lg(z);
    2230      253227 :   z += v;
    2231     4656826 :   for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
    2232      676007 :   for (i = 0;  i < v; i++) gel(--z,0) = gen_0;
    2233      253227 :   z -= 2;
    2234      253227 :   z[1] = x[1];
    2235      253227 :   z[0] = evaltyp(t_POL) | evallg(lx+v);
    2236      253227 :   stackdummy((pari_sp)z, (pari_sp)x); return z;
    2237             : }
    2238             : 
    2239             : 
    2240             : /* LINEAR ALGEBRA */
    2241             : INLINE GEN
    2242      138369 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
    2243             : INLINE GEN
    2244     5327184 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
    2245             : INLINE GEN
    2246     3306476 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
    2247             : INLINE GEN
    2248           0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
    2249             : INLINE GEN
    2250           0 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
    2251             : INLINE GEN
    2252           0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
    2253             : INLINE GEN
    2254           0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
    2255             : INLINE GEN
    2256    17338880 : zero_zv(long x) { return zero_Flv(x); }
    2257             : INLINE GEN
    2258         238 : zm_transpose(GEN x) { return Flm_transpose(x); }
    2259             : INLINE GEN
    2260           0 : zm_copy(GEN x) { return Flm_copy(x); }
    2261             : INLINE GEN
    2262     1902208 : zv_copy(GEN x) { return Flv_copy(x); }
    2263             : INLINE GEN
    2264           0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
    2265             : 
    2266             : INLINE GEN
    2267     1998728 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfremdiv(x,y,NULL); }
    2268             : INLINE GEN
    2269       41950 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
    2270             : INLINE GEN
    2271     2704837 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
    2272             : INLINE void
    2273     6069596 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
    2274             : INLINE GEN
    2275    10686573 : RgM_shallowcopy(GEN x)
    2276             : {
    2277             :   long l;
    2278    10686573 :   GEN y = cgetg_copy(x, &l);
    2279    49814901 :   while (--l > 0) gel(y,l) = leafcopy(gel(x,l));
    2280    10686551 :   return y;
    2281             : }
    2282             : INLINE GEN
    2283       20452 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
    2284             : 
    2285             : INLINE GEN
    2286     1585526 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
    2287             : 
    2288             : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
    2289             :  * x,y are ZV of the same length */
    2290             : INLINE int
    2291       23198 : ZV_dvd(GEN x, GEN y)
    2292             : {
    2293       23198 :   long i, l = lg(x);
    2294       36666 :   for (i=1; i < l; i++)
    2295       29533 :     if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
    2296        7133 :   return 1;
    2297             : }
    2298             : 
    2299             : /* Fq */
    2300             : INLINE GEN
    2301     2481376 : Fq_red(GEN x, GEN T, GEN p)
    2302     2481376 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
    2303             : INLINE GEN
    2304       99665 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
    2305             : {
    2306             :   (void) p;
    2307       99665 :   return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
    2308             : }
    2309             : INLINE GEN
    2310         756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
    2311             : 
    2312             : INLINE GEN
    2313         805 : gener_Fq_local(GEN T, GEN p, GEN L)
    2314           0 : { return T? gener_FpXQ_local(T,p, L)
    2315         805 :           : pgener_Fp_local(p, L); }
    2316             : 
    2317             : /* FpXQX */
    2318             : INLINE GEN
    2319        4417 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
    2320             : INLINE GEN
    2321       38275 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
    2322             : INLINE GEN
    2323       33334 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
    2324             : 
    2325             : INLINE GEN
    2326       20006 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    2327       20006 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
    2328             : 
    2329             : /* FqX */
    2330             : INLINE GEN
    2331       25067 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
    2332             : INLINE GEN
    2333      105343 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2334             : INLINE GEN
    2335       24313 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
    2336             : INLINE GEN
    2337       66458 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2338             : INLINE GEN
    2339      204076 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
    2340      204076 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
    2341             : INLINE GEN
    2342       87335 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
    2343       87335 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
    2344             : INLINE GEN
    2345      160066 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
    2346      160066 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
    2347             : INLINE GEN
    2348       17594 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
    2349       17594 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
    2350             : INLINE GEN
    2351       69678 : FqX_sqr(GEN x, GEN T, GEN p)
    2352       69678 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
    2353             : INLINE GEN
    2354        1281 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
    2355        1281 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
    2356             : INLINE GEN
    2357       18774 : FqX_halve(GEN x, GEN T, GEN p)
    2358       18774 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
    2359             : INLINE GEN
    2360        6027 : FqX_div(GEN x, GEN y, GEN T, GEN p)
    2361        6027 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
    2362             : INLINE GEN
    2363        6081 : FqX_get_red(GEN S, GEN T, GEN p)
    2364        6081 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
    2365             : INLINE GEN
    2366       31086 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
    2367       31086 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
    2368             : INLINE GEN
    2369           0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2370           0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
    2371             : INLINE GEN
    2372        5936 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2373        5936 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
    2374             : INLINE GEN
    2375           0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
    2376           0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
    2377             : INLINE GEN
    2378      260001 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
    2379      260001 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
    2380             : INLINE GEN
    2381      100896 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
    2382      100896 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
    2383             : INLINE GEN
    2384        3514 : FqX_normalize(GEN z, GEN T, GEN p)
    2385        3514 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
    2386             : INLINE GEN
    2387      291893 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
    2388             : INLINE GEN
    2389           0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
    2390             : INLINE GEN
    2391       76048 : FqX_factor(GEN f, GEN T, GEN p)
    2392       76048 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
    2393             : INLINE GEN
    2394           7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
    2395           7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
    2396             : INLINE GEN
    2397           7 : FqX_ddf(GEN f, GEN T, GEN p)
    2398           7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
    2399             : INLINE GEN
    2400        1862 : FqX_degfact(GEN f, GEN T, GEN p)
    2401        1862 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
    2402             : INLINE GEN
    2403        7147 : FqX_roots(GEN f, GEN T, GEN p)
    2404        7147 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
    2405             : INLINE GEN
    2406         175 : FqX_to_mod(GEN f, GEN T, GEN p)
    2407         175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
    2408             : 
    2409             : /*FqXQ*/
    2410             : INLINE GEN
    2411           0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2412           0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2413             : INLINE GEN
    2414           0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2415           0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2416             : INLINE GEN
    2417           0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
    2418           0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
    2419             : INLINE GEN
    2420           0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
    2421           0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
    2422             : INLINE GEN
    2423           0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
    2424           0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
    2425             : INLINE GEN
    2426       22320 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
    2427       22320 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
    2428             : INLINE GEN
    2429           0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
    2430           0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
    2431             : INLINE GEN
    2432           0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    2433           0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
    2434             : 
    2435             : /*FqXn*/
    2436             : INLINE GEN
    2437        6610 : FqXn_expint(GEN x, long n, GEN T, GEN p)
    2438        6610 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
    2439             : INLINE GEN
    2440           0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
    2441           0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
    2442             : INLINE GEN
    2443        6916 : FqXn_inv(GEN x, long n, GEN T, GEN p)
    2444        6916 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
    2445             : INLINE GEN
    2446      254926 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
    2447      254926 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
    2448             : INLINE GEN
    2449           0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
    2450           0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
    2451             : 
    2452             : /*FpXQ*/
    2453             : INLINE GEN
    2454           0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2455           0 : { (void)T; return FpX_add(x,y,p); }
    2456             : INLINE GEN
    2457           0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2458           0 : { (void)T; return FpX_sub(x,y,p); }
    2459             : 
    2460             : /*Flxq*/
    2461             : INLINE GEN
    2462           0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2463           0 : { (void)T; return Flx_add(x,y,p); }
    2464             : INLINE GEN
    2465           0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2466           0 : { (void)T; return Flx_sub(x,y,p); }
    2467             : 
    2468             : /* F2x */
    2469             : 
    2470             : INLINE ulong
    2471    53831037 : F2x_coeff(GEN x,long v)
    2472             : {
    2473    53831037 :    ulong u=(ulong)x[2+divsBIL(v)];
    2474    53829277 :    return (u>>remsBIL(v))&1UL;
    2475             : }
    2476             : 
    2477             : INLINE void
    2478     5078203 : F2x_clear(GEN x,long v)
    2479             : {
    2480     5078203 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2481     5078202 :    *u&=~(1UL<<remsBIL(v));
    2482     5078202 : }
    2483             : 
    2484             : INLINE void
    2485    32761035 : F2x_set(GEN x,long v)
    2486             : {
    2487    32761035 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2488    32739687 :    *u|=1UL<<remsBIL(v);
    2489    32730923 : }
    2490             : 
    2491             : INLINE void
    2492     1506758 : F2x_flip(GEN x,long v)
    2493             : {
    2494     1506758 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2495     1506757 :    *u^=1UL<<remsBIL(v);
    2496     1506760 : }
    2497             : 
    2498             : /* F2v */
    2499             : 
    2500             : INLINE ulong
    2501    49184051 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
    2502             : 
    2503             : INLINE void
    2504     5078202 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
    2505             : 
    2506             : INLINE void
    2507     9031371 : F2v_set(GEN x,long v)   { F2x_set(x,v-1); }
    2508             : 
    2509             : INLINE void
    2510     1506759 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
    2511             : 
    2512             : /* F2m */
    2513             : 
    2514             : INLINE ulong
    2515     4476968 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
    2516             : 
    2517             : INLINE void
    2518           0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
    2519             : 
    2520             : INLINE void
    2521           0 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
    2522             : 
    2523             : INLINE void
    2524     1506759 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
    2525             : 
    2526             : /* ARITHMETIC */
    2527             : INLINE GEN
    2528        2205 : matpascal(long n) { return matqpascal(n, NULL); }
    2529             : INLINE long
    2530     2341956 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
    2531             : INLINE long
    2532          14 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
    2533             : INLINE GEN
    2534     4407615 : sqrti(GEN x) { return sqrtremi(x,NULL); }
    2535             : INLINE GEN
    2536     5965198 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
    2537             : INLINE int
    2538       75620 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
    2539             : INLINE int
    2540       24549 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
    2541             : INLINE GEN
    2542           0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
    2543             : INLINE GEN
    2544           0 : gminsg(long s, GEN y) { return gmings(y,s); }
    2545             : INLINE GEN
    2546    22051891 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
    2547             : INLINE GEN
    2548     1075888 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
    2549             : INLINE GEN
    2550      197749 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
    2551             : 
    2552             : INLINE GEN
    2553     9672258 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
    2554             : INLINE GEN
    2555      258516 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
    2556             : 
    2557             : /* x t_COMPLEX */
    2558             : INLINE GEN
    2559    16177101 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
    2560             : /* q t_QUAD */
    2561             : INLINE GEN
    2562         840 : quadnorm(GEN q)
    2563             : {
    2564         840 :   GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
    2565         840 :   GEN z, u = gel(q,3), v = gel(q,2);
    2566         840 :   if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
    2567             :   {
    2568         651 :     z = signe(b)? mulii(v, addii(u,v)): sqri(v);
    2569         651 :     return addii(z, mulii(c, sqri(u)));
    2570             :   }
    2571             :   else
    2572             :   {
    2573         189 :     z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
    2574         189 :     return gadd(z, gmul(c, gsqr(u)));
    2575             :   }
    2576             : }
    2577             : /* x a t_QUAD, return the attached discriminant */
    2578             : INLINE GEN
    2579        1001 : quad_disc(GEN x)
    2580             : {
    2581        1001 :   GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
    2582        1001 :   if (is_pm1(b)) return subsi(1, c4);
    2583         147 :   togglesign_safe(&c4); return c4;
    2584             : }
    2585             : INLINE GEN
    2586     5633278 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
    2587             : INLINE GEN
    2588     5556558 : qfb_disc(GEN x) { return qfb_disc3(gel(x,1), gel(x,2), gel(x,3)); }
    2589             : 
    2590             : INLINE GEN
    2591     1621066 : sqrfrac(GEN x)
    2592             : {
    2593     1621066 :   GEN z = cgetg(3,t_FRAC);
    2594     1621066 :   gel(z,1) = sqri(gel(x,1));
    2595     1621066 :   gel(z,2) = sqri(gel(x,2)); return z;
    2596             : }
    2597             : 
    2598             : INLINE void
    2599     8934012 : normalize_frac(GEN z) {
    2600     8934012 :   if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
    2601     8934012 : }
    2602             : 
    2603             : INLINE GEN
    2604      353477 : powii(GEN x, GEN n)
    2605             : {
    2606      353477 :   long ln = lgefint(n);
    2607      353477 :   if (ln == 3) {
    2608             :     GEN z;
    2609      348976 :     if (signe(n) > 0) return powiu(x, n[2]);
    2610        8260 :     z = cgetg(3, t_FRAC);
    2611        8260 :     gel(z,1) = gen_1;
    2612        8260 :     gel(z,2) = powiu(x, n[2]);
    2613        8260 :     return z;
    2614             :   }
    2615        4501 :   if (ln == 2) return gen_1; /* rare */
    2616             :   /* should never happen */
    2617           0 :   return powgi(x, n); /* overflow unless x = 0, 1, -1 */
    2618             : }
    2619             : INLINE GEN
    2620        1498 : powIs(long n) {
    2621        1498 :   switch(n & 3)
    2622             :   {
    2623          63 :     case 1: return mkcomplex(gen_0,gen_1);
    2624         385 :     case 2: return gen_m1;
    2625         707 :     case 3: return mkcomplex(gen_0,gen_m1);
    2626             :   }
    2627         343 :   return gen_1;
    2628             : }
    2629             : 
    2630             : /*******************************************************************/
    2631             : /*                                                                 */
    2632             : /*                             ASSIGNMENTS                         */
    2633             : /*                                                                 */
    2634             : /*******************************************************************/
    2635           0 : INLINE void mpexpz(GEN x, GEN z)
    2636           0 : { pari_sp av = avma; gaffect(mpexp(x), z); set_avma(av); }
    2637           0 : INLINE void mplogz(GEN x, GEN z)
    2638           0 : { pari_sp av = avma; gaffect(mplog(x), z); set_avma(av); }
    2639           0 : INLINE void mpcosz(GEN x, GEN z)
    2640           0 : { pari_sp av = avma; gaffect(mpcos(x), z); set_avma(av); }
    2641           0 : INLINE void mpsinz(GEN x, GEN z)
    2642           0 : { pari_sp av = avma; gaffect(mpsin(x), z); set_avma(av); }
    2643           0 : INLINE void gnegz(GEN x, GEN z)
    2644           0 : { pari_sp av = avma; gaffect(gneg(x), z); set_avma(av); }
    2645           0 : INLINE void gabsz(GEN x, long prec, GEN z)
    2646           0 : { pari_sp av = avma; gaffect(gabs(x,prec), z); set_avma(av); }
    2647           0 : INLINE void gaddz(GEN x, GEN y, GEN z)
    2648           0 : { pari_sp av = avma; gaffect(gadd(x,y), z); set_avma(av); }
    2649           0 : INLINE void gsubz(GEN x, GEN y, GEN z)
    2650           0 : { pari_sp av = avma; gaffect(gsub(x,y), z); set_avma(av); }
    2651           0 : INLINE void gmulz(GEN x, GEN y, GEN z)
    2652           0 : { pari_sp av = avma; gaffect(gmul(x,y), z); set_avma(av); }
    2653           0 : INLINE void gdivz(GEN x, GEN y, GEN z)
    2654           0 : { pari_sp av = avma; gaffect(gdiv(x,y), z); set_avma(av); }
    2655           0 : INLINE void gdiventz(GEN x, GEN y, GEN z)
    2656           0 : { pari_sp av = avma; gaffect(gdivent(x,y), z); set_avma(av); }
    2657           0 : INLINE void gmodz(GEN x, GEN y, GEN z)
    2658           0 : { pari_sp av = avma; gaffect(gmod(x,y), z); set_avma(av); }
    2659           0 : INLINE void gmul2nz(GEN x, long s, GEN z)
    2660           0 : { pari_sp av = avma; gaffect(gmul2n(x,s), z); set_avma(av); }
    2661           0 : INLINE void gshiftz(GEN x, long s, GEN z)
    2662           0 : { pari_sp av = avma; gaffect(gshift(x,s), z); set_avma(av); }
    2663             : 
    2664             : /*******************************************************************/
    2665             : /*                                                                 */
    2666             : /*                       ELLIPTIC CURVES                           */
    2667             : /*                                                                 */
    2668             : /*******************************************************************/
    2669     3985056 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
    2670     3098804 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
    2671     3866372 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
    2672     3891715 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
    2673     4708496 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
    2674     3323419 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
    2675     1027618 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
    2676     1633636 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
    2677     1464918 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
    2678     6213416 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
    2679     7451547 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
    2680     9705800 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
    2681     1031380 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
    2682    11473863 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
    2683     1570130 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
    2684      682694 : INLINE GEN ellff_get_a4a6(GEN x)  { return gmael(x, 15, 2); }
    2685        1407 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
    2686         287 : INLINE long ellQp_get_prec(GEN E) { GEN z = ellQp_get_zero(E); return valp(z); }
    2687        1085 : INLINE GEN ellQp_get_p(GEN E) { GEN z = ellQp_get_zero(E); return gel(z,2); }
    2688       37471 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
    2689       13762 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
    2690      701648 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
    2691          56 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
    2692             : 
    2693     4630394 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
    2694    33925086 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
    2695      807996 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
    2696             : 
    2697             : /*******************************************************************/
    2698             : /*                                                                 */
    2699             : /*                    ALGEBRAIC NUMBER THEORY                      */
    2700             : /*                                                                 */
    2701             : /*******************************************************************/
    2702     8431349 : INLINE GEN modpr_get_pr(GEN x)  { return gel(x,3); }
    2703      628922 : INLINE GEN modpr_get_p(GEN x)  { return pr_get_p(modpr_get_pr(x)); }
    2704     2518476 : INLINE GEN modpr_get_T(GEN x)  { return lg(x) == 4? NULL: gel(x,4); }
    2705             : 
    2706    26956970 : INLINE GEN pr_get_p(GEN pr)  { return gel(pr,1); }
    2707     8029767 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
    2708             : /* .[2] instead of itos works: e and f are small positive integers */
    2709     2541755 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
    2710    13309881 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
    2711    10651891 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
    2712             : INLINE int
    2713     3863406 : pr_is_inert(GEN P) { return pr_get_f(P) == lg(pr_get_gen(P))-1; }
    2714             : INLINE GEN
    2715     2131426 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
    2716             : INLINE ulong
    2717       91532 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
    2718             : 
    2719             : /* assume nf a genuine nf */
    2720             : INLINE long
    2721      390043 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
    2722             : INLINE GEN
    2723    37252029 : nf_get_pol(GEN nf) { return gel(nf,1); }
    2724             : INLINE long
    2725    30087324 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
    2726             : INLINE long
    2727     4194530 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
    2728             : INLINE long
    2729        3032 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
    2730             : INLINE GEN
    2731       58711 : nf_get_disc(GEN nf) { return gel(nf,3); }
    2732             : INLINE GEN
    2733      910848 : nf_get_index(GEN nf) { return gel(nf,4); }
    2734             : INLINE GEN
    2735     2673713 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
    2736             : INLINE GEN
    2737     1874945 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
    2738             : INLINE GEN
    2739     2288621 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
    2740             : INLINE GEN
    2741       16121 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
    2742             : INLINE GEN
    2743        2779 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
    2744             : INLINE GEN
    2745        2863 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
    2746             : INLINE GEN
    2747      394559 : nf_get_roots(GEN nf) { return gel(nf,6); }
    2748             : INLINE GEN
    2749          98 : nf_get_zk(GEN nf)
    2750             : {
    2751          98 :   GEN y = gel(nf,7), D = gel(y, 1);
    2752          98 :   if (typ(D) == t_POL) D = gel(D, 2);
    2753          98 :   if (!equali1(D)) y = gdiv(y, D);
    2754          98 :   return y;
    2755             : }
    2756             : INLINE GEN
    2757     1209604 : nf_get_zkprimpart(GEN nf)
    2758             : {
    2759     1209604 :   GEN y = gel(nf,7);
    2760             :   /* test for old format of nf.zk: non normalized */
    2761     1209604 :   if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
    2762     1209605 :   return y;
    2763             : }
    2764             : INLINE GEN
    2765     1211160 : nf_get_zkden(GEN nf)
    2766             : {
    2767     1211160 :   GEN y = gel(nf,7), D = gel(y,1);
    2768     1211160 :   if (typ(D) == t_POL) D = gel(D,2);
    2769             :   /* test for old format of nf.zk: non normalized */
    2770     1211160 :   if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
    2771     1211157 :   return D;
    2772             : }
    2773             : INLINE GEN
    2774     2799228 : nf_get_invzk(GEN nf) { return gel(nf,8); }
    2775             : INLINE void
    2776       23595 : nf_get_sign(GEN nf, long *r1, long *r2)
    2777             : {
    2778       23595 :   GEN x = gel(nf,2);
    2779       23595 :   *r1 = itou(gel(x,1));
    2780       23595 :   *r2 = itou(gel(x,2));
    2781       23595 : }
    2782             : 
    2783             : INLINE GEN
    2784      432115 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
    2785             : INLINE GEN
    2786       98407 : abgrp_get_no(GEN x) { return gel(x,1); }
    2787             : INLINE GEN
    2788     2996055 : abgrp_get_cyc(GEN x) { return gel(x,2); }
    2789             : INLINE GEN
    2790      343460 : abgrp_get_gen(GEN x) { return gel(x,3); }
    2791             : INLINE GEN
    2792     2342902 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
    2793             : INLINE GEN
    2794     1215791 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
    2795             : INLINE GEN
    2796        7294 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
    2797             : INLINE GEN
    2798      915752 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
    2799             : INLINE GEN
    2800      292731 : bnf_get_gen(GEN bnf)  { return abgrp_get_gen(bnf_get_clgp(bnf)); }
    2801             : INLINE GEN
    2802         741 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
    2803             : INLINE GEN
    2804      530003 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
    2805             : INLINE GEN
    2806        5667 : bnf_get_sunits(GEN bnf)
    2807        5667 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
    2808             : INLINE GEN
    2809       39019 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
    2810             : INLINE long
    2811       40495 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
    2812             : INLINE GEN
    2813       38864 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
    2814             : INLINE GEN
    2815        1897 : bnf_get_fu(GEN bnf) {
    2816        1897 :   GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
    2817             :   long i, l;
    2818        1897 :   if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
    2819        1897 :   l = lg(fu)-1; fu = vecslice(fu, 2, l);
    2820        4242 :   for (i = 1; i < l; i++) gel(fu,i) = nf_to_scalar_or_alg(nf, gel(fu,i));
    2821        1897 :   return fu;
    2822             : }
    2823             : 
    2824             : INLINE GEN
    2825      884175 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
    2826             : INLINE GEN
    2827      501605 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
    2828             : INLINE GEN
    2829       23744 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
    2830             : INLINE GEN
    2831      134442 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
    2832             : INLINE GEN
    2833      746906 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
    2834             : INLINE GEN
    2835       84406 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
    2836             : INLINE GEN
    2837      646162 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
    2838             : INLINE GEN
    2839          70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
    2840             : INLINE GEN
    2841       14812 : bnr_get_gen(GEN bnr) {
    2842       14812 :   GEN G = bnr_get_clgp(bnr);
    2843       14812 :   if (lg(G) !=  4)
    2844           7 :     pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
    2845       14805 :   return gel(G,3);
    2846             : }
    2847             : 
    2848             : INLINE GEN
    2849      296224 : bid_get_mod(GEN bid) { return gel(bid,1); }
    2850             : INLINE GEN
    2851       64757 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
    2852             : INLINE GEN
    2853        2695 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
    2854             : INLINE GEN
    2855     1276851 : bid_get_grp(GEN bid) { return gel(bid,2); }
    2856             : INLINE GEN
    2857      504075 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
    2858             : INLINE GEN
    2859      432556 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
    2860             : INLINE GEN
    2861      431632 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
    2862             : INLINE GEN
    2863       49084 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
    2864             : INLINE GEN
    2865      468053 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
    2866             : INLINE GEN
    2867      728115 : bid_get_U(GEN bid) { return gel(bid,5); }
    2868             : INLINE GEN
    2869           0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
    2870             : INLINE GEN
    2871     1229412 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
    2872             : INLINE GEN
    2873           0 : bid_get_gen_nocheck(GEN bid)  { return abgrp_get_gen(bid_get_grp(bid)); }
    2874             : INLINE GEN
    2875       46858 : bid_get_gen(GEN bid) {
    2876       46858 :   GEN G = bid_get_grp(bid);
    2877       46858 :   if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
    2878       46858 :   return abgrp_get_gen(G);
    2879             : }
    2880             : 
    2881             : INLINE GEN
    2882    26240935 : znstar_get_N(GEN G) { return gmael(G,1,1); }
    2883             : INLINE GEN
    2884     3215772 : znstar_get_faN(GEN G) { return gel(G,3); }
    2885             : INLINE GEN
    2886           7 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
    2887             : INLINE GEN
    2888      191107 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
    2889             : INLINE GEN
    2890           7 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
    2891             : INLINE GEN
    2892     3892917 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
    2893             : INLINE GEN
    2894     1386273 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
    2895             : INLINE GEN
    2896       45927 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
    2897             : INLINE GEN
    2898      120162 : znstar_get_U(GEN G) { return gel(G,5); }
    2899             : INLINE GEN
    2900      989737 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
    2901             : INLINE GEN
    2902       20881 : gal_get_pol(GEN gal) { return gel(gal,1); }
    2903             : INLINE GEN
    2904        1470 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
    2905             : INLINE GEN
    2906         119 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
    2907             : INLINE GEN
    2908       16170 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
    2909             : INLINE GEN
    2910       17255 : gal_get_roots(GEN gal) { return gel(gal,3); }
    2911             : INLINE GEN
    2912       16779 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
    2913             : INLINE GEN
    2914       16779 : gal_get_den(GEN gal) { return gel(gal,5); }
    2915             : INLINE GEN
    2916       68251 : gal_get_group(GEN gal) { return gel(gal,6); }
    2917             : INLINE GEN
    2918        1813 : gal_get_gen(GEN gal) { return gel(gal,7); }
    2919             : INLINE GEN
    2920        1456 : gal_get_orders(GEN gal) { return gel(gal,8); }
    2921             : 
    2922             : /* assume rnf a genuine rnf */
    2923             : INLINE long
    2924     1232903 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
    2925             : INLINE long
    2926       16688 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
    2927             : INLINE long
    2928      775320 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
    2929             : INLINE GEN
    2930         700 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
    2931             : INLINE GEN
    2932         749 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
    2933             : INLINE GEN
    2934         812 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
    2935             : INLINE GEN
    2936      424172 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
    2937             : INLINE GEN
    2938        3829 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
    2939             : INLINE GEN
    2940      209160 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
    2941             : INLINE GEN
    2942     1478967 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
    2943             : INLINE GEN
    2944         175 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
    2945             : INLINE GEN
    2946         105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
    2947             : INLINE GEN
    2948        1568 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
    2949             : INLINE long
    2950         231 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
    2951             : INLINE GEN
    2952      188314 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
    2953             : INLINE long
    2954        3332 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
    2955             : INLINE GEN
    2956        3507 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
    2957             : INLINE GEN
    2958      102697 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
    2959             : INLINE GEN
    2960        1197 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
    2961             : 
    2962             : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
    2963             :  * T2 matrix. Reduce I wrt G */
    2964             : INLINE GEN
    2965       22218 : idealpseudored(GEN I, GEN G)
    2966       22218 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
    2967             : 
    2968             : /* I integral (not necessarily HNF), G ZM, rounded Cholesky form of a weighted
    2969             :  * T2 matrix. Return m in I with T2(m) small */
    2970             : INLINE GEN
    2971      107408 : idealpseudomin(GEN I, GEN G)
    2972             : {
    2973      107408 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2974      107408 :   return ZM_ZC_mul(I, gel(u,1));
    2975             : }
    2976             : /* I, G as in idealpseudomin. Return an irrational m in I with T2(m) small */
    2977             : INLINE GEN
    2978      349338 : idealpseudomin_nonscalar(GEN I, GEN G)
    2979             : {
    2980      349338 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2981      349338 :   GEN m = ZM_ZC_mul(I, gel(u,1));
    2982      349338 :   if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
    2983      349338 :   return m;
    2984             : }
    2985             : 
    2986             : INLINE GEN
    2987         952 : idealred_elt(GEN nf, GEN I) {
    2988         952 :   pari_sp av = avma;
    2989         952 :   GEN u = idealpseudomin(I, nf_get_roundG(nf));
    2990         952 :   return gerepileupto(av, u);
    2991             : }
    2992             : INLINE GEN
    2993      115010 : idealred(GEN nf, GEN I) { return idealred0(nf, I, NULL); }
    2994             : 
    2995             : INLINE GEN
    2996        2044 : idealchineseinit(GEN nf, GEN x)
    2997        2044 : { return idealchinese(nf,x,NULL); }
    2998             : 
    2999             : /*******************************************************************/
    3000             : /*                                                                 */
    3001             : /*                              CLOSURES                           */
    3002             : /*                                                                 */
    3003             : /*******************************************************************/
    3004   235841709 : INLINE long closure_arity(GEN C)          { return ((ulong)C[1])&ARITYBITS; }
    3005    35402930 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
    3006   195916038 : INLINE const char *closure_codestr(GEN C)  { return GSTR(gel(C,2))-1; }
    3007           0 : INLINE GEN closure_get_code(GEN C)  { return gel(C,2); }
    3008   195898863 : INLINE GEN closure_get_oper(GEN C)  { return gel(C,3); }
    3009   195877940 : INLINE GEN closure_get_data(GEN C)  { return gel(C,4); }
    3010       10402 : INLINE GEN closure_get_dbg(GEN C)   { return gel(C,5); }
    3011       24727 : INLINE GEN closure_get_text(GEN C)  { return gel(C,6); }
    3012     5345583 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
    3013             : 
    3014             : /*******************************************************************/
    3015             : /*                                                                 */
    3016             : /*                               ERRORS                            */
    3017             : /*                                                                 */
    3018             : /*******************************************************************/
    3019             : INLINE long
    3020       54423 : err_get_num(GEN e) { return e[1]; }
    3021             : INLINE GEN
    3022          91 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
    3023             : 
    3024             : INLINE void
    3025          14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
    3026             : INLINE void
    3027           7 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
    3028             : INLINE void
    3029          70 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
    3030             : INLINE void
    3031         529 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
    3032             : INLINE void
    3033           0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
    3034             : INLINE void
    3035           0 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
    3036             : INLINE void
    3037          91 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
    3038             : INLINE void
    3039         448 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
    3040             : INLINE void
    3041       19765 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
    3042             : INLINE void
    3043          42 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
    3044             : INLINE void
    3045        2155 : pari_err_DOMAIN(const char *f, const char *v, const char *op, GEN l, GEN x) { pari_err(e_DOMAIN, f,v,op,l,x); }
    3046             : INLINE void
    3047         200 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
    3048             : INLINE void
    3049           0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
    3050             : INLINE void
    3051         364 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
    3052             : INLINE void
    3053         114 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
    3054             : INLINE void
    3055         225 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
    3056             : INLINE void
    3057           0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
    3058             : INLINE void
    3059          70 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
    3060             : INLINE void
    3061         749 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
    3062             : INLINE void
    3063          49 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
    3064             : INLINE void
    3065          84 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
    3066             : INLINE void
    3067       12972 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
    3068             : INLINE void
    3069        3388 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
    3070             : INLINE void
    3071         280 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
    3072             : INLINE void
    3073         196 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
    3074         196 : { pari_err(e_PRIORITY, f,x,op,v); }

Generated by: LCOV version 1.13