mux/src/strtod.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * The author of this software is David M. Gay.
00004  *
00005  * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
00006  *
00007  * Permission to use, copy, modify, and distribute this software for any
00008  * purpose without fee is hereby granted, provided that this entire notice
00009  * is included in all copies of any software which is or includes a copy
00010  * or modification of this software and in all copies of the supporting
00011  * documentation for such software.
00012  *
00013  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
00014  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
00015  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
00016  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
00017  *
00018  ***************************************************************/
00019 
00020 /* Please send bug reports to
00021     David M. Gay
00022     Bell Laboratories, Room 2C-463
00023     600 Mountain Avenue
00024     Murray Hill, NJ 07974-0636
00025     U.S.A.
00026     dmg@bell-labs.com
00027  */
00028 
00029 /* On a machine with IEEE extended-precision registers, it is
00030  * necessary to specify double-precision (53-bit) rounding precision
00031  * before invoking strtod or dtoa.  If the machine uses (the equivalent
00032  * of) Intel 80x87 arithmetic, the call
00033  *  _control87(PC_53, MCW_PC);
00034  * does this with many compilers.  Whether this or another call is
00035  * appropriate depends on the compiler; for this to work, it may be
00036  * necessary to #include "float.h" or another system-dependent header
00037  * file.
00038  */
00039 
00040 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
00041  *
00042  * This strtod returns a nearest machine number to the input decimal
00043  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
00044  * broken by the IEEE round-even rule.  Otherwise ties are broken by
00045  * biased rounding (add half and chop).
00046  *
00047  * Inspired loosely by William D. Clinger's paper "How to Read Floating
00048  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
00049  *
00050  * Modifications:
00051  *
00052  *  1. We only require IEEE, IBM, or VAX double-precision
00053  *      arithmetic (not IEEE double-extended).
00054  *  2. We get by with floating-point arithmetic in a case that
00055  *      Clinger missed -- when we're computing d * 10^n
00056  *      for a small integer d and the integer n is not too
00057  *      much larger than 22 (the maximum integer k for which
00058  *      we can represent 10^k exactly), we may be able to
00059  *      compute (d*10^k) * 10^(e-k) with just one roundoff.
00060  *  3. Rather than a bit-at-a-time adjustment of the binary
00061  *      result in the hard case, we use floating-point
00062  *      arithmetic to determine the adjustment to within
00063  *      one bit; only in really hard cases do we need to
00064  *      compute a second residual.
00065  *  4. Because of 3., we don't need a large table of powers of 10
00066  *      for ten-to-e (just some small tables, e.g. of 10^k
00067  *      for 0 <= k <= 22).
00068  */
00069 
00070 /*
00071  * #define IEEE_8087 for IEEE-arithmetic machines where the least
00072  *  significant byte has the lowest address.
00073  * #define IEEE_MC68k for IEEE-arithmetic machines where the most
00074  *  significant byte has the lowest address.
00075  * #define Long int on machines with 32-bit ints and 64-bit longs.
00076  * #define IBM for IBM mainframe-style floating-point arithmetic.
00077  * #define VAX for VAX-style floating-point arithmetic (D_floating).
00078  * #define No_leftright to omit left-right logic in fast floating-point
00079  *  computation of dtoa.
00080  * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
00081  *  and strtod and dtoa should round accordingly.
00082  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
00083  *  and Honor_FLT_ROUNDS is not #defined.
00084  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
00085  *  that use extended-precision instructions to compute rounded
00086  *  products and quotients) with IBM.
00087  * #define ROUND_BIASED for IEEE-format with biased rounding.
00088  * #define Inaccurate_Divide for IEEE-format with correctly rounded
00089  *  products but inaccurate quotients, e.g., for Intel i860.
00090  * #define Bad_float_h if your system lacks a float.h or if it does not
00091  *  define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
00092  *  FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
00093  * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
00094  *  memory allocations from a private pool of memory when possible.
00095  *  When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
00096  *  unless #defined to be a different length.  This default length
00097  *  suffices to get rid of MALLOC calls except for unusual cases,
00098  *  such as decimal-to-binary conversion of a very long string of
00099  *  digits.  The longest string dtoa can return is about 751 bytes
00100  *  long.  For conversions by strtod of strings of 800 digits and
00101  *  all dtoa conversions in single-threaded executions with 8-byte
00102  *  pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
00103  *  pointers, PRIVATE_MEM >= 7112 appears adequate.
00104  * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
00105  *  avoids underflows on inputs whose result does not underflow.
00106  *  If you #define NO_IEEE_Scale on a machine that uses IEEE-format
00107  *  floating-point numbers and flushes underflows to zero rather
00108  *  than implementing gradual underflow, then you must also #define
00109  *  Sudden_Underflow.
00110  * #define YES_ALIAS to permit aliasing certain double values with
00111  *  arrays of ULongs.  This leads to slightly better code with
00112  *  some compilers and was always used prior to 19990916, but it
00113  *  is not strictly legal and can cause trouble with aggressively
00114  *  optimizing compilers (e.g., gcc 2.95.1 under -O2).
00115  * #define SET_INEXACT if IEEE arithmetic is being used and extra
00116  *  computation should be done to set the inexact flag when the
00117  *  result is inexact and avoid setting inexact when the result
00118  *  is exact.  In this case, dtoa.c must be compiled in
00119  *  an environment, perhaps provided by #include "dtoa.c" in a
00120  *  suitable wrapper, that defines two functions,
00121  *      int get_inexact(void);
00122  *      void clear_inexact(void);
00123  *  such that get_inexact() returns a nonzero value if the
00124  *  inexact bit is already set, and clear_inexact() sets the
00125  *  inexact bit to 0.  When SET_INEXACT is #defined, strtod
00126  *  also does extra computations to set the underflow and overflow
00127  *  flags when appropriate (i.e., when the result is tiny and
00128  *  inexact or when it is a numeric value rounded to +-infinity).
00129  */
00130 
00131 #include "autoconf.h"
00132 #include "config.h"
00133 #include "externs.h"
00134 #include "stringutil.h"
00135 
00136 #if   defined(HAVE_FPU_CONTROL_H)
00137 #include <fpu_control.h>
00138 #elif defined(IEEEFP_H_USEABLE)
00139 #include <ieeefp.h>
00140 #elif defined(HAVE_FENV_H)
00141 #include <fenv.h>
00142 #endif
00143 
00144 #if defined(WORDS_BIGENDIAN)
00145 #define IEEE_MC68k
00146 #elif defined(WORDS_LITTLEENDIAN)
00147 #define IEEE_8087
00148 #else
00149 #error Must be either Big or Little Endian.
00150 #endif
00151 
00152 #define Long INT32
00153 typedef UINT32 ULong;
00154 
00155 #ifndef Omit_Private_Memory
00156 #ifndef PRIVATE_MEM
00157 #define PRIVATE_MEM 2304
00158 #endif
00159 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
00160 static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
00161 #endif
00162 
00163 #undef IEEE_Arith
00164 #undef Avoid_Underflow
00165 #ifdef IEEE_MC68k
00166 #define IEEE_Arith
00167 #endif
00168 #ifdef IEEE_8087
00169 #define IEEE_Arith
00170 #endif
00171 
00172 #ifdef Bad_float_h
00173 
00174 #ifdef IEEE_Arith
00175 #define DBL_DIG 15
00176 #define DBL_MAX_10_EXP 308
00177 #define DBL_MAX_EXP 1024
00178 #define FLT_RADIX 2
00179 #endif /*IEEE_Arith*/
00180 
00181 #ifdef IBM
00182 #define DBL_DIG 16
00183 #define DBL_MAX_10_EXP 75
00184 #define DBL_MAX_EXP 63
00185 #define FLT_RADIX 16
00186 #define DBL_MAX 7.2370055773322621e+75
00187 #endif
00188 
00189 #ifdef VAX
00190 #define DBL_DIG 16
00191 #define DBL_MAX_10_EXP 38
00192 #define DBL_MAX_EXP 127
00193 #define FLT_RADIX 2
00194 #define DBL_MAX 1.7014118346046923e+38
00195 #endif
00196 
00197 #ifndef LONG_MAX
00198 #define LONG_MAX 2147483647
00199 #endif
00200 
00201 #else /* ifndef Bad_float_h */
00202 #include "float.h"
00203 #endif /* Bad_float_h */
00204 
00205 #ifndef __MATH_H__
00206 #include "math.h"
00207 #endif
00208 
00209 #ifndef CONST
00210 #define CONST const
00211 #endif
00212 
00213 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
00214 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
00215 #endif
00216 
00217 typedef union
00218 {
00219     double d;
00220     ULong L[2];
00221 } U;
00222 
00223 #ifdef YES_ALIAS
00224 #define dval(x) x
00225 #ifdef IEEE_8087
00226 #define word0(x) ((ULong *)&x)[1]
00227 #define word1(x) ((ULong *)&x)[0]
00228 #else
00229 #define word0(x) ((ULong *)&x)[0]
00230 #define word1(x) ((ULong *)&x)[1]
00231 #endif
00232 #else
00233 #ifdef IEEE_8087
00234 #define word0(x) ((U*)&x)->L[1]
00235 #define word1(x) ((U*)&x)->L[0]
00236 #else
00237 #define word0(x) ((U*)&x)->L[0]
00238 #define word1(x) ((U*)&x)->L[1]
00239 #endif
00240 #define dval(x) ((U*)&x)->d
00241 #endif
00242 
00243 /* The following definition of Storeinc is appropriate for MIPS processors.
00244  * An alternative that might be better on some machines is
00245  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
00246  */
00247 #if defined(IEEE_8087) + defined(VAX)
00248 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
00249 ((unsigned short *)a)[0] = (unsigned short)c, a++)
00250 #else
00251 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
00252 ((unsigned short *)a)[1] = (unsigned short)c, a++)
00253 #endif
00254 
00255 /* #define P DBL_MANT_DIG */
00256 /* Ten_pmax = floor(P*log(2)/log(5)) */
00257 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
00258 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
00259 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
00260 
00261 #ifdef IEEE_Arith
00262 #define Exp_shift  20
00263 #define Exp_shift1 20
00264 #define Exp_msk1    0x100000
00265 #define Exp_msk11   0x100000
00266 #define Exp_mask  0x7ff00000
00267 #define P 53
00268 #define Bias 1023
00269 #define Emin (-1022)
00270 #define Exp_1  0x3ff00000
00271 #define Exp_11 0x3ff00000
00272 #define Ebits 11
00273 #define Frac_mask  0xfffff
00274 #define Frac_mask1 0xfffff
00275 #define Ten_pmax 22
00276 #define Bletch 0x10
00277 #define Bndry_mask  0xfffff
00278 #define Bndry_mask1 0xfffff
00279 #define LSB 1
00280 #define Sign_bit 0x80000000
00281 #define Log2P 1
00282 #define Tiny0 0
00283 #define Tiny1 1
00284 #define Quick_max 14
00285 #define Int_max 14
00286 #ifndef NO_IEEE_Scale
00287 #define Avoid_Underflow
00288 #ifdef Flush_Denorm /* debugging option */
00289 #undef Sudden_Underflow
00290 #endif
00291 #endif
00292 
00293 #ifndef Flt_Rounds
00294 #ifdef FLT_ROUNDS
00295 #define Flt_Rounds FLT_ROUNDS
00296 #else
00297 #define Flt_Rounds 1
00298 #endif
00299 #endif /*Flt_Rounds*/
00300 
00301 #ifdef Honor_FLT_ROUNDS
00302 #define Rounding rounding
00303 #undef Check_FLT_ROUNDS
00304 #define Check_FLT_ROUNDS
00305 #else
00306 #define Rounding Flt_Rounds
00307 #endif
00308 
00309 #else /* ifndef IEEE_Arith */
00310 #undef Check_FLT_ROUNDS
00311 #undef Honor_FLT_ROUNDS
00312 #undef SET_INEXACT
00313 #undef  Sudden_Underflow
00314 #define Sudden_Underflow
00315 #ifdef IBM
00316 #undef Flt_Rounds
00317 #define Flt_Rounds 0
00318 #define Exp_shift  24
00319 #define Exp_shift1 24
00320 #define Exp_msk1   0x1000000
00321 #define Exp_msk11  0x1000000
00322 #define Exp_mask  0x7f000000
00323 #define P 14
00324 #define Bias 65
00325 #define Exp_1  0x41000000
00326 #define Exp_11 0x41000000
00327 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
00328 #define Frac_mask  0xffffff
00329 #define Frac_mask1 0xffffff
00330 #define Bletch 4
00331 #define Ten_pmax 22
00332 #define Bndry_mask  0xefffff
00333 #define Bndry_mask1 0xffffff
00334 #define LSB 1
00335 #define Sign_bit 0x80000000
00336 #define Log2P 4
00337 #define Tiny0 0x100000
00338 #define Tiny1 0
00339 #define Quick_max 14
00340 #define Int_max 15
00341 #else /* VAX */
00342 #undef Flt_Rounds
00343 #define Flt_Rounds 1
00344 #define Exp_shift  23
00345 #define Exp_shift1 7
00346 #define Exp_msk1    0x80
00347 #define Exp_msk11   0x800000
00348 #define Exp_mask  0x7f80
00349 #define P 56
00350 #define Bias 129
00351 #define Exp_1  0x40800000
00352 #define Exp_11 0x4080
00353 #define Ebits 8
00354 #define Frac_mask  0x7fffff
00355 #define Frac_mask1 0xffff007f
00356 #define Ten_pmax 24
00357 #define Bletch 2
00358 #define Bndry_mask  0xffff007f
00359 #define Bndry_mask1 0xffff007f
00360 #define LSB 0x10000
00361 #define Sign_bit 0x8000
00362 #define Log2P 1
00363 #define Tiny0 0x80
00364 #define Tiny1 0
00365 #define Quick_max 15
00366 #define Int_max 15
00367 #endif /* IBM, VAX */
00368 #endif /* IEEE_Arith */
00369 
00370 #ifndef IEEE_Arith
00371 #define ROUND_BIASED
00372 #endif
00373 
00374 #ifdef RND_PRODQUOT
00375 #define rounded_product(a,b) a = rnd_prod(a, b)
00376 #define rounded_quotient(a,b) a = rnd_quot(a, b)
00377 extern double rnd_prod(double, double), rnd_quot(double, double);
00378 #else
00379 #define rounded_product(a,b) a *= b
00380 #define rounded_quotient(a,b) a /= b
00381 #endif
00382 
00383 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
00384 #define Big1 0xffffffff
00385 
00386 #ifndef Pack_32
00387 #define Pack_32
00388 #endif
00389 
00390 #define FFFFFFFF 0xffffffffUL
00391 
00392 #ifndef Llong
00393 #define Llong INT64
00394 #endif
00395 #ifndef ULLong
00396 #define ULLong UINT64
00397 #endif
00398 
00399 #define Kmax 15
00400 
00401 struct Bigint
00402 {
00403     struct Bigint *next;
00404     int k, maxwds, sign, wds;
00405     ULong x[1];
00406 };
00407 
00408 typedef struct Bigint Bigint;
00409 
00410 static Bigint *freelist[Kmax+1];
00411 
00412 static Bigint *Balloc(int k)
00413 {
00414     int x;
00415     Bigint *rv;
00416 #ifndef Omit_Private_Memory
00417     unsigned int len;
00418 #endif
00419 
00420     if ((rv = freelist[k]))
00421     {
00422         freelist[k] = rv->next;
00423     }
00424     else
00425     {
00426         x = 1 << k;
00427 #ifdef Omit_Private_Memory
00428         rv = (Bigint *)MEMALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
00429         ISOUTOFMEMORY(rv);
00430 #else
00431         len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
00432             /sizeof(double);
00433         if (pmem_next - private_mem + len <= PRIVATE_mem)
00434         {
00435             rv = (Bigint*)pmem_next;
00436             pmem_next += len;
00437         }
00438         else
00439         {
00440             rv = (Bigint*)MEMALLOC(len*sizeof(double));
00441             ISOUTOFMEMORY(rv);
00442         }
00443 #endif
00444         rv->k = k;
00445         rv->maxwds = x;
00446     }
00447     rv->sign = rv->wds = 0;
00448     return rv;
00449 }
00450 
00451 static void Bfree(Bigint *v)
00452 {
00453     if (v)
00454     {
00455         v->next = freelist[v->k];
00456         freelist[v->k] = v;
00457     }
00458 }
00459 
00460 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
00461 y->wds*sizeof(Long) + 2*sizeof(int))
00462 
00463 // multiply by m and add a.
00464 //
00465 static Bigint *multadd(Bigint *b, int m, int a)
00466 {
00467     int i, wds;
00468 #ifdef ULLong
00469     ULong *x;
00470     ULLong carry, y;
00471 #else
00472     ULong carry, *x, y;
00473 #ifdef Pack_32
00474     ULong xi, z;
00475 #endif
00476 #endif
00477     Bigint *b1;
00478 
00479     wds = b->wds;
00480     x = b->x;
00481     i = 0;
00482     carry = a;
00483     do
00484     {
00485 #ifdef ULLong
00486         y = *x * (ULLong)m + carry;
00487         carry = y >> 32;
00488         *x++ = (unsigned int)(y & 0xFFFFFFFF);
00489 #else
00490 #ifdef Pack_32
00491         xi = *x;
00492         y = (xi & 0xffff) * m + carry;
00493         z = (xi >> 16) * m + (y >> 16);
00494         carry = z >> 16;
00495         *x++ = (z << 16) + (y & 0xffff);
00496 #else
00497         y = *x * m + carry;
00498         carry = y >> 16;
00499         *x++ = y & 0xffff;
00500 #endif
00501 #endif
00502     } while (++i < wds);
00503     if (carry)
00504     {
00505         if (wds >= b->maxwds)
00506         {
00507             b1 = Balloc(b->k+1);
00508             Bcopy(b1, b);
00509             Bfree(b);
00510             b = b1;
00511         }
00512         b->x[wds++] = (unsigned int)carry;
00513         b->wds = wds;
00514     }
00515     return b;
00516 }
00517 
00518 static Bigint *s2b(CONST char *s, int nd0, int nd, ULong y9)
00519 {
00520     Bigint *b;
00521     int i, k;
00522     Long x, y;
00523 
00524     x = (nd + 8) / 9;
00525     for (k = 0, y = 1; x > y; y <<= 1, k++)
00526     {
00527         ; // Nothing.
00528     }
00529 #ifdef Pack_32
00530     b = Balloc(k);
00531     b->x[0] = y9;
00532     b->wds = 1;
00533 #else
00534     b = Balloc(k+1);
00535     b->x[0] = y9 & 0xffff;
00536     b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
00537 #endif
00538 
00539     i = 9;
00540     if (9 < nd0)
00541     {
00542         s += 9;
00543         do
00544         {
00545             b = multadd(b, 10, *s++ - '0');
00546         } while (++i < nd0);
00547         s++;
00548     }
00549     else
00550     {
00551         s += 10;
00552     }
00553     for (; i < nd; i++)
00554     {
00555         b = multadd(b, 10, *s++ - '0');
00556     }
00557     return b;
00558 }
00559 
00560 static int hi0bits(register ULong x)
00561 {
00562     register int k = 0;
00563 
00564     if (!(x & 0xffff0000))
00565     {
00566         k = 16;
00567         x <<= 16;
00568     }
00569     if (!(x & 0xff000000))
00570     {
00571         k += 8;
00572         x <<= 8;
00573     }
00574     if (!(x & 0xf0000000))
00575     {
00576         k += 4;
00577         x <<= 4;
00578     }
00579     if (!(x & 0xc0000000))
00580     {
00581         k += 2;
00582         x <<= 2;
00583     }
00584     if (!(x & 0x80000000))
00585     {
00586         k++;
00587         if (!(x & 0x40000000))
00588         {
00589             return 32;
00590         }
00591     }
00592     return k;
00593 }
00594 
00595 static int lo0bits(ULong *y)
00596 {
00597     register int k;
00598     register ULong x = *y;
00599 
00600     if (x & 7)
00601     {
00602         if (x & 1)
00603         {
00604             return 0;
00605         }
00606         if (x & 2)
00607         {
00608             *y = x >> 1;
00609             return 1;
00610         }
00611         *y = x >> 2;
00612         return 2;
00613     }
00614     k = 0;
00615     if (!(x & 0xffff))
00616     {
00617         k = 16;
00618         x >>= 16;
00619     }
00620     if (!(x & 0xff))
00621     {
00622         k += 8;
00623         x >>= 8;
00624     }
00625     if (!(x & 0xf))
00626     {
00627         k += 4;
00628         x >>= 4;
00629     }
00630     if (!(x & 0x3))
00631     {
00632         k += 2;
00633         x >>= 2;
00634     }
00635     if (!(x & 1))
00636     {
00637         k++;
00638         x >>= 1;
00639         if (!(x & 1))
00640         {
00641             return 32;
00642         }
00643     }
00644     *y = x;
00645     return k;
00646 }
00647 
00648 static Bigint *i2b(int i)
00649 {
00650     Bigint *b;
00651 
00652     b = Balloc(1);
00653     b->x[0] = i;
00654     b->wds = 1;
00655     return b;
00656 }
00657 
00658 static Bigint *mult(Bigint *a, Bigint *b)
00659 {
00660     Bigint *c;
00661     int k, wa, wb, wc;
00662     ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
00663     ULong y;
00664 #ifdef ULLong
00665     ULLong carry, z;
00666 #else
00667     ULong carry, z;
00668 #ifdef Pack_32
00669     ULong z2;
00670 #endif
00671 #endif
00672 
00673     if (a->wds < b->wds)
00674     {
00675         c = a;
00676         a = b;
00677         b = c;
00678     }
00679     k = a->k;
00680     wa = a->wds;
00681     wb = b->wds;
00682     wc = wa + wb;
00683     if (wc > a->maxwds)
00684     {
00685         k++;
00686     }
00687     c = Balloc(k);
00688     for (x = c->x, xa = x + wc; x < xa; x++)
00689     {
00690         *x = 0;
00691     }
00692     xa = a->x;
00693     xae = xa + wa;
00694     xb = b->x;
00695     xbe = xb + wb;
00696     xc0 = c->x;
00697 #ifdef ULLong
00698     for (; xb < xbe; xc0++)
00699     {
00700         if ((y = *xb++))
00701         {
00702             x = xa;
00703             xc = xc0;
00704             carry = 0;
00705             do
00706             {
00707                 z = *x++ * (ULLong)y + *xc + carry;
00708                 carry = z >> 32;
00709                 *xc++ = (unsigned int)(z & 0xFFFFFFFF);
00710             } while (x < xae);
00711             *xc = (unsigned int)carry;
00712         }
00713     }
00714 #else
00715 #ifdef Pack_32
00716     for (; xb < xbe; xb++, xc0++)
00717     {
00718         if (y = *xb & 0xffff)
00719         {
00720             x = xa;
00721             xc = xc0;
00722             carry = 0;
00723             do
00724             {
00725                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
00726                 carry = z >> 16;
00727                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
00728                 carry = z2 >> 16;
00729                 Storeinc(xc, z2, z);
00730             } while (x < xae);
00731             *xc = carry;
00732         }
00733         if (y = *xb >> 16)
00734         {
00735             x = xa;
00736             xc = xc0;
00737             carry = 0;
00738             z2 = *xc;
00739             do
00740             {
00741                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
00742                 carry = z >> 16;
00743                 Storeinc(xc, z, z2);
00744                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
00745                 carry = z2 >> 16;
00746             } while (x < xae);
00747             *xc = z2;
00748         }
00749     }
00750 #else
00751     for (; xb < xbe; xc0++)
00752     {
00753         if (y = *xb++)
00754         {
00755             x = xa;
00756             xc = xc0;
00757             carry = 0;
00758             do
00759             {
00760                 z = *x++ * y + *xc + carry;
00761                 carry = z >> 16;
00762                 *xc++ = z & 0xffff;
00763             } while (x < xae);
00764             *xc = carry;
00765         }
00766     }
00767 #endif
00768 #endif
00769     for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc)
00770     {
00771         ; // Nothing.
00772     }
00773     c->wds = wc;
00774     return c;
00775 }
00776 
00777 static Bigint *p5s;
00778 
00779 static Bigint *pow5mult(Bigint *b, int k)
00780 {
00781     Bigint *b1, *p5, *p51;
00782     int i;
00783     static int p05[3] = { 5, 25, 125 };
00784 
00785     if ((i = k & 3))
00786     {
00787         b = multadd(b, p05[i-1], 0);
00788     }
00789 
00790     if (!(k >>= 2))
00791     {
00792         return b;
00793     }
00794     if (!(p5 = p5s))
00795     {
00796         /* first time */
00797         p5 = p5s = i2b(625);
00798         p5->next = 0;
00799     }
00800     for (;;)
00801     {
00802         if (k & 1)
00803         {
00804             b1 = mult(b, p5);
00805             Bfree(b);
00806             b = b1;
00807         }
00808         if (!(k >>= 1))
00809         {
00810             break;
00811         }
00812         if (!(p51 = p5->next))
00813         {
00814             p51 = p5->next = mult(p5,p5);
00815             p51->next = 0;
00816         }
00817         p5 = p51;
00818     }
00819     return b;
00820 }
00821 
00822 static Bigint *lshift(Bigint *b, int k)
00823 {
00824     int i, k1, n, n1;
00825     Bigint *b1;
00826     ULong *x, *x1, *xe, z;
00827 
00828 #ifdef Pack_32
00829     n = k >> 5;
00830 #else
00831     n = k >> 4;
00832 #endif
00833     k1 = b->k;
00834     n1 = n + b->wds + 1;
00835     for (i = b->maxwds; n1 > i; i <<= 1)
00836     {
00837         k1++;
00838     }
00839     b1 = Balloc(k1);
00840     x1 = b1->x;
00841     for (i = 0; i < n; i++)
00842     {
00843         *x1++ = 0;
00844     }
00845     x = b->x;
00846     xe = x + b->wds;
00847 #ifdef Pack_32
00848     if (k &= 0x1f)
00849     {
00850         k1 = 32 - k;
00851         z = 0;
00852         do
00853         {
00854             *x1++ = *x << k | z;
00855             z = *x++ >> k1;
00856         } while (x < xe);
00857         if ((*x1 = z))
00858         {
00859             ++n1;
00860         }
00861     }
00862 #else
00863     if (k &= 0xf)
00864     {
00865         k1 = 16 - k;
00866         z = 0;
00867         do
00868         {
00869             *x1++ = *x << k  & 0xffff | z;
00870             z = *x++ >> k1;
00871         } while (x < xe);
00872         if (*x1 = z)
00873         {
00874             ++n1;
00875         }
00876     }
00877 #endif
00878     else
00879     {
00880         do
00881         {
00882             *x1++ = *x++;
00883         } while (x < xe);
00884     }
00885     b1->wds = n1 - 1;
00886     Bfree(b);
00887     return b1;
00888 }
00889 
00890 static int cmp(Bigint *a, Bigint *b)
00891 {
00892     ULong *xa, *xa0, *xb, *xb0;
00893     int i, j;
00894 
00895     i = a->wds;
00896     j = b->wds;
00897     if (i -= j)
00898     {
00899         return i;
00900     }
00901     xa0 = a->x;
00902     xa = xa0 + j;
00903     xb0 = b->x;
00904     xb = xb0 + j;
00905     for (;;)
00906     {
00907         if (*--xa != *--xb)
00908         {
00909             return *xa < *xb ? -1 : 1;
00910         }
00911         if (xa <= xa0)
00912         {
00913             break;
00914         }
00915     }
00916     return 0;
00917 }
00918 
00919 static Bigint *diff(Bigint *a, Bigint *b)
00920 {
00921     Bigint *c;
00922     int i, wa, wb;
00923     ULong *xa, *xae, *xb, *xbe, *xc;
00924 #ifdef ULLong
00925     ULLong borrow, y;
00926 #else
00927     ULong borrow, y;
00928 #ifdef Pack_32
00929     ULong z;
00930 #endif
00931 #endif
00932 
00933     i = cmp(a,b);
00934     if (!i)
00935     {
00936         c = Balloc(0);
00937         c->wds = 1;
00938         c->x[0] = 0;
00939         return c;
00940     }
00941     if (i < 0)
00942     {
00943         c = a;
00944         a = b;
00945         b = c;
00946         i = 1;
00947     }
00948     else
00949     {
00950         i = 0;
00951     }
00952     c = Balloc(a->k);
00953     c->sign = i;
00954     wa = a->wds;
00955     xa = a->x;
00956     xae = xa + wa;
00957     wb = b->wds;
00958     xb = b->x;
00959     xbe = xb + wb;
00960     xc = c->x;
00961     borrow = 0;
00962 #ifdef ULLong
00963     do
00964     {
00965         y = (ULLong)*xa++ - *xb++ - borrow;
00966         borrow = y >> 32 & (ULong)1;
00967         *xc++ = (unsigned int)(y & 0xFFFFFFFF);
00968     } while (xb < xbe);
00969     while (xa < xae)
00970     {
00971         y = *xa++ - borrow;
00972         borrow = y >> 32 & (ULong)1;
00973         *xc++ = (unsigned int)(y & 0xFFFFFFFF);
00974     }
00975 #else
00976 #ifdef Pack_32
00977     do
00978     {
00979         y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
00980         borrow = (y & 0x10000) >> 16;
00981         z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
00982         borrow = (z & 0x10000) >> 16;
00983         Storeinc(xc, z, y);
00984     } while (xb < xbe);
00985     while (xa < xae)
00986     {
00987         y = (*xa & 0xffff) - borrow;
00988         borrow = (y & 0x10000) >> 16;
00989         z = (*xa++ >> 16) - borrow;
00990         borrow = (z & 0x10000) >> 16;
00991         Storeinc(xc, z, y);
00992     }
00993 #else
00994     do
00995     {
00996         y = *xa++ - *xb++ - borrow;
00997         borrow = (y & 0x10000) >> 16;
00998         *xc++ = y & 0xffff;
00999     } while (xb < xbe);
01000     while (xa < xae)
01001     {
01002         y = *xa++ - borrow;
01003         borrow = (y & 0x10000) >> 16;
01004         *xc++ = y & 0xffff;
01005     }
01006 #endif
01007 #endif
01008     while (!*--xc)
01009     {
01010         wa--;
01011     }
01012     c->wds = wa;
01013     return c;
01014 }
01015 
01016 double ulp(double x)
01017 {
01018     register Long L;
01019     double a;
01020 
01021     L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
01022 #ifndef Avoid_Underflow
01023 #ifndef Sudden_Underflow
01024     if (L > 0)
01025     {
01026 #endif
01027 #endif
01028 #ifdef IBM
01029         L |= Exp_msk1 >> 4;
01030 #endif
01031         word0(a) = L;
01032         word1(a) = 0;
01033 #ifndef Avoid_Underflow
01034 #ifndef Sudden_Underflow
01035     }
01036     else
01037     {
01038         L = -L >> Exp_shift;
01039         if (L < Exp_shift)
01040         {
01041             word0(a) = 0x80000 >> L;
01042             word1(a) = 0;
01043         }
01044         else
01045         {
01046             word0(a) = 0;
01047             L -= Exp_shift;
01048             word1(a) = L >= 31 ? 1 : 1 << 31 - L;
01049         }
01050     }
01051 #endif
01052 #endif
01053     return dval(a);
01054 }
01055 
01056 static double b2d(Bigint *a, int *e)
01057 {
01058     ULong *xa, *xa0, w, y, z;
01059     int k;
01060     double d;
01061 #ifdef VAX
01062     ULong d0, d1;
01063 #else
01064 #define d0 word0(d)
01065 #define d1 word1(d)
01066 #endif
01067 
01068     xa0 = a->x;
01069     xa = xa0 + a->wds;
01070     y = *--xa;
01071     k = hi0bits(y);
01072     *e = 32 - k;
01073 #ifdef Pack_32
01074     if (k < Ebits)
01075     {
01076         d0 = Exp_1 | y >> (Ebits - k);
01077         w = xa > xa0 ? *--xa : 0;
01078         d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
01079         goto ret_d;
01080     }
01081     z = xa > xa0 ? *--xa : 0;
01082     if (k -= Ebits)
01083     {
01084         d0 = Exp_1 | y << k | z >> (32 - k);
01085         y = xa > xa0 ? *--xa : 0;
01086         d1 = z << k | y >> (32 - k);
01087     }
01088     else
01089     {
01090         d0 = Exp_1 | y;
01091         d1 = z;
01092     }
01093 #else
01094     if (k < Ebits + 16)
01095     {
01096         z = xa > xa0 ? *--xa : 0;
01097         d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
01098         w = xa > xa0 ? *--xa : 0;
01099         y = xa > xa0 ? *--xa : 0;
01100         d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
01101         goto ret_d;
01102     }
01103     z = xa > xa0 ? *--xa : 0;
01104     w = xa > xa0 ? *--xa : 0;
01105     k -= Ebits + 16;
01106     d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
01107     y = xa > xa0 ? *--xa : 0;
01108     d1 = w << k + 16 | y << k;
01109 #endif
01110 ret_d:
01111 #ifdef VAX
01112     word0(d) = d0 >> 16 | d0 << 16;
01113     word1(d) = d1 >> 16 | d1 << 16;
01114 #else
01115 #undef d0
01116 #undef d1
01117 #endif
01118     return dval(d);
01119 }
01120 
01121 static Bigint *d2b(double d, int *e, int *bits)
01122 {
01123     Bigint *b;
01124     int de, k;
01125     ULong *x, y, z;
01126 #ifndef Sudden_Underflow
01127     int i;
01128 #endif
01129 #ifdef VAX
01130     ULong d0, d1;
01131     d0 = word0(d) >> 16 | word0(d) << 16;
01132     d1 = word1(d) >> 16 | word1(d) << 16;
01133 #else
01134 #define d0 word0(d)
01135 #define d1 word1(d)
01136 #endif
01137 
01138 #ifdef Pack_32
01139     b = Balloc(1);
01140 #else
01141     b = Balloc(2);
01142 #endif
01143     x = b->x;
01144 
01145     z = d0 & Frac_mask;
01146     d0 &= 0x7fffffff;   /* clear sign bit, which we ignore */
01147 #ifdef Sudden_Underflow
01148     de = (int)(d0 >> Exp_shift);
01149 #ifndef IBM
01150     z |= Exp_msk11;
01151 #endif
01152 #else
01153     if ((de = (int)(d0 >> Exp_shift)))
01154     {
01155         z |= Exp_msk1;
01156     }
01157 #endif
01158 #ifdef Pack_32
01159     if ((y = d1))
01160     {
01161         if ((k = lo0bits(&y)))
01162         {
01163             x[0] = y | z << (32 - k);
01164             z >>= k;
01165         }
01166         else
01167         {
01168             x[0] = y;
01169         }
01170 #ifndef Sudden_Underflow
01171         i =
01172 #endif
01173             b->wds = (x[1] = z) ? 2 : 1;
01174     }
01175     else
01176     {
01177         k = lo0bits(&z);
01178         x[0] = z;
01179 #ifndef Sudden_Underflow
01180         i =
01181 #endif
01182             b->wds = 1;
01183         k += 32;
01184     }
01185 #else
01186     if (y = d1)
01187     {
01188         if (k = lo0bits(&y))
01189         {
01190             if (k >= 16)
01191             {
01192                 x[0] = y | z << 32 - k & 0xffff;
01193                 x[1] = z >> k - 16 & 0xffff;
01194                 x[2] = z >> k;
01195                 i = 2;
01196             }
01197             else
01198             {
01199                 x[0] = y & 0xffff;
01200                 x[1] = y >> 16 | z << 16 - k & 0xffff;
01201                 x[2] = z >> k & 0xffff;
01202                 x[3] = z >> k+16;
01203                 i = 3;
01204             }
01205         }
01206         else
01207         {
01208             x[0] = y & 0xffff;
01209             x[1] = y >> 16;
01210             x[2] = z & 0xffff;
01211             x[3] = z >> 16;
01212             i = 3;
01213         }
01214     }
01215     else
01216     {
01217         k = lo0bits(&z);
01218         if (k >= 16)
01219         {
01220             x[0] = z;
01221             i = 0;
01222         }
01223         else
01224         {
01225             x[0] = z & 0xffff;
01226             x[1] = z >> 16;
01227             i = 1;
01228         }
01229         k += 32;
01230     }
01231     while (!x[i])
01232     {
01233         --i;
01234     }
01235     b->wds = i + 1;
01236 #endif
01237 #ifndef Sudden_Underflow
01238     if (de)
01239     {
01240 #endif
01241 #ifdef IBM
01242         *e = (de - Bias - (P-1) << 2) + k;
01243         *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
01244 #else
01245         *e = de - Bias - (P-1) + k;
01246         *bits = P - k;
01247 #endif
01248 #ifndef Sudden_Underflow
01249     }
01250     else
01251     {
01252         *e = de - Bias - (P-1) + 1 + k;
01253 #ifdef Pack_32
01254         *bits = 32*i - hi0bits(x[i-1]);
01255 #else
01256         *bits = (i+2)*16 - hi0bits(x[i]);
01257 #endif
01258     }
01259 #endif
01260     return b;
01261 }
01262 #undef d0
01263 #undef d1
01264 
01265 static double ratio(Bigint *a, Bigint *b)
01266 {
01267     double da, db;
01268     int k, ka, kb;
01269 
01270     dval(da) = b2d(a, &ka);
01271     dval(db) = b2d(b, &kb);
01272 #ifdef Pack_32
01273     k = ka - kb + 32*(a->wds - b->wds);
01274 #else
01275     k = ka - kb + 16*(a->wds - b->wds);
01276 #endif
01277 #ifdef IBM
01278     if (k > 0)
01279     {
01280         word0(da) += (k >> 2)*Exp_msk1;
01281         if (k &= 3)
01282         {
01283             dval(da) *= 1 << k;
01284         }
01285     }
01286     else
01287     {
01288         k = -k;
01289         word0(db) += (k >> 2)*Exp_msk1;
01290         if (k &= 3)
01291         {
01292             dval(db) *= 1 << k;
01293         }
01294     }
01295 #else
01296     if (k > 0)
01297     {
01298         word0(da) += k*Exp_msk1;
01299     }
01300     else
01301     {
01302         k = -k;
01303         word0(db) += k*Exp_msk1;
01304     }
01305 #endif
01306     return dval(da) / dval(db);
01307 }
01308 
01309 static CONST double tens[] =
01310 {
01311     1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
01312     1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
01313     1e20, 1e21, 1e22
01314 #ifdef VAX
01315     , 1e23, 1e24
01316 #endif
01317 };
01318 
01319  static