1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991 by AT&T.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 ***************************************************************/
20 /* Please send bug reports to
22 AT&T Bell Laboratories, Room 2C-463
24 Murray Hill, NJ 07974-2070
26 dmg@research.att.com or research!dmg
29 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
31 * This strtod returns a nearest machine number to the input decimal
32 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
33 * broken by the IEEE round-even rule. Otherwise ties are broken by
34 * biased rounding (add half and chop).
36 * Inspired loosely by William D. Clinger's paper "How to Read Floating
37 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
41 * 1. We only require IEEE, IBM, or VAX double-precision
42 * arithmetic (not IEEE double-extended).
43 * 2. We get by with floating-point arithmetic in a case that
44 * Clinger missed -- when we're computing d * 10^n
45 * for a small integer d and the integer n is not too
46 * much larger than 22 (the maximum integer k for which
47 * we can represent 10^k exactly), we may be able to
48 * compute (d*10^k) * 10^(e-k) with just one roundoff.
49 * 3. Rather than a bit-at-a-time adjustment of the binary
50 * result in the hard case, we use floating-point
51 * arithmetic to determine the adjustment to within
52 * one bit; only in really hard cases do we need to
53 * compute a second residual.
54 * 4. Because of 3., we don't need a large table of powers of 10
55 * for ten-to-e (just some small tables, e.g. of 10^k
60 * #define IEEE_8087 for IEEE-arithmetic machines where the least
61 * significant byte has the lowest address.
62 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
63 * significant byte has the lowest address.
64 * #define Sudden_Underflow for IEEE-format machines without gradual
65 * underflow (i.e., that flush to zero on underflow).
66 * #define IBM for IBM mainframe-style floating-point arithmetic.
67 * #define VAX for VAX-style floating-point arithmetic.
68 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
69 * #define No_leftright to omit left-right logic in fast floating-point
70 * computation of dtoa.
71 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
72 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
73 * that use extended-precision instructions to compute rounded
74 * products and quotients) with IBM.
75 * #define ROUND_BIASED for IEEE-format with biased rounding.
76 * #define Inaccurate_Divide for IEEE-format with correctly rounded
77 * products but inaccurate quotients, e.g., for Intel i860.
78 * #define Just_16 to store 16 bits per 32-bit long when doing high-precision
79 * integer arithmetic. Whether this speeds things up or slows things
80 * down depends on the machine and the number being converted.
85 #include <java-assert.h>
88 /* reent.c knows this value */
93 _DEFUN (Balloc
, (ptr
, k
), struct _Jv_reent
*ptr _AND
int k
)
95 _Jv_Bigint
*rv
= NULL
;
100 JvAssert ((1 << k
) < MAX_BIGNUM_WDS
);
102 while ((ptr
->_allocation_map
& j
) && i
< MAX_BIGNUMS
)
105 JvAssert (i
< MAX_BIGNUMS
);
107 if (i
>= MAX_BIGNUMS
)
110 ptr
->_allocation_map
|= j
;
111 rv
= &ptr
->_freelist
[i
];
121 _DEFUN (Bfree
, (ptr
, v
), struct _Jv_reent
*ptr _AND _Jv_Bigint
* v
)
125 i
= v
- ptr
->_freelist
;
127 JvAssert (i
>= 0 && i
< MAX_BIGNUMS
);
129 if (i
>= 0 && i
< MAX_BIGNUMS
)
130 ptr
->_allocation_map
&= ~ (1 << i
);
135 _DEFUN (multadd
, (ptr
, b
, m
, a
),
136 struct _Jv_reent
*ptr _AND
155 y
= (xi
& 0xffff) * m
+ a
;
156 z
= (xi
>> 16) * m
+ (y
>> 16);
158 *x
++ = (z
<< 16) + (y
& 0xffff);
168 if (wds
>= b
->_maxwds
)
170 b1
= Balloc (ptr
, b
->_k
+ 1);
182 _DEFUN (s2b
, (ptr
, s
, nd0
, nd
, y9
),
183 struct _Jv_reent
* ptr _AND
194 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++);
200 b
= Balloc (ptr
, k
+ 1);
201 b
->_x
[0] = y9
& 0xffff;
202 b
->_wds
= (b
->_x
[1] = y9
>> 16) ? 2 : 1;
210 b
= multadd (ptr
, b
, 10, *s
++ - '0');
217 b
= multadd (ptr
, b
, 10, *s
++ - '0');
223 (x
), register unsigned long x
)
227 if (!(x
& 0xffff0000))
232 if (!(x
& 0xff000000))
237 if (!(x
& 0xf0000000))
242 if (!(x
& 0xc0000000))
247 if (!(x
& 0x80000000))
250 if (!(x
& 0x40000000))
257 _DEFUN (lo0bits
, (y
), unsigned long *y
)
260 register unsigned long x
= *y
;
307 _DEFUN (i2b
, (ptr
, i
), struct _Jv_reent
* ptr _AND
int i
)
318 _DEFUN (mult
, (ptr
, a
, b
), struct _Jv_reent
* ptr _AND _Jv_Bigint
* a _AND _Jv_Bigint
* b
)
322 unsigned long carry
, y
, z
;
323 unsigned long *x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
328 if (a
->_wds
< b
->_wds
)
341 for (x
= c
->_x
, xa
= x
+ wc
; x
< xa
; x
++)
349 for (; xb
< xbe
; xb
++, xc0
++)
351 if ((y
= *xb
& 0xffff))
358 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
360 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
362 Storeinc (xc
, z2
, z
);
375 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
377 Storeinc (xc
, z
, z2
);
378 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
386 for (; xb
< xbe
; xc0
++)
395 z
= *x
++ * y
+ *xc
+ carry
;
404 for (xc0
= c
->_x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
);
411 (ptr
, b
, k
), struct _Jv_reent
* ptr _AND _Jv_Bigint
* b _AND
int k
)
413 _Jv_Bigint
*b1
, *p5
, *p51
;
415 static _CONST
int p05
[3] = {5, 25, 125};
418 b
= multadd (ptr
, b
, p05
[i
- 1], 0);
422 if (!(p5
= ptr
->_p5s
))
425 p5
= ptr
->_p5s
= i2b (ptr
, 625);
432 b1
= mult (ptr
, b
, p5
);
438 if (!(p51
= p5
->_next
))
440 p51
= p5
->_next
= mult (ptr
, p5
, p5
);
449 _DEFUN (lshift
, (ptr
, b
, k
), struct _Jv_reent
* ptr _AND _Jv_Bigint
* b _AND
int k
)
453 unsigned long *x
, *x1
, *xe
, z
;
461 n1
= n
+ b
->_wds
+ 1;
462 for (i
= b
->_maxwds
; n1
> i
; i
<<= 1)
464 b1
= Balloc (ptr
, k1
);
466 for (i
= 0; i
< n
; i
++)
491 *x1
++ = *x
<< k
& 0xffff | z
;
509 _DEFUN (cmp
, (a
, b
), _Jv_Bigint
* a _AND _Jv_Bigint
* b
)
511 unsigned long *xa
, *xa0
, *xb
, *xb0
;
517 if (i
> 1 && !a
->_x
[i
- 1])
518 Bug ("cmp called with a->_x[a->_wds-1] == 0");
519 if (j
> 1 && !b
->_x
[j
- 1])
520 Bug ("cmp called with b->_x[b->_wds-1] == 0");
531 return *xa
< *xb
? -1 : 1;
539 _DEFUN (diff
, (ptr
, a
, b
), struct _Jv_reent
* ptr _AND
540 _Jv_Bigint
* a _AND _Jv_Bigint
* b
)
544 long borrow
, y
; /* We need signed shifts here. */
545 unsigned long *xa
, *xae
, *xb
, *xbe
, *xc
;
567 c
= Balloc (ptr
, a
->_k
);
580 y
= (*xa
& 0xffff) - (*xb
& 0xffff) + borrow
;
582 Sign_Extend (borrow
, y
);
583 z
= (*xa
++ >> 16) - (*xb
++ >> 16) + borrow
;
585 Sign_Extend (borrow
, z
);
591 y
= (*xa
& 0xffff) + borrow
;
593 Sign_Extend (borrow
, y
);
594 z
= (*xa
++ >> 16) + borrow
;
596 Sign_Extend (borrow
, z
);
602 y
= *xa
++ - *xb
++ + borrow
;
604 Sign_Extend (borrow
, y
);
612 Sign_Extend (borrow
, y
);
623 _DEFUN (ulp
, (_x
), double _x
)
625 union double_union x
, a
;
630 L
= (word0 (x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
631 #ifndef Sudden_Underflow
639 #ifndef _DOUBLE_IS_32BITS
643 #ifndef Sudden_Underflow
650 word0 (a
) = 0x80000 >> L
;
651 #ifndef _DOUBLE_IS_32BITS
659 #ifndef _DOUBLE_IS_32BITS
660 word1 (a
) = L
>= 31 ? 1 : 1 << (31 - L
);
670 _Jv_Bigint
* a _AND
int *e
)
672 unsigned long *xa
, *xa0
, w
, y
, z
;
674 union double_union d
;
676 unsigned long d0
, d1
;
687 Bug ("zero y in b2d");
694 d0
= Exp_1
| y
>> (Ebits
- k
);
695 w
= xa
> xa0
? *--xa
: 0;
696 #ifndef _DOUBLE_IS_32BITS
697 d1
= y
<< (32 - Ebits
+ k
) | w
>> (Ebits
- k
);
701 z
= xa
> xa0
? *--xa
: 0;
704 d0
= Exp_1
| y
<< k
| z
>> (32 - k
);
705 y
= xa
> xa0
? *--xa
: 0;
706 #ifndef _DOUBLE_IS_32BITS
707 d1
= z
<< k
| y
>> (32 - k
);
713 #ifndef _DOUBLE_IS_32BITS
720 z
= xa
> xa0
? *--xa
: 0;
721 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
722 w
= xa
> xa0
? *--xa
: 0;
723 y
= xa
> xa0
? *--xa
: 0;
724 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
727 z
= xa
> xa0
? *--xa
: 0;
728 w
= xa
> xa0
? *--xa
: 0;
730 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
731 y
= xa
> xa0
? *--xa
: 0;
732 d1
= w
<< k
+ 16 | y
<< k
;
736 word0 (d
) = d0
>> 16 | d0
<< 16;
737 word1 (d
) = d1
>> 16 | d1
<< 16;
748 struct _Jv_reent
* ptr _AND
754 union double_union d
;
757 unsigned long *x
, y
, z
;
759 unsigned long d0
, d1
;
761 d0
= word0 (d
) >> 16 | word0 (d
) << 16;
762 d1
= word1 (d
) >> 16 | word1 (d
) << 16;
777 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
778 #ifdef Sudden_Underflow
779 de
= (int) (d0
>> Exp_shift
);
784 if ((de
= (int) (d0
>> Exp_shift
)))
788 #ifndef _DOUBLE_IS_32BITS
791 if ((k
= lo0bits (&y
)))
793 x
[0] = y
| z
<< (32 - k
);
798 i
= b
->_wds
= (x
[1] = z
) ? 2 : 1;
805 Bug ("Zero passed to d2b");
810 #ifndef _DOUBLE_IS_32BITS
817 if (k
= lo0bits (&y
))
820 x
[0] = y
| z
<< 32 - k
& 0xffff;
821 x
[1] = z
>> k
- 16 & 0xffff;
828 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
829 x
[2] = z
>> k
& 0xffff;
846 Bug ("Zero passed to d2b");
866 #ifndef Sudden_Underflow
871 *e
= (de
- Bias
- (P
- 1) << 2) + k
;
872 *bits
= 4 * P
+ 8 - k
- hi0bits (word0 (d
) & Frac_mask
);
874 *e
= de
- Bias
- (P
- 1) + k
;
877 #ifndef Sudden_Underflow
881 *e
= de
- Bias
- (P
- 1) + 1 + k
;
883 *bits
= 32 * i
- hi0bits (x
[i
- 1]);
885 *bits
= (i
+ 2) * 16 - hi0bits (x
[i
]);
895 _DEFUN (ratio
, (a
, b
), _Jv_Bigint
* a _AND _Jv_Bigint
* b
)
898 union double_union da
, db
;
904 k
= ka
- kb
+ 32 * (a
->_wds
- b
->_wds
);
906 k
= ka
- kb
+ 16 * (a
->_wds
- b
->_wds
);
911 word0 (da
) += (k
>> 2) * Exp_msk1
;
918 word0 (db
) += (k
>> 2) * Exp_msk1
;
924 word0 (da
) += k
* Exp_msk1
;
928 word0 (db
) += k
* Exp_msk1
;
938 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
939 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
940 1e20
, 1e21
, 1e22
, 1e23
, 1e24
944 #if !defined(_DOUBLE_IS_32BITS) && !defined(__v800)
945 _CONST
double bigtens
[] =
946 {1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
948 _CONST
double tinytens
[] =
949 {1e-16, 1e-32, 1e-64, 1e-128, 1e-256};
951 _CONST
double bigtens
[] =
954 _CONST
double tinytens
[] =