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