Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - kernel/none - mp.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25819-e703fe1174) Lines: 1121 1153 97.2 %
Date: 2020-09-18 06:10:04 Functions: 70 70 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #line 2 "../src/kernel/none/mp.c"
       2             : /* Copyright (C) 2000-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             : /**                       MULTIPRECISION KERNEL                       **/
      18             : /**                                                                   **/
      19             : /***********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : #include "../src/kernel/none/tune-gen.h"
      23             : 
      24             : void
      25         698 : pari_kernel_init(void) { }
      26             : void
      27         696 : pari_kernel_close(void) { }
      28             : const char *
      29           2 : pari_kernel_version(void) { return ""; }
      30             : 
      31             : /* NOTE: arguments of "spec" routines (muliispec, addiispec, etc.) aren't
      32             :  * GENs but pairs (long *a, long na) representing a list of digits (in basis
      33             :  * BITS_IN_LONG) : a[0], ..., a[na-1]. [ In ordre to facilitate splitting: no
      34             :  * need to reintroduce codewords ] */
      35             : 
      36             : #define LIMBS(x)  ((x)+2)
      37             : #define NLIMBS(x) (lgefint(x)-2)
      38             : 
      39             : /* Normalize a non-negative integer */
      40             : GEN
      41   228060762 : int_normalize(GEN x, long known_zero_words)
      42             : {
      43   228060762 :   long i, lx = lgefint(x);
      44             :   GEN x0;
      45   228060762 :   if (lx == 2) { x[1] = evalsigne(0) | evallgefint(2); return x; }
      46   228060762 :   if (!known_zero_words && x[2]) return x;
      47   936401922 :   for (i = 2+known_zero_words; i < lx; i++)
      48   910316127 :     if (x[i]) break;
      49    94355433 :   x0 = x; i -= 2; x += i;
      50    94355433 :   if (x0 == (GEN)avma) set_avma((pari_sp)x);
      51    48470532 :   else stackdummy((pari_sp)(x0+i), (pari_sp)x0);
      52    94355433 :   lx -= i;
      53    94355433 :   x[0] = evaltyp(t_INT) | evallg(lx);
      54    94355433 :   if (lx == 2) x[1] = evalsigne(0) | evallgefint(lx);
      55    68269638 :   else         x[1] = evalsigne(1) | evallgefint(lx);
      56    94355433 :   return x;
      57             : }
      58             : 
      59             : /***********************************************************************/
      60             : /**                                                                   **/
      61             : /**                      ADDITION / SUBTRACTION                       **/
      62             : /**                                                                   **/
      63             : /***********************************************************************/
      64             : 
      65             : GEN
      66     2234898 : setloop(GEN a)
      67             : {
      68     2234898 :   pari_sp av = avma;
      69     2234898 :   (void)cgetg(lgefint(a) + 3, t_VECSMALL);
      70     2234898 :   return icopy_avma(a, av); /* two cells of extra space before a */
      71             : }
      72             : 
      73             : /* we had a = setloop(?), then some incloops. Reset a to b */
      74             : GEN
      75      128046 : resetloop(GEN a, GEN b) {
      76      128046 :   long lb = lgefint(b);
      77      128046 :   a += lgefint(a) - lb;
      78      128046 :   a[0] = evaltyp(t_INT) | evallg(lb);
      79      128046 :   affii(b, a); return a;
      80             : }
      81             : 
      82             : /* assume a > 0, initialized by setloop. Do a++ */
      83             : static GEN
      84    20333814 : incpos(GEN a)
      85             : {
      86    20333814 :   long i, l = lgefint(a);
      87    20333817 :   for (i=l-1; i>1; i--)
      88    20333814 :     if (++a[i]) return a;
      89           3 :   l++; a--; /* use extra cell */
      90           3 :   a[0]=evaltyp(t_INT) | _evallg(l);
      91           3 :   a[1]=evalsigne(1) | evallgefint(l);
      92           3 :   a[2]=1; return a;
      93             : }
      94             : 
      95             : /* assume a < 0, initialized by setloop. Do a++ */
      96             : static GEN
      97        8214 : incneg(GEN a)
      98             : {
      99        8214 :   long i, l = lgefint(a)-1;
     100        8214 :   if (uel(a,l)--)
     101             :   {
     102        8211 :     if (l == 2 && !a[2])
     103             :     {
     104         453 :       a++; /* save one cell */
     105         453 :       a[0] = evaltyp(t_INT) | _evallg(2);
     106         453 :       a[1] = evalsigne(0) | evallgefint(2);
     107             :     }
     108        8211 :     return a;
     109             :   }
     110           3 :   for (i = l-1;; i--) /* finishes since a[2] != 0 */
     111           3 :     if (uel(a,i)--) break;
     112           3 :   if (!a[2])
     113             :   {
     114           3 :     a++; /* save one cell */
     115           3 :     a[0] = evaltyp(t_INT) | _evallg(l);
     116           3 :     a[1] = evalsigne(-1) | evallgefint(l);
     117             :   }
     118           3 :   return a;
     119             : }
     120             : 
     121             : /* assume a initialized by setloop. Do a++ */
     122             : GEN
     123    20590191 : incloop(GEN a)
     124             : {
     125    20590191 :   switch(signe(a))
     126             :   {
     127      248163 :     case 0: a--; /* use extra cell */
     128      248163 :       a[0]=evaltyp(t_INT) | _evallg(3);
     129      248163 :       a[1]=evalsigne(1) | evallgefint(3);
     130      248163 :       a[2]=1; return a;
     131        8214 :     case -1: return incneg(a);
     132    20333814 :     default: return incpos(a);
     133             :   }
     134             : }
     135             : 
     136             : INLINE GEN
     137  1178528541 : adduispec(ulong s, GEN x, long nx)
     138             : {
     139  1178528541 :   GEN xd, zd = (GEN)avma;
     140             :   long lz;
     141             : 
     142  1178528541 :   if (nx == 1) return adduu(s, uel(x,0));
     143   139876056 :   lz = nx+3; (void)new_chunk(lz);
     144   139876056 :   xd = x + nx;
     145   139876056 :   *--zd = (ulong)*--xd + s;
     146   139876056 :   if ((ulong)*zd < s)
     147             :     for(;;)
     148             :     {
     149    18622623 :       if (xd == x) { *--zd = 1; break; } /* enlarge z */
     150    14926056 :       *--zd = ((ulong)*--xd) + 1;
     151    14926056 :       if (*zd) { lz--; break; }
     152             :     }
     153   127609110 :   else lz--;
     154   574996311 :   while (xd > x) *--zd = *--xd;
     155   139876056 :   *--zd = evalsigne(1) | evallgefint(lz);
     156   139876056 :   *--zd = evaltyp(t_INT) | evallg(lz);
     157   139876056 :   return gc_const((pari_sp)zd, zd);
     158             : }
     159             : 
     160             : GEN
     161   178239594 : adduispec_offset(ulong s, GEN x, long offset, long nx)
     162             : {
     163   178239594 :   GEN xd = x+lgefint(x)-nx-offset;
     164   232027512 :   while (nx && *xd==0) {xd++; nx--;}
     165   178239594 :   if (!nx) return utoi(s);
     166   162657453 :   return adduispec(s,xd,nx);
     167             : }
     168             : 
     169             : static GEN
     170  2425280298 : addiispec(GEN x, GEN y, long nx, long ny)
     171             : {
     172             :   GEN xd, yd, zd;
     173  2425280298 :   long lz, i = -2;
     174             :   LOCAL_OVERFLOW;
     175             : 
     176  2425280298 :   if (nx < ny) swapspec(x,y, nx,ny);
     177  2425280298 :   if (ny == 1) return adduispec(*y,x,nx);
     178  1463131242 :   zd = (GEN)avma;
     179  1463131242 :   lz = nx+3; (void)new_chunk(lz);
     180  1463131242 :   xd = x + nx;
     181  1463131242 :   yd = y + ny;
     182  1463131242 :   zd[-1] = addll(xd[-1], yd[-1]);
     183             : #ifdef addllx8
     184   930181903 :   for (  ; i-8 > -ny; i-=8)
     185   442471489 :     addllx8(xd+i, yd+i, zd+i, overflow);
     186             : #endif
     187 14212611247 :   for (  ; i >= -ny; i--) zd[i] = addllx(xd[i], yd[i]);
     188  1463131242 :   if (overflow)
     189             :     for(;;)
     190             :     {
     191   165415113 :       if (i < -nx) { zd[i] = 1; i--; break; } /* enlarge z */
     192   117008661 :       zd[i] = uel(xd,i) + 1;
     193   117008661 :       if (zd[i]) { i--; lz--; break; }
     194    39929751 :       i--;
     195             :     }
     196  1337645880 :   else lz--;
     197  6093521130 :   for (; i >= -nx; i--) zd[i] = xd[i];
     198  1463131242 :   zd += i+1;
     199  1463131242 :   *--zd = evalsigne(1) | evallgefint(lz);
     200  1463131242 :   *--zd = evaltyp(t_INT) | evallg(lz);
     201  1463131242 :   return gc_const((pari_sp)zd, zd);
     202             : }
     203             : 
     204             : /* assume x >= s */
     205             : INLINE GEN
     206   938693344 : subiuspec(GEN x, ulong s, long nx)
     207             : {
     208   938693344 :   GEN xd, zd = (GEN)avma;
     209             :   long lz;
     210             :   LOCAL_OVERFLOW;
     211             : 
     212   938693344 :   if (nx == 1) return utoi(x[0] - s);
     213             : 
     214   108261565 :   lz = nx+2; (void)new_chunk(lz);
     215   108261565 :   xd = x + nx;
     216   108261565 :   *--zd = subll(*--xd, s);
     217   108261565 :   if (overflow)
     218             :     for(;;)
     219             :     {
     220     3964854 :       *--zd = ((ulong)*--xd) - 1;
     221    41742210 :       if (*xd) break;
     222             :     }
     223   108261565 :   if (xd == x)
     224    62202897 :     while (*zd == 0) { zd++; lz--; } /* shorten z */
     225             :   else
     226  1001916914 :     do  *--zd = *--xd; while (xd > x);
     227   108261565 :   *--zd = evalsigne(1) | evallgefint(lz);
     228   108261565 :   *--zd = evaltyp(t_INT) | evallg(lz);
     229   108261565 :   return gc_const((pari_sp)zd, zd);
     230             : }
     231             : 
     232             : /* assume x > y */
     233             : static GEN
     234  2086071669 : subiispec(GEN x, GEN y, long nx, long ny)
     235             : {
     236             :   GEN xd,yd,zd;
     237  2086071669 :   long lz, i = -2;
     238             :   LOCAL_OVERFLOW;
     239             : 
     240  2086071669 :   if (ny==1) return subiuspec(x,*y,nx);
     241  1182261749 :   zd = (GEN)avma;
     242  1182261749 :   lz = nx+2; (void)new_chunk(lz);
     243  1182261749 :   xd = x + nx;
     244  1182261749 :   yd = y + ny;
     245  1182261749 :   zd[-1] = subll(xd[-1], yd[-1]);
     246             : #ifdef subllx8
     247   807740107 :   for (  ; i-8 > -ny; i-=8)
     248   413652857 :     subllx8(xd+i, yd+i, zd+i, overflow);
     249             : #endif
     250 12076198378 :   for (  ; i >= -ny; i--) zd[i] = subllx(xd[i], yd[i]);
     251  1182261749 :   if (overflow)
     252             :     for(;;)
     253             :     {
     254   146061078 :       zd[i] = uel(xd,i) - 1;
     255   237678005 :       if (xd[i--]) break;
     256             :     }
     257  1182261749 :   if (i>=-nx)
     258  1121425653 :     for (; i >= -nx; i--) zd[i] = xd[i];
     259             :   else
     260  1307376900 :     while (zd[i+1] == 0) { i++; lz--; } /* shorten z */
     261  1182261749 :   zd += i+1;
     262  1182261749 :   *--zd = evalsigne(1) | evallgefint(lz);
     263  1182261749 :   *--zd = evaltyp(t_INT) | evallg(lz);
     264  1182261749 :   return gc_const((pari_sp)zd, zd);
     265             : }
     266             : 
     267             : static void
     268   303184443 : roundr_up_ip(GEN x, long l)
     269             : {
     270   303184443 :   long i = l;
     271             :   for(;;)
     272             :   {
     273   304483128 :     if (++uel(x,--i)) break;
     274     1467651 :     if (i == 2) { x[2] = (long)HIGHBIT; shiftr_inplace(x, 1); break; }
     275             :   }
     276   303184443 : }
     277             : 
     278             : void
     279   192642363 : affir(GEN x, GEN y)
     280             : {
     281   192642363 :   const long s = signe(x), ly = lg(y);
     282             :   long lx, sh, i;
     283             : 
     284   192642363 :   if (!s)
     285             :   {
     286     4147989 :     y[1] = evalexpo(-prec2nbits(ly));
     287     4147989 :     return;
     288             :   }
     289             : 
     290   188494374 :   lx = lgefint(x); sh = bfffo(x[2]);
     291   188494374 :   y[1] = evalsigne(s) | evalexpo(bit_accuracy(lx)-sh-1);
     292   188494374 :   if (sh) {
     293   186045594 :     if (lx <= ly)
     294             :     {
     295   394523748 :       for (i=lx; i<ly; i++) y[i]=0;
     296   100875084 :       shift_left(y,x,2,lx-1, 0,sh);
     297   100875084 :       return;
     298             :     }
     299    85170510 :     shift_left(y,x,2,ly-1, x[ly],sh);
     300             :     /* lx > ly: round properly */
     301    85170510 :     if ((uel(x,ly)<<sh) & HIGHBIT) roundr_up_ip(y, ly);
     302             :   }
     303             :   else {
     304     2448780 :     if (lx <= ly)
     305             :     {
     306     4341456 :       for (i=2; i<lx; i++) y[i]=x[i];
     307     3285795 :       for (   ; i<ly; i++) y[i]=0;
     308     1253271 :       return;
     309             :     }
     310     3309165 :     for (i=2; i<ly; i++) y[i]=x[i];
     311             :     /* lx > ly: round properly */
     312     1195509 :     if (uel(x,ly) & HIGHBIT) roundr_up_ip(y, ly);
     313             :   }
     314             : }
     315             : 
     316             : INLINE GEN
     317   553006056 : shiftispec(GEN x, long nx, long n)
     318             : {
     319             :   long ny, i, m;
     320             :   GEN y, yd;
     321   553006056 :   if (!n)  return icopyspec(x, nx);
     322             : 
     323   518638368 :   if (n > 0)
     324             :   {
     325   331816404 :     GEN z = (GEN)avma;
     326   331816404 :     long d = dvmdsBIL(n, &m);
     327             : 
     328   331816404 :     ny = nx+d; y = new_chunk(ny + 2); yd = y + 2;
     329  1845887595 :     for ( ; d; d--) *--z = 0;
     330   559557891 :     if (!m) for (i=0; i<nx; i++) yd[i]=x[i];
     331             :     else
     332             :     {
     333   326629962 :       register const ulong sh = BITS_IN_LONG - m;
     334   326629962 :       shift_left(yd,x, 0,nx-1, 0,m);
     335   326629962 :       i = uel(x,0) >> sh;
     336             :       /* Extend y on the left? */
     337   326629962 :       if (i) { ny++; y = new_chunk(1); y[2] = i; }
     338             :     }
     339             :   }
     340             :   else
     341             :   {
     342   186821964 :     ny = nx - dvmdsBIL(-n, &m);
     343   186821964 :     if (ny<1) return gen_0;
     344   186451155 :     y = new_chunk(ny + 2); yd = y + 2;
     345   186451155 :     if (m) {
     346   150204162 :       shift_right(yd,x, 0,ny, 0,m);
     347   150204162 :       if (yd[0] == 0)
     348             :       {
     349    12616200 :         if (ny==1) return gc_const((pari_sp)(y+3), gen_0);
     350    11078946 :         ny--; set_avma((pari_sp)(++y));
     351             :       }
     352             :     } else {
     353  1476017265 :       for (i=0; i<ny; i++) yd[i]=x[i];
     354             :     }
     355             :   }
     356   516730305 :   y[1] = evalsigne(1)|evallgefint(ny + 2);
     357   516730305 :   y[0] = evaltyp(t_INT)|evallg(ny + 2); return y;
     358             : }
     359             : 
     360             : GEN
     361    22214211 : mantissa2nr(GEN x, long n)
     362             : { /*This is a kludge since x is not an integer*/
     363    22214211 :   long s = signe(x);
     364             :   GEN y;
     365             : 
     366    22214211 :   if(s == 0) return gen_0;
     367    22213344 :   y = shiftispec(x + 2, lg(x) - 2, n);
     368    22213344 :   if (signe(y)) setsigne(y, s);
     369    22213344 :   return y;
     370             : }
     371             : 
     372             : GEN
     373     1532673 : truncr(GEN x)
     374             : {
     375             :   long d,e,i,s,m;
     376             :   GEN y;
     377             : 
     378     1532673 :   if ((s=signe(x)) == 0 || (e=expo(x)) < 0) return gen_0;
     379     1230309 :   d = nbits2lg(e+1); m = remsBIL(e);
     380     1230309 :   if (d > lg(x)) pari_err_PREC( "truncr (precision loss in truncation)");
     381             : 
     382     1230309 :   y=cgeti(d); y[1] = evalsigne(s) | evallgefint(d);
     383     1230309 :   if (++m == BITS_IN_LONG)
     384         870 :     for (i=2; i<d; i++) y[i]=x[i];
     385             :   else
     386     1229874 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     387     1230309 :   return y;
     388             : }
     389             : 
     390             : /* integral part */
     391             : GEN
     392      642981 : floorr(GEN x)
     393             : {
     394             :   long d,e,i,lx,m;
     395             :   GEN y;
     396             : 
     397      642981 :   if (signe(x) >= 0) return truncr(x);
     398      225750 :   if ((e=expo(x)) < 0) return gen_m1;
     399       93624 :   d = nbits2lg(e+1); m = remsBIL(e);
     400       93624 :   lx=lg(x); if (d>lx) pari_err_PREC( "floorr (precision loss in truncation)");
     401       93624 :   y = new_chunk(d);
     402       93624 :   if (++m == BITS_IN_LONG)
     403             :   {
     404        1296 :     for (i=2; i<d; i++) y[i]=x[i];
     405        4947 :     i=d; while (i<lx && !x[i]) i++;
     406         510 :     if (i==lx) goto END;
     407             :   }
     408             :   else
     409             :   {
     410       93114 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     411       93114 :     if (uel(x,d-1)<<m == 0)
     412             :     {
     413      212352 :       i=d; while (i<lx && !x[i]) i++;
     414       25923 :       if (i==lx) goto END;
     415             :     }
     416             :   }
     417             :   /* set y:=y+1 */
     418       77556 :   for (i=d-1; i>=2; i--) { uel(y,i)++; if (y[i]) goto END; }
     419           0 :   y=new_chunk(1); y[2]=1; d++;
     420       93624 : END:
     421       93624 :   y[1] = evalsigne(-1) | evallgefint(d);
     422       93624 :   y[0] = evaltyp(t_INT) | evallg(d); return y;
     423             : }
     424             : 
     425             : INLINE int
     426  2557359489 : cmpiispec(GEN x, GEN y, long lx, long ly)
     427             : {
     428             :   long i;
     429  2557359489 :   if (lx < ly) return -1;
     430  2449790130 :   if (lx > ly) return  1;
     431  2525759817 :   i = 0; while (i<lx && x[i]==y[i]) i++;
     432  2282498619 :   if (i==lx) return 0;
     433  2169660390 :   return (uel(x,i) > uel(y,i))? 1: -1;
     434             : }
     435             : 
     436             : INLINE int
     437   140267649 : equaliispec(GEN x, GEN y, long lx, long ly)
     438             : {
     439             :   long i;
     440   140267649 :   if (lx != ly) return 0;
     441   272252139 :   i = ly-1; while (i>=0 && x[i]==y[i]) i--;
     442   140210505 :   return i < 0;
     443             : }
     444             : 
     445             : /***********************************************************************/
     446             : /**                                                                   **/
     447             : /**                          MULTIPLICATION                           **/
     448             : /**                                                                   **/
     449             : /***********************************************************************/
     450             : /* assume ny > 0 */
     451             : INLINE GEN
     452  2346328587 : muluispec(ulong x, GEN y, long ny)
     453             : {
     454  2346328587 :   GEN yd, z = (GEN)avma;
     455  2346328587 :   long lz = ny+3;
     456             :   LOCAL_HIREMAINDER;
     457             : 
     458  2346328587 :   (void)new_chunk(lz);
     459  2346328587 :   yd = y + ny; *--z = mulll(x, *--yd);
     460  4581610584 :   while (yd > y) *--z = addmul(x,*--yd);
     461  2346328587 :   if (hiremainder) *--z = hiremainder; else lz--;
     462  2346328587 :   *--z = evalsigne(1) | evallgefint(lz);
     463  2346328587 :   *--z = evaltyp(t_INT) | evallg(lz);
     464  2346328587 :   return gc_const((pari_sp)z, z);
     465             : }
     466             : 
     467             : /* a + b*|Y| */
     468             : GEN
     469      417918 : addumului(ulong a, ulong b, GEN Y)
     470             : {
     471             :   GEN yd,y,z;
     472             :   long ny,lz;
     473             :   LOCAL_HIREMAINDER;
     474             :   LOCAL_OVERFLOW;
     475             : 
     476      417918 :   if (!b || !signe(Y)) return utoi(a);
     477             : 
     478      417870 :   y = LIMBS(Y); z = (GEN)avma;
     479      417870 :   ny = NLIMBS(Y);
     480      417870 :   lz = ny+3;
     481             : 
     482      417870 :   (void)new_chunk(lz);
     483      417870 :   yd = y + ny; *--z = addll(a, mulll(b, *--yd));
     484      417870 :   if (overflow) hiremainder++; /* can't overflow */
     485      417870 :   while (yd > y) *--z = addmul(b,*--yd);
     486      417870 :   if (hiremainder) *--z = hiremainder; else lz--;
     487      417870 :   *--z = evalsigne(1) | evallgefint(lz);
     488      417870 :   *--z = evaltyp(t_INT) | evallg(lz);
     489      417870 :   return gc_const((pari_sp)z, z);
     490             : }
     491             : 
     492             : /***********************************************************************/
     493             : /**                                                                   **/
     494             : /**                          DIVISION                                 **/
     495             : /**                                                                   **/
     496             : /***********************************************************************/
     497             : 
     498             : ulong
     499  1020155550 : umodiu(GEN y, ulong x)
     500             : {
     501  1020155550 :   long sy=signe(y),ly,i;
     502             :   ulong xi;
     503             :   LOCAL_HIREMAINDER;
     504             : 
     505  1020155550 :   if (!x) pari_err_INV("umodiu",gen_0);
     506  1020155550 :   if (!sy) return 0;
     507   838471185 :   ly = lgefint(y);
     508   838471185 :   if (x <= uel(y,2))
     509             :   {
     510   383917611 :     hiremainder=0;
     511   383917611 :     if (ly==3)
     512             :     {
     513   369104214 :       hiremainder=uel(y,2)%x;
     514   369104214 :       if (!hiremainder) return 0;
     515   337916892 :       return (sy > 0)? hiremainder: x - hiremainder;
     516             :     }
     517             :   }
     518             :   else
     519             :   {
     520   454553574 :     if (ly==3) return (sy > 0)? uel(y,2): x - uel(y,2);
     521    63728964 :     hiremainder=uel(y,2); ly--; y++;
     522             :   }
     523    78542361 :   xi = get_Fl_red(x);
     524   296315595 :   for (i=2; i<ly; i++) (void)divll_pre(y[i],x,xi);
     525    78542361 :   if (!hiremainder) return 0;
     526    74721522 :   return (sy > 0)? hiremainder: x - hiremainder;
     527             : }
     528             : 
     529             : /* return |y| \/ x */
     530             : GEN
     531   159707664 : absdiviu_rem(GEN y, ulong x, ulong *rem)
     532             : {
     533             :   long ly,i;
     534             :   GEN z;
     535             :   ulong xi;
     536             :   LOCAL_HIREMAINDER;
     537             : 
     538   159707664 :   if (!x) pari_err_INV("absdiviu_rem",gen_0);
     539   159707664 :   if (!signe(y)) { *rem = 0; return gen_0; }
     540             : 
     541   154350369 :   ly = lgefint(y);
     542   154350369 :   if (x <= uel(y,2))
     543             :   {
     544   130211115 :     hiremainder=0;
     545   130211115 :     if (ly==3)
     546             :     {
     547    63192390 :       z = cgetipos(3);
     548    63192390 :       z[2] = divll(uel(y,2),x);
     549    63192390 :       *rem = hiremainder; return z;
     550             :     }
     551             :   }
     552             :   else
     553             :   {
     554    24139254 :     if (ly==3) { *rem = uel(y,2); return gen_0; }
     555    15918594 :     hiremainder = uel(y,2); ly--; y++;
     556             :   }
     557    82937319 :   xi = get_Fl_red(x);
     558    82937319 :   z = cgetipos(ly);
     559   595630836 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     560    82937319 :   *rem = hiremainder; return z;
     561             : }
     562             : 
     563             : GEN
     564    45468000 : divis_rem(GEN y, long x, long *rem)
     565             : {
     566    45468000 :   long sy=signe(y),ly,s,i;
     567             :   GEN z;
     568             :   ulong xi;
     569             :   LOCAL_HIREMAINDER;
     570             : 
     571    45468000 :   if (!x) pari_err_INV("divis_rem",gen_0);
     572    45468000 :   if (!sy) { *rem=0; return gen_0; }
     573    34726062 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     574             : 
     575    34726062 :   ly = lgefint(y);
     576    34726062 :   if ((ulong)x <= uel(y,2))
     577             :   {
     578    24988170 :     hiremainder=0;
     579    24988170 :     if (ly==3)
     580             :     {
     581    24555999 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     582    24555999 :       z[2] = divll(uel(y,2),x);
     583    24555999 :       if (sy<0) hiremainder = - ((long)hiremainder);
     584    24555999 :       *rem = (long)hiremainder; return z;
     585             :     }
     586             :   }
     587             :   else
     588             :   {
     589     9737892 :     if (ly==3) { *rem = itos(y); return gen_0; }
     590      173934 :     hiremainder = uel(y,2); ly--; y++;
     591             :   }
     592      606105 :   xi = get_Fl_red(x);
     593      606105 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     594     2478546 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     595      606105 :   if (sy<0) hiremainder = - ((long)hiremainder);
     596      606105 :   *rem = (long)hiremainder; return z;
     597             : }
     598             : 
     599             : GEN
     600      806859 : divis(GEN y, long x)
     601             : {
     602      806859 :   long sy=signe(y),ly,s,i;
     603             :   ulong xi;
     604             :   GEN z;
     605             :   LOCAL_HIREMAINDER;
     606             : 
     607      806859 :   if (!x) pari_err_INV("divis",gen_0);
     608      806859 :   if (!sy) return gen_0;
     609      806853 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     610             : 
     611      806853 :   ly = lgefint(y);
     612      806853 :   if ((ulong)x <= uel(y,2))
     613             :   {
     614      796332 :     hiremainder=0;
     615      796332 :     if (ly==3)
     616             :     {
     617      720429 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     618      720429 :       z[2] = divll(y[2],x);
     619      720429 :       return z;
     620             :     }
     621             :   }
     622             :   else
     623             :   {
     624       10521 :     if (ly==3) return gen_0;
     625       10521 :     hiremainder=y[2]; ly--; y++;
     626             :   }
     627       86424 :   xi = get_Fl_red(x);
     628       86424 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     629      623577 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x, xi);
     630       86424 :   return z;
     631             : }
     632             : 
     633             : GEN
     634   135363195 : divrr(GEN x, GEN y)
     635             : {
     636   135363195 :   long sx=signe(x), sy=signe(y), lx,ly,lr,e,i,j;
     637             :   ulong y0,y1;
     638             :   GEN r, r1;
     639             : 
     640   135363195 :   if (!x) pari_err_INV("divrr",y);
     641   135363195 :   e = expo(x) - expo(y);
     642   135363195 :   if (!sx) return real_0_bit(e);
     643   133959228 :   if (sy<0) sx = -sx;
     644             : 
     645   133959228 :   lx=lg(x); ly=lg(y);
     646   133959228 :   if (ly==3)
     647             :   {
     648    98304516 :     ulong k = x[2], l = (lx>3)? x[3]: 0;
     649             :     LOCAL_HIREMAINDER;
     650    98304516 :     if (k < uel(y,2)) e--;
     651             :     else
     652             :     {
     653    45627102 :       l >>= 1; if (k&1) l |= HIGHBIT;
     654    45627102 :       k >>= 1;
     655             :     }
     656    98304516 :     hiremainder = k; k = divll(l,y[2]);
     657    98304516 :     if (hiremainder > (uel(y,2) >> 1) && !++k) { k = HIGHBIT; e++; }
     658    98304516 :     r = cgetr(3);
     659    98304516 :     r[1] = evalsigne(sx) | evalexpo(e);
     660    98304516 :     r[2] = k; return r;
     661             :   }
     662             : 
     663    35654712 :   lr = minss(lx,ly); r = new_chunk(lr);
     664    35654712 :   r1 = r-1;
     665   244753848 :   r1[1] = 0; for (i=2; i<lr; i++) r1[i]=x[i];
     666    35654712 :   r1[lr] = (lx>ly)? x[lr]: 0;
     667    35654712 :   y0 = y[2]; y1 = y[3];
     668   280408560 :   for (i=0; i<lr-1; i++)
     669             :   { /* r1 = r + (i-1), OK up to r1[2] (accesses at most r[lr]) */
     670             :     ulong k, qp;
     671             :     LOCAL_HIREMAINDER;
     672             :     LOCAL_OVERFLOW;
     673             : 
     674   244753848 :     if (uel(r1,1) == y0) { qp = ULONG_MAX; k = addll(y0,r1[2]); }
     675             :     else
     676             :     {
     677   241318611 :       if (uel(r1,1) > y0) /* can't happen if i=0 */
     678             :       {
     679           0 :         GEN y1 = y+1;
     680           0 :         j = lr-i; r1[j] = subll(r1[j],y1[j]);
     681           0 :         for (j--; j>0; j--) r1[j] = subllx(r1[j],y1[j]);
     682           0 :         j=i; do uel(r,--j)++; while (j && !uel(r,j));
     683             :       }
     684   241318611 :       hiremainder = r1[1]; overflow = 0;
     685   241318611 :       qp = divll(r1[2],y0); k = hiremainder;
     686             :     }
     687   244753848 :     j = lr-i+1;
     688   244753848 :     if (!overflow)
     689             :     {
     690             :       long k3, k4;
     691   242203224 :       k3 = mulll(qp,y1);
     692   242203224 :       if (j == 3) /* i = lr - 2 maximal, r1[3] undefined -> 0 */
     693    35632395 :         k4 = subll(hiremainder,k);
     694             :       else
     695             :       {
     696   206570829 :         k3 = subll(k3, r1[3]);
     697   206570829 :         k4 = subllx(hiremainder,k);
     698             :       }
     699   308637378 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     700             :     }
     701   244753848 :     if (j<ly) (void)mulll(qp,y[j]); else { hiremainder = 0 ; j = ly; }
     702  2249305050 :     for (j--; j>1; j--)
     703             :     {
     704  2004551202 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     705  2004551202 :       hiremainder += overflow;
     706             :     }
     707   244753848 :     if (uel(r1,1) != hiremainder)
     708             :     {
     709      469257 :       if (uel(r1,1) < hiremainder)
     710             :       {
     711      469257 :         qp--;
     712      469257 :         j = lr-i-(lr-i>=ly); r1[j] = addll(r1[j], y[j]);
     713     4721643 :         for (j--; j>1; j--) r1[j] = addllx(r1[j], y[j]);
     714             :       }
     715             :       else
     716             :       {
     717           0 :         r1[1] -= hiremainder;
     718           0 :         while (r1[1])
     719             :         {
     720           0 :           qp++; if (!qp) { j=i; do uel(r,--j)++; while (j && !r[j]); }
     721           0 :           j = lr-i-(lr-i>=ly); r1[j] = subll(r1[j],y[j]);
     722           0 :           for (j--; j>1; j--) r1[j] = subllx(r1[j],y[j]);
     723           0 :           r1[1] -= overflow;
     724             :         }
     725             :       }
     726             :     }
     727   244753848 :     *++r1 = qp;
     728             :   }
     729             :   /* i = lr-1 */
     730             :   /* round correctly */
     731    35654712 :   if (uel(r1,1) > (y0>>1))
     732             :   {
     733    17384370 :     j=i; do uel(r,--j)++; while (j && !r[j]);
     734             :   }
     735   244753848 :   r1 = r-1; for (j=i; j>=2; j--) r[j]=r1[j];
     736    35654712 :   if (r[0] == 0) e--;
     737    15482826 :   else if (r[0] == 1) { shift_right(r,r, 2,lr, 1,1); }
     738             :   else { /* possible only when rounding up to 0x2 0x0 ... */
     739           0 :     r[2] = (long)HIGHBIT; e++;
     740             :   }
     741    35654712 :   r[0] = evaltyp(t_REAL)|evallg(lr);
     742    35654712 :   r[1] = evalsigne(sx) | evalexpo(e);
     743    35654712 :   return r;
     744             : }
     745             : 
     746             : GEN
     747    25703694 : divri(GEN x, GEN y)
     748             : {
     749    25703694 :   long lx, s = signe(y);
     750             :   pari_sp av;
     751             :   GEN z;
     752             : 
     753    25703694 :   if (!s) pari_err_INV("divri",y);
     754    25703694 :   if (!signe(x)) return real_0_bit(expo(x) - expi(y));
     755    25595706 :   if (!is_bigint(y)) {
     756    21164985 :     GEN z = divru(x, y[2]);
     757    21164985 :     if (s < 0) togglesign(z);
     758    21164985 :     return z;
     759             :   }
     760     4430721 :   lx = lg(x); z = cgetr(lx); av = avma;
     761     4430721 :   affrr(divrr(x, itor(y, lx+1)), z);
     762     4430721 :   return gc_const(av, z);
     763             : }
     764             : 
     765             : /* Integer division x / y: such that sign(r) = sign(x)
     766             :  *   if z = ONLY_REM return remainder, otherwise return quotient
     767             :  *   if z != NULL set *z to remainder
     768             :  *   *z is the last object on stack (and thus can be disposed of with cgiv
     769             :  *   instead of gerepile)
     770             :  * If *z is zero, we put gen_0 here and no copy.
     771             :  * space needed: lx + ly */
     772             : GEN
     773   723941406 : dvmdii(GEN x, GEN y, GEN *z)
     774             : {
     775   723941406 :   long sx = signe(x), sy = signe(y);
     776   723941406 :   long lx, ly = lgefint(y), lz, i, j, sh, lq, lr;
     777             :   pari_sp av;
     778             :   ulong y0,y0i,y1, *xd,*rd,*qd;
     779             :   GEN q, r, r1;
     780             : 
     781   723941406 :   if (!sx)
     782             :   {
     783    20716608 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     784    20716605 :     if (!z || z == ONLY_REM) return gen_0;
     785     6646548 :     *z=gen_0; return gen_0;
     786             :   }
     787   703224798 :   if (ly <= 3)
     788             :   {
     789             :     ulong rem;
     790   360036126 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     791   360036126 :     if (z == ONLY_REM)
     792             :     {
     793   300135606 :       rem = umodiu(x,uel(y,2));
     794   300135606 :       if (!rem) return gen_0;
     795   273019392 :       return (sx < 0)? utoineg(uel(y,2) - rem): utoipos(rem);
     796             :     }
     797    59900520 :     q = absdiviu_rem(x, uel(y,2), &rem);
     798    59900520 :     if (sx != sy) togglesign(q);
     799    59900520 :     if (!z) return q;
     800    57974559 :     if (!rem) *z = gen_0;
     801    21218121 :     else *z = sx < 0? utoineg(rem): utoipos(rem);
     802    57974559 :     return q;
     803             :   }
     804   343188672 :   lx=lgefint(x);
     805   343188672 :   lz=lx-ly;
     806   343188672 :   if (lz <= 0)
     807             :   {
     808   149402025 :     if (lz == 0)
     809             :     {
     810   137084214 :       for (i=2; i<lx; i++)
     811   136489122 :         if (x[i] != y[i])
     812             :         {
     813   122522673 :           if (uel(x,i) > uel(y,i)) goto DIVIDE;
     814    45135138 :           goto TRIVIAL;
     815             :         }
     816      595092 :       if (z == ONLY_REM) return gen_0;
     817       23844 :       if (z) *z = gen_0;
     818       23844 :       if (sx < 0) sy = -sy;
     819       23844 :       return stoi(sy);
     820             :     }
     821    26284260 : TRIVIAL:
     822    71419398 :     if (z == ONLY_REM) return icopy(x);
     823     1091580 :     if (z) *z = icopy(x);
     824     1091580 :     return gen_0;
     825             :   }
     826   193786647 : DIVIDE: /* quotient is non-zero */
     827   271174182 :   av=avma; if (sx<0) sy = -sy;
     828   271174182 :   r1 = new_chunk(lx); sh = bfffo(y[2]);
     829   271174182 :   if (sh)
     830             :   { /* normalize so that highbit(y) = 1 (shift left x and y by sh bits)*/
     831   262986738 :     register const ulong m = BITS_IN_LONG - sh;
     832   262986738 :     r = new_chunk(ly);
     833   262986738 :     shift_left(r, y,2,ly-1, 0,sh); y = r;
     834   262986738 :     shift_left(r1,x,2,lx-1, 0,sh);
     835   262986738 :     r1[1] = uel(x,2) >> m;
     836             :   }
     837             :   else
     838             :   {
     839    82140546 :     r1[1] = 0; for (j=2; j<lx; j++) r1[j] = x[j];
     840             :   }
     841   271174182 :   x = r1;
     842   271174182 :   y0 = y[2]; y0i = get_Fl_red(y0);
     843   271174182 :   y1 = y[3];
     844  1643748408 :   for (i=0; i<=lz; i++)
     845             :   { /* r1 = x + i */
     846             :     ulong k, qp;
     847             :     LOCAL_HIREMAINDER;
     848             :     LOCAL_OVERFLOW;
     849             : 
     850  1372574226 :     if (uel(r1,1) == y0)
     851             :     {
     852       25802 :       qp = ULONG_MAX; k = addll(y0,r1[2]);
     853             :     }
     854             :     else
     855             :     {
     856  1372548424 :       hiremainder = r1[1]; overflow = 0;
     857  1372548424 :       qp = divll_pre(r1[2],y0,y0i); k = hiremainder;
     858             :     }
     859  1372574226 :     if (!overflow)
     860             :     {
     861  1372549030 :       long k3 = subll(mulll(qp,y1), r1[3]);
     862  1372549030 :       long k4 = subllx(hiremainder,k);
     863  1597440051 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     864             :     }
     865  1372574226 :     hiremainder = 0; j = ly;
     866 26521956438 :     for (j--; j>1; j--)
     867             :     {
     868 25149382212 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     869 25149382212 :       hiremainder += overflow;
     870             :     }
     871  1372574226 :     if (uel(r1,1) < hiremainder)
     872             :     {
     873     2564488 :       qp--;
     874     2564488 :       j = ly-1; r1[j] = addll(r1[j],y[j]);
     875    11723407 :       for (j--; j>1; j--) r1[j] = addllx(r1[j],y[j]);
     876             :     }
     877  1372574226 :     *++r1 = qp;
     878             :   }
     879             : 
     880   271174182 :   lq = lz+2;
     881   271174182 :   if (!z)
     882             :   {
     883     2089548 :     qd = (ulong*)av;
     884     2089548 :     xd = (ulong*)(x + lq);
     885     2089548 :     if (x[1]) { lz++; lq++; }
     886    28756287 :     while (lz--) *--qd = *--xd;
     887     2089548 :     *--qd = evalsigne(sy) | evallgefint(lq);
     888     2089548 :     *--qd = evaltyp(t_INT) | evallg(lq);
     889     2089548 :     return gc_const((pari_sp)qd, (GEN)qd);
     890             :   }
     891             : 
     892   326815200 :   j=lq; while (j<lx && !x[j]) j++;
     893   269084634 :   lz = lx-j;
     894   269084634 :   if (z == ONLY_REM)
     895             :   {
     896   203450919 :     if (lz==0) return gc_const(av, gen_0);
     897   197839386 :     rd = (ulong*)av; lr = lz+2;
     898   197839386 :     xd = (ulong*)(x + lx);
     899   231883044 :     if (!sh) while (lz--) *--rd = *--xd;
     900             :     else
     901             :     { /* shift remainder right by sh bits */
     902   189925653 :       const ulong shl = BITS_IN_LONG - sh;
     903             :       ulong l;
     904   189925653 :       xd--;
     905   808343604 :       while (--lz) /* fill r[3..] */
     906             :       {
     907   618417951 :         l = *xd >> sh;
     908   618417951 :         *--rd = l | (*--xd << shl);
     909             :       }
     910   189925653 :       l = *xd >> sh;
     911   189925653 :       if (l) *--rd = l; else lr--;
     912             :     }
     913   197839386 :     *--rd = evalsigne(sx) | evallgefint(lr);
     914   197839386 :     *--rd = evaltyp(t_INT) | evallg(lr);
     915   197839386 :     return gc_const((pari_sp)rd, (GEN)rd);
     916             :   }
     917             : 
     918    65633715 :   lr = lz+2;
     919    65633715 :   rd = NULL; /* gcc -Wall */
     920    65633715 :   if (lz)
     921             :   { /* non zero remainder: initialize rd */
     922    63582324 :     xd = (ulong*)(x + lx);
     923    63582324 :     if (!sh)
     924             :     {
     925      159624 :       rd = (ulong*)avma; (void)new_chunk(lr);
     926      635847 :       while (lz--) *--rd = *--xd;
     927             :     }
     928             :     else
     929             :     { /* shift remainder right by sh bits */
     930    63422700 :       const ulong shl = BITS_IN_LONG - sh;
     931             :       ulong l;
     932    63422700 :       rd = (ulong*)x; /* overwrite shifted y */
     933    63422700 :       xd--;
     934   235802043 :       while (--lz)
     935             :       {
     936   172379343 :         l = *xd >> sh;
     937   172379343 :         *--rd = l | (*--xd << shl);
     938             :       }
     939    63422700 :       l = *xd >> sh;
     940    63422700 :       if (l) *--rd = l; else lr--;
     941             :     }
     942    63582324 :     *--rd = evalsigne(sx) | evallgefint(lr);
     943    63582324 :     *--rd = evaltyp(t_INT) | evallg(lr);
     944    63582324 :     rd += lr;
     945             :   }
     946    65633715 :   qd = (ulong*)av;
     947    65633715 :   xd = (ulong*)(x + lq);
     948    65633715 :   if (x[1]) lq++;
     949   219133242 :   j = lq-2; while (j--) *--qd = *--xd;
     950    65633715 :   *--qd = evalsigne(sy) | evallgefint(lq);
     951    65633715 :   *--qd = evaltyp(t_INT) | evallg(lq);
     952    65633715 :   q = (GEN)qd;
     953    65633715 :   if (lr==2) *z = gen_0;
     954             :   else
     955             :   { /* rd has been properly initialized: we had lz > 0 */
     956   387403917 :     while (lr--) *--qd = *--rd;
     957    63582324 :     *z = (GEN)qd;
     958             :   }
     959    65633715 :   return gc_const((pari_sp)qd, q);
     960             : }
     961             : 
     962             : /* Montgomery reduction.
     963             :  * N has k words, assume T >= 0 has less than 2k.
     964             :  * Return res := T / B^k mod N, where B = 2^BIL
     965             :  * such that 0 <= res < T/B^k + N  and  res has less than k words */
     966             : GEN
     967    24107394 : red_montgomery(GEN T, GEN N, ulong inv)
     968             : {
     969             :   pari_sp av;
     970             :   GEN Te, Td, Ne, Nd, scratch;
     971    24107394 :   ulong i, j, m, t, d, k = NLIMBS(N);
     972             :   int carry;
     973             :   LOCAL_HIREMAINDER;
     974             :   LOCAL_OVERFLOW;
     975             : 
     976    24107394 :   if (k == 0) return gen_0;
     977    24107394 :   d = NLIMBS(T); /* <= 2*k */
     978    24107394 :   if (d == 0) return gen_0;
     979             : #ifdef DEBUG
     980             :   if (d > 2*k) pari_err_BUG("red_montgomery");
     981             : #endif
     982    24107385 :   if (k == 1)
     983             :   { /* as below, special cased for efficiency */
     984         447 :     ulong n = uel(N,2);
     985         447 :     if (d == 1) {
     986         447 :       hiremainder = uel(T,2);
     987         447 :       m = hiremainder * inv;
     988         447 :       (void)addmul(m, n); /* t + m*n = 0 */
     989         447 :       return utoi(hiremainder);
     990             :     } else { /* d = 2 */
     991           0 :       hiremainder = uel(T,3);
     992           0 :       m = hiremainder * inv;
     993           0 :       (void)addmul(m, n); /* t + m*n = 0 */
     994           0 :       t = addll(hiremainder, uel(T,2));
     995           0 :       if (overflow) t -= n; /* t > n doesn't fit in 1 word */
     996           0 :       return utoi(t);
     997             :     }
     998             :   }
     999             :   /* assume k >= 2 */
    1000    24106938 :   av = avma; scratch = new_chunk(k<<1); /* >= k + 2: result fits */
    1001             : 
    1002             :   /* copy T to scratch space (pad with zeroes to 2k words) */
    1003    24106938 :   Td = (GEN)av;
    1004    24106938 :   Te = T + (d+2);
    1005   668479470 :   for (i=0; i < d     ; i++) *--Td = *--Te;
    1006    43954056 :   for (   ; i < (k<<1); i++) *--Td = 0;
    1007             : 
    1008    24106938 :   Te = (GEN)av; /* 1 beyond end of current T mantissa (in scratch) */
    1009    24106938 :   Ne = N + k+2; /* 1 beyond end of N mantissa */
    1010             : 
    1011    24106938 :   carry = 0;
    1012   356216763 :   for (i=0; i<k; i++) /* set T := T/B nod N, k times */
    1013             :   {
    1014   332109825 :     Td = Te; /* one beyond end of (new) T mantissa */
    1015   332109825 :     Nd = Ne;
    1016   332109825 :     hiremainder = *--Td;
    1017   332109825 :     m = hiremainder * inv; /* solve T + m N = O(B) */
    1018             : 
    1019             :     /* set T := (T + mN) / B */
    1020   332109825 :     Te = Td;
    1021   332109825 :     (void)addmul(m, *--Nd); /* = 0 */
    1022  6274763727 :     for (j=1; j<k; j++)
    1023             :     {
    1024  5942653902 :       t = addll(addmul(m, *--Nd), *--Td);
    1025  5942653902 :       *Td = t;
    1026  5942653902 :       hiremainder += overflow;
    1027             :     }
    1028   332109825 :     t = addll(hiremainder, *--Td); *Td = t + carry;
    1029   332109825 :     carry = (overflow || (carry && *Td == 0));
    1030             :   }
    1031    24106938 :   if (carry)
    1032             :   { /* Td > N overflows (k+1 words), set Td := Td - N */
    1033      360885 :     Td = Te;
    1034      360885 :     Nd = Ne;
    1035      360885 :     t = subll(*--Td, *--Nd); *Td = t;
    1036     6917724 :     while (Td > scratch) { t = subllx(*--Td, *--Nd); *Td = t; }
    1037             :   }
    1038             : 
    1039             :   /* copy result */
    1040    24106938 :   Td = (GEN)av;
    1041    27795705 :   while (*scratch == 0 && Te > scratch) scratch++; /* strip leading 0s */
    1042   352527996 :   while (Te > scratch) *--Td = *--Te;
    1043    24106938 :   k = (GEN)av - Td; if (!k) return gc_const(av, gen_0);
    1044    24106938 :   k += 2;
    1045    24106938 :   *--Td = evalsigne(1) | evallgefint(k);
    1046    24106938 :   *--Td = evaltyp(t_INT) | evallg(k);
    1047             : #ifdef DEBUG
    1048             : {
    1049             :   long l = NLIMBS(N), s = BITS_IN_LONG*l;
    1050             :   GEN R = int2n(s);
    1051             :   GEN res = remii(mulii(T, Fp_inv(R, N)), N);
    1052             :   if (k > lgefint(N)
    1053             :     || !equalii(remii(Td,N),res)
    1054             :     || cmpii(Td, addii(shifti(T, -s), N)) >= 0) pari_err_BUG("red_montgomery");
    1055             : }
    1056             : #endif
    1057    24106938 :   return gc_const((pari_sp)Td, Td);
    1058             : }
    1059             : 
    1060             : /* EXACT INTEGER DIVISION */
    1061             : 
    1062             : /* assume xy>0, the division is exact and y is odd. Destroy x */
    1063             : static GEN
    1064    20826636 : diviuexact_i(GEN x, ulong y)
    1065             : {
    1066             :   long i, lz, lx;
    1067             :   ulong q, yinv;
    1068             :   GEN z, z0, x0, x0min;
    1069             : 
    1070    20826636 :   if (y == 1) return icopy(x);
    1071    16870290 :   lx = lgefint(x);
    1072    16870290 :   if (lx == 3)
    1073             :   {
    1074      622578 :     q = uel(x,2) / y;
    1075      622578 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1076      622578 :     return utoipos(q);
    1077             :   }
    1078    16247712 :   yinv = invmod2BIL(y);
    1079    16247712 :   lz = (y <= uel(x,2)) ? lx : lx-1;
    1080    16247712 :   z = new_chunk(lz);
    1081    16247712 :   z0 = z + lz;
    1082    16247712 :   x0 = x + lx; x0min = x + lx-lz+2;
    1083             : 
    1084    50394978 :   while (x0 > x0min)
    1085             :   {
    1086    34147266 :     *--z0 = q = yinv*uel(--x0,0); /* i-th quotient */
    1087    34147266 :     if (!q) continue;
    1088             :     /* x := x - q * y */
    1089             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1090    33695742 :       register GEN x1 = x0 - 1;
    1091             :       LOCAL_HIREMAINDER;
    1092    33695742 :       (void)mulll(q,y);
    1093    33695742 :       if (hiremainder)
    1094             :       {
    1095    28044984 :         if (uel(x1,0) < hiremainder)
    1096             :         {
    1097       50865 :           uel(x1,0) -= hiremainder;
    1098       52656 :           do uel(--x1,0)--; while (uel(x1,0) == ULONG_MAX);
    1099             :         }
    1100             :         else
    1101    27994119 :           uel(x1,0) -= hiremainder;
    1102             :       }
    1103             :     }
    1104             :   }
    1105    16247712 :   i=2; while(!z[i]) i++;
    1106    16247712 :   z += i-2; lz -= i-2;
    1107    16247712 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1108    16247712 :   z[1] = evalsigne(1)|evallg(lz);
    1109    16247712 :   if (lz == 2) pari_err_OP("exact division", x, utoi(y));
    1110    16247712 :   return gc_const((pari_sp)z, z);
    1111             : }
    1112             : 
    1113             : /* assume y != 0 and the division is exact */
    1114             : GEN
    1115    29728572 : diviuexact(GEN x, ulong y)
    1116             : {
    1117             :   pari_sp av;
    1118    29728572 :   long lx, vy, s = signe(x);
    1119             :   GEN z;
    1120             : 
    1121    29728572 :   if (!s) return gen_0;
    1122    28895616 :   if (y == 1) return icopy(x);
    1123    25924362 :   lx = lgefint(x);
    1124    25924362 :   if (lx == 3) {
    1125    23443635 :     ulong q = uel(x,2) / y;
    1126    23443635 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1127    23443635 :     return (s > 0)? utoipos(q): utoineg(q);
    1128             :   }
    1129     2480727 :   av = avma; (void)new_chunk(lx); vy = vals(y);
    1130     2480727 :   if (vy) {
    1131     1193340 :     y >>= vy;
    1132     1193340 :     if (y == 1) { set_avma(av); return shifti(x, -vy); }
    1133      580380 :     x = shifti(x, -vy);
    1134      580380 :     if (lx == 3) {
    1135           0 :       ulong q = uel(x,2) / y;
    1136           0 :       set_avma(av);
    1137           0 :       if (!q) pari_err_OP("exact division", x, utoi(y));
    1138           0 :       return (s > 0)? utoipos(q): utoineg(q);
    1139             :     }
    1140     1287387 :   } else x = icopy(x);
    1141     1867767 :   set_avma(av);
    1142     1867767 :   z = diviuexact_i(x, y);
    1143     1867767 :   setsigne(z, s); return z;
    1144             : }
    1145             : 
    1146             : /* Find z such that x=y*z, knowing that y | x (unchecked)
    1147             :  * Method: y0 z0 = x0 mod B = 2^BITS_IN_LONG ==> z0 = 1/y0 mod B.
    1148             :  *    Set x := (x - z0 y) / B, updating only relevant words, and repeat */
    1149             : GEN
    1150   233778945 : diviiexact(GEN x, GEN y)
    1151             : {
    1152   233778945 :   long lx, ly, lz, vy, i, ii, sx = signe(x), sy = signe(y);
    1153             :   pari_sp av;
    1154             :   ulong y0inv,q;
    1155             :   GEN z;
    1156             : 
    1157   233778945 :   if (!sy) pari_err_INV("diviiexact",gen_0);
    1158   233778945 :   if (!sx) return gen_0;
    1159   197342868 :   lx = lgefint(x);
    1160   197342868 :   if (lx == 3) {
    1161   164745540 :     q = uel(x,2) / uel(y,2);
    1162   164745540 :     if (!q) pari_err_OP("exact division", x, y);
    1163   164745531 :     return (sx+sy) ? utoipos(q): utoineg(q);
    1164             :   }
    1165    32597328 :   vy = vali(y); av = avma;
    1166    32597328 :   (void)new_chunk(lx); /* enough room for z */
    1167    32597328 :   if (vy)
    1168             :   { /* make y odd */
    1169    16417488 :     y = shifti(y,-vy);
    1170    16417488 :     x = shifti(x,-vy); lx = lgefint(x);
    1171             :   }
    1172    16179840 :   else x = icopy(x); /* necessary because we destroy x */
    1173    32597328 :   set_avma(av); /* will erase our x,y when exiting */
    1174             :   /* now y is odd */
    1175    32597328 :   ly = lgefint(y);
    1176    32597328 :   if (ly == 3)
    1177             :   {
    1178    18958869 :     z = diviuexact_i(x,uel(y,2)); /* x != 0 */
    1179    18958869 :     setsigne(z, (sx+sy)? 1: -1); return z;
    1180             :   }
    1181    13638459 :   y0inv = invmod2BIL(y[ly-1]);
    1182    23504463 :   i=2; while (i<ly && y[i]==x[i]) i++;
    1183    13638459 :   lz = (i==ly || uel(y,i) < uel(x,i)) ? lx-ly+3 : lx-ly+2;
    1184    13638459 :   z = new_chunk(lz);
    1185             : 
    1186    13638459 :   y += ly - 1; /* now y[-i] = i-th word of y */
    1187    57875778 :   for (ii=lx-1,i=lz-1; i>=2; i--,ii--)
    1188             :   {
    1189             :     long limj;
    1190             :     LOCAL_HIREMAINDER;
    1191             :     LOCAL_OVERFLOW;
    1192             : 
    1193    44237319 :     z[i] = q = y0inv*uel(x,ii); /* i-th quotient */
    1194    44237319 :     if (!q) continue;
    1195             : 
    1196             :     /* x := x - q * y */
    1197    44167905 :     (void)mulll(q,y[0]); limj = maxss(lx - lz, ii+3-ly);
    1198             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1199    44167905 :       register GEN x0 = x + (ii - 1), y0 = y - 1, xlim = x + limj;
    1200   218461821 :       for (; x0 >= xlim; x0--, y0--)
    1201             :       {
    1202   174293916 :         *x0 = subll(*x0, addmul(q,*y0));
    1203   174293916 :         hiremainder += overflow;
    1204             :       }
    1205    44167905 :       if (hiremainder && limj != lx - lz)
    1206             :       {
    1207    21768183 :         if ((ulong)*x0 < hiremainder)
    1208             :         {
    1209      231681 :           *x0 -= hiremainder;
    1210      231681 :           do (*--x0)--; while ((ulong)*x0 == ULONG_MAX);
    1211             :         }
    1212             :         else
    1213    21536502 :           *x0 -= hiremainder;
    1214             :       }
    1215             :     }
    1216             :   }
    1217    13638459 :   i=2; while(!z[i]) i++;
    1218    13638459 :   z += i-2; lz -= (i-2);
    1219    13638459 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1220    13638459 :   z[1] = evalsigne((sx+sy)? 1: -1) | evallg(lz);
    1221    13638459 :   if (lz == 2) pari_err_OP("exact division", x, y);
    1222    13638459 :   return gc_const((pari_sp)z, z);
    1223             : }
    1224             : 
    1225             : /* assume yz != and yz | x */
    1226             : GEN
    1227      132099 : diviuuexact(GEN x, ulong y, ulong z)
    1228             : {
    1229             :   long tmp[4];
    1230             :   ulong t;
    1231             :   LOCAL_HIREMAINDER;
    1232      132099 :   t = mulll(y, z);
    1233      132099 :   if (!hiremainder) return diviuexact(x, t);
    1234           0 :   tmp[0] = evaltyp(t_INT)|_evallg(4);
    1235           0 :   tmp[1] = evalsigne(1)|evallgefint(4);
    1236           0 :   tmp[2] = hiremainder;
    1237           0 :   tmp[3] = t;
    1238           0 :   return diviiexact(x, tmp);
    1239             : }
    1240             : 
    1241             : /********************************************************************/
    1242             : /**                                                                **/
    1243             : /**               INTEGER MULTIPLICATION (BASECASE)                **/
    1244             : /**                                                                **/
    1245             : /********************************************************************/
    1246             : /* nx >= ny = num. of digits of x, y (not GEN, see mulii) */
    1247             : INLINE GEN
    1248  2573143125 : muliispec_basecase(GEN x, GEN y, long nx, long ny)
    1249             : {
    1250             :   GEN z2e,z2d,yd,xd,ye,zd;
    1251             :   long p1,lz;
    1252             :   LOCAL_HIREMAINDER;
    1253             : 
    1254  2573143125 :   if (ny == 1) return muluispec((ulong)*y, x, nx);
    1255   704309931 :   if (ny == 0) return gen_0;
    1256   703556562 :   zd = (GEN)avma; lz = nx+ny+2;
    1257   703556562 :   (void)new_chunk(lz);
    1258   703556562 :   xd = x + nx;
    1259   703556562 :   yd = y + ny;
    1260   703556562 :   ye = yd; p1 = *--xd;
    1261             : 
    1262   703556562 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1263  3932031597 :   while (yd > y) *--zd = addmul(p1, *--yd);
    1264   703556562 :   *--zd = hiremainder;
    1265             : 
    1266  5045629245 :   while (xd > x)
    1267             :   {
    1268             :     LOCAL_OVERFLOW;
    1269  4342072683 :     yd = ye; p1 = *--xd;
    1270             : 
    1271  4342072683 :     z2d = --z2e;
    1272  4342072683 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1273 45435657498 :     while (yd > y)
    1274             :     {
    1275 41093584815 :       hiremainder += overflow;
    1276 41093584815 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1277             :     }
    1278  4342072683 :     *--zd = hiremainder + overflow;
    1279             :   }
    1280   703556562 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1281   703556562 :   *--zd = evalsigne(1) | evallgefint(lz);
    1282   703556562 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1283   703556562 :   return gc_const((pari_sp)zd, zd);
    1284             : }
    1285             : 
    1286             : INLINE GEN
    1287   560285166 : sqrispec_basecase(GEN x, long nx)
    1288             : {
    1289             :   GEN z2e,z2d,yd,xd,zd,x0,z0;
    1290             :   long p1,lz;
    1291             :   LOCAL_HIREMAINDER;
    1292             :   LOCAL_OVERFLOW;
    1293             : 
    1294   560285166 :   if (nx == 1) return sqru((ulong)*x);
    1295   460646172 :   if (nx == 0) return gen_0;
    1296    80041104 :   zd = (GEN)avma; lz = (nx+1) << 1;
    1297    80041104 :   z0 = new_chunk(lz);
    1298    80041104 :   if (nx == 1)
    1299             :   {
    1300           0 :     *--zd = mulll(*x, *x);
    1301           0 :     *--zd = hiremainder; goto END;
    1302             :   }
    1303    80041104 :   xd = x + nx;
    1304             : 
    1305             :   /* compute double products --> zd */
    1306    80041104 :   p1 = *--xd; yd = xd; --zd;
    1307    80041104 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1308   685005852 :   while (yd > x) *--zd = addmul(p1, *--yd);
    1309    80041104 :   *--zd = hiremainder;
    1310             : 
    1311    80041104 :   x0 = x+1;
    1312   685005852 :   while (xd > x0)
    1313             :   {
    1314             :     LOCAL_OVERFLOW;
    1315   604964748 :     p1 = *--xd; yd = xd;
    1316             : 
    1317   604964748 :     z2e -= 2; z2d = z2e;
    1318   604964748 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1319  5557662387 :     while (yd > x)
    1320             :     {
    1321  4952697639 :       hiremainder += overflow;
    1322  4952697639 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1323             :     }
    1324   604964748 :     *--zd = hiremainder + overflow;
    1325             :   }
    1326             :   /* multiply zd by 2 (put result in zd - 1) */
    1327    80041104 :   zd[-1] = ((*zd & HIGHBIT) != 0);
    1328    80041104 :   shift_left(zd, zd, 0, (nx<<1)-3, 0, 1);
    1329             : 
    1330             :   /* add the squares */
    1331    80041104 :   xd = x + nx; zd = z0 + lz;
    1332    80041104 :   p1 = *--xd;
    1333    80041104 :   zd--; *zd = mulll(p1,p1);
    1334    80041104 :   zd--; *zd = addll(hiremainder, *zd);
    1335   765046956 :   while (xd > x)
    1336             :   {
    1337   685005852 :     p1 = *--xd;
    1338   685005852 :     zd--; *zd = addll(mulll(p1,p1)+ overflow, *zd);
    1339   685005852 :     zd--; *zd = addll(hiremainder + overflow, *zd);
    1340             :   }
    1341             : 
    1342    80041104 : END:
    1343    80041104 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1344    80041104 :   *--zd = evalsigne(1) | evallgefint(lz);
    1345    80041104 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1346    80041104 :   return gc_const((pari_sp)zd, zd);
    1347             : }
    1348             : 
    1349             : /********************************************************************/
    1350             : /**                                                                **/
    1351             : /**               INTEGER MULTIPLICATION (FFT)                     **/
    1352             : /**                                                                **/
    1353             : /********************************************************************/
    1354             : 
    1355             : /*
    1356             :  Compute parameters for FFT:
    1357             :    len: result length
    1358             :    k: FFT depth.
    1359             :    n: number of blocks (2^k)
    1360             :    bs: block size
    1361             :    mod: Modulus is M=2^(BIL*mod)+1
    1362             :    ord: order of 2 in Z/MZ.
    1363             :  We must have:
    1364             :    bs*n >= l
    1365             :    2^(BIL*mod) > nb*2^(2*BIL*bs)
    1366             :    2^k | 2*BIL*mod
    1367             : */
    1368             : static void
    1369       16215 : mulliifft_params(long len, long *k, long *mod, long *bs, long *n, ulong *ord)
    1370             : {
    1371             :   long r;
    1372       16215 :   *k = expu((3*len)>>2)-3;
    1373             :   do {
    1374       16215 :     (*k)--;
    1375       16215 :     r  = *k-(TWOPOTBITS_IN_LONG+2);
    1376       16215 :     *n = 1L<<*k;
    1377       16215 :     *bs = (len+*n-1)>>*k;
    1378       16215 :     *mod= 2**bs+1;
    1379       16215 :     if (r>0)
    1380        1077 :       *mod=((*mod+(1L<<r)-1)>>r)<<r;
    1381       16215 :   } while(*mod>=3**bs);
    1382       16215 :   *ord= 4**mod*BITS_IN_LONG;
    1383       16215 : }
    1384             : 
    1385             : /* Zf_: arithmetic in ring Z/MZ where M= 2^(BITS_IN_LONG*mod)+1
    1386             :  * for some mod.
    1387             :  * Do not garbage collect.
    1388             :  */
    1389             : 
    1390             : static GEN
    1391    38238912 : Zf_add(GEN a, GEN b, GEN M)
    1392             : {
    1393    38238912 :   GEN y, z = addii(a,b);
    1394    38238912 :   long mod = lgefint(M)-3;
    1395    38238912 :   long l = NLIMBS(z);
    1396    38238912 :   if (l<=mod) return z;
    1397    14675301 :   y = subiu(z, 1);
    1398    14675301 :   if (NLIMBS(y)<=mod) return z;
    1399    14675301 :   return int_normalize(y,1);
    1400             : }
    1401             : 
    1402             : static GEN
    1403    39073305 : Zf_sub(GEN a, GEN b, GEN M)
    1404             : {
    1405    39073305 :   GEN z = subii(a,b);
    1406    39073305 :   return signe(z)>=0? z: addii(M,z);
    1407             : }
    1408             : 
    1409             : /* destroy z */
    1410             : static GEN
    1411    81206043 : Zf_red_destroy(GEN z, GEN M)
    1412             : {
    1413    81206043 :   long mod = lgefint(M)-3;
    1414    81206043 :   long l = NLIMBS(z);
    1415             :   GEN y;
    1416    81206043 :   if (l<=mod) return z;
    1417    35982003 :   y = shifti(z, -mod*BITS_IN_LONG);
    1418    35982003 :   z = int_normalize(z, NLIMBS(y));
    1419    35982003 :   y = Zf_red_destroy(y, M);
    1420    35982003 :   z = subii(z, y);
    1421    35982003 :   if (signe(z)<0) z = addii(z, M);
    1422    35982003 :   return z;
    1423             : }
    1424             : 
    1425             : INLINE GEN
    1426    41948136 : Zf_shift(GEN a, ulong s, GEN M) { return Zf_red_destroy(shifti(a, s), M); }
    1427             : 
    1428             : /*
    1429             :  Multiply by sqrt(2)^s
    1430             :  We use the formula sqrt(2)=z_8*(1-z_4)) && z_8=2^(ord/16) [2^(ord/4)+1]
    1431             : */
    1432             : 
    1433             : static GEN
    1434    38238912 : Zf_mulsqrt2(GEN a, ulong s, ulong ord, GEN M)
    1435             : {
    1436    38238912 :   ulong hord = ord>>1;
    1437    38238912 :   if (!signe(a)) return gen_0;
    1438    37003446 :   if (odd(s)) /* Multiply by 2^(s/2) */
    1439             :   {
    1440      834393 :     GEN az8  = Zf_shift(a,   ord>>4, M);
    1441      834393 :     GEN az83 = Zf_shift(az8, ord>>3, M);
    1442      834393 :     a = Zf_sub(az8, az83, M);
    1443      834393 :     s--;
    1444             :   }
    1445    37003446 :   if (s < hord)
    1446    27240021 :     return Zf_shift(a, s>>1, M);
    1447             :   else
    1448     9763425 :     return subii(M,Zf_shift(a, (s-hord)>>1, M));
    1449             : }
    1450             : 
    1451             : INLINE GEN
    1452      332544 : Zf_sqr(GEN a, GEN M) { return Zf_red_destroy(sqri(a), M); }
    1453             : 
    1454             : INLINE GEN
    1455     2943360 : Zf_mul(GEN a, GEN b, GEN M) { return Zf_red_destroy(mulii(a,b), M); }
    1456             : 
    1457             : /* In place, bit reversing FFT */
    1458             : static void
    1459     6188190 : muliifft_dit(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1460             : {
    1461     6188190 :   pari_sp av = avma;
    1462             :   long i;
    1463     6188190 :   ulong j, no = (o<<1)%ord;
    1464     6188190 :   long hstep=step>>1;
    1465    31213086 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1466             :   {
    1467    25024896 :     GEN a = Zf_add(gel(FFT,i), gel(FFT,i+hstep), M);
    1468    25024896 :     GEN b = Zf_mulsqrt2(Zf_sub(gel(FFT,i), gel(FFT,i+hstep), M), j, ord, M);
    1469    25024896 :     affii(a,gel(FFT,i));
    1470    25024896 :     affii(b,gel(FFT,i+hstep));
    1471    25024896 :     set_avma(av);
    1472             :   }
    1473     6188190 :   if (hstep>1)
    1474             :   {
    1475     3078558 :     muliifft_dit(no, ord, M, FFT, d, hstep);
    1476     3078558 :     muliifft_dit(no, ord, M, FFT, d+hstep, hstep);
    1477             :   }
    1478     6188190 : }
    1479             : 
    1480             : /* In place, bit reversed FFT, inverse of muliifft_dit */
    1481             : static void
    1482     3259689 : muliifft_dis(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1483             : {
    1484     3259689 :   pari_sp av = avma;
    1485             :   long i;
    1486     3259689 :   ulong j, no = (o<<1)%ord;
    1487     3259689 :   long hstep=step>>1;
    1488     3259689 :   if (hstep>1)
    1489             :   {
    1490     1621737 :     muliifft_dis(no, ord, M, FFT, d, hstep);
    1491     1621737 :     muliifft_dis(no, ord, M, FFT, d+hstep, hstep);
    1492             :   }
    1493    16473705 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1494             :   {
    1495    13214016 :     GEN z = Zf_mulsqrt2(gel(FFT,i+hstep), j, ord, M);
    1496    13214016 :     GEN a = Zf_add(gel(FFT,i), z, M);
    1497    13214016 :     GEN b = Zf_sub(gel(FFT,i), z, M);
    1498    13214016 :     affii(a,gel(FFT,i));
    1499    13214016 :     affii(b,gel(FFT,i+hstep));
    1500    13214016 :     set_avma(av);
    1501             :   }
    1502     3259689 : }
    1503             : 
    1504             : static GEN
    1505       31074 : muliifft_spliti(GEN a, long na, long bs, long n, long mod)
    1506             : {
    1507       31074 :   GEN ap = a+na-1;
    1508       31074 :   GEN c  = cgetg(n+1, t_VEC);
    1509             :   long i,j;
    1510     6250338 :   for(i=1;i<=n;i++)
    1511             :   {
    1512     6219264 :     GEN z = cgeti(mod+3);
    1513     6219264 :     if (na)
    1514             :     {
    1515     3071541 :       long m = minss(bs, na), v=0;
    1516     3071541 :       GEN zp, aa=ap-m+1;
    1517    21974196 :       while (!*aa && v<m) {aa++; v++;}
    1518     3071541 :       zp = z+m-v+1;
    1519    69115452 :       for (j=v; j < m; j++)
    1520    66043911 :         *zp-- = *ap--;
    1521     3071541 :       ap -= v; na -= m;
    1522     3071541 :       z[1] = evalsigne(m!=v) | evallgefint(m-v+2);
    1523             :     } else
    1524     3147723 :       z[1] = evalsigne(0) | evallgefint(2);
    1525     6219264 :     gel(c, i) = z;
    1526             :   }
    1527       31074 :   return c;
    1528             : }
    1529             : 
    1530             : static GEN
    1531       16215 : muliifft_unspliti(GEN V, long bs, long len)
    1532             : {
    1533       16215 :   long s, i, j, l = lg(V);
    1534       16215 :   GEN a = cgeti(len);
    1535       16215 :   a[1] = evalsigne(1)|evallgefint(len);
    1536    89915601 :   for(i=2;i<len;i++)
    1537    89899386 :     a[i] = 0;
    1538     3292119 :   for(i=1, s=0; i<l; i++, s+=bs)
    1539             :   {
    1540     3275904 :     GEN  u = gel(V,i);
    1541     3275904 :     if (signe(u))
    1542             :     {
    1543     3097512 :       GEN ap = int_W(a,s);
    1544     3097512 :       GEN up = int_LSW(u);
    1545     3097512 :       long lu = NLIMBS(u);
    1546             :       LOCAL_OVERFLOW;
    1547     3097512 :       *ap = addll(*ap, *up--); ap--;
    1548   170621589 :       for (j=1; j<lu; j++)
    1549   167524077 :        { *ap = addllx(*ap, *up--); ap--; }
    1550     3097641 :       while (overflow)
    1551         129 :        { *ap = addll(*ap, 1); ap--; }
    1552             :     }
    1553             :   }
    1554       16215 :   return int_normalize(a,0);
    1555             : }
    1556             : 
    1557             : static GEN
    1558        1356 : sqrispec_fft(GEN a, long na)
    1559             : {
    1560        1356 :   pari_sp av, ltop = avma;
    1561        1356 :   long len = 2*na;
    1562             :   long k, mod, bs, n;
    1563             :   GEN  FFT, M;
    1564             :   long i;
    1565             :   ulong o, ord;
    1566             : 
    1567        1356 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1568        1356 :   o = ord>>k;
    1569        1356 :   M = int2n(mod*BITS_IN_LONG);
    1570        1356 :   M[2+mod] = 1;
    1571        1356 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1572        1356 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1573        1356 :   av = avma;
    1574      333900 :   for(i=1; i<=n; i++)
    1575             :   {
    1576      332544 :     affii(Zf_sqr(gel(FFT,i), M), gel(FFT,i));
    1577      332544 :     set_avma(av);
    1578             :   }
    1579        1356 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1580      333900 :   for(i=1; i<=n; i++)
    1581             :   {
    1582      332544 :     affii(Zf_shift(gel(FFT,i), (ord>>1)-k, M), gel(FFT,i));
    1583      332544 :     set_avma(av);
    1584             :   }
    1585        1356 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1586             : }
    1587             : 
    1588             : static GEN
    1589       14859 : muliispec_fft(GEN a, GEN b, long na, long nb)
    1590             : {
    1591       14859 :   pari_sp av, av2, ltop = avma;
    1592       14859 :   long len = na+nb;
    1593             :   long k, mod, bs, n;
    1594             :   GEN FFT, FFTb, M;
    1595             :   long i;
    1596             :   ulong o, ord;
    1597             : 
    1598       14859 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1599       14859 :   o = ord>>k;
    1600       14859 :   M = int2n(mod*BITS_IN_LONG);
    1601       14859 :   M[2+mod] = 1;
    1602       14859 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1603       14859 :   av=avma;
    1604       14859 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1605       14859 :   FFTb = muliifft_spliti(b, nb, bs, n, mod);
    1606       14859 :   av2 = avma;
    1607       14859 :   muliifft_dit(o, ord, M, FFTb, 0, n);
    1608     2958219 :   for(i=1; i<=n; i++)
    1609             :   {
    1610     2943360 :     affii(Zf_mul(gel(FFT,i), gel(FFTb,i), M), gel(FFT,i));
    1611     2943360 :     set_avma(av2);
    1612             :   }
    1613       14859 :   set_avma(av);
    1614       14859 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1615     2958219 :   for(i=1; i<=n; i++)
    1616             :   {
    1617     2943360 :     affii(Zf_shift(gel(FFT,i),(ord>>1)-k,M), gel(FFT,i));
    1618     2943360 :     set_avma(av);
    1619             :   }
    1620       14859 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1621             : }
    1622             : 
    1623             : /********************************************************************/
    1624             : /**                                                                **/
    1625             : /**               INTEGER MULTIPLICATION (KARATSUBA)               **/
    1626             : /**                                                                **/
    1627             : /********************************************************************/
    1628             : 
    1629             : /* return (x shifted left d words) + y. Assume d > 0, x > 0 and y >= 0 */
    1630             : static GEN
    1631   168047856 : addshiftw(GEN x, GEN y, long d)
    1632             : {
    1633   168047856 :   GEN z,z0,y0,yd, zd = (GEN)avma;
    1634   168047856 :   long a,lz,ly = lgefint(y);
    1635             : 
    1636   168047856 :   z0 = new_chunk(d);
    1637   168047856 :   a = ly-2; yd = y+ly;
    1638   168047856 :   if (a >= d)
    1639             :   {
    1640  3123197694 :     y0 = yd-d; while (yd > y0) *--zd = *--yd; /* copy last d words of y */
    1641   165667053 :     a -= d;
    1642   165667053 :     if (a)
    1643   114428850 :       z = addiispec(LIMBS(x), LIMBS(y), NLIMBS(x), a);
    1644             :     else
    1645    51238203 :       z = icopy(x);
    1646             :   }
    1647             :   else
    1648             :   {
    1649    11723358 :     y0 = yd-a; while (yd > y0) *--zd = *--yd; /* copy last a words of y */
    1650    39440844 :     while (zd > z0) *--zd = 0;    /* complete with 0s */
    1651     2380803 :     z = icopy(x);
    1652             :   }
    1653   168047856 :   lz = lgefint(z)+d;
    1654   168047856 :   z[1] = evalsigne(1) | evallgefint(lz);
    1655   168047856 :   z[0] = evaltyp(t_INT) | evallg(lz); return z;
    1656             : }
    1657             : 
    1658             : /* Fast product (Karatsuba) of integers. a and b are "special" GENs
    1659             :  * c,c0,c1,c2 are genuine GENs.
    1660             :  */
    1661             : GEN
    1662  2628973257 : muliispec(GEN a, GEN b, long na, long nb)
    1663             : {
    1664             :   GEN a0,c,c0;
    1665             :   long n0, n0a, i;
    1666             :   pari_sp av;
    1667             : 
    1668  2628973257 :   if (na < nb) swapspec(a,b, na,nb);
    1669  2628973257 :   if (nb < MULII_KARATSUBA_LIMIT) return muliispec_basecase(a,b,na,nb);
    1670    55830132 :   if (nb >= MULII_FFT_LIMIT)      return muliispec_fft(a,b,na,nb);
    1671    55815273 :   i=(na>>1); n0=na-i; na=i;
    1672    55815273 :   av=avma; a0=a+na; n0a=n0;
    1673   116285445 :   while (n0a && !*a0) { a0++; n0a--; }
    1674             : 
    1675    55815273 :   if (n0a && nb > n0)
    1676    53919489 :   { /* nb <= na <= n0 */
    1677             :     GEN b0,c1,c2;
    1678             :     long n0b;
    1679             : 
    1680    53919489 :     nb -= n0;
    1681    53919489 :     c = muliispec(a,b,na,nb);
    1682    53919489 :     b0 = b+nb; n0b = n0;
    1683   111595401 :     while (n0b && !*b0) { b0++; n0b--; }
    1684    53919489 :     if (n0b)
    1685             :     {
    1686    53345049 :       c0 = muliispec(a0,b0, n0a,n0b);
    1687             : 
    1688    53345049 :       c2 = addiispec(a0,a, n0a,na);
    1689    53345049 :       c1 = addiispec(b0,b, n0b,nb);
    1690    53345049 :       c1 = muliispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1691    53345049 :       c2 = addiispec(LIMBS(c0),LIMBS(c),  NLIMBS(c0),NLIMBS(c));
    1692             : 
    1693    53345049 :       c1 = subiispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1694             :     }
    1695             :     else
    1696             :     {
    1697      574440 :       c0 = gen_0;
    1698      574440 :       c1 = muliispec(a0,b, n0a,nb);
    1699             :     }
    1700    53919489 :     c = addshiftw(c,c1, n0);
    1701             :   }
    1702             :   else
    1703             :   {
    1704     1895784 :     c = muliispec(a,b,na,nb);
    1705     1895784 :     c0 = muliispec(a0,b,n0a,nb);
    1706             :   }
    1707    55815273 :   return gerepileuptoint(av, addshiftw(c,c0, n0));
    1708             : }
    1709             : GEN
    1710      148767 : muluui(ulong x, ulong y, GEN z)
    1711             : {
    1712      148767 :   long t, s = signe(z);
    1713             :   GEN r;
    1714             :   LOCAL_HIREMAINDER;
    1715             : 
    1716      148767 :   if (!x || !y || !signe(z)) return gen_0;
    1717      148470 :   t = mulll(x,y);
    1718      148470 :   if (!hiremainder)
    1719      148470 :     r = muluispec(t, z+2, lgefint(z)-2);
    1720             :   else
    1721             :   {
    1722             :     long tmp[2];
    1723           0 :     tmp[0] = hiremainder;
    1724           0 :     tmp[1] = t;
    1725           0 :     r = muliispec(z+2,tmp,lgefint(z)-2,2);
    1726             :   }
    1727      148470 :   setsigne(r,s); return r;
    1728             : }
    1729             : 
    1730             : #define sqrispec_mirror sqrispec
    1731             : #define muliispec_mirror muliispec
    1732             : 
    1733             : /* x % (2^n), assuming n >= 0 */
    1734             : GEN
    1735    17311029 : remi2n(GEN x, long n)
    1736             : {
    1737    17311029 :   long hi,l,k,lx,ly, sx = signe(x);
    1738             :   GEN z, xd, zd;
    1739             : 
    1740    17311029 :   if (!sx || !n) return gen_0;
    1741             : 
    1742    17290416 :   k = dvmdsBIL(n, &l);
    1743    17290416 :   lx = lgefint(x);
    1744    17290416 :   if (lx < k+3) return icopy(x);
    1745             : 
    1746    17249742 :   xd = x + (lx-k-1);
    1747             :   /* x = |_|...|#|1|...|k| : copy the last l bits of # and the last k words
    1748             :    *            ^--- initial xd  */
    1749    17249742 :   hi = ((ulong)*xd) & ((1UL<<l)-1); /* last l bits of # = top bits of result */
    1750    17249742 :   if (!hi)
    1751             :   { /* strip leading zeroes from result */
    1752      306453 :     xd++; while (k && !*xd) { k--; xd++; }
    1753      301974 :     if (!k) return gen_0;
    1754       90330 :     ly = k+2; xd--;
    1755             :   }
    1756             :   else
    1757    16947768 :     ly = k+3;
    1758             : 
    1759    17038098 :   zd = z = cgeti(ly);
    1760    17038098 :   *++zd = evalsigne(sx) | evallgefint(ly);
    1761    17038098 :   if (hi) *++zd = hi;
    1762    36839631 :   for ( ;k; k--) *++zd = *++xd;
    1763    17038098 :   return z;
    1764             : }
    1765             : 
    1766             : GEN
    1767   563593194 : sqrispec(GEN a, long na)
    1768             : {
    1769             :   GEN a0,c;
    1770             :   long n0, n0a, i;
    1771             :   pari_sp av;
    1772             : 
    1773   563593194 :   if (na < SQRI_KARATSUBA_LIMIT) return sqrispec_basecase(a,na);
    1774     3308028 :   if (na >= SQRI_FFT_LIMIT) return sqrispec_fft(a,na);
    1775     3306672 :   i=(na>>1); n0=na-i; na=i;
    1776     3306672 :   av=avma; a0=a+na; n0a=n0;
    1777     5494602 :   while (n0a && !*a0) { a0++; n0a--; }
    1778     3306672 :   c = sqrispec(a,na);
    1779     3306672 :   if (n0a)
    1780             :   {
    1781     3300717 :     GEN t, c1, c0 = sqrispec(a0,n0a);
    1782             : #if 0
    1783             :     c1 = shifti(muliispec(a0,a, n0a,na),1);
    1784             : #else /* faster */
    1785     3300717 :     t = addiispec(a0,a,n0a,na);
    1786     3300717 :     t = sqrispec(LIMBS(t),NLIMBS(t));
    1787     3300717 :     c1= addiispec(LIMBS(c0),LIMBS(c), NLIMBS(c0), NLIMBS(c));
    1788     3300717 :     c1= subiispec(LIMBS(t),LIMBS(c1), NLIMBS(t), NLIMBS(c1));
    1789             : #endif
    1790     3300717 :     c = addshiftw(c,c1, n0);
    1791     3300717 :     c = addshiftw(c,c0, n0);
    1792             :   }
    1793             :   else
    1794        5955 :     c = addshiftw(c,gen_0,n0<<1);
    1795     3306672 :   return gerepileuptoint(av, c);
    1796             : }
    1797             : 
    1798             : /********************************************************************/
    1799             : /**                                                                **/
    1800             : /**                    KARATSUBA SQUARE ROOT                       **/
    1801             : /**      adapted from Paul Zimmermann's implementation of          **/
    1802             : /**      his algorithm in GMP (mpn_sqrtrem)                        **/
    1803             : /**                                                                **/
    1804             : /********************************************************************/
    1805             : 
    1806             : /* Square roots table */
    1807             : static const unsigned char approx_tab[192] = {
    1808             :   128,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,
    1809             :   143,144,144,145,146,147,148,149,150,150,151,152,153,154,155,155,
    1810             :   156,157,158,159,160,160,161,162,163,163,164,165,166,167,167,168,
    1811             :   169,170,170,171,172,173,173,174,175,176,176,177,178,178,179,180,
    1812             :   181,181,182,183,183,184,185,185,186,187,187,188,189,189,190,191,
    1813             :   192,192,193,193,194,195,195,196,197,197,198,199,199,200,201,201,
    1814             :   202,203,203,204,204,205,206,206,207,208,208,209,209,210,211,211,
    1815             :   212,212,213,214,214,215,215,216,217,217,218,218,219,219,220,221,
    1816             :   221,222,222,223,224,224,225,225,226,226,227,227,228,229,229,230,
    1817             :   230,231,231,232,232,233,234,234,235,235,236,236,237,237,238,238,
    1818             :   239,240,240,241,241,242,242,243,243,244,244,245,245,246,246,247,
    1819             :   247,248,248,249,249,250,250,251,251,252,252,253,253,254,254,255
    1820             : };
    1821             : 
    1822             : /* N[0], assume N[0] >= 2^(BIL-2).
    1823             :  * Return r,s such that s^2 + r = N, 0 <= r <= 2s */
    1824             : static void
    1825    24467355 : p_sqrtu1(ulong *N, ulong *ps, ulong *pr)
    1826             : {
    1827    24467355 :   ulong prec, r, s, q, u, n0 = N[0];
    1828             : 
    1829    24467355 :   q = n0 >> (BITS_IN_LONG - 8);
    1830             :   /* 2^6 = 64 <= q < 256 = 2^8 */
    1831    24467355 :   s = approx_tab[q - 64];                                /* 128 <= s < 255 */
    1832    24467355 :   r = (n0 >> (BITS_IN_LONG - 16)) - s * s;                /* r <= 2*s */
    1833    24467355 :   if (r > (s << 1)) { r -= (s << 1) | 1; s++; }
    1834             : 
    1835             :   /* 8-bit approximation from the high 8-bits of N[0] */
    1836    24467355 :   prec = 8;
    1837    24467355 :   n0 <<= 2 * prec;
    1838    73402065 :   while (2 * prec < BITS_IN_LONG)
    1839             :   { /* invariant: s has prec bits, and r <= 2*s */
    1840    48934710 :     r = (r << prec) + (n0 >> (BITS_IN_LONG - prec));
    1841    48934710 :     n0 <<= prec;
    1842    48934710 :     u = 2 * s;
    1843    48934710 :     q = r / u; u = r - q * u;
    1844    48934710 :     s = (s << prec) + q;
    1845    48934710 :     u = (u << prec) + (n0 >> (BITS_IN_LONG - prec));
    1846    48934710 :     q = q * q;
    1847    48934710 :     r = u - q;
    1848    48934710 :     if (u < q) { s--; r += (s << 1) | 1; }
    1849    48934710 :     n0 <<= prec;
    1850    48934710 :     prec = 2 * prec;
    1851             :   }
    1852    24467355 :   *ps = s;
    1853    24467355 :   *pr = r;
    1854    24467355 : }
    1855             : 
    1856             : /* N[0..1], assume N[0] >= 2^(BIL-2).
    1857             :  * Return 1 if remainder overflows, 0 otherwise */
    1858             : static int
    1859    22805097 : p_sqrtu2(ulong *N, ulong *ps, ulong *pr)
    1860             : {
    1861    22805097 :   ulong cc, qhl, r, s, q, u, n1 = N[1];
    1862             :   LOCAL_OVERFLOW;
    1863             : 
    1864    22805097 :   p_sqrtu1(N, &s, &r); /* r <= 2s */
    1865    34492173 :   qhl = 0; while (r >= s) { qhl++; r -= s; }
    1866             :   /* now r < s < 2^(BIL/2) */
    1867    22805097 :   r = (r << BITS_IN_HALFULONG) | (n1 >> BITS_IN_HALFULONG);
    1868    22805097 :   u = s << 1;
    1869    22805097 :   q = r / u; u = r - q * u;
    1870    22805097 :   q += (qhl & 1) << (BITS_IN_HALFULONG - 1);
    1871    22805097 :   qhl >>= 1;
    1872             :   /* (initial r)<<(BIL/2) + n1>>(BIL/2) = (qhl<<(BIL/2) + q) * 2s + u */
    1873    22805097 :   s = ((s + qhl) << BITS_IN_HALFULONG) + q;
    1874    22805097 :   cc = u >> BITS_IN_HALFULONG;
    1875    22805097 :   r = (u << BITS_IN_HALFULONG) | (n1 & LOWMASK);
    1876    22805097 :   r = subll(r, q * q);
    1877    22805097 :   cc -= overflow + qhl;
    1878             :   /* now subtract 2*q*2^(BIL/2) + 2^BIL if qhl is set */
    1879    22805097 :   if ((long)cc < 0)
    1880             :   {
    1881     6131265 :     if (s) {
    1882     6104037 :       r = addll(r, s);
    1883     6104037 :       cc += overflow;
    1884     6104037 :       s--;
    1885             :     } else {
    1886       27228 :       cc++;
    1887       27228 :       s = ~0UL;
    1888             :     }
    1889     6131265 :     r = addll(r, s);
    1890     6131265 :     cc += overflow;
    1891             :   }
    1892    22805097 :   *ps = s;
    1893    22805097 :   *pr = r; return cc;
    1894             : }
    1895             : 
    1896             : static void
    1897    22331139 : xmpn_zero(GEN x, long n)
    1898             : {
    1899   166938894 :   while (--n >= 0) x[n]=0;
    1900    22331139 : }
    1901             : static void
    1902   239111985 : xmpn_copy(GEN z, GEN x, long n)
    1903             : {
    1904   239111985 :   long k = n;
    1905   988471632 :   while (--k >= 0) z[k] = x[k];
    1906   239111985 : }
    1907             : /* a[0..la-1] * 2^(lb BIL) | b[0..lb-1] */
    1908             : static GEN
    1909   103411410 : catii(GEN a, long la, GEN b, long lb)
    1910             : {
    1911   103411410 :   long l = la + lb + 2;
    1912   103411410 :   GEN z = cgetipos(l);
    1913   103411410 :   xmpn_copy(LIMBS(z), a, la);
    1914   103411410 :   xmpn_copy(LIMBS(z) + la, b, lb);
    1915   103411410 :   return int_normalize(z, 0);
    1916             : }
    1917             : 
    1918             : /* sqrt n[0..1], assume n normalized */
    1919             : static GEN
    1920    22365171 : sqrtispec2(GEN n, GEN *pr)
    1921             : {
    1922             :   ulong s, r;
    1923    22365171 :   int hi = p_sqrtu2((ulong*)n, &s, &r);
    1924    22365171 :   GEN S = utoi(s);
    1925    22365171 :   *pr = hi? uutoi(1,r): utoi(r);
    1926    22365171 :   return S;
    1927             : }
    1928             : 
    1929             : /* sqrt n[0], _dont_ assume n normalized */
    1930             : static GEN
    1931     1662258 : sqrtispec1_sh(GEN n, GEN *pr)
    1932             : {
    1933             :   GEN S;
    1934     1662258 :   ulong r, s, u0 = uel(n,0);
    1935     1662258 :   int sh = bfffo(u0) & ~1UL;
    1936     1662258 :   if (sh) u0 <<= sh;
    1937     1662258 :   p_sqrtu1(&u0, &s, &r);
    1938             :   /* s^2 + r = u0, s < 2^(BIL/2). Rescale back:
    1939             :    * 2^(2k) n = S^2 + R
    1940             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1941     1662258 :   if (sh) {
    1942     1582695 :     int k = sh >> 1;
    1943     1582695 :     ulong s0 = s & ((1L<<k) - 1);
    1944     1582695 :     r += s * (s0<<1);
    1945     1582695 :     s >>= k;
    1946     1582695 :     r >>= sh;
    1947             :   }
    1948     1662258 :   S = utoi(s);
    1949     1662258 :   if (pr) *pr = utoi(r);
    1950     1662258 :   return S;
    1951             : }
    1952             : 
    1953             : /* sqrt n[0..1], _dont_ assume n normalized */
    1954             : static GEN
    1955      439926 : sqrtispec2_sh(GEN n, GEN *pr)
    1956             : {
    1957             :   GEN S;
    1958      439926 :   ulong U[2], r, s, u0 = uel(n,0), u1 = uel(n,1);
    1959      439926 :   int hi, sh = bfffo(u0) & ~1UL;
    1960      439926 :   if (sh) {
    1961      439821 :     u0 = (u0 << sh) | (u1 >> (BITS_IN_LONG-sh));
    1962      439821 :     u1 <<= sh;
    1963             :   }
    1964      439926 :   U[0] = u0;
    1965      439926 :   U[1] = u1; hi = p_sqrtu2(U, &s, &r);
    1966             :   /* s^2 + R = u0|u1. Rescale back:
    1967             :    * 2^(2k) n = S^2 + R
    1968             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1969      439926 :   if (sh) {
    1970      439821 :     int k = sh >> 1;
    1971      439821 :     ulong s0 = s & ((1L<<k) - 1);
    1972             :     LOCAL_HIREMAINDER;
    1973             :     LOCAL_OVERFLOW;
    1974      439821 :     r = addll(r, mulll(s, (s0<<1)));
    1975      439821 :     if (overflow) hiremainder++;
    1976      439821 :     hiremainder += hi; /* + 0 or 1 */
    1977      439821 :     s >>= k;
    1978      439821 :     r = (r>>sh) | (hiremainder << (BITS_IN_LONG-sh));
    1979      439821 :     hi = (hiremainder & (1L<<sh));
    1980             :   }
    1981      439926 :   S = utoi(s);
    1982      439926 :   if (pr) *pr = hi? uutoi(1,r): utoi(r);
    1983      439926 :   return S;
    1984             : }
    1985             : 
    1986             : /* Let N = N[0..2n-1]. Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S
    1987             :  * Assume N normalized */
    1988             : static GEN
    1989    74070876 : sqrtispec(GEN N, long n, GEN *r)
    1990             : {
    1991             :   GEN S, R, q, z, u;
    1992             :   long l, h;
    1993             : 
    1994    74070876 :   if (n == 1) return sqrtispec2(N, r);
    1995    51705705 :   l = n >> 1;
    1996    51705705 :   h = n - l; /* N = a3(h) | a2(h) | a1(l) | a0(l words) */
    1997    51705705 :   S = sqrtispec(N, h, &R); /* S^2 + R = a3|a2 */
    1998             : 
    1999    51705705 :   z = catii(LIMBS(R), NLIMBS(R), N + 2*h, l); /* = R | a1(l) */
    2000    51705705 :   q = dvmdii(z, shifti(S,1), &u);
    2001    51705705 :   z = catii(LIMBS(u), NLIMBS(u), N + n + h, l); /* = u | a0(l) */
    2002             : 
    2003    51705705 :   S = addshiftw(S, q, l);
    2004    51705705 :   R = subii(z, sqri(q));
    2005    51705705 :   if (signe(R) < 0)
    2006             :   {
    2007     8331684 :     GEN S2 = shifti(S,1);
    2008     8331684 :     R = addis(subiispec(LIMBS(S2),LIMBS(R), NLIMBS(S2),NLIMBS(R)), -1);
    2009     8331684 :     S = addis(S, -1);
    2010             :   }
    2011    51705705 :   *r = R; return S;
    2012             : }
    2013             : 
    2014             : /* Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S.
    2015             :  * As for dvmdii, R is last on stack and guaranteed to be gen_0 in case the
    2016             :  * remainder is 0. R = NULL is allowed. */
    2017             : GEN
    2018     2136270 : sqrtremi(GEN N, GEN *r)
    2019             : {
    2020             :   pari_sp av;
    2021     2136270 :   GEN S, R, n = N+2;
    2022     2136270 :   long k, l2, ln = NLIMBS(N);
    2023             :   int sh;
    2024             : 
    2025     2136270 :   if (ln <= 2)
    2026             :   {
    2027     2102238 :     if (ln == 2) return sqrtispec2_sh(n, r);
    2028     1662312 :     if (ln == 1) return sqrtispec1_sh(n, r);
    2029          54 :     if (r) *r = gen_0;
    2030          54 :     return gen_0;
    2031             :   }
    2032       34032 :   av = avma;
    2033       34032 :   sh = bfffo(n[0]) >> 1;
    2034       34032 :   l2 = (ln + 1) >> 1;
    2035       34032 :   if (sh || (ln & 1)) { /* normalize n, so that n[0] >= 2^BIL / 4 */
    2036       33882 :     GEN s0, t = new_chunk(ln + 1);
    2037       33882 :     t[ln] = 0;
    2038       33882 :     if (sh)
    2039       33525 :       shift_left(t, n, 0,ln-1, 0, sh << 1);
    2040             :     else
    2041         357 :       xmpn_copy(t, n, ln);
    2042       33882 :     S = sqrtispec(t, l2, &R); /* t normalized, 2 * l2 words */
    2043             :     /* Rescale back:
    2044             :      * 2^(2k) n = S^2 + R, k = sh + (ln & 1)*BIL/2
    2045             :      * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    2046       33882 :     k = sh + (ln & 1) * (BITS_IN_LONG/2);
    2047       33882 :     s0 = remi2n(S, k);
    2048       33882 :     R = addii(shifti(R,-1), mulii(s0, S));
    2049       33882 :     R = shifti(R, 1 - (k<<1));
    2050       33882 :     S = shifti(S, -k);
    2051             :   }
    2052             :   else
    2053         150 :     S = sqrtispec(n, l2, &R);
    2054             : 
    2055       34032 :   if (!r) { set_avma((pari_sp)S); return gerepileuptoint(av, S); }
    2056        6969 :   gerepileall(av, 2, &S, &R); *r = R; return S;
    2057             : }
    2058             : 
    2059             : /* compute sqrt(|a|), assuming a != 0 */
    2060             : 
    2061             : #if 1
    2062             : GEN
    2063    22331139 : sqrtr_abs(GEN x)
    2064             : {
    2065    22331139 :   long l = realprec(x) - 2, e = expo(x), er = e>>1;
    2066    22331139 :   GEN b, c, res = cgetr(2 + l);
    2067    22331139 :   res[1] = evalsigne(1) | evalexpo(er);
    2068    22331139 :   if (e&1) {
    2069     9957669 :     b = new_chunk(l << 1);
    2070     9957669 :     xmpn_copy(b, x+2, l);
    2071     9957669 :     xmpn_zero(b + l,l);
    2072     9957669 :     b = sqrtispec(b, l, &c);
    2073     9957669 :     xmpn_copy(res+2, b+2, l);
    2074     9957669 :     if (cmpii(c, b) > 0) roundr_up_ip(res, l+2);
    2075             :   } else {
    2076             :     ulong u;
    2077    12373470 :     b = new_chunk(2 + (l << 1));
    2078    12373470 :     shift_left(b+1, x+2, 0,l-1, 0, BITS_IN_LONG-1);
    2079    12373470 :     b[0] = uel(x,2)>>1;
    2080    12373470 :     xmpn_zero(b + l+1,l+1);
    2081    12373470 :     b = sqrtispec(b, l+1, &c);
    2082    12373470 :     xmpn_copy(res+2, b+2, l);
    2083    12373470 :     u = uel(b,l+2);
    2084    12373470 :     if ( u&HIGHBIT || (u == ~HIGHBIT && cmpii(c,b) > 0))
    2085     6074175 :       roundr_up_ip(res, l+2);
    2086             :   }
    2087    22331139 :   return gc_const((pari_sp)res, res);
    2088             : }
    2089             : 
    2090             : #else /* use t_REAL: currently much slower (quadratic division) */
    2091             : 
    2092             : #ifdef LONG_IS_64BIT
    2093             : /* 64 bits of b = sqrt(a[0] * 2^64 + a[1])  [ up to 1ulp ] */
    2094             : static ulong
    2095             : sqrtu2(ulong *a)
    2096             : {
    2097             :   ulong c, b = dblmantissa( sqrt((double)a[0]) );
    2098             :   LOCAL_HIREMAINDER;
    2099             :   LOCAL_OVERFLOW;
    2100             : 
    2101             :   /* > 32 correct bits, 1 Newton iteration to reach 64 */
    2102             :   if (b <= a[0]) return HIGHBIT | (a[0] >> 1);
    2103             :   hiremainder = a[0]; c = divll(a[1], b);
    2104             :   return (addll(c, b) >> 1) | HIGHBIT;
    2105             : }
    2106             : /* 64 bits of sqrt(a[0] * 2^63) */
    2107             : static ulong
    2108             : sqrtu2_1(ulong *a)
    2109             : {
    2110             :   ulong t[2];
    2111             :   t[0] = (a[0] >> 1);
    2112             :   t[1] = (a[0] << (BITS_IN_LONG-1)) | (a[1] >> 1);
    2113             :   return sqrtu2(t);
    2114             : }
    2115             : #else
    2116             : /* 32 bits of sqrt(a[0] * 2^32) */
    2117             : static ulong
    2118             : sqrtu2(ulong *a)   { return dblmantissa( sqrt((double)a[0]) ); }
    2119             : /* 32 bits of sqrt(a[0] * 2^31) */
    2120             : static ulong
    2121             : sqrtu2_1(ulong *a) { return dblmantissa( sqrt(2. * a[0]) ); }
    2122             : #endif
    2123             : 
    2124             : GEN
    2125             : sqrtr_abs(GEN x)
    2126             : {
    2127             :   long l1, i, l = lg(x), ex = expo(x);
    2128             :   GEN a, t, y = cgetr(l);
    2129             :   pari_sp av, av0 = avma;
    2130             : 
    2131             :   a = rtor(x, l+1);
    2132             :   t = cgetr(l+1);
    2133             :   if (ex & 1) { /* odd exponent */
    2134             :     a[1] = evalsigne(1) | _evalexpo(1);
    2135             :     t[2] = (long)sqrtu2((ulong*)a + 2);
    2136             :   } else { /* even exponent */
    2137             :     a[1] = evalsigne(1) | _evalexpo(0);
    2138             :     t[2] = (long)sqrtu2_1((ulong*)a + 2);
    2139             :   }
    2140             :   t[1] = evalsigne(1) | _evalexpo(0);
    2141             :   for (i = 3; i <= l; i++) t[i] = 0;
    2142             : 
    2143             :   /* |x| = 2^(ex/2) a, t ~ sqrt(a) */
    2144             :   l--; l1 = 1; av = avma;
    2145             :   while (l1 < l) { /* let t := (t + a/t)/2 */
    2146             :     l1 <<= 1; if (l1 > l) l1 = l;
    2147             :     setlg(a, l1 + 2);
    2148             :     setlg(t, l1 + 2);
    2149             :     affrr(addrr(t, divrr(a,t)), t); shiftr_inplace(t, -1);
    2150             :     set_avma(av);
    2151             :   }
    2152             :   affrr(t,y); shiftr_inplace(y, (ex>>1));
    2153             :   return gc_const(av0, y);
    2154             : }
    2155             : 
    2156             : #endif
    2157             : 
    2158             : /*******************************************************************
    2159             :  *                                                                 *
    2160             :  *                           Base Conversion                       *
    2161             :  *                                                                 *
    2162             :  *******************************************************************/
    2163             : 
    2164             : static void
    2165      688722 : convi_dac(GEN x, ulong l, ulong *res)
    2166             : {
    2167      688722 :   pari_sp ltop=avma;
    2168             :   ulong m;
    2169             :   GEN x1,x2;
    2170      688722 :   if (l==1) { *res=itou(x); return; }
    2171      327195 :   m=l>>1;
    2172      327195 :   x1=dvmdii(x,powuu(1000000000UL,m),&x2);
    2173      327195 :   convi_dac(x1,l-m,res+m);
    2174      327195 :   convi_dac(x2,m,res);
    2175      327195 :   set_avma(ltop);
    2176             : }
    2177             : 
    2178             : /* convert integer --> base 10^9 [not memory clean] */
    2179             : ulong *
    2180      285397 : convi(GEN x, long *l)
    2181             : {
    2182      285397 :   long lz, lx = lgefint(x);
    2183             :   ulong *z;
    2184      285397 :   if (lx == 3 && uel(x,2) < 1000000000UL) {
    2185      251065 :     z = (ulong*)new_chunk(1);
    2186      251065 :     *z = x[2];
    2187      251065 :     *l = 1; return z+1;
    2188             :   }
    2189       34332 :   lz = 1 + (long)bit_accuracy_mul(lx, LOG10_2/9);
    2190       34332 :   z = (ulong*)new_chunk(lz);
    2191       34332 :   convi_dac(x,(ulong)lz,z);
    2192       62568 :   while (z[lz-1]==0) lz--;
    2193       34332 :   *l=lz; return z+lz;
    2194             : }
    2195             : 

Generated by: LCOV version 1.13