xref: /csrg-svn/lib/libc/stdlib/strtod.c (revision 56731)
1*56731Sbostic #if defined(LIBC_SCCS) && !defined(lint)
2*56731Sbostic static char sccsid[] = "@(#)strtod.c	5.1 (Berkeley) 11/13/92";
3*56731Sbostic #endif /* LIBC_SCCS and not lint */
4*56731Sbostic 
5*56731Sbostic /****************************************************************
6*56731Sbostic  *
7*56731Sbostic  * The author of this software is David M. Gay.
8*56731Sbostic  *
9*56731Sbostic  * Copyright (c) 1991 by AT&T.
10*56731Sbostic  *
11*56731Sbostic  * Permission to use, copy, modify, and distribute this software for any
12*56731Sbostic  * purpose without fee is hereby granted, provided that this entire notice
13*56731Sbostic  * is included in all copies of any software which is or includes a copy
14*56731Sbostic  * or modification of this software and in all copies of the supporting
15*56731Sbostic  * documentation for such software.
16*56731Sbostic  *
17*56731Sbostic  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
18*56731Sbostic  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
19*56731Sbostic  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
20*56731Sbostic  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
21*56731Sbostic  *
22*56731Sbostic  ***************************************************************/
23*56731Sbostic 
24*56731Sbostic /* Please send bug reports to
25*56731Sbostic 	David M. Gay
26*56731Sbostic 	AT&T Bell Laboratories, Room 2C-463
27*56731Sbostic 	600 Mountain Avenue
28*56731Sbostic 	Murray Hill, NJ 07974-2070
29*56731Sbostic 	U.S.A.
30*56731Sbostic 	dmg@research.att.com or research!dmg
31*56731Sbostic  */
32*56731Sbostic 
33*56731Sbostic /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
34*56731Sbostic  *
35*56731Sbostic  * This strtod returns a nearest machine number to the input decimal
36*56731Sbostic  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
37*56731Sbostic  * broken by the IEEE round-even rule.  Otherwise ties are broken by
38*56731Sbostic  * biased rounding (add half and chop).
39*56731Sbostic  *
40*56731Sbostic  * Inspired loosely by William D. Clinger's paper "How to Read Floating
41*56731Sbostic  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
42*56731Sbostic  *
43*56731Sbostic  * Modifications:
44*56731Sbostic  *
45*56731Sbostic  *	1. We only require IEEE, IBM, or VAX double-precision
46*56731Sbostic  *		arithmetic (not IEEE double-extended).
47*56731Sbostic  *	2. We get by with floating-point arithmetic in a case that
48*56731Sbostic  *		Clinger missed -- when we're computing d * 10^n
49*56731Sbostic  *		for a small integer d and the integer n is not too
50*56731Sbostic  *		much larger than 22 (the maximum integer k for which
51*56731Sbostic  *		we can represent 10^k exactly), we may be able to
52*56731Sbostic  *		compute (d*10^k) * 10^(e-k) with just one roundoff.
53*56731Sbostic  *	3. Rather than a bit-at-a-time adjustment of the binary
54*56731Sbostic  *		result in the hard case, we use floating-point
55*56731Sbostic  *		arithmetic to determine the adjustment to within
56*56731Sbostic  *		one bit; only in really hard cases do we need to
57*56731Sbostic  *		compute a second residual.
58*56731Sbostic  *	4. Because of 3., we don't need a large table of powers of 10
59*56731Sbostic  *		for ten-to-e (just some small tables, e.g. of 10^k
60*56731Sbostic  *		for 0 <= k <= 22).
61*56731Sbostic  */
62*56731Sbostic 
63*56731Sbostic /*
64*56731Sbostic  * #define IEEE_8087 for IEEE-arithmetic machines where the least
65*56731Sbostic  *	significant byte has the lowest address.
66*56731Sbostic  * #define IEEE_MC68k for IEEE-arithmetic machines where the most
67*56731Sbostic  *	significant byte has the lowest address.
68*56731Sbostic  * #define Sudden_Underflow for IEEE-format machines without gradual
69*56731Sbostic  *	underflow (i.e., that flush to zero on underflow).
70*56731Sbostic  * #define IBM for IBM mainframe-style floating-point arithmetic.
71*56731Sbostic  * #define VAX for VAX-style floating-point arithmetic.
72*56731Sbostic  * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
73*56731Sbostic  * #define No_leftright to omit left-right logic in fast floating-point
74*56731Sbostic  *	computation of dtoa.
75*56731Sbostic  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
76*56731Sbostic  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
77*56731Sbostic  *	that use extended-precision instructions to compute rounded
78*56731Sbostic  *	products and quotients) with IBM.
79*56731Sbostic  * #define ROUND_BIASED for IEEE-format with biased rounding.
80*56731Sbostic  * #define Inaccurate_Divide for IEEE-format with correctly rounded
81*56731Sbostic  *	products but inaccurate quotients, e.g., for Intel i860.
82*56731Sbostic  * #define Just_16 to store 16 bits per 32-bit long when doing high-precision
83*56731Sbostic  *	integer arithmetic.  Whether this speeds things up or slows things
84*56731Sbostic  *	down depends on the machine and the number being converted.
85*56731Sbostic  * #define KR_headers for old-style C function headers.
86*56731Sbostic  * #define Bad_float_h if your system lacks a float.h or if it does not
87*56731Sbostic  *	define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
88*56731Sbostic  *	FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
89*56731Sbostic  */
90*56731Sbostic 
91*56731Sbostic #define IEEE_MC68k
92*56731Sbostic 
93*56731Sbostic #ifdef DEBUG
94*56731Sbostic #include "stdio.h"
95*56731Sbostic #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
96*56731Sbostic #endif
97*56731Sbostic 
98*56731Sbostic #ifdef __cplusplus
99*56731Sbostic #include "malloc.h"
100*56731Sbostic #include "memory.h"
101*56731Sbostic #else
102*56731Sbostic #ifndef KR_headers
103*56731Sbostic #include "stdlib.h"
104*56731Sbostic #include "string.h"
105*56731Sbostic #else
106*56731Sbostic #include "malloc.h"
107*56731Sbostic #include "memory.h"
108*56731Sbostic #endif
109*56731Sbostic #endif
110*56731Sbostic 
111*56731Sbostic #include "errno.h"
112*56731Sbostic #ifdef Bad_float_h
113*56731Sbostic #undef __STDC__
114*56731Sbostic #ifdef IEEE_MC68k
115*56731Sbostic #define IEEE_ARITHMETIC
116*56731Sbostic #endif
117*56731Sbostic #ifdef IEEE_8087
118*56731Sbostic #define IEEE_ARITHMETIC
119*56731Sbostic #endif
120*56731Sbostic #ifdef IEEE_ARITHMETIC
121*56731Sbostic #define DBL_DIG 15
122*56731Sbostic #define DBL_MAX_10_EXP 308
123*56731Sbostic #define DBL_MAX_EXP 1024
124*56731Sbostic #define FLT_RADIX 2
125*56731Sbostic #define FLT_ROUNDS 1
126*56731Sbostic #define DBL_MAX 1.7976931348623157e+308
127*56731Sbostic #endif
128*56731Sbostic 
129*56731Sbostic #ifdef IBM
130*56731Sbostic #define DBL_DIG 16
131*56731Sbostic #define DBL_MAX_10_EXP 75
132*56731Sbostic #define DBL_MAX_EXP 63
133*56731Sbostic #define FLT_RADIX 16
134*56731Sbostic #define FLT_ROUNDS 0
135*56731Sbostic #define DBL_MAX 7.2370055773322621e+75
136*56731Sbostic #endif
137*56731Sbostic 
138*56731Sbostic #ifdef VAX
139*56731Sbostic #define DBL_DIG 16
140*56731Sbostic #define DBL_MAX_10_EXP 38
141*56731Sbostic #define DBL_MAX_EXP 127
142*56731Sbostic #define FLT_RADIX 2
143*56731Sbostic #define FLT_ROUNDS 1
144*56731Sbostic #define DBL_MAX 1.7014118346046923e+38
145*56731Sbostic #endif
146*56731Sbostic 
147*56731Sbostic #ifndef LONG_MAX
148*56731Sbostic #define LONG_MAX 2147483647
149*56731Sbostic #endif
150*56731Sbostic #else
151*56731Sbostic #include "float.h"
152*56731Sbostic #endif
153*56731Sbostic #ifndef __MATH_H__
154*56731Sbostic #include "math.h"
155*56731Sbostic #endif
156*56731Sbostic 
157*56731Sbostic #ifdef __cplusplus
158*56731Sbostic extern "C" {
159*56731Sbostic #endif
160*56731Sbostic 
161*56731Sbostic #ifndef CONST
162*56731Sbostic #ifdef KR_headers
163*56731Sbostic #define CONST /* blank */
164*56731Sbostic #else
165*56731Sbostic #define CONST const
166*56731Sbostic #endif
167*56731Sbostic #endif
168*56731Sbostic 
169*56731Sbostic #ifdef Unsigned_Shifts
170*56731Sbostic #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
171*56731Sbostic #else
172*56731Sbostic #define Sign_Extend(a,b) /*no-op*/
173*56731Sbostic #endif
174*56731Sbostic 
175*56731Sbostic #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
176*56731Sbostic Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
177*56731Sbostic #endif
178*56731Sbostic 
179*56731Sbostic #ifdef IEEE_8087
180*56731Sbostic #define word0(x) ((unsigned long *)&x)[1]
181*56731Sbostic #define word1(x) ((unsigned long *)&x)[0]
182*56731Sbostic #else
183*56731Sbostic #define word0(x) ((unsigned long *)&x)[0]
184*56731Sbostic #define word1(x) ((unsigned long *)&x)[1]
185*56731Sbostic #endif
186*56731Sbostic 
187*56731Sbostic /* The following definition of Storeinc is appropriate for MIPS processors.
188*56731Sbostic  * An alternative that might be better on some machines is
189*56731Sbostic  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
190*56731Sbostic  */
191*56731Sbostic #if defined(IEEE_8087) + defined(VAX)
192*56731Sbostic #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
193*56731Sbostic ((unsigned short *)a)[0] = (unsigned short)c, a++)
194*56731Sbostic #else
195*56731Sbostic #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
196*56731Sbostic ((unsigned short *)a)[1] = (unsigned short)c, a++)
197*56731Sbostic #endif
198*56731Sbostic 
199*56731Sbostic /* #define P DBL_MANT_DIG */
200*56731Sbostic /* Ten_pmax = floor(P*log(2)/log(5)) */
201*56731Sbostic /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
202*56731Sbostic /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
203*56731Sbostic /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
204*56731Sbostic 
205*56731Sbostic #if defined(IEEE_8087) + defined(IEEE_MC68k)
206*56731Sbostic #define Exp_shift  20
207*56731Sbostic #define Exp_shift1 20
208*56731Sbostic #define Exp_msk1    0x100000
209*56731Sbostic #define Exp_msk11   0x100000
210*56731Sbostic #define Exp_mask  0x7ff00000
211*56731Sbostic #define P 53
212*56731Sbostic #define Bias 1023
213*56731Sbostic #define IEEE_Arith
214*56731Sbostic #define Emin (-1022)
215*56731Sbostic #define Exp_1  0x3ff00000
216*56731Sbostic #define Exp_11 0x3ff00000
217*56731Sbostic #define Ebits 11
218*56731Sbostic #define Frac_mask  0xfffff
219*56731Sbostic #define Frac_mask1 0xfffff
220*56731Sbostic #define Ten_pmax 22
221*56731Sbostic #define Bletch 0x10
222*56731Sbostic #define Bndry_mask  0xfffff
223*56731Sbostic #define Bndry_mask1 0xfffff
224*56731Sbostic #define LSB 1
225*56731Sbostic #define Sign_bit 0x80000000
226*56731Sbostic #define Log2P 1
227*56731Sbostic #define Tiny0 0
228*56731Sbostic #define Tiny1 1
229*56731Sbostic #define Quick_max 14
230*56731Sbostic #define Int_max 14
231*56731Sbostic #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
232*56731Sbostic #else
233*56731Sbostic #undef  Sudden_Underflow
234*56731Sbostic #define Sudden_Underflow
235*56731Sbostic #ifdef IBM
236*56731Sbostic #define Exp_shift  24
237*56731Sbostic #define Exp_shift1 24
238*56731Sbostic #define Exp_msk1   0x1000000
239*56731Sbostic #define Exp_msk11  0x1000000
240*56731Sbostic #define Exp_mask  0x7f000000
241*56731Sbostic #define P 14
242*56731Sbostic #define Bias 65
243*56731Sbostic #define Exp_1  0x41000000
244*56731Sbostic #define Exp_11 0x41000000
245*56731Sbostic #define Ebits 8	/* exponent has 7 bits, but 8 is the right value in b2d */
246*56731Sbostic #define Frac_mask  0xffffff
247*56731Sbostic #define Frac_mask1 0xffffff
248*56731Sbostic #define Bletch 4
249*56731Sbostic #define Ten_pmax 22
250*56731Sbostic #define Bndry_mask  0xefffff
251*56731Sbostic #define Bndry_mask1 0xffffff
252*56731Sbostic #define LSB 1
253*56731Sbostic #define Sign_bit 0x80000000
254*56731Sbostic #define Log2P 4
255*56731Sbostic #define Tiny0 0x100000
256*56731Sbostic #define Tiny1 0
257*56731Sbostic #define Quick_max 14
258*56731Sbostic #define Int_max 15
259*56731Sbostic #else /* VAX */
260*56731Sbostic #define Exp_shift  23
261*56731Sbostic #define Exp_shift1 7
262*56731Sbostic #define Exp_msk1    0x80
263*56731Sbostic #define Exp_msk11   0x800000
264*56731Sbostic #define Exp_mask  0x7f80
265*56731Sbostic #define P 56
266*56731Sbostic #define Bias 129
267*56731Sbostic #define Exp_1  0x40800000
268*56731Sbostic #define Exp_11 0x4080
269*56731Sbostic #define Ebits 8
270*56731Sbostic #define Frac_mask  0x7fffff
271*56731Sbostic #define Frac_mask1 0xffff007f
272*56731Sbostic #define Ten_pmax 24
273*56731Sbostic #define Bletch 2
274*56731Sbostic #define Bndry_mask  0xffff007f
275*56731Sbostic #define Bndry_mask1 0xffff007f
276*56731Sbostic #define LSB 0x10000
277*56731Sbostic #define Sign_bit 0x8000
278*56731Sbostic #define Log2P 1
279*56731Sbostic #define Tiny0 0x80
280*56731Sbostic #define Tiny1 0
281*56731Sbostic #define Quick_max 15
282*56731Sbostic #define Int_max 15
283*56731Sbostic #endif
284*56731Sbostic #endif
285*56731Sbostic 
286*56731Sbostic #ifndef IEEE_Arith
287*56731Sbostic #define ROUND_BIASED
288*56731Sbostic #endif
289*56731Sbostic 
290*56731Sbostic #ifdef RND_PRODQUOT
291*56731Sbostic #define rounded_product(a,b) a = rnd_prod(a, b)
292*56731Sbostic #define rounded_quotient(a,b) a = rnd_quot(a, b)
293*56731Sbostic #ifdef KR_headers
294*56731Sbostic extern double rnd_prod(), rnd_quot();
295*56731Sbostic #else
296*56731Sbostic extern double rnd_prod(double, double), rnd_quot(double, double);
297*56731Sbostic #endif
298*56731Sbostic #else
299*56731Sbostic #define rounded_product(a,b) a *= b
300*56731Sbostic #define rounded_quotient(a,b) a /= b
301*56731Sbostic #endif
302*56731Sbostic 
303*56731Sbostic #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
304*56731Sbostic #define Big1 0xffffffff
305*56731Sbostic 
306*56731Sbostic #ifndef Just_16
307*56731Sbostic /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
308*56731Sbostic  * This makes some inner loops simpler and sometimes saves work
309*56731Sbostic  * during multiplications, but it often seems to make things slightly
310*56731Sbostic  * slower.  Hence the default is now to store 32 bits per long.
311*56731Sbostic  */
312*56731Sbostic #ifndef Pack_32
313*56731Sbostic #define Pack_32
314*56731Sbostic #endif
315*56731Sbostic #endif
316*56731Sbostic 
317*56731Sbostic #define Kmax 15
318*56731Sbostic 
319*56731Sbostic #ifdef __cplusplus
320*56731Sbostic extern "C" double strtod(const char *s00, char **se);
321*56731Sbostic extern "C" char *dtoa(double d, int mode, int ndigits,
322*56731Sbostic 			int *decpt, int *sign, char **rve);
323*56731Sbostic #endif
324*56731Sbostic 
325*56731Sbostic  struct
326*56731Sbostic Bigint {
327*56731Sbostic 	struct Bigint *next;
328*56731Sbostic 	int k, maxwds, sign, wds;
329*56731Sbostic 	unsigned long x[1];
330*56731Sbostic };
331*56731Sbostic 
332*56731Sbostic  typedef struct Bigint Bigint;
333*56731Sbostic 
334*56731Sbostic  static Bigint *freelist[Kmax+1];
335*56731Sbostic 
336*56731Sbostic  static Bigint *
337*56731Sbostic Balloc
338*56731Sbostic #ifdef KR_headers
339*56731Sbostic 	(k) int k;
340*56731Sbostic #else
341*56731Sbostic 	(int k)
342*56731Sbostic #endif
343*56731Sbostic {
344*56731Sbostic 	int x;
345*56731Sbostic 	Bigint *rv;
346*56731Sbostic 
347*56731Sbostic 	if (rv = freelist[k]) {
348*56731Sbostic 		freelist[k] = rv->next;
349*56731Sbostic 	} else {
350*56731Sbostic 		x = 1 << k;
351*56731Sbostic 		rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long));
352*56731Sbostic 		rv->k = k;
353*56731Sbostic 		rv->maxwds = x;
354*56731Sbostic 	}
355*56731Sbostic 	rv->sign = rv->wds = 0;
356*56731Sbostic 	return rv;
357*56731Sbostic }
358*56731Sbostic 
359*56731Sbostic  static void
360*56731Sbostic Bfree
361*56731Sbostic #ifdef KR_headers
362*56731Sbostic 	(v) Bigint *v;
363*56731Sbostic #else
364*56731Sbostic 	(Bigint *v)
365*56731Sbostic #endif
366*56731Sbostic {
367*56731Sbostic 	if (v) {
368*56731Sbostic 		v->next = freelist[v->k];
369*56731Sbostic 		freelist[v->k] = v;
370*56731Sbostic 	}
371*56731Sbostic }
372*56731Sbostic 
373*56731Sbostic #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
374*56731Sbostic y->wds*sizeof(long) + 2*sizeof(int))
375*56731Sbostic 
376*56731Sbostic  static Bigint *
377*56731Sbostic multadd
378*56731Sbostic #ifdef KR_headers
379*56731Sbostic 	(b, m, a) Bigint *b; int m, a;
380*56731Sbostic #else
381*56731Sbostic 	(Bigint *b, int m, int a)	/* multiply by m and add a */
382*56731Sbostic #endif
383*56731Sbostic {
384*56731Sbostic 	int i, wds;
385*56731Sbostic 	unsigned long *x, y;
386*56731Sbostic #ifdef Pack_32
387*56731Sbostic 	unsigned long xi, z;
388*56731Sbostic #endif
389*56731Sbostic 	Bigint *b1;
390*56731Sbostic 
391*56731Sbostic 	wds = b->wds;
392*56731Sbostic 	x = b->x;
393*56731Sbostic 	i = 0;
394*56731Sbostic 	do {
395*56731Sbostic #ifdef Pack_32
396*56731Sbostic 		xi = *x;
397*56731Sbostic 		y = (xi & 0xffff) * m + a;
398*56731Sbostic 		z = (xi >> 16) * m + (y >> 16);
399*56731Sbostic 		a = (int)(z >> 16);
400*56731Sbostic 		*x++ = (z << 16) + (y & 0xffff);
401*56731Sbostic #else
402*56731Sbostic 		y = *x * m + a;
403*56731Sbostic 		a = (int)(y >> 16);
404*56731Sbostic 		*x++ = y & 0xffff;
405*56731Sbostic #endif
406*56731Sbostic 	} while (++i < wds);
407*56731Sbostic 	if (a) {
408*56731Sbostic 		if (wds >= b->maxwds) {
409*56731Sbostic 			b1 = Balloc(b->k+1);
410*56731Sbostic 			Bcopy(b1, b);
411*56731Sbostic 			Bfree(b);
412*56731Sbostic 			b = b1;
413*56731Sbostic 			}
414*56731Sbostic 		b->x[wds++] = a;
415*56731Sbostic 		b->wds = wds;
416*56731Sbostic 	}
417*56731Sbostic 	return b;
418*56731Sbostic }
419*56731Sbostic 
420*56731Sbostic  static Bigint *
421*56731Sbostic s2b
422*56731Sbostic #ifdef KR_headers
423*56731Sbostic 	(s, nd0, nd, y9) CONST char *s; int nd0, nd; unsigned long y9;
424*56731Sbostic #else
425*56731Sbostic 	(CONST char *s, int nd0, int nd, unsigned long y9)
426*56731Sbostic #endif
427*56731Sbostic {
428*56731Sbostic 	Bigint *b;
429*56731Sbostic 	int i, k;
430*56731Sbostic 	long x, y;
431*56731Sbostic 
432*56731Sbostic 	x = (nd + 8) / 9;
433*56731Sbostic 	for (k = 0, y = 1; x > y; y <<= 1, k++) ;
434*56731Sbostic #ifdef Pack_32
435*56731Sbostic 	b = Balloc(k);
436*56731Sbostic 	b->x[0] = y9;
437*56731Sbostic 	b->wds = 1;
438*56731Sbostic #else
439*56731Sbostic 	b = Balloc(k+1);
440*56731Sbostic 	b->x[0] = y9 & 0xffff;
441*56731Sbostic 	b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
442*56731Sbostic #endif
443*56731Sbostic 
444*56731Sbostic 	i = 9;
445*56731Sbostic 	if (9 < nd0) {
446*56731Sbostic 		s += 9;
447*56731Sbostic 		do
448*56731Sbostic 			b = multadd(b, 10, *s++ - '0');
449*56731Sbostic 		while (++i < nd0);
450*56731Sbostic 		s++;
451*56731Sbostic 	} else
452*56731Sbostic 		s += 10;
453*56731Sbostic 	for (; i < nd; i++)
454*56731Sbostic 		b = multadd(b, 10, *s++ - '0');
455*56731Sbostic 	return b;
456*56731Sbostic }
457*56731Sbostic 
458*56731Sbostic  static int
459*56731Sbostic hi0bits
460*56731Sbostic #ifdef KR_headers
461*56731Sbostic 	(x) register unsigned long x;
462*56731Sbostic #else
463*56731Sbostic 	(register unsigned long x)
464*56731Sbostic #endif
465*56731Sbostic {
466*56731Sbostic 	register int k = 0;
467*56731Sbostic 
468*56731Sbostic 	if (!(x & 0xffff0000)) {
469*56731Sbostic 		k = 16;
470*56731Sbostic 		x <<= 16;
471*56731Sbostic 	}
472*56731Sbostic 	if (!(x & 0xff000000)) {
473*56731Sbostic 		k += 8;
474*56731Sbostic 		x <<= 8;
475*56731Sbostic 	}
476*56731Sbostic 	if (!(x & 0xf0000000)) {
477*56731Sbostic 		k += 4;
478*56731Sbostic 		x <<= 4;
479*56731Sbostic 	}
480*56731Sbostic 	if (!(x & 0xc0000000)) {
481*56731Sbostic 		k += 2;
482*56731Sbostic 		x <<= 2;
483*56731Sbostic 	}
484*56731Sbostic 	if (!(x & 0x80000000)) {
485*56731Sbostic 		k++;
486*56731Sbostic 		if (!(x & 0x40000000))
487*56731Sbostic 			return 32;
488*56731Sbostic 	}
489*56731Sbostic 	return k;
490*56731Sbostic }
491*56731Sbostic 
492*56731Sbostic  static int
493*56731Sbostic lo0bits
494*56731Sbostic #ifdef KR_headers
495*56731Sbostic 	(y) unsigned long *y;
496*56731Sbostic #else
497*56731Sbostic 	(unsigned long *y)
498*56731Sbostic #endif
499*56731Sbostic {
500*56731Sbostic 	register int k;
501*56731Sbostic 	register unsigned long x = *y;
502*56731Sbostic 
503*56731Sbostic 	if (x & 7) {
504*56731Sbostic 		if (x & 1)
505*56731Sbostic 			return 0;
506*56731Sbostic 		if (x & 2) {
507*56731Sbostic 			*y = x >> 1;
508*56731Sbostic 			return 1;
509*56731Sbostic 		}
510*56731Sbostic 		*y = x >> 2;
511*56731Sbostic 		return 2;
512*56731Sbostic 	}
513*56731Sbostic 	k = 0;
514*56731Sbostic 	if (!(x & 0xffff)) {
515*56731Sbostic 		k = 16;
516*56731Sbostic 		x >>= 16;
517*56731Sbostic 	}
518*56731Sbostic 	if (!(x & 0xff)) {
519*56731Sbostic 		k += 8;
520*56731Sbostic 		x >>= 8;
521*56731Sbostic 	}
522*56731Sbostic 	if (!(x & 0xf)) {
523*56731Sbostic 		k += 4;
524*56731Sbostic 		x >>= 4;
525*56731Sbostic 	}
526*56731Sbostic 	if (!(x & 0x3)) {
527*56731Sbostic 		k += 2;
528*56731Sbostic 		x >>= 2;
529*56731Sbostic 	}
530*56731Sbostic 	if (!(x & 1)) {
531*56731Sbostic 		k++;
532*56731Sbostic 		x >>= 1;
533*56731Sbostic 		if (!x & 1)
534*56731Sbostic 			return 32;
535*56731Sbostic 	}
536*56731Sbostic 	*y = x;
537*56731Sbostic 	return k;
538*56731Sbostic }
539*56731Sbostic 
540*56731Sbostic  static Bigint *
541*56731Sbostic i2b
542*56731Sbostic #ifdef KR_headers
543*56731Sbostic 	(i) int i;
544*56731Sbostic #else
545*56731Sbostic 	(int i)
546*56731Sbostic #endif
547*56731Sbostic {
548*56731Sbostic 	Bigint *b;
549*56731Sbostic 
550*56731Sbostic 	b = Balloc(1);
551*56731Sbostic 	b->x[0] = i;
552*56731Sbostic 	b->wds = 1;
553*56731Sbostic 	return b;
554*56731Sbostic 	}
555*56731Sbostic 
556*56731Sbostic  static Bigint *
557*56731Sbostic mult
558*56731Sbostic #ifdef KR_headers
559*56731Sbostic 	(a, b) Bigint *a, *b;
560*56731Sbostic #else
561*56731Sbostic 	(Bigint *a, Bigint *b)
562*56731Sbostic #endif
563*56731Sbostic {
564*56731Sbostic 	Bigint *c;
565*56731Sbostic 	int k, wa, wb, wc;
566*56731Sbostic 	unsigned long carry, y, z;
567*56731Sbostic 	unsigned long *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
568*56731Sbostic #ifdef Pack_32
569*56731Sbostic 	unsigned long z2;
570*56731Sbostic #endif
571*56731Sbostic 
572*56731Sbostic 	if (a->wds < b->wds) {
573*56731Sbostic 		c = a;
574*56731Sbostic 		a = b;
575*56731Sbostic 		b = c;
576*56731Sbostic 	}
577*56731Sbostic 	k = a->k;
578*56731Sbostic 	wa = a->wds;
579*56731Sbostic 	wb = b->wds;
580*56731Sbostic 	wc = wa + wb;
581*56731Sbostic 	if (wc > a->maxwds)
582*56731Sbostic 		k++;
583*56731Sbostic 	c = Balloc(k);
584*56731Sbostic 	for (x = c->x, xa = x + wc; x < xa; x++)
585*56731Sbostic 		*x = 0;
586*56731Sbostic 	xa = a->x;
587*56731Sbostic 	xae = xa + wa;
588*56731Sbostic 	xb = b->x;
589*56731Sbostic 	xbe = xb + wb;
590*56731Sbostic 	xc0 = c->x;
591*56731Sbostic #ifdef Pack_32
592*56731Sbostic 	for (; xb < xbe; xb++, xc0++) {
593*56731Sbostic 		if (y = *xb & 0xffff) {
594*56731Sbostic 			x = xa;
595*56731Sbostic 			xc = xc0;
596*56731Sbostic 			carry = 0;
597*56731Sbostic 			do {
598*56731Sbostic 				z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
599*56731Sbostic 				carry = z >> 16;
600*56731Sbostic 				z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
601*56731Sbostic 				carry = z2 >> 16;
602*56731Sbostic 				Storeinc(xc, z2, z);
603*56731Sbostic 			} while (x < xae);
604*56731Sbostic 			*xc = carry;
605*56731Sbostic 		}
606*56731Sbostic 		if (y = *xb >> 16) {
607*56731Sbostic 			x = xa;
608*56731Sbostic 			xc = xc0;
609*56731Sbostic 			carry = 0;
610*56731Sbostic 			z2 = *xc;
611*56731Sbostic 			do {
612*56731Sbostic 				z = (*x & 0xffff) * y + (*xc >> 16) + carry;
613*56731Sbostic 				carry = z >> 16;
614*56731Sbostic 				Storeinc(xc, z, z2);
615*56731Sbostic 				z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
616*56731Sbostic 				carry = z2 >> 16;
617*56731Sbostic 			} while (x < xae);
618*56731Sbostic 			*xc = z2;
619*56731Sbostic 		}
620*56731Sbostic 	}
621*56731Sbostic #else
622*56731Sbostic 	for (; xb < xbe; xc0++) {
623*56731Sbostic 		if (y = *xb++) {
624*56731Sbostic 			x = xa;
625*56731Sbostic 			xc = xc0;
626*56731Sbostic 			carry = 0;
627*56731Sbostic 			do {
628*56731Sbostic 				z = *x++ * y + *xc + carry;
629*56731Sbostic 				carry = z >> 16;
630*56731Sbostic 				*xc++ = z & 0xffff;
631*56731Sbostic 			} while (x < xae);
632*56731Sbostic 			*xc = carry;
633*56731Sbostic 		}
634*56731Sbostic 	}
635*56731Sbostic #endif
636*56731Sbostic 	for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
637*56731Sbostic 	c->wds = wc;
638*56731Sbostic 	return c;
639*56731Sbostic }
640*56731Sbostic 
641*56731Sbostic  static Bigint *p5s;
642*56731Sbostic 
643*56731Sbostic  static Bigint *
644*56731Sbostic pow5mult
645*56731Sbostic #ifdef KR_headers
646*56731Sbostic 	(b, k) Bigint *b; int k;
647*56731Sbostic #else
648*56731Sbostic 	(Bigint *b, int k)
649*56731Sbostic #endif
650*56731Sbostic {
651*56731Sbostic 	Bigint *b1, *p5, *p51;
652*56731Sbostic 	int i;
653*56731Sbostic 	static int p05[3] = { 5, 25, 125 };
654*56731Sbostic 
655*56731Sbostic 	if (i = k & 3)
656*56731Sbostic 		b = multadd(b, p05[i-1], 0);
657*56731Sbostic 
658*56731Sbostic 	if (!(k >>= 2))
659*56731Sbostic 		return b;
660*56731Sbostic 	if (!(p5 = p5s)) {
661*56731Sbostic 		/* first time */
662*56731Sbostic 		p5 = p5s = i2b(625);
663*56731Sbostic 		p5->next = 0;
664*56731Sbostic 	}
665*56731Sbostic 	for (;;) {
666*56731Sbostic 		if (k & 1) {
667*56731Sbostic 			b1 = mult(b, p5);
668*56731Sbostic 			Bfree(b);
669*56731Sbostic 			b = b1;
670*56731Sbostic 		}
671*56731Sbostic 		if (!(k >>= 1))
672*56731Sbostic 			break;
673*56731Sbostic 		if (!(p51 = p5->next)) {
674*56731Sbostic 			p51 = p5->next = mult(p5,p5);
675*56731Sbostic 			p51->next = 0;
676*56731Sbostic 		}
677*56731Sbostic 		p5 = p51;
678*56731Sbostic 	}
679*56731Sbostic 	return b;
680*56731Sbostic }
681*56731Sbostic 
682*56731Sbostic  static Bigint *
683*56731Sbostic lshift
684*56731Sbostic #ifdef KR_headers
685*56731Sbostic 	(b, k) Bigint *b; int k;
686*56731Sbostic #else
687*56731Sbostic 	(Bigint *b, int k)
688*56731Sbostic #endif
689*56731Sbostic {
690*56731Sbostic 	int i, k1, n, n1;
691*56731Sbostic 	Bigint *b1;
692*56731Sbostic 	unsigned long *x, *x1, *xe, z;
693*56731Sbostic 
694*56731Sbostic #ifdef Pack_32
695*56731Sbostic 	n = k >> 5;
696*56731Sbostic #else
697*56731Sbostic 	n = k >> 4;
698*56731Sbostic #endif
699*56731Sbostic 	k1 = b->k;
700*56731Sbostic 	n1 = n + b->wds + 1;
701*56731Sbostic 	for (i = b->maxwds; n1 > i; i <<= 1)
702*56731Sbostic 		k1++;
703*56731Sbostic 	b1 = Balloc(k1);
704*56731Sbostic 	x1 = b1->x;
705*56731Sbostic 	for (i = 0; i < n; i++)
706*56731Sbostic 		*x1++ = 0;
707*56731Sbostic 	x = b->x;
708*56731Sbostic 	xe = x + b->wds;
709*56731Sbostic #ifdef Pack_32
710*56731Sbostic 	if (k &= 0x1f) {
711*56731Sbostic 		k1 = 32 - k;
712*56731Sbostic 		z = 0;
713*56731Sbostic 		do {
714*56731Sbostic 			*x1++ = *x << k | z;
715*56731Sbostic 			z = *x++ >> k1;
716*56731Sbostic 		} while (x < xe);
717*56731Sbostic 		if (*x1 = z)
718*56731Sbostic 			++n1;
719*56731Sbostic 	}
720*56731Sbostic #else
721*56731Sbostic 	if (k &= 0xf) {
722*56731Sbostic 		k1 = 16 - k;
723*56731Sbostic 		z = 0;
724*56731Sbostic 		do {
725*56731Sbostic 			*x1++ = *x << k  & 0xffff | z;
726*56731Sbostic 			z = *x++ >> k1;
727*56731Sbostic 		} while (x < xe);
728*56731Sbostic 		if (*x1 = z)
729*56731Sbostic 			++n1;
730*56731Sbostic 	}
731*56731Sbostic #endif
732*56731Sbostic 	else
733*56731Sbostic 		do
734*56731Sbostic 			*x1++ = *x++;
735*56731Sbostic 		while (x < xe);
736*56731Sbostic 	b1->wds = n1 - 1;
737*56731Sbostic 	Bfree(b);
738*56731Sbostic 	return b1;
739*56731Sbostic }
740*56731Sbostic 
741*56731Sbostic  static int
742*56731Sbostic cmp
743*56731Sbostic #ifdef KR_headers
744*56731Sbostic 	(a, b) Bigint *a, *b;
745*56731Sbostic #else
746*56731Sbostic 	(Bigint *a, Bigint *b)
747*56731Sbostic #endif
748*56731Sbostic {
749*56731Sbostic 	unsigned long *xa, *xa0, *xb, *xb0;
750*56731Sbostic 	int i, j;
751*56731Sbostic 
752*56731Sbostic 	i = a->wds;
753*56731Sbostic 	j = b->wds;
754*56731Sbostic #ifdef DEBUG
755*56731Sbostic 	if (i > 1 && !a->x[i-1])
756*56731Sbostic 		Bug("cmp called with a->x[a->wds-1] == 0");
757*56731Sbostic 	if (j > 1 && !b->x[j-1])
758*56731Sbostic 		Bug("cmp called with b->x[b->wds-1] == 0");
759*56731Sbostic #endif
760*56731Sbostic 	if (i -= j)
761*56731Sbostic 		return i;
762*56731Sbostic 	xa0 = a->x;
763*56731Sbostic 	xa = xa0 + j;
764*56731Sbostic 	xb0 = b->x;
765*56731Sbostic 	xb = xb0 + j;
766*56731Sbostic 	for (;;) {
767*56731Sbostic 		if (*--xa != *--xb)
768*56731Sbostic 			return *xa < *xb ? -1 : 1;
769*56731Sbostic 		if (xa <= xa0)
770*56731Sbostic 			break;
771*56731Sbostic 	}
772*56731Sbostic 	return 0;
773*56731Sbostic }
774*56731Sbostic 
775*56731Sbostic  static Bigint *
776*56731Sbostic diff
777*56731Sbostic #ifdef KR_headers
778*56731Sbostic 	(a, b) Bigint *a, *b;
779*56731Sbostic #else
780*56731Sbostic 	(Bigint *a, Bigint *b)
781*56731Sbostic #endif
782*56731Sbostic {
783*56731Sbostic 	Bigint *c;
784*56731Sbostic 	int i, wa, wb;
785*56731Sbostic 	long borrow, y;	/* We need signed shifts here. */
786*56731Sbostic 	unsigned long *xa, *xae, *xb, *xbe, *xc;
787*56731Sbostic #ifdef Pack_32
788*56731Sbostic 	long z;
789*56731Sbostic #endif
790*56731Sbostic 
791*56731Sbostic 	i = cmp(a,b);
792*56731Sbostic 	if (!i) {
793*56731Sbostic 		c = Balloc(0);
794*56731Sbostic 		c->wds = 1;
795*56731Sbostic 		c->x[0] = 0;
796*56731Sbostic 		return c;
797*56731Sbostic 	}
798*56731Sbostic 	if (i < 0) {
799*56731Sbostic 		c = a;
800*56731Sbostic 		a = b;
801*56731Sbostic 		b = c;
802*56731Sbostic 		i = 1;
803*56731Sbostic 	} else
804*56731Sbostic 		i = 0;
805*56731Sbostic 	c = Balloc(a->k);
806*56731Sbostic 	c->sign = i;
807*56731Sbostic 	wa = a->wds;
808*56731Sbostic 	xa = a->x;
809*56731Sbostic 	xae = xa + wa;
810*56731Sbostic 	wb = b->wds;
811*56731Sbostic 	xb = b->x;
812*56731Sbostic 	xbe = xb + wb;
813*56731Sbostic 	xc = c->x;
814*56731Sbostic 	borrow = 0;
815*56731Sbostic #ifdef Pack_32
816*56731Sbostic 	do {
817*56731Sbostic 		y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
818*56731Sbostic 		borrow = y >> 16;
819*56731Sbostic 		Sign_Extend(borrow, y);
820*56731Sbostic 		z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
821*56731Sbostic 		borrow = z >> 16;
822*56731Sbostic 		Sign_Extend(borrow, z);
823*56731Sbostic 		Storeinc(xc, z, y);
824*56731Sbostic 	} while (xb < xbe);
825*56731Sbostic 	while (xa < xae) {
826*56731Sbostic 		y = (*xa & 0xffff) + borrow;
827*56731Sbostic 		borrow = y >> 16;
828*56731Sbostic 		Sign_Extend(borrow, y);
829*56731Sbostic 		z = (*xa++ >> 16) + borrow;
830*56731Sbostic 		borrow = z >> 16;
831*56731Sbostic 		Sign_Extend(borrow, z);
832*56731Sbostic 		Storeinc(xc, z, y);
833*56731Sbostic 	}
834*56731Sbostic #else
835*56731Sbostic 	do {
836*56731Sbostic 		y = *xa++ - *xb++ + borrow;
837*56731Sbostic 		borrow = y >> 16;
838*56731Sbostic 		Sign_Extend(borrow, y);
839*56731Sbostic 		*xc++ = y & 0xffff;
840*56731Sbostic 	} while (xb < xbe);
841*56731Sbostic 	while (xa < xae) {
842*56731Sbostic 		y = *xa++ + borrow;
843*56731Sbostic 		borrow = y >> 16;
844*56731Sbostic 		Sign_Extend(borrow, y);
845*56731Sbostic 		*xc++ = y & 0xffff;
846*56731Sbostic 	}
847*56731Sbostic #endif
848*56731Sbostic 	while (!*--xc)
849*56731Sbostic 		wa--;
850*56731Sbostic 	c->wds = wa;
851*56731Sbostic 	return c;
852*56731Sbostic }
853*56731Sbostic 
854*56731Sbostic  static double
855*56731Sbostic ulp
856*56731Sbostic #ifdef KR_headers
857*56731Sbostic 	(x) double x;
858*56731Sbostic #else
859*56731Sbostic 	(double x)
860*56731Sbostic #endif
861*56731Sbostic {
862*56731Sbostic 	register long L;
863*56731Sbostic 	double a;
864*56731Sbostic 
865*56731Sbostic 	L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
866*56731Sbostic #ifndef Sudden_Underflow
867*56731Sbostic 	if (L > 0) {
868*56731Sbostic #endif
869*56731Sbostic #ifdef IBM
870*56731Sbostic 		L |= Exp_msk1 >> 4;
871*56731Sbostic #endif
872*56731Sbostic 		word0(a) = L;
873*56731Sbostic 		word1(a) = 0;
874*56731Sbostic #ifndef Sudden_Underflow
875*56731Sbostic 	} else {
876*56731Sbostic 		L = -L >> Exp_shift;
877*56731Sbostic 		if (L < Exp_shift) {
878*56731Sbostic 			word0(a) = 0x80000 >> L;
879*56731Sbostic 			word1(a) = 0;
880*56731Sbostic 		} else {
881*56731Sbostic 			word0(a) = 0;
882*56731Sbostic 			L -= Exp_shift;
883*56731Sbostic 			word1(a) = L >= 31 ? 1 : 1 << 31 - L;
884*56731Sbostic 		}
885*56731Sbostic 	}
886*56731Sbostic #endif
887*56731Sbostic 	return a;
888*56731Sbostic }
889*56731Sbostic 
890*56731Sbostic  static double
891*56731Sbostic b2d
892*56731Sbostic #ifdef KR_headers
893*56731Sbostic 	(a, e) Bigint *a; int *e;
894*56731Sbostic #else
895*56731Sbostic 	(Bigint *a, int *e)
896*56731Sbostic #endif
897*56731Sbostic {
898*56731Sbostic 	unsigned long *xa, *xa0, w, y, z;
899*56731Sbostic 	int k;
900*56731Sbostic 	double d;
901*56731Sbostic #ifdef VAX
902*56731Sbostic 	unsigned long d0, d1;
903*56731Sbostic #else
904*56731Sbostic #define d0 word0(d)
905*56731Sbostic #define d1 word1(d)
906*56731Sbostic #endif
907*56731Sbostic 
908*56731Sbostic 	xa0 = a->x;
909*56731Sbostic 	xa = xa0 + a->wds;
910*56731Sbostic 	y = *--xa;
911*56731Sbostic #ifdef DEBUG
912*56731Sbostic 	if (!y) Bug("zero y in b2d");
913*56731Sbostic #endif
914*56731Sbostic 	k = hi0bits(y);
915*56731Sbostic 	*e = 32 - k;
916*56731Sbostic #ifdef Pack_32
917*56731Sbostic 	if (k < Ebits) {
918*56731Sbostic 		d0 = Exp_1 | y >> Ebits - k;
919*56731Sbostic 		w = xa > xa0 ? *--xa : 0;
920*56731Sbostic 		d1 = y << (32-Ebits) + k | w >> Ebits - k;
921*56731Sbostic 		goto ret_d;
922*56731Sbostic 		}
923*56731Sbostic 	z = xa > xa0 ? *--xa : 0;
924*56731Sbostic 	if (k -= Ebits) {
925*56731Sbostic 		d0 = Exp_1 | y << k | z >> 32 - k;
926*56731Sbostic 		y = xa > xa0 ? *--xa : 0;
927*56731Sbostic 		d1 = z << k | y >> 32 - k;
928*56731Sbostic 	} else {
929*56731Sbostic 		d0 = Exp_1 | y;
930*56731Sbostic 		d1 = z;
931*56731Sbostic 	}
932*56731Sbostic #else
933*56731Sbostic 	if (k < Ebits + 16) {
934*56731Sbostic 		z = xa > xa0 ? *--xa : 0;
935*56731Sbostic 		d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
936*56731Sbostic 		w = xa > xa0 ? *--xa : 0;
937*56731Sbostic 		y = xa > xa0 ? *--xa : 0;
938*56731Sbostic 		d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
939*56731Sbostic 		goto ret_d;
940*56731Sbostic 	}
941*56731Sbostic 	z = xa > xa0 ? *--xa : 0;
942*56731Sbostic 	w = xa > xa0 ? *--xa : 0;
943*56731Sbostic 	k -= Ebits + 16;
944*56731Sbostic 	d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
945*56731Sbostic 	y = xa > xa0 ? *--xa : 0;
946*56731Sbostic 	d1 = w << k + 16 | y << k;
947*56731Sbostic #endif
948*56731Sbostic  ret_d:
949*56731Sbostic #ifdef VAX
950*56731Sbostic 	word0(d) = d0 >> 16 | d0 << 16;
951*56731Sbostic 	word1(d) = d1 >> 16 | d1 << 16;
952*56731Sbostic #else
953*56731Sbostic #undef d0
954*56731Sbostic #undef d1
955*56731Sbostic #endif
956*56731Sbostic 	return d;
957*56731Sbostic }
958*56731Sbostic 
959*56731Sbostic  static Bigint *
960*56731Sbostic d2b
961*56731Sbostic #ifdef KR_headers
962*56731Sbostic 	(d, e, bits) double d; int *e, *bits;
963*56731Sbostic #else
964*56731Sbostic 	(double d, int *e, int *bits)
965*56731Sbostic #endif
966*56731Sbostic {
967*56731Sbostic 	Bigint *b;
968*56731Sbostic 	int de, i, k;
969*56731Sbostic 	unsigned long *x, y, z;
970*56731Sbostic #ifdef VAX
971*56731Sbostic 	unsigned long d0, d1;
972*56731Sbostic 	d0 = word0(d) >> 16 | word0(d) << 16;
973*56731Sbostic 	d1 = word1(d) >> 16 | word1(d) << 16;
974*56731Sbostic #else
975*56731Sbostic #define d0 word0(d)
976*56731Sbostic #define d1 word1(d)
977*56731Sbostic #endif
978*56731Sbostic 
979*56731Sbostic #ifdef Pack_32
980*56731Sbostic 	b = Balloc(1);
981*56731Sbostic #else
982*56731Sbostic 	b = Balloc(2);
983*56731Sbostic #endif
984*56731Sbostic 	x = b->x;
985*56731Sbostic 
986*56731Sbostic 	z = d0 & Frac_mask;
987*56731Sbostic 	d0 &= 0x7fffffff;	/* clear sign bit, which we ignore */
988*56731Sbostic #ifdef Sudden_Underflow
989*56731Sbostic 	de = (int)(d0 >> Exp_shift);
990*56731Sbostic #ifndef IBM
991*56731Sbostic 	z |= Exp_msk11;
992*56731Sbostic #endif
993*56731Sbostic #else
994*56731Sbostic 	if (de = (int)(d0 >> Exp_shift))
995*56731Sbostic 		z |= Exp_msk1;
996*56731Sbostic #endif
997*56731Sbostic #ifdef Pack_32
998*56731Sbostic 	if (y = d1) {
999*56731Sbostic 		if (k = lo0bits(&y)) {
1000*56731Sbostic 			x[0] = y | z << 32 - k;
1001*56731Sbostic 			z >>= k;
1002*56731Sbostic 			}
1003*56731Sbostic 		else
1004*56731Sbostic 			x[0] = y;
1005*56731Sbostic 		i = b->wds = (x[1] = z) ? 2 : 1;
1006*56731Sbostic 	} else {
1007*56731Sbostic #ifdef DEBUG
1008*56731Sbostic 		if (!z)
1009*56731Sbostic 			Bug("Zero passed to d2b");
1010*56731Sbostic #endif
1011*56731Sbostic 		k = lo0bits(&z);
1012*56731Sbostic 		x[0] = z;
1013*56731Sbostic 		i = b->wds = 1;
1014*56731Sbostic 		k += 32;
1015*56731Sbostic 	}
1016*56731Sbostic #else
1017*56731Sbostic 	if (y = d1) {
1018*56731Sbostic 		if (k = lo0bits(&y))
1019*56731Sbostic 			if (k >= 16) {
1020*56731Sbostic 				x[0] = y | z << 32 - k & 0xffff;
1021*56731Sbostic 				x[1] = z >> k - 16 & 0xffff;
1022*56731Sbostic 				x[2] = z >> k;
1023*56731Sbostic 				i = 2;
1024*56731Sbostic 			} else {
1025*56731Sbostic 				x[0] = y & 0xffff;
1026*56731Sbostic 				x[1] = y >> 16 | z << 16 - k & 0xffff;
1027*56731Sbostic 				x[2] = z >> k & 0xffff;
1028*56731Sbostic 				x[3] = z >> k+16;
1029*56731Sbostic 				i = 3;
1030*56731Sbostic 			}
1031*56731Sbostic 		else {
1032*56731Sbostic 			x[0] = y & 0xffff;
1033*56731Sbostic 			x[1] = y >> 16;
1034*56731Sbostic 			x[2] = z & 0xffff;
1035*56731Sbostic 			x[3] = z >> 16;
1036*56731Sbostic 			i = 3;
1037*56731Sbostic 		}
1038*56731Sbostic 	} else {
1039*56731Sbostic #ifdef DEBUG
1040*56731Sbostic 		if (!z)
1041*56731Sbostic 			Bug("Zero passed to d2b");
1042*56731Sbostic #endif
1043*56731Sbostic 		k = lo0bits(&z);
1044*56731Sbostic 		if (k >= 16) {
1045*56731Sbostic 			x[0] = z;
1046*56731Sbostic 			i = 0;
1047*56731Sbostic 		} else {
1048*56731Sbostic 			x[0] = z & 0xffff;
1049*56731Sbostic 			x[1] = z >> 16;
1050*56731Sbostic 			i = 1;
1051*56731Sbostic 		}
1052*56731Sbostic 		k += 32;
1053*56731Sbostic 	}
1054*56731Sbostic 	while (!x[i])
1055*56731Sbostic 		--i;
1056*56731Sbostic 	b->wds = i + 1;
1057*56731Sbostic #endif
1058*56731Sbostic #ifndef Sudden_Underflow
1059*56731Sbostic 	if (de) {
1060*56731Sbostic #endif
1061*56731Sbostic #ifdef IBM
1062*56731Sbostic 		*e = (de - Bias - (P-1) << 2) + k;
1063*56731Sbostic 		*bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1064*56731Sbostic #else
1065*56731Sbostic 		*e = de - Bias - (P-1) + k;
1066*56731Sbostic 		*bits = P - k;
1067*56731Sbostic #endif
1068*56731Sbostic #ifndef Sudden_Underflow
1069*56731Sbostic 	} else {
1070*56731Sbostic 		*e = de - Bias - (P-1) + 1 + k;
1071*56731Sbostic #ifdef Pack_32
1072*56731Sbostic 		*bits = 32*i - hi0bits(x[i-1]);
1073*56731Sbostic #else
1074*56731Sbostic 		*bits = (i+2)*16 - hi0bits(x[i]);
1075*56731Sbostic #endif
1076*56731Sbostic 	}
1077*56731Sbostic #endif
1078*56731Sbostic 	return b;
1079*56731Sbostic }
1080*56731Sbostic #undef d0
1081*56731Sbostic #undef d1
1082*56731Sbostic 
1083*56731Sbostic  static double
1084*56731Sbostic ratio
1085*56731Sbostic #ifdef KR_headers
1086*56731Sbostic 	(a, b) Bigint *a, *b;
1087*56731Sbostic #else
1088*56731Sbostic 	(Bigint *a, Bigint *b)
1089*56731Sbostic #endif
1090*56731Sbostic {
1091*56731Sbostic 	double da, db;
1092*56731Sbostic 	int k, ka, kb;
1093*56731Sbostic 
1094*56731Sbostic 	da = b2d(a, &ka);
1095*56731Sbostic 	db = b2d(b, &kb);
1096*56731Sbostic #ifdef Pack_32
1097*56731Sbostic 	k = ka - kb + 32*(a->wds - b->wds);
1098*56731Sbostic #else
1099*56731Sbostic 	k = ka - kb + 16*(a->wds - b->wds);
1100*56731Sbostic #endif
1101*56731Sbostic #ifdef IBM
1102*56731Sbostic 	if (k > 0) {
1103*56731Sbostic 		word0(da) += (k >> 2)*Exp_msk1;
1104*56731Sbostic 		if (k &= 3)
1105*56731Sbostic 			da *= 1 << k;
1106*56731Sbostic 	} else {
1107*56731Sbostic 		k = -k;
1108*56731Sbostic 		word0(db) += (k >> 2)*Exp_msk1;
1109*56731Sbostic 		if (k &= 3)
1110*56731Sbostic 			db *= 1 << k;
1111*56731Sbostic 	}
1112*56731Sbostic #else
1113*56731Sbostic 	if (k > 0)
1114*56731Sbostic 		word0(da) += k*Exp_msk1;
1115*56731Sbostic 	else {
1116*56731Sbostic 		k = -k;
1117*56731Sbostic 		word0(db) += k*Exp_msk1;
1118*56731Sbostic 	}
1119*56731Sbostic #endif
1120*56731Sbostic 	return da / db;
1121*56731Sbostic }
1122*56731Sbostic 
1123*56731Sbostic  static double
1124*56731Sbostic tens[] = {
1125*56731Sbostic 		1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1126*56731Sbostic 		1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1127*56731Sbostic 		1e20, 1e21, 1e22
1128*56731Sbostic #ifdef VAX
1129*56731Sbostic 		, 1e23, 1e24
1130*56731Sbostic #endif
1131*56731Sbostic 		};
1132*56731Sbostic 
1133*56731Sbostic  static double
1134*56731Sbostic #ifdef IEEE_Arith
1135*56731Sbostic bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1136*56731Sbostic static double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1137*56731Sbostic #define n_bigtens 5
1138*56731Sbostic #else
1139*56731Sbostic #ifdef IBM
1140*56731Sbostic bigtens[] = { 1e16, 1e32, 1e64 };
1141*56731Sbostic static double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1142*56731Sbostic #define n_bigtens 3
1143*56731Sbostic #else
1144*56731Sbostic bigtens[] = { 1e16, 1e32 };
1145*56731Sbostic static double tinytens[] = { 1e-16, 1e-32 };
1146*56731Sbostic #define n_bigtens 2
1147*56731Sbostic #endif
1148*56731Sbostic #endif
1149*56731Sbostic 
1150*56731Sbostic  double
1151*56731Sbostic strtod
1152*56731Sbostic #ifdef KR_headers
1153*56731Sbostic 	(s00, se) CONST char *s00; char **se;
1154*56731Sbostic #else
1155*56731Sbostic 	(CONST char *s00, char **se)
1156*56731Sbostic #endif
1157*56731Sbostic {
1158*56731Sbostic 	int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1159*56731Sbostic 		 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1160*56731Sbostic 	CONST char *s, *s0, *s1;
1161*56731Sbostic 	double aadj, aadj1, adj, rv, rv0;
1162*56731Sbostic 	long L;
1163*56731Sbostic 	unsigned long y, z;
1164*56731Sbostic 	Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1165*56731Sbostic 	sign = nz0 = nz = 0;
1166*56731Sbostic 	rv = 0.;
1167*56731Sbostic 	for (s = s00;;s++) switch(*s) {
1168*56731Sbostic 		case '-':
1169*56731Sbostic 			sign = 1;
1170*56731Sbostic 			/* no break */
1171*56731Sbostic 		case '+':
1172*56731Sbostic 			if (*++s)
1173*56731Sbostic 				goto break2;
1174*56731Sbostic 			/* no break */
1175*56731Sbostic 		case 0:
1176*56731Sbostic 			s = s00;
1177*56731Sbostic 			goto ret;
1178*56731Sbostic 		case '\t':
1179*56731Sbostic 		case '\n':
1180*56731Sbostic 		case '\v':
1181*56731Sbostic 		case '\f':
1182*56731Sbostic 		case '\r':
1183*56731Sbostic 		case ' ':
1184*56731Sbostic 			continue;
1185*56731Sbostic 		default:
1186*56731Sbostic 			goto break2;
1187*56731Sbostic 	}
1188*56731Sbostic  break2:
1189*56731Sbostic 	if (*s == '0') {
1190*56731Sbostic 		nz0 = 1;
1191*56731Sbostic 		while (*++s == '0') ;
1192*56731Sbostic 		if (!*s)
1193*56731Sbostic 			goto ret;
1194*56731Sbostic 	}
1195*56731Sbostic 	s0 = s;
1196*56731Sbostic 	y = z = 0;
1197*56731Sbostic 	for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1198*56731Sbostic 		if (nd < 9)
1199*56731Sbostic 			y = 10*y + c - '0';
1200*56731Sbostic 		else if (nd < 16)
1201*56731Sbostic 			z = 10*z + c - '0';
1202*56731Sbostic 	nd0 = nd;
1203*56731Sbostic 	if (c == '.') {
1204*56731Sbostic 		c = *++s;
1205*56731Sbostic 		if (!nd) {
1206*56731Sbostic 			for (; c == '0'; c = *++s)
1207*56731Sbostic 				nz++;
1208*56731Sbostic 			if (c > '0' && c <= '9') {
1209*56731Sbostic 				s0 = s;
1210*56731Sbostic 				nf += nz;
1211*56731Sbostic 				nz = 0;
1212*56731Sbostic 				goto have_dig;
1213*56731Sbostic 			}
1214*56731Sbostic 			goto dig_done;
1215*56731Sbostic 		}
1216*56731Sbostic 		for (; c >= '0' && c <= '9'; c = *++s) {
1217*56731Sbostic  have_dig:
1218*56731Sbostic 			nz++;
1219*56731Sbostic 			if (c -= '0') {
1220*56731Sbostic 				nf += nz;
1221*56731Sbostic 				for (i = 1; i < nz; i++)
1222*56731Sbostic 					if (nd++ < 9)
1223*56731Sbostic 						y *= 10;
1224*56731Sbostic 					else if (nd <= DBL_DIG + 1)
1225*56731Sbostic 						z *= 10;
1226*56731Sbostic 				if (nd++ < 9)
1227*56731Sbostic 					y = 10*y + c;
1228*56731Sbostic 				else if (nd <= DBL_DIG + 1)
1229*56731Sbostic 					z = 10*z + c;
1230*56731Sbostic 				nz = 0;
1231*56731Sbostic 			}
1232*56731Sbostic 		}
1233*56731Sbostic 	}
1234*56731Sbostic  dig_done:
1235*56731Sbostic 	e = 0;
1236*56731Sbostic 	if (c == 'e' || c == 'E') {
1237*56731Sbostic 		if (!nd && !nz && !nz0) {
1238*56731Sbostic 			s = s00;
1239*56731Sbostic 			goto ret;
1240*56731Sbostic 		}
1241*56731Sbostic 		s00 = s;
1242*56731Sbostic 		esign = 0;
1243*56731Sbostic 		switch(c = *++s) {
1244*56731Sbostic 			case '-':
1245*56731Sbostic 				esign = 1;
1246*56731Sbostic 			case '+':
1247*56731Sbostic 				c = *++s;
1248*56731Sbostic 		}
1249*56731Sbostic 		if (c >= '0' && c <= '9') {
1250*56731Sbostic 			while (c == '0')
1251*56731Sbostic 				c = *++s;
1252*56731Sbostic 			if (c > '0' && c <= '9') {
1253*56731Sbostic 				L = c - '0';
1254*56731Sbostic 				s1 = s;
1255*56731Sbostic 				while ((c = *++s) >= '0' && c <= '9')
1256*56731Sbostic 					L = 10*L + c - '0';
1257*56731Sbostic 				if (s - s1 > 8 || L > 19999)
1258*56731Sbostic 					/* Avoid confusion from exponents
1259*56731Sbostic 					 * so large that e might overflow.
1260*56731Sbostic 					 */
1261*56731Sbostic 					e = 19999; /* safe for 16 bit ints */
1262*56731Sbostic 				else
1263*56731Sbostic 					e = (int)L;
1264*56731Sbostic 				if (esign)
1265*56731Sbostic 					e = -e;
1266*56731Sbostic 			} else
1267*56731Sbostic 				e = 0;
1268*56731Sbostic 		} else
1269*56731Sbostic 			s = s00;
1270*56731Sbostic 	}
1271*56731Sbostic 	if (!nd) {
1272*56731Sbostic 		if (!nz && !nz0)
1273*56731Sbostic 			s = s00;
1274*56731Sbostic 		goto ret;
1275*56731Sbostic 	}
1276*56731Sbostic 	e1 = e -= nf;
1277*56731Sbostic 
1278*56731Sbostic 	/* Now we have nd0 digits, starting at s0, followed by a
1279*56731Sbostic 	 * decimal point, followed by nd-nd0 digits.  The number we're
1280*56731Sbostic 	 * after is the integer represented by those digits times
1281*56731Sbostic 	 * 10**e */
1282*56731Sbostic 
1283*56731Sbostic 	if (!nd0)
1284*56731Sbostic 		nd0 = nd;
1285*56731Sbostic 	k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1286*56731Sbostic 	rv = y;
1287*56731Sbostic 	if (k > 9)
1288*56731Sbostic 		rv = tens[k - 9] * rv + z;
1289*56731Sbostic 	if (nd <= DBL_DIG
1290*56731Sbostic #ifndef RND_PRODQUOT
1291*56731Sbostic 		&& FLT_ROUNDS == 1
1292*56731Sbostic #endif
1293*56731Sbostic 			) {
1294*56731Sbostic 		if (!e)
1295*56731Sbostic 			goto ret;
1296*56731Sbostic 		if (e > 0) {
1297*56731Sbostic 			if (e <= Ten_pmax) {
1298*56731Sbostic #ifdef VAX
1299*56731Sbostic 				goto vax_ovfl_check;
1300*56731Sbostic #else
1301*56731Sbostic 				/* rv = */ rounded_product(rv, tens[e]);
1302*56731Sbostic 				goto ret;
1303*56731Sbostic #endif
1304*56731Sbostic 				}
1305*56731Sbostic 			i = DBL_DIG - nd;
1306*56731Sbostic 			if (e <= Ten_pmax + i) {
1307*56731Sbostic 				/* A fancier test would sometimes let us do
1308*56731Sbostic 				 * this for larger i values.
1309*56731Sbostic 				 */
1310*56731Sbostic 				e -= i;
1311*56731Sbostic 				rv *= tens[i];
1312*56731Sbostic #ifdef VAX
1313*56731Sbostic 				/* VAX exponent range is so narrow we must
1314*56731Sbostic 				 * worry about overflow here...
1315*56731Sbostic 				 */
1316*56731Sbostic  vax_ovfl_check:
1317*56731Sbostic 				word0(rv) -= P*Exp_msk1;
1318*56731Sbostic 				/* rv = */ rounded_product(rv, tens[e]);
1319*56731Sbostic 				if ((word0(rv) & Exp_mask)
1320*56731Sbostic 				 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1321*56731Sbostic 					goto ovfl;
1322*56731Sbostic 				word0(rv) += P*Exp_msk1;
1323*56731Sbostic #else
1324*56731Sbostic 				/* rv = */ rounded_product(rv, tens[e]);
1325*56731Sbostic #endif
1326*56731Sbostic 				goto ret;
1327*56731Sbostic 			}
1328*56731Sbostic 		}
1329*56731Sbostic #ifndef Inaccurate_Divide
1330*56731Sbostic 		else if (e >= -Ten_pmax) {
1331*56731Sbostic 			/* rv = */ rounded_quotient(rv, tens[-e]);
1332*56731Sbostic 			goto ret;
1333*56731Sbostic 		}
1334*56731Sbostic #endif
1335*56731Sbostic 	}
1336*56731Sbostic 	e1 += nd - k;
1337*56731Sbostic 
1338*56731Sbostic 	/* Get starting approximation = rv * 10**e1 */
1339*56731Sbostic 
1340*56731Sbostic 	if (e1 > 0) {
1341*56731Sbostic 		if (i = e1 & 15)
1342*56731Sbostic 			rv *= tens[i];
1343*56731Sbostic 		if (e1 &= ~15) {
1344*56731Sbostic 			if (e1 > DBL_MAX_10_EXP) {
1345*56731Sbostic  ovfl:
1346*56731Sbostic 				errno = ERANGE;
1347*56731Sbostic #ifdef __STDC__
1348*56731Sbostic 				rv = HUGE_VAL;
1349*56731Sbostic #else
1350*56731Sbostic 				/* Can't trust HUGE_VAL */
1351*56731Sbostic #ifdef IEEE_Arith
1352*56731Sbostic 				word0(rv) = Exp_mask;
1353*56731Sbostic 				word1(rv) = 0;
1354*56731Sbostic #else
1355*56731Sbostic 				word0(rv) = Big0;
1356*56731Sbostic 				word1(rv) = Big1;
1357*56731Sbostic #endif
1358*56731Sbostic #endif
1359*56731Sbostic 				goto ret;
1360*56731Sbostic 			}
1361*56731Sbostic 			if (e1 >>= 4) {
1362*56731Sbostic 				for (j = 0; e1 > 1; j++, e1 >>= 1)
1363*56731Sbostic 					if (e1 & 1)
1364*56731Sbostic 						rv *= bigtens[j];
1365*56731Sbostic 			/* The last multiplication could overflow. */
1366*56731Sbostic 				word0(rv) -= P*Exp_msk1;
1367*56731Sbostic 				rv *= bigtens[j];
1368*56731Sbostic 				if ((z = word0(rv) & Exp_mask)
1369*56731Sbostic 				 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1370*56731Sbostic 					goto ovfl;
1371*56731Sbostic 				if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1372*56731Sbostic 					/* set to largest number */
1373*56731Sbostic 					/* (Can't trust DBL_MAX) */
1374*56731Sbostic 					word0(rv) = Big0;
1375*56731Sbostic 					word1(rv) = Big1;
1376*56731Sbostic 					}
1377*56731Sbostic 				else
1378*56731Sbostic 					word0(rv) += P*Exp_msk1;
1379*56731Sbostic 			}
1380*56731Sbostic 		}
1381*56731Sbostic 	} else if (e1 < 0) {
1382*56731Sbostic 		e1 = -e1;
1383*56731Sbostic 		if (i = e1 & 15)
1384*56731Sbostic 			rv /= tens[i];
1385*56731Sbostic 		if (e1 &= ~15) {
1386*56731Sbostic 			e1 >>= 4;
1387*56731Sbostic 			for (j = 0; e1 > 1; j++, e1 >>= 1)
1388*56731Sbostic 				if (e1 & 1)
1389*56731Sbostic 					rv *= tinytens[j];
1390*56731Sbostic 			/* The last multiplication could underflow. */
1391*56731Sbostic 			rv0 = rv;
1392*56731Sbostic 			rv *= tinytens[j];
1393*56731Sbostic 			if (!rv) {
1394*56731Sbostic 				rv = 2.*rv0;
1395*56731Sbostic 				rv *= tinytens[j];
1396*56731Sbostic 				if (!rv) {
1397*56731Sbostic  undfl:
1398*56731Sbostic 					rv = 0.;
1399*56731Sbostic 					errno = ERANGE;
1400*56731Sbostic 					goto ret;
1401*56731Sbostic 					}
1402*56731Sbostic 				word0(rv) = Tiny0;
1403*56731Sbostic 				word1(rv) = Tiny1;
1404*56731Sbostic 				/* The refinement below will clean
1405*56731Sbostic 				 * this approximation up.
1406*56731Sbostic 				 */
1407*56731Sbostic 			}
1408*56731Sbostic 		}
1409*56731Sbostic 	}
1410*56731Sbostic 
1411*56731Sbostic 	/* Now the hard part -- adjusting rv to the correct value.*/
1412*56731Sbostic 
1413*56731Sbostic 	/* Put digits into bd: true value = bd * 10^e */
1414*56731Sbostic 
1415*56731Sbostic 	bd0 = s2b(s0, nd0, nd, y);
1416*56731Sbostic 
1417*56731Sbostic 	for (;;) {
1418*56731Sbostic 		bd = Balloc(bd0->k);
1419*56731Sbostic 		Bcopy(bd, bd0);
1420*56731Sbostic 		bb = d2b(rv, &bbe, &bbbits);	/* rv = bb * 2^bbe */
1421*56731Sbostic 		bs = i2b(1);
1422*56731Sbostic 
1423*56731Sbostic 		if (e >= 0) {
1424*56731Sbostic 			bb2 = bb5 = 0;
1425*56731Sbostic 			bd2 = bd5 = e;
1426*56731Sbostic 		} else {
1427*56731Sbostic 			bb2 = bb5 = -e;
1428*56731Sbostic 			bd2 = bd5 = 0;
1429*56731Sbostic 		}
1430*56731Sbostic 		if (bbe >= 0)
1431*56731Sbostic 			bb2 += bbe;
1432*56731Sbostic 		else
1433*56731Sbostic 			bd2 -= bbe;
1434*56731Sbostic 		bs2 = bb2;
1435*56731Sbostic #ifdef Sudden_Underflow
1436*56731Sbostic #ifdef IBM
1437*56731Sbostic 		j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1438*56731Sbostic #else
1439*56731Sbostic 		j = P + 1 - bbbits;
1440*56731Sbostic #endif
1441*56731Sbostic #else
1442*56731Sbostic 		i = bbe + bbbits - 1;	/* logb(rv) */
1443*56731Sbostic 		if (i < Emin)	/* denormal */
1444*56731Sbostic 			j = bbe + (P-Emin);
1445*56731Sbostic 		else
1446*56731Sbostic 			j = P + 1 - bbbits;
1447*56731Sbostic #endif
1448*56731Sbostic 		bb2 += j;
1449*56731Sbostic 		bd2 += j;
1450*56731Sbostic 		i = bb2 < bd2 ? bb2 : bd2;
1451*56731Sbostic 		if (i > bs2)
1452*56731Sbostic 			i = bs2;
1453*56731Sbostic 		if (i > 0) {
1454*56731Sbostic 			bb2 -= i;
1455*56731Sbostic 			bd2 -= i;
1456*56731Sbostic 			bs2 -= i;
1457*56731Sbostic 			}
1458*56731Sbostic 		if (bb5 > 0) {
1459*56731Sbostic 			bs = pow5mult(bs, bb5);
1460*56731Sbostic 			bb1 = mult(bs, bb);
1461*56731Sbostic 			Bfree(bb);
1462*56731Sbostic 			bb = bb1;
1463*56731Sbostic 			}
1464*56731Sbostic 		if (bb2 > 0)
1465*56731Sbostic 			bb = lshift(bb, bb2);
1466*56731Sbostic 		if (bd5 > 0)
1467*56731Sbostic 			bd = pow5mult(bd, bd5);
1468*56731Sbostic 		if (bd2 > 0)
1469*56731Sbostic 			bd = lshift(bd, bd2);
1470*56731Sbostic 		if (bs2 > 0)
1471*56731Sbostic 			bs = lshift(bs, bs2);
1472*56731Sbostic 		delta = diff(bb, bd);
1473*56731Sbostic 		dsign = delta->sign;
1474*56731Sbostic 		delta->sign = 0;
1475*56731Sbostic 		i = cmp(delta, bs);
1476*56731Sbostic 		if (i < 0) {
1477*56731Sbostic 			/* Error is less than half an ulp -- check for
1478*56731Sbostic 			 * special case of mantissa a power of two.
1479*56731Sbostic 			 */
1480*56731Sbostic 			if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1481*56731Sbostic 				break;
1482*56731Sbostic 			delta = lshift(delta,Log2P);
1483*56731Sbostic 			if (cmp(delta, bs) > 0)
1484*56731Sbostic 				goto drop_down;
1485*56731Sbostic 			break;
1486*56731Sbostic 		}
1487*56731Sbostic 		if (i == 0) {
1488*56731Sbostic 			/* exactly half-way between */
1489*56731Sbostic 			if (dsign) {
1490*56731Sbostic 				if ((word0(rv) & Bndry_mask1) == Bndry_mask1
1491*56731Sbostic 				 &&  word1(rv) == 0xffffffff) {
1492*56731Sbostic 					/*boundary case -- increment exponent*/
1493*56731Sbostic 					word0(rv) = (word0(rv) & Exp_mask)
1494*56731Sbostic 						+ Exp_msk1
1495*56731Sbostic #ifdef IBM
1496*56731Sbostic 						| Exp_msk1 >> 4
1497*56731Sbostic #endif
1498*56731Sbostic 						;
1499*56731Sbostic 					word1(rv) = 0;
1500*56731Sbostic 					break;
1501*56731Sbostic 				}
1502*56731Sbostic 			} else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
1503*56731Sbostic  drop_down:
1504*56731Sbostic 				/* boundary case -- decrement exponent */
1505*56731Sbostic #ifdef Sudden_Underflow
1506*56731Sbostic 				L = word0(rv) & Exp_mask;
1507*56731Sbostic #ifdef IBM
1508*56731Sbostic 				if (L <  Exp_msk1)
1509*56731Sbostic #else
1510*56731Sbostic 				if (L <= Exp_msk1)
1511*56731Sbostic #endif
1512*56731Sbostic 					goto undfl;
1513*56731Sbostic 				L -= Exp_msk1;
1514*56731Sbostic #else
1515*56731Sbostic 				L = (word0(rv) & Exp_mask) - Exp_msk1;
1516*56731Sbostic #endif
1517*56731Sbostic 				word0(rv) = L | Bndry_mask1;
1518*56731Sbostic 				word1(rv) = 0xffffffff;
1519*56731Sbostic #ifdef IBM
1520*56731Sbostic 				goto cont;
1521*56731Sbostic #else
1522*56731Sbostic 				break;
1523*56731Sbostic #endif
1524*56731Sbostic 			}
1525*56731Sbostic #ifndef ROUND_BIASED
1526*56731Sbostic 			if (!(word1(rv) & LSB))
1527*56731Sbostic 				break;
1528*56731Sbostic #endif
1529*56731Sbostic 			if (dsign)
1530*56731Sbostic 				rv += ulp(rv);
1531*56731Sbostic #ifndef ROUND_BIASED
1532*56731Sbostic 			else {
1533*56731Sbostic 				rv -= ulp(rv);
1534*56731Sbostic #ifndef Sudden_Underflow
1535*56731Sbostic 				if (!rv)
1536*56731Sbostic 					goto undfl;
1537*56731Sbostic #endif
1538*56731Sbostic 			}
1539*56731Sbostic #endif
1540*56731Sbostic 			break;
1541*56731Sbostic 		}
1542*56731Sbostic 		if ((aadj = ratio(delta, bs)) <= 2.) {
1543*56731Sbostic 			if (dsign)
1544*56731Sbostic 				aadj = aadj1 = 1.;
1545*56731Sbostic 			else if (word1(rv) || word0(rv) & Bndry_mask) {
1546*56731Sbostic #ifndef Sudden_Underflow
1547*56731Sbostic 				if (word1(rv) == Tiny1 && !word0(rv))
1548*56731Sbostic 					goto undfl;
1549*56731Sbostic #endif
1550*56731Sbostic 				aadj = 1.;
1551*56731Sbostic 				aadj1 = -1.;
1552*56731Sbostic 			} else {
1553*56731Sbostic 				/* special case -- power of FLT_RADIX to be */
1554*56731Sbostic 				/* rounded down... */
1555*56731Sbostic 
1556*56731Sbostic 				if (aadj < 2./FLT_RADIX)
1557*56731Sbostic 					aadj = 1./FLT_RADIX;
1558*56731Sbostic 				else
1559*56731Sbostic 					aadj *= 0.5;
1560*56731Sbostic 				aadj1 = -aadj;
1561*56731Sbostic 			}
1562*56731Sbostic 		} else {
1563*56731Sbostic 			aadj *= 0.5;
1564*56731Sbostic 			aadj1 = dsign ? aadj : -aadj;
1565*56731Sbostic #ifdef Check_FLT_ROUNDS
1566*56731Sbostic 			switch(FLT_ROUNDS) {
1567*56731Sbostic 				case 2: /* towards +infinity */
1568*56731Sbostic 					aadj1 -= 0.5;
1569*56731Sbostic 					break;
1570*56731Sbostic 				case 0: /* towards 0 */
1571*56731Sbostic 				case 3: /* towards -infinity */
1572*56731Sbostic 					aadj1 += 0.5;
1573*56731Sbostic 			}
1574*56731Sbostic #else
1575*56731Sbostic 			if (FLT_ROUNDS == 0)
1576*56731Sbostic 				aadj1 += 0.5;
1577*56731Sbostic #endif
1578*56731Sbostic 		}
1579*56731Sbostic 		y = word0(rv) & Exp_mask;
1580*56731Sbostic 
1581*56731Sbostic 		/* Check for overflow */
1582*56731Sbostic 
1583*56731Sbostic 		if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1584*56731Sbostic 			rv0 = rv;
1585*56731Sbostic 			word0(rv) -= P*Exp_msk1;
1586*56731Sbostic 			adj = aadj1 * ulp(rv);
1587*56731Sbostic 			rv += adj;
1588*56731Sbostic 			if ((word0(rv) & Exp_mask) >=
1589*56731Sbostic 					Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1590*56731Sbostic 				if (word0(rv0) == Big0 && word1(rv0) == Big1)
1591*56731Sbostic 					goto ovfl;
1592*56731Sbostic 				word0(rv) = Big0;
1593*56731Sbostic 				word1(rv) = Big1;
1594*56731Sbostic 				goto cont;
1595*56731Sbostic 			} else
1596*56731Sbostic 				word0(rv) += P*Exp_msk1;
1597*56731Sbostic 		} else {
1598*56731Sbostic #ifdef Sudden_Underflow
1599*56731Sbostic 			if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1600*56731Sbostic 				rv0 = rv;
1601*56731Sbostic 				word0(rv) += P*Exp_msk1;
1602*56731Sbostic 				adj = aadj1 * ulp(rv);
1603*56731Sbostic 				rv += adj;
1604*56731Sbostic #ifdef IBM
1605*56731Sbostic 				if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
1606*56731Sbostic #else
1607*56731Sbostic 				if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
1608*56731Sbostic #endif
1609*56731Sbostic 				{
1610*56731Sbostic 					if (word0(rv0) == Tiny0
1611*56731Sbostic 					 && word1(rv0) == Tiny1)
1612*56731Sbostic 						goto undfl;
1613*56731Sbostic 					word0(rv) = Tiny0;
1614*56731Sbostic 					word1(rv) = Tiny1;
1615*56731Sbostic 					goto cont;
1616*56731Sbostic 				} else
1617*56731Sbostic 					word0(rv) -= P*Exp_msk1;
1618*56731Sbostic 			} else {
1619*56731Sbostic 				adj = aadj1 * ulp(rv);
1620*56731Sbostic 				rv += adj;
1621*56731Sbostic 			}
1622*56731Sbostic #else
1623*56731Sbostic 			/* Compute adj so that the IEEE rounding rules will
1624*56731Sbostic 			 * correctly round rv + adj in some half-way cases.
1625*56731Sbostic 			 * If rv * ulp(rv) is denormalized (i.e.,
1626*56731Sbostic 			 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1627*56731Sbostic 			 * trouble from bits lost to denormalization;
1628*56731Sbostic 			 * example: 1.2e-307 .
1629*56731Sbostic 			 */
1630*56731Sbostic 			if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
1631*56731Sbostic 				aadj1 = (double)(int)(aadj + 0.5);
1632*56731Sbostic 				if (!dsign)
1633*56731Sbostic 					aadj1 = -aadj1;
1634*56731Sbostic 			}
1635*56731Sbostic 			adj = aadj1 * ulp(rv);
1636*56731Sbostic 			rv += adj;
1637*56731Sbostic #endif
1638*56731Sbostic 		}
1639*56731Sbostic 		z = word0(rv) & Exp_mask;
1640*56731Sbostic 		if (y == z) {
1641*56731Sbostic 			/* Can we stop now? */
1642*56731Sbostic 			L = aadj;
1643*56731Sbostic 			aadj -= L;
1644*56731Sbostic 			/* The tolerances below are conservative. */
1645*56731Sbostic 			if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
1646*56731Sbostic 				if (aadj < .4999999 || aadj > .5000001)
1647*56731Sbostic 					break;
1648*56731Sbostic 			} else if (aadj < .4999999/FLT_RADIX)
1649*56731Sbostic 				break;
1650*56731Sbostic 		}
1651*56731Sbostic  cont:
1652*56731Sbostic 		Bfree(bb);
1653*56731Sbostic 		Bfree(bd);
1654*56731Sbostic 		Bfree(bs);
1655*56731Sbostic 		Bfree(delta);
1656*56731Sbostic 	}
1657*56731Sbostic 	Bfree(bb);
1658*56731Sbostic 	Bfree(bd);
1659*56731Sbostic 	Bfree(bs);
1660*56731Sbostic 	Bfree(bd0);
1661*56731Sbostic 	Bfree(delta);
1662*56731Sbostic  ret:
1663*56731Sbostic 	if (se)
1664*56731Sbostic 		*se = (char *)s;
1665*56731Sbostic 	return sign ? -rv : rv;
1666*56731Sbostic }
1667*56731Sbostic 
1668*56731Sbostic  static int
1669*56731Sbostic quorem
1670*56731Sbostic #ifdef KR_headers
1671*56731Sbostic 	(b, S) Bigint *b, *S;
1672*56731Sbostic #else
1673*56731Sbostic 	(Bigint *b, Bigint *S)
1674*56731Sbostic #endif
1675*56731Sbostic {
1676*56731Sbostic 	int n;
1677*56731Sbostic 	long borrow, y;
1678*56731Sbostic 	unsigned long carry, q, ys;
1679*56731Sbostic 	unsigned long *bx, *bxe, *sx, *sxe;
1680*56731Sbostic #ifdef Pack_32
1681*56731Sbostic 	long z;
1682*56731Sbostic 	unsigned long si, zs;
1683*56731Sbostic #endif
1684*56731Sbostic 
1685*56731Sbostic 	n = S->wds;
1686*56731Sbostic #ifdef DEBUG
1687*56731Sbostic 	/*debug*/ if (b->wds > n)
1688*56731Sbostic 	/*debug*/	Bug("oversize b in quorem");
1689*56731Sbostic #endif
1690*56731Sbostic 	if (b->wds < n)
1691*56731Sbostic 		return 0;
1692*56731Sbostic 	sx = S->x;
1693*56731Sbostic 	sxe = sx + --n;
1694*56731Sbostic 	bx = b->x;
1695*56731Sbostic 	bxe = bx + n;
1696*56731Sbostic 	q = *bxe / (*sxe + 1);	/* ensure q <= true quotient */
1697*56731Sbostic #ifdef DEBUG
1698*56731Sbostic 	/*debug*/ if (q > 9)
1699*56731Sbostic 	/*debug*/	Bug("oversized quotient in quorem");
1700*56731Sbostic #endif
1701*56731Sbostic 	if (q) {
1702*56731Sbostic 		borrow = 0;
1703*56731Sbostic 		carry = 0;
1704*56731Sbostic 		do {
1705*56731Sbostic #ifdef Pack_32
1706*56731Sbostic 			si = *sx++;
1707*56731Sbostic 			ys = (si & 0xffff) * q + carry;
1708*56731Sbostic 			zs = (si >> 16) * q + (ys >> 16);
1709*56731Sbostic 			carry = zs >> 16;
1710*56731Sbostic 			y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1711*56731Sbostic 			borrow = y >> 16;
1712*56731Sbostic 			Sign_Extend(borrow, y);
1713*56731Sbostic 			z = (*bx >> 16) - (zs & 0xffff) + borrow;
1714*56731Sbostic 			borrow = z >> 16;
1715*56731Sbostic 			Sign_Extend(borrow, z);
1716*56731Sbostic 			Storeinc(bx, z, y);
1717*56731Sbostic #else
1718*56731Sbostic 			ys = *sx++ * q + carry;
1719*56731Sbostic 			carry = ys >> 16;
1720*56731Sbostic 			y = *bx - (ys & 0xffff) + borrow;
1721*56731Sbostic 			borrow = y >> 16;
1722*56731Sbostic 			Sign_Extend(borrow, y);
1723*56731Sbostic 			*bx++ = y & 0xffff;
1724*56731Sbostic #endif
1725*56731Sbostic 		} while (sx <= sxe);
1726*56731Sbostic 		if (!*bxe) {
1727*56731Sbostic 			bx = b->x;
1728*56731Sbostic 			while (--bxe > bx && !*bxe)
1729*56731Sbostic 				--n;
1730*56731Sbostic 			b->wds = n;
1731*56731Sbostic 		}
1732*56731Sbostic 	}
1733*56731Sbostic 	if (cmp(b, S) >= 0) {
1734*56731Sbostic 		q++;
1735*56731Sbostic 		borrow = 0;
1736*56731Sbostic 		carry = 0;
1737*56731Sbostic 		bx = b->x;
1738*56731Sbostic 		sx = S->x;
1739*56731Sbostic 		do {
1740*56731Sbostic #ifdef Pack_32
1741*56731Sbostic 			si = *sx++;
1742*56731Sbostic 			ys = (si & 0xffff) + carry;
1743*56731Sbostic 			zs = (si >> 16) + (ys >> 16);
1744*56731Sbostic 			carry = zs >> 16;
1745*56731Sbostic 			y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1746*56731Sbostic 			borrow = y >> 16;
1747*56731Sbostic 			Sign_Extend(borrow, y);
1748*56731Sbostic 			z = (*bx >> 16) - (zs & 0xffff) + borrow;
1749*56731Sbostic 			borrow = z >> 16;
1750*56731Sbostic 			Sign_Extend(borrow, z);
1751*56731Sbostic 			Storeinc(bx, z, y);
1752*56731Sbostic #else
1753*56731Sbostic 			ys = *sx++ + carry;
1754*56731Sbostic 			carry = ys >> 16;
1755*56731Sbostic 			y = *bx - (ys & 0xffff) + borrow;
1756*56731Sbostic 			borrow = y >> 16;
1757*56731Sbostic 			Sign_Extend(borrow, y);
1758*56731Sbostic 			*bx++ = y & 0xffff;
1759*56731Sbostic #endif
1760*56731Sbostic 		} while (sx <= sxe);
1761*56731Sbostic 		bx = b->x;
1762*56731Sbostic 		bxe = bx + n;
1763*56731Sbostic 		if (!*bxe) {
1764*56731Sbostic 			while (--bxe > bx && !*bxe)
1765*56731Sbostic 				--n;
1766*56731Sbostic 			b->wds = n;
1767*56731Sbostic 		}
1768*56731Sbostic 	}
1769*56731Sbostic 	return q;
1770*56731Sbostic }
1771*56731Sbostic 
1772*56731Sbostic /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1773*56731Sbostic  *
1774*56731Sbostic  * Inspired by "How to Print Floating-Point Numbers Accurately" by
1775*56731Sbostic  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1776*56731Sbostic  *
1777*56731Sbostic  * Modifications:
1778*56731Sbostic  *	1. Rather than iterating, we use a simple numeric overestimate
1779*56731Sbostic  *	   to determine k = floor(log10(d)).  We scale relevant
1780*56731Sbostic  *	   quantities using O(log2(k)) rather than O(k) multiplications.
1781*56731Sbostic  *	2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1782*56731Sbostic  *	   try to generate digits strictly left to right.  Instead, we
1783*56731Sbostic  *	   compute with fewer bits and propagate the carry if necessary
1784*56731Sbostic  *	   when rounding the final digit up.  This is often faster.
1785*56731Sbostic  *	3. Under the assumption that input will be rounded nearest,
1786*56731Sbostic  *	   mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1787*56731Sbostic  *	   That is, we allow equality in stopping tests when the
1788*56731Sbostic  *	   round-nearest rule will give the same floating-point value
1789*56731Sbostic  *	   as would satisfaction of the stopping test with strict
1790*56731Sbostic  *	   inequality.
1791*56731Sbostic  *	4. We remove common factors of powers of 2 from relevant
1792*56731Sbostic  *	   quantities.
1793*56731Sbostic  *	5. When converting floating-point integers less than 1e16,
1794*56731Sbostic  *	   we use floating-point arithmetic rather than resorting
1795*56731Sbostic  *	   to multiple-precision integers.
1796*56731Sbostic  *	6. When asked to produce fewer than 15 digits, we first try
1797*56731Sbostic  *	   to get by with floating-point arithmetic; we resort to
1798*56731Sbostic  *	   multiple-precision integer arithmetic only if we cannot
1799*56731Sbostic  *	   guarantee that the floating-point calculation has given
1800*56731Sbostic  *	   the correctly rounded result.  For k requested digits and
1801*56731Sbostic  *	   "uniformly" distributed input, the probability is
1802*56731Sbostic  *	   something like 10^(k-15) that we must resort to the long
1803*56731Sbostic  *	   calculation.
1804*56731Sbostic  */
1805*56731Sbostic 
1806*56731Sbostic char *
1807*56731Sbostic __dtoa
1808*56731Sbostic #ifdef KR_headers
1809*56731Sbostic 	(d, mode, ndigits, decpt, sign, rve)
1810*56731Sbostic 	double d; int mode, ndigits, *decpt, *sign; char **rve;
1811*56731Sbostic #else
1812*56731Sbostic 	(double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
1813*56731Sbostic #endif
1814*56731Sbostic {
1815*56731Sbostic  /*	Arguments ndigits, decpt, sign are similar to those
1816*56731Sbostic 	of ecvt and fcvt; trailing zeros are suppressed from
1817*56731Sbostic 	the returned string.  If not null, *rve is set to point
1818*56731Sbostic 	to the end of the return value.  If d is +-Infinity or NaN,
1819*56731Sbostic 	then *decpt is set to 9999.
1820*56731Sbostic 
1821*56731Sbostic 	mode:
1822*56731Sbostic 		0 ==> shortest string that yields d when read in
1823*56731Sbostic 			and rounded to nearest.
1824*56731Sbostic 		1 ==> like 0, but with Steele & White stopping rule;
1825*56731Sbostic 			e.g. with IEEE P754 arithmetic , mode 0 gives
1826*56731Sbostic 			1e23 whereas mode 1 gives 9.999999999999999e22.
1827*56731Sbostic 		2 ==> max(1,ndigits) significant digits.  This gives a
1828*56731Sbostic 			return value similar to that of ecvt, except
1829*56731Sbostic 			that trailing zeros are suppressed.
1830*56731Sbostic 		3 ==> through ndigits past the decimal point.  This
1831*56731Sbostic 			gives a return value similar to that from fcvt,
1832*56731Sbostic 			except that trailing zeros are suppressed, and
1833*56731Sbostic 			ndigits can be negative.
1834*56731Sbostic 		4-9 should give the same return values as 2-3, i.e.,
1835*56731Sbostic 			4 <= mode <= 9 ==> same return as mode
1836*56731Sbostic 			2 + (mode & 1).  These modes are mainly for
1837*56731Sbostic 			debugging; often they run slower but sometimes
1838*56731Sbostic 			faster than modes 2-3.
1839*56731Sbostic 		4,5,8,9 ==> left-to-right digit generation.
1840*56731Sbostic 		6-9 ==> don't try fast floating-point estimate
1841*56731Sbostic 			(if applicable).
1842*56731Sbostic 
1843*56731Sbostic 		Values of mode other than 0-9 are treated as mode 0.
1844*56731Sbostic 
1845*56731Sbostic 		Sufficient space is allocated to the return value
1846*56731Sbostic 		to hold the suppressed trailing zeros.
1847*56731Sbostic 	*/
1848*56731Sbostic 
1849*56731Sbostic 	int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
1850*56731Sbostic 		j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1851*56731Sbostic 		spec_case, try_quick;
1852*56731Sbostic 	long L;
1853*56731Sbostic #ifndef Sudden_Underflow
1854*56731Sbostic 	int denorm;
1855*56731Sbostic 	unsigned long x;
1856*56731Sbostic #endif
1857*56731Sbostic 	Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1858*56731Sbostic 	double d2, ds, eps;
1859*56731Sbostic 	char *s, *s0;
1860*56731Sbostic 	static Bigint *result;
1861*56731Sbostic 	static int result_k;
1862*56731Sbostic 
1863*56731Sbostic 	if (result) {
1864*56731Sbostic 		result->k = result_k;
1865*56731Sbostic 		result->maxwds = 1 << result_k;
1866*56731Sbostic 		Bfree(result);
1867*56731Sbostic 		result = 0;
1868*56731Sbostic 	}
1869*56731Sbostic 
1870*56731Sbostic 	if (word0(d) & Sign_bit) {
1871*56731Sbostic 		/* set sign for everything, including 0's and NaNs */
1872*56731Sbostic 		*sign = 1;
1873*56731Sbostic 		word0(d) &= ~Sign_bit;	/* clear sign bit */
1874*56731Sbostic 	}
1875*56731Sbostic 	else
1876*56731Sbostic 		*sign = 0;
1877*56731Sbostic 
1878*56731Sbostic #if defined(IEEE_Arith) + defined(VAX)
1879*56731Sbostic #ifdef IEEE_Arith
1880*56731Sbostic 	if ((word0(d) & Exp_mask) == Exp_mask)
1881*56731Sbostic #else
1882*56731Sbostic 	if (word0(d)  == 0x8000)
1883*56731Sbostic #endif
1884*56731Sbostic 	{
1885*56731Sbostic 		/* Infinity or NaN */
1886*56731Sbostic 		*decpt = 9999;
1887*56731Sbostic 		s =
1888*56731Sbostic #ifdef IEEE_Arith
1889*56731Sbostic 			!word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
1890*56731Sbostic #endif
1891*56731Sbostic 				"NaN";
1892*56731Sbostic 		if (rve)
1893*56731Sbostic 			*rve =
1894*56731Sbostic #ifdef IEEE_Arith
1895*56731Sbostic 				s[3] ? s + 8 :
1896*56731Sbostic #endif
1897*56731Sbostic 						s + 3;
1898*56731Sbostic 		return s;
1899*56731Sbostic 	}
1900*56731Sbostic #endif
1901*56731Sbostic #ifdef IBM
1902*56731Sbostic 	d += 0; /* normalize */
1903*56731Sbostic #endif
1904*56731Sbostic 	if (!d) {
1905*56731Sbostic 		*decpt = 1;
1906*56731Sbostic 		s = "0";
1907*56731Sbostic 		if (rve)
1908*56731Sbostic 			*rve = s + 1;
1909*56731Sbostic 		return s;
1910*56731Sbostic 	}
1911*56731Sbostic 
1912*56731Sbostic 	b = d2b(d, &be, &bbits);
1913*56731Sbostic #ifdef Sudden_Underflow
1914*56731Sbostic 	i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1915*56731Sbostic #else
1916*56731Sbostic 	if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) {
1917*56731Sbostic #endif
1918*56731Sbostic 		d2 = d;
1919*56731Sbostic 		word0(d2) &= Frac_mask1;
1920*56731Sbostic 		word0(d2) |= Exp_11;
1921*56731Sbostic #ifdef IBM
1922*56731Sbostic 		if (j = 11 - hi0bits(word0(d2) & Frac_mask))
1923*56731Sbostic 			d2 /= 1 << j;
1924*56731Sbostic #endif
1925*56731Sbostic 
1926*56731Sbostic 		/* log(x)	~=~ log(1.5) + (x-1.5)/1.5
1927*56731Sbostic 		 * log10(x)	 =  log(x) / log(10)
1928*56731Sbostic 		 *		~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1929*56731Sbostic 		 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1930*56731Sbostic 		 *
1931*56731Sbostic 		 * This suggests computing an approximation k to log10(d) by
1932*56731Sbostic 		 *
1933*56731Sbostic 		 * k = (i - Bias)*0.301029995663981
1934*56731Sbostic 		 *	+ ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1935*56731Sbostic 		 *
1936*56731Sbostic 		 * We want k to be too large rather than too small.
1937*56731Sbostic 		 * The error in the first-order Taylor series approximation
1938*56731Sbostic 		 * is in our favor, so we just round up the constant enough
1939*56731Sbostic 		 * to compensate for any error in the multiplication of
1940*56731Sbostic 		 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1941*56731Sbostic 		 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1942*56731Sbostic 		 * adding 1e-13 to the constant term more than suffices.
1943*56731Sbostic 		 * Hence we adjust the constant term to 0.1760912590558.
1944*56731Sbostic 		 * (We could get a more accurate k by invoking log10,
1945*56731Sbostic 		 *  but this is probably not worthwhile.)
1946*56731Sbostic 		 */
1947*56731Sbostic 
1948*56731Sbostic 		i -= Bias;
1949*56731Sbostic #ifdef IBM
1950*56731Sbostic 		i <<= 2;
1951*56731Sbostic 		i += j;
1952*56731Sbostic #endif
1953*56731Sbostic #ifndef Sudden_Underflow
1954*56731Sbostic 		denorm = 0;
1955*56731Sbostic 	} else {
1956*56731Sbostic 		/* d is denormalized */
1957*56731Sbostic 
1958*56731Sbostic 		i = bbits + be + (Bias + (P-1) - 1);
1959*56731Sbostic 		x = i > 32  ? word0(d) << 64 - i | word1(d) >> i - 32
1960*56731Sbostic 			    : word1(d) << 32 - i;
1961*56731Sbostic 		d2 = x;
1962*56731Sbostic 		word0(d2) -= 31*Exp_msk1; /* adjust exponent */
1963*56731Sbostic 		i -= (Bias + (P-1) - 1) + 1;
1964*56731Sbostic 		denorm = 1;
1965*56731Sbostic 	}
1966*56731Sbostic #endif
1967*56731Sbostic 	ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
1968*56731Sbostic 	k = (int)ds;
1969*56731Sbostic 	if (ds < 0. && ds != k)
1970*56731Sbostic 		k--;	/* want k = floor(ds) */
1971*56731Sbostic 	k_check = 1;
1972*56731Sbostic 	if (k >= 0 && k <= Ten_pmax) {
1973*56731Sbostic 		if (d < tens[k])
1974*56731Sbostic 			k--;
1975*56731Sbostic 		k_check = 0;
1976*56731Sbostic 	}
1977*56731Sbostic 	j = bbits - i - 1;
1978*56731Sbostic 	if (j >= 0) {
1979*56731Sbostic 		b2 = 0;
1980*56731Sbostic 		s2 = j;
1981*56731Sbostic 	} else {
1982*56731Sbostic 		b2 = -j;
1983*56731Sbostic 		s2 = 0;
1984*56731Sbostic 	}
1985*56731Sbostic 	if (k >= 0) {
1986*56731Sbostic 		b5 = 0;
1987*56731Sbostic 		s5 = k;
1988*56731Sbostic 		s2 += k;
1989*56731Sbostic 	} else {
1990*56731Sbostic 		b2 -= k;
1991*56731Sbostic 		b5 = -k;
1992*56731Sbostic 		s5 = 0;
1993*56731Sbostic 	}
1994*56731Sbostic 	if (mode < 0 || mode > 9)
1995*56731Sbostic 		mode = 0;
1996*56731Sbostic 	try_quick = 1;
1997*56731Sbostic 	if (mode > 5) {
1998*56731Sbostic 		mode -= 4;
1999*56731Sbostic 		try_quick = 0;
2000*56731Sbostic 	}
2001*56731Sbostic 	leftright = 1;
2002*56731Sbostic 	switch(mode) {
2003*56731Sbostic 		case 0:
2004*56731Sbostic 		case 1:
2005*56731Sbostic 			ilim = ilim1 = -1;
2006*56731Sbostic 			i = 18;
2007*56731Sbostic 			ndigits = 0;
2008*56731Sbostic 			break;
2009*56731Sbostic 		case 2:
2010*56731Sbostic 			leftright = 0;
2011*56731Sbostic 			/* no break */
2012*56731Sbostic 		case 4:
2013*56731Sbostic 			if (ndigits <= 0)
2014*56731Sbostic 				ndigits = 1;
2015*56731Sbostic 			ilim = ilim1 = i = ndigits;
2016*56731Sbostic 			break;
2017*56731Sbostic 		case 3:
2018*56731Sbostic 			leftright = 0;
2019*56731Sbostic 			/* no break */
2020*56731Sbostic 		case 5:
2021*56731Sbostic 			i = ndigits + k + 1;
2022*56731Sbostic 			ilim = i;
2023*56731Sbostic 			ilim1 = i - 1;
2024*56731Sbostic 			if (i <= 0)
2025*56731Sbostic 				i = 1;
2026*56731Sbostic 	}
2027*56731Sbostic 	j = sizeof(unsigned long);
2028*56731Sbostic 	for (result_k = 0; sizeof(Bigint) - sizeof(unsigned long) + j < i;
2029*56731Sbostic 		j <<= 1) result_k++;
2030*56731Sbostic 	result = Balloc(result_k);
2031*56731Sbostic 	s = s0 = (char *)result;
2032*56731Sbostic 
2033*56731Sbostic 	if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2034*56731Sbostic 
2035*56731Sbostic 		/* Try to get by with floating-point arithmetic. */
2036*56731Sbostic 
2037*56731Sbostic 		i = 0;
2038*56731Sbostic 		d2 = d;
2039*56731Sbostic 		k0 = k;
2040*56731Sbostic 		ilim0 = ilim;
2041*56731Sbostic 		ieps = 2; /* conservative */
2042*56731Sbostic 		if (k > 0) {
2043*56731Sbostic 			ds = tens[k&0xf];
2044*56731Sbostic 			j = k >> 4;
2045*56731Sbostic 			if (j & Bletch) {
2046*56731Sbostic 				/* prevent overflows */
2047*56731Sbostic 				j &= Bletch - 1;
2048*56731Sbostic 				d /= bigtens[n_bigtens-1];
2049*56731Sbostic 				ieps++;
2050*56731Sbostic 			}
2051*56731Sbostic 			for (; j; j >>= 1, i++)
2052*56731Sbostic 				if (j & 1) {
2053*56731Sbostic 					ieps++;
2054*56731Sbostic 					ds *= bigtens[i];
2055*56731Sbostic 				}
2056*56731Sbostic 			d /= ds;
2057*56731Sbostic 		} else if (j1 = -k) {
2058*56731Sbostic 			d *= tens[j1 & 0xf];
2059*56731Sbostic 			for (j = j1 >> 4; j; j >>= 1, i++)
2060*56731Sbostic 				if (j & 1) {
2061*56731Sbostic 					ieps++;
2062*56731Sbostic 					d *= bigtens[i];
2063*56731Sbostic 				}
2064*56731Sbostic 		}
2065*56731Sbostic 		if (k_check && d < 1. && ilim > 0) {
2066*56731Sbostic 			if (ilim1 <= 0)
2067*56731Sbostic 				goto fast_failed;
2068*56731Sbostic 			ilim = ilim1;
2069*56731Sbostic 			k--;
2070*56731Sbostic 			d *= 10.;
2071*56731Sbostic 			ieps++;
2072*56731Sbostic 		}
2073*56731Sbostic 		eps = ieps*d + 7.;
2074*56731Sbostic 		word0(eps) -= (P-1)*Exp_msk1;
2075*56731Sbostic 		if (ilim == 0) {
2076*56731Sbostic 			S = mhi = 0;
2077*56731Sbostic 			d -= 5.;
2078*56731Sbostic 			if (d > eps)
2079*56731Sbostic 				goto one_digit;
2080*56731Sbostic 			if (d < -eps)
2081*56731Sbostic 				goto no_digits;
2082*56731Sbostic 			goto fast_failed;
2083*56731Sbostic 		}
2084*56731Sbostic #ifndef No_leftright
2085*56731Sbostic 		if (leftright) {
2086*56731Sbostic 			/* Use Steele & White method of only
2087*56731Sbostic 			 * generating digits needed.
2088*56731Sbostic 			 */
2089*56731Sbostic 			eps = 0.5/tens[ilim-1] - eps;
2090*56731Sbostic 			for (i = 0;;) {
2091*56731Sbostic 				L = d;
2092*56731Sbostic 				d -= L;
2093*56731Sbostic 				*s++ = '0' + (int)L;
2094*56731Sbostic 				if (d < eps)
2095*56731Sbostic 					goto ret1;
2096*56731Sbostic 				if (1. - d < eps)
2097*56731Sbostic 					goto bump_up;
2098*56731Sbostic 				if (++i >= ilim)
2099*56731Sbostic 					break;
2100*56731Sbostic 				eps *= 10.;
2101*56731Sbostic 				d *= 10.;
2102*56731Sbostic 			}
2103*56731Sbostic 		} else {
2104*56731Sbostic #endif
2105*56731Sbostic 			/* Generate ilim digits, then fix them up. */
2106*56731Sbostic 			eps *= tens[ilim-1];
2107*56731Sbostic 			for (i = 1;; i++, d *= 10.) {
2108*56731Sbostic 				L = d;
2109*56731Sbostic 				d -= L;
2110*56731Sbostic 				*s++ = '0' + (int)L;
2111*56731Sbostic 				if (i == ilim) {
2112*56731Sbostic 					if (d > 0.5 + eps)
2113*56731Sbostic 						goto bump_up;
2114*56731Sbostic 					else if (d < 0.5 - eps) {
2115*56731Sbostic 						while (*--s == '0');
2116*56731Sbostic 						s++;
2117*56731Sbostic 						goto ret1;
2118*56731Sbostic 					}
2119*56731Sbostic 					break;
2120*56731Sbostic 				}
2121*56731Sbostic 			}
2122*56731Sbostic #ifndef No_leftright
2123*56731Sbostic 		}
2124*56731Sbostic #endif
2125*56731Sbostic  fast_failed:
2126*56731Sbostic 		s = s0;
2127*56731Sbostic 		d = d2;
2128*56731Sbostic 		k = k0;
2129*56731Sbostic 		ilim = ilim0;
2130*56731Sbostic 	}
2131*56731Sbostic 
2132*56731Sbostic 	/* Do we have a "small" integer? */
2133*56731Sbostic 
2134*56731Sbostic 	if (be >= 0 && k <= Int_max) {
2135*56731Sbostic 		/* Yes. */
2136*56731Sbostic 		ds = tens[k];
2137*56731Sbostic 		if (ndigits < 0 && ilim <= 0) {
2138*56731Sbostic 			S = mhi = 0;
2139*56731Sbostic 			if (ilim < 0 || d <= 5*ds)
2140*56731Sbostic 				goto no_digits;
2141*56731Sbostic 			goto one_digit;
2142*56731Sbostic 		}
2143*56731Sbostic 		for (i = 1;; i++) {
2144*56731Sbostic 			L = d / ds;
2145*56731Sbostic 			d -= L*ds;
2146*56731Sbostic #ifdef Check_FLT_ROUNDS
2147*56731Sbostic 			/* If FLT_ROUNDS == 2, L will usually be high by 1 */
2148*56731Sbostic 			if (d < 0) {
2149*56731Sbostic 				L--;
2150*56731Sbostic 				d += ds;
2151*56731Sbostic 			}
2152*56731Sbostic #endif
2153*56731Sbostic 			*s++ = '0' + (int)L;
2154*56731Sbostic 			if (i == ilim) {
2155*56731Sbostic 				d += d;
2156*56731Sbostic 				if (d > ds || d == ds && L & 1) {
2157*56731Sbostic  bump_up:
2158*56731Sbostic 					while (*--s == '9')
2159*56731Sbostic 						if (s == s0) {
2160*56731Sbostic 							k++;
2161*56731Sbostic 							*s = '0';
2162*56731Sbostic 							break;
2163*56731Sbostic 						}
2164*56731Sbostic 					++*s++;
2165*56731Sbostic 				}
2166*56731Sbostic 				break;
2167*56731Sbostic 			}
2168*56731Sbostic 			if (!(d *= 10.))
2169*56731Sbostic 				break;
2170*56731Sbostic 		}
2171*56731Sbostic 		goto ret1;
2172*56731Sbostic 	}
2173*56731Sbostic 
2174*56731Sbostic 	m2 = b2;
2175*56731Sbostic 	m5 = b5;
2176*56731Sbostic 	mhi = mlo = 0;
2177*56731Sbostic 	if (leftright) {
2178*56731Sbostic 		if (mode < 2) {
2179*56731Sbostic 			i =
2180*56731Sbostic #ifndef Sudden_Underflow
2181*56731Sbostic 				denorm ? be + (Bias + (P-1) - 1 + 1) :
2182*56731Sbostic #endif
2183*56731Sbostic #ifdef IBM
2184*56731Sbostic 				1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2185*56731Sbostic #else
2186*56731Sbostic 				1 + P - bbits;
2187*56731Sbostic #endif
2188*56731Sbostic 		} else {
2189*56731Sbostic 			j = ilim - 1;
2190*56731Sbostic 			if (m5 >= j)
2191*56731Sbostic 				m5 -= j;
2192*56731Sbostic 			else {
2193*56731Sbostic 				s5 += j -= m5;
2194*56731Sbostic 				b5 += j;
2195*56731Sbostic 				m5 = 0;
2196*56731Sbostic 			}
2197*56731Sbostic 			if ((i = ilim) < 0) {
2198*56731Sbostic 				m2 -= i;
2199*56731Sbostic 				i = 0;
2200*56731Sbostic 			}
2201*56731Sbostic 		}
2202*56731Sbostic 		b2 += i;
2203*56731Sbostic 		s2 += i;
2204*56731Sbostic 		mhi = i2b(1);
2205*56731Sbostic 	}
2206*56731Sbostic 	if (m2 > 0 && s2 > 0) {
2207*56731Sbostic 		i = m2 < s2 ? m2 : s2;
2208*56731Sbostic 		b2 -= i;
2209*56731Sbostic 		m2 -= i;
2210*56731Sbostic 		s2 -= i;
2211*56731Sbostic 	}
2212*56731Sbostic 	if (b5 > 0) {
2213*56731Sbostic 		if (leftright) {
2214*56731Sbostic 			if (m5 > 0) {
2215*56731Sbostic 				mhi = pow5mult(mhi, m5);
2216*56731Sbostic 				b1 = mult(mhi, b);
2217*56731Sbostic 				Bfree(b);
2218*56731Sbostic 				b = b1;
2219*56731Sbostic 				}
2220*56731Sbostic 			if (j = b5 - m5)
2221*56731Sbostic 				b = pow5mult(b, j);
2222*56731Sbostic 		} else
2223*56731Sbostic 			b = pow5mult(b, b5);
2224*56731Sbostic 	}
2225*56731Sbostic 	S = i2b(1);
2226*56731Sbostic 	if (s5 > 0)
2227*56731Sbostic 		S = pow5mult(S, s5);
2228*56731Sbostic 
2229*56731Sbostic 	/* Check for special case that d is a normalized power of 2. */
2230*56731Sbostic 
2231*56731Sbostic 	if (mode < 2) {
2232*56731Sbostic 		if (!word1(d) && !(word0(d) & Bndry_mask)
2233*56731Sbostic #ifndef Sudden_Underflow
2234*56731Sbostic 		 && word0(d) & Exp_mask
2235*56731Sbostic #endif
2236*56731Sbostic 				) {
2237*56731Sbostic 			/* The special case */
2238*56731Sbostic 			b2 += Log2P;
2239*56731Sbostic 			s2 += Log2P;
2240*56731Sbostic 			spec_case = 1;
2241*56731Sbostic 		} else
2242*56731Sbostic 			spec_case = 0;
2243*56731Sbostic 	}
2244*56731Sbostic 
2245*56731Sbostic 	/* Arrange for convenient computation of quotients:
2246*56731Sbostic 	 * shift left if necessary so divisor has 4 leading 0 bits.
2247*56731Sbostic 	 *
2248*56731Sbostic 	 * Perhaps we should just compute leading 28 bits of S once
2249*56731Sbostic 	 * and for all and pass them and a shift to quorem, so it
2250*56731Sbostic 	 * can do shifts and ors to compute the numerator for q.
2251*56731Sbostic 	 */
2252*56731Sbostic #ifdef Pack_32
2253*56731Sbostic 	if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)
2254*56731Sbostic 		i = 32 - i;
2255*56731Sbostic #else
2256*56731Sbostic 	if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
2257*56731Sbostic 		i = 16 - i;
2258*56731Sbostic #endif
2259*56731Sbostic 	if (i > 4) {
2260*56731Sbostic 		i -= 4;
2261*56731Sbostic 		b2 += i;
2262*56731Sbostic 		m2 += i;
2263*56731Sbostic 		s2 += i;
2264*56731Sbostic 	} else if (i < 4) {
2265*56731Sbostic 		i += 28;
2266*56731Sbostic 		b2 += i;
2267*56731Sbostic 		m2 += i;
2268*56731Sbostic 		s2 += i;
2269*56731Sbostic 	}
2270*56731Sbostic 	if (b2 > 0)
2271*56731Sbostic 		b = lshift(b, b2);
2272*56731Sbostic 	if (s2 > 0)
2273*56731Sbostic 		S = lshift(S, s2);
2274*56731Sbostic 	if (k_check) {
2275*56731Sbostic 		if (cmp(b,S) < 0) {
2276*56731Sbostic 			k--;
2277*56731Sbostic 			b = multadd(b, 10, 0);	/* we botched the k estimate */
2278*56731Sbostic 			if (leftright)
2279*56731Sbostic 				mhi = multadd(mhi, 10, 0);
2280*56731Sbostic 			ilim = ilim1;
2281*56731Sbostic 		}
2282*56731Sbostic 	}
2283*56731Sbostic 	if (ilim <= 0 && mode > 2) {
2284*56731Sbostic 		if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2285*56731Sbostic 			/* no digits, fcvt style */
2286*56731Sbostic  no_digits:
2287*56731Sbostic 			k = -1 - ndigits;
2288*56731Sbostic 			goto ret;
2289*56731Sbostic 		}
2290*56731Sbostic  one_digit:
2291*56731Sbostic 		*s++ = '1';
2292*56731Sbostic 		k++;
2293*56731Sbostic 		goto ret;
2294*56731Sbostic 	}
2295*56731Sbostic 	if (leftright) {
2296*56731Sbostic 		if (m2 > 0)
2297*56731Sbostic 			mhi = lshift(mhi, m2);
2298*56731Sbostic 
2299*56731Sbostic 		/* Compute mlo -- check for special case
2300*56731Sbostic 		 * that d is a normalized power of 2.
2301*56731Sbostic 		 */
2302*56731Sbostic 
2303*56731Sbostic 		mlo = mhi;
2304*56731Sbostic 		if (spec_case) {
2305*56731Sbostic 			mhi = Balloc(mhi->k);
2306*56731Sbostic 			Bcopy(mhi, mlo);
2307*56731Sbostic 			mhi = lshift(mhi, Log2P);
2308*56731Sbostic 		}
2309*56731Sbostic 
2310*56731Sbostic 		for (i = 1;;i++) {
2311*56731Sbostic 			dig = quorem(b,S) + '0';
2312*56731Sbostic 			/* Do we yet have the shortest decimal string
2313*56731Sbostic 			 * that will round to d?
2314*56731Sbostic 			 */
2315*56731Sbostic 			j = cmp(b, mlo);
2316*56731Sbostic 			delta = diff(S, mhi);
2317*56731Sbostic 			j1 = delta->sign ? 1 : cmp(b, delta);
2318*56731Sbostic 			Bfree(delta);
2319*56731Sbostic #ifndef ROUND_BIASED
2320*56731Sbostic 			if (j1 == 0 && !mode && !(word1(d) & 1)) {
2321*56731Sbostic 				if (dig == '9')
2322*56731Sbostic 					goto round_9_up;
2323*56731Sbostic 				if (j > 0)
2324*56731Sbostic 					dig++;
2325*56731Sbostic 				*s++ = dig;
2326*56731Sbostic 				goto ret;
2327*56731Sbostic 			}
2328*56731Sbostic #endif
2329*56731Sbostic 			if (j < 0 || j == 0 && !mode
2330*56731Sbostic #ifndef ROUND_BIASED
2331*56731Sbostic 							&& !(word1(d) & 1)
2332*56731Sbostic #endif
2333*56731Sbostic 					) {
2334*56731Sbostic 				if (j1 > 0) {
2335*56731Sbostic 					b = lshift(b, 1);
2336*56731Sbostic 					j1 = cmp(b, S);
2337*56731Sbostic 					if ((j1 > 0 || j1 == 0 && dig & 1)
2338*56731Sbostic 					&& dig++ == '9')
2339*56731Sbostic 						goto round_9_up;
2340*56731Sbostic 				}
2341*56731Sbostic 				*s++ = dig;
2342*56731Sbostic 				goto ret;
2343*56731Sbostic 			}
2344*56731Sbostic 			if (j1 > 0) {
2345*56731Sbostic 				if (dig == '9') { /* possible if i == 1 */
2346*56731Sbostic  round_9_up:
2347*56731Sbostic 					*s++ = '9';
2348*56731Sbostic 					goto roundoff;
2349*56731Sbostic 				}
2350*56731Sbostic 				*s++ = dig + 1;
2351*56731Sbostic 				goto ret;
2352*56731Sbostic 			}
2353*56731Sbostic 			*s++ = dig;
2354*56731Sbostic 			if (i == ilim)
2355*56731Sbostic 				break;
2356*56731Sbostic 			b = multadd(b, 10, 0);
2357*56731Sbostic 			if (mlo == mhi)
2358*56731Sbostic 				mlo = mhi = multadd(mhi, 10, 0);
2359*56731Sbostic 			else {
2360*56731Sbostic 				mlo = multadd(mlo, 10, 0);
2361*56731Sbostic 				mhi = multadd(mhi, 10, 0);
2362*56731Sbostic 			}
2363*56731Sbostic 		}
2364*56731Sbostic 	} else
2365*56731Sbostic 		for (i = 1;; i++) {
2366*56731Sbostic 			*s++ = dig = quorem(b,S) + '0';
2367*56731Sbostic 			if (i >= ilim)
2368*56731Sbostic 				break;
2369*56731Sbostic 			b = multadd(b, 10, 0);
2370*56731Sbostic 		}
2371*56731Sbostic 
2372*56731Sbostic 	/* Round off last digit */
2373*56731Sbostic 
2374*56731Sbostic 	b = lshift(b, 1);
2375*56731Sbostic 	j = cmp(b, S);
2376*56731Sbostic 	if (j > 0 || j == 0 && dig & 1) {
2377*56731Sbostic  roundoff:
2378*56731Sbostic 		while (*--s == '9')
2379*56731Sbostic 			if (s == s0) {
2380*56731Sbostic 				k++;
2381*56731Sbostic 				*s++ = '1';
2382*56731Sbostic 				goto ret;
2383*56731Sbostic 			}
2384*56731Sbostic 		++*s++;
2385*56731Sbostic 	} else {
2386*56731Sbostic 		while (*--s == '0');
2387*56731Sbostic 		s++;
2388*56731Sbostic 	}
2389*56731Sbostic  ret:
2390*56731Sbostic 	Bfree(S);
2391*56731Sbostic 	if (mhi) {
2392*56731Sbostic 		if (mlo && mlo != mhi)
2393*56731Sbostic 			Bfree(mlo);
2394*56731Sbostic 		Bfree(mhi);
2395*56731Sbostic 	}
2396*56731Sbostic  ret1:
2397*56731Sbostic 	Bfree(b);
2398*56731Sbostic 	if (s == s0) {	/* don't return empty string */
2399*56731Sbostic 		*s++ = '0';
2400*56731Sbostic 		k = 0;
2401*56731Sbostic 	}
2402*56731Sbostic 	*s = 0;
2403*56731Sbostic 	*decpt = k + 1;
2404*56731Sbostic 	if (rve)
2405*56731Sbostic 		*rve = s;
2406*56731Sbostic 	return s0;
2407*56731Sbostic 	}
2408*56731Sbostic #ifdef __cplusplus
2409*56731Sbostic }
2410*56731Sbostic #endif
2411