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) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23026-c0ccc8713) Lines: 560 730 76.7 %
Date: 2018-09-25 05:41:47 Functions: 203 270 75.2 %
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 44053880824 : evallg(long x)
      21             : {
      22 44053880824 :   if (x & ~LGBITS) pari_err_OVERFLOW("lg()");
      23 44056662014 :   return _evallg(x);
      24             : }
      25             : INLINE long
      26    16460341 : evalvalp(long x)
      27             : {
      28    16460341 :   long v = _evalvalp(x);
      29    16460341 :   if (v & ~VALPBITS) pari_err_OVERFLOW("valp()");
      30    16460342 :   return v;
      31             : }
      32             : INLINE long
      33  6222582897 : evalexpo(long x)
      34             : {
      35  6222582897 :   long v = _evalexpo(x);
      36  6222582897 :   if (v & ~EXPOBITS) pari_err_OVERFLOW("expo()");
      37  6222582897 :   return v;
      38             : }
      39             : INLINE long
      40     1875983 : evalprecp(long x)
      41             : {
      42     1875983 :   long v = _evalprecp(x);
      43     1875983 :   if (x & ~((1UL<<(BITS_IN_LONG-VALPnumBITS))-1)) pari_err_OVERFLOW("precp()");
      44     1875982 :   return v;
      45             : }
      46             : 
      47             : INLINE int
      48   147408931 : varncmp(long x, long y)
      49             : {
      50   147408931 :   if (varpriority[x] < varpriority[y]) return  1;
      51   115689718 :   if (varpriority[x] > varpriority[y]) return -1;
      52    63337582 :   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  1729940372 : stackdummy(pari_sp av, pari_sp ltop) {
      70  1729940372 :   long l = ((GEN)av) - ((GEN)ltop);
      71  1729940372 :   if (l > 0) {
      72   609521078 :     GEN z = (GEN)ltop;
      73   609521078 :     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  1730069164 : }
      79             : INLINE void
      80    62210268 : fixlg(GEN x, long ly) {
      81    62210268 :   long lx = lg(x), l = lx - ly;
      82    62210268 :   if (l > 0)
      83             :   { /* stackdummy(x+lx, x+ly) */
      84    42963024 :     GEN z = x + ly;
      85    42963024 :     z[0] = evaltyp(t_VECSMALL) | evallg(l);
      86    42963017 :     setlg(x, ly);
      87             : #ifdef DEBUG
      88             :     { long i; for (i = 1; i < l; i++) z[i] = 0; }
      89             : #endif
      90             :   }
      91    62210226 : }
      92             : /* update lg(z) before affrr(y, z)  [ to cater for precision loss ]*/
      93             : INLINE void
      94    19269012 : affrr_fixlg(GEN y, GEN z) { fixlg(z, lg(y)); affrr(y, z); }
      95             : 
      96             : /*******************************************************************/
      97             : /*                                                                 */
      98             : /*                       ALLOCATE ON STACK                         */
      99             : /*                                                                 */
     100             : /*******************************************************************/
     101             : INLINE void
     102  6671282543 : set_avma(ulong av) { avma = av; }
     103             : INLINE GEN
     104 41551274473 : new_chunk(size_t x) /* x is a number of longs */
     105             : {
     106 41551274473 :   GEN z = ((GEN) avma) - x;
     107             :   CHECK_CTRLC
     108 41551274473 :   if (x > (avma-pari_mainstack->bot) / sizeof(long))
     109           7 :     new_chunk_resize(x);
     110 41558128623 :   avma = (pari_sp)z;
     111             : #ifdef MEMSTEP
     112             :   if (DEBUGMEM>1 && pari_mainstack->memused != DISABLE_MEMUSED) {
     113             :     long d = (long)pari_mainstack->memused - (long)z;
     114             :     if (labs(d) > 4*MEMSTEP)
     115             :     {
     116             :       pari_mainstack->memused = (pari_sp)z;
     117             :       err_printf("...%4.0lf Mbytes used\n",
     118             :                 (pari_mainstack->top-pari_mainstack->memused)/1048576.);
     119             :     }
     120             :   }
     121             : #endif
     122 41558128623 :   return z;
     123             : }
     124             : 
     125             : INLINE char *
     126     4155468 : stack_malloc(size_t N)
     127             : {
     128     4155468 :   long n = nchar2nlong(N);
     129     4155469 :   return (char*)new_chunk(n);
     130             : }
     131             : 
     132             : INLINE char *
     133     1198143 : stack_malloc_align(size_t N, long k)
     134             : {
     135     1198143 :   ulong d = ((ulong)avma) % k;
     136     1198143 :   if (d) (void)new_chunk(d/sizeof(long));
     137     1198143 :   return (char*) new_chunk(nchar2nlong(N));
     138             : }
     139             : 
     140             : INLINE char *
     141       41541 : stack_calloc(size_t N)
     142             : {
     143       41541 :   char *p = stack_malloc(N);
     144       41541 :   memset(p, 0, N); return p;
     145             : }
     146             : 
     147             : /* cgetg(lg(x), typ(x)), set *lx. Implicit unsetisclone() */
     148             : INLINE GEN
     149   433858951 : cgetg_copy(GEN x, long *plx) {
     150             :   GEN y;
     151   433858951 :   *plx = lg(x); y = new_chunk((size_t)*plx);
     152   433858907 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     153             : }
     154             : INLINE GEN
     155      146595 : cgetg_block(long x, long y)
     156             : {
     157      146595 :   GEN z = newblock((size_t)x);
     158      146642 :   z[0] = CLONEBIT | evaltyp(y) | evallg(x);
     159      146581 :   return z;
     160             : }
     161             : INLINE GEN
     162  5689083838 : cgetg(long x, long y)
     163             : {
     164  5689083838 :   GEN z = new_chunk((size_t)x);
     165  5688682631 :   z[0] = evaltyp(y) | evallg(x);
     166  5686416081 :   return z;
     167             : }
     168             : INLINE GEN
     169 13821960660 : cgeti(long x)
     170             : {
     171 13821960660 :   GEN z = new_chunk((size_t)x);
     172 13812016858 :   z[0] = evaltyp(t_INT) | evallg(x);
     173 13803836105 :   return z;
     174             : }
     175             : INLINE GEN
     176  8524461507 : cgetipos(long x)
     177             : {
     178  8524461507 :   GEN z = cgeti(x);
     179  8522826464 :   z[1] = evalsigne(1) | evallgefint(x);
     180  8522826464 :   return z;
     181             : }
     182             : INLINE GEN
     183   209182089 : cgetineg(long x)
     184             : {
     185   209182089 :   GEN z = cgeti(x);
     186   209182084 :   z[1] = evalsigne(-1) | evallgefint(x);
     187   209182084 :   return z;
     188             : }
     189             : INLINE GEN
     190        2914 : cgetr_block(long x)
     191             : {
     192        2914 :   GEN z = newblock((size_t)x);
     193        2914 :   z[0] = CLONEBIT | evaltyp(t_REAL) | evallg(x);
     194        2914 :   return z;
     195             : }
     196             : INLINE GEN
     197  6477781124 : cgetr(long x)
     198             : {
     199  6477781124 :   GEN z = new_chunk((size_t)x);
     200  6477781124 :   z[0] = evaltyp(t_REAL) | evallg(x);
     201  6477781124 :   return z;
     202             : }
     203             : 
     204             : /*******************************************************************/
     205             : /*                                                                 */
     206             : /*                     COPY, NEGATION, ABSOLUTE VALUE              */
     207             : /*                                                                 */
     208             : /*******************************************************************/
     209             : /* cannot do memcpy because sometimes x and y overlap */
     210             : INLINE GEN
     211  1510462559 : leafcopy(GEN x)
     212             : {
     213  1510462559 :   register long lx = lg(x);
     214  1510462559 :   GEN y = new_chunk(lx); /* can't use cgetg_copy, in case x,y overlap */
     215  1510797754 :   while (--lx > 0) y[lx] = x[lx];
     216  1510797754 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     217             : }
     218             : INLINE GEN
     219  4476664145 : icopy(GEN x)
     220             : {
     221  4476664145 :   long i = lgefint(x), lx = i;
     222  4476664145 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     223  4481820489 :   while (--i > 0) y[i] = x[i];
     224  4481820489 :   y[0] = evaltyp(t_INT) | evallg(lx);
     225  4479938146 :   return y;
     226             : }
     227             : INLINE GEN
     228    30175463 : icopyspec(GEN x, long nx)
     229             : {
     230    30175463 :   long i = nx+2, lx = i;
     231    30175463 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     232    30175428 :   x -= 2; while (--i >= 2) y[i] = x[i];
     233    30175428 :   y[1] = evalsigne(1) | evallgefint(lx);
     234    30175428 :   y[0] = evaltyp(t_INT) | evallg(lx);
     235    30175377 :   return y;
     236             : }
     237   268828200 : INLINE GEN rcopy(GEN x) { return leafcopy(x); }
     238         224 : INLINE GEN mpcopy(GEN x) { return leafcopy(x); }
     239             : 
     240             : INLINE GEN
     241   351530650 : mpabs(GEN x) { GEN y = leafcopy(x); setabssign(y); return y; }
     242             : INLINE GEN
     243     7837416 : mpabs_shallow(GEN x) { return signe(x) < 0? mpabs(x): x; }
     244   310012210 : INLINE GEN absi(GEN x) { return mpabs(x); }
     245    30125615 : INLINE GEN absi_shallow(GEN x) { return signe(x) < 0? negi(x): x; }
     246    31441545 : INLINE GEN absr(GEN x) { return mpabs(x); }
     247             : 
     248             : INLINE GEN
     249   359257905 : mpneg(GEN x) { GEN y = leafcopy(x); togglesign(y); return y; }
     250   250187741 : INLINE GEN negi(GEN x) { return mpneg(x); }
     251     1494466 : INLINE GEN negr(GEN x) { return mpneg(x); }
     252     7832400 : INLINE GEN gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
     253       89249 : INLINE GEN gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
     254             : 
     255             : /* negate in place */
     256             : INLINE void
     257   790375680 : togglesign(GEN x) { if (x[1] & SIGNBITS) { x[1] ^= HIGHBIT; } }
     258             : INLINE void
     259   389497185 : setabssign(GEN x) { x[1] &= ~HIGHBIT; }
     260             : /* negate in place, except universal constants */
     261             : INLINE void
     262    56904638 : togglesign_safe(GEN *px)
     263             : {
     264    56904638 :   switch(*px - gen_1) /* gen_1, gen_2, gen_m1, gen_m2 */
     265             :   {
     266      663756 :     case 0: *px = gen_m1; break;
     267           0 :     case 3: *px = gen_m2;  break;
     268      155599 :     case 6: *px = gen_1; break;
     269           0 :     case 9: *px = gen_2;  break;
     270    56085283 :     default: togglesign(*px);
     271             :   }
     272    56904638 : }
     273             : /* setsigne(y, signe(x)) */
     274             : INLINE void
     275           0 : affectsign(GEN x, GEN y)
     276             : {
     277           0 :   y[1] = (x[1] & SIGNBITS) | (y[1] & ~SIGNBITS);
     278           0 : }
     279             : /* copies sign in place, except for universal constants */
     280             : INLINE void
     281     5318223 : affectsign_safe(GEN x, GEN *py)
     282             : {
     283     5318223 :   if (((*py)[1] ^ x[1]) & HIGHBIT) togglesign_safe(py);
     284     5318223 : }
     285             : /*******************************************************************/
     286             : /*                                                                 */
     287             : /*                     GEN -> LONG, LONG -> GEN                    */
     288             : /*                                                                 */
     289             : /*******************************************************************/
     290             : /* assume x != 0, return -x as a t_INT */
     291             : INLINE GEN
     292   208996983 : utoineg(ulong x) { GEN y = cgetineg(3); y[2] = x; return y; }
     293             : /* assume x != 0, return utoi(x) */
     294             : INLINE GEN
     295  7500265782 : utoipos(ulong x) { GEN y = cgetipos(3); y[2] = x; return y; }
     296             : INLINE GEN
     297  6204379132 : utoi(ulong x) { return x? utoipos(x): gen_0; }
     298             : INLINE GEN
     299   364435768 : stoi(long x)
     300             : {
     301   364435768 :   if (!x) return gen_0;
     302   233172411 :   return x > 0? utoipos((ulong)x): utoineg((ulong)-x);
     303             : }
     304             : 
     305             : /* x 2^BIL + y */
     306             : INLINE GEN
     307  4506063665 : uutoi(ulong x, ulong y)
     308             : {
     309             :   GEN z;
     310  4506063665 :   if (!x) return utoi(y);
     311   480914794 :   z = cgetipos(4);
     312   480506382 :   *int_W_lg(z, 1, 4) = x;
     313   480506382 :   *int_W_lg(z, 0, 4) = y; return z;
     314             : }
     315             : /* - (x 2^BIL + y) */
     316             : INLINE GEN
     317       91747 : uutoineg(ulong x, ulong y)
     318             : {
     319             :   GEN z;
     320       91747 :   if (!x) return y? utoineg(y): gen_0;
     321       10243 :   z = cgetineg(4);
     322       10243 :   *int_W_lg(z, 1, 4) = x;
     323       10243 :   *int_W_lg(z, 0, 4) = y; return z;
     324             : }
     325             : 
     326             : INLINE long
     327   317456237 : itos(GEN x)
     328             : {
     329   317456237 :   long s = signe(x);
     330             :   long u;
     331             : 
     332   317456237 :   if (!s) return 0;
     333   298692280 :   u = x[2];
     334   298692280 :   if (lgefint(x) > 3 || u < 0)
     335          19 :     pari_err_OVERFLOW("t_INT-->long assignment");
     336   298692268 :   return (s>0) ? u : -u;
     337             : }
     338             : /* as itos, but return 0 if too large. Cf is_bigint */
     339             : INLINE long
     340    13538226 : itos_or_0(GEN x) {
     341             :   long n;
     342    13538226 :   if (lgefint(x) != 3 || (n = x[2]) & HIGHBIT) return 0;
     343    11950501 :   return signe(x) > 0? n: -n;
     344             : }
     345             : INLINE ulong
     346    80602485 : itou(GEN x)
     347             : {
     348    80602485 :   switch(lgefint(x)) {
     349     9747824 :     case 2: return 0;
     350    70854661 :     case 3: return x[2];
     351             :     default:
     352           0 :       pari_err_OVERFLOW("t_INT-->ulong assignment");
     353             :       return 0; /* LCOV_EXCL_LINE */
     354             :   }
     355             : }
     356             : 
     357             : /* as itou, but return 0 if too large. Cf is_bigint */
     358             : INLINE ulong
     359     4597308 : itou_or_0(GEN x) {
     360     4597308 :   if (lgefint(x) != 3) return 0;
     361     4586453 :   return (ulong)x[2];
     362             : }
     363             : 
     364             : INLINE ulong
     365     4870748 : umuluu_or_0(ulong x, ulong y)
     366             : {
     367             :   ulong z;
     368             :   LOCAL_HIREMAINDER;
     369     4870748 :   z = mulll(x, y);
     370     4870748 :   return hiremainder? 0: z;
     371             : }
     372             : /* return x*y if <= n, else 0. Beware overflow */
     373             : INLINE ulong
     374     2854168 : umuluu_le(ulong x, ulong y, ulong n)
     375             : {
     376             :   ulong z;
     377             :   LOCAL_HIREMAINDER;
     378     2854168 :   z = mulll(x, y);
     379     2854168 :   return (hiremainder || z > n)? 0: z;
     380             : }
     381             : 
     382             : INLINE GEN
     383   129526350 : real_0_bit(long bitprec) { GEN x=cgetr(2); x[1]=evalexpo(bitprec); return x; }
     384             : INLINE GEN
     385    32566832 : real_0(long prec) { return real_0_bit(-prec2nbits(prec)); }
     386             : INLINE GEN
     387      584677 : real_1_bit(long bit) { return real_1(nbits2prec(bit)); }
     388             : INLINE GEN
     389    40172209 : real_1(long prec) {
     390    40172209 :   GEN x = cgetr(prec);
     391             :   long i;
     392    40172209 :   x[1] = evalsigne(1) | _evalexpo(0);
     393    40172209 :   x[2] = (long)HIGHBIT; for (i=3; i<prec; i++) x[i] = 0;
     394    40172209 :   return x;
     395             : }
     396             : INLINE GEN
     397         147 : real_m1(long prec) {
     398         147 :   GEN x = cgetr(prec);
     399             :   long i;
     400         147 :   x[1] = evalsigne(-1) | _evalexpo(0);
     401         147 :   x[2] = (long)HIGHBIT; for (i=3; i<prec; i++) x[i] = 0;
     402         147 :   return x;
     403             : }
     404             : 
     405             : /* 2.^n */
     406             : INLINE GEN
     407      115309 : real2n(long n, long prec) { GEN z = real_1(prec); setexpo(z, n); return z; }
     408             : INLINE GEN
     409           0 : real_m2n(long n, long prec) { GEN z = real_m1(prec); setexpo(z, n); return z; }
     410             : INLINE GEN
     411   183897331 : stor(long s, long prec) { GEN z = cgetr(prec); affsr(s,z); return z; }
     412             : INLINE GEN
     413     8780595 : utor(ulong s, long prec){ GEN z = cgetr(prec); affur(s,z); return z; }
     414             : INLINE GEN
     415   208969800 : itor(GEN x, long prec) { GEN z = cgetr(prec); affir(x,z); return z; }
     416             : INLINE GEN
     417    99776017 : rtor(GEN x, long prec) { GEN z = cgetr(prec); affrr(x,z); return z; }
     418             : 
     419    11555544 : INLINE ulong int_bit(GEN x, long n)
     420             : {
     421    11555544 :   long r, q = dvmdsBIL(n, &r);
     422    11553615 :   return q < lgefint(x)-2?((ulong)*int_W(x,q) >> r) & 1UL:0;
     423             : }
     424             : 
     425             : /*******************************************************************/
     426             : /*                                                                 */
     427             : /*                           COMPARISON                            */
     428             : /*                                                                 */
     429             : /*******************************************************************/
     430             : INLINE int
     431      647321 : cmpir(GEN x, GEN y)
     432             : {
     433             :   pari_sp av;
     434             :   GEN z;
     435             : 
     436      647321 :   if (!signe(x)) return -signe(y);
     437      271841 :   if (!signe(y))
     438             :   {
     439        3823 :     if (expo(y) >= expi(x)) return 0;
     440        3795 :     return signe(x);
     441             :   }
     442      268018 :   av=avma; z = itor(x, realprec(y)); set_avma(av);
     443      268018 :   return cmprr(z,y); /* cmprr does no memory adjustment */
     444             : }
     445             : INLINE int
     446      369362 : cmpri(GEN x, GEN y) { return -cmpir(y,x); }
     447             : INLINE int
     448       76817 : cmpsr(long x, GEN y)
     449             : {
     450             :   pari_sp av;
     451             :   GEN z;
     452             : 
     453       76817 :   if (!x) return -signe(y);
     454       76817 :   av=avma; z = stor(x, LOWDEFAULTPREC); set_avma(av);
     455       76817 :   return cmprr(z,y);
     456             : }
     457             : INLINE int
     458       38829 : cmprs(GEN x, long y) { return -cmpsr(y,x); }
     459             : /* compare x and y */
     460             : INLINE int
     461         994 : cmpui(ulong x, GEN y)
     462             : {
     463             :   ulong p;
     464         994 :   if (!x) return -signe(y);
     465         994 :   if (signe(y) <= 0) return 1;
     466         994 :   if (lgefint(y) > 3) return -1;
     467         987 :   p = y[2]; if (p == x) return 0;
     468         798 :   return p < x ? 1 : -1;
     469             : }
     470             : INLINE int
     471         994 : cmpiu(GEN x, ulong y) { return -cmpui(y,x); }
     472             : /* compare x and |y| */
     473             : INLINE int
     474    31039571 : abscmpui(ulong x, GEN y)
     475             : {
     476    31039571 :   long l = lgefint(y);
     477             :   ulong p;
     478             : 
     479    31039571 :   if (!x) return (l > 2)? -1: 0;
     480    31039557 :   if (l == 2) return 1;
     481    30843103 :   if (l > 3) return -1;
     482    30831711 :   p = y[2]; if (p == x) return 0;
     483    30428063 :   return p < x ? 1 : -1;
     484             : }
     485             : INLINE int
     486    30982885 : abscmpiu(GEN x, ulong y) { return -abscmpui(y,x); }
     487             : INLINE int
     488     4336908 : cmpsi(long x, GEN y)
     489             : {
     490             :   ulong p;
     491             : 
     492     4336908 :   if (!x) return -signe(y);
     493             : 
     494     4335858 :   if (x > 0)
     495             :   {
     496     4334983 :     if (signe(y)<=0) return 1;
     497     4334675 :     if (lgefint(y)>3) return -1;
     498     4320429 :     p = y[2]; if (p == (ulong)x) return 0;
     499     4254510 :     return p < (ulong)x ? 1 : -1;
     500             :   }
     501             : 
     502         875 :   if (signe(y)>=0) return -1;
     503         147 :   if (lgefint(y)>3) return 1;
     504         147 :   p = y[2]; if (p == (ulong)-x) return 0;
     505          42 :   return p < (ulong)(-x) ? -1 : 1;
     506             : }
     507             : INLINE int
     508     4328368 : cmpis(GEN x, long y) { return -cmpsi(y,x); }
     509             : INLINE int
     510      544149 : mpcmp(GEN x, GEN y)
     511             : {
     512      544149 :   if (typ(x)==t_INT)
     513        7521 :     return (typ(y)==t_INT) ? cmpii(x,y) : cmpir(x,y);
     514      536628 :   return (typ(y)==t_INT) ? -cmpir(y,x) : cmprr(x,y);
     515             : }
     516             : 
     517             : /* x == y ? */
     518             : INLINE int
     519       22723 : equalui(ulong x, GEN y)
     520             : {
     521       22723 :   if (!x) return !signe(y);
     522       22723 :   if (signe(y) <= 0 || lgefint(y) != 3) return 0;
     523       22714 :   return ((ulong)y[2] == (ulong)x);
     524             : }
     525             : /* x == y ? */
     526             : INLINE int
     527      154160 : equalsi(long x, GEN y)
     528             : {
     529      154160 :   if (!x) return !signe(y);
     530      154160 :   if (x > 0)
     531             :   {
     532      154132 :     if (signe(y) <= 0 || lgefint(y) != 3) return 0;
     533      144161 :     return ((ulong)y[2] == (ulong)x);
     534             :   }
     535          28 :   if (signe(y) >= 0 || lgefint(y) != 3) return 0;
     536          28 :   return ((ulong)y[2] == (ulong)-x);
     537             : }
     538             : /* x == |y| ? */
     539             : INLINE int
     540    36522452 : absequalui(ulong x, GEN y)
     541             : {
     542    36522452 :   if (!x) return !signe(y);
     543    36522452 :   return (lgefint(y) == 3 && (ulong)y[2] == x);
     544             : }
     545             : INLINE int
     546    36415913 : absequaliu(GEN x, ulong y) { return absequalui(y,x); }
     547             : INLINE int
     548      153943 : equalis(GEN x, long y) { return equalsi(y,x); }
     549             : INLINE int
     550       22723 : equaliu(GEN x, ulong y) { return equalui(y,x); }
     551             : 
     552             : /* assume x != 0, is |x| == 2^n ? */
     553             : INLINE int
     554      308658 : absrnz_equal2n(GEN x) {
     555      308658 :   if ((ulong)x[2]==HIGHBIT)
     556             :   {
     557       15205 :     long i, lx = lg(x);
     558       70176 :     for (i = 3; i < lx; i++)
     559       63704 :       if (x[i]) return 0;
     560        6472 :     return 1;
     561             :   }
     562      293453 :   return 0;
     563             : }
     564             : /* assume x != 0, is |x| == 1 ? */
     565             : INLINE int
     566      718101 : absrnz_equal1(GEN x) { return !expo(x) && absrnz_equal2n(x); }
     567             : 
     568             : INLINE long
     569  3017106245 : maxss(long x, long y) { return x>y?x:y; }
     570             : INLINE long
     571   781731704 : minss(long x, long y) { return x<y?x:y; }
     572             : INLINE long
     573     1965662 : minuu(ulong x, ulong y) { return x<y?x:y; }
     574             : INLINE long
     575    10466013 : maxuu(ulong x, ulong y) { return x>y?x:y; }
     576             : INLINE double
     577     3502588 : maxdd(double x, double y) { return x>y?x:y; }
     578             : INLINE double
     579     1635252 : mindd(double x, double y) { return x<y?x:y; }
     580             : 
     581             : /*******************************************************************/
     582             : /*                                                                 */
     583             : /*                             ADD / SUB                           */
     584             : /*                                                                 */
     585             : /*******************************************************************/
     586             : INLINE GEN
     587       25046 : subuu(ulong x, ulong y)
     588             : {
     589             :   ulong z;
     590             :   LOCAL_OVERFLOW;
     591       25046 :   z = subll(x, y);
     592       25046 :   return overflow? utoineg(-z): utoi(z);
     593             : }
     594             : INLINE GEN
     595  1742008995 : adduu(ulong x, ulong y) { ulong t = x+y; return uutoi((t < x), t); }
     596             : 
     597             : INLINE GEN
     598       25046 : addss(long x, long y)
     599             : {
     600       25046 :   if (!x) return stoi(y);
     601       25046 :   if (!y) return stoi(x);
     602       25046 :   if (x > 0) return y > 0? adduu(x,y): subuu(x, -y);
     603             : 
     604       25046 :   if (y > 0) return subuu(y, -x);
     605             :   else { /* - adduu(-x, -y) */
     606           0 :     ulong t = (-x)+(-y); return uutoineg((t < (ulong)(-x)), t);
     607             :   }
     608             : }
     609       25046 : INLINE GEN subss(long x, long y) { return addss(-y,x); }
     610             : 
     611             : INLINE GEN
     612  3942372928 : subii(GEN x, GEN y)
     613             : {
     614  3942372928 :   if (x==y) return gen_0; /* frequent with x = y = gen_0 */
     615  2859442457 :   return addii_sign(x, signe(x), y, -signe(y));
     616             : }
     617             : INLINE GEN
     618  4716378577 : addii(GEN x, GEN y) { return addii_sign(x, signe(x), y, signe(y)); }
     619             : INLINE GEN
     620   926245889 : addrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, signe(y)); }
     621             : INLINE GEN
     622   916826677 : subrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, -signe(y)); }
     623             : INLINE GEN
     624   150588421 : addir(GEN x, GEN y) { return addir_sign(x, signe(x), y, signe(y)); }
     625             : INLINE GEN
     626    80340791 : subir(GEN x, GEN y) { return addir_sign(x, signe(x), y, -signe(y)); }
     627             : INLINE GEN
     628     1505829 : subri(GEN x, GEN y) { return addir_sign(y, -signe(y), x, signe(x)); }
     629             : INLINE GEN
     630    73160822 : addsi(long x, GEN y) { return addsi_sign(x, y, signe(y)); }
     631             : INLINE GEN
     632    65376761 : addui(ulong x, GEN y) { return addui_sign(x, y, signe(y)); }
     633             : INLINE GEN
     634     5265850 : subsi(long x, GEN y) { return addsi_sign(x, y, -signe(y)); }
     635             : INLINE GEN
     636    33186559 : subui(ulong x, GEN y) { return addui_sign(x, y, -signe(y)); }
     637             : 
     638             : /*******************************************************************/
     639             : /*                                                                 */
     640             : /*                           MOD, REM, DIV                         */
     641             : /*                                                                 */
     642             : /*******************************************************************/
     643    97228837 : INLINE ulong mod2BIL(GEN x) { return *int_LSW(x); }
     644           0 : INLINE long mod64(GEN x) { return mod2BIL(x) & 63; }
     645         259 : INLINE long mod32(GEN x) { return mod2BIL(x) & 31; }
     646       25123 : INLINE long mod16(GEN x) { return mod2BIL(x) & 15; }
     647    49990148 : INLINE long mod8(GEN x)  { return mod2BIL(x) & 7; }
     648    11865134 : INLINE long mod4(GEN x)  { return mod2BIL(x) & 3; }
     649    20911112 : INLINE long mod2(GEN x)  { return mod2BIL(x) & 1; }
     650             : INLINE int
     651    35748670 : mpodd(GEN x) { return signe(x) && mod2(x); }
     652             : /* x mod 2^n, n < BITS_IN_LONG */
     653             : INLINE ulong
     654     4846282 : umodi2n(GEN x, long n)
     655             : {
     656     4846282 :   long s = signe(x);
     657     4846282 :   const ulong _2n = 1UL << n;
     658             :   ulong m;
     659     4846282 :   if (!s) return 0;
     660     4839492 :   m = *int_LSW(x) & (_2n - 1);
     661     4839492 :   if (s < 0 && m) m = _2n - m;
     662     4839492 :   return m;
     663             : }
     664           0 : INLINE ulong Mod64(GEN x){ return umodi2n(x,6); }
     665      166810 : INLINE ulong Mod32(GEN x){ return umodi2n(x,5); }
     666      216692 : INLINE ulong Mod16(GEN x){ return umodi2n(x,4); }
     667     1307999 : INLINE ulong Mod8(GEN x) { return umodi2n(x,3); }
     668     1329979 : INLINE ulong Mod4(GEN x) { return umodi2n(x,2); }
     669     1824732 : INLINE ulong Mod2(GEN x) { return umodi2n(x,1); }
     670             : 
     671             : INLINE GEN
     672    22887893 : truedivii(GEN a,GEN b) { return truedvmdii(a,b,NULL); }
     673             : INLINE GEN
     674        4474 : truedivis(GEN a, long b) { return truedvmdis(a,b,NULL); }
     675             : INLINE GEN
     676     6168028 : truedivsi(long a, GEN b) { return truedvmdsi(a,b,NULL); }
     677             : 
     678             : INLINE GEN
     679     7714207 : divii(GEN a, GEN b) { return dvmdii(a,b,NULL); }
     680             : INLINE GEN
     681  1220286032 : remii(GEN a, GEN b) { return dvmdii(a,b,ONLY_REM); }
     682             : 
     683             : INLINE GEN
     684           0 : divss(long x, long y) { return stoi(x / y); }
     685             : INLINE GEN
     686      658546 : modss(long x, long y) { return stoi(smodss(x, y)); }
     687             : INLINE GEN
     688           0 : remss(long x, long y) { return stoi(x % y); }
     689             : INLINE long
     690   201128511 : smodss(long x, long y)
     691             : {
     692   201128511 :   long r = x%y;
     693   201128511 :   return (r >= 0)? r: labs(y) + r;
     694             : }
     695             : INLINE ulong
     696    11587171 : umodsu(long x, ulong y)
     697             : {
     698    11587171 :   return x>=0 ? x%y: Fl_neg((-x)%y, y);
     699             : }
     700             : 
     701             : INLINE long
     702           0 : sdivss_rem(long x, long y, long *r)
     703             : {
     704             :   long q;
     705             :   LOCAL_HIREMAINDER;
     706           0 :   if (!y) pari_err_INV("sdivss_rem",gen_0);
     707           0 :   hiremainder = 0; q = divll((ulong)labs(x),(ulong)labs(y));
     708           0 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     709           0 :   if (y < 0) q = -q;
     710           0 :   *r = hiremainder; return q;
     711             : }
     712             : INLINE GEN
     713           0 : divss_rem(long x, long y, long *r) { return stoi(sdivss_rem(x,y,r)); }
     714             : INLINE ulong
     715    37006887 : udivuu_rem(ulong x, ulong y, ulong *r)
     716             : {
     717    37006887 :   if (!y) pari_err_INV("udivuu_rem",gen_0);
     718    37006887 :   *r = x % y; return x / y;
     719             : }
     720             : INLINE ulong
     721      158400 : ceildivuu(ulong a, ulong b)
     722             : {
     723      158400 :   ulong c = a/b;
     724      158400 :   return (a%b)? c+1: c;
     725             : }
     726             : 
     727             : INLINE ulong
     728       10591 : uabsdivui_rem(ulong x, GEN y, ulong *r)
     729             : {
     730       10591 :   long q, s = signe(y);
     731             :   LOCAL_HIREMAINDER;
     732             : 
     733       10591 :   if (!s) pari_err_INV("uabsdivui_rem",gen_0);
     734       10591 :   if (!x || lgefint(y)>3) { *r = x; return 0; }
     735       10318 :   hiremainder=0; q = (long)divll(x, (ulong)y[2]);
     736       10318 :   if (s < 0) q = -q;
     737       10318 :   *r = hiremainder; return q;
     738             : }
     739             : 
     740             : /* assume d != 0 and |n| / d can be represented as an ulong.
     741             :  * Return |n|/d, set *r = |n| % d */
     742             : INLINE ulong
     743    12480475 : uabsdiviu_rem(GEN n, ulong d, ulong *r)
     744             : {
     745    12480475 :   switch(lgefint(n))
     746             :   {
     747           0 :     case 2: *r = 0; return 0;
     748             :     case 3:
     749             :     {
     750    12480475 :       ulong nn = n[2];
     751    12480475 :       *r = nn % d; return nn / d;
     752             :     }
     753             :     default: /* 4 */
     754             :     {
     755             :       ulong n1, n0, q;
     756             :       LOCAL_HIREMAINDER;
     757           0 :       n0 = *int_W(n,0);
     758           0 :       n1 = *int_W(n,1);
     759           0 :       hiremainder = n1;
     760           0 :       q = divll(n0, d);
     761           0 :       *r = hiremainder; return q;
     762             :     }
     763             :   }
     764             : }
     765             : 
     766             : INLINE long
     767    13900037 : sdivsi_rem(long x, GEN y, long *r)
     768             : {
     769    13900037 :   long q, s = signe(y);
     770             :   LOCAL_HIREMAINDER;
     771             : 
     772    13900037 :   if (!s) pari_err_INV("sdivsi_rem",gen_0);
     773    13900037 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) { *r = x; return 0; }
     774    11991901 :   hiremainder=0; q = (long)divll(labs(x), (ulong)y[2]);
     775    11991901 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     776    11991901 :   if (s < 0) q = -q;
     777    11991901 :   *r = hiremainder; return q;
     778             : }
     779             : INLINE GEN
     780           0 : divsi_rem(long s, GEN y, long *r) { return stoi(sdivsi_rem(s,y,r)); }
     781             : 
     782             : INLINE long
     783         140 : sdivsi(long x, GEN y)
     784             : {
     785         140 :   long q, s = signe(y);
     786             : 
     787         140 :   if (!s) pari_err_INV("sdivsi",gen_0);
     788         140 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) return 0;
     789          98 :   q = labs(x) / y[2];
     790          98 :   if (x < 0) q = -q;
     791          98 :   if (s < 0) q = -q;
     792          98 :   return q;
     793             : }
     794             : 
     795             : INLINE GEN
     796           0 : dvmdss(long x, long y, GEN *z)
     797             : {
     798             :   long r;
     799           0 :   GEN q = divss_rem(x,y, &r);
     800           0 :   *z = stoi(r); return q;
     801             : }
     802             : INLINE long
     803  3069937930 : dvmdsBIL(long n, long *r) { *r = remsBIL(n); return divsBIL(n); }
     804             : INLINE ulong
     805   143856998 : dvmduBIL(ulong n, ulong *r) { *r = remsBIL(n); return divsBIL(n); }
     806             : INLINE GEN
     807           0 : dvmdsi(long x, GEN y, GEN *z)
     808             : {
     809             :   long r;
     810           0 :   GEN q = divsi_rem(x,y, &r);
     811           0 :   *z = stoi(r); return q;
     812             : }
     813             : INLINE GEN
     814           0 : dvmdis(GEN x, long y, GEN *z)
     815             : {
     816             :   long r;
     817           0 :   GEN q = divis_rem(x,y, &r);
     818           0 :   *z = stoi(r); return q;
     819             : }
     820             : 
     821             : INLINE long
     822    19628782 : smodis(GEN x, long y)
     823             : {
     824    19628782 :   pari_sp av = avma;
     825    19628782 :   long r; (void)divis_rem(x,y, &r);
     826    19628782 :   return gc_long(av, (r >= 0)? r: labs(y) + r);
     827             : }
     828             : INLINE GEN
     829    19590254 : modis(GEN x, long y) { return stoi(smodis(x,y)); }
     830             : INLINE GEN
     831     7731764 : modsi(long x, GEN y) {
     832     7731764 :   long r; (void)sdivsi_rem(x, y, &r);
     833     7731764 :   return (r >= 0)? stoi(r): addsi_sign(r, y, 1);
     834             : }
     835             : 
     836             : INLINE ulong
     837     1259948 : umodui(ulong x, GEN y)
     838             : {
     839     1259948 :   if (!signe(y)) pari_err_INV("umodui",gen_0);
     840     1259948 :   if (!x || lgefint(y) > 3) return x;
     841      234495 :   return x % (ulong)y[2];
     842             : }
     843             : 
     844             : INLINE ulong
     845       42933 : ugcdiu(GEN x, ulong y) { return ugcd(umodiu(x,y), y); }
     846             : INLINE ulong
     847        4792 : ugcdui(ulong y, GEN x) { return ugcd(umodiu(x,y), y); }
     848             : 
     849             : INLINE GEN
     850           0 : remsi(long x, GEN y)
     851           0 : { long r; (void)sdivsi_rem(x,y, &r); return stoi(r); }
     852             : INLINE GEN
     853           0 : remis(GEN x, long y)
     854             : {
     855           0 :   pari_sp av = avma;
     856             :   long r;
     857           0 :   (void)divis_rem(x,y, &r); set_avma(av); return stoi(r);
     858             : }
     859             : 
     860             : INLINE GEN
     861           0 : rdivis(GEN x, long y, long prec)
     862             : {
     863           0 :   GEN z = cgetr(prec);
     864           0 :   pari_sp av = avma;
     865           0 :   affrr(divrs(itor(x,prec), y),z);
     866           0 :   set_avma(av); return z;
     867             : }
     868             : INLINE GEN
     869           0 : rdivsi(long x, GEN y, long prec)
     870             : {
     871           0 :   GEN z = cgetr(prec);
     872           0 :   pari_sp av = avma;
     873           0 :   affrr(divsr(x, itor(y,prec)), z);
     874           0 :   set_avma(av); return z;
     875             : }
     876             : INLINE GEN
     877      839647 : rdivss(long x, long y, long prec)
     878             : {
     879      839647 :   GEN z = cgetr(prec);
     880      839647 :   pari_sp av = avma;
     881      839647 :   affrr(divrs(stor(x, prec), y), z);
     882      839647 :   set_avma(av); return z;
     883             : }
     884             : 
     885             : INLINE void
     886           0 : rdiviiz(GEN x, GEN y, GEN z)
     887             : {
     888           0 :   pari_sp av = avma;
     889           0 :   long prec = realprec(z);
     890           0 :   affir(x, z);
     891           0 :   if (!is_bigint(y)) {
     892           0 :     affrr(divrs(z, y[2]), z);
     893           0 :     if (signe(y) < 0) togglesign(z);
     894             :   }
     895             :   else
     896           0 :     affrr(divrr(z, itor(y,prec)), z);
     897           0 :   set_avma(av);
     898           0 : }
     899             : INLINE GEN
     900     4921568 : rdivii(GEN x, GEN y, long prec)
     901             : {
     902     4921568 :   GEN z = itor(x, prec);
     903     4921568 :   pari_sp av = avma;
     904     4921568 :   if (lg(y) == 3) {
     905      707392 :     affrr(divru(z, y[2]), z);
     906      707392 :     if (signe(y) < 0) togglesign(z);
     907             :   }
     908             :   else
     909     4214176 :     affrr(divrr(z, itor(y,prec)), z);
     910     4921568 :   set_avma(av); return z;
     911             : }
     912             : INLINE GEN
     913     4805651 : fractor(GEN x, long prec) { return rdivii(gel(x,1), gel(x,2), prec); }
     914             : 
     915             : INLINE int
     916     6049639 : dvdii(GEN x, GEN y)
     917             : {
     918     6049639 :   pari_sp av = avma;
     919     6049639 :   GEN r = remii(x,y);
     920     6061572 :   return gc_bool(av, r == gen_0);
     921             : }
     922             : INLINE int
     923          84 : dvdsi(long x, GEN y)
     924             : {
     925          84 :   if (!signe(y)) return x == 0;
     926          84 :   if (lgefint(y) != 3) return 0;
     927          84 :   return x % y[2] == 0;
     928             : }
     929             : INLINE int
     930         147 : dvdui(ulong x, GEN y)
     931             : {
     932         147 :   if (!signe(y)) return x == 0;
     933         147 :   if (lgefint(y) != 3) return 0;
     934         147 :   return x % y[2] == 0;
     935             : }
     936             : INLINE int
     937        5558 : dvdis(GEN x, long y)
     938        5558 : { return y? smodis(x, y) == 0: signe(x) == 0; }
     939             : INLINE int
     940        2261 : dvdiu(GEN x, ulong y)
     941        2261 : { return y? umodiu(x, y) == 0: signe(x) == 0; }
     942             : 
     943             : INLINE int
     944           0 : dvdisz(GEN x, long y, GEN z)
     945             : {
     946           0 :   const pari_sp av = avma;
     947             :   long r;
     948           0 :   GEN p1 = divis_rem(x,y, &r);
     949           0 :   set_avma(av); if (r) return 0;
     950           0 :   affii(p1,z); return 1;
     951             : }
     952             : INLINE int
     953           0 : dvdiuz(GEN x, ulong y, GEN z)
     954             : {
     955           0 :   const pari_sp av = avma;
     956             :   ulong r;
     957           0 :   GEN p1 = absdiviu_rem(x,y, &r);
     958           0 :   set_avma(av); if (r) return 0;
     959           0 :   affii(p1,z); return 1;
     960             : }
     961             : INLINE int
     962        3985 : dvdiiz(GEN x, GEN y, GEN z)
     963             : {
     964        3985 :   const pari_sp av=avma;
     965        3985 :   GEN p2, p1 = dvmdii(x,y,&p2);
     966        3985 :   if (signe(p2)) return gc_bool(av,0);
     967        3282 :   affii(p1,z); return gc_bool(av,1);
     968             : }
     969             : 
     970             : INLINE ulong
     971     5423242 : remlll_pre(ulong u2, ulong u1, ulong u0, ulong n, ulong ninv)
     972             : {
     973     5423242 :   u1 = remll_pre(u2, u1, n, ninv);
     974     5423202 :   return remll_pre(u1, u0, n, ninv);
     975             : }
     976             : 
     977             : INLINE ulong
     978  1696204787 : Fl_sqr_pre(ulong a, ulong p, ulong pi)
     979             : {
     980             :   register ulong x;
     981             :   LOCAL_HIREMAINDER;
     982  1696204787 :   x = mulll(a,a);
     983  1696204787 :   return remll_pre(hiremainder, x, p, pi);
     984             : }
     985             : 
     986             : INLINE ulong
     987  2572667454 : Fl_mul_pre(ulong a, ulong b, ulong p, ulong pi)
     988             : {
     989             :   register ulong x;
     990             :   LOCAL_HIREMAINDER;
     991  2572667454 :   x = mulll(a,b);
     992  2572667454 :   return remll_pre(hiremainder, x, p, pi);
     993             : }
     994             : 
     995             : INLINE ulong
     996  4814065333 : Fl_addmul_pre(ulong y0, ulong x0, ulong x1, ulong p, ulong pi)
     997             : {
     998             :   ulong l0, h0;
     999             :   LOCAL_HIREMAINDER;
    1000  4814065333 :   hiremainder = y0;
    1001  4814065333 :   l0 = addmul(x0, x1); h0 = hiremainder;
    1002  4814065333 :   return remll_pre(h0, l0, p, pi);
    1003             : }
    1004             : 
    1005             : INLINE ulong
    1006    47804811 : Fl_addmulmul_pre(ulong x0, ulong y0, ulong x1, ulong y1, ulong p, ulong pi)
    1007             : {
    1008             :   ulong l0, l1, h0, h1;
    1009             :   LOCAL_OVERFLOW;
    1010             :   LOCAL_HIREMAINDER;
    1011    47804811 :   l0 = mulll(x0, y0); h0 = hiremainder;
    1012    47804811 :   l1 = mulll(x1, y1); h1 = hiremainder;
    1013    47804811 :   l0 = addll(l0, l1); h0 = addllx(h0, h1);
    1014    47804811 :   return overflow ? remlll_pre(1, h0, l0, p, pi): remll_pre(h0, l0, p, pi);
    1015             : }
    1016             : 
    1017             : INLINE ulong
    1018      189154 : Fl_ellj_pre(ulong a4, ulong a6, ulong p, ulong pi)
    1019             : {
    1020             :   /* a43 = 4 a4^3 */
    1021      189154 :   ulong a43 = Fl_double(Fl_double(
    1022             :               Fl_mul_pre(a4, Fl_sqr_pre(a4, p, pi), p, pi), p), p);
    1023             :   /* a62 = 27 a6^2 */
    1024      189158 :   ulong a62 = Fl_mul_pre(Fl_sqr_pre(a6, p, pi), 27 % p, p, pi);
    1025      189155 :   ulong z1 = Fl_mul_pre(a43, 1728 % p, p, pi);
    1026      189156 :   ulong z2 = Fl_add(a43, a62, p);
    1027      189154 :   return Fl_div(z1, z2, p);
    1028             : }
    1029             : 
    1030             : /*******************************************************************/
    1031             : /*                                                                 */
    1032             : /*                        MP (INT OR REAL)                         */
    1033             : /*                                                                 */
    1034             : /*******************************************************************/
    1035             : INLINE GEN
    1036          21 : mptrunc(GEN x) { return typ(x)==t_INT? icopy(x): truncr(x); }
    1037             : INLINE GEN
    1038           0 : mpfloor(GEN x) { return typ(x)==t_INT? icopy(x): floorr(x); }
    1039             : INLINE GEN
    1040           0 : mpceil(GEN x) { return typ(x)==t_INT? icopy(x): ceilr(x); }
    1041             : INLINE GEN
    1042     1300804 : mpround(GEN x) { return typ(x) == t_INT? icopy(x): roundr(x); }
    1043             : 
    1044             : INLINE long
    1045      250421 : mpexpo(GEN x) { return typ(x) == t_INT? expi(x): expo(x); }
    1046             : 
    1047             : INLINE GEN
    1048    56497853 : mpadd(GEN x, GEN y)
    1049             : {
    1050    56497853 :   if (typ(x)==t_INT)
    1051    16118173 :     return (typ(y)==t_INT) ? addii(x,y) : addir(x,y);
    1052    40379680 :   return (typ(y)==t_INT) ? addir(y,x) : addrr(x,y);
    1053             : }
    1054             : INLINE GEN
    1055    20897925 : mpsub(GEN x, GEN y)
    1056             : {
    1057    20897925 :   if (typ(x)==t_INT)
    1058     1363912 :     return (typ(y)==t_INT) ? subii(x,y) : subir(x,y);
    1059    19534013 :   return (typ(y)==t_INT) ? subri(x,y) : subrr(x,y);
    1060             : }
    1061             : INLINE GEN
    1062    72602858 : mpmul(GEN x, GEN y)
    1063             : {
    1064    72602858 :   if (typ(x)==t_INT)
    1065    24889618 :     return (typ(y)==t_INT) ? mulii(x,y) : mulir(x,y);
    1066    47713240 :   return (typ(y)==t_INT) ? mulir(y,x) : mulrr(x,y);
    1067             : }
    1068             : INLINE GEN
    1069    11338757 : mpsqr(GEN x) { return (typ(x)==t_INT) ? sqri(x) : sqrr(x); }
    1070             : INLINE GEN
    1071      152735 : mpdiv(GEN x, GEN y)
    1072             : {
    1073      152735 :   if (typ(x)==t_INT)
    1074       78907 :     return (typ(y)==t_INT) ? divii(x,y) : divir(x,y);
    1075       73828 :   return (typ(y)==t_INT) ? divri(x,y) : divrr(x,y);
    1076             : }
    1077             : 
    1078             : /*******************************************************************/
    1079             : /*                                                                 */
    1080             : /*                          Z/nZ, n ULONG                          */
    1081             : /*                                                                 */
    1082             : /*******************************************************************/
    1083             : INLINE ulong
    1084   385439917 : Fl_double(ulong a, ulong p)
    1085             : {
    1086   385439917 :   ulong res = a << 1;
    1087   385439917 :   return (res >= p || res < a) ? res - p : res;
    1088             : }
    1089             : INLINE ulong
    1090    72402504 : Fl_triple(ulong a, ulong p)
    1091             : {
    1092    72402504 :   ulong res = a << 1;
    1093    72402504 :   if (res >= p || res < a) res -= p;
    1094    72402504 :   res += a;
    1095    72402504 :   return (res >= p || res < a)? res - p: res;
    1096             : }
    1097             : INLINE ulong
    1098    14042215 : Fl_halve(ulong a, ulong p)
    1099             : {
    1100             :   ulong ap, ap2;
    1101    14042215 :   if ((a&1UL)==0) return a>>1;
    1102     7087542 :   ap = a + p; ap2 = ap>>1;
    1103     7087542 :   return ap>=a ? ap2: (ap2|HIGHBIT);
    1104             : }
    1105             : 
    1106             : INLINE ulong
    1107  2160634754 : Fl_add(ulong a, ulong b, ulong p)
    1108             : {
    1109  2160634754 :   ulong res = a + b;
    1110  2160634754 :   return (res >= p || res < a) ? res - p : res;
    1111             : }
    1112             : INLINE ulong
    1113    70831326 : Fl_neg(ulong x, ulong p) { return x ? p - x: 0; }
    1114             : 
    1115             : INLINE ulong
    1116  4478987114 : Fl_sub(ulong a, ulong b, ulong p)
    1117             : {
    1118  4478987114 :   ulong res = a - b;
    1119  4478987114 :   return (res > a) ? res + p: res;
    1120             : }
    1121             : 
    1122             : /* centerlift(u mod p) */
    1123             : INLINE long
    1124    12034424 : Fl_center(ulong u, ulong p, ulong ps2) { return (long) (u > ps2)? u - p: u; }
    1125             : 
    1126             : INLINE ulong
    1127   862542848 : Fl_mul(ulong a, ulong b, ulong p)
    1128             : {
    1129             :   register ulong x;
    1130             :   LOCAL_HIREMAINDER;
    1131   862542848 :   x = mulll(a,b);
    1132   862542848 :   if (!hiremainder) return x % p;
    1133   165842605 :   (void)divll(x,p); return hiremainder;
    1134             : }
    1135             : INLINE ulong
    1136    65828823 : Fl_sqr(ulong a, ulong p)
    1137             : {
    1138             :   register ulong x;
    1139             :   LOCAL_HIREMAINDER;
    1140    65828823 :   x = mulll(a,a);
    1141    65828823 :   if (!hiremainder) return x % p;
    1142    37961399 :   (void)divll(x,p); return hiremainder;
    1143             : }
    1144             : INLINE ulong
    1145     8142241 : Fl_div(ulong a, ulong b, ulong p) { return Fl_mul(a, Fl_inv(b, p), p); }
    1146             : 
    1147             : /*******************************************************************/
    1148             : /*                                                                 */
    1149             : /*        DEFINED FROM EXISTING ONE EXPLOITING COMMUTATIVITY       */
    1150             : /*                                                                 */
    1151             : /*******************************************************************/
    1152             : INLINE GEN
    1153      208652 : addri(GEN x, GEN y) { return addir(y,x); }
    1154             : INLINE GEN
    1155    62266459 : addis(GEN x, long s) { return addsi(s,x); }
    1156             : INLINE GEN
    1157    64701833 : addiu(GEN x, ulong s) { return addui(s,x); }
    1158             : INLINE GEN
    1159     3264774 : addrs(GEN x, long s) { return addsr(s,x); }
    1160             : 
    1161             : INLINE GEN
    1162    29936046 : subiu(GEN x, long y) { GEN z = subui(y, x); togglesign(z); return z; }
    1163             : INLINE GEN
    1164        1653 : subis(GEN x, long y) { return addsi(-y,x); }
    1165             : INLINE GEN
    1166     3489919 : subrs(GEN x, long y) { return addsr(-y,x); }
    1167             : 
    1168             : INLINE GEN
    1169   143248562 : mulis(GEN x, long s) { return mulsi(s,x); }
    1170             : INLINE GEN
    1171   607393812 : muliu(GEN x, ulong s) { return mului(s,x); }
    1172             : INLINE GEN
    1173     3116183 : mulru(GEN x, ulong s) { return mulur(s,x); }
    1174             : INLINE GEN
    1175    20913351 : mulri(GEN x, GEN s) { return mulir(s,x); }
    1176             : INLINE GEN
    1177    10015591 : mulrs(GEN x, long s) { return mulsr(s,x); }
    1178             : 
    1179             : /*******************************************************************/
    1180             : /*                                                                 */
    1181             : /*                  VALUATION, EXPONENT, SHIFTS                    */
    1182             : /*                                                                 */
    1183             : /*******************************************************************/
    1184             : INLINE long
    1185    60670017 : vali(GEN x)
    1186             : {
    1187             :   long i;
    1188             :   GEN xp;
    1189             : 
    1190    60670017 :   if (!signe(x)) return -1;
    1191    60669849 :   xp=int_LSW(x);
    1192    60669849 :   for (i=0; !*xp; i++) xp=int_nextW(xp);
    1193    60669849 :   return vals(*xp) + i * BITS_IN_LONG;
    1194             : }
    1195             : 
    1196             : 
    1197             : /* assume x > 0 */
    1198             : INLINE long
    1199   388520266 : expu(ulong x) { return (BITS_IN_LONG-1) - (long)bfffo(x); }
    1200             : 
    1201             : INLINE long
    1202   616378468 : expi(GEN x)
    1203             : {
    1204   616378468 :   const long lx=lgefint(x);
    1205   616378468 :   return lx==2? -(long)HIGHEXPOBIT: bit_accuracy(lx)-(long)bfffo(*int_MSW(x))-1;
    1206             : }
    1207             : 
    1208             : INLINE GEN
    1209    66524504 : shiftr(GEN x, long n)
    1210             : {
    1211    66524504 :   const long e = evalexpo(expo(x)+n);
    1212    66524504 :   const GEN y = rcopy(x);
    1213             : 
    1214    66524504 :   if (e & ~EXPOBITS) pari_err_OVERFLOW("expo()");
    1215    66524504 :   y[1] = (y[1]&~EXPOBITS) | e; return y;
    1216             : }
    1217             : INLINE GEN
    1218    17085219 : mpshift(GEN x,long s) { return (typ(x)==t_INT)?shifti(x,s):shiftr(x,s); }
    1219             : 
    1220             : /* FIXME: adapt/use mpn_[lr]shift instead */
    1221             : /* z2[imin..imax] := z1[imin..imax].f shifted left sh bits
    1222             :  * (feeding f from the right). Assume sh > 0 */
    1223             : INLINE void
    1224  3204563512 : shift_left(GEN z2, GEN z1, long imin, long imax, ulong f,  ulong sh)
    1225             : {
    1226  3204563512 :   GEN sb = z1 + imin, se = z1 + imax, te = z2 + imax;
    1227  3204563512 :   ulong l, m = BITS_IN_LONG - sh, k = f >> m;
    1228 18645524782 :   while (se > sb) {
    1229 12236397758 :     l     = *se--;
    1230 12236397758 :     *te-- = (l << sh) | k;
    1231 12236397758 :     k     = l >> m;
    1232             :   }
    1233  3204563512 :   *te = (((ulong)*se) << sh) | k;
    1234  3204563512 : }
    1235             : /* z2[imin..imax] := f.z1[imin..imax-1] shifted right sh bits
    1236             :  * (feeding f from the left). Assume sh > 0 */
    1237             : INLINE void
    1238  2690851575 : shift_right(GEN z2, GEN z1, long imin, long imax, ulong f, ulong sh)
    1239             : {
    1240  2690851575 :   GEN sb = z1 + imin, se = z1 + imax, tb = z2 + imin;
    1241  2690851575 :   ulong k, l = *sb++, m = BITS_IN_LONG - sh;
    1242  2690851575 :   *tb++ = (l >> sh) | (f << m);
    1243 14768273246 :   while (sb < se) {
    1244  9386570096 :     k     = l << m;
    1245  9386570096 :     l     = *sb++;
    1246  9386570096 :     *tb++ = (l >> sh) | k;
    1247             :   }
    1248  2690851575 : }
    1249             : 
    1250             : /* Backward compatibility. Inefficient && unused */
    1251             : extern ulong hiremainder;
    1252             : INLINE ulong
    1253           0 : shiftl(ulong x, ulong y)
    1254           0 : { hiremainder = x>>(BITS_IN_LONG-y); return (x<<y); }
    1255             : 
    1256             : INLINE ulong
    1257           0 : shiftlr(ulong x, ulong y)
    1258           0 : { hiremainder = x<<(BITS_IN_LONG-y); return (x>>y); }
    1259             : 
    1260             : INLINE void
    1261    90281831 : shiftr_inplace(GEN z, long d)
    1262             : {
    1263    90281831 :   setexpo(z, expo(z)+d);
    1264    90281831 : }
    1265             : 
    1266             : /*******************************************************************/
    1267             : /*                                                                 */
    1268             : /*                           ASSIGNMENT                            */
    1269             : /*                                                                 */
    1270             : /*******************************************************************/
    1271             : INLINE void
    1272    70139568 : affii(GEN x, GEN y)
    1273             : {
    1274    70139568 :   long lx = lgefint(x);
    1275    70139568 :   if (lg(y)<lx) pari_err_OVERFLOW("t_INT-->t_INT assignment");
    1276    70139569 :   while (--lx) y[lx] = x[lx];
    1277    70139569 : }
    1278             : INLINE void
    1279      432492 : affsi(long s, GEN x)
    1280             : {
    1281      432492 :   if (!s) x[1] = evalsigne(0) | evallgefint(2);
    1282             :   else
    1283             :   {
    1284      410302 :     if (s > 0) { x[1] = evalsigne( 1) | evallgefint(3); x[2] =  s; }
    1285      152796 :     else       { x[1] = evalsigne(-1) | evallgefint(3); x[2] = -s; }
    1286             :   }
    1287      432492 : }
    1288             : INLINE void
    1289     8404752 : affui(ulong u, GEN x)
    1290             : {
    1291     8404752 :   if (!u) x[1] = evalsigne(0) | evallgefint(2);
    1292     8403478 :   else  { x[1] = evalsigne(1) | evallgefint(3); x[2] = u; }
    1293     8404752 : }
    1294             : 
    1295             : INLINE void
    1296   183968353 : affsr(long x, GEN y)
    1297             : {
    1298   183968353 :   long sh, i, ly = lg(y);
    1299             : 
    1300   183968353 :   if (!x)
    1301             :   {
    1302       71022 :     y[1] = evalexpo(-prec2nbits(ly));
    1303       71022 :     return;
    1304             :   }
    1305   183897331 :   if (x < 0) {
    1306      170752 :     x = -x; sh = bfffo(x);
    1307      170752 :     y[1] = evalsigne(-1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1308             :   }
    1309             :   else
    1310             :   {
    1311   183726579 :     sh = bfffo(x);
    1312   183726579 :     y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1313             :   }
    1314   183897331 :   y[2] = ((ulong)x)<<sh; for (i=3; i<ly; i++) y[i]=0;
    1315             : }
    1316             : 
    1317             : INLINE void
    1318     8780595 : affur(ulong x, GEN y)
    1319             : {
    1320     8780595 :   long sh, i, ly = lg(y);
    1321             : 
    1322     8780595 :   if (!x)
    1323             :   {
    1324     1296306 :     y[1] = evalexpo(-prec2nbits(ly));
    1325     1296306 :     return;
    1326             :   }
    1327     7484289 :   sh = bfffo(x);
    1328     7484289 :   y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1329     7484289 :   y[2] = x<<sh; for (i=3; i<ly; i++) y[i] = 0;
    1330             : }
    1331             : 
    1332             : INLINE void
    1333      138568 : affiz(GEN x, GEN y) { if (typ(y)==t_INT) affii(x,y); else affir(x,y); }
    1334             : INLINE void
    1335           0 : affsz(long x, GEN y) { if (typ(y)==t_INT) affsi(x,y); else affsr(x,y); }
    1336             : INLINE void
    1337      162582 : mpaff(GEN x, GEN y) { if (typ(x)==t_INT) affiz(x, y); else affrr(x,y); }
    1338             : 
    1339             : /*******************************************************************/
    1340             : /*                                                                 */
    1341             : /*                    OPERATION + ASSIGNMENT                       */
    1342             : /*                                                                 */
    1343             : /*******************************************************************/
    1344             : 
    1345           0 : INLINE void addiiz(GEN x, GEN y, GEN z)
    1346           0 : { pari_sp av = avma; affii(addii(x,y),z); set_avma(av); }
    1347           0 : INLINE void addirz(GEN x, GEN y, GEN z)
    1348           0 : { pari_sp av = avma; affrr(addir(x,y),z); set_avma(av); }
    1349           0 : INLINE void addriz(GEN x, GEN y, GEN z)
    1350           0 : { pari_sp av = avma; affrr(addri(x,y),z); set_avma(av); }
    1351     1306252 : INLINE void addrrz(GEN x, GEN y, GEN z)
    1352     1306252 : { pari_sp av = avma; affrr(addrr(x,y),z); set_avma(av); }
    1353           0 : INLINE void addsiz(long s, GEN y, GEN z)
    1354           0 : { pari_sp av = avma; affii(addsi(s,y),z); set_avma(av); }
    1355           0 : INLINE void addsrz(long s, GEN y, GEN z)
    1356           0 : { pari_sp av = avma; affrr(addsr(s,y),z); set_avma(av); }
    1357           0 : INLINE void addssz(long s, long y, GEN z)
    1358           0 : { pari_sp av = avma; affii(addss(s,y),z); set_avma(av); }
    1359             : 
    1360          14 : INLINE void diviiz(GEN x, GEN y, GEN z)
    1361          14 : { pari_sp av = avma; affii(divii(x,y),z); set_avma(av); }
    1362           0 : INLINE void divirz(GEN x, GEN y, GEN z)
    1363           0 : { pari_sp av = avma; mpaff(divir(x,y),z); set_avma(av); }
    1364           0 : INLINE void divisz(GEN x, long y, GEN z)
    1365           0 : { pari_sp av = avma; affii(divis(x,y),z); set_avma(av); }
    1366           0 : INLINE void divriz(GEN x, GEN y, GEN z)
    1367           0 : { pari_sp av = avma; affrr(divri(x,y),z); set_avma(av); }
    1368         404 : INLINE void divrrz(GEN x, GEN y, GEN z)
    1369         404 : { pari_sp av = avma; affrr(divrr(x,y),z); set_avma(av); }
    1370           0 : INLINE void divrsz(GEN y, long s, GEN z)
    1371           0 : { pari_sp av = avma; affrr(divrs(y,s),z); set_avma(av); }
    1372           0 : INLINE void divsiz(long x, GEN y, GEN z)
    1373           0 : { long junk; affsi(sdivsi_rem(x,y,&junk), z); }
    1374           0 : INLINE void divsrz(long s, GEN y, GEN z)
    1375           0 : { pari_sp av = avma; mpaff(divsr(s,y),z); set_avma(av); }
    1376           0 : INLINE void divssz(long x, long y, GEN z)
    1377           0 : { affsi(x/y, z); }
    1378             : 
    1379           0 : INLINE void modisz(GEN y, long s, GEN z)
    1380           0 : { affsi(smodis(y,s),z); }
    1381           0 : INLINE void modsiz(long s, GEN y, GEN z)
    1382           0 : { pari_sp av = avma; affii(modsi(s,y),z); set_avma(av); }
    1383           0 : INLINE void modssz(long s, long y, GEN z)
    1384           0 : { affsi(smodss(s,y),z); }
    1385             : 
    1386           0 : INLINE void mpaddz(GEN x, GEN y, GEN z)
    1387           0 : { pari_sp av = avma; mpaff(mpadd(x,y),z); set_avma(av); }
    1388           0 : INLINE void mpsubz(GEN x, GEN y, GEN z)
    1389           0 : { pari_sp av = avma; mpaff(mpsub(x,y),z); set_avma(av); }
    1390           0 : INLINE void mpmulz(GEN x, GEN y, GEN z)
    1391           0 : { pari_sp av = avma; mpaff(mpmul(x,y),z); set_avma(av); }
    1392             : 
    1393           0 : INLINE void muliiz(GEN x, GEN y, GEN z)
    1394           0 : { pari_sp av = avma; affii(mulii(x,y),z); set_avma(av); }
    1395           0 : INLINE void mulirz(GEN x, GEN y, GEN z)
    1396           0 : { pari_sp av = avma; mpaff(mulir(x,y),z); set_avma(av); }
    1397           0 : INLINE void mulriz(GEN x, GEN y, GEN z)
    1398           0 : { pari_sp av = avma; mpaff(mulri(x,y),z); set_avma(av); }
    1399           0 : INLINE void mulrrz(GEN x, GEN y, GEN z)
    1400           0 : { pari_sp av = avma; affrr(mulrr(x,y),z); set_avma(av); }
    1401           0 : INLINE void mulsiz(long s, GEN y, GEN z)
    1402           0 : { pari_sp av = avma; affii(mulsi(s,y),z); set_avma(av); }
    1403           0 : INLINE void mulsrz(long s, GEN y, GEN z)
    1404           0 : { pari_sp av = avma; mpaff(mulsr(s,y),z); set_avma(av); }
    1405           0 : INLINE void mulssz(long s, long y, GEN z)
    1406           0 : { pari_sp av = avma; affii(mulss(s,y),z); set_avma(av); }
    1407             : 
    1408           0 : INLINE void remiiz(GEN x, GEN y, GEN z)
    1409           0 : { pari_sp av = avma; affii(remii(x,y),z); set_avma(av); }
    1410           0 : INLINE void remisz(GEN y, long s, GEN z)
    1411           0 : { pari_sp av = avma; affii(remis(y,s),z); set_avma(av); }
    1412           0 : INLINE void remsiz(long s, GEN y, GEN z)
    1413           0 : { pari_sp av = avma; affii(remsi(s,y),z); set_avma(av); }
    1414           0 : INLINE void remssz(long s, long y, GEN z)
    1415           0 : { pari_sp av = avma; affii(remss(s,y),z); set_avma(av); }
    1416             : 
    1417           0 : INLINE void subiiz(GEN x, GEN y, GEN z)
    1418           0 : { pari_sp av = avma; affii(subii(x,y),z); set_avma(av); }
    1419           0 : INLINE void subirz(GEN x, GEN y, GEN z)
    1420           0 : { pari_sp av = avma; affrr(subir(x,y),z); set_avma(av); }
    1421           0 : INLINE void subisz(GEN y, long s, GEN z)
    1422           0 : { pari_sp av = avma; affii(addsi(-s,y),z); set_avma(av); }
    1423           0 : INLINE void subriz(GEN x, GEN y, GEN z)
    1424           0 : { pari_sp av = avma; affrr(subri(x,y),z); set_avma(av); }
    1425     1295880 : INLINE void subrrz(GEN x, GEN y, GEN z)
    1426     1295880 : { pari_sp av = avma; affrr(subrr(x,y),z); set_avma(av); }
    1427           0 : INLINE void subrsz(GEN y, long s, GEN z)
    1428           0 : { pari_sp av = avma; affrr(addsr(-s,y),z); set_avma(av); }
    1429           0 : INLINE void subsiz(long s, GEN y, GEN z)
    1430           0 : { pari_sp av = avma; affii(subsi(s,y),z); set_avma(av); }
    1431           0 : INLINE void subsrz(long s, GEN y, GEN z)
    1432           0 : { pari_sp av = avma; affrr(subsr(s,y),z); set_avma(av); }
    1433           0 : INLINE void subssz(long x, long y, GEN z) { addssz(x,-y,z); }
    1434             : 
    1435             : INLINE void
    1436           0 : dvmdssz(long x, long y, GEN z, GEN t) {
    1437           0 :   pari_sp av = avma;
    1438             :   long r;
    1439           0 :   affii(divss_rem(x,y, &r), z); set_avma(av); affsi(r,t);
    1440           0 : }
    1441             : INLINE void
    1442           0 : dvmdsiz(long x, GEN y, GEN z, GEN t) {
    1443           0 :   pari_sp av = avma;
    1444             :   long r;
    1445           0 :   affii(divsi_rem(x,y, &r), z); set_avma(av); affsi(r,t);
    1446           0 : }
    1447             : INLINE void
    1448           0 : dvmdisz(GEN x, long y, GEN z, GEN t) {
    1449           0 :   pari_sp av = avma;
    1450             :   long r;
    1451           0 :   affii(divis_rem(x,y, &r),z); set_avma(av); affsi(r,t);
    1452           0 : }
    1453             : INLINE void
    1454           0 : dvmdiiz(GEN x, GEN y, GEN z, GEN t) {
    1455           0 :   pari_sp av = avma;
    1456             :   GEN r;
    1457           0 :   affii(dvmdii(x,y,&r),z); affii(r,t); set_avma(av);
    1458           0 : }

Generated by: LCOV version 1.13