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