⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _dtoa.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
			goto one_digit;			}		for(i = 1;; i++) {			L = floor(d.d / ds);			d.d -= L*ds;#ifdef Check_FLT_ROUNDS			/* If FLT_ROUNDS == 2, L will usually be high by 1 */			if (d.d < 0) {				L--;				d.d += ds;				}#endif			*s++ = '0' + (int)L;			if (i == ilim) {				d.d += d.d;				if (d.d > ds || d.d == ds && L & 1) { bump_up:					while(*--s == '9')						if (s == s0) {							k++;							*s = '0';							break;							}					++*s++;					}				break;				}			d.d *= 10.;			if (d.d == 0.)				break;			}		goto ret1;		}	m2 = b2;	m5 = b5;	mhi = mlo = 0;	if (leftright) {		if (mode < 2) {			i =#ifndef Sudden_Underflow				denorm ? be + (Bias + (P-1) - 1 + 1) :#endif#ifdef IBM				1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);#else				1 + P - bbits;#endif			}		else {			j = ilim - 1;			if (m5 >= j)				m5 -= j;			else {				s5 += j -= m5;				b5 += j;				m5 = 0;				}			if ((i = ilim) < 0) {				m2 -= i;				i = 0;				}			}		b2 += i;		s2 += i;		mhi = i2b(1);		}	if (m2 > 0 && s2 > 0) {		i = m2 < s2 ? m2 : s2;		b2 -= i;		m2 -= i;		s2 -= i;		}	if (b5 > 0) {		if (leftright) {			if (m5 > 0) {				mhi = pow5mult(mhi, m5);				b1 = mult(mhi, b);				Bfree(b);				b = b1;				}			if (j = b5 - m5)				b = pow5mult(b, j);			}		else			b = pow5mult(b, b5);		}	S = i2b(1);	if (s5 > 0)		S = pow5mult(S, s5);	/* Check for special case that d is a normalized power of 2. */	if (mode < 2) {		if (!word1(d) && !(word0(d) & Bndry_mask)#ifndef Sudden_Underflow		 && word0(d) & Exp_mask#endif				) {			/* The special case */			b2 += Log2P;			s2 += Log2P;			spec_case = 1;			}		else			spec_case = 0;		}	/* Arrange for convenient computation of quotients:	 * shift left if necessary so divisor has 4 leading 0 bits.	 *	 * Perhaps we should just compute leading 28 bits of S once	 * and for all and pass them and a shift to quorem, so it	 * can do shifts and ors to compute the numerator for q.	 */#ifdef Pack_32	if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)		i = 32 - i;#else	if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)		i = 16 - i;#endif	if (i > 4) {		i -= 4;		b2 += i;		m2 += i;		s2 += i;		}	else if (i < 4) {		i += 28;		b2 += i;		m2 += i;		s2 += i;		}	if (b2 > 0)		b = lshift(b, b2);	if (s2 > 0)		S = lshift(S, s2);	if (k_check) {		if (cmp(b,S) < 0) {			k--;			b = multadd(b, 10, 0);	/* we botched the k estimate */			if (leftright)				mhi = multadd(mhi, 10, 0);			ilim = ilim1;			}		}	if (ilim <= 0 && mode > 2) {		if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {			/* no digits, fcvt style */ no_digits:			k = -1 - ndigits;			goto ret;			} one_digit:		*s++ = '1';		k++;		goto ret;		}	if (leftright) {		if (m2 > 0)			mhi = lshift(mhi, m2);		/* Compute mlo -- check for special case		 * that d is a normalized power of 2.		 */		mlo = mhi;		if (spec_case) {			mhi = Balloc(mhi->k);			Bcopy(mhi, mlo);			mhi = lshift(mhi, Log2P);			}		for(i = 1;;i++) {			dig = quorem(b,S) + '0';			/* Do we yet have the shortest decimal string			 * that will round to d?			 */			j = cmp(b, mlo);			delta = diff(S, mhi);			j1 = delta->sign ? 1 : cmp(b, delta);			Bfree(delta);#ifndef ROUND_BIASED			if (j1 == 0 && !mode && !(word1(d) & 1)) {				if (dig == '9')					goto round_9_up;				if (j > 0)					dig++;				*s++ = dig;				goto ret;				}#endif			if (j < 0 || j == 0 && !mode#ifndef ROUND_BIASED							&& !(word1(d) & 1)#endif					) {				if (j1 > 0) {					b = lshift(b, 1);					j1 = cmp(b, S);					if ((j1 > 0 || j1 == 0 && dig & 1)					&& dig++ == '9')						goto round_9_up;					}				*s++ = dig;				goto ret;				}			if (j1 > 0) {				if (dig == '9') { /* possible if i == 1 */ round_9_up:					*s++ = '9';					goto roundoff;					}				*s++ = dig + 1;				goto ret;				}			*s++ = dig;			if (i == ilim)				break;			b = multadd(b, 10, 0);			if (mlo == mhi)				mlo = mhi = multadd(mhi, 10, 0);			else {				mlo = multadd(mlo, 10, 0);				mhi = multadd(mhi, 10, 0);				}			}		}	else		for(i = 1;; i++) {			*s++ = dig = quorem(b,S) + '0';			if (i >= ilim)				break;			b = multadd(b, 10, 0);			}	/* Round off last digit */	b = lshift(b, 1);	j = cmp(b, S);	if (j > 0 || j == 0 && dig & 1) { roundoff:		while(*--s == '9')			if (s == s0) {				k++;				*s++ = '1';				goto ret;				}		++*s++;		}	else {		while(*--s == '0');		s++;		} ret:	Bfree(S);	if (mhi) {		if (mlo && mlo != mhi)			Bfree(mlo);		Bfree(mhi);		} ret1:	Bfree(b);	*s = 0;	*decpt = k + 1;	if (rve)		*rve = s;	return s0;	} static intquorem(Bigint *b, Bigint *S){	int n;	long borrow, y;	unsigned long carry, q, ys;	unsigned long *bx, *bxe, *sx, *sxe;#ifdef Pack_32	long z;	unsigned long si, zs;#endif	n = S->wds;#ifdef DEBUG	/*debug*/ if (b->wds > n)	/*debug*/	Bug("oversize b in quorem");#endif	if (b->wds < n)		return 0;	sx = S->x;	sxe = sx + --n;	bx = b->x;	bxe = bx + n;	q = *bxe / (*sxe + 1);	/* ensure q <= true quotient */#ifdef DEBUG	/*debug*/ if (q > 9)	/*debug*/	Bug("oversized quotient in quorem");#endif	if (q) {		borrow = 0;		carry = 0;		do {#ifdef Pack_32			si = *sx++;			ys = (si & 0xffff) * q + carry;			zs = (si >> 16) * q + (ys >> 16);			carry = zs >> 16;			y = (*bx & 0xffff) - (ys & 0xffff) + borrow;			borrow = y >> 16;			Sign_Extend(borrow, y);			z = (*bx >> 16) - (zs & 0xffff) + borrow;			borrow = z >> 16;			Sign_Extend(borrow, z);			Storeinc(bx, z, y);#else			ys = *sx++ * q + carry;			carry = ys >> 16;			y = *bx - (ys & 0xffff) + borrow;			borrow = y >> 16;			Sign_Extend(borrow, y);			*bx++ = y & 0xffff;#endif			}			while(sx <= sxe);		if (!*bxe) {			bx = b->x;			while(--bxe > bx && !*bxe)				--n;			b->wds = n;			}		}	if (cmp(b, S) >= 0) {		q++;		borrow = 0;		carry = 0;		bx = b->x;		sx = S->x;		do {#ifdef Pack_32			si = *sx++;			ys = (si & 0xffff) + carry;			zs = (si >> 16) + (ys >> 16);			carry = zs >> 16;			y = (*bx & 0xffff) - (ys & 0xffff) + borrow;			borrow = y >> 16;			Sign_Extend(borrow, y);			z = (*bx >> 16) - (zs & 0xffff) + borrow;			borrow = z >> 16;			Sign_Extend(borrow, z);			Storeinc(bx, z, y);#else			ys = *sx++ + carry;			carry = ys >> 16;			y = *bx - (ys & 0xffff) + borrow;			borrow = y >> 16;			Sign_Extend(borrow, y);			*bx++ = y & 0xffff;#endif			}			while(sx <= sxe);		bx = b->x;		bxe = bx + n;		if (!*bxe) {			while(--bxe > bx && !*bxe)				--n;			b->wds = n;			}		}	return q;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -