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.
89 #ifdef _REENT_THREAD_LOCAL
90 _Thread_local
struct _Bigint
*_tls_mp_p5s
;
91 _Thread_local
struct _Bigint
**_tls_mp_freelist
;
94 /* This is defined in sys/reent.h as (sizeof (size_t) << 3) now, as in NetBSD.
95 The old value of 15 was wrong and made newlib vulnerable against buffer
96 overrun attacks (CVE-2009-0689), same as other implementations of gdtoa
102 Balloc (struct _reent
*ptr
, int k
)
107 _REENT_CHECK_MP(ptr
);
108 if (_REENT_MP_FREELIST(ptr
) == NULL
)
110 /* Allocate a list of pointers to the mprec objects */
111 _REENT_MP_FREELIST(ptr
) = (struct _Bigint
**) _calloc_r (ptr
,
112 sizeof (struct _Bigint
*),
114 if (_REENT_MP_FREELIST(ptr
) == NULL
)
120 if ((rv
= _REENT_MP_FREELIST(ptr
)[k
]) != 0)
122 _REENT_MP_FREELIST(ptr
)[k
] = rv
->_next
;
127 /* Allocate an mprec Bigint and stick in in the freelist */
128 rv
= (_Bigint
*) _calloc_r (ptr
,
131 (x
-1) * sizeof(rv
->_x
));
132 if (rv
== NULL
) return NULL
;
136 rv
->_sign
= rv
->_wds
= 0;
141 Bfree (struct _reent
*ptr
, _Bigint
* v
)
143 _REENT_CHECK_MP(ptr
);
146 v
->_next
= _REENT_MP_FREELIST(ptr
)[v
->_k
];
147 _REENT_MP_FREELIST(ptr
)[v
->_k
] = v
;
152 multadd (struct _reent
*ptr
,
171 y
= (xi
& 0xffff) * m
+ a
;
172 z
= (xi
>> 16) * m
+ (y
>> 16);
174 *x
++ = (z
<< 16) + (y
& 0xffff);
184 if (wds
>= b
->_maxwds
)
186 b1
= eBalloc (ptr
, b
->_k
+ 1);
198 s2b (struct _reent
* ptr
,
209 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++);
211 b
= eBalloc (ptr
, k
);
215 b
= eBalloc (ptr
, k
+ 1);
216 b
->_x
[0] = y9
& 0xffff;
217 b
->_wds
= (b
->_x
[1] = y9
>> 16) ? 2 : 1;
225 b
= multadd (ptr
, b
, 10, *s
++ - '0');
232 b
= multadd (ptr
, b
, 10, *s
++ - '0');
237 hi0bits (register __ULong x
)
241 if (!(x
& 0xffff0000))
246 if (!(x
& 0xff000000))
251 if (!(x
& 0xf0000000))
256 if (!(x
& 0xc0000000))
261 if (!(x
& 0x80000000))
264 if (!(x
& 0x40000000))
274 register __ULong x
= *y
;
321 i2b (struct _reent
* ptr
, int i
)
325 b
= eBalloc (ptr
, 1);
332 mult (struct _reent
* ptr
, _Bigint
* a
, _Bigint
* b
)
337 __ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
342 if (a
->_wds
< b
->_wds
)
354 c
= eBalloc (ptr
, k
);
355 for (x
= c
->_x
, xa
= x
+ wc
; x
< xa
; x
++)
363 for (; xb
< xbe
; xb
++, xc0
++)
365 if ((y
= *xb
& 0xffff) != 0)
372 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
374 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
376 Storeinc (xc
, z2
, z
);
381 if ((y
= *xb
>> 16) != 0)
389 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
391 Storeinc (xc
, z
, z2
);
392 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
400 for (; xb
< xbe
; xc0
++)
409 z
= *x
++ * y
+ *xc
+ carry
;
418 for (xc0
= c
->_x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
);
424 pow5mult (struct _reent
* ptr
, _Bigint
* b
, int k
)
426 _Bigint
*b1
, *p5
, *p51
;
428 static const int p05
[3] = {5, 25, 125};
430 if ((i
= k
& 3) != 0)
431 b
= multadd (ptr
, b
, p05
[i
- 1], 0);
435 _REENT_CHECK_MP(ptr
);
436 if (!(p5
= _REENT_MP_P5S(ptr
)))
439 p5
= _REENT_MP_P5S(ptr
) = i2b (ptr
, 625);
446 b1
= mult (ptr
, b
, p5
);
452 if (!(p51
= p5
->_next
))
454 p51
= p5
->_next
= mult (ptr
, p5
, p5
);
463 lshift (struct _reent
* ptr
, _Bigint
* b
, int k
)
467 __ULong
*x
, *x1
, *xe
, z
;
475 n1
= n
+ b
->_wds
+ 1;
476 for (i
= b
->_maxwds
; n1
> i
; i
<<= 1)
478 b1
= eBalloc (ptr
, k1
);
480 for (i
= 0; i
< n
; i
++)
505 *x1
++ = *x
<< k
& 0xffff | z
;
523 cmp (_Bigint
* a
, _Bigint
* b
)
525 __ULong
*xa
, *xa0
, *xb
, *xb0
;
531 if (i
> 1 && !a
->_x
[i
- 1])
532 Bug ("cmp called with a->_x[a->_wds-1] == 0");
533 if (j
> 1 && !b
->_x
[j
- 1])
534 Bug ("cmp called with b->_x[b->_wds-1] == 0");
545 return *xa
< *xb
? -1 : 1;
553 diff (struct _reent
* ptr
,
554 _Bigint
* a
, _Bigint
* b
)
558 __Long borrow
, y
; /* We need signed shifts here. */
559 __ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
567 c
= eBalloc (ptr
, 0);
581 c
= eBalloc (ptr
, a
->_k
);
594 y
= (*xa
& 0xffff) - (*xb
& 0xffff) + borrow
;
596 Sign_Extend (borrow
, y
);
597 z
= (*xa
++ >> 16) - (*xb
++ >> 16) + borrow
;
599 Sign_Extend (borrow
, z
);
605 y
= (*xa
& 0xffff) + borrow
;
607 Sign_Extend (borrow
, y
);
608 z
= (*xa
++ >> 16) + borrow
;
610 Sign_Extend (borrow
, z
);
616 y
= *xa
++ - *xb
++ + borrow
;
618 Sign_Extend (borrow
, y
);
626 Sign_Extend (borrow
, y
);
639 union double_union x
, a
;
644 L
= (word0 (x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
645 #ifndef Sudden_Underflow
653 #ifndef _DOUBLE_IS_32BITS
657 #ifndef Sudden_Underflow
664 word0 (a
) = 0x80000 >> L
;
665 #ifndef _DOUBLE_IS_32BITS
673 #ifndef _DOUBLE_IS_32BITS
674 word1 (a
) = L
>= 31 ? 1 : 1 << (31 - L
);
683 b2d (_Bigint
* a
, int *e
)
685 __ULong
*xa
, *xa0
, w
, y
, z
;
687 union double_union d
;
700 Bug ("zero y in b2d");
707 d0
= Exp_1
| y
>> (Ebits
- k
);
708 w
= xa
> xa0
? *--xa
: 0;
709 #ifndef _DOUBLE_IS_32BITS
710 d1
= y
<< ((32 - Ebits
) + k
) | w
>> (Ebits
- k
);
714 z
= xa
> xa0
? *--xa
: 0;
717 d0
= Exp_1
| y
<< k
| z
>> (32 - k
);
718 y
= xa
> xa0
? *--xa
: 0;
719 #ifndef _DOUBLE_IS_32BITS
720 d1
= z
<< k
| y
>> (32 - k
);
726 #ifndef _DOUBLE_IS_32BITS
733 z
= xa
> xa0
? *--xa
: 0;
734 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
735 w
= xa
> xa0
? *--xa
: 0;
736 y
= xa
> xa0
? *--xa
: 0;
737 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
740 z
= xa
> xa0
? *--xa
: 0;
741 w
= xa
> xa0
? *--xa
: 0;
743 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
744 y
= xa
> xa0
? *--xa
: 0;
745 d1
= w
<< k
+ 16 | y
<< k
;
749 word0 (d
) = d0
>> 16 | d0
<< 16;
750 word1 (d
) = d1
>> 16 | d1
<< 16;
759 d2b (struct _reent
* ptr
,
765 union double_union d
;
774 d0
= word0 (d
) >> 16 | word0 (d
) << 16;
775 d1
= word1 (d
) >> 16 | word1 (d
) << 16;
783 b
= eBalloc (ptr
, 1);
785 b
= eBalloc (ptr
, 2);
790 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
791 #ifdef Sudden_Underflow
792 de
= (int) (d0
>> Exp_shift
);
797 if ((de
= (int) (d0
>> Exp_shift
)) != 0)
801 #ifndef _DOUBLE_IS_32BITS
808 x
[0] = y
| z
<< (32 - k
);
813 i
= b
->_wds
= (x
[1] = z
) ? 2 : 1;
820 Bug ("Zero passed to d2b");
825 #ifndef _DOUBLE_IS_32BITS
837 x
[0] = y
| z
<< 32 - k
& 0xffff;
838 x
[1] = z
>> k
- 16 & 0xffff;
845 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
846 x
[2] = z
>> k
& 0xffff;
863 Bug ("Zero passed to d2b");
883 #ifndef Sudden_Underflow
888 *e
= (de
- Bias
- (P
- 1) << 2) + k
;
889 *bits
= 4 * P
+ 8 - k
- hi0bits (word0 (d
) & Frac_mask
);
891 *e
= de
- Bias
- (P
- 1) + k
;
894 #ifndef Sudden_Underflow
898 *e
= de
- Bias
- (P
- 1) + 1 + k
;
900 *bits
= 32 * i
- hi0bits (x
[i
- 1]);
902 *bits
= (i
+ 2) * 16 - hi0bits (x
[i
]);
912 ratio (_Bigint
* a
, _Bigint
* b
)
915 union double_union da
, db
;
921 k
= ka
- kb
+ 32 * (a
->_wds
- b
->_wds
);
923 k
= ka
- kb
+ 16 * (a
->_wds
- b
->_wds
);
928 word0 (da
) += (k
>> 2) * Exp_msk1
;
935 word0 (db
) += (k
>> 2) * Exp_msk1
;
941 word0 (da
) += k
* Exp_msk1
;
945 word0 (db
) += k
* Exp_msk1
;
955 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
956 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
957 1e20
, 1e21
, 1e22
, 1e23
, 1e24
961 #if !defined(_DOUBLE_IS_32BITS) && !defined(__v800)
962 const double bigtens
[] =
963 {1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
965 const double tinytens
[] =
966 {1e-16, 1e-32, 1e-64, 1e-128, 1e-256};
968 const double bigtens
[] =
971 const double tinytens
[] =
977 _mprec_log10 (int dig
)
991 copybits (__ULong
*c
,
995 __ULong
*ce
, *x
, *xe
;
1000 ce
= c
+ ((n
-1) >> kshift
) + 1;
1009 for(xe
= x
+ (nw
- nw1
); x
< xe
; x
+= 2)
1010 Storeinc(c
, x
[1], x
[0]);
1023 __ULong
*x
, *x0
, x1
, x2
;
1030 else if (n
< nwds
&& (k
&= kmask
)) {