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