190 typedef unsigned Long ULong;
195 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
206 extern void *MALLOC(
size_t);
208 #define MALLOC malloc
211 #ifndef Omit_Private_Memory
213 #define PRIVATE_MEM 2304
215 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
216 static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
220 #undef Avoid_Underflow
234 #define DBL_MAX_10_EXP 308
235 #define DBL_MAX_EXP 1024
241 #define DBL_MAX_10_EXP 75
242 #define DBL_MAX_EXP 63
244 #define DBL_MAX 7.2370055773322621e+75
249 #define DBL_MAX_10_EXP 38
250 #define DBL_MAX_EXP 127
252 #define DBL_MAX 1.7014118346046923e+38
256 #define LONG_MAX 2147483647
267 #define strtod kjs_strtod
268 #define dtoa kjs_dtoa
269 #define freedtoa kjs_freedtoa
279 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
280 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
283 typedef union {
double d; ULong L[2]; } U;
285 #define dval(x) (x).d
287 #define word0(x) (x).L[1]
288 #define word1(x) (x).L[0]
290 #define word0(x) (x).L[0]
291 #define word1(x) (x).L[1]
297 #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
307 #define Exp_shift1 20
308 #define Exp_msk1 0x100000
309 #define Exp_msk11 0x100000
310 #define Exp_mask 0x7ff00000
314 #define Exp_1 0x3ff00000
315 #define Exp_11 0x3ff00000
317 #define Frac_mask 0xfffff
318 #define Frac_mask1 0xfffff
321 #define Bndry_mask 0xfffff
322 #define Bndry_mask1 0xfffff
324 #define Sign_bit 0x80000000
330 #ifndef NO_IEEE_Scale
331 #define Avoid_Underflow
333 #undef Sudden_Underflow
339 #define Flt_Rounds FLT_ROUNDS
345 #ifdef Honor_FLT_ROUNDS
346 #define Rounding rounding
347 #undef Check_FLT_ROUNDS
348 #define Check_FLT_ROUNDS
350 #define Rounding Flt_Rounds
354 #undef Check_FLT_ROUNDS
355 #undef Honor_FLT_ROUNDS
357 #undef Sudden_Underflow
358 #define Sudden_Underflow
363 #define Exp_shift1 24
364 #define Exp_msk1 0x1000000
365 #define Exp_msk11 0x1000000
366 #define Exp_mask 0x7f000000
369 #define Exp_1 0x41000000
370 #define Exp_11 0x41000000
372 #define Frac_mask 0xffffff
373 #define Frac_mask1 0xffffff
376 #define Bndry_mask 0xefffff
377 #define Bndry_mask1 0xffffff
379 #define Sign_bit 0x80000000
381 #define Tiny0 0x100000
390 #define Exp_msk1 0x80
391 #define Exp_msk11 0x800000
392 #define Exp_mask 0x7f80
395 #define Exp_1 0x40800000
396 #define Exp_11 0x4080
398 #define Frac_mask 0x7fffff
399 #define Frac_mask1 0xffff007f
402 #define Bndry_mask 0xffff007f
403 #define Bndry_mask1 0xffff007f
405 #define Sign_bit 0x8000
419 #define rounded_product(a,b) a = rnd_prod(a, b)
420 #define rounded_quotient(a,b) a = rnd_quot(a, b)
421 extern double rnd_prod(
double,
double), rnd_quot(
double,
double);
423 #define rounded_product(a,b) a *= b
424 #define rounded_quotient(a,b) a /= b
427 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
428 #define Big1 0xffffffff
434 #define FFFFFFFF 0xffffffffUL
448 #define Llong long long
451 #define ULLong unsigned Llong
455 #ifndef MULTIPLE_THREADS
456 #define ACQUIRE_DTOA_LOCK(n)
457 #define FREE_DTOA_LOCK(n)
460 #define Kmax (sizeof(size_t) << 3)
465 int k, maxwds, sign, wds;
469 typedef struct Bigint Bigint;
471 static Bigint *freelist[Kmax+1];
479 #ifndef Omit_Private_Memory
483 ACQUIRE_DTOA_LOCK(0);
484 if ((rv = freelist[k])) {
485 freelist[k] = rv->next;
489 #ifdef Omit_Private_Memory
490 rv = (Bigint *)MALLOC(
sizeof(Bigint) + (x-1)*
sizeof(ULong));
492 len = (
sizeof(Bigint) + (x-1)*
sizeof(ULong) +
sizeof(
double) - 1)
494 if (pmem_next - private_mem + len <= (
unsigned)PRIVATE_mem) {
495 rv = (Bigint*)pmem_next;
499 rv = (Bigint*)MALLOC(len*
sizeof(
double));
505 rv->sign = rv->wds = 0;
514 ACQUIRE_DTOA_LOCK(0);
515 v->next = freelist[v->k];
521 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
522 y->wds*sizeof(Long) + 2*sizeof(int))
526 (Bigint *b,
int m,
int a)
546 y = *x * (ULLong)m + carry;
548 *x++ = (ULong)y & FFFFFFFF;
552 y = (xi & 0xffff) * m + carry;
553 z = (xi >> 16) * m + (y >> 16);
555 *x++ = (z << 16) + (y & 0xffff);
565 if (wds >= b->maxwds) {
571 b->x[wds++] = (ULong)carry;
579 (CONST
char *s,
int nd0,
int nd, ULong y9)
586 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
593 b->x[0] = y9 & 0xffff;
594 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
600 do b = multadd(b, 10, *s++ -
'0');
607 b = multadd(b, 10, *s++ -
'0');
617 if (!(x & 0xffff0000)) {
621 if (!(x & 0xff000000)) {
625 if (!(x & 0xf0000000)) {
629 if (!(x & 0xc0000000)) {
633 if (!(x & 0x80000000)) {
635 if (!(x & 0x40000000))
699 (Bigint *a, Bigint *b)
703 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
714 if (a->wds < b->wds) {
726 for(x = c->x, xa = x + wc; x < xa; x++)
734 for(; xb < xbe; xc0++) {
740 z = *x++ * (ULLong)y + *xc + carry;
742 *xc++ = (ULong)z & FFFFFFFF;
750 for(; xb < xbe; xb++, xc0++) {
751 if (y = *xb & 0xffff) {
756 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
758 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
771 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
774 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
782 for(; xb < xbe; xc0++) {
788 z = *x++ * y + *xc + carry;
798 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
809 Bigint *b1, *p5, *p51;
811 static int p05[3] = { 5, 25, 125 };
814 b = multadd(b, p05[i-1], 0);
820 #ifdef MULTIPLE_THREADS
821 ACQUIRE_DTOA_LOCK(1);
840 if (!(p51 = p5->next)) {
841 #ifdef MULTIPLE_THREADS
842 ACQUIRE_DTOA_LOCK(1);
843 if (!(p51 = p5->next)) {
844 p51 = p5->next = mult(p5,p5);
849 p51 = p5->next = mult(p5,p5);
864 ULong *x, *x1, *xe, z;
873 for(i = b->maxwds; n1 > i; i <<= 1)
877 for(i = 0; i < n; i++)
898 *x1++ = *x << k & 0xffff | z;
916 (Bigint *a, Bigint *b)
918 ULong *xa, *xa0, *xb, *xb0;
924 if (i > 1 && !a->x[i-1])
925 Bug(
"cmp called with a->x[a->wds-1] == 0");
926 if (j > 1 && !b->x[j-1])
927 Bug(
"cmp called with b->x[b->wds-1] == 0");
937 return *xa < *xb ? -1 : 1;
946 (Bigint *a, Bigint *b)
950 ULong *xa, *xae, *xb, *xbe, *xc;
987 y = (ULLong)*xa++ - *xb++ - borrow;
988 borrow = y >> 32 & (ULong)1;
989 *xc++ = (ULong)y & FFFFFFFF;
994 borrow = y >> 32 & (ULong)1;
995 *xc++ = (ULong)y & FFFFFFFF;
1000 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1001 borrow = (y & 0x10000) >> 16;
1002 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1003 borrow = (z & 0x10000) >> 16;
1008 y = (*xa & 0xffff) - borrow;
1009 borrow = (y & 0x10000) >> 16;
1010 z = (*xa++ >> 16) - borrow;
1011 borrow = (z & 0x10000) >> 16;
1016 y = *xa++ - *xb++ - borrow;
1017 borrow = (y & 0x10000) >> 16;
1023 borrow = (y & 0x10000) >> 16;
1042 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1043 #ifndef Avoid_Underflow
1044 #ifndef Sudden_Underflow
1053 #ifndef Avoid_Underflow
1054 #ifndef Sudden_Underflow
1057 L = -L >> Exp_shift;
1058 if (L < Exp_shift) {
1059 word0(a) = 0x80000 >> L;
1065 word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1077 ULong *xa, *xa0, w, y, z;
1091 if (!y) Bug(
"zero y in b2d");
1097 d0 = Exp_1 | y >> Ebits - k;
1098 w = xa > xa0 ? *--xa : 0;
1099 d1 = y << (32-Ebits) + k | w >> Ebits - k;
1102 z = xa > xa0 ? *--xa : 0;
1104 d0 = Exp_1 | y << k | z >> 32 - k;
1105 y = xa > xa0 ? *--xa : 0;
1106 d1 = z << k | y >> 32 - k;
1113 if (k < Ebits + 16) {
1114 z = xa > xa0 ? *--xa : 0;
1115 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1116 w = xa > xa0 ? *--xa : 0;
1117 y = xa > xa0 ? *--xa : 0;
1118 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1121 z = xa > xa0 ? *--xa : 0;
1122 w = xa > xa0 ? *--xa : 0;
1124 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1125 y = xa > xa0 ? *--xa : 0;
1126 d1 = w << k + 16 | y << k;
1130 word0(d) = d0 >> 16 | d0 << 16;
1131 word1(d) = d1 >> 16 | d1 << 16;
1141 (
double dd,
int *e,
int *bits)
1147 #ifndef Sudden_Underflow
1155 d0 = word0(d) >> 16 | word0(d) << 16;
1156 d1 = word1(d) >> 16 | word1(d) << 16;
1171 #ifdef Sudden_Underflow
1172 de = (int)(d0 >> Exp_shift);
1177 if ((de = (
int)(d0 >> Exp_shift)))
1182 if ((k = lo0bits(&y))) {
1183 x[0] = y | z << 32 - k;
1188 #ifndef Sudden_Underflow
1191 b->wds = (x[1] = z) ? 2 : 1;
1196 Bug(
"Zero passed to d2b");
1200 #ifndef Sudden_Underflow
1208 if (k = lo0bits(&y))
1210 x[0] = y | z << 32 - k & 0xffff;
1211 x[1] = z >> k - 16 & 0xffff;
1217 x[1] = y >> 16 | z << 16 - k & 0xffff;
1218 x[2] = z >> k & 0xffff;
1233 Bug(
"Zero passed to d2b");
1251 #ifndef Sudden_Underflow
1255 *e = (de - Bias - (P-1) << 2) + k;
1256 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1258 *e = de - Bias - (P-1) + k;
1261 #ifndef Sudden_Underflow
1264 *e = de - Bias - (P-1) + 1 + k;
1266 *bits = 32*i - hi0bits(x[i-1]);
1268 *bits = (i+2)*16 - hi0bits(x[i]);
1279 (Bigint *a, Bigint *b)
1284 dval(da) = b2d(a, &ka);
1285 dval(db) = b2d(b, &kb);
1287 k = ka - kb + 32*(a->wds - b->wds);
1289 k = ka - kb + 16*(a->wds - b->wds);
1293 word0(da) += (k >> 2)*Exp_msk1;
1299 word0(db) += (k >> 2)*Exp_msk1;
1305 word0(da) += k*Exp_msk1;
1308 word0(db) += k*Exp_msk1;
1311 return dval(da) / dval(db);
1316 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1317 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1326 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1327 static CONST
double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1328 #ifdef Avoid_Underflow
1329 9007199254740992.*9007199254740992.e-256
1337 #define Scale_Bit 0x10
1341 bigtens[] = { 1e16, 1e32, 1e64 };
1342 static CONST
double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1345 bigtens[] = { 1e16, 1e32 };
1346 static CONST
double tinytens[] = { 1e-16, 1e-32 };
1358 #define NAN_WORD0 0x7ff80000
1367 (CONST
char **sp, CONST
char *t)
1370 CONST
char *s = *sp;
1373 if ((c = *++s) >=
'A' && c <=
'Z')
1385 (U *rvp, CONST
char **sp)
1389 int havedig, udx0, xshift;
1392 havedig = xshift = 0;
1395 while((c = *(CONST
unsigned char*)++s)) {
1396 if (c >=
'0' && c <=
'9')
1398 else if (c >=
'a' && c <=
'f')
1400 else if (c >=
'A' && c <=
'F')
1402 else if (c <=
' ') {
1403 if (udx0 && havedig) {
1409 else if ( c ==
')' && havedig) {
1422 x[0] = (x[0] << 4) | (x[1] >> 28);
1423 x[1] = (x[1] << 4) | c;
1425 if ((x[0] &= 0xfffff) || x[1]) {
1426 word0(*rvp) = Exp_mask | x[0];
1435 (CONST
char *s00,
char **se)
1437 #ifdef Avoid_Underflow
1440 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1441 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1442 CONST
char *s, *s0, *s1;
1443 double aadj, aadj1, adj;
1447 Bigint *bb = NULL, *bb1 = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;
1449 int inexact, oldinexact;
1451 #ifdef Honor_FLT_ROUNDS
1458 sign = nz0 = nz = 0;
1460 for(s = s00;;s++)
switch(*s) {
1483 while(*++s ==
'0') ;
1489 for(nd = nf = 0; (c = *s) >=
'0' && c <=
'9'; nd++, s++)
1496 s1 = localeconv()->decimal_point;
1517 for(; c ==
'0'; c = *++s)
1519 if (c >
'0' && c <=
'9') {
1527 for(; c >=
'0' && c <=
'9'; c = *++s) {
1532 for(i = 1; i < nz; i++)
1535 else if (nd <= DBL_DIG + 1)
1539 else if (nd <= DBL_DIG + 1)
1547 if (c ==
'e' || c ==
'E') {
1548 if (!nd && !nz && !nz0) {
1559 if (c >=
'0' && c <=
'9') {
1562 if (c >
'0' && c <=
'9') {
1565 while((c = *++s) >=
'0' && c <=
'9')
1567 if (s - s1 > 8 || L > 19999)
1590 if (match(&s,
"nf")) {
1592 if (!match(&s,
"inity"))
1594 word0(rv) = 0x7ff00000;
1601 if (match(&s,
"an")) {
1602 word0(rv) = NAN_WORD0;
1603 word1(rv) = NAN_WORD1;
1627 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1632 oldinexact = get_inexact();
1634 dval(rv) = tens[k - 9] * dval(rv) + z;
1638 #ifndef RND_PRODQUOT
1639 #ifndef Honor_FLT_ROUNDS
1647 if (e <= Ten_pmax) {
1649 goto vax_ovfl_check;
1651 #ifdef Honor_FLT_ROUNDS
1658 rounded_product(dval(rv), tens[e]);
1663 if (e <= Ten_pmax + i) {
1667 #ifdef Honor_FLT_ROUNDS
1675 dval(rv) *= tens[i];
1681 word0(rv) -= P*Exp_msk1;
1682 rounded_product(dval(rv), tens[e]);
1683 if ((word0(rv) & Exp_mask)
1684 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1686 word0(rv) += P*Exp_msk1;
1688 rounded_product(dval(rv), tens[e]);
1693 #ifndef Inaccurate_Divide
1694 else if (e >= -Ten_pmax) {
1695 #ifdef Honor_FLT_ROUNDS
1702 rounded_quotient(dval(rv), tens[-e]);
1713 oldinexact = get_inexact();
1715 #ifdef Avoid_Underflow
1718 #ifdef Honor_FLT_ROUNDS
1719 if ((rounding = Flt_Rounds) >= 2) {
1721 rounding = rounding == 2 ? 0 : 2;
1733 dval(rv) *= tens[i];
1735 if (e1 > DBL_MAX_10_EXP) {
1742 #ifdef Honor_FLT_ROUNDS
1750 word0(rv) = Exp_mask;
1754 word0(rv) = Exp_mask;
1760 dval(rv0) *= dval(rv0);
1771 for(j = 0; e1 > 1; j++, e1 >>= 1)
1773 dval(rv) *= bigtens[j];
1775 word0(rv) -= P*Exp_msk1;
1776 dval(rv) *= bigtens[j];
1777 if ((z = word0(rv) & Exp_mask)
1778 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1780 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1787 word0(rv) += P*Exp_msk1;
1793 dval(rv) /= tens[i];
1795 if (e1 >= 1 << n_bigtens)
1797 #ifdef Avoid_Underflow
1800 for(j = 0; e1 > 0; j++, e1 >>= 1)
1802 dval(rv) *= tinytens[j];
1803 if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
1804 >> Exp_shift)) > 0) {
1809 word0(rv) = (P+2)*Exp_msk1;
1811 word0(rv) &= 0xffffffff << j-32;
1814 word1(rv) &= 0xffffffff << j;
1817 for(j = 0; e1 > 1; j++, e1 >>= 1)
1819 dval(rv) *= tinytens[j];
1821 dval(rv0) = dval(rv);
1822 dval(rv) *= tinytens[j];
1824 dval(rv) = 2.*dval(rv0);
1825 dval(rv) *= tinytens[j];
1837 #ifndef Avoid_Underflow
1852 bd0 = s2b(s0, nd0, nd, y);
1855 bd = Balloc(bd0->k);
1857 bb = d2b(dval(rv), &bbe, &bbbits);
1873 #ifdef Honor_FLT_ROUNDS
1877 #ifdef Avoid_Underflow
1885 #ifdef Sudden_Underflow
1887 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1902 #ifdef Avoid_Underflow
1905 i = bb2 < bd2 ? bb2 : bd2;
1914 bs = pow5mult(bs, bb5);
1920 bb = lshift(bb, bb2);
1922 bd = pow5mult(bd, bd5);
1924 bd = lshift(bd, bd2);
1926 bs = lshift(bs, bs2);
1927 delta = diff(bb, bd);
1928 dsign = delta->sign;
1931 #ifdef Honor_FLT_ROUNDS
1932 if (rounding != 1) {
1935 if (!delta->x[0] && delta->wds <= 1) {
1951 && !(word0(rv) & Frac_mask)) {
1952 y = word0(rv) & Exp_mask;
1953 #ifdef Avoid_Underflow
1954 if (!scale || y > 2*P*Exp_msk1)
1959 delta = lshift(delta,Log2P);
1960 if (cmp(delta, bs) <= 0)
1965 #ifdef Avoid_Underflow
1966 if (scale && (y = word0(rv) & Exp_mask)
1968 word0(adj) += (2*P+1)*Exp_msk1 - y;
1970 #ifdef Sudden_Underflow
1971 if ((word0(rv) & Exp_mask) <=
1973 word0(rv) += P*Exp_msk1;
1974 dval(rv) += adj*ulp(dval(rv));
1975 word0(rv) -= P*Exp_msk1;
1980 dval(rv) += adj*ulp(dval(rv));
1984 adj = ratio(delta, bs);
1987 if (adj <= 0x7ffffffe) {
1991 if (!((rounding>>1) ^ dsign))
1996 #ifdef Avoid_Underflow
1997 if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
1998 word0(adj) += (2*P+1)*Exp_msk1 - y;
2000 #ifdef Sudden_Underflow
2001 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2002 word0(rv) += P*Exp_msk1;
2003 adj *= ulp(dval(rv));
2008 word0(rv) -= P*Exp_msk1;
2013 adj *= ulp(dval(rv));
2026 if (dsign || word1(rv) || word0(rv) & Bndry_mask
2028 #ifdef Avoid_Underflow
2029 || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2031 || (word0(rv) & Exp_mask) <= Exp_msk1
2036 if (!delta->x[0] && delta->wds <= 1)
2041 if (!delta->x[0] && delta->wds <= 1) {
2048 delta = lshift(delta,Log2P);
2049 if (cmp(delta, bs) > 0)
2056 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2058 #ifdef Avoid_Underflow
2059 (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2060 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2064 word0(rv) = (word0(rv) & Exp_mask)
2071 #ifdef Avoid_Underflow
2077 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2080 #ifdef Sudden_Underflow
2081 L = word0(rv) & Exp_mask;
2085 #ifdef Avoid_Underflow
2086 if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2094 #ifdef Avoid_Underflow
2096 L = word0(rv) & Exp_mask;
2097 if (L <= (2*P+1)*Exp_msk1) {
2098 if (L > (P+2)*Exp_msk1)
2107 L = (word0(rv) & Exp_mask) - Exp_msk1;
2109 word0(rv) = L | Bndry_mask1;
2110 word1(rv) = 0xffffffff;
2117 #ifndef ROUND_BIASED
2118 if (!(word1(rv) & LSB))
2122 dval(rv) += ulp(dval(rv));
2123 #ifndef ROUND_BIASED
2125 dval(rv) -= ulp(dval(rv));
2126 #ifndef Sudden_Underflow
2131 #ifdef Avoid_Underflow
2137 if ((aadj = ratio(delta, bs)) <= 2.) {
2140 else if (word1(rv) || word0(rv) & Bndry_mask) {
2141 #ifndef Sudden_Underflow
2142 if (word1(rv) == Tiny1 && !word0(rv))
2152 if (aadj < 2./FLT_RADIX)
2153 aadj = 1./FLT_RADIX;
2161 aadj1 = dsign ? aadj : -aadj;
2162 #ifdef Check_FLT_ROUNDS
2172 if (Flt_Rounds == 0)
2176 y = word0(rv) & Exp_mask;
2180 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2181 dval(rv0) = dval(rv);
2182 word0(rv) -= P*Exp_msk1;
2183 adj = aadj1 * ulp(dval(rv));
2185 if ((word0(rv) & Exp_mask) >=
2186 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2187 if (word0(rv0) == Big0 && word1(rv0) == Big1)
2194 word0(rv) += P*Exp_msk1;
2197 #ifdef Avoid_Underflow
2198 if (scale && y <= 2*P*Exp_msk1) {
2199 if (aadj <= 0x7fffffff) {
2200 if ((z = (ULong)aadj) <= 0)
2203 aadj1 = dsign ? aadj : -aadj;
2205 dval(aadj2) = aadj1;
2206 word0(aadj2) += (2*P+1)*Exp_msk1 - y;
2207 aadj1 = dval(aadj2);
2209 adj = aadj1 * ulp(dval(rv));
2212 #ifdef Sudden_Underflow
2213 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2214 dval(rv0) = dval(rv);
2215 word0(rv) += P*Exp_msk1;
2216 adj = aadj1 * ulp(dval(rv));
2219 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
2221 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2224 if (word0(rv0) == Tiny0
2225 && word1(rv0) == Tiny1)
2232 word0(rv) -= P*Exp_msk1;
2235 adj = aadj1 * ulp(dval(rv));
2246 if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
2247 aadj1 = (double)(
int)(aadj + 0.5);
2251 adj = aadj1 * ulp(dval(rv));
2256 z = word0(rv) & Exp_mask;
2258 #ifdef Avoid_Underflow
2266 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2267 if (aadj < .4999999 || aadj > .5000001)
2270 else if (aadj < .4999999/FLT_RADIX)
2283 word0(rv0) = Exp_1 + (70 << Exp_shift);
2288 else if (!oldinexact)
2291 #ifdef Avoid_Underflow
2293 word0(rv0) = Exp_1 - 2*P*Exp_msk1;
2295 dval(rv) *= dval(rv0);
2298 if (word0(rv) == 0 && word1(rv) == 0)
2304 if (inexact && !(word0(rv) & Exp_mask)) {
2307 dval(rv0) *= dval(rv0);
2319 return sign ? -dval(rv) : dval(rv);
2324 (Bigint *b, Bigint *S)
2327 ULong *bx, *bxe, q, *sx, *sxe;
2329 ULLong borrow, carry, y, ys;
2331 ULong borrow, carry, y, ys;
2340 Bug(
"oversize b in quorem");
2348 q = *bxe / (*sxe + 1);
2351 Bug(
"oversized quotient in quorem");
2358 ys = *sx++ * (ULLong)q + carry;
2360 y = *bx - (ys & FFFFFFFF) - borrow;
2361 borrow = y >> 32 & (ULong)1;
2362 *bx++ = (ULong)y & FFFFFFFF;
2366 ys = (si & 0xffff) * q + carry;
2367 zs = (si >> 16) * q + (ys >> 16);
2369 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2370 borrow = (y & 0x10000) >> 16;
2371 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2372 borrow = (z & 0x10000) >> 16;
2375 ys = *sx++ * q + carry;
2377 y = *bx - (ys & 0xffff) - borrow;
2378 borrow = (y & 0x10000) >> 16;
2386 while(--bxe > bx && !*bxe)
2391 if (cmp(b, S) >= 0) {
2401 y = *bx - (ys & FFFFFFFF) - borrow;
2402 borrow = y >> 32 & (ULong)1;
2403 *bx++ = (ULong)y & FFFFFFFF;
2407 ys = (si & 0xffff) + carry;
2408 zs = (si >> 16) + (ys >> 16);
2410 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2411 borrow = (y & 0x10000) >> 16;
2412 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2413 borrow = (z & 0x10000) >> 16;
2418 y = *bx - (ys & 0xffff) - borrow;
2419 borrow = (y & 0x10000) >> 16;
2428 while(--bxe > bx && !*bxe)
2436 #ifndef MULTIPLE_THREADS
2437 static char *dtoa_result;
2447 sizeof(Bigint) -
sizeof(ULong) -
sizeof(int) + j <= (
unsigned)i;
2450 r = (
int*)Balloc(k);
2453 #ifndef MULTIPLE_THREADS
2460 nrv_alloc(CONST
char *s,
char **rve,
int n)
2464 t = rv = rv_alloc(n);
2465 while((*t = *s++)) t++;
2480 Bigint *b = (Bigint *)((
int *)s - 1);
2481 b->maxwds = 1 << (b->k = *(
int*)b);
2483 #ifndef MULTIPLE_THREADS
2484 if (s == dtoa_result)
2525 (
double dd,
int mode,
int ndigits,
int *decpt,
int *sign,
char **rve)
2561 int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
2562 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2563 spec_case, try_quick;
2565 #ifndef Sudden_Underflow
2569 Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
2573 #ifdef Honor_FLT_ROUNDS
2577 int inexact, oldinexact;
2580 #ifndef MULTIPLE_THREADS
2582 freedtoa(dtoa_result);
2588 if (word0(d) & Sign_bit) {
2591 word0(d) &= ~Sign_bit;
2596 #if defined(IEEE_Arith) + defined(VAX)
2598 if ((word0(d) & Exp_mask) == Exp_mask)
2600 if (word0(d) == 0x8000)
2606 if (!word1(d) && !(word0(d) & 0xfffff))
2607 return nrv_alloc(
"Infinity", rve, 8);
2609 return nrv_alloc(
"NaN", rve, 3);
2617 return nrv_alloc(
"0", rve, 1);
2621 try_quick = oldinexact = get_inexact();
2624 #ifdef Honor_FLT_ROUNDS
2625 if ((rounding = Flt_Rounds) >= 2) {
2627 rounding = rounding == 2 ? 0 : 2;
2634 b = d2b(dval(d), &be, &bbits);
2635 #ifdef Sudden_Underflow
2636 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2638 if ((i = (
int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2641 word0(d2) &= Frac_mask1;
2642 word0(d2) |= Exp_11;
2644 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2675 #ifndef Sudden_Underflow
2681 i = bbits + be + (Bias + (P-1) - 1);
2682 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32
2683 : word1(d) << 32 - i;
2685 word0(d2) -= 31*Exp_msk1;
2686 i -= (Bias + (P-1) - 1) + 1;
2690 ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2692 if (ds < 0. && ds != k)
2695 if (k >= 0 && k <= Ten_pmax) {
2696 if (dval(d) < tens[k])
2719 if (mode < 0 || mode > 9)
2723 #ifdef Check_FLT_ROUNDS
2724 try_quick = Rounding == 1;
2748 ilim = ilim1 = i = ndigits;
2754 i = ndigits + k + 1;
2760 s = s0 = rv_alloc(i);
2762 #ifdef Honor_FLT_ROUNDS
2763 if (mode > 1 && rounding != 1)
2767 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2782 dval(d) /= bigtens[n_bigtens-1];
2785 for(; j; j >>= 1, i++)
2792 else if ((j1 = -k)) {
2793 dval(d) *= tens[j1 & 0xf];
2794 for(j = j1 >> 4; j; j >>= 1, i++)
2797 dval(d) *= bigtens[i];
2800 if (k_check && dval(d) < 1. && ilim > 0) {
2808 dval(eps) = ieps*dval(d) + 7.;
2809 word0(eps) -= (P-1)*Exp_msk1;
2813 if (dval(d) > dval(eps))
2815 if (dval(d) < -dval(eps))
2819 #ifndef No_leftright
2824 dval(eps) = 0.5/tens[ilim-1] - dval(eps);
2826 L = (
long int)dval(d);
2828 *s++ =
'0' + (int)L;
2829 if (dval(d) < dval(eps))
2831 if (1. - dval(d) < dval(eps))
2842 dval(eps) *= tens[ilim-1];
2843 for(i = 1;; i++, dval(d) *= 10.) {
2844 L = (Long)(dval(d));
2845 if (!(dval(d) -= L))
2847 *s++ =
'0' + (int)L;
2849 if (dval(d) > 0.5 + dval(eps))
2851 else if (dval(d) < 0.5 - dval(eps)) {
2860 #ifndef No_leftright
2872 if (be >= 0 && k <= Int_max) {
2875 if (ndigits < 0 && ilim <= 0) {
2877 if (ilim < 0 || dval(d) <= 5*ds)
2881 for(i = 1;; i++, dval(d) *= 10.) {
2882 L = (Long)(dval(d) / ds);
2884 #ifdef Check_FLT_ROUNDS
2891 *s++ =
'0' + (int)L;
2899 #ifdef Honor_FLT_ROUNDS
2903 case 2:
goto bump_up;
2907 if (dval(d) > ds || dval(d) == ds && L & 1) {
2928 #ifndef Sudden_Underflow
2929 denorm ? be + (Bias + (P-1) - 1 + 1) :
2932 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2940 if (m2 > 0 && s2 > 0) {
2941 i = m2 < s2 ? m2 : s2;
2949 mhi = pow5mult(mhi, m5);
2958 b = pow5mult(b, b5);
2962 S = pow5mult(S, s5);
2967 if ((mode < 2 || leftright)
2968 #ifdef Honor_FLT_ROUNDS
2972 if (!word1(d) && !(word0(d) & Bndry_mask)
2973 #ifndef Sudden_Underflow
2974 && word0(d) & (Exp_mask & ~Exp_msk1)
2992 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
2995 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
3017 b = multadd(b, 10, 0);
3019 mhi = multadd(mhi, 10, 0);
3023 if (ilim <= 0 && (mode == 3 || mode == 5)) {
3024 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
3037 mhi = lshift(mhi, m2);
3045 mhi = Balloc(mhi->k);
3047 mhi = lshift(mhi, Log2P);
3051 dig = quorem(b,S) +
'0';
3056 delta = diff(S, mhi);
3057 j1 = delta->sign ? 1 : cmp(b, delta);
3059 #ifndef ROUND_BIASED
3060 if (j1 == 0 && mode != 1 && !(word1(d) & 1)
3061 #ifdef Honor_FLT_ROUNDS
3070 else if (!b->x[0] && b->wds <= 1)
3077 if (j < 0 || j == 0 && mode != 1
3078 #ifndef ROUND_BIASED
3082 if (!b->x[0] && b->wds <= 1) {
3088 #ifdef Honor_FLT_ROUNDS
3091 case 0:
goto accept_dig;
3092 case 2:
goto keep_dig;
3098 if ((j1 > 0 || j1 == 0 && dig & 1)
3107 #ifdef Honor_FLT_ROUNDS
3119 #ifdef Honor_FLT_ROUNDS
3125 b = multadd(b, 10, 0);
3127 mlo = mhi = multadd(mhi, 10, 0);
3129 mlo = multadd(mlo, 10, 0);
3130 mhi = multadd(mhi, 10, 0);
3136 *s++ = dig = quorem(b,S) +
'0';
3137 if (!b->x[0] && b->wds <= 1) {
3145 b = multadd(b, 10, 0);
3150 #ifdef Honor_FLT_ROUNDS
3152 case 0:
goto trimzeros;
3153 case 2:
goto roundoff;
3158 if (j > 0 || j == 0 && dig & 1) {
3169 #ifdef Honor_FLT_ROUNDS
3179 if (mlo && mlo != mhi)
3187 word0(d) = Exp_1 + (70 << Exp_shift);
3192 else if (!oldinexact)
const TDEShortcut & next()