1 /*
2 * Copyright (c) 2015, 2018, 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 #include "runtime/sharedRuntime.hpp"
27
28 #ifdef _WIN64
29 // These are copied defines from fdlibm.h, this allows us to keep the code
30 // the same as in the JDK, for easier maintenance.
31
32 #define __HI(x) *(1+(int*)&x)
33 #define __LO(x) *(int*)&x
34
35 // This code is a copy of __ieee754_fmod() from the JDK's libfdlibm and is
36 // used as a workaround for issues with the Windows x64 CRT implementation
37 // of fmod. Microsoft has acknowledged that this is an issue in Visual Studio
38 // 2012 and forward, but has not provided a time frame for a fix other than that
39 // it'll not be fixed in Visual Studio 2013 or 2015.
40
41 static const double one = 1.0, Zero[] = { 0.0, -0.0, };
42
fmod_winx64(double x,double y)43 double SharedRuntime::fmod_winx64(double x, double y)
44 {
45 int n, hx, hy, hz, ix, iy, sx, i;
46 unsigned lx, ly, lz;
47
48 hx = __HI(x); /* high word of x */
49 lx = __LO(x); /* low word of x */
50 hy = __HI(y); /* high word of y */
51 ly = __LO(y); /* low word of y */
52 sx = hx & 0x80000000; /* sign of x */
53 hx ^= sx; /* |x| */
54 hy &= 0x7fffffff; /* |y| */
55
56 #pragma warning( disable : 4146 )
57 /* purge off exception values */
58 if ((hy | ly) == 0 || (hx >= 0x7ff00000) || /* y=0,or x not finite */
59 ((hy | ((ly | -ly) >> 31))>0x7ff00000)) /* or y is NaN */
60 #pragma warning( default : 4146 )
61 return (x*y) / (x*y);
62 if (hx <= hy) {
63 if ((hx<hy) || (lx<ly)) return x; /* |x|<|y| return x */
64 if (lx == ly)
65 return Zero[(unsigned)sx >> 31]; /* |x|=|y| return x*0*/
66 }
67
68 /* determine ix = ilogb(x) */
69 if (hx<0x00100000) { /* subnormal x */
70 if (hx == 0) {
71 for (ix = -1043, i = lx; i>0; i <<= 1) ix -= 1;
72 }
73 else {
74 for (ix = -1022, i = (hx << 11); i>0; i <<= 1) ix -= 1;
75 }
76 }
77 else ix = (hx >> 20) - 1023;
78
79 /* determine iy = ilogb(y) */
80 if (hy<0x00100000) { /* subnormal y */
81 if (hy == 0) {
82 for (iy = -1043, i = ly; i>0; i <<= 1) iy -= 1;
83 }
84 else {
85 for (iy = -1022, i = (hy << 11); i>0; i <<= 1) iy -= 1;
86 }
87 }
88 else iy = (hy >> 20) - 1023;
89
90 /* set up {hx,lx}, {hy,ly} and align y to x */
91 if (ix >= -1022)
92 hx = 0x00100000 | (0x000fffff & hx);
93 else { /* subnormal x, shift x to normal */
94 n = -1022 - ix;
95 if (n <= 31) {
96 hx = (hx << n) | (lx >> (32 - n));
97 lx <<= n;
98 }
99 else {
100 hx = lx << (n - 32);
101 lx = 0;
102 }
103 }
104 if (iy >= -1022)
105 hy = 0x00100000 | (0x000fffff & hy);
106 else { /* subnormal y, shift y to normal */
107 n = -1022 - iy;
108 if (n <= 31) {
109 hy = (hy << n) | (ly >> (32 - n));
110 ly <<= n;
111 }
112 else {
113 hy = ly << (n - 32);
114 ly = 0;
115 }
116 }
117
118 /* fix point fmod */
119 n = ix - iy;
120 while (n--) {
121 hz = hx - hy; lz = lx - ly; if (lx<ly) hz -= 1;
122 if (hz<0){ hx = hx + hx + (lx >> 31); lx = lx + lx; }
123 else {
124 if ((hz | lz) == 0) /* return sign(x)*0 */
125 return Zero[(unsigned)sx >> 31];
126 hx = hz + hz + (lz >> 31); lx = lz + lz;
127 }
128 }
129 hz = hx - hy; lz = lx - ly; if (lx<ly) hz -= 1;
130 if (hz >= 0) { hx = hz; lx = lz; }
131
132 /* convert back to floating value and restore the sign */
133 if ((hx | lx) == 0) /* return sign(x)*0 */
134 return Zero[(unsigned)sx >> 31];
135 while (hx<0x00100000) { /* normalize x */
136 hx = hx + hx + (lx >> 31); lx = lx + lx;
137 iy -= 1;
138 }
139 if (iy >= -1022) { /* normalize output */
140 hx = ((hx - 0x00100000) | ((iy + 1023) << 20));
141 __HI(x) = hx | sx;
142 __LO(x) = lx;
143 }
144 else { /* subnormal output */
145 n = -1022 - iy;
146 if (n <= 20) {
147 lx = (lx >> n) | ((unsigned)hx << (32 - n));
148 hx >>= n;
149 }
150 else if (n <= 31) {
151 lx = (hx << (32 - n)) | (lx >> n); hx = sx;
152 }
153 else {
154 lx = hx >> (n - 32); hx = sx;
155 }
156 __HI(x) = hx | sx;
157 __LO(x) = lx;
158 x *= one; /* create necessary signal */
159 }
160 return x; /* exact output */
161 }
162
163 #endif
164