xref: /minix3/lib/libm/src/s_remquof.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* @(#)e_fmod.c 1.3 95/01/18 */
2*84d9c625SLionel Sambuc /*-
3*84d9c625SLionel Sambuc  * ====================================================
4*84d9c625SLionel Sambuc  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*84d9c625SLionel Sambuc  *
6*84d9c625SLionel Sambuc  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7*84d9c625SLionel Sambuc  * Permission to use, copy, modify, and distribute this
8*84d9c625SLionel Sambuc  * software is freely granted, provided that this notice
9*84d9c625SLionel Sambuc  * is preserved.
10*84d9c625SLionel Sambuc  * ====================================================
11*84d9c625SLionel Sambuc  */
12*84d9c625SLionel Sambuc 
13*84d9c625SLionel Sambuc #include <sys/cdefs.h>
14*84d9c625SLionel Sambuc 
15*84d9c625SLionel Sambuc #include "math.h"
16*84d9c625SLionel Sambuc #include "math_private.h"
17*84d9c625SLionel Sambuc 
18*84d9c625SLionel Sambuc static const float Zero[] = {0.0, -0.0,};
19*84d9c625SLionel Sambuc 
20*84d9c625SLionel Sambuc /*
21*84d9c625SLionel Sambuc  * Return the IEEE remainder and set *quo to the last n bits of the
22*84d9c625SLionel Sambuc  * quotient, rounded to the nearest integer.  We choose n=31 because
23*84d9c625SLionel Sambuc  * we wind up computing all the integer bits of the quotient anyway as
24*84d9c625SLionel Sambuc  * a side-effect of computing the remainder by the shift and subtract
25*84d9c625SLionel Sambuc  * method.  In practice, this is far more bits than are needed to use
26*84d9c625SLionel Sambuc  * remquo in reduction algorithms.
27*84d9c625SLionel Sambuc  */
28*84d9c625SLionel Sambuc float
remquof(float x,float y,int * quo)29*84d9c625SLionel Sambuc remquof(float x, float y, int *quo)
30*84d9c625SLionel Sambuc {
31*84d9c625SLionel Sambuc 	int32_t n,hx,hy,hz,ix,iy,sx,i;
32*84d9c625SLionel Sambuc 	u_int32_t q,sxy;
33*84d9c625SLionel Sambuc 
34*84d9c625SLionel Sambuc 	GET_FLOAT_WORD(hx,x);
35*84d9c625SLionel Sambuc 	GET_FLOAT_WORD(hy,y);
36*84d9c625SLionel Sambuc 	sxy = (hx ^ hy) & 0x80000000;
37*84d9c625SLionel Sambuc 	sx = hx&0x80000000;		/* sign of x */
38*84d9c625SLionel Sambuc 	hx ^=sx;		/* |x| */
39*84d9c625SLionel Sambuc 	hy &= 0x7fffffff;	/* |y| */
40*84d9c625SLionel Sambuc 
41*84d9c625SLionel Sambuc     /* purge off exception values */
42*84d9c625SLionel Sambuc 	if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */
43*84d9c625SLionel Sambuc 	    return (x*y)/(x*y);
44*84d9c625SLionel Sambuc 	if(hx<hy) {
45*84d9c625SLionel Sambuc 	    q = 0;
46*84d9c625SLionel Sambuc 	    goto fixup;	/* |x|<|y| return x or x-y */
47*84d9c625SLionel Sambuc 	} else if(hx==hy) {
48*84d9c625SLionel Sambuc 	    *quo = 1;
49*84d9c625SLionel Sambuc 	    return Zero[(u_int32_t)sx>>31];	/* |x|=|y| return x*0*/
50*84d9c625SLionel Sambuc 	}
51*84d9c625SLionel Sambuc 
52*84d9c625SLionel Sambuc     /* determine ix = ilogb(x) */
53*84d9c625SLionel Sambuc 	if(hx<0x00800000) {	/* subnormal x */
54*84d9c625SLionel Sambuc 	    for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
55*84d9c625SLionel Sambuc 	} else ix = (hx>>23)-127;
56*84d9c625SLionel Sambuc 
57*84d9c625SLionel Sambuc     /* determine iy = ilogb(y) */
58*84d9c625SLionel Sambuc 	if(hy<0x00800000) {	/* subnormal y */
59*84d9c625SLionel Sambuc 	    for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1;
60*84d9c625SLionel Sambuc 	} else iy = (hy>>23)-127;
61*84d9c625SLionel Sambuc 
62*84d9c625SLionel Sambuc     /* set up {hx,lx}, {hy,ly} and align y to x */
63*84d9c625SLionel Sambuc 	if(ix >= -126)
64*84d9c625SLionel Sambuc 	    hx = 0x00800000|(0x007fffff&hx);
65*84d9c625SLionel Sambuc 	else {		/* subnormal x, shift x to normal */
66*84d9c625SLionel Sambuc 	    n = -126-ix;
67*84d9c625SLionel Sambuc 	    hx <<= n;
68*84d9c625SLionel Sambuc 	}
69*84d9c625SLionel Sambuc 	if(iy >= -126)
70*84d9c625SLionel Sambuc 	    hy = 0x00800000|(0x007fffff&hy);
71*84d9c625SLionel Sambuc 	else {		/* subnormal y, shift y to normal */
72*84d9c625SLionel Sambuc 	    n = -126-iy;
73*84d9c625SLionel Sambuc 	    hy <<= n;
74*84d9c625SLionel Sambuc 	}
75*84d9c625SLionel Sambuc 
76*84d9c625SLionel Sambuc     /* fix point fmod */
77*84d9c625SLionel Sambuc 	n = ix - iy;
78*84d9c625SLionel Sambuc 	q = 0;
79*84d9c625SLionel Sambuc 	while(n--) {
80*84d9c625SLionel Sambuc 	    hz=hx-hy;
81*84d9c625SLionel Sambuc 	    if(hz<0) hx = hx << 1;
82*84d9c625SLionel Sambuc 	    else {hx = hz << 1; q++;}
83*84d9c625SLionel Sambuc 	    q <<= 1;
84*84d9c625SLionel Sambuc 	}
85*84d9c625SLionel Sambuc 	hz=hx-hy;
86*84d9c625SLionel Sambuc 	if(hz>=0) {hx=hz;q++;}
87*84d9c625SLionel Sambuc 
88*84d9c625SLionel Sambuc     /* convert back to floating value and restore the sign */
89*84d9c625SLionel Sambuc 	if(hx==0) {				/* return sign(x)*0 */
90*84d9c625SLionel Sambuc 	    *quo = (sxy ? -q : q);
91*84d9c625SLionel Sambuc 	    return Zero[(u_int32_t)sx>>31];
92*84d9c625SLionel Sambuc 	}
93*84d9c625SLionel Sambuc 	while(hx<0x00800000) {		/* normalize x */
94*84d9c625SLionel Sambuc 	    hx <<= 1;
95*84d9c625SLionel Sambuc 	    iy -= 1;
96*84d9c625SLionel Sambuc 	}
97*84d9c625SLionel Sambuc 	if(iy>= -126) {		/* normalize output */
98*84d9c625SLionel Sambuc 	    hx = ((hx-0x00800000)|((iy+127)<<23));
99*84d9c625SLionel Sambuc 	} else {		/* subnormal output */
100*84d9c625SLionel Sambuc 	    n = -126 - iy;
101*84d9c625SLionel Sambuc 	    hx >>= n;
102*84d9c625SLionel Sambuc 	}
103*84d9c625SLionel Sambuc fixup:
104*84d9c625SLionel Sambuc 	SET_FLOAT_WORD(x,hx);
105*84d9c625SLionel Sambuc 	y = fabsf(y);
106*84d9c625SLionel Sambuc 	if (y < 0x1p-125f) {
107*84d9c625SLionel Sambuc 	    if (x+x>y || (x+x==y && (q & 1))) {
108*84d9c625SLionel Sambuc 		q++;
109*84d9c625SLionel Sambuc 		x-=y;
110*84d9c625SLionel Sambuc 	    }
111*84d9c625SLionel Sambuc 	} else if (x>0.5f*y || (x==0.5f*y && (q & 1))) {
112*84d9c625SLionel Sambuc 	    q++;
113*84d9c625SLionel Sambuc 	    x-=y;
114*84d9c625SLionel Sambuc 	}
115*84d9c625SLionel Sambuc 	GET_FLOAT_WORD(hx,x);
116*84d9c625SLionel Sambuc 	SET_FLOAT_WORD(x,hx^sx);
117*84d9c625SLionel Sambuc 	q &= 0x7fffffff;
118*84d9c625SLionel Sambuc 	*quo = (sxy ? -q : q);
119*84d9c625SLionel Sambuc 	return x;
120*84d9c625SLionel Sambuc }
121