dtoa.cpp

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 NO_LONG_LONG on machines that do not have a "long long"
00091  *  integer type (of >= 64 bits).  On such machines, you can
00092  *  #define Just_16 to store 16 bits per 32-bit Long when doing
00093  *  high-precision integer arithmetic.  Whether this speeds things
00094  *  up or slows things down depends on the machine and the number
00095  *  being converted.  If long long is available and the name is
00096  *  something other than "long long", #define Llong to be the name,
00097  *  and if "unsigned Llong" does not work as an unsigned version of
00098  *  Llong, #define #ULLong to be the corresponding unsigned type.
00099  * #define KR_headers for old-style C function headers.
00100  * #define Bad_float_h if your system lacks a float.h or if it does not
00101  *  define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
00102  *  FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
00103  * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
00104  *  if memory is available and otherwise does something you deem
00105  *  appropriate.  If MALLOC is undefined, malloc will be invoked
00106  *  directly -- and assumed always to succeed.
00107  * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
00108  *  memory allocations from a private pool of memory when possible.
00109  *  When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
00110  *  unless #defined to be a different length.  This default length
00111  *  suffices to get rid of MALLOC calls except for unusual cases,
00112  *  such as decimal-to-binary conversion of a very long string of
00113  *  digits.  The longest string dtoa can return is about 751 bytes
00114  *  long.  For conversions by strtod of strings of 800 digits and
00115  *  all dtoa conversions in single-threaded executions with 8-byte
00116  *  pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
00117  *  pointers, PRIVATE_MEM >= 7112 appears adequate.
00118  * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
00119  *  Infinity and NaN (case insensitively).  On some systems (e.g.,
00120  *  some HP systems), it may be necessary to #define NAN_WORD0
00121  *  appropriately -- to the most significant word of a quiet NaN.
00122  *  (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
00123  *  When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
00124  *  strtod also accepts (case insensitively) strings of the form
00125  *  NaN(x), where x is a string of hexadecimal digits and spaces;
00126  *  if there is only one string of hexadecimal digits, it is taken
00127  *  for the 52 fraction bits of the resulting NaN; if there are two
00128  *  or more strings of hex digits, the first is for the high 20 bits,
00129  *  the second and subsequent for the low 32 bits, with intervening
00130  *  white space ignored; but if this results in none of the 52
00131  *  fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
00132  *  and NAN_WORD1 are used instead.
00133  * #define MULTIPLE_THREADS if the system offers preemptively scheduled
00134  *  multiple threads.  In this case, you must provide (or suitably
00135  *  #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
00136  *  by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
00137  *  in pow5mult, ensures lazy evaluation of only one copy of high
00138  *  powers of 5; omitting this lock would introduce a small
00139  *  probability of wasting memory, but would otherwise be harmless.)
00140  *  You must also invoke freedtoa(s) to free the value s returned by
00141  *  dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
00142  * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
00143  *  avoids underflows on inputs whose result does not underflow.
00144  *  If you #define NO_IEEE_Scale on a machine that uses IEEE-format
00145  *  floating-point numbers and flushes underflows to zero rather
00146  *  than implementing gradual underflow, then you must also #define
00147  *  Sudden_Underflow.
00148  * #define YES_ALIAS to permit aliasing certain double values with
00149  *  arrays of ULongs.  This leads to slightly better code with
00150  *  some compilers and was always used prior to 19990916, but it
00151  *  is not strictly legal and can cause trouble with aggressively
00152  *  optimizing compilers (e.g., gcc 2.95.1 under -O2).
00153  * #define USE_LOCALE to use the current locale's decimal_point value.
00154  * #define SET_INEXACT if IEEE arithmetic is being used and extra
00155  *  computation should be done to set the inexact flag when the
00156  *  result is inexact and avoid setting inexact when the result
00157  *  is exact.  In this case, dtoa.c must be compiled in
00158  *  an environment, perhaps provided by #include "dtoa.c" in a
00159  *  suitable wrapper, that defines two functions,
00160  *      int get_inexact(void);
00161  *      void clear_inexact(void);
00162  *  such that get_inexact() returns a nonzero value if the
00163  *  inexact bit is already set, and clear_inexact() sets the
00164  *  inexact bit to 0.  When SET_INEXACT is #defined, strtod
00165  *  also does extra computations to set the underflow and overflow
00166  *  flags when appropriate (i.e., when the result is tiny and
00167  *  inexact or when it is a numeric value rounded to +-infinity).
00168  * #define NO_ERRNO if strtod should not assign errno = ERANGE when
00169  *  the result overflows to +-Infinity or underflows to 0.
00170  */
00171 
00172 // Put this before anything else that may import a definition of CONST. CONST from grammar.cpp conflicts with this.
00173 #ifdef KDE_USE_FINAL
00174 #undef CONST
00175 #endif
00176 
00177 #include <config.h>
00178 
00179 #include "stdlib.h"
00180 
00181 #ifdef WORDS_BIGENDIAN
00182 #define IEEE_MC68k
00183 #else
00184 #define IEEE_8087
00185 #endif
00186 #define INFNAN_CHECK
00187 #include "dtoa.h"
00188 
00189 
00190 
00191 #ifndef Long
00192 #define Long int
00193 #endif
00194 #ifndef ULong
00195 typedef unsigned Long ULong;
00196 #endif
00197 
00198 #ifdef DEBUG
00199 #include "stdio.h"
00200 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
00201 #endif
00202 
00203 #include "string.h"
00204 
00205 #ifdef USE_LOCALE
00206 #include "locale.h"
00207 #endif
00208 
00209 #ifdef MALLOC
00210 #ifdef KR_headers
00211 extern char *MALLOC();
00212 #else
00213 extern void *MALLOC(size_t);
00214 #endif
00215 #else
00216 #define MALLOC malloc
00217 #endif
00218 
00219 #ifndef Omit_Private_Memory
00220 #ifndef PRIVATE_MEM
00221 #define PRIVATE_MEM 2304
00222 #endif
00223 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
00224 static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
00225 #endif
00226 
00227 #undef IEEE_Arith
00228 #undef Avoid_Underflow
00229 #ifdef IEEE_MC68k
00230 #define IEEE_Arith
00231 #endif
00232 #ifdef IEEE_8087
00233 #define IEEE_Arith
00234 #endif
00235 
00236 #include "errno.h"
00237 
00238 #ifdef Bad_float_h
00239 
00240 #ifdef IEEE_Arith
00241 #define DBL_DIG 15
00242 #define DBL_MAX_10_EXP 308
00243 #define DBL_MAX_EXP 1024
00244 #define FLT_RADIX 2
00245 #endif /*IEEE_Arith*/
00246 
00247 #ifdef IBM
00248 #define DBL_DIG 16
00249 #define DBL_MAX_10_EXP 75
00250 #define DBL_MAX_EXP 63
00251 #define FLT_RADIX 16
00252 #define DBL_MAX 7.2370055773322621e+75
00253 #endif
00254 
00255 #ifdef VAX
00256 #define DBL_DIG 16
00257 #define DBL_MAX_10_EXP 38
00258 #define DBL_MAX_EXP 127
00259 #define FLT_RADIX 2
00260 #define DBL_MAX 1.7014118346046923e+38
00261 #endif
00262 
00263 #else /* ifndef Bad_float_h */
00264 #include "float.h"
00265 #endif /* Bad_float_h */
00266 
00267 #ifndef __MATH_H__
00268 #include "math.h"
00269 #endif
00270 
00271 #ifdef __cplusplus
00272 extern "C" {
00273 #endif
00274 
00275 #ifndef CONST
00276 #ifdef KR_headers
00277 #define CONST /* blank */
00278 #else
00279 #define CONST const
00280 #endif
00281 #endif
00282 
00283 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
00284 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
00285 #endif
00286 
00287 typedef union { double d; ULong L[2]; } U;
00288 
00289 #ifdef YES_ALIAS
00290 #define dval(x) x
00291 #ifdef IEEE_8087
00292 #define word0(x) ((ULong *)&x)[1]
00293 #define word1(x) ((ULong *)&x)[0]
00294 #else
00295 #define word0(x) ((ULong *)&x)[0]
00296 #define word1(x) ((ULong *)&x)[1]
00297 #endif
00298 #else
00299 #ifdef IEEE_8087
00300 #define word0(x) ((U*)&x)->L[1]
00301 #define word1(x) ((U*)&x)->L[0]
00302 #else
00303 #define word0(x) ((U*)&x)->L[0]
00304 #define word1(x) ((U*)&x)->L[1]
00305 #endif
00306 #define dval(x) ((U*)&x)->d
00307 #endif
00308 
00309 /* The following definition of Storeinc is appropriate for MIPS processors.
00310  * An alternative that might be better on some machines is
00311  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
00312  */
00313 #if defined(IEEE_8087) + defined(VAX)
00314 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
00315 ((unsigned short *)a)[0] = (unsigned short)c, a++)
00316 #else
00317 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
00318 ((unsigned short *)a)[1] = (unsigned short)c, a++)
00319 #endif
00320 
00321 /* #define P DBL_MANT_DIG */
00322 /* Ten_pmax = floor(P*log(2)/log(5)) */
00323 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
00324 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
00325 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
00326 
00327 #ifdef IEEE_Arith
00328 #define Exp_shift  20
00329 #define Exp_shift1 20
00330 #define Exp_msk1    0x100000
00331 #define Exp_msk11   0x100000
00332 #define Exp_mask  0x7ff00000
00333 #define P 53
00334 #define Bias 1023
00335 #define Emin (-1022)
00336 #define Exp_1  0x3ff00000
00337 #define Exp_11 0x3ff00000
00338 #define Ebits 11
00339 #define Frac_mask  0xfffff
00340 #define Frac_mask1 0xfffff
00341 #define Ten_pmax 22
00342 #define Bletch 0x10
00343 #define Bndry_mask  0xfffff
00344 #define Bndry_mask1 0xfffff
00345 #define LSB 1
00346 #define Sign_bit 0x80000000
00347 #define Log2P 1
00348 #define Tiny0 0
00349 #define Tiny1 1
00350 #define Quick_max 14
00351 #define Int_max 14
00352 #ifndef NO_IEEE_Scale
00353 #define Avoid_Underflow
00354 #ifdef Flush_Denorm /* debugging option */
00355 #undef Sudden_Underflow
00356 #endif
00357 #endif
00358 
00359 #ifndef Flt_Rounds
00360 #ifdef FLT_ROUNDS
00361 #define Flt_Rounds FLT_ROUNDS
00362 #else
00363 #define Flt_Rounds 1
00364 #endif
00365 #endif /*Flt_Rounds*/
00366 
00367 #ifdef Honor_FLT_ROUNDS
00368 #define Rounding rounding
00369 #undef Check_FLT_ROUNDS
00370 #define Check_FLT_ROUNDS
00371 #else
00372 #define Rounding Flt_Rounds
00373 #endif
00374 
00375 #else /* ifndef IEEE_Arith */
00376 #undef Check_FLT_ROUNDS
00377 #undef Honor_FLT_ROUNDS
00378 #undef SET_INEXACT
00379 #undef  Sudden_Underflow
00380 #define Sudden_Underflow
00381 #ifdef IBM
00382 #undef Flt_Rounds
00383 #define Flt_Rounds 0
00384 #define Exp_shift  24
00385 #define Exp_shift1 24
00386 #define Exp_msk1   0x1000000
00387 #define Exp_msk11  0x1000000
00388 #define Exp_mask  0x7f000000
00389 #define P 14
00390 #define Bias 65
00391 #define Exp_1  0x41000000
00392 #define Exp_11 0x41000000
00393 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
00394 #define Frac_mask  0xffffff
00395 #define Frac_mask1 0xffffff
00396 #define Bletch 4
00397 #define Ten_pmax 22
00398 #define Bndry_mask  0xefffff
00399 #define Bndry_mask1 0xffffff
00400 #define LSB 1
00401 #define Sign_bit 0x80000000
00402 #define Log2P 4
00403 #define Tiny0 0x100000
00404 #define Tiny1 0
00405 #define Quick_max 14
00406 #define Int_max 15
00407 #else /* VAX */
00408 #undef Flt_Rounds
00409 #define Flt_Rounds 1
00410 #define Exp_shift  23
00411 #define Exp_shift1 7
00412 #define Exp_msk1    0x80
00413 #define Exp_msk11   0x800000
00414 #define Exp_mask  0x7f80
00415 #define P 56
00416 #define Bias 129
00417 #define Exp_1  0x40800000
00418 #define Exp_11 0x4080
00419 #define Ebits 8
00420 #define Frac_mask  0x7fffff
00421 #define Frac_mask1 0xffff007f
00422 #define Ten_pmax 24
00423 #define Bletch 2
00424 #define Bndry_mask  0xffff007f
00425 #define Bndry_mask1 0xffff007f
00426 #define LSB 0x10000
00427 #define Sign_bit 0x8000
00428 #define Log2P 1
00429 #define Tiny0 0x80
00430 #define Tiny1 0
00431 #define Quick_max 15
00432 #define Int_max 15
00433 #endif /* IBM, VAX */
00434 #endif /* IEEE_Arith */
00435 
00436 #ifndef IEEE_Arith
00437 #define ROUND_BIASED
00438 #endif
00439 
00440 #ifdef RND_PRODQUOT
00441 #define rounded_product(a,b) a = rnd_prod(a, b)
00442 #define rounded_quotient(a,b) a = rnd_quot(a, b)
00443 #ifdef KR_headers
00444 extern double rnd_prod(), rnd_quot();
00445 #else
00446 extern double rnd_prod(double, double), rnd_quot(double, double);
00447 #endif
00448 #else
00449 #define rounded_product(a,b) a *= b
00450 #define rounded_quotient(a,b) a /= b
00451 #endif
00452 
00453 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
00454 #define Big1 0xffffffff
00455 
00456 #ifndef Pack_32
00457 #define Pack_32
00458 #endif
00459 
00460 #ifdef KR_headers
00461 #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
00462 #else
00463 #define FFFFFFFF 0xffffffffUL
00464 #endif
00465 
00466 #ifdef NO_LONG_LONG
00467 #undef ULLong
00468 #ifdef Just_16
00469 #undef Pack_32
00470 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
00471  * This makes some inner loops simpler and sometimes saves work
00472  * during multiplications, but it often seems to make things slightly
00473  * slower.  Hence the default is now to store 32 bits per Long.
00474  */
00475 #endif
00476 #else   /* long long available */
00477 #ifndef Llong
00478 #define Llong long long
00479 #endif
00480 #ifndef ULLong
00481 #define ULLong unsigned Llong
00482 #endif
00483 #endif /* NO_LONG_LONG */
00484 
00485 #ifndef MULTIPLE_THREADS
00486 #define ACQUIRE_DTOA_LOCK(n)    /*nothing*/
00487 #define FREE_DTOA_LOCK(n)   /*nothing*/
00488 #endif
00489 
00490 #define Kmax 15
00491 
00492  struct
00493 Bigint {
00494     struct Bigint *next;
00495     int k, maxwds, sign, wds;
00496     ULong x[1];
00497     };
00498 
00499  typedef struct Bigint Bigint;
00500 
00501  static Bigint *freelist[Kmax+1];
00502 
00503  static Bigint *
00504 Balloc
00505 #ifdef KR_headers
00506     (k) int k;
00507 #else
00508     (int k)
00509 #endif
00510 {
00511     int x;
00512     Bigint *rv;
00513 #ifndef Omit_Private_Memory
00514     unsigned int len;
00515 #endif
00516 
00517     ACQUIRE_DTOA_LOCK(0);
00518     if ((rv = freelist[k])) {
00519         freelist[k] = rv->next;
00520         }
00521     else {
00522         x = 1 << k;
00523 #ifdef Omit_Private_Memory
00524         rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
00525 #else
00526         len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
00527             /sizeof(double);
00528         if (pmem_next - private_mem + len <= PRIVATE_mem) {
00529             rv = (Bigint*)pmem_next;
00530             pmem_next += len;
00531             }
00532         else
00533             rv = (Bigint*)MALLOC(len*sizeof(double));
00534 #endif
00535         rv->k = k;
00536         rv->maxwds = x;
00537         }
00538     FREE_DTOA_LOCK(0);
00539     rv->sign = rv->wds = 0;
00540     return rv;
00541     }
00542 
00543  static void
00544 Bfree
00545 #ifdef KR_headers
00546     (v) Bigint *v;
00547 #else
00548     (Bigint *v)
00549 #endif
00550 {
00551     if (v) {
00552         ACQUIRE_DTOA_LOCK(0);
00553         v->next = freelist[v->k];
00554         freelist[v->k] = v;
00555         FREE_DTOA_LOCK(0);
00556         }
00557     }
00558 
00559 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
00560 y->wds*sizeof(Long) + 2*sizeof(int))
00561 
00562  static Bigint *
00563 multadd
00564 #ifdef KR_headers
00565     (b, m, a) Bigint *b; int m, a;
00566 #else
00567     (Bigint *b, int m, int a)   /* multiply by m and add a */
00568 #endif
00569 {
00570     int i, wds;
00571 #ifdef ULLong
00572     ULong *x;
00573     ULLong carry, y;
00574 #else
00575     ULong carry, *x, y;
00576 #ifdef Pack_32
00577     ULong xi, z;
00578 #endif
00579 #endif
00580     Bigint *b1;
00581 
00582     wds = b->wds;
00583     x = b->x;
00584     i = 0;
00585     carry = a;
00586     do {
00587 #ifdef ULLong
00588         y = *x * (ULLong)m + carry;
00589         carry = y >> 32;
00590         *x++ = y & FFFFFFFF;
00591 #else
00592 #ifdef Pack_32
00593         xi = *x;
00594         y = (xi & 0xffff) * m + carry;
00595         z = (xi >> 16) * m + (y >> 16);
00596         carry = z >> 16;
00597         *x++ = (z << 16) + (y & 0xffff);
00598 #else
00599         y = *x * m + carry;
00600         carry = y >> 16;
00601         *x++ = y & 0xffff;
00602 #endif
00603 #endif
00604         }
00605         while(++i < wds);
00606     if (carry) {
00607         if (wds >= b->maxwds) {
00608             b1 = Balloc(b->k+1);
00609             Bcopy(b1, b);
00610             Bfree(b);
00611             b = b1;
00612             }
00613         b->x[wds++] = carry;
00614         b->wds = wds;
00615         }
00616     return b;
00617     }
00618 
00619  static Bigint *
00620 s2b
00621 #ifdef KR_headers
00622     (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
00623 #else
00624     (CONST char *s, int nd0, int nd, ULong y9)
00625 #endif
00626 {
00627     Bigint *b;
00628     int i, k;
00629     Long x, y;
00630 
00631     x = (nd + 8) / 9;
00632     for(k = 0, y = 1; x > y; y <<= 1, k++) ;
00633 #ifdef Pack_32
00634     b = Balloc(k);
00635     b->x[0] = y9;
00636     b->wds = 1;
00637 #else
00638     b = Balloc(k+1);
00639     b->x[0] = y9 & 0xffff;
00640     b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
00641 #endif
00642 
00643     i = 9;
00644     if (9 < nd0) {
00645         s += 9;
00646         do b = multadd(b, 10, *s++ - '0');
00647             while(++i < nd0);
00648         s++;
00649         }
00650     else
00651         s += 10;
00652     for(; i < nd; i++)
00653         b = multadd(b, 10, *s++ - '0');
00654     return b;
00655     }
00656 
00657  static int
00658 hi0bits
00659 #ifdef KR_headers
00660     (x) register ULong x;
00661 #else
00662     (register ULong x)
00663 #endif
00664 {
00665     register int k = 0;
00666 
00667     if (!(x & 0xffff0000)) {
00668         k = 16;
00669         x <<= 16;
00670         }
00671     if (!(x & 0xff000000)) {
00672         k += 8;
00673         x <<= 8;
00674         }
00675     if (!(x & 0xf0000000)) {
00676         k += 4;
00677         x <<= 4;
00678         }
00679     if (!(x & 0xc0000000)) {
00680         k += 2;
00681         x <<= 2;
00682         }
00683     if (!(x & 0x80000000)) {
00684         k++;
00685         if (!(x & 0x40000000))
00686             return 32;
00687         }
00688     return k;
00689     }
00690 
00691  static int
00692 lo0bits
00693 #ifdef KR_headers
00694     (y) ULong *y;
00695 #else
00696     (ULong *y)
00697 #endif
00698 {
00699     register int k;
00700     register ULong x = *y;
00701 
00702     if (x & 7) {
00703         if (x & 1)
00704             return 0;
00705         if (x & 2) {
00706             *y = x >> 1;
00707             return 1;
00708             }
00709         *y = x >> 2;
00710         return 2;
00711         }
00712     k = 0;
00713     if (!(x & 0xffff)) {
00714         k = 16;
00715         x >>= 16;
00716         }
00717     if (!(x & 0xff)) {
00718         k += 8;
00719         x >>= 8;
00720         }
00721     if (!(x & 0xf)) {
00722         k += 4;
00723         x >>= 4;
00724         }
00725     if (!(x & 0x3)) {
00726         k += 2;
00727         x >>= 2;
00728         }
00729     if (!(x & 1)) {
00730         k++;
00731         x >>= 1;
00732         if (!x & 1)
00733             return 32;
00734         }
00735     *y = x;
00736     return k;
00737     }
00738 
00739  static Bigint *
00740 i2b
00741 #ifdef KR_headers
00742     (i) int i;
00743 #else
00744     (int i)
00745 #endif
00746 {
00747     Bigint *b;
00748 
00749     b = Balloc(1);
00750     b->x[0] = i;
00751     b->wds = 1;
00752     return b;
00753     }
00754 
00755  static Bigint *
00756 mult
00757 #ifdef KR_headers
00758     (a, b) Bigint *a, *b;
00759 #else
00760     (Bigint *a, Bigint *b)
00761 #endif
00762 {
00763     Bigint *c;
00764     int k, wa, wb, wc;
00765     ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
00766     ULong y;
00767 #ifdef ULLong
00768     ULLong carry, z;
00769 #else
00770     ULong carry, z;
00771 #ifdef Pack_32
00772     ULong z2;
00773 #endif
00774 #endif
00775 
00776     if (a->wds < b->wds) {
00777         c = a;
00778         a = b;
00779         b = c;
00780         }
00781     k = a->k;
00782     wa = a->wds;
00783     wb = b->wds;
00784     wc = wa + wb;
00785     if (wc > a->maxwds)
00786         k++;
00787     c = Balloc(k);
00788     for(x = c->x, xa = x + wc; x < xa; x++)
00789         *x = 0;
00790     xa = a->x;
00791     xae = xa + wa;
00792     xb = b->x;
00793     xbe = xb + wb;
00794     xc0 = c->x;
00795 #ifdef ULLong
00796     for(; xb < xbe; xc0++) {
00797         if ((y = *xb++)) {
00798             x = xa;
00799             xc = xc0;
00800             carry = 0;
00801             do {
00802                 z = *x++ * (ULLong)y + *xc + carry;
00803                 carry = z >> 32;
00804                 *xc++ = z & FFFFFFFF;
00805                 }
00806                 while(x < xae);
00807             *xc = carry;
00808             }
00809         }
00810 #else
00811 #ifdef Pack_32
00812     for(; xb < xbe; xb++, xc0++) {
00813         if (y = *xb & 0xffff) {
00814             x = xa;
00815             xc = xc0;
00816             carry = 0;
00817             do {
00818                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
00819                 carry = z >> 16;
00820                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
00821                 carry = z2 >> 16;
00822                 Storeinc(xc, z2, z);
00823                 }
00824                 while(x < xae);
00825             *xc = carry;
00826             }
00827         if (y = *xb >> 16) {
00828             x = xa;
00829             xc = xc0;
00830             carry = 0;
00831             z2 = *xc;
00832             do {
00833                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
00834                 carry = z >> 16;
00835                 Storeinc(xc, z, z2);
00836                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
00837                 carry = z2 >> 16;
00838                 }
00839                 while(x < xae);
00840             *xc = z2;
00841             }
00842         }
00843 #else
00844     for(; xb < xbe; xc0++) {
00845         if (y = *xb++) {
00846             x = xa;
00847             xc = xc0;
00848             carry = 0;
00849             do {
00850                 z = *x++ * y + *xc + carry;
00851                 carry = z >> 16;
00852                 *xc++ = z & 0xffff;
00853                 }
00854                 while(x < xae);
00855             *xc = carry;
00856             }
00857         }
00858 #endif
00859 #endif
00860     for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
00861     c->wds = wc;
00862     return c;
00863     }
00864 
00865  static Bigint *p5s;
00866 
00867  static Bigint *
00868 pow5mult
00869 #ifdef KR_headers
00870     (b, k) Bigint *b; int k;
00871 #else
00872     (Bigint *b, int k)
00873 #endif
00874 {
00875     Bigint *b1, *p5, *p51;
00876     int i;
00877     static int p05[3] = { 5, 25, 125 };
00878 
00879     if ((i = k & 3))
00880         b = multadd(b, p05[i-1], 0);
00881 
00882     if (!(k >>= 2))
00883         return b;
00884     if (!(p5 = p5s)) {
00885         /* first time */
00886 #ifdef MULTIPLE_THREADS
00887         ACQUIRE_DTOA_LOCK(1);
00888         if (!(p5 = p5s)) {
00889             p5 = p5s = i2b(625);
00890             p5->next = 0;
00891             }
00892         FREE_DTOA_LOCK(1);
00893 #else
00894         p5 = p5s = i2b(625);
00895         p5->next = 0;
00896 #endif
00897         }
00898     for(;;) {
00899         if (k & 1) {
00900             b1 = mult(b, p5);
00901             Bfree(b);
00902             b = b1;
00903             }
00904         if (!(k >>= 1))
00905             break;
00906         if (!(p51 = p5->next)) {
00907 #ifdef MULTIPLE_THREADS
00908             ACQUIRE_DTOA_LOCK(1);
00909             if (!(p51 = p5->next)) {
00910                 p51 = p5->next = mult(p5,p5);
00911                 p51->next = 0;
00912                 }
00913             FREE_DTOA_LOCK(1);
00914 #else
00915             p51 = p5->next = mult(p5,p5);
00916             p51->next = 0;
00917 #endif
00918             }
00919         p5 = p51;
00920         }
00921     return b;
00922     }
00923 
00924  static Bigint *
00925 lshift
00926 #ifdef KR_headers
00927     (b, k) Bigint *b; int k;
00928 #else
00929     (Bigint *b, int k)
00930 #endif
00931 {
00932     int i, k1, n, n1;
00933     Bigint *b1;
00934     ULong *x, *x1, *xe, z;
00935 
00936 #ifdef Pack_32
00937     n = k >> 5;
00938 #else
00939     n = k >> 4;
00940 #endif
00941     k1 = b->k;
00942     n1 = n + b->wds + 1;
00943     for(i = b->maxwds; n1 > i; i <<= 1)
00944         k1++;
00945     b1 = Balloc(k1);
00946     x1 = b1->x;
00947     for(i = 0; i < n; i++)
00948         *x1++ = 0;
00949     x = b->x;
00950     xe = x + b->wds;
00951 #ifdef Pack_32
00952     if (k &= 0x1f) {
00953         k1 = 32 - k;
00954         z = 0;
00955         do {
00956             *x1++ = *x << k | z;
00957             z = *x++ >> k1;
00958             }
00959             while(x < xe);
00960         if ((*x1 = z))
00961             ++n1;
00962         }
00963 #else
00964     if (k &= 0xf) {
00965         k1 = 16 - k;
00966         z = 0;
00967         do {
00968             *x1++ = *x << k  & 0xffff | z;
00969             z = *x++ >> k1;
00970             }
00971             while(x < xe);
00972         if (*x1 = z)
00973             ++n1;
00974         }
00975 #endif
00976     else do
00977         *x1++ = *x++;
00978         while(x < xe);
00979     b1->wds = n1 - 1;
00980     Bfree(b);
00981     return b1;
00982     }
00983 
00984  static int
00985 cmp
00986 #ifdef KR_headers
00987     (a, b) Bigint *a, *b;
00988 #else
00989     (Bigint *a, Bigint *b)
00990 #endif
00991 {
00992     ULong *xa, *xa0, *xb, *xb0;
00993     int i, j;
00994 
00995     i = a->wds;
00996     j = b->wds;
00997 #ifdef DEBUG
00998     if (i > 1 && !a->x[i-1])
00999         Bug("cmp called with a->x[a->wds-1] == 0");
01000     if (j > 1 && !b->x[j-1])
01001         Bug("cmp called with b->x[b->wds-1] == 0");
01002 #endif
01003     if (i -= j)
01004         return i;
01005     xa0 = a->x;
01006     xa = xa0 + j;
01007     xb0 = b->x;
01008     xb = xb0 + j;
01009     for(;;) {
01010         if (*--xa != *--xb)
01011             return *xa < *xb ? -1 : 1;
01012         if (xa <= xa0)
01013             break;
01014         }
01015     return 0;
01016     }
01017 
01018  static Bigint *
01019 diff
01020 #ifdef KR_headers
01021     (a, b) Bigint *a, *b;
01022 #else
01023     (Bigint *a, Bigint *b)
01024 #endif
01025 {
01026     Bigint *c;
01027     int i, wa, wb;
01028     ULong *xa, *xae, *xb, *xbe, *xc;
01029 #ifdef ULLong
01030     ULLong borrow, y;
01031 #else
01032     ULong borrow, y;
01033 #ifdef Pack_32
01034     ULong z;
01035 #endif
01036 #endif
01037 
01038     i = cmp(a,b);
01039     if (!i) {
01040         c = Balloc(0);
01041         c->wds = 1;
01042         c->x[0] = 0;
01043         return c;
01044         }
01045     if (i < 0) {
01046         c = a;
01047         a = b;
01048         b = c;
01049         i = 1;
01050         }
01051     else
01052         i = 0;
01053     c = Balloc(a->k);
01054     c->sign = i;
01055     wa = a->wds;
01056     xa = a->x;
01057     xae = xa + wa;
01058     wb = b->wds;
01059     xb = b->x;
01060     xbe = xb + wb;
01061     xc = c->x;
01062     borrow = 0;
01063 #ifdef ULLong
01064     do {
01065         y = (ULLong)*xa++ - *xb++ - borrow;
01066         borrow = y >> 32 & (ULong)1;
01067         *xc++ = y & FFFFFFFF;
01068         }
01069         while(xb < xbe);
01070     while(xa < xae) {
01071         y = *xa++ - borrow;
01072         borrow = y >> 32 & (ULong)1;
01073         *xc++ = y & FFFFFFFF;
01074         }
01075 #else
01076 #ifdef Pack_32
01077     do {
01078         y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
01079         borrow = (y & 0x10000) >> 16;
01080         z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
01081         borrow = (z & 0x10000) >> 16;
01082         Storeinc(xc, z, y);
01083         }
01084         while(xb < xbe);
01085     while(xa < xae) {
01086         y = (*xa & 0xffff) - borrow;
01087         borrow = (y & 0x10000) >> 16;
01088         z = (*xa++ >> 16) - borrow;
01089         borrow = (z & 0x10000) >> 16;
01090         Storeinc(xc, z, y);
01091         }
01092 #else
01093     do {
01094         y = *xa++ - *xb++ - borrow;
01095         borrow = (y & 0x10000) >> 16;
01096         *xc++ = y & 0xffff;
01097         }
01098         while(xb < xbe);
01099     while(xa < xae) {
01100         y = *xa++ - borrow;
01101         borrow = (y & 0x10000) >> 16;
01102         *xc++ = y & 0xffff;
01103         }
01104 #endif
01105 #endif
01106     while(!*--xc)
01107         wa--;
01108     c->wds = wa;
01109     return c;
01110     }
01111 
01112  static double
01113 ulp
01114 #ifdef KR_headers
01115     (x) double x;
01116 #else
01117     (double x)
01118 #endif
01119 {
01120     register Long L;
01121     double a;
01122 
01123     L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
01124 #ifndef Avoid_Underflow
01125 #ifndef Sudden_Underflow
01126     if (L > 0) {
01127 #endif
01128 #endif
01129 #ifdef IBM
01130         L |= Exp_msk1 >> 4;
01131 #endif
01132         word0(a) = L;
01133         word1(a) = 0;
01134 #ifndef Avoid_Underflow
01135 #ifndef Sudden_Underflow
01136         }
01137     else {
01138         L = -L >> Exp_shift;
01139         if (L < Exp_shift) {
01140             word0(a) = 0x80000 >> L;
01141             word1(a) = 0;
01142             }
01143         else {
01144             word0(a) = 0;
01145             L -= Exp_shift;
01146             word1(a) = L >= 31 ? 1 : 1 << 31 - L;
01147             }
01148         }
01149 #endif
01150 #endif
01151     return dval(a);
01152     }
01153 
01154  static double
01155 b2d
01156 #ifdef KR_headers
01157     (a, e) Bigint *a; int *e;
01158 #else
01159     (Bigint *a, int *e)
01160 #endif
01161 {
01162     ULong *xa, *xa0, w, y, z;
01163     int k;
01164     double d;
01165 #ifdef VAX
01166     ULong d0, d1;
01167 #else
01168 #define d0 word0(d)
01169 #define d1 word1(d)
01170 #endif
01171 
01172     xa0 = a->x;
01173     xa = xa0 + a->wds;
01174     y = *--xa;
01175 #ifdef DEBUG
01176     if (!y) Bug("zero y in b2d");
01177 #endif
01178     k = hi0bits(y);
01179     *e = 32 - k;
01180 #ifdef Pack_32
01181     if (k < Ebits) {
01182         d0 = Exp_1 | y >> Ebits - k;
01183         w = xa > xa0 ? *--xa : 0;
01184         d1 = y << (32-Ebits) + k | w >> Ebits - k;
01185         goto ret_d;
01186         }
01187     z = xa > xa0 ? *--xa : 0;
01188     if (k -= Ebits) {
01189         d0 = Exp_1 | y << k | z >> 32 - k;
01190         y = xa > xa0 ? *--xa : 0;
01191         d1 = z << k | y >> 32 - k;
01192         }
01193     else {
01194         d0 = Exp_1 | y;
01195         d1 = z;
01196         }
01197 #else
01198     if (k < Ebits + 16) {
01199         z = xa > xa0 ? *--xa : 0;
01200         d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
01201         w = xa > xa0 ? *--xa : 0;
01202         y = xa > xa0 ? *--xa : 0;
01203         d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
01204         goto ret_d;
01205         }
01206     z = xa > xa0 ? *--xa : 0;
01207     w = xa > xa0 ? *--xa : 0;
01208     k -= Ebits + 16;
01209     d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
01210     y = xa > xa0 ? *--xa : 0;
01211     d1 = w << k + 16 | y << k;
01212 #endif
01213  ret_d:
01214 #ifdef VAX
01215     word0(d) = d0 >> 16 | d0 << 16;
01216     word1(d) = d1 >> 16 | d1 << 16;
01217 #else
01218 #undef d0
01219 #undef d1
01220 #endif
01221     return dval(d);
01222     }
01223 
01224  static Bigint *
01225 d2b
01226 #ifdef KR_headers
01227     (d, e, bits) double d; int *e, *bits;
01228 #else
01229     (double d, int *e, int *bits)
01230 #endif
01231 {
01232     Bigint *b;
01233     int de, k;
01234     ULong *x, y, z;
01235 #ifndef Sudden_Underflow
01236     int i;
01237 #endif
01238 #ifdef VAX
01239     ULong d0, d1;
01240     d0 = word0(d) >> 16 | word0(d) << 16;
01241     d1 = word1(d) >> 16 | word1(d) << 16;
01242 #else
01243 #define d0 word0(d)
01244 #define d1 word1(d)
01245 #endif
01246 
01247 #ifdef Pack_32
01248     b = Balloc(1);
01249 #else
01250     b = Balloc(2);
01251 #endif
01252     x = b->x;
01253 
01254     z = d0 & Frac_mask;
01255     d0 &= 0x7fffffff;   /* clear sign bit, which we ignore */
01256 #ifdef Sudden_Underflow
01257     de = (int)(d0 >> Exp_shift);
01258 #ifndef IBM
01259     z |= Exp_msk11;
01260 #endif
01261 #else
01262     if ((de = (int)(d0 >> Exp_shift)))
01263         z |= Exp_msk1;
01264 #endif
01265 #ifdef Pack_32
01266     if ((y = d1)) {
01267         if ((k = lo0bits(&y))) {
01268             x[0] = y | z << 32 - k;
01269             z >>= k;
01270             }
01271         else
01272             x[0] = y;
01273 #ifndef Sudden_Underflow
01274         i =
01275 #endif
01276             b->wds = (x[1] = z) ? 2 : 1;
01277         }
01278     else {
01279 #ifdef DEBUG
01280         if (!z)
01281             Bug("Zero passed to d2b");
01282 #endif
01283         k = lo0bits(&z);
01284         x[0] = z;
01285 #ifndef Sudden_Underflow
01286         i =
01287 #endif
01288             b->wds = 1;
01289         k += 32;
01290         }
01291 #else
01292     if (y = d1) {
01293         if (k = lo0bits(&y))
01294             if (k >= 16) {
01295                 x[0] = y | z << 32 - k & 0xffff;
01296                 x[1] = z >> k - 16 & 0xffff;
01297                 x[2] = z >> k;
01298                 i = 2;
01299                 }
01300             else {
01301                 x[0] = y & 0xffff;
01302                 x[1] = y >> 16 | z << 16 - k & 0xffff;
01303                 x[2] = z >> k & 0xffff;
01304                 x[3] = z >> k+16;
01305                 i = 3;
01306                 }
01307         else {
01308             x[0] = y & 0xffff;
01309             x[1] = y >> 16;
01310             x[2] = z & 0xffff;
01311             x[3] = z >> 16;
01312             i = 3;
01313             }
01314         }
01315     else {
01316 #ifdef DEBUG
01317         if (!z)
01318             Bug("Zero passed to d2b");
01319 #endif
01320         k = lo0bits(&z);
01321         if (k >= 16) {
01322             x[0] = z;
01323             i = 0;
01324             }
01325         else {
01326             x[0] = z & 0xffff;
01327             x[1] = z >> 16;
01328             i = 1;
01329             }
01330         k += 32;
01331         }
01332     while(!x[i])
01333         --i;
01334     b->wds = i + 1;
01335 #endif
01336 #ifndef Sudden_Underflow
01337     if (de) {
01338 #endif
01339 #ifdef IBM
01340         *e = (de - Bias - (P-1) << 2) + k;
01341         *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
01342 #else
01343         *e = de - Bias - (P-1) + k;
01344         *bits = P - k;
01345 #endif
01346 #ifndef Sudden_Underflow
01347         }
01348     else {
01349         *e = de - Bias - (P-1) + 1 + k;
01350 #ifdef Pack_32
01351         *bits = 32*i - hi0bits(x[i-1]);
01352 #else
01353         *bits = (i+2)*16 - hi0bits(x[i]);
01354 #endif
01355         }
01356 #endif
01357     return b;
01358     }
01359 #undef d0
01360 #undef d1
01361 
01362  static double
01363 ratio
01364 #ifdef KR_headers
01365     (a, b) Bigint *a, *b;
01366 #else
01367     (Bigint *a, Bigint *b)
01368 #endif
01369 {
01370     double da, db;
01371     int k, ka, kb;
01372 
01373     dval(da) = b2d(a, &ka);
01374     dval(db) = b2d(b, &kb);
01375 #ifdef Pack_32
01376     k = ka - kb + 32*(a->wds - b->wds);
01377 #else
01378     k = ka - kb + 16*(a->wds - b->wds);
01379 #endif
01380 #ifdef IBM
01381     if (k > 0) {
01382         word0(da) += (k >> 2)*Exp_msk1;
01383         if (k &= 3)
01384             dval(da) *= 1 << k;
01385         }
01386     else {
01387         k = -k;
01388         word0(db) += (k >> 2)*Exp_msk1;
01389         if (k &= 3)
01390             dval(db) *= 1 << k;
01391         }
01392 #else
01393     if (k > 0)
01394         word0(da) += k*Exp_msk1;
01395     else {
01396         k = -k;
01397         word0(db) += k*Exp_msk1;
01398         }
01399 #endif
01400     return dval(da) / dval(db);
01401     }
01402 
01403  static CONST double
01404 tens[] = {
01405         1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
01406         1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
01407         1e20, 1e21, 1e22
01408 #ifdef VAX
01409         , 1e23, 1e24
01410 #endif
01411         };
01412 
01413  static CONST double
01414 #ifdef IEEE_Arith
01415 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
01416 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
01417 #ifdef Avoid_Underflow
01418         9007199254740992.*9007199254740992.e-256
01419         /* = 2^106 * 1e-53 */
01420 #else
01421         1e-256
01422 #endif
01423         };
01424 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
01425 /* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
01426 #define Scale_Bit 0x10
01427 #define n_bigtens 5
01428 #else
01429 #ifdef IBM
01430 bigtens[] = { 1e16, 1e32, 1e64 };
01431 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
01432 #define n_bigtens 3
01433 #else
01434 bigtens[] = { 1e16, 1e32 };
01435 static CONST double tinytens[] = { 1e-16, 1e-32 };
01436 #define n_bigtens 2
01437 #endif
01438 #endif
01439 
01440 #ifndef IEEE_Arith
01441 #undef INFNAN_CHECK
01442 #endif
01443 
01444 #ifdef INFNAN_CHECK
01445 
01446 #ifndef NAN_WORD0
01447 #define NAN_WORD0 0x7ff80000
01448 #endif
01449 
01450 #ifndef NAN_WORD1
01451 #define NAN_WORD1 0
01452 #endif
01453 
01454  static int
01455 match
01456 #ifdef KR_headers
01457     (sp, t) char **sp, *t;
01458 #else
01459     (CONST char **sp, CONST char *t)
01460 #endif
01461 {
01462     int c, d;
01463     CONST char *s = *sp;
01464 
01465     while((d = *t++)) {
01466         if ((c = *++s) >= 'A' && c <= 'Z')
01467             c += 'a' - 'A';
01468         if (c != d)
01469             return 0;
01470         }
01471     *sp = s + 1;
01472     return 1;
01473     }
01474 
01475 #ifndef No_Hex_NaN
01476  static void
01477 hexnan
01478 #ifdef KR_headers
01479     (rvp, sp) double *rvp; CONST char **sp;
01480 #else
01481     (double *rvp, CONST char **sp)
01482 #endif
01483 {
01484     ULong c, x[2];
01485     CONST char *s;
01486     int havedig, udx0, xshift;
01487 
01488     x[0] = x[1] = 0;
01489     havedig = xshift = 0;
01490     udx0 = 1;
01491     s = *sp;
01492     while((c = *(CONST unsigned char*)++s)) {
01493         if (c >= '0' && c <= '9')
01494             c -= '0';
01495         else if (c >= 'a' && c <= 'f')
01496             c += 10 - 'a';
01497         else if (c >= 'A' && c <= 'F')
01498             c += 10 - 'A';
01499         else if (c <= ' ') {
01500             if (udx0 && havedig) {
01501                 udx0 = 0;
01502                 xshift = 1;
01503                 }
01504             continue;
01505             }
01506         else if (/*(*/ c == ')' && havedig) {
01507             *sp = s + 1;
01508             break;
01509             }
01510         else
01511             return; /* invalid form: don't change *sp */
01512         havedig = 1;
01513         if (xshift) {
01514             xshift = 0;
01515             x[0] = x[1];
01516             x[1] = 0;
01517             }
01518         if (udx0)
01519             x[0] = (x[0] << 4) | (x[1] >> 28);
01520         x[1] = (x[1] << 4) | c;
01521         }
01522     if ((x[0] &= 0xfffff) || x[1]) {
01523         word0(*rvp) = Exp_mask | x[0];
01524         word1(*rvp) = x[1];
01525         }
01526     }
01527 #endif /*No_Hex_NaN*/
01528 #endif /* INFNAN_CHECK */
01529 
01530  double
01531 kjs_strtod
01532 #ifdef KR_headers
01533     (s00, se) CONST char *s00; char **se;
01534 #else
01535     (CONST char *s00, char **se)
01536 #endif
01537 {
01538 #ifdef Avoid_Underflow
01539     int scale;
01540 #endif
01541     int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
01542          e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
01543     CONST char *s, *s0, *s1;
01544     double aadj, aadj1, adj, rv, rv0;
01545     Long L;
01546     ULong y, z;
01547     Bigint *bb = NULL, *bb1 = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;
01548 #ifdef SET_INEXACT
01549     int inexact, oldinexact;
01550 #endif
01551 #ifdef Honor_FLT_ROUNDS
01552     int rounding;
01553 #endif
01554 #ifdef USE_LOCALE
01555     CONST char *s2;
01556 #endif
01557 
01558     sign = nz0 = nz = 0;
01559     dval(rv) = 0.;
01560     for(s = s00;;s++) switch(*s) {
01561         case '-':
01562             sign = 1;
01563             /* no break */
01564         case '+':
01565             if (*++s)
01566                 goto break2;
01567             /* no break */
01568         case 0:
01569             goto ret0;
01570         case '\t':
01571         case '\n':
01572