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