]>
git.saurik.com Git - apple/javascriptcore.git/blob - kjs/dtoa.cpp
1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 ***************************************************************/
20 /* Please send bug reports to
22 Bell Laboratories, Room 2C-463
24 Murray Hill, NJ 07974-0636
29 /* On a machine with IEEE extended-precision registers, it is
30 * necessary to specify double-precision (53-bit) rounding precision
31 * before invoking strtod or dtoa. If the machine uses (the equivalent
32 * of) Intel 80x87 arithmetic, the call
33 * _control87(PC_53, MCW_PC);
34 * does this with many compilers. Whether this or another call is
35 * appropriate depends on the compiler; for this to work, it may be
36 * necessary to #include "float.h" or another system-dependent header
40 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
42 * This strtod returns a nearest machine number to the input decimal
43 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
44 * broken by the IEEE round-even rule. Otherwise ties are broken by
45 * biased rounding (add half and chop).
47 * Inspired loosely by William D. Clinger's paper "How to Read Floating
48 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
52 * 1. We only require IEEE, IBM, or VAX double-precision
53 * arithmetic (not IEEE double-extended).
54 * 2. We get by with floating-point arithmetic in a case that
55 * Clinger missed -- when we're computing d * 10^n
56 * for a small integer d and the integer n is not too
57 * much larger than 22 (the maximum integer k for which
58 * we can represent 10^k exactly), we may be able to
59 * compute (d*10^k) * 10^(e-k) with just one roundoff.
60 * 3. Rather than a bit-at-a-time adjustment of the binary
61 * result in the hard case, we use floating-point
62 * arithmetic to determine the adjustment to within
63 * one bit; only in really hard cases do we need to
64 * compute a second residual.
65 * 4. Because of 3., we don't need a large table of powers of 10
66 * for ten-to-e (just some small tables, e.g. of 10^k
71 * #define IEEE_8087 for IEEE-arithmetic machines where the least
72 * significant byte has the lowest address.
73 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
74 * significant byte has the lowest address.
75 * #define Long int on machines with 32-bit ints and 64-bit longs.
76 * #define IBM for IBM mainframe-style floating-point arithmetic.
77 * #define VAX for VAX-style floating-point arithmetic (D_floating).
78 * #define No_leftright to omit left-right logic in fast floating-point
79 * computation of dtoa.
80 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
81 * and strtod and dtoa should round accordingly.
82 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
83 * and Honor_FLT_ROUNDS is not #defined.
84 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
85 * that use extended-precision instructions to compute rounded
86 * products and quotients) with IBM.
87 * #define ROUND_BIASED for IEEE-format with biased rounding.
88 * #define Inaccurate_Divide for IEEE-format with correctly rounded
89 * products but inaccurate quotients, e.g., for Intel i860.
90 * #define NO_LONG_LONG on machines that do not have a "long long"
91 * integer type (of >= 64 bits). On such machines, you can
92 * #define Just_16 to store 16 bits per 32-bit Long when doing
93 * high-precision integer arithmetic. Whether this speeds things
94 * up or slows things down depends on the machine and the number
95 * being converted. If long long is available and the name is
96 * something other than "long long", #define Llong to be the name,
97 * and if "unsigned Llong" does not work as an unsigned version of
98 * Llong, #define #ULLong to be the corresponding unsigned type.
99 * #define KR_headers for old-style C function headers.
100 * #define Bad_float_h if your system lacks a float.h or if it does not
101 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
102 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
103 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
104 * if memory is available and otherwise does something you deem
105 * appropriate. If MALLOC is undefined, malloc will be invoked
106 * directly -- and assumed always to succeed.
107 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
108 * memory allocations from a private pool of memory when possible.
109 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
110 * unless #defined to be a different length. This default length
111 * suffices to get rid of MALLOC calls except for unusual cases,
112 * such as decimal-to-binary conversion of a very long string of
113 * digits. The longest string dtoa can return is about 751 bytes
114 * long. For conversions by strtod of strings of 800 digits and
115 * all dtoa conversions in single-threaded executions with 8-byte
116 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
117 * pointers, PRIVATE_MEM >= 7112 appears adequate.
118 * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
119 * Infinity and NaN (case insensitively). On some systems (e.g.,
120 * some HP systems), it may be necessary to #define NAN_WORD0
121 * appropriately -- to the most significant word of a quiet NaN.
122 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
123 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
124 * strtod also accepts (case insensitively) strings of the form
125 * NaN(x), where x is a string of hexadecimal digits and spaces;
126 * if there is only one string of hexadecimal digits, it is taken
127 * for the 52 fraction bits of the resulting NaN; if there are two
128 * or more strings of hex digits, the first is for the high 20 bits,
129 * the second and subsequent for the low 32 bits, with intervening
130 * white space ignored; but if this results in none of the 52
131 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
132 * and NAN_WORD1 are used instead.
133 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
134 * multiple threads. In this case, you must provide (or suitably
135 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
136 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
137 * in pow5mult, ensures lazy evaluation of only one copy of high
138 * powers of 5; omitting this lock would introduce a small
139 * probability of wasting memory, but would otherwise be harmless.)
140 * You must also invoke freedtoa(s) to free the value s returned by
141 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
142 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
143 * avoids underflows on inputs whose result does not underflow.
144 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
145 * floating-point numbers and flushes underflows to zero rather
146 * than implementing gradual underflow, then you must also #define
148 * #define YES_ALIAS to permit aliasing certain double values with
149 * arrays of ULongs. This leads to slightly better code with
150 * some compilers and was always used prior to 19990916, but it
151 * is not strictly legal and can cause trouble with aggressively
152 * optimizing compilers (e.g., gcc 2.95.1 under -O2).
153 * #define SET_INEXACT if IEEE arithmetic is being used and extra
154 * computation should be done to set the inexact flag when the
155 * result is inexact and avoid setting inexact when the result
156 * is exact. In this case, dtoa.c must be compiled in
157 * an environment, perhaps provided by #include "dtoa.c" in a
158 * suitable wrapper, that defines two functions,
159 * int get_inexact(void);
160 * void clear_inexact(void);
161 * such that get_inexact() returns a nonzero value if the
162 * inexact bit is already set, and clear_inexact() sets the
163 * inexact bit to 0. When SET_INEXACT is #defined, strtod
164 * also does extra computations to set the underflow and overflow
165 * flags when appropriate (i.e., when the result is tiny and
166 * inexact or when it is a numeric value rounded to +-infinity).
167 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
168 * the result overflows to +-Infinity or underflows to 0.
175 #pragma warning(disable: 4244)
176 #pragma warning(disable: 4245)
177 #pragma warning(disable: 4554)
180 #if PLATFORM(BIG_ENDIAN)
193 typedef unsigned Long ULong
;
198 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
206 extern char *MALLOC();
208 extern void *MALLOC(size_t);
211 #define MALLOC malloc
214 #ifndef Omit_Private_Memory
216 #define PRIVATE_MEM 2304
218 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
219 static double private_mem
[PRIVATE_mem
], *pmem_next
= private_mem
;
223 #undef Avoid_Underflow
237 #define DBL_MAX_10_EXP 308
238 #define DBL_MAX_EXP 1024
240 #endif /*IEEE_Arith*/
244 #define DBL_MAX_10_EXP 75
245 #define DBL_MAX_EXP 63
247 #define DBL_MAX 7.2370055773322621e+75
252 #define DBL_MAX_10_EXP 38
253 #define DBL_MAX_EXP 127
255 #define DBL_MAX 1.7014118346046923e+38
259 #define LONG_MAX 2147483647
262 #else /* ifndef Bad_float_h */
264 #endif /* Bad_float_h */
270 #define strtod kjs_strtod
271 #define dtoa kjs_dtoa
272 #define freedtoa kjs_freedtoa
280 #define CONST_ /* blank */
286 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
287 Exactly one of IEEE_8087
, IEEE_MC68k
, VAX
, or IBM should be defined
.
290 typedef union { double d
; ULong L
[2]; } U
;
295 #define word0(x) ((ULong *)&x)[1]
296 #define word1(x) ((ULong *)&x)[0]
298 #define word0(x) ((ULong *)&x)[0]
299 #define word1(x) ((ULong *)&x)[1]
303 #define word0(x) ((U*)&x)->L[1]
304 #define word1(x) ((U*)&x)->L[0]
306 #define word0(x) ((U*)&x)->L[0]
307 #define word1(x) ((U*)&x)->L[1]
309 #define dval(x) ((U*)&x)->d
312 /* The following definition of Storeinc is appropriate for MIPS processors.
313 * An alternative that might be better on some machines is
314 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
316 #if defined(IEEE_8087) + defined(VAX)
317 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
318 ((unsigned short *)a)[0] = (unsigned short)c, a++)
320 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
321 ((unsigned short *)a)[1] = (unsigned short)c, a++)
324 /* #define P DBL_MANT_DIG */
325 /* Ten_pmax = floor(P*log(2)/log(5)) */
326 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
327 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
328 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
332 #define Exp_shift1 20
333 #define Exp_msk1 0x100000
334 #define Exp_msk11 0x100000
335 #define Exp_mask 0x7ff00000
339 #define Exp_1 0x3ff00000
340 #define Exp_11 0x3ff00000
342 #define Frac_mask 0xfffff
343 #define Frac_mask1 0xfffff
346 #define Bndry_mask 0xfffff
347 #define Bndry_mask1 0xfffff
349 #define Sign_bit 0x80000000
355 #ifndef NO_IEEE_Scale
356 #define Avoid_Underflow
357 #ifdef Flush_Denorm /* debugging option */
358 #undef Sudden_Underflow
364 #define Flt_Rounds FLT_ROUNDS
368 #endif /*Flt_Rounds*/
370 #ifdef Honor_FLT_ROUNDS
371 #define Rounding rounding
372 #undef Check_FLT_ROUNDS
373 #define Check_FLT_ROUNDS
375 #define Rounding Flt_Rounds
378 #else /* ifndef IEEE_Arith */
379 #undef Check_FLT_ROUNDS
380 #undef Honor_FLT_ROUNDS
382 #undef Sudden_Underflow
383 #define Sudden_Underflow
388 #define Exp_shift1 24
389 #define Exp_msk1 0x1000000
390 #define Exp_msk11 0x1000000
391 #define Exp_mask 0x7f000000
394 #define Exp_1 0x41000000
395 #define Exp_11 0x41000000
396 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
397 #define Frac_mask 0xffffff
398 #define Frac_mask1 0xffffff
401 #define Bndry_mask 0xefffff
402 #define Bndry_mask1 0xffffff
404 #define Sign_bit 0x80000000
406 #define Tiny0 0x100000
415 #define Exp_msk1 0x80
416 #define Exp_msk11 0x800000
417 #define Exp_mask 0x7f80
420 #define Exp_1 0x40800000
421 #define Exp_11 0x4080
423 #define Frac_mask 0x7fffff
424 #define Frac_mask1 0xffff007f
427 #define Bndry_mask 0xffff007f
428 #define Bndry_mask1 0xffff007f
430 #define Sign_bit 0x8000
436 #endif /* IBM, VAX */
437 #endif /* IEEE_Arith */
444 #define rounded_product(a,b) a = rnd_prod(a, b)
445 #define rounded_quotient(a,b) a = rnd_quot(a, b)
447 extern double rnd_prod(), rnd_quot();
449 extern double rnd_prod(double, double), rnd_quot(double, double);
452 #define rounded_product(a,b) a *= b
453 #define rounded_quotient(a,b) a /= b
456 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
457 #define Big1 0xffffffff
464 #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
466 #define FFFFFFFF 0xffffffffUL
473 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
474 * This makes some inner loops simpler and sometimes saves work
475 * during multiplications, but it often seems to make things slightly
476 * slower. Hence the default is now to store 32 bits per Long.
479 #else /* long long available */
481 #define Llong long long
484 #define ULLong unsigned Llong
486 #endif /* NO_LONG_LONG */
488 #ifndef MULTIPLE_THREADS
489 #define ACQUIRE_DTOA_LOCK(n) /*nothing*/
490 #define FREE_DTOA_LOCK(n) /*nothing*/
498 int k
, maxwds
, sign
, wds
;
502 typedef struct Bigint Bigint
;
504 static Bigint
*freelist
[Kmax
+1];
516 #ifndef Omit_Private_Memory
520 ACQUIRE_DTOA_LOCK(0);
521 if ((rv
= freelist
[k
])) {
522 freelist
[k
] = rv
->next
;
526 #ifdef Omit_Private_Memory
527 rv
= (Bigint
*)MALLOC(sizeof(Bigint
) + (x
-1)*sizeof(ULong
));
529 len
= (sizeof(Bigint
) + (x
-1)*sizeof(ULong
) + sizeof(double) - 1)
531 if (pmem_next
- private_mem
+ len
<= (unsigned)PRIVATE_mem
) {
532 rv
= (Bigint
*)pmem_next
;
536 rv
= (Bigint
*)MALLOC(len
*sizeof(double));
542 rv
->sign
= rv
->wds
= 0;
555 ACQUIRE_DTOA_LOCK(0);
556 v
->next
= freelist
[v
->k
];
562 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
563 y->wds*sizeof(Long) + 2*sizeof(int))
568 (b
, m
, a
) Bigint
*b
; int m
, a
;
570 (Bigint
*b
, int m
, int a
) /* multiply by m and add a */
591 y
= *x
* (ULLong
)m
+ carry
;
593 *x
++ = (ULong
)y
& FFFFFFFF
;
597 y
= (xi
& 0xffff) * m
+ carry
;
598 z
= (xi
>> 16) * m
+ (y
>> 16);
600 *x
++ = (z
<< 16) + (y
& 0xffff);
610 if (wds
>= b
->maxwds
) {
616 b
->x
[wds
++] = (ULong
)carry
;
625 (s
, nd0
, nd
, y9
) CONST_
char *s
; int nd0
, nd
; ULong y9
;
627 (CONST_
char *s
, int nd0
, int nd
, ULong y9
)
635 for(k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) ;
642 b
->x
[0] = y9
& 0xffff;
643 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
649 do b
= multadd(b
, 10, *s
++ - '0');
656 b
= multadd(b
, 10, *s
++ - '0');
663 (x
) register ULong x
;
670 if (!(x
& 0xffff0000)) {
674 if (!(x
& 0xff000000)) {
678 if (!(x
& 0xf0000000)) {
682 if (!(x
& 0xc0000000)) {
686 if (!(x
& 0x80000000)) {
688 if (!(x
& 0x40000000))
703 register ULong x
= *y
;
761 (a
, b
) Bigint
*a
, *b
;
763 (Bigint
*a
, Bigint
*b
)
768 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
779 if (a
->wds
< b
->wds
) {
791 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
799 for(; xb
< xbe
; xc0
++) {
805 z
= *x
++ * (ULLong
)y
+ *xc
+ carry
;
807 *xc
++ = (ULong
)z
& FFFFFFFF
;
815 for(; xb
< xbe
; xb
++, xc0
++) {
816 if (y
= *xb
& 0xffff) {
821 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
823 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
836 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
839 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
847 for(; xb
< xbe
; xc0
++) {
853 z
= *x
++ * y
+ *xc
+ carry
;
863 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
873 (b
, k
) Bigint
*b
; int k
;
878 Bigint
*b1
, *p5
, *p51
;
880 static int p05
[3] = { 5, 25, 125 };
883 b
= multadd(b
, p05
[i
-1], 0);
889 #ifdef MULTIPLE_THREADS
890 ACQUIRE_DTOA_LOCK(1);
909 if (!(p51
= p5
->next
)) {
910 #ifdef MULTIPLE_THREADS
911 ACQUIRE_DTOA_LOCK(1);
912 if (!(p51
= p5
->next
)) {
913 p51
= p5
->next
= mult(p5
,p5
);
918 p51
= p5
->next
= mult(p5
,p5
);
930 (b
, k
) Bigint
*b
; int k
;
937 ULong
*x
, *x1
, *xe
, z
;
946 for(i
= b
->maxwds
; n1
> i
; i
<<= 1)
950 for(i
= 0; i
< n
; i
++)
971 *x1
++ = *x
<< k
& 0xffff | z
;
990 (a
, b
) Bigint
*a
, *b
;
992 (Bigint
*a
, Bigint
*b
)
995 ULong
*xa
, *xa0
, *xb
, *xb0
;
1001 if (i
> 1 && !a
->x
[i
-1])
1002 Bug("cmp called with a->x[a->wds-1] == 0");
1003 if (j
> 1 && !b
->x
[j
-1])
1004 Bug("cmp called with b->x[b->wds-1] == 0");
1014 return *xa
< *xb
? -1 : 1;
1024 (a
, b
) Bigint
*a
, *b
;
1026 (Bigint
*a
, Bigint
*b
)
1031 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
1068 y
= (ULLong
)*xa
++ - *xb
++ - borrow
;
1069 borrow
= y
>> 32 & (ULong
)1;
1070 *xc
++ = (ULong
)y
& FFFFFFFF
;
1075 borrow
= y
>> 32 & (ULong
)1;
1076 *xc
++ = (ULong
)y
& FFFFFFFF
;
1081 y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
1082 borrow
= (y
& 0x10000) >> 16;
1083 z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
1084 borrow
= (z
& 0x10000) >> 16;
1089 y
= (*xa
& 0xffff) - borrow
;
1090 borrow
= (y
& 0x10000) >> 16;
1091 z
= (*xa
++ >> 16) - borrow
;
1092 borrow
= (z
& 0x10000) >> 16;
1097 y
= *xa
++ - *xb
++ - borrow
;
1098 borrow
= (y
& 0x10000) >> 16;
1104 borrow
= (y
& 0x10000) >> 16;
1126 L
= (word0(x
) & Exp_mask
) - (P
-1)*Exp_msk1
;
1127 #ifndef Avoid_Underflow
1128 #ifndef Sudden_Underflow
1137 #ifndef Avoid_Underflow
1138 #ifndef Sudden_Underflow
1141 L
= -L
>> Exp_shift
;
1142 if (L
< Exp_shift
) {
1143 word0(a
) = 0x80000 >> L
;
1149 word1(a
) = L
>= 31 ? 1 : 1 << 31 - L
;
1160 (a
, e
) Bigint
*a
; int *e
;
1165 ULong
*xa
, *xa0
, w
, y
, z
;
1179 if (!y
) Bug("zero y in b2d");
1185 d0
= Exp_1
| y
>> Ebits
- k
;
1186 w
= xa
> xa0
? *--xa
: 0;
1187 d1
= y
<< (32-Ebits
) + k
| w
>> Ebits
- k
;
1190 z
= xa
> xa0
? *--xa
: 0;
1192 d0
= Exp_1
| y
<< k
| z
>> 32 - k
;
1193 y
= xa
> xa0
? *--xa
: 0;
1194 d1
= z
<< k
| y
>> 32 - k
;
1201 if (k
< Ebits
+ 16) {
1202 z
= xa
> xa0
? *--xa
: 0;
1203 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
1204 w
= xa
> xa0
? *--xa
: 0;
1205 y
= xa
> xa0
? *--xa
: 0;
1206 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
1209 z
= xa
> xa0
? *--xa
: 0;
1210 w
= xa
> xa0
? *--xa
: 0;
1212 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
1213 y
= xa
> xa0
? *--xa
: 0;
1214 d1
= w
<< k
+ 16 | y
<< k
;
1218 word0(d
) = d0
>> 16 | d0
<< 16;
1219 word1(d
) = d1
>> 16 | d1
<< 16;
1230 (d
, e
, bits
) double d
; int *e
, *bits
;
1232 (double d
, int *e
, int *bits
)
1238 #ifndef Sudden_Underflow
1243 d0
= word0(d
) >> 16 | word0(d
) << 16;
1244 d1
= word1(d
) >> 16 | word1(d
) << 16;
1258 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1259 #ifdef Sudden_Underflow
1260 de
= (int)(d0
>> Exp_shift
);
1265 if ((de
= (int)(d0
>> Exp_shift
)))
1270 if ((k
= lo0bits(&y
))) {
1271 x
[0] = y
| z
<< 32 - k
;
1276 #ifndef Sudden_Underflow
1279 b
->wds
= (x
[1] = z
) ? 2 : 1;
1284 Bug("Zero passed to d2b");
1288 #ifndef Sudden_Underflow
1296 if (k
= lo0bits(&y
))
1298 x
[0] = y
| z
<< 32 - k
& 0xffff;
1299 x
[1] = z
>> k
- 16 & 0xffff;
1305 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1306 x
[2] = z
>> k
& 0xffff;
1321 Bug("Zero passed to d2b");
1339 #ifndef Sudden_Underflow
1343 *e
= (de
- Bias
- (P
-1) << 2) + k
;
1344 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1346 *e
= de
- Bias
- (P
-1) + k
;
1349 #ifndef Sudden_Underflow
1352 *e
= de
- Bias
- (P
-1) + 1 + k
;
1354 *bits
= 32*i
- hi0bits(x
[i
-1]);
1356 *bits
= (i
+2)*16 - hi0bits(x
[i
]);
1368 (a
, b
) Bigint
*a
, *b
;
1370 (Bigint
*a
, Bigint
*b
)
1376 dval(da
) = b2d(a
, &ka
);
1377 dval(db
) = b2d(b
, &kb
);
1379 k
= ka
- kb
+ 32*(a
->wds
- b
->wds
);
1381 k
= ka
- kb
+ 16*(a
->wds
- b
->wds
);
1385 word0(da
) += (k
>> 2)*Exp_msk1
;
1391 word0(db
) += (k
>> 2)*Exp_msk1
;
1397 word0(da
) += k
*Exp_msk1
;
1400 word0(db
) += k
*Exp_msk1
;
1403 return dval(da
) / dval(db
);
1406 static CONST_
double
1408 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1409 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1416 static CONST_
double
1418 bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1419 static CONST_
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1420 #ifdef Avoid_Underflow
1421 9007199254740992.*9007199254740992.e
-256
1422 /* = 2^106 * 1e-53 */
1427 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1428 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1429 #define Scale_Bit 0x10
1433 bigtens
[] = { 1e16
, 1e32
, 1e64
};
1434 static CONST_
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
1437 bigtens
[] = { 1e16
, 1e32
};
1438 static CONST_
double tinytens
[] = { 1e-16, 1e-32 };
1450 #define NAN_WORD0 0x7ff80000
1460 (sp
, t
) char **sp
, *t
;
1462 (CONST_
char **sp
, CONST_
char *t
)
1466 CONST_
char *s
= *sp
;
1469 if ((c
= *++s
) >= 'A' && c
<= 'Z')
1482 (rvp
, sp
) double *rvp
; CONST_
char **sp
;
1484 (double *rvp
, CONST_
char **sp
)
1489 int havedig
, udx0
, xshift
;
1492 havedig
= xshift
= 0;
1495 while((c
= *(CONST_
unsigned char*)++s
)) {
1496 if (c
>= '0' && c
<= '9')
1498 else if (c
>= 'a' && c
<= 'f')
1500 else if (c
>= 'A' && c
<= 'F')
1502 else if (c
<= ' ') {
1503 if (udx0
&& havedig
) {
1509 else if (/*(*/ c
== ')' && havedig
) {
1514 return; /* invalid form: don't change *sp */
1522 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1523 x
[1] = (x
[1] << 4) | c
;
1525 if ((x
[0] &= 0xfffff) || x
[1]) {
1526 word0(*rvp
) = Exp_mask
| x
[0];
1530 #endif /*No_Hex_NaN*/
1531 #endif /* INFNAN_CHECK */
1536 (s00
, se
) CONST_
char *s00
; char **se
;
1538 (CONST_
char *s00
, char **se
)
1541 #ifdef Avoid_Underflow
1544 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1545 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1546 CONST_
char *s
, *s0
, *s1
;
1547 double aadj
, aadj1
, adj
, rv
, rv0
;
1550 Bigint
*bb
= NULL
, *bb1
= NULL
, *bd
= NULL
, *bd0
= NULL
, *bs
= NULL
, *delta
= NULL
;
1552 int inexact
, oldinexact
;
1554 #ifdef Honor_FLT_ROUNDS
1558 sign
= nz0
= nz
= 0;
1560 for(s
= s00
;;s
++) switch(*s
) {
1583 while(*++s
== '0') ;
1589 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1598 for(; c
== '0'; c
= *++s
)
1600 if (c
> '0' && c
<= '9') {
1608 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
1613 for(i
= 1; i
< nz
; i
++)
1616 else if (nd
<= DBL_DIG
+ 1)
1620 else if (nd
<= DBL_DIG
+ 1)
1628 if (c
== 'e' || c
== 'E') {
1629 if (!nd
&& !nz
&& !nz0
) {
1640 if (c
>= '0' && c
<= '9') {
1643 if (c
> '0' && c
<= '9') {
1646 while((c
= *++s
) >= '0' && c
<= '9')
1648 if (s
- s1
> 8 || L
> 19999)
1649 /* Avoid confusion from exponents
1650 * so large that e might overflow.
1652 e
= 19999; /* safe for 16 bit ints */
1667 /* Check for Nan and Infinity */
1671 if (match(&s
,"nf")) {
1673 if (!match(&s
,"inity"))
1675 word0(rv
) = 0x7ff00000;
1682 if (match(&s
, "an")) {
1683 word0(rv
) = NAN_WORD0
;
1684 word1(rv
) = NAN_WORD1
;
1686 if (*s
== '(') /*)*/
1692 #endif /* INFNAN_CHECK */
1701 /* Now we have nd0 digits, starting at s0, followed by a
1702 * decimal point, followed by nd-nd0 digits. The number we're
1703 * after is the integer represented by those digits times
1708 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1713 oldinexact
= get_inexact();
1715 dval(rv
) = tens
[k
- 9] * dval(rv
) + z
;
1719 #ifndef RND_PRODQUOT
1720 #ifndef Honor_FLT_ROUNDS
1728 if (e
<= Ten_pmax
) {
1730 goto vax_ovfl_check
;
1732 #ifdef Honor_FLT_ROUNDS
1733 /* round correctly FLT_ROUNDS = 2 or 3 */
1739 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1744 if (e
<= Ten_pmax
+ i
) {
1745 /* A fancier test would sometimes let us do
1746 * this for larger i values.
1748 #ifdef Honor_FLT_ROUNDS
1749 /* round correctly FLT_ROUNDS = 2 or 3 */
1756 dval(rv
) *= tens
[i
];
1758 /* VAX exponent range is so narrow we must
1759 * worry about overflow here...
1762 word0(rv
) -= P
*Exp_msk1
;
1763 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1764 if ((word0(rv
) & Exp_mask
)
1765 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
1767 word0(rv
) += P
*Exp_msk1
;
1769 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1774 #ifndef Inaccurate_Divide
1775 else if (e
>= -Ten_pmax
) {
1776 #ifdef Honor_FLT_ROUNDS
1777 /* round correctly FLT_ROUNDS = 2 or 3 */
1783 /* rv = */ rounded_quotient(dval(rv
), tens
[-e
]);
1794 oldinexact
= get_inexact();
1796 #ifdef Avoid_Underflow
1799 #ifdef Honor_FLT_ROUNDS
1800 if ((rounding
= Flt_Rounds
) >= 2) {
1802 rounding
= rounding
== 2 ? 0 : 2;
1808 #endif /*IEEE_Arith*/
1810 /* Get starting approximation = rv * 10**e1 */
1814 dval(rv
) *= tens
[i
];
1816 if (e1
> DBL_MAX_10_EXP
) {
1821 /* Can't trust HUGE_VAL */
1823 #ifdef Honor_FLT_ROUNDS
1825 case 0: /* toward 0 */
1826 case 3: /* toward -infinity */
1831 word0(rv
) = Exp_mask
;
1834 #else /*Honor_FLT_ROUNDS*/
1835 word0(rv
) = Exp_mask
;
1837 #endif /*Honor_FLT_ROUNDS*/
1839 /* set overflow bit */
1841 dval(rv0
) *= dval(rv0
);
1843 #else /*IEEE_Arith*/
1846 #endif /*IEEE_Arith*/
1852 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1854 dval(rv
) *= bigtens
[j
];
1855 /* The last multiplication could overflow. */
1856 word0(rv
) -= P
*Exp_msk1
;
1857 dval(rv
) *= bigtens
[j
];
1858 if ((z
= word0(rv
) & Exp_mask
)
1859 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
1861 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
1862 /* set to largest number */
1863 /* (Can't trust DBL_MAX) */
1868 word0(rv
) += P
*Exp_msk1
;
1874 dval(rv
) /= tens
[i
];
1876 if (e1
>= 1 << n_bigtens
)
1878 #ifdef Avoid_Underflow
1881 for(j
= 0; e1
> 0; j
++, e1
>>= 1)
1883 dval(rv
) *= tinytens
[j
];
1884 if (scale
&& (j
= 2*P
+ 1 - ((word0(rv
) & Exp_mask
)
1885 >> Exp_shift
)) > 0) {
1886 /* scaled rv is denormal; zap j low bits */
1890 word0(rv
) = (P
+2)*Exp_msk1
;
1892 word0(rv
) &= 0xffffffff << j
-32;
1895 word1(rv
) &= 0xffffffff << j
;
1898 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
1900 dval(rv
) *= tinytens
[j
];
1901 /* The last multiplication could underflow. */
1902 dval(rv0
) = dval(rv
);
1903 dval(rv
) *= tinytens
[j
];
1905 dval(rv
) = 2.*dval(rv0
);
1906 dval(rv
) *= tinytens
[j
];
1918 #ifndef Avoid_Underflow
1921 /* The refinement below will clean
1922 * this approximation up.
1929 /* Now the hard part -- adjusting rv to the correct value.*/
1931 /* Put digits into bd: true value = bd * 10^e */
1933 bd0
= s2b(s0
, nd0
, nd
, y
);
1936 bd
= Balloc(bd0
->k
);
1938 bb
= d2b(dval(rv
), &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1954 #ifdef Honor_FLT_ROUNDS
1958 #ifdef Avoid_Underflow
1960 i
= j
+ bbbits
- 1; /* logb(rv) */
1961 if (i
< Emin
) /* denormal */
1965 #else /*Avoid_Underflow*/
1966 #ifdef Sudden_Underflow
1968 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
1972 #else /*Sudden_Underflow*/
1974 i
= j
+ bbbits
- 1; /* logb(rv) */
1975 if (i
< Emin
) /* denormal */
1979 #endif /*Sudden_Underflow*/
1980 #endif /*Avoid_Underflow*/
1983 #ifdef Avoid_Underflow
1986 i
= bb2
< bd2
? bb2
: bd2
;
1995 bs
= pow5mult(bs
, bb5
);
2001 bb
= lshift(bb
, bb2
);
2003 bd
= pow5mult(bd
, bd5
);
2005 bd
= lshift(bd
, bd2
);
2007 bs
= lshift(bs
, bs2
);
2008 delta
= diff(bb
, bd
);
2009 dsign
= delta
->sign
;
2012 #ifdef Honor_FLT_ROUNDS
2013 if (rounding
!= 1) {
2015 /* Error is less than an ulp */
2016 if (!delta
->x
[0] && delta
->wds
<= 1) {
2032 && !(word0(rv
) & Frac_mask
)) {
2033 y
= word0(rv
) & Exp_mask
;
2034 #ifdef Avoid_Underflow
2035 if (!scale
|| y
> 2*P
*Exp_msk1
)
2040 delta
= lshift(delta
,Log2P
);
2041 if (cmp(delta
, bs
) <= 0)
2046 #ifdef Avoid_Underflow
2047 if (scale
&& (y
= word0(rv
) & Exp_mask
)
2049 word0(adj
) += (2*P
+1)*Exp_msk1
- y
;
2051 #ifdef Sudden_Underflow
2052 if ((word0(rv
) & Exp_mask
) <=
2054 word0(rv
) += P
*Exp_msk1
;
2055 dval(rv
) += adj
*ulp(dval(rv
));
2056 word0(rv
) -= P
*Exp_msk1
;
2059 #endif /*Sudden_Underflow*/
2060 #endif /*Avoid_Underflow*/
2061 dval(rv
) += adj
*ulp(dval(rv
));
2065 adj
= ratio(delta
, bs
);
2068 if (adj
<= 0x7ffffffe) {
2069 /* adj = rounding ? ceil(adj) : floor(adj); */
2072 if (!((rounding
>>1) ^ dsign
))
2077 #ifdef Avoid_Underflow
2078 if (scale
&& (y
= word0(rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
2079 word0(adj
) += (2*P
+1)*Exp_msk1
- y
;
2081 #ifdef Sudden_Underflow
2082 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
2083 word0(rv
) += P
*Exp_msk1
;
2084 adj
*= ulp(dval(rv
));
2089 word0(rv
) -= P
*Exp_msk1
;
2092 #endif /*Sudden_Underflow*/
2093 #endif /*Avoid_Underflow*/
2094 adj
*= ulp(dval(rv
));
2101 #endif /*Honor_FLT_ROUNDS*/
2104 /* Error is less than half an ulp -- check for
2105 * special case of mantissa a power of two.
2107 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
2109 #ifdef Avoid_Underflow
2110 || (word0(rv
) & Exp_mask
) <= (2*P
+1)*Exp_msk1
2112 || (word0(rv
) & Exp_mask
) <= Exp_msk1
2117 if (!delta
->x
[0] && delta
->wds
<= 1)
2122 if (!delta
->x
[0] && delta
->wds
<= 1) {
2129 delta
= lshift(delta
,Log2P
);
2130 if (cmp(delta
, bs
) > 0)
2135 /* exactly half-way between */
2137 if ((word0(rv
) & Bndry_mask1
) == Bndry_mask1
2139 #ifdef Avoid_Underflow
2140 (scale
&& (y
= word0(rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
2141 ? (0xffffffff & (0xffffffff << (2*P
+1-(y
>>Exp_shift
)))) :
2144 /*boundary case -- increment exponent*/
2145 word0(rv
) = (word0(rv
) & Exp_mask
)
2152 #ifdef Avoid_Underflow
2158 else if (!(word0(rv
) & Bndry_mask
) && !word1(rv
)) {
2160 /* boundary case -- decrement exponent */
2161 #ifdef Sudden_Underflow /*{{*/
2162 L
= word0(rv
) & Exp_mask
;
2166 #ifdef Avoid_Underflow
2167 if (L
<= (scale
? (2*P
+1)*Exp_msk1
: Exp_msk1
))
2170 #endif /*Avoid_Underflow*/
2174 #else /*Sudden_Underflow}{*/
2175 #ifdef Avoid_Underflow
2177 L
= word0(rv
) & Exp_mask
;
2178 if (L
<= (2*P
+1)*Exp_msk1
) {
2179 if (L
> (P
+2)*Exp_msk1
)
2180 /* round even ==> */
2183 /* rv = smallest denormal */
2187 #endif /*Avoid_Underflow*/
2188 L
= (word0(rv
) & Exp_mask
) - Exp_msk1
;
2189 #endif /*Sudden_Underflow}}*/
2190 word0(rv
) = L
| Bndry_mask1
;
2191 word1(rv
) = 0xffffffff;
2198 #ifndef ROUND_BIASED
2199 if (!(word1(rv
) & LSB
))
2203 dval(rv
) += ulp(dval(rv
));
2204 #ifndef ROUND_BIASED
2206 dval(rv
) -= ulp(dval(rv
));
2207 #ifndef Sudden_Underflow
2212 #ifdef Avoid_Underflow
2218 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
2221 else if (word1(rv
) || word0(rv
) & Bndry_mask
) {
2222 #ifndef Sudden_Underflow
2223 if (word1(rv
) == Tiny1
&& !word0(rv
))
2230 /* special case -- power of FLT_RADIX to be */
2231 /* rounded down... */
2233 if (aadj
< 2./FLT_RADIX
)
2234 aadj
= 1./FLT_RADIX
;
2242 aadj1
= dsign
? aadj
: -aadj
;
2243 #ifdef Check_FLT_ROUNDS
2245 case 2: /* towards +infinity */
2248 case 0: /* towards 0 */
2249 case 3: /* towards -infinity */
2253 if (Flt_Rounds
== 0)
2255 #endif /*Check_FLT_ROUNDS*/
2257 y
= word0(rv
) & Exp_mask
;
2259 /* Check for overflow */
2261 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
2262 dval(rv0
) = dval(rv
);
2263 word0(rv
) -= P
*Exp_msk1
;
2264 adj
= aadj1
* ulp(dval(rv
));
2266 if ((word0(rv
) & Exp_mask
) >=
2267 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
2268 if (word0(rv0
) == Big0
&& word1(rv0
) == Big1
)
2275 word0(rv
) += P
*Exp_msk1
;
2278 #ifdef Avoid_Underflow
2279 if (scale
&& y
<= 2*P
*Exp_msk1
) {
2280 if (aadj
<= 0x7fffffff) {
2281 if ((z
= (ULong
)aadj
) <= 0)
2284 aadj1
= dsign
? aadj
: -aadj
;
2286 word0(aadj1
) += (2*P
+1)*Exp_msk1
- y
;
2288 adj
= aadj1
* ulp(dval(rv
));
2291 #ifdef Sudden_Underflow
2292 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
2293 dval(rv0
) = dval(rv
);
2294 word0(rv
) += P
*Exp_msk1
;
2295 adj
= aadj1
* ulp(dval(rv
));
2298 if ((word0(rv
) & Exp_mask
) < P
*Exp_msk1
)
2300 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
)
2303 if (word0(rv0
) == Tiny0
2304 && word1(rv0
) == Tiny1
)
2311 word0(rv
) -= P
*Exp_msk1
;
2314 adj
= aadj1
* ulp(dval(rv
));
2317 #else /*Sudden_Underflow*/
2318 /* Compute adj so that the IEEE rounding rules will
2319 * correctly round rv + adj in some half-way cases.
2320 * If rv * ulp(rv) is denormalized (i.e.,
2321 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2322 * trouble from bits lost to denormalization;
2323 * example: 1.2e-307 .
2325 if (y
<= (P
-1)*Exp_msk1
&& aadj
> 1.) {
2326 aadj1
= (double)(int)(aadj
+ 0.5);
2330 adj
= aadj1
* ulp(dval(rv
));
2332 #endif /*Sudden_Underflow*/
2333 #endif /*Avoid_Underflow*/
2335 z
= word0(rv
) & Exp_mask
;
2337 #ifdef Avoid_Underflow
2341 /* Can we stop now? */
2344 /* The tolerances below are conservative. */
2345 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
) {
2346 if (aadj
< .4999999 || aadj
> .5000001)
2349 else if (aadj
< .4999999/FLT_RADIX
)
2362 word0(rv0
) = Exp_1
+ (70 << Exp_shift
);
2367 else if (!oldinexact
)
2370 #ifdef Avoid_Underflow
2372 word0(rv0
) = Exp_1
- 2*P
*Exp_msk1
;
2374 dval(rv
) *= dval(rv0
);
2376 /* try to avoid the bug of testing an 8087 register value */
2377 if (word0(rv
) == 0 && word1(rv
) == 0)
2381 #endif /* Avoid_Underflow */
2383 if (inexact
&& !(word0(rv
) & Exp_mask
)) {
2384 /* set underflow bit */
2386 dval(rv0
) *= dval(rv0
);
2398 return sign
? -dval(rv
) : dval(rv
);
2404 (b
, S
) Bigint
*b
, *S
;
2406 (Bigint
*b
, Bigint
*S
)
2410 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
2412 ULLong borrow
, carry
, y
, ys
;
2414 ULong borrow
, carry
, y
, ys
;
2422 /*debug*/ if (b
->wds
> n
)
2423 /*debug*/ Bug("oversize b in quorem");
2431 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
2433 /*debug*/ if (q
> 9)
2434 /*debug*/ Bug("oversized quotient in quorem");
2441 ys
= *sx
++ * (ULLong
)q
+ carry
;
2443 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2444 borrow
= y
>> 32 & (ULong
)1;
2445 *bx
++ = (ULong
)y
& FFFFFFFF
;
2449 ys
= (si
& 0xffff) * q
+ carry
;
2450 zs
= (si
>> 16) * q
+ (ys
>> 16);
2452 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2453 borrow
= (y
& 0x10000) >> 16;
2454 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2455 borrow
= (z
& 0x10000) >> 16;
2458 ys
= *sx
++ * q
+ carry
;
2460 y
= *bx
- (ys
& 0xffff) - borrow
;
2461 borrow
= (y
& 0x10000) >> 16;
2469 while(--bxe
> bx
&& !*bxe
)
2474 if (cmp(b
, S
) >= 0) {
2484 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2485 borrow
= y
>> 32 & (ULong
)1;
2486 *bx
++ = (ULong
)y
& FFFFFFFF
;
2490 ys
= (si
& 0xffff) + carry
;
2491 zs
= (si
>> 16) + (ys
>> 16);
2493 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2494 borrow
= (y
& 0x10000) >> 16;
2495 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2496 borrow
= (z
& 0x10000) >> 16;
2501 y
= *bx
- (ys
& 0xffff) - borrow
;
2502 borrow
= (y
& 0x10000) >> 16;
2511 while(--bxe
> bx
&& !*bxe
)
2519 #ifndef MULTIPLE_THREADS
2520 static char *dtoa_result
;
2534 sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= (unsigned)i
;
2537 r
= (int*)Balloc(k
);
2540 #ifndef MULTIPLE_THREADS
2548 nrv_alloc(s
, rve
, n
) char *s
, **rve
; int n
;
2550 nrv_alloc(CONST_
char *s
, char **rve
, int n
)
2555 t
= rv
= rv_alloc(n
);
2556 while((*t
= *s
++)) t
++;
2562 /* freedtoa(s) must be used to free values s returned by dtoa
2563 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2564 * but for consistency with earlier versions of dtoa, it is optional
2565 * when MULTIPLE_THREADS is not defined.
2570 freedtoa(s
) char *s
;
2575 Bigint
*b
= (Bigint
*)((int *)s
- 1);
2576 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
2578 #ifndef MULTIPLE_THREADS
2579 if (s
== dtoa_result
)
2584 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2586 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2587 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
2590 * 1. Rather than iterating, we use a simple numeric overestimate
2591 * to determine k = floor(log10(d)). We scale relevant
2592 * quantities using O(log2(k)) rather than O(k) multiplications.
2593 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2594 * try to generate digits strictly left to right. Instead, we
2595 * compute with fewer bits and propagate the carry if necessary
2596 * when rounding the final digit up. This is often faster.
2597 * 3. Under the assumption that input will be rounded nearest,
2598 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2599 * That is, we allow equality in stopping tests when the
2600 * round-nearest rule will give the same floating-point value
2601 * as would satisfaction of the stopping test with strict
2603 * 4. We remove common factors of powers of 2 from relevant
2605 * 5. When converting floating-point integers less than 1e16,
2606 * we use floating-point arithmetic rather than resorting
2607 * to multiple-precision integers.
2608 * 6. When asked to produce fewer than 15 digits, we first try
2609 * to get by with floating-point arithmetic; we resort to
2610 * multiple-precision integer arithmetic only if we cannot
2611 * guarantee that the floating-point calculation has given
2612 * the correctly rounded result. For k requested digits and
2613 * "uniformly" distributed input, the probability is
2614 * something like 10^(k-15) that we must resort to the Long
2621 (d
, mode
, ndigits
, decpt
, sign
, rve
)
2622 double d
; int mode
, ndigits
, *decpt
, *sign
; char **rve
;
2624 (double d
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
)
2627 /* Arguments ndigits, decpt, sign are similar to those
2628 of ecvt and fcvt; trailing zeros are suppressed from
2629 the returned string. If not null, *rve is set to point
2630 to the end of the return value. If d is +-Infinity or NaN,
2631 then *decpt is set to 9999.
2634 0 ==> shortest string that yields d when read in
2635 and rounded to nearest.
2636 1 ==> like 0, but with Steele & White stopping rule;
2637 e.g. with IEEE P754 arithmetic , mode 0 gives
2638 1e23 whereas mode 1 gives 9.999999999999999e22.
2639 2 ==> max(1,ndigits) significant digits. This gives a
2640 return value similar to that of ecvt, except
2641 that trailing zeros are suppressed.
2642 3 ==> through ndigits past the decimal point. This
2643 gives a return value similar to that from fcvt,
2644 except that trailing zeros are suppressed, and
2645 ndigits can be negative.
2646 4,5 ==> similar to 2 and 3, respectively, but (in
2647 round-nearest mode) with the tests of mode 0 to
2648 possibly return a shorter string that rounds to d.
2649 With IEEE arithmetic and compilation with
2650 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2651 as modes 2 and 3 when FLT_ROUNDS != 1.
2652 6-9 ==> Debugging modes similar to mode - 4: don't try
2653 fast floating-point estimate (if applicable).
2655 Values of mode other than 0-9 are treated as mode 0.
2657 Sufficient space is allocated to the return value
2658 to hold the suppressed trailing zeros.
2661 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0,
2662 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
2663 spec_case
, try_quick
;
2665 #ifndef Sudden_Underflow
2669 Bigint
*b
, *b1
, *delta
, *mlo
= NULL
, *mhi
, *S
;
2672 #ifdef Honor_FLT_ROUNDS
2676 int inexact
, oldinexact
;
2679 #ifndef MULTIPLE_THREADS
2681 freedtoa(dtoa_result
);
2686 if (word0(d
) & Sign_bit
) {
2687 /* set sign for everything, including 0's and NaNs */
2689 word0(d
) &= ~Sign_bit
; /* clear sign bit */
2694 #if defined(IEEE_Arith) + defined(VAX)
2696 if ((word0(d
) & Exp_mask
) == Exp_mask
)
2698 if (word0(d
) == 0x8000)
2701 /* Infinity or NaN */
2704 if (!word1(d
) && !(word0(d
) & 0xfffff))
2705 return nrv_alloc("Infinity", rve
, 8);
2707 return nrv_alloc("NaN", rve
, 3);
2711 dval(d
) += 0; /* normalize */
2715 return nrv_alloc("0", rve
, 1);
2719 try_quick
= oldinexact
= get_inexact();
2722 #ifdef Honor_FLT_ROUNDS
2723 if ((rounding
= Flt_Rounds
) >= 2) {
2725 rounding
= rounding
== 2 ? 0 : 2;
2732 b
= d2b(dval(d
), &be
, &bbits
);
2733 #ifdef Sudden_Underflow
2734 i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
));
2736 if ((i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
)))) {
2739 word0(d2
) &= Frac_mask1
;
2740 word0(d2
) |= Exp_11
;
2742 if (j
= 11 - hi0bits(word0(d2
) & Frac_mask
))
2746 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2747 * log10(x) = log(x) / log(10)
2748 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2749 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2751 * This suggests computing an approximation k to log10(d) by
2753 * k = (i - Bias)*0.301029995663981
2754 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2756 * We want k to be too large rather than too small.
2757 * The error in the first-order Taylor series approximation
2758 * is in our favor, so we just round up the constant enough
2759 * to compensate for any error in the multiplication of
2760 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2761 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2762 * adding 1e-13 to the constant term more than suffices.
2763 * Hence we adjust the constant term to 0.1760912590558.
2764 * (We could get a more accurate k by invoking log10,
2765 * but this is probably not worthwhile.)
2773 #ifndef Sudden_Underflow
2777 /* d is denormalized */
2779 i
= bbits
+ be
+ (Bias
+ (P
-1) - 1);
2780 x
= i
> 32 ? word0(d
) << 64 - i
| word1(d
) >> i
- 32
2781 : word1(d
) << 32 - i
;
2783 word0(d2
) -= 31*Exp_msk1
; /* adjust exponent */
2784 i
-= (Bias
+ (P
-1) - 1) + 1;
2788 ds
= (dval(d2
)-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
2790 if (ds
< 0. && ds
!= k
)
2791 k
--; /* want k = floor(ds) */
2793 if (k
>= 0 && k
<= Ten_pmax
) {
2794 if (dval(d
) < tens
[k
])
2817 if (mode
< 0 || mode
> 9)
2821 #ifdef Check_FLT_ROUNDS
2822 try_quick
= Rounding
== 1;
2826 #endif /*SET_INEXACT*/
2846 ilim
= ilim1
= i
= ndigits
;
2852 i
= ndigits
+ k
+ 1;
2858 s
= s0
= rv_alloc(i
);
2860 #ifdef Honor_FLT_ROUNDS
2861 if (mode
> 1 && rounding
!= 1)
2865 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2867 /* Try to get by with floating-point arithmetic. */
2873 ieps
= 2; /* conservative */
2878 /* prevent overflows */
2880 dval(d
) /= bigtens
[n_bigtens
-1];
2883 for(; j
; j
>>= 1, i
++)
2890 else if ((j1
= -k
)) {
2891 dval(d
) *= tens
[j1
& 0xf];
2892 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
2895 dval(d
) *= bigtens
[i
];
2898 if (k_check
&& dval(d
) < 1. && ilim
> 0) {
2906 dval(eps
) = ieps
*dval(d
) + 7.;
2907 word0(eps
) -= (P
-1)*Exp_msk1
;
2911 if (dval(d
) > dval(eps
))
2913 if (dval(d
) < -dval(eps
))
2917 #ifndef No_leftright
2919 /* Use Steele & White method of only
2920 * generating digits needed.
2922 dval(eps
) = 0.5/tens
[ilim
-1] - dval(eps
);
2924 L
= (long int)dval(d
);
2926 *s
++ = '0' + (int)L
;
2927 if (dval(d
) < dval(eps
))
2929 if (1. - dval(d
) < dval(eps
))
2939 /* Generate ilim digits, then fix them up. */
2940 dval(eps
) *= tens
[ilim
-1];
2941 for(i
= 1;; i
++, dval(d
) *= 10.) {
2942 L
= (Long
)(dval(d
));
2943 if (!(dval(d
) -= L
))
2945 *s
++ = '0' + (int)L
;
2947 if (dval(d
) > 0.5 + dval(eps
))
2949 else if (dval(d
) < 0.5 - dval(eps
)) {
2950 while (*--s
== '0') { }
2957 #ifndef No_leftright
2967 /* Do we have a "small" integer? */
2969 if (be
>= 0 && k
<= Int_max
) {
2972 if (ndigits
< 0 && ilim
<= 0) {
2974 if (ilim
< 0 || dval(d
) <= 5*ds
)
2978 for(i
= 1;; i
++, dval(d
) *= 10.) {
2979 L
= (Long
)(dval(d
) / ds
);
2981 #ifdef Check_FLT_ROUNDS
2982 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2988 *s
++ = '0' + (int)L
;
2996 #ifdef Honor_FLT_ROUNDS
3000 case 2: goto bump_up
;
3004 if (dval(d
) > ds
|| dval(d
) == ds
&& L
& 1) {
3025 #ifndef Sudden_Underflow
3026 denorm
? be
+ (Bias
+ (P
-1) - 1 + 1) :
3029 1 + 4*P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
3037 if (m2
> 0 && s2
> 0) {
3038 i
= m2
< s2
? m2
: s2
;
3046 mhi
= pow5mult(mhi
, m5
);
3055 b
= pow5mult(b
, b5
);
3059 S
= pow5mult(S
, s5
);
3061 /* Check for special case that d is a normalized power of 2. */
3064 if ((mode
< 2 || leftright
)
3065 #ifdef Honor_FLT_ROUNDS
3069 if (!word1(d
) && !(word0(d
) & Bndry_mask
)
3070 #ifndef Sudden_Underflow
3071 && word0(d
) & (Exp_mask
& ~Exp_msk1
)
3074 /* The special case */
3081 /* Arrange for convenient computation of quotients:
3082 * shift left if necessary so divisor has 4 leading 0 bits.
3084 * Perhaps we should just compute leading 28 bits of S once
3085 * and for all and pass them and a shift to quorem, so it
3086 * can do shifts and ors to compute the numerator for q.
3089 if ((i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f))
3092 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0xf)
3114 b
= multadd(b
, 10, 0); /* we botched the k estimate */
3116 mhi
= multadd(mhi
, 10, 0);
3120 if (ilim
<= 0 && (mode
== 3 || mode
== 5)) {
3121 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
3122 /* no digits, fcvt style */
3134 mhi
= lshift(mhi
, m2
);
3136 /* Compute mlo -- check for special case
3137 * that d is a normalized power of 2.
3142 mhi
= Balloc(mhi
->k
);
3144 mhi
= lshift(mhi
, Log2P
);
3148 dig
= quorem(b
,S
) + '0';
3149 /* Do we yet have the shortest decimal string
3150 * that will round to d?
3153 delta
= diff(S
, mhi
);
3154 j1
= delta
->sign
? 1 : cmp(b
, delta
);
3156 #ifndef ROUND_BIASED
3157 if (j1
== 0 && mode
!= 1 && !(word1(d
) & 1)
3158 #ifdef Honor_FLT_ROUNDS
3167 else if (!b
->x
[0] && b
->wds
<= 1)
3174 if (j
< 0 || j
== 0 && mode
!= 1
3175 #ifndef ROUND_BIASED
3179 if (!b
->x
[0] && b
->wds
<= 1) {
3185 #ifdef Honor_FLT_ROUNDS
3188 case 0: goto accept_dig
;
3189 case 2: goto keep_dig
;
3191 #endif /*Honor_FLT_ROUNDS*/
3195 if ((j1
> 0 || j1
== 0 && dig
& 1)
3204 #ifdef Honor_FLT_ROUNDS
3208 if (dig
== '9') { /* possible if i == 1 */
3216 #ifdef Honor_FLT_ROUNDS
3222 b
= multadd(b
, 10, 0);
3224 mlo
= mhi
= multadd(mhi
, 10, 0);
3226 mlo
= multadd(mlo
, 10, 0);
3227 mhi
= multadd(mhi
, 10, 0);
3233 *s
++ = dig
= quorem(b
,S
) + '0';
3234 if (!b
->x
[0] && b
->wds
<= 1) {
3242 b
= multadd(b
, 10, 0);
3245 /* Round off last digit */
3247 #ifdef Honor_FLT_ROUNDS
3249 case 0: goto trimzeros
;
3250 case 2: goto roundoff
;
3255 if (j
> 0 || j
== 0 && dig
& 1) {
3266 #ifdef Honor_FLT_ROUNDS
3269 while (*--s
== '0') { }
3275 if (mlo
&& mlo
!= mhi
)
3283 word0(d
) = Exp_1
+ (70 << Exp_shift
);
3288 else if (!oldinexact
)