📄 prdtoa.c
字号:
* a new Bigint to the list. */static Bigint *p5s;static PRLock *p5s_lock;static Bigint *pow5mult(Bigint *b, PRInt32 k){ Bigint *b1, *p5, *p51; PRInt32 i; static CONST PRInt32 p05[3] = { 5, 25, 125 }; if ((i = k & 3) != 0) b = multadd(b, p05[i-1], 0); if (!(k >>= 2)) return b; if (!(p5 = p5s)) { /* * We take great care to not call i2b() and Bfree() * while holding the lock. */ Bigint *wasted_effort = NULL; p5 = i2b(625); /* lock and check again */ PR_Lock(p5s_lock); if (!p5s) { /* first time */ p5s = p5; p5->next = 0; } else { /* some other thread just beat us */ wasted_effort = p5; p5 = p5s; } PR_Unlock(p5s_lock); if (wasted_effort) { Bfree(wasted_effort); } } for(;;) { if (k & 1) { b1 = mult(b, p5); Bfree(b); b = b1; } if (!(k >>= 1)) break; if (!(p51 = p5->next)) { Bigint *wasted_effort = NULL; p51 = mult(p5, p5); PR_Lock(p5s_lock); if (!p5->next) { p5->next = p51; p51->next = 0; } else { wasted_effort = p51; p51 = p5->next; } PR_Unlock(p5s_lock); if (wasted_effort) { Bfree(wasted_effort); } } p5 = p51; } return b;}static Bigint *lshift(Bigint *b, PRInt32 k){ PRInt32 i, k1, n, n1; Bigint *b1; unsigned Long *x, *x1, *xe, z;#ifdef Pack_32 n = k >> 5;#else n = k >> 4;#endif k1 = b->k; n1 = n + b->wds + 1; for(i = b->maxwds; n1 > i; i <<= 1) k1++; b1 = Balloc(k1); x1 = b1->x; for(i = 0; i < n; i++) *x1++ = 0; x = b->x; xe = x + b->wds;#ifdef Pack_32 if (k &= 0x1f) { k1 = 32 - k; z = 0; do { *x1++ = *x << k | z; z = *x++ >> k1; } while(x < xe); if ((*x1 = z) != 0) ++n1; }#else if (k &= 0xf) { k1 = 16 - k; z = 0; do { *x1++ = *x << k & 0xffff | z; z = *x++ >> k1; } while(x < xe); if ((*x1 = z) != 0) ++n1; }#endif else do *x1++ = *x++; while(x < xe); b1->wds = n1 - 1; Bfree(b); return b1;}static PRInt32 cmp(Bigint *a, Bigint *b){ unsigned Long *xa, *xa0, *xb, *xb0; PRInt32 i, j; i = a->wds; j = b->wds;#ifdef DEBUG_DTOA if ((i > 1 && !a->x[i-1])) Bug("cmp called with a->x[a->wds-1] == 0"); if ((j > 1 && !b->x[j-1])) Bug("cmp called with b->x[b->wds-1] == 0");#endif if (i -= j) return i; xa0 = a->x; xa = xa0 + j; xb0 = b->x; xb = xb0 + j; for(;;) { if (*--xa != *--xb) return *xa < *xb ? -1 : 1; if (xa <= xa0) break; } return 0;}static Bigint *diff(Bigint *a, Bigint *b){ Bigint *c; PRInt32 i, wa, wb; Long borrow, y; /* We need signed shifts here. */ unsigned Long *xa, *xae, *xb, *xbe, *xc;#ifdef Pack_32 Long z;#endif i = cmp(a,b); if (!i) { c = Balloc(0); c->wds = 1; c->x[0] = 0; return c; } if (i < 0) { c = a; a = b; b = c; i = 1; } else i = 0; c = Balloc(a->k); c->sign = i; wa = a->wds; xa = a->x; xae = xa + wa; wb = b->wds; xb = b->x; xbe = xb + wb; xc = c->x; borrow = 0;#ifdef Pack_32 do { y = (long)((*xa & 0xffff) - (*xb & 0xffff) + borrow); borrow = y >> 16; Sign_Extend(borrow, y); z = (long)((*xa++ >> 16) - (*xb++ >> 16) + borrow); borrow = z >> 16; Sign_Extend(borrow, z); Storeinc(xc, z, y); } while(xb < xbe); while(xa < xae) { y = (long)((*xa & 0xffff) + borrow); borrow = y >> 16; Sign_Extend(borrow, y); z = (long)((*xa++ >> 16) + borrow); borrow = z >> 16; Sign_Extend(borrow, z); Storeinc(xc, z, y); }#else do { y = *xa++ - *xb++ + borrow; borrow = y >> 16; Sign_Extend(borrow, y); *xc++ = y & 0xffff; } while(xb < xbe); while(xa < xae) { y = *xa++ + borrow; borrow = y >> 16; Sign_Extend(borrow, y); *xc++ = y & 0xffff; }#endif while(!*--xc) wa--; c->wds = wa; return c;}static double ulp(double x){ register Long L; double a; L = (long)((word0(x) & Exp_mask) - (P-1)*Exp_msk1);#ifndef Sudden_Underflow if (L > 0) {#endif#ifdef IBM L |= Exp_msk1 >> 4;#endif word0(a) = L; word1(a) = 0;#ifndef Sudden_Underflow } else { L = -L >> Exp_shift; if (L < Exp_shift) { word0(a) = 0x80000 >> L; word1(a) = 0; } else { word0(a) = 0; L -= Exp_shift; word1(a) = L >= 31 ? 1 : 1 << (31 - L); } }#endif return a;}static doubleb2d#ifdef KR_headers(a, e) Bigint *a; PRInt32 *e;#else(Bigint *a, PRInt32 *e)#endif{ unsigned Long *xa, *xa0, w, y, z; PRInt32 k; double d;#ifdef VAX unsigned Long d0, d1;#else#define d0 word0(d)#define d1 word1(d)#endif xa0 = a->x; xa = xa0 + a->wds; y = *--xa;#ifdef DEBUG_DTOA if (!y) Bug("zero y in b2d");#endif k = hi0bits(y); *e = 32 - k;#ifdef Pack_32 if (k < Ebits) { d0 = Exp_1 | y >> (Ebits - k); w = xa > xa0 ? *--xa : 0; d1 = y << (32 - Ebits + k) | w >> (Ebits - k); goto ret_d; } z = xa > xa0 ? *--xa : 0; if (k -= Ebits) { d0 = Exp_1 | y << k | z >> (32 - k); y = xa > xa0 ? *--xa : 0; d1 = z << k | y >> (32 - k); } else { d0 = Exp_1 | y; d1 = z; }#else if (k < Ebits + 16) { z = xa > xa0 ? *--xa : 0; d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; w = xa > xa0 ? *--xa : 0; y = xa > xa0 ? *--xa : 0; d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; goto ret_d; } z = xa > xa0 ? *--xa : 0; w = xa > xa0 ? *--xa : 0; k -= Ebits + 16; d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; y = xa > xa0 ? *--xa : 0; d1 = w << k + 16 | y << k;#endifret_d:#ifdef VAX word0(d) = d0 >> 16 | d0 << 16; word1(d) = d1 >> 16 | d1 << 16;#else#undef d0#undef d1#endif return d;}static Bigint *d2b#ifdef KR_headers(d, e, bits) double d; PRInt32 *e, *bits;#else(double d, PRInt32 *e, PRInt32 *bits)#endif{ Bigint *b; PRInt32 de, i, k; unsigned Long *x, y, z;#ifdef VAX unsigned Long d0, d1; d0 = word0(d) >> 16 | word0(d) << 16; d1 = word1(d) >> 16 | word1(d) << 16;#else#define d0 word0(d)#define d1 word1(d)#endif#ifdef Pack_32 b = Balloc(1);#else b = Balloc(2);#endif x = b->x; z = d0 & Frac_mask; d0 &= 0x7fffffff; /* clear sign bit, which we ignore */#ifdef Sudden_Underflow de = (PRInt32)(d0 >> Exp_shift);#ifndef IBM z |= Exp_msk11;#endif#else if ((de = (PRInt32)(d0 >> Exp_shift)) != 0) z |= Exp_msk1;#endif#ifdef Pack_32 if ((y = d1) != 0) { if ((k = lo0bits(&y)) != 0) { x[0] = y | z << (32 - k); z >>= k; } else x[0] = y; i = b->wds = (x[1] = z) ? 2 : 1; } else {#ifdef DEBUG_DTOA if (!z) Bug("Zero passed to d2b");#endif k = lo0bits(&z); x[0] = z; i = b->wds = 1; k += 32; }#else if ((y = d1) != 0) { if ((k = lo0bits(&y)) != 0) if (k >= 16) { x[0] = y | z << 32 - k & 0xffff; x[1] = z >> k - 16 & 0xffff; x[2] = z >> k; i = 2; } else { x[0] = y & 0xffff; x[1] = y >> 16 | z << 16 - k & 0xffff; x[2] = z >> k & 0xffff; x[3] = z >> k+16; i = 3; } else { x[0] = y & 0xffff; x[1] = y >> 16; x[2] = z & 0xffff; x[3] = z >> 16; i = 3; } } else {#ifdef DEBUG_DTOA if (!z) Bug("Zero passed to d2b");#endif k = lo0bits(&z); if (k >= 16) { x[0] = z; i = 0; } else { x[0] = z & 0xffff; x[1] = z >> 16; i = 1; } k += 32; } while(!x[i]) --i; b->wds = i + 1;#endif#ifndef Sudden_Underflow if (de) {#endif#ifdef IBM *e = (de - Bias - (P-1) << 2) + k; *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);#else *e = de - Bias - (P-1) + k; *bits = P - k;#endif#ifndef Sudden_Underflow } else { *e = de - Bias - (P-1) + 1 + k;#ifdef Pack_32 *bits = 32*i - hi0bits(x[i-1]);#else *bits = (i+2)*16 - hi0bits(x[i]);#endif }#endif return b;}#undef d0#undef d1static doubleratio#ifdef KR_headers(a, b) Bigint *a, *b;#else(Bigint *a, Bigint *b)#endif{ double da, db; PRInt32 k, ka, kb; da = b2d(a, &ka); db = b2d(b, &kb);#ifdef Pack_32 k = ka - kb + 32*(a->wds - b->wds);#else k = ka - kb + 16*(a->wds - b->wds);#endif#ifdef IBM if (k > 0) { word0(da) += (k >> 2)*Exp_msk1; if (k &= 3) da *= 1 << k; } else { k = -k; word0(db) += (k >> 2)*Exp_msk1; if (k &= 3) db *= 1 << k; }#else if (k > 0) word0(da) += k*Exp_msk1; else { k = -k; word0(db) += k*Exp_msk1; }#endif return da / db;}static CONST doubletens[] = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22#ifdef VAX , 1e23, 1e24#endif};static CONST double#ifdef IEEE_Arithbigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };#define n_bigtens 5#else#ifdef IBMbigtens[] = { 1e16, 1e32, 1e64 };static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };#define n_bigtens 3#elsebigtens[] = { 1e16, 1e32 };static CONST double tinytens[] = { 1e-16, 1e-32 };#define n_bigtens 2#endif#endifvoid _PR_InitDtoa(void){ freelist_lock = PR_NewLock(); p5s_lock = PR_NewLock();}void _PR_CleanupDtoa(void){ PR_DestroyLock(freelist_lock); freelist_lock = NULL; PR_DestroyLock(p5s_lock); p5s_lock = NULL; /* FIXME: deal with freelist and p5s. */}#if defined(HAVE_WATCOM_BUG_1)PRFloat64 __pascal __loadds __export#elsePR_IMPLEMENT(PRFloat64) #endifPR_strtod(CONST char *s00, char **se){ PRInt32 bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; CONST char *s, *s0, *s1; PRFloat64 aadj, aadj1, adj, rv, rv0; Long L; unsigned Long y, z; Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; if (!_pr_initialized) _PR_ImplicitInitialization(); sign = nz0 = nz = 0; rv = 0.; for(s = s00;;s++) switch(*s) { case '-': sign = 1; /* no break */ case '+': if (*++s) goto break2; /* no break */ case 0: s = s00; goto ret; case '\t': case '\n': case '\v': case '\f': case '\r': case ' ': continue; default: goto break2; }break2: if (*s == '0') { nz0 = 1; while(*++s == '0') ; if (!*s) goto ret; } s0 = s; y = z = 0; for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) if (nd < 9) y = 10*y + c - '0'; else if (nd < 16) z = 10*z + c - '0'; nd0 = nd; if (c == '.') { c = *++s; if (!nd) { for(; c == '0'; c = *++s) nz++; if (c > '0' && c <= '9') { s0 = s; nf += nz; nz = 0; goto have_dig; } goto dig_done; } for(; c >= '0' && c <= '9'; c = *++s) { have_dig: nz++; if (c -= '0') { nf += nz; for(i = 1; i < nz; i++) if (nd++ < 9) y *= 10; else if (nd <= DBL_DIG + 1) z *= 10; if (nd++ < 9) y = 10*y + c; else if (nd <= DBL_DIG + 1) z = 10*z + c; nz = 0; } } }dig_done: e = 0; if (c == 'e' || c == 'E') { if (!nd && !nz && !nz0) { s = s00; goto ret; } s00 = s; esign = 0; switch(c = *++s) { case '-': esign = 1; case '+': c = *++s; } if (c >= '0' && c <= '9') { while(c == '0') c = *++s; if (c > '0' && c <= '9') {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -