Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - kernel/gmp - mp.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 22875-74571cde7) Lines: 650 719 90.4 %
Date: 2018-07-21 05:35:54 Functions: 54 55 98.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #line 2 "../src/kernel/gmp/mp.c"
       2             : /* Copyright (C) 2002-2003  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             : /***********************************************************************/
      16             : /**                                                                   **/
      17             : /**                               GMP KERNEL                          **/
      18             : /** BA2002Sep24                                                       **/
      19             : /***********************************************************************/
      20             : /* GMP t_INT as just like normal t_INT, just the mantissa is the other way
      21             :  * round
      22             :  *
      23             :  *   `How would you like to live in Looking-glass House, Kitty?  I
      24             :  *   wonder if they'd give you milk in there?  Perhaps Looking-glass
      25             :  *   milk isn't good to drink--But oh, Kitty! now we come to the
      26             :  *   passage.  You can just see a little PEEP of the passage in
      27             :  *   Looking-glass House, if you leave the door of our drawing-room
      28             :  *   wide open:  and it's very like our passage as far as you can see,
      29             :  *   only you know it may be quite different on beyond.  Oh, Kitty!
      30             :  *   how nice it would be if we could only get through into Looking-
      31             :  *   glass House!  I'm sure it's got, oh! such beautiful things in it!
      32             :  *
      33             :  *  Through the Looking Glass,  Lewis Carrol
      34             :  *
      35             :  *  (pityful attempt to beat GN code/comments rate)
      36             :  *  */
      37             : 
      38             : #include <gmp.h>
      39             : #include "pari.h"
      40             : #include "paripriv.h"
      41             : #include "../src/kernel/none/tune-gen.h"
      42             : 
      43             : /*We need PARI invmod renamed to invmod_pari*/
      44             : #define INVMOD_PARI
      45             : 
      46           0 : static void *pari_gmp_realloc(void *ptr, size_t old_size, size_t new_size) {
      47           0 :   (void)old_size; return (void *) pari_realloc(ptr,new_size);
      48             : }
      49             : 
      50      278364 : static void pari_gmp_free(void *ptr, size_t old_size){
      51      278364 :   (void)old_size; pari_free(ptr);
      52      278364 : }
      53             : 
      54             : static void *(*old_gmp_malloc)(size_t new_size);
      55             : static void *(*old_gmp_realloc)(void *ptr, size_t old_size, size_t new_size);
      56             : static void (*old_gmp_free)(void *ptr, size_t old_size);
      57             : 
      58             : void
      59         892 : pari_kernel_init(void)
      60             : {
      61         892 :   mp_get_memory_functions (&old_gmp_malloc, &old_gmp_realloc, &old_gmp_free);
      62         892 :   mp_set_memory_functions((void *(*)(size_t)) pari_malloc, pari_gmp_realloc, pari_gmp_free);
      63         892 : }
      64             : 
      65             : void
      66         944 : pari_kernel_close(void)
      67             : {
      68             :   void *(*new_gmp_malloc)(size_t new_size);
      69             :   void *(*new_gmp_realloc)(void *ptr, size_t old_size, size_t new_size);
      70             :   void (*new_gmp_free)(void *ptr, size_t old_size);
      71         944 :   mp_get_memory_functions (&new_gmp_malloc, &new_gmp_realloc, &new_gmp_free);
      72         944 :   if (new_gmp_malloc==pari_malloc) new_gmp_malloc = old_gmp_malloc;
      73         944 :   if (new_gmp_realloc==pari_gmp_realloc) new_gmp_realloc = old_gmp_realloc;
      74         944 :   if (new_gmp_free==pari_gmp_free) new_gmp_free = old_gmp_free;
      75         944 :   mp_set_memory_functions(new_gmp_malloc, new_gmp_realloc, new_gmp_free);
      76         944 : }
      77             : 
      78             : #define LIMBS(x)  ((mp_limb_t *)((x)+2))
      79             : #define NLIMBS(x) (lgefint(x)-2)
      80             : /*This one is for t_REALs to emphasize they are not t_INTs*/
      81             : #define RLIMBS(x)  ((mp_limb_t *)((x)+2))
      82             : #define RNLIMBS(x) (lg(x)-2)
      83             : 
      84             : INLINE void
      85     1836155 : xmpn_copy(mp_limb_t *x, mp_limb_t *y, long n)
      86             : {
      87     1836155 :   while (--n >= 0) x[n]=y[n];
      88     1836155 : }
      89             : 
      90             : INLINE void
      91   168735646 : xmpn_mirror(mp_limb_t *x, long n)
      92             : {
      93             :   long i;
      94   883539900 :   for(i=0;i<(n>>1);i++)
      95             :   {
      96   714804254 :     ulong m=x[i];
      97   714804254 :     x[i]=x[n-1-i];
      98   714804254 :     x[n-1-i]=m;
      99             :   }
     100   168735646 : }
     101             : 
     102             : INLINE void
     103   232848578 : xmpn_mirrorcopy(mp_limb_t *z, mp_limb_t *x, long n)
     104             : {
     105             :   long i;
     106  2543357582 :   for(i=0;i<n;i++)
     107  2310509004 :     z[i]=x[n-1-i];
     108   232848578 : }
     109             : 
     110             : INLINE void
     111    78234481 : xmpn_zero(mp_ptr x, mp_size_t n)
     112             : {
     113    78234481 :   while (--n >= 0) x[n]=0;
     114    78234481 : }
     115             : 
     116             : INLINE GEN
     117    15953120 : icopy_ef(GEN x, long l)
     118             : {
     119    15953120 :   register long lx = lgefint(x);
     120    15953120 :   const GEN y = cgeti(l);
     121             : 
     122    15953088 :   while (--lx > 0) y[lx]=x[lx];
     123    15953088 :   return y;
     124             : }
     125             : 
     126             : /* NOTE: arguments of "spec" routines (muliispec, addiispec, etc.) aren't
     127             :  * GENs but pairs (long *a, long na) representing a list of digits (in basis
     128             :  * BITS_IN_LONG) : a[0], ..., a[na-1]. [ In ordre to facilitate splitting: no
     129             :  * need to reintroduce codewords ]
     130             :  * Use speci(a,na) to visualize the corresponding GEN.
     131             :  */
     132             : 
     133             : /***********************************************************************/
     134             : /**                                                                   **/
     135             : /**                     ADDITION / SUBTRACTION                        **/
     136             : /**                                                                   **/
     137             : /***********************************************************************/
     138             : 
     139             : GEN
     140     2900381 : setloop(GEN a)
     141             : {
     142     2900381 :   pari_sp av = avma - 2 * sizeof(long);
     143     2900381 :   (void)cgetg(lgefint(a) + 3, t_VECSMALL);
     144     2900380 :   return icopy_avma(a, av); /* two cells of extra space after a */
     145             : }
     146             : 
     147             : /* we had a = setloop(?), then some incloops. Reset a to b */
     148             : GEN
     149      170652 : resetloop(GEN a, GEN b) {
     150      170652 :   a[0] = evaltyp(t_INT) | evallg(lgefint(b));
     151      170652 :   affii(b, a); return a;
     152             : }
     153             : 
     154             : /* assume a > 0, initialized by setloop. Do a++ */
     155             : static GEN
     156    32290283 : incpos(GEN a)
     157             : {
     158    32290283 :   long i, l = lgefint(a);
     159    32290288 :   for (i=2; i<l; i++)
     160    32290284 :     if (++uel(a,i)) return a;
     161           4 :   a[l] = 1; l++;
     162           4 :   a[0]=evaltyp(t_INT) | _evallg(l);
     163           4 :   a[1]=evalsigne(1) | evallgefint(l);
     164           4 :   return a;
     165             : }
     166             : 
     167             : /* assume a < 0, initialized by setloop. Do a++ */
     168             : static GEN
     169        6272 : incneg(GEN a)
     170             : {
     171        6272 :   long i, l = lgefint(a)-1;
     172        6272 :   if (uel(a,2)--)
     173             :   {
     174        6268 :     if (!a[l]) /* implies l = 2 */
     175             :     {
     176         556 :       a[0] = evaltyp(t_INT) | _evallg(2);
     177         556 :       a[1] = evalsigne(0) | evallgefint(2);
     178             :     }
     179        6268 :     return a;
     180             :   }
     181           5 :   for (i=3; i<=l; i++)
     182           5 :     if (uel(a,i)--) break;
     183           4 :   if (!a[l])
     184             :   {
     185           4 :     a[0] = evaltyp(t_INT) | _evallg(l);
     186           4 :     a[1] = evalsigne(-1) | evallgefint(l);
     187             :   }
     188           4 :   return a;
     189             : }
     190             : 
     191             : /* assume a initialized by setloop. Do a++ */
     192             : GEN
     193    32596076 : incloop(GEN a)
     194             : {
     195    32596076 :   switch(signe(a))
     196             :   {
     197             :     case 0:
     198      331624 :       a[0]=evaltyp(t_INT) | _evallg(3);
     199      331624 :       a[1]=evalsigne(1) | evallgefint(3);
     200      331624 :       a[2]=1; return a;
     201        6272 :     case -1: return incneg(a);
     202    32258180 :     default: return incpos(a);
     203             :   }
     204             : }
     205             : 
     206             : INLINE GEN
     207  1182360431 : adduispec(ulong s, GEN x, long nx)
     208             : {
     209             :   GEN  zd;
     210             :   long lz;
     211             : 
     212  1182360431 :   if (nx == 1) return adduu(uel(x,0), s);
     213   228164366 :   lz = nx+3; zd = cgeti(lz);
     214   226897532 :   if (mpn_add_1(LIMBS(zd),(mp_limb_t *)x,nx,s))
     215     4863705 :     zd[lz-1]=1;
     216             :   else
     217   222060830 :     lz--;
     218   226924535 :   zd[1] = evalsigne(1) | evallgefint(lz);
     219   226924535 :   return zd;
     220             : }
     221             : 
     222             : GEN
     223   250273848 : adduispec_offset(ulong s, GEN x, long offset, long nx)
     224             : {
     225   250273848 :   GEN xd=x+2+offset;
     226   250273848 :   while (nx && *(xd+nx-1)==0) nx--;
     227   250273848 :   if (!nx) return utoi(s);
     228   233870124 :   return adduispec(s,xd,nx);
     229             : }
     230             : 
     231             : INLINE GEN
     232  1622438369 : addiispec(GEN x, GEN y, long nx, long ny)
     233             : {
     234             :   GEN zd;
     235             :   long lz;
     236             : 
     237  1622438369 :   if (nx < ny) swapspec(x,y, nx,ny);
     238  1622438369 :   if (ny == 1) return adduispec(*y,x,nx);
     239   735250096 :   lz = nx+3; zd = cgeti(lz);
     240             : 
     241   731566895 :   if (mpn_add(LIMBS(zd),(mp_limb_t *)x,nx,(mp_limb_t *)y,ny))
     242    11297476 :     zd[lz-1]=1;
     243             :   else
     244   720971772 :     lz--;
     245             : 
     246   732269248 :   zd[1] = evalsigne(1) | evallgefint(lz);
     247   732269248 :   return zd;
     248             : }
     249             : 
     250             : /* assume x >= y */
     251             : INLINE GEN
     252   832601029 : subiuspec(GEN x, ulong s, long nx)
     253             : {
     254             :   GEN zd;
     255             :   long lz;
     256             : 
     257   832601029 :   if (nx == 1) return utoi(x[0] - s);
     258             : 
     259    83679375 :   lz = nx + 2; zd = cgeti(lz);
     260    83625537 :   mpn_sub_1 (LIMBS(zd), (mp_limb_t *)x, nx, s);
     261    83626344 :   if (! zd[lz - 1]) { --lz; }
     262             : 
     263    83626344 :   zd[1] = evalsigne(1) | evallgefint(lz);
     264    83626344 :   return zd;
     265             : }
     266             : 
     267             : /* assume x > y */
     268             : INLINE GEN
     269  1541876119 : subiispec(GEN x, GEN y, long nx, long ny)
     270             : {
     271             :   GEN zd;
     272             :   long lz;
     273  1541876119 :   if (ny==1) return subiuspec(x,*y,nx);
     274   719418484 :   lz = nx+2; zd = cgeti(lz);
     275             : 
     276   710463293 :   mpn_sub (LIMBS(zd), (mp_limb_t *)x, nx, (mp_limb_t *)y, ny);
     277   711449118 :   while (lz >= 3 && zd[lz - 1] == 0) { lz--; }
     278             : 
     279   711449118 :   zd[1] = evalsigne(1) | evallgefint(lz);
     280   711449118 :   return zd;
     281             : }
     282             : 
     283             : static void
     284   326865633 : roundr_up_ip(GEN x, long l)
     285             : {
     286   326865633 :   long i = l;
     287             :   for(;;)
     288             :   {
     289   328924381 :     if (++((ulong*)x)[--i]) break;
     290     1138376 :     if (i == 2) { x[2] = HIGHBIT; shiftr_inplace(x, 1); break; }
     291             :   }
     292   326865633 : }
     293             : 
     294             : void
     295   139133311 : affir(GEN x, GEN y)
     296             : {
     297   139133311 :   const long s = signe(x), ly = lg(y);
     298             :   long lx, sh, i;
     299             : 
     300   139133311 :   if (!s)
     301             :   {
     302     5151795 :     y[1] = evalexpo(-bit_accuracy(ly));
     303     5151795 :     return;
     304             :   }
     305   133981516 :   lx = lgefint(x); sh = bfffo(*int_MSW(x));
     306   133981516 :   y[1] = evalsigne(s) | evalexpo(bit_accuracy(lx)-sh-1);
     307   133981516 :   if (sh) {
     308   132471270 :     if (lx <= ly)
     309             :     {
     310    82702446 :       for (i=lx; i<ly; i++) y[i]=0;
     311    82702446 :       mpn_lshift(LIMBS(y),LIMBS(x),lx-2,sh);
     312    82702446 :       xmpn_mirror(LIMBS(y),lx-2);
     313    82702446 :       return;
     314             :     }
     315    49768824 :     mpn_lshift(LIMBS(y),LIMBS(x)+lx-ly,ly-2,sh);
     316    49768824 :     uel(y,2) |= uel(x,lx-ly+1) >> (BITS_IN_LONG-sh);
     317    49768824 :     xmpn_mirror(LIMBS(y),ly-2);
     318             :     /* lx > ly: round properly */
     319    49768824 :     if ((uel(x,lx-ly+1)<<sh) & HIGHBIT) roundr_up_ip(y, ly);
     320             :   }
     321             :   else {
     322     1510246 :     GEN xd=int_MSW(x);
     323     1510246 :     if (lx <= ly)
     324             :     {
     325      781360 :       for (i=2; i<lx; i++,xd=int_precW(xd)) y[i]=*xd;
     326      781360 :       for (   ; i<ly; i++) y[i]=0;
     327      781360 :       return;
     328             :     }
     329      728886 :     for (i=2; i<ly; i++,xd=int_precW(xd)) y[i]=*xd;
     330             :     /* lx > ly: round properly */
     331      728886 :     if (uel(x,lx-ly+1) & HIGHBIT) roundr_up_ip(y, ly);
     332             :   }
     333             : }
     334             : 
     335             : INLINE GEN
     336   391025819 : shiftispec(GEN x, long nx, long n)
     337             : {
     338             :   long ny,m;
     339             :   GEN yd, y;
     340             : 
     341   391025819 :   if (!n) return icopyspec(x, nx);
     342             : 
     343   381543510 :   if (n > 0)
     344             :   {
     345   239789434 :     long d = dvmdsBIL(n, &m);
     346             :     long i;
     347             : 
     348   239767581 :     ny = nx + d + (m!=0);
     349   239767581 :     y = cgeti(ny + 2); yd = y + 2;
     350   239204294 :     for (i=0; i<d; i++) yd[i] = 0;
     351             : 
     352   239204294 :     if (!m) xmpn_copy((mp_limb_t *) (yd + d), (mp_limb_t *) x, nx);
     353             :     else
     354             :     {
     355   238451398 :       ulong carryd = mpn_lshift((mp_limb_t *) (yd + d), (mp_limb_t *) x, nx, m);
     356   238781959 :       if (carryd) yd[ny - 1] = carryd;
     357   226594681 :       else ny--;
     358             :     }
     359             :   }
     360             :   else
     361             :   {
     362   141754076 :     long d = dvmdsBIL(-n, &m);
     363             : 
     364   141753931 :     ny = nx - d;
     365   141753931 :     if (ny < 1) return gen_0;
     366   141096489 :     y = cgeti(ny + 2); yd = y + 2;
     367             : 
     368   141094383 :     if (!m) xmpn_copy((mp_limb_t *) yd, (mp_limb_t *) (x + d), nx - d);
     369             :     else
     370             :     {
     371   140091675 :       mpn_rshift((mp_limb_t *) yd, (mp_limb_t *) (x + d), nx - d, m);
     372   140091691 :       if (yd[ny - 1] == 0)
     373             :       {
     374    15284829 :         if (ny == 1) { avma = (pari_sp)(yd + 1); return gen_0; }
     375    13992749 :         ny--;
     376             :       }
     377             :     }
     378             :   }
     379   379207715 :   y[1] = evalsigne(1)|evallgefint(ny + 2);
     380   379207715 :   return y;
     381             : }
     382             : 
     383             : GEN
     384    29346969 : mantissa2nr(GEN x, long n)
     385             : {
     386    29346969 :   long ly, i, m, s = signe(x), lx = lg(x);
     387             :   GEN y;
     388    29346969 :   if (!s) return gen_0;
     389    29345661 :   if (!n)
     390             :   {
     391    11426669 :     y = cgeti(lx);
     392    11426669 :     y[1] = evalsigne(s) | evallgefint(lx);
     393    11426669 :     xmpn_mirrorcopy(LIMBS(y),RLIMBS(x),lx-2);
     394    11426669 :     return y;
     395             :   }
     396    17918992 :   if (n > 0)
     397             :   {
     398      315021 :     GEN z = (GEN)avma;
     399      315021 :     long d = dvmdsBIL(n, &m);
     400             : 
     401      315021 :     ly = lx+d; y = new_chunk(ly);
     402      315021 :     for ( ; d; d--) *--z = 0;
     403      315021 :     if (!m) for (i=2; i<lx; i++) y[i]=x[i];
     404             :     else
     405             :     {
     406      314624 :       register const ulong sh = BITS_IN_LONG - m;
     407      314624 :       shift_left(y,x, 2,lx-1, 0,m);
     408      314624 :       i = uel(x,2) >> sh;
     409             :       /* Extend y on the left? */
     410      314624 :       if (i) { ly++; y = new_chunk(1); y[2] = i; }
     411             :     }
     412             :   }
     413             :   else
     414             :   {
     415    17603971 :     ly = lx - dvmdsBIL(-n, &m);
     416    17603971 :     if (ly<3) return gen_0;
     417    17603971 :     y = new_chunk(ly);
     418    17603971 :     if (m) {
     419    17561711 :       shift_right(y,x, 2,ly, 0,m);
     420    17561711 :       if (y[2] == 0)
     421             :       {
     422           0 :         if (ly==3) { avma = (pari_sp)(y+3); return gen_0; }
     423           0 :         ly--; avma = (pari_sp)(++y);
     424             :       }
     425             :     } else {
     426       42260 :       for (i=2; i<ly; i++) y[i]=x[i];
     427             :     }
     428             :   }
     429    17918992 :   xmpn_mirror(LIMBS(y),ly-2);
     430    17918992 :   y[1] = evalsigne(s)|evallgefint(ly);
     431    17918992 :   y[0] = evaltyp(t_INT)|evallg(ly); return y;
     432             : }
     433             : 
     434             : GEN
     435     1944015 : truncr(GEN x)
     436             : {
     437             :   long s, e, d, m, i;
     438             :   GEN y;
     439     1944015 :   if ((s=signe(x)) == 0 || (e=expo(x)) < 0) return gen_0;
     440     1577288 :   d = nbits2lg(e+1); m = remsBIL(e);
     441     1577288 :   if (d > lg(x)) pari_err_PREC( "truncr (precision loss in truncation)");
     442             : 
     443     1577288 :   y=cgeti(d); y[1] = evalsigne(s) | evallgefint(d);
     444     1577288 :   if (++m == BITS_IN_LONG)
     445         147 :     for (i=2; i<d; i++) y[d-i+1]=x[i];
     446             :   else
     447             :   {
     448     1577141 :     GEN z=cgeti(d);
     449     1577141 :     for (i=2; i<d; i++) z[d-i+1]=x[i];
     450     1577141 :     mpn_rshift(LIMBS(y),LIMBS(z),d-2,BITS_IN_LONG-m);
     451     1577141 :     avma=(pari_sp)y;
     452             :   }
     453     1577288 :   return y;
     454             : }
     455             : 
     456             : /* integral part */
     457             : GEN
     458      737104 : floorr(GEN x)
     459             : {
     460             :   long e, d, m, i, lx;
     461             :   GEN y;
     462      737104 :   if (signe(x) >= 0) return truncr(x);
     463      245048 :   if ((e=expo(x)) < 0) return gen_m1;
     464       92253 :   d = nbits2lg(e+1); m = remsBIL(e);
     465       92253 :   lx=lg(x); if (d>lx) pari_err_PREC( "floorr (precision loss in truncation)");
     466       92253 :   y = cgeti(d+1);
     467       92253 :   if (++m == BITS_IN_LONG)
     468             :   {
     469          52 :     for (i=2; i<d; i++) y[d-i+1]=x[i];
     470          52 :     i=d; while (i<lx && !x[i]) i++;
     471          52 :     if (i==lx) goto END;
     472             :   }
     473             :   else
     474             :   {
     475       92201 :     GEN z=cgeti(d);
     476       92201 :     for (i=2; i<d; i++) z[d-i+1]=x[i];
     477       92201 :     mpn_rshift(LIMBS(y),LIMBS(z),d-2,BITS_IN_LONG-m);
     478       92201 :     if (uel(x,d-1)<<m == 0)
     479             :     {
     480       21727 :       i=d; while (i<lx && !x[i]) i++;
     481       21727 :       if (i==lx) goto END;
     482             :     }
     483             :   }
     484       81042 :   if (mpn_add_1(LIMBS(y),LIMBS(y),d-2,1))
     485           0 :     y[d++]=1;
     486             : END:
     487       92253 :   y[1] = evalsigne(-1) | evallgefint(d);
     488       92253 :   return y;
     489             : }
     490             : 
     491             : INLINE int
     492  1933593033 : cmpiispec(GEN x, GEN y, long lx, long ly)
     493             : {
     494  1933593033 :   if (lx < ly) return -1;
     495  1846079713 :   if (lx > ly) return  1;
     496  1756717957 :   return mpn_cmp((mp_limb_t*)x,(mp_limb_t*)y, lx);
     497             : }
     498             : 
     499             : INLINE int
     500   119306369 : equaliispec(GEN x, GEN y, long lx, long ly)
     501             : {
     502   119306369 :   if (lx != ly) return 0;
     503   119303860 :   return !mpn_cmp((mp_limb_t*)x,(mp_limb_t*)y, lx);
     504             : }
     505             : 
     506             : /***********************************************************************/
     507             : /**                                                                   **/
     508             : /**                          MULTIPLICATION                           **/
     509             : /**                                                                   **/
     510             : /***********************************************************************/
     511             : /* assume ny > 0 */
     512             : INLINE GEN
     513  2380695403 : muluispec(ulong x, GEN y, long ny)
     514             : {
     515  2380695403 :   if (ny == 1)
     516  1849271144 :     return muluu(x, *y);
     517             :   else
     518             :   {
     519   531424259 :     long lz = ny+3;
     520   531424259 :     GEN z = cgeti(lz);
     521   528828713 :     ulong hi = mpn_mul_1 (LIMBS(z), (mp_limb_t *)y, ny, x);
     522   528958554 :     if (hi) { z[lz - 1] = hi; } else lz--;
     523   528958554 :     z[1] = evalsigne(1) | evallgefint(lz);
     524   528958554 :     return z;
     525             :   }
     526             : }
     527             : 
     528             : /* a + b*|y| */
     529             : GEN
     530      931443 : addumului(ulong a, ulong b, GEN y)
     531             : {
     532             :   GEN z;
     533             :   long i, lz;
     534             :   ulong hi;
     535      931443 :   if (!b || !signe(y)) return utoi(a);
     536      931361 :   lz = lgefint(y)+1;
     537      931361 :   z = cgeti(lz);
     538      931361 :   z[2]=a;
     539      931361 :   for(i=3;i<lz;i++) z[i]=0;
     540      931361 :   hi=mpn_addmul_1(LIMBS(z), LIMBS(y), NLIMBS(y), b);
     541      931361 :   if (hi) z[lz-1]=hi; else lz--;
     542      931361 :   z[1] = evalsigne(1) | evallgefint(lz);
     543      931361 :   avma=(pari_sp)z; return z;
     544             : }
     545             : 
     546             : /***********************************************************************/
     547             : /**                                                                   **/
     548             : /**                          DIVISION                                 **/
     549             : /**                                                                   **/
     550             : /***********************************************************************/
     551             : 
     552             : ulong
     553   968841715 : umodiu(GEN y, ulong x)
     554             : {
     555   968841715 :   long sy=signe(y);
     556             :   ulong hi;
     557   968841715 :   if (!x) pari_err_INV("umodiu",gen_0);
     558   968884567 :   if (!sy) return 0;
     559   791088563 :   hi = mpn_mod_1(LIMBS(y),NLIMBS(y),x);
     560   791109967 :   if (!hi) return 0;
     561   781414538 :   return (sy > 0)? hi: x - hi;
     562             : }
     563             : 
     564             : /* return |y| \/ x */
     565             : GEN
     566   141701066 : absdiviu_rem(GEN y, ulong x, ulong *rem)
     567             : {
     568             :   long ly;
     569             :   GEN z;
     570             : 
     571   141701066 :   if (!x) pari_err_INV("absdiviu_rem",gen_0);
     572   141701265 :   if (!signe(y)) { *rem = 0; return gen_0; }
     573             : 
     574   141151744 :   ly = lgefint(y);
     575   141151744 :   if (ly == 3 && (ulong)x > uel(y,2)) { *rem = uel(y,2); return gen_0; }
     576             : 
     577   141084134 :   z = cgeti(ly);
     578   141027526 :   *rem = mpn_divrem_1(LIMBS(z), 0, LIMBS(y), NLIMBS(y), x);
     579   141061318 :   if (z [ly - 1] == 0) ly--;
     580   141061318 :   z[1] = evallgefint(ly) | evalsigne(1);
     581   141061318 :   return z;
     582             : }
     583             : 
     584             : GEN
     585    44886424 : divis_rem(GEN y, long x, long *rem)
     586             : {
     587    44886424 :   long sy=signe(y),ly,s;
     588             :   GEN z;
     589             : 
     590    44886424 :   if (!x) pari_err_INV("divis_rem",gen_0);
     591    44886424 :   if (!sy) { *rem = 0; return gen_0; }
     592    32306129 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     593             : 
     594    32306129 :   ly = lgefint(y);
     595    32306129 :   if (ly == 3 && (ulong)x > uel(y,2)) { *rem = itos(y); return gen_0; }
     596             : 
     597    20265003 :   z = cgeti(ly);
     598    20265003 :   *rem = mpn_divrem_1(LIMBS(z), 0, LIMBS(y), NLIMBS(y), x);
     599    20265003 :   if (sy<0) *rem = -  *rem;
     600    20265003 :   if (z[ly - 1] == 0) ly--;
     601    20265003 :   z[1] = evallgefint(ly) | evalsigne(s);
     602    20265003 :   return z;
     603             : }
     604             : 
     605             : GEN
     606      431073 : divis(GEN y, long x)
     607             : {
     608      431073 :   long sy=signe(y),ly,s;
     609             :   GEN z;
     610             : 
     611      431073 :   if (!x) pari_err_INV("divis",gen_0);
     612      431073 :   if (!sy) return gen_0;
     613      431073 :   if (x<0) { s = -sy; x = -x; } else s=sy;
     614             : 
     615      431073 :   ly = lgefint(y);
     616      431073 :   if (ly == 3 && (ulong)x > uel(y,2)) return gen_0;
     617             : 
     618      431073 :   z = cgeti(ly);
     619      431073 :   (void)mpn_divrem_1(LIMBS(z), 0, LIMBS(y), NLIMBS(y), x);
     620      431073 :   if (z[ly - 1] == 0) ly--;
     621      431073 :   z[1] = evallgefint(ly) | evalsigne(s);
     622      431073 :   return z;
     623             : }
     624             : 
     625             : /* We keep llx bits of x and lly bits of y*/
     626             : static GEN
     627    48316104 : divrr_with_gmp(GEN x, GEN y)
     628             : {
     629    48316104 :   long lx=RNLIMBS(x),ly=RNLIMBS(y);
     630    48316104 :   long lw=minss(lx,ly);
     631    48316104 :   long lly=minss(lw+1,ly);
     632    48316104 :   GEN  w=cgetr(lw+2);
     633    48316104 :   long lu=lw+lly;
     634    48316104 :   long llx=minss(lu,lx);
     635    48316104 :   mp_limb_t *u=(mp_limb_t *)new_chunk(lu);
     636    48316104 :   mp_limb_t *z=(mp_limb_t *)new_chunk(lly);
     637             :   mp_limb_t *q,*r;
     638    48316104 :   long e=expo(x)-expo(y);
     639    48316104 :   long sx=signe(x),sy=signe(y);
     640    48316104 :   xmpn_mirrorcopy(z,RLIMBS(y),lly);
     641    48316104 :   xmpn_mirrorcopy(u+lu-llx,RLIMBS(x),llx);
     642    48316104 :   xmpn_zero(u,lu-llx);
     643    48316104 :   q = (mp_limb_t *)new_chunk(lw+1);
     644    48316104 :   r = (mp_limb_t *)new_chunk(lly);
     645             : 
     646    48316104 :   mpn_tdiv_qr(q,r,0,u,lu,z,lly);
     647             : 
     648             :   /*Round up: This is not exactly correct we should test 2*r>z*/
     649    48316104 :   if (uel(r,lly-1) > (uel(z,lly-1)>>1))
     650    23911522 :     mpn_add_1(q,q,lw+1,1);
     651             : 
     652    48316104 :   xmpn_mirrorcopy(RLIMBS(w),q,lw);
     653             : 
     654    48316104 :   if (q[lw] == 0) e--;
     655    18395128 :   else if (q[lw] == 1) { shift_right(w,w, 2,lw+2, 1,1); }
     656          18 :   else { w[2] = HIGHBIT; e++; }
     657    48316104 :   if (sy < 0) sx = -sx;
     658    48316104 :   w[1] = evalsigne(sx) | evalexpo(e);
     659    48316104 :   avma=(pari_sp) w;
     660    48316104 :   return w;
     661             : }
     662             : 
     663             : /* We keep llx bits of x and lly bits of y*/
     664             : static GEN
     665     3617711 : divri_with_gmp(GEN x, GEN y)
     666             : {
     667     3617711 :   long llx=RNLIMBS(x),ly=NLIMBS(y);
     668     3617711 :   long lly=minss(llx+1,ly);
     669     3617711 :   GEN  w=cgetr(llx+2);
     670     3617711 :   long lu=llx+lly, ld=ly-lly;
     671     3617711 :   mp_limb_t *u=(mp_limb_t *)new_chunk(lu);
     672     3617711 :   mp_limb_t *z=(mp_limb_t *)new_chunk(lly);
     673             :   mp_limb_t *q,*r;
     674     3617711 :   long sh=bfffo(y[ly+1]);
     675     3617711 :   long e=expo(x)-expi(y);
     676     3617711 :   long sx=signe(x),sy=signe(y);
     677     3617711 :   if (sh) mpn_lshift(z,LIMBS(y)+ld,lly,sh);
     678       80551 :   else xmpn_copy(z,LIMBS(y)+ld,lly);
     679     3617711 :   xmpn_mirrorcopy(u+lu-llx,RLIMBS(x),llx);
     680     3617711 :   xmpn_zero(u,lu-llx);
     681     3617711 :   q = (mp_limb_t *)new_chunk(llx+1);
     682     3617711 :   r = (mp_limb_t *)new_chunk(lly);
     683             : 
     684     3617711 :   mpn_tdiv_qr(q,r,0,u,lu,z,lly);
     685             : 
     686             :   /*Round up: This is not exactly correct we should test 2*r>z*/
     687     3617711 :   if (uel(r,lly-1) > (uel(z,lly-1)>>1))
     688     1490658 :     mpn_add_1(q,q,llx+1,1);
     689             : 
     690     3617711 :   xmpn_mirrorcopy(RLIMBS(w),q,llx);
     691             : 
     692     3617711 :   if (q[llx] == 0) e--;
     693     2963985 :   else if (q[llx] == 1) { shift_right(w,w, 2,llx+2, 1,1); }
     694           0 :   else { w[2] = HIGHBIT; e++; }
     695     3617711 :   if (sy < 0) sx = -sx;
     696     3617711 :   w[1] = evalsigne(sx) | evalexpo(e);
     697     3617711 :   avma=(pari_sp) w;
     698     3617711 :   return w;
     699             : }
     700             : 
     701             : GEN
     702    20499179 : divri(GEN x, GEN y)
     703             : {
     704    20499179 :   long  s = signe(y);
     705             : 
     706    20499179 :   if (!s) pari_err_INV("divri",gen_0);
     707    20499179 :   if (!signe(x)) return real_0_bit(expo(x) - expi(y));
     708    20364161 :   if (!is_bigint(y)) {
     709    16746450 :     GEN z = divru(x, y[2]);
     710    16746450 :     if (s < 0) togglesign(z);
     711    16746450 :     return z;
     712             :   }
     713     3617711 :   return divri_with_gmp(x,y);
     714             : }
     715             : 
     716             : GEN
     717   106339030 : divrr(GEN x, GEN y)
     718             : {
     719   106339030 :   long sx=signe(x), sy=signe(y), lx,ly,lr,e,i,j;
     720             :   ulong y0,y1;
     721             :   GEN r, r1;
     722             : 
     723   106339030 :   if (!sy) pari_err_INV("divrr",y);
     724   106339030 :   e = expo(x) - expo(y);
     725   106339030 :   if (!sx) return real_0_bit(e);
     726   104742739 :   if (sy<0) sx = -sx;
     727             : 
     728   104742739 :   lx=lg(x); ly=lg(y);
     729   104742739 :   if (ly==3)
     730             :   {
     731    56426635 :     ulong k = x[2], l = (lx>3)? x[3]: 0;
     732             :     LOCAL_HIREMAINDER;
     733    56426635 :     if (k < uel(y,2)) e--;
     734             :     else
     735             :     {
     736    26047911 :       l >>= 1; if (k&1) l |= HIGHBIT;
     737    26047911 :       k >>= 1;
     738             :     }
     739    56426635 :     hiremainder = k; k = divll(l,y[2]);
     740    56426635 :     if (hiremainder & HIGHBIT)
     741             :     {
     742    15640409 :       k++;
     743    15640409 :       if (!k) { k = HIGHBIT; e++; }
     744             :     }
     745    56426635 :     r = cgetr(3);
     746    56426635 :     r[1] = evalsigne(sx) | evalexpo(e);
     747    56426635 :     r[2] = k; return r;
     748             :   }
     749             : 
     750    48316104 :   if (ly>=DIVRR_GMP_LIMIT)
     751    48316104 :     return divrr_with_gmp(x,y);
     752             : 
     753           0 :   lr = minss(lx,ly); r = new_chunk(lr);
     754           0 :   r1 = r-1;
     755           0 :   r1[1] = 0; for (i=2; i<lr; i++) r1[i]=x[i];
     756           0 :   r1[lr] = (lx>ly)? x[lr]: 0;
     757           0 :   y0 = y[2]; y1 = y[3];
     758           0 :   for (i=0; i<lr-1; i++)
     759             :   { /* r1 = r + (i-1), OK up to r1[2] (accesses at most r[lr]) */
     760             :     ulong k, qp;
     761             :     LOCAL_HIREMAINDER;
     762             :     LOCAL_OVERFLOW;
     763             : 
     764           0 :     if (uel(r1,1) == y0)
     765             :     {
     766           0 :       qp = ULONG_MAX; k = addll(y0,r1[2]);
     767             :     }
     768             :     else
     769             :     {
     770           0 :       if (uel(r1,1) > y0) /* can't happen if i=0 */
     771             :       {
     772           0 :         GEN y1 = y+1;
     773           0 :         j = lr-i; r1[j] = subll(r1[j],y1[j]);
     774           0 :         for (j--; j>0; j--) r1[j] = subllx(r1[j],y1[j]);
     775           0 :         j=i; do uel(r,--j)++; while (j && !r[j]);
     776             :       }
     777           0 :       hiremainder = r1[1]; overflow = 0;
     778           0 :       qp = divll(r1[2],y0); k = hiremainder;
     779             :     }
     780           0 :     j = lr-i+1;
     781           0 :     if (!overflow)
     782             :     {
     783             :       long k3, k4;
     784           0 :       k3 = mulll(qp,y1);
     785           0 :       if (j == 3) /* i = lr - 2 maximal, r1[3] undefined -> 0 */
     786           0 :         k4 = subll(hiremainder,k);
     787             :       else
     788             :       {
     789           0 :         k3 = subll(k3, r1[3]);
     790           0 :         k4 = subllx(hiremainder,k);
     791             :       }
     792           0 :       while (!overflow && k4) { qp--; k3=subll(k3,y1); k4=subllx(k4,y0); }
     793             :     }
     794           0 :     if (j<ly) (void)mulll(qp,y[j]); else { hiremainder = 0 ; j = ly; }
     795           0 :     for (j--; j>1; j--)
     796             :     {
     797           0 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     798           0 :       hiremainder += overflow;
     799             :     }
     800           0 :     if (uel(r1,1) != hiremainder)
     801             :     {
     802           0 :       if (uel(r1,1) < hiremainder)
     803             :       {
     804           0 :         qp--;
     805           0 :         j = lr-i-(lr-i>=ly); r1[j] = addll(r1[j], y[j]);
     806           0 :         for (j--; j>1; j--) r1[j] = addllx(r1[j], y[j]);
     807             :       }
     808             :       else
     809             :       {
     810           0 :         uel(r1,1) -= hiremainder;
     811           0 :         while (r1[1])
     812             :         {
     813           0 :           qp++; if (!qp) { j=i; do uel(r,--j)++; while (j && !r[j]); }
     814           0 :           j = lr-i-(lr-i>=ly); r1[j] = subll(r1[j],y[j]);
     815           0 :           for (j--; j>1; j--) r1[j] = subllx(r1[j],y[j]);
     816           0 :           uel(r1,1) -= overflow;
     817             :         }
     818             :       }
     819             :     }
     820           0 :     *++r1 = qp;
     821             :   }
     822             :   /* i = lr-1 */
     823             :   /* round correctly */
     824           0 :   if (uel(r1,1) > (y0>>1))
     825             :   {
     826           0 :     j=i; do uel(r,--j)++; while (j && !r[j]);
     827             :   }
     828           0 :   r1 = r-1; for (j=i; j>=2; j--) r[j]=r1[j];
     829           0 :   if (r[0] == 0) e--;
     830           0 :   else if (r[0] == 1) { shift_right(r,r, 2,lr, 1,1); }
     831             :   else { /* possible only when rounding up to 0x2 0x0 ... */
     832           0 :     r[2] = (long)HIGHBIT; e++;
     833             :   }
     834           0 :   r[0] = evaltyp(t_REAL)|evallg(lr);
     835           0 :   r[1] = evalsigne(sx) | evalexpo(e);
     836           0 :   return r;
     837             : }
     838             : 
     839             : /* Integer division x / y: such that sign(r) = sign(x)
     840             :  *   if z = ONLY_REM return remainder, otherwise return quotient
     841             :  *   if z != NULL set *z to remainder
     842             :  *   *z is the last object on stack (and thus can be disposed of with cgiv
     843             :  *   instead of gerepile)
     844             :  * If *z is zero, we put gen_0 here and no copy.
     845             :  * space needed: lx + ly
     846             :  */
     847             : GEN
     848   839174631 : dvmdii(GEN x, GEN y, GEN *z)
     849             : {
     850   839174631 :   long sx=signe(x),sy=signe(y);
     851             :   long lx, ly, lq;
     852             :   pari_sp av;
     853             :   GEN r,q;
     854             : 
     855   839174631 :   if (!sy) pari_err_INV("dvmdii",y);
     856   842603026 :   if (!sx)
     857             :   {
     858    22523511 :     if (!z || z == ONLY_REM) return gen_0;
     859     8382982 :     *z=gen_0; return gen_0;
     860             :   }
     861   820079515 :   lx=lgefint(x);
     862   820079515 :   ly=lgefint(y); lq=lx-ly;
     863   820079515 :   if (lq <= 0)
     864             :   {
     865   506549507 :     if (lq == 0)
     866             :     {
     867   484510127 :       long s=mpn_cmp(LIMBS(x),LIMBS(y),NLIMBS(x));
     868   486730904 :       if (s>0) goto DIVIDE;
     869   177619811 :       if (s==0)
     870             :       {
     871     9285987 :         if (z == ONLY_REM) return gen_0;
     872     4143818 :         if (z) *z = gen_0;
     873     4143818 :         if (sx < 0) sy = -sy;
     874     4143818 :         return stoi(sy);
     875             :       }
     876             :     }
     877   190373204 :     if (z == ONLY_REM) return icopy(x);
     878     6674933 :     if (z) *z = icopy(x);
     879     6674933 :     return gen_0;
     880             :   }
     881             : DIVIDE: /* quotient is non-zero */
     882   622641101 :   av=avma; if (sx<0) sy = -sy;
     883   622641101 :   if (ly==3)
     884             :   {
     885   266038773 :     ulong lq = lx;
     886             :     ulong si;
     887   266038773 :     q  = cgeti(lq);
     888   265819885 :     si = mpn_divrem_1(LIMBS(q), 0, LIMBS(x), NLIMBS(x), y[2]);
     889   265966256 :     if (q[lq - 1] == 0) lq--;
     890   265966256 :     if (z == ONLY_REM)
     891             :     {
     892   221951902 :       avma=av; if (!si) return gen_0;
     893   195174624 :       r=cgeti(3);
     894   195121433 :       r[1] = evalsigne(sx) | evallgefint(3);
     895   195121433 :       r[2] = si; return r;
     896             :     }
     897    44014354 :     q[1] = evalsigne(sy) | evallgefint(lq);
     898    44014354 :     if (!z) return q;
     899    40339965 :     if (!si) { *z=gen_0; return q; }
     900    16986699 :     r=cgeti(3);
     901    16986676 :     r[1] = evalsigne(sx) | evallgefint(3);
     902    16986676 :     r[2] = si; *z=r; return q;
     903             :   }
     904   356602328 :   if (z == ONLY_REM)
     905             :   {
     906   349508559 :     ulong lr = lgefint(y);
     907   349508559 :     ulong lq = lgefint(x)-lgefint(y)+3;
     908   349508559 :     GEN r = cgeti(lr);
     909   340788908 :     GEN q = cgeti(lq);
     910   336849693 :     mpn_tdiv_qr(LIMBS(q), LIMBS(r),0, LIMBS(x), NLIMBS(x), LIMBS(y), NLIMBS(y));
     911   357901674 :     if (!r[lr - 1])
     912             :     {
     913    88663202 :       while(lr>2 && !r[lr - 1]) lr--;
     914    88663202 :       if (lr == 2) {avma=av; return gen_0;} /* exact division */
     915             :     }
     916   352655458 :     r[1] = evalsigne(sx) | evallgefint(lr);
     917   352655458 :     avma = (pari_sp) r; return r;
     918             :   }
     919             :   else
     920             :   {
     921     7093769 :     ulong lq = lgefint(x)-lgefint(y)+3;
     922     7093769 :     ulong lr = lgefint(y);
     923     7093769 :     GEN q = cgeti(lq);
     924     7093769 :     GEN r = cgeti(lr);
     925     7093769 :     mpn_tdiv_qr(LIMBS(q), LIMBS(r),0, LIMBS(x), NLIMBS(x), LIMBS(y), NLIMBS(y));
     926     7093769 :     if (q[lq - 1] == 0) lq--;
     927     7093769 :     q[1] = evalsigne(sy) | evallgefint(lq);
     928     7093769 :     if (!z) { avma = (pari_sp)q; return q; }
     929     6469830 :     if (!r[lr - 1])
     930             :     {
     931     2206516 :       while(lr>2 && !r[lr - 1]) lr--;
     932     2206516 :       if (lr == 2) {avma=(pari_sp) q; *z=gen_0; return q;} /* exact division */
     933             :     }
     934     4772243 :     r[1] = evalsigne(sx) | evallgefint(lr);
     935     4772243 :     avma = (pari_sp) r; *z = r; return q;
     936             :   }
     937             : }
     938             : 
     939             : /* Montgomery reduction.
     940             :  * N has k words, assume T >= 0 has less than 2k.
     941             :  * Return res := T / B^k mod N, where B = 2^BIL
     942             :  * such that 0 <= res < T/B^k + N  and  res has less than k words */
     943             : GEN
     944    10770054 : red_montgomery(GEN T, GEN N, ulong inv)
     945             : {
     946             :   pari_sp av;
     947             :   GEN Te, Td, Ne, Nd, scratch;
     948    10770054 :   ulong i, j, m, t, d, k = NLIMBS(N);
     949             :   int carry;
     950             :   LOCAL_HIREMAINDER;
     951             :   LOCAL_OVERFLOW;
     952             : 
     953    10770054 :   if (k == 0) return gen_0;
     954    10770054 :   d = NLIMBS(T); /* <= 2*k */
     955    10770054 :   if (d == 0) return gen_0;
     956             : #ifdef DEBUG
     957             :   if (d > 2*k) pari_err_BUG("red_montgomery");
     958             : #endif
     959    10770045 :   if (k == 1)
     960             :   { /* as below, special cased for efficiency */
     961         492 :     ulong n = uel(N,2);
     962         492 :     if (d == 1) {
     963         492 :       hiremainder = uel(T,2);
     964         492 :       m = hiremainder * inv;
     965         492 :       (void)addmul(m, n); /* t + m*n = 0 */
     966         492 :       return utoi(hiremainder);
     967             :     } else { /* d = 2 */
     968           0 :       hiremainder = uel(T,2);
     969           0 :       m = hiremainder * inv;
     970           0 :       (void)addmul(m, n); /* t + m*n = 0 */
     971           0 :       t = addll(hiremainder, uel(T,3));
     972           0 :       if (overflow) t -= n; /* t > n doesn't fit in 1 word */
     973           0 :       return utoi(t);
     974             :     }
     975             :   }
     976             :   /* assume k >= 2 */
     977    10769553 :   av = avma; scratch = new_chunk(k<<1); /* >= k + 2: result fits */
     978             : 
     979             :   /* copy T to scratch space (pad with zeroes to 2k words) */
     980    10753621 :   Td = scratch;
     981    10753621 :   Te = T + 2;
     982    10753621 :   for (i=0; i < d     ; i++) *Td++ = *Te++;
     983    10753621 :   for (   ; i < (k<<1); i++) *Td++ = 0;
     984             : 
     985    10753621 :   Te = scratch - 1; /* 1 beyond end of current T mantissa (in scratch) */
     986    10753621 :   Ne = N + 1;       /* 1 beyond end of N mantissa */
     987             : 
     988    10753621 :   carry = 0;
     989    81721979 :   for (i=0; i<k; i++) /* set T := T/B nod N, k times */
     990             :   {
     991    70968358 :     Td = Te; /* one beyond end of (new) T mantissa */
     992    70968358 :     Nd = Ne;
     993    70968358 :     hiremainder = *++Td;
     994    70968358 :     m = hiremainder * inv; /* solve T + m N = O(B) */
     995             : 
     996             :     /* set T := (T + mN) / B */
     997    70968358 :     Te = Td;
     998    70968358 :     (void)addmul(m, *++Nd); /* = 0 */
     999   685607727 :     for (j=1; j<k; j++)
    1000             :     {
    1001   614639369 :       t = addll(addmul(m, *++Nd), *++Td);
    1002   614639369 :       *Td = t;
    1003   614639369 :       hiremainder += overflow;
    1004             :     }
    1005    70968358 :     t = addll(hiremainder, *++Td); *Td = t + carry;
    1006    70968358 :     carry = (overflow || (carry && *Td == 0));
    1007             :   }
    1008    10753621 :   if (carry)
    1009             :   { /* Td > N overflows (k+1 words), set Td := Td - N */
    1010       34378 :     GEN NE = N + k+2;
    1011       34378 :     Td = Te;
    1012       34378 :     Nd = Ne;
    1013       34378 :     t = subll(*++Td, *++Nd); *Td = t;
    1014       34378 :     while (Nd < NE) { t = subllx(*++Td, *++Nd); *Td = t; }
    1015             :   }
    1016             : 
    1017             :   /* copy result */
    1018    10753621 :   Td = (GEN)av - 1; /* *Td = high word of final result */
    1019    10753621 :   while (*Td == 0 && Te < Td) Td--; /* strip leading 0s */
    1020    10753621 :   k = Td - Te; if (!k) { set_avma(av); return gen_0; }
    1021    10753621 :   Td = (GEN)av - k; /* will write mantissa there */
    1022    10753621 :   (void)memmove(Td, Te+1, k*sizeof(long));
    1023    10753621 :   Td -= 2;
    1024    10753621 :   Td[0] = evaltyp(t_INT) | evallg(k+2);
    1025    10750962 :   Td[1] = evalsigne(1) | evallgefint(k+2);
    1026             : #ifdef DEBUG
    1027             : {
    1028             :   long l = NLIMBS(N), s = BITS_IN_LONG*l;
    1029             :   GEN R = int2n(s);
    1030             :   GEN res = remii(mulii(T, Fp_inv(R, N)), N);
    1031             :   if (k > lgefint(N)
    1032             :     || !equalii(remii(Td,N),res)
    1033             :     || cmpii(Td, addii(shifti(T, -s), N)) >= 0) pari_err_BUG("red_montgomery");
    1034             : }
    1035             : #endif
    1036    10750962 :   avma = (pari_sp)Td; return Td;
    1037             : }
    1038             : 
    1039             : /* EXACT INTEGER DIVISION */
    1040             : 
    1041             : /* use undocumented GMP interface */
    1042             : static void
    1043    37553036 : GEN2mpz(mpz_t X, GEN x)
    1044             : {
    1045    37553036 :   long l = lgefint(x)-2;
    1046    37553036 :   X->_mp_alloc = l;
    1047    37553036 :   X->_mp_size = signe(x) > 0? l: -l;
    1048    37553036 :   X->_mp_d = LIMBS(x);
    1049    37553036 : }
    1050             : static void
    1051    18776518 : mpz2GEN(GEN z, mpz_t Z)
    1052             : {
    1053    18776518 :   long l = Z->_mp_size;
    1054    18776518 :   z[1] = evalsigne(l > 0? 1: -1) | evallgefint(labs(l)+2);
    1055    18776518 : }
    1056             : 
    1057             : #ifdef mpn_divexact_1
    1058             : static GEN
    1059   265428169 : diviuexact_i(GEN x, ulong y)
    1060             : {
    1061   265428169 :   long l = lgefint(x);
    1062   265428169 :   GEN z = cgeti(l);
    1063   265416187 :   mpn_divexact_1(LIMBS(z), LIMBS(x), NLIMBS(x), y);
    1064   265416479 :   if (z[l-1] == 0) l--;
    1065   265416479 :   z[1] = evallgefint(l) | evalsigne(signe(x));
    1066   265416479 :   return z;
    1067             : }
    1068             : #elif 1 && !defined(_WIN64) /* mpz_divexact_ui is not LLP64 friendly   */
    1069             :                             /* assume y != 0 and the division is exact */
    1070             : static GEN
    1071             : diviuexact_i(GEN x, ulong y)
    1072             : {
    1073             :   long l = lgefint(x);
    1074             :   GEN z = cgeti(l);
    1075             :   mpz_t X, Z;
    1076             :   GEN2mpz(X, x);
    1077             :   Z->_mp_alloc = l-2;
    1078             :   Z->_mp_size  = l-2;
    1079             :   Z->_mp_d = LIMBS(z);
    1080             :   mpz_divexact_ui(Z, X, y);
    1081             :   mpz2GEN(z, Z); return z;
    1082             : }
    1083             : #else
    1084             : /* assume y != 0 and the division is exact */
    1085             : static GEN
    1086             : diviuexact_i(GEN x, ulong y)
    1087             : {
    1088             :   /*TODO: implement true exact division.*/
    1089             :   return divii(x,utoi(y));
    1090             : }
    1091             : #endif
    1092             : 
    1093             : GEN
    1094    33389630 : diviuexact(GEN x, ulong y)
    1095             : {
    1096             :   GEN z;
    1097    33389630 :   if (!signe(x)) return gen_0;
    1098    33125106 :   z = diviuexact_i(x, y);
    1099    33125153 :   if (lgefint(z) == 2) pari_err_OP("exact division", x, utoi(y));
    1100    33125122 :   return z;
    1101             : }
    1102             : 
    1103             : /* Find z such that x=y*z, knowing that y | x (unchecked) */
    1104             : GEN
    1105   306348242 : diviiexact(GEN x, GEN y)
    1106             : {
    1107             :   GEN z;
    1108   306348242 :   if (!signe(y)) pari_err_INV("diviiexact",y);
    1109   306361942 :   if (!signe(x)) return gen_0;
    1110   251071041 :   if (lgefint(y) == 3)
    1111             :   {
    1112   232294523 :     z = diviuexact_i(x, y[2]);
    1113   232290860 :     if (signe(y) < 0) togglesign(z);
    1114             :   }
    1115             :   else
    1116             :   {
    1117    18776518 :     long l = lgefint(x);
    1118             :     mpz_t X, Y, Z;
    1119    18776518 :     z = cgeti(l);
    1120    18776518 :     GEN2mpz(X, x);
    1121    18776518 :     GEN2mpz(Y, y);
    1122    18776518 :     Z->_mp_alloc = l-2;
    1123    18776518 :     Z->_mp_size  = l-2;
    1124    18776518 :     Z->_mp_d = LIMBS(z);
    1125    18776518 :     mpz_divexact(Z, X, Y);
    1126    18776518 :     mpz2GEN(z, Z);
    1127             :   }
    1128   251067466 :   if (lgefint(z) == 2) pari_err_OP("exact division", x, y);
    1129   251067171 :   return z;
    1130             : }
    1131             : 
    1132             : /* assume yz != and yz | x */
    1133             : GEN
    1134      580213 : diviuuexact(GEN x, ulong y, ulong z)
    1135             : {
    1136             :   long tmp[4];
    1137             :   ulong t;
    1138             :   LOCAL_HIREMAINDER;
    1139      580213 :   t = mulll(y, z);
    1140      580213 :   if (!hiremainder) return diviuexact(x, t);
    1141           0 :   tmp[0] = evaltyp(t_INT)|_evallg(4);
    1142           0 :   tmp[1] = evalsigne(1)|evallgefint(4);
    1143           0 :   tmp[2] = t;
    1144           0 :   tmp[3] = hiremainder;
    1145           0 :   return diviiexact(x, tmp);
    1146             : }
    1147             : 
    1148             : 
    1149             : /********************************************************************/
    1150             : /**                                                                **/
    1151             : /**               INTEGER MULTIPLICATION                           **/
    1152             : /**                                                                **/
    1153             : /********************************************************************/
    1154             : 
    1155             : /* nx >= ny = num. of digits of x, y (not GEN, see mulii) */
    1156             : GEN
    1157  2224194822 : muliispec(GEN x, GEN y, long nx, long ny)
    1158             : {
    1159             :   GEN zd;
    1160             :   long lz;
    1161             :   ulong hi;
    1162             : 
    1163  2224194822 :   if (nx < ny) swapspec(x,y, nx,ny);
    1164  2224194822 :   if (!ny) return gen_0;
    1165  2224194822 :   if (ny == 1) return muluispec((ulong)*y, x, nx);
    1166             : 
    1167   523583212 :   lz = nx+ny+2;
    1168   523583212 :   zd = cgeti(lz);
    1169   520242409 :   hi = mpn_mul(LIMBS(zd), (mp_limb_t *)x, nx, (mp_limb_t *)y, ny);
    1170   526781516 :   if (!hi) lz--;
    1171             :   /*else zd[lz-1]=hi; GH tell me it is not necessary.*/
    1172             : 
    1173   526781516 :   zd[1] = evalsigne(1) | evallgefint(lz);
    1174   526781516 :   return zd;
    1175             : }
    1176             : GEN
    1177      602421 : muluui(ulong x, ulong y, GEN z)
    1178             : {
    1179      602421 :   long t, s = signe(z);
    1180             :   GEN r;
    1181             :   LOCAL_HIREMAINDER;
    1182             : 
    1183      602421 :   if (!x || !y || !signe(z)) return gen_0;
    1184      602025 :   t = mulll(x,y);
    1185      602025 :   if (!hiremainder)
    1186      602025 :     r = muluispec(t, z+2, lgefint(z)-2);
    1187             :   else
    1188             :   {
    1189             :     long tmp[2];
    1190           0 :     tmp[1] = hiremainder;
    1191           0 :     tmp[0] = t;
    1192           0 :     r = muliispec(z+2,tmp, lgefint(z)-2, 2);
    1193             :   }
    1194      602025 :   setsigne(r,s); return r;
    1195             : }
    1196             : 
    1197             : GEN
    1198   752699795 : sqrispec(GEN x, long nx)
    1199             : {
    1200             :   GEN zd;
    1201             :   long lz;
    1202             : 
    1203   752699795 :   if (!nx) return gen_0;
    1204   210537099 :   if (nx==1) return sqru(*x);
    1205             : 
    1206   139967181 :   lz = (nx<<1)+2;
    1207   139967181 :   zd = cgeti(lz);
    1208             : #ifdef mpn_sqr
    1209   135531654 :   mpn_sqr(LIMBS(zd), (mp_limb_t *)x, nx);
    1210             : #else
    1211             :   mpn_mul_n(LIMBS(zd), (mp_limb_t *)x, (mp_limb_t *)x, nx);
    1212             : #endif
    1213   137323461 :   if (zd[lz-1]==0) lz--;
    1214             : 
    1215   137323461 :   zd[1] = evalsigne(1) | evallgefint(lz);
    1216   137323461 :   return zd;
    1217             : }
    1218             : 
    1219             : INLINE GEN
    1220     6649573 : sqrispec_mirror(GEN x, long nx)
    1221             : {
    1222     6649573 :   GEN cx=new_chunk(nx);
    1223             :   GEN z;
    1224     6649573 :   xmpn_mirrorcopy((mp_limb_t *)cx,(mp_limb_t *)x,nx);
    1225     6649573 :   z=sqrispec(cx, nx);
    1226     6649573 :   xmpn_mirror(LIMBS(z), NLIMBS(z));
    1227     6649573 :   return z;
    1228             : }
    1229             : 
    1230             : /* leaves garbage on the stack. */
    1231             : INLINE GEN
    1232    11695811 : muliispec_mirror(GEN x, GEN y, long nx, long ny)
    1233             : {
    1234             :   GEN cx, cy, z;
    1235    11695811 :   long s = 0;
    1236    11695811 :   while (nx && x[nx-1]==0) { nx--; s++; }
    1237    11695811 :   while (ny && y[ny-1]==0) { ny--; s++; }
    1238    11695811 :   cx=new_chunk(nx); cy=new_chunk(ny);
    1239    11695811 :   xmpn_mirrorcopy((mp_limb_t *)cx,(mp_limb_t *)x,nx);
    1240    11695811 :   xmpn_mirrorcopy((mp_limb_t *)cy,(mp_limb_t *)y,ny);
    1241    11695811 :   z =  nx>=ny ? muliispec(cx, cy, nx, ny): muliispec(cy, cx, ny, nx);
    1242    11695811 :   if (s)
    1243             :   {
    1244     1892718 :     long i, lz = lgefint(z) + s;
    1245     1892718 :     (void)new_chunk(s);
    1246     1892718 :     z -= s;
    1247     1892718 :     for (i=0; i<s; i++) z[2+i]=0;
    1248     1892718 :     z[1] = evalsigne(1) | evallgefint(lz);
    1249     1892718 :     z[0] = evaltyp(t_INT) | evallg(lz);
    1250             :   }
    1251    11695811 :   xmpn_mirror(LIMBS(z), NLIMBS(z));
    1252    11695811 :   return z;
    1253             : }
    1254             : 
    1255             : /* x % (2^n), assuming n >= 0 */
    1256             : GEN
    1257    16959970 : remi2n(GEN x, long n)
    1258             : {
    1259             :   ulong hi;
    1260    16959970 :   long l, k, lx, ly, sx = signe(x);
    1261             :   GEN z, xd, zd;
    1262             : 
    1263    16959970 :   if (!sx || !n) return gen_0;
    1264             : 
    1265    16804939 :   k = dvmdsBIL(n, &l);
    1266    17417495 :   lx = lgefint(x);
    1267    17417495 :   if (lx < k+3) return icopy(x);
    1268             : 
    1269    17109787 :   xd = x + (2 + k);
    1270             :   /* x = |k|...|1|#|... : copy the last l bits of # and the first k words
    1271             :    *              ^--- initial xd  */
    1272    17109787 :   hi = ((ulong)*xd) & ((1UL<<l)-1); /* last l bits of # = top bits of result */
    1273    17109787 :   if (!hi)
    1274             :   { /* strip leading zeroes from result */
    1275     1761249 :     xd--; while (k && !*xd) { k--; xd--; }
    1276     1761249 :     if (!k) return gen_0;
    1277     1009782 :     ly = k+2;
    1278             :   }
    1279             :   else
    1280    15348538 :     ly = k+3;
    1281             : 
    1282    16358320 :   zd = z = cgeti(ly);
    1283    15911640 :   *++zd = evalsigne(sx) | evallgefint(ly);
    1284    15911640 :   xd = x+1; for ( ;k; k--) *++zd = *++xd;
    1285    15911640 :   if (hi) *++zd = hi;
    1286    15911640 :   return z;
    1287             : }
    1288             : 
    1289             : /********************************************************************/
    1290             : /**                                                                **/
    1291             : /**                      INTEGER SQUARE ROOT                       **/
    1292             : /**                                                                **/
    1293             : /********************************************************************/
    1294             : 
    1295             : /* Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S.
    1296             :  * As for dvmdii, R is last on stack and guaranteed to be gen_0 in case the
    1297             :  * remainder is 0. R = NULL is allowed. */
    1298             : GEN
    1299     3603948 : sqrtremi(GEN a, GEN *r)
    1300             : {
    1301     3603948 :   long l, na = NLIMBS(a);
    1302             :   mp_size_t nr;
    1303             :   GEN S;
    1304     3603948 :   if (!na) {
    1305           8 :     if (r) *r = gen_0;
    1306           8 :     return gen_0;
    1307             :   }
    1308     3603940 :   l = (na + 5) >> 1; /* 2 + ceil(na/2) */
    1309     3603940 :   S = cgetipos(l);
    1310     3603940 :   if (r) {
    1311      348219 :     GEN R = cgeti(2 + na);
    1312      348219 :     nr = mpn_sqrtrem(LIMBS(S), LIMBS(R), LIMBS(a), na);
    1313      348219 :     if (nr) R[1] = evalsigne(1) | evallgefint(nr+2);
    1314       10481 :     else    { avma = (pari_sp)S; R = gen_0; }
    1315      348219 :     *r = R;
    1316             :   }
    1317             :   else
    1318     3255721 :     (void)mpn_sqrtrem(LIMBS(S), NULL, LIMBS(a), na);
    1319     3603940 :   return S;
    1320             : }
    1321             : 
    1322             : /* compute sqrt(|a|), assuming a != 0 */
    1323             : GEN
    1324    26300666 : sqrtr_abs(GEN a)
    1325             : {
    1326             :   GEN res;
    1327             :   mp_limb_t *b, *c;
    1328    26300666 :   long l = RNLIMBS(a), e = expo(a), er = e>>1;
    1329             :   long n;
    1330    26300666 :   res = cgetr(2 + l);
    1331    26300666 :   res[1] = evalsigne(1) | evalexpo(er);
    1332    26300666 :   if (e&1)
    1333             :   {
    1334    12896314 :     b = (mp_limb_t *) new_chunk(l<<1);
    1335    12896314 :     xmpn_zero(b,l);
    1336    12896314 :     xmpn_mirrorcopy(b+l, RLIMBS(a), l);
    1337    12896314 :     c = (mp_limb_t *) new_chunk(l);
    1338    12896314 :     n = mpn_sqrtrem(c,b,b,l<<1); /* c <- sqrt; b <- rem */
    1339    12896314 :     if (n>l || (n==l && mpn_cmp(b,c,l) > 0)) mpn_add_1(c,c,l,1);
    1340             :   }
    1341             :   else
    1342             :   {
    1343             :     ulong u;
    1344    13404352 :     b = (mp_limb_t *) mantissa2nr(a,-1);
    1345    13404352 :     b[1] = uel(a,l+1)<<(BITS_IN_LONG-1);
    1346    13404352 :     b = (mp_limb_t *) new_chunk(l);
    1347    13404352 :     xmpn_zero(b,l+1); /* overwrites the former b[0] */
    1348    13404352 :     c = (mp_limb_t *) new_chunk(l + 1);
    1349    13404352 :     n = mpn_sqrtrem(c,b,b,(l<<1)+2); /* c <- sqrt; b <- rem */
    1350    13404352 :     u = (ulong)*c++;
    1351    13404352 :     if ( u&HIGHBIT || (u == ~HIGHBIT &&
    1352           0 :              (n>l || (n==l && mpn_cmp(b,c,l)>0))))
    1353     6545412 :       mpn_add_1(c,c,l,1);
    1354             :   }
    1355    26300666 :   xmpn_mirrorcopy(RLIMBS(res),c,l);
    1356    26300666 :   avma = (pari_sp)res; return res;
    1357             : }
    1358             : 
    1359             : /* Normalize a non-negative integer */
    1360             : GEN
    1361    90147258 : int_normalize(GEN x, long known_zero_words)
    1362             : {
    1363    90147258 :   long i =  lgefint(x) - 1 - known_zero_words;
    1364  1049646271 :   for ( ; i > 1; i--)
    1365  1017899122 :     if (x[i]) { setlgefint(x, i+1); return x; }
    1366    31747149 :   x[1] = evalsigne(0) | evallgefint(2); return x;
    1367             : }
    1368             : 
    1369             : /********************************************************************
    1370             :  **                                                                **
    1371             :  **                           Base Conversion                      **
    1372             :  **                                                                **
    1373             :  ********************************************************************/
    1374             : 
    1375             : ulong *
    1376      535170 : convi(GEN x, long *l)
    1377             : {
    1378      535170 :   long n = nchar2nlong(2 + (long)(NLIMBS(x) * (BITS_IN_LONG * LOG10_2)));
    1379      535170 :   GEN str = cgetg(n+1, t_VECSMALL);
    1380      535170 :   unsigned char *res = (unsigned char*) GSTR(str);
    1381      535170 :   long llz = mpn_get_str(res, 10, LIMBS(icopy(x)), NLIMBS(x));
    1382             :   long lz;
    1383             :   ulong *z;
    1384             :   long i, j;
    1385             :   unsigned char *t;
    1386      535170 :   while (!*res) {res++; llz--;} /*Strip leading zeros*/
    1387      535170 :   lz  = (8+llz)/9;
    1388      535170 :   z = (ulong*)new_chunk(1+lz);
    1389      535170 :   t=res+llz+9;
    1390     1199972 :   for(i=0;i<llz-8;i+=9)
    1391             :   {
    1392             :     ulong s;
    1393      664802 :     t-=18;
    1394      664802 :     s=*t++;
    1395     5983218 :     for (j=1; j<9;j++)
    1396     5318416 :       s=10*s+*t++;
    1397      664802 :     *z++=s;
    1398             :   }
    1399      535170 :   if (i<llz)
    1400             :   {
    1401      526692 :     unsigned char *t = res;
    1402      526692 :     ulong s=*t++;
    1403     2134322 :     for (j=i+1; j<llz;j++)
    1404     1607630 :       s=10*s+*t++;
    1405      526692 :     *z++=s;
    1406             :   }
    1407      535170 :   *l = lz;
    1408      535170 :   return z;
    1409             : }

Generated by: LCOV version 1.13