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_indep.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.1 lcov report (development 30256-57c6c2b57f) Lines: 535 567 94.4 %
Date: 2025-05-08 09:19:02 Functions: 51 52 98.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #line 2 "../src/kernel/none/mp_indep.c"
       2             : /* Copyright (C) 2000  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; either version 2 of the License, or (at your option) any later
       9             : version. It is distributed in the hope that it will be useful, but WITHOUT
      10             : ANY WARRANTY WHATSOEVER.
      11             : 
      12             : Check the License for details. You should have received a copy of it, along
      13             : with the package; see the file 'COPYING'. If not, write to the Free Software
      14             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      15             : 
      16             : /* Find c such that 1=c*b mod 2^BITS_IN_LONG, assuming b odd (unchecked) */
      17             : ulong
      18    60531460 : invmod2BIL(ulong b)
      19             : {
      20             :   static int tab[] = { 0, 0, 0, 8, 0, 8, 0, 0 };
      21    60531460 :   ulong x = b + tab[b & 7]; /* b^(-1) mod 2^4 */
      22             : 
      23             :   /* Newton applied to 1/x - b = 0 */
      24             : #ifdef LONG_IS_64BIT
      25    60531460 :   x = x*(2-b*x); /* one more pass necessary */
      26             : #endif
      27    60531460 :   x = x*(2-b*x);
      28    60531460 :   x = x*(2-b*x); return x*(2-b*x);
      29             : }
      30             : 
      31             : void
      32  1369274785 : affrr(GEN x, GEN y)
      33             : {
      34  1369274785 :   long i, lx, ly = lg(y);
      35  1369274785 :   if (!signe(x))
      36             :   {
      37      127272 :     y[1] = evalexpo(minss(expo(x), -bit_accuracy(ly)));
      38      127271 :     return;
      39             :   }
      40  1369147513 :   y[1] = x[1]; lx = lg(x);
      41  1369147513 :   if (lx <= ly)
      42             :   {
      43  7450224809 :     for (i=2; i<lx; i++) y[i]=x[i];
      44  1313087905 :     for (   ; i<ly; i++) y[i]=0;
      45  1154984344 :     return;
      46             :   }
      47  1156554840 :   for (i=2; i<ly; i++) y[i]=x[i];
      48             :   /* lx > ly: round properly */
      49   214163169 :   if (x[ly] & HIGHBIT) roundr_up_ip(y, ly);
      50             : }
      51             : 
      52             : GEN
      53    62646069 : trunc2nr(GEN x, long n)
      54             : {
      55             :   long ex;
      56    62646069 :   if (!signe(x)) return gen_0;
      57    62204483 :   ex = expo(x) + n; if (ex < 0) return gen_0;
      58    59636766 :   return mantissa2nr(x, ex - bit_prec(x) + 1);
      59             : }
      60             : 
      61             : /* x a t_REAL, x = i/2^e, i a t_INT */
      62             : GEN
      63    52651739 : mantissa_real(GEN x, long *e)
      64             : {
      65    52651739 :   *e = bit_prec(x)-1-expo(x);
      66    52651545 :   return mantissa2nr(x, 0);
      67             : }
      68             : 
      69             : GEN
      70   932193683 : mului(ulong x, GEN y)
      71             : {
      72   932193683 :   long s = signe(y);
      73             :   GEN z;
      74             : 
      75   932193683 :   if (!s || !x) return gen_0;
      76   780116031 :   z = muluispec(x, y+2, lgefint(y)-2);
      77   779766074 :   setsigne(z,s); return z;
      78             : }
      79             : 
      80             : GEN
      81   603833168 : mulsi(long x, GEN y)
      82             : {
      83   603833168 :   long s = signe(y);
      84             :   GEN z;
      85             : 
      86   603833168 :   if (!s || !x) return gen_0;
      87   360766537 :   if (x<0) { s = -s; x = -x; }
      88   360766537 :   z = muluispec((ulong)x, y+2, lgefint(y)-2);
      89   360762509 :   setsigne(z,s); return z;
      90             : }
      91             : 
      92             : GEN
      93   201150132 : mulss(long x, long y)
      94             : {
      95             :   ulong p1;
      96             :   LOCAL_HIREMAINDER;
      97             : 
      98   201150132 :   if (!x || !y) return gen_0;
      99   200813931 :   if (x<0) {
     100      414272 :     x = -x;
     101      414272 :     if (y<0) { y = -y; p1 = mulll(x,y); return uutoi(hiremainder, p1); }
     102      299735 :     p1 = mulll(x,y); return uutoineg(hiremainder, p1);
     103             :   } else {
     104   200399659 :     if (y<0) { y = -y; p1 = mulll(x,y); return uutoineg(hiremainder, p1); }
     105   200356085 :     p1 = mulll(x,y); return uutoi(hiremainder, p1);
     106             :   }
     107             : }
     108             : GEN
     109     1500436 : sqrs(long x)
     110             : {
     111             :   ulong p1;
     112             :   LOCAL_HIREMAINDER;
     113             : 
     114     1500436 :   if (!x) return gen_0;
     115     1498665 :   if (x<0) x = -x;
     116     1498665 :   p1 = mulll(x,x); return uutoi(hiremainder, p1);
     117             : }
     118             : GEN
     119  4549131800 : muluu(ulong x, ulong y)
     120             : {
     121             :   ulong p1;
     122             :   LOCAL_HIREMAINDER;
     123             : 
     124  4549131800 :   if (!x || !y) return gen_0;
     125  4549835732 :   p1 = mulll(x,y); return uutoi(hiremainder, p1);
     126             : }
     127             : GEN
     128   614276021 : sqru(ulong x)
     129             : {
     130             :   ulong p1;
     131             :   LOCAL_HIREMAINDER;
     132             : 
     133   614276021 :   if (!x) return gen_0;
     134   613943334 :   p1 = mulll(x,x); return uutoi(hiremainder, p1);
     135             : }
     136             : 
     137             : /* assume x > 1, y != 0. Return u * y with sign s */
     138             : static GEN
     139   703168803 : mulur_2(ulong x, GEN y, long s)
     140             : {
     141   703168803 :   long m, sh, i, lx = lg(y), e = expo(y);
     142   703168803 :   GEN z = cgetg(lx, t_REAL);
     143             :   ulong garde;
     144             :   LOCAL_HIREMAINDER;
     145             : 
     146   702452210 :   y--; garde = mulll(x,y[lx]);
     147  3694628941 :   for (i=lx-1; i>=3; i--) z[i]=addmul(x,y[i]);
     148   702452210 :   z[2]=hiremainder; /* != 0 since y normalized and |x| > 1 */
     149   702452210 :   sh = bfffo(hiremainder); m = BITS_IN_LONG-sh;
     150   702452210 :   if (sh) shift_left(z,z, 2,lx-1, garde,sh);
     151   703727747 :   z[1] = evalsigne(s) | evalexpo(m+e);
     152   705202723 :   if ((garde << sh) & HIGHBIT) roundr_up_ip(z, lx);
     153   704954716 :   return z;
     154             : }
     155             : 
     156             : INLINE GEN
     157      474030 : mul0r(GEN x)
     158             : {
     159      474030 :   long l = realprec(x), e = expo(x);
     160      474030 :   e = (l > 0)? e - l: (e < 0? 2*e: 0);
     161      474030 :   return real_0_bit(e);
     162             : }
     163             : /* lg(x) > 2 */
     164             : INLINE GEN
     165      186336 : div0r(GEN x) {
     166      186336 :   long l = realprec(x), e = expo(x);
     167      186336 :   return real_0_bit(-l - e);
     168             : }
     169             : 
     170             : GEN
     171   117359773 : mulsr(long x, GEN y)
     172             : {
     173             :   long s;
     174             : 
     175   117359773 :   if (!x) return mul0r(y);
     176   117359731 :   s = signe(y);
     177   117359731 :   if (!s)
     178             :   {
     179      234311 :     if (x < 0) x = -x;
     180      234311 :     return real_0_bit( expo(y) + expu(x) );
     181             :   }
     182   117125420 :   if (x==1)  return rcopy(y);
     183   108407925 :   if (x==-1) return negr(y);
     184   105283654 :   if (x < 0)
     185    34929843 :     return mulur_2((ulong)-x, y, -s);
     186             :   else
     187    70353811 :     return mulur_2((ulong)x, y, s);
     188             : }
     189             : 
     190             : GEN
     191   919959489 : mulur(ulong x, GEN y)
     192             : {
     193             :   long s;
     194             : 
     195   919959489 :   if (!x) return mul0r(y);
     196   919959482 :   s = signe(y);
     197   919959482 :   if (!s) return real_0_bit( expo(y) + expu(x) );
     198   912647455 :   if (x==1) return rcopy(y);
     199   597636857 :   return mulur_2(x, y, s);
     200             : }
     201             : 
     202             : INLINE void
     203  3565216564 : mulrrz_end(GEN z, GEN hi, long lz, long sz, long ez, ulong garde)
     204             : {
     205             :   long i;
     206  3565216564 :   if (hi[2] < 0)
     207             :   {
     208  1645945202 :     if (z != hi)
     209  1724590081 :       for (i=2; i<lz ; i++) z[i] = hi[i];
     210  1645945202 :     ez++;
     211             :   }
     212             :   else
     213             :   {
     214  1919271362 :     shift_left(z,hi,2,lz-1, garde, 1);
     215  1922484682 :     garde <<= 1;
     216             :   }
     217  3568429884 :   if (garde & HIGHBIT)
     218             :   { /* round to nearest */
     219  1748013261 :     i = lz; do ((ulong*)z)[--i]++; while (i>1 && z[i]==0);
     220  1730467135 :     if (i == 1) { z[2] = (long)HIGHBIT; ez++; }
     221             :   }
     222  3568429884 :   z[1] = evalsigne(sz)|evalexpo(ez);
     223  3564128009 : }
     224             : /* mulrrz_end for lz = 3, minor simplifications. z[2]=hiremainder from mulll */
     225             : INLINE void
     226   772164097 : mulrrz_3end(GEN z, long sz, long ez, ulong garde)
     227             : {
     228   772164097 :   if (z[2] < 0)
     229             :   { /* z2 < (2^BIL-1)^2 / 2^BIL, hence z2+1 != 0 */
     230   362471392 :     if (garde & HIGHBIT) z[2]++; /* round properly */
     231   362471392 :     ez++;
     232             :   }
     233             :   else
     234             :   {
     235   409692705 :     uel(z,2) = (uel(z,2)<<1) | (garde>>(BITS_IN_LONG-1));
     236   409692705 :     if (garde & (1UL<<(BITS_IN_LONG-2)))
     237             :     {
     238   168165615 :       uel(z,2)++; /* round properly, z2+1 can overflow */
     239   168165615 :       if (!uel(z,2)) { uel(z,2) = HIGHBIT; ez++; }
     240             :     }
     241             :   }
     242   772164097 :   z[1] = evalsigne(sz)|evalexpo(ez);
     243   772099354 : }
     244             : 
     245             : /* set z <-- x^2 != 0, floating point multiplication.
     246             :  * lz = lg(z) = lg(x) */
     247             : INLINE void
     248   559389647 : sqrz_i(GEN z, GEN x, long lz)
     249             : {
     250   559389647 :   long ez = 2*expo(x);
     251             :   long i, j, lzz, p1;
     252             :   ulong garde;
     253             :   GEN x1;
     254             :   LOCAL_HIREMAINDER;
     255             :   LOCAL_OVERFLOW;
     256             : 
     257   559389647 :   if (lz > prec2lg(SQRR_SQRI_LIMIT))
     258             :   {
     259    41271184 :     pari_sp av = avma;
     260    41271184 :     GEN hi = sqrispec_mirror(x+2, lz-2);
     261    41564203 :     mulrrz_end(z, hi, lz, 1, ez, hi[lz]);
     262    41549173 :     set_avma(av); return;
     263             :   }
     264   518016375 :   if (lz == 3)
     265             :   {
     266   121218407 :     garde = mulll(x[2],x[2]);
     267   121218407 :     z[2] = hiremainder;
     268   121218407 :     mulrrz_3end(z, 1, ez, garde);
     269   121196485 :     return;
     270             :   }
     271             : 
     272   396797968 :   lzz = lz-1; p1 = x[lzz];
     273   396797968 :   if (p1)
     274             :   {
     275   360872019 :     (void)mulll(p1,x[3]);
     276   360872019 :     garde = addmul(p1,x[2]);
     277   360872019 :     z[lzz] = hiremainder;
     278             :   }
     279             :   else
     280             :   {
     281    35925949 :     garde = 0;
     282    35925949 :     z[lzz] = 0;
     283             :   }
     284  1837603983 :   for (j=lz-2, x1=x-j; j>=3; j--)
     285             :   {
     286  1440806015 :     p1 = x[j]; x1++;
     287  1440806015 :     if (p1)
     288             :     {
     289  1413777950 :       (void)mulll(p1,x1[lz+1]);
     290  1413777950 :       garde = addll(addmul(p1,x1[lz]), garde);
     291  7226394544 :       for (i=lzz; i>j; i--)
     292             :       {
     293  5812616594 :         hiremainder += overflow;
     294  5812616594 :         z[i] = addll(addmul(p1,x1[i]), z[i]);
     295             :       }
     296  1413777950 :       z[j] = hiremainder+overflow;
     297             :     }
     298    27028065 :     else z[j]=0;
     299             :   }
     300   396797968 :   p1 = x[2]; x1++;
     301   396797968 :   garde = addll(mulll(p1,x1[lz]), garde);
     302  2239342487 :   for (i=lzz; i>2; i--)
     303             :   {
     304  1842544519 :     hiremainder += overflow;
     305  1842544519 :     z[i] = addll(addmul(p1,x1[i]), z[i]);
     306             :   }
     307   396797968 :   z[2] = hiremainder+overflow;
     308   396797968 :   mulrrz_end(z, z, lz, 1, ez, garde);
     309             : }
     310             : 
     311             : /* lz "large" = lg(y) = lg(z), lg(x) > lz if flag = 1 and >= if flag = 0 */
     312             : INLINE void
     313    55648752 : mulrrz_int(GEN z, GEN x, GEN y, long lz, long flag, long sz)
     314             : {
     315    55648752 :   pari_sp av = avma;
     316    55648752 :   GEN hi = muliispec_mirror(y+2, x+2, lz+flag-2, lz-2);
     317    55648752 :   mulrrz_end(z, hi, lz, sz, expo(x)+expo(y), hi[lz]);
     318    55648752 :   set_avma(av);
     319    55648752 : }
     320             : 
     321             : /* lz = 3 */
     322             : INLINE void
     323   651910071 : mulrrz_3(GEN z, GEN x, GEN y, long flag, long sz)
     324             : {
     325             :   ulong garde;
     326             :   LOCAL_HIREMAINDER;
     327   651910071 :   if (flag)
     328             :   {
     329    77752583 :     (void)mulll(x[2],y[3]);
     330    77752583 :     garde = addmul(x[2],y[2]);
     331             :   }
     332             :   else
     333   574157488 :     garde = mulll(x[2],y[2]);
     334   651910071 :   z[2] = hiremainder;
     335   651910071 :   mulrrz_3end(z, sz, expo(x)+expo(y), garde);
     336   651970956 : }
     337             : 
     338             : /* set z <-- x*y, floating point multiplication. Trailing 0s for x are
     339             :  * treated efficiently (important application: mulir).
     340             :  * lz = lg(z) = lg(x) <= ly <= lg(y), sz = signe(z). flag = lg(x) < lg(y) */
     341             : INLINE void
     342  3715093882 : mulrrz_i(GEN z, GEN x, GEN y, long lz, long flag, long sz)
     343             : {
     344             :   long ez, i, j, lzz, p1;
     345             :   ulong garde;
     346             :   GEN y1;
     347             :   LOCAL_HIREMAINDER;
     348             :   LOCAL_OVERFLOW;
     349             : 
     350  3715093882 :   if (x == y) { sqrz_i(z,x,lz); return; }
     351  3715093882 :   if (lz > prec2lg(MULRR_MULII_LIMIT)) { mulrrz_int(z,x,y,lz,flag,sz); return; }
     352  3662434015 :   if (lz == 3) { mulrrz_3(z,x,y,flag,sz); return; }
     353  3010525045 :   ez = expo(x) + expo(y);
     354  3010525045 :   if (flag) { (void)mulll(x[2],y[lz]); garde = hiremainder; } else garde = 0;
     355  3010525045 :   lzz=lz-1; p1=x[lzz];
     356  3010525045 :   if (p1)
     357             :   {
     358  2807891678 :     (void)mulll(p1,y[3]);
     359  2807891678 :     garde = addll(addmul(p1,y[2]), garde);
     360  2807891678 :     z[lzz] = overflow+hiremainder;
     361             :   }
     362   202633367 :   else z[lzz]=0;
     363 13640286357 :   for (j=lz-2, y1=y-j; j>=3; j--)
     364             :   {
     365 10629761312 :     p1 = x[j]; y1++;
     366 10629761312 :     if (p1)
     367             :     {
     368 10326805920 :       (void)mulll(p1,y1[lz+1]);
     369 10326805920 :       garde = addll(addmul(p1,y1[lz]), garde);
     370 63241878523 :       for (i=lzz; i>j; i--)
     371             :       {
     372 52915072603 :         hiremainder += overflow;
     373 52915072603 :         z[i] = addll(addmul(p1,y1[i]), z[i]);
     374             :       }
     375 10326805920 :       z[j] = hiremainder+overflow;
     376             :     }
     377   302955392 :     else z[j]=0;
     378             :   }
     379  3010525045 :   p1 = x[2]; y1++;
     380  3010525045 :   garde = addll(mulll(p1,y1[lz]), garde);
     381 16711300106 :   for (i=lzz; i>2; i--)
     382             :   {
     383 13700775061 :     hiremainder += overflow;
     384 13700775061 :     z[i] = addll(addmul(p1,y1[i]), z[i]);
     385             :   }
     386  3010525045 :   z[2] = hiremainder+overflow;
     387  3010525045 :   mulrrz_end(z, z, lz, sz, ez, garde);
     388             : }
     389             : 
     390             : GEN
     391  3961014662 : mulrr(GEN x, GEN y)
     392             : {
     393             :   long flag, ly, lz, sx, sy;
     394             :   GEN z;
     395             : 
     396  3961014662 :   if (x == y) return sqrr(x);
     397  3959401230 :   sx = signe(x); if (!sx) return real_0_bit(expo(x) + expo(y));
     398  3691847567 :   sy = signe(y); if (!sy) return real_0_bit(expo(x) + expo(y));
     399  3658336478 :   if (sy < 0) sx = -sx;
     400  3658336478 :   lz = lg(x);
     401  3658336478 :   ly = lg(y);
     402  3658336478 :   if (lz > ly) { lz = ly; swap(x, y); flag = 1; } else flag = (lz != ly);
     403  3658336478 :   z = cgetg(lz, t_REAL);
     404  3653463588 :   mulrrz_i(z, x,y, lz,flag, sx);
     405  3661241829 :   return z;
     406             : }
     407             : 
     408             : GEN
     409   591964777 : sqrr(GEN x)
     410             : {
     411   591964777 :   long lz, sx = signe(x);
     412             :   GEN z;
     413             : 
     414   591964777 :   if (!sx) return real_0_bit(2*expo(x));
     415   559973946 :   lz = lg(x); z = cgetg(lz, t_REAL);
     416   559412561 :   sqrz_i(z, x, lz);
     417   560206347 :   return z;
     418             : }
     419             : 
     420             : GEN
     421   978721822 : mulir(GEN x, GEN y)
     422             : {
     423   978721822 :   long sx = signe(x), sy;
     424   978721822 :   if (!sx) return mul0r(y);
     425   978247841 :   if (lgefint(x) == 3) {
     426   867759821 :     GEN z = mulur(uel(x,2), y);
     427   868978101 :     if (sx < 0) togglesign(z);
     428   868699975 :     return z;
     429             :   }
     430   110488020 :   sy = signe(y);
     431   110488020 :   if (!sy) return real_0_bit(expi(x) + expo(y));
     432   109803403 :   if (sy < 0) sx = -sx;
     433             :   {
     434   109803403 :     long lz = lg(y), lx = lgefint(x);
     435   109803403 :     GEN hi, z = cgetg(lz, t_REAL);
     436   109358221 :     pari_sp av = avma;
     437   109358221 :     if (lx < (lz>>1) || (lx < lz && lz > prec2lg(MULRR_MULII_LIMIT)))
     438             :     { /* size mantissa of x < half size of mantissa z, or lx < lz so large
     439             :        * that mulrr will call mulii anyway: mulii */
     440    47828010 :       x = itor(x, lg2prec(lx));
     441    47453453 :       hi = muliispec_mirror(y+2, x+2, lz-2, lx-2);
     442    49016311 :       mulrrz_end(z, hi, lz, sx, expo(x)+expo(y), hi[lz]);
     443             :     }
     444             :     else /* dubious: complete x with 0s and call mulrr */
     445    61501600 :       mulrrz_i(z, itor(x, lg2prec(lz)), y, lz, 0, sx);
     446   110945294 :     set_avma(av); return z;
     447             :   }
     448             : }
     449             : 
     450             : /* x + y*z, generic. If lgefint(z) <= 3, caller should use faster variants  */
     451             : static GEN
     452    90689434 : addmulii_gen(GEN x, GEN y, GEN z, long lz)
     453             : {
     454    90689434 :   long lx = lgefint(x), ly;
     455             :   pari_sp av;
     456             :   GEN t;
     457    90689434 :   if (lx == 2) return mulii(z,y);
     458    87892624 :   ly = lgefint(y);
     459    87892624 :   if (ly == 2) return icopy(x); /* y = 0, wasteful copy */
     460    87283013 :   av = avma; (void)new_chunk(lx+ly+lz); /*HACK*/
     461    87284377 :   t = mulii(z, y);
     462    87276597 :   set_avma(av); return addii(t,x);
     463             : }
     464             : /* x + y*z, lgefint(z) == 3 */
     465             : static GEN
     466   467070359 : addmulii_lg3(GEN x, GEN y, GEN z)
     467             : {
     468   467070359 :   long s = signe(z), lx, ly;
     469   467070359 :   ulong w = z[2];
     470             :   pari_sp av;
     471             :   GEN t;
     472   467070359 :   if (w == 1) return (s > 0)? addii(x,y): subii(x,y); /* z = +- 1 */
     473   385415199 :   lx = lgefint(x);
     474   385415199 :   ly = lgefint(y);
     475   385415199 :   if (lx == 2)
     476             :   { /* x = 0 */
     477    71404223 :     if (ly == 2) return gen_0;
     478    39556072 :     t = muluispec(w, y+2, ly-2);
     479    39556742 :     if (signe(y) < 0) s = -s;
     480    39556742 :     setsigne(t, s); return t;
     481             :   }
     482   314010976 :   if (ly == 2) return icopy(x); /* y = 0, wasteful copy */
     483   273426096 :   av = avma; (void)new_chunk(1+lx+ly);/*HACK*/
     484   273567993 :   t = muluispec(w, y+2, ly-2);
     485   273522549 :   if (signe(y) < 0) s = -s;
     486   273522549 :   setsigne(t, s);
     487   273522549 :   set_avma(av); return addii(x,t);
     488             : }
     489             : /* x + y*z */
     490             : GEN
     491   167023776 : addmulii(GEN x, GEN y, GEN z)
     492             : {
     493   167023776 :   long lz = lgefint(z);
     494   167023776 :   switch(lz)
     495             :   {
     496      740502 :     case 2: return icopy(x); /* z = 0, wasteful copy */
     497   126134926 :     case 3: return addmulii_lg3(x, y, z);
     498    40148348 :     default:return addmulii_gen(x, y, z, lz);
     499             :   }
     500             : }
     501             : /* x + y*z, returns x itself and not a copy when y*z = 0 */
     502             : GEN
     503  1568174510 : addmulii_inplace(GEN x, GEN y, GEN z)
     504             : {
     505             :   long lz;
     506  1568174510 :   if (lgefint(y) == 2) return x;
     507   391313407 :   lz = lgefint(z);
     508   391313407 :   switch(lz)
     509             :   {
     510       17712 :     case 2: return x;
     511   340973701 :     case 3: return addmulii_lg3(x, y, z);
     512    50321994 :     default:return addmulii_gen(x, y, z, lz);
     513             :   }
     514             : }
     515             : 
     516             : /* written by Bruno Haible following an idea of Robert Harley */
     517             : long
     518  2216723841 : vals(ulong z)
     519             : {
     520             :   static char tab[64]={-1,0,1,12,2,6,-1,13,3,-1,7,-1,-1,-1,-1,14,10,4,-1,-1,8,-1,-1,25,-1,-1,-1,-1,-1,21,27,15,31,11,5,-1,-1,-1,-1,-1,9,-1,-1,24,-1,-1,20,26,30,-1,-1,-1,-1,23,-1,19,29,-1,22,18,28,17,16,-1};
     521             : #ifdef LONG_IS_64BIT
     522             :   long s;
     523             : #endif
     524             : 
     525  2216723841 :   if (!z) return -1;
     526             : #ifdef LONG_IS_64BIT
     527  1989626777 :   if (! (z&0xffffffff)) { s = 32; z >>=32; } else s = 0;
     528             : #endif
     529  2216723841 :   z |= ~z + 1;
     530  2216723841 :   z += z << 4;
     531  2216723841 :   z += z << 6;
     532  2216723841 :   z ^= z << 16; /* or  z -= z<<16 */
     533             : #ifdef LONG_IS_64BIT
     534  1989626777 :   return s + tab[(z&0xffffffff)>>26];
     535             : #else
     536   227097064 :   return tab[z>>26];
     537             : #endif
     538             : }
     539             : 
     540             : GEN
     541         175 : divsi(long x, GEN y)
     542             : {
     543         175 :   long p1, s = signe(y);
     544             :   LOCAL_HIREMAINDER;
     545             : 
     546         175 :   if (!s) pari_err_INV("divsi",gen_0);
     547         175 :   if (!x || lgefint(y)>3 || ((long)y[2])<0) return gen_0;
     548         175 :   hiremainder=0; p1=divll(labs(x),y[2]);
     549         175 :   if (x<0) { hiremainder = -((long)hiremainder); p1 = -p1; }
     550         175 :   if (s<0) p1 = -p1;
     551         175 :   return stoi(p1);
     552             : }
     553             : 
     554             : GEN
     555     3492035 : divir(GEN x, GEN y)
     556             : {
     557             :   GEN z;
     558     3492035 :   long ly = lg(y), lx = lgefint(x);
     559             :   pari_sp av;
     560             : 
     561     3492035 :   if (ly == 2) pari_err_INV("divir",y);
     562     3492038 :   if (lx == 2) return div0r(y);
     563     3305702 :   if (lx == 3) {
     564     1958549 :     z = divur(x[2], y);
     565     1958551 :     if (signe(x) < 0) togglesign(z);
     566     1958551 :     return z;
     567             :   }
     568     1347153 :   z = cgetg(ly, t_REAL); av = avma;
     569     1347152 :   affrr(divrr(itor(x, lg2prec(ly+1)), y), z);
     570     1347154 :   set_avma(av); return z;
     571             : }
     572             : 
     573             : GEN
     574     2491770 : divur(ulong x, GEN y)
     575             : {
     576             :   pari_sp av;
     577     2491770 :   long p = realprec(y);
     578             :   GEN z;
     579             : 
     580     2491770 :   if (p == 0) pari_err_INV("divur",y);
     581     2491770 :   if (!x) return div0r(y);
     582     2491770 :   if (p > INVNEWTON_LIMIT) {
     583         183 :     av = avma; z = invr(y);
     584         183 :     if (x == 1) return z;
     585         183 :     return gc_uptoleaf(av, mulur(x, z));
     586             :   }
     587     2491587 :   z = cgetr(p); av = avma;
     588     2491570 :   affrr(divrr(utor(x, p + BITS_IN_LONG), y), z);
     589     2491579 :   set_avma(av); return z;
     590             : }
     591             : 
     592             : GEN
     593         798 : divsr(long x, GEN y)
     594             : {
     595             :   pari_sp av;
     596         798 :   long p = realprec(y);
     597             :   GEN z;
     598             : 
     599         798 :   if (p == 0) pari_err_INV("divsr",y);
     600         798 :   if (!x) return div0r(y);
     601         798 :   if (p > INVNEWTON_LIMIT) {
     602           0 :     av = avma; z = invr(y);
     603           0 :     if (x == 1) return z;
     604           0 :     if (x ==-1) { togglesign(z); return z; }
     605           0 :     return gc_uptoleaf(av, mulsr(x, z));
     606             :   }
     607         798 :   z = cgetr(p); av = avma;
     608         798 :   affrr(divrr(stor(x,p + BITS_IN_LONG), y), z);
     609         798 :   set_avma(av); return z;
     610             : }
     611             : 
     612             : /* returns 1/y, assume y != 0 */
     613             : static GEN
     614    62419208 : invr_basecase(GEN y)
     615             : {
     616    62419208 :   long p = realprec(y);
     617    62419208 :   GEN z = cgetr(p);
     618    62408965 :   pari_sp av = avma;
     619    62408965 :   affrr(divrr(real_1(p + BITS_IN_LONG), y), z);
     620    62417814 :   set_avma(av); return z;
     621             : }
     622             : /* returns 1/b, Newton iteration */
     623             : GEN
     624    62420078 : invr(GEN b)
     625             : {
     626    62420078 :   const long s = 6;
     627    62420078 :   long i, p, l = lg(b);
     628             :   GEN x, a;
     629             :   ulong mask;
     630             : 
     631    62420078 :   if (l <= maxss(prec2lg(INVNEWTON_LIMIT), (1L<<s) + 2)) {
     632    62415158 :     if (l == 2) pari_err_INV("invr",b);
     633    62415158 :     return invr_basecase(b);
     634             :   }
     635        4294 :   mask = quadratic_prec_mask(l-2);
     636       30058 :   for(i=0, p=1; i<s; i++) { p <<= 1; if (mask & 1) p--; mask >>= 1; }
     637        4294 :   x = cgetg(l, t_REAL);
     638        4294 :   a = rcopy(b); a[1] = _evalexpo(0) | evalsigne(1);
     639        4294 :   affrr(invr_basecase(rtor(a, lg2prec(p+2))), x);
     640       13747 :   while (mask > 1)
     641             :   {
     642        9453 :     p <<= 1; if (mask & 1) p--;
     643        9453 :     mask >>= 1;
     644        9453 :     setlg(a, p + 2);
     645        9453 :     setlg(x, p + 2);
     646             :     /* TODO: mulrr(a,x) should be a half product (the higher half is known).
     647             :      * mulrr(x, ) already is */
     648        9453 :     affrr(addrr(x, mulrr(x, subsr(1, mulrr(a,x)))), x);
     649        9453 :     set_avma((pari_sp)a);
     650             :   }
     651        4294 :   x[1] = (b[1] & SIGNBITS) | evalexpo(expo(x)-expo(b));
     652        4294 :   set_avma((pari_sp)x); return x;
     653             : }
     654             : 
     655             : GEN
     656  3059392729 : modii(GEN x, GEN y)
     657             : {
     658  3059392729 :   switch(signe(x))
     659             :   {
     660   494391737 :     case 0: return gen_0;
     661  2031836393 :     case 1: return remii(x,y);
     662   533164599 :     default:
     663             :     {
     664   533164599 :       pari_sp av = avma;
     665   533164599 :       (void)new_chunk(lgefint(y));
     666   537558381 :       x = remii(x,y); set_avma(av);
     667   537633767 :       if (x==gen_0) return x;
     668   496563584 :       return subiispec(y+2,x+2,lgefint(y)-2,lgefint(x)-2);
     669             :     }
     670             :   }
     671             : }
     672             : 
     673             : GEN
     674     9823208 : divrs(GEN x, long y)
     675             : {
     676             :   GEN z;
     677     9823208 :   if (y < 0)
     678             :   {
     679     5766780 :     z = divru(x, (ulong)-y);
     680     5766732 :     togglesign(z);
     681             :   }
     682             :   else
     683     4056428 :     z = divru(x, (ulong)y);
     684     9823155 :   return z;
     685             : }
     686             : 
     687             : GEN
     688  1105578139 : divru(GEN x, ulong y)
     689             : {
     690  1105578139 :   long i, lx, sh, e, s = signe(x);
     691             :   ulong garde;
     692             :   GEN z;
     693             :   LOCAL_HIREMAINDER;
     694             : 
     695  1105578139 :   if (!y) pari_err_INV("divru",gen_0);
     696  1105684172 :   if (!s) return real_0_bit(expo(x) - expu(y));
     697  1105046670 :   if (!(y & (y-1))) /* power of 2 */
     698             :   {
     699    92933338 :     if (y == 1) return rcopy(x);
     700    90277968 :     return shiftr(x, -expu(y));
     701             :   }
     702  1012113332 :   e = expo(x);
     703  1012113332 :   lx = lg(x);
     704  1012113332 :   z = cgetg(lx, t_REAL);
     705  1011231375 :   if (lx == 3)
     706             :   {
     707   160234523 :     if (y <= uel(x,2))
     708             :     {
     709   160234253 :       hiremainder = 0;
     710   160234253 :       z[2] = divll(x[2],y);
     711             :       /* we may have hiremainder != 0 ==> garde */
     712   160234253 :       garde = divll(0,y);
     713             :     }
     714             :     else
     715             :     {
     716         270 :       hiremainder = x[2];
     717         270 :       z[2] = divll(0,y);
     718         270 :       garde = hiremainder;
     719         270 :       e -= BITS_IN_LONG;
     720             :     }
     721             :   }
     722             :   else
     723             :   {
     724   850996852 :     ulong yp = get_Fl_red(y);
     725   851732745 :     if (y <= uel(x,2))
     726             :     {
     727   851741354 :       hiremainder = 0;
     728  7404356360 :       for (i=2; i<lx; i++) z[i] = divll_pre(x[i],y,yp);
     729             :       /* we may have hiremainder != 0 ==> garde */
     730   856061296 :       garde = divll_pre(0,y,yp);
     731             :     }
     732             :     else
     733             :     {
     734        3231 :       long l = lx-1;
     735        3231 :       hiremainder = x[2];
     736       56821 :       for (i=2; i<l; i++) z[i] = divll_pre(x[i+1],y,yp);
     737        3231 :       z[i] = divll_pre(0,y,yp);
     738        3765 :       garde = hiremainder;
     739        3765 :       e -= BITS_IN_LONG;
     740             :     }
     741             :   }
     742  1014460809 :   sh=bfffo(z[2]); /* z[2] != 0 */
     743  1014460809 :   if (sh) shift_left(z,z, 2,lx-1, garde,sh);
     744  1014570794 :   z[1] = evalsigne(s) | evalexpo(e-sh);
     745  1013389782 :   if ((garde << sh) & HIGHBIT) roundr_up_ip(z, lx);
     746  1013322885 :   return z;
     747             : }
     748             : 
     749             : GEN
     750   132251827 : truedvmdii(GEN x, GEN y, GEN *z)
     751             : {
     752             :   pari_sp av;
     753             :   GEN r, q;
     754   132251827 :   if (!is_bigint(y)) return truedvmdis(x, itos(y), z);
     755     5301384 :   if (z == ONLY_REM) return modii(x,y);
     756             : 
     757     5301384 :   av = avma;
     758     5301384 :   q = dvmdii(x,y,&r); /* assume that r is last on stack */
     759     5301384 :   switch(signe(r))
     760             :   {
     761      703385 :     case 0:
     762      703385 :       if (z) *z = gen_0;
     763      703385 :       return q;
     764     3565124 :     case 1:
     765     3565124 :       if (z) *z = r; else cgiv(r);
     766     3565124 :       return q;
     767     1032875 :     case -1: break;
     768             :   }
     769     1032875 :   q = addis(q, -signe(y));
     770     1032875 :   if (!z) return gc_INT(av, q);
     771             : 
     772      856723 :   *z = subiispec(y+2,r+2, lgefint(y)-2,lgefint(r)-2);
     773      856723 :   return gc_all_unsafe(av,(pari_sp)r,2,&q,z);
     774             : }
     775             : GEN
     776   129104022 : truedvmdis(GEN x, long y, GEN *z)
     777             : {
     778   129104022 :   pari_sp av = avma;
     779             :   long r;
     780             :   GEN q;
     781             : 
     782   129104022 :   if (z == ONLY_REM) return modis(x, y);
     783   129104022 :   q = divis_rem(x,y,&r);
     784             : 
     785   129083666 :   if (r >= 0)
     786             :   {
     787   117514426 :     if (z) *z = utoi(r);
     788   117513485 :     return q;
     789             :   }
     790    11569240 :   q = gc_INT(av, addis(q, (y < 0)? 1: -1));
     791    11581569 :   if (z) *z = utoi(r + labs(y));
     792    11581538 :   return q;
     793             : }
     794             : GEN
     795     6202031 : truedvmdsi(long x, GEN y, GEN *z)
     796             : {
     797             :   long q, r;
     798     6202031 :   if (z == ONLY_REM) return modsi(x, y);
     799     6202031 :   q = sdivsi_rem(x,y,&r);
     800     6202031 :   if (r >= 0) {
     801     6202031 :     if (z) *z = utoi(r);
     802     6202031 :     return stoi(q);
     803             :   }
     804           0 :   q = q - signe(y);
     805           0 :   if (!z) return stoi(q);
     806             : 
     807           0 :   *z = subiuspec(y+2,(ulong)-r, lgefint(y)-2);
     808           0 :   return stoi(q);
     809             : }
     810             : 
     811             : /* 2^n = shifti(gen_1, n) */
     812             : GEN
     813   105882224 : int2n(long n) {
     814             :   long i, m, l;
     815             :   GEN z;
     816   105882224 :   if (n < 0) return gen_0;
     817   105863326 :   if (n == 0) return gen_1;
     818             : 
     819   102207750 :   l = dvmdsBIL(n, &m) + 3;
     820   102206693 :   z = cgetipos(l);
     821   553050552 :   for (i = 2; i < l; i++) z[i] = 0;
     822   102181494 :   *int_MSW(z) = 1UL << m; return z;
     823             : }
     824             : /* To avoid problems when 2^(BIL-1) < n. Overflow cleanly, where int2n
     825             :  * returns gen_0 */
     826             : GEN
     827    25518900 : int2u(ulong n) {
     828             :   ulong i, m, l;
     829             :   GEN z;
     830    25518900 :   if (n == 0) return gen_1;
     831             : 
     832    25518900 :   l = dvmduBIL(n, &m) + 3;
     833    25519261 :   z = cgetipos(l);
     834    51419205 :   for (i = 2; i < l; i++) z[i] = 0;
     835    25517629 :   *int_MSW(z) = 1UL << m; return z;
     836             : }
     837             : /* 2^n - 1 */
     838             : GEN
     839        8831 : int2um1(ulong n) {
     840             :   ulong i, m, l;
     841             :   GEN z;
     842        8831 :   if (n == 0) return gen_0;
     843             : 
     844        8831 :   l = dvmduBIL(n, &m);
     845        8831 :   l += m? 3: 2;
     846        8831 :   z = cgetipos(l);
     847       17802 :   for (i = 2; i < l; i++) z[i] = ~0UL;
     848        8831 :   if (m) *int_MSW(z) = (1UL << m) - 1;
     849        8831 :   return z;
     850             : }
     851             : 
     852             : GEN
     853  2237317758 : shifti(GEN x, long n)
     854             : {
     855  2237317758 :   long s = signe(x);
     856             :   GEN y;
     857             : 
     858  2237317758 :   if(s == 0) return gen_0;
     859  1930161222 :   y = shiftispec(x + 2, lgefint(x) - 2, n);
     860  1930136294 :   if (signe(y)) setsigne(y, s);
     861  1930136294 :   return y;
     862             : }
     863             : 
     864             : /* actual operations will take place on a+2 and b+2: we strip the codewords */
     865             : GEN
     866 20996563786 : mulii(GEN a,GEN b)
     867             : {
     868             :   long sa,sb;
     869             :   GEN z;
     870             : 
     871 20996563786 :   sa=signe(a); if (!sa) return gen_0;
     872 14288300042 :   sb=signe(b); if (!sb) return gen_0;
     873 10562899610 :   if (sb<0) sa = -sa;
     874 10562899610 :   z = muliispec(a+2,b+2, lgefint(a)-2,lgefint(b)-2);
     875 10592618667 :   setsigne(z,sa); return z;
     876             : }
     877             : 
     878             : GEN
     879  1887043871 : sqri(GEN a) { return sqrispec(a+2, lgefint(a)-2); }
     880             : 
     881             : /* sqrt()'s result may be off by 1 when a is not representable exactly as a
     882             :  * double [64bit machine] */
     883             : ulong
     884   228848417 : usqrt(ulong a)
     885             : {
     886   228848417 :   ulong x = (ulong)sqrt((double)a);
     887             : #ifdef LONG_IS_64BIT
     888   196053565 :   if (x > LOWMASK || x*x > a) x--;
     889             : #endif
     890   228848417 :   return x;
     891             : }
     892             : 
     893             : /********************************************************************/
     894             : /**                                                                **/
     895             : /**              EXPONENT / CONVERSION t_REAL --> double           **/
     896             : /**                                                                **/
     897             : /********************************************************************/
     898             : 
     899             : #ifdef LONG_IS_64BIT
     900             : long
     901    22515096 : dblexpo(double x)
     902             : {
     903             :   union { double f; ulong i; } fi;
     904    22515096 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     905    22515096 :   const int exp_mid = 0x3ff;/* exponent bias */
     906             : 
     907    22515096 :   if (x==0.) return -exp_mid;
     908    22515084 :   fi.f = x;
     909    22515084 :   return ((fi.i & (HIGHBIT-1)) >> mant_len) - exp_mid;
     910             : }
     911             : 
     912             : ulong
     913           0 : dblmantissa(double x)
     914             : {
     915             :   union { double f; ulong i; } fi;
     916           0 :   const int expo_len = 11; /* number of bits of exponent */
     917             : 
     918           0 :   if (x==0.) return 0;
     919           0 :   fi.f = x;
     920           0 :   return (fi.i << expo_len) | HIGHBIT;
     921             : }
     922             : 
     923             : GEN
     924    11481834 : dbltor(double x)
     925             : {
     926             :   GEN z;
     927             :   long e;
     928             :   union { double f; ulong i; } fi;
     929    11481834 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     930    11481834 :   const int exp_mid = 0x3ff;/* exponent bias */
     931    11481834 :   const int expo_len = 11; /* number of bits of exponent */
     932             : 
     933    11481834 :   if (x==0.) return real_0_bit(-exp_mid);
     934    11476514 :   fi.f = x; z = cgetr(DEFAULTPREC);
     935             :   {
     936    11474015 :     const ulong a = fi.i;
     937             :     ulong A;
     938    11474015 :     e = ((a & (HIGHBIT-1)) >> mant_len) - exp_mid;
     939    11474015 :     if (e == exp_mid+1) pari_err_OVERFLOW("dbltor [NaN or Infinity]");
     940    11474003 :     A = a << expo_len;
     941    11474003 :     if (e == -exp_mid)
     942             :     { /* unnormalized values */
     943           0 :       int sh = bfffo(A);
     944           0 :       e -= sh-1;
     945           0 :       z[2] = A << sh;
     946             :     }
     947             :     else
     948    11474003 :       z[2] = HIGHBIT | A;
     949    11474003 :     z[1] = _evalexpo(e) | evalsigne(x<0? -1: 1);
     950             :   }
     951    11474003 :   return z;
     952             : }
     953             : 
     954             : double
     955   247609603 : rtodbl(GEN x)
     956             : {
     957   247609603 :   long ex,s=signe(x);
     958             :   ulong a;
     959             :   union { double f; ulong i; } fi;
     960   247609603 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     961   247609603 :   const int exp_mid = 0x3ff;/* exponent bias */
     962   247609603 :   const int expo_len = 11; /* number of bits of exponent */
     963             : 
     964   247609603 :   if (!s || (ex=expo(x)) < - exp_mid) return 0.0;
     965             : 
     966             :   /* start by rounding to closest */
     967   218940940 :   a = (x[2] & (HIGHBIT-1)) + 0x400;
     968   218940940 :   if (a & HIGHBIT) { ex++; a=0; }
     969   218940940 :   if (ex >= exp_mid) pari_err_OVERFLOW("t_REAL->double conversion");
     970   218935589 :   fi.i = ((ex + exp_mid) << mant_len) | (a >> expo_len);
     971   218935589 :   if (s<0) fi.i |= HIGHBIT;
     972   218935589 :   return fi.f;
     973             : }
     974             : 
     975             : #else /* LONG_IS_64BIT */
     976             : 
     977             : #if   PARI_DOUBLE_FORMAT == 1
     978             : #  define INDEX0 1
     979             : #  define INDEX1 0
     980             : #elif PARI_DOUBLE_FORMAT == 0
     981             : #  define INDEX0 0
     982             : #  define INDEX1 1
     983             : #endif
     984             : 
     985             : long
     986     3782523 : dblexpo(double x)
     987             : {
     988             :   union { double f; ulong i[2]; } fi;
     989     3782523 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     990     3782523 :   const int exp_mid = 0x3ff;/* exponent bias */
     991     3782523 :   const int shift = mant_len-32;
     992             : 
     993     3782523 :   if (x==0.) return -exp_mid;
     994     3782521 :   fi.f = x;
     995             :   {
     996     3782521 :     const ulong a = fi.i[INDEX0];
     997     3782521 :     return ((a & (HIGHBIT-1)) >> shift) - exp_mid;
     998             :   }
     999             : }
    1000             : 
    1001             : ulong
    1002           0 : dblmantissa(double x)
    1003             : {
    1004             :   union { double f; ulong i[2]; } fi;
    1005           0 :   const int expo_len = 11; /* number of bits of exponent */
    1006             : 
    1007           0 :   if (x==0.) return 0;
    1008           0 :   fi.f = x;
    1009             :   {
    1010           0 :     const ulong a = fi.i[INDEX0];
    1011           0 :     const ulong b = fi.i[INDEX1];
    1012           0 :     return HIGHBIT | b >> (BITS_IN_LONG-expo_len) | (a << expo_len);
    1013             :   }
    1014             : }
    1015             : 
    1016             : GEN
    1017     1875888 : dbltor(double x)
    1018             : {
    1019             :   GEN z;
    1020             :   long e;
    1021             :   union { double f; ulong i[2]; } fi;
    1022     1875888 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
    1023     1875888 :   const int exp_mid = 0x3ff;/* exponent bias */
    1024     1875888 :   const int expo_len = 11; /* number of bits of exponent */
    1025     1875888 :   const int shift = mant_len-32;
    1026             : 
    1027     1875888 :   if (x==0.) return real_0_bit(-exp_mid);
    1028     1875014 :   fi.f = x; z = cgetr(DEFAULTPREC);
    1029             :   {
    1030     1875014 :     const ulong a = fi.i[INDEX0];
    1031     1875014 :     const ulong b = fi.i[INDEX1];
    1032             :     ulong A, B;
    1033     1875014 :     e = ((a & (HIGHBIT-1)) >> shift) - exp_mid;
    1034     1875014 :     if (e == exp_mid+1) pari_err_OVERFLOW("dbltor [NaN or Infinity]");
    1035     1875014 :     A = b >> (BITS_IN_LONG-expo_len) | (a << expo_len);
    1036     1875014 :     B = b << expo_len;
    1037     1875014 :     if (e == -exp_mid)
    1038             :     { /* unnormalized values */
    1039             :       int sh;
    1040           0 :       if (A)
    1041             :       {
    1042           0 :         sh = bfffo(A);
    1043           0 :         e -= sh-1;
    1044           0 :         z[2] = (A << sh) | (B >> (32-sh));
    1045           0 :         z[3] = B << sh;
    1046             :       }
    1047             :       else
    1048             :       {
    1049           0 :         sh = bfffo(B); /* B != 0 */
    1050           0 :         e -= sh-1 + 32;
    1051           0 :         z[2] = B << sh;
    1052           0 :         z[3] = 0;
    1053             :       }
    1054             :     }
    1055             :     else
    1056             :     {
    1057     1875014 :       z[3] = B;
    1058     1875014 :       z[2] = HIGHBIT | A;
    1059             :     }
    1060     1875014 :     z[1] = _evalexpo(e) | evalsigne(x<0? -1: 1);
    1061             :   }
    1062     1875014 :   return z;
    1063             : }
    1064             : 
    1065             : double
    1066    40552285 : rtodbl(GEN x)
    1067             : {
    1068    40552285 :   long ex,s=signe(x),lx=lg(x);
    1069             :   ulong a,b,k;
    1070             :   union { double f; ulong i[2]; } fi;
    1071    40552285 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
    1072    40552285 :   const int exp_mid = 0x3ff;/* exponent bias */
    1073    40552285 :   const int expo_len = 11; /* number of bits of exponent */
    1074    40552285 :   const int shift = mant_len-32;
    1075             : 
    1076    40552285 :   if (!s || (ex=expo(x)) < - exp_mid) return 0.0;
    1077             : 
    1078             :   /* start by rounding to closest */
    1079    35722163 :   a = x[2] & (HIGHBIT-1);
    1080    35722163 :   if (lx > 3)
    1081             :   {
    1082    35595183 :     b = x[3] + 0x400UL; if (b < 0x400UL) a++;
    1083    35595183 :     if (a & HIGHBIT) { ex++; a=0; }
    1084             :   }
    1085      126980 :   else b = 0;
    1086    35722163 :   if (ex >= exp_mid) pari_err_OVERFLOW("t_REAL->double conversion");
    1087    35722163 :   ex += exp_mid;
    1088    35722163 :   k = (a >> expo_len) | (ex << shift);
    1089    35722163 :   if (s<0) k |= HIGHBIT;
    1090    35722163 :   fi.i[INDEX0] = k;
    1091    35722163 :   fi.i[INDEX1] = (a << (BITS_IN_LONG-expo_len)) | (b >> expo_len);
    1092    35722163 :   return fi.f;
    1093             : }
    1094             : #endif /* LONG_IS_64BIT */
    1095             : 

Generated by: LCOV version 1.16