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