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 25819-e703fe1174) Lines: 1354 1509 89.7 %
Date: 2020-09-18 06:10:04 Functions: 584 654 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     3086973 : 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     3412463 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
     175             : INLINE GEN
     176    26306973 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
     177             : INLINE GEN
     178       32671 : 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    68195495 : sstoQ(long n, long d)
     188             : {
     189             :   ulong r;
     190             :   long g, q;
     191    68195495 :   if (!n)
     192             :   {
     193      100452 :     if (!d) pari_err_INV("sstoQ",gen_0);
     194      100452 :     return gen_0;
     195             :   }
     196    68095043 :   if (d < 0) { d = -d; n = -n; }
     197    68095043 :   if (d == 1) return stoi(n);
     198    68082688 :   q = udivuu_rem(labs(n),d,&r);
     199    68082688 :   if (!r) return n > 0? utoipos(q): utoineg(q);
     200     3584065 :   g = ugcd(d,r); /* gcd(n,d) */
     201     3584065 :   if (g != 1) { n /= g; d /= g; }
     202     3584065 :   retmkfrac(stoi(n), utoi(d));
     203             : }
     204             : 
     205             : INLINE GEN
     206     2466056 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
     207             : INLINE GEN
     208        1645 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
     209        1645 :   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    25223891 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
     215             : INLINE GEN
     216      460727 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
     217             : INLINE GEN
     218      285821 : 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    73233602 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
     225             : INLINE GEN
     226   117931059 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
     227   117930571 :   v[1]=x; v[2]=y; return v; }
     228             : INLINE GEN
     229    58948853 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
     230    58942514 :   v[1]=x; v[2]=y; v[3]=z; return v; }
     231             : INLINE GEN
     232    11996386 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
     233    11981068 :   v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
     234             : INLINE GEN
     235      471648 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
     236      471646 :   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    35348794 : mkvec(GEN x) { retmkvec(x); }
     243             : INLINE GEN
     244   110567000 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
     245             : INLINE GEN
     246    91559853 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
     247             : INLINE GEN
     248     7364857 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
     249             : INLINE GEN
     250    12250383 : 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       26633 : 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       85349 : mkvec2copy(GEN x, GEN y) {
     263       85349 :   GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
     264             : /* col */
     265             : INLINE GEN
     266     3015043 : mkcol(GEN x) { retmkcol(x); }
     267             : INLINE GEN
     268    37011806 : 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        9509 : 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       14474 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
     287             : /* mat */
     288             : INLINE GEN
     289     2490398 : mkmat(GEN x) { retmkmat(x); }
     290             : INLINE GEN
     291    32902996 : 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       26384 : 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       91095 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
     304             : INLINE GEN
     305       18852 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
     306             : INLINE long
     307      122132 : 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     4734634 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
     312             : 
     313             : /* pol */
     314             : INLINE GEN
     315     1035911 : pol_x(long v) {
     316     1035911 :   GEN p = cgetg(4, t_POL);
     317     1035912 :   p[1] = evalsigne(1)|evalvarn(v);
     318     1035912 :   gel(p,2) = gen_0;
     319     1035912 :   gel(p,3) = gen_1; return p;
     320             : }
     321             : /* x^n, assume n >= 0 */
     322             : INLINE GEN
     323     1702063 : pol_xn(long n, long v) {
     324     1702063 :   long i, a = n+2;
     325     1702063 :   GEN p = cgetg(a+1, t_POL);
     326     1702063 :   p[1] = evalsigne(1)|evalvarn(v);
     327     4338950 :   for (i = 2; i < a; i++) gel(p,i) = gen_0;
     328     1702063 :   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     1871019 : pol_1(long v) {
     348     1871019 :   GEN p = cgetg(3, t_POL);
     349     1871021 :   p[1] = evalsigne(1)|evalvarn(v);
     350     1871021 :   gel(p,2) = gen_1; return p;
     351             : }
     352             : INLINE GEN
     353    30512386 : pol_0(long v)
     354             : {
     355    30512386 :   GEN x = cgetg(2,t_POL);
     356    30512389 :   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   138233967 : 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    35789909 : const_col(long n, GEN x) { retconst_col(n, x); }
     372             : INLINE GEN
     373    11199141 : const_vecsmall(long n, long c)
     374             : {
     375             :   long i;
     376    11199141 :   GEN V = cgetg(n+1,t_VECSMALL);
     377   382083428 :   for(i=1;i<=n;i++) V[i] = c;
     378    11199149 :   return V;
     379             : }
     380             : 
     381             : /***   ZERO   ***/
     382             : /* O(p^e) */
     383             : INLINE GEN
     384      361998 : zeropadic(GEN p, long e)
     385             : {
     386      361998 :   GEN y = cgetg(5,t_PADIC);
     387      361998 :   gel(y,4) = gen_0;
     388      361998 :   gel(y,3) = gen_1;
     389      361998 :   gel(y,2) = icopy(p);
     390      361998 :   y[1] = evalvalp(e) | _evalprecp(0);
     391      361998 :   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      138054 : zeroser(long v, long e)
     406             : {
     407      138054 :   GEN x = cgetg(2, t_SER);
     408      138054 :   x[1] = evalvalp(e) | evalvarn(v); return x;
     409             : }
     410             : INLINE int
     411     7080663 : ser_isexactzero(GEN x)
     412             : {
     413     7080663 :   if (!signe(x)) switch(lg(x))
     414             :   {
     415      132265 :     case 2: return 1;
     416        1267 :     case 3: return isexactzero(gel(x,2));
     417             :   }
     418     6947131 :   return 0;
     419             : }
     420             : /* 0 * pol_x(v) */
     421             : INLINE GEN
     422    14230398 : zeropol(long v) { return pol_0(v); }
     423             : /* vector(n) */
     424             : INLINE GEN
     425    42975518 : zerocol(long n)
     426             : {
     427    42975518 :   GEN y = cgetg(n+1,t_COL);
     428   305445253 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     429    42975643 :   return y;
     430             : }
     431             : /* vectorv(n) */
     432             : INLINE GEN
     433    16977333 : zerovec(long n)
     434             : {
     435    16977333 :   GEN y = cgetg(n+1,t_VEC);
     436   135062844 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     437    16977332 :   return y;
     438             : }
     439             : /* matrix(m, n) */
     440             : INLINE GEN
     441       25724 : zeromat(long m, long n)
     442             : {
     443       25724 :   GEN y = cgetg(n+1,t_MAT);
     444       25724 :   GEN v = zerocol(m);
     445       95348 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     446       25724 :   return y;
     447             : }
     448             : /* = zero_zx, sv is a evalvarn()*/
     449             : INLINE GEN
     450      949403 : zero_Flx(long sv) { return pol0_Flx(sv); }
     451             : INLINE GEN
     452    35749375 : zero_Flv(long n)
     453             : {
     454    35749375 :   GEN y = cgetg(n+1,t_VECSMALL);
     455   765953804 :   long i; for (i=1; i<=n; i++) y[i] = 0;
     456    35750091 :   return y;
     457             : }
     458             : /* matrix(m, n) */
     459             : INLINE GEN
     460     1327984 : zero_Flm(long m, long n)
     461             : {
     462     1327984 :   GEN y = cgetg(n+1,t_MAT);
     463     1327976 :   GEN v = zero_Flv(m);
     464     9108171 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     465     1327959 :   return y;
     466             : }
     467             : /* matrix(m, n) */
     468             : INLINE GEN
     469      113312 : zero_Flm_copy(long m, long n)
     470             : {
     471      113312 :   GEN y = cgetg(n+1,t_MAT);
     472     1661304 :   long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
     473      113313 :   return y;
     474             : }
     475             : 
     476             : INLINE GEN
     477      930690 : zero_F2v(long m)
     478             : {
     479      930690 :   long l = nbits2nlong(m);
     480      930690 :   GEN v  = zero_Flv(l+1);
     481      930690 :   v[1] = m;
     482      930690 :   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      310688 : zero_F2m_copy(long m, long n)
     499             : {
     500             :   long i;
     501      310688 :   GEN M = cgetg(n+1, t_MAT);
     502      826659 :   for (i = 1; i <= n; i++)
     503      515971 :     gel(M,i)= zero_F2v(m);
     504      310688 :   return M;
     505             : }
     506             : 
     507             : /* matrix(m, n) */
     508             : INLINE GEN
     509     5586763 : zeromatcopy(long m, long n)
     510             : {
     511     5586763 :   GEN y = cgetg(n+1,t_MAT);
     512    22900338 :   long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
     513     5586763 :   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     1388366 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
     529             : INLINE GEN
     530      395895 : 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       35001 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
     535             : INLINE GEN
     536      506467 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
     537             : 
     538             : INLINE GEN
     539    13112237 : shallowcopy(GEN x)
     540    13112237 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
     541             : 
     542             : /* routines for naive growarrays */
     543             : INLINE GEN
     544    12382666 : vectrunc_init(long l)
     545             : {
     546    12382666 :   GEN z = new_chunk(l);
     547    12382511 :   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   267241942 : lg_increase(GEN x) { x[0]++; }
     557             : INLINE void
     558    51348171 : 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    89129968 : vecsmalltrunc_init(long l)
     569             : {
     570    89129968 :   GEN z = new_chunk(l);
     571    89129968 :   z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
     572             : }
     573             : INLINE void
     574    34079865 : 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     2209357 : hash_str(const char *str)
     583             : {
     584     2209357 :   ulong hash = 5381UL, c;
     585    22164467 :   while ( (c = (ulong)*str++) )
     586    19955110 :     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     587     2209357 :   return hash;
     588             : }
     589             : INLINE ulong
     590    10028281 : hash_str_len(const char *str, long len)
     591             : {
     592    10028281 :   ulong hash = 5381UL;
     593             :   long i;
     594    76000193 :   for (i = 0; i < len; i++)
     595             :   {
     596    65971912 :     ulong c = (ulong)*str++;
     597    65971912 :     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     598             :   }
     599    10028281 :   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       13731 : vec_lengthen(GEN v, long n)
     619             : {
     620             :   long i;
     621       13731 :   long l=lg(v);
     622       13731 :   GEN V = cgetg(n+1,t_VEC);
     623      455493 :   for(i=1;i<l;i++) gel(V,i) = gel(v,i);
     624       13731 :   return V;
     625             : }
     626             : /* shallow*/
     627             : INLINE GEN
     628       80388 : vec_append(GEN V, GEN s)
     629             : {
     630       80388 :   long i, l2 = lg(V);
     631       80388 :   GEN res = cgetg(l2+1, typ(V));
     632      832110 :   for (i = 1; i < l2; ++i) gel(res, i) = gel(V,i);
     633       80388 :   gel(res,l2) = s; return res;
     634             : }
     635             : /* shallow*/
     636             : INLINE GEN
     637       40827 : vec_prepend(GEN v, GEN s)
     638             : {
     639       40827 :   long i, l = lg(v);
     640       40827 :   GEN w = cgetg(l+1, typ(v));
     641       40827 :   gel(w,1) = s;
     642       77439 :   for (i = 2; i <= l; ++i) gel(w,i) = gel(v,i-1);
     643       40827 :   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       43420 : vecsmall_shorten(GEN v, long n)
     655             : {
     656             :   long i;
     657       43420 :   GEN V = cgetg(n+1,t_VECSMALL);
     658      569623 :   for(i=1;i<=n;i++) V[i] = v[i];
     659       43421 :   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     3330986 : vec_to_vecsmall(GEN x)
     672    16221055 : { pari_APPLY_long(itos(gel(x,i))) }
     673             : INLINE GEN
     674      143346 : vecsmall_to_vec(GEN x)
     675     4464623 : { pari_APPLY_type(t_VEC, stoi(x[i])) }
     676             : INLINE GEN
     677        2457 : vecsmall_to_vec_inplace(GEN z)
     678             : {
     679        2457 :   long i, l = lg(z);
     680       76587 :   for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
     681        2457 :   settyp(z, t_VEC); return z;
     682             : }
     683             : INLINE GEN
     684     5356776 : vecsmall_to_col(GEN x)
     685    53103962 : { pari_APPLY_type(t_COL, stoi(x[i])) }
     686             : 
     687             : INLINE int
     688     6088725 : vecsmall_lexcmp(GEN x, GEN y)
     689             : {
     690             :   long lx,ly,l,i;
     691     6088725 :   lx = lg(x);
     692     6088725 :   ly = lg(y); l = minss(lx,ly);
     693    29479884 :   for (i=1; i<l; i++)
     694    27864432 :     if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
     695     1615452 :   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       22414 : vecsmall_prepend(GEN V, long s)
     711             : {
     712       22414 :   long i, l2 = lg(V);
     713       22414 :   GEN res = cgetg(l2+1, typ(V));
     714       22414 :   res[1] = s;
     715       71071 :   for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
     716       22414 :   return res;
     717             : }
     718             : 
     719             : INLINE GEN
     720     2963322 : vecsmall_append(GEN V, long s)
     721             : {
     722     2963322 :   long i, l2 = lg(V);
     723     2963322 :   GEN res = cgetg(l2+1, t_VECSMALL);
     724     4438366 :   for (i = 1; i < l2; ++i) res[i] = V[i];
     725     2963321 :   res[l2] = s; return res;
     726             : }
     727             : 
     728             : INLINE GEN
     729      945911 : vecsmall_concat(GEN u, GEN v)
     730             : {
     731      945911 :   long i, l1 = lg(u)-1, l2 = lg(v)-1;
     732      945911 :   GEN res = cgetg(l1+l2+1, t_VECSMALL);
     733     7320085 :   for (i = 1; i <= l1; ++i) res[i]    = u[i];
     734     8207787 :   for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
     735      945911 :   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      381384 : vecsmall_max(GEN x)
     777             : {
     778      381384 :   long i, t = x[1], lx = lg(x);
     779      977051 :   for (i=2; i<lx; i++)
     780      595667 :     if (x[i] > t) t = x[i];
     781      381384 :   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     8052359 : ZV_isscalar(GEN x)
     804             : {
     805     8052359 :   long l = lg(x);
     806    18298577 :   while (--l > 1)
     807    16951633 :     if (signe(gel(x, l))) return 0;
     808     1346944 :   return 1;
     809             : }
     810             : INLINE int
     811    24491139 : QV_isscalar(GEN x)
     812             : {
     813    24491139 :   long lx = lg(x),i;
     814    27253949 :   for (i=2; i<lx; i++)
     815    26493827 :     if (!isintzero(gel(x, i))) return 0;
     816      760122 :   return 1;
     817             : }
     818             : INLINE int
     819        7184 : RgV_isscalar(GEN x)
     820             : {
     821        7184 :   long lx = lg(x),i;
     822        7340 :   for (i=2; i<lx; i++)
     823        7298 :     if (!gequal0(gel(x, i))) return 0;
     824          42 :   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    24839762 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
     836             : 
     837             : INLINE int
     838       10213 : RgX_is_rational(GEN x)
     839             : {
     840             :   long i;
     841       57697 :   for (i = lg(x)-1; i > 1; i--)
     842       53385 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     843        4312 :   return 1;
     844             : }
     845             : INLINE int
     846     5243890 : RgX_is_ZX(GEN x)
     847             : {
     848             :   long i;
     849    23640873 :   for (i = lg(x)-1; i > 1; i--)
     850    18449530 :     if (typ(gel(x,i)) != t_INT) return 0;
     851     5191343 :   return 1;
     852             : }
     853             : INLINE int
     854      258247 : RgX_is_QX(GEN x)
     855             : {
     856      258247 :   long k = lg(x)-1;
     857      955644 :   for ( ; k>1; k--)
     858      697677 :     if (!is_rational_t(typ(gel(x,k)))) return 0;
     859      257967 :   return 1;
     860             : }
     861             : INLINE int
     862     2343176 : RgX_is_monomial(GEN x)
     863             : {
     864             :   long i;
     865     2343176 :   if (!signe(x)) return 0;
     866     5858214 :   for (i=lg(x)-2; i>1; i--)
     867     3563053 :     if (!isexactzero(gel(x,i))) return 0;
     868     2295161 :   return 1;
     869             : }
     870             : INLINE int
     871    27696364 : RgV_is_ZV(GEN x)
     872             : {
     873             :   long i;
     874   113260125 :   for (i = lg(x)-1; i > 0; i--)
     875    85567065 :     if (typ(gel(x,i)) != t_INT) return 0;
     876    27693060 :   return 1;
     877             : }
     878             : INLINE int
     879       24283 : RgV_is_QV(GEN x)
     880             : {
     881             :   long i;
     882      111930 :   for (i = lg(x)-1; i > 0; i--)
     883       89341 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     884       22589 :   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   525587134 : pari_stack_base(pari_stack *s) { return s->data; }
     904             : 
     905             : INLINE void
     906     2982600 : pari_stack_init(pari_stack *s, size_t size, void **data)
     907             : {
     908     2982600 :   s->data = data;
     909     2982600 :   *data = NULL;
     910     2982600 :   s->n = 0;
     911     2982600 :   s->alloc = 0;
     912     2982600 :   s->size = size;
     913     2982600 : }
     914             : 
     915             : INLINE void
     916   522803300 : pari_stack_alloc(pari_stack *s, long nb)
     917             : {
     918   522803300 :   void **sdat = pari_stack_base(s);
     919   522806023 :   long alloc = s->alloc;
     920   522806023 :   if (s->n+nb <= alloc) return;
     921     1131869 :   if (!alloc)
     922     1065014 :     alloc = nb;
     923             :   else
     924             :   {
     925      136361 :     while (s->n+nb > alloc) alloc <<= 1;
     926             :   }
     927     1131869 :   pari_realloc_ip(sdat,alloc*s->size);
     928     1138919 :   s->alloc = alloc;
     929             : }
     930             : 
     931             : INLINE long
     932   439598002 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
     933             : 
     934             : INLINE void
     935     2779208 : pari_stack_delete(pari_stack *s)
     936             : {
     937     2779208 :   void **sdat = pari_stack_base(s);
     938     2774294 :   if (*sdat) pari_free(*sdat);
     939     2791118 : }
     940             : 
     941             : INLINE void
     942        5221 : pari_stack_pushp(pari_stack *s, void *u)
     943             : {
     944        5221 :   long n = pari_stack_new(s);
     945        5221 :   void **sdat =(void**) *pari_stack_base(s);
     946        5221 :   sdat[n] = u;
     947        5221 : }
     948             : 
     949             : /*******************************************************************/
     950             : /*                                                                 */
     951             : /*                            EXTRACT                              */
     952             : /*                                                                 */
     953             : /*******************************************************************/
     954             : INLINE GEN
     955   397428101 : vecslice(GEN A, long y1, long y2)
     956             : {
     957   397428101 :   long i,lB = y2 - y1 + 2;
     958   397428101 :   GEN B = cgetg(lB, typ(A));
     959  1811625918 :   for (i=1; i<lB; i++) B[i] = A[y1-1+i];
     960   397421377 :   return B;
     961             : }
     962             : INLINE GEN
     963     1170798 : vecslicepermute(GEN A, GEN p, long y1, long y2)
     964             : {
     965     1170798 :   long i,lB = y2 - y1 + 2;
     966     1170798 :   GEN B = cgetg(lB, typ(A));
     967    20868585 :   for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
     968     1170792 :   return B;
     969             : }
     970             : /* rowslice(rowpermute(A,p), x1, x2) */
     971             : INLINE GEN
     972       53336 : rowslicepermute(GEN x, GEN p, long j1, long j2)
     973      259932 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
     974             : 
     975             : INLINE GEN
     976    34782605 : rowslice(GEN x, long j1, long j2)
     977   404647906 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
     978             : 
     979             : INLINE GEN
     980     5802486 : matslice(GEN A, long x1, long x2, long y1, long y2)
     981     5802486 : { return rowslice(vecslice(A, y1, y2), x1, x2); }
     982             : 
     983             : /* shallow, remove coeff of index j */
     984             : INLINE GEN
     985         133 : rowsplice(GEN x, long j)
     986         805 : { pari_APPLY_same(vecsplice(gel(x,i), j)) }
     987             : 
     988             : /* shallow, remove coeff of index j */
     989             : INLINE GEN
     990      268453 : vecsplice(GEN a, long j)
     991             : {
     992      268453 :   long i, k, l = lg(a);
     993             :   GEN b;
     994      268453 :   if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
     995      268453 :   b = cgetg(l-1, typ(a));
     996     1242175 :   for (i = k = 1; i < l; i++)
     997      973722 :     if (i != j) gel(b, k++) = gel(a,i);
     998      268453 :   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      390386 : row(GEN x, long j)
    1013     4020298 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
    1014             : INLINE GEN
    1015     5038458 : Flm_row(GEN x, long j)
    1016   125233933 : { 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     5971771 : vecreverse(GEN A)
    1033             : {
    1034             :   long i, l;
    1035     5971771 :   GEN B = cgetg_copy(A, &l);
    1036    14497704 :   for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
    1037     5971771 :   return B;
    1038             : }
    1039             : 
    1040             : INLINE GEN
    1041         686 : vecsmall_reverse(GEN A)
    1042             : {
    1043             :   long i, l;
    1044         686 :   GEN B = cgetg_copy(A, &l);
    1045        2268 :   for (i=1; i<l; i++) B[i] = A[l-i];
    1046         686 :   return B;
    1047             : }
    1048             : 
    1049             : INLINE void
    1050         435 : vecreverse_inplace(GEN y)
    1051             : {
    1052         435 :   long l = lg(y), lim = l>>1, i;
    1053        1648 :   for (i = 1; i <= lim; i++)
    1054             :   {
    1055        1213 :     GEN z = gel(y,i);
    1056        1213 :     gel(y,i)    = gel(y,l-i);
    1057        1213 :     gel(y,l-i) = z;
    1058             :   }
    1059         435 : }
    1060             : 
    1061             : INLINE GEN
    1062    40723145 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
    1063             : 
    1064             : INLINE GEN
    1065     7142675 : vecpermute(GEN A, GEN x)
    1066    85588388 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
    1067             : 
    1068             : INLINE GEN
    1069     4085342 : rowpermute(GEN x, GEN p)
    1070    44635029 : { 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      931824 : identity_zv(long n)
    1080             : {
    1081      931824 :   GEN v = cgetg(n+1, t_VECSMALL);
    1082             :   long i;
    1083    15367364 :   for (i = 1; i <= n; i++) v[i] = i;
    1084      931819 :   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      931252 : 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    41669425 : perm_mul(GEN s, GEN x)
    1112   558585335 : { pari_APPLY_long(s[x[i]]) }
    1113             : 
    1114             : /* Compute the inverse (reciprocal) of a permutation. */
    1115             : INLINE GEN
    1116     1348703 : perm_inv(GEN x)
    1117             : {
    1118             :   long i, lx;
    1119     1348703 :   GEN y = cgetg_copy(x, &lx);
    1120    19128690 :   for (i=1; i<lx; i++) y[ x[i] ] = i;
    1121     1348703 :   return y;
    1122             : }
    1123             : /* Return s*t*s^-1 */
    1124             : INLINE GEN
    1125      417312 : perm_conj(GEN s, GEN t)
    1126             : {
    1127             :   long i, l;
    1128      417312 :   GEN v = cgetg_copy(s, &l);
    1129     6838846 :   for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
    1130      417312 :   return v;
    1131             : }
    1132             : 
    1133             : INLINE void
    1134   305596057 : pari_free(void *pointer)
    1135             : {
    1136   305596057 :   BLOCK_SIGINT_START;
    1137   305618144 :   free(pointer);
    1138   305618144 :   BLOCK_SIGINT_END;
    1139   305620705 : }
    1140             : INLINE void*
    1141   516324413 : pari_malloc(size_t size)
    1142             : {
    1143   516324413 :   if (size)
    1144             :   {
    1145             :     char *tmp;
    1146   516324421 :     BLOCK_SIGINT_START;
    1147   516325049 :     tmp = (char*)malloc(size);
    1148   516325049 :     BLOCK_SIGINT_END;
    1149   516324179 :     if (!tmp) pari_err(e_MEM);
    1150   516328594 :     return tmp;
    1151             :   }
    1152           0 :   return NULL;
    1153             : }
    1154             : INLINE void*
    1155        1680 : pari_realloc(void *pointer, size_t size)
    1156             : {
    1157             :   char *tmp;
    1158             : 
    1159        1680 :   BLOCK_SIGINT_START;
    1160        1680 :   if (!pointer) tmp = (char *) malloc(size);
    1161        1680 :   else tmp = (char *) realloc(pointer,size);
    1162        1680 :   BLOCK_SIGINT_END;
    1163        1680 :   if (!tmp) pari_err(e_MEM);
    1164        1680 :   return tmp;
    1165             : }
    1166             : INLINE void
    1167     1147322 : pari_realloc_ip(void **pointer, size_t size)
    1168             : {
    1169             :   char *tmp;
    1170     1147322 :   BLOCK_SIGINT_START;
    1171     1146209 :   if (!*pointer) tmp = (char *) malloc(size);
    1172       70817 :   else tmp = (char *) realloc(*pointer,size);
    1173     1146209 :   if (!tmp) pari_err(e_MEM);
    1174     1146209 :   *pointer = tmp;
    1175     1146209 :   BLOCK_SIGINT_END;
    1176     1142046 : }
    1177             : 
    1178             : INLINE void*
    1179       39994 : pari_calloc(size_t size)
    1180             : {
    1181       39994 :   void *t = pari_malloc(size);
    1182       39994 :   memset(t, 0, size); return t;
    1183             : }
    1184             : INLINE GEN
    1185        7892 : cgetalloc(long t, size_t l)
    1186             : { /* evallg may raise e_OVERFLOW, which would leak x */
    1187        7892 :   ulong x0 = evaltyp(t) | evallg(l);
    1188        7892 :   GEN x = (GEN)pari_malloc(l * sizeof(long));
    1189        7892 :   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  4827719801 : icopy_avma(GEN x, pari_sp av)
    1200             : {
    1201  4827719801 :   long i = lgefint(x), lx = i;
    1202  4827719801 :   GEN y = ((GEN)av) - i;
    1203 26918243586 :   while (--i > 0) y[i] = x[i];
    1204  4827719801 :   y[0] = evaltyp(t_INT)|evallg(lx);
    1205  4830915067 :   return y;
    1206             : }
    1207             : /* copy leaf x as if we had set_avma(av) */
    1208             : INLINE GEN
    1209   252821318 : leafcopy_avma(GEN x, pari_sp av)
    1210             : {
    1211   252821318 :   long i = lg(x);
    1212   252821318 :   GEN y = ((GEN)av) - i;
    1213  1837465392 :   while (--i > 0) y[i] = x[i];
    1214   252821318 :   y[0] = x[0] & (~CLONEBIT);
    1215   252821318 :   return y;
    1216             : }
    1217             : INLINE GEN
    1218   662083132 : gerepileuptoleaf(pari_sp av, GEN x)
    1219             : {
    1220             :   long lx;
    1221             :   GEN q;
    1222             : 
    1223   662083132 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1224   661824941 :   lx = lg(x);
    1225   661824941 :   q = ((GEN)av) - lx;
    1226   661824941 :   set_avma((pari_sp)q);
    1227  7277640838 :   while (--lx >= 0) q[lx] = x[lx];
    1228   661760557 :   return q;
    1229             : }
    1230             : INLINE GEN
    1231  1929776259 : gerepileuptoint(pari_sp av, GEN x)
    1232             : {
    1233  1929776259 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1234  1698643574 :   set_avma((pari_sp)icopy_avma(x, av));
    1235  1701209321 :   return (GEN)avma;
    1236             : }
    1237             : INLINE GEN
    1238   842656306 : gerepileupto(pari_sp av, GEN x)
    1239             : {
    1240   842656306 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1241   747032796 :   switch(typ(x))
    1242             :   { /* non-default = !is_recursive_t(tq) */
    1243   220331314 :     case t_INT: return gerepileuptoint(av, x);
    1244   150967892 :     case t_REAL:
    1245             :     case t_STR:
    1246   150967892 :     case t_VECSMALL: return gerepileuptoleaf(av,x);
    1247   375733590 :     default:
    1248             :       /* NB: x+i --> ((long)x) + i*sizeof(long) */
    1249   375733590 :       return gerepile(av, (pari_sp) (x+lg(x)), x);
    1250             :   }
    1251             : }
    1252             : 
    1253             : /* gerepileupto(av, gcopy(x)) */
    1254             : INLINE GEN
    1255    75433548 : gerepilecopy(pari_sp av, GEN x)
    1256             : {
    1257    75433548 :   if (is_recursive_t(typ(x)))
    1258             :   {
    1259    70823721 :     GENbin *p = copy_bin(x);
    1260    70824663 :     set_avma(av); return bin_copy(p);
    1261             :   }
    1262             :   else
    1263             :   {
    1264     4609904 :     set_avma(av);
    1265     4609907 :     if (x < (GEN)av) {
    1266     3114022 :       if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
    1267     3114022 :       x = leafcopy_avma(x, av);
    1268     3114029 :       set_avma((pari_sp)x);
    1269             :     } else
    1270     1495885 :       x = leafcopy(x);
    1271     4609919 :     return x;
    1272             :   }
    1273             : }
    1274             : 
    1275             : INLINE void
    1276    25250311 : guncloneNULL(GEN x) { if (x) gunclone(x); }
    1277             : INLINE void
    1278      208412 : 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      194160 : gerepilemany(pari_sp av, GEN* gptr[], int n)
    1285             : {
    1286             :   int i;
    1287      770145 :   for (i=0; i<n; i++) *gptr[i] = (GEN)copy_bin(*gptr[i]);
    1288      194160 :   set_avma(av);
    1289      770145 :   for (i=0; i<n; i++) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
    1290      194160 : }
    1291             : 
    1292             : INLINE void
    1293    94821801 : gerepileall(pari_sp av, int n, ...)
    1294             : {
    1295             :   int i;
    1296    94821801 :   va_list a; va_start(a, n);
    1297    94821801 :   if (n < 10)
    1298             :   {
    1299             :     GEN *gptr[10];
    1300   310685747 :     for (i=0; i<n; i++)
    1301   215863788 :     { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
    1302    94821959 :     set_avma(av);
    1303   310685984 :     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    94822091 :   va_end(a);
    1316    94822091 : }
    1317             : 
    1318             : INLINE void
    1319      749534 : gerepilecoeffs(pari_sp av, GEN x, int n)
    1320             : {
    1321             :   int i;
    1322     9833530 :   for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
    1323      749534 :   set_avma(av);
    1324     9833530 :   for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
    1325      749534 : }
    1326             : 
    1327             : /* p from copy_bin. Copy p->x back to stack, then destroy p */
    1328             : INLINE GEN
    1329   296398791 : bin_copy(GENbin *p)
    1330             : {
    1331             :   GEN x, y, base;
    1332             :   long dx, len;
    1333             : 
    1334   296398791 :   x   = p->x; if (!x) { pari_free(p); return gen_0; }
    1335   272496145 :   len = p->len;
    1336   272496145 :   base= p->base; dx = x - base;
    1337   272496145 :   y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
    1338   272495854 :   y += dx;
    1339   272495854 :   p->rebase(y, ((ulong)y-(ulong)x));
    1340   272496065 :   pari_free(p); return y;
    1341             : }
    1342             : 
    1343             : INLINE GEN
    1344   568895331 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
    1345             : 
    1346             : INLINE void
    1347    81024854 : cgiv(GEN x)
    1348             : {
    1349    81024854 :   pari_sp av = (pari_sp)(x+lg(x));
    1350    81024854 :   if (isonstack((GEN)av)) set_avma(av);
    1351    81023987 : }
    1352             : 
    1353             : INLINE void
    1354     6599337 : killblock(GEN x) { gunclone(x); }
    1355             : 
    1356             : INLINE int
    1357   240376252 : 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     5305375 : cxcompotor(GEN z, long prec)
    1367             : {
    1368     5305375 :   switch(typ(z))
    1369             :   {
    1370     3062203 :     case t_INT:  return itor(z, prec);
    1371      136689 :     case t_FRAC: return fractor(z, prec);
    1372     2106483 :     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     2639645 : cxtofp(GEN x, long prec)
    1379     2639645 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
    1380             : 
    1381             : INLINE GEN
    1382      273415 : cxtoreal(GEN q)
    1383      273415 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
    1384             : 
    1385             : INLINE double
    1386    36938128 : gtodouble(GEN x)
    1387             : {
    1388    36938128 :   if (typ(x)!=t_REAL) {
    1389     6045107 :     pari_sp av = avma;
    1390     6045107 :     x = gtofp(x, DEFAULTPREC);
    1391     6044497 :     if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
    1392     6044497 :     set_avma(av);
    1393             :   }
    1394    36937420 :   return rtodbl(x);
    1395             : }
    1396             : INLINE long
    1397    73482151 : gtos(GEN x) {
    1398    73482151 :   if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
    1399    73482144 :   return itos(x);
    1400             : }
    1401             : 
    1402             : INLINE ulong
    1403       87955 : gtou(GEN x) {
    1404       87955 :   if (typ(x) != t_INT || signe(x)<0)
    1405           7 :     pari_err_TYPE("gtou [integer >=0 expected]",x);
    1406       87948 :   return itou(x);
    1407             : }
    1408             : 
    1409             : INLINE GEN
    1410    35825377 : absfrac(GEN x)
    1411             : {
    1412    35825377 :   GEN y = cgetg(3, t_FRAC);
    1413    35825377 :   gel(y,1) = absi(gel(x,1));
    1414    35825377 :   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    10024082 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
    1421             : INLINE GEN
    1422      125734 : Q_abs_shallow(GEN x)
    1423      125734 : { 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    39263982 : gtofp(GEN z, long prec)
    1434             : {
    1435    39263982 :   switch(typ(z))
    1436             :   {
    1437    23864255 :     case t_INT:  return itor(z, prec);
    1438     4068126 :     case t_FRAC: return fractor(z, prec);
    1439     8941403 :     case t_REAL: return rtor(z, prec);
    1440     2392155 :     case t_COMPLEX: {
    1441     2392155 :       GEN a = gel(z,1), b = gel(z,2);
    1442     2392155 :       if (isintzero(b)) return cxcompotor(a, prec);
    1443     2392155 :       if (isintzero(a)) {
    1444        3947 :         GEN y = cgetg(3, t_COMPLEX);
    1445        3947 :         b = cxcompotor(b, prec);
    1446        3947 :         gel(y,1) = real_0_bit(expo(b) - prec2nbits(prec));
    1447        3947 :         gel(y,2) = b; return y;
    1448             :       }
    1449     2388208 :       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     1132213 : RgX_gtofp(GEN x, long prec)
    1474             : {
    1475             :   long l;
    1476     1132213 :   GEN y = cgetg_copy(x, &l);
    1477     6995848 :   while (--l > 1) gel(y,l) = gtofp(gel(x,l), prec);
    1478     1132213 :   y[1] = x[1]; return y;
    1479             : }
    1480             : INLINE GEN
    1481     1313472 : RgC_gtofp(GEN x, long prec)
    1482    15992606 : { pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
    1483             : 
    1484             : INLINE GEN
    1485       13174 : RgV_gtofp(GEN x, long prec)
    1486       40712 : { pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
    1487             : 
    1488             : INLINE GEN
    1489      144464 : RgM_gtofp(GEN x, long prec)
    1490     1199017 : { 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       11732 : RgX_fpnorml2(GEN x, long prec)
    1502             : {
    1503       11732 :   pari_sp av = avma;
    1504       11732 :   return gerepileupto(av, gnorml2(RgX_gtofp(x, prec)));
    1505             : }
    1506             : INLINE GEN
    1507      246334 : RgC_fpnorml2(GEN x, long prec)
    1508             : {
    1509      246334 :   pari_sp av = avma;
    1510      246334 :   return gerepileupto(av, gnorml2(RgC_gtofp(x, prec)));
    1511             : }
    1512             : INLINE GEN
    1513        7682 : RgM_fpnorml2(GEN x, long prec)
    1514             : {
    1515        7682 :   pari_sp av = avma;
    1516        7682 :   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      243054 : affc_fixlg(GEN x, GEN res)
    1535             : {
    1536      243054 :   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       32396 :     set_avma((pari_sp)(res+3));
    1544       32392 :     res = cgetr(realprec(gel(res,1)));
    1545       32398 :     affrr_fixlg(x, res);
    1546             :   }
    1547      243061 :   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       36046 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
    1560             : INLINE long
    1561       27777 : 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     6994018 : nbits2nlong(long x) {
    1569     6994018 :   return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1570             : }
    1571             : 
    1572             : INLINE long
    1573   396527976 : nbits2extraprec(long x) {
    1574   396527976 :   return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1575             : }
    1576             : 
    1577             : /* Fast implementation of 2 + nbits2nlong(x) */
    1578             : INLINE long
    1579   138871350 : nbits2prec(long x) {
    1580   138871350 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1581             : }
    1582             : INLINE long
    1583   198830919 : nbits2lg(long x) {
    1584   198830919 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1585             : }
    1586             : /* ceil(x / sizeof(long)) */
    1587             : INLINE long
    1588    16894798 : nchar2nlong(long x) {
    1589    16894798 :   return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
    1590             : }
    1591             : INLINE long
    1592  1682555090 : prec2nbits(long x) { return (x-2) * BITS_IN_LONG; }
    1593             : INLINE double
    1594     2865311 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
    1595             : INLINE double
    1596      272346 : prec2nbits_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
    1597             : INLINE long
    1598    56820025 : bit_prec(GEN x) { return prec2nbits(realprec(x)); }
    1599             : INLINE long
    1600  1494130427 : bit_accuracy(long x) { return prec2nbits(x); }
    1601             : INLINE long
    1602       12833 : 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        3148 : precdbl(long x) {return (x - 1) << 1;}
    1607             : INLINE long
    1608  4615024032 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
    1609             : INLINE long
    1610  4558620482 : 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    36835783 : Fp_red(GEN a, GEN m) { return modii(a, m); }
    1621             : INLINE GEN
    1622   108258335 : Fp_add(GEN a, GEN b, GEN m)
    1623             : {
    1624   108258335 :   pari_sp av=avma;
    1625   108258335 :   GEN p = addii(a,b);
    1626   106766214 :   long s = signe(p);
    1627   106766214 :   if (!s) return p; /* = gen_0 */
    1628    96050893 :   if (s > 0) /* general case */
    1629             :   {
    1630    95997570 :     GEN t = subii(p, m);
    1631    95783826 :     s = signe(t);
    1632    95783826 :     if (!s) return gc_const(av, gen_0);
    1633    88270744 :     if (s < 0) return gc_const((pari_sp)p, p);
    1634    44482110 :     if (cmpii(t, m) < 0) return gerepileuptoint(av, t); /* general case ! */
    1635     2990699 :     p = remii(t, m);
    1636             :   }
    1637             :   else
    1638      226414 :     p = modii(p, m);
    1639     3217113 :   return gerepileuptoint(av, p);
    1640             : }
    1641             : INLINE GEN
    1642    81980786 : Fp_sub(GEN a, GEN b, GEN m)
    1643             : {
    1644    81980786 :   pari_sp av=avma;
    1645    81980786 :   GEN p = subii(a,b);
    1646    80481324 :   long s = signe(p);
    1647    80481324 :   if (!s) return p; /* = gen_0 */
    1648    72718302 :   if (s > 0)
    1649             :   {
    1650    33710348 :     if (cmpii(p, m) < 0) return p; /* general case ! */
    1651     1244383 :     p = remii(p, m);
    1652             :   }
    1653             :   else
    1654             :   {
    1655    39007954 :     GEN t = addii(p, m);
    1656    40779562 :     if (!s) return gc_const(av, gen_0);
    1657    40815553 :     if (s > 0) return gerepileuptoint(av, t); /* general case ! */
    1658    40815553 :     p = modii(t, m);
    1659             :   }
    1660    42343539 :   return gerepileuptoint(av, p);
    1661             : }
    1662             : INLINE GEN
    1663    14446704 : Fp_neg(GEN b, GEN m)
    1664             : {
    1665    14446704 :   pari_sp av = avma;
    1666    14446704 :   long s = signe(b);
    1667             :   GEN p;
    1668    14446704 :   if (!s) return gen_0;
    1669    11217244 :   if (s > 0)
    1670             :   {
    1671    11018650 :     p = subii(m, b);
    1672    11015683 :     if (signe(p) >= 0) return p; /* general case ! */
    1673      131659 :     p = modii(p, m);
    1674             :   } else
    1675      199867 :     p = remii(negi(b), m);
    1676      331577 :   return gerepileuptoint(av, p);
    1677             : }
    1678             : 
    1679             : INLINE GEN
    1680       63497 : Fp_halve(GEN a, GEN p)
    1681             : {
    1682       63497 :   if (mpodd(a)) a = addii(a,p);
    1683       63497 :   return shifti(a,-1);
    1684             : }
    1685             : 
    1686             : /* assume 0 <= u < p and ps2 = p>>1 */
    1687             : INLINE GEN
    1688    40065302 : Fp_center(GEN u, GEN p, GEN ps2)
    1689    40065302 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
    1690             : /* same without copy */
    1691             : INLINE GEN
    1692     5402371 : Fp_center_i(GEN u, GEN p, GEN ps2)
    1693     5402371 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
    1694             : 
    1695             : /* x + y*z mod p */
    1696             : INLINE GEN
    1697     7925963 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
    1698             : {
    1699             :   pari_sp av;
    1700     7925963 :   if (!signe(y) || !signe(z)) return Fp_red(x, p);
    1701     7648004 :   if (!signe(x)) return Fp_mul(z,y, p);
    1702     7331444 :   av = avma;
    1703     7331444 :   return gerepileuptoint(av, modii(addii(x, mulii(y,z)), p));
    1704             : }
    1705             : 
    1706             : INLINE GEN
    1707    99078272 : Fp_mul(GEN a, GEN b, GEN m)
    1708             : {
    1709    99078272 :   pari_sp av=avma;
    1710             :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1711    99078272 :   (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
    1712    99079433 :   p = mulii(a,b);
    1713    99243693 :   set_avma(av); return modii(p,m);
    1714             : }
    1715             : INLINE GEN
    1716    57636406 : Fp_sqr(GEN a, GEN m)
    1717             : {
    1718    57636406 :   pari_sp av=avma;
    1719             :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1720    57636406 :   (void)new_chunk((lg(a)+lg(m))<<1);
    1721    57687579 :   p = sqri(a);
    1722    55508679 :   set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
    1723             : }
    1724             : INLINE GEN
    1725    78650523 : Fp_mulu(GEN a, ulong b, GEN m)
    1726             : {
    1727    78650523 :   long l = lgefint(m);
    1728    78650523 :   if (l == 3)
    1729             :   {
    1730    44936668 :     ulong mm = m[2];
    1731    44936668 :     return utoi( Fl_mul(umodiu(a, mm), b, mm) );
    1732             :   } else {
    1733    33713855 :     pari_sp av = avma;
    1734             :     GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1735    33713855 :     (void)new_chunk(lg(a)+1+(l<<1));
    1736    33670674 :     p = muliu(a,b);
    1737    31950314 :     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    12763385 : Fp_inv(GEN a, GEN m)
    1765             : {
    1766             :   GEN res;
    1767    12763385 :   if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
    1768    12762857 :   return res;
    1769             : }
    1770             : INLINE GEN
    1771      344849 : Fp_invsafe(GEN a, GEN m)
    1772             : {
    1773             :   GEN res;
    1774      344849 :   if (! invmod(a,m,&res)) return NULL;
    1775      344849 :   return res;
    1776             : }
    1777             : INLINE GEN
    1778     1499389 : Fp_div(GEN a, GEN b, GEN m)
    1779             : {
    1780     1499389 :   pari_sp av = avma;
    1781             :   GEN p;
    1782     1499389 :   if (lgefint(b) == 3)
    1783             :   {
    1784      526402 :     a = Fp_divu(a, b[2], m);
    1785      526402 :     if (signe(b) < 0) a = Fp_neg(a, m);
    1786      526402 :     return a;
    1787             :   }
    1788             :   /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1789      972987 :   (void)new_chunk(lg(a)+(lg(m)<<1));
    1790      972987 :   p = mulii(a, Fp_inv(b,m));
    1791      972987 :   set_avma(av); return modii(p,m);
    1792             : }
    1793             : INLINE GEN
    1794     1691832 : Fp_divu(GEN x, ulong a, GEN p)
    1795             : {
    1796     1691832 :   pari_sp av = avma;
    1797             :   ulong b;
    1798     1691832 :   if (lgefint(p) == 3)
    1799             :   {
    1800     1393299 :     ulong pp = p[2], xp = umodiu(x, pp);
    1801     1393299 :     return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
    1802             :   }
    1803      298533 :   x = Fp_red(x, p);
    1804      298533 :   b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
    1805      298536 :   return gerepileuptoint(av, diviuexact(addmuliu(x, p, b), a));
    1806             : }
    1807             : 
    1808             : INLINE GEN
    1809     1103857 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
    1810             : 
    1811             : INLINE GEN
    1812     1749275 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1813             : 
    1814             : INLINE long
    1815     2259753 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
    1816             : 
    1817             : INLINE long
    1818     1903710 : 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     9081350 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1831             : 
    1832             : INLINE long
    1833    24303357 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
    1834             : 
    1835             : INLINE long
    1836    42818309 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1837             : 
    1838             : INLINE GEN
    1839        6850 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1840             : 
    1841             : INLINE long
    1842      240670 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1843             : 
    1844             : INLINE long
    1845      296462 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1846             : 
    1847             : INLINE GEN
    1848     1635131 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1849             : 
    1850             : INLINE long
    1851     4190869 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1852             : 
    1853             : INLINE long
    1854     3255915 : 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     1015367 : submulii(GEN x, GEN y, GEN z)
    1873             : {
    1874     1015367 :   long lx = lgefint(x), ly, lz;
    1875             :   pari_sp av;
    1876             :   GEN t;
    1877     1015367 :   if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
    1878      860056 :   ly = lgefint(y);
    1879      860056 :   if (ly == 2) return icopy(x);
    1880      703724 :   lz = lgefint(z);
    1881      703724 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1882      703724 :   t = mulii(z, y);
    1883      703724 :   set_avma(av); return subii(x,t);
    1884             : }
    1885             : /* y*z - x */
    1886             : INLINE GEN
    1887     1302306 : mulsubii(GEN y, GEN z, GEN x)
    1888             : {
    1889     1302306 :   long lx = lgefint(x), ly, lz;
    1890             :   pari_sp av;
    1891             :   GEN t;
    1892     1302306 :   if (lx == 2) return mulii(z,y);
    1893      782774 :   ly = lgefint(y);
    1894      782774 :   if (ly == 2) return negi(x);
    1895      714457 :   lz = lgefint(z);
    1896      714457 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1897      714457 :   t = mulii(z, y);
    1898      714457 :   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      306492 : addmuliu(GEN x, GEN y, ulong u)
    1916             : {
    1917             :   pari_sp av;
    1918      306492 :   long ly = lgefint(y);
    1919      306492 :   if (ly == 2) return icopy(x);
    1920      306492 :   av = avma;
    1921      306492 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1922      306492 :   y = mului(u,y);
    1923      306487 :   set_avma(av); return addii(x, y);
    1924             : }
    1925             : /* x - u*y */
    1926             : INLINE GEN
    1927   199606811 : submuliu_inplace(GEN x, GEN y, ulong u)
    1928             : {
    1929             :   pari_sp av;
    1930   199606811 :   long ly = lgefint(y);
    1931   199606811 :   if (ly == 2) return x;
    1932   162750239 :   av = avma;
    1933   162750239 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1934   162750249 :   y = mului(u,y);
    1935   162750177 :   set_avma(av); return subii(x, y);
    1936             : }
    1937             : /* x + u*y */
    1938             : INLINE GEN
    1939   196847839 : addmuliu_inplace(GEN x, GEN y, ulong u)
    1940             : {
    1941             :   pari_sp av;
    1942   196847839 :   long ly = lgefint(y);
    1943   196847839 :   if (ly == 2) return x;
    1944   161670188 :   av = avma;
    1945   161670188 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1946   161670218 :   y = mului(u,y);
    1947   161670146 :   set_avma(av); return addii(x, y);
    1948             : }
    1949             : /* ux + vy */
    1950             : INLINE GEN
    1951    24371799 : lincombii(GEN u, GEN v, GEN x, GEN y)
    1952             : {
    1953    24371799 :   long lx = lgefint(x), ly;
    1954             :   GEN p1, p2;
    1955             :   pari_sp av;
    1956    24371799 :   if (lx == 2) return mulii(v,y);
    1957    16311224 :   ly = lgefint(y);
    1958    16311224 :   if (ly == 2) return mulii(u,x);
    1959    15025900 :   av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
    1960    15026664 :   p1 = mulii(u,x);
    1961    15026548 :   p2 = mulii(v,y);
    1962    15026544 :   set_avma(av); return addii(p1,p2);
    1963             : }
    1964             : 
    1965             : /*******************************************************************/
    1966             : /*                                                                 */
    1967             : /*                          GEN SUBTYPES                           */
    1968             : /*                                                                 */
    1969             : /*******************************************************************/
    1970             : 
    1971             : INLINE int
    1972  1719758413 : is_const_t(long t) { return (t < t_POLMOD); }
    1973             : INLINE int
    1974        6811 : is_extscalar_t(long t) { return (t <= t_POL); }
    1975             : INLINE int
    1976     1359189 : is_intreal_t(long t) { return (t <= t_REAL); }
    1977             : INLINE int
    1978   467626130 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
    1979             : INLINE int
    1980    72297867 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
    1981             : INLINE int
    1982     1605897 : 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     1105823 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
    1987             : INLINE int
    1988  4165751155 : is_recursive_t(long t) { return lontyp[t]; }
    1989             : INLINE int
    1990   179485994 : is_scalar_t(long t) { return (t < t_POL); }
    1991             : INLINE int
    1992     1054695 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
    1993             : 
    1994             : /*******************************************************************/
    1995             : /*                                                                 */
    1996             : /*                         TRANSCENDENTAL                          */
    1997             : /*                                                                 */
    1998             : /*******************************************************************/
    1999             : INLINE GEN
    2000    24746386 : sqrtr(GEN x) {
    2001    24746386 :   long s = signe(x);
    2002    24746386 :   if (s == 0) return real_0_bit(expo(x) >> 1);
    2003    24713150 :   if (s >= 0) return sqrtr_abs(x);
    2004       43762 :   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      767674 : sqrtnr(GEN x, long n) {
    2019      767674 :   long s = signe(x);
    2020             :   GEN r;
    2021      767674 :   if (s == 0) return real_0_bit(expo(x) / n);
    2022      767674 :   r = sqrtnr_abs(x, n);
    2023      767674 :   if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
    2024      767674 :   return r;
    2025             : }
    2026             : INLINE long
    2027      266248 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
    2028             : INLINE ulong
    2029     2019389 : ulogint(ulong B, ulong y)
    2030             : {
    2031             :   ulong r;
    2032             :   long e;
    2033     2019389 :   if (y == 2) return expu(B);
    2034     2000363 :   r = y;
    2035     2000363 :   for (e=1;; e++)
    2036             :   { /* here, r = y^e, r2 = y^(e-1) */
    2037     6438718 :     if (r >= B) return r == B? e: e-1;
    2038     4438354 :     r = umuluu_or_0(y, r);
    2039     4438356 :     if (!r) return e;
    2040             :   }
    2041             : }
    2042             : 
    2043             : /*******************************************************************/
    2044             : /*                                                                 */
    2045             : /*                         MISCELLANEOUS                           */
    2046             : /*                                                                 */
    2047             : /*******************************************************************/
    2048     1359112 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
    2049   852232635 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
    2050    11178319 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
    2051     2001905 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
    2052   751115162 : INLINE int equali1(GEN n)
    2053   751115162 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
    2054    61492650 : INLINE int equalim1(GEN n)
    2055    61492650 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
    2056             : /* works only for POSITIVE integers */
    2057  1360176646 : INLINE int is_pm1(GEN n)
    2058  1360176646 : { return lgefint(n) == 3 && n[2] == 1; }
    2059   282386229 : INLINE int is_bigint(GEN n)
    2060   282386229 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
    2061             : 
    2062  2162468898 : INLINE int odd(long x) { return x & 1; }
    2063    30293291 : INLINE int both_odd(long x, long y) { return x & y & 1; }
    2064             : 
    2065             : INLINE int
    2066  3966482089 : isonstack(GEN x)
    2067  3966482089 : { return ((pari_sp)x >= pari_mainstack->bot
    2068  3966482089 :        && (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    27175533 : dbllog2r(GEN x)
    2073    27175533 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
    2074             : 
    2075             : INLINE GEN
    2076      432106 : mul_content(GEN cx, GEN cy)
    2077             : {
    2078      432106 :   if (!cx) return cy;
    2079      158407 :   if (!cy) return cx;
    2080      104171 :   return gmul(cx,cy);
    2081             : }
    2082             : INLINE GEN
    2083        6139 : inv_content(GEN c) { return c? ginv(c): NULL; }
    2084             : INLINE GEN
    2085        7777 : div_content(GEN cx, GEN cy)
    2086             : {
    2087        7777 :   if (!cy) return cx;
    2088        7574 :   if (!cx) return ginv(cy);
    2089        3772 :   return gdiv(cx,cy);
    2090             : }
    2091             : INLINE GEN
    2092     2929872 : mul_denom(GEN dx, GEN dy)
    2093             : {
    2094     2929872 :   if (!dx) return dy;
    2095     1311428 :   if (!dy) return dx;
    2096      458293 :   return mulii(dx,dy);
    2097             : }
    2098             : 
    2099             : /* POLYNOMIALS */
    2100             : INLINE GEN
    2101      839021 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
    2102             : INLINE GEN
    2103    52150092 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
    2104             : INLINE ulong
    2105      505527 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
    2106             : INLINE ulong
    2107       15164 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
    2108             : INLINE long
    2109  2872295236 : degpol(GEN x) { return lg(x)-3; }
    2110             : INLINE long
    2111  1552037141 : lgpol(GEN x) { return lg(x)-2; }
    2112             : INLINE long
    2113   149583512 : lgcols(GEN x) { return lg(gel(x,1)); }
    2114             : INLINE long
    2115    79914545 : nbrows(GEN x) { return lg(gel(x,1))-1; }
    2116             : INLINE GEN
    2117           0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
    2118             : 
    2119             : INLINE GEN
    2120      712889 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
    2121             : INLINE GEN
    2122      317371 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
    2123             : 
    2124             : INLINE GEN
    2125    92358529 : RgX_copy(GEN x)
    2126             : {
    2127             :   long lx, i;
    2128    92358529 :   GEN y = cgetg_copy(x, &lx); y[1] = x[1];
    2129   334753237 :   for (i = 2; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    2130    92358530 :   return y;
    2131             : }
    2132             : /* have to use ulong to avoid silly warnings from gcc "assuming signed
    2133             :  * overflow does not occur" */
    2134             : INLINE GEN
    2135     1083635 : RgX_coeff(GEN x, long n)
    2136             : {
    2137     1083635 :   ulong l = lg(x);
    2138     1083635 :   return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
    2139             : }
    2140             : INLINE GEN
    2141      369316 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
    2142             : INLINE GEN
    2143     6813000 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
    2144             : INLINE GEN
    2145        1792 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
    2146             : INLINE GEN
    2147      102927 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
    2148             : INLINE GEN
    2149     1137867 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
    2150             : INLINE GEN
    2151     6320450 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
    2152             : INLINE GEN
    2153      856294 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
    2154             : INLINE GEN
    2155           0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
    2156             : INLINE GEN
    2157    56745615 : pol0_Flx(long sv) { return mkvecsmall(sv); }
    2158             : INLINE GEN
    2159    18432827 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
    2160             : INLINE GEN
    2161    19243183 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
    2162             : INLINE GEN
    2163           0 : zero_zx(long sv) { return zero_Flx(sv); }
    2164             : INLINE GEN
    2165           0 : polx_zx(long sv) { return polx_Flx(sv); }
    2166             : INLINE GEN
    2167           0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
    2168             : INLINE GEN
    2169           0 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
    2170             : INLINE GEN
    2171        1341 : zero_F2x(long sv) { return zero_Flx(sv); }
    2172             : INLINE GEN
    2173    11249855 : pol0_F2x(long sv) { return pol0_Flx(sv); }
    2174             : INLINE GEN
    2175     2841379 : pol1_F2x(long sv) { return pol1_Flx(sv); }
    2176             : INLINE GEN
    2177      704723 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
    2178             : INLINE int
    2179      518228 : F2x_equal1(GEN x) { return Flx_equal1(x); }
    2180             : INLINE int
    2181     3044665 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
    2182             : INLINE GEN
    2183    54780601 : F2x_copy(GEN x) { return leafcopy(x); }
    2184             : INLINE GEN
    2185        3535 : F2v_copy(GEN x) { return leafcopy(x); }
    2186             : INLINE GEN
    2187     2279228 : Flv_copy(GEN x) { return leafcopy(x); }
    2188             : INLINE GEN
    2189   107949454 : Flx_copy(GEN x) { return leafcopy(x); }
    2190             : INLINE GEN
    2191     1980535 : vecsmall_copy(GEN x) { return leafcopy(x); }
    2192             : INLINE int
    2193     4647575 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
    2194             : INLINE int
    2195       18632 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
    2196             : INLINE int
    2197     6390163 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
    2198             : 
    2199             : INLINE GEN
    2200    65355990 : ZX_renormalize(GEN x, long lx)    { return ZXX_renormalize(x,lx); }
    2201             : INLINE GEN
    2202    74129304 : FpX_renormalize(GEN x, long lx)   { return ZXX_renormalize(x,lx); }
    2203             : INLINE GEN
    2204      675481 : FpXX_renormalize(GEN x, long lx)  { return ZXX_renormalize(x,lx); }
    2205             : INLINE GEN
    2206     1478580 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
    2207             : INLINE GEN
    2208   108989226 : F2x_renormalize(GEN x, long lx)   { return Flx_renormalize(x,lx); }
    2209             : INLINE GEN
    2210       32977 : F2v_to_F2x(GEN x, long sv) {
    2211       32977 :   GEN y = leafcopy(x);
    2212       32977 :   y[1] = sv; F2x_renormalize(y, lg(y)); return y;
    2213             : }
    2214             : 
    2215             : INLINE long
    2216         322 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
    2217             : 
    2218             : INLINE long
    2219        4844 : gval(GEN x, long v)
    2220        4844 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
    2221             : 
    2222             : INLINE void
    2223      472989 : RgX_shift_inplace_init(long v)
    2224      472989 : { if (v) (void)cgetg(v, t_VECSMALL); }
    2225             : /* shift polynomial in place. assume v free cells have been left before x */
    2226             : INLINE GEN
    2227      472989 : RgX_shift_inplace(GEN x, long v)
    2228             : {
    2229             :   long i, lx;
    2230             :   GEN z;
    2231      472989 :   if (!v) return x;
    2232      253165 :   lx = lg(x);
    2233      253165 :   if (lx == 2) return x;
    2234      253165 :   z = x + lx;
    2235             :   /* stackdummy's from normalizepol */
    2236      253165 :   while (lg(z) != v) z += lg(z);
    2237      253165 :   z += v;
    2238     4654452 :   for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
    2239      675883 :   for (i = 0;  i < v; i++) gel(--z,0) = gen_0;
    2240      253165 :   z -= 2;
    2241      253165 :   z[1] = x[1];
    2242      253165 :   z[0] = evaltyp(t_POL) | evallg(lx+v);
    2243      253165 :   stackdummy((pari_sp)z, (pari_sp)x); return z;
    2244             : }
    2245             : 
    2246             : 
    2247             : /* LINEAR ALGEBRA */
    2248             : INLINE GEN
    2249      138565 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
    2250             : INLINE GEN
    2251     5345086 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
    2252             : INLINE GEN
    2253     3305370 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
    2254             : INLINE GEN
    2255           0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
    2256             : INLINE GEN
    2257           0 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
    2258             : INLINE GEN
    2259           0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
    2260             : INLINE GEN
    2261           0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
    2262             : INLINE GEN
    2263    17845701 : zero_zv(long x) { return zero_Flv(x); }
    2264             : INLINE GEN
    2265         238 : zm_transpose(GEN x) { return Flm_transpose(x); }
    2266             : INLINE GEN
    2267           0 : zm_copy(GEN x) { return Flm_copy(x); }
    2268             : INLINE GEN
    2269     1905012 : zv_copy(GEN x) { return Flv_copy(x); }
    2270             : INLINE GEN
    2271           0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
    2272             : 
    2273             : INLINE GEN
    2274     1735433 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfremdiv(x,y,NULL); }
    2275             : INLINE GEN
    2276       42166 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
    2277             : INLINE GEN
    2278     2742189 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
    2279             : INLINE void
    2280     6081486 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
    2281             : INLINE GEN
    2282    10938958 : RgM_shallowcopy(GEN x)
    2283             : {
    2284             :   long l;
    2285    10938958 :   GEN y = cgetg_copy(x, &l);
    2286    54640294 :   while (--l > 0) gel(y,l) = leafcopy(gel(x,l));
    2287    10938945 :   return y;
    2288             : }
    2289             : INLINE GEN
    2290       22257 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
    2291             : 
    2292             : INLINE GEN
    2293     1749680 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
    2294             : 
    2295             : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
    2296             :  * x,y are ZV of the same length */
    2297             : INLINE int
    2298       23198 : ZV_dvd(GEN x, GEN y)
    2299             : {
    2300       23198 :   long i, l = lg(x);
    2301       36666 :   for (i=1; i < l; i++)
    2302       29533 :     if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
    2303        7133 :   return 1;
    2304             : }
    2305             : 
    2306             : /* Fq */
    2307             : INLINE GEN
    2308     2501599 : Fq_red(GEN x, GEN T, GEN p)
    2309     2501599 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
    2310             : INLINE GEN
    2311       99889 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
    2312             : {
    2313             :   (void) p;
    2314       99889 :   return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
    2315             : }
    2316             : INLINE GEN
    2317         756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
    2318             : 
    2319             : INLINE GEN
    2320         805 : gener_Fq_local(GEN T, GEN p, GEN L)
    2321           0 : { return T? gener_FpXQ_local(T,p, L)
    2322         805 :           : pgener_Fp_local(p, L); }
    2323             : 
    2324             : /* FpXQX */
    2325             : INLINE GEN
    2326        4424 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
    2327             : INLINE GEN
    2328       44582 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
    2329             : INLINE GEN
    2330       33334 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
    2331             : 
    2332             : INLINE GEN
    2333       20006 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    2334       20006 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
    2335             : 
    2336             : /* FqX */
    2337             : INLINE GEN
    2338       25067 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
    2339             : INLINE GEN
    2340      105343 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2341             : INLINE GEN
    2342       24313 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
    2343             : INLINE GEN
    2344       66458 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2345             : INLINE GEN
    2346      211973 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
    2347      211973 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
    2348             : INLINE GEN
    2349      100019 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
    2350      100019 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
    2351             : INLINE GEN
    2352      168372 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
    2353      168372 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
    2354             : INLINE GEN
    2355       17594 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
    2356       17594 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
    2357             : INLINE GEN
    2358       69678 : FqX_sqr(GEN x, GEN T, GEN p)
    2359       69678 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
    2360             : INLINE GEN
    2361        1281 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
    2362        1281 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
    2363             : INLINE GEN
    2364       18774 : FqX_halve(GEN x, GEN T, GEN p)
    2365       18774 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
    2366             : INLINE GEN
    2367        6328 : FqX_div(GEN x, GEN y, GEN T, GEN p)
    2368        6328 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
    2369             : INLINE GEN
    2370        6081 : FqX_get_red(GEN S, GEN T, GEN p)
    2371        6081 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
    2372             : INLINE GEN
    2373       31086 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
    2374       31086 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
    2375             : INLINE GEN
    2376           0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2377           0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
    2378             : INLINE GEN
    2379        5936 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2380        5936 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
    2381             : INLINE GEN
    2382           0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
    2383           0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
    2384             : INLINE GEN
    2385      260113 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
    2386      260113 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
    2387             : INLINE GEN
    2388      111213 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
    2389      111213 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
    2390             : INLINE GEN
    2391        3570 : FqX_normalize(GEN z, GEN T, GEN p)
    2392        3570 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
    2393             : INLINE GEN
    2394      291893 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
    2395             : INLINE GEN
    2396           0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
    2397             : INLINE GEN
    2398       76279 : FqX_factor(GEN f, GEN T, GEN p)
    2399       76279 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
    2400             : INLINE GEN
    2401           7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
    2402           7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
    2403             : INLINE GEN
    2404           7 : FqX_ddf(GEN f, GEN T, GEN p)
    2405           7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
    2406             : INLINE GEN
    2407        1862 : FqX_degfact(GEN f, GEN T, GEN p)
    2408        1862 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
    2409             : INLINE GEN
    2410        7147 : FqX_roots(GEN f, GEN T, GEN p)
    2411        7147 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
    2412             : INLINE GEN
    2413         175 : FqX_to_mod(GEN f, GEN T, GEN p)
    2414         175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
    2415             : 
    2416             : /*FqXQ*/
    2417             : INLINE GEN
    2418           0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2419           0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2420             : INLINE GEN
    2421           0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2422           0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2423             : INLINE GEN
    2424           0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
    2425           0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
    2426             : INLINE GEN
    2427           0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
    2428           0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
    2429             : INLINE GEN
    2430           0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
    2431           0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
    2432             : INLINE GEN
    2433       22320 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
    2434       22320 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
    2435             : INLINE GEN
    2436           0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
    2437           0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
    2438             : INLINE GEN
    2439           0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    2440           0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
    2441             : 
    2442             : /*FqXn*/
    2443             : INLINE GEN
    2444        6610 : FqXn_expint(GEN x, long n, GEN T, GEN p)
    2445        6610 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
    2446             : INLINE GEN
    2447           0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
    2448           0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
    2449             : INLINE GEN
    2450        6916 : FqXn_inv(GEN x, long n, GEN T, GEN p)
    2451        6916 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
    2452             : INLINE GEN
    2453      254926 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
    2454      254926 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
    2455             : INLINE GEN
    2456           0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
    2457           0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
    2458             : 
    2459             : /*FpXQ*/
    2460             : INLINE GEN
    2461           0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2462           0 : { (void)T; return FpX_add(x,y,p); }
    2463             : INLINE GEN
    2464           0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2465           0 : { (void)T; return FpX_sub(x,y,p); }
    2466             : 
    2467             : /*Flxq*/
    2468             : INLINE GEN
    2469           0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2470           0 : { (void)T; return Flx_add(x,y,p); }
    2471             : INLINE GEN
    2472           0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2473           0 : { (void)T; return Flx_sub(x,y,p); }
    2474             : 
    2475             : /* F2x */
    2476             : 
    2477             : INLINE ulong
    2478    65085478 : F2x_coeff(GEN x,long v)
    2479             : {
    2480    65085478 :    ulong u=(ulong)x[2+divsBIL(v)];
    2481    65084028 :    return (u>>remsBIL(v))&1UL;
    2482             : }
    2483             : 
    2484             : INLINE void
    2485     5497073 : F2x_clear(GEN x,long v)
    2486             : {
    2487     5497073 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2488     5497073 :    *u&=~(1UL<<remsBIL(v));
    2489     5497074 : }
    2490             : 
    2491             : INLINE void
    2492    38507895 : F2x_set(GEN x,long v)
    2493             : {
    2494    38507895 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2495    38495245 :    *u|=1UL<<remsBIL(v);
    2496    38485916 : }
    2497             : 
    2498             : INLINE void
    2499     1508964 : F2x_flip(GEN x,long v)
    2500             : {
    2501     1508964 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2502     1508964 :    *u^=1UL<<remsBIL(v);
    2503     1508964 : }
    2504             : 
    2505             : /* F2v */
    2506             : 
    2507             : INLINE ulong
    2508    58280646 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
    2509             : 
    2510             : INLINE void
    2511     5497073 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
    2512             : 
    2513             : INLINE void
    2514    13446679 : F2v_set(GEN x,long v)   { F2x_set(x,v-1); }
    2515             : 
    2516             : INLINE void
    2517     1508964 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
    2518             : 
    2519             : /* F2m */
    2520             : 
    2521             : INLINE ulong
    2522     4799612 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
    2523             : 
    2524             : INLINE void
    2525           0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
    2526             : 
    2527             : INLINE void
    2528           0 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
    2529             : 
    2530             : INLINE void
    2531     1508964 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
    2532             : 
    2533             : /* ARITHMETIC */
    2534             : INLINE GEN
    2535        2849 : matpascal(long n) { return matqpascal(n, NULL); }
    2536             : INLINE long
    2537     2342773 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
    2538             : INLINE long
    2539          14 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
    2540             : INLINE GEN
    2541     4386149 : sqrti(GEN x) { return sqrtremi(x,NULL); }
    2542             : INLINE GEN
    2543     6001800 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
    2544             : INLINE int
    2545       74192 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
    2546             : INLINE int
    2547       24549 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
    2548             : INLINE GEN
    2549           0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
    2550             : INLINE GEN
    2551           0 : gminsg(long s, GEN y) { return gmings(y,s); }
    2552             : INLINE GEN
    2553    34798721 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
    2554             : INLINE GEN
    2555     1083547 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
    2556             : INLINE GEN
    2557      204231 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
    2558             : 
    2559             : INLINE GEN
    2560     9672125 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
    2561             : INLINE GEN
    2562      264159 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
    2563             : 
    2564             : /* x t_COMPLEX */
    2565             : INLINE GEN
    2566    22781186 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
    2567             : /* q t_QUAD */
    2568             : INLINE GEN
    2569         840 : quadnorm(GEN q)
    2570             : {
    2571         840 :   GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
    2572         840 :   GEN z, u = gel(q,3), v = gel(q,2);
    2573         840 :   if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
    2574             :   {
    2575         651 :     z = signe(b)? mulii(v, addii(u,v)): sqri(v);
    2576         651 :     return addii(z, mulii(c, sqri(u)));
    2577             :   }
    2578             :   else
    2579             :   {
    2580         189 :     z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
    2581         189 :     return gadd(z, gmul(c, gsqr(u)));
    2582             :   }
    2583             : }
    2584             : /* x a t_QUAD, return the attached discriminant */
    2585             : INLINE GEN
    2586        1001 : quad_disc(GEN x)
    2587             : {
    2588        1001 :   GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
    2589        1001 :   if (is_pm1(b)) return subsi(1, c4);
    2590         147 :   togglesign_safe(&c4); return c4;
    2591             : }
    2592             : INLINE GEN
    2593     5633278 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
    2594             : INLINE GEN
    2595     5556558 : qfb_disc(GEN x) { return qfb_disc3(gel(x,1), gel(x,2), gel(x,3)); }
    2596             : 
    2597             : INLINE GEN
    2598     1644302 : sqrfrac(GEN x)
    2599             : {
    2600     1644302 :   GEN z = cgetg(3,t_FRAC);
    2601     1644302 :   gel(z,1) = sqri(gel(x,1));
    2602     1644302 :   gel(z,2) = sqri(gel(x,2)); return z;
    2603             : }
    2604             : 
    2605             : INLINE void
    2606     9220204 : normalize_frac(GEN z) {
    2607     9220204 :   if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
    2608     9220204 : }
    2609             : 
    2610             : INLINE GEN
    2611      356609 : powii(GEN x, GEN n)
    2612             : {
    2613      356609 :   long ln = lgefint(n);
    2614      356609 :   if (ln == 3) {
    2615             :     GEN z;
    2616      352108 :     if (signe(n) > 0) return powiu(x, n[2]);
    2617        8484 :     z = cgetg(3, t_FRAC);
    2618        8484 :     gel(z,1) = gen_1;
    2619        8484 :     gel(z,2) = powiu(x, n[2]);
    2620        8484 :     return z;
    2621             :   }
    2622        4501 :   if (ln == 2) return gen_1; /* rare */
    2623             :   /* should never happen */
    2624           0 :   return powgi(x, n); /* overflow unless x = 0, 1, -1 */
    2625             : }
    2626             : INLINE GEN
    2627        1498 : powIs(long n) {
    2628        1498 :   switch(n & 3)
    2629             :   {
    2630          63 :     case 1: return mkcomplex(gen_0,gen_1);
    2631         385 :     case 2: return gen_m1;
    2632         707 :     case 3: return mkcomplex(gen_0,gen_m1);
    2633             :   }
    2634         343 :   return gen_1;
    2635             : }
    2636             : 
    2637             : /*******************************************************************/
    2638             : /*                                                                 */
    2639             : /*                             ASSIGNMENTS                         */
    2640             : /*                                                                 */
    2641             : /*******************************************************************/
    2642           0 : INLINE void mpexpz(GEN x, GEN z)
    2643           0 : { pari_sp av = avma; gaffect(mpexp(x), z); set_avma(av); }
    2644           0 : INLINE void mplogz(GEN x, GEN z)
    2645           0 : { pari_sp av = avma; gaffect(mplog(x), z); set_avma(av); }
    2646           0 : INLINE void mpcosz(GEN x, GEN z)
    2647           0 : { pari_sp av = avma; gaffect(mpcos(x), z); set_avma(av); }
    2648           0 : INLINE void mpsinz(GEN x, GEN z)
    2649           0 : { pari_sp av = avma; gaffect(mpsin(x), z); set_avma(av); }
    2650           0 : INLINE void gnegz(GEN x, GEN z)
    2651           0 : { pari_sp av = avma; gaffect(gneg(x), z); set_avma(av); }
    2652           0 : INLINE void gabsz(GEN x, long prec, GEN z)
    2653           0 : { pari_sp av = avma; gaffect(gabs(x,prec), z); set_avma(av); }
    2654           0 : INLINE void gaddz(GEN x, GEN y, GEN z)
    2655           0 : { pari_sp av = avma; gaffect(gadd(x,y), z); set_avma(av); }
    2656           0 : INLINE void gsubz(GEN x, GEN y, GEN z)
    2657           0 : { pari_sp av = avma; gaffect(gsub(x,y), z); set_avma(av); }
    2658           0 : INLINE void gmulz(GEN x, GEN y, GEN z)
    2659           0 : { pari_sp av = avma; gaffect(gmul(x,y), z); set_avma(av); }
    2660           0 : INLINE void gdivz(GEN x, GEN y, GEN z)
    2661           0 : { pari_sp av = avma; gaffect(gdiv(x,y), z); set_avma(av); }
    2662           0 : INLINE void gdiventz(GEN x, GEN y, GEN z)
    2663           0 : { pari_sp av = avma; gaffect(gdivent(x,y), z); set_avma(av); }
    2664           0 : INLINE void gmodz(GEN x, GEN y, GEN z)
    2665           0 : { pari_sp av = avma; gaffect(gmod(x,y), z); set_avma(av); }
    2666           0 : INLINE void gmul2nz(GEN x, long s, GEN z)
    2667           0 : { pari_sp av = avma; gaffect(gmul2n(x,s), z); set_avma(av); }
    2668           0 : INLINE void gshiftz(GEN x, long s, GEN z)
    2669           0 : { pari_sp av = avma; gaffect(gshift(x,s), z); set_avma(av); }
    2670             : 
    2671             : /*******************************************************************/
    2672             : /*                                                                 */
    2673             : /*                       ELLIPTIC CURVES                           */
    2674             : /*                                                                 */
    2675             : /*******************************************************************/
    2676     3986068 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
    2677     3099194 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
    2678     3867383 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
    2679     3892105 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
    2680     4708886 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
    2681     3324116 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
    2682     1027722 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
    2683     1633740 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
    2684     1464918 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
    2685     6232976 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
    2686     7470238 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
    2687     9726317 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
    2688     1031513 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
    2689    11494031 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
    2690     1571495 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
    2691      683573 : INLINE GEN ellff_get_a4a6(GEN x)  { return gmael(x, 15, 2); }
    2692        1407 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
    2693         287 : INLINE long ellQp_get_prec(GEN E) { GEN z = ellQp_get_zero(E); return valp(z); }
    2694        1085 : INLINE GEN ellQp_get_p(GEN E) { GEN z = ellQp_get_zero(E); return gel(z,2); }
    2695       37471 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
    2696       13684 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
    2697      711089 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
    2698          56 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
    2699             : 
    2700     4637534 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
    2701    34044137 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
    2702      808849 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
    2703             : 
    2704             : /*******************************************************************/
    2705             : /*                                                                 */
    2706             : /*                    ALGEBRAIC NUMBER THEORY                      */
    2707             : /*                                                                 */
    2708             : /*******************************************************************/
    2709     8474971 : INLINE GEN modpr_get_pr(GEN x)  { return gel(x,3); }
    2710      631281 : INLINE GEN modpr_get_p(GEN x)  { return pr_get_p(modpr_get_pr(x)); }
    2711     2541828 : INLINE GEN modpr_get_T(GEN x)  { return lg(x) == 4? NULL: gel(x,4); }
    2712             : 
    2713    37425933 : INLINE GEN pr_get_p(GEN pr)  { return gel(pr,1); }
    2714     4361884 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
    2715             : /* .[2] instead of itos works: e and f are small positive integers */
    2716     2853923 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
    2717    12794841 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
    2718    24564873 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
    2719             : INLINE int
    2720     3978123 : pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
    2721             : INLINE GEN
    2722     2233023 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
    2723             : INLINE ulong
    2724       97006 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
    2725             : 
    2726             : /* assume nf a genuine nf */
    2727             : INLINE long
    2728      395285 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
    2729             : INLINE GEN
    2730    37862017 : nf_get_pol(GEN nf) { return gel(nf,1); }
    2731             : INLINE long
    2732    30512645 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
    2733             : INLINE long
    2734     4458621 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
    2735             : INLINE long
    2736        3032 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
    2737             : INLINE GEN
    2738       62164 : nf_get_disc(GEN nf) { return gel(nf,3); }
    2739             : INLINE GEN
    2740      933693 : nf_get_index(GEN nf) { return gel(nf,4); }
    2741             : INLINE GEN
    2742     2845514 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
    2743             : INLINE GEN
    2744     1936098 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
    2745             : INLINE GEN
    2746     2350119 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
    2747             : INLINE GEN
    2748       16121 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
    2749             : INLINE GEN
    2750        2814 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
    2751             : INLINE GEN
    2752        2898 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
    2753             : INLINE GEN
    2754      397391 : nf_get_roots(GEN nf) { return gel(nf,6); }
    2755             : INLINE GEN
    2756          98 : nf_get_zk(GEN nf)
    2757             : {
    2758          98 :   GEN y = gel(nf,7), D = gel(y, 1);
    2759          98 :   if (typ(D) == t_POL) D = gel(D, 2);
    2760          98 :   if (!equali1(D)) y = gdiv(y, D);
    2761          98 :   return y;
    2762             : }
    2763             : INLINE GEN
    2764     1264559 : nf_get_zkprimpart(GEN nf)
    2765             : {
    2766     1264559 :   GEN y = gel(nf,7);
    2767             :   /* test for old format of nf.zk: non normalized */
    2768     1264559 :   if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
    2769     1264558 :   return y;
    2770             : }
    2771             : INLINE GEN
    2772     1265394 : nf_get_zkden(GEN nf)
    2773             : {
    2774     1265394 :   GEN y = gel(nf,7), D = gel(y,1);
    2775     1265394 :   if (typ(D) == t_POL) D = gel(D,2);
    2776             :   /* test for old format of nf.zk: non normalized */
    2777     1265394 :   if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
    2778     1265394 :   return D;
    2779             : }
    2780             : INLINE GEN
    2781     2889630 : nf_get_invzk(GEN nf) { return gel(nf,8); }
    2782             : INLINE void
    2783       23798 : nf_get_sign(GEN nf, long *r1, long *r2)
    2784             : {
    2785       23798 :   GEN x = gel(nf,2);
    2786       23798 :   *r1 = itou(gel(x,1));
    2787       23798 :   *r2 = itou(gel(x,2));
    2788       23798 : }
    2789             : 
    2790             : INLINE GEN
    2791      438716 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
    2792             : INLINE GEN
    2793       98757 : abgrp_get_no(GEN x) { return gel(x,1); }
    2794             : INLINE GEN
    2795     3027282 : abgrp_get_cyc(GEN x) { return gel(x,2); }
    2796             : INLINE GEN
    2797      390080 : abgrp_get_gen(GEN x) { return gel(x,3); }
    2798             : INLINE GEN
    2799     2407370 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
    2800             : INLINE GEN
    2801     1265414 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
    2802             : INLINE GEN
    2803        7364 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
    2804             : INLINE GEN
    2805      920428 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
    2806             : INLINE GEN
    2807      337608 : bnf_get_gen(GEN bnf)  { return abgrp_get_gen(bnf_get_clgp(bnf)); }
    2808             : INLINE GEN
    2809         713 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
    2810             : INLINE GEN
    2811      529914 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
    2812             : INLINE GEN
    2813        5425 : bnf_get_sunits(GEN bnf)
    2814        5425 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
    2815             : INLINE GEN
    2816       39166 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
    2817             : INLINE long
    2818       40649 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
    2819             : INLINE GEN
    2820       39004 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
    2821             : INLINE GEN
    2822        1687 : bnf_get_fu(GEN bnf) {
    2823        1687 :   GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
    2824             :   long i, l;
    2825        1687 :   if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
    2826        1687 :   l = lg(fu)-1; fu = vecslice(fu, 2, l);
    2827        3822 :   for (i = 1; i < l; i++) gel(fu,i) = nf_to_scalar_or_alg(nf, gel(fu,i));
    2828        1687 :   return fu;
    2829             : }
    2830             : 
    2831             : INLINE GEN
    2832      898098 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
    2833             : INLINE GEN
    2834      508689 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
    2835             : INLINE GEN
    2836       24402 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
    2837             : INLINE GEN
    2838      134855 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
    2839             : INLINE GEN
    2840      754732 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
    2841             : INLINE GEN
    2842       84665 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
    2843             : INLINE GEN
    2844      654065 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
    2845             : INLINE GEN
    2846          70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
    2847             : INLINE GEN
    2848        7812 : bnr_get_gen(GEN bnr) {
    2849        7812 :   GEN G = bnr_get_clgp(bnr);
    2850        7812 :   if (lg(G) !=  4)
    2851           0 :     pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
    2852        7812 :   return gel(G,3);
    2853             : }
    2854             : 
    2855             : INLINE GEN
    2856      302188 : bid_get_mod(GEN bid) { return gel(bid,1); }
    2857             : INLINE GEN
    2858       65030 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
    2859             : INLINE GEN
    2860        2695 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
    2861             : INLINE GEN
    2862     1297207 : bid_get_grp(GEN bid) { return gel(bid,2); }
    2863             : INLINE GEN
    2864      510557 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
    2865             : INLINE GEN
    2866      438744 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
    2867             : INLINE GEN
    2868      437813 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
    2869             : INLINE GEN
    2870       49322 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
    2871             : INLINE GEN
    2872      474360 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
    2873             : INLINE GEN
    2874      740267 : bid_get_U(GEN bid) { return gel(bid,5); }
    2875             : INLINE GEN
    2876           0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
    2877             : INLINE GEN
    2878     1248053 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
    2879             : INLINE GEN
    2880           0 : bid_get_gen_nocheck(GEN bid)  { return abgrp_get_gen(bid_get_grp(bid)); }
    2881             : INLINE GEN
    2882       48573 : bid_get_gen(GEN bid) {
    2883       48573 :   GEN G = bid_get_grp(bid);
    2884       48573 :   if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
    2885       48573 :   return abgrp_get_gen(G);
    2886             : }
    2887             : 
    2888             : INLINE GEN
    2889    26241313 : znstar_get_N(GEN G) { return gmael(G,1,1); }
    2890             : INLINE GEN
    2891     3216115 : znstar_get_faN(GEN G) { return gel(G,3); }
    2892             : INLINE GEN
    2893           7 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
    2894             : INLINE GEN
    2895      191107 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
    2896             : INLINE GEN
    2897           7 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
    2898             : INLINE GEN
    2899     3893225 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
    2900             : INLINE GEN
    2901     1386476 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
    2902             : INLINE GEN
    2903       45927 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
    2904             : INLINE GEN
    2905      120162 : znstar_get_U(GEN G) { return gel(G,5); }
    2906             : INLINE GEN
    2907      989856 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
    2908             : INLINE GEN
    2909       21623 : gal_get_pol(GEN gal) { return gel(gal,1); }
    2910             : INLINE GEN
    2911        2037 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
    2912             : INLINE GEN
    2913          84 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
    2914             : INLINE GEN
    2915       16793 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
    2916             : INLINE GEN
    2917       17906 : gal_get_roots(GEN gal) { return gel(gal,3); }
    2918             : INLINE GEN
    2919       16863 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
    2920             : INLINE GEN
    2921       16863 : gal_get_den(GEN gal) { return gel(gal,5); }
    2922             : INLINE GEN
    2923       69619 : gal_get_group(GEN gal) { return gel(gal,6); }
    2924             : INLINE GEN
    2925        1981 : gal_get_gen(GEN gal) { return gel(gal,7); }
    2926             : INLINE GEN
    2927        1624 : gal_get_orders(GEN gal) { return gel(gal,8); }
    2928             : 
    2929             : /* assume rnf a genuine rnf */
    2930             : INLINE long
    2931     1233932 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
    2932             : INLINE long
    2933       16688 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
    2934             : INLINE long
    2935      775705 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
    2936             : INLINE GEN
    2937         700 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
    2938             : INLINE GEN
    2939         749 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
    2940             : INLINE GEN
    2941         812 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
    2942             : INLINE GEN
    2943      425789 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
    2944             : INLINE GEN
    2945        3829 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
    2946             : INLINE GEN
    2947      210301 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
    2948             : INLINE GEN
    2949     1480220 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
    2950             : INLINE GEN
    2951         175 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
    2952             : INLINE GEN
    2953         105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
    2954             : INLINE GEN
    2955        1568 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
    2956             : INLINE long
    2957         231 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
    2958             : INLINE GEN
    2959      188538 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
    2960             : INLINE long
    2961        3332 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
    2962             : INLINE GEN
    2963        3507 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
    2964             : INLINE GEN
    2965      102711 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
    2966             : INLINE GEN
    2967        1197 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
    2968             : 
    2969             : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
    2970             :  * T2 matrix. Reduce I wrt G */
    2971             : INLINE GEN
    2972      352585 : idealpseudored(GEN I, GEN G)
    2973      352585 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
    2974             : 
    2975             : /* Same I, G; m in I with T2(m) small */
    2976             : INLINE GEN
    2977       84744 : idealpseudomin(GEN I, GEN G)
    2978             : {
    2979       84744 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2980       84744 :   return ZM_ZC_mul(I, gel(u,1));
    2981             : }
    2982             : /* Same I,G; irrational m in I with T2(m) small */
    2983             : INLINE GEN
    2984           0 : idealpseudomin_nonscalar(GEN I, GEN G)
    2985             : {
    2986           0 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2987           0 :   GEN m = ZM_ZC_mul(I, gel(u,1));
    2988           0 :   if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
    2989           0 :   return m;
    2990             : }
    2991             : /* Same I,G; t_VEC of irrational m in I with T2(m) small */
    2992             : INLINE GEN
    2993      330290 : idealpseudominvec(GEN I, GEN G)
    2994             : {
    2995      330290 :   long i, j, k, n = lg(I)-1;
    2996      330290 :   GEN x, L, b = idealpseudored(I, G);
    2997      330290 :   L = cgetg(1 + (n*(n+1))/2, t_VEC);
    2998     1288963 :   for (i = k = 1; i <= n; i++)
    2999             :   {
    3000      958673 :     x = gel(b,i);
    3001      958673 :     if (!ZV_isscalar(x)) gel(L,k++) = x;
    3002             :   }
    3003      958673 :   for (i = 2; i <= n; i++)
    3004     1691560 :     for (j = 1; j < i; j++)
    3005             :     {
    3006     1063177 :       x = ZC_add(gel(b,i),gel(b,j));
    3007     1063177 :       if (!ZV_isscalar(x)) gel(L,k++) = x;
    3008             :     }
    3009      330290 :   setlg(L,k); return L;
    3010             : }
    3011             : 
    3012             : INLINE GEN
    3013        1694 : idealred_elt(GEN nf, GEN I) {
    3014        1694 :   pari_sp av = avma;
    3015        1694 :   GEN u = idealpseudomin(I, nf_get_roundG(nf));
    3016        1694 :   return gerepileupto(av, u);
    3017             : }
    3018             : INLINE GEN
    3019       92021 : idealred(GEN nf, GEN I) { return idealred0(nf, I, NULL); }
    3020             : 
    3021             : INLINE GEN
    3022        2051 : idealchineseinit(GEN nf, GEN x)
    3023        2051 : { return idealchinese(nf,x,NULL); }
    3024             : 
    3025             : /*******************************************************************/
    3026             : /*                                                                 */
    3027             : /*                              CLOSURES                           */
    3028             : /*                                                                 */
    3029             : /*******************************************************************/
    3030   290260776 : INLINE long closure_arity(GEN C)          { return ((ulong)C[1])&ARITYBITS; }
    3031    35543805 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
    3032   250124537 : INLINE const char *closure_codestr(GEN C)  { return GSTR(gel(C,2))-1; }
    3033           0 : INLINE GEN closure_get_code(GEN C)  { return gel(C,2); }
    3034   250108466 : INLINE GEN closure_get_oper(GEN C)  { return gel(C,3); }
    3035   250083967 : INLINE GEN closure_get_data(GEN C)  { return gel(C,4); }
    3036       10534 : INLINE GEN closure_get_dbg(GEN C)   { return gel(C,5); }
    3037       24983 : INLINE GEN closure_get_text(GEN C)  { return gel(C,6); }
    3038     5492708 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
    3039             : 
    3040             : /*******************************************************************/
    3041             : /*                                                                 */
    3042             : /*                               ERRORS                            */
    3043             : /*                                                                 */
    3044             : /*******************************************************************/
    3045             : INLINE long
    3046       54707 : err_get_num(GEN e) { return e[1]; }
    3047             : INLINE GEN
    3048         105 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
    3049             : 
    3050             : INLINE void
    3051          14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
    3052             : INLINE void
    3053           7 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
    3054             : INLINE void
    3055          70 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
    3056             : INLINE void
    3057         529 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
    3058             : INLINE void
    3059           0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
    3060             : INLINE void
    3061          36 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
    3062             : INLINE void
    3063          91 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
    3064             : INLINE void
    3065         448 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
    3066             : INLINE void
    3067       19787 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
    3068             : INLINE void
    3069          49 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
    3070             : INLINE void
    3071        2162 : 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); }
    3072             : INLINE void
    3073         200 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
    3074             : INLINE void
    3075           0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
    3076             : INLINE void
    3077         364 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
    3078             : INLINE void
    3079         114 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
    3080             : INLINE void
    3081         225 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
    3082             : INLINE void
    3083           0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
    3084             : INLINE void
    3085          77 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
    3086             : INLINE void
    3087         749 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
    3088             : INLINE void
    3089          49 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
    3090             : INLINE void
    3091          84 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
    3092             : INLINE void
    3093       13046 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
    3094             : INLINE void
    3095        3388 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
    3096             : INLINE void
    3097         280 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
    3098             : INLINE void
    3099         196 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
    3100         196 : { pari_err(e_PRIORITY, f,x,op,v); }

Generated by: LCOV version 1.13