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