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.0 lcov report (development 23022-5b494f78e) Lines: 1119 1151 97.2 %
Date: 2018-09-24 05:37:44 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         656 : void pari_kernel_init(void) { }
      25         654 : 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   173688624 : int_normalize(GEN x, long known_zero_words)
      38             : {
      39   173688624 :   long i, lx = lgefint(x);
      40             :   GEN x0;
      41   173688624 :   if (lx == 2) { x[1] = evalsigne(0) | evallgefint(2); return x; }
      42   173688624 :   if (!known_zero_words && x[2]) return x;
      43   721713507 :   for (i = 2+known_zero_words; i < lx; i++)
      44   701106366 :     if (x[i]) break;
      45    79346334 :   x0 = x; i -= 2; x += i;
      46    79346334 :   if (x0 == (GEN)avma) avma = (pari_sp)x;
      47    32861043 :   else stackdummy((pari_sp)(x0+i), (pari_sp)x0);
      48    79346334 :   lx -= i;
      49    79346334 :   x[0] = evaltyp(t_INT) | evallg(lx);
      50    79346334 :   if (lx == 2) x[1] = evalsigne(0) | evallgefint(lx);
      51    58739193 :   else         x[1] = evalsigne(1) | evallgefint(lx);
      52    79346334 :   return x;
      53             : }
      54             : 
      55             : /***********************************************************************/
      56             : /**                                                                   **/
      57             : /**                      ADDITION / SUBTRACTION                       **/
      58             : /**                                                                   **/
      59             : /***********************************************************************/
      60             : 
      61             : GEN
      62     2174991 : setloop(GEN a)
      63             : {
      64     2174991 :   pari_sp av = avma;
      65     2174991 :   (void)cgetg(lgefint(a) + 3, t_VECSMALL);
      66     2174991 :   return icopy_avma(a, av); /* two cells of extra space before a */
      67             : }
      68             : 
      69             : /* we had a = setloop(?), then some incloops. Reset a to b */
      70             : GEN
      71      127959 : resetloop(GEN a, GEN b) {
      72      127959 :   long lb = lgefint(b);
      73      127959 :   a += lgefint(a) - lb;
      74      127959 :   a[0] = evaltyp(t_INT) | evallg(lb);
      75      127959 :   affii(b, a); return a;
      76             : }
      77             : 
      78             : /* assume a > 0, initialized by setloop. Do a++ */
      79             : static GEN
      80    20168568 : incpos(GEN a)
      81             : {
      82    20168568 :   long i, l = lgefint(a);
      83    20168571 :   for (i=l-1; i>1; i--)
      84    20168568 :     if (++a[i]) return a;
      85           3 :   l++; a--; /* use extra cell */
      86           3 :   a[0]=evaltyp(t_INT) | _evallg(l);
      87           3 :   a[1]=evalsigne(1) | evallgefint(l);
      88           3 :   a[2]=1; return a;
      89             : }
      90             : 
      91             : /* assume a < 0, initialized by setloop. Do a++ */
      92             : static GEN
      93        4704 : incneg(GEN a)
      94             : {
      95        4704 :   long i, l = lgefint(a)-1;
      96        4704 :   if (uel(a,l)--)
      97             :   {
      98        4701 :     if (l == 2 && !a[2])
      99             :     {
     100         417 :       a++; /* save one cell */
     101         417 :       a[0] = evaltyp(t_INT) | _evallg(2);
     102         417 :       a[1] = evalsigne(0) | evallgefint(2);
     103             :     }
     104        4701 :     return a;
     105             :   }
     106           3 :   for (i = l-1;; i--) /* finishes since a[2] != 0 */
     107           3 :     if (uel(a,i)--) break;
     108           3 :   if (!a[2])
     109             :   {
     110           3 :     a++; /* save one cell */
     111           3 :     a[0] = evaltyp(t_INT) | _evallg(l);
     112           3 :     a[1] = evalsigne(-1) | evallgefint(l);
     113             :   }
     114           3 :   return a;
     115             : }
     116             : 
     117             : /* assume a initialized by setloop. Do a++ */
     118             : GEN
     119    20421990 : incloop(GEN a)
     120             : {
     121    20421990 :   switch(signe(a))
     122             :   {
     123      248718 :     case 0: a--; /* use extra cell */
     124      248718 :       a[0]=evaltyp(t_INT) | _evallg(3);
     125      248718 :       a[1]=evalsigne(1) | evallgefint(3);
     126      248718 :       a[2]=1; return a;
     127        4704 :     case -1: return incneg(a);
     128    20168568 :     default: return incpos(a);
     129             :   }
     130             : }
     131             : 
     132             : INLINE GEN
     133   891965568 : adduispec(ulong s, GEN x, long nx)
     134             : {
     135   891965568 :   GEN xd, zd = (GEN)avma;
     136             :   long lz;
     137             : 
     138   891965568 :   if (nx == 1) return adduu(s, uel(x,0));
     139   112492593 :   lz = nx+3; (void)new_chunk(lz);
     140   112492593 :   xd = x + nx;
     141   112492593 :   *--zd = (ulong)*--xd + s;
     142   112492593 :   if ((ulong)*zd < s)
     143             :     for(;;)
     144             :     {
     145    21752571 :       if (xd == x) { *--zd = 1; break; } /* enlarge z */
     146    12308382 :       *--zd = ((ulong)*--xd) + 1;
     147    12308382 :       if (*zd) { lz--; break; }
     148             :     }
     149   102398574 :   else lz--;
     150   112492593 :   while (xd > x) *--zd = *--xd;
     151   112492593 :   *--zd = evalsigne(1) | evallgefint(lz);
     152   112492593 :   *--zd = evaltyp(t_INT) | evallg(lz);
     153   112492593 :   avma=(pari_sp)zd; return zd;
     154             : }
     155             : 
     156             : GEN
     157   138916740 : adduispec_offset(ulong s, GEN x, long offset, long nx)
     158             : {
     159   138916740 :   GEN xd = x+lgefint(x)-nx-offset;
     160   138916740 :   while (nx && *xd==0) {xd++; nx--;}
     161   138916740 :   if (!nx) return utoi(s);
     162   127828233 :   return adduispec(s,xd,nx);
     163             : }
     164             : 
     165             : static GEN
     166  1767554325 : addiispec(GEN x, GEN y, long nx, long ny)
     167             : {
     168             :   GEN xd, yd, zd;
     169  1767554325 :   long lz, i = -2;
     170             :   LOCAL_OVERFLOW;
     171             : 
     172  1767554325 :   if (nx < ny) swapspec(x,y, nx,ny);
     173  1767554325 :   if (ny == 1) return adduispec(*y,x,nx);
     174  1048270419 :   zd = (GEN)avma;
     175  1048270419 :   lz = nx+3; (void)new_chunk(lz);
     176  1048270419 :   xd = x + nx;
     177  1048270419 :   yd = y + ny;
     178  1048270419 :   zd[-1] = addll(xd[-1], yd[-1]);
     179             : #ifdef addllx8
     180   676417306 :   for (  ; i-8 > -ny; i-=8)
     181   326993833 :     addllx8(xd+i, yd+i, zd+i, overflow);
     182             : #endif
     183  1048270419 :   for (  ; i >= -ny; i--) zd[i] = addllx(xd[i], yd[i]);
     184  1048270419 :   if (overflow)
     185             :     for(;;)
     186             :     {
     187   160128360 :       if (i < -nx) { zd[i] = 1; i--; break; } /* enlarge z */
     188    92358699 :       zd[i] = uel(xd,i) + 1;
     189    92358699 :       if (zd[i]) { i--; lz--; break; }
     190    31654041 :       i--;
     191             :     }
     192   951450141 :   else lz--;
     193  1048270419 :   for (; i >= -nx; i--) zd[i] = xd[i];
     194  1048270419 :   zd += i+1;
     195  1048270419 :   *--zd = evalsigne(1) | evallgefint(lz);
     196  1048270419 :   *--zd = evaltyp(t_INT) | evallg(lz);
     197  1048270419 :   avma=(pari_sp)zd; return zd;
     198             : }
     199             : 
     200             : /* assume x >= s */
     201             : INLINE GEN
     202   686208665 : subiuspec(GEN x, ulong s, long nx)
     203             : {
     204   686208665 :   GEN xd, zd = (GEN)avma;
     205             :   long lz;
     206             :   LOCAL_OVERFLOW;
     207             : 
     208   686208665 :   if (nx == 1) return utoi(x[0] - s);
     209             : 
     210    83785249 :   lz = nx+2; (void)new_chunk(lz);
     211    83785249 :   xd = x + nx;
     212    83785249 :   *--zd = subll(*--xd, s);
     213    83785249 :   if (overflow)
     214             :     for(;;)
     215             :     {
     216    39618357 :       *--zd = ((ulong)*--xd) - 1;
     217    36778836 :       if (*xd) break;
     218             :     }
     219    83785249 :   if (xd == x)
     220    33384240 :     while (*zd == 0) { zd++; lz--; } /* shorten z */
     221             :   else
     222   621281909 :     do  *--zd = *--xd; while (xd > x);
     223    83785249 :   *--zd = evalsigne(1) | evallgefint(lz);
     224    83785249 :   *--zd = evaltyp(t_INT) | evallg(lz);
     225    83785249 :   avma=(pari_sp)zd; return zd;
     226             : }
     227             : 
     228             : /* assume x > y */
     229             : static GEN
     230  1360931470 : subiispec(GEN x, GEN y, long nx, long ny)
     231             : {
     232             :   GEN xd,yd,zd;
     233  1360931470 :   long lz, i = -2;
     234             :   LOCAL_OVERFLOW;
     235             : 
     236  1360931470 :   if (ny==1) return subiuspec(x,*y,nx);
     237   699922322 :   zd = (GEN)avma;
     238   699922322 :   lz = nx+2; (void)new_chunk(lz);
     239   699922322 :   xd = x + nx;
     240   699922322 :   yd = y + ny;
     241   699922322 :   zd[-1] = subll(xd[-1], yd[-1]);
     242             : #ifdef subllx8
     243   497896965 :   for (  ; i-8 > -ny; i-=8)
     244   264589524 :     subllx8(xd+i, yd+i, zd+i, overflow);
     245             : #endif
     246   699922322 :   for (  ; i >= -ny; i--) zd[i] = subllx(xd[i], yd[i]);
     247   699922322 :   if (overflow)
     248             :     for(;;)
     249             :     {
     250   252173474 :       zd[i] = uel(xd,i) - 1;
     251   158146814 :       if (xd[i--]) break;
     252             :     }
     253   699922322 :   if (i>=-nx)
     254    82056446 :     for (; i >= -nx; i--) zd[i] = xd[i];
     255             :   else
     256   617865876 :     while (zd[i+1] == 0) { i++; lz--; } /* shorten z */
     257   699922322 :   zd += i+1;
     258   699922322 :   *--zd = evalsigne(1) | evallgefint(lz);
     259   699922322 :   *--zd = evaltyp(t_INT) | evallg(lz);
     260   699922322 :   avma=(pari_sp)zd; return zd;
     261             : }
     262             : 
     263             : static void
     264   264124694 : roundr_up_ip(GEN x, long l)
     265             : {
     266   264124694 :   long i = l;
     267             :   for(;;)
     268             :   {
     269   266205392 :     if (++uel(x,--i)) break;
     270     1137039 :     if (i == 2) { x[2] = (long)HIGHBIT; shiftr_inplace(x, 1); break; }
     271             :   }
     272   264124694 : }
     273             : 
     274             : void
     275   109706268 : affir(GEN x, GEN y)
     276             : {
     277   109706268 :   const long s = signe(x), ly = lg(y);
     278             :   long lx, sh, i;
     279             : 
     280   109706268 :   if (!s)
     281             :   {
     282     3867294 :     y[1] = evalexpo(-prec2nbits(ly));
     283     3867294 :     return;
     284             :   }
     285             : 
     286   105838974 :   lx = lgefint(x); sh = bfffo(x[2]);
     287   105838974 :   y[1] = evalsigne(s) | evalexpo(bit_accuracy(lx)-sh-1);
     288   105838974 :   if (sh) {
     289   104942208 :     if (lx <= ly)
     290             :     {
     291    68193318 :       for (i=lx; i<ly; i++) y[i]=0;
     292    68193318 :       shift_left(y,x,2,lx-1, 0,sh);
     293    68193318 :       return;
     294             :     }
     295    36748890 :     shift_left(y,x,2,ly-1, x[ly],sh);
     296             :     /* lx > ly: round properly */
     297    36748890 :     if ((uel(x,ly)<<sh) & HIGHBIT) roundr_up_ip(y, ly);
     298             :   }
     299             :   else {
     300      896766 :     if (lx <= ly)
     301             :     {
     302      483957 :       for (i=2; i<lx; i++) y[i]=x[i];
     303      483957 :       for (   ; i<ly; i++) y[i]=0;
     304      483957 :       return;
     305             :     }
     306      412809 :     for (i=2; i<ly; i++) y[i]=x[i];
     307             :     /* lx > ly: round properly */
     308      412809 :     if (uel(x,ly) & HIGHBIT) roundr_up_ip(y, ly);
     309             :   }
     310             : }
     311             : 
     312             : INLINE GEN
     313   410964274 : shiftispec(GEN x, long nx, long n)
     314             : {
     315             :   long ny, i, m;
     316             :   GEN y, yd;
     317   410964274 :   if (!n)  return icopyspec(x, nx);
     318             : 
     319   390406210 :   if (n > 0)
     320             :   {
     321   248063760 :     GEN z = (GEN)avma;
     322   248063760 :     long d = dvmdsBIL(n, &m);
     323             : 
     324   248063760 :     ny = nx+d; y = new_chunk(ny + 2); yd = y + 2;
     325   248063760 :     for ( ; d; d--) *--z = 0;
     326   248063760 :     if (!m) for (i=0; i<nx; i++) yd[i]=x[i];
     327             :     else
     328             :     {
     329   244994106 :       register const ulong sh = BITS_IN_LONG - m;
     330   244994106 :       shift_left(yd,x, 0,nx-1, 0,m);
     331   244994106 :       i = uel(x,0) >> sh;
     332             :       /* Extend y on the left? */
     333   244994106 :       if (i) { ny++; y = new_chunk(1); y[2] = i; }
     334             :     }
     335             :   }
     336             :   else
     337             :   {
     338   142342450 :     ny = nx - dvmdsBIL(-n, &m);
     339   142342450 :     if (ny<1) return gen_0;
     340   142112515 :     y = new_chunk(ny + 2); yd = y + 2;
     341   142112515 :     if (m) {
     342   119713780 :       shift_right(yd,x, 0,ny, 0,m);
     343   119713780 :       if (yd[0] == 0)
     344             :       {
     345     8247696 :         if (ny==1) { avma = (pari_sp)(y+3); return gen_0; }
     346     7213089 :         ny--; avma = (pari_sp)(++y);
     347             :       }
     348             :     } else {
     349    22398735 :       for (i=0; i<ny; i++) yd[i]=x[i];
     350             :     }
     351             :   }
     352   389141668 :   y[1] = evalsigne(1)|evallgefint(ny + 2);
     353   389141668 :   y[0] = evaltyp(t_INT)|evallg(ny + 2); return y;
     354             : }
     355             : 
     356             : GEN
     357    11838393 : mantissa2nr(GEN x, long n)
     358             : { /*This is a kludge since x is not an integer*/
     359    11838393 :   long s = signe(x);
     360             :   GEN y;
     361             : 
     362    11838393 :   if(s == 0) return gen_0;
     363    11837523 :   y = shiftispec(x + 2, lg(x) - 2, n);
     364    11837523 :   if (signe(y)) setsigne(y, s);
     365    11837523 :   return y;
     366             : }
     367             : 
     368             : GEN
     369     1439142 : truncr(GEN x)
     370             : {
     371             :   long d,e,i,s,m;
     372             :   GEN y;
     373             : 
     374     1439142 :   if ((s=signe(x)) == 0 || (e=expo(x)) < 0) return gen_0;
     375     1167570 :   d = nbits2lg(e+1); m = remsBIL(e);
     376     1167570 :   if (d > lg(x)) pari_err_PREC( "truncr (precision loss in truncation)");
     377             : 
     378     1167570 :   y=cgeti(d); y[1] = evalsigne(s) | evallgefint(d);
     379     1167570 :   if (++m == BITS_IN_LONG)
     380          90 :     for (i=2; i<d; i++) y[i]=x[i];
     381             :   else
     382     1167480 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     383     1167570 :   return y;
     384             : }
     385             : 
     386             : /* integral part */
     387             : GEN
     388      543120 : floorr(GEN x)
     389             : {
     390             :   long d,e,i,lx,m;
     391             :   GEN y;
     392             : 
     393      543120 :   if (signe(x) >= 0) return truncr(x);
     394      179727 :   if ((e=expo(x)) < 0) return gen_m1;
     395       67227 :   d = nbits2lg(e+1); m = remsBIL(e);
     396       67227 :   lx=lg(x); if (d>lx) pari_err_PREC( "floorr (precision loss in truncation)");
     397       67227 :   y = new_chunk(d);
     398       67227 :   if (++m == BITS_IN_LONG)
     399             :   {
     400           6 :     for (i=2; i<d; i++) y[i]=x[i];
     401           6 :     i=d; while (i<lx && !x[i]) i++;
     402           6 :     if (i==lx) goto END;
     403             :   }
     404             :   else
     405             :   {
     406       67221 :     shift_right(y,x, 2,d,0, BITS_IN_LONG - m);
     407       67221 :     if (uel(x,d-1)<<m == 0)
     408             :     {
     409       16368 :       i=d; while (i<lx && !x[i]) i++;
     410       16368 :       if (i==lx) goto END;
     411             :     }
     412             :   }
     413             :   /* set y:=y+1 */
     414       58800 :   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       67227 :   y[1] = evalsigne(-1) | evallgefint(d);
     418       67227 :   y[0] = evaltyp(t_INT) | evallg(d); return y;
     419             : }
     420             : 
     421             : INLINE int
     422  1780670382 : cmpiispec(GEN x, GEN y, long lx, long ly)
     423             : {
     424             :   long i;
     425  1780670382 :   if (lx < ly) return -1;
     426  1704796716 :   if (lx > ly) return  1;
     427  1576716325 :   i = 0; while (i<lx && x[i]==y[i]) i++;
     428  1576716325 :   if (i==lx) return 0;
     429  1476356807 :   return (uel(x,i) > uel(y,i))? 1: -1;
     430             : }
     431             : 
     432             : INLINE int
     433    89269061 : equaliispec(GEN x, GEN y, long lx, long ly)
     434             : {
     435             :   long i;
     436    89269061 :   if (lx != ly) return 0;
     437    89269028 :   i = ly-1; while (i>=0 && x[i]==y[i]) i--;
     438    89269028 :   return i < 0;
     439             : }
     440             : 
     441             : /***********************************************************************/
     442             : /**                                                                   **/
     443             : /**                          MULTIPLICATION                           **/
     444             : /**                                                                   **/
     445             : /***********************************************************************/
     446             : /* assume ny > 0 */
     447             : INLINE GEN
     448  1812952489 : muluispec(ulong x, GEN y, long ny)
     449             : {
     450  1812952489 :   GEN yd, z = (GEN)avma;
     451  1812952489 :   long lz = ny+3;
     452             :   LOCAL_HIREMAINDER;
     453             : 
     454  1812952489 :   (void)new_chunk(lz);
     455  1812952489 :   yd = y + ny; *--z = mulll(x, *--yd);
     456  1812952489 :   while (yd > y) *--z = addmul(x,*--yd);
     457  1812952489 :   if (hiremainder) *--z = hiremainder; else lz--;
     458  1812952489 :   *--z = evalsigne(1) | evallgefint(lz);
     459  1812952489 :   *--z = evaltyp(t_INT) | evallg(lz);
     460  1812952489 :   avma=(pari_sp)z; return z;
     461             : }
     462             : 
     463             : /* a + b*|Y| */
     464             : GEN
     465      534990 : 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      534990 :   if (!b || !signe(Y)) return utoi(a);
     473             : 
     474      534987 :   y = LIMBS(Y); z = (GEN)avma;
     475      534987 :   ny = NLIMBS(Y);
     476      534987 :   lz = ny+3;
     477             : 
     478      534987 :   (void)new_chunk(lz);
     479      534987 :   yd = y + ny; *--z = addll(a, mulll(b, *--yd));
     480      534987 :   if (overflow) hiremainder++; /* can't overflow */
     481      534987 :   while (yd > y) *--z = addmul(b,*--yd);
     482      534987 :   if (hiremainder) *--z = hiremainder; else lz--;
     483      534987 :   *--z = evalsigne(1) | evallgefint(lz);
     484      534987 :   *--z = evaltyp(t_INT) | evallg(lz);
     485      534987 :   avma=(pari_sp)z; return z;
     486             : }
     487             : 
     488             : /***********************************************************************/
     489             : /**                                                                   **/
     490             : /**                          DIVISION                                 **/
     491             : /**                                                                   **/
     492             : /***********************************************************************/
     493             : 
     494             : ulong
     495  1026897100 : umodiu(GEN y, ulong x)
     496             : {
     497  1026897100 :   long sy=signe(y),ly,i;
     498             :   ulong xi;
     499             :   LOCAL_HIREMAINDER;
     500             : 
     501  1026897100 :   if (!x) pari_err_INV("umodiu",gen_0);
     502  1026897100 :   if (!sy) return 0;
     503   894819919 :   ly = lgefint(y);
     504   894819919 :   if (x <= uel(y,2))
     505             :   {
     506   352880110 :     hiremainder=0;
     507   352880110 :     if (ly==3)
     508             :     {
     509   339437968 :       hiremainder=uel(y,2)%x;
     510   339437968 :       if (!hiremainder) return 0;
     511   312391879 :       return (sy > 0)? hiremainder: x - hiremainder;
     512             :     }
     513             :   }
     514             :   else
     515             :   {
     516   541939809 :     if (ly==3) return (sy > 0)? uel(y,2): x - uel(y,2);
     517    27931092 :     hiremainder=uel(y,2); ly--; y++;
     518             :   }
     519    41373234 :   xi = get_Fl_red(x);
     520    41373234 :   for (i=2; i<ly; i++) (void)divll_pre(y[i],x,xi);
     521    41373234 :   if (!hiremainder) return 0;
     522    37551720 :   return (sy > 0)? hiremainder: x - hiremainder;
     523             : }
     524             : 
     525             : /* return |y| \/ x */
     526             : GEN
     527   110691933 : absdiviu_rem(GEN y, ulong x, ulong *rem)
     528             : {
     529             :   long ly,i;
     530             :   GEN z;
     531             :   ulong xi;
     532             :   LOCAL_HIREMAINDER;
     533             : 
     534   110691933 :   if (!x) pari_err_INV("absdiviu_rem",gen_0);
     535   110691933 :   if (!signe(y)) { *rem = 0; return gen_0; }
     536             : 
     537   110279787 :   ly = lgefint(y);
     538   110279787 :   if (x <= uel(y,2))
     539             :   {
     540    92500299 :     hiremainder=0;
     541    92500299 :     if (ly==3)
     542             :     {
     543    31226457 :       z = cgetipos(3);
     544    31226457 :       z[2] = divll(uel(y,2),x);
     545    31226457 :       *rem = hiremainder; return z;
     546             :     }
     547             :   }
     548             :   else
     549             :   {
     550    17779488 :     if (ly==3) { *rem = uel(y,2); return gen_0; }
     551    13074393 :     hiremainder = uel(y,2); ly--; y++;
     552             :   }
     553    74348235 :   xi = get_Fl_red(x);
     554    74348235 :   z = cgetipos(ly);
     555    74348235 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     556    74348235 :   *rem = hiremainder; return z;
     557             : }
     558             : 
     559             : GEN
     560    33627087 : divis_rem(GEN y, long x, long *rem)
     561             : {
     562    33627087 :   long sy=signe(y),ly,s,i;
     563             :   GEN z;
     564             :   ulong xi;
     565             :   LOCAL_HIREMAINDER;
     566             : 
     567    33627087 :   if (!x) pari_err_INV("divis_rem",gen_0);
     568    33627087 :   if (!sy) { *rem=0; return gen_0; }
     569    24627300 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     570             : 
     571    24627300 :   ly = lgefint(y);
     572    24627300 :   if ((ulong)x <= uel(y,2))
     573             :   {
     574    14851245 :     hiremainder=0;
     575    14851245 :     if (ly==3)
     576             :     {
     577    14518836 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     578    14518836 :       z[2] = divll(uel(y,2),x);
     579    14518836 :       if (sy<0) hiremainder = - ((long)hiremainder);
     580    14518836 :       *rem = (long)hiremainder; return z;
     581             :     }
     582             :   }
     583             :   else
     584             :   {
     585     9776055 :     if (ly==3) { *rem = itos(y); return gen_0; }
     586       98064 :     hiremainder = uel(y,2); ly--; y++;
     587             :   }
     588      430473 :   xi = get_Fl_red(x);
     589      430473 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     590      430473 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x,xi);
     591      430473 :   if (sy<0) hiremainder = - ((long)hiremainder);
     592      430473 :   *rem = (long)hiremainder; return z;
     593             : }
     594             : 
     595             : GEN
     596      783702 : divis(GEN y, long x)
     597             : {
     598      783702 :   long sy=signe(y),ly,s,i;
     599             :   ulong xi;
     600             :   GEN z;
     601             :   LOCAL_HIREMAINDER;
     602             : 
     603      783702 :   if (!x) pari_err_INV("divis",gen_0);
     604      783702 :   if (!sy) return gen_0;
     605      783702 :   if (x<0) { s = -sy; x = -x; } else s = sy;
     606             : 
     607      783702 :   ly = lgefint(y);
     608      783702 :   if ((ulong)x <= uel(y,2))
     609             :   {
     610      772380 :     hiremainder=0;
     611      772380 :     if (ly==3)
     612             :     {
     613      675966 :       z = cgeti(3); z[1] = evallgefint(3) | evalsigne(s);
     614      675966 :       z[2] = divll(y[2],x);
     615      675966 :       return z;
     616             :     }
     617             :   }
     618             :   else
     619             :   {
     620       11322 :     if (ly==3) return gen_0;
     621       11322 :     hiremainder=y[2]; ly--; y++;
     622             :   }
     623      107736 :   xi = get_Fl_red(x);
     624      107736 :   z = cgeti(ly); z[1] = evallgefint(ly) | evalsigne(s);
     625      107736 :   for (i=2; i<ly; i++) z[i]=divll_pre(y[i],x, xi);
     626      107736 :   return z;
     627             : }
     628             : 
     629             : GEN
     630    82542783 : divrr(GEN x, GEN y)
     631             : {
     632    82542783 :   long sx=signe(x), sy=signe(y), lx,ly,lr,e,i,j;
     633             :   ulong y0,y1;
     634             :   GEN r, r1;
     635             : 
     636    82542783 :   if (!x) pari_err_INV("divrr",y);
     637    82542783 :   e = expo(x) - expo(y);
     638    82542783 :   if (!sx) return real_0_bit(e);
     639    81360474 :   if (sy<0) sx = -sx;
     640             : 
     641    81360474 :   lx=lg(x); ly=lg(y);
     642    81360474 :   if (ly==3)
     643             :   {
     644    46232970 :     ulong k = x[2], l = (lx>3)? x[3]: 0;
     645             :     LOCAL_HIREMAINDER;
     646    46232970 :     if (k < uel(y,2)) e--;
     647             :     else
     648             :     {
     649    21166587 :       l >>= 1; if (k&1) l |= HIGHBIT;
     650    21166587 :       k >>= 1;
     651             :     }
     652    46232970 :     hiremainder = k; k = divll(l,y[2]);
     653    46232970 :     if (hiremainder & HIGHBIT)
     654             :     {
     655    12840189 :       k++;
     656    12840189 :       if (!k) { k = HIGHBIT; e++; }
     657             :     }
     658    46232970 :     r = cgetr(3);
     659    46232970 :     r[1] = evalsigne(sx) | evalexpo(e);
     660    46232970 :     r[2] = k; return r;
     661             :   }
     662             : 
     663    35127504 :   lr = minss(lx,ly); r = new_chunk(lr);
     664    35127504 :   r1 = r-1;
     665    35127504 :   r1[1] = 0; for (i=2; i<lr; i++) r1[i]=x[i];
     666    35127504 :   r1[lr] = (lx>ly)? x[lr]: 0;
     667    35127504 :   y0 = y[2]; y1 = y[3];
     668   278694738 :   for (i=0; i<lr-1; i++)
     669             :   { /* r1 = r + (i-1), OK up to r1[2] (accesses at most r[lr]) */
     670             :     ulong k, qp;
     671             :     LOCAL_HIREMAINDER;
     672             :     LOCAL_OVERFLOW;
     673             : 
     674   243567234 :     if (uel(r1,1) == y0)
     675             :     {
     676      818268 :       qp = ULONG_MAX; k = addll(y0,r1[2]);
     677             :     }
     678             :     else
     679             :     {
     680   242748966 :       if (uel(r1,1) > y0) /* can't happen if i=0 */
     681             :       {
     682           0 :         GEN y1 = y+1;
     683           0 :         j = lr-i; r1[j] = subll(r1[j],y1[j]);
     684           0 :         for (j--; j>0; j--) r1[j] = subllx(r1[j],y1[j]);
     685           0 :         j=i; do uel(r,--j)++; while (j && !uel(r,j));
     686             :       }
     687   242748966 :       hiremainder = r1[1]; overflow = 0;
     688   242748966 :       qp = divll(r1[2],y0); k = hiremainder;
     689             :     }
     690   243567234 :     j = lr-i+1;
     691   243567234 :     if (!overflow)
     692             :     {
     693             :       long k3, k4;
     694   242865723 :       k3 = mulll(qp,y1);
     695   242865723 :       if (j == 3) /* i = lr - 2 maximal, r1[3] undefined -> 0 */
     696    35098500 :         k4 = subll(hiremainder,k);
     697             :       else
     698             :       {
     699   207767223 :         k3 = subll(k3, r1[3]);
     700   207767223 :         k4 = subllx(hiremainder,k);
     701             :       }
     702   242865723 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     703             :     }
     704   243567234 :     if (j<ly) (void)mulll(qp,y[j]); else { hiremainder = 0 ; j = ly; }
     705  3444982320 :     for (j--; j>1; j--)
     706             :     {
     707  3201415086 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     708  3201415086 :       hiremainder += overflow;
     709             :     }
     710   243567234 :     if (uel(r1,1) != hiremainder)
     711             :     {
     712      235209 :       if (uel(r1,1) < hiremainder)
     713             :       {
     714      235209 :         qp--;
     715      235209 :         j = lr-i-(lr-i>=ly); r1[j] = addll(r1[j], y[j]);
     716      235209 :         for (j--; j>1; j--) r1[j] = addllx(r1[j], y[j]);
     717             :       }
     718             :       else
     719             :       {
     720           0 :         r1[1] -= hiremainder;
     721           0 :         while (r1[1])
     722             :         {
     723           0 :           qp++; if (!qp) { j=i; do uel(r,--j)++; while (j && !r[j]); }
     724           0 :           j = lr-i-(lr-i>=ly); r1[j] = subll(r1[j],y[j]);
     725           0 :           for (j--; j>1; j--) r1[j] = subllx(r1[j],y[j]);
     726           0 :           r1[1] -= overflow;
     727             :         }
     728             :       }
     729             :     }
     730   243567234 :     *++r1 = qp;
     731             :   }
     732             :   /* i = lr-1 */
     733             :   /* round correctly */
     734    35127504 :   if (uel(r1,1) > (y0>>1))
     735             :   {
     736    17256518 :     j=i; do uel(r,--j)++; while (j && !r[j]);
     737             :   }
     738    35127504 :   r1 = r-1; for (j=i; j>=2; j--) r[j]=r1[j];
     739    35127504 :   if (r[0] == 0) e--;
     740    14283714 :   else if (r[0] == 1) { shift_right(r,r, 2,lr, 1,1); }
     741             :   else { /* possible only when rounding up to 0x2 0x0 ... */
     742           0 :     r[2] = (long)HIGHBIT; e++;
     743             :   }
     744    35127504 :   r[0] = evaltyp(t_REAL)|evallg(lr);
     745    35127504 :   r[1] = evalsigne(sx) | evalexpo(e);
     746    35127504 :   return r;
     747             : }
     748             : 
     749             : GEN
     750    23050023 : divri(GEN x, GEN y)
     751             : {
     752    23050023 :   long lx, s = signe(y);
     753             :   pari_sp av;
     754             :   GEN z;
     755             : 
     756    23050023 :   if (!s) pari_err_INV("divri",y);
     757    23050023 :   if (!signe(x)) return real_0_bit(expo(x) - expi(y));
     758    22953564 :   if (!is_bigint(y)) {
     759    21022374 :     GEN z = divru(x, y[2]);
     760    21022374 :     if (s < 0) togglesign(z);
     761    21022374 :     return z;
     762             :   }
     763     1931190 :   lx = lg(x); z = cgetr(lx); av = avma;
     764     1931190 :   affrr(divrr(x, itor(y, lx+1)), z);
     765     1931190 :   set_avma(av); return z;
     766             : }
     767             : 
     768             : /* Integer division x / y: such that sign(r) = sign(x)
     769             :  *   if z = ONLY_REM return remainder, otherwise return quotient
     770             :  *   if z != NULL set *z to remainder
     771             :  *   *z is the last object on stack (and thus can be disposed of with cgiv
     772             :  *   instead of gerepile)
     773             :  * If *z is zero, we put gen_0 here and no copy.
     774             :  * space needed: lx + ly */
     775             : GEN
     776   540474131 : dvmdii(GEN x, GEN y, GEN *z)
     777             : {
     778   540474131 :   long sx = signe(x), sy = signe(y);
     779   540474131 :   long lx, ly = lgefint(y), lz, i, j, sh, lq, lr;
     780             :   pari_sp av;
     781             :   ulong y0,y0i,y1, *xd,*rd,*qd;
     782             :   GEN q, r, r1;
     783             : 
     784   540474131 :   if (!sx)
     785             :   {
     786    16891212 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     787    16891209 :     if (!z || z == ONLY_REM) return gen_0;
     788     6763203 :     *z=gen_0; return gen_0;
     789             :   }
     790   523582919 :   if (ly <= 3)
     791             :   {
     792             :     ulong rem;
     793   296334221 :     if (ly < 3) pari_err_INV("dvmdii",gen_0);
     794   296334221 :     if (z == ONLY_REM)
     795             :     {
     796   254796218 :       rem = umodiu(x,uel(y,2));
     797   254796218 :       if (!rem) return gen_0;
     798   231110786 :       return (sx < 0)? utoineg(uel(y,2) - rem): utoipos(rem);
     799             :     }
     800    41538003 :     q = absdiviu_rem(x, uel(y,2), &rem);
     801    41538003 :     if (sx != sy) togglesign(q);
     802    41538003 :     if (!z) return q;
     803    38676561 :     if (!rem) *z = gen_0;
     804    18048540 :     else *z = sx < 0? utoineg(rem): utoipos(rem);
     805    38676561 :     return q;
     806             :   }
     807   227248698 :   lx=lgefint(x);
     808   227248698 :   lz=lx-ly;
     809   227248698 :   if (lz <= 0)
     810             :   {
     811    95958549 :     if (lz == 0)
     812             :     {
     813    87658311 :       for (i=2; i<lx; i++)
     814    87366231 :         if (x[i] != y[i])
     815             :         {
     816    83315349 :           if (uel(x,i) > uel(y,i)) goto DIVIDE;
     817    27472380 :           goto TRIVIAL;
     818             :         }
     819      292080 :       if (z == ONLY_REM) return gen_0;
     820       10509 :       if (z) *z = gen_0;
     821       10509 :       if (sx < 0) sy = -sy;
     822       10509 :       return stoi(sy);
     823             :     }
     824             : TRIVIAL:
     825    39823500 :     if (z == ONLY_REM) return icopy(x);
     826      565830 :     if (z) *z = icopy(x);
     827      565830 :     return gen_0;
     828             :   }
     829             : DIVIDE: /* quotient is non-zero */
     830   187133118 :   av=avma; if (sx<0) sy = -sy;
     831   187133118 :   r1 = new_chunk(lx); sh = bfffo(y[2]);
     832   187133118 :   if (sh)
     833             :   { /* normalize so that highbit(y) = 1 (shift left x and y by sh bits)*/
     834   178913322 :     register const ulong m = BITS_IN_LONG - sh;
     835   178913322 :     r = new_chunk(ly);
     836   178913322 :     shift_left(r, y,2,ly-1, 0,sh); y = r;
     837   178913322 :     shift_left(r1,x,2,lx-1, 0,sh);
     838   178913322 :     r1[1] = uel(x,2) >> m;
     839             :   }
     840             :   else
     841             :   {
     842     8219796 :     r1[1] = 0; for (j=2; j<lx; j++) r1[j] = x[j];
     843             :   }
     844   187133118 :   x = r1;
     845   187133118 :   y0 = y[2]; y0i = get_Fl_red(y0);
     846   187133118 :   y1 = y[3];
     847  1056652842 :   for (i=0; i<=lz; i++)
     848             :   { /* r1 = x + i */
     849             :     ulong k, qp;
     850             :     LOCAL_HIREMAINDER;
     851             :     LOCAL_OVERFLOW;
     852             : 
     853   869519724 :     if (uel(r1,1) == y0)
     854             :     {
     855       71012 :       qp = ULONG_MAX; k = addll(y0,r1[2]);
     856             :     }
     857             :     else
     858             :     {
     859   869448712 :       hiremainder = r1[1]; overflow = 0;
     860   869448712 :       qp = divll_pre(r1[2],y0,y0i); k = hiremainder;
     861             :     }
     862   869519724 :     if (!overflow)
     863             :     {
     864   869458981 :       long k3 = subll(mulll(qp,y1), r1[3]);
     865   869458981 :       long k4 = subllx(hiremainder,k);
     866   869458981 :       while (!overflow && k4) { qp--; k3 = subll(k3,y1); k4 = subllx(k4,y0); }
     867             :     }
     868   869519724 :     hiremainder = 0; j = ly;
     869 22301186292 :     for (j--; j>1; j--)
     870             :     {
     871 21431666568 :       r1[j] = subll(r1[j], addmul(qp,y[j]));
     872 21431666568 :       hiremainder += overflow;
     873             :     }
     874   869519724 :     if (uel(r1,1) < hiremainder)
     875             :     {
     876      504607 :       qp--;
     877      504607 :       j = ly-1; r1[j] = addll(r1[j],y[j]);
     878      504607 :       for (j--; j>1; j--) r1[j] = addllx(r1[j],y[j]);
     879             :     }
     880   869519724 :     *++r1 = qp;
     881             :   }
     882             : 
     883   187133118 :   lq = lz+2;
     884   187133118 :   if (!z)
     885             :   {
     886      474297 :     qd = (ulong*)av;
     887      474297 :     xd = (ulong*)(x + lq);
     888      474297 :     if (x[1]) { lz++; lq++; }
     889      474297 :     while (lz--) *--qd = *--xd;
     890      474297 :     *--qd = evalsigne(sy) | evallgefint(lq);
     891      474297 :     *--qd = evaltyp(t_INT) | evallg(lq);
     892      474297 :     avma = (pari_sp)qd; return (GEN)qd;
     893             :   }
     894             : 
     895   186658821 :   j=lq; while (j<lx && !x[j]) j++;
     896   186658821 :   lz = lx-j;
     897   186658821 :   if (z == ONLY_REM)
     898             :   {
     899   139948641 :     if (lz==0) { set_avma(av); return gen_0; }
     900   137233614 :     rd = (ulong*)av; lr = lz+2;
     901   137233614 :     xd = (ulong*)(x + lx);
     902   137233614 :     if (!sh) while (lz--) *--rd = *--xd;
     903             :     else
     904             :     { /* shift remainder right by sh bits */
     905   129074868 :       const ulong shl = BITS_IN_LONG - sh;
     906             :       ulong l;
     907   129074868 :       xd--;
     908   749107797 :       while (--lz) /* fill r[3..] */
     909             :       {
     910   490958061 :         l = *xd >> sh;
     911   490958061 :         *--rd = l | (*--xd << shl);
     912             :       }
     913   129074868 :       l = *xd >> sh;
     914   129074868 :       if (l) *--rd = l; else lr--;
     915             :     }
     916   137233614 :     *--rd = evalsigne(sx) | evallgefint(lr);
     917   137233614 :     *--rd = evaltyp(t_INT) | evallg(lr);
     918   137233614 :     avma = (pari_sp)rd; return (GEN)rd;
     919             :   }
     920             : 
     921    46710180 :   lr = lz+2;
     922    46710180 :   rd = NULL; /* gcc -Wall */
     923    46710180 :   if (lz)
     924             :   { /* non zero remainder: initialize rd */
     925    45471756 :     xd = (ulong*)(x + lx);
     926    45471756 :     if (!sh)
     927             :     {
     928       25803 :       rd = (ulong*)avma; (void)new_chunk(lr);
     929       25803 :       while (lz--) *--rd = *--xd;
     930             :     }
     931             :     else
     932             :     { /* shift remainder right by sh bits */
     933    45445953 :       const ulong shl = BITS_IN_LONG - sh;
     934             :       ulong l;
     935    45445953 :       rd = (ulong*)x; /* overwrite shifted y */
     936    45445953 :       xd--;
     937   225306762 :       while (--lz)
     938             :       {
     939   134414856 :         l = *xd >> sh;
     940   134414856 :         *--rd = l | (*--xd << shl);
     941             :       }
     942    45445953 :       l = *xd >> sh;
     943    45445953 :       if (l) *--rd = l; else lr--;
     944             :     }
     945    45471756 :     *--rd = evalsigne(sx) | evallgefint(lr);
     946    45471756 :     *--rd = evaltyp(t_INT) | evallg(lr);
     947    45471756 :     rd += lr;
     948             :   }
     949    46710180 :   qd = (ulong*)av;
     950    46710180 :   xd = (ulong*)(x + lq);
     951    46710180 :   if (x[1]) lq++;
     952    46710180 :   j = lq-2; while (j--) *--qd = *--xd;
     953    46710180 :   *--qd = evalsigne(sy) | evallgefint(lq);
     954    46710180 :   *--qd = evaltyp(t_INT) | evallg(lq);
     955    46710180 :   q = (GEN)qd;
     956    46710180 :   if (lr==2) *z = gen_0;
     957             :   else
     958             :   { /* rd has been properly initialized: we had lz > 0 */
     959    45471756 :     while (lr--) *--qd = *--rd;
     960    45471756 :     *z = (GEN)qd;
     961             :   }
     962    46710180 :   avma = (pari_sp)qd; return q;
     963             : }
     964             : 
     965             : /* Montgomery reduction.
     966             :  * N has k words, assume T >= 0 has less than 2k.
     967             :  * Return res := T / B^k mod N, where B = 2^BIL
     968             :  * such that 0 <= res < T/B^k + N  and  res has less than k words */
     969             : GEN
     970    21496602 : red_montgomery(GEN T, GEN N, ulong inv)
     971             : {
     972             :   pari_sp av;
     973             :   GEN Te, Td, Ne, Nd, scratch;
     974    21496602 :   ulong i, j, m, t, d, k = NLIMBS(N);
     975             :   int carry;
     976             :   LOCAL_HIREMAINDER;
     977             :   LOCAL_OVERFLOW;
     978             : 
     979    21496602 :   if (k == 0) return gen_0;
     980    21496602 :   d = NLIMBS(T); /* <= 2*k */
     981    21496602 :   if (d == 0) return gen_0;
     982             : #ifdef DEBUG
     983             :   if (d > 2*k) pari_err_BUG("red_montgomery");
     984             : #endif
     985    21496593 :   if (k == 1)
     986             :   { /* as below, special cased for efficiency */
     987         492 :     ulong n = uel(N,2);
     988         492 :     if (d == 1) {
     989         492 :       hiremainder = uel(T,2);
     990         492 :       m = hiremainder * inv;
     991         492 :       (void)addmul(m, n); /* t + m*n = 0 */
     992         492 :       return utoi(hiremainder);
     993             :     } else { /* d = 2 */
     994           0 :       hiremainder = uel(T,3);
     995           0 :       m = hiremainder * inv;
     996           0 :       (void)addmul(m, n); /* t + m*n = 0 */
     997           0 :       t = addll(hiremainder, uel(T,2));
     998           0 :       if (overflow) t -= n; /* t > n doesn't fit in 1 word */
     999           0 :       return utoi(t);
    1000             :     }
    1001             :   }
    1002             :   /* assume k >= 2 */
    1003    21496101 :   av = avma; scratch = new_chunk(k<<1); /* >= k + 2: result fits */
    1004             : 
    1005             :   /* copy T to scratch space (pad with zeroes to 2k words) */
    1006    21496101 :   Td = (GEN)av;
    1007    21496101 :   Te = T + (d+2);
    1008    21496101 :   for (i=0; i < d     ; i++) *--Td = *--Te;
    1009    21496101 :   for (   ; i < (k<<1); i++) *--Td = 0;
    1010             : 
    1011    21496101 :   Te = (GEN)av; /* 1 beyond end of current T mantissa (in scratch) */
    1012    21496101 :   Ne = N + k+2; /* 1 beyond end of N mantissa */
    1013             : 
    1014    21496101 :   carry = 0;
    1015   323268078 :   for (i=0; i<k; i++) /* set T := T/B nod N, k times */
    1016             :   {
    1017   301771977 :     Td = Te; /* one beyond end of (new) T mantissa */
    1018   301771977 :     Nd = Ne;
    1019   301771977 :     hiremainder = *--Td;
    1020   301771977 :     m = hiremainder * inv; /* solve T + m N = O(B) */
    1021             : 
    1022             :     /* set T := (T + mN) / B */
    1023   301771977 :     Te = Td;
    1024   301771977 :     (void)addmul(m, *--Nd); /* = 0 */
    1025  5621517675 :     for (j=1; j<k; j++)
    1026             :     {
    1027  5319745698 :       t = addll(addmul(m, *--Nd), *--Td);
    1028  5319745698 :       *Td = t;
    1029  5319745698 :       hiremainder += overflow;
    1030             :     }
    1031   301771977 :     t = addll(hiremainder, *--Td); *Td = t + carry;
    1032   301771977 :     carry = (overflow || (carry && *Td == 0));
    1033             :   }
    1034    21496101 :   if (carry)
    1035             :   { /* Td > N overflows (k+1 words), set Td := Td - N */
    1036      346299 :     Td = Te;
    1037      346299 :     Nd = Ne;
    1038      346299 :     t = subll(*--Td, *--Nd); *Td = t;
    1039      346299 :     while (Td > scratch) { t = subllx(*--Td, *--Nd); *Td = t; }
    1040             :   }
    1041             : 
    1042             :   /* copy result */
    1043    21496101 :   Td = (GEN)av;
    1044    21496101 :   while (*scratch == 0 && Te > scratch) scratch++; /* strip leading 0s */
    1045    21496101 :   while (Te > scratch) *--Td = *--Te;
    1046    21496101 :   k = (GEN)av - Td; if (!k) { set_avma(av); return gen_0; }
    1047    21496101 :   k += 2;
    1048    21496101 :   *--Td = evalsigne(1) | evallgefint(k);
    1049    21496101 :   *--Td = evaltyp(t_INT) | evallg(k);
    1050             : #ifdef DEBUG
    1051             : {
    1052             :   long l = NLIMBS(N), s = BITS_IN_LONG*l;
    1053             :   GEN R = int2n(s);
    1054             :   GEN res = remii(mulii(T, Fp_inv(R, N)), N);
    1055             :   if (k > lgefint(N)
    1056             :     || !equalii(remii(Td,N),res)
    1057             :     || cmpii(Td, addii(shifti(T, -s), N)) >= 0) pari_err_BUG("red_montgomery");
    1058             : }
    1059             : #endif
    1060    21496101 :   avma = (pari_sp)Td; return Td;
    1061             : }
    1062             : 
    1063             : /* EXACT INTEGER DIVISION */
    1064             : 
    1065             : /* assume xy>0, the division is exact and y is odd. Destroy x */
    1066             : static GEN
    1067    16740918 : diviuexact_i(GEN x, ulong y)
    1068             : {
    1069             :   long i, lz, lx;
    1070             :   ulong q, yinv;
    1071             :   GEN z, z0, x0, x0min;
    1072             : 
    1073    16740918 :   if (y == 1) return icopy(x);
    1074    13435197 :   lx = lgefint(x);
    1075    13435197 :   if (lx == 3)
    1076             :   {
    1077      550791 :     q = uel(x,2) / y;
    1078      550791 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1079      550791 :     return utoipos(q);
    1080             :   }
    1081    12884406 :   yinv = invmod2BIL(y);
    1082    12884406 :   lz = (y <= uel(x,2)) ? lx : lx-1;
    1083    12884406 :   z = new_chunk(lz);
    1084    12884406 :   z0 = z + lz;
    1085    12884406 :   x0 = x + lx; x0min = x + lx-lz+2;
    1086             : 
    1087    53925078 :   while (x0 > x0min)
    1088             :   {
    1089    28156266 :     *--z0 = q = yinv*uel(--x0,0); /* i-th quotient */
    1090    28156266 :     if (!q) continue;
    1091             :     /* x := x - q * y */
    1092             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1093    27137175 :       register GEN x1 = x0 - 1;
    1094             :       LOCAL_HIREMAINDER;
    1095    27137175 :       (void)mulll(q,y);
    1096    27137175 :       if (hiremainder)
    1097             :       {
    1098    23141754 :         if (uel(x1,0) < hiremainder)
    1099             :         {
    1100      881487 :           uel(x1,0) -= hiremainder;
    1101      883251 :           do uel(--x1,0)--; while (uel(x1,0) == ULONG_MAX);
    1102             :         }
    1103             :         else
    1104    22260267 :           uel(x1,0) -= hiremainder;
    1105             :       }
    1106             :     }
    1107             :   }
    1108    12884406 :   i=2; while(!z[i]) i++;
    1109    12884406 :   z += i-2; lz -= i-2;
    1110    12884406 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1111    12884406 :   z[1] = evalsigne(1)|evallg(lz);
    1112    12884406 :   if (lz == 2) pari_err_OP("exact division", x, utoi(y));
    1113    12884406 :   avma = (pari_sp)z; return z;
    1114             : }
    1115             : 
    1116             : /* assume y != 0 and the division is exact */
    1117             : GEN
    1118    25948512 : diviuexact(GEN x, ulong y)
    1119             : {
    1120             :   pari_sp av;
    1121    25948512 :   long lx, vy, s = signe(x);
    1122             :   GEN z;
    1123             : 
    1124    25948512 :   if (!s) return gen_0;
    1125    25750119 :   if (y == 1) return icopy(x);
    1126    25743501 :   lx = lgefint(x);
    1127    25743501 :   if (lx == 3) {
    1128    24622842 :     ulong q = uel(x,2) / y;
    1129    24622842 :     if (!q) pari_err_OP("exact division", x, utoi(y));
    1130    24622842 :     return (s > 0)? utoipos(q): utoineg(q);
    1131             :   }
    1132     1120659 :   av = avma; (void)new_chunk(lx); vy = vals(y);
    1133     1120659 :   if (vy) {
    1134      591426 :     y >>= vy;
    1135      591426 :     if (y == 1) { set_avma(av); return shifti(x, -vy); }
    1136      533868 :     x = shifti(x, -vy);
    1137      533868 :     if (lx == 3) {
    1138           0 :       ulong q = uel(x,2) / y;
    1139           0 :       set_avma(av);
    1140           0 :       if (!q) pari_err_OP("exact division", x, utoi(y));
    1141           0 :       return (s > 0)? utoipos(q): utoineg(q);
    1142             :     }
    1143      529233 :   } else x = icopy(x);
    1144     1063101 :   set_avma(av);
    1145     1063101 :   z = diviuexact_i(x, y);
    1146     1063101 :   setsigne(z, s); return z;
    1147             : }
    1148             : 
    1149             : /* Find z such that x=y*z, knowing that y | x (unchecked)
    1150             :  * Method: y0 z0 = x0 mod B = 2^BITS_IN_LONG ==> z0 = 1/y0 mod B.
    1151             :  *    Set x := (x - z0 y) / B, updating only relevant words, and repeat */
    1152             : GEN
    1153   228708271 : diviiexact(GEN x, GEN y)
    1154             : {
    1155   228708271 :   long lx, ly, lz, vy, i, ii, sx = signe(x), sy = signe(y);
    1156             :   pari_sp av;
    1157             :   ulong y0inv,q;
    1158             :   GEN z;
    1159             : 
    1160   228708271 :   if (!sy) pari_err_INV("diviiexact",gen_0);
    1161   228708271 :   if (!sx) return gen_0;
    1162   187878496 :   lx = lgefint(x);
    1163   187878496 :   if (lx == 3) {
    1164   162542791 :     q = uel(x,2) / uel(y,2);
    1165   162542791 :     if (!q) pari_err_OP("exact division", x, y);
    1166   162542782 :     return (sx+sy) ? utoipos(q): utoineg(q);
    1167             :   }
    1168    25335705 :   vy = vali(y); av = avma;
    1169    25335705 :   (void)new_chunk(lx); /* enough room for z */
    1170    25335705 :   if (vy)
    1171             :   { /* make y odd */
    1172    11423169 :     y = shifti(y,-vy);
    1173    11423169 :     x = shifti(x,-vy); lx = lgefint(x);
    1174             :   }
    1175    13912536 :   else x = icopy(x); /* necessary because we destroy x */
    1176    25335705 :   set_avma(av); /* will erase our x,y when exiting */
    1177             :   /* now y is odd */
    1178    25335705 :   ly = lgefint(y);
    1179    25335705 :   if (ly == 3)
    1180             :   {
    1181    15677817 :     z = diviuexact_i(x,uel(y,2)); /* x != 0 */
    1182    15677817 :     setsigne(z, (sx+sy)? 1: -1); return z;
    1183             :   }
    1184     9657888 :   y0inv = invmod2BIL(y[ly-1]);
    1185     9657888 :   i=2; while (i<ly && y[i]==x[i]) i++;
    1186     9657888 :   lz = (i==ly || uel(y,i) < uel(x,i)) ? lx-ly+3 : lx-ly+2;
    1187     9657888 :   z = new_chunk(lz);
    1188             : 
    1189     9657888 :   y += ly - 1; /* now y[-i] = i-th word of y */
    1190    44823267 :   for (ii=lx-1,i=lz-1; i>=2; i--,ii--)
    1191             :   {
    1192             :     long limj;
    1193             :     LOCAL_HIREMAINDER;
    1194             :     LOCAL_OVERFLOW;
    1195             : 
    1196    35165379 :     z[i] = q = y0inv*uel(x,ii); /* i-th quotient */
    1197    35165379 :     if (!q) continue;
    1198             : 
    1199             :     /* x := x - q * y */
    1200    35122347 :     (void)mulll(q,y[0]); limj = maxss(lx - lz, ii+3-ly);
    1201             :     { /* update neither lowest word (could set it to 0) nor highest ones */
    1202    35122347 :       register GEN x0 = x + (ii - 1), y0 = y - 1, xlim = x + limj;
    1203   189975441 :       for (; x0 >= xlim; x0--, y0--)
    1204             :       {
    1205   154853094 :         *x0 = subll(*x0, addmul(q,*y0));
    1206   154853094 :         hiremainder += overflow;
    1207             :       }
    1208    35122347 :       if (hiremainder && limj != lx - lz)
    1209             :       {
    1210    17848308 :         if ((ulong)*x0 < hiremainder)
    1211             :         {
    1212      212718 :           *x0 -= hiremainder;
    1213      212718 :           do (*--x0)--; while ((ulong)*x0 == ULONG_MAX);
    1214             :         }
    1215             :         else
    1216    17635590 :           *x0 -= hiremainder;
    1217             :       }
    1218             :     }
    1219             :   }
    1220     9657888 :   i=2; while(!z[i]) i++;
    1221     9657888 :   z += i-2; lz -= (i-2);
    1222     9657888 :   z[0] = evaltyp(t_INT)|evallg(lz);
    1223     9657888 :   z[1] = evalsigne((sx+sy)? 1: -1) | evallg(lz);
    1224     9657888 :   if (lz == 2) pari_err_OP("exact division", x, y);
    1225     9657888 :   avma = (pari_sp)z; return z;
    1226             : }
    1227             : 
    1228             : /* assume yz != and yz | x */
    1229             : GEN
    1230      471324 : diviuuexact(GEN x, ulong y, ulong z)
    1231             : {
    1232             :   long tmp[4];
    1233             :   ulong t;
    1234             :   LOCAL_HIREMAINDER;
    1235      471324 :   t = mulll(y, z);
    1236      471324 :   if (!hiremainder) return diviuexact(x, t);
    1237           0 :   tmp[0] = evaltyp(t_INT)|_evallg(4);
    1238           0 :   tmp[1] = evalsigne(1)|evallgefint(4);
    1239           0 :   tmp[2] = hiremainder;
    1240           0 :   tmp[3] = t;
    1241           0 :   return diviiexact(x, tmp);
    1242             : }
    1243             : 
    1244             : /********************************************************************/
    1245             : /**                                                                **/
    1246             : /**               INTEGER MULTIPLICATION (BASECASE)                **/
    1247             : /**                                                                **/
    1248             : /********************************************************************/
    1249             : /* nx >= ny = num. of digits of x, y (not GEN, see mulii) */
    1250             : INLINE GEN
    1251  1840732751 : muliispec_basecase(GEN x, GEN y, long nx, long ny)
    1252             : {
    1253             :   GEN z2e,z2d,yd,xd,ye,zd;
    1254             :   long p1,lz;
    1255             :   LOCAL_HIREMAINDER;
    1256             : 
    1257  1840732751 :   if (ny == 1) return muluispec((ulong)*y, x, nx);
    1258   554065410 :   if (ny == 0) return gen_0;
    1259   553473756 :   zd = (GEN)avma; lz = nx+ny+2;
    1260   553473756 :   (void)new_chunk(lz);
    1261   553473756 :   xd = x + nx;
    1262   553473756 :   yd = y + ny;
    1263   553473756 :   ye = yd; p1 = *--xd;
    1264             : 
    1265   553473756 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1266   553473756 :   while (yd > y) *--zd = addmul(p1, *--yd);
    1267   553473756 :   *--zd = hiremainder;
    1268             : 
    1269  4314334260 :   while (xd > x)
    1270             :   {
    1271             :     LOCAL_OVERFLOW;
    1272  3207386748 :     yd = ye; p1 = *--xd;
    1273             : 
    1274  3207386748 :     z2d = --z2e;
    1275  3207386748 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1276 39815898921 :     while (yd > y)
    1277             :     {
    1278 33401125425 :       hiremainder += overflow;
    1279 33401125425 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1280             :     }
    1281  3207386748 :     *--zd = hiremainder + overflow;
    1282             :   }
    1283   553473756 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1284   553473756 :   *--zd = evalsigne(1) | evallgefint(lz);
    1285   553473756 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1286   553473756 :   avma=(pari_sp)zd; return zd;
    1287             : }
    1288             : 
    1289             : INLINE GEN
    1290   603860721 : sqrispec_basecase(GEN x, long nx)
    1291             : {
    1292             :   GEN z2e,z2d,yd,xd,zd,x0,z0;
    1293             :   long p1,lz;
    1294             :   LOCAL_HIREMAINDER;
    1295             :   LOCAL_OVERFLOW;
    1296             : 
    1297   603860721 :   if (nx == 1) return sqru((ulong)*x);
    1298   519461232 :   if (nx == 0) return gen_0;
    1299   114594027 :   zd = (GEN)avma; lz = (nx+1) << 1;
    1300   114594027 :   z0 = new_chunk(lz);
    1301   114594027 :   if (nx == 1)
    1302             :   {
    1303           0 :     *--zd = mulll(*x, *x);
    1304           0 :     *--zd = hiremainder; goto END;
    1305             :   }
    1306   114594027 :   xd = x + nx;
    1307             : 
    1308             :   /* compute double products --> zd */
    1309   114594027 :   p1 = *--xd; yd = xd; --zd;
    1310   114594027 :   *--zd = mulll(p1, *--yd); z2e = zd;
    1311   114594027 :   while (yd > x) *--zd = addmul(p1, *--yd);
    1312   114594027 :   *--zd = hiremainder;
    1313             : 
    1314   114594027 :   x0 = x+1;
    1315  1159707597 :   while (xd > x0)
    1316             :   {
    1317             :     LOCAL_OVERFLOW;
    1318   930519543 :     p1 = *--xd; yd = xd;
    1319             : 
    1320   930519543 :     z2e -= 2; z2d = z2e;
    1321   930519543 :     *z2d = addll(mulll(p1, *--yd), *z2d); z2d--;
    1322  9122714472 :     while (yd > x)
    1323             :     {
    1324  7261675386 :       hiremainder += overflow;
    1325  7261675386 :       *z2d = addll(addmul(p1, *--yd), *z2d); z2d--;
    1326             :     }
    1327   930519543 :     *--zd = hiremainder + overflow;
    1328             :   }
    1329             :   /* multiply zd by 2 (put result in zd - 1) */
    1330   114594027 :   zd[-1] = ((*zd & HIGHBIT) != 0);
    1331   114594027 :   shift_left(zd, zd, 0, (nx<<1)-3, 0, 1);
    1332             : 
    1333             :   /* add the squares */
    1334   114594027 :   xd = x + nx; zd = z0 + lz;
    1335   114594027 :   p1 = *--xd;
    1336   114594027 :   zd--; *zd = mulll(p1,p1);
    1337   114594027 :   zd--; *zd = addll(hiremainder, *zd);
    1338  1274301624 :   while (xd > x)
    1339             :   {
    1340  1045113570 :     p1 = *--xd;
    1341  1045113570 :     zd--; *zd = addll(mulll(p1,p1)+ overflow, *zd);
    1342  1045113570 :     zd--; *zd = addll(hiremainder + overflow, *zd);
    1343             :   }
    1344             : 
    1345             : END:
    1346   114594027 :   if (*zd == 0) { zd++; lz--; } /* normalize */
    1347   114594027 :   *--zd = evalsigne(1) | evallgefint(lz);
    1348   114594027 :   *--zd = evaltyp(t_INT) | evallg(lz);
    1349   114594027 :   avma=(pari_sp)zd; return zd;
    1350             : }
    1351             : 
    1352             : /********************************************************************/
    1353             : /**                                                                **/
    1354             : /**               INTEGER MULTIPLICATION (FFT)                     **/
    1355             : /**                                                                **/
    1356             : /********************************************************************/
    1357             : 
    1358             : /*
    1359             :  Compute parameters for FFT:
    1360             :    len: result length
    1361             :    k: FFT depth.
    1362             :    n: number of blocks (2^k)
    1363             :    bs: block size
    1364             :    mod: Modulus is M=2^(BIL*mod)+1
    1365             :    ord: order of 2 in Z/MZ.
    1366             :  We must have:
    1367             :    bs*n >= l
    1368             :    2^(BIL*mod) > nb*2^(2*BIL*bs)
    1369             :    2^k | 2*BIL*mod
    1370             : */
    1371             : static void
    1372       11802 : mulliifft_params(long len, long *k, long *mod, long *bs, long *n, ulong *ord)
    1373             : {
    1374             :   long r;
    1375       11802 :   *k = expu((3*len)>>2)-3;
    1376             :   do {
    1377       11802 :     (*k)--;
    1378       11802 :     r  = *k-(TWOPOTBITS_IN_LONG+2);
    1379       11802 :     *n = 1L<<*k;
    1380       11802 :     *bs = (len+*n-1)>>*k;
    1381       11802 :     *mod= 2**bs+1;
    1382       11802 :     if (r>0)
    1383         597 :       *mod=((*mod+(1L<<r)-1)>>r)<<r;
    1384       11802 :   } while(*mod>=3**bs);
    1385       11802 :   *ord= 4**mod*BITS_IN_LONG;
    1386       11802 : }
    1387             : 
    1388             : /* Zf_: arithmetic in ring Z/MZ where M= 2^(BITS_IN_LONG*mod)+1
    1389             :  * for some mod.
    1390             :  * Do not garbage collect.
    1391             :  */
    1392             : 
    1393             : static GEN
    1394    24249216 : Zf_add(GEN a, GEN b, GEN M)
    1395             : {
    1396    24249216 :   GEN y, z = addii(a,b);
    1397    24249216 :   long mod = lgefint(M)-3;
    1398    24249216 :   long l = NLIMBS(z);
    1399    24249216 :   if (l<=mod) return z;
    1400     9019887 :   y = subiu(z, 1);
    1401     9019887 :   if (NLIMBS(y)<=mod) return z;
    1402     9019887 :   return int_normalize(y,1);
    1403             : }
    1404             : 
    1405             : static GEN
    1406    24601071 : Zf_sub(GEN a, GEN b, GEN M)
    1407             : {
    1408    24601071 :   GEN z = subii(a,b);
    1409    24601071 :   return signe(z)>=0? z: addii(M,z);
    1410             : }
    1411             : 
    1412             : /* destroy z */
    1413             : static GEN
    1414    50345466 : Zf_red_destroy(GEN z, GEN M)
    1415             : {
    1416    50345466 :   long mod = lgefint(M)-3;
    1417    50345466 :   long l = NLIMBS(z);
    1418             :   GEN y;
    1419    50345466 :   if (l<=mod) return z;
    1420    22200960 :   y = shifti(z, -mod*BITS_IN_LONG);
    1421    22200960 :   z = int_normalize(z, NLIMBS(y));
    1422    22200960 :   y = Zf_red_destroy(y, M);
    1423    22200960 :   z = subii(z, y);
    1424    22200960 :   if (signe(z)<0) z = addii(z, M);
    1425    22200960 :   return z;
    1426             : }
    1427             : 
    1428             : INLINE GEN
    1429    26012922 : Zf_shift(GEN a, ulong s, GEN M) { return Zf_red_destroy(shifti(a, s), M); }
    1430             : 
    1431             : /*
    1432             :  Multiply by sqrt(2)^s
    1433             :  We use the formula sqrt(2)=z_8*(1-z_4)) && z_8=2^(ord/16) [2^(ord/4)+1]
    1434             : */
    1435             : 
    1436             : static GEN
    1437    24249216 : Zf_mulsqrt2(GEN a, ulong s, ulong ord, GEN M)
    1438             : {
    1439    24249216 :   ulong hord = ord>>1;
    1440    24249216 :   if (!signe(a)) return gen_0;
    1441    23177628 :   if (odd(s)) /* Multiply by 2^(s/2) */
    1442             :   {
    1443      351855 :     GEN az8  = Zf_shift(a,   ord>>4, M);
    1444      351855 :     GEN az83 = Zf_shift(az8, ord>>3, M);
    1445      351855 :     a = Zf_sub(az8, az83, M);
    1446      351855 :     s--;
    1447             :   }
    1448    23177628 :   if (s < hord)
    1449    17091933 :     return Zf_shift(a, s>>1, M);
    1450             :   else
    1451     6085695 :     return subii(M,Zf_shift(a, (s-hord)>>1, M));
    1452             : }
    1453             : 
    1454             : INLINE GEN
    1455      269184 : Zf_sqr(GEN a, GEN M) { return Zf_red_destroy(sqri(a), M); }
    1456             : 
    1457             : INLINE GEN
    1458     1862400 : Zf_mul(GEN a, GEN b, GEN M) { return Zf_red_destroy(mulii(a,b), M); }
    1459             : 
    1460             : /* In place, bit reversing FFT */
    1461             : static void
    1462     3971562 : muliifft_dit(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1463             : {
    1464     3971562 :   pari_sp av = avma;
    1465             :   long i;
    1466     3971562 :   ulong j, no = (o<<1)%ord;
    1467     3971562 :   long hstep=step>>1;
    1468    19760298 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1469             :   {
    1470    15788736 :     GEN a = Zf_add(gel(FFT,i), gel(FFT,i+hstep), M);
    1471    15788736 :     GEN b = Zf_mulsqrt2(Zf_sub(gel(FFT,i), gel(FFT,i+hstep), M), j, ord, M);
    1472    15788736 :     affii(a,gel(FFT,i));
    1473    15788736 :     affii(b,gel(FFT,i+hstep));
    1474    15788736 :     set_avma(av);
    1475             :   }
    1476     3971562 :   if (hstep>1)
    1477             :   {
    1478     1974570 :     muliifft_dit(no, ord, M, FFT, d, hstep);
    1479     1974570 :     muliifft_dit(no, ord, M, FFT, d+hstep, hstep);
    1480             :   }
    1481     3971562 : }
    1482             : 
    1483             : /* In place, bit reversed FFT, inverse of muliifft_dit */
    1484             : static void
    1485     2119782 : muliifft_dis(ulong o, ulong ord, GEN M, GEN FFT, long d, long step)
    1486             : {
    1487     2119782 :   pari_sp av = avma;
    1488             :   long i;
    1489     2119782 :   ulong j, no = (o<<1)%ord;
    1490     2119782 :   long hstep=step>>1;
    1491     2119782 :   if (hstep>1)
    1492             :   {
    1493     1053990 :     muliifft_dis(no, ord, M, FFT, d, hstep);
    1494     1053990 :     muliifft_dis(no, ord, M, FFT, d+hstep, hstep);
    1495             :   }
    1496    10580262 :   for (i = d+1, j = 0; i <= d+hstep; ++i, j =(j+o)%ord)
    1497             :   {
    1498     8460480 :     GEN z = Zf_mulsqrt2(gel(FFT,i+hstep), j, ord, M);
    1499     8460480 :     GEN a = Zf_add(gel(FFT,i), z, M);
    1500     8460480 :     GEN b = Zf_sub(gel(FFT,i), z, M);
    1501     8460480 :     affii(a,gel(FFT,i));
    1502     8460480 :     affii(b,gel(FFT,i+hstep));
    1503     8460480 :     set_avma(av);
    1504             :   }
    1505     2119782 : }
    1506             : 
    1507             : static GEN
    1508       22422 : muliifft_spliti(GEN a, long na, long bs, long n, long mod)
    1509             : {
    1510       22422 :   GEN ap = a+na-1;
    1511       22422 :   GEN c  = cgetg(n+1, t_VEC);
    1512             :   long i,j;
    1513     4016406 :   for(i=1;i<=n;i++)
    1514             :   {
    1515     3993984 :     GEN z = cgeti(mod+3);
    1516     3993984 :     if (na)
    1517             :     {
    1518     1972821 :       long m = minss(bs, na), v=0;
    1519     1972821 :       GEN zp, aa=ap-m+1;
    1520     1972821 :       while (!*aa && v<m) {aa++; v++;}
    1521     1972821 :       zp = z+m-v+1;
    1522    44381940 :       for (j=v; j < m; j++)
    1523    42409119 :         *zp-- = *ap--;
    1524     1972821 :       ap -= v; na -= m;
    1525     1972821 :       z[1] = evalsigne(m!=v) | evallgefint(m-v+2);
    1526             :     } else
    1527     2021163 :       z[1] = evalsigne(0) | evallgefint(2);
    1528     3993984 :     gel(c, i) = z;
    1529             :   }
    1530       22422 :   return c;
    1531             : }
    1532             : 
    1533             : static GEN
    1534       11802 : muliifft_unspliti(GEN V, long bs, long len)
    1535             : {
    1536       11802 :   long s, i, j, l = lg(V);
    1537       11802 :   GEN a = cgeti(len);
    1538       11802 :   a[1] = evalsigne(1)|evallgefint(len);
    1539    58620948 :   for(i=2;i<len;i++)
    1540    58609146 :     a[i] = 0;
    1541     2143386 :   for(i=1, s=0; i<l; i++, s+=bs)
    1542             :   {
    1543     2131584 :     GEN  u = gel(V,i);
    1544     2131584 :     if (signe(u))
    1545             :     {
    1546     1965885 :       GEN ap = int_W(a,s);
    1547     1965885 :       GEN up = int_LSW(u);
    1548     1965885 :       long lu = NLIMBS(u);
    1549             :       LOCAL_OVERFLOW;
    1550     1965885 :       *ap = addll(*ap, *up--); ap--;
    1551   106275495 :       for (j=1; j<lu; j++)
    1552   104309610 :        { *ap = addllx(*ap, *up--); ap--; }
    1553     3931896 :       while (overflow)
    1554         126 :        { *ap = addll(*ap, 1); ap--; }
    1555             :     }
    1556             :   }
    1557       11802 :   return int_normalize(a,0);
    1558             : }
    1559             : 
    1560             : static GEN
    1561        1182 : sqrispec_fft(GEN a, long na)
    1562             : {
    1563        1182 :   pari_sp av, ltop = avma;
    1564        1182 :   long len = 2*na;
    1565             :   long k, mod, bs, n;
    1566             :   GEN  FFT, M;
    1567             :   long i;
    1568             :   ulong o, ord;
    1569             : 
    1570        1182 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1571        1182 :   o = ord>>k;
    1572        1182 :   M = int2n(mod*BITS_IN_LONG);
    1573        1182 :   M[2+mod] = 1;
    1574        1182 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1575        1182 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1576        1182 :   av = avma;
    1577      270366 :   for(i=1; i<=n; i++)
    1578             :   {
    1579      269184 :     affii(Zf_sqr(gel(FFT,i), M), gel(FFT,i));
    1580      269184 :     set_avma(av);
    1581             :   }
    1582        1182 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1583      270366 :   for(i=1; i<=n; i++)
    1584             :   {
    1585      269184 :     affii(Zf_shift(gel(FFT,i), (ord>>1)-k, M), gel(FFT,i));
    1586      269184 :     set_avma(av);
    1587             :   }
    1588        1182 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1589             : }
    1590             : 
    1591             : static GEN
    1592       10620 : muliispec_fft(GEN a, GEN b, long na, long nb)
    1593             : {
    1594       10620 :   pari_sp av, av2, ltop = avma;
    1595       10620 :   long len = na+nb;
    1596             :   long k, mod, bs, n;
    1597             :   GEN FFT, FFTb, M;
    1598             :   long i;
    1599             :   ulong o, ord;
    1600             : 
    1601       10620 :   mulliifft_params(len,&k,&mod,&bs,&n,&ord);
    1602       10620 :   o = ord>>k;
    1603       10620 :   M = int2n(mod*BITS_IN_LONG);
    1604       10620 :   M[2+mod] = 1;
    1605       10620 :   FFT = muliifft_spliti(a, na, bs, n, mod);
    1606       10620 :   av=avma;
    1607       10620 :   muliifft_dit(o, ord, M, FFT, 0, n);
    1608       10620 :   FFTb = muliifft_spliti(b, nb, bs, n, mod);
    1609       10620 :   av2 = avma;
    1610       10620 :   muliifft_dit(o, ord, M, FFTb, 0, n);
    1611     1873020 :   for(i=1; i<=n; i++)
    1612             :   {
    1613     1862400 :     affii(Zf_mul(gel(FFT,i), gel(FFTb,i), M), gel(FFT,i));
    1614     1862400 :     set_avma(av2);
    1615             :   }
    1616       10620 :   set_avma(av);
    1617       10620 :   muliifft_dis(ord-o, ord, M, FFT, 0, n);
    1618     1873020 :   for(i=1; i<=n; i++)
    1619             :   {
    1620     1862400 :     affii(Zf_shift(gel(FFT,i),(ord>>1)-k,M), gel(FFT,i));
    1621     1862400 :     set_avma(av);
    1622             :   }
    1623       10620 :   return gerepileuptoint(ltop, muliifft_unspliti(FFT,bs,2+len));
    1624             : }
    1625             : 
    1626             : /********************************************************************/
    1627             : /**                                                                **/
    1628             : /**               INTEGER MULTIPLICATION (KARATSUBA)               **/
    1629             : /**                                                                **/
    1630             : /********************************************************************/
    1631             : 
    1632             : /* return (x shifted left d words) + y. Assume d > 0, x > 0 and y >= 0 */
    1633             : static GEN
    1634   138322461 : addshiftw(GEN x, GEN y, long d)
    1635             : {
    1636   138322461 :   GEN z,z0,y0,yd, zd = (GEN)avma;
    1637   138322461 :   long a,lz,ly = lgefint(y);
    1638             : 
    1639   138322461 :   z0 = new_chunk(d);
    1640   138322461 :   a = ly-2; yd = y+ly;
    1641   138322461 :   if (a >= d)
    1642             :   {
    1643   136098573 :     y0 = yd-d; while (yd > y0) *--zd = *--yd; /* copy last d words of y */
    1644   136098573 :     a -= d;
    1645   136098573 :     if (a)
    1646    92556519 :       z = addiispec(LIMBS(x), LIMBS(y), NLIMBS(x), a);
    1647             :     else
    1648    43542054 :       z = icopy(x);
    1649             :   }
    1650             :   else
    1651             :   {
    1652     2223888 :     y0 = yd-a; while (yd > y0) *--zd = *--yd; /* copy last a words of y */
    1653     2223888 :     while (zd > z0) *--zd = 0;    /* complete with 0s */
    1654     2223888 :     z = icopy(x);
    1655             :   }
    1656   138322461 :   lz = lgefint(z)+d;
    1657   138322461 :   z[1] = evalsigne(1) | evallgefint(lz);
    1658   138322461 :   z[0] = evaltyp(t_INT) | evallg(lz); return z;
    1659             : }
    1660             : 
    1661             : /* Fast product (Karatsuba) of integers. a and b are "special" GENs
    1662             :  * c,c0,c1,c2 are genuine GENs.
    1663             :  */
    1664             : GEN
    1665  1883788556 : muliispec(GEN a, GEN b, long na, long nb)
    1666             : {
    1667             :   GEN a0,c,c0;
    1668             :   long n0, n0a, i;
    1669             :   pari_sp av;
    1670             : 
    1671  1883788556 :   if (na < nb) swapspec(a,b, na,nb);
    1672  1883788556 :   if (nb < MULII_KARATSUBA_LIMIT) return muliispec_basecase(a,b,na,nb);
    1673    43055805 :   if (nb >= MULII_FFT_LIMIT)      return muliispec_fft(a,b,na,nb);
    1674    43045185 :   i=(na>>1); n0=na-i; na=i;
    1675    43045185 :   av=avma; a0=a+na; n0a=n0;
    1676    43045185 :   while (n0a && !*a0) { a0++; n0a--; }
    1677             : 
    1678    43045185 :   if (n0a && nb > n0)
    1679    41513424 :   { /* nb <= na <= n0 */
    1680             :     GEN b0,c1,c2;
    1681             :     long n0b;
    1682             : 
    1683    41513424 :     nb -= n0;
    1684    41513424 :     c = muliispec(a,b,na,nb);
    1685    41513424 :     b0 = b+nb; n0b = n0;
    1686    41513424 :     while (n0b && !*b0) { b0++; n0b--; }
    1687    41513424 :     if (n0b)
    1688             :     {
    1689    40848786 :       c0 = muliispec(a0,b0, n0a,n0b);
    1690             : 
    1691    40848786 :       c2 = addiispec(a0,a, n0a,na);
    1692    40848786 :       c1 = addiispec(b0,b, n0b,nb);
    1693    40848786 :       c1 = muliispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1694    40848786 :       c2 = addiispec(LIMBS(c0),LIMBS(c),  NLIMBS(c0),NLIMBS(c));
    1695             : 
    1696    40848786 :       c1 = subiispec(LIMBS(c1),LIMBS(c2), NLIMBS(c1),NLIMBS(c2));
    1697             :     }
    1698             :     else
    1699             :     {
    1700      664638 :       c0 = gen_0;
    1701      664638 :       c1 = muliispec(a0,b, n0a,nb);
    1702             :     }
    1703    41513424 :     c = addshiftw(c,c1, n0);
    1704             :   }
    1705             :   else
    1706             :   {
    1707     1531761 :     c = muliispec(a,b,na,nb);
    1708     1531761 :     c0 = muliispec(a0,b,n0a,nb);
    1709             :   }
    1710    43045185 :   return gerepileuptoint(av, addshiftw(c,c0, n0));
    1711             : }
    1712             : GEN
    1713      487980 : muluui(ulong x, ulong y, GEN z)
    1714             : {
    1715      487980 :   long t, s = signe(z);
    1716             :   GEN r;
    1717             :   LOCAL_HIREMAINDER;
    1718             : 
    1719      487980 :   if (!x || !y || !signe(z)) return gen_0;
    1720      487683 :   t = mulll(x,y);
    1721      487683 :   if (!hiremainder)
    1722      487683 :     r = muluispec(t, z+2, lgefint(z)-2);
    1723             :   else
    1724             :   {
    1725             :     long tmp[2];
    1726           0 :     tmp[0] = hiremainder;
    1727           0 :     tmp[1] = t;
    1728           0 :     r = muliispec(z+2,tmp,lgefint(z)-2,2);
    1729             :   }
    1730      487683 :   setsigne(r,s); return r;
    1731             : }
    1732             : 
    1733             : #define sqrispec_mirror sqrispec
    1734             : #define muliispec_mirror muliispec
    1735             : 
    1736             : /* x % (2^n), assuming n >= 0 */
    1737             : GEN
    1738     8296638 : remi2n(GEN x, long n)
    1739             : {
    1740     8296638 :   long hi,l,k,lx,ly, sx = signe(x);
    1741             :   GEN z, xd, zd;
    1742             : 
    1743     8296638 :   if (!sx || !n) return gen_0;
    1744             : 
    1745     8278488 :   k = dvmdsBIL(n, &l);
    1746     8278488 :   lx = lgefint(x);
    1747     8278488 :   if (lx < k+3) return icopy(x);
    1748             : 
    1749     8240094 :   xd = x + (lx-k-1);
    1750             :   /* x = |_|...|#|1|...|k| : copy the last l bits of # and the last k words
    1751             :    *            ^--- initial xd  */
    1752     8240094 :   hi = ((ulong)*xd) & ((1UL<<l)-1); /* last l bits of # = top bits of result */
    1753     8240094 :   if (!hi)
    1754             :   { /* strip leading zeroes from result */
    1755      277389 :     xd++; while (k && !*xd) { k--; xd++; }
    1756      277389 :     if (!k) return gen_0;
    1757       70707 :     ly = k+2; xd--;
    1758             :   }
    1759             :   else
    1760     7962705 :     ly = k+3;
    1761             : 
    1762     8033412 :   zd = z = cgeti(ly);
    1763     8033412 :   *++zd = evalsigne(sx) | evallgefint(ly);
    1764     8033412 :   if (hi) *++zd = hi;
    1765     8033412 :   for ( ;k; k--) *++zd = *++xd;
    1766     8033412 :   return z;
    1767             : }
    1768             : 
    1769             : GEN
    1770   608728407 : sqrispec(GEN a, long na)
    1771             : {
    1772             :   GEN a0,c;
    1773             :   long n0, n0a, i;
    1774             :   pari_sp av;
    1775             : 
    1776   608728407 :   if (na < SQRI_KARATSUBA_LIMIT) return sqrispec_basecase(a,na);
    1777     4867686 :   if (na >= SQRI_FFT_LIMIT) return sqrispec_fft(a,na);
    1778     4866504 :   i=(na>>1); n0=na-i; na=i;
    1779     4866504 :   av=avma; a0=a+na; n0a=n0;
    1780     4866504 :   while (n0a && !*a0) { a0++; n0a--; }
    1781     4866504 :   c = sqrispec(a,na);
    1782     4866504 :   if (n0a)
    1783             :   {
    1784     4854678 :     GEN t, c1, c0 = sqrispec(a0,n0a);
    1785             : #if 0
    1786             :     c1 = shifti(muliispec(a0,a, n0a,na),1);
    1787             : #else /* faster */
    1788     4854678 :     t = addiispec(a0,a,n0a,na);
    1789     4854678 :     t = sqrispec(LIMBS(t),NLIMBS(t));
    1790     4854678 :     c1= addiispec(LIMBS(c0),LIMBS(c), NLIMBS(c0), NLIMBS(c));
    1791     4854678 :     c1= subiispec(LIMBS(t),LIMBS(c1), NLIMBS(t), NLIMBS(c1));
    1792             : #endif
    1793     4854678 :     c = addshiftw(c,c1, n0);
    1794     4854678 :     c = addshiftw(c,c0, n0);
    1795             :   }
    1796             :   else
    1797       11826 :     c = addshiftw(c,gen_0,n0<<1);
    1798     4866504 :   return gerepileuptoint(av, c);
    1799             : }
    1800             : 
    1801             : /********************************************************************/
    1802             : /**                                                                **/
    1803             : /**                    KARATSUBA SQUARE ROOT                       **/
    1804             : /**      adapted from Paul Zimmermann's implementation of          **/
    1805             : /**      his algorithm in GMP (mpn_sqrtrem)                        **/
    1806             : /**                                                                **/
    1807             : /********************************************************************/
    1808             : 
    1809             : /* Square roots table */
    1810             : static const unsigned char approx_tab[192] = {
    1811             :   128,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,
    1812             :   143,144,144,145,146,147,148,149,150,150,151,152,153,154,155,155,
    1813             :   156,157,158,159,160,160,161,162,163,163,164,165,166,167,167,168,
    1814             :   169,170,170,171,172,173,173,174,175,176,176,177,178,178,179,180,
    1815             :   181,181,182,183,183,184,185,185,186,187,187,188,189,189,190,191,
    1816             :   192,192,193,193,194,195,195,196,197,197,198,199,199,200,201,201,
    1817             :   202,203,203,204,204,205,206,206,207,208,208,209,209,210,211,211,
    1818             :   212,212,213,214,214,215,215,216,217,217,218,218,219,219,220,221,
    1819             :   221,222,222,223,224,224,225,225,226,226,227,227,228,229,229,230,
    1820             :   230,231,231,232,232,233,234,234,235,235,236,236,237,237,238,238,
    1821             :   239,240,240,241,241,242,242,243,243,244,244,245,245,246,246,247,
    1822             :   247,248,248,249,249,250,250,251,251,252,252,253,253,254,254,255
    1823             : };
    1824             : 
    1825             : /* N[0], assume N[0] >= 2^(BIL-2).
    1826             :  * Return r,s such that s^2 + r = N, 0 <= r <= 2s */
    1827             : static void
    1828    22328911 : p_sqrtu1(ulong *N, ulong *ps, ulong *pr)
    1829             : {
    1830    22328911 :   ulong prec, r, s, q, u, n0 = N[0];
    1831             : 
    1832    22328911 :   q = n0 >> (BITS_IN_LONG - 8);
    1833             :   /* 2^6 = 64 <= q < 256 = 2^8 */
    1834    22328911 :   s = approx_tab[q - 64];                                /* 128 <= s < 255 */
    1835    22328911 :   r = (n0 >> (BITS_IN_LONG - 16)) - s * s;                /* r <= 2*s */
    1836    22328911 :   if (r > (s << 1)) { r -= (s << 1) | 1; s++; }
    1837             : 
    1838             :   /* 8-bit approximation from the high 8-bits of N[0] */
    1839    22328911 :   prec = 8;
    1840    22328911 :   n0 <<= 2 * prec;
    1841    89315644 :   while (2 * prec < BITS_IN_LONG)
    1842             :   { /* invariant: s has prec bits, and r <= 2*s */
    1843    44657822 :     r = (r << prec) + (n0 >> (BITS_IN_LONG - prec));
    1844    44657822 :     n0 <<= prec;
    1845    44657822 :     u = 2 * s;
    1846    44657822 :     q = r / u; u = r - q * u;
    1847    44657822 :     s = (s << prec) + q;
    1848    44657822 :     u = (u << prec) + (n0 >> (BITS_IN_LONG - prec));
    1849    44657822 :     q = q * q;
    1850    44657822 :     r = u - q;
    1851    44657822 :     if (u < q) { s--; r += (s << 1) | 1; }
    1852    44657822 :     n0 <<= prec;
    1853    44657822 :     prec = 2 * prec;
    1854             :   }
    1855    22328911 :   *ps = s;
    1856    22328911 :   *pr = r;
    1857    22328911 : }
    1858             : 
    1859             : /* N[0..1], assume N[0] >= 2^(BIL-2).
    1860             :  * Return 1 if remainder overflows, 0 otherwise */
    1861             : static int
    1862    20054268 : p_sqrtu2(ulong *N, ulong *ps, ulong *pr)
    1863             : {
    1864    20054268 :   ulong cc, qhl, r, s, q, u, n1 = N[1];
    1865             :   LOCAL_OVERFLOW;
    1866             : 
    1867    20054268 :   p_sqrtu1(N, &s, &r); /* r <= 2s */
    1868    20054268 :   qhl = 0; while (r >= s) { qhl++; r -= s; }
    1869             :   /* now r < s < 2^(BIL/2) */
    1870    20054268 :   r = (r << BITS_IN_HALFULONG) | (n1 >> BITS_IN_HALFULONG);
    1871    20054268 :   u = s << 1;
    1872    20054268 :   q = r / u; u = r - q * u;
    1873    20054268 :   q += (qhl & 1) << (BITS_IN_HALFULONG - 1);
    1874    20054268 :   qhl >>= 1;
    1875             :   /* (initial r)<<(BIL/2) + n1>>(BIL/2) = (qhl<<(BIL/2) + q) * 2s + u */
    1876    20054268 :   s = ((s + qhl) << BITS_IN_HALFULONG) + q;
    1877    20054268 :   cc = u >> BITS_IN_HALFULONG;
    1878    20054268 :   r = (u << BITS_IN_HALFULONG) | (n1 & LOWMASK);
    1879    20054268 :   r = subll(r, q * q);
    1880    20054268 :   cc -= overflow + qhl;
    1881             :   /* now subtract 2*q*2^(BIL/2) + 2^BIL if qhl is set */
    1882    20054268 :   if ((long)cc < 0)
    1883             :   {
    1884     5292627 :     if (s) {
    1885     5277816 :       r = addll(r, s);
    1886     5277816 :       cc += overflow;
    1887     5277816 :       s--;
    1888             :     } else {
    1889       14811 :       cc++;
    1890       14811 :       s = ~0UL;
    1891             :     }
    1892     5292627 :     r = addll(r, s);
    1893     5292627 :     cc += overflow;
    1894             :   }
    1895    20054268 :   *ps = s;
    1896    20054268 :   *pr = r; return cc;
    1897             : }
    1898             : 
    1899             : static void
    1900    19646265 : xmpn_zero(GEN x, long n)
    1901             : {
    1902    19646265 :   while (--n >= 0) x[n]=0;
    1903    19646265 : }
    1904             : static void
    1905   204872373 : xmpn_copy(GEN z, GEN x, long n)
    1906             : {
    1907   204872373 :   long k = n;
    1908   204872373 :   while (--k >= 0) z[k] = x[k];
    1909   204872373 : }
    1910             : /* a[0..la-1] * 2^(lb BIL) | b[0..lb-1] */
    1911             : static GEN
    1912    88085340 : catii(GEN a, long la, GEN b, long lb)
    1913             : {
    1914    88085340 :   long l = la + lb + 2;
    1915    88085340 :   GEN z = cgetipos(l);
    1916    88085340 :   xmpn_copy(LIMBS(z), a, la);
    1917    88085340 :   xmpn_copy(LIMBS(z) + la, b, lb);
    1918    88085340 :   return int_normalize(z, 0);
    1919             : }
    1920             : 
    1921             : /* sqrt n[0..1], assume n normalized */
    1922             : static GEN
    1923    19729803 : sqrtispec2(GEN n, GEN *pr)
    1924             : {
    1925             :   ulong s, r;
    1926    19729803 :   int hi = p_sqrtu2((ulong*)n, &s, &r);
    1927    19729803 :   GEN S = utoi(s);
    1928    19729803 :   *pr = hi? uutoi(1,r): utoi(r);
    1929    19729803 :   return S;
    1930             : }
    1931             : 
    1932             : /* sqrt n[0], _dont_ assume n normalized */
    1933             : static GEN
    1934     2274643 : sqrtispec1_sh(GEN n, GEN *pr)
    1935             : {
    1936             :   GEN S;
    1937     2274643 :   ulong r, s, u0 = uel(n,0);
    1938     2274643 :   int sh = bfffo(u0) & ~1UL;
    1939     2274643 :   if (sh) u0 <<= sh;
    1940     2274643 :   p_sqrtu1(&u0, &s, &r);
    1941             :   /* s^2 + r = u0, s < 2^(BIL/2). Rescale back:
    1942             :    * 2^(2k) n = S^2 + R
    1943             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1944     2274643 :   if (sh) {
    1945     2042983 :     int k = sh >> 1;
    1946     2042983 :     ulong s0 = s & ((1L<<k) - 1);
    1947     2042983 :     r += s * (s0<<1);
    1948     2042983 :     s >>= k;
    1949     2042983 :     r >>= sh;
    1950             :   }
    1951     2274643 :   S = utoi(s);
    1952     2274643 :   if (pr) *pr = utoi(r);
    1953     2274643 :   return S;
    1954             : }
    1955             : 
    1956             : /* sqrt n[0..1], _dont_ assume n normalized */
    1957             : static GEN
    1958      324465 : sqrtispec2_sh(GEN n, GEN *pr)
    1959             : {
    1960             :   GEN S;
    1961      324465 :   ulong U[2], r, s, u0 = uel(n,0), u1 = uel(n,1);
    1962      324465 :   int hi, sh = bfffo(u0) & ~1UL;
    1963      324465 :   if (sh) {
    1964      324357 :     u0 = (u0 << sh) | (u1 >> (BITS_IN_LONG-sh));
    1965      324357 :     u1 <<= sh;
    1966             :   }
    1967      324465 :   U[0] = u0;
    1968      324465 :   U[1] = u1; hi = p_sqrtu2(U, &s, &r);
    1969             :   /* s^2 + R = u0|u1. Rescale back:
    1970             :    * 2^(2k) n = S^2 + R
    1971             :    * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    1972      324465 :   if (sh) {
    1973      324357 :     int k = sh >> 1;
    1974      324357 :     ulong s0 = s & ((1L<<k) - 1);
    1975             :     LOCAL_HIREMAINDER;
    1976             :     LOCAL_OVERFLOW;
    1977      324357 :     r = addll(r, mulll(s, (s0<<1)));
    1978      324357 :     if (overflow) hiremainder++;
    1979      324357 :     hiremainder += hi; /* + 0 or 1 */
    1980      324357 :     s >>= k;
    1981      324357 :     r = (r>>sh) | (hiremainder << (BITS_IN_LONG-sh));
    1982      324357 :     hi = (hiremainder & (1L<<sh));
    1983             :   }
    1984      324465 :   S = utoi(s);
    1985      324465 :   if (pr) *pr = hi? uutoi(1,r): utoi(r);
    1986      324465 :   return S;
    1987             : }
    1988             : 
    1989             : /* Let N = N[0..2n-1]. Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S
    1990             :  * Assume N normalized */
    1991             : static GEN
    1992    63772473 : sqrtispec(GEN N, long n, GEN *r)
    1993             : {
    1994             :   GEN S, R, q, z, u;
    1995             :   long l, h;
    1996             : 
    1997    63772473 :   if (n == 1) return sqrtispec2(N, r);
    1998    44042670 :   l = n >> 1;
    1999    44042670 :   h = n - l; /* N = a3(h) | a2(h) | a1(l) | a0(l words) */
    2000    44042670 :   S = sqrtispec(N, h, &R); /* S^2 + R = a3|a2 */
    2001             : 
    2002    44042670 :   z = catii(LIMBS(R), NLIMBS(R), N + 2*h, l); /* = R | a1(l) */
    2003    44042670 :   q = dvmdii(z, shifti(S,1), &u);
    2004    44042670 :   z = catii(LIMBS(u), NLIMBS(u), N + n + h, l); /* = u | a0(l) */
    2005             : 
    2006    44042670 :   S = addshiftw(S, q, l);
    2007    44042670 :   R = subii(z, sqri(q));
    2008    44042670 :   if (signe(R) < 0)
    2009             :   {
    2010     7020270 :     GEN S2 = shifti(S,1);
    2011     7020270 :     R = addis(subiispec(LIMBS(S2),LIMBS(R), NLIMBS(S2),NLIMBS(R)), -1);
    2012     7020270 :     S = addis(S, -1);
    2013             :   }
    2014    44042670 :   *r = R; return S;
    2015             : }
    2016             : 
    2017             : /* Return S (and set R) s.t S^2 + R = N, 0 <= R <= 2S.
    2018             :  * As for dvmdii, R is last on stack and guaranteed to be gen_0 in case the
    2019             :  * remainder is 0. R = NULL is allowed. */
    2020             : GEN
    2021     2682652 : sqrtremi(GEN N, GEN *r)
    2022             : {
    2023             :   pari_sp av;
    2024     2682652 :   GEN S, R, n = N+2;
    2025     2682652 :   long k, l2, ln = NLIMBS(N);
    2026             :   int sh;
    2027             : 
    2028     2682652 :   if (ln <= 2)
    2029             :   {
    2030     2599114 :     if (ln == 2) return sqrtispec2_sh(n, r);
    2031     2274649 :     if (ln == 1) return sqrtispec1_sh(n, r);
    2032           6 :     if (r) *r = gen_0;
    2033           6 :     return gen_0;
    2034             :   }
    2035       83538 :   av = avma;
    2036       83538 :   sh = bfffo(n[0]) >> 1;
    2037       83538 :   l2 = (ln + 1) >> 1;
    2038      166881 :   if (sh || (ln & 1)) { /* normalize n, so that n[0] >= 2^BIL / 4 */
    2039       83343 :     GEN s0, t = new_chunk(ln + 1);
    2040       83343 :     t[ln] = 0;
    2041       83343 :     if (sh)
    2042       82941 :       shift_left(t, n, 0,ln-1, 0, sh << 1);
    2043             :     else
    2044         402 :       xmpn_copy(t, n, ln);
    2045       83343 :     S = sqrtispec(t, l2, &R); /* t normalized, 2 * l2 words */
    2046             :     /* Rescale back:
    2047             :      * 2^(2k) n = S^2 + R, k = sh + (ln & 1)*BIL/2
    2048             :      * so 2^(2k) n = (S - s0)^2 + (2*S*s0 - s0^2 + R), s0 = S mod 2^k. */
    2049       83343 :     k = sh + (ln & 1) * (BITS_IN_LONG/2);
    2050       83343 :     s0 = remi2n(S, k);
    2051       83343 :     R = addii(shifti(R,-1), mulii(s0, S));
    2052       83343 :     R = shifti(R, 1 - (k<<1));
    2053       83343 :     S = shifti(S, -k);
    2054             :   }
    2055             :   else
    2056         195 :     S = sqrtispec(n, l2, &R);
    2057             : 
    2058       83538 :   if (!r) { avma = (pari_sp)S; return gerepileuptoint(av, S); }
    2059        6633 :   gerepileall(av, 2, &S, &R); *r = R; return S;
    2060             : }
    2061             : 
    2062             : /* compute sqrt(|a|), assuming a != 0 */
    2063             : 
    2064             : #if 1
    2065             : GEN
    2066    19646265 : sqrtr_abs(GEN x)
    2067             : {
    2068    19646265 :   long l = realprec(x) - 2, e = expo(x), er = e>>1;
    2069    19646265 :   GEN b, c, res = cgetr(2 + l);
    2070    19646265 :   res[1] = evalsigne(1) | evalexpo(er);
    2071    19646265 :   if (e&1) {
    2072     9055026 :     b = new_chunk(l << 1);
    2073     9055026 :     xmpn_copy(b, x+2, l);
    2074     9055026 :     xmpn_zero(b + l,l);
    2075     9055026 :     b = sqrtispec(b, l, &c);
    2076     9055026 :     xmpn_copy(res+2, b+2, l);
    2077     9055026 :     if (cmpii(c, b) > 0) roundr_up_ip(res, l+2);
    2078             :   } else {
    2079             :     ulong u;
    2080    10591239 :     b = new_chunk(2 + (l << 1));
    2081    10591239 :     shift_left(b+1, x+2, 0,l-1, 0, BITS_IN_LONG-1);
    2082    10591239 :     b[0] = uel(x,2)>>1;
    2083    10591239 :     xmpn_zero(b + l+1,l+1);
    2084    10591239 :     b = sqrtispec(b, l+1, &c);
    2085    10591239 :     xmpn_copy(res+2, b+2, l);
    2086    10591239 :     u = uel(b,l+2);
    2087    10591239 :     if ( u&HIGHBIT || (u == ~HIGHBIT && cmpii(c,b) > 0))
    2088     5211320 :       roundr_up_ip(res, l+2);
    2089             :   }
    2090    19646265 :   avma = (pari_sp)res; return res;
    2091             : }
    2092             : 
    2093             : #else /* use t_REAL: currently much slower (quadratic division) */
    2094             : 
    2095             : #ifdef LONG_IS_64BIT
    2096             : /* 64 bits of b = sqrt(a[0] * 2^64 + a[1])  [ up to 1ulp ] */
    2097             : static ulong
    2098             : sqrtu2(ulong *a)
    2099             : {
    2100             :   ulong c, b = dblmantissa( sqrt((double)a[0]) );
    2101             :   LOCAL_HIREMAINDER;
    2102             :   LOCAL_OVERFLOW;
    2103             : 
    2104             :   /* > 32 correct bits, 1 Newton iteration to reach 64 */
    2105             :   if (b <= a[0]) return HIGHBIT | (a[0] >> 1);
    2106             :   hiremainder = a[0]; c = divll(a[1], b);
    2107             :   return (addll(c, b) >> 1) | HIGHBIT;
    2108             : }
    2109             : /* 64 bits of sqrt(a[0] * 2^63) */
    2110             : static ulong
    2111             : sqrtu2_1(ulong *a)
    2112             : {
    2113             :   ulong t[2];
    2114             :   t[0] = (a[0] >> 1);
    2115             :   t[1] = (a[0] << (BITS_IN_LONG-1)) | (a[1] >> 1);
    2116             :   return sqrtu2(t);
    2117             : }
    2118             : #else
    2119             : /* 32 bits of sqrt(a[0] * 2^32) */
    2120             : static ulong
    2121             : sqrtu2(ulong *a)   { return dblmantissa( sqrt((double)a[0]) ); }
    2122             : /* 32 bits of sqrt(a[0] * 2^31) */
    2123             : static ulong
    2124             : sqrtu2_1(ulong *a) { return dblmantissa( sqrt(2. * a[0]) ); }
    2125             : #endif
    2126             : 
    2127             : GEN
    2128             : sqrtr_abs(GEN x)
    2129             : {
    2130             :   long l1, i, l = lg(x), ex = expo(x);
    2131             :   GEN a, t, y = cgetr(l);
    2132             :   pari_sp av, av0 = avma;
    2133             : 
    2134             :   a = rtor(x, l+1);
    2135             :   t = cgetr(l+1);
    2136             :   if (ex & 1) { /* odd exponent */
    2137             :     a[1] = evalsigne(1) | _evalexpo(1);
    2138             :     t[2] = (long)sqrtu2((ulong*)a + 2);
    2139             :   } else { /* even exponent */
    2140             :     a[1] = evalsigne(1) | _evalexpo(0);
    2141             :     t[2] = (long)sqrtu2_1((ulong*)a + 2);
    2142             :   }
    2143             :   t[1] = evalsigne(1) | _evalexpo(0);
    2144             :   for (i = 3; i <= l; i++) t[i] = 0;
    2145             : 
    2146             :   /* |x| = 2^(ex/2) a, t ~ sqrt(a) */
    2147             :   l--; l1 = 1; av = avma;
    2148             :   while (l1 < l) { /* let t := (t + a/t)/2 */
    2149             :     l1 <<= 1; if (l1 > l) l1 = l;
    2150             :     setlg(a, l1 + 2);
    2151             :     setlg(t, l1 + 2);
    2152             :     affrr(addrr(t, divrr(a,t)), t); shiftr_inplace(t, -1);
    2153             :     set_avma(av);
    2154             :   }
    2155             :   affrr(t,y); shiftr_inplace(y, (ex>>1));
    2156             :   set_avma(av0); return y;
    2157             : }
    2158             : 
    2159             : #endif
    2160             : 
    2161             : /*******************************************************************
    2162             :  *                                                                 *
    2163             :  *                           Base Conversion                       *
    2164             :  *                                                                 *
    2165             :  *******************************************************************/
    2166             : 
    2167             : static void
    2168      926310 : convi_dac(GEN x, ulong l, ulong *res)
    2169             : {
    2170      926310 :   pari_sp ltop=avma;
    2171             :   ulong m;
    2172             :   GEN x1,x2;
    2173      926310 :   if (l==1) { *res=itou(x); return; }
    2174      425538 :   m=l>>1;
    2175      425538 :   x1=dvmdii(x,powuu(1000000000UL,m),&x2);
    2176      425538 :   convi_dac(x1,l-m,res+m);
    2177      425538 :   convi_dac(x2,m,res);
    2178      425538 :   set_avma(ltop);
    2179             : }
    2180             : 
    2181             : /* convert integer --> base 10^9 [not memory clean] */
    2182             : ulong *
    2183      266941 : convi(GEN x, long *l)
    2184             : {
    2185      266941 :   long lz, lx = lgefint(x);
    2186             :   ulong *z;
    2187      266941 :   if (lx == 3 && uel(x,2) < 1000000000UL) {
    2188      191707 :     z = (ulong*)new_chunk(1);
    2189      191707 :     *z = x[2];
    2190      191707 :     *l = 1; return z+1;
    2191             :   }
    2192       75234 :   lz = 1 + (long)bit_accuracy_mul(lx, LOG10_2/9);
    2193       75234 :   z = (ulong*)new_chunk(lz);
    2194       75234 :   convi_dac(x,(ulong)lz,z);
    2195       75234 :   while (z[lz-1]==0) lz--;
    2196       75234 :   *l=lz; return z+lz;
    2197             : }
    2198             : 

Generated by: LCOV version 1.13