|
1 /*-------------------------------------------------------------------- |
|
2 *© Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
3 *-------------------------------------------------------------------- |
|
4 */ |
|
5 /* @(#)e_fmod.c 1.3 95/01/18 */ |
|
6 /*- |
|
7 * ==================================================== |
|
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
|
9 * |
|
10 * Developed at SunSoft, a Sun Microsystems, Inc. business. |
|
11 * Permission to use, copy, modify, and distribute this |
|
12 * software is freely granted, provided that this notice |
|
13 * is preserved. |
|
14 * ==================================================== |
|
15 */ |
|
16 |
|
17 #include <sys/cdefs.h> |
|
18 #ifndef __SYMBIAN32__ |
|
19 __FBSDID("$FreeBSD: src/lib/msun/src/s_remquo.c,v 1.1 2005/03/25 04:40:44 das Exp $"); |
|
20 #endif //__SYMBIAN32__ |
|
21 |
|
22 #include <math.h> |
|
23 #include "math_private.h" |
|
24 |
|
25 static const double Zero[] = {0.0, -0.0,}; |
|
26 |
|
27 /* |
|
28 * Return the IEEE remainder and set *quo to the last n bits of the |
|
29 * quotient, rounded to the nearest integer. We choose n=31 because |
|
30 * we wind up computing all the integer bits of the quotient anyway as |
|
31 * a side-effect of computing the remainder by the shift and subtract |
|
32 * method. In practice, this is far more bits than are needed to use |
|
33 * remquo in reduction algorithms. |
|
34 */ |
|
35 EXPORT_C double |
|
36 remquo(double x, double y, int *quo) |
|
37 { |
|
38 int32_t n,hx,hy,hz,ix,iy,sx,i; |
|
39 u_int32_t lx,ly,lz,q,sxy; |
|
40 |
|
41 EXTRACT_WORDS(hx,lx,x); |
|
42 EXTRACT_WORDS(hy,ly,y); |
|
43 sxy = (hx ^ hy) & 0x80000000; |
|
44 sx = hx&0x80000000; /* sign of x */ |
|
45 hx ^=sx; /* |x| */ |
|
46 hy &= 0x7fffffff; /* |y| */ |
|
47 |
|
48 /* purge off exception values */ |
|
49 if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ |
|
50 ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ |
|
51 return (x*y)/(x*y); |
|
52 if(hx<=hy) { |
|
53 if((hx<hy)||(lx<ly)) { |
|
54 q = 0; |
|
55 goto fixup; /* |x|<|y| return x or x-y */ |
|
56 } |
|
57 if(lx==ly) { |
|
58 *quo = 1; |
|
59 return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/ |
|
60 } |
|
61 } |
|
62 |
|
63 /* determine ix = ilogb(x) */ |
|
64 if(hx<0x00100000) { /* subnormal x */ |
|
65 if(hx==0) { |
|
66 for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; |
|
67 } else { |
|
68 for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; |
|
69 } |
|
70 } else ix = (hx>>20)-1023; |
|
71 |
|
72 /* determine iy = ilogb(y) */ |
|
73 if(hy<0x00100000) { /* subnormal y */ |
|
74 if(hy==0) { |
|
75 for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; |
|
76 } else { |
|
77 for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; |
|
78 } |
|
79 } else iy = (hy>>20)-1023; |
|
80 |
|
81 /* set up {hx,lx}, {hy,ly} and align y to x */ |
|
82 if(ix >= -1022) |
|
83 hx = 0x00100000|(0x000fffff&hx); |
|
84 else { /* subnormal x, shift x to normal */ |
|
85 n = -1022-ix; |
|
86 if(n<=31) { |
|
87 hx = (hx<<n)|(lx>>(32-n)); |
|
88 lx <<= n; |
|
89 } else { |
|
90 hx = lx<<(n-32); |
|
91 lx = 0; |
|
92 } |
|
93 } |
|
94 if(iy >= -1022) |
|
95 hy = 0x00100000|(0x000fffff&hy); |
|
96 else { /* subnormal y, shift y to normal */ |
|
97 n = -1022-iy; |
|
98 if(n<=31) { |
|
99 hy = (hy<<n)|(ly>>(32-n)); |
|
100 ly <<= n; |
|
101 } else { |
|
102 hy = ly<<(n-32); |
|
103 ly = 0; |
|
104 } |
|
105 } |
|
106 |
|
107 /* fix point fmod */ |
|
108 n = ix - iy; |
|
109 q = 0; |
|
110 while(n--) { |
|
111 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; |
|
112 if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;} |
|
113 else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;} |
|
114 q <<= 1; |
|
115 } |
|
116 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; |
|
117 if(hz>=0) {hx=hz;lx=lz;q++;} |
|
118 |
|
119 /* convert back to floating value and restore the sign */ |
|
120 if((hx|lx)==0) { /* return sign(x)*0 */ |
|
121 *quo = (sxy ? -q : q); |
|
122 return Zero[(u_int32_t)sx>>31]; |
|
123 } |
|
124 while(hx<0x00100000) { /* normalize x */ |
|
125 hx = hx+hx+(lx>>31); lx = lx+lx; |
|
126 iy -= 1; |
|
127 } |
|
128 if(iy>= -1022) { /* normalize output */ |
|
129 hx = ((hx-0x00100000)|((iy+1023)<<20)); |
|
130 } else { /* subnormal output */ |
|
131 n = -1022 - iy; |
|
132 if(n<=20) { |
|
133 lx = (lx>>n)|((u_int32_t)hx<<(32-n)); |
|
134 hx >>= n; |
|
135 } else if (n<=31) { |
|
136 lx = (hx<<(32-n))|(lx>>n); hx = sx; |
|
137 } else { |
|
138 lx = hx>>(n-32); hx = sx; |
|
139 } |
|
140 } |
|
141 fixup: |
|
142 INSERT_WORDS(x,hx,lx); |
|
143 y = fabs(y); |
|
144 #ifdef __SYMBIAN32__ |
|
145 if (y < 4.45015e-308) { |
|
146 #else |
|
147 if (y < 0x1p-1021) { |
|
148 #endif //__SYMBIAN32__ |
|
149 if (x+x>y || (x+x==y && (q & 1))) { |
|
150 q++; |
|
151 x-=y; |
|
152 } |
|
153 } else if (x>0.5*y || (x==0.5*y && (q & 1))) { |
|
154 q++; |
|
155 x-=y; |
|
156 } |
|
157 GET_HIGH_WORD(hx,x); |
|
158 SET_HIGH_WORD(x,hx^sx); |
|
159 q &= 0x7fffffff; |
|
160 *quo = (sxy ? -q : q); |
|
161 return x; |
|
162 } |