📄 jsdtoa.c
字号:
z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; borrow = (z & 0x10000) >> 16; Storeinc(xc, z, y); } while(xb < xbe); while(xa < xae) { y = (*xa & 0xffff) - borrow; borrow = (y & 0x10000) >> 16; z = (*xa++ >> 16) - borrow; borrow = (z & 0x10000) >> 16; Storeinc(xc, z, y); }#endif while(!*--xc) wa--; c->wds = wa; return c;}/* Return the absolute difference between x and the adjacent greater-magnitude double number (ignoring exponent overflows). */static double ulp(double x){ register Long L; double a = 0; L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;#ifndef Sudden_Underflow if (L > 0) {#endif set_word0(a, L); set_word1(a, 0);#ifndef Sudden_Underflow } else { L = -L >> Exp_shift; if (L < Exp_shift) { set_word0(a, 0x80000 >> L); set_word1(a, 0); } else { set_word0(a, 0); L -= Exp_shift; set_word1(a, L >= 31 ? 1 : 1 << (31 - L)); } }#endif return a;}static double b2d(Bigint *a, int32 *e){ ULong *xa, *xa0, w, y, z; int32 k; double d = 0;#define d0 word0(d)#define d1 word1(d)#define set_d0(x) set_word0(d, x)#define set_d1(x) set_word1(d, x) xa0 = a->x; xa = xa0 + a->wds; y = *--xa;#ifdef DEBUG if (!y) Bug("zero y in b2d");#endif k = hi0bits(y); *e = 32 - k; if (k < Ebits) { set_d0(Exp_1 | y >> (Ebits - k)); w = xa > xa0 ? *--xa : 0; set_d1(y << (32-Ebits + k) | w >> (Ebits - k)); goto ret_d; } z = xa > xa0 ? *--xa : 0; if (k -= Ebits) { set_d0(Exp_1 | y << k | z >> (32 - k)); y = xa > xa0 ? *--xa : 0; set_d1(z << k | y >> (32 - k)); } else { set_d0(Exp_1 | y); set_d1(z); } ret_d:#undef d0#undef d1#undef set_d0#undef set_d1 return d;}/* Convert d into the form b*2^e, where b is an odd integer. b is the returned * Bigint and e is the returned binary exponent. Return the number of significant * bits in b in bits. d must be finite and nonzero. */static Bigint *d2b(double d, int32 *e, int32 *bits){ Bigint *b; int32 de, i, k; ULong *x, y, z;#define d0 word0(d)#define d1 word1(d)#define set_d0(x) set_word0(d, x)#define set_d1(x) set_word1(d, x) b = Balloc(1); if (!b) return NULL; x = b->x; z = d0 & Frac_mask; set_d0(d0 & 0x7fffffff); /* clear sign bit, which we ignore */#ifdef Sudden_Underflow de = (int32)(d0 >> Exp_shift); z |= Exp_msk11;#else if ((de = (int32)(d0 >> Exp_shift)) != 0) z |= Exp_msk1;#endif 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 { JS_ASSERT(z); k = lo0bits(&z); x[0] = z; i = b->wds = 1; k += 32; }#ifndef Sudden_Underflow if (de) {#endif *e = de - Bias - (P-1) + k; *bits = P - k;#ifndef Sudden_Underflow } else { *e = de - Bias - (P-1) + 1 + k; *bits = 32*i - hi0bits(x[i-1]); }#endif return b;}#undef d0#undef d1#undef set_d0#undef set_d1static double ratio(Bigint *a, Bigint *b){ double da, db; int32 k, ka, kb; da = b2d(a, &ka); db = b2d(b, &kb); k = ka - kb + 32*(a->wds - b->wds); if (k > 0) set_word0(da, word0(da) + k*Exp_msk1); else { k = -k; set_word0(db, word0(db) + k*Exp_msk1); } 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};static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,#ifdef Avoid_Underflow 9007199254740992.e-256#else 1e-256#endif };/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow *//* flag unnecessarily. It leads to a song and dance at the end of strtod. */#define Scale_Bit 0x10#define n_bigtens 5#ifdef INFNAN_CHECK#ifndef NAN_WORD0#define NAN_WORD0 0x7ff80000#endif#ifndef NAN_WORD1#define NAN_WORD1 0#endifstatic int match(CONST char **sp, char *t){ int c, d; CONST char *s = *sp; while(d = *t++) { if ((c = *++s) >= 'A' && c <= 'Z') c += 'a' - 'A'; if (c != d) return 0; } *sp = s + 1; return 1; }#endif /* INFNAN_CHECK */#ifdef JS_THREADSAFEstatic JSBool initialized = JS_FALSE;/* hacked replica of nspr _PR_InitDtoa */static void InitDtoa(void){ freelist_lock = PR_NewLock(); p5s_lock = PR_NewLock(); initialized = JS_TRUE;}#endifvoid js_FinishDtoa(void){ int count; Bigint *temp;#ifdef JS_THREADSAFE if (initialized == JS_TRUE) { PR_DestroyLock(freelist_lock); PR_DestroyLock(p5s_lock); initialized = JS_FALSE; }#endif /* clear down the freelist array and p5s */ /* static Bigint *freelist[Kmax+1]; */ for (count = 0; count <= Kmax; count++) { Bigint **listp = &freelist[count]; while ((temp = *listp) != NULL) { *listp = temp->next; free(temp); } freelist[count] = NULL; } /* static Bigint *p5s; */ while (p5s) { temp = p5s; p5s = p5s->next; free(temp); }}/* nspr2 watcom bug ifdef omitted */JS_FRIEND_API(double)JS_strtod(CONST char *s00, char **se, int *err){ int32 scale; int32 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; double aadj, aadj1, adj, rv, rv0; Long L; ULong y, z; Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; *err = 0; bb = bd = bs = delta = NULL; sign = nz0 = nz = 0; rv = 0.; /* Locking for Balloc's shared buffers that will be used in this block */ ACQUIRE_DTOA_LOCK(); 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') { L = c - '0'; s1 = s; while((c = *++s) >= '0' && c <= '9') L = 10*L + c - '0'; if (s - s1 > 8 || L > 19999) /* Avoid confusion from exponents * so large that e might overflow. */ e = 19999; /* safe for 16 bit ints */ else e = (int32)L; if (esign) e = -e; } else e = 0; } else s = s00; } if (!nd) { if (!nz && !nz0) {#ifdef INFNAN_CHECK /* Check for Nan and Infinity */ switch(c) { case 'i': case 'I': if (match(&s,"nfinity")) { set_word0(rv, 0x7ff00000); set_word1(rv, 0); goto ret; } break; case 'n': case 'N': if (match(&s, "an")) { set_word0(rv, NAN_WORD0); set_word1(rv, NAN_WORD1); goto ret; } }#endif /* INFNAN_CHECK */ s = s00; } goto ret; } e1 = e -= nf; /* Now we have nd0 digits, starting at s0, followed by a * decimal point, followed by nd-nd0 digits. The number we're * after is the integer represented by those digits times * 10**e */ if (!nd0) nd0 = nd; k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; rv = y; if (k > 9) rv = tens[k - 9] * rv + z; bd0 = 0; if (nd <= DBL_DIG#ifndef RND_PRODQUOT && FLT_ROUNDS == 1#endif ) { if (!e) goto ret; if (e > 0) { if (e <= Ten_pmax) { /* rv = */ rounded_product(rv, tens[e]); goto ret; } i = DBL_DIG - nd; if (e <= Ten_pmax + i) { /* A fancier test would sometimes let us do * this for larger i values. */ e -= i; rv *= tens[i]; /* rv = */ rounded_product(rv, tens[e]); goto ret; } }#ifndef Inaccurate_Divide else if (e >= -Ten_pmax) { /* rv = */ rounded_quotient(rv, tens[-e]); goto ret; }#endif } e1 += nd - k; scale = 0; /* Get starting approximation = rv * 10**e1 */ if (e1 > 0) { if ((i = e1 & 15) != 0) rv *= tens[i]; if (e1 &= ~15) { if (e1 > DBL_MAX_10_EXP) { ovfl: *err = JS_DTOA_ERANGE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -