1331 lines · 25.2 KB · C
RawDownload
/*  * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ /* derived from /netlib/fp/dtoa.c assuming IEEE, Standard C *//* kudos to dmg@bell-labs.com, gripes to ehg@bell-labs.com */ /* Let x be the exact mathematical number defined by a decimal *	string s.  Then atof(s) is the round-nearest-even IEEE *	floating point value. * Let y be an IEEE floating point value and let s be the string *	printed as %.17g.  Then atof(s) is exactly y. */#include <u.h>#include <libc.h> static Lock _dtoalk[2];#define ACQUIRE_DTOA_LOCK(n)	lock(&_dtoalk[n])#define FREE_DTOA_LOCK(n)	unlock(&_dtoalk[n]) #define PRIVATE_mem ((2000+sizeof(double)-1)/sizeof(double))static double private_mem[PRIVATE_mem], *pmem_next = private_mem; #define FLT_ROUNDS	1#define DBL_DIG		15#define DBL_MAX_10_EXP	308#define DBL_MAX_EXP	1024#define FLT_RADIX	2#define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) /* Ten_pmax = floor(P*log(2)/log(5)) *//* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 *//* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) *//* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ #define Exp_shift  20#define Exp_shift1 20#define Exp_msk1    0x100000#define Exp_msk11   0x100000#define Exp_mask  0x7ff00000#define P 53#define Bias 1023#define Emin (-1022)#define Exp_1  0x3ff00000#define Exp_11 0x3ff00000#define Ebits 11#define Frac_mask  0xfffff#define Frac_mask1 0xfffff#define Ten_pmax 22#define Bletch 0x10#define Bndry_mask  0xfffff#define Bndry_mask1 0xfffff#define LSB 1#define Sign_bit 0x80000000#define Log2P 1#define Tiny0 0#define Tiny1 1#define Quick_max 14#define Int_max 14#define Avoid_Underflow #define rounded_product(a,b) a *= b#define rounded_quotient(a,b) a /= b #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))#define Big1 0xffffffff #define FFFFFFFF 0xffffffffUL #define Kmax 15 typedef struct Bigint Bigint;typedef struct Ulongs Ulongs; struct Bigint {	Bigint *next;	int	k, maxwds, sign, wds;	unsigned x[1];}; struct Ulongs {	ulong	hi;	ulong	lo;}; static Bigint *freelist[Kmax+1]; Ulongsdouble2ulongs(double d){	FPdbleword dw;	Ulongs uls; 	dw.x = d;	uls.hi = dw.hi;	uls.lo = dw.lo;	return uls;} doubleulongs2double(Ulongs uls){	FPdbleword dw; 	dw.hi = uls.hi;	dw.lo = uls.lo;	return dw.x;} static Bigint *Balloc(int k){	int	x;	Bigint * rv;	unsigned int	len; 	ACQUIRE_DTOA_LOCK(0);	if (rv = freelist[k]) {		freelist[k] = rv->next;	} else {		x = 1 << k;		len = (sizeof(Bigint) + (x - 1) * sizeof(unsigned int) + sizeof(double) -1)		 / sizeof(double);		if (pmem_next - private_mem + len <= PRIVATE_mem) {			rv = (Bigint * )pmem_next;			pmem_next += len;		} else			rv = (Bigint * )malloc(len * sizeof(double));		rv->k = k;		rv->maxwds = x;	}	FREE_DTOA_LOCK(0);	rv->sign = rv->wds = 0;	return rv;} static void	Bfree(Bigint *v){	if (v) {		ACQUIRE_DTOA_LOCK(0);		v->next = freelist[v->k];		freelist[v->k] = v;		FREE_DTOA_LOCK(0);	}} #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \y->wds*sizeof(int) + 2*sizeof(int)) static Bigint *multadd(Bigint *b, int m, int a)	/* multiply by m and add a */{	int	i, wds;	unsigned int carry, *x, y;	unsigned int xi, z;	Bigint * b1; 	wds = b->wds;	x = b->x;	i = 0;	carry = a;	do {		xi = *x;		y = (xi & 0xffff) * m + carry;		z = (xi >> 16) * m + (y >> 16);		carry = z >> 16;		*x++ = (z << 16) + (y & 0xffff);	} while (++i < wds);	if (carry) {		if (wds >= b->maxwds) {			b1 = Balloc(b->k + 1);			Bcopy(b1, b);			Bfree(b);			b = b1;		}		b->x[wds++] = carry;		b->wds = wds;	}	return b;} static Bigint *s2b(const char *s, int nd0, int nd, unsigned int y9){	Bigint * b;	int	i, k;	int x, y; 	x = (nd + 8) / 9;	for (k = 0, y = 1; x > y; y <<= 1, k++) 		;	b = Balloc(k);	b->x[0] = y9;	b->wds = 1; 	i = 9;	if (9 < nd0) {		s += 9;		do 			b = multadd(b, 10, *s++ - '0');		while (++i < nd0);		s++;	} else		s += 10;	for (; i < nd; i++)		b = multadd(b, 10, *s++ - '0');	return b;} static int	hi0bits(register unsigned int x){	register int	k = 0; 	if (!(x & 0xffff0000)) {		k = 16;		x <<= 16;	}	if (!(x & 0xff000000)) {		k += 8;		x <<= 8;	}	if (!(x & 0xf0000000)) {		k += 4;		x <<= 4;	}	if (!(x & 0xc0000000)) {		k += 2;		x <<= 2;	}	if (!(x & 0x80000000)) {		k++;		if (!(x & 0x40000000))			return 32;	}	return k;} static int	lo0bits(unsigned int *y){	register int	k;	register unsigned int x = *y; 	if (x & 7) {		if (x & 1)			return 0;		if (x & 2) {			*y = x >> 1;			return 1;		}		*y = x >> 2;		return 2;	}	k = 0;	if (!(x & 0xffff)) {		k = 16;		x >>= 16;	}	if (!(x & 0xff)) {		k += 8;		x >>= 8;	}	if (!(x & 0xf)) {		k += 4;		x >>= 4;	}	if (!(x & 0x3)) {		k += 2;		x >>= 2;	}	if (!(x & 1)) {		k++;		x >>= 1;		if (!x & 1)			return 32;	}	*y = x;	return k;} static Bigint *i2b(int i){	Bigint * b; 	b = Balloc(1);	b->x[0] = i;	b->wds = 1;	return b;} static Bigint *mult(Bigint *a, Bigint *b){	Bigint * c;	int	k, wa, wb, wc;	unsigned int * x, *xa, *xae, *xb, *xbe, *xc, *xc0;	unsigned int y;	unsigned int carry, z;	unsigned int z2; 	if (a->wds < b->wds) {		c = a;		a = b;		b = c;	}	k = a->k;	wa = a->wds;	wb = b->wds;	wc = wa + wb;	if (wc > a->maxwds)		k++;	c = Balloc(k);	for (x = c->x, xa = x + wc; x < xa; x++)		*x = 0;	xa = a->x;	xae = xa + wa;	xb = b->x;	xbe = xb + wb;	xc0 = c->x;	for (; xb < xbe; xb++, xc0++) {		if (y = *xb & 0xffff) {			x = xa;			xc = xc0;			carry = 0;			do {				z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;				carry = z >> 16;				z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;				carry = z2 >> 16;				Storeinc(xc, z2, z);			} while (x < xae);			*xc = carry;		}		if (y = *xb >> 16) {			x = xa;			xc = xc0;			carry = 0;			z2 = *xc;			do {				z = (*x & 0xffff) * y + (*xc >> 16) + carry;				carry = z >> 16;				Storeinc(xc, z, z2);				z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;				carry = z2 >> 16;			} while (x < xae);			*xc = z2;		}	}	for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) 		;	c->wds = wc;	return c;} static Bigint *p5s; static Bigint *pow5mult(Bigint *b, int k){	Bigint * b1, *p5, *p51;	int	i;	static int	p05[3] = { 		5, 25, 125 	}; 	if (i = k & 3)		b = multadd(b, p05[i-1], 0); 	if (!(k >>= 2))		return b;	if (!(p5 = p5s)) {		/* first time */		ACQUIRE_DTOA_LOCK(1);		if (!(p5 = p5s)) {			p5 = p5s = i2b(625);			p5->next = 0;		}		FREE_DTOA_LOCK(1);	}	for (; ; ) {		if (k & 1) {			b1 = mult(b, p5);			Bfree(b);			b = b1;		}		if (!(k >>= 1))			break;		if (!(p51 = p5->next)) {			ACQUIRE_DTOA_LOCK(1);			if (!(p51 = p5->next)) {				p51 = p5->next = mult(p5, p5);				p51->next = 0;			}			FREE_DTOA_LOCK(1);		}		p5 = p51;	}	return b;} static Bigint *lshift(Bigint *b, int k){	int	i, k1, n, n1;	Bigint * b1;	unsigned int * x, *x1, *xe, z; 	n = k >> 5;	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;	if (k &= 0x1f) {		k1 = 32 - k;		z = 0;		do {			*x1++ = *x << k | z;			z = *x++ >> k1;		} while (x < xe);		if (*x1 = z)			++n1;	} else 		do			*x1++ = *x++;		while (x < xe);	b1->wds = n1 - 1;	Bfree(b);	return b1;} static int	cmp(Bigint *a, Bigint *b){	unsigned int * xa, *xa0, *xb, *xb0;	int	i, j; 	i = a->wds;	j = b->wds;	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;	int	i, wa, wb;	unsigned int * xa, *xae, *xb, *xbe, *xc;	unsigned int borrow, y;	unsigned int z; 	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;	do {

Lines 1–500 of 1331.