|
1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 #include "precompiled.hpp" |
|
26 |
|
27 #ifdef _WIN64 |
|
28 // These are copied defines from fdlibm.h, this allows us to keep the code |
|
29 // the same as in the JDK, for easier maintenance. |
|
30 |
|
31 #define __HI(x) *(1+(int*)&x) |
|
32 #define __LO(x) *(int*)&x |
|
33 |
|
34 // This code is a copy of __ieee754_fmod() from the JDK's libfdlibm and is |
|
35 // used as a workaround for issues with the Windows x64 CRT implementation |
|
36 // of fmod. Microsoft has acknowledged that this is an issue in Visual Studio |
|
37 // 2012 and forward, but has not provided a time frame for a fix other than that |
|
38 // it'll not be fixed in Visual Studio 2013 or 2015. |
|
39 |
|
40 static const double one = 1.0, Zero[] = { 0.0, -0.0, }; |
|
41 |
|
42 double SharedRuntime::fmod_winx64(double x, double y) |
|
43 { |
|
44 int n, hx, hy, hz, ix, iy, sx, i; |
|
45 unsigned lx, ly, lz; |
|
46 |
|
47 hx = __HI(x); /* high word of x */ |
|
48 lx = __LO(x); /* low word of x */ |
|
49 hy = __HI(y); /* high word of y */ |
|
50 ly = __LO(y); /* low word of y */ |
|
51 sx = hx & 0x80000000; /* sign of x */ |
|
52 hx ^= sx; /* |x| */ |
|
53 hy &= 0x7fffffff; /* |y| */ |
|
54 |
|
55 #pragma warning( disable : 4146 ) |
|
56 /* purge off exception values */ |
|
57 if ((hy | ly) == 0 || (hx >= 0x7ff00000) || /* y=0,or x not finite */ |
|
58 ((hy | ((ly | -ly) >> 31))>0x7ff00000)) /* or y is NaN */ |
|
59 #pragma warning( default : 4146 ) |
|
60 return (x*y) / (x*y); |
|
61 if (hx <= hy) { |
|
62 if ((hx<hy) || (lx<ly)) return x; /* |x|<|y| return x */ |
|
63 if (lx == ly) |
|
64 return Zero[(unsigned)sx >> 31]; /* |x|=|y| return x*0*/ |
|
65 } |
|
66 |
|
67 /* determine ix = ilogb(x) */ |
|
68 if (hx<0x00100000) { /* subnormal x */ |
|
69 if (hx == 0) { |
|
70 for (ix = -1043, i = lx; i>0; i <<= 1) ix -= 1; |
|
71 } |
|
72 else { |
|
73 for (ix = -1022, i = (hx << 11); i>0; i <<= 1) ix -= 1; |
|
74 } |
|
75 } |
|
76 else ix = (hx >> 20) - 1023; |
|
77 |
|
78 /* determine iy = ilogb(y) */ |
|
79 if (hy<0x00100000) { /* subnormal y */ |
|
80 if (hy == 0) { |
|
81 for (iy = -1043, i = ly; i>0; i <<= 1) iy -= 1; |
|
82 } |
|
83 else { |
|
84 for (iy = -1022, i = (hy << 11); i>0; i <<= 1) iy -= 1; |
|
85 } |
|
86 } |
|
87 else iy = (hy >> 20) - 1023; |
|
88 |
|
89 /* set up {hx,lx}, {hy,ly} and align y to x */ |
|
90 if (ix >= -1022) |
|
91 hx = 0x00100000 | (0x000fffff & hx); |
|
92 else { /* subnormal x, shift x to normal */ |
|
93 n = -1022 - ix; |
|
94 if (n <= 31) { |
|
95 hx = (hx << n) | (lx >> (32 - n)); |
|
96 lx <<= n; |
|
97 } |
|
98 else { |
|
99 hx = lx << (n - 32); |
|
100 lx = 0; |
|
101 } |
|
102 } |
|
103 if (iy >= -1022) |
|
104 hy = 0x00100000 | (0x000fffff & hy); |
|
105 else { /* subnormal y, shift y to normal */ |
|
106 n = -1022 - iy; |
|
107 if (n <= 31) { |
|
108 hy = (hy << n) | (ly >> (32 - n)); |
|
109 ly <<= n; |
|
110 } |
|
111 else { |
|
112 hy = ly << (n - 32); |
|
113 ly = 0; |
|
114 } |
|
115 } |
|
116 |
|
117 /* fix point fmod */ |
|
118 n = ix - iy; |
|
119 while (n--) { |
|
120 hz = hx - hy; lz = lx - ly; if (lx<ly) hz -= 1; |
|
121 if (hz<0){ hx = hx + hx + (lx >> 31); lx = lx + lx; } |
|
122 else { |
|
123 if ((hz | lz) == 0) /* return sign(x)*0 */ |
|
124 return Zero[(unsigned)sx >> 31]; |
|
125 hx = hz + hz + (lz >> 31); lx = lz + lz; |
|
126 } |
|
127 } |
|
128 hz = hx - hy; lz = lx - ly; if (lx<ly) hz -= 1; |
|
129 if (hz >= 0) { hx = hz; lx = lz; } |
|
130 |
|
131 /* convert back to floating value and restore the sign */ |
|
132 if ((hx | lx) == 0) /* return sign(x)*0 */ |
|
133 return Zero[(unsigned)sx >> 31]; |
|
134 while (hx<0x00100000) { /* normalize x */ |
|
135 hx = hx + hx + (lx >> 31); lx = lx + lx; |
|
136 iy -= 1; |
|
137 } |
|
138 if (iy >= -1022) { /* normalize output */ |
|
139 hx = ((hx - 0x00100000) | ((iy + 1023) << 20)); |
|
140 __HI(x) = hx | sx; |
|
141 __LO(x) = lx; |
|
142 } |
|
143 else { /* subnormal output */ |
|
144 n = -1022 - iy; |
|
145 if (n <= 20) { |
|
146 lx = (lx >> n) | ((unsigned)hx << (32 - n)); |
|
147 hx >>= n; |
|
148 } |
|
149 else if (n <= 31) { |
|
150 lx = (hx << (32 - n)) | (lx >> n); hx = sx; |
|
151 } |
|
152 else { |
|
153 lx = hx >> (n - 32); hx = sx; |
|
154 } |
|
155 __HI(x) = hx | sx; |
|
156 __LO(x) = lx; |
|
157 x *= one; /* create necessary signal */ |
|
158 } |
|
159 return x; /* exact output */ |
|
160 } |
|
161 |
|
162 #endif |