Beispiel #1
0
/*
 * Return the IEEE remainder and set *quo to the last n bits of the
 * quotient, rounded to the nearest integer.  We choose n=31 because
 * we wind up computing all the integer bits of the quotient anyway as
 * a side-effect of computing the remainder by the shift and subtract
 * method.  In practice, this is far more bits than are needed to use
 * remquo in reduction algorithms.
 *
 * Assumptions:
 * - The low part of the mantissa fits in a manl_t exactly.
 * - The high part of the mantissa fits in an int64_t with enough room
 *   for an explicit integer bit in front of the fractional bits.
 */
long double remquol(long double x, long double y, int *quo)
{
	union IEEEl2bits ux, uy;
	int64_t hx,hz;  /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
	manh_t hy;
	manl_t lx,ly,lz;
	int ix,iy,n,q,sx,sxy;

	ux.e = x;
	uy.e = y;
	sx = ux.bits.sign;
	sxy = sx ^ uy.bits.sign;
	ux.bits.sign = 0;       /* |x| */
	uy.bits.sign = 0;       /* |y| */
	x = ux.e;

	/* purge off exception values */
	if ((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */
	    (ux.bits.exp == BIAS + LDBL_MAX_EXP) ||       /* or x not finite */
	    (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
		((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */
		return (x*y)/(x*y);
	if (ux.bits.exp <= uy.bits.exp) {
		if ((ux.bits.exp < uy.bits.exp) ||
		    (ux.bits.manh <= uy.bits.manh &&
		     (ux.bits.manh < uy.bits.manh ||
		      ux.bits.manl < uy.bits.manl))) {
			q = 0;
			goto fixup;       /* |x|<|y| return x or x-y */
		}
		if (ux.bits.manh == uy.bits.manh && ux.bits.manl == uy.bits.manl) {
			*quo = sxy ? -1 : 1;
			return Zero[sx];  /* |x|=|y| return x*0*/
		}
	}

	/* determine ix = ilogb(x) */
	if (ux.bits.exp == 0) {  /* subnormal x */
		ux.e *= 0x1.0p512;
		ix = ux.bits.exp - (BIAS + 512);
	} else {
		ix = ux.bits.exp - BIAS;
	}

	/* determine iy = ilogb(y) */
	if (uy.bits.exp == 0) {  /* subnormal y */
		uy.e *= 0x1.0p512;
		iy = uy.bits.exp - (BIAS + 512);
	} else {
		iy = uy.bits.exp - BIAS;
	}

	/* set up {hx,lx}, {hy,ly} and align y to x */
	hx = SET_NBIT(ux.bits.manh);
	hy = SET_NBIT(uy.bits.manh);
	lx = ux.bits.manl;
	ly = uy.bits.manl;

	/* fix point fmod */
	n = ix - iy;
	q = 0;

	while (n--) {
		hz = hx - hy;
		lz = lx - ly;
		if (lx < ly)
			hz -= 1;
		if (hz < 0) {
			hx = hx + hx + (lx>>MANL_SHIFT);
			lx = lx + lx;
		} else {
Beispiel #2
0
/*
 * fmodl(x,y)
 * Return x mod y in exact arithmetic
 * Method: shift and subtract
 *
 * Assumptions:
 * - The low part of the mantissa fits in a manl_t exactly.
 * - The high part of the mantissa fits in an int64_t with enough room
 *   for an explicit integer bit in front of the fractional bits.
 */
long double
fmodl(long double x, long double y)
{
	union {
		long double e;
		struct ieee_ext bits;
	} ux, uy;
	int64_t hx,hz;	/* We need a carry bit even if LDBL_MANH_SIZE is 32. */
	uint32_t hy;
	uint32_t lx,ly,lz;
	int ix,iy,n,sx;

	ux.e = x;
	uy.e = y;
	sx = ux.bits.ext_sign;

    /* purge off exception values */
	if((uy.bits.ext_exp|uy.bits.ext_frach|uy.bits.ext_fracl)==0 || /* y=0 */
	   (ux.bits.ext_exp == BIAS + LDBL_MAX_EXP) ||	 /* or x not finite */
	   (uy.bits.ext_exp == BIAS + LDBL_MAX_EXP &&
	    ((uy.bits.ext_frach&~LDBL_NBIT)|uy.bits.ext_fracl)!=0)) /* or y is NaN */
	    return (x*y)/(x*y);
	if(ux.bits.ext_exp<=uy.bits.ext_exp) {
	    if((ux.bits.ext_exp<uy.bits.ext_exp) ||
	       (ux.bits.ext_frach<=uy.bits.ext_frach &&
		(ux.bits.ext_frach<uy.bits.ext_frach ||
		 ux.bits.ext_fracl<uy.bits.ext_fracl))) {
		return x;		/* |x|<|y| return x or x-y */
	    }
	    if(ux.bits.ext_frach==uy.bits.ext_frach &&
		ux.bits.ext_fracl==uy.bits.ext_fracl) {
		return Zero[sx];	/* |x|=|y| return x*0*/
	    }
	}

    /* determine ix = ilogb(x) */
	if(ux.bits.ext_exp == 0) {	/* subnormal x */
	    ux.e *= 0x1.0p512;
	    ix = ux.bits.ext_exp - (BIAS + 512);
	} else {
	    ix = ux.bits.ext_exp - BIAS;
	}

    /* determine iy = ilogb(y) */
	if(uy.bits.ext_exp == 0) {	/* subnormal y */
	    uy.e *= 0x1.0p512;
	    iy = uy.bits.ext_exp - (BIAS + 512);
	} else {
	    iy = uy.bits.ext_exp - BIAS;
	}

    /* set up {hx,lx}, {hy,ly} and align y to x */
	hx = SET_NBIT(ux.bits.ext_frach);
	hy = SET_NBIT(uy.bits.ext_frach);
	lx = ux.bits.ext_fracl;
	ly = uy.bits.ext_fracl;

    /* fix point fmod */
	n = ix - iy;

	while(n--) {
	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
	    else {
		if ((hz|lz)==0)		/* return sign(x)*0 */