xref: /openbsd-src/lib/libm/src/ld128/e_fmodl.c (revision 49393c004c040ee201e6408db68882c3fe4cb110)
1*49393c00Smartynas /* @(#)e_fmod.c 5.1 93/09/24 */
2*49393c00Smartynas /*
3*49393c00Smartynas  * ====================================================
4*49393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*49393c00Smartynas  *
6*49393c00Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*49393c00Smartynas  * Permission to use, copy, modify, and distribute this
8*49393c00Smartynas  * software is freely granted, provided that this notice
9*49393c00Smartynas  * is preserved.
10*49393c00Smartynas  * ====================================================
11*49393c00Smartynas  */
12*49393c00Smartynas 
13*49393c00Smartynas /*
14*49393c00Smartynas  * fmodl(x,y)
15*49393c00Smartynas  * Return x mod y in exact arithmetic
16*49393c00Smartynas  * Method: shift and subtract
17*49393c00Smartynas  */
18*49393c00Smartynas 
19*49393c00Smartynas #include <math.h>
20*49393c00Smartynas 
21*49393c00Smartynas #include "math_private.h"
22*49393c00Smartynas 
23*49393c00Smartynas static const long double one = 1.0, Zero[] = {0.0, -0.0,};
24*49393c00Smartynas 
25*49393c00Smartynas long double
fmodl(long double x,long double y)26*49393c00Smartynas fmodl(long double x, long double y)
27*49393c00Smartynas {
28*49393c00Smartynas 	int64_t n,hx,hy,hz,ix,iy,sx,i;
29*49393c00Smartynas 	u_int64_t lx,ly,lz;
30*49393c00Smartynas 
31*49393c00Smartynas 	GET_LDOUBLE_WORDS64(hx,lx,x);
32*49393c00Smartynas 	GET_LDOUBLE_WORDS64(hy,ly,y);
33*49393c00Smartynas 	sx = hx&0x8000000000000000ULL;		/* sign of x */
34*49393c00Smartynas 	hx ^=sx;				/* |x| */
35*49393c00Smartynas 	hy &= 0x7fffffffffffffffLL;		/* |y| */
36*49393c00Smartynas 
37*49393c00Smartynas     /* purge off exception values */
38*49393c00Smartynas 	if((hy|ly)==0||(hx>=0x7fff000000000000LL)|| /* y=0,or x not finite */
39*49393c00Smartynas 	  ((hy|((ly|-ly)>>63))>0x7fff000000000000LL))	/* or y is NaN */
40*49393c00Smartynas 	    return (x*y)/(x*y);
41*49393c00Smartynas 	if(hx<=hy) {
42*49393c00Smartynas 	    if((hx<hy)||(lx<ly)) return x;	/* |x|<|y| return x */
43*49393c00Smartynas 	    if(lx==ly)
44*49393c00Smartynas 		return Zero[(u_int64_t)sx>>63];	/* |x|=|y| return x*0*/
45*49393c00Smartynas 	}
46*49393c00Smartynas 
47*49393c00Smartynas     /* determine ix = ilogb(x) */
48*49393c00Smartynas 	if(hx<0x0001000000000000LL) {	/* subnormal x */
49*49393c00Smartynas 	    if(hx==0) {
50*49393c00Smartynas 		for (ix = -16431, i=lx; i>0; i<<=1) ix -=1;
51*49393c00Smartynas 	    } else {
52*49393c00Smartynas 		for (ix = -16382, i=hx<<15; i>0; i<<=1) ix -=1;
53*49393c00Smartynas 	    }
54*49393c00Smartynas 	} else ix = (hx>>48)-0x3fff;
55*49393c00Smartynas 
56*49393c00Smartynas     /* determine iy = ilogb(y) */
57*49393c00Smartynas 	if(hy<0x0001000000000000LL) {	/* subnormal y */
58*49393c00Smartynas 	    if(hy==0) {
59*49393c00Smartynas 		for (iy = -16431, i=ly; i>0; i<<=1) iy -=1;
60*49393c00Smartynas 	    } else {
61*49393c00Smartynas 		for (iy = -16382, i=hy<<15; i>0; i<<=1) iy -=1;
62*49393c00Smartynas 	    }
63*49393c00Smartynas 	} else iy = (hy>>48)-0x3fff;
64*49393c00Smartynas 
65*49393c00Smartynas     /* set up {hx,lx}, {hy,ly} and align y to x */
66*49393c00Smartynas 	if(ix >= -16382)
67*49393c00Smartynas 	    hx = 0x0001000000000000LL|(0x0000ffffffffffffLL&hx);
68*49393c00Smartynas 	else {		/* subnormal x, shift x to normal */
69*49393c00Smartynas 	    n = -16382-ix;
70*49393c00Smartynas 	    if(n<=63) {
71*49393c00Smartynas 		hx = (hx<<n)|(lx>>(64-n));
72*49393c00Smartynas 		lx <<= n;
73*49393c00Smartynas 	    } else {
74*49393c00Smartynas 		hx = lx<<(n-64);
75*49393c00Smartynas 		lx = 0;
76*49393c00Smartynas 	    }
77*49393c00Smartynas 	}
78*49393c00Smartynas 	if(iy >= -16382)
79*49393c00Smartynas 	    hy = 0x0001000000000000LL|(0x0000ffffffffffffLL&hy);
80*49393c00Smartynas 	else {		/* subnormal y, shift y to normal */
81*49393c00Smartynas 	    n = -16382-iy;
82*49393c00Smartynas 	    if(n<=63) {
83*49393c00Smartynas 		hy = (hy<<n)|(ly>>(64-n));
84*49393c00Smartynas 		ly <<= n;
85*49393c00Smartynas 	    } else {
86*49393c00Smartynas 		hy = ly<<(n-64);
87*49393c00Smartynas 		ly = 0;
88*49393c00Smartynas 	    }
89*49393c00Smartynas 	}
90*49393c00Smartynas 
91*49393c00Smartynas     /* fix point fmod */
92*49393c00Smartynas 	n = ix - iy;
93*49393c00Smartynas 	while(n--) {
94*49393c00Smartynas 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
95*49393c00Smartynas 	    if(hz<0){hx = hx+hx+(lx>>63); lx = lx+lx;}
96*49393c00Smartynas 	    else {
97*49393c00Smartynas 		if((hz|lz)==0)		/* return sign(x)*0 */
98*49393c00Smartynas 		    return Zero[(u_int64_t)sx>>63];
99*49393c00Smartynas 		hx = hz+hz+(lz>>63); lx = lz+lz;
100*49393c00Smartynas 	    }
101*49393c00Smartynas 	}
102*49393c00Smartynas 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
103*49393c00Smartynas 	if(hz>=0) {hx=hz;lx=lz;}
104*49393c00Smartynas 
105*49393c00Smartynas     /* convert back to floating value and restore the sign */
106*49393c00Smartynas 	if((hx|lx)==0)			/* return sign(x)*0 */
107*49393c00Smartynas 	    return Zero[(u_int64_t)sx>>63];
108*49393c00Smartynas 	while(hx<0x0001000000000000LL) {	/* normalize x */
109*49393c00Smartynas 	    hx = hx+hx+(lx>>63); lx = lx+lx;
110*49393c00Smartynas 	    iy -= 1;
111*49393c00Smartynas 	}
112*49393c00Smartynas 	if(iy>= -16382) {	/* normalize output */
113*49393c00Smartynas 	    hx = ((hx-0x0001000000000000LL)|((iy+16383)<<48));
114*49393c00Smartynas 	    SET_LDOUBLE_WORDS64(x,hx|sx,lx);
115*49393c00Smartynas 	} else {		/* subnormal output */
116*49393c00Smartynas 	    n = -16382 - iy;
117*49393c00Smartynas 	    if(n<=48) {
118*49393c00Smartynas 		lx = (lx>>n)|((u_int64_t)hx<<(64-n));
119*49393c00Smartynas 		hx >>= n;
120*49393c00Smartynas 	    } else if (n<=63) {
121*49393c00Smartynas 		lx = (hx<<(64-n))|(lx>>n); hx = sx;
122*49393c00Smartynas 	    } else {
123*49393c00Smartynas 		lx = hx>>(n-64); hx = sx;
124*49393c00Smartynas 	    }
125*49393c00Smartynas 	    SET_LDOUBLE_WORDS64(x,hx|sx,lx);
126*49393c00Smartynas 	    x *= one;		/* create necessary signal */
127*49393c00Smartynas 	}
128*49393c00Smartynas 	return x;		/* exact output */
129*49393c00Smartynas }
130