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
&& freelist
[k
]) {
553 freelist
[k
] = rv
->next
;
557 #ifdef Omit_Private_Memory
558 rv
= (Bigint
*)MALLOC(sizeof(Bigint
) + (x
-1)*sizeof(ULong
));
560 len
= (sizeof(Bigint
) + (x
-1)*sizeof(ULong
) + sizeof(double) - 1)
562 if (k
<= Kmax
&& pmem_next
- private_mem
+ len
<= PRIVATE_mem
) {
563 rv
= (Bigint
*)pmem_next
;
567 rv
= (Bigint
*)MALLOC(len
*sizeof(double));
573 rv
->sign
= rv
->wds
= 0;
593 ACQUIRE_DTOA_LOCK(0);
594 v
->next
= freelist
[v
->k
];
601 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
602 y->wds*sizeof(Long) + 2*sizeof(int))
607 (b
, m
, a
) Bigint
*b
; int m
, a
;
609 (Bigint
*b
, int m
, int a
) /* multiply by m and add a */
630 y
= *x
* (ULLong
)m
+ carry
;
636 y
= (xi
& 0xffff) * m
+ carry
;
637 z
= (xi
>> 16) * m
+ (y
>> 16);
639 *x
++ = (z
<< 16) + (y
& 0xffff);
649 if (wds
>= b
->maxwds
) {
655 b
->x
[wds
++] = (ULong
)carry
;
664 (s
, nd0
, nd
, y9
, dplen
) CONST
char *s
; int nd0
, nd
, dplen
; ULong y9
;
666 (CONST
char *s
, int nd0
, int nd
, ULong y9
, int dplen
)
674 for(k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) ;
681 b
->x
[0] = y9
& 0xffff;
682 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
688 do b
= multadd(b
, 10, *s
++ - '0');
695 b
= multadd(b
, 10, *s
++ - '0');
709 if (!(x
& 0xffff0000)) {
713 if (!(x
& 0xff000000)) {
717 if (!(x
& 0xf0000000)) {
721 if (!(x
& 0xc0000000)) {
725 if (!(x
& 0x80000000)) {
727 if (!(x
& 0x40000000))
800 (a
, b
) Bigint
*a
, *b
;
802 (Bigint
*a
, Bigint
*b
)
807 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
818 if (a
->wds
< b
->wds
) {
830 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
838 for(; xb
< xbe
; xc0
++) {
845 z
= *x
++ * (ULLong
)y
+ *xc
+ carry
;
847 *xc
++ = z
& FFFFFFFF
;
855 for(; xb
< xbe
; xb
++, xc0
++) {
856 if (y
= *xb
& 0xffff) {
861 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
863 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
876 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
879 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
887 for(; xb
< xbe
; xc0
++) {
893 z
= *x
++ * y
+ *xc
+ carry
;
903 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
913 (b
, k
) Bigint
*b
; int k
;
918 Bigint
*b1
, *p5
, *p51
;
920 static int p05
[3] = { 5, 25, 125 };
924 b
= multadd(b
, p05
[i
-1], 0);
931 #ifdef MULTIPLE_THREADS
932 ACQUIRE_DTOA_LOCK(1);
954 #ifdef MULTIPLE_THREADS
955 ACQUIRE_DTOA_LOCK(1);
958 p51
= p5
->next
= mult(p5
,p5
);
963 p51
= p5
->next
= mult(p5
,p5
);
975 (b
, k
) Bigint
*b
; int k
;
982 ULong
*x
, *x1
, *xe
, z
;
991 for(i
= b
->maxwds
; n1
> i
; i
<<= 1)
995 for(i
= 0; i
< n
; i
++)
1004 *x1
++ = *x
<< k
| z
;
1017 *x1
++ = *x
<< k
& 0xffff | z
;
1036 (a
, b
) Bigint
*a
, *b
;
1038 (Bigint
*a
, Bigint
*b
)
1041 ULong
*xa
, *xa0
, *xb
, *xb0
;
1047 if (i
> 1 && !a
->x
[i
-1])
1048 Bug("cmp called with a->x[a->wds-1] == 0");
1049 if (j
> 1 && !b
->x
[j
-1])
1050 Bug("cmp called with b->x[b->wds-1] == 0");
1060 return *xa
< *xb
? -1 : 1;
1070 (a
, b
) Bigint
*a
, *b
;
1072 (Bigint
*a
, Bigint
*b
)
1077 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
1114 y
= (ULLong
)*xa
++ - *xb
++ - borrow
;
1115 borrow
= y
>> 32 & (ULong
)1;
1116 *xc
++ = y
& FFFFFFFF
;
1121 borrow
= y
>> 32 & (ULong
)1;
1122 *xc
++ = y
& FFFFFFFF
;
1127 y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
1128 borrow
= (y
& 0x10000) >> 16;
1129 z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
1130 borrow
= (z
& 0x10000) >> 16;
1135 y
= (*xa
& 0xffff) - borrow
;
1136 borrow
= (y
& 0x10000) >> 16;
1137 z
= (*xa
++ >> 16) - borrow
;
1138 borrow
= (z
& 0x10000) >> 16;
1143 y
= *xa
++ - *xb
++ - borrow
;
1144 borrow
= (y
& 0x10000) >> 16;
1150 borrow
= (y
& 0x10000) >> 16;
1172 L
= (word0(x
) & Exp_mask
) - (P
-1)*Exp_msk1
;
1173 #ifndef Avoid_Underflow
1174 #ifndef Sudden_Underflow
1183 #ifndef Avoid_Underflow
1184 #ifndef Sudden_Underflow
1187 L
= -L
>> Exp_shift
;
1188 if (L
< Exp_shift
) {
1189 word0(&u
) = 0x80000 >> L
;
1195 word1(&u
) = L
>= 31 ? 1 : 1 << 31 - L
;
1206 (a
, e
) Bigint
*a
; int *e
;
1211 ULong
*xa
, *xa0
, w
, y
, z
;
1217 #define d0 word0(&d)
1218 #define d1 word1(&d)
1225 if (!y
) Bug("zero y in b2d");
1231 d0
= Exp_1
| y
>> (Ebits
- k
);
1232 w
= xa
> xa0
? *--xa
: 0;
1233 d1
= y
<< ((32-Ebits
) + k
) | w
>> (Ebits
- k
);
1236 z
= xa
> xa0
? *--xa
: 0;
1238 d0
= Exp_1
| y
<< k
| z
>> (32 - k
);
1239 y
= xa
> xa0
? *--xa
: 0;
1240 d1
= z
<< k
| y
>> (32 - k
);
1247 if (k
< Ebits
+ 16) {
1248 z
= xa
> xa0
? *--xa
: 0;
1249 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
1250 w
= xa
> xa0
? *--xa
: 0;
1251 y
= xa
> xa0
? *--xa
: 0;
1252 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
1255 z
= xa
> xa0
? *--xa
: 0;
1256 w
= xa
> xa0
? *--xa
: 0;
1258 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
1259 y
= xa
> xa0
? *--xa
: 0;
1260 d1
= w
<< k
+ 16 | y
<< k
;
1264 word0(&d
) = d0
>> 16 | d0
<< 16;
1265 word1(&d
) = d1
>> 16 | d1
<< 16;
1276 (d
, e
, bits
) U
*d
; int *e
, *bits
;
1278 (U
*d
, int *e
, int *bits
)
1284 #ifndef Sudden_Underflow
1289 d0
= word0(d
) >> 16 | word0(d
) << 16;
1290 d1
= word1(d
) >> 16 | word1(d
) << 16;
1304 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1305 #ifdef Sudden_Underflow
1306 de
= (int)(d0
>> Exp_shift
);
1311 de
= (int)(d0
>> Exp_shift
);
1320 x
[0] = y
| z
<< (32 - k
);
1326 b
->wds
= x
[1] ? 2 : 1;
1327 #ifndef Sudden_Underflow
1334 #ifndef Sudden_Underflow
1342 if (k
= lo0bits(&y
))
1344 x
[0] = y
| z
<< 32 - k
& 0xffff;
1345 x
[1] = z
>> k
- 16 & 0xffff;
1351 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1352 x
[2] = z
>> k
& 0xffff;
1367 Bug("Zero passed to d2b");
1385 #ifndef Sudden_Underflow
1389 *e
= (de
- Bias
- (P
-1) << 2) + k
;
1390 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1392 *e
= de
- Bias
- (P
-1) + k
;
1395 #ifndef Sudden_Underflow
1398 *e
= de
- Bias
- (P
-1) + 1 + k
;
1400 *bits
= 32*i
- hi0bits(x
[i
-1]);
1402 *bits
= (i
+2)*16 - hi0bits(x
[i
]);
1414 (a
, b
) Bigint
*a
, *b
;
1416 (Bigint
*a
, Bigint
*b
)
1422 dval(&da
) = b2d(a
, &ka
);
1423 dval(&db
) = b2d(b
, &kb
);
1425 k
= ka
- kb
+ 32*(a
->wds
- b
->wds
);
1427 k
= ka
- kb
+ 16*(a
->wds
- b
->wds
);
1431 word0(&da
) += (k
>> 2)*Exp_msk1
;
1433 dval(&da
) *= 1 << k
;
1437 word0(&db
) += (k
>> 2)*Exp_msk1
;
1439 dval(&db
) *= 1 << k
;
1443 word0(&da
) += k
*Exp_msk1
;
1446 word0(&db
) += k
*Exp_msk1
;
1449 return dval(&da
) / dval(&db
);
1454 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1455 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1464 bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1465 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1466 #ifdef Avoid_Underflow
1467 9007199254740992.*9007199254740992.e
-256
1468 /* = 2^106 * 1e-256 */
1473 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1474 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1475 #define Scale_Bit 0x10
1479 bigtens
[] = { 1e16
, 1e32
, 1e64
};
1480 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
1483 bigtens
[] = { 1e16
, 1e32
};
1484 static CONST
double tinytens
[] = { 1e-16, 1e-32 };
1502 #ifdef Need_Hexdig /*{*/
1503 static unsigned char hexdig
[256];
1507 htinit(h
, s
, inc
) unsigned char *h
; unsigned char *s
; int inc
;
1509 htinit(unsigned char *h
, unsigned char *s
, int inc
)
1513 for(i
= 0; (j
= s
[i
]) !=0; i
++)
1514 h
[j
] = (unsigned char)(i
+ inc
);
1524 #define USC (unsigned char *)
1525 htinit(hexdig
, USC
"0123456789", 0x10);
1526 htinit(hexdig
, USC
"abcdef", 0x10 + 10);
1527 htinit(hexdig
, USC
"ABCDEF", 0x10 + 10);
1529 #endif /* } Need_Hexdig */
1534 #define NAN_WORD0 0x7ff80000
1544 (sp
, t
) char **sp
, *t
;
1546 (CONST
char **sp
, CONST
char *t
)
1550 CONST
char *s
= *sp
;
1552 for(d
= *t
++; d
; d
= *t
++) {
1553 if ((c
= *++s
) >= 'A' && c
<= 'Z')
1566 (rvp
, sp
) U
*rvp
; CONST
char **sp
;
1568 (U
*rvp
, CONST
char **sp
)
1573 int c1
, havedig
, udx0
, xshift
;
1578 havedig
= xshift
= 0;
1581 /* allow optional initial 0x or 0X */
1582 for(c
= *(CONST
unsigned char*)(s
+1); c
&& c
<= ' '; c
= *(CONST
unsigned char*)(s
+1))
1584 if (s
[1] == '0' && (s
[2] == 'x' || s
[2] == 'X'))
1586 for(c
= *(CONST
unsigned char*)++s
; c
; c
= *(CONST
unsigned char*)++s
) {
1590 else if (c
<= ' ') {
1591 if (udx0
&& havedig
) {
1597 #ifdef GDTOA_NON_PEDANTIC_NANCHECK
1598 else if (/*(*/ c
== ')' && havedig
) {
1603 return; /* invalid form: don't change *sp */
1607 if (/*(*/ c
== ')') {
1623 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1624 x
[1] = (x
[1] << 4) | c
;
1626 if ((x
[0] &= 0xfffff) || x
[1]) {
1627 word0(rvp
) = Exp_mask
| x
[0];
1631 #endif /*No_Hex_NaN*/
1632 #endif /* INFNAN_CHECK */
1643 #ifndef NO_HEX_FP /*{*/
1647 rshift(b
, k
) Bigint
*b
; int k
;
1649 rshift(Bigint
*b
, int k
)
1652 ULong
*x
, *x1
, *xe
, y
;
1664 *x1
++ = (y
| (*x
<< n
)) & 0xffffffff;
1674 if ((b
->wds
= x1
- b
->x
) == 0)
1680 any_on(b
, k
) Bigint
*b
; int k
;
1682 any_on(Bigint
*b
, int k
)
1686 ULong
*x
, *x0
, x1
, x2
;
1693 else if (n
< nwds
&& (k
&= kmask
)) {
1708 enum { /* rounding values: same as FLT_ROUNDS */
1717 increment(b
) Bigint
*b
;
1719 increment(Bigint
*b
)
1728 if (*x
< (ULong
)0xffffffffL
) {
1735 if (b
->wds
>= b
->maxwds
) {
1736 b1
= Balloc(b
->k
+1);
1748 gethex(sp
, rvp
, rounding
, sign
)
1749 CONST
char **sp
; U
*rvp
; int rounding
, sign
;
1751 gethex( CONST
char **sp
, U
*rvp
, int rounding
, int sign
)
1755 CONST
unsigned char *decpt
, *s0
, *s
, *s1
;
1757 ULong L
, lostbits
, *x
;
1758 int big
, denorm
, esign
, havedig
, k
, n
, nbits
, up
, zret
;
1763 #ifdef IEEE_Arith /*{{*/
1764 emax
= 0x7fe - Bias
- P
+ 1,
1769 emax
= 0x7ff - Bias
- P
+ 1
1772 emax
= 0x7f - Bias
- P
1778 #ifdef NO_LOCALE_CACHE
1779 const unsigned char *decimalpoint
= (unsigned char*)
1780 localeconv()->decimal_point
;
1782 const unsigned char *decimalpoint
;
1783 static unsigned char *decimalpoint_cache
;
1784 if (!(s0
= decimalpoint_cache
)) {
1785 s0
= (unsigned char*)localeconv()->decimal_point
;
1786 if ((decimalpoint_cache
= (unsigned char*)
1787 MALLOC(strlen((CONST
char*)s0
) + 1))) {
1788 strcpy((char*)decimalpoint_cache
, (CONST
char*)s0
);
1789 s0
= decimalpoint_cache
;
1799 s0
= *(CONST
unsigned char **)sp
+ 2;
1800 while(s0
[havedig
] == '0')
1812 for(i
= 0; decimalpoint
[i
]; ++i
) {
1813 if (s
[i
] != decimalpoint
[i
])
1834 if (*s
== *decimalpoint
&& !decpt
) {
1835 for(i
= 1; decimalpoint
[i
]; ++i
) {
1836 if (s
[i
] != decimalpoint
[i
])
1841 if (*s
== '.' && !decpt
) {
1848 e
= -(((Long
)(s
-decpt
)) << 2);
1862 if ((n
= hexdig
[*s
]) == 0 || n
> 0x19) {
1867 while((n
= hexdig
[*++s
]) !=0 && n
<= 0x19) {
1868 if (e1
& 0xf8000000)
1870 e1
= 10*e1
+ n
- 0x10;
1878 *sp
= (char*)s0
- 1;
1904 #endif /* IEEE_Arith */
1924 for(k
= 0; n
> (1 << (kshift
-2)) - 1; n
>>= 1)
1931 for(i
= 0; decimalpoint
[i
+1]; ++i
);
1935 if (*--s1
== decimalpoint
[i
]) {
1948 L
|= (hexdig
[*s1
] & 0x0f) << n
;
1952 b
->wds
= n
= x
- b
->x
;
1953 n
= ULbits
*n
- hi0bits(L
);
1962 if (x
[k
>>kshift
] & 1 << (k
& kmask
)) {
1964 if (k
> 0 && any_on(b
,k
))
1971 else if (n
< nbits
) {
1984 word0(rvp
) = Exp_mask
;
1993 #ifdef IEEE_Arith /*{*/
1996 if (n
== nbits
&& (n
< 2 || any_on(b
,n
-1)))
2007 #endif /* } IEEE_Arith */
2021 lostbits
= any_on(b
,k
);
2022 if (x
[k
>>kshift
] & 1 << (k
& kmask
))
2035 && (lostbits
& 1) | (x
[0] & 1))
2050 if (nbits
== Nbits
- 1
2051 && x
[nbits
>> kshift
] & 1 << (nbits
& kmask
))
2052 denorm
= 0; /* not currently used */
2056 || ((n
= nbits
& kmask
) !=0
2057 && hi0bits(x
[k
-1]) < 32-n
)) {
2066 word0(rvp
) = b
->wds
> 1 ? b
->x
[1] & ~0x100000 : 0;
2068 word0(rvp
) = (b
->x
[1] & ~0x100000) | ((e
+ 0x3ff + 52) << 20);
2069 word1(rvp
) = b
->x
[0];
2073 k
= b
->x
[0] & ((1 << j
) - 1);
2087 if (k
& j
&& ((k
& (j
-1)) | lostbits
))
2093 word0(rvp
) = b
->x
[1] | ((e
+ 65 + 13) << 24);
2094 word1(rvp
) = b
->x
[0];
2097 /* The next two lines ignore swap of low- and high-order 2 bytes. */
2098 /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
2099 /* word1(rvp) = b->x[0]; */
2100 word0(rvp
) = ((b
->x
[1] & ~0x800000) >> 16) | ((e
+ 129 + 55) << 7) | (b
->x
[1] << 16);
2101 word1(rvp
) = (b
->x
[0] >> 16) | (b
->x
[0] << 16);
2105 #endif /*}!NO_HEX_FP*/
2109 dshift(b
, p2
) Bigint
*b
; int p2
;
2111 dshift(Bigint
*b
, int p2
)
2114 int rv
= hi0bits(b
->x
[b
->wds
-1]) - 4;
2123 (b
, S
) Bigint
*b
, *S
;
2125 (Bigint
*b
, Bigint
*S
)
2129 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
2131 ULLong borrow
, carry
, y
, ys
;
2133 ULong borrow
, carry
, y
, ys
;
2141 /*debug*/ if (b
->wds
> n
)
2142 /*debug*/ Bug("oversize b in quorem");
2150 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
2152 /*debug*/ if (q
> 9)
2153 /*debug*/ Bug("oversized quotient in quorem");
2160 ys
= *sx
++ * (ULLong
)q
+ carry
;
2162 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2163 borrow
= y
>> 32 & (ULong
)1;
2164 *bx
++ = y
& FFFFFFFF
;
2168 ys
= (si
& 0xffff) * q
+ carry
;
2169 zs
= (si
>> 16) * q
+ (ys
>> 16);
2171 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2172 borrow
= (y
& 0x10000) >> 16;
2173 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2174 borrow
= (z
& 0x10000) >> 16;
2177 ys
= *sx
++ * q
+ carry
;
2179 y
= *bx
- (ys
& 0xffff) - borrow
;
2180 borrow
= (y
& 0x10000) >> 16;
2188 while(--bxe
> bx
&& !*bxe
)
2193 if (cmp(b
, S
) >= 0) {
2203 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2204 borrow
= y
>> 32 & (ULong
)1;
2205 *bx
++ = y
& FFFFFFFF
;
2209 ys
= (si
& 0xffff) + carry
;
2210 zs
= (si
>> 16) + (ys
>> 16);
2212 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2213 borrow
= (y
& 0x10000) >> 16;
2214 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2215 borrow
= (z
& 0x10000) >> 16;
2220 y
= *bx
- (ys
& 0xffff) - borrow
;
2221 borrow
= (y
& 0x10000) >> 16;
2230 while(--bxe
> bx
&& !*bxe
)
2238 #ifndef NO_STRTOD_BIGCOMP
2244 U
*rv
; CONST
char *s0
; BCinfo
*bc
;
2246 (U
*rv
, CONST
char *s0
, BCinfo
*bc
)
2250 int b2
, bbits
, d2
, dd
, dig
, dsign
, i
, j
, nd
, nd0
, p2
, p5
, speccase
;
2255 p5
= nd
+ bc
->e0
- 1;
2257 #ifndef Sudden_Underflow
2258 if (rv
->d
== 0.) { /* special case: value near underflow-to-zero */
2259 /* threshold was rounded to zero */
2263 #ifdef Avoid_Underflow
2264 word0(rv
) = (P
+2) << Exp_shift
;
2269 #ifdef Honor_FLT_ROUNDS
2270 if (bc
->rounding
== 1)
2281 b
= d2b(rv
, &p2
, &bbits
);
2282 #ifdef Avoid_Underflow
2285 /* floor(log2(rv)) == bbits - 1 + p2 */
2286 /* Check for denormal case. */
2288 if (i
> (j
= P
- Emin
- 1 + p2
)) {
2289 #ifdef Sudden_Underflow
2294 #ifdef Avoid_Underflow
2295 word0(rv
) = (1 + bc
->scale
) << Exp_shift
;
2297 word0(rv
) = Exp_msk1
;
2304 #ifdef Honor_FLT_ROUNDS
2305 if (bc
->rounding
!= 1) {
2317 #ifndef Sudden_Underflow
2322 /* Arrange for convenient computation of quotients:
2323 * shift left if necessary so divisor has 4 leading 0 bits.
2326 d
= pow5mult(d
, p5
);
2328 b
= pow5mult(b
, -p5
);
2343 /* Now b/d = exactly half-way between the two floating-point values */
2344 /* on either side of the input string. Compute first digit of b/d. */
2348 b
= multadd(b
, 10, 0); /* very unlikely */
2352 /* Compare b/d with s0 */
2354 for(i
= 0; i
< nd0
; ) {
2355 dd
= s0
[i
++] - '0' - dig
;
2358 if (!b
->x
[0] && b
->wds
== 1) {
2363 b
= multadd(b
, 10, 0);
2366 for(j
= bc
->dp1
; i
++ < nd
;) {
2367 dd
= s0
[j
++] - '0' - dig
;
2370 if (!b
->x
[0] && b
->wds
== 1) {
2375 b
= multadd(b
, 10, 0);
2378 if (b
->x
[0] || b
->wds
> 1)
2383 #ifdef Honor_FLT_ROUNDS
2384 if (bc
->rounding
!= 1) {
2386 if (bc
->rounding
== 0) {
2394 if (bc
->rounding
== 0) {
2401 dval(rv
) += 2.*ulp(rv
);
2416 if (!dsign
) /* does not happen for round-near */
2418 dval(rv
) -= ulp(rv
);
2423 dval(rv
) += ulp(rv
);
2427 /* Exact half-way case: apply round-even rule. */
2428 if (word1(rv
) & 1) {
2435 #ifdef Honor_FLT_ROUNDS
2440 #endif /* NO_STRTOD_BIGCOMP */
2445 (s00
, se
) CONST
char *s00
; char **se
;
2447 (CONST
char *s00
, char **se
)
2450 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, e
, e1
;
2451 int esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
2452 CONST
char *s
, *s0
, *s1
;
2455 U aadj2
, adj
, rv
, rv0
;
2458 Bigint
*bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
;
2462 #ifdef Honor_FLT_ROUNDS /*{*/
2463 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
2464 bc
.rounding
= Flt_Rounds
;
2467 switch(fegetround()) {
2468 case FE_TOWARDZERO
: bc
.rounding
= 0; break;
2469 case FE_UPWARD
: bc
.rounding
= 2; break;
2470 case FE_DOWNWARD
: bc
.rounding
= 3;
2478 sign
= nz0
= nz
= bc
.dplen
= bc
.uflchk
= 0;
2480 for(s
= s00
;;s
++) switch(*s
) {
2502 #ifndef NO_HEX_FP /*{*/
2506 #ifdef Honor_FLT_ROUNDS
2507 gethex(&s
, &rv
, bc
.rounding
, sign
);
2509 gethex(&s
, &rv
, 1, sign
);
2515 while(*++s
== '0') ;
2521 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
2527 bc
.dp0
= bc
.dp1
= s
- s0
;
2529 s1
= localeconv()->decimal_point
;
2550 bc
.dplen
= bc
.dp1
- bc
.dp0
;
2552 for(; c
== '0'; c
= *++s
)
2554 if (c
> '0' && c
<= '9') {
2562 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
2567 for(i
= 1; i
< nz
; i
++)
2570 else if (nd
<= DBL_DIG
+ 1)
2574 else if (nd
<= DBL_DIG
+ 1)
2582 if (c
== 'e' || c
== 'E') {
2583 if (!nd
&& !nz
&& !nz0
) {
2594 if (c
>= '0' && c
<= '9') {
2597 if (c
> '0' && c
<= '9') {
2600 while((c
= *++s
) >= '0' && c
<= '9')
2602 if (s
- s1
> 8 || L
> 19999)
2603 /* Avoid confusion from exponents
2604 * so large that e might overflow.
2606 e
= 19999; /* safe for 16 bit ints */
2621 /* Check for Nan and Infinity */
2626 if (match(&s
,"nf")) {
2628 if (!match(&s
,"inity"))
2630 word0(&rv
) = 0x7ff00000;
2637 if (match(&s
, "an")) {
2638 word0(&rv
) = NAN_WORD0
;
2639 word1(&rv
) = NAN_WORD1
;
2641 if (*s
== '(') /*)*/
2647 #endif /* INFNAN_CHECK */
2654 bc
.e0
= e1
= e
-= nf
;
2656 /* Now we have nd0 digits, starting at s0, followed by a
2657 * decimal point, followed by nd-nd0 digits. The number we're
2658 * after is the integer represented by those digits times
2663 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
2668 oldinexact
= get_inexact();
2670 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
2674 #ifndef RND_PRODQUOT
2675 #ifndef Honor_FLT_ROUNDS
2683 if (e
<= Ten_pmax
) {
2685 goto vax_ovfl_check
;
2687 #ifdef Honor_FLT_ROUNDS
2688 /* round correctly FLT_ROUNDS = 2 or 3 */
2694 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2699 if (e
<= Ten_pmax
+ i
) {
2700 /* A fancier test would sometimes let us do
2701 * this for larger i values.
2703 #ifdef Honor_FLT_ROUNDS
2704 /* round correctly FLT_ROUNDS = 2 or 3 */
2711 dval(&rv
) *= tens
[i
];
2713 /* VAX exponent range is so narrow we must
2714 * worry about overflow here...
2717 word0(&rv
) -= P
*Exp_msk1
;
2718 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2719 if ((word0(&rv
) & Exp_mask
)
2720 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
2722 word0(&rv
) += P
*Exp_msk1
;
2724 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
2729 #ifndef Inaccurate_Divide
2730 else if (e
>= -Ten_pmax
) {
2731 #ifdef Honor_FLT_ROUNDS
2732 /* round correctly FLT_ROUNDS = 2 or 3 */
2738 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
2749 oldinexact
= get_inexact();
2751 #ifdef Avoid_Underflow
2754 #ifdef Honor_FLT_ROUNDS
2755 if (bc
.rounding
>= 2) {
2757 bc
.rounding
= bc
.rounding
== 2 ? 0 : 2;
2759 if (bc
.rounding
!= 2)
2763 #endif /*IEEE_Arith*/
2765 /* Get starting approximation = rv * 10**e1 */
2770 dval(&rv
) *= tens
[i
];
2772 if (e1
> DBL_MAX_10_EXP
) {
2777 /* Can't trust HUGE_VAL */
2779 #ifdef Honor_FLT_ROUNDS
2780 switch(bc
.rounding
) {
2781 case 0: /* toward 0 */
2782 case 3: /* toward -infinity */
2787 word0(&rv
) = Exp_mask
;
2790 #else /*Honor_FLT_ROUNDS*/
2791 word0(&rv
) = Exp_mask
;
2793 #endif /*Honor_FLT_ROUNDS*/
2795 /* set overflow bit */
2797 dval(&rv0
) *= dval(&rv0
);
2799 #else /*IEEE_Arith*/
2802 #endif /*IEEE_Arith*/
2806 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
2808 dval(&rv
) *= bigtens
[j
];
2809 /* The last multiplication could overflow. */
2810 word0(&rv
) -= P
*Exp_msk1
;
2811 dval(&rv
) *= bigtens
[j
];
2812 if ((z
= word0(&rv
) & Exp_mask
)
2813 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
2815 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
2816 /* set to largest number */
2817 /* (Can't trust DBL_MAX) */
2822 word0(&rv
) += P
*Exp_msk1
;
2829 dval(&rv
) /= tens
[i
];
2831 if (e1
>= 1 << n_bigtens
)
2833 #ifdef Avoid_Underflow
2836 for(j
= 0; e1
> 0; j
++, e1
>>= 1)
2838 dval(&rv
) *= tinytens
[j
];
2839 if (bc
.scale
&& (j
= 2*P
+ 1 - ((word0(&rv
) & Exp_mask
)
2840 >> Exp_shift
)) > 0) {
2841 /* scaled rv is denormal; clear j low bits */
2845 word0(&rv
) = (P
+2)*Exp_msk1
;
2847 word0(&rv
) &= 0xffffffff << (j
-32);
2850 word1(&rv
) &= 0xffffffff << j
;
2853 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
2855 dval(&rv
) *= tinytens
[j
];
2856 /* The last multiplication could underflow. */
2857 dval(&rv0
) = dval(&rv
);
2858 dval(&rv
) *= tinytens
[j
];
2860 dval(&rv
) = 2.*dval(&rv0
);
2861 dval(&rv
) *= tinytens
[j
];
2871 #ifndef Avoid_Underflow
2874 /* The refinement below will clean
2875 * this approximation up.
2882 /* Now the hard part -- adjusting rv to the correct value.*/
2884 /* Put digits into bd: true value = bd * 10^e */
2887 #ifndef NO_STRTOD_BIGCOMP
2888 bc
.nd0
= nd0
; /* Only needed if nd > strtod_diglim, but done here */
2889 /* to silence an erroneous warning about bc.nd0 */
2890 /* possibly not being initialized. */
2891 if (nd
> strtod_diglim
) {
2892 /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
2893 /* minimum number of decimal digits to distinguish double values */
2894 /* in IEEE arithmetic. */
2899 if (--j
<= bc
.dp1
&& j
>= bc
.dp0
)
2909 if (nd
< 9) { /* must recompute y */
2911 for(i
= 0; i
< nd0
; ++i
)
2912 y
= 10*y
+ s0
[i
] - '0';
2913 for(j
= bc
.dp1
; i
< nd
; ++i
)
2914 y
= 10*y
+ s0
[j
++] - '0';
2918 bd0
= s2b(s0
, nd0
, nd
, y
, bc
.dplen
);
2921 bd
= Balloc(bd0
->k
);
2923 bb
= d2b(&rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
2939 #ifdef Honor_FLT_ROUNDS
2940 if (bc
.rounding
!= 1)
2943 #ifdef Avoid_Underflow
2945 i
= j
+ bbbits
- 1; /* logb(rv) */
2946 if (i
< Emin
) /* denormal */
2950 #else /*Avoid_Underflow*/
2951 #ifdef Sudden_Underflow
2953 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
2957 #else /*Sudden_Underflow*/
2959 i
= j
+ bbbits
- 1; /* logb(rv) */
2960 if (i
< Emin
) /* denormal */
2964 #endif /*Sudden_Underflow*/
2965 #endif /*Avoid_Underflow*/
2968 #ifdef Avoid_Underflow
2971 i
= bb2
< bd2
? bb2
: bd2
;
2980 bs
= pow5mult(bs
, bb5
);
2986 bb
= lshift(bb
, bb2
);
2988 bd
= pow5mult(bd
, bd5
);
2990 bd
= lshift(bd
, bd2
);
2992 bs
= lshift(bs
, bs2
);
2993 delta
= diff(bb
, bd
);
2994 bc
.dsign
= delta
->sign
;
2997 #ifndef NO_STRTOD_BIGCOMP
2998 if (bc
.nd
> nd
&& i
<= 0) {
3000 break; /* Must use bigcomp(). */
3001 #ifdef Honor_FLT_ROUNDS
3002 if (bc
.rounding
!= 1) {
3010 i
= -1; /* Discarded digits make delta smaller. */
3014 #ifdef Honor_FLT_ROUNDS
3015 if (bc
.rounding
!= 1) {
3017 /* Error is less than an ulp */
3018 if (!delta
->x
[0] && delta
->wds
<= 1) {
3031 else if (!bc
.dsign
) {
3034 && !(word0(&rv
) & Frac_mask
)) {
3035 y
= word0(&rv
) & Exp_mask
;
3036 #ifdef Avoid_Underflow
3037 if (!bc
.scale
|| y
> 2*P
*Exp_msk1
)
3042 delta
= lshift(delta
,Log2P
);
3043 if (cmp(delta
, bs
) <= 0)
3048 #ifdef Avoid_Underflow
3049 if (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
)
3051 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
3053 #ifdef Sudden_Underflow
3054 if ((word0(&rv
) & Exp_mask
) <=
3056 word0(&rv
) += P
*Exp_msk1
;
3057 dval(&rv
) += adj
.d
*ulp(dval(&rv
));
3058 word0(&rv
) -= P
*Exp_msk1
;
3061 #endif /*Sudden_Underflow*/
3062 #endif /*Avoid_Underflow*/
3063 dval(&rv
) += adj
.d
*ulp(&rv
);
3067 adj
.d
= ratio(delta
, bs
);
3070 if (adj
.d
<= 0x7ffffffe) {
3071 /* adj = rounding ? ceil(adj) : floor(adj); */
3074 if (!((bc
.rounding
>>1) ^ bc
.dsign
))
3079 #ifdef Avoid_Underflow
3080 if (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
3081 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
3083 #ifdef Sudden_Underflow
3084 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
3085 word0(&rv
) += P
*Exp_msk1
;
3086 adj
.d
*= ulp(dval(&rv
));
3091 word0(&rv
) -= P
*Exp_msk1
;
3094 #endif /*Sudden_Underflow*/
3095 #endif /*Avoid_Underflow*/
3098 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
)
3106 #endif /*Honor_FLT_ROUNDS*/
3109 /* Error is less than half an ulp -- check for
3110 * special case of mantissa a power of two.
3112 if (bc
.dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
3114 #ifdef Avoid_Underflow
3115 || (word0(&rv
) & Exp_mask
) <= (2*P
+1)*Exp_msk1
3117 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
3122 if (!delta
->x
[0] && delta
->wds
<= 1)
3127 if (!delta
->x
[0] && delta
->wds
<= 1) {
3134 delta
= lshift(delta
,Log2P
);
3135 if (cmp(delta
, bs
) > 0)
3140 /* exactly half-way between */
3142 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
3144 #ifdef Avoid_Underflow
3145 (bc
.scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
3146 ? (0xffffffff & (0xffffffff << (2*P
+1-(y
>>Exp_shift
)))) :
3149 /*boundary case -- increment exponent*/
3150 word0(&rv
) = (word0(&rv
) & Exp_mask
)
3157 #ifdef Avoid_Underflow
3163 else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
3165 /* boundary case -- decrement exponent */
3166 #ifdef Sudden_Underflow /*{{*/
3167 L
= word0(&rv
) & Exp_mask
;
3171 #ifdef Avoid_Underflow
3172 if (L
<= (bc
.scale
? (2*P
+1)*Exp_msk1
: Exp_msk1
))
3175 #endif /*Avoid_Underflow*/
3185 #else /*Sudden_Underflow}{*/
3186 #ifdef Avoid_Underflow
3188 L
= word0(&rv
) & Exp_mask
;
3189 if (L
<= (2*P
+1)*Exp_msk1
) {
3190 if (L
> (P
+2)*Exp_msk1
)
3191 /* round even ==> */
3194 /* rv = smallest denormal */
3202 #endif /*Avoid_Underflow*/
3203 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
3204 #endif /*Sudden_Underflow}}*/
3205 word0(&rv
) = L
| Bndry_mask1
;
3206 word1(&rv
) = 0xffffffff;
3213 #ifndef ROUND_BIASED
3214 if (!(word1(&rv
) & LSB
))
3218 dval(&rv
) += ulp(&rv
);
3219 #ifndef ROUND_BIASED
3221 dval(&rv
) -= ulp(&rv
);
3222 #ifndef Sudden_Underflow
3232 #ifdef Avoid_Underflow
3233 bc
.dsign
= 1 - bc
.dsign
;
3238 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
3241 else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
3242 #ifndef Sudden_Underflow
3243 if (word1(&rv
) == Tiny1
&& !word0(&rv
)) {
3255 /* special case -- power of FLT_RADIX to be */
3256 /* rounded down... */
3258 if (aadj
< 2./FLT_RADIX
)
3259 aadj
= 1./FLT_RADIX
;
3267 aadj1
= bc
.dsign
? aadj
: -aadj
;
3268 #ifdef Check_FLT_ROUNDS
3269 switch(bc
.rounding
) {
3270 case 2: /* towards +infinity */
3273 case 0: /* towards 0 */
3274 case 3: /* towards -infinity */
3278 if (Flt_Rounds
== 0)
3280 #endif /*Check_FLT_ROUNDS*/
3282 y
= word0(&rv
) & Exp_mask
;
3284 /* Check for overflow */
3286 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
3287 dval(&rv0
) = dval(&rv
);
3288 word0(&rv
) -= P
*Exp_msk1
;
3289 adj
.d
= aadj1
* ulp(&rv
);
3291 if ((word0(&rv
) & Exp_mask
) >=
3292 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
3293 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
)
3300 word0(&rv
) += P
*Exp_msk1
;
3303 #ifdef Avoid_Underflow
3304 if (bc
.scale
&& y
<= 2*P
*Exp_msk1
) {
3305 if (aadj
<= 0x7fffffff) {
3306 if ((z
= (ULong
)aadj
) <= 0)
3309 aadj1
= bc
.dsign
? aadj
: -aadj
;
3311 dval(&aadj2
) = aadj1
;
3312 word0(&aadj2
) += (2*P
+1)*Exp_msk1
- y
;
3313 aadj1
= dval(&aadj2
);
3315 adj
.d
= aadj1
* ulp(&rv
);
3318 #ifdef Sudden_Underflow
3319 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
3320 dval(&rv0
) = dval(&rv
);
3321 word0(&rv
) += P
*Exp_msk1
;
3322 adj
.d
= aadj1
* ulp(&rv
);
3325 if ((word0(&rv
) & Exp_mask
) < P
*Exp_msk1
)
3327 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
)
3330 if (word0(&rv0
) == Tiny0
3331 && word1(&rv0
) == Tiny1
) {
3343 word0(&rv
) -= P
*Exp_msk1
;
3346 adj
.d
= aadj1
* ulp(&rv
);
3349 #else /*Sudden_Underflow*/
3350 /* Compute adj so that the IEEE rounding rules will
3351 * correctly round rv + adj in some half-way cases.
3352 * If rv * ulp(rv) is denormalized (i.e.,
3353 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
3354 * trouble from bits lost to denormalization;
3355 * example: 1.2e-307 .
3357 if (y
<= (P
-1)*Exp_msk1
&& aadj
> 1.) {
3358 aadj1
= (double)(int)(aadj
+ 0.5);
3362 adj
.d
= aadj1
* ulp(&rv
);
3364 #endif /*Sudden_Underflow*/
3365 #endif /*Avoid_Underflow*/
3367 z
= word0(&rv
) & Exp_mask
;
3370 #ifdef Avoid_Underflow
3374 /* Can we stop now? */
3377 /* The tolerances below are conservative. */
3378 if (bc
.dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
3379 if (aadj
< .4999999 || aadj
> .5000001)
3382 else if (aadj
< .4999999/FLT_RADIX
)
3398 #ifndef NO_STRTOD_BIGCOMP
3400 bigcomp(&rv
, s0
, &bc
);
3405 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
3410 else if (!oldinexact
)
3413 #ifdef Avoid_Underflow
3415 word0(&rv0
) = Exp_1
- 2*P
*Exp_msk1
;
3417 dval(&rv
) *= dval(&rv0
);
3419 /* try to avoid the bug of testing an 8087 register value */
3421 if (!(word0(&rv
) & Exp_mask
))
3423 if (word0(&rv
) == 0 && word1(&rv
) == 0)
3428 #endif /* Avoid_Underflow */
3430 if (bc
.inexact
&& !(word0(&rv
) & Exp_mask
)) {
3431 /* set underflow bit */
3432 dval(&rv0
) = 1e-300;
3433 dval(&rv0
) *= dval(&rv0
);
3439 return sign
? -dval(&rv
) : dval(&rv
);
3442 #ifndef MULTIPLE_THREADS
3443 static char *dtoa_result
;
3457 sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= (size_t)i
;
3460 r
= (int*)Balloc(k
);
3463 #ifndef MULTIPLE_THREADS
3471 nrv_alloc(s
, rve
, n
) char *s
, **rve
; int n
;
3473 nrv_alloc(CONST
char *s
, char **rve
, int n
)
3478 t
= rv
= rv_alloc(n
);
3479 for(*t
= *s
++; *t
; *t
= *s
++) t
++;
3485 /* freedtoa(s) must be used to free values s returned by dtoa
3486 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
3487 * but for consistency with earlier versions of dtoa, it is optional
3488 * when MULTIPLE_THREADS is not defined.
3493 freedtoa(s
) char *s
;
3498 Bigint
*b
= (Bigint
*)((int *)s
- 1);
3499 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
3501 #ifndef MULTIPLE_THREADS
3502 if (s
== dtoa_result
)
3507 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
3509 * Inspired by "How to Print Floating-Point Numbers Accurately" by
3510 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
3513 * 1. Rather than iterating, we use a simple numeric overestimate
3514 * to determine k = floor(log10(d)). We scale relevant
3515 * quantities using O(log2(k)) rather than O(k) multiplications.
3516 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
3517 * try to generate digits strictly left to right. Instead, we
3518 * compute with fewer bits and propagate the carry if necessary
3519 * when rounding the final digit up. This is often faster.
3520 * 3. Under the assumption that input will be rounded nearest,
3521 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
3522 * That is, we allow equality in stopping tests when the
3523 * round-nearest rule will give the same floating-point value
3524 * as would satisfaction of the stopping test with strict
3526 * 4. We remove common factors of powers of 2 from relevant
3528 * 5. When converting floating-point integers less than 1e16,
3529 * we use floating-point arithmetic rather than resorting
3530 * to multiple-precision integers.
3531 * 6. When asked to produce fewer than 15 digits, we first try
3532 * to get by with floating-point arithmetic; we resort to
3533 * multiple-precision integer arithmetic only if we cannot
3534 * guarantee that the floating-point calculation has given
3535 * the correctly rounded result. For k requested digits and
3536 * "uniformly" distributed input, the probability is
3537 * something like 10^(k-15) that we must resort to the Long
3544 (dd
, mode
, ndigits
, decpt
, sign
, rve
)
3545 double dd
; int mode
, ndigits
, *decpt
, *sign
; char **rve
;
3547 (double dd
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
)
3550 /* Arguments ndigits, decpt, sign are similar to those
3551 of ecvt and fcvt; trailing zeros are suppressed from
3552 the returned string. If not null, *rve is set to point
3553 to the end of the return value. If d is +-Infinity or NaN,
3554 then *decpt is set to 9999.
3557 0 ==> shortest string that yields d when read in
3558 and rounded to nearest.
3559 1 ==> like 0, but with Steele & White stopping rule;
3560 e.g. with IEEE P754 arithmetic , mode 0 gives
3561 1e23 whereas mode 1 gives 9.999999999999999e22.
3562 2 ==> max(1,ndigits) significant digits. This gives a
3563 return value similar to that of ecvt, except
3564 that trailing zeros are suppressed.
3565 3 ==> through ndigits past the decimal point. This
3566 gives a return value similar to that from fcvt,
3567 except that trailing zeros are suppressed, and
3568 ndigits can be negative.
3569 4,5 ==> similar to 2 and 3, respectively, but (in
3570 round-nearest mode) with the tests of mode 0 to
3571 possibly return a shorter string that rounds to d.
3572 With IEEE arithmetic and compilation with
3573 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
3574 as modes 2 and 3 when FLT_ROUNDS != 1.
3575 6-9 ==> Debugging modes similar to mode - 4: don't try
3576 fast floating-point estimate (if applicable).
3578 Values of mode other than 0-9 are treated as mode 0.
3580 Sufficient space is allocated to the return value
3581 to hold the suppressed trailing zeros.
3584 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
,
3585 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
3586 spec_case
, try_quick
;
3588 #ifndef Sudden_Underflow
3592 Bigint
*b
, *b1
, *delta
, *mlo
= NULL
, *mhi
, *S
;
3597 int inexact
, oldinexact
;
3599 #ifdef Honor_FLT_ROUNDS /*{*/
3601 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
3602 Rounding
= Flt_Rounds
;
3605 switch(fegetround()) {
3606 case FE_TOWARDZERO
: Rounding
= 0; break;
3607 case FE_UPWARD
: Rounding
= 2; break;
3608 case FE_DOWNWARD
: Rounding
= 3;
3613 #ifndef MULTIPLE_THREADS
3615 freedtoa(dtoa_result
);
3621 if (word0(&u
) & Sign_bit
) {
3622 /* set sign for everything, including 0's and NaNs */
3624 word0(&u
) &= ~Sign_bit
; /* clear sign bit */
3629 #if defined(IEEE_Arith) + defined(VAX)
3631 if ((word0(&u
) & Exp_mask
) == Exp_mask
)
3633 if (word0(&u
) == 0x8000)
3636 /* Infinity or NaN */
3639 if (!word1(&u
) && !(word0(&u
) & 0xfffff))
3640 return nrv_alloc("Infinity", rve
, 8);
3642 return nrv_alloc("NaN", rve
, 3);
3646 dval(&u
) += 0; /* normalize */
3650 return nrv_alloc("0", rve
, 1);
3654 try_quick
= oldinexact
= get_inexact();
3657 #ifdef Honor_FLT_ROUNDS
3658 if (Rounding
>= 2) {
3660 Rounding
= Rounding
== 2 ? 0 : 2;
3667 b
= d2b(&u
, &be
, &bbits
);
3668 i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
));
3669 #ifndef Sudden_Underflow
3672 dval(&d2
) = dval(&u
);
3673 word0(&d2
) &= Frac_mask1
;
3674 word0(&d2
) |= Exp_11
;
3676 if (j
= 11 - hi0bits(word0(&d2
) & Frac_mask
))
3677 dval(&d2
) /= 1 << j
;
3680 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
3681 * log10(x) = log(x) / log(10)
3682 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
3683 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
3685 * This suggests computing an approximation k to log10(d) by
3687 * k = (i - Bias)*0.301029995663981
3688 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
3690 * We want k to be too large rather than too small.
3691 * The error in the first-order Taylor series approximation
3692 * is in our favor, so we just round up the constant enough
3693 * to compensate for any error in the multiplication of
3694 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
3695 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
3696 * adding 1e-13 to the constant term more than suffices.
3697 * Hence we adjust the constant term to 0.1760912590558.
3698 * (We could get a more accurate k by invoking log10,
3699 * but this is probably not worthwhile.)
3707 #ifndef Sudden_Underflow
3711 /* d is denormalized */
3713 i
= bbits
+ be
+ (Bias
+ (P
-1) - 1);
3714 x
= i
> 32 ? word0(&u
) << (64 - i
) | word1(&u
) >> (i
- 32)
3715 : word1(&u
) << (32 - i
);
3717 word0(&d2
) -= 31*Exp_msk1
; /* adjust exponent */
3718 i
-= (Bias
+ (P
-1) - 1) + 1;
3722 ds
= (dval(&d2
)-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
3724 if (ds
< 0. && ds
!= k
)
3725 k
--; /* want k = floor(ds) */
3727 if (k
>= 0 && k
<= Ten_pmax
) {
3728 if (dval(&u
) < tens
[k
])
3751 if (mode
< 0 || mode
> 9)
3755 #ifdef Check_FLT_ROUNDS
3756 try_quick
= Rounding
== 1;
3760 #endif /*SET_INEXACT*/
3767 ilim
= ilim1
= -1; /* Values for cases 0 and 1; done here to */
3768 /* silence erroneous "gcc -Wall" warning. */
3781 ilim
= ilim1
= i
= ndigits
;
3787 i
= ndigits
+ k
+ 1;
3793 s
= s0
= rv_alloc(i
);
3795 #ifdef Honor_FLT_ROUNDS
3796 if (mode
> 1 && Rounding
!= 1)
3800 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
3802 /* Try to get by with floating-point arithmetic. */
3805 dval(&d2
) = dval(&u
);
3808 ieps
= 2; /* conservative */
3813 /* prevent overflows */
3815 dval(&u
) /= bigtens
[n_bigtens
-1];
3818 for(; j
; j
>>= 1, i
++)
3828 dval(&u
) *= tens
[j1
& 0xf];
3829 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
3832 dval(&u
) *= bigtens
[i
];
3836 if (k_check
&& dval(&u
) < 1. && ilim
> 0) {
3844 dval(&eps
) = ieps
*dval(&u
) + 7.;
3845 word0(&eps
) -= (P
-1)*Exp_msk1
;
3849 if (dval(&u
) > dval(&eps
))
3851 if (dval(&u
) < -dval(&eps
))
3855 #ifndef No_leftright
3857 /* Use Steele & White method of only
3858 * generating digits needed.
3860 dval(&eps
) = 0.5/tens
[ilim
-1] - dval(&eps
);
3864 *s
++ = '0' + (char)L
;
3865 if (dval(&u
) < dval(&eps
))
3867 if (1. - dval(&u
) < dval(&eps
))
3877 /* Generate ilim digits, then fix them up. */
3878 dval(&eps
) *= tens
[ilim
-1];
3879 for(i
= 1;; i
++, dval(&u
) *= 10.) {
3880 L
= (Long
)(dval(&u
));
3881 if (!(dval(&u
) -= L
))
3883 *s
++ = '0' + (char)L
;
3885 if (dval(&u
) > 0.5 + dval(&eps
))
3887 else if (dval(&u
) < 0.5 - dval(&eps
)) {
3888 while(*--s
== '0') {}
3895 #ifndef No_leftright
3900 dval(&u
) = dval(&d2
);
3905 /* Do we have a "small" integer? */
3907 if (be
>= 0 && k
<= Int_max
) {
3910 if (ndigits
< 0 && ilim
<= 0) {
3912 if (ilim
< 0 || dval(&u
) <= 5*ds
)
3916 for(i
= 1; i
<= k
+ 1; i
++, dval(&u
) *= 10.) {
3917 L
= (Long
)(dval(&u
) / ds
);
3919 #ifdef Check_FLT_ROUNDS
3920 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
3926 *s
++ = '0' + (char)L
;
3934 #ifdef Honor_FLT_ROUNDS
3938 case 2: goto bump_up
;
3941 dval(&u
) += dval(&u
);
3942 if (dval(&u
) > ds
|| (dval(&u
) == ds
&& L
& 1)) {
3963 #ifndef Sudden_Underflow
3964 denorm
? be
+ (Bias
+ (P
-1) - 1 + 1) :
3967 1 + 4*P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
3975 if (m2
> 0 && s2
> 0) {
3976 i
= m2
< s2
? m2
: s2
;
3984 mhi
= pow5mult(mhi
, m5
);
3994 b
= pow5mult(b
, b5
);
3998 S
= pow5mult(S
, s5
);
4000 /* Check for special case that d is a normalized power of 2. */
4003 if ((mode
< 2 || leftright
)
4004 #ifdef Honor_FLT_ROUNDS
4008 if (!word1(&u
) && !(word0(&u
) & Bndry_mask
)
4009 #ifndef Sudden_Underflow
4010 && word0(&u
) & (Exp_mask
& ~Exp_msk1
)
4013 /* The special case */
4020 /* Arrange for convenient computation of quotients:
4021 * shift left if necessary so divisor has 4 leading 0 bits.
4023 * Perhaps we should just compute leading 28 bits of S once
4024 * and for all and pass them and a shift to quorem, so it
4025 * can do shifts and ors to compute the numerator for q.
4028 i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f;
4033 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0xf)
4048 b
= multadd(b
, 10, 0); /* we botched the k estimate */
4050 mhi
= multadd(mhi
, 10, 0);
4054 if (ilim
<= 0 && (mode
== 3 || mode
== 5)) {
4055 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
4056 /* no digits, fcvt style */
4068 mhi
= lshift(mhi
, m2
);
4070 /* Compute mlo -- check for special case
4071 * that d is a normalized power of 2.
4076 mhi
= Balloc(mhi
->k
);
4078 mhi
= lshift(mhi
, Log2P
);
4082 dig
= quorem(b
,S
) + '0';
4083 /* Do we yet have the shortest decimal string
4084 * that will round to d?
4087 delta
= diff(S
, mhi
);
4088 j1
= delta
->sign
? 1 : cmp(b
, delta
);
4090 #ifndef ROUND_BIASED
4091 if (j1
== 0 && mode
!= 1 && !(word1(&u
) & 1)
4092 #ifdef Honor_FLT_ROUNDS
4101 else if (!b
->x
[0] && b
->wds
<= 1)
4108 if (j
< 0 || (j
== 0 && mode
!= 1
4109 #ifndef ROUND_BIASED
4113 if (!b
->x
[0] && b
->wds
<= 1) {
4119 #ifdef Honor_FLT_ROUNDS
4122 case 0: goto accept_dig
;
4123 case 2: goto keep_dig
;
4125 #endif /*Honor_FLT_ROUNDS*/
4129 if ((j1
> 0 || (j1
== 0 && dig
& 1))
4138 #ifdef Honor_FLT_ROUNDS
4142 if (dig
== '9') { /* possible if i == 1 */
4147 *s
++ = (char)dig
+ 1;
4150 #ifdef Honor_FLT_ROUNDS
4156 b
= multadd(b
, 10, 0);
4158 mlo
= mhi
= multadd(mhi
, 10, 0);
4160 mlo
= multadd(mlo
, 10, 0);
4161 mhi
= multadd(mhi
, 10, 0);
4167 dig
= quorem(b
,S
) + '0';
4169 if (!b
->x
[0] && b
->wds
<= 1) {
4177 b
= multadd(b
, 10, 0);
4180 /* Round off last digit */
4182 #ifdef Honor_FLT_ROUNDS
4184 case 0: goto trimzeros
;
4185 case 2: goto roundoff
;
4190 if (j
> 0 || (j
== 0 && dig
& 1)) {
4201 #ifdef Honor_FLT_ROUNDS
4204 while(*--s
== '0') {}
4210 if (mlo
&& mlo
!= mhi
)
4218 word0(&u
) = Exp_1
+ (70 << Exp_shift
);
4223 else if (!oldinexact
)
4234 } // namespace dmg_fp