Changeset 60468 in webkit for trunk/JavaScriptCore/wtf/dtoa.cpp
- Timestamp:
- Jun 1, 2010, 1:57:38 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/dtoa.cpp
r52729 r60468 143 143 #include <math.h> 144 144 #include <stdint.h> 145 #include <stdio.h> 145 146 #include <stdlib.h> 146 147 #include <string.h> … … 149 150 #include <wtf/FastMalloc.h> 150 151 #include <wtf/MathExtras.h> 152 #include <wtf/Threading.h> 151 153 #include <wtf/Vector.h> 152 #include <wtf/Threading.h>153 154 #include <stdio.h>155 154 156 155 #if COMPILER(MSVC) … … 180 179 #endif 181 180 182 typedef union { double d; uint32_t L[2]; } U; 181 typedef union { 182 double d; 183 uint32_t L[2]; 184 } U; 183 185 184 186 #ifdef YES_ALIAS … … 204 206 /* The following definition of Storeinc is appropriate for MIPS processors. 205 207 * An alternative that might be better on some machines is 206 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)208 * *p++ = high << 16 | low & 0xffff; 207 209 */ 210 static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low) 211 { 212 uint16_t* p16 = reinterpret_cast<uint16_t*>(p); 208 213 #if defined(IEEE_8087) || defined(IEEE_ARM) 209 #define Storeinc(a,b,c) (((unsigned short*)a)[1] = (unsigned short)b, ((unsigned short*)a)[0] = (unsigned short)c, a++) 210 #else 211 #define Storeinc(a,b,c) (((unsigned short*)a)[0] = (unsigned short)b, ((unsigned short*)a)[1] = (unsigned short)c, a++) 212 #endif 214 p16[1] = high; 215 p16[0] = low; 216 #else 217 p16[0] = high; 218 p16[1] = low; 219 #endif 220 return p + 1; 221 } 213 222 214 223 #define Exp_shift 20 … … 248 257 #define Flt_Rounds 1 249 258 #endif 250 #endif /* Flt_Rounds*/251 252 253 #define rounded_product(a, b) a *= b254 #define rounded_quotient(a, b) a /= b259 #endif /* Flt_Rounds */ 260 261 262 #define rounded_product(a, b) a *= b 263 #define rounded_quotient(a, b) a /= b 255 264 256 265 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) … … 414 423 } 415 424 416 static int lo0bits 425 static int lo0bits(uint32_t* y) 417 426 { 418 427 int k; … … 469 478 BigInt c; 470 479 int wa, wb, wc; 471 const uint32_t *x = 0, *xa, *xb, *xae, *xbe; 472 uint32_t *xc, *xc0; 480 const uint32_t* x = 0; 481 const uint32_t* xa; 482 const uint32_t* xb; 483 const uint32_t* xae; 484 const uint32_t* xbe; 485 uint32_t* xc; 486 uint32_t* xc0; 473 487 uint32_t y; 474 488 #ifdef USE_LONG_LONG … … 522 536 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; 523 537 carry = z2 >> 16; 524 Storeinc(xc, z2, z);538 xc = storeInc(xc, z2, z); 525 539 } while (x < xae); 526 540 *xc = carry; … … 534 548 z = (*x & 0xffff) * y + (*xc >> 16) + carry; 535 549 carry = z >> 16; 536 Storeinc(xc, z, z2);550 xc = storeInc(xc, z, z2); 537 551 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; 538 552 carry = z2 >> 16; … … 542 556 } 543 557 #else 544 for (; xb < xbe; xc0++) {558 for (; xb < xbe; xc0++) { 545 559 if ((y = *xb++)) { 546 560 x = xa; … … 568 582 569 583 static P5Node* p5s; 570 static int p5s _count;584 static int p5sCount; 571 585 572 586 static ALWAYS_INLINE void pow5mult(BigInt& b, int k) … … 591 605 p5->next = 0; 592 606 p5s = p5; 593 p5s _count = 1;594 } 595 596 int p5s _count_local = p5s_count;607 p5sCount = 1; 608 } 609 610 int p5sCountLocal = p5sCount; 597 611 #if ENABLE(JSC_MULTIPLE_THREADS) 598 612 s_dtoaP5Mutex->unlock(); 599 613 #endif 600 int p5s _used = 0;614 int p5sUsed = 0; 601 615 602 616 for (;;) { … … 607 621 break; 608 622 609 if (++p5s _used == p5s_count_local) {623 if (++p5sUsed == p5sCountLocal) { 610 624 #if ENABLE(JSC_MULTIPLE_THREADS) 611 625 s_dtoaP5Mutex->lock(); 612 626 #endif 613 if (p5s _used == p5s_count) {627 if (p5sUsed == p5sCount) { 614 628 ASSERT(!p5->next); 615 629 p5->next = new P5Node; … … 617 631 p5->next->val = p5->val; 618 632 mult(p5->next->val, p5->next->val); 619 ++p5s _count;633 ++p5sCount; 620 634 } 621 635 622 p5s _count_local = p5s_count;636 p5sCountLocal = p5sCount; 623 637 #if ENABLE(JSC_MULTIPLE_THREADS) 624 638 s_dtoaP5Mutex->unlock(); … … 660 674 ASSERT(dst == dstStart + n); 661 675 662 b.resize(origSize + n + (b.words()[n1 - 1] != 0));676 b.resize(origSize + n + !!b.words()[n1 - 1]); 663 677 } 664 678 #else … … 672 686 *dst = hiSubword; 673 687 ASSERT(dst == dstStart + n); 674 result->wds = b->wds + n + (result->x[n1 - 1] != 0);688 result->wds = b->wds + n + !!result->x[n1 - 1]; 675 689 } 676 690 #endif 677 691 else { 678 692 do { … … 715 729 const BigInt* b = &bRef; 716 730 int i, wa, wb; 717 uint32_t *xc;731 uint32_t* xc; 718 732 719 733 i = cmp(*a, *b); … … 762 776 uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; 763 777 borrow = (z & 0x10000) >> 16; 764 Storeinc(xc, z, y);778 xc = storeInc(xc, z, y); 765 779 } while (xb < xbe); 766 780 while (xa < xae) { … … 769 783 uint32_t z = (*xa++ >> 16) - borrow; 770 784 borrow = (z & 0x10000) >> 16; 771 Storeinc(xc, z, y);785 xc = storeInc(xc, z, y); 772 786 } 773 787 #else … … 844 858 w = xa > xa0 ? *--xa : 0; 845 859 d1 = (y << (32 - Ebits + k)) | (w >> (Ebits - k)); 846 goto ret _d;860 goto returnD; 847 861 } 848 862 z = xa > xa0 ? *--xa : 0; … … 862 876 y = xa > xa0 ? *--xa : 0; 863 877 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; 864 goto ret _d;878 goto returnD; 865 879 } 866 880 z = xa > xa0 ? *--xa : 0; … … 871 885 d1 = w << k + 16 | y << k; 872 886 #endif 873 ret _d:887 returnD: 874 888 #undef d0 875 889 #undef d1 … … 880 894 { 881 895 int de, k; 882 uint32_t *x, y, z; 896 uint32_t* x; 897 uint32_t y, z; 883 898 #ifndef Sudden_Underflow 884 899 int i; … … 1116 1131 sign = nz0 = nz = 0; 1117 1132 dval(&rv) = 0; 1118 for (s = s00; ; s++) 1133 for (s = s00; ; s++) { 1119 1134 switch (*s) { 1120 case '-': 1121 sign = 1; 1122 /* no break */ 1123 case '+': 1124 if (*++s) 1125 goto break2; 1126 /* no break */ 1127 case 0: 1128 goto ret0; 1129 case '\t': 1130 case '\n': 1131 case '\v': 1132 case '\f': 1133 case '\r': 1134 case ' ': 1135 continue; 1136 default: 1135 case '-': 1136 sign = 1; 1137 /* no break */ 1138 case '+': 1139 if (*++s) 1137 1140 goto break2; 1138 } 1141 /* no break */ 1142 case 0: 1143 goto ret0; 1144 case '\t': 1145 case '\n': 1146 case '\v': 1147 case '\f': 1148 case '\r': 1149 case ' ': 1150 continue; 1151 default: 1152 goto break2; 1153 } 1154 } 1139 1155 break2: 1140 1156 if (*s == '0') { … … 1161 1177 nf += nz; 1162 1178 nz = 0; 1163 goto have _dig;1164 } 1165 goto dig _done;1179 goto haveDig; 1180 } 1181 goto digDone; 1166 1182 } 1167 1183 for (; c >= '0' && c <= '9'; c = *++s) { 1168 have _dig:1184 haveDig: 1169 1185 nz++; 1170 1186 if (c -= '0') { … … 1183 1199 } 1184 1200 } 1185 dig _done:1201 digDone: 1186 1202 e = 0; 1187 1203 if (c == 'e' || c == 'E') { 1188 if (!nd && !nz && !nz0) {1204 if (!nd && !nz && !nz0) 1189 1205 goto ret0; 1190 }1191 1206 s00 = s; 1192 1207 esign = 0; 1193 1208 switch (c = *++s) { 1194 1195 1196 1197 1209 case '-': 1210 esign = 1; 1211 case '+': 1212 c = *++s; 1198 1213 } 1199 1214 if (c >= '0' && c <= '9') { … … 1223 1238 #ifdef INFNAN_CHECK 1224 1239 /* Check for Nan and Infinity */ 1225 switch (c) {1226 1227 1228 if (match(&s,"nf")) {1229 1230 if (!match(&s,"inity"))1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1240 switch (c) { 1241 case 'i': 1242 case 'I': 1243 if (match(&s, "nf")) { 1244 --s; 1245 if (!match(&s, "inity")) 1246 ++s; 1247 word0(&rv) = 0x7ff00000; 1248 word1(&rv) = 0; 1249 goto ret; 1250 } 1251 break; 1252 case 'n': 1253 case 'N': 1254 if (match(&s, "an")) { 1255 word0(&rv) = NAN_WORD0; 1256 word1(&rv) = NAN_WORD1; 1242 1257 #ifndef No_Hex_NaN 1243 1244 1245 #endif 1246 1247 1258 if (*s == '(') /*)*/ 1259 hexnan(&rv, &s); 1260 #endif 1261 goto ret; 1262 } 1248 1263 } 1249 1264 #endif /* INFNAN_CHECK */ … … 1499 1514 lshift(delta, Log2P); 1500 1515 if (cmp(delta, bs) > 0) 1501 goto drop _down;1516 goto dropDown; 1502 1517 break; 1503 1518 } 1504 if ( i == 0) {1519 if (!i) { 1505 1520 /* exactly half-way between */ 1506 1521 if (dsign) { … … 1521 1536 } 1522 1537 } else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) { 1523 drop _down:1538 dropDown: 1524 1539 /* boundary case -- decrement exponent */ 1525 1540 #ifdef Sudden_Underflow /*{{*/ … … 1593 1608 #ifdef Check_FLT_ROUNDS 1594 1609 switch (Rounding) { 1595 1596 1597 1598 1599 1600 1601 } 1602 #else 1603 if ( Flt_Rounds == 0)1610 case 2: /* towards +infinity */ 1611 aadj1 -= 0.5; 1612 break; 1613 case 0: /* towards 0 */ 1614 case 3: /* towards -infinity */ 1615 aadj1 += 0.5; 1616 } 1617 #else 1618 if (!Flt_Rounds) 1604 1619 aadj1 += 0.5; 1605 1620 #endif /*Check_FLT_ROUNDS*/ … … 1620 1635 word1(&rv) = Big1; 1621 1636 goto cont; 1622 } else1623 1637 } 1638 word0(&rv) += P * Exp_msk1; 1624 1639 } else { 1625 1640 #ifdef Avoid_Underflow … … 1644 1659 adj.d = aadj1 * ulp(&rv); 1645 1660 dval(&rv) += adj.d; 1646 if ((word0(&rv) & Exp_mask) <= P * Exp_msk1) 1647 { 1661 if ((word0(&rv) & Exp_mask) <= P * Exp_msk1) { 1648 1662 if (word0(&rv0) == Tiny0 && word1(&rv0) == Tiny1) 1649 1663 goto undfl; … … 1652 1666 goto cont; 1653 1667 } 1654 else 1655 word0(&rv) -= P * Exp_msk1; 1668 word0(&rv) -= P * Exp_msk1; 1656 1669 } else { 1657 1670 adj.d = aadj1 * ulp(&rv); … … 1694 1707 #endif 1695 1708 cont: 1696 ;1709 {} 1697 1710 } 1698 1711 #ifdef SET_INEXACT … … 1713 1726 #ifndef NO_ERRNO 1714 1727 /* try to avoid the bug of testing an 8087 register value */ 1715 if ( word0(&rv) == 0 && word1(&rv) == 0)1728 if (!word0(&rv) && !word1(&rv)) 1716 1729 errno = ERANGE; 1717 1730 #endif … … 1734 1747 { 1735 1748 size_t n; 1736 uint32_t *bx, *bxe, q, *sx, *sxe; 1749 uint32_t* bx; 1750 uint32_t* bxe; 1751 uint32_t q; 1752 uint32_t* sx; 1753 uint32_t* sxe; 1737 1754 #ifdef USE_LONG_LONG 1738 1755 unsigned long long borrow, carry, y, ys; … … 1776 1793 z = (*bx >> 16) - (zs & 0xffff) - borrow; 1777 1794 borrow = (z & 0x10000) >> 16; 1778 Storeinc(bx, z, y);1795 bx = storeInc(bx, z, y); 1779 1796 #else 1780 1797 ys = *sx++ * q + carry; … … 1816 1833 z = (*bx >> 16) - (zs & 0xffff) - borrow; 1817 1834 borrow = (z & 0x10000) >> 16; 1818 Storeinc(bx, z, y);1835 bx = storeInc(bx, z, y); 1819 1836 #else 1820 1837 ys = *sx++ + carry; … … 1893 1910 U d2, eps, u; 1894 1911 double ds; 1895 char *s, *s0; 1912 char* s; 1913 char* s0; 1896 1914 #ifdef SET_INEXACT 1897 1915 int inexact, oldinexact; … … 1906 1924 *sign = 0; 1907 1925 1908 if ((word0(&u) & Exp_mask) == Exp_mask) 1909 { 1926 if ((word0(&u) & Exp_mask) == Exp_mask) { 1910 1927 /* Infinity or NaN */ 1911 1928 *decpt = 9999; … … 2060 2077 if (k_check && dval(&u) < 1. && ilim > 0) { 2061 2078 if (ilim1 <= 0) 2062 goto fast _failed;2079 goto fastFailed; 2063 2080 ilim = ilim1; 2064 2081 k--; … … 2068 2085 dval(&eps) = (ieps * dval(&u)) + 7.; 2069 2086 word0(&eps) -= (P - 1) * Exp_msk1; 2070 if ( ilim == 0) {2087 if (!ilim) { 2071 2088 S.clear(); 2072 2089 mhi.clear(); 2073 2090 dval(&u) -= 5.; 2074 2091 if (dval(&u) > dval(&eps)) 2075 goto one _digit;2092 goto oneDigit; 2076 2093 if (dval(&u) < -dval(&eps)) 2077 goto no _digits;2078 goto fast _failed;2094 goto noDigits; 2095 goto fastFailed; 2079 2096 } 2080 2097 #ifndef No_leftright … … 2091 2108 goto ret; 2092 2109 if (1. - dval(&u) < dval(&eps)) 2093 goto bump _up;2110 goto bumpUp; 2094 2111 if (++i >= ilim) 2095 2112 break; … … 2108 2125 if (i == ilim) { 2109 2126 if (dval(&u) > 0.5 + dval(&eps)) 2110 goto bump _up;2111 elseif (dval(&u) < 0.5 - dval(&eps)) {2127 goto bumpUp; 2128 if (dval(&u) < 0.5 - dval(&eps)) { 2112 2129 while (*--s == '0') { } 2113 2130 s++; … … 2120 2137 } 2121 2138 #endif 2122 fast _failed:2139 fastFailed: 2123 2140 s = s0; 2124 2141 dval(&u) = dval(&d2); … … 2136 2153 mhi.clear(); 2137 2154 if (ilim < 0 || dval(&u) <= 5 * ds) 2138 goto no _digits;2139 goto one _digit;2155 goto noDigits; 2156 goto oneDigit; 2140 2157 } 2141 2158 for (i = 1;; i++, dval(&u) *= 10.) { … … 2159 2176 dval(&u) += dval(&u); 2160 2177 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) { 2161 bump _up:2178 bumpUp: 2162 2179 while (*--s == '9') 2163 2180 if (s == s0) { … … 2253 2270 lshift(S, s2); 2254 2271 if (k_check) { 2255 if (cmp(b, S) < 0) {2272 if (cmp(b, S) < 0) { 2256 2273 k--; 2257 2274 multadd(b, 10, 0); /* we botched the k estimate */ … … 2277 2294 2278 2295 for (i = 1;;i++) { 2279 dig = quorem(b, S) + '0';2296 dig = quorem(b, S) + '0'; 2280 2297 /* Do we yet have the shortest decimal string 2281 2298 * that will round to d? … … 2284 2301 diff(delta, S, mhi); 2285 2302 j1 = delta.sign ? 1 : cmp(b, delta); 2286 if ( j1 == 0&& !(word1(&u) & 1)) {2303 if (!j1 && !(word1(&u) & 1)) { 2287 2304 if (dig == '9') 2288 goto round _9_up;2305 goto round9up; 2289 2306 if (j > 0) 2290 2307 dig++; … … 2296 2313 goto ret; 2297 2314 } 2298 if (j < 0 || ( j == 0&& !(word1(&u) & 1))) {2315 if (j < 0 || (!j && !(word1(&u) & 1))) { 2299 2316 if (!b.words()[0] && b.size() <= 1) { 2300 2317 #ifdef SET_INEXACT 2301 2318 inexact = 0; 2302 2319 #endif 2303 goto accept _dig;2320 goto acceptDig; 2304 2321 } 2305 2322 if (j1 > 0) { 2306 2323 lshift(b, 1); 2307 2324 j1 = cmp(b, S); 2308 if ((j1 > 0 || ( j1 == 0&& (dig & 1))) && dig++ == '9')2309 goto round _9_up;2325 if ((j1 > 0 || (!j1 && (dig & 1))) && dig++ == '9') 2326 goto round9up; 2310 2327 } 2311 accept _dig:2328 acceptDig: 2312 2329 *s++ = dig; 2313 2330 goto ret; … … 2315 2332 if (j1 > 0) { 2316 2333 if (dig == '9') { /* possible if i == 1 */ 2317 round _9_up:2334 round9up: 2318 2335 *s++ = '9'; 2319 2336 goto roundoff; … … 2331 2348 } else 2332 2349 for (i = 1;; i++) { 2333 *s++ = dig = quorem(b, S) + '0';2350 *s++ = dig = quorem(b, S) + '0'; 2334 2351 if (!b.words()[0] && b.size() <= 1) { 2335 2352 #ifdef SET_INEXACT … … 2347 2364 lshift(b, 1); 2348 2365 j = cmp(b, S); 2349 if (j > 0 || ( j == 0&& (dig & 1))) {2366 if (j > 0 || (!j && (dig & 1))) { 2350 2367 roundoff: 2351 2368 while (*--s == '9') … … 2361 2378 } 2362 2379 goto ret; 2363 no _digits:2380 noDigits: 2364 2381 k = -1 - ndigits; 2365 2382 goto ret; 2366 one _digit:2383 oneDigit: 2367 2384 *s++ = '1'; 2368 2385 k++;
Note:
See TracChangeset
for help on using the changeset viewer.