2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /****************************************************************
27 * The author of this software is David M. Gay.
29 * Copyright (c) 1991 by AT&T.
31 * Permission to use, copy, modify, and distribute this software for any
32 * purpose without fee is hereby granted, provided that this entire notice
33 * is included in all copies of any software which is or includes a copy
34 * or modification of this software and in all copies of the supporting
35 * documentation for such software.
37 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
38 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
39 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
40 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
42 ***************************************************************/
44 /* Please send bug reports to
46 AT&T Bell Laboratories, Room 2C-463
48 Murray Hill, NJ 07974-2070
50 dmg@research.att.com or research!dmg
53 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
55 * This strtod returns a nearest machine number to the input decimal
56 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
57 * broken by the IEEE round-even rule. Otherwise ties are broken by
58 * biased rounding (add half and chop).
60 * Inspired loosely by William D. Clinger's paper "How to Read Floating
61 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
65 * 1. We only require IEEE, IBM, or VAX double-precision
66 * arithmetic (not IEEE double-extended).
67 * 2. We get by with floating-point arithmetic in a case that
68 * Clinger missed -- when we're computing d * 10^n
69 * for a small integer d and the integer n is not too
70 * much larger than 22 (the maximum integer k for which
71 * we can represent 10^k exactly), we may be able to
72 * compute (d*10^k) * 10^(e-k) with just one roundoff.
73 * 3. Rather than a bit-at-a-time adjustment of the binary
74 * result in the hard case, we use floating-point
75 * arithmetic to determine the adjustment to within
76 * one bit; only in really hard cases do we need to
77 * compute a second residual.
78 * 4. Because of 3., we don't need a large table of powers of 10
79 * for ten-to-e (just some small tables, e.g. of 10^k
84 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
85 * significant byte has the lowest address.
86 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
87 * significant byte has the lowest address.
88 * #define Long int on machines with 32-bit ints and 64-bit longs.
89 * #define Sudden_Underflow for IEEE-format machines without gradual
90 * underflow (i.e., that flush to zero on underflow).
91 * #define IBM for IBM mainframe-style floating-point arithmetic.
92 * #define VAX for VAX-style floating-point arithmetic.
93 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
94 * #define No_leftright to omit left-right logic in fast floating-point
95 * computation of dtoa.
96 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
97 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
98 * that use extended-precision instructions to compute rounded
99 * products and quotients) with IBM.
100 * #define ROUND_BIASED for IEEE-format with biased rounding.
101 * #define Inaccurate_Divide for IEEE-format with correctly rounded
102 * products but inaccurate quotients, e.g., for Intel i860.
103 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
104 * integer arithmetic. Whether this speeds things up or slows things
105 * down depends on the machine and the number being converted.
106 * #define KR_headers for old-style C function headers.
107 * #define Bad_float_h if your system lacks a float.h or if it does not
108 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
109 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
110 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
111 * if memory is available and otherwise does something you deem
112 * appropriate. If MALLOC is undefined, malloc will be invoked
113 * directly -- and assumed always to succeed.
116 #if defined(LIBC_SCCS) && !defined(lint)
117 static char *rcsid
= "$OpenBSD: strtod.c,v 1.9 1997/03/25 17:07:30 rahnds Exp $";
118 #endif /* LIBC_SCCS and not lint */
120 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
121 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
122 defined(__powerpc__) || defined(__m88k__) || defined(__ppc__)
123 #include <sys/types.h>
124 #if BYTE_ORDER == BIG_ENDIAN
125 #define IEEE_BIG_ENDIAN
127 #define IEEE_LITTLE_ENDIAN
133 * Although the CPU is little endian the FP has different
134 * byte and word endianness. The byte order is still little endian
135 * but the word order is big endian.
137 #define IEEE_BIG_ENDIAN
145 #define ULong u_int32_t
149 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
168 extern char *MALLOC();
170 extern void *MALLOC(size_t);
173 #define MALLOC malloc
179 #if defined(__APPLE__)
181 * Temporarily define this, so we avoid pulling in symbols from libm
188 #ifdef IEEE_BIG_ENDIAN
189 #define IEEE_ARITHMETIC
191 #ifdef IEEE_LITTLE_ENDIAN
192 #define IEEE_ARITHMETIC
195 #ifdef IEEE_ARITHMETIC
197 #define DBL_MAX_10_EXP 308
198 #define DBL_MAX_EXP 1024
201 #define DBL_MAX 1.7976931348623157e+308
206 #define DBL_MAX_10_EXP 75
207 #define DBL_MAX_EXP 63
210 #define DBL_MAX 7.2370055773322621e+75
215 #define DBL_MAX_10_EXP 38
216 #define DBL_MAX_EXP 127
219 #define DBL_MAX 1.7014118346046923e+38
223 #define LONG_MAX 2147483647
238 #define CONST /* blank */
244 #ifdef Unsigned_Shifts
245 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
247 #define Sign_Extend(a,b) /*no-op*/
250 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
252 Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN
, VAX
, or
253 IBM should be defined
.
256 #ifdef IEEE_LITTLE_ENDIAN
257 #define word0(x) ((ULong *)&x)[1]
258 #define word1(x) ((ULong *)&x)[0]
260 #define word0(x) ((ULong *)&x)[0]
261 #define word1(x) ((ULong *)&x)[1]
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)
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++)
272 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
273 ((unsigned short *)a)[1] = (unsigned short)c, a++)
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) */
282 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
284 #define Exp_shift1 20
285 #define Exp_msk1 0x100000
286 #define Exp_msk11 0x100000
287 #define Exp_mask 0x7ff00000
292 #define Exp_1 0x3ff00000
293 #define Exp_11 0x3ff00000
295 #define Frac_mask 0xfffff
296 #define Frac_mask1 0xfffff
299 #define Bndry_mask 0xfffff
300 #define Bndry_mask1 0xfffff
302 #define Sign_bit 0x80000000
308 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
310 #undef Sudden_Underflow
311 #define Sudden_Underflow
314 #define Exp_shift1 24
315 #define Exp_msk1 0x1000000
316 #define Exp_msk11 0x1000000
317 #define Exp_mask 0x7f000000
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
327 #define Bndry_mask 0xefffff
328 #define Bndry_mask1 0xffffff
330 #define Sign_bit 0x80000000
332 #define Tiny0 0x100000
339 #define Exp_msk1 0x80
340 #define Exp_msk11 0x800000
341 #define Exp_mask 0x7f80
344 #define Exp_1 0x40800000
345 #define Exp_11 0x4080
347 #define Frac_mask 0x7fffff
348 #define Frac_mask1 0xffff007f
351 #define Bndry_mask 0xffff007f
352 #define Bndry_mask1 0xffff007f
354 #define Sign_bit 0x8000
368 #define rounded_product(a,b) a = rnd_prod(a, b)
369 #define rounded_quotient(a,b) a = rnd_quot(a, b)
371 extern double rnd_prod(), rnd_quot();
373 extern double rnd_prod(double, double), rnd_quot(double, double);
376 #define rounded_product(a,b) a *= b
377 #define rounded_quotient(a,b) a /= b
380 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
381 #define Big1 0xffffffff
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.
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
, char **resultp
);
405 int k
, maxwds
, sign
, wds
;
409 typedef struct Bigint Bigint
;
423 rv
= (Bigint
*)malloc(sizeof(Bigint
) + (x
-1)*sizeof(Long
));
426 rv
->sign
= rv
->wds
= 0;
441 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
442 y->wds*sizeof(Long) + 2*sizeof(int))
447 (b
, m
, a
) Bigint
*b
; int m
, a
;
449 (Bigint
*b
, int m
, int a
) /* multiply by m and add a */
465 y
= (xi
& 0xffff) * m
+ a
;
466 z
= (xi
>> 16) * m
+ (y
>> 16);
468 *x
++ = (z
<< 16) + (y
& 0xffff);
477 if (wds
>= b
->maxwds
) {
492 (s
, nd0
, nd
, y9
) CONST
char *s
; int nd0
, nd
; ULong y9
;
494 (CONST
char *s
, int nd0
, int nd
, ULong y9
)
502 for(k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) ;
509 b
->x
[0] = y9
& 0xffff;
510 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
516 do b
= multadd(b
, 10, *s
++ - '0');
523 b
= multadd(b
, 10, *s
++ - '0');
530 (x
) register ULong x
;
537 if (!(x
& 0xffff0000)) {
541 if (!(x
& 0xff000000)) {
545 if (!(x
& 0xf0000000)) {
549 if (!(x
& 0xc0000000)) {
553 if (!(x
& 0x80000000)) {
555 if (!(x
& 0x40000000))
570 register ULong x
= *y
;
628 (a
, b
) Bigint
*a
, *b
;
630 (Bigint
*a
, Bigint
*b
)
636 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
641 if (a
->wds
< b
->wds
) {
653 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
661 for(; xb
< xbe
; xb
++, xc0
++) {
662 if (y
= *xb
& 0xffff) {
667 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
669 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
682 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
685 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
693 for(; xb
< xbe
; xc0
++) {
699 z
= *x
++ * y
+ *xc
+ carry
;
708 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
718 (b
, k
) Bigint
*b
; int k
;
723 Bigint
*b1
, *p5
, *p51
;
725 static int p05
[3] = { 5, 25, 125 };
728 b
= multadd(b
, p05
[i
-1], 0);
745 if (!(p51
= p5
->next
)) {
746 p51
= p5
->next
= mult(p5
,p5
);
757 (b
, k
) Bigint
*b
; int k
;
764 ULong
*x
, *x1
, *xe
, z
;
773 for(i
= b
->maxwds
; n1
> i
; i
<<= 1)
777 for(i
= 0; i
< n
; i
++)
798 *x1
++ = *x
<< k
& 0xffff | z
;
817 (a
, b
) Bigint
*a
, *b
;
819 (Bigint
*a
, Bigint
*b
)
822 ULong
*xa
, *xa0
, *xb
, *xb0
;
828 if (i
> 1 && !a
->x
[i
-1])
829 Bug("cmp called with a->x[a->wds-1] == 0");
830 if (j
> 1 && !b
->x
[j
-1])
831 Bug("cmp called with b->x[b->wds-1] == 0");
841 return *xa
< *xb
? -1 : 1;
851 (a
, b
) Bigint
*a
, *b
;
853 (Bigint
*a
, Bigint
*b
)
858 Long borrow
, y
; /* We need signed shifts here. */
859 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
891 y
= (*xa
& 0xffff) - (*xb
& 0xffff) + borrow
;
893 Sign_Extend(borrow
, y
);
894 z
= (*xa
++ >> 16) - (*xb
++ >> 16) + borrow
;
896 Sign_Extend(borrow
, z
);
901 y
= (*xa
& 0xffff) + borrow
;
903 Sign_Extend(borrow
, y
);
904 z
= (*xa
++ >> 16) + borrow
;
906 Sign_Extend(borrow
, z
);
911 y
= *xa
++ - *xb
++ + borrow
;
913 Sign_Extend(borrow
, y
);
920 Sign_Extend(borrow
, y
);
941 L
= (word0(x
) & Exp_mask
) - (P
-1)*Exp_msk1
;
942 #ifndef Sudden_Underflow
950 #ifndef Sudden_Underflow
955 word0(a
) = 0x80000 >> L
;
961 word1(a
) = L
>= 31 ? 1 : 1 << 31 - L
;
971 (a
, e
) Bigint
*a
; int *e
;
976 ULong
*xa
, *xa0
, w
, y
, z
;
990 if (!y
) Bug("zero y in b2d");
996 d0
= Exp_1
| y
>> Ebits
- k
;
997 w
= xa
> xa0
? *--xa
: 0;
998 d1
= y
<< (32-Ebits
) + k
| w
>> Ebits
- k
;
1001 z
= xa
> xa0
? *--xa
: 0;
1003 d0
= Exp_1
| y
<< k
| z
>> 32 - k
;
1004 y
= xa
> xa0
? *--xa
: 0;
1005 d1
= z
<< k
| y
>> 32 - k
;
1012 if (k
< Ebits
+ 16) {
1013 z
= xa
> xa0
? *--xa
: 0;
1014 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
1015 w
= xa
> xa0
? *--xa
: 0;
1016 y
= xa
> xa0
? *--xa
: 0;
1017 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
1020 z
= xa
> xa0
? *--xa
: 0;
1021 w
= xa
> xa0
? *--xa
: 0;
1023 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
1024 y
= xa
> xa0
? *--xa
: 0;
1025 d1
= w
<< k
+ 16 | y
<< k
;
1029 word0(d
) = d0
>> 16 | d0
<< 16;
1030 word1(d
) = d1
>> 16 | d1
<< 16;
1041 (d
, e
, bits
) double d
; int *e
, *bits
;
1043 (double d
, int *e
, int *bits
)
1051 d0
= word0(d
) >> 16 | word0(d
) << 16;
1052 d1
= word1(d
) >> 16 | word1(d
) << 16;
1066 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1067 #ifdef Sudden_Underflow
1068 de
= (int)(d0
>> Exp_shift
);
1073 if (de
= (int)(d0
>> Exp_shift
))
1078 if (k
= lo0bits(&y
)) {
1079 x
[0] = y
| z
<< 32 - k
;
1084 i
= b
->wds
= (x
[1] = z
) ? 2 : 1;
1089 Bug("Zero passed to d2b");
1098 if (k
= lo0bits(&y
))
1100 x
[0] = y
| z
<< 32 - k
& 0xffff;
1101 x
[1] = z
>> k
- 16 & 0xffff;
1107 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1108 x
[2] = z
>> k
& 0xffff;
1123 Bug("Zero passed to d2b");
1141 #ifndef Sudden_Underflow
1145 *e
= (de
- Bias
- (P
-1) << 2) + k
;
1146 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1148 *e
= de
- Bias
- (P
-1) + k
;
1151 #ifndef Sudden_Underflow
1154 *e
= de
- Bias
- (P
-1) + 1 + k
;
1156 *bits
= 32*i
- hi0bits(x
[i
-1]);
1158 *bits
= (i
+2)*16 - hi0bits(x
[i
]);
1170 (a
, b
) Bigint
*a
, *b
;
1172 (Bigint
*a
, Bigint
*b
)
1181 k
= ka
- kb
+ 32*(a
->wds
- b
->wds
);
1183 k
= ka
- kb
+ 16*(a
->wds
- b
->wds
);
1187 word0(da
) += (k
>> 2)*Exp_msk1
;
1193 word0(db
) += (k
>> 2)*Exp_msk1
;
1199 word0(da
) += k
*Exp_msk1
;
1202 word0(db
) += k
*Exp_msk1
;
1210 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1211 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1219 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1220 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1224 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
};
1225 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
1228 static CONST
double bigtens
[] = { 1e16
, 1e32
};
1229 static CONST
double tinytens
[] = { 1e-16, 1e-32 };
1237 (s00
, se
) CONST
char *s00
; char **se
;
1239 (CONST
char *s00
, char **se
)
1242 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1243 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1244 CONST
char *s
, *s0
, *s1
;
1245 double aadj
, aadj1
, adj
, rv
, rv0
;
1248 Bigint
*bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
;
1251 CONST
char decimal_point
= localeconv()->decimal_point
[0];
1253 CONST
char decimal_point
= '.';
1256 sign
= nz0
= nz
= 0;
1260 for(s
= s00
; isspace((unsigned char) *s
); s
++)
1266 } else if (*s
== '+') {
1277 while(*++s
== '0') ;
1283 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1289 if (c
== decimal_point
) {
1292 for(; c
== '0'; c
= *++s
)
1294 if (c
> '0' && c
<= '9') {
1302 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
1307 for(i
= 1; i
< nz
; i
++)
1310 else if (nd
<= DBL_DIG
+ 1)
1314 else if (nd
<= DBL_DIG
+ 1)
1322 if (c
== 'e' || c
== 'E') {
1323 if (!nd
&& !nz
&& !nz0
) {
1335 if (c
>= '0' && c
<= '9') {
1338 if (c
> '0' && c
<= '9') {
1341 while((c
= *++s
) >= '0' && c
<= '9')
1343 if (s
- s1
> 8 || L
> 19999)
1344 /* Avoid confusion from exponents
1345 * so large that e might overflow.
1347 e
= 19999; /* safe for 16 bit ints */
1366 /* Now we have nd0 digits, starting at s0, followed by a
1367 * decimal point, followed by nd-nd0 digits. The number we're
1368 * after is the integer represented by those digits times
1373 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1376 rv
= tens
[k
- 9] * rv
+ z
;
1379 #ifndef RND_PRODQUOT
1386 if (e
<= Ten_pmax
) {
1388 goto vax_ovfl_check
;
1390 /* rv = */ rounded_product(rv
, tens
[e
]);
1395 if (e
<= Ten_pmax
+ i
) {
1396 /* A fancier test would sometimes let us do
1397 * this for larger i values.
1402 /* VAX exponent range is so narrow we must
1403 * worry about overflow here...
1406 word0(rv
) -= P
*Exp_msk1
;
1407 /* rv = */ rounded_product(rv
, tens
[e
]);
1408 if ((word0(rv
) & Exp_mask
)
1409 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
1411 word0(rv
) += P
*Exp_msk1
;
1413 /* rv = */ rounded_product(rv
, tens
[e
]);
1418 #ifndef Inaccurate_Divide
1419 else if (e
>= -Ten_pmax
) {
1420 /* rv = */ rounded_quotient(rv
, tens
[-e
]);
1427 /* Get starting approximation = rv * 10**e1 */
1433 if (e1
> DBL_MAX_10_EXP
) {
1439 /* Can't trust HUGE_VAL */
1441 word0(rv
) = Exp_mask
;
1453 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1456 /* The last multiplication could overflow. */
1457 word0(rv
) -= P
*Exp_msk1
;
1459 if ((z
= word0(rv
) & Exp_mask
)
1460 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
1462 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
1463 /* set to largest number */
1464 /* (Can't trust DBL_MAX) */
1469 word0(rv
) += P
*Exp_msk1
;
1480 if (e1
>= 1 << n_bigtens
)
1482 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1485 /* The last multiplication could underflow. */
1501 /* The refinement below will clean
1502 * this approximation up.
1508 /* Now the hard part -- adjusting rv to the correct value.*/
1510 /* Put digits into bd: true value = bd * 10^e */
1512 bd0
= s2b(s0
, nd0
, nd
, y
);
1515 bd
= Balloc(bd0
->k
);
1517 bb
= d2b(rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1533 #ifdef Sudden_Underflow
1535 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
1540 i
= bbe
+ bbbits
- 1; /* logb(rv) */
1541 if (i
< Emin
) /* denormal */
1548 i
= bb2
< bd2
? bb2
: bd2
;
1557 bs
= pow5mult(bs
, bb5
);
1563 bb
= lshift(bb
, bb2
);
1565 bd
= pow5mult(bd
, bd5
);
1567 bd
= lshift(bd
, bd2
);
1569 bs
= lshift(bs
, bs2
);
1570 delta
= diff(bb
, bd
);
1571 dsign
= delta
->sign
;
1575 /* Error is less than half an ulp -- check for
1576 * special case of mantissa a power of two.
1578 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
)
1580 delta
= lshift(delta
,Log2P
);
1581 if (cmp(delta
, bs
) > 0)
1586 /* exactly half-way between */
1588 if ((word0(rv
) & Bndry_mask1
) == Bndry_mask1
1589 && word1(rv
) == 0xffffffff) {
1590 /*boundary case -- increment exponent*/
1591 word0(rv
) = (word0(rv
) & Exp_mask
)
1601 else if (!(word0(rv
) & Bndry_mask
) && !word1(rv
)) {
1603 /* boundary case -- decrement exponent */
1604 #ifdef Sudden_Underflow
1605 L
= word0(rv
) & Exp_mask
;
1614 L
= (word0(rv
) & Exp_mask
) - Exp_msk1
;
1616 word0(rv
) = L
| Bndry_mask1
;
1617 word1(rv
) = 0xffffffff;
1624 #ifndef ROUND_BIASED
1625 if (!(word1(rv
) & LSB
))
1630 #ifndef ROUND_BIASED
1633 #ifndef Sudden_Underflow
1641 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1644 else if (word1(rv
) || word0(rv
) & Bndry_mask
) {
1645 #ifndef Sudden_Underflow
1646 if (word1(rv
) == Tiny1
&& !word0(rv
))
1653 /* special case -- power of FLT_RADIX to be */
1654 /* rounded down... */
1656 if (aadj
< 2./FLT_RADIX
)
1657 aadj
= 1./FLT_RADIX
;
1665 aadj1
= dsign
? aadj
: -aadj
;
1666 #ifdef Check_FLT_ROUNDS
1667 switch(FLT_ROUNDS
) {
1668 case 2: /* towards +infinity */
1671 case 0: /* towards 0 */
1672 case 3: /* towards -infinity */
1676 if (FLT_ROUNDS
== 0)
1680 y
= word0(rv
) & Exp_mask
;
1682 /* Check for overflow */
1684 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
1686 word0(rv
) -= P
*Exp_msk1
;
1687 adj
= aadj1
* ulp(rv
);
1689 if ((word0(rv
) & Exp_mask
) >=
1690 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
1691 if (word0(rv0
) == Big0
&& word1(rv0
) == Big1
)
1698 word0(rv
) += P
*Exp_msk1
;
1701 #ifdef Sudden_Underflow
1702 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
1704 word0(rv
) += P
*Exp_msk1
;
1705 adj
= aadj1
* ulp(rv
);
1708 if ((word0(rv
) & Exp_mask
) < P
*Exp_msk1
)
1710 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
)
1713 if (word0(rv0
) == Tiny0
1714 && word1(rv0
) == Tiny1
)
1721 word0(rv
) -= P
*Exp_msk1
;
1724 adj
= aadj1
* ulp(rv
);
1728 /* Compute adj so that the IEEE rounding rules will
1729 * correctly round rv + adj in some half-way cases.
1730 * If rv * ulp(rv) is denormalized (i.e.,
1731 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1732 * trouble from bits lost to denormalization;
1733 * example: 1.2e-307 .
1735 if (y
<= (P
-1)*Exp_msk1
&& aadj
>= 1.) {
1736 aadj1
= (double)(int)(aadj
+ 0.5);
1740 adj
= aadj1
* ulp(rv
);
1744 z
= word0(rv
) & Exp_mask
;
1746 /* Can we stop now? */
1749 /* The tolerances below are conservative. */
1750 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
) {
1751 if (aadj
< .4999999 || aadj
> .5000001)
1754 else if (aadj
< .4999999/FLT_RADIX
)
1772 return sign
? -rv
: rv
;
1778 (b
, S
) Bigint
*b
, *S
;
1780 (Bigint
*b
, Bigint
*S
)
1786 ULong
*bx
, *bxe
, *sx
, *sxe
;
1794 /*debug*/ if (b
->wds
> n
)
1795 /*debug*/ Bug("oversize b in quorem");
1803 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1805 /*debug*/ if (q
> 9)
1806 /*debug*/ Bug("oversized quotient in quorem");
1814 ys
= (si
& 0xffff) * q
+ carry
;
1815 zs
= (si
>> 16) * q
+ (ys
>> 16);
1817 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1819 Sign_Extend(borrow
, y
);
1820 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1822 Sign_Extend(borrow
, z
);
1825 ys
= *sx
++ * q
+ carry
;
1827 y
= *bx
- (ys
& 0xffff) + borrow
;
1829 Sign_Extend(borrow
, y
);
1836 while(--bxe
> bx
&& !*bxe
)
1841 if (cmp(b
, S
) >= 0) {
1850 ys
= (si
& 0xffff) + carry
;
1851 zs
= (si
>> 16) + (ys
>> 16);
1853 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1855 Sign_Extend(borrow
, y
);
1856 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1858 Sign_Extend(borrow
, z
);
1863 y
= *bx
- (ys
& 0xffff) + borrow
;
1865 Sign_Extend(borrow
, y
);
1873 while(--bxe
> bx
&& !*bxe
)
1881 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1883 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1884 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1887 * 1. Rather than iterating, we use a simple numeric overestimate
1888 * to determine k = floor(log10(d)). We scale relevant
1889 * quantities using O(log2(k)) rather than O(k) multiplications.
1890 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1891 * try to generate digits strictly left to right. Instead, we
1892 * compute with fewer bits and propagate the carry if necessary
1893 * when rounding the final digit up. This is often faster.
1894 * 3. Under the assumption that input will be rounded nearest,
1895 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1896 * That is, we allow equality in stopping tests when the
1897 * round-nearest rule will give the same floating-point value
1898 * as would satisfaction of the stopping test with strict
1900 * 4. We remove common factors of powers of 2 from relevant
1902 * 5. When converting floating-point integers less than 1e16,
1903 * we use floating-point arithmetic rather than resorting
1904 * to multiple-precision integers.
1905 * 6. When asked to produce fewer than 15 digits, we first try
1906 * to get by with floating-point arithmetic; we resort to
1907 * multiple-precision integer arithmetic only if we cannot
1908 * guarantee that the floating-point calculation has given
1909 * the correctly rounded result. For k requested digits and
1910 * "uniformly" distributed input, the probability is
1911 * something like 10^(k-15) that we must resort to the Long
1918 (d
, mode
, ndigits
, decpt
, sign
, rve
)
1919 double d
; int mode
, ndigits
, *decpt
, *sign
; char **rve
, char **resultp
;
1921 (double d
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
, char **resultp
)
1924 /* Arguments ndigits, decpt, sign are similar to those
1925 of ecvt and fcvt; trailing zeros are suppressed from
1926 the returned string. If not null, *rve is set to point
1927 to the end of the return value. If d is +-Infinity or NaN,
1928 then *decpt is set to 9999.
1931 0 ==> shortest string that yields d when read in
1932 and rounded to nearest.
1933 1 ==> like 0, but with Steele & White stopping rule;
1934 e.g. with IEEE P754 arithmetic , mode 0 gives
1935 1e23 whereas mode 1 gives 9.999999999999999e22.
1936 2 ==> max(1,ndigits) significant digits. This gives a
1937 return value similar to that of ecvt, except
1938 that trailing zeros are suppressed.
1939 3 ==> through ndigits past the decimal point. This
1940 gives a return value similar to that from fcvt,
1941 except that trailing zeros are suppressed, and
1942 ndigits can be negative.
1943 4-9 should give the same return values as 2-3, i.e.,
1944 4 <= mode <= 9 ==> same return as mode
1945 2 + (mode & 1). These modes are mainly for
1946 debugging; often they run slower but sometimes
1947 faster than modes 2-3.
1948 4,5,8,9 ==> left-to-right digit generation.
1949 6-9 ==> don't try fast floating-point estimate
1952 Values of mode other than 0-9 are treated as mode 0.
1954 Sufficient space is allocated to the return value
1955 to hold the suppressed trailing zeros.
1958 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
,
1959 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1960 spec_case
, try_quick
;
1962 #ifndef Sudden_Underflow
1966 Bigint
*b
, *b1
, *delta
, *mlo
, *mhi
, *S
;
1970 if (word0(d
) & Sign_bit
) {
1971 /* set sign for everything, including 0's and NaNs */
1973 word0(d
) &= ~Sign_bit
; /* clear sign bit */
1978 #if defined(IEEE_Arith) + defined(VAX)
1980 if ((word0(d
) & Exp_mask
) == Exp_mask
)
1982 if (word0(d
) == 0x8000)
1985 /* Infinity or NaN */
1989 !word1(d
) && !(word0(d
) & 0xfffff) ? "Infinity" :
2002 d
+= 0; /* normalize */
2012 b
= d2b(d
, &be
, &bbits
);
2013 #ifdef Sudden_Underflow
2014 i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
));
2016 if (i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
))) {
2019 word0(d2
) &= Frac_mask1
;
2020 word0(d2
) |= Exp_11
;
2022 if (j
= 11 - hi0bits(word0(d2
) & Frac_mask
))
2026 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2027 * log10(x) = log(x) / log(10)
2028 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2029 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2031 * This suggests computing an approximation k to log10(d) by
2033 * k = (i - Bias)*0.301029995663981
2034 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2036 * We want k to be too large rather than too small.
2037 * The error in the first-order Taylor series approximation
2038 * is in our favor, so we just round up the constant enough
2039 * to compensate for any error in the multiplication of
2040 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2041 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2042 * adding 1e-13 to the constant term more than suffices.
2043 * Hence we adjust the constant term to 0.1760912590558.
2044 * (We could get a more accurate k by invoking log10,
2045 * but this is probably not worthwhile.)
2053 #ifndef Sudden_Underflow
2057 /* d is denormalized */
2059 i
= bbits
+ be
+ (Bias
+ (P
-1) - 1);
2060 x
= i
> 32 ? word0(d
) << 64 - i
| word1(d
) >> i
- 32
2061 : word1(d
) << 32 - i
;
2063 word0(d2
) -= 31*Exp_msk1
; /* adjust exponent */
2064 i
-= (Bias
+ (P
-1) - 1) + 1;
2068 ds
= (d2
-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
2070 if (ds
< 0. && ds
!= k
)
2071 k
--; /* want k = floor(ds) */
2073 if (k
>= 0 && k
<= Ten_pmax
) {
2097 if (mode
< 0 || mode
> 9)
2118 ilim
= ilim1
= i
= ndigits
;
2124 i
= ndigits
+ k
+ 1;
2130 *resultp
= (char *) malloc(i
+ 1);
2133 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2135 /* Try to get by with floating-point arithmetic. */
2141 ieps
= 2; /* conservative */
2146 /* prevent overflows */
2148 d
/= bigtens
[n_bigtens
-1];
2151 for(; j
; j
>>= 1, i
++)
2159 d
*= tens
[j1
& 0xf];
2160 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
2166 if (k_check
&& d
< 1. && ilim
> 0) {
2175 word0(eps
) -= (P
-1)*Exp_msk1
;
2185 #ifndef No_leftright
2187 /* Use Steele & White method of only
2188 * generating digits needed.
2190 eps
= 0.5/tens
[ilim
-1] - eps
;
2194 *s
++ = '0' + (int)L
;
2207 /* Generate ilim digits, then fix them up. */
2208 eps
*= tens
[ilim
-1];
2209 for(i
= 1;; i
++, d
*= 10.) {
2212 *s
++ = '0' + (int)L
;
2216 else if (d
< 0.5 - eps
) {
2224 #ifndef No_leftright
2234 /* Do we have a "small" integer? */
2236 if (be
>= 0 && k
<= Int_max
) {
2239 if (ndigits
< 0 && ilim
<= 0) {
2241 if (ilim
< 0 || d
<= 5*ds
)
2248 #ifdef Check_FLT_ROUNDS
2249 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2255 *s
++ = '0' + (int)L
;
2258 if (d
> ds
|| d
== ds
&& L
& 1) {
2282 #ifndef Sudden_Underflow
2283 denorm
? be
+ (Bias
+ (P
-1) - 1 + 1) :
2286 1 + 4*P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
2300 if ((i
= ilim
) < 0) {
2309 if (m2
> 0 && s2
> 0) {
2310 i
= m2
< s2
? m2
: s2
;
2318 mhi
= pow5mult(mhi
, m5
);
2327 b
= pow5mult(b
, b5
);
2331 S
= pow5mult(S
, s5
);
2333 /* Check for special case that d is a normalized power of 2. */
2336 if (!word1(d
) && !(word0(d
) & Bndry_mask
)
2337 #ifndef Sudden_Underflow
2338 && word0(d
) & Exp_mask
2341 /* The special case */
2350 /* Arrange for convenient computation of quotients:
2351 * shift left if necessary so divisor has 4 leading 0 bits.
2353 * Perhaps we should just compute leading 28 bits of S once
2354 * and for all and pass them and a shift to quorem, so it
2355 * can do shifts and ors to compute the numerator for q.
2358 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f)
2361 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0xf)
2383 b
= multadd(b
, 10, 0); /* we botched the k estimate */
2385 mhi
= multadd(mhi
, 10, 0);
2389 if (ilim
<= 0 && mode
> 2) {
2390 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
2391 /* no digits, fcvt style */
2403 mhi
= lshift(mhi
, m2
);
2405 /* Compute mlo -- check for special case
2406 * that d is a normalized power of 2.
2411 mhi
= Balloc(mhi
->k
);
2413 mhi
= lshift(mhi
, Log2P
);
2417 dig
= quorem(b
,S
) + '0';
2418 /* Do we yet have the shortest decimal string
2419 * that will round to d?
2422 delta
= diff(S
, mhi
);
2423 j1
= delta
->sign
? 1 : cmp(b
, delta
);
2425 #ifndef ROUND_BIASED
2426 if (j1
== 0 && !mode
&& !(word1(d
) & 1)) {
2435 if (j
< 0 || j
== 0 && !mode
2436 #ifndef ROUND_BIASED
2443 if ((j1
> 0 || j1
== 0 && dig
& 1)
2451 if (dig
== '9') { /* possible if i == 1 */
2462 b
= multadd(b
, 10, 0);
2464 mlo
= mhi
= multadd(mhi
, 10, 0);
2466 mlo
= multadd(mlo
, 10, 0);
2467 mhi
= multadd(mhi
, 10, 0);
2473 *s
++ = dig
= quorem(b
,S
) + '0';
2476 b
= multadd(b
, 10, 0);
2479 /* Round off last digit */
2483 if (j
> 0 || j
== 0 && dig
& 1) {
2500 if (mlo
&& mlo
!= mhi
)
2506 if (s
== s0
) { /* don't return empty string */