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 David M. Gay (dmg at acm dot org,
21 * with " at " changed at "@" and " dot " changed to "."). */
23 /* On a machine with IEEE extended-precision registers, it is
24 * necessary to specify double-precision (53-bit) rounding precision
25 * before invoking strtod or dtoa. If the machine uses (the equivalent
26 * of) Intel 80x87 arithmetic, the call
27 * _control87(PC_53, MCW_PC);
28 * does this with many compilers. Whether this or another call is
29 * appropriate depends on the compiler; for this to work, it may be
30 * necessary to #include "float.h" or another system-dependent header
34 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
36 * This strtod returns a nearest machine number to the input decimal
37 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
38 * broken by the IEEE round-even rule. Otherwise ties are broken by
39 * biased rounding (add half and chop).
41 * Inspired loosely by William D. Clinger's paper "How to Read Floating
42 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
46 * 1. We only require IEEE, IBM, or VAX double-precision
47 * arithmetic (not IEEE double-extended).
48 * 2. We get by with floating-point arithmetic in a case that
49 * Clinger missed -- when we're computing d * 10^n
50 * for a small integer d and the integer n is not too
51 * much larger than 22 (the maximum integer k for which
52 * we can represent 10^k exactly), we may be able to
53 * compute (d*10^k) * 10^(e-k) with just one roundoff.
54 * 3. Rather than a bit-at-a-time adjustment of the binary
55 * result in the hard case, we use floating-point
56 * arithmetic to determine the adjustment to within
57 * one bit; only in really hard cases do we need to
58 * compute a second residual.
59 * 4. Because of 3., we don't need a large table of powers of 10
60 * for ten-to-e (just some small tables, e.g. of 10^k
65 * #define IEEE_8087 for IEEE-arithmetic machines where the least
66 * significant byte has the lowest address.
67 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
68 * significant byte has the lowest address.
69 * #define Long int on machines with 32-bit ints and 64-bit longs.
70 * #define IBM for IBM mainframe-style floating-point arithmetic.
71 * #define VAX for VAX-style floating-point arithmetic (D_floating).
72 * #define No_leftright to omit left-right logic in fast floating-point
73 * computation of dtoa. This will cause dtoa modes 4 and 5 to be
74 * treated the same as modes 2 and 3 for some inputs.
75 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
76 * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS
77 * is also #defined, fegetround() will be queried for the rounding mode.
78 * Note that both FLT_ROUNDS and fegetround() are specified by the C99
79 * standard (and are specified to be consistent, with fesetround()
80 * affecting the value of FLT_ROUNDS), but that some (Linux) systems
81 * do not work correctly in this regard, so using fegetround() is more
82 * portable than using FLT_ROUNDS directly.
83 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
84 * and Honor_FLT_ROUNDS is not #defined.
85 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
86 * that use extended-precision instructions to compute rounded
87 * products and quotients) with IBM.
88 * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
89 * that rounds toward +Infinity.
90 * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
91 * rounding when the underlying floating-point arithmetic uses
92 * unbiased rounding. This prevent using ordinary floating-point
93 * arithmetic when the result could be computed with one rounding error.
94 * #define Inaccurate_Divide for IEEE-format with correctly rounded
95 * products but inaccurate quotients, e.g., for Intel i860.
96 * #define NO_LONG_LONG on machines that do not have a "long long"
97 * integer type (of >= 64 bits). On such machines, you can
98 * #define Just_16 to store 16 bits per 32-bit Long when doing
99 * high-precision integer arithmetic. Whether this speeds things
100 * up or slows things down depends on the machine and the number
101 * being converted. If long long is available and the name is
102 * something other than "long long", #define Llong to be the name,
103 * and if "unsigned Llong" does not work as an unsigned version of
104 * Llong, #define #ULLong to be the corresponding unsigned type.
105 * #define KR_headers for old-style C function headers.
106 * #define Bad_float_h if your system lacks a float.h or if it does not
107 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
108 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
109 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
110 * if memory is available and otherwise does something you deem
111 * appropriate. If MALLOC is undefined, malloc will be invoked
112 * directly -- and assumed always to succeed. Similarly, if you
113 * want something other than the system's free() to be called to
114 * recycle memory acquired from MALLOC, #define FREE to be the
115 * name of the alternate routine. (FREE or free is only called in
116 * pathological cases, e.g., in a dtoa call after a dtoa return in
117 * mode 3 with thousands of digits requested.)
118 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
119 * memory allocations from a private pool of memory when possible.
120 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
121 * unless #defined to be a different length. This default length
122 * suffices to get rid of MALLOC calls except for unusual cases,
123 * such as decimal-to-binary conversion of a very long string of
124 * digits. The longest string dtoa can return is about 751 bytes
125 * long. For conversions by strtod of strings of 800 digits and
126 * all dtoa conversions in single-threaded executions with 8-byte
127 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
128 * pointers, PRIVATE_MEM >= 7112 appears adequate.
129 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
130 * #defined automatically on IEEE systems. On such systems,
131 * when INFNAN_CHECK is #defined, strtod checks
132 * for Infinity and NaN (case insensitively). On some systems
133 * (e.g., some HP systems), it may be necessary to #define NAN_WORD0
134 * appropriately -- to the most significant word of a quiet NaN.
135 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
136 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
137 * strtod also accepts (case insensitively) strings of the form
138 * NaN(x), where x is a string of hexadecimal digits and spaces;
139 * if there is only one string of hexadecimal digits, it is taken
140 * for the 52 fraction bits of the resulting NaN; if there are two
141 * or more strings of hex digits, the first is for the high 20 bits,
142 * the second and subsequent for the low 32 bits, with intervening
143 * white space ignored; but if this results in none of the 52
144 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
145 * and NAN_WORD1 are used instead.
146 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
147 * multiple threads. In this case, you must provide (or suitably
148 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
149 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
150 * in pow5mult, ensures lazy evaluation of only one copy of high
151 * powers of 5; omitting this lock would introduce a small
152 * probability of wasting memory, but would otherwise be harmless.)
153 * You must also invoke freedtoa(s) to free the value s returned by
154 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
155 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
156 * avoids underflows on inputs whose result does not underflow.
157 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
158 * floating-point numbers and flushes underflows to zero rather
159 * than implementing gradual underflow, then you must also #define
161 * #define USE_LOCALE to use the current locale's decimal_point value.
162 * #define SET_INEXACT if IEEE arithmetic is being used and extra
163 * computation should be done to set the inexact flag when the
164 * result is inexact and avoid setting inexact when the result
165 * is exact. In this case, dtoa.c must be compiled in
166 * an environment, perhaps provided by #include "dtoa.c" in a
167 * suitable wrapper, that defines two functions,
168 * int get_inexact(void);
169 * void clear_inexact(void);
170 * such that get_inexact() returns a nonzero value if the
171 * inexact bit is already set, and clear_inexact() sets the
172 * inexact bit to 0. When SET_INEXACT is #defined, strtod
173 * also does extra computations to set the underflow and overflow
174 * flags when appropriate (i.e., when the result is tiny and
175 * inexact or when it is a numeric value rounded to +-infinity).
176 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
177 * the result overflows to +-Infinity or underflows to 0.
178 * #define NO_HEX_FP to omit recognition of hexadecimal floating-point
180 * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
181 * to disable logic for "fast" testing of very long input strings
182 * to strtod. This testing proceeds by initially truncating the
183 * input string, then if necessary comparing the whole string with
184 * a decimal expansion to decide close cases. This logic is only
185 * used for input more than STRTOD_DIGLIM digits long (default 40).
192 typedef unsigned Long ULong
;
199 fprintf(stderr, "%s\n", x); \
211 #ifdef Honor_FLT_ROUNDS
212 # ifndef Trust_FLT_ROUNDS
219 extern char* MALLOC();
221 extern void* MALLOC(size_t);
224 # define MALLOC malloc
227 #ifndef Omit_Private_Memory
229 # define PRIVATE_MEM 2304
231 # define PRIVATE_mem ((PRIVATE_MEM + sizeof(double) - 1) / sizeof(double))
232 static double private_mem
[PRIVATE_mem
], *pmem_next
= private_mem
;
236 #undef Avoid_Underflow
245 # ifndef NO_INFNAN_CHECK
247 # define INFNAN_CHECK
251 # define NO_STRTOD_BIGCOMP
260 # define DBL_MAX_10_EXP 308
261 # define DBL_MAX_EXP 1024
263 # endif /*IEEE_Arith*/
267 # define DBL_MAX_10_EXP 75
268 # define DBL_MAX_EXP 63
269 # define FLT_RADIX 16
270 # define DBL_MAX 7.2370055773322621e+75
275 # define DBL_MAX_10_EXP 38
276 # define DBL_MAX_EXP 127
278 # define DBL_MAX 1.7014118346046923e+38
282 # define LONG_MAX 2147483647
285 #else /* ifndef Bad_float_h */
287 #endif /* Bad_float_h */
299 # define CONST /* blank */
305 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
306 Exactly one of IEEE_8087
, IEEE_MC68k
, VAX
, or IBM should be defined
.
315 # define word0(x) (x)->L[1]
316 # define word1(x) (x)->L[0]
318 # define word0(x) (x)->L[0]
319 # define word1(x) (x)->L[1]
321 #define dval(x) (x)->d
323 #ifndef STRTOD_DIGLIM
324 # define STRTOD_DIGLIM 40
328 extern int strtod_diglim
;
330 # define strtod_diglim STRTOD_DIGLIM
333 /* The following definition of Storeinc is appropriate for MIPS processors.
334 * An alternative that might be better on some machines is
335 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
337 #if defined(IEEE_8087) + defined(VAX)
338 # define Storeinc(a, b, c) \
339 (((unsigned short*)a)[1] = (unsigned short)b, \
340 ((unsigned short*)a)[0] = (unsigned short)c, a++)
342 # define Storeinc(a, b, c) \
343 (((unsigned short*)a)[0] = (unsigned short)b, \
344 ((unsigned short*)a)[1] = (unsigned short)c, a++)
347 /* #define P DBL_MANT_DIG */
348 /* Ten_pmax = floor(P*log(2)/log(5)) */
349 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
350 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
351 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
354 # define Exp_shift 20
355 # define Exp_shift1 20
356 # define Exp_msk1 0x100000
357 # define Exp_msk11 0x100000
358 # define Exp_mask 0x7ff00000
363 # define Emin (-1022)
364 # define Exp_1 0x3ff00000
365 # define Exp_11 0x3ff00000
367 # define Frac_mask 0xfffff
368 # define Frac_mask1 0xfffff
371 # define Bndry_mask 0xfffff
372 # define Bndry_mask1 0xfffff
374 # define Sign_bit 0x80000000
378 # define Quick_max 14
380 # ifndef NO_IEEE_Scale
381 # define Avoid_Underflow
382 # ifdef Flush_Denorm /* debugging option */
383 # undef Sudden_Underflow
389 # define Flt_Rounds FLT_ROUNDS
391 # define Flt_Rounds 1
393 # endif /*Flt_Rounds*/
395 # ifdef Honor_FLT_ROUNDS
396 # undef Check_FLT_ROUNDS
397 # define Check_FLT_ROUNDS
399 # define Rounding Flt_Rounds
402 #else /* ifndef IEEE_Arith */
403 # undef Check_FLT_ROUNDS
404 # undef Honor_FLT_ROUNDS
406 # undef Sudden_Underflow
407 # define Sudden_Underflow
410 # define Flt_Rounds 0
411 # define Exp_shift 24
412 # define Exp_shift1 24
413 # define Exp_msk1 0x1000000
414 # define Exp_msk11 0x1000000
415 # define Exp_mask 0x7f000000
421 # define Exp_1 0x41000000
422 # define Exp_11 0x41000000
423 # define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
424 # define Frac_mask 0xffffff
425 # define Frac_mask1 0xffffff
428 # define Bndry_mask 0xefffff
429 # define Bndry_mask1 0xffffff
431 # define Sign_bit 0x80000000
433 # define Tiny0 0x100000
435 # define Quick_max 14
439 # define Flt_Rounds 1
440 # define Exp_shift 23
441 # define Exp_shift1 7
442 # define Exp_msk1 0x80
443 # define Exp_msk11 0x800000
444 # define Exp_mask 0x7f80
450 # define Exp_1 0x40800000
451 # define Exp_11 0x4080
453 # define Frac_mask 0x7fffff
454 # define Frac_mask1 0xffff007f
457 # define Bndry_mask 0xffff007f
458 # define Bndry_mask1 0xffff007f
460 # define Sign_bit 0x8000
464 # define Quick_max 15
466 # endif /* IBM, VAX */
467 #endif /* IEEE_Arith */
470 # define ROUND_BIASED
472 # ifdef ROUND_BIASED_without_Round_Up
474 # define ROUND_BIASED
479 # define rounded_product(a, b) a = rnd_prod(a, b)
480 # define rounded_quotient(a, b) a = rnd_quot(a, b)
482 extern double rnd_prod(), rnd_quot();
484 extern double rnd_prod(double, double), rnd_quot(double, double);
487 # define rounded_product(a, b) a *= b
488 # define rounded_quotient(a, b) a /= b
491 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
492 #define Big1 0xffffffff
498 typedef struct BCinfo BCinfo
;
500 int dp0
, dp1
, dplen
, dsign
, e0
, inexact
, nd
, nd0
, rounding
, scale
, uflchk
;
504 # define FFFFFFFF ((((unsigned long)0xffff) << 16) | (unsigned long)0xffff)
506 # define FFFFFFFF 0xffffffffUL
513 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
514 * This makes some inner loops simpler and sometimes saves work
515 * during multiplications, but it often seems to make things slightly
516 * slower. Hence the default is now to store 32 bits per Long.
519 #else /* long long available */
521 # define Llong long long
524 # define ULLong unsigned Llong
526 #endif /* NO_LONG_LONG */
528 #ifndef MULTIPLE_THREADS
529 # define ACQUIRE_DTOA_LOCK(n) /*nothing*/
530 # define FREE_DTOA_LOCK(n) /*nothing*/
536 extern "C" double strtod(const char* s00
, char** se
);
537 extern "C" char* dtoa(double d
, int mode
, int ndigits
, int* decpt
, int* sign
,
543 int k
, maxwds
, sign
, wds
;
547 typedef struct Bigint Bigint
;
549 static Bigint
* freelist
[Kmax
+ 1];
551 static Bigint
* Balloc
560 #ifndef Omit_Private_Memory
564 ACQUIRE_DTOA_LOCK(0);
565 /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
566 /* but this case seems very unlikely. */
567 if (k
<= Kmax
&& (rv
= freelist
[k
])) {
568 freelist
[k
] = rv
->next
;
571 #ifdef Omit_Private_Memory
572 rv
= (Bigint
*)MALLOC(sizeof(Bigint
) + (x
- 1) * sizeof(ULong
));
574 len
= (sizeof(Bigint
) + (x
- 1) * sizeof(ULong
) + sizeof(double) - 1) /
576 if (k
<= Kmax
&& pmem_next
- private_mem
+ len
<= PRIVATE_mem
) {
577 rv
= (Bigint
*)pmem_next
;
580 rv
= (Bigint
*)MALLOC(len
* sizeof(double));
587 rv
->sign
= rv
->wds
= 0;
606 ACQUIRE_DTOA_LOCK(0);
607 v
->next
= freelist
[v
->k
];
614 #define Bcopy(x, y) \
615 memcpy((char*)&x->sign, (char*)&y->sign, \
616 y->wds * sizeof(Long) + 2 * sizeof(int))
618 static Bigint
* multadd
623 (Bigint
* b
, int m
, int a
) /* multiply by m and add a */
644 y
= *x
* (ULLong
)m
+ carry
;
650 y
= (xi
& 0xffff) * m
+ carry
;
651 z
= (xi
>> 16) * m
+ (y
>> 16);
653 *x
++ = (z
<< 16) + (y
& 0xffff);
662 if (wds
>= b
->maxwds
) {
663 b1
= Balloc(b
->k
+ 1);
676 (s
, nd0
, nd
, y9
, dplen
) CONST
char* s
;
680 (const char* s
, int nd0
, int nd
, ULong y9
, int dplen
)
688 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++);
695 b
->x
[0] = y9
& 0xffff;
696 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
703 b
= multadd(b
, 10, *s
++ - '0');
709 for (; i
< nd
; i
++) {
710 b
= multadd(b
, 10, *s
++ - '0');
724 if (!(x
& 0xffff0000)) {
728 if (!(x
& 0xff000000)) {
732 if (!(x
& 0xf0000000)) {
736 if (!(x
& 0xc0000000)) {
740 if (!(x
& 0x80000000)) {
742 if (!(x
& 0x40000000)) {
818 (Bigint
* a
, Bigint
* b
)
823 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
834 if (a
->wds
< b
->wds
) {
843 if (wc
> a
->maxwds
) {
847 for (x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++) {
856 for (; xb
< xbe
; xc0
++) {
862 z
= *x
++ * (ULLong
)y
+ *xc
+ carry
;
864 *xc
++ = z
& FFFFFFFF
;
871 for (; xb
< xbe
; xb
++, xc0
++) {
872 if (y
= *xb
& 0xffff) {
877 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
879 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
891 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
894 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
901 for (; xb
< xbe
; xc0
++) {
907 z
= *x
++ * y
+ *xc
+ carry
;
916 for (xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
);
923 static Bigint
* pow5mult
931 Bigint
*b1
, *p5
, *p51
;
933 static int p05
[3] = {5, 25, 125};
936 b
= multadd(b
, p05
[i
- 1], 0);
944 #ifdef MULTIPLE_THREADS
945 ACQUIRE_DTOA_LOCK(1);
965 if (!(p51
= p5
->next
)) {
966 #ifdef MULTIPLE_THREADS
967 ACQUIRE_DTOA_LOCK(1);
968 if (!(p51
= p5
->next
)) {
969 p51
= p5
->next
= mult(p5
, p5
);
974 p51
= p5
->next
= mult(p5
, p5
);
983 static Bigint
* lshift
993 ULong
*x
, *x1
, *xe
, z
;
1001 n1
= n
+ b
->wds
+ 1;
1002 for (i
= b
->maxwds
; n1
> i
; i
<<= 1) {
1007 for (i
= 0; i
< n
; i
++) {
1017 *x1
++ = *x
<< k
| z
;
1029 *x1
++ = *x
<< k
& 0xffff | z
;
1051 (Bigint
* a
, Bigint
* b
)
1054 ULong
*xa
, *xa0
, *xb
, *xb0
;
1060 if (i
> 1 && !a
->x
[i
- 1]) {
1061 Bug("cmp called with a->x[a->wds-1] == 0");
1063 if (j
> 1 && !b
->x
[j
- 1]) {
1064 Bug("cmp called with b->x[b->wds-1] == 0");
1075 if (*--xa
!= *--xb
) {
1076 return *xa
< *xb
? -1 : 1;
1090 (Bigint
* a
, Bigint
* b
)
1095 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
1132 y
= (ULLong
)*xa
++ - *xb
++ - borrow
;
1133 borrow
= y
>> 32 & (ULong
)1;
1134 *xc
++ = y
& FFFFFFFF
;
1138 borrow
= y
>> 32 & (ULong
)1;
1139 *xc
++ = y
& FFFFFFFF
;
1144 y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
1145 borrow
= (y
& 0x10000) >> 16;
1146 z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
1147 borrow
= (z
& 0x10000) >> 16;
1151 y
= (*xa
& 0xffff) - borrow
;
1152 borrow
= (y
& 0x10000) >> 16;
1153 z
= (*xa
++ >> 16) - borrow
;
1154 borrow
= (z
& 0x10000) >> 16;
1159 y
= *xa
++ - *xb
++ - borrow
;
1160 borrow
= (y
& 0x10000) >> 16;
1165 borrow
= (y
& 0x10000) >> 16;
1187 L
= (word0(x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
1188 #ifndef Avoid_Underflow
1189 # ifndef Sudden_Underflow
1198 #ifndef Avoid_Underflow
1199 # ifndef Sudden_Underflow
1201 L
= -L
>> Exp_shift
;
1202 if (L
< Exp_shift
) {
1203 word0(&u
) = 0x80000 >> L
;
1208 word1(&u
) = L
>= 31 ? 1 : 1 << 31 - L
;
1224 ULong
*xa
, *xa0
, w
, y
, z
;
1230 # define d0 word0(&d)
1231 # define d1 word1(&d)
1239 Bug("zero y in b2d");
1246 d0
= Exp_1
| y
>> (Ebits
- k
);
1247 w
= xa
> xa0
? *--xa
: 0;
1248 d1
= y
<< ((32 - Ebits
) + k
) | w
>> (Ebits
- k
);
1251 z
= xa
> xa0
? *--xa
: 0;
1253 d0
= Exp_1
| y
<< k
| z
>> (32 - k
);
1254 y
= xa
> xa0
? *--xa
: 0;
1255 d1
= z
<< k
| y
>> (32 - k
);
1261 if (k
< Ebits
+ 16) {
1262 z
= xa
> xa0
? *--xa
: 0;
1263 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
1264 w
= xa
> xa0
? *--xa
: 0;
1265 y
= xa
> xa0
? *--xa
: 0;
1266 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
1269 z
= xa
> xa0
? *--xa
: 0;
1270 w
= xa
> xa0
? *--xa
: 0;
1272 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
1273 y
= xa
> xa0
? *--xa
: 0;
1274 d1
= w
<< k
+ 16 | y
<< k
;
1278 word0(&d
) = d0
>> 16 | d0
<< 16;
1279 word1(&d
) = d1
>> 16 | d1
<< 16;
1292 (U
* d
, int* e
, int* bits
)
1298 #ifndef Sudden_Underflow
1303 d0
= word0(d
) >> 16 | word0(d
) << 16;
1304 d1
= word1(d
) >> 16 | word1(d
) << 16;
1306 # define d0 word0(d)
1307 # define d1 word1(d)
1318 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1319 #ifdef Sudden_Underflow
1320 de
= (int)(d0
>> Exp_shift
);
1325 if ((de
= (int)(d0
>> Exp_shift
))) {
1331 if ((k
= lo0bits(&y
))) {
1332 x
[0] = y
| z
<< (32 - k
);
1337 # ifndef Sudden_Underflow
1340 b
->wds
= (x
[1] = z
) ? 2 : 1;
1344 # ifndef Sudden_Underflow
1352 if (k
= lo0bits(&y
))
1354 x
[0] = y
| z
<< 32 - k
& 0xffff;
1355 x
[1] = z
>> k
- 16 & 0xffff;
1360 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1361 x
[2] = z
>> k
& 0xffff;
1375 Bug("Zero passed to d2b");
1394 #ifndef Sudden_Underflow
1398 *e
= (de
- Bias
- (P
- 1) << 2) + k
;
1399 *bits
= 4 * P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1401 *e
= de
- Bias
- (P
- 1) + k
;
1404 #ifndef Sudden_Underflow
1406 *e
= de
- Bias
- (P
- 1) + 1 + k
;
1408 *bits
= 32 * i
- hi0bits(x
[i
- 1]);
1410 *bits
= (i
+ 2) * 16 - hi0bits(x
[i
]);
1424 (Bigint
* a
, Bigint
* b
)
1430 dval(&da
) = b2d(a
, &ka
);
1431 dval(&db
) = b2d(b
, &kb
);
1433 k
= ka
- kb
+ 32 * (a
->wds
- b
->wds
);
1435 k
= ka
- kb
+ 16 * (a
->wds
- b
->wds
);
1439 word0(&da
) += (k
>> 2) * Exp_msk1
;
1441 dval(&da
) *= 1 << k
;
1445 word0(&db
) += (k
>> 2) * Exp_msk1
;
1447 dval(&db
) *= 1 << k
;
1452 word0(&da
) += k
* Exp_msk1
;
1455 word0(&db
) += k
* Exp_msk1
;
1458 return dval(&da
) / dval(&db
);
1461 static CONST
double tens
[] = {1e0
,
1493 bigtens
[] = {1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1494 static CONST
double tinytens
[] = {1e-16, 1e-32, 1e-64, 1e-128,
1495 # ifdef Avoid_Underflow
1496 9007199254740992. * 9007199254740992.e
-256
1497 /* = 2^106 * 1e-256 */
1502 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1503 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1504 # define Scale_Bit 0x10
1505 # define n_bigtens 5
1508 bigtens
[] = {1e16
, 1e32
, 1e64
};
1509 static CONST
double tinytens
[] = {1e-16, 1e-32, 1e-64};
1510 # define n_bigtens 3
1512 bigtens
[] = {1e16
, 1e32
};
1513 static CONST
double tinytens
[] = {1e-16, 1e-32};
1514 # define n_bigtens 2
1521 # define Need_Hexdig
1527 # define Need_Hexdig
1531 #ifdef Need_Hexdig /*{*/
1532 static unsigned char hexdig
[256];
1536 htinit(h
, s
, inc
) unsigned char* h
;
1540 htinit(unsigned char* h
, unsigned char* s
, int inc
)
1544 for (i
= 0; (j
= s
[i
]) != 0; i
++) {
1556 # define USC (unsigned char*)
1557 htinit(hexdig
, USC
"0123456789", 0x10);
1558 htinit(hexdig
, USC
"abcdef", 0x10 + 10);
1559 htinit(hexdig
, USC
"ABCDEF", 0x10 + 10);
1561 #endif /* } Need_Hexdig */
1566 # define NAN_WORD0 0x7ff80000
1570 # define NAN_WORD1 0
1578 (const char** sp
, const char* t
)
1582 CONST
char* s
= *sp
;
1584 while ((d
= *t
++)) {
1585 if ((c
= *++s
) >= 'A' && c
<= 'Z') {
1602 (U
* rvp
, const char** sp
)
1607 int c1
, havedig
, udx0
, xshift
;
1613 havedig
= xshift
= 0;
1616 /* allow optional initial 0x or 0X */
1617 while ((c
= *(CONST
unsigned char*)(s
+ 1)) && c
<= ' ') {
1620 if (s
[1] == '0' && (s
[2] == 'x' || s
[2] == 'X')) {
1623 while ((c
= *(CONST
unsigned char*)++s
)) {
1624 if ((c1
= hexdig
[c
])) {
1626 } else if (c
<= ' ') {
1627 if (udx0
&& havedig
) {
1633 # ifdef GDTOA_NON_PEDANTIC_NANCHECK
1634 else if (/*(*/ c
== ')' && havedig
) {
1638 return; /* invalid form: don't change *sp */
1643 if (/*(*/ c
== ')') {
1647 } while ((c
= *++s
));
1658 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1660 x
[1] = (x
[1] << 4) | c
;
1662 if ((x
[0] &= 0xfffff) || x
[1]) {
1663 word0(rvp
) = Exp_mask
| x
[0];
1667 # endif /*No_Hex_NaN*/
1668 #endif /* INFNAN_CHECK */
1680 #if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/
1683 increment(b
) Bigint
* b
;
1685 increment(Bigint
* b
)
1694 if (*x
< (ULong
)0xffffffffL
) {
1701 if (b
->wds
>= b
->maxwds
) {
1702 b1
= Balloc(b
->k
+ 1);
1714 #ifndef NO_HEX_FP /*{*/
1718 rshift(b
, k
) Bigint
* b
;
1721 rshift(Bigint
* b
, int k
)
1724 ULong
*x
, *x1
, *xe
, y
;
1736 *x1
++ = (y
| (*x
<< n
)) & 0xffffffff;
1739 if ((*x1
= y
) != 0) {
1747 if ((b
->wds
= x1
- b
->x
) == 0) {
1754 any_on(b
, k
) Bigint
* b
;
1757 any_on(Bigint
* b
, int k
)
1761 ULong
*x
, *x0
, x1
, x2
;
1768 } else if (n
< nwds
&& (k
&= kmask
)) {
1785 enum { /* rounding values: same as FLT_ROUNDS */
1794 gethex(sp
, rvp
, rounding
, sign
) CONST
char** sp
;
1798 gethex( CONST
char **sp
, U
*rvp
, int rounding
, int sign
)
1802 CONST
unsigned char *decpt
, *s0
, *s
, *s1
;
1804 ULong L
, lostbits
, *x
;
1805 int big
, denorm
, esign
, havedig
, k
, n
, nbits
, up
, zret
;
1810 # ifdef IEEE_Arith /*{{*/
1811 emax
= 0x7fe - Bias
- P
+ 1,
1816 emax
= 0x7ff - Bias
- P
+
1820 emax
= 0x7f - Bias
- P
1826 # ifdef NO_LOCALE_CACHE
1827 const unsigned char* decimalpoint
=
1828 (unsigned char*)localeconv()->decimal_point
;
1830 const unsigned char* decimalpoint
;
1831 static unsigned char* decimalpoint_cache
;
1832 if (!(s0
= decimalpoint_cache
)) {
1833 s0
= (unsigned char*)localeconv()->decimal_point
;
1834 if ((decimalpoint_cache
=
1835 (unsigned char*)MALLOC(strlen((CONST
char*)s0
) + 1))) {
1836 strcpy((char*)decimalpoint_cache
, (CONST
char*)s0
);
1837 s0
= decimalpoint_cache
;
1848 s0
= *(CONST
unsigned char**)sp
+ 2;
1849 while (s0
[havedig
] == '0') {
1862 for (i
= 0; decimalpoint
[i
]; ++i
) {
1863 if (s
[i
] != decimalpoint
[i
]) {
1886 while (hexdig
[*s
]) {
1890 if (*s
== *decimalpoint
&& !decpt
) {
1891 for (i
= 1; decimalpoint
[i
]; ++i
) {
1892 if (s
[i
] != decimalpoint
[i
]) {
1898 if (*s
== '.' && !decpt
) {
1901 while (hexdig
[*s
]) {
1906 e
= -(((Long
)(s
- decpt
)) << 2);
1921 if ((n
= hexdig
[*s
]) == 0 || n
> 0x19) {
1926 while ((n
= hexdig
[*++s
]) != 0 && n
<= 0x19) {
1927 if (e1
& 0xf8000000) {
1930 e1
= 10 * e1
+ n
- 0x10;
1939 *sp
= (char*)s0
- 1;
1969 # endif /* IEEE_Arith */
1991 for (k
= 0; n
> (1 << (kshift
- 2)) - 1; n
>>= 1) {
1999 for (i
= 0; decimalpoint
[i
+ 1]; ++i
);
2003 if (*--s1
== decimalpoint
[i
]) {
2017 L
|= (hexdig
[*s1
] & 0x0f) << n
;
2021 b
->wds
= n
= x
- b
->x
;
2022 n
= ULbits
* n
- hi0bits(L
);
2031 if (x
[k
>> kshift
] & 1 << (k
& kmask
)) {
2033 if (k
> 0 && any_on(b
, k
)) {
2040 } else if (n
< nbits
) {
2053 word0(rvp
) = Exp_mask
;
2062 # ifdef IEEE_Arith /*{*/
2065 if (n
== nbits
&& (n
< 2 || any_on(b
, n
- 1))) {
2079 # endif /* } IEEE_Arith */
2093 lostbits
= any_on(b
, k
);
2095 if (x
[k
>> kshift
] & 1 << (k
& kmask
)) {
2108 if (lostbits
& 2 && (lostbits
& 1) | (x
[0] & 1)) {
2124 if (nbits
== Nbits
- 1
2125 && x
[nbits
>> kshift
] & 1 << (nbits
& kmask
)) {
2126 denorm
= 0; /* not currently used */
2129 } else if (b
->wds
> k
||
2130 ((n
= nbits
& kmask
) != 0 && hi0bits(x
[k
- 1]) < 32 - n
)) {
2140 word0(rvp
) = b
->wds
> 1 ? b
->x
[1] & ~0x100000 : 0;
2142 word0(rvp
) = (b
->x
[1] & ~0x100000) | ((e
+ 0x3ff + 52) << 20);
2144 word1(rvp
) = b
->x
[0];
2148 k
= b
->x
[0] & ((1 << j
) - 1);
2164 if (k
& j
&& ((k
& (j
- 1)) | lostbits
)) {
2171 word0(rvp
) = b
->x
[1] | ((e
+ 65 + 13) << 24);
2172 word1(rvp
) = b
->x
[0];
2175 /* The next two lines ignore swap of low- and high-order 2 bytes. */
2176 /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
2177 /* word1(rvp) = b->x[0]; */
2179 ((b
->x
[1] & ~0x800000) >> 16) | ((e
+ 129 + 55) << 7) | (b
->x
[1] << 16);
2180 word1(rvp
) = (b
->x
[0] >> 16) | (b
->x
[0] << 16);
2184 #endif /*!NO_HEX_FP}*/
2188 dshift(b
, p2
) Bigint
* b
;
2191 dshift(Bigint
* b
, int p2
)
2194 int rv
= hi0bits(b
->x
[b
->wds
- 1]) - 4;
2206 (Bigint
* b
, Bigint
* S
)
2210 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
2212 ULLong borrow
, carry
, y
, ys
;
2214 ULong borrow
, carry
, y
, ys
;
2222 /*debug*/ if (b
->wds
> n
)
2224 Bug("oversize b in quorem");
2234 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
2236 # ifdef NO_STRTOD_BIGCOMP
2237 /*debug*/ if (q
> 9)
2239 /* An oversized q is possible when quorem is called from bigcomp and */
2240 /* the input is near, e.g., twice the smallest denormalized number. */
2241 /*debug*/ if (q
> 15)
2243 /*debug*/ Bug("oversized quotient in quorem");
2250 ys
= *sx
++ * (ULLong
)q
+ carry
;
2252 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2253 borrow
= y
>> 32 & (ULong
)1;
2254 *bx
++ = y
& FFFFFFFF
;
2258 ys
= (si
& 0xffff) * q
+ carry
;
2259 zs
= (si
>> 16) * q
+ (ys
>> 16);
2261 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2262 borrow
= (y
& 0x10000) >> 16;
2263 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2264 borrow
= (z
& 0x10000) >> 16;
2267 ys
= *sx
++ * q
+ carry
;
2269 y
= *bx
- (ys
& 0xffff) - borrow
;
2270 borrow
= (y
& 0x10000) >> 16;
2274 } while (sx
<= sxe
);
2277 while (--bxe
> bx
&& !*bxe
) {
2283 if (cmp(b
, S
) >= 0) {
2293 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2294 borrow
= y
>> 32 & (ULong
)1;
2295 *bx
++ = y
& FFFFFFFF
;
2299 ys
= (si
& 0xffff) + carry
;
2300 zs
= (si
>> 16) + (ys
>> 16);
2302 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2303 borrow
= (y
& 0x10000) >> 16;
2304 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2305 borrow
= (z
& 0x10000) >> 16;
2310 y
= *bx
- (ys
& 0xffff) - borrow
;
2311 borrow
= (y
& 0x10000) >> 16;
2315 } while (sx
<= sxe
);
2319 while (--bxe
> bx
&& !*bxe
) {
2328 #if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/
2343 (i
= 2 * P
+ 1 - ((word0(x
) & Exp_mask
) >> Exp_shift
)) <= 0) {
2344 return rv
; /* Is there an example where i <= 0 ? */
2346 word0(&u
) = Exp_1
+ (i
<< Exp_shift
);
2352 #ifndef NO_STRTOD_BIGCOMP
2359 (U
* rv
, const char* s0
, BCinfo
* bc
)
2363 int b2
, bbits
, d2
, dd
, dig
, dsign
, i
, j
, nd
, nd0
, p2
, p5
, speccase
;
2368 p5
= nd
+ bc
->e0
- 1;
2370 # ifndef Sudden_Underflow
2371 if (rv
->d
== 0.) { /* special case: value near underflow-to-zero */
2372 /* threshold was rounded to zero */
2376 # ifdef Avoid_Underflow
2377 word0(rv
) = (P
+ 2) << Exp_shift
;
2382 # ifdef Honor_FLT_ROUNDS
2383 if (bc
->rounding
== 1)
2393 b
= d2b(rv
, &p2
, &bbits
);
2394 # ifdef Avoid_Underflow
2397 /* floor(log2(rv)) == bbits - 1 + p2 */
2398 /* Check for denormal case. */
2400 if (i
> (j
= P
- Emin
- 1 + p2
)) {
2401 # ifdef Sudden_Underflow
2406 # ifdef Avoid_Underflow
2407 word0(rv
) = (1 + bc
->scale
) << Exp_shift
;
2409 word0(rv
) = Exp_msk1
;
2416 # ifdef Honor_FLT_ROUNDS
2417 if (bc
->rounding
!= 1) {
2430 # ifndef Sudden_Underflow
2435 /* Arrange for convenient computation of quotients:
2436 * shift left if necessary so divisor has 4 leading 0 bits.
2439 d
= pow5mult(d
, p5
);
2440 } else if (p5
< 0) {
2441 b
= pow5mult(b
, -p5
);
2451 if ((b2
+= i
) > 0) {
2454 if ((d2
+= i
) > 0) {
2458 /* Now b/d = exactly half-way between the two floating-point values */
2459 /* on either side of the input string. Compute first digit of b/d. */
2461 if (!(dig
= quorem(b
, d
))) {
2462 b
= multadd(b
, 10, 0); /* very unlikely */
2466 /* Compare b/d with s0 */
2468 for (i
= 0; i
< nd0
;) {
2469 if ((dd
= s0
[i
++] - '0' - dig
)) {
2472 if (!b
->x
[0] && b
->wds
== 1) {
2478 b
= multadd(b
, 10, 0);
2481 for (j
= bc
->dp1
; i
++ < nd
;) {
2482 if ((dd
= s0
[j
++] - '0' - dig
)) {
2485 if (!b
->x
[0] && b
->wds
== 1) {
2491 b
= multadd(b
, 10, 0);
2494 if (b
->x
[0] || b
->wds
> 1 || dig
> 0) {
2500 # ifdef Honor_FLT_ROUNDS
2501 if (bc
->rounding
!= 1) {
2503 if (bc
->rounding
== 0) {
2510 } else if (dd
> 0) {
2511 if (bc
->rounding
== 0) {
2520 dval(rv
) += 2. * sulp(rv
, bc
);
2533 } else if (dd
< 0) {
2534 if (!dsign
) /* does not happen for round-near */
2536 dval(rv
) -= sulp(rv
, bc
);
2537 } else if (dd
> 0) {
2540 dval(rv
) += sulp(rv
, bc
);
2543 /* Exact half-way case: apply round-even rule. */
2544 if ((j
= ((word0(rv
) & Exp_mask
) >> Exp_shift
) - bc
->scale
) <= 0) {
2547 if (word1(rv
) & (0x1 << i
)) {
2550 } else if (word0(rv
) & (0x1 << (i
- 32))) {
2553 } else if (word1(rv
) & 1) {
2562 # ifdef Honor_FLT_ROUNDS
2567 #endif /* NO_STRTOD_BIGCOMP */
2571 (s00
, se
) CONST
char* s00
;
2574 (const char* s00
, char** se
)
2577 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, e
, e1
;
2578 int esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, nz1
, sign
;
2579 CONST
char *s
, *s0
, *s1
;
2582 U aadj2
, adj
, rv
, rv0
;
2585 Bigint
*bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
;
2586 #ifdef Avoid_Underflow
2592 #ifndef NO_STRTOD_BIGCOMP
2593 int req_bigcomp
= 0;
2595 #ifdef Honor_FLT_ROUNDS /*{*/
2596 # ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
2597 bc
.rounding
= Flt_Rounds
;
2600 switch (fegetround()) {
2616 sign
= nz0
= nz1
= nz
= bc
.dplen
= bc
.uflchk
= 0;
2618 for (s
= s00
;; s
++) switch (*s
) {
2641 #ifndef NO_HEX_FP /*{*/
2645 # ifdef Honor_FLT_ROUNDS
2646 gethex(&s
, &rv
, bc
.rounding
, sign
);
2648 gethex(&s
, &rv
, 1, sign
);
2654 while (*++s
== '0');
2661 for (nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
2663 y
= 10 * y
+ c
- '0';
2664 } else if (nd
< 16) {
2665 z
= 10 * z
+ c
- '0';
2668 bc
.dp0
= bc
.dp1
= s
- s0
;
2669 for (s1
= s
; s1
> s0
&& *--s1
== '0';) {
2673 s1
= localeconv()->decimal_point
;
2694 bc
.dplen
= bc
.dp1
- bc
.dp0
;
2696 for (; c
== '0'; c
= *++s
) {
2699 if (c
> '0' && c
<= '9') {
2701 bc
.dp1
= bc
.dp0
+ bc
.dplen
;
2709 for (; c
>= '0' && c
<= '9'; c
= *++s
) {
2714 for (i
= 1; i
< nz
; i
++)
2717 } else if (nd
<= DBL_DIG
+ 1) {
2722 } else if (nd
<= DBL_DIG
+ 1) {
2731 if (c
== 'e' || c
== 'E') {
2732 if (!nd
&& !nz
&& !nz0
) {
2743 if (c
>= '0' && c
<= '9') {
2747 if (c
> '0' && c
<= '9') {
2750 while ((c
= *++s
) >= '0' && c
<= '9') {
2751 L
= 10 * L
+ c
- '0';
2753 if (s
- s1
> 8 || L
> 19999)
2754 /* Avoid confusion from exponents
2755 * so large that e might overflow.
2758 e
= 19999; /* safe for 16 bit ints */
2775 /* Check for Nan and Infinity */
2776 if (!bc
.dplen
) switch (c
) {
2779 if (match(&s
, "nf")) {
2781 if (!match(&s
, "inity")) {
2784 word0(&rv
) = 0x7ff00000;
2791 if (match(&s
, "an")) {
2792 word0(&rv
) = NAN_WORD0
;
2793 word1(&rv
) = NAN_WORD1
;
2795 if (*s
== '(') { /*)*/
2802 #endif /* INFNAN_CHECK */
2809 bc
.e0
= e1
= e
-= nf
;
2811 /* Now we have nd0 digits, starting at s0, followed by a
2812 * decimal point, followed by nd-nd0 digits. The number we're
2813 * after is the integer represented by those digits times
2819 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
2824 oldinexact
= get_inexact();
2827 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
2831 #ifndef RND_PRODQUOT
2832 # ifndef Honor_FLT_ROUNDS
2840 #ifndef ROUND_BIASED_without_Round_Up
2842 if (e
<= Ten_pmax
) {
2844 goto vax_ovfl_check
;
2846 # ifdef Honor_FLT_ROUNDS
2847 /* round correctly FLT_ROUNDS = 2 or 3 */
2853 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2858 if (e
<= Ten_pmax
+ i
) {
2859 /* A fancier test would sometimes let us do
2860 * this for larger i values.
2862 # ifdef Honor_FLT_ROUNDS
2863 /* round correctly FLT_ROUNDS = 2 or 3 */
2870 dval(&rv
) *= tens
[i
];
2872 /* VAX exponent range is so narrow we must
2873 * worry about overflow here...
2876 word0(&rv
) -= P
* Exp_msk1
;
2877 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2878 if ((word0(&rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
2881 word0(&rv
) += P
* Exp_msk1
;
2883 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2888 # ifndef Inaccurate_Divide
2889 else if (e
>= -Ten_pmax
) {
2890 # ifdef Honor_FLT_ROUNDS
2891 /* round correctly FLT_ROUNDS = 2 or 3 */
2897 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
2901 #endif /* ROUND_BIASED_without_Round_Up */
2909 oldinexact
= get_inexact();
2912 # ifdef Avoid_Underflow
2915 # ifdef Honor_FLT_ROUNDS
2916 if (bc
.rounding
>= 2) {
2918 bc
.rounding
= bc
.rounding
== 2 ? 0 : 2;
2919 } else if (bc
.rounding
!= 2) {
2924 #endif /*IEEE_Arith*/
2926 /* Get starting approximation = rv * 10**e1 */
2929 if ((i
= e1
& 15)) {
2930 dval(&rv
) *= tens
[i
];
2933 if (e1
> DBL_MAX_10_EXP
) {
2935 /* Can't trust HUGE_VAL */
2937 # ifdef Honor_FLT_ROUNDS
2938 switch (bc
.rounding
) {
2939 case 0: /* toward 0 */
2940 case 3: /* toward -infinity */
2945 word0(&rv
) = Exp_mask
;
2948 # else /*Honor_FLT_ROUNDS*/
2949 word0(&rv
) = Exp_mask
;
2951 # endif /*Honor_FLT_ROUNDS*/
2953 /* set overflow bit */
2955 dval(&rv0
) *= dval(&rv0
);
2957 #else /*IEEE_Arith*/
2960 #endif /*IEEE_Arith*/
2975 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
2977 dval(&rv
) *= bigtens
[j
];
2979 /* The last multiplication could overflow. */
2980 word0(&rv
) -= P
* Exp_msk1
;
2981 dval(&rv
) *= bigtens
[j
];
2982 if ((z
= word0(&rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
2985 if (z
> Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
2986 /* set to largest number */
2987 /* (Can't trust DBL_MAX) */
2991 word0(&rv
) += P
* Exp_msk1
;
2994 } else if (e1
< 0) {
2996 if ((i
= e1
& 15)) {
2997 dval(&rv
) /= tens
[i
];
3000 if (e1
>= 1 << n_bigtens
) {
3003 #ifdef Avoid_Underflow
3004 if (e1
& Scale_Bit
) {
3007 for (j
= 0; e1
> 0; j
++, e1
>>= 1)
3009 dval(&rv
) *= tinytens
[j
];
3012 (j
= 2 * P
+ 1 - ((word0(&rv
) & Exp_mask
) >> Exp_shift
)) > 0) {
3013 /* scaled rv is denormal; clear j low bits */
3020 word0(&rv
) = (P
+ 2) * Exp_msk1
;
3022 word0(&rv
) &= 0xffffffff << (j
- 32);
3025 word1(&rv
) &= 0xffffffff << j
;
3029 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
3031 dval(&rv
) *= tinytens
[j
];
3033 /* The last multiplication could underflow. */
3034 dval(&rv0
) = dval(&rv
);
3035 dval(&rv
) *= tinytens
[j
];
3037 dval(&rv
) = 2. * dval(&rv0
);
3038 dval(&rv
) *= tinytens
[j
];
3045 #ifndef Avoid_Underflow
3048 /* The refinement below will clean
3049 * this approximation up.
3056 /* Now the hard part -- adjusting rv to the correct value.*/
3058 /* Put digits into bd: true value = bd * 10^e */
3061 #ifndef NO_STRTOD_BIGCOMP
3062 bc
.nd0
= nd0
; /* Only needed if nd > strtod_diglim, but done here */
3063 /* to silence an erroneous warning about bc.nd0 */
3064 /* possibly not being initialized. */
3065 if (nd
> strtod_diglim
) {
3066 /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
3067 /* minimum number of decimal digits to distinguish double values */
3068 /* in IEEE arithmetic. */
3074 if (--j
< bc
.dp1
&& j
>= bc
.dp0
) {
3087 if (nd
< 9) { /* must recompute y */
3089 for (i
= 0; i
< nd0
; ++i
) {
3090 y
= 10 * y
+ s0
[i
] - '0';
3092 for (j
= bc
.dp1
; i
< nd
; ++i
) {
3093 y
= 10 * y
+ s0
[j
++] - '0';
3098 bd0
= s2b(s0
, nd0
, nd
, y
, bc
.dplen
);
3101 bd
= Balloc(bd0
->k
);
3103 bb
= d2b(&rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
3119 #ifdef Honor_FLT_ROUNDS
3120 if (bc
.rounding
!= 1) {
3124 #ifdef Avoid_Underflow
3128 i
= j
+ bbbits
- 1; /* logb(rv) */
3130 if (i
< Emin
) { /* denormal */
3135 } else if (i
< 52) {
3136 Lsb1
= Lsb
<< (i
- 32);
3141 #else /*Avoid_Underflow*/
3142 # ifdef Sudden_Underflow
3144 j
= 1 + 4 * P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
3148 # else /*Sudden_Underflow*/
3150 i
= j
+ bbbits
- 1; /* logb(rv) */
3151 if (i
< Emin
) { /* denormal */
3156 # endif /*Sudden_Underflow*/
3157 #endif /*Avoid_Underflow*/
3160 #ifdef Avoid_Underflow
3163 i
= bb2
< bd2
? bb2
: bd2
;
3173 bs
= pow5mult(bs
, bb5
);
3179 bb
= lshift(bb
, bb2
);
3182 bd
= pow5mult(bd
, bd5
);
3185 bd
= lshift(bd
, bd2
);
3188 bs
= lshift(bs
, bs2
);
3190 delta
= diff(bb
, bd
);
3191 bc
.dsign
= delta
->sign
;
3194 #ifndef NO_STRTOD_BIGCOMP /*{*/
3195 if (bc
.nd
> nd
&& i
<= 0) {
3197 /* Must use bigcomp(). */
3201 # ifdef Honor_FLT_ROUNDS
3202 if (bc
.rounding
!= 1) {
3209 i
= -1; /* Discarded digits make delta smaller. */
3212 #ifdef Honor_FLT_ROUNDS /*{*/
3213 if (bc
.rounding
!= 1) {
3215 /* Error is less than an ulp */
3216 if (!delta
->x
[0] && delta
->wds
<= 1) {
3228 } else if (!bc
.dsign
) {
3230 if (!word1(&rv
) && !(word0(&rv
) & Frac_mask
)) {
3231 y
= word0(&rv
) & Exp_mask
;
3232 # ifdef Avoid_Underflow
3233 if (!bc
.scale
|| y
> 2 * P
* Exp_msk1
)
3238 delta
= lshift(delta
, Log2P
);
3239 if (cmp(delta
, bs
) <= 0) {
3245 # ifdef Avoid_Underflow /*{*/
3246 if (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
) {
3247 word0(&adj
) += (2 * P
+ 1) * Exp_msk1
- y
;
3250 # ifdef Sudden_Underflow
3251 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
) {
3252 word0(&rv
) += P
* Exp_msk1
;
3253 dval(&rv
) += adj
.d
* ulp(dval(&rv
));
3254 word0(&rv
) -= P
* Exp_msk1
;
3256 # endif /*Sudden_Underflow*/
3257 # endif /*Avoid_Underflow}*/
3258 dval(&rv
) += adj
.d
* ulp(&rv
);
3262 adj
.d
= ratio(delta
, bs
);
3266 if (adj
.d
<= 0x7ffffffe) {
3267 /* adj = rounding ? ceil(adj) : floor(adj); */
3270 if (!((bc
.rounding
>> 1) ^ bc
.dsign
)) {
3276 # ifdef Avoid_Underflow /*{*/
3277 if (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
) {
3278 word0(&adj
) += (2 * P
+ 1) * Exp_msk1
- y
;
3281 # ifdef Sudden_Underflow
3282 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
) {
3283 word0(&rv
) += P
* Exp_msk1
;
3284 adj
.d
*= ulp(dval(&rv
));
3290 word0(&rv
) -= P
* Exp_msk1
;
3293 # endif /*Sudden_Underflow*/
3294 # endif /*Avoid_Underflow}*/
3297 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
) {
3306 #endif /*}Honor_FLT_ROUNDS*/
3309 /* Error is less than half an ulp -- check for
3310 * special case of mantissa a power of two.
3312 if (bc
.dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
3313 #ifdef IEEE_Arith /*{*/
3314 # ifdef Avoid_Underflow
3315 || (word0(&rv
) & Exp_mask
) <= (2 * P
+ 1) * Exp_msk1
3317 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
3322 if (!delta
->x
[0] && delta
->wds
<= 1) {
3328 if (!delta
->x
[0] && delta
->wds
<= 1) {
3335 delta
= lshift(delta
, Log2P
);
3336 if (cmp(delta
, bs
) > 0) {
3342 /* exactly half-way between */
3344 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
&&
3347 #ifdef Avoid_Underflow
3348 (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
)
3350 (0xffffffff << (2 * P
+ 1 - (y
>> Exp_shift
))))
3354 /*boundary case -- increment exponent*/
3355 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
) {
3358 word0(&rv
) = (word0(&rv
) & Exp_mask
) + Exp_msk1
3364 #ifdef Avoid_Underflow
3369 } else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
3371 /* boundary case -- decrement exponent */
3372 #ifdef Sudden_Underflow /*{{*/
3373 L
= word0(&rv
) & Exp_mask
;
3377 # ifdef Avoid_Underflow
3378 if (L
<= (bc
.scale
? (2 * P
+ 1) * Exp_msk1
: Exp_msk1
))
3381 # endif /*Avoid_Underflow*/
3391 #else /*Sudden_Underflow}{*/
3392 # ifdef Avoid_Underflow
3394 L
= word0(&rv
) & Exp_mask
;
3395 if (L
<= (2 * P
+ 1) * Exp_msk1
) {
3396 if (L
> (P
+ 2) * Exp_msk1
)
3397 /* round even ==> */
3402 /* rv = smallest denormal */
3410 # endif /*Avoid_Underflow*/
3411 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
3412 #endif /*Sudden_Underflow}}*/
3413 word0(&rv
) = L
| Bndry_mask1
;
3414 word1(&rv
) = 0xffffffff;
3418 # ifndef NO_STRTOD_BIGCOMP
3426 #ifndef ROUND_BIASED
3427 # ifdef Avoid_Underflow
3429 if (!(word0(&rv
) & Lsb1
)) {
3432 } else if (!(word1(&rv
) & Lsb
)) {
3436 if (!(word1(&rv
) & LSB
)) {
3442 #ifdef Avoid_Underflow
3443 dval(&rv
) += sulp(&rv
, &bc
);
3445 dval(&rv
) += ulp(&rv
);
3447 #ifndef ROUND_BIASED
3449 # ifdef Avoid_Underflow
3450 dval(&rv
) -= sulp(&rv
, &bc
);
3452 dval(&rv
) -= ulp(&rv
);
3454 # ifndef Sudden_Underflow
3464 # ifdef Avoid_Underflow
3465 bc
.dsign
= 1 - bc
.dsign
;
3470 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
3473 } else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
3474 #ifndef Sudden_Underflow
3475 if (word1(&rv
) == Tiny1
&& !word0(&rv
)) {
3486 /* special case -- power of FLT_RADIX to be */
3487 /* rounded down... */
3489 if (aadj
< 2. / FLT_RADIX
) {
3490 aadj
= 1. / FLT_RADIX
;
3498 aadj1
= bc
.dsign
? aadj
: -aadj
;
3499 #ifdef Check_FLT_ROUNDS
3500 switch (bc
.rounding
) {
3501 case 2: /* towards +infinity */
3504 case 0: /* towards 0 */
3505 case 3: /* towards -infinity */
3509 if (Flt_Rounds
== 0) {
3512 #endif /*Check_FLT_ROUNDS*/
3514 y
= word0(&rv
) & Exp_mask
;
3516 /* Check for overflow */
3518 if (y
== Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1)) {
3519 dval(&rv0
) = dval(&rv
);
3520 word0(&rv
) -= P
* Exp_msk1
;
3521 adj
.d
= aadj1
* ulp(&rv
);
3523 if ((word0(&rv
) & Exp_mask
) >= Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
3524 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
) {
3531 word0(&rv
) += P
* Exp_msk1
;
3534 #ifdef Avoid_Underflow
3535 if (bc
.scale
&& y
<= 2 * P
* Exp_msk1
) {
3536 if (aadj
<= 0x7fffffff) {
3537 if ((z
= aadj
) <= 0) {
3541 aadj1
= bc
.dsign
? aadj
: -aadj
;
3543 dval(&aadj2
) = aadj1
;
3544 word0(&aadj2
) += (2 * P
+ 1) * Exp_msk1
- y
;
3545 aadj1
= dval(&aadj2
);
3546 adj
.d
= aadj1
* ulp(&rv
);
3549 # ifdef NO_STRTOD_BIGCOMP
3560 adj
.d
= aadj1
* ulp(&rv
);
3564 # ifdef Sudden_Underflow
3565 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
) {
3566 dval(&rv0
) = dval(&rv
);
3567 word0(&rv
) += P
* Exp_msk1
;
3568 adj
.d
= aadj1
* ulp(&rv
);
3571 if ((word0(&rv
) & Exp_mask
) < P
* Exp_msk1
)
3573 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
)
3576 if (word0(&rv0
) == Tiny0
&& word1(&rv0
) == Tiny1
) {
3587 word0(&rv
) -= P
* Exp_msk1
;
3590 adj
.d
= aadj1
* ulp(&rv
);
3593 # else /*Sudden_Underflow*/
3594 /* Compute adj so that the IEEE rounding rules will
3595 * correctly round rv + adj in some half-way cases.
3596 * If rv * ulp(rv) is denormalized (i.e.,
3597 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
3598 * trouble from bits lost to denormalization;
3599 * example: 1.2e-307 .
3601 if (y
<= (P
- 1) * Exp_msk1
&& aadj
> 1.) {
3602 aadj1
= (double)(int)(aadj
+ 0.5);
3607 adj
.d
= aadj1
* ulp(&rv
);
3609 # endif /*Sudden_Underflow*/
3610 #endif /*Avoid_Underflow*/
3612 z
= word0(&rv
) & Exp_mask
;
3615 # ifdef Avoid_Underflow
3619 /* Can we stop now? */
3622 /* The tolerances below are conservative. */
3623 if (bc
.dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
3624 if (aadj
< .4999999 || aadj
> .5000001) {
3627 } else if (aadj
< .4999999 / FLT_RADIX
) {
3644 #ifndef NO_STRTOD_BIGCOMP
3648 bigcomp(&rv
, s0
, &bc
);
3649 y
= word0(&rv
) & Exp_mask
;
3650 if (y
== Exp_mask
) {
3653 if (y
== 0 && rv
.d
== 0.) {
3661 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
3665 } else if (!oldinexact
) {
3669 #ifdef Avoid_Underflow
3671 word0(&rv0
) = Exp_1
- 2 * P
* Exp_msk1
;
3673 dval(&rv
) *= dval(&rv0
);
3675 /* try to avoid the bug of testing an 8087 register value */
3677 if (!(word0(&rv
) & Exp_mask
))
3679 if (word0(&rv
) == 0 && word1(&rv
) == 0)
3684 #endif /* Avoid_Underflow */
3686 if (bc
.inexact
&& !(word0(&rv
) & Exp_mask
)) {
3687 /* set underflow bit */
3688 dval(&rv0
) = 1e-300;
3689 dval(&rv0
) *= dval(&rv0
);
3692 ret
: if (se
) { *se
= (char*)s
; }
3693 return sign
? -dval(&rv
) : dval(&rv
);
3696 #ifndef MULTIPLE_THREADS
3697 static char* dtoa_result
;
3711 for (k
= 0; sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= i
; j
<<= 1) {
3714 r
= (int*)Balloc(k
);
3717 #ifndef MULTIPLE_THREADS
3725 nrv_alloc(s
, rve
, n
)
3729 nrv_alloc(const char* s
, char** rve
, int n
)
3734 t
= rv
= rv_alloc(n
);
3735 while ((*t
= *s
++)) {
3744 /* freedtoa(s) must be used to free values s returned by dtoa
3745 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
3746 * but for consistency with earlier versions of dtoa, it is optional
3747 * when MULTIPLE_THREADS is not defined.
3752 freedtoa(s
) char* s
;
3757 Bigint
* b
= (Bigint
*)((int*)s
- 1);
3758 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
3760 #ifndef MULTIPLE_THREADS
3761 if (s
== dtoa_result
) {
3767 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
3769 * Inspired by "How to Print Floating-Point Numbers Accurately" by
3770 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
3773 * 1. Rather than iterating, we use a simple numeric overestimate
3774 * to determine k = floor(log10(d)). We scale relevant
3775 * quantities using O(log2(k)) rather than O(k) multiplications.
3776 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
3777 * try to generate digits strictly left to right. Instead, we
3778 * compute with fewer bits and propagate the carry if necessary
3779 * when rounding the final digit up. This is often faster.
3780 * 3. Under the assumption that input will be rounded nearest,
3781 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
3782 * That is, we allow equality in stopping tests when the
3783 * round-nearest rule will give the same floating-point value
3784 * as would satisfaction of the stopping test with strict
3786 * 4. We remove common factors of powers of 2 from relevant
3788 * 5. When converting floating-point integers less than 1e16,
3789 * we use floating-point arithmetic rather than resorting
3790 * to multiple-precision integers.
3791 * 6. When asked to produce fewer than 15 digits, we first try
3792 * to get by with floating-point arithmetic; we resort to
3793 * multiple-precision integer arithmetic only if we cannot
3794 * guarantee that the floating-point calculation has given
3795 * the correctly rounded result. For k requested digits and
3796 * "uniformly" distributed input, the probability is
3797 * something like 10^(k-15) that we must resort to the Long
3803 (dd
, mode
, ndigits
, decpt
, sign
, rve
)
3805 int mode
, ndigits
, *decpt
, *sign
;
3808 (double dd
, int mode
, int ndigits
, int* decpt
, int* sign
, char** rve
)
3811 /* Arguments ndigits, decpt, sign are similar to those
3812 of ecvt and fcvt; trailing zeros are suppressed from
3813 the returned string. If not null, *rve is set to point
3814 to the end of the return value. If d is +-Infinity or NaN,
3815 then *decpt is set to 9999.
3818 0 ==> shortest string that yields d when read in
3819 and rounded to nearest.
3820 1 ==> like 0, but with Steele & White stopping rule;
3821 e.g. with IEEE P754 arithmetic , mode 0 gives
3822 1e23 whereas mode 1 gives 9.999999999999999e22.
3823 2 ==> max(1,ndigits) significant digits. This gives a
3824 return value similar to that of ecvt, except
3825 that trailing zeros are suppressed.
3826 3 ==> through ndigits past the decimal point. This
3827 gives a return value similar to that from fcvt,
3828 except that trailing zeros are suppressed, and
3829 ndigits can be negative.
3830 4,5 ==> similar to 2 and 3, respectively, but (in
3831 round-nearest mode) with the tests of mode 0 to
3832 possibly return a shorter string that rounds to d.
3833 With IEEE arithmetic and compilation with
3834 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
3835 as modes 2 and 3 when FLT_ROUNDS != 1.
3836 6-9 ==> Debugging modes similar to mode - 4: don't try
3837 fast floating-point estimate (if applicable).
3839 Values of mode other than 0-9 are treated as mode 0.
3841 Sufficient space is allocated to the return value
3842 to hold the suppressed trailing zeros.
3845 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
, j
, j1
, k
, k0
,
3846 k_check
, leftright
, m2
, m5
, s2
, s5
, spec_case
, try_quick
;
3848 #ifndef Sudden_Underflow
3852 Bigint
*b
, *b1
, *delta
, *mlo
, *mhi
, *S
;
3856 #ifndef No_leftright
3862 int inexact
, oldinexact
;
3864 #ifdef Honor_FLT_ROUNDS /*{*/
3866 # ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
3867 Rounding
= Flt_Rounds
;
3870 switch (fegetround()) {
3883 #ifndef MULTIPLE_THREADS
3885 freedtoa(dtoa_result
);
3891 if (word0(&u
) & Sign_bit
) {
3892 /* set sign for everything, including 0's and NaNs */
3894 word0(&u
) &= ~Sign_bit
; /* clear sign bit */
3899 #if defined(IEEE_Arith) + defined(VAX)
3901 if ((word0(&u
) & Exp_mask
) == Exp_mask
)
3903 if (word0(&u
) == 0x8000)
3906 /* Infinity or NaN */
3909 if (!word1(&u
) && !(word0(&u
) & 0xfffff)) {
3910 return nrv_alloc("Infinity", rve
, 8);
3913 return nrv_alloc("NaN", rve
, 3);
3917 dval(&u
) += 0; /* normalize */
3921 return nrv_alloc("0", rve
, 1);
3925 try_quick
= oldinexact
= get_inexact();
3928 #ifdef Honor_FLT_ROUNDS
3929 if (Rounding
>= 2) {
3931 Rounding
= Rounding
== 2 ? 0 : 2;
3932 } else if (Rounding
!= 2) {
3938 b
= d2b(&u
, &be
, &bbits
);
3939 #ifdef Sudden_Underflow
3940 i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
));
3942 if ((i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
)))) {
3944 dval(&d2
) = dval(&u
);
3945 word0(&d2
) &= Frac_mask1
;
3946 word0(&d2
) |= Exp_11
;
3948 if (j
= 11 - hi0bits(word0(&d2
) & Frac_mask
)) {
3949 dval(&d2
) /= 1 << j
;
3953 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
3954 * log10(x) = log(x) / log(10)
3955 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
3956 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
3958 * This suggests computing an approximation k to log10(d) by
3960 * k = (i - Bias)*0.301029995663981
3961 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
3963 * We want k to be too large rather than too small.
3964 * The error in the first-order Taylor series approximation
3965 * is in our favor, so we just round up the constant enough
3966 * to compensate for any error in the multiplication of
3967 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
3968 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
3969 * adding 1e-13 to the constant term more than suffices.
3970 * Hence we adjust the constant term to 0.1760912590558.
3971 * (We could get a more accurate k by invoking log10,
3972 * but this is probably not worthwhile.)
3980 #ifndef Sudden_Underflow
3984 /* d is denormalized */
3986 i
= bbits
+ be
+ (Bias
+ (P
- 1) - 1);
3987 x
= i
> 32 ? word0(&u
) << (64 - i
) | word1(&u
) >> (i
- 32)
3988 : word1(&u
) << (32 - i
);
3990 word0(&d2
) -= 31 * Exp_msk1
; /* adjust exponent */
3991 i
-= (Bias
+ (P
- 1) - 1) + 1;
3995 ds
= (dval(&d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 +
3996 i
* 0.301029995663981;
3998 if (ds
< 0. && ds
!= k
) {
3999 k
--; /* want k = floor(ds) */
4002 if (k
>= 0 && k
<= Ten_pmax
) {
4003 if (dval(&u
) < tens
[k
]) {
4025 if (mode
< 0 || mode
> 9) {
4030 # ifdef Check_FLT_ROUNDS
4031 try_quick
= Rounding
== 1;
4035 #endif /*SET_INEXACT*/
4042 ilim
= ilim1
= -1; /* Values for cases 0 and 1; done here to */
4043 /* silence erroneous "gcc -Wall" warning. */
4057 ilim
= ilim1
= i
= ndigits
;
4063 i
= ndigits
+ k
+ 1;
4070 s
= s0
= rv_alloc(i
);
4072 #ifdef Honor_FLT_ROUNDS
4073 if (mode
> 1 && Rounding
!= 1) {
4078 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
4079 /* Try to get by with floating-point arithmetic. */
4082 dval(&d2
) = dval(&u
);
4085 ieps
= 2; /* conservative */
4090 /* prevent overflows */
4092 dval(&u
) /= bigtens
[n_bigtens
- 1];
4095 for (; j
; j
>>= 1, i
++)
4101 } else if ((j1
= -k
)) {
4102 dval(&u
) *= tens
[j1
& 0xf];
4103 for (j
= j1
>> 4; j
; j
>>= 1, i
++)
4106 dval(&u
) *= bigtens
[i
];
4109 if (k_check
&& dval(&u
) < 1. && ilim
> 0) {
4118 dval(&eps
) = ieps
* dval(&u
) + 7.;
4119 word0(&eps
) -= (P
- 1) * Exp_msk1
;
4123 if (dval(&u
) > dval(&eps
)) {
4126 if (dval(&u
) < -dval(&eps
)) {
4131 #ifndef No_leftright
4133 /* Use Steele & White method of only
4134 * generating digits needed.
4136 dval(&eps
) = 0.5 / tens
[ilim
- 1] - dval(&eps
);
4138 if (k0
< 0 && j1
>= 307) {
4139 eps1
.d
= 1.01e256
; /* 1.01 allows roundoff in the next few lines */
4140 word0(&eps1
) -= Exp_msk1
* (Bias
+ P
- 1);
4141 dval(&eps1
) *= tens
[j1
& 0xf];
4142 for (i
= 0, j
= (j1
- 256) >> 4; j
; j
>>= 1, i
++)
4144 dval(&eps1
) *= bigtens
[i
];
4146 if (eps
.d
< eps1
.d
) {
4154 *s
++ = '0' + (int)L
;
4155 if (1. - dval(&u
) < dval(&eps
)) {
4158 if (dval(&u
) < dval(&eps
)) {
4169 /* Generate ilim digits, then fix them up. */
4170 dval(&eps
) *= tens
[ilim
- 1];
4171 for (i
= 1;; i
++, dval(&u
) *= 10.) {
4172 L
= (Long
)(dval(&u
));
4173 if (!(dval(&u
) -= L
)) {
4176 *s
++ = '0' + (int)L
;
4178 if (dval(&u
) > 0.5 + dval(&eps
)) {
4180 } else if (dval(&u
) < 0.5 - dval(&eps
)) {
4181 while (*--s
== '0');
4188 #ifndef No_leftright
4193 dval(&u
) = dval(&d2
);
4198 /* Do we have a "small" integer? */
4200 if (be
>= 0 && k
<= Int_max
) {
4203 if (ndigits
< 0 && ilim
<= 0) {
4205 if (ilim
< 0 || dval(&u
) <= 5 * ds
) {
4210 for (i
= 1;; i
++, dval(&u
) *= 10.) {
4211 L
= (Long
)(dval(&u
) / ds
);
4213 #ifdef Check_FLT_ROUNDS
4214 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
4220 *s
++ = '0' + (int)L
;
4228 #ifdef Honor_FLT_ROUNDS
4229 if (mode
> 1) switch (Rounding
) {
4236 dval(&u
) += dval(&u
);
4240 if (dval(&u
) > ds
|| (dval(&u
) == ds
&& L
& 1))
4263 #ifndef Sudden_Underflow
4264 denorm
? be
+ (Bias
+ (P
- 1) - 1 + 1) :
4267 1 + 4 * P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
4275 if (m2
> 0 && s2
> 0) {
4276 i
= m2
< s2
? m2
: s2
;
4284 mhi
= pow5mult(mhi
, m5
);
4289 if ((j
= b5
- m5
)) {
4293 b
= pow5mult(b
, b5
);
4298 S
= pow5mult(S
, s5
);
4301 /* Check for special case that d is a normalized power of 2. */
4304 if ((mode
< 2 || leftright
)
4305 #ifdef Honor_FLT_ROUNDS
4309 if (!word1(&u
) && !(word0(&u
) & Bndry_mask
)
4310 #ifndef Sudden_Underflow
4311 && word0(&u
) & (Exp_mask
& ~Exp_msk1
)
4314 /* The special case */
4321 /* Arrange for convenient computation of quotients:
4322 * shift left if necessary so divisor has 4 leading 0 bits.
4324 * Perhaps we should just compute leading 28 bits of S once
4325 * and for all and pass them and a shift to quorem, so it
4326 * can do shifts and ors to compute the numerator for q.
4339 if (cmp(b
, S
) < 0) {
4341 b
= multadd(b
, 10, 0); /* we botched the k estimate */
4343 mhi
= multadd(mhi
, 10, 0);
4348 if (ilim
<= 0 && (mode
== 3 || mode
== 5)) {
4349 if (ilim
< 0 || cmp(b
, S
= multadd(S
, 5, 0)) <= 0) {
4350 /* no digits, fcvt style */
4362 mhi
= lshift(mhi
, m2
);
4365 /* Compute mlo -- check for special case
4366 * that d is a normalized power of 2.
4371 mhi
= Balloc(mhi
->k
);
4373 mhi
= lshift(mhi
, Log2P
);
4377 dig
= quorem(b
, S
) + '0';
4378 /* Do we yet have the shortest decimal string
4379 * that will round to d?
4382 delta
= diff(S
, mhi
);
4383 j1
= delta
->sign
? 1 : cmp(b
, delta
);
4385 #ifndef ROUND_BIASED
4386 if (j1
== 0 && mode
!= 1 && !(word1(&u
) & 1)
4387 # ifdef Honor_FLT_ROUNDS
4398 else if (!b
->x
[0] && b
->wds
<= 1) {
4406 if (j
< 0 || (j
== 0 && mode
!= 1
4407 #ifndef ROUND_BIASED
4411 if (!b
->x
[0] && b
->wds
<= 1) {
4417 #ifdef Honor_FLT_ROUNDS
4418 if (mode
> 1) switch (Rounding
) {
4424 #endif /*Honor_FLT_ROUNDS*/
4431 if ((j1
> 0 || (j1
== 0 && dig
& 1))
4441 #ifdef Honor_FLT_ROUNDS
4446 if (dig
== '9') { /* possible if i == 1 */
4454 #ifdef Honor_FLT_ROUNDS
4461 b
= multadd(b
, 10, 0);
4463 mlo
= mhi
= multadd(mhi
, 10, 0);
4465 mlo
= multadd(mlo
, 10, 0);
4466 mhi
= multadd(mhi
, 10, 0);
4471 *s
++ = dig
= quorem(b
, S
) + '0';
4472 if (!b
->x
[0] && b
->wds
<= 1) {
4481 b
= multadd(b
, 10, 0);
4484 /* Round off last digit */
4486 #ifdef Honor_FLT_ROUNDS
4499 if (j
> 0 || (j
== 0 && dig
& 1))
4511 #ifdef Honor_FLT_ROUNDS
4514 while (*--s
== '0');
4519 if (mlo
&& mlo
!= mhi
) {
4528 word0(&u
) = Exp_1
+ (70 << Exp_shift
);
4533 else if (!oldinexact
) {