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 * This file has been modified to remove dtoa() and all
66 * non-reentrancy. This makes it slower, but it also makes life a lot
67 * easier on Windows and other platforms without static lock
68 * initializers (grumble).
71 /* Added by dhuggins@cs.cmu.edu to use autoconf results. */
72 /* We do not care about the VAX. */
74 #ifdef WORDS_BIGENDIAN
79 #ifndef HAVE_LONG_LONG
82 #define Omit_Private_Memory
83 #include "sphinxbase/ckd_alloc.h"
86 /* Correct totally bogus typedefs in this code. */
87 #include "sphinxbase/prim_type.h"
88 #define Long int32 /* ZOMG */
89 #define ULong uint32 /* WTF */
92 * #define IEEE_8087 for IEEE-arithmetic machines where the least
93 * significant byte has the lowest address.
94 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
95 * significant byte has the lowest address.
96 * #define Long int on machines with 32-bit ints and 64-bit longs.
97 * #define IBM for IBM mainframe-style floating-point arithmetic.
98 * #define VAX for VAX-style floating-point arithmetic (D_floating).
99 * #define No_leftright to omit left-right logic in fast floating-point
100 * computation of dtoa.
101 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
102 * and strtod and dtoa should round accordingly.
103 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
104 * and Honor_FLT_ROUNDS is not #defined.
105 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
106 * that use extended-precision instructions to compute rounded
107 * products and quotients) with IBM.
108 * #define ROUND_BIASED for IEEE-format with biased rounding.
109 * #define Inaccurate_Divide for IEEE-format with correctly rounded
110 * products but inaccurate quotients, e.g., for Intel i860.
111 * #define NO_LONG_LONG on machines that do not have a "long long"
112 * integer type (of >= 64 bits). On such machines, you can
113 * #define Just_16 to store 16 bits per 32-bit Long when doing
114 * high-precision integer arithmetic. Whether this speeds things
115 * up or slows things down depends on the machine and the number
116 * being converted. If long long is available and the name is
117 * something other than "long long", #define Llong to be the name,
118 * and if "unsigned Llong" does not work as an unsigned version of
119 * Llong, #define #ULLong to be the corresponding unsigned type.
120 * #define KR_headers for old-style C function headers.
121 * #define Bad_float_h if your system lacks a float.h or if it does not
122 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
123 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
124 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
125 * if memory is available and otherwise does something you deem
126 * appropriate. If MALLOC is undefined, malloc will be invoked
127 * directly -- and assumed always to succeed.
128 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
129 * memory allocations from a private pool of memory when possible.
130 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
131 * unless #defined to be a different length. This default length
132 * suffices to get rid of MALLOC calls except for unusual cases,
133 * such as decimal-to-binary conversion of a very long string of
134 * digits. The longest string dtoa can return is about 751 bytes
135 * long. For conversions by strtod of strings of 800 digits and
136 * all dtoa conversions in single-threaded executions with 8-byte
137 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
138 * pointers, PRIVATE_MEM >= 7112 appears adequate.
139 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
140 * #defined automatically on IEEE systems. On such systems,
141 * when INFNAN_CHECK is #defined, strtod checks
142 * for Infinity and NaN (case insensitively). On some systems
143 * (e.g., some HP systems), it may be necessary to #define NAN_WORD0
144 * appropriately -- to the most significant word of a quiet NaN.
145 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
146 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
147 * strtod also accepts (case insensitively) strings of the form
148 * NaN(x), where x is a string of hexadecimal digits and spaces;
149 * if there is only one string of hexadecimal digits, it is taken
150 * for the 52 fraction bits of the resulting NaN; if there are two
151 * or more strings of hex digits, the first is for the high 20 bits,
152 * the second and subsequent for the low 32 bits, with intervening
153 * white space ignored; but if this results in none of the 52
154 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
155 * and NAN_WORD1 are used instead.
156 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
157 * multiple threads. In this case, you must provide (or suitably
158 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
159 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
160 * in pow5mult, ensures lazy evaluation of only one copy of high
161 * powers of 5; omitting this lock would introduce a small
162 * probability of wasting memory, but would otherwise be harmless.)
163 * You must also invoke freedtoa(s) to free the value s returned by
164 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
165 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
166 * avoids underflows on inputs whose result does not underflow.
167 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
168 * floating-point numbers and flushes underflows to zero rather
169 * than implementing gradual underflow, then you must also #define
171 * #define YES_ALIAS to permit aliasing certain double values with
172 * arrays of ULongs. This leads to slightly better code with
173 * some compilers and was always used prior to 19990916, but it
174 * is not strictly legal and can cause trouble with aggressively
175 * optimizing compilers (e.g., gcc 2.95.1 under -O2).
176 * #define USE_LOCALE to use the current locale's decimal_point value.
177 * #define SET_INEXACT if IEEE arithmetic is being used and extra
178 * computation should be done to set the inexact flag when the
179 * result is inexact and avoid setting inexact when the result
180 * is exact. In this case, dtoa.c must be compiled in
181 * an environment, perhaps provided by #include "dtoa.c" in a
182 * suitable wrapper, that defines two functions,
183 * int get_inexact(void);
184 * void clear_inexact(void);
185 * such that get_inexact() returns a nonzero value if the
186 * inexact bit is already set, and clear_inexact() sets the
187 * inexact bit to 0. When SET_INEXACT is #defined, strtod
188 * also does extra computations to set the underflow and overflow
189 * flags when appropriate (i.e., when the result is tiny and
190 * inexact or when it is a numeric value rounded to +-infinity).
191 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
192 * the result overflows to +-Infinity or underflows to 0.
199 typedef unsigned Long ULong;
204 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
214 /* Private memory and other non-reentrant stuff removed. */
217 #undef Avoid_Underflow
226 #ifndef NO_INFNAN_CHECK
240 #define DBL_MAX_10_EXP 308
241 #define DBL_MAX_EXP 1024
243 #endif /*IEEE_Arith*/
247 #define DBL_MAX_10_EXP 75
248 #define DBL_MAX_EXP 63
250 #define DBL_MAX 7.2370055773322621e+75
255 #define DBL_MAX_10_EXP 38
256 #define DBL_MAX_EXP 127
258 #define DBL_MAX 1.7014118346046923e+38
262 #define LONG_MAX 2147483647
265 #else /* ifndef Bad_float_h */
267 #endif /* Bad_float_h */
279 #define CONST /* blank */
286 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
287 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
290 /** Union to extract the bytes of a double */
291 typedef union { double d; ULong L[2]; } U;
296 #define word0(x) ((ULong *)&x)[1]
297 #define word1(x) ((ULong *)&x)[0]
299 #define word0(x) ((ULong *)&x)[0]
300 #define word1(x) ((ULong *)&x)[1]
304 #define word0(x) ((U*)&x)->L[1]
305 #define word1(x) ((U*)&x)->L[0]
307 #define word0(x) ((U*)&x)->L[0]
308 #define word1(x) ((U*)&x)->L[1]
310 #define dval(x) ((U*)&x)->d
313 /* The following definition of Storeinc is appropriate for MIPS processors.
314 * An alternative that might be better on some machines is
315 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
317 #if defined(IEEE_8087) + defined(VAX)
318 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
319 ((unsigned short *)a)[0] = (unsigned short)c, a++)
321 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
322 ((unsigned short *)a)[1] = (unsigned short)c, a++)
325 /* #define P DBL_MANT_DIG */
326 /* Ten_pmax = floor(P*log(2)/log(5)) */
327 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
328 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
329 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
333 #define Exp_shift1 20
334 #define Exp_msk1 0x100000
335 #define Exp_msk11 0x100000
336 #define Exp_mask 0x7ff00000
340 #define Exp_1 0x3ff00000
341 #define Exp_11 0x3ff00000
343 #define Frac_mask 0xfffff
344 #define Frac_mask1 0xfffff
347 #define Bndry_mask 0xfffff
348 #define Bndry_mask1 0xfffff
350 #define Sign_bit 0x80000000
356 #ifndef NO_IEEE_Scale
357 #define Avoid_Underflow
358 #ifdef Flush_Denorm /* debugging option */
359 #undef Sudden_Underflow
365 #define Flt_Rounds FLT_ROUNDS
369 #endif /*Flt_Rounds*/
371 #ifdef Honor_FLT_ROUNDS
372 #define Rounding rounding
373 #undef Check_FLT_ROUNDS
374 #define Check_FLT_ROUNDS
376 #define Rounding Flt_Rounds
379 #else /* ifndef IEEE_Arith */
380 #undef Check_FLT_ROUNDS
381 #undef Honor_FLT_ROUNDS
383 #undef Sudden_Underflow
384 #define Sudden_Underflow
389 #define Exp_shift1 24
390 #define Exp_msk1 0x1000000
391 #define Exp_msk11 0x1000000
392 #define Exp_mask 0x7f000000
395 #define Exp_1 0x41000000
396 #define Exp_11 0x41000000
397 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
398 #define Frac_mask 0xffffff
399 #define Frac_mask1 0xffffff
402 #define Bndry_mask 0xefffff
403 #define Bndry_mask1 0xffffff
405 #define Sign_bit 0x80000000
407 #define Tiny0 0x100000
416 #define Exp_msk1 0x80
417 #define Exp_msk11 0x800000
418 #define Exp_mask 0x7f80
421 #define Exp_1 0x40800000
422 #define Exp_11 0x4080
424 #define Frac_mask 0x7fffff
425 #define Frac_mask1 0xffff007f
428 #define Bndry_mask 0xffff007f
429 #define Bndry_mask1 0xffff007f
431 #define Sign_bit 0x8000
437 #endif /* IBM, VAX */
438 #endif /* IEEE_Arith */
445 #define rounded_product(a,b) a = rnd_prod(a, b)
446 #define rounded_quotient(a,b) a = rnd_quot(a, b)
448 extern double rnd_prod(), rnd_quot();
450 extern double rnd_prod(double, double), rnd_quot(double, double);
453 #define rounded_product(a,b) a *= b
454 #define rounded_quotient(a,b) a /= b
457 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
458 #define Big1 0xffffffff
465 #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
467 #define FFFFFFFF 0xffffffffUL
474 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
475 * This makes some inner loops simpler and sometimes saves work
476 * during multiplications, but it often seems to make things slightly
477 * slower. Hence the default is now to store 32 bits per Long.
480 #else /* long long available */
482 #define Llong long long
485 #define ULLong unsigned Llong
487 #endif /* NO_LONG_LONG */
489 #ifndef MULTIPLE_THREADS
490 #define ACQUIRE_DTOA_LOCK(n) /*nothing*/
491 #define FREE_DTOA_LOCK(n) /*nothing*/
497 extern "C" double sb_strtod(const char *s00, char **se);
503 int k, maxwds, sign, wds;
507 typedef struct Bigint Bigint;
522 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
524 rv = ckd_malloc(len*sizeof(double));
527 rv->sign = rv->wds = 0;
542 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
543 y->wds*sizeof(Long) + 2*sizeof(int))
548 (b, m, a) Bigint *b; int m, a;
550 (Bigint *b, int m, int a) /* multiply by m and add a */
571 y = *x * (ULLong)m + carry;
577 y = (xi & 0xffff) * m + carry;
578 z = (xi >> 16) * m + (y >> 16);
580 *x++ = (z << 16) + (y & 0xffff);
590 if (wds >= b->maxwds) {
605 (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
607 (CONST char *s, int nd0, int nd, ULong y9)
615 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
622 b->x[0] = y9 & 0xffff;
623 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
629 do b = multadd(b, 10, *s++ - '0');
636 b = multadd(b, 10, *s++ - '0');
643 (x) register ULong x;
650 if (!(x & 0xffff0000)) {
654 if (!(x & 0xff000000)) {
658 if (!(x & 0xf0000000)) {
662 if (!(x & 0xc0000000)) {
666 if (!(x & 0x80000000)) {
668 if (!(x & 0x40000000))
683 register ULong x = *y;
741 (a, b) Bigint *a, *b;
743 (Bigint *a, Bigint *b)
748 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
759 if (a->wds < b->wds) {
771 for(x = c->x, xa = x + wc; x < xa; x++)
779 for(; xb < xbe; xc0++) {
785 z = *x++ * (ULLong)y + *xc + carry;
787 *xc++ = z & FFFFFFFF;
795 for(; xb < xbe; xb++, xc0++) {
796 if (y = *xb & 0xffff) {
801 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
803 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
816 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
819 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
827 for(; xb < xbe; xc0++) {
833 z = *x++ * y + *xc + carry;
843 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
851 (b, k) Bigint *b; int k;
856 Bigint *b1, *p5, *p51;
858 static int CONST p05[3] = { 5, 25, 125 };
861 b = multadd(b, p05[i-1], 0);
886 (b, k) Bigint *b; int k;
893 ULong *x, *x1, *xe, z;
902 for(i = b->maxwds; n1 > i; i <<= 1)
906 for(i = 0; i < n; i++)
927 *x1++ = *x << k & 0xffff | z;
946 (a, b) Bigint *a, *b;
948 (Bigint *a, Bigint *b)
951 ULong *xa, *xa0, *xb, *xb0;
957 if (i > 1 && !a->x[i-1])
958 Bug("cmp called with a->x[a->wds-1] == 0");
959 if (j > 1 && !b->x[j-1])
960 Bug("cmp called with b->x[b->wds-1] == 0");
970 return *xa < *xb ? -1 : 1;
980 (a, b) Bigint *a, *b;
982 (Bigint *a, Bigint *b)
987 ULong *xa, *xae, *xb, *xbe, *xc;
1024 y = (ULLong)*xa++ - *xb++ - borrow;
1025 borrow = y >> 32 & (ULong)1;
1026 *xc++ = y & FFFFFFFF;
1031 borrow = y >> 32 & (ULong)1;
1032 *xc++ = y & FFFFFFFF;
1037 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1038 borrow = (y & 0x10000) >> 16;
1039 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1040 borrow = (z & 0x10000) >> 16;
1045 y = (*xa & 0xffff) - borrow;
1046 borrow = (y & 0x10000) >> 16;
1047 z = (*xa++ >> 16) - borrow;
1048 borrow = (z & 0x10000) >> 16;
1053 y = *xa++ - *xb++ - borrow;
1054 borrow = (y & 0x10000) >> 16;
1060 borrow = (y & 0x10000) >> 16;
1082 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1083 #ifndef Avoid_Underflow
1084 #ifndef Sudden_Underflow
1093 #ifndef Avoid_Underflow
1094 #ifndef Sudden_Underflow
1097 L = -L >> Exp_shift;
1098 if (L < Exp_shift) {
1099 word0(a) = 0x80000 >> L;
1105 word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1116 (a, e) Bigint *a; int *e;
1121 ULong *xa, *xa0, w, y, z;
1135 if (!y) Bug("zero y in b2d");
1141 d0 = Exp_1 | y >> (Ebits - k);
1142 w = xa > xa0 ? *--xa : 0;
1143 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1146 z = xa > xa0 ? *--xa : 0;
1148 d0 = Exp_1 | y << k | z >> (32 - k);
1149 y = xa > xa0 ? *--xa : 0;
1150 d1 = z << k | y >> (32 - k);
1157 if (k < Ebits + 16) {
1158 z = xa > xa0 ? *--xa : 0;
1159 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1160 w = xa > xa0 ? *--xa : 0;
1161 y = xa > xa0 ? *--xa : 0;
1162 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1165 z = xa > xa0 ? *--xa : 0;
1166 w = xa > xa0 ? *--xa : 0;
1168 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1169 y = xa > xa0 ? *--xa : 0;
1170 d1 = w << k + 16 | y << k;
1174 word0(d) = d0 >> 16 | d0 << 16;
1175 word1(d) = d1 >> 16 | d1 << 16;
1186 (d, e, bits) double d; int *e, *bits;
1188 (double _d, int *e, int *bits)
1195 #ifndef Sudden_Underflow
1200 d0 = word0(d) >> 16 | word0(d) << 16;
1201 d1 = word1(d) >> 16 | word1(d) << 16;
1216 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1217 #ifdef Sudden_Underflow
1218 de = (int)(d0 >> Exp_shift);
1223 if ((de = (int)(d0 >> Exp_shift)))
1228 if ((k = lo0bits(&y))) {
1229 x[0] = y | z << (32 - k);
1234 #ifndef Sudden_Underflow
1237 b->wds = (x[1] = z) ? 2 : 1;
1242 Bug("Zero passed to d2b");
1246 #ifndef Sudden_Underflow
1254 if (k = lo0bits(&y))
1256 x[0] = y | z << 32 - k & 0xffff;
1257 x[1] = z >> k - 16 & 0xffff;
1263 x[1] = y >> 16 | z << 16 - k & 0xffff;
1264 x[2] = z >> k & 0xffff;
1279 Bug("Zero passed to d2b");
1297 #ifndef Sudden_Underflow
1301 *e = (de - Bias - (P-1) << 2) + k;
1302 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1304 *e = de - Bias - (P-1) + k;
1307 #ifndef Sudden_Underflow
1310 *e = de - Bias - (P-1) + 1 + k;
1312 *bits = 32*i - hi0bits(x[i-1]);
1314 *bits = (i+2)*16 - hi0bits(x[i]);
1326 (a, b) Bigint *a, *b;
1328 (Bigint *a, Bigint *b)
1334 dval(da) = b2d(a, &ka);
1335 dval(db) = b2d(b, &kb);
1337 k = ka - kb + 32*(a->wds - b->wds);
1339 k = ka - kb + 16*(a->wds - b->wds);
1343 word0(da) += (k >> 2)*Exp_msk1;
1349 word0(db) += (k >> 2)*Exp_msk1;
1355 word0(da) += k*Exp_msk1;
1358 word0(db) += k*Exp_msk1;
1361 return dval(da) / dval(db);
1366 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1367 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1376 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1377 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1378 #ifdef Avoid_Underflow
1379 9007199254740992.*9007199254740992.e-256
1380 /* = 2^106 * 1e-53 */
1385 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1386 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1387 #define Scale_Bit 0x10
1391 bigtens[] = { 1e16, 1e32, 1e64 };
1392 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1395 bigtens[] = { 1e16, 1e32 };
1396 static CONST double tinytens[] = { 1e-16, 1e-32 };
1404 #define NAN_WORD0 0x7ff80000
1414 (sp, t) char **sp, *t;
1416 (CONST char **sp, char *t)
1420 CONST char *s = *sp;
1423 if ((c = *++s) >= 'A' && c <= 'Z')
1436 (rvp, sp) double *rvp; CONST char **sp;
1438 (U *rvp, CONST char **sp)
1443 int havedig, udx0, xshift;
1446 havedig = xshift = 0;
1449 /* allow optional initial 0x or 0X */
1450 while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
1452 if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
1454 while((c = *(CONST unsigned char*)++s)) {
1455 if (c >= '0' && c <= '9')
1457 else if (c >= 'a' && c <= 'f')
1459 else if (c >= 'A' && c <= 'F')
1461 else if (c <= ' ') {
1462 if (udx0 && havedig) {
1468 #ifdef GDTOA_NON_PEDANTIC_NANCHECK
1469 else if (/*(*/ c == ')' && havedig) {
1474 return; /* invalid form: don't change *sp */
1478 if (/*(*/ c == ')') {
1482 } while((c = *++s));
1493 x[0] = (x[0] << 4) | (x[1] >> 28);
1494 x[1] = (x[1] << 4) | c;
1496 if ((x[0] &= 0xfffff) || x[1]) {
1497 word0(*rvp) = Exp_mask | x[0];
1501 #endif /*No_Hex_NaN*/
1502 #endif /* INFNAN_CHECK */
1507 (s00, se) CONST char *s00; char **se;
1509 (CONST char *s00, char **se)
1512 #ifdef Avoid_Underflow
1515 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1516 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1517 CONST char *s, *s0, *s1;
1522 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1524 int inexact, oldinexact;
1526 #ifdef Honor_FLT_ROUNDS
1533 sign = nz0 = nz = 0;
1535 for(s = s00;;s++) switch(*s) {
1558 while(*++s == '0') ;
1564 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1571 s1 = localeconv()->decimal_point;
1592 for(; c == '0'; c = *++s)
1594 if (c > '0' && c <= '9') {
1602 for(; c >= '0' && c <= '9'; c = *++s) {
1607 for(i = 1; i < nz; i++)
1610 else if (nd <= DBL_DIG + 1)
1614 else if (nd <= DBL_DIG + 1)
1622 if (c == 'e' || c == 'E') {
1623 if (!nd && !nz && !nz0) {
1634 if (c >= '0' && c <= '9') {
1637 if (c > '0' && c <= '9') {
1640 while((c = *++s) >= '0' && c <= '9')
1642 if (s - s1 > 8 || L > 19999)
1643 /* Avoid confusion from exponents
1644 * so large that e might overflow.
1646 e = 19999; /* safe for 16 bit ints */
1661 /* Check for Nan and Infinity */
1665 if (match(&s,"nf")) {
1667 if (!match(&s,"inity"))
1669 word0(rv) = 0x7ff00000;
1676 if (match(&s, "an")) {
1677 word0(rv) = NAN_WORD0;
1678 word1(rv) = NAN_WORD1;
1680 if (*s == '(') /*)*/
1686 #endif /* INFNAN_CHECK */
1695 /* Now we have nd0 digits, starting at s0, followed by a
1696 * decimal point, followed by nd-nd0 digits. The number we're
1697 * after is the integer represented by those digits times
1702 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1707 oldinexact = get_inexact();
1709 dval(rv) = tens[k - 9] * dval(rv) + z;
1713 #ifndef RND_PRODQUOT
1714 #ifndef Honor_FLT_ROUNDS
1722 if (e <= Ten_pmax) {
1724 goto vax_ovfl_check;
1726 #ifdef Honor_FLT_ROUNDS
1727 /* round correctly FLT_ROUNDS = 2 or 3 */
1733 /* rv = */ rounded_product(dval(rv), tens[e]);
1738 if (e <= Ten_pmax + i) {
1739 /* A fancier test would sometimes let us do
1740 * this for larger i values.
1742 #ifdef Honor_FLT_ROUNDS
1743 /* round correctly FLT_ROUNDS = 2 or 3 */
1750 dval(rv) *= tens[i];
1752 /* VAX exponent range is so narrow we must
1753 * worry about overflow here...
1756 word0(rv) -= P*Exp_msk1;
1757 /* rv = */ rounded_product(dval(rv), tens[e]);
1758 if ((word0(rv) & Exp_mask)
1759 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1761 word0(rv) += P*Exp_msk1;
1763 /* rv = */ rounded_product(dval(rv), tens[e]);
1768 #ifndef Inaccurate_Divide
1769 else if (e >= -Ten_pmax) {
1770 #ifdef Honor_FLT_ROUNDS
1771 /* round correctly FLT_ROUNDS = 2 or 3 */
1777 /* rv = */ rounded_quotient(dval(rv), tens[-e]);
1788 oldinexact = get_inexact();
1790 #ifdef Avoid_Underflow
1793 #ifdef Honor_FLT_ROUNDS
1794 if ((rounding = Flt_Rounds) >= 2) {
1796 rounding = rounding == 2 ? 0 : 2;
1802 #endif /*IEEE_Arith*/
1804 /* Get starting approximation = rv * 10**e1 */
1808 dval(rv) *= tens[i];
1810 if (e1 > DBL_MAX_10_EXP) {
1815 /* Can't trust HUGE_VAL */
1817 #ifdef Honor_FLT_ROUNDS
1819 case 0: /* toward 0 */
1820 case 3: /* toward -infinity */
1825 word0(rv) = Exp_mask;
1828 #else /*Honor_FLT_ROUNDS*/
1829 word0(rv) = Exp_mask;
1831 #endif /*Honor_FLT_ROUNDS*/
1833 /* set overflow bit */
1835 dval(rv0) *= dval(rv0);
1837 #else /*IEEE_Arith*/
1840 #endif /*IEEE_Arith*/
1846 for(j = 0; e1 > 1; j++, e1 >>= 1)
1848 dval(rv) *= bigtens[j];
1849 /* The last multiplication could overflow. */
1850 word0(rv) -= P*Exp_msk1;
1851 dval(rv) *= bigtens[j];
1852 if ((z = word0(rv) & Exp_mask)
1853 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1855 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1856 /* set to largest number */
1857 /* (Can't trust DBL_MAX) */
1862 word0(rv) += P*Exp_msk1;
1868 dval(rv) /= tens[i];
1870 if (e1 >= 1 << n_bigtens)
1872 #ifdef Avoid_Underflow
1875 for(j = 0; e1 > 0; j++, e1 >>= 1)
1877 dval(rv) *= tinytens[j];
1878 if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
1879 >> Exp_shift)) > 0) {
1880 /* scaled rv is denormal; zap j low bits */
1884 word0(rv) = (P+2)*Exp_msk1;
1886 word0(rv) &= 0xffffffff << (j-32);
1889 word1(rv) &= 0xffffffff << j;
1892 for(j = 0; e1 > 1; j++, e1 >>= 1)
1894 dval(rv) *= tinytens[j];
1895 /* The last multiplication could underflow. */
1896 dval(rv0) = dval(rv);
1897 dval(rv) *= tinytens[j];
1899 dval(rv) = 2.*dval(rv0);
1900 dval(rv) *= tinytens[j];
1912 #ifndef Avoid_Underflow
1915 /* The refinement below will clean
1916 * this approximation up.
1923 /* Now the hard part -- adjusting rv to the correct value.*/
1925 /* Put digits into bd: true value = bd * 10^e */
1927 bd0 = s2b(s0, nd0, nd, y);
1930 bd = Balloc(bd0->k);
1932 bb = d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1948 #ifdef Honor_FLT_ROUNDS
1952 #ifdef Avoid_Underflow
1954 i = j + bbbits - 1; /* logb(rv) */
1955 if (i < Emin) /* denormal */
1959 #else /*Avoid_Underflow*/
1960 #ifdef Sudden_Underflow
1962 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1966 #else /*Sudden_Underflow*/
1968 i = j + bbbits - 1; /* logb(rv) */
1969 if (i < Emin) /* denormal */
1973 #endif /*Sudden_Underflow*/
1974 #endif /*Avoid_Underflow*/
1977 #ifdef Avoid_Underflow
1980 i = bb2 < bd2 ? bb2 : bd2;
1989 bs = pow5mult(bs, bb5);
1995 bb = lshift(bb, bb2);
1997 bd = pow5mult(bd, bd5);
1999 bd = lshift(bd, bd2);
2001 bs = lshift(bs, bs2);
2002 delta = diff(bb, bd);
2003 dsign = delta->sign;
2006 #ifdef Honor_FLT_ROUNDS
2007 if (rounding != 1) {
2009 /* Error is less than an ulp */
2010 if (!delta->x[0] && delta->wds <= 1) {
2026 && !(word0(rv) & Frac_mask)) {
2027 y = word0(rv) & Exp_mask;
2028 #ifdef Avoid_Underflow
2029 if (!scale || y > 2*P*Exp_msk1)
2034 delta = lshift(delta,Log2P);
2035 if (cmp(delta, bs) <= 0)
2040 #ifdef Avoid_Underflow
2041 if (scale && (y = word0(rv) & Exp_mask)
2043 word0(adj) += (2*P+1)*Exp_msk1 - y;
2045 #ifdef Sudden_Underflow
2046 if ((word0(rv) & Exp_mask) <=
2048 word0(rv) += P*Exp_msk1;
2049 dval(rv) += adj*ulp(dval(rv));
2050 word0(rv) -= P*Exp_msk1;
2053 #endif /*Sudden_Underflow*/
2054 #endif /*Avoid_Underflow*/
2055 dval(rv) += adj*ulp(dval(rv));
2059 adj = ratio(delta, bs);
2062 if (adj <= 0x7ffffffe) {
2063 /* adj = rounding ? ceil(adj) : floor(adj); */
2066 if (!((rounding>>1) ^ dsign))
2071 #ifdef Avoid_Underflow
2072 if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2073 word0(adj) += (2*P+1)*Exp_msk1 - y;
2075 #ifdef Sudden_Underflow
2076 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2077 word0(rv) += P*Exp_msk1;
2078 adj *= ulp(dval(rv));
2083 word0(rv) -= P*Exp_msk1;
2086 #endif /*Sudden_Underflow*/
2087 #endif /*Avoid_Underflow*/
2088 adj *= ulp(dval(rv));
2095 #endif /*Honor_FLT_ROUNDS*/
2098 /* Error is less than half an ulp -- check for
2099 * special case of mantissa a power of two.
2101 if (dsign || word1(rv) || word0(rv) & Bndry_mask
2103 #ifdef Avoid_Underflow
2104 || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2106 || (word0(rv) & Exp_mask) <= Exp_msk1
2111 if (!delta->x[0] && delta->wds <= 1)
2116 if (!delta->x[0] && delta->wds <= 1) {
2123 delta = lshift(delta,Log2P);
2124 if (cmp(delta, bs) > 0)
2129 /* exactly half-way between */
2131 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2133 #ifdef Avoid_Underflow
2134 (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2135 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2138 /*boundary case -- increment exponent*/
2139 word0(rv) = (word0(rv) & Exp_mask)
2146 #ifdef Avoid_Underflow
2152 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2154 /* boundary case -- decrement exponent */
2155 #ifdef Sudden_Underflow /*{{*/
2156 L = word0(rv) & Exp_mask;
2160 #ifdef Avoid_Underflow
2161 if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2164 #endif /*Avoid_Underflow*/
2168 #else /*Sudden_Underflow}{*/
2169 #ifdef Avoid_Underflow
2171 L = word0(rv) & Exp_mask;
2172 if (L <= (2*P+1)*Exp_msk1) {
2173 if (L > (P+2)*Exp_msk1)
2174 /* round even ==> */
2177 /* rv = smallest denormal */
2181 #endif /*Avoid_Underflow*/
2182 L = (word0(rv) & Exp_mask) - Exp_msk1;
2183 #endif /*Sudden_Underflow}}*/
2184 word0(rv) = L | Bndry_mask1;
2185 word1(rv) = 0xffffffff;
2192 #ifndef ROUND_BIASED
2193 if (!(word1(rv) & LSB))
2197 dval(rv) += ulp(dval(rv));
2198 #ifndef ROUND_BIASED
2200 dval(rv) -= ulp(dval(rv));
2201 #ifndef Sudden_Underflow
2206 #ifdef Avoid_Underflow
2212 if ((aadj = ratio(delta, bs)) <= 2.) {
2214 aadj = dval(aadj1) = 1.;
2215 else if (word1(rv) || word0(rv) & Bndry_mask) {
2216 #ifndef Sudden_Underflow
2217 if (word1(rv) == Tiny1 && !word0(rv))
2224 /* special case -- power of FLT_RADIX to be */
2225 /* rounded down... */
2227 if (aadj < 2./FLT_RADIX)
2228 aadj = 1./FLT_RADIX;
2231 dval(aadj1) = -aadj;
2236 dval(aadj1) = dsign ? aadj : -aadj;
2237 #ifdef Check_FLT_ROUNDS
2239 case 2: /* towards +infinity */
2242 case 0: /* towards 0 */
2243 case 3: /* towards -infinity */
2247 if (Flt_Rounds == 0)
2249 #endif /*Check_FLT_ROUNDS*/
2251 y = word0(rv) & Exp_mask;
2253 /* Check for overflow */
2255 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2256 dval(rv0) = dval(rv);
2257 word0(rv) -= P*Exp_msk1;
2258 adj = dval(aadj1) * ulp(dval(rv));
2260 if ((word0(rv) & Exp_mask) >=
2261 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2262 if (word0(rv0) == Big0 && word1(rv0) == Big1)
2269 word0(rv) += P*Exp_msk1;
2272 #ifdef Avoid_Underflow
2273 if (scale && y <= 2*P*Exp_msk1) {
2274 if (aadj <= 0x7fffffff) {
2275 if ((z = (uint32)aadj) <= 0)
2278 dval(aadj1) = dsign ? aadj : -aadj;
2280 word0(aadj1) += (2*P+1)*Exp_msk1 - y;
2282 adj = dval(aadj1) * ulp(dval(rv));
2285 #ifdef Sudden_Underflow
2286 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2287 dval(rv0) = dval(rv);
2288 word0(rv) += P*Exp_msk1;
2289 adj = aadj1 * ulp(dval(rv));
2292 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
2294 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2297 if (word0(rv0) == Tiny0
2298 && word1(rv0) == Tiny1)
2305 word0(rv) -= P*Exp_msk1;
2308 adj = aadj1 * ulp(dval(rv));
2311 #else /*Sudden_Underflow*/
2312 /* Compute adj so that the IEEE rounding rules will
2313 * correctly round rv + adj in some half-way cases.
2314 * If rv * ulp(rv) is denormalized (i.e.,
2315 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2316 * trouble from bits lost to denormalization;
2317 * example: 1.2e-307 .
2319 if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
2320 aadj1 = (double)(int)(aadj + 0.5);
2324 adj = aadj1 * ulp(dval(rv));
2326 #endif /*Sudden_Underflow*/
2327 #endif /*Avoid_Underflow*/
2329 z = word0(rv) & Exp_mask;
2331 #ifdef Avoid_Underflow
2335 /* Can we stop now? */
2338 /* The tolerances below are conservative. */
2339 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2340 if (aadj < .4999999 || aadj > .5000001)
2343 else if (aadj < .4999999/FLT_RADIX)
2356 word0(rv0) = Exp_1 + (70 << Exp_shift);
2361 else if (!oldinexact)
2364 #ifdef Avoid_Underflow
2366 word0(rv0) = Exp_1 - 2*P*Exp_msk1;
2368 dval(rv) *= dval(rv0);
2370 /* try to avoid the bug of testing an 8087 register value */
2371 if (word0(rv) == 0 && word1(rv) == 0)
2375 #endif /* Avoid_Underflow */
2377 if (inexact && !(word0(rv) & Exp_mask)) {
2378 /* set underflow bit */
2380 dval(rv0) *= dval(rv0);
2392 return sign ? -dval(rv) : dval(rv);