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