Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - kernel/none - mp.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.11.0 lcov report (development 22860-5579deb0b) Lines: 1119 1151 97.2 %
Date: 2018-07-18 05:36:42 Functions: 69 69 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         653 : void pari_kernel_init(void) { }
      25         651 : void pari_kernel_close(void) { }
      26             : 
      27             : /* NOTE: arguments of "spec" routines (muliispec, addiispec, etc.) aren't
      28             :  * GENs but pairs (long *a, long na) representing a list of digits (in basis
      29             :  * BITS_IN_LONG) : a[0], ..., a[na-1]. [ In ordre to facilitate splitting: no
      30             :  * need to reintroduce codewords ] */
      31             : 
      32             : #define LIMBS(x)  ((x)+2)
      33             : #define NLIMBS(x) (lgefint(x)-2)
      34             : 
      35             : /* Normalize a non-negative integer */
      36             : GEN
      37   166571007 : int_normalize(GEN x, long known_zero_words)
      38             : {
      39   166571007 :   long i, lx = lgefint(x);
      40             :   GEN x0;
      41   166571007 :   if (lx == 2) { x[1] = evalsigne(0) | evallgefint(2); return x; }
      42   166571007 :   if (!known_zero_words && x[2]) return x;
      43   721246908 :   for (i = 2+known_zero_words; i < lx; i++)
      44   700674096 :     if (x[i]) break;
      45    79309707 :   x0 = x; i -= 2; x += i;
      46    79309707 :   if (x0 == (GEN)avma) avma = (pari_sp)x;
      47    32870799 :   else stackdummy((pari_sp)(x0+i), (pari_sp)x0);
      48    79309707 :   lx -= i;
      49    79309707 :   x[0] = evaltyp(t_INT) | evallg(lx);
      50    79309707 :   if (lx == 2) x[1] = evalsigne(0) | evallgefint(lx);
      51    58736895 :   else         x[1] = evalsigne(1) | evallgefint(lx);
      52    79309707 :   return x;
      53             : }
      54             : 
      55             : /***********************************************************************/
      56             : /**                                                                   **/
      57             : /**                      ADDITION / SUBTRACTION                       **/
      58             : /**                                                                   **/
      59             : /***********************************************************************/
      60             : 
      61             : GEN
      62     2174928 : setloop(GEN a)
      63             : {
      64     2174928 :   pari_sp av = avma;
      65     2174928 :   (void)cgetg(lgefint(a) + 3, t_VECSMALL);
      66     2174928 :   return icopy_avma(a, av); /* two cells of extra space before a */
      67             : }
      68             : 
      69             : /* we had a = setloop(?), then some incloops. Reset a to b */
      70             : GEN
      71      127959 : resetloop(GEN a, GEN b) {
      72      127959 :   long lb = lgefint(b);
      73      127959 :   a += lgefint(a) - lb;
      74      127959 :   a[0] = evaltyp(t_INT) | evallg(lb);
      75      127959 :   affii(b, a); return a;
      76             : }
      77             : 
      78             : /* assume a > 0, initialized by setloop. Do a++ */
      79             : static GEN
      80    20167923 : incpos(GEN a)
      81             : {
      82    20167923 :   long i, l = lgefint(a);
      83    20167926 :   for (i=l-1; i>1; i--)
      84    20167923 :     if (++a[i]) return a;
      85           3 :   l++; a--; /* use extra cell */
      86           3 :   a[0]=evaltyp(t_INT) | _evallg(l);
      87           3 :   a[1]=evalsigne(1) | evallgefint(l);
      88           3 :   a[2]=1; return a;
      89             : }
      90             : 
      91             : /* assume a < 0, initialized by setloop. Do a++ */
      92             : static GEN
      93        4704 : incneg(GEN a)
      94             : {
      95        4704 :   long i, l = lgefint(a)-1;
      96        4704 :   if (uel(a,l)--)
      97             :   {
      98        4701 :     if (l == 2 && !a[2])
      99             :     {
     100         417 :       a++; /* save one cell */
     101         417 :       a[0] = evaltyp(t_INT) | _evallg(2);
     102         417 :       a[1] = evalsigne(0) | evallgefint(2);
     103             :     }
     104        4701 :     return a;
     105             :   }
     106           3 :   for (i = l-1;; i--) /* finishes since a[2] != 0 */
     107           3 :     if (uel(a,i)--) break;
     108           3 :   if (!a[2])
     109             :   {
     110           3 :     a++; /* save one cell */
     111           3 :     a[0] = evaltyp(t_INT) | _evallg(l);
     112           3 :     a[1] = evalsigne(-1) | evallgefint(l);
     113             :   }
     114           3 :   return a;
     115             : }
     116             : 
     117             : /* assume a initialized by setloop. Do a++ */
     118             : GEN
     119    20421345 : incloop(GEN a)
     120             : {
     121    20421345 :   switch(signe(a))
     122             :   {
     123      248718 :     case 0: a--; /* use extra cell */
     124      248718 :       a[0]=evaltyp(t_INT) | _evallg(3);
     125      248718 :       a[1]=evalsigne(1) | evallgefint(3);
     126      248718 :       a[2]=1; return a;
     127        4704 :     case -1: return incneg(a);
     128    20167923 :     default: return incpos(a);
     129             :   }
     130             : }
     131             : 
     132             : INLINE GEN
     133   886203018 : adduispec(ulong s, GEN x, long nx)
     134             : {
     135   886203018 :   GEN xd, zd = (GEN)avma;
     136             :   long lz;
     137             : 
     138   886203018 :   if (nx == 1) return adduu(s, uel(x,0));
     139   112303014 :   lz = nx+3; (void)new_chunk(lz);
     140   112303014 :   xd = x + nx;
     141   112303014 :   *--zd = (ulong)*--xd + s;
     142   112303014 :   if ((ulong)*zd < s)
     143             :     for(;;)
     144             :     {
     145    21721248 :       if (xd == x) { *--zd = 1; break; } /* enlarge z */
     146    12290100 :       *--zd = ((ulong)*--xd) + 1;
     147    12290100 :       if (*zd) { lz--; break; }
     148             :     }
     149   102222792 :   else lz--;
     150   112303014 :   while (xd > x) *--zd = *--xd;
     151   112303014 :   *--zd = evalsigne(1) | evallgefint(lz);
     152   112303014 :   *--zd = evaltyp(t_INT) | evallg(lz);
     153   112303014 :   avma=(pari_sp)zd; return zd;
     154             : }
     155             : 
     156             : GEN
     157   138834495 : adduispec_offset(ulong s, GEN x, long offset, long nx)
     158             : {
     159   138834495 :   GEN xd = x+lgefint(x)-nx-offset;
     160   138834495 :   while (nx && *xd==0) {xd++; nx--;}
     161   138834495 :   if (!nx) return utoi(s);
     162   127748490 :   return adduispec(s,xd,nx);
     163             : }
     164             : 
     165             : static GEN
     166  1761349103 : addiispec(GEN x, GEN y, long nx, long ny)
     167             : {
     168             :   GEN xd, yd, zd;
     169  1761349103 :   long lz, i = -2;
     170             :   LOCAL_OVERFLOW;
     171             : 
     172  1761349103 :   if (nx < ny) swapspec(x,y, nx,ny);
     173  1761349103 :   if (ny == 1) return adduispec(*y,x,nx);
     174  1045307316 :   zd = (GEN)avma;
     175  1045307316 :   lz = nx+3; (void)new_chunk(lz);
     176  1045307316 :   xd = x + nx;
     177  1045307316 :   yd = y + ny;
     178  1045307316 :   zd[-1] = addll(xd[-1], yd[-1]);
     179             : #ifdef addllx8
     180   674115436 :   for (  ; i-8 > -ny; i-=8)
     181   325679664 :     addllx8(xd+i, yd+i, zd+i, overflow);
     182             : #endif
     183  1045307316 :   for (  ; i >= -ny; i--) zd[i] = addllx(xd[i], yd[i]);
     184  1045307316 :   if (overflow)
     185             :     for(;;)
     186             :     {
     187   160033053 :       if (i < -nx) { zd[i] = 1; i--; break; } /* enlarge z */
     188    92299254 :       zd[i] = uel(xd,i) + 1;
     189    92299254 :       if (zd[i]) { i--; lz--; break; }
     190    31633812 :       i--;
     191             :     }
     192   948541887 :   else lz--;
     193  1045307316 :   for (; i >= -nx; i--) zd[i] = xd[i];
     194  1045307316 :   zd += i+1;
     195  1045307316 :   *--zd = evalsigne(1) | evallgefint(lz);
     196  1045307316 :   *--zd = evaltyp(t_INT) | evallg(lz);
     197  1045307316 :   avma=(pari_sp)zd; return zd;
     198             : }
     199             : 
     200             : /* assume x >= s */
     201             : INLINE GEN
     202   682452975 : subiuspec(GEN x, ulong s, long nx)
     203             : {
     204   682452975 :   GEN xd, zd = (GEN)avma;
     205             :   long lz;
     206             :   LOCAL_OVERFLOW;
     207             : 
     208   682452975 :   if (nx == 1) return utoi(x[0] - s);
     209             : 
     210    82431827 :   lz = nx+2; (void)new_chunk(lz);
     211    82431827 :   xd = x + nx;
     212    82431827 :   *--zd = subll(*--xd, s);
     213    82431827 :   if (overflow)
     214             :     for(;;)
     215             :     {
     216    39476284 :       *--zd = ((ulong)*--xd) - 1;
     217    36667888 :       if (*xd) break;
     218             :     }
     219    82431827 :   if (xd == x)
     220    33307078 :     while (*zd == 0) { zd++; lz--; } /* shorten z */
     221             :   else
     222   617637365 :     do  *--zd = *--xd; while (xd > x);
     223    82431827 :   *--zd = evalsigne(1) | evallgefint(lz);
     224    82431827 :   *--zd = evaltyp(t_INT) | evallg(lz);
     225    82431827 :   avma=(pari_sp)zd; return zd;
     226             : }
     227             : 
     228             : /* assume x > y */
     229             : static GEN
     230  1341418812 : subiispec(GEN x, GEN y, long nx, long ny)
     231             : {
     232             :   GEN xd,yd,zd;
     233  1341418812 :   long lz, i = -2;
     234             :   LOCAL_OVERFLOW;
     235             : 
     236  1341418812 :   if (ny==1) return subiuspec(x,*y,nx);
     237   683111073 :   zd = (GEN)avma;
     238   683111073 :   lz = nx+2; (void)new_chunk(lz);
     239   683111073 :   xd = x + nx;
     240   683111073 :   yd = y + ny;
     241   683111073 :   zd[-1] = subll(xd[-1], yd[-1]);
     242             : #ifdef subllx8
     243   490840757 :   for (  ; i-8 > -ny; i-=8)
     244   263137065 :     subllx8(xd+i, yd+i, zd+i, overflow);
     245             : #endif
     246   683111073 :   for (  ; i >= -ny; i--) zd[i] = subllx(xd[i], yd[i]);
     247   683111073 :   if (overflow)
     248             :     for(;;)
     249             :     {
     250   250753610 :       zd[i] = uel(xd,i) - 1;
     251   156754736 :       if (xd[i--]) break;
     252             :     }
     253   683111073 :   if (i>=-nx)
     254    80302106 :     for (; i >= -nx; i--) zd[i] = xd[i];
     255             :   else
     256   602808967 :     while (zd[i+1] == 0) { i++; lz--; } /* shorten z */
     257   683111073 :   zd += i+1;
     258   683111073 :   *--zd = evalsigne(1) | evallgefint(lz);
     259   683111073 :   *--zd = evaltyp(t_INT) | evallg(lz);
     260   683111073 :   avma=(pari_sp)zd; return zd;
     261             : }
     262             : 
     263             : static void
     264   253635509 : roundr_up_ip(GEN x, long l)
     265             : {
     266   253635509 :   long i = l;
     267             :   for(;;)
     268             :   {
     269   254867135 :     if (++uel(x,--i)) break;
     270      695793 :     if (i == 2) { x[2] = (long)HIGHBIT; shiftr_inplace(x, 1); break; }
     271             :   }
     272   253635509 : }
     273             : 
     274             : void
     275   103386921 : affir(GEN x, GEN y)
     276             : {
     277   103386921 :   const long s = signe(x), ly = lg(y);
     278             :   long lx, sh, i;
     279             : 
     280   103386921 :   if (!s)
     281             :   {
     282     3857943 :     y[1] = evalexpo(-prec2nbits(ly));
     283     3857943 :     return;
     284             :   }
     285             : 
     286    99528978 :   lx = lgefint(x); sh = bfffo(x[2]);
     287    99528978 :   y[1] = evalsigne(s) | evalexpo(bit_accuracy(lx)-sh-1);
     288    99528978 :   if (sh) {
     289    98828760 :     if (lx <= ly)
     290             :     {
     291    64585437 :       for (i=lx; i<ly; i++) y[i]=0;
     292    64585437 :       shift_left(y,x,2,lx-1, 0,sh);
     293    64585437 :       return;
     294             :     }
     295    34243323 :     shift_left(y,x,2,ly-1, x[ly],sh);
     296             :     /* lx > ly: round properly */
     297    34243323 :     if ((uel(x,ly)<<sh) & HIGHBIT) roundr_up_ip(y, ly);
     298             :   }
     299             :   else {
     300      700218 :     if (lx <= ly)
     301             :     {
     302      383298 :       for (i=2; i<lx; i++) y[i]=x[i];
     303      383298 :       for (   ; i<ly; i++) y[i]=0;
     304      383298 :       return;
     305             :     }
     306      316920 :     for (i=2; i<ly; i++) y[i]=x[i];
     307             :     /* lx > ly: round properly */
     308      316920 :     if (uel(x,ly) & HIGHBIT) roundr_up_ip(y, ly);
     309             :   }
     310             : }
     311             : 
     312             : INLINE GEN
     313   406123133 : shiftispec(GEN x, long nx, long n)
     314             : {
     315             :   long ny, i, m;
     316             :   GEN y, yd;
     317   406123133 :   if (!n)  return icopyspec(x, nx);
     318             : 
     319   385743746 :   if (n > 0)
     320             :   {
     321   243815823 :     GEN z = (GEN)avma;
     322   243815823 :     long d = dvmdsBIL(n, &m);
     323             : 
     324   243815823 :     ny = nx+d; y = new_chunk(ny + 2); yd = y + 2;
     325   243815823 :     for ( ; d; d--) *--z = 0;
     326   243815823 :     if (!m) for (i=0; i<nx; i++) yd[i]=x[i];
     327             :     else
     328             :     {
     329   240756621 :       register const ulong sh = BITS_IN_LONG - m;
     330   240756621 :       shift_left(yd,x, 0,nx-1, 0,m);
     331   240756621 :       i = uel(x,0) >> sh;
     332             :       /* Extend y on the left? */
     333   240756621 :       if (i) { ny++; y = new_chunk(1); y[2] = i; }
     334             :     }
     335             :   }
     336             :   else
     337             :   {
     338   141927923 :     ny = nx - dvmdsBIL(-n, &m);
     339   141927923 :     if (ny<1) return gen_0;
     340   141697997 :     y = new_chunk(ny + 2); yd = y + 2;
     341   141697997 :     if (m) {
     342   119292740 :       shift_right(yd,x, 0,ny, 0,m);
     343   119292740 :       if (yd[0] == 0)
     344             :       {
     345     8139273 :         if (ny==1) { avma = (pari_sp)(y+3); return gen_0; }
     346     7104744 :         ny--; avma = (pari_sp)(++y);
     347             :       }
     348             :     } else {
     349    22405257 :       for (i=0; i<ny; i++) yd[i]=x[i];
     350             :     }
     351             :   }
     352   384479291 :   y[1] = evalsigne(1)|evallgefint(ny + 2);
     353   384479291 :   y[0] = evaltyp(t_INT)|evallg(ny + 2); return y;
     354             : }
     355             : 
     356             : GEN
     357    11740200 : mantissa2nr(GEN x, long n)
     358             : { /*This is a kludge since x is not an integer*/
     359    11740200 :   long s = signe(x);
     360             :   GEN y;
     361             : 
     362    11740200 :   if(s == 0) return gen_0;
     363    11739330 :   y = shiftispec(x + 2, lg(x) - 2, n);
     364    11739330 :   if (signe(y)) setsigne(y, s);
     365    11739330 :   return y;
     366             : }
     367             : 
     368             : GEN
     369     1438968 : truncr(GEN x)
     370             : {
     371             :   long d,e,i,s,m;
     372             :   GEN y;
     373             : 
     374     1438968 :   if ((s=signe(x)) == 0 || (e=expo(x)) < 0) return gen_0;
     375     1167501 :   d = nbits2lg(e+1); m = remsBIL(e);
     376     1167501 :   if (d > lg(x)) pari_err_PREC( "truncr (precision loss in truncation)");
     377             : 
     378     1167501 :   y=cgeti(d); y[1] = evalsigne(s) | evallgefint(d);
     379     1167501 :   if (++m == BITS_IN_LONG)
     380          90 :     for (i=2; i<d; i++) y[i]=x[i];
     381             :   else
     382     1167411 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     383     1167501 :   return y;
     384             : }
     385             : 
     386             : /* integral part */
     387             : GEN
     388      542850 : floorr(GEN x)
     389             : {
     390             :   long d,e,i,lx,m;
     391             :   GEN y;
     392             : 
     393      542850 :   if (signe(x) >= 0) return truncr(x);
     394      179631 :   if ((e=expo(x)) < 0) return gen_m1;
     395       67167 :   d = nbits2lg(e+1); m = remsBIL(e);
     396       67167 :   lx=lg(x); if (d>lx) pari_err_PREC( "floorr (precision loss in truncation)");
     397       67167 :   y = new_chunk(d);
     398       67167 :   if (++m == BITS_IN_LONG)
     399             :   {
     400           6 :     for (i=2; i<d; i++) y[i]=x[i];
     401           6 :     i=d; while (i<lx && !x[i]) i++;
     402           6 :     if (i==lx) goto END;
     403             :   }
     404             :   else
     405             :   {
     406       67161 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     407       67161 :     if (uel(x,d-1)<<m == 0)
     408             :     {
     409       16362 :       i=d; while (i<lx && !x[i]) i++;
     410       16362 :       if (i==lx) goto END;
     411             :     }
     412             :   }
     413             :   /* set y:=y+1 */
     414       58746 :   for (i=d-1; i>=2; i--) { uel(y,i)++; if (y[i]) goto END; }
     415           0 :   y=new_chunk(1); y[2]=1; d++;
     416             : END:
     417       67167 :   y[1] = evalsigne(-1) | evallgefint(d);
     418       67167 :   y[0] = evaltyp(t_INT) | evallg(d); return y;
     419             : }
     420             : 
     421             : INLINE int
     422  1751754782 : cmpiispec(GEN x, GEN y, long lx, long ly)
     423             : {
     424             :   long i;
     425  1751754782 :   if (lx < ly) return -1;
     426  1676226151 :   if (lx > ly) return  1;
     427  1551029480 :   i = 0; while (i<lx && x[i]==y[i]) i++;
     428  1551029480 :   if (i==lx) return 0;
     429  1451388007 :   return (uel(x,i) > uel(y,i))? 1: -1;
     430             : }
     431             : 
     432             : INLINE int
     433    89084255 : equaliispec(GEN x, GEN y, long lx, long ly)
     434             : {
     435             :   long i;
     436    89084255 :   if (lx != ly) return 0;
     437    89084222 :   i = ly-1; while (i>=0 && x[i]==y[i]) i--;
     438    89084222 :   return i < 0;
     439             : }
     440             : 
     441             : /***********************************************************************/
     442             : /**                                                                   **/
     443             : /**                          MULTIPLICATION                           **/
     444             : /**                                                                   **/
     445             : /***********************************************************************/
     446             : /* assume ny > 0 */
     447             : INLINE GEN
     448  1782234751 : muluispec(ulong x, GEN y, long ny)
     449             : {
     450  1782234751 :   GEN yd, z = (GEN)avma;
     451  1782234751 :   long lz = ny+3;
     452             :   LOCAL_HIREMAINDER;
     453             : 
     454  1782234751 :   (void)new_chunk(lz);
     455  1782234751 :   yd = y + ny; *--z = mulll(x, *--yd);
     456  1782234751 :   while (yd > y) *--z = addmul(x,*--yd);
     457  1782234751 :   if (hiremainder) *--z = hiremainder; else lz--;
     458  1782234751 :   *--z = evalsigne(1) | evallgefint(lz);
     459  1782234751 :   *--z = evaltyp(t_INT) | evallg(lz);
     460  1782234751 :   avma=(pari_sp)z; return z;
     461             : }
     462             : 
     463             : /* a + b*|Y| */
     464             : GEN
     465      534984 : addumului(ulong a, ulong b, GEN Y)
     466             : {
     467             :   GEN yd,y,z;
     468             :   long ny,lz;
     469             :   LOCAL_HIREMAINDER;
     470             :   LOCAL_OVERFLOW;
     471             : 
     472      534984 :   if (!b || !signe(Y)) return utoi(a);
     473             : 
     474      534981 :   y = LIMBS(Y); z = (GEN)avma;
     475      534981 :   ny = NLIMBS(Y);
     476      534981 :   lz = ny+3;
     477             : 
     478      534981 :   (void)new_chunk(lz);
     479      534981 :   yd = y + ny; *--z = addll(a, mulll(b, *--yd));
     480      534981 :   if (overflow) hiremainder++; /* can't overflow */
     481      534981 :   while (yd > y) *--z = addmul(b,*--yd);
     482      534981 :   if (hiremainder) *--z = hiremainder; else lz--;
     483      534981 :   *--z = evalsigne(1) | evallgefint(lz);
     484      534981 :   *--z = evaltyp(t_INT) | evallg(lz);
     485      534981 :   avma=(pari_sp)z; return z;
     486             : }
     487             : 
     488             : /***********************************************************************/
     489             : /**                                                                   **/
     490             : /**                          DIVISION                                 **/
     491             : /**                                                                   **/
     492             : /***********************************************************************/
     493             : 
     494             : ulong
     495  1025915353 : umodiu(GEN y, ulong x)
     496             : {
     497  1025915353 :   long sy=signe(y),ly,i;
     498             :   ulong xi;
     499             :   LOCAL_HIREMAINDER;
     500             : 
     501  1025915353 :   if (!x) pari_err_INV("umodiu",gen_0);
     502  1025915353 :   if (!sy) return 0;
     503   893881390 :   ly = lgefint(y);
     504   893881390 :   if (x <= uel(y,2))
     505             :   {
     506   352720262 :     hiremainder=0;
     507   352720262 :     if (ly==3)
     508             :     {
     509   339391556 :       hiremainder=uel(y,2)%x;
     510   339391556 :       if (!hiremainder) return 0;
     511   312364552 :       return (sy > 0)? hiremainder: x - hiremainder;
     512             :     }
     513             :   }
     514             :   else
     515             :   {
     516   541161128 :     if (ly==3) return (sy > 0)? uel(y,2): x - uel(y,2);
     517    27449977 :     hiremainder=uel(y,2); ly--; y++;
     518             :   }
     519    40778683 :   xi = get_Fl_red(x);
     520    40778683 :   for (i=2; i<ly; i++) (void)divll_pre(y[i],x,xi);
     521    40778683 :   if (!hiremainder) return 0;
     522    36958000 :   return (sy > 0)? hiremainder: x - hiremainder;
     523             : }
     524             : 
     525             : /* return |y| \/ x */
     526             : GEN
     527   108967851 : absdiviu_rem(GEN y, ulong x, ulong *rem)
     528             : {
     529             :   long ly,i;
     530             :   GEN z;
     531             :   ulong xi;
     532             :   LOCAL_HIREMAINDER;
     533             : 
     534   108967851 :   if (!x) pari_err_INV("absdiviu_rem",gen_0);
     535   108967851 :   if (!signe(y)) { *rem = 0; return gen_0; }
     536             : 
     537   108555705 :   ly = lgefint(y);
     538   108555705 :   if (x <= uel(y,2))
     539             :   {
     540    91465425 :     hiremainder=0;
     541    91465425 :     if (ly==3)
     542             :     {
     543    30219951 :       z = cgetipos(3);
     544    30219951 :       z[2] = divll(uel(y,2),x);
     545    30219951 :       *rem = hiremainder; return z;
     546             :     }
     547             :   }
     548             :   else
     549             :   {
     550    17090280 :     if (ly==3) { *rem = uel(y,2); return gen_0; }
     551    13058349 :     hiremainder = uel(y,2); ly--; y++;
     552             :   }
     553    74303823 :   xi = get_Fl_red(x);
     554    74303823 :   z = cgetipos(ly);
     555    74303823 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     556    74303823 :   *rem = hiremainder; return z;
     557             : }
     558             : 
     559             : GEN
     560    32856309 : divis_rem(GEN y, long x, long *rem)
     561             : {
     562    32856309 :   long sy=signe(y),ly,s,i;
     563             :   GEN z;
     564             :   ulong xi;
     565             :   LOCAL_HIREMAINDER;
     566             : 
     567    32856309 :   if (!x) pari_err_INV("divis_rem",gen_0);
     568    32856309 :   if (!sy) { *rem=0; return gen_0; }
     569    23868741 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     570             : 
     571    23868741 :   ly = lgefint(y);
     572    23868741 :   if ((ulong)x <= uel(y,2))
     573             :   {
     574    14777085 :     hiremainder=0;
     575    14777085 :     if (ly==3)
     576             :     {
     577    14444676 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     578    14444676 :       z[2] = divll(uel(y,2),x);
     579    14444676 :       if (sy<0) hiremainder = - ((long)hiremainder);
     580    14444676 :       *rem = (long)hiremainder; return z;
     581             :     }
     582             :   }
     583             :   else
     584             :   {
     585     9091656 :     if (ly==3) { *rem = itos(y); return gen_0; }
     586       98064 :     hiremainder = uel(y,2); ly--; y++;
     587             :   }
     588      430473 :   xi = get_Fl_red(x);
     589      430473 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     590      430473 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     591      430473 :   if (sy<0) hiremainder = - ((long)hiremainder);
     592      430473 :   *rem = (long)hiremainder; return z;
     593             : }
     594             : 
     595             : GEN
     596      320184 : divis(GEN y, long x)
     597             : {
     598      320184 :   long sy=signe(y),ly,s,i;
     599             :   ulong xi;
     600             :   GEN z;
     601             :   LOCAL_HIREMAINDER;
     602             : 
     603      320184 :   if (!x) pari_err_INV("divis",gen_0);
     604      320184 :   if (!sy) return gen_0;
     605      320184 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     606             : 
     607      320184 :   ly = lgefint(y);
     608      320184 :   if ((ulong)x <= uel(y,2))
     609             :   {
     610      309030 :     hiremainder=0;
     611      309030 :     if (ly==3)
     612             :     {
     613      213567 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     614      213567 :       z[2] = divll(y[2],x);
     615      213567 :       return z;
     616             :     }
     617             :   }
     618             :   else
     619             :   {
     620       11154 :     if (ly==3) return gen_0;
     621       11154 :     hiremainder=y[2]; ly--; y++;
     622             :   }
     623      106617 :   xi = get_Fl_red(x);
     624      106617 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     625      106617 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x, xi);
     626      106617 :   return z;
     627             : }
     628             : 
     629             : GEN
     630    80879751 : divrr(GEN x, GEN y)
     631             : {
     632    80879751 :   long sx=signe(x), sy=signe(y), lx,ly,lr,e,i,j;
     633             :   ulong y0,y1;
     634             :   GEN r, r1;
     635             : 
     636    80879751 :   if (!x) pari_err_INV("divrr",y);
     637    80879751 :   e = expo(x) - expo(y);
     638    80879751 :   if (!sx) return real_0_bit(e);
     639    79700991 :   if (sy<0) sx = -sx;
     640             : 
     641    79700991 :   lx=lg(x); ly=lg(y);
     642    79700991 :   if (ly==3)
     643             :   {
     644    46175169 :     ulong k = x[2], l = (lx>3)? x[3]: 0;
     645             :     LOCAL_HIREMAINDER;
     646    46175169 :     if (k < uel(y,2)) e--;
     647             :     else
     648             :     {
     649    21139704 :       l >>= 1; if (k&1) l |= HIGHBIT;
     650    21139704 :       k >>= 1;
     651             :     }
     652    46175169 :     hiremainder = k; k = divll(l,y[2]);
     653    46175169 :     if (hiremainder & HIGHBIT)
     654             :     {
     655    12829371 :       k++;
     656    12829371 :       if (!k) { k = HIGHBIT; e++; }
     657             :     }
     658    46175169 :     r = cgetr(3);
     659    46175169 :     r[1] = evalsigne(sx) | evalexpo(e);
     660    46175169 :     r[2] = k; return r;
     661             :   }
     662             : 
     663    33525822 :   lr = minss(lx,ly); r = new_chunk(lr);
     664    33525822 :   r1 = r-1;
     665    33525822 :   r1[1] = 0; for (i=2; i<lr; i++) r1[i]=x[i];
     666    33525822 :   r1[lr] = (lx>ly)? x[lr]: 0;
     667    33525822 :   y0 = y[2]; y1 = y[3];
     668   265493157 :   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   231967335 :     if (uel(r1,1) == y0)
     675             :     {
     676      789864 :       qp = ULONG_MAX; k = addll(y0,r1[2]);
     677             :     }
     678             :     else
     679             :     {
     680   231177471 :       if (uel(r1,1) > y0) /* can't happen if i=0 */
     681             :       {
     682           0 :         GEN y1 = y+1;
     683           0 :         j = lr-i; r1[j] = subll(r1[j],y1[j]);
     684           0 :         for (j--; j>0; j--) r1[j] = subllx(r1[j],y1[j]);
     685           0 :         j=i; do uel(r,--j)++; while (j && !uel(r,j));
     686             :       }
     687   231177471 :       hiremainder = r1[1]; overflow = 0;
     688   231177471 :       qp = divll(r1[2],y0); k = hiremainder;
     689             :     }
     690   231967335 :     j = lr-i+1;
     691   231967335 :     if (!overflow)
     692             :     {
     693             :       long k3, k4;
     694   231284211 :       k3 = mulll(qp,y1);
     695   231284211 :       if (j == 3) /* i = lr - 2 maximal, r1[3] undefined -> 0 */
     696    33496929 :         k4 = subll(hiremainder,k);
     697             :       else
     698             :       {
     699   197787282 :         k3 = subll(k3, r1[3]);
     700   197787282 :         k4 = subllx(hiremainder,k);
     701             :       }
     702   231284211 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     703             :     }
     704   231967335 :     if (j<ly) (void)mulll(qp,y[j]); else { hiremainder = 0 ; j = ly; }
     705  3372617022 :     for (j--; j>1; j--)
     706             :     {
     707  3140649687 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     708  3140649687 :       hiremainder += overflow;
     709             :     }
     710   231967335 :     if (uel(r1,1) != hiremainder)
     711             :     {
     712      219570 :       if (uel(r1,1) < hiremainder)
     713             :       {
     714      219570 :         qp--;
     715      219570 :         j = lr-i-(lr-i>=ly); r1[j] = addll(r1[j], y[j]);
     716      219570 :         for (j--; j>1; j--) r1[j] = addllx(r1[j], y[j]);
     717             :       }
     718             :       else
     719             :       {
     720           0 :         r1[1] -= hiremainder;
     721           0 :         while (r1[1])
     722             :         {
     723           0 :           qp++; if (!qp) { j=i; do uel(r,--j)++; while (j && !r[j]); }
     724           0 :           j = lr-i-(lr-i>=ly); r1[j] = subll(r1[j],y[j]);
     725           0 :           for (j--; j>1; j--) r1[j] = subllx(r1[j],y[j]);
     726           0 :           r1[1] -= overflow;
     727             :         }
     728             :       }
     729             :     }
     730   231967335 :     *++r1 = qp;
     731             :   }
     732             :   /* i = lr-1 */
     733             :   /* round correctly */
     734    33525822 :   if (uel(r1,1) > (y0>>1))
     735             :   {
     736    16469087 :     j=i; do uel(r,--j)++; while (j && !r[j]);
     737             :   }
     738    33525822 :   r1 = r-1; for (j=i; j>=2; j--) r[j]=r1[j];
     739    33525822 :   if (r[0] == 0) e--;
     740    13449165 :   else if (r[0] == 1) { shift_right(r,r, 2,lr, 1,1); }
     741             :   else { /* possible only when rounding up to 0x2 0x0 ... */
     742           0 :     r[2] = (long)HIGHBIT; e++;
     743             :   }
     744    33525822 :   r[0] = evaltyp(t_REAL)|evallg(lr);
     745    33525822 :   r[1] = evalsigne(sx) | evalexpo(e);
     746    33525822 :   return r;
     747             : }
     748             : 
     749             : GEN
     750    15315972 : divri(GEN x, GEN y)
     751             : {
     752    15315972 :   long lx, s = signe(y);
     753             :   pari_sp av;
     754             :   GEN z;
     755             : 
     756    15315972 :   if (!s) pari_err_INV("divri",y);
     757    15315972 :   if (!signe(x)) return real_0_bit(expo(x) - expi(y));
     758    15234168 :   if (!is_bigint(y)) {
     759    13316418 :     GEN z = divru(x, y[2]);
     760    13316418 :     if (s < 0) togglesign(z);
     761    13316418 :     return z;
     762             :   }
     763     1917750 :   lx = lg(x); z = cgetr(lx); av = avma;
     764     1917750 :   affrr(divrr(x, itor(y, lx+1)), z);
     765     1917750 :   avma = av; return z;
     766             : }
     767             : 
     768             : /* Integer division x / y: such that sign(r) = sign(x)
     769             :  *   if z = ONLY_REM return remainder, otherwise return quotient
     770             :  *   if z != NULL set *z to remainder
     771             :  *   *z is the last object on stack (and thus can be disposed of with cgiv
     772             :  *   instead of gerepile)
     773             :  * If *z is zero, we put gen_0 here and no copy.
     774             :  * space needed: lx + ly */
     775             : GEN
     776   534907434 : dvmdii(GEN x, GEN y, GEN *z)
     777             : {
     778   534907434 :   long sx = signe(x), sy = signe(y);
     779   534907434 :   long lx, ly = lgefint(y), lz, i, j, sh, lq, lr;
     780             :   pari_sp av;
     781             :   ulong y0,y0i,y1, *xd,*rd,*qd;
     782             :   GEN q, r, r1;
     783             : 
     784   534907434 :   if (!sx)
     785             :   {
     786    16883133 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     787    16883130 :     if (!z || z == ONLY_REM) return gen_0;
     788     6755397 :     *z=gen_0; return gen_0;
     789             :   }
     790   518024301 :   if (ly <= 3)
     791             :   {
     792             :     ulong rem;
     793   294550356 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     794   294550356 :     if (z == ONLY_REM)
     795             :     {
     796   254736435 :       rem = umodiu(x,uel(y,2));
     797   254736435 :       if (!rem) return gen_0;
     798   231069404 :       return (sx < 0)? utoineg(uel(y,2) - rem): utoipos(rem);
     799             :     }
     800    39813921 :     q = absdiviu_rem(x, uel(y,2), &rem);
     801    39813921 :     if (sx != sy) togglesign(q);
     802    39813921 :     if (!z) return q;
     803    36995643 :     if (!rem) *z = gen_0;
     804    16454871 :     else *z = sx < 0? utoineg(rem): utoipos(rem);
     805    36995643 :     return q;
     806             :   }
     807   223473945 :   lx=lgefint(x);
     808   223473945 :   lz=lx-ly;
     809   223473945 :   if (lz <= 0)
     810             :   {
     811    94479048 :     if (lz == 0)
     812             :     {
     813    86176734 :       for (i=2; i<lx; i++)
     814    85885167 :         if (x[i] != y[i])
     815             :         {
     816    81840858 :           if (uel(x,i) > uel(y,i)) goto DIVIDE;
     817    27449760 :           goto TRIVIAL;
     818             :         }
     819      291567 :       if (z == ONLY_REM) return gen_0;
     820       10017 :       if (z) *z = gen_0;
     821       10017 :       if (sx < 0) sy = -sy;
     822       10017 :       return stoi(sy);
     823             :     }
     824             : TRIVIAL:
     825    39796383 :     if (z == ONLY_REM) return icopy(x);
     826      562302 :     if (z) *z = icopy(x);
     827      562302 :     return gen_0;
     828             :   }
     829             : DIVIDE: /* quotient is non-zero */
     830   183385995 :   av=avma; if (sx<0) sy = -sy;
     831   183385995 :   r1 = new_chunk(lx); sh = bfffo(y[2]);
     832   183385995 :   if (sh)
     833             :   { /* normalize so that highbit(y) = 1 (shift left x and y by sh bits)*/
     834   175175208 :     register const ulong m = BITS_IN_LONG - sh;
     835   175175208 :     r = new_chunk(ly);
     836   175175208 :     shift_left(r, y,2,ly-1, 0,sh); y = r;
     837   175175208 :     shift_left(r1,x,2,lx-1, 0,sh);
     838   175175208 :     r1[1] = uel(x,2) >> m;
     839             :   }
     840             :   else
     841             :   {
     842     8210787 :     r1[1] = 0; for (j=2; j<lx; j++) r1[j] = x[j];
     843             :   }
     844   183385995 :   x = r1;
     845   183385995 :   y0 = y[2]; y0i = get_Fl_red(y0);
     846   183385995 :   y1 = y[3];
     847  1043514048 :   for (i=0; i<=lz; i++)
     848             :   { /* r1 = x + i */
     849             :     ulong k, qp;
     850             :     LOCAL_HIREMAINDER;
     851             :     LOCAL_OVERFLOW;
     852             : 
     853   860128053 :     if (uel(r1,1) == y0)
     854             :     {
     855       64592 :       qp = ULONG_MAX; k = addll(y0,r1[2]);
     856             :     }
     857             :     else
     858             :     {
     859   860063461 :       hiremainder = r1[1]; overflow = 0;
     860   860063461 :       qp = divll_pre(r1[2],y0,y0i); k = hiremainder;
     861             :     }
     862   860128053 :     if (!overflow)
     863             :     {
     864   860073790 :       long k3 = subll(mulll(qp,y1), r1[3]);
     865   860073790 :       long k4 = subllx(hiremainder,k);
     866   860073790 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     867             :     }
     868   860128053 :     hiremainder = 0; j = ly;
     869 22231565697 :     for (j--; j>1; j--)
     870             :     {
     871 21371437644 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     872 21371437644 :       hiremainder += overflow;
     873             :     }
     874   860128053 :     if (uel(r1,1) < hiremainder)
     875             :     {
     876      495773 :       qp--;
     877      495773 :       j = ly-1; r1[j] = addll(r1[j],y[j]);
     878      495773 :       for (j--; j>1; j--) r1[j] = addllx(r1[j],y[j]);
     879             :     }
     880   860128053 :     *++r1 = qp;
     881             :   }
     882             : 
     883   183385995 :   lq = lz+2;
     884   183385995 :   if (!z)
     885             :   {
     886      466722 :     qd = (ulong*)av;
     887      466722 :     xd = (ulong*)(x + lq);
     888      466722 :     if (x[1]) { lz++; lq++; }
     889      466722 :     while (lz--) *--qd = *--xd;
     890      466722 :     *--qd = evalsigne(sy) | evallgefint(lq);
     891      466722 :     *--qd = evaltyp(t_INT) | evallg(lq);
     892      466722 :     avma = (pari_sp)qd; return (GEN)qd;
     893             :   }
     894             : 
     895   182919273 :   j=lq; while (j<lx && !x[j]) j++;
     896   182919273 :   lz = lx-j;
     897   182919273 :   if (z == ONLY_REM)
     898             :   {
     899   139745658 :     if (lz==0) { avma = av; return gen_0; }
     900   137062119 :     rd = (ulong*)av; lr = lz+2;
     901   137062119 :     xd = (ulong*)(x + lx);
     902   137062119 :     if (!sh) while (lz--) *--rd = *--xd;
     903             :     else
     904             :     { /* shift remainder right by sh bits */
     905   128909274 :       const ulong shl = BITS_IN_LONG - sh;
     906             :       ulong l;
     907   128909274 :       xd--;
     908   747949800 :       while (--lz) /* fill r[3..] */
     909             :       {
     910   490131252 :         l = *xd >> sh;
     911   490131252 :         *--rd = l | (*--xd << shl);
     912             :       }
     913   128909274 :       l = *xd >> sh;
     914   128909274 :       if (l) *--rd = l; else lr--;
     915             :     }
     916   137062119 :     *--rd = evalsigne(sx) | evallgefint(lr);
     917   137062119 :     *--rd = evaltyp(t_INT) | evallg(lr);
     918   137062119 :     avma = (pari_sp)rd; return (GEN)rd;
     919             :   }
     920             : 
     921    43173615 :   lr = lz+2;
     922    43173615 :   rd = NULL; /* gcc -Wall */
     923    43173615 :   if (lz)
     924             :   { /* non zero remainder: initialize rd */
     925    41942508 :     xd = (ulong*)(x + lx);
     926    41942508 :     if (!sh)
     927             :     {
     928       25695 :       rd = (ulong*)avma; (void)new_chunk(lr);
     929       25695 :       while (lz--) *--rd = *--xd;
     930             :     }
     931             :     else
     932             :     { /* shift remainder right by sh bits */
     933    41916813 :       const ulong shl = BITS_IN_LONG - sh;
     934             :       ulong l;
     935    41916813 :       rd = (ulong*)x; /* overwrite shifted y */
     936    41916813 :       xd--;
     937   208151544 :       while (--lz)
     938             :       {
     939   124317918 :         l = *xd >> sh;
     940   124317918 :         *--rd = l | (*--xd << shl);
     941             :       }
     942    41916813 :       l = *xd >> sh;
     943    41916813 :       if (l) *--rd = l; else lr--;
     944             :     }
     945    41942508 :     *--rd = evalsigne(sx) | evallgefint(lr);
     946    41942508 :     *--rd = evaltyp(t_INT) | evallg(lr);
     947    41942508 :     rd += lr;
     948             :   }
     949    43173615 :   qd = (ulong*)av;
     950    43173615 :   xd = (ulong*)(x + lq);
     951    43173615 :   if (x[1]) lq++;
     952    43173615 :   j = lq-2; while (j--) *--qd = *--xd;
     953    43173615 :   *--qd = evalsigne(sy) | evallgefint(lq);
     954    43173615 :   *--qd = evaltyp(t_INT) | evallg(lq);
     955    43173615 :   q = (GEN)qd;
     956    43173615 :   if (lr==2) *z = gen_0;
     957             :   else
     958             :   { /* rd has been properly initialized: we had lz > 0 */
     959    41942508 :     while (lr--) *--qd = *--rd;
     960    41942508 :     *z = (GEN)qd;
     961             :   }
     962    43173615 :   avma = (pari_sp)qd; return q;
     963             : }
     964             : 
     965             : /* Montgomery reduction.
     966             :  * N has k words, assume T >= 0 has less than 2k.
     967             :  * Return res := T / B^k mod N, where B = 2^BIL
     968             :  * such that 0 <= res < T/B^k + N  and  res has less than k words */
     969             : GEN
     970    21506463 : red_montgomery(GEN T, GEN N, ulong inv)
     971             : {
     972             :   pari_sp av;
     973             :   GEN Te, Td, Ne, Nd, scratch;
     974    21506463 :   ulong i, j, m, t, d, k = NLIMBS(N);
     975             :   int carry;
     976             :   LOCAL_HIREMAINDER;
     977             :   LOCAL_OVERFLOW;
     978             : 
     979    21506463 :   if (k == 0) return gen_0;
     980    21506463 :   d = NLIMBS(T); /* <= 2*k */
     981    21506463 :   if (d == 0) return gen_0;
     982             : #ifdef DEBUG
     983             :   if (d > 2*k) pari_err_BUG("red_montgomery");
     984             : #endif
     985    21506454 :   if (k == 1)
     986             :   { /* as below, special cased for efficiency */
     987         492 :     ulong n = uel(N,2);
     988         492 :     if (d == 1) {
     989         492 :       hiremainder = uel(T,2);
     990         492 :       m = hiremainder * inv;
     991         492 :       (void)addmul(m, n); /* t + m*n = 0 */
     992         492 :       return utoi(hiremainder);
     993             :     } else { /* d = 2 */
     994           0 :       hiremainder = uel(T,3);
     995           0 :       m = hiremainder * inv;
     996           0 :       (void)addmul(m, n); /* t + m*n = 0 */
     997           0 :       t = addll(hiremainder, uel(T,2));
     998           0 :       if (overflow) t -= n; /* t > n doesn't fit in 1 word */
     999           0 :       return utoi(t);
    1000             :     }
    1001             :   }
    1002             :   /* assume k >= 2 */
    1003    21505962 :   av = avma; scratch = new_chunk(k<<1); /* >= k + 2: result fits */
    1004             : 
    1005             :   /* copy T to scratch space (pad with zeroes to 2k words) */
    1006    21505962 :   Td = (GEN)av;
    1007    21505962 :   Te = T + (d+2);
    1008    21505962 :   for (i=0; i < d     ; i++) *--Td = *--Te;
    1009    21505962 :   for (   ; i < (k<<1); i++) *--Td = 0;
    1010             : 
    1011    21505962 :   Te = (GEN)av; /* 1 beyond end of current T mantissa (in scratch) */
    1012    21505962 :   Ne = N + k+2; /* 1 beyond end of N mantissa */
    1013             : 
    1014    21505962 :   carry = 0;
    1015   323297661 :   for (i=0; i<k; i++) /* set T := T/B nod N, k times */
    1016             :   {
    1017   301791699 :     Td = Te; /* one beyond end of (new) T mantissa */
    1018   301791699 :     Nd = Ne;
    1019   301791699 :     hiremainder = *--Td;
    1020   301791699 :     m = hiremainder * inv; /* solve T + m N = O(B) */
    1021             : 
    1022             :     /* set T := (T + mN) / B */
    1023   301791699 :     Te = Td;
    1024   301791699 :     (void)addmul(m, *--Nd); /* = 0 */
    1025  5621557119 :     for (j=1; j<k; j++)
    1026             :     {
    1027  5319765420 :       t = addll(addmul(m, *--Nd), *--Td);
    1028  5319765420 :       *Td = t;
    1029  5319765420 :       hiremainder += overflow;
    1030             :     }
    1031   301791699 :     t = addll(hiremainder, *--Td); *Td = t + carry;
    1032   301791699 :     carry = (overflow || (carry && *Td == 0));
    1033             :   }
    1034    21505962 :   if (carry)
    1035             :   { /* Td > N overflows (k+1 words), set Td := Td - N */
    1036      346299 :     Td = Te;
    1037      346299 :     Nd = Ne;
    1038      346299 :     t = subll(*--Td, *--Nd); *Td = t;
    1039      346299 :     while (Td > scratch) { t = subllx(*--Td, *--Nd); *Td = t; }
    1040             :   }
    1041             : 
    1042             :   /* copy result */
    1043    21505962 :   Td = (GEN)av;
    1044    21505962 :   while (*scratch == 0 && Te > scratch) scratch++; /* strip leading 0s */
    1045    21505962 :   while (Te > scratch) *--Td = *--Te;
    1046    21505962 :   k = (GEN)av - Td; if (!k) { avma = av; return gen_0; }
    1047    21505962 :   k += 2;
    1048    21505962 :   *--Td = evalsigne(1) | evallgefint(k);
    1049    21505962 :   *--Td = evaltyp(t_INT) | evallg(k);
    1050             : #ifdef DEBUG
    1051             : {
    1052             :   long l = NLIMBS(N), s = BITS_IN_LONG*l;
    1053             :   GEN R = int2n(s);
    1054             :   GEN res = remii(mulii(T, Fp_inv(R, N)), N);
    1055             :   if (k > lgefint(N)
    1056             :     || !equalii(remii(Td,N),res)
    1057             :     || cmpii(Td, addii(shifti(T, -s), N)) >= 0) pari_err_BUG("red_montgomery");
    1058             : }
    1059             : #endif
    1060    21505962 :   avma = (pari_sp)Td; return Td;
    1061             : }
    1062             : 
    1063             : /* EXACT INTEGER DIVISION */
    1064             : 
    1065             : /* assume xy>0, the division is exact and y is odd. Destroy x */
    1066             : static GEN
    1067    16602207 : diviuexact_i(GEN x, ulong y)
    1068             : {
    1069             :   long i, lz, lx;
    1070             :   ulong q, yinv;
    1071             :   GEN z, z0, x0, x0min;
    1072             : 
    1073    16602207 :   if (y == 1) return icopy(x);
    1074    13390191 :   lx = lgefint(x);
    1075    13390191 :   if (lx == 3)
    1076             :   {
    1077      550140 :     q = uel(x,2) / y;
    1078      550140 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1079      550140 :     return utoipos(q);
    1080             :   }
    1081    12840051 :   yinv = invmod2BIL(y);
    1082    12840051 :   lz = (y <= uel(x,2)) ? lx : lx-1;
    1083    12840051 :   z = new_chunk(lz);
    1084    12840051 :   z0 = z + lz;
    1085    12840051 :   x0 = x + lx; x0min = x + lx-lz+2;
    1086             : 
    1087    53553285 :   while (x0 > x0min)
    1088             :   {
    1089    27873183 :     *--z0 = q = yinv*uel(--x0,0); /* i-th quotient */
    1090    27873183 :     if (!q) continue;
    1091             :     /* x := x - q * y */
    1092             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1093    26935911 :       register GEN x1 = x0 - 1;
    1094             :       LOCAL_HIREMAINDER;
    1095    26935911 :       (void)mulll(q,y);
    1096    26935911 :       if (hiremainder)
    1097             :       {
    1098    22977837 :         if (uel(x1,0) < hiremainder)
    1099             :         {
    1100      881460 :           uel(x1,0) -= hiremainder;
    1101      883224 :           do uel(--x1,0)--; while (uel(x1,0) == ULONG_MAX);
    1102             :         }
    1103             :         else
    1104    22096377 :           uel(x1,0) -= hiremainder;
    1105             :       }
    1106             :     }
    1107             :   }
    1108    12840051 :   i=2; while(!z[i]) i++;
    1109    12840051 :   z += i-2; lz -= i-2;
    1110    12840051 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1111    12840051 :   z[1] = evalsigne(1)|evallg(lz);
    1112    12840051 :   if (lz == 2) pari_err_OP("exact division", x, utoi(y));
    1113    12840051 :   avma = (pari_sp)z; return z;
    1114             : }
    1115             : 
    1116             : /* assume y != 0 and the division is exact */
    1117             : GEN
    1118    24804243 : diviuexact(GEN x, ulong y)
    1119             : {
    1120             :   pari_sp av;
    1121    24804243 :   long lx, vy, s = signe(x);
    1122             :   GEN z;
    1123             : 
    1124    24804243 :   if (!s) return gen_0;
    1125    24605850 :   if (y == 1) return icopy(x);
    1126    24599241 :   lx = lgefint(x);
    1127    24599241 :   if (lx == 3) {
    1128    23508849 :     ulong q = uel(x,2) / y;
    1129    23508849 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1130    23508849 :     return (s > 0)? utoipos(q): utoineg(q);
    1131             :   }
    1132     1090392 :   av = avma; (void)new_chunk(lx); vy = vals(y);
    1133     1090392 :   if (vy) {
    1134      561921 :     y >>= vy;
    1135      561921 :     if (y == 1) { avma = av; return shifti(x, -vy); }
    1136      504951 :     x = shifti(x, -vy);
    1137      504951 :     if (lx == 3) {
    1138           0 :       ulong q = uel(x,2) / y;
    1139           0 :       avma = av;
    1140           0 :       if (!q) pari_err_OP("exact division", x, utoi(y));
    1141           0 :       return (s > 0)? utoipos(q): utoineg(q);
    1142             :     }
    1143      528471 :   } else x = icopy(x);
    1144     1033422 :   avma = av;
    1145     1033422 :   z = diviuexact_i(x, y);
    1146     1033422 :   setsigne(z, s); return z;
    1147             : }
    1148             : 
    1149             : /* Find z such that x=y*z, knowing that y | x (unchecked)
    1150             :  * Method: y0 z0 = x0 mod B = 2^BITS_IN_LONG ==> z0 = 1/y0 mod B.
    1151             :  *    Set x := (x - z0 y) / B, updating only relevant words, and repeat */
    1152             : GEN
    1153   224785887 : diviiexact(GEN x, GEN y)
    1154             : {
    1155   224785887 :   long lx, ly, lz, vy, i, ii, sx = signe(x), sy = signe(y);
    1156             :   pari_sp av;
    1157             :   ulong y0inv,q;
    1158             :   GEN z;
    1159             : 
    1160   224785887 :   if (!sy) pari_err_INV("diviiexact",gen_0);
    1161   224785887 :   if (!sx) return gen_0;
    1162   183963072 :   lx = lgefint(x);
    1163   183963072 :   if (lx == 3) {
    1164   158764044 :     q = uel(x,2) / uel(y,2);
    1165   158764044 :     if (!q) pari_err_OP("exact division", x, y);
    1166   158764035 :     return (sx+sy) ? utoipos(q): utoineg(q);
    1167             :   }
    1168    25199028 :   vy = vali(y); av = avma;
    1169    25199028 :   (void)new_chunk(lx); /* enough room for z */
    1170    25199028 :   if (vy)
    1171             :   { /* make y odd */
    1172    11310249 :     y = shifti(y,-vy);
    1173    11310249 :     x = shifti(x,-vy); lx = lgefint(x);
    1174             :   }
    1175    13888779 :   else x = icopy(x); /* necessary because we destroy x */
    1176    25199028 :   avma = av; /* will erase our x,y when exiting */
    1177             :   /* now y is odd */
    1178    25199028 :   ly = lgefint(y);
    1179    25199028 :   if (ly == 3)
    1180             :   {
    1181    15568785 :     z = diviuexact_i(x,uel(y,2)); /* x != 0 */
    1182    15568785 :     setsigne(z, (sx+sy)? 1: -1); return z;
    1183             :   }
    1184     9630243 :   y0inv = invmod2BIL(y[ly-1]);
    1185     9630243 :   i=2; while (i<ly && y[i]==x[i]) i++;
    1186     9630243 :   lz = (i==ly || uel(y,i) < uel(x,i)) ? lx-ly+3 : lx-ly+2;
    1187     9630243 :   z = new_chunk(lz);
    1188             : 
    1189     9630243 :   y += ly - 1; /* now y[-i] = i-th word of y */
    1190    44618088 :   for (ii=lx-1,i=lz-1; i>=2; i--,ii--)
    1191             :   {
    1192             :     long limj;
    1193             :     LOCAL_HIREMAINDER;
    1194             :     LOCAL_OVERFLOW;
    1195             : 
    1196    34987845 :     z[i] = q = y0inv*uel(x,ii); /* i-th quotient */
    1197    34987845 :     if (!q) continue;
    1198             : 
    1199             :     /* x := x - q * y */
    1200    34945809 :     (void)mulll(q,y[0]); limj = maxss(lx - lz, ii+3-ly);
    1201             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1202    34945809 :       register GEN x0 = x + (ii - 1), y0 = y - 1, xlim = x + limj;
    1203   188301435 :       for (; x0 >= xlim; x0--, y0--)
    1204             :       {
    1205   153355626 :         *x0 = subll(*x0, addmul(q,*y0));
    1206   153355626 :         hiremainder += overflow;
    1207             :       }
    1208    34945809 :       if (hiremainder && limj != lx - lz)
    1209             :       {
    1210    17796468 :         if ((ulong)*x0 < hiremainder)
    1211             :         {
    1212      212673 :           *x0 -= hiremainder;
    1213      212673 :           do (*--x0)--; while ((ulong)*x0 == ULONG_MAX);
    1214             :         }
    1215             :         else
    1216    17583795 :           *x0 -= hiremainder;
    1217             :       }
    1218             :     }
    1219             :   }
    1220     9630243 :   i=2; while(!z[i]) i++;
    1221     9630243 :   z += i-2; lz -= (i-2);
    1222     9630243 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1223     9630243 :   z[1] = evalsigne((sx+sy)? 1: -1) | evallg(lz);
    1224     9630243 :   if (lz == 2) pari_err_OP("exact division", x, y);
    1225     9630243 :   avma = (pari_sp)z; return z;
    1226             : }
    1227             : 
    1228             : /* assume yz != and yz | x */
    1229             : GEN
    1230      441918 : diviuuexact(GEN x, ulong y, ulong z)
    1231             : {
    1232             :   long tmp[4];
    1233             :   ulong t;
    1234             :   LOCAL_HIREMAINDER;
    1235      441918 :   t = mulll(y, z);
    1236      441918 :   if (!hiremainder) return diviuexact(x, t);
    1237           0 :   tmp[0] = evaltyp(t_INT)|_evallg(4);
    1238           0 :   tmp[1] = evalsigne(1)|evallgefint(4);
    1239           0 :   tmp[2] = hiremainder;
    1240           0 :   tmp[3] = t;
    1241           0 :   return diviiexact(x, tmp);
    1242             : }
    1243             : 
    1244             : /********************************************************************/
    1245             : /**                                                                **/
    1246             : /**               INTEGER MULTIPLICATION (BASECASE)                **/
    1247             : /**                                                                **/
    1248             : /********************************************************************/
    1249             : /* nx >= ny = num. of digits of x, y (not GEN, see mulii) */
    1250             : INLINE GEN
    1251  1827774130 : muliispec_basecase(GEN x, GEN y, long nx, long ny)
    1252             : {
    1253             :   GEN z2e,z2d,yd,xd,ye,zd;
    1254             :   long p1,lz;
    1255             :   LOCAL_HIREMAINDER;
    1256             : 
    1257  1827774130 :   if (ny == 1) return muluispec((ulong)*y, x, nx);
    1258   553461174 :   if (ny == 0) return gen_0;
    1259   552869322 :   zd = (GEN)avma; lz = nx+ny+2;
    1260   552869322 :   (void)new_chunk(lz);
    1261   552869322 :   xd = x + nx;
    1262   552869322 :   yd = y + ny;
    1263   552869322 :   ye = yd; p1 = *--xd;
    1264             : 
    1265   552869322 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1266   552869322 :   while (yd > y) *--zd = addmul(p1, *--yd);
    1267   552869322 :   *--zd = hiremainder;
    1268             : 
    1269  4306466349 :   while (xd > x)
    1270             :   {
    1271             :     LOCAL_OVERFLOW;
    1272  3200727705 :     yd = ye; p1 = *--xd;
    1273             : 
    1274  3200727705 :     z2d = --z2e;
    1275  3200727705 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1276 39764690331 :     while (yd > y)
    1277             :     {
    1278 33363234921 :       hiremainder += overflow;
    1279 33363234921 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1280             :     }
    1281  3200727705 :     *--zd = hiremainder + overflow;
    1282             :   }
    1283   552869322 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1284   552869322 :   *--zd = evalsigne(1) | evallgefint(lz);
    1285   552869322 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1286   552869322 :   avma=(pari_sp)zd; return zd;
    1287             : }
    1288             : 
    1289             : INLINE GEN
    1290   600979704 : sqrispec_basecase(GEN x, long nx)
    1291             : {
    1292             :   GEN z2e,z2d,yd,xd,zd,x0,z0;
    1293             :   long p1,lz;
    1294             :   LOCAL_HIREMAINDER;
    1295             :   LOCAL_OVERFLOW;
    1296             : 
    1297   600979704 :   if (nx == 1) return sqru((ulong)*x);
    1298   519563379 :   if (nx == 0) return gen_0;
    1299   112707258 :   zd = (GEN)avma; lz = (nx+1) << 1;
    1300   112707258 :   z0 = new_chunk(lz);
    1301   112707258 :   if (nx == 1)
    1302             :   {
    1303           0 :     *--zd = mulll(*x, *x);
    1304           0 :     *--zd = hiremainder; goto END;
    1305             :   }
    1306   112707258 :   xd = x + nx;
    1307             : 
    1308             :   /* compute double products --> zd */
    1309   112707258 :   p1 = *--xd; yd = xd; --zd;
    1310   112707258 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1311   112707258 :   while (yd > x) *--zd = addmul(p1, *--yd);
    1312   112707258 :   *--zd = hiremainder;
    1313             : 
    1314   112707258 :   x0 = x+1;
    1315  1152970407 :   while (xd > x0)
    1316             :   {
    1317             :     LOCAL_OVERFLOW;
    1318   927555891 :     p1 = *--xd; yd = xd;
    1319             : 
    1320   927555891 :     z2e -= 2; z2d = z2e;
    1321   927555891 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1322  9108833502 :     while (yd > x)
    1323             :     {
    1324  7253721720 :       hiremainder += overflow;
    1325  7253721720 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1326             :     }
    1327   927555891 :     *--zd = hiremainder + overflow;
    1328             :   }
    1329             :   /* multiply zd by 2 (put result in zd - 1) */
    1330   112707258 :   zd[-1] = ((*zd & HIGHBIT) != 0);
    1331   112707258 :   shift_left(zd, zd, 0, (nx<<1)-3, 0, 1);
    1332             : 
    1333             :   /* add the squares */
    1334   112707258 :   xd = x + nx; zd = z0 + lz;
    1335   112707258 :   p1 = *--xd;
    1336   112707258 :   zd--; *zd = mulll(p1,p1);
    1337   112707258 :   zd--; *zd = addll(hiremainder, *zd);
    1338  1265677665 :   while (xd > x)
    1339             :   {
    1340  1040263149 :     p1 = *--xd;
    1341  1040263149 :     zd--; *zd = addll(mulll(p1,p1)+ overflow, *zd);
    1342  1040263149 :     zd--; *zd = addll(hiremainder + overflow, *zd);
    1343             :   }
    1344             : 
    1345             : END:
    1346   112707258 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1347   112707258 :   *--zd = evalsigne(1) | evallgefint(lz);
    1348   112707258 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1349   112707258 :   avma=(pari_sp)zd; return zd;
    1350             : }
    1351             : 
    1352             : /********************************************************************/
    1353             : /**                                                                **/
    1354             : /**               INTEGER MULTIPLICATION (FFT)                     **/
    1355             : /**                                                                **/
    1356             : /********************************************************************/
    1357             : 
    1358             : /*
    1359             :  Compute parameters for FFT:
    1360             :    len: result length
    1361             :    k: FFT depth.
    1362             :    n: number of blocks (2^k)
    1363             :    bs: block size
    1364             :    mod: Modulus is M=2^(BIL*mod)+1
    1365             :    ord: order of 2 in Z/MZ.
    1366             :  We must have:
    1367             :    bs*n >= l
    1368             :    2^(BIL*mod) > nb*2^(2*BIL*bs)
    1369             :    2^k | 2*BIL*mod
    1370             : */
    1371             : static void
    1372       11799 : mulliifft_params(long len, long *k, long *mod, long *bs, long *n, ulong *ord)
    1373             : {
    1374             :   long r;
    1375       11799 :   *k = expu((3*len)>>2)-3;
    1376             :   do {
    1377       11799 :     (*k)--;
    1378       11799 :     r  = *k-(TWOPOTBITS_IN_LONG+2);
    1379       11799 :     *n = 1L<<*k;
    1380       11799 :     *bs = (len+*n-1)>>*k;
    1381       11799 :     *mod= 2**bs+1;
    1382       11799 :     if (r>0)
    1383         600 :       *mod=((*mod+(1L<<r)-1)>>r)<<r;
    1384       11799 :   } while(*mod>=3**bs);
    1385       11799 :   *ord= 4**mod*BITS_IN_LONG;
    1386       11799 : }
    1387             : 
    1388             : /* Zf_: arithmetic in ring Z/MZ where M= 2^(BITS_IN_LONG*mod)+1
    1389             :  * for some mod.
    1390             :  * Do not garbage collect.
    1391             :  */
    1392             : 
    1393             : static GEN
    1394    24254592 : Zf_add(GEN a, GEN b, GEN M)
    1395             : {
    1396    24254592 :   GEN y, z = addii(a,b);
    1397    24254592 :   long mod = lgefint(M)-3;
    1398    24254592 :   long l = NLIMBS(z);
    1399    24254592 :   if (l<=mod) return z;
    1400     9024354 :   y = subiu(z, 1);
    1401     9024354 :   if (NLIMBS(y)<=mod) return z;
    1402     9024354 :   return int_normalize(y,1);
    1403             : }
    1404             : 
    1405             : static GEN
    1406    24608844 : Zf_sub(GEN a, GEN b, GEN M)
    1407             : {
    1408    24608844 :   GEN z = subii(a,b);
    1409    24608844 :   return signe(z)>=0? z: addii(M,z);
    1410             : }
    1411             : 
    1412             : /* destroy z */
    1413             : static GEN
    1414    50366214 : Zf_red_destroy(GEN z, GEN M)
    1415             : {
    1416    50366214 :   long mod = lgefint(M)-3;
    1417    50366214 :   long l = NLIMBS(z);
    1418             :   GEN y;
    1419    50366214 :   if (l<=mod) return z;
    1420    22210488 :   y = shifti(z, -mod*BITS_IN_LONG);
    1421    22210488 :   z = int_normalize(z, NLIMBS(y));
    1422    22210488 :   y = Zf_red_destroy(y, M);
    1423    22210488 :   z = subii(z, y);
    1424    22210488 :   if (signe(z)<0) z = addii(z, M);
    1425    22210488 :   return z;
    1426             : }
    1427             : 
    1428             : INLINE GEN
    1429    26024142 : Zf_shift(GEN a, ulong s, GEN M) { return Zf_red_destroy(shifti(a, s), M); }
    1430             : 
    1431             : /*
    1432             :  Multiply by sqrt(2)^s
    1433             :  We use the formula sqrt(2)=z_8*(1-z_4)) && z_8=2^(ord/16) [2^(ord/4)+1]
    1434             : */
    1435             : 
    1436             : static GEN
    1437    24254592 : Zf_mulsqrt2(GEN a, ulong s, ulong ord, GEN M)
    1438             : {
    1439    24254592 :   ulong hord = ord>>1;
    1440    24254592 :   if (!signe(a)) return gen_0;
    1441    23184054 :   if (odd(s)) /* Multiply by 2^(s/2) */
    1442             :   {
    1443      354252 :     GEN az8  = Zf_shift(a,   ord>>4, M);
    1444      354252 :     GEN az83 = Zf_shift(az8, ord>>3, M);
    1445      354252 :     a = Zf_sub(az8, az83, M);
    1446      354252 :     s--;
    1447             :   }
    1448    23184054 :   if (s < hord)
    1449    17097594 :     return Zf_shift(a, s>>1, M);
    1450             :   else
    1451     6086460 :     return subii(M,Zf_shift(a, (s-hord)>>1, M));
    1452             : }
    1453             : 
    1454             : INLINE GEN
    1455      268416 : Zf_sqr(GEN a, GEN M) { return Zf_red_destroy(sqri(a), M); }
    1456             : 
    1457             : INLINE GEN
    1458     1863168 : Zf_mul(GEN a, GEN b, GEN M) { return Zf_red_destroy(mulii(a,b), M); }
    1459             : 
    1460             : /* In place, bit reversing FFT */
    1461             : static void
    1462     3972333 : muliifft_dit(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1463             : {
    1464     3972333 :   pari_sp av = avma;
    1465             :   long i;
    1466     3972333 :   ulong j, no = (o<<1)%ord;
    1467     3972333 :   long hstep=step>>1;
    1468    19765677 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1469             :   {
    1470    15793344 :     GEN a = Zf_add(gel(FFT,i), gel(FFT,i+hstep), M);
    1471    15793344 :     GEN b = Zf_mulsqrt2(Zf_sub(gel(FFT,i), gel(FFT,i+hstep), M), j, ord, M);
    1472    15793344 :     affii(a,gel(FFT,i));
    1473    15793344 :     affii(b,gel(FFT,i+hstep));
    1474    15793344 :     avma = av;
    1475             :   }
    1476     3972333 :   if (hstep>1)
    1477             :   {
    1478     1974957 :     muliifft_dit(no, ord, M, FFT, d, hstep);
    1479     1974957 :     muliifft_dit(no, ord, M, FFT, d+hstep, hstep);
    1480             :   }
    1481     3972333 : }
    1482             : 
    1483             : /* In place, bit reversed FFT, inverse of muliifft_dit */
    1484             : static void
    1485     2119785 : muliifft_dis(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1486             : {
    1487     2119785 :   pari_sp av = avma;
    1488             :   long i;
    1489     2119785 :   ulong j, no = (o<<1)%ord;
    1490     2119785 :   long hstep=step>>1;
    1491     2119785 :   if (hstep>1)
    1492             :   {
    1493     1053993 :     muliifft_dis(no, ord, M, FFT, d, hstep);
    1494     1053993 :     muliifft_dis(no, ord, M, FFT, d+hstep, hstep);
    1495             :   }
    1496    10581033 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1497             :   {
    1498     8461248 :     GEN z = Zf_mulsqrt2(gel(FFT,i+hstep), j, ord, M);
    1499     8461248 :     GEN a = Zf_add(gel(FFT,i), z, M);
    1500     8461248 :     GEN b = Zf_sub(gel(FFT,i), z, M);
    1501     8461248 :     affii(a,gel(FFT,i));
    1502     8461248 :     affii(b,gel(FFT,i+hstep));
    1503     8461248 :     avma = av;
    1504             :   }
    1505     2119785 : }
    1506             : 
    1507             : static GEN
    1508       22419 : muliifft_spliti(GEN a, long na, long bs, long n, long mod)
    1509             : {
    1510       22419 :   GEN ap = a+na-1;
    1511       22419 :   GEN c  = cgetg(n+1, t_VEC);
    1512             :   long i,j;
    1513     4017171 :   for(i=1;i<=n;i++)
    1514             :   {
    1515     3994752 :     GEN z = cgeti(mod+3);
    1516     3994752 :     if (na)
    1517             :     {
    1518     1973118 :       long m = minss(bs, na), v=0;
    1519     1973118 :       GEN zp, aa=ap-m+1;
    1520     1973118 :       while (!*aa && v<m) {aa++; v++;}
    1521     1973118 :       zp = z+m-v+1;
    1522    44348205 :       for (j=v; j < m; j++)
    1523    42375087 :         *zp-- = *ap--;
    1524     1973118 :       ap -= v; na -= m;
    1525     1973118 :       z[1] = evalsigne(m!=v) | evallgefint(m-v+2);
    1526             :     } else
    1527     2021634 :       z[1] = evalsigne(0) | evallgefint(2);
    1528     3994752 :     gel(c, i) = z;
    1529             :   }
    1530       22419 :   return c;
    1531             : }
    1532             : 
    1533             : static GEN
    1534       11799 : muliifft_unspliti(GEN V, long bs, long len)
    1535             : {
    1536       11799 :   long s, i, j, l = lg(V);
    1537       11799 :   GEN a = cgeti(len);
    1538       11799 :   a[1] = evalsigne(1)|evallgefint(len);
    1539    58579542 :   for(i=2;i<len;i++)
    1540    58567743 :     a[i] = 0;
    1541     2143383 :   for(i=1, s=0; i<l; i++, s+=bs)
    1542             :   {
    1543     2131584 :     GEN  u = gel(V,i);
    1544     2131584 :     if (signe(u))
    1545             :     {
    1546     1965093 :       GEN ap = int_W(a,s);
    1547     1965093 :       GEN up = int_LSW(u);
    1548     1965093 :       long lu = NLIMBS(u);
    1549             :       LOCAL_OVERFLOW;
    1550     1965093 :       *ap = addll(*ap, *up--); ap--;
    1551   106160253 :       for (j=1; j<lu; j++)
    1552   104195160 :        { *ap = addllx(*ap, *up--); ap--; }
    1553     3930312 :       while (overflow)
    1554         126 :        { *ap = addll(*ap, 1); ap--; }
    1555             :     }
    1556             :   }
    1557       11799 :   return int_normalize(a,0);
    1558             : }
    1559             : 
    1560             : static GEN
    1561        1179 : sqrispec_fft(GEN a, long na)
    1562             : {
    1563        1179 :   pari_sp av, ltop = avma;
    1564        1179 :   long len = 2*na;
    1565             :   long k, mod, bs, n;
    1566             :   GEN  FFT, M;
    1567             :   long i;
    1568             :   ulong o, ord;
    1569             : 
    1570        1179 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1571        1179 :   o = ord>>k;
    1572        1179 :   M = int2n(mod*BITS_IN_LONG);
    1573        1179 :   M[2+mod] = 1;
    1574        1179 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1575        1179 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1576        1179 :   av = avma;
    1577      269595 :   for(i=1; i<=n; i++)
    1578             :   {
    1579      268416 :     affii(Zf_sqr(gel(FFT,i), M), gel(FFT,i));
    1580      268416 :     avma=av;
    1581             :   }
    1582        1179 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1583      269595 :   for(i=1; i<=n; i++)
    1584             :   {
    1585      268416 :     affii(Zf_shift(gel(FFT,i), (ord>>1)-k, M), gel(FFT,i));
    1586      268416 :     avma=av;
    1587             :   }
    1588        1179 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1589             : }
    1590             : 
    1591             : static GEN
    1592       10620 : muliispec_fft(GEN a, GEN b, long na, long nb)
    1593             : {
    1594       10620 :   pari_sp av, av2, ltop = avma;
    1595       10620 :   long len = na+nb;
    1596             :   long k, mod, bs, n;
    1597             :   GEN FFT, FFTb, M;
    1598             :   long i;
    1599             :   ulong o, ord;
    1600             : 
    1601       10620 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1602       10620 :   o = ord>>k;
    1603       10620 :   M = int2n(mod*BITS_IN_LONG);
    1604       10620 :   M[2+mod] = 1;
    1605       10620 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1606       10620 :   av=avma;
    1607       10620 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1608       10620 :   FFTb = muliifft_spliti(b, nb, bs, n, mod);
    1609       10620 :   av2 = avma;
    1610       10620 :   muliifft_dit(o, ord, M, FFTb, 0, n);
    1611     1873788 :   for(i=1; i<=n; i++)
    1612             :   {
    1613     1863168 :     affii(Zf_mul(gel(FFT,i), gel(FFTb,i), M), gel(FFT,i));
    1614     1863168 :     avma=av2;
    1615             :   }
    1616       10620 :   avma=av;
    1617       10620 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1618     1873788 :   for(i=1; i<=n; i++)
    1619             :   {
    1620     1863168 :     affii(Zf_shift(gel(FFT,i),(ord>>1)-k,M), gel(FFT,i));
    1621     1863168 :     avma=av;
    1622             :   }
    1623       10620 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1624             : }
    1625             : 
    1626             : /********************************************************************/
    1627             : /**                                                                **/
    1628             : /**               INTEGER MULTIPLICATION (KARATSUBA)               **/
    1629             : /**                                                                **/
    1630             : /********************************************************************/
    1631             : 
    1632             : /* return (x shifted left d words) + y. Assume d > 0, x > 0 and y >= 0 */
    1633             : static GEN
    1634   134785908 : addshiftw(GEN x, GEN y, long d)
    1635             : {
    1636   134785908 :   GEN z,z0,y0,yd, zd = (GEN)avma;
    1637   134785908 :   long a,lz,ly = lgefint(y);
    1638             : 
    1639   134785908 :   z0 = new_chunk(d);
    1640   134785908 :   a = ly-2; yd = y+ly;
    1641   134785908 :   if (a >= d)
    1642             :   {
    1643   132568230 :     y0 = yd-d; while (yd > y0) *--zd = *--yd; /* copy last d words of y */
    1644   132568230 :     a -= d;
    1645   132568230 :     if (a)
    1646    92540355 :       z = addiispec(LIMBS(x), LIMBS(y), NLIMBS(x), a);
    1647             :     else
    1648    40027875 :       z = icopy(x);
    1649             :   }
    1650             :   else
    1651             :   {
    1652     2217678 :     y0 = yd-a; while (yd > y0) *--zd = *--yd; /* copy last a words of y */
    1653     2217678 :     while (zd > z0) *--zd = 0;    /* complete with 0s */
    1654     2217678 :     z = icopy(x);
    1655             :   }
    1656   134785908 :   lz = lgefint(z)+d;
    1657   134785908 :   z[1] = evalsigne(1) | evallgefint(lz);
    1658   134785908 :   z[0] = evaltyp(t_INT) | evallg(lz); return z;
    1659             : }
    1660             : 
    1661             : /* Fast product (Karatsuba) of integers. a and b are "special" GENs
    1662             :  * c,c0,c1,c2 are genuine GENs.
    1663             :  */
    1664             : GEN
    1665  1870829392 : muliispec(GEN a, GEN b, long na, long nb)
    1666             : {
    1667             :   GEN a0,c,c0;
    1668             :   long n0, n0a, i;
    1669             :   pari_sp av;
    1670             : 
    1671  1870829392 :   if (na < nb) swapspec(a,b, na,nb);
    1672  1870829392 :   if (nb < MULII_KARATSUBA_LIMIT) return muliispec_basecase(a,b,na,nb);
    1673    43055262 :   if (nb >= MULII_FFT_LIMIT)      return muliispec_fft(a,b,na,nb);
    1674    43044642 :   i=(na>>1); n0=na-i; na=i;
    1675    43044642 :   av=avma; a0=a+na; n0a=n0;
    1676    43044642 :   while (n0a && !*a0) { a0++; n0a--; }
    1677             : 
    1678    43044642 :   if (n0a && nb > n0)
    1679    41515461 :   { /* nb <= na <= n0 */
    1680             :     GEN b0,c1,c2;
    1681             :     long n0b;
    1682             : 
    1683    41515461 :     nb -= n0;
    1684    41515461 :     c = muliispec(a,b,na,nb);
    1685    41515461 :     b0 = b+nb; n0b = n0;
    1686    41515461 :     while (n0b && !*b0) { b0++; n0b--; }
    1687    41515461 :     if (n0b)
    1688             :     {
    1689    40851252 :       c0 = muliispec(a0,b0, n0a,n0b);
    1690             : 
    1691    40851252 :       c2 = addiispec(a0,a, n0a,na);
    1692    40851252 :       c1 = addiispec(b0,b, n0b,nb);
    1693    40851252 :       c1 = muliispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1694    40851252 :       c2 = addiispec(LIMBS(c0),LIMBS(c),  NLIMBS(c0),NLIMBS(c));
    1695             : 
    1696    40851252 :       c1 = subiispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1697             :     }
    1698             :     else
    1699             :     {
    1700      664209 :       c0 = gen_0;
    1701      664209 :       c1 = muliispec(a0,b, n0a,nb);
    1702             :     }
    1703    41515461 :     c = addshiftw(c,c1, n0);
    1704             :   }
    1705             :   else
    1706             :   {
    1707     1529181 :     c = muliispec(a,b,na,nb);
    1708     1529181 :     c0 = muliispec(a0,b,n0a,nb);
    1709             :   }
    1710    43044642 :   return gerepileuptoint(av, addshiftw(c,c0, n0));
    1711             : }
    1712             : GEN
    1713      458574 : muluui(ulong x, ulong y, GEN z)
    1714             : {
    1715      458574 :   long t, s = signe(z);
    1716             :   GEN r;
    1717             :   LOCAL_HIREMAINDER;
    1718             : 
    1719      458574 :   if (!x || !y || !signe(z)) return gen_0;
    1720      458277 :   t = mulll(x,y);
    1721      458277 :   if (!hiremainder)
    1722      458277 :     r = muluispec(t, z+2, lgefint(z)-2);
    1723             :   else
    1724             :   {
    1725             :     long tmp[2];
    1726           0 :     tmp[0] = hiremainder;
    1727           0 :     tmp[1] = t;
    1728           0 :     r = muliispec(z+2,tmp,lgefint(z)-2,2);
    1729             :   }
    1730      458277 :   setsigne(r,s); return r;
    1731             : }
    1732             : 
    1733             : #define sqrispec_mirror sqrispec
    1734             : #define muliispec_mirror muliispec
    1735             : 
    1736             : /* x % (2^n), assuming n >= 0 */
    1737             : GEN
    1738     8285124 : remi2n(GEN x, long n)
    1739             : {
    1740     8285124 :   long hi,l,k,lx,ly, sx = signe(x);
    1741             :   GEN z, xd, zd;
    1742             : 
    1743     8285124 :   if (!sx || !n) return gen_0;
    1744             : 
    1745     8266974 :   k = dvmdsBIL(n, &l);
    1746     8266974 :   lx = lgefint(x);
    1747     8266974 :   if (lx < k+3) return icopy(x);
    1748             : 
    1749     8228589 :   xd = x + (lx-k-1);
    1750             :   /* x = |_|...|#|1|...|k| : copy the last l bits of # and the last k words
    1751             :    *            ^--- initial xd  */
    1752     8228589 :   hi = ((ulong)*xd) & ((1UL<<l)-1); /* last l bits of # = top bits of result */
    1753     8228589 :   if (!hi)
    1754             :   { /* strip leading zeroes from result */
    1755      276894 :     xd++; while (k && !*xd) { k--; xd++; }
    1756      276894 :     if (!k) return gen_0;
    1757       70395 :     ly = k+2; xd--;
    1758             :   }
    1759             :   else
    1760     7951695 :     ly = k+3;
    1761             : 
    1762     8022090 :   zd = z = cgeti(ly);
    1763     8022090 :   *++zd = evalsigne(sx) | evallgefint(ly);
    1764     8022090 :   if (hi) *++zd = hi;
    1765     8022090 :   for ( ;k; k--) *++zd = *++xd;
    1766     8022090 :   return z;
    1767             : }
    1768             : 
    1769             : GEN
    1770   605843199 : sqrispec(GEN a, long na)
    1771             : {
    1772             :   GEN a0,c;
    1773             :   long n0, n0a, i;
    1774             :   pari_sp av;
    1775             : 
    1776   605843199 :   if (na < SQRI_KARATSUBA_LIMIT) return sqrispec_basecase(a,na);
    1777     4863495 :   if (na >= SQRI_FFT_LIMIT) return sqrispec_fft(a,na);
    1778     4862316 :   i=(na>>1); n0=na-i; na=i;
    1779     4862316 :   av=avma; a0=a+na; n0a=n0;
    1780     4862316 :   while (n0a && !*a0) { a0++; n0a--; }
    1781     4862316 :   c = sqrispec(a,na);
    1782     4862316 :   if (n0a)
    1783             :   {
    1784     4850490 :     GEN t, c1, c0 = sqrispec(a0,n0a);
    1785             : #if 0
    1786             :     c1 = shifti(muliispec(a0,a, n0a,na),1);
    1787             : #else /* faster */
    1788     4850490 :     t = addiispec(a0,a,n0a,na);
    1789     4850490 :     t = sqrispec(LIMBS(t),NLIMBS(t));
    1790     4850490 :     c1= addiispec(LIMBS(c0),LIMBS(c), NLIMBS(c0), NLIMBS(c));
    1791     4850490 :     c1= subiispec(LIMBS(t),LIMBS(c1), NLIMBS(t), NLIMBS(c1));
    1792             : #endif
    1793     4850490 :     c = addshiftw(c,c1, n0);
    1794     4850490 :     c = addshiftw(c,c0, n0);
    1795             :   }
    1796             :   else
    1797       11826 :     c = addshiftw(c,gen_0,n0<<1);
    1798     4862316 :   return gerepileuptoint(av, c);
    1799             : }
    1800             : 
    1801             : /********************************************************************/
    1802             : /**                                                                **/
    1803             : /**                    KARATSUBA SQUARE ROOT                       **/
    1804             : /**      adapted from Paul Zimmermann's implementation of          **/
    1805             : /**      his algorithm in GMP (mpn_sqrtrem)                        **/
    1806             : /**                                                                **/
    1807             : /********************************************************************/
    1808             : 
    1809             : /* Square roots table */
    1810             : static const unsigned char approx_tab[192] = {
    1811             :   128,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,
    1812             :   143,144,144,145,146,147,148,149,150,150,151,152,153,154,155,155,
    1813             :   156,157,158,159,160,160,161,162,163,163,164,165,166,167,167,168,
    1814             :   169,170,170,171,172,173,173,174,175,176,176,177,178,178,179,180,
    1815             :   181,181,182,183,183,184,185,185,186,187,187,188,189,189,190,191,
    1816             :   192,192,193,193,194,195,195,196,197,197,198,199,199,200,201,201,
    1817             :   202,203,203,204,204,205,206,206,207,208,208,209,209,210,211,211,
    1818             :   212,212,213,214,214,215,215,216,217,217,218,218,219,219,220,221,
    1819             :   221,222,222,223,224,224,225,225,226,226,227,227,228,229,229,230,
    1820             :   230,231,231,232,232,233,234,234,235,235,236,236,237,237,238,238,
    1821             :   239,240,240,241,241,242,242,243,243,244,244,245,245,246,246,247,
    1822             :   247,248,248,249,249,250,250,251,251,252,252,253,253,254,254,255
    1823             : };
    1824             : 
    1825             : /* N[0], assume N[0] >= 2^(BIL-2).
    1826             :  * Return r,s such that s^2 + r = N, 0 <= r <= 2s */
    1827             : static void
    1828    21349271 : p_sqrtu1(ulong *N, ulong *ps, ulong *pr)
    1829             : {
    1830    21349271 :   ulong prec, r, s, q, u, n0 = N[0];
    1831             : 
    1832    21349271 :   q = n0 >> (BITS_IN_LONG - 8);
    1833             :   /* 2^6 = 64 <= q < 256 = 2^8 */
    1834    21349271 :   s = approx_tab[q - 64];                                /* 128 <= s < 255 */
    1835    21349271 :   r = (n0 >> (BITS_IN_LONG - 16)) - s * s;                /* r <= 2*s */
    1836    21349271 :   if (r > (s << 1)) { r -= (s << 1) | 1; s++; }
    1837             : 
    1838             :   /* 8-bit approximation from the high 8-bits of N[0] */
    1839    21349271 :   prec = 8;
    1840    21349271 :   n0 <<= 2 * prec;
    1841    85397084 :   while (2 * prec < BITS_IN_LONG)
    1842             :   { /* invariant: s has prec bits, and r <= 2*s */
    1843    42698542 :     r = (r << prec) + (n0 >> (BITS_IN_LONG - prec));
    1844    42698542 :     n0 <<= prec;
    1845    42698542 :     u = 2 * s;
    1846    42698542 :     q = r / u; u = r - q * u;
    1847    42698542 :     s = (s << prec) + q;
    1848    42698542 :     u = (u << prec) + (n0 >> (BITS_IN_LONG - prec));
    1849    42698542 :     q = q * q;
    1850    42698542 :     r = u - q;
    1851    42698542 :     if (u < q) { s--; r += (s << 1) | 1; }
    1852    42698542 :     n0 <<= prec;
    1853    42698542 :     prec = 2 * prec;
    1854             :   }
    1855    21349271 :   *ps = s;
    1856    21349271 :   *pr = r;
    1857    21349271 : }
    1858             : 
    1859             : /* N[0..1], assume N[0] >= 2^(BIL-2).
    1860             :  * Return 1 if remainder overflows, 0 otherwise */
    1861             : static int
    1862    19074966 : p_sqrtu2(ulong *N, ulong *ps, ulong *pr)
    1863             : {
    1864    19074966 :   ulong cc, qhl, r, s, q, u, n1 = N[1];
    1865             :   LOCAL_OVERFLOW;
    1866             : 
    1867    19074966 :   p_sqrtu1(N, &s, &r); /* r <= 2s */
    1868    19074966 :   qhl = 0; while (r >= s) { qhl++; r -= s; }
    1869             :   /* now r < s < 2^(BIL/2) */
    1870    19074966 :   r = (r << BITS_IN_HALFULONG) | (n1 >> BITS_IN_HALFULONG);
    1871    19074966 :   u = s << 1;
    1872    19074966 :   q = r / u; u = r - q * u;
    1873    19074966 :   q += (qhl & 1) << (BITS_IN_HALFULONG - 1);
    1874    19074966 :   qhl >>= 1;
    1875             :   /* (initial r)<<(BIL/2) + n1>>(BIL/2) = (qhl<<(BIL/2) + q) * 2s + u */
    1876    19074966 :   s = ((s + qhl) << BITS_IN_HALFULONG) + q;
    1877    19074966 :   cc = u >> BITS_IN_HALFULONG;
    1878    19074966 :   r = (u << BITS_IN_HALFULONG) | (n1 & LOWMASK);
    1879    19074966 :   r = subll(r, q * q);
    1880    19074966 :   cc -= overflow + qhl;
    1881             :   /* now subtract 2*q*2^(BIL/2) + 2^BIL if qhl is set */
    1882    19074966 :   if ((long)cc < 0)
    1883             :   {
    1884     4968456 :     if (s) {
    1885     4961898 :       r = addll(r, s);
    1886     4961898 :       cc += overflow;
    1887     4961898 :       s--;
    1888             :     } else {
    1889        6558 :       cc++;
    1890        6558 :       s = ~0UL;
    1891             :     }
    1892     4968456 :     r = addll(r, s);
    1893     4968456 :     cc += overflow;
    1894             :   }
    1895    19074966 :   *ps = s;
    1896    19074966 :   *pr = r; return cc;
    1897             : }
    1898             : 
    1899             : static void
    1900    18667272 : xmpn_zero(GEN x, long n)
    1901             : {
    1902    18667272 :   while (--n >= 0) x[n]=0;
    1903    18667272 : }
    1904             : static void
    1905   189566181 : xmpn_copy(GEN z, GEN x, long n)
    1906             : {
    1907   189566181 :   long k = n;
    1908   189566181 :   while (--k >= 0) z[k] = x[k];
    1909   189566181 : }
    1910             : /* a[0..la-1] * 2^(lb BIL) | b[0..lb-1] */
    1911             : static GEN
    1912    81025998 : catii(GEN a, long la, GEN b, long lb)
    1913             : {
    1914    81025998 :   long l = la + lb + 2;
    1915    81025998 :   GEN z = cgetipos(l);
    1916    81025998 :   xmpn_copy(LIMBS(z), a, la);
    1917    81025998 :   xmpn_copy(LIMBS(z) + la, b, lb);
    1918    81025998 :   return int_normalize(z, 0);
    1919             : }
    1920             : 
    1921             : /* sqrt n[0..1], assume n normalized */
    1922             : static GEN
    1923    18750660 : sqrtispec2(GEN n, GEN *pr)
    1924             : {
    1925             :   ulong s, r;
    1926    18750660 :   int hi = p_sqrtu2((ulong*)n, &s, &r);
    1927    18750660 :   GEN S = utoi(s);
    1928    18750660 :   *pr = hi? uutoi(1,r): utoi(r);
    1929    18750660 :   return S;
    1930             : }
    1931             : 
    1932             : /* sqrt n[0], _dont_ assume n normalized */
    1933             : static GEN
    1934     2274305 : sqrtispec1_sh(GEN n, GEN *pr)
    1935             : {
    1936             :   GEN S;
    1937     2274305 :   ulong r, s, u0 = uel(n,0);
    1938     2274305 :   int sh = bfffo(u0) & ~1UL;
    1939     2274305 :   if (sh) u0 <<= sh;
    1940     2274305 :   p_sqrtu1(&u0, &s, &r);
    1941             :   /* s^2 + r = u0, s < 2^(BIL/2). Rescale back:
    1942             :    * 2^(2k) n = S^2 + R
    1943             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1944     2274305 :   if (sh) {
    1945     2042672 :     int k = sh >> 1;
    1946     2042672 :     ulong s0 = s & ((1L<<k) - 1);
    1947     2042672 :     r += s * (s0<<1);
    1948     2042672 :     s >>= k;
    1949     2042672 :     r >>= sh;
    1950             :   }
    1951     2274305 :   S = utoi(s);
    1952     2274305 :   if (pr) *pr = utoi(r);
    1953     2274305 :   return S;
    1954             : }
    1955             : 
    1956             : /* sqrt n[0..1], _dont_ assume n normalized */
    1957             : static GEN
    1958      324306 : sqrtispec2_sh(GEN n, GEN *pr)
    1959             : {
    1960             :   GEN S;
    1961      324306 :   ulong U[2], r, s, u0 = uel(n,0), u1 = uel(n,1);
    1962      324306 :   int hi, sh = bfffo(u0) & ~1UL;
    1963      324306 :   if (sh) {
    1964      324234 :     u0 = (u0 << sh) | (u1 >> (BITS_IN_LONG-sh));
    1965      324234 :     u1 <<= sh;
    1966             :   }
    1967      324306 :   U[0] = u0;
    1968      324306 :   U[1] = u1; hi = p_sqrtu2(U, &s, &r);
    1969             :   /* s^2 + R = u0|u1. Rescale back:
    1970             :    * 2^(2k) n = S^2 + R
    1971             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1972      324306 :   if (sh) {
    1973      324234 :     int k = sh >> 1;
    1974      324234 :     ulong s0 = s & ((1L<<k) - 1);
    1975             :     LOCAL_HIREMAINDER;
    1976             :     LOCAL_OVERFLOW;
    1977      324234 :     r = addll(r, mulll(s, (s0<<1)));
    1978      324234 :     if (overflow) hiremainder++;
    1979      324234 :     hiremainder += hi; /* + 0 or 1 */
    1980      324234 :     s >>= k;
    1981      324234 :     r = (r>>sh) | (hiremainder << (BITS_IN_LONG-sh));
    1982      324234 :     hi = (hiremainder & (1L<<sh));
    1983             :   }
    1984      324306 :   S = utoi(s);
    1985      324306 :   if (pr) *pr = hi? uutoi(1,r): utoi(r);
    1986      324306 :   return S;
    1987             : }
    1988             : 
    1989             : /* Let N = N[0..2n-1]. Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S
    1990             :  * Assume N normalized */
    1991             : static GEN
    1992    59263659 : sqrtispec(GEN N, long n, GEN *r)
    1993             : {
    1994             :   GEN S, R, q, z, u;
    1995             :   long l, h;
    1996             : 
    1997    59263659 :   if (n == 1) return sqrtispec2(N, r);
    1998    40512999 :   l = n >> 1;
    1999    40512999 :   h = n - l; /* N = a3(h) | a2(h) | a1(l) | a0(l words) */
    2000    40512999 :   S = sqrtispec(N, h, &R); /* S^2 + R = a3|a2 */
    2001             : 
    2002    40512999 :   z = catii(LIMBS(R), NLIMBS(R), N + 2*h, l); /* = R | a1(l) */
    2003    40512999 :   q = dvmdii(z, shifti(S,1), &u);
    2004    40512999 :   z = catii(LIMBS(u), NLIMBS(u), N + n + h, l); /* = u | a0(l) */
    2005             : 
    2006    40512999 :   S = addshiftw(S, q, l);
    2007    40512999 :   R = subii(z, sqri(q));
    2008    40512999 :   if (signe(R) < 0)
    2009             :   {
    2010     6490932 :     GEN S2 = shifti(S,1);
    2011     6490932 :     R = addis(subiispec(LIMBS(S2),LIMBS(R), NLIMBS(S2),NLIMBS(R)), -1);
    2012     6490932 :     S = addis(S, -1);
    2013             :   }
    2014    40512999 :   *r = R; return S;
    2015             : }
    2016             : 
    2017             : /* Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S.
    2018             :  * As for dvmdii, R is last on stack and guaranteed to be gen_0 in case the
    2019             :  * remainder is 0. R = NULL is allowed. */
    2020             : GEN
    2021     2682005 : sqrtremi(GEN N, GEN *r)
    2022             : {
    2023             :   pari_sp av;
    2024     2682005 :   GEN S, R, n = N+2;
    2025     2682005 :   long k, l2, ln = NLIMBS(N);
    2026             :   int sh;
    2027             : 
    2028     2682005 :   if (ln <= 2)
    2029             :   {
    2030     2598617 :     if (ln == 2) return sqrtispec2_sh(n, r);
    2031     2274311 :     if (ln == 1) return sqrtispec1_sh(n, r);
    2032           6 :     if (r) *r = gen_0;
    2033           6 :     return gen_0;
    2034             :   }
    2035       83388 :   av = avma;
    2036       83388 :   sh = bfffo(n[0]) >> 1;
    2037       83388 :   l2 = (ln + 1) >> 1;
    2038      166581 :   if (sh || (ln & 1)) { /* normalize n, so that n[0] >= 2^BIL / 4 */
    2039       83193 :     GEN s0, t = new_chunk(ln + 1);
    2040       83193 :     t[ln] = 0;
    2041       83193 :     if (sh)
    2042       82809 :       shift_left(t, n, 0,ln-1, 0, sh << 1);
    2043             :     else
    2044         384 :       xmpn_copy(t, n, ln);
    2045       83193 :     S = sqrtispec(t, l2, &R); /* t normalized, 2 * l2 words */
    2046             :     /* Rescale back:
    2047             :      * 2^(2k) n = S^2 + R, k = sh + (ln & 1)*BIL/2
    2048             :      * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    2049       83193 :     k = sh + (ln & 1) * (BITS_IN_LONG/2);
    2050       83193 :     s0 = remi2n(S, k);
    2051       83193 :     R = addii(shifti(R,-1), mulii(s0, S));
    2052       83193 :     R = shifti(R, 1 - (k<<1));
    2053       83193 :     S = shifti(S, -k);
    2054             :   }
    2055             :   else
    2056         195 :     S = sqrtispec(n, l2, &R);
    2057             : 
    2058       83388 :   if (!r) { avma = (pari_sp)S; return gerepileuptoint(av, S); }
    2059        6633 :   gerepileall(av, 2, &S, &R); *r = R; return S;
    2060             : }
    2061             : 
    2062             : /* compute sqrt(|a|), assuming a != 0 */
    2063             : 
    2064             : #if 1
    2065             : GEN
    2066    18667272 : sqrtr_abs(GEN x)
    2067             : {
    2068    18667272 :   long l = realprec(x) - 2, e = expo(x), er = e>>1;
    2069    18667272 :   GEN b, c, res = cgetr(2 + l);
    2070    18667272 :   res[1] = evalsigne(1) | evalexpo(er);
    2071    18667272 :   if (e&1) {
    2072     8846529 :     b = new_chunk(l << 1);
    2073     8846529 :     xmpn_copy(b, x+2, l);
    2074     8846529 :     xmpn_zero(b + l,l);
    2075     8846529 :     b = sqrtispec(b, l, &c);
    2076     8846529 :     xmpn_copy(res+2, b+2, l);
    2077     8846529 :     if (cmpii(c, b) > 0) roundr_up_ip(res, l+2);
    2078             :   } else {
    2079             :     ulong u;
    2080     9820743 :     b = new_chunk(2 + (l << 1));
    2081     9820743 :     shift_left(b+1, x+2, 0,l-1, 0, BITS_IN_LONG-1);
    2082     9820743 :     b[0] = uel(x,2)>>1;
    2083     9820743 :     xmpn_zero(b + l+1,l+1);
    2084     9820743 :     b = sqrtispec(b, l+1, &c);
    2085     9820743 :     xmpn_copy(res+2, b+2, l);
    2086     9820743 :     u = uel(b,l+2);
    2087     9820743 :     if ( u&HIGHBIT || (u == ~HIGHBIT && cmpii(c,b) > 0))
    2088     4803713 :       roundr_up_ip(res, l+2);
    2089             :   }
    2090    18667272 :   avma = (pari_sp)res; return res;
    2091             : }
    2092             : 
    2093             : #else /* use t_REAL: currently much slower (quadratic division) */
    2094             : 
    2095             : #ifdef LONG_IS_64BIT
    2096             : /* 64 bits of b = sqrt(a[0] * 2^64 + a[1])  [ up to 1ulp ] */
    2097             : static ulong
    2098             : sqrtu2(ulong *a)
    2099             : {
    2100             :   ulong c, b = dblmantissa( sqrt((double)a[0]) );
    2101             :   LOCAL_HIREMAINDER;
    2102             :   LOCAL_OVERFLOW;
    2103             : 
    2104             :   /* > 32 correct bits, 1 Newton iteration to reach 64 */
    2105             :   if (b <= a[0]) return HIGHBIT | (a[0] >> 1);
    2106             :   hiremainder = a[0]; c = divll(a[1], b);
    2107             :   return (addll(c, b) >> 1) | HIGHBIT;
    2108             : }
    2109             : /* 64 bits of sqrt(a[0] * 2^63) */
    2110             : static ulong
    2111             : sqrtu2_1(ulong *a)
    2112             : {
    2113             :   ulong t[2];
    2114             :   t[0] = (a[0] >> 1);
    2115             :   t[1] = (a[0] << (BITS_IN_LONG-1)) | (a[1] >> 1);
    2116             :   return sqrtu2(t);
    2117             : }
    2118             : #else
    2119             : /* 32 bits of sqrt(a[0] * 2^32) */
    2120             : static ulong
    2121             : sqrtu2(ulong *a)   { return dblmantissa( sqrt((double)a[0]) ); }
    2122             : /* 32 bits of sqrt(a[0] * 2^31) */
    2123             : static ulong
    2124             : sqrtu2_1(ulong *a) { return dblmantissa( sqrt(2. * a[0]) ); }
    2125             : #endif
    2126             : 
    2127             : GEN
    2128             : sqrtr_abs(GEN x)
    2129             : {
    2130             :   long l1, i, l = lg(x), ex = expo(x);
    2131             :   GEN a, t, y = cgetr(l);
    2132             :   pari_sp av, av0 = avma;
    2133             : 
    2134             :   a = rtor(x, l+1);
    2135             :   t = cgetr(l+1);
    2136             :   if (ex & 1) { /* odd exponent */
    2137             :     a[1] = evalsigne(1) | _evalexpo(1);
    2138             :     t[2] = (long)sqrtu2((ulong*)a + 2);
    2139             :   } else { /* even exponent */
    2140             :     a[1] = evalsigne(1) | _evalexpo(0);
    2141             :     t[2] = (long)sqrtu2_1((ulong*)a + 2);
    2142             :   }
    2143             :   t[1] = evalsigne(1) | _evalexpo(0);
    2144             :   for (i = 3; i <= l; i++) t[i] = 0;
    2145             : 
    2146             :   /* |x| = 2^(ex/2) a, t ~ sqrt(a) */
    2147             :   l--; l1 = 1; av = avma;
    2148             :   while (l1 < l) { /* let t := (t + a/t)/2 */
    2149             :     l1 <<= 1; if (l1 > l) l1 = l;
    2150             :     setlg(a, l1 + 2);
    2151             :     setlg(t, l1 + 2);
    2152             :     affrr(addrr(t, divrr(a,t)), t); shiftr_inplace(t, -1);
    2153             :     avma = av;
    2154             :   }
    2155             :   affrr(t,y); shiftr_inplace(y, (ex>>1));
    2156             :   avma = av0; return y;
    2157             : }
    2158             : 
    2159             : #endif
    2160             : 
    2161             : /*******************************************************************
    2162             :  *                                                                 *
    2163             :  *                           Base Conversion                       *
    2164             :  *                                                                 *
    2165             :  *******************************************************************/
    2166             : 
    2167             : static void
    2168      924849 : convi_dac(GEN x, ulong l, ulong *res)
    2169             : {
    2170      924849 :   pari_sp ltop=avma;
    2171             :   ulong m;
    2172             :   GEN x1,x2;
    2173      924849 :   if (l==1) { *res=itou(x); return; }
    2174      424890 :   m=l>>1;
    2175      424890 :   x1=dvmdii(x,powuu(1000000000UL,m),&x2);
    2176      424890 :   convi_dac(x1,l-m,res+m);
    2177      424890 :   convi_dac(x2,m,res);
    2178      424890 :   avma=ltop;
    2179             : }
    2180             : 
    2181             : /* convert integer --> base 10^9 [not memory clean] */
    2182             : ulong *
    2183      266470 : convi(GEN x, long *l)
    2184             : {
    2185      266470 :   long lz, lx = lgefint(x);
    2186             :   ulong *z;
    2187      266470 :   if (lx == 3 && uel(x,2) < 1000000000UL) {
    2188      191401 :     z = (ulong*)new_chunk(1);
    2189      191401 :     *z = x[2];
    2190      191401 :     *l = 1; return z+1;
    2191             :   }
    2192       75069 :   lz = 1 + (long)bit_accuracy_mul(lx, LOG10_2/9);
    2193       75069 :   z = (ulong*)new_chunk(lz);
    2194       75069 :   convi_dac(x,(ulong)lz,z);
    2195       75069 :   while (z[lz-1]==0) lz--;
    2196       75069 :   *l=lz; return z+lz;
    2197             : }
    2198             : 

Generated by: LCOV version 1.13