]> git.saurik.com Git - apple/libc.git/blob - stdlib.subproj/strtod.c
Libc-167.tar.gz
[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);
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 *freelist[Kmax+1];
402
403 static Bigint *
404 Balloc
405 #ifdef KR_headers
406 (k) int k;
407 #else
408 (int k)
409 #endif
410 {
411 int x;
412 Bigint *rv;
413
414 if (rv = freelist[k]) {
415 freelist[k] = rv->next;
416 }
417 else {
418 x = 1 << k;
419 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
420 rv->k = k;
421 rv->maxwds = x;
422 }
423 rv->sign = rv->wds = 0;
424 return rv;
425 }
426
427 static void
428 Bfree
429 #ifdef KR_headers
430 (v) Bigint *v;
431 #else
432 (Bigint *v)
433 #endif
434 {
435 if (v) {
436 v->next = freelist[v->k];
437 freelist[v->k] = v;
438 }
439 }
440
441 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
442 y->wds*sizeof(Long) + 2*sizeof(int))
443
444 static Bigint *
445 multadd
446 #ifdef KR_headers
447 (b, m, a) Bigint *b; int m, a;
448 #else
449 (Bigint *b, int m, int a) /* multiply by m and add a */
450 #endif
451 {
452 int i, wds;
453 ULong *x, y;
454 #ifdef Pack_32
455 ULong xi, z;
456 #endif
457 Bigint *b1;
458
459 wds = b->wds;
460 x = b->x;
461 i = 0;
462 do {
463 #ifdef Pack_32
464 xi = *x;
465 y = (xi & 0xffff) * m + a;
466 z = (xi >> 16) * m + (y >> 16);
467 a = (int)(z >> 16);
468 *x++ = (z << 16) + (y & 0xffff);
469 #else
470 y = *x * m + a;
471 a = (int)(y >> 16);
472 *x++ = y & 0xffff;
473 #endif
474 }
475 while(++i < wds);
476 if (a) {
477 if (wds >= b->maxwds) {
478 b1 = Balloc(b->k+1);
479 Bcopy(b1, b);
480 Bfree(b);
481 b = b1;
482 }
483 b->x[wds++] = a;
484 b->wds = wds;
485 }
486 return b;
487 }
488
489 static Bigint *
490 s2b
491 #ifdef KR_headers
492 (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
493 #else
494 (CONST char *s, int nd0, int nd, ULong y9)
495 #endif
496 {
497 Bigint *b;
498 int i, k;
499 Long x, y;
500
501 x = (nd + 8) / 9;
502 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
503 #ifdef Pack_32
504 b = Balloc(k);
505 b->x[0] = y9;
506 b->wds = 1;
507 #else
508 b = Balloc(k+1);
509 b->x[0] = y9 & 0xffff;
510 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
511 #endif
512
513 i = 9;
514 if (9 < nd0) {
515 s += 9;
516 do b = multadd(b, 10, *s++ - '0');
517 while(++i < nd0);
518 s++;
519 }
520 else
521 s += 10;
522 for(; i < nd; i++)
523 b = multadd(b, 10, *s++ - '0');
524 return b;
525 }
526
527 static int
528 hi0bits
529 #ifdef KR_headers
530 (x) register ULong x;
531 #else
532 (register ULong x)
533 #endif
534 {
535 register int k = 0;
536
537 if (!(x & 0xffff0000)) {
538 k = 16;
539 x <<= 16;
540 }
541 if (!(x & 0xff000000)) {
542 k += 8;
543 x <<= 8;
544 }
545 if (!(x & 0xf0000000)) {
546 k += 4;
547 x <<= 4;
548 }
549 if (!(x & 0xc0000000)) {
550 k += 2;
551 x <<= 2;
552 }
553 if (!(x & 0x80000000)) {
554 k++;
555 if (!(x & 0x40000000))
556 return 32;
557 }
558 return k;
559 }
560
561 static int
562 lo0bits
563 #ifdef KR_headers
564 (y) ULong *y;
565 #else
566 (ULong *y)
567 #endif
568 {
569 register int k;
570 register ULong x = *y;
571
572 if (x & 7) {
573 if (x & 1)
574 return 0;
575 if (x & 2) {
576 *y = x >> 1;
577 return 1;
578 }
579 *y = x >> 2;
580 return 2;
581 }
582 k = 0;
583 if (!(x & 0xffff)) {
584 k = 16;
585 x >>= 16;
586 }
587 if (!(x & 0xff)) {
588 k += 8;
589 x >>= 8;
590 }
591 if (!(x & 0xf)) {
592 k += 4;
593 x >>= 4;
594 }
595 if (!(x & 0x3)) {
596 k += 2;
597 x >>= 2;
598 }
599 if (!(x & 1)) {
600 k++;
601 x >>= 1;
602 if (!x & 1)
603 return 32;
604 }
605 *y = x;
606 return k;
607 }
608
609 static Bigint *
610 i2b
611 #ifdef KR_headers
612 (i) int i;
613 #else
614 (int i)
615 #endif
616 {
617 Bigint *b;
618
619 b = Balloc(1);
620 b->x[0] = i;
621 b->wds = 1;
622 return b;
623 }
624
625 static Bigint *
626 mult
627 #ifdef KR_headers
628 (a, b) Bigint *a, *b;
629 #else
630 (Bigint *a, Bigint *b)
631 #endif
632 {
633 Bigint *c;
634 int k, wa, wb, wc;
635 ULong carry, y, z;
636 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
637 #ifdef Pack_32
638 ULong z2;
639 #endif
640
641 if (a->wds < b->wds) {
642 c = a;
643 a = b;
644 b = c;
645 }
646 k = a->k;
647 wa = a->wds;
648 wb = b->wds;
649 wc = wa + wb;
650 if (wc > a->maxwds)
651 k++;
652 c = Balloc(k);
653 for(x = c->x, xa = x + wc; x < xa; x++)
654 *x = 0;
655 xa = a->x;
656 xae = xa + wa;
657 xb = b->x;
658 xbe = xb + wb;
659 xc0 = c->x;
660 #ifdef Pack_32
661 for(; xb < xbe; xb++, xc0++) {
662 if (y = *xb & 0xffff) {
663 x = xa;
664 xc = xc0;
665 carry = 0;
666 do {
667 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
668 carry = z >> 16;
669 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
670 carry = z2 >> 16;
671 Storeinc(xc, z2, z);
672 }
673 while(x < xae);
674 *xc = carry;
675 }
676 if (y = *xb >> 16) {
677 x = xa;
678 xc = xc0;
679 carry = 0;
680 z2 = *xc;
681 do {
682 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
683 carry = z >> 16;
684 Storeinc(xc, z, z2);
685 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
686 carry = z2 >> 16;
687 }
688 while(x < xae);
689 *xc = z2;
690 }
691 }
692 #else
693 for(; xb < xbe; xc0++) {
694 if (y = *xb++) {
695 x = xa;
696 xc = xc0;
697 carry = 0;
698 do {
699 z = *x++ * y + *xc + carry;
700 carry = z >> 16;
701 *xc++ = z & 0xffff;
702 }
703 while(x < xae);
704 *xc = carry;
705 }
706 }
707 #endif
708 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
709 c->wds = wc;
710 return c;
711 }
712
713 static Bigint *p5s;
714
715 static Bigint *
716 pow5mult
717 #ifdef KR_headers
718 (b, k) Bigint *b; int k;
719 #else
720 (Bigint *b, int k)
721 #endif
722 {
723 Bigint *b1, *p5, *p51;
724 int i;
725 static int p05[3] = { 5, 25, 125 };
726
727 if (i = k & 3)
728 b = multadd(b, p05[i-1], 0);
729
730 if (!(k >>= 2))
731 return b;
732 if (!(p5 = p5s)) {
733 /* first time */
734 p5 = p5s = i2b(625);
735 p5->next = 0;
736 }
737 for(;;) {
738 if (k & 1) {
739 b1 = mult(b, p5);
740 Bfree(b);
741 b = b1;
742 }
743 if (!(k >>= 1))
744 break;
745 if (!(p51 = p5->next)) {
746 p51 = p5->next = mult(p5,p5);
747 p51->next = 0;
748 }
749 p5 = p51;
750 }
751 return b;
752 }
753
754 static Bigint *
755 lshift
756 #ifdef KR_headers
757 (b, k) Bigint *b; int k;
758 #else
759 (Bigint *b, int k)
760 #endif
761 {
762 int i, k1, n, n1;
763 Bigint *b1;
764 ULong *x, *x1, *xe, z;
765
766 #ifdef Pack_32
767 n = k >> 5;
768 #else
769 n = k >> 4;
770 #endif
771 k1 = b->k;
772 n1 = n + b->wds + 1;
773 for(i = b->maxwds; n1 > i; i <<= 1)
774 k1++;
775 b1 = Balloc(k1);
776 x1 = b1->x;
777 for(i = 0; i < n; i++)
778 *x1++ = 0;
779 x = b->x;
780 xe = x + b->wds;
781 #ifdef Pack_32
782 if (k &= 0x1f) {
783 k1 = 32 - k;
784 z = 0;
785 do {
786 *x1++ = *x << k | z;
787 z = *x++ >> k1;
788 }
789 while(x < xe);
790 if (*x1 = z)
791 ++n1;
792 }
793 #else
794 if (k &= 0xf) {
795 k1 = 16 - k;
796 z = 0;
797 do {
798 *x1++ = *x << k & 0xffff | z;
799 z = *x++ >> k1;
800 }
801 while(x < xe);
802 if (*x1 = z)
803 ++n1;
804 }
805 #endif
806 else do
807 *x1++ = *x++;
808 while(x < xe);
809 b1->wds = n1 - 1;
810 Bfree(b);
811 return b1;
812 }
813
814 static int
815 cmp
816 #ifdef KR_headers
817 (a, b) Bigint *a, *b;
818 #else
819 (Bigint *a, Bigint *b)
820 #endif
821 {
822 ULong *xa, *xa0, *xb, *xb0;
823 int i, j;
824
825 i = a->wds;
826 j = b->wds;
827 #ifdef DEBUG
828 if (i > 1 && !a->x[i-1])
829 Bug("cmp called with a->x[a->wds-1] == 0");
830 if (j > 1 && !b->x[j-1])
831 Bug("cmp called with b->x[b->wds-1] == 0");
832 #endif
833 if (i -= j)
834 return i;
835 xa0 = a->x;
836 xa = xa0 + j;
837 xb0 = b->x;
838 xb = xb0 + j;
839 for(;;) {
840 if (*--xa != *--xb)
841 return *xa < *xb ? -1 : 1;
842 if (xa <= xa0)
843 break;
844 }
845 return 0;
846 }
847
848 static Bigint *
849 diff
850 #ifdef KR_headers
851 (a, b) Bigint *a, *b;
852 #else
853 (Bigint *a, Bigint *b)
854 #endif
855 {
856 Bigint *c;
857 int i, wa, wb;
858 Long borrow, y; /* We need signed shifts here. */
859 ULong *xa, *xae, *xb, *xbe, *xc;
860 #ifdef Pack_32
861 Long z;
862 #endif
863
864 i = cmp(a,b);
865 if (!i) {
866 c = Balloc(0);
867 c->wds = 1;
868 c->x[0] = 0;
869 return c;
870 }
871 if (i < 0) {
872 c = a;
873 a = b;
874 b = c;
875 i = 1;
876 }
877 else
878 i = 0;
879 c = Balloc(a->k);
880 c->sign = i;
881 wa = a->wds;
882 xa = a->x;
883 xae = xa + wa;
884 wb = b->wds;
885 xb = b->x;
886 xbe = xb + wb;
887 xc = c->x;
888 borrow = 0;
889 #ifdef Pack_32
890 do {
891 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
892 borrow = y >> 16;
893 Sign_Extend(borrow, y);
894 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
895 borrow = z >> 16;
896 Sign_Extend(borrow, z);
897 Storeinc(xc, z, y);
898 }
899 while(xb < xbe);
900 while(xa < xae) {
901 y = (*xa & 0xffff) + borrow;
902 borrow = y >> 16;
903 Sign_Extend(borrow, y);
904 z = (*xa++ >> 16) + borrow;
905 borrow = z >> 16;
906 Sign_Extend(borrow, z);
907 Storeinc(xc, z, y);
908 }
909 #else
910 do {
911 y = *xa++ - *xb++ + borrow;
912 borrow = y >> 16;
913 Sign_Extend(borrow, y);
914 *xc++ = y & 0xffff;
915 }
916 while(xb < xbe);
917 while(xa < xae) {
918 y = *xa++ + borrow;
919 borrow = y >> 16;
920 Sign_Extend(borrow, y);
921 *xc++ = y & 0xffff;
922 }
923 #endif
924 while(!*--xc)
925 wa--;
926 c->wds = wa;
927 return c;
928 }
929
930 static double
931 ulp
932 #ifdef KR_headers
933 (x) double x;
934 #else
935 (double x)
936 #endif
937 {
938 register Long L;
939 double a;
940
941 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
942 #ifndef Sudden_Underflow
943 if (L > 0) {
944 #endif
945 #ifdef IBM
946 L |= Exp_msk1 >> 4;
947 #endif
948 word0(a) = L;
949 word1(a) = 0;
950 #ifndef Sudden_Underflow
951 }
952 else {
953 L = -L >> Exp_shift;
954 if (L < Exp_shift) {
955 word0(a) = 0x80000 >> L;
956 word1(a) = 0;
957 }
958 else {
959 word0(a) = 0;
960 L -= Exp_shift;
961 word1(a) = L >= 31 ? 1 : 1 << 31 - L;
962 }
963 }
964 #endif
965 return a;
966 }
967
968 static double
969 b2d
970 #ifdef KR_headers
971 (a, e) Bigint *a; int *e;
972 #else
973 (Bigint *a, int *e)
974 #endif
975 {
976 ULong *xa, *xa0, w, y, z;
977 int k;
978 double d;
979 #ifdef VAX
980 ULong d0, d1;
981 #else
982 #define d0 word0(d)
983 #define d1 word1(d)
984 #endif
985
986 xa0 = a->x;
987 xa = xa0 + a->wds;
988 y = *--xa;
989 #ifdef DEBUG
990 if (!y) Bug("zero y in b2d");
991 #endif
992 k = hi0bits(y);
993 *e = 32 - k;
994 #ifdef Pack_32
995 if (k < Ebits) {
996 d0 = Exp_1 | y >> Ebits - k;
997 w = xa > xa0 ? *--xa : 0;
998 d1 = y << (32-Ebits) + k | w >> Ebits - k;
999 goto ret_d;
1000 }
1001 z = xa > xa0 ? *--xa : 0;
1002 if (k -= Ebits) {
1003 d0 = Exp_1 | y << k | z >> 32 - k;
1004 y = xa > xa0 ? *--xa : 0;
1005 d1 = z << k | y >> 32 - k;
1006 }
1007 else {
1008 d0 = Exp_1 | y;
1009 d1 = z;
1010 }
1011 #else
1012 if (k < Ebits + 16) {
1013 z = xa > xa0 ? *--xa : 0;
1014 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1015 w = xa > xa0 ? *--xa : 0;
1016 y = xa > xa0 ? *--xa : 0;
1017 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1018 goto ret_d;
1019 }
1020 z = xa > xa0 ? *--xa : 0;
1021 w = xa > xa0 ? *--xa : 0;
1022 k -= Ebits + 16;
1023 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1024 y = xa > xa0 ? *--xa : 0;
1025 d1 = w << k + 16 | y << k;
1026 #endif
1027 ret_d:
1028 #ifdef VAX
1029 word0(d) = d0 >> 16 | d0 << 16;
1030 word1(d) = d1 >> 16 | d1 << 16;
1031 #else
1032 #undef d0
1033 #undef d1
1034 #endif
1035 return d;
1036 }
1037
1038 static Bigint *
1039 d2b
1040 #ifdef KR_headers
1041 (d, e, bits) double d; int *e, *bits;
1042 #else
1043 (double d, int *e, int *bits)
1044 #endif
1045 {
1046 Bigint *b;
1047 int de, i, k;
1048 ULong *x, y, z;
1049 #ifdef VAX
1050 ULong d0, d1;
1051 d0 = word0(d) >> 16 | word0(d) << 16;
1052 d1 = word1(d) >> 16 | word1(d) << 16;
1053 #else
1054 #define d0 word0(d)
1055 #define d1 word1(d)
1056 #endif
1057
1058 #ifdef Pack_32
1059 b = Balloc(1);
1060 #else
1061 b = Balloc(2);
1062 #endif
1063 x = b->x;
1064
1065 z = d0 & Frac_mask;
1066 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1067 #ifdef Sudden_Underflow
1068 de = (int)(d0 >> Exp_shift);
1069 #ifndef IBM
1070 z |= Exp_msk11;
1071 #endif
1072 #else
1073 if (de = (int)(d0 >> Exp_shift))
1074 z |= Exp_msk1;
1075 #endif
1076 #ifdef Pack_32
1077 if (y = d1) {
1078 if (k = lo0bits(&y)) {
1079 x[0] = y | z << 32 - k;
1080 z >>= k;
1081 }
1082 else
1083 x[0] = y;
1084 i = b->wds = (x[1] = z) ? 2 : 1;
1085 }
1086 else {
1087 #ifdef DEBUG
1088 if (!z)
1089 Bug("Zero passed to d2b");
1090 #endif
1091 k = lo0bits(&z);
1092 x[0] = z;
1093 i = b->wds = 1;
1094 k += 32;
1095 }
1096 #else
1097 if (y = d1) {
1098 if (k = lo0bits(&y))
1099 if (k >= 16) {
1100 x[0] = y | z << 32 - k & 0xffff;
1101 x[1] = z >> k - 16 & 0xffff;
1102 x[2] = z >> k;
1103 i = 2;
1104 }
1105 else {
1106 x[0] = y & 0xffff;
1107 x[1] = y >> 16 | z << 16 - k & 0xffff;
1108 x[2] = z >> k & 0xffff;
1109 x[3] = z >> k+16;
1110 i = 3;
1111 }
1112 else {
1113 x[0] = y & 0xffff;
1114 x[1] = y >> 16;
1115 x[2] = z & 0xffff;
1116 x[3] = z >> 16;
1117 i = 3;
1118 }
1119 }
1120 else {
1121 #ifdef DEBUG
1122 if (!z)
1123 Bug("Zero passed to d2b");
1124 #endif
1125 k = lo0bits(&z);
1126 if (k >= 16) {
1127 x[0] = z;
1128 i = 0;
1129 }
1130 else {
1131 x[0] = z & 0xffff;
1132 x[1] = z >> 16;
1133 i = 1;
1134 }
1135 k += 32;
1136 }
1137 while(!x[i])
1138 --i;
1139 b->wds = i + 1;
1140 #endif
1141 #ifndef Sudden_Underflow
1142 if (de) {
1143 #endif
1144 #ifdef IBM
1145 *e = (de - Bias - (P-1) << 2) + k;
1146 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1147 #else
1148 *e = de - Bias - (P-1) + k;
1149 *bits = P - k;
1150 #endif
1151 #ifndef Sudden_Underflow
1152 }
1153 else {
1154 *e = de - Bias - (P-1) + 1 + k;
1155 #ifdef Pack_32
1156 *bits = 32*i - hi0bits(x[i-1]);
1157 #else
1158 *bits = (i+2)*16 - hi0bits(x[i]);
1159 #endif
1160 }
1161 #endif
1162 return b;
1163 }
1164 #undef d0
1165 #undef d1
1166
1167 static double
1168 ratio
1169 #ifdef KR_headers
1170 (a, b) Bigint *a, *b;
1171 #else
1172 (Bigint *a, Bigint *b)
1173 #endif
1174 {
1175 double da, db;
1176 int k, ka, kb;
1177
1178 da = b2d(a, &ka);
1179 db = b2d(b, &kb);
1180 #ifdef Pack_32
1181 k = ka - kb + 32*(a->wds - b->wds);
1182 #else
1183 k = ka - kb + 16*(a->wds - b->wds);
1184 #endif
1185 #ifdef IBM
1186 if (k > 0) {
1187 word0(da) += (k >> 2)*Exp_msk1;
1188 if (k &= 3)
1189 da *= 1 << k;
1190 }
1191 else {
1192 k = -k;
1193 word0(db) += (k >> 2)*Exp_msk1;
1194 if (k &= 3)
1195 db *= 1 << k;
1196 }
1197 #else
1198 if (k > 0)
1199 word0(da) += k*Exp_msk1;
1200 else {
1201 k = -k;
1202 word0(db) += k*Exp_msk1;
1203 }
1204 #endif
1205 return da / db;
1206 }
1207
1208 static CONST double
1209 tens[] = {
1210 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1211 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1212 1e20, 1e21, 1e22
1213 #ifdef VAX
1214 , 1e23, 1e24
1215 #endif
1216 };
1217
1218 #ifdef IEEE_Arith
1219 static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1220 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1221 #define n_bigtens 5
1222 #else
1223 #ifdef IBM
1224 static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
1225 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1226 #define n_bigtens 3
1227 #else
1228 static CONST double bigtens[] = { 1e16, 1e32 };
1229 static CONST double tinytens[] = { 1e-16, 1e-32 };
1230 #define n_bigtens 2
1231 #endif
1232 #endif
1233
1234 double
1235 strtod
1236 #ifdef KR_headers
1237 (s00, se) CONST char *s00; char **se;
1238 #else
1239 (CONST char *s00, char **se)
1240 #endif
1241 {
1242 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1243 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1244 CONST char *s, *s0, *s1;
1245 double aadj, aadj1, adj, rv, rv0;
1246 Long L;
1247 ULong y, z;
1248 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1249
1250 #ifndef KR_headers
1251 CONST char decimal_point = localeconv()->decimal_point[0];
1252 #else
1253 CONST char decimal_point = '.';
1254 #endif
1255
1256 sign = nz0 = nz = 0;
1257 rv = 0.;
1258
1259
1260 for(s = s00; isspace((unsigned char) *s); s++)
1261 ;
1262
1263 if (*s == '-') {
1264 sign = 1;
1265 s++;
1266 } else if (*s == '+') {
1267 s++;
1268 }
1269
1270 if (*s == '\0') {
1271 s = s00;
1272 goto ret;
1273 }
1274
1275 if (*s == '0') {
1276 nz0 = 1;
1277 while(*++s == '0') ;
1278 if (!*s)
1279 goto ret;
1280 }
1281 s0 = s;
1282 y = z = 0;
1283 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1284 if (nd < 9)
1285 y = 10*y + c - '0';
1286 else if (nd < 16)
1287 z = 10*z + c - '0';
1288 nd0 = nd;
1289 if (c == decimal_point) {
1290 c = *++s;
1291 if (!nd) {
1292 for(; c == '0'; c = *++s)
1293 nz++;
1294 if (c > '0' && c <= '9') {
1295 s0 = s;
1296 nf += nz;
1297 nz = 0;
1298 goto have_dig;
1299 }
1300 goto dig_done;
1301 }
1302 for(; c >= '0' && c <= '9'; c = *++s) {
1303 have_dig:
1304 nz++;
1305 if (c -= '0') {
1306 nf += nz;
1307 for(i = 1; i < nz; i++)
1308 if (nd++ < 9)
1309 y *= 10;
1310 else if (nd <= DBL_DIG + 1)
1311 z *= 10;
1312 if (nd++ < 9)
1313 y = 10*y + c;
1314 else if (nd <= DBL_DIG + 1)
1315 z = 10*z + c;
1316 nz = 0;
1317 }
1318 }
1319 }
1320 dig_done:
1321 e = 0;
1322 if (c == 'e' || c == 'E') {
1323 if (!nd && !nz && !nz0) {
1324 s = s00;
1325 goto ret;
1326 }
1327 s00 = s;
1328 esign = 0;
1329 switch(c = *++s) {
1330 case '-':
1331 esign = 1;
1332 case '+':
1333 c = *++s;
1334 }
1335 if (c >= '0' && c <= '9') {
1336 while(c == '0')
1337 c = *++s;
1338 if (c > '0' && c <= '9') {
1339 L = c - '0';
1340 s1 = s;
1341 while((c = *++s) >= '0' && c <= '9')
1342 L = 10*L + c - '0';
1343 if (s - s1 > 8 || L > 19999)
1344 /* Avoid confusion from exponents
1345 * so large that e might overflow.
1346 */
1347 e = 19999; /* safe for 16 bit ints */
1348 else
1349 e = (int)L;
1350 if (esign)
1351 e = -e;
1352 }
1353 else
1354 e = 0;
1355 }
1356 else
1357 s = s00;
1358 }
1359 if (!nd) {
1360 if (!nz && !nz0)
1361 s = s00;
1362 goto ret;
1363 }
1364 e1 = e -= nf;
1365
1366 /* Now we have nd0 digits, starting at s0, followed by a
1367 * decimal point, followed by nd-nd0 digits. The number we're
1368 * after is the integer represented by those digits times
1369 * 10**e */
1370
1371 if (!nd0)
1372 nd0 = nd;
1373 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1374 rv = y;
1375 if (k > 9)
1376 rv = tens[k - 9] * rv + z;
1377 bd0 = 0;
1378 if (nd <= DBL_DIG
1379 #ifndef RND_PRODQUOT
1380 && FLT_ROUNDS == 1
1381 #endif
1382 ) {
1383 if (!e)
1384 goto ret;
1385 if (e > 0) {
1386 if (e <= Ten_pmax) {
1387 #ifdef VAX
1388 goto vax_ovfl_check;
1389 #else
1390 /* rv = */ rounded_product(rv, tens[e]);
1391 goto ret;
1392 #endif
1393 }
1394 i = DBL_DIG - nd;
1395 if (e <= Ten_pmax + i) {
1396 /* A fancier test would sometimes let us do
1397 * this for larger i values.
1398 */
1399 e -= i;
1400 rv *= tens[i];
1401 #ifdef VAX
1402 /* VAX exponent range is so narrow we must
1403 * worry about overflow here...
1404 */
1405 vax_ovfl_check:
1406 word0(rv) -= P*Exp_msk1;
1407 /* rv = */ rounded_product(rv, tens[e]);
1408 if ((word0(rv) & Exp_mask)
1409 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1410 goto ovfl;
1411 word0(rv) += P*Exp_msk1;
1412 #else
1413 /* rv = */ rounded_product(rv, tens[e]);
1414 #endif
1415 goto ret;
1416 }
1417 }
1418 #ifndef Inaccurate_Divide
1419 else if (e >= -Ten_pmax) {
1420 /* rv = */ rounded_quotient(rv, tens[-e]);
1421 goto ret;
1422 }
1423 #endif
1424 }
1425 e1 += nd - k;
1426
1427 /* Get starting approximation = rv * 10**e1 */
1428
1429 if (e1 > 0) {
1430 if (i = e1 & 15)
1431 rv *= tens[i];
1432 if (e1 &= ~15) {
1433 if (e1 > DBL_MAX_10_EXP) {
1434 ovfl:
1435 errno = ERANGE;
1436 #ifdef __STDC__
1437 rv = HUGE_VAL;
1438 #else
1439 /* Can't trust HUGE_VAL */
1440 #ifdef IEEE_Arith
1441 word0(rv) = Exp_mask;
1442 word1(rv) = 0;
1443 #else
1444 word0(rv) = Big0;
1445 word1(rv) = Big1;
1446 #endif
1447 #endif
1448 if (bd0)
1449 goto retfree;
1450 goto ret;
1451 }
1452 if (e1 >>= 4) {
1453 for(j = 0; e1 > 1; j++, e1 >>= 1)
1454 if (e1 & 1)
1455 rv *= bigtens[j];
1456 /* The last multiplication could overflow. */
1457 word0(rv) -= P*Exp_msk1;
1458 rv *= bigtens[j];
1459 if ((z = word0(rv) & Exp_mask)
1460 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1461 goto ovfl;
1462 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1463 /* set to largest number */
1464 /* (Can't trust DBL_MAX) */
1465 word0(rv) = Big0;
1466 word1(rv) = Big1;
1467 }
1468 else
1469 word0(rv) += P*Exp_msk1;
1470 }
1471
1472 }
1473 }
1474 else if (e1 < 0) {
1475 e1 = -e1;
1476 if (i = e1 & 15)
1477 rv /= tens[i];
1478 if (e1 &= ~15) {
1479 e1 >>= 4;
1480 if (e1 >= 1 << n_bigtens)
1481 goto undfl;
1482 for(j = 0; e1 > 1; j++, e1 >>= 1)
1483 if (e1 & 1)
1484 rv *= tinytens[j];
1485 /* The last multiplication could underflow. */
1486 rv0 = rv;
1487 rv *= tinytens[j];
1488 if (!rv) {
1489 rv = 2.*rv0;
1490 rv *= tinytens[j];
1491 if (!rv) {
1492 undfl:
1493 rv = 0.;
1494 errno = ERANGE;
1495 if (bd0)
1496 goto retfree;
1497 goto ret;
1498 }
1499 word0(rv) = Tiny0;
1500 word1(rv) = Tiny1;
1501 /* The refinement below will clean
1502 * this approximation up.
1503 */
1504 }
1505 }
1506 }
1507
1508 /* Now the hard part -- adjusting rv to the correct value.*/
1509
1510 /* Put digits into bd: true value = bd * 10^e */
1511
1512 bd0 = s2b(s0, nd0, nd, y);
1513
1514 for(;;) {
1515 bd = Balloc(bd0->k);
1516 Bcopy(bd, bd0);
1517 bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
1518 bs = i2b(1);
1519
1520 if (e >= 0) {
1521 bb2 = bb5 = 0;
1522 bd2 = bd5 = e;
1523 }
1524 else {
1525 bb2 = bb5 = -e;
1526 bd2 = bd5 = 0;
1527 }
1528 if (bbe >= 0)
1529 bb2 += bbe;
1530 else
1531 bd2 -= bbe;
1532 bs2 = bb2;
1533 #ifdef Sudden_Underflow
1534 #ifdef IBM
1535 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1536 #else
1537 j = P + 1 - bbbits;
1538 #endif
1539 #else
1540 i = bbe + bbbits - 1; /* logb(rv) */
1541 if (i < Emin) /* denormal */
1542 j = bbe + (P-Emin);
1543 else
1544 j = P + 1 - bbbits;
1545 #endif
1546 bb2 += j;
1547 bd2 += j;
1548 i = bb2 < bd2 ? bb2 : bd2;
1549 if (i > bs2)
1550 i = bs2;
1551 if (i > 0) {
1552 bb2 -= i;
1553 bd2 -= i;
1554 bs2 -= i;
1555 }
1556 if (bb5 > 0) {
1557 bs = pow5mult(bs, bb5);
1558 bb1 = mult(bs, bb);
1559 Bfree(bb);
1560 bb = bb1;
1561 }
1562 if (bb2 > 0)
1563 bb = lshift(bb, bb2);
1564 if (bd5 > 0)
1565 bd = pow5mult(bd, bd5);
1566 if (bd2 > 0)
1567 bd = lshift(bd, bd2);
1568 if (bs2 > 0)
1569 bs = lshift(bs, bs2);
1570 delta = diff(bb, bd);
1571 dsign = delta->sign;
1572 delta->sign = 0;
1573 i = cmp(delta, bs);
1574 if (i < 0) {
1575 /* Error is less than half an ulp -- check for
1576 * special case of mantissa a power of two.
1577 */
1578 if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1579 break;
1580 delta = lshift(delta,Log2P);
1581 if (cmp(delta, bs) > 0)
1582 goto drop_down;
1583 break;
1584 }
1585 if (i == 0) {
1586 /* exactly half-way between */
1587 if (dsign) {
1588 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
1589 && word1(rv) == 0xffffffff) {
1590 /*boundary case -- increment exponent*/
1591 word0(rv) = (word0(rv) & Exp_mask)
1592 + Exp_msk1
1593 #ifdef IBM
1594 | Exp_msk1 >> 4
1595 #endif
1596 ;
1597 word1(rv) = 0;
1598 break;
1599 }
1600 }
1601 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
1602 drop_down:
1603 /* boundary case -- decrement exponent */
1604 #ifdef Sudden_Underflow
1605 L = word0(rv) & Exp_mask;
1606 #ifdef IBM
1607 if (L < Exp_msk1)
1608 #else
1609 if (L <= Exp_msk1)
1610 #endif
1611 goto undfl;
1612 L -= Exp_msk1;
1613 #else
1614 L = (word0(rv) & Exp_mask) - Exp_msk1;
1615 #endif
1616 word0(rv) = L | Bndry_mask1;
1617 word1(rv) = 0xffffffff;
1618 #ifdef IBM
1619 goto cont;
1620 #else
1621 break;
1622 #endif
1623 }
1624 #ifndef ROUND_BIASED
1625 if (!(word1(rv) & LSB))
1626 break;
1627 #endif
1628 if (dsign)
1629 rv += ulp(rv);
1630 #ifndef ROUND_BIASED
1631 else {
1632 rv -= ulp(rv);
1633 #ifndef Sudden_Underflow
1634 if (!rv)
1635 goto undfl;
1636 #endif
1637 }
1638 #endif
1639 break;
1640 }
1641 if ((aadj = ratio(delta, bs)) <= 2.) {
1642 if (dsign)
1643 aadj = aadj1 = 1.;
1644 else if (word1(rv) || word0(rv) & Bndry_mask) {
1645 #ifndef Sudden_Underflow
1646 if (word1(rv) == Tiny1 && !word0(rv))
1647 goto undfl;
1648 #endif
1649 aadj = 1.;
1650 aadj1 = -1.;
1651 }
1652 else {
1653 /* special case -- power of FLT_RADIX to be */
1654 /* rounded down... */
1655
1656 if (aadj < 2./FLT_RADIX)
1657 aadj = 1./FLT_RADIX;
1658 else
1659 aadj *= 0.5;
1660 aadj1 = -aadj;
1661 }
1662 }
1663 else {
1664 aadj *= 0.5;
1665 aadj1 = dsign ? aadj : -aadj;
1666 #ifdef Check_FLT_ROUNDS
1667 switch(FLT_ROUNDS) {
1668 case 2: /* towards +infinity */
1669 aadj1 -= 0.5;
1670 break;
1671 case 0: /* towards 0 */
1672 case 3: /* towards -infinity */
1673 aadj1 += 0.5;
1674 }
1675 #else
1676 if (FLT_ROUNDS == 0)
1677 aadj1 += 0.5;
1678 #endif
1679 }
1680 y = word0(rv) & Exp_mask;
1681
1682 /* Check for overflow */
1683
1684 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1685 rv0 = rv;
1686 word0(rv) -= P*Exp_msk1;
1687 adj = aadj1 * ulp(rv);
1688 rv += adj;
1689 if ((word0(rv) & Exp_mask) >=
1690 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1691 if (word0(rv0) == Big0 && word1(rv0) == Big1)
1692 goto ovfl;
1693 word0(rv) = Big0;
1694 word1(rv) = Big1;
1695 goto cont;
1696 }
1697 else
1698 word0(rv) += P*Exp_msk1;
1699 }
1700 else {
1701 #ifdef Sudden_Underflow
1702 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1703 rv0 = rv;
1704 word0(rv) += P*Exp_msk1;
1705 adj = aadj1 * ulp(rv);
1706 rv += adj;
1707 #ifdef IBM
1708 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
1709 #else
1710 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
1711 #endif
1712 {
1713 if (word0(rv0) == Tiny0
1714 && word1(rv0) == Tiny1)
1715 goto undfl;
1716 word0(rv) = Tiny0;
1717 word1(rv) = Tiny1;
1718 goto cont;
1719 }
1720 else
1721 word0(rv) -= P*Exp_msk1;
1722 }
1723 else {
1724 adj = aadj1 * ulp(rv);
1725 rv += adj;
1726 }
1727 #else
1728 /* Compute adj so that the IEEE rounding rules will
1729 * correctly round rv + adj in some half-way cases.
1730 * If rv * ulp(rv) is denormalized (i.e.,
1731 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1732 * trouble from bits lost to denormalization;
1733 * example: 1.2e-307 .
1734 */
1735 if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
1736 aadj1 = (double)(int)(aadj + 0.5);
1737 if (!dsign)
1738 aadj1 = -aadj1;
1739 }
1740 adj = aadj1 * ulp(rv);
1741 rv += adj;
1742 #endif
1743 }
1744 z = word0(rv) & Exp_mask;
1745 if (y == z) {
1746 /* Can we stop now? */
1747 L = aadj;
1748 aadj -= L;
1749 /* The tolerances below are conservative. */
1750 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
1751 if (aadj < .4999999 || aadj > .5000001)
1752 break;
1753 }
1754 else if (aadj < .4999999/FLT_RADIX)
1755 break;
1756 }
1757 cont:
1758 Bfree(bb);
1759 Bfree(bd);
1760 Bfree(bs);
1761 Bfree(delta);
1762 }
1763 retfree:
1764 Bfree(bb);
1765 Bfree(bd);
1766 Bfree(bs);
1767 Bfree(bd0);
1768 Bfree(delta);
1769 ret:
1770 if (se)
1771 *se = (char *)s;
1772 return sign ? -rv : rv;
1773 }
1774
1775 static int
1776 quorem
1777 #ifdef KR_headers
1778 (b, S) Bigint *b, *S;
1779 #else
1780 (Bigint *b, Bigint *S)
1781 #endif
1782 {
1783 int n;
1784 Long borrow, y;
1785 ULong carry, q, ys;
1786 ULong *bx, *bxe, *sx, *sxe;
1787 #ifdef Pack_32
1788 Long z;
1789 ULong si, zs;
1790 #endif
1791
1792 n = S->wds;
1793 #ifdef DEBUG
1794 /*debug*/ if (b->wds > n)
1795 /*debug*/ Bug("oversize b in quorem");
1796 #endif
1797 if (b->wds < n)
1798 return 0;
1799 sx = S->x;
1800 sxe = sx + --n;
1801 bx = b->x;
1802 bxe = bx + n;
1803 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1804 #ifdef DEBUG
1805 /*debug*/ if (q > 9)
1806 /*debug*/ Bug("oversized quotient in quorem");
1807 #endif
1808 if (q) {
1809 borrow = 0;
1810 carry = 0;
1811 do {
1812 #ifdef Pack_32
1813 si = *sx++;
1814 ys = (si & 0xffff) * q + carry;
1815 zs = (si >> 16) * q + (ys >> 16);
1816 carry = zs >> 16;
1817 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1818 borrow = y >> 16;
1819 Sign_Extend(borrow, y);
1820 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1821 borrow = z >> 16;
1822 Sign_Extend(borrow, z);
1823 Storeinc(bx, z, y);
1824 #else
1825 ys = *sx++ * q + carry;
1826 carry = ys >> 16;
1827 y = *bx - (ys & 0xffff) + borrow;
1828 borrow = y >> 16;
1829 Sign_Extend(borrow, y);
1830 *bx++ = y & 0xffff;
1831 #endif
1832 }
1833 while(sx <= sxe);
1834 if (!*bxe) {
1835 bx = b->x;
1836 while(--bxe > bx && !*bxe)
1837 --n;
1838 b->wds = n;
1839 }
1840 }
1841 if (cmp(b, S) >= 0) {
1842 q++;
1843 borrow = 0;
1844 carry = 0;
1845 bx = b->x;
1846 sx = S->x;
1847 do {
1848 #ifdef Pack_32
1849 si = *sx++;
1850 ys = (si & 0xffff) + carry;
1851 zs = (si >> 16) + (ys >> 16);
1852 carry = zs >> 16;
1853 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1854 borrow = y >> 16;
1855 Sign_Extend(borrow, y);
1856 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1857 borrow = z >> 16;
1858 Sign_Extend(borrow, z);
1859 Storeinc(bx, z, y);
1860 #else
1861 ys = *sx++ + carry;
1862 carry = ys >> 16;
1863 y = *bx - (ys & 0xffff) + borrow;
1864 borrow = y >> 16;
1865 Sign_Extend(borrow, y);
1866 *bx++ = y & 0xffff;
1867 #endif
1868 }
1869 while(sx <= sxe);
1870 bx = b->x;
1871 bxe = bx + n;
1872 if (!*bxe) {
1873 while(--bxe > bx && !*bxe)
1874 --n;
1875 b->wds = n;
1876 }
1877 }
1878 return q;
1879 }
1880
1881 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1882 *
1883 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1884 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1885 *
1886 * Modifications:
1887 * 1. Rather than iterating, we use a simple numeric overestimate
1888 * to determine k = floor(log10(d)). We scale relevant
1889 * quantities using O(log2(k)) rather than O(k) multiplications.
1890 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1891 * try to generate digits strictly left to right. Instead, we
1892 * compute with fewer bits and propagate the carry if necessary
1893 * when rounding the final digit up. This is often faster.
1894 * 3. Under the assumption that input will be rounded nearest,
1895 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1896 * That is, we allow equality in stopping tests when the
1897 * round-nearest rule will give the same floating-point value
1898 * as would satisfaction of the stopping test with strict
1899 * inequality.
1900 * 4. We remove common factors of powers of 2 from relevant
1901 * quantities.
1902 * 5. When converting floating-point integers less than 1e16,
1903 * we use floating-point arithmetic rather than resorting
1904 * to multiple-precision integers.
1905 * 6. When asked to produce fewer than 15 digits, we first try
1906 * to get by with floating-point arithmetic; we resort to
1907 * multiple-precision integer arithmetic only if we cannot
1908 * guarantee that the floating-point calculation has given
1909 * the correctly rounded result. For k requested digits and
1910 * "uniformly" distributed input, the probability is
1911 * something like 10^(k-15) that we must resort to the Long
1912 * calculation.
1913 */
1914
1915 char *
1916 __dtoa
1917 #ifdef KR_headers
1918 (d, mode, ndigits, decpt, sign, rve)
1919 double d; int mode, ndigits, *decpt, *sign; char **rve;
1920 #else
1921 (double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
1922 #endif
1923 {
1924 /* Arguments ndigits, decpt, sign are similar to those
1925 of ecvt and fcvt; trailing zeros are suppressed from
1926 the returned string. If not null, *rve is set to point
1927 to the end of the return value. If d is +-Infinity or NaN,
1928 then *decpt is set to 9999.
1929
1930 mode:
1931 0 ==> shortest string that yields d when read in
1932 and rounded to nearest.
1933 1 ==> like 0, but with Steele & White stopping rule;
1934 e.g. with IEEE P754 arithmetic , mode 0 gives
1935 1e23 whereas mode 1 gives 9.999999999999999e22.
1936 2 ==> max(1,ndigits) significant digits. This gives a
1937 return value similar to that of ecvt, except
1938 that trailing zeros are suppressed.
1939 3 ==> through ndigits past the decimal point. This
1940 gives a return value similar to that from fcvt,
1941 except that trailing zeros are suppressed, and
1942 ndigits can be negative.
1943 4-9 should give the same return values as 2-3, i.e.,
1944 4 <= mode <= 9 ==> same return as mode
1945 2 + (mode & 1). These modes are mainly for
1946 debugging; often they run slower but sometimes
1947 faster than modes 2-3.
1948 4,5,8,9 ==> left-to-right digit generation.
1949 6-9 ==> don't try fast floating-point estimate
1950 (if applicable).
1951
1952 Values of mode other than 0-9 are treated as mode 0.
1953
1954 Sufficient space is allocated to the return value
1955 to hold the suppressed trailing zeros.
1956 */
1957
1958 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
1959 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1960 spec_case, try_quick;
1961 Long L;
1962 #ifndef Sudden_Underflow
1963 int denorm;
1964 ULong x;
1965 #endif
1966 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1967 double d2, ds, eps;
1968 char *s, *s0;
1969 static Bigint *result;
1970 static int result_k;
1971
1972 if (result) {
1973 result->k = result_k;
1974 result->maxwds = 1 << result_k;
1975 Bfree(result);
1976 result = 0;
1977 }
1978
1979 if (word0(d) & Sign_bit) {
1980 /* set sign for everything, including 0's and NaNs */
1981 *sign = 1;
1982 word0(d) &= ~Sign_bit; /* clear sign bit */
1983 }
1984 else
1985 *sign = 0;
1986
1987 #if defined(IEEE_Arith) + defined(VAX)
1988 #ifdef IEEE_Arith
1989 if ((word0(d) & Exp_mask) == Exp_mask)
1990 #else
1991 if (word0(d) == 0x8000)
1992 #endif
1993 {
1994 /* Infinity or NaN */
1995 *decpt = 9999;
1996 s =
1997 #ifdef IEEE_Arith
1998 !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
1999 #endif
2000 "NaN";
2001 if (rve)
2002 *rve =
2003 #ifdef IEEE_Arith
2004 s[3] ? s + 8 :
2005 #endif
2006 s + 3;
2007 return s;
2008 }
2009 #endif
2010 #ifdef IBM
2011 d += 0; /* normalize */
2012 #endif
2013 if (!d) {
2014 *decpt = 1;
2015 s = "0";
2016 if (rve)
2017 *rve = s + 1;
2018 return s;
2019 }
2020
2021 b = d2b(d, &be, &bbits);
2022 #ifdef Sudden_Underflow
2023 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2024 #else
2025 if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) {
2026 #endif
2027 d2 = d;
2028 word0(d2) &= Frac_mask1;
2029 word0(d2) |= Exp_11;
2030 #ifdef IBM
2031 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2032 d2 /= 1 << j;
2033 #endif
2034
2035 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2036 * log10(x) = log(x) / log(10)
2037 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2038 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2039 *
2040 * This suggests computing an approximation k to log10(d) by
2041 *
2042 * k = (i - Bias)*0.301029995663981
2043 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2044 *
2045 * We want k to be too large rather than too small.
2046 * The error in the first-order Taylor series approximation
2047 * is in our favor, so we just round up the constant enough
2048 * to compensate for any error in the multiplication of
2049 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2050 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2051 * adding 1e-13 to the constant term more than suffices.
2052 * Hence we adjust the constant term to 0.1760912590558.
2053 * (We could get a more accurate k by invoking log10,
2054 * but this is probably not worthwhile.)
2055 */
2056
2057 i -= Bias;
2058 #ifdef IBM
2059 i <<= 2;
2060 i += j;
2061 #endif
2062 #ifndef Sudden_Underflow
2063 denorm = 0;
2064 }
2065 else {
2066 /* d is denormalized */
2067
2068 i = bbits + be + (Bias + (P-1) - 1);
2069 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32
2070 : word1(d) << 32 - i;
2071 d2 = x;
2072 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2073 i -= (Bias + (P-1) - 1) + 1;
2074 denorm = 1;
2075 }
2076 #endif
2077 ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2078 k = (int)ds;
2079 if (ds < 0. && ds != k)
2080 k--; /* want k = floor(ds) */
2081 k_check = 1;
2082 if (k >= 0 && k <= Ten_pmax) {
2083 if (d < tens[k])
2084 k--;
2085 k_check = 0;
2086 }
2087 j = bbits - i - 1;
2088 if (j >= 0) {
2089 b2 = 0;
2090 s2 = j;
2091 }
2092 else {
2093 b2 = -j;
2094 s2 = 0;
2095 }
2096 if (k >= 0) {
2097 b5 = 0;
2098 s5 = k;
2099 s2 += k;
2100 }
2101 else {
2102 b2 -= k;
2103 b5 = -k;
2104 s5 = 0;
2105 }
2106 if (mode < 0 || mode > 9)
2107 mode = 0;
2108 try_quick = 1;
2109 if (mode > 5) {
2110 mode -= 4;
2111 try_quick = 0;
2112 }
2113 leftright = 1;
2114 switch(mode) {
2115 case 0:
2116 case 1:
2117 ilim = ilim1 = -1;
2118 i = 18;
2119 ndigits = 0;
2120 break;
2121 case 2:
2122 leftright = 0;
2123 /* no break */
2124 case 4:
2125 if (ndigits <= 0)
2126 ndigits = 1;
2127 ilim = ilim1 = i = ndigits;
2128 break;
2129 case 3:
2130 leftright = 0;
2131 /* no break */
2132 case 5:
2133 i = ndigits + k + 1;
2134 ilim = i;
2135 ilim1 = i - 1;
2136 if (i <= 0)
2137 i = 1;
2138 }
2139 j = sizeof(ULong);
2140 for(result_k = 0; sizeof(Bigint) - sizeof(ULong) + j <= i;
2141 j <<= 1) result_k++;
2142 result = Balloc(result_k);
2143 s = s0 = (char *)result;
2144
2145 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2146
2147 /* Try to get by with floating-point arithmetic. */
2148
2149 i = 0;
2150 d2 = d;
2151 k0 = k;
2152 ilim0 = ilim;
2153 ieps = 2; /* conservative */
2154 if (k > 0) {
2155 ds = tens[k&0xf];
2156 j = k >> 4;
2157 if (j & Bletch) {
2158 /* prevent overflows */
2159 j &= Bletch - 1;
2160 d /= bigtens[n_bigtens-1];
2161 ieps++;
2162 }
2163 for(; j; j >>= 1, i++)
2164 if (j & 1) {
2165 ieps++;
2166 ds *= bigtens[i];
2167 }
2168 d /= ds;
2169 }
2170 else if (j1 = -k) {
2171 d *= tens[j1 & 0xf];
2172 for(j = j1 >> 4; j; j >>= 1, i++)
2173 if (j & 1) {
2174 ieps++;
2175 d *= bigtens[i];
2176 }
2177 }
2178 if (k_check && d < 1. && ilim > 0) {
2179 if (ilim1 <= 0)
2180 goto fast_failed;
2181 ilim = ilim1;
2182 k--;
2183 d *= 10.;
2184 ieps++;
2185 }
2186 eps = ieps*d + 7.;
2187 word0(eps) -= (P-1)*Exp_msk1;
2188 if (ilim == 0) {
2189 S = mhi = 0;
2190 d -= 5.;
2191 if (d > eps)
2192 goto one_digit;
2193 if (d < -eps)
2194 goto no_digits;
2195 goto fast_failed;
2196 }
2197 #ifndef No_leftright
2198 if (leftright) {
2199 /* Use Steele & White method of only
2200 * generating digits needed.
2201 */
2202 eps = 0.5/tens[ilim-1] - eps;
2203 for(i = 0;;) {
2204 L = d;
2205 d -= L;
2206 *s++ = '0' + (int)L;
2207 if (d < eps)
2208 goto ret1;
2209 if (1. - d < eps)
2210 goto bump_up;
2211 if (++i >= ilim)
2212 break;
2213 eps *= 10.;
2214 d *= 10.;
2215 }
2216 }
2217 else {
2218 #endif
2219 /* Generate ilim digits, then fix them up. */
2220 eps *= tens[ilim-1];
2221 for(i = 1;; i++, d *= 10.) {
2222 L = d;
2223 d -= L;
2224 *s++ = '0' + (int)L;
2225 if (i == ilim) {
2226 if (d > 0.5 + eps)
2227 goto bump_up;
2228 else if (d < 0.5 - eps) {
2229 while(*--s == '0');
2230 s++;
2231 goto ret1;
2232 }
2233 break;
2234 }
2235 }
2236 #ifndef No_leftright
2237 }
2238 #endif
2239 fast_failed:
2240 s = s0;
2241 d = d2;
2242 k = k0;
2243 ilim = ilim0;
2244 }
2245
2246 /* Do we have a "small" integer? */
2247
2248 if (be >= 0 && k <= Int_max) {
2249 /* Yes. */
2250 ds = tens[k];
2251 if (ndigits < 0 && ilim <= 0) {
2252 S = mhi = 0;
2253 if (ilim < 0 || d <= 5*ds)
2254 goto no_digits;
2255 goto one_digit;
2256 }
2257 for(i = 1;; i++) {
2258 L = d / ds;
2259 d -= L*ds;
2260 #ifdef Check_FLT_ROUNDS
2261 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2262 if (d < 0) {
2263 L--;
2264 d += ds;
2265 }
2266 #endif
2267 *s++ = '0' + (int)L;
2268 if (i == ilim) {
2269 d += d;
2270 if (d > ds || d == ds && L & 1) {
2271 bump_up:
2272 while(*--s == '9')
2273 if (s == s0) {
2274 k++;
2275 *s = '0';
2276 break;
2277 }
2278 ++*s++;
2279 }
2280 break;
2281 }
2282 if (!(d *= 10.))
2283 break;
2284 }
2285 goto ret1;
2286 }
2287
2288 m2 = b2;
2289 m5 = b5;
2290 mhi = mlo = 0;
2291 if (leftright) {
2292 if (mode < 2) {
2293 i =
2294 #ifndef Sudden_Underflow
2295 denorm ? be + (Bias + (P-1) - 1 + 1) :
2296 #endif
2297 #ifdef IBM
2298 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2299 #else
2300 1 + P - bbits;
2301 #endif
2302 }
2303 else {
2304 j = ilim - 1;
2305 if (m5 >= j)
2306 m5 -= j;
2307 else {
2308 s5 += j -= m5;
2309 b5 += j;
2310 m5 = 0;
2311 }
2312 if ((i = ilim) < 0) {
2313 m2 -= i;
2314 i = 0;
2315 }
2316 }
2317 b2 += i;
2318 s2 += i;
2319 mhi = i2b(1);
2320 }
2321 if (m2 > 0 && s2 > 0) {
2322 i = m2 < s2 ? m2 : s2;
2323 b2 -= i;
2324 m2 -= i;
2325 s2 -= i;
2326 }
2327 if (b5 > 0) {
2328 if (leftright) {
2329 if (m5 > 0) {
2330 mhi = pow5mult(mhi, m5);
2331 b1 = mult(mhi, b);
2332 Bfree(b);
2333 b = b1;
2334 }
2335 if (j = b5 - m5)
2336 b = pow5mult(b, j);
2337 }
2338 else
2339 b = pow5mult(b, b5);
2340 }
2341 S = i2b(1);
2342 if (s5 > 0)
2343 S = pow5mult(S, s5);
2344
2345 /* Check for special case that d is a normalized power of 2. */
2346
2347 if (mode < 2) {
2348 if (!word1(d) && !(word0(d) & Bndry_mask)
2349 #ifndef Sudden_Underflow
2350 && word0(d) & Exp_mask
2351 #endif
2352 ) {
2353 /* The special case */
2354 b2 += Log2P;
2355 s2 += Log2P;
2356 spec_case = 1;
2357 }
2358 else
2359 spec_case = 0;
2360 }
2361
2362 /* Arrange for convenient computation of quotients:
2363 * shift left if necessary so divisor has 4 leading 0 bits.
2364 *
2365 * Perhaps we should just compute leading 28 bits of S once
2366 * and for all and pass them and a shift to quorem, so it
2367 * can do shifts and ors to compute the numerator for q.
2368 */
2369 #ifdef Pack_32
2370 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)
2371 i = 32 - i;
2372 #else
2373 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
2374 i = 16 - i;
2375 #endif
2376 if (i > 4) {
2377 i -= 4;
2378 b2 += i;
2379 m2 += i;
2380 s2 += i;
2381 }
2382 else if (i < 4) {
2383 i += 28;
2384 b2 += i;
2385 m2 += i;
2386 s2 += i;
2387 }
2388 if (b2 > 0)
2389 b = lshift(b, b2);
2390 if (s2 > 0)
2391 S = lshift(S, s2);
2392 if (k_check) {
2393 if (cmp(b,S) < 0) {
2394 k--;
2395 b = multadd(b, 10, 0); /* we botched the k estimate */
2396 if (leftright)
2397 mhi = multadd(mhi, 10, 0);
2398 ilim = ilim1;
2399 }
2400 }
2401 if (ilim <= 0 && mode > 2) {
2402 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2403 /* no digits, fcvt style */
2404 no_digits:
2405 k = -1 - ndigits;
2406 goto ret;
2407 }
2408 one_digit:
2409 *s++ = '1';
2410 k++;
2411 goto ret;
2412 }
2413 if (leftright) {
2414 if (m2 > 0)
2415 mhi = lshift(mhi, m2);
2416
2417 /* Compute mlo -- check for special case
2418 * that d is a normalized power of 2.
2419 */
2420
2421 mlo = mhi;
2422 if (spec_case) {
2423 mhi = Balloc(mhi->k);
2424 Bcopy(mhi, mlo);
2425 mhi = lshift(mhi, Log2P);
2426 }
2427
2428 for(i = 1;;i++) {
2429 dig = quorem(b,S) + '0';
2430 /* Do we yet have the shortest decimal string
2431 * that will round to d?
2432 */
2433 j = cmp(b, mlo);
2434 delta = diff(S, mhi);
2435 j1 = delta->sign ? 1 : cmp(b, delta);
2436 Bfree(delta);
2437 #ifndef ROUND_BIASED
2438 if (j1 == 0 && !mode && !(word1(d) & 1)) {
2439 if (dig == '9')
2440 goto round_9_up;
2441 if (j > 0)
2442 dig++;
2443 *s++ = dig;
2444 goto ret;
2445 }
2446 #endif
2447 if (j < 0 || j == 0 && !mode
2448 #ifndef ROUND_BIASED
2449 && !(word1(d) & 1)
2450 #endif
2451 ) {
2452 if (j1 > 0) {
2453 b = lshift(b, 1);
2454 j1 = cmp(b, S);
2455 if ((j1 > 0 || j1 == 0 && dig & 1)
2456 && dig++ == '9')
2457 goto round_9_up;
2458 }
2459 *s++ = dig;
2460 goto ret;
2461 }
2462 if (j1 > 0) {
2463 if (dig == '9') { /* possible if i == 1 */
2464 round_9_up:
2465 *s++ = '9';
2466 goto roundoff;
2467 }
2468 *s++ = dig + 1;
2469 goto ret;
2470 }
2471 *s++ = dig;
2472 if (i == ilim)
2473 break;
2474 b = multadd(b, 10, 0);
2475 if (mlo == mhi)
2476 mlo = mhi = multadd(mhi, 10, 0);
2477 else {
2478 mlo = multadd(mlo, 10, 0);
2479 mhi = multadd(mhi, 10, 0);
2480 }
2481 }
2482 }
2483 else
2484 for(i = 1;; i++) {
2485 *s++ = dig = quorem(b,S) + '0';
2486 if (i >= ilim)
2487 break;
2488 b = multadd(b, 10, 0);
2489 }
2490
2491 /* Round off last digit */
2492
2493 b = lshift(b, 1);
2494 j = cmp(b, S);
2495 if (j > 0 || j == 0 && dig & 1) {
2496 roundoff:
2497 while(*--s == '9')
2498 if (s == s0) {
2499 k++;
2500 *s++ = '1';
2501 goto ret;
2502 }
2503 ++*s++;
2504 }
2505 else {
2506 while(*--s == '0');
2507 s++;
2508 }
2509 ret:
2510 Bfree(S);
2511 if (mhi) {
2512 if (mlo && mlo != mhi)
2513 Bfree(mlo);
2514 Bfree(mhi);
2515 }
2516 ret1:
2517 Bfree(b);
2518 if (s == s0) { /* don't return empty string */
2519 *s++ = '0';
2520 k = 0;
2521 }
2522 *s = 0;
2523 *decpt = k + 1;
2524 if (rve)
2525 *rve = s;
2526 return s0;
2527 }
2528 #ifdef __cplusplus
2529 }
2530 #endif