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.12.0 lcov report (development 23346-507fdfd83) Lines: 525 569 92.3 %
Date: 2018-12-13 05:42:04 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    23098589 : invmod2BIL(ulong b)
      18             : {
      19             :   static int tab[] = { 0, 0, 0, 8, 0, 8, 0, 0 };
      20    23098589 :   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    23098589 :   x = x*(2-b*x); /* one more pass necessary */
      25             : #endif
      26    23098589 :   x = x*(2-b*x);
      27    23098589 :   x = x*(2-b*x); return x*(2-b*x);
      28             : }
      29             : 
      30             : void
      31  1141441302 : affrr(GEN x, GEN y)
      32             : {
      33  1141441302 :   long i, lx, ly = lg(y);
      34  1141441302 :   if (!signe(x))
      35             :   {
      36     7380303 :     y[1] = evalexpo(minss(expo(x), -bit_accuracy(ly)));
      37     7380303 :     return;
      38             :   }
      39  1134060999 :   y[1] = x[1]; lx = lg(x);
      40  1134060999 :   if (lx <= ly)
      41             :   {
      42  1055264065 :     for (i=2; i<lx; i++) y[i]=x[i];
      43  1055264065 :     for (   ; i<ly; i++) y[i]=0;
      44  1055264065 :     return;
      45             :   }
      46    78796934 :   for (i=2; i<ly; i++) y[i]=x[i];
      47             :   /* lx > ly: round properly */
      48    78796934 :   if (x[ly] & HIGHBIT) roundr_up_ip(y, ly);
      49             : }
      50             : 
      51             : GEN
      52     8456540 : trunc2nr(GEN x, long n)
      53             : {
      54             :   long ex;
      55     8456540 :   if (!signe(x)) return gen_0;
      56     8356244 :   ex = expo(x) + n; if (ex < 0) return gen_0;
      57     7934819 :   return mantissa2nr(x, ex - bit_prec(x) + 1);
      58             : }
      59             : 
      60             : /* x a t_REAL, x = i/2^e, i a t_INT */
      61             : GEN
      62    20256238 : mantissa_real(GEN x, long *e)
      63             : {
      64    20256238 :   *e = bit_prec(x)-1-expo(x);
      65    20256238 :   return mantissa2nr(x, 0);
      66             : }
      67             : 
      68             : GEN
      69   924371258 : mului(ulong x, GEN y)
      70             : {
      71   924371258 :   long s = signe(y);
      72             :   GEN z;
      73             : 
      74   924371258 :   if (!s || !x) return gen_0;
      75   878579871 :   z = muluispec(x, y+2, lgefint(y)-2);
      76   876580884 :   setsigne(z,s); return z;
      77             : }
      78             : 
      79             : GEN
      80   235871170 : mulsi(long x, GEN y)
      81             : {
      82   235871170 :   long s = signe(y);
      83             :   GEN z;
      84             : 
      85   235871170 :   if (!s || !x) return gen_0;
      86   178288740 :   if (x<0) { s = -s; x = -x; }
      87   178288740 :   z = muluispec((ulong)x, y+2, lgefint(y)-2);
      88   178287994 :   setsigne(z,s); return z;
      89             : }
      90             : 
      91             : GEN
      92   185206184 : mulss(long x, long y)
      93             : {
      94             :   long p1;
      95             :   LOCAL_HIREMAINDER;
      96             : 
      97   185206184 :   if (!x || !y) return gen_0;
      98   185073842 :   if (x<0) {
      99       79562 :     x = -x;
     100       79562 :     if (y<0) { y = -y; p1 = mulll(x,y); return uutoi(hiremainder, p1); }
     101       79562 :     p1 = mulll(x,y); return uutoineg(hiremainder, p1);
     102             :   } else {
     103   184994280 :     if (y<0) { y = -y; p1 = mulll(x,y); return uutoineg(hiremainder, p1); }
     104   184992339 :     p1 = mulll(x,y); return uutoi(hiremainder, p1);
     105             :   }
     106             : }
     107             : GEN
     108      359955 : sqrs(long x)
     109             : {
     110             :   long p1;
     111             :   LOCAL_HIREMAINDER;
     112             : 
     113      359955 :   if (!x) return gen_0;
     114      359633 :   if (x<0) x = -x;
     115      359633 :   p1 = mulll(x,x); return uutoi(hiremainder, p1);
     116             : }
     117             : GEN
     118  1953497064 : muluu(ulong x, ulong y)
     119             : {
     120             :   long p1;
     121             :   LOCAL_HIREMAINDER;
     122             : 
     123  1953497064 :   if (!x || !y) return gen_0;
     124  1953449632 :   p1 = mulll(x,y); return uutoi(hiremainder, p1);
     125             : }
     126             : GEN
     127   698851851 : sqru(ulong x)
     128             : {
     129             :   long p1;
     130             :   LOCAL_HIREMAINDER;
     131             : 
     132   698851851 :   if (!x) return gen_0;
     133   698571284 :   p1 = mulll(x,x); return uutoi(hiremainder, p1);
     134             : }
     135             : 
     136             : /* assume x > 1, y != 0. Return u * y with sign s */
     137             : static GEN
     138   564569401 : mulur_2(ulong x, GEN y, long s)
     139             : {
     140   564569401 :   long m, sh, i, lx = lg(y), e = expo(y);
     141   564569401 :   GEN z = cgetr(lx);
     142             :   ulong garde;
     143             :   LOCAL_HIREMAINDER;
     144             : 
     145   564569401 :   y--; garde = mulll(x,y[lx]);
     146   564569401 :   for (i=lx-1; i>=3; i--) z[i]=addmul(x,y[i]);
     147   564569401 :   z[2]=hiremainder; /* != 0 since y normalized and |x| > 1 */
     148   564569401 :   sh = bfffo(hiremainder); m = BITS_IN_LONG-sh;
     149   564569401 :   if (sh) shift_left(z,z, 2,lx-1, garde,sh);
     150   564569401 :   z[1] = evalsigne(s) | evalexpo(m+e);
     151   564569401 :   if ((garde << sh) & HIGHBIT) roundr_up_ip(z, lx);
     152   564569401 :   return z;
     153             : }
     154             : 
     155             : INLINE GEN
     156     1205263 : mul0r(GEN x)
     157             : {
     158     1205263 :   long l = lg(x), e = expo(x);
     159     1205263 :   e = (l > 2)? -prec2nbits(l) + e: (e < 0? 2*e: 0);
     160     1205263 :   return real_0_bit(e);
     161             : }
     162             : /* lg(x) > 2 */
     163             : INLINE GEN
     164       64784 : div0r(GEN x) {
     165       64784 :   long l = lg(x), e = expo(x);
     166       64784 :   return real_0_bit(-prec2nbits(l) - e);
     167             : }
     168             : 
     169             : GEN
     170    58993643 : mulsr(long x, GEN y)
     171             : {
     172             :   long s;
     173             : 
     174    58993643 :   if (!x) return mul0r(y);
     175    57976250 :   s = signe(y);
     176    57976250 :   if (!s)
     177             :   {
     178      240744 :     if (x < 0) x = -x;
     179      240744 :     return real_0_bit( expo(y) + expu(x) );
     180             :   }
     181    57735506 :   if (x==1)  return rcopy(y);
     182    54165491 :   if (x==-1) return negr(y);
     183    52892186 :   if (x < 0)
     184    26831662 :     return mulur_2((ulong)-x, y, -s);
     185             :   else
     186    26060524 :     return mulur_2((ulong)x, y, s);
     187             : }
     188             : 
     189             : GEN
     190   585182024 : mulur(ulong x, GEN y)
     191             : {
     192             :   long s;
     193             : 
     194   585182024 :   if (!x) return mul0r(y);
     195   585182024 :   s = signe(y);
     196   585182024 :   if (!s) return real_0_bit( expo(y) + expu(x) );
     197   581590958 :   if (x==1) return rcopy(y);
     198   511677215 :   return mulur_2(x, y, s);
     199             : }
     200             : 
     201             : INLINE void
     202  1295914511 : mulrrz_end(GEN z, GEN hi, long lz, long sz, long ez, ulong garde)
     203             : {
     204             :   long i;
     205  1295914511 :   if (hi[2] < 0)
     206             :   {
     207   563621578 :     if (z != hi)
     208    11720759 :       for (i=2; i<lz ; i++) z[i] = hi[i];
     209   563621578 :     ez++;
     210             :   }
     211             :   else
     212             :   {
     213   732292933 :     shift_left(z,hi,2,lz-1, garde, 1);
     214   732292933 :     garde <<= 1;
     215             :   }
     216  1295914511 :   if (garde & HIGHBIT)
     217             :   { /* round to nearest */
     218   631665685 :     i = lz; do ((ulong*)z)[--i]++; while (i>1 && z[i]==0);
     219   631665685 :     if (i == 1) { z[2] = (long)HIGHBIT; ez++; }
     220             :   }
     221  1295914511 :   z[1] = evalsigne(sz)|evalexpo(ez);
     222  1295914511 : }
     223             : /* mulrrz_end for lz = 3, minor simplifications. z[2]=hiremainder from mulll */
     224             : INLINE void
     225   833051325 : mulrrz_3end(GEN z, long sz, long ez, ulong garde)
     226             : {
     227   833051325 :   if (z[2] < 0)
     228             :   { /* z2 < (2^BIL-1)^2 / 2^BIL, hence z2+1 != 0 */
     229   439972368 :     if (garde & HIGHBIT) z[2]++; /* round properly */
     230   439972368 :     ez++;
     231             :   }
     232             :   else
     233             :   {
     234   393078957 :     uel(z,2) = (uel(z,2)<<1) | (garde>>(BITS_IN_LONG-1));
     235   393078957 :     if (garde & (1UL<<(BITS_IN_LONG-2)))
     236             :     {
     237   178166333 :       uel(z,2)++; /* round properly, z2+1 can overflow */
     238   178166333 :       if (!uel(z,2)) { uel(z,2) = HIGHBIT; ez++; }
     239             :     }
     240             :   }
     241   833051325 :   z[1] = evalsigne(sz)|evalexpo(ez);
     242   833051325 : }
     243             : 
     244             : /* set z <-- x^2 != 0, floating point multiplication.
     245             :  * lz = lg(z) = lg(x) */
     246             : INLINE void
     247    98335693 : sqrz_i(GEN z, GEN x, long lz)
     248             : {
     249    98335693 :   long ez = 2*expo(x);
     250             :   long i, j, lzz, p1;
     251             :   ulong garde;
     252             :   GEN x1;
     253             :   LOCAL_HIREMAINDER;
     254             :   LOCAL_OVERFLOW;
     255             : 
     256    98335693 :   if (lz > SQRR_SQRI_LIMIT)
     257             :   {
     258     7178733 :     pari_sp av = avma;
     259     7178733 :     GEN hi = sqrispec_mirror(x+2, lz-2);
     260     7178733 :     mulrrz_end(z, hi, lz, 1, ez, hi[lz]);
     261     7178733 :     set_avma(av); return;
     262             :   }
     263    91156960 :   if (lz == 3)
     264             :   {
     265    18474380 :     garde = mulll(x[2],x[2]);
     266    18474380 :     z[2] = hiremainder;
     267    18474380 :     mulrrz_3end(z, 1, ez, garde);
     268    18474380 :     return;
     269             :   }
     270             : 
     271    72682580 :   lzz = lz-1; p1 = x[lzz];
     272    72682580 :   if (p1)
     273             :   {
     274    64543633 :     (void)mulll(p1,x[3]);
     275    64543633 :     garde = addmul(p1,x[2]);
     276    64543633 :     z[lzz] = hiremainder;
     277             :   }
     278             :   else
     279             :   {
     280     8138947 :     garde = 0;
     281     8138947 :     z[lzz] = 0;
     282             :   }
     283   240858891 :   for (j=lz-2, x1=x-j; j>=3; j--)
     284             :   {
     285   168176311 :     p1 = x[j]; x1++;
     286   168176311 :     if (p1)
     287             :     {
     288   160610234 :       (void)mulll(p1,x1[lz+1]);
     289   160610234 :       garde = addll(addmul(p1,x1[lz]), garde);
     290   651633170 :       for (i=lzz; i>j; i--)
     291             :       {
     292   491022936 :         hiremainder += overflow;
     293   491022936 :         z[i] = addll(addmul(p1,x1[i]), z[i]);
     294             :       }
     295   160610234 :       z[j] = hiremainder+overflow;
     296             :     }
     297     7566077 :     else z[j]=0;
     298             :   }
     299    72682580 :   p1 = x[2]; x1++;
     300    72682580 :   garde = addll(mulll(p1,x1[lz]), garde);
     301   313541471 :   for (i=lzz; i>2; i--)
     302             :   {
     303   240858891 :     hiremainder += overflow;
     304   240858891 :     z[i] = addll(addmul(p1,x1[i]), z[i]);
     305             :   }
     306    72682580 :   z[2] = hiremainder+overflow;
     307    72682580 :   mulrrz_end(z, z, lz, 1, ez, garde);
     308             : }
     309             : 
     310             : /* lz "large" = lg(y) = lg(z), lg(x) > lz if flag = 1 and >= if flag = 0 */
     311             : INLINE void
     312    10949002 : mulrrz_int(GEN z, GEN x, GEN y, long lz, long flag, long sz)
     313             : {
     314    10949002 :   pari_sp av = avma;
     315    10949002 :   GEN hi = muliispec_mirror(y+2, x+2, lz+flag-2, lz-2);
     316    10949002 :   mulrrz_end(z, hi, lz, sz, expo(x)+expo(y), hi[lz]);
     317    10949002 :   set_avma(av);
     318    10949002 : }
     319             : 
     320             : /* lz = 3 */
     321             : INLINE void
     322   814576945 : mulrrz_3(GEN z, GEN x, GEN y, long flag, long sz)
     323             : {
     324             :   ulong garde;
     325             :   LOCAL_HIREMAINDER;
     326   814576945 :   if (flag)
     327             :   {
     328    35206587 :     (void)mulll(x[2],y[3]);
     329    35206587 :     garde = addmul(x[2],y[2]);
     330             :   }
     331             :   else
     332   779370358 :     garde = mulll(x[2],y[2]);
     333   814576945 :   z[2] = hiremainder;
     334   814576945 :   mulrrz_3end(z, sz, expo(x)+expo(y), garde);
     335   814576945 : }
     336             : 
     337             : /* set z <-- x*y, floating point multiplication. Trailing 0s for x are
     338             :  * treated efficiently (important application: mulir).
     339             :  * lz = lg(z) = lg(x) <= ly <= lg(y), sz = signe(z). flag = lg(x) < lg(y) */
     340             : INLINE void
     341  2025660755 : mulrrz_i(GEN z, GEN x, GEN y, long lz, long flag, long sz)
     342             : {
     343             :   long ez, i, j, lzz, p1;
     344             :   ulong garde;
     345             :   GEN y1;
     346             :   LOCAL_HIREMAINDER;
     347             :   LOCAL_OVERFLOW;
     348             : 
     349  2025660755 :   if (x == y) { sqrz_i(z,x,lz); return; }
     350  2025660755 :   if (lz > MULRR_MULII_LIMIT) { mulrrz_int(z,x,y,lz,flag,sz); return; }
     351  2014711753 :   if (lz == 3) { mulrrz_3(z,x,y,flag,sz); return; }
     352  1200134808 :   ez = expo(x) + expo(y);
     353  1200134808 :   if (flag) { (void)mulll(x[2],y[lz]); garde = hiremainder; } else garde = 0;
     354  1200134808 :   lzz=lz-1; p1=x[lzz];
     355  1200134808 :   if (p1)
     356             :   {
     357  1145538779 :     (void)mulll(p1,y[3]);
     358  1145538779 :     garde = addll(addmul(p1,y[2]), garde);
     359  1145538779 :     z[lzz] = overflow+hiremainder;
     360             :   }
     361    54596029 :   else z[lzz]=0;
     362  4366679796 :   for (j=lz-2, y1=y-j; j>=3; j--)
     363             :   {
     364  3166544988 :     p1 = x[j]; y1++;
     365  3166544988 :     if (p1)
     366             :     {
     367  3060147398 :       (void)mulll(p1,y1[lz+1]);
     368  3060147398 :       garde = addll(addmul(p1,y1[lz]), garde);
     369 17737416382 :       for (i=lzz; i>j; i--)
     370             :       {
     371 14677268984 :         hiremainder += overflow;
     372 14677268984 :         z[i] = addll(addmul(p1,y1[i]), z[i]);
     373             :       }
     374  3060147398 :       z[j] = hiremainder+overflow;
     375             :     }
     376   106397590 :     else z[j]=0;
     377             :   }
     378  1200134808 :   p1 = x[2]; y1++;
     379  1200134808 :   garde = addll(mulll(p1,y1[lz]), garde);
     380  5566814604 :   for (i=lzz; i>2; i--)
     381             :   {
     382  4366679796 :     hiremainder += overflow;
     383  4366679796 :     z[i] = addll(addmul(p1,y1[i]), z[i]);
     384             :   }
     385  1200134808 :   z[2] = hiremainder+overflow;
     386  1200134808 :   mulrrz_end(z, z, lz, sz, ez, garde);
     387             : }
     388             : 
     389             : GEN
     390  2051794767 : mulrr(GEN x, GEN y)
     391             : {
     392             :   long flag, ly, lz, sx, sy;
     393             :   GEN z;
     394             : 
     395  2051794767 :   if (x == y) return sqrr(x);
     396  2051624199 :   sx = signe(x); if (!sx) return real_0_bit(expo(x) + expo(y));
     397  2014309286 :   sy = signe(y); if (!sy) return real_0_bit(expo(x) + expo(y));
     398  2000939967 :   if (sy < 0) sx = -sx;
     399  2000939967 :   lz = lg(x);
     400  2000939967 :   ly = lg(y);
     401  2000939967 :   if (lz > ly) { lz = ly; swap(x, y); flag = 1; } else flag = (lz != ly);
     402  2000939967 :   z = cgetr(lz);
     403  2000939967 :   mulrrz_i(z, x,y, lz,flag, sx);
     404  2000939967 :   return z;
     405             : }
     406             : 
     407             : GEN
     408   106516614 : sqrr(GEN x)
     409             : {
     410   106516614 :   long lz, sx = signe(x);
     411             :   GEN z;
     412             : 
     413   106516614 :   if (!sx) return real_0_bit(2*expo(x));
     414    98335693 :   lz = lg(x); z = cgetr(lz);
     415    98335693 :   sqrz_i(z, x, lz);
     416    98335693 :   return z;
     417             : }
     418             : 
     419             : GEN
     420   253487017 : mulir(GEN x, GEN y)
     421             : {
     422   253487017 :   long sx = signe(x), sy;
     423   253487017 :   if (!sx) return mul0r(y);
     424   253299147 :   if (lgefint(x) == 3) {
     425   219127768 :     GEN z = mulur(uel(x,2), y);
     426   219127768 :     if (sx < 0) togglesign(z);
     427   219127768 :     return z;
     428             :   }
     429    34171379 :   sy = signe(y);
     430    34171379 :   if (!sy) return real_0_bit(expi(x) + expo(y));
     431    29690176 :   if (sy < 0) sx = -sx;
     432             :   {
     433    29690176 :     long lz = lg(y), lx = lgefint(x);
     434    29690176 :     GEN hi, z = cgetr(lz);
     435    29690176 :     pari_sp av = avma;
     436    29690176 :     if (lx < (lz>>1) || (lx < lz && lz > MULRR_MULII_LIMIT))
     437             :     { /* size mantissa of x < half size of mantissa z, or lx < lz so large
     438             :        * that mulrr will call mulii anyway: mulii */
     439     4969388 :       x = itor(x, lx);
     440     4969388 :       hi = muliispec_mirror(y+2, x+2, lz-2, lx-2);
     441     4969388 :       mulrrz_end(z, hi, lz, sx, expo(x)+expo(y), hi[lz]);
     442             :     }
     443             :     else /* dubious: complete x with 0s and call mulrr */
     444    24720788 :       mulrrz_i(z, itor(x,lz), y, lz, 0, sx);
     445    29690176 :     set_avma(av); return z;
     446             :   }
     447             : }
     448             : 
     449             : /* x + y*z, generic. If lgefint(z) <= 3, caller should use faster variants  */
     450             : static GEN
     451    35571910 : addmulii_gen(GEN x, GEN y, GEN z, long lz)
     452             : {
     453    35571910 :   long lx = lgefint(x), ly;
     454             :   pari_sp av;
     455             :   GEN t;
     456    35571910 :   if (lx == 2) return mulii(z,y);
     457    33782030 :   ly = lgefint(y);
     458    33782030 :   if (ly == 2) return icopy(x); /* y = 0, wasteful copy */
     459    33773113 :   av = avma; (void)new_chunk(lx+ly+lz); /*HACK*/
     460    33773113 :   t = mulii(z, y);
     461    33773113 :   set_avma(av); return addii(t,x);
     462             : }
     463             : /* x + y*z, lgefint(z) == 3 */
     464             : static GEN
     465   266668574 : addmulii_lg3(GEN x, GEN y, GEN z)
     466             : {
     467   266668574 :   long s = signe(z), lx, ly;
     468   266668574 :   ulong w = z[2];
     469             :   pari_sp av;
     470             :   GEN t;
     471   266668574 :   if (w == 1) return (s > 0)? addii(x,y): subii(x,y); /* z = +- 1 */
     472   209963350 :   lx = lgefint(x);
     473   209963350 :   ly = lgefint(y);
     474   209963350 :   if (lx == 2)
     475             :   { /* x = 0 */
     476    28050341 :     if (ly == 2) return gen_0;
     477    24396081 :     t = muluispec(w, y+2, ly-2);
     478    24395766 :     if (signe(y) < 0) s = -s;
     479    24395766 :     setsigne(t, s); return t;
     480             :   }
     481   181913009 :   if (ly == 2) return icopy(x); /* y = 0, wasteful copy */
     482   177267996 :   av = avma; (void)new_chunk(1+lx+ly);/*HACK*/
     483   177267358 :   t = muluispec(w, y+2, ly-2);
     484   177265569 :   if (signe(y) < 0) s = -s;
     485   177265569 :   setsigne(t, s);
     486   177265569 :   set_avma(av); return addii(x,t);
     487             : }
     488             : /* x + y*z */
     489             : GEN
     490    46398761 : addmulii(GEN x, GEN y, GEN z)
     491             : {
     492    46398761 :   long lz = lgefint(z);
     493    46398761 :   switch(lz)
     494             :   {
     495      378440 :     case 2: return icopy(x); /* z = 0, wasteful copy */
     496    39793448 :     case 3: return addmulii_lg3(x, y, z);
     497     6226873 :     default:return addmulii_gen(x, y, z, lz);
     498             :   }
     499             : }
     500             : /* x + y*z, returns x itself and not a copy when y*z = 0 */
     501             : GEN
     502  1129762522 : addmulii_inplace(GEN x, GEN y, GEN z)
     503             : {
     504             :   long lz;
     505  1129762522 :   if (lgefint(y) == 2) return x;
     506   256216026 :   lz = lgefint(z);
     507   256216026 :   switch(lz)
     508             :   {
     509        1050 :     case 2: return x;
     510   226869939 :     case 3: return addmulii_lg3(x, y, z);
     511    29345037 :     default:return addmulii_gen(x, y, z, lz);
     512             :   }
     513             : }
     514             : 
     515             : /* written by Bruno Haible following an idea of Robert Harley */
     516             : long
     517  2447800719 : vals(ulong z)
     518             : {
     519             :   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};
     520             : #ifdef LONG_IS_64BIT
     521             :   long s;
     522             : #endif
     523             : 
     524  2447800719 :   if (!z) return -1;
     525             : #ifdef LONG_IS_64BIT
     526  2133770970 :   if (! (z&0xffffffff)) { s = 32; z >>=32; } else s = 0;
     527             : #endif
     528  2447800719 :   z |= ~z + 1;
     529  2447800719 :   z += z << 4;
     530  2447800719 :   z += z << 6;
     531  2447800719 :   z ^= z << 16; /* or  z -= z<<16 */
     532             : #ifdef LONG_IS_64BIT
     533  2133770970 :   return s + tab[(z&0xffffffff)>>26];
     534             : #else
     535   314029749 :   return tab[z>>26];
     536             : #endif
     537             : }
     538             : 
     539             : GEN
     540          21 : divsi(long x, GEN y)
     541             : {
     542          21 :   long p1, s = signe(y);
     543             :   LOCAL_HIREMAINDER;
     544             : 
     545          21 :   if (!s) pari_err_INV("divsi",gen_0);
     546          21 :   if (!x || lgefint(y)>3 || ((long)y[2])<0) return gen_0;
     547          21 :   hiremainder=0; p1=divll(labs(x),y[2]);
     548          21 :   if (x<0) { hiremainder = -((long)hiremainder); p1 = -p1; }
     549          21 :   if (s<0) p1 = -p1;
     550          21 :   return stoi(p1);
     551             : }
     552             : 
     553             : GEN
     554     2608166 : divir(GEN x, GEN y)
     555             : {
     556             :   GEN z;
     557     2608166 :   long ly = lg(y), lx = lgefint(x);
     558             :   pari_sp av;
     559             : 
     560     2608166 :   if (ly == 2) pari_err_INV("divir",y);
     561     2608166 :   if (lx == 2) return div0r(y);
     562     2543382 :   if (lx == 3) {
     563     1367547 :     z = divur(x[2], y);
     564     1367547 :     if (signe(x) < 0) togglesign(z);
     565     1367547 :     return z;
     566             :   }
     567     1175835 :   z = cgetr(ly); av = avma;
     568     1175835 :   affrr(divrr(itor(x, ly+1), y), z);
     569     1175835 :   set_avma(av); return z;
     570             : }
     571             : 
     572             : GEN
     573     1413227 : divur(ulong x, GEN y)
     574             : {
     575             :   pari_sp av;
     576     1413227 :   long ly = lg(y);
     577             :   GEN z;
     578             : 
     579     1413227 :   if (ly == 2) pari_err_INV("divur",y);
     580     1413227 :   if (!x) return div0r(y);
     581     1413227 :   if (ly > INVNEWTON_LIMIT) {
     582         571 :     av = avma; z = invr(y);
     583         571 :     if (x == 1) return z;
     584         571 :     return gerepileuptoleaf(av, mulur(x, z));
     585             :   }
     586     1412656 :   z = cgetr(ly); av = avma;
     587     1412656 :   affrr(divrr(utor(x,ly+1), y), z);
     588     1412656 :   set_avma(av); return z;
     589             : }
     590             : 
     591             : GEN
     592        7422 : divsr(long x, GEN y)
     593             : {
     594             :   pari_sp av;
     595        7422 :   long ly = lg(y);
     596             :   GEN z;
     597             : 
     598        7422 :   if (ly == 2) pari_err_INV("divsr",y);
     599        7422 :   if (!x) return div0r(y);
     600        7422 :   if (ly > INVNEWTON_LIMIT) {
     601           0 :     av = avma; z = invr(y);
     602           0 :     if (x == 1) return z;
     603           0 :     if (x ==-1) { togglesign(z); return z; }
     604           0 :     return gerepileuptoleaf(av, mulsr(x, z));
     605             :   }
     606        7422 :   z = cgetr(ly); av = avma;
     607        7422 :   affrr(divrr(stor(x,ly+1), y), z);
     608        7422 :   set_avma(av); return z;
     609             : }
     610             : 
     611             : /* returns 1/y, assume y != 0 */
     612             : static GEN
     613    19399669 : invr_basecase(GEN y)
     614             : {
     615    19399669 :   long ly = lg(y);
     616    19399669 :   GEN z = cgetr(ly);
     617    19399669 :   pari_sp av = avma;
     618    19399669 :   affrr(divrr(real_1(ly+1), y), z);
     619    19399669 :   set_avma(av); return z;
     620             : }
     621             : /* returns 1/b, Newton iteration */
     622             : GEN
     623    19399669 : invr(GEN b)
     624             : {
     625    19399669 :   const long s = 6;
     626    19399669 :   long i, p, l = lg(b);
     627             :   GEN x, a;
     628             :   ulong mask;
     629             : 
     630    19399669 :   if (l <= maxss(INVNEWTON_LIMIT, (1L<<s) + 2)) {
     631    19085691 :     if (l == 2) pari_err_INV("invr",b);
     632    19085691 :     return invr_basecase(b);
     633             :   }
     634      313978 :   mask = quadratic_prec_mask(l-2);
     635      313978 :   for(i=0, p=1; i<s; i++) { p <<= 1; if (mask & 1) p--; mask >>= 1; }
     636      313978 :   x = cgetr(l);
     637      313978 :   a = rcopy(b); a[1] = _evalexpo(0) | evalsigne(1);
     638      313978 :   affrr(invr_basecase(rtor(a, p+2)), x);
     639     1083288 :   while (mask > 1)
     640             :   {
     641      455332 :     p <<= 1; if (mask & 1) p--;
     642      455332 :     mask >>= 1;
     643      455332 :     setlg(a, p + 2);
     644      455332 :     setlg(x, p + 2);
     645             :     /* TODO: mulrr(a,x) should be a half product (the higher half is known).
     646             :      * mulrr(x, ) already is */
     647      455332 :     affrr(addrr(x, mulrr(x, subsr(1, mulrr(a,x)))), x);
     648      455332 :     set_avma((pari_sp)a);
     649             :   }
     650      313978 :   x[1] = (b[1] & SIGNBITS) | evalexpo(expo(x)-expo(b));
     651      313978 :   set_avma((pari_sp)x); return x;
     652             : }
     653             : 
     654             : GEN
     655  1176147826 : modii(GEN x, GEN y)
     656             : {
     657  1176147826 :   switch(signe(x))
     658             :   {
     659   174560019 :     case 0: return gen_0;
     660   697610851 :     case 1: return remii(x,y);
     661             :     default:
     662             :     {
     663   303976956 :       pari_sp av = avma;
     664   303976956 :       (void)new_chunk(lgefint(y));
     665   303971318 :       x = remii(x,y); set_avma(av);
     666   303965017 :       if (x==gen_0) return x;
     667   278124256 :       return subiispec(y+2,x+2,lgefint(y)-2,lgefint(x)-2);
     668             :     }
     669             :   }
     670             : }
     671             : 
     672             : void
     673      253788 : modiiz(GEN x, GEN y, GEN z)
     674             : {
     675      253788 :   const pari_sp av = avma;
     676      253788 :   affii(modii(x,y),z); set_avma(av);
     677      253788 : }
     678             : 
     679             : GEN
     680     8620576 : divrs(GEN x, long y)
     681             : {
     682             :   GEN z;
     683     8620576 :   if (y < 0)
     684             :   {
     685      758740 :     z = divru(x, (ulong)-y);
     686      758740 :     togglesign(z);
     687             :   }
     688             :   else
     689     7861836 :     z = divru(x, (ulong)y);
     690     8620576 :   return z;
     691             : }
     692             : 
     693             : GEN
     694   619577075 : divru(GEN x, ulong y)
     695             : {
     696   619577075 :   long i, lx, sh, e, s = signe(x);
     697             :   ulong garde;
     698             :   GEN z;
     699             :   LOCAL_HIREMAINDER;
     700             : 
     701   619577075 :   if (!y) pari_err_INV("divru",gen_0);
     702   619577075 :   if (!s) return real_0_bit(expo(x) - expu(y));
     703   619446908 :   if (!(y & (y-1))) /* power of 2 */
     704             :   {
     705    49653264 :     if (y == 1) return rcopy(x);
     706    47405841 :     return shiftr(x, -expu(y));
     707             :   }
     708   569793644 :   e = expo(x);
     709   569793644 :   lx = lg(x);
     710   569793644 :   z = cgetr(lx);
     711   569793644 :   if (lx == 3)
     712             :   {
     713   341192581 :     if (y <= uel(x,2))
     714             :     {
     715   341192521 :       hiremainder = 0;
     716   341192521 :       z[2] = divll(x[2],y);
     717             :       /* we may have hiremainder != 0 ==> garde */
     718   341192521 :       garde = divll(0,y);
     719             :     }
     720             :     else
     721             :     {
     722          60 :       hiremainder = x[2];
     723          60 :       z[2] = divll(0,y);
     724          60 :       garde = hiremainder;
     725          60 :       e -= BITS_IN_LONG;
     726             :     }
     727             :   }
     728             :   else
     729             :   {
     730   228601063 :     ulong yp = get_Fl_red(y);
     731   228601063 :     if (y <= uel(x,2))
     732             :     {
     733   228598205 :       hiremainder = 0;
     734   228598205 :       for (i=2; i<lx; i++) z[i] = divll_pre(x[i],y,yp);
     735             :       /* we may have hiremainder != 0 ==> garde */
     736   228598205 :       garde = divll_pre(0,y,yp);
     737             :     }
     738             :     else
     739             :     {
     740        2858 :       long l = lx-1;
     741        2858 :       hiremainder = x[2];
     742        2858 :       for (i=2; i<l; i++) z[i] = divll_pre(x[i+1],y,yp);
     743        2858 :       z[i] = divll_pre(0,y,yp);
     744        2858 :       garde = hiremainder;
     745        2858 :       e -= BITS_IN_LONG;
     746             :     }
     747             :   }
     748   569793644 :   sh=bfffo(z[2]); /* z[2] != 0 */
     749   569793644 :   if (sh) shift_left(z,z, 2,lx-1, garde,sh);
     750   569793644 :   z[1] = evalsigne(s) | evalexpo(e-sh);
     751   569793644 :   if ((garde << sh) & HIGHBIT) roundr_up_ip(z, lx);
     752   569793644 :   return z;
     753             : }
     754             : 
     755             : GEN
     756    57005523 : truedvmdii(GEN x, GEN y, GEN *z)
     757             : {
     758             :   pari_sp av;
     759             :   GEN r, q, *gptr[2];
     760    57005523 :   if (!is_bigint(y)) return truedvmdis(x, itos(y), z);
     761     2314506 :   if (z == ONLY_REM) return modii(x,y);
     762             : 
     763     2314506 :   av = avma;
     764     2314506 :   q = dvmdii(x,y,&r); /* assume that r is last on stack */
     765     2314506 :   switch(signe(r))
     766             :   {
     767             :     case 0:
     768      425124 :       if (z) *z = gen_0;
     769      425124 :       return q;
     770             :     case 1:
     771     1240936 :       if (z) *z = r; else cgiv(r);
     772     1240936 :       return q;
     773      648446 :     case -1: break;
     774             :   }
     775      648446 :   q = addis(q, -signe(y));
     776      648446 :   if (!z) return gerepileuptoint(av, q);
     777             : 
     778      218554 :   *z = subiispec(y+2,r+2, lgefint(y)-2,lgefint(r)-2);
     779      218554 :   gptr[0]=&q; gptr[1]=z; gerepilemanysp(av,(pari_sp)r,gptr,2);
     780      218554 :   return q;
     781             : }
     782             : GEN
     783    54735188 : truedvmdis(GEN x, long y, GEN *z)
     784             : {
     785    54735188 :   pari_sp av = avma;
     786             :   long r;
     787             :   GEN q;
     788             : 
     789    54735188 :   if (z == ONLY_REM) return modis(x, y);
     790    54735188 :   q = divis_rem(x,y,&r);
     791             : 
     792    54735214 :   if (r >= 0)
     793             :   {
     794    50555481 :     if (z) *z = utoi(r);
     795    50555399 :     return q;
     796             :   }
     797     4179733 :   q = gerepileuptoint(av, addis(q, (y < 0)? 1: -1));
     798     4179733 :   if (z) *z = utoi(r + labs(y));
     799     4179733 :   return q;
     800             : }
     801             : GEN
     802     6168049 : truedvmdsi(long x, GEN y, GEN *z)
     803             : {
     804             :   long q, r;
     805     6168049 :   if (z == ONLY_REM) return modsi(x, y);
     806     6168049 :   q = sdivsi_rem(x,y,&r);
     807     6168049 :   if (r >= 0) {
     808     6167832 :     if (z) *z = utoi(r);
     809     6167832 :     return stoi(q);
     810             :   }
     811         217 :   q = q - signe(y);
     812         217 :   if (!z) return stoi(q);
     813             : 
     814           0 :   *z = subiuspec(y+2,(ulong)-r, lgefint(y)-2);
     815           0 :   return stoi(q);
     816             : }
     817             : 
     818             : /* 2^n = shifti(gen_1, n) */
     819             : GEN
     820    49431209 : int2n(long n) {
     821             :   long i, m, l;
     822             :   GEN z;
     823    49431209 :   if (n < 0) return gen_0;
     824    49429743 :   if (n == 0) return gen_1;
     825             : 
     826    48857535 :   l = dvmdsBIL(n, &m) + 3;
     827    48857060 :   z = cgetipos(l);
     828    48853855 :   for (i = 2; i < l; i++) z[i] = 0;
     829    48853855 :   *int_MSW(z) = 1UL << m; return z;
     830             : }
     831             : /* To avoid problems when 2^(BIL-1) < n. Overflow cleanly, where int2n
     832             :  * returns gen_0 */
     833             : GEN
     834    11442133 : int2u(ulong n) {
     835             :   ulong i, m, l;
     836             :   GEN z;
     837    11442133 :   if (n == 0) return gen_1;
     838             : 
     839    11442133 :   l = dvmduBIL(n, &m) + 3;
     840    11442130 :   z = cgetipos(l);
     841    11442054 :   for (i = 2; i < l; i++) z[i] = 0;
     842    11442054 :   *int_MSW(z) = 1UL << m; return z;
     843             : }
     844             : /* 2^n - 1 */
     845             : GEN
     846         483 : int2um1(ulong n) {
     847             :   ulong i, m, l;
     848             :   GEN z;
     849         483 :   if (n == 0) return gen_0;
     850             : 
     851         483 :   l = dvmduBIL(n, &m);
     852         483 :   l += m? 3: 2;
     853         483 :   z = cgetipos(l);
     854         483 :   for (i = 2; i < l; i++) z[i] = ~0UL;
     855         483 :   if (m) *int_MSW(z) = (1UL << m) - 1;
     856         483 :   return z;
     857             : }
     858             : 
     859             : GEN
     860   900585263 : shifti(GEN x, long n)
     861             : {
     862   900585263 :   long s = signe(x);
     863             :   GEN y;
     864             : 
     865   900585263 :   if(s == 0) return gen_0;
     866   825472481 :   y = shiftispec(x + 2, lgefint(x) - 2, n);
     867   825216506 :   if (signe(y)) setsigne(y, s);
     868   825216506 :   return y;
     869             : }
     870             : 
     871             : /* actual operations will take place on a+2 and b+2: we strip the codewords */
     872             : GEN
     873  7865009143 : mulii(GEN a,GEN b)
     874             : {
     875             :   long sa,sb;
     876             :   GEN z;
     877             : 
     878  7865009143 :   sa=signe(a); if (!sa) return gen_0;
     879  5827304687 :   sb=signe(b); if (!sb) return gen_0;
     880  4094824360 :   if (sb<0) sa = -sa;
     881  4094824360 :   z = muliispec(a+2,b+2, lgefint(a)-2,lgefint(b)-2);
     882  4096883581 :   setsigne(z,sa); return z;
     883             : }
     884             : 
     885             : GEN
     886  1311001818 : sqri(GEN a) { return sqrispec(a+2, lgefint(a)-2); }
     887             : 
     888             : /* sqrt()'s result may be off by 1 when a is not representable exactly as a
     889             :  * double [64bit machine] */
     890             : ulong
     891   103988913 : usqrt(ulong a)
     892             : {
     893   103988913 :   ulong x = (ulong)sqrt((double)a);
     894             : #ifdef LONG_IS_64BIT
     895    89099987 :   if (x > LOWMASK || x*x > a) x--;
     896             : #endif
     897   103988913 :   return x;
     898             : }
     899             : 
     900             : /********************************************************************/
     901             : /**                                                                **/
     902             : /**              EXPONENT / CONVERSION t_REAL --> double           **/
     903             : /**                                                                **/
     904             : /********************************************************************/
     905             : 
     906             : #ifdef LONG_IS_64BIT
     907             : long
     908           0 : dblexpo(double x)
     909             : {
     910             :   union { double f; ulong i; } fi;
     911           0 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     912           0 :   const int exp_mid = 0x3ff;/* exponent bias */
     913             : 
     914           0 :   if (x==0.) return -exp_mid;
     915           0 :   fi.f = x;
     916           0 :   return ((fi.i & (HIGHBIT-1)) >> mant_len) - exp_mid;
     917             : }
     918             : 
     919             : ulong
     920           0 : dblmantissa(double x)
     921             : {
     922             :   union { double f; ulong i; } fi;
     923           0 :   const int expo_len = 11; /* number of bits of exponent */
     924             : 
     925           0 :   if (x==0.) return 0;
     926           0 :   fi.f = x;
     927           0 :   return (fi.i << expo_len) | HIGHBIT;
     928             : }
     929             : 
     930             : GEN
     931     9248546 : dbltor(double x)
     932             : {
     933             :   GEN z;
     934             :   long e;
     935             :   union { double f; ulong i; } fi;
     936     9248546 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     937     9248546 :   const int exp_mid = 0x3ff;/* exponent bias */
     938     9248546 :   const int expo_len = 11; /* number of bits of exponent */
     939             : 
     940     9248546 :   if (x==0.) return real_0_bit(-exp_mid);
     941     9248066 :   fi.f = x; z = cgetr(DEFAULTPREC);
     942             :   {
     943     9248066 :     const ulong a = fi.i;
     944             :     ulong A;
     945     9248066 :     e = ((a & (HIGHBIT-1)) >> mant_len) - exp_mid;
     946     9248066 :     if (e == exp_mid+1) pari_err_OVERFLOW("dbltor [NaN or Infinity]");
     947     9248066 :     A = a << expo_len;
     948     9248066 :     if (e == -exp_mid)
     949             :     { /* unnormalized values */
     950           0 :       int sh = bfffo(A);
     951           0 :       e -= sh-1;
     952           0 :       z[2] = A << sh;
     953             :     }
     954             :     else
     955     9248066 :       z[2] = HIGHBIT | A;
     956     9248066 :     z[1] = _evalexpo(e) | evalsigne(x<0? -1: 1);
     957             :   }
     958     9248066 :   return z;
     959             : }
     960             : 
     961             : double
     962    43385713 : rtodbl(GEN x)
     963             : {
     964    43385713 :   long ex,s=signe(x);
     965             :   ulong a;
     966             :   union { double f; ulong i; } fi;
     967    43385713 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     968    43385713 :   const int exp_mid = 0x3ff;/* exponent bias */
     969    43385713 :   const int expo_len = 11; /* number of bits of exponent */
     970             : 
     971    43385713 :   if (!s || (ex=expo(x)) < - exp_mid) return 0.0;
     972             : 
     973             :   /* start by rounding to closest */
     974    42832538 :   a = (x[2] & (HIGHBIT-1)) + 0x400;
     975    42832538 :   if (a & HIGHBIT) { ex++; a=0; }
     976    42832538 :   if (ex >= exp_mid) pari_err_OVERFLOW("t_REAL->double conversion");
     977    42832538 :   fi.i = ((ex + exp_mid) << mant_len) | (a >> expo_len);
     978    42832538 :   if (s<0) fi.i |= HIGHBIT;
     979    42832538 :   return fi.f;
     980             : }
     981             : 
     982             : #else /* LONG_IS_64BIT */
     983             : 
     984             : #if   PARI_DOUBLE_FORMAT == 1
     985             : #  define INDEX0 1
     986             : #  define INDEX1 0
     987             : #elif PARI_DOUBLE_FORMAT == 0
     988             : #  define INDEX0 0
     989             : #  define INDEX1 1
     990             : #endif
     991             : 
     992             : long
     993           0 : dblexpo(double x)
     994             : {
     995             :   union { double f; ulong i[2]; } fi;
     996           0 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
     997           0 :   const int exp_mid = 0x3ff;/* exponent bias */
     998           0 :   const int shift = mant_len-32;
     999             : 
    1000           0 :   if (x==0.) return -exp_mid;
    1001           0 :   fi.f = x;
    1002             :   {
    1003           0 :     const ulong a = fi.i[INDEX0];
    1004           0 :     return ((a & (HIGHBIT-1)) >> shift) - exp_mid;
    1005             :   }
    1006             : }
    1007             : 
    1008             : ulong
    1009           0 : dblmantissa(double x)
    1010             : {
    1011             :   union { double f; ulong i[2]; } fi;
    1012           0 :   const int expo_len = 11; /* number of bits of exponent */
    1013             : 
    1014           0 :   if (x==0.) return 0;
    1015           0 :   fi.f = x;
    1016             :   {
    1017           0 :     const ulong a = fi.i[INDEX0];
    1018           0 :     const ulong b = fi.i[INDEX1];
    1019           0 :     return HIGHBIT | b >> (BITS_IN_LONG-expo_len) | (a << expo_len);
    1020             :   }
    1021             : }
    1022             : 
    1023             : GEN
    1024     1549102 : dbltor(double x)
    1025             : {
    1026             :   GEN z;
    1027             :   long e;
    1028             :   union { double f; ulong i[2]; } fi;
    1029     1549102 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
    1030     1549102 :   const int exp_mid = 0x3ff;/* exponent bias */
    1031     1549102 :   const int expo_len = 11; /* number of bits of exponent */
    1032     1549102 :   const int shift = mant_len-32;
    1033             : 
    1034     1549102 :   if (x==0.) return real_0_bit(-exp_mid);
    1035     1549034 :   fi.f = x; z = cgetr(DEFAULTPREC);
    1036             :   {
    1037     1549034 :     const ulong a = fi.i[INDEX0];
    1038     1549034 :     const ulong b = fi.i[INDEX1];
    1039             :     ulong A, B;
    1040     1549034 :     e = ((a & (HIGHBIT-1)) >> shift) - exp_mid;
    1041     1549034 :     if (e == exp_mid+1) pari_err_OVERFLOW("dbltor [NaN or Infinity]");
    1042     1549034 :     A = b >> (BITS_IN_LONG-expo_len) | (a << expo_len);
    1043     1549034 :     B = b << expo_len;
    1044     1549034 :     if (e == -exp_mid)
    1045             :     { /* unnormalized values */
    1046             :       int sh;
    1047           0 :       if (A)
    1048             :       {
    1049           0 :         sh = bfffo(A);
    1050           0 :         e -= sh-1;
    1051           0 :         z[2] = (A << sh) | (B >> (32-sh));
    1052           0 :         z[3] = B << sh;
    1053             :       }
    1054             :       else
    1055             :       {
    1056           0 :         sh = bfffo(B); /* B != 0 */
    1057           0 :         e -= sh-1 + 32;
    1058           0 :         z[2] = B << sh;
    1059           0 :         z[3] = 0;
    1060             :       }
    1061             :     }
    1062             :     else
    1063             :     {
    1064     1549034 :       z[3] = B;
    1065     1549034 :       z[2] = HIGHBIT | A;
    1066             :     }
    1067     1549034 :     z[1] = _evalexpo(e) | evalsigne(x<0? -1: 1);
    1068             :   }
    1069     1549034 :   return z;
    1070             : }
    1071             : 
    1072             : double
    1073     7080829 : rtodbl(GEN x)
    1074             : {
    1075     7080829 :   long ex,s=signe(x),lx=lg(x);
    1076             :   ulong a,b,k;
    1077             :   union { double f; ulong i[2]; } fi;
    1078     7080829 :   const int mant_len = 52;  /* mantissa bits (excl. hidden bit) */
    1079     7080829 :   const int exp_mid = 0x3ff;/* exponent bias */
    1080     7080829 :   const int expo_len = 11; /* number of bits of exponent */
    1081     7080829 :   const int shift = mant_len-32;
    1082             : 
    1083     7080829 :   if (!s || (ex=expo(x)) < - exp_mid) return 0.0;
    1084             : 
    1085             :   /* start by rounding to closest */
    1086     6989239 :   a = x[2] & (HIGHBIT-1);
    1087     6989239 :   if (lx > 3)
    1088             :   {
    1089     6749122 :     b = x[3] + 0x400UL; if (b < 0x400UL) a++;
    1090     6749122 :     if (a & HIGHBIT) { ex++; a=0; }
    1091             :   }
    1092      240117 :   else b = 0;
    1093     6989239 :   if (ex >= exp_mid) pari_err_OVERFLOW("t_REAL->double conversion");
    1094     6989239 :   ex += exp_mid;
    1095     6989239 :   k = (a >> expo_len) | (ex << shift);
    1096     6989239 :   if (s<0) k |= HIGHBIT;
    1097     6989239 :   fi.i[INDEX0] = k;
    1098     6989239 :   fi.i[INDEX1] = (a << (BITS_IN_LONG-expo_len)) | (b >> expo_len);
    1099     6989239 :   return fi.f;
    1100             : }
    1101             : #endif /* LONG_IS_64BIT */
    1102             : 

Generated by: LCOV version 1.13