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