2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /****************************************************************
24 * The author of this software is David M. Gay.
26 * Copyright (c) 1991 by AT&T.
28 * Permission to use, copy, modify, and distribute this software for any
29 * purpose without fee is hereby granted, provided that this entire notice
30 * is included in all copies of any software which is or includes a copy
31 * or modification of this software and in all copies of the supporting
32 * documentation for such software.
34 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
35 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
36 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
37 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
39 ***************************************************************/
41 /* Please send bug reports to
43 AT&T Bell Laboratories, Room 2C-463
45 Murray Hill, NJ 07974-2070
47 dmg@research.att.com or research!dmg
50 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
52 * This strtod returns a nearest machine number to the input decimal
53 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
54 * broken by the IEEE round-even rule. Otherwise ties are broken by
55 * biased rounding (add half and chop).
57 * Inspired loosely by William D. Clinger's paper "How to Read Floating
58 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
62 * 1. We only require IEEE, IBM, or VAX double-precision
63 * arithmetic (not IEEE double-extended).
64 * 2. We get by with floating-point arithmetic in a case that
65 * Clinger missed -- when we're computing d * 10^n
66 * for a small integer d and the integer n is not too
67 * much larger than 22 (the maximum integer k for which
68 * we can represent 10^k exactly), we may be able to
69 * compute (d*10^k) * 10^(e-k) with just one roundoff.
70 * 3. Rather than a bit-at-a-time adjustment of the binary
71 * result in the hard case, we use floating-point
72 * arithmetic to determine the adjustment to within
73 * one bit; only in really hard cases do we need to
74 * compute a second residual.
75 * 4. Because of 3., we don't need a large table of powers of 10
76 * for ten-to-e (just some small tables, e.g. of 10^k
81 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
82 * significant byte has the lowest address.
83 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
84 * significant byte has the lowest address.
85 * #define Long int on machines with 32-bit ints and 64-bit longs.
86 * #define Sudden_Underflow for IEEE-format machines without gradual
87 * underflow (i.e., that flush to zero on underflow).
88 * #define IBM for IBM mainframe-style floating-point arithmetic.
89 * #define VAX for VAX-style floating-point arithmetic.
90 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
91 * #define No_leftright to omit left-right logic in fast floating-point
92 * computation of dtoa.
93 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
94 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
95 * that use extended-precision instructions to compute rounded
96 * products and quotients) with IBM.
97 * #define ROUND_BIASED for IEEE-format with biased rounding.
98 * #define Inaccurate_Divide for IEEE-format with correctly rounded
99 * products but inaccurate quotients, e.g., for Intel i860.
100 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
101 * integer arithmetic. Whether this speeds things up or slows things
102 * down depends on the machine and the number being converted.
103 * #define KR_headers for old-style C function headers.
104 * #define Bad_float_h if your system lacks a float.h or if it does not
105 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
106 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
107 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
108 * if memory is available and otherwise does something you deem
109 * appropriate. If MALLOC is undefined, malloc will be invoked
110 * directly -- and assumed always to succeed.
113 #if defined(LIBC_SCCS) && !defined(lint)
114 static char *rcsid
= "$OpenBSD: strtod.c,v 1.9 1997/03/25 17:07:30 rahnds Exp $";
115 #endif /* LIBC_SCCS and not lint */
117 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
118 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
119 defined(__powerpc__) || defined(__m88k__) || defined(__ppc__)
120 #include <sys/types.h>
121 #if BYTE_ORDER == BIG_ENDIAN
122 #define IEEE_BIG_ENDIAN
124 #define IEEE_LITTLE_ENDIAN
130 * Although the CPU is little endian the FP has different
131 * byte and word endianness. The byte order is still little endian
132 * but the word order is big endian.
134 #define IEEE_BIG_ENDIAN
142 #define ULong u_int32_t
146 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
165 extern char *MALLOC();
167 extern void *MALLOC(size_t);
170 #define MALLOC malloc
178 #ifdef IEEE_BIG_ENDIAN
179 #define IEEE_ARITHMETIC
181 #ifdef IEEE_LITTLE_ENDIAN
182 #define IEEE_ARITHMETIC
185 #ifdef IEEE_ARITHMETIC
187 #define DBL_MAX_10_EXP 308
188 #define DBL_MAX_EXP 1024
191 #define DBL_MAX 1.7976931348623157e+308
196 #define DBL_MAX_10_EXP 75
197 #define DBL_MAX_EXP 63
200 #define DBL_MAX 7.2370055773322621e+75
205 #define DBL_MAX_10_EXP 38
206 #define DBL_MAX_EXP 127
209 #define DBL_MAX 1.7014118346046923e+38
213 #define LONG_MAX 2147483647
228 #define CONST /* blank */
234 #ifdef Unsigned_Shifts
235 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
237 #define Sign_Extend(a,b) /*no-op*/
240 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
242 Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN
, VAX
, or
243 IBM should be defined
.
246 #ifdef IEEE_LITTLE_ENDIAN
247 #define word0(x) ((ULong *)&x)[1]
248 #define word1(x) ((ULong *)&x)[0]
250 #define word0(x) ((ULong *)&x)[0]
251 #define word1(x) ((ULong *)&x)[1]
254 /* The following definition of Storeinc is appropriate for MIPS processors.
255 * An alternative that might be better on some machines is
256 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
258 #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm32__)
259 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
260 ((unsigned short *)a)[0] = (unsigned short)c, a++)
262 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
263 ((unsigned short *)a)[1] = (unsigned short)c, a++)
266 /* #define P DBL_MANT_DIG */
267 /* Ten_pmax = floor(P*log(2)/log(5)) */
268 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
269 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
270 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
272 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
274 #define Exp_shift1 20
275 #define Exp_msk1 0x100000
276 #define Exp_msk11 0x100000
277 #define Exp_mask 0x7ff00000
282 #define Exp_1 0x3ff00000
283 #define Exp_11 0x3ff00000
285 #define Frac_mask 0xfffff
286 #define Frac_mask1 0xfffff
289 #define Bndry_mask 0xfffff
290 #define Bndry_mask1 0xfffff
292 #define Sign_bit 0x80000000
298 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
300 #undef Sudden_Underflow
301 #define Sudden_Underflow
304 #define Exp_shift1 24
305 #define Exp_msk1 0x1000000
306 #define Exp_msk11 0x1000000
307 #define Exp_mask 0x7f000000
310 #define Exp_1 0x41000000
311 #define Exp_11 0x41000000
312 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
313 #define Frac_mask 0xffffff
314 #define Frac_mask1 0xffffff
317 #define Bndry_mask 0xefffff
318 #define Bndry_mask1 0xffffff
320 #define Sign_bit 0x80000000
322 #define Tiny0 0x100000
329 #define Exp_msk1 0x80
330 #define Exp_msk11 0x800000
331 #define Exp_mask 0x7f80
334 #define Exp_1 0x40800000
335 #define Exp_11 0x4080
337 #define Frac_mask 0x7fffff
338 #define Frac_mask1 0xffff007f
341 #define Bndry_mask 0xffff007f
342 #define Bndry_mask1 0xffff007f
344 #define Sign_bit 0x8000
358 #define rounded_product(a,b) a = rnd_prod(a, b)
359 #define rounded_quotient(a,b) a = rnd_quot(a, b)
361 extern double rnd_prod(), rnd_quot();
363 extern double rnd_prod(double, double), rnd_quot(double, double);
366 #define rounded_product(a,b) a *= b
367 #define rounded_quotient(a,b) a /= b
370 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
371 #define Big1 0xffffffff
374 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
375 * This makes some inner loops simpler and sometimes saves work
376 * during multiplications, but it often seems to make things slightly
377 * slower. Hence the default is now to store 32 bits per Long.
387 extern "C" double strtod(const char *s00
, char **se
);
388 extern "C" char *__dtoa(double d
, int mode
, int ndigits
,
389 int *decpt
, int *sign
, char **rve
, char **resultp
);
395 int k
, maxwds
, sign
, wds
;
399 typedef struct Bigint Bigint
;
413 rv
= (Bigint
*)malloc(sizeof(Bigint
) + (x
-1)*sizeof(Long
));
416 rv
->sign
= rv
->wds
= 0;
431 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
432 y->wds*sizeof(Long) + 2*sizeof(int))
437 (b
, m
, a
) Bigint
*b
; int m
, a
;
439 (Bigint
*b
, int m
, int a
) /* multiply by m and add a */
455 y
= (xi
& 0xffff) * m
+ a
;
456 z
= (xi
>> 16) * m
+ (y
>> 16);
458 *x
++ = (z
<< 16) + (y
& 0xffff);
467 if (wds
>= b
->maxwds
) {
482 (s
, nd0
, nd
, y9
) CONST
char *s
; int nd0
, nd
; ULong y9
;
484 (CONST
char *s
, int nd0
, int nd
, ULong y9
)
492 for(k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) ;
499 b
->x
[0] = y9
& 0xffff;
500 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
506 do b
= multadd(b
, 10, *s
++ - '0');
513 b
= multadd(b
, 10, *s
++ - '0');
520 (x
) register ULong x
;
527 if (!(x
& 0xffff0000)) {
531 if (!(x
& 0xff000000)) {
535 if (!(x
& 0xf0000000)) {
539 if (!(x
& 0xc0000000)) {
543 if (!(x
& 0x80000000)) {
545 if (!(x
& 0x40000000))
560 register ULong x
= *y
;
618 (a
, b
) Bigint
*a
, *b
;
620 (Bigint
*a
, Bigint
*b
)
626 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
631 if (a
->wds
< b
->wds
) {
643 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
651 for(; xb
< xbe
; xb
++, xc0
++) {
652 if (y
= *xb
& 0xffff) {
657 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
659 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
672 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
675 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
683 for(; xb
< xbe
; xc0
++) {
689 z
= *x
++ * y
+ *xc
+ carry
;
698 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
708 (b
, k
) Bigint
*b
; int k
;
713 Bigint
*b1
, *p5
, *p51
;
715 static int p05
[3] = { 5, 25, 125 };
718 b
= multadd(b
, p05
[i
-1], 0);
735 if (!(p51
= p5
->next
)) {
736 p51
= p5
->next
= mult(p5
,p5
);
747 (b
, k
) Bigint
*b
; int k
;
754 ULong
*x
, *x1
, *xe
, z
;
763 for(i
= b
->maxwds
; n1
> i
; i
<<= 1)
767 for(i
= 0; i
< n
; i
++)
788 *x1
++ = *x
<< k
& 0xffff | z
;
807 (a
, b
) Bigint
*a
, *b
;
809 (Bigint
*a
, Bigint
*b
)
812 ULong
*xa
, *xa0
, *xb
, *xb0
;
818 if (i
> 1 && !a
->x
[i
-1])
819 Bug("cmp called with a->x[a->wds-1] == 0");
820 if (j
> 1 && !b
->x
[j
-1])
821 Bug("cmp called with b->x[b->wds-1] == 0");
831 return *xa
< *xb
? -1 : 1;
841 (a
, b
) Bigint
*a
, *b
;
843 (Bigint
*a
, Bigint
*b
)
848 Long borrow
, y
; /* We need signed shifts here. */
849 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
881 y
= (*xa
& 0xffff) - (*xb
& 0xffff) + borrow
;
883 Sign_Extend(borrow
, y
);
884 z
= (*xa
++ >> 16) - (*xb
++ >> 16) + borrow
;
886 Sign_Extend(borrow
, z
);
891 y
= (*xa
& 0xffff) + borrow
;
893 Sign_Extend(borrow
, y
);
894 z
= (*xa
++ >> 16) + borrow
;
896 Sign_Extend(borrow
, z
);
901 y
= *xa
++ - *xb
++ + borrow
;
903 Sign_Extend(borrow
, y
);
910 Sign_Extend(borrow
, y
);
931 L
= (word0(x
) & Exp_mask
) - (P
-1)*Exp_msk1
;
932 #ifndef Sudden_Underflow
940 #ifndef Sudden_Underflow
945 word0(a
) = 0x80000 >> L
;
951 word1(a
) = L
>= 31 ? 1 : 1 << 31 - L
;
961 (a
, e
) Bigint
*a
; int *e
;
966 ULong
*xa
, *xa0
, w
, y
, z
;
980 if (!y
) Bug("zero y in b2d");
986 d0
= Exp_1
| y
>> Ebits
- k
;
987 w
= xa
> xa0
? *--xa
: 0;
988 d1
= y
<< (32-Ebits
) + k
| w
>> Ebits
- k
;
991 z
= xa
> xa0
? *--xa
: 0;
993 d0
= Exp_1
| y
<< k
| z
>> 32 - k
;
994 y
= xa
> xa0
? *--xa
: 0;
995 d1
= z
<< k
| y
>> 32 - k
;
1002 if (k
< Ebits
+ 16) {
1003 z
= xa
> xa0
? *--xa
: 0;
1004 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
1005 w
= xa
> xa0
? *--xa
: 0;
1006 y
= xa
> xa0
? *--xa
: 0;
1007 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
1010 z
= xa
> xa0
? *--xa
: 0;
1011 w
= xa
> xa0
? *--xa
: 0;
1013 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
1014 y
= xa
> xa0
? *--xa
: 0;
1015 d1
= w
<< k
+ 16 | y
<< k
;
1019 word0(d
) = d0
>> 16 | d0
<< 16;
1020 word1(d
) = d1
>> 16 | d1
<< 16;
1031 (d
, e
, bits
) double d
; int *e
, *bits
;
1033 (double d
, int *e
, int *bits
)
1041 d0
= word0(d
) >> 16 | word0(d
) << 16;
1042 d1
= word1(d
) >> 16 | word1(d
) << 16;
1056 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1057 #ifdef Sudden_Underflow
1058 de
= (int)(d0
>> Exp_shift
);
1063 if (de
= (int)(d0
>> Exp_shift
))
1068 if (k
= lo0bits(&y
)) {
1069 x
[0] = y
| z
<< 32 - k
;
1074 i
= b
->wds
= (x
[1] = z
) ? 2 : 1;
1079 Bug("Zero passed to d2b");
1088 if (k
= lo0bits(&y
))
1090 x
[0] = y
| z
<< 32 - k
& 0xffff;
1091 x
[1] = z
>> k
- 16 & 0xffff;
1097 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1098 x
[2] = z
>> k
& 0xffff;
1113 Bug("Zero passed to d2b");
1131 #ifndef Sudden_Underflow
1135 *e
= (de
- Bias
- (P
-1) << 2) + k
;
1136 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1138 *e
= de
- Bias
- (P
-1) + k
;
1141 #ifndef Sudden_Underflow
1144 *e
= de
- Bias
- (P
-1) + 1 + k
;
1146 *bits
= 32*i
- hi0bits(x
[i
-1]);
1148 *bits
= (i
+2)*16 - hi0bits(x
[i
]);
1160 (a
, b
) Bigint
*a
, *b
;
1162 (Bigint
*a
, Bigint
*b
)
1171 k
= ka
- kb
+ 32*(a
->wds
- b
->wds
);
1173 k
= ka
- kb
+ 16*(a
->wds
- b
->wds
);
1177 word0(da
) += (k
>> 2)*Exp_msk1
;
1183 word0(db
) += (k
>> 2)*Exp_msk1
;
1189 word0(da
) += k
*Exp_msk1
;
1192 word0(db
) += k
*Exp_msk1
;
1200 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1201 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1209 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1210 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1214 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
};
1215 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
1218 static CONST
double bigtens
[] = { 1e16
, 1e32
};
1219 static CONST
double tinytens
[] = { 1e-16, 1e-32 };
1227 (s00
, se
) CONST
char *s00
; char **se
;
1229 (CONST
char *s00
, char **se
)
1232 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1233 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1234 CONST
char *s
, *s0
, *s1
;
1235 double aadj
, aadj1
, adj
, rv
, rv0
;
1238 Bigint
*bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
;
1241 CONST
char decimal_point
= localeconv()->decimal_point
[0];
1243 CONST
char decimal_point
= '.';
1246 sign
= nz0
= nz
= 0;
1250 for(s
= s00
; isspace((unsigned char) *s
); s
++)
1256 } else if (*s
== '+') {
1267 while(*++s
== '0') ;
1273 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1279 if (c
== decimal_point
) {
1282 for(; c
== '0'; c
= *++s
)
1284 if (c
> '0' && c
<= '9') {
1292 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
1297 for(i
= 1; i
< nz
; i
++)
1300 else if (nd
<= DBL_DIG
+ 1)
1304 else if (nd
<= DBL_DIG
+ 1)
1312 if (c
== 'e' || c
== 'E') {
1313 if (!nd
&& !nz
&& !nz0
) {
1325 if (c
>= '0' && c
<= '9') {
1328 if (c
> '0' && c
<= '9') {
1331 while((c
= *++s
) >= '0' && c
<= '9')
1333 if (s
- s1
> 8 || L
> 19999)
1334 /* Avoid confusion from exponents
1335 * so large that e might overflow.
1337 e
= 19999; /* safe for 16 bit ints */
1356 /* Now we have nd0 digits, starting at s0, followed by a
1357 * decimal point, followed by nd-nd0 digits. The number we're
1358 * after is the integer represented by those digits times
1363 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1366 rv
= tens
[k
- 9] * rv
+ z
;
1369 #ifndef RND_PRODQUOT
1376 if (e
<= Ten_pmax
) {
1378 goto vax_ovfl_check
;
1380 /* rv = */ rounded_product(rv
, tens
[e
]);
1385 if (e
<= Ten_pmax
+ i
) {
1386 /* A fancier test would sometimes let us do
1387 * this for larger i values.
1392 /* VAX exponent range is so narrow we must
1393 * worry about overflow here...
1396 word0(rv
) -= P
*Exp_msk1
;
1397 /* rv = */ rounded_product(rv
, tens
[e
]);
1398 if ((word0(rv
) & Exp_mask
)
1399 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
1401 word0(rv
) += P
*Exp_msk1
;
1403 /* rv = */ rounded_product(rv
, tens
[e
]);
1408 #ifndef Inaccurate_Divide
1409 else if (e
>= -Ten_pmax
) {
1410 /* rv = */ rounded_quotient(rv
, tens
[-e
]);
1417 /* Get starting approximation = rv * 10**e1 */
1423 if (e1
> DBL_MAX_10_EXP
) {
1429 /* Can't trust HUGE_VAL */
1431 word0(rv
) = Exp_mask
;
1443 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1446 /* The last multiplication could overflow. */
1447 word0(rv
) -= P
*Exp_msk1
;
1449 if ((z
= word0(rv
) & Exp_mask
)
1450 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
1452 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
1453 /* set to largest number */
1454 /* (Can't trust DBL_MAX) */
1459 word0(rv
) += P
*Exp_msk1
;
1470 if (e1
>= 1 << n_bigtens
)
1472 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1475 /* The last multiplication could underflow. */
1491 /* The refinement below will clean
1492 * this approximation up.
1498 /* Now the hard part -- adjusting rv to the correct value.*/
1500 /* Put digits into bd: true value = bd * 10^e */
1502 bd0
= s2b(s0
, nd0
, nd
, y
);
1505 bd
= Balloc(bd0
->k
);
1507 bb
= d2b(rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1523 #ifdef Sudden_Underflow
1525 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
1530 i
= bbe
+ bbbits
- 1; /* logb(rv) */
1531 if (i
< Emin
) /* denormal */
1538 i
= bb2
< bd2
? bb2
: bd2
;
1547 bs
= pow5mult(bs
, bb5
);
1553 bb
= lshift(bb
, bb2
);
1555 bd
= pow5mult(bd
, bd5
);
1557 bd
= lshift(bd
, bd2
);
1559 bs
= lshift(bs
, bs2
);
1560 delta
= diff(bb
, bd
);
1561 dsign
= delta
->sign
;
1565 /* Error is less than half an ulp -- check for
1566 * special case of mantissa a power of two.
1568 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
)
1570 delta
= lshift(delta
,Log2P
);
1571 if (cmp(delta
, bs
) > 0)
1576 /* exactly half-way between */
1578 if ((word0(rv
) & Bndry_mask1
) == Bndry_mask1
1579 && word1(rv
) == 0xffffffff) {
1580 /*boundary case -- increment exponent*/
1581 word0(rv
) = (word0(rv
) & Exp_mask
)
1591 else if (!(word0(rv
) & Bndry_mask
) && !word1(rv
)) {
1593 /* boundary case -- decrement exponent */
1594 #ifdef Sudden_Underflow
1595 L
= word0(rv
) & Exp_mask
;
1604 L
= (word0(rv
) & Exp_mask
) - Exp_msk1
;
1606 word0(rv
) = L
| Bndry_mask1
;
1607 word1(rv
) = 0xffffffff;
1614 #ifndef ROUND_BIASED
1615 if (!(word1(rv
) & LSB
))
1620 #ifndef ROUND_BIASED
1623 #ifndef Sudden_Underflow
1631 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1634 else if (word1(rv
) || word0(rv
) & Bndry_mask
) {
1635 #ifndef Sudden_Underflow
1636 if (word1(rv
) == Tiny1
&& !word0(rv
))
1643 /* special case -- power of FLT_RADIX to be */
1644 /* rounded down... */
1646 if (aadj
< 2./FLT_RADIX
)
1647 aadj
= 1./FLT_RADIX
;
1655 aadj1
= dsign
? aadj
: -aadj
;
1656 #ifdef Check_FLT_ROUNDS
1657 switch(FLT_ROUNDS
) {
1658 case 2: /* towards +infinity */
1661 case 0: /* towards 0 */
1662 case 3: /* towards -infinity */
1666 if (FLT_ROUNDS
== 0)
1670 y
= word0(rv
) & Exp_mask
;
1672 /* Check for overflow */
1674 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
1676 word0(rv
) -= P
*Exp_msk1
;
1677 adj
= aadj1
* ulp(rv
);
1679 if ((word0(rv
) & Exp_mask
) >=
1680 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
1681 if (word0(rv0
) == Big0
&& word1(rv0
) == Big1
)
1688 word0(rv
) += P
*Exp_msk1
;
1691 #ifdef Sudden_Underflow
1692 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
1694 word0(rv
) += P
*Exp_msk1
;
1695 adj
= aadj1
* ulp(rv
);
1698 if ((word0(rv
) & Exp_mask
) < P
*Exp_msk1
)
1700 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
)
1703 if (word0(rv0
) == Tiny0
1704 && word1(rv0
) == Tiny1
)
1711 word0(rv
) -= P
*Exp_msk1
;
1714 adj
= aadj1
* ulp(rv
);
1718 /* Compute adj so that the IEEE rounding rules will
1719 * correctly round rv + adj in some half-way cases.
1720 * If rv * ulp(rv) is denormalized (i.e.,
1721 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1722 * trouble from bits lost to denormalization;
1723 * example: 1.2e-307 .
1725 if (y
<= (P
-1)*Exp_msk1
&& aadj
>= 1.) {
1726 aadj1
= (double)(int)(aadj
+ 0.5);
1730 adj
= aadj1
* ulp(rv
);
1734 z
= word0(rv
) & Exp_mask
;
1736 /* Can we stop now? */
1739 /* The tolerances below are conservative. */
1740 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
) {
1741 if (aadj
< .4999999 || aadj
> .5000001)
1744 else if (aadj
< .4999999/FLT_RADIX
)
1762 return sign
? -rv
: rv
;
1768 (b
, S
) Bigint
*b
, *S
;
1770 (Bigint
*b
, Bigint
*S
)
1776 ULong
*bx
, *bxe
, *sx
, *sxe
;
1784 /*debug*/ if (b
->wds
> n
)
1785 /*debug*/ Bug("oversize b in quorem");
1793 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1795 /*debug*/ if (q
> 9)
1796 /*debug*/ Bug("oversized quotient in quorem");
1804 ys
= (si
& 0xffff) * q
+ carry
;
1805 zs
= (si
>> 16) * q
+ (ys
>> 16);
1807 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1809 Sign_Extend(borrow
, y
);
1810 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1812 Sign_Extend(borrow
, z
);
1815 ys
= *sx
++ * q
+ carry
;
1817 y
= *bx
- (ys
& 0xffff) + borrow
;
1819 Sign_Extend(borrow
, y
);
1826 while(--bxe
> bx
&& !*bxe
)
1831 if (cmp(b
, S
) >= 0) {
1840 ys
= (si
& 0xffff) + carry
;
1841 zs
= (si
>> 16) + (ys
>> 16);
1843 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1845 Sign_Extend(borrow
, y
);
1846 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1848 Sign_Extend(borrow
, z
);
1853 y
= *bx
- (ys
& 0xffff) + borrow
;
1855 Sign_Extend(borrow
, y
);
1863 while(--bxe
> bx
&& !*bxe
)
1871 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1873 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1874 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1877 * 1. Rather than iterating, we use a simple numeric overestimate
1878 * to determine k = floor(log10(d)). We scale relevant
1879 * quantities using O(log2(k)) rather than O(k) multiplications.
1880 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1881 * try to generate digits strictly left to right. Instead, we
1882 * compute with fewer bits and propagate the carry if necessary
1883 * when rounding the final digit up. This is often faster.
1884 * 3. Under the assumption that input will be rounded nearest,
1885 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1886 * That is, we allow equality in stopping tests when the
1887 * round-nearest rule will give the same floating-point value
1888 * as would satisfaction of the stopping test with strict
1890 * 4. We remove common factors of powers of 2 from relevant
1892 * 5. When converting floating-point integers less than 1e16,
1893 * we use floating-point arithmetic rather than resorting
1894 * to multiple-precision integers.
1895 * 6. When asked to produce fewer than 15 digits, we first try
1896 * to get by with floating-point arithmetic; we resort to
1897 * multiple-precision integer arithmetic only if we cannot
1898 * guarantee that the floating-point calculation has given
1899 * the correctly rounded result. For k requested digits and
1900 * "uniformly" distributed input, the probability is
1901 * something like 10^(k-15) that we must resort to the Long
1908 (d
, mode
, ndigits
, decpt
, sign
, rve
)
1909 double d
; int mode
, ndigits
, *decpt
, *sign
; char **rve
, char **resultp
;
1911 (double d
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
, char **resultp
)
1914 /* Arguments ndigits, decpt, sign are similar to those
1915 of ecvt and fcvt; trailing zeros are suppressed from
1916 the returned string. If not null, *rve is set to point
1917 to the end of the return value. If d is +-Infinity or NaN,
1918 then *decpt is set to 9999.
1921 0 ==> shortest string that yields d when read in
1922 and rounded to nearest.
1923 1 ==> like 0, but with Steele & White stopping rule;
1924 e.g. with IEEE P754 arithmetic , mode 0 gives
1925 1e23 whereas mode 1 gives 9.999999999999999e22.
1926 2 ==> max(1,ndigits) significant digits. This gives a
1927 return value similar to that of ecvt, except
1928 that trailing zeros are suppressed.
1929 3 ==> through ndigits past the decimal point. This
1930 gives a return value similar to that from fcvt,
1931 except that trailing zeros are suppressed, and
1932 ndigits can be negative.
1933 4-9 should give the same return values as 2-3, i.e.,
1934 4 <= mode <= 9 ==> same return as mode
1935 2 + (mode & 1). These modes are mainly for
1936 debugging; often they run slower but sometimes
1937 faster than modes 2-3.
1938 4,5,8,9 ==> left-to-right digit generation.
1939 6-9 ==> don't try fast floating-point estimate
1942 Values of mode other than 0-9 are treated as mode 0.
1944 Sufficient space is allocated to the return value
1945 to hold the suppressed trailing zeros.
1948 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
,
1949 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1950 spec_case
, try_quick
;
1952 #ifndef Sudden_Underflow
1956 Bigint
*b
, *b1
, *delta
, *mlo
, *mhi
, *S
;
1960 if (word0(d
) & Sign_bit
) {
1961 /* set sign for everything, including 0's and NaNs */
1963 word0(d
) &= ~Sign_bit
; /* clear sign bit */
1968 #if defined(IEEE_Arith) + defined(VAX)
1970 if ((word0(d
) & Exp_mask
) == Exp_mask
)
1972 if (word0(d
) == 0x8000)
1975 /* Infinity or NaN */
1979 !word1(d
) && !(word0(d
) & 0xfffff) ? "Infinity" :
1992 d
+= 0; /* normalize */
2002 b
= d2b(d
, &be
, &bbits
);
2003 #ifdef Sudden_Underflow
2004 i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
));
2006 if (i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
))) {
2009 word0(d2
) &= Frac_mask1
;
2010 word0(d2
) |= Exp_11
;
2012 if (j
= 11 - hi0bits(word0(d2
) & Frac_mask
))
2016 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2017 * log10(x) = log(x) / log(10)
2018 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2019 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2021 * This suggests computing an approximation k to log10(d) by
2023 * k = (i - Bias)*0.301029995663981
2024 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2026 * We want k to be too large rather than too small.
2027 * The error in the first-order Taylor series approximation
2028 * is in our favor, so we just round up the constant enough
2029 * to compensate for any error in the multiplication of
2030 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2031 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2032 * adding 1e-13 to the constant term more than suffices.
2033 * Hence we adjust the constant term to 0.1760912590558.
2034 * (We could get a more accurate k by invoking log10,
2035 * but this is probably not worthwhile.)
2043 #ifndef Sudden_Underflow
2047 /* d is denormalized */
2049 i
= bbits
+ be
+ (Bias
+ (P
-1) - 1);
2050 x
= i
> 32 ? word0(d
) << 64 - i
| word1(d
) >> i
- 32
2051 : word1(d
) << 32 - i
;
2053 word0(d2
) -= 31*Exp_msk1
; /* adjust exponent */
2054 i
-= (Bias
+ (P
-1) - 1) + 1;
2058 ds
= (d2
-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
2060 if (ds
< 0. && ds
!= k
)
2061 k
--; /* want k = floor(ds) */
2063 if (k
>= 0 && k
<= Ten_pmax
) {
2087 if (mode
< 0 || mode
> 9)
2108 ilim
= ilim1
= i
= ndigits
;
2114 i
= ndigits
+ k
+ 1;
2120 *resultp
= (char *) malloc(i
+ 1);
2123 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2125 /* Try to get by with floating-point arithmetic. */
2131 ieps
= 2; /* conservative */
2136 /* prevent overflows */
2138 d
/= bigtens
[n_bigtens
-1];
2141 for(; j
; j
>>= 1, i
++)
2149 d
*= tens
[j1
& 0xf];
2150 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
2156 if (k_check
&& d
< 1. && ilim
> 0) {
2165 word0(eps
) -= (P
-1)*Exp_msk1
;
2175 #ifndef No_leftright
2177 /* Use Steele & White method of only
2178 * generating digits needed.
2180 eps
= 0.5/tens
[ilim
-1] - eps
;
2184 *s
++ = '0' + (int)L
;
2197 /* Generate ilim digits, then fix them up. */
2198 eps
*= tens
[ilim
-1];
2199 for(i
= 1;; i
++, d
*= 10.) {
2202 *s
++ = '0' + (int)L
;
2206 else if (d
< 0.5 - eps
) {
2214 #ifndef No_leftright
2224 /* Do we have a "small" integer? */
2226 if (be
>= 0 && k
<= Int_max
) {
2229 if (ndigits
< 0 && ilim
<= 0) {
2231 if (ilim
< 0 || d
<= 5*ds
)
2238 #ifdef Check_FLT_ROUNDS
2239 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2245 *s
++ = '0' + (int)L
;
2248 if (d
> ds
|| d
== ds
&& L
& 1) {
2272 #ifndef Sudden_Underflow
2273 denorm
? be
+ (Bias
+ (P
-1) - 1 + 1) :
2276 1 + 4*P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
2290 if ((i
= ilim
) < 0) {
2299 if (m2
> 0 && s2
> 0) {
2300 i
= m2
< s2
? m2
: s2
;
2308 mhi
= pow5mult(mhi
, m5
);
2317 b
= pow5mult(b
, b5
);
2321 S
= pow5mult(S
, s5
);
2323 /* Check for special case that d is a normalized power of 2. */
2326 if (!word1(d
) && !(word0(d
) & Bndry_mask
)
2327 #ifndef Sudden_Underflow
2328 && word0(d
) & Exp_mask
2331 /* The special case */
2340 /* Arrange for convenient computation of quotients:
2341 * shift left if necessary so divisor has 4 leading 0 bits.
2343 * Perhaps we should just compute leading 28 bits of S once
2344 * and for all and pass them and a shift to quorem, so it
2345 * can do shifts and ors to compute the numerator for q.
2348 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f)
2351 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0xf)
2373 b
= multadd(b
, 10, 0); /* we botched the k estimate */
2375 mhi
= multadd(mhi
, 10, 0);
2379 if (ilim
<= 0 && mode
> 2) {
2380 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
2381 /* no digits, fcvt style */
2393 mhi
= lshift(mhi
, m2
);
2395 /* Compute mlo -- check for special case
2396 * that d is a normalized power of 2.
2401 mhi
= Balloc(mhi
->k
);
2403 mhi
= lshift(mhi
, Log2P
);
2407 dig
= quorem(b
,S
) + '0';
2408 /* Do we yet have the shortest decimal string
2409 * that will round to d?
2412 delta
= diff(S
, mhi
);
2413 j1
= delta
->sign
? 1 : cmp(b
, delta
);
2415 #ifndef ROUND_BIASED
2416 if (j1
== 0 && !mode
&& !(word1(d
) & 1)) {
2425 if (j
< 0 || j
== 0 && !mode
2426 #ifndef ROUND_BIASED
2433 if ((j1
> 0 || j1
== 0 && dig
& 1)
2441 if (dig
== '9') { /* possible if i == 1 */
2452 b
= multadd(b
, 10, 0);
2454 mlo
= mhi
= multadd(mhi
, 10, 0);
2456 mlo
= multadd(mlo
, 10, 0);
2457 mhi
= multadd(mhi
, 10, 0);
2463 *s
++ = dig
= quorem(b
,S
) + '0';
2466 b
= multadd(b
, 10, 0);
2469 /* Round off last digit */
2473 if (j
> 0 || j
== 0 && dig
& 1) {
2490 if (mlo
&& mlo
!= mhi
)
2496 if (s
== s0
) { /* don't return empty string */