xref: /openbsd-src/lib/libm/src/ld128/s_remquol.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
149393c00Smartynas /* @(#)e_fmod.c 1.3 95/01/18 */
249393c00Smartynas /*-
349393c00Smartynas  * ====================================================
449393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
549393c00Smartynas  *
649393c00Smartynas  * Developed at SunSoft, a Sun Microsystems, Inc. business.
749393c00Smartynas  * Permission to use, copy, modify, and distribute this
849393c00Smartynas  * software is freely granted, provided that this notice
949393c00Smartynas  * is preserved.
1049393c00Smartynas  * ====================================================
1149393c00Smartynas  */
1249393c00Smartynas 
1349393c00Smartynas #include <sys/types.h>
1449393c00Smartynas #include <machine/ieee.h>
1549393c00Smartynas 
1649393c00Smartynas #include <float.h>
1749393c00Smartynas #include <math.h>
1849393c00Smartynas #include <stdint.h>
1949393c00Smartynas 
2049393c00Smartynas #include "math_private.h"
2149393c00Smartynas 
2249393c00Smartynas #define	BIAS (LDBL_MAX_EXP - 1)
2349393c00Smartynas 
2449393c00Smartynas /*
2549393c00Smartynas  * These macros add and remove an explicit integer bit in front of the
2649393c00Smartynas  * fractional mantissa, if the architecture doesn't have such a bit by
2749393c00Smartynas  * default already.
2849393c00Smartynas  */
2949393c00Smartynas #ifdef LDBL_IMPLICIT_NBIT
3049393c00Smartynas #define	LDBL_NBIT	0
3149393c00Smartynas #define	SET_NBIT(hx)	((hx) | (1ULL << LDBL_MANH_SIZE))
3249393c00Smartynas #define	HFRAC_BITS	(EXT_FRACHBITS + EXT_FRACHMBITS)
3349393c00Smartynas #else
3449393c00Smartynas #define	LDBL_NBIT	0x80000000
3549393c00Smartynas #define	SET_NBIT(hx)	(hx)
3649393c00Smartynas #define	HFRAC_BITS	(EXT_FRACHBITS + EXT_FRACHMBITS - 1)
3749393c00Smartynas #endif
3849393c00Smartynas 
3949393c00Smartynas #define	MANL_SHIFT	(EXT_FRACLMBITS + EXT_FRACLBITS - 1)
4049393c00Smartynas 
4149393c00Smartynas static const long double Zero[] = {0.0L, -0.0L};
4249393c00Smartynas 
4349393c00Smartynas /*
4449393c00Smartynas  * Return the IEEE remainder and set *quo to the last n bits of the
4549393c00Smartynas  * quotient, rounded to the nearest integer.  We choose n=31 because
4649393c00Smartynas  * we wind up computing all the integer bits of the quotient anyway as
4749393c00Smartynas  * a side-effect of computing the remainder by the shift and subtract
4849393c00Smartynas  * method.  In practice, this is far more bits than are needed to use
4949393c00Smartynas  * remquo in reduction algorithms.
5049393c00Smartynas  *
5149393c00Smartynas  * Assumptions:
5249393c00Smartynas  * - The low part of the mantissa fits in a manl_t exactly.
5349393c00Smartynas  * - The high part of the mantissa fits in an int64_t with enough room
5449393c00Smartynas  *   for an explicit integer bit in front of the fractional bits.
5549393c00Smartynas  */
5649393c00Smartynas long double
remquol(long double x,long double y,int * quo)5749393c00Smartynas remquol(long double x, long double y, int *quo)
5849393c00Smartynas {
5949393c00Smartynas 	int64_t hx,hz,hy,_hx;
6049393c00Smartynas 	uint64_t lx,ly,lz;
6149393c00Smartynas 	uint64_t sx,sxy;
6249393c00Smartynas 	int ix,iy,n,q;
6349393c00Smartynas 
6449393c00Smartynas 	GET_LDOUBLE_WORDS64(hx,lx,x);
6549393c00Smartynas 	GET_LDOUBLE_WORDS64(hy,ly,y);
6649393c00Smartynas 	sx = (hx>>48)&0x8000;
6749393c00Smartynas 	sxy = sx ^ ((hy>>48)&0x8000);
6849393c00Smartynas 	hx &= 0x7fffffffffffffffLL;	/* |x| */
6949393c00Smartynas 	hy &= 0x7fffffffffffffffLL;	/* |y| */
7049393c00Smartynas 	SET_LDOUBLE_WORDS64(x,hx,lx);
7149393c00Smartynas 	SET_LDOUBLE_WORDS64(y,hy,ly);
7249393c00Smartynas 
7349393c00Smartynas     /* purge off exception values */
7449393c00Smartynas 	if((hy|ly)==0 || /* y=0 */
7549393c00Smartynas 	   ((hx>>48) == BIAS + LDBL_MAX_EXP) ||	 /* or x not finite */
7649393c00Smartynas 	   ((hy>>48) == BIAS + LDBL_MAX_EXP &&
7749393c00Smartynas 	    (((hy&0x0000ffffffffffffLL)&~LDBL_NBIT)|ly)!=0)) /* or y is NaN */
7849393c00Smartynas 	    return (x*y)/(x*y);
7949393c00Smartynas 	if((hx>>48)<=(hy>>48)) {
8049393c00Smartynas 	    if(((hx>>48)<(hy>>48)) ||
8149393c00Smartynas 	       ((hx&0x0000ffffffffffffLL)<=(hy&0x0000ffffffffffffLL) &&
8249393c00Smartynas 		((hx&0x0000ffffffffffffLL)<(hy&0x0000ffffffffffffLL) ||
8349393c00Smartynas 		 lx<ly))) {
8449393c00Smartynas 		q = 0;
8549393c00Smartynas 		goto fixup;	/* |x|<|y| return x or x-y */
8649393c00Smartynas 	    }
8749393c00Smartynas 	    if((hx&0x0000ffffffffffffLL)==(hy&0x0000ffffffffffffLL) &&
8849393c00Smartynas 		lx==ly) {
8949393c00Smartynas 		*quo = 1;
9049393c00Smartynas 		return Zero[sx!=0];	/* |x|=|y| return x*0*/
9149393c00Smartynas 	    }
9249393c00Smartynas 	}
9349393c00Smartynas 
9449393c00Smartynas     /* determine ix = ilogb(x) */
9549393c00Smartynas 	if((hx>>48) == 0) {	/* subnormal x */
9649393c00Smartynas 	    x *= 0x1.0p512;
9749393c00Smartynas 	    GET_LDOUBLE_WORDS64(hx,lx,x);
9849393c00Smartynas 	    ix = (hx>>48) - (BIAS + 512);
9949393c00Smartynas 	} else {
10049393c00Smartynas 	    ix = (hx>>48) - BIAS;
10149393c00Smartynas 	}
10249393c00Smartynas 
10349393c00Smartynas     /* determine iy = ilogb(y) */
10449393c00Smartynas 	if((hy>>48) == 0) {	/* subnormal y */
10549393c00Smartynas 	    y *= 0x1.0p512;
10649393c00Smartynas 	    GET_LDOUBLE_WORDS64(hy,ly,y);
10749393c00Smartynas 	    iy = (hy>>48) - (BIAS + 512);
10849393c00Smartynas 	} else {
10949393c00Smartynas 	    iy = (hy>>48) - BIAS;
11049393c00Smartynas 	}
11149393c00Smartynas 
11249393c00Smartynas     /* set up {hx,lx}, {hy,ly} and align y to x */
11349393c00Smartynas 	_hx = SET_NBIT(hx) & 0x0000ffffffffffffLL;
11449393c00Smartynas 	hy = SET_NBIT(hy);
11549393c00Smartynas 
11649393c00Smartynas     /* fix point fmod */
11749393c00Smartynas 	n = ix - iy;
11849393c00Smartynas 	q = 0;
11949393c00Smartynas 
12049393c00Smartynas 	while(n--) {
12149393c00Smartynas 	    hz=_hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
12249393c00Smartynas 	    if(hz<0){_hx = _hx+_hx+(lx>>MANL_SHIFT); lx = lx+lx;}
12349393c00Smartynas 	    else {_hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
12449393c00Smartynas 	    q <<= 1;
12549393c00Smartynas 	}
12649393c00Smartynas 	hz=_hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
12749393c00Smartynas 	if(hz>=0) {_hx=hz;lx=lz;q++;}
12849393c00Smartynas 
12949393c00Smartynas     /* convert back to floating value and restore the sign */
13049393c00Smartynas 	if((_hx|lx)==0) {			/* return sign(x)*0 */
13149393c00Smartynas 	    *quo = (sxy ? -q : q);
13249393c00Smartynas 	    return Zero[sx!=0];
13349393c00Smartynas 	}
13449393c00Smartynas 	while(_hx<(1ULL<<HFRAC_BITS)) {	/* normalize x */
13549393c00Smartynas 	    _hx = _hx+_hx+(lx>>MANL_SHIFT); lx = lx+lx;
13649393c00Smartynas 	    iy -= 1;
13749393c00Smartynas 	}
13849393c00Smartynas 	hx = (hx&0xffff000000000000LL) | (_hx&0x0000ffffffffffffLL);
13949393c00Smartynas 	if (iy < LDBL_MIN_EXP) {
14049393c00Smartynas 	    hx = (hx&0x0000ffffffffffffLL) | (uint64_t)(iy + BIAS + 512)<<48;
14149393c00Smartynas 	    SET_LDOUBLE_WORDS64(x,hx,lx);
14249393c00Smartynas 	    x *= 0x1p-512;
14349393c00Smartynas 	    GET_LDOUBLE_WORDS64(hx,lx,x);
14449393c00Smartynas 	} else {
14549393c00Smartynas 	    hx = (hx&0x0000ffffffffffffLL) | (uint64_t)(iy + BIAS)<<48;
14649393c00Smartynas 	}
14749393c00Smartynas 	hx &= 0x7fffffffffffffffLL;
14849393c00Smartynas 	SET_LDOUBLE_WORDS64(x,hx,lx);
14949393c00Smartynas fixup:
15049393c00Smartynas 	y = fabsl(y);
15149393c00Smartynas 	if (y < LDBL_MIN * 2) {
15249393c00Smartynas 	    if (x+x>y || (x+x==y && (q & 1))) {
15349393c00Smartynas 		q++;
15449393c00Smartynas 		x-=y;
15549393c00Smartynas 	    }
15649393c00Smartynas 	} else if (x>0.5*y || (x==0.5*y && (q & 1))) {
15749393c00Smartynas 	    q++;
15849393c00Smartynas 	    x-=y;
15949393c00Smartynas 	}
16049393c00Smartynas 
16149393c00Smartynas 	GET_LDOUBLE_MSW64(hx,x);
16249393c00Smartynas 	hx ^= sx;
16349393c00Smartynas 	SET_LDOUBLE_MSW64(x,hx);
16449393c00Smartynas 
16549393c00Smartynas 	q &= 0x7fffffff;
16649393c00Smartynas 	*quo = (sxy ? -q : q);
16749393c00Smartynas 	return x;
16849393c00Smartynas }
169*2f2c0062Sguenther DEF_STD(remquol);
170