Changeset 43457 in webkit for trunk/JavaScriptCore/wtf/dtoa.cpp
- Timestamp:
- May 10, 2009, 4:32:31 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/dtoa.cpp
r42262 r43457 151 151 #include <wtf/Threading.h> 152 152 153 #include <stdio.h> 154 153 155 #if COMPILER(MSVC) 154 156 #pragma warning(disable: 4244) … … 276 278 #define Kmax 15 277 279 278 struct Bigint { 279 struct Bigint* next; 280 int k, maxwds, sign, wds; 281 uint32_t x[1]; 280 struct BigInt { 281 BigInt() : sign(0), wds(0) { } 282 BigInt(const BigInt& other) : sign(other.sign), wds(other.wds) 283 { 284 for (int i = 0; i < 32; ++i) 285 x[i] = other.x[i]; 286 } 287 288 BigInt& operator=(const BigInt& other) 289 { 290 sign = other.sign; 291 wds = other.wds; 292 for (int i = 0; i < 32; ++i) 293 x[i] = other.x[i]; 294 return *this; 295 } 296 297 int sign; 298 int wds; 299 uint32_t x[32]; 282 300 }; 283 301 284 static Bigint* Balloc(int k) 285 { 286 int x = 1 << k; 287 Bigint* rv = (Bigint*)fastMalloc(sizeof(Bigint) + (x - 1)*sizeof(uint32_t)); 288 rv->k = k; 289 rv->maxwds = x; 290 rv->next = 0; 291 rv->sign = rv->wds = 0; 292 293 return rv; 294 } 295 296 static void Bfree(Bigint* v) 297 { 298 fastFree(v); 299 } 300 301 #define Bcopy(x, y) memcpy((char*)&x->sign, (char*)&y->sign, y->wds * sizeof(int32_t) + 2 * sizeof(int)) 302 303 static Bigint* multadd(Bigint* b, int m, int a) /* multiply by m and add a */ 302 static void multadd(BigInt& b, int m, int a) /* multiply by m and add a */ 304 303 { 305 304 #ifdef USE_LONG_LONG … … 309 308 #endif 310 309 311 int wds = b ->wds;312 uint32_t* x = b ->x;310 int wds = b.wds; 311 uint32_t* x = b.x; 313 312 int i = 0; 314 313 carry = a; … … 334 333 335 334 if (carry) { 336 if (wds >= b->maxwds) { 337 Bigint* b1 = Balloc(b->k + 1); 338 Bcopy(b1, b); 339 Bfree(b); 340 b = b1; 341 } 342 b->x[wds++] = (uint32_t)carry; 343 b->wds = wds; 344 } 345 return b; 335 b.x[wds++] = (uint32_t)carry; 336 b.wds = wds; 337 } 346 338 } 347 339 348 static Bigint* s2b(const char* s, int nd0, int nd, uint32_t y9)340 static void s2b(BigInt& b, const char* s, int nd0, int nd, uint32_t y9) 349 341 { 350 342 int k; … … 354 346 for (k = 0, y = 1; x > y; y <<= 1, k++) { } 355 347 #ifdef Pack_32 356 Bigint* b = Balloc(k);357 b ->x[0] = y9;358 b ->wds = 1;359 #else 360 Bigint* b = Balloc(k + 1);361 b ->x[0] = y9 & 0xffff;362 b ->wds = (b->x[1] = y9 >> 16) ? 2 : 1;348 b.sign = 0; 349 b.x[0] = y9; 350 b.wds = 1; 351 #else 352 b.sign = 0; 353 b.x[0] = y9 & 0xffff; 354 b.wds = (b->x[1] = y9 >> 16) ? 2 : 1; 363 355 #endif 364 356 … … 367 359 s += 9; 368 360 do { 369 b =multadd(b, 10, *s++ - '0');361 multadd(b, 10, *s++ - '0'); 370 362 } while (++i < nd0); 371 363 s++; … … 373 365 s += 10; 374 366 for (; i < nd; i++) 375 b = multadd(b, 10, *s++ - '0'); 376 return b; 367 multadd(b, 10, *s++ - '0'); 377 368 } 378 369 … … 447 438 } 448 439 449 static Bigint* i2b(int i)440 static void i2b(BigInt& b, int i) 450 441 { 451 Bigint* b; 452 453 b = Balloc(1); 454 b->x[0] = i; 455 b->wds = 1; 456 return b; 442 b.sign = 0; 443 b.x[0] = i; 444 b.wds = 1; 457 445 } 458 446 459 static Bigint* mult(Bigint* a, Bigint* b)447 static void mult(BigInt& aRef, const BigInt& bRef) 460 448 { 461 Bigint* c; 462 int k, wa, wb, wc; 463 uint32_t *x, *xa, *xae, *xb, *xbe, *xc, *xc0; 449 const BigInt* a = &aRef; 450 const BigInt* b = &bRef; 451 BigInt c; 452 int wa, wb, wc; 453 const uint32_t *x = 0, *xa, *xb, *xae, *xbe; 454 uint32_t *xc, *xc0; 464 455 uint32_t y; 465 456 #ifdef USE_LONG_LONG … … 470 461 471 462 if (a->wds < b->wds) { 472 c = a;463 const BigInt* tmp = a; 473 464 a = b; 474 b = c;475 } 476 k = a->k;465 b = tmp; 466 } 467 477 468 wa = a->wds; 478 469 wb = b->wds; 479 470 wc = wa + wb; 480 if (wc > a->maxwds) 481 k++; 482 c = Balloc(k); 483 for (x = c->x, xa = x + wc; x < xa; x++) 484 *x = 0; 471 472 for (xc = c.x, xa = xc + wc; xc < xa; xc++) 473 *xc = 0; 485 474 xa = a->x; 486 475 xae = xa + wa; 487 476 xb = b->x; 488 477 xbe = xb + wb; 489 xc0 = c ->x;478 xc0 = c.x; 490 479 #ifdef USE_LONG_LONG 491 480 for (; xb < xbe; xc0++) { … … 549 538 #endif 550 539 #endif 551 for (xc0 = c ->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }552 c ->wds = wc;553 returnc;540 for (xc0 = c.x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) { } 541 c.wds = wc; 542 aRef = c; 554 543 } 555 544 556 static Bigint* p5s; 545 struct P5Node { 546 BigInt val; 547 P5Node* next; 548 }; 549 550 static P5Node* p5s; 557 551 static int p5s_count; 558 552 559 static Bigint* pow5mult(Bigint*b, int k)553 static ALWAYS_INLINE void pow5mult(BigInt& b, int k) 560 554 { 561 555 static int p05[3] = { 5, 25, 125 }; 562 556 563 557 if (int i = k & 3) 564 b =multadd(b, p05[i - 1], 0);558 multadd(b, p05[i - 1], 0); 565 559 566 560 if (!(k >>= 2)) 567 return b;561 return; 568 562 569 563 #if ENABLE(JSC_MULTIPLE_THREADS) 570 564 s_dtoaP5Mutex->lock(); 571 565 #endif 572 Bigint* p5 = p5s; 566 P5Node* p5 = p5s; 567 573 568 if (!p5) { 574 569 /* first time */ 575 p5 = p5s = i2b(625); 570 p5 = new P5Node; 571 i2b(p5->val, 625); 572 p5->next = 0; 573 p5s = p5; 576 574 p5s_count = 1; 577 575 } 576 578 577 int p5s_count_local = p5s_count; 579 578 #if ENABLE(JSC_MULTIPLE_THREADS) … … 583 582 584 583 for (;;) { 585 if (k & 1) { 586 Bigint* b1 = mult(b, p5); 587 Bfree(b); 588 b = b1; 589 } 584 if (k & 1) 585 mult(b, p5->val); 586 590 587 if (!(k >>= 1)) 591 588 break; … … 597 594 if (p5s_used == p5s_count) { 598 595 ASSERT(!p5->next); 599 p5->next = mult(p5, p5); 596 p5->next = new P5Node; 597 p5->next->next = 0; 598 p5->next->val = p5->val; 599 mult(p5->next->val, p5->next->val); 600 600 ++p5s_count; 601 601 } … … 608 608 p5 = p5->next; 609 609 } 610 611 return b;612 610 } 613 611 614 static Bigint* lshift(Bigint*b, int k)612 static ALWAYS_INLINE void lshift(BigInt& b, int k) 615 613 { 616 Bigint* result = b;617 618 614 #ifdef Pack_32 619 615 int n = k >> 5; … … 622 618 #endif 623 619 624 int k1 = b->k; 625 int n1 = n + b->wds + 1; 626 for (int i = b->maxwds; n1 > i; i <<= 1) 627 k1++; 628 if (b->k < k1) 629 result = Balloc(k1); 630 631 const uint32_t* srcStart = b->x; 632 uint32_t* dstStart = result->x; 633 const uint32_t* src = srcStart + b->wds - 1; 620 int n1 = n + b.wds + 1; 621 622 const uint32_t* srcStart = b.x; 623 uint32_t* dstStart = b.x; 624 const uint32_t* src = srcStart + b.wds - 1; 634 625 uint32_t* dst = dstStart + n1 - 1; 635 626 #ifdef Pack_32 … … 643 634 *dst = hiSubword; 644 635 ASSERT(dst == dstStart + n); 645 result->wds = b->wds + n + (result->x[n1 - 1] != 0);636 b.wds = b.wds + n + (b.x[n1 - 1] != 0); 646 637 } 647 638 #else … … 662 653 *--dst = *src--; 663 654 } while (src >= srcStart); 664 result->wds = b->wds + n;655 b.wds = b.wds + n; 665 656 } 666 657 for (dst = dstStart + n; dst != dstStart; ) 667 658 *--dst = 0; 668 669 if (result != b)670 Bfree(b);671 return result;672 659 } 673 660 674 static int cmp( Bigint* a, Bigint*b)661 static int cmp(const BigInt& a, const BigInt& b) 675 662 { 676 uint32_t *xa, *xa0, *xb, *xb0;663 const uint32_t *xa, *xa0, *xb, *xb0; 677 664 int i, j; 678 665 679 i = a ->wds;680 j = b ->wds;681 ASSERT(i <= 1 || a ->x[i - 1]);682 ASSERT(j <= 1 || b ->x[j - 1]);666 i = a.wds; 667 j = b.wds; 668 ASSERT(i <= 1 || a.x[i - 1]); 669 ASSERT(j <= 1 || b.x[j - 1]); 683 670 if (i -= j) 684 671 return i; 685 xa0 = a ->x;672 xa0 = a.x; 686 673 xa = xa0 + j; 687 xb0 = b ->x;674 xb0 = b.x; 688 675 xb = xb0 + j; 689 676 for (;;) { … … 696 683 } 697 684 698 static Bigint* diff(Bigint* a, Bigint* b)685 static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef) 699 686 { 700 Bigint* c; 687 const BigInt* a = &aRef; 688 const BigInt* b = &bRef; 701 689 int i, wa, wb; 702 uint32_t *x a, *xae, *xb, *xbe, *xc;703 704 i = cmp( a,b);690 uint32_t *xc; 691 692 i = cmp(*a, *b); 705 693 if (!i) { 706 c = Balloc(0);707 c ->wds = 1;708 c ->x[0] = 0;709 return c;694 c.sign = 0; 695 c.wds = 1; 696 c.x[0] = 0; 697 return; 710 698 } 711 699 if (i < 0) { 712 c = a;700 const BigInt* tmp = a; 713 701 a = b; 714 b = c;702 b = tmp; 715 703 i = 1; 716 704 } else 717 705 i = 0; 718 c = Balloc(a->k); 719 c->sign = i; 706 707 c.wds = 0; 708 c.sign = i; 720 709 wa = a->wds; 721 xa = a->x;722 xae = xa + wa;710 const uint32_t* xa = a->x; 711 const uint32_t* xae = xa + wa; 723 712 wb = b->wds; 724 xb = b->x;725 xbe = xb + wb;726 xc = c ->x;713 const uint32_t* xb = b->x; 714 const uint32_t* xbe = xb + wb; 715 xc = c.x; 727 716 #ifdef USE_LONG_LONG 728 717 unsigned long long borrow = 0; … … 769 758 while (!*--xc) 770 759 wa--; 771 c->wds = wa; 772 return c; 760 c.wds = wa; 773 761 } 774 762 … … 804 792 } 805 793 806 static double b2d( Bigint*a, int* e)794 static double b2d(const BigInt& a, int* e) 807 795 { 808 uint32_t* xa;809 uint32_t* xa0;796 const uint32_t* xa; 797 const uint32_t* xa0; 810 798 uint32_t w; 811 799 uint32_t y; … … 817 805 #define d1 word1(&d) 818 806 819 xa0 = a ->x;820 xa = xa0 + a ->wds;807 xa0 = a.x; 808 xa = xa0 + a.wds; 821 809 y = *--xa; 822 810 ASSERT(y); … … 861 849 } 862 850 863 static Bigint* d2b(U* d, int* e, int* bits)851 static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits) 864 852 { 865 Bigint* b;866 853 int de, k; 867 854 uint32_t *x, y, z; … … 872 859 #define d1 word1(d) 873 860 861 b.sign = 0; 874 862 #ifdef Pack_32 875 b = Balloc(1);876 #else 877 b = Balloc(2);878 #endif 879 x = b ->x;863 b.wds = 1; 864 #else 865 b.wds = 2; 866 #endif 867 x = b.x; 880 868 881 869 z = d0 & Frac_mask; … … 897 885 i = 898 886 #endif 899 b ->wds = (x[1] = z) ? 2 : 1;887 b.wds = (x[1] = z) ? 2 : 1; 900 888 } else { 901 889 k = lo0bits(&z); … … 904 892 i = 905 893 #endif 906 b ->wds = 1;894 b.wds = 1; 907 895 k += 32; 908 896 } … … 959 947 } 960 948 #endif 961 return b;962 949 } 963 950 #undef d0 964 951 #undef d1 965 952 966 static double ratio( Bigint* a, Bigint*b)953 static double ratio(const BigInt& a, const BigInt& b) 967 954 { 968 955 U da, db; … … 972 959 dval(&db) = b2d(b, &kb); 973 960 #ifdef Pack_32 974 k = ka - kb + 32 * (a ->wds - b->wds);975 #else 976 k = ka - kb + 16 * (a ->wds - b->wds);961 k = ka - kb + 32 * (a.wds - b.wds); 962 #else 963 k = ka - kb + 16 * (a.wds - b.wds); 977 964 #endif 978 965 if (k > 0) … … 1090 1077 int32_t L; 1091 1078 uint32_t y, z; 1092 Big int *bb = NULL, *bb1 = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;1079 BigInt bb, bb1, bd, bd0, bs, delta; 1093 1080 #ifdef SET_INEXACT 1094 1081 int inexact, oldinexact; … … 1253 1240 dval(&rv) = tens[k - 9] * dval(&rv) + z; 1254 1241 } 1255 bd0 = 0;1256 1242 if (nd <= DBL_DIG && Flt_Rounds == 1) { 1257 1243 if (!e) … … 1310 1296 dval(&rv0) *= dval(&rv0); 1311 1297 #endif 1312 if (bd0)1313 goto retfree;1314 1298 goto ret; 1315 1299 } … … 1372 1356 errno = ERANGE; 1373 1357 #endif 1374 if (bd0)1375 goto retfree;1376 1358 goto ret; 1377 1359 } … … 1391 1373 /* Put digits into bd: true value = bd * 10^e */ 1392 1374 1393 bd0 = s2b(s0, nd0, nd, y);1375 s2b(bd0, s0, nd0, nd, y); 1394 1376 1395 1377 for (;;) { 1396 bd = Balloc(bd0->k); 1397 Bcopy(bd, bd0); 1398 bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ 1399 bs = i2b(1); 1378 bd = bd0; 1379 d2b(bb, &rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ 1380 i2b(bs, 1); 1400 1381 1401 1382 if (e >= 0) { … … 1444 1425 } 1445 1426 if (bb5 > 0) { 1446 bs = pow5mult(bs, bb5); 1447 bb1 = mult(bs, bb); 1448 Bfree(bb); 1449 bb = bb1; 1427 pow5mult(bs, bb5); 1428 mult(bb, bs); 1450 1429 } 1451 1430 if (bb2 > 0) 1452 bb =lshift(bb, bb2);1431 lshift(bb, bb2); 1453 1432 if (bd5 > 0) 1454 bd =pow5mult(bd, bd5);1433 pow5mult(bd, bd5); 1455 1434 if (bd2 > 0) 1456 bd =lshift(bd, bd2);1435 lshift(bd, bd2); 1457 1436 if (bs2 > 0) 1458 bs =lshift(bs, bs2);1459 d elta = diff(bb, bd);1460 dsign = delta ->sign;1461 delta ->sign = 0;1437 lshift(bs, bs2); 1438 diff(delta, bb, bd); 1439 dsign = delta.sign; 1440 delta.sign = 0; 1462 1441 i = cmp(delta, bs); 1463 1442 … … 1479 1458 break; 1480 1459 } 1481 if (!delta ->x[0] && delta->wds <= 1) {1460 if (!delta.x[0] && delta.wds <= 1) { 1482 1461 /* exact result */ 1483 1462 #ifdef SET_INEXACT … … 1486 1465 break; 1487 1466 } 1488 delta = lshift(delta,Log2P);1467 lshift(delta, Log2P); 1489 1468 if (cmp(delta, bs) > 0) 1490 1469 goto drop_down; … … 1683 1662 #endif 1684 1663 cont: 1685 Bfree(bb); 1686 Bfree(bd); 1687 Bfree(bs); 1688 Bfree(delta); 1664 ; 1689 1665 } 1690 1666 #ifdef SET_INEXACT … … 1717 1693 } 1718 1694 #endif 1719 retfree:1720 Bfree(bb);1721 Bfree(bd);1722 Bfree(bs);1723 Bfree(bd0);1724 Bfree(delta);1725 1695 ret: 1726 1696 if (se) … … 1729 1699 } 1730 1700 1731 static int quorem(Bigint* b, Bigint*S)1701 static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S) 1732 1702 { 1733 1703 int n; … … 1742 1712 #endif 1743 1713 1744 n = S ->wds;1745 ASSERT_WITH_MESSAGE(b ->wds <= n, "oversize b in quorem");1746 if (b ->wds < n)1714 n = S.wds; 1715 ASSERT_WITH_MESSAGE(b.wds <= n, "oversize b in quorem"); 1716 if (b.wds < n) 1747 1717 return 0; 1748 sx = S ->x;1718 sx = S.x; 1749 1719 sxe = sx + --n; 1750 bx = b ->x;1720 bx = b.x; 1751 1721 bxe = bx + n; 1752 1722 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ … … 1783 1753 } while (sx <= sxe); 1784 1754 if (!*bxe) { 1785 bx = b ->x;1755 bx = b.x; 1786 1756 while (--bxe > bx && !*bxe) 1787 1757 --n; 1788 b ->wds = n;1758 b.wds = n; 1789 1759 } 1790 1760 } … … 1793 1763 borrow = 0; 1794 1764 carry = 0; 1795 bx = b ->x;1796 sx = S ->x;1765 bx = b.x; 1766 sx = S.x; 1797 1767 do { 1798 1768 #ifdef USE_LONG_LONG … … 1822 1792 #endif 1823 1793 } while (sx <= sxe); 1824 bx = b ->x;1794 bx = b.x; 1825 1795 bxe = bx + n; 1826 1796 if (!*bxe) { 1827 1797 while (--bxe > bx && !*bxe) 1828 1798 --n; 1829 b ->wds = n;1799 b.wds = n; 1830 1800 } 1831 1801 } 1832 1802 return q; 1833 }1834 1835 #if !ENABLE(JSC_MULTIPLE_THREADS)1836 static char* dtoa_result;1837 #endif1838 1839 static char* rv_alloc(int i)1840 {1841 int k;1842 1843 int j = sizeof(uint32_t);1844 for (k = 0;1845 sizeof(Bigint) - sizeof(uint32_t) - sizeof(int) + j <= (unsigned)i;1846 j <<= 1)1847 k++;1848 int* r = (int*)Balloc(k);1849 *r = k;1850 return1851 #if !ENABLE(JSC_MULTIPLE_THREADS)1852 dtoa_result =1853 #endif1854 (char*)(r + 1);1855 }1856 1857 static char* nrv_alloc(const char* s, char** rve, int n)1858 {1859 char* rv = rv_alloc(n);1860 char* t = rv;1861 1862 while ((*t = *s++))1863 t++;1864 if (rve)1865 *rve = t;1866 return rv;1867 }1868 1869 /* freedtoa(s) must be used to free values s returned by dtoa1870 * when MULTIPLE_THREADS is #defined. It should be used in all cases,1871 * but for consistency with earlier versions of dtoa, it is optional1872 * when MULTIPLE_THREADS is not defined.1873 */1874 1875 void freedtoa(char* s)1876 {1877 Bigint* b = (Bigint*)((int*)s - 1);1878 b->maxwds = 1 << (b->k = *(int*)b);1879 Bfree(b);1880 #if !ENABLE(JSC_MULTIPLE_THREADS)1881 if (s == dtoa_result)1882 dtoa_result = 0;1883 #endif1884 1803 } 1885 1804 … … 1918 1837 */ 1919 1838 1920 char* dtoa(double dd, int ndigits, int* decpt, int* sign, char** rve)1839 void dtoa(char* result, double dd, int ndigits, int* decpt, int* sign, char** rve) 1921 1840 { 1922 1841 /* … … 1937 1856 uint32_t x; 1938 1857 #endif 1939 Big int *b, *b1, *delta, *mlo = NULL, *mhi, *S;1858 BigInt b, b1, delta, mlo, mhi, S; 1940 1859 U d2, eps, u; 1941 1860 double ds; … … 1965 1884 *decpt = 9999; 1966 1885 if (!word1(&u) && !(word0(&u) & 0xfffff)) 1967 return nrv_alloc("Infinity", rve, 8); 1968 return nrv_alloc("NaN", rve, 3); 1886 strcpy(result, "Infinity"); 1887 else 1888 strcpy(result, "NaN"); 1889 return; 1969 1890 } 1970 1891 if (!dval(&u)) { 1971 1892 *decpt = 1; 1972 return nrv_alloc("0", rve, 1); 1893 result[0] = '0'; 1894 result[1] = '\0'; 1895 return; 1973 1896 } 1974 1897 … … 1978 1901 #endif 1979 1902 1980 b = d2b(&u, &be, &bbits);1903 d2b(b, &u, &be, &bbits); 1981 1904 #ifdef Sudden_Underflow 1982 1905 i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)); … … 2065 1988 i = 18; 2066 1989 ndigits = 0; 2067 s = s0 = r v_alloc(i);1990 s = s0 = result; 2068 1991 2069 1992 if (ilim >= 0 && ilim <= Quick_max && try_quick) { … … 2112 2035 word0(&eps) -= (P - 1) * Exp_msk1; 2113 2036 if (ilim == 0) { 2114 S = mhi = 0;2037 S = mhi = BigInt(); 2115 2038 dval(&u) -= 5.; 2116 2039 if (dval(&u) > dval(&eps)) … … 2131 2054 *s++ = '0' + (int)L; 2132 2055 if (dval(&u) < dval(&eps)) 2133 goto ret 1;2056 goto ret; 2134 2057 if (1. - dval(&u) < dval(&eps)) 2135 2058 goto bump_up; … … 2154 2077 while (*--s == '0') { } 2155 2078 s++; 2156 goto ret 1;2079 goto ret; 2157 2080 } 2158 2081 break; … … 2175 2098 ds = tens[k]; 2176 2099 if (ndigits < 0 && ilim <= 0) { 2177 S = mhi = 0;2100 S = mhi = BigInt(); 2178 2101 if (ilim < 0 || dval(&u) <= 5 * ds) 2179 2102 goto no_digits; … … 2212 2135 } 2213 2136 } 2214 goto ret 1;2137 goto ret; 2215 2138 } 2216 2139 2217 2140 m2 = b2; 2218 2141 m5 = b5; 2219 mhi = mlo = 0;2142 mhi = mlo = BigInt(); 2220 2143 if (leftright) { 2221 2144 i = … … 2226 2149 b2 += i; 2227 2150 s2 += i; 2228 mhi = i2b(1);2151 i2b(mhi, 1); 2229 2152 } 2230 2153 if (m2 > 0 && s2 > 0) { … … 2237 2160 if (leftright) { 2238 2161 if (m5 > 0) { 2239 mhi = pow5mult(mhi, m5); 2240 b1 = mult(mhi, b); 2241 Bfree(b); 2242 b = b1; 2162 pow5mult(mhi, m5); 2163 mult(b, mhi); 2243 2164 } 2244 2165 if ((j = b5 - m5)) 2245 b =pow5mult(b, j);2166 pow5mult(b, j); 2246 2167 } else 2247 b =pow5mult(b, b5);2248 } 2249 S = i2b(1);2168 pow5mult(b, b5); 2169 } 2170 i2b(S, 1); 2250 2171 if (s5 > 0) 2251 S =pow5mult(S, s5);2172 pow5mult(S, s5); 2252 2173 2253 2174 /* Check for special case that d is a normalized power of 2. */ … … 2273 2194 */ 2274 2195 #ifdef Pack_32 2275 if ((i = ((s5 ? 32 - hi0bits(S ->x[S->wds - 1]) : 1) + s2) & 0x1f))2196 if ((i = ((s5 ? 32 - hi0bits(S.x[S.wds - 1]) : 1) + s2) & 0x1f)) 2276 2197 i = 32 - i; 2277 2198 #else 2278 if ((i = ((s5 ? 32 - hi0bits(S ->x[S->wds - 1]) : 1) + s2) & 0xf))2199 if ((i = ((s5 ? 32 - hi0bits(S.x[S.wds - 1]) : 1) + s2) & 0xf)) 2279 2200 i = 16 - i; 2280 2201 #endif … … 2291 2212 } 2292 2213 if (b2 > 0) 2293 b =lshift(b, b2);2214 lshift(b, b2); 2294 2215 if (s2 > 0) 2295 S =lshift(S, s2);2216 lshift(S, s2); 2296 2217 if (k_check) { 2297 2218 if (cmp(b,S) < 0) { 2298 2219 k--; 2299 b =multadd(b, 10, 0); /* we botched the k estimate */2220 multadd(b, 10, 0); /* we botched the k estimate */ 2300 2221 if (leftright) 2301 m hi = multadd(mhi, 10, 0);2222 multadd(mhi, 10, 0); 2302 2223 ilim = ilim1; 2303 2224 } … … 2306 2227 if (leftright) { 2307 2228 if (m2 > 0) 2308 mhi =lshift(mhi, m2);2229 lshift(mhi, m2); 2309 2230 2310 2231 /* Compute mlo -- check for special case … … 2314 2235 mlo = mhi; 2315 2236 if (spec_case) { 2316 mhi = Balloc(mhi->k); 2317 Bcopy(mhi, mlo); 2318 mhi = lshift(mhi, Log2P); 2237 mhi = mlo; 2238 lshift(mhi, Log2P); 2319 2239 } 2320 2240 … … 2325 2245 */ 2326 2246 j = cmp(b, mlo); 2327 delta = diff(S, mhi); 2328 j1 = delta->sign ? 1 : cmp(b, delta); 2329 Bfree(delta); 2247 diff(delta, S, mhi); 2248 j1 = delta.sign ? 1 : cmp(b, delta); 2330 2249 if (j1 == 0 && !(word1(&u) & 1)) { 2331 2250 if (dig == '9') … … 2341 2260 } 2342 2261 if (j < 0 || (j == 0 && !(word1(&u) & 1))) { 2343 if (!b ->x[0] && b->wds <= 1) {2262 if (!b.x[0] && b.wds <= 1) { 2344 2263 #ifdef SET_INEXACT 2345 2264 inexact = 0; … … 2348 2267 } 2349 2268 if (j1 > 0) { 2350 b =lshift(b, 1);2269 lshift(b, 1); 2351 2270 j1 = cmp(b, S); 2352 2271 if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ == '9') … … 2369 2288 if (i == ilim) 2370 2289 break; 2371 b = multadd(b, 10, 0); 2372 if (mlo == mhi) 2373 mlo = mhi = multadd(mhi, 10, 0); 2374 else { 2375 mlo = multadd(mlo, 10, 0); 2376 mhi = multadd(mhi, 10, 0); 2377 } 2290 multadd(b, 10, 0); 2291 multadd(mlo, 10, 0); 2292 multadd(mhi, 10, 0); 2378 2293 } 2379 2294 } else 2380 2295 for (i = 1;; i++) { 2381 2296 *s++ = dig = quorem(b,S) + '0'; 2382 if (!b ->x[0] && b->wds <= 1) {2297 if (!b.x[0] && b.wds <= 1) { 2383 2298 #ifdef SET_INEXACT 2384 2299 inexact = 0; … … 2388 2303 if (i >= ilim) 2389 2304 break; 2390 b =multadd(b, 10, 0);2305 multadd(b, 10, 0); 2391 2306 } 2392 2307 2393 2308 /* Round off last digit */ 2394 2309 2395 b =lshift(b, 1);2310 lshift(b, 1); 2396 2311 j = cmp(b, S); 2397 2312 if (j > 0 || (j == 0 && (dig & 1))) { … … 2417 2332 goto ret; 2418 2333 ret: 2419 Bfree(S);2420 if (mhi) {2421 if (mlo && mlo != mhi)2422 Bfree(mlo);2423 Bfree(mhi);2424 }2425 ret1:2426 2334 #ifdef SET_INEXACT 2427 2335 if (inexact) { … … 2434 2342 clear_inexact(); 2435 2343 #endif 2436 Bfree(b);2437 2344 *s = 0; 2438 2345 *decpt = k + 1; 2439 2346 if (rve) 2440 2347 *rve = s; 2441 return s0;2442 2348 } 2443 2349
Note:
See TracChangeset
for help on using the changeset viewer.