|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 8351515 |
| 27 | + * @summary The compiler must not fold `0-(0-x)` for x: float | double |
| 28 | + * @library /test/lib / |
| 29 | + * @modules jdk.incubator.vector |
| 30 | + * @run driver compiler.floatingpoint.TestSubNodeFloatDoubleNegation |
| 31 | + */ |
| 32 | +package compiler.floatingpoint; |
| 33 | + |
| 34 | +import compiler.lib.ir_framework.*; |
| 35 | +import jdk.incubator.vector.Float16; |
| 36 | +import jdk.test.lib.Asserts; |
| 37 | + |
| 38 | +public class TestSubNodeFloatDoubleNegation { |
| 39 | + |
| 40 | + public static void main(String[] args) { |
| 41 | + TestFramework.runWithFlags("--add-modules=jdk.incubator.vector", "-XX:CompileCommand=inline,jdk.incubator.vector.Float16::*"); |
| 42 | + } |
| 43 | + |
| 44 | + @Run(test = { "testHalfFloat", "testFloat", "testDouble" }) |
| 45 | + public static void assertResults() { |
| 46 | + short halfFloatRes = Float16.float16ToShortBits(testHalfFloat(Float16.valueOf(-0.0f))); |
| 47 | + int floatRes = Float.floatToIntBits(testFloat(-0.0f)); |
| 48 | + long doubleRes = Double.doubleToLongBits(testDouble(-0.0)); |
| 49 | + |
| 50 | + Asserts.assertEQ((short) 0, halfFloatRes); |
| 51 | + Asserts.assertEQ((int) 0, floatRes); |
| 52 | + Asserts.assertEQ((long) 0, doubleRes); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + // Match a generic SubNode, because scalar Float16 operations are often |
| 57 | + // performed as float operations. |
| 58 | + @IR(counts = { IRNode.SUB, "2" }) |
| 59 | + // Checks that the subtractions in 0 - (0 - hf) do not get eliminiated |
| 60 | + public static Float16 testHalfFloat(Float16 hf) { |
| 61 | + return Float16.subtract( |
| 62 | + Float16.shortBitsToFloat16((short) 0), |
| 63 | + Float16.subtract(Float16.shortBitsToFloat16((short) 0), hf)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @IR(counts = { IRNode.SUB_F, "2" }) |
| 68 | + // Checks that the subtractions in 0 - (0 - f) do not get eliminated |
| 69 | + public static float testFloat(float f) { |
| 70 | + return 0 - (0 - f); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @IR(counts = { IRNode.SUB_D, "2" }) |
| 75 | + // Checks that the subtractions in 0 - (0 - d) do not get eliminated |
| 76 | + public static double testDouble(double d) { |
| 77 | + return 0 - (0 - d); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments