1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8015396
* @summary double a%b returns NaN for some (a,b) (|a| < inf, |b|>0) (on Core i7 980X)
*
* @run main compiler.floatingpoint.ModNaN
*/
package compiler.floatingpoint;
public class ModNaN {
/* This bug was seen in the field for a particular version of the VM,
* but never reproduced internally, and the reason was never known,
* nor were the exact circumstances of the failure.
*/
/*
* Failed on Windows 7/Core i7 980X/1.6.0_38 (64-bit):
*
* NaNs (i.e., when it fails, this is what we expect to see):
*
* 8.98846567431158E307 % 1.295163E-318 = NaN
* (0x7FE0000000000000L % 0x0000000000040000L)
*
* 1.7976931348623157E308 % 2.59032E-318 = NaN
* (0x7FEFFFFFFFFFFFFFL % 0x000000000007FFFFL)
*
* 1.7976931348623157E308 % 1.060997895E-314 = NaN
* (0x7FEFFFFFFFFFFFFFL % 0x000000007FFFFFFFL)
*
* 1.7976931348623157E308 % 6.767486E-317 = NaN
* (0x7FEFFFFFFFFFFFFFL % 0x0000000000d10208L)
*
* 1.7976931348623157E308 % 7.528725E-318 = NaN
* (0x7FEFFFFFFFFFFFFFL % 0x0000000000174077L)
*
* These cases did not fail, even when the previous five did:
* 8.98846567431158E307 % 1.29516E-318 = 2.53E-321
* (0x7fe0000000000000L % 0x000000000003ffffL)
*
* 1.7976931348623157E308 % 2.590327E-318 = 0.0
* (0x7fefffffffffffffL % 0x0000000000080000L)
*
* 1.7976931348623157E308 % 1.060965516E-314 = 9.35818525E-315
* (0x7fefffffffffffffL % 0x000000007ffeffffL)
*
*/
static double[][] bad = new double[][] {
/*
* These hex numbers correspond to the base-10 doubles in the
* comment above; this can be checked by observing the output
* of testWithPrint.
*/
new double[] { Double.longBitsToDouble(0x7FE0000000000000L),
Double.longBitsToDouble(0x0000000000040000L) },
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
Double.longBitsToDouble(0x000000000007FFFFL) },
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
Double.longBitsToDouble(0x000000007FFFFFFFL) },
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
6.767486E-317 },
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
7.528725E-318 }, };
static double[][] good = new double[][] {
new double[] { Double.longBitsToDouble(0x7FE0000000000000L),
Double.longBitsToDouble(0x000000000003FFFFL) },
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
Double.longBitsToDouble(0x0000000000080000L) },
new double[] { Double.longBitsToDouble(0x7FEFFFFFFFFFFFFFL),
Double.longBitsToDouble(0x000000007FFEFFFFL) }, };
public static void main(String[] args) throws InterruptedException {
int N = 10000;
testWithPrint();
for (int i = 0; i < N; i++)
testStrict();
for (int i = 0; i < N; i++)
test();
Thread.sleep(1000); // pause to let the compiler work
for (int i = 0; i < 10; i++)
testStrict();
for (int i = 0; i < 10; i++)
test();
}
public strictfp static void testWithPrint() {
for (double[] ab : bad) {
double a = ab[0];
double b = ab[1];
double mod = a % b;
System.out.println("" + a + "("+toHexRep(a)+") mod " +
b + "("+toHexRep(b)+") yields " +
mod + "("+toHexRep(mod)+")");
}
for (double[] ab : good) {
double a = ab[0];
double b = ab[1];
double mod = a % b;
System.out.println("" + a + "("+toHexRep(a)+") mod " +
b + "("+toHexRep(b)+") yields " +
mod + "("+toHexRep(mod)+")");
}
}
public strictfp static void testStrict() {
for (double[] ab : bad) {
double a = ab[0];
double b = ab[1];
double mod = a % b;
check(mod);
}
for (double[] ab : good) {
double a = ab[0];
double b = ab[1];
double mod = a % b;
check(mod);
}
}
public static void test() {
for (double[] ab : bad) {
double a = ab[0];
double b = ab[1];
double mod = a % b;
check(mod);
}
for (double[] ab : good) {
double a = ab[0];
double b = ab[1];
double mod = a % b;
check(mod);
}
}
static String toHexRep(double d) {
return "0x" + Long.toHexString(Double.doubleToRawLongBits(d)) + "L";
}
static void check(double mod) {
if (Double.isNaN(mod)) {
throw new Error("Saw a NaN, fail");
}
}
}
|