1 /*
2 * Copyright (c) 2022, 2024, 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 8294588
27 * @summary Auto-vectorize Float.floatToFloat16, Float.float16ToFloat APIs
28 * @requires vm.compiler2.enabled
29 * @library /test/lib /
30 * @run driver compiler.vectorization.TestFloatConversionsVector nCOH_nAV
31 * @run driver compiler.vectorization.TestFloatConversionsVector nCOH_yAV
32 * @run driver compiler.vectorization.TestFloatConversionsVector yCOH_nAV
33 * @run driver compiler.vectorization.TestFloatConversionsVector yCOH_yAV
34 */
35
36 package compiler.vectorization;
37
38 import compiler.lib.ir_framework.*;
39 import jdk.test.lib.Asserts;
40
41 public class TestFloatConversionsVector {
42 private static final int ARRLEN = 1024;
43 private static final int ITERS = 11000;
44 private static float [] finp;
45 private static short [] sout;
46 private static short [] sinp;
47 private static float [] fout;
48
49 public static void main(String args[]) {
50 TestFramework framework = new TestFramework(TestFloatConversionsVector.class);
51 framework.addFlags("-XX:-TieredCompilation", "-XX:CompileThresholdScaling=0.3");
52 switch (args[0]) {
53 case "nCOH_nAV" -> { framework.addFlags("-XX:-UseCompactObjectHeaders", "-XX:-AlignVector"); }
54 case "nCOH_yAV" -> { framework.addFlags("-XX:-UseCompactObjectHeaders", "-XX:+AlignVector"); }
55 case "yCOH_nAV" -> { framework.addFlags("-XX:+UseCompactObjectHeaders", "-XX:-AlignVector"); }
56 case "yCOH_yAV" -> { framework.addFlags("-XX:+UseCompactObjectHeaders", "-XX:+AlignVector"); }
57 default -> { throw new RuntimeException("Test argument not recognized: " + args[0]); }
58 };
59 framework.start();
60 System.out.println("PASSED");
61 }
62
63 @Test
64 @IR(counts = {IRNode.VECTOR_CAST_F2HF, IRNode.VECTOR_SIZE + "min(max_float, max_short)", "> 0"},
65 applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"},
66 applyIfCPUFeatureOr = {"f16c", "true", "avx512f", "true", "zvfh", "true", "asimd", "true", "sve", "true"})
67 public void test_float_float16(short[] sout, float[] finp) {
68 for (int i = 0; i < finp.length; i++) {
69 sout[i] = Float.floatToFloat16(finp[i]);
70 }
71 }
72
73 @Test
74 public void test_float_float16_strided(short[] sout, float[] finp) {
75 for (int i = 0; i < finp.length/2; i++) {
76 sout[i*2] = Float.floatToFloat16(finp[i*2]);
77 }
78 }
79
80 @Test
81 public void test_float_float16_short_vector(short[] sout, float[] finp) {
82 for (int i = 0; i < finp.length; i+= 4) {
83 sout[i+0] = Float.floatToFloat16(finp[i+0]);
84 sout[i+1] = Float.floatToFloat16(finp[i+1]);
85 }
86 }
87
88 @Run(test = {"test_float_float16", "test_float_float16_strided",
89 "test_float_float16_short_vector"}, mode = RunMode.STANDALONE)
90 public void kernel_test_float_float16() {
91 finp = new float[ARRLEN];
92 sout = new short[ARRLEN];
93
94 for (int i = 0; i < ARRLEN; i++) {
95 finp[i] = (float) i * 1.4f;
96 }
97
98 for (int i = 0; i < ITERS; i++) {
99 test_float_float16(sout, finp);
100 }
101
102 // Verifying the result
103 for (int i = 0; i < ARRLEN; i++) {
104 Asserts.assertEquals(Float.floatToFloat16(finp[i]), sout[i]);
105 }
106
107 for (int i = 0; i < ITERS; i++) {
108 test_float_float16_strided(sout, finp);
109 }
110
111 // Verifying the result
112 for (int i = 0; i < ARRLEN/2; i++) {
113 Asserts.assertEquals(Float.floatToFloat16(finp[i*2]), sout[i*2]);
114 }
115
116 for (int i = 0; i < ITERS; i++) {
117 test_float_float16_short_vector(sout, finp);
118 }
119
120 // Verifying the result
121 for (int i = 0; i < ARRLEN; i++) {
122 Asserts.assertEquals(Float.floatToFloat16(finp[i]), sout[i]);
123 }
124 }
125
126 @Test
127 @IR(counts = {IRNode.VECTOR_CAST_HF2F, IRNode.VECTOR_SIZE + "min(max_float, max_short)", "> 0"},
128 applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"},
129 applyIfCPUFeatureOr = {"f16c", "true", "avx512f", "true", "zvfh", "true", "asimd", "true", "sve", "true"})
130 public void test_float16_float(float[] fout, short[] sinp) {
131 for (int i = 0; i < sinp.length; i++) {
132 fout[i] = Float.float16ToFloat(sinp[i]);
133 }
134 }
135
136 @Test
137 public void test_float16_float_strided(float[] fout, short[] sinp) {
138 for (int i = 0; i < sinp.length/2; i++) {
139 fout[i*2] = Float.float16ToFloat(sinp[i*2]);
140 }
141 }
142
143 @Run(test = {"test_float16_float", "test_float16_float_strided"}, mode = RunMode.STANDALONE)
144 public void kernel_test_float16_float() {
145 sinp = new short[ARRLEN];
146 fout = new float[ARRLEN];
147
148 for (int i = 0; i < ARRLEN; i++) {
149 sinp[i] = (short)i;
150 }
151
152 for (int i = 0; i < ITERS; i++) {
153 test_float16_float(fout, sinp);
154 }
155
156 // Verifying the result
157 for (int i = 0; i < ARRLEN; i++) {
158 Asserts.assertEquals(Float.float16ToFloat(sinp[i]), fout[i]);
159 }
160
161 for (int i = 0; i < ITERS; i++) {
162 test_float16_float_strided(fout, sinp);
163 }
164
165 // Verifying the result
166 for (int i = 0; i < ARRLEN/2; i++) {
167 Asserts.assertEquals(Float.float16ToFloat(sinp[i*2]), fout[i*2]);
168 }
169 }
170 }