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 - kernel/none - level1.h (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 22303-eb3e11d) Lines: 558 713 78.3 %
Date: 2018-04-20 06:16:30 Functions: 198 261 75.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #line 2 "../src/kernel/none/level1.h"
       2             : /* Copyright (C) 2000  The PARI group.
       3             : 
       4             : This file is part of the PARI/GP package.
       5             : 
       6             : PARI/GP is free software; you can redistribute it and/or modify it under the
       7             : terms of the GNU General Public License as published by the Free Software
       8             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /* This file defines "level 1" kernel functions.
      16             :  * These functions can be inline; they are also defined externally in
      17             :  * mpinl.c, which includes this file and never needs to be changed */
      18             : 
      19             : INLINE long
      20 42534378227 : evallg(long x)
      21             : {
      22 42534378227 :   if (x & ~LGBITS) pari_err_OVERFLOW("lg()");
      23 42531062014 :   return _evallg(x);
      24             : }
      25             : INLINE long
      26    14520981 : evalvalp(long x)
      27             : {
      28    14520981 :   long v = _evalvalp(x);
      29    14520981 :   if (v & ~VALPBITS) pari_err_OVERFLOW("valp()");
      30    14520976 :   return v;
      31             : }
      32             : INLINE long
      33  5507641133 : evalexpo(long x)
      34             : {
      35  5507641133 :   long v = _evalexpo(x);
      36  5507641133 :   if (v & ~EXPOBITS) pari_err_OVERFLOW("expo()");
      37  5507641133 :   return v;
      38             : }
      39             : INLINE long
      40     1905928 : evalprecp(long x)
      41             : {
      42     1905928 :   long v = _evalprecp(x);
      43     1905928 :   if (x & ~((1UL<<(BITS_IN_LONG-VALPnumBITS))-1)) pari_err_OVERFLOW("precp()");
      44     1905926 :   return v;
      45             : }
      46             : 
      47             : INLINE int
      48   145644581 : varncmp(long x, long y)
      49             : {
      50   145644581 :   if (varpriority[x] < varpriority[y]) return  1;
      51   113185604 :   if (varpriority[x] > varpriority[y]) return -1;
      52    61426359 :   return 0;
      53             : }
      54             : INLINE long
      55           0 : varnmin(long x, long y)
      56           0 : { return (varpriority[x] <= varpriority[y])? x: y; }
      57             : INLINE long
      58         196 : varnmax(long x, long y)
      59         196 : { return (varpriority[x] >= varpriority[y])? x: y; }
      60             : 
      61             : /* Inhibit some area gerepile-wise: declare it to be a non recursive
      62             :  * type, of length l. Thus gerepile won't inspect the zone, just copy it.
      63             :  * For the following situation:
      64             :  *   z = cgetg(t,a); av = avma; garbage(); ltop = avma;
      65             :  *   for (i=1; i<HUGE; i++) gel(z,i) = blah();
      66             :  *   stackdummy(av,ltop);
      67             :  * loses (av-ltop) words but save a costly gerepile. */
      68             : INLINE void
      69  1736543228 : stackdummy(pari_sp av, pari_sp ltop) {
      70  1736543228 :   long l = ((GEN)av) - ((GEN)ltop);
      71  1736543228 :   if (l > 0) {
      72   599258089 :     GEN z = (GEN)ltop;
      73   599258089 :     z[0] = evaltyp(t_VECSMALL) | evallg(l);
      74             : #ifdef DEBUG
      75             :     { long i; for (i = 1; i < l; i++) z[i] = 0; }
      76             : #endif
      77             :   }
      78  1735962417 : }
      79             : INLINE void
      80    58217268 : fixlg(GEN x, long ly) {
      81    58217268 :   long lx = lg(x), l = lx - ly;
      82    58217268 :   if (l > 0)
      83             :   { /* stackdummy(x+lx, x+ly) */
      84    42839041 :     GEN z = x + ly;
      85    42839041 :     z[0] = evaltyp(t_VECSMALL) | evallg(l);
      86    42839021 :     setlg(x, ly);
      87             : #ifdef DEBUG
      88             :     { long i; for (i = 1; i < l; i++) z[i] = 0; }
      89             : #endif
      90             :   }
      91    58217245 : }
      92             : /* update lg(z) before affrr(y, z)  [ to cater for precision loss ]*/
      93             : INLINE void
      94    15379604 : affrr_fixlg(GEN y, GEN z) { fixlg(z, lg(y)); affrr(y, z); }
      95             : 
      96             : /*******************************************************************/
      97             : /*                                                                 */
      98             : /*                       ALLOCATE ON STACK                         */
      99             : /*                                                                 */
     100             : /*******************************************************************/
     101             : INLINE GEN
     102 40077554666 : new_chunk(size_t x) /* x is a number of longs */
     103             : {
     104 40077554666 :   GEN z = ((GEN) avma) - x;
     105             :   CHECK_CTRLC
     106 40077554666 :   if (x > (avma-pari_mainstack->bot) / sizeof(long))
     107           7 :     new_chunk_resize(x);
     108 40090759260 :   avma = (pari_sp)z;
     109             : #ifdef MEMSTEP
     110             :   if (DEBUGMEM>1 && pari_mainstack->memused != DISABLE_MEMUSED) {
     111             :     long d = (long)pari_mainstack->memused - (long)z;
     112             :     if (labs(d) > 4*MEMSTEP)
     113             :     {
     114             :       pari_mainstack->memused = (pari_sp)z;
     115             :       err_printf("...%4.0lf Mbytes used\n",
     116             :                 (pari_mainstack->top-pari_mainstack->memused)/1048576.);
     117             :     }
     118             :   }
     119             : #endif
     120 40090759260 :   return z;
     121             : }
     122             : 
     123             : INLINE char *
     124     4097139 : stack_malloc(size_t N)
     125             : {
     126     4097139 :   long n = nchar2nlong(N);
     127     4097139 :   return (char*)new_chunk(n);
     128             : }
     129             : 
     130             : INLINE char *
     131     1017979 : stack_malloc_align(size_t N, long k)
     132             : {
     133     1017979 :   ulong d = ((ulong)avma) % k;
     134     1017979 :   if (d) (void)new_chunk(d/sizeof(long));
     135     1017979 :   return (char*) new_chunk(nchar2nlong(N));
     136             : }
     137             : 
     138             : INLINE char *
     139       36663 : stack_calloc(size_t N)
     140             : {
     141       36663 :   char *p = stack_malloc(N);
     142       36663 :   memset(p, 0, N); return p;
     143             : }
     144             : 
     145             : /* cgetg(lg(x), typ(x)), set *lx. Implicit unsetisclone() */
     146             : INLINE GEN
     147   443430444 : cgetg_copy(GEN x, long *plx) {
     148             :   GEN y;
     149   443430444 :   *plx = lg(x); y = new_chunk((size_t)*plx);
     150   443429665 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     151             : }
     152             : INLINE GEN
     153      123410 : cgetg_block(long x, long y)
     154             : {
     155      123410 :   GEN z = newblock((size_t)x);
     156      123295 :   z[0] = CLONEBIT | evaltyp(y) | evallg(x);
     157      123337 :   return z;
     158             : }
     159             : INLINE GEN
     160  5301540742 : cgetg(long x, long y)
     161             : {
     162  5301540742 :   GEN z = new_chunk((size_t)x);
     163  5300749636 :   z[0] = evaltyp(y) | evallg(x);
     164  5297233539 :   return z;
     165             : }
     166             : INLINE GEN
     167 14084195365 : cgeti(long x)
     168             : {
     169 14084195365 :   GEN z = new_chunk((size_t)x);
     170 14066627328 :   z[0] = evaltyp(t_INT) | evallg(x);
     171 14053313237 :   return z;
     172             : }
     173             : INLINE GEN
     174  8192250884 : cgetipos(long x)
     175             : {
     176  8192250884 :   GEN z = cgeti(x);
     177  8191569520 :   z[1] = evalsigne(1) | evallgefint(x);
     178  8191569520 :   return z;
     179             : }
     180             : INLINE GEN
     181   204305584 : cgetineg(long x)
     182             : {
     183   204305584 :   GEN z = cgeti(x);
     184   204305567 :   z[1] = evalsigne(-1) | evallgefint(x);
     185   204305567 :   return z;
     186             : }
     187             : INLINE GEN
     188        2855 : cgetr_block(long x)
     189             : {
     190        2855 :   GEN z = newblock((size_t)x);
     191        2855 :   z[0] = CLONEBIT | evaltyp(t_REAL) | evallg(x);
     192        2855 :   return z;
     193             : }
     194             : INLINE GEN
     195  5706552685 : cgetr(long x)
     196             : {
     197  5706552685 :   GEN z = new_chunk((size_t)x);
     198  5706552685 :   z[0] = evaltyp(t_REAL) | evallg(x);
     199  5706552685 :   return z;
     200             : }
     201             : 
     202             : /*******************************************************************/
     203             : /*                                                                 */
     204             : /*                     COPY, NEGATION, ABSOLUTE VALUE              */
     205             : /*                                                                 */
     206             : /*******************************************************************/
     207             : /* cannot do memcpy because sometimes x and y overlap */
     208             : INLINE GEN
     209  1460958804 : leafcopy(GEN x)
     210             : {
     211  1460958804 :   register long lx = lg(x);
     212  1460958804 :   GEN y = new_chunk(lx); /* can't use cgetg_copy, in case x,y overlap */
     213  1461516279 :   while (--lx > 0) y[lx] = x[lx];
     214  1461516279 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     215             : }
     216             : INLINE GEN
     217  4427085594 : icopy(GEN x)
     218             : {
     219  4427085594 :   long i = lgefint(x), lx = i;
     220  4427085594 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     221  4430628067 :   while (--i > 0) y[i] = x[i];
     222  4430628067 :   y[0] = evaltyp(t_INT) | evallg(lx);
     223  4429179782 :   return y;
     224             : }
     225             : INLINE GEN
     226    26376391 : icopyspec(GEN x, long nx)
     227             : {
     228    26376391 :   long i = nx+2, lx = i;
     229    26376391 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     230    26376339 :   x -= 2; while (--i >= 2) y[i] = x[i];
     231    26376339 :   y[1] = evalsigne(1) | evallgefint(lx);
     232    26376339 :   y[0] = evaltyp(t_INT) | evallg(lx);
     233    26376330 :   return y;
     234             : }
     235   228356410 : INLINE GEN rcopy(GEN x) { return leafcopy(x); }
     236     3758576 : INLINE GEN mpcopy(GEN x) { return leafcopy(x); }
     237             : 
     238             : INLINE GEN
     239   368472173 : mpabs(GEN x) { GEN y = leafcopy(x); setabssign(y); return y; }
     240             : INLINE GEN
     241       11522 : mpabs_shallow(GEN x) { return signe(x) < 0? mpabs(x): x; }
     242   327529091 : INLINE GEN absi(GEN x) { return mpabs(x); }
     243      156051 : INLINE GEN absi_shallow(GEN x) { return signe(x) < 0? negi(x): x; }
     244    25402779 : INLINE GEN absr(GEN x) { return mpabs(x); }
     245             : 
     246             : INLINE GEN
     247   342000065 : mpneg(GEN x) { GEN y = leafcopy(x); togglesign(y); return y; }
     248   236853861 : INLINE GEN negi(GEN x) { return mpneg(x); }
     249     1199122 : INLINE GEN negr(GEN x) { return mpneg(x); }
     250             : 
     251             : /* negate in place */
     252             : INLINE void
     253   736910249 : togglesign(GEN x) { if (x[1] & SIGNBITS) { x[1] ^= HIGHBIT; } }
     254             : INLINE void
     255   403254325 : setabssign(GEN x) { x[1] &= ~HIGHBIT; }
     256             : /* negate in place, except universal constants */
     257             : INLINE void
     258    48399292 : togglesign_safe(GEN *px)
     259             : {
     260    48399292 :   switch(*px - gen_1) /* gen_1, gen_2, gen_m1, gen_m2 */
     261             :   {
     262      647260 :     case 0: *px = gen_m1; break;
     263           0 :     case 3: *px = gen_m2;  break;
     264      145876 :     case 6: *px = gen_1; break;
     265           0 :     case 9: *px = gen_2;  break;
     266    47606156 :     default: togglesign(*px);
     267             :   }
     268    48399292 : }
     269             : /* setsigne(y, signe(x)) */
     270             : INLINE void
     271           0 : affectsign(GEN x, GEN y)
     272             : {
     273           0 :   y[1] = (x[1] & SIGNBITS) | (y[1] & ~SIGNBITS);
     274           0 : }
     275             : /* copies sign in place, except for universal constants */
     276             : INLINE void
     277     5300926 : affectsign_safe(GEN x, GEN *py)
     278             : {
     279     5300926 :   if (((*py)[1] ^ x[1]) & HIGHBIT) togglesign_safe(py);
     280     5300926 : }
     281             : /*******************************************************************/
     282             : /*                                                                 */
     283             : /*                     GEN -> LONG, LONG -> GEN                    */
     284             : /*                                                                 */
     285             : /*******************************************************************/
     286             : /* assume x != 0, return -x as a t_INT */
     287             : INLINE GEN
     288   204144770 : utoineg(ulong x) { GEN y = cgetineg(3); y[2] = x; return y; }
     289             : /* assume x != 0, return utoi(x) */
     290             : INLINE GEN
     291  7214433346 : utoipos(ulong x) { GEN y = cgetipos(3); y[2] = x; return y; }
     292             : INLINE GEN
     293  5948198859 : utoi(ulong x) { return x? utoipos(x): gen_0; }
     294             : INLINE GEN
     295   408272854 : stoi(long x)
     296             : {
     297   408272854 :   if (!x) return gen_0;
     298   266061949 :   return x > 0? utoipos((ulong)x): utoineg((ulong)-x);
     299             : }
     300             : 
     301             : /* x 2^BIL + y */
     302             : INLINE GEN
     303  4323163904 : uutoi(ulong x, ulong y)
     304             : {
     305             :   GEN z;
     306  4323163904 :   if (!x) return utoi(y);
     307   457135452 :   z = cgetipos(4);
     308   456815510 :   *int_W_lg(z, 1, 4) = x;
     309   456815510 :   *int_W_lg(z, 0, 4) = y; return z;
     310             : }
     311             : /* - (x 2^BIL + y) */
     312             : INLINE GEN
     313       76646 : uutoineg(ulong x, ulong y)
     314             : {
     315             :   GEN z;
     316       76646 :   if (!x) return y? utoineg(y): gen_0;
     317           0 :   z = cgetineg(4);
     318           0 :   *int_W_lg(z, 1, 4) = x;
     319           0 :   *int_W_lg(z, 0, 4) = y; return z;
     320             : }
     321             : 
     322             : INLINE long
     323   300456310 : itos(GEN x)
     324             : {
     325   300456310 :   long s = signe(x);
     326             :   long u;
     327             : 
     328   300456310 :   if (!s) return 0;
     329   286998874 :   u = x[2];
     330   286998874 :   if (lgefint(x) > 3 || u < 0)
     331          19 :     pari_err_OVERFLOW("t_INT-->long assignment");
     332   286998919 :   return (s>0) ? u : -u;
     333             : }
     334             : /* as itos, but return 0 if too large. Cf is_bigint */
     335             : INLINE long
     336     9648088 : itos_or_0(GEN x) {
     337             :   long n;
     338     9648088 :   if (lgefint(x) != 3 || (n = x[2]) & HIGHBIT) return 0;
     339     9628025 :   return signe(x) > 0? n: -n;
     340             : }
     341             : INLINE ulong
     342    54304654 : itou(GEN x)
     343             : {
     344    54304654 :   switch(lgefint(x)) {
     345     8208357 :     case 2: return 0;
     346    46096297 :     case 3: return x[2];
     347             :     default:
     348           0 :       pari_err_OVERFLOW("t_INT-->ulong assignment");
     349             :       return 0; /* LCOV_EXCL_LINE */
     350             :   }
     351             : }
     352             : 
     353             : /* as itou, but return 0 if too large. Cf is_bigint */
     354             : INLINE ulong
     355     4573109 : itou_or_0(GEN x) {
     356     4573109 :   if (lgefint(x) != 3) return 0;
     357     4562409 :   return (ulong)x[2];
     358             : }
     359             : 
     360             : INLINE ulong
     361     4863317 : umuluu_or_0(ulong x, ulong y)
     362             : {
     363             :   ulong z;
     364             :   LOCAL_HIREMAINDER;
     365     4863317 :   z = mulll(x, y);
     366     4863317 :   return hiremainder? 0: z;
     367             : }
     368             : /* return x*y if <= n, else 0. Beware overflow */
     369             : INLINE ulong
     370     2002814 : umuluu_le(ulong x, ulong y, ulong n)
     371             : {
     372             :   ulong z;
     373             :   LOCAL_HIREMAINDER;
     374     2002814 :   z = mulll(x, y);
     375     2002814 :   return (hiremainder || z > n)? 0: z;
     376             : }
     377             : 
     378             : INLINE GEN
     379   112036081 : real_0_bit(long bitprec) { GEN x=cgetr(2); x[1]=evalexpo(bitprec); return x; }
     380             : INLINE GEN
     381    26507371 : real_0(long prec) { return real_0_bit(-prec2nbits(prec)); }
     382             : INLINE GEN
     383      589073 : real_1_bit(long bit) { return real_1(nbits2prec(bit)); }
     384             : INLINE GEN
     385    29368070 : real_1(long prec) {
     386    29368070 :   GEN x = cgetr(prec);
     387             :   long i;
     388    29368070 :   x[1] = evalsigne(1) | _evalexpo(0);
     389    29368070 :   x[2] = (long)HIGHBIT; for (i=3; i<prec; i++) x[i] = 0;
     390    29368070 :   return x;
     391             : }
     392             : INLINE GEN
     393          91 : real_m1(long prec) {
     394          91 :   GEN x = cgetr(prec);
     395             :   long i;
     396          91 :   x[1] = evalsigne(-1) | _evalexpo(0);
     397          91 :   x[2] = (long)HIGHBIT; for (i=3; i<prec; i++) x[i] = 0;
     398          91 :   return x;
     399             : }
     400             : 
     401             : /* 2.^n */
     402             : INLINE GEN
     403       93367 : real2n(long n, long prec) { GEN z = real_1(prec); setexpo(z, n); return z; }
     404             : INLINE GEN
     405           0 : real_m2n(long n, long prec) { GEN z = real_m1(prec); setexpo(z, n); return z; }
     406             : INLINE GEN
     407   145378065 : stor(long s, long prec) { GEN z = cgetr(prec); affsr(s,z); return z; }
     408             : INLINE GEN
     409     5678038 : utor(ulong s, long prec){ GEN z = cgetr(prec); affur(s,z); return z; }
     410             : INLINE GEN
     411   162875648 : itor(GEN x, long prec) { GEN z = cgetr(prec); affir(x,z); return z; }
     412             : INLINE GEN
     413    84008296 : rtor(GEN x, long prec) { GEN z = cgetr(prec); affrr(x,z); return z; }
     414             : 
     415    11670248 : INLINE ulong int_bit(GEN x, long n)
     416             : {
     417    11670248 :   long r, q = dvmdsBIL(n, &r);
     418    11671882 :   return q < lgefint(x)-2?((ulong)*int_W(x,q) >> r) & 1UL:0;
     419             : }
     420             : 
     421             : /*******************************************************************/
     422             : /*                                                                 */
     423             : /*                           COMPARISON                            */
     424             : /*                                                                 */
     425             : /*******************************************************************/
     426             : INLINE int
     427      568898 : cmpir(GEN x, GEN y)
     428             : {
     429             :   pari_sp av;
     430             :   GEN z;
     431             : 
     432      568898 :   if (!signe(x)) return -signe(y);
     433      193474 :   if (!signe(y))
     434             :   {
     435        3536 :     if (expo(y) >= expi(x)) return 0;
     436        3508 :     return signe(x);
     437             :   }
     438      189938 :   av=avma; z = itor(x, realprec(y)); avma=av;
     439      189938 :   return cmprr(z,y); /* cmprr does no memory adjustment */
     440             : }
     441             : INLINE int
     442      317555 : cmpri(GEN x, GEN y) { return -cmpir(y,x); }
     443             : INLINE int
     444       62327 : cmpsr(long x, GEN y)
     445             : {
     446             :   pari_sp av;
     447             :   GEN z;
     448             : 
     449       62327 :   if (!x) return -signe(y);
     450       62327 :   av=avma; z = stor(x, LOWDEFAULTPREC); avma=av;
     451       62327 :   return cmprr(z,y);
     452             : }
     453             : INLINE int
     454       38668 : cmprs(GEN x, long y) { return -cmpsr(y,x); }
     455             : /* compare x and |y| */
     456             : INLINE int
     457    26112760 : abscmpui(ulong x, GEN y)
     458             : {
     459    26112760 :   long l = lgefint(y);
     460             :   ulong p;
     461             : 
     462    26112760 :   if (!x) return (l > 2)? -1: 0;
     463    26112746 :   if (l == 2) return 1;
     464    25917202 :   if (l > 3) return -1;
     465    25905275 :   p = y[2]; if (p == x) return 0;
     466    25502040 :   return p < x ? 1 : -1;
     467             : }
     468             : INLINE int
     469    26056047 : abscmpiu(GEN x, ulong y) { return -abscmpui(y,x); }
     470             : INLINE int
     471     4438081 : cmpsi(long x, GEN y)
     472             : {
     473             :   ulong p;
     474             : 
     475     4438081 :   if (!x) return -signe(y);
     476             : 
     477     4437332 :   if (x > 0)
     478             :   {
     479     4436905 :     if (signe(y)<=0) return 1;
     480     4436709 :     if (lgefint(y)>3) return -1;
     481     4422512 :     p = y[2]; if (p == (ulong)x) return 0;
     482     4361745 :     return p < (ulong)x ? 1 : -1;
     483             :   }
     484             : 
     485         427 :   if (signe(y)>=0) return -1;
     486          63 :   if (lgefint(y)>3) return 1;
     487          63 :   p = y[2]; if (p == (ulong)-x) return 0;
     488          14 :   return p < (ulong)(-x) ? -1 : 1;
     489             : }
     490             : INLINE int
     491     4435869 : cmpis(GEN x, long y) { return -cmpsi(y,x); }
     492             : INLINE int
     493      547472 : mpcmp(GEN x, GEN y)
     494             : {
     495      547472 :   if (typ(x)==t_INT)
     496       12468 :     return (typ(y)==t_INT) ? cmpii(x,y) : cmpir(x,y);
     497      535004 :   return (typ(y)==t_INT) ? -cmpir(y,x) : cmprr(x,y);
     498             : }
     499             : 
     500             : /* x == y ? */
     501             : INLINE int
     502      138549 : equalsi(long x, GEN y)
     503             : {
     504      138549 :   if (!x) return !signe(y);
     505      138549 :   if (x > 0)
     506             :   {
     507      138528 :     if (signe(y) <= 0 || lgefint(y) != 3) return 0;
     508      128979 :     return ((ulong)y[2] == (ulong)x);
     509             :   }
     510          21 :   if (signe(y) >= 0 || lgefint(y) != 3) return 0;
     511          21 :   return ((ulong)y[2] == (ulong)-x);
     512             : }
     513             : /* x == |y| ? */
     514             : INLINE int
     515    28562368 : absequalui(ulong x, GEN y)
     516             : {
     517    28562368 :   if (!x) return !signe(y);
     518    28562368 :   return (lgefint(y) == 3 && (ulong)y[2] == x);
     519             : }
     520             : INLINE int
     521    28455899 : absequaliu(GEN x, ulong y) { return absequalui(y,x); }
     522             : INLINE int
     523      138431 : equalis(GEN x, long y) { return equalsi(y,x); }
     524             : 
     525             : /* assume x != 0, is |x| == 2^n ? */
     526             : INLINE int
     527      301036 : absrnz_equal2n(GEN x) {
     528      301036 :   if ((ulong)x[2]==HIGHBIT)
     529             :   {
     530       14801 :     long i, lx = lg(x);
     531       67970 :     for (i = 3; i < lx; i++)
     532       61901 :       if (x[i]) return 0;
     533        6069 :     return 1;
     534             :   }
     535      286235 :   return 0;
     536             : }
     537             : /* assume x != 0, is |x| == 1 ? */
     538             : INLINE int
     539      607476 : absrnz_equal1(GEN x) { return !expo(x) && absrnz_equal2n(x); }
     540             : 
     541             : INLINE long
     542  2945782780 : maxss(long x, long y) { return x>y?x:y; }
     543             : INLINE long
     544   742670845 : minss(long x, long y) { return x<y?x:y; }
     545             : INLINE long
     546     1858880 : minuu(ulong x, ulong y) { return x<y?x:y; }
     547             : INLINE long
     548     4998748 : maxuu(ulong x, ulong y) { return x>y?x:y; }
     549             : INLINE double
     550     1532058 : maxdd(double x, double y) { return x>y?x:y; }
     551             : INLINE double
     552      399789 : mindd(double x, double y) { return x<y?x:y; }
     553             : 
     554             : /*******************************************************************/
     555             : /*                                                                 */
     556             : /*                             ADD / SUB                           */
     557             : /*                                                                 */
     558             : /*******************************************************************/
     559             : INLINE GEN
     560       25046 : subuu(ulong x, ulong y)
     561             : {
     562             :   ulong z;
     563             :   LOCAL_OVERFLOW;
     564       25046 :   z = subll(x, y);
     565       25046 :   return overflow? utoineg(-z): utoi(z);
     566             : }
     567             : INLINE GEN
     568  1676151954 : adduu(ulong x, ulong y) { ulong t = x+y; return uutoi((t < x), t); }
     569             : 
     570             : INLINE GEN
     571       25046 : addss(long x, long y)
     572             : {
     573       25046 :   if (!x) return stoi(y);
     574       25046 :   if (!y) return stoi(x);
     575       25046 :   if (x > 0) return y > 0? adduu(x,y): subuu(x, -y);
     576             : 
     577       25046 :   if (y > 0) return subuu(y, -x);
     578             :   else { /* - adduu(-x, -y) */
     579           0 :     ulong t = (-x)+(-y); return uutoineg((t < (ulong)(-x)), t);
     580             :   }
     581             : }
     582       25046 : INLINE GEN subss(long x, long y) { return addss(-y,x); }
     583             : 
     584             : INLINE GEN
     585  3960862702 : subii(GEN x, GEN y)
     586             : {
     587  3960862702 :   if (x==y) return gen_0; /* frequent with x = y = gen_0 */
     588  2973220594 :   return addii_sign(x, signe(x), y, -signe(y));
     589             : }
     590             : INLINE GEN
     591  4490789595 : addii(GEN x, GEN y) { return addii_sign(x, signe(x), y, signe(y)); }
     592             : INLINE GEN
     593   795990270 : addrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, signe(y)); }
     594             : INLINE GEN
     595   879982299 : subrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, -signe(y)); }
     596             : INLINE GEN
     597   116346594 : addir(GEN x, GEN y) { return addir_sign(x, signe(x), y, signe(y)); }
     598             : INLINE GEN
     599    79200755 : subir(GEN x, GEN y) { return addir_sign(x, signe(x), y, -signe(y)); }
     600             : INLINE GEN
     601     1522820 : subri(GEN x, GEN y) { return addir_sign(y, -signe(y), x, signe(x)); }
     602             : INLINE GEN
     603    72499999 : addsi(long x, GEN y) { return addsi_sign(x, y, signe(y)); }
     604             : INLINE GEN
     605    60350040 : addui(ulong x, GEN y) { return addui_sign(x, y, signe(y)); }
     606             : INLINE GEN
     607     5225403 : subsi(long x, GEN y) { return addsi_sign(x, y, -signe(y)); }
     608             : INLINE GEN
     609    29198279 : subui(ulong x, GEN y) { return addui_sign(x, y, -signe(y)); }
     610             : 
     611             : /*******************************************************************/
     612             : /*                                                                 */
     613             : /*                           MOD, REM, DIV                         */
     614             : /*                                                                 */
     615             : /*******************************************************************/
     616    96730036 : INLINE ulong mod2BIL(GEN x) { return *int_LSW(x); }
     617           0 : INLINE long mod64(GEN x) { return mod2BIL(x) & 63; }
     618         252 : INLINE long mod32(GEN x) { return mod2BIL(x) & 31; }
     619       25074 : INLINE long mod16(GEN x) { return mod2BIL(x) & 15; }
     620    49673301 : INLINE long mod8(GEN x)  { return mod2BIL(x) & 7; }
     621    11677437 : INLINE long mod4(GEN x)  { return mod2BIL(x) & 3; }
     622    20859361 : INLINE long mod2(GEN x)  { return mod2BIL(x) & 1; }
     623             : INLINE int
     624    29842516 : mpodd(GEN x) { return signe(x) && mod2(x); }
     625             : /* x mod 2^n, n < BITS_IN_LONG */
     626             : INLINE ulong
     627     4845512 : umodi2n(GEN x, long n)
     628             : {
     629     4845512 :   long s = signe(x);
     630     4845512 :   const ulong _2n = 1UL << n;
     631             :   ulong m;
     632     4845512 :   if (!s) return 0;
     633     4838736 :   m = *int_LSW(x) & (_2n - 1);
     634     4838736 :   if (s < 0 && m) m = _2n - m;
     635     4838736 :   return m;
     636             : }
     637           0 : INLINE ulong Mod64(GEN x){ return umodi2n(x,6); }
     638      166663 : INLINE ulong Mod32(GEN x){ return umodi2n(x,5); }
     639      216545 : INLINE ulong Mod16(GEN x){ return umodi2n(x,4); }
     640     1307999 : INLINE ulong Mod8(GEN x) { return umodi2n(x,3); }
     641     1329755 : INLINE ulong Mod4(GEN x) { return umodi2n(x,2); }
     642     1824480 : INLINE ulong Mod2(GEN x) { return umodi2n(x,1); }
     643             : 
     644             : INLINE GEN
     645    18917381 : truedivii(GEN a,GEN b) { return truedvmdii(a,b,NULL); }
     646             : INLINE GEN
     647         876 : truedivis(GEN a, long b) { return truedvmdis(a,b,NULL); }
     648             : INLINE GEN
     649     6167132 : truedivsi(long a, GEN b) { return truedvmdsi(a,b,NULL); }
     650             : 
     651             : INLINE GEN
     652     7412480 : divii(GEN a, GEN b) { return dvmdii(a,b,NULL); }
     653             : INLINE GEN
     654  1344809821 : remii(GEN a, GEN b) { return dvmdii(a,b,ONLY_REM); }
     655             : 
     656             : INLINE GEN
     657           0 : divss(long x, long y) { return stoi(x / y); }
     658             : INLINE GEN
     659      658364 : modss(long x, long y) { return stoi(smodss(x, y)); }
     660             : INLINE GEN
     661           0 : remss(long x, long y) { return stoi(x % y); }
     662             : INLINE long
     663   198246180 : smodss(long x, long y)
     664             : {
     665   198246180 :   long r = x%y;
     666   198246180 :   return (r >= 0)? r: labs(y) + r;
     667             : }
     668             : INLINE ulong
     669    11542240 : umodsu(long x, ulong y)
     670             : {
     671    11542240 :   return x>=0 ? x%y: Fl_neg((-x)%y, y);
     672             : }
     673             : 
     674             : INLINE long
     675    35824999 : sdivss_rem(long x, long y, long *r)
     676             : {
     677             :   long q;
     678             :   LOCAL_HIREMAINDER;
     679    35824999 :   if (!y) pari_err_INV("sdivss_rem",gen_0);
     680    35824999 :   hiremainder = 0; q = divll((ulong)labs(x),(ulong)labs(y));
     681    35824999 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     682    35824999 :   if (y < 0) q = -q;
     683    35824999 :   *r = hiremainder; return q;
     684             : }
     685             : INLINE GEN
     686           0 : divss_rem(long x, long y, long *r) { return stoi(sdivss_rem(x,y,r)); }
     687             : INLINE ulong
     688         897 : udivuu_rem(ulong x, ulong y, ulong *r)
     689             : {
     690         897 :   if (!y) pari_err_INV("udivuu_rem",gen_0);
     691         897 :   *r = x % y; return x / y;
     692             : }
     693             : INLINE ulong
     694      148396 : ceildivuu(ulong a, ulong b)
     695             : {
     696      148396 :   ulong c = a/b;
     697      148396 :   return (a%b)? c+1: c;
     698             : }
     699             : 
     700             : 
     701             : INLINE ulong
     702       10577 : udivui_rem(ulong x, GEN y, ulong *r)
     703             : {
     704       10577 :   long q, s = signe(y);
     705             :   LOCAL_HIREMAINDER;
     706             : 
     707       10577 :   if (!s) pari_err_INV("udivui_rem",gen_0);
     708       10577 :   if (!x || lgefint(y)>3) { *r = x; return 0; }
     709       10318 :   hiremainder=0; q = (long)divll(x, (ulong)y[2]);
     710       10318 :   if (s < 0) q = -q;
     711       10318 :   *r = hiremainder; return q;
     712             : }
     713             : 
     714             : /* assume d != 0 and |n| / d can be represented as an ulong.
     715             :  * Return |n|/d, set *r = |n| % d */
     716             : INLINE ulong
     717    12480475 : udiviu_rem(GEN n, ulong d, ulong *r)
     718             : {
     719    12480475 :   switch(lgefint(n))
     720             :   {
     721           0 :     case 2: *r = 0; return 0;
     722             :     case 3:
     723             :     {
     724    12480475 :       ulong nn = n[2];
     725    12480475 :       *r = nn % d; return nn / d;
     726             :     }
     727             :     default: /* 4 */
     728             :     {
     729             :       ulong n1, n0, q;
     730             :       LOCAL_HIREMAINDER;
     731           0 :       n0 = *int_W(n,0);
     732           0 :       n1 = *int_W(n,1);
     733           0 :       hiremainder = n1;
     734           0 :       q = divll(n0, d);
     735           0 :       *r = hiremainder; return q;
     736             :     }
     737             :   }
     738             : }
     739             : 
     740             : INLINE long
     741    13900471 : sdivsi_rem(long x, GEN y, long *r)
     742             : {
     743    13900471 :   long q, s = signe(y);
     744             :   LOCAL_HIREMAINDER;
     745             : 
     746    13900471 :   if (!s) pari_err_INV("sdivsi_rem",gen_0);
     747    13900471 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) { *r = x; return 0; }
     748    11991104 :   hiremainder=0; q = (long)divll(labs(x), (ulong)y[2]);
     749    11991104 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     750    11991104 :   if (s < 0) q = -q;
     751    11991104 :   *r = hiremainder; return q;
     752             : }
     753             : INLINE GEN
     754           0 : divsi_rem(long s, GEN y, long *r) { return stoi(sdivsi_rem(s,y,r)); }
     755             : 
     756             : INLINE long
     757         140 : sdivsi(long x, GEN y)
     758             : {
     759         140 :   long q, s = signe(y);
     760             : 
     761         140 :   if (!s) pari_err_INV("sdivsi",gen_0);
     762         140 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) return 0;
     763          98 :   q = labs(x) / y[2];
     764          98 :   if (x < 0) q = -q;
     765          98 :   if (s < 0) q = -q;
     766          98 :   return q;
     767             : }
     768             : 
     769             : INLINE GEN
     770           0 : dvmdss(long x, long y, GEN *z)
     771             : {
     772             :   long r;
     773           0 :   GEN q = divss_rem(x,y, &r);
     774           0 :   *z = stoi(r); return q;
     775             : }
     776             : INLINE long
     777  2761984525 : dvmdsBIL(long n, long *r) { *r = remsBIL(n); return divsBIL(n); }
     778             : INLINE ulong
     779   149334867 : dvmduBIL(ulong n, ulong *r) { *r = remsBIL(n); return divsBIL(n); }
     780             : INLINE GEN
     781           0 : dvmdsi(long x, GEN y, GEN *z)
     782             : {
     783             :   long r;
     784           0 :   GEN q = divsi_rem(x,y, &r);
     785           0 :   *z = stoi(r); return q;
     786             : }
     787             : INLINE GEN
     788           0 : dvmdis(GEN x, long y, GEN *z)
     789             : {
     790             :   long r;
     791           0 :   GEN q = divis_rem(x,y, &r);
     792           0 :   *z = stoi(r); return q;
     793             : }
     794             : 
     795             : INLINE long
     796    20814972 : smodis(GEN x, long y)
     797             : {
     798    20814972 :   pari_sp av = avma;
     799             :   long r;
     800    20814972 :   (void)divis_rem(x,y, &r); avma = av; return (r >= 0) ? r: labs(y) + r;
     801             : }
     802             : INLINE GEN
     803    19590211 : modis(GEN x, long y) { return stoi(smodis(x,y)); }
     804             : INLINE GEN
     805     7733093 : modsi(long x, GEN y) {
     806             :   long r;
     807     7733093 :   (void)sdivsi_rem(x, y, &r);
     808     7733094 :   return (r >= 0)? stoi(r): addsi_sign(r, y, 1);
     809             : }
     810             : 
     811             : INLINE ulong
     812     1261248 : umodui(ulong x, GEN y)
     813             : {
     814     1261248 :   if (!signe(y)) pari_err_INV("umodui",gen_0);
     815     1261248 :   if (!x || lgefint(y) > 3) return x;
     816      237231 :   return x % (ulong)y[2];
     817             : }
     818             : 
     819             : INLINE GEN
     820           0 : remsi(long x, GEN y)
     821           0 : { long r; (void)sdivsi_rem(x,y, &r); return stoi(r); }
     822             : INLINE GEN
     823           0 : remis(GEN x, long y)
     824             : {
     825           0 :   pari_sp av = avma;
     826             :   long r;
     827           0 :   (void)divis_rem(x,y, &r); avma = av; return stoi(r);
     828             : }
     829             : 
     830             : INLINE GEN
     831           0 : rdivis(GEN x, long y, long prec)
     832             : {
     833           0 :   GEN z = cgetr(prec);
     834           0 :   pari_sp av = avma;
     835           0 :   affrr(divrs(itor(x,prec), y),z);
     836           0 :   avma = av; return z;
     837             : }
     838             : INLINE GEN
     839           0 : rdivsi(long x, GEN y, long prec)
     840             : {
     841           0 :   GEN z = cgetr(prec);
     842           0 :   pari_sp av = avma;
     843           0 :   affrr(divsr(x, itor(y,prec)), z);
     844           0 :   avma = av; return z;
     845             : }
     846             : INLINE GEN
     847      839647 : rdivss(long x, long y, long prec)
     848             : {
     849      839647 :   GEN z = cgetr(prec);
     850      839647 :   pari_sp av = avma;
     851      839647 :   affrr(divrs(stor(x, prec), y), z);
     852      839647 :   avma = av; return z;
     853             : }
     854             : 
     855             : INLINE void
     856          14 : rdiviiz(GEN x, GEN y, GEN z)
     857             : {
     858          14 :   pari_sp av = avma;
     859          14 :   long prec = realprec(z);
     860          14 :   affir(x, z);
     861          14 :   if (!is_bigint(y)) {
     862           0 :     affrr(divrs(z, y[2]), z);
     863           0 :     if (signe(y) < 0) togglesign(z);
     864             :   }
     865             :   else
     866          14 :     affrr(divrr(z, itor(y,prec)), z);
     867          14 :   avma = av;
     868          14 : }
     869             : INLINE GEN
     870    11255178 : rdivii(GEN x, GEN y, long prec)
     871             : {
     872    11255178 :   GEN z = cgetr(prec);
     873    11255178 :   pari_sp av = avma;
     874    11255178 :   affir(x, z);
     875    11255178 :   if (lg(y) == 3) {
     876     7104729 :     affrr(divru(z, y[2]), z);
     877     7104729 :     if (signe(y) < 0) togglesign(z);
     878             :   }
     879             :   else
     880     4150449 :     affrr(divrr(z, itor(y,prec)), z);
     881    11255178 :   avma = av; return z;
     882             : }
     883             : INLINE GEN
     884    11181885 : fractor(GEN x, long prec) { return rdivii(gel(x,1), gel(x,2), prec); }
     885             : 
     886             : INLINE int
     887     6855101 : dvdii(GEN x, GEN y)
     888             : {
     889     6855101 :   pari_sp av=avma;
     890     6855101 :   GEN r = remii(x,y);
     891     6867044 :   avma = av; return r == gen_0;
     892             : }
     893             : INLINE int
     894          84 : dvdsi(long x, GEN y)
     895             : {
     896          84 :   if (!signe(y)) return x == 0;
     897          84 :   if (lgefint(y) != 3) return 0;
     898          84 :   return x % y[2] == 0;
     899             : }
     900             : INLINE int
     901         147 : dvdui(ulong x, GEN y)
     902             : {
     903         147 :   if (!signe(y)) return x == 0;
     904         147 :   if (lgefint(y) != 3) return 0;
     905         147 :   return x % y[2] == 0;
     906             : }
     907             : INLINE int
     908        5537 : dvdis(GEN x, long y)
     909        5537 : { return y? smodis(x, y) == 0: signe(x) == 0; }
     910             : INLINE int
     911        2261 : dvdiu(GEN x, ulong y)
     912        2261 : { return y? umodiu(x, y) == 0: signe(x) == 0; }
     913             : 
     914             : INLINE int
     915           0 : dvdisz(GEN x, long y, GEN z)
     916             : {
     917           0 :   const pari_sp av = avma;
     918             :   long r;
     919           0 :   GEN p1 = divis_rem(x,y, &r);
     920           0 :   avma = av; if (r) return 0;
     921           0 :   affii(p1,z); return 1;
     922             : }
     923             : INLINE int
     924           0 : dvdiuz(GEN x, ulong y, GEN z)
     925             : {
     926           0 :   const pari_sp av = avma;
     927             :   ulong r;
     928           0 :   GEN p1 = diviu_rem(x,y, &r);
     929           0 :   avma = av; if (r) return 0;
     930           0 :   affii(p1,z); return 1;
     931             : }
     932             : INLINE int
     933        4007 : dvdiiz(GEN x, GEN y, GEN z)
     934             : {
     935        4007 :   const pari_sp av=avma;
     936             :   GEN p2;
     937        4007 :   const GEN p1=dvmdii(x,y,&p2);
     938             : 
     939        4007 :   if (signe(p2)) { avma=av; return 0; }
     940        3292 :   affii(p1,z); avma=av; return 1;
     941             : }
     942             : 
     943             : INLINE ulong
     944     5424910 : remlll_pre(ulong u2, ulong u1, ulong u0, ulong n, ulong ninv)
     945             : {
     946     5424910 :   u1 = remll_pre(u2, u1, n, ninv);
     947     5424966 :   return remll_pre(u1, u0, n, ninv);
     948             : }
     949             : 
     950             : INLINE ulong
     951  1638506429 : Fl_sqr_pre(ulong a, ulong p, ulong pi)
     952             : {
     953             :   register ulong x;
     954             :   LOCAL_HIREMAINDER;
     955  1638506429 :   x = mulll(a,a);
     956  1638506429 :   return remll_pre(hiremainder, x, p, pi);
     957             : }
     958             : 
     959             : INLINE ulong
     960  2625431167 : Fl_mul_pre(ulong a, ulong b, ulong p, ulong pi)
     961             : {
     962             :   register ulong x;
     963             :   LOCAL_HIREMAINDER;
     964  2625431167 :   x = mulll(a,b);
     965  2625431167 :   return remll_pre(hiremainder, x, p, pi);
     966             : }
     967             : 
     968             : INLINE ulong
     969  4579361526 : Fl_addmul_pre(ulong y0, ulong x0, ulong x1, ulong p, ulong pi)
     970             : {
     971             :   ulong l0, h0;
     972             :   LOCAL_HIREMAINDER;
     973  4579361526 :   hiremainder = y0;
     974  4579361526 :   l0 = addmul(x0, x1); h0 = hiremainder;
     975  4579361526 :   return remll_pre(h0, l0, p, pi);
     976             : }
     977             : 
     978             : INLINE ulong
     979    47943407 : Fl_addmulmul_pre(ulong x0, ulong y0, ulong x1, ulong y1, ulong p, ulong pi)
     980             : {
     981             :   ulong l0, l1, h0, h1;
     982             :   LOCAL_OVERFLOW;
     983             :   LOCAL_HIREMAINDER;
     984    47943407 :   l0 = mulll(x0, y0); h0 = hiremainder;
     985    47943407 :   l1 = mulll(x1, y1); h1 = hiremainder;
     986    47943407 :   l0 = addll(l0, l1); h0 = addllx(h0, h1);
     987    47943407 :   return overflow ? remlll_pre(1, h0, l0, p, pi): remll_pre(h0, l0, p, pi);
     988             : }
     989             : 
     990             : INLINE ulong
     991      188107 : Fl_ellj_pre(ulong a4, ulong a6, ulong p, ulong pi)
     992             : {
     993             :   /* a43 = 4 a4^3 */
     994      188107 :   ulong a43 = Fl_double(Fl_double(
     995             :               Fl_mul_pre(a4, Fl_sqr_pre(a4, p, pi), p, pi), p), p);
     996             :   /* a62 = 27 a6^2 */
     997      188109 :   ulong a62 = Fl_mul_pre(Fl_sqr_pre(a6, p, pi), 27 % p, p, pi);
     998      188111 :   ulong z1 = Fl_mul_pre(a43, 1728 % p, p, pi);
     999      188112 :   ulong z2 = Fl_add(a43, a62, p);
    1000      188112 :   return Fl_div(z1, z2, p);
    1001             : }
    1002             : 
    1003             : /*******************************************************************/
    1004             : /*                                                                 */
    1005             : /*                        MP (INT OR REAL)                         */
    1006             : /*                                                                 */
    1007             : /*******************************************************************/
    1008             : INLINE GEN
    1009          21 : mptrunc(GEN x) { return typ(x)==t_INT? icopy(x): truncr(x); }
    1010             : INLINE GEN
    1011           0 : mpfloor(GEN x) { return typ(x)==t_INT? icopy(x): floorr(x); }
    1012             : INLINE GEN
    1013           0 : mpceil(GEN x) { return typ(x)==t_INT? icopy(x): ceilr(x); }
    1014             : INLINE GEN
    1015     1300390 : mpround(GEN x) { return typ(x) == t_INT? icopy(x): roundr(x); }
    1016             : 
    1017             : INLINE long
    1018      249542 : mpexpo(GEN x) { return typ(x) == t_INT? expi(x): expo(x); }
    1019             : 
    1020             : INLINE GEN
    1021    47314897 : mpadd(GEN x, GEN y)
    1022             : {
    1023    47314897 :   if (typ(x)==t_INT)
    1024     9896469 :     return (typ(y)==t_INT) ? addii(x,y) : addir(x,y);
    1025    37418428 :   return (typ(y)==t_INT) ? addir(y,x) : addrr(x,y);
    1026             : }
    1027             : INLINE GEN
    1028    18420101 : mpsub(GEN x, GEN y)
    1029             : {
    1030    18420101 :   if (typ(x)==t_INT)
    1031      236264 :     return (typ(y)==t_INT) ? subii(x,y) : subir(x,y);
    1032    18183837 :   return (typ(y)==t_INT) ? subri(x,y) : subrr(x,y);
    1033             : }
    1034             : INLINE GEN
    1035    54218991 : mpmul(GEN x, GEN y)
    1036             : {
    1037    54218991 :   if (typ(x)==t_INT)
    1038    16514701 :     return (typ(y)==t_INT) ? mulii(x,y) : mulir(x,y);
    1039    37704290 :   return (typ(y)==t_INT) ? mulir(y,x) : mulrr(x,y);
    1040             : }
    1041             : INLINE GEN
    1042     6411081 : mpsqr(GEN x) { return (typ(x)==t_INT) ? sqri(x) : sqrr(x); }
    1043             : INLINE GEN
    1044      150187 : mpdiv(GEN x, GEN y)
    1045             : {
    1046      150187 :   if (typ(x)==t_INT)
    1047       77458 :     return (typ(y)==t_INT) ? divii(x,y) : divir(x,y);
    1048       72729 :   return (typ(y)==t_INT) ? divri(x,y) : divrr(x,y);
    1049             : }
    1050             : 
    1051             : /*******************************************************************/
    1052             : /*                                                                 */
    1053             : /*                          Z/nZ, n ULONG                          */
    1054             : /*                                                                 */
    1055             : /*******************************************************************/
    1056             : INLINE ulong
    1057   399280504 : Fl_double(ulong a, ulong p)
    1058             : {
    1059   399280504 :   ulong res = a << 1;
    1060   399280504 :   return (res >= p || res < a) ? res - p : res;
    1061             : }
    1062             : INLINE ulong
    1063    72065358 : Fl_triple(ulong a, ulong p)
    1064             : {
    1065    72065358 :   ulong res = a << 1;
    1066    72065358 :   if (res >= p || res < a) res -= p;
    1067    72065358 :   res += a;
    1068    72065358 :   return (res >= p || res < a)? res - p: res;
    1069             : }
    1070             : INLINE ulong
    1071    14037869 : Fl_halve(ulong a, ulong p)
    1072             : {
    1073             :   ulong ap, ap2;
    1074    14037869 :   if ((a&1UL)==0) return a>>1;
    1075     7083754 :   ap = a + p; ap2 = ap>>1;
    1076     7083754 :   return ap>=a ? ap2: (ap2|HIGHBIT);
    1077             : }
    1078             : 
    1079             : INLINE ulong
    1080  2516301393 : Fl_add(ulong a, ulong b, ulong p)
    1081             : {
    1082  2516301393 :   ulong res = a + b;
    1083  2516301393 :   return (res >= p || res < a) ? res - p : res;
    1084             : }
    1085             : INLINE ulong
    1086    71396812 : Fl_neg(ulong x, ulong p) { return x ? p - x: 0; }
    1087             : 
    1088             : INLINE ulong
    1089  4612529488 : Fl_sub(ulong a, ulong b, ulong p)
    1090             : {
    1091  4612529488 :   ulong res = a - b;
    1092  4612529488 :   return (res > a) ? res + p: res;
    1093             : }
    1094             : 
    1095             : /* centerlift(u mod p) */
    1096             : INLINE long
    1097    11993880 : Fl_center(ulong u, ulong p, ulong ps2) { return (long) (u > ps2)? u - p: u; }
    1098             : 
    1099             : INLINE ulong
    1100   863496867 : Fl_mul(ulong a, ulong b, ulong p)
    1101             : {
    1102             :   register ulong x;
    1103             :   LOCAL_HIREMAINDER;
    1104   863496867 :   x = mulll(a,b);
    1105   863496867 :   if (!hiremainder) return x % p;
    1106   163143102 :   (void)divll(x,p); return hiremainder;
    1107             : }
    1108             : INLINE ulong
    1109    67046954 : Fl_sqr(ulong a, ulong p)
    1110             : {
    1111             :   register ulong x;
    1112             :   LOCAL_HIREMAINDER;
    1113    67046954 :   x = mulll(a,a);
    1114    67046954 :   if (!hiremainder) return x % p;
    1115    37969713 :   (void)divll(x,p); return hiremainder;
    1116             : }
    1117             : INLINE ulong
    1118     8522679 : Fl_div(ulong a, ulong b, ulong p) { return Fl_mul(a, Fl_inv(b, p), p); }
    1119             : 
    1120             : /*******************************************************************/
    1121             : /*                                                                 */
    1122             : /*        DEFINED FROM EXISTING ONE EXPLOITING COMMUTATIVITY       */
    1123             : /*                                                                 */
    1124             : /*******************************************************************/
    1125             : INLINE GEN
    1126      208267 : addri(GEN x, GEN y) { return addir(y,x); }
    1127             : INLINE GEN
    1128    69173615 : addis(GEN x, long s) { return addsi(s,x); }
    1129             : INLINE GEN
    1130    58881225 : addiu(GEN x, ulong s) { return addui(s,x); }
    1131             : INLINE GEN
    1132     2835348 : addrs(GEN x, long s) { return addsr(s,x); }
    1133             : 
    1134             : INLINE GEN
    1135    25966602 : subiu(GEN x, long y) { GEN z = subui(y, x); togglesign(z); return z; }
    1136             : INLINE GEN
    1137        1639 : subis(GEN x, long y) { return addsi(-y,x); }
    1138             : INLINE GEN
    1139     3153605 : subrs(GEN x, long y) { return addsr(-y,x); }
    1140             : 
    1141             : INLINE GEN
    1142   130992359 : mulis(GEN x, long s) { return mulsi(s,x); }
    1143             : INLINE GEN
    1144   581928744 : muliu(GEN x, ulong s) { return mului(s,x); }
    1145             : INLINE GEN
    1146     4378788 : mulru(GEN x, ulong s) { return mulur(s,x); }
    1147             : INLINE GEN
    1148    15736873 : mulri(GEN x, GEN s) { return mulir(s,x); }
    1149             : INLINE GEN
    1150    10015584 : mulrs(GEN x, long s) { return mulsr(s,x); }
    1151             : 
    1152             : /*******************************************************************/
    1153             : /*                                                                 */
    1154             : /*                  VALUATION, EXPONENT, SHIFTS                    */
    1155             : /*                                                                 */
    1156             : /*******************************************************************/
    1157             : INLINE long
    1158    52449197 : vali(GEN x)
    1159             : {
    1160             :   long i;
    1161             :   GEN xp;
    1162             : 
    1163    52449197 :   if (!signe(x)) return -1;
    1164    52449029 :   xp=int_LSW(x);
    1165    52449029 :   for (i=0; !*xp; i++) xp=int_nextW(xp);
    1166    52449029 :   return vals(*xp) + i * BITS_IN_LONG;
    1167             : }
    1168             : 
    1169             : 
    1170             : /* assume x > 0 */
    1171             : INLINE long
    1172   327931270 : expu(ulong x) { return (BITS_IN_LONG-1) - (long)bfffo(x); }
    1173             : 
    1174             : INLINE long
    1175   603253894 : expi(GEN x)
    1176             : {
    1177   603253894 :   const long lx=lgefint(x);
    1178   603253894 :   return lx==2? -(long)HIGHEXPOBIT: bit_accuracy(lx)-(long)bfffo(*int_MSW(x))-1;
    1179             : }
    1180             : 
    1181             : INLINE GEN
    1182    54066550 : shiftr(GEN x, long n)
    1183             : {
    1184    54066550 :   const long e = evalexpo(expo(x)+n);
    1185    54066550 :   const GEN y = rcopy(x);
    1186             : 
    1187    54066550 :   if (e & ~EXPOBITS) pari_err_OVERFLOW("expo()");
    1188    54066550 :   y[1] = (y[1]&~EXPOBITS) | e; return y;
    1189             : }
    1190             : INLINE GEN
    1191    17038399 : mpshift(GEN x,long s) { return (typ(x)==t_INT)?shifti(x,s):shiftr(x,s); }
    1192             : 
    1193             : /* FIXME: adapt/use mpn_[lr]shift instead */
    1194             : /* z2[imin..imax] := z1[imin..imax].f shifted left sh bits
    1195             :  * (feeding f from the right). Assume sh > 0 */
    1196             : INLINE void
    1197  2888431015 : shift_left(GEN z2, GEN z1, long imin, long imax, ulong f,  ulong sh)
    1198             : {
    1199  2888431015 :   GEN sb = z1 + imin, se = z1 + imax, te = z2 + imax;
    1200  2888431015 :   ulong l, m = BITS_IN_LONG - sh, k = f >> m;
    1201 15596765706 :   while (se > sb) {
    1202  9819903676 :     l     = *se--;
    1203  9819903676 :     *te-- = (l << sh) | k;
    1204  9819903676 :     k     = l >> m;
    1205             :   }
    1206  2888431015 :   *te = (((ulong)*se) << sh) | k;
    1207  2888431015 : }
    1208             : /* z2[imin..imax] := f.z1[imin..imax-1] shifted right sh bits
    1209             :  * (feeding f from the left). Assume sh > 0 */
    1210             : INLINE void
    1211  2369724242 : shift_right(GEN z2, GEN z1, long imin, long imax, ulong f, ulong sh)
    1212             : {
    1213  2369724242 :   GEN sb = z1 + imin, se = z1 + imax, tb = z2 + imin;
    1214  2369724242 :   ulong k, l = *sb++, m = BITS_IN_LONG - sh;
    1215  2369724242 :   *tb++ = (l >> sh) | (f << m);
    1216 11956953552 :   while (sb < se) {
    1217  7217505068 :     k     = l << m;
    1218  7217505068 :     l     = *sb++;
    1219  7217505068 :     *tb++ = (l >> sh) | k;
    1220             :   }
    1221  2369724242 : }
    1222             : 
    1223             : /* Backward compatibility. Inefficient && unused */
    1224             : extern ulong hiremainder;
    1225             : INLINE ulong
    1226           0 : shiftl(ulong x, ulong y)
    1227           0 : { hiremainder = x>>(BITS_IN_LONG-y); return (x<<y); }
    1228             : 
    1229             : INLINE ulong
    1230           0 : shiftlr(ulong x, ulong y)
    1231           0 : { hiremainder = x<<(BITS_IN_LONG-y); return (x>>y); }
    1232             : 
    1233             : INLINE void
    1234    79845579 : shiftr_inplace(GEN z, long d)
    1235             : {
    1236    79845579 :   setexpo(z, expo(z)+d);
    1237    79845579 : }
    1238             : 
    1239             : /*******************************************************************/
    1240             : /*                                                                 */
    1241             : /*                           ASSIGNMENT                            */
    1242             : /*                                                                 */
    1243             : /*******************************************************************/
    1244             : INLINE void
    1245    83690151 : affii(GEN x, GEN y)
    1246             : {
    1247    83690151 :   long lx = lgefint(x);
    1248    83690151 :   if (lg(y)<lx) pari_err_OVERFLOW("t_INT-->t_INT assignment");
    1249    83690155 :   while (--lx) y[lx] = x[lx];
    1250    83690155 : }
    1251             : INLINE void
    1252      429418 : affsi(long s, GEN x)
    1253             : {
    1254      429418 :   if (!s) x[1] = evalsigne(0) | evallgefint(2);
    1255             :   else
    1256             :   {
    1257      407525 :     if (s > 0) { x[1] = evalsigne( 1) | evallgefint(3); x[2] =  s; }
    1258      151757 :     else       { x[1] = evalsigne(-1) | evallgefint(3); x[2] = -s; }
    1259             :   }
    1260      429418 : }
    1261             : INLINE void
    1262     8407504 : affui(ulong u, GEN x)
    1263             : {
    1264     8407504 :   if (!u) x[1] = evalsigne(0) | evallgefint(2);
    1265     8406230 :   else  { x[1] = evalsigne(1) | evallgefint(3); x[2] = u; }
    1266     8407504 : }
    1267             : 
    1268             : INLINE void
    1269   145449073 : affsr(long x, GEN y)
    1270             : {
    1271   145449073 :   long sh, i, ly = lg(y);
    1272             : 
    1273   145449073 :   if (!x)
    1274             :   {
    1275     1366460 :     y[1] = evalexpo(-prec2nbits(ly));
    1276   146815533 :     return;
    1277             :   }
    1278   144082613 :   if (x < 0) {
    1279      170423 :     x = -x; sh = bfffo(x);
    1280      170423 :     y[1] = evalsigne(-1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1281             :   }
    1282             :   else
    1283             :   {
    1284   143912190 :     sh = bfffo(x);
    1285   143912190 :     y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1286             :   }
    1287   144082613 :   y[2] = ((ulong)x)<<sh; for (i=3; i<ly; i++) y[i]=0;
    1288             : }
    1289             : 
    1290             : INLINE void
    1291     5678038 : affur(ulong x, GEN y)
    1292             : {
    1293     5678038 :   long sh, i, ly = lg(y);
    1294             : 
    1295     5678038 :   if (!x)
    1296             :   {
    1297         266 :     y[1] = evalexpo(-prec2nbits(ly));
    1298     5678304 :     return;
    1299             :   }
    1300     5677772 :   sh = bfffo(x);
    1301     5677772 :   y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1302     5677772 :   y[2] = x<<sh; for (i=3; i<ly; i++) y[i] = 0;
    1303             : }
    1304             : 
    1305             : INLINE void
    1306      147747 : affiz(GEN x, GEN y) { if (typ(y)==t_INT) affii(x,y); else affir(x,y); }
    1307             : INLINE void
    1308           0 : affsz(long x, GEN y) { if (typ(y)==t_INT) affsi(x,y); else affsr(x,y); }
    1309             : INLINE void
    1310      173810 : mpaff(GEN x, GEN y) { if (typ(x)==t_INT) affiz(x, y); else affrr(x,y); }
    1311             : 
    1312             : /*******************************************************************/
    1313             : /*                                                                 */
    1314             : /*                    OPERATION + ASSIGNMENT                       */
    1315             : /*                                                                 */
    1316             : /*******************************************************************/
    1317             : 
    1318           0 : INLINE void addiiz(GEN x, GEN y, GEN z)
    1319           0 : { pari_sp av = avma; affii(addii(x,y),z); avma = av; }
    1320           0 : INLINE void addirz(GEN x, GEN y, GEN z)
    1321           0 : { pari_sp av = avma; affrr(addir(x,y),z); avma = av; }
    1322           0 : INLINE void addriz(GEN x, GEN y, GEN z)
    1323           0 : { pari_sp av = avma; affrr(addri(x,y),z); avma = av; }
    1324     1396548 : INLINE void addrrz(GEN x, GEN y, GEN z)
    1325     1396548 : { pari_sp av = avma; affrr(addrr(x,y),z); avma = av; }
    1326           0 : INLINE void addsiz(long s, GEN y, GEN z)
    1327           0 : { pari_sp av = avma; affii(addsi(s,y),z); avma = av; }
    1328           0 : INLINE void addsrz(long s, GEN y, GEN z)
    1329           0 : { pari_sp av = avma; affrr(addsr(s,y),z); avma = av; }
    1330           0 : INLINE void addssz(long s, long y, GEN z)
    1331           0 : { pari_sp av = avma; affii(addss(s,y),z); avma = av; }
    1332             : 
    1333          14 : INLINE void diviiz(GEN x, GEN y, GEN z)
    1334          14 : { pari_sp av = avma; affii(divii(x,y),z); avma = av; }
    1335           0 : INLINE void divirz(GEN x, GEN y, GEN z)
    1336           0 : { pari_sp av = avma; mpaff(divir(x,y),z); avma = av; }
    1337           0 : INLINE void divisz(GEN x, long y, GEN z)
    1338           0 : { pari_sp av = avma; affii(divis(x,y),z); avma = av; }
    1339           0 : INLINE void divriz(GEN x, GEN y, GEN z)
    1340           0 : { pari_sp av = avma; affrr(divri(x,y),z); avma = av; }
    1341         376 : INLINE void divrrz(GEN x, GEN y, GEN z)
    1342         376 : { pari_sp av = avma; affrr(divrr(x,y),z); avma = av; }
    1343       90548 : INLINE void divrsz(GEN y, long s, GEN z)
    1344       90548 : { pari_sp av = avma; affrr(divrs(y,s),z); avma = av; }
    1345           0 : INLINE void divsiz(long x, GEN y, GEN z)
    1346           0 : { long junk; affsi(sdivsi_rem(x,y,&junk), z); }
    1347           0 : INLINE void divsrz(long s, GEN y, GEN z)
    1348           0 : { pari_sp av = avma; mpaff(divsr(s,y),z); avma = av; }
    1349           0 : INLINE void divssz(long x, long y, GEN z)
    1350           0 : { affsi(x/y, z); }
    1351             : 
    1352           0 : INLINE void modisz(GEN y, long s, GEN z)
    1353           0 : { affsi(smodis(y,s),z); }
    1354           0 : INLINE void modsiz(long s, GEN y, GEN z)
    1355           0 : { pari_sp av = avma; affii(modsi(s,y),z); avma = av; }
    1356           0 : INLINE void modssz(long s, long y, GEN z)
    1357           0 : { affsi(smodss(s,y),z); }
    1358             : 
    1359           0 : INLINE void mpaddz(GEN x, GEN y, GEN z)
    1360           0 : { pari_sp av = avma; mpaff(mpadd(x,y),z); avma = av; }
    1361           0 : INLINE void mpsubz(GEN x, GEN y, GEN z)
    1362           0 : { pari_sp av = avma; mpaff(mpsub(x,y),z); avma = av; }
    1363           0 : INLINE void mpmulz(GEN x, GEN y, GEN z)
    1364           0 : { pari_sp av = avma; mpaff(mpmul(x,y),z); avma = av; }
    1365             : 
    1366           0 : INLINE void muliiz(GEN x, GEN y, GEN z)
    1367           0 : { pari_sp av = avma; affii(mulii(x,y),z); avma = av; }
    1368           0 : INLINE void mulirz(GEN x, GEN y, GEN z)
    1369           0 : { pari_sp av = avma; mpaff(mulir(x,y),z); avma = av; }
    1370           0 : INLINE void mulriz(GEN x, GEN y, GEN z)
    1371           0 : { pari_sp av = avma; mpaff(mulri(x,y),z); avma = av; }
    1372       56940 : INLINE void mulrrz(GEN x, GEN y, GEN z)
    1373       56940 : { pari_sp av = avma; affrr(mulrr(x,y),z); avma = av; }
    1374           0 : INLINE void mulsiz(long s, GEN y, GEN z)
    1375           0 : { pari_sp av = avma; affii(mulsi(s,y),z); avma = av; }
    1376           0 : INLINE void mulsrz(long s, GEN y, GEN z)
    1377           0 : { pari_sp av = avma; mpaff(mulsr(s,y),z); avma = av; }
    1378           0 : INLINE void mulssz(long s, long y, GEN z)
    1379           0 : { pari_sp av = avma; affii(mulss(s,y),z); avma = av; }
    1380             : 
    1381           0 : INLINE void remiiz(GEN x, GEN y, GEN z)
    1382           0 : { pari_sp av = avma; affii(remii(x,y),z); avma = av; }
    1383           0 : INLINE void remisz(GEN y, long s, GEN z)
    1384           0 : { pari_sp av = avma; affii(remis(y,s),z); avma = av; }
    1385           0 : INLINE void remsiz(long s, GEN y, GEN z)
    1386           0 : { pari_sp av = avma; affii(remsi(s,y),z); avma = av; }
    1387           0 : INLINE void remssz(long s, long y, GEN z)
    1388           0 : { pari_sp av = avma; affii(remss(s,y),z); avma = av; }
    1389             : 
    1390           0 : INLINE void subiiz(GEN x, GEN y, GEN z)
    1391           0 : { pari_sp av = avma; affii(subii(x,y),z); avma = av; }
    1392           0 : INLINE void subirz(GEN x, GEN y, GEN z)
    1393           0 : { pari_sp av = avma; affrr(subir(x,y),z); avma = av; }
    1394           0 : INLINE void subisz(GEN y, long s, GEN z)
    1395           0 : { pari_sp av = avma; affii(addsi(-s,y),z); avma = av; }
    1396           0 : INLINE void subriz(GEN x, GEN y, GEN z)
    1397           0 : { pari_sp av = avma; affrr(subri(x,y),z); avma = av; }
    1398     1295642 : INLINE void subrrz(GEN x, GEN y, GEN z)
    1399     1295642 : { pari_sp av = avma; affrr(subrr(x,y),z); avma = av; }
    1400           0 : INLINE void subrsz(GEN y, long s, GEN z)
    1401           0 : { pari_sp av = avma; affrr(addsr(-s,y),z); avma = av; }
    1402           0 : INLINE void subsiz(long s, GEN y, GEN z)
    1403           0 : { pari_sp av = avma; affii(subsi(s,y),z); avma = av; }
    1404           0 : INLINE void subsrz(long s, GEN y, GEN z)
    1405           0 : { pari_sp av = avma; affrr(subsr(s,y),z); avma = av; }
    1406           0 : INLINE void subssz(long x, long y, GEN z) { addssz(x,-y,z); }
    1407             : 
    1408             : INLINE void
    1409           0 : dvmdssz(long x, long y, GEN z, GEN t) {
    1410           0 :   pari_sp av = avma;
    1411             :   long r;
    1412           0 :   affii(divss_rem(x,y, &r), z); avma = av; affsi(r,t);
    1413           0 : }
    1414             : INLINE void
    1415           0 : dvmdsiz(long x, GEN y, GEN z, GEN t) {
    1416           0 :   pari_sp av = avma;
    1417             :   long r;
    1418           0 :   affii(divsi_rem(x,y, &r), z); avma = av; affsi(r,t);
    1419           0 : }
    1420             : INLINE void
    1421           0 : dvmdisz(GEN x, long y, GEN z, GEN t) {
    1422           0 :   pari_sp av = avma;
    1423             :   long r;
    1424           0 :   affii(divis_rem(x,y, &r),z); avma = av; affsi(r,t);
    1425           0 : }
    1426             : INLINE void
    1427           0 : dvmdiiz(GEN x, GEN y, GEN z, GEN t) {
    1428           0 :   pari_sp av = avma;
    1429             :   GEN r;
    1430           0 :   affii(dvmdii(x,y,&r),z); affii(r,t); avma=av;
    1431           0 : }

Generated by: LCOV version 1.11