1 /*
2 * Copyright (c) 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 8320646
27 * @summary Auto-vectorize Float.floatToFloat16, Float.float16ToFloat APIs, with NaN
28 * @requires vm.compiler2.enabled
29 * @requires (os.arch == "riscv64" & vm.cpu.features ~= ".*zvfh.*")
30 * @library /test/lib /
31 * @run driver compiler.vectorization.TestFloatConversionsVectorNaN nCOH_nAV
32 * @run driver compiler.vectorization.TestFloatConversionsVectorNaN nCOH_yAV
33 * @run driver compiler.vectorization.TestFloatConversionsVectorNaN yCOH_nAV
34 * @run driver compiler.vectorization.TestFloatConversionsVectorNaN yCOH_yAV
35 */
36
37 package compiler.vectorization;
38
39 import java.util.HexFormat;
40
41 import compiler.lib.ir_framework.*;
42 import jdk.test.lib.Asserts;
43
44 public class TestFloatConversionsVectorNaN {
45 private static final int ARRLEN = 1024;
46 private static final int ITERS = 11000;
47 private static float [] finp;
48 private static short [] sout;
49 private static short [] sinp;
50 private static float [] fout;
51
52 public static void main(String args[]) {
53 TestFramework framework = new TestFramework(TestFloatConversionsVectorNaN.class);
54 framework.addFlags("-XX:-TieredCompilation", "-XX:CompileThresholdScaling=0.3");
55 switch (args[0]) {
56 case "nCOH_nAV" -> { framework.addFlags("-XX:-UseCompactObjectHeaders", "-XX:-AlignVector"); }
57 case "nCOH_yAV" -> { framework.addFlags("-XX:-UseCompactObjectHeaders", "-XX:+AlignVector"); }
58 case "yCOH_nAV" -> { framework.addFlags("-XX:+UseCompactObjectHeaders", "-XX:-AlignVector"); }
59 case "yCOH_yAV" -> { framework.addFlags("-XX:+UseCompactObjectHeaders", "-XX:+AlignVector"); }
60 default -> { throw new RuntimeException("Test argument not recognized: " + args[0]); }
61 };
62 framework.start();
63 System.out.println("PASSED");
64 }
65
66 @Test
67 @IR(counts = {IRNode.VECTOR_CAST_F2HF, IRNode.VECTOR_SIZE + "min(max_float, max_short)", "> 0"},
68 public void test_float_float16(short[] sout, float[] finp) {
69 for (int i = 0; i < finp.length; i++) {
70 sout[i] = Float.floatToFloat16(finp[i]);
71 }
72 }
73
74 @Run(test = {"test_float_float16"}, mode = RunMode.STANDALONE)
75 public void kernel_test_float_float16() {
76 int errno = 0;
77 finp = new float[ARRLEN];
78 sout = new short[ARRLEN];
79
80 // Setup
81 for (int i = 0; i < ARRLEN; i++) {
82 if (i%39 == 0) {
83 int x = 0x7f800000 + ((i/39) << 13);
84 x = (i%2 == 0) ? x : (x | 0x80000000);
85 finp[i] = Float.intBitsToFloat(x);
86 } else {
87 finp[i] = (float) i * 1.4f;
88 }
89 }
90 int ranges[][] = {
91 {128, 64},
92 {256, 19},
93 {384-19, 19},
94 {512-19, 17},
95 {640+19, 19},
96 {768+19, 32},
97 {896-19, 32}
98 };
99 for (int range[] : ranges) {
100 int start = range[0];
101 int offset = range[1];
102 for (int i = start; i < start+offset; i++) {
103 int x = 0x7f800000 + (i << 13);
104 finp[i] = Float.intBitsToFloat(x);
105 }
106 }
107
108 // Test
109 for (int i = 0; i < ITERS; i++) {
110 test_float_float16(sout, finp);
111 }
112
113 // Verifying the result
114 for (int i = 0; i < ARRLEN; i++) {
115 errno += assertEquals(i, finp[i], Float.floatToFloat16(finp[i]), sout[i]);
116 }
117
118 if (errno > 0) {
119 throw new RuntimeException("errors occur");
120 }
121 }
122
123 static int assertEquals(int idx, float f, short expected, short actual) {
124 HexFormat hf = HexFormat.of();
125 String msg = "floatToFloat16 wrong result: idx: " + idx + ", \t" + f +
126 ",\t expected: " + hf.toHexDigits(expected) +
127 ",\t actual: " + hf.toHexDigits(actual);
128 if ((expected & 0x7c00) != 0x7c00) {
129 if (expected != actual) {
130 System.err.println(msg);
131 return 1;
132 }
133 } else if ((expected & 0x3ff) != 0) {
134 if (((actual & 0x7c00) != 0x7c00) || (actual & 0x3ff) == 0) {
135 System.err.println(msg);
136 return 1;
137 }
138 }
139 return 0;
140 }
141
142 @Test
143 @IR(counts = {IRNode.VECTOR_CAST_HF2F, IRNode.VECTOR_SIZE + "min(max_float, max_short)", "> 0"})
144 public void test_float16_float(float[] fout, short[] sinp) {
145 for (int i = 0; i < sinp.length; i++) {
146 fout[i] = Float.float16ToFloat(sinp[i]);
147 }
148 }
149
150 @Run(test = {"test_float16_float"}, mode = RunMode.STANDALONE)
151 public void kernel_test_float16_float() {
152 int errno = 0;
153 sinp = new short[ARRLEN];
154 fout = new float[ARRLEN];
155
156 // Setup
157 for (int i = 0; i < ARRLEN; i++) {
158 if (i%39 == 0) {
159 int x = 0x7c00 + i;
160 x = (i%2 == 0) ? x : (x | 0x8000);
161 sinp[i] = (short)x;
162 } else {
163 sinp[i] = (short)i;
164 }
165 }
166
167 int ranges[][] = {
168 {128, 64},
169 {256, 19},
170 {384-19, 19},
171 {512-19, 17},
172 {640+19, 19},
173 {768+19, 32},
174 {896-19, 32}
175 };
176 for (int range[] : ranges) {
177 int start = range[0];
178 int offset = range[1];
179 for (int i = start; i < start+offset; i++) {
180 int x = 0x7c00 + i;
181 x = (i%2 == 0) ? x : (x | 0x8000);
182 sinp[i] = (short)x;
183 }
184 }
185
186 // Test
187 for (int i = 0; i < ITERS; i++) {
188 test_float16_float(fout, sinp);
189 }
190
191 // Verifying the result
192 for (int i = 0; i < ARRLEN; i++) {
193 errno += assertEquals(i, sinp[i], Float.float16ToFloat(sinp[i]), fout[i]);
194 }
195
196 if (errno > 0) {
197 throw new RuntimeException("errors occur");
198 }
199 }
200
201 static int assertEquals(int idx, short s, float expected, float actual) {
202 String msg = "float16ToFloat wrong result: idx: " + idx + ", \t" + s +
203 ",\t expected: " + expected + ",\t" + Integer.toHexString(Float.floatToIntBits(expected)) +
204 ",\t actual: " + actual + ",\t" + Integer.toHexString(Float.floatToIntBits(actual));
205 if (!Float.isNaN(expected) || !Float.isNaN(actual)) {
206 if (expected != actual) {
207 System.err.println(msg);
208 return 1;
209 }
210 }
211 return 0;
212 }
213 }