Path: blob/master/test/hotspot/jtreg/compiler/c2/FloatingPointFoldingTest.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 807367026* @summary Test that causes C2 to fold two NaNs with different values into a single NaN.27*28* @run main/othervm -XX:-TieredCompilation -Xcomp29* -XX:CompileCommand=compileonly,compiler.c2.FloatingPointFoldingTest::test_double_inf30* -XX:CompileCommand=compileonly,compiler.c2.FloatingPointFoldingTest::test_double_zero31* -XX:CompileCommand=compileonly,compiler.c2.FloatingPointFoldingTest::test_double_nan32* -XX:CompileCommand=compileonly,compiler.c2.FloatingPointFoldingTest::test_float_inf33* -XX:CompileCommand=compileonly,compiler.c2.FloatingPointFoldingTest::test_float_zero34* -XX:CompileCommand=compileonly,compiler.c2.FloatingPointFoldingTest::test_float_nan35* compiler.c2.FloatingPointFoldingTest36*/3738package compiler.c2;3940public class FloatingPointFoldingTest {41// Double values.42public static final long MINUS_INF_LONGBITS = 0xfff0000000000000L;43public static final double DOUBLE_MINUS_INF = Double.longBitsToDouble(MINUS_INF_LONGBITS);4445public static final long PLUS_INF_LONGBITS = 0x7ff0000000000000L;46public static final double DOUBLE_PLUS_INF = Double.longBitsToDouble(PLUS_INF_LONGBITS);4748public static final long MINUS_ZERO_LONGBITS = 0x8000000000000000L;49public static final double DOUBLE_MINUS_ZERO = Double.longBitsToDouble(MINUS_ZERO_LONGBITS);5051// We need two different NaN values. A floating point number is52// considered to be NaN is the sign bit is 0, all exponent bits53// are set to 1, and at least one bit of the exponent is not zero.54//55// As java.lang.Double.NaN is 0x7ff8000000000000L, we use56// 0x7ffc000000000000L as a second NaN double value.57public static final long NAN_LONGBITS = 0x7ffc000000000000L;58public static final double DOUBLE_NAN = Double.longBitsToDouble(NAN_LONGBITS);5960// Float values.61public static final int MINUS_INF_INTBITS = 0xff800000;62public static final float FLOAT_MINUS_INF = Float.intBitsToFloat(MINUS_INF_INTBITS);6364public static final int PLUS_INF_INTBITS = 0x7f800000;65public static final float FLOAT_PLUS_INF = Float.intBitsToFloat(PLUS_INF_INTBITS);6667public static final int MINUS_ZERO_INTBITS = 0x80000000;68public static final float FLOAT_MINUS_ZERO = Float.intBitsToFloat(MINUS_ZERO_INTBITS);6970// As java.lang.Float.NaN is 0x7fc00000, we use 0x7fe0000071// as a second NaN float value.72public static final int NAN_INTBITS = 0x7fe00000;73public static final float FLOAT_NAN = Float.intBitsToFloat(NAN_INTBITS);747576// Double tests.77static void test_double_inf(long[] result) {78double d1 = DOUBLE_MINUS_INF;79double d2 = DOUBLE_PLUS_INF;80result[0] = Double.doubleToRawLongBits(d1);81result[1] = Double.doubleToRawLongBits(d2);82}8384static void test_double_zero(long[] result) {85double d1 = DOUBLE_MINUS_ZERO;86double d2 = 0;87result[0] = Double.doubleToRawLongBits(d1);88result[1] = Double.doubleToRawLongBits(d2);89}9091static void test_double_nan(long[] result) {92double d1 = DOUBLE_NAN;93double d2 = Double.NaN;94result[0] = Double.doubleToRawLongBits(d1);95result[1] = Double.doubleToRawLongBits(d2);96}9798// Float tests.99static void test_float_inf(int[] result) {100float f1 = FLOAT_MINUS_INF;101float f2 = FLOAT_PLUS_INF;102result[0] = Float.floatToRawIntBits(f1);103result[1] = Float.floatToRawIntBits(f2);104}105106static void test_float_zero(int[] result) {107float f1 = FLOAT_MINUS_ZERO;108float f2 = 0;109result[0] = Float.floatToRawIntBits(f1);110result[1] = Float.floatToRawIntBits(f2);111}112113static void test_float_nan(int[] result) {114float f1 = FLOAT_NAN;115float f2 = Float.NaN;116result[0] = Float.floatToRawIntBits(f1);117result[1] = Float.floatToRawIntBits(f2);118}119120// Check doubles.121static void check_double(long[] result, double d1, double d2) {122if (result[0] == result[1]) {123throw new RuntimeException("ERROR: Two different double values are considered equal. \n"124+ String.format("\toriginal values: 0x%x 0x%x\n", Double.doubleToRawLongBits(d1), Double.doubleToRawLongBits(d2))125+ String.format("\tvalues after execution of method test(): 0x%x 0x%x", result[0], result[1]));126}127}128129// Check floats.130static void check_float(int[] result, float f1, float f2) {131if (result[0] == result[1]) {132throw new RuntimeException("ERROR: Two different float values are considered equal. \n"133+ String.format("\toriginal values: 0x%x 0x%x\n", Float.floatToRawIntBits(f1), Float.floatToRawIntBits(f2))134+ String.format("\tvalues after execution of method test(): 0x%x 0x%x", result[0], result[1]));135}136}137138public static void main(String[] args) {139// Float tests.140141int[] iresult = new int[2];142143// -Inf and +Inf.144test_float_inf(iresult);145check_float(iresult, FLOAT_MINUS_INF, FLOAT_PLUS_INF);146147// 0 and -0.148test_float_zero(iresult);149check_float(iresult, FLOAT_MINUS_ZERO, 0);150151// Diferrent NaNs.152test_float_nan(iresult);153check_float(iresult, FLOAT_NAN, Float.NaN);154155// Double tests.156157long[] lresult = new long[2];158159// -Inf and +Inf.160test_double_inf(lresult);161check_double(lresult, DOUBLE_MINUS_INF, DOUBLE_PLUS_INF);162163// 0 and -0.164test_double_zero(lresult);165check_double(lresult, DOUBLE_MINUS_ZERO, 0);166167// Diferrent NaNs.168test_double_nan(lresult);169check_double(lresult, DOUBLE_NAN, Double.NaN);170}171}172173174