xref: /netbsd/lib/libm/src/e_fmodf.c (revision bf9ec67e)
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 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: e_fmodf.c,v 1.7 2002/05/26 22:01:49 wiz Exp $");
19 #endif
20 
21 /*
22  * __ieee754_fmodf(x,y)
23  * Return x mod y in exact arithmetic
24  * Method: shift and subtract
25  */
26 
27 #include "math.h"
28 #include "math_private.h"
29 
30 static const float one = 1.0, Zero[] = {0.0, -0.0,};
31 
32 float
33 __ieee754_fmodf(float x, float y)
34 {
35 	int32_t n,hx,hy,hz,ix,iy,sx,i;
36 
37 	GET_FLOAT_WORD(hx,x);
38 	GET_FLOAT_WORD(hy,y);
39 	sx = hx&0x80000000;		/* sign of x */
40 	hx ^=sx;		/* |x| */
41 	hy &= 0x7fffffff;	/* |y| */
42 
43     /* purge off exception values */
44 	if(hy==0||(hx>=0x7f800000)||		/* y=0,or x not finite */
45 	   (hy>0x7f800000))			/* or y is NaN */
46 	    return (x*y)/(x*y);
47 	if(hx<hy) return x;			/* |x|<|y| return x */
48 	if(hx==hy)
49 	    return Zero[(u_int32_t)sx>>31];	/* |x|=|y| return x*0*/
50 
51     /* determine ix = ilogb(x) */
52 	if(hx<0x00800000) {	/* subnormal x */
53 	    for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
54 	} else ix = (hx>>23)-127;
55 
56     /* determine iy = ilogb(y) */
57 	if(hy<0x00800000) {	/* subnormal y */
58 	    for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
59 	} else iy = (hy>>23)-127;
60 
61     /* set up {hx,lx}, {hy,ly} and align y to x */
62 	if(ix >= -126)
63 	    hx = 0x00800000|(0x007fffff&hx);
64 	else {		/* subnormal x, shift x to normal */
65 	    n = -126-ix;
66 	    hx = hx<<n;
67 	}
68 	if(iy >= -126)
69 	    hy = 0x00800000|(0x007fffff&hy);
70 	else {		/* subnormal y, shift y to normal */
71 	    n = -126-iy;
72 	    hy = hy<<n;
73 	}
74 
75     /* fix point fmod */
76 	n = ix - iy;
77 	while(n--) {
78 	    hz=hx-hy;
79 	    if(hz<0){hx = hx+hx;}
80 	    else {
81 	    	if(hz==0) 		/* return sign(x)*0 */
82 		    return Zero[(u_int32_t)sx>>31];
83 	    	hx = hz+hz;
84 	    }
85 	}
86 	hz=hx-hy;
87 	if(hz>=0) {hx=hz;}
88 
89     /* convert back to floating value and restore the sign */
90 	if(hx==0) 			/* return sign(x)*0 */
91 	    return Zero[(u_int32_t)sx>>31];
92 	while(hx<0x00800000) {		/* normalize x */
93 	    hx = hx+hx;
94 	    iy -= 1;
95 	}
96 	if(iy>= -126) {		/* normalize output */
97 	    hx = ((hx-0x00800000)|((iy+127)<<23));
98 	    SET_FLOAT_WORD(x,hx|sx);
99 	} else {		/* subnormal output */
100 	    n = -126 - iy;
101 	    hx >>= n;
102 	    SET_FLOAT_WORD(x,hx|sx);
103 	    x *= one;		/* create necessary signal */
104 	}
105 	return x;		/* exact output */
106 }
107