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) Coverage Total Hit
Test: PARI/GP v2.18.1 lcov report (development 31042-0fbe168e69) Lines: 92.6 % 1535 1422
Test Date: 2026-07-23 17:04:59 Functions: 92.1 % 707 651
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; either version 2 of the License, or (at your option) any later
       8              : version. It is distributed in the hope that it will be useful, but WITHOUT
       9              : ANY WARRANTY WHATSOEVER.
      10              : 
      11              : Check the License for details. You should have received a copy of it, along
      12              : with the package; see the file 'COPYING'. If not, write to the Free Software
      13              : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14              : 
      15              : 
      16              : /*********************************************************************/
      17              : /*                       MALLOC/FREE WRAPPERS                        */
      18              : /*********************************************************************/
      19              : #define BLOCK_SIGALRM_START          \
      20              : {                                    \
      21              :   int block=PARI_SIGINT_block;       \
      22              :   PARI_SIGINT_block = 2;             \
      23              :   MT_SIGINT_BLOCK(block);
      24              : 
      25              : #define BLOCK_SIGINT_START           \
      26              : {                                    \
      27              :   int block=PARI_SIGINT_block;       \
      28              :   PARI_SIGINT_block = 1;             \
      29              :   MT_SIGINT_BLOCK(block);
      30              : 
      31              : #define BLOCK_SIGINT_END             \
      32              :   PARI_SIGINT_block = block;         \
      33              :   MT_SIGINT_UNBLOCK(block);          \
      34              :   if (!block && PARI_SIGINT_pending) \
      35              :   {                                  \
      36              :     int sig = PARI_SIGINT_pending;   \
      37              :     PARI_SIGINT_pending = 0;         \
      38              :     raise(sig);                      \
      39              :   }                                  \
      40              : }
      41              : 
      42              : /*******************************************************************/
      43              : /*                                                                 */
      44              : /*                          CONSTRUCTORS                           */
      45              : /*                                                                 */
      46              : /*******************************************************************/
      47              : #define retmkfrac(x,y)\
      48              :   do { GEN _v = cgetg(3, t_FRAC);\
      49              :        gel(_v,1) = (x);\
      50              :        gel(_v,2) = (y); return _v; } while(0)
      51              : #define retmkrfrac(x,y)\
      52              :   do { GEN _v = cgetg(3, t_RFRAC);\
      53              :        gel(_v,1) = (x);\
      54              :        gel(_v,2) = (y); return _v; } while(0)
      55              : #define retmkintmod(x,y)\
      56              :   do { GEN _v = cgetg(3, t_INTMOD);\
      57              :        gel(_v,1) = (y);\
      58              :        gel(_v,2) = (x); return _v; } while(0)
      59              : #define retmkcomplex(x,y)\
      60              :   do { GEN _v = cgetg(3, t_COMPLEX);\
      61              :        gel(_v,1) = (x);\
      62              :        gel(_v,2) = (y); return _v; } while(0)
      63              : #define retmkpolmod(x,y)\
      64              :   do { GEN _v = cgetg(3, t_POLMOD);\
      65              :        gel(_v,1) = (y);\
      66              :        gel(_v,2) = (x); return _v; } while(0)
      67              : #define retmkvec(x)\
      68              :   do { GEN _v = cgetg(2, t_VEC);\
      69              :        gel(_v,1) = (x); return _v; } while(0)
      70              : #define retmkvec2(x,y)\
      71              :   do { GEN _v = cgetg(3, t_VEC);\
      72              :        gel(_v,1) = (x);\
      73              :        gel(_v,2) = (y); return _v; } while(0)
      74              : #define retmkvec3(x,y,z)\
      75              :   do { GEN _v = cgetg(4, t_VEC);\
      76              :        gel(_v,1) = (x);\
      77              :        gel(_v,2) = (y);\
      78              :        gel(_v,3) = (z); return _v; } while(0)
      79              : #define retmkpadic(x,p,pd,e,d)\
      80              :   do { GEN _v = cgetg(5, t_PADIC);\
      81              :        _v[1] = evalvalp(e) | evalprecp(d);\
      82              :        gel(_v,2) = (p);\
      83              :        gel(_v,3) = (pd);\
      84              :        gel(_v,4) = (x); return _v; } while(0)
      85              : /* x allowed to contain _pd */
      86              : #define retmkpadic_i(x,p,pd,e,d)\
      87              :   do { GEN _v = cgetg(5, t_PADIC), _pd = (pd);\
      88              :        _v[1] = evalvalp(e) | evalprecp(d);\
      89              :        gel(_v,2) = (p);\
      90              :        gel(_v,3) = (_pd);\
      91              :        gel(_v,4) = (x); return _v; } while(0)
      92              : #define retmkqfb(x,y,z,d)\
      93              :   do { GEN _v = cgetg(5, t_QFB);\
      94              :        gel(_v,1) = (x);\
      95              :        gel(_v,2) = (y);\
      96              :        gel(_v,3) = (z);\
      97              :        gel(_v,4) = (d); return _v; } while(0)
      98              : #define retmkquad(x,y,z)\
      99              :   do { GEN _v = cgetg(4, t_QUAD);\
     100              :        gel(_v,1) = (x);\
     101              :        gel(_v,2) = (y);\
     102              :        gel(_v,3) = (z); return _v; } while(0)
     103              : #define retmkvec4(x,y,z,t)\
     104              :   do { GEN _v = cgetg(5, t_VEC);\
     105              :        gel(_v,1) = (x);\
     106              :        gel(_v,2) = (y);\
     107              :        gel(_v,3) = (z);\
     108              :        gel(_v,4) = (t); return _v; } while(0)
     109              : #define retmkvec5(x,y,z,t,u)\
     110              :   do { GEN _v = cgetg(6, t_VEC);\
     111              :        gel(_v,1) = (x);\
     112              :        gel(_v,2) = (y);\
     113              :        gel(_v,3) = (z);\
     114              :        gel(_v,4) = (t);\
     115              :        gel(_v,5) = (u); return _v; } while(0)
     116              : #define retmkvec6(x,y,z,t,u,v)\
     117              :   do { GEN _v = cgetg(7, t_VEC);\
     118              :        gel(_v,1) = (x);\
     119              :        gel(_v,2) = (y);\
     120              :        gel(_v,3) = (z);\
     121              :        gel(_v,4) = (t);\
     122              :        gel(_v,5) = (u);\
     123              :        gel(_v,6) = (v); return _v; } while(0)
     124              : #define retmkcol(x)\
     125              :   do { GEN _v = cgetg(2, t_COL);\
     126              :        gel(_v,1) = (x); return _v; } while(0)
     127              : #define retmkcol2(x,y)\
     128              :   do { GEN _v = cgetg(3, t_COL);\
     129              :        gel(_v,1) = (x);\
     130              :        gel(_v,2) = (y); return _v; } while(0)
     131              : #define retmkcol3(x,y,z)\
     132              :   do { GEN _v = cgetg(4, t_COL);\
     133              :        gel(_v,1) = (x);\
     134              :        gel(_v,2) = (y);\
     135              :        gel(_v,3) = (z); return _v; } while(0)
     136              : #define retmkcol4(x,y,z,t)\
     137              :   do { GEN _v = cgetg(5, t_COL);\
     138              :        gel(_v,1) = (x);\
     139              :        gel(_v,2) = (y);\
     140              :        gel(_v,3) = (z);\
     141              :        gel(_v,4) = (t); return _v; } while(0)
     142              : #define retmkcol5(x,y,z,t,u)\
     143              :   do { GEN _v = cgetg(6, t_COL);\
     144              :        gel(_v,1) = (x);\
     145              :        gel(_v,2) = (y);\
     146              :        gel(_v,3) = (z);\
     147              :        gel(_v,4) = (t);\
     148              :        gel(_v,5) = (u); return _v; } while(0)
     149              : #define retmkcol6(x,y,z,t,u,v)\
     150              :   do { GEN _v = cgetg(7, t_COL);\
     151              :        gel(_v,1) = (x);\
     152              :        gel(_v,2) = (y);\
     153              :        gel(_v,3) = (z);\
     154              :        gel(_v,4) = (t);\
     155              :        gel(_v,5) = (u);\
     156              :        gel(_v,6) = (v); return _v; } while(0)
     157              : #define retmkmat(x)\
     158              :   do { GEN _v = cgetg(2, t_MAT);\
     159              :        gel(_v,1) = (x); return _v; } while(0)
     160              : #define retmkmat2(x,y)\
     161              :   do { GEN _v = cgetg(3, t_MAT);\
     162              :        gel(_v,1) = (x);\
     163              :        gel(_v,2) = (y); return _v; } while(0)
     164              : #define retmkmat3(x,y,z)\
     165              :   do { GEN _v = cgetg(4, t_MAT);\
     166              :        gel(_v,1) = (x);\
     167              :        gel(_v,2) = (y);\
     168              :        gel(_v,3) = (z); return _v; } while(0)
     169              : #define retmkmat4(x,y,z,t)\
     170              :   do { GEN _v = cgetg(5, t_MAT);\
     171              :        gel(_v,1) = (x);\
     172              :        gel(_v,2) = (y);\
     173              :        gel(_v,3) = (z);\
     174              :        gel(_v,4) = (t); return _v; } while(0)
     175              : #define retmkmat5(x,y,z,t,u)\
     176              :   do { GEN _v = cgetg(6, t_MAT);\
     177              :        gel(_v,1) = (x);\
     178              :        gel(_v,2) = (y);\
     179              :        gel(_v,3) = (z);\
     180              :        gel(_v,4) = (t);\
     181              :        gel(_v,5) = (u); return _v; } while(0)
     182              : #define retmkmat22(a,b,c,d)\
     183              :   do { GEN _v = cgetg(3, t_MAT);\
     184              :        gel(_v,1) = mkcol2(a,c);\
     185              :        gel(_v,2) = mkcol2(b,d); return _v; } while(0)
     186              : INLINE GEN
     187      8073833 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
     188              : INLINE GEN
     189         1834 : mkintmodu(ulong x, ulong y) {
     190         1834 :   GEN v = cgetg(3,t_INTMOD);
     191         1834 :   gel(v,1) = utoipos(y);
     192         1834 :   gel(v,2) = utoi(x); return v;
     193              : }
     194              : INLINE GEN
     195         2009 : mkpadic(GEN x, GEN p, GEN pd, long e, long d) { retmkpadic(x,p,pd,e,d); }
     196              : INLINE GEN
     197      3297284 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
     198              : INLINE GEN
     199     43050920 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
     200              : INLINE GEN
     201      1209605 : mkfracss(long x, long y) { retmkfrac(stoi(x),utoipos(y)); }
     202              : /* q = n/d a t_FRAC or t_INT; recover (n,d) */
     203              : INLINE void
     204        35364 : Qtoss(GEN q, long *n, long *d)
     205              : {
     206        35364 :   if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
     207         5824 :   else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
     208        35364 : }
     209              : INLINE GEN
     210      1097313 : sstoQ(long n, long d)
     211              : {
     212              :   ulong r;
     213              :   long g, q;
     214      1097313 :   if (!n)
     215              :   {
     216       117971 :     if (!d) pari_err_INV("sstoQ",gen_0);
     217       117971 :     return gen_0;
     218              :   }
     219       979342 :   if (d < 0) { d = -d; n = -n; }
     220       979342 :   if (d == 1) return stoi(n);
     221       960008 :   r = labs(n);
     222       960008 :   if (r == 1) retmkfrac(n > 0? gen_1: gen_m1, utoipos(d));
     223       909839 :   q = udivuu_rem(r, d, &r);
     224       909839 :   if (!r) return n > 0? utoipos(q): utoineg(q);
     225       854966 :   g = ugcd(d,r); /* gcd(n,d) */
     226       854966 :   if (g != 1) { n /= g; d /= g; }
     227       854966 :   retmkfrac(stoi(n), utoipos(d));
     228              : }
     229              : 
     230              : INLINE GEN
     231      1602256 : uutoQ(ulong n, ulong d)
     232              : {
     233              :   ulong r;
     234              :   long g, q;
     235      1602256 :   if (!n)
     236              :   {
     237          595 :     if (!d) pari_err_INV("uutoQ",gen_0);
     238          595 :     return gen_0;
     239              :   }
     240      1601661 :   if (d == 1) return utoipos(n);
     241      1590559 :   if (n == 1) retmkfrac(gen_1, utoipos(d));
     242      1501421 :   q = udivuu_rem(n,d,&r);
     243      1501421 :   if (!r) return utoipos(q);
     244      1259669 :   g = ugcd(d,r); /* gcd(n,d) */
     245      1259669 :   if (g != 1) { n /= g; d /= g; }
     246      1259669 :   retmkfrac(utoipos(n), utoipos(d));
     247              : }
     248              : 
     249              : INLINE GEN
     250      5129655 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
     251              : INLINE GEN
     252       698726 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
     253       698726 :   gel(v,1) = x; gel(v,2) = y; return v; }
     254              : INLINE GEN
     255           14 : mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
     256           14 :   gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
     257              : INLINE GEN
     258     92994806 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
     259              : INLINE GEN
     260      1269369 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
     261              : INLINE GEN
     262       976273 : cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
     263              : INLINE GEN
     264        69118 : mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
     265        69118 :   gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
     266              : /* vecsmall */
     267              : INLINE GEN
     268    143655003 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
     269              : INLINE GEN
     270    262029248 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
     271    262029248 :   v[1]=x; v[2]=y; return v; }
     272              : INLINE GEN
     273    106873679 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
     274    106873679 :   v[1]=x; v[2]=y; v[3]=z; return v; }
     275              : INLINE GEN
     276     20247785 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
     277     20247785 :   v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
     278              : INLINE GEN
     279      1898408 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
     280      1898408 :   v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
     281              : 
     282              : INLINE GEN
     283     28269497 : mkqfb(GEN x, GEN y, GEN z, GEN d) { retmkqfb(x,y,z,d); }
     284              : /* vec */
     285              : INLINE GEN
     286     70139249 : mkvec(GEN x) { retmkvec(x); }
     287              : INLINE GEN
     288    222857113 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
     289              : INLINE GEN
     290    123643266 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
     291              : INLINE GEN
     292     10532632 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
     293              : INLINE GEN
     294     15190564 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
     295              : INLINE GEN
     296           42 : mkvec6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkvec6(x,y,z,t,u,v); }
     297              : INLINE GEN
     298          168 : mkvecs(long x) { retmkvec(stoi(x)); }
     299              : INLINE GEN
     300       221653 : mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
     301              : INLINE GEN
     302       524441 : mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
     303              : INLINE GEN
     304           21 : mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
     305              : INLINE GEN
     306            0 : mkvec5s(long x, long y, long z, long t, long u) { retmkvec5(stoi(x),stoi(y),stoi(z),stoi(t),stoi(u)); }
     307              : INLINE GEN
     308          301 : mkvec6s(long x, long y, long z, long t, long u, long v) { retmkvec6(stoi(x),stoi(y),stoi(z),stoi(t),stoi(u),stoi(v)); }
     309              : INLINE GEN
     310       122528 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
     311              : INLINE GEN
     312       114555 : mkvec2copy(GEN x, GEN y) {
     313       114555 :   GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
     314              : /* col */
     315              : INLINE GEN
     316     10667527 : mkcol(GEN x) { retmkcol(x); }
     317              : INLINE GEN
     318    168415523 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
     319              : INLINE GEN
     320        88314 : mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
     321              : INLINE GEN
     322       103467 : mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
     323              : INLINE GEN
     324           94 : mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
     325              : INLINE GEN
     326       232582 : mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
     327              : INLINE GEN
     328        40411 : mkcols(long x) { retmkcol(stoi(x)); }
     329              : INLINE GEN
     330      4416244 : mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
     331              : INLINE GEN
     332          315 : mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
     333              : INLINE GEN
     334            0 : mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
     335              : INLINE GEN
     336        33555 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
     337              : /* mat */
     338              : INLINE GEN
     339      4783300 : mkmat(GEN x) { retmkmat(x); }
     340              : INLINE GEN
     341     33670094 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
     342              : INLINE GEN
     343        17001 : mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
     344              : INLINE GEN
     345           84 : mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
     346              : INLINE GEN
     347            0 : mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
     348              : INLINE GEN
     349       122337 : mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
     350              : INLINE GEN
     351            0 : mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
     352              : INLINE GEN
     353      1333383 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
     354              : INLINE GEN
     355       108951 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
     356              : INLINE long
     357      1454556 : inf_get_sign(GEN x) { return signe(gel(x,1)); }
     358              : INLINE GEN
     359       207578 : mkmat22s(long a, long b, long c, long d) {retmkmat2(mkcol2s(a,c),mkcol2s(b,d));}
     360              : INLINE GEN
     361     54382154 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
     362              : 
     363              : /* pol */
     364              : INLINE GEN
     365      3887031 : pol_x(long v) {
     366      3887031 :   GEN p = cgetg(4, t_POL);
     367      3887031 :   p[1] = evalsigne(1)|evalvarn(v);
     368      3887031 :   gel(p,2) = gen_0;
     369      3887031 :   gel(p,3) = gen_1; return p;
     370              : }
     371              : /* x^n, assume n >= 0 */
     372              : INLINE GEN
     373      2237756 : pol_xn(long n, long v) {
     374      2237756 :   long i, a = n+2;
     375      2237756 :   GEN p = cgetg(a+1, t_POL);
     376      2237756 :   p[1] = evalsigne(1)|evalvarn(v);
     377      5438701 :   for (i = 2; i < a; i++) gel(p,i) = gen_0;
     378      2237756 :   gel(p,a) = gen_1; return p;
     379              : }
     380              : /* x^n, no assumption on n */
     381              : INLINE GEN
     382          294 : pol_xnall(long n, long v)
     383              : {
     384          294 :   if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
     385          287 :   return pol_xn(n, v);
     386              : }
     387              : /* x^n, assume n >= 0 */
     388              : INLINE GEN
     389          371 : polxn_Flx(long n, long sv) {
     390          371 :   long i, a = n+2;
     391          371 :   GEN p = cgetg(a+1, t_VECSMALL);
     392          371 :   p[1] = sv;
     393         1806 :   for (i = 2; i < a; i++) p[i] = 0;
     394          371 :   p[a] = 1; return p;
     395              : }
     396              : INLINE GEN
     397      3762772 : pol_1(long v) {
     398      3762772 :   GEN p = cgetg(3, t_POL);
     399      3762772 :   p[1] = evalsigne(1)|evalvarn(v);
     400      3762772 :   gel(p,2) = gen_1; return p;
     401              : }
     402              : INLINE GEN
     403     84220405 : pol_0(long v)
     404              : {
     405     84220405 :   GEN x = cgetg(2,t_POL);
     406     84220405 :   x[1] = evalvarn(v); return x;
     407              : }
     408              : #define retconst_vec(n,x)\
     409              :   do { long _i, _n = (n);\
     410              :        GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
     411              :        for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
     412              :        return _v; } while(0)
     413              : INLINE GEN
     414    245598758 : const_vec(long n, GEN x) { retconst_vec(n, x); }
     415              : #define retconst_col(n,x)\
     416              :   do { long _i, _n = (n);\
     417              :        GEN _v = cgetg(_n+1, t_COL), _x = (x);\
     418              :        for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
     419              :        return _v; } while(0)
     420              : INLINE GEN
     421     39636066 : const_col(long n, GEN x) { retconst_col(n, x); }
     422              : INLINE GEN
     423     18915582 : const_vecsmall(long n, long c)
     424              : {
     425              :   long i;
     426     18915582 :   GEN V = cgetg(n+1,t_VECSMALL);
     427    902213495 :   for(i=1;i<=n;i++) V[i] = c;
     428     18915582 :   return V;
     429              : }
     430              : 
     431              : /***   ZERO   ***/
     432              : /* O(p^e) */
     433              : INLINE GEN
     434       858086 : zeropadic(GEN p, long e) { retmkpadic(gen_0, icopy(p), gen_1, e, 0); }
     435              : INLINE GEN
     436        17150 : zeropadic_shallow(GEN p, long e) { retmkpadic(gen_0, icopy(p), gen_1, e, 0); }
     437              : /* O(pol_x(v)^e) */
     438              : INLINE GEN
     439       139020 : zeroser(long v, long e)
     440              : {
     441       139020 :   GEN x = cgetg(2, t_SER);
     442       139020 :   x[1] = evalvalser(e) | evalvarn(v); return x;
     443              : }
     444              : INLINE int
     445     11871524 : ser_isexactzero(GEN x)
     446              : {
     447     11871524 :   if (!signe(x)) switch(lg(x))
     448              :   {
     449       132951 :     case 2: return 1;
     450         4872 :     case 3: return isexactzero(gel(x,2));
     451              :   }
     452     11733701 :   return 0;
     453              : }
     454              : /* 0 * pol_x(v) */
     455              : INLINE GEN
     456     42429168 : zeropol(long v) { return pol_0(v); }
     457              : /* vector(n) */
     458              : INLINE GEN
     459    125380642 : zerocol(long n)
     460              : {
     461    125380642 :   GEN y = cgetg(n+1,t_COL);
     462    867975931 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     463    125380642 :   return y;
     464              : }
     465              : /* vectorv(n) */
     466              : INLINE GEN
     467     29356336 : zerovec(long n)
     468              : {
     469     29356336 :   GEN y = cgetg(n+1,t_VEC);
     470    423753749 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     471     29356336 :   return y;
     472              : }
     473              : /* matrix(m, n) */
     474              : INLINE GEN
     475       108888 : zeromat(long m, long n)
     476              : {
     477       108888 :   GEN y = cgetg(n+1,t_MAT);
     478       108888 :   GEN v = zerocol(m);
     479       401035 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     480       108888 :   return y;
     481              : }
     482              : /* = zero_zx, sv is a evalvarn()*/
     483              : INLINE GEN
     484      3256287 : zero_Flx(long sv) { return pol0_Flx(sv); }
     485              : INLINE GEN
     486     61823899 : zero_Flv(long n)
     487              : {
     488     61823899 :   GEN y = cgetg(n+1,t_VECSMALL);
     489   1031687955 :   long i; for (i=1; i<=n; i++) y[i] = 0;
     490     61823899 :   return y;
     491              : }
     492              : /* matrix(m, n) */
     493              : INLINE GEN
     494      2854824 : zero_Flm(long m, long n)
     495              : {
     496      2854824 :   GEN y = cgetg(n+1,t_MAT);
     497      2854824 :   GEN v = zero_Flv(m);
     498     18259337 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     499      2854824 :   return y;
     500              : }
     501              : /* matrix(m, n) */
     502              : INLINE GEN
     503       241029 : zero_Flm_copy(long m, long n)
     504              : {
     505       241029 :   GEN y = cgetg(n+1,t_MAT);
     506      2708309 :   long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
     507       241029 :   return y;
     508              : }
     509              : 
     510              : INLINE GEN
     511      3663647 : zero_F2v(long m)
     512              : {
     513      3663647 :   long l = nbits2nlong(m);
     514      3663647 :   GEN v  = zero_Flv(l+1);
     515      3663647 :   v[1] = m;
     516      3663647 :   return v;
     517              : }
     518              : 
     519              : INLINE GEN
     520            0 : zero_F2m(long m, long n)
     521              : {
     522              :   long i;
     523            0 :   GEN M = cgetg(n+1, t_MAT);
     524            0 :   GEN v = zero_F2v(m);
     525            0 :   for (i = 1; i <= n; i++)
     526            0 :     gel(M,i) = v;
     527            0 :   return M;
     528              : }
     529              : 
     530              : 
     531              : INLINE GEN
     532       847268 : zero_F2m_copy(long m, long n)
     533              : {
     534              :   long i;
     535       847268 :   GEN M = cgetg(n+1, t_MAT);
     536      2025009 :   for (i = 1; i <= n; i++)
     537      1177741 :     gel(M,i)= zero_F2v(m);
     538       847268 :   return M;
     539              : }
     540              : 
     541              : /* matrix(m, n) */
     542              : INLINE GEN
     543     14387211 : zeromatcopy(long m, long n)
     544              : {
     545     14387211 :   GEN y = cgetg(n+1,t_MAT);
     546     73099441 :   long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
     547     14387211 :   return y;
     548              : }
     549              : 
     550              : INLINE GEN
     551        29675 : zerovec_block(long len)
     552              : {
     553              :   long i;
     554        29675 :   GEN blk = cgetg_block(len + 1, t_VEC);
     555       979275 :   for (i = 1; i <= len; ++i)
     556       949600 :     gel(blk, i) = gen_0;
     557        29675 :   return blk;
     558              : }
     559              : 
     560              : /* i-th vector in the standard basis */
     561              : INLINE GEN
     562      5929194 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
     563              : INLINE GEN
     564      1716800 : vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
     565              : INLINE GEN
     566           42 : F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
     567              : INLINE GEN
     568       585528 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
     569              : INLINE GEN
     570      1454577 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
     571              : 
     572              : INLINE GEN
     573     27931246 : shallowcopy(GEN x)
     574     27931246 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
     575              : 
     576              : /* routines for naive growarrays */
     577              : INLINE GEN
     578     11795454 : vectrunc_init(long l)
     579              : {
     580     11795454 :   GEN z = new_chunk(l);
     581     11795454 :   z[0] = evaltyp(t_VEC) | _evallg(1); return z;
     582              : }
     583              : INLINE GEN
     584       740376 : coltrunc_init(long l)
     585              : {
     586       740376 :   GEN z = new_chunk(l);
     587       740376 :   z[0] = evaltyp(t_COL) | _evallg(1); return z;
     588              : }
     589              : INLINE void
     590    475863174 : lg_increase(GEN x) { x[0]++; }
     591              : INLINE void
     592     15914589 : vectrunc_append(GEN x, GEN t) { gel(x, lg(x)) = t; lg_increase(x); }
     593              : INLINE void
     594         9667 : vectrunc_append_batch(GEN x, GEN y)
     595              : {
     596         9667 :   long i, l = lg(x), ly = lg(y);
     597         9667 :   GEN z = x + l-1;
     598        32753 :   for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
     599         9667 :   setlg(x, l+ly-1);
     600         9667 : }
     601              : INLINE GEN
     602    256326875 : vecsmalltrunc_init(long l)
     603              : {
     604    256326875 :   GEN z = new_chunk(l);
     605    256326871 :   z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
     606              : }
     607              : INLINE void
     608    101231021 : vecsmalltrunc_append(GEN x, long t) { x[ lg(x) ] = t; lg_increase(x); }
     609              : 
     610              : /*******************************************************************/
     611              : /*                                                                 */
     612              : /*                    STRING HASH FUNCTIONS                        */
     613              : /*                                                                 */
     614              : /*******************************************************************/
     615              : INLINE ulong
     616      2787689 : hash_str(const char *str)
     617              : {
     618      2787689 :   ulong hash = 5381UL, c;
     619     29247051 :   while ( (c = (ulong)*str++) )
     620     26459362 :     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     621      2787689 :   return hash;
     622              : }
     623              : INLINE ulong
     624     31640706 : hash_str_len(const char *str, long len)
     625              : {
     626     31640706 :   ulong hash = 5381UL;
     627              :   long i;
     628    229408571 :   for (i = 0; i < len; i++)
     629              :   {
     630    197767865 :     ulong c = (ulong)*str++;
     631    197767865 :     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     632              :   }
     633     31640706 :   return hash;
     634              : }
     635              : 
     636              : /*******************************************************************/
     637              : /*                                                                 */
     638              : /*                        VEC / COL / VECSMALL                     */
     639              : /*                                                                 */
     640              : /*******************************************************************/
     641              : /* shallow*/
     642              : INLINE GEN
     643        11564 : vec_shorten(GEN v, long n)
     644              : {
     645        11564 :   GEN V = cgetg(n+1, t_VEC);
     646              :   long i;
     647        13027 :   for(i = 1; i <= n; i++) gel(V,i) = gel(v,i);
     648        11564 :   return V;
     649              : }
     650              : /* shallow*/
     651              : INLINE GEN
     652        18983 : vec_lengthen(GEN v, long n)
     653              : {
     654        18983 :   GEN V = cgetg(n+1, t_VEC);
     655        18983 :   long i, l = lg(v);
     656      1941005 :   for(i = 1; i < l; i++) gel(V,i) = gel(v,i);
     657        18983 :   return V;
     658              : }
     659              : /* shallow*/
     660              : INLINE GEN
     661      3470858 : vec_append(GEN V, GEN s)
     662              : {
     663      3470858 :   long i, l2 = lg(V);
     664      3470858 :   GEN res = cgetg(l2+1, typ(V));
     665     10827192 :   for (i = 1; i < l2; i++) gel(res, i) = gel(V,i);
     666      3470858 :   gel(res,l2) = s; return res;
     667              : }
     668              : /* shallow*/
     669              : INLINE GEN
     670       313943 : vec_prepend(GEN v, GEN s)
     671              : {
     672       313943 :   long i, l = lg(v);
     673       313943 :   GEN w = cgetg(l+1, typ(v));
     674       313943 :   gel(w,1) = s;
     675       796282 :   for (i = 2; i <= l; i++) gel(w,i) = gel(v,i-1);
     676       313943 :   return w;
     677              : }
     678              : /* shallow*/
     679              : INLINE GEN
     680            0 : vec_setconst(GEN v, GEN x)
     681              : {
     682            0 :   long i, l = lg(v);
     683            0 :   for (i = 1; i < l; i++) gel(v,i) = x;
     684            0 :   return v;
     685              : }
     686              : INLINE GEN
     687        77082 : vecsmall_shorten(GEN v, long n)
     688              : {
     689        77082 :   GEN V = cgetg(n+1,t_VECSMALL);
     690              :   long i;
     691      1050607 :   for(i = 1; i <= n; i++) V[i] = v[i];
     692        77082 :   return V;
     693              : }
     694              : INLINE GEN
     695          547 : vecsmall_lengthen(GEN v, long n)
     696              : {
     697          547 :   long i, l = lg(v);
     698          547 :   GEN V = cgetg(n+1,t_VECSMALL);
     699        53385 :   for(i = 1; i < l; i++) V[i] = v[i];
     700          547 :   return V;
     701              : }
     702              : 
     703              : INLINE GEN
     704      4149429 : vec_to_vecsmall(GEN x)
     705     19366861 : { pari_APPLY_long(itos(gel(x,i))) }
     706              : INLINE GEN
     707       513116 : vecsmall_to_vec(GEN x)
     708      7078761 : { pari_APPLY_type(t_VEC, stoi(x[i])) }
     709              : INLINE GEN
     710         3304 : vecsmall_to_vec_inplace(GEN z)
     711              : {
     712         3304 :   long i, l = lg(z);
     713        79604 :   for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
     714         3304 :   settyp(z, t_VEC); return z;
     715              : }
     716              : INLINE GEN
     717      7993578 : vecsmall_to_col(GEN x)
     718     60180275 : { pari_APPLY_type(t_COL, stoi(x[i])) }
     719              : 
     720              : INLINE int
     721      7211133 : vecsmall_lexcmp(GEN x, GEN y)
     722              : {
     723              :   long lx,ly,l,i;
     724      7211133 :   lx = lg(x);
     725      7211133 :   ly = lg(y); l = minss(lx,ly);
     726     30806200 :   for (i=1; i<l; i++)
     727     29323028 :     if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
     728      1483172 :   if (lx == ly) return 0;
     729         4154 :   return (lx < ly)? -1 : 1;
     730              : }
     731              : 
     732              : INLINE int
     733    108088071 : vecsmall_prefixcmp(GEN x, GEN y)
     734              : {
     735    108088071 :   long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
     736    542691324 :   for (i=1; i<l; i++)
     737    519806035 :     if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
     738     22885289 :   return 0;
     739              : }
     740              : 
     741              : /*Can be used on t_VEC, but coeffs not gcopy-ed*/
     742              : INLINE GEN
     743       187025 : vecsmall_prepend(GEN V, long s)
     744              : {
     745       187025 :   long i, l2 = lg(V);
     746       187025 :   GEN res = cgetg(l2+1, typ(V));
     747       187025 :   res[1] = s;
     748       625432 :   for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
     749       187025 :   return res;
     750              : }
     751              : 
     752              : INLINE GEN
     753      4117794 : vecsmall_append(GEN V, long s)
     754              : {
     755      4117794 :   long i, l2 = lg(V);
     756      4117794 :   GEN res = cgetg(l2+1, t_VECSMALL);
     757      6045097 :   for (i = 1; i < l2; ++i) res[i] = V[i];
     758      4117794 :   res[l2] = s; return res;
     759              : }
     760              : 
     761              : INLINE GEN
     762      1466275 : vecsmall_concat(GEN u, GEN v)
     763              : {
     764      1466275 :   long i, l1 = lg(u)-1, l2 = lg(v)-1;
     765      1466275 :   GEN res = cgetg(l1+l2+1, t_VECSMALL);
     766     11639224 :   for (i = 1; i <= l1; ++i) res[i]    = u[i];
     767     15285883 :   for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
     768      1466275 :   return res;
     769              : }
     770              : 
     771              : /* return the number of indices where u and v are equal */
     772              : INLINE long
     773            0 : vecsmall_coincidence(GEN u, GEN v)
     774              : {
     775            0 :   long i, s = 0, l = minss(lg(u),lg(v));
     776            0 :   for(i=1; i<l; i++)
     777            0 :     if(u[i] == v[i]) s++;
     778            0 :   return s;
     779              : }
     780              : 
     781              : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
     782              : INLINE long
     783           84 : vecsmall_isin(GEN v, long x)
     784              : {
     785           84 :   long i, l = lg(v);
     786          124 :   for (i = 1; i < l; i++)
     787           80 :     if (v[i] == x) return i;
     788           44 :   return 0;
     789              : }
     790              : 
     791              : INLINE long
     792           84 : vecsmall_pack(GEN V, long base, long mod)
     793              : {
     794           84 :   long i, s = 0;
     795          273 :   for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
     796           84 :   return s;
     797              : }
     798              : 
     799              : INLINE long
     800           21 : vecsmall_indexmax(GEN x)
     801              : {
     802           21 :   long i, i0 = 1, t = x[1], lx = lg(x);
     803           84 :   for (i=2; i<lx; i++)
     804           63 :     if (x[i] > t) t = x[i0=i];
     805           21 :   return i0;
     806              : }
     807              : 
     808              : INLINE long
     809      1177894 : vecsmall_max(GEN x)
     810              : {
     811      1177894 :   long i, t = x[1], lx = lg(x);
     812      3410841 :   for (i=2; i<lx; i++)
     813      2232947 :     if (x[i] > t) t = x[i];
     814      1177894 :   return t;
     815              : }
     816              : 
     817              : INLINE long
     818           21 : vecsmall_indexmin(GEN x)
     819              : {
     820           21 :   long i, i0 = 1, t = x[1], lx =lg(x);
     821           84 :   for (i=2; i<lx; i++)
     822           63 :     if (x[i] < t) t = x[i0=i];
     823           21 :   return i0;
     824              : }
     825              : 
     826              : INLINE long
     827         5509 : vecsmall_min(GEN x)
     828              : {
     829         5509 :   long i, t = x[1], lx =lg(x);
     830        38563 :   for (i=2; i<lx; i++)
     831        33054 :     if (x[i] < t) t = x[i];
     832         5509 :   return t;
     833              : }
     834              : 
     835              : INLINE int
     836     23682389 : ZV_isscalar(GEN x)
     837              : {
     838     23682389 :   long l = lg(x);
     839     55570415 :   while (--l > 1)
     840     55136273 :     if (signe(gel(x, l))) return 0;
     841       434142 :   return 1;
     842              : }
     843              : INLINE int
     844     54435560 : QV_isscalar(GEN x)
     845              : {
     846     54435560 :   long lx = lg(x),i;
     847     70444517 :   for (i=2; i<lx; i++)
     848     66481706 :     if (!isintzero(gel(x, i))) return 0;
     849      3962811 :   return 1;
     850              : }
     851              : INLINE int
     852      1211818 : RgV_isscalar(GEN x)
     853              : {
     854      1211818 :   long lx = lg(x),i;
     855      1238089 :   for (i=2; i<lx; i++)
     856      1234371 :     if (!gequal0(gel(x, i))) return 0;
     857         3718 :   return 1;
     858              : }
     859              : INLINE int
     860            0 : RgX_isscalar(GEN x)
     861              : {
     862              :   long i;
     863            0 :   for (i=lg(x)-1; i>2; i--)
     864            0 :     if (!gequal0(gel(x, i))) return 0;
     865            0 :   return 1;
     866              : }
     867              : INLINE long
     868     27145972 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
     869              : INLINE GEN
     870          140 : RgX_to_RgV(GEN x, long N) { x = RgX_to_RgC(x, N); settyp(x, t_VEC); return x; }
     871              : 
     872              : #define RgX_type_code(t1,t2) ((t1 << 6) | t2)
     873              : 
     874              : INLINE int
     875        68841 : RgX_is_rational(GEN x)
     876              : {
     877              :   long i;
     878       313850 :   for (i = lg(x)-1; i > 1; i--)
     879       297381 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     880        16469 :   return 1;
     881              : }
     882              : INLINE int
     883     22521796 : RgX_is_ZX(GEN x)
     884              : {
     885              :   long i;
     886     83300803 :   for (i = lg(x)-1; i > 1; i--)
     887     62560750 :     if (typ(gel(x,i)) != t_INT) return 0;
     888     20740053 :   return 1;
     889              : }
     890              : INLINE int
     891       400113 : RgX_is_QX(GEN x)
     892              : {
     893       400113 :   long k = lg(x)-1;
     894      1497181 :   for ( ; k>1; k--)
     895      1097362 :     if (!is_rational_t(typ(gel(x,k)))) return 0;
     896       399819 :   return 1;
     897              : }
     898              : INLINE int
     899      1345838 : RgX_is_monomial(GEN x)
     900              : {
     901              :   long i;
     902      1345838 :   if (!signe(x)) return 0;
     903      3393431 :   for (i=lg(x)-2; i>1; i--)
     904      2695778 :     if (!isexactzero(gel(x,i))) return 0;
     905       697653 :   return 1;
     906              : }
     907              : INLINE int
     908     32138227 : RgV_is_ZV(GEN x)
     909              : {
     910              :   long i;
     911    127610769 :   for (i = lg(x)-1; i > 0; i--)
     912     95505365 :     if (typ(gel(x,i)) != t_INT) return 0;
     913     32105404 :   return 1;
     914              : }
     915              : INLINE int
     916       142052 : RgV_is_QV(GEN x)
     917              : {
     918              :   long i;
     919       629471 :   for (i = lg(x)-1; i > 0; i--)
     920       490373 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     921       139098 :   return 1;
     922              : }
     923              : INLINE long
     924       111957 : RgV_isin_i(GEN v, GEN x, long n)
     925              : {
     926              :   long i;
     927      1035292 :   for (i = 1; i <= n; i++)
     928      1028530 :     if (gequal(gel(v,i), x)) return i;
     929         6762 :   return 0;
     930              : }
     931              : INLINE long
     932       111957 : RgV_isin(GEN v, GEN x) { return RgV_isin_i(v, x, lg(v)-1); }
     933              : 
     934              : /********************************************************************/
     935              : /**                                                                **/
     936              : /**            Dynamic arrays implementation                       **/
     937              : /**                                                                **/
     938              : /********************************************************************/
     939              : INLINE void **
     940    988119037 : pari_stack_base(pari_stack *s) { return s->data; }
     941              : 
     942              : INLINE void
     943      6444814 : pari_stack_init(pari_stack *s, size_t size, void **data)
     944              : {
     945      6444814 :   s->data = data;
     946      6444814 :   *data = NULL;
     947      6444814 :   s->n = 0;
     948      6444814 :   s->alloc = 0;
     949      6444814 :   s->size = size;
     950      6444814 : }
     951              : 
     952              : INLINE void
     953    982050867 : pari_stack_alloc(pari_stack *s, long nb)
     954              : {
     955    982050867 :   void **sdat = pari_stack_base(s);
     956    982050867 :   long alloc = s->alloc;
     957    982050867 :   if (s->n+nb <= alloc) return;
     958      2377394 :   if (!alloc)
     959      2277572 :     alloc = nb;
     960              :   else
     961              :   {
     962       202943 :     while (s->n+nb > alloc) alloc <<= 1;
     963              :   }
     964      2377394 :   pari_realloc_ip(sdat,alloc*s->size);
     965      2377394 :   s->alloc = alloc;
     966              : }
     967              : 
     968              : INLINE long
     969    886587133 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
     970              : 
     971              : INLINE void
     972      6062324 : pari_stack_delete(pari_stack *s)
     973              : {
     974      6062324 :   void **sdat = pari_stack_base(s);
     975      6062324 :   if (*sdat) pari_free(*sdat);
     976      6062324 : }
     977              : 
     978              : INLINE void
     979         5840 : pari_stack_pushp(pari_stack *s, void *u)
     980              : {
     981         5840 :   long n = pari_stack_new(s);
     982         5840 :   void **sdat =(void**) *pari_stack_base(s);
     983         5840 :   sdat[n] = u;
     984         5840 : }
     985              : 
     986              : /*******************************************************************/
     987              : /*                                                                 */
     988              : /*                            EXTRACT                              */
     989              : /*                                                                 */
     990              : /*******************************************************************/
     991              : INLINE GEN
     992    670252788 : vecslice(GEN A, long y1, long y2)
     993              : {
     994    670252788 :   long i,lB = y2 - y1 + 2;
     995    670252788 :   GEN B = cgetg(lB, typ(A));
     996   2988786364 :   for (i=1; i<lB; i++) B[i] = A[y1-1+i];
     997    670252788 :   return B;
     998              : }
     999              : INLINE GEN
    1000      2881046 : vecslicepermute(GEN A, GEN p, long y1, long y2)
    1001              : {
    1002      2881046 :   long i,lB = y2 - y1 + 2;
    1003      2881046 :   GEN B = cgetg(lB, typ(A));
    1004     32696886 :   for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
    1005      2881046 :   return B;
    1006              : }
    1007              : /* rowslice(rowpermute(A,p), x1, x2) */
    1008              : INLINE GEN
    1009       234388 : rowslicepermute(GEN x, GEN p, long j1, long j2)
    1010       998206 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
    1011              : 
    1012              : INLINE GEN
    1013     80075257 : rowslice(GEN x, long j1, long j2)
    1014    686091203 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
    1015              : 
    1016              : INLINE GEN
    1017     16932343 : matslice(GEN A, long x1, long x2, long y1, long y2)
    1018     16932343 : { return rowslice(vecslice(A, y1, y2), x1, x2); }
    1019              : 
    1020              : /* shallow, remove coeff of index j */
    1021              : INLINE GEN
    1022          273 : rowsplice(GEN x, long j)
    1023         1225 : { pari_APPLY_same(vecsplice(gel(x,i), j)) }
    1024              : 
    1025              : /* shallow, remove coeff of index j */
    1026              : INLINE GEN
    1027       385292 : vecsplice(GEN a, long j)
    1028              : {
    1029       385292 :   long i, k, l = lg(a);
    1030              :   GEN b;
    1031       385292 :   if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
    1032       385292 :   b = cgetg(l-1, typ(a));
    1033      1594668 :   for (i = k = 1; i < l; i++)
    1034      1209376 :     if (i != j) gel(b, k++) = gel(a,i);
    1035       385292 :   return b;
    1036              : }
    1037              : /* shallow */
    1038              : INLINE GEN
    1039         1036 : RgM_minor(GEN a, long i, long j)
    1040              : {
    1041         1036 :   GEN b = vecsplice(a, j);
    1042         1036 :   long k, l = lg(b);
    1043         4242 :   for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
    1044         1036 :   return b;
    1045              : }
    1046              : 
    1047              : /* A[x0,] */
    1048              : INLINE GEN
    1049       767828 : row(GEN x, long j)
    1050      6928867 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
    1051              : INLINE GEN
    1052      8756510 : Flm_row(GEN x, long j)
    1053    232787920 : { pari_APPLY_ulong((ulong)coeff(x, j, i)) }
    1054              : /* A[x0,] */
    1055              : INLINE GEN
    1056       204988 : rowcopy(GEN x, long j)
    1057      2105509 : { pari_APPLY_type(t_VEC, gcopy(gcoeff(x, j, i))) }
    1058              : /* A[x0, x1..x2] */
    1059              : INLINE GEN
    1060          987 : row_i(GEN A, long x0, long x1, long x2)
    1061              : {
    1062          987 :   long i, lB = x2 - x1 + 2;
    1063          987 :   GEN B  = cgetg(lB, t_VEC);
    1064         2989 :   for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
    1065          987 :   return B;
    1066              : }
    1067              : 
    1068              : INLINE GEN
    1069       697305 : vecreverse(GEN A)
    1070              : {
    1071              :   long i, l;
    1072       697305 :   GEN B = cgetg_copy(A, &l);
    1073      2408595 :   for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
    1074       697305 :   return B;
    1075              : }
    1076              : 
    1077              : INLINE GEN
    1078         3052 : vecsmall_reverse(GEN A)
    1079              : {
    1080              :   long i, l;
    1081         3052 :   GEN B = cgetg_copy(A, &l);
    1082        12698 :   for (i=1; i<l; i++) B[i] = A[l-i];
    1083         3052 :   return B;
    1084              : }
    1085              : 
    1086              : INLINE void
    1087         2612 : vecreverse_inplace(GEN y)
    1088              : {
    1089         2612 :   long l = lg(y), lim = l>>1, i;
    1090         8326 :   for (i = 1; i <= lim; i++)
    1091              :   {
    1092         5714 :     GEN z = gel(y,i);
    1093         5714 :     gel(y,i)    = gel(y,l-i);
    1094         5714 :     gel(y,l-i) = z;
    1095              :   }
    1096         2612 : }
    1097              : 
    1098              : INLINE GEN
    1099     79343445 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
    1100              : 
    1101              : INLINE GEN
    1102     21132678 : vecpermute(GEN A, GEN x)
    1103    118194678 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
    1104              : 
    1105              : INLINE GEN
    1106      9557863 : veclast(GEN A) { return gel(A, lg(A)-1); }
    1107              : 
    1108              : INLINE GEN
    1109     14794922 : rowpermute(GEN x, GEN p)
    1110    103492440 : { pari_APPLY_same(typ(gel(x,i)) == t_VECSMALL ? vecsmallpermute(gel(x, i), p)
    1111              :                                               : vecpermute(gel(x, i), p))
    1112              : }
    1113              : /*******************************************************************/
    1114              : /*                                                                 */
    1115              : /*                          PERMUTATIONS                           */
    1116              : /*                                                                 */
    1117              : /*******************************************************************/
    1118              : INLINE GEN
    1119      3284502 : identity_zv(long n)
    1120              : {
    1121      3284502 :   GEN v = cgetg(n+1, t_VECSMALL);
    1122              :   long i;
    1123     33352210 :   for (i = 1; i <= n; i++) v[i] = i;
    1124      3284502 :   return v;
    1125              : }
    1126              : INLINE GEN
    1127         8120 : identity_ZV(long n)
    1128              : {
    1129         8120 :   GEN v = cgetg(n+1, t_VEC);
    1130              :   long i;
    1131       169113 :   for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
    1132         8120 :   return v;
    1133              : }
    1134              : /* identity permutation */
    1135              : INLINE GEN
    1136      3260139 : identity_perm(long n) { return identity_zv(n); }
    1137              : 
    1138              : /* assume d <= n */
    1139              : INLINE GEN
    1140        99932 : cyclic_perm(long n, long d)
    1141              : {
    1142        99932 :   GEN perm = cgetg(n+1, t_VECSMALL);
    1143              :   long i;
    1144       520401 :   for (i = 1; i <= n-d; i++) perm[i] = i+d;
    1145       226723 :   for (     ; i <= n;   i++) perm[i] = i-n+d;
    1146        99932 :   return perm;
    1147              : }
    1148              : 
    1149              : /* Multiply (compose) two permutations */
    1150              : INLINE GEN
    1151     81656797 : perm_mul(GEN s, GEN x)
    1152   1044559918 : { pari_APPLY_long(s[x[i]]) }
    1153              : 
    1154              : INLINE GEN
    1155          728 : perm_sqr(GEN x)
    1156        18284 : { pari_APPLY_long(x[x[i]]) }
    1157              : 
    1158              : /* Compute the inverse (reciprocal) of a permutation. */
    1159              : INLINE GEN
    1160      3781182 : perm_inv(GEN x)
    1161              : {
    1162              :   long i, lx;
    1163      3781182 :   GEN y = cgetg_copy(x, &lx);
    1164     47184597 :   for (i=1; i<lx; i++) y[ x[i] ] = i;
    1165      3781182 :   return y;
    1166              : }
    1167              : /* Return s*t*s^-1 */
    1168              : INLINE GEN
    1169       422429 : perm_conj(GEN s, GEN t)
    1170              : {
    1171              :   long i, l;
    1172       422429 :   GEN v = cgetg_copy(s, &l);
    1173      7109823 :   for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
    1174       422429 :   return v;
    1175              : }
    1176              : 
    1177              : INLINE void
    1178    494948277 : pari_free(void *pointer)
    1179              : {
    1180    494948277 :   BLOCK_SIGINT_START;
    1181    494948277 :   free(pointer);
    1182    494948277 :   BLOCK_SIGINT_END;
    1183    494948276 : }
    1184              : INLINE void*
    1185    730491712 : pari_malloc(size_t size)
    1186              : {
    1187    730491712 :   if (size)
    1188              :   {
    1189              :     char *tmp;
    1190    730491712 :     BLOCK_SIGINT_START;
    1191    730491712 :     tmp = (char*)malloc(size);
    1192    730491712 :     BLOCK_SIGINT_END;
    1193    730491712 :     if (!tmp) pari_err(e_MEM);
    1194    730491712 :     return tmp;
    1195              :   }
    1196            0 :   return NULL;
    1197              : }
    1198              : INLINE void*
    1199         1887 : pari_realloc(void *pointer, size_t size)
    1200              : {
    1201              :   char *tmp;
    1202              : 
    1203         1887 :   BLOCK_SIGINT_START;
    1204         1887 :   if (!pointer) tmp = (char *) malloc(size);
    1205         1887 :   else tmp = (char *) realloc(pointer,size);
    1206         1887 :   BLOCK_SIGINT_END;
    1207         1887 :   if (!tmp) pari_err(e_MEM);
    1208         1887 :   return tmp;
    1209              : }
    1210              : INLINE void
    1211      2384842 : pari_realloc_ip(void **pointer, size_t size)
    1212              : {
    1213              :   char *tmp;
    1214      2384842 :   BLOCK_SIGINT_START;
    1215      2384842 :   if (!*pointer) tmp = (char *) malloc(size);
    1216       107270 :   else tmp = (char *) realloc(*pointer,size);
    1217      2384842 :   if (!tmp) pari_err(e_MEM);
    1218      2384842 :   *pointer = tmp;
    1219      2384842 :   BLOCK_SIGINT_END;
    1220      2384842 : }
    1221              : 
    1222              : INLINE void*
    1223        48209 : pari_calloc(size_t size)
    1224              : {
    1225        48209 :   void *t = pari_malloc(size);
    1226        48209 :   memset(t, 0, size); return t;
    1227              : }
    1228              : INLINE GEN
    1229        10153 : cgetalloc(size_t l, long t)
    1230              : { /* evallg may raise e_OVERFLOW, which would leak x */
    1231        10153 :   ulong x0 = evaltyp(t) | evallg(l);
    1232        10153 :   GEN x = (GEN)pari_malloc(l * sizeof(long));
    1233        10153 :   x[0] = x0; return x;
    1234              : }
    1235              : 
    1236              : /*******************************************************************/
    1237              : /*                                                                 */
    1238              : /*                       GARBAGE COLLECTION                        */
    1239              : /*                                                                 */
    1240              : /*******************************************************************/
    1241              : /* copy integer x as if we had set_avma(av) */
    1242              : INLINE GEN
    1243   8130314400 : icopy_avma(GEN x, pari_sp av)
    1244              : {
    1245   8130314400 :   long i = lgefint(x), lx = i;
    1246   8130314400 :   GEN y = ((GEN)av) - i;
    1247  59895348869 :   while (--i > 0) y[i] = x[i];
    1248   8130314400 :   y[0] = evaltyp(t_INT)|evallg(lx);
    1249   8130314400 :   return y;
    1250              : }
    1251              : /* copy leaf x as if we had set_avma(av) */
    1252              : INLINE GEN
    1253    587602332 : leafcopy_avma(GEN x, pari_sp av)
    1254              : {
    1255    587602332 :   long i = lg(x);
    1256    587602332 :   GEN y = ((GEN)av) - i;
    1257   3188294435 :   while (--i > 0) y[i] = x[i];
    1258    587602332 :   y[0] = x[0] & (~CLONEBIT);
    1259    587602332 :   return y;
    1260              : }
    1261              : INLINE GEN
    1262   1205436968 : gc_leaf(pari_sp av, GEN x)
    1263              : {
    1264              :   long lx;
    1265              :   GEN q;
    1266              : 
    1267   1205436968 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1268   1204432908 :   lx = lg(x);
    1269   1204432908 :   q = ((GEN)av) - lx;
    1270   1204432908 :   set_avma((pari_sp)q);
    1271  11845650736 :   while (--lx >= 0) q[lx] = x[lx];
    1272   1204432908 :   return q;
    1273              : }
    1274              : INLINE GEN
    1275   4192333117 : gc_INT(pari_sp av, GEN x)
    1276              : {
    1277   4192333117 :   if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
    1278   3737178269 :   set_avma((pari_sp)icopy_avma(x, av));
    1279   3737178269 :   return (GEN)avma;
    1280              : }
    1281              : INLINE void
    1282     11659440 : gc_INT_affii(pari_sp av, GEN x, GEN *y)
    1283              : {
    1284     11659440 :   long l = lg(*y);
    1285     11659440 :   if (lgefint(x) <= l && isonstack(*y))
    1286              :   {
    1287      9443249 :     affii(x,*y);
    1288      9443249 :     set_avma(av);
    1289              :   }
    1290              :   else
    1291      2216191 :     *y = gc_INT(av, x);
    1292     11659440 : }
    1293              : INLINE GEN
    1294   2014474580 : gc_upto(pari_sp av, GEN q)
    1295              : {
    1296   2014474580 :   if (!isonstack(q) || (GEN)av<=q) return gc_const(av,q);
    1297   1820206655 :   switch(typ(q))
    1298              :   { /* first all non recursive types */
    1299    531471102 :     case t_INT: return gc_INT(av, q);
    1300    391147555 :     case t_REAL:
    1301              :     case t_STR:
    1302    391147555 :     case t_VECSMALL: return gc_leaf(av,q);
    1303    897587998 :     default: return (GEN)((pari_sp)q + gc_stack_update(av, (pari_sp)(q+lg(q))));
    1304              :   }
    1305              : }
    1306              : 
    1307              : /* gc_upto(av, gcopy(x)) */
    1308              : INLINE GEN
    1309    239420789 : gc_GEN(pari_sp av, GEN x)
    1310              : {
    1311    239420789 :   if (is_recursive_t(typ(x)))
    1312              :   {
    1313    217135617 :     GENbin *p = copy_bin(x);
    1314    217135617 :     set_avma(av); return bin_copy(p);
    1315              :   }
    1316              :   else
    1317              :   {
    1318     22285172 :     set_avma(av);
    1319     22285172 :     if (x < (GEN)av) {
    1320     22067319 :       if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
    1321     22067319 :       x = leafcopy_avma(x, av);
    1322     22067319 :       set_avma((pari_sp)x);
    1323              :     } else
    1324       217853 :       x = leafcopy(x);
    1325     22285172 :     return x;
    1326              :   }
    1327              : }
    1328              : 
    1329              : INLINE void
    1330     48996922 : guncloneNULL(GEN x) { if (x) gunclone(x); }
    1331              : INLINE void
    1332      1555058 : guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
    1333              : 
    1334              : /* assume 1 <= n < 10 */
    1335              : INLINE GEN
    1336    103369344 : gc_all(pari_sp av, int n, ...)
    1337              : {
    1338              :   int i;
    1339    103369344 :   va_list a; va_start(a, n);
    1340    103369344 :   if (n < 10)
    1341              :   {
    1342              :     GEN *v[10];
    1343    342642437 :     for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
    1344    103369344 :     set_avma(av);
    1345    342642437 :     for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
    1346    103369344 :     va_end(a); return *v[0];
    1347              :   }
    1348              :   else
    1349              :   {
    1350            0 :     GEN z, **v = (GEN**)pari_malloc(n*sizeof(GEN*));
    1351            0 :     for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
    1352            0 :     set_avma(av);
    1353            0 :     for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
    1354            0 :     z = *v[0]; pari_free(v); va_end(a); return z;
    1355              :   }
    1356              : }
    1357              : 
    1358              : INLINE void
    1359      2217800 : gc_slice(pari_sp av, GEN x, int n)
    1360              : {
    1361              :   int i;
    1362     26390137 :   for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
    1363      2217800 :   set_avma(av);
    1364     26390137 :   for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
    1365      2217800 : }
    1366              : 
    1367              : /* p from copy_bin. Copy p->x back to stack, then destroy p */
    1368              : INLINE GEN
    1369    480632320 : bin_copy(GENbin *p)
    1370              : {
    1371              :   GEN x, y, base;
    1372              :   long dx, len;
    1373              : 
    1374    480632320 :   x   = p->x; if (!x) { pari_free(p); return gen_0; }
    1375    453638019 :   len = p->len;
    1376    453638019 :   base= p->base; dx = x - base;
    1377    453638019 :   y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
    1378    453638019 :   y += dx;
    1379    453638019 :   p->rebase(y, ((ulong)y-(ulong)x));
    1380    453638019 :   pari_free(p); return y;
    1381              : }
    1382              : 
    1383              : INLINE GEN
    1384    934270387 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
    1385              : 
    1386              : INLINE void
    1387    106229372 : cgiv(GEN x)
    1388              : {
    1389    106229372 :   pari_sp av = (pari_sp)(x+lg(x));
    1390    106229372 :   if (isonstack((GEN)av)) set_avma(av);
    1391    106229372 : }
    1392              : 
    1393              : INLINE void
    1394      1982231 : killblock(GEN x) { gunclone(x); }
    1395              : 
    1396              : INLINE int
    1397    334730895 : is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
    1398              : 
    1399              : /*******************************************************************/
    1400              : /*                                                                 */
    1401              : /*                    CONVERSION / ASSIGNMENT                      */
    1402              : /*                                                                 */
    1403              : /*******************************************************************/
    1404              : /* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
    1405              : INLINE GEN
    1406     18766243 : cxcompotor(GEN z, long prec)
    1407              : {
    1408     18766243 :   switch(typ(z))
    1409              :   {
    1410     11780077 :     case t_INT:  return itor(z, prec);
    1411       294462 :     case t_FRAC: return fractor(z, prec);
    1412      6691704 :     case t_REAL: return rtor(z, prec);
    1413            0 :     default: pari_err_TYPE("cxcompotor",z);
    1414              :              return NULL; /* LCOV_EXCL_LINE */
    1415              :   }
    1416              : }
    1417              : INLINE GEN
    1418      9248581 : cxtofp(GEN x, long prec)
    1419      9248581 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
    1420              : 
    1421              : INLINE GEN
    1422       338020 : cxtoreal(GEN q)
    1423       338020 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
    1424              : 
    1425              : INLINE double
    1426     66283891 : gtodouble(GEN x)
    1427              : {
    1428     66283891 :   if (typ(x)!=t_REAL) {
    1429      9629878 :     pari_sp av = avma;
    1430      9629878 :     x = gtofp(x, DEFAULTPREC);
    1431      9629878 :     if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
    1432      9629878 :     set_avma(av);
    1433              :   }
    1434     66283891 :   return rtodbl(x);
    1435              : }
    1436              : 
    1437              : INLINE int
    1438      2992307 : gisdouble(GEN x, double *g)
    1439              : {
    1440      2992307 :   if (typ(x)!=t_REAL) {
    1441        42338 :     pari_sp av = avma;
    1442        42338 :     x = gtofp(x, DEFAULTPREC);
    1443        42338 :     if (typ(x)!=t_REAL) return gc_double(av, 0);
    1444        42338 :     set_avma(av);
    1445              :   }
    1446      2992307 :   if (expo(x) >= 0x3ff) return 0;
    1447      2992307 :   *g = rtodbl(x); return 1;
    1448              : }
    1449              : 
    1450              : INLINE long
    1451     85888562 : gtos(GEN x) {
    1452     85888562 :   if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
    1453     85888548 :   return itos(x);
    1454              : }
    1455              : 
    1456              : INLINE ulong
    1457       102023 : gtou(GEN x) {
    1458       102023 :   if (typ(x) != t_INT || signe(x)<0)
    1459            0 :     pari_err_TYPE("gtou [integer >=0 expected]",x);
    1460       102023 :   return itou(x);
    1461              : }
    1462              : 
    1463              : INLINE GEN
    1464     48086710 : absfrac(GEN x)
    1465              : {
    1466     48086710 :   GEN y = cgetg(3, t_FRAC);
    1467     48086710 :   gel(y,1) = absi(gel(x,1));
    1468     48086710 :   gel(y,2) = icopy(gel(x,2)); return y;
    1469              : }
    1470              : INLINE GEN
    1471        28237 : absfrac_shallow(GEN x)
    1472        28237 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
    1473              : INLINE GEN
    1474      8131105 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
    1475              : INLINE GEN
    1476       120267 : Q_abs_shallow(GEN x)
    1477       120267 : { return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
    1478              : INLINE GEN
    1479        13097 : R_abs_shallow(GEN x)
    1480        13097 : { return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
    1481              : INLINE GEN
    1482            0 : R_abs(GEN x)
    1483            0 : { return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
    1484              : 
    1485              : /* Force z to be of type real/complex with floating point components */
    1486              : INLINE GEN
    1487    260503736 : gtofp(GEN z, long prec)
    1488              : {
    1489    260503736 :   switch(typ(z))
    1490              :   {
    1491    219774263 :     case t_INT:  return itor(z, prec);
    1492      4106414 :     case t_FRAC: return fractor(z, prec);
    1493     27458504 :     case t_REAL: return rtor(z, prec);
    1494      9164555 :     case t_COMPLEX: {
    1495      9164555 :       GEN a = gel(z,1), b = gel(z,2);
    1496      9164555 :       if (isintzero(b)) return cxcompotor(a, prec);
    1497      9164555 :       if (isintzero(a)) {
    1498       244214 :         GEN y = cgetg(3, t_COMPLEX);
    1499       244214 :         b = cxcompotor(b, prec);
    1500       244214 :         gel(y,1) = real_0_bit(expo(b) - prec);
    1501       244214 :         gel(y,2) = b; return y;
    1502              :       }
    1503      8920341 :       return cxtofp(z, prec);
    1504              :     }
    1505            0 :     case t_QUAD: return quadtofp(z, prec);
    1506            0 :     default: pari_err_TYPE("gtofp",z);
    1507              :              return NULL; /* LCOV_EXCL_LINE */
    1508              :   }
    1509              : }
    1510              : /* Force z to be of type real / int */
    1511              : INLINE GEN
    1512        22428 : gtomp(GEN z, long prec)
    1513              : {
    1514        22428 :   switch(typ(z))
    1515              :   {
    1516           42 :     case t_INT:  return z;
    1517        22386 :     case t_FRAC: return fractor(z, prec);
    1518            0 :     case t_REAL: return rtor(z, prec);
    1519            0 :     case t_QUAD: z = quadtofp(z, prec);
    1520            0 :                  if (typ(z) == t_REAL) return z;
    1521            0 :     default: pari_err_TYPE("gtomp",z);
    1522              :              return NULL; /* LCOV_EXCL_LINE */
    1523              :   }
    1524              : }
    1525              : 
    1526              : INLINE GEN
    1527      7173870 : RgX_gtofp(GEN x, long prec)
    1528     37433341 : { pari_APPLY_pol_normalized(gtofp(gel(x,i), prec)); }
    1529              : 
    1530              : INLINE GEN
    1531     34121248 : RgC_gtofp(GEN x, long prec)
    1532    222310639 : { pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
    1533              : 
    1534              : INLINE GEN
    1535           56 : RgV_gtofp(GEN x, long prec)
    1536         4781 : { pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
    1537              : 
    1538              : INLINE GEN
    1539      8264329 : RgM_gtofp(GEN x, long prec)
    1540     41753579 : { pari_APPLY_same(RgC_gtofp(gel(x,i), prec)) }
    1541              : 
    1542              : INLINE GEN
    1543          574 : RgC_gtomp(GEN x, long prec)
    1544        23002 : { pari_APPLY_type(t_COL, gtomp(gel(x,i), prec)) }
    1545              : 
    1546              : INLINE GEN
    1547           21 : RgM_gtomp(GEN x, long prec)
    1548          595 : { pari_APPLY_same(RgC_gtomp(gel(x,i), prec)) }
    1549              : 
    1550              : INLINE GEN
    1551        62751 : RgX_fpnorml2(GEN x, long prec)
    1552              : {
    1553        62751 :   pari_sp av = avma;
    1554        62751 :   return gc_upto(av, gnorml2(RgX_gtofp(x, prec)));
    1555              : }
    1556              : INLINE GEN
    1557       620024 : RgC_fpnorml2(GEN x, long prec)
    1558              : {
    1559       620024 :   pari_sp av = avma;
    1560       620024 :   return gc_upto(av, gnorml2(RgC_gtofp(x, prec)));
    1561              : }
    1562              : INLINE GEN
    1563        23337 : RgM_fpnorml2(GEN x, long prec)
    1564              : {
    1565        23337 :   pari_sp av = avma;
    1566        23337 :   return gc_upto(av, gnorml2(RgM_gtofp(x, prec)));
    1567              : }
    1568              : 
    1569              : /* y a t_REAL */
    1570              : INLINE void
    1571       665850 : affgr(GEN x, GEN y)
    1572              : {
    1573       665850 :   switch(typ(x)) {
    1574       289569 :     case t_INT:  affir(x,y); break;
    1575       376281 :     case t_REAL: affrr(x,y); break;
    1576            0 :     case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
    1577            0 :     default: pari_err_TYPE2("=",x,y);
    1578              :   }
    1579       665850 : }
    1580              : 
    1581              : INLINE GEN
    1582       250436 : affc_fixlg(GEN x, GEN res)
    1583              : {
    1584       250436 :   if (typ(x) == t_COMPLEX)
    1585              :   {
    1586       207410 :     affrr_fixlg(gel(x,1), gel(res,1));
    1587       207410 :     affrr_fixlg(gel(x,2), gel(res,2));
    1588              :   }
    1589              :   else
    1590              :   {
    1591        43026 :     set_avma((pari_sp)(res+3));
    1592        43026 :     res = cgetr(realprec(gel(res,1)));
    1593        43026 :     affrr_fixlg(x, res);
    1594              :   }
    1595       250436 :   return res;
    1596              : }
    1597              : 
    1598              : INLINE GEN
    1599            0 : trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
    1600              : 
    1601              : /*******************************************************************/
    1602              : /*                                                                 */
    1603              : /*                          LENGTH CONVERSIONS                     */
    1604              : /*                                                                 */
    1605              : /*******************************************************************/
    1606              : INLINE long
    1607        41829 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
    1608              : INLINE long
    1609        33559 : ndec2prec(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
    1610              : INLINE long
    1611         8270 : ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
    1612              : /* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
    1613              :  * to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
    1614              :  *   addl, shrl instead of subl, sarl, addl */
    1615              : INLINE long
    1616     13262380 : nbits2nlong(long x) {
    1617     13262380 :   return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1618              : }
    1619              : 
    1620              : INLINE long
    1621   1528181196 : nbits2extraprec(long x) {
    1622   1528181196 :   return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
    1623              : }
    1624              : 
    1625              : INLINE long
    1626    199165902 : nbits2prec(long x) {
    1627    199165902 :   return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
    1628              : }
    1629              : 
    1630              : INLINE long
    1631   4753444650 : prec2lg(long x) {
    1632   4753444650 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1633              : }
    1634              : /* ceil(x / sizeof(long)) */
    1635              : INLINE long
    1636    127802222 : nchar2nlong(long x) {
    1637    127802222 :   return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
    1638              : }
    1639              : INLINE long
    1640    108463957 : prec2nbits(long x) { return x; }
    1641              : INLINE double
    1642      3176882 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
    1643              : INLINE double
    1644       581910 : prec2nbits_mul(long x, double y) { return x * y; }
    1645              : INLINE long
    1646    144343293 : bit_prec(GEN x) { return realprec(x); }
    1647              : INLINE long
    1648   3245648975 : bit_accuracy(long x) { return (x-2) * BITS_IN_LONG; }
    1649              : INLINE long
    1650         9778 : prec2ndec(long x) { return (long)(x * LOG10_2); }
    1651              : INLINE long
    1652          228 : nbits2ndec(long x) { return prec2ndec(x); }
    1653              : INLINE long
    1654       476735 : precdbl(long x) {return x << 1;}
    1655              : INLINE long
    1656   8468647494 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
    1657              : INLINE long
    1658   8384133267 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
    1659              : 
    1660              : /*********************************************************************/
    1661              : /**                                                                 **/
    1662              : /**                      OPERATIONS MODULO m                        **/
    1663              : /**                                                                 **/
    1664              : /*********************************************************************/
    1665              : /* Assume m > 0, more efficient if 0 <= a, b < m */
    1666              : 
    1667              : INLINE GEN
    1668     83002417 : Fp_red(GEN a, GEN m) { return modii(a, m); }
    1669              : INLINE GEN
    1670    185108678 : Fp_add(GEN a, GEN b, GEN m)
    1671              : {
    1672    185108678 :   pari_sp av=avma;
    1673    185108678 :   GEN p = addii(a,b);
    1674    185108678 :   long s = signe(p);
    1675    185108678 :   if (!s) return p; /* = gen_0 */
    1676    171929879 :   if (s > 0) /* general case */
    1677              :   {
    1678    171672503 :     GEN t = subii(p, m);
    1679    171672502 :     s = signe(t);
    1680    171672502 :     if (!s) return gc_const(av, gen_0);
    1681    162490129 :     if (s < 0) return gc_const((pari_sp)p, p);
    1682     78112429 :     if (cmpii(t, m) < 0) return gc_INT(av, t); /* general case ! */
    1683      3292052 :     p = remii(t, m);
    1684              :   }
    1685              :   else
    1686       257376 :     p = modii(p, m);
    1687      3549427 :   return gc_INT(av, p);
    1688              : }
    1689              : INLINE GEN
    1690     21452011 : Fp_double(GEN x, GEN N)
    1691              : {
    1692     21452011 :   GEN z = shifti(x, 1);
    1693     21452010 :   return cmpii(z, N) >= 0? subii(z, N): z;
    1694              : }
    1695              : INLINE GEN
    1696    203815482 : Fp_sub(GEN a, GEN b, GEN m)
    1697              : {
    1698    203815482 :   pari_sp av=avma;
    1699    203815482 :   GEN p = subii(a,b);
    1700    203815478 :   long s = signe(p);
    1701    203815478 :   if (!s) return p; /* = gen_0 */
    1702    188144727 :   if (s > 0)
    1703              :   {
    1704     93480863 :     if (cmpii(p, m) < 0) return p; /* general case ! */
    1705      1485390 :     p = remii(p, m);
    1706              :   }
    1707              :   else
    1708              :   {
    1709     94663864 :     GEN t = addii(p, m);
    1710     94663862 :     if (!s) return gc_const(av, gen_0);
    1711     94663862 :     if (s > 0) return gc_INT(av, t); /* general case ! */
    1712     94663862 :     p = modii(t, m);
    1713              :   }
    1714     96149250 :   return gc_INT(av, p);
    1715              : }
    1716              : INLINE GEN
    1717     37478342 : Fp_neg(GEN b, GEN m)
    1718              : {
    1719     37478342 :   pari_sp av = avma;
    1720     37478342 :   long s = signe(b);
    1721              :   GEN p;
    1722     37478342 :   if (!s) return gen_0;
    1723     30043736 :   if (s > 0)
    1724              :   {
    1725     28780985 :     p = subii(m, b);
    1726     28780985 :     if (signe(p) >= 0) return p; /* general case ! */
    1727       614816 :     p = modii(p, m);
    1728              :   } else
    1729      1262751 :     p = remii(negi(b), m);
    1730      1877567 :   return gc_INT(av, p);
    1731              : }
    1732              : 
    1733              : INLINE GEN
    1734       243517 : Fp_halve(GEN a, GEN p)
    1735              : {
    1736       243517 :   if (mpodd(a)) a = addii(a,p);
    1737       243517 :   return shifti(a,-1);
    1738              : }
    1739              : 
    1740              : /* assume 0 <= u < p and ps2 = p>>1 */
    1741              : INLINE GEN
    1742     78861016 : Fp_center(GEN u, GEN p, GEN ps2)
    1743     78861016 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
    1744              : /* same without copy */
    1745              : INLINE GEN
    1746     18208029 : Fp_center_i(GEN u, GEN p, GEN ps2)
    1747     18208029 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
    1748              : 
    1749              : /* x + y*z mod p */
    1750              : INLINE GEN
    1751     11358940 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
    1752              : {
    1753              :   pari_sp av;
    1754     11358940 :   if (!signe(y) || !signe(z)) return Fp_red(x, p);
    1755     10891689 :   if (!signe(x)) return Fp_mul(z,y, p);
    1756      9847282 :   av = avma;
    1757      9847282 :   return gc_INT(av, modii(addii(x, mulii(y,z)), p));
    1758              : }
    1759              : 
    1760              : INLINE GEN
    1761    205140247 : Fp_mul(GEN a, GEN b, GEN m)
    1762              : {
    1763    205140247 :   pari_sp av=avma;
    1764              :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1765    205140247 :   (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
    1766    205140247 :   p = mulii(a,b);
    1767    205140247 :   set_avma(av); return modii(p,m);
    1768              : }
    1769              : INLINE GEN
    1770     69088032 : Fp_sqr(GEN a, GEN m)
    1771              : {
    1772     69088032 :   pari_sp av=avma;
    1773              :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1774     69088032 :   (void)new_chunk((lg(a)+lg(m))<<1);
    1775     69088031 :   p = sqri(a);
    1776     69088025 :   set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
    1777              : }
    1778              : INLINE GEN
    1779     71121272 : Fp_mulu(GEN a, ulong b, GEN m)
    1780              : {
    1781     71121272 :   long l = lgefint(m);
    1782     71121272 :   if (l == 3)
    1783              :   {
    1784     51408961 :     ulong mm = m[2];
    1785     51408961 :     return utoi( Fl_mul(umodiu(a, mm), b, mm) );
    1786              :   } else {
    1787     19712311 :     pari_sp av = avma;
    1788              :     GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1789     19712311 :     (void)new_chunk(lg(a)+1+(l<<1));
    1790     19712311 :     p = muliu(a,b);
    1791     19712311 :     set_avma(av); return modii(p,m);
    1792              :   }
    1793              : }
    1794              : INLINE GEN
    1795        17640 : Fp_muls(GEN a, long b, GEN m)
    1796              : {
    1797        17640 :   long l = lgefint(m);
    1798        17640 :   if (l == 3)
    1799              :   {
    1800         3438 :     ulong mm = m[2];
    1801         3438 :     if (b < 0)
    1802              :     {
    1803         3438 :       ulong t = Fl_mul(umodiu(a, mm), -b, mm);
    1804         3438 :       return t? utoipos(mm - t): gen_0;
    1805              :     }
    1806              :     else
    1807            0 :       return utoi( Fl_mul(umodiu(a, mm), b, mm) );
    1808              :   } else {
    1809        14202 :     pari_sp av = avma;
    1810              :     GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1811        14202 :     (void)new_chunk(lg(a)+1+(l<<1));
    1812        14202 :     p = mulis(a,b);
    1813        14202 :     set_avma(av); return modii(p,m);
    1814              :   }
    1815              : }
    1816              : 
    1817              : INLINE GEN
    1818     23541654 : Fp_inv(GEN a, GEN m)
    1819              : {
    1820              :   GEN res;
    1821     23541654 :   if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
    1822     23541326 :   return res;
    1823              : }
    1824              : INLINE GEN
    1825      1098685 : Fp_invsafe(GEN a, GEN m)
    1826              : {
    1827              :   GEN res;
    1828      1098685 :   if (! invmod(a,m,&res)) return NULL;
    1829      1098685 :   return res;
    1830              : }
    1831              : INLINE GEN
    1832     14993238 : Fp_div(GEN a, GEN b, GEN m)
    1833              : {
    1834     14993238 :   pari_sp av = avma;
    1835              :   GEN p;
    1836     14993238 :   if (lgefint(b) == 3)
    1837              :   {
    1838     13848654 :     a = Fp_divu(a, b[2], m);
    1839     13848654 :     if (signe(b) < 0) a = Fp_neg(a, m);
    1840     13848654 :     return a;
    1841              :   }
    1842              :   /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1843      1144584 :   (void)new_chunk(lg(a)+(lg(m)<<1));
    1844      1144584 :   p = mulii(a, Fp_inv(b,m));
    1845      1144584 :   set_avma(av); return modii(p,m);
    1846              : }
    1847              : INLINE GEN
    1848     15087984 : Fp_divu(GEN x, ulong a, GEN p)
    1849              : {
    1850     15087984 :   pari_sp av = avma;
    1851              :   ulong b;
    1852     15087984 :   if (lgefint(p) == 3)
    1853              :   {
    1854     14560101 :     ulong pp = p[2], xp = umodiu(x, pp);
    1855     14560101 :     return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
    1856              :   }
    1857       527883 :   x = Fp_red(x, p);
    1858       527883 :   b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
    1859       527883 :   return gc_INT(av, diviuexact(addmuliu(x, p, b), a));
    1860              : }
    1861              : 
    1862              : INLINE GEN
    1863      1155304 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
    1864              : 
    1865              : INLINE GEN
    1866      3311089 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1867              : 
    1868              : INLINE long
    1869      2176817 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
    1870              : 
    1871              : INLINE long
    1872      1936696 : get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
    1873              : 
    1874              : INLINE GEN
    1875          343 : get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1876              : 
    1877              : INLINE long
    1878       376106 : get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1879              : 
    1880              : INLINE long
    1881       201185 : get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1882              : 
    1883              : INLINE GEN
    1884     25663053 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1885              : 
    1886              : INLINE long
    1887     69185175 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
    1888              : 
    1889              : INLINE long
    1890     83426280 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1891              : 
    1892              : INLINE GEN
    1893         8033 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1894              : 
    1895              : INLINE long
    1896       266299 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1897              : 
    1898              : INLINE long
    1899       320067 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1900              : 
    1901              : INLINE GEN
    1902      3391363 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1903              : 
    1904              : INLINE long
    1905      6879915 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1906              : 
    1907              : INLINE long
    1908      8094089 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1909              : 
    1910              : INLINE GEN
    1911       146583 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
    1912              : 
    1913              : INLINE long
    1914        51430 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
    1915              : 
    1916              : INLINE long
    1917         3569 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
    1918              : 
    1919              : /*******************************************************************/
    1920              : /*                                                                 */
    1921              : /*                        ADDMULII / SUBMULII                      */
    1922              : /*                                                                 */
    1923              : /*******************************************************************/
    1924              : /* x - y*z */
    1925              : INLINE GEN
    1926     33023974 : submulii(GEN x, GEN y, GEN z)
    1927              : {
    1928     33023974 :   long lx = lgefint(x), ly, lz;
    1929              :   pari_sp av;
    1930              :   GEN t;
    1931     33023974 :   if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
    1932     30277746 :   ly = lgefint(y);
    1933     30277746 :   if (ly == 2) return icopy(x);
    1934     28966317 :   lz = lgefint(z);
    1935     28966317 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1936     28966317 :   t = mulii(z, y);
    1937     28966317 :   set_avma(av); return subii(x,t);
    1938              : }
    1939              : /* y*z - x */
    1940              : INLINE GEN
    1941      3899482 : mulsubii(GEN y, GEN z, GEN x)
    1942              : {
    1943      3899482 :   long lx = lgefint(x), ly, lz;
    1944              :   pari_sp av;
    1945              :   GEN t;
    1946      3899482 :   if (lx == 2) return mulii(z,y);
    1947      2461806 :   ly = lgefint(y);
    1948      2461806 :   if (ly == 2) return negi(x);
    1949      2175878 :   lz = lgefint(z);
    1950      2175878 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1951      2175878 :   t = mulii(z, y);
    1952      2175878 :   set_avma(av); return subii(t,x);
    1953              : }
    1954              : 
    1955              : /* x - u*y */
    1956              : INLINE GEN
    1957         7700 : submuliu(GEN x, GEN y, ulong u)
    1958              : {
    1959              :   pari_sp av;
    1960         7700 :   long ly = lgefint(y);
    1961         7700 :   if (ly == 2) return icopy(x);
    1962         7700 :   av = avma;
    1963         7700 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1964         7700 :   y = mului(u,y);
    1965         7700 :   set_avma(av); return subii(x, y);
    1966              : }
    1967              : /* x + u*y */
    1968              : INLINE GEN
    1969       549832 : addmuliu(GEN x, GEN y, ulong u)
    1970              : {
    1971              :   pari_sp av;
    1972       549832 :   long ly = lgefint(y);
    1973       549832 :   if (ly == 2) return icopy(x);
    1974       549832 :   av = avma;
    1975       549832 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1976       549832 :   y = mului(u,y);
    1977       549832 :   set_avma(av); return addii(x, y);
    1978              : }
    1979              : /* x - u*y */
    1980              : INLINE GEN
    1981     66526418 : submuliu_inplace(GEN x, GEN y, ulong u)
    1982              : {
    1983              :   pari_sp av;
    1984     66526418 :   long ly = lgefint(y);
    1985     66526418 :   if (ly == 2) return x;
    1986     43410641 :   av = avma;
    1987     43410641 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1988     43410641 :   y = mului(u,y);
    1989     43410641 :   set_avma(av); return subii(x, y);
    1990              : }
    1991              : /* x + u*y */
    1992              : INLINE GEN
    1993     66021124 : addmuliu_inplace(GEN x, GEN y, ulong u)
    1994              : {
    1995              :   pari_sp av;
    1996     66021124 :   long ly = lgefint(y);
    1997     66021124 :   if (ly == 2) return x;
    1998     43353056 :   av = avma;
    1999     43353056 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    2000     43353056 :   y = mului(u,y);
    2001     43353056 :   set_avma(av); return addii(x, y);
    2002              : }
    2003              : /* ux + vy */
    2004              : INLINE GEN
    2005     38348562 : lincombii(GEN u, GEN v, GEN x, GEN y)
    2006              : {
    2007     38348562 :   long lx = lgefint(x), ly;
    2008              :   GEN p1, p2;
    2009              :   pari_sp av;
    2010     38348562 :   if (lx == 2) return mulii(v,y);
    2011     24113954 :   ly = lgefint(y);
    2012     24113954 :   if (ly == 2) return mulii(u,x);
    2013     21372297 :   av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
    2014     21372297 :   p1 = mulii(u,x);
    2015     21372297 :   p2 = mulii(v,y);
    2016     21372297 :   set_avma(av); return addii(p1,p2);
    2017              : }
    2018              : 
    2019              : /*******************************************************************/
    2020              : /*                                                                 */
    2021              : /*                          GEN SUBTYPES                           */
    2022              : /*                                                                 */
    2023              : /*******************************************************************/
    2024              : 
    2025              : INLINE int
    2026   5001693798 : is_const_t(long t) { return (t < t_POLMOD); }
    2027              : INLINE int
    2028         6904 : is_extscalar_t(long t) { return (t <= t_POL); }
    2029              : INLINE int
    2030      9716551 : is_intreal_t(long t) { return (t <= t_REAL); }
    2031              : INLINE int
    2032    639613654 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
    2033              : INLINE int
    2034     86220160 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
    2035              : INLINE int
    2036            0 : is_qfb_t(long t) { return (t == t_QFB); }
    2037              : INLINE int
    2038      5047069 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
    2039              : INLINE int
    2040     62080000 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
    2041              : INLINE int
    2042   9573279592 : is_recursive_t(long t) { return lontyp[t]; }
    2043              : INLINE int
    2044    376859516 : is_scalar_t(long t) { return (t < t_POL); }
    2045              : INLINE int
    2046     44253505 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
    2047              : 
    2048              : INLINE int
    2049     65341381 : qfb_is_qfi(GEN q) { return signe(gel(q,4)) < 0; }
    2050              : 
    2051              : /*******************************************************************/
    2052              : /*                                                                 */
    2053              : /*                         TRANSCENDENTAL                          */
    2054              : /*                                                                 */
    2055              : /*******************************************************************/
    2056              : INLINE GEN
    2057     87238835 : sqrtr(GEN x) {
    2058     87238835 :   long s = signe(x);
    2059     87238835 :   if (s == 0) return real_0_bit(expo(x) >> 1);
    2060     87205650 :   if (s >= 0) return sqrtr_abs(x);
    2061       443246 :   retmkcomplex(gen_0, sqrtr_abs(x));
    2062              : }
    2063              : INLINE GEN
    2064            0 : cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
    2065              : INLINE GEN
    2066            0 : cbrtr(GEN x) {
    2067            0 :   long s = signe(x);
    2068              :   GEN r;
    2069            0 :   if (s == 0) return real_0_bit(expo(x) / 3);
    2070            0 :   r = cbrtr_abs(x);
    2071            0 :   if (s < 0) togglesign(r);
    2072            0 :   return r;
    2073              : }
    2074              : INLINE GEN
    2075      2811966 : sqrtnr(GEN x, long n) {
    2076      2811966 :   long s = signe(x);
    2077              :   GEN r;
    2078      2811966 :   if (s == 0) return real_0_bit(expo(x) / n);
    2079      2811966 :   r = sqrtnr_abs(x, n);
    2080      2811966 :   if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
    2081      2811966 :   return r;
    2082              : }
    2083              : INLINE long
    2084       757174 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
    2085              : INLINE ulong
    2086      2116695 : ulogint(ulong B, ulong y)
    2087              : {
    2088              :   ulong r;
    2089              :   long e;
    2090      2116695 :   if (y == 2) return expu(B);
    2091      2068165 :   r = y;
    2092      6270929 :   for (e=1;; e++)
    2093              :   { /* here, r = y^e, r2 = y^(e-1) */
    2094      6270929 :     if (r >= B) return r == B? e: e-1;
    2095      4204320 :     r = umuluu_or_0(y, r);
    2096      4204320 :     if (!r) return e;
    2097              :   }
    2098              : }
    2099              : 
    2100              : /*******************************************************************/
    2101              : /*                                                                 */
    2102              : /*                         MISCELLANEOUS                           */
    2103              : /*                                                                 */
    2104              : /*******************************************************************/
    2105      9716460 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
    2106   2910504674 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
    2107     18140988 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
    2108      6224531 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
    2109   1181232714 : INLINE int equali1(GEN n)
    2110   1181232714 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
    2111    139584506 : INLINE int equalim1(GEN n)
    2112    139584506 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
    2113              : /* works only for POSITIVE integers */
    2114   2295934027 : INLINE int is_pm1(GEN n)
    2115   2295934027 : { return lgefint(n) == 3 && n[2] == 1; }
    2116    589905559 : INLINE int is_bigint(GEN n)
    2117    589905559 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
    2118              : 
    2119   2140067927 : INLINE int odd(long x) { return x & 1; }
    2120    124810656 : INLINE int both_odd(long x, long y) { return x & y & 1; }
    2121              : 
    2122              : INLINE int
    2123   8495438898 : isonstack(GEN x)
    2124   8495438898 : { return ((pari_sp)x >= pari_mainstack->bot
    2125   8495438898 :        && (pari_sp)x <  pari_mainstack->top); }
    2126              : 
    2127              : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
    2128              : INLINE double
    2129     94958834 : dbllog2r(GEN x)
    2130     94958834 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
    2131              : 
    2132              : INLINE GEN
    2133      2660541 : mul_content(GEN cx, GEN cy)
    2134              : {
    2135      2660541 :   if (!cx) return cy;
    2136       568784 :   if (!cy) return cx;
    2137       374505 :   return gmul(cx,cy);
    2138              : }
    2139              : INLINE GEN
    2140            0 : inv_content(GEN c) { return c? ginv(c): NULL; }
    2141              : INLINE GEN
    2142        58501 : div_content(GEN cx, GEN cy)
    2143              : {
    2144        58501 :   if (!cy) return cx;
    2145        57367 :   if (!cx) return ginv(cy);
    2146        26019 :   return gdiv(cx,cy);
    2147              : }
    2148              : INLINE GEN
    2149     10783040 : mul_denom(GEN dx, GEN dy)
    2150              : {
    2151     10783040 :   if (!dx) return dy;
    2152      1903671 :   if (!dy) return dx;
    2153      1391453 :   return mulii(dx,dy);
    2154              : }
    2155              : 
    2156              : /* POLYNOMIALS */
    2157              : INLINE GEN
    2158      1065040 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
    2159              : INLINE GEN
    2160    169545342 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
    2161              : INLINE ulong
    2162      1066584 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
    2163              : INLINE ulong
    2164      1092086 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
    2165              : INLINE long
    2166   5647657768 : degpol(GEN x) { return lg(x)-3; }
    2167              : INLINE long
    2168   2771871999 : lgpol(GEN x) { return lg(x)-2; }
    2169              : INLINE long
    2170    354354817 : lgcols(GEN x) { return lg(gel(x,1)); }
    2171              : INLINE long
    2172    159332019 : nbrows(GEN x) { return lg(gel(x,1))-1; }
    2173              : INLINE GEN
    2174            0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
    2175              : 
    2176              : INLINE GEN
    2177      2192227 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
    2178              : INLINE GEN
    2179       963420 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
    2180              : 
    2181              : INLINE GEN
    2182    103802800 : RgX_copy(GEN x)
    2183    377978441 : { pari_APPLY_pol_normalized(gcopy(gel(x,i))); }
    2184              : /* have to use ulong to avoid silly warnings from gcc "assuming signed
    2185              :  * overflow does not occur" */
    2186              : INLINE GEN
    2187      2909086 : RgX_coeff(GEN x, long n)
    2188              : {
    2189      2909086 :   ulong l = lg(x);
    2190      2909086 :   return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
    2191              : }
    2192              : INLINE GEN
    2193        34135 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
    2194              : INLINE GEN
    2195      7001097 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
    2196              : INLINE GEN
    2197         1904 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
    2198              : INLINE GEN
    2199       127908 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
    2200              : INLINE GEN
    2201      5609820 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
    2202              : INLINE GEN
    2203      2003309 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
    2204              : INLINE GEN
    2205     21817862 : Flx_div_pre(GEN x, GEN y, ulong p, ulong pi)
    2206     21817862 : { return Flx_divrem_pre(x,y,p,pi, NULL); }
    2207              : INLINE GEN
    2208      1579897 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
    2209              : INLINE GEN
    2210            0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
    2211              : INLINE GEN
    2212    114795902 : pol0_Flx(long sv) { return mkvecsmall(sv); }
    2213              : INLINE GEN
    2214     56079249 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
    2215              : INLINE GEN
    2216     37978369 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
    2217              : INLINE GEN
    2218            0 : zero_zx(long sv) { return zero_Flx(sv); }
    2219              : INLINE GEN
    2220            0 : polx_zx(long sv) { return polx_Flx(sv); }
    2221              : INLINE GEN
    2222            0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
    2223              : INLINE GEN
    2224        14049 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
    2225              : INLINE GEN
    2226         1350 : zero_F2x(long sv) { return zero_Flx(sv); }
    2227              : INLINE GEN
    2228     17432385 : pol0_F2x(long sv) { return pol0_Flx(sv); }
    2229              : INLINE GEN
    2230      4777984 : pol1_F2x(long sv) { return pol1_Flx(sv); }
    2231              : INLINE GEN
    2232      1083528 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
    2233              : INLINE int
    2234      2871869 : F2x_equal1(GEN x) { return Flx_equal1(x); }
    2235              : INLINE int
    2236      4256902 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
    2237              : INLINE GEN
    2238     66590817 : F2x_copy(GEN x) { return leafcopy(x); }
    2239              : INLINE GEN
    2240         4893 : F2v_copy(GEN x) { return leafcopy(x); }
    2241              : INLINE GEN
    2242      2251230 : Flv_copy(GEN x) { return leafcopy(x); }
    2243              : INLINE GEN
    2244    196470581 : Flx_copy(GEN x) { return leafcopy(x); }
    2245              : INLINE GEN
    2246      7353981 : vecsmall_copy(GEN x) { return leafcopy(x); }
    2247              : INLINE int
    2248      8248281 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
    2249              : INLINE int
    2250        10514 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
    2251              : INLINE int
    2252      7723160 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
    2253              : 
    2254              : INLINE GEN
    2255    158573561 : ZX_renormalize(GEN x, long lx)    { return ZXX_renormalize(x,lx); }
    2256              : INLINE GEN
    2257    146564168 : FpX_renormalize(GEN x, long lx)   { return ZXX_renormalize(x,lx); }
    2258              : INLINE GEN
    2259       782142 : FpXX_renormalize(GEN x, long lx)  { return ZXX_renormalize(x,lx); }
    2260              : INLINE GEN
    2261      4641900 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
    2262              : INLINE GEN
    2263    130597024 : F2x_renormalize(GEN x, long lx)   { return Flx_renormalize(x,lx); }
    2264              : 
    2265              : INLINE GEN
    2266            0 : F2xX_shift(GEN a, long n, long vs) { return FlxX_shift(a, n, vs); }
    2267              : 
    2268              : INLINE GEN
    2269        38959 : F2v_to_F2x(GEN x, long sv) {
    2270        38959 :   GEN y = leafcopy(x);
    2271        38959 :   y[1] = sv; F2x_renormalize(y, lg(y)); return y;
    2272              : }
    2273              : 
    2274              : INLINE long
    2275          385 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
    2276              : 
    2277              : INLINE long
    2278         6391 : gval(GEN x, long v)
    2279         6391 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
    2280              : 
    2281              : INLINE void
    2282       479616 : RgX_shift_inplace_init(long v)
    2283       479616 : { if (v) (void)cgetg(v, t_VECSMALL); }
    2284              : /* shift polynomial in place. assume v free cells have been left before x */
    2285              : INLINE GEN
    2286       479616 : RgX_shift_inplace(GEN x, long v)
    2287              : {
    2288              :   long i, lx;
    2289              :   GEN z;
    2290       479616 :   if (!v) return x;
    2291       252260 :   lx = lg(x);
    2292       252260 :   if (lx == 2) return x;
    2293       252260 :   z = x + lx;
    2294              :   /* stackdummy's from normalizepol */
    2295       252260 :   while (lg(z) != v) z += lg(z);
    2296       252260 :   z += v;
    2297      4637681 :   for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
    2298       672402 :   for (i = 0;  i < v; i++) gel(--z,0) = gen_0;
    2299       252260 :   z -= 2;
    2300       252260 :   z[1] = x[1];
    2301       252260 :   z[0] = evaltyp(t_POL) | evallg(lx+v);
    2302       252260 :   stackdummy((pari_sp)z, (pari_sp)x); return z;
    2303              : }
    2304              : 
    2305              : 
    2306              : /* LINEAR ALGEBRA */
    2307              : INLINE GEN
    2308       504212 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
    2309              : INLINE GEN
    2310      7979032 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
    2311              : INLINE GEN
    2312      3318879 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
    2313              : INLINE GEN
    2314            0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
    2315              : INLINE GEN
    2316       104146 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
    2317              : INLINE GEN
    2318            0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
    2319              : INLINE GEN
    2320            0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
    2321              : INLINE GEN
    2322     39191510 : zero_zv(long x) { return zero_Flv(x); }
    2323              : INLINE GEN
    2324          266 : zm_transpose(GEN x) { return Flm_transpose(x); }
    2325              : INLINE GEN
    2326            0 : zm_copy(GEN x) { return Flm_copy(x); }
    2327              : INLINE GEN
    2328      1879869 : zv_copy(GEN x) { return Flv_copy(x); }
    2329              : INLINE GEN
    2330            0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
    2331              : 
    2332              : INLINE GEN
    2333     10699829 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfdivrem(x,y,NULL); }
    2334              : INLINE GEN
    2335       401485 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
    2336              : INLINE GEN
    2337      7142551 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
    2338              : INLINE void
    2339      9956603 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
    2340              : INLINE GEN
    2341     22241375 : RgM_shallowcopy(GEN x)
    2342     96470494 : { pari_APPLY_same(leafcopy(gel(x,i))); }
    2343              : INLINE GEN
    2344       201411 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
    2345              : 
    2346              : INLINE GEN
    2347            0 : F3m_copy(GEN x) { return RgM_shallowcopy(x); }
    2348              : 
    2349              : INLINE GEN
    2350      6442240 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
    2351              : 
    2352              : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
    2353              :  * x,y are ZV of the same length */
    2354              : INLINE int
    2355        23485 : ZV_dvd(GEN x, GEN y)
    2356              : {
    2357        23485 :   long i, l = lg(x);
    2358        37856 :   for (i=1; i < l; i++)
    2359        30464 :     if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
    2360         7392 :   return 1;
    2361              : }
    2362              : INLINE GEN
    2363       610999 : ZM_ZV_mod(GEN x, GEN y)
    2364      1796144 : { pari_APPLY_same(ZV_ZV_mod(gel(x,i), y)) }
    2365              : INLINE GEN
    2366      8148008 : ZV_ZV_mod(GEN x, GEN y)
    2367     22536744 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
    2368              : INLINE GEN
    2369            0 : vecmodii(GEN x, GEN y) { return ZV_ZV_mod(x,y); }
    2370              : INLINE GEN
    2371       174881 : vecmoduu(GEN x, GEN y) { pari_APPLY_ulong(((ulong*)x)[i] % ((ulong*)y)[i]) }
    2372              : 
    2373              : /* Fq */
    2374              : INLINE GEN
    2375      3109222 : Fq_red(GEN x, GEN T, GEN p)
    2376      3109222 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
    2377              : INLINE GEN
    2378        73652 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
    2379              : {
    2380              :   (void) p;
    2381        73652 :   return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
    2382              : }
    2383              : INLINE GEN
    2384          756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
    2385              : 
    2386              : INLINE GEN
    2387        29946 : gener_Fq_local(GEN T, GEN p, GEN L)
    2388            0 : { return T? gener_FpXQ_local(T,p, L)
    2389        29946 :           : pgener_Fp_local(p, L); }
    2390              : 
    2391              : INLINE GEN
    2392            0 : random_Fq(GEN T, GEN p)
    2393            0 : { return T ? random_FpX(get_FpX_degree(T), get_FpX_var(T), p): randomi(p); }
    2394              : 
    2395              : /* FpXQX */
    2396              : INLINE GEN
    2397         4519 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
    2398              : INLINE GEN
    2399            0 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
    2400              : INLINE GEN
    2401       165917 : FlxqX_div_pre(GEN x, GEN y, GEN T, ulong p, ulong pi) { return FlxqX_divrem_pre(x, y, T, p, pi, NULL); }
    2402              : INLINE GEN
    2403        33187 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
    2404              : 
    2405              : INLINE GEN
    2406        20405 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    2407        20405 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
    2408              : 
    2409              : /* FqX */
    2410              : INLINE GEN
    2411        25298 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
    2412              : INLINE GEN
    2413       124082 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2414              : INLINE GEN
    2415        24900 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
    2416              : INLINE GEN
    2417        68026 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2418              : INLINE GEN
    2419       579197 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
    2420       579197 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
    2421              : INLINE GEN
    2422       424510 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
    2423       424510 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
    2424              : INLINE GEN
    2425       368912 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
    2426       368912 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
    2427              : INLINE GEN
    2428        21900 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
    2429        21900 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
    2430              : INLINE GEN
    2431        71232 : FqX_sqr(GEN x, GEN T, GEN p)
    2432        71232 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
    2433              : INLINE GEN
    2434         1288 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
    2435         1288 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
    2436              : INLINE GEN
    2437        19250 : FqX_halve(GEN x, GEN T, GEN p)
    2438        19250 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
    2439              : INLINE GEN
    2440        38569 : FqX_div(GEN x, GEN y, GEN T, GEN p)
    2441        38569 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
    2442              : INLINE GEN
    2443         6069 : FqX_get_red(GEN S, GEN T, GEN p)
    2444         6069 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
    2445              : INLINE GEN
    2446        37659 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
    2447        37659 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
    2448              : INLINE GEN
    2449            0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2450            0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
    2451              : INLINE GEN
    2452        69699 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2453        69699 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
    2454              : INLINE GEN
    2455            0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
    2456            0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
    2457              : INLINE GEN
    2458         8288 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
    2459         8288 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
    2460              : INLINE GEN
    2461       449519 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
    2462       449519 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
    2463              : INLINE GEN
    2464         4144 : FqX_normalize(GEN z, GEN T, GEN p)
    2465         4144 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
    2466              : INLINE GEN
    2467        39697 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
    2468              : INLINE long
    2469       683718 : FqX_is_squarefree(GEN P, GEN T, GEN p)
    2470       683718 : { return T ? FpXQX_is_squarefree(P, T, p): FpX_is_squarefree(P, p); }
    2471              : INLINE GEN
    2472            0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
    2473              : INLINE GEN
    2474       140960 : FqX_factor(GEN f, GEN T, GEN p)
    2475       140960 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
    2476              : INLINE GEN
    2477            7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
    2478            7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
    2479              : INLINE GEN
    2480            7 : FqX_ddf(GEN f, GEN T, GEN p)
    2481            7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
    2482              : INLINE GEN
    2483        47786 : FqX_degfact(GEN f, GEN T, GEN p)
    2484        47786 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
    2485              : INLINE GEN
    2486         7231 : FqX_roots(GEN f, GEN T, GEN p)
    2487         7231 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
    2488              : INLINE GEN
    2489          175 : FqX_to_mod(GEN f, GEN T, GEN p)
    2490          175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
    2491              : 
    2492              : /*FqXQ*/
    2493              : INLINE GEN
    2494            0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2495            0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2496              : INLINE GEN
    2497            0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2498            0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2499              : INLINE GEN
    2500            0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
    2501            0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
    2502              : INLINE GEN
    2503            0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
    2504            0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
    2505              : INLINE GEN
    2506            0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
    2507            0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
    2508              : INLINE GEN
    2509        22745 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
    2510        22745 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
    2511              : INLINE GEN
    2512            0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
    2513            0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
    2514              : INLINE GEN
    2515            0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    2516            0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
    2517              : 
    2518              : /*FqXn*/
    2519              : INLINE GEN
    2520         6590 : FqXn_expint(GEN x, long n, GEN T, GEN p)
    2521         6590 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
    2522              : INLINE GEN
    2523            0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
    2524            0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
    2525              : INLINE GEN
    2526         7070 : FqXn_inv(GEN x, long n, GEN T, GEN p)
    2527         7070 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
    2528              : INLINE GEN
    2529       261240 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
    2530       261240 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
    2531              : INLINE GEN
    2532            0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
    2533            0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
    2534              : 
    2535              : /*FpXQ*/
    2536              : INLINE GEN
    2537            0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2538            0 : { (void)T; return FpX_add(x,y,p); }
    2539              : INLINE GEN
    2540            0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2541            0 : { (void)T; return FpX_sub(x,y,p); }
    2542              : 
    2543              : /*Flxq*/
    2544              : INLINE GEN
    2545            0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2546            0 : { (void)T; return Flx_add(x,y,p); }
    2547              : INLINE GEN
    2548            0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2549            0 : { (void)T; return Flx_sub(x,y,p); }
    2550              : 
    2551              : /* F2x */
    2552              : 
    2553              : INLINE ulong
    2554    672512608 : F2x_coeff(GEN x,long v)
    2555              : {
    2556    672512608 :    ulong u=(ulong)x[2+divsBIL(v)];
    2557    672512608 :    return (u>>remsBIL(v))&1UL;
    2558              : }
    2559              : 
    2560              : INLINE void
    2561     12463961 : F2x_clear(GEN x,long v)
    2562              : {
    2563     12463961 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2564     12463961 :    *u&=~(1UL<<remsBIL(v));
    2565     12463961 : }
    2566              : 
    2567              : INLINE void
    2568    130281215 : F2x_set(GEN x,long v)
    2569              : {
    2570    130281215 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2571    130281215 :    *u|=1UL<<remsBIL(v);
    2572    130281215 : }
    2573              : 
    2574              : INLINE void
    2575      1560492 : F2x_flip(GEN x,long v)
    2576              : {
    2577      1560492 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2578      1560492 :    *u^=1UL<<remsBIL(v);
    2579      1560492 : }
    2580              : 
    2581              : /* F2v */
    2582              : 
    2583              : INLINE ulong
    2584    665814570 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
    2585              : 
    2586              : INLINE void
    2587     12463961 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
    2588              : 
    2589              : INLINE void
    2590    104219398 : F2v_set(GEN x,long v)   { F2x_set(x,v-1); }
    2591              : 
    2592              : INLINE void
    2593      1560492 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
    2594              : 
    2595              : /* F2m */
    2596              : 
    2597              : INLINE ulong
    2598      8462959 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
    2599              : 
    2600              : INLINE void
    2601            0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
    2602              : 
    2603              : INLINE void
    2604          798 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
    2605              : 
    2606              : INLINE void
    2607      1560492 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
    2608              : 
    2609              : /* F3m */
    2610              : 
    2611              : INLINE ulong
    2612       144859 : F3m_coeff(GEN x, long a, long b) { return F3v_coeff(gel(x,b), a); }
    2613              : 
    2614              : INLINE void
    2615            0 : F3m_set(GEN x, long a, long b, ulong c) { F3v_set(gel(x,b), a, c); }
    2616              : 
    2617              : /* ARITHMETIC */
    2618              : INLINE GEN
    2619         4935 : matpascal(long n) { return matqpascal(n, NULL); }
    2620              : INLINE long
    2621       509832 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
    2622              : INLINE long
    2623           98 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
    2624              : INLINE GEN
    2625      6618091 : sqrti(GEN x) { return sqrtremi(x,NULL); }
    2626              : INLINE GEN
    2627    123273820 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
    2628              : INLINE int
    2629       761951 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
    2630              : INLINE int
    2631        29246 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
    2632              : INLINE GEN
    2633            0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
    2634              : INLINE GEN
    2635            0 : gminsg(long s, GEN y) { return gmings(y,s); }
    2636              : INLINE GEN
    2637     29751800 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
    2638              : INLINE GEN
    2639    283377954 : gmulgu(GEN y, ulong s) { return gmulug(s,y); }
    2640              : INLINE GEN
    2641      1731854 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
    2642              : INLINE GEN
    2643       561958 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
    2644              : 
    2645              : INLINE GEN
    2646     13510262 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
    2647              : INLINE GEN
    2648       902770 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
    2649              : 
    2650              : /* x t_COMPLEX */
    2651              : INLINE GEN
    2652     95484246 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
    2653              : /* q t_QUAD */
    2654              : INLINE GEN
    2655        70105 : quadnorm(GEN q)
    2656              : {
    2657        70105 :   GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
    2658        70105 :   GEN z, u = gel(q,3), v = gel(q,2);
    2659        70105 :   if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
    2660              :   {
    2661        69720 :     z = signe(b)? mulii(v, addii(u,v)): sqri(v);
    2662        69720 :     return addii(z, mulii(c, sqri(u)));
    2663              :   }
    2664              :   else
    2665              :   {
    2666          385 :     z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
    2667          385 :     return gadd(z, gmul(c, gsqr(u)));
    2668              :   }
    2669              : }
    2670              : /* x a t_QUAD, return the attached discriminant */
    2671              : INLINE GEN
    2672         1379 : quad_disc(GEN x)
    2673              : {
    2674         1379 :   GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
    2675         1379 :   if (is_pm1(b)) return subsi(1, c4);
    2676          392 :   togglesign_safe(&c4); return c4;
    2677              : }
    2678              : INLINE GEN
    2679      5002025 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
    2680              : INLINE GEN
    2681     18522407 : qfb_disc(GEN x) { return gel(x,4); }
    2682              : 
    2683              : INLINE GEN
    2684      4399698 : sqrfrac(GEN x)
    2685              : {
    2686      4399698 :   GEN z = cgetg(3,t_FRAC);
    2687      4399698 :   gel(z,1) = sqri(gel(x,1));
    2688      4399698 :   gel(z,2) = sqri(gel(x,2)); return z;
    2689              : }
    2690              : 
    2691              : INLINE void
    2692     17758204 : normalize_frac(GEN z) {
    2693     17758204 :   if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
    2694     17758204 : }
    2695              : 
    2696              : INLINE GEN
    2697     43757892 : powii(GEN x, GEN n)
    2698              : {
    2699     43757892 :   long ln = lgefint(n);
    2700     43757892 :   if (ln == 3) {
    2701              :     GEN z;
    2702     43716267 :     if (signe(n) > 0) return powiu(x, n[2]);
    2703        75075 :     z = cgetg(3, t_FRAC);
    2704        75075 :     gel(z,1) = gen_1;
    2705        75075 :     gel(z,2) = powiu(x, n[2]);
    2706        75075 :     return z;
    2707              :   }
    2708        41625 :   if (ln == 2) return gen_1; /* rare */
    2709              :   /* should never happen */
    2710            0 :   return powgi(x, n); /* overflow unless x = 0, 1, -1 */
    2711              : }
    2712              : INLINE GEN
    2713       620095 : powIs(long n) {
    2714       620095 :   switch(n & 3)
    2715              :   {
    2716       154938 :     case 1: return mkcomplex(gen_0,gen_1);
    2717       156632 :     case 2: return gen_m1;
    2718        91308 :     case 3: return mkcomplex(gen_0,gen_m1);
    2719              :   }
    2720       217217 :   return gen_1;
    2721              : }
    2722              : 
    2723              : /*******************************************************************/
    2724              : /*                                                                 */
    2725              : /*                       ELLIPTIC CURVES                           */
    2726              : /*                                                                 */
    2727              : /*******************************************************************/
    2728     17419146 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
    2729     11293929 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
    2730     13229559 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
    2731     10507553 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
    2732     10552258 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
    2733      7552268 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
    2734      2550720 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
    2735      3863696 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
    2736      3297568 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
    2737     13581157 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
    2738     16040406 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
    2739     18018733 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
    2740      2126899 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
    2741     22568894 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
    2742      1791876 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
    2743       551897 : INLINE GEN ellff_get_a4a6(GEN x)  { return gmael(x, 15, 2); }
    2744         1463 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
    2745          308 : INLINE long ellQp_get_prec(GEN E) { return valp(ellQp_get_zero(E)); }
    2746         1134 : INLINE GEN ellQp_get_p(GEN E) { return padic_p(ellQp_get_zero(E)); }
    2747       232939 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
    2748       371829 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
    2749      2027312 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
    2750          119 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
    2751              : 
    2752     10828010 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
    2753     59322527 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
    2754      1097909 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
    2755              : 
    2756              : /*******************************************************************/
    2757              : /*                                                                 */
    2758              : /*                    ALGEBRAIC NUMBER THEORY                      */
    2759              : /*                                                                 */
    2760              : /*******************************************************************/
    2761     31237605 : INLINE GEN modpr_get_pr(GEN x)  { return gel(x,3); }
    2762      1887627 : INLINE GEN modpr_get_p(GEN x)  { return pr_get_p(modpr_get_pr(x)); }
    2763     13867549 : INLINE GEN modpr_get_T(GEN x)  { return lg(x) == 4? NULL: gel(x,4); }
    2764              : 
    2765     92766024 : INLINE GEN pr_get_p(GEN pr)  { return gel(pr,1); }
    2766     12077978 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
    2767              : /* .[2] instead of itos works: e and f are small positive integers */
    2768     16165373 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
    2769     26447816 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
    2770     49777658 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
    2771              : INLINE int
    2772     17227870 : pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
    2773              : INLINE GEN
    2774      1090626 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
    2775              : INLINE ulong
    2776       347921 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
    2777              : 
    2778              : /* assume nf a genuine nf */
    2779              : INLINE long
    2780      1504035 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
    2781              : INLINE GEN
    2782     98183052 : nf_get_pol(GEN nf) { return gel(nf,1); }
    2783              : INLINE long
    2784     71400071 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
    2785              : INLINE long
    2786     23476650 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
    2787              : INLINE long
    2788         4154 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
    2789              : INLINE GEN
    2790       464441 : nf_get_disc(GEN nf) { return gel(nf,3); }
    2791              : INLINE GEN
    2792      3716981 : nf_get_index(GEN nf) { return gel(nf,4); }
    2793              : INLINE GEN
    2794     21751546 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
    2795              : INLINE GEN
    2796       304645 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
    2797              : INLINE GEN
    2798      1749568 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
    2799              : INLINE GEN
    2800        23352 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
    2801              : INLINE GEN
    2802         4445 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
    2803              : INLINE GEN
    2804        65098 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
    2805              : INLINE GEN
    2806      1632880 : nf_get_roots(GEN nf) { return gel(nf,6); }
    2807              : INLINE GEN
    2808         4382 : nf_get_zk(GEN nf)
    2809              : {
    2810         4382 :   GEN y = gel(nf,7), D = gel(y, 1);
    2811         4382 :   if (typ(D) == t_POL) D = gel(D, 2);
    2812         4382 :   if (!equali1(D)) y = gdiv(y, D);
    2813         4382 :   return y;
    2814              : }
    2815              : INLINE GEN
    2816      5419661 : nf_get_zkprimpart(GEN nf)
    2817              : {
    2818      5419661 :   GEN y = gel(nf,7);
    2819              :   /* test for old format of nf.zk: non normalized */
    2820      5419661 :   if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
    2821      5419661 :   return y;
    2822              : }
    2823              : INLINE GEN
    2824      5465385 : nf_get_zkden(GEN nf)
    2825              : {
    2826      5465385 :   GEN y = gel(nf,7), D = gel(y,1);
    2827      5465385 :   if (typ(D) == t_POL) D = gel(D,2);
    2828              :   /* test for old format of nf.zk: non normalized */
    2829      5465385 :   if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
    2830      5465385 :   return D;
    2831              : }
    2832              : INLINE GEN
    2833      9782854 : nf_get_invzk(GEN nf) { return gel(nf,8); }
    2834              : INLINE void
    2835       522482 : nf_get_sign(GEN nf, long *r1, long *r2)
    2836              : {
    2837       522482 :   GEN x = gel(nf,2);
    2838       522482 :   *r1 = itou(gel(x,1));
    2839       522482 :   *r2 = itou(gel(x,2));
    2840       522482 : }
    2841              : 
    2842              : INLINE GEN
    2843      2757632 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
    2844              : INLINE GEN
    2845       407115 : abgrp_get_no(GEN x) { return gel(x,1); }
    2846              : INLINE GEN
    2847     12235182 : abgrp_get_cyc(GEN x) { return gel(x,2); }
    2848              : INLINE GEN
    2849      1766676 : abgrp_get_gen(GEN x) { return gel(x,3); }
    2850              : INLINE GEN
    2851      9875388 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
    2852              : INLINE GEN
    2853      5380461 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
    2854              : INLINE GEN
    2855         8386 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
    2856              : INLINE GEN
    2857      3689995 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
    2858              : INLINE GEN
    2859      1681898 : bnf_get_gen(GEN bnf)  { return abgrp_get_gen(bnf_get_clgp(bnf)); }
    2860              : INLINE GEN
    2861          648 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
    2862              : INLINE GEN
    2863      2877897 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
    2864              : INLINE GEN
    2865      1369338 : bnf_get_sunits(GEN bnf)
    2866      1369338 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
    2867              : INLINE GEN
    2868       303613 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
    2869              : INLINE long
    2870       283031 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
    2871              : INLINE GEN
    2872       289112 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
    2873              : INLINE GEN
    2874        26300 : nfV_to_scalar_or_alg(GEN nf, GEN x)
    2875        63442 : { pari_APPLY_same(nf_to_scalar_or_alg(nf, gel(x,i))) }
    2876              : INLINE GEN
    2877        25859 : bnf_get_fu(GEN bnf) {
    2878        25859 :   GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
    2879        25859 :   if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
    2880        25859 :   return nfV_to_scalar_or_alg(nf, vecslice(fu, 2, lg(fu)-1));
    2881              : }
    2882              : 
    2883              : INLINE GEN
    2884      3710982 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
    2885              : INLINE GEN
    2886      2046122 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
    2887              : INLINE GEN
    2888        77126 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
    2889              : INLINE GEN
    2890      1399132 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
    2891              : INLINE GEN
    2892      2830388 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
    2893              : INLINE GEN
    2894       277494 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
    2895              : INLINE GEN
    2896      2522682 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
    2897              : INLINE GEN
    2898           70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
    2899              : INLINE GEN
    2900         7847 : bnr_get_gen(GEN bnr) {
    2901         7847 :   GEN G = bnr_get_clgp(bnr);
    2902         7847 :   if (lg(G) !=  4)
    2903            0 :     pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
    2904         7847 :   return gel(G,3);
    2905              : }
    2906              : 
    2907              : /* localstar, used in gchar */
    2908              : INLINE GEN
    2909        54144 : locs_get_cyc(GEN locs) { return gel(locs,1); }
    2910              : INLINE GEN
    2911       195755 : locs_get_Lsprk(GEN locs) { return gel(locs,2); }
    2912              : INLINE GEN
    2913         1288 : locs_get_Lgenfil(GEN locs) { return gel(locs,3); }
    2914              : INLINE GEN
    2915         8225 : locs_get_mod(GEN locs) { return gel(locs,4); }
    2916              : /* pr dividing the modulus N of locs, 0 <= i < v_pr(N)
    2917              :  * return a t_MAT whose columns are the logs
    2918              :  * of generators of U_i(pr)/U_{i+1}(pr). */
    2919              : INLINE GEN
    2920         1288 : locs_get_famod(GEN locs) { return gmael(locs,4,1); }
    2921              : INLINE GEN
    2922       211534 : locs_get_m_infty(GEN locs) { return gmael(locs,4,2); }
    2923              : 
    2924              : /* G a grossenchar group */
    2925              : INLINE GEN
    2926        34731 : gchar_get_basis(GEN gc)  { return  gel(gc, 1); }
    2927              : INLINE GEN
    2928       207685 : gchar_get_bnf(GEN gc)    { return  gel(gc, 2); }
    2929              : INLINE GEN
    2930       247942 : gchar_get_nf(GEN gc)    { return  gel(gc, 3); }
    2931              : INLINE GEN
    2932       286343 : gchar_get_zm(GEN gc)     { return  gel(gc, 4); }
    2933              : INLINE GEN
    2934         8225 : gchar_get_mod(GEN gc)    { return  locs_get_mod(gchar_get_zm(gc)); }
    2935              : INLINE GEN
    2936         4095 : gchar_get_modP(GEN gc)    { return gmael(gchar_get_mod(gc),1,1); }
    2937              : INLINE GEN
    2938        38371 : gchar_get_S(GEN gc)      { return  gel(gc, 5); }
    2939              : INLINE GEN
    2940       192479 : gchar_get_DLdata(GEN gc)   { return  gel(gc, 6); }
    2941              : INLINE GEN
    2942         1996 : gchar_get_sfu(GEN gc) { return  gel(gc, 7); }
    2943              : INLINE GEN
    2944        16535 : gchar_get_cyc(GEN gc)    { return  gel(gc, 9); }
    2945              : INLINE GEN
    2946            0 : gchar_get_hnf(GEN gc)    { return  gmael(gc, 10, 1); }
    2947              : INLINE GEN
    2948            0 : gchar_get_U(GEN gc)      { return  gmael(gc, 10, 2); }
    2949              : INLINE GEN
    2950        11096 : gchar_get_Ui(GEN gc)     { return  gmael(gc, 10, 3); }
    2951              : INLINE GEN
    2952         3823 : gchar_get_m0(GEN gc)     { return gel(gc, 11); }
    2953              : INLINE GEN
    2954         5002 : gchar_get_u0(GEN gc)     { return gel(gc, 12); }
    2955              : INLINE long
    2956        17683 : gchar_get_r1(GEN gc)     { return nf_get_r1(gchar_get_nf(gc)); }
    2957              : INLINE long
    2958          623 : gchar_get_r2(GEN gc)     { return nf_get_r2(gchar_get_nf(gc)); }
    2959              : INLINE GEN
    2960        52520 : gchar_get_loccyc(GEN gc) { return locs_get_cyc(gchar_get_zm(gc)); }
    2961              : INLINE long
    2962        38876 : gchar_get_nc(GEN gc)     { return lg(gchar_get_loccyc(gc))-1; }
    2963              : INLINE long
    2964        38371 : gchar_get_ns(GEN gc)     { return lg(gchar_get_S(gc))-1; }
    2965              : INLINE long
    2966         2108 : gchar_get_nm(GEN gc)     { return lg(gchar_get_basis(gc))-1; }
    2967              : INLINE long
    2968         4011 : gchar_get_evalprec(GEN gc)   { return  gmael(gc, 8, 1)[1]; }
    2969              : INLINE long
    2970        19427 : gchar_get_prec(GEN gc)   { return  gmael(gc, 8, 1)[2]; }
    2971              : INLINE long
    2972        13854 : gchar_get_nfprec(GEN gc) { return  gmael(gc, 8, 1)[3]; }
    2973              : INLINE void
    2974         2086 : gchar_set_evalprec(GEN gc, long prec) { gmael(gc, 8, 1)[1] = prec; }
    2975              : INLINE void
    2976         1470 : gchar_set_prec(GEN gc, long prec) { gmael(gc, 8, 1)[2] = prec; }
    2977              : INLINE void
    2978         1470 : gchar_copy_precs(GEN gc, GEN gc2)
    2979              : {
    2980         1470 :   gel(gc2, 8) = shallowcopy(gel(gc,8));
    2981         1470 :   gmael(gc2, 8, 1) = shallowcopy(gmael(gc, 8, 1));
    2982         1470 : }
    2983              : INLINE void
    2984         3466 : gchar_set_nfprec(GEN gc, long prec) { gmael(gc, 8, 1)[3] = prec; }
    2985              : INLINE long
    2986          322 : gchar_get_ntors(GEN gc)   { return  gmael(gc, 8, 2)[1]; }
    2987              : INLINE long
    2988          322 : gchar_get_nfree(GEN gc)   { return  gmael(gc, 8, 2)[2]; }
    2989              : INLINE long
    2990         2296 : gchar_get_nalg(GEN gc)   { return  gmael(gc, 8, 2)[3]; }
    2991              : INLINE void
    2992         1827 : gchar_set_basis(GEN gc, GEN m_inv)  { gel(gc, 1) = m_inv; }
    2993              : INLINE void
    2994         2363 : gchar_set_nf(GEN gc, GEN nf)      { gel(gc, 3) = nf; }
    2995              : INLINE void
    2996          637 : gchar_set_ntors(GEN gc, long n)    { gmael(gc, 8, 2)[1] = n; }
    2997              : INLINE void
    2998          637 : gchar_set_nfree(GEN gc, long n)    { gmael(gc, 8, 2)[2] = n; }
    2999              : INLINE void
    3000          637 : gchar_set_nalg(GEN gc, long n)    { gmael(gc, 8, 2)[3] = n; }
    3001              : INLINE void
    3002         1827 : gchar_set_cyc(GEN gc, GEN cyc)      { gel(gc, 9) = cyc; }
    3003              : INLINE void
    3004          637 : gchar_set_HUUi(GEN gc, GEN hnf, GEN U, GEN Ui) { gel(gc, 10) = mkvec3(hnf, U, Ui); }
    3005              : INLINE void
    3006         1190 : gchar_set_m0(GEN gc, GEN m0)     { gel(gc, 11) = m0; }
    3007              : INLINE void
    3008         2142 : gchar_set_u0(GEN gc, GEN u0)     { gel(gc, 12) = u0; }
    3009              : 
    3010              : INLINE GEN
    3011      1521921 : bid_get_mod(GEN bid) { return gel(bid,1); }
    3012              : INLINE GEN
    3013        72849 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
    3014              : INLINE GEN
    3015        41188 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
    3016              : INLINE GEN
    3017      5804874 : bid_get_grp(GEN bid) { return gel(bid,2); }
    3018              : INLINE GEN
    3019      2499065 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
    3020              : INLINE GEN
    3021      2201621 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
    3022              : INLINE GEN
    3023      2157304 : bid_get_MOD(GEN bid) { GEN y = gel(bid,4); return lg(y) == 4? gel(y,3): NULL; }
    3024              : INLINE GEN
    3025      2158039 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
    3026              : INLINE GEN
    3027        48594 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
    3028              : INLINE GEN
    3029      2397285 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
    3030              : INLINE GEN
    3031      3680282 : bid_get_U(GEN bid) { return gel(bid,5); }
    3032              : INLINE GEN
    3033            0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
    3034              : INLINE GEN
    3035      5740019 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
    3036              : INLINE GEN
    3037            0 : bid_get_gen_nocheck(GEN bid)  { return abgrp_get_gen(bid_get_grp(bid)); }
    3038              : INLINE GEN
    3039        64274 : bid_get_gen(GEN bid) {
    3040        64274 :   GEN G = bid_get_grp(bid);
    3041        64274 :   if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
    3042        64274 :   return abgrp_get_gen(G);
    3043              : }
    3044              : 
    3045              : INLINE GEN
    3046     38658755 : znstar_get_N(GEN G) { return gmael(G,1,1); }
    3047              : INLINE GEN
    3048      7334854 : znstar_get_faN(GEN G) { return gel(G,3); }
    3049              : INLINE GEN
    3050           14 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
    3051              : INLINE GEN
    3052       262088 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
    3053              : INLINE GEN
    3054        16150 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
    3055              : INLINE GEN
    3056      7982136 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
    3057              : INLINE GEN
    3058      3290533 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
    3059              : INLINE GEN
    3060        46788 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
    3061              : INLINE GEN
    3062       176107 : znstar_get_U(GEN G) { return gel(G,5); }
    3063              : INLINE GEN
    3064      2869070 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
    3065              : INLINE GEN
    3066        36428 : gal_get_pol(GEN gal) { return gel(gal,1); }
    3067              : INLINE GEN
    3068         6118 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
    3069              : INLINE GEN
    3070           91 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
    3071              : INLINE GEN
    3072        23870 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
    3073              : INLINE GEN
    3074        32844 : gal_get_roots(GEN gal) { return gel(gal,3); }
    3075              : INLINE GEN
    3076        27874 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
    3077              : INLINE GEN
    3078        27867 : gal_get_den(GEN gal) { return gel(gal,5); }
    3079              : INLINE GEN
    3080        80528 : gal_get_group(GEN gal) { return gel(gal,6); }
    3081              : INLINE GEN
    3082        15624 : gal_get_gen(GEN gal) { return gel(gal,7); }
    3083              : INLINE GEN
    3084         6713 : gal_get_orders(GEN gal) { return gel(gal,8); }
    3085              : 
    3086              : /* assume rnf a genuine rnf */
    3087              : INLINE long
    3088      2506335 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
    3089              : INLINE long
    3090        21455 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
    3091              : INLINE long
    3092      1392746 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
    3093              : INLINE GEN
    3094         1386 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
    3095              : INLINE GEN
    3096         1225 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
    3097              : INLINE GEN
    3098         1428 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
    3099              : INLINE GEN
    3100       786086 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
    3101              : INLINE GEN
    3102         7245 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
    3103              : INLINE GEN
    3104       327628 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
    3105              : INLINE GEN
    3106      2845192 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
    3107              : INLINE GEN
    3108          357 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
    3109              : INLINE GEN
    3110          105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
    3111              : INLINE GEN
    3112         2849 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
    3113              : INLINE long
    3114          574 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
    3115              : INLINE GEN
    3116       255354 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
    3117              : INLINE long
    3118         4886 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
    3119              : INLINE GEN
    3120         4830 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
    3121              : INLINE GEN
    3122       135659 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
    3123              : INLINE GEN
    3124         1225 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
    3125              : 
    3126              : INLINE GEN
    3127       139122 : idealred(GEN nf, GEN id) { return idealred0(nf, id, NULL); }
    3128              : 
    3129              : INLINE GEN
    3130         5427 : idealchineseinit(GEN nf, GEN x)
    3131         5427 : { return idealchinese(nf,x,NULL); }
    3132              : 
    3133              : /*******************************************************************/
    3134              : /*                                                                 */
    3135              : /*                              CLOSURES                           */
    3136              : /*                                                                 */
    3137              : /*******************************************************************/
    3138    333230347 : INLINE long closure_arity(GEN C)          { return ((ulong)C[1])&ARITYBITS; }
    3139     46074563 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
    3140    282679033 : INLINE const char *closure_codestr(GEN C)  { return GSTR(gel(C,2))-1; }
    3141           28 : INLINE GEN closure_get_code(GEN C)  { return gel(C,2); }
    3142    282679033 : INLINE GEN closure_get_oper(GEN C)  { return gel(C,3); }
    3143    282665636 : INLINE GEN closure_get_data(GEN C)  { return gel(C,4); }
    3144        13674 : INLINE GEN closure_get_dbg(GEN C)   { return gel(C,5); }
    3145        37178 : INLINE GEN closure_get_text(GEN C)  { return gel(C,6); }
    3146     14756522 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
    3147              : 
    3148              : /*******************************************************************/
    3149              : /*                                                                 */
    3150              : /*                               ERRORS                            */
    3151              : /*                                                                 */
    3152              : /*******************************************************************/
    3153              : INLINE long
    3154       439223 : err_get_num(GEN e) { return e[1]; }
    3155              : INLINE GEN
    3156          294 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
    3157              : 
    3158              : INLINE void
    3159           14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
    3160              : INLINE void
    3161           21 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
    3162              : INLINE void
    3163           91 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
    3164              : INLINE void
    3165          718 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
    3166              : INLINE void
    3167            0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
    3168              : INLINE void
    3169           36 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
    3170              : INLINE void
    3171           98 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
    3172              : INLINE void
    3173          539 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
    3174              : INLINE void
    3175        20032 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
    3176              : INLINE void
    3177           63 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
    3178              : INLINE void
    3179         2876 : 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); }
    3180              : INLINE void
    3181          214 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
    3182              : INLINE void
    3183            0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
    3184              : INLINE void
    3185          406 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
    3186              : INLINE void
    3187          156 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
    3188              : INLINE void
    3189          238 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
    3190              : INLINE void
    3191            0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
    3192              : INLINE void
    3193           98 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
    3194              : INLINE void
    3195         1316 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
    3196              : INLINE void
    3197           70 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
    3198              : INLINE void
    3199         7084 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
    3200              : INLINE void
    3201        15370 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
    3202              : INLINE void
    3203         3493 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
    3204              : INLINE void
    3205          371 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
    3206              : INLINE void
    3207          245 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
    3208          245 : { pari_err(e_PRIORITY, f,x,op,v); }
    3209              : 
        

Generated by: LCOV version 2.0-1