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:+UnlockExperimentalVMOptions", "-XX:-UseCompactObjectHeaders", "-XX:-AlignVector"); }
 57             case "nCOH_yAV" -> { framework.addFlags("-XX:+UnlockExperimentalVMOptions", "-XX:-UseCompactObjectHeaders", "-XX:+AlignVector"); }
 58             case "yCOH_nAV" -> { framework.addFlags("-XX:+UnlockExperimentalVMOptions", "-XX:+UseCompactObjectHeaders", "-XX:-AlignVector"); }
 59             case "yCOH_yAV" -> { framework.addFlags("-XX:+UnlockExperimentalVMOptions", "-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         applyIfOr = {"UseCompactObjectHeaders", "false", "AlignVector", "false"})
 69     public void test_float_float16(short[] sout, float[] finp) {
 70         for (int i = 0; i < finp.length; i++) {
 71             sout[i] = Float.floatToFloat16(finp[i]);
 72             // With AlignVector, we need 8-byte alignment of vector loads/stores.
 73             // UseCompactObjectHeaders=false                 UseCompactObjectHeaders=true
 74             // F_adr = base + 16 + 4*i   ->  i % 2 = 0       F_adr = base + 12 + 4*i   ->  i % 2 = 1
 75             // S_adr = base + 16 + 2*i   ->  i % 4 = 0       S_adr = base + 12 + 2*i   ->  i % 4 = 2
 76             // -> vectorize                                  -> no vectorization
 77         }
 78     }
 79 
 80     @Run(test = {"test_float_float16"}, mode = RunMode.STANDALONE)
 81     public void kernel_test_float_float16() {
 82         int errno = 0;
 83         finp = new float[ARRLEN];
 84         sout = new short[ARRLEN];
 85 
 86         // Setup
 87         for (int i = 0; i < ARRLEN; i++) {
 88             if (i%39 == 0) {
 89                 int x = 0x7f800000 + ((i/39) << 13);
 90                 x = (i%2 == 0) ? x : (x | 0x80000000);
 91                 finp[i] = Float.intBitsToFloat(x);
 92             } else {
 93                 finp[i] = (float) i * 1.4f;
 94             }
 95         }
 96         int ranges[][] = {
 97             {128, 64},
 98             {256, 19},
 99             {384-19, 19},
100             {512-19, 17},
101             {640+19, 19},
102             {768+19, 32},
103             {896-19, 32}
104         };
105         for (int range[] : ranges) {
106             int start = range[0];
107             int offset = range[1];
108             for (int i = start; i < start+offset; i++) {
109                 int x = 0x7f800000 + (i << 13);
110                 finp[i] = Float.intBitsToFloat(x);
111             }
112         }
113 
114         // Test
115         for (int i = 0; i < ITERS; i++) {
116             test_float_float16(sout, finp);
117         }
118 
119         // Verifying the result
120         for (int i = 0; i < ARRLEN; i++) {
121             errno += assertEquals(i, finp[i], Float.floatToFloat16(finp[i]), sout[i]);
122         }
123 
124         if (errno > 0) {
125             throw new RuntimeException("errors occur");
126         }
127     }
128 
129     static int assertEquals(int idx, float f, short expected, short actual) {
130         HexFormat hf = HexFormat.of();
131         String msg = "floatToFloat16 wrong result: idx: " + idx + ", \t" + 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         applyIfOr = {"UseCompactObjectHeaders", "false", "AlignVector", "false"})
151     public void test_float16_float(float[] fout, short[] sinp) {
152         for (int i = 0; i < sinp.length; i++) {
153             fout[i] = Float.float16ToFloat(sinp[i]);
154             // With AlignVector, we need 8-byte alignment of vector loads/stores.
155             // UseCompactObjectHeaders=false                 UseCompactObjectHeaders=true
156             // F_adr = base + 16 + 4*i   ->  i % 2 = 0       F_adr = base + 12 + 4*i   ->  i % 2 = 1
157             // S_adr = base + 16 + 2*i   ->  i % 4 = 0       S_adr = base + 12 + 2*i   ->  i % 4 = 2
158             // -> vectorize                                  -> no vectorization
159         }
160     }
161 
162     @Run(test = {"test_float16_float"}, mode = RunMode.STANDALONE)
163     public void kernel_test_float16_float() {
164         int errno = 0;
165         sinp = new short[ARRLEN];
166         fout = new float[ARRLEN];
167 
168         // Setup
169         for (int i = 0; i < ARRLEN; i++) {
170             if (i%39 == 0) {
171                 int x = 0x7c00 + i;
172                 x = (i%2 == 0) ? x : (x | 0x8000);
173                 sinp[i] = (short)x;
174             } else {
175                 sinp[i] = (short)i;
176             }
177         }
178 
179         int ranges[][] = {
180             {128, 64},
181             {256, 19},
182             {384-19, 19},
183             {512-19, 17},
184             {640+19, 19},
185             {768+19, 32},
186             {896-19, 32}
187         };
188         for (int range[] : ranges) {
189             int start = range[0];
190             int offset = range[1];
191             for (int i = start; i < start+offset; i++) {
192                 int x = 0x7c00 + i;
193                 x = (i%2 == 0) ? x : (x | 0x8000);
194                 sinp[i] = (short)x;
195             }
196         }
197 
198         // Test
199         for (int i = 0; i < ITERS; i++) {
200             test_float16_float(fout, sinp);
201         }
202 
203         // Verifying the result
204         for (int i = 0; i < ARRLEN; i++) {
205             errno += assertEquals(i, sinp[i], Float.float16ToFloat(sinp[i]), fout[i]);
206         }
207 
208         if (errno > 0) {
209             throw new RuntimeException("errors occur");
210         }
211     }
212 
213     static int assertEquals(int idx, short s, float expected, float actual) {
214         String msg = "float16ToFloat wrong result: idx: " + idx + ", \t" + s +
215                      ",\t expected: " + expected + ",\t" + Integer.toHexString(Float.floatToIntBits(expected)) +
216                      ",\t actual: " + actual + ",\t" + Integer.toHexString(Float.floatToIntBits(actual));
217         if (!Float.isNaN(expected) || !Float.isNaN(actual)) {
218             if (expected != actual) {
219                 System.err.println(msg);
220                 return 1;
221             }
222         }
223         return 0;
224     }
225 }