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