Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - kernel/none - level1.h (source / functions) Coverage Total Hit
Test: PARI/GP v2.18.1 lcov report (development 31041-bd73e9fcdd) Lines: 88.5 % 684 605
Test Date: 2026-07-22 22:45:42 Functions: 89.6 % 241 216
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; either version 2 of the License, or (at your option) any later
       9              : version. It is distributed in the hope that it will be useful, but WITHOUT
      10              : ANY WARRANTY WHATSOEVER.
      11              : 
      12              : Check the License for details. You should have received a copy of it, along
      13              : with the package; see the file 'COPYING'. If not, write to the Free Software
      14              : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      15              : 
      16              : /* This file defines "level 1" kernel functions.
      17              :  * These functions can be inline; they are also defined externally in
      18              :  * mpinl.c, which includes this file and never needs to be changed */
      19              : 
      20              : INLINE long
      21   2508899478 : nbits2lg(long x) {
      22   2508899478 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
      23              : }
      24              : INLINE long
      25    834465889 : lg2prec(long x){ return (x-2) * BITS_IN_LONG; }
      26              : 
      27              : INLINE long
      28  98650448394 : evallg(long x)
      29              : {
      30  98650448394 :   if (x & ~LGBITS) pari_err_OVERFLOW("lg()");
      31  98658551289 :   return _evallg(x);
      32              : }
      33              : INLINE long
      34     81627166 : evalvalp(long x)
      35              : {
      36     81627166 :   long v = _evalvalp(x);
      37     81627166 :   if (v & ~VALPBITS) pari_err_OVERFLOW("valp()");
      38     81627429 :   return v;
      39              : }
      40              : INLINE long
      41     25309350 : evalvalser(long x)
      42              : {
      43     25309350 :   long v = _evalvalser(x);
      44     25309350 :   if (v & ~VALSERBITS) pari_err_OVERFLOW("valser()");
      45     25309350 :   return v;
      46              : }
      47              : INLINE long
      48  13830421104 : evalexpo(long x)
      49              : {
      50  13830421104 :   long v = _evalexpo(x);
      51  13830421104 :   if (v & ~EXPOBITS) pari_err_OVERFLOW("expo()");
      52  13835504631 :   return v;
      53              : }
      54              : INLINE long
      55     78670683 : evalprecp(long x)
      56              : {
      57     78670683 :   long v = _evalprecp(x);
      58     78670683 :   if (x & ~((1UL<<(BITS_IN_LONG-VALPnumBITS))-1)) pari_err_OVERFLOW("precp()");
      59     78670793 :   return v;
      60              : }
      61              : 
      62              : INLINE int
      63    218512974 : varncmp(long x, long y)
      64              : {
      65    218512974 :   if (varpriority[x] < varpriority[y]) return  1;
      66    153653334 :   if (varpriority[x] > varpriority[y]) return -1;
      67     74173963 :   return 0;
      68              : }
      69              : INLINE long
      70        26180 : varnmin(long x, long y)
      71        26180 : { return (varpriority[x] <= varpriority[y])? x: y; }
      72              : INLINE long
      73          203 : varnmax(long x, long y)
      74          203 : { return (varpriority[x] >= varpriority[y])? x: y; }
      75              : 
      76              : /* Inhibit some area GC-wise: declare it to be a non recursive
      77              :  * type, of length l. Thus gc_stack_update won't inspect the zone, just copy it.
      78              :  * For the following situation:
      79              :  *   z = cgetg(t,a); av = avma; garbage(); ltop = avma;
      80              :  *   for (i=1; i<HUGE; i++) gel(z,i) = blah();
      81              :  *   stackdummy(av,ltop);
      82              :  * loses (av-ltop) words but saves a GC. */
      83              : INLINE void
      84   3706748601 : stackdummy(pari_sp av, pari_sp ltop) {
      85   3706748601 :   long l = ((GEN)av) - ((GEN)ltop);
      86   3706748601 :   if (l > 0) {
      87   1234095522 :     GEN z = (GEN)ltop;
      88   1234095522 :     z[0] = evaltyp(t_VECSMALL) | evallg(l);
      89              : #ifdef DEBUG
      90              :     { long i; for (i = 1; i < l; i++) z[i] = 0; }
      91              : #endif
      92              :   }
      93   3707216224 : }
      94              : INLINE void
      95    106519927 : fixlg(GEN x, long ly) {
      96    106519927 :   long lx = lg(x), l = lx - ly;
      97    106519927 :   if (l > 0)
      98              :   { /* stackdummy(x+lx, x+ly) */
      99     49850248 :     GEN z = x + ly;
     100     49850248 :     z[0] = evaltyp(t_VECSMALL) | evallg(l);
     101     49850221 :     setlg(x, ly);
     102              : #ifdef DEBUG
     103              :     { long i; for (i = 1; i < l; i++) z[i] = 0; }
     104              : #endif
     105              :   }
     106    106519865 : }
     107              : /* update lg(z) before affrr(y, z)  [ to cater for precision loss ]*/
     108              : INLINE void
     109     56683461 : affrr_fixlg(GEN y, GEN z) { fixlg(z, lg(y)); affrr(y, z); }
     110              : 
     111              : /*******************************************************************/
     112              : /*                                                                 */
     113              : /*                       ALLOCATE ON STACK                         */
     114              : /*                                                                 */
     115              : /*******************************************************************/
     116              : INLINE ulong
     117            0 : get_avma(void) { return avma; }
     118              : INLINE void
     119  >13408*10^7 : set_avma(ulong av) { avma = av; }
     120              : 
     121              : INLINE double
     122    198686430 : gc_double(pari_sp av, double d) { set_avma(av); return d; }
     123              : INLINE long
     124    243045919 : gc_long(pari_sp av, long s) { set_avma(av); return s; }
     125              : INLINE ulong
     126     52308524 : gc_ulong(pari_sp av, ulong s) { set_avma(av); return s; }
     127              : INLINE int
     128     55208905 : gc_bool(pari_sp av, int s) { set_avma(av); return s; }
     129              : INLINE int
     130      2818106 : gc_int(pari_sp av, int s) { set_avma(av); return s; }
     131              : INLINE GEN
     132      7910312 : gc_NULL(pari_sp av) { set_avma(av); return NULL; }
     133              : INLINE GEN
     134  15898320659 : gc_const(pari_sp av, GEN x) { set_avma(av); return x; }
     135              : #define retgc_const(av, x) \
     136              :   do { set_avma(av); return x; } while(0)
     137              : 
     138              : INLINE GEN
     139       155900 : gc_stoi(pari_sp av, long x) { set_avma(av); return stoi(x); }
     140              : INLINE GEN
     141       467902 : gc_utoi(pari_sp av, ulong x) { set_avma(av); return utoi(x); }
     142              : INLINE GEN
     143      1155307 : gc_utoipos(pari_sp av, ulong x) { set_avma(av); return utoipos(x); }
     144              : 
     145              : INLINE GEN
     146  97171403543 : new_chunk(size_t x) /* x is a number of longs */
     147              : {
     148  97171403543 :   GEN z = ((GEN) avma) - x;
     149              :   CHECK_CTRLC
     150  97171403543 :   if (x > (avma-pari_mainstack->bot) / sizeof(long))
     151           18 :     new_chunk_resize(x);
     152  97171403525 :   set_avma((pari_sp)z);
     153              : #ifdef MEMSTEP
     154              :   if (DEBUGMEM>1 && pari_mainstack->memused != DISABLE_MEMUSED) {
     155              :     long d = (long)pari_mainstack->memused - (long)z;
     156              :     if (labs(d) > 4*MEMSTEP)
     157              :     {
     158              :       pari_mainstack->memused = (pari_sp)z;
     159              :       err_printf("...%4.0lf Mbytes used\n",
     160              :                 (pari_mainstack->top-pari_mainstack->memused)/1048576.);
     161              :     }
     162              :   }
     163              : #endif
     164  97127438598 :   return z;
     165              : }
     166              : 
     167              : INLINE char *
     168     46710822 : stack_malloc(size_t N)
     169              : {
     170     46710822 :   long n = nchar2nlong(N);
     171     46710844 :   return (char*)new_chunk(n);
     172              : }
     173              : 
     174              : INLINE char *
     175     55664617 : stack_malloc_align(size_t N, long k)
     176              : {
     177     55664617 :   ulong d = ((ulong)avma) % k, e = ((ulong)N) % k;
     178     55664617 :   if (d) (void)new_chunk(d/sizeof(long));
     179     55664617 :   if (e) N += k-e;
     180     55664617 :   return (char*) new_chunk(nchar2nlong(N));
     181              : }
     182              : 
     183              : INLINE char *
     184       110651 : stack_calloc(size_t N)
     185              : {
     186       110651 :   char *p = stack_malloc(N);
     187       110651 :   memset(p, 0, N); return p;
     188              : }
     189              : 
     190              : INLINE char *
     191         3325 : stack_calloc_align(size_t N, long k)
     192              : {
     193         3325 :   ulong d = ((ulong)avma) % k, e = ((ulong)N) % k;
     194         3325 :   if (d) (void)new_chunk(d/sizeof(long));
     195         3325 :   if (e) N += k-e;
     196         3325 :   return stack_calloc(N);
     197              : }
     198              : 
     199              : INLINE GEN
     200       856999 : newblock(size_t n)
     201       856999 : { return newblock_t(n, 0); }
     202              : 
     203              : /* cgetg(lg(x), typ(x)), set *lx. Implicit unsetisclone() */
     204              : INLINE GEN
     205   1490316638 : cgetg_copy(GEN x, long *plx) {
     206              :   GEN y;
     207   1490316638 :   *plx = lg(x); y = new_chunk((size_t)*plx);
     208   1490307232 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     209              : }
     210              : INLINE GEN
     211       430117 : cgetg_block(long x, long y)
     212              : {
     213       430117 :   GEN z = newblock((size_t)x);
     214       430098 :   z[0] = CLONEBIT | evaltyp(y) | evallg(x);
     215       430052 :   return z;
     216              : }
     217              : INLINE GEN
     218  26035407823 : cgetg(long x, long y)
     219              : {
     220  26035407823 :   GEN z = new_chunk((size_t)x);
     221  26020960326 :   z[0] = evaltyp(y) | evallg(x);
     222  26007916386 :   return z;
     223              : }
     224              : INLINE GEN
     225  28029343604 : cgeti(long x)
     226              : {
     227  28029343604 :   GEN z = new_chunk((size_t)x);
     228  27987559054 :   z[0] = evaltyp(t_INT) | evallg(x);
     229  27965935285 :   return z;
     230              : }
     231              : INLINE GEN
     232  16696054398 : cgetipos(long x)
     233              : {
     234  16696054398 :   GEN z = cgeti(x);
     235  16672369729 :   z[1] = evalsigne(1) | evallgefint(x);
     236  16672369729 :   return z;
     237              : }
     238              : INLINE GEN
     239    300963604 : cgetineg(long x)
     240              : {
     241    300963604 :   GEN z = cgeti(x);
     242    300957943 :   z[1] = evalsigne(-1) | evallgefint(x);
     243    300957943 :   return z;
     244              : }
     245              : INLINE GEN
     246        44717 : cgetr_block(long x)
     247              : {
     248        44717 :   long l = nbits2lg(x);
     249        44713 :   GEN z = newblock((size_t)l);
     250        44733 :   z[0] = CLONEBIT | evaltyp(t_REAL) | evallg(l);
     251        44733 :   return z;
     252              : }
     253              : INLINE GEN
     254   1940149895 : cgetr(long x)
     255              : {
     256   1940149895 :   long l = nbits2lg(x);
     257   1939929565 :   GEN z = new_chunk((size_t)l);
     258   1939373603 :   z[0] = evaltyp(t_REAL) | evallg(l);
     259   1939095268 :   return z;
     260              : }
     261              : 
     262              : /*******************************************************************/
     263              : /*                                                                 */
     264              : /*                     COPY, NEGATION, ABSOLUTE VALUE              */
     265              : /*                                                                 */
     266              : /*******************************************************************/
     267              : /* cannot do memcpy because sometimes x and y overlap */
     268              : INLINE GEN
     269   5276956281 : leafcopy(GEN x)
     270              : {
     271   5276956281 :   long lx = lg(x);
     272   5276956281 :   GEN y = new_chunk(lx); /* can't use cgetg_copy, in case x,y overlap */
     273  26974668394 :   while (--lx > 0) y[lx] = x[lx];
     274   5276275088 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     275              : }
     276              : INLINE GEN
     277   9291885219 : icopy(GEN x)
     278              : {
     279   9291885219 :   long i = lgefint(x), lx = i;
     280   9291885219 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     281  41607168348 :   while (--i > 0) y[i] = x[i];
     282   9288348233 :   y[0] = evaltyp(t_INT) | evallg(lx);
     283   9304396222 :   return y;
     284              : }
     285              : INLINE GEN
     286    119683595 : icopyspec(GEN x, long nx)
     287              : {
     288    119683595 :   long i = nx+2, lx = i;
     289    119683595 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     290   3141642872 :   x -= 2; while (--i >= 2) y[i] = x[i];
     291    119682627 :   y[1] = evalsigne(1) | evallgefint(lx);
     292    119682627 :   y[0] = evaltyp(t_INT) | evallg(lx);
     293    119682792 :   return y;
     294              : }
     295   1104107462 : INLINE GEN rcopy(GEN x) { return leafcopy(x); }
     296          665 : INLINE GEN mpcopy(GEN x) { return leafcopy(x); }
     297              : 
     298              : INLINE GEN
     299   2117372251 : mpabs(GEN x) { GEN y = leafcopy(x); setabssign(y); return y; }
     300              : INLINE GEN
     301     13428280 : mpabs_shallow(GEN x) { return signe(x) < 0? mpabs(x): x; }
     302   2075304645 : INLINE GEN absi(GEN x) { return mpabs(x); }
     303     60448950 : INLINE GEN absi_shallow(GEN x) { return signe(x) < 0? negi(x): x; }
     304        20510 : INLINE GEN absr(GEN x) { return mpabs(x); }
     305              : 
     306              : INLINE GEN
     307    951158967 : mpneg(GEN x) { GEN y = leafcopy(x); togglesign(y); return y; }
     308    660935628 : INLINE GEN negi(GEN x) { return mpneg(x); }
     309      3959485 : INLINE GEN negr(GEN x) { return mpneg(x); }
     310              : 
     311              : /* negate in place */
     312              : INLINE void
     313   1992616779 : togglesign(GEN x) { if (x[1] & SIGNBITS) { x[1] ^= HIGHBIT; } }
     314              : INLINE void
     315   2185975216 : setabssign(GEN x) { x[1] &= ~HIGHBIT; }
     316              : /* negate in place, except universal constants */
     317              : INLINE void
     318    157383504 : togglesign_safe(GEN *px)
     319              : {
     320    157383504 :   switch(*px - gen_1) /* gen_1, gen_2, gen_m1, gen_m2 */
     321              :   {
     322      3671723 :     case 0: *px = gen_m1; break;
     323            4 :     case 3: *px = gen_m2;  break;
     324       846494 :     case 6: *px = gen_1; break;
     325            0 :     case 9: *px = gen_2;  break;
     326    152865283 :     default: togglesign(*px);
     327              :   }
     328    157393332 : }
     329              : /* setsigne(y, signe(x)) */
     330              : INLINE void
     331            0 : affectsign(GEN x, GEN y)
     332              : {
     333            0 :   y[1] = (x[1] & SIGNBITS) | (y[1] & ~SIGNBITS);
     334            0 : }
     335              : /* copies sign in place, except for universal constants */
     336              : INLINE void
     337     10753723 : affectsign_safe(GEN x, GEN *py)
     338              : {
     339     10753723 :   if (((*py)[1] ^ x[1]) & HIGHBIT) togglesign_safe(py);
     340     10753723 : }
     341              : /*******************************************************************/
     342              : /*                                                                 */
     343              : /*                     GEN -> LONG, LONG -> GEN                    */
     344              : /*                                                                 */
     345              : /*******************************************************************/
     346              : /* assume x != 0, return -x as a t_INT */
     347              : INLINE GEN
     348    300044525 : utoineg(ulong x) { GEN y = cgetineg(3); y[2] = x; return y; }
     349              : /* assume x != 0, return utoi(x) */
     350              : INLINE GEN
     351  14478385391 : utoipos(ulong x) { GEN y = cgetipos(3); y[2] = x; return y; }
     352              : INLINE GEN
     353  12244848705 : utoi(ulong x) { return x? utoipos(x): gen_0; }
     354              : INLINE GEN
     355    799573472 : stoi(long x)
     356              : {
     357    799573472 :   if (!x) return gen_0;
     358    554767682 :   return x > 0? utoipos((ulong)x): utoineg((ulong)-x);
     359              : }
     360              : 
     361              : /* x 2^BIL + y */
     362              : INLINE GEN
     363   9073323923 : uutoi(ulong x, ulong y)
     364              : {
     365              :   GEN z;
     366   9073323923 :   if (!x) return utoi(y);
     367    871973600 :   z = cgetipos(4);
     368    880993696 :   *int_W_lg(z, 1, 4) = x;
     369    880993696 :   *int_W_lg(z, 0, 4) = y; return z;
     370              : }
     371              : /* - (x 2^BIL + y) */
     372              : INLINE GEN
     373       362928 : uutoineg(ulong x, ulong y)
     374              : {
     375              :   GEN z;
     376       362928 :   if (!x) return y? utoineg(y): gen_0;
     377        16534 :   z = cgetineg(4);
     378        16534 :   *int_W_lg(z, 1, 4) = x;
     379        16534 :   *int_W_lg(z, 0, 4) = y; return z;
     380              : }
     381              : 
     382              : INLINE long
     383    477589393 : itos(GEN x)
     384              : {
     385    477589393 :   long s = signe(x);
     386              :   long u;
     387              : 
     388    477589393 :   if (!s) return 0;
     389    445694321 :   u = x[2];
     390    445694321 :   if (lgefint(x) > 3 || u < 0)
     391           30 :     pari_err_OVERFLOW("t_INT-->long assignment");
     392    445698306 :   return (s>0) ? u : -u;
     393              : }
     394              : /* as itos, but return 0 if too large. Cf is_bigint */
     395              : INLINE long
     396     44906230 : itos_or_0(GEN x) {
     397              :   long n;
     398     44906230 :   if (lgefint(x) != 3 || (n = x[2]) & HIGHBIT) return 0;
     399     38498060 :   return signe(x) > 0? n: -n;
     400              : }
     401              : INLINE ulong
     402    180889432 : itou(GEN x)
     403              : {
     404    180889432 :   switch(lgefint(x)) {
     405     13843877 :     case 2: return 0;
     406    167046062 :     case 3: return x[2];
     407            0 :     default:
     408            0 :       pari_err_OVERFLOW("t_INT-->ulong assignment");
     409              :       return 0; /* LCOV_EXCL_LINE */
     410              :   }
     411              : }
     412              : 
     413              : /* as itou, but return 0 if too large. Cf is_bigint */
     414              : INLINE ulong
     415      4917772 : itou_or_0(GEN x) {
     416      4917772 :   if (lgefint(x) != 3) return 0;
     417      4896693 :   return (ulong)x[2];
     418              : }
     419              : 
     420              : INLINE ulong
     421      6323185 : umuluu_or_0(ulong x, ulong y)
     422              : {
     423              :   ulong z;
     424              :   LOCAL_HIREMAINDER;
     425      6323185 :   z = mulll(x, y);
     426      6323185 :   return hiremainder? 0: z;
     427              : }
     428              : /* return x*y if <= n, else 0. Beware overflow */
     429              : INLINE ulong
     430      7618608 : umuluu_le(ulong x, ulong y, ulong n)
     431              : {
     432              :   ulong z;
     433              :   LOCAL_HIREMAINDER;
     434      7618608 :   z = mulll(x, y);
     435      7618608 :   return (hiremainder || z > n)? 0: z;
     436              : }
     437              : 
     438              : INLINE GEN
     439    500663133 : real_0_bit(long bitprec) { GEN x=cgetg(2, t_REAL); x[1]=evalexpo(bitprec); return x; }
     440              : INLINE GEN
     441      1160676 : real_0(long prec) { return real_0_bit(-prec); }
     442              : INLINE GEN
     443      4955122 : real_1_bit(long bit) { return real_1(nbits2prec(bit)); }
     444              : INLINE GEN
     445    144068859 : real_1(long prec) {
     446    144068859 :   GEN x = cgetr(prec);
     447    144048944 :   long i, l = lg(x);
     448    144048944 :   x[1] = evalsigne(1) | _evalexpo(0);
     449    710042021 :   x[2] = (long)HIGHBIT; for (i=3; i<l; i++) x[i] = 0;
     450    144048944 :   return x;
     451              : }
     452              : INLINE GEN
     453          455 : real_m1(long prec) {
     454          455 :   GEN x = cgetr(prec);
     455          455 :   long i, l = lg(x);
     456          455 :   x[1] = evalsigne(-1) | _evalexpo(0);
     457         1761 :   x[2] = (long)HIGHBIT; for (i=3; i<l; i++) x[i] = 0;
     458          455 :   return x;
     459              : }
     460              : 
     461              : /* 2.^n */
     462              : INLINE GEN
     463      1070066 : real2n(long n, long prec) { GEN z = real_1(prec); setexpo(z, n); return z; }
     464              : INLINE GEN
     465          126 : real_m2n(long n, long prec) { GEN z = real_m1(prec); setexpo(z, n); return z; }
     466              : INLINE GEN
     467    507137375 : stor(long s, long prec) { GEN z = cgetr(prec); affsr(s,z); return z; }
     468              : INLINE GEN
     469     13344535 : utor(ulong s, long prec){ GEN z = cgetr(prec); affur(s,z); return z; }
     470              : INLINE GEN
     471    749227019 : itor(GEN x, long prec) { GEN z = cgetr(prec); affir(x,z); return z; }
     472              : INLINE GEN
     473    308719645 : rtor(GEN x, long prec) { GEN z = cgetr(prec); affrr(x,z); return z; }
     474              : 
     475              : INLINE ulong
     476     20964974 : int_bit(GEN x, long n)
     477              : {
     478     20964974 :   long r, q = dvmdsBIL(n, &r);
     479     20952689 :   return q < lgefint(x)-2?((ulong)*int_W(x,q) >> r) & 1UL:0;
     480              : }
     481              : 
     482              : /*******************************************************************/
     483              : /*                                                                 */
     484              : /*                           COMPARISON                            */
     485              : /*                                                                 */
     486              : /*******************************************************************/
     487              : INLINE int
     488      1416033 : cmpss(long a, long b)
     489      1416033 : { return a>b? 1: (a<b? -1: 0); }
     490              : 
     491              : INLINE int
     492    317914605 : cmpuu(ulong a, ulong b)
     493    317914605 : { return a>b? 1: (a<b? -1: 0); }
     494              : 
     495              : INLINE int
     496      1730021 : cmpir(GEN x, GEN y)
     497              : {
     498              :   pari_sp av;
     499              :   GEN z;
     500              : 
     501      1730021 :   if (!signe(x)) return -signe(y);
     502       421436 :   if (!signe(y))
     503              :   {
     504         6182 :     if (expo(y) >= expi(x)) return 0;
     505         6154 :     return signe(x);
     506              :   }
     507       415254 :   av=avma; z = itor(x, realprec(y)); set_avma(av);
     508       415254 :   return cmprr(z,y); /* cmprr does no memory adjustment */
     509              : }
     510              : INLINE int
     511       303854 : cmpri(GEN x, GEN y) { return -cmpir(y,x); }
     512              : INLINE int
     513       796208 : cmpsr(long x, GEN y)
     514              : {
     515              :   pari_sp av;
     516              :   GEN z;
     517              : 
     518       796208 :   if (!x) return -signe(y);
     519       796208 :   av=avma; z = stor(x, LOWDEFAULTPREC); set_avma(av);
     520       796219 :   return cmprr(z,y);
     521              : }
     522              : INLINE int
     523        40996 : cmprs(GEN x, long y) { return -cmpsr(y,x); }
     524              : /* compare x and y */
     525              : INLINE int
     526     12976944 : cmpui(ulong x, GEN y)
     527              : {
     528              :   ulong p;
     529     12976944 :   if (!x) return -signe(y);
     530     12976944 :   if (signe(y) <= 0) return 1;
     531     12848774 :   if (lgefint(y) > 3) return -1;
     532     12575969 :   p = y[2]; if (p == x) return 0;
     533     12472079 :   return p < x ? 1 : -1;
     534              : }
     535              : INLINE int
     536     12976792 : cmpiu(GEN x, ulong y) { return -cmpui(y,x); }
     537              : /* compare x and |y| */
     538              : INLINE int
     539     32490467 : abscmpui(ulong x, GEN y)
     540              : {
     541     32490467 :   long l = lgefint(y);
     542              :   ulong p;
     543              : 
     544     32490467 :   if (!x) return (l > 2)? -1: 0;
     545     32490446 :   if (l == 2) return 1;
     546     32311912 :   if (l > 3) return -1;
     547     32290301 :   p = y[2]; if (p == x) return 0;
     548     31358727 :   return p < x ? 1 : -1;
     549              : }
     550              : INLINE int
     551     32490452 : abscmpiu(GEN x, ulong y) { return -abscmpui(y,x); }
     552              : INLINE int
     553      6225100 : cmpsi(long x, GEN y)
     554              : {
     555              :   ulong p;
     556              : 
     557      6225100 :   if (!x) return -signe(y);
     558              : 
     559      6220887 :   if (x > 0)
     560              :   {
     561      6219851 :     if (signe(y)<=0) return 1;
     562      6208910 :     if (lgefint(y)>3) return -1;
     563      6203007 :     p = y[2]; if (p == (ulong)x) return 0;
     564      6132849 :     return p < (ulong)x ? 1 : -1;
     565              :   }
     566              : 
     567         1036 :   if (signe(y)>=0) return -1;
     568          119 :   if (lgefint(y)>3) return 1;
     569          119 :   p = y[2]; if (p == (ulong)-x) return 0;
     570           14 :   return p < (ulong)(-x) ? -1 : 1;
     571              : }
     572              : INLINE int
     573      5993101 : cmpis(GEN x, long y) { return -cmpsi(y,x); }
     574              : INLINE int
     575      2163054 : mpcmp(GEN x, GEN y)
     576              : {
     577      2163054 :   if (typ(x)==t_INT)
     578        70433 :     return (typ(y)==t_INT) ? cmpii(x,y) : cmpir(x,y);
     579      2092621 :   return (typ(y)==t_INT) ? -cmpir(y,x) : cmprr(x,y);
     580              : }
     581              : 
     582              : /* x == y ? */
     583              : INLINE int
     584      3042274 : equalui(ulong x, GEN y)
     585              : {
     586      3042274 :   if (!x) return !signe(y);
     587      3041581 :   if (signe(y) <= 0 || lgefint(y) != 3) return 0;
     588      3030020 :   return ((ulong)y[2] == (ulong)x);
     589              : }
     590              : /* x == y ? */
     591              : INLINE int
     592      1150101 : equalsi(long x, GEN y)
     593              : {
     594      1150101 :   if (!x) return !signe(y);
     595      1150101 :   if (x > 0)
     596              :   {
     597      1142765 :     if (signe(y) <= 0 || lgefint(y) != 3) return 0;
     598      1065143 :     return ((ulong)y[2] == (ulong)x);
     599              :   }
     600         7336 :   if (signe(y) >= 0 || lgefint(y) != 3) return 0;
     601         4885 :   return ((ulong)y[2] == (ulong)-x);
     602              : }
     603              : /* x == |y| ? */
     604              : INLINE int
     605     47338132 : absequalui(ulong x, GEN y)
     606              : {
     607     47338132 :   if (!x) return !signe(y);
     608     47338132 :   return (lgefint(y) == 3 && (ulong)y[2] == x);
     609              : }
     610              : INLINE int
     611     45588367 : absequaliu(GEN x, ulong y) { return absequalui(y,x); }
     612              : INLINE int
     613      1149919 : equalis(GEN x, long y) { return equalsi(y,x); }
     614              : INLINE int
     615      3042273 : equaliu(GEN x, ulong y) { return equalui(y,x); }
     616              : 
     617              : /* assume x != 0, is |x| == 2^n ? */
     618              : INLINE int
     619      1842392 : absrnz_equal2n(GEN x) {
     620      1842392 :   if ((ulong)x[2]==HIGHBIT)
     621              :   {
     622       206722 :     long i, lx = lg(x);
     623       615847 :     for (i = 3; i < lx; i++)
     624       424774 :       if (x[i]) return 0;
     625       191073 :     return 1;
     626              :   }
     627      1635670 :   return 0;
     628              : }
     629              : /* assume x != 0, is |x| == 1 ? */
     630              : INLINE int
     631      4559163 : absrnz_equal1(GEN x) { return !expo(x) && absrnz_equal2n(x); }
     632              : 
     633              : INLINE long
     634   7346188144 : maxss(long x, long y) { return x>y?x:y; }
     635              : INLINE long
     636   2051046085 : minss(long x, long y) { return x<y?x:y; }
     637              : INLINE long
     638     69396375 : minuu(ulong x, ulong y) { return x<y?x:y; }
     639              : INLINE long
     640      6600171 : maxuu(ulong x, ulong y) { return x>y?x:y; }
     641              : INLINE double
     642      3218138 : maxdd(double x, double y) { return x>y?x:y; }
     643              : INLINE double
     644       265750 : mindd(double x, double y) { return x<y?x:y; }
     645              : 
     646              : /*******************************************************************/
     647              : /*                                                                 */
     648              : /*                             ADD / SUB                           */
     649              : /*                                                                 */
     650              : /*******************************************************************/
     651              : INLINE GEN
     652        25081 : subuu(ulong x, ulong y)
     653              : {
     654              :   ulong z;
     655              :   LOCAL_OVERFLOW;
     656        25081 :   z = subll(x, y);
     657        25081 :   return overflow? utoineg(-z): utoi(z);
     658              : }
     659              : INLINE GEN
     660   3515533994 : adduu(ulong x, ulong y) { ulong t = x+y; return uutoi((t < x), t); }
     661              : 
     662              : INLINE GEN
     663        25081 : addss(long x, long y)
     664              : {
     665        25081 :   if (!x) return stoi(y);
     666        25081 :   if (!y) return stoi(x);
     667        25081 :   if (x > 0) return y > 0? adduu(x,y): subuu(x, -y);
     668              : 
     669        25081 :   if (y > 0) return subuu(y, -x);
     670              :   else { /* - adduu(-x, -y) */
     671            0 :     ulong t = (-x)+(-y); return uutoineg((t < (ulong)(-x)), t);
     672              :   }
     673              : }
     674        25081 : INLINE GEN subss(long x, long y) { return addss(-y,x); }
     675              : 
     676              : INLINE GEN
     677   7943666188 : subii(GEN x, GEN y)
     678              : {
     679   7943666188 :   if (x==y) return gen_0; /* frequent with x = y = gen_0 */
     680   6585442893 :   return addii_sign(x, signe(x), y, -signe(y));
     681              : }
     682              : INLINE GEN
     683  12797283763 : addii(GEN x, GEN y) { return addii_sign(x, signe(x), y, signe(y)); }
     684              : INLINE GEN
     685   3015236853 : addrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, signe(y)); }
     686              : INLINE GEN
     687   1061170904 : subrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, -signe(y)); }
     688              : INLINE GEN
     689    490226678 : addir(GEN x, GEN y) { return addir_sign(x, signe(x), y, signe(y)); }
     690              : INLINE GEN
     691      3030806 : subir(GEN x, GEN y) { return addir_sign(x, signe(x), y, -signe(y)); }
     692              : INLINE GEN
     693     14332935 : subri(GEN x, GEN y) { return addir_sign(y, -signe(y), x, signe(x)); }
     694              : INLINE GEN
     695    320223636 : addsi(long x, GEN y) { return addsi_sign(x, y, signe(y)); }
     696              : INLINE GEN
     697    105566106 : addui(ulong x, GEN y) { return addui_sign(x, y, signe(y)); }
     698              : INLINE GEN
     699      6244858 : subsi(long x, GEN y) { return addsi_sign(x, y, -signe(y)); }
     700              : INLINE GEN
     701    138440445 : subui(ulong x, GEN y) { return addui_sign(x, y, -signe(y)); }
     702              : 
     703              : /*******************************************************************/
     704              : /*                                                                 */
     705              : /*                           MOD, REM, DIV                         */
     706              : /*                                                                 */
     707              : /*******************************************************************/
     708    113262528 : INLINE ulong mod2BIL(GEN x) { return *int_LSW(x); }
     709            0 : INLINE long mod64(GEN x) { return mod2BIL(x) & 63; }
     710          259 : INLINE long mod32(GEN x) { return mod2BIL(x) & 31; }
     711       308665 : INLINE long mod16(GEN x) { return mod2BIL(x) & 15; }
     712     15663637 : INLINE long mod8(GEN x)  { return mod2BIL(x) & 7; }
     713      7468987 : INLINE long mod4(GEN x)  { return mod2BIL(x) & 3; }
     714     65773730 : INLINE long mod2(GEN x)  { return mod2BIL(x) & 1; }
     715              : INLINE int
     716    118006257 : mpodd(GEN x) { return signe(x) && mod2(x); }
     717              : /* x mod 2^n, n < BITS_IN_LONG */
     718              : INLINE ulong
     719     57838302 : umodi2n(GEN x, long n)
     720              : {
     721     57838302 :   long s = signe(x);
     722     57838302 :   const ulong _2n = 1UL << n;
     723              :   ulong m;
     724     57838302 :   if (!s) return 0;
     725     56109090 :   m = *int_LSW(x) & (_2n - 1);
     726     56109090 :   if (s < 0 && m) m = _2n - m;
     727     56109090 :   return m;
     728              : }
     729            0 : INLINE ulong Mod64(GEN x){ return umodi2n(x,6); }
     730       311157 : INLINE ulong Mod32(GEN x){ return umodi2n(x,5); }
     731       389789 : INLINE ulong Mod16(GEN x){ return umodi2n(x,4); }
     732      3246107 : INLINE ulong Mod8(GEN x) { return umodi2n(x,3); }
     733     50359121 : INLINE ulong Mod4(GEN x) { return umodi2n(x,2); }
     734      3532973 : INLINE ulong Mod2(GEN x) { return umodi2n(x,1); }
     735              : 
     736              : INLINE GEN
     737     54866076 : truedivii(GEN a,GEN b) { return truedvmdii(a,b,NULL); }
     738              : INLINE GEN
     739       268452 : truedivis(GEN a, long b) { return truedvmdis(a,b,NULL); }
     740              : INLINE GEN
     741      6202318 : truedivsi(long a, GEN b) { return truedvmdsi(a,b,NULL); }
     742              : 
     743              : INLINE GEN
     744     15814803 : divii(GEN a, GEN b) { return dvmdii(a,b,NULL); }
     745              : INLINE GEN
     746   3075978589 : remii(GEN a, GEN b) { return dvmdii(a,b,ONLY_REM); }
     747              : 
     748              : INLINE GEN
     749            0 : divss(long x, long y) { return stoi(x / y); }
     750              : INLINE GEN
     751            0 : modss(long x, long y) { return utoi(smodss(x, y)); }
     752              : INLINE GEN
     753            0 : remss(long x, long y) { return stoi(x % y); }
     754              : INLINE long
     755     12458511 : smodss(long x, long y)
     756              : {
     757     12458511 :   long r = x%y;
     758     12458511 :   return (r >= 0)? r: labs(y) + r;
     759              : }
     760              : INLINE ulong
     761    734367662 : umodsu(long x, ulong y)
     762              : {
     763    734367662 :   return x>=0 ? x%y: Fl_neg((-x)%y, y);
     764              : }
     765              : 
     766              : INLINE long
     767            0 : sdivss_rem(long x, long y, long *r)
     768              : {
     769              :   long q;
     770              :   LOCAL_HIREMAINDER;
     771            0 :   if (!y) pari_err_INV("sdivss_rem",gen_0);
     772            0 :   hiremainder = 0; q = divll((ulong)labs(x),(ulong)labs(y));
     773            0 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     774            0 :   if (y < 0) q = -q;
     775            0 :   *r = hiremainder; return q;
     776              : }
     777              : INLINE GEN
     778            0 : divss_rem(long x, long y, long *r) { return stoi(sdivss_rem(x,y,r)); }
     779              : INLINE ulong
     780      2418435 : udivuu_rem(ulong x, ulong y, ulong *r)
     781              : {
     782      2418435 :   if (!y) pari_err_INV("udivuu_rem",gen_0);
     783      2418435 :   *r = x % y; return x / y;
     784              : }
     785              : INLINE ulong
     786      3914930 : ceildivuu(ulong a, ulong b)
     787              : {
     788              :   ulong c;
     789      3914930 :   if (!a) return 0;
     790      3730945 :   c = a / b; return (a % b)? c+1: c;
     791              : }
     792              : 
     793              : INLINE ulong
     794       128885 : uabsdivui_rem(ulong x, GEN y, ulong *r)
     795              : {
     796       128885 :   long q, s = signe(y);
     797              :   LOCAL_HIREMAINDER;
     798              : 
     799       128885 :   if (!s) pari_err_INV("uabsdivui_rem",gen_0);
     800       128885 :   if (!x || lgefint(y)>3) { *r = x; return 0; }
     801       128178 :   hiremainder=0; q = (long)divll(x, (ulong)y[2]);
     802       128178 :   if (s < 0) q = -q;
     803       128178 :   *r = hiremainder; return q;
     804              : }
     805              : 
     806              : /* assume d != 0 and |n| / d can be represented as an ulong.
     807              :  * Return |n|/d, set *r = |n| % d */
     808              : INLINE ulong
     809     25949953 : uabsdiviu_rem(GEN n, ulong d, ulong *r)
     810              : {
     811     25949953 :   switch(lgefint(n))
     812              :   {
     813            0 :     case 2: *r = 0; return 0;
     814     25949953 :     case 3:
     815              :     {
     816     25949953 :       ulong nn = n[2];
     817     25949953 :       *r = nn % d; return nn / d;
     818              :     }
     819            0 :     default: /* 4 */
     820              :     {
     821              :       ulong n1, n0, q;
     822              :       LOCAL_HIREMAINDER;
     823            0 :       n0 = *int_W(n,0);
     824            0 :       n1 = *int_W(n,1);
     825            0 :       hiremainder = n1;
     826            0 :       q = divll(n0, d);
     827            0 :       *r = hiremainder; return q;
     828              :     }
     829              :   }
     830              : }
     831              : 
     832              : INLINE long
     833     51446741 : sdivsi_rem(long x, GEN y, long *r)
     834              : {
     835     51446741 :   long q, s = signe(y);
     836              :   LOCAL_HIREMAINDER;
     837              : 
     838     51446741 :   if (!s) pari_err_INV("sdivsi_rem",gen_0);
     839     51446746 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) { *r = x; return 0; }
     840     49489315 :   hiremainder=0; q = (long)divll(labs(x), (ulong)y[2]);
     841     49489315 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     842     49489315 :   if (s < 0) q = -q;
     843     49489315 :   *r = hiremainder; return q;
     844              : }
     845              : INLINE GEN
     846            0 : divsi_rem(long s, GEN y, long *r) { return stoi(sdivsi_rem(s,y,r)); }
     847              : 
     848              : INLINE long
     849       102165 : sdivsi(long x, GEN y)
     850              : {
     851       102165 :   long q, s = signe(y);
     852              : 
     853       102165 :   if (!s) pari_err_INV("sdivsi",gen_0);
     854       102165 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) return 0;
     855       102060 :   q = labs(x) / y[2];
     856       102060 :   if (x < 0) q = -q;
     857       102060 :   if (s < 0) q = -q;
     858       102060 :   return q;
     859              : }
     860              : 
     861              : INLINE GEN
     862            0 : dvmdss(long x, long y, GEN *z)
     863              : {
     864              :   long r;
     865            0 :   GEN q = divss_rem(x,y, &r);
     866            0 :   *z = stoi(r); return q;
     867              : }
     868              : INLINE long
     869   7320091517 : dvmdsBIL(long n, long *r) { *r = remsBIL(n); return divsBIL(n); }
     870              : INLINE ulong
     871    166542017 : dvmduBIL(ulong n, ulong *r) { *r = remsBIL(n); return divsBIL(n); }
     872              : INLINE GEN
     873            0 : dvmdsi(long x, GEN y, GEN *z)
     874              : {
     875              :   long r;
     876            0 :   GEN q = divsi_rem(x,y, &r);
     877            0 :   *z = stoi(r); return q;
     878              : }
     879              : INLINE GEN
     880            0 : dvmdis(GEN x, long y, GEN *z)
     881              : {
     882              :   long r;
     883            0 :   GEN q = divis_rem(x,y, &r);
     884            0 :   *z = stoi(r); return q;
     885              : }
     886              : 
     887              : INLINE long
     888     21176116 : smodis(GEN x, long y)
     889              : {
     890     21176116 :   pari_sp av = avma;
     891     21176116 :   long r; (void)divis_rem(x,y, &r);
     892     21176116 :   return gc_long(av, (r >= 0)? r: labs(y) + r);
     893              : }
     894              : INLINE GEN
     895     19638526 : modis(GEN x, long y) { return stoi(smodis(x,y)); }
     896              : INLINE GEN
     897     45242132 : modsi(long x, GEN y) {
     898     45242132 :   long r; (void)sdivsi_rem(x, y, &r);
     899     45242131 :   return (r >= 0)? stoi(r): addsi_sign(r, y, 1);
     900              : }
     901              : 
     902              : INLINE ulong
     903       710986 : umodui(ulong x, GEN y)
     904              : {
     905       710986 :   if (!signe(y)) pari_err_INV("umodui",gen_0);
     906       710986 :   if (!x || lgefint(y) > 3) return x;
     907       414849 :   return x % (ulong)y[2];
     908              : }
     909              : 
     910              : INLINE ulong
     911     10312159 : ugcdiu(GEN x, ulong y) { return ugcd(umodiu(x,y), y); }
     912              : INLINE ulong
     913         2737 : ugcdui(ulong y, GEN x) { return ugcd(umodiu(x,y), y); }
     914              : 
     915              : INLINE GEN
     916            0 : remsi(long x, GEN y)
     917            0 : { long r; (void)sdivsi_rem(x,y, &r); return stoi(r); }
     918              : INLINE GEN
     919            0 : remis(GEN x, long y)
     920              : {
     921            0 :   pari_sp av = avma;
     922              :   long r;
     923            0 :   (void)divis_rem(x,y, &r); return gc_stoi(av, r);
     924              : }
     925              : 
     926              : INLINE GEN
     927            0 : rdivis(GEN x, long y, long prec)
     928              : {
     929            0 :   GEN z = cgetr(prec);
     930            0 :   pari_sp av = avma;
     931            0 :   affrr(divrs(itor(x,prec), y),z);
     932            0 :   set_avma(av); return z;
     933              : }
     934              : INLINE GEN
     935            0 : rdivsi(long x, GEN y, long prec)
     936              : {
     937            0 :   GEN z = cgetr(prec);
     938            0 :   pari_sp av = avma;
     939            0 :   affrr(divsr(x, itor(y,prec)), z);
     940            0 :   set_avma(av); return z;
     941              : }
     942              : INLINE GEN
     943       839647 : rdivss(long x, long y, long prec)
     944              : {
     945       839647 :   GEN z = cgetr(prec);
     946       839647 :   pari_sp av = avma;
     947       839647 :   affrr(divrs(stor(x, prec), y), z);
     948       839647 :   set_avma(av); return z;
     949              : }
     950              : 
     951              : INLINE void
     952     13163965 : rdiviiz(GEN x, GEN y, GEN z)
     953              : {
     954     13163965 :   long lz = lg(z), lx = lgefint(x), ly = lgefint(y);
     955     13163965 :   if (lx == 2) { affur(0, z); return; }
     956     13163965 :   if (ly == 3)
     957              :   {
     958      2256057 :     affir(x, z); if (signe(y) < 0) togglesign(z);
     959      2256049 :     affrr(divru(z, y[2]), z);
     960              :   }
     961     10907908 :   else if (lx > lz + 1 || ly > lz + 1)
     962              :   {
     963      5953779 :     affir(x,z); affrr(divri(z, y), z);
     964              :   }
     965              :   else
     966              :   {
     967      4954129 :     long b = lg2prec(lz) + expi(y) - expi(x) + 1;
     968      4954384 :     GEN q = divii(b > 0? shifti(x, b): x, y);
     969      4955046 :     affir(q, z); if (b > 0) shiftr_inplace(z, -b);
     970              :   }
     971     13171051 :   set_avma((ulong)z);
     972              : }
     973              : INLINE GEN
     974     13118604 : rdivii(GEN x, GEN y, long prec)
     975     13118604 : { GEN z = cgetr(prec); rdiviiz(x, y, z); return z; }
     976              : INLINE GEN
     977      7389424 : fractor(GEN x, long prec)
     978      7389424 : { return rdivii(gel(x,1), gel(x,2), prec); }
     979              : 
     980              : INLINE int
     981     17033246 : dvdii(GEN x, GEN y)
     982              : {
     983     17033246 :   pari_sp av = avma;
     984              :   GEN r;
     985     17033246 :   if (!signe(x)) return 1;
     986     15596512 :   if (!signe(y)) return 0;
     987     15596512 :   r = remii(x,y);
     988     15604524 :   return gc_bool(av, r == gen_0);
     989              : }
     990              : INLINE int
     991          371 : dvdsi(long x, GEN y)
     992              : {
     993          371 :   if (x == 0) return 1;
     994          266 :   if (!signe(y)) return 0;
     995          266 :   if (lgefint(y) != 3) return 0;
     996          259 :   return x % y[2] == 0;
     997              : }
     998              : INLINE int
     999       167545 : dvdui(ulong x, GEN y)
    1000              : {
    1001       167545 :   if (x == 0) return 1;
    1002       167545 :   if (!signe(y)) return 0;
    1003       167545 :   if (lgefint(y) != 3) return 0;
    1004       156924 :   return x % y[2] == 0;
    1005              : }
    1006              : INLINE int
    1007        33765 : dvdis(GEN x, long y)
    1008        33765 : { return y? smodis(x, y) == 0: signe(x) == 0; }
    1009              : INLINE int
    1010       576273 : dvdiu(GEN x, ulong y)
    1011       576273 : { return y? umodiu(x, y) == 0: signe(x) == 0; }
    1012              : 
    1013              : INLINE int
    1014            0 : dvdisz(GEN x, long y, GEN z)
    1015              : {
    1016            0 :   const pari_sp av = avma;
    1017              :   long r;
    1018            0 :   GEN p1 = divis_rem(x,y, &r);
    1019            0 :   set_avma(av); if (r) return 0;
    1020            0 :   affii(p1,z); return 1;
    1021              : }
    1022              : INLINE int
    1023            0 : dvdiuz(GEN x, ulong y, GEN z)
    1024              : {
    1025            0 :   const pari_sp av = avma;
    1026              :   ulong r;
    1027            0 :   GEN p1 = absdiviu_rem(x,y, &r);
    1028            0 :   set_avma(av); if (r) return 0;
    1029            0 :   affii(p1,z); return 1;
    1030              : }
    1031              : INLINE int
    1032            0 : dvdiiz(GEN x, GEN y, GEN z)
    1033              : {
    1034            0 :   const pari_sp av=avma;
    1035            0 :   GEN p2, p1 = dvmdii(x,y,&p2);
    1036            0 :   if (signe(p2)) return gc_bool(av,0);
    1037            0 :   affii(p1,z); return gc_bool(av,1);
    1038              : }
    1039              : 
    1040              : INLINE ulong
    1041     77355417 : remlll_pre(ulong u2, ulong u1, ulong u0, ulong n, ulong ninv)
    1042              : {
    1043     77355417 :   u1 = remll_pre(u2, u1, n, ninv);
    1044     78236147 :   return remll_pre(u1, u0, n, ninv);
    1045              : }
    1046              : 
    1047              : INLINE ulong
    1048   2483764914 : Fl_sqr_pre(ulong a, ulong p, ulong pi)
    1049              : {
    1050              :   ulong x;
    1051              :   LOCAL_HIREMAINDER;
    1052   2483764914 :   x = mulll(a,a);
    1053   2483764914 :   return remll_pre(hiremainder, x, p, pi);
    1054              : }
    1055              : 
    1056              : INLINE ulong
    1057   4331155704 : Fl_mul_pre(ulong a, ulong b, ulong p, ulong pi)
    1058              : {
    1059              :   ulong x;
    1060              :   LOCAL_HIREMAINDER;
    1061   4331155704 :   x = mulll(a,b);
    1062   4331155704 :   return remll_pre(hiremainder, x, p, pi);
    1063              : }
    1064              : 
    1065              : INLINE ulong
    1066   7771236344 : Fl_addmul_pre(ulong y0, ulong x0, ulong x1, ulong p, ulong pi)
    1067              : {
    1068              :   ulong l0, h0;
    1069              :   LOCAL_HIREMAINDER;
    1070   7771236344 :   hiremainder = y0;
    1071   7771236344 :   l0 = addmul(x0, x1); h0 = hiremainder;
    1072   7771236344 :   return remll_pre(h0, l0, p, pi);
    1073              : }
    1074              : 
    1075              : INLINE ulong
    1076     73185429 : Fl_addmulmul_pre(ulong x0, ulong y0, ulong x1, ulong y1, ulong p, ulong pi)
    1077              : {
    1078              :   ulong l0, l1, h0, h1;
    1079              :   LOCAL_OVERFLOW;
    1080              :   LOCAL_HIREMAINDER;
    1081     73185429 :   l0 = mulll(x0, y0); h0 = hiremainder;
    1082     73185429 :   l1 = mulll(x1, y1); h1 = hiremainder;
    1083     73185429 :   l0 = addll(l0, l1); h0 = addllx(h0, h1);
    1084     73185429 :   return overflow ? remlll_pre(1, h0, l0, p, pi): remll_pre(h0, l0, p, pi);
    1085              : }
    1086              : 
    1087              : INLINE ulong
    1088       242705 : Fl_ellj_pre(ulong a4, ulong a6, ulong p, ulong pi)
    1089              : {
    1090              :   /* a43 = 4 a4^3 */
    1091       242705 :   ulong a43 = Fl_double(Fl_double(
    1092              :               Fl_mul_pre(a4, Fl_sqr_pre(a4, p, pi), p, pi), p), p);
    1093              :   /* a62 = 27 a6^2 */
    1094       242710 :   ulong a62 = Fl_mul_pre(Fl_sqr_pre(a6, p, pi), 27 % p, p, pi);
    1095       242711 :   ulong z1 = Fl_mul_pre(a43, 1728 % p, p, pi);
    1096       242712 :   ulong z2 = Fl_add(a43, a62, p);
    1097       242713 :   return Fl_div(z1, z2, p);
    1098              : }
    1099              : 
    1100              : /*******************************************************************/
    1101              : /*                                                                 */
    1102              : /*                        MP (INT OR REAL)                         */
    1103              : /*                                                                 */
    1104              : /*******************************************************************/
    1105              : INLINE GEN
    1106           49 : mptrunc(GEN x) { return typ(x)==t_INT? icopy(x): truncr(x); }
    1107              : INLINE GEN
    1108            0 : mpfloor(GEN x) { return typ(x)==t_INT? icopy(x): floorr(x); }
    1109              : INLINE GEN
    1110            0 : mpceil(GEN x) { return typ(x)==t_INT? icopy(x): ceilr(x); }
    1111              : INLINE GEN
    1112      1216172 : mpround(GEN x) { return typ(x) == t_INT? icopy(x): roundr(x); }
    1113              : 
    1114              : INLINE long
    1115     37845644 : mpexpo(GEN x) { return typ(x) == t_INT? expi(x): expo(x); }
    1116              : 
    1117              : INLINE GEN
    1118    599109314 : mpadd(GEN x, GEN y)
    1119              : {
    1120    599109314 :   if (typ(x)==t_INT)
    1121     16455014 :     return (typ(y)==t_INT) ? addii(x,y) : addir(x,y);
    1122    582654300 :   return (typ(y)==t_INT) ? addir(y,x) : addrr(x,y);
    1123              : }
    1124              : INLINE GEN
    1125    273975351 : mpsub(GEN x, GEN y)
    1126              : {
    1127    273975351 :   if (typ(x)==t_INT)
    1128       519594 :     return (typ(y)==t_INT) ? subii(x,y) : subir(x,y);
    1129    273455757 :   return (typ(y)==t_INT) ? subri(x,y) : subrr(x,y);
    1130              : }
    1131              : INLINE GEN
    1132    890251703 : mpmul(GEN x, GEN y)
    1133              : {
    1134    890251703 :   if (typ(x)==t_INT)
    1135     38230156 :     return (typ(y)==t_INT) ? mulii(x,y) : mulir(x,y);
    1136    852021547 :   return (typ(y)==t_INT) ? mulir(y,x) : mulrr(x,y);
    1137              : }
    1138              : INLINE GEN
    1139     91844377 : mpsqr(GEN x) { return (typ(x)==t_INT) ? sqri(x) : sqrr(x); }
    1140              : 
    1141              : /* obsolete */
    1142              : INLINE GEN
    1143            0 : mpdiv(GEN x, GEN y)
    1144              : {
    1145            0 :   if (typ(x) == t_REAL) return divrmp(x,y);
    1146            0 :   if (typ(y) == t_REAL) return divmpr(x,y);
    1147            0 :   pari_err_TYPE2("mpdiv",x,y); return NULL;
    1148              : }
    1149              : INLINE GEN
    1150      1202393 : divrmp(GEN x, GEN y) { return (typ(y)==t_INT) ? divri(x,y) : divrr(x,y); }
    1151              : INLINE GEN
    1152         3759 : divmpr(GEN x, GEN y) { return (typ(x)==t_INT) ? divir(x,y) : divrr(x,y); }
    1153              : 
    1154              : /*******************************************************************/
    1155              : /*                                                                 */
    1156              : /*                          Z/nZ, n ULONG                          */
    1157              : /*                                                                 */
    1158              : /*******************************************************************/
    1159              : INLINE ulong
    1160    503823953 : Fl_double(ulong a, ulong p)
    1161              : {
    1162    503823953 :   ulong res = a << 1;
    1163    503823953 :   return (res >= p || res < a) ? res - p : res;
    1164              : }
    1165              : INLINE ulong
    1166     96955898 : Fl_triple(ulong a, ulong p)
    1167              : {
    1168     96955898 :   ulong res = a << 1;
    1169     96955898 :   if (res >= p || res < a) res -= p;
    1170     96955898 :   res += a;
    1171     96955898 :   return (res >= p || res < a)? res - p: res;
    1172              : }
    1173              : INLINE ulong
    1174     20468251 : Fl_halve(ulong a, ulong p)
    1175              : {
    1176              :   ulong ap, ap2;
    1177     20468251 :   if ((a&1UL)==0) return a>>1;
    1178     10292177 :   ap = a + p; ap2 = ap>>1;
    1179     10292177 :   return ap>=a ? ap2: (ap2|HIGHBIT);
    1180              : }
    1181              : 
    1182              : INLINE ulong
    1183   6376367648 : Fl_add(ulong a, ulong b, ulong p)
    1184              : {
    1185   6376367648 :   ulong res = a + b;
    1186   6376367648 :   return (res >= p || res < a) ? res - p : res;
    1187              : }
    1188              : INLINE ulong
    1189    738519888 : Fl_neg(ulong x, ulong p) { return x ? p - x: 0; }
    1190              : 
    1191              : INLINE ulong
    1192   7515940347 : Fl_sub(ulong a, ulong b, ulong p)
    1193              : {
    1194   7515940347 :   ulong res = a - b;
    1195   7515940347 :   return (res > a) ? res + p: res;
    1196              : }
    1197              : 
    1198              : /* centerlift(u mod p) */
    1199              : INLINE long
    1200      4490058 : Fl_center(ulong u, ulong p, ulong ps2) { return (long) (u > ps2)? u - p: u; }
    1201              : 
    1202              : INLINE ulong
    1203   2521615695 : Fl_mul(ulong a, ulong b, ulong p)
    1204              : {
    1205              :   ulong x;
    1206              :   LOCAL_HIREMAINDER;
    1207   2521615695 :   x = mulll(a,b);
    1208   2521615695 :   if (!hiremainder) return x % p;
    1209    430576393 :   (void)divll(x,p); return hiremainder;
    1210              : }
    1211              : INLINE ulong
    1212    107628564 : Fl_sqr(ulong a, ulong p)
    1213              : {
    1214              :   ulong x;
    1215              :   LOCAL_HIREMAINDER;
    1216    107628564 :   x = mulll(a,a);
    1217    107628564 :   if (!hiremainder) return x % p;
    1218     26890396 :   (void)divll(x,p); return hiremainder;
    1219              : }
    1220              : /* don't assume that p is prime: can't special case a = 0 */
    1221              : INLINE ulong
    1222     49478362 : Fl_div(ulong a, ulong b, ulong p)
    1223     49478362 : { return Fl_mul(a, Fl_inv(b, p), p); }
    1224              : 
    1225              : /*******************************************************************/
    1226              : /*                                                                 */
    1227              : /*        DEFINED FROM EXISTING ONE EXPLOITING COMMUTATIVITY       */
    1228              : /*                                                                 */
    1229              : /*******************************************************************/
    1230              : INLINE GEN
    1231      1111674 : addri(GEN x, GEN y) { return addir(y,x); }
    1232              : INLINE GEN
    1233    195036761 : addis(GEN x, long s) { return addsi(s,x); }
    1234              : INLINE GEN
    1235    101493246 : addiu(GEN x, ulong s) { return addui(s,x); }
    1236              : INLINE GEN
    1237     12387478 : addrs(GEN x, long s) { return addsr(s,x); }
    1238              : 
    1239              : INLINE GEN
    1240    133445784 : subiu(GEN x, long y) { GEN z = subui(y, x); togglesign(z); return z; }
    1241              : INLINE GEN
    1242       171754 : subis(GEN x, long y) { return addsi(-y,x); }
    1243              : INLINE GEN
    1244     16572332 : subrs(GEN x, long y) { return addsr(-y,x); }
    1245              : 
    1246              : INLINE GEN
    1247    470628730 : mulis(GEN x, long s) { return mulsi(s,x); }
    1248              : INLINE GEN
    1249    378601251 : muliu(GEN x, ulong s) { return mului(s,x); }
    1250              : INLINE GEN
    1251      2765059 : mulru(GEN x, ulong s) { return mulur(s,x); }
    1252              : INLINE GEN
    1253     53047296 : mulri(GEN x, GEN s) { return mulir(s,x); }
    1254              : INLINE GEN
    1255      7181596 : mulrs(GEN x, long s) { return mulsr(s,x); }
    1256              : 
    1257              : /*******************************************************************/
    1258              : /*                                                                 */
    1259              : /*                  VALUATION, EXPONENT, SHIFTS                    */
    1260              : /*                                                                 */
    1261              : /*******************************************************************/
    1262              : INLINE long
    1263    193208775 : vali(GEN x)
    1264              : {
    1265              :   long i;
    1266              :   GEN xp;
    1267              : 
    1268    193208775 :   if (!signe(x)) return -1;
    1269    193123930 :   xp=int_LSW(x);
    1270    201963213 :   for (i=0; !*xp; i++) xp=int_nextW(xp);
    1271    193123930 :   return vals(*xp) + i * BITS_IN_LONG;
    1272              : }
    1273              : 
    1274              : /* assume x > 0 */
    1275              : INLINE long
    1276    978736324 : expu(ulong x) { return (BITS_IN_LONG-1) - (long)bfffo(x); }
    1277              : 
    1278              : INLINE long
    1279   2483086577 : expi(GEN x)
    1280              : {
    1281   2483086577 :   const long lx=lgefint(x);
    1282   2483086577 :   return lx==2? -(long)HIGHEXPOBIT: bit_accuracy(lx)-(long)bfffo(*int_MSW(x))-1;
    1283              : }
    1284              : 
    1285              : INLINE GEN
    1286    363418308 : shiftr(GEN x, long n)
    1287              : {
    1288    363418308 :   const long e = evalexpo(expo(x)+n);
    1289    363396949 :   const GEN y = rcopy(x);
    1290              : 
    1291    363321102 :   if (e & ~EXPOBITS) pari_err_OVERFLOW("expo()");
    1292    363323888 :   y[1] = (y[1]&~EXPOBITS) | e; return y;
    1293              : }
    1294              : INLINE GEN
    1295    153542913 : mpshift(GEN x,long s) { return (typ(x)==t_INT)?shifti(x,s):shiftr(x,s); }
    1296              : 
    1297              : /* FIXME: adapt/use mpn_[lr]shift instead */
    1298              : /* z2[imin..imax] := z1[imin..imax].f shifted left sh bits
    1299              :  * (feeding f from the right). Assume sh > 0 */
    1300              : INLINE void
    1301   7817485373 : shift_left(GEN z2, GEN z1, long imin, long imax, ulong f,  ulong sh)
    1302              : {
    1303   7817485373 :   GEN sb = z1 + imin, se = z1 + imax, te = z2 + imax;
    1304   7817485373 :   ulong l, m = BITS_IN_LONG - sh, k = f >> m;
    1305  49967887926 :   while (se > sb) {
    1306  42150402553 :     l     = *se--;
    1307  42150402553 :     *te-- = (l << sh) | k;
    1308  42150402553 :     k     = l >> m;
    1309              :   }
    1310   7817485373 :   *te = (((ulong)*se) << sh) | k;
    1311   7817485373 : }
    1312              : /* z2[imin..imax] := f.z1[imin..imax-1] shifted right sh bits
    1313              :  * (feeding f from the left). Assume sh > 0 */
    1314              : INLINE void
    1315   5898456570 : shift_right(GEN z2, GEN z1, long imin, long imax, ulong f, ulong sh)
    1316              : {
    1317   5898456570 :   GEN sb = z1 + imin, se = z1 + imax, tb = z2 + imin;
    1318   5898456570 :   ulong k, l = *sb++, m = BITS_IN_LONG - sh;
    1319   5898456570 :   *tb++ = (l >> sh) | (f << m);
    1320  30835152549 :   while (sb < se) {
    1321  24936695979 :     k     = l << m;
    1322  24936695979 :     l     = *sb++;
    1323  24936695979 :     *tb++ = (l >> sh) | k;
    1324              :   }
    1325   5898456570 : }
    1326              : 
    1327              : /* Backward compatibility. Inefficient && unused */
    1328              : extern ulong hiremainder;
    1329              : INLINE ulong
    1330            0 : shiftl(ulong x, ulong y)
    1331            0 : { hiremainder = x>>(BITS_IN_LONG-y); return (x<<y); }
    1332              : 
    1333              : INLINE ulong
    1334            0 : shiftlr(ulong x, ulong y)
    1335            0 : { hiremainder = x<<(BITS_IN_LONG-y); return (x>>y); }
    1336              : 
    1337              : INLINE void
    1338    470318458 : shiftr_inplace(GEN z, long d)
    1339              : {
    1340    470318458 :   setexpo(z, expo(z)+d);
    1341    470134721 : }
    1342              : 
    1343              : /*******************************************************************/
    1344              : /*                                                                 */
    1345              : /*                           ASSIGNMENT                            */
    1346              : /*                                                                 */
    1347              : /*******************************************************************/
    1348              : INLINE void
    1349    992897810 : affii(GEN x, GEN y)
    1350              : {
    1351    992897810 :   long lx = lgefint(x);
    1352    992897810 :   if (lg(y)<lx) pari_err_OVERFLOW("t_INT-->t_INT assignment");
    1353  38487130119 :   while (--lx) y[lx] = x[lx];
    1354    993018103 : }
    1355              : INLINE void
    1356      6484482 : affsi(long s, GEN x)
    1357              : {
    1358      6484482 :   if (!s) x[1] = evalsigne(0) | evallgefint(2);
    1359              :   else
    1360              :   {
    1361      6208015 :     if (s > 0) { x[1] = evalsigne( 1) | evallgefint(3); x[2] =  s; }
    1362      2082079 :     else       { x[1] = evalsigne(-1) | evallgefint(3); x[2] = -s; }
    1363              :   }
    1364      6484482 : }
    1365              : INLINE void
    1366     47850644 : affui(ulong u, GEN x)
    1367              : {
    1368     47850644 :   if (!u) x[1] = evalsigne(0) | evallgefint(2);
    1369     47811346 :   else  { x[1] = evalsigne(1) | evallgefint(3); x[2] = u; }
    1370     47850644 : }
    1371              : 
    1372              : INLINE void
    1373    506826595 : affsr(long x, GEN y)
    1374              : {
    1375    506826595 :   long sh, i, ly = lg(y);
    1376              : 
    1377    506826595 :   if (!x)
    1378              :   {
    1379            0 :     y[1] = evalexpo(-bit_accuracy(ly));
    1380            0 :     return;
    1381              :   }
    1382    506826595 :   if (x < 0) {
    1383        13805 :     x = -x; sh = bfffo(x);
    1384        13805 :     y[1] = evalsigne(-1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1385              :   }
    1386              :   else
    1387              :   {
    1388    506812790 :     sh = bfffo(x);
    1389    506812790 :     y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1390              :   }
    1391   5174076603 :   y[2] = ((ulong)x)<<sh; for (i=3; i<ly; i++) y[i]=0;
    1392              : }
    1393              : 
    1394              : INLINE void
    1395     13344662 : affur(ulong x, GEN y)
    1396              : {
    1397     13344662 :   long sh, i, ly = lg(y);
    1398              : 
    1399     13344662 :   if (!x)
    1400              :   {
    1401      1368994 :     y[1] = evalexpo(-bit_accuracy(ly));
    1402      1368994 :     return;
    1403              :   }
    1404     11975668 :   sh = bfffo(x);
    1405     11975668 :   y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1406     43193968 :   y[2] = x<<sh; for (i=3; i<ly; i++) y[i] = 0;
    1407              : }
    1408              : 
    1409              : INLINE long
    1410     24078751 : hammingu(ulong x)
    1411              : {
    1412              : #ifdef LONG_IS_64BIT
    1413     20521549 :   x = (x & 0x5555555555555555UL) + ((x >> 1) & 0x5555555555555555UL);
    1414     20521549 :   x = (x & 0x3333333333333333UL) + ((x >> 2) & 0x3333333333333333UL);
    1415     20521549 :   x = (x & 0x0F0F0F0F0F0F0F0FUL) + ((x >> 4) & 0x0F0F0F0F0F0F0F0FUL);
    1416     20521549 :   return (x * 0x0101010101010101UL) >> 56;
    1417              : #else
    1418      3557202 :   x = (x & 0x55555555UL) + ((x >> 1) & 0x55555555UL);
    1419      3557202 :   x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
    1420      3557202 :   x = (x & 0x0F0F0F0FUL) + ((x >> 4) & 0x0F0F0F0FUL);
    1421      3557202 :   return (x * 0x01010101UL) >> 24;
    1422              : #endif
    1423              : }
    1424              : 
    1425              : INLINE ulong
    1426         3313 : thuemorseu(ulong c)
    1427              : {
    1428              : #ifdef LONG_IS_64BIT
    1429         2838 :     c ^= c >> 32;
    1430              : #endif
    1431         3313 :     c ^= c >> 16;
    1432         3313 :     c ^= c >>  8;
    1433         3313 :     c ^= c >>  4;
    1434         3313 :     c ^= c >>  2;
    1435         3313 :     c ^= c >>  1;
    1436         3313 :     return c & 1;
    1437              : }
        

Generated by: LCOV version 2.0-1