add .gitignore file
[musl] / src / math / s_remquo.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 <math.h>
14 #include "math_private.h"
15
16 static const double Zero[] = {0.0, -0.0,};
17
18 /*
19  * Return the IEEE remainder and set *quo to the last n bits of the
20  * quotient, rounded to the nearest integer.  We choose n=31 because
21  * we wind up computing all the integer bits of the quotient anyway as
22  * a side-effect of computing the remainder by the shift and subtract
23  * method.  In practice, this is far more bits than are needed to use
24  * remquo in reduction algorithms.
25  */
26 double
27 remquo(double x, double y, int *quo)
28 {
29         int32_t n,hx,hy,hz,ix,iy,sx,i;
30         uint32_t lx,ly,lz,q,sxy;
31
32         EXTRACT_WORDS(hx,lx,x);
33         EXTRACT_WORDS(hy,ly,y);
34         sxy = (hx ^ hy) & 0x80000000;
35         sx = hx&0x80000000;             /* sign of x */
36         hx ^=sx;                /* |x| */
37         hy &= 0x7fffffff;       /* |y| */
38
39     /* purge off exception values */
40         if((hy|ly)==0||(hx>=0x7ff00000)||       /* y=0,or x not finite */
41           ((hy|((ly|-ly)>>31))>0x7ff00000))     /* or y is NaN */
42             return (x*y)/(x*y);
43         if(hx<=hy) {
44             if((hx<hy)||(lx<ly)) {
45                 q = 0;
46                 goto fixup;     /* |x|<|y| return x or x-y */
47             }
48             if(lx==ly) {
49                 *quo = 1;
50                 return Zero[(uint32_t)sx>>31]; /* |x|=|y| return x*0*/
51             }
52         }
53
54     /* determine ix = ilogb(x) */
55         if(hx<0x00100000) {     /* subnormal x */
56             if(hx==0) {
57                 for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
58             } else {
59                 for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
60             }
61         } else ix = (hx>>20)-1023;
62
63     /* determine iy = ilogb(y) */
64         if(hy<0x00100000) {     /* subnormal y */
65             if(hy==0) {
66                 for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
67             } else {
68                 for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
69             }
70         } else iy = (hy>>20)-1023;
71
72     /* set up {hx,lx}, {hy,ly} and align y to x */
73         if(ix >= -1022) 
74             hx = 0x00100000|(0x000fffff&hx);
75         else {          /* subnormal x, shift x to normal */
76             n = -1022-ix;
77             if(n<=31) {
78                 hx = (hx<<n)|(lx>>(32-n));
79                 lx <<= n;
80             } else {
81                 hx = lx<<(n-32);
82                 lx = 0;
83             }
84         }
85         if(iy >= -1022) 
86             hy = 0x00100000|(0x000fffff&hy);
87         else {          /* subnormal y, shift y to normal */
88             n = -1022-iy;
89             if(n<=31) {
90                 hy = (hy<<n)|(ly>>(32-n));
91                 ly <<= n;
92             } else {
93                 hy = ly<<(n-32);
94                 ly = 0;
95             }
96         }
97
98     /* fix point fmod */
99         n = ix - iy;
100         q = 0;
101         while(n--) {
102             hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
103             if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
104             else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;}
105             q <<= 1;
106         }
107         hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
108         if(hz>=0) {hx=hz;lx=lz;q++;}
109
110     /* convert back to floating value and restore the sign */
111         if((hx|lx)==0) {                        /* return sign(x)*0 */
112             *quo = (sxy ? -q : q);
113             return Zero[(uint32_t)sx>>31];
114         }
115         while(hx<0x00100000) {          /* normalize x */
116             hx = hx+hx+(lx>>31); lx = lx+lx;
117             iy -= 1;
118         }
119         if(iy>= -1022) {        /* normalize output */
120             hx = ((hx-0x00100000)|((iy+1023)<<20));
121         } else {                /* subnormal output */
122             n = -1022 - iy;
123             if(n<=20) {
124                 lx = (lx>>n)|((uint32_t)hx<<(32-n));
125                 hx >>= n;
126             } else if (n<=31) {
127                 lx = (hx<<(32-n))|(lx>>n); hx = sx;
128             } else {
129                 lx = hx>>(n-32); hx = sx;
130             }
131         }
132 fixup:
133         INSERT_WORDS(x,hx,lx);
134         y = fabs(y);
135         if (y < 0x1p-1021) {
136             if (x+x>y || (x+x==y && (q & 1))) {
137                 q++;
138                 x-=y;
139             }
140         } else if (x>0.5*y || (x==0.5*y && (q & 1))) {
141             q++;
142             x-=y;
143         }
144         GET_HIGH_WORD(hx,x);
145         SET_HIGH_WORD(x,hx^sx);
146         q &= 0x7fffffff;
147         *quo = (sxy ? -q : q);
148         return x;
149 }