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 /** 26 * @test 27 * @bug 8073670 28 * @summary Test that causes C2 to fold two NaNs with different values into a single NaN. 29 * @run main/othervm -XX:-TieredCompilation -Xcomp -XX:CompileCommand=compileonly,FloatingPointFoldingTest.test_double_inf -XX:CompileCommand=compileonly,FloatingPointFoldingTest.test_double_zero -XX:CompileCommand=compileonly,FloatingPointFoldingTest.test_double_nan -XX:CompileCommand=compileonly,FloatingPointFoldingTest.test_float_inf -XX:CompileCommand=compileonly,FloatingPointFoldingTest.test_float_zero -XX:CompileCommand=compileonly,FloatingPointFoldingTest.test_float_nan FloatingPointFoldingTest 30 */ 31 32 public class FloatingPointFoldingTest { 33 // Double values. 34 public static final long MINUS_INF_LONGBITS = 0xfff0000000000000L; 35 public static final double DOUBLE_MINUS_INF = Double.longBitsToDouble(MINUS_INF_LONGBITS); 36 37 public static final long PLUS_INF_LONGBITS = 0x7ff0000000000000L; 38 public static final double DOUBLE_PLUS_INF = Double.longBitsToDouble(PLUS_INF_LONGBITS); 39 40 public static final long MINUS_ZERO_LONGBITS = 0x8000000000000000L; 41 public static final double DOUBLE_MINUS_ZERO = Double.longBitsToDouble(MINUS_ZERO_LONGBITS); 42 43 // We need two different NaN values. A floating point number is 44 // considered to be NaN is the sign bit is 0, all exponent bits 45 // are set to 1, and at least one bit of the exponent is not zero. 46 // 47 // As java.lang.Double.NaN is 0x7ff8000000000000L, we use 48 // 0x7ffc000000000000L as a second NaN double value. 49 public static final long NAN_LONGBITS = 0x7ffc000000000000L; 50 public static final double DOUBLE_NAN = Double.longBitsToDouble(NAN_LONGBITS); 51 52 // Float values. 53 public static final int MINUS_INF_INTBITS = 0xff800000; 54 public static final float FLOAT_MINUS_INF = Float.intBitsToFloat(MINUS_INF_INTBITS); 55 56 public static final int PLUS_INF_INTBITS = 0x7f800000; 57 public static final float FLOAT_PLUS_INF = Float.intBitsToFloat(PLUS_INF_INTBITS); 58 59 public static final int MINUS_ZERO_INTBITS = 0x80000000; 60 public static final float FLOAT_MINUS_ZERO = Float.intBitsToFloat(MINUS_ZERO_INTBITS); 61 62 // As java.lang.Float.NaN is 0x7fc00000, we use 0x7fe00000 63 // as a second NaN float value. 64 public static final int NAN_INTBITS = 0x7fe00000; 65 public static final float FLOAT_NAN = Float.intBitsToFloat(NAN_INTBITS); 66 67 68 // Double tests. 69 static void test_double_inf(long[] result) { 70 double d1 = DOUBLE_MINUS_INF; 71 double d2 = DOUBLE_PLUS_INF; 72 result[0] = Double.doubleToRawLongBits(d1); 73 result[1] = Double.doubleToRawLongBits(d2); 74 } 75 76 static void test_double_zero(long[] result) { 77 double d1 = DOUBLE_MINUS_ZERO; 78 double d2 = 0; 79 result[0] = Double.doubleToRawLongBits(d1); 80 result[1] = Double.doubleToRawLongBits(d2); 81 } 82 83 static void test_double_nan(long[] result) { 84 double d1 = DOUBLE_NAN; 85 double d2 = Double.NaN; 86 result[0] = Double.doubleToRawLongBits(d1); 87 result[1] = Double.doubleToRawLongBits(d2); 88 } 89 90 // Float tests. 91 static void test_float_inf(int[] result) { 92 float f1 = FLOAT_MINUS_INF; 93 float f2 = FLOAT_PLUS_INF; 94 result[0] = Float.floatToRawIntBits(f1); 95 result[1] = Float.floatToRawIntBits(f2); 96 } 97 98 static void test_float_zero(int[] result) { 99 float f1 = FLOAT_MINUS_ZERO; 100 float f2 = 0; 101 result[0] = Float.floatToRawIntBits(f1); 102 result[1] = Float.floatToRawIntBits(f2); 103 } 104 105 static void test_float_nan(int[] result) { 106 float f1 = FLOAT_NAN; 107 float f2 = Float.NaN; 108 result[0] = Float.floatToRawIntBits(f1); 109 result[1] = Float.floatToRawIntBits(f2); 110 } 111 112 // Check doubles. 113 static void check_double(long[] result, double d1, double d2) { 114 if (result[0] == result[1]) { 115 throw new RuntimeException("ERROR: Two different double values are considered equal. \n" 116 + String.format("\toriginal values: 0x%x 0x%x\n", Double.doubleToRawLongBits(d1), Double.doubleToRawLongBits(d2)) 117 + String.format("\tvalues after execution of method test(): 0x%x 0x%x", result[0], result[1])); 118 } 119 } 120 121 // Check floats. 122 static void check_float(int[] result, float f1, float f2) { 123 if (result[0] == result[1]) { 124 throw new RuntimeException("ERROR: Two different float values are considered equal. \n" 125 + String.format("\toriginal values: 0x%x 0x%x\n", Float.floatToRawIntBits(f1), Float.floatToRawIntBits(f2)) 126 + String.format("\tvalues after execution of method test(): 0x%x 0x%x", result[0], result[1])); 127 } 128 } 129 130 public static void main(String[] args) { 131 // Float tests. 132 133 int[] iresult = new int[2]; 134 135 // -Inf and +Inf. 136 test_float_inf(iresult); 137 check_float(iresult, FLOAT_MINUS_INF, FLOAT_PLUS_INF); 138 139 // 0 and -0. 140 test_float_zero(iresult); 141 check_float(iresult, FLOAT_MINUS_ZERO, 0); 142 143 // Diferrent NaNs. 144 test_float_nan(iresult); 145 check_float(iresult, FLOAT_NAN, Float.NaN); 146 147 // Double tests. 148 149 long[] lresult = new long[2]; 150 151 // -Inf and +Inf. 152 test_double_inf(lresult); 153 check_double(lresult, DOUBLE_MINUS_INF, DOUBLE_PLUS_INF); 154 155 // 0 and -0. 156 test_double_zero(lresult); 157 check_double(lresult, DOUBLE_MINUS_ZERO, 0); 158 159 // Diferrent NaNs. 160 test_double_nan(lresult); 161 check_double(lresult, DOUBLE_NAN, Double.NaN); 162 } 163 }