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/gmp - mp.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23026-c0ccc8713) Lines: 650 719 90.4 %
Date: 2018-09-25 05:41:47 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      278403 : static void pari_gmp_free(void *ptr, size_t old_size){
      51      278403 :   (void)old_size; pari_free(ptr);
      52      278403 : }
      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         896 : pari_kernel_init(void)
      60             : {
      61         896 :   mp_get_memory_functions (&old_gmp_malloc, &old_gmp_realloc, &old_gmp_free);
      62         896 :   mp_set_memory_functions((void *(*)(size_t)) pari_malloc, pari_gmp_realloc, pari_gmp_free);
      63         896 : }
      64             : 
      65             : void
      66         888 : 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         888 :   mp_get_memory_functions (&new_gmp_malloc, &new_gmp_realloc, &new_gmp_free);
      72         888 :   if (new_gmp_malloc==pari_malloc) new_gmp_malloc = old_gmp_malloc;
      73         888 :   if (new_gmp_realloc==pari_gmp_realloc) new_gmp_realloc = old_gmp_realloc;
      74         888 :   if (new_gmp_free==pari_gmp_free) new_gmp_free = old_gmp_free;
      75         888 :   mp_set_memory_functions(new_gmp_malloc, new_gmp_realloc, new_gmp_free);
      76         888 : }
      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     1885893 : xmpn_copy(mp_limb_t *x, mp_limb_t *y, long n)
      86             : {
      87     1885893 :   while (--n >= 0) x[n]=y[n];
      88     1885893 : }
      89             : 
      90             : INLINE void
      91   180143949 : xmpn_mirror(mp_limb_t *x, long n)
      92             : {
      93             :   long i;
      94   952721243 :   for(i=0;i<(n>>1);i++)
      95             :   {
      96   772577294 :     ulong m=x[i];
      97   772577294 :     x[i]=x[n-1-i];
      98   772577294 :     x[n-1-i]=m;
      99             :   }
     100   180143949 : }
     101             : 
     102             : INLINE void
     103   246541096 : xmpn_mirrorcopy(mp_limb_t *z, mp_limb_t *x, long n)
     104             : {
     105             :   long i;
     106  2713917761 :   for(i=0;i<n;i++)
     107  2467376665 :     z[i]=x[n-1-i];
     108   246541096 : }
     109             : 
     110             : INLINE void
     111    82189312 : xmpn_zero(mp_ptr x, mp_size_t n)
     112             : {
     113    82189312 :   while (--n >= 0) x[n]=0;
     114    82189312 : }
     115             : 
     116             : INLINE GEN
     117    16084972 : icopy_ef(GEN x, long l)
     118             : {
     119    16084972 :   register long lx = lgefint(x);
     120    16084972 :   const GEN y = cgeti(l);
     121             : 
     122    16084917 :   while (--lx > 0) y[lx]=x[lx];
     123    16084917 :   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     2900417 : setloop(GEN a)
     141             : {
     142     2900417 :   pari_sp av = avma - 2 * sizeof(long);
     143     2900417 :   (void)cgetg(lgefint(a) + 3, t_VECSMALL);
     144     2900418 :   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    32304667 : incpos(GEN a)
     157             : {
     158    32304667 :   long i, l = lgefint(a);
     159    32304672 :   for (i=2; i<l; i++)
     160    32304668 :     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    32610608 : incloop(GEN a)
     194             : {
     195    32610608 :   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    32272712 :     default: return incpos(a);
     203             :   }
     204             : }
     205             : 
     206             : INLINE GEN
     207  1190947175 : adduispec(ulong s, GEN x, long nx)
     208             : {
     209             :   GEN  zd;
     210             :   long lz;
     211             : 
     212  1190947175 :   if (nx == 1) return adduu(uel(x,0), s);
     213   228387519 :   lz = nx+3; zd = cgeti(lz);
     214   227127081 :   if (mpn_add_1(LIMBS(zd),(mp_limb_t *)x,nx,s))
     215     4865100 :     zd[lz-1]=1;
     216             :   else
     217   222281968 :     lz--;
     218   227147068 :   zd[1] = evalsigne(1) | evallgefint(lz);
     219   227147068 :   return zd;
     220             : }
     221             : 
     222             : GEN
     223   250599659 : adduispec_offset(ulong s, GEN x, long offset, long nx)
     224             : {
     225   250599659 :   GEN xd=x+2+offset;
     226   250599659 :   while (nx && *(xd+nx-1)==0) nx--;
     227   250599659 :   if (!nx) return utoi(s);
     228   234191207 :   return adduispec(s,xd,nx);
     229             : }
     230             : 
     231             : INLINE GEN
     232  1627880187 : addiispec(GEN x, GEN y, long nx, long ny)
     233             : {
     234             :   GEN zd;
     235             :   long lz;
     236             : 
     237  1627880187 :   if (nx < ny) swapspec(x,y, nx,ny);
     238  1627880187 :   if (ny == 1) return adduispec(*y,x,nx);
     239   736647423 :   lz = nx+3; zd = cgeti(lz);
     240             : 
     241   732980571 :   if (mpn_add(LIMBS(zd),(mp_limb_t *)x,nx,(mp_limb_t *)y,ny))
     242    11296397 :     zd[lz-1]=1;
     243             :   else
     244   722322059 :     lz--;
     245             : 
     246   733618456 :   zd[1] = evalsigne(1) | evallgefint(lz);
     247   733618456 :   return zd;
     248             : }
     249             : 
     250             : /* assume x >= y */
     251             : INLINE GEN
     252   835670165 : subiuspec(GEN x, ulong s, long nx)
     253             : {
     254             :   GEN zd;
     255             :   long lz;
     256             : 
     257   835670165 :   if (nx == 1) return utoi(x[0] - s);
     258             : 
     259    83880627 :   lz = nx + 2; zd = cgeti(lz);
     260    83832451 :   mpn_sub_1 (LIMBS(zd), (mp_limb_t *)x, nx, s);
     261    83829511 :   if (! zd[lz - 1]) { --lz; }
     262             : 
     263    83829511 :   zd[1] = evalsigne(1) | evallgefint(lz);
     264    83829511 :   return zd;
     265             : }
     266             : 
     267             : /* assume x > y */
     268             : INLINE GEN
     269  1560095800 : subiispec(GEN x, GEN y, long nx, long ny)
     270             : {
     271             :   GEN zd;
     272             :   long lz;
     273  1560095800 :   if (ny==1) return subiuspec(x,*y,nx);
     274   734564024 :   lz = nx+2; zd = cgeti(lz);
     275             : 
     276   725468541 :   mpn_sub (LIMBS(zd), (mp_limb_t *)x, nx, (mp_limb_t *)y, ny);
     277   726301378 :   while (lz >= 3 && zd[lz - 1] == 0) { lz--; }
     278             : 
     279   726301378 :   zd[1] = evalsigne(1) | evallgefint(lz);
     280   726301378 :   return zd;
     281             : }
     282             : 
     283             : static void
     284   339168797 : roundr_up_ip(GEN x, long l)
     285             : {
     286   339168797 :   long i = l;
     287             :   for(;;)
     288             :   {
     289   342689903 :     if (++((ulong*)x)[--i]) break;
     290     1893372 :     if (i == 2) { x[2] = HIGHBIT; shiftr_inplace(x, 1); break; }
     291             :   }
     292   339168797 : }
     293             : 
     294             : void
     295   147505845 : affir(GEN x, GEN y)
     296             : {
     297   147505845 :   const long s = signe(x), ly = lg(y);
     298             :   long lx, sh, i;
     299             : 
     300   147505845 :   if (!s)
     301             :   {
     302     5164220 :     y[1] = evalexpo(-bit_accuracy(ly));
     303     5164220 :     return;
     304             :   }
     305   142341625 :   lx = lgefint(x); sh = bfffo(*int_MSW(x));
     306   142341625 :   y[1] = evalsigne(s) | evalexpo(bit_accuracy(lx)-sh-1);
     307   142341625 :   if (sh) {
     308   140564856 :     if (lx <= ly)
     309             :     {
     310    87486229 :       for (i=lx; i<ly; i++) y[i]=0;
     311    87486229 :       mpn_lshift(LIMBS(y),LIMBS(x),lx-2,sh);
     312    87486229 :       xmpn_mirror(LIMBS(y),lx-2);
     313    87486229 :       return;
     314             :     }
     315    53078627 :     mpn_lshift(LIMBS(y),LIMBS(x)+lx-ly,ly-2,sh);
     316    53078627 :     uel(y,2) |= uel(x,lx-ly+1) >> (BITS_IN_LONG-sh);
     317    53078627 :     xmpn_mirror(LIMBS(y),ly-2);
     318             :     /* lx > ly: round properly */
     319    53078627 :     if ((uel(x,lx-ly+1)<<sh) & HIGHBIT) roundr_up_ip(y, ly);
     320             :   }
     321             :   else {
     322     1776769 :     GEN xd=int_MSW(x);
     323     1776769 :     if (lx <= ly)
     324             :     {
     325      905634 :       for (i=2; i<lx; i++,xd=int_precW(xd)) y[i]=*xd;
     326      905634 :       for (   ; i<ly; i++) y[i]=0;
     327      905634 :       return;
     328             :     }
     329      871135 :     for (i=2; i<ly; i++,xd=int_precW(xd)) y[i]=*xd;
     330             :     /* lx > ly: round properly */
     331      871135 :     if (uel(x,lx-ly+1) & HIGHBIT) roundr_up_ip(y, ly);
     332             :   }
     333             : }
     334             : 
     335             : INLINE GEN
     336   391704276 : shiftispec(GEN x, long nx, long n)
     337             : {
     338             :   long ny,m;
     339             :   GEN yd, y;
     340             : 
     341   391704276 :   if (!n) return icopyspec(x, nx);
     342             : 
     343   382086855 :   if (n > 0)
     344             :   {
     345   240126022 :     long d = dvmdsBIL(n, &m);
     346             :     long i;
     347             : 
     348   240062302 :     ny = nx + d + (m!=0);
     349   240062302 :     y = cgeti(ny + 2); yd = y + 2;
     350   239543929 :     for (i=0; i<d; i++) yd[i] = 0;
     351             : 
     352   239543929 :     if (!m) xmpn_copy((mp_limb_t *) (yd + d), (mp_limb_t *) x, nx);
     353             :     else
     354             :     {
     355   238784002 :       ulong carryd = mpn_lshift((mp_limb_t *) (yd + d), (mp_limb_t *) x, nx, m);
     356   239110298 :       if (carryd) yd[ny - 1] = carryd;
     357   226808194 :       else ny--;
     358             :     }
     359             :   }
     360             :   else
     361             :   {
     362   141960833 :     long d = dvmdsBIL(-n, &m);
     363             : 
     364   141960520 :     ny = nx - d;
     365   141960520 :     if (ny < 1) return gen_0;
     366   141303104 :     y = cgeti(ny + 2); yd = y + 2;
     367             : 
     368   141301519 :     if (!m) xmpn_copy((mp_limb_t *) yd, (mp_limb_t *) (x + d), nx - d);
     369             :     else
     370             :     {
     371   140296453 :       mpn_rshift((mp_limb_t *) yd, (mp_limb_t *) (x + d), nx - d, m);
     372   140296465 :       if (yd[ny - 1] == 0)
     373             :       {
     374    15344080 :         if (ny == 1) { avma = (pari_sp)(yd + 1); return gen_0; }
     375    14051973 :         ny--;
     376             :       }
     377             :     }
     378             :   }
     379   379747503 :   y[1] = evalsigne(1)|evallgefint(ny + 2);
     380   379747503 :   return y;
     381             : }
     382             : 
     383             : GEN
     384    30419293 : mantissa2nr(GEN x, long n)
     385             : {
     386    30419293 :   long ly, i, m, s = signe(x), lx = lg(x);
     387             :   GEN y;
     388    30419293 :   if (!s) return gen_0;
     389    30417985 :   if (!n)
     390             :   {
     391    11535670 :     y = cgeti(lx);
     392    11535670 :     y[1] = evalsigne(s) | evallgefint(lx);
     393    11535670 :     xmpn_mirrorcopy(LIMBS(y),RLIMBS(x),lx-2);
     394    11535670 :     return y;
     395             :   }
     396    18882315 :   if (n > 0)
     397             :   {
     398      315076 :     GEN z = (GEN)avma;
     399      315076 :     long d = dvmdsBIL(n, &m);
     400             : 
     401      315076 :     ly = lx+d; y = new_chunk(ly);
     402      315076 :     for ( ; d; d--) *--z = 0;
     403      315076 :     if (!m) for (i=2; i<lx; i++) y[i]=x[i];
     404             :     else
     405             :     {
     406      314679 :       register const ulong sh = BITS_IN_LONG - m;
     407      314679 :       shift_left(y,x, 2,lx-1, 0,m);
     408      314679 :       i = uel(x,2) >> sh;
     409             :       /* Extend y on the left? */
     410      314679 :       if (i) { ly++; y = new_chunk(1); y[2] = i; }
     411             :     }
     412             :   }
     413             :   else
     414             :   {
     415    18567239 :     ly = lx - dvmdsBIL(-n, &m);
     416    18567239 :     if (ly<3) return gen_0;
     417    18567239 :     y = new_chunk(ly);
     418    18567239 :     if (m) {
     419    18524964 :       shift_right(y,x, 2,ly, 0,m);
     420    18524964 :       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       42275 :       for (i=2; i<ly; i++) y[i]=x[i];
     427             :     }
     428             :   }
     429    18882315 :   xmpn_mirror(LIMBS(y),ly-2);
     430    18882315 :   y[1] = evalsigne(s)|evallgefint(ly);
     431    18882315 :   y[0] = evaltyp(t_INT)|evallg(ly); return y;
     432             : }
     433             : 
     434             : GEN
     435     1944248 : truncr(GEN x)
     436             : {
     437             :   long s, e, d, m, i;
     438             :   GEN y;
     439     1944248 :   if ((s=signe(x)) == 0 || (e=expo(x)) < 0) return gen_0;
     440     1577381 :   d = nbits2lg(e+1); m = remsBIL(e);
     441     1577381 :   if (d > lg(x)) pari_err_PREC( "truncr (precision loss in truncation)");
     442             : 
     443     1577381 :   y=cgeti(d); y[1] = evalsigne(s) | evallgefint(d);
     444     1577381 :   if (++m == BITS_IN_LONG)
     445         147 :     for (i=2; i<d; i++) y[d-i+1]=x[i];
     446             :   else
     447             :   {
     448     1577234 :     GEN z=cgeti(d);
     449     1577234 :     for (i=2; i<d; i++) z[d-i+1]=x[i];
     450     1577234 :     mpn_rshift(LIMBS(y),LIMBS(z),d-2,BITS_IN_LONG-m);
     451     1577234 :     avma=(pari_sp)y;
     452             :   }
     453     1577381 :   return y;
     454             : }
     455             : 
     456             : /* integral part */
     457             : GEN
     458      737464 : floorr(GEN x)
     459             : {
     460             :   long e, d, m, i, lx;
     461             :   GEN y;
     462      737464 :   if (signe(x) >= 0) return truncr(x);
     463      245176 :   if ((e=expo(x)) < 0) return gen_m1;
     464       92331 :   d = nbits2lg(e+1); m = remsBIL(e);
     465       92331 :   lx=lg(x); if (d>lx) pari_err_PREC( "floorr (precision loss in truncation)");
     466       92331 :   y = cgeti(d+1);
     467       92331 :   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       92279 :     GEN z=cgeti(d);
     476       92279 :     for (i=2; i<d; i++) z[d-i+1]=x[i];
     477       92279 :     mpn_rshift(LIMBS(y),LIMBS(z),d-2,BITS_IN_LONG-m);
     478       92279 :     if (uel(x,d-1)<<m == 0)
     479             :     {
     480       21731 :       i=d; while (i<lx && !x[i]) i++;
     481       21731 :       if (i==lx) goto END;
     482             :     }
     483             :   }
     484       81116 :   if (mpn_add_1(LIMBS(y),LIMBS(y),d-2,1))
     485           0 :     y[d++]=1;
     486             : END:
     487       92331 :   y[1] = evalsigne(-1) | evallgefint(d);
     488       92331 :   return y;
     489             : }
     490             : 
     491             : INLINE int
     492  1962035974 : cmpiispec(GEN x, GEN y, long lx, long ly)
     493             : {
     494  1962035974 :   if (lx < ly) return -1;
     495  1874182685 :   if (lx > ly) return  1;
     496  1784112466 :   return mpn_cmp((mp_limb_t*)x,(mp_limb_t*)y, lx);
     497             : }
     498             : 
     499             : INLINE int
     500   119550833 : equaliispec(GEN x, GEN y, long lx, long ly)
     501             : {
     502   119550833 :   if (lx != ly) return 0;
     503   119548324 :   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  2424067336 : muluispec(ulong x, GEN y, long ny)
     514             : {
     515  2424067336 :   if (ny == 1)
     516  1875284851 :     return muluu(x, *y);
     517             :   else
     518             :   {
     519   548782485 :     long lz = ny+3;
     520   548782485 :     GEN z = cgeti(lz);
     521   546338730 :     ulong hi = mpn_mul_1 (LIMBS(z), (mp_limb_t *)y, ny, x);
     522   546442106 :     if (hi) { z[lz - 1] = hi; } else lz--;
     523   546442106 :     z[1] = evalsigne(1) | evallgefint(lz);
     524   546442106 :     return z;
     525             :   }
     526             : }
     527             : 
     528             : /* a + b*|y| */
     529             : GEN
     530      931451 : addumului(ulong a, ulong b, GEN y)
     531             : {
     532             :   GEN z;
     533             :   long i, lz;
     534             :   ulong hi;
     535      931451 :   if (!b || !signe(y)) return utoi(a);
     536      931368 :   lz = lgefint(y)+1;
     537      931368 :   z = cgeti(lz);
     538      931368 :   z[2]=a;
     539      931368 :   for(i=3;i<lz;i++) z[i]=0;
     540      931368 :   hi=mpn_addmul_1(LIMBS(z), LIMBS(y), NLIMBS(y), b);
     541      931368 :   if (hi) z[lz-1]=hi; else lz--;
     542      931368 :   z[1] = evalsigne(1) | evallgefint(lz);
     543      931368 :   avma=(pari_sp)z; return z;
     544             : }
     545             : 
     546             : /***********************************************************************/
     547             : /**                                                                   **/
     548             : /**                          DIVISION                                 **/
     549             : /**                                                                   **/
     550             : /***********************************************************************/
     551             : 
     552             : ulong
     553   971235477 : umodiu(GEN y, ulong x)
     554             : {
     555   971235477 :   long sy=signe(y);
     556             :   ulong hi;
     557   971235477 :   if (!x) pari_err_INV("umodiu",gen_0);
     558   971279239 :   if (!sy) return 0;
     559   793393640 :   hi = mpn_mod_1(LIMBS(y),NLIMBS(y),x);
     560   793406821 :   if (!hi) return 0;
     561   783709677 :   return (sy > 0)? hi: x - hi;
     562             : }
     563             : 
     564             : /* return |y| \/ x */
     565             : GEN
     566   141743463 : absdiviu_rem(GEN y, ulong x, ulong *rem)
     567             : {
     568             :   long ly;
     569             :   GEN z;
     570             : 
     571   141743463 :   if (!x) pari_err_INV("absdiviu_rem",gen_0);
     572   141743718 :   if (!signe(y)) { *rem = 0; return gen_0; }
     573             : 
     574   141194197 :   ly = lgefint(y);
     575   141194197 :   if (ly == 3 && (ulong)x > uel(y,2)) { *rem = uel(y,2); return gen_0; }
     576             : 
     577   141126587 :   z = cgeti(ly);
     578   141101216 :   *rem = mpn_divrem_1(LIMBS(z), 0, LIMBS(y), NLIMBS(y), x);
     579   141121760 :   if (z [ly - 1] == 0) ly--;
     580   141121760 :   z[1] = evallgefint(ly) | evalsigne(1);
     581   141121760 :   return z;
     582             : }
     583             : 
     584             : GEN
     585    45899640 : divis_rem(GEN y, long x, long *rem)
     586             : {
     587    45899640 :   long sy=signe(y),ly,s;
     588             :   GEN z;
     589             : 
     590    45899640 :   if (!x) pari_err_INV("divis_rem",gen_0);
     591    45899640 :   if (!sy) { *rem = 0; return gen_0; }
     592    33301809 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     593             : 
     594    33301809 :   ly = lgefint(y);
     595    33301809 :   if (ly == 3 && (ulong)x > uel(y,2)) { *rem = itos(y); return gen_0; }
     596             : 
     597    20363883 :   z = cgeti(ly);
     598    20363883 :   *rem = mpn_divrem_1(LIMBS(z), 0, LIMBS(y), NLIMBS(y), x);
     599    20363883 :   if (sy<0) *rem = -  *rem;
     600    20363883 :   if (z[ly - 1] == 0) ly--;
     601    20363883 :   z[1] = evallgefint(ly) | evalsigne(s);
     602    20363883 :   return z;
     603             : }
     604             : 
     605             : GEN
     606     1046921 : divis(GEN y, long x)
     607             : {
     608     1046921 :   long sy=signe(y),ly,s;
     609             :   GEN z;
     610             : 
     611     1046921 :   if (!x) pari_err_INV("divis",gen_0);
     612     1046921 :   if (!sy) return gen_0;
     613     1046921 :   if (x<0) { s = -sy; x = -x; } else s=sy;
     614             : 
     615     1046921 :   ly = lgefint(y);
     616     1046921 :   if (ly == 3 && (ulong)x > uel(y,2)) return gen_0;
     617             : 
     618     1046921 :   z = cgeti(ly);
     619     1046921 :   (void)mpn_divrem_1(LIMBS(z), 0, LIMBS(y), NLIMBS(y), x);
     620     1046921 :   if (z[ly - 1] == 0) ly--;
     621     1046921 :   z[1] = evallgefint(ly) | evalsigne(s);
     622     1046921 :   return z;
     623             : }
     624             : 
     625             : /* We keep llx bits of x and lly bits of y*/
     626             : static GEN
     627    50467671 : divrr_with_gmp(GEN x, GEN y)
     628             : {
     629    50467671 :   long lx=RNLIMBS(x),ly=RNLIMBS(y);
     630    50467671 :   long lw=minss(lx,ly);
     631    50467671 :   long lly=minss(lw+1,ly);
     632    50467671 :   GEN  w=cgetr(lw+2);
     633    50467671 :   long lu=lw+lly;
     634    50467671 :   long llx=minss(lu,lx);
     635    50467671 :   mp_limb_t *u=(mp_limb_t *)new_chunk(lu);
     636    50467671 :   mp_limb_t *z=(mp_limb_t *)new_chunk(lly);
     637             :   mp_limb_t *q,*r;
     638    50467671 :   long e=expo(x)-expo(y);
     639    50467671 :   long sx=signe(x),sy=signe(y);
     640    50467671 :   xmpn_mirrorcopy(z,RLIMBS(y),lly);
     641    50467671 :   xmpn_mirrorcopy(u+lu-llx,RLIMBS(x),llx);
     642    50467671 :   xmpn_zero(u,lu-llx);
     643    50467671 :   q = (mp_limb_t *)new_chunk(lw+1);
     644    50467671 :   r = (mp_limb_t *)new_chunk(lly);
     645             : 
     646    50467671 :   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    50467671 :   if (uel(r,lly-1) > (uel(z,lly-1)>>1))
     650    24977732 :     mpn_add_1(q,q,lw+1,1);
     651             : 
     652    50467671 :   xmpn_mirrorcopy(RLIMBS(w),q,lw);
     653             : 
     654    50467671 :   if (q[lw] == 0) e--;
     655    19466613 :   else if (q[lw] == 1) { shift_right(w,w, 2,lw+2, 1,1); }
     656          18 :   else { w[2] = HIGHBIT; e++; }
     657    50467671 :   if (sy < 0) sx = -sx;
     658    50467671 :   w[1] = evalsigne(sx) | evalexpo(e);
     659    50467671 :   avma=(pari_sp) w;
     660    50467671 :   return w;
     661             : }
     662             : 
     663             : /* We keep llx bits of x and lly bits of y*/
     664             : static GEN
     665     3749541 : divri_with_gmp(GEN x, GEN y)
     666             : {
     667     3749541 :   long llx=RNLIMBS(x),ly=NLIMBS(y);
     668     3749541 :   long lly=minss(llx+1,ly);
     669     3749541 :   GEN  w=cgetr(llx+2);
     670     3749541 :   long lu=llx+lly, ld=ly-lly;
     671     3749541 :   mp_limb_t *u=(mp_limb_t *)new_chunk(lu);
     672     3749541 :   mp_limb_t *z=(mp_limb_t *)new_chunk(lly);
     673             :   mp_limb_t *q,*r;
     674     3749541 :   long sh=bfffo(y[ly+1]);
     675     3749541 :   long e=expo(x)-expi(y);
     676     3749541 :   long sx=signe(x),sy=signe(y);
     677     3749541 :   if (sh) mpn_lshift(z,LIMBS(y)+ld,lly,sh);
     678      120899 :   else xmpn_copy(z,LIMBS(y)+ld,lly);
     679     3749541 :   xmpn_mirrorcopy(u+lu-llx,RLIMBS(x),llx);
     680     3749541 :   xmpn_zero(u,lu-llx);
     681     3749541 :   q = (mp_limb_t *)new_chunk(llx+1);
     682     3749541 :   r = (mp_limb_t *)new_chunk(lly);
     683             : 
     684     3749541 :   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     3749541 :   if (uel(r,lly-1) > (uel(z,lly-1)>>1))
     688     1559255 :     mpn_add_1(q,q,llx+1,1);
     689             : 
     690     3749541 :   xmpn_mirrorcopy(RLIMBS(w),q,llx);
     691             : 
     692     3749541 :   if (q[llx] == 0) e--;
     693     3078540 :   else if (q[llx] == 1) { shift_right(w,w, 2,llx+2, 1,1); }
     694           0 :   else { w[2] = HIGHBIT; e++; }
     695     3749541 :   if (sy < 0) sx = -sx;
     696     3749541 :   w[1] = evalsigne(sx) | evalexpo(e);
     697     3749541 :   avma=(pari_sp) w;
     698     3749541 :   return w;
     699             : }
     700             : 
     701             : GEN
     702    30666810 : divri(GEN x, GEN y)
     703             : {
     704    30666810 :   long  s = signe(y);
     705             : 
     706    30666810 :   if (!s) pari_err_INV("divri",gen_0);
     707    30666810 :   if (!signe(x)) return real_0_bit(expo(x) - expi(y));
     708    30512428 :   if (!is_bigint(y)) {
     709    26762887 :     GEN z = divru(x, y[2]);
     710    26762887 :     if (s < 0) togglesign(z);
     711    26762887 :     return z;
     712             :   }
     713     3749541 :   return divri_with_gmp(x,y);
     714             : }
     715             : 
     716             : GEN
     717   108560834 : divrr(GEN x, GEN y)
     718             : {
     719   108560834 :   long sx=signe(x), sy=signe(y), lx,ly,lr,e,i,j;
     720             :   ulong y0,y1;
     721             :   GEN r, r1;
     722             : 
     723   108560834 :   if (!sy) pari_err_INV("divrr",y);
     724   108560834 :   e = expo(x) - expo(y);
     725   108560834 :   if (!sx) return real_0_bit(e);
     726   106959804 :   if (sy<0) sx = -sx;
     727             : 
     728   106959804 :   lx=lg(x); ly=lg(y);
     729   106959804 :   if (ly==3)
     730             :   {
     731    56492133 :     ulong k = x[2], l = (lx>3)? x[3]: 0;
     732             :     LOCAL_HIREMAINDER;
     733    56492133 :     if (k < uel(y,2)) e--;
     734             :     else
     735             :     {
     736    26080564 :       l >>= 1; if (k&1) l |= HIGHBIT;
     737    26080564 :       k >>= 1;
     738             :     }
     739    56492133 :     hiremainder = k; k = divll(l,y[2]);
     740    56492133 :     if (hiremainder & HIGHBIT)
     741             :     {
     742    15651821 :       k++;
     743    15651821 :       if (!k) { k = HIGHBIT; e++; }
     744             :     }
     745    56492133 :     r = cgetr(3);
     746    56492133 :     r[1] = evalsigne(sx) | evalexpo(e);
     747    56492133 :     r[2] = k; return r;
     748             :   }
     749             : 
     750    50467671 :   if (ly>=DIVRR_GMP_LIMIT)
     751    50467671 :     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   843072690 : dvmdii(GEN x, GEN y, GEN *z)
     849             : {
     850   843072690 :   long sx=signe(x),sy=signe(y);
     851             :   long lx, ly, lq;
     852             :   pari_sp av;
     853             :   GEN r,q;
     854             : 
     855   843072690 :   if (!sy) pari_err_INV("dvmdii",y);
     856   846643753 :   if (!sx)
     857             :   {
     858    22528614 :     if (!z || z == ONLY_REM) return gen_0;
     859     8387592 :     *z=gen_0; return gen_0;
     860             :   }
     861   824115139 :   lx=lgefint(x);
     862   824115139 :   ly=lgefint(y); lq=lx-ly;
     863   824115139 :   if (lq <= 0)
     864             :   {
     865   509343465 :     if (lq == 0)
     866             :     {
     867   487283270 :       long s=mpn_cmp(LIMBS(x),LIMBS(y),NLIMBS(x));
     868   489342546 :       if (s>0) goto DIVIDE;
     869   178705375 :       if (s==0)
     870             :       {
     871     9311343 :         if (z == ONLY_REM) return gen_0;
     872     4156239 :         if (z) *z = gen_0;
     873     4156239 :         if (sx < 0) sy = -sy;
     874     4156239 :         return stoi(sy);
     875             :       }
     876             :     }
     877   191454227 :     if (z == ONLY_REM) return icopy(x);
     878     7553173 :     if (z) *z = icopy(x);
     879     7553173 :     return gen_0;
     880             :   }
     881             : DIVIDE: /* quotient is non-zero */
     882   625408845 :   av=avma; if (sx<0) sy = -sy;
     883   625408845 :   if (ly==3)
     884             :   {
     885   267526939 :     ulong lq = lx;
     886             :     ulong si;
     887   267526939 :     q  = cgeti(lq);
     888   267331367 :     si = mpn_divrem_1(LIMBS(q), 0, LIMBS(x), NLIMBS(x), y[2]);
     889   267360675 :     if (q[lq - 1] == 0) lq--;
     890   267360675 :     if (z == ONLY_REM)
     891             :     {
     892   221991958 :       set_avma(av); if (!si) return gen_0;
     893   195274969 :       r=cgeti(3);
     894   195204434 :       r[1] = evalsigne(sx) | evallgefint(3);
     895   195204434 :       r[2] = si; return r;
     896             :     }
     897    45368717 :     q[1] = evalsigne(sy) | evallgefint(lq);
     898    45368717 :     if (!z) return q;
     899    41639773 :     if (!si) { *z=gen_0; return q; }
     900    18185658 :     r=cgeti(3);
     901    18185638 :     r[1] = evalsigne(sx) | evallgefint(3);
     902    18185638 :     r[2] = si; *z=r; return q;
     903             :   }
     904   357881906 :   if (z == ONLY_REM)
     905             :   {
     906   350761107 :     ulong lr = lgefint(y);
     907   350761107 :     ulong lq = lgefint(x)-lgefint(y)+3;
     908   350761107 :     GEN r = cgeti(lr);
     909   341779132 :     GEN q = cgeti(lq);
     910   337620185 :     mpn_tdiv_qr(LIMBS(q), LIMBS(r),0, LIMBS(x), NLIMBS(x), LIMBS(y), NLIMBS(y));
     911   358243694 :     if (!r[lr - 1])
     912             :     {
     913    88735635 :       while(lr>2 && !r[lr - 1]) lr--;
     914    88735635 :       if (lr == 2) {set_avma(av); return gen_0;} /* exact division */
     915             :     }
     916   352955731 :     r[1] = evalsigne(sx) | evallgefint(lr);
     917   352955731 :     avma = (pari_sp) r; return r;
     918             :   }
     919             :   else
     920             :   {
     921     7120799 :     ulong lq = lgefint(x)-lgefint(y)+3;
     922     7120799 :     ulong lr = lgefint(y);
     923     7120799 :     GEN q = cgeti(lq);
     924     7120799 :     GEN r = cgeti(lr);
     925     7120799 :     mpn_tdiv_qr(LIMBS(q), LIMBS(r),0, LIMBS(x), NLIMBS(x), LIMBS(y), NLIMBS(y));
     926     7120799 :     if (q[lq - 1] == 0) lq--;
     927     7120799 :     q[1] = evalsigne(sy) | evallgefint(lq);
     928     7120799 :     if (!z) { avma = (pari_sp)q; return q; }
     929     6486172 :     if (!r[lr - 1])
     930             :     {
     931     2216061 :       while(lr>2 && !r[lr - 1]) lr--;
     932     2216061 :       if (lr == 2) {avma=(pari_sp) q; *z=gen_0; return q;} /* exact division */
     933             :     }
     934     4779537 :     r[1] = evalsigne(sx) | evallgefint(lr);
     935     4779537 :     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    10780612 : red_montgomery(GEN T, GEN N, ulong inv)
     945             : {
     946             :   pari_sp av;
     947             :   GEN Te, Td, Ne, Nd, scratch;
     948    10780612 :   ulong i, j, m, t, d, k = NLIMBS(N);
     949             :   int carry;
     950             :   LOCAL_HIREMAINDER;
     951             :   LOCAL_OVERFLOW;
     952             : 
     953    10780612 :   if (k == 0) return gen_0;
     954    10780612 :   d = NLIMBS(T); /* <= 2*k */
     955    10780612 :   if (d == 0) return gen_0;
     956             : #ifdef DEBUG
     957             :   if (d > 2*k) pari_err_BUG("red_montgomery");
     958             : #endif
     959    10780603 :   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    10780111 :   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    10761206 :   Td = scratch;
     981    10761206 :   Te = T + 2;
     982    10761206 :   for (i=0; i < d     ; i++) *Td++ = *Te++;
     983    10761206 :   for (   ; i < (k<<1); i++) *Td++ = 0;
     984             : 
     985    10761206 :   Te = scratch - 1; /* 1 beyond end of current T mantissa (in scratch) */
     986    10761206 :   Ne = N + 1;       /* 1 beyond end of N mantissa */
     987             : 
     988    10761206 :   carry = 0;
     989    81748556 :   for (i=0; i<k; i++) /* set T := T/B nod N, k times */
     990             :   {
     991    70987350 :     Td = Te; /* one beyond end of (new) T mantissa */
     992    70987350 :     Nd = Ne;
     993    70987350 :     hiremainder = *++Td;
     994    70987350 :     m = hiremainder * inv; /* solve T + m N = O(B) */
     995             : 
     996             :     /* set T := (T + mN) / B */
     997    70987350 :     Te = Td;
     998    70987350 :     (void)addmul(m, *++Nd); /* = 0 */
     999   685071534 :     for (j=1; j<k; j++)
    1000             :     {
    1001   614084184 :       t = addll(addmul(m, *++Nd), *++Td);
    1002   614084184 :       *Td = t;
    1003   614084184 :       hiremainder += overflow;
    1004             :     }
    1005    70987350 :     t = addll(hiremainder, *++Td); *Td = t + carry;
    1006    70987350 :     carry = (overflow || (carry && *Td == 0));
    1007             :   }
    1008    10761206 :   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    10761206 :   Td = (GEN)av - 1; /* *Td = high word of final result */
    1019    10761206 :   while (*Td == 0 && Te < Td) Td--; /* strip leading 0s */
    1020    10761206 :   k = Td - Te; if (!k) { set_avma(av); return gen_0; }
    1021    10761206 :   Td = (GEN)av - k; /* will write mantissa there */
    1022    10761206 :   (void)memmove(Td, Te+1, k*sizeof(long));
    1023    10761206 :   Td -= 2;
    1024    10761206 :   Td[0] = evaltyp(t_INT) | evallg(k+2);
    1025    10751877 :   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    10751877 :   avma = (pari_sp)Td; return Td;
    1037             : }
    1038             : 
    1039             : /* EXACT INTEGER DIVISION */
    1040             : 
    1041             : /* use undocumented GMP interface */
    1042             : static void
    1043    37657332 : GEN2mpz(mpz_t X, GEN x)
    1044             : {
    1045    37657332 :   long l = lgefint(x)-2;
    1046    37657332 :   X->_mp_alloc = l;
    1047    37657332 :   X->_mp_size = signe(x) > 0? l: -l;
    1048    37657332 :   X->_mp_d = LIMBS(x);
    1049    37657332 : }
    1050             : static void
    1051    18828666 : mpz2GEN(GEN z, mpz_t Z)
    1052             : {
    1053    18828666 :   long l = Z->_mp_size;
    1054    18828666 :   z[1] = evalsigne(l > 0? 1: -1) | evallgefint(labs(l)+2);
    1055    18828666 : }
    1056             : 
    1057             : #ifdef mpn_divexact_1
    1058             : static GEN
    1059   271985989 : diviuexact_i(GEN x, ulong y)
    1060             : {
    1061   271985989 :   long l = lgefint(x);
    1062   271985989 :   GEN z = cgeti(l);
    1063   271973795 :   mpn_divexact_1(LIMBS(z), LIMBS(x), NLIMBS(x), y);
    1064   271974366 :   if (z[l-1] == 0) l--;
    1065   271974366 :   z[1] = evallgefint(l) | evalsigne(signe(x));
    1066   271974366 :   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    34886253 : diviuexact(GEN x, ulong y)
    1095             : {
    1096             :   GEN z;
    1097    34886253 :   if (!signe(x)) return gen_0;
    1098    34621729 :   z = diviuexact_i(x, y);
    1099    34621739 :   if (lgefint(z) == 2) pari_err_OP("exact division", x, utoi(y));
    1100    34621736 :   return z;
    1101             : }
    1102             : 
    1103             : /* Find z such that x=y*z, knowing that y | x (unchecked) */
    1104             : GEN
    1105   311472886 : diviiexact(GEN x, GEN y)
    1106             : {
    1107             :   GEN z;
    1108   311472886 :   if (!signe(y)) pari_err_INV("diviiexact",y);
    1109   311486609 :   if (!signe(x)) return gen_0;
    1110   256185772 :   if (lgefint(y) == 3)
    1111             :   {
    1112   237357106 :     z = diviuexact_i(x, y[2]);
    1113   237352186 :     if (signe(y) < 0) togglesign(z);
    1114             :   }
    1115             :   else
    1116             :   {
    1117    18828666 :     long l = lgefint(x);
    1118             :     mpz_t X, Y, Z;
    1119    18828666 :     z = cgeti(l);
    1120    18828666 :     GEN2mpz(X, x);
    1121    18828666 :     GEN2mpz(Y, y);
    1122    18828666 :     Z->_mp_alloc = l-2;
    1123    18828666 :     Z->_mp_size  = l-2;
    1124    18828666 :     Z->_mp_d = LIMBS(z);
    1125    18828666 :     mpz_divexact(Z, X, Y);
    1126    18828666 :     mpz2GEN(z, Z);
    1127             :   }
    1128   256180925 :   if (lgefint(z) == 2) pari_err_OP("exact division", x, y);
    1129   256178733 :   return z;
    1130             : }
    1131             : 
    1132             : /* assume yz != and yz | x */
    1133             : GEN
    1134      618647 : diviuuexact(GEN x, ulong y, ulong z)
    1135             : {
    1136             :   long tmp[4];
    1137             :   ulong t;
    1138             :   LOCAL_HIREMAINDER;
    1139      618647 :   t = mulll(y, z);
    1140      618647 :   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  2243267729 : muliispec(GEN x, GEN y, long nx, long ny)
    1158             : {
    1159             :   GEN zd;
    1160             :   long lz;
    1161             :   ulong hi;
    1162             : 
    1163  2243267729 :   if (nx < ny) swapspec(x,y, nx,ny);
    1164  2243267729 :   if (!ny) return gen_0;
    1165  2243267729 :   if (ny == 1) return muluispec((ulong)*y, x, nx);
    1166             : 
    1167   526532583 :   lz = nx+ny+2;
    1168   526532583 :   zd = cgeti(lz);
    1169   523170586 :   hi = mpn_mul(LIMBS(zd), (mp_limb_t *)x, nx, (mp_limb_t *)y, ny);
    1170   529735346 :   if (!hi) lz--;
    1171             :   /*else zd[lz-1]=hi; GH tell me it is not necessary.*/
    1172             : 
    1173   529735346 :   zd[1] = evalsigne(1) | evallgefint(lz);
    1174   529735346 :   return zd;
    1175             : }
    1176             : GEN
    1177      640855 : muluui(ulong x, ulong y, GEN z)
    1178             : {
    1179      640855 :   long t, s = signe(z);
    1180             :   GEN r;
    1181             :   LOCAL_HIREMAINDER;
    1182             : 
    1183      640855 :   if (!x || !y || !signe(z)) return gen_0;
    1184      640459 :   t = mulll(x,y);
    1185      640459 :   if (!hiremainder)
    1186      640459 :     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      640459 :   setsigne(r,s); return r;
    1195             : }
    1196             : 
    1197             : GEN
    1198   752686223 : sqrispec(GEN x, long nx)
    1199             : {
    1200             :   GEN zd;
    1201             :   long lz;
    1202             : 
    1203   752686223 :   if (!nx) return gen_0;
    1204   213342051 :   if (nx==1) return sqru(*x);
    1205             : 
    1206   141123485 :   lz = (nx<<1)+2;
    1207   141123485 :   zd = cgeti(lz);
    1208             : #ifdef mpn_sqr
    1209   136699487 :   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   138411667 :   if (zd[lz-1]==0) lz--;
    1214             : 
    1215   138411667 :   zd[1] = evalsigne(1) | evallgefint(lz);
    1216   138411667 :   return zd;
    1217             : }
    1218             : 
    1219             : INLINE GEN
    1220     6888435 : sqrispec_mirror(GEN x, long nx)
    1221             : {
    1222     6888435 :   GEN cx=new_chunk(nx);
    1223             :   GEN z;
    1224     6888435 :   xmpn_mirrorcopy((mp_limb_t *)cx,(mp_limb_t *)x,nx);
    1225     6888435 :   z=sqrispec(cx, nx);
    1226     6888435 :   xmpn_mirror(LIMBS(z), NLIMBS(z));
    1227     6888435 :   return z;
    1228             : }
    1229             : 
    1230             : /* leaves garbage on the stack. */
    1231             : INLINE GEN
    1232    13808343 : muliispec_mirror(GEN x, GEN y, long nx, long ny)
    1233             : {
    1234             :   GEN cx, cy, z;
    1235    13808343 :   long s = 0;
    1236    13808343 :   while (nx && x[nx-1]==0) { nx--; s++; }
    1237    13808343 :   while (ny && y[ny-1]==0) { ny--; s++; }
    1238    13808343 :   cx=new_chunk(nx); cy=new_chunk(ny);
    1239    13808343 :   xmpn_mirrorcopy((mp_limb_t *)cx,(mp_limb_t *)x,nx);
    1240    13808343 :   xmpn_mirrorcopy((mp_limb_t *)cy,(mp_limb_t *)y,ny);
    1241    13808343 :   z =  nx>=ny ? muliispec(cx, cy, nx, ny): muliispec(cy, cx, ny, nx);
    1242    13808343 :   if (s)
    1243             :   {
    1244     1962345 :     long i, lz = lgefint(z) + s;
    1245     1962345 :     (void)new_chunk(s);
    1246     1962345 :     z -= s;
    1247     1962345 :     for (i=0; i<s; i++) z[2+i]=0;
    1248     1962345 :     z[1] = evalsigne(1) | evallgefint(lz);
    1249     1962345 :     z[0] = evaltyp(t_INT) | evallg(lz);
    1250             :   }
    1251    13808343 :   xmpn_mirror(LIMBS(z), NLIMBS(z));
    1252    13808343 :   return z;
    1253             : }
    1254             : 
    1255             : /* x % (2^n), assuming n >= 0 */
    1256             : GEN
    1257    17102975 : remi2n(GEN x, long n)
    1258             : {
    1259             :   ulong hi;
    1260    17102975 :   long l, k, lx, ly, sx = signe(x);
    1261             :   GEN z, xd, zd;
    1262             : 
    1263    17102975 :   if (!sx || !n) return gen_0;
    1264             : 
    1265    16947003 :   k = dvmdsBIL(n, &l);
    1266    17486871 :   lx = lgefint(x);
    1267    17486871 :   if (lx < k+3) return icopy(x);
    1268             : 
    1269    17179321 :   xd = x + (2 + k);
    1270             :   /* x = |k|...|1|#|... : copy the last l bits of # and the first k words
    1271             :    *              ^--- initial xd  */
    1272    17179321 :   hi = ((ulong)*xd) & ((1UL<<l)-1); /* last l bits of # = top bits of result */
    1273    17179321 :   if (!hi)
    1274             :   { /* strip leading zeroes from result */
    1275     1766384 :     xd--; while (k && !*xd) { k--; xd--; }
    1276     1766384 :     if (!k) return gen_0;
    1277     1010804 :     ly = k+2;
    1278             :   }
    1279             :   else
    1280    15412937 :     ly = k+3;
    1281             : 
    1282    16423741 :   zd = z = cgeti(ly);
    1283    16035891 :   *++zd = evalsigne(sx) | evallgefint(ly);
    1284    16035891 :   xd = x+1; for ( ;k; k--) *++zd = *++xd;
    1285    16035891 :   if (hi) *++zd = hi;
    1286    16035891 :   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     3604798 : sqrtremi(GEN a, GEN *r)
    1300             : {
    1301     3604798 :   long l, na = NLIMBS(a);
    1302             :   mp_size_t nr;
    1303             :   GEN S;
    1304     3604798 :   if (!na) {
    1305           8 :     if (r) *r = gen_0;
    1306           8 :     return gen_0;
    1307             :   }
    1308     3604790 :   l = (na + 5) >> 1; /* 2 + ceil(na/2) */
    1309     3604790 :   S = cgetipos(l);
    1310     3604790 :   if (r) {
    1311      348251 :     GEN R = cgeti(2 + na);
    1312      348251 :     nr = mpn_sqrtrem(LIMBS(S), LIMBS(R), LIMBS(a), na);
    1313      348251 :     if (nr) R[1] = evalsigne(1) | evallgefint(nr+2);
    1314       10513 :     else    { avma = (pari_sp)S; R = gen_0; }
    1315      348251 :     *r = R;
    1316             :   }
    1317             :   else
    1318     3256539 :     (void)mpn_sqrtrem(LIMBS(S), NULL, LIMBS(a), na);
    1319     3604790 :   return S;
    1320             : }
    1321             : 
    1322             : /* compute sqrt(|a|), assuming a != 0 */
    1323             : GEN
    1324    27972100 : sqrtr_abs(GEN a)
    1325             : {
    1326             :   GEN res;
    1327             :   mp_limb_t *b, *c;
    1328    27972100 :   long l = RNLIMBS(a), e = expo(a), er = e>>1;
    1329             :   long n;
    1330    27972100 :   res = cgetr(2 + l);
    1331    27972100 :   res[1] = evalsigne(1) | evalexpo(er);
    1332    27972100 :   if (e&1)
    1333             :   {
    1334    13626110 :     b = (mp_limb_t *) new_chunk(l<<1);
    1335    13626110 :     xmpn_zero(b,l);
    1336    13626110 :     xmpn_mirrorcopy(b+l, RLIMBS(a), l);
    1337    13626110 :     c = (mp_limb_t *) new_chunk(l);
    1338    13626110 :     n = mpn_sqrtrem(c,b,b,l<<1); /* c <- sqrt; b <- rem */
    1339    13626110 :     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    14345990 :     b = (mp_limb_t *) mantissa2nr(a,-1);
    1345    14345990 :     b[1] = uel(a,l+1)<<(BITS_IN_LONG-1);
    1346    14345990 :     b = (mp_limb_t *) new_chunk(l);
    1347    14345990 :     xmpn_zero(b,l+1); /* overwrites the former b[0] */
    1348    14345990 :     c = (mp_limb_t *) new_chunk(l + 1);
    1349    14345990 :     n = mpn_sqrtrem(c,b,b,(l<<1)+2); /* c <- sqrt; b <- rem */
    1350    14345990 :     u = (ulong)*c++;
    1351    14345990 :     if ( u&HIGHBIT || (u == ~HIGHBIT &&
    1352           0 :              (n>l || (n==l && mpn_cmp(b,c,l)>0))))
    1353     7010666 :       mpn_add_1(c,c,l,1);
    1354             :   }
    1355    27972100 :   xmpn_mirrorcopy(RLIMBS(res),c,l);
    1356    27972100 :   avma = (pari_sp)res; return res;
    1357             : }
    1358             : 
    1359             : /* Normalize a non-negative integer */
    1360             : GEN
    1361    90242050 : int_normalize(GEN x, long known_zero_words)
    1362             : {
    1363    90242050 :   long i =  lgefint(x) - 1 - known_zero_words;
    1364  1049921561 :   for ( ; i > 1; i--)
    1365  1018142164 :     if (x[i]) { setlgefint(x, i+1); return x; }
    1366    31779397 :   x[1] = evalsigne(0) | evallgefint(2); return x;
    1367             : }
    1368             : 
    1369             : /********************************************************************
    1370             :  **                                                                **
    1371             :  **                           Base Conversion                      **
    1372             :  **                                                                **
    1373             :  ********************************************************************/
    1374             : 
    1375             : ulong *
    1376      535840 : convi(GEN x, long *l)
    1377             : {
    1378      535840 :   long n = nchar2nlong(2 + (long)(NLIMBS(x) * (BITS_IN_LONG * LOG10_2)));
    1379      535840 :   GEN str = cgetg(n+1, t_VECSMALL);
    1380      535840 :   unsigned char *res = (unsigned char*) GSTR(str);
    1381      535840 :   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      535840 :   while (!*res) {res++; llz--;} /*Strip leading zeros*/
    1387      535840 :   lz  = (8+llz)/9;
    1388      535840 :   z = (ulong*)new_chunk(1+lz);
    1389      535840 :   t=res+llz+9;
    1390     1201434 :   for(i=0;i<llz-8;i+=9)
    1391             :   {
    1392             :     ulong s;
    1393      665594 :     t-=18;
    1394      665594 :     s=*t++;
    1395     5990346 :     for (j=1; j<9;j++)
    1396     5324752 :       s=10*s+*t++;
    1397      665594 :     *z++=s;
    1398             :   }
    1399      535840 :   if (i<llz)
    1400             :   {
    1401      527362 :     unsigned char *t = res;
    1402      527362 :     ulong s=*t++;
    1403     2135730 :     for (j=i+1; j<llz;j++)
    1404     1608368 :       s=10*s+*t++;
    1405      527362 :     *z++=s;
    1406             :   }
    1407      535840 :   *l = lz;
    1408      535840 :   return z;
    1409             : }

Generated by: LCOV version 1.13