2 Copyright (C) 1993, 1994 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
27 /****************************************************************
29 * The author of this software is David M. Gay.
31 * Copyright (c) 1991 by AT&T.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose without fee is hereby granted, provided that this entire notice
35 * is included in all copies of any software which is or includes a copy
36 * or modification of this software and in all copies of the supporting
37 * documentation for such software.
39 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
40 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
41 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
42 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
44 ***************************************************************/
46 /* Some cleaning up by Per Bothner, bothner@cygnus.com, 1992, 1993.
47 Re-written to not need static variables
48 (except result, result_k, HIWORD, LOWORD). */
50 /* Note that the checking of _DOUBLE_IS_32BITS is for use with the
51 cross targets that employ the newlib ieeefp.h header. -- brendan */
53 /* Please send bug reports to
55 AT&T Bell Laboratories, Room 2C-463
57 Murray Hill, NJ 07974-2070
59 dmg@research.att.com or research!dmg
62 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
64 * This strtod returns a nearest machine number to the input decimal
65 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
66 * broken by the IEEE round-even rule. Otherwise ties are broken by
67 * biased rounding (add half and chop).
69 * Inspired loosely by William D. Clinger's paper "How to Read Floating
70 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
74 * 1. We only require IEEE, IBM, or VAX double-precision
75 * arithmetic (not IEEE double-extended).
76 * 2. We get by with floating-point arithmetic in a case that
77 * Clinger missed -- when we're computing d * 10^n
78 * for a small integer d and the integer n is not too
79 * much larger than 22 (the maximum integer k for which
80 * we can represent 10^k exactly), we may be able to
81 * compute (d*10^k) * 10^(e-k) with just one roundoff.
82 * 3. Rather than a bit-at-a-time adjustment of the binary
83 * result in the hard case, we use floating-point
84 * arithmetic to determine the adjustment to within
85 * one bit; only in really hard cases do we need to
86 * compute a second residual.
87 * 4. Because of 3., we don't need a large table of powers of 10
88 * for ten-to-e (just some small tables, e.g. of 10^k
93 * #define IEEE_8087 for IEEE-arithmetic machines where the least
94 * significant byte has the lowest address.
95 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
96 * significant byte has the lowest address.
97 * #define Sudden_Underflow for IEEE-format machines without gradual
98 * underflow (i.e., that flush to zero on underflow).
99 * #define IBM for IBM mainframe-style floating-point arithmetic.
100 * #define VAX for VAX-style floating-point arithmetic.
101 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
102 * #define No_leftright to omit left-right logic in fast floating-point
103 * computation of dtoa.
104 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
105 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
106 * that use extended-precision instructions to compute rounded
107 * products and quotients) with IBM.
108 * #define ROUND_BIASED for IEEE-format with biased rounding.
109 * #define Inaccurate_Divide for IEEE-format with correctly rounded
110 * products but inaccurate quotients, e.g., for Intel i860.
111 * #define KR_headers for old-style C function headers.
116 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
128 /* In this case, we assume IEEE floats. */
131 #define DBL_MANT_DIG 53
133 #define DBL_MAX_10_EXP 308
134 #define DBL_MAX_EXP 1024
142 #ifdef Unsigned_Shifts
143 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
145 #define Sign_Extend(a,b) /*no-op*/
148 #if defined(__i386__) || defined(__i860__) || defined(clipper)
151 #if defined(MIPSEL) || defined(__alpha__)
154 #if defined(__sparc__) || defined(sparc) || defined(MIPSEB)
158 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
160 #ifndef _DOUBLE_IS_32BITS
167 #if DBL_MANT_DIG==53 && DBL_MAX_10_EXP==308
170 Exactly one of IEEE_8087
, IEEE_MC68k
, VAX
, or IBM should be defined
.
174 #endif /* !_DOUBLE_IS_32BITS */
177 typedef _G_uint32_t unsigned32
;
187 #define TEST_ENDIANNESS /* nothing */
189 #if defined(IEEE_MC68k)
192 #define TEST_ENDIANNESS /* nothing */
194 static int HIWORD
= -1, LOWORD
;
195 static void test_endianness()
199 if (dw
.u
[0] != 0) /* big-endian */
204 #define TEST_ENDIANNESS if (HIWORD<0) test_endianness();
209 union doubleword _temp
;
211 #if defined(__GNUC__) && !defined(_DOUBLE_IS_32BITS)
212 #define word0(x) ({ union doubleword _du; _du.d = (x); _du.u[HIWORD]; })
213 #define word1(x) ({ union doubleword _du; _du.d = (x); _du.u[LOWORD]; })
214 #define setword0(D,W) \
215 ({ union doubleword _du; _du.d = (D); _du.u[HIWORD]=(W); (D)=_du.d; })
216 #define setword1(D,W) \
217 ({ union doubleword _du; _du.d = (D); _du.u[LOWORD]=(W); (D)=_du.d; })
218 #define setwords(D,W0,W1) ({ union doubleword _du; \
219 _du.u[HIWORD]=(W0); _du.u[LOWORD]=(W1); (D)=_du.d; })
220 #define addword0(D,W) \
221 ({ union doubleword _du; _du.d = (D); _du.u[HIWORD]+=(W); (D)=_du.d; })
223 #define word0(x) ((unsigned32 *)&x)[HIWORD]
224 #ifndef _DOUBLE_IS_32BITS
225 #define word1(x) ((unsigned32 *)&x)[LOWORD]
229 #define setword0(D,W) word0(D) = (W)
230 #ifndef _DOUBLE_IS_32BITS
231 #define setword1(D,W) word1(D) = (W)
232 #define setwords(D,W0,W1) (setword0(D,W0),setword1(D,W1))
234 #define setword1(D,W)
235 #define setwords(D,W0,W1) (setword0(D,W0))
237 #define addword0(D,X) (word0(D) += (X))
240 /* The following definition of Storeinc is appropriate for MIPS processors. */
241 #if defined(IEEE_8087) + defined(VAX)
242 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
243 ((unsigned short *)a)[0] = (unsigned short)c, a++)
245 #if defined(IEEE_MC68k)
246 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
247 ((unsigned short *)a)[1] = (unsigned short)c, a++)
249 #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
253 /* #define P DBL_MANT_DIG */
254 /* Ten_pmax = floor(P*log(2)/log(5)) */
255 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
256 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
257 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
259 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_Unknown)
261 #define Exp_shift1 20
262 #define Exp_msk1 0x100000
263 #define Exp_msk11 0x100000
264 #define Exp_mask 0x7ff00000
269 #define Exp_1 0x3ff00000
270 #define Exp_11 0x3ff00000
272 #define Frac_mask 0xfffff
273 #define Frac_mask1 0xfffff
276 #define Bndry_mask 0xfffff
277 #define Bndry_mask1 0xfffff
279 #define Sign_bit 0x80000000
285 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
287 #undef Sudden_Underflow
288 #define Sudden_Underflow
291 #define Exp_shift1 24
292 #define Exp_msk1 0x1000000
293 #define Exp_msk11 0x1000000
294 #define Exp_mask 0x7f000000
297 #define Exp_1 0x41000000
298 #define Exp_11 0x41000000
299 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
300 #define Frac_mask 0xffffff
301 #define Frac_mask1 0xffffff
304 #define Bndry_mask 0xefffff
305 #define Bndry_mask1 0xffffff
307 #define Sign_bit 0x80000000
309 #define Tiny0 0x100000
316 #define Exp_msk1 0x80
317 #define Exp_msk11 0x800000
318 #define Exp_mask 0x7f80
321 #define Exp_1 0x40800000
322 #define Exp_11 0x4080
324 #define Frac_mask 0x7fffff
325 #define Frac_mask1 0xffff007f
328 #define Bndry_mask 0xffff007f
329 #define Bndry_mask1 0xffff007f
331 #define Sign_bit 0x8000
345 #define rounded_product(a,b) a = rnd_prod(a, b)
346 #define rounded_quotient(a,b) a = rnd_quot(a, b)
347 extern double rnd_prod(double, double), rnd_quot(double, double);
349 #define rounded_product(a,b) a *= b
350 #define rounded_quotient(a,b) a /= b
353 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
354 #define Big1 0xffffffff
358 /* (1<<BIGINT_MINIMUM_K) is the minimum number of words to allocate
359 in a Bigint. dtoa usually manages with 1<<2, and has not been
360 known to need more than 1<<3. */
362 #define BIGINT_MINIMUM_K 3
366 int k
; /* Parameter given to Balloc(k) */
367 int maxwds
; /* Allocated space: equals 1<<k. */
368 short on_stack
; /* 1 if stack-allocated. */
369 short sign
; /* 0 if value is positive or zero; 1 if negative. */
370 int wds
; /* Current length. */
371 unsigned32 x
[1<<BIGINT_MINIMUM_K
]; /* Actually: x[maxwds] */
374 #define BIGINT_HEADER_SIZE \
375 (sizeof(Bigint) - (1<<BIGINT_MINIMUM_K) * sizeof(unsigned32))
377 typedef struct Bigint Bigint
;
379 /* Initialize a stack-allocated Bigint. */
390 v
->k
= BIGINT_MINIMUM_K
;
391 v
->maxwds
= 1 << BIGINT_MINIMUM_K
;
392 v
->sign
= v
->wds
= 0;
396 /* Allocate a Bigint with '1<<k' big digits. */
409 if (k
< BIGINT_MINIMUM_K
)
410 k
= BIGINT_MINIMUM_K
;
414 malloc(BIGINT_HEADER_SIZE
+ x
* sizeof(unsigned32
));
417 rv
->sign
= rv
->wds
= 0;
430 if (v
&& !v
->on_stack
)
437 (x
, y
) Bigint
*x
, *y
;
439 (Bigint
*x
, Bigint
*y
)
442 register unsigned32
*xp
, *yp
;
443 register int i
= y
->wds
;
446 for (xp
= x
->x
, yp
= y
->x
; --i
>= 0; )
450 /* Make sure b has room for at least 1<<k big digits. */
455 (b
, k
) Bigint
*b
; int k
;
466 Bigint
*rv
= Balloc (k
);
473 /* Return b*m+a. b is modified.
474 Assumption: 0xFFFF*m+a fits in 32 bits. */
479 (b
, m
, a
) Bigint
*b
; int m
, a
;
481 (Bigint
*b
, int m
, int a
)
493 y
= (xi
& 0xffff) * m
+ a
;
494 z
= (xi
>> 16) * m
+ (y
>> 16);
496 *x
++ = (z
<< 16) + (y
& 0xffff);
500 if (wds
>= b
->maxwds
)
501 b
= Brealloc(b
, b
->k
+1);
511 (result
, s
, nd0
, nd
, y9
)
512 Bigint
*result
; CONST
char *s
; int nd0
, nd
; unsigned32 y9
;
514 (Bigint
*result
, CONST
char *s
, int nd0
, int nd
, unsigned32 y9
)
521 for(k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) ;
522 result
= Brealloc(result
, k
);
531 result
= multadd(result
, 10, *s
++ - '0');
538 result
= multadd(result
, 10, *s
++ - '0');
545 (x
) register unsigned32 x
;
547 (register unsigned32 x
)
552 if (!(x
& 0xffff0000)) {
556 if (!(x
& 0xff000000)) {
560 if (!(x
& 0xf0000000)) {
564 if (!(x
& 0xc0000000)) {
568 if (!(x
& 0x80000000)) {
570 if (!(x
& 0x40000000))
585 register unsigned32 x
= *y
;
627 (result
, i
) Bigint
*result
; int i
;
629 (Bigint
* result
, int i
)
632 result
= Brealloc(result
, 1);
643 (c
, a
, b
) Bigint
*a
, *b
, *c
;
645 (Bigint
*c
, Bigint
*a
, Bigint
*b
)
649 unsigned32 carry
, y
, z
;
650 unsigned32
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
652 if (a
->wds
< b
->wds
) {
664 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
671 for(; xb
< xbe
; xb
++, xc0
++) {
672 if ((y
= *xb
& 0xffff)) {
677 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
679 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
686 if ((y
= *xb
>> 16)) {
692 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
695 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
702 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
707 /* Returns b*(5**k). b is modified. */
708 /* Re-written by Per Bothner to not need a static list. */
713 (b
, k
) Bigint
*b
; int k
;
718 static int p05
[6] = { 5, 25, 125, 625, 3125, 15625 };
720 for (; k
> 6; k
-= 6)
721 b
= multadd(b
, 15625, 0); /* b *= 5**6 */
725 return multadd(b
, p05
[k
-1], 0);
728 /* Re-written by Per Bothner so shift can be in place. */
733 (b
, k
) Bigint
*b
; int k
;
739 unsigned32
*x
, *x1
, *xe
;
740 int old_wds
= b
->wds
;
743 int n1
= n
+ old_wds
+ 1;
748 for(i
= b
->maxwds
; n1
> i
; i
<<= 1)
752 xe
= b
->x
; /* Source limit */
753 x
= xe
+ old_wds
; /* Source pointer */
754 x1
= x
+ n
; /* Destination pointer */
758 if ((*x1
= (z
>> k1
)) != 0) {
763 *--x1
= (z
<< k
) | (w
>> k1
);
781 (a
, b
) Bigint
*a
, *b
;
783 (Bigint
*a
, Bigint
*b
)
786 unsigned32
*xa
, *xa0
, *xb
, *xb0
;
792 if (i
> 1 && !a
->x
[i
-1])
793 Bug("cmp called with a->x[a->wds-1] == 0");
794 if (j
> 1 && !b
->x
[j
-1])
795 Bug("cmp called with b->x[b->wds-1] == 0");
805 return *xa
< *xb
? -1 : 1;
817 (c
, a
, b
) Bigint
*c
, *a
, *b
;
819 (Bigint
*c
, Bigint
*a
, Bigint
*b
)
823 _G_int32_t borrow
, y
; /* We need signed shifts here. */
824 unsigned32
*xa
, *xae
, *xb
, *xbe
, *xc
;
842 c
= Brealloc(c
, a
->k
);
853 y
= (*xa
& 0xffff) - (*xb
& 0xffff) + borrow
;
855 Sign_Extend(borrow
, y
);
856 z
= (*xa
++ >> 16) - (*xb
++ >> 16) + borrow
;
858 Sign_Extend(borrow
, z
);
863 y
= (*xa
& 0xffff) + borrow
;
865 Sign_Extend(borrow
, y
);
866 z
= (*xa
++ >> 16) + borrow
;
868 Sign_Extend(borrow
, z
);
885 register _G_int32_t L
;
888 L
= (word0(x
) & Exp_mask
) - (P
-1)*Exp_msk1
;
889 #ifndef Sudden_Underflow
896 #ifndef Sudden_Underflow
901 setwords(a
, 0x80000 >> L
, 0);
904 setwords(a
, 0, L
>= 31 ? 1 : 1 << (31 - L
));
914 (a
, e
) Bigint
*a
; int *e
;
919 unsigned32
*xa
, *xa0
, w
, y
, z
;
928 if (!y
) Bug("zero y in b2d");
933 d0
= Exp_1
| y
>> (Ebits
- k
);
934 w
= xa
> xa0
? *--xa
: 0;
935 #ifndef _DOUBLE_IS_32BITS
936 d1
= y
<< ((32-Ebits
) + k
) | w
>> (Ebits
- k
);
940 z
= xa
> xa0
? *--xa
: 0;
942 d0
= Exp_1
| y
<< k
| z
>> (32 - k
);
943 y
= xa
> xa0
? *--xa
: 0;
944 #ifndef _DOUBLE_IS_32BITS
945 d1
= z
<< k
| y
>> (32 - k
);
950 #ifndef _DOUBLE_IS_32BITS
956 setwords(d
, d0
>> 16 | d0
<< 16, d1
>> 16 | d1
<< 16);
958 setwords (d
, d0
, d1
);
966 (result
, d
, e
, bits
) Bigint
*result
; double d
; _G_int32_t
*e
, *bits
;
968 (Bigint
*result
, double d
, _G_int32_t
*e
, _G_int32_t
*bits
)
975 d0
= word0(d
) >> 16 | word0(d
) << 16;
976 d1
= word1(d
) >> 16 | word1(d
) << 16;
982 result
= Brealloc(result
, 1);
986 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
988 de
= (int)(d0
>> Exp_shift
); /* The exponent part of d. */
990 /* Put back the suppressed high-order bit, if normalized. */
992 #ifndef Sudden_Underflow
998 #ifndef _DOUBLE_IS_32BITS
1000 if ((k
= lo0bits(&y
))) {
1001 x
[0] = y
| z
<< (32 - k
);
1006 i
= result
->wds
= (x
[1] = z
) ? 2 : 1;
1009 #endif /* !_DOUBLE_IS_32BITS */
1012 Bug("Zero passed to d2b");
1016 i
= result
->wds
= 1;
1017 #ifndef _DOUBLE_IS_32BITS
1021 #ifndef Sudden_Underflow
1025 *e
= (de
- Bias
- (P
-1) << 2) + k
;
1026 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1028 *e
= de
- Bias
- (P
-1) + k
;
1031 #ifndef Sudden_Underflow
1034 *e
= de
- Bias
- (P
-1) + 1 + k
;
1035 *bits
= 32*i
- hi0bits(x
[i
-1]);
1044 (a
, b
) Bigint
*a
, *b
;
1046 (Bigint
*a
, Bigint
*b
)
1054 k
= ka
- kb
+ 32*(a
->wds
- b
->wds
);
1057 addword0(da
, (k
>> 2)*Exp_msk1
);
1063 addword0(db
,(k
>> 2)*Exp_msk1
);
1069 addword0(da
, k
*Exp_msk1
);
1072 addword0(db
, k
*Exp_msk1
);
1080 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1081 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1089 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1090 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1094 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
};
1095 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
1098 /* Also used for the case when !_DOUBLE_IS_32BITS. */
1099 static CONST
double bigtens
[] = { 1e16
, 1e32
};
1100 static CONST
double tinytens
[] = { 1e-16, 1e-32 };
1108 (s00
, se
) CONST
char *s00
; char **se
;
1110 (CONST
char *s00
, char **se
)
1113 _G_int32_t bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1114 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1115 CONST
char *s
, *s0
, *s1
;
1116 double aadj
, aadj1
, adj
, rv
, rv0
;
1119 Bigint _bb
, _b_avail
, _bd
, _bd0
, _bs
, _delta
;
1120 Bigint
*bb
= Binit(&_bb
);
1121 Bigint
*bd
= Binit(&_bd
);
1122 Bigint
*bd0
= Binit(&_bd0
);
1123 Bigint
*bs
= Binit(&_bs
);
1124 Bigint
*b_avail
= Binit(&_b_avail
);
1125 Bigint
*delta
= Binit(&_delta
);
1128 sign
= nz0
= nz
= 0;
1130 (void)&rv
; /* Force rv into the stack */
1131 for(s
= s00
;;s
++) switch(*s
) {
1140 /* "+" and "-" should be reported as an error? */
1157 while(*++s
== '0') ;
1163 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1172 for(; c
== '0'; c
= *++s
)
1174 if (c
> '0' && c
<= '9') {
1182 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
1187 for(i
= 1; i
< nz
; i
++)
1190 else if (nd
<= DBL_DIG
+ 1)
1194 else if (nd
<= DBL_DIG
+ 1)
1202 if (c
== 'e' || c
== 'E') {
1203 if (!nd
&& !nz
&& !nz0
) {
1215 if (c
>= '0' && c
<= '9') {
1218 if (c
> '0' && c
<= '9') {
1221 while((c
= *++s
) >= '0' && c
<= '9')
1224 /* Avoid confusion from exponents
1225 * so large that e might overflow.
1244 /* Now we have nd0 digits, starting at s0, followed by a
1245 * decimal point, followed by nd-nd0 digits. The number we're
1246 * after is the integer represented by those digits times
1251 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1254 rv
= tens
[k
- 9] * rv
+ z
;
1256 #ifndef RND_PRODQUOT
1263 if (e
<= Ten_pmax
) {
1265 goto vax_ovfl_check
;
1267 /* rv = */ rounded_product(rv
, tens
[e
]);
1272 if (e
<= Ten_pmax
+ i
) {
1273 /* A fancier test would sometimes let us do
1274 * this for larger i values.
1279 /* VAX exponent range is so narrow we must
1280 * worry about overflow here...
1283 addword0(rv
, - P
*Exp_msk1
);
1284 /* rv = */ rounded_product(rv
, tens
[e
]);
1285 if ((word0(rv
) & Exp_mask
)
1286 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
1288 addword0(rv
, P
*Exp_msk1
);
1290 /* rv = */ rounded_product(rv
, tens
[e
]);
1295 #ifndef Inaccurate_Divide
1296 else if (e
>= -Ten_pmax
) {
1297 /* rv = */ rounded_quotient(rv
, tens
[-e
]);
1304 /* Get starting approximation = rv * 10**e1 */
1310 if (e1
> DBL_MAX_10_EXP
) {
1313 #if defined(sun) && !defined(__svr4__)
1314 /* SunOS defines HUGE_VAL as __infinity(), which is in libm. */
1318 #define HUGE_VAL 1.7976931348623157E+308
1324 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1327 /* The last multiplication could overflow. */
1328 addword0(rv
, -P
*Exp_msk1
);
1330 if ((z
= word0(rv
) & Exp_mask
)
1331 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
1333 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
1334 /* set to largest number */
1335 /* (Can't trust DBL_MAX) */
1336 setwords(rv
, Big0
, Big1
);
1339 addword0(rv
, P
*Exp_msk1
);
1350 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1353 /* The last multiplication could underflow. */
1365 setwords(rv
, Tiny0
, Tiny1
);
1366 /* The refinement below will clean
1367 * this approximation up.
1373 /* Now the hard part -- adjusting rv to the correct value.*/
1375 /* Put digits into bd: true value = bd * 10^e */
1377 bd0
= s2b(bd0
, s0
, nd0
, nd
, y
);
1378 bd
= Brealloc(bd
, bd0
->k
);
1382 bb
= d2b(bb
, rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1398 #ifdef Sudden_Underflow
1400 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
1405 i
= bbe
+ bbbits
- 1; /* logb(rv) */
1406 if (i
< Emin
) /* denormal */
1413 i
= bb2
< bd2
? bb2
: bd2
;
1423 bs
= pow5mult(bs
, bb5
);
1424 b_tmp
= mult(b_avail
, bs
, bb
);
1429 bb
= lshift(bb
, bb2
);
1431 bd
= pow5mult(bd
, bd5
);
1433 bd
= lshift(bd
, bd2
);
1435 bs
= lshift(bs
, bs2
);
1436 delta
= diff(delta
, bb
, bd
);
1437 dsign
= delta
->sign
;
1441 /* Error is less than half an ulp -- check for
1442 * special case of mantissa a power of two.
1444 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
)
1446 delta
= lshift(delta
,Log2P
);
1447 if (cmp(delta
, bs
) > 0)
1452 /* exactly half-way between */
1454 if ((word0(rv
) & Bndry_mask1
) == Bndry_mask1
1455 && word1(rv
) == 0xffffffff) {
1456 /*boundary case -- increment exponent*/
1457 setword0(rv
, (word0(rv
) & Exp_mask
)
1461 word0(rv
) | (Exp_msk1
>> 4));
1467 else if (!(word0(rv
) & Bndry_mask
) && !word1(rv
)) {
1469 /* boundary case -- decrement exponent */
1470 #ifdef Sudden_Underflow
1471 L
= word0(rv
) & Exp_mask
;
1480 L
= (word0(rv
) & Exp_mask
) - Exp_msk1
;
1482 setwords(rv
, L
| Bndry_mask1
, 0xffffffff);
1489 #ifndef ROUND_BIASED
1490 if (!(word1(rv
) & LSB
))
1495 #ifndef ROUND_BIASED
1498 #ifndef Sudden_Underflow
1506 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1509 else if (word1(rv
) || word0(rv
) & Bndry_mask
) {
1510 #ifndef Sudden_Underflow
1511 if (word1(rv
) == Tiny1
&& !word0(rv
))
1518 /* special case -- power of FLT_RADIX to be */
1519 /* rounded down... */
1521 if (aadj
< 2./FLT_RADIX
)
1522 aadj
= 1./FLT_RADIX
;
1530 aadj1
= dsign
? aadj
: -aadj
;
1531 #ifdef Check_FLT_ROUNDS
1532 switch(FLT_ROUNDS
) {
1533 case 2: /* towards +infinity */
1536 case 0: /* towards 0 */
1537 case 3: /* towards -infinity */
1541 if (FLT_ROUNDS
== 0)
1545 y
= word0(rv
) & Exp_mask
;
1547 /* Check for overflow */
1549 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
1551 addword0(rv
, - P
*Exp_msk1
);
1552 adj
= aadj1
* ulp(rv
);
1554 if ((word0(rv
) & Exp_mask
) >=
1555 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
1556 if (word0(rv0
) == Big0
&& word1(rv0
) == Big1
)
1558 setwords(rv
, Big0
, Big1
);
1562 addword0(rv
, P
*Exp_msk1
);
1565 #ifdef Sudden_Underflow
1566 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
1568 addword0(rv
, P
*Exp_msk1
);
1569 adj
= aadj1
* ulp(rv
);
1572 if ((word0(rv
) & Exp_mask
) < P
*Exp_msk1
)
1574 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
)
1577 if (word0(rv0
) == Tiny0
1578 && word1(rv0
) == Tiny1
)
1580 setwords(rv
, Tiny0
, Tiny1
);
1584 addword0(rv
, -P
*Exp_msk1
);
1587 adj
= aadj1
* ulp(rv
);
1591 /* Compute adj so that the IEEE rounding rules will
1592 * correctly round rv + adj in some half-way cases.
1593 * If rv * ulp(rv) is denormalized (i.e.,
1594 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1595 * trouble from bits lost to denormalization;
1596 * example: 1.2e-307 .
1598 if (y
<= (P
-1)*Exp_msk1
&& aadj
>= 1.) {
1599 aadj1
= (double)(int)(aadj
+ 0.5);
1603 adj
= aadj1
* ulp(rv
);
1607 z
= word0(rv
) & Exp_mask
;
1609 /* Can we stop now? */
1610 L
= (_G_int32_t
)aadj
;
1612 /* The tolerances below are conservative. */
1613 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
) {
1614 if (aadj
< .4999999 || aadj
> .5000001)
1617 else if (aadj
< .4999999/FLT_RADIX
)
1630 return sign
? -rv
: rv
;
1636 (b
, S
) Bigint
*b
, *S
;
1638 (Bigint
*b
, Bigint
*S
)
1642 _G_int32_t borrow
, y
;
1643 unsigned32 carry
, q
, ys
;
1644 unsigned32
*bx
, *bxe
, *sx
, *sxe
;
1650 /*debug*/ if (b
->wds
> n
)
1651 /*debug*/ Bug("oversize b in quorem");
1659 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1661 /*debug*/ if (q
> 9)
1662 /*debug*/ Bug("oversized quotient in quorem");
1669 ys
= (si
& 0xffff) * q
+ carry
;
1670 zs
= (si
>> 16) * q
+ (ys
>> 16);
1672 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1674 Sign_Extend(borrow
, y
);
1675 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1677 Sign_Extend(borrow
, z
);
1683 while(--bxe
> bx
&& !*bxe
)
1688 if (cmp(b
, S
) >= 0) {
1696 ys
= (si
& 0xffff) + carry
;
1697 zs
= (si
>> 16) + (ys
>> 16);
1699 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1701 Sign_Extend(borrow
, y
);
1702 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1704 Sign_Extend(borrow
, z
);
1711 while(--bxe
> bx
&& !*bxe
)
1719 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1721 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1722 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1725 * 1. Rather than iterating, we use a simple numeric overestimate
1726 * to determine k = floor(log10(d)). We scale relevant
1727 * quantities using O(log2(k)) rather than O(k) multiplications.
1728 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1729 * try to generate digits strictly left to right. Instead, we
1730 * compute with fewer bits and propagate the carry if necessary
1731 * when rounding the final digit up. This is often faster.
1732 * 3. Under the assumption that input will be rounded nearest,
1733 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1734 * That is, we allow equality in stopping tests when the
1735 * round-nearest rule will give the same floating-point value
1736 * as would satisfaction of the stopping test with strict
1738 * 4. We remove common factors of powers of 2 from relevant
1740 * 5. When converting floating-point integers less than 1e16,
1741 * we use floating-point arithmetic rather than resorting
1742 * to multiple-precision integers.
1743 * 6. When asked to produce fewer than 15 digits, we first try
1744 * to get by with floating-point arithmetic; we resort to
1745 * multiple-precision integer arithmetic only if we cannot
1746 * guarantee that the floating-point calculation has given
1747 * the correctly rounded result. For k requested digits and
1748 * "uniformly" distributed input, the probability is
1749 * something like 10^(k-15) that we must resort to the long
1756 (d
, mode
, ndigits
, decpt
, sign
, rve
)
1757 double d
; int mode
, ndigits
, *decpt
, *sign
; char **rve
;
1759 (double d
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
)
1762 /* Arguments ndigits, decpt, sign are similar to those
1763 of ecvt and fcvt; trailing zeros are suppressed from
1764 the returned string. If not null, *rve is set to point
1765 to the end of the return value. If d is +-Infinity or NaN,
1766 then *decpt is set to 9999.
1769 0 ==> shortest string that yields d when read in
1770 and rounded to nearest.
1771 1 ==> like 0, but with Steele & White stopping rule;
1772 e.g. with IEEE P754 arithmetic , mode 0 gives
1773 1e23 whereas mode 1 gives 9.999999999999999e22.
1774 2 ==> max(1,ndigits) significant digits. This gives a
1775 return value similar to that of ecvt, except
1776 that trailing zeros are suppressed.
1777 3 ==> through ndigits past the decimal point. This
1778 gives a return value similar to that from fcvt,
1779 except that trailing zeros are suppressed, and
1780 ndigits can be negative.
1781 4-9 should give the same return values as 2-3, i.e.,
1782 4 <= mode <= 9 ==> same return as mode
1783 2 + (mode & 1). These modes are mainly for
1784 debugging; often they run slower but sometimes
1785 faster than modes 2-3.
1786 4,5,8,9 ==> left-to-right digit generation.
1787 6-9 ==> don't try fast floating-point estimate
1790 Values of mode other than 0-9 are treated as mode 0.
1792 Sufficient space is allocated to the return value
1793 to hold the suppressed trailing zeros.
1796 _G_int32_t bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
,
1797 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1798 spec_case
, try_quick
;
1800 #ifndef Sudden_Underflow
1803 Bigint _b_avail
, _b
, _mhi
, _mlo
, _S
;
1804 Bigint
*b_avail
= Binit(&_b_avail
);
1805 Bigint
*b
= Binit(&_b
);
1806 Bigint
*S
= Binit(&_S
);
1807 /* mhi and mlo are only set and used if leftright. */
1808 Bigint
*mhi
= NULL
, *mlo
= NULL
;
1811 static Bigint
*result
= NULL
;
1812 static int result_k
;
1816 /* result is contains a string, so its fields (interpreted
1817 as a Bigint have been trashed. Restore them.
1818 This is a really ugly interface - result should
1819 not be static, since that is not thread-safe. FIXME. */
1820 result
->k
= result_k
;
1821 result
->maxwds
= 1 << result_k
;
1822 result
->on_stack
= 0;
1825 if (word0(d
) & Sign_bit
) {
1826 /* set sign for everything, including 0's and NaNs */
1828 setword0(d
, word0(d
) & ~Sign_bit
); /* clear sign bit */
1833 #if defined(IEEE_Arith) + defined(VAX)
1835 if ((word0(d
) & Exp_mask
) == Exp_mask
)
1837 if (word0(d
) == 0x8000)
1840 /* Infinity or NaN */
1843 if (!word1(d
) && !(word0(d
) & 0xfffff))
1860 d
+= 0; /* normalize */
1870 b
= d2b(b
, d
, &be
, &bbits
);
1871 i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
));
1872 #ifndef Sudden_Underflow
1876 setword0(d2
, (word0(d2
) & Frac_mask1
) | Exp_11
);
1878 if (j
= 11 - hi0bits(word0(d2
) & Frac_mask
))
1887 #ifndef Sudden_Underflow
1891 /* d is denormalized */
1894 i
= bbits
+ be
+ (Bias
+ (P
-1) - 1);
1895 x
= i
> 32 ? word0(d
) << (64 - i
) | word1(d
) >> (i
- 32)
1896 : word1(d
) << (32 - i
);
1898 addword0(d2
, - 31*Exp_msk1
); /* adjust exponent */
1899 i
-= (Bias
+ (P
-1) - 1) + 1;
1904 /* Now i is the unbiased base-2 exponent. */
1906 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1907 * log10(x) = log(x) / log(10)
1908 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1909 * log10(d) = i*log(2)/log(10) + log10(d2)
1911 * This suggests computing an approximation k to log10(d) by
1913 * k = i*0.301029995663981
1914 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1916 * We want k to be too large rather than too small.
1917 * The error in the first-order Taylor series approximation
1918 * is in our favor, so we just round up the constant enough
1919 * to compensate for any error in the multiplication of
1920 * (i) by 0.301029995663981; since |i| <= 1077,
1921 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1922 * adding 1e-13 to the constant term more than suffices.
1923 * Hence we adjust the constant term to 0.1760912590558.
1924 * (We could get a more accurate k by invoking log10,
1925 * but this is probably not worthwhile.)
1928 ds
= (d2
-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
1930 if (ds
< 0. && ds
!= k
)
1931 k
--; /* want k = floor(ds) */
1933 if (k
>= 0 && k
<= Ten_pmax
) {
1957 if (mode
< 0 || mode
> 9)
1978 ilim
= ilim1
= i
= ndigits
;
1984 i
= ndigits
+ k
+ 1;
1990 /* i is now an upper bound of the number of digits to generate. */
1991 j
= sizeof(unsigned32
) * (1<<BIGINT_MINIMUM_K
);
1992 /* The test is <= so as to allow room for the final '\0'. */
1993 for(result_k
= BIGINT_MINIMUM_K
; BIGINT_HEADER_SIZE
+ j
<= i
;
1994 j
<<= 1) result_k
++;
1995 if (!result
|| result_k
> result
->k
)
1998 result
= Balloc(result_k
);
2000 s
= s0
= (char *)result
;
2002 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2004 /* Try to get by with floating-point arithmetic. */
2010 ieps
= 2; /* conservative */
2015 /* prevent overflows */
2017 d
/= bigtens
[n_bigtens
-1];
2020 for(; j
; j
>>= 1, i
++)
2027 else if ((j1
= -k
)) {
2028 d
*= tens
[j1
& 0xf];
2029 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
2035 if (k_check
&& d
< 1. && ilim
> 0) {
2044 addword0(eps
, - (P
-1)*Exp_msk1
);
2053 #ifndef No_leftright
2055 /* Use Steele & White method of only
2056 * generating digits needed.
2058 eps
= 0.5/tens
[ilim
-1] - eps
;
2062 *s
++ = '0' + (int)L
;
2075 /* Generate ilim digits, then fix them up. */
2076 eps
*= tens
[ilim
-1];
2077 for(i
= 1;; i
++, d
*= 10.) {
2080 *s
++ = '0' + (int)L
;
2084 else if (d
< 0.5 - eps
) {
2092 #ifndef No_leftright
2102 /* Do we have a "small" integer? */
2104 if (be
>= 0 && k
<= Int_max
) {
2107 if (ndigits
< 0 && ilim
<= 0) {
2108 if (ilim
< 0 || d
<= 5*ds
)
2113 L
= (_G_int32_t
)(d
/ ds
);
2115 #ifdef Check_FLT_ROUNDS
2116 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2122 *s
++ = '0' + (int)L
;
2125 if (d
> ds
|| (d
== ds
&& L
& 1)) {
2148 #ifndef Sudden_Underflow
2149 denorm
? be
+ (Bias
+ (P
-1) - 1 + 1) :
2152 1 + 4*P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
2166 if ((i
= ilim
) < 0) {
2173 mhi
= i2b(Binit(&_mhi
), 1);
2175 if (m2
> 0 && s2
> 0) {
2176 i
= m2
< s2
? m2
: s2
;
2185 mhi
= pow5mult(mhi
, m5
);
2186 b_tmp
= mult(b_avail
, mhi
, b
);
2194 b
= pow5mult(b
, b5
);
2198 S
= pow5mult(S
, s5
);
2200 /* Check for special case that d is a normalized power of 2. */
2203 if (!word1(d
) && !(word0(d
) & Bndry_mask
)
2204 #ifndef Sudden_Underflow
2205 && word0(d
) & Exp_mask
2208 /* The special case */
2217 /* Arrange for convenient computation of quotients:
2218 * shift left if necessary so divisor has 4 leading 0 bits.
2220 * Perhaps we should just compute leading 28 bits of S once
2221 * and for all and pass them and a shift to quorem, so it
2222 * can do shifts and ors to compute the numerator for q.
2224 if ((i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f))
2245 b
= multadd(b
, 10, 0); /* we botched the k estimate */
2247 mhi
= multadd(mhi
, 10, 0);
2251 if (ilim
<= 0 && mode
> 2) {
2252 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
2253 /* no digits, fcvt style */
2265 mhi
= lshift(mhi
, m2
);
2267 /* Compute mlo -- check for special case
2268 * that d is a normalized power of 2.
2272 mlo
= Brealloc(Binit(&_mlo
), mhi
->k
);
2274 mhi
= lshift(mhi
, Log2P
);
2280 dig
= quorem(b
,S
) + '0';
2281 /* Do we yet have the shortest decimal string
2282 * that will round to d?
2285 b_avail
= diff(b_avail
, S
, mhi
); /* b_avail = S - mi */
2286 j1
= b_avail
->sign
? 1 : cmp(b
, b_avail
);
2287 #ifndef ROUND_BIASED
2288 if (j1
== 0 && !mode
&& !(word1(d
) & 1)) {
2297 if (j
< 0 || (j
== 0 && !mode
2298 #ifndef ROUND_BIASED
2305 if ((j1
> 0 || (j1
== 0 && dig
& 1))
2313 if (dig
== '9') { /* possible if i == 1 */
2324 b
= multadd(b
, 10, 0);
2326 mlo
= mhi
= multadd(mhi
, 10, 0);
2328 mlo
= multadd(mlo
, 10, 0);
2329 mhi
= multadd(mhi
, 10, 0);
2335 *s
++ = dig
= quorem(b
,S
) + '0';
2338 b
= multadd(b
, 10, 0);
2341 /* Round off last digit */
2345 if (j
> 0 || (j
== 0 && dig
& 1)) {
2363 if (mlo
&& mlo
!= mhi
)
2375 #endif /* _IO_USE_DTOA */