ipiq: Add simple IPI latency measure sysctls (2)
[dragonfly.git] / lib / libm / 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
14 #include "math.h"
15 #include "math_private.h"
16
17 static const float Zero[] = {0.0, -0.0,};
18
19 /*
20  * Return the IEEE remainder and set *quo to the last n bits of the
21  * quotient, rounded to the nearest integer.  We choose n=31 because
22  * we wind up computing all the integer bits of the quotient anyway as
23  * a side-effect of computing the remainder by the shift and subtract
24  * method.  In practice, this is far more bits than are needed to use
25  * remquo in reduction algorithms.
26  */
27 float
28 remquof(float x, float y, int *quo)
29 {
30         int32_t n,hx,hy,hz,ix,iy,sx,i;
31         u_int32_t q,sxy;
32
33         GET_FLOAT_WORD(hx,x);
34         GET_FLOAT_WORD(hy,y);
35         sxy = (hx ^ hy) & 0x80000000;
36         sx = hx&0x80000000;             /* sign of x */
37         hx ^=sx;                /* |x| */
38         hy &= 0x7fffffff;       /* |y| */
39
40     /* purge off exception values */
41         if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */
42             return (x*y)/(x*y);
43         if(hx<hy) {
44             q = 0;
45             goto fixup; /* |x|<|y| return x or x-y */
46         } else if(hx==hy) {
47             *quo = (sxy ? -1 : 1);
48             return Zero[(u_int32_t)sx>>31];     /* |x|=|y| return x*0*/
49         }
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 <<= 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 <<= n;
73         }
74
75     /* fix point fmod */
76         n = ix - iy;
77         q = 0;
78         while(n--) {
79             hz=hx-hy;
80             if(hz<0) hx = hx << 1;
81             else {hx = hz << 1; q++;}
82             q <<= 1;
83         }
84         hz=hx-hy;
85         if(hz>=0) {hx=hz;q++;}
86
87     /* convert back to floating value and restore the sign */
88         if(hx==0) {                             /* return sign(x)*0 */
89             q &= 0x7fffffff;
90             *quo = (sxy ? -q : q);
91             return Zero[(u_int32_t)sx>>31];
92         }
93         while(hx<0x00800000) {          /* normalize x */
94             hx <<= 1;
95             iy -= 1;
96         }
97         if(iy>= -126) {         /* normalize output */
98             hx = ((hx-0x00800000)|((iy+127)<<23));
99         } else {                /* subnormal output */
100             n = -126 - iy;
101             hx >>= n;
102         }
103 fixup:
104         SET_FLOAT_WORD(x,hx);
105         y = fabsf(y);
106         if (y < 0x1p-125f) {
107             if (x+x>y || (x+x==y && (q & 1))) {
108                 q++;
109                 x-=y;
110             }
111         } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) {
112             q++;
113             x-=y;
114         }
115         GET_FLOAT_WORD(hx,x);
116         SET_FLOAT_WORD(x,hx^sx);
117         q &= 0x7fffffff;
118         *quo = (sxy ? -q : q);
119         return x;
120 }