nsprpub/pr/src/misc/dtoa.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/misc/dtoa.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,4356 @@
     1.4 +/****************************************************************
     1.5 + *
     1.6 + * The author of this software is David M. Gay.
     1.7 + *
     1.8 + * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
     1.9 + *
    1.10 + * Permission to use, copy, modify, and distribute this software for any
    1.11 + * purpose without fee is hereby granted, provided that this entire notice
    1.12 + * is included in all copies of any software which is or includes a copy
    1.13 + * or modification of this software and in all copies of the supporting
    1.14 + * documentation for such software.
    1.15 + *
    1.16 + * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
    1.17 + * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
    1.18 + * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
    1.19 + * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
    1.20 + *
    1.21 + ***************************************************************/
    1.22 +
    1.23 +/* Please send bug reports to David M. Gay (dmg at acm dot org,
    1.24 + * with " at " changed at "@" and " dot " changed to ".").	*/
    1.25 +
    1.26 +/* On a machine with IEEE extended-precision registers, it is
    1.27 + * necessary to specify double-precision (53-bit) rounding precision
    1.28 + * before invoking strtod or dtoa.  If the machine uses (the equivalent
    1.29 + * of) Intel 80x87 arithmetic, the call
    1.30 + *	_control87(PC_53, MCW_PC);
    1.31 + * does this with many compilers.  Whether this or another call is
    1.32 + * appropriate depends on the compiler; for this to work, it may be
    1.33 + * necessary to #include "float.h" or another system-dependent header
    1.34 + * file.
    1.35 + */
    1.36 +
    1.37 +/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
    1.38 + *
    1.39 + * This strtod returns a nearest machine number to the input decimal
    1.40 + * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
    1.41 + * broken by the IEEE round-even rule.  Otherwise ties are broken by
    1.42 + * biased rounding (add half and chop).
    1.43 + *
    1.44 + * Inspired loosely by William D. Clinger's paper "How to Read Floating
    1.45 + * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
    1.46 + *
    1.47 + * Modifications:
    1.48 + *
    1.49 + *	1. We only require IEEE, IBM, or VAX double-precision
    1.50 + *		arithmetic (not IEEE double-extended).
    1.51 + *	2. We get by with floating-point arithmetic in a case that
    1.52 + *		Clinger missed -- when we're computing d * 10^n
    1.53 + *		for a small integer d and the integer n is not too
    1.54 + *		much larger than 22 (the maximum integer k for which
    1.55 + *		we can represent 10^k exactly), we may be able to
    1.56 + *		compute (d*10^k) * 10^(e-k) with just one roundoff.
    1.57 + *	3. Rather than a bit-at-a-time adjustment of the binary
    1.58 + *		result in the hard case, we use floating-point
    1.59 + *		arithmetic to determine the adjustment to within
    1.60 + *		one bit; only in really hard cases do we need to
    1.61 + *		compute a second residual.
    1.62 + *	4. Because of 3., we don't need a large table of powers of 10
    1.63 + *		for ten-to-e (just some small tables, e.g. of 10^k
    1.64 + *		for 0 <= k <= 22).
    1.65 + */
    1.66 +
    1.67 +/*
    1.68 + * #define IEEE_8087 for IEEE-arithmetic machines where the least
    1.69 + *	significant byte has the lowest address.
    1.70 + * #define IEEE_MC68k for IEEE-arithmetic machines where the most
    1.71 + *	significant byte has the lowest address.
    1.72 + * #define Long int on machines with 32-bit ints and 64-bit longs.
    1.73 + * #define IBM for IBM mainframe-style floating-point arithmetic.
    1.74 + * #define VAX for VAX-style floating-point arithmetic (D_floating).
    1.75 + * #define No_leftright to omit left-right logic in fast floating-point
    1.76 + *	computation of dtoa.  This will cause dtoa modes 4 and 5 to be
    1.77 + *	treated the same as modes 2 and 3 for some inputs.
    1.78 + * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
    1.79 + *	and strtod and dtoa should round accordingly.  Unless Trust_FLT_ROUNDS
    1.80 + *	is also #defined, fegetround() will be queried for the rounding mode.
    1.81 + *	Note that both FLT_ROUNDS and fegetround() are specified by the C99
    1.82 + *	standard (and are specified to be consistent, with fesetround()
    1.83 + *	affecting the value of FLT_ROUNDS), but that some (Linux) systems
    1.84 + *	do not work correctly in this regard, so using fegetround() is more
    1.85 + *	portable than using FLT_ROUNDS directly.
    1.86 + * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
    1.87 + *	and Honor_FLT_ROUNDS is not #defined.
    1.88 + * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
    1.89 + *	that use extended-precision instructions to compute rounded
    1.90 + *	products and quotients) with IBM.
    1.91 + * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
    1.92 + *	that rounds toward +Infinity.
    1.93 + * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
    1.94 + *	rounding when the underlying floating-point arithmetic uses
    1.95 + *	unbiased rounding.  This prevent using ordinary floating-point
    1.96 + *	arithmetic when the result could be computed with one rounding error.
    1.97 + * #define Inaccurate_Divide for IEEE-format with correctly rounded
    1.98 + *	products but inaccurate quotients, e.g., for Intel i860.
    1.99 + * #define NO_LONG_LONG on machines that do not have a "long long"
   1.100 + *	integer type (of >= 64 bits).  On such machines, you can
   1.101 + *	#define Just_16 to store 16 bits per 32-bit Long when doing
   1.102 + *	high-precision integer arithmetic.  Whether this speeds things
   1.103 + *	up or slows things down depends on the machine and the number
   1.104 + *	being converted.  If long long is available and the name is
   1.105 + *	something other than "long long", #define Llong to be the name,
   1.106 + *	and if "unsigned Llong" does not work as an unsigned version of
   1.107 + *	Llong, #define #ULLong to be the corresponding unsigned type.
   1.108 + * #define KR_headers for old-style C function headers.
   1.109 + * #define Bad_float_h if your system lacks a float.h or if it does not
   1.110 + *	define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
   1.111 + *	FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
   1.112 + * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
   1.113 + *	if memory is available and otherwise does something you deem
   1.114 + *	appropriate.  If MALLOC is undefined, malloc will be invoked
   1.115 + *	directly -- and assumed always to succeed.  Similarly, if you
   1.116 + *	want something other than the system's free() to be called to
   1.117 + *	recycle memory acquired from MALLOC, #define FREE to be the
   1.118 + *	name of the alternate routine.  (FREE or free is only called in
   1.119 + *	pathological cases, e.g., in a dtoa call after a dtoa return in
   1.120 + *	mode 3 with thousands of digits requested.)
   1.121 + * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
   1.122 + *	memory allocations from a private pool of memory when possible.
   1.123 + *	When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
   1.124 + *	unless #defined to be a different length.  This default length
   1.125 + *	suffices to get rid of MALLOC calls except for unusual cases,
   1.126 + *	such as decimal-to-binary conversion of a very long string of
   1.127 + *	digits.  The longest string dtoa can return is about 751 bytes
   1.128 + *	long.  For conversions by strtod of strings of 800 digits and
   1.129 + *	all dtoa conversions in single-threaded executions with 8-byte
   1.130 + *	pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
   1.131 + *	pointers, PRIVATE_MEM >= 7112 appears adequate.
   1.132 + * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
   1.133 + *	#defined automatically on IEEE systems.  On such systems,
   1.134 + *	when INFNAN_CHECK is #defined, strtod checks
   1.135 + *	for Infinity and NaN (case insensitively).  On some systems
   1.136 + *	(e.g., some HP systems), it may be necessary to #define NAN_WORD0
   1.137 + *	appropriately -- to the most significant word of a quiet NaN.
   1.138 + *	(On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
   1.139 + *	When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
   1.140 + *	strtod also accepts (case insensitively) strings of the form
   1.141 + *	NaN(x), where x is a string of hexadecimal digits and spaces;
   1.142 + *	if there is only one string of hexadecimal digits, it is taken
   1.143 + *	for the 52 fraction bits of the resulting NaN; if there are two
   1.144 + *	or more strings of hex digits, the first is for the high 20 bits,
   1.145 + *	the second and subsequent for the low 32 bits, with intervening
   1.146 + *	white space ignored; but if this results in none of the 52
   1.147 + *	fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
   1.148 + *	and NAN_WORD1 are used instead.
   1.149 + * #define MULTIPLE_THREADS if the system offers preemptively scheduled
   1.150 + *	multiple threads.  In this case, you must provide (or suitably
   1.151 + *	#define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
   1.152 + *	by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
   1.153 + *	in pow5mult, ensures lazy evaluation of only one copy of high
   1.154 + *	powers of 5; omitting this lock would introduce a small
   1.155 + *	probability of wasting memory, but would otherwise be harmless.)
   1.156 + *	You must also invoke freedtoa(s) to free the value s returned by
   1.157 + *	dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
   1.158 + * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
   1.159 + *	avoids underflows on inputs whose result does not underflow.
   1.160 + *	If you #define NO_IEEE_Scale on a machine that uses IEEE-format
   1.161 + *	floating-point numbers and flushes underflows to zero rather
   1.162 + *	than implementing gradual underflow, then you must also #define
   1.163 + *	Sudden_Underflow.
   1.164 + * #define USE_LOCALE to use the current locale's decimal_point value.
   1.165 + * #define SET_INEXACT if IEEE arithmetic is being used and extra
   1.166 + *	computation should be done to set the inexact flag when the
   1.167 + *	result is inexact and avoid setting inexact when the result
   1.168 + *	is exact.  In this case, dtoa.c must be compiled in
   1.169 + *	an environment, perhaps provided by #include "dtoa.c" in a
   1.170 + *	suitable wrapper, that defines two functions,
   1.171 + *		int get_inexact(void);
   1.172 + *		void clear_inexact(void);
   1.173 + *	such that get_inexact() returns a nonzero value if the
   1.174 + *	inexact bit is already set, and clear_inexact() sets the
   1.175 + *	inexact bit to 0.  When SET_INEXACT is #defined, strtod
   1.176 + *	also does extra computations to set the underflow and overflow
   1.177 + *	flags when appropriate (i.e., when the result is tiny and
   1.178 + *	inexact or when it is a numeric value rounded to +-infinity).
   1.179 + * #define NO_ERRNO if strtod should not assign errno = ERANGE when
   1.180 + *	the result overflows to +-Infinity or underflows to 0.
   1.181 + * #define NO_HEX_FP to omit recognition of hexadecimal floating-point
   1.182 + *	values by strtod.
   1.183 + * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
   1.184 + *	to disable logic for "fast" testing of very long input strings
   1.185 + *	to strtod.  This testing proceeds by initially truncating the
   1.186 + *	input string, then if necessary comparing the whole string with
   1.187 + *	a decimal expansion to decide close cases. This logic is only
   1.188 + *	used for input more than STRTOD_DIGLIM digits long (default 40).
   1.189 + */
   1.190 +
   1.191 +#ifndef Long
   1.192 +#define Long long
   1.193 +#endif
   1.194 +#ifndef ULong
   1.195 +typedef unsigned Long ULong;
   1.196 +#endif
   1.197 +
   1.198 +#ifdef DEBUG
   1.199 +#include "stdio.h"
   1.200 +#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
   1.201 +#endif
   1.202 +
   1.203 +#include "stdlib.h"
   1.204 +#include "string.h"
   1.205 +
   1.206 +#ifdef USE_LOCALE
   1.207 +#include "locale.h"
   1.208 +#endif
   1.209 +
   1.210 +#ifdef Honor_FLT_ROUNDS
   1.211 +#ifndef Trust_FLT_ROUNDS
   1.212 +#include <fenv.h>
   1.213 +#endif
   1.214 +#endif
   1.215 +
   1.216 +#ifdef MALLOC
   1.217 +#ifdef KR_headers
   1.218 +extern char *MALLOC();
   1.219 +#else
   1.220 +extern void *MALLOC(size_t);
   1.221 +#endif
   1.222 +#else
   1.223 +#define MALLOC malloc
   1.224 +#endif
   1.225 +
   1.226 +#ifndef Omit_Private_Memory
   1.227 +#ifndef PRIVATE_MEM
   1.228 +#define PRIVATE_MEM 2304
   1.229 +#endif
   1.230 +#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
   1.231 +static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
   1.232 +#endif
   1.233 +
   1.234 +#undef IEEE_Arith
   1.235 +#undef Avoid_Underflow
   1.236 +#ifdef IEEE_MC68k
   1.237 +#define IEEE_Arith
   1.238 +#endif
   1.239 +#ifdef IEEE_8087
   1.240 +#define IEEE_Arith
   1.241 +#endif
   1.242 +
   1.243 +#ifdef IEEE_Arith
   1.244 +#ifndef NO_INFNAN_CHECK
   1.245 +#undef INFNAN_CHECK
   1.246 +#define INFNAN_CHECK
   1.247 +#endif
   1.248 +#else
   1.249 +#undef INFNAN_CHECK
   1.250 +#define NO_STRTOD_BIGCOMP
   1.251 +#endif
   1.252 +
   1.253 +#include "errno.h"
   1.254 +
   1.255 +#ifdef Bad_float_h
   1.256 +
   1.257 +#ifdef IEEE_Arith
   1.258 +#define DBL_DIG 15
   1.259 +#define DBL_MAX_10_EXP 308
   1.260 +#define DBL_MAX_EXP 1024
   1.261 +#define FLT_RADIX 2
   1.262 +#endif /*IEEE_Arith*/
   1.263 +
   1.264 +#ifdef IBM
   1.265 +#define DBL_DIG 16
   1.266 +#define DBL_MAX_10_EXP 75
   1.267 +#define DBL_MAX_EXP 63
   1.268 +#define FLT_RADIX 16
   1.269 +#define DBL_MAX 7.2370055773322621e+75
   1.270 +#endif
   1.271 +
   1.272 +#ifdef VAX
   1.273 +#define DBL_DIG 16
   1.274 +#define DBL_MAX_10_EXP 38
   1.275 +#define DBL_MAX_EXP 127
   1.276 +#define FLT_RADIX 2
   1.277 +#define DBL_MAX 1.7014118346046923e+38
   1.278 +#endif
   1.279 +
   1.280 +#ifndef LONG_MAX
   1.281 +#define LONG_MAX 2147483647
   1.282 +#endif
   1.283 +
   1.284 +#else /* ifndef Bad_float_h */
   1.285 +#include "float.h"
   1.286 +#endif /* Bad_float_h */
   1.287 +
   1.288 +#ifndef __MATH_H__
   1.289 +#include "math.h"
   1.290 +#endif
   1.291 +
   1.292 +#ifdef __cplusplus
   1.293 +extern "C" {
   1.294 +#endif
   1.295 +
   1.296 +#ifndef CONST
   1.297 +#ifdef KR_headers
   1.298 +#define CONST /* blank */
   1.299 +#else
   1.300 +#define CONST const
   1.301 +#endif
   1.302 +#endif
   1.303 +
   1.304 +#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
   1.305 +Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
   1.306 +#endif
   1.307 +
   1.308 +typedef union { double d; ULong L[2]; } U;
   1.309 +
   1.310 +#ifdef IEEE_8087
   1.311 +#define word0(x) (x)->L[1]
   1.312 +#define word1(x) (x)->L[0]
   1.313 +#else
   1.314 +#define word0(x) (x)->L[0]
   1.315 +#define word1(x) (x)->L[1]
   1.316 +#endif
   1.317 +#define dval(x) (x)->d
   1.318 +
   1.319 +#ifndef STRTOD_DIGLIM
   1.320 +#define STRTOD_DIGLIM 40
   1.321 +#endif
   1.322 +
   1.323 +#ifdef DIGLIM_DEBUG
   1.324 +extern int strtod_diglim;
   1.325 +#else
   1.326 +#define strtod_diglim STRTOD_DIGLIM
   1.327 +#endif
   1.328 +
   1.329 +/* The following definition of Storeinc is appropriate for MIPS processors.
   1.330 + * An alternative that might be better on some machines is
   1.331 + * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
   1.332 + */
   1.333 +#if defined(IEEE_8087) + defined(VAX)
   1.334 +#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
   1.335 +((unsigned short *)a)[0] = (unsigned short)c, a++)
   1.336 +#else
   1.337 +#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
   1.338 +((unsigned short *)a)[1] = (unsigned short)c, a++)
   1.339 +#endif
   1.340 +
   1.341 +/* #define P DBL_MANT_DIG */
   1.342 +/* Ten_pmax = floor(P*log(2)/log(5)) */
   1.343 +/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
   1.344 +/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
   1.345 +/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
   1.346 +
   1.347 +#ifdef IEEE_Arith
   1.348 +#define Exp_shift  20
   1.349 +#define Exp_shift1 20
   1.350 +#define Exp_msk1    0x100000
   1.351 +#define Exp_msk11   0x100000
   1.352 +#define Exp_mask  0x7ff00000
   1.353 +#define P 53
   1.354 +#define Nbits 53
   1.355 +#define Bias 1023
   1.356 +#define Emax 1023
   1.357 +#define Emin (-1022)
   1.358 +#define Exp_1  0x3ff00000
   1.359 +#define Exp_11 0x3ff00000
   1.360 +#define Ebits 11
   1.361 +#define Frac_mask  0xfffff
   1.362 +#define Frac_mask1 0xfffff
   1.363 +#define Ten_pmax 22
   1.364 +#define Bletch 0x10
   1.365 +#define Bndry_mask  0xfffff
   1.366 +#define Bndry_mask1 0xfffff
   1.367 +#define LSB 1
   1.368 +#define Sign_bit 0x80000000
   1.369 +#define Log2P 1
   1.370 +#define Tiny0 0
   1.371 +#define Tiny1 1
   1.372 +#define Quick_max 14
   1.373 +#define Int_max 14
   1.374 +#ifndef NO_IEEE_Scale
   1.375 +#define Avoid_Underflow
   1.376 +#ifdef Flush_Denorm	/* debugging option */
   1.377 +#undef Sudden_Underflow
   1.378 +#endif
   1.379 +#endif
   1.380 +
   1.381 +#ifndef Flt_Rounds
   1.382 +#ifdef FLT_ROUNDS
   1.383 +#define Flt_Rounds FLT_ROUNDS
   1.384 +#else
   1.385 +#define Flt_Rounds 1
   1.386 +#endif
   1.387 +#endif /*Flt_Rounds*/
   1.388 +
   1.389 +#ifdef Honor_FLT_ROUNDS
   1.390 +#undef Check_FLT_ROUNDS
   1.391 +#define Check_FLT_ROUNDS
   1.392 +#else
   1.393 +#define Rounding Flt_Rounds
   1.394 +#endif
   1.395 +
   1.396 +#else /* ifndef IEEE_Arith */
   1.397 +#undef Check_FLT_ROUNDS
   1.398 +#undef Honor_FLT_ROUNDS
   1.399 +#undef SET_INEXACT
   1.400 +#undef  Sudden_Underflow
   1.401 +#define Sudden_Underflow
   1.402 +#ifdef IBM
   1.403 +#undef Flt_Rounds
   1.404 +#define Flt_Rounds 0
   1.405 +#define Exp_shift  24
   1.406 +#define Exp_shift1 24
   1.407 +#define Exp_msk1   0x1000000
   1.408 +#define Exp_msk11  0x1000000
   1.409 +#define Exp_mask  0x7f000000
   1.410 +#define P 14
   1.411 +#define Nbits 56
   1.412 +#define Bias 65
   1.413 +#define Emax 248
   1.414 +#define Emin (-260)
   1.415 +#define Exp_1  0x41000000
   1.416 +#define Exp_11 0x41000000
   1.417 +#define Ebits 8	/* exponent has 7 bits, but 8 is the right value in b2d */
   1.418 +#define Frac_mask  0xffffff
   1.419 +#define Frac_mask1 0xffffff
   1.420 +#define Bletch 4
   1.421 +#define Ten_pmax 22
   1.422 +#define Bndry_mask  0xefffff
   1.423 +#define Bndry_mask1 0xffffff
   1.424 +#define LSB 1
   1.425 +#define Sign_bit 0x80000000
   1.426 +#define Log2P 4
   1.427 +#define Tiny0 0x100000
   1.428 +#define Tiny1 0
   1.429 +#define Quick_max 14
   1.430 +#define Int_max 15
   1.431 +#else /* VAX */
   1.432 +#undef Flt_Rounds
   1.433 +#define Flt_Rounds 1
   1.434 +#define Exp_shift  23
   1.435 +#define Exp_shift1 7
   1.436 +#define Exp_msk1    0x80
   1.437 +#define Exp_msk11   0x800000
   1.438 +#define Exp_mask  0x7f80
   1.439 +#define P 56
   1.440 +#define Nbits 56
   1.441 +#define Bias 129
   1.442 +#define Emax 126
   1.443 +#define Emin (-129)
   1.444 +#define Exp_1  0x40800000
   1.445 +#define Exp_11 0x4080
   1.446 +#define Ebits 8
   1.447 +#define Frac_mask  0x7fffff
   1.448 +#define Frac_mask1 0xffff007f
   1.449 +#define Ten_pmax 24
   1.450 +#define Bletch 2
   1.451 +#define Bndry_mask  0xffff007f
   1.452 +#define Bndry_mask1 0xffff007f
   1.453 +#define LSB 0x10000
   1.454 +#define Sign_bit 0x8000
   1.455 +#define Log2P 1
   1.456 +#define Tiny0 0x80
   1.457 +#define Tiny1 0
   1.458 +#define Quick_max 15
   1.459 +#define Int_max 15
   1.460 +#endif /* IBM, VAX */
   1.461 +#endif /* IEEE_Arith */
   1.462 +
   1.463 +#ifndef IEEE_Arith
   1.464 +#define ROUND_BIASED
   1.465 +#else
   1.466 +#ifdef ROUND_BIASED_without_Round_Up
   1.467 +#undef  ROUND_BIASED
   1.468 +#define ROUND_BIASED
   1.469 +#endif
   1.470 +#endif
   1.471 +
   1.472 +#ifdef RND_PRODQUOT
   1.473 +#define rounded_product(a,b) a = rnd_prod(a, b)
   1.474 +#define rounded_quotient(a,b) a = rnd_quot(a, b)
   1.475 +#ifdef KR_headers
   1.476 +extern double rnd_prod(), rnd_quot();
   1.477 +#else
   1.478 +extern double rnd_prod(double, double), rnd_quot(double, double);
   1.479 +#endif
   1.480 +#else
   1.481 +#define rounded_product(a,b) a *= b
   1.482 +#define rounded_quotient(a,b) a /= b
   1.483 +#endif
   1.484 +
   1.485 +#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
   1.486 +#define Big1 0xffffffff
   1.487 +
   1.488 +#ifndef Pack_32
   1.489 +#define Pack_32
   1.490 +#endif
   1.491 +
   1.492 +typedef struct BCinfo BCinfo;
   1.493 + struct
   1.494 +BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflchk; };
   1.495 +
   1.496 +#ifdef KR_headers
   1.497 +#define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
   1.498 +#else
   1.499 +#define FFFFFFFF 0xffffffffUL
   1.500 +#endif
   1.501 +
   1.502 +#ifdef NO_LONG_LONG
   1.503 +#undef ULLong
   1.504 +#ifdef Just_16
   1.505 +#undef Pack_32
   1.506 +/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
   1.507 + * This makes some inner loops simpler and sometimes saves work
   1.508 + * during multiplications, but it often seems to make things slightly
   1.509 + * slower.  Hence the default is now to store 32 bits per Long.
   1.510 + */
   1.511 +#endif
   1.512 +#else	/* long long available */
   1.513 +#ifndef Llong
   1.514 +#define Llong long long
   1.515 +#endif
   1.516 +#ifndef ULLong
   1.517 +#define ULLong unsigned Llong
   1.518 +#endif
   1.519 +#endif /* NO_LONG_LONG */
   1.520 +
   1.521 +#ifndef MULTIPLE_THREADS
   1.522 +#define ACQUIRE_DTOA_LOCK(n)	/*nothing*/
   1.523 +#define FREE_DTOA_LOCK(n)	/*nothing*/
   1.524 +#endif
   1.525 +
   1.526 +#define Kmax 7
   1.527 +
   1.528 +#ifdef __cplusplus
   1.529 +extern "C" double strtod(const char *s00, char **se);
   1.530 +extern "C" char *dtoa(double d, int mode, int ndigits,
   1.531 +			int *decpt, int *sign, char **rve);
   1.532 +#endif
   1.533 +
   1.534 + struct
   1.535 +Bigint {
   1.536 +	struct Bigint *next;
   1.537 +	int k, maxwds, sign, wds;
   1.538 +	ULong x[1];
   1.539 +	};
   1.540 +
   1.541 + typedef struct Bigint Bigint;
   1.542 +
   1.543 + static Bigint *freelist[Kmax+1];
   1.544 +
   1.545 + static Bigint *
   1.546 +Balloc
   1.547 +#ifdef KR_headers
   1.548 +	(k) int k;
   1.549 +#else
   1.550 +	(int k)
   1.551 +#endif
   1.552 +{
   1.553 +	int x;
   1.554 +	Bigint *rv;
   1.555 +#ifndef Omit_Private_Memory
   1.556 +	unsigned int len;
   1.557 +#endif
   1.558 +
   1.559 +	ACQUIRE_DTOA_LOCK(0);
   1.560 +	/* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
   1.561 +	/* but this case seems very unlikely. */
   1.562 +	if (k <= Kmax && (rv = freelist[k]))
   1.563 +		freelist[k] = rv->next;
   1.564 +	else {
   1.565 +		x = 1 << k;
   1.566 +#ifdef Omit_Private_Memory
   1.567 +		rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
   1.568 +#else
   1.569 +		len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
   1.570 +			/sizeof(double);
   1.571 +		if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
   1.572 +			rv = (Bigint*)pmem_next;
   1.573 +			pmem_next += len;
   1.574 +			}
   1.575 +		else
   1.576 +			rv = (Bigint*)MALLOC(len*sizeof(double));
   1.577 +#endif
   1.578 +		rv->k = k;
   1.579 +		rv->maxwds = x;
   1.580 +		}
   1.581 +	FREE_DTOA_LOCK(0);
   1.582 +	rv->sign = rv->wds = 0;
   1.583 +	return rv;
   1.584 +	}
   1.585 +
   1.586 + static void
   1.587 +Bfree
   1.588 +#ifdef KR_headers
   1.589 +	(v) Bigint *v;
   1.590 +#else
   1.591 +	(Bigint *v)
   1.592 +#endif
   1.593 +{
   1.594 +	if (v) {
   1.595 +		if (v->k > Kmax)
   1.596 +#ifdef FREE
   1.597 +			FREE((void*)v);
   1.598 +#else
   1.599 +			free((void*)v);
   1.600 +#endif
   1.601 +		else {
   1.602 +			ACQUIRE_DTOA_LOCK(0);
   1.603 +			v->next = freelist[v->k];
   1.604 +			freelist[v->k] = v;
   1.605 +			FREE_DTOA_LOCK(0);
   1.606 +			}
   1.607 +		}
   1.608 +	}
   1.609 +
   1.610 +#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
   1.611 +y->wds*sizeof(Long) + 2*sizeof(int))
   1.612 +
   1.613 + static Bigint *
   1.614 +multadd
   1.615 +#ifdef KR_headers
   1.616 +	(b, m, a) Bigint *b; int m, a;
   1.617 +#else
   1.618 +	(Bigint *b, int m, int a)	/* multiply by m and add a */
   1.619 +#endif
   1.620 +{
   1.621 +	int i, wds;
   1.622 +#ifdef ULLong
   1.623 +	ULong *x;
   1.624 +	ULLong carry, y;
   1.625 +#else
   1.626 +	ULong carry, *x, y;
   1.627 +#ifdef Pack_32
   1.628 +	ULong xi, z;
   1.629 +#endif
   1.630 +#endif
   1.631 +	Bigint *b1;
   1.632 +
   1.633 +	wds = b->wds;
   1.634 +	x = b->x;
   1.635 +	i = 0;
   1.636 +	carry = a;
   1.637 +	do {
   1.638 +#ifdef ULLong
   1.639 +		y = *x * (ULLong)m + carry;
   1.640 +		carry = y >> 32;
   1.641 +		*x++ = y & FFFFFFFF;
   1.642 +#else
   1.643 +#ifdef Pack_32
   1.644 +		xi = *x;
   1.645 +		y = (xi & 0xffff) * m + carry;
   1.646 +		z = (xi >> 16) * m + (y >> 16);
   1.647 +		carry = z >> 16;
   1.648 +		*x++ = (z << 16) + (y & 0xffff);
   1.649 +#else
   1.650 +		y = *x * m + carry;
   1.651 +		carry = y >> 16;
   1.652 +		*x++ = y & 0xffff;
   1.653 +#endif
   1.654 +#endif
   1.655 +		}
   1.656 +		while(++i < wds);
   1.657 +	if (carry) {
   1.658 +		if (wds >= b->maxwds) {
   1.659 +			b1 = Balloc(b->k+1);
   1.660 +			Bcopy(b1, b);
   1.661 +			Bfree(b);
   1.662 +			b = b1;
   1.663 +			}
   1.664 +		b->x[wds++] = carry;
   1.665 +		b->wds = wds;
   1.666 +		}
   1.667 +	return b;
   1.668 +	}
   1.669 +
   1.670 + static Bigint *
   1.671 +s2b
   1.672 +#ifdef KR_headers
   1.673 +	(s, nd0, nd, y9, dplen) CONST char *s; int nd0, nd, dplen; ULong y9;
   1.674 +#else
   1.675 +	(const char *s, int nd0, int nd, ULong y9, int dplen)
   1.676 +#endif
   1.677 +{
   1.678 +	Bigint *b;
   1.679 +	int i, k;
   1.680 +	Long x, y;
   1.681 +
   1.682 +	x = (nd + 8) / 9;
   1.683 +	for(k = 0, y = 1; x > y; y <<= 1, k++) ;
   1.684 +#ifdef Pack_32
   1.685 +	b = Balloc(k);
   1.686 +	b->x[0] = y9;
   1.687 +	b->wds = 1;
   1.688 +#else
   1.689 +	b = Balloc(k+1);
   1.690 +	b->x[0] = y9 & 0xffff;
   1.691 +	b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
   1.692 +#endif
   1.693 +
   1.694 +	i = 9;
   1.695 +	if (9 < nd0) {
   1.696 +		s += 9;
   1.697 +		do b = multadd(b, 10, *s++ - '0');
   1.698 +			while(++i < nd0);
   1.699 +		s += dplen;
   1.700 +		}
   1.701 +	else
   1.702 +		s += dplen + 9;
   1.703 +	for(; i < nd; i++)
   1.704 +		b = multadd(b, 10, *s++ - '0');
   1.705 +	return b;
   1.706 +	}
   1.707 +
   1.708 + static int
   1.709 +hi0bits
   1.710 +#ifdef KR_headers
   1.711 +	(x) ULong x;
   1.712 +#else
   1.713 +	(ULong x)
   1.714 +#endif
   1.715 +{
   1.716 +	int k = 0;
   1.717 +
   1.718 +	if (!(x & 0xffff0000)) {
   1.719 +		k = 16;
   1.720 +		x <<= 16;
   1.721 +		}
   1.722 +	if (!(x & 0xff000000)) {
   1.723 +		k += 8;
   1.724 +		x <<= 8;
   1.725 +		}
   1.726 +	if (!(x & 0xf0000000)) {
   1.727 +		k += 4;
   1.728 +		x <<= 4;
   1.729 +		}
   1.730 +	if (!(x & 0xc0000000)) {
   1.731 +		k += 2;
   1.732 +		x <<= 2;
   1.733 +		}
   1.734 +	if (!(x & 0x80000000)) {
   1.735 +		k++;
   1.736 +		if (!(x & 0x40000000))
   1.737 +			return 32;
   1.738 +		}
   1.739 +	return k;
   1.740 +	}
   1.741 +
   1.742 + static int
   1.743 +lo0bits
   1.744 +#ifdef KR_headers
   1.745 +	(y) ULong *y;
   1.746 +#else
   1.747 +	(ULong *y)
   1.748 +#endif
   1.749 +{
   1.750 +	int k;
   1.751 +	ULong x = *y;
   1.752 +
   1.753 +	if (x & 7) {
   1.754 +		if (x & 1)
   1.755 +			return 0;
   1.756 +		if (x & 2) {
   1.757 +			*y = x >> 1;
   1.758 +			return 1;
   1.759 +			}
   1.760 +		*y = x >> 2;
   1.761 +		return 2;
   1.762 +		}
   1.763 +	k = 0;
   1.764 +	if (!(x & 0xffff)) {
   1.765 +		k = 16;
   1.766 +		x >>= 16;
   1.767 +		}
   1.768 +	if (!(x & 0xff)) {
   1.769 +		k += 8;
   1.770 +		x >>= 8;
   1.771 +		}
   1.772 +	if (!(x & 0xf)) {
   1.773 +		k += 4;
   1.774 +		x >>= 4;
   1.775 +		}
   1.776 +	if (!(x & 0x3)) {
   1.777 +		k += 2;
   1.778 +		x >>= 2;
   1.779 +		}
   1.780 +	if (!(x & 1)) {
   1.781 +		k++;
   1.782 +		x >>= 1;
   1.783 +		if (!x)
   1.784 +			return 32;
   1.785 +		}
   1.786 +	*y = x;
   1.787 +	return k;
   1.788 +	}
   1.789 +
   1.790 + static Bigint *
   1.791 +i2b
   1.792 +#ifdef KR_headers
   1.793 +	(i) int i;
   1.794 +#else
   1.795 +	(int i)
   1.796 +#endif
   1.797 +{
   1.798 +	Bigint *b;
   1.799 +
   1.800 +	b = Balloc(1);
   1.801 +	b->x[0] = i;
   1.802 +	b->wds = 1;
   1.803 +	return b;
   1.804 +	}
   1.805 +
   1.806 + static Bigint *
   1.807 +mult
   1.808 +#ifdef KR_headers
   1.809 +	(a, b) Bigint *a, *b;
   1.810 +#else
   1.811 +	(Bigint *a, Bigint *b)
   1.812 +#endif
   1.813 +{
   1.814 +	Bigint *c;
   1.815 +	int k, wa, wb, wc;
   1.816 +	ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
   1.817 +	ULong y;
   1.818 +#ifdef ULLong
   1.819 +	ULLong carry, z;
   1.820 +#else
   1.821 +	ULong carry, z;
   1.822 +#ifdef Pack_32
   1.823 +	ULong z2;
   1.824 +#endif
   1.825 +#endif
   1.826 +
   1.827 +	if (a->wds < b->wds) {
   1.828 +		c = a;
   1.829 +		a = b;
   1.830 +		b = c;
   1.831 +		}
   1.832 +	k = a->k;
   1.833 +	wa = a->wds;
   1.834 +	wb = b->wds;
   1.835 +	wc = wa + wb;
   1.836 +	if (wc > a->maxwds)
   1.837 +		k++;
   1.838 +	c = Balloc(k);
   1.839 +	for(x = c->x, xa = x + wc; x < xa; x++)
   1.840 +		*x = 0;
   1.841 +	xa = a->x;
   1.842 +	xae = xa + wa;
   1.843 +	xb = b->x;
   1.844 +	xbe = xb + wb;
   1.845 +	xc0 = c->x;
   1.846 +#ifdef ULLong
   1.847 +	for(; xb < xbe; xc0++) {
   1.848 +		if ((y = *xb++)) {
   1.849 +			x = xa;
   1.850 +			xc = xc0;
   1.851 +			carry = 0;
   1.852 +			do {
   1.853 +				z = *x++ * (ULLong)y + *xc + carry;
   1.854 +				carry = z >> 32;
   1.855 +				*xc++ = z & FFFFFFFF;
   1.856 +				}
   1.857 +				while(x < xae);
   1.858 +			*xc = carry;
   1.859 +			}
   1.860 +		}
   1.861 +#else
   1.862 +#ifdef Pack_32
   1.863 +	for(; xb < xbe; xb++, xc0++) {
   1.864 +		if (y = *xb & 0xffff) {
   1.865 +			x = xa;
   1.866 +			xc = xc0;
   1.867 +			carry = 0;
   1.868 +			do {
   1.869 +				z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
   1.870 +				carry = z >> 16;
   1.871 +				z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
   1.872 +				carry = z2 >> 16;
   1.873 +				Storeinc(xc, z2, z);
   1.874 +				}
   1.875 +				while(x < xae);
   1.876 +			*xc = carry;
   1.877 +			}
   1.878 +		if (y = *xb >> 16) {
   1.879 +			x = xa;
   1.880 +			xc = xc0;
   1.881 +			carry = 0;
   1.882 +			z2 = *xc;
   1.883 +			do {
   1.884 +				z = (*x & 0xffff) * y + (*xc >> 16) + carry;
   1.885 +				carry = z >> 16;
   1.886 +				Storeinc(xc, z, z2);
   1.887 +				z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
   1.888 +				carry = z2 >> 16;
   1.889 +				}
   1.890 +				while(x < xae);
   1.891 +			*xc = z2;
   1.892 +			}
   1.893 +		}
   1.894 +#else
   1.895 +	for(; xb < xbe; xc0++) {
   1.896 +		if (y = *xb++) {
   1.897 +			x = xa;
   1.898 +			xc = xc0;
   1.899 +			carry = 0;
   1.900 +			do {
   1.901 +				z = *x++ * y + *xc + carry;
   1.902 +				carry = z >> 16;
   1.903 +				*xc++ = z & 0xffff;
   1.904 +				}
   1.905 +				while(x < xae);
   1.906 +			*xc = carry;
   1.907 +			}
   1.908 +		}
   1.909 +#endif
   1.910 +#endif
   1.911 +	for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
   1.912 +	c->wds = wc;
   1.913 +	return c;
   1.914 +	}
   1.915 +
   1.916 + static Bigint *p5s;
   1.917 +
   1.918 + static Bigint *
   1.919 +pow5mult
   1.920 +#ifdef KR_headers
   1.921 +	(b, k) Bigint *b; int k;
   1.922 +#else
   1.923 +	(Bigint *b, int k)
   1.924 +#endif
   1.925 +{
   1.926 +	Bigint *b1, *p5, *p51;
   1.927 +	int i;
   1.928 +	static int p05[3] = { 5, 25, 125 };
   1.929 +
   1.930 +	if ((i = k & 3))
   1.931 +		b = multadd(b, p05[i-1], 0);
   1.932 +
   1.933 +	if (!(k >>= 2))
   1.934 +		return b;
   1.935 +	if (!(p5 = p5s)) {
   1.936 +		/* first time */
   1.937 +#ifdef MULTIPLE_THREADS
   1.938 +		ACQUIRE_DTOA_LOCK(1);
   1.939 +		if (!(p5 = p5s)) {
   1.940 +			p5 = p5s = i2b(625);
   1.941 +			p5->next = 0;
   1.942 +			}
   1.943 +		FREE_DTOA_LOCK(1);
   1.944 +#else
   1.945 +		p5 = p5s = i2b(625);
   1.946 +		p5->next = 0;
   1.947 +#endif
   1.948 +		}
   1.949 +	for(;;) {
   1.950 +		if (k & 1) {
   1.951 +			b1 = mult(b, p5);
   1.952 +			Bfree(b);
   1.953 +			b = b1;
   1.954 +			}
   1.955 +		if (!(k >>= 1))
   1.956 +			break;
   1.957 +		if (!(p51 = p5->next)) {
   1.958 +#ifdef MULTIPLE_THREADS
   1.959 +			ACQUIRE_DTOA_LOCK(1);
   1.960 +			if (!(p51 = p5->next)) {
   1.961 +				p51 = p5->next = mult(p5,p5);
   1.962 +				p51->next = 0;
   1.963 +				}
   1.964 +			FREE_DTOA_LOCK(1);
   1.965 +#else
   1.966 +			p51 = p5->next = mult(p5,p5);
   1.967 +			p51->next = 0;
   1.968 +#endif
   1.969 +			}
   1.970 +		p5 = p51;
   1.971 +		}
   1.972 +	return b;
   1.973 +	}
   1.974 +
   1.975 + static Bigint *
   1.976 +lshift
   1.977 +#ifdef KR_headers
   1.978 +	(b, k) Bigint *b; int k;
   1.979 +#else
   1.980 +	(Bigint *b, int k)
   1.981 +#endif
   1.982 +{
   1.983 +	int i, k1, n, n1;
   1.984 +	Bigint *b1;
   1.985 +	ULong *x, *x1, *xe, z;
   1.986 +
   1.987 +#ifdef Pack_32
   1.988 +	n = k >> 5;
   1.989 +#else
   1.990 +	n = k >> 4;
   1.991 +#endif
   1.992 +	k1 = b->k;
   1.993 +	n1 = n + b->wds + 1;
   1.994 +	for(i = b->maxwds; n1 > i; i <<= 1)
   1.995 +		k1++;
   1.996 +	b1 = Balloc(k1);
   1.997 +	x1 = b1->x;
   1.998 +	for(i = 0; i < n; i++)
   1.999 +		*x1++ = 0;
  1.1000 +	x = b->x;
  1.1001 +	xe = x + b->wds;
  1.1002 +#ifdef Pack_32
  1.1003 +	if (k &= 0x1f) {
  1.1004 +		k1 = 32 - k;
  1.1005 +		z = 0;
  1.1006 +		do {
  1.1007 +			*x1++ = *x << k | z;
  1.1008 +			z = *x++ >> k1;
  1.1009 +			}
  1.1010 +			while(x < xe);
  1.1011 +		if ((*x1 = z))
  1.1012 +			++n1;
  1.1013 +		}
  1.1014 +#else
  1.1015 +	if (k &= 0xf) {
  1.1016 +		k1 = 16 - k;
  1.1017 +		z = 0;
  1.1018 +		do {
  1.1019 +			*x1++ = *x << k  & 0xffff | z;
  1.1020 +			z = *x++ >> k1;
  1.1021 +			}
  1.1022 +			while(x < xe);
  1.1023 +		if (*x1 = z)
  1.1024 +			++n1;
  1.1025 +		}
  1.1026 +#endif
  1.1027 +	else do
  1.1028 +		*x1++ = *x++;
  1.1029 +		while(x < xe);
  1.1030 +	b1->wds = n1 - 1;
  1.1031 +	Bfree(b);
  1.1032 +	return b1;
  1.1033 +	}
  1.1034 +
  1.1035 + static int
  1.1036 +cmp
  1.1037 +#ifdef KR_headers
  1.1038 +	(a, b) Bigint *a, *b;
  1.1039 +#else
  1.1040 +	(Bigint *a, Bigint *b)
  1.1041 +#endif
  1.1042 +{
  1.1043 +	ULong *xa, *xa0, *xb, *xb0;
  1.1044 +	int i, j;
  1.1045 +
  1.1046 +	i = a->wds;
  1.1047 +	j = b->wds;
  1.1048 +#ifdef DEBUG
  1.1049 +	if (i > 1 && !a->x[i-1])
  1.1050 +		Bug("cmp called with a->x[a->wds-1] == 0");
  1.1051 +	if (j > 1 && !b->x[j-1])
  1.1052 +		Bug("cmp called with b->x[b->wds-1] == 0");
  1.1053 +#endif
  1.1054 +	if (i -= j)
  1.1055 +		return i;
  1.1056 +	xa0 = a->x;
  1.1057 +	xa = xa0 + j;
  1.1058 +	xb0 = b->x;
  1.1059 +	xb = xb0 + j;
  1.1060 +	for(;;) {
  1.1061 +		if (*--xa != *--xb)
  1.1062 +			return *xa < *xb ? -1 : 1;
  1.1063 +		if (xa <= xa0)
  1.1064 +			break;
  1.1065 +		}
  1.1066 +	return 0;
  1.1067 +	}
  1.1068 +
  1.1069 + static Bigint *
  1.1070 +diff
  1.1071 +#ifdef KR_headers
  1.1072 +	(a, b) Bigint *a, *b;
  1.1073 +#else
  1.1074 +	(Bigint *a, Bigint *b)
  1.1075 +#endif
  1.1076 +{
  1.1077 +	Bigint *c;
  1.1078 +	int i, wa, wb;
  1.1079 +	ULong *xa, *xae, *xb, *xbe, *xc;
  1.1080 +#ifdef ULLong
  1.1081 +	ULLong borrow, y;
  1.1082 +#else
  1.1083 +	ULong borrow, y;
  1.1084 +#ifdef Pack_32
  1.1085 +	ULong z;
  1.1086 +#endif
  1.1087 +#endif
  1.1088 +
  1.1089 +	i = cmp(a,b);
  1.1090 +	if (!i) {
  1.1091 +		c = Balloc(0);
  1.1092 +		c->wds = 1;
  1.1093 +		c->x[0] = 0;
  1.1094 +		return c;
  1.1095 +		}
  1.1096 +	if (i < 0) {
  1.1097 +		c = a;
  1.1098 +		a = b;
  1.1099 +		b = c;
  1.1100 +		i = 1;
  1.1101 +		}
  1.1102 +	else
  1.1103 +		i = 0;
  1.1104 +	c = Balloc(a->k);
  1.1105 +	c->sign = i;
  1.1106 +	wa = a->wds;
  1.1107 +	xa = a->x;
  1.1108 +	xae = xa + wa;
  1.1109 +	wb = b->wds;
  1.1110 +	xb = b->x;
  1.1111 +	xbe = xb + wb;
  1.1112 +	xc = c->x;
  1.1113 +	borrow = 0;
  1.1114 +#ifdef ULLong
  1.1115 +	do {
  1.1116 +		y = (ULLong)*xa++ - *xb++ - borrow;
  1.1117 +		borrow = y >> 32 & (ULong)1;
  1.1118 +		*xc++ = y & FFFFFFFF;
  1.1119 +		}
  1.1120 +		while(xb < xbe);
  1.1121 +	while(xa < xae) {
  1.1122 +		y = *xa++ - borrow;
  1.1123 +		borrow = y >> 32 & (ULong)1;
  1.1124 +		*xc++ = y & FFFFFFFF;
  1.1125 +		}
  1.1126 +#else
  1.1127 +#ifdef Pack_32
  1.1128 +	do {
  1.1129 +		y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  1.1130 +		borrow = (y & 0x10000) >> 16;
  1.1131 +		z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  1.1132 +		borrow = (z & 0x10000) >> 16;
  1.1133 +		Storeinc(xc, z, y);
  1.1134 +		}
  1.1135 +		while(xb < xbe);
  1.1136 +	while(xa < xae) {
  1.1137 +		y = (*xa & 0xffff) - borrow;
  1.1138 +		borrow = (y & 0x10000) >> 16;
  1.1139 +		z = (*xa++ >> 16) - borrow;
  1.1140 +		borrow = (z & 0x10000) >> 16;
  1.1141 +		Storeinc(xc, z, y);
  1.1142 +		}
  1.1143 +#else
  1.1144 +	do {
  1.1145 +		y = *xa++ - *xb++ - borrow;
  1.1146 +		borrow = (y & 0x10000) >> 16;
  1.1147 +		*xc++ = y & 0xffff;
  1.1148 +		}
  1.1149 +		while(xb < xbe);
  1.1150 +	while(xa < xae) {
  1.1151 +		y = *xa++ - borrow;
  1.1152 +		borrow = (y & 0x10000) >> 16;
  1.1153 +		*xc++ = y & 0xffff;
  1.1154 +		}
  1.1155 +#endif
  1.1156 +#endif
  1.1157 +	while(!*--xc)
  1.1158 +		wa--;
  1.1159 +	c->wds = wa;
  1.1160 +	return c;
  1.1161 +	}
  1.1162 +
  1.1163 + static double
  1.1164 +ulp
  1.1165 +#ifdef KR_headers
  1.1166 +	(x) U *x;
  1.1167 +#else
  1.1168 +	(U *x)
  1.1169 +#endif
  1.1170 +{
  1.1171 +	Long L;
  1.1172 +	U u;
  1.1173 +
  1.1174 +	L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  1.1175 +#ifndef Avoid_Underflow
  1.1176 +#ifndef Sudden_Underflow
  1.1177 +	if (L > 0) {
  1.1178 +#endif
  1.1179 +#endif
  1.1180 +#ifdef IBM
  1.1181 +		L |= Exp_msk1 >> 4;
  1.1182 +#endif
  1.1183 +		word0(&u) = L;
  1.1184 +		word1(&u) = 0;
  1.1185 +#ifndef Avoid_Underflow
  1.1186 +#ifndef Sudden_Underflow
  1.1187 +		}
  1.1188 +	else {
  1.1189 +		L = -L >> Exp_shift;
  1.1190 +		if (L < Exp_shift) {
  1.1191 +			word0(&u) = 0x80000 >> L;
  1.1192 +			word1(&u) = 0;
  1.1193 +			}
  1.1194 +		else {
  1.1195 +			word0(&u) = 0;
  1.1196 +			L -= Exp_shift;
  1.1197 +			word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
  1.1198 +			}
  1.1199 +		}
  1.1200 +#endif
  1.1201 +#endif
  1.1202 +	return dval(&u);
  1.1203 +	}
  1.1204 +
  1.1205 + static double
  1.1206 +b2d
  1.1207 +#ifdef KR_headers
  1.1208 +	(a, e) Bigint *a; int *e;
  1.1209 +#else
  1.1210 +	(Bigint *a, int *e)
  1.1211 +#endif
  1.1212 +{
  1.1213 +	ULong *xa, *xa0, w, y, z;
  1.1214 +	int k;
  1.1215 +	U d;
  1.1216 +#ifdef VAX
  1.1217 +	ULong d0, d1;
  1.1218 +#else
  1.1219 +#define d0 word0(&d)
  1.1220 +#define d1 word1(&d)
  1.1221 +#endif
  1.1222 +
  1.1223 +	xa0 = a->x;
  1.1224 +	xa = xa0 + a->wds;
  1.1225 +	y = *--xa;
  1.1226 +#ifdef DEBUG
  1.1227 +	if (!y) Bug("zero y in b2d");
  1.1228 +#endif
  1.1229 +	k = hi0bits(y);
  1.1230 +	*e = 32 - k;
  1.1231 +#ifdef Pack_32
  1.1232 +	if (k < Ebits) {
  1.1233 +		d0 = Exp_1 | y >> (Ebits - k);
  1.1234 +		w = xa > xa0 ? *--xa : 0;
  1.1235 +		d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
  1.1236 +		goto ret_d;
  1.1237 +		}
  1.1238 +	z = xa > xa0 ? *--xa : 0;
  1.1239 +	if (k -= Ebits) {
  1.1240 +		d0 = Exp_1 | y << k | z >> (32 - k);
  1.1241 +		y = xa > xa0 ? *--xa : 0;
  1.1242 +		d1 = z << k | y >> (32 - k);
  1.1243 +		}
  1.1244 +	else {
  1.1245 +		d0 = Exp_1 | y;
  1.1246 +		d1 = z;
  1.1247 +		}
  1.1248 +#else
  1.1249 +	if (k < Ebits + 16) {
  1.1250 +		z = xa > xa0 ? *--xa : 0;
  1.1251 +		d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
  1.1252 +		w = xa > xa0 ? *--xa : 0;
  1.1253 +		y = xa > xa0 ? *--xa : 0;
  1.1254 +		d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
  1.1255 +		goto ret_d;
  1.1256 +		}
  1.1257 +	z = xa > xa0 ? *--xa : 0;
  1.1258 +	w = xa > xa0 ? *--xa : 0;
  1.1259 +	k -= Ebits + 16;
  1.1260 +	d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
  1.1261 +	y = xa > xa0 ? *--xa : 0;
  1.1262 +	d1 = w << k + 16 | y << k;
  1.1263 +#endif
  1.1264 + ret_d:
  1.1265 +#ifdef VAX
  1.1266 +	word0(&d) = d0 >> 16 | d0 << 16;
  1.1267 +	word1(&d) = d1 >> 16 | d1 << 16;
  1.1268 +#else
  1.1269 +#undef d0
  1.1270 +#undef d1
  1.1271 +#endif
  1.1272 +	return dval(&d);
  1.1273 +	}
  1.1274 +
  1.1275 + static Bigint *
  1.1276 +d2b
  1.1277 +#ifdef KR_headers
  1.1278 +	(d, e, bits) U *d; int *e, *bits;
  1.1279 +#else
  1.1280 +	(U *d, int *e, int *bits)
  1.1281 +#endif
  1.1282 +{
  1.1283 +	Bigint *b;
  1.1284 +	int de, k;
  1.1285 +	ULong *x, y, z;
  1.1286 +#ifndef Sudden_Underflow
  1.1287 +	int i;
  1.1288 +#endif
  1.1289 +#ifdef VAX
  1.1290 +	ULong d0, d1;
  1.1291 +	d0 = word0(d) >> 16 | word0(d) << 16;
  1.1292 +	d1 = word1(d) >> 16 | word1(d) << 16;
  1.1293 +#else
  1.1294 +#define d0 word0(d)
  1.1295 +#define d1 word1(d)
  1.1296 +#endif
  1.1297 +
  1.1298 +#ifdef Pack_32
  1.1299 +	b = Balloc(1);
  1.1300 +#else
  1.1301 +	b = Balloc(2);
  1.1302 +#endif
  1.1303 +	x = b->x;
  1.1304 +
  1.1305 +	z = d0 & Frac_mask;
  1.1306 +	d0 &= 0x7fffffff;	/* clear sign bit, which we ignore */
  1.1307 +#ifdef Sudden_Underflow
  1.1308 +	de = (int)(d0 >> Exp_shift);
  1.1309 +#ifndef IBM
  1.1310 +	z |= Exp_msk11;
  1.1311 +#endif
  1.1312 +#else
  1.1313 +	if ((de = (int)(d0 >> Exp_shift)))
  1.1314 +		z |= Exp_msk1;
  1.1315 +#endif
  1.1316 +#ifdef Pack_32
  1.1317 +	if ((y = d1)) {
  1.1318 +		if ((k = lo0bits(&y))) {
  1.1319 +			x[0] = y | z << (32 - k);
  1.1320 +			z >>= k;
  1.1321 +			}
  1.1322 +		else
  1.1323 +			x[0] = y;
  1.1324 +#ifndef Sudden_Underflow
  1.1325 +		i =
  1.1326 +#endif
  1.1327 +		    b->wds = (x[1] = z) ? 2 : 1;
  1.1328 +		}
  1.1329 +	else {
  1.1330 +		k = lo0bits(&z);
  1.1331 +		x[0] = z;
  1.1332 +#ifndef Sudden_Underflow
  1.1333 +		i =
  1.1334 +#endif
  1.1335 +		    b->wds = 1;
  1.1336 +		k += 32;
  1.1337 +		}
  1.1338 +#else
  1.1339 +	if (y = d1) {
  1.1340 +		if (k = lo0bits(&y))
  1.1341 +			if (k >= 16) {
  1.1342 +				x[0] = y | z << 32 - k & 0xffff;
  1.1343 +				x[1] = z >> k - 16 & 0xffff;
  1.1344 +				x[2] = z >> k;
  1.1345 +				i = 2;
  1.1346 +				}
  1.1347 +			else {
  1.1348 +				x[0] = y & 0xffff;
  1.1349 +				x[1] = y >> 16 | z << 16 - k & 0xffff;
  1.1350 +				x[2] = z >> k & 0xffff;
  1.1351 +				x[3] = z >> k+16;
  1.1352 +				i = 3;
  1.1353 +				}
  1.1354 +		else {
  1.1355 +			x[0] = y & 0xffff;
  1.1356 +			x[1] = y >> 16;
  1.1357 +			x[2] = z & 0xffff;
  1.1358 +			x[3] = z >> 16;
  1.1359 +			i = 3;
  1.1360 +			}
  1.1361 +		}
  1.1362 +	else {
  1.1363 +#ifdef DEBUG
  1.1364 +		if (!z)
  1.1365 +			Bug("Zero passed to d2b");
  1.1366 +#endif
  1.1367 +		k = lo0bits(&z);
  1.1368 +		if (k >= 16) {
  1.1369 +			x[0] = z;
  1.1370 +			i = 0;
  1.1371 +			}
  1.1372 +		else {
  1.1373 +			x[0] = z & 0xffff;
  1.1374 +			x[1] = z >> 16;
  1.1375 +			i = 1;
  1.1376 +			}
  1.1377 +		k += 32;
  1.1378 +		}
  1.1379 +	while(!x[i])
  1.1380 +		--i;
  1.1381 +	b->wds = i + 1;
  1.1382 +#endif
  1.1383 +#ifndef Sudden_Underflow
  1.1384 +	if (de) {
  1.1385 +#endif
  1.1386 +#ifdef IBM
  1.1387 +		*e = (de - Bias - (P-1) << 2) + k;
  1.1388 +		*bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1.1389 +#else
  1.1390 +		*e = de - Bias - (P-1) + k;
  1.1391 +		*bits = P - k;
  1.1392 +#endif
  1.1393 +#ifndef Sudden_Underflow
  1.1394 +		}
  1.1395 +	else {
  1.1396 +		*e = de - Bias - (P-1) + 1 + k;
  1.1397 +#ifdef Pack_32
  1.1398 +		*bits = 32*i - hi0bits(x[i-1]);
  1.1399 +#else
  1.1400 +		*bits = (i+2)*16 - hi0bits(x[i]);
  1.1401 +#endif
  1.1402 +		}
  1.1403 +#endif
  1.1404 +	return b;
  1.1405 +	}
  1.1406 +#undef d0
  1.1407 +#undef d1
  1.1408 +
  1.1409 + static double
  1.1410 +ratio
  1.1411 +#ifdef KR_headers
  1.1412 +	(a, b) Bigint *a, *b;
  1.1413 +#else
  1.1414 +	(Bigint *a, Bigint *b)
  1.1415 +#endif
  1.1416 +{
  1.1417 +	U da, db;
  1.1418 +	int k, ka, kb;
  1.1419 +
  1.1420 +	dval(&da) = b2d(a, &ka);
  1.1421 +	dval(&db) = b2d(b, &kb);
  1.1422 +#ifdef Pack_32
  1.1423 +	k = ka - kb + 32*(a->wds - b->wds);
  1.1424 +#else
  1.1425 +	k = ka - kb + 16*(a->wds - b->wds);
  1.1426 +#endif
  1.1427 +#ifdef IBM
  1.1428 +	if (k > 0) {
  1.1429 +		word0(&da) += (k >> 2)*Exp_msk1;
  1.1430 +		if (k &= 3)
  1.1431 +			dval(&da) *= 1 << k;
  1.1432 +		}
  1.1433 +	else {
  1.1434 +		k = -k;
  1.1435 +		word0(&db) += (k >> 2)*Exp_msk1;
  1.1436 +		if (k &= 3)
  1.1437 +			dval(&db) *= 1 << k;
  1.1438 +		}
  1.1439 +#else
  1.1440 +	if (k > 0)
  1.1441 +		word0(&da) += k*Exp_msk1;
  1.1442 +	else {
  1.1443 +		k = -k;
  1.1444 +		word0(&db) += k*Exp_msk1;
  1.1445 +		}
  1.1446 +#endif
  1.1447 +	return dval(&da) / dval(&db);
  1.1448 +	}
  1.1449 +
  1.1450 + static CONST double
  1.1451 +tens[] = {
  1.1452 +		1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1.1453 +		1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1.1454 +		1e20, 1e21, 1e22
  1.1455 +#ifdef VAX
  1.1456 +		, 1e23, 1e24
  1.1457 +#endif
  1.1458 +		};
  1.1459 +
  1.1460 + static CONST double
  1.1461 +#ifdef IEEE_Arith
  1.1462 +bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1.1463 +static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
  1.1464 +#ifdef Avoid_Underflow
  1.1465 +		9007199254740992.*9007199254740992.e-256
  1.1466 +		/* = 2^106 * 1e-256 */
  1.1467 +#else
  1.1468 +		1e-256
  1.1469 +#endif
  1.1470 +		};
  1.1471 +/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
  1.1472 +/* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
  1.1473 +#define Scale_Bit 0x10
  1.1474 +#define n_bigtens 5
  1.1475 +#else
  1.1476 +#ifdef IBM
  1.1477 +bigtens[] = { 1e16, 1e32, 1e64 };
  1.1478 +static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
  1.1479 +#define n_bigtens 3
  1.1480 +#else
  1.1481 +bigtens[] = { 1e16, 1e32 };
  1.1482 +static CONST double tinytens[] = { 1e-16, 1e-32 };
  1.1483 +#define n_bigtens 2
  1.1484 +#endif
  1.1485 +#endif
  1.1486 +
  1.1487 +#undef Need_Hexdig
  1.1488 +#ifdef INFNAN_CHECK
  1.1489 +#ifndef No_Hex_NaN
  1.1490 +#define Need_Hexdig
  1.1491 +#endif
  1.1492 +#endif
  1.1493 +
  1.1494 +#ifndef Need_Hexdig
  1.1495 +#ifndef NO_HEX_FP
  1.1496 +#define Need_Hexdig
  1.1497 +#endif
  1.1498 +#endif
  1.1499 +
  1.1500 +#ifdef Need_Hexdig /*{*/
  1.1501 +static unsigned char hexdig[256];
  1.1502 +
  1.1503 + static void
  1.1504 +#ifdef KR_headers
  1.1505 +htinit(h, s, inc) unsigned char *h; unsigned char *s; int inc;
  1.1506 +#else
  1.1507 +htinit(unsigned char *h, unsigned char *s, int inc)
  1.1508 +#endif
  1.1509 +{
  1.1510 +	int i, j;
  1.1511 +	for(i = 0; (j = s[i]) !=0; i++)
  1.1512 +		h[j] = i + inc;
  1.1513 +	}
  1.1514 +
  1.1515 + static void
  1.1516 +#ifdef KR_headers
  1.1517 +hexdig_init()
  1.1518 +#else
  1.1519 +hexdig_init(void)
  1.1520 +#endif
  1.1521 +{
  1.1522 +#define USC (unsigned char *)
  1.1523 +	htinit(hexdig, USC "0123456789", 0x10);
  1.1524 +	htinit(hexdig, USC "abcdef", 0x10 + 10);
  1.1525 +	htinit(hexdig, USC "ABCDEF", 0x10 + 10);
  1.1526 +	}
  1.1527 +#endif /* } Need_Hexdig */
  1.1528 +
  1.1529 +#ifdef INFNAN_CHECK
  1.1530 +
  1.1531 +#ifndef NAN_WORD0
  1.1532 +#define NAN_WORD0 0x7ff80000
  1.1533 +#endif
  1.1534 +
  1.1535 +#ifndef NAN_WORD1
  1.1536 +#define NAN_WORD1 0
  1.1537 +#endif
  1.1538 +
  1.1539 + static int
  1.1540 +match
  1.1541 +#ifdef KR_headers
  1.1542 +	(sp, t) char **sp, *t;
  1.1543 +#else
  1.1544 +	(const char **sp, const char *t)
  1.1545 +#endif
  1.1546 +{
  1.1547 +	int c, d;
  1.1548 +	CONST char *s = *sp;
  1.1549 +
  1.1550 +	while((d = *t++)) {
  1.1551 +		if ((c = *++s) >= 'A' && c <= 'Z')
  1.1552 +			c += 'a' - 'A';
  1.1553 +		if (c != d)
  1.1554 +			return 0;
  1.1555 +		}
  1.1556 +	*sp = s + 1;
  1.1557 +	return 1;
  1.1558 +	}
  1.1559 +
  1.1560 +#ifndef No_Hex_NaN
  1.1561 + static void
  1.1562 +hexnan
  1.1563 +#ifdef KR_headers
  1.1564 +	(rvp, sp) U *rvp; CONST char **sp;
  1.1565 +#else
  1.1566 +	(U *rvp, const char **sp)
  1.1567 +#endif
  1.1568 +{
  1.1569 +	ULong c, x[2];
  1.1570 +	CONST char *s;
  1.1571 +	int c1, havedig, udx0, xshift;
  1.1572 +
  1.1573 +	if (!hexdig['0'])
  1.1574 +		hexdig_init();
  1.1575 +	x[0] = x[1] = 0;
  1.1576 +	havedig = xshift = 0;
  1.1577 +	udx0 = 1;
  1.1578 +	s = *sp;
  1.1579 +	/* allow optional initial 0x or 0X */
  1.1580 +	while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
  1.1581 +		++s;
  1.1582 +	if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
  1.1583 +		s += 2;
  1.1584 +	while((c = *(CONST unsigned char*)++s)) {
  1.1585 +		if ((c1 = hexdig[c]))
  1.1586 +			c  = c1 & 0xf;
  1.1587 +		else if (c <= ' ') {
  1.1588 +			if (udx0 && havedig) {
  1.1589 +				udx0 = 0;
  1.1590 +				xshift = 1;
  1.1591 +				}
  1.1592 +			continue;
  1.1593 +			}
  1.1594 +#ifdef GDTOA_NON_PEDANTIC_NANCHECK
  1.1595 +		else if (/*(*/ c == ')' && havedig) {
  1.1596 +			*sp = s + 1;
  1.1597 +			break;
  1.1598 +			}
  1.1599 +		else
  1.1600 +			return;	/* invalid form: don't change *sp */
  1.1601 +#else
  1.1602 +		else {
  1.1603 +			do {
  1.1604 +				if (/*(*/ c == ')') {
  1.1605 +					*sp = s + 1;
  1.1606 +					break;
  1.1607 +					}
  1.1608 +				} while((c = *++s));
  1.1609 +			break;
  1.1610 +			}
  1.1611 +#endif
  1.1612 +		havedig = 1;
  1.1613 +		if (xshift) {
  1.1614 +			xshift = 0;
  1.1615 +			x[0] = x[1];
  1.1616 +			x[1] = 0;
  1.1617 +			}
  1.1618 +		if (udx0)
  1.1619 +			x[0] = (x[0] << 4) | (x[1] >> 28);
  1.1620 +		x[1] = (x[1] << 4) | c;
  1.1621 +		}
  1.1622 +	if ((x[0] &= 0xfffff) || x[1]) {
  1.1623 +		word0(rvp) = Exp_mask | x[0];
  1.1624 +		word1(rvp) = x[1];
  1.1625 +		}
  1.1626 +	}
  1.1627 +#endif /*No_Hex_NaN*/
  1.1628 +#endif /* INFNAN_CHECK */
  1.1629 +
  1.1630 +#ifdef Pack_32
  1.1631 +#define ULbits 32
  1.1632 +#define kshift 5
  1.1633 +#define kmask 31
  1.1634 +#else
  1.1635 +#define ULbits 16
  1.1636 +#define kshift 4
  1.1637 +#define kmask 15
  1.1638 +#endif
  1.1639 +
  1.1640 +#if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/
  1.1641 + static Bigint *
  1.1642 +#ifdef KR_headers
  1.1643 +increment(b) Bigint *b;
  1.1644 +#else
  1.1645 +increment(Bigint *b)
  1.1646 +#endif
  1.1647 +{
  1.1648 +	ULong *x, *xe;
  1.1649 +	Bigint *b1;
  1.1650 +
  1.1651 +	x = b->x;
  1.1652 +	xe = x + b->wds;
  1.1653 +	do {
  1.1654 +		if (*x < (ULong)0xffffffffL) {
  1.1655 +			++*x;
  1.1656 +			return b;
  1.1657 +			}
  1.1658 +		*x++ = 0;
  1.1659 +		} while(x < xe);
  1.1660 +	{
  1.1661 +		if (b->wds >= b->maxwds) {
  1.1662 +			b1 = Balloc(b->k+1);
  1.1663 +			Bcopy(b1,b);
  1.1664 +			Bfree(b);
  1.1665 +			b = b1;
  1.1666 +			}
  1.1667 +		b->x[b->wds++] = 1;
  1.1668 +		}
  1.1669 +	return b;
  1.1670 +	}
  1.1671 +
  1.1672 +#endif /*}*/
  1.1673 +
  1.1674 +#ifndef NO_HEX_FP /*{*/
  1.1675 +
  1.1676 + static void
  1.1677 +#ifdef KR_headers
  1.1678 +rshift(b, k) Bigint *b; int k;
  1.1679 +#else
  1.1680 +rshift(Bigint *b, int k)
  1.1681 +#endif
  1.1682 +{
  1.1683 +	ULong *x, *x1, *xe, y;
  1.1684 +	int n;
  1.1685 +
  1.1686 +	x = x1 = b->x;
  1.1687 +	n = k >> kshift;
  1.1688 +	if (n < b->wds) {
  1.1689 +		xe = x + b->wds;
  1.1690 +		x += n;
  1.1691 +		if (k &= kmask) {
  1.1692 +			n = 32 - k;
  1.1693 +			y = *x++ >> k;
  1.1694 +			while(x < xe) {
  1.1695 +				*x1++ = (y | (*x << n)) & 0xffffffff;
  1.1696 +				y = *x++ >> k;
  1.1697 +				}
  1.1698 +			if ((*x1 = y) !=0)
  1.1699 +				x1++;
  1.1700 +			}
  1.1701 +		else
  1.1702 +			while(x < xe)
  1.1703 +				*x1++ = *x++;
  1.1704 +		}
  1.1705 +	if ((b->wds = x1 - b->x) == 0)
  1.1706 +		b->x[0] = 0;
  1.1707 +	}
  1.1708 +
  1.1709 + static ULong
  1.1710 +#ifdef KR_headers
  1.1711 +any_on(b, k) Bigint *b; int k;
  1.1712 +#else
  1.1713 +any_on(Bigint *b, int k)
  1.1714 +#endif
  1.1715 +{
  1.1716 +	int n, nwds;
  1.1717 +	ULong *x, *x0, x1, x2;
  1.1718 +
  1.1719 +	x = b->x;
  1.1720 +	nwds = b->wds;
  1.1721 +	n = k >> kshift;
  1.1722 +	if (n > nwds)
  1.1723 +		n = nwds;
  1.1724 +	else if (n < nwds && (k &= kmask)) {
  1.1725 +		x1 = x2 = x[n];
  1.1726 +		x1 >>= k;
  1.1727 +		x1 <<= k;
  1.1728 +		if (x1 != x2)
  1.1729 +			return 1;
  1.1730 +		}
  1.1731 +	x0 = x;
  1.1732 +	x += n;
  1.1733 +	while(x > x0)
  1.1734 +		if (*--x)
  1.1735 +			return 1;
  1.1736 +	return 0;
  1.1737 +	}
  1.1738 +
  1.1739 +enum {	/* rounding values: same as FLT_ROUNDS */
  1.1740 +	Round_zero = 0,
  1.1741 +	Round_near = 1,
  1.1742 +	Round_up = 2,
  1.1743 +	Round_down = 3
  1.1744 +	};
  1.1745 +
  1.1746 + void
  1.1747 +#ifdef KR_headers
  1.1748 +gethex(sp, rvp, rounding, sign)
  1.1749 +	CONST char **sp; U *rvp; int rounding, sign;
  1.1750 +#else
  1.1751 +gethex( CONST char **sp, U *rvp, int rounding, int sign)
  1.1752 +#endif
  1.1753 +{
  1.1754 +	Bigint *b;
  1.1755 +	CONST unsigned char *decpt, *s0, *s, *s1;
  1.1756 +	Long e, e1;
  1.1757 +	ULong L, lostbits, *x;
  1.1758 +	int big, denorm, esign, havedig, k, n, nbits, up, zret;
  1.1759 +#ifdef IBM
  1.1760 +	int j;
  1.1761 +#endif
  1.1762 +	enum {
  1.1763 +#ifdef IEEE_Arith /*{{*/
  1.1764 +		emax = 0x7fe - Bias - P + 1,
  1.1765 +		emin = Emin - P + 1
  1.1766 +#else /*}{*/
  1.1767 +		emin = Emin - P,
  1.1768 +#ifdef VAX
  1.1769 +		emax = 0x7ff - Bias - P + 1
  1.1770 +#endif
  1.1771 +#ifdef IBM
  1.1772 +		emax = 0x7f - Bias - P
  1.1773 +#endif
  1.1774 +#endif /*}}*/
  1.1775 +		};
  1.1776 +#ifdef USE_LOCALE
  1.1777 +	int i;
  1.1778 +#ifdef NO_LOCALE_CACHE
  1.1779 +	const unsigned char *decimalpoint = (unsigned char*)
  1.1780 +		localeconv()->decimal_point;
  1.1781 +#else
  1.1782 +	const unsigned char *decimalpoint;
  1.1783 +	static unsigned char *decimalpoint_cache;
  1.1784 +	if (!(s0 = decimalpoint_cache)) {
  1.1785 +		s0 = (unsigned char*)localeconv()->decimal_point;
  1.1786 +		if ((decimalpoint_cache = (unsigned char*)
  1.1787 +				MALLOC(strlen((CONST char*)s0) + 1))) {
  1.1788 +			strcpy((char*)decimalpoint_cache, (CONST char*)s0);
  1.1789 +			s0 = decimalpoint_cache;
  1.1790 +			}
  1.1791 +		}
  1.1792 +	decimalpoint = s0;
  1.1793 +#endif
  1.1794 +#endif
  1.1795 +
  1.1796 +	if (!hexdig['0'])
  1.1797 +		hexdig_init();
  1.1798 +	havedig = 0;
  1.1799 +	s0 = *(CONST unsigned char **)sp + 2;
  1.1800 +	while(s0[havedig] == '0')
  1.1801 +		havedig++;
  1.1802 +	s0 += havedig;
  1.1803 +	s = s0;
  1.1804 +	decpt = 0;
  1.1805 +	zret = 0;
  1.1806 +	e = 0;
  1.1807 +	if (hexdig[*s])
  1.1808 +		havedig++;
  1.1809 +	else {
  1.1810 +		zret = 1;
  1.1811 +#ifdef USE_LOCALE
  1.1812 +		for(i = 0; decimalpoint[i]; ++i) {
  1.1813 +			if (s[i] != decimalpoint[i])
  1.1814 +				goto pcheck;
  1.1815 +			}
  1.1816 +		decpt = s += i;
  1.1817 +#else
  1.1818 +		if (*s != '.')
  1.1819 +			goto pcheck;
  1.1820 +		decpt = ++s;
  1.1821 +#endif
  1.1822 +		if (!hexdig[*s])
  1.1823 +			goto pcheck;
  1.1824 +		while(*s == '0')
  1.1825 +			s++;
  1.1826 +		if (hexdig[*s])
  1.1827 +			zret = 0;
  1.1828 +		havedig = 1;
  1.1829 +		s0 = s;
  1.1830 +		}
  1.1831 +	while(hexdig[*s])
  1.1832 +		s++;
  1.1833 +#ifdef USE_LOCALE
  1.1834 +	if (*s == *decimalpoint && !decpt) {
  1.1835 +		for(i = 1; decimalpoint[i]; ++i) {
  1.1836 +			if (s[i] != decimalpoint[i])
  1.1837 +				goto pcheck;
  1.1838 +			}
  1.1839 +		decpt = s += i;
  1.1840 +#else
  1.1841 +	if (*s == '.' && !decpt) {
  1.1842 +		decpt = ++s;
  1.1843 +#endif
  1.1844 +		while(hexdig[*s])
  1.1845 +			s++;
  1.1846 +		}/*}*/
  1.1847 +	if (decpt)
  1.1848 +		e = -(((Long)(s-decpt)) << 2);
  1.1849 + pcheck:
  1.1850 +	s1 = s;
  1.1851 +	big = esign = 0;
  1.1852 +	switch(*s) {
  1.1853 +	  case 'p':
  1.1854 +	  case 'P':
  1.1855 +		switch(*++s) {
  1.1856 +		  case '-':
  1.1857 +			esign = 1;
  1.1858 +			/* no break */
  1.1859 +		  case '+':
  1.1860 +			s++;
  1.1861 +		  }
  1.1862 +		if ((n = hexdig[*s]) == 0 || n > 0x19) {
  1.1863 +			s = s1;
  1.1864 +			break;
  1.1865 +			}
  1.1866 +		e1 = n - 0x10;
  1.1867 +		while((n = hexdig[*++s]) !=0 && n <= 0x19) {
  1.1868 +			if (e1 & 0xf8000000)
  1.1869 +				big = 1;
  1.1870 +			e1 = 10*e1 + n - 0x10;
  1.1871 +			}
  1.1872 +		if (esign)
  1.1873 +			e1 = -e1;
  1.1874 +		e += e1;
  1.1875 +	  }
  1.1876 +	*sp = (char*)s;
  1.1877 +	if (!havedig)
  1.1878 +		*sp = (char*)s0 - 1;
  1.1879 +	if (zret)
  1.1880 +		goto retz1;
  1.1881 +	if (big) {
  1.1882 +		if (esign) {
  1.1883 +#ifdef IEEE_Arith
  1.1884 +			switch(rounding) {
  1.1885 +			  case Round_up:
  1.1886 +				if (sign)
  1.1887 +					break;
  1.1888 +				goto ret_tiny;
  1.1889 +			  case Round_down:
  1.1890 +				if (!sign)
  1.1891 +					break;
  1.1892 +				goto ret_tiny;
  1.1893 +			  }
  1.1894 +#endif
  1.1895 +			goto retz;
  1.1896 +#ifdef IEEE_Arith
  1.1897 + ret_tiny:
  1.1898 +#ifndef NO_ERRNO
  1.1899 +			errno = ERANGE;
  1.1900 +#endif
  1.1901 +			word0(rvp) = 0;
  1.1902 +			word1(rvp) = 1;
  1.1903 +			return;
  1.1904 +#endif /* IEEE_Arith */
  1.1905 +			}
  1.1906 +		switch(rounding) {
  1.1907 +		  case Round_near:
  1.1908 +			goto ovfl1;
  1.1909 +		  case Round_up:
  1.1910 +			if (!sign)
  1.1911 +				goto ovfl1;
  1.1912 +			goto ret_big;
  1.1913 +		  case Round_down:
  1.1914 +			if (sign)
  1.1915 +				goto ovfl1;
  1.1916 +			goto ret_big;
  1.1917 +		  }
  1.1918 + ret_big:
  1.1919 +		word0(rvp) = Big0;
  1.1920 +		word1(rvp) = Big1;
  1.1921 +		return;
  1.1922 +		}
  1.1923 +	n = s1 - s0 - 1;
  1.1924 +	for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
  1.1925 +		k++;
  1.1926 +	b = Balloc(k);
  1.1927 +	x = b->x;
  1.1928 +	n = 0;
  1.1929 +	L = 0;
  1.1930 +#ifdef USE_LOCALE
  1.1931 +	for(i = 0; decimalpoint[i+1]; ++i);
  1.1932 +#endif
  1.1933 +	while(s1 > s0) {
  1.1934 +#ifdef USE_LOCALE
  1.1935 +		if (*--s1 == decimalpoint[i]) {
  1.1936 +			s1 -= i;
  1.1937 +			continue;
  1.1938 +			}
  1.1939 +#else
  1.1940 +		if (*--s1 == '.')
  1.1941 +			continue;
  1.1942 +#endif
  1.1943 +		if (n == ULbits) {
  1.1944 +			*x++ = L;
  1.1945 +			L = 0;
  1.1946 +			n = 0;
  1.1947 +			}
  1.1948 +		L |= (hexdig[*s1] & 0x0f) << n;
  1.1949 +		n += 4;
  1.1950 +		}
  1.1951 +	*x++ = L;
  1.1952 +	b->wds = n = x - b->x;
  1.1953 +	n = ULbits*n - hi0bits(L);
  1.1954 +	nbits = Nbits;
  1.1955 +	lostbits = 0;
  1.1956 +	x = b->x;
  1.1957 +	if (n > nbits) {
  1.1958 +		n -= nbits;
  1.1959 +		if (any_on(b,n)) {
  1.1960 +			lostbits = 1;
  1.1961 +			k = n - 1;
  1.1962 +			if (x[k>>kshift] & 1 << (k & kmask)) {
  1.1963 +				lostbits = 2;
  1.1964 +				if (k > 0 && any_on(b,k))
  1.1965 +					lostbits = 3;
  1.1966 +				}
  1.1967 +			}
  1.1968 +		rshift(b, n);
  1.1969 +		e += n;
  1.1970 +		}
  1.1971 +	else if (n < nbits) {
  1.1972 +		n = nbits - n;
  1.1973 +		b = lshift(b, n);
  1.1974 +		e -= n;
  1.1975 +		x = b->x;
  1.1976 +		}
  1.1977 +	if (e > Emax) {
  1.1978 + ovfl:
  1.1979 +		Bfree(b);
  1.1980 + ovfl1:
  1.1981 +#ifndef NO_ERRNO
  1.1982 +		errno = ERANGE;
  1.1983 +#endif
  1.1984 +		word0(rvp) = Exp_mask;
  1.1985 +		word1(rvp) = 0;
  1.1986 +		return;
  1.1987 +		}
  1.1988 +	denorm = 0;
  1.1989 +	if (e < emin) {
  1.1990 +		denorm = 1;
  1.1991 +		n = emin - e;
  1.1992 +		if (n >= nbits) {
  1.1993 +#ifdef IEEE_Arith /*{*/
  1.1994 +			switch (rounding) {
  1.1995 +			  case Round_near:
  1.1996 +				if (n == nbits && (n < 2 || any_on(b,n-1)))
  1.1997 +					goto ret_tiny;
  1.1998 +				break;
  1.1999 +			  case Round_up:
  1.2000 +				if (!sign)
  1.2001 +					goto ret_tiny;
  1.2002 +				break;
  1.2003 +			  case Round_down:
  1.2004 +				if (sign)
  1.2005 +					goto ret_tiny;
  1.2006 +			  }
  1.2007 +#endif /* } IEEE_Arith */
  1.2008 +			Bfree(b);
  1.2009 + retz:
  1.2010 +#ifndef NO_ERRNO
  1.2011 +			errno = ERANGE;
  1.2012 +#endif
  1.2013 + retz1:
  1.2014 +			rvp->d = 0.;
  1.2015 +			return;
  1.2016 +			}
  1.2017 +		k = n - 1;
  1.2018 +		if (lostbits)
  1.2019 +			lostbits = 1;
  1.2020 +		else if (k > 0)
  1.2021 +			lostbits = any_on(b,k);
  1.2022 +		if (x[k>>kshift] & 1 << (k & kmask))
  1.2023 +			lostbits |= 2;
  1.2024 +		nbits -= n;
  1.2025 +		rshift(b,n);
  1.2026 +		e = emin;
  1.2027 +		}
  1.2028 +	if (lostbits) {
  1.2029 +		up = 0;
  1.2030 +		switch(rounding) {
  1.2031 +		  case Round_zero:
  1.2032 +			break;
  1.2033 +		  case Round_near:
  1.2034 +			if (lostbits & 2
  1.2035 +			 && (lostbits & 1) | (x[0] & 1))
  1.2036 +				up = 1;
  1.2037 +			break;
  1.2038 +		  case Round_up:
  1.2039 +			up = 1 - sign;
  1.2040 +			break;
  1.2041 +		  case Round_down:
  1.2042 +			up = sign;
  1.2043 +		  }
  1.2044 +		if (up) {
  1.2045 +			k = b->wds;
  1.2046 +			b = increment(b);
  1.2047 +			x = b->x;
  1.2048 +			if (denorm) {
  1.2049 +#if 0
  1.2050 +				if (nbits == Nbits - 1
  1.2051 +				 && x[nbits >> kshift] & 1 << (nbits & kmask))
  1.2052 +					denorm = 0; /* not currently used */
  1.2053 +#endif
  1.2054 +				}
  1.2055 +			else if (b->wds > k
  1.2056 +			 || ((n = nbits & kmask) !=0
  1.2057 +			     && hi0bits(x[k-1]) < 32-n)) {
  1.2058 +				rshift(b,1);
  1.2059 +				if (++e > Emax)
  1.2060 +					goto ovfl;
  1.2061 +				}
  1.2062 +			}
  1.2063 +		}
  1.2064 +#ifdef IEEE_Arith
  1.2065 +	if (denorm)
  1.2066 +		word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;
  1.2067 +	else
  1.2068 +		word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);
  1.2069 +	word1(rvp) = b->x[0];
  1.2070 +#endif
  1.2071 +#ifdef IBM
  1.2072 +	if ((j = e & 3)) {
  1.2073 +		k = b->x[0] & ((1 << j) - 1);
  1.2074 +		rshift(b,j);
  1.2075 +		if (k) {
  1.2076 +			switch(rounding) {
  1.2077 +			  case Round_up:
  1.2078 +				if (!sign)
  1.2079 +					increment(b);
  1.2080 +				break;
  1.2081 +			  case Round_down:
  1.2082 +				if (sign)
  1.2083 +					increment(b);
  1.2084 +				break;
  1.2085 +			  case Round_near:
  1.2086 +				j = 1 << (j-1);
  1.2087 +				if (k & j && ((k & (j-1)) | lostbits))
  1.2088 +					increment(b);
  1.2089 +			  }
  1.2090 +			}
  1.2091 +		}
  1.2092 +	e >>= 2;
  1.2093 +	word0(rvp) = b->x[1] | ((e + 65 + 13) << 24);
  1.2094 +	word1(rvp) = b->x[0];
  1.2095 +#endif
  1.2096 +#ifdef VAX
  1.2097 +	/* The next two lines ignore swap of low- and high-order 2 bytes. */
  1.2098 +	/* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
  1.2099 +	/* word1(rvp) = b->x[0]; */
  1.2100 +	word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16);
  1.2101 +	word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16);
  1.2102 +#endif
  1.2103 +	Bfree(b);
  1.2104 +	}
  1.2105 +#endif /*!NO_HEX_FP}*/
  1.2106 +
  1.2107 + static int
  1.2108 +#ifdef KR_headers
  1.2109 +dshift(b, p2) Bigint *b; int p2;
  1.2110 +#else
  1.2111 +dshift(Bigint *b, int p2)
  1.2112 +#endif
  1.2113 +{
  1.2114 +	int rv = hi0bits(b->x[b->wds-1]) - 4;
  1.2115 +	if (p2 > 0)
  1.2116 +		rv -= p2;
  1.2117 +	return rv & kmask;
  1.2118 +	}
  1.2119 +
  1.2120 + static int
  1.2121 +quorem
  1.2122 +#ifdef KR_headers
  1.2123 +	(b, S) Bigint *b, *S;
  1.2124 +#else
  1.2125 +	(Bigint *b, Bigint *S)
  1.2126 +#endif
  1.2127 +{
  1.2128 +	int n;
  1.2129 +	ULong *bx, *bxe, q, *sx, *sxe;
  1.2130 +#ifdef ULLong
  1.2131 +	ULLong borrow, carry, y, ys;
  1.2132 +#else
  1.2133 +	ULong borrow, carry, y, ys;
  1.2134 +#ifdef Pack_32
  1.2135 +	ULong si, z, zs;
  1.2136 +#endif
  1.2137 +#endif
  1.2138 +
  1.2139 +	n = S->wds;
  1.2140 +#ifdef DEBUG
  1.2141 +	/*debug*/ if (b->wds > n)
  1.2142 +	/*debug*/	Bug("oversize b in quorem");
  1.2143 +#endif
  1.2144 +	if (b->wds < n)
  1.2145 +		return 0;
  1.2146 +	sx = S->x;
  1.2147 +	sxe = sx + --n;
  1.2148 +	bx = b->x;
  1.2149 +	bxe = bx + n;
  1.2150 +	q = *bxe / (*sxe + 1);	/* ensure q <= true quotient */
  1.2151 +#ifdef DEBUG
  1.2152 +#ifdef NO_STRTOD_BIGCOMP
  1.2153 +	/*debug*/ if (q > 9)
  1.2154 +#else
  1.2155 +	/* An oversized q is possible when quorem is called from bigcomp and */
  1.2156 +	/* the input is near, e.g., twice the smallest denormalized number. */
  1.2157 +	/*debug*/ if (q > 15)
  1.2158 +#endif
  1.2159 +	/*debug*/	Bug("oversized quotient in quorem");
  1.2160 +#endif
  1.2161 +	if (q) {
  1.2162 +		borrow = 0;
  1.2163 +		carry = 0;
  1.2164 +		do {
  1.2165 +#ifdef ULLong
  1.2166 +			ys = *sx++ * (ULLong)q + carry;
  1.2167 +			carry = ys >> 32;
  1.2168 +			y = *bx - (ys & FFFFFFFF) - borrow;
  1.2169 +			borrow = y >> 32 & (ULong)1;
  1.2170 +			*bx++ = y & FFFFFFFF;
  1.2171 +#else
  1.2172 +#ifdef Pack_32
  1.2173 +			si = *sx++;
  1.2174 +			ys = (si & 0xffff) * q + carry;
  1.2175 +			zs = (si >> 16) * q + (ys >> 16);
  1.2176 +			carry = zs >> 16;
  1.2177 +			y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  1.2178 +			borrow = (y & 0x10000) >> 16;
  1.2179 +			z = (*bx >> 16) - (zs & 0xffff) - borrow;
  1.2180 +			borrow = (z & 0x10000) >> 16;
  1.2181 +			Storeinc(bx, z, y);
  1.2182 +#else
  1.2183 +			ys = *sx++ * q + carry;
  1.2184 +			carry = ys >> 16;
  1.2185 +			y = *bx - (ys & 0xffff) - borrow;
  1.2186 +			borrow = (y & 0x10000) >> 16;
  1.2187 +			*bx++ = y & 0xffff;
  1.2188 +#endif
  1.2189 +#endif
  1.2190 +			}
  1.2191 +			while(sx <= sxe);
  1.2192 +		if (!*bxe) {
  1.2193 +			bx = b->x;
  1.2194 +			while(--bxe > bx && !*bxe)
  1.2195 +				--n;
  1.2196 +			b->wds = n;
  1.2197 +			}
  1.2198 +		}
  1.2199 +	if (cmp(b, S) >= 0) {
  1.2200 +		q++;
  1.2201 +		borrow = 0;
  1.2202 +		carry = 0;
  1.2203 +		bx = b->x;
  1.2204 +		sx = S->x;
  1.2205 +		do {
  1.2206 +#ifdef ULLong
  1.2207 +			ys = *sx++ + carry;
  1.2208 +			carry = ys >> 32;
  1.2209 +			y = *bx - (ys & FFFFFFFF) - borrow;
  1.2210 +			borrow = y >> 32 & (ULong)1;
  1.2211 +			*bx++ = y & FFFFFFFF;
  1.2212 +#else
  1.2213 +#ifdef Pack_32
  1.2214 +			si = *sx++;
  1.2215 +			ys = (si & 0xffff) + carry;
  1.2216 +			zs = (si >> 16) + (ys >> 16);
  1.2217 +			carry = zs >> 16;
  1.2218 +			y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  1.2219 +			borrow = (y & 0x10000) >> 16;
  1.2220 +			z = (*bx >> 16) - (zs & 0xffff) - borrow;
  1.2221 +			borrow = (z & 0x10000) >> 16;
  1.2222 +			Storeinc(bx, z, y);
  1.2223 +#else
  1.2224 +			ys = *sx++ + carry;
  1.2225 +			carry = ys >> 16;
  1.2226 +			y = *bx - (ys & 0xffff) - borrow;
  1.2227 +			borrow = (y & 0x10000) >> 16;
  1.2228 +			*bx++ = y & 0xffff;
  1.2229 +#endif
  1.2230 +#endif
  1.2231 +			}
  1.2232 +			while(sx <= sxe);
  1.2233 +		bx = b->x;
  1.2234 +		bxe = bx + n;
  1.2235 +		if (!*bxe) {
  1.2236 +			while(--bxe > bx && !*bxe)
  1.2237 +				--n;
  1.2238 +			b->wds = n;
  1.2239 +			}
  1.2240 +		}
  1.2241 +	return q;
  1.2242 +	}
  1.2243 +
  1.2244 +#if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/
  1.2245 + static double
  1.2246 +sulp
  1.2247 +#ifdef KR_headers
  1.2248 +	(x, bc) U *x; BCinfo *bc;
  1.2249 +#else
  1.2250 +	(U *x, BCinfo *bc)
  1.2251 +#endif
  1.2252 +{
  1.2253 +	U u;
  1.2254 +	double rv;
  1.2255 +	int i;
  1.2256 +
  1.2257 +	rv = ulp(x);
  1.2258 +	if (!bc->scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0)
  1.2259 +		return rv; /* Is there an example where i <= 0 ? */
  1.2260 +	word0(&u) = Exp_1 + (i << Exp_shift);
  1.2261 +	word1(&u) = 0;
  1.2262 +	return rv * u.d;
  1.2263 +	}
  1.2264 +#endif /*}*/
  1.2265 +
  1.2266 +#ifndef NO_STRTOD_BIGCOMP
  1.2267 + static void
  1.2268 +bigcomp
  1.2269 +#ifdef KR_headers
  1.2270 +	(rv, s0, bc)
  1.2271 +	U *rv; CONST char *s0; BCinfo *bc;
  1.2272 +#else
  1.2273 +	(U *rv, const char *s0, BCinfo *bc)
  1.2274 +#endif
  1.2275 +{
  1.2276 +	Bigint *b, *d;
  1.2277 +	int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase;
  1.2278 +
  1.2279 +	dsign = bc->dsign;
  1.2280 +	nd = bc->nd;
  1.2281 +	nd0 = bc->nd0;
  1.2282 +	p5 = nd + bc->e0 - 1;
  1.2283 +	speccase = 0;
  1.2284 +#ifndef Sudden_Underflow
  1.2285 +	if (rv->d == 0.) {	/* special case: value near underflow-to-zero */
  1.2286 +				/* threshold was rounded to zero */
  1.2287 +		b = i2b(1);
  1.2288 +		p2 = Emin - P + 1;
  1.2289 +		bbits = 1;
  1.2290 +#ifdef Avoid_Underflow
  1.2291 +		word0(rv) = (P+2) << Exp_shift;
  1.2292 +#else
  1.2293 +		word1(rv) = 1;
  1.2294 +#endif
  1.2295 +		i = 0;
  1.2296 +#ifdef Honor_FLT_ROUNDS
  1.2297 +		if (bc->rounding == 1)
  1.2298 +#endif
  1.2299 +			{
  1.2300 +			speccase = 1;
  1.2301 +			--p2;
  1.2302 +			dsign = 0;
  1.2303 +			goto have_i;
  1.2304 +			}
  1.2305 +		}
  1.2306 +	else
  1.2307 +#endif
  1.2308 +		b = d2b(rv, &p2, &bbits);
  1.2309 +#ifdef Avoid_Underflow
  1.2310 +	p2 -= bc->scale;
  1.2311 +#endif
  1.2312 +	/* floor(log2(rv)) == bbits - 1 + p2 */
  1.2313 +	/* Check for denormal case. */
  1.2314 +	i = P - bbits;
  1.2315 +	if (i > (j = P - Emin - 1 + p2)) {
  1.2316 +#ifdef Sudden_Underflow
  1.2317 +		Bfree(b);
  1.2318 +		b = i2b(1);
  1.2319 +		p2 = Emin;
  1.2320 +		i = P - 1;
  1.2321 +#ifdef Avoid_Underflow
  1.2322 +		word0(rv) = (1 + bc->scale) << Exp_shift;
  1.2323 +#else
  1.2324 +		word0(rv) = Exp_msk1;
  1.2325 +#endif
  1.2326 +		word1(rv) = 0;
  1.2327 +#else
  1.2328 +		i = j;
  1.2329 +#endif
  1.2330 +		}
  1.2331 +#ifdef Honor_FLT_ROUNDS
  1.2332 +	if (bc->rounding != 1) {
  1.2333 +		if (i > 0)
  1.2334 +			b = lshift(b, i);
  1.2335 +		if (dsign)
  1.2336 +			b = increment(b);
  1.2337 +		}
  1.2338 +	else
  1.2339 +#endif
  1.2340 +		{
  1.2341 +		b = lshift(b, ++i);
  1.2342 +		b->x[0] |= 1;
  1.2343 +		}
  1.2344 +#ifndef Sudden_Underflow
  1.2345 + have_i:
  1.2346 +#endif
  1.2347 +	p2 -= p5 + i;
  1.2348 +	d = i2b(1);
  1.2349 +	/* Arrange for convenient computation of quotients:
  1.2350 +	 * shift left if necessary so divisor has 4 leading 0 bits.
  1.2351 +	 */
  1.2352 +	if (p5 > 0)
  1.2353 +		d = pow5mult(d, p5);
  1.2354 +	else if (p5 < 0)
  1.2355 +		b = pow5mult(b, -p5);
  1.2356 +	if (p2 > 0) {
  1.2357 +		b2 = p2;
  1.2358 +		d2 = 0;
  1.2359 +		}
  1.2360 +	else {
  1.2361 +		b2 = 0;
  1.2362 +		d2 = -p2;
  1.2363 +		}
  1.2364 +	i = dshift(d, d2);
  1.2365 +	if ((b2 += i) > 0)
  1.2366 +		b = lshift(b, b2);
  1.2367 +	if ((d2 += i) > 0)
  1.2368 +		d = lshift(d, d2);
  1.2369 +
  1.2370 +	/* Now b/d = exactly half-way between the two floating-point values */
  1.2371 +	/* on either side of the input string.  Compute first digit of b/d. */
  1.2372 +
  1.2373 +	if (!(dig = quorem(b,d))) {
  1.2374 +		b = multadd(b, 10, 0);	/* very unlikely */
  1.2375 +		dig = quorem(b,d);
  1.2376 +		}
  1.2377 +
  1.2378 +	/* Compare b/d with s0 */
  1.2379 +
  1.2380 +	for(i = 0; i < nd0; ) {
  1.2381 +		if ((dd = s0[i++] - '0' - dig))
  1.2382 +			goto ret;
  1.2383 +		if (!b->x[0] && b->wds == 1) {
  1.2384 +			if (i < nd)
  1.2385 +				dd = 1;
  1.2386 +			goto ret;
  1.2387 +			}
  1.2388 +		b = multadd(b, 10, 0);
  1.2389 +		dig = quorem(b,d);
  1.2390 +		}
  1.2391 +	for(j = bc->dp1; i++ < nd;) {
  1.2392 +		if ((dd = s0[j++] - '0' - dig))
  1.2393 +			goto ret;
  1.2394 +		if (!b->x[0] && b->wds == 1) {
  1.2395 +			if (i < nd)
  1.2396 +				dd = 1;
  1.2397 +			goto ret;
  1.2398 +			}
  1.2399 +		b = multadd(b, 10, 0);
  1.2400 +		dig = quorem(b,d);
  1.2401 +		}
  1.2402 +	if (b->x[0] || b->wds > 1 || dig > 0)
  1.2403 +		dd = -1;
  1.2404 + ret:
  1.2405 +	Bfree(b);
  1.2406 +	Bfree(d);
  1.2407 +#ifdef Honor_FLT_ROUNDS
  1.2408 +	if (bc->rounding != 1) {
  1.2409 +		if (dd < 0) {
  1.2410 +			if (bc->rounding == 0) {
  1.2411 +				if (!dsign)
  1.2412 +					goto retlow1;
  1.2413 +				}
  1.2414 +			else if (dsign)
  1.2415 +				goto rethi1;
  1.2416 +			}
  1.2417 +		else if (dd > 0) {
  1.2418 +			if (bc->rounding == 0) {
  1.2419 +				if (dsign)
  1.2420 +					goto rethi1;
  1.2421 +				goto ret1;
  1.2422 +				}
  1.2423 +			if (!dsign)
  1.2424 +				goto rethi1;
  1.2425 +			dval(rv) += 2.*sulp(rv,bc);
  1.2426 +			}
  1.2427 +		else {
  1.2428 +			bc->inexact = 0;
  1.2429 +			if (dsign)
  1.2430 +				goto rethi1;
  1.2431 +			}
  1.2432 +		}
  1.2433 +	else
  1.2434 +#endif
  1.2435 +	if (speccase) {
  1.2436 +		if (dd <= 0)
  1.2437 +			rv->d = 0.;
  1.2438 +		}
  1.2439 +	else if (dd < 0) {
  1.2440 +		if (!dsign)	/* does not happen for round-near */
  1.2441 +retlow1:
  1.2442 +			dval(rv) -= sulp(rv,bc);
  1.2443 +		}
  1.2444 +	else if (dd > 0) {
  1.2445 +		if (dsign) {
  1.2446 + rethi1:
  1.2447 +			dval(rv) += sulp(rv,bc);
  1.2448 +			}
  1.2449 +		}
  1.2450 +	else {
  1.2451 +		/* Exact half-way case:  apply round-even rule. */
  1.2452 +		if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) {
  1.2453 +			i = 1 - j;
  1.2454 +			if (i <= 31) {
  1.2455 +				if (word1(rv) & (0x1 << i))
  1.2456 +					goto odd;
  1.2457 +				}
  1.2458 +			else if (word0(rv) & (0x1 << (i-32)))
  1.2459 +				goto odd;
  1.2460 +			}
  1.2461 +		else if (word1(rv) & 1) {
  1.2462 + odd:
  1.2463 +			if (dsign)
  1.2464 +				goto rethi1;
  1.2465 +			goto retlow1;
  1.2466 +			}
  1.2467 +		}
  1.2468 +
  1.2469 +#ifdef Honor_FLT_ROUNDS
  1.2470 + ret1:
  1.2471 +#endif
  1.2472 +	return;
  1.2473 +	}
  1.2474 +#endif /* NO_STRTOD_BIGCOMP */
  1.2475 +
  1.2476 + double
  1.2477 +strtod
  1.2478 +#ifdef KR_headers
  1.2479 +	(s00, se) CONST char *s00; char **se;
  1.2480 +#else
  1.2481 +	(const char *s00, char **se)
  1.2482 +#endif
  1.2483 +{
  1.2484 +	int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
  1.2485 +	int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
  1.2486 +	CONST char *s, *s0, *s1;
  1.2487 +	double aadj, aadj1;
  1.2488 +	Long L;
  1.2489 +	U aadj2, adj, rv, rv0;
  1.2490 +	ULong y, z;
  1.2491 +	BCinfo bc;
  1.2492 +	Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
  1.2493 +#ifdef Avoid_Underflow
  1.2494 +	ULong Lsb, Lsb1;
  1.2495 +#endif
  1.2496 +#ifdef SET_INEXACT
  1.2497 +	int oldinexact;
  1.2498 +#endif
  1.2499 +#ifndef NO_STRTOD_BIGCOMP
  1.2500 +	int req_bigcomp = 0;
  1.2501 +#endif
  1.2502 +#ifdef Honor_FLT_ROUNDS /*{*/
  1.2503 +#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
  1.2504 +	bc.rounding = Flt_Rounds;
  1.2505 +#else /*}{*/
  1.2506 +	bc.rounding = 1;
  1.2507 +	switch(fegetround()) {
  1.2508 +	  case FE_TOWARDZERO:	bc.rounding = 0; break;
  1.2509 +	  case FE_UPWARD:	bc.rounding = 2; break;
  1.2510 +	  case FE_DOWNWARD:	bc.rounding = 3;
  1.2511 +	  }
  1.2512 +#endif /*}}*/
  1.2513 +#endif /*}*/
  1.2514 +#ifdef USE_LOCALE
  1.2515 +	CONST char *s2;
  1.2516 +#endif
  1.2517 +
  1.2518 +	sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
  1.2519 +	dval(&rv) = 0.;
  1.2520 +	for(s = s00;;s++) switch(*s) {
  1.2521 +		case '-':
  1.2522 +			sign = 1;
  1.2523 +			/* no break */
  1.2524 +		case '+':
  1.2525 +			if (*++s)
  1.2526 +				goto break2;
  1.2527 +			/* no break */
  1.2528 +		case 0:
  1.2529 +			goto ret0;
  1.2530 +		case '\t':
  1.2531 +		case '\n':
  1.2532 +		case '\v':
  1.2533 +		case '\f':
  1.2534 +		case '\r':
  1.2535 +		case ' ':
  1.2536 +			continue;
  1.2537 +		default:
  1.2538 +			goto break2;
  1.2539 +		}
  1.2540 + break2:
  1.2541 +	if (*s == '0') {
  1.2542 +#ifndef NO_HEX_FP /*{*/
  1.2543 +		switch(s[1]) {
  1.2544 +		  case 'x':
  1.2545 +		  case 'X':
  1.2546 +#ifdef Honor_FLT_ROUNDS
  1.2547 +			gethex(&s, &rv, bc.rounding, sign);
  1.2548 +#else
  1.2549 +			gethex(&s, &rv, 1, sign);
  1.2550 +#endif
  1.2551 +			goto ret;
  1.2552 +		  }
  1.2553 +#endif /*}*/
  1.2554 +		nz0 = 1;
  1.2555 +		while(*++s == '0') ;
  1.2556 +		if (!*s)
  1.2557 +			goto ret;
  1.2558 +		}
  1.2559 +	s0 = s;
  1.2560 +	y = z = 0;
  1.2561 +	for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
  1.2562 +		if (nd < 9)
  1.2563 +			y = 10*y + c - '0';
  1.2564 +		else if (nd < 16)
  1.2565 +			z = 10*z + c - '0';
  1.2566 +	nd0 = nd;
  1.2567 +	bc.dp0 = bc.dp1 = s - s0;
  1.2568 +	for(s1 = s; s1 > s0 && *--s1 == '0'; )
  1.2569 +		++nz1;
  1.2570 +#ifdef USE_LOCALE
  1.2571 +	s1 = localeconv()->decimal_point;
  1.2572 +	if (c == *s1) {
  1.2573 +		c = '.';
  1.2574 +		if (*++s1) {
  1.2575 +			s2 = s;
  1.2576 +			for(;;) {
  1.2577 +				if (*++s2 != *s1) {
  1.2578 +					c = 0;
  1.2579 +					break;
  1.2580 +					}
  1.2581 +				if (!*++s1) {
  1.2582 +					s = s2;
  1.2583 +					break;
  1.2584 +					}
  1.2585 +				}
  1.2586 +			}
  1.2587 +		}
  1.2588 +#endif
  1.2589 +	if (c == '.') {
  1.2590 +		c = *++s;
  1.2591 +		bc.dp1 = s - s0;
  1.2592 +		bc.dplen = bc.dp1 - bc.dp0;
  1.2593 +		if (!nd) {
  1.2594 +			for(; c == '0'; c = *++s)
  1.2595 +				nz++;
  1.2596 +			if (c > '0' && c <= '9') {
  1.2597 +				bc.dp0 = s0 - s;
  1.2598 +				bc.dp1 = bc.dp0 + bc.dplen;
  1.2599 +				s0 = s;
  1.2600 +				nf += nz;
  1.2601 +				nz = 0;
  1.2602 +				goto have_dig;
  1.2603 +				}
  1.2604 +			goto dig_done;
  1.2605 +			}
  1.2606 +		for(; c >= '0' && c <= '9'; c = *++s) {
  1.2607 + have_dig:
  1.2608 +			nz++;
  1.2609 +			if (c -= '0') {
  1.2610 +				nf += nz;
  1.2611 +				for(i = 1; i < nz; i++)
  1.2612 +					if (nd++ < 9)
  1.2613 +						y *= 10;
  1.2614 +					else if (nd <= DBL_DIG + 1)
  1.2615 +						z *= 10;
  1.2616 +				if (nd++ < 9)
  1.2617 +					y = 10*y + c;
  1.2618 +				else if (nd <= DBL_DIG + 1)
  1.2619 +					z = 10*z + c;
  1.2620 +				nz = nz1 = 0;
  1.2621 +				}
  1.2622 +			}
  1.2623 +		}
  1.2624 + dig_done:
  1.2625 +	e = 0;
  1.2626 +	if (c == 'e' || c == 'E') {
  1.2627 +		if (!nd && !nz && !nz0) {
  1.2628 +			goto ret0;
  1.2629 +			}
  1.2630 +		s00 = s;
  1.2631 +		esign = 0;
  1.2632 +		switch(c = *++s) {
  1.2633 +			case '-':
  1.2634 +				esign = 1;
  1.2635 +			case '+':
  1.2636 +				c = *++s;
  1.2637 +			}
  1.2638 +		if (c >= '0' && c <= '9') {
  1.2639 +			while(c == '0')
  1.2640 +				c = *++s;
  1.2641 +			if (c > '0' && c <= '9') {
  1.2642 +				L = c - '0';
  1.2643 +				s1 = s;
  1.2644 +				while((c = *++s) >= '0' && c <= '9')
  1.2645 +					L = 10*L + c - '0';
  1.2646 +				if (s - s1 > 8 || L > 19999)
  1.2647 +					/* Avoid confusion from exponents
  1.2648 +					 * so large that e might overflow.
  1.2649 +					 */
  1.2650 +					e = 19999; /* safe for 16 bit ints */
  1.2651 +				else
  1.2652 +					e = (int)L;
  1.2653 +				if (esign)
  1.2654 +					e = -e;
  1.2655 +				}
  1.2656 +			else
  1.2657 +				e = 0;
  1.2658 +			}
  1.2659 +		else
  1.2660 +			s = s00;
  1.2661 +		}
  1.2662 +	if (!nd) {
  1.2663 +		if (!nz && !nz0) {
  1.2664 +#ifdef INFNAN_CHECK
  1.2665 +			/* Check for Nan and Infinity */
  1.2666 +			if (!bc.dplen)
  1.2667 +			 switch(c) {
  1.2668 +			  case 'i':
  1.2669 +			  case 'I':
  1.2670 +				if (match(&s,"nf")) {
  1.2671 +					--s;
  1.2672 +					if (!match(&s,"inity"))
  1.2673 +						++s;
  1.2674 +					word0(&rv) = 0x7ff00000;
  1.2675 +					word1(&rv) = 0;
  1.2676 +					goto ret;
  1.2677 +					}
  1.2678 +				break;
  1.2679 +			  case 'n':
  1.2680 +			  case 'N':
  1.2681 +				if (match(&s, "an")) {
  1.2682 +					word0(&rv) = NAN_WORD0;
  1.2683 +					word1(&rv) = NAN_WORD1;
  1.2684 +#ifndef No_Hex_NaN
  1.2685 +					if (*s == '(') /*)*/
  1.2686 +						hexnan(&rv, &s);
  1.2687 +#endif
  1.2688 +					goto ret;
  1.2689 +					}
  1.2690 +			  }
  1.2691 +#endif /* INFNAN_CHECK */
  1.2692 + ret0:
  1.2693 +			s = s00;
  1.2694 +			sign = 0;
  1.2695 +			}
  1.2696 +		goto ret;
  1.2697 +		}
  1.2698 +	bc.e0 = e1 = e -= nf;
  1.2699 +
  1.2700 +	/* Now we have nd0 digits, starting at s0, followed by a
  1.2701 +	 * decimal point, followed by nd-nd0 digits.  The number we're
  1.2702 +	 * after is the integer represented by those digits times
  1.2703 +	 * 10**e */
  1.2704 +
  1.2705 +	if (!nd0)
  1.2706 +		nd0 = nd;
  1.2707 +	k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  1.2708 +	dval(&rv) = y;
  1.2709 +	if (k > 9) {
  1.2710 +#ifdef SET_INEXACT
  1.2711 +		if (k > DBL_DIG)
  1.2712 +			oldinexact = get_inexact();
  1.2713 +#endif
  1.2714 +		dval(&rv) = tens[k - 9] * dval(&rv) + z;
  1.2715 +		}
  1.2716 +	bd0 = 0;
  1.2717 +	if (nd <= DBL_DIG
  1.2718 +#ifndef RND_PRODQUOT
  1.2719 +#ifndef Honor_FLT_ROUNDS
  1.2720 +		&& Flt_Rounds == 1
  1.2721 +#endif
  1.2722 +#endif
  1.2723 +			) {
  1.2724 +		if (!e)
  1.2725 +			goto ret;
  1.2726 +#ifndef ROUND_BIASED_without_Round_Up
  1.2727 +		if (e > 0) {
  1.2728 +			if (e <= Ten_pmax) {
  1.2729 +#ifdef VAX
  1.2730 +				goto vax_ovfl_check;
  1.2731 +#else
  1.2732 +#ifdef Honor_FLT_ROUNDS
  1.2733 +				/* round correctly FLT_ROUNDS = 2 or 3 */
  1.2734 +				if (sign) {
  1.2735 +					rv.d = -rv.d;
  1.2736 +					sign = 0;
  1.2737 +					}
  1.2738 +#endif
  1.2739 +				/* rv = */ rounded_product(dval(&rv), tens[e]);
  1.2740 +				goto ret;
  1.2741 +#endif
  1.2742 +				}
  1.2743 +			i = DBL_DIG - nd;
  1.2744 +			if (e <= Ten_pmax + i) {
  1.2745 +				/* A fancier test would sometimes let us do
  1.2746 +				 * this for larger i values.
  1.2747 +				 */
  1.2748 +#ifdef Honor_FLT_ROUNDS
  1.2749 +				/* round correctly FLT_ROUNDS = 2 or 3 */
  1.2750 +				if (sign) {
  1.2751 +					rv.d = -rv.d;
  1.2752 +					sign = 0;
  1.2753 +					}
  1.2754 +#endif
  1.2755 +				e -= i;
  1.2756 +				dval(&rv) *= tens[i];
  1.2757 +#ifdef VAX
  1.2758 +				/* VAX exponent range is so narrow we must
  1.2759 +				 * worry about overflow here...
  1.2760 +				 */
  1.2761 + vax_ovfl_check:
  1.2762 +				word0(&rv) -= P*Exp_msk1;
  1.2763 +				/* rv = */ rounded_product(dval(&rv), tens[e]);
  1.2764 +				if ((word0(&rv) & Exp_mask)
  1.2765 +				 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
  1.2766 +					goto ovfl;
  1.2767 +				word0(&rv) += P*Exp_msk1;
  1.2768 +#else
  1.2769 +				/* rv = */ rounded_product(dval(&rv), tens[e]);
  1.2770 +#endif
  1.2771 +				goto ret;
  1.2772 +				}
  1.2773 +			}
  1.2774 +#ifndef Inaccurate_Divide
  1.2775 +		else if (e >= -Ten_pmax) {
  1.2776 +#ifdef Honor_FLT_ROUNDS
  1.2777 +			/* round correctly FLT_ROUNDS = 2 or 3 */
  1.2778 +			if (sign) {
  1.2779 +				rv.d = -rv.d;
  1.2780 +				sign = 0;
  1.2781 +				}
  1.2782 +#endif
  1.2783 +			/* rv = */ rounded_quotient(dval(&rv), tens[-e]);
  1.2784 +			goto ret;
  1.2785 +			}
  1.2786 +#endif
  1.2787 +#endif /* ROUND_BIASED_without_Round_Up */
  1.2788 +		}
  1.2789 +	e1 += nd - k;
  1.2790 +
  1.2791 +#ifdef IEEE_Arith
  1.2792 +#ifdef SET_INEXACT
  1.2793 +	bc.inexact = 1;
  1.2794 +	if (k <= DBL_DIG)
  1.2795 +		oldinexact = get_inexact();
  1.2796 +#endif
  1.2797 +#ifdef Avoid_Underflow
  1.2798 +	bc.scale = 0;
  1.2799 +#endif
  1.2800 +#ifdef Honor_FLT_ROUNDS
  1.2801 +	if (bc.rounding >= 2) {
  1.2802 +		if (sign)
  1.2803 +			bc.rounding = bc.rounding == 2 ? 0 : 2;
  1.2804 +		else
  1.2805 +			if (bc.rounding != 2)
  1.2806 +				bc.rounding = 0;
  1.2807 +		}
  1.2808 +#endif
  1.2809 +#endif /*IEEE_Arith*/
  1.2810 +
  1.2811 +	/* Get starting approximation = rv * 10**e1 */
  1.2812 +
  1.2813 +	if (e1 > 0) {
  1.2814 +		if ((i = e1 & 15))
  1.2815 +			dval(&rv) *= tens[i];
  1.2816 +		if (e1 &= ~15) {
  1.2817 +			if (e1 > DBL_MAX_10_EXP) {
  1.2818 + ovfl:
  1.2819 +				/* Can't trust HUGE_VAL */
  1.2820 +#ifdef IEEE_Arith
  1.2821 +#ifdef Honor_FLT_ROUNDS
  1.2822 +				switch(bc.rounding) {
  1.2823 +				  case 0: /* toward 0 */
  1.2824 +				  case 3: /* toward -infinity */
  1.2825 +					word0(&rv) = Big0;
  1.2826 +					word1(&rv) = Big1;
  1.2827 +					break;
  1.2828 +				  default:
  1.2829 +					word0(&rv) = Exp_mask;
  1.2830 +					word1(&rv) = 0;
  1.2831 +				  }
  1.2832 +#else /*Honor_FLT_ROUNDS*/
  1.2833 +				word0(&rv) = Exp_mask;
  1.2834 +				word1(&rv) = 0;
  1.2835 +#endif /*Honor_FLT_ROUNDS*/
  1.2836 +#ifdef SET_INEXACT
  1.2837 +				/* set overflow bit */
  1.2838 +				dval(&rv0) = 1e300;
  1.2839 +				dval(&rv0) *= dval(&rv0);
  1.2840 +#endif
  1.2841 +#else /*IEEE_Arith*/
  1.2842 +				word0(&rv) = Big0;
  1.2843 +				word1(&rv) = Big1;
  1.2844 +#endif /*IEEE_Arith*/
  1.2845 + range_err:
  1.2846 +				if (bd0) {
  1.2847 +					Bfree(bb);
  1.2848 +					Bfree(bd);
  1.2849 +					Bfree(bs);
  1.2850 +					Bfree(bd0);
  1.2851 +					Bfree(delta);
  1.2852 +					}
  1.2853 +#ifndef NO_ERRNO
  1.2854 +				errno = ERANGE;
  1.2855 +#endif
  1.2856 +				goto ret;
  1.2857 +				}
  1.2858 +			e1 >>= 4;
  1.2859 +			for(j = 0; e1 > 1; j++, e1 >>= 1)
  1.2860 +				if (e1 & 1)
  1.2861 +					dval(&rv) *= bigtens[j];
  1.2862 +		/* The last multiplication could overflow. */
  1.2863 +			word0(&rv) -= P*Exp_msk1;
  1.2864 +			dval(&rv) *= bigtens[j];
  1.2865 +			if ((z = word0(&rv) & Exp_mask)
  1.2866 +			 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  1.2867 +				goto ovfl;
  1.2868 +			if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  1.2869 +				/* set to largest number */
  1.2870 +				/* (Can't trust DBL_MAX) */
  1.2871 +				word0(&rv) = Big0;
  1.2872 +				word1(&rv) = Big1;
  1.2873 +				}
  1.2874 +			else
  1.2875 +				word0(&rv) += P*Exp_msk1;
  1.2876 +			}
  1.2877 +		}
  1.2878 +	else if (e1 < 0) {
  1.2879 +		e1 = -e1;
  1.2880 +		if ((i = e1 & 15))
  1.2881 +			dval(&rv) /= tens[i];
  1.2882 +		if (e1 >>= 4) {
  1.2883 +			if (e1 >= 1 << n_bigtens)
  1.2884 +				goto undfl;
  1.2885 +#ifdef Avoid_Underflow
  1.2886 +			if (e1 & Scale_Bit)
  1.2887 +				bc.scale = 2*P;
  1.2888 +			for(j = 0; e1 > 0; j++, e1 >>= 1)
  1.2889 +				if (e1 & 1)
  1.2890 +					dval(&rv) *= tinytens[j];
  1.2891 +			if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
  1.2892 +						>> Exp_shift)) > 0) {
  1.2893 +				/* scaled rv is denormal; clear j low bits */
  1.2894 +				if (j >= 32) {
  1.2895 +					if (j > 54)
  1.2896 +						goto undfl;
  1.2897 +					word1(&rv) = 0;
  1.2898 +					if (j >= 53)
  1.2899 +					 word0(&rv) = (P+2)*Exp_msk1;
  1.2900 +					else
  1.2901 +					 word0(&rv) &= 0xffffffff << (j-32);
  1.2902 +					}
  1.2903 +				else
  1.2904 +					word1(&rv) &= 0xffffffff << j;
  1.2905 +				}
  1.2906 +#else
  1.2907 +			for(j = 0; e1 > 1; j++, e1 >>= 1)
  1.2908 +				if (e1 & 1)
  1.2909 +					dval(&rv) *= tinytens[j];
  1.2910 +			/* The last multiplication could underflow. */
  1.2911 +			dval(&rv0) = dval(&rv);
  1.2912 +			dval(&rv) *= tinytens[j];
  1.2913 +			if (!dval(&rv)) {
  1.2914 +				dval(&rv) = 2.*dval(&rv0);
  1.2915 +				dval(&rv) *= tinytens[j];
  1.2916 +#endif
  1.2917 +				if (!dval(&rv)) {
  1.2918 + undfl:
  1.2919 +					dval(&rv) = 0.;
  1.2920 +					goto range_err;
  1.2921 +					}
  1.2922 +#ifndef Avoid_Underflow
  1.2923 +				word0(&rv) = Tiny0;
  1.2924 +				word1(&rv) = Tiny1;
  1.2925 +				/* The refinement below will clean
  1.2926 +				 * this approximation up.
  1.2927 +				 */
  1.2928 +				}
  1.2929 +#endif
  1.2930 +			}
  1.2931 +		}
  1.2932 +
  1.2933 +	/* Now the hard part -- adjusting rv to the correct value.*/
  1.2934 +
  1.2935 +	/* Put digits into bd: true value = bd * 10^e */
  1.2936 +
  1.2937 +	bc.nd = nd - nz1;
  1.2938 +#ifndef NO_STRTOD_BIGCOMP
  1.2939 +	bc.nd0 = nd0;	/* Only needed if nd > strtod_diglim, but done here */
  1.2940 +			/* to silence an erroneous warning about bc.nd0 */
  1.2941 +			/* possibly not being initialized. */
  1.2942 +	if (nd > strtod_diglim) {
  1.2943 +		/* ASSERT(strtod_diglim >= 18); 18 == one more than the */
  1.2944 +		/* minimum number of decimal digits to distinguish double values */
  1.2945 +		/* in IEEE arithmetic. */
  1.2946 +		i = j = 18;
  1.2947 +		if (i > nd0)
  1.2948 +			j += bc.dplen;
  1.2949 +		for(;;) {
  1.2950 +			if (--j < bc.dp1 && j >= bc.dp0)
  1.2951 +				j = bc.dp0 - 1;
  1.2952 +			if (s0[j] != '0')
  1.2953 +				break;
  1.2954 +			--i;
  1.2955 +			}
  1.2956 +		e += nd - i;
  1.2957 +		nd = i;
  1.2958 +		if (nd0 > nd)
  1.2959 +			nd0 = nd;
  1.2960 +		if (nd < 9) { /* must recompute y */
  1.2961 +			y = 0;
  1.2962 +			for(i = 0; i < nd0; ++i)
  1.2963 +				y = 10*y + s0[i] - '0';
  1.2964 +			for(j = bc.dp1; i < nd; ++i)
  1.2965 +				y = 10*y + s0[j++] - '0';
  1.2966 +			}
  1.2967 +		}
  1.2968 +#endif
  1.2969 +	bd0 = s2b(s0, nd0, nd, y, bc.dplen);
  1.2970 +
  1.2971 +	for(;;) {
  1.2972 +		bd = Balloc(bd0->k);
  1.2973 +		Bcopy(bd, bd0);
  1.2974 +		bb = d2b(&rv, &bbe, &bbbits);	/* rv = bb * 2^bbe */
  1.2975 +		bs = i2b(1);
  1.2976 +
  1.2977 +		if (e >= 0) {
  1.2978 +			bb2 = bb5 = 0;
  1.2979 +			bd2 = bd5 = e;
  1.2980 +			}
  1.2981 +		else {
  1.2982 +			bb2 = bb5 = -e;
  1.2983 +			bd2 = bd5 = 0;
  1.2984 +			}
  1.2985 +		if (bbe >= 0)
  1.2986 +			bb2 += bbe;
  1.2987 +		else
  1.2988 +			bd2 -= bbe;
  1.2989 +		bs2 = bb2;
  1.2990 +#ifdef Honor_FLT_ROUNDS
  1.2991 +		if (bc.rounding != 1)
  1.2992 +			bs2++;
  1.2993 +#endif
  1.2994 +#ifdef Avoid_Underflow
  1.2995 +		Lsb = LSB;
  1.2996 +		Lsb1 = 0;
  1.2997 +		j = bbe - bc.scale;
  1.2998 +		i = j + bbbits - 1;	/* logb(rv) */
  1.2999 +		j = P + 1 - bbbits;
  1.3000 +		if (i < Emin) {	/* denormal */
  1.3001 +			i = Emin - i;
  1.3002 +			j -= i;
  1.3003 +			if (i < 32)
  1.3004 +				Lsb <<= i;
  1.3005 +			else if (i < 52)
  1.3006 +				Lsb1 = Lsb << (i-32);
  1.3007 +			else
  1.3008 +				Lsb1 = Exp_mask;
  1.3009 +			}
  1.3010 +#else /*Avoid_Underflow*/
  1.3011 +#ifdef Sudden_Underflow
  1.3012 +#ifdef IBM
  1.3013 +		j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
  1.3014 +#else
  1.3015 +		j = P + 1 - bbbits;
  1.3016 +#endif
  1.3017 +#else /*Sudden_Underflow*/
  1.3018 +		j = bbe;
  1.3019 +		i = j + bbbits - 1;	/* logb(rv) */
  1.3020 +		if (i < Emin)	/* denormal */
  1.3021 +			j += P - Emin;
  1.3022 +		else
  1.3023 +			j = P + 1 - bbbits;
  1.3024 +#endif /*Sudden_Underflow*/
  1.3025 +#endif /*Avoid_Underflow*/
  1.3026 +		bb2 += j;
  1.3027 +		bd2 += j;
  1.3028 +#ifdef Avoid_Underflow
  1.3029 +		bd2 += bc.scale;
  1.3030 +#endif
  1.3031 +		i = bb2 < bd2 ? bb2 : bd2;
  1.3032 +		if (i > bs2)
  1.3033 +			i = bs2;
  1.3034 +		if (i > 0) {
  1.3035 +			bb2 -= i;
  1.3036 +			bd2 -= i;
  1.3037 +			bs2 -= i;
  1.3038 +			}
  1.3039 +		if (bb5 > 0) {
  1.3040 +			bs = pow5mult(bs, bb5);
  1.3041 +			bb1 = mult(bs, bb);
  1.3042 +			Bfree(bb);
  1.3043 +			bb = bb1;
  1.3044 +			}
  1.3045 +		if (bb2 > 0)
  1.3046 +			bb = lshift(bb, bb2);
  1.3047 +		if (bd5 > 0)
  1.3048 +			bd = pow5mult(bd, bd5);
  1.3049 +		if (bd2 > 0)
  1.3050 +			bd = lshift(bd, bd2);
  1.3051 +		if (bs2 > 0)
  1.3052 +			bs = lshift(bs, bs2);
  1.3053 +		delta = diff(bb, bd);
  1.3054 +		bc.dsign = delta->sign;
  1.3055 +		delta->sign = 0;
  1.3056 +		i = cmp(delta, bs);
  1.3057 +#ifndef NO_STRTOD_BIGCOMP /*{*/
  1.3058 +		if (bc.nd > nd && i <= 0) {
  1.3059 +			if (bc.dsign) {
  1.3060 +				/* Must use bigcomp(). */
  1.3061 +				req_bigcomp = 1;
  1.3062 +				break;
  1.3063 +				}
  1.3064 +#ifdef Honor_FLT_ROUNDS
  1.3065 +			if (bc.rounding != 1) {
  1.3066 +				if (i < 0) {
  1.3067 +					req_bigcomp = 1;
  1.3068 +					break;
  1.3069 +					}
  1.3070 +				}
  1.3071 +			else
  1.3072 +#endif
  1.3073 +				i = -1;	/* Discarded digits make delta smaller. */
  1.3074 +			}
  1.3075 +#endif /*}*/
  1.3076 +#ifdef Honor_FLT_ROUNDS /*{*/
  1.3077 +		if (bc.rounding != 1) {
  1.3078 +			if (i < 0) {
  1.3079 +				/* Error is less than an ulp */
  1.3080 +				if (!delta->x[0] && delta->wds <= 1) {
  1.3081 +					/* exact */
  1.3082 +#ifdef SET_INEXACT
  1.3083 +					bc.inexact = 0;
  1.3084 +#endif
  1.3085 +					break;
  1.3086 +					}
  1.3087 +				if (bc.rounding) {
  1.3088 +					if (bc.dsign) {
  1.3089 +						adj.d = 1.;
  1.3090 +						goto apply_adj;
  1.3091 +						}
  1.3092 +					}
  1.3093 +				else if (!bc.dsign) {
  1.3094 +					adj.d = -1.;
  1.3095 +					if (!word1(&rv)
  1.3096 +					 && !(word0(&rv) & Frac_mask)) {
  1.3097 +						y = word0(&rv) & Exp_mask;
  1.3098 +#ifdef Avoid_Underflow
  1.3099 +						if (!bc.scale || y > 2*P*Exp_msk1)
  1.3100 +#else
  1.3101 +						if (y)
  1.3102 +#endif
  1.3103 +						  {
  1.3104 +						  delta = lshift(delta,Log2P);
  1.3105 +						  if (cmp(delta, bs) <= 0)
  1.3106 +							adj.d = -0.5;
  1.3107 +						  }
  1.3108 +						}
  1.3109 + apply_adj:
  1.3110 +#ifdef Avoid_Underflow /*{*/
  1.3111 +					if (bc.scale && (y = word0(&rv) & Exp_mask)
  1.3112 +						<= 2*P*Exp_msk1)
  1.3113 +					  word0(&adj) += (2*P+1)*Exp_msk1 - y;
  1.3114 +#else
  1.3115 +#ifdef Sudden_Underflow
  1.3116 +					if ((word0(&rv) & Exp_mask) <=
  1.3117 +							P*Exp_msk1) {
  1.3118 +						word0(&rv) += P*Exp_msk1;
  1.3119 +						dval(&rv) += adj.d*ulp(dval(&rv));
  1.3120 +						word0(&rv) -= P*Exp_msk1;
  1.3121 +						}
  1.3122 +					else
  1.3123 +#endif /*Sudden_Underflow*/
  1.3124 +#endif /*Avoid_Underflow}*/
  1.3125 +					dval(&rv) += adj.d*ulp(&rv);
  1.3126 +					}
  1.3127 +				break;
  1.3128 +				}
  1.3129 +			adj.d = ratio(delta, bs);
  1.3130 +			if (adj.d < 1.)
  1.3131 +				adj.d = 1.;
  1.3132 +			if (adj.d <= 0x7ffffffe) {
  1.3133 +				/* adj = rounding ? ceil(adj) : floor(adj); */
  1.3134 +				y = adj.d;
  1.3135 +				if (y != adj.d) {
  1.3136 +					if (!((bc.rounding>>1) ^ bc.dsign))
  1.3137 +						y++;
  1.3138 +					adj.d = y;
  1.3139 +					}
  1.3140 +				}
  1.3141 +#ifdef Avoid_Underflow /*{*/
  1.3142 +			if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
  1.3143 +				word0(&adj) += (2*P+1)*Exp_msk1 - y;
  1.3144 +#else
  1.3145 +#ifdef Sudden_Underflow
  1.3146 +			if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
  1.3147 +				word0(&rv) += P*Exp_msk1;
  1.3148 +				adj.d *= ulp(dval(&rv));
  1.3149 +				if (bc.dsign)
  1.3150 +					dval(&rv) += adj.d;
  1.3151 +				else
  1.3152 +					dval(&rv) -= adj.d;
  1.3153 +				word0(&rv) -= P*Exp_msk1;
  1.3154 +				goto cont;
  1.3155 +				}
  1.3156 +#endif /*Sudden_Underflow*/
  1.3157 +#endif /*Avoid_Underflow}*/
  1.3158 +			adj.d *= ulp(&rv);
  1.3159 +			if (bc.dsign) {
  1.3160 +				if (word0(&rv) == Big0 && word1(&rv) == Big1)
  1.3161 +					goto ovfl;
  1.3162 +				dval(&rv) += adj.d;
  1.3163 +				}
  1.3164 +			else
  1.3165 +				dval(&rv) -= adj.d;
  1.3166 +			goto cont;
  1.3167 +			}
  1.3168 +#endif /*}Honor_FLT_ROUNDS*/
  1.3169 +
  1.3170 +		if (i < 0) {
  1.3171 +			/* Error is less than half an ulp -- check for
  1.3172 +			 * special case of mantissa a power of two.
  1.3173 +			 */
  1.3174 +			if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
  1.3175 +#ifdef IEEE_Arith /*{*/
  1.3176 +#ifdef Avoid_Underflow
  1.3177 +			 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
  1.3178 +#else
  1.3179 +			 || (word0(&rv) & Exp_mask) <= Exp_msk1
  1.3180 +#endif
  1.3181 +#endif /*}*/
  1.3182 +				) {
  1.3183 +#ifdef SET_INEXACT
  1.3184 +				if (!delta->x[0] && delta->wds <= 1)
  1.3185 +					bc.inexact = 0;
  1.3186 +#endif
  1.3187 +				break;
  1.3188 +				}
  1.3189 +			if (!delta->x[0] && delta->wds <= 1) {
  1.3190 +				/* exact result */
  1.3191 +#ifdef SET_INEXACT
  1.3192 +				bc.inexact = 0;
  1.3193 +#endif
  1.3194 +				break;
  1.3195 +				}
  1.3196 +			delta = lshift(delta,Log2P);
  1.3197 +			if (cmp(delta, bs) > 0)
  1.3198 +				goto drop_down;
  1.3199 +			break;
  1.3200 +			}
  1.3201 +		if (i == 0) {
  1.3202 +			/* exactly half-way between */
  1.3203 +			if (bc.dsign) {
  1.3204 +				if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
  1.3205 +				 &&  word1(&rv) == (
  1.3206 +#ifdef Avoid_Underflow
  1.3207 +			(bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
  1.3208 +		? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
  1.3209 +#endif
  1.3210 +						   0xffffffff)) {
  1.3211 +					/*boundary case -- increment exponent*/
  1.3212 +					if (word0(&rv) == Big0 && word1(&rv) == Big1)
  1.3213 +						goto ovfl;
  1.3214 +					word0(&rv) = (word0(&rv) & Exp_mask)
  1.3215 +						+ Exp_msk1
  1.3216 +#ifdef IBM
  1.3217 +						| Exp_msk1 >> 4
  1.3218 +#endif
  1.3219 +						;
  1.3220 +					word1(&rv) = 0;
  1.3221 +#ifdef Avoid_Underflow
  1.3222 +					bc.dsign = 0;
  1.3223 +#endif
  1.3224 +					break;
  1.3225 +					}
  1.3226 +				}
  1.3227 +			else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
  1.3228 + drop_down:
  1.3229 +				/* boundary case -- decrement exponent */
  1.3230 +#ifdef Sudden_Underflow /*{{*/
  1.3231 +				L = word0(&rv) & Exp_mask;
  1.3232 +#ifdef IBM
  1.3233 +				if (L <  Exp_msk1)
  1.3234 +#else
  1.3235 +#ifdef Avoid_Underflow
  1.3236 +				if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
  1.3237 +#else
  1.3238 +				if (L <= Exp_msk1)
  1.3239 +#endif /*Avoid_Underflow*/
  1.3240 +#endif /*IBM*/
  1.3241 +					{
  1.3242 +					if (bc.nd >nd) {
  1.3243 +						bc.uflchk = 1;
  1.3244 +						break;
  1.3245 +						}
  1.3246 +					goto undfl;
  1.3247 +					}
  1.3248 +				L -= Exp_msk1;
  1.3249 +#else /*Sudden_Underflow}{*/
  1.3250 +#ifdef Avoid_Underflow
  1.3251 +				if (bc.scale) {
  1.3252 +					L = word0(&rv) & Exp_mask;
  1.3253 +					if (L <= (2*P+1)*Exp_msk1) {
  1.3254 +						if (L > (P+2)*Exp_msk1)
  1.3255 +							/* round even ==> */
  1.3256 +							/* accept rv */
  1.3257 +							break;
  1.3258 +						/* rv = smallest denormal */
  1.3259 +						if (bc.nd >nd) {
  1.3260 +							bc.uflchk = 1;
  1.3261 +							break;
  1.3262 +							}
  1.3263 +						goto undfl;
  1.3264 +						}
  1.3265 +					}
  1.3266 +#endif /*Avoid_Underflow*/
  1.3267 +				L = (word0(&rv) & Exp_mask) - Exp_msk1;
  1.3268 +#endif /*Sudden_Underflow}}*/
  1.3269 +				word0(&rv) = L | Bndry_mask1;
  1.3270 +				word1(&rv) = 0xffffffff;
  1.3271 +#ifdef IBM
  1.3272 +				goto cont;
  1.3273 +#else
  1.3274 +#ifndef NO_STRTOD_BIGCOMP
  1.3275 +				if (bc.nd > nd)
  1.3276 +					goto cont;
  1.3277 +#endif
  1.3278 +				break;
  1.3279 +#endif
  1.3280 +				}
  1.3281 +#ifndef ROUND_BIASED
  1.3282 +#ifdef Avoid_Underflow
  1.3283 +			if (Lsb1) {
  1.3284 +				if (!(word0(&rv) & Lsb1))
  1.3285 +					break;
  1.3286 +				}
  1.3287 +			else if (!(word1(&rv) & Lsb))
  1.3288 +				break;
  1.3289 +#else
  1.3290 +			if (!(word1(&rv) & LSB))
  1.3291 +				break;
  1.3292 +#endif
  1.3293 +#endif
  1.3294 +			if (bc.dsign)
  1.3295 +#ifdef Avoid_Underflow
  1.3296 +				dval(&rv) += sulp(&rv, &bc);
  1.3297 +#else
  1.3298 +				dval(&rv) += ulp(&rv);
  1.3299 +#endif
  1.3300 +#ifndef ROUND_BIASED
  1.3301 +			else {
  1.3302 +#ifdef Avoid_Underflow
  1.3303 +				dval(&rv) -= sulp(&rv, &bc);
  1.3304 +#else
  1.3305 +				dval(&rv) -= ulp(&rv);
  1.3306 +#endif
  1.3307 +#ifndef Sudden_Underflow
  1.3308 +				if (!dval(&rv)) {
  1.3309 +					if (bc.nd >nd) {
  1.3310 +						bc.uflchk = 1;
  1.3311 +						break;
  1.3312 +						}
  1.3313 +					goto undfl;
  1.3314 +					}
  1.3315 +#endif
  1.3316 +				}
  1.3317 +#ifdef Avoid_Underflow
  1.3318 +			bc.dsign = 1 - bc.dsign;
  1.3319 +#endif
  1.3320 +#endif
  1.3321 +			break;
  1.3322 +			}
  1.3323 +		if ((aadj = ratio(delta, bs)) <= 2.) {
  1.3324 +			if (bc.dsign)
  1.3325 +				aadj = aadj1 = 1.;
  1.3326 +			else if (word1(&rv) || word0(&rv) & Bndry_mask) {
  1.3327 +#ifndef Sudden_Underflow
  1.3328 +				if (word1(&rv) == Tiny1 && !word0(&rv)) {
  1.3329 +					if (bc.nd >nd) {
  1.3330 +						bc.uflchk = 1;
  1.3331 +						break;
  1.3332 +						}
  1.3333 +					goto undfl;
  1.3334 +					}
  1.3335 +#endif
  1.3336 +				aadj = 1.;
  1.3337 +				aadj1 = -1.;
  1.3338 +				}
  1.3339 +			else {
  1.3340 +				/* special case -- power of FLT_RADIX to be */
  1.3341 +				/* rounded down... */
  1.3342 +
  1.3343 +				if (aadj < 2./FLT_RADIX)
  1.3344 +					aadj = 1./FLT_RADIX;
  1.3345 +				else
  1.3346 +					aadj *= 0.5;
  1.3347 +				aadj1 = -aadj;
  1.3348 +				}
  1.3349 +			}
  1.3350 +		else {
  1.3351 +			aadj *= 0.5;
  1.3352 +			aadj1 = bc.dsign ? aadj : -aadj;
  1.3353 +#ifdef Check_FLT_ROUNDS
  1.3354 +			switch(bc.rounding) {
  1.3355 +				case 2: /* towards +infinity */
  1.3356 +					aadj1 -= 0.5;
  1.3357 +					break;
  1.3358 +				case 0: /* towards 0 */
  1.3359 +				case 3: /* towards -infinity */
  1.3360 +					aadj1 += 0.5;
  1.3361 +				}
  1.3362 +#else
  1.3363 +			if (Flt_Rounds == 0)
  1.3364 +				aadj1 += 0.5;
  1.3365 +#endif /*Check_FLT_ROUNDS*/
  1.3366 +			}
  1.3367 +		y = word0(&rv) & Exp_mask;
  1.3368 +
  1.3369 +		/* Check for overflow */
  1.3370 +
  1.3371 +		if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  1.3372 +			dval(&rv0) = dval(&rv);
  1.3373 +			word0(&rv) -= P*Exp_msk1;
  1.3374 +			adj.d = aadj1 * ulp(&rv);
  1.3375 +			dval(&rv) += adj.d;
  1.3376 +			if ((word0(&rv) & Exp_mask) >=
  1.3377 +					Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  1.3378 +				if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
  1.3379 +					goto ovfl;
  1.3380 +				word0(&rv) = Big0;
  1.3381 +				word1(&rv) = Big1;
  1.3382 +				goto cont;
  1.3383 +				}
  1.3384 +			else
  1.3385 +				word0(&rv) += P*Exp_msk1;
  1.3386 +			}
  1.3387 +		else {
  1.3388 +#ifdef Avoid_Underflow
  1.3389 +			if (bc.scale && y <= 2*P*Exp_msk1) {
  1.3390 +				if (aadj <= 0x7fffffff) {
  1.3391 +					if ((z = aadj) <= 0)
  1.3392 +						z = 1;
  1.3393 +					aadj = z;
  1.3394 +					aadj1 = bc.dsign ? aadj : -aadj;
  1.3395 +					}
  1.3396 +				dval(&aadj2) = aadj1;
  1.3397 +				word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
  1.3398 +				aadj1 = dval(&aadj2);
  1.3399 +				adj.d = aadj1 * ulp(&rv);
  1.3400 +				dval(&rv) += adj.d;
  1.3401 +				if (rv.d == 0.)
  1.3402 +#ifdef NO_STRTOD_BIGCOMP
  1.3403 +					goto undfl;
  1.3404 +#else
  1.3405 +					{
  1.3406 +					if (bc.nd > nd)
  1.3407 +						bc.dsign = 1;
  1.3408 +					break;
  1.3409 +					}
  1.3410 +#endif
  1.3411 +				}
  1.3412 +			else {
  1.3413 +				adj.d = aadj1 * ulp(&rv);
  1.3414 +				dval(&rv) += adj.d;
  1.3415 +				}
  1.3416 +#else
  1.3417 +#ifdef Sudden_Underflow
  1.3418 +			if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
  1.3419 +				dval(&rv0) = dval(&rv);
  1.3420 +				word0(&rv) += P*Exp_msk1;
  1.3421 +				adj.d = aadj1 * ulp(&rv);
  1.3422 +				dval(&rv) += adj.d;
  1.3423 +#ifdef IBM
  1.3424 +				if ((word0(&rv) & Exp_mask) <  P*Exp_msk1)
  1.3425 +#else
  1.3426 +				if ((word0(&rv) & Exp_mask) <= P*Exp_msk1)
  1.3427 +#endif
  1.3428 +					{
  1.3429 +					if (word0(&rv0) == Tiny0
  1.3430 +					 && word1(&rv0) == Tiny1) {
  1.3431 +						if (bc.nd >nd) {
  1.3432 +							bc.uflchk = 1;
  1.3433 +							break;
  1.3434 +							}
  1.3435 +						goto undfl;
  1.3436 +						}
  1.3437 +					word0(&rv) = Tiny0;
  1.3438 +					word1(&rv) = Tiny1;
  1.3439 +					goto cont;
  1.3440 +					}
  1.3441 +				else
  1.3442 +					word0(&rv) -= P*Exp_msk1;
  1.3443 +				}
  1.3444 +			else {
  1.3445 +				adj.d = aadj1 * ulp(&rv);
  1.3446 +				dval(&rv) += adj.d;
  1.3447 +				}
  1.3448 +#else /*Sudden_Underflow*/
  1.3449 +			/* Compute adj so that the IEEE rounding rules will
  1.3450 +			 * correctly round rv + adj in some half-way cases.
  1.3451 +			 * If rv * ulp(rv) is denormalized (i.e.,
  1.3452 +			 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
  1.3453 +			 * trouble from bits lost to denormalization;
  1.3454 +			 * example: 1.2e-307 .
  1.3455 +			 */
  1.3456 +			if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
  1.3457 +				aadj1 = (double)(int)(aadj + 0.5);
  1.3458 +				if (!bc.dsign)
  1.3459 +					aadj1 = -aadj1;
  1.3460 +				}
  1.3461 +			adj.d = aadj1 * ulp(&rv);
  1.3462 +			dval(&rv) += adj.d;
  1.3463 +#endif /*Sudden_Underflow*/
  1.3464 +#endif /*Avoid_Underflow*/
  1.3465 +			}
  1.3466 +		z = word0(&rv) & Exp_mask;
  1.3467 +#ifndef SET_INEXACT
  1.3468 +		if (bc.nd == nd) {
  1.3469 +#ifdef Avoid_Underflow
  1.3470 +		if (!bc.scale)
  1.3471 +#endif
  1.3472 +		if (y == z) {
  1.3473 +			/* Can we stop now? */
  1.3474 +			L = (Long)aadj;
  1.3475 +			aadj -= L;
  1.3476 +			/* The tolerances below are conservative. */
  1.3477 +			if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
  1.3478 +				if (aadj < .4999999 || aadj > .5000001)
  1.3479 +					break;
  1.3480 +				}
  1.3481 +			else if (aadj < .4999999/FLT_RADIX)
  1.3482 +				break;
  1.3483 +			}
  1.3484 +		}
  1.3485 +#endif
  1.3486 + cont:
  1.3487 +		Bfree(bb);
  1.3488 +		Bfree(bd);
  1.3489 +		Bfree(bs);
  1.3490 +		Bfree(delta);
  1.3491 +		}
  1.3492 +	Bfree(bb);
  1.3493 +	Bfree(bd);
  1.3494 +	Bfree(bs);
  1.3495 +	Bfree(bd0);
  1.3496 +	Bfree(delta);
  1.3497 +#ifndef NO_STRTOD_BIGCOMP
  1.3498 +	if (req_bigcomp) {
  1.3499 +		bd0 = 0;
  1.3500 +		bc.e0 += nz1;
  1.3501 +		bigcomp(&rv, s0, &bc);
  1.3502 +		y = word0(&rv) & Exp_mask;
  1.3503 +		if (y == Exp_mask)
  1.3504 +			goto ovfl;
  1.3505 +		if (y == 0 && rv.d == 0.)
  1.3506 +			goto undfl;
  1.3507 +		}
  1.3508 +#endif
  1.3509 +#ifdef SET_INEXACT
  1.3510 +	if (bc.inexact) {
  1.3511 +		if (!oldinexact) {
  1.3512 +			word0(&rv0) = Exp_1 + (70 << Exp_shift);
  1.3513 +			word1(&rv0) = 0;
  1.3514 +			dval(&rv0) += 1.;
  1.3515 +			}
  1.3516 +		}
  1.3517 +	else if (!oldinexact)
  1.3518 +		clear_inexact();
  1.3519 +#endif
  1.3520 +#ifdef Avoid_Underflow
  1.3521 +	if (bc.scale) {
  1.3522 +		word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
  1.3523 +		word1(&rv0) = 0;
  1.3524 +		dval(&rv) *= dval(&rv0);
  1.3525 +#ifndef NO_ERRNO
  1.3526 +		/* try to avoid the bug of testing an 8087 register value */
  1.3527 +#ifdef IEEE_Arith
  1.3528 +		if (!(word0(&rv) & Exp_mask))
  1.3529 +#else
  1.3530 +		if (word0(&rv) == 0 && word1(&rv) == 0)
  1.3531 +#endif
  1.3532 +			errno = ERANGE;
  1.3533 +#endif
  1.3534 +		}
  1.3535 +#endif /* Avoid_Underflow */
  1.3536 +#ifdef SET_INEXACT
  1.3537 +	if (bc.inexact && !(word0(&rv) & Exp_mask)) {
  1.3538 +		/* set underflow bit */
  1.3539 +		dval(&rv0) = 1e-300;
  1.3540 +		dval(&rv0) *= dval(&rv0);
  1.3541 +		}
  1.3542 +#endif
  1.3543 + ret:
  1.3544 +	if (se)
  1.3545 +		*se = (char *)s;
  1.3546 +	return sign ? -dval(&rv) : dval(&rv);
  1.3547 +	}
  1.3548 +
  1.3549 +#ifndef MULTIPLE_THREADS
  1.3550 + static char *dtoa_result;
  1.3551 +#endif
  1.3552 +
  1.3553 + static char *
  1.3554 +#ifdef KR_headers
  1.3555 +rv_alloc(i) int i;
  1.3556 +#else
  1.3557 +rv_alloc(int i)
  1.3558 +#endif
  1.3559 +{
  1.3560 +	int j, k, *r;
  1.3561 +
  1.3562 +	j = sizeof(ULong);
  1.3563 +	for(k = 0;
  1.3564 +		sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
  1.3565 +		j <<= 1)
  1.3566 +			k++;
  1.3567 +	r = (int*)Balloc(k);
  1.3568 +	*r = k;
  1.3569 +	return
  1.3570 +#ifndef MULTIPLE_THREADS
  1.3571 +	dtoa_result =
  1.3572 +#endif
  1.3573 +		(char *)(r+1);
  1.3574 +	}
  1.3575 +
  1.3576 + static char *
  1.3577 +#ifdef KR_headers
  1.3578 +nrv_alloc(s, rve, n) char *s, **rve; int n;
  1.3579 +#else
  1.3580 +nrv_alloc(const char *s, char **rve, int n)
  1.3581 +#endif
  1.3582 +{
  1.3583 +	char *rv, *t;
  1.3584 +
  1.3585 +	t = rv = rv_alloc(n);
  1.3586 +	while((*t = *s++)) t++;
  1.3587 +	if (rve)
  1.3588 +		*rve = t;
  1.3589 +	return rv;
  1.3590 +	}
  1.3591 +
  1.3592 +/* freedtoa(s) must be used to free values s returned by dtoa
  1.3593 + * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
  1.3594 + * but for consistency with earlier versions of dtoa, it is optional
  1.3595 + * when MULTIPLE_THREADS is not defined.
  1.3596 + */
  1.3597 +
  1.3598 + void
  1.3599 +#ifdef KR_headers
  1.3600 +freedtoa(s) char *s;
  1.3601 +#else
  1.3602 +freedtoa(char *s)
  1.3603 +#endif
  1.3604 +{
  1.3605 +	Bigint *b = (Bigint *)((int *)s - 1);
  1.3606 +	b->maxwds = 1 << (b->k = *(int*)b);
  1.3607 +	Bfree(b);
  1.3608 +#ifndef MULTIPLE_THREADS
  1.3609 +	if (s == dtoa_result)
  1.3610 +		dtoa_result = 0;
  1.3611 +#endif
  1.3612 +	}
  1.3613 +
  1.3614 +/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  1.3615 + *
  1.3616 + * Inspired by "How to Print Floating-Point Numbers Accurately" by
  1.3617 + * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
  1.3618 + *
  1.3619 + * Modifications:
  1.3620 + *	1. Rather than iterating, we use a simple numeric overestimate
  1.3621 + *	   to determine k = floor(log10(d)).  We scale relevant
  1.3622 + *	   quantities using O(log2(k)) rather than O(k) multiplications.
  1.3623 + *	2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  1.3624 + *	   try to generate digits strictly left to right.  Instead, we
  1.3625 + *	   compute with fewer bits and propagate the carry if necessary
  1.3626 + *	   when rounding the final digit up.  This is often faster.
  1.3627 + *	3. Under the assumption that input will be rounded nearest,
  1.3628 + *	   mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  1.3629 + *	   That is, we allow equality in stopping tests when the
  1.3630 + *	   round-nearest rule will give the same floating-point value
  1.3631 + *	   as would satisfaction of the stopping test with strict
  1.3632 + *	   inequality.
  1.3633 + *	4. We remove common factors of powers of 2 from relevant
  1.3634 + *	   quantities.
  1.3635 + *	5. When converting floating-point integers less than 1e16,
  1.3636 + *	   we use floating-point arithmetic rather than resorting
  1.3637 + *	   to multiple-precision integers.
  1.3638 + *	6. When asked to produce fewer than 15 digits, we first try
  1.3639 + *	   to get by with floating-point arithmetic; we resort to
  1.3640 + *	   multiple-precision integer arithmetic only if we cannot
  1.3641 + *	   guarantee that the floating-point calculation has given
  1.3642 + *	   the correctly rounded result.  For k requested digits and
  1.3643 + *	   "uniformly" distributed input, the probability is
  1.3644 + *	   something like 10^(k-15) that we must resort to the Long
  1.3645 + *	   calculation.
  1.3646 + */
  1.3647 +
  1.3648 + char *
  1.3649 +dtoa
  1.3650 +#ifdef KR_headers
  1.3651 +	(dd, mode, ndigits, decpt, sign, rve)
  1.3652 +	double dd; int mode, ndigits, *decpt, *sign; char **rve;
  1.3653 +#else
  1.3654 +	(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
  1.3655 +#endif
  1.3656 +{
  1.3657 + /*	Arguments ndigits, decpt, sign are similar to those
  1.3658 +	of ecvt and fcvt; trailing zeros are suppressed from
  1.3659 +	the returned string.  If not null, *rve is set to point
  1.3660 +	to the end of the return value.  If d is +-Infinity or NaN,
  1.3661 +	then *decpt is set to 9999.
  1.3662 +
  1.3663 +	mode:
  1.3664 +		0 ==> shortest string that yields d when read in
  1.3665 +			and rounded to nearest.
  1.3666 +		1 ==> like 0, but with Steele & White stopping rule;
  1.3667 +			e.g. with IEEE P754 arithmetic , mode 0 gives
  1.3668 +			1e23 whereas mode 1 gives 9.999999999999999e22.
  1.3669 +		2 ==> max(1,ndigits) significant digits.  This gives a
  1.3670 +			return value similar to that of ecvt, except
  1.3671 +			that trailing zeros are suppressed.
  1.3672 +		3 ==> through ndigits past the decimal point.  This
  1.3673 +			gives a return value similar to that from fcvt,
  1.3674 +			except that trailing zeros are suppressed, and
  1.3675 +			ndigits can be negative.
  1.3676 +		4,5 ==> similar to 2 and 3, respectively, but (in
  1.3677 +			round-nearest mode) with the tests of mode 0 to
  1.3678 +			possibly return a shorter string that rounds to d.
  1.3679 +			With IEEE arithmetic and compilation with
  1.3680 +			-DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
  1.3681 +			as modes 2 and 3 when FLT_ROUNDS != 1.
  1.3682 +		6-9 ==> Debugging modes similar to mode - 4:  don't try
  1.3683 +			fast floating-point estimate (if applicable).
  1.3684 +
  1.3685 +		Values of mode other than 0-9 are treated as mode 0.
  1.3686 +
  1.3687 +		Sufficient space is allocated to the return value
  1.3688 +		to hold the suppressed trailing zeros.
  1.3689 +	*/
  1.3690 +
  1.3691 +	int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  1.3692 +		j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  1.3693 +		spec_case, try_quick;
  1.3694 +	Long L;
  1.3695 +#ifndef Sudden_Underflow
  1.3696 +	int denorm;
  1.3697 +	ULong x;
  1.3698 +#endif
  1.3699 +	Bigint *b, *b1, *delta, *mlo, *mhi, *S;
  1.3700 +	U d2, eps, u;
  1.3701 +	double ds;
  1.3702 +	char *s, *s0;
  1.3703 +#ifndef No_leftright
  1.3704 +#ifdef IEEE_Arith
  1.3705 +	U eps1;
  1.3706 +#endif
  1.3707 +#endif
  1.3708 +#ifdef SET_INEXACT
  1.3709 +	int inexact, oldinexact;
  1.3710 +#endif
  1.3711 +#ifdef Honor_FLT_ROUNDS /*{*/
  1.3712 +	int Rounding;
  1.3713 +#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
  1.3714 +	Rounding = Flt_Rounds;
  1.3715 +#else /*}{*/
  1.3716 +	Rounding = 1;
  1.3717 +	switch(fegetround()) {
  1.3718 +	  case FE_TOWARDZERO:	Rounding = 0; break;
  1.3719 +	  case FE_UPWARD:	Rounding = 2; break;
  1.3720 +	  case FE_DOWNWARD:	Rounding = 3;
  1.3721 +	  }
  1.3722 +#endif /*}}*/
  1.3723 +#endif /*}*/
  1.3724 +
  1.3725 +#ifndef MULTIPLE_THREADS
  1.3726 +	if (dtoa_result) {
  1.3727 +		freedtoa(dtoa_result);
  1.3728 +		dtoa_result = 0;
  1.3729 +		}
  1.3730 +#endif
  1.3731 +
  1.3732 +	u.d = dd;
  1.3733 +	if (word0(&u) & Sign_bit) {
  1.3734 +		/* set sign for everything, including 0's and NaNs */
  1.3735 +		*sign = 1;
  1.3736 +		word0(&u) &= ~Sign_bit;	/* clear sign bit */
  1.3737 +		}
  1.3738 +	else
  1.3739 +		*sign = 0;
  1.3740 +
  1.3741 +#if defined(IEEE_Arith) + defined(VAX)
  1.3742 +#ifdef IEEE_Arith
  1.3743 +	if ((word0(&u) & Exp_mask) == Exp_mask)
  1.3744 +#else
  1.3745 +	if (word0(&u)  == 0x8000)
  1.3746 +#endif
  1.3747 +		{
  1.3748 +		/* Infinity or NaN */
  1.3749 +		*decpt = 9999;
  1.3750 +#ifdef IEEE_Arith
  1.3751 +		if (!word1(&u) && !(word0(&u) & 0xfffff))
  1.3752 +			return nrv_alloc("Infinity", rve, 8);
  1.3753 +#endif
  1.3754 +		return nrv_alloc("NaN", rve, 3);
  1.3755 +		}
  1.3756 +#endif
  1.3757 +#ifdef IBM
  1.3758 +	dval(&u) += 0; /* normalize */
  1.3759 +#endif
  1.3760 +	if (!dval(&u)) {
  1.3761 +		*decpt = 1;
  1.3762 +		return nrv_alloc("0", rve, 1);
  1.3763 +		}
  1.3764 +
  1.3765 +#ifdef SET_INEXACT
  1.3766 +	try_quick = oldinexact = get_inexact();
  1.3767 +	inexact = 1;
  1.3768 +#endif
  1.3769 +#ifdef Honor_FLT_ROUNDS
  1.3770 +	if (Rounding >= 2) {
  1.3771 +		if (*sign)
  1.3772 +			Rounding = Rounding == 2 ? 0 : 2;
  1.3773 +		else
  1.3774 +			if (Rounding != 2)
  1.3775 +				Rounding = 0;
  1.3776 +		}
  1.3777 +#endif
  1.3778 +
  1.3779 +	b = d2b(&u, &be, &bbits);
  1.3780 +#ifdef Sudden_Underflow
  1.3781 +	i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  1.3782 +#else
  1.3783 +	if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
  1.3784 +#endif
  1.3785 +		dval(&d2) = dval(&u);
  1.3786 +		word0(&d2) &= Frac_mask1;
  1.3787 +		word0(&d2) |= Exp_11;
  1.3788 +#ifdef IBM
  1.3789 +		if (j = 11 - hi0bits(word0(&d2) & Frac_mask))
  1.3790 +			dval(&d2) /= 1 << j;
  1.3791 +#endif
  1.3792 +
  1.3793 +		/* log(x)	~=~ log(1.5) + (x-1.5)/1.5
  1.3794 +		 * log10(x)	 =  log(x) / log(10)
  1.3795 +		 *		~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  1.3796 +		 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  1.3797 +		 *
  1.3798 +		 * This suggests computing an approximation k to log10(d) by
  1.3799 +		 *
  1.3800 +		 * k = (i - Bias)*0.301029995663981
  1.3801 +		 *	+ ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  1.3802 +		 *
  1.3803 +		 * We want k to be too large rather than too small.
  1.3804 +		 * The error in the first-order Taylor series approximation
  1.3805 +		 * is in our favor, so we just round up the constant enough
  1.3806 +		 * to compensate for any error in the multiplication of
  1.3807 +		 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  1.3808 +		 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  1.3809 +		 * adding 1e-13 to the constant term more than suffices.
  1.3810 +		 * Hence we adjust the constant term to 0.1760912590558.
  1.3811 +		 * (We could get a more accurate k by invoking log10,
  1.3812 +		 *  but this is probably not worthwhile.)
  1.3813 +		 */
  1.3814 +
  1.3815 +		i -= Bias;
  1.3816 +#ifdef IBM
  1.3817 +		i <<= 2;
  1.3818 +		i += j;
  1.3819 +#endif
  1.3820 +#ifndef Sudden_Underflow
  1.3821 +		denorm = 0;
  1.3822 +		}
  1.3823 +	else {
  1.3824 +		/* d is denormalized */
  1.3825 +
  1.3826 +		i = bbits + be + (Bias + (P-1) - 1);
  1.3827 +		x = i > 32  ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
  1.3828 +			    : word1(&u) << (32 - i);
  1.3829 +		dval(&d2) = x;
  1.3830 +		word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
  1.3831 +		i -= (Bias + (P-1) - 1) + 1;
  1.3832 +		denorm = 1;
  1.3833 +		}
  1.3834 +#endif
  1.3835 +	ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  1.3836 +	k = (int)ds;
  1.3837 +	if (ds < 0. && ds != k)
  1.3838 +		k--;	/* want k = floor(ds) */
  1.3839 +	k_check = 1;
  1.3840 +	if (k >= 0 && k <= Ten_pmax) {
  1.3841 +		if (dval(&u) < tens[k])
  1.3842 +			k--;
  1.3843 +		k_check = 0;
  1.3844 +		}
  1.3845 +	j = bbits - i - 1;
  1.3846 +	if (j >= 0) {
  1.3847 +		b2 = 0;
  1.3848 +		s2 = j;
  1.3849 +		}
  1.3850 +	else {
  1.3851 +		b2 = -j;
  1.3852 +		s2 = 0;
  1.3853 +		}
  1.3854 +	if (k >= 0) {
  1.3855 +		b5 = 0;
  1.3856 +		s5 = k;
  1.3857 +		s2 += k;
  1.3858 +		}
  1.3859 +	else {
  1.3860 +		b2 -= k;
  1.3861 +		b5 = -k;
  1.3862 +		s5 = 0;
  1.3863 +		}
  1.3864 +	if (mode < 0 || mode > 9)
  1.3865 +		mode = 0;
  1.3866 +
  1.3867 +#ifndef SET_INEXACT
  1.3868 +#ifdef Check_FLT_ROUNDS
  1.3869 +	try_quick = Rounding == 1;
  1.3870 +#else
  1.3871 +	try_quick = 1;
  1.3872 +#endif
  1.3873 +#endif /*SET_INEXACT*/
  1.3874 +
  1.3875 +	if (mode > 5) {
  1.3876 +		mode -= 4;
  1.3877 +		try_quick = 0;
  1.3878 +		}
  1.3879 +	leftright = 1;
  1.3880 +	ilim = ilim1 = -1;	/* Values for cases 0 and 1; done here to */
  1.3881 +				/* silence erroneous "gcc -Wall" warning. */
  1.3882 +	switch(mode) {
  1.3883 +		case 0:
  1.3884 +		case 1:
  1.3885 +			i = 18;
  1.3886 +			ndigits = 0;
  1.3887 +			break;
  1.3888 +		case 2:
  1.3889 +			leftright = 0;
  1.3890 +			/* no break */
  1.3891 +		case 4:
  1.3892 +			if (ndigits <= 0)
  1.3893 +				ndigits = 1;
  1.3894 +			ilim = ilim1 = i = ndigits;
  1.3895 +			break;
  1.3896 +		case 3:
  1.3897 +			leftright = 0;
  1.3898 +			/* no break */
  1.3899 +		case 5:
  1.3900 +			i = ndigits + k + 1;
  1.3901 +			ilim = i;
  1.3902 +			ilim1 = i - 1;
  1.3903 +			if (i <= 0)
  1.3904 +				i = 1;
  1.3905 +		}
  1.3906 +	s = s0 = rv_alloc(i);
  1.3907 +
  1.3908 +#ifdef Honor_FLT_ROUNDS
  1.3909 +	if (mode > 1 && Rounding != 1)
  1.3910 +		leftright = 0;
  1.3911 +#endif
  1.3912 +
  1.3913 +	if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  1.3914 +
  1.3915 +		/* Try to get by with floating-point arithmetic. */
  1.3916 +
  1.3917 +		i = 0;
  1.3918 +		dval(&d2) = dval(&u);
  1.3919 +		k0 = k;
  1.3920 +		ilim0 = ilim;
  1.3921 +		ieps = 2; /* conservative */
  1.3922 +		if (k > 0) {
  1.3923 +			ds = tens[k&0xf];
  1.3924 +			j = k >> 4;
  1.3925 +			if (j & Bletch) {
  1.3926 +				/* prevent overflows */
  1.3927 +				j &= Bletch - 1;
  1.3928 +				dval(&u) /= bigtens[n_bigtens-1];
  1.3929 +				ieps++;
  1.3930 +				}
  1.3931 +			for(; j; j >>= 1, i++)
  1.3932 +				if (j & 1) {
  1.3933 +					ieps++;
  1.3934 +					ds *= bigtens[i];
  1.3935 +					}
  1.3936 +			dval(&u) /= ds;
  1.3937 +			}
  1.3938 +		else if ((j1 = -k)) {
  1.3939 +			dval(&u) *= tens[j1 & 0xf];
  1.3940 +			for(j = j1 >> 4; j; j >>= 1, i++)
  1.3941 +				if (j & 1) {
  1.3942 +					ieps++;
  1.3943 +					dval(&u) *= bigtens[i];
  1.3944 +					}
  1.3945 +			}
  1.3946 +		if (k_check && dval(&u) < 1. && ilim > 0) {
  1.3947 +			if (ilim1 <= 0)
  1.3948 +				goto fast_failed;
  1.3949 +			ilim = ilim1;
  1.3950 +			k--;
  1.3951 +			dval(&u) *= 10.;
  1.3952 +			ieps++;
  1.3953 +			}
  1.3954 +		dval(&eps) = ieps*dval(&u) + 7.;
  1.3955 +		word0(&eps) -= (P-1)*Exp_msk1;
  1.3956 +		if (ilim == 0) {
  1.3957 +			S = mhi = 0;
  1.3958 +			dval(&u) -= 5.;
  1.3959 +			if (dval(&u) > dval(&eps))
  1.3960 +				goto one_digit;
  1.3961 +			if (dval(&u) < -dval(&eps))
  1.3962 +				goto no_digits;
  1.3963 +			goto fast_failed;
  1.3964 +			}
  1.3965 +#ifndef No_leftright
  1.3966 +		if (leftright) {
  1.3967 +			/* Use Steele & White method of only
  1.3968 +			 * generating digits needed.
  1.3969 +			 */
  1.3970 +			dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
  1.3971 +#ifdef IEEE_Arith
  1.3972 +			if (k0 < 0 && j1 >= 307) {
  1.3973 +				eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
  1.3974 +				word0(&eps1) -= Exp_msk1 * (Bias+P-1);
  1.3975 +				dval(&eps1) *= tens[j1 & 0xf];
  1.3976 +				for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
  1.3977 +					if (j & 1)
  1.3978 +						dval(&eps1) *= bigtens[i];
  1.3979 +				if (eps.d < eps1.d)
  1.3980 +					eps.d = eps1.d;
  1.3981 +				}
  1.3982 +#endif
  1.3983 +			for(i = 0;;) {
  1.3984 +				L = dval(&u);
  1.3985 +				dval(&u) -= L;
  1.3986 +				*s++ = '0' + (int)L;
  1.3987 +				if (1. - dval(&u) < dval(&eps))
  1.3988 +					goto bump_up;
  1.3989 +				if (dval(&u) < dval(&eps))
  1.3990 +					goto ret1;
  1.3991 +				if (++i >= ilim)
  1.3992 +					break;
  1.3993 +				dval(&eps) *= 10.;
  1.3994 +				dval(&u) *= 10.;
  1.3995 +				}
  1.3996 +			}
  1.3997 +		else {
  1.3998 +#endif
  1.3999 +			/* Generate ilim digits, then fix them up. */
  1.4000 +			dval(&eps) *= tens[ilim-1];
  1.4001 +			for(i = 1;; i++, dval(&u) *= 10.) {
  1.4002 +				L = (Long)(dval(&u));
  1.4003 +				if (!(dval(&u) -= L))
  1.4004 +					ilim = i;
  1.4005 +				*s++ = '0' + (int)L;
  1.4006 +				if (i == ilim) {
  1.4007 +					if (dval(&u) > 0.5 + dval(&eps))
  1.4008 +						goto bump_up;
  1.4009 +					else if (dval(&u) < 0.5 - dval(&eps)) {
  1.4010 +						while(*--s == '0');
  1.4011 +						s++;
  1.4012 +						goto ret1;
  1.4013 +						}
  1.4014 +					break;
  1.4015 +					}
  1.4016 +				}
  1.4017 +#ifndef No_leftright
  1.4018 +			}
  1.4019 +#endif
  1.4020 + fast_failed:
  1.4021 +		s = s0;
  1.4022 +		dval(&u) = dval(&d2);
  1.4023 +		k = k0;
  1.4024 +		ilim = ilim0;
  1.4025 +		}
  1.4026 +
  1.4027 +	/* Do we have a "small" integer? */
  1.4028 +
  1.4029 +	if (be >= 0 && k <= Int_max) {
  1.4030 +		/* Yes. */
  1.4031 +		ds = tens[k];
  1.4032 +		if (ndigits < 0 && ilim <= 0) {
  1.4033 +			S = mhi = 0;
  1.4034 +			if (ilim < 0 || dval(&u) <= 5*ds)
  1.4035 +				goto no_digits;
  1.4036 +			goto one_digit;
  1.4037 +			}
  1.4038 +		for(i = 1;; i++, dval(&u) *= 10.) {
  1.4039 +			L = (Long)(dval(&u) / ds);
  1.4040 +			dval(&u) -= L*ds;
  1.4041 +#ifdef Check_FLT_ROUNDS
  1.4042 +			/* If FLT_ROUNDS == 2, L will usually be high by 1 */
  1.4043 +			if (dval(&u) < 0) {
  1.4044 +				L--;
  1.4045 +				dval(&u) += ds;
  1.4046 +				}
  1.4047 +#endif
  1.4048 +			*s++ = '0' + (int)L;
  1.4049 +			if (!dval(&u)) {
  1.4050 +#ifdef SET_INEXACT
  1.4051 +				inexact = 0;
  1.4052 +#endif
  1.4053 +				break;
  1.4054 +				}
  1.4055 +			if (i == ilim) {
  1.4056 +#ifdef Honor_FLT_ROUNDS
  1.4057 +				if (mode > 1)
  1.4058 +				switch(Rounding) {
  1.4059 +				  case 0: goto ret1;
  1.4060 +				  case 2: goto bump_up;
  1.4061 +				  }
  1.4062 +#endif
  1.4063 +				dval(&u) += dval(&u);
  1.4064 +#ifdef ROUND_BIASED
  1.4065 +				if (dval(&u) >= ds)
  1.4066 +#else
  1.4067 +				if (dval(&u) > ds || (dval(&u) == ds && L & 1))
  1.4068 +#endif
  1.4069 +					{
  1.4070 + bump_up:
  1.4071 +					while(*--s == '9')
  1.4072 +						if (s == s0) {
  1.4073 +							k++;
  1.4074 +							*s = '0';
  1.4075 +							break;
  1.4076 +							}
  1.4077 +					++*s++;
  1.4078 +					}
  1.4079 +				break;
  1.4080 +				}
  1.4081 +			}
  1.4082 +		goto ret1;
  1.4083 +		}
  1.4084 +
  1.4085 +	m2 = b2;
  1.4086 +	m5 = b5;
  1.4087 +	mhi = mlo = 0;
  1.4088 +	if (leftright) {
  1.4089 +		i =
  1.4090 +#ifndef Sudden_Underflow
  1.4091 +			denorm ? be + (Bias + (P-1) - 1 + 1) :
  1.4092 +#endif
  1.4093 +#ifdef IBM
  1.4094 +			1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  1.4095 +#else
  1.4096 +			1 + P - bbits;
  1.4097 +#endif
  1.4098 +		b2 += i;
  1.4099 +		s2 += i;
  1.4100 +		mhi = i2b(1);
  1.4101 +		}
  1.4102 +	if (m2 > 0 && s2 > 0) {
  1.4103 +		i = m2 < s2 ? m2 : s2;
  1.4104 +		b2 -= i;
  1.4105 +		m2 -= i;
  1.4106 +		s2 -= i;
  1.4107 +		}
  1.4108 +	if (b5 > 0) {
  1.4109 +		if (leftright) {
  1.4110 +			if (m5 > 0) {
  1.4111 +				mhi = pow5mult(mhi, m5);
  1.4112 +				b1 = mult(mhi, b);
  1.4113 +				Bfree(b);
  1.4114 +				b = b1;
  1.4115 +				}
  1.4116 +			if ((j = b5 - m5))
  1.4117 +				b = pow5mult(b, j);
  1.4118 +			}
  1.4119 +		else
  1.4120 +			b = pow5mult(b, b5);
  1.4121 +		}
  1.4122 +	S = i2b(1);
  1.4123 +	if (s5 > 0)
  1.4124 +		S = pow5mult(S, s5);
  1.4125 +
  1.4126 +	/* Check for special case that d is a normalized power of 2. */
  1.4127 +
  1.4128 +	spec_case = 0;
  1.4129 +	if ((mode < 2 || leftright)
  1.4130 +#ifdef Honor_FLT_ROUNDS
  1.4131 +			&& Rounding == 1
  1.4132 +#endif
  1.4133 +				) {
  1.4134 +		if (!word1(&u) && !(word0(&u) & Bndry_mask)
  1.4135 +#ifndef Sudden_Underflow
  1.4136 +		 && word0(&u) & (Exp_mask & ~Exp_msk1)
  1.4137 +#endif
  1.4138 +				) {
  1.4139 +			/* The special case */
  1.4140 +			b2 += Log2P;
  1.4141 +			s2 += Log2P;
  1.4142 +			spec_case = 1;
  1.4143 +			}
  1.4144 +		}
  1.4145 +
  1.4146 +	/* Arrange for convenient computation of quotients:
  1.4147 +	 * shift left if necessary so divisor has 4 leading 0 bits.
  1.4148 +	 *
  1.4149 +	 * Perhaps we should just compute leading 28 bits of S once
  1.4150 +	 * and for all and pass them and a shift to quorem, so it
  1.4151 +	 * can do shifts and ors to compute the numerator for q.
  1.4152 +	 */
  1.4153 +	i = dshift(S, s2);
  1.4154 +	b2 += i;
  1.4155 +	m2 += i;
  1.4156 +	s2 += i;
  1.4157 +	if (b2 > 0)
  1.4158 +		b = lshift(b, b2);
  1.4159 +	if (s2 > 0)
  1.4160 +		S = lshift(S, s2);
  1.4161 +	if (k_check) {
  1.4162 +		if (cmp(b,S) < 0) {
  1.4163 +			k--;
  1.4164 +			b = multadd(b, 10, 0);	/* we botched the k estimate */
  1.4165 +			if (leftright)
  1.4166 +				mhi = multadd(mhi, 10, 0);
  1.4167 +			ilim = ilim1;
  1.4168 +			}
  1.4169 +		}
  1.4170 +	if (ilim <= 0 && (mode == 3 || mode == 5)) {
  1.4171 +		if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  1.4172 +			/* no digits, fcvt style */
  1.4173 + no_digits:
  1.4174 +			k = -1 - ndigits;
  1.4175 +			goto ret;
  1.4176 +			}
  1.4177 + one_digit:
  1.4178 +		*s++ = '1';
  1.4179 +		k++;
  1.4180 +		goto ret;
  1.4181 +		}
  1.4182 +	if (leftright) {
  1.4183 +		if (m2 > 0)
  1.4184 +			mhi = lshift(mhi, m2);
  1.4185 +
  1.4186 +		/* Compute mlo -- check for special case
  1.4187 +		 * that d is a normalized power of 2.
  1.4188 +		 */
  1.4189 +
  1.4190 +		mlo = mhi;
  1.4191 +		if (spec_case) {
  1.4192 +			mhi = Balloc(mhi->k);
  1.4193 +			Bcopy(mhi, mlo);
  1.4194 +			mhi = lshift(mhi, Log2P);
  1.4195 +			}
  1.4196 +
  1.4197 +		for(i = 1;;i++) {
  1.4198 +			dig = quorem(b,S) + '0';
  1.4199 +			/* Do we yet have the shortest decimal string
  1.4200 +			 * that will round to d?
  1.4201 +			 */
  1.4202 +			j = cmp(b, mlo);
  1.4203 +			delta = diff(S, mhi);
  1.4204 +			j1 = delta->sign ? 1 : cmp(b, delta);
  1.4205 +			Bfree(delta);
  1.4206 +#ifndef ROUND_BIASED
  1.4207 +			if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
  1.4208 +#ifdef Honor_FLT_ROUNDS
  1.4209 +				&& Rounding >= 1
  1.4210 +#endif
  1.4211 +								   ) {
  1.4212 +				if (dig == '9')
  1.4213 +					goto round_9_up;
  1.4214 +				if (j > 0)
  1.4215 +					dig++;
  1.4216 +#ifdef SET_INEXACT
  1.4217 +				else if (!b->x[0] && b->wds <= 1)
  1.4218 +					inexact = 0;
  1.4219 +#endif
  1.4220 +				*s++ = dig;
  1.4221 +				goto ret;
  1.4222 +				}
  1.4223 +#endif
  1.4224 +			if (j < 0 || (j == 0 && mode != 1
  1.4225 +#ifndef ROUND_BIASED
  1.4226 +							&& !(word1(&u) & 1)
  1.4227 +#endif
  1.4228 +					)) {
  1.4229 +				if (!b->x[0] && b->wds <= 1) {
  1.4230 +#ifdef SET_INEXACT
  1.4231 +					inexact = 0;
  1.4232 +#endif
  1.4233 +					goto accept_dig;
  1.4234 +					}
  1.4235 +#ifdef Honor_FLT_ROUNDS
  1.4236 +				if (mode > 1)
  1.4237 +				 switch(Rounding) {
  1.4238 +				  case 0: goto accept_dig;
  1.4239 +				  case 2: goto keep_dig;
  1.4240 +				  }
  1.4241 +#endif /*Honor_FLT_ROUNDS*/
  1.4242 +				if (j1 > 0) {
  1.4243 +					b = lshift(b, 1);
  1.4244 +					j1 = cmp(b, S);
  1.4245 +#ifdef ROUND_BIASED
  1.4246 +					if (j1 >= 0 /*)*/
  1.4247 +#else
  1.4248 +					if ((j1 > 0 || (j1 == 0 && dig & 1))
  1.4249 +#endif
  1.4250 +					&& dig++ == '9')
  1.4251 +						goto round_9_up;
  1.4252 +					}
  1.4253 + accept_dig:
  1.4254 +				*s++ = dig;
  1.4255 +				goto ret;
  1.4256 +				}
  1.4257 +			if (j1 > 0) {
  1.4258 +#ifdef Honor_FLT_ROUNDS
  1.4259 +				if (!Rounding)
  1.4260 +					goto accept_dig;
  1.4261 +#endif
  1.4262 +				if (dig == '9') { /* possible if i == 1 */
  1.4263 + round_9_up:
  1.4264 +					*s++ = '9';
  1.4265 +					goto roundoff;
  1.4266 +					}
  1.4267 +				*s++ = dig + 1;
  1.4268 +				goto ret;
  1.4269 +				}
  1.4270 +#ifdef Honor_FLT_ROUNDS
  1.4271 + keep_dig:
  1.4272 +#endif
  1.4273 +			*s++ = dig;
  1.4274 +			if (i == ilim)
  1.4275 +				break;
  1.4276 +			b = multadd(b, 10, 0);
  1.4277 +			if (mlo == mhi)
  1.4278 +				mlo = mhi = multadd(mhi, 10, 0);
  1.4279 +			else {
  1.4280 +				mlo = multadd(mlo, 10, 0);
  1.4281 +				mhi = multadd(mhi, 10, 0);
  1.4282 +				}
  1.4283 +			}
  1.4284 +		}
  1.4285 +	else
  1.4286 +		for(i = 1;; i++) {
  1.4287 +			*s++ = dig = quorem(b,S) + '0';
  1.4288 +			if (!b->x[0] && b->wds <= 1) {
  1.4289 +#ifdef SET_INEXACT
  1.4290 +				inexact = 0;
  1.4291 +#endif
  1.4292 +				goto ret;
  1.4293 +				}
  1.4294 +			if (i >= ilim)
  1.4295 +				break;
  1.4296 +			b = multadd(b, 10, 0);
  1.4297 +			}
  1.4298 +
  1.4299 +	/* Round off last digit */
  1.4300 +
  1.4301 +#ifdef Honor_FLT_ROUNDS
  1.4302 +	switch(Rounding) {
  1.4303 +	  case 0: goto trimzeros;
  1.4304 +	  case 2: goto roundoff;
  1.4305 +	  }
  1.4306 +#endif
  1.4307 +	b = lshift(b, 1);
  1.4308 +	j = cmp(b, S);
  1.4309 +#ifdef ROUND_BIASED
  1.4310 +	if (j >= 0)
  1.4311 +#else
  1.4312 +	if (j > 0 || (j == 0 && dig & 1))
  1.4313 +#endif
  1.4314 +		{
  1.4315 + roundoff:
  1.4316 +		while(*--s == '9')
  1.4317 +			if (s == s0) {
  1.4318 +				k++;
  1.4319 +				*s++ = '1';
  1.4320 +				goto ret;
  1.4321 +				}
  1.4322 +		++*s++;
  1.4323 +		}
  1.4324 +	else {
  1.4325 +#ifdef Honor_FLT_ROUNDS
  1.4326 + trimzeros:
  1.4327 +#endif
  1.4328 +		while(*--s == '0');
  1.4329 +		s++;
  1.4330 +		}
  1.4331 + ret:
  1.4332 +	Bfree(S);
  1.4333 +	if (mhi) {
  1.4334 +		if (mlo && mlo != mhi)
  1.4335 +			Bfree(mlo);
  1.4336 +		Bfree(mhi);
  1.4337 +		}
  1.4338 + ret1:
  1.4339 +#ifdef SET_INEXACT
  1.4340 +	if (inexact) {
  1.4341 +		if (!oldinexact) {
  1.4342 +			word0(&u) = Exp_1 + (70 << Exp_shift);
  1.4343 +			word1(&u) = 0;
  1.4344 +			dval(&u) += 1.;
  1.4345 +			}
  1.4346 +		}
  1.4347 +	else if (!oldinexact)
  1.4348 +		clear_inexact();
  1.4349 +#endif
  1.4350 +	Bfree(b);
  1.4351 +	*s = 0;
  1.4352 +	*decpt = k + 1;
  1.4353 +	if (rve)
  1.4354 +		*rve = s;
  1.4355 +	return s0;
  1.4356 +	}
  1.4357 +#ifdef __cplusplus
  1.4358 +}
  1.4359 +#endif

mercurial