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.18.1 lcov report (development 30338-25bc3391a4) Lines: 596 671 88.8 %
Date: 2025-06-21 09:20:53 Functions: 213 237 89.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #line 2 "../src/kernel/none/level1.h"
       2             : /* Copyright (C) 2000  The PARI group.
       3             : 
       4             : This file is part of the PARI/GP package.
       5             : 
       6             : PARI/GP is free software; you can redistribute it and/or modify it under the
       7             : terms of the GNU General Public License as published by the Free Software
       8             : Foundation; 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  2380053531 : nbits2lg(long x) {
      22  2380053531 :   return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
      23             : }
      24             : INLINE long
      25   806337373 : lg2prec(long x){ return (x-2) * BITS_IN_LONG; }
      26             : 
      27             : INLINE long
      28 95197982490 : evallg(long x)
      29             : {
      30 95197982490 :   if (x & ~LGBITS) pari_err_OVERFLOW("lg()");
      31 95201116148 :   return _evallg(x);
      32             : }
      33             : INLINE long
      34    81621061 : evalvalp(long x)
      35             : {
      36    81621061 :   long v = _evalvalp(x);
      37    81621061 :   if (v & ~VALPBITS) pari_err_OVERFLOW("valp()");
      38    81621328 :   return v;
      39             : }
      40             : INLINE long
      41    21443016 : evalvalser(long x)
      42             : {
      43    21443016 :   long v = _evalvalser(x);
      44    21443016 :   if (v & ~VALSERBITS) pari_err_OVERFLOW("valser()");
      45    21443016 :   return v;
      46             : }
      47             : INLINE long
      48 13215146826 : evalexpo(long x)
      49             : {
      50 13215146826 :   long v = _evalexpo(x);
      51 13215146826 :   if (v & ~EXPOBITS) pari_err_OVERFLOW("expo()");
      52 13218521879 :   return v;
      53             : }
      54             : INLINE long
      55    78664494 : evalprecp(long x)
      56             : {
      57    78664494 :   long v = _evalprecp(x);
      58    78664494 :   if (x & ~((1UL<<(BITS_IN_LONG-VALPnumBITS))-1)) pari_err_OVERFLOW("precp()");
      59    78664535 :   return v;
      60             : }
      61             : 
      62             : INLINE int
      63   217148594 : varncmp(long x, long y)
      64             : {
      65   217148594 :   if (varpriority[x] < varpriority[y]) return  1;
      66   152468294 :   if (varpriority[x] > varpriority[y]) return -1;
      67    73307690 :   return 0;
      68             : }
      69             : INLINE long
      70       15547 : varnmin(long x, long y)
      71       15547 : { 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  3623248465 : stackdummy(pari_sp av, pari_sp ltop) {
      85  3623248465 :   long l = ((GEN)av) - ((GEN)ltop);
      86  3623248465 :   if (l > 0) {
      87  1217757500 :     GEN z = (GEN)ltop;
      88  1217757500 :     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  3623521639 : }
      94             : INLINE void
      95   103804391 : fixlg(GEN x, long ly) {
      96   103804391 :   long lx = lg(x), l = lx - ly;
      97   103804391 :   if (l > 0)
      98             :   { /* stackdummy(x+lx, x+ly) */
      99    47962337 :     GEN z = x + ly;
     100    47962337 :     z[0] = evaltyp(t_VECSMALL) | evallg(l);
     101    47962344 :     setlg(x, ly);
     102             : #ifdef DEBUG
     103             :     { long i; for (i = 1; i < l; i++) z[i] = 0; }
     104             : #endif
     105             :   }
     106   103804389 : }
     107             : /* update lg(z) before affrr(y, z)  [ to cater for precision loss ]*/
     108             : INLINE void
     109    55855733 : 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 >12881*10^7 : set_avma(ulong av) { avma = av; }
     120             : 
     121             : INLINE double
     122   182072480 : gc_double(pari_sp av, double d) { set_avma(av); return d; }
     123             : INLINE long
     124   239206931 : gc_long(pari_sp av, long s) { set_avma(av); return s; }
     125             : INLINE ulong
     126    37199897 : gc_ulong(pari_sp av, ulong s) { set_avma(av); return s; }
     127             : INLINE int
     128    51923055 : gc_bool(pari_sp av, int s) { set_avma(av); return s; }
     129             : INLINE int
     130     2574153 : gc_int(pari_sp av, int s) { set_avma(av); return s; }
     131             : INLINE GEN
     132     7901127 : gc_NULL(pari_sp av) { set_avma(av); return NULL; }
     133             : INLINE GEN
     134 15488284058 : 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      150799 : gc_stoi(pari_sp av, long x) { set_avma(av); return stoi(x); }
     140             : INLINE GEN
     141      468633 : gc_utoi(pari_sp av, ulong x) { set_avma(av); return utoi(x); }
     142             : INLINE GEN
     143     1153906 : gc_utoipos(pari_sp av, ulong x) { set_avma(av); return utoipos(x); }
     144             : 
     145             : INLINE GEN
     146 93225451129 : new_chunk(size_t x) /* x is a number of longs */
     147             : {
     148 93225451129 :   GEN z = ((GEN) avma) - x;
     149             :   CHECK_CTRLC
     150 93225451129 :   if (x > (avma-pari_mainstack->bot) / sizeof(long))
     151          14 :     new_chunk_resize(x);
     152 93225451115 :   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 93198945203 :   return z;
     165             : }
     166             : 
     167             : INLINE char *
     168    46469931 : stack_malloc(size_t N)
     169             : {
     170    46469931 :   long n = nchar2nlong(N);
     171    46469914 :   return (char*)new_chunk(n);
     172             : }
     173             : 
     174             : INLINE char *
     175    55380775 : stack_malloc_align(size_t N, long k)
     176             : {
     177    55380775 :   ulong d = ((ulong)avma) % k, e = ((ulong)N) % k;
     178    55380775 :   if (d) (void)new_chunk(d/sizeof(long));
     179    55380772 :   if (e) N += k-e;
     180    55380772 :   return (char*) new_chunk(nchar2nlong(N));
     181             : }
     182             : 
     183             : INLINE char *
     184      109432 : stack_calloc(size_t N)
     185             : {
     186      109432 :   char *p = stack_malloc(N);
     187      109432 :   memset(p, 0, N); return p;
     188             : }
     189             : 
     190             : INLINE char *
     191        3300 : stack_calloc_align(size_t N, long k)
     192             : {
     193        3300 :   ulong d = ((ulong)avma) % k, e = ((ulong)N) % k;
     194        3300 :   if (d) (void)new_chunk(d/sizeof(long));
     195        3300 :   if (e) N += k-e;
     196        3300 :   return stack_calloc(N);
     197             : }
     198             : 
     199             : /* cgetg(lg(x), typ(x)), set *lx. Implicit unsetisclone() */
     200             : INLINE GEN
     201  1469665618 : cgetg_copy(GEN x, long *plx) {
     202             :   GEN y;
     203  1469665618 :   *plx = lg(x); y = new_chunk((size_t)*plx);
     204  1469657927 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     205             : }
     206             : INLINE GEN
     207      392979 : cgetg_block(long x, long y)
     208             : {
     209      392979 :   GEN z = newblock((size_t)x);
     210      392971 :   z[0] = CLONEBIT | evaltyp(y) | evallg(x);
     211      392972 :   return z;
     212             : }
     213             : INLINE GEN
     214 24427374983 : cgetg(long x, long y)
     215             : {
     216 24427374983 :   GEN z = new_chunk((size_t)x);
     217 24416590233 :   z[0] = evaltyp(y) | evallg(x);
     218 24402359445 :   return z;
     219             : }
     220             : INLINE GEN
     221 27204779939 : cgeti(long x)
     222             : {
     223 27204779939 :   GEN z = new_chunk((size_t)x);
     224 27170169590 :   z[0] = evaltyp(t_INT) | evallg(x);
     225 27151014573 :   return z;
     226             : }
     227             : INLINE GEN
     228 16392827502 : cgetipos(long x)
     229             : {
     230 16392827502 :   GEN z = cgeti(x);
     231 16372816649 :   z[1] = evalsigne(1) | evallgefint(x);
     232 16372816649 :   return z;
     233             : }
     234             : INLINE GEN
     235   268095194 : cgetineg(long x)
     236             : {
     237   268095194 :   GEN z = cgeti(x);
     238   268093461 :   z[1] = evalsigne(-1) | evallgefint(x);
     239   268093461 :   return z;
     240             : }
     241             : INLINE GEN
     242       43478 : cgetr_block(long x)
     243             : {
     244       43478 :   long l = nbits2lg(x);
     245       43477 :   GEN z = newblock((size_t)l);
     246       43496 :   z[0] = CLONEBIT | evaltyp(t_REAL) | evallg(l);
     247       43495 :   return z;
     248             : }
     249             : INLINE GEN
     250  1855977082 : cgetr(long x)
     251             : {
     252  1855977082 :   long l = nbits2lg(x);
     253  1855689917 :   GEN z = new_chunk((size_t)l);
     254  1855193995 :   z[0] = evaltyp(t_REAL) | evallg(l);
     255  1854928493 :   return z;
     256             : }
     257             : 
     258             : /*******************************************************************/
     259             : /*                                                                 */
     260             : /*                     COPY, NEGATION, ABSOLUTE VALUE              */
     261             : /*                                                                 */
     262             : /*******************************************************************/
     263             : /* cannot do memcpy because sometimes x and y overlap */
     264             : INLINE GEN
     265  4953490262 : leafcopy(GEN x)
     266             : {
     267  4953490262 :   long lx = lg(x);
     268  4953490262 :   GEN y = new_chunk(lx); /* can't use cgetg_copy, in case x,y overlap */
     269 24835628895 :   while (--lx > 0) y[lx] = x[lx];
     270  4952953839 :   y[0] = x[0] & (TYPBITS|LGBITS); return y;
     271             : }
     272             : INLINE GEN
     273  9077485887 : icopy(GEN x)
     274             : {
     275  9077485887 :   long i = lgefint(x), lx = i;
     276  9077485887 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     277 39767087201 :   while (--i > 0) y[i] = x[i];
     278  9075597011 :   y[0] = evaltyp(t_INT) | evallg(lx);
     279  9088863974 :   return y;
     280             : }
     281             : INLINE GEN
     282   116479305 : icopyspec(GEN x, long nx)
     283             : {
     284   116479305 :   long i = nx+2, lx = i;
     285   116479305 :   GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
     286  3119614974 :   x -= 2; while (--i >= 2) y[i] = x[i];
     287   116478930 :   y[1] = evalsigne(1) | evallgefint(lx);
     288   116478930 :   y[0] = evaltyp(t_INT) | evallg(lx);
     289   116478832 :   return y;
     290             : }
     291   898987678 : INLINE GEN rcopy(GEN x) { return leafcopy(x); }
     292         708 : INLINE GEN mpcopy(GEN x) { return leafcopy(x); }
     293             : 
     294             : INLINE GEN
     295  2135088127 : mpabs(GEN x) { GEN y = leafcopy(x); setabssign(y); return y; }
     296             : INLINE GEN
     297    13428463 : mpabs_shallow(GEN x) { return signe(x) < 0? mpabs(x): x; }
     298  2063908507 : INLINE GEN absi(GEN x) { return mpabs(x); }
     299    57700065 : INLINE GEN absi_shallow(GEN x) { return signe(x) < 0? negi(x): x; }
     300         140 : INLINE GEN absr(GEN x) { return mpabs(x); }
     301             : 
     302             : INLINE GEN
     303   920423374 : mpneg(GEN x) { GEN y = leafcopy(x); togglesign(y); return y; }
     304   641566246 : INLINE GEN negi(GEN x) { return mpneg(x); }
     305     3692506 : INLINE GEN negr(GEN x) { return mpneg(x); }
     306             : 
     307             : /* negate in place */
     308             : INLINE void
     309  1924687963 : togglesign(GEN x) { if (x[1] & SIGNBITS) { x[1] ^= HIGHBIT; } }
     310             : INLINE void
     311  2201326118 : setabssign(GEN x) { x[1] &= ~HIGHBIT; }
     312             : /* negate in place, except universal constants */
     313             : INLINE void
     314   125041704 : togglesign_safe(GEN *px)
     315             : {
     316   125041704 :   switch(*px - gen_1) /* gen_1, gen_2, gen_m1, gen_m2 */
     317             :   {
     318     2559333 :     case 0: *px = gen_m1; break;
     319           4 :     case 3: *px = gen_m2;  break;
     320      569162 :     case 6: *px = gen_1; break;
     321           0 :     case 9: *px = gen_2;  break;
     322   121913205 :     default: togglesign(*px);
     323             :   }
     324   125044964 : }
     325             : /* setsigne(y, signe(x)) */
     326             : INLINE void
     327           0 : affectsign(GEN x, GEN y)
     328             : {
     329           0 :   y[1] = (x[1] & SIGNBITS) | (y[1] & ~SIGNBITS);
     330           0 : }
     331             : /* copies sign in place, except for universal constants */
     332             : INLINE void
     333    10736318 : affectsign_safe(GEN x, GEN *py)
     334             : {
     335    10736318 :   if (((*py)[1] ^ x[1]) & HIGHBIT) togglesign_safe(py);
     336    10736318 : }
     337             : /*******************************************************************/
     338             : /*                                                                 */
     339             : /*                     GEN -> LONG, LONG -> GEN                    */
     340             : /*                                                                 */
     341             : /*******************************************************************/
     342             : /* assume x != 0, return -x as a t_INT */
     343             : INLINE GEN
     344   267237635 : utoineg(ulong x) { GEN y = cgetineg(3); y[2] = x; return y; }
     345             : /* assume x != 0, return utoi(x) */
     346             : INLINE GEN
     347 14213426905 : utoipos(ulong x) { GEN y = cgetipos(3); y[2] = x; return y; }
     348             : INLINE GEN
     349 11861269644 : utoi(ulong x) { return x? utoipos(x): gen_0; }
     350             : INLINE GEN
     351   745401479 : stoi(long x)
     352             : {
     353   745401479 :   if (!x) return gen_0;
     354   523809748 :   return x > 0? utoipos((ulong)x): utoineg((ulong)-x);
     355             : }
     356             : 
     357             : /* x 2^BIL + y */
     358             : INLINE GEN
     359  8804574786 : uutoi(ulong x, ulong y)
     360             : {
     361             :   GEN z;
     362  8804574786 :   if (!x) return utoi(y);
     363   850823809 :   z = cgetipos(4);
     364   857876077 :   *int_W_lg(z, 1, 4) = x;
     365   857876077 :   *int_W_lg(z, 0, 4) = y; return z;
     366             : }
     367             : /* - (x 2^BIL + y) */
     368             : INLINE GEN
     369      355742 : uutoineg(ulong x, ulong y)
     370             : {
     371             :   GEN z;
     372      355742 :   if (!x) return y? utoineg(y): gen_0;
     373        9379 :   z = cgetineg(4);
     374        9379 :   *int_W_lg(z, 1, 4) = x;
     375        9379 :   *int_W_lg(z, 0, 4) = y; return z;
     376             : }
     377             : 
     378             : INLINE long
     379   458817392 : itos(GEN x)
     380             : {
     381   458817392 :   long s = signe(x);
     382             :   long u;
     383             : 
     384   458817392 :   if (!s) return 0;
     385   429856292 :   u = x[2];
     386   429856292 :   if (lgefint(x) > 3 || u < 0)
     387          32 :     pari_err_OVERFLOW("t_INT-->long assignment");
     388   429856466 :   return (s>0) ? u : -u;
     389             : }
     390             : /* as itos, but return 0 if too large. Cf is_bigint */
     391             : INLINE long
     392    24090669 : itos_or_0(GEN x) {
     393             :   long n;
     394    24090669 :   if (lgefint(x) != 3 || (n = x[2]) & HIGHBIT) return 0;
     395    22167747 :   return signe(x) > 0? n: -n;
     396             : }
     397             : INLINE ulong
     398   171933376 : itou(GEN x)
     399             : {
     400   171933376 :   switch(lgefint(x)) {
     401    13080420 :     case 2: return 0;
     402   158853136 :     case 3: return x[2];
     403           0 :     default:
     404           0 :       pari_err_OVERFLOW("t_INT-->ulong assignment");
     405             :       return 0; /* LCOV_EXCL_LINE */
     406             :   }
     407             : }
     408             : 
     409             : /* as itou, but return 0 if too large. Cf is_bigint */
     410             : INLINE ulong
     411     2996088 : itou_or_0(GEN x) {
     412     2996088 :   if (lgefint(x) != 3) return 0;
     413     2975009 :   return (ulong)x[2];
     414             : }
     415             : 
     416             : INLINE ulong
     417     5500656 : umuluu_or_0(ulong x, ulong y)
     418             : {
     419             :   ulong z;
     420             :   LOCAL_HIREMAINDER;
     421     5500656 :   z = mulll(x, y);
     422     5500656 :   return hiremainder? 0: z;
     423             : }
     424             : /* return x*y if <= n, else 0. Beware overflow */
     425             : INLINE ulong
     426     5800680 : umuluu_le(ulong x, ulong y, ulong n)
     427             : {
     428             :   ulong z;
     429             :   LOCAL_HIREMAINDER;
     430     5800680 :   z = mulll(x, y);
     431     5800680 :   return (hiremainder || z > n)? 0: z;
     432             : }
     433             : 
     434             : INLINE GEN
     435   478474135 : real_0_bit(long bitprec) { GEN x=cgetg(2, t_REAL); x[1]=evalexpo(bitprec); return x; }
     436             : INLINE GEN
     437     1155978 : real_0(long prec) { return real_0_bit(-prec); }
     438             : INLINE GEN
     439     4700774 : real_1_bit(long bit) { return real_1(nbits2prec(bit)); }
     440             : INLINE GEN
     441   131551922 : real_1(long prec) {
     442   131551922 :   GEN x = cgetr(prec);
     443   131530776 :   long i, l = lg(x);
     444   131530776 :   x[1] = evalsigne(1) | _evalexpo(0);
     445   657074768 :   x[2] = (long)HIGHBIT; for (i=3; i<l; i++) x[i] = 0;
     446   131530776 :   return x;
     447             : }
     448             : INLINE GEN
     449         455 : real_m1(long prec) {
     450         455 :   GEN x = cgetr(prec);
     451         455 :   long i, l = lg(x);
     452         455 :   x[1] = evalsigne(-1) | _evalexpo(0);
     453        1761 :   x[2] = (long)HIGHBIT; for (i=3; i<l; i++) x[i] = 0;
     454         455 :   return x;
     455             : }
     456             : 
     457             : /* 2.^n */
     458             : INLINE GEN
     459     1060990 : real2n(long n, long prec) { GEN z = real_1(prec); setexpo(z, n); return z; }
     460             : INLINE GEN
     461         126 : real_m2n(long n, long prec) { GEN z = real_m1(prec); setexpo(z, n); return z; }
     462             : INLINE GEN
     463   495763254 : stor(long s, long prec) { GEN z = cgetr(prec); affsr(s,z); return z; }
     464             : INLINE GEN
     465    13443764 : utor(ulong s, long prec){ GEN z = cgetr(prec); affur(s,z); return z; }
     466             : INLINE GEN
     467   712057366 : itor(GEN x, long prec) { GEN z = cgetr(prec); affir(x,z); return z; }
     468             : INLINE GEN
     469   300125302 : rtor(GEN x, long prec) { GEN z = cgetr(prec); affrr(x,z); return z; }
     470             : 
     471             : INLINE ulong
     472    22272281 : int_bit(GEN x, long n)
     473             : {
     474    22272281 :   long r, q = dvmdsBIL(n, &r);
     475    22254144 :   return q < lgefint(x)-2?((ulong)*int_W(x,q) >> r) & 1UL:0;
     476             : }
     477             : 
     478             : /*******************************************************************/
     479             : /*                                                                 */
     480             : /*                           COMPARISON                            */
     481             : /*                                                                 */
     482             : /*******************************************************************/
     483             : INLINE int
     484     1432120 : cmpss(long a, long b)
     485     1432120 : { return a>b? 1: (a<b? -1: 0); }
     486             : 
     487             : INLINE int
     488  1440484040 : cmpuu(ulong a, ulong b)
     489  1440484040 : { return a>b? 1: (a<b? -1: 0); }
     490             : 
     491             : INLINE int
     492     9427601 : cmpir(GEN x, GEN y)
     493             : {
     494             :   pari_sp av;
     495             :   GEN z;
     496             : 
     497     9427601 :   if (!signe(x)) return -signe(y);
     498      414907 :   if (!signe(y))
     499             :   {
     500        6161 :     if (expo(y) >= expi(x)) return 0;
     501        6133 :     return signe(x);
     502             :   }
     503      408746 :   av=avma; z = itor(x, realprec(y)); set_avma(av);
     504      408747 :   return cmprr(z,y); /* cmprr does no memory adjustment */
     505             : }
     506             : INLINE int
     507      282261 : cmpri(GEN x, GEN y) { return -cmpir(y,x); }
     508             : INLINE int
     509      824659 : cmpsr(long x, GEN y)
     510             : {
     511             :   pari_sp av;
     512             :   GEN z;
     513             : 
     514      824659 :   if (!x) return -signe(y);
     515      824659 :   av=avma; z = stor(x, LOWDEFAULTPREC); set_avma(av);
     516      824664 :   return cmprr(z,y);
     517             : }
     518             : INLINE int
     519       40996 : cmprs(GEN x, long y) { return -cmpsr(y,x); }
     520             : /* compare x and y */
     521             : INLINE int
     522     9658653 : cmpui(ulong x, GEN y)
     523             : {
     524             :   ulong p;
     525     9658653 :   if (!x) return -signe(y);
     526     9658653 :   if (signe(y) <= 0) return 1;
     527     9530483 :   if (lgefint(y) > 3) return -1;
     528     9277799 :   p = y[2]; if (p == x) return 0;
     529     9183266 :   return p < x ? 1 : -1;
     530             : }
     531             : INLINE int
     532     9658828 : cmpiu(GEN x, ulong y) { return -cmpui(y,x); }
     533             : /* compare x and |y| */
     534             : INLINE int
     535    31067956 : abscmpui(ulong x, GEN y)
     536             : {
     537    31067956 :   long l = lgefint(y);
     538             :   ulong p;
     539             : 
     540    31067956 :   if (!x) return (l > 2)? -1: 0;
     541    31067942 :   if (l == 2) return 1;
     542    30879527 :   if (l > 3) return -1;
     543    30858084 :   p = y[2]; if (p == x) return 0;
     544    30226952 :   return p < x ? 1 : -1;
     545             : }
     546             : INLINE int
     547    31067907 : abscmpiu(GEN x, ulong y) { return -abscmpui(y,x); }
     548             : INLINE int
     549     3865403 : cmpsi(long x, GEN y)
     550             : {
     551             :   ulong p;
     552             : 
     553     3865403 :   if (!x) return -signe(y);
     554             : 
     555     3861218 :   if (x > 0)
     556             :   {
     557     3860182 :     if (signe(y)<=0) return 1;
     558     3849283 :     if (lgefint(y)>3) return -1;
     559     3843423 :     p = y[2]; if (p == (ulong)x) return 0;
     560     3773545 :     return p < (ulong)x ? 1 : -1;
     561             :   }
     562             : 
     563        1036 :   if (signe(y)>=0) return -1;
     564         119 :   if (lgefint(y)>3) return 1;
     565         119 :   p = y[2]; if (p == (ulong)-x) return 0;
     566          14 :   return p < (ulong)(-x) ? -1 : 1;
     567             : }
     568             : INLINE int
     569     3633451 : cmpis(GEN x, long y) { return -cmpsi(y,x); }
     570             : INLINE int
     571     2227480 : mpcmp(GEN x, GEN y)
     572             : {
     573     2227480 :   if (typ(x)==t_INT)
     574       70327 :     return (typ(y)==t_INT) ? cmpii(x,y) : cmpir(x,y);
     575     2157153 :   return (typ(y)==t_INT) ? -cmpir(y,x) : cmprr(x,y);
     576             : }
     577             : 
     578             : /* x == y ? */
     579             : INLINE int
     580     2994998 : equalui(ulong x, GEN y)
     581             : {
     582     2994998 :   if (!x) return !signe(y);
     583     2994305 :   if (signe(y) <= 0 || lgefint(y) != 3) return 0;
     584     2982597 :   return ((ulong)y[2] == (ulong)x);
     585             : }
     586             : /* x == y ? */
     587             : INLINE int
     588     1174626 : equalsi(long x, GEN y)
     589             : {
     590     1174626 :   if (!x) return !signe(y);
     591     1174626 :   if (x > 0)
     592             :   {
     593     1172415 :     if (signe(y) <= 0 || lgefint(y) != 3) return 0;
     594     1095730 :     return ((ulong)y[2] == (ulong)x);
     595             :   }
     596        2212 :   if (signe(y) >= 0 || lgefint(y) != 3) return 0;
     597        2092 :   return ((ulong)y[2] == (ulong)-x);
     598             : }
     599             : /* x == |y| ? */
     600             : INLINE int
     601    41350662 : absequalui(ulong x, GEN y)
     602             : {
     603    41350662 :   if (!x) return !signe(y);
     604    41350662 :   return (lgefint(y) == 3 && (ulong)y[2] == x);
     605             : }
     606             : INLINE int
     607    39601758 : absequaliu(GEN x, ulong y) { return absequalui(y,x); }
     608             : INLINE int
     609     1174444 : equalis(GEN x, long y) { return equalsi(y,x); }
     610             : INLINE int
     611     2994998 : equaliu(GEN x, ulong y) { return equalui(y,x); }
     612             : 
     613             : /* assume x != 0, is |x| == 2^n ? */
     614             : INLINE int
     615     1288941 : absrnz_equal2n(GEN x) {
     616     1288941 :   if ((ulong)x[2]==HIGHBIT)
     617             :   {
     618       45063 :     long i, lx = lg(x);
     619      146708 :     for (i = 3; i < lx; i++)
     620      110763 :       if (x[i]) return 0;
     621       35945 :     return 1;
     622             :   }
     623     1243878 :   return 0;
     624             : }
     625             : /* assume x != 0, is |x| == 1 ? */
     626             : INLINE int
     627     4515572 : absrnz_equal1(GEN x) { return !expo(x) && absrnz_equal2n(x); }
     628             : 
     629             : INLINE long
     630  9578041319 : maxss(long x, long y) { return x>y?x:y; }
     631             : INLINE long
     632  2009102430 : minss(long x, long y) { return x<y?x:y; }
     633             : INLINE long
     634    68058520 : minuu(ulong x, ulong y) { return x<y?x:y; }
     635             : INLINE long
     636     4805025 : maxuu(ulong x, ulong y) { return x>y?x:y; }
     637             : INLINE double
     638     3162816 : maxdd(double x, double y) { return x>y?x:y; }
     639             : INLINE double
     640      268637 : mindd(double x, double y) { return x<y?x:y; }
     641             : 
     642             : /*******************************************************************/
     643             : /*                                                                 */
     644             : /*                             ADD / SUB                           */
     645             : /*                                                                 */
     646             : /*******************************************************************/
     647             : INLINE GEN
     648       25067 : subuu(ulong x, ulong y)
     649             : {
     650             :   ulong z;
     651             :   LOCAL_OVERFLOW;
     652       25067 :   z = subll(x, y);
     653       25067 :   return overflow? utoineg(-z): utoi(z);
     654             : }
     655             : INLINE GEN
     656  3406504662 : adduu(ulong x, ulong y) { ulong t = x+y; return uutoi((t < x), t); }
     657             : 
     658             : INLINE GEN
     659       25067 : addss(long x, long y)
     660             : {
     661       25067 :   if (!x) return stoi(y);
     662       25067 :   if (!y) return stoi(x);
     663       25067 :   if (x > 0) return y > 0? adduu(x,y): subuu(x, -y);
     664             : 
     665       25067 :   if (y > 0) return subuu(y, -x);
     666             :   else { /* - adduu(-x, -y) */
     667           0 :     ulong t = (-x)+(-y); return uutoineg((t < (ulong)(-x)), t);
     668             :   }
     669             : }
     670       25067 : INLINE GEN subss(long x, long y) { return addss(-y,x); }
     671             : 
     672             : INLINE GEN
     673  7636589633 : subii(GEN x, GEN y)
     674             : {
     675  7636589633 :   if (x==y) return gen_0; /* frequent with x = y = gen_0 */
     676  6300300905 :   return addii_sign(x, signe(x), y, -signe(y));
     677             : }
     678             : INLINE GEN
     679 12363152815 : addii(GEN x, GEN y) { return addii_sign(x, signe(x), y, signe(y)); }
     680             : INLINE GEN
     681  2865034757 : addrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, signe(y)); }
     682             : INLINE GEN
     683   995375929 : subrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, -signe(y)); }
     684             : INLINE GEN
     685   477163061 : addir(GEN x, GEN y) { return addir_sign(x, signe(x), y, signe(y)); }
     686             : INLINE GEN
     687     3006041 : subir(GEN x, GEN y) { return addir_sign(x, signe(x), y, -signe(y)); }
     688             : INLINE GEN
     689     6132738 : subri(GEN x, GEN y) { return addir_sign(y, -signe(y), x, signe(x)); }
     690             : INLINE GEN
     691   306470811 : addsi(long x, GEN y) { return addsi_sign(x, y, signe(y)); }
     692             : INLINE GEN
     693    98170357 : addui(ulong x, GEN y) { return addui_sign(x, y, signe(y)); }
     694             : INLINE GEN
     695     6104904 : subsi(long x, GEN y) { return addsi_sign(x, y, -signe(y)); }
     696             : INLINE GEN
     697   134394807 : subui(ulong x, GEN y) { return addui_sign(x, y, -signe(y)); }
     698             : 
     699             : /*******************************************************************/
     700             : /*                                                                 */
     701             : /*                           MOD, REM, DIV                         */
     702             : /*                                                                 */
     703             : /*******************************************************************/
     704   102078135 : INLINE ulong mod2BIL(GEN x) { return *int_LSW(x); }
     705           0 : INLINE long mod64(GEN x) { return mod2BIL(x) & 63; }
     706         259 : INLINE long mod32(GEN x) { return mod2BIL(x) & 31; }
     707      236579 : INLINE long mod16(GEN x) { return mod2BIL(x) & 15; }
     708    13198796 : INLINE long mod8(GEN x)  { return mod2BIL(x) & 7; }
     709     4676528 : INLINE long mod4(GEN x)  { return mod2BIL(x) & 3; }
     710    61024839 : INLINE long mod2(GEN x)  { return mod2BIL(x) & 1; }
     711             : INLINE int
     712   114961380 : mpodd(GEN x) { return signe(x) && mod2(x); }
     713             : /* x mod 2^n, n < BITS_IN_LONG */
     714             : INLINE ulong
     715    48915942 : umodi2n(GEN x, long n)
     716             : {
     717    48915942 :   long s = signe(x);
     718    48915942 :   const ulong _2n = 1UL << n;
     719             :   ulong m;
     720    48915942 :   if (!s) return 0;
     721    47602038 :   m = *int_LSW(x) & (_2n - 1);
     722    47602038 :   if (s < 0 && m) m = _2n - m;
     723    47602038 :   return m;
     724             : }
     725           0 : INLINE ulong Mod64(GEN x){ return umodi2n(x,6); }
     726      199255 : INLINE ulong Mod32(GEN x){ return umodi2n(x,5); }
     727      277481 : INLINE ulong Mod16(GEN x){ return umodi2n(x,4); }
     728     2070299 : INLINE ulong Mod8(GEN x) { return umodi2n(x,3); }
     729    44344316 : INLINE ulong Mod4(GEN x) { return umodi2n(x,2); }
     730     2024379 : INLINE ulong Mod2(GEN x) { return umodi2n(x,1); }
     731             : 
     732             : INLINE GEN
     733    46772945 : truedivii(GEN a,GEN b) { return truedvmdii(a,b,NULL); }
     734             : INLINE GEN
     735      266809 : truedivis(GEN a, long b) { return truedvmdis(a,b,NULL); }
     736             : INLINE GEN
     737     6202031 : truedivsi(long a, GEN b) { return truedvmdsi(a,b,NULL); }
     738             : 
     739             : INLINE GEN
     740    14061239 : divii(GEN a, GEN b) { return dvmdii(a,b,NULL); }
     741             : INLINE GEN
     742  3008031801 : remii(GEN a, GEN b) { return dvmdii(a,b,ONLY_REM); }
     743             : 
     744             : INLINE GEN
     745           0 : divss(long x, long y) { return stoi(x / y); }
     746             : INLINE GEN
     747           0 : modss(long x, long y) { return utoi(smodss(x, y)); }
     748             : INLINE GEN
     749           0 : remss(long x, long y) { return stoi(x % y); }
     750             : INLINE long
     751    12475146 : smodss(long x, long y)
     752             : {
     753    12475146 :   long r = x%y;
     754    12475146 :   return (r >= 0)? r: labs(y) + r;
     755             : }
     756             : INLINE ulong
     757   722979001 : umodsu(long x, ulong y)
     758             : {
     759   722979001 :   return x>=0 ? x%y: Fl_neg((-x)%y, y);
     760             : }
     761             : 
     762             : INLINE long
     763           0 : sdivss_rem(long x, long y, long *r)
     764             : {
     765             :   long q;
     766             :   LOCAL_HIREMAINDER;
     767           0 :   if (!y) pari_err_INV("sdivss_rem",gen_0);
     768           0 :   hiremainder = 0; q = divll((ulong)labs(x),(ulong)labs(y));
     769           0 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     770           0 :   if (y < 0) q = -q;
     771           0 :   *r = hiremainder; return q;
     772             : }
     773             : INLINE GEN
     774           0 : divss_rem(long x, long y, long *r) { return stoi(sdivss_rem(x,y,r)); }
     775             : INLINE ulong
     776   159298367 : udivuu_rem(ulong x, ulong y, ulong *r)
     777             : {
     778   159298367 :   if (!y) pari_err_INV("udivuu_rem",gen_0);
     779   159298367 :   *r = x % y; return x / y;
     780             : }
     781             : INLINE ulong
     782     3846386 : ceildivuu(ulong a, ulong b)
     783             : {
     784             :   ulong c;
     785     3846386 :   if (!a) return 0;
     786     3666483 :   c = a / b; return (a % b)? c+1: c;
     787             : }
     788             : 
     789             : INLINE ulong
     790       19089 : uabsdivui_rem(ulong x, GEN y, ulong *r)
     791             : {
     792       19089 :   long q, s = signe(y);
     793             :   LOCAL_HIREMAINDER;
     794             : 
     795       19089 :   if (!s) pari_err_INV("uabsdivui_rem",gen_0);
     796       19089 :   if (!x || lgefint(y)>3) { *r = x; return 0; }
     797       18410 :   hiremainder=0; q = (long)divll(x, (ulong)y[2]);
     798       18410 :   if (s < 0) q = -q;
     799       18410 :   *r = hiremainder; return q;
     800             : }
     801             : 
     802             : /* assume d != 0 and |n| / d can be represented as an ulong.
     803             :  * Return |n|/d, set *r = |n| % d */
     804             : INLINE ulong
     805    11892741 : uabsdiviu_rem(GEN n, ulong d, ulong *r)
     806             : {
     807    11892741 :   switch(lgefint(n))
     808             :   {
     809           0 :     case 2: *r = 0; return 0;
     810    11892741 :     case 3:
     811             :     {
     812    11892741 :       ulong nn = n[2];
     813    11892741 :       *r = nn % d; return nn / d;
     814             :     }
     815           0 :     default: /* 4 */
     816             :     {
     817             :       ulong n1, n0, q;
     818             :       LOCAL_HIREMAINDER;
     819           0 :       n0 = *int_W(n,0);
     820           0 :       n1 = *int_W(n,1);
     821           0 :       hiremainder = n1;
     822           0 :       q = divll(n0, d);
     823           0 :       *r = hiremainder; return q;
     824             :     }
     825             :   }
     826             : }
     827             : 
     828             : INLINE long
     829    51427549 : sdivsi_rem(long x, GEN y, long *r)
     830             : {
     831    51427549 :   long q, s = signe(y);
     832             :   LOCAL_HIREMAINDER;
     833             : 
     834    51427549 :   if (!s) pari_err_INV("sdivsi_rem",gen_0);
     835    51427550 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) { *r = x; return 0; }
     836    49470514 :   hiremainder=0; q = (long)divll(labs(x), (ulong)y[2]);
     837    49470514 :   if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
     838    49470514 :   if (s < 0) q = -q;
     839    49470514 :   *r = hiremainder; return q;
     840             : }
     841             : INLINE GEN
     842           0 : divsi_rem(long s, GEN y, long *r) { return stoi(sdivsi_rem(s,y,r)); }
     843             : 
     844             : INLINE long
     845      102147 : sdivsi(long x, GEN y)
     846             : {
     847      102147 :   long q, s = signe(y);
     848             : 
     849      102147 :   if (!s) pari_err_INV("sdivsi",gen_0);
     850      102147 :   if (!x || lgefint(y)>3 || ((long)y[2]) < 0) return 0;
     851      102042 :   q = labs(x) / y[2];
     852      102042 :   if (x < 0) q = -q;
     853      102042 :   if (s < 0) q = -q;
     854      102042 :   return q;
     855             : }
     856             : 
     857             : INLINE GEN
     858           0 : dvmdss(long x, long y, GEN *z)
     859             : {
     860             :   long r;
     861           0 :   GEN q = divss_rem(x,y, &r);
     862           0 :   *z = stoi(r); return q;
     863             : }
     864             : INLINE long
     865  7058843797 : dvmdsBIL(long n, long *r) { *r = remsBIL(n); return divsBIL(n); }
     866             : INLINE ulong
     867   163000075 : dvmduBIL(ulong n, ulong *r) { *r = remsBIL(n); return divsBIL(n); }
     868             : INLINE GEN
     869           0 : dvmdsi(long x, GEN y, GEN *z)
     870             : {
     871             :   long r;
     872           0 :   GEN q = divsi_rem(x,y, &r);
     873           0 :   *z = stoi(r); return q;
     874             : }
     875             : INLINE GEN
     876           0 : dvmdis(GEN x, long y, GEN *z)
     877             : {
     878             :   long r;
     879           0 :   GEN q = divis_rem(x,y, &r);
     880           0 :   *z = stoi(r); return q;
     881             : }
     882             : 
     883             : INLINE long
     884    21140226 : smodis(GEN x, long y)
     885             : {
     886    21140226 :   pari_sp av = avma;
     887    21140226 :   long r; (void)divis_rem(x,y, &r);
     888    21140226 :   return gc_long(av, (r >= 0)? r: labs(y) + r);
     889             : }
     890             : INLINE GEN
     891    19602559 : modis(GEN x, long y) { return stoi(smodis(x,y)); }
     892             : INLINE GEN
     893    45223222 : modsi(long x, GEN y) {
     894    45223222 :   long r; (void)sdivsi_rem(x, y, &r);
     895    45223222 :   return (r >= 0)? stoi(r): addsi_sign(r, y, 1);
     896             : }
     897             : 
     898             : INLINE ulong
     899     1296416 : umodui(ulong x, GEN y)
     900             : {
     901     1296416 :   if (!signe(y)) pari_err_INV("umodui",gen_0);
     902     1296416 :   if (!x || lgefint(y) > 3) return x;
     903      415304 :   return x % (ulong)y[2];
     904             : }
     905             : 
     906             : INLINE ulong
     907     9986356 : ugcdiu(GEN x, ulong y) { return ugcd(umodiu(x,y), y); }
     908             : INLINE ulong
     909        2737 : ugcdui(ulong y, GEN x) { return ugcd(umodiu(x,y), y); }
     910             : 
     911             : INLINE GEN
     912           0 : remsi(long x, GEN y)
     913           0 : { long r; (void)sdivsi_rem(x,y, &r); return stoi(r); }
     914             : INLINE GEN
     915           0 : remis(GEN x, long y)
     916             : {
     917           0 :   pari_sp av = avma;
     918             :   long r;
     919           0 :   (void)divis_rem(x,y, &r); return gc_stoi(av, r);
     920             : }
     921             : 
     922             : INLINE GEN
     923           0 : rdivis(GEN x, long y, long prec)
     924             : {
     925           0 :   GEN z = cgetr(prec);
     926           0 :   pari_sp av = avma;
     927           0 :   affrr(divrs(itor(x,prec), y),z);
     928           0 :   set_avma(av); return z;
     929             : }
     930             : INLINE GEN
     931           0 : rdivsi(long x, GEN y, long prec)
     932             : {
     933           0 :   GEN z = cgetr(prec);
     934           0 :   pari_sp av = avma;
     935           0 :   affrr(divsr(x, itor(y,prec)), z);
     936           0 :   set_avma(av); return z;
     937             : }
     938             : INLINE GEN
     939      839647 : rdivss(long x, long y, long prec)
     940             : {
     941      839647 :   GEN z = cgetr(prec);
     942      839647 :   pari_sp av = avma;
     943      839647 :   affrr(divrs(stor(x, prec), y), z);
     944      839647 :   set_avma(av); return z;
     945             : }
     946             : 
     947             : INLINE void
     948    13141071 : rdiviiz(GEN x, GEN y, GEN z)
     949             : {
     950    13141071 :   long lz = lg(z), lx = lgefint(x), ly = lgefint(y);
     951    13141071 :   if (lx == 2) { affur(0, z); return; }
     952    13141071 :   if (ly == 3)
     953             :   {
     954     2243010 :     affir(x, z); if (signe(y) < 0) togglesign(z);
     955     2243003 :     affrr(divru(z, y[2]), z);
     956             :   }
     957    10898061 :   else if (lx > lz + 1 || ly > lz + 1)
     958             :   {
     959     5948508 :     affir(x,z); affrr(divri(z, y), z);
     960             :   }
     961             :   else
     962             :   {
     963     4949553 :     long b = lg2prec(lz) + expi(y) - expi(x) + 1;
     964     4949890 :     GEN q = divii(b > 0? shifti(x, b): x, y);
     965     4950753 :     affir(q, z); if (b > 0) shiftr_inplace(z, -b);
     966             :   }
     967    13150114 :   set_avma((ulong)z);
     968             : }
     969             : INLINE GEN
     970    13096911 : rdivii(GEN x, GEN y, long prec)
     971    13096911 : { GEN z = cgetr(prec); rdiviiz(x, y, z); return z; }
     972             : INLINE GEN
     973     7375271 : fractor(GEN x, long prec)
     974     7375271 : { return rdivii(gel(x,1), gel(x,2), prec); }
     975             : 
     976             : INLINE int
     977    16482825 : dvdii(GEN x, GEN y)
     978             : {
     979    16482825 :   pari_sp av = avma;
     980             :   GEN r;
     981    16482825 :   if (!signe(x)) return 1;
     982    15069480 :   if (!signe(y)) return 0;
     983    15069480 :   r = remii(x,y);
     984    15078743 :   return gc_bool(av, r == gen_0);
     985             : }
     986             : INLINE int
     987         371 : dvdsi(long x, GEN y)
     988             : {
     989         371 :   if (x == 0) return 1;
     990         266 :   if (!signe(y)) return 0;
     991         266 :   if (lgefint(y) != 3) return 0;
     992         259 :   return x % y[2] == 0;
     993             : }
     994             : INLINE int
     995      167195 : dvdui(ulong x, GEN y)
     996             : {
     997      167195 :   if (x == 0) return 1;
     998      167195 :   if (!signe(y)) return 0;
     999      167195 :   if (lgefint(y) != 3) return 0;
    1000      156574 :   return x % y[2] == 0;
    1001             : }
    1002             : INLINE int
    1003       33912 : dvdis(GEN x, long y)
    1004       33912 : { return y? smodis(x, y) == 0: signe(x) == 0; }
    1005             : INLINE int
    1006      576566 : dvdiu(GEN x, ulong y)
    1007      576566 : { return y? umodiu(x, y) == 0: signe(x) == 0; }
    1008             : 
    1009             : INLINE int
    1010           0 : dvdisz(GEN x, long y, GEN z)
    1011             : {
    1012           0 :   const pari_sp av = avma;
    1013             :   long r;
    1014           0 :   GEN p1 = divis_rem(x,y, &r);
    1015           0 :   set_avma(av); if (r) return 0;
    1016           0 :   affii(p1,z); return 1;
    1017             : }
    1018             : INLINE int
    1019           0 : dvdiuz(GEN x, ulong y, GEN z)
    1020             : {
    1021           0 :   const pari_sp av = avma;
    1022             :   ulong r;
    1023           0 :   GEN p1 = absdiviu_rem(x,y, &r);
    1024           0 :   set_avma(av); if (r) return 0;
    1025           0 :   affii(p1,z); return 1;
    1026             : }
    1027             : INLINE int
    1028           0 : dvdiiz(GEN x, GEN y, GEN z)
    1029             : {
    1030           0 :   const pari_sp av=avma;
    1031           0 :   GEN p2, p1 = dvmdii(x,y,&p2);
    1032           0 :   if (signe(p2)) return gc_bool(av,0);
    1033           0 :   affii(p1,z); return gc_bool(av,1);
    1034             : }
    1035             : 
    1036             : INLINE ulong
    1037    75067748 : remlll_pre(ulong u2, ulong u1, ulong u0, ulong n, ulong ninv)
    1038             : {
    1039    75067748 :   u1 = remll_pre(u2, u1, n, ninv);
    1040    75884347 :   return remll_pre(u1, u0, n, ninv);
    1041             : }
    1042             : 
    1043             : INLINE ulong
    1044  2161338515 : Fl_sqr_pre(ulong a, ulong p, ulong pi)
    1045             : {
    1046             :   ulong x;
    1047             :   LOCAL_HIREMAINDER;
    1048  2161338515 :   x = mulll(a,a);
    1049  2161338515 :   return remll_pre(hiremainder, x, p, pi);
    1050             : }
    1051             : 
    1052             : INLINE ulong
    1053  4023350939 : Fl_mul_pre(ulong a, ulong b, ulong p, ulong pi)
    1054             : {
    1055             :   ulong x;
    1056             :   LOCAL_HIREMAINDER;
    1057  4023350939 :   x = mulll(a,b);
    1058  4023350939 :   return remll_pre(hiremainder, x, p, pi);
    1059             : }
    1060             : 
    1061             : INLINE ulong
    1062  7600893223 : Fl_addmul_pre(ulong y0, ulong x0, ulong x1, ulong p, ulong pi)
    1063             : {
    1064             :   ulong l0, h0;
    1065             :   LOCAL_HIREMAINDER;
    1066  7600893223 :   hiremainder = y0;
    1067  7600893223 :   l0 = addmul(x0, x1); h0 = hiremainder;
    1068  7600893223 :   return remll_pre(h0, l0, p, pi);
    1069             : }
    1070             : 
    1071             : INLINE ulong
    1072    59631841 : Fl_addmulmul_pre(ulong x0, ulong y0, ulong x1, ulong y1, ulong p, ulong pi)
    1073             : {
    1074             :   ulong l0, l1, h0, h1;
    1075             :   LOCAL_OVERFLOW;
    1076             :   LOCAL_HIREMAINDER;
    1077    59631841 :   l0 = mulll(x0, y0); h0 = hiremainder;
    1078    59631841 :   l1 = mulll(x1, y1); h1 = hiremainder;
    1079    59631841 :   l0 = addll(l0, l1); h0 = addllx(h0, h1);
    1080    59631841 :   return overflow ? remlll_pre(1, h0, l0, p, pi): remll_pre(h0, l0, p, pi);
    1081             : }
    1082             : 
    1083             : INLINE ulong
    1084      242809 : Fl_ellj_pre(ulong a4, ulong a6, ulong p, ulong pi)
    1085             : {
    1086             :   /* a43 = 4 a4^3 */
    1087      242809 :   ulong a43 = Fl_double(Fl_double(
    1088             :               Fl_mul_pre(a4, Fl_sqr_pre(a4, p, pi), p, pi), p), p);
    1089             :   /* a62 = 27 a6^2 */
    1090      242812 :   ulong a62 = Fl_mul_pre(Fl_sqr_pre(a6, p, pi), 27 % p, p, pi);
    1091      242812 :   ulong z1 = Fl_mul_pre(a43, 1728 % p, p, pi);
    1092      242812 :   ulong z2 = Fl_add(a43, a62, p);
    1093      242812 :   return Fl_div(z1, z2, p);
    1094             : }
    1095             : 
    1096             : /*******************************************************************/
    1097             : /*                                                                 */
    1098             : /*                        MP (INT OR REAL)                         */
    1099             : /*                                                                 */
    1100             : /*******************************************************************/
    1101             : INLINE GEN
    1102          49 : mptrunc(GEN x) { return typ(x)==t_INT? icopy(x): truncr(x); }
    1103             : INLINE GEN
    1104           0 : mpfloor(GEN x) { return typ(x)==t_INT? icopy(x): floorr(x); }
    1105             : INLINE GEN
    1106           0 : mpceil(GEN x) { return typ(x)==t_INT? icopy(x): ceilr(x); }
    1107             : INLINE GEN
    1108     1216540 : mpround(GEN x) { return typ(x) == t_INT? icopy(x): roundr(x); }
    1109             : 
    1110             : INLINE long
    1111    38846926 : mpexpo(GEN x) { return typ(x) == t_INT? expi(x): expo(x); }
    1112             : 
    1113             : INLINE GEN
    1114   574233143 : mpadd(GEN x, GEN y)
    1115             : {
    1116   574233143 :   if (typ(x)==t_INT)
    1117    16599744 :     return (typ(y)==t_INT) ? addii(x,y) : addir(x,y);
    1118   557633399 :   return (typ(y)==t_INT) ? addir(y,x) : addrr(x,y);
    1119             : }
    1120             : INLINE GEN
    1121   251495771 : mpsub(GEN x, GEN y)
    1122             : {
    1123   251495771 :   if (typ(x)==t_INT)
    1124      494831 :     return (typ(y)==t_INT) ? subii(x,y) : subir(x,y);
    1125   251000940 :   return (typ(y)==t_INT) ? subri(x,y) : subrr(x,y);
    1126             : }
    1127             : INLINE GEN
    1128   837285105 : mpmul(GEN x, GEN y)
    1129             : {
    1130   837285105 :   if (typ(x)==t_INT)
    1131    37884635 :     return (typ(y)==t_INT) ? mulii(x,y) : mulir(x,y);
    1132   799400470 :   return (typ(y)==t_INT) ? mulir(y,x) : mulrr(x,y);
    1133             : }
    1134             : INLINE GEN
    1135    90947942 : mpsqr(GEN x) { return (typ(x)==t_INT) ? sqri(x) : sqrr(x); }
    1136             : INLINE GEN
    1137      667694 : mpdiv(GEN x, GEN y)
    1138             : {
    1139      667694 :   if (typ(x)==t_INT)
    1140      262736 :     return (typ(y)==t_INT) ? divii(x,y) : divir(x,y);
    1141      404958 :   return (typ(y)==t_INT) ? divri(x,y) : divrr(x,y);
    1142             : }
    1143             : 
    1144             : /*******************************************************************/
    1145             : /*                                                                 */
    1146             : /*                          Z/nZ, n ULONG                          */
    1147             : /*                                                                 */
    1148             : /*******************************************************************/
    1149             : INLINE ulong
    1150   475826329 : Fl_double(ulong a, ulong p)
    1151             : {
    1152   475826329 :   ulong res = a << 1;
    1153   475826329 :   return (res >= p || res < a) ? res - p : res;
    1154             : }
    1155             : INLINE ulong
    1156    96385040 : Fl_triple(ulong a, ulong p)
    1157             : {
    1158    96385040 :   ulong res = a << 1;
    1159    96385040 :   if (res >= p || res < a) res -= p;
    1160    96385040 :   res += a;
    1161    96385040 :   return (res >= p || res < a)? res - p: res;
    1162             : }
    1163             : INLINE ulong
    1164    18065207 : Fl_halve(ulong a, ulong p)
    1165             : {
    1166             :   ulong ap, ap2;
    1167    18065207 :   if ((a&1UL)==0) return a>>1;
    1168     9095635 :   ap = a + p; ap2 = ap>>1;
    1169     9095635 :   return ap>=a ? ap2: (ap2|HIGHBIT);
    1170             : }
    1171             : 
    1172             : INLINE ulong
    1173  4348458015 : Fl_add(ulong a, ulong b, ulong p)
    1174             : {
    1175  4348458015 :   ulong res = a + b;
    1176  4348458015 :   return (res >= p || res < a) ? res - p : res;
    1177             : }
    1178             : INLINE ulong
    1179   711003427 : Fl_neg(ulong x, ulong p) { return x ? p - x: 0; }
    1180             : 
    1181             : INLINE ulong
    1182  7261198690 : Fl_sub(ulong a, ulong b, ulong p)
    1183             : {
    1184  7261198690 :   ulong res = a - b;
    1185  7261198690 :   return (res > a) ? res + p: res;
    1186             : }
    1187             : 
    1188             : /* centerlift(u mod p) */
    1189             : INLINE long
    1190     4030158 : Fl_center(ulong u, ulong p, ulong ps2) { return (long) (u > ps2)? u - p: u; }
    1191             : 
    1192             : INLINE ulong
    1193  2381328543 : Fl_mul(ulong a, ulong b, ulong p)
    1194             : {
    1195             :   ulong x;
    1196             :   LOCAL_HIREMAINDER;
    1197  2381328543 :   x = mulll(a,b);
    1198  2381328543 :   if (!hiremainder) return x % p;
    1199   403062201 :   (void)divll(x,p); return hiremainder;
    1200             : }
    1201             : INLINE ulong
    1202    94842215 : Fl_sqr(ulong a, ulong p)
    1203             : {
    1204             :   ulong x;
    1205             :   LOCAL_HIREMAINDER;
    1206    94842215 :   x = mulll(a,a);
    1207    94842215 :   if (!hiremainder) return x % p;
    1208    25963631 :   (void)divll(x,p); return hiremainder;
    1209             : }
    1210             : /* don't assume that p is prime: can't special case a = 0 */
    1211             : INLINE ulong
    1212    46529119 : Fl_div(ulong a, ulong b, ulong p)
    1213    46529119 : { return Fl_mul(a, Fl_inv(b, p), p); }
    1214             : 
    1215             : /*******************************************************************/
    1216             : /*                                                                 */
    1217             : /*        DEFINED FROM EXISTING ONE EXPLOITING COMMUTATIVITY       */
    1218             : /*                                                                 */
    1219             : /*******************************************************************/
    1220             : INLINE GEN
    1221     1109361 : addri(GEN x, GEN y) { return addir(y,x); }
    1222             : INLINE GEN
    1223   181288629 : addis(GEN x, long s) { return addsi(s,x); }
    1224             : INLINE GEN
    1225    94666128 : addiu(GEN x, ulong s) { return addui(s,x); }
    1226             : INLINE GEN
    1227    12354106 : addrs(GEN x, long s) { return addsr(s,x); }
    1228             : 
    1229             : INLINE GEN
    1230   130156011 : subiu(GEN x, long y) { GEN z = subui(y, x); togglesign(z); return z; }
    1231             : INLINE GEN
    1232      170993 : subis(GEN x, long y) { return addsi(-y,x); }
    1233             : INLINE GEN
    1234    16512747 : subrs(GEN x, long y) { return addsr(-y,x); }
    1235             : 
    1236             : INLINE GEN
    1237   463890561 : mulis(GEN x, long s) { return mulsi(s,x); }
    1238             : INLINE GEN
    1239   371953270 : muliu(GEN x, ulong s) { return mului(s,x); }
    1240             : INLINE GEN
    1241     2765822 : mulru(GEN x, ulong s) { return mulur(s,x); }
    1242             : INLINE GEN
    1243    37887785 : mulri(GEN x, GEN s) { return mulir(s,x); }
    1244             : INLINE GEN
    1245     7181596 : mulrs(GEN x, long s) { return mulsr(s,x); }
    1246             : 
    1247             : /*******************************************************************/
    1248             : /*                                                                 */
    1249             : /*                  VALUATION, EXPONENT, SHIFTS                    */
    1250             : /*                                                                 */
    1251             : /*******************************************************************/
    1252             : INLINE long
    1253   187747893 : vali(GEN x)
    1254             : {
    1255             :   long i;
    1256             :   GEN xp;
    1257             : 
    1258   187747893 :   if (!signe(x)) return -1;
    1259   187662960 :   xp=int_LSW(x);
    1260   196373840 :   for (i=0; !*xp; i++) xp=int_nextW(xp);
    1261   187662960 :   return vals(*xp) + i * BITS_IN_LONG;
    1262             : }
    1263             : 
    1264             : /* assume x > 0 */
    1265             : INLINE long
    1266   783524153 : expu(ulong x) { return (BITS_IN_LONG-1) - (long)bfffo(x); }
    1267             : 
    1268             : INLINE long
    1269  2452524977 : expi(GEN x)
    1270             : {
    1271  2452524977 :   const long lx=lgefint(x);
    1272  2452524977 :   return lx==2? -(long)HIGHEXPOBIT: bit_accuracy(lx)-(long)bfffo(*int_MSW(x))-1;
    1273             : }
    1274             : 
    1275             : INLINE GEN
    1276   180023362 : shiftr(GEN x, long n)
    1277             : {
    1278   180023362 :   const long e = evalexpo(expo(x)+n);
    1279   180019735 :   const GEN y = rcopy(x);
    1280             : 
    1281   180009250 :   if (e & ~EXPOBITS) pari_err_OVERFLOW("expo()");
    1282   180009636 :   y[1] = (y[1]&~EXPOBITS) | e; return y;
    1283             : }
    1284             : INLINE GEN
    1285   153178600 : mpshift(GEN x,long s) { return (typ(x)==t_INT)?shifti(x,s):shiftr(x,s); }
    1286             : 
    1287             : /* FIXME: adapt/use mpn_[lr]shift instead */
    1288             : /* z2[imin..imax] := z1[imin..imax].f shifted left sh bits
    1289             :  * (feeding f from the right). Assume sh > 0 */
    1290             : INLINE void
    1291  7731496817 : shift_left(GEN z2, GEN z1, long imin, long imax, ulong f,  ulong sh)
    1292             : {
    1293  7731496817 :   GEN sb = z1 + imin, se = z1 + imax, te = z2 + imax;
    1294  7731496817 :   ulong l, m = BITS_IN_LONG - sh, k = f >> m;
    1295 50486049891 :   while (se > sb) {
    1296 42754553074 :     l     = *se--;
    1297 42754553074 :     *te-- = (l << sh) | k;
    1298 42754553074 :     k     = l >> m;
    1299             :   }
    1300  7731496817 :   *te = (((ulong)*se) << sh) | k;
    1301  7731496817 : }
    1302             : /* z2[imin..imax] := f.z1[imin..imax-1] shifted right sh bits
    1303             :  * (feeding f from the left). Assume sh > 0 */
    1304             : INLINE void
    1305  5639881242 : shift_right(GEN z2, GEN z1, long imin, long imax, ulong f, ulong sh)
    1306             : {
    1307  5639881242 :   GEN sb = z1 + imin, se = z1 + imax, tb = z2 + imin;
    1308  5639881242 :   ulong k, l = *sb++, m = BITS_IN_LONG - sh;
    1309  5639881242 :   *tb++ = (l >> sh) | (f << m);
    1310 30068923815 :   while (sb < se) {
    1311 24429042573 :     k     = l << m;
    1312 24429042573 :     l     = *sb++;
    1313 24429042573 :     *tb++ = (l >> sh) | k;
    1314             :   }
    1315  5639881242 : }
    1316             : 
    1317             : /* Backward compatibility. Inefficient && unused */
    1318             : extern ulong hiremainder;
    1319             : INLINE ulong
    1320           0 : shiftl(ulong x, ulong y)
    1321           0 : { hiremainder = x>>(BITS_IN_LONG-y); return (x<<y); }
    1322             : 
    1323             : INLINE ulong
    1324           0 : shiftlr(ulong x, ulong y)
    1325           0 : { hiremainder = x<<(BITS_IN_LONG-y); return (x>>y); }
    1326             : 
    1327             : INLINE void
    1328   477695587 : shiftr_inplace(GEN z, long d)
    1329             : {
    1330   477695587 :   setexpo(z, expo(z)+d);
    1331   477490500 : }
    1332             : 
    1333             : /*******************************************************************/
    1334             : /*                                                                 */
    1335             : /*                           ASSIGNMENT                            */
    1336             : /*                                                                 */
    1337             : /*******************************************************************/
    1338             : INLINE void
    1339   908773948 : affii(GEN x, GEN y)
    1340             : {
    1341   908773948 :   long lx = lgefint(x);
    1342   908773948 :   if (lg(y)<lx) pari_err_OVERFLOW("t_INT-->t_INT assignment");
    1343 38035585376 :   while (--lx) y[lx] = x[lx];
    1344   908796164 : }
    1345             : INLINE void
    1346     6396417 : affsi(long s, GEN x)
    1347             : {
    1348     6396417 :   if (!s) x[1] = evalsigne(0) | evallgefint(2);
    1349             :   else
    1350             :   {
    1351     6121192 :     if (s > 0) { x[1] = evalsigne( 1) | evallgefint(3); x[2] =  s; }
    1352     2023081 :     else       { x[1] = evalsigne(-1) | evallgefint(3); x[2] = -s; }
    1353             :   }
    1354     6396417 : }
    1355             : INLINE void
    1356    45473650 : affui(ulong u, GEN x)
    1357             : {
    1358    45473650 :   if (!u) x[1] = evalsigne(0) | evallgefint(2);
    1359    45434352 :   else  { x[1] = evalsigne(1) | evallgefint(3); x[2] = u; }
    1360    45473650 : }
    1361             : 
    1362             : INLINE void
    1363   495464185 : affsr(long x, GEN y)
    1364             : {
    1365   495464185 :   long sh, i, ly = lg(y);
    1366             : 
    1367   495464185 :   if (!x)
    1368             :   {
    1369           0 :     y[1] = evalexpo(-bit_accuracy(ly));
    1370           0 :     return;
    1371             :   }
    1372   495464185 :   if (x < 0) {
    1373       13371 :     x = -x; sh = bfffo(x);
    1374       13371 :     y[1] = evalsigne(-1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1375             :   }
    1376             :   else
    1377             :   {
    1378   495450814 :     sh = bfffo(x);
    1379   495450814 :     y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1380             :   }
    1381  5166329254 :   y[2] = ((ulong)x)<<sh; for (i=3; i<ly; i++) y[i]=0;
    1382             : }
    1383             : 
    1384             : INLINE void
    1385    13443866 : affur(ulong x, GEN y)
    1386             : {
    1387    13443866 :   long sh, i, ly = lg(y);
    1388             : 
    1389    13443866 :   if (!x)
    1390             :   {
    1391     1369414 :     y[1] = evalexpo(-bit_accuracy(ly));
    1392     1369414 :     return;
    1393             :   }
    1394    12074452 :   sh = bfffo(x);
    1395    12074452 :   y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
    1396    47434542 :   y[2] = x<<sh; for (i=3; i<ly; i++) y[i] = 0;
    1397             : }
    1398             : 
    1399             : INLINE ulong
    1400        2851 : thuemorseu(ulong c)
    1401             : {
    1402             : #ifdef LONG_IS_64BIT
    1403        2442 :     c ^= c >> 32;
    1404             : #endif
    1405        2851 :     c ^= c >> 16;
    1406        2851 :     c ^= c >>  8;
    1407        2851 :     c ^= c >>  4;
    1408        2851 :     c ^= c >>  2;
    1409        2851 :     c ^= c >>  1;
    1410        2851 :     return c & 1;
    1411             : }

Generated by: LCOV version 1.16