Changeset 31948 in webkit for trunk/JavaScriptCore/kjs/dtoa.cpp
- Timestamp:
- Apr 16, 2008, 11:41:54 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/dtoa.cpp
r31561 r31948 39 39 */ 40 40 41 /* strtod for IEEE- , VAX-, and IBM-arithmetic machines.41 /* strtod for IEEE-arithmetic machines. 42 42 * 43 43 * This strtod returns a nearest machine number to the input decimal … … 51 51 * Modifications: 52 52 * 53 * 1. We only require IEEE, IBM, or VAX double-precision 54 * arithmetic (not IEEE double-extended). 53 * 1. We only require IEEE. 55 54 * 2. We get by with floating-point arithmetic in a case that 56 55 * Clinger missed -- when we're computing d * 10^n … … 74 73 * #define IEEE_MC68k for IEEE-arithmetic machines where the most 75 74 * significant byte has the lowest address. 76 * #define IBM for IBM mainframe-style floating-point arithmetic.77 * #define VAX for VAX-style floating-point arithmetic (D_floating).78 75 * #define No_leftright to omit left-right logic in fast floating-point 79 76 * computation of dtoa. 80 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 381 * and strtod and dtoa should round accordingly.82 77 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 83 78 * and Honor_FLT_ROUNDS is not #defined. 84 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines85 * that use extended-precision instructions to compute rounded86 * products and quotients) with IBM.87 * #define ROUND_BIASED for IEEE-format with biased rounding.88 79 * #define Inaccurate_Divide for IEEE-format with correctly rounded 89 80 * products but inaccurate quotients, e.g., for Intel i860. … … 145 136 #include "dtoa.h" 146 137 138 #include <errno.h> 139 #include <float.h> 140 #include <math.h> 141 #include <stdint.h> 142 #include <stdlib.h> 143 #include <string.h> 144 #include <wtf/AlwaysInline.h> 145 #include <wtf/Assertions.h> 146 #include <wtf/FastMalloc.h> 147 #include <wtf/Threading.h> 148 147 149 #if COMPILER(MSVC) 148 150 #pragma warning(disable: 4244) … … 158 160 #define IEEE_8087 159 161 #endif 162 160 163 #define INFNAN_CHECK 161 164 162 163 164 #include <stdint.h> 165 #include <stdlib.h> 166 #include <string.h> 167 #include <wtf/Assertions.h> 168 #include <wtf/FastMalloc.h> 169 #include <wtf/Threading.h> 170 171 #undef IEEE_Arith 172 #undef Avoid_Underflow 173 #ifdef IEEE_MC68k 174 #define IEEE_Arith 175 #endif 176 #ifdef IEEE_8087 177 #define IEEE_Arith 178 #endif 179 #ifdef IEEE_ARM 180 #define IEEE_Arith 181 #endif 182 183 #include <errno.h> 184 185 #ifdef Bad_float_h 186 187 #ifdef IEEE_Arith 188 #define DBL_DIG 15 189 #define DBL_MAX_10_EXP 308 190 #define DBL_MAX_EXP 1024 191 #define FLT_RADIX 2 192 #endif /*IEEE_Arith*/ 193 194 #ifdef IBM 195 #define DBL_DIG 16 196 #define DBL_MAX_10_EXP 75 197 #define DBL_MAX_EXP 63 198 #define FLT_RADIX 16 199 #define DBL_MAX 7.2370055773322621e+75 200 #endif 201 202 #ifdef VAX 203 #define DBL_DIG 16 204 #define DBL_MAX_10_EXP 38 205 #define DBL_MAX_EXP 127 206 #define FLT_RADIX 2 207 #define DBL_MAX 1.7014118346046923e+38 208 #endif 209 210 #ifndef LONG_MAX 211 #define LONG_MAX 2147483647 212 #endif 213 214 #else /* ifndef Bad_float_h */ 215 #include <float.h> 216 #endif /* Bad_float_h */ 217 218 #ifndef __MATH_H__ 219 #include <math.h> 220 #endif 221 222 using namespace KJS; 165 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1 166 Exactly one of IEEE_8087, IEEE_ARM or IEEE_MC68k should be defined. 167 #endif 168 169 namespace KJS { 223 170 224 171 #if USE(MULTIPLE_THREADS) 225 Mutex* KJS::s_dtoaP5Mutex; 226 #endif 227 228 #ifdef __cplusplus 229 extern "C" { 230 #endif 231 232 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) + defined(VAX) + defined(IBM) != 1 233 Exactly one of IEEE_8087, IEEE_ARM, IEEE_MC68k, VAX, or IBM should be defined. 172 Mutex* s_dtoaP5Mutex; 234 173 #endif 235 174 … … 260 199 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) 261 200 */ 262 #if defined(IEEE_8087) + defined(IEEE_ARM) + defined(VAX)201 #if defined(IEEE_8087) || defined(IEEE_ARM) 263 202 #define Storeinc(a,b,c) (((unsigned short*)a)[1] = (unsigned short)b, ((unsigned short*)a)[0] = (unsigned short)c, a++) 264 203 #else … … 266 205 #endif 267 206 268 #ifdef IEEE_Arith269 207 #define Exp_shift 20 270 208 #define Exp_shift1 20 … … 291 229 #define Quick_max 14 292 230 #define Int_max 14 293 #ifndef NO_IEEE_Scale 231 232 #if !defined(NO_IEEE_Scale) 233 #undef Avoid_Underflow 294 234 #define Avoid_Underflow 295 #ifdef Flush_Denorm /* debugging option */ 296 #undef Sudden_Underflow 297 #endif 298 #endif 299 300 #ifndef Flt_Rounds 301 #ifdef FLT_ROUNDS 235 #endif 236 237 #if !defined(Flt_Rounds) 238 #if defined(FLT_ROUNDS) 302 239 #define Flt_Rounds FLT_ROUNDS 303 240 #else … … 306 243 #endif /*Flt_Rounds*/ 307 244 308 #ifdef Honor_FLT_ROUNDS 309 #define Rounding rounding 310 #undef Check_FLT_ROUNDS 311 #define Check_FLT_ROUNDS 312 #else 313 #define Rounding Flt_Rounds 314 #endif 315 316 #else /* ifndef IEEE_Arith */ 317 #undef Check_FLT_ROUNDS 318 #undef Honor_FLT_ROUNDS 319 #undef SET_INEXACT 320 #undef Sudden_Underflow 321 #define Sudden_Underflow 322 #ifdef IBM 323 #undef Flt_Rounds 324 #define Flt_Rounds 0 325 #define Exp_shift 24 326 #define Exp_shift1 24 327 #define Exp_msk1 0x1000000 328 #define Exp_msk11 0x1000000 329 #define Exp_mask 0x7f000000 330 #define P 14 331 #define Bias 65 332 #define Exp_1 0x41000000 333 #define Exp_11 0x41000000 334 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ 335 #define Frac_mask 0xffffff 336 #define Frac_mask1 0xffffff 337 #define Bletch 4 338 #define Ten_pmax 22 339 #define Bndry_mask 0xefffff 340 #define Bndry_mask1 0xffffff 341 #define LSB 1 342 #define Sign_bit 0x80000000 343 #define Log2P 4 344 #define Tiny0 0x100000 345 #define Tiny1 0 346 #define Quick_max 14 347 #define Int_max 15 348 #else /* VAX */ 349 #undef Flt_Rounds 350 #define Flt_Rounds 1 351 #define Exp_shift 23 352 #define Exp_shift1 7 353 #define Exp_msk1 0x80 354 #define Exp_msk11 0x800000 355 #define Exp_mask 0x7f80 356 #define P 56 357 #define Bias 129 358 #define Exp_1 0x40800000 359 #define Exp_11 0x4080 360 #define Ebits 8 361 #define Frac_mask 0x7fffff 362 #define Frac_mask1 0xffff007f 363 #define Ten_pmax 24 364 #define Bletch 2 365 #define Bndry_mask 0xffff007f 366 #define Bndry_mask1 0xffff007f 367 #define LSB 0x10000 368 #define Sign_bit 0x8000 369 #define Log2P 1 370 #define Tiny0 0x80 371 #define Tiny1 0 372 #define Quick_max 15 373 #define Int_max 15 374 #endif /* IBM, VAX */ 375 #endif /* IEEE_Arith */ 376 377 #ifndef IEEE_Arith 378 #define ROUND_BIASED 379 #endif 380 381 #ifdef RND_PRODQUOT 382 #define rounded_product(a,b) a = rnd_prod(a, b) 383 #define rounded_quotient(a,b) a = rnd_quot(a, b) 384 extern double rnd_prod(double, double), rnd_quot(double, double); 385 #else 245 386 246 #define rounded_product(a,b) a *= b 387 247 #define rounded_quotient(a,b) a /= b 388 #endif389 248 390 249 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) … … 921 780 #endif 922 781 #endif 923 #ifdef IBM924 L |= Exp_msk1 >> 4;925 #endif926 782 word0(a) = L; 927 783 word1(a) = 0; … … 953 809 int k; 954 810 double d; 955 #ifdef VAX 956 uint32_t d0, d1; 957 #else 811 958 812 #define d0 word0(d) 959 813 #define d1 word1(d) 960 #endif961 814 962 815 xa0 = a->x; … … 999 852 #endif 1000 853 ret_d: 1001 #ifdef VAX1002 word0(d) = d0 >> 16 | d0 << 16;1003 word1(d) = d1 >> 16 | d1 << 16;1004 #else1005 854 #undef d0 1006 855 #undef d1 1007 #endif1008 856 return dval(d); 1009 857 } … … 1017 865 int i; 1018 866 #endif 1019 #ifdef VAX1020 uint32_t d0, d1;1021 d0 = word0(d) >> 16 | word0(d) << 16;1022 d1 = word1(d) >> 16 | word1(d) << 16;1023 #else1024 867 #define d0 word0(d) 1025 868 #define d1 word1(d) 1026 #endif1027 869 1028 870 #ifdef Pack_32 … … 1037 879 #ifdef Sudden_Underflow 1038 880 de = (int)(d0 >> Exp_shift); 1039 #ifndef IBM1040 z |= Exp_msk11;1041 #endif1042 881 #else 1043 882 if ((de = (int)(d0 >> Exp_shift))) … … 1104 943 if (de) { 1105 944 #endif 1106 #ifdef IBM1107 *e = (de - Bias - (P - 1) << 2) + k;1108 *bits = (4 * P) + 8 - k - hi0bits(word0(d) & Frac_mask);1109 #else1110 945 *e = de - Bias - (P - 1) + k; 1111 946 *bits = P - k; 1112 #endif1113 947 #ifndef Sudden_Underflow 1114 948 } else { … … 1138 972 k = ka - kb + 16 * (a->wds - b->wds); 1139 973 #endif 1140 #ifdef IBM1141 if (k > 0) {1142 word0(da) += (k >> 2) * Exp_msk1;1143 if (k &= 3)1144 dval(da) *= 1 << k;1145 } else {1146 k = -k;1147 word0(db) += (k >> 2) * Exp_msk1;1148 if (k &= 3)1149 dval(db) *= 1 << k;1150 }1151 #else1152 974 if (k > 0) 1153 975 word0(da) += k * Exp_msk1; … … 1156 978 word0(db) += k * Exp_msk1; 1157 979 } 1158 #endif1159 980 return dval(da) / dval(db); 1160 981 } … … 1164 985 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1165 986 1e20, 1e21, 1e22 1166 #ifdef VAX1167 , 1e23, 1e241168 #endif1169 987 }; 1170 988 1171 static const double 1172 #ifdef IEEE_Arith 1173 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; 989 static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; 1174 990 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1175 991 #ifdef Avoid_Underflow … … 1185 1001 #define Scale_Bit 0x10 1186 1002 #define n_bigtens 5 1187 #else 1188 #ifdef IBM 1189 bigtens[] = { 1e16, 1e32, 1e64 }; 1190 static const double tinytens[] = { 1e-16, 1e-32, 1e-64 }; 1191 #define n_bigtens 3 1192 #else 1193 bigtens[] = { 1e16, 1e32 }; 1194 static const double tinytens[] = { 1e-16, 1e-32 }; 1195 #define n_bigtens 2 1196 #endif 1197 #endif 1198 1199 #ifndef IEEE_Arith 1200 #undef INFNAN_CHECK 1201 #endif 1202 1203 #ifdef INFNAN_CHECK 1003 1004 #if defined(INFNAN_CHECK) 1204 1005 1205 1006 #ifndef NAN_WORD0 … … 1273 1074 #endif /* INFNAN_CHECK */ 1274 1075 1275 double kjs_strtod(const char* s00, char** se)1076 double strtod(const char* s00, char** se) 1276 1077 { 1277 1078 #ifdef Avoid_Underflow … … 1287 1088 #ifdef SET_INEXACT 1288 1089 int inexact, oldinexact; 1289 #endif1290 #ifdef Honor_FLT_ROUNDS1291 int rounding;1292 1090 #endif 1293 1091 … … 1451 1249 } 1452 1250 bd0 = 0; 1453 if (nd <= DBL_DIG 1454 #ifndef RND_PRODQUOT 1455 #ifndef Honor_FLT_ROUNDS 1456 && Flt_Rounds == 1 1457 #endif 1458 #endif 1459 ) { 1251 if (nd <= DBL_DIG && Flt_Rounds == 1) { 1460 1252 if (!e) 1461 1253 goto ret; 1462 1254 if (e > 0) { 1463 1255 if (e <= Ten_pmax) { 1464 #ifdef VAX1465 goto vax_ovfl_check;1466 #else1467 #ifdef Honor_FLT_ROUNDS1468 /* round correctly FLT_ROUNDS = 2 or 3 */1469 if (sign) {1470 rv = -rv;1471 sign = 0;1472 }1473 #endif1474 1256 /* rv = */ rounded_product(dval(rv), tens[e]); 1475 1257 goto ret; 1476 #endif1477 1258 } 1478 1259 i = DBL_DIG - nd; … … 1481 1262 * this for larger i values. 1482 1263 */ 1483 #ifdef Honor_FLT_ROUNDS1484 /* round correctly FLT_ROUNDS = 2 or 3 */1485 if (sign) {1486 rv = -rv;1487 sign = 0;1488 }1489 #endif1490 1264 e -= i; 1491 1265 dval(rv) *= tens[i]; 1492 #ifdef VAX1493 /* VAX exponent range is so narrow we must1494 * worry about overflow here...1495 */1496 vax_ovfl_check:1497 word0(rv) -= P * Exp_msk1;1498 1266 /* rv = */ rounded_product(dval(rv), tens[e]); 1499 if ((word0(rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P))1500 goto ovfl;1501 word0(rv) += P * Exp_msk1;1502 #else1503 /* rv = */ rounded_product(dval(rv), tens[e]);1504 #endif1505 1267 goto ret; 1506 1268 } … … 1508 1270 #ifndef Inaccurate_Divide 1509 1271 else if (e >= -Ten_pmax) { 1510 #ifdef Honor_FLT_ROUNDS1511 /* round correctly FLT_ROUNDS = 2 or 3 */1512 if (sign) {1513 rv = -rv;1514 sign = 0;1515 }1516 #endif1517 1272 /* rv = */ rounded_quotient(dval(rv), tens[-e]); 1518 1273 goto ret; … … 1522 1277 e1 += nd - k; 1523 1278 1524 #ifdef IEEE_Arith1525 1279 #ifdef SET_INEXACT 1526 1280 inexact = 1; … … 1531 1285 scale = 0; 1532 1286 #endif 1533 #ifdef Honor_FLT_ROUNDS1534 if ((rounding = Flt_Rounds) >= 2) {1535 if (sign)1536 rounding = rounding == 2 ? 0 : 2;1537 else1538 if (rounding != 2)1539 rounding = 0;1540 }1541 #endif1542 #endif /*IEEE_Arith*/1543 1287 1544 1288 /* Get starting approximation = rv * 10**e1 */ … … 1554 1298 #endif 1555 1299 /* Can't trust HUGE_VAL */ 1556 #ifdef IEEE_Arith1557 #ifdef Honor_FLT_ROUNDS1558 switch (rounding) {1559 case 0: /* toward 0 */1560 case 3: /* toward -infinity */1561 word0(rv) = Big0;1562 word1(rv) = Big1;1563 break;1564 default:1565 word0(rv) = Exp_mask;1566 word1(rv) = 0;1567 }1568 #else /*Honor_FLT_ROUNDS*/1569 1300 word0(rv) = Exp_mask; 1570 1301 word1(rv) = 0; 1571 #endif /*Honor_FLT_ROUNDS*/1572 1302 #ifdef SET_INEXACT 1573 1303 /* set overflow bit */ … … 1575 1305 dval(rv0) *= dval(rv0); 1576 1306 #endif 1577 #else /*IEEE_Arith*/1578 word0(rv) = Big0;1579 word1(rv) = Big1;1580 #endif /*IEEE_Arith*/1581 1307 if (bd0) 1582 1308 goto retfree; … … 1680 1406 bd2 -= bbe; 1681 1407 bs2 = bb2; 1682 #ifdef Honor_FLT_ROUNDS1683 if (rounding != 1)1684 bs2++;1685 #endif1686 1408 #ifdef Avoid_Underflow 1687 1409 j = bbe - scale; … … 1693 1415 #else /*Avoid_Underflow*/ 1694 1416 #ifdef Sudden_Underflow 1695 #ifdef IBM1696 j = 1 + (4 * P) - 3 - bbbits + ((bbe + bbbits - 1) & 3);1697 #else1698 1417 j = P + 1 - bbbits; 1699 #endif1700 1418 #else /*Sudden_Underflow*/ 1701 1419 j = bbe; … … 1738 1456 delta->sign = 0; 1739 1457 i = cmp(delta, bs); 1740 #ifdef Honor_FLT_ROUNDS1741 if (rounding != 1) {1742 if (i < 0) {1743 /* Error is less than an ulp */1744 if (!delta->x[0] && delta->wds <= 1) {1745 /* exact */1746 #ifdef SET_INEXACT1747 inexact = 0;1748 #endif1749 break;1750 }1751 if (rounding) {1752 if (dsign) {1753 adj = 1.;1754 goto apply_adj;1755 }1756 } else if (!dsign) {1757 adj = -1.;1758 if (!word1(rv) && !(word0(rv) & Frac_mask)) {1759 y = word0(rv) & Exp_mask;1760 #ifdef Avoid_Underflow1761 if (!scale || y > 2 * P * Exp_msk1)1762 #else1763 if (y)1764 #endif1765 {1766 delta = lshift(delta, Log2P);1767 if (cmp(delta, bs) <= 0)1768 adj = -0.5;1769 }1770 }1771 apply_adj:1772 #ifdef Avoid_Underflow1773 if (scale && (y = word0(rv) & Exp_mask) <= 2 * P * Exp_msk1)1774 word0(adj) += (2 * P + 1) * Exp_msk1 - y;1775 #else1776 #ifdef Sudden_Underflow1777 if ((word0(rv) & Exp_mask) <= P * Exp_msk1) {1778 word0(rv) += P * Exp_msk1;1779 dval(rv) += adj * ulp(dval(rv));1780 word0(rv) -= P * Exp_msk1;1781 } else1782 #endif /*Sudden_Underflow*/1783 #endif /*Avoid_Underflow*/1784 dval(rv) += adj * ulp(dval(rv));1785 }1786 break;1787 }1788 adj = ratio(delta, bs);1789 if (adj < 1.)1790 adj = 1.;1791 if (adj <= 0x7ffffffe) {1792 y = adj;1793 if (y != adj) {1794 if (!((rounding >> 1) ^ dsign))1795 y++;1796 adj = y;1797 }1798 }1799 #ifdef Avoid_Underflow1800 if (scale && (y = word0(rv) & Exp_mask) <= 2 * P * Exp_msk1)1801 word0(adj) += (2 * P + 1) * Exp_msk1 - y;1802 #else1803 #ifdef Sudden_Underflow1804 if ((word0(rv) & Exp_mask) <= P * Exp_msk1) {1805 word0(rv) += P * Exp_msk1;1806 adj *= ulp(dval(rv));1807 if (dsign)1808 dval(rv) += adj;1809 else1810 dval(rv) -= adj;1811 word0(rv) -= P * Exp_msk1;1812 goto cont;1813 }1814 #endif /*Sudden_Underflow*/1815 #endif /*Avoid_Underflow*/1816 adj *= ulp(dval(rv));1817 if (dsign)1818 dval(rv) += adj;1819 else1820 dval(rv) -= adj;1821 goto cont;1822 }1823 #endif /*Honor_FLT_ROUNDS*/1824 1458 1825 1459 if (i < 0) { … … 1828 1462 */ 1829 1463 if (dsign || word1(rv) || word0(rv) & Bndry_mask 1830 #ifdef IEEE_Arith1831 1464 #ifdef Avoid_Underflow 1832 1465 || (word0(rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1 1833 1466 #else 1834 1467 || (word0(rv) & Exp_mask) <= Exp_msk1 1835 #endif1836 1468 #endif 1837 1469 ) { … … 1865 1497 0xffffffff)) { 1866 1498 /*boundary case -- increment exponent*/ 1867 word0(rv) = (word0(rv) & Exp_mask) 1868 + Exp_msk1 1869 #ifdef IBM 1870 | Exp_msk1 >> 4 1871 #endif 1872 ; 1499 word0(rv) = (word0(rv) & Exp_mask) + Exp_msk1; 1873 1500 word1(rv) = 0; 1874 1501 #ifdef Avoid_Underflow … … 1882 1509 #ifdef Sudden_Underflow /*{{*/ 1883 1510 L = word0(rv) & Exp_mask; 1884 #ifdef IBM1885 if (L < Exp_msk1)1886 #else1887 1511 #ifdef Avoid_Underflow 1888 1512 if (L <= (scale ? (2 * P + 1) * Exp_msk1 : Exp_msk1)) … … 1890 1514 if (L <= Exp_msk1) 1891 1515 #endif /*Avoid_Underflow*/ 1892 #endif /*IBM*/1893 1516 goto undfl; 1894 1517 L -= Exp_msk1; … … 1911 1534 word0(rv) = L | Bndry_mask1; 1912 1535 word1(rv) = 0xffffffff; 1913 #ifdef IBM1914 goto cont;1915 #else1916 1536 break; 1917 #endif 1918 } 1919 #ifndef ROUND_BIASED 1537 } 1920 1538 if (!(word1(rv) & LSB)) 1921 1539 break; 1922 #endif1923 1540 if (dsign) 1924 1541 dval(rv) += ulp(dval(rv)); 1925 #ifndef ROUND_BIASED1926 1542 else { 1927 1543 dval(rv) -= ulp(dval(rv)); … … 1933 1549 #ifdef Avoid_Underflow 1934 1550 dsign = 1 - dsign; 1935 #endif1936 1551 #endif 1937 1552 break; … … 2011 1626 adj = aadj1 * ulp(dval(rv)); 2012 1627 dval(rv) += adj; 2013 #ifdef IBM2014 if ((word0(rv) & Exp_mask) < P * Exp_msk1)2015 #else2016 1628 if ((word0(rv) & Exp_mask) <= P * Exp_msk1) 2017 #endif2018 1629 { 2019 1630 if (word0(rv0) == Tiny0 && word1(rv0) == Tiny1) … … 2255 1866 */ 2256 1867 2257 void kjs_freedtoa(char* s)1868 void freedtoa(char* s) 2258 1869 { 2259 1870 Bigint* b = (Bigint*)((int*)s - 1); … … 2300 1911 */ 2301 1912 2302 char* kjs_dtoa(double d, int mode, int ndigits, int* decpt, int* sign, char** rve)1913 char* dtoa(double d, int ndigits, int* decpt, int* sign, char** rve) 2303 1914 { 2304 /* Arguments ndigits, decpt, sign are similar to those 1915 /* 1916 Arguments ndigits, decpt, sign are similar to those 2305 1917 of ecvt and fcvt; trailing zeros are suppressed from 2306 1918 the returned string. If not null, *rve is set to point … … 2308 1920 then *decpt is set to 9999. 2309 1921 2310 mode:2311 0 ==> shortest string that yields d when read in2312 and rounded to nearest.2313 1 ==> like 0, but with Steele & White stopping rule;2314 e.g. with IEEE P754 arithmetic , mode 0 gives2315 1e23 whereas mode 1 gives 9.999999999999999e22.2316 2 ==> max(1,ndigits) significant digits. This gives a2317 return value similar to that of ecvt, except2318 that trailing zeros are suppressed.2319 3 ==> through ndigits past the decimal point. This2320 gives a return value similar to that from fcvt,2321 except that trailing zeros are suppressed, and2322 ndigits can be negative.2323 4,5 ==> similar to 2 and 3, respectively, but (in2324 round-nearest mode) with the tests of mode 0 to2325 possibly return a shorter string that rounds to d.2326 With IEEE arithmetic and compilation with2327 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same2328 as modes 2 and 3 when FLT_ROUNDS != 1.2329 6-9 ==> Debugging modes similar to mode - 4: don't try2330 fast floating-point estimate (if applicable).2331 2332 Values of mode other than 0-9 are treated as mode 0.2333 2334 Sufficient space is allocated to the return value2335 to hold the suppressed trailing zeros.2336 1922 */ 2337 1923 … … 2347 1933 double d2, ds, eps; 2348 1934 char *s, *s0; 2349 #ifdef Honor_FLT_ROUNDS2350 int rounding;2351 #endif2352 1935 #ifdef SET_INEXACT 2353 1936 int inexact, oldinexact; … … 2356 1939 #if !USE(MULTIPLE_THREADS) 2357 1940 if (dtoa_result) { 2358 kjs_freedtoa(dtoa_result);1941 freedtoa(dtoa_result); 2359 1942 dtoa_result = 0; 2360 1943 } … … 2368 1951 *sign = 0; 2369 1952 2370 #if defined(IEEE_Arith) + defined(VAX)2371 #ifdef IEEE_Arith2372 1953 if ((word0(d) & Exp_mask) == Exp_mask) 2373 #else2374 if (word0(d) == 0x8000)2375 #endif2376 1954 { 2377 1955 /* Infinity or NaN */ 2378 1956 *decpt = 9999; 2379 #ifdef IEEE_Arith2380 1957 if (!word1(d) && !(word0(d) & 0xfffff)) 2381 1958 return nrv_alloc("Infinity", rve, 8); 2382 #endif2383 1959 return nrv_alloc("NaN", rve, 3); 2384 1960 } 2385 #endif2386 #ifdef IBM2387 dval(d) += 0; /* normalize */2388 #endif2389 1961 if (!dval(d)) { 2390 1962 *decpt = 1; … … 2396 1968 inexact = 1; 2397 1969 #endif 2398 #ifdef Honor_FLT_ROUNDS2399 if ((rounding = Flt_Rounds) >= 2) {2400 if (*sign)2401 rounding = rounding == 2 ? 0 : 2;2402 else if (rounding != 2)2403 rounding = 0;2404 }2405 #endif2406 1970 2407 1971 b = d2b(dval(d), &be, &bbits); … … 2414 1978 word0(d2) &= Frac_mask1; 2415 1979 word0(d2) |= Exp_11; 2416 #ifdef IBM2417 if (j = 11 - hi0bits(word0(d2) & Frac_mask))2418 dval(d2) /= 1 << j;2419 #endif2420 1980 2421 1981 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 … … 2442 2002 2443 2003 i -= Bias; 2444 #ifdef IBM2445 i <<= 2;2446 i += j;2447 #endif2448 2004 #ifndef Sudden_Underflow 2449 2005 denorm = 0; … … 2487 2043 s5 = 0; 2488 2044 } 2489 if (mode < 0 || mode > 9)2490 mode = 0;2491 2045 2492 2046 #ifndef SET_INEXACT … … 2498 2052 #endif /*SET_INEXACT*/ 2499 2053 2500 if (mode > 5) {2501 mode -= 4;2502 try_quick = 0;2503 }2504 2054 leftright = 1; 2505 switch (mode) { 2506 case 0: 2507 case 1: 2508 ilim = ilim1 = -1; 2509 i = 18; 2510 ndigits = 0; 2511 break; 2512 case 2: 2513 leftright = 0; 2514 /* no break */ 2515 case 4: 2516 if (ndigits <= 0) 2517 ndigits = 1; 2518 ilim = ilim1 = i = ndigits; 2519 break; 2520 case 3: 2521 leftright = 0; 2522 /* no break */ 2523 case 5: 2524 i = ndigits + k + 1; 2525 ilim = i; 2526 ilim1 = i - 1; 2527 if (i <= 0) 2528 i = 1; 2529 } 2055 ilim = ilim1 = -1; 2056 i = 18; 2057 ndigits = 0; 2530 2058 s = s0 = rv_alloc(i); 2531 2532 #ifdef Honor_FLT_ROUNDS2533 if (mode > 1 && rounding != 1)2534 leftright = 0;2535 #endif2536 2059 2537 2060 if (ilim >= 0 && ilim <= Quick_max && try_quick) { … … 2666 2189 } 2667 2190 if (i == ilim) { 2668 #ifdef Honor_FLT_ROUNDS2669 if (mode > 1) {2670 switch (rounding) {2671 case 0:2672 goto ret1;2673 case 2:2674 goto bump_up;2675 }2676 }2677 #endif2678 2191 dval(d) += dval(d); 2679 2192 if (dval(d) > ds || dval(d) == ds && L & 1) { … … 2701 2214 denorm ? be + (Bias + (P - 1) - 1 + 1) : 2702 2215 #endif 2703 #ifdef IBM2704 1 + (4 * P) - 3 - bbits + ((bbits + be - 1) & 3);2705 #else2706 2216 1 + P - bbits; 2707 #endif2708 2217 b2 += i; 2709 2218 s2 += i; … … 2736 2245 2737 2246 spec_case = 0; 2738 if ((mode < 2 || leftright) 2739 #ifdef Honor_FLT_ROUNDS 2740 && rounding == 1 2741 #endif 2742 ) { 2743 if (!word1(d) && !(word0(d) & Bndry_mask) 2247 if (!word1(d) && !(word0(d) & Bndry_mask) 2744 2248 #ifndef Sudden_Underflow 2745 && word0(d) & (Exp_mask & ~Exp_msk1) 2746 #endif 2747 ) { 2748 /* The special case */ 2749 b2 += Log2P; 2750 s2 += Log2P; 2751 spec_case = 1; 2752 } 2249 && word0(d) & (Exp_mask & ~Exp_msk1) 2250 #endif 2251 ) { 2252 /* The special case */ 2253 b2 += Log2P; 2254 s2 += Log2P; 2255 spec_case = 1; 2753 2256 } 2754 2257 … … 2791 2294 } 2792 2295 } 2793 if (ilim <= 0 && (mode == 3 || mode == 5)) { 2794 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { 2795 /* no digits, fcvt style */ 2796 no_digits: 2797 k = -1 - ndigits; 2798 goto ret; 2799 } 2800 one_digit: 2801 *s++ = '1'; 2802 k++; 2803 goto ret; 2804 } 2296 2805 2297 if (leftright) { 2806 2298 if (m2 > 0) … … 2827 2319 j1 = delta->sign ? 1 : cmp(b, delta); 2828 2320 Bfree(delta); 2829 #ifndef ROUND_BIASED 2830 if (j1 == 0 && mode != 1 && !(word1(d) & 1) 2831 #ifdef Honor_FLT_ROUNDS 2832 && rounding >= 1 2833 #endif 2834 ) { 2321 if (j1 == 0 && !(word1(d) & 1)) { 2835 2322 if (dig == '9') 2836 2323 goto round_9_up; … … 2844 2331 goto ret; 2845 2332 } 2846 #endif 2847 if (j < 0 || j == 0 && mode != 1 2848 #ifndef ROUND_BIASED 2849 && !(word1(d) & 1) 2850 #endif 2851 ) { 2333 if (j < 0 || j == 0 && !(word1(d) & 1)) { 2852 2334 if (!b->x[0] && b->wds <= 1) { 2853 2335 #ifdef SET_INEXACT … … 2856 2338 goto accept_dig; 2857 2339 } 2858 #ifdef Honor_FLT_ROUNDS2859 if (mode > 1) {2860 switch (rounding) {2861 case 0:2862 goto accept_dig;2863 case 2:2864 goto keep_dig;2865 }2866 }2867 #endif /*Honor_FLT_ROUNDS*/2868 2340 if (j1 > 0) { 2869 2341 b = lshift(b, 1); … … 2877 2349 } 2878 2350 if (j1 > 0) { 2879 #ifdef Honor_FLT_ROUNDS2880 if (!rounding)2881 goto accept_dig;2882 #endif2883 2351 if (dig == '9') { /* possible if i == 1 */ 2884 2352 round_9_up: … … 2889 2357 goto ret; 2890 2358 } 2891 #ifdef Honor_FLT_ROUNDS2892 keep_dig:2893 #endif2894 2359 *s++ = dig; 2895 2360 if (i == ilim) … … 2919 2384 /* Round off last digit */ 2920 2385 2921 #ifdef Honor_FLT_ROUNDS2922 switch (rounding) {2923 case 0:2924 goto trimzeros;2925 case 2:2926 goto roundoff;2927 }2928 #endif2929 2386 b = lshift(b, 1); 2930 2387 j = cmp(b, S); … … 2939 2396 ++*s++; 2940 2397 } else { 2941 #ifdef Honor_FLT_ROUNDS2942 trimzeros:2943 #endif2944 2398 while (*--s == '0') { } 2945 2399 s++; 2946 2400 } 2401 goto ret; 2402 no_digits: 2403 k = -1 - ndigits; 2404 goto ret; 2405 one_digit: 2406 *s++ = '1'; 2407 k++; 2408 goto ret; 2947 2409 ret: 2948 2410 Bfree(S); … … 2971 2433 } 2972 2434 2973 #ifdef __cplusplus 2974 } 2975 #endif 2435 } // namespace KJS
Note:
See TracChangeset
for help on using the changeset viewer.