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