Charles.Forsyth20060303a828770d19 years agoHistory
1807 lines · 34.0 KB · C
RawDownload
/* derived from /netlib/fp/dtoa.c assuming IEEE, Standard C *//* kudos to dmg@bell-labs.com, gripes to ehg@bell-labs.com */#include "lib9.h"#define ACQUIRE_DTOA_LOCK(n)	/*nothing*/#define FREE_DTOA_LOCK(n)	/*nothing*/ /* let's provide reasonable defaults for usual implementation of IEEE f.p. */#ifndef DBL_DIG#define DBL_DIG		15#endif#ifndef DBL_MAX_10_EXP#define DBL_MAX_10_EXP	308#endif#ifndef DBL_MAX_EXP#define DBL_MAX_EXP	1024#endif#ifndef FLT_RADIX#define FLT_RADIX	2#endif#ifndef FLT_ROUNDS#define FLT_ROUNDS 1#endif#ifndef Storeinc#define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)#endif #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; #ifdef __LITTLE_ENDIAN#define word0(x) ((unsigned  long *)&x)[1]#define word1(x) ((unsigned  long *)&x)[0]#else#define word0(x) ((unsigned  long *)&x)[0]#define word1(x) ((unsigned  long *)&x)[1]#endif /* #define P DBL_MANT_DIG *//* 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 Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */#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 Kmax 15 structBigint {	struct Bigint *next;	int	k, maxwds, sign, wds;	unsigned  long x[1];}; typedef struct Bigint Bigint; static Bigint *freelist[Kmax+1]; static Bigint *Balloc(int k){	int	x;	Bigint * rv; 	ACQUIRE_DTOA_LOCK(0);	if (rv = freelist[k]) {		freelist[k] = rv->next;	} else {		x = 1 << k;		rv = (Bigint * )malloc(sizeof(Bigint) + (x - 1) * sizeof(unsigned  long));		if(rv == nil)			return nil;		rv->k = k;		rv->maxwds = x;	}	FREE_DTOA_LOCK(0);	rv->sign = rv->wds = 0;	return rv;} static voidBfree(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(long) + 2*sizeof(int)) static Bigint *multadd(Bigint *b, int m, int a)	/* multiply by m and add a */{	int	i, wds;	unsigned  long * x, y;	unsigned  long xi, z;	Bigint * b1; 	wds = b->wds;	x = b->x;	i = 0;	do {		xi = *x;		y = (xi & 0xffff) * m + a;		z = (xi >> 16) * m + (y >> 16);		a = (int)(z >> 16);		*x++ = (z << 16) + (y & 0xffff);	} while (++i < wds);	if (a) {		if (wds >= b->maxwds) {			b1 = Balloc(b->k + 1);			Bcopy(b1, b);			Bfree(b);			b = b1;		}		b->x[wds++] = a;		b->wds = wds;	}	return b;} static Bigint *s2b(const char *s, int nd0, int nd, unsigned  long y9){	Bigint * b;	int	i, k;	long 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  long 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  long *y){	register int	k;	register unsigned  long 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  long carry, y, z;	unsigned  long * x, *xa, *xae, *xb, *xbe, *xc, *xc0;	unsigned  long 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  long * 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  long * 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;	long borrow, y;	/* We need signed shifts here. */	unsigned  long * xa, *xae, *xb, *xbe, *xc;	long 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 {		y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;		borrow = y >> 16;		Sign_Extend(borrow, y);		z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;		borrow = z >> 16;		Sign_Extend(borrow, z);		Storeinc(xc, z, y);	} while (xb < xbe);	while (xa < xae) {		y = (*xa & 0xffff) + borrow;		borrow = y >> 16;		Sign_Extend(borrow, y);		z = (*xa++ >> 16) + borrow;		borrow = z >> 16;		Sign_Extend(borrow, z);		Storeinc(xc, z, y);	}	while (!*--xc)		wa--;	c->wds = wa;	return c;} static double	ulp(double x){	register long L;	double	a; 	L = (word0(x) & Exp_mask) - (P - 1) * Exp_msk1;#ifndef Sudden_Underflow	if (L > 0) {#endif

Lines 1–500 of 1807.