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