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.
74 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
75 * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS
76 * is also #defined, fegetround() will be queried for the rounding mode.
77 * Note that both FLT_ROUNDS and fegetround() are specified by the C99
78 * standard (and are specified to be consistent, with fesetround()
79 * affecting the value of FLT_ROUNDS), but that some (Linux) systems
80 * do not work correctly in this regard, so using fegetround() is more
81 * portable than using FLT_FOUNDS directly.
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. Similarly, if you
107 * want something other than the system's free() to be called to
108 * recycle memory acquired from MALLOC, #define FREE to be the
109 * name of the alternate routine. (FREE or free is only called in
110 * pathological cases, e.g., in a dtoa call after a dtoa return in
111 * mode 3 with thousands of digits requested.)
112 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
113 * memory allocations from a private pool of memory when possible.
114 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
115 * unless #defined to be a different length. This default length
116 * suffices to get rid of MALLOC calls except for unusual cases,
117 * such as decimal-to-binary conversion of a very long string of
118 * digits. The longest string dtoa can return is about 751 bytes
119 * long. For conversions by strtod of strings of 800 digits and
120 * all dtoa conversions in single-threaded executions with 8-byte
121 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
122 * pointers, PRIVATE_MEM >= 7112 appears adequate.
123 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
124 * #defined automatically on IEEE systems. On such systems,
125 * when INFNAN_CHECK is #defined, strtod checks
126 * for Infinity and NaN (case insensitively). On some systems
127 * (e.g., some HP systems), it may be necessary to #define NAN_WORD0
128 * appropriately -- to the most significant word of a quiet NaN.
129 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
130 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
131 * strtod also accepts (case insensitively) strings of the form
132 * NaN(x), where x is a string of hexadecimal digits and spaces;
133 * if there is only one string of hexadecimal digits, it is taken
134 * for the 52 fraction bits of the resulting NaN; if there are two
135 * or more strings of hex digits, the first is for the high 20 bits,
136 * the second and subsequent for the low 32 bits, with intervening
137 * white space ignored; but if this results in none of the 52
138 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
139 * and NAN_WORD1 are used instead.
140 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
141 * multiple threads. In this case, you must provide (or suitably
142 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
143 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
144 * in pow5mult, ensures lazy evaluation of only one copy of high
145 * powers of 5; omitting this lock would introduce a small
146 * probability of wasting memory, but would otherwise be harmless.)
147 * You must also invoke freedtoa(s) to free the value s returned by
148 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
149 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
150 * avoids underflows on inputs whose result does not underflow.
151 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
152 * floating-point numbers and flushes underflows to zero rather
153 * than implementing gradual underflow, then you must also #define
155 * #define USE_LOCALE to use the current locale's decimal_point value.
156 * #define SET_INEXACT if IEEE arithmetic is being used and extra
157 * computation should be done to set the inexact flag when the
158 * result is inexact and avoid setting inexact when the result
159 * is exact. In this case, dtoa.c must be compiled in
160 * an environment, perhaps provided by #include "dtoa.c" in a
161 * suitable wrapper, that defines two functions,
162 * int get_inexact(void);
163 * void clear_inexact(void);
164 * such that get_inexact() returns a nonzero value if the
165 * inexact bit is already set, and clear_inexact() sets the
166 * inexact bit to 0. When SET_INEXACT is #defined, strtod
167 * also does extra computations to set the underflow and overflow
168 * flags when appropriate (i.e., when the result is tiny and
169 * inexact or when it is a numeric value rounded to +-infinity).
170 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
171 * the result overflows to +-Infinity or underflows to 0.
172 * #define NO_HEX_FP to omit recognition of hexadecimal floating-point
174 * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
175 * to disable logic for "fast" testing of very long input strings
176 * to strtod. This testing proceeds by initially truncating the
177 * input string, then if necessary comparing the whole string with
178 * a decimal expansion to decide close cases. This logic is only
179 * used for input more than STRTOD_DIGLIM digits long (default 40).
193 typedef unsigned Long ULong
;
198 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
208 #ifdef Honor_FLT_ROUNDS
209 #ifndef Trust_FLT_ROUNDS
216 extern char *MALLOC();
218 extern void *MALLOC(size_t);
221 #define MALLOC malloc
224 #ifndef Omit_Private_Memory
226 #define PRIVATE_MEM 2304
228 #define PRIVATE_mem ((unsigned)((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)))
229 static double private_mem
[PRIVATE_mem
], *pmem_next
= private_mem
;
233 #undef Avoid_Underflow
242 #ifndef NO_INFNAN_CHECK
248 #define NO_STRTOD_BIGCOMP
257 #define DBL_MAX_10_EXP 308
258 #define DBL_MAX_EXP 1024
260 #endif /*IEEE_Arith*/
264 #define DBL_MAX_10_EXP 75
265 #define DBL_MAX_EXP 63
267 #define DBL_MAX 7.2370055773322621e+75
272 #define DBL_MAX_10_EXP 38
273 #define DBL_MAX_EXP 127
275 #define DBL_MAX 1.7014118346046923e+38
279 #define LONG_MAX 2147483647
282 #else /* ifndef Bad_float_h */
284 #endif /* Bad_float_h */
294 #define CONST /* blank */
300 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
301 Exactly one of IEEE_8087
, IEEE_MC68k
, VAX
, or IBM should be defined
.
304 typedef union { double d
; ULong L
[2]; } U
;
307 #define word0(x) (x)->L[1]
308 #define word1(x) (x)->L[0]
310 #define word0(x) (x)->L[0]
311 #define word1(x) (x)->L[1]
313 #define dval(x) (x)->d
315 #ifndef STRTOD_DIGLIM
316 #define STRTOD_DIGLIM 40
320 extern int strtod_diglim
;
322 #define strtod_diglim STRTOD_DIGLIM
325 /* The following definition of Storeinc is appropriate for MIPS processors.
326 * An alternative that might be better on some machines is
327 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
329 #if defined(IEEE_8087) + defined(VAX)
330 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
331 ((unsigned short *)a)[0] = (unsigned short)c, a++)
333 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
334 ((unsigned short *)a)[1] = (unsigned short)c, a++)
337 /* #define P DBL_MANT_DIG */
338 /* Ten_pmax = floor(P*log(2)/log(5)) */
339 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
340 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
341 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
345 #define Exp_shift1 20
346 #define Exp_msk1 0x100000
347 #define Exp_msk11 0x100000
348 #define Exp_mask 0x7ff00000
354 #define Exp_1 0x3ff00000
355 #define Exp_11 0x3ff00000
357 #define Frac_mask 0xfffff
358 #define Frac_mask1 0xfffff
361 #define Bndry_mask 0xfffff
362 #define Bndry_mask1 0xfffff
364 #define Sign_bit 0x80000000
370 #ifndef NO_IEEE_Scale
371 #define Avoid_Underflow
372 #ifdef Flush_Denorm /* debugging option */
373 #undef Sudden_Underflow
379 #define Flt_Rounds FLT_ROUNDS
383 #endif /*Flt_Rounds*/
385 #ifdef Honor_FLT_ROUNDS
386 #undef Check_FLT_ROUNDS
387 #define Check_FLT_ROUNDS
389 #define Rounding Flt_Rounds
392 #else /* ifndef IEEE_Arith */
393 #undef Check_FLT_ROUNDS
394 #undef Honor_FLT_ROUNDS
396 #undef Sudden_Underflow
397 #define Sudden_Underflow
402 #define Exp_shift1 24
403 #define Exp_msk1 0x1000000
404 #define Exp_msk11 0x1000000
405 #define Exp_mask 0x7f000000
411 #define Exp_1 0x41000000
412 #define Exp_11 0x41000000
413 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
414 #define Frac_mask 0xffffff
415 #define Frac_mask1 0xffffff
418 #define Bndry_mask 0xefffff
419 #define Bndry_mask1 0xffffff
421 #define Sign_bit 0x80000000
423 #define Tiny0 0x100000
432 #define Exp_msk1 0x80
433 #define Exp_msk11 0x800000
434 #define Exp_mask 0x7f80
440 #define Exp_1 0x40800000
441 #define Exp_11 0x4080
443 #define Frac_mask 0x7fffff
444 #define Frac_mask1 0xffff007f
447 #define Bndry_mask 0xffff007f
448 #define Bndry_mask1 0xffff007f
450 #define Sign_bit 0x8000
456 #endif /* IBM, VAX */
457 #endif /* IEEE_Arith */
464 #define rounded_product(a,b) a = rnd_prod(a, b)
465 #define rounded_quotient(a,b) a = rnd_quot(a, b)
467 extern double rnd_prod(), rnd_quot();
469 extern double rnd_prod(double, double), rnd_quot(double, double);
472 #define rounded_product(a,b) a *= b
473 #define rounded_quotient(a,b) a /= b
476 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
477 #define Big1 0xffffffff
483 typedef struct BCinfo BCinfo
;
485 BCinfo
{ int dp0
, dp1
, dplen
, dsign
, e0
, inexact
, nd
, nd0
, rounding
, scale
, uflchk
; };
488 #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
490 #define FFFFFFFF 0xffffffffUL
497 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
498 * This makes some inner loops simpler and sometimes saves work
499 * during multiplications, but it often seems to make things slightly
500 * slower. Hence the default is now to store 32 bits per Long.
503 #else /* long long available */
505 #define Llong long long
508 #define ULLong unsigned Llong
510 #endif /* NO_LONG_LONG */
512 #ifndef MULTIPLE_THREADS
513 #define ACQUIRE_DTOA_LOCK(n) /*nothing*/
514 #define FREE_DTOA_LOCK(n) /*nothing*/
519 double strtod(const char *s00
, char **se
);
520 char *dtoa(double d
, int mode
, int ndigits
,
521 int *decpt
, int *sign
, char **rve
);
526 int k
, maxwds
, sign
, wds
;
530 typedef struct Bigint Bigint
;
532 static Bigint
*freelist
[Kmax
+1];
544 #ifndef Omit_Private_Memory
548 ACQUIRE_DTOA_LOCK(0);
549 /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
550 /* but this case seems very unlikely. */
551 if (k
<= Kmax
&& (rv
= freelist
[k
]))
552 freelist
[k
] = rv
->next
;
555 #ifdef Omit_Private_Memory
556 rv
= (Bigint
*)MALLOC(sizeof(Bigint
) + (x
-1)*sizeof(ULong
));
558 len
= (sizeof(Bigint
) + (x
-1)*sizeof(ULong
) + sizeof(double) - 1)
560 if (k
<= Kmax
&& pmem_next
- private_mem
+ len
<= PRIVATE_mem
) {
561 rv
= (Bigint
*)pmem_next
;
565 rv
= (Bigint
*)MALLOC(len
*sizeof(double));
571 rv
->sign
= rv
->wds
= 0;
591 ACQUIRE_DTOA_LOCK(0);
592 v
->next
= freelist
[v
->k
];
599 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
600 y->wds*sizeof(Long) + 2*sizeof(int))
605 (b
, m
, a
) Bigint
*b
; int m
, a
;
607 (Bigint
*b
, int m
, int a
) /* multiply by m and add a */
628 y
= *x
* (ULLong
)m
+ carry
;
634 y
= (xi
& 0xffff) * m
+ carry
;
635 z
= (xi
>> 16) * m
+ (y
>> 16);
637 *x
++ = (z
<< 16) + (y
& 0xffff);
647 if (wds
>= b
->maxwds
) {
662 (s
, nd0
, nd
, y9
, dplen
) CONST
char *s
; int nd0
, nd
, dplen
; ULong y9
;
664 (CONST
char *s
, int nd0
, int nd
, ULong y9
, int dplen
)
672 for(k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) ;
679 b
->x
[0] = y9
& 0xffff;
680 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
686 do b
= multadd(b
, 10, *s
++ - '0');
693 b
= multadd(b
, 10, *s
++ - '0');
707 if (!(x
& 0xffff0000)) {
711 if (!(x
& 0xff000000)) {
715 if (!(x
& 0xf0000000)) {
719 if (!(x
& 0xc0000000)) {
723 if (!(x
& 0x80000000)) {
725 if (!(x
& 0x40000000))
798 (a
, b
) Bigint
*a
, *b
;
800 (Bigint
*a
, Bigint
*b
)
805 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
816 if (a
->wds
< b
->wds
) {
828 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
836 for(; xb
< xbe
; xc0
++) {
842 z
= *x
++ * (ULLong
)y
+ *xc
+ carry
;
844 *xc
++ = z
& FFFFFFFF
;
852 for(; xb
< xbe
; xb
++, xc0
++) {
853 if (y
= *xb
& 0xffff) {
858 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
860 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
873 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
876 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
884 for(; xb
< xbe
; xc0
++) {
890 z
= *x
++ * y
+ *xc
+ carry
;
900 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
910 (b
, k
) Bigint
*b
; int k
;
915 Bigint
*b1
, *p5
, *p51
;
917 static int p05
[3] = { 5, 25, 125 };
920 b
= multadd(b
, p05
[i
-1], 0);
926 #ifdef MULTIPLE_THREADS
927 ACQUIRE_DTOA_LOCK(1);
946 if (!(p51
= p5
->next
)) {
947 #ifdef MULTIPLE_THREADS
948 ACQUIRE_DTOA_LOCK(1);
949 if (!(p51
= p5
->next
)) {
950 p51
= p5
->next
= mult(p5
,p5
);
955 p51
= p5
->next
= mult(p5
,p5
);
967 (b
, k
) Bigint
*b
; int k
;
974 ULong
*x
, *x1
, *xe
, z
;
983 for(i
= b
->maxwds
; n1
> i
; i
<<= 1)
987 for(i
= 0; i
< n
; i
++)
1008 *x1
++ = *x
<< k
& 0xffff | z
;
1027 (a
, b
) Bigint
*a
, *b
;
1029 (Bigint
*a
, Bigint
*b
)
1032 ULong
*xa
, *xa0
, *xb
, *xb0
;
1038 if (i
> 1 && !a
->x
[i
-1])
1039 Bug("cmp called with a->x[a->wds-1] == 0");
1040 if (j
> 1 && !b
->x
[j
-1])
1041 Bug("cmp called with b->x[b->wds-1] == 0");
1051 return *xa
< *xb
? -1 : 1;
1061 (a
, b
) Bigint
*a
, *b
;
1063 (Bigint
*a
, Bigint
*b
)
1068 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
1105 y
= (ULLong
)*xa
++ - *xb
++ - borrow
;
1106 borrow
= y
>> 32 & (ULong
)1;
1107 *xc
++ = y
& FFFFFFFF
;
1112 borrow
= y
>> 32 & (ULong
)1;
1113 *xc
++ = y
& FFFFFFFF
;
1118 y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
1119 borrow
= (y
& 0x10000) >> 16;
1120 z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
1121 borrow
= (z
& 0x10000) >> 16;
1126 y
= (*xa
& 0xffff) - borrow
;
1127 borrow
= (y
& 0x10000) >> 16;
1128 z
= (*xa
++ >> 16) - borrow
;
1129 borrow
= (z
& 0x10000) >> 16;
1134 y
= *xa
++ - *xb
++ - borrow
;
1135 borrow
= (y
& 0x10000) >> 16;
1141 borrow
= (y
& 0x10000) >> 16;
1163 L
= (word0(x
) & Exp_mask
) - (P
-1)*Exp_msk1
;
1164 #ifndef Avoid_Underflow
1165 #ifndef Sudden_Underflow
1174 #ifndef Avoid_Underflow
1175 #ifndef Sudden_Underflow
1178 L
= -L
>> Exp_shift
;
1179 if (L
< Exp_shift
) {
1180 word0(&u
) = 0x80000 >> L
;
1186 word1(&u
) = L
>= 31 ? 1 : 1 << 31 - L
;
1197 (a
, e
) Bigint
*a
; int *e
;
1202 ULong
*xa
, *xa0
, w
, y
, z
;
1208 #define d0 word0(&d)
1209 #define d1 word1(&d)
1216 if (!y
) Bug("zero y in b2d");
1222 d0
= Exp_1
| y
>> (Ebits
- k
);
1223 w
= xa
> xa0
? *--xa
: 0;
1224 d1
= y
<< ((32-Ebits
) + k
) | w
>> (Ebits
- k
);
1227 z
= xa
> xa0
? *--xa
: 0;
1229 d0
= Exp_1
| y
<< k
| z
>> (32 - k
);
1230 y
= xa
> xa0
? *--xa
: 0;
1231 d1
= z
<< k
| y
>> (32 - k
);
1238 if (k
< Ebits
+ 16) {
1239 z
= xa
> xa0
? *--xa
: 0;
1240 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
1241 w
= xa
> xa0
? *--xa
: 0;
1242 y
= xa
> xa0
? *--xa
: 0;
1243 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
1246 z
= xa
> xa0
? *--xa
: 0;
1247 w
= xa
> xa0
? *--xa
: 0;
1249 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
1250 y
= xa
> xa0
? *--xa
: 0;
1251 d1
= w
<< k
+ 16 | y
<< k
;
1255 word0(&d
) = d0
>> 16 | d0
<< 16;
1256 word1(&d
) = d1
>> 16 | d1
<< 16;
1267 (d
, e
, bits
) U
*d
; int *e
, *bits
;
1269 (U
*d
, int *e
, int *bits
)
1275 #ifndef Sudden_Underflow
1280 d0
= word0(d
) >> 16 | word0(d
) << 16;
1281 d1
= word1(d
) >> 16 | word1(d
) << 16;
1295 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1296 #ifdef Sudden_Underflow
1297 de
= (int)(d0
>> Exp_shift
);
1302 if ((de
= (int)(d0
>> Exp_shift
)))
1307 if ((k
= lo0bits(&y
))) {
1308 x
[0] = y
| z
<< (32 - k
);
1313 #ifndef Sudden_Underflow
1316 b
->wds
= (x
[1] = z
) ? 2 : 1;
1321 #ifndef Sudden_Underflow
1329 if (k
= lo0bits(&y
))
1331 x
[0] = y
| z
<< 32 - k
& 0xffff;
1332 x
[1] = z
>> k
- 16 & 0xffff;
1338 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1339 x
[2] = z
>> k
& 0xffff;
1354 Bug("Zero passed to d2b");
1372 #ifndef Sudden_Underflow
1376 *e
= (de
- Bias
- (P
-1) << 2) + k
;
1377 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1379 *e
= de
- Bias
- (P
-1) + k
;
1382 #ifndef Sudden_Underflow
1385 *e
= de
- Bias
- (P
-1) + 1 + k
;
1387 *bits
= 32*i
- hi0bits(x
[i
-1]);
1389 *bits
= (i
+2)*16 - hi0bits(x
[i
]);
1401 (a
, b
) Bigint
*a
, *b
;
1403 (Bigint
*a
, Bigint
*b
)
1409 dval(&da
) = b2d(a
, &ka
);
1410 dval(&db
) = b2d(b
, &kb
);
1412 k
= ka
- kb
+ 32*(a
->wds
- b
->wds
);
1414 k
= ka
- kb
+ 16*(a
->wds
- b
->wds
);
1418 word0(&da
) += (k
>> 2)*Exp_msk1
;
1420 dval(&da
) *= 1 << k
;
1424 word0(&db
) += (k
>> 2)*Exp_msk1
;
1426 dval(&db
) *= 1 << k
;
1430 word0(&da
) += k
*Exp_msk1
;
1433 word0(&db
) += k
*Exp_msk1
;
1436 return dval(&da
) / dval(&db
);
1441 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1442 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1451 bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1452 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1453 #ifdef Avoid_Underflow
1454 9007199254740992.*9007199254740992.e
-256
1455 /* = 2^106 * 1e-256 */
1460 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1461 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1462 #define Scale_Bit 0x10
1466 bigtens
[] = { 1e16
, 1e32
, 1e64
};
1467 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
1470 bigtens
[] = { 1e16
, 1e32
};
1471 static CONST
double tinytens
[] = { 1e-16, 1e-32 };
1489 #ifdef Need_Hexdig /*{*/
1490 static unsigned char hexdig
[256];
1494 htinit(h
, s
, inc
) unsigned char *h
; unsigned char *s
; int inc
;
1496 htinit(unsigned char *h
, unsigned char *s
, int inc
)
1500 for(i
= 0; (j
= s
[i
]) !=0; i
++)
1511 #define USC (unsigned char *)
1512 htinit(hexdig
, USC
"0123456789", 0x10);
1513 htinit(hexdig
, USC
"abcdef", 0x10 + 10);
1514 htinit(hexdig
, USC
"ABCDEF", 0x10 + 10);
1516 #endif /* } Need_Hexdig */
1521 #define NAN_WORD0 0x7ff80000
1531 (sp
, t
) char **sp
, *t
;
1533 (CONST
char **sp
, CONST
char *t
)
1537 CONST
char *s
= *sp
;
1540 if ((c
= *++s
) >= 'A' && c
<= 'Z')
1553 (rvp
, sp
) U
*rvp
; CONST
char **sp
;
1555 (U
*rvp
, CONST
char **sp
)
1560 int c1
, havedig
, udx0
, xshift
;
1565 havedig
= xshift
= 0;
1568 /* allow optional initial 0x or 0X */
1569 while((c
= *(CONST
unsigned char*)(s
+1)) && c
<= ' ')
1571 if (s
[1] == '0' && (s
[2] == 'x' || s
[2] == 'X'))
1573 while((c
= *(CONST
unsigned char*)++s
)) {
1574 if ((c1
= hexdig
[c
]))
1576 else if (c
<= ' ') {
1577 if (udx0
&& havedig
) {
1583 #ifdef GDTOA_NON_PEDANTIC_NANCHECK
1584 else if (/*(*/ c
== ')' && havedig
) {
1589 return; /* invalid form: don't change *sp */
1593 if (/*(*/ c
== ')') {
1597 } while((c
= *++s
));
1608 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1609 x
[1] = (x
[1] << 4) | c
;
1611 if ((x
[0] &= 0xfffff) || x
[1]) {
1612 word0(rvp
) = Exp_mask
| x
[0];
1616 #endif /*No_Hex_NaN*/
1617 #endif /* INFNAN_CHECK */
1628 #ifndef NO_HEX_FP /*{*/
1632 rshift(b
, k
) Bigint
*b
; int k
;
1634 rshift(Bigint
*b
, int k
)
1637 ULong
*x
, *x1
, *xe
, y
;
1649 *x1
++ = (y
| (*x
<< n
)) & 0xffffffff;
1659 if ((b
->wds
= x1
- b
->x
) == 0)
1665 any_on(b
, k
) Bigint
*b
; int k
;
1667 any_on(Bigint
*b
, int k
)
1671 ULong
*x
, *x0
, x1
, x2
;
1678 else if (n
< nwds
&& (k
&= kmask
)) {
1693 enum { /* rounding values: same as FLT_ROUNDS */
1702 increment(b
) Bigint
*b
;
1704 increment(Bigint
*b
)
1713 if (*x
< (ULong
)0xffffffffL
) {
1720 if (b
->wds
>= b
->maxwds
) {
1721 b1
= Balloc(b
->k
+1);
1733 gethex(sp
, rvp
, rounding
, sign
)
1734 CONST
char **sp
; U
*rvp
; int rounding
, sign
;
1736 gethex( CONST
char **sp
, U
*rvp
, int rounding
, int sign
)
1740 CONST
unsigned char *decpt
, *s0
, *s
, *s1
;
1742 ULong L
, lostbits
, *x
;
1743 int big
, denorm
, esign
, havedig
, k
, n
, nbits
, up
, zret
;
1748 #ifdef IEEE_Arith /*{{*/
1749 emax
= 0x7fe - Bias
- P
+ 1,
1754 emax
= 0x7ff - Bias
- P
+ 1
1757 emax
= 0x7f - Bias
- P
1763 #ifdef NO_LOCALE_CACHE
1764 const unsigned char *decimalpoint
= (unsigned char*)
1765 localeconv()->decimal_point
;
1767 const unsigned char *decimalpoint
;
1768 static unsigned char *decimalpoint_cache
;
1769 if (!(s0
= decimalpoint_cache
)) {
1770 s0
= (unsigned char*)localeconv()->decimal_point
;
1771 if ((decimalpoint_cache
= (unsigned char*)
1772 MALLOC(strlen((CONST
char*)s0
) + 1))) {
1773 strcpy((char*)decimalpoint_cache
, (CONST
char*)s0
);
1774 s0
= decimalpoint_cache
;
1784 s0
= *(CONST
unsigned char **)sp
+ 2;
1785 while(s0
[havedig
] == '0')
1797 for(i
= 0; decimalpoint
[i
]; ++i
) {
1798 if (s
[i
] != decimalpoint
[i
])
1819 if (*s
== *decimalpoint
&& !decpt
) {
1820 for(i
= 1; decimalpoint
[i
]; ++i
) {
1821 if (s
[i
] != decimalpoint
[i
])
1826 if (*s
== '.' && !decpt
) {
1833 e
= -(((Long
)(s
-decpt
)) << 2);
1847 if ((n
= hexdig
[*s
]) == 0 || n
> 0x19) {
1852 while((n
= hexdig
[*++s
]) !=0 && n
<= 0x19) {
1853 if (e1
& 0xf8000000)
1855 e1
= 10*e1
+ n
- 0x10;
1863 *sp
= (char*)s0
- 1;
1889 #endif /* IEEE_Arith */
1909 for(k
= 0; n
> (1 << (kshift
-2)) - 1; n
>>= 1)
1916 for(i
= 0; decimalpoint
[i
+1]; ++i
);
1920 if (*--s1
== decimalpoint
[i
]) {
1933 L
|= (hexdig
[*s1
] & 0x0f) << n
;
1937 b
->wds
= n
= x
- b
->x
;
1938 n
= ULbits
*n
- hi0bits(L
);
1947 if (x
[k
>>kshift
] & 1 << (k
& kmask
)) {
1949 if (k
> 0 && any_on(b
,k
))
1956 else if (n
< nbits
) {
1969 word0(rvp
) = Exp_mask
;
1978 #ifdef IEEE_Arith /*{*/
1981 if (n
== nbits
&& (n
< 2 || any_on(b
,n
-1)))
1992 #endif /* } IEEE_Arith */
2006 lostbits
= any_on(b
,k
);
2007 if (x
[k
>>kshift
] & 1 << (k
& kmask
))
2020 && (lostbits
& 1) | (x
[0] & 1))
2035 if (nbits
== Nbits
- 1
2036 && x
[nbits
>> kshift
] & 1 << (nbits
& kmask
))
2037 denorm
= 0; /* not currently used */
2041 || ((n
= nbits
& kmask
) !=0
2042 && hi0bits(x
[k
-1]) < 32-n
)) {
2051 word0(rvp
) = b
->wds
> 1 ? b
->x
[1] & ~0x100000 : 0;
2053 word0(rvp
) = (b
->x
[1] & ~0x100000) | ((e
+ 0x3ff + 52) << 20);
2054 word1(rvp
) = b
->x
[0];
2058 k
= b
->x
[0] & ((1 << j
) - 1);
2072 if (k
& j
&& ((k
& (j
-1)) | lostbits
))
2078 word0(rvp
) = b
->x
[1] | ((e
+ 65 + 13) << 24);
2079 word1(rvp
) = b
->x
[0];
2082 /* The next two lines ignore swap of low- and high-order 2 bytes. */
2083 /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
2084 /* word1(rvp) = b->x[0]; */
2085 word0(rvp
) = ((b
->x
[1] & ~0x800000) >> 16) | ((e
+ 129 + 55) << 7) | (b
->x
[1] << 16);
2086 word1(rvp
) = (b
->x
[0] >> 16) | (b
->x
[0] << 16);
2090 #endif /*}!NO_HEX_FP*/
2094 dshift(b
, p2
) Bigint
*b
; int p2
;
2096 dshift(Bigint
*b
, int p2
)
2099 int rv
= hi0bits(b
->x
[b
->wds
-1]) - 4;
2108 (b
, S
) Bigint
*b
, *S
;
2110 (Bigint
*b
, Bigint
*S
)
2114 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
2116 ULLong borrow
, carry
, y
, ys
;
2118 ULong borrow
, carry
, y
, ys
;
2126 /*debug*/ if (b
->wds
> n
)
2127 /*debug*/ Bug("oversize b in quorem");
2135 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
2137 /*debug*/ if (q
> 9)
2138 /*debug*/ Bug("oversized quotient in quorem");
2145 ys
= *sx
++ * (ULLong
)q
+ carry
;
2147 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2148 borrow
= y
>> 32 & (ULong
)1;
2149 *bx
++ = y
& FFFFFFFF
;
2153 ys
= (si
& 0xffff) * q
+ carry
;
2154 zs
= (si
>> 16) * q
+ (ys
>> 16);
2156 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2157 borrow
= (y
& 0x10000) >> 16;
2158 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2159 borrow
= (z
& 0x10000) >> 16;
2162 ys
= *sx
++ * q
+ carry
;
2164 y
= *bx
- (ys
& 0xffff) - borrow
;
2165 borrow
= (y
& 0x10000) >> 16;
2173 while(--bxe
> bx
&& !*bxe
)
2178 if (cmp(b
, S
) >= 0) {
2188 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2189 borrow
= y
>> 32 & (ULong
)1;
2190 *bx
++ = y
& FFFFFFFF
;
2194 ys
= (si
& 0xffff) + carry
;
2195 zs
= (si
>> 16) + (ys
>> 16);
2197 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2198 borrow
= (y
& 0x10000) >> 16;
2199 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2200 borrow
= (z
& 0x10000) >> 16;
2205 y
= *bx
- (ys
& 0xffff) - borrow
;
2206 borrow
= (y
& 0x10000) >> 16;
2215 while(--bxe
> bx
&& !*bxe
)
2223 #ifndef NO_STRTOD_BIGCOMP
2229 U
*rv
; CONST
char *s0
; BCinfo
*bc
;
2231 (U
*rv
, CONST
char *s0
, BCinfo
*bc
)
2235 int b2
, bbits
, d2
, dd
, dig
, dsign
, i
, j
, nd
, nd0
, p2
, p5
, speccase
;
2240 p5
= nd
+ bc
->e0
- 1;
2242 #ifndef Sudden_Underflow
2243 if (rv
->d
== 0.) { /* special case: value near underflow-to-zero */
2244 /* threshold was rounded to zero */
2248 #ifdef Avoid_Underflow
2249 word0(rv
) = (P
+2) << Exp_shift
;
2254 #ifdef Honor_FLT_ROUNDS
2255 if (bc
->rounding
== 1)
2266 b
= d2b(rv
, &p2
, &bbits
);
2267 #ifdef Avoid_Underflow
2270 /* floor(log2(rv)) == bbits - 1 + p2 */
2271 /* Check for denormal case. */
2273 if (i
> (j
= P
- Emin
- 1 + p2
)) {
2274 #ifdef Sudden_Underflow
2279 #ifdef Avoid_Underflow
2280 word0(rv
) = (1 + bc
->scale
) << Exp_shift
;
2282 word0(rv
) = Exp_msk1
;
2289 #ifdef Honor_FLT_ROUNDS
2290 if (bc
->rounding
!= 1) {
2302 #ifndef Sudden_Underflow
2307 /* Arrange for convenient computation of quotients:
2308 * shift left if necessary so divisor has 4 leading 0 bits.
2311 d
= pow5mult(d
, p5
);
2313 b
= pow5mult(b
, -p5
);
2328 /* Now b/d = exactly half-way between the two floating-point values */
2329 /* on either side of the input string. Compute first digit of b/d. */
2331 if (!(dig
= quorem(b
,d
))) {
2332 b
= multadd(b
, 10, 0); /* very unlikely */
2336 /* Compare b/d with s0 */
2338 for(i
= 0; i
< nd0
; ) {
2339 if ((dd
= s0
[i
++] - '0' - dig
))
2341 if (!b
->x
[0] && b
->wds
== 1) {
2346 b
= multadd(b
, 10, 0);
2349 for(j
= bc
->dp1
; i
++ < nd
;) {
2350 if ((dd
= s0
[j
++] - '0' - dig
))
2352 if (!b
->x
[0] && b
->wds
== 1) {
2357 b
= multadd(b
, 10, 0);
2360 if (b
->x
[0] || b
->wds
> 1)
2365 #ifdef Honor_FLT_ROUNDS
2366 if (bc
->rounding
!= 1) {
2368 if (bc
->rounding
== 0) {
2376 if (bc
->rounding
== 0) {
2383 dval(rv
) += 2.*ulp(rv
);
2398 if (!dsign
) /* does not happen for round-near */
2400 dval(rv
) -= ulp(rv
);
2405 dval(rv
) += ulp(rv
);
2409 /* Exact half-way case: apply round-even rule. */
2410 if (word1(rv
) & 1) {
2417 #ifdef Honor_FLT_ROUNDS
2422 #endif /* NO_STRTOD_BIGCOMP */
2427 (s00
, se
) CONST
char *s00
; char **se
;
2429 (CONST
char *s00
, char **se
)
2432 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, e
, e1
;
2433 int esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
2434 CONST
char *s
, *s0
, *s1
;
2437 U aadj2
, adj
, rv
, rv0
;
2440 Bigint
*bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
;
2444 #ifdef Honor_FLT_ROUNDS /*{*/
2445 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
2446 bc
.rounding
= Flt_Rounds
;
2449 switch(fegetround()) {
2450 case FE_TOWARDZERO
: bc
.rounding
= 0; break;
2451 case FE_UPWARD
: bc
.rounding
= 2; break;
2452 case FE_DOWNWARD
: bc
.rounding
= 3;
2460 sign
= nz0
= nz
= bc
.dplen
= bc
.uflchk
= 0;
2462 for(s
= s00
;;s
++) switch(*s
) {
2484 #ifndef NO_HEX_FP /*{*/
2488 #ifdef Honor_FLT_ROUNDS
2489 gethex(&s
, &rv
, bc
.rounding
, sign
);
2491 gethex(&s
, &rv
, 1, sign
);
2497 while(*++s
== '0') ;
2503 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
2509 bc
.dp0
= bc
.dp1
= s
- s0
;
2511 s1
= localeconv()->decimal_point
;
2532 bc
.dplen
= bc
.dp1
- bc
.dp0
;
2534 for(; c
== '0'; c
= *++s
)
2536 if (c
> '0' && c
<= '9') {
2544 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
2549 for(i
= 1; i
< nz
; i
++)
2552 else if (nd
<= DBL_DIG
+ 1)
2556 else if (nd
<= DBL_DIG
+ 1)
2564 if (c
== 'e' || c
== 'E') {
2565 if (!nd
&& !nz
&& !nz0
) {
2576 if (c
>= '0' && c
<= '9') {
2579 if (c
> '0' && c
<= '9') {
2582 while((c
= *++s
) >= '0' && c
<= '9')
2584 if (s
- s1
> 8 || L
> 19999)
2585 /* Avoid confusion from exponents
2586 * so large that e might overflow.
2588 e
= 19999; /* safe for 16 bit ints */
2603 /* Check for Nan and Infinity */
2608 if (match(&s
,"nf")) {
2610 if (!match(&s
,"inity"))
2612 word0(&rv
) = 0x7ff00000;
2619 if (match(&s
, "an")) {
2620 word0(&rv
) = NAN_WORD0
;
2621 word1(&rv
) = NAN_WORD1
;
2623 if (*s
== '(') /*)*/
2629 #endif /* INFNAN_CHECK */
2636 bc
.e0
= e1
= e
-= nf
;
2638 /* Now we have nd0 digits, starting at s0, followed by a
2639 * decimal point, followed by nd-nd0 digits. The number we're
2640 * after is the integer represented by those digits times
2645 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
2650 oldinexact
= get_inexact();
2652 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
2656 #ifndef RND_PRODQUOT
2657 #ifndef Honor_FLT_ROUNDS
2665 if (e
<= Ten_pmax
) {
2667 goto vax_ovfl_check
;
2669 #ifdef Honor_FLT_ROUNDS
2670 /* round correctly FLT_ROUNDS = 2 or 3 */
2676 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2681 if (e
<= Ten_pmax
+ i
) {
2682 /* A fancier test would sometimes let us do
2683 * this for larger i values.
2685 #ifdef Honor_FLT_ROUNDS
2686 /* round correctly FLT_ROUNDS = 2 or 3 */
2693 dval(&rv
) *= tens
[i
];
2695 /* VAX exponent range is so narrow we must
2696 * worry about overflow here...
2699 word0(&rv
) -= P
*Exp_msk1
;
2700 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2701 if ((word0(&rv
) & Exp_mask
)
2702 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
2704 word0(&rv
) += P
*Exp_msk1
;
2706 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2711 #ifndef Inaccurate_Divide
2712 else if (e
>= -Ten_pmax
) {
2713 #ifdef Honor_FLT_ROUNDS
2714 /* round correctly FLT_ROUNDS = 2 or 3 */
2720 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
2731 oldinexact
= get_inexact();
2733 #ifdef Avoid_Underflow
2736 #ifdef Honor_FLT_ROUNDS
2737 if (bc
.rounding
>= 2) {
2739 bc
.rounding
= bc
.rounding
== 2 ? 0 : 2;
2741 if (bc
.rounding
!= 2)
2745 #endif /*IEEE_Arith*/
2747 /* Get starting approximation = rv * 10**e1 */
2751 dval(&rv
) *= tens
[i
];
2753 if (e1
> DBL_MAX_10_EXP
) {
2758 /* Can't trust HUGE_VAL */
2760 #ifdef Honor_FLT_ROUNDS
2761 switch(bc
.rounding
) {
2762 case 0: /* toward 0 */
2763 case 3: /* toward -infinity */
2768 word0(&rv
) = Exp_mask
;
2771 #else /*Honor_FLT_ROUNDS*/
2772 word0(&rv
) = Exp_mask
;
2774 #endif /*Honor_FLT_ROUNDS*/
2776 /* set overflow bit */
2778 dval(&rv0
) *= dval(&rv0
);
2780 #else /*IEEE_Arith*/
2783 #endif /*IEEE_Arith*/
2787 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
2789 dval(&rv
) *= bigtens
[j
];
2790 /* The last multiplication could overflow. */
2791 word0(&rv
) -= P
*Exp_msk1
;
2792 dval(&rv
) *= bigtens
[j
];
2793 if ((z
= word0(&rv
) & Exp_mask
)
2794 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
2796 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
2797 /* set to largest number */
2798 /* (Can't trust DBL_MAX) */
2803 word0(&rv
) += P
*Exp_msk1
;
2809 dval(&rv
) /= tens
[i
];
2811 if (e1
>= 1 << n_bigtens
)
2813 #ifdef Avoid_Underflow
2816 for(j
= 0; e1
> 0; j
++, e1
>>= 1)
2818 dval(&rv
) *= tinytens
[j
];
2819 if (bc
.scale
&& (j
= 2*P
+ 1 - ((word0(&rv
) & Exp_mask
)
2820 >> Exp_shift
)) > 0) {
2821 /* scaled rv is denormal; clear j low bits */
2825 word0(&rv
) = (P
+2)*Exp_msk1
;
2827 word0(&rv
) &= 0xffffffff << (j
-32);
2830 word1(&rv
) &= 0xffffffff << j
;
2833 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
2835 dval(&rv
) *= tinytens
[j
];
2836 /* The last multiplication could underflow. */
2837 dval(&rv0
) = dval(&rv
);
2838 dval(&rv
) *= tinytens
[j
];
2840 dval(&rv
) = 2.*dval(&rv0
);
2841 dval(&rv
) *= tinytens
[j
];
2851 #ifndef Avoid_Underflow
2854 /* The refinement below will clean
2855 * this approximation up.
2862 /* Now the hard part -- adjusting rv to the correct value.*/
2864 /* Put digits into bd: true value = bd * 10^e */
2867 #ifndef NO_STRTOD_BIGCOMP
2868 bc
.nd0
= nd0
; /* Only needed if nd > strtod_diglim, but done here */
2869 /* to silence an erroneous warning about bc.nd0 */
2870 /* possibly not being initialized. */
2871 if (nd
> strtod_diglim
) {
2872 /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
2873 /* minimum number of decimal digits to distinguish double values */
2874 /* in IEEE arithmetic. */
2879 if (--j
<= bc
.dp1
&& j
>= bc
.dp0
)
2889 if (nd
< 9) { /* must recompute y */
2891 for(i
= 0; i
< nd0
; ++i
)
2892 y
= 10*y
+ s0
[i
] - '0';
2893 for(j
= bc
.dp1
; i
< nd
; ++i
)
2894 y
= 10*y
+ s0
[j
++] - '0';
2898 bd0
= s2b(s0
, nd0
, nd
, y
, bc
.dplen
);
2901 bd
= Balloc(bd0
->k
);
2903 bb
= d2b(&rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
2919 #ifdef Honor_FLT_ROUNDS
2920 if (bc
.rounding
!= 1)
2923 #ifdef Avoid_Underflow
2925 i
= j
+ bbbits
- 1; /* logb(rv) */
2926 if (i
< Emin
) /* denormal */
2930 #else /*Avoid_Underflow*/
2931 #ifdef Sudden_Underflow
2933 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
2937 #else /*Sudden_Underflow*/
2939 i
= j
+ bbbits
- 1; /* logb(rv) */
2940 if (i
< Emin
) /* denormal */
2944 #endif /*Sudden_Underflow*/
2945 #endif /*Avoid_Underflow*/
2948 #ifdef Avoid_Underflow
2951 i
= bb2
< bd2
? bb2
: bd2
;
2960 bs
= pow5mult(bs
, bb5
);
2966 bb
= lshift(bb
, bb2
);
2968 bd
= pow5mult(bd
, bd5
);
2970 bd
= lshift(bd
, bd2
);
2972 bs
= lshift(bs
, bs2
);
2973 delta
= diff(bb
, bd
);
2974 bc
.dsign
= delta
->sign
;
2977 #ifndef NO_STRTOD_BIGCOMP
2978 if (bc
.nd
> nd
&& i
<= 0) {
2980 break; /* Must use bigcomp(). */
2981 #ifdef Honor_FLT_ROUNDS
2982 if (bc
.rounding
!= 1) {
2990 i
= -1; /* Discarded digits make delta smaller. */
2994 #ifdef Honor_FLT_ROUNDS
2995 if (bc
.rounding
!= 1) {
2997 /* Error is less than an ulp */
2998 if (!delta
->x
[0] && delta
->wds
<= 1) {
3011 else if (!bc
.dsign
) {
3014 && !(word0(&rv
) & Frac_mask
)) {
3015 y
= word0(&rv
) & Exp_mask
;
3016 #ifdef Avoid_Underflow
3017 if (!bc
.scale
|| y
> 2*P
*Exp_msk1
)
3022 delta
= lshift(delta
,Log2P
);
3023 if (cmp(delta
, bs
) <= 0)
3028 #ifdef Avoid_Underflow
3029 if (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
)
3031 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
3033 #ifdef Sudden_Underflow
3034 if ((word0(&rv
) & Exp_mask
) <=
3036 word0(&rv
) += P
*Exp_msk1
;
3037 dval(&rv
) += adj
.d
*ulp(dval(&rv
));
3038 word0(&rv
) -= P
*Exp_msk1
;
3041 #endif /*Sudden_Underflow*/
3042 #endif /*Avoid_Underflow*/
3043 dval(&rv
) += adj
.d
*ulp(&rv
);
3047 adj
.d
= ratio(delta
, bs
);
3050 if (adj
.d
<= 0x7ffffffe) {
3051 /* adj = rounding ? ceil(adj) : floor(adj); */
3054 if (!((bc
.rounding
>>1) ^ bc
.dsign
))
3059 #ifdef Avoid_Underflow
3060 if (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
3061 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
3063 #ifdef Sudden_Underflow
3064 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
3065 word0(&rv
) += P
*Exp_msk1
;
3066 adj
.d
*= ulp(dval(&rv
));
3071 word0(&rv
) -= P
*Exp_msk1
;
3074 #endif /*Sudden_Underflow*/
3075 #endif /*Avoid_Underflow*/
3078 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
)
3086 #endif /*Honor_FLT_ROUNDS*/
3089 /* Error is less than half an ulp -- check for
3090 * special case of mantissa a power of two.
3092 if (bc
.dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
3094 #ifdef Avoid_Underflow
3095 || (word0(&rv
) & Exp_mask
) <= (2*P
+1)*Exp_msk1
3097 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
3102 if (!delta
->x
[0] && delta
->wds
<= 1)
3107 if (!delta
->x
[0] && delta
->wds
<= 1) {
3114 delta
= lshift(delta
,Log2P
);
3115 if (cmp(delta
, bs
) > 0)
3120 /* exactly half-way between */
3122 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
3124 #ifdef Avoid_Underflow
3125 (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
3126 ? (0xffffffff & (0xffffffff << (2*P
+1-(y
>>Exp_shift
)))) :
3129 /*boundary case -- increment exponent*/
3130 word0(&rv
) = (word0(&rv
) & Exp_mask
)
3137 #ifdef Avoid_Underflow
3143 else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
3145 /* boundary case -- decrement exponent */
3146 #ifdef Sudden_Underflow /*{{*/
3147 L
= word0(&rv
) & Exp_mask
;
3151 #ifdef Avoid_Underflow
3152 if (L
<= (bc
.scale
? (2*P
+1)*Exp_msk1
: Exp_msk1
))
3155 #endif /*Avoid_Underflow*/
3165 #else /*Sudden_Underflow}{*/
3166 #ifdef Avoid_Underflow
3168 L
= word0(&rv
) & Exp_mask
;
3169 if (L
<= (2*P
+1)*Exp_msk1
) {
3170 if (L
> (P
+2)*Exp_msk1
)
3171 /* round even ==> */
3174 /* rv = smallest denormal */
3182 #endif /*Avoid_Underflow*/
3183 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
3184 #endif /*Sudden_Underflow}}*/
3185 word0(&rv
) = L
| Bndry_mask1
;
3186 word1(&rv
) = 0xffffffff;
3193 #ifndef ROUND_BIASED
3194 if (!(word1(&rv
) & LSB
))
3198 dval(&rv
) += ulp(&rv
);
3199 #ifndef ROUND_BIASED
3201 dval(&rv
) -= ulp(&rv
);
3202 #ifndef Sudden_Underflow
3212 #ifdef Avoid_Underflow
3213 bc
.dsign
= 1 - bc
.dsign
;
3218 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
3221 else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
3222 #ifndef Sudden_Underflow
3223 if (word1(&rv
) == Tiny1
&& !word0(&rv
)) {
3235 /* special case -- power of FLT_RADIX to be */
3236 /* rounded down... */
3238 if (aadj
< 2./FLT_RADIX
)
3239 aadj
= 1./FLT_RADIX
;
3247 aadj1
= bc
.dsign
? aadj
: -aadj
;
3248 #ifdef Check_FLT_ROUNDS
3249 switch(bc
.rounding
) {
3250 case 2: /* towards +infinity */
3253 case 0: /* towards 0 */
3254 case 3: /* towards -infinity */
3258 if (Flt_Rounds
== 0)
3260 #endif /*Check_FLT_ROUNDS*/
3262 y
= word0(&rv
) & Exp_mask
;
3264 /* Check for overflow */
3266 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
3267 dval(&rv0
) = dval(&rv
);
3268 word0(&rv
) -= P
*Exp_msk1
;
3269 adj
.d
= aadj1
* ulp(&rv
);
3271 if ((word0(&rv
) & Exp_mask
) >=
3272 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
3273 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
)
3280 word0(&rv
) += P
*Exp_msk1
;
3283 #ifdef Avoid_Underflow
3284 if (bc
.scale
&& y
<= 2*P
*Exp_msk1
) {
3285 if (aadj
<= 0x7fffffff) {
3286 if ((z
= aadj
) <= 0)
3289 aadj1
= bc
.dsign
? aadj
: -aadj
;
3291 dval(&aadj2
) = aadj1
;
3292 word0(&aadj2
) += (2*P
+1)*Exp_msk1
- y
;
3293 aadj1
= dval(&aadj2
);
3295 adj
.d
= aadj1
* ulp(&rv
);
3298 #ifdef Sudden_Underflow
3299 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
3300 dval(&rv0
) = dval(&rv
);
3301 word0(&rv
) += P
*Exp_msk1
;
3302 adj
.d
= aadj1
* ulp(&rv
);
3305 if ((word0(&rv
) & Exp_mask
) < P
*Exp_msk1
)
3307 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
)
3310 if (word0(&rv0
) == Tiny0
3311 && word1(&rv0
) == Tiny1
) {
3323 word0(&rv
) -= P
*Exp_msk1
;
3326 adj
.d
= aadj1
* ulp(&rv
);
3329 #else /*Sudden_Underflow*/
3330 /* Compute adj so that the IEEE rounding rules will
3331 * correctly round rv + adj in some half-way cases.
3332 * If rv * ulp(rv) is denormalized (i.e.,
3333 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
3334 * trouble from bits lost to denormalization;
3335 * example: 1.2e-307 .
3337 if (y
<= (P
-1)*Exp_msk1
&& aadj
> 1.) {
3338 aadj1
= (double)(int)(aadj
+ 0.5);
3342 adj
.d
= aadj1
* ulp(&rv
);
3344 #endif /*Sudden_Underflow*/
3345 #endif /*Avoid_Underflow*/
3347 z
= word0(&rv
) & Exp_mask
;
3350 #ifdef Avoid_Underflow
3354 /* Can we stop now? */
3357 /* The tolerances below are conservative. */
3358 if (bc
.dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
3359 if (aadj
< .4999999 || aadj
> .5000001)
3362 else if (aadj
< .4999999/FLT_RADIX
)
3378 #ifndef NO_STRTOD_BIGCOMP
3380 bigcomp(&rv
, s0
, &bc
);
3385 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
3390 else if (!oldinexact
)
3393 #ifdef Avoid_Underflow
3395 word0(&rv0
) = Exp_1
- 2*P
*Exp_msk1
;
3397 dval(&rv
) *= dval(&rv0
);
3399 /* try to avoid the bug of testing an 8087 register value */
3401 if (!(word0(&rv
) & Exp_mask
))
3403 if (word0(&rv
) == 0 && word1(&rv
) == 0)
3408 #endif /* Avoid_Underflow */
3410 if (bc
.inexact
&& !(word0(&rv
) & Exp_mask
)) {
3411 /* set underflow bit */
3412 dval(&rv0
) = 1e-300;
3413 dval(&rv0
) *= dval(&rv0
);
3419 return sign
? -dval(&rv
) : dval(&rv
);
3422 #ifndef MULTIPLE_THREADS
3423 static char *dtoa_result
;
3437 sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= (size_t)i
;
3440 r
= (int*)Balloc(k
);
3443 #ifndef MULTIPLE_THREADS
3451 nrv_alloc(s
, rve
, n
) char *s
, **rve
; int n
;
3453 nrv_alloc(CONST
char *s
, char **rve
, int n
)
3458 t
= rv
= rv_alloc(n
);
3459 while((*t
= *s
++)) t
++;
3465 /* freedtoa(s) must be used to free values s returned by dtoa
3466 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
3467 * but for consistency with earlier versions of dtoa, it is optional
3468 * when MULTIPLE_THREADS is not defined.
3473 freedtoa(s
) char *s
;
3478 Bigint
*b
= (Bigint
*)((int *)s
- 1);
3479 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
3481 #ifndef MULTIPLE_THREADS
3482 if (s
== dtoa_result
)
3487 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
3489 * Inspired by "How to Print Floating-Point Numbers Accurately" by
3490 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
3493 * 1. Rather than iterating, we use a simple numeric overestimate
3494 * to determine k = floor(log10(d)). We scale relevant
3495 * quantities using O(log2(k)) rather than O(k) multiplications.
3496 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
3497 * try to generate digits strictly left to right. Instead, we
3498 * compute with fewer bits and propagate the carry if necessary
3499 * when rounding the final digit up. This is often faster.
3500 * 3. Under the assumption that input will be rounded nearest,
3501 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
3502 * That is, we allow equality in stopping tests when the
3503 * round-nearest rule will give the same floating-point value
3504 * as would satisfaction of the stopping test with strict
3506 * 4. We remove common factors of powers of 2 from relevant
3508 * 5. When converting floating-point integers less than 1e16,
3509 * we use floating-point arithmetic rather than resorting
3510 * to multiple-precision integers.
3511 * 6. When asked to produce fewer than 15 digits, we first try
3512 * to get by with floating-point arithmetic; we resort to
3513 * multiple-precision integer arithmetic only if we cannot
3514 * guarantee that the floating-point calculation has given
3515 * the correctly rounded result. For k requested digits and
3516 * "uniformly" distributed input, the probability is
3517 * something like 10^(k-15) that we must resort to the Long
3524 (dd
, mode
, ndigits
, decpt
, sign
, rve
)
3525 double dd
; int mode
, ndigits
, *decpt
, *sign
; char **rve
;
3527 (double dd
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
)
3530 /* Arguments ndigits, decpt, sign are similar to those
3531 of ecvt and fcvt; trailing zeros are suppressed from
3532 the returned string. If not null, *rve is set to point
3533 to the end of the return value. If d is +-Infinity or NaN,
3534 then *decpt is set to 9999.
3537 0 ==> shortest string that yields d when read in
3538 and rounded to nearest.
3539 1 ==> like 0, but with Steele & White stopping rule;
3540 e.g. with IEEE P754 arithmetic , mode 0 gives
3541 1e23 whereas mode 1 gives 9.999999999999999e22.
3542 2 ==> max(1,ndigits) significant digits. This gives a
3543 return value similar to that of ecvt, except
3544 that trailing zeros are suppressed.
3545 3 ==> through ndigits past the decimal point. This
3546 gives a return value similar to that from fcvt,
3547 except that trailing zeros are suppressed, and
3548 ndigits can be negative.
3549 4,5 ==> similar to 2 and 3, respectively, but (in
3550 round-nearest mode) with the tests of mode 0 to
3551 possibly return a shorter string that rounds to d.
3552 With IEEE arithmetic and compilation with
3553 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
3554 as modes 2 and 3 when FLT_ROUNDS != 1.
3555 6-9 ==> Debugging modes similar to mode - 4: don't try
3556 fast floating-point estimate (if applicable).
3558 Values of mode other than 0-9 are treated as mode 0.
3560 Sufficient space is allocated to the return value
3561 to hold the suppressed trailing zeros.
3564 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
,
3565 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
3566 spec_case
, try_quick
;
3568 #ifndef Sudden_Underflow
3572 Bigint
*b
, *b1
, *delta
, *mlo
= NULL
, *mhi
, *S
;
3577 int inexact
, oldinexact
;
3579 #ifdef Honor_FLT_ROUNDS /*{*/
3581 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
3582 Rounding
= Flt_Rounds
;
3585 switch(fegetround()) {
3586 case FE_TOWARDZERO
: Rounding
= 0; break;
3587 case FE_UPWARD
: Rounding
= 2; break;
3588 case FE_DOWNWARD
: Rounding
= 3;
3593 #ifndef MULTIPLE_THREADS
3595 freedtoa(dtoa_result
);
3601 if (word0(&u
) & Sign_bit
) {
3602 /* set sign for everything, including 0's and NaNs */
3604 word0(&u
) &= ~Sign_bit
; /* clear sign bit */
3609 #if defined(IEEE_Arith) + defined(VAX)
3611 if ((word0(&u
) & Exp_mask
) == Exp_mask
)
3613 if (word0(&u
) == 0x8000)
3616 /* Infinity or NaN */
3619 if (!word1(&u
) && !(word0(&u
) & 0xfffff))
3620 return nrv_alloc("Infinity", rve
, 8);
3622 return nrv_alloc("NaN", rve
, 3);
3626 dval(&u
) += 0; /* normalize */
3630 return nrv_alloc("0", rve
, 1);
3634 try_quick
= oldinexact
= get_inexact();
3637 #ifdef Honor_FLT_ROUNDS
3638 if (Rounding
>= 2) {
3640 Rounding
= Rounding
== 2 ? 0 : 2;
3647 b
= d2b(&u
, &be
, &bbits
);
3648 #ifdef Sudden_Underflow
3649 i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
));
3651 if ((i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
)))) {
3653 dval(&d2
) = dval(&u
);
3654 word0(&d2
) &= Frac_mask1
;
3655 word0(&d2
) |= Exp_11
;
3657 if (j
= 11 - hi0bits(word0(&d2
) & Frac_mask
))
3658 dval(&d2
) /= 1 << j
;
3661 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
3662 * log10(x) = log(x) / log(10)
3663 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
3664 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
3666 * This suggests computing an approximation k to log10(d) by
3668 * k = (i - Bias)*0.301029995663981
3669 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
3671 * We want k to be too large rather than too small.
3672 * The error in the first-order Taylor series approximation
3673 * is in our favor, so we just round up the constant enough
3674 * to compensate for any error in the multiplication of
3675 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
3676 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
3677 * adding 1e-13 to the constant term more than suffices.
3678 * Hence we adjust the constant term to 0.1760912590558.
3679 * (We could get a more accurate k by invoking log10,
3680 * but this is probably not worthwhile.)
3688 #ifndef Sudden_Underflow
3692 /* d is denormalized */
3694 i
= bbits
+ be
+ (Bias
+ (P
-1) - 1);
3695 x
= i
> 32 ? word0(&u
) << (64 - i
) | word1(&u
) >> (i
- 32)
3696 : word1(&u
) << (32 - i
);
3698 word0(&d2
) -= 31*Exp_msk1
; /* adjust exponent */
3699 i
-= (Bias
+ (P
-1) - 1) + 1;
3703 ds
= (dval(&d2
)-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
3705 if (ds
< 0. && ds
!= k
)
3706 k
--; /* want k = floor(ds) */
3708 if (k
>= 0 && k
<= Ten_pmax
) {
3709 if (dval(&u
) < tens
[k
])
3732 if (mode
< 0 || mode
> 9)
3736 #ifdef Check_FLT_ROUNDS
3737 try_quick
= Rounding
== 1;
3741 #endif /*SET_INEXACT*/
3748 ilim
= ilim1
= -1; /* Values for cases 0 and 1; done here to */
3749 /* silence erroneous "gcc -Wall" warning. */
3762 ilim
= ilim1
= i
= ndigits
;
3768 i
= ndigits
+ k
+ 1;
3774 s
= s0
= rv_alloc(i
);
3776 #ifdef Honor_FLT_ROUNDS
3777 if (mode
> 1 && Rounding
!= 1)
3781 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
3783 /* Try to get by with floating-point arithmetic. */
3786 dval(&d2
) = dval(&u
);
3789 ieps
= 2; /* conservative */
3794 /* prevent overflows */
3796 dval(&u
) /= bigtens
[n_bigtens
-1];
3799 for(; j
; j
>>= 1, i
++)
3806 else if ((j1
= -k
)) {
3807 dval(&u
) *= tens
[j1
& 0xf];
3808 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
3811 dval(&u
) *= bigtens
[i
];
3814 if (k_check
&& dval(&u
) < 1. && ilim
> 0) {
3822 dval(&eps
) = ieps
*dval(&u
) + 7.;
3823 word0(&eps
) -= (P
-1)*Exp_msk1
;
3827 if (dval(&u
) > dval(&eps
))
3829 if (dval(&u
) < -dval(&eps
))
3833 #ifndef No_leftright
3835 /* Use Steele & White method of only
3836 * generating digits needed.
3838 dval(&eps
) = 0.5/tens
[ilim
-1] - dval(&eps
);
3842 *s
++ = '0' + (int)L
;
3843 if (dval(&u
) < dval(&eps
))
3845 if (1. - dval(&u
) < dval(&eps
))
3855 /* Generate ilim digits, then fix them up. */
3856 dval(&eps
) *= tens
[ilim
-1];
3857 for(i
= 1;; i
++, dval(&u
) *= 10.) {
3858 L
= (Long
)(dval(&u
));
3859 if (!(dval(&u
) -= L
))
3861 *s
++ = '0' + (int)L
;
3863 if (dval(&u
) > 0.5 + dval(&eps
))
3865 else if (dval(&u
) < 0.5 - dval(&eps
)) {
3866 while(*--s
== '0') {}
3873 #ifndef No_leftright
3878 dval(&u
) = dval(&d2
);
3883 /* Do we have a "small" integer? */
3885 if (be
>= 0 && k
<= Int_max
) {
3888 if (ndigits
< 0 && ilim
<= 0) {
3890 if (ilim
< 0 || dval(&u
) <= 5*ds
)
3894 for(i
= 1; i
<= k
+ 1; i
++, dval(&u
) *= 10.) {
3895 L
= (Long
)(dval(&u
) / ds
);
3897 #ifdef Check_FLT_ROUNDS
3898 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
3904 *s
++ = '0' + (int)L
;
3912 #ifdef Honor_FLT_ROUNDS
3916 case 2: goto bump_up
;
3919 dval(&u
) += dval(&u
);
3920 if (dval(&u
) > ds
|| (dval(&u
) == ds
&& L
& 1)) {
3941 #ifndef Sudden_Underflow
3942 denorm
? be
+ (Bias
+ (P
-1) - 1 + 1) :
3945 1 + 4*P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
3953 if (m2
> 0 && s2
> 0) {
3954 i
= m2
< s2
? m2
: s2
;
3962 mhi
= pow5mult(mhi
, m5
);
3971 b
= pow5mult(b
, b5
);
3975 S
= pow5mult(S
, s5
);
3977 /* Check for special case that d is a normalized power of 2. */
3980 if ((mode
< 2 || leftright
)
3981 #ifdef Honor_FLT_ROUNDS
3985 if (!word1(&u
) && !(word0(&u
) & Bndry_mask
)
3986 #ifndef Sudden_Underflow
3987 && word0(&u
) & (Exp_mask
& ~Exp_msk1
)
3990 /* The special case */
3997 /* Arrange for convenient computation of quotients:
3998 * shift left if necessary so divisor has 4 leading 0 bits.
4000 * Perhaps we should just compute leading 28 bits of S once
4001 * and for all and pass them and a shift to quorem, so it
4002 * can do shifts and ors to compute the numerator for q.
4005 if ((i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f))
4009 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0xf)
4024 b
= multadd(b
, 10, 0); /* we botched the k estimate */
4026 mhi
= multadd(mhi
, 10, 0);
4030 if (ilim
<= 0 && (mode
== 3 || mode
== 5)) {
4031 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
4032 /* no digits, fcvt style */
4044 mhi
= lshift(mhi
, m2
);
4046 /* Compute mlo -- check for special case
4047 * that d is a normalized power of 2.
4052 mhi
= Balloc(mhi
->k
);
4054 mhi
= lshift(mhi
, Log2P
);
4058 dig
= quorem(b
,S
) + '0';
4059 /* Do we yet have the shortest decimal string
4060 * that will round to d?
4063 delta
= diff(S
, mhi
);
4064 j1
= delta
->sign
? 1 : cmp(b
, delta
);
4066 #ifndef ROUND_BIASED
4067 if (j1
== 0 && mode
!= 1 && !(word1(&u
) & 1)
4068 #ifdef Honor_FLT_ROUNDS
4077 else if (!b
->x
[0] && b
->wds
<= 1)
4084 if (j
< 0 || (j
== 0 && mode
!= 1
4085 #ifndef ROUND_BIASED
4089 if (!b
->x
[0] && b
->wds
<= 1) {
4095 #ifdef Honor_FLT_ROUNDS
4098 case 0: goto accept_dig
;
4099 case 2: goto keep_dig
;
4101 #endif /*Honor_FLT_ROUNDS*/
4105 if ((j1
> 0 || (j1
== 0 && dig
& 1))
4114 #ifdef Honor_FLT_ROUNDS
4118 if (dig
== '9') { /* possible if i == 1 */
4126 #ifdef Honor_FLT_ROUNDS
4132 b
= multadd(b
, 10, 0);
4134 mlo
= mhi
= multadd(mhi
, 10, 0);
4136 mlo
= multadd(mlo
, 10, 0);
4137 mhi
= multadd(mhi
, 10, 0);
4143 *s
++ = dig
= quorem(b
,S
) + '0';
4144 if (!b
->x
[0] && b
->wds
<= 1) {
4152 b
= multadd(b
, 10, 0);
4155 /* Round off last digit */
4157 #ifdef Honor_FLT_ROUNDS
4159 case 0: goto trimzeros
;
4160 case 2: goto roundoff
;
4165 if (j
> 0 || (j
== 0 && dig
& 1)) {
4176 #ifdef Honor_FLT_ROUNDS
4179 while(*--s
== '0') {}
4185 if (mlo
&& mlo
!= mhi
)
4193 word0(&u
) = Exp_1
+ (70 << Exp_shift
);
4198 else if (!oldinexact
)
4209 } // namespace dmg_fp