Changeset 43457 in webkit for trunk/JavaScriptCore/wtf/dtoa.cpp


Ignore:
Timestamp:
May 10, 2009, 4:32:31 AM (16 years ago)
Author:
[email protected]
Message:

2009-05-10 Maciej Stachowiak <[email protected]>

Reviewed by Alexey Proskuryakov.


  • speedup dtoa/strtod


Added a bunch of inlining, and replaced malloc with stack allocation.


0.5% SunSpider speedup (7% on string-tagcloud).

  • runtime/NumberPrototype.cpp: (JSC::integerPartNoExp): (JSC::numberProtoFuncToExponential):
  • runtime/UString.cpp: (JSC::concatenate): (JSC::UString::from):
  • wtf/dtoa.cpp: (WTF::BigInt::BigInt): (WTF::BigInt::operator=): (WTF::Balloc): (WTF::Bfree): (WTF::multadd): (WTF::s2b): (WTF::i2b): (WTF::mult): (WTF::pow5mult): (WTF::lshift): (WTF::cmp): (WTF::diff): (WTF::b2d): (WTF::d2b): (WTF::ratio): (WTF::strtod): (WTF::quorem): (WTF::freedtoa): (WTF::dtoa):
  • wtf/dtoa.h:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/dtoa.cpp

    r42262 r43457  
    151151#include <wtf/Threading.h>
    152152
     153#include <stdio.h>
     154
    153155#if COMPILER(MSVC)
    154156#pragma warning(disable: 4244)
     
    276278#define Kmax 15
    277279
    278 struct Bigint {
    279     struct Bigint* next;
    280     int k, maxwds, sign, wds;
    281     uint32_t x[1];
     280struct 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];
    282300};
    283301
    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 */
     302static void multadd(BigInt& b, int m, int a)    /* multiply by m and add a */
    304303{
    305304#ifdef USE_LONG_LONG
     
    309308#endif
    310309
    311     int wds = b->wds;
    312     uint32_t* x = b->x;
     310    int wds = b.wds;
     311    uint32_t* x = b.x;
    313312    int i = 0;
    314313    carry = a;
     
    334333
    335334    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    }
    346338}
    347339
    348 static Bigint* s2b(const char* s, int nd0, int nd, uint32_t y9)
     340static void s2b(BigInt& b, const char* s, int nd0, int nd, uint32_t y9)
    349341{
    350342    int k;
     
    354346    for (k = 0, y = 1; x > y; y <<= 1, k++) { }
    355347#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;
    363355#endif
    364356
     
    367359        s += 9;
    368360        do {
    369             b = multadd(b, 10, *s++ - '0');
     361            multadd(b, 10, *s++ - '0');
    370362        } while (++i < nd0);
    371363        s++;
     
    373365        s += 10;
    374366    for (; i < nd; i++)
    375         b = multadd(b, 10, *s++ - '0');
    376     return b;
     367        multadd(b, 10, *s++ - '0');
    377368}
    378369
     
    447438}
    448439
    449 static Bigint* i2b(int i)
     440static void i2b(BigInt& b, int i)
    450441{
    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;
    457445}
    458446
    459 static Bigint* mult(Bigint* a, Bigint* b)
     447static void mult(BigInt& aRef, const BigInt& bRef)
    460448{
    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;
    464455    uint32_t y;
    465456#ifdef USE_LONG_LONG
     
    470461
    471462    if (a->wds < b->wds) {
    472         c = a;
     463        const BigInt* tmp = a;
    473464        a = b;
    474         b = c;
    475     }
    476     k = a->k;
     465        b = tmp;
     466    }
     467   
    477468    wa = a->wds;
    478469    wb = b->wds;
    479470    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;
    485474    xa = a->x;
    486475    xae = xa + wa;
    487476    xb = b->x;
    488477    xbe = xb + wb;
    489     xc0 = c->x;
     478    xc0 = c.x;
    490479#ifdef USE_LONG_LONG
    491480    for (; xb < xbe; xc0++) {
     
    549538#endif
    550539#endif
    551     for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
    552     c->wds = wc;
    553     return c;
     540    for (xc0 = c.x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
     541    c.wds = wc;
     542    aRef = c;
    554543}
    555544
    556 static Bigint* p5s;
     545struct P5Node {
     546    BigInt val;
     547    P5Node* next;
     548};
     549   
     550static P5Node* p5s;
    557551static int p5s_count;
    558552
    559 static Bigint* pow5mult(Bigint* b, int k)
     553static ALWAYS_INLINE void pow5mult(BigInt& b, int k)
    560554{
    561555    static int p05[3] = { 5, 25, 125 };
    562556
    563557    if (int i = k & 3)
    564         b = multadd(b, p05[i - 1], 0);
     558        multadd(b, p05[i - 1], 0);
    565559
    566560    if (!(k >>= 2))
    567         return b;
     561        return;
    568562
    569563#if ENABLE(JSC_MULTIPLE_THREADS)
    570564    s_dtoaP5Mutex->lock();
    571565#endif
    572     Bigint* p5 = p5s;
     566    P5Node* p5 = p5s;
     567
    573568    if (!p5) {
    574569        /* first time */
    575         p5 = p5s = i2b(625);
     570        p5 = new P5Node;
     571        i2b(p5->val, 625);
     572        p5->next = 0;
     573        p5s = p5;
    576574        p5s_count = 1;
    577575    }
     576
    578577    int p5s_count_local = p5s_count;
    579578#if ENABLE(JSC_MULTIPLE_THREADS)
     
    583582
    584583    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
    590587        if (!(k >>= 1))
    591588            break;
     
    597594            if (p5s_used == p5s_count) {
    598595                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);
    600600                ++p5s_count;
    601601            }
     
    608608        p5 = p5->next;
    609609    }
    610 
    611     return b;
    612610}
    613611
    614 static Bigint* lshift(Bigint* b, int k)
     612static ALWAYS_INLINE void lshift(BigInt& b, int k)
    615613{
    616     Bigint* result = b;
    617 
    618614#ifdef Pack_32
    619615    int n = k >> 5;
     
    622618#endif
    623619
    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;
    634625    uint32_t* dst = dstStart + n1 - 1;
    635626#ifdef Pack_32
     
    643634        *dst = hiSubword;
    644635        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);
    646637    }
    647638#else
     
    662653            *--dst = *src--;
    663654        } while (src >= srcStart);
    664         result->wds = b->wds + n;
     655        b.wds = b.wds + n;
    665656    }
    666657    for (dst = dstStart + n; dst != dstStart; )
    667658        *--dst = 0;
    668 
    669     if (result != b)
    670         Bfree(b);
    671     return result;
    672659}
    673660
    674 static int cmp(Bigint* a, Bigint* b)
     661static int cmp(const BigInt& a, const BigInt& b)
    675662{
    676     uint32_t *xa, *xa0, *xb, *xb0;
     663    const uint32_t *xa, *xa0, *xb, *xb0;
    677664    int i, j;
    678665
    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]);
    683670    if (i -= j)
    684671        return i;
    685     xa0 = a->x;
     672    xa0 = a.x;
    686673    xa = xa0 + j;
    687     xb0 = b->x;
     674    xb0 = b.x;
    688675    xb = xb0 + j;
    689676    for (;;) {
     
    696683}
    697684
    698 static Bigint* diff(Bigint* a, Bigint* b)
     685static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef)
    699686{
    700     Bigint* c;
     687    const BigInt* a = &aRef;
     688    const BigInt* b = &bRef;
    701689    int i, wa, wb;
    702     uint32_t *xa, *xae, *xb, *xbe, *xc;
    703 
    704     i = cmp(a,b);
     690    uint32_t *xc;
     691
     692    i = cmp(*a, *b);
    705693    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;
    710698    }
    711699    if (i < 0) {
    712         c = a;
     700        const BigInt* tmp = a;
    713701        a = b;
    714         b = c;
     702        b = tmp;
    715703        i = 1;
    716704    } else
    717705        i = 0;
    718     c = Balloc(a->k);
    719     c->sign = i;
     706
     707    c.wds = 0;
     708    c.sign = i;
    720709    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;
    723712    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;
    727716#ifdef USE_LONG_LONG
    728717    unsigned long long borrow = 0;
     
    769758    while (!*--xc)
    770759        wa--;
    771     c->wds = wa;
    772     return c;
     760    c.wds = wa;
    773761}
    774762
     
    804792}
    805793
    806 static double b2d(Bigint* a, int* e)
     794static double b2d(const BigInt& a, int* e)
    807795{
    808     uint32_t* xa;
    809     uint32_t* xa0;
     796    const uint32_t* xa;
     797    const uint32_t* xa0;
    810798    uint32_t w;
    811799    uint32_t y;
     
    817805#define d1 word1(&d)
    818806
    819     xa0 = a->x;
    820     xa = xa0 + a->wds;
     807    xa0 = a.x;
     808    xa = xa0 + a.wds;
    821809    y = *--xa;
    822810    ASSERT(y);
     
    861849}
    862850
    863 static Bigint* d2b(U* d, int* e, int* bits)
     851static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits)
    864852{
    865     Bigint* b;
    866853    int de, k;
    867854    uint32_t *x, y, z;
     
    872859#define d1 word1(d)
    873860
     861    b.sign = 0;
    874862#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;
    880868
    881869    z = d0 & Frac_mask;
     
    897885        i =
    898886#endif
    899             b->wds = (x[1] = z) ? 2 : 1;
     887            b.wds = (x[1] = z) ? 2 : 1;
    900888    } else {
    901889        k = lo0bits(&z);
     
    904892        i =
    905893#endif
    906             b->wds = 1;
     894            b.wds = 1;
    907895        k += 32;
    908896    }
     
    959947    }
    960948#endif
    961     return b;
    962949}
    963950#undef d0
    964951#undef d1
    965952
    966 static double ratio(Bigint* a, Bigint* b)
     953static double ratio(const BigInt& a, const BigInt& b)
    967954{
    968955    U da, db;
     
    972959    dval(&db) = b2d(b, &kb);
    973960#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);
    977964#endif
    978965    if (k > 0)
     
    10901077    int32_t L;
    10911078    uint32_t y, z;
    1092     Bigint *bb = NULL, *bb1 = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;
     1079    BigInt bb, bb1, bd, bd0, bs, delta;
    10931080#ifdef SET_INEXACT
    10941081    int inexact, oldinexact;
     
    12531240        dval(&rv) = tens[k - 9] * dval(&rv) + z;
    12541241    }
    1255     bd0 = 0;
    12561242    if (nd <= DBL_DIG && Flt_Rounds == 1) {
    12571243        if (!e)
     
    13101296                dval(&rv0) *= dval(&rv0);
    13111297#endif
    1312                 if (bd0)
    1313                     goto retfree;
    13141298                goto ret;
    13151299            }
     
    13721356                    errno = ERANGE;
    13731357#endif
    1374                     if (bd0)
    1375                         goto retfree;
    13761358                    goto ret;
    13771359                }
     
    13911373    /* Put digits into bd: true value = bd * 10^e */
    13921374
    1393     bd0 = s2b(s0, nd0, nd, y);
     1375    s2b(bd0, s0, nd0, nd, y);
    13941376
    13951377    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);
    14001381
    14011382        if (e >= 0) {
     
    14441425        }
    14451426        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);
    14501429        }
    14511430        if (bb2 > 0)
    1452             bb = lshift(bb, bb2);
     1431            lshift(bb, bb2);
    14531432        if (bd5 > 0)
    1454             bd = pow5mult(bd, bd5);
     1433            pow5mult(bd, bd5);
    14551434        if (bd2 > 0)
    1456             bd = lshift(bd, bd2);
     1435            lshift(bd, bd2);
    14571436        if (bs2 > 0)
    1458             bs = lshift(bs, bs2);
    1459         delta = 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;
    14621441        i = cmp(delta, bs);
    14631442
     
    14791458                break;
    14801459            }
    1481             if (!delta->x[0] && delta->wds <= 1) {
     1460            if (!delta.x[0] && delta.wds <= 1) {
    14821461                /* exact result */
    14831462#ifdef SET_INEXACT
     
    14861465                break;
    14871466            }
    1488             delta = lshift(delta,Log2P);
     1467            lshift(delta, Log2P);
    14891468            if (cmp(delta, bs) > 0)
    14901469                goto drop_down;
     
    16831662#endif
    16841663cont:
    1685         Bfree(bb);
    1686         Bfree(bd);
    1687         Bfree(bs);
    1688         Bfree(delta);
     1664        ;
    16891665    }
    16901666#ifdef SET_INEXACT
     
    17171693    }
    17181694#endif
    1719 retfree:
    1720     Bfree(bb);
    1721     Bfree(bd);
    1722     Bfree(bs);
    1723     Bfree(bd0);
    1724     Bfree(delta);
    17251695ret:
    17261696    if (se)
     
    17291699}
    17301700
    1731 static int quorem(Bigint* b, Bigint* S)
     1701static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S)
    17321702{
    17331703    int n;
     
    17421712#endif
    17431713
    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)
    17471717        return 0;
    1748     sx = S->x;
     1718    sx = S.x;
    17491719    sxe = sx + --n;
    1750     bx = b->x;
     1720    bx = b.x;
    17511721    bxe = bx + n;
    17521722    q = *bxe / (*sxe + 1);    /* ensure q <= true quotient */
     
    17831753        } while (sx <= sxe);
    17841754        if (!*bxe) {
    1785             bx = b->x;
     1755            bx = b.x;
    17861756            while (--bxe > bx && !*bxe)
    17871757                --n;
    1788             b->wds = n;
     1758            b.wds = n;
    17891759        }
    17901760    }
     
    17931763        borrow = 0;
    17941764        carry = 0;
    1795         bx = b->x;
    1796         sx = S->x;
     1765        bx = b.x;
     1766        sx = S.x;
    17971767        do {
    17981768#ifdef USE_LONG_LONG
     
    18221792#endif
    18231793        } while (sx <= sxe);
    1824         bx = b->x;
     1794        bx = b.x;
    18251795        bxe = bx + n;
    18261796        if (!*bxe) {
    18271797            while (--bxe > bx && !*bxe)
    18281798                --n;
    1829             b->wds = n;
     1799            b.wds = n;
    18301800        }
    18311801    }
    18321802    return q;
    1833 }
    1834 
    1835 #if !ENABLE(JSC_MULTIPLE_THREADS)
    1836 static char* dtoa_result;
    1837 #endif
    1838 
    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     return
    1851 #if !ENABLE(JSC_MULTIPLE_THREADS)
    1852     dtoa_result =
    1853 #endif
    1854         (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 dtoa
    1870  * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
    1871  * but for consistency with earlier versions of dtoa, it is optional
    1872  * 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 #endif
    18841803}
    18851804
     
    19181837 */
    19191838
    1920 char* dtoa(double dd, int ndigits, int* decpt, int* sign, char** rve)
     1839void dtoa(char* result, double dd, int ndigits, int* decpt, int* sign, char** rve)
    19211840{
    19221841    /*
     
    19371856    uint32_t x;
    19381857#endif
    1939     Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
     1858    BigInt b, b1, delta, mlo, mhi, S;
    19401859    U d2, eps, u;
    19411860    double ds;
     
    19651884        *decpt = 9999;
    19661885        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;
    19691890    }
    19701891    if (!dval(&u)) {
    19711892        *decpt = 1;
    1972         return nrv_alloc("0", rve, 1);
     1893        result[0] = '0';
     1894        result[1] = '\0';
     1895        return;
    19731896    }
    19741897
     
    19781901#endif
    19791902
    1980     b = d2b(&u, &be, &bbits);
     1903    d2b(b, &u, &be, &bbits);
    19811904#ifdef Sudden_Underflow
    19821905    i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
     
    20651988    i = 18;
    20661989    ndigits = 0;
    2067     s = s0 = rv_alloc(i);
     1990    s = s0 = result;
    20681991
    20691992    if (ilim >= 0 && ilim <= Quick_max && try_quick) {
     
    21122035        word0(&eps) -= (P - 1) * Exp_msk1;
    21132036        if (ilim == 0) {
    2114             S = mhi = 0;
     2037            S = mhi = BigInt();
    21152038            dval(&u) -= 5.;
    21162039            if (dval(&u) > dval(&eps))
     
    21312054                *s++ = '0' + (int)L;
    21322055                if (dval(&u) < dval(&eps))
    2133                     goto ret1;
     2056                    goto ret;
    21342057                if (1. - dval(&u) < dval(&eps))
    21352058                    goto bump_up;
     
    21542077                        while (*--s == '0') { }
    21552078                        s++;
    2156                         goto ret1;
     2079                        goto ret;
    21572080                    }
    21582081                    break;
     
    21752098        ds = tens[k];
    21762099        if (ndigits < 0 && ilim <= 0) {
    2177             S = mhi = 0;
     2100            S = mhi = BigInt();
    21782101            if (ilim < 0 || dval(&u) <= 5 * ds)
    21792102                goto no_digits;
     
    22122135            }
    22132136        }
    2214         goto ret1;
     2137        goto ret;
    22152138    }
    22162139
    22172140    m2 = b2;
    22182141    m5 = b5;
    2219     mhi = mlo = 0;
     2142    mhi = mlo = BigInt();
    22202143    if (leftright) {
    22212144        i =
     
    22262149        b2 += i;
    22272150        s2 += i;
    2228         mhi = i2b(1);
     2151        i2b(mhi, 1);
    22292152    }
    22302153    if (m2 > 0 && s2 > 0) {
     
    22372160        if (leftright) {
    22382161            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);
    22432164            }
    22442165            if ((j = b5 - m5))
    2245                 b = pow5mult(b, j);
     2166                pow5mult(b, j);
    22462167        } else
    2247             b = pow5mult(b, b5);
    2248         }
    2249     S = i2b(1);
     2168            pow5mult(b, b5);
     2169        }
     2170    i2b(S, 1);
    22502171    if (s5 > 0)
    2251         S = pow5mult(S, s5);
     2172        pow5mult(S, s5);
    22522173
    22532174    /* Check for special case that d is a normalized power of 2. */
     
    22732194     */
    22742195#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))
    22762197        i = 32 - i;
    22772198#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))
    22792200        i = 16 - i;
    22802201#endif
     
    22912212    }
    22922213    if (b2 > 0)
    2293         b = lshift(b, b2);
     2214        lshift(b, b2);
    22942215    if (s2 > 0)
    2295         S = lshift(S, s2);
     2216        lshift(S, s2);
    22962217    if (k_check) {
    22972218        if (cmp(b,S) < 0) {
    22982219            k--;
    2299             b = multadd(b, 10, 0);    /* we botched the k estimate */
     2220            multadd(b, 10, 0);    /* we botched the k estimate */
    23002221            if (leftright)
    2301                 mhi = multadd(mhi, 10, 0);
     2222                multadd(mhi, 10, 0);
    23022223            ilim = ilim1;
    23032224        }
     
    23062227    if (leftright) {
    23072228        if (m2 > 0)
    2308             mhi = lshift(mhi, m2);
     2229            lshift(mhi, m2);
    23092230
    23102231        /* Compute mlo -- check for special case
     
    23142235        mlo = mhi;
    23152236        if (spec_case) {
    2316             mhi = Balloc(mhi->k);
    2317             Bcopy(mhi, mlo);
    2318             mhi = lshift(mhi, Log2P);
     2237            mhi = mlo;
     2238            lshift(mhi, Log2P);
    23192239        }
    23202240
     
    23252245             */
    23262246            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);
    23302249            if (j1 == 0 && !(word1(&u) & 1)) {
    23312250                if (dig == '9')
     
    23412260            }
    23422261            if (j < 0 || (j == 0 && !(word1(&u) & 1))) {
    2343                 if (!b->x[0] && b->wds <= 1) {
     2262                if (!b.x[0] && b.wds <= 1) {
    23442263#ifdef SET_INEXACT
    23452264                    inexact = 0;
     
    23482267                }
    23492268                if (j1 > 0) {
    2350                     b = lshift(b, 1);
     2269                    lshift(b, 1);
    23512270                    j1 = cmp(b, S);
    23522271                    if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ == '9')
     
    23692288            if (i == ilim)
    23702289                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);
    23782293        }
    23792294    } else
    23802295        for (i = 1;; i++) {
    23812296            *s++ = dig = quorem(b,S) + '0';
    2382             if (!b->x[0] && b->wds <= 1) {
     2297            if (!b.x[0] && b.wds <= 1) {
    23832298#ifdef SET_INEXACT
    23842299                inexact = 0;
     
    23882303            if (i >= ilim)
    23892304                break;
    2390             b = multadd(b, 10, 0);
     2305            multadd(b, 10, 0);
    23912306        }
    23922307
    23932308    /* Round off last digit */
    23942309
    2395     b = lshift(b, 1);
     2310    lshift(b, 1);
    23962311    j = cmp(b, S);
    23972312    if (j > 0 || (j == 0 && (dig & 1))) {
     
    24172332    goto ret;
    24182333ret:
    2419     Bfree(S);
    2420     if (mhi) {
    2421         if (mlo && mlo != mhi)
    2422             Bfree(mlo);
    2423         Bfree(mhi);
    2424     }
    2425 ret1:
    24262334#ifdef SET_INEXACT
    24272335    if (inexact) {
     
    24342342        clear_inexact();
    24352343#endif
    2436     Bfree(b);
    24372344    *s = 0;
    24382345    *decpt = k + 1;
    24392346    if (rve)
    24402347        *rve = s;
    2441     return s0;
    24422348}
    24432349
Note: See TracChangeset for help on using the changeset viewer.