1 /*
  2  * Copyright (c) 2021, 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 package compiler.vectorapi;
 25 
 26 import java.util.Random;
 27 
 28 import jdk.incubator.vector.VectorSpecies;
 29 import jdk.incubator.vector.ByteVector;
 30 import jdk.incubator.vector.DoubleVector;
 31 import jdk.incubator.vector.FloatVector;
 32 import jdk.incubator.vector.IntVector;
 33 import jdk.incubator.vector.LongVector;
 34 import jdk.incubator.vector.ShortVector;
 35 import jdk.incubator.vector.VectorMask;
 36 import jdk.test.lib.Utils;
 37 
 38 import org.testng.Assert;
 39 import org.testng.annotations.Test;
 40 
 41 /**
 42  * @test
 43  * @bug 8274569
 44  * @key randomness
 45  * @library /test/lib
 46  * @summary Tests X86 backend related incorrectness issues in legacy storemask patterns
 47  * @modules jdk.incubator.vector
 48  *
 49  * @run testng/othervm -XX:-TieredCompilation -XX:CompileThreshold=100 compiler.vectorapi.VectorMaskLoadStoreTest
 50  */
 51 
 52 
 53 public class VectorMaskLoadStoreTest{
 54 
 55     private static final int NUM_ITER = 5000;
 56     private static final Random rd = Utils.getRandomInstance();
 57 
 58     public static void testByte64(long val) {
 59         VectorSpecies<Byte> SPECIES = ByteVector.SPECIES_64;
 60         VectorMask<Byte> mask = VectorMask.fromLong(SPECIES, val);
 61         Assert.assertEquals(mask.toLong(), val & 0xFFL);
 62     }
 63 
 64     public static void testByte128(long val) {
 65         VectorSpecies<Byte> SPECIES = ByteVector.SPECIES_128;
 66         VectorMask<Byte> mask = VectorMask.fromLong(SPECIES, val);
 67         Assert.assertEquals(mask.toLong(), val & 0xFFFFL);
 68     }
 69 
 70     public static void testByte256(long val) {
 71         VectorSpecies<Byte> SPECIES = ByteVector.SPECIES_256;
 72         VectorMask<Byte> mask = VectorMask.fromLong(SPECIES, val);
 73         Assert.assertEquals(mask.toLong(), val & 0xFFFFFFFFL);
 74     }
 75 
 76     public static void testByte512(long val) {
 77         VectorSpecies<Byte> SPECIES = ByteVector.SPECIES_512;
 78         VectorMask<Byte> mask = VectorMask.fromLong(SPECIES, val);
 79         Assert.assertEquals(mask.toLong(), val & -1L);
 80     }
 81 
 82     public static void testShort64(long val) {
 83         VectorSpecies<Short> SPECIES = ShortVector.SPECIES_64;
 84         VectorMask<Short> mask = VectorMask.fromLong(SPECIES, val);
 85         Assert.assertEquals(mask.toLong(), val & 0xFL);
 86     }
 87 
 88     public static void testShort128(long val) {
 89         VectorSpecies<Short> SPECIES = ShortVector.SPECIES_128;
 90         VectorMask<Short> mask = VectorMask.fromLong(SPECIES, val);
 91         Assert.assertEquals(mask.toLong(), val & 0xFFL);
 92     }
 93 
 94     public static void testShort256(long val) {
 95         VectorSpecies<Short> SPECIES = ShortVector.SPECIES_256;
 96         VectorMask<Short> mask = VectorMask.fromLong(SPECIES, val);
 97         Assert.assertEquals(mask.toLong(), val & 0xFFFFL);
 98     }
 99 
100     public static void testShort512(long val) {
101         VectorSpecies<Short> SPECIES = ShortVector.SPECIES_512;
102         VectorMask<Short> mask = VectorMask.fromLong(SPECIES, val);
103         Assert.assertEquals(mask.toLong(), val & 0xFFFFFFFFL);
104     }
105 
106     public static void testInteger64(long val) {
107         VectorSpecies<Integer> SPECIES = IntVector.SPECIES_64;
108         VectorMask<Integer> mask = VectorMask.fromLong(SPECIES, val);
109         Assert.assertEquals(mask.toLong(), val & 0x3L);
110     }
111 
112     public static void testInteger128(long val) {
113         VectorSpecies<Integer> SPECIES = IntVector.SPECIES_128;
114         VectorMask<Integer> mask = VectorMask.fromLong(SPECIES, val);
115         Assert.assertEquals(mask.toLong(), val & 0xFL);
116     }
117 
118     public static void testInteger256(long val) {
119         VectorSpecies<Integer> SPECIES = IntVector.SPECIES_256;
120         VectorMask<Integer> mask = VectorMask.fromLong(SPECIES, val);
121         Assert.assertEquals(mask.toLong(), val & 0xFFL);
122     }
123 
124     public static void testInteger512(long val) {
125         VectorSpecies<Integer> SPECIES = IntVector.SPECIES_512;
126         VectorMask<Integer> mask = VectorMask.fromLong(SPECIES, val);
127         Assert.assertEquals(mask.toLong(), val & 0xFFFFL);
128     }
129 
130     public static void testLong64(long val) {
131         VectorSpecies<Long> SPECIES = LongVector.SPECIES_64;
132         VectorMask<Long> mask = VectorMask.fromLong(SPECIES, val);
133         Assert.assertEquals(mask.toLong(), val & 0x1L);
134     }
135 
136     public static void testLong128(long val) {
137         VectorSpecies<Long> SPECIES = LongVector.SPECIES_128;
138         VectorMask<Long> mask = VectorMask.fromLong(SPECIES, val);
139         Assert.assertEquals(mask.toLong(), val & 0x3L);
140     }
141 
142     public static void testLong256(long val) {
143         VectorSpecies<Long> SPECIES = LongVector.SPECIES_256;
144         VectorMask<Long> mask = VectorMask.fromLong(SPECIES, val);
145         Assert.assertEquals(mask.toLong(), val & 0xFL);
146     }
147 
148     public static void testLong512(long val) {
149         VectorSpecies<Long> SPECIES = LongVector.SPECIES_512;
150         VectorMask<Long> mask = VectorMask.fromLong(SPECIES, val);
151         Assert.assertEquals(mask.toLong(), val & 0xFFL);
152     }
153 
154     public static void testFloat64(long val) {
155         VectorSpecies<Float> SPECIES = FloatVector.SPECIES_64;
156         VectorMask<Float> mask = VectorMask.fromLong(SPECIES, val);
157         Assert.assertEquals(mask.toLong(), val & 0x3L);
158     }
159 
160     public static void testFloat128(long val) {
161         VectorSpecies<Float> SPECIES = FloatVector.SPECIES_128;
162         VectorMask<Float> mask = VectorMask.fromLong(SPECIES, val);
163         Assert.assertEquals(mask.toLong(), val & 0xFL);
164     }
165 
166     public static void testFloat256(long val) {
167         VectorSpecies<Float> SPECIES = FloatVector.SPECIES_256;
168         VectorMask<Float> mask = VectorMask.fromLong(SPECIES, val);
169         Assert.assertEquals(mask.toLong(), val & 0xFFL);
170     }
171 
172     public static void testFloat512(long val) {
173         VectorSpecies<Float> SPECIES = FloatVector.SPECIES_512;
174         VectorMask<Float> mask = VectorMask.fromLong(SPECIES, val);
175         Assert.assertEquals(mask.toLong(), val & 0xFFFFL);
176     }
177 
178     public static void testDouble64(long val) {
179         VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_64;
180         VectorMask<Double> mask = VectorMask.fromLong(SPECIES, val);
181         Assert.assertEquals(mask.toLong(), val & 0x1L);
182     }
183 
184     public static void testDouble128(long val) {
185         VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_128;
186         VectorMask<Double> mask = VectorMask.fromLong(SPECIES, val);
187         Assert.assertEquals(mask.toLong(), val & 0x3L);
188     }
189 
190     public static void testDouble256(long val) {
191         VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_256;
192         VectorMask<Double> mask = VectorMask.fromLong(SPECIES, val);
193         Assert.assertEquals(mask.toLong(), val & 0xFL);
194     }
195 
196     public static void testDouble512(long val) {
197         VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_512;
198         VectorMask<Double> mask = VectorMask.fromLong(SPECIES, val);
199         Assert.assertEquals(mask.toLong(), val & 0xFFL);
200     }
201 
202     @Test
203     public static void testMaskCast() {
204         long [] vals = {-1L, 0, rd.nextLong(), rd.nextLong()};
205         for(int i = 0; i < vals.length; i++) {
206             long val = vals[i];
207             for (int ctr = 0; ctr < NUM_ITER; ctr++) {
208                 testByte64(val);
209                 testByte128(val);
210                 testByte256(val);
211                 testByte512(val);
212                 testShort64(val);
213                 testShort128(val);
214                 testShort256(val);
215                 testShort512(val);
216                 testInteger64(val);
217                 testInteger128(val);
218                 testInteger256(val);
219                 testInteger512(val);
220                 testLong64(val);
221                 testLong128(val);
222                 testLong256(val);
223                 testLong512(val);
224                 testFloat64(val);
225                 testFloat128(val);
226                 testFloat256(val);
227                 testFloat512(val);
228                 testDouble64(val);
229                 testDouble128(val);
230                 testDouble256(val);
231                 testDouble512(val);
232             }
233         }
234     }
235 }