Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - headers - pariinl.h (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 19825-b77c7f8) Lines: 1206 1365 88.4 %
Date: 2016-12-05 05:49:04 Functions: 511 579 88.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2010  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                          CONSTRUCTORS                           */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #define retmkfrac(x,y)\
      20             :   do { GEN _v = cgetg(3, t_FRAC);\
      21             :        gel(_v,1) = (x);\
      22             :        gel(_v,2) = (y); return _v; } while(0)
      23             : #define retmkrfrac(x,y)\
      24             :   do { GEN _v = cgetg(3, t_RFRAC);\
      25             :        gel(_v,1) = (x);\
      26             :        gel(_v,2) = (y); return _v; } while(0)
      27             : #define retmkintmod(x,y)\
      28             :   do { GEN _v = cgetg(3, t_INTMOD);\
      29             :        gel(_v,1) = (y);\
      30             :        gel(_v,2) = (x); return _v; } while(0)
      31             : #define retmkcomplex(x,y)\
      32             :   do { GEN _v = cgetg(3, t_COMPLEX);\
      33             :        gel(_v,1) = (x);\
      34             :        gel(_v,2) = (y); return _v; } while(0)
      35             : #define retmkpolmod(x,y)\
      36             :   do { GEN _v = cgetg(3, t_POLMOD);\
      37             :        gel(_v,1) = (y);\
      38             :        gel(_v,2) = (x); return _v; } while(0)
      39             : #define retmkvec(x)\
      40             :   do { GEN _v = cgetg(2, t_VEC);\
      41             :        gel(_v,1) = (x); return _v; } while(0)
      42             : #define retmkvec2(x,y)\
      43             :   do { GEN _v = cgetg(3, t_VEC);\
      44             :        gel(_v,1) = (x);\
      45             :        gel(_v,2) = (y); return _v; } while(0)
      46             : #define retmkvec3(x,y,z)\
      47             :   do { GEN _v = cgetg(4, t_VEC);\
      48             :        gel(_v,1) = (x);\
      49             :        gel(_v,2) = (y);\
      50             :        gel(_v,3) = (z); return _v; } while(0)
      51             : #define retmkqfi(x,y,z)\
      52             :   do { GEN _v = cgetg(4, t_QFI);\
      53             :        gel(_v,1) = (x);\
      54             :        gel(_v,2) = (y);\
      55             :        gel(_v,3) = (z); return _v; } while(0)
      56             : #define retmkqfr(x,y,z,d)\
      57             :   do { GEN _v = cgetg(5, t_QFR);\
      58             :        gel(_v,1) = (x);\
      59             :        gel(_v,2) = (y);\
      60             :        gel(_v,3) = (z);\
      61             :        gel(_v,4) = (d); return _v; } while(0)
      62             : #define retmkquad(x,y,z)\
      63             :   do { GEN _v = cgetg(4, t_QUAD);\
      64             :        gel(_v,1) = (x);\
      65             :        gel(_v,2) = (y);\
      66             :        gel(_v,3) = (z); return _v; } while(0)
      67             : #define retmkvec4(x,y,z,t)\
      68             :   do { GEN _v = cgetg(5, t_VEC);\
      69             :        gel(_v,1) = (x);\
      70             :        gel(_v,2) = (y);\
      71             :        gel(_v,3) = (z);\
      72             :        gel(_v,4) = (t); return _v; } while(0)
      73             : #define retmkvec5(x,y,z,t,u)\
      74             :   do { GEN _v = cgetg(6, t_VEC);\
      75             :        gel(_v,1) = (x);\
      76             :        gel(_v,2) = (y);\
      77             :        gel(_v,3) = (z);\
      78             :        gel(_v,4) = (t);\
      79             :        gel(_v,5) = (u); return _v; } while(0)
      80             : #define retmkcol(x)\
      81             :   do { GEN _v = cgetg(2, t_COL);\
      82             :        gel(_v,1) = (x); return _v; } while(0)
      83             : #define retmkcol2(x,y)\
      84             :   do { GEN _v = cgetg(3, t_COL);\
      85             :        gel(_v,1) = (x);\
      86             :        gel(_v,2) = (y); return _v; } while(0)
      87             : #define retmkcol3(x,y,z)\
      88             :   do { GEN _v = cgetg(4, t_COL);\
      89             :        gel(_v,1) = (x);\
      90             :        gel(_v,2) = (y);\
      91             :        gel(_v,3) = (z); return _v; } while(0)
      92             : #define retmkcol4(x,y,z,t)\
      93             :   do { GEN _v = cgetg(5, t_COL);\
      94             :        gel(_v,1) = (x);\
      95             :        gel(_v,2) = (y);\
      96             :        gel(_v,3) = (z);\
      97             :        gel(_v,4) = (t); return _v; } while(0)
      98             : #define retmkcol5(x,y,z,t,u)\
      99             :   do { GEN _v = cgetg(6, t_COL);\
     100             :        gel(_v,1) = (x);\
     101             :        gel(_v,2) = (y);\
     102             :        gel(_v,3) = (z);\
     103             :        gel(_v,4) = (t);\
     104             :        gel(_v,5) = (u); return _v; } while(0)
     105             : #define retmkcol6(x,y,z,t,u,v)\
     106             :   do { GEN _v = cgetg(7, t_COL);\
     107             :        gel(_v,1) = (x);\
     108             :        gel(_v,2) = (y);\
     109             :        gel(_v,3) = (z);\
     110             :        gel(_v,4) = (t);\
     111             :        gel(_v,5) = (u);\
     112             :        gel(_v,6) = (v); return _v; } while(0)
     113             : #define retmkmat(x)\
     114             :   do { GEN _v = cgetg(2, t_MAT);\
     115             :        gel(_v,1) = (x); return _v; } while(0)
     116             : #define retmkmat2(x,y)\
     117             :   do { GEN _v = cgetg(3, t_MAT);\
     118             :        gel(_v,1) = (x);\
     119             :        gel(_v,2) = (y); return _v; } while(0)
     120             : #define retmkmat3(x,y,z)\
     121             :   do { GEN _v = cgetg(4, t_MAT);\
     122             :        gel(_v,1) = (x);\
     123             :        gel(_v,2) = (y);\
     124             :        gel(_v,3) = (z); return _v; } while(0)
     125             : #define retmkmat4(x,y,z,t)\
     126             :   do { GEN _v = cgetg(5, t_MAT);\
     127             :        gel(_v,1) = (x);\
     128             :        gel(_v,2) = (y);\
     129             :        gel(_v,3) = (z);\
     130             :        gel(_v,4) = (t); return _v; } while(0)
     131             : #define retmkmat5(x,y,z,t,u)\
     132             :   do { GEN _v = cgetg(6, t_MAT);\
     133             :        gel(_v,1) = (x);\
     134             :        gel(_v,2) = (y);\
     135             :        gel(_v,3) = (z);\
     136             :        gel(_v,4) = (t);\
     137             :        gel(_v,5) = (u); return _v; } while(0)
     138             : 
     139             : INLINE GEN
     140      323186 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
     141             : INLINE GEN
     142        3780 : mkintmodu(ulong x, ulong y) {
     143        3780 :   GEN v = cgetg(3,t_INTMOD);
     144        3780 :   gel(v,1) = utoipos(y);
     145        3780 :   gel(v,2) = utoi(x); return v;
     146             : }
     147             : INLINE GEN
     148      248112 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
     149             : INLINE GEN
     150     1758364 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
     151             : INLINE GEN
     152     2438493 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
     153             : INLINE GEN
     154        5019 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
     155        5019 :   gel(v,1) = x; gel(v,2) = y; return v; }
     156             : INLINE GEN
     157          14 : mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
     158          14 :   gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
     159             : INLINE GEN
     160     2927066 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
     161             : INLINE GEN
     162      425681 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
     163             : INLINE GEN
     164      200662 : cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
     165             : INLINE GEN
     166          42 : mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
     167          42 :   gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
     168             : /* vecsmall */
     169             : INLINE GEN
     170    49461573 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
     171             : INLINE GEN
     172   114451927 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
     173   114452234 :   v[1]=x; v[2]=y; return v; }
     174             : INLINE GEN
     175    63926243 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
     176    63923988 :   v[1]=x; v[2]=y; v[3]=z; return v; }
     177             : INLINE GEN
     178     9275204 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
     179     9269979 :   v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
     180             : INLINE GEN
     181      610865 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
     182      610865 :   v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
     183             : 
     184             : INLINE GEN
     185      158123 : mkqfi(GEN x, GEN y, GEN z) { retmkqfi(x,y,z); }
     186             : /* vec */
     187             : INLINE GEN
     188     3220121 : mkvec(GEN x) { retmkvec(x); }
     189             : INLINE GEN
     190    22592272 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
     191             : INLINE GEN
     192    52958535 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
     193             : INLINE GEN
     194     7327533 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
     195             : INLINE GEN
     196    11734098 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
     197             : INLINE GEN
     198           7 : mkvecs(long x) { retmkvec(stoi(x)); }
     199             : INLINE GEN
     200       11858 : mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
     201             : INLINE GEN
     202      492702 : mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
     203             : INLINE GEN
     204           0 : mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
     205             : INLINE GEN
     206       33264 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
     207             : INLINE GEN
     208      195346 : mkvec2copy(GEN x, GEN y) {
     209      195346 :   GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
     210             : /* col */
     211             : INLINE GEN
     212     2039355 : mkcol(GEN x) { retmkcol(x); }
     213             : INLINE GEN
     214    14735500 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
     215             : INLINE GEN
     216       41615 : mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
     217             : INLINE GEN
     218           0 : mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
     219             : INLINE GEN
     220           0 : mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
     221             : INLINE GEN
     222         729 : mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
     223             : INLINE GEN
     224         721 : mkcols(long x) { retmkcol(stoi(x)); }
     225             : INLINE GEN
     226     1181376 : mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
     227             : INLINE GEN
     228           0 : mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
     229             : INLINE GEN
     230           0 : mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
     231             : INLINE GEN
     232       26049 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
     233             : /* mat */
     234             : INLINE GEN
     235      659771 : mkmat(GEN x) { retmkmat(x); }
     236             : INLINE GEN
     237    15683097 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
     238             : INLINE GEN
     239        2352 : mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
     240             : INLINE GEN
     241           0 : mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
     242             : INLINE GEN
     243           0 : mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
     244             : INLINE GEN
     245        7110 : mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
     246             : INLINE GEN
     247           0 : mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
     248             : INLINE GEN
     249       12905 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
     250             : INLINE GEN
     251        7489 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
     252             : INLINE long
     253       27132 : inf_get_sign(GEN x) { return signe(gel(x,1)); }
     254             : /* pol */
     255             : INLINE GEN
     256      684956 : pol_x(long v) {
     257      684956 :   GEN p = cgetg(4, t_POL);
     258      684956 :   p[1] = evalsigne(1)|evalvarn(v);
     259      684956 :   gel(p,2) = gen_0;
     260      684956 :   gel(p,3) = gen_1; return p;
     261             : }
     262             : /* x^n, assume n >= 0 */
     263             : INLINE GEN
     264     2306964 : pol_xn(long n, long v) {
     265     2306964 :   long i, a = n+2;
     266     2306964 :   GEN p = cgetg(a+1, t_POL);
     267     2306964 :   p[1] = evalsigne(1)|evalvarn(v);
     268     2306964 :   for (i = 2; i < a; i++) gel(p,i) = gen_0;
     269     2306964 :   gel(p,a) = gen_1; return p;
     270             : }
     271             : /* x^n, no assumption on n */
     272             : INLINE GEN
     273          98 : pol_xnall(long n, long v)
     274             : {
     275          98 :   if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
     276          42 :   return pol_xn(n, v);
     277             : }
     278             : INLINE GEN
     279     1184879 : pol_1(long v) {
     280     1184879 :   GEN p = cgetg(3, t_POL);
     281     1184879 :   p[1] = evalsigne(1)|evalvarn(v);
     282     1184879 :   gel(p,2) = gen_1; return p;
     283             : }
     284             : INLINE GEN
     285    12775116 : pol_0(long v)
     286             : {
     287    12775116 :   GEN x = cgetg(2,t_POL);
     288    12775116 :   x[1] = evalvarn(v); return x;
     289             : }
     290             : #define retconst_vec(n,x)\
     291             :   do { long _i, _n = (n);\
     292             :        GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
     293             :        for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
     294             :        return _v; } while(0)
     295             : INLINE GEN
     296     9258339 : const_vec(long n, GEN x) { retconst_vec(n, x); }
     297             : #define retconst_col(n,x)\
     298             :   do { long _i, _n = (n);\
     299             :        GEN _v = cgetg(_n+1, t_COL), _x = (x);\
     300             :        for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
     301             :        return _v; } while(0)
     302             : INLINE GEN
     303       30757 : const_col(long n, GEN x) { retconst_col(n, x); }
     304             : INLINE GEN
     305     5031350 : const_vecsmall(long n, long c)
     306             : {
     307             :   long i;
     308     5031350 :   GEN V = cgetg(n+1,t_VECSMALL);
     309     5031350 :   for(i=1;i<=n;i++) V[i] = c;
     310     5031350 :   return V;
     311             : }
     312             : 
     313             : /***   ZERO   ***/
     314             : /* O(p^e) */
     315             : INLINE GEN
     316      300881 : zeropadic(GEN p, long e)
     317             : {
     318      300881 :   GEN y = cgetg(5,t_PADIC);
     319      300881 :   gel(y,4) = gen_0;
     320      300881 :   gel(y,3) = gen_1;
     321      300881 :   gel(y,2) = icopy(p);
     322      300881 :   y[1] = evalvalp(e) | _evalprecp(0);
     323      300881 :   return y;
     324             : }
     325             : INLINE GEN
     326         483 : zeropadic_shallow(GEN p, long e)
     327             : {
     328         483 :   GEN y = cgetg(5,t_PADIC);
     329         483 :   gel(y,4) = gen_0;
     330         483 :   gel(y,3) = gen_1;
     331         483 :   gel(y,2) = p;
     332         483 :   y[1] = evalvalp(e) | _evalprecp(0);
     333         483 :   return y;
     334             : }
     335             : /* O(pol_x(v)^e) */
     336             : INLINE GEN
     337       12495 : zeroser(long v, long e)
     338             : {
     339       12495 :   GEN x = cgetg(2, t_SER);
     340       12495 :   x[1] = evalvalp(e) | evalvarn(v); return x;
     341             : }
     342             : INLINE int
     343     3848029 : ser_isexactzero(GEN x)
     344             : {
     345     3848029 :   if (!signe(x)) switch(lg(x))
     346             :   {
     347       10976 :     case 2: return 1;
     348         252 :     case 3: return isexactzero(gel(x,2));
     349             :   }
     350     3836801 :   return 0;
     351             : }
     352             : /* 0 * pol_x(v) */
     353             : INLINE GEN
     354     4167131 : zeropol(long v) { return pol_0(v); }
     355             : /* vector(n) */
     356             : INLINE GEN
     357    22680688 : zerocol(long n)
     358             : {
     359    22680688 :   GEN y = cgetg(n+1,t_COL);
     360    22680688 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     361    22680688 :   return y;
     362             : }
     363             : /* vectorv(n) */
     364             : INLINE GEN
     365     7757278 : zerovec(long n)
     366             : {
     367     7757278 :   GEN y = cgetg(n+1,t_VEC);
     368     7757277 :   long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
     369     7757277 :   return y;
     370             : }
     371             : /* matrix(m, n) */
     372             : INLINE GEN
     373        4291 : zeromat(long m, long n)
     374             : {
     375        4291 :   GEN y = cgetg(n+1,t_MAT);
     376        4291 :   GEN v = zerocol(m);
     377        4291 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     378        4291 :   return y;
     379             : }
     380             : /* = zero_zx, sv is a evalvarn()*/
     381             : INLINE GEN
     382      759953 : zero_Flx(long sv) { return pol0_Flx(sv); }
     383             : INLINE GEN
     384    29672025 : zero_Flv(long n)
     385             : {
     386    29672025 :   GEN y = cgetg(n+1,t_VECSMALL);
     387    29672209 :   long i; for (i=1; i<=n; i++) y[i] = 0;
     388    29672209 :   return y;
     389             : }
     390             : /* matrix(m, n) */
     391             : INLINE GEN
     392         287 : zero_Flm(long m, long n)
     393             : {
     394         287 :   GEN y = cgetg(n+1,t_MAT);
     395         287 :   GEN v = zero_Flv(m);
     396         287 :   long i; for (i=1; i<=n; i++) gel(y,i) = v;
     397         287 :   return y;
     398             : }
     399             : /* matrix(m, n) */
     400             : INLINE GEN
     401      103457 : zero_Flm_copy(long m, long n)
     402             : {
     403      103457 :   GEN y = cgetg(n+1,t_MAT);
     404      103470 :   long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
     405      103463 :   return y;
     406             : }
     407             : 
     408             : INLINE GEN
     409      715634 : zero_F2v(long m)
     410             : {
     411      715634 :   long l = nbits2nlong(m);
     412      715634 :   GEN v  = zero_Flv(l+1);
     413      715634 :   v[1] = m;
     414      715634 :   return v;
     415             : }
     416             : 
     417             : INLINE GEN
     418           0 : zero_F2m(long m, long n)
     419             : {
     420             :   long i;
     421           0 :   GEN M = cgetg(n+1, t_MAT);
     422           0 :   GEN v = zero_F2v(m);
     423           0 :   for (i = 1; i <= n; i++)
     424           0 :     gel(M,i) = v;
     425           0 :   return M;
     426             : }
     427             : 
     428             : 
     429             : INLINE GEN
     430      269609 : zero_F2m_copy(long m, long n)
     431             : {
     432             :   long i;
     433      269609 :   GEN M = cgetg(n+1, t_MAT);
     434      761526 :   for (i = 1; i <= n; i++)
     435      491917 :     gel(M,i)= zero_F2v(m);
     436      269609 :   return M;
     437             : }
     438             : 
     439             : /* matrix(m, n) */
     440             : INLINE GEN
     441     1956053 : zeromatcopy(long m, long n)
     442             : {
     443     1956053 :   GEN y = cgetg(n+1,t_MAT);
     444     1956053 :   long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
     445     1956053 :   return y;
     446             : }
     447             : 
     448             : /* i-th vector in the standard basis */
     449             : INLINE GEN
     450     1202289 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
     451             : INLINE GEN
     452      303997 : vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
     453             : INLINE GEN
     454           7 : F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
     455             : INLINE GEN
     456        3302 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
     457             : INLINE GEN
     458     2827978 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
     459             : 
     460             : INLINE GEN
     461     2258313 : shallowcopy(GEN x)
     462     2258313 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
     463             : 
     464             : /* routines for naive growarrays */
     465             : INLINE GEN
     466    11510560 : vectrunc_init(long l)
     467             : {
     468    11510560 :   GEN z = new_chunk(l);
     469    11510896 :   z[0] = evaltyp(t_VEC) | _evallg(1); return z;
     470             : }
     471             : INLINE void
     472    42841631 : vectrunc_append(GEN x, GEN t)
     473    42841631 : { long l = lg(x); gel(x,l) = t; setlg(x, l+1); }
     474             : INLINE void
     475           0 : vectrunc_append_batch(GEN x, GEN y)
     476             : {
     477           0 :   long i, l = lg(x), ly = lg(y);
     478           0 :   GEN z = x + l-1;
     479           0 :   for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
     480           0 :   setlg(x, l+ly-1);
     481           0 : }
     482             : INLINE GEN
     483     1355838 : vecsmalltrunc_init(long l)
     484             : {
     485     1355838 :   GEN z = new_chunk(l);
     486     1355838 :   z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
     487             : }
     488             : INLINE void
     489     2754411 : vecsmalltrunc_append(GEN x, long t)
     490     2754411 : { long l = lg(x); x[l] = t; setlg(x, l+1); }
     491             : 
     492             : /*******************************************************************/
     493             : /*                                                                 */
     494             : /*                        VEC / COL / VECSMALL                     */
     495             : /*                                                                 */
     496             : /*******************************************************************/
     497             : /* shallow*/
     498             : INLINE GEN
     499           0 : vec_shorten(GEN v, long n)
     500             : {
     501             :   long i;
     502           0 :   GEN V = cgetg(n+1,t_VEC);
     503           0 :   for(i=1;i<=n;i++) gel(V,i) = gel(v,i);
     504           0 :   return V;
     505             : }
     506             : /* shallow*/
     507             : INLINE GEN
     508       10577 : vec_lengthen(GEN v, long n)
     509             : {
     510             :   long i;
     511       10577 :   long l=lg(v);
     512       10577 :   GEN V = cgetg(n+1,t_VEC);
     513       10577 :   for(i=1;i<l;i++) gel(V,i) = gel(v,i);
     514       10577 :   return V;
     515             : }
     516             : /* shallow*/
     517             : INLINE GEN
     518       56042 : vec_append(GEN V, GEN s)
     519             : {
     520       56042 :   long i, l2 = lg(V);
     521       56042 :   GEN res = cgetg(l2+1, typ(V));
     522       56042 :   for (i = 1; i < l2; ++i) gel(res, i) = gel(V,i);
     523       56042 :   gel(res,l2) = s; return res;
     524             : }
     525             : /* shallow*/
     526             : INLINE GEN
     527           0 : vec_setconst(GEN v, GEN x)
     528             : {
     529           0 :   long i, l = lg(v);
     530           0 :   for (i = 1; i < l; i++) gel(v,i) = x;
     531           0 :   return v;
     532             : }
     533             : INLINE GEN
     534      127540 : vecsmall_shorten(GEN v, long n)
     535             : {
     536             :   long i;
     537      127540 :   GEN V = cgetg(n+1,t_VECSMALL);
     538      127540 :   for(i=1;i<=n;i++) V[i] = v[i];
     539      127540 :   return V;
     540             : }
     541             : INLINE GEN
     542          21 : vecsmall_lengthen(GEN v, long n)
     543             : {
     544          21 :   long i, l = lg(v);
     545          21 :   GEN V = cgetg(n+1,t_VECSMALL);
     546          21 :   for(i=1;i<l;i++) V[i] = v[i];
     547          21 :   return V;
     548             : }
     549             : 
     550             : INLINE GEN
     551      236129 : vec_to_vecsmall(GEN z)
     552             : {
     553      236129 :   long i, l = lg(z);
     554      236129 :   GEN x = cgetg(l, t_VECSMALL);
     555      236129 :   for (i=1; i<l; i++) x[i] = itos(gel(z,i));
     556      236129 :   return x;
     557             : }
     558             : INLINE GEN
     559       15603 : vecsmall_to_vec(GEN z)
     560             : {
     561       15603 :   long i, l = lg(z);
     562       15603 :   GEN x = cgetg(l,t_VEC);
     563       15603 :   for (i=1; i<l; i++) gel(x,i) = stoi(z[i]);
     564       15603 :   return x;
     565             : }
     566             : INLINE GEN
     567     1243090 : vecsmall_to_col(GEN z)
     568             : {
     569     1243090 :   long i, l = lg(z);
     570     1243090 :   GEN x = cgetg(l,t_COL);
     571     1243090 :   for (i=1; i<l; i++) gel(x,i) = stoi(z[i]);
     572     1243090 :   return x;
     573             : }
     574             : 
     575             : INLINE int
     576     2763591 : vecsmall_lexcmp(GEN x, GEN y)
     577             : {
     578             :   long lx,ly,l,i;
     579     2763591 :   lx = lg(x);
     580     2763591 :   ly = lg(y); l = minss(lx,ly);
     581    15197765 :   for (i=1; i<l; i++)
     582    14512950 :     if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
     583      684815 :   if (lx == ly) return 0;
     584           0 :   return (lx < ly)? -1 : 1;
     585             : }
     586             : 
     587             : INLINE int
     588    47066978 : vecsmall_prefixcmp(GEN x, GEN y)
     589             : {
     590    47066978 :   long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
     591   148811348 :   for (i=1; i<l; i++)
     592   134951817 :     if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
     593    13859531 :   return 0;
     594             : }
     595             : 
     596             : /*Can be used on t_VEC, but coeffs not gcopy-ed*/
     597             : INLINE GEN
     598        1232 : vecsmall_prepend(GEN V, long s)
     599             : {
     600        1232 :   long i, l2 = lg(V);
     601        1232 :   GEN res = cgetg(l2+1, typ(V));
     602        1232 :   res[1] = s;
     603        1232 :   for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
     604        1232 :   return res;
     605             : }
     606             : 
     607             : INLINE GEN
     608      364169 : vecsmall_append(GEN V, long s)
     609             : {
     610      364169 :   long i, l2 = lg(V);
     611      364169 :   GEN res = cgetg(l2+1, t_VECSMALL);
     612      364169 :   for (i = 1; i < l2; ++i) res[i] = V[i];
     613      364169 :   res[l2] = s; return res;
     614             : }
     615             : 
     616             : INLINE GEN
     617      242973 : vecsmall_concat(GEN u, GEN v)
     618             : {
     619      242973 :   long i, l1 = lg(u)-1, l2 = lg(v)-1;
     620      242973 :   GEN res = cgetg(l1+l2+1, t_VECSMALL);
     621      242973 :   for (i = 1; i <= l1; ++i) res[i]    = u[i];
     622      242973 :   for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
     623      242973 :   return res;
     624             : }
     625             : 
     626             : /* return the number of indices where u and v are equal */
     627             : INLINE long
     628           0 : vecsmall_coincidence(GEN u, GEN v)
     629             : {
     630           0 :   long i, s = 0, l = minss(lg(u),lg(v));
     631           0 :   for(i=1; i<l; i++)
     632           0 :     if(u[i] == v[i]) s++;
     633           0 :   return s;
     634             : }
     635             : 
     636             : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
     637             : INLINE long
     638           0 : vecsmall_isin(GEN v, long x)
     639             : {
     640           0 :   long i, l = lg(v);
     641           0 :   for (i = 1; i < l; i++)
     642           0 :     if (v[i] == x) return i;
     643           0 :   return 0;
     644             : }
     645             : 
     646             : INLINE long
     647          84 : vecsmall_pack(GEN V, long base, long mod)
     648             : {
     649          84 :   long i, s = 0;
     650          84 :   for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
     651          84 :   return s;
     652             : }
     653             : 
     654             : INLINE long
     655          14 : vecsmall_indexmax(GEN x)
     656             : {
     657          14 :   long i, i0 = 1, t = x[1], lx = lg(x);
     658          56 :   for (i=2; i<lx; i++)
     659          42 :     if (x[i] > t) t = x[i0=i];
     660          14 :   return i0;
     661             : }
     662             : 
     663             : INLINE long
     664       35970 : vecsmall_max(GEN x)
     665             : {
     666       35970 :   long i, t = x[1], lx = lg(x);
     667       61601 :   for (i=2; i<lx; i++)
     668       25631 :     if (x[i] > t) t = x[i];
     669       35970 :   return t;
     670             : }
     671             : 
     672             : INLINE long
     673          14 : vecsmall_indexmin(GEN x)
     674             : {
     675          14 :   long i, i0 = 1, t = x[1], lx =lg(x);
     676          56 :   for (i=2; i<lx; i++)
     677          42 :     if (x[i] < t) t = x[i0=i];
     678          14 :   return i0;
     679             : }
     680             : 
     681             : INLINE long
     682         133 : vecsmall_min(GEN x)
     683             : {
     684         133 :   long i, t = x[1], lx =lg(x);
     685         959 :   for (i=2; i<lx; i++)
     686         826 :     if (x[i] < t) t = x[i];
     687         133 :   return t;
     688             : }
     689             : 
     690             : INLINE int
     691     2018478 : ZV_isscalar(GEN x)
     692             : {
     693     2018478 :   long l = lg(x);
     694     7475645 :   while (--l > 1)
     695     5089658 :     if (signe(gel(x, l))) return 0;
     696      367509 :   return 1;
     697             : }
     698             : INLINE int
     699     6105929 : QV_isscalar(GEN x)
     700             : {
     701     6105929 :   long lx = lg(x),i;
     702     7028755 :   for (i=2; i<lx; i++)
     703     6527500 :     if (!isintzero(gel(x, i))) return 0;
     704      501255 :   return 1;
     705             : }
     706             : INLINE int
     707        2884 : RgV_isscalar(GEN x)
     708             : {
     709        2884 :   long lx = lg(x),i;
     710        6161 :   for (i=2; i<lx; i++)
     711        4977 :     if (!gequal0(gel(x, i))) return 0;
     712        1184 :   return 1;
     713             : }
     714             : INLINE int
     715           0 : RgX_isscalar(GEN x)
     716             : {
     717             :   long i;
     718           0 :   for (i=lg(x)-1; i>2; i--)
     719           0 :     if (!gequal0(gel(x, i))) return 0;
     720           0 :   return 1;
     721             : }
     722             : INLINE long
     723     3787921 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
     724             : 
     725             : INLINE int
     726        2374 : RgX_is_rational(GEN x)
     727             : {
     728             :   long i;
     729       16142 :   for (i = lg(x)-1; i > 1; i--)
     730       14965 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     731        1177 :   return 1;
     732             : }
     733             : INLINE int
     734    54688363 : RgX_is_ZX(GEN x)
     735             : {
     736             :   long i;
     737   242782603 :   for (i = lg(x)-1; i > 1; i--)
     738   193312669 :     if (typ(gel(x,i)) != t_INT) return 0;
     739    49469934 :   return 1;
     740             : }
     741             : INLINE int
     742      161941 : RgX_is_QX(GEN x)
     743             : {
     744      161941 :   long k = lg(x)-1;
     745      635215 :   for ( ; k>1; k--)
     746      474128 :     if (!is_rational_t(typ(gel(x,k)))) return 0;
     747      161087 :   return 1;
     748             : }
     749             : INLINE int
     750    15106491 : RgX_is_monomial(GEN x)
     751             : {
     752             :   long i;
     753    15106491 :   if (!signe(x)) return 0;
     754    24287714 :   for (i=lg(x)-2; i>1; i--)
     755    16784964 :     if (!isexactzero(gel(x,i))) return 0;
     756     7502750 :   return 1;
     757             : }
     758             : INLINE int
     759     2434665 : RgV_is_ZV(GEN x)
     760             : {
     761             :   long i;
     762     8641669 :   for (i = lg(x)-1; i > 0; i--)
     763     6207690 :     if (typ(gel(x,i)) != t_INT) return 0;
     764     2433979 :   return 1;
     765             : }
     766             : INLINE int
     767         147 : RgV_is_QV(GEN x)
     768             : {
     769             :   long i;
     770         413 :   for (i = lg(x)-1; i > 0; i--)
     771         280 :     if (!is_rational_t(typ(gel(x,i)))) return 0;
     772         133 :   return 1;
     773             : }
     774             : 
     775             : /********************************************************************/
     776             : /**                                                                **/
     777             : /**            Dynamic arrays implementation                       **/
     778             : /**                                                                **/
     779             : /********************************************************************/
     780             : INLINE void **
     781   329713655 : pari_stack_base(pari_stack *s) { return s->data; }
     782             : 
     783             : INLINE void
     784      898021 : pari_stack_init(pari_stack *s, size_t size, void **data)
     785             : {
     786      898021 :   s->data = data;
     787      898021 :   *data = NULL;
     788      898021 :   s->n = 0;
     789      898021 :   s->alloc = 0;
     790      898021 :   s->size = size;
     791      898021 : }
     792             : 
     793             : INLINE void
     794   328849421 : pari_stack_alloc(pari_stack *s, long nb)
     795             : {
     796   328849421 :   void **sdat = pari_stack_base(s);
     797   329002636 :   long alloc = s->alloc;
     798   658006061 :   if (s->n+nb <= alloc) return;
     799      306668 :   if (!alloc)
     800      261633 :     alloc = nb;
     801             :   else
     802             :   {
     803       45035 :     while (s->n+nb > alloc) alloc <<= 1;
     804             :   }
     805      306668 :   *sdat = pari_realloc(*sdat,alloc*s->size);
     806      307457 :   s->alloc = alloc;
     807             : }
     808             : 
     809             : INLINE long
     810   275562631 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
     811             : 
     812             : INLINE void
     813      777242 : pari_stack_delete(pari_stack *s)
     814             : {
     815      777242 :   void **sdat = pari_stack_base(s);
     816      777005 :   if (*sdat) pari_free(*sdat);
     817      777181 : }
     818             : 
     819             : INLINE void
     820        5544 : pari_stack_pushp(pari_stack *s, void *u)
     821             : {
     822        5544 :   long n = pari_stack_new(s);
     823        5544 :   void **sdat =(void**) *pari_stack_base(s);
     824        5544 :   sdat[n] = u;
     825        5544 : }
     826             : 
     827             : /*******************************************************************/
     828             : /*                                                                 */
     829             : /*                            EXTRACT                              */
     830             : /*                                                                 */
     831             : /*******************************************************************/
     832             : INLINE GEN
     833     3715507 : vecslice(GEN A, long y1, long y2)
     834             : {
     835     3715507 :   long i,lB = y2 - y1 + 2;
     836     3715507 :   GEN B = cgetg(lB, typ(A));
     837     3715507 :   for (i=1; i<lB; i++) B[i] = A[y1-1+i];
     838     3715507 :   return B;
     839             : }
     840             : INLINE GEN
     841      140600 : vecslicepermute(GEN A, GEN p, long y1, long y2)
     842             : {
     843      140600 :   long i,lB = y2 - y1 + 2;
     844      140600 :   GEN B = cgetg(lB, typ(A));
     845      140600 :   for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
     846      140600 :   return B;
     847             : }
     848             : /* rowslice(rowpermute(A,p), x1, x2) */
     849             : INLINE GEN
     850       35196 : rowslicepermute(GEN A, GEN p, long x1, long x2)
     851             : {
     852       35196 :   long i, lB = lg(A);
     853       35196 :   GEN B = cgetg(lB, typ(A));
     854       35196 :   for (i=1; i<lB; i++) gel(B,i) = vecslicepermute(gel(A,i),p,x1,x2);
     855       35196 :   return B;
     856             : }
     857             : INLINE GEN
     858      211601 : rowslice(GEN A, long x1, long x2)
     859             : {
     860      211601 :   long i, lB = lg(A);
     861      211601 :   GEN B = cgetg(lB, typ(A));
     862      211601 :   for (i=1; i<lB; i++) gel(B,i) = vecslice(gel(A,i),x1,x2);
     863      211601 :   return B;
     864             : }
     865             : 
     866             : INLINE GEN
     867      114359 : matslice(GEN A, long x1, long x2, long y1, long y2)
     868             : {
     869      114359 :   return rowslice(vecslice(A, y1, y2), x1, x2);
     870             : }
     871             : 
     872             : /* shallow, remove coeff of index j */
     873             : INLINE GEN
     874         133 : rowsplice(GEN a, long j)
     875             : {
     876             :   long i, l;
     877         133 :   GEN b = cgetg_copy(a,&l);
     878         133 :   for (i = 1; i < l; i++) gel(b,i) = vecsplice(gel(a,i), j);
     879         133 :   return b;
     880             : }
     881             : /* shallow, remove coeff of index j */
     882             : INLINE GEN
     883        8274 : vecsplice(GEN a, long j)
     884             : {
     885        8274 :   long i, k, l = lg(a);
     886             :   GEN b;
     887        8274 :   if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
     888        8274 :   b = cgetg(l-1, typ(a));
     889       45920 :   for (i = k = 1; i < l; i++)
     890       37646 :     if (i != j) gel(b, k++) = gel(a,i);
     891        8274 :   return b;
     892             : }
     893             : /* shallow */
     894             : INLINE GEN
     895         756 : RgM_minor(GEN a, long i, long j)
     896             : {
     897         756 :   GEN b = vecsplice(a, j);
     898         756 :   long k, l = lg(b);
     899         756 :   for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
     900         756 :   return b;
     901             : }
     902             : 
     903             : /* A[x0,] */
     904             : INLINE GEN
     905      115790 : row(GEN A, long x0)
     906             : {
     907      115790 :   long i, lB = lg(A);
     908      115790 :   GEN B  = cgetg(lB, t_VEC);
     909      115790 :   for (i=1; i<lB; i++) gel(B, i) = gcoeff(A, x0, i);
     910      115790 :   return B;
     911             : }
     912             : INLINE GEN
     913       25475 : Flm_row(GEN A, long x0)
     914             : {
     915       25475 :   long i, lB = lg(A);
     916       25475 :   GEN B  = cgetg(lB, t_VECSMALL);
     917       25476 :   for (i=1; i<lB; i++) B[i] = coeff(A, x0, i);
     918       25476 :   return B;
     919             : }
     920             : /* A[x0,] */
     921             : INLINE GEN
     922          56 : rowcopy(GEN A, long x0)
     923             : {
     924          56 :   long i, lB = lg(A);
     925          56 :   GEN B  = cgetg(lB, t_VEC);
     926          56 :   for (i=1; i<lB; i++) gel(B, i) = gcopy(gcoeff(A, x0, i));
     927          56 :   return B;
     928             : }
     929             : /* A[x0, x1..x2] */
     930             : INLINE GEN
     931        2177 : row_i(GEN A, long x0, long x1, long x2)
     932             : {
     933        2177 :   long i, lB = x2 - x1 + 2;
     934        2177 :   GEN B  = cgetg(lB, t_VEC);
     935        2177 :   for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
     936        2177 :   return B;
     937             : }
     938             : 
     939             : INLINE GEN
     940     4730006 : vecreverse(GEN A)
     941             : {
     942             :   long i, l;
     943     4730006 :   GEN B = cgetg_copy(A, &l);
     944     4730006 :   for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
     945     4730006 :   return B;
     946             : }
     947             : 
     948             : INLINE GEN
     949         252 : vecsmall_reverse(GEN A)
     950             : {
     951             :   long i, l;
     952         252 :   GEN B = cgetg_copy(A, &l);
     953         252 :   for (i=1; i<l; i++) B[i] = A[l-i];
     954         252 :   return B;
     955             : }
     956             : 
     957             : INLINE void
     958         232 : vecreverse_inplace(GEN y)
     959             : {
     960         232 :   long ly = lg(y), lim = ly>>1, i;
     961         640 :   for (i = 1; i <= lim; i++)
     962             :   {
     963         408 :     GEN z = gel(y,i);
     964         408 :     gel(y,i)    = gel(y,ly-i);
     965         408 :     gel(y,ly-i) = z;
     966             :   }
     967         232 : }
     968             : 
     969             : INLINE GEN
     970    20064539 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
     971             : 
     972             : INLINE GEN
     973     4290795 : vecpermute(GEN A, GEN p)
     974             : {
     975     4290795 :   long i,lB = lg(p);
     976     4290795 :   GEN B = cgetg(lB, typ(A));
     977     4290795 :   for (i=1; i<lB; i++) gel(B, i) = gel(A, p[i]);
     978     4290795 :   return B;
     979             : }
     980             : INLINE GEN
     981       84450 : rowpermute(GEN A, GEN p)
     982             : {
     983       84450 :   long i, lB = lg(A);
     984       84450 :   GEN B = cgetg(lB, typ(A));
     985     1386053 :   for (i=1; i<lB; i++)
     986     2561507 :     gel(B, i) = typ(gel(A,i)) == t_VECSMALL ? vecsmallpermute(gel(A, i), p):
     987     1259904 :                                                     vecpermute(gel(A, i), p);
     988       84450 :   return B;
     989             : }
     990             : /*******************************************************************/
     991             : /*                                                                 */
     992             : /*                          PERMUTATIONS                           */
     993             : /*                                                                 */
     994             : /*******************************************************************/
     995             : /* identity permutation */
     996             : INLINE GEN
     997       68624 : identity_perm(long n)
     998             : {
     999       68624 :   GEN perm = cgetg(n+1, t_VECSMALL);
    1000             :   long i;
    1001       68624 :   for (i = 1; i <= n; i++) perm[i] = i;
    1002       68624 :   return perm;
    1003             : }
    1004             : 
    1005             : /* assume d <= n */
    1006             : INLINE GEN
    1007           0 : cyclic_perm(long n, long d)
    1008             : {
    1009           0 :   GEN perm = cgetg(n+1, t_VECSMALL);
    1010             :   long i;
    1011           0 :   for (i = 1; i <= n-d; i++) perm[i] = i+d;
    1012           0 :   for (     ; i <= n;   i++) perm[i] = i-n+d;
    1013           0 :   return perm;
    1014             : }
    1015             : 
    1016             : /* Multiply (compose) two permutations */
    1017             : INLINE GEN
    1018    20481851 : perm_mul(GEN s, GEN t)
    1019             : {
    1020             :   GEN u;
    1021    20481851 :   long i, l = lg(t);
    1022    20481851 :   u = cgetg(l, t_VECSMALL);
    1023    20481851 :   for (i = 1; i < l; i++) u[i] = s[ t[i] ];
    1024    20481851 :   return u;
    1025             : }
    1026             : /* Compute the inverse (reciprocal) of a permutation. */
    1027             : INLINE GEN
    1028        3801 : perm_inv(GEN x)
    1029             : {
    1030             :   long i, lx;
    1031        3801 :   GEN y = cgetg_copy(x, &lx);
    1032        3801 :   for (i=1; i<lx; i++) y[ x[i] ] = i;
    1033        3801 :   return y;
    1034             : }
    1035             : /* Return s*t*s^-1 */
    1036             : INLINE GEN
    1037          35 : perm_conj(GEN s, GEN t)
    1038             : {
    1039             :   long i, l;
    1040          35 :   GEN v = cgetg_copy(s, &l);
    1041          35 :   for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
    1042          35 :   return v;
    1043             : }
    1044             : 
    1045             : /*********************************************************************/
    1046             : /*                       MALLOC/FREE WRAPPERS                        */
    1047             : /*********************************************************************/
    1048             : #define BLOCK_SIGALRM_START          \
    1049             : {                                    \
    1050             :   int block=PARI_SIGINT_block;       \
    1051             :   PARI_SIGINT_block = 2;             \
    1052             :   MT_SIGINT_BLOCK(block);
    1053             : 
    1054             : #define BLOCK_SIGINT_START           \
    1055             : {                                    \
    1056             :   int block=PARI_SIGINT_block;       \
    1057             :   PARI_SIGINT_block = 1;             \
    1058             :   MT_SIGINT_BLOCK(block);
    1059             : 
    1060             : #define BLOCK_SIGINT_END             \
    1061             :   PARI_SIGINT_block = block;         \
    1062             :   MT_SIGINT_UNBLOCK(block);          \
    1063             :   if (!block && PARI_SIGINT_pending) \
    1064             :   {                                  \
    1065             :     int sig = PARI_SIGINT_pending;   \
    1066             :     PARI_SIGINT_pending = 0;         \
    1067             :     raise(sig);                      \
    1068             :   }                                  \
    1069             : }
    1070             : 
    1071             : INLINE void
    1072    45958633 : pari_free(void *pointer)
    1073             : {
    1074    45958633 :   BLOCK_SIGINT_START;
    1075    45959638 :   free(pointer);
    1076    45959638 :   BLOCK_SIGINT_END;
    1077    45959340 : }
    1078             : INLINE void*
    1079   183318160 : pari_malloc(size_t size)
    1080             : {
    1081   183318160 :   if (size)
    1082             :   {
    1083             :     char *tmp;
    1084   183318160 :     BLOCK_SIGINT_START;
    1085   183319204 :     tmp = (char*)malloc(size);
    1086   183319204 :     BLOCK_SIGINT_END;
    1087   183319201 :     if (!tmp) pari_err(e_MEM);
    1088   183319201 :     return tmp;
    1089             :   }
    1090           0 :   if (DEBUGMEM) pari_warn(warner,"mallocing NULL object");
    1091           0 :   return NULL;
    1092             : }
    1093             : INLINE void*
    1094      312112 : pari_realloc(void *pointer, size_t size)
    1095             : {
    1096             :   char *tmp;
    1097             : 
    1098      312112 :   BLOCK_SIGINT_START;
    1099      313305 :   if (!pointer) tmp = (char *) malloc(size);
    1100       48435 :   else tmp = (char *) realloc(pointer,size);
    1101      313305 :   BLOCK_SIGINT_END;
    1102      312900 :   if (!tmp) pari_err(e_MEM);
    1103      312900 :   return tmp;
    1104             : }
    1105             : INLINE void*
    1106       29174 : pari_calloc(size_t size)
    1107             : {
    1108       29174 :   void *t = pari_malloc(size);
    1109       29174 :   memset(t, 0, size); return t;
    1110             : }
    1111             : INLINE GEN
    1112        4905 : cgetalloc(long t, size_t l)
    1113             : {
    1114        4905 :   GEN x = (GEN)pari_malloc(l * sizeof(long));
    1115        4905 :   x[0] = evaltyp(t) | evallg(l); return x;
    1116             : }
    1117             : 
    1118             : /*******************************************************************/
    1119             : /*                                                                 */
    1120             : /*                       GARBAGE COLLECTION                        */
    1121             : /*                                                                 */
    1122             : /*******************************************************************/
    1123             : /* copy integer x as if we had avma = av */
    1124             : INLINE GEN
    1125  3563998950 : icopy_avma(GEN x, pari_sp av)
    1126             : {
    1127  3563998950 :   long i = lgefint(x), lx = i;
    1128  3563998950 :   GEN y = ((GEN)av) - i;
    1129  3563998950 :   while (--i > 0) y[i] = x[i];
    1130  3563998950 :   y[0] = evaltyp(t_INT)|evallg(lx);
    1131  3563893328 :   return y;
    1132             : }
    1133             : /* copy leaf x as if we had avma = av */
    1134             : INLINE GEN
    1135    98566351 : leafcopy_avma(GEN x, pari_sp av)
    1136             : {
    1137    98566351 :   long i = lg(x);
    1138    98566351 :   GEN y = ((GEN)av) - i;
    1139    98566351 :   while (--i > 0) y[i] = x[i];
    1140    98566351 :   y[0] = x[0] & (~CLONEBIT);
    1141    98566351 :   return y;
    1142             : }
    1143             : INLINE GEN
    1144   518112455 : gerepileuptoleaf(pari_sp av, GEN x)
    1145             : {
    1146             :   long lx;
    1147             :   GEN q;
    1148             : 
    1149   518112455 :   if (!isonstack(x) || (GEN)av<=x) { avma = av; return x; }
    1150   518817765 :   lx = lg(x);
    1151   518817765 :   q = ((GEN)av) - lx;
    1152   518817765 :   avma = (pari_sp)q;
    1153   518817765 :   while (--lx >= 0) q[lx] = x[lx];
    1154   518817765 :   return q;
    1155             : }
    1156             : INLINE GEN
    1157   876349794 : gerepileuptoint(pari_sp av, GEN x)
    1158             : {
    1159   876349794 :   if (!isonstack(x) || (GEN)av<=x) { avma = av; return x; }
    1160   758915056 :   avma = (pari_sp)icopy_avma(x, av);
    1161   759168844 :   return (GEN)avma;
    1162             : }
    1163             : INLINE GEN
    1164   619540794 : gerepileupto(pari_sp av, GEN x)
    1165             : {
    1166   619540794 :   if (!isonstack(x) || (GEN)av<=x) { avma = av; return x; }
    1167   555226324 :   switch(typ(x))
    1168             :   { /* non-default = !is_recursive_t(tq) */
    1169   170225541 :     case t_INT: return gerepileuptoint(av, x);
    1170             :     case t_REAL:
    1171             :     case t_STR:
    1172   109601546 :     case t_VECSMALL: return gerepileuptoleaf(av,x);
    1173             :     default:
    1174             :       /* NB: x+i --> ((long)x) + i*sizeof(long) */
    1175   275399237 :       return gerepile(av, (pari_sp) (x+lg(x)), x);
    1176             :   }
    1177             : }
    1178             : 
    1179             : /* gerepileupto(av, gcopy(x)) */
    1180             : INLINE GEN
    1181    33491607 : gerepilecopy(pari_sp av, GEN x)
    1182             : {
    1183    33491607 :   if (is_recursive_t(typ(x)))
    1184             :   {
    1185    27258469 :     GENbin *p = copy_bin(x);
    1186    27258476 :     avma = av; return bin_copy(p);
    1187             :   }
    1188             :   else
    1189             :   {
    1190     6233392 :     avma = av;
    1191     6233392 :     if (x < (GEN)av) {
    1192     4912018 :       if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
    1193     4912018 :       x = leafcopy_avma(x, av);
    1194     4920266 :       avma = (pari_sp)x;
    1195             :     } else
    1196     1321374 :       x = leafcopy(x);
    1197     6243785 :     return x;
    1198             :   }
    1199             : }
    1200             : 
    1201             : /* Takes an array of pointers to GENs, of length n. Copies all
    1202             :  * objects to contiguous locations and cleans up the stack between
    1203             :  * av and avma. */
    1204             : INLINE void
    1205       64491 : gerepilemany(pari_sp av, GEN* gptr[], int n)
    1206             : {
    1207             :   int i;
    1208       64491 :   for (i=0; i<n; i++) *gptr[i] = (GEN)copy_bin(*gptr[i]);
    1209       64491 :   avma = av;
    1210       64491 :   for (i=0; i<n; i++) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
    1211       64491 : }
    1212             : 
    1213             : INLINE void
    1214     4909064 : gerepileall(pari_sp av, int n, ...)
    1215             : {
    1216             :   int i;
    1217     4909064 :   va_list a; va_start(a, n);
    1218     4909064 :   if (n < 10)
    1219             :   {
    1220             :     GEN *gptr[10];
    1221    15050919 :     for (i=0; i<n; i++)
    1222    10141860 :     { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
    1223     4909059 :     avma = av;
    1224     4909059 :     for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
    1225             : 
    1226             :   }
    1227             :   else
    1228             :   {
    1229           0 :     GEN **gptr = (GEN**)  pari_malloc(n*sizeof(GEN*));
    1230           0 :     for (i=0; i<n; i++)
    1231           0 :     { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
    1232           0 :     avma = av;
    1233           0 :     for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
    1234           0 :     pari_free(gptr);
    1235             :   }
    1236     4909069 :   va_end(a);
    1237     4909069 : }
    1238             : 
    1239             : INLINE void
    1240           8 : gerepilecoeffs(pari_sp av, GEN x, int n)
    1241             : {
    1242             :   int i;
    1243           8 :   for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
    1244           8 :   avma = av;
    1245           8 :   for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
    1246           8 : }
    1247             : 
    1248             : /* p from copy_bin. Copy p->x back to stack, then destroy p */
    1249             : INLINE GEN
    1250    37572923 : bin_copy(GENbin *p)
    1251             : {
    1252             :   GEN x, y, base;
    1253             :   long dx, len;
    1254             : 
    1255    37572923 :   x   = p->x; if (!x) { pari_free(p); return gen_0; }
    1256    37570321 :   len = p->len;
    1257    37570321 :   base= p->base; dx = x - base;
    1258    37570321 :   y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
    1259    37570283 :   y += dx;
    1260    37570283 :   p->rebase(y, ((ulong)y-(ulong)x));
    1261    37570296 :   pari_free(p); return y;
    1262             : }
    1263             : 
    1264             : INLINE GEN
    1265    75143264 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
    1266             : 
    1267             : INLINE void
    1268    26797205 : cgiv(GEN x)
    1269             : {
    1270    26797205 :   pari_sp av = (pari_sp)(x+lg(x));
    1271    26797205 :   if (isonstack((GEN)av)) avma = av;
    1272    26797039 : }
    1273             : 
    1274             : INLINE void
    1275     1230437 : killblock(GEN x) { gunclone(x); }
    1276             : 
    1277             : INLINE int
    1278    17285198 : is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
    1279             : 
    1280             : /*******************************************************************/
    1281             : /*                                                                 */
    1282             : /*                    CONVERSION / ASSIGNMENT                      */
    1283             : /*                                                                 */
    1284             : /*******************************************************************/
    1285             : /* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
    1286             : INLINE GEN
    1287     2067600 : cxcompotor(GEN z, long prec)
    1288             : {
    1289     2067600 :   switch(typ(z))
    1290             :   {
    1291     1046980 :     case t_INT:  return itor(z, prec);
    1292      134120 :     case t_FRAC: return fractor(z, prec);
    1293      886500 :     case t_REAL: return rtor(z, prec);
    1294           0 :     default: pari_err_TYPE("cxcompotor",z); return NULL; /* not reached */
    1295             :   }
    1296             : }
    1297             : INLINE GEN
    1298     1032108 : cxtofp(GEN x, long prec)
    1299     1032108 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
    1300             : 
    1301             : INLINE double
    1302    25877814 : gtodouble(GEN x)
    1303             : {
    1304    25877814 :   if (typ(x)!=t_REAL) {
    1305     4810528 :     pari_sp av = avma;
    1306     4810528 :     x = gtofp(x, DEFAULTPREC);
    1307     4810528 :     if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
    1308     4810528 :     avma = av;
    1309             :   }
    1310    25877814 :   return rtodbl(x);
    1311             : }
    1312             : INLINE long
    1313    64507736 : gtos(GEN x) {
    1314    64507736 :   if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
    1315    64507729 :   return itos(x);
    1316             : }
    1317             : 
    1318             : INLINE ulong
    1319       82542 : gtou(GEN x) {
    1320       82542 :   if (typ(x) != t_INT || signe(x)<0)
    1321           0 :     pari_err_TYPE("gtou [integer >=0 expected]",x);
    1322       82738 :   return itou(x);
    1323             : }
    1324             : 
    1325             : INLINE GEN
    1326     1673152 : absfrac(GEN x)
    1327             : {
    1328     1673152 :   GEN y = cgetg(3, t_FRAC);
    1329     1673152 :   gel(y,1) = absi(gel(x,1));
    1330     1673152 :   gel(y,2) = icopy(gel(x,2)); return y;
    1331             : }
    1332             : INLINE GEN
    1333       10895 : absfrac_shallow(GEN x)
    1334       10895 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
    1335             : INLINE GEN
    1336      531657 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
    1337             : INLINE GEN
    1338       31694 : Q_abs_shallow(GEN x)
    1339       31694 : { return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
    1340             : INLINE GEN
    1341         322 : R_abs_shallow(GEN x)
    1342         322 : { return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
    1343             : INLINE GEN
    1344           0 : R_abs(GEN x)
    1345           0 : { return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
    1346             : 
    1347             : /* Force z to be of type real/complex with floating point components */
    1348             : INLINE GEN
    1349    18535058 : gtofp(GEN z, long prec)
    1350             : {
    1351    18535058 :   switch(typ(z))
    1352             :   {
    1353     8422824 :     case t_INT:  return itor(z, prec);
    1354     4953620 :     case t_FRAC: return fractor(z, prec);
    1355     4298395 :     case t_REAL: return rtor(z, prec);
    1356             :     case t_COMPLEX: {
    1357      860219 :       GEN a = gel(z,1), b = gel(z,2);
    1358      860219 :       if (isintzero(b)) return cxcompotor(a, prec);
    1359      858518 :       if (isintzero(a)) {
    1360        1641 :         GEN y = cgetg(3, t_COMPLEX);
    1361        1641 :         b = cxcompotor(b, prec);
    1362        1641 :         gel(y,1) = real_0_bit(expo(b) - prec2nbits(prec));
    1363        1641 :         gel(y,2) = b; return y;
    1364             :       }
    1365      856877 :       return cxtofp(z, prec);
    1366             :     }
    1367           0 :     case t_QUAD: return quadtofp(z, prec);
    1368           0 :     default: pari_err_TYPE("gtofp",z); return NULL; /* not reached */
    1369             :   }
    1370             : }
    1371             : /* Force z to be of type real / int */
    1372             : INLINE GEN
    1373       22400 : gtomp(GEN z, long prec)
    1374             : {
    1375       22400 :   switch(typ(z))
    1376             :   {
    1377          14 :     case t_INT:  return z;
    1378       22386 :     case t_FRAC: return fractor(z, prec);
    1379           0 :     case t_REAL: return rtor(z, prec);
    1380           0 :     case t_QUAD: z = quadtofp(z, prec);
    1381           0 :                  if (typ(z) == t_REAL) return z;
    1382           0 :     default: pari_err_TYPE("gtomp",z); return NULL; /* not reached */
    1383             :   }
    1384             : }
    1385             : 
    1386             : INLINE GEN
    1387      487235 : RgX_gtofp(GEN x, long prec)
    1388             : {
    1389             :   long l;
    1390      487235 :   GEN y = cgetg_copy(x, &l);
    1391      487235 :   while (--l > 1) gel(y,l) = gtofp(gel(x,l), prec);
    1392      487235 :   y[1] = x[1]; return y;
    1393             : }
    1394             : INLINE GEN
    1395      264009 : RgC_gtofp(GEN x, long prec)
    1396             : {
    1397      264009 :   long l = lg(x);
    1398      264009 :   GEN y = cgetg(l, t_COL);
    1399      264009 :   while (--l > 0) gel(y,l) = gtofp(gel(x,l), prec);
    1400      264009 :   return y;
    1401             : }
    1402             : INLINE GEN
    1403       18649 : RgM_gtofp(GEN x, long prec)
    1404             : {
    1405             :   long l;
    1406       18649 :   GEN y = cgetg_copy(x, &l);
    1407       18649 :   while (--l > 0) gel(y,l) = RgC_gtofp(gel(x,l), prec);
    1408       18649 :   return y;
    1409             : }
    1410             : INLINE GEN
    1411         560 : RgC_gtomp(GEN x, long prec)
    1412             : {
    1413         560 :   long l = lg(x);
    1414         560 :   GEN y = cgetg(l, t_COL);
    1415         560 :   while (--l > 0) gel(y,l) = gtomp(gel(x,l), prec);
    1416         560 :   return y;
    1417             : }
    1418             : INLINE GEN
    1419          14 : RgM_gtomp(GEN x, long prec)
    1420             : {
    1421             :   long l;
    1422          14 :   GEN y = cgetg_copy(x, &l);
    1423          14 :   while (--l > 0) gel(y,l) = RgC_gtomp(gel(x,l), prec);
    1424          14 :   return y;
    1425             : }
    1426             : 
    1427             : INLINE GEN
    1428        6434 : RgX_fpnorml2(GEN x, long prec)
    1429             : {
    1430        6434 :   pari_sp av = avma;
    1431        6434 :   return gerepileupto(av, gnorml2(RgX_gtofp(x, prec)));
    1432             : }
    1433             : INLINE GEN
    1434      132371 : RgC_fpnorml2(GEN x, long prec)
    1435             : {
    1436      132371 :   pari_sp av = avma;
    1437      132371 :   return gerepileupto(av, gnorml2(RgC_gtofp(x, prec)));
    1438             : }
    1439             : INLINE GEN
    1440        1374 : RgM_fpnorml2(GEN x, long prec)
    1441             : {
    1442        1374 :   pari_sp av = avma;
    1443        1374 :   return gerepileupto(av, gnorml2(RgM_gtofp(x, prec)));
    1444             : }
    1445             : 
    1446             : /* y a t_REAL */
    1447             : INLINE void
    1448      180734 : affgr(GEN x, GEN y)
    1449             : {
    1450             :   pari_sp av;
    1451      180734 :   switch(typ(x)) {
    1452         512 :     case t_INT:  affir(x,y); break;
    1453      180222 :     case t_REAL: affrr(x,y); break;
    1454           0 :     case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
    1455           0 :     case t_QUAD: av = avma; affgr(quadtofp(x,realprec(y)), y); avma = av; break;
    1456           0 :     default: pari_err_TYPE2("=",x,y);
    1457             :   }
    1458      180734 : }
    1459             : 
    1460             : INLINE GEN
    1461      183809 : affc_fixlg(GEN x, GEN res)
    1462             : {
    1463      183809 :   if (typ(x) == t_COMPLEX)
    1464             :   {
    1465      175308 :     affrr_fixlg(gel(x,1), gel(res,1));
    1466      175308 :     affrr_fixlg(gel(x,2), gel(res,2));
    1467             :   }
    1468             :   else
    1469             :   {
    1470        8501 :     avma = (pari_sp)(res+3);
    1471        8501 :     res = cgetr(realprec(gel(res,1)));
    1472        8501 :     affrr_fixlg(x, res);
    1473             :   }
    1474      183809 :   return res;
    1475             : }
    1476             : 
    1477             : INLINE GEN
    1478           0 : trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
    1479             : 
    1480             : /*******************************************************************/
    1481             : /*                                                                 */
    1482             : /*                          LENGTH CONVERSIONS                     */
    1483             : /*                                                                 */
    1484             : /*******************************************************************/
    1485             : INLINE long
    1486       22505 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
    1487             : INLINE long
    1488       22062 : ndec2prec(long x) { return 2 + ndec2nlong(x); }
    1489             : INLINE long
    1490         443 : ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
    1491             : /* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
    1492             :  * to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
    1493             :  *   addl, shrl instead of subl, sarl, addl */
    1494             : INLINE long
    1495      974563 : nbits2nlong(long x) {
    1496      974563 :   return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1497             : }
    1498             : 
    1499             : INLINE long
    1500   237472474 : nbits2extraprec(long x) {
    1501   237472474 :   return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1502             : }
    1503             : 
    1504             : /* Fast implementation of 2 + nbits2nlong(x) */
    1505             : INLINE long
    1506    74060554 : nbits2prec(long x) {
    1507    74060554 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1508             : }
    1509             : INLINE long
    1510     3281550 : nbits2lg(long x) {
    1511     3281550 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
    1512             : }
    1513             : /* ceil(x / sizeof(long)) */
    1514             : INLINE long
    1515     6902917 : nchar2nlong(long x) {
    1516     6902917 :   return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
    1517             : }
    1518             : INLINE long
    1519  1166030248 : prec2nbits(long x) { return (x-2) * BITS_IN_LONG; }
    1520             : INLINE double
    1521      451159 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
    1522             : INLINE double
    1523      223954 : prec2nbits_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
    1524             : INLINE long
    1525    23438946 : bit_prec(GEN x) { return prec2nbits(realprec(x)); }
    1526             : INLINE long
    1527  1088470916 : bit_accuracy(long x) { return prec2nbits(x); }
    1528             : INLINE long
    1529       12001 : prec2ndec(long x) { return (long)prec2nbits_mul(x, LOG10_2); }
    1530             : INLINE long
    1531         165 : nbits2ndec(long x) { return (long)(x * LOG10_2); }
    1532             : INLINE long
    1533        1741 : precdbl(long x) {return (x - 1) << 1;}
    1534             : INLINE long
    1535  2880543683 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
    1536             : INLINE long
    1537  2816693291 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
    1538             : 
    1539             : /*********************************************************************/
    1540             : /**                                                                 **/
    1541             : /**                      OPERATIONS MODULO m                        **/
    1542             : /**                                                                 **/
    1543             : /*********************************************************************/
    1544             : /* Assume m > 0, more efficient if 0 <= a, b < m */
    1545             : 
    1546             : INLINE GEN
    1547     1147108 : Fp_red(GEN a, GEN m) { return modii(a, m); }
    1548             : INLINE GEN
    1549   359339035 : Fp_add(GEN a, GEN b, GEN m)
    1550             : {
    1551   359339035 :   pari_sp av=avma;
    1552   359339035 :   GEN p = addii(a,b);
    1553   359319983 :   long s = signe(p);
    1554   359319983 :   if (!s) return p; /* = gen_0 */
    1555   172165821 :   if (s > 0) /* general case */
    1556             :   {
    1557   171943957 :     GEN t = subii(p, m);
    1558   171955262 :     s = signe(t);
    1559   171955262 :     if (!s) { avma = av; return gen_0; }
    1560   159249577 :     if (s < 0) { avma = (pari_sp)p; return p; }
    1561    27078403 :     if (cmpii(t, m) < 0) return gerepileuptoint(av, t); /* general case ! */
    1562     1088039 :     p = remii(t, m);
    1563             :   }
    1564             :   else
    1565      221864 :     p = modii(p, m);
    1566     1309903 :   return gerepileuptoint(av, p);
    1567             : }
    1568             : INLINE GEN
    1569    45143186 : Fp_sub(GEN a, GEN b, GEN m)
    1570             : {
    1571    45143186 :   pari_sp av=avma;
    1572    45143186 :   GEN p = subii(a,b);
    1573    45190124 :   long s = signe(p);
    1574    45190124 :   if (!s) return p; /* = gen_0 */
    1575    38270362 :   if (s > 0)
    1576             :   {
    1577    18660588 :     if (cmpii(p, m) < 0) return p; /* general case ! */
    1578      742259 :     p = remii(p, m);
    1579             :   }
    1580             :   else
    1581             :   {
    1582    19609774 :     GEN t = addii(p, m);
    1583    19601334 :     if (!s) { avma = av; return gen_0; }
    1584    19601334 :     if (s > 0) return gerepileuptoint(av, t); /* general case ! */
    1585    19601334 :     p = modii(t, m);
    1586             :   }
    1587    20358845 :   return gerepileuptoint(av, p);
    1588             : }
    1589             : INLINE GEN
    1590    10726916 : Fp_neg(GEN b, GEN m)
    1591             : {
    1592    10726916 :   pari_sp av = avma;
    1593    10726916 :   long s = signe(b);
    1594             :   GEN p;
    1595    10726916 :   if (!s) return gen_0;
    1596     7395277 :   if (s > 0)
    1597             :   {
    1598     7213904 :     p = subii(m, b);
    1599     7213647 :     if (signe(p) >= 0) return p; /* general case ! */
    1600       67284 :     p = modii(p, m);
    1601             :   } else
    1602      181373 :     p = remii(negi(b), m);
    1603      248657 :   return gerepileuptoint(av, p);
    1604             : }
    1605             : 
    1606             : INLINE GEN
    1607       19785 : Fp_halve(GEN a, GEN p)
    1608             : {
    1609       19785 :   if (mpodd(a)) a = addii(a,p);
    1610       19785 :   return shifti(a,-1);
    1611             : }
    1612             : 
    1613             : /* assume 0 <= u < p and ps2 = p>>1 */
    1614             : INLINE GEN
    1615     5062980 : Fp_center(GEN u, GEN p, GEN ps2)
    1616     5062980 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
    1617             : 
    1618             : /* x + y*z mod p */
    1619             : INLINE GEN
    1620     1423835 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
    1621             : {
    1622             :   pari_sp av;
    1623     1423835 :   if (!signe(y) || !signe(z)) return Fp_red(x, p);
    1624     1423562 :   if (!signe(x)) return Fp_mul(z,y, p);
    1625     1136394 :   av = avma;
    1626     1136394 :   return gerepileuptoint(av, modii(addii(x, mulii(y,z)), p));
    1627             : }
    1628             : 
    1629             : INLINE GEN
    1630   342511939 : Fp_mul(GEN a, GEN b, GEN m)
    1631             : {
    1632   342511939 :   pari_sp av=avma;
    1633             :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1634   342511939 :   (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
    1635   342511722 :   p = mulii(a,b);
    1636   342512532 :   avma = av; return modii(p,m);
    1637             : }
    1638             : INLINE GEN
    1639     6956941 : Fp_sqr(GEN a, GEN m)
    1640             : {
    1641     6956941 :   pari_sp av=avma;
    1642             :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1643     6956941 :   (void)new_chunk((lg(a)+lg(m))<<1);
    1644     6989718 :   p = sqri(a);
    1645     7017671 :   avma = av; return modii(p,m);
    1646             : }
    1647             : INLINE GEN
    1648    44806058 : Fp_mulu(GEN a, ulong b, GEN m)
    1649             : {
    1650    44806058 :   long l = lgefint(m);
    1651    44806058 :   if (l == 3)
    1652             :   {
    1653    32118753 :     ulong mm = m[2];
    1654    32118753 :     return utoi( Fl_mul(umodiu(a, mm), b, mm) );
    1655             :   } else {
    1656    12687305 :     pari_sp av = avma;
    1657             :     GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1658    12687305 :     (void)new_chunk(lg(a)+1+(l<<1));
    1659    12699678 :     p = muliu(a,b);
    1660    12433815 :     avma = av; return modii(p,m);
    1661             :   }
    1662             : }
    1663             : INLINE GEN
    1664           0 : Fp_muls(GEN a, long b, GEN m)
    1665             : {
    1666           0 :   long l = lgefint(m);
    1667           0 :   if (l == 3)
    1668             :   {
    1669           0 :     ulong mm = m[2];
    1670           0 :     if (b < 0)
    1671             :     {
    1672           0 :       ulong t = Fl_mul(umodiu(a, mm), -b, mm);
    1673           0 :       return t? utoipos(mm - t): gen_0;
    1674             :     }
    1675             :     else
    1676           0 :       return utoi( Fl_mul(umodiu(a, mm), b, mm) );
    1677             :   } else {
    1678           0 :     pari_sp av = avma;
    1679             :     GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1680           0 :     (void)new_chunk(lg(a)+1+(l<<1));
    1681           0 :     p = mulis(a,b);
    1682           0 :     avma = av; return modii(p,m);
    1683             :   }
    1684             : }
    1685             : 
    1686             : INLINE GEN
    1687     7583560 : Fp_inv(GEN a, GEN m)
    1688             : {
    1689             :   GEN res;
    1690     7583560 :   if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
    1691     7568400 :   return res;
    1692             : }
    1693             : INLINE GEN
    1694      285874 : Fp_invsafe(GEN a, GEN m)
    1695             : {
    1696             :   GEN res;
    1697      285874 :   if (! invmod(a,m,&res)) return NULL;
    1698      285874 :   return res;
    1699             : }
    1700             : INLINE GEN
    1701     4600669 : Fp_div(GEN a, GEN b, GEN m)
    1702             : {
    1703     4600669 :   pari_sp av=avma;
    1704             :   GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
    1705     4600669 :   (void)new_chunk(lg(a)+(lg(m)<<1));
    1706     4611364 :   p = mulii(a, Fp_inv(b,m));
    1707     4619655 :   avma = av; return modii(p,m);
    1708             : }
    1709             : 
    1710             : INLINE GEN
    1711      665878 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
    1712             : 
    1713             : /*******************************************************************/
    1714             : /*                                                                 */
    1715             : /*                        ADDMULII / SUBMULII                      */
    1716             : /*                                                                 */
    1717             : /*******************************************************************/
    1718             : /* x - y*z */
    1719             : INLINE GEN
    1720    13425654 : submulii(GEN x, GEN y, GEN z)
    1721             : {
    1722    13425654 :   long lx = lgefint(x), ly, lz;
    1723             :   pari_sp av;
    1724             :   GEN t;
    1725    13425654 :   if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
    1726    11619016 :   ly = lgefint(y);
    1727    11619016 :   if (ly == 2) return icopy(x);
    1728    10623568 :   lz = lgefint(z);
    1729    10623568 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1730    10623568 :   t = mulii(z, y);
    1731    10623568 :   avma = av; return subii(x,t);
    1732             : }
    1733             : /* y*z - x */
    1734             : INLINE GEN
    1735     2209638 : mulsubii(GEN y, GEN z, GEN x)
    1736             : {
    1737     2209638 :   long lx = lgefint(x), ly, lz;
    1738             :   pari_sp av;
    1739             :   GEN t;
    1740     2209638 :   if (lx == 2) return mulii(z,y);
    1741     1368570 :   ly = lgefint(y);
    1742     1368570 :   if (ly == 2) return negi(x);
    1743     1319280 :   lz = lgefint(z);
    1744     1319280 :   av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
    1745     1319280 :   t = mulii(z, y);
    1746     1319280 :   avma = av; return subii(t,x);
    1747             : }
    1748             : 
    1749             : /* x - u*y */
    1750             : INLINE GEN
    1751        7000 : submuliu(GEN x, GEN y, ulong u)
    1752             : {
    1753             :   pari_sp av;
    1754        7000 :   long ly = lgefint(y);
    1755        7000 :   if (ly == 2) return icopy(x);
    1756        7000 :   av = avma;
    1757        7000 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1758        7000 :   y = mului(u,y);
    1759        7000 :   avma = av; return subii(x, y);
    1760             : }
    1761             : /* x + u*y */
    1762             : INLINE GEN
    1763        7000 : addmuliu(GEN x, GEN y, ulong u)
    1764             : {
    1765             :   pari_sp av;
    1766        7000 :   long ly = lgefint(y);
    1767        7000 :   if (ly == 2) return icopy(x);
    1768        7000 :   av = avma;
    1769        7000 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1770        7000 :   y = mului(u,y);
    1771        7000 :   avma = av; return addii(x, y);
    1772             : }
    1773             : /* x - u*y */
    1774             : INLINE GEN
    1775   109514067 : submuliu_inplace(GEN x, GEN y, ulong u)
    1776             : {
    1777             :   pari_sp av;
    1778   109514067 :   long ly = lgefint(y);
    1779   109514067 :   if (ly == 2) return x;
    1780    87768831 :   av = avma;
    1781    87768831 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1782    87768831 :   y = mului(u,y);
    1783    87768831 :   avma = av; return subii(x, y);
    1784             : }
    1785             : /* x + u*y */
    1786             : INLINE GEN
    1787   108231796 : addmuliu_inplace(GEN x, GEN y, ulong u)
    1788             : {
    1789             :   pari_sp av;
    1790   108231796 :   long ly = lgefint(y);
    1791   108231796 :   if (ly == 2) return x;
    1792    87452877 :   av = avma;
    1793    87452877 :   (void)new_chunk(3+ly+lgefint(x)); /* HACK */
    1794    87452877 :   y = mului(u,y);
    1795    87452877 :   avma = av; return addii(x, y);
    1796             : }
    1797             : /* ux + vy */
    1798             : INLINE GEN
    1799    26206218 : lincombii(GEN u, GEN v, GEN x, GEN y)
    1800             : {
    1801    26206218 :   long lx = lgefint(x), ly;
    1802             :   GEN p1, p2;
    1803             :   pari_sp av;
    1804    26206218 :   if (lx == 2) return mulii(v,y);
    1805    16018906 :   ly = lgefint(y);
    1806    16018906 :   if (ly == 2) return mulii(u,x);
    1807    14812735 :   av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
    1808    14812735 :   p1 = mulii(u,x);
    1809    14812735 :   p2 = mulii(v,y);
    1810    14812735 :   avma = av; return addii(p1,p2);
    1811             : }
    1812             : 
    1813             : /*******************************************************************/
    1814             : /*                                                                 */
    1815             : /*                          GEN SUBTYPES                           */
    1816             : /*                                                                 */
    1817             : /*******************************************************************/
    1818             : 
    1819             : INLINE int
    1820   984582703 : is_const_t(long t) { return (t < t_POLMOD); }
    1821             : INLINE int
    1822       25185 : is_extscalar_t(long t) { return (t <= t_POL); }
    1823             : INLINE int
    1824      863430 : is_intreal_t(long t) { return (t <= t_REAL); }
    1825             : INLINE int
    1826   345998311 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
    1827             : INLINE int
    1828    50813162 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
    1829             : INLINE int
    1830      574866 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
    1831             : INLINE int
    1832       34440 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
    1833             : INLINE int
    1834  2462378627 : is_recursive_t(long t) { return lontyp[t]; }
    1835             : INLINE int
    1836   151536963 : is_scalar_t(long t) { return (t < t_POL); }
    1837             : INLINE int
    1838      405158 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
    1839             : 
    1840             : /*******************************************************************/
    1841             : /*                                                                 */
    1842             : /*                         TRANSCENDENTAL                          */
    1843             : /*                                                                 */
    1844             : /*******************************************************************/
    1845             : INLINE GEN
    1846    15176504 : sqrtr(GEN x) {
    1847    15176504 :   long s = signe(x);
    1848    15176504 :   if (s == 0) return real_0_bit(expo(x) >> 1);
    1849    15143015 :   if (s >= 0) return sqrtr_abs(x);
    1850       13474 :   retmkcomplex(gen_0, sqrtr_abs(x));
    1851             : }
    1852             : INLINE GEN
    1853       12019 : cbrtr(GEN x) {
    1854       12019 :   long s = signe(x);
    1855             :   GEN r;
    1856       12019 :   if (s == 0) return real_0_bit(expo(x) / 3);
    1857       12019 :   r = cbrtr_abs(x);
    1858       12019 :   if (s < 0) togglesign(r);
    1859       12019 :   return r;
    1860             : }
    1861             : /* x^(1/n) */
    1862             : INLINE GEN
    1863       34800 : sqrtnr(GEN x, long n) {
    1864       34800 :   switch(n)
    1865             :   {
    1866         636 :     case 1: return rcopy(x);
    1867        5364 :     case 2: return sqrtr(x);
    1868       12019 :     case 3: return cbrtr(x);
    1869             :   }
    1870       16781 :   return mpexp(divrs(mplog(x), n));
    1871             : }
    1872             : INLINE long
    1873      402688 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
    1874             : 
    1875             : /*******************************************************************/
    1876             : /*                                                                 */
    1877             : /*                         MISCELLANEOUS                           */
    1878             : /*                                                                 */
    1879             : /*******************************************************************/
    1880      863353 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
    1881   367120699 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
    1882     6272876 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
    1883         301 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
    1884   210818054 : INLINE int equali1(GEN n)
    1885   210818054 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
    1886    23576568 : INLINE int equalim1(GEN n)
    1887    23576568 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
    1888             : /* works only for POSITIVE integers */
    1889  1456841918 : INLINE int is_pm1(GEN n)
    1890  1456841918 : { return lgefint(n) == 3 && n[2] == 1; }
    1891   126568028 : INLINE int is_bigint(GEN n)
    1892   126568028 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
    1893             : 
    1894  1287339368 : INLINE int odd(long x) { return x & 1; }
    1895     7400521 : INLINE int both_odd(long x, long y) { return x & y & 1; }
    1896             : 
    1897             : INLINE int
    1898  2040904327 : isonstack(GEN x)
    1899  4081808654 : { return ((pari_sp)x >= pari_mainstack->bot
    1900  2040904327 :        && (pari_sp)x <  pari_mainstack->top); }
    1901             : 
    1902             : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
    1903             : INLINE double
    1904    17039058 : dbllog2r(GEN x)
    1905    17039058 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
    1906             : 
    1907             : INLINE GEN
    1908      225229 : mul_content(GEN cx, GEN cy)
    1909             : {
    1910      225229 :   if (!cx) return cy;
    1911       52339 :   if (!cy) return cx;
    1912       37322 :   return gmul(cx,cy);
    1913             : }
    1914             : INLINE GEN
    1915      424857 : mul_denom(GEN dx, GEN dy)
    1916             : {
    1917      424857 :   if (!dx) return dy;
    1918      317769 :   if (!dy) return dx;
    1919      126910 :   return mulii(dx,dy);
    1920             : }
    1921             : 
    1922             : /* POLYNOMIALS */
    1923             : INLINE GEN
    1924    17559289 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
    1925             : INLINE GEN
    1926    73365376 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
    1927             : INLINE ulong
    1928      406877 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
    1929             : INLINE long
    1930  2462996951 : degpol(GEN x) { return lg(x)-3; }
    1931             : INLINE long
    1932  1297156981 : lgpol(GEN x) { return lg(x)-2; }
    1933             : INLINE long
    1934    58620483 : lgcols(GEN x) { return lg(gel(x,1)); }
    1935             : INLINE long
    1936    13907441 : nbrows(GEN x) { return lg(gel(x,1))-1; }
    1937             : INLINE GEN
    1938           0 : truecoeff(GEN x, long n) { return polcoeff0(x,n,-1); }
    1939             : 
    1940             : INLINE GEN
    1941      425444 : RgXQ_mul(GEN y, GEN x, GEN T) { return RgX_rem(RgX_mul(y, x), T); }
    1942             : INLINE GEN
    1943      210823 : RgXQ_sqr(GEN x, GEN T) { return RgX_rem(RgX_sqr(x), T); }
    1944             : 
    1945             : INLINE GEN
    1946      117705 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
    1947             : INLINE GEN
    1948       20867 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
    1949             : 
    1950             : INLINE GEN
    1951    61863188 : RgX_copy(GEN x)
    1952             : {
    1953             :   long lx, i;
    1954    61863188 :   GEN y = cgetg_copy(x, &lx); y[1] = x[1];
    1955    61863189 :   for (i = 2; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    1956    61863189 :   return y;
    1957             : }
    1958             : /* have to use ulong to avoid silly warnings from gcc "assuming signed
    1959             :  * overflow does not occur" */
    1960             : INLINE GEN
    1961     2235339 : RgX_coeff(GEN x, long n)
    1962             : {
    1963     2235339 :   ulong l = lg(x);
    1964     2235339 :   return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
    1965             : }
    1966             : INLINE GEN
    1967      310625 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
    1968             : INLINE GEN
    1969     6098199 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
    1970             : INLINE GEN
    1971     2415929 : RgX_rem(GEN x, GEN y) { return RgX_divrem(x,y,ONLY_REM); }
    1972             : INLINE GEN
    1973           7 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
    1974             : INLINE GEN
    1975       17192 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
    1976             : INLINE GEN
    1977      739237 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
    1978             : INLINE GEN
    1979     3091468 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
    1980             : INLINE GEN
    1981     2863853 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
    1982             : INLINE GEN
    1983           0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
    1984             : INLINE GEN
    1985    43546201 : pol0_Flx(long sv) { return mkvecsmall(sv); }
    1986             : INLINE GEN
    1987    17547393 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
    1988             : INLINE GEN
    1989    17151038 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
    1990             : INLINE GEN
    1991           0 : zero_zx(long sv) { return zero_Flx(sv); }
    1992             : INLINE GEN
    1993           0 : polx_zx(long sv) { return polx_Flx(sv); }
    1994             : INLINE GEN
    1995           0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
    1996             : INLINE GEN
    1997           0 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
    1998             : INLINE GEN
    1999          56 : zero_F2x(long sv) { return zero_Flx(sv); }
    2000             : INLINE GEN
    2001    10881975 : pol0_F2x(long sv) { return pol0_Flx(sv); }
    2002             : INLINE GEN
    2003     2645647 : pol1_F2x(long sv) { return pol1_Flx(sv); }
    2004             : INLINE GEN
    2005      655740 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
    2006             : INLINE int
    2007      791063 : F2x_equal1(GEN x) { return Flx_equal1(x); }
    2008             : INLINE int
    2009     3342683 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
    2010             : INLINE GEN
    2011    66308763 : F2x_copy(GEN x) { return leafcopy(x); }
    2012             : INLINE GEN
    2013           0 : F2v_copy(GEN x) { return leafcopy(x); }
    2014             : INLINE GEN
    2015     2445996 : Flv_copy(GEN x) { return leafcopy(x); }
    2016             : INLINE GEN
    2017    74774038 : Flx_copy(GEN x) { return leafcopy(x); }
    2018             : INLINE GEN
    2019     1611629 : vecsmall_copy(GEN x) { return leafcopy(x); }
    2020             : INLINE int
    2021     4642138 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
    2022             : INLINE int
    2023       17993 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
    2024             : 
    2025             : INLINE GEN
    2026    73565818 : ZX_renormalize(GEN x, long lx)    { return ZXX_renormalize(x,lx); }
    2027             : INLINE GEN
    2028    86879860 : FpX_renormalize(GEN x, long lx)   { return ZXX_renormalize(x,lx); }
    2029             : INLINE GEN
    2030      946830 : FpXX_renormalize(GEN x, long lx)  { return ZXX_renormalize(x,lx); }
    2031             : INLINE GEN
    2032      984994 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
    2033             : INLINE GEN
    2034   126414385 : F2x_renormalize(GEN x, long lx)   { return Flx_renormalize(x,lx); }
    2035             : INLINE GEN
    2036        7187 : F2v_to_F2x(GEN x, long sv) {
    2037        7187 :   GEN y = leafcopy(x);
    2038        7187 :   y[1] = sv; F2x_renormalize(y, lg(y)); return y;
    2039             : }
    2040             : 
    2041             : INLINE GEN
    2042           0 : ZX_ZXY_resultant(GEN a, GEN b) { return ZX_ZXY_rnfequation(a,b,NULL); }
    2043             : INLINE long
    2044         441 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
    2045             : INLINE GEN
    2046       34933 : resultant(GEN x, GEN y) { return resultant_all(x,y,NULL); }
    2047             : 
    2048             : INLINE long
    2049        2065 : gval(GEN x, long v) {
    2050        2065 :   pari_sp av = avma;
    2051        2065 :   long n = gvaluation(x, pol_x(v));
    2052        2065 :   avma = av; return n;
    2053             : }
    2054             : 
    2055             : INLINE void
    2056      585145 : RgX_shift_inplace_init(long v)
    2057      585145 : { if (v) (void)cgetg(v, t_VECSMALL); }
    2058             : /* shift polynomial in place. assume v free cells have been left before x */
    2059             : INLINE GEN
    2060      585144 : RgX_shift_inplace(GEN x, long v)
    2061             : {
    2062             :   long i, lx;
    2063             :   GEN y, z;
    2064      585144 :   if (!v) return x;
    2065      257816 :   lx = lg(x);
    2066      257816 :   if (lx == 2) return x;
    2067      257816 :   y = x + v;
    2068      257816 :   z = x + lx;
    2069             :   /* stackdummy from normalizepol: move it up */
    2070      257816 :   if (lg(z) != v) x[lx + v] = z[0];
    2071      257816 :   for (i = lx-1; i >= 2; i--) gel(y,i) = gel(x,i);
    2072      257816 :   for (i = v+1;  i >= 2; i--) gel(x,i) = gen_0;
    2073             :   /* leave x[1] alone: it is correct */
    2074      257816 :   x[0] = evaltyp(t_POL) | evallg(lx+v); return x;
    2075             : }
    2076             : 
    2077             : 
    2078             : /* LINEAR ALGEBRA */
    2079             : INLINE GEN
    2080        8785 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
    2081             : INLINE GEN
    2082     1205647 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
    2083             : INLINE GEN
    2084      225566 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
    2085             : INLINE GEN
    2086           0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
    2087             : INLINE GEN
    2088           0 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
    2089             : INLINE GEN
    2090           0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
    2091             : INLINE GEN
    2092           0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
    2093             : INLINE GEN
    2094    16029082 : zero_zv(long x) { return zero_Flv(x); }
    2095             : INLINE GEN
    2096         168 : zm_transpose(GEN x) { return Flm_transpose(x); }
    2097             : INLINE GEN
    2098           0 : zm_copy(GEN x) { return Flm_copy(x); }
    2099             : INLINE GEN
    2100     2147251 : zv_copy(GEN x) { return Flv_copy(x); }
    2101             : INLINE GEN
    2102           0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
    2103             : 
    2104             : INLINE GEN
    2105      775240 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfremdiv(x,y,NULL); }
    2106             : INLINE GEN
    2107       23391 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
    2108             : INLINE GEN
    2109     1223244 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
    2110             : INLINE void
    2111      107286 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
    2112             : INLINE GEN
    2113       22494 : RgM_inv(GEN a) { return RgM_solve(a, NULL); }
    2114             : INLINE GEN
    2115     7892602 : RgM_shallowcopy(GEN x)
    2116             : {
    2117             :   long l;
    2118     7892602 :   GEN y = cgetg_copy(x, &l);
    2119     7892602 :   while (--l > 0) gel(y,l) = leafcopy(gel(x,l));
    2120     7892602 :   return y;
    2121             : }
    2122             : INLINE GEN
    2123        1733 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
    2124             : 
    2125             : INLINE GEN
    2126      852317 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
    2127             : 
    2128             : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
    2129             :  * x,y are ZV of the same length */
    2130             : INLINE int
    2131       23198 : ZV_dvd(GEN x, GEN y)
    2132             : {
    2133       23198 :   long i, l = lg(x);
    2134       36652 :   for (i=1; i < l; i++)
    2135       29519 :     if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
    2136        7133 :   return 1;
    2137             : }
    2138             : 
    2139             : /* Fq */
    2140             : INLINE GEN
    2141     1298260 : Fq_red(GEN x, GEN T, GEN p)
    2142     1298260 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
    2143             : INLINE GEN
    2144       10887 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
    2145             : {
    2146             :   (void) p;
    2147       10887 :   return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
    2148             : }
    2149             : 
    2150             : INLINE GEN
    2151         833 : gener_Fq_local(GEN T, GEN p, GEN L)
    2152         833 : { return T? gener_FpXQ_local(T,p, L)
    2153         833 :           : pgener_Fp_local(p, L); }
    2154             : 
    2155             : /* FpXQX */
    2156             : INLINE GEN
    2157         940 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
    2158             : INLINE GEN
    2159       31345 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
    2160             : INLINE GEN
    2161       29785 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
    2162             : 
    2163             : /* FqX */
    2164             : INLINE GEN
    2165       27419 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
    2166             : INLINE GEN
    2167      903963 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2168             : INLINE GEN
    2169       16438 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
    2170             : INLINE GEN
    2171        2380 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2172             : INLINE GEN
    2173      601520 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
    2174      601520 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
    2175             : INLINE GEN
    2176      552441 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
    2177      552441 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
    2178             : INLINE GEN
    2179       81348 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
    2180       81348 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
    2181             : INLINE GEN
    2182      459861 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
    2183      459861 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
    2184             : INLINE GEN
    2185        4760 : FqX_sqr(GEN x, GEN T, GEN p)
    2186        4760 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
    2187             : INLINE GEN
    2188        1428 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
    2189        1428 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
    2190             : INLINE GEN
    2191        2921 : FqX_div(GEN x, GEN y, GEN T, GEN p)
    2192        2921 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
    2193             : INLINE GEN
    2194        5605 : FqX_get_red(GEN S, GEN T, GEN p)
    2195        5605 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
    2196             : INLINE GEN
    2197       47875 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
    2198       47875 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
    2199             : INLINE GEN
    2200         329 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2201         329 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
    2202             : INLINE GEN
    2203        5957 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
    2204        5957 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
    2205             : INLINE GEN
    2206           0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
    2207           0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
    2208             : INLINE GEN
    2209      254060 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
    2210      254060 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
    2211             : INLINE GEN
    2212       75978 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
    2213       75978 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
    2214             : INLINE GEN
    2215        1974 : FqX_normalize(GEN z, GEN T, GEN p) { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
    2216             : INLINE GEN
    2217      268604 : FqX_deriv(GEN f, /*unused*/GEN T, GEN p) { (void)T; return FpXX_deriv(f, p); }
    2218             : INLINE GEN
    2219       71106 : FqX_factor(GEN f, GEN T, GEN p) { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
    2220             : INLINE GEN
    2221        5810 : FqX_roots(GEN f, GEN T, GEN p) { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
    2222             : 
    2223             : /*FqXQ*/
    2224             : INLINE GEN
    2225           0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2226           0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
    2227             : INLINE GEN
    2228           0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
    2229           0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
    2230             : INLINE GEN
    2231           0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
    2232           0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
    2233             : INLINE GEN
    2234           0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
    2235           0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
    2236             : INLINE GEN
    2237           0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
    2238           0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
    2239             : INLINE GEN
    2240       21018 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
    2241       21018 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
    2242             : INLINE GEN
    2243           0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
    2244           0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
    2245             : INLINE GEN
    2246           0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
    2247           0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
    2248             : 
    2249             : /*FpXQ*/
    2250             : INLINE GEN
    2251           0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2252           0 : { (void)T; return FpX_add(x,y,p); }
    2253             : INLINE GEN
    2254           0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
    2255           0 : { (void)T; return FpX_sub(x,y,p); }
    2256             : 
    2257             : /*Flxq*/
    2258             : INLINE GEN
    2259           0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2260           0 : { (void)T; return Flx_add(x,y,p); }
    2261             : INLINE GEN
    2262         110 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
    2263         110 : { (void)T; return Flx_sub(x,y,p); }
    2264             : 
    2265             : /* F2x */
    2266             : 
    2267             : INLINE ulong
    2268   104115814 : F2x_coeff(GEN x,long v)
    2269             : {
    2270   104115814 :    ulong u=(ulong)x[2+divsBIL(v)];
    2271   104115081 :    return (u>>remsBIL(v))&1UL;
    2272             : }
    2273             : 
    2274             : INLINE void
    2275     3549023 : F2x_clear(GEN x,long v)
    2276             : {
    2277     3549023 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2278     3549023 :    *u&=~(1UL<<remsBIL(v));
    2279     3549023 : }
    2280             : 
    2281             : INLINE void
    2282    39523227 : F2x_set(GEN x,long v)
    2283             : {
    2284    39523227 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2285    39524029 :    *u|=1UL<<remsBIL(v);
    2286    39527922 : }
    2287             : 
    2288             : INLINE void
    2289     1338546 : F2x_flip(GEN x,long v)
    2290             : {
    2291     1338546 :    ulong* u=(ulong*)&x[2+divsBIL(v)];
    2292     1338546 :    *u^=1UL<<remsBIL(v);
    2293     1338546 : }
    2294             : 
    2295             : /* F2v */
    2296             : 
    2297             : INLINE ulong
    2298   100256677 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
    2299             : 
    2300             : INLINE void
    2301     3549023 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
    2302             : 
    2303             : INLINE void
    2304    14985486 : F2v_set(GEN x,long v)   { F2x_set(x,v-1); }
    2305             : 
    2306             : INLINE void
    2307     1338546 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
    2308             : 
    2309             : /* F2m */
    2310             : 
    2311             : INLINE ulong
    2312    12584063 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
    2313             : 
    2314             : INLINE void
    2315           0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
    2316             : 
    2317             : INLINE void
    2318     1603119 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
    2319             : 
    2320             : INLINE void
    2321     1338546 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
    2322             : 
    2323             : /* ARITHMETIC */
    2324             : INLINE GEN
    2325         938 : matpascal(long n) { return matqpascal(n, NULL); }
    2326             : INLINE long
    2327     1964338 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
    2328             : INLINE long
    2329          14 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
    2330             : INLINE GEN
    2331     4637425 : sqrti(GEN x) { return sqrtremi(x,NULL); }
    2332             : INLINE GEN
    2333     7346352 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
    2334             : INLINE int
    2335       24114 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
    2336             : INLINE int
    2337       22855 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
    2338             : INLINE GEN
    2339           0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
    2340             : INLINE GEN
    2341           0 : gminsg(long s, GEN y) { return gmings(y,s); }
    2342             : INLINE GEN
    2343     5779583 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
    2344             : INLINE GEN
    2345      614535 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
    2346             : INLINE GEN
    2347      445710 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
    2348             : 
    2349             : /* x t_COMPLEX */
    2350             : INLINE GEN
    2351     8746351 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
    2352             : /* q t_QUAD */
    2353             : INLINE GEN
    2354         623 : quadnorm(GEN q)
    2355             : {
    2356         623 :   GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
    2357         623 :   GEN z, u = gel(q,3), v = gel(q,2);
    2358         623 :   if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
    2359             :   {
    2360         308 :     z = signe(b)? mulii(v, addii(u,v)): sqri(v);
    2361         308 :     return addii(z, mulii(c, sqri(u)));
    2362             :   }
    2363             :   else
    2364             :   {
    2365         315 :     z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
    2366         315 :     return gadd(z, gmul(c, gsqr(u)));
    2367             :   }
    2368             : }
    2369             : /* x a t_QUAD, return the attached discriminant */
    2370             : INLINE GEN
    2371         252 : quad_disc(GEN x)
    2372             : {
    2373         252 :   GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4;
    2374         252 :   if (is_pm1(b))
    2375             :   {
    2376         196 :     pari_sp av = avma; (void)new_chunk(lgefint(c) + 1);
    2377         196 :     c4 = shifti(c,2); avma = av; return subsi(1, c4);
    2378             :   }
    2379          56 :   c4 = shifti(c,2); togglesign_safe(&c4); return c4;
    2380             : }
    2381             : INLINE GEN
    2382     5997600 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
    2383             : INLINE GEN
    2384     5905760 : qfb_disc(GEN x) { return qfb_disc3(gel(x,1), gel(x,2), gel(x,3)); }
    2385             : 
    2386             : INLINE GEN
    2387     1418063 : sqrfrac(GEN x)
    2388             : {
    2389     1418063 :   GEN z = cgetg(3,t_FRAC);
    2390     1418063 :   gel(z,1) = sqri(gel(x,1));
    2391     1418063 :   gel(z,2) = sqri(gel(x,2)); return z;
    2392             : }
    2393             : 
    2394             : INLINE void
    2395     7483381 : normalize_frac(GEN z) {
    2396     7483381 :   if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
    2397     7483381 : }
    2398             : 
    2399             : INLINE GEN
    2400      211193 : powii(GEN x, GEN n)
    2401             : {
    2402      211193 :   long ln = lgefint(n);
    2403      211193 :   if (ln == 3) {
    2404             :     GEN z;
    2405      209282 :     if (signe(n) > 0) return powiu(x, n[2]);
    2406        2646 :     z = cgetg(3, t_FRAC);
    2407        2646 :     gel(z,1) = gen_1;
    2408        2646 :     gel(z,2) = powiu(x, n[2]);
    2409        2646 :     return z;
    2410             :   }
    2411        1911 :   if (ln == 2) return gen_1; /* rare */
    2412             :   /* should never happen */
    2413           0 :   return powgi(x, n); /* overflow unless x = 0, 1, -1 */
    2414             : }
    2415             : INLINE GEN
    2416         840 : powIs(long n) {
    2417         840 :   switch(n & 3)
    2418             :   {
    2419           7 :     case 1: return mkcomplex(gen_0,gen_1);
    2420         315 :     case 2: return gen_m1;
    2421         455 :     case 3: return mkcomplex(gen_0,gen_m1);
    2422             :   }
    2423          63 :   return gen_1;
    2424             : }
    2425             : 
    2426             : /*******************************************************************/
    2427             : /*                                                                 */
    2428             : /*                             ASSIGNMENTS                         */
    2429             : /*                                                                 */
    2430             : /*******************************************************************/
    2431           0 : INLINE void mpexpz(GEN x, GEN z)
    2432           0 : { pari_sp av = avma; gaffect(mpexp(x), z); avma = av; }
    2433           0 : INLINE void mplogz(GEN x, GEN z)
    2434           0 : { pari_sp av = avma; gaffect(mplog(x), z); avma = av; }
    2435           0 : INLINE void mpcosz(GEN x, GEN z)
    2436           0 : { pari_sp av = avma; gaffect(mpcos(x), z); avma = av; }
    2437           0 : INLINE void mpsinz(GEN x, GEN z)
    2438           0 : { pari_sp av = avma; gaffect(mpsin(x), z); avma = av; }
    2439           0 : INLINE void gnegz(GEN x, GEN z)
    2440           0 : { pari_sp av = avma; gaffect(gneg(x), z); avma = av; }
    2441           0 : INLINE void gabsz(GEN x, long prec, GEN z)
    2442           0 : { pari_sp av = avma; gaffect(gabs(x,prec), z); avma = av; }
    2443       23318 : INLINE void gaddz(GEN x, GEN y, GEN z)
    2444       23318 : { pari_sp av = avma; gaffect(gadd(x,y), z); avma = av; }
    2445           0 : INLINE void gsubz(GEN x, GEN y, GEN z)
    2446           0 : { pari_sp av = avma; gaffect(gsub(x,y), z); avma = av; }
    2447       11673 : INLINE void gmulz(GEN x, GEN y, GEN z)
    2448       11673 : { pari_sp av = avma; gaffect(gmul(x,y), z); avma = av; }
    2449           0 : INLINE void gdivz(GEN x, GEN y, GEN z)
    2450           0 : { pari_sp av = avma; gaffect(gdiv(x,y), z); avma = av; }
    2451           0 : INLINE void gdiventz(GEN x, GEN y, GEN z)
    2452           0 : { pari_sp av = avma; gaffect(gdivent(x,y), z); avma = av; }
    2453           0 : INLINE void gmodz(GEN x, GEN y, GEN z)
    2454           0 : { pari_sp av = avma; gaffect(gmod(x,y), z); avma = av; }
    2455           0 : INLINE void gmul2nz(GEN x, long s, GEN z)
    2456           0 : { pari_sp av = avma; gaffect(gmul2n(x,s), z); avma = av; }
    2457           0 : INLINE void gshiftz(GEN x, long s, GEN z)
    2458           0 : { pari_sp av = avma; gaffect(gshift(x,s), z); avma = av; }
    2459             : 
    2460             : /*******************************************************************/
    2461             : /*                                                                 */
    2462             : /*                       ELLIPTIC CURVES                           */
    2463             : /*                                                                 */
    2464             : /*******************************************************************/
    2465     5004102 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
    2466     3296728 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
    2467     4476827 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
    2468     4094469 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
    2469     4668322 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
    2470     3948768 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
    2471     1009191 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
    2472     1613025 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
    2473     1457050 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
    2474     6278104 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
    2475     8309679 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
    2476     9738484 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
    2477     1174558 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
    2478     8080909 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
    2479     1510136 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
    2480      652432 : INLINE GEN ellff_get_a4a6(GEN x)  { return gmael(x, 15, 2); }
    2481         917 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
    2482         287 : INLINE long ellQp_get_prec(GEN E) { GEN z = ellQp_get_zero(E); return valp(z); }
    2483         623 : INLINE GEN ellQp_get_p(GEN E) { GEN z = ellQp_get_zero(E); return gel(z,2); }
    2484       36946 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
    2485        1409 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
    2486      708337 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
    2487          42 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
    2488             : 
    2489    34090705 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
    2490      927157 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
    2491             : 
    2492             : /*******************************************************************/
    2493             : /*                                                                 */
    2494             : /*                    ALGEBRAIC NUMBER THEORY                      */
    2495             : /*                                                                 */
    2496             : /*******************************************************************/
    2497     6194087 : INLINE GEN modpr_get_pr(GEN x)  { return gel(x,3); }
    2498      602377 : INLINE GEN modpr_get_p(GEN x)  { return pr_get_p(modpr_get_pr(x)); }
    2499     1771261 : INLINE GEN modpr_get_T(GEN x)  { return lg(x) == 4? NULL: gel(x,4); }
    2500             : 
    2501    14742025 : INLINE GEN pr_get_p(GEN pr)  { return gel(pr,1); }
    2502     2122428 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
    2503             : /* .[2] instead of itos works: e and f are small positive integers */
    2504     2052595 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
    2505     6032563 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
    2506     5783061 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
    2507             : INLINE int
    2508      743622 : pr_is_inert(GEN P) { return pr_get_f(P) == lg(pr_get_gen(P))-1; }
    2509             : INLINE GEN
    2510      309724 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
    2511             : INLINE ulong
    2512       31731 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
    2513             : 
    2514             : /* assume nf a genuine nf */
    2515             : INLINE long
    2516        3002 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
    2517             : INLINE GEN
    2518     6275048 : nf_get_pol(GEN nf) { return gel(nf,1); }
    2519             : INLINE long
    2520     2974987 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
    2521             : INLINE long
    2522     1105087 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
    2523             : INLINE long
    2524        1057 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
    2525             : INLINE GEN
    2526       22686 : nf_get_disc(GEN nf) { return gel(nf,3); }
    2527             : INLINE GEN
    2528      621981 : nf_get_index(GEN nf) { return gel(nf,4); }
    2529             : INLINE GEN
    2530      997013 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
    2531             : INLINE GEN
    2532       45385 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
    2533             : INLINE GEN
    2534      394526 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
    2535             : INLINE GEN
    2536         819 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
    2537             : INLINE GEN
    2538        1729 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
    2539             : INLINE GEN
    2540          35 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
    2541             : INLINE GEN
    2542       98917 : nf_get_roots(GEN nf) { return gel(nf,6); }
    2543             : INLINE GEN
    2544     6602931 : nf_get_zk(GEN nf) { return gel(nf,7); }
    2545             : INLINE GEN
    2546     1280576 : nf_get_invzk(GEN nf) { return gel(nf,8); }
    2547             : INLINE void
    2548        6209 : nf_get_sign(GEN nf, long *r1, long *r2)
    2549             : {
    2550        6209 :   GEN x = gel(nf,2);
    2551        6209 :   *r1 = itou(gel(x,1));
    2552        6209 :   *r2 = itou(gel(x,2));
    2553        6209 : }
    2554             : 
    2555             : INLINE GEN
    2556        9366 : abgrp_get_no(GEN x) { return gel(x,1); }
    2557             : INLINE GEN
    2558     1128462 : abgrp_get_cyc(GEN x) { return gel(x,2); }
    2559             : INLINE GEN
    2560      280794 : abgrp_get_gen(GEN x) { return gel(x,3); }
    2561             : INLINE GEN
    2562     1193650 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
    2563             : INLINE GEN
    2564      573656 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
    2565             : INLINE GEN
    2566        1092 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
    2567             : INLINE GEN
    2568      300240 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
    2569             : INLINE GEN
    2570      271995 : bnf_get_gen(GEN bnf)  { return abgrp_get_gen(bnf_get_clgp(bnf)); }
    2571             : INLINE GEN
    2572         853 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
    2573             : INLINE GEN
    2574      432870 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
    2575             : INLINE GEN
    2576        2493 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
    2577             : INLINE long
    2578        7574 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
    2579             : INLINE GEN
    2580        1806 : bnf_get_fu(GEN bnf) {
    2581        1806 :   GEN fu = bnf_get_fu_nocheck(bnf);
    2582        1806 :   if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
    2583        1806 :   return fu;
    2584             : }
    2585             : INLINE GEN
    2586        3767 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
    2587             : 
    2588             : INLINE GEN
    2589      401329 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
    2590             : INLINE GEN
    2591      204924 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
    2592             : INLINE GEN
    2593       11382 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
    2594             : INLINE GEN
    2595       13195 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
    2596             : INLINE GEN
    2597      233708 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
    2598             : INLINE GEN
    2599        8274 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
    2600             : INLINE GEN
    2601      223446 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
    2602             : INLINE GEN
    2603          70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
    2604             : INLINE GEN
    2605        1141 : bnr_get_gen(GEN bnr) {
    2606        1141 :   GEN G = bnr_get_clgp(bnr);
    2607        1141 :   if (lg(G) !=  4)
    2608           0 :     pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
    2609        1141 :   return gel(G,3);
    2610             : }
    2611             : 
    2612             : INLINE GEN
    2613      234135 : bid_get_mod(GEN bid) { return gel(bid,1); }
    2614             : INLINE GEN
    2615       28553 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
    2616             : INLINE GEN
    2617         448 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
    2618             : INLINE GEN
    2619      611195 : bid_get_grp(GEN bid) { return gel(bid,2); }
    2620             : INLINE GEN
    2621      213373 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
    2622             : INLINE GEN
    2623      205981 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
    2624             : INLINE GEN
    2625      206051 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
    2626             : INLINE GEN
    2627        6335 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
    2628             : INLINE GEN
    2629      209684 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
    2630             : INLINE GEN
    2631           0 : bid_get_ind(GEN bid) { return gmael(bid,4,3); }
    2632             : INLINE GEN
    2633      411605 : bid_get_U(GEN bid) { return gel(bid,5); }
    2634             : INLINE GEN
    2635           0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
    2636             : INLINE GEN
    2637      603362 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
    2638             : INLINE GEN
    2639           0 : bid_get_gen_nocheck(GEN bid)  { return abgrp_get_gen(bid_get_grp(bid)); }
    2640             : INLINE GEN
    2641        7238 : bid_get_gen(GEN bid) {
    2642        7238 :   GEN G = bid_get_grp(bid);
    2643        7238 :   if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
    2644        7238 :   return abgrp_get_gen(G);
    2645             : }
    2646             : INLINE GEN
    2647        2247 : gal_get_pol(GEN gal) { return gel(gal,1); }
    2648             : INLINE GEN
    2649         994 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
    2650             : INLINE GEN
    2651          63 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
    2652             : INLINE GEN
    2653        1820 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
    2654             : INLINE GEN
    2655        2198 : gal_get_roots(GEN gal) { return gel(gal,3); }
    2656             : INLINE GEN
    2657        1827 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
    2658             : INLINE GEN
    2659        1827 : gal_get_den(GEN gal) { return gel(gal,5); }
    2660             : INLINE GEN
    2661        3836 : gal_get_group(GEN gal) { return gel(gal,6); }
    2662             : INLINE GEN
    2663        1232 : gal_get_gen(GEN gal) { return gel(gal,7); }
    2664             : INLINE GEN
    2665         777 : gal_get_orders(GEN gal) { return gel(gal,8); }
    2666             : 
    2667             : /* assume rnf a genuine rnf */
    2668             : INLINE long
    2669      310765 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
    2670             : INLINE long
    2671       14742 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
    2672             : INLINE long
    2673      156254 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
    2674             : INLINE GEN
    2675         427 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
    2676             : INLINE GEN
    2677         448 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
    2678             : INLINE GEN
    2679         448 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
    2680             : INLINE GEN
    2681      123410 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
    2682             : INLINE void
    2683        1687 : rnf_get_nfzk(GEN rnf, GEN *b, GEN *cb) {*b=gmael(rnf,2,1); *cb=gmael(rnf,2,2);}
    2684             : INLINE GEN
    2685       93443 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
    2686             : INLINE GEN
    2687      462133 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
    2688             : INLINE GEN
    2689          63 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
    2690             : INLINE GEN
    2691         105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
    2692             : INLINE long
    2693           0 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
    2694             : INLINE GEN
    2695      106295 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
    2696             : INLINE long
    2697         483 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
    2698             : INLINE GEN
    2699        2436 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
    2700             : INLINE GEN
    2701       33943 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
    2702             : INLINE GEN
    2703        1190 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
    2704             : 
    2705             : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
    2706             :  * T2 matrix. Reduce I wrt G */
    2707             : INLINE GEN
    2708        5068 : idealpseudored(GEN I, GEN G)
    2709        5068 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
    2710             : 
    2711             : /* I integral (not necessarily HNF), G ZM, rounded Cholesky form of a weighted
    2712             :  * T2 matrix. Return m in I with T2(m) small */
    2713             : INLINE GEN
    2714      170028 : idealpseudomin(GEN I, GEN G)
    2715             : {
    2716      170028 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2717      170028 :   return ZM_ZC_mul(I, gel(u,1));
    2718             : }
    2719             : /* I, G as in idealpseudomin. Return an irrational m in I with T2(m) small */
    2720             : INLINE GEN
    2721      256785 : idealpseudomin_nonscalar(GEN I, GEN G)
    2722             : {
    2723      256785 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2724      256785 :   GEN m = ZM_ZC_mul(I, gel(u,1));
    2725      256785 :   if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
    2726      256785 :   return m;
    2727             : }
    2728             : 
    2729             : INLINE GEN
    2730           0 : idealred_elt(GEN nf, GEN I) {
    2731           0 :   pari_sp av = avma;
    2732           0 :   GEN u = idealpseudomin(I, nf_get_roundG(nf));
    2733           0 :   return gerepileupto(av, u);
    2734             : }
    2735             : INLINE GEN
    2736      175028 : idealred(GEN nf, GEN I) { return idealred0(nf, I, NULL); }
    2737             : 
    2738             : INLINE GEN
    2739        1197 : idealchineseinit(GEN nf, GEN x)
    2740        1197 : { return idealchinese(nf,x,NULL); }
    2741             : 
    2742             : /*******************************************************************/
    2743             : /*                                                                 */
    2744             : /*                              CLOSURES                           */
    2745             : /*                                                                 */
    2746             : /*******************************************************************/
    2747   186253587 : INLINE long closure_arity(GEN C)          { return ((ulong)C[1])&ARITYBITS; }
    2748    34290166 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
    2749   151743085 : INLINE const char *closure_codestr(GEN C)  { return GSTR(gel(C,2))-1; }
    2750           0 : INLINE GEN closure_get_code(GEN C)  { return gel(C,2); }
    2751   151728817 : INLINE GEN closure_get_oper(GEN C)  { return gel(C,3); }
    2752   151718473 : INLINE GEN closure_get_data(GEN C)  { return gel(C,4); }
    2753       16407 : INLINE GEN closure_get_dbg(GEN C)   { return gel(C,5); }
    2754       34690 : INLINE GEN closure_get_text(GEN C)  { return gel(C,6); }
    2755      431985 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
    2756             : 
    2757             : /*******************************************************************/
    2758             : /*                                                                 */
    2759             : /*                               ERRORS                            */
    2760             : /*                                                                 */
    2761             : /*******************************************************************/
    2762             : INLINE long
    2763       46186 : err_get_num(GEN e) { return e[1]; }
    2764             : INLINE GEN
    2765          70 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
    2766             : 
    2767             : INLINE void
    2768          14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
    2769             : INLINE void
    2770           7 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
    2771             : INLINE void
    2772          63 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
    2773             : INLINE void
    2774         918 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
    2775             : INLINE void
    2776           0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
    2777             : INLINE void
    2778          21 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
    2779             : INLINE void
    2780         252 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
    2781             : INLINE void
    2782         578 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
    2783             : INLINE void
    2784          21 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
    2785             : INLINE void
    2786        5096 : 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); }
    2787             : INLINE void
    2788         224 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
    2789             : INLINE void
    2790           0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
    2791             : INLINE void
    2792         287 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
    2793             : INLINE void
    2794          63 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
    2795             : INLINE void
    2796         225 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
    2797             : INLINE void
    2798           0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
    2799             : INLINE void
    2800          56 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
    2801             : INLINE void
    2802         728 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
    2803             : INLINE void
    2804          35 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
    2805             : INLINE void
    2806          77 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
    2807             : INLINE void
    2808       24194 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
    2809             : INLINE void
    2810        3010 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
    2811             : INLINE void
    2812         196 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
    2813             : INLINE void
    2814         119 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
    2815         119 : { pari_err(e_PRIORITY, f,x,op,v); }

Generated by: LCOV version 1.11