xref: /minix3/lib/libm/src/e_fmodf.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* e_fmodf.c -- float version of e_fmod.c.
2*2fe8fb19SBen Gras  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*2fe8fb19SBen Gras  */
4*2fe8fb19SBen Gras 
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras  * ====================================================
7*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*2fe8fb19SBen Gras  *
9*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
11*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
12*2fe8fb19SBen Gras  * is preserved.
13*2fe8fb19SBen Gras  * ====================================================
14*2fe8fb19SBen Gras  */
15*2fe8fb19SBen Gras 
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: e_fmodf.c,v 1.7 2002/05/26 22:01:49 wiz Exp $");
19*2fe8fb19SBen Gras #endif
20*2fe8fb19SBen Gras 
21*2fe8fb19SBen Gras /*
22*2fe8fb19SBen Gras  * __ieee754_fmodf(x,y)
23*2fe8fb19SBen Gras  * Return x mod y in exact arithmetic
24*2fe8fb19SBen Gras  * Method: shift and subtract
25*2fe8fb19SBen Gras  */
26*2fe8fb19SBen Gras 
27*2fe8fb19SBen Gras #include "math.h"
28*2fe8fb19SBen Gras #include "math_private.h"
29*2fe8fb19SBen Gras 
30*2fe8fb19SBen Gras static const float one = 1.0, Zero[] = {0.0, -0.0,};
31*2fe8fb19SBen Gras 
32*2fe8fb19SBen Gras float
__ieee754_fmodf(float x,float y)33*2fe8fb19SBen Gras __ieee754_fmodf(float x, float y)
34*2fe8fb19SBen Gras {
35*2fe8fb19SBen Gras 	int32_t n,hx,hy,hz,ix,iy,sx,i;
36*2fe8fb19SBen Gras 
37*2fe8fb19SBen Gras 	GET_FLOAT_WORD(hx,x);
38*2fe8fb19SBen Gras 	GET_FLOAT_WORD(hy,y);
39*2fe8fb19SBen Gras 	sx = hx&0x80000000;		/* sign of x */
40*2fe8fb19SBen Gras 	hx ^=sx;		/* |x| */
41*2fe8fb19SBen Gras 	hy &= 0x7fffffff;	/* |y| */
42*2fe8fb19SBen Gras 
43*2fe8fb19SBen Gras     /* purge off exception values */
44*2fe8fb19SBen Gras 	if(hy==0||(hx>=0x7f800000)||		/* y=0,or x not finite */
45*2fe8fb19SBen Gras 	   (hy>0x7f800000))			/* or y is NaN */
46*2fe8fb19SBen Gras 	    return (x*y)/(x*y);
47*2fe8fb19SBen Gras 	if(hx<hy) return x;			/* |x|<|y| return x */
48*2fe8fb19SBen Gras 	if(hx==hy)
49*2fe8fb19SBen Gras 	    return Zero[(u_int32_t)sx>>31];	/* |x|=|y| return x*0*/
50*2fe8fb19SBen Gras 
51*2fe8fb19SBen Gras     /* determine ix = ilogb(x) */
52*2fe8fb19SBen Gras 	if(hx<0x00800000) {	/* subnormal x */
53*2fe8fb19SBen Gras 	    for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
54*2fe8fb19SBen Gras 	} else ix = (hx>>23)-127;
55*2fe8fb19SBen Gras 
56*2fe8fb19SBen Gras     /* determine iy = ilogb(y) */
57*2fe8fb19SBen Gras 	if(hy<0x00800000) {	/* subnormal y */
58*2fe8fb19SBen Gras 	    for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
59*2fe8fb19SBen Gras 	} else iy = (hy>>23)-127;
60*2fe8fb19SBen Gras 
61*2fe8fb19SBen Gras     /* set up {hx,lx}, {hy,ly} and align y to x */
62*2fe8fb19SBen Gras 	if(ix >= -126)
63*2fe8fb19SBen Gras 	    hx = 0x00800000|(0x007fffff&hx);
64*2fe8fb19SBen Gras 	else {		/* subnormal x, shift x to normal */
65*2fe8fb19SBen Gras 	    n = -126-ix;
66*2fe8fb19SBen Gras 	    hx = hx<<n;
67*2fe8fb19SBen Gras 	}
68*2fe8fb19SBen Gras 	if(iy >= -126)
69*2fe8fb19SBen Gras 	    hy = 0x00800000|(0x007fffff&hy);
70*2fe8fb19SBen Gras 	else {		/* subnormal y, shift y to normal */
71*2fe8fb19SBen Gras 	    n = -126-iy;
72*2fe8fb19SBen Gras 	    hy = hy<<n;
73*2fe8fb19SBen Gras 	}
74*2fe8fb19SBen Gras 
75*2fe8fb19SBen Gras     /* fix point fmod */
76*2fe8fb19SBen Gras 	n = ix - iy;
77*2fe8fb19SBen Gras 	while(n--) {
78*2fe8fb19SBen Gras 	    hz=hx-hy;
79*2fe8fb19SBen Gras 	    if(hz<0){hx = hx+hx;}
80*2fe8fb19SBen Gras 	    else {
81*2fe8fb19SBen Gras 	    	if(hz==0) 		/* return sign(x)*0 */
82*2fe8fb19SBen Gras 		    return Zero[(u_int32_t)sx>>31];
83*2fe8fb19SBen Gras 	    	hx = hz+hz;
84*2fe8fb19SBen Gras 	    }
85*2fe8fb19SBen Gras 	}
86*2fe8fb19SBen Gras 	hz=hx-hy;
87*2fe8fb19SBen Gras 	if(hz>=0) {hx=hz;}
88*2fe8fb19SBen Gras 
89*2fe8fb19SBen Gras     /* convert back to floating value and restore the sign */
90*2fe8fb19SBen Gras 	if(hx==0) 			/* return sign(x)*0 */
91*2fe8fb19SBen Gras 	    return Zero[(u_int32_t)sx>>31];
92*2fe8fb19SBen Gras 	while(hx<0x00800000) {		/* normalize x */
93*2fe8fb19SBen Gras 	    hx = hx+hx;
94*2fe8fb19SBen Gras 	    iy -= 1;
95*2fe8fb19SBen Gras 	}
96*2fe8fb19SBen Gras 	if(iy>= -126) {		/* normalize output */
97*2fe8fb19SBen Gras 	    hx = ((hx-0x00800000)|((iy+127)<<23));
98*2fe8fb19SBen Gras 	    SET_FLOAT_WORD(x,hx|sx);
99*2fe8fb19SBen Gras 	} else {		/* subnormal output */
100*2fe8fb19SBen Gras 	    n = -126 - iy;
101*2fe8fb19SBen Gras 	    hx >>= n;
102*2fe8fb19SBen Gras 	    SET_FLOAT_WORD(x,hx|sx);
103*2fe8fb19SBen Gras 	    x *= one;		/* create necessary signal */
104*2fe8fb19SBen Gras 	}
105*2fe8fb19SBen Gras 	return x;		/* exact output */
106*2fe8fb19SBen Gras }
107