Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - kernel/none - mp_indep.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21343-6216058) Lines: 521 566 92.0 %
Date: 2017-11-19 06:21:17 Functions: 51 53 96.2 %
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. 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             : /* Find c such that 1=c*b mod 2^BITS_IN_LONG, assuming b odd (unchecked) */
      16             : ulong
      17    18684688 : invmod2BIL(ulong b)
      18             : {
      19             :   static int tab[] = { 0, 0, 0, 8, 0, 8, 0, 0 };
      20    18684688 :   ulong x = b + tab[b & 7]; /* b^(-1) mod 2^4 */
      21             : 
      22             :   /* Newton applied to 1/x - b = 0 */
      23             : #ifdef LONG_IS_64BIT
      24    18684688 :   x = x*(2-b*x); /* one more pass necessary */
      25             : #endif
      26    18684688 :   x = x*(2-b*x);
      27    18684688 :   x = x*(2-b*x); return x*(2-b*x);
      28             : }
      29             : 
      30             : void
      31  1017347270 : affrr(GEN x, GEN y)
      32             : {
      33             :   long lx,ly,i;
      34             : 
      35  1017347270 :   y[1] = x[1]; if (!signe(x)) return;
      36             : 
      37  1011126997 :   lx=lg(x); ly=lg(y);
      38  1011126997 :   if (lx <= ly)
      39             :   {
      40   950566330 :     for (i=2; i<lx; i++) y[i]=x[i];
      41   950566330 :     for (   ; i<ly; i++) y[i]=0;
      42   950566330 :     return;
      43             :   }
      44    60560667 :   for (i=2; i<ly; i++) y[i]=x[i];
      45             :   /* lx > ly: round properly */
      46    60560667 :   if (x[ly] & HIGHBIT) roundr_up_ip(y, ly);
      47             : }
      48             : 
      49             : GEN
      50     7181912 : trunc2nr(GEN x, long n)
      51             : {
      52             :   long ex;
      53     7181912 :   if (!signe(x)) return gen_0;
      54     7120099 :   ex = expo(x) + n; if (ex < 0) return gen_0;
      55     6757542 :   return mantissa2nr(x, ex - bit_prec(x) + 1);
      56             : }
      57             : 
      58             : /* x a t_REAL, x = i/2^e, i a t_INT */
      59             : GEN
      60    16132213 : mantissa_real(GEN x, long *e)
      61             : {
      62    16132213 :   *e = bit_prec(x)-1-expo(x);
      63    16132213 :   return mantissa2nr(x, 0);
      64             : }
      65             : 
      66             : GEN
      67   960344762 : mului(ulong x, GEN y)
      68             : {
      69   960344762 :   long s = signe(y);
      70             :   GEN z;
      71             : 
      72   960344762 :   if (!s || !x) return gen_0;
      73   902675106 :   z = muluispec(x, y+2, lgefint(y)-2);
      74   902014163 :   setsigne(z,s); return z;
      75             : }
      76             : 
      77             : GEN
      78   200742097 : mulsi(long x, GEN y)
      79             : {
      80   200742097 :   long s = signe(y);
      81             :   GEN z;
      82             : 
      83   200742097 :   if (!s || !x) return gen_0;
      84   155989710 :   if (x<0) { s = -s; x = -x; }
      85   155989710 :   z = muluispec((ulong)x, y+2, lgefint(y)-2);
      86   155989664 :   setsigne(z,s); return z;
      87             : }
      88             : 
      89             : GEN
      90   176920594 : mulss(long x, long y)
      91             : {
      92             :   long p1;
      93             :   LOCAL_HIREMAINDER;
      94             : 
      95   176920594 :   if (!x || !y) return gen_0;
      96   176822958 :   if (x<0) {
      97       59724 :     x = -x;
      98       59724 :     if (y<0) { y = -y; p1 = mulll(x,y); return uutoi(hiremainder, p1); }
      99       59724 :     p1 = mulll(x,y); return uutoineg(hiremainder, p1);
     100             :   } else {
     101   176763234 :     if (y<0) { y = -y; p1 = mulll(x,y); return uutoineg(hiremainder, p1); }
     102   176762540 :     p1 = mulll(x,y); return uutoi(hiremainder, p1);
     103             :   }
     104             : }
     105             : GEN
     106      314119 : sqrs(long x)
     107             : {
     108             :   long p1;
     109             :   LOCAL_HIREMAINDER;
     110             : 
     111      314119 :   if (!x) return gen_0;
     112      313797 :   if (x<0) x = -x;
     113      313797 :   p1 = mulll(x,x); return uutoi(hiremainder, p1);
     114             : }
     115             : GEN
     116  1816792445 : muluu(ulong x, ulong y)
     117             : {
     118             :   long p1;
     119             :   LOCAL_HIREMAINDER;
     120             : 
     121  1816792445 :   if (!x || !y) return gen_0;
     122  1816745952 :   p1 = mulll(x,y); return uutoi(hiremainder, p1);
     123             : }
     124             : GEN
     125   683938129 : sqru(ulong x)
     126             : {
     127             :   long p1;
     128             :   LOCAL_HIREMAINDER;
     129             : 
     130   683938129 :   if (!x) return gen_0;
     131   683598250 :   p1 = mulll(x,x); return uutoi(hiremainder, p1);
     132             : }
     133             : 
     134             : /* assume x > 1, y != 0. Return u * y with sign s */
     135             : static GEN
     136   484632408 : mulur_2(ulong x, GEN y, long s)
     137             : {
     138   484632408 :   long m, sh, i, lx = lg(y), e = expo(y);
     139   484632408 :   GEN z = cgetr(lx);
     140             :   ulong garde;
     141             :   LOCAL_HIREMAINDER;
     142             : 
     143   484632408 :   y--; garde = mulll(x,y[lx]);
     144   484632408 :   for (i=lx-1; i>=3; i--) z[i]=addmul(x,y[i]);
     145   484632408 :   z[2]=hiremainder; /* != 0 since y normalized and |x| > 1 */
     146   484632408 :   sh = bfffo(hiremainder); m = BITS_IN_LONG-sh;
     147   484632408 :   if (sh) shift_left(z,z, 2,lx-1, garde,sh);
     148   484632408 :   z[1] = evalsigne(s) | evalexpo(m+e);
     149   484632408 :   if ((garde << sh) & HIGHBIT) roundr_up_ip(z, lx);
     150   484632408 :   return z;
     151             : }
     152             : 
     153             : INLINE GEN
     154     1175887 : mul0r(GEN x)
     155             : {
     156     1175887 :   long l = lg(x), e = expo(x);
     157     1175887 :   e = (l > 2)? -prec2nbits(l) + e: (e < 0? 2*e: 0);
     158     1175887 :   return real_0_bit(e);
     159             : }
     160             : /* lg(x) > 2 */
     161             : INLINE GEN
     162       65001 : div0r(GEN x) {
     163       65001 :   long l = lg(x), e = expo(x);
     164       65001 :   return real_0_bit(-prec2nbits(l) - e);
     165             : }
     166             : 
     167             : GEN
     168    39694869 : mulsr(long x, GEN y)
     169             : {
     170             :   long s;
     171             : 
     172    39694869 :   if (!x) return mul0r(y);
     173    38677476 :   s = signe(y);
     174    38677476 :   if (!s)
     175             :   {
     176      224164 :     if (x < 0) x = -x;
     177      224164 :     return real_0_bit( expo(y) + expu(x) );
     178             :   }
     179    38453312 :   if (x==1)  return rcopy(y);
     180    37711597 :   if (x==-1) return negr(y);
     181    36723027 :   if (x < 0)
     182    21144669 :     return mulur_2((ulong)-x, y, -s);
     183             :   else
     184    15578358 :     return mulur_2((ulong)x, y, s);
     185             : }
     186             : 
     187             : GEN
     188   517355622 : mulur(ulong x, GEN y)
     189             : {
     190             :   long s;
     191             : 
     192   517355622 :   if (!x) return mul0r(y);
     193   517355622 :   s = signe(y);
     194   517355622 :   if (!s) return real_0_bit( expo(y) + expu(x) );
     195   514646207 :   if (x==1) return rcopy(y);
     196   447909381 :   return mulur_2(x, y, s);
     197             : }
     198             : 
     199             : INLINE void
     200  1102658967 : mulrrz_end(GEN z, GEN hi, long lz, long sz, long ez, ulong garde)
     201             : {
     202             :   long i;
     203  1102658967 :   if (hi[2] < 0)
     204             :   {
     205   505485898 :     if (z != hi)
     206    30304228 :       for (i=2; i<lz ; i++) z[i] = hi[i];
     207   505485898 :     ez++;
     208             :   }
     209             :   else
     210             :   {
     211   597173069 :     shift_left(z,hi,2,lz-1, garde, 1);
     212   597173069 :     garde <<= 1;
     213             :   }
     214  1102658967 :   if (garde & HIGHBIT)
     215             :   { /* round to nearest */
     216   538955903 :     i = lz; do ((ulong*)z)[--i]++; while (i>1 && z[i]==0);
     217   538955903 :     if (i == 1) { z[2] = (long)HIGHBIT; ez++; }
     218             :   }
     219  1102658967 :   z[1] = evalsigne(sz)|evalexpo(ez);
     220  1102658967 : }
     221             : /* mulrrz_end for lz = 3, minor simplifications. z[2]=hiremainder from mulll */
     222             : INLINE void
     223   769174749 : mulrrz_3end(GEN z, long sz, long ez, ulong garde)
     224             : {
     225   769174749 :   if (z[2] < 0)
     226             :   { /* z2 < (2^BIL-1)^2 / 2^BIL, hence z2+1 != 0 */
     227   409692380 :     if (garde & HIGHBIT) z[2]++; /* round properly */
     228   409692380 :     ez++;
     229             :   }
     230             :   else
     231             :   {
     232   359482369 :     uel(z,2) = (uel(z,2)<<1) | (garde>>(BITS_IN_LONG-1));
     233   359482369 :     if (garde & (1UL<<(BITS_IN_LONG-2)))
     234             :     {
     235   164120240 :       uel(z,2)++; /* round properly, z2+1 can overflow */
     236   164120240 :       if (!uel(z,2)) { uel(z,2) = HIGHBIT; ez++; }
     237             :     }
     238             :   }
     239   769174749 :   z[1] = evalsigne(sz)|evalexpo(ez);
     240   769174749 : }
     241             : 
     242             : /* set z <-- x^2 != 0, floating point multiplication.
     243             :  * lz = lg(z) = lg(x) */
     244             : INLINE void
     245    84236006 : sqrz_i(GEN z, GEN x, long lz)
     246             : {
     247    84236006 :   long ez = 2*expo(x);
     248             :   long i, j, lzz, p1;
     249             :   ulong garde;
     250             :   GEN x1;
     251             :   LOCAL_HIREMAINDER;
     252             :   LOCAL_OVERFLOW;
     253             : 
     254    84236006 :   if (lz > MULRR_MULII_LIMIT)
     255             :   {
     256     5328820 :     pari_sp av = avma;
     257     5328820 :     GEN hi = sqrispec_mirror(x+2, lz-2);
     258     5328820 :     mulrrz_end(z, hi, lz, 1, ez, hi[lz]);
     259     5328820 :     avma = av; return;
     260             :   }
     261    78907186 :   if (lz == 3)
     262             :   {
     263    15168864 :     garde = mulll(x[2],x[2]);
     264    15168864 :     z[2] = hiremainder;
     265    15168864 :     mulrrz_3end(z, 1, ez, garde);
     266    15168864 :     return;
     267             :   }
     268             : 
     269    63738322 :   lzz = lz-1; p1 = x[lzz];
     270    63738322 :   if (p1)
     271             :   {
     272    56756590 :     (void)mulll(p1,x[3]);
     273    56756590 :     garde = addmul(p1,x[2]);
     274    56756590 :     z[lzz] = hiremainder;
     275             :   }
     276             :   else
     277             :   {
     278     6981732 :     garde = 0;
     279     6981732 :     z[lzz] = 0;
     280             :   }
     281   214381758 :   for (j=lz-2, x1=x-j; j>=3; j--)
     282             :   {
     283   150643436 :     p1 = x[j]; x1++;
     284   150643436 :     if (p1)
     285             :     {
     286   145098686 :       (void)mulll(p1,x1[lz+1]);
     287   145098686 :       garde = addll(addmul(p1,x1[lz]), garde);
     288   637219830 :       for (i=lzz; i>j; i--)
     289             :       {
     290   492121144 :         hiremainder += overflow;
     291   492121144 :         z[i] = addll(addmul(p1,x1[i]), z[i]);
     292             :       }
     293   145098686 :       z[j] = hiremainder+overflow;
     294             :     }
     295     5544750 :     else z[j]=0;
     296             :   }
     297    63738322 :   p1 = x[2]; x1++;
     298    63738322 :   garde = addll(mulll(p1,x1[lz]), garde);
     299   278120080 :   for (i=lzz; i>2; i--)
     300             :   {
     301   214381758 :     hiremainder += overflow;
     302   214381758 :     z[i] = addll(addmul(p1,x1[i]), z[i]);
     303             :   }
     304    63738322 :   z[2] = hiremainder+overflow;
     305    63738322 :   mulrrz_end(z, z, lz, 1, ez, garde);
     306             : }
     307             : 
     308             : /* lz "large" = lg(y) = lg(z), lg(x) > lz if flag = 1 and >= if flag = 0 */
     309             : INLINE void
     310    60558978 : mulrrz_int(GEN z, GEN x, GEN y, long lz, long flag, long sz)
     311             : {
     312    60558978 :   pari_sp av = avma;
     313    60558978 :   GEN hi = muliispec_mirror(y+2, x+2, lz+flag-2, lz-2);
     314    60558978 :   mulrrz_end(z, hi, lz, sz, expo(x)+expo(y), hi[lz]);
     315    60558978 :   avma = av;
     316    60558978 : }
     317             : 
     318             : /* lz = 3 */
     319             : INLINE void
     320   754005885 : mulrrz_3(GEN z, GEN x, GEN y, long flag, long sz)
     321             : {
     322             :   ulong garde;
     323             :   LOCAL_HIREMAINDER;
     324   754005885 :   if (flag)
     325             :   {
     326    30913826 :     (void)mulll(x[2],y[3]);
     327    30913826 :     garde = addmul(x[2],y[2]);
     328             :   }
     329             :   else
     330   723092059 :     garde = mulll(x[2],y[2]);
     331   754005885 :   z[2] = hiremainder;
     332   754005885 :   mulrrz_3end(z, sz, expo(x)+expo(y), garde);
     333   754005885 : }
     334             : 
     335             : /* set z <-- x*y, floating point multiplication. Trailing 0s for x are
     336             :  * treated efficiently (important application: mulir).
     337             :  * lz = lg(z) = lg(x) <= ly <= lg(y), sz = signe(z). flag = lg(x) < lg(y) */
     338             : INLINE void
     339  1786130034 : mulrrz_i(GEN z, GEN x, GEN y, long lz, long flag, long sz)
     340             : {
     341             :   long ez, i, j, lzz, p1;
     342             :   ulong garde;
     343             :   GEN y1;
     344             :   LOCAL_HIREMAINDER;
     345             :   LOCAL_OVERFLOW;
     346             : 
     347  1786130034 :   if (x == y) { sqrz_i(z,x,lz); return; }
     348  1786130034 :   if (lz > MULRR_MULII_LIMIT) { mulrrz_int(z,x,y,lz,flag,sz); return; }
     349  1725571056 :   if (lz == 3) { mulrrz_3(z,x,y,flag,sz); return; }
     350   971565171 :   ez = expo(x) + expo(y);
     351   971565171 :   if (flag) { (void)mulll(x[2],y[lz]); garde = hiremainder; } else garde = 0;
     352   971565171 :   lzz=lz-1; p1=x[lzz];
     353   971565171 :   if (p1)
     354             :   {
     355   936787188 :     (void)mulll(p1,y[3]);
     356   936787188 :     garde = addll(addmul(p1,y[2]), garde);
     357   936787188 :     z[lzz] = overflow+hiremainder;
     358             :   }
     359    34777983 :   else z[lzz]=0;
     360  2994156050 :   for (j=lz-2, y1=y-j; j>=3; j--)
     361             :   {
     362  2022590879 :     p1 = x[j]; y1++;
     363  2022590879 :     if (p1)
     364             :     {
     365  1956112459 :       (void)mulll(p1,y1[lz+1]);
     366  1956112459 :       garde = addll(addmul(p1,y1[lz]), garde);
     367  7890228937 :       for (i=lzz; i>j; i--)
     368             :       {
     369  5934116478 :         hiremainder += overflow;
     370  5934116478 :         z[i] = addll(addmul(p1,y1[i]), z[i]);
     371             :       }
     372  1956112459 :       z[j] = hiremainder+overflow;
     373             :     }
     374    66478420 :     else z[j]=0;
     375             :   }
     376   971565171 :   p1 = x[2]; y1++;
     377   971565171 :   garde = addll(mulll(p1,y1[lz]), garde);
     378  3965721221 :   for (i=lzz; i>2; i--)
     379             :   {
     380  2994156050 :     hiremainder += overflow;
     381  2994156050 :     z[i] = addll(addmul(p1,y1[i]), z[i]);
     382             :   }
     383   971565171 :   z[2] = hiremainder+overflow;
     384   971565171 :   mulrrz_end(z, z, lz, sz, ez, garde);
     385             : }
     386             : 
     387             : GEN
     388  1812601369 : mulrr(GEN x, GEN y)
     389             : {
     390             :   long flag, ly, lz, sx, sy;
     391             :   GEN z;
     392             : 
     393  1812601369 :   if (x == y) return sqrr(x);
     394  1812456603 :   sx = signe(x); if (!sx) return real_0_bit(expo(x) + expo(y));
     395  1779545020 :   sy = signe(y); if (!sy) return real_0_bit(expo(x) + expo(y));
     396  1767965202 :   if (sy < 0) sx = -sx;
     397  1767965202 :   lz = lg(x);
     398  1767965202 :   ly = lg(y);
     399  1767965202 :   if (lz > ly) { lz = ly; swap(x, y); flag = 1; } else flag = (lz != ly);
     400  1767965202 :   z = cgetr(lz);
     401  1767965202 :   mulrrz_i(z, x,y, lz,flag, sx);
     402  1767965202 :   return z;
     403             : }
     404             : 
     405             : GEN
     406    90408614 : sqrr(GEN x)
     407             : {
     408    90408614 :   long lz, sx = signe(x);
     409             :   GEN z;
     410             : 
     411    90408614 :   if (!sx) return real_0_bit(2*expo(x));
     412    84236006 :   lz = lg(x); z = cgetr(lz);
     413    84236006 :   sqrz_i(z, x, lz);
     414    84236006 :   return z;
     415             : }
     416             : 
     417             : GEN
     418   188849359 : mulir(GEN x, GEN y)
     419             : {
     420   188849359 :   long sx = signe(x), sy;
     421   188849359 :   if (!sx) return mul0r(y);
     422   188690865 :   if (lgefint(x) == 3) {
     423   167964368 :     GEN z = mulur(uel(x,2), y);
     424   167964368 :     if (sx < 0) togglesign(z);
     425   167964368 :     return z;
     426             :   }
     427    20726497 :   sy = signe(y);
     428    20726497 :   if (!sy) return real_0_bit(expi(x) + expo(y));
     429    19632508 :   if (sy < 0) sx = -sx;
     430             :   {
     431    19632508 :     long lz = lg(y), lx = lgefint(x);
     432    19632508 :     GEN hi, z = cgetr(lz);
     433    19632508 :     pari_sp av = avma;
     434    19632508 :     if (lx < (lz>>1) || (lx < lz && lz > MULRR_MULII_LIMIT))
     435             :     { /* size mantissa of x < half size of mantissa z, or lx < lz so large
     436             :        * that mulrr will call mulii anyway: mulii */
     437     1467676 :       x = itor(x, lx);
     438     1467676 :       hi = muliispec_mirror(y+2, x+2, lz-2, lx-2);
     439     1467676 :       mulrrz_end(z, hi, lz, sx, expo(x)+expo(y), hi[lz]);
     440             :     }
     441             :     else /* dubious: complete x with 0s and call mulrr */
     442    18164832 :       mulrrz_i(z, itor(x,lz), y, lz, 0, sx);
     443    19632508 :     avma = av; return z;
     444             :   }
     445             : }
     446             : 
     447             : /* x + y*z, generic. If lgefint(z) <= 3, caller should use faster variants  */
     448             : static GEN
     449    20761698 : addmulii_gen(GEN x, GEN y, GEN z, long lz)
     450             : {
     451    20761698 :   long lx = lgefint(x), ly;
     452             :   pari_sp av;
     453             :   GEN t;
     454    20761698 :   if (lx == 2) return mulii(z,y);
     455    19510969 :   ly = lgefint(y);
     456    19510969 :   if (ly == 2) return icopy(x); /* y = 0, wasteful copy */
     457    19504306 :   av = avma; (void)new_chunk(lx+ly+lz); /*HACK*/
     458    19504306 :   t = mulii(z, y);
     459    19504306 :   avma = av; return addii(t,x);
     460             : }
     461             : /* x + y*z, lgefint(z) == 3 */
     462             : static GEN
     463   223297188 : addmulii_lg3(GEN x, GEN y, GEN z)
     464             : {
     465   223297188 :   long s = signe(z), lx, ly;
     466   223297188 :   ulong w = z[2];
     467             :   pari_sp av;
     468             :   GEN t;
     469   223297188 :   if (w == 1) return (s > 0)? addii(x,y): subii(x,y); /* z = +- 1 */
     470   177234672 :   lx = lgefint(x);
     471   177234672 :   ly = lgefint(y);
     472   177234672 :   if (lx == 2)
     473             :   { /* x = 0 */
     474    24217382 :     if (ly == 2) return gen_0;
     475    20104970 :     t = muluispec(w, y+2, ly-2);
     476    20104970 :     if (signe(y) < 0) s = -s;
     477    20104970 :     setsigne(t, s); return t;
     478             :   }
     479   153017290 :   if (ly == 2) return icopy(x); /* y = 0, wasteful copy */
     480   148310184 :   av = avma; (void)new_chunk(1+lx+ly);/*HACK*/
     481   148310184 :   t = muluispec(w, y+2, ly-2);
     482   148310184 :   if (signe(y) < 0) s = -s;
     483   148310184 :   setsigne(t, s);
     484   148310184 :   avma = av; return addii(x,t);
     485             : }
     486             : /* x + y*z */
     487             : GEN
     488    27608220 : addmulii(GEN x, GEN y, GEN z)
     489             : {
     490    27608220 :   long lz = lgefint(z);
     491    27608220 :   switch(lz)
     492             :   {
     493      259501 :     case 2: return icopy(x); /* z = 0, wasteful copy */
     494    25373993 :     case 3: return addmulii_lg3(x, y, z);
     495     1974726 :     default:return addmulii_gen(x, y, z, lz);
     496             :   }
     497             : }
     498             : /* x + y*z, returns x itself and not a copy when y*z = 0 */
     499             : GEN
     500   781010667 : addmulii_inplace(GEN x, GEN y, GEN z)
     501             : {
     502             :   long lz;
     503   781010667 :   if (lgefint(y) == 2) return x;
     504   216710167 :   lz = lgefint(z);
     505   216710167 :   switch(lz)
     506             :   {
     507           0 :     case 2: return x;
     508   197923195 :     case 3: return addmulii_lg3(x, y, z);
     509    18786972 :     default:return addmulii_gen(x, y, z, lz);
     510             :   }
     511             : }
     512             : 
     513             : /* written by Bruno Haible following an idea of Robert Harley */
     514             : long
     515  2429299956 : vals(ulong z)
     516             : {
     517             :   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};
     518             : #ifdef LONG_IS_64BIT
     519             :   long s;
     520             : #endif
     521             : 
     522  2429299956 :   if (!z) return -1;
     523             : #ifdef LONG_IS_64BIT
     524  2126374485 :   if (! (z&0xffffffff)) { s = 32; z >>=32; } else s = 0;
     525             : #endif
     526  2429299956 :   z |= ~z + 1;
     527  2429299956 :   z += z << 4;
     528  2429299956 :   z += z << 6;
     529  2429299956 :   z ^= z << 16; /* or  z -= z<<16 */
     530             : #ifdef LONG_IS_64BIT
     531  2126374485 :   return s + tab[(z&0xffffffff)>>26];
     532             : #else
     533   302925471 :   return tab[z>>26];
     534             : #endif
     535             : }
     536             : 
     537             : GEN
     538          21 : divsi(long x, GEN y)
     539             : {
     540          21 :   long p1, s = signe(y);
     541             :   LOCAL_HIREMAINDER;
     542             : 
     543          21 :   if (!s) pari_err_INV("divsi",gen_0);
     544          21 :   if (!x || lgefint(y)>3 || ((long)y[2])<0) return gen_0;
     545          21 :   hiremainder=0; p1=divll(labs(x),y[2]);
     546          21 :   if (x<0) { hiremainder = -((long)hiremainder); p1 = -p1; }
     547          21 :   if (s<0) p1 = -p1;
     548          21 :   return stoi(p1);
     549             : }
     550             : 
     551             : GEN
     552     2545745 : divir(GEN x, GEN y)
     553             : {
     554             :   GEN z;
     555     2545745 :   long ly = lg(y), lx = lgefint(x);
     556             :   pari_sp av;
     557             : 
     558     2545745 :   if (ly == 2) pari_err_INV("divir",y);
     559     2545745 :   if (lx == 2) return div0r(y);
     560     2480744 :   if (lx == 3) {
     561     1314316 :     z = divur(x[2], y);
     562     1314316 :     if (signe(x) < 0) togglesign(z);
     563     1314316 :     return z;
     564             :   }
     565     1166428 :   z = cgetr(ly); av = avma;
     566     1166428 :   affrr(divrr(itor(x, ly+1), y), z);
     567     1166428 :   avma = av; return z;
     568             : }
     569             : 
     570             : GEN
     571     1348219 : divur(ulong x, GEN y)
     572             : {
     573             :   pari_sp av;
     574     1348219 :   long ly = lg(y);
     575             :   GEN z;
     576             : 
     577     1348219 :   if (ly == 2) pari_err_INV("divur",y);
     578     1348219 :   if (!x) return div0r(y);
     579     1348219 :   if (ly > INVNEWTON_LIMIT) {
     580         571 :     av = avma; z = invr(y);
     581         571 :     if (x == 1) return z;
     582         571 :     return gerepileuptoleaf(av, mulur(x, z));
     583             :   }
     584     1347648 :   z = cgetr(ly); av = avma;
     585     1347648 :   affrr(divrr(utor(x,ly+1), y), z);
     586     1347648 :   avma = av; return z;
     587             : }
     588             : 
     589             : GEN
     590        7324 : divsr(long x, GEN y)
     591             : {
     592             :   pari_sp av;
     593        7324 :   long ly = lg(y);
     594             :   GEN z;
     595             : 
     596        7324 :   if (ly == 2) pari_err_INV("divsr",y);
     597        7324 :   if (!x) return div0r(y);
     598        7324 :   if (ly > INVNEWTON_LIMIT) {
     599           0 :     av = avma; z = invr(y);
     600           0 :     if (x == 1) return z;
     601           0 :     if (x ==-1) { togglesign(z); return z; }
     602           0 :     return gerepileuptoleaf(av, mulsr(x, z));
     603             :   }
     604        7324 :   z = cgetr(ly); av = avma;
     605        7324 :   affrr(divrr(stor(x,ly+1), y), z);
     606        7324 :   avma = av; return z;
     607             : }
     608             : 
     609             : /* returns 1/y, assume y != 0 */
     610             : static GEN
     611    11299793 : invr_basecase(GEN y)
     612             : {
     613    11299793 :   long ly = lg(y);
     614    11299793 :   GEN z = cgetr(ly);
     615    11299793 :   pari_sp av = avma;
     616    11299793 :   affrr(divrr(real_1(ly+1), y), z);
     617    11299793 :   avma = av; return z;
     618             : }
     619             : /* returns 1/b, Newton iteration */
     620             : GEN
     621    11299793 : invr(GEN b)
     622             : {
     623    11299793 :   const long s = 6;
     624    11299793 :   long i, p, l = lg(b);
     625             :   GEN x, a;
     626             :   ulong mask;
     627             : 
     628    11299793 :   if (l <= maxss(INVNEWTON_LIMIT, (1L<<s) + 2)) {
     629    11296598 :     if (l == 2) pari_err_INV("invr",b);
     630    11296598 :     return invr_basecase(b);
     631             :   }
     632        3195 :   mask = quadratic_prec_mask(l-2);
     633        3195 :   for(i=0, p=1; i<s; i++) { p <<= 1; if (mask & 1) p--; mask >>= 1; }
     634        3195 :   x = cgetr(l);
     635        3195 :   a = rcopy(b); a[1] = _evalexpo(0) | evalsigne(1);
     636        3195 :   affrr(invr_basecase(rtor(a, p+2)), x);
     637       11328 :   while (mask > 1)
     638             :   {
     639        4938 :     p <<= 1; if (mask & 1) p--;
     640        4938 :     mask >>= 1;
     641        4938 :     setlg(a, p + 2);
     642        4938 :     setlg(x, p + 2);
     643             :     /* TODO: mulrr(a,x) should be a half product (the higher half is known).
     644             :      * mulrr(x, ) already is */
     645        4938 :     affrr(addrr(x, mulrr(x, subsr(1, mulrr(a,x)))), x);
     646        4938 :     avma = (pari_sp)a;
     647             :   }
     648        3195 :   x[1] = (b[1] & SIGNBITS) | evalexpo(expo(x)-expo(b));
     649        3195 :   avma = (pari_sp)x; return x;
     650             : }
     651             : 
     652             : GEN
     653  1215546368 : modii(GEN x, GEN y)
     654             : {
     655  1215546368 :   switch(signe(x))
     656             :   {
     657   156941249 :     case 0: return gen_0;
     658   732165878 :     case 1: return remii(x,y);
     659             :     default:
     660             :     {
     661   326439241 :       pari_sp av = avma;
     662   326439241 :       (void)new_chunk(lgefint(y));
     663   326433190 :       x = remii(x,y); avma=av;
     664   326442932 :       if (x==gen_0) return x;
     665   302927688 :       return subiispec(y+2,x+2,lgefint(y)-2,lgefint(x)-2);
     666             :     }
     667             :   }
     668             : }
     669             : 
     670             : void
     671      229926 : modiiz(GEN x, GEN y, GEN z)
     672             : {
     673      229926 :   const pari_sp av = avma;
     674      229926 :   affii(modii(x,y),z); avma=av;
     675      229926 : }
     676             : 
     677             : GEN
     678     3506345 : divrs(GEN x, long y)
     679             : {
     680             :   GEN z;
     681     3506345 :   if (y < 0)
     682             :   {
     683      476557 :     z = divru(x, (ulong)-y);
     684      476557 :     togglesign(z);
     685             :   }
     686             :   else
     687     3029788 :     z = divru(x, (ulong)y);
     688     3506345 :   return z;
     689             : }
     690             : 
     691             : GEN
     692   545503638 : divru(GEN x, ulong y)
     693             : {
     694   545503638 :   long i, lx, sh, e, s = signe(x);
     695             :   ulong garde;
     696             :   GEN z;
     697             :   LOCAL_HIREMAINDER;
     698             : 
     699   545503638 :   if (!y) pari_err_INV("divru",gen_0);
     700   545503638 :   if (!s) return real_0_bit(expo(x) - expu(y));
     701   545440757 :   if (!(y & (y-1))) /* power of 2 */
     702             :   {
     703    37893714 :     if (y == 1) return rcopy(x);
     704    35879126 :     return shiftr(x, -expu(y));
     705             :   }
     706   507547043 :   e = expo(x);
     707   507547043 :   lx = lg(x);
     708   507547043 :   z = cgetr(lx);
     709   507547043 :   if (lx == 3)
     710             :   {
     711   314354955 :     if (y <= uel(x,2))
     712             :     {
     713   314354895 :       hiremainder = 0;
     714   314354895 :       z[2] = divll(x[2],y);
     715             :       /* we may have hiremainder != 0 ==> garde */
     716   314354895 :       garde = divll(0,y);
     717             :     }
     718             :     else
     719             :     {
     720          60 :       hiremainder = x[2];
     721          60 :       z[2] = divll(0,y);
     722          60 :       garde = hiremainder;
     723          60 :       e -= BITS_IN_LONG;
     724             :     }
     725             :   }
     726             :   else
     727             :   {
     728   193192088 :     ulong yp = get_Fl_red(y);
     729   193192088 :     if (y <= uel(x,2))
     730             :     {
     731   193189279 :       hiremainder = 0;
     732   193189279 :       for (i=2; i<lx; i++) z[i] = divll_pre(x[i],y,yp);
     733             :       /* we may have hiremainder != 0 ==> garde */
     734   193189279 :       garde = divll_pre(0,y,yp);
     735             :     }
     736             :     else
     737             :     {
     738        2809 :       long l = lx-1;
     739        2809 :       hiremainder = x[2];
     740        2809 :       for (i=2; i<l; i++) z[i] = divll_pre(x[i+1],y,yp);
     741        2809 :       z[i] = divll_pre(0,y,yp);
     742        2809 :       garde = hiremainder;
     743        2809 :       e -= BITS_IN_LONG;
     744             :     }
     745             :   }
     746   507547043 :   sh=bfffo(z[2]); /* z[2] != 0 */
     747   507547043 :   if (sh) shift_left(z,z, 2,lx-1, garde,sh);
     748   507547043 :   z[1] = evalsigne(s) | evalexpo(e-sh);
     749   507547043 :   if ((garde << sh) & HIGHBIT) roundr_up_ip(z, lx);
     750   507547043 :   return z;
     751             : }
     752             : 
     753             : GEN
     754    45816619 : truedvmdii(GEN x, GEN y, GEN *z)
     755             : {
     756             :   pari_sp av;
     757             :   GEN r, q, *gptr[2];
     758    45816619 :   if (!is_bigint(y)) return truedvmdis(x, itos(y), z);
     759      893050 :   if (z == ONLY_REM) return modii(x,y);
     760             : 
     761      893050 :   av = avma;
     762      893050 :   q = dvmdii(x,y,&r); /* assume that r is last on stack */
     763      893050 :   switch(signe(r))
     764             :   {
     765             :     case 0:
     766      201182 :       if (z) *z = gen_0;
     767      201182 :       return q;
     768             :     case 1:
     769      564428 :       if (z) *z = r; else cgiv(r);
     770      564428 :       return q;
     771      127440 :     case -1: break;
     772             :   }
     773      127440 :   q = addis(q, -signe(y));
     774      127440 :   if (!z) return gerepileuptoint(av, q);
     775             : 
     776       19563 :   *z = subiispec(y+2,r+2, lgefint(y)-2,lgefint(r)-2);
     777       19563 :   gptr[0]=&q; gptr[1]=z; gerepilemanysp(av,(pari_sp)r,gptr,2);
     778       19563 :   return q;
     779             : }
     780             : GEN
     781    44941128 : truedvmdis(GEN x, long y, GEN *z)
     782             : {
     783    44941128 :   pari_sp av = avma;
     784             :   long r;
     785             :   GEN q;
     786             : 
     787    44941128 :   if (z == ONLY_REM) return modis(x, y);
     788    44941128 :   q = divis_rem(x,y,&r);
     789             : 
     790    44941128 :   if (r >= 0)
     791             :   {
     792    40349386 :     if (z) *z = utoi(r);
     793    40349386 :     return q;
     794             :   }
     795     4591742 :   q = gerepileuptoint(av, addis(q, (y < 0)? 1: -1));
     796     4591742 :   if (z) *z = utoi(r + labs(y));
     797     4591742 :   return q;
     798             : }
     799             : GEN
     800     6084679 : truedvmdsi(long x, GEN y, GEN *z)
     801             : {
     802             :   long q, r;
     803     6084679 :   if (z == ONLY_REM) return modsi(x, y);
     804     6084679 :   q = sdivsi_rem(x,y,&r);
     805     6084679 :   if (r >= 0) {
     806     6084630 :     if (z) *z = utoi(r);
     807     6084630 :     return stoi(q);
     808             :   }
     809          49 :   q = q - signe(y);
     810          49 :   if (!z) return stoi(q);
     811             : 
     812           0 :   *z = subiuspec(y+2,(ulong)-r, lgefint(y)-2);
     813           0 :   return stoi(q);
     814             : }
     815             : 
     816             : /* 2^n = shifti(gen_1, n) */
     817             : GEN
     818    42710477 : int2n(long n) {
     819             :   long i, m, l;
     820             :   GEN z;
     821    42710477 :   if (n < 0) return gen_0;
     822    42709675 :   if (n == 0) return gen_1;
     823             : 
     824    42233708 :   l = dvmdsBIL(n, &m) + 3;
     825    42233440 :   z = cgetipos(l);
     826    42233538 :   for (i = 2; i < l; i++) z[i] = 0;
     827    42233538 :   *int_MSW(z) = 1UL << m; return z;
     828             : }
     829             : /* To avoid problems when 2^(BIL-1) < n. Overflow cleanly, where int2n
     830             :  * returns gen_0 */
     831             : GEN
     832    10179367 : int2u(ulong n) {
     833             :   ulong i, m, l;
     834             :   GEN z;
     835    10179367 :   if (n == 0) return gen_1;
     836             : 
     837    10179366 :   l = dvmduBIL(n, &m) + 3;
     838    10179361 :   z = cgetipos(l);
     839    10179342 :   for (i = 2; i < l; i++) z[i] = 0;
     840    10179342 :   *int_MSW(z) = 1UL << m; return z;
     841             : }
     842             : /* 2^n - 1 */
     843             : GEN
     844         476 : int2um1(ulong n) {
     845             :   ulong i, m, l;
     846             :   GEN z;
     847         476 :   if (n == 0) return gen_0;
     848             : 
     849         476 :   l = dvmduBIL(n, &m);
     850         476 :   l += m? 3: 2;
     851         476 :   z = cgetipos(l);
     852         476 :   for (i = 2; i < l; i++) z[i] = ~0UL;
     853         476 :   if (m) *int_MSW(z) = (1UL << m) - 1;
     854         476 :   return z;
     855             : }
     856             : 
     857             : GEN
     858   838961824 : shifti(GEN x, long n)
     859             : {
     860   838961824 :   long s = signe(x);
     861             :   GEN y;
     862             : 
     863   838961824 :   if(s == 0) return gen_0;
     864   763235116 :   y = shiftispec(x + 2, lgefint(x) - 2, n);
     865   763206938 :   if (signe(y)) setsigne(y, s);
     866   763206938 :   return y;
     867             : }
     868             : 
     869             : /* actual operations will take place on a+2 and b+2: we strip the codewords */
     870             : GEN
     871  7120344852 : mulii(GEN a,GEN b)
     872             : {
     873             :   long sa,sb;
     874             :   GEN z;
     875             : 
     876  7120344852 :   sa=signe(a); if (!sa) return gen_0;
     877  5434578131 :   sb=signe(b); if (!sb) return gen_0;
     878  3893795613 :   if (sb<0) sa = -sa;
     879  3893795613 :   z = muliispec(a+2,b+2, lgefint(a)-2,lgefint(b)-2);
     880  3894428260 :   setsigne(z,sa); return z;
     881             : }
     882             : 
     883             : GEN
     884  1309127006 : sqri(GEN a) { return sqrispec(a+2, lgefint(a)-2); }
     885             : 
     886             : /* sqrt()'s result may be off by 1 when a is not representable exactly as a
     887             :  * double [64bit machine] */
     888             : ulong
     889    67185384 : usqrt(ulong a)
     890             : {
     891    67185384 :   ulong x = (ulong)sqrt((double)a);
     892             : #ifdef LONG_IS_64BIT
     893    57556993 :   if (x > LOWMASK || x*x > a) x--;
     894             : #endif
     895    67185384 :   return x;
     896             : }
     897             : 
     898             : /********************************************************************/
     899             : /**                                                                **/
     900             : /**              EXPONENT / CONVERSION t_REAL --> double           **/
     901             : /**                                                                **/
     902             : /********************************************************************/
     903             : 
     904             : #ifdef LONG_IS_64BIT
     905             : long
     906           0 : dblexpo(double x)
     907             : {
     908             :   union { double f; ulong i; } fi;
     909           0 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     910           0 :   const int exp_mid = 0x3ff;/* exponent bias */
     911             : 
     912           0 :   if (x==0.) return -exp_mid;
     913           0 :   fi.f = x;
     914           0 :   return ((fi.i & (HIGHBIT-1)) >> mant_len) - exp_mid;
     915             : }
     916             : 
     917             : ulong
     918           0 : dblmantissa(double x)
     919             : {
     920             :   union { double f; ulong i; } fi;
     921           0 :   const int expo_len = 11; /* number of bits of exponent */
     922             : 
     923           0 :   if (x==0.) return 0;
     924           0 :   fi.f = x;
     925           0 :   return (fi.i << expo_len) | HIGHBIT;
     926             : }
     927             : 
     928             : GEN
     929     5766452 : dbltor(double x)
     930             : {
     931             :   GEN z;
     932             :   long e;
     933             :   union { double f; ulong i; } fi;
     934     5766452 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     935     5766452 :   const int exp_mid = 0x3ff;/* exponent bias */
     936     5766452 :   const int expo_len = 11; /* number of bits of exponent */
     937             : 
     938     5766452 :   if (x==0.) return real_0_bit(-exp_mid);
     939     5754812 :   fi.f = x; z = cgetr(DEFAULTPREC);
     940             :   {
     941     5754812 :     const ulong a = fi.i;
     942             :     ulong A;
     943     5754812 :     e = ((a & (HIGHBIT-1)) >> mant_len) - exp_mid;
     944     5754812 :     if (e == exp_mid+1) pari_err_OVERFLOW("dbltor [NaN or Infinity]");
     945     5754812 :     A = a << expo_len;
     946     5754812 :     if (e == -exp_mid)
     947             :     { /* unnormalized values */
     948           0 :       int sh = bfffo(A);
     949           0 :       e -= sh-1;
     950           0 :       z[2] = A << sh;
     951             :     }
     952             :     else
     953     5754812 :       z[2] = HIGHBIT | A;
     954     5754812 :     z[1] = _evalexpo(e) | evalsigne(x<0? -1: 1);
     955             :   }
     956     5754812 :   return z;
     957             : }
     958             : 
     959             : double
     960    26041914 : rtodbl(GEN x)
     961             : {
     962    26041914 :   long ex,s=signe(x);
     963             :   ulong a;
     964             :   union { double f; ulong i; } fi;
     965    26041914 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     966    26041914 :   const int exp_mid = 0x3ff;/* exponent bias */
     967    26041914 :   const int expo_len = 11; /* number of bits of exponent */
     968             : 
     969    26041914 :   if (!s || (ex=expo(x)) < - exp_mid) return 0.0;
     970             : 
     971             :   /* start by rounding to closest */
     972    25556593 :   a = (x[2] & (HIGHBIT-1)) + 0x400;
     973    25556593 :   if (a & HIGHBIT) { ex++; a=0; }
     974    25556593 :   if (ex >= exp_mid) pari_err_OVERFLOW("t_REAL->double conversion");
     975    25556593 :   fi.i = ((ex + exp_mid) << mant_len) | (a >> expo_len);
     976    25556593 :   if (s<0) fi.i |= HIGHBIT;
     977    25556593 :   return fi.f;
     978             : }
     979             : 
     980             : #else /* LONG_IS_64BIT */
     981             : 
     982             : #if   PARI_DOUBLE_FORMAT == 1
     983             : #  define INDEX0 1
     984             : #  define INDEX1 0
     985             : #elif PARI_DOUBLE_FORMAT == 0
     986             : #  define INDEX0 0
     987             : #  define INDEX1 1
     988             : #endif
     989             : 
     990             : long
     991           0 : dblexpo(double x)
     992             : {
     993             :   union { double f; ulong i[2]; } fi;
     994           0 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     995           0 :   const int exp_mid = 0x3ff;/* exponent bias */
     996           0 :   const int shift = mant_len-32;
     997             : 
     998           0 :   if (x==0.) return -exp_mid;
     999           0 :   fi.f = x;
    1000             :   {
    1001           0 :     const ulong a = fi.i[INDEX0];
    1002           0 :     return ((a & (HIGHBIT-1)) >> shift) - exp_mid;
    1003             :   }
    1004             : }
    1005             : 
    1006             : ulong
    1007           0 : dblmantissa(double x)
    1008             : {
    1009             :   union { double f; ulong i[2]; } fi;
    1010           0 :   const int expo_len = 11; /* number of bits of exponent */
    1011             : 
    1012           0 :   if (x==0.) return 0;
    1013           0 :   fi.f = x;
    1014             :   {
    1015           0 :     const ulong a = fi.i[INDEX0];
    1016           0 :     const ulong b = fi.i[INDEX1];
    1017           0 :     return HIGHBIT | b >> (BITS_IN_LONG-expo_len) | (a << expo_len);
    1018             :   }
    1019             : }
    1020             : 
    1021             : GEN
    1022      959735 : dbltor(double x)
    1023             : {
    1024             :   GEN z;
    1025             :   long e;
    1026             :   union { double f; ulong i[2]; } fi;
    1027      959735 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
    1028      959735 :   const int exp_mid = 0x3ff;/* exponent bias */
    1029      959735 :   const int expo_len = 11; /* number of bits of exponent */
    1030      959735 :   const int shift = mant_len-32;
    1031             : 
    1032      959735 :   if (x==0.) return real_0_bit(-exp_mid);
    1033      958415 :   fi.f = x; z = cgetr(DEFAULTPREC);
    1034             :   {
    1035      958415 :     const ulong a = fi.i[INDEX0];
    1036      958415 :     const ulong b = fi.i[INDEX1];
    1037             :     ulong A, B;
    1038      958415 :     e = ((a & (HIGHBIT-1)) >> shift) - exp_mid;
    1039      958415 :     if (e == exp_mid+1) pari_err_OVERFLOW("dbltor [NaN or Infinity]");
    1040      958415 :     A = b >> (BITS_IN_LONG-expo_len) | (a << expo_len);
    1041      958415 :     B = b << expo_len;
    1042      958415 :     if (e == -exp_mid)
    1043             :     { /* unnormalized values */
    1044             :       int sh;
    1045           0 :       if (A)
    1046             :       {
    1047           0 :         sh = bfffo(A);
    1048           0 :         e -= sh-1;
    1049           0 :         z[2] = (A << sh) | (B >> (32-sh));
    1050           0 :         z[3] = B << sh;
    1051             :       }
    1052             :       else
    1053             :       {
    1054           0 :         sh = bfffo(B); /* B != 0 */
    1055           0 :         e -= sh-1 + 32;
    1056           0 :         z[2] = B << sh;
    1057           0 :         z[3] = 0;
    1058             :       }
    1059             :     }
    1060             :     else
    1061             :     {
    1062      958415 :       z[3] = B;
    1063      958415 :       z[2] = HIGHBIT | A;
    1064             :     }
    1065      958415 :     z[1] = _evalexpo(e) | evalsigne(x<0? -1: 1);
    1066             :   }
    1067      958415 :   return z;
    1068             : }
    1069             : 
    1070             : double
    1071     4231372 : rtodbl(GEN x)
    1072             : {
    1073     4231372 :   long ex,s=signe(x),lx=lg(x);
    1074             :   ulong a,b,k;
    1075             :   union { double f; ulong i[2]; } fi;
    1076     4231372 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
    1077     4231372 :   const int exp_mid = 0x3ff;/* exponent bias */
    1078     4231372 :   const int expo_len = 11; /* number of bits of exponent */
    1079     4231372 :   const int shift = mant_len-32;
    1080             : 
    1081     4231372 :   if (!s || (ex=expo(x)) < - exp_mid) return 0.0;
    1082             : 
    1083             :   /* start by rounding to closest */
    1084     4150540 :   a = x[2] & (HIGHBIT-1);
    1085     4150540 :   if (lx > 3)
    1086             :   {
    1087     3961312 :     b = x[3] + 0x400UL; if (b < 0x400UL) a++;
    1088     3961312 :     if (a & HIGHBIT) { ex++; a=0; }
    1089             :   }
    1090      189228 :   else b = 0;
    1091     4150540 :   if (ex >= exp_mid) pari_err_OVERFLOW("t_REAL->double conversion");
    1092     4150540 :   ex += exp_mid;
    1093     4150540 :   k = (a >> expo_len) | (ex << shift);
    1094     4150540 :   if (s<0) k |= HIGHBIT;
    1095     4150540 :   fi.i[INDEX0] = k;
    1096     4150540 :   fi.i[INDEX1] = (a << (BITS_IN_LONG-expo_len)) | (b >> expo_len);
    1097     4150540 :   return fi.f;
    1098             : }
    1099             : #endif /* LONG_IS_64BIT */
    1100             : 

Generated by: LCOV version 1.11