Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - kernel/none - mp.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 1116 1148 97.2 %
Date: 2020-01-26 05:57:03 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         689 : void pari_kernel_init(void) { }
      25         687 : 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   195449649 : int_normalize(GEN x, long known_zero_words)
      38             : {
      39   195449649 :   long i, lx = lgefint(x);
      40             :   GEN x0;
      41   195449649 :   if (lx == 2) { x[1] = evalsigne(0) | evallgefint(2); return x; }
      42   195449649 :   if (!known_zero_words && x[2]) return x;
      43   771467400 :   for (i = 2+known_zero_words; i < lx; i++)
      44   747784920 :     if (x[i]) break;
      45    76644963 :   x0 = x; i -= 2; x += i;
      46    76644963 :   if (x0 == (GEN)avma) set_avma((pari_sp)x);
      47    36793143 :   else stackdummy((pari_sp)(x0+i), (pari_sp)x0);
      48    76644963 :   lx -= i;
      49    76644963 :   x[0] = evaltyp(t_INT) | evallg(lx);
      50    76644963 :   if (lx == 2) x[1] = evalsigne(0) | evallgefint(lx);
      51    52962483 :   else         x[1] = evalsigne(1) | evallgefint(lx);
      52    76644963 :   return x;
      53             : }
      54             : 
      55             : /***********************************************************************/
      56             : /**                                                                   **/
      57             : /**                      ADDITION / SUBTRACTION                       **/
      58             : /**                                                                   **/
      59             : /***********************************************************************/
      60             : 
      61             : GEN
      62     2234787 : setloop(GEN a)
      63             : {
      64     2234787 :   pari_sp av = avma;
      65     2234787 :   (void)cgetg(lgefint(a) + 3, t_VECSMALL);
      66     2234787 :   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      127992 : resetloop(GEN a, GEN b) {
      72      127992 :   long lb = lgefint(b);
      73      127992 :   a += lgefint(a) - lb;
      74      127992 :   a[0] = evaltyp(t_INT) | evallg(lb);
      75      127992 :   affii(b, a); return a;
      76             : }
      77             : 
      78             : /* assume a > 0, initialized by setloop. Do a++ */
      79             : static GEN
      80    20332728 : incpos(GEN a)
      81             : {
      82    20332728 :   long i, l = lgefint(a);
      83    20332731 :   for (i=l-1; i>1; i--)
      84    20332728 :     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        8214 : incneg(GEN a)
      94             : {
      95        8214 :   long i, l = lgefint(a)-1;
      96        8214 :   if (uel(a,l)--)
      97             :   {
      98        8211 :     if (l == 2 && !a[2])
      99             :     {
     100         453 :       a++; /* save one cell */
     101         453 :       a[0] = evaltyp(t_INT) | _evallg(2);
     102         453 :       a[1] = evalsigne(0) | evallgefint(2);
     103             :     }
     104        8211 :     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    20589084 : incloop(GEN a)
     120             : {
     121    20589084 :   switch(signe(a))
     122             :   {
     123      248142 :     case 0: a--; /* use extra cell */
     124      248142 :       a[0]=evaltyp(t_INT) | _evallg(3);
     125      248142 :       a[1]=evalsigne(1) | evallgefint(3);
     126      248142 :       a[2]=1; return a;
     127        8214 :     case -1: return incneg(a);
     128    20332728 :     default: return incpos(a);
     129             :   }
     130             : }
     131             : 
     132             : INLINE GEN
     133  1060746714 : adduispec(ulong s, GEN x, long nx)
     134             : {
     135  1060746714 :   GEN xd, zd = (GEN)avma;
     136             :   long lz;
     137             : 
     138  1060746714 :   if (nx == 1) return adduu(s, uel(x,0));
     139   132117177 :   lz = nx+3; (void)new_chunk(lz);
     140   132117177 :   xd = x + nx;
     141   132117177 :   *--zd = (ulong)*--xd + s;
     142   132117177 :   if ((ulong)*zd < s)
     143             :     for(;;)
     144             :     {
     145    24164769 :       if (xd == x) { *--zd = 1; break; } /* enlarge z */
     146    14265567 :       *--zd = ((ulong)*--xd) + 1;
     147    14265567 :       if (*zd) { lz--; break; }
     148             :     }
     149   120382008 :   else lz--;
     150   132117177 :   while (xd > x) *--zd = *--xd;
     151   132117177 :   *--zd = evalsigne(1) | evallgefint(lz);
     152   132117177 :   *--zd = evaltyp(t_INT) | evallg(lz);
     153   132117177 :   set_avma((pari_sp)zd); return zd;
     154             : }
     155             : 
     156             : GEN
     157   168728547 : adduispec_offset(ulong s, GEN x, long offset, long nx)
     158             : {
     159   168728547 :   GEN xd = x+lgefint(x)-nx-offset;
     160   168728547 :   while (nx && *xd==0) {xd++; nx--;}
     161   168728547 :   if (!nx) return utoi(s);
     162   155046294 :   return adduispec(s,xd,nx);
     163             : }
     164             : 
     165             : static GEN
     166  2157778737 : addiispec(GEN x, GEN y, long nx, long ny)
     167             : {
     168             :   GEN xd, yd, zd;
     169  2157778737 :   long lz, i = -2;
     170             :   LOCAL_OVERFLOW;
     171             : 
     172  2157778737 :   if (nx < ny) swapspec(x,y, nx,ny);
     173  2157778737 :   if (ny == 1) return adduispec(*y,x,nx);
     174  1296047631 :   zd = (GEN)avma;
     175  1296047631 :   lz = nx+3; (void)new_chunk(lz);
     176  1296047631 :   xd = x + nx;
     177  1296047631 :   yd = y + ny;
     178  1296047631 :   zd[-1] = addll(xd[-1], yd[-1]);
     179             : #ifdef addllx8
     180   788655754 :   for (  ; i-8 > -ny; i-=8)
     181   356639877 :     addllx8(xd+i, yd+i, zd+i, overflow);
     182             : #endif
     183  1296047631 :   for (  ; i >= -ny; i--) zd[i] = addllx(xd[i], yd[i]);
     184  1296047631 :   if (overflow)
     185             :     for(;;)
     186             :     {
     187   174056592 :       if (i < -nx) { zd[i] = 1; i--; break; } /* enlarge z */
     188   100608369 :       zd[i] = uel(xd,i) + 1;
     189   100608369 :       if (zd[i]) { i--; lz--; break; }
     190    32691978 :       i--;
     191             :     }
     192  1187374995 :   else lz--;
     193  1296047631 :   for (; i >= -nx; i--) zd[i] = xd[i];
     194  1296047631 :   zd += i+1;
     195  1296047631 :   *--zd = evalsigne(1) | evallgefint(lz);
     196  1296047631 :   *--zd = evaltyp(t_INT) | evallg(lz);
     197  1296047631 :   set_avma((pari_sp)zd); return zd;
     198             : }
     199             : 
     200             : /* assume x >= s */
     201             : INLINE GEN
     202   848867110 : subiuspec(GEN x, ulong s, long nx)
     203             : {
     204   848867110 :   GEN xd, zd = (GEN)avma;
     205             :   long lz;
     206             :   LOCAL_OVERFLOW;
     207             : 
     208   848867110 :   if (nx == 1) return utoi(x[0] - s);
     209             : 
     210    97029133 :   lz = nx+2; (void)new_chunk(lz);
     211    97029133 :   xd = x + nx;
     212    97029133 :   *--zd = subll(*--xd, s);
     213    97029133 :   if (overflow)
     214             :     for(;;)
     215             :     {
     216    43789167 :       *--zd = ((ulong)*--xd) - 1;
     217    40560036 :       if (*xd) break;
     218             :     }
     219    97029133 :   if (xd == x)
     220    36597240 :     while (*zd == 0) { zd++; lz--; } /* shorten z */
     221             :   else
     222   689491118 :     do  *--zd = *--xd; while (xd > x);
     223    97029133 :   *--zd = evalsigne(1) | evallgefint(lz);
     224    97029133 :   *--zd = evaltyp(t_INT) | evallg(lz);
     225    97029133 :   set_avma((pari_sp)zd); return zd;
     226             : }
     227             : 
     228             : /* assume x > y */
     229             : static GEN
     230  1808984727 : subiispec(GEN x, GEN y, long nx, long ny)
     231             : {
     232             :   GEN xd,yd,zd;
     233  1808984727 :   long lz, i = -2;
     234             :   LOCAL_OVERFLOW;
     235             : 
     236  1808984727 :   if (ny==1) return subiuspec(x,*y,nx);
     237   986863103 :   zd = (GEN)avma;
     238   986863103 :   lz = nx+2; (void)new_chunk(lz);
     239   986863103 :   xd = x + nx;
     240   986863103 :   yd = y + ny;
     241   986863103 :   zd[-1] = subll(xd[-1], yd[-1]);
     242             : #ifdef subllx8
     243   624469910 :   for (  ; i-8 > -ny; i-=8)
     244   295515542 :     subllx8(xd+i, yd+i, zd+i, overflow);
     245             : #endif
     246   986863103 :   for (  ; i >= -ny; i--) zd[i] = subllx(xd[i], yd[i]);
     247   986863103 :   if (overflow)
     248             :     for(;;)
     249             :     {
     250   274363217 :       zd[i] = uel(xd,i) - 1;
     251   173303765 :       if (xd[i--]) break;
     252             :     }
     253   986863103 :   if (i>=-nx)
     254    98935595 :     for (; i >= -nx; i--) zd[i] = xd[i];
     255             :   else
     256   887927508 :     while (zd[i+1] == 0) { i++; lz--; } /* shorten z */
     257   986863103 :   zd += i+1;
     258   986863103 :   *--zd = evalsigne(1) | evallgefint(lz);
     259   986863103 :   *--zd = evaltyp(t_INT) | evallg(lz);
     260   986863103 :   set_avma((pari_sp)zd); return zd;
     261             : }
     262             : 
     263             : static void
     264   277254747 : roundr_up_ip(GEN x, long l)
     265             : {
     266   277254747 :   long i = l;
     267             :   for(;;)
     268             :   {
     269   279752121 :     if (++uel(x,--i)) break;
     270     1381437 :     if (i == 2) { x[2] = (long)HIGHBIT; shiftr_inplace(x, 1); break; }
     271             :   }
     272   277254747 : }
     273             : 
     274             : void
     275   154615578 : affir(GEN x, GEN y)
     276             : {
     277   154615578 :   const long s = signe(x), ly = lg(y);
     278             :   long lx, sh, i;
     279             : 
     280   154615578 :   if (!s)
     281             :   {
     282     3911088 :     y[1] = evalexpo(-prec2nbits(ly));
     283     3911088 :     return;
     284             :   }
     285             : 
     286   150704490 :   lx = lgefint(x); sh = bfffo(x[2]);
     287   150704490 :   y[1] = evalsigne(s) | evalexpo(bit_accuracy(lx)-sh-1);
     288   150704490 :   if (sh) {
     289   148814502 :     if (lx <= ly)
     290             :     {
     291    82560438 :       for (i=lx; i<ly; i++) y[i]=0;
     292    82560438 :       shift_left(y,x,2,lx-1, 0,sh);
     293    82560438 :       return;
     294             :     }
     295    66254064 :     shift_left(y,x,2,ly-1, x[ly],sh);
     296             :     /* lx > ly: round properly */
     297    66254064 :     if ((uel(x,ly)<<sh) & HIGHBIT) roundr_up_ip(y, ly);
     298             :   }
     299             :   else {
     300     1889988 :     if (lx <= ly)
     301             :     {
     302     1009401 :       for (i=2; i<lx; i++) y[i]=x[i];
     303     1009401 :       for (   ; i<ly; i++) y[i]=0;
     304     1009401 :       return;
     305             :     }
     306      880587 :     for (i=2; i<ly; i++) y[i]=x[i];
     307             :     /* lx > ly: round properly */
     308      880587 :     if (uel(x,ly) & HIGHBIT) roundr_up_ip(y, ly);
     309             :   }
     310             : }
     311             : 
     312             : INLINE GEN
     313   500904117 : shiftispec(GEN x, long nx, long n)
     314             : {
     315             :   long ny, i, m;
     316             :   GEN y, yd;
     317   500904117 :   if (!n)  return icopyspec(x, nx);
     318             : 
     319   471028527 :   if (n > 0)
     320             :   {
     321   296414808 :     GEN z = (GEN)avma;
     322   296414808 :     long d = dvmdsBIL(n, &m);
     323             : 
     324   296414808 :     ny = nx+d; y = new_chunk(ny + 2); yd = y + 2;
     325   296414808 :     for ( ; d; d--) *--z = 0;
     326   296414808 :     if (!m) for (i=0; i<nx; i++) yd[i]=x[i];
     327             :     else
     328             :     {
     329   292705062 :       register const ulong sh = BITS_IN_LONG - m;
     330   292705062 :       shift_left(yd,x, 0,nx-1, 0,m);
     331   292705062 :       i = uel(x,0) >> sh;
     332             :       /* Extend y on the left? */
     333   292705062 :       if (i) { ny++; y = new_chunk(1); y[2] = i; }
     334             :     }
     335             :   }
     336             :   else
     337             :   {
     338   174613719 :     ny = nx - dvmdsBIL(-n, &m);
     339   174613719 :     if (ny<1) return gen_0;
     340   174309690 :     y = new_chunk(ny + 2); yd = y + 2;
     341   174309690 :     if (m) {
     342   149643948 :       shift_right(yd,x, 0,ny, 0,m);
     343   149643948 :       if (yd[0] == 0)
     344             :       {
     345    11238027 :         if (ny==1) { set_avma((pari_sp)(y+3)); return gen_0; }
     346     9907236 :         ny--; set_avma((pari_sp)(++y));
     347             :       }
     348             :     } else {
     349    24665742 :       for (i=0; i<ny; i++) yd[i]=x[i];
     350             :     }
     351             :   }
     352   469393707 :   y[1] = evalsigne(1)|evallgefint(ny + 2);
     353   469393707 :   y[0] = evaltyp(t_INT)|evallg(ny + 2); return y;
     354             : }
     355             : 
     356             : GEN
     357    20056305 : mantissa2nr(GEN x, long n)
     358             : { /*This is a kludge since x is not an integer*/
     359    20056305 :   long s = signe(x);
     360             :   GEN y;
     361             : 
     362    20056305 :   if(s == 0) return gen_0;
     363    20055438 :   y = shiftispec(x + 2, lg(x) - 2, n);
     364    20055438 :   if (signe(y)) setsigne(y, s);
     365    20055438 :   return y;
     366             : }
     367             : 
     368             : GEN
     369     1418166 : truncr(GEN x)
     370             : {
     371             :   long d,e,i,s,m;
     372             :   GEN y;
     373             : 
     374     1418166 :   if ((s=signe(x)) == 0 || (e=expo(x)) < 0) return gen_0;
     375     1125918 :   d = nbits2lg(e+1); m = remsBIL(e);
     376     1125918 :   if (d > lg(x)) pari_err_PREC( "truncr (precision loss in truncation)");
     377             : 
     378     1125918 :   y=cgeti(d); y[1] = evalsigne(s) | evallgefint(d);
     379     1125918 :   if (++m == BITS_IN_LONG)
     380          90 :     for (i=2; i<d; i++) y[i]=x[i];
     381             :   else
     382     1125828 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     383     1125918 :   return y;
     384             : }
     385             : 
     386             : /* integral part */
     387             : GEN
     388      571599 : floorr(GEN x)
     389             : {
     390             :   long d,e,i,lx,m;
     391             :   GEN y;
     392             : 
     393      571599 :   if (signe(x) >= 0) return truncr(x);
     394      190311 :   if ((e=expo(x)) < 0) return gen_m1;
     395       68634 :   d = nbits2lg(e+1); m = remsBIL(e);
     396       68634 :   lx=lg(x); if (d>lx) pari_err_PREC( "floorr (precision loss in truncation)");
     397       68634 :   y = new_chunk(d);
     398       68634 :   if (++m == BITS_IN_LONG)
     399             :   {
     400         153 :     for (i=2; i<d; i++) y[i]=x[i];
     401         153 :     i=d; while (i<lx && !x[i]) i++;
     402         153 :     if (i==lx) goto END;
     403             :   }
     404             :   else
     405             :   {
     406       68481 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     407       68481 :     if (uel(x,d-1)<<m == 0)
     408             :     {
     409       19278 :       i=d; while (i<lx && !x[i]) i++;
     410       19278 :       if (i==lx) goto END;
     411             :     }
     412             :   }
     413             :   /* set y:=y+1 */
     414       59616 :   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       68634 :   y[1] = evalsigne(-1) | evallgefint(d);
     418       68634 :   y[0] = evaltyp(t_INT) | evallg(d); return y;
     419             : }
     420             : 
     421             : INLINE int
     422  2289855687 : cmpiispec(GEN x, GEN y, long lx, long ly)
     423             : {
     424             :   long i;
     425  2289855687 :   if (lx < ly) return -1;
     426  2188989963 :   if (lx > ly) return  1;
     427  2042229554 :   i = 0; while (i<lx && x[i]==y[i]) i++;
     428  2042229554 :   if (i==lx) return 0;
     429  1935325292 :   return (uel(x,i) > uel(y,i))? 1: -1;
     430             : }
     431             : 
     432             : INLINE int
     433   128671626 : equaliispec(GEN x, GEN y, long lx, long ly)
     434             : {
     435             :   long i;
     436   128671626 :   if (lx != ly) return 0;
     437   128661216 :   i = ly-1; while (i>=0 && x[i]==y[i]) i--;
     438   128661216 :   return i < 0;
     439             : }
     440             : 
     441             : /***********************************************************************/
     442             : /**                                                                   **/
     443             : /**                          MULTIPLICATION                           **/
     444             : /**                                                                   **/
     445             : /***********************************************************************/
     446             : /* assume ny > 0 */
     447             : INLINE GEN
     448  2043218379 : muluispec(ulong x, GEN y, long ny)
     449             : {
     450  2043218379 :   GEN yd, z = (GEN)avma;
     451  2043218379 :   long lz = ny+3;
     452             :   LOCAL_HIREMAINDER;
     453             : 
     454  2043218379 :   (void)new_chunk(lz);
     455  2043218379 :   yd = y + ny; *--z = mulll(x, *--yd);
     456  2043218379 :   while (yd > y) *--z = addmul(x,*--yd);
     457  2043218379 :   if (hiremainder) *--z = hiremainder; else lz--;
     458  2043218379 :   *--z = evalsigne(1) | evallgefint(lz);
     459  2043218379 :   *--z = evaltyp(t_INT) | evallg(lz);
     460  2043218379 :   set_avma((pari_sp)z); return z;
     461             : }
     462             : 
     463             : /* a + b*|Y| */
     464             : GEN
     465      417567 : 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      417567 :   if (!b || !signe(Y)) return utoi(a);
     473             : 
     474      417564 :   y = LIMBS(Y); z = (GEN)avma;
     475      417564 :   ny = NLIMBS(Y);
     476      417564 :   lz = ny+3;
     477             : 
     478      417564 :   (void)new_chunk(lz);
     479      417564 :   yd = y + ny; *--z = addll(a, mulll(b, *--yd));
     480      417564 :   if (overflow) hiremainder++; /* can't overflow */
     481      417564 :   while (yd > y) *--z = addmul(b,*--yd);
     482      417564 :   if (hiremainder) *--z = hiremainder; else lz--;
     483      417564 :   *--z = evalsigne(1) | evallgefint(lz);
     484      417564 :   *--z = evaltyp(t_INT) | evallg(lz);
     485      417564 :   set_avma((pari_sp)z); return z;
     486             : }
     487             : 
     488             : /***********************************************************************/
     489             : /**                                                                   **/
     490             : /**                          DIVISION                                 **/
     491             : /**                                                                   **/
     492             : /***********************************************************************/
     493             : 
     494             : ulong
     495   966633285 : umodiu(GEN y, ulong x)
     496             : {
     497   966633285 :   long sy=signe(y),ly,i;
     498             :   ulong xi;
     499             :   LOCAL_HIREMAINDER;
     500             : 
     501   966633285 :   if (!x) pari_err_INV("umodiu",gen_0);
     502   966633285 :   if (!sy) return 0;
     503   799439961 :   ly = lgefint(y);
     504   799439961 :   if (x <= uel(y,2))
     505             :   {
     506   374828940 :     hiremainder=0;
     507   374828940 :     if (ly==3)
     508             :     {
     509   360354216 :       hiremainder=uel(y,2)%x;
     510   360354216 :       if (!hiremainder) return 0;
     511   331540983 :       return (sy > 0)? hiremainder: x - hiremainder;
     512             :     }
     513             :   }
     514             :   else
     515             :   {
     516   424611021 :     if (ly==3) return (sy > 0)? uel(y,2): x - uel(y,2);
     517    60007911 :     hiremainder=uel(y,2); ly--; y++;
     518             :   }
     519    74482635 :   xi = get_Fl_red(x);
     520    74482635 :   for (i=2; i<ly; i++) (void)divll_pre(y[i],x,xi);
     521    74482635 :   if (!hiremainder) return 0;
     522    70913073 :   return (sy > 0)? hiremainder: x - hiremainder;
     523             : }
     524             : 
     525             : /* return |y| \/ x */
     526             : GEN
     527   132618444 : absdiviu_rem(GEN y, ulong x, ulong *rem)
     528             : {
     529             :   long ly,i;
     530             :   GEN z;
     531             :   ulong xi;
     532             :   LOCAL_HIREMAINDER;
     533             : 
     534   132618444 :   if (!x) pari_err_INV("absdiviu_rem",gen_0);
     535   132618444 :   if (!signe(y)) { *rem = 0; return gen_0; }
     536             : 
     537   132177906 :   ly = lgefint(y);
     538   132177906 :   if (x <= uel(y,2))
     539             :   {
     540   109615788 :     hiremainder=0;
     541   109615788 :     if (ly==3)
     542             :     {
     543    42540837 :       z = cgetipos(3);
     544    42540837 :       z[2] = divll(uel(y,2),x);
     545    42540837 :       *rem = hiremainder; return z;
     546             :     }
     547             :   }
     548             :   else
     549             :   {
     550    22562118 :     if (ly==3) { *rem = uel(y,2); return gen_0; }
     551    15519867 :     hiremainder = uel(y,2); ly--; y++;
     552             :   }
     553    82594818 :   xi = get_Fl_red(x);
     554    82594818 :   z = cgetipos(ly);
     555    82594818 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     556    82594818 :   *rem = hiremainder; return z;
     557             : }
     558             : 
     559             : GEN
     560    44464707 : divis_rem(GEN y, long x, long *rem)
     561             : {
     562    44464707 :   long sy=signe(y),ly,s,i;
     563             :   GEN z;
     564             :   ulong xi;
     565             :   LOCAL_HIREMAINDER;
     566             : 
     567    44464707 :   if (!x) pari_err_INV("divis_rem",gen_0);
     568    44464707 :   if (!sy) { *rem=0; return gen_0; }
     569    35532462 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     570             : 
     571    35532462 :   ly = lgefint(y);
     572    35532462 :   if ((ulong)x <= uel(y,2))
     573             :   {
     574    25431249 :     hiremainder=0;
     575    25431249 :     if (ly==3)
     576             :     {
     577    24998049 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     578    24998049 :       z[2] = divll(uel(y,2),x);
     579    24998049 :       if (sy<0) hiremainder = - ((long)hiremainder);
     580    24998049 :       *rem = (long)hiremainder; return z;
     581             :     }
     582             :   }
     583             :   else
     584             :   {
     585    10101213 :     if (ly==3) { *rem = itos(y); return gen_0; }
     586      139620 :     hiremainder = uel(y,2); ly--; y++;
     587             :   }
     588      572820 :   xi = get_Fl_red(x);
     589      572820 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     590      572820 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     591      572820 :   if (sy<0) hiremainder = - ((long)hiremainder);
     592      572820 :   *rem = (long)hiremainder; return z;
     593             : }
     594             : 
     595             : GEN
     596      890433 : divis(GEN y, long x)
     597             : {
     598      890433 :   long sy=signe(y),ly,s,i;
     599             :   ulong xi;
     600             :   GEN z;
     601             :   LOCAL_HIREMAINDER;
     602             : 
     603      890433 :   if (!x) pari_err_INV("divis",gen_0);
     604      890433 :   if (!sy) return gen_0;
     605      890430 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     606             : 
     607      890430 :   ly = lgefint(y);
     608      890430 :   if ((ulong)x <= uel(y,2))
     609             :   {
     610      872778 :     hiremainder=0;
     611      872778 :     if (ly==3)
     612             :     {
     613      714300 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     614      714300 :       z[2] = divll(y[2],x);
     615      714300 :       return z;
     616             :     }
     617             :   }
     618             :   else
     619             :   {
     620       17652 :     if (ly==3) return gen_0;
     621       17652 :     hiremainder=y[2]; ly--; y++;
     622             :   }
     623      176130 :   xi = get_Fl_red(x);
     624      176130 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     625      176130 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x, xi);
     626      176130 :   return z;
     627             : }
     628             : 
     629             : GEN
     630   108835593 : divrr(GEN x, GEN y)
     631             : {
     632   108835593 :   long sx=signe(x), sy=signe(y), lx,ly,lr,e,i,j;
     633             :   ulong y0,y1;
     634             :   GEN r, r1;
     635             : 
     636   108835593 :   if (!x) pari_err_INV("divrr",y);
     637   108835593 :   e = expo(x) - expo(y);
     638   108835593 :   if (!sx) return real_0_bit(e);
     639   107538600 :   if (sy<0) sx = -sx;
     640             : 
     641   107538600 :   lx=lg(x); ly=lg(y);
     642   107538600 :   if (ly==3)
     643             :   {
     644    76679232 :     ulong k = x[2], l = (lx>3)? x[3]: 0;
     645             :     LOCAL_HIREMAINDER;
     646    76679232 :     if (k < uel(y,2)) e--;
     647             :     else
     648             :     {
     649    35645466 :       l >>= 1; if (k&1) l |= HIGHBIT;
     650    35645466 :       k >>= 1;
     651             :     }
     652    76679232 :     hiremainder = k; k = divll(l,y[2]);
     653    76679232 :     if (hiremainder > (uel(y,2) >> 1) && !++k) { k = HIGHBIT; e++; }
     654    76679232 :     r = cgetr(3);
     655    76679232 :     r[1] = evalsigne(sx) | evalexpo(e);
     656    76679232 :     r[2] = k; return r;
     657             :   }
     658             : 
     659    30859368 :   lr = minss(lx,ly); r = new_chunk(lr);
     660    30859368 :   r1 = r-1;
     661    30859368 :   r1[1] = 0; for (i=2; i<lr; i++) r1[i]=x[i];
     662    30859368 :   r1[lr] = (lx>ly)? x[lr]: 0;
     663    30859368 :   y0 = y[2]; y1 = y[3];
     664   253659657 :   for (i=0; i<lr-1; i++)
     665             :   { /* r1 = r + (i-1), OK up to r1[2] (accesses at most r[lr]) */
     666             :     ulong k, qp;
     667             :     LOCAL_HIREMAINDER;
     668             :     LOCAL_OVERFLOW;
     669             : 
     670   222800289 :     if (uel(r1,1) == y0) { qp = ULONG_MAX; k = addll(y0,r1[2]); }
     671             :     else
     672             :     {
     673   218651898 :       if (uel(r1,1) > y0) /* can't happen if i=0 */
     674             :       {
     675           0 :         GEN y1 = y+1;
     676           0 :         j = lr-i; r1[j] = subll(r1[j],y1[j]);
     677           0 :         for (j--; j>0; j--) r1[j] = subllx(r1[j],y1[j]);
     678           0 :         j=i; do uel(r,--j)++; while (j && !uel(r,j));
     679             :       }
     680   218651898 :       hiremainder = r1[1]; overflow = 0;
     681   218651898 :       qp = divll(r1[2],y0); k = hiremainder;
     682             :     }
     683   222800289 :     j = lr-i+1;
     684   222800289 :     if (!overflow)
     685             :     {
     686             :       long k3, k4;
     687   219599616 :       k3 = mulll(qp,y1);
     688   219599616 :       if (j == 3) /* i = lr - 2 maximal, r1[3] undefined -> 0 */
     689    30838230 :         k4 = subll(hiremainder,k);
     690             :       else
     691             :       {
     692   188761386 :         k3 = subll(k3, r1[3]);
     693   188761386 :         k4 = subllx(hiremainder,k);
     694             :       }
     695   219599616 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     696             :     }
     697   222800289 :     if (j<ly) (void)mulll(qp,y[j]); else { hiremainder = 0 ; j = ly; }
     698  3265409655 :     for (j--; j>1; j--)
     699             :     {
     700  3042609366 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     701  3042609366 :       hiremainder += overflow;
     702             :     }
     703   222800289 :     if (uel(r1,1) != hiremainder)
     704             :     {
     705      436329 :       if (uel(r1,1) < hiremainder)
     706             :       {
     707      436329 :         qp--;
     708      436329 :         j = lr-i-(lr-i>=ly); r1[j] = addll(r1[j], y[j]);
     709      436329 :         for (j--; j>1; j--) r1[j] = addllx(r1[j], y[j]);
     710             :       }
     711             :       else
     712             :       {
     713           0 :         r1[1] -= hiremainder;
     714           0 :         while (r1[1])
     715             :         {
     716           0 :           qp++; if (!qp) { j=i; do uel(r,--j)++; while (j && !r[j]); }
     717           0 :           j = lr-i-(lr-i>=ly); r1[j] = subll(r1[j],y[j]);
     718           0 :           for (j--; j>1; j--) r1[j] = subllx(r1[j],y[j]);
     719           0 :           r1[1] -= overflow;
     720             :         }
     721             :       }
     722             :     }
     723   222800289 :     *++r1 = qp;
     724             :   }
     725             :   /* i = lr-1 */
     726             :   /* round correctly */
     727    30859368 :   if (uel(r1,1) > (y0>>1))
     728             :   {
     729    14960597 :     j=i; do uel(r,--j)++; while (j && !r[j]);
     730             :   }
     731    30859368 :   r1 = r-1; for (j=i; j>=2; j--) r[j]=r1[j];
     732    30859368 :   if (r[0] == 0) e--;
     733    13131810 :   else if (r[0] == 1) { shift_right(r,r, 2,lr, 1,1); }
     734             :   else { /* possible only when rounding up to 0x2 0x0 ... */
     735           0 :     r[2] = (long)HIGHBIT; e++;
     736             :   }
     737    30859368 :   r[0] = evaltyp(t_REAL)|evallg(lr);
     738    30859368 :   r[1] = evalsigne(sx) | evalexpo(e);
     739    30859368 :   return r;
     740             : }
     741             : 
     742             : GEN
     743    23125245 : divri(GEN x, GEN y)
     744             : {
     745    23125245 :   long lx, s = signe(y);
     746             :   pari_sp av;
     747             :   GEN z;
     748             : 
     749    23125245 :   if (!s) pari_err_INV("divri",y);
     750    23125245 :   if (!signe(x)) return real_0_bit(expo(x) - expi(y));
     751    23024049 :   if (!is_bigint(y)) {
     752    19505712 :     GEN z = divru(x, y[2]);
     753    19505712 :     if (s < 0) togglesign(z);
     754    19505712 :     return z;
     755             :   }
     756     3518337 :   lx = lg(x); z = cgetr(lx); av = avma;
     757     3518337 :   affrr(divrr(x, itor(y, lx+1)), z);
     758     3518337 :   set_avma(av); return z;
     759             : }
     760             : 
     761             : /* Integer division x / y: such that sign(r) = sign(x)
     762             :  *   if z = ONLY_REM return remainder, otherwise return quotient
     763             :  *   if z != NULL set *z to remainder
     764             :  *   *z is the last object on stack (and thus can be disposed of with cgiv
     765             :  *   instead of gerepile)
     766             :  * If *z is zero, we put gen_0 here and no copy.
     767             :  * space needed: lx + ly */
     768             : GEN
     769   676406742 : dvmdii(GEN x, GEN y, GEN *z)
     770             : {
     771   676406742 :   long sx = signe(x), sy = signe(y);
     772   676406742 :   long lx, ly = lgefint(y), lz, i, j, sh, lq, lr;
     773             :   pari_sp av;
     774             :   ulong y0,y0i,y1, *xd,*rd,*qd;
     775             :   GEN q, r, r1;
     776             : 
     777   676406742 :   if (!sx)
     778             :   {
     779    22402983 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     780    22402980 :     if (!z || z == ONLY_REM) return gen_0;
     781    11231406 :     *z=gen_0; return gen_0;
     782             :   }
     783   654003759 :   if (ly <= 3)
     784             :   {
     785             :     ulong rem;
     786   343399941 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     787   343399941 :     if (z == ONLY_REM)
     788             :     {
     789   288984009 :       rem = umodiu(x,uel(y,2));
     790   288984009 :       if (!rem) return gen_0;
     791   264415422 :       return (sx < 0)? utoineg(uel(y,2) - rem): utoipos(rem);
     792             :     }
     793    54415932 :     q = absdiviu_rem(x, uel(y,2), &rem);
     794    54415932 :     if (sx != sy) togglesign(q);
     795    54415932 :     if (!z) return q;
     796    53144427 :     if (!rem) *z = gen_0;
     797    24977472 :     else *z = sx < 0? utoineg(rem): utoipos(rem);
     798    53144427 :     return q;
     799             :   }
     800   310603818 :   lx=lgefint(x);
     801   310603818 :   lz=lx-ly;
     802   310603818 :   if (lz <= 0)
     803             :   {
     804   137764401 :     if (lz == 0)
     805             :     {
     806   130256208 :       for (i=2; i<lx; i++)
     807   129708609 :         if (x[i] != y[i])
     808             :         {
     809   116668074 :           if (uel(x,i) > uel(y,i)) goto DIVIDE;
     810    45481005 :           goto TRIVIAL;
     811             :         }
     812      547599 :       if (z == ONLY_REM) return gen_0;
     813       20505 :       if (z) *z = gen_0;
     814       20505 :       if (sx < 0) sy = -sy;
     815       20505 :       return stoi(sy);
     816             :     }
     817             : TRIVIAL:
     818    66029733 :     if (z == ONLY_REM) return icopy(x);
     819      992745 :     if (z) *z = icopy(x);
     820      992745 :     return gen_0;
     821             :   }
     822             : DIVIDE: /* quotient is non-zero */
     823   244026486 :   av=avma; if (sx<0) sy = -sy;
     824   244026486 :   r1 = new_chunk(lx); sh = bfffo(y[2]);
     825   244026486 :   if (sh)
     826             :   { /* normalize so that highbit(y) = 1 (shift left x and y by sh bits)*/
     827   235558557 :     register const ulong m = BITS_IN_LONG - sh;
     828   235558557 :     r = new_chunk(ly);
     829   235558557 :     shift_left(r, y,2,ly-1, 0,sh); y = r;
     830   235558557 :     shift_left(r1,x,2,lx-1, 0,sh);
     831   235558557 :     r1[1] = uel(x,2) >> m;
     832             :   }
     833             :   else
     834             :   {
     835     8467929 :     r1[1] = 0; for (j=2; j<lx; j++) r1[j] = x[j];
     836             :   }
     837   244026486 :   x = r1;
     838   244026486 :   y0 = y[2]; y0i = get_Fl_red(y0);
     839   244026486 :   y1 = y[3];
     840  1868147508 :   for (i=0; i<=lz; i++)
     841             :   { /* r1 = x + i */
     842             :     ulong k, qp;
     843             :     LOCAL_HIREMAINDER;
     844             :     LOCAL_OVERFLOW;
     845             : 
     846  1624121022 :     if (uel(r1,1) == y0)
     847             :     {
     848       82580 :       qp = ULONG_MAX; k = addll(y0,r1[2]);
     849             :     }
     850             :     else
     851             :     {
     852  1624038442 :       hiremainder = r1[1]; overflow = 0;
     853  1624038442 :       qp = divll_pre(r1[2],y0,y0i); k = hiremainder;
     854             :     }
     855  1624121022 :     if (!overflow)
     856             :     {
     857  1624048750 :       long k3 = subll(mulll(qp,y1), r1[3]);
     858  1624048750 :       long k4 = subllx(hiremainder,k);
     859  1624048750 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     860             :     }
     861  1624121022 :     hiremainder = 0; j = ly;
     862 25991650218 :     for (j--; j>1; j--)
     863             :     {
     864 24367529196 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     865 24367529196 :       hiremainder += overflow;
     866             :     }
     867  1624121022 :     if (uel(r1,1) < hiremainder)
     868             :     {
     869      904176 :       qp--;
     870      904176 :       j = ly-1; r1[j] = addll(r1[j],y[j]);
     871      904176 :       for (j--; j>1; j--) r1[j] = addllx(r1[j],y[j]);
     872             :     }
     873  1624121022 :     *++r1 = qp;
     874             :   }
     875             : 
     876   244026486 :   lq = lz+2;
     877   244026486 :   if (!z)
     878             :   {
     879     1500129 :     qd = (ulong*)av;
     880     1500129 :     xd = (ulong*)(x + lq);
     881     1500129 :     if (x[1]) { lz++; lq++; }
     882     1500129 :     while (lz--) *--qd = *--xd;
     883     1500129 :     *--qd = evalsigne(sy) | evallgefint(lq);
     884     1500129 :     *--qd = evaltyp(t_INT) | evallg(lq);
     885     1500129 :     set_avma((pari_sp)qd); return (GEN)qd;
     886             :   }
     887             : 
     888   242526357 :   j=lq; while (j<lx && !x[j]) j++;
     889   242526357 :   lz = lx-j;
     890   242526357 :   if (z == ONLY_REM)
     891             :   {
     892   193848924 :     if (lz==0) { set_avma(av); return gen_0; }
     893   188258655 :     rd = (ulong*)av; lr = lz+2;
     894   188258655 :     xd = (ulong*)(x + lx);
     895   188258655 :     if (!sh) while (lz--) *--rd = *--xd;
     896             :     else
     897             :     { /* shift remainder right by sh bits */
     898   179911494 :       const ulong shl = BITS_IN_LONG - sh;
     899             :       ulong l;
     900   179911494 :       xd--;
     901   966525711 :       while (--lz) /* fill r[3..] */
     902             :       {
     903   606702723 :         l = *xd >> sh;
     904   606702723 :         *--rd = l | (*--xd << shl);
     905             :       }
     906   179911494 :       l = *xd >> sh;
     907   179911494 :       if (l) *--rd = l; else lr--;
     908             :     }
     909   188258655 :     *--rd = evalsigne(sx) | evallgefint(lr);
     910   188258655 :     *--rd = evaltyp(t_INT) | evallg(lr);
     911   188258655 :     set_avma((pari_sp)rd); return (GEN)rd;
     912             :   }
     913             : 
     914    48677433 :   lr = lz+2;
     915    48677433 :   rd = NULL; /* gcc -Wall */
     916    48677433 :   if (lz)
     917             :   { /* non zero remainder: initialize rd */
     918    46913040 :     xd = (ulong*)(x + lx);
     919    46913040 :     if (!sh)
     920             :     {
     921       31029 :       rd = (ulong*)avma; (void)new_chunk(lr);
     922       31029 :       while (lz--) *--rd = *--xd;
     923             :     }
     924             :     else
     925             :     { /* shift remainder right by sh bits */
     926    46882011 :       const ulong shl = BITS_IN_LONG - sh;
     927             :       ulong l;
     928    46882011 :       rd = (ulong*)x; /* overwrite shifted y */
     929    46882011 :       xd--;
     930   245166084 :       while (--lz)
     931             :       {
     932   151402062 :         l = *xd >> sh;
     933   151402062 :         *--rd = l | (*--xd << shl);
     934             :       }
     935    46882011 :       l = *xd >> sh;
     936    46882011 :       if (l) *--rd = l; else lr--;
     937             :     }
     938    46913040 :     *--rd = evalsigne(sx) | evallgefint(lr);
     939    46913040 :     *--rd = evaltyp(t_INT) | evallg(lr);
     940    46913040 :     rd += lr;
     941             :   }
     942    48677433 :   qd = (ulong*)av;
     943    48677433 :   xd = (ulong*)(x + lq);
     944    48677433 :   if (x[1]) lq++;
     945    48677433 :   j = lq-2; while (j--) *--qd = *--xd;
     946    48677433 :   *--qd = evalsigne(sy) | evallgefint(lq);
     947    48677433 :   *--qd = evaltyp(t_INT) | evallg(lq);
     948    48677433 :   q = (GEN)qd;
     949    48677433 :   if (lr==2) *z = gen_0;
     950             :   else
     951             :   { /* rd has been properly initialized: we had lz > 0 */
     952    46913040 :     while (lr--) *--qd = *--rd;
     953    46913040 :     *z = (GEN)qd;
     954             :   }
     955    48677433 :   set_avma((pari_sp)qd); return q;
     956             : }
     957             : 
     958             : /* Montgomery reduction.
     959             :  * N has k words, assume T >= 0 has less than 2k.
     960             :  * Return res := T / B^k mod N, where B = 2^BIL
     961             :  * such that 0 <= res < T/B^k + N  and  res has less than k words */
     962             : GEN
     963    22598595 : red_montgomery(GEN T, GEN N, ulong inv)
     964             : {
     965             :   pari_sp av;
     966             :   GEN Te, Td, Ne, Nd, scratch;
     967    22598595 :   ulong i, j, m, t, d, k = NLIMBS(N);
     968             :   int carry;
     969             :   LOCAL_HIREMAINDER;
     970             :   LOCAL_OVERFLOW;
     971             : 
     972    22598595 :   if (k == 0) return gen_0;
     973    22598595 :   d = NLIMBS(T); /* <= 2*k */
     974    22598595 :   if (d == 0) return gen_0;
     975             : #ifdef DEBUG
     976             :   if (d > 2*k) pari_err_BUG("red_montgomery");
     977             : #endif
     978    22598586 :   if (k == 1)
     979             :   { /* as below, special cased for efficiency */
     980         492 :     ulong n = uel(N,2);
     981         492 :     if (d == 1) {
     982         492 :       hiremainder = uel(T,2);
     983         492 :       m = hiremainder * inv;
     984         492 :       (void)addmul(m, n); /* t + m*n = 0 */
     985         492 :       return utoi(hiremainder);
     986             :     } else { /* d = 2 */
     987           0 :       hiremainder = uel(T,3);
     988           0 :       m = hiremainder * inv;
     989           0 :       (void)addmul(m, n); /* t + m*n = 0 */
     990           0 :       t = addll(hiremainder, uel(T,2));
     991           0 :       if (overflow) t -= n; /* t > n doesn't fit in 1 word */
     992           0 :       return utoi(t);
     993             :     }
     994             :   }
     995             :   /* assume k >= 2 */
     996    22598094 :   av = avma; scratch = new_chunk(k<<1); /* >= k + 2: result fits */
     997             : 
     998             :   /* copy T to scratch space (pad with zeroes to 2k words) */
     999    22598094 :   Td = (GEN)av;
    1000    22598094 :   Te = T + (d+2);
    1001    22598094 :   for (i=0; i < d     ; i++) *--Td = *--Te;
    1002    22598094 :   for (   ; i < (k<<1); i++) *--Td = 0;
    1003             : 
    1004    22598094 :   Te = (GEN)av; /* 1 beyond end of current T mantissa (in scratch) */
    1005    22598094 :   Ne = N + k+2; /* 1 beyond end of N mantissa */
    1006             : 
    1007    22598094 :   carry = 0;
    1008   328685361 :   for (i=0; i<k; i++) /* set T := T/B nod N, k times */
    1009             :   {
    1010   306087267 :     Td = Te; /* one beyond end of (new) T mantissa */
    1011   306087267 :     Nd = Ne;
    1012   306087267 :     hiremainder = *--Td;
    1013   306087267 :     m = hiremainder * inv; /* solve T + m N = O(B) */
    1014             : 
    1015             :     /* set T := (T + mN) / B */
    1016   306087267 :     Te = Td;
    1017   306087267 :     (void)addmul(m, *--Nd); /* = 0 */
    1018  5643076401 :     for (j=1; j<k; j++)
    1019             :     {
    1020  5336989134 :       t = addll(addmul(m, *--Nd), *--Td);
    1021  5336989134 :       *Td = t;
    1022  5336989134 :       hiremainder += overflow;
    1023             :     }
    1024   306087267 :     t = addll(hiremainder, *--Td); *Td = t + carry;
    1025   306087267 :     carry = (overflow || (carry && *Td == 0));
    1026             :   }
    1027    22598094 :   if (carry)
    1028             :   { /* Td > N overflows (k+1 words), set Td := Td - N */
    1029      347985 :     Td = Te;
    1030      347985 :     Nd = Ne;
    1031      347985 :     t = subll(*--Td, *--Nd); *Td = t;
    1032      347985 :     while (Td > scratch) { t = subllx(*--Td, *--Nd); *Td = t; }
    1033             :   }
    1034             : 
    1035             :   /* copy result */
    1036    22598094 :   Td = (GEN)av;
    1037    22598094 :   while (*scratch == 0 && Te > scratch) scratch++; /* strip leading 0s */
    1038    22598094 :   while (Te > scratch) *--Td = *--Te;
    1039    22598094 :   k = (GEN)av - Td; if (!k) { set_avma(av); return gen_0; }
    1040    22598094 :   k += 2;
    1041    22598094 :   *--Td = evalsigne(1) | evallgefint(k);
    1042    22598094 :   *--Td = evaltyp(t_INT) | evallg(k);
    1043             : #ifdef DEBUG
    1044             : {
    1045             :   long l = NLIMBS(N), s = BITS_IN_LONG*l;
    1046             :   GEN R = int2n(s);
    1047             :   GEN res = remii(mulii(T, Fp_inv(R, N)), N);
    1048             :   if (k > lgefint(N)
    1049             :     || !equalii(remii(Td,N),res)
    1050             :     || cmpii(Td, addii(shifti(T, -s), N)) >= 0) pari_err_BUG("red_montgomery");
    1051             : }
    1052             : #endif
    1053    22598094 :   set_avma((pari_sp)Td); return Td;
    1054             : }
    1055             : 
    1056             : /* EXACT INTEGER DIVISION */
    1057             : 
    1058             : /* assume xy>0, the division is exact and y is odd. Destroy x */
    1059             : static GEN
    1060    23290335 : diviuexact_i(GEN x, ulong y)
    1061             : {
    1062             :   long i, lz, lx;
    1063             :   ulong q, yinv;
    1064             :   GEN z, z0, x0, x0min;
    1065             : 
    1066    23290335 :   if (y == 1) return icopy(x);
    1067    17674953 :   lx = lgefint(x);
    1068    17674953 :   if (lx == 3)
    1069             :   {
    1070      665847 :     q = uel(x,2) / y;
    1071      665847 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1072      665847 :     return utoipos(q);
    1073             :   }
    1074    17009106 :   yinv = invmod2BIL(y);
    1075    17009106 :   lz = (y <= uel(x,2)) ? lx : lx-1;
    1076    17009106 :   z = new_chunk(lz);
    1077    17009106 :   z0 = z + lz;
    1078    17009106 :   x0 = x + lx; x0min = x + lx-lz+2;
    1079             : 
    1080    67164717 :   while (x0 > x0min)
    1081             :   {
    1082    33146505 :     *--z0 = q = yinv*uel(--x0,0); /* i-th quotient */
    1083    33146505 :     if (!q) continue;
    1084             :     /* x := x - q * y */
    1085             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1086    32661936 :       register GEN x1 = x0 - 1;
    1087             :       LOCAL_HIREMAINDER;
    1088    32661936 :       (void)mulll(q,y);
    1089    32661936 :       if (hiremainder)
    1090             :       {
    1091    27000426 :         if (uel(x1,0) < hiremainder)
    1092             :         {
    1093       39210 :           uel(x1,0) -= hiremainder;
    1094       41055 :           do uel(--x1,0)--; while (uel(x1,0) == ULONG_MAX);
    1095             :         }
    1096             :         else
    1097    26961216 :           uel(x1,0) -= hiremainder;
    1098             :       }
    1099             :     }
    1100             :   }
    1101    17009106 :   i=2; while(!z[i]) i++;
    1102    17009106 :   z += i-2; lz -= i-2;
    1103    17009106 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1104    17009106 :   z[1] = evalsigne(1)|evallg(lz);
    1105    17009106 :   if (lz == 2) pari_err_OP("exact division", x, utoi(y));
    1106    17009106 :   set_avma((pari_sp)z); return z;
    1107             : }
    1108             : 
    1109             : /* assume y != 0 and the division is exact */
    1110             : GEN
    1111    29660190 : diviuexact(GEN x, ulong y)
    1112             : {
    1113             :   pari_sp av;
    1114    29660190 :   long lx, vy, s = signe(x);
    1115             :   GEN z;
    1116             : 
    1117    29660190 :   if (!s) return gen_0;
    1118    29456028 :   if (y == 1) return icopy(x);
    1119    29446239 :   lx = lgefint(x);
    1120    29446239 :   if (lx == 3) {
    1121    27251349 :     ulong q = uel(x,2) / y;
    1122    27251349 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1123    27251349 :     return (s > 0)? utoipos(q): utoineg(q);
    1124             :   }
    1125     2194890 :   av = avma; (void)new_chunk(lx); vy = vals(y);
    1126     2194890 :   if (vy) {
    1127     1153755 :     y >>= vy;
    1128     1153755 :     if (y == 1) { set_avma(av); return shifti(x, -vy); }
    1129      548466 :     x = shifti(x, -vy);
    1130      548466 :     if (lx == 3) {
    1131           0 :       ulong q = uel(x,2) / y;
    1132           0 :       set_avma(av);
    1133           0 :       if (!q) pari_err_OP("exact division", x, utoi(y));
    1134           0 :       return (s > 0)? utoipos(q): utoineg(q);
    1135             :     }
    1136     1041135 :   } else x = icopy(x);
    1137     1589601 :   set_avma(av);
    1138     1589601 :   z = diviuexact_i(x, y);
    1139     1589601 :   setsigne(z, s); return z;
    1140             : }
    1141             : 
    1142             : /* Find z such that x=y*z, knowing that y | x (unchecked)
    1143             :  * Method: y0 z0 = x0 mod B = 2^BITS_IN_LONG ==> z0 = 1/y0 mod B.
    1144             :  *    Set x := (x - z0 y) / B, updating only relevant words, and repeat */
    1145             : GEN
    1146   247600506 : diviiexact(GEN x, GEN y)
    1147             : {
    1148   247600506 :   long lx, ly, lz, vy, i, ii, sx = signe(x), sy = signe(y);
    1149             :   pari_sp av;
    1150             :   ulong y0inv,q;
    1151             :   GEN z;
    1152             : 
    1153   247600506 :   if (!sy) pari_err_INV("diviiexact",gen_0);
    1154   247600506 :   if (!sx) return gen_0;
    1155   207596766 :   lx = lgefint(x);
    1156   207596766 :   if (lx == 3) {
    1157   169998642 :     q = uel(x,2) / uel(y,2);
    1158   169998642 :     if (!q) pari_err_OP("exact division", x, y);
    1159   169998633 :     return (sx+sy) ? utoipos(q): utoineg(q);
    1160             :   }
    1161    37598124 :   vy = vali(y); av = avma;
    1162    37598124 :   (void)new_chunk(lx); /* enough room for z */
    1163    37598124 :   if (vy)
    1164             :   { /* make y odd */
    1165    18985809 :     y = shifti(y,-vy);
    1166    18985809 :     x = shifti(x,-vy); lx = lgefint(x);
    1167             :   }
    1168    18612315 :   else x = icopy(x); /* necessary because we destroy x */
    1169    37598124 :   set_avma(av); /* will erase our x,y when exiting */
    1170             :   /* now y is odd */
    1171    37598124 :   ly = lgefint(y);
    1172    37598124 :   if (ly == 3)
    1173             :   {
    1174    21700734 :     z = diviuexact_i(x,uel(y,2)); /* x != 0 */
    1175    21700734 :     setsigne(z, (sx+sy)? 1: -1); return z;
    1176             :   }
    1177    15897390 :   y0inv = invmod2BIL(y[ly-1]);
    1178    15897390 :   i=2; while (i<ly && y[i]==x[i]) i++;
    1179    15897390 :   lz = (i==ly || uel(y,i) < uel(x,i)) ? lx-ly+3 : lx-ly+2;
    1180    15897390 :   z = new_chunk(lz);
    1181             : 
    1182    15897390 :   y += ly - 1; /* now y[-i] = i-th word of y */
    1183    60712983 :   for (ii=lx-1,i=lz-1; i>=2; i--,ii--)
    1184             :   {
    1185             :     long limj;
    1186             :     LOCAL_HIREMAINDER;
    1187             :     LOCAL_OVERFLOW;
    1188             : 
    1189    44815593 :     z[i] = q = y0inv*uel(x,ii); /* i-th quotient */
    1190    44815593 :     if (!q) continue;
    1191             : 
    1192             :     /* x := x - q * y */
    1193    44747151 :     (void)mulll(q,y[0]); limj = maxss(lx - lz, ii+3-ly);
    1194             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1195    44747151 :       register GEN x0 = x + (ii - 1), y0 = y - 1, xlim = x + limj;
    1196   221798100 :       for (; x0 >= xlim; x0--, y0--)
    1197             :       {
    1198   177050949 :         *x0 = subll(*x0, addmul(q,*y0));
    1199   177050949 :         hiremainder += overflow;
    1200             :       }
    1201    44747151 :       if (hiremainder && limj != lx - lz)
    1202             :       {
    1203    20284269 :         if ((ulong)*x0 < hiremainder)
    1204             :         {
    1205      242295 :           *x0 -= hiremainder;
    1206      242295 :           do (*--x0)--; while ((ulong)*x0 == ULONG_MAX);
    1207             :         }
    1208             :         else
    1209    20041974 :           *x0 -= hiremainder;
    1210             :       }
    1211             :     }
    1212             :   }
    1213    15897390 :   i=2; while(!z[i]) i++;
    1214    15897390 :   z += i-2; lz -= (i-2);
    1215    15897390 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1216    15897390 :   z[1] = evalsigne((sx+sy)? 1: -1) | evallg(lz);
    1217    15897390 :   if (lz == 2) pari_err_OP("exact division", x, y);
    1218    15897390 :   set_avma((pari_sp)z); return z;
    1219             : }
    1220             : 
    1221             : /* assume yz != and yz | x */
    1222             : GEN
    1223      129495 : diviuuexact(GEN x, ulong y, ulong z)
    1224             : {
    1225             :   long tmp[4];
    1226             :   ulong t;
    1227             :   LOCAL_HIREMAINDER;
    1228      129495 :   t = mulll(y, z);
    1229      129495 :   if (!hiremainder) return diviuexact(x, t);
    1230           0 :   tmp[0] = evaltyp(t_INT)|_evallg(4);
    1231           0 :   tmp[1] = evalsigne(1)|evallgefint(4);
    1232           0 :   tmp[2] = hiremainder;
    1233           0 :   tmp[3] = t;
    1234           0 :   return diviiexact(x, tmp);
    1235             : }
    1236             : 
    1237             : /********************************************************************/
    1238             : /**                                                                **/
    1239             : /**               INTEGER MULTIPLICATION (BASECASE)                **/
    1240             : /**                                                                **/
    1241             : /********************************************************************/
    1242             : /* nx >= ny = num. of digits of x, y (not GEN, see mulii) */
    1243             : INLINE GEN
    1244  2259486666 : muliispec_basecase(GEN x, GEN y, long nx, long ny)
    1245             : {
    1246             :   GEN z2e,z2d,yd,xd,ye,zd;
    1247             :   long p1,lz;
    1248             :   LOCAL_HIREMAINDER;
    1249             : 
    1250  2259486666 :   if (ny == 1) return muluispec((ulong)*y, x, nx);
    1251   656536767 :   if (ny == 0) return gen_0;
    1252   655821900 :   zd = (GEN)avma; lz = nx+ny+2;
    1253   655821900 :   (void)new_chunk(lz);
    1254   655821900 :   xd = x + nx;
    1255   655821900 :   yd = y + ny;
    1256   655821900 :   ye = yd; p1 = *--xd;
    1257             : 
    1258   655821900 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1259   655821900 :   while (yd > y) *--zd = addmul(p1, *--yd);
    1260   655821900 :   *--zd = hiremainder;
    1261             : 
    1262  5009598408 :   while (xd > x)
    1263             :   {
    1264             :     LOCAL_OVERFLOW;
    1265  3697954608 :     yd = ye; p1 = *--xd;
    1266             : 
    1267  3697954608 :     z2d = --z2e;
    1268  3697954608 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1269 43032259014 :     while (yd > y)
    1270             :     {
    1271 35636349798 :       hiremainder += overflow;
    1272 35636349798 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1273             :     }
    1274  3697954608 :     *--zd = hiremainder + overflow;
    1275             :   }
    1276   655821900 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1277   655821900 :   *--zd = evalsigne(1) | evallgefint(lz);
    1278   655821900 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1279   655821900 :   set_avma((pari_sp)zd); return zd;
    1280             : }
    1281             : 
    1282             : INLINE GEN
    1283   529308105 : sqrispec_basecase(GEN x, long nx)
    1284             : {
    1285             :   GEN z2e,z2d,yd,xd,zd,x0,z0;
    1286             :   long p1,lz;
    1287             :   LOCAL_HIREMAINDER;
    1288             :   LOCAL_OVERFLOW;
    1289             : 
    1290   529308105 :   if (nx == 1) return sqru((ulong)*x);
    1291   439356306 :   if (nx == 0) return gen_0;
    1292    76042761 :   zd = (GEN)avma; lz = (nx+1) << 1;
    1293    76042761 :   z0 = new_chunk(lz);
    1294    76042761 :   if (nx == 1)
    1295             :   {
    1296           0 :     *--zd = mulll(*x, *x);
    1297           0 :     *--zd = hiremainder; goto END;
    1298             :   }
    1299    76042761 :   xd = x + nx;
    1300             : 
    1301             :   /* compute double products --> zd */
    1302    76042761 :   p1 = *--xd; yd = xd; --zd;
    1303    76042761 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1304    76042761 :   while (yd > x) *--zd = addmul(p1, *--yd);
    1305    76042761 :   *--zd = hiremainder;
    1306             : 
    1307    76042761 :   x0 = x+1;
    1308   776787696 :   while (xd > x0)
    1309             :   {
    1310             :     LOCAL_OVERFLOW;
    1311   624702174 :     p1 = *--xd; yd = xd;
    1312             : 
    1313   624702174 :     z2e -= 2; z2d = z2e;
    1314   624702174 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1315  6439234326 :     while (yd > x)
    1316             :     {
    1317  5189829978 :       hiremainder += overflow;
    1318  5189829978 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1319             :     }
    1320   624702174 :     *--zd = hiremainder + overflow;
    1321             :   }
    1322             :   /* multiply zd by 2 (put result in zd - 1) */
    1323    76042761 :   zd[-1] = ((*zd & HIGHBIT) != 0);
    1324    76042761 :   shift_left(zd, zd, 0, (nx<<1)-3, 0, 1);
    1325             : 
    1326             :   /* add the squares */
    1327    76042761 :   xd = x + nx; zd = z0 + lz;
    1328    76042761 :   p1 = *--xd;
    1329    76042761 :   zd--; *zd = mulll(p1,p1);
    1330    76042761 :   zd--; *zd = addll(hiremainder, *zd);
    1331   852830457 :   while (xd > x)
    1332             :   {
    1333   700744935 :     p1 = *--xd;
    1334   700744935 :     zd--; *zd = addll(mulll(p1,p1)+ overflow, *zd);
    1335   700744935 :     zd--; *zd = addll(hiremainder + overflow, *zd);
    1336             :   }
    1337             : 
    1338             : END:
    1339    76042761 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1340    76042761 :   *--zd = evalsigne(1) | evallgefint(lz);
    1341    76042761 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1342    76042761 :   set_avma((pari_sp)zd); return zd;
    1343             : }
    1344             : 
    1345             : /********************************************************************/
    1346             : /**                                                                **/
    1347             : /**               INTEGER MULTIPLICATION (FFT)                     **/
    1348             : /**                                                                **/
    1349             : /********************************************************************/
    1350             : 
    1351             : /*
    1352             :  Compute parameters for FFT:
    1353             :    len: result length
    1354             :    k: FFT depth.
    1355             :    n: number of blocks (2^k)
    1356             :    bs: block size
    1357             :    mod: Modulus is M=2^(BIL*mod)+1
    1358             :    ord: order of 2 in Z/MZ.
    1359             :  We must have:
    1360             :    bs*n >= l
    1361             :    2^(BIL*mod) > nb*2^(2*BIL*bs)
    1362             :    2^k | 2*BIL*mod
    1363             : */
    1364             : static void
    1365       12324 : mulliifft_params(long len, long *k, long *mod, long *bs, long *n, ulong *ord)
    1366             : {
    1367             :   long r;
    1368       12324 :   *k = expu((3*len)>>2)-3;
    1369             :   do {
    1370       12324 :     (*k)--;
    1371       12324 :     r  = *k-(TWOPOTBITS_IN_LONG+2);
    1372       12324 :     *n = 1L<<*k;
    1373       12324 :     *bs = (len+*n-1)>>*k;
    1374       12324 :     *mod= 2**bs+1;
    1375       12324 :     if (r>0)
    1376         705 :       *mod=((*mod+(1L<<r)-1)>>r)<<r;
    1377       12324 :   } while(*mod>=3**bs);
    1378       12324 :   *ord= 4**mod*BITS_IN_LONG;
    1379       12324 : }
    1380             : 
    1381             : /* Zf_: arithmetic in ring Z/MZ where M= 2^(BITS_IN_LONG*mod)+1
    1382             :  * for some mod.
    1383             :  * Do not garbage collect.
    1384             :  */
    1385             : 
    1386             : static GEN
    1387    26110848 : Zf_add(GEN a, GEN b, GEN M)
    1388             : {
    1389    26110848 :   GEN y, z = addii(a,b);
    1390    26110848 :   long mod = lgefint(M)-3;
    1391    26110848 :   long l = NLIMBS(z);
    1392    26110848 :   if (l<=mod) return z;
    1393     9901803 :   y = subiu(z, 1);
    1394     9901803 :   if (NLIMBS(y)<=mod) return z;
    1395     9901803 :   return int_normalize(y,1);
    1396             : }
    1397             : 
    1398             : static GEN
    1399    26547666 : Zf_sub(GEN a, GEN b, GEN M)
    1400             : {
    1401    26547666 :   GEN z = subii(a,b);
    1402    26547666 :   return signe(z)>=0? z: addii(M,z);
    1403             : }
    1404             : 
    1405             : /* destroy z */
    1406             : static GEN
    1407    55183320 : Zf_red_destroy(GEN z, GEN M)
    1408             : {
    1409    55183320 :   long mod = lgefint(M)-3;
    1410    55183320 :   long l = NLIMBS(z);
    1411             :   GEN y;
    1412    55183320 :   if (l<=mod) return z;
    1413    24427107 :   y = shifti(z, -mod*BITS_IN_LONG);
    1414    24427107 :   z = int_normalize(z, NLIMBS(y));
    1415    24427107 :   y = Zf_red_destroy(y, M);
    1416    24427107 :   z = subii(z, y);
    1417    24427107 :   if (signe(z)<0) z = addii(z, M);
    1418    24427107 :   return z;
    1419             : }
    1420             : 
    1421             : INLINE GEN
    1422    28465653 : Zf_shift(GEN a, ulong s, GEN M) { return Zf_red_destroy(shifti(a, s), M); }
    1423             : 
    1424             : /*
    1425             :  Multiply by sqrt(2)^s
    1426             :  We use the formula sqrt(2)=z_8*(1-z_4)) && z_8=2^(ord/16) [2^(ord/4)+1]
    1427             : */
    1428             : 
    1429             : static GEN
    1430    26110848 : Zf_mulsqrt2(GEN a, ulong s, ulong ord, GEN M)
    1431             : {
    1432    26110848 :   ulong hord = ord>>1;
    1433    26110848 :   if (!signe(a)) return gen_0;
    1434    25301457 :   if (odd(s)) /* Multiply by 2^(s/2) */
    1435             :   {
    1436      436818 :     GEN az8  = Zf_shift(a,   ord>>4, M);
    1437      436818 :     GEN az83 = Zf_shift(az8, ord>>3, M);
    1438      436818 :     a = Zf_sub(az8, az83, M);
    1439      436818 :     s--;
    1440             :   }
    1441    25301457 :   if (s < hord)
    1442    18628515 :     return Zf_shift(a, s>>1, M);
    1443             :   else
    1444     6672942 :     return subii(M,Zf_shift(a, (s-hord)>>1, M));
    1445             : }
    1446             : 
    1447             : INLINE GEN
    1448      297600 : Zf_sqr(GEN a, GEN M) { return Zf_red_destroy(sqri(a), M); }
    1449             : 
    1450             : INLINE GEN
    1451     1992960 : Zf_mul(GEN a, GEN b, GEN M) { return Zf_red_destroy(mulii(a,b), M); }
    1452             : 
    1453             : /* In place, bit reversing FFT */
    1454             : static void
    1455     4260144 : muliifft_dit(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1456             : {
    1457     4260144 :   pari_sp av = avma;
    1458             :   long i;
    1459     4260144 :   ulong j, no = (o<<1)%ord;
    1460     4260144 :   long hstep=step>>1;
    1461    21251184 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1462             :   {
    1463    16991040 :     GEN a = Zf_add(gel(FFT,i), gel(FFT,i+hstep), M);
    1464    16991040 :     GEN b = Zf_mulsqrt2(Zf_sub(gel(FFT,i), gel(FFT,i+hstep), M), j, ord, M);
    1465    16991040 :     affii(a,gel(FFT,i));
    1466    16991040 :     affii(b,gel(FFT,i+hstep));
    1467    16991040 :     set_avma(av);
    1468             :   }
    1469     4260144 :   if (hstep>1)
    1470             :   {
    1471     2118384 :     muliifft_dit(no, ord, M, FFT, d, hstep);
    1472     2118384 :     muliifft_dit(no, ord, M, FFT, d+hstep, hstep);
    1473             :   }
    1474     4260144 : }
    1475             : 
    1476             : /* In place, bit reversed FFT, inverse of muliifft_dit */
    1477             : static void
    1478     2278236 : muliifft_dis(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1479             : {
    1480     2278236 :   pari_sp av = avma;
    1481             :   long i;
    1482     2278236 :   ulong j, no = (o<<1)%ord;
    1483     2278236 :   long hstep=step>>1;
    1484     2278236 :   if (hstep>1)
    1485             :   {
    1486     1132956 :     muliifft_dis(no, ord, M, FFT, d, hstep);
    1487     1132956 :     muliifft_dis(no, ord, M, FFT, d+hstep, hstep);
    1488             :   }
    1489    11398044 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1490             :   {
    1491     9119808 :     GEN z = Zf_mulsqrt2(gel(FFT,i+hstep), j, ord, M);
    1492     9119808 :     GEN a = Zf_add(gel(FFT,i), z, M);
    1493     9119808 :     GEN b = Zf_sub(gel(FFT,i), z, M);
    1494     9119808 :     affii(a,gel(FFT,i));
    1495     9119808 :     affii(b,gel(FFT,i+hstep));
    1496     9119808 :     set_avma(av);
    1497             :   }
    1498     2278236 : }
    1499             : 
    1500             : static GEN
    1501       23376 : muliifft_spliti(GEN a, long na, long bs, long n, long mod)
    1502             : {
    1503       23376 :   GEN ap = a+na-1;
    1504       23376 :   GEN c  = cgetg(n+1, t_VEC);
    1505             :   long i,j;
    1506     4306896 :   for(i=1;i<=n;i++)
    1507             :   {
    1508     4283520 :     GEN z = cgeti(mod+3);
    1509     4283520 :     if (na)
    1510             :     {
    1511     2117409 :       long m = minss(bs, na), v=0;
    1512     2117409 :       GEN zp, aa=ap-m+1;
    1513     2117409 :       while (!*aa && v<m) {aa++; v++;}
    1514     2117409 :       zp = z+m-v+1;
    1515    47994081 :       for (j=v; j < m; j++)
    1516    45876672 :         *zp-- = *ap--;
    1517     2117409 :       ap -= v; na -= m;
    1518     2117409 :       z[1] = evalsigne(m!=v) | evallgefint(m-v+2);
    1519             :     } else
    1520     2166111 :       z[1] = evalsigne(0) | evallgefint(2);
    1521     4283520 :     gel(c, i) = z;
    1522             :   }
    1523       23376 :   return c;
    1524             : }
    1525             : 
    1526             : static GEN
    1527       12324 : muliifft_unspliti(GEN V, long bs, long len)
    1528             : {
    1529       12324 :   long s, i, j, l = lg(V);
    1530       12324 :   GEN a = cgeti(len);
    1531       12324 :   a[1] = evalsigne(1)|evallgefint(len);
    1532    62696613 :   for(i=2;i<len;i++)
    1533    62684289 :     a[i] = 0;
    1534     2302884 :   for(i=1, s=0; i<l; i++, s+=bs)
    1535             :   {
    1536     2290560 :     GEN  u = gel(V,i);
    1537     2290560 :     if (signe(u))
    1538             :     {
    1539     2158176 :       GEN ap = int_W(a,s);
    1540     2158176 :       GEN up = int_LSW(u);
    1541     2158176 :       long lu = NLIMBS(u);
    1542             :       LOCAL_OVERFLOW;
    1543     2158176 :       *ap = addll(*ap, *up--); ap--;
    1544   117527589 :       for (j=1; j<lu; j++)
    1545   115369413 :        { *ap = addllx(*ap, *up--); ap--; }
    1546     4316478 :       while (overflow)
    1547         126 :        { *ap = addll(*ap, 1); ap--; }
    1548             :     }
    1549             :   }
    1550       12324 :   return int_normalize(a,0);
    1551             : }
    1552             : 
    1553             : static GEN
    1554        1272 : sqrispec_fft(GEN a, long na)
    1555             : {
    1556        1272 :   pari_sp av, ltop = avma;
    1557        1272 :   long len = 2*na;
    1558             :   long k, mod, bs, n;
    1559             :   GEN  FFT, M;
    1560             :   long i;
    1561             :   ulong o, ord;
    1562             : 
    1563        1272 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1564        1272 :   o = ord>>k;
    1565        1272 :   M = int2n(mod*BITS_IN_LONG);
    1566        1272 :   M[2+mod] = 1;
    1567        1272 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1568        1272 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1569        1272 :   av = avma;
    1570      298872 :   for(i=1; i<=n; i++)
    1571             :   {
    1572      297600 :     affii(Zf_sqr(gel(FFT,i), M), gel(FFT,i));
    1573      297600 :     set_avma(av);
    1574             :   }
    1575        1272 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1576      298872 :   for(i=1; i<=n; i++)
    1577             :   {
    1578      297600 :     affii(Zf_shift(gel(FFT,i), (ord>>1)-k, M), gel(FFT,i));
    1579      297600 :     set_avma(av);
    1580             :   }
    1581        1272 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1582             : }
    1583             : 
    1584             : static GEN
    1585       11052 : muliispec_fft(GEN a, GEN b, long na, long nb)
    1586             : {
    1587       11052 :   pari_sp av, av2, ltop = avma;
    1588       11052 :   long len = na+nb;
    1589             :   long k, mod, bs, n;
    1590             :   GEN FFT, FFTb, M;
    1591             :   long i;
    1592             :   ulong o, ord;
    1593             : 
    1594       11052 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1595       11052 :   o = ord>>k;
    1596       11052 :   M = int2n(mod*BITS_IN_LONG);
    1597       11052 :   M[2+mod] = 1;
    1598       11052 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1599       11052 :   av=avma;
    1600       11052 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1601       11052 :   FFTb = muliifft_spliti(b, nb, bs, n, mod);
    1602       11052 :   av2 = avma;
    1603       11052 :   muliifft_dit(o, ord, M, FFTb, 0, n);
    1604     2004012 :   for(i=1; i<=n; i++)
    1605             :   {
    1606     1992960 :     affii(Zf_mul(gel(FFT,i), gel(FFTb,i), M), gel(FFT,i));
    1607     1992960 :     set_avma(av2);
    1608             :   }
    1609       11052 :   set_avma(av);
    1610       11052 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1611     2004012 :   for(i=1; i<=n; i++)
    1612             :   {
    1613     1992960 :     affii(Zf_shift(gel(FFT,i),(ord>>1)-k,M), gel(FFT,i));
    1614     1992960 :     set_avma(av);
    1615             :   }
    1616       11052 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1617             : }
    1618             : 
    1619             : /********************************************************************/
    1620             : /**                                                                **/
    1621             : /**               INTEGER MULTIPLICATION (KARATSUBA)               **/
    1622             : /**                                                                **/
    1623             : /********************************************************************/
    1624             : 
    1625             : /* return (x shifted left d words) + y. Assume d > 0, x > 0 and y >= 0 */
    1626             : static GEN
    1627   148720932 : addshiftw(GEN x, GEN y, long d)
    1628             : {
    1629   148720932 :   GEN z,z0,y0,yd, zd = (GEN)avma;
    1630   148720932 :   long a,lz,ly = lgefint(y);
    1631             : 
    1632   148720932 :   z0 = new_chunk(d);
    1633   148720932 :   a = ly-2; yd = y+ly;
    1634   148720932 :   if (a >= d)
    1635             :   {
    1636   146501808 :     y0 = yd-d; while (yd > y0) *--zd = *--yd; /* copy last d words of y */
    1637   146501808 :     a -= d;
    1638   146501808 :     if (a)
    1639   102132384 :       z = addiispec(LIMBS(x), LIMBS(y), NLIMBS(x), a);
    1640             :     else
    1641    44369424 :       z = icopy(x);
    1642             :   }
    1643             :   else
    1644             :   {
    1645     2219124 :     y0 = yd-a; while (yd > y0) *--zd = *--yd; /* copy last a words of y */
    1646     2219124 :     while (zd > z0) *--zd = 0;    /* complete with 0s */
    1647     2219124 :     z = icopy(x);
    1648             :   }
    1649   148720932 :   lz = lgefint(z)+d;
    1650   148720932 :   z[1] = evalsigne(1) | evallgefint(lz);
    1651   148720932 :   z[0] = evaltyp(t_INT) | evallg(lz); return z;
    1652             : }
    1653             : 
    1654             : /* Fast product (Karatsuba) of integers. a and b are "special" GENs
    1655             :  * c,c0,c1,c2 are genuine GENs.
    1656             :  */
    1657             : GEN
    1658  2308022883 : muliispec(GEN a, GEN b, long na, long nb)
    1659             : {
    1660             :   GEN a0,c,c0;
    1661             :   long n0, n0a, i;
    1662             :   pari_sp av;
    1663             : 
    1664  2308022883 :   if (na < nb) swapspec(a,b, na,nb);
    1665  2308022883 :   if (nb < MULII_KARATSUBA_LIMIT) return muliispec_basecase(a,b,na,nb);
    1666    48536217 :   if (nb >= MULII_FFT_LIMIT)      return muliispec_fft(a,b,na,nb);
    1667    48525165 :   i=(na>>1); n0=na-i; na=i;
    1668    48525165 :   av=avma; a0=a+na; n0a=n0;
    1669    48525165 :   while (n0a && !*a0) { a0++; n0a--; }
    1670             : 
    1671    48525165 :   if (n0a && nb > n0)
    1672    46632159 :   { /* nb <= na <= n0 */
    1673             :     GEN b0,c1,c2;
    1674             :     long n0b;
    1675             : 
    1676    46632159 :     nb -= n0;
    1677    46632159 :     c = muliispec(a,b,na,nb);
    1678    46632159 :     b0 = b+nb; n0b = n0;
    1679    46632159 :     while (n0b && !*b0) { b0++; n0b--; }
    1680    46632159 :     if (n0b)
    1681             :     {
    1682    46085754 :       c0 = muliispec(a0,b0, n0a,n0b);
    1683             : 
    1684    46085754 :       c2 = addiispec(a0,a, n0a,na);
    1685    46085754 :       c1 = addiispec(b0,b, n0b,nb);
    1686    46085754 :       c1 = muliispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1687    46085754 :       c2 = addiispec(LIMBS(c0),LIMBS(c),  NLIMBS(c0),NLIMBS(c));
    1688             : 
    1689    46085754 :       c1 = subiispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1690             :     }
    1691             :     else
    1692             :     {
    1693      546405 :       c0 = gen_0;
    1694      546405 :       c1 = muliispec(a0,b, n0a,nb);
    1695             :     }
    1696    46632159 :     c = addshiftw(c,c1, n0);
    1697             :   }
    1698             :   else
    1699             :   {
    1700     1893006 :     c = muliispec(a,b,na,nb);
    1701     1893006 :     c0 = muliispec(a0,b,n0a,nb);
    1702             :   }
    1703    48525165 :   return gerepileuptoint(av, addshiftw(c,c0, n0));
    1704             : }
    1705             : GEN
    1706      146163 : muluui(ulong x, ulong y, GEN z)
    1707             : {
    1708      146163 :   long t, s = signe(z);
    1709             :   GEN r;
    1710             :   LOCAL_HIREMAINDER;
    1711             : 
    1712      146163 :   if (!x || !y || !signe(z)) return gen_0;
    1713      145866 :   t = mulll(x,y);
    1714      145866 :   if (!hiremainder)
    1715      145866 :     r = muluispec(t, z+2, lgefint(z)-2);
    1716             :   else
    1717             :   {
    1718             :     long tmp[2];
    1719           0 :     tmp[0] = hiremainder;
    1720           0 :     tmp[1] = t;
    1721           0 :     r = muliispec(z+2,tmp,lgefint(z)-2,2);
    1722             :   }
    1723      145866 :   setsigne(r,s); return r;
    1724             : }
    1725             : 
    1726             : #define sqrispec_mirror sqrispec
    1727             : #define muliispec_mirror muliispec
    1728             : 
    1729             : /* x % (2^n), assuming n >= 0 */
    1730             : GEN
    1731    15140091 : remi2n(GEN x, long n)
    1732             : {
    1733    15140091 :   long hi,l,k,lx,ly, sx = signe(x);
    1734             :   GEN z, xd, zd;
    1735             : 
    1736    15140091 :   if (!sx || !n) return gen_0;
    1737             : 
    1738    15121941 :   k = dvmdsBIL(n, &l);
    1739    15121941 :   lx = lgefint(x);
    1740    15121941 :   if (lx < k+3) return icopy(x);
    1741             : 
    1742    15081573 :   xd = x + (lx-k-1);
    1743             :   /* x = |_|...|#|1|...|k| : copy the last l bits of # and the last k words
    1744             :    *            ^--- initial xd  */
    1745    15081573 :   hi = ((ulong)*xd) & ((1UL<<l)-1); /* last l bits of # = top bits of result */
    1746    15081573 :   if (!hi)
    1747             :   { /* strip leading zeroes from result */
    1748      291435 :     xd++; while (k && !*xd) { k--; xd++; }
    1749      291435 :     if (!k) return gen_0;
    1750       78249 :     ly = k+2; xd--;
    1751             :   }
    1752             :   else
    1753    14790138 :     ly = k+3;
    1754             : 
    1755    14868387 :   zd = z = cgeti(ly);
    1756    14868387 :   *++zd = evalsigne(sx) | evallgefint(ly);
    1757    14868387 :   if (hi) *++zd = hi;
    1758    14868387 :   for ( ;k; k--) *++zd = *++xd;
    1759    14868387 :   return z;
    1760             : }
    1761             : 
    1762             : GEN
    1763   533672151 : sqrispec(GEN a, long na)
    1764             : {
    1765             :   GEN a0,c;
    1766             :   long n0, n0a, i;
    1767             :   pari_sp av;
    1768             : 
    1769   533672151 :   if (na < SQRI_KARATSUBA_LIMIT) return sqrispec_basecase(a,na);
    1770     4364046 :   if (na >= SQRI_FFT_LIMIT) return sqrispec_fft(a,na);
    1771     4362774 :   i=(na>>1); n0=na-i; na=i;
    1772     4362774 :   av=avma; a0=a+na; n0a=n0;
    1773     4362774 :   while (n0a && !*a0) { a0++; n0a--; }
    1774     4362774 :   c = sqrispec(a,na);
    1775     4362774 :   if (n0a)
    1776             :   {
    1777     4356741 :     GEN t, c1, c0 = sqrispec(a0,n0a);
    1778             : #if 0
    1779             :     c1 = shifti(muliispec(a0,a, n0a,na),1);
    1780             : #else /* faster */
    1781     4356741 :     t = addiispec(a0,a,n0a,na);
    1782     4356741 :     t = sqrispec(LIMBS(t),NLIMBS(t));
    1783     4356741 :     c1= addiispec(LIMBS(c0),LIMBS(c), NLIMBS(c0), NLIMBS(c));
    1784     4356741 :     c1= subiispec(LIMBS(t),LIMBS(c1), NLIMBS(t), NLIMBS(c1));
    1785             : #endif
    1786     4356741 :     c = addshiftw(c,c1, n0);
    1787     4356741 :     c = addshiftw(c,c0, n0);
    1788             :   }
    1789             :   else
    1790        6033 :     c = addshiftw(c,gen_0,n0<<1);
    1791     4362774 :   return gerepileuptoint(av, c);
    1792             : }
    1793             : 
    1794             : /********************************************************************/
    1795             : /**                                                                **/
    1796             : /**                    KARATSUBA SQUARE ROOT                       **/
    1797             : /**      adapted from Paul Zimmermann's implementation of          **/
    1798             : /**      his algorithm in GMP (mpn_sqrtrem)                        **/
    1799             : /**                                                                **/
    1800             : /********************************************************************/
    1801             : 
    1802             : /* Square roots table */
    1803             : static const unsigned char approx_tab[192] = {
    1804             :   128,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,
    1805             :   143,144,144,145,146,147,148,149,150,150,151,152,153,154,155,155,
    1806             :   156,157,158,159,160,160,161,162,163,163,164,165,166,167,167,168,
    1807             :   169,170,170,171,172,173,173,174,175,176,176,177,178,178,179,180,
    1808             :   181,181,182,183,183,184,185,185,186,187,187,188,189,189,190,191,
    1809             :   192,192,193,193,194,195,195,196,197,197,198,199,199,200,201,201,
    1810             :   202,203,203,204,204,205,206,206,207,208,208,209,209,210,211,211,
    1811             :   212,212,213,214,214,215,215,216,217,217,218,218,219,219,220,221,
    1812             :   221,222,222,223,224,224,225,225,226,226,227,227,228,229,229,230,
    1813             :   230,231,231,232,232,233,234,234,235,235,236,236,237,237,238,238,
    1814             :   239,240,240,241,241,242,242,243,243,244,244,245,245,246,246,247,
    1815             :   247,248,248,249,249,250,250,251,251,252,252,253,253,254,254,255
    1816             : };
    1817             : 
    1818             : /* N[0], assume N[0] >= 2^(BIL-2).
    1819             :  * Return r,s such that s^2 + r = N, 0 <= r <= 2s */
    1820             : static void
    1821    22661580 : p_sqrtu1(ulong *N, ulong *ps, ulong *pr)
    1822             : {
    1823    22661580 :   ulong prec, r, s, q, u, n0 = N[0];
    1824             : 
    1825    22661580 :   q = n0 >> (BITS_IN_LONG - 8);
    1826             :   /* 2^6 = 64 <= q < 256 = 2^8 */
    1827    22661580 :   s = approx_tab[q - 64];                                /* 128 <= s < 255 */
    1828    22661580 :   r = (n0 >> (BITS_IN_LONG - 16)) - s * s;                /* r <= 2*s */
    1829    22661580 :   if (r > (s << 1)) { r -= (s << 1) | 1; s++; }
    1830             : 
    1831             :   /* 8-bit approximation from the high 8-bits of N[0] */
    1832    22661580 :   prec = 8;
    1833    22661580 :   n0 <<= 2 * prec;
    1834    90646320 :   while (2 * prec < BITS_IN_LONG)
    1835             :   { /* invariant: s has prec bits, and r <= 2*s */
    1836    45323160 :     r = (r << prec) + (n0 >> (BITS_IN_LONG - prec));
    1837    45323160 :     n0 <<= prec;
    1838    45323160 :     u = 2 * s;
    1839    45323160 :     q = r / u; u = r - q * u;
    1840    45323160 :     s = (s << prec) + q;
    1841    45323160 :     u = (u << prec) + (n0 >> (BITS_IN_LONG - prec));
    1842    45323160 :     q = q * q;
    1843    45323160 :     r = u - q;
    1844    45323160 :     if (u < q) { s--; r += (s << 1) | 1; }
    1845    45323160 :     n0 <<= prec;
    1846    45323160 :     prec = 2 * prec;
    1847             :   }
    1848    22661580 :   *ps = s;
    1849    22661580 :   *pr = r;
    1850    22661580 : }
    1851             : 
    1852             : /* N[0..1], assume N[0] >= 2^(BIL-2).
    1853             :  * Return 1 if remainder overflows, 0 otherwise */
    1854             : static int
    1855    20408493 : p_sqrtu2(ulong *N, ulong *ps, ulong *pr)
    1856             : {
    1857    20408493 :   ulong cc, qhl, r, s, q, u, n1 = N[1];
    1858             :   LOCAL_OVERFLOW;
    1859             : 
    1860    20408493 :   p_sqrtu1(N, &s, &r); /* r <= 2s */
    1861    20408493 :   qhl = 0; while (r >= s) { qhl++; r -= s; }
    1862             :   /* now r < s < 2^(BIL/2) */
    1863    20408493 :   r = (r << BITS_IN_HALFULONG) | (n1 >> BITS_IN_HALFULONG);
    1864    20408493 :   u = s << 1;
    1865    20408493 :   q = r / u; u = r - q * u;
    1866    20408493 :   q += (qhl & 1) << (BITS_IN_HALFULONG - 1);
    1867    20408493 :   qhl >>= 1;
    1868             :   /* (initial r)<<(BIL/2) + n1>>(BIL/2) = (qhl<<(BIL/2) + q) * 2s + u */
    1869    20408493 :   s = ((s + qhl) << BITS_IN_HALFULONG) + q;
    1870    20408493 :   cc = u >> BITS_IN_HALFULONG;
    1871    20408493 :   r = (u << BITS_IN_HALFULONG) | (n1 & LOWMASK);
    1872    20408493 :   r = subll(r, q * q);
    1873    20408493 :   cc -= overflow + qhl;
    1874             :   /* now subtract 2*q*2^(BIL/2) + 2^BIL if qhl is set */
    1875    20408493 :   if ((long)cc < 0)
    1876             :   {
    1877     5508330 :     if (s) {
    1878     5481621 :       r = addll(r, s);
    1879     5481621 :       cc += overflow;
    1880     5481621 :       s--;
    1881             :     } else {
    1882       26709 :       cc++;
    1883       26709 :       s = ~0UL;
    1884             :     }
    1885     5508330 :     r = addll(r, s);
    1886     5508330 :     cc += overflow;
    1887             :   }
    1888    20408493 :   *ps = s;
    1889    20408493 :   *pr = r; return cc;
    1890             : }
    1891             : 
    1892             : static void
    1893    19857210 : xmpn_zero(GEN x, long n)
    1894             : {
    1895    19857210 :   while (--n >= 0) x[n]=0;
    1896    19857210 : }
    1897             : static void
    1898   208400790 : xmpn_copy(GEN z, GEN x, long n)
    1899             : {
    1900   208400790 :   long k = n;
    1901   208400790 :   while (--k >= 0) z[k] = x[k];
    1902   208400790 : }
    1903             : /* a[0..la-1] * 2^(lb BIL) | b[0..lb-1] */
    1904             : static GEN
    1905    89688186 : catii(GEN a, long la, GEN b, long lb)
    1906             : {
    1907    89688186 :   long l = la + lb + 2;
    1908    89688186 :   GEN z = cgetipos(l);
    1909    89688186 :   xmpn_copy(LIMBS(z), a, la);
    1910    89688186 :   xmpn_copy(LIMBS(z) + la, b, lb);
    1911    89688186 :   return int_normalize(z, 0);
    1912             : }
    1913             : 
    1914             : /* sqrt n[0..1], assume n normalized */
    1915             : static GEN
    1916    19924950 : sqrtispec2(GEN n, GEN *pr)
    1917             : {
    1918             :   ulong s, r;
    1919    19924950 :   int hi = p_sqrtu2((ulong*)n, &s, &r);
    1920    19924950 :   GEN S = utoi(s);
    1921    19924950 :   *pr = hi? uutoi(1,r): utoi(r);
    1922    19924950 :   return S;
    1923             : }
    1924             : 
    1925             : /* sqrt n[0], _dont_ assume n normalized */
    1926             : static GEN
    1927     2253087 : sqrtispec1_sh(GEN n, GEN *pr)
    1928             : {
    1929             :   GEN S;
    1930     2253087 :   ulong r, s, u0 = uel(n,0);
    1931     2253087 :   int sh = bfffo(u0) & ~1UL;
    1932     2253087 :   if (sh) u0 <<= sh;
    1933     2253087 :   p_sqrtu1(&u0, &s, &r);
    1934             :   /* s^2 + r = u0, s < 2^(BIL/2). Rescale back:
    1935             :    * 2^(2k) n = S^2 + R
    1936             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1937     2253087 :   if (sh) {
    1938     2174571 :     int k = sh >> 1;
    1939     2174571 :     ulong s0 = s & ((1L<<k) - 1);
    1940     2174571 :     r += s * (s0<<1);
    1941     2174571 :     s >>= k;
    1942     2174571 :     r >>= sh;
    1943             :   }
    1944     2253087 :   S = utoi(s);
    1945     2253087 :   if (pr) *pr = utoi(r);
    1946     2253087 :   return S;
    1947             : }
    1948             : 
    1949             : /* sqrt n[0..1], _dont_ assume n normalized */
    1950             : static GEN
    1951      483543 : sqrtispec2_sh(GEN n, GEN *pr)
    1952             : {
    1953             :   GEN S;
    1954      483543 :   ulong U[2], r, s, u0 = uel(n,0), u1 = uel(n,1);
    1955      483543 :   int hi, sh = bfffo(u0) & ~1UL;
    1956      483543 :   if (sh) {
    1957      483438 :     u0 = (u0 << sh) | (u1 >> (BITS_IN_LONG-sh));
    1958      483438 :     u1 <<= sh;
    1959             :   }
    1960      483543 :   U[0] = u0;
    1961      483543 :   U[1] = u1; hi = p_sqrtu2(U, &s, &r);
    1962             :   /* s^2 + R = u0|u1. Rescale back:
    1963             :    * 2^(2k) n = S^2 + R
    1964             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1965      483543 :   if (sh) {
    1966      483438 :     int k = sh >> 1;
    1967      483438 :     ulong s0 = s & ((1L<<k) - 1);
    1968             :     LOCAL_HIREMAINDER;
    1969             :     LOCAL_OVERFLOW;
    1970      483438 :     r = addll(r, mulll(s, (s0<<1)));
    1971      483438 :     if (overflow) hiremainder++;
    1972      483438 :     hiremainder += hi; /* + 0 or 1 */
    1973      483438 :     s >>= k;
    1974      483438 :     r = (r>>sh) | (hiremainder << (BITS_IN_LONG-sh));
    1975      483438 :     hi = (hiremainder & (1L<<sh));
    1976             :   }
    1977      483543 :   S = utoi(s);
    1978      483543 :   if (pr) *pr = hi? uutoi(1,r): utoi(r);
    1979      483543 :   return S;
    1980             : }
    1981             : 
    1982             : /* Let N = N[0..2n-1]. Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S
    1983             :  * Assume N normalized */
    1984             : static GEN
    1985    64769043 : sqrtispec(GEN N, long n, GEN *r)
    1986             : {
    1987             :   GEN S, R, q, z, u;
    1988             :   long l, h;
    1989             : 
    1990    64769043 :   if (n == 1) return sqrtispec2(N, r);
    1991    44844093 :   l = n >> 1;
    1992    44844093 :   h = n - l; /* N = a3(h) | a2(h) | a1(l) | a0(l words) */
    1993    44844093 :   S = sqrtispec(N, h, &R); /* S^2 + R = a3|a2 */
    1994             : 
    1995    44844093 :   z = catii(LIMBS(R), NLIMBS(R), N + 2*h, l); /* = R | a1(l) */
    1996    44844093 :   q = dvmdii(z, shifti(S,1), &u);
    1997    44844093 :   z = catii(LIMBS(u), NLIMBS(u), N + n + h, l); /* = u | a0(l) */
    1998             : 
    1999    44844093 :   S = addshiftw(S, q, l);
    2000    44844093 :   R = subii(z, sqri(q));
    2001    44844093 :   if (signe(R) < 0)
    2002             :   {
    2003     7182390 :     GEN S2 = shifti(S,1);
    2004     7182390 :     R = addis(subiispec(LIMBS(S2),LIMBS(R), NLIMBS(S2),NLIMBS(R)), -1);
    2005     7182390 :     S = addis(S, -1);
    2006             :   }
    2007    44844093 :   *r = R; return S;
    2008             : }
    2009             : 
    2010             : /* Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S.
    2011             :  * As for dvmdii, R is last on stack and guaranteed to be gen_0 in case the
    2012             :  * remainder is 0. R = NULL is allowed. */
    2013             : GEN
    2014     2804424 : sqrtremi(GEN N, GEN *r)
    2015             : {
    2016             :   pari_sp av;
    2017     2804424 :   GEN S, R, n = N+2;
    2018     2804424 :   long k, l2, ln = NLIMBS(N);
    2019             :   int sh;
    2020             : 
    2021     2804424 :   if (ln <= 2)
    2022             :   {
    2023     2736684 :     if (ln == 2) return sqrtispec2_sh(n, r);
    2024     2253141 :     if (ln == 1) return sqrtispec1_sh(n, r);
    2025          54 :     if (r) *r = gen_0;
    2026          54 :     return gen_0;
    2027             :   }
    2028       67740 :   av = avma;
    2029       67740 :   sh = bfffo(n[0]) >> 1;
    2030       67740 :   l2 = (ln + 1) >> 1;
    2031      135300 :   if (sh || (ln & 1)) { /* normalize n, so that n[0] >= 2^BIL / 4 */
    2032       67560 :     GEN s0, t = new_chunk(ln + 1);
    2033       67560 :     t[ln] = 0;
    2034       67560 :     if (sh)
    2035       67215 :       shift_left(t, n, 0,ln-1, 0, sh << 1);
    2036             :     else
    2037         345 :       xmpn_copy(t, n, ln);
    2038       67560 :     S = sqrtispec(t, l2, &R); /* t normalized, 2 * l2 words */
    2039             :     /* Rescale back:
    2040             :      * 2^(2k) n = S^2 + R, k = sh + (ln & 1)*BIL/2
    2041             :      * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    2042       67560 :     k = sh + (ln & 1) * (BITS_IN_LONG/2);
    2043       67560 :     s0 = remi2n(S, k);
    2044       67560 :     R = addii(shifti(R,-1), mulii(s0, S));
    2045       67560 :     R = shifti(R, 1 - (k<<1));
    2046       67560 :     S = shifti(S, -k);
    2047             :   }
    2048             :   else
    2049         180 :     S = sqrtispec(n, l2, &R);
    2050             : 
    2051       67740 :   if (!r) { set_avma((pari_sp)S); return gerepileuptoint(av, S); }
    2052        6801 :   gerepileall(av, 2, &S, &R); *r = R; return S;
    2053             : }
    2054             : 
    2055             : /* compute sqrt(|a|), assuming a != 0 */
    2056             : 
    2057             : #if 1
    2058             : GEN
    2059    19857210 : sqrtr_abs(GEN x)
    2060             : {
    2061    19857210 :   long l = realprec(x) - 2, e = expo(x), er = e>>1;
    2062    19857210 :   GEN b, c, res = cgetr(2 + l);
    2063    19857210 :   res[1] = evalsigne(1) | evalexpo(er);
    2064    19857210 :   if (e&1) {
    2065     9166863 :     b = new_chunk(l << 1);
    2066     9166863 :     xmpn_copy(b, x+2, l);
    2067     9166863 :     xmpn_zero(b + l,l);
    2068     9166863 :     b = sqrtispec(b, l, &c);
    2069     9166863 :     xmpn_copy(res+2, b+2, l);
    2070     9166863 :     if (cmpii(c, b) > 0) roundr_up_ip(res, l+2);
    2071             :   } else {
    2072             :     ulong u;
    2073    10690347 :     b = new_chunk(2 + (l << 1));
    2074    10690347 :     shift_left(b+1, x+2, 0,l-1, 0, BITS_IN_LONG-1);
    2075    10690347 :     b[0] = uel(x,2)>>1;
    2076    10690347 :     xmpn_zero(b + l+1,l+1);
    2077    10690347 :     b = sqrtispec(b, l+1, &c);
    2078    10690347 :     xmpn_copy(res+2, b+2, l);
    2079    10690347 :     u = uel(b,l+2);
    2080    10690347 :     if ( u&HIGHBIT || (u == ~HIGHBIT && cmpii(c,b) > 0))
    2081     5248539 :       roundr_up_ip(res, l+2);
    2082             :   }
    2083    19857210 :   set_avma((pari_sp)res); return res;
    2084             : }
    2085             : 
    2086             : #else /* use t_REAL: currently much slower (quadratic division) */
    2087             : 
    2088             : #ifdef LONG_IS_64BIT
    2089             : /* 64 bits of b = sqrt(a[0] * 2^64 + a[1])  [ up to 1ulp ] */
    2090             : static ulong
    2091             : sqrtu2(ulong *a)
    2092             : {
    2093             :   ulong c, b = dblmantissa( sqrt((double)a[0]) );
    2094             :   LOCAL_HIREMAINDER;
    2095             :   LOCAL_OVERFLOW;
    2096             : 
    2097             :   /* > 32 correct bits, 1 Newton iteration to reach 64 */
    2098             :   if (b <= a[0]) return HIGHBIT | (a[0] >> 1);
    2099             :   hiremainder = a[0]; c = divll(a[1], b);
    2100             :   return (addll(c, b) >> 1) | HIGHBIT;
    2101             : }
    2102             : /* 64 bits of sqrt(a[0] * 2^63) */
    2103             : static ulong
    2104             : sqrtu2_1(ulong *a)
    2105             : {
    2106             :   ulong t[2];
    2107             :   t[0] = (a[0] >> 1);
    2108             :   t[1] = (a[0] << (BITS_IN_LONG-1)) | (a[1] >> 1);
    2109             :   return sqrtu2(t);
    2110             : }
    2111             : #else
    2112             : /* 32 bits of sqrt(a[0] * 2^32) */
    2113             : static ulong
    2114             : sqrtu2(ulong *a)   { return dblmantissa( sqrt((double)a[0]) ); }
    2115             : /* 32 bits of sqrt(a[0] * 2^31) */
    2116             : static ulong
    2117             : sqrtu2_1(ulong *a) { return dblmantissa( sqrt(2. * a[0]) ); }
    2118             : #endif
    2119             : 
    2120             : GEN
    2121             : sqrtr_abs(GEN x)
    2122             : {
    2123             :   long l1, i, l = lg(x), ex = expo(x);
    2124             :   GEN a, t, y = cgetr(l);
    2125             :   pari_sp av, av0 = avma;
    2126             : 
    2127             :   a = rtor(x, l+1);
    2128             :   t = cgetr(l+1);
    2129             :   if (ex & 1) { /* odd exponent */
    2130             :     a[1] = evalsigne(1) | _evalexpo(1);
    2131             :     t[2] = (long)sqrtu2((ulong*)a + 2);
    2132             :   } else { /* even exponent */
    2133             :     a[1] = evalsigne(1) | _evalexpo(0);
    2134             :     t[2] = (long)sqrtu2_1((ulong*)a + 2);
    2135             :   }
    2136             :   t[1] = evalsigne(1) | _evalexpo(0);
    2137             :   for (i = 3; i <= l; i++) t[i] = 0;
    2138             : 
    2139             :   /* |x| = 2^(ex/2) a, t ~ sqrt(a) */
    2140             :   l--; l1 = 1; av = avma;
    2141             :   while (l1 < l) { /* let t := (t + a/t)/2 */
    2142             :     l1 <<= 1; if (l1 > l) l1 = l;
    2143             :     setlg(a, l1 + 2);
    2144             :     setlg(t, l1 + 2);
    2145             :     affrr(addrr(t, divrr(a,t)), t); shiftr_inplace(t, -1);
    2146             :     set_avma(av);
    2147             :   }
    2148             :   affrr(t,y); shiftr_inplace(y, (ex>>1));
    2149             :   set_avma(av0); return y;
    2150             : }
    2151             : 
    2152             : #endif
    2153             : 
    2154             : /*******************************************************************
    2155             :  *                                                                 *
    2156             :  *                           Base Conversion                       *
    2157             :  *                                                                 *
    2158             :  *******************************************************************/
    2159             : 
    2160             : static void
    2161      675858 : convi_dac(GEN x, ulong l, ulong *res)
    2162             : {
    2163      675858 :   pari_sp ltop=avma;
    2164             :   ulong m;
    2165             :   GEN x1,x2;
    2166      675858 :   if (l==1) { *res=itou(x); return; }
    2167      321291 :   m=l>>1;
    2168      321291 :   x1=dvmdii(x,powuu(1000000000UL,m),&x2);
    2169      321291 :   convi_dac(x1,l-m,res+m);
    2170      321291 :   convi_dac(x2,m,res);
    2171      321291 :   set_avma(ltop);
    2172             : }
    2173             : 
    2174             : /* convert integer --> base 10^9 [not memory clean] */
    2175             : ulong *
    2176      245912 : convi(GEN x, long *l)
    2177             : {
    2178      245912 :   long lz, lx = lgefint(x);
    2179             :   ulong *z;
    2180      245912 :   if (lx == 3 && uel(x,2) < 1000000000UL) {
    2181      212636 :     z = (ulong*)new_chunk(1);
    2182      212636 :     *z = x[2];
    2183      212636 :     *l = 1; return z+1;
    2184             :   }
    2185       33276 :   lz = 1 + (long)bit_accuracy_mul(lx, LOG10_2/9);
    2186       33276 :   z = (ulong*)new_chunk(lz);
    2187       33276 :   convi_dac(x,(ulong)lz,z);
    2188       33276 :   while (z[lz-1]==0) lz--;
    2189       33276 :   *l=lz; return z+lz;
    2190             : }
    2191             : 

Generated by: LCOV version 1.13