24319
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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 |
* @test
|
|
26 |
* @bug 8015396
|
|
27 |
* @summary double a%b returns NaN for some (a,b) (|a| < inf, |b|>0) (on Core i7 980X)
|
|
28 |
* @run main ModNaN
|
|
29 |
*/
|
|
30 |
public class ModNaN {
|
|
31 |
/* This bug was seen in the field for a particular version of the VM,
|
|
32 |
* but never reproduced internally, and the reason was never known,
|
|
33 |
* nor were the exact circumstances of the failure.
|
|
34 |
*/
|
|
35 |
/*
|
|
36 |
* Failed on Windows 7/Core i7 980X/1.6.0_38 (64-bit):
|
|
37 |
*
|
|
38 |
* NaNs (i.e., when it fails, this is what we expect to see):
|
|
39 |
*
|
|
40 |
* 8.98846567431158E307 % 1.295163E-318 = NaN
|
|
41 |
* (0x7FE0000000000000L % 0x0000000000040000L)
|
|
42 |
*
|
|
43 |
* 1.7976931348623157E308 % 2.59032E-318 = NaN
|
|
44 |
* (0x7FEFFFFFFFFFFFFFL % 0x000000000007FFFFL)
|
|
45 |
*
|
|
46 |
* 1.7976931348623157E308 % 1.060997895E-314 = NaN
|
|
47 |
* (0x7FEFFFFFFFFFFFFFL % 0x000000007FFFFFFFL)
|
|
48 |
*
|
|
49 |
* 1.7976931348623157E308 % 6.767486E-317 = NaN
|
|
50 |
* (0x7FEFFFFFFFFFFFFFL % 0x0000000000d10208L)
|
|
51 |
*
|
|
52 |
* 1.7976931348623157E308 % 7.528725E-318 = NaN
|
|
53 |
* (0x7FEFFFFFFFFFFFFFL % 0x0000000000174077L)
|
|
54 |
*
|
|
55 |
* These cases did not fail, even when the previous five did:
|
|
56 |
* 8.98846567431158E307 % 1.29516E-318 = 2.53E-321
|
|
57 |
* (0x7fe0000000000000L % 0x000000000003ffffL)
|
|
58 |
*
|
|
59 |
* 1.7976931348623157E308 % 2.590327E-318 = 0.0
|
|
60 |
* (0x7fefffffffffffffL % 0x0000000000080000L)
|
|
61 |
*
|
|
62 |
* 1.7976931348623157E308 % 1.060965516E-314 = 9.35818525E-315
|
|
63 |
* (0x7fefffffffffffffL % 0x000000007ffeffffL)
|
|
64 |
*
|
|
65 |
*/
|
|
66 |
|
|
67 |
static double[][] bad = new double[][] {
|
|
68 |
/*
|
|
69 |
* These hex numbers correspond to the base-10 doubles in the
|
|
70 |
* comment above; this can be checked by observing the output
|
|
71 |
* of testWithPrint.
|
|
72 |
*/
|
|
73 |
new double[] { Double.longBitsToDouble(0x7FE0000000000000L),
|
|
74 |
Double.longBitsToDouble(0x0000000000040000L) },
|
|
75 |
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
|
|
76 |
Double.longBitsToDouble(0x000000000007FFFFL) },
|
|
77 |
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
|
|
78 |
Double.longBitsToDouble(0x000000007FFFFFFFL) },
|
|
79 |
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
|
|
80 |
6.767486E-317 },
|
|
81 |
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
|
|
82 |
7.528725E-318 }, };
|
|
83 |
|
|
84 |
static double[][] good = new double[][] {
|
|
85 |
new double[] { Double.longBitsToDouble(0x7FE0000000000000L),
|
|
86 |
Double.longBitsToDouble(0x000000000003FFFFL) },
|
|
87 |
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
|
|
88 |
Double.longBitsToDouble(0x0000000000080000L) },
|
|
89 |
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
|
|
90 |
Double.longBitsToDouble(0x000000007FFEFFFFL) }, };
|
|
91 |
|
|
92 |
public static void main(String[] args) throws InterruptedException {
|
|
93 |
int N = 10000;
|
|
94 |
testWithPrint();
|
|
95 |
for (int i = 0; i < N; i++)
|
|
96 |
testStrict();
|
|
97 |
for (int i = 0; i < N; i++)
|
|
98 |
test();
|
|
99 |
Thread.sleep(1000); // pause to let the compiler work
|
|
100 |
for (int i = 0; i < 10; i++)
|
|
101 |
testStrict();
|
|
102 |
for (int i = 0; i < 10; i++)
|
|
103 |
test();
|
|
104 |
}
|
|
105 |
|
|
106 |
public strictfp static void testWithPrint() {
|
|
107 |
for (double[] ab : bad) {
|
|
108 |
double a = ab[0];
|
|
109 |
double b = ab[1];
|
|
110 |
double mod = a % b;
|
|
111 |
System.out.println("" + a + "("+toHexRep(a)+") mod " +
|
|
112 |
b + "("+toHexRep(b)+") yields " +
|
|
113 |
mod + "("+toHexRep(mod)+")");
|
|
114 |
}
|
|
115 |
|
|
116 |
for (double[] ab : good) {
|
|
117 |
double a = ab[0];
|
|
118 |
double b = ab[1];
|
|
119 |
double mod = a % b;
|
|
120 |
System.out.println("" + a + "("+toHexRep(a)+") mod " +
|
|
121 |
b + "("+toHexRep(b)+") yields " +
|
|
122 |
mod + "("+toHexRep(mod)+")");
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
public strictfp static void testStrict() {
|
|
127 |
for (double[] ab : bad) {
|
|
128 |
double a = ab[0];
|
|
129 |
double b = ab[1];
|
|
130 |
double mod = a % b;
|
|
131 |
check(mod);
|
|
132 |
}
|
|
133 |
|
|
134 |
for (double[] ab : good) {
|
|
135 |
double a = ab[0];
|
|
136 |
double b = ab[1];
|
|
137 |
double mod = a % b;
|
|
138 |
check(mod);
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
public static void test() {
|
|
143 |
for (double[] ab : bad) {
|
|
144 |
double a = ab[0];
|
|
145 |
double b = ab[1];
|
|
146 |
double mod = a % b;
|
|
147 |
check(mod);
|
|
148 |
}
|
|
149 |
|
|
150 |
for (double[] ab : good) {
|
|
151 |
double a = ab[0];
|
|
152 |
double b = ab[1];
|
|
153 |
double mod = a % b;
|
|
154 |
check(mod);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
static String toHexRep(double d) {
|
|
159 |
return "0x" + Long.toHexString(Double.doubleToRawLongBits(d)) + "L";
|
|
160 |
}
|
|
161 |
|
|
162 |
static void check(double mod) {
|
|
163 |
if (Double.isNaN(mod)) {
|
|
164 |
throw new Error("Saw a NaN, fail");
|
|
165 |
}
|
|
166 |
}
|
|
167 |
}
|