40 #include <my_global.h>
49 #define DTOA_BUFF_SIZE (460 * sizeof(void *))
52 #define DTOA_OVERFLOW 9999
54 static double my_strtod_int(
const char *,
char **,
int *,
char *,
size_t);
55 static char *dtoa(
double,
int,
int,
int *,
int *,
char **,
char *,
size_t);
56 static void dtoa_free(
char *,
char *,
size_t);
89 size_t my_fcvt(
double x,
int precision,
char *
to, my_bool *error)
91 int decpt, sign, len,
i;
92 char *res, *src, *end, *dst=
to;
93 char buf[DTOA_BUFF_SIZE];
94 DBUG_ASSERT(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
96 res= dtoa(x, 5, precision, &decpt, &sign, &end, buf,
sizeof(buf));
98 if (decpt == DTOA_OVERFLOW)
100 dtoa_free(res, buf,
sizeof(buf));
118 for (i= decpt; i < 0; i++)
122 for (i= 1; i <= len; i++)
125 if (i == decpt && i < len)
136 for (i= precision - MY_MAX(0, (len - decpt)); i > 0; i--)
144 dtoa_free(res, buf,
sizeof(buf));
212 size_t my_gcvt(
double x, my_gcvt_arg_type
type,
int width,
char *to,
215 int decpt, sign, len, exp_len;
216 char *res, *src, *end, *dst=
to, *dend= dst + width;
217 char buf[DTOA_BUFF_SIZE];
218 my_bool have_space, force_e_format;
219 DBUG_ASSERT(width > 0 && to != NULL);
225 res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? width : MY_MIN(width, FLT_DIG),
226 &decpt, &sign, &end, buf,
sizeof(buf));
227 if (decpt == DTOA_OVERFLOW)
229 dtoa_free(res, buf,
sizeof(buf));
248 exp_len= 1 + (decpt >= 101 || decpt <= -99) + (decpt >= 11 || decpt <= -9);
259 have_space= (decpt <= 0 ? len - decpt + 2 :
260 decpt > 0 && decpt < len ? len + 1 :
267 force_e_format= (decpt <= 0 && width <= 2 - decpt && width >= 3 + exp_len);
299 ((decpt <= width && (decpt >= -1 || (decpt == -2 &&
300 (len > 1 || !force_e_format)))) &&
307 (!have_space || (decpt >= -MAX_DECPT_FOR_F_FORMAT + 1 &&
308 (decpt <= MAX_DECPT_FOR_F_FORMAT || len > decpt))))
313 width-= (decpt < len) + (decpt <= 0 ? 1 - decpt : 0);
330 dtoa_free(res, buf,
sizeof(buf));
331 res= dtoa(x, 5, width - decpt, &decpt, &sign, &end, buf,
sizeof(buf));
347 if (sign && dst < dend)
353 if (len > 0 && dst < dend)
355 for (; decpt < 0 && dst < dend; decpt++)
359 for (i= 1; i <= len && dst < dend; i++)
362 if (i == decpt && i < len && dst < dend)
365 while (i++ <= decpt && dst < dend)
396 dtoa_free(res, buf,
sizeof(buf));
397 res= dtoa(x, 4, width, &decpt, &sign, &end, buf,
sizeof(buf));
407 if (sign && dst < dend)
411 if (len > 1 && dst < dend)
414 while (src < end && dst < dend)
419 if (decpt_sign && dst < dend)
422 if (decpt >= 100 && dst < dend)
424 *dst++= decpt / 100 +
'0';
427 *dst++= decpt / 10 +
'0';
429 else if (decpt >= 10 && dst < dend)
430 *dst++= decpt / 10 +
'0';
432 *dst++= decpt % 10 +
'0';
437 dtoa_free(res, buf,
sizeof(buf));
461 double my_strtod(
const char *str,
char **end,
int *error)
463 char buf[DTOA_BUFF_SIZE];
465 DBUG_ASSERT(end != NULL && ((str != NULL && *end != NULL) ||
466 (str == NULL && *end == NULL)) &&
469 res= my_strtod_int(str, end, error, buf,
sizeof(buf));
470 return (*error == 0) ? res : (res < 0 ? -DBL_MAX : DBL_MAX);
474 double my_atof(
const char *nptr)
477 const char *end= nptr+65535;
478 return (my_strtod(nptr, (
char**) &end, &error));
546 typedef uint32 ULong;
548 typedef uint64 ULLong;
550 typedef union {
double d; ULong L[2]; }
U;
552 #if defined(WORDS_BIGENDIAN) || (defined(__FLOAT_WORD_ORDER) && \
553 (__FLOAT_WORD_ORDER == __BIG_ENDIAN))
554 #define word0(x) (x)->L[0]
555 #define word1(x) (x)->L[1]
557 #define word0(x) (x)->L[1]
558 #define word1(x) (x)->L[0]
561 #define dval(x) (x)->d
570 #define Exp_shift1 20
571 #define Exp_msk1 0x100000
572 #define Exp_mask 0x7ff00000
576 #define Exp_1 0x3ff00000
577 #define Exp_11 0x3ff00000
579 #define Frac_mask 0xfffff
580 #define Frac_mask1 0xfffff
583 #define Bndry_mask 0xfffff
584 #define Bndry_mask1 0xfffff
586 #define Sign_bit 0x80000000
594 #define Flt_Rounds FLT_ROUNDS
600 #ifdef Honor_FLT_ROUNDS
601 #define Rounding rounding
602 #undef Check_FLT_ROUNDS
603 #define Check_FLT_ROUNDS
605 #define Rounding Flt_Rounds
608 #define rounded_product(a,b) a*= b
609 #define rounded_quotient(a,b) a/= b
611 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
612 #define Big1 0xffffffff
613 #define FFFFFFFF 0xffffffffUL
619 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
620 2*sizeof(int) + y->wds*sizeof(ULong))
662 DBUG_ASSERT(k <= Kmax);
663 if (k <= Kmax && alloc->freelist[k])
665 rv= alloc->freelist[k];
666 alloc->freelist[k]= rv->p.next;
673 len= MY_ALIGN(
sizeof(
Bigint) + x *
sizeof(ULong), SIZEOF_CHARP);
675 if (alloc->free + len <= alloc->end)
677 rv= (
Bigint*) alloc->free;
681 rv= (
Bigint*) malloc(len);
686 rv->sign= rv->wds= 0;
687 rv->p.x= (ULong*) (rv + 1);
699 char *gptr= (
char*) v;
700 if (gptr < alloc->begin || gptr >= alloc->end)
702 else if (v->k <= Kmax)
709 v->p.next= alloc->freelist[v->k];
710 alloc->freelist[v->k]= v;
725 int aligned_size= MY_ALIGN(i, SIZEOF_CHARP);
726 if (alloc->free + aligned_size <= alloc->end)
729 alloc->free+= aligned_size;
742 static void dtoa_free(
char *gptr,
char *buf,
size_t buf_size)
744 if (gptr < buf || gptr >= buf + buf_size)
766 y= *x * (ULLong)m + carry;
768 *x++= (ULong)(y & FFFFFFFF);
773 if (wds >= b->maxwds)
775 b1= Balloc(b->k+1, alloc);
780 b->p.x[wds++]= (ULong) carry;
807 for (k= 0, y= 1; x > y; y <<= 1, k++) ;
817 b= multadd(b, 10, *s++ -
'0', alloc);
825 b= multadd(b, 10, *s++ -
'0', alloc);
830 static int hi0bits(
register ULong x)
834 if (!(x & 0xffff0000))
839 if (!(x & 0xff000000))
844 if (!(x & 0xf0000000))
849 if (!(x & 0xc0000000))
854 if (!(x & 0x80000000))
857 if (!(x & 0x40000000))
864 static int lo0bits(ULong *y)
867 register ULong x= *y;
933 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
950 for (x= c->p.x, xa= x + wc; x < xa; x++)
957 for (; xb < xbe; xc0++)
966 z= *x++ * (ULLong)y + *xc + carry;
968 *xc++= (ULong) (z & FFFFFFFF);
974 for (xc0= c->p.x, xc= xc0 + wc; wc > 0 && !*--xc; --wc) ;
985 static ULong powers5[]=
993 2242703233UL, 762134875UL, 1262UL,
995 3211403009UL, 1849224548UL, 3668416493UL, 3913284084UL, 1593091UL,
997 781532673UL, 64985353UL, 253049085UL, 594863151UL, 3553621484UL,
998 3288652808UL, 3167596762UL, 2788392729UL, 3911132675UL, 590UL,
1000 2553183233UL, 3201533787UL, 3638140786UL, 303378311UL, 1809731782UL,
1001 3477761648UL, 3583367183UL, 649228654UL, 2915460784UL, 487929380UL,
1002 1011012442UL, 1677677582UL, 3428152256UL, 1710878487UL, 1438394610UL,
1003 2161952759UL, 4100910556UL, 1608314830UL, 349175UL
1010 { { powers5 }, 1, 1, 0, 1 },
1011 { { powers5 + 1 }, 1, 1, 0, 1 },
1012 { { powers5 + 2 }, 1, 2, 0, 2 },
1013 { { powers5 + 4 }, 2, 3, 0, 3 },
1014 { { powers5 + 7 }, 3, 5, 0, 5 },
1015 { { powers5 + 12 }, 4, 10, 0, 10 },
1016 { { powers5 + 22 }, 5, 19, 0, 19 }
1019 #define P5A_MAX (sizeof(p5_a)/sizeof(*p5_a) - 1)
1023 Bigint *b1, *p5, *p51=NULL;
1025 static int p05[3]= { 5, 25, 125 };
1026 my_bool overflow= FALSE;
1029 b= multadd(b, p05[i-1], 0, alloc);
1038 b1= mult(b, p5, alloc);
1047 p51= mult(p5, p5, alloc);
1051 else if (p5 < p5_a + P5A_MAX)
1053 else if (p5 == p5_a + P5A_MAX)
1055 p5= mult(p5, p5, alloc);
1069 ULong *x, *x1, *xe, z;
1074 for (i= b->maxwds; n1 > i; i<<= 1)
1076 b1= Balloc(k1, alloc);
1078 for (i= 0; i <
n; i++)
1107 ULong *xa, *xa0, *xb, *xb0;
1121 return *xa < *xb ? -1 : 1;
1133 ULong *xa, *xae, *xb, *xbe, *xc;
1139 c= Balloc(0, alloc);
1153 c= Balloc(a->k, alloc);
1165 y= (ULLong)*xa++ - *xb++ - borrow;
1166 borrow= y >> 32 & (ULong)1;
1167 *xc++= (ULong) (y & FFFFFFFF);
1173 borrow= y >> 32 & (ULong)1;
1174 *xc++= (ULong) (y & FFFFFFFF);
1183 static double ulp(
U *x)
1188 L= (word0(x) & Exp_mask) - (P - 1)*Exp_msk1;
1195 static double b2d(
Bigint *a,
int *e)
1197 ULong *xa, *xa0, w, y, z;
1200 #define d0 word0(&d)
1201 #define d1 word1(&d)
1210 d0= Exp_1 | y >> (Ebits - k);
1211 w= xa > xa0 ? *--xa : 0;
1212 d1= y << ((32-Ebits) + k) | w >> (Ebits - k);
1215 z= xa > xa0 ? *--xa : 0;
1218 d0= Exp_1 | y << k | z >> (32 - k);
1219 y= xa > xa0 ? *--xa : 0;
1220 d1= z << k | y >> (32 - k);
1243 b= Balloc(1, alloc);
1248 if ((de= (
int)(d0 >> Exp_shift)))
1252 if ((k= lo0bits(&y)))
1254 x[0]= y | z << (32 - k);
1259 i= b->wds= (x[1]= z) ? 2 : 1;
1270 *e= de - Bias - (P-1) + k;
1275 *e= de - Bias - (P-1) + 1 + k;
1276 *bits= 32*i - hi0bits(x[i-1]);
1289 dval(&da)= b2d(a, &ka);
1290 dval(&db)= b2d(b, &kb);
1291 k= ka - kb + 32*(a->wds - b->wds);
1293 word0(&da)+= k*Exp_msk1;
1297 word0(&db)+= k*Exp_msk1;
1299 return dval(&da) / dval(&db);
1302 static const double tens[] =
1304 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1305 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1309 static const double bigtens[]= { 1e16, 1e32, 1e64, 1e128, 1e256 };
1310 static const double tinytens[]=
1311 { 1e-16, 1e-32, 1e-64, 1e-128,
1312 9007199254740992.*9007199254740992.e-256
1318 #define Scale_Bit 0x10
1350 static double my_strtod_int(
const char *s00,
char **se,
int *error,
char *buf,
size_t buf_size)
1353 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, UNINIT_VAR(c), dsign,
1354 e, e1, esign,
i, j, k, nd, nd0, nf, nz, nz0, sign;
1355 const char *s, *s0, *s1, *end = *se;
1357 U aadj2, adj, rv, rv0;
1360 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1362 int inexact, oldinexact;
1364 #ifdef Honor_FLT_ROUNDS
1371 alloc.begin= alloc.free=
buf;
1372 alloc.end= buf + buf_size;
1373 memset(alloc.freelist, 0,
sizeof(alloc.freelist));
1377 for (s= s00; s < end; s++)
1402 while (++s < end && *s ==
'0') ;
1408 for (nd= nf= 0; s < end && (c= *s) >=
'0' && c <=
'9'; nd++, s++)
1414 if (s < end && c ==
'.')
1419 for (; s < end; ++s)
1426 if (s < end && c >
'0' && c <=
'9')
1435 for (; s < end; ++s)
1438 if (c < '0' || c >
'9')
1446 if (nd < 2 * DBL_DIG)
1452 for (i= 1; i < nz; i++)
1455 else if (nd <= DBL_DIG + 1)
1459 else if (nd <= DBL_DIG + 1)
1468 if (s < end && (c ==
'e' || c ==
'E'))
1470 if (!nd && !nz && !nz0)
1481 if (s < end && c >=
'0' && c <=
'9')
1483 while (s < end && c ==
'0')
1485 if (s < end && c >
'0' && c <=
'9') {
1488 while (++s < end && (c= *s) >=
'0' && c <=
'9')
1490 if (s - s1 > 8 || L > 19999)
1527 k= nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1533 oldinexact = get_inexact();
1535 dval(&rv)= tens[k - 9] * dval(&rv) + z;
1539 #ifndef Honor_FLT_ROUNDS
1550 #ifdef Honor_FLT_ROUNDS
1558 rounded_product(dval(&rv), tens[e]);
1562 if (e <= Ten_pmax + i)
1568 #ifdef Honor_FLT_ROUNDS
1577 dval(&rv)*= tens[
i];
1578 rounded_product(dval(&rv), tens[e]);
1582 #ifndef Inaccurate_Divide
1583 else if (e >= -Ten_pmax)
1585 #ifdef Honor_FLT_ROUNDS
1593 rounded_quotient(dval(&rv), tens[-e]);
1603 oldinexact= get_inexact();
1606 #ifdef Honor_FLT_ROUNDS
1607 if ((rounding= Flt_Rounds) >= 2)
1610 rounding= rounding == 2 ? 0 : 2;
1622 dval(&rv)*= tens[
i];
1625 if (e1 > DBL_MAX_10_EXP)
1630 #ifdef Honor_FLT_ROUNDS
1639 word0(&rv)= Exp_mask;
1643 word0(&rv)= Exp_mask;
1649 dval(&rv0)*= dval(&rv0);
1656 for(j= 0; e1 > 1; j++, e1>>= 1)
1658 dval(&rv)*= bigtens[j];
1660 word0(&rv)-= P*Exp_msk1;
1661 dval(&rv)*= bigtens[j];
1662 if ((z= word0(&rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
1664 if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P))
1671 word0(&rv)+= P*Exp_msk1;
1678 dval(&rv)/= tens[
i];
1681 if (e1 >= 1 << n_bigtens)
1685 for(j= 0; e1 > 0; j++, e1>>= 1)
1687 dval(&rv)*= tinytens[j];
1688 if (scale && (j = 2 * P + 1 - ((word0(&rv) & Exp_mask) >> Exp_shift)) > 0)
1695 word0(&rv)= (P + 2) * Exp_msk1;
1697 word0(&rv)&= 0xffffffff << (j - 32);
1700 word1(&rv)&= 0xffffffff << j;
1717 bd0= s2b(s0, nd0, nd, y, &alloc);
1721 bd= Balloc(bd0->k, &alloc);
1723 bb= d2b(&rv, &bbe, &bbbits, &alloc);
1741 #ifdef Honor_FLT_ROUNDS
1754 i= bb2 < bd2 ? bb2 : bd2;
1765 bs= pow5mult(bs, bb5, &alloc);
1766 bb1= mult(bs, bb, &alloc);
1771 bb= lshift(bb, bb2, &alloc);
1773 bd= pow5mult(bd, bd5, &alloc);
1775 bd= lshift(bd, bd2, &alloc);
1777 bs= lshift(bs, bs2, &alloc);
1778 delta= diff(bb, bd, &alloc);
1782 #ifdef Honor_FLT_ROUNDS
1788 if (!delta->p.x[0] && delta->wds <= 1)
1807 if (!word1(&rv) && !(word0(&rv) & Frac_mask))
1809 y= word0(&rv) & Exp_mask;
1810 if (!scale || y > 2*P*Exp_msk1)
1812 delta= lshift(delta, Log2P, &alloc);
1813 if (cmp(delta, bs) <= 0)
1818 if (scale && (y= word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
1819 word0(&adj)+= (2 * P + 1) * Exp_msk1 - y;
1820 dval(&rv)+= adj.d * ulp(&rv);
1824 adj.d= ratio(delta, bs);
1827 if (adj.d <= 0x7ffffffe)
1833 if (!((rounding >> 1) ^ dsign))
1838 if (scale && (y= word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
1839 word0(&adj)+= (2 * P + 1) * Exp_msk1 - y;
1855 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask ||
1856 (word0(&rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1)
1859 if (!delta->x[0] && delta->wds <= 1)
1864 if (!delta->p.x[0] && delta->wds <= 1)
1872 delta= lshift(delta, Log2P, &alloc);
1873 if (cmp(delta, bs) > 0)
1882 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1 &&
1884 ((scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1) ?
1885 (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
1889 word0(&rv)= (word0(&rv) & Exp_mask) + Exp_msk1;
1895 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv))
1901 L= word0(&rv) & Exp_mask;
1902 if (L <= (2 *P + 1) * Exp_msk1)
1904 if (L > (P + 2) * Exp_msk1)
1911 L= (word0(&rv) & Exp_mask) - Exp_msk1;
1912 word0(&rv)= L | Bndry_mask1;
1913 word1(&rv)= 0xffffffff;
1916 if (!(word1(&rv) & LSB))
1919 dval(&rv)+= ulp(&rv);
1922 dval(&rv)-= ulp(&rv);
1929 if ((aadj= ratio(delta, bs)) <= 2.)
1933 else if (word1(&rv) || word0(&rv) & Bndry_mask)
1935 if (word1(&rv) == Tiny1 && !word0(&rv))
1943 if (aadj < 2. / FLT_RADIX)
1944 aadj= 1. / FLT_RADIX;
1953 aadj1= dsign ? aadj : -aadj;
1954 #ifdef Check_FLT_ROUNDS
1965 if (Flt_Rounds == 0)
1969 y= word0(&rv) & Exp_mask;
1973 if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
1975 dval(&rv0)= dval(&rv);
1976 word0(&rv)-= P * Exp_msk1;
1977 adj.d= aadj1 * ulp(&rv);
1979 if ((word0(&rv) & Exp_mask) >= Exp_msk1 * (DBL_MAX_EXP + Bias - P))
1981 if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
1988 word0(&rv)+= P * Exp_msk1;
1992 if (scale && y <= 2 * P * Exp_msk1)
1994 if (aadj <= 0x7fffffff)
1996 if ((z= (ULong) aadj) <= 0)
1999 aadj1= dsign ? aadj : -aadj;
2001 dval(&aadj2) = aadj1;
2002 word0(&aadj2)+= (2 * P + 1) * Exp_msk1 - y;
2003 aadj1= dval(&aadj2);
2004 adj.d= aadj1 * ulp(&rv);
2011 adj.d= aadj1 * ulp(&rv);
2015 z= word0(&rv) & Exp_mask;
2024 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask)
2026 if (aadj < .4999999 || aadj > .5000001)
2029 else if (aadj < .4999999 / FLT_RADIX)
2037 Bfree(delta, &alloc);
2044 word0(&rv0)= Exp_1 + (70 << Exp_shift);
2049 else if (!oldinexact)
2054 word0(&rv0)= Exp_1 - 2 * P * Exp_msk1;
2056 dval(&rv)*= dval(&rv0);
2059 if (inexact && !(word0(&rv) & Exp_mask))
2063 dval(&rv0)*= dval(&rv0);
2071 Bfree(delta, &alloc);
2074 return sign ? -dval(&rv) : dval(&rv);
2081 ULong *bx, *bxe, q, *sx, *sxe;
2082 ULLong borrow, carry, y, ys;
2091 q= *bxe / (*sxe + 1);
2098 ys= *sx++ * (ULLong)q + carry;
2100 y= *bx - (ys & FFFFFFFF) - borrow;
2101 borrow= y >> 32 & (ULong)1;
2102 *bx++= (ULong) (y & FFFFFFFF);
2108 while (--bxe > bx && !*bxe)
2124 y= *bx - (ys & FFFFFFFF) - borrow;
2125 borrow= y >> 32 & (ULong)1;
2126 *bx++= (ULong) (y & FFFFFFFF);
2133 while (--bxe > bx && !*bxe)
2177 static char *dtoa(
double dd,
int mode,
int ndigits,
int *decpt,
int *sign,
2178 char **rve,
char *buf,
size_t buf_size)
2215 int bbits, b2, b5, be, dig,
i, ieps, UNINIT_VAR(ilim), ilim0,
2216 UNINIT_VAR(ilim1), j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2217 spec_case, try_quick;
2221 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2225 #ifdef Honor_FLT_ROUNDS
2230 alloc.begin= alloc.free=
buf;
2231 alloc.end= buf + buf_size;
2232 memset(alloc.freelist, 0,
sizeof(alloc.freelist));
2235 if (word0(&u) & Sign_bit)
2239 word0(&u) &= ~Sign_bit;
2245 if (((word0(&u) & Exp_mask) == Exp_mask && (*decpt= DTOA_OVERFLOW)) ||
2246 (!dval(&u) && (*decpt= 1)))
2249 char *res= (
char*) dtoa_alloc(2, &alloc);
2257 #ifdef Honor_FLT_ROUNDS
2258 if ((rounding= Flt_Rounds) >= 2)
2261 rounding= rounding == 2 ? 0 : 2;
2268 b= d2b(&u, &be, &bbits, &alloc);
2269 if ((i= (
int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1))))
2271 dval(&d2)= dval(&u);
2272 word0(&d2) &= Frac_mask1;
2273 word0(&d2) |= Exp_11;
2305 i= bbits + be + (Bias + (P-1) - 1);
2306 x= i > 32 ? word0(&u) << (64 -
i) | word1(&u) >> (i - 32)
2307 : word1(&u) << (32 -
i);
2309 word0(&d2)-= 31*Exp_msk1;
2310 i-= (Bias + (P-1) - 1) + 1;
2313 ds= (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2315 if (ds < 0. && ds != k)
2318 if (k >= 0 && k <= Ten_pmax)
2320 if (dval(&u) < tens[k])
2347 if (mode < 0 || mode > 9)
2350 #ifdef Check_FLT_ROUNDS
2351 try_quick= Rounding == 1;
2375 ilim= ilim1= i= ndigits;
2387 s= s0= dtoa_alloc(i, &alloc);
2389 #ifdef Honor_FLT_ROUNDS
2390 if (mode > 1 && rounding != 1)
2394 if (ilim >= 0 && ilim <= Quick_max && try_quick)
2398 dval(&d2)= dval(&u);
2410 dval(&u)/= bigtens[n_bigtens-1];
2413 for (; j; j>>= 1, i++)
2425 dval(&u)*= tens[j1 & 0xf];
2426 for (j= j1 >> 4; j; j>>= 1, i++)
2431 dval(&u)*= bigtens[
i];
2435 if (k_check && dval(&u) < 1. && ilim > 0)
2444 dval(&eps)= ieps*dval(&u) + 7.;
2445 word0(&eps)-= (P-1)*Exp_msk1;
2450 if (dval(&u) > dval(&eps))
2452 if (dval(&u) < -dval(&eps))
2459 dval(&eps)= 0.5/tens[ilim-1] - dval(&eps);
2465 if (dval(&u) < dval(&eps))
2467 if (1. - dval(&u) < dval(&eps))
2478 dval(&eps)*= tens[ilim-1];
2479 for (i= 1;; i++, dval(&u)*= 10.)
2481 L= (Long)(dval(&u));
2482 if (!(dval(&u)-= L))
2487 if (dval(&u) > 0.5 + dval(&eps))
2489 else if (dval(&u) < 0.5 - dval(&eps))
2491 while (*--s ==
'0');
2501 dval(&u)= dval(&d2);
2508 if (be >= 0 && k <= Int_max)
2512 if (ndigits < 0 && ilim <= 0)
2515 if (ilim < 0 || dval(&u) <= 5*ds)
2519 for (i= 1;; i++, dval(&u)*= 10.)
2521 L= (Long)(dval(&u) / ds);
2523 #ifdef Check_FLT_ROUNDS
2538 #ifdef Honor_FLT_ROUNDS
2543 case 2:
goto bump_up;
2547 dval(&u)+= dval(&u);
2548 if (dval(&u) > ds || (dval(&u) == ds && L & 1))
2571 i = denorm ? be + (Bias + (P-1) - 1 + 1) : 1 + P - bbits;
2574 mhi= i2b(1, &alloc);
2576 if (m2 > 0 && s2 > 0)
2578 i= m2 < s2 ? m2 : s2;
2589 mhi= pow5mult(mhi, m5, &alloc);
2590 b1= mult(mhi, b, &alloc);
2595 b= pow5mult(b, j, &alloc);
2598 b= pow5mult(b, b5, &alloc);
2602 S= pow5mult(S, s5, &alloc);
2607 if ((mode < 2 || leftright)
2608 #ifdef Honor_FLT_ROUNDS
2613 if (!word1(&u) && !(word0(&u) & Bndry_mask) &&
2614 word0(&u) & (Exp_mask & ~Exp_msk1)
2632 if ((i= ((s5 ? 32 - hi0bits(S->p.x[S->wds-1]) : 1) + s2) & 0x1f))
2649 b= lshift(b, b2, &alloc);
2651 S= lshift(S, s2, &alloc);
2658 b= multadd(b, 10, 0, &alloc);
2660 mhi= multadd(mhi, 10, 0, &alloc);
2664 if (ilim <= 0 && (mode == 3 || mode == 5))
2666 if (ilim < 0 || cmp(b,S= multadd(S,5,0, &alloc)) <= 0)
2681 mhi= lshift(mhi, m2, &alloc);
2690 mhi= Balloc(mhi->k, &alloc);
2692 mhi= lshift(mhi, Log2P, &alloc);
2697 dig= quorem(b,S) +
'0';
2700 delta= diff(S, mhi, &alloc);
2701 j1= delta->sign ? 1 : cmp(b, delta);
2702 Bfree(delta, &alloc);
2703 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2704 #ifdef Honor_FLT_ROUNDS
2716 if (j < 0 || (j == 0 && mode != 1 && !(word1(&u) & 1)))
2718 if (!b->p.x[0] && b->wds <= 1)
2722 #ifdef Honor_FLT_ROUNDS
2725 case 0:
goto accept_dig;
2726 case 2:
goto keep_dig;
2731 b= lshift(b, 1, &alloc);
2733 if ((j1 > 0 || (j1 == 0 && dig & 1))
2743 #ifdef Honor_FLT_ROUNDS
2756 #ifdef Honor_FLT_ROUNDS
2762 b= multadd(b, 10, 0, &alloc);
2764 mlo= mhi= multadd(mhi, 10, 0, &alloc);
2767 mlo= multadd(mlo, 10, 0, &alloc);
2768 mhi= multadd(mhi, 10, 0, &alloc);
2775 *s++= dig= quorem(b,S) +
'0';
2776 if (!b->p.x[0] && b->wds <= 1)
2782 b= multadd(b, 10, 0, &alloc);
2787 #ifdef Honor_FLT_ROUNDS
2789 case 0:
goto trimzeros;
2790 case 2:
goto roundoff;
2793 b= lshift(b, 1, &alloc);
2795 if (j > 0 || (j == 0 && dig & 1))
2809 #ifdef Honor_FLT_ROUNDS
2812 while (*--s ==
'0');
2819 if (mlo && mlo != mhi)