Blame sysdeps/ieee754/ldbl-128/e_fmodl.c

Packit 6c4009
/* e_fmodl.c -- long double version of e_fmod.c.
Packit 6c4009
 * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
Packit 6c4009
 */
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993, 2011 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * __ieee754_fmodl(x,y)
Packit 6c4009
 * Return x mod y in exact arithmetic
Packit 6c4009
 * Method: shift and subtract
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
Packit 6c4009
static const _Float128 one = 1.0, Zero[] = {0.0, -0.0,};
Packit 6c4009
Packit 6c4009
_Float128
Packit 6c4009
__ieee754_fmodl (_Float128 x, _Float128 y)
Packit 6c4009
{
Packit 6c4009
	int64_t n,hx,hy,hz,ix,iy,sx,i;
Packit 6c4009
	uint64_t lx,ly,lz;
Packit 6c4009
Packit 6c4009
	GET_LDOUBLE_WORDS64(hx,lx,x);
Packit 6c4009
	GET_LDOUBLE_WORDS64(hy,ly,y);
Packit 6c4009
	sx = hx&0x8000000000000000ULL;		/* sign of x */
Packit 6c4009
	hx ^=sx;				/* |x| */
Packit 6c4009
	hy &= 0x7fffffffffffffffLL;		/* |y| */
Packit 6c4009
Packit 6c4009
    /* purge off exception values */
Packit 6c4009
	if((hy|ly)==0||(hx>=0x7fff000000000000LL)|| /* y=0,or x not finite */
Packit 6c4009
	  ((hy|((ly|-ly)>>63))>0x7fff000000000000LL))	/* or y is NaN */
Packit 6c4009
	    return (x*y)/(x*y);
Packit 6c4009
	if(hx<=hy) {
Packit 6c4009
	    if((hx
Packit 6c4009
	    if(lx==ly)
Packit 6c4009
		return Zero[(uint64_t)sx>>63];	/* |x|=|y| return x*0*/
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
    /* determine ix = ilogb(x) */
Packit 6c4009
	if(hx<0x0001000000000000LL) {	/* subnormal x */
Packit 6c4009
	    if(hx==0) {
Packit 6c4009
		for (ix = -16431, i=lx; i>0; i<<=1) ix -=1;
Packit 6c4009
	    } else {
Packit 6c4009
		for (ix = -16382, i=hx<<15; i>0; i<<=1) ix -=1;
Packit 6c4009
	    }
Packit 6c4009
	} else ix = (hx>>48)-0x3fff;
Packit 6c4009
Packit 6c4009
    /* determine iy = ilogb(y) */
Packit 6c4009
	if(hy<0x0001000000000000LL) {	/* subnormal y */
Packit 6c4009
	    if(hy==0) {
Packit 6c4009
		for (iy = -16431, i=ly; i>0; i<<=1) iy -=1;
Packit 6c4009
	    } else {
Packit 6c4009
		for (iy = -16382, i=hy<<15; i>0; i<<=1) iy -=1;
Packit 6c4009
	    }
Packit 6c4009
	} else iy = (hy>>48)-0x3fff;
Packit 6c4009
Packit 6c4009
    /* set up {hx,lx}, {hy,ly} and align y to x */
Packit 6c4009
	if(ix >= -16382)
Packit 6c4009
	    hx = 0x0001000000000000LL|(0x0000ffffffffffffLL&hx;;
Packit 6c4009
	else {		/* subnormal x, shift x to normal */
Packit 6c4009
	    n = -16382-ix;
Packit 6c4009
	    if(n<=63) {
Packit 6c4009
		hx = (hx<<n)|(lx>>(64-n));
Packit 6c4009
		lx <<= n;
Packit 6c4009
	    } else {
Packit 6c4009
		hx = lx<<(n-64);
Packit 6c4009
		lx = 0;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	if(iy >= -16382)
Packit 6c4009
	    hy = 0x0001000000000000LL|(0x0000ffffffffffffLL&hy);
Packit 6c4009
	else {		/* subnormal y, shift y to normal */
Packit 6c4009
	    n = -16382-iy;
Packit 6c4009
	    if(n<=63) {
Packit 6c4009
		hy = (hy<<n)|(ly>>(64-n));
Packit 6c4009
		ly <<= n;
Packit 6c4009
	    } else {
Packit 6c4009
		hy = ly<<(n-64);
Packit 6c4009
		ly = 0;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
    /* fix point fmod */
Packit 6c4009
	n = ix - iy;
Packit 6c4009
	while(n--) {
Packit 6c4009
	    hz=hx-hy;lz=lx-ly; if(lx
Packit 6c4009
	    if(hz<0){hx = hx+hx+(lx>>63); lx = lx+lx;}
Packit 6c4009
	    else {
Packit 6c4009
		if((hz|lz)==0)		/* return sign(x)*0 */
Packit 6c4009
		    return Zero[(uint64_t)sx>>63];
Packit 6c4009
		hx = hz+hz+(lz>>63); lx = lz+lz;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	hz=hx-hy;lz=lx-ly; if(lx
Packit 6c4009
	if(hz>=0) {hx=hz;lx=lz;}
Packit 6c4009
Packit 6c4009
    /* convert back to floating value and restore the sign */
Packit 6c4009
	if((hx|lx)==0)			/* return sign(x)*0 */
Packit 6c4009
	    return Zero[(uint64_t)sx>>63];
Packit 6c4009
	while(hx<0x0001000000000000LL) {	/* normalize x */
Packit 6c4009
	    hx = hx+hx+(lx>>63); lx = lx+lx;
Packit 6c4009
	    iy -= 1;
Packit 6c4009
	}
Packit 6c4009
	if(iy>= -16382) {	/* normalize output */
Packit 6c4009
	    hx = ((hx-0x0001000000000000LL)|((iy+16383)<<48));
Packit 6c4009
	    SET_LDOUBLE_WORDS64(x,hx|sx,lx);
Packit 6c4009
	} else {		/* subnormal output */
Packit 6c4009
	    n = -16382 - iy;
Packit 6c4009
	    if(n<=48) {
Packit 6c4009
		lx = (lx>>n)|((uint64_t)hx<<(64-n));
Packit 6c4009
		hx >>= n;
Packit 6c4009
	    } else if (n<=63) {
Packit 6c4009
		lx = (hx<<(64-n))|(lx>>n); hx = sx;
Packit 6c4009
	    } else {
Packit 6c4009
		lx = hx>>(n-64); hx = sx;
Packit 6c4009
	    }
Packit 6c4009
	    SET_LDOUBLE_WORDS64(x,hx|sx,lx);
Packit 6c4009
	    x *= one;		/* create necessary signal */
Packit 6c4009
	}
Packit 6c4009
	return x;		/* exact output */
Packit 6c4009
}
Packit 6c4009
strong_alias (__ieee754_fmodl, __fmodl_finite)