|
| 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 | +package compiler.intrinsics.float16; |
| 24 | + |
| 25 | +import compiler.lib.ir_framework.*; |
| 26 | +import jdk.incubator.vector.*; |
| 27 | +import java.util.Random; |
| 28 | +import jdk.test.lib.*; |
| 29 | + |
| 30 | +/** |
| 31 | + * @test |
| 32 | + * @bug 8352585 |
| 33 | + * @library /test/lib / |
| 34 | + * @summary Add special case handling for Float16.max/min x86 backend |
| 35 | + * @modules jdk.incubator.vector |
| 36 | + * @run driver compiler.intrinsics.float16.TestFloat16MaxMinSpecialValues |
| 37 | + */ |
| 38 | + |
| 39 | + |
| 40 | +public class TestFloat16MaxMinSpecialValues { |
| 41 | + public static Float16 POS_ZERO = Float16.valueOf(0.0f); |
| 42 | + public static Float16 NEG_ZERO = Float16.valueOf(-0.0f); |
| 43 | + public static Float16 SRC = Float16.valueOf(Float.MAX_VALUE); |
| 44 | + public static Random rd = Utils.getRandomInstance(); |
| 45 | + |
| 46 | + public static Float16 genNaN() { |
| 47 | + // IEEE 754 Half Precision QNaN Format |
| 48 | + // S EEEEE MMMMMMMMMM |
| 49 | + // X 11111 1XXXXXXXXX |
| 50 | + short sign = (short)(rd.nextBoolean() ? 1 << 15 : 0); |
| 51 | + short significand = (short)rd.nextInt(512); |
| 52 | + return Float16.shortBitsToFloat16((short)(sign | 0x7E00 | significand)); |
| 53 | + } |
| 54 | + |
| 55 | + public static boolean assertionCheck(Float16 actual, Float16 expected) { |
| 56 | + return !actual.equals(expected); |
| 57 | + } |
| 58 | + |
| 59 | + public Float16 RES; |
| 60 | + |
| 61 | + public static void main(String [] args) { |
| 62 | + TestFramework.runWithFlags("--add-modules=jdk.incubator.vector"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @IR(counts = {IRNode.MAX_HF, " >0 "}, applyIfCPUFeatureAnd = {"avx512_fp16", "true", "avx512bw", "true", "avx512vl", "true"}) |
| 67 | + public Float16 testMaxNaNOperands(Float16 src1, Float16 src2) { |
| 68 | + return Float16.max(src1, src2); |
| 69 | + } |
| 70 | + |
| 71 | + @Run(test = "testMaxNaNOperands") |
| 72 | + public void launchMaxNaNOperands() { |
| 73 | + Float16 NAN = null; |
| 74 | + for (int i = 0; i < 100; i++) { |
| 75 | + NAN = genNaN(); |
| 76 | + RES = testMaxNaNOperands(SRC, NAN); |
| 77 | + if (assertionCheck(RES, NAN)) { |
| 78 | + throw new AssertionError("input1 = " + SRC.floatValue() + " input2 = NaN , expected = NaN, actual = " + RES.floatValue()); |
| 79 | + } |
| 80 | + NAN = genNaN(); |
| 81 | + RES = testMaxNaNOperands(NAN, SRC); |
| 82 | + if (assertionCheck(RES, NAN)) { |
| 83 | + throw new AssertionError("input1 = NaN, input2 = " + SRC.floatValue() + ", expected = NaN, actual = " + RES.floatValue()); |
| 84 | + } |
| 85 | + NAN = genNaN(); |
| 86 | + RES = testMaxNaNOperands(NAN, NAN); |
| 87 | + if (assertionCheck(RES, NAN)) { |
| 88 | + throw new AssertionError("input1 = NaN, input2 = NaN, expected = NaN, actual = " + RES.floatValue()); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @IR(counts = {IRNode.MIN_HF, " >0 "}, applyIfCPUFeatureAnd = {"avx512_fp16", "true", "avx512bw", "true", "avx512vl", "true"}) |
| 95 | + public Float16 testMinNaNOperands(Float16 src1, Float16 src2) { |
| 96 | + return Float16.min(src1, src2); |
| 97 | + } |
| 98 | + |
| 99 | + @Run(test = "testMinNaNOperands") |
| 100 | + public void launchMinNaNOperands() { |
| 101 | + Float16 NAN = null; |
| 102 | + for (int i = 0; i < 100; i++) { |
| 103 | + NAN = genNaN(); |
| 104 | + RES = testMinNaNOperands(SRC, NAN); |
| 105 | + if (assertionCheck(RES, NAN)) { |
| 106 | + throw new AssertionError("input1 = " + SRC.floatValue() + " input2 = NaN, expected = NaN, actual = " + RES.floatValue()); |
| 107 | + } |
| 108 | + NAN = genNaN(); |
| 109 | + RES = testMinNaNOperands(NAN, SRC); |
| 110 | + if (assertionCheck(RES, NAN)) { |
| 111 | + throw new AssertionError("input1 = NaN, input2 = " + SRC.floatValue() + ", expected = NaN, actual = " + RES.floatValue()); |
| 112 | + } |
| 113 | + NAN = genNaN(); |
| 114 | + RES = testMinNaNOperands(NAN, NAN); |
| 115 | + if (assertionCheck(RES, NAN)) { |
| 116 | + throw new AssertionError("input1 = NaN, input2 = NaN, expected = NaN, actual = " + RES.floatValue()); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @IR(counts = {IRNode.MAX_HF, " >0 "}, applyIfCPUFeatureAnd = {"avx512_fp16", "true", "avx512bw", "true", "avx512vl", "true"}) |
| 123 | + public Float16 testMaxZeroOperands(Float16 src1, Float16 src2) { |
| 124 | + return Float16.max(src1, src2); |
| 125 | + } |
| 126 | + |
| 127 | + @Run(test = "testMaxZeroOperands") |
| 128 | + public void launchMaxZeroOperands() { |
| 129 | + RES = testMaxZeroOperands(POS_ZERO, NEG_ZERO); |
| 130 | + if (assertionCheck(RES, POS_ZERO)) { |
| 131 | + throw new AssertionError("input1 = +0.0, input2 = -0.0, expected = +0.0, actual = " + RES.floatValue()); |
| 132 | + } |
| 133 | + RES = testMaxZeroOperands(NEG_ZERO, POS_ZERO); |
| 134 | + if (assertionCheck(RES, POS_ZERO)) { |
| 135 | + throw new AssertionError("input1 = -0.0, input2 = +0.0, expected = +0.0, actual = " + RES.floatValue()); |
| 136 | + } |
| 137 | + RES = testMaxZeroOperands(POS_ZERO, POS_ZERO); |
| 138 | + if (assertionCheck(RES, POS_ZERO)) { |
| 139 | + throw new AssertionError("input1 = +0.0, input2 = +0.0, expected = +0.0, actual = " + RES.floatValue()); |
| 140 | + } |
| 141 | + RES = testMaxZeroOperands(NEG_ZERO, NEG_ZERO); |
| 142 | + if (assertionCheck(RES, NEG_ZERO)) { |
| 143 | + throw new AssertionError("input1 = -0.0, input2 = -0.0, expected = -0.0, actual = " + RES.floatValue()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + @IR(counts = {IRNode.MIN_HF, " >0 "}, applyIfCPUFeatureAnd = {"avx512_fp16", "true", "avx512bw", "true", "avx512vl", "true"}) |
| 149 | + public Float16 testMinZeroOperands(Float16 src1, Float16 src2) { |
| 150 | + return Float16.min(src1, src2); |
| 151 | + } |
| 152 | + |
| 153 | + @Run(test = "testMinZeroOperands") |
| 154 | + public void launchMinZeroOperands() { |
| 155 | + RES = testMinZeroOperands(POS_ZERO, NEG_ZERO); |
| 156 | + if (assertionCheck(RES, NEG_ZERO)) { |
| 157 | + throw new AssertionError("input1 = +0.0, input2 = -0.0, expected = -0.0, actual = " + RES.floatValue()); |
| 158 | + } |
| 159 | + |
| 160 | + RES = testMinZeroOperands(NEG_ZERO, POS_ZERO); |
| 161 | + if (assertionCheck(RES, NEG_ZERO)) { |
| 162 | + throw new AssertionError("input1 = -0.0, input2 = +0.0, expected = -0.0, actual = " + RES.floatValue()); |
| 163 | + } |
| 164 | + |
| 165 | + RES = testMinZeroOperands(POS_ZERO, POS_ZERO); |
| 166 | + if (assertionCheck(RES, POS_ZERO)) { |
| 167 | + throw new AssertionError("input1 = +0.0, input2 = +0.0, expected = +0.0, actual = " + RES.floatValue()); |
| 168 | + } |
| 169 | + |
| 170 | + RES = testMinZeroOperands(NEG_ZERO, NEG_ZERO); |
| 171 | + if (assertionCheck(RES, NEG_ZERO)) { |
| 172 | + throw new AssertionError("input1 = -0.0, input2 = -0.0, expected = -0.0, actual = " + RES.floatValue()); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments