File: VectorRearrangeTest.java

package info (click to toggle)
openjdk-25 25~32ea-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,280 kB
  • sloc: java: 5,584,902; cpp: 1,333,941; xml: 1,321,242; ansic: 487,993; asm: 404,003; objc: 21,088; sh: 15,102; javascript: 13,265; python: 8,319; makefile: 2,515; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (311 lines) | stat: -rw-r--r-- 12,109 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 8350463
 * @summary AArch64: Add vector rearrange support for small lane count vectors
 * @modules jdk.incubator.vector
 * @library /test/lib /
 *
 * @run driver compiler.vectorapi.VectorRearrangeTest
 */

package compiler.vectorapi;

import compiler.lib.generators.*;
import compiler.lib.ir_framework.*;
import jdk.incubator.vector.*;
import jdk.test.lib.Asserts;

public class VectorRearrangeTest {
    private static final int LENGTH = 1024;
    private static final Generators random = Generators.G;

    private static final VectorSpecies<Byte> bspec128    = ByteVector.SPECIES_128;
    private static final VectorSpecies<Short> sspec128   = ShortVector.SPECIES_128;
    private static final VectorSpecies<Integer> ispec128 = IntVector.SPECIES_128;
    private static final VectorSpecies<Long> lspec128    = LongVector.SPECIES_128;
    private static final VectorSpecies<Float> fspec128   = FloatVector.SPECIES_128;
    private static final VectorSpecies<Double> dspec128  = DoubleVector.SPECIES_128;
    private static final VectorSpecies<Byte> bspec64     = ByteVector.SPECIES_64;
    private static final VectorSpecies<Short> sspec64    = ShortVector.SPECIES_64;
    private static final VectorSpecies<Integer> ispec64  = IntVector.SPECIES_64;
    private static final VectorSpecies<Float> fspec64    = FloatVector.SPECIES_64;

    private static byte[]   bsrc;
    private static short[]  ssrc;
    private static int[]    isrc;
    private static long[]   lsrc;
    private static float[]  fsrc;
    private static double[] dsrc;

    private static byte[]   bdst;
    private static short[]  sdst;
    private static int[]    idst;
    private static long[]   ldst;
    private static float[]  fdst;
    private static double[] ddst;

    private static int[][] indexes;

    static {
        bsrc = new byte[LENGTH];
        ssrc = new short[LENGTH];
        isrc = new int[LENGTH];
        lsrc = new long[LENGTH];
        fsrc = new float[LENGTH];
        dsrc = new double[LENGTH];
        bdst = new byte[LENGTH];
        sdst = new short[LENGTH];
        idst = new int[LENGTH];
        ldst = new long[LENGTH];
        fdst = new float[LENGTH];
        ddst = new double[LENGTH];

        Generator<Integer> byteGen = random.uniformInts(Byte.MIN_VALUE, Byte.MAX_VALUE);
        Generator<Integer> shortGen = random.uniformInts(Short.MIN_VALUE, Short.MAX_VALUE);
        for (int i = 0; i < LENGTH; i++) {
            bsrc[i] = byteGen.next().byteValue();
            ssrc[i] = shortGen.next().shortValue();
        }
        random.fill(random.ints(), isrc);
        random.fill(random.longs(), lsrc);
        random.fill(random.floats(), fsrc);
        random.fill(random.doubles(), dsrc);

        int[] nums = {2, 4, 8, 16};
        indexes = new int[4][];
        for (int i = 0; i < 4; i++) {
            indexes[i] = new int[nums[i]];
            random.fill(random.uniformInts(0, nums[i] - 1), indexes[i]);
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VB, IRNode.VECTOR_SIZE_8, " >0 "}, applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"})
    public void rearrange_byte64() {
        VectorShuffle<Byte> shuffle = VectorShuffle.fromArray(bspec64, indexes[2], 0);
        for (int i = 0; i < LENGTH; i += bspec64.length()) {
            ByteVector.fromArray(bspec64, bsrc, i)
                      .rearrange(shuffle)
                      .intoArray(bdst, i);
        }
    }

    @Check(test = "rearrange_byte64")
    public void rearrange_byte64_verify() {
        for (int i = 0; i < LENGTH; i += bspec64.length()) {
            for (int j = 0; j < bspec64.length(); j++) {
                Asserts.assertEquals(bsrc[indexes[2][j] + i], bdst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VB, IRNode.VECTOR_SIZE_16, " >0 "}, applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"})
    public void rearrange_byte128() {
        VectorShuffle<Byte> shuffle = VectorShuffle.fromArray(bspec128, indexes[3], 0);
        for (int i = 0; i < LENGTH; i += bspec128.length()) {
            ByteVector.fromArray(bspec128, bsrc, i)
                      .rearrange(shuffle)
                      .intoArray(bdst, i);
        }
    }

    @Check(test = "rearrange_byte128")
    public void rearrange_byte128_verify() {
        for (int i = 0; i < LENGTH; i += bspec128.length()) {
            for (int j = 0; j < bspec128.length(); j++) {
                Asserts.assertEquals(bsrc[indexes[3][j] + i], bdst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VS, IRNode.VECTOR_SIZE_4, " >0 "}, applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"})
    public void rearrange_short64() {
        VectorShuffle<Short> shuffle = VectorShuffle.fromArray(sspec64, indexes[1], 0);
        for (int i = 0; i < LENGTH; i += sspec64.length()) {
            ShortVector.fromArray(sspec64, ssrc, i)
                       .rearrange(shuffle)
                       .intoArray(sdst, i);
        }
    }

    @Check(test = "rearrange_short64")
    public void rearrange_short64_verify() {
        for (int i = 0; i < LENGTH; i += sspec64.length()) {
            for (int j = 0; j < sspec64.length(); j++) {
                Asserts.assertEquals(ssrc[indexes[1][j] + i], sdst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VS, IRNode.VECTOR_SIZE_8, " >0 "}, applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"})
    public void rearrange_short128() {
        VectorShuffle<Short> shuffle = VectorShuffle.fromArray(sspec128, indexes[2], 0);
        for (int i = 0; i < LENGTH; i += sspec128.length()) {
            ShortVector.fromArray(sspec128, ssrc, i)
                       .rearrange(shuffle)
                       .intoArray(sdst, i);
        }
    }

    @Check(test = "rearrange_short128")
    public void rearrange_short128_verify() {
        for (int i = 0; i < LENGTH; i += sspec128.length()) {
            for (int j = 0; j < sspec128.length(); j++) {
                Asserts.assertEquals(ssrc[indexes[2][j] + i], sdst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VI, IRNode.VECTOR_SIZE_2, " >0 "}, applyIfCPUFeature = {"asimd", "true"})
    public void rearrange_int64() {
        VectorShuffle<Integer> shuffle = VectorShuffle.fromArray(ispec64, indexes[0], 0);
        for (int i = 0; i < LENGTH; i += ispec64.length()) {
            IntVector.fromArray(ispec64, isrc, i)
                     .rearrange(shuffle)
                     .intoArray(idst, i);
        }
    }

    @Check(test = "rearrange_int64")
    public void rearrange_int64_verify() {
        for (int i = 0; i < LENGTH; i += ispec64.length()) {
            for (int j = 0; j < ispec64.length(); j++) {
                Asserts.assertEquals(isrc[indexes[0][j] + i], idst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VI, IRNode.VECTOR_SIZE_4, " >0 "}, applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"})
    public void rearrange_int128() {
        VectorShuffle<Integer> shuffle = VectorShuffle.fromArray(ispec128, indexes[1], 0);
        for (int i = 0; i < LENGTH; i += ispec128.length()) {
            IntVector.fromArray(ispec128, isrc, i)
                     .rearrange(shuffle)
                     .intoArray(idst, i);
        }
    }

    @Check(test = "rearrange_int128")
    public void rearrange_int128_verify() {
        for (int i = 0; i < LENGTH; i += ispec128.length()) {
            for (int j = 0; j < ispec128.length(); j++) {
                Asserts.assertEquals(isrc[indexes[1][j] + i], idst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VL, IRNode.VECTOR_SIZE_2, " >0 "}, applyIfCPUFeature = {"asimd", "true"})
    public void rearrange_long128() {
        VectorShuffle<Long> shuffle = VectorShuffle.fromArray(lspec128, indexes[0], 0);
        for (int i = 0; i < LENGTH; i += lspec128.length()) {
            LongVector.fromArray(lspec128, lsrc, i)
                      .rearrange(shuffle)
                      .intoArray(ldst, i);
        }
    }

    @Check(test = "rearrange_long128")
    public void rearrange_long128_verify() {
        for (int i = 0; i < LENGTH; i += lspec128.length()) {
            for (int j = 0; j < lspec128.length(); j++) {
                Asserts.assertEquals(lsrc[indexes[0][j] + i], ldst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VF, IRNode.VECTOR_SIZE_2, " >0 "}, applyIfCPUFeature = {"asimd", "true"})
    public void rearrange_float64() {
        VectorShuffle<Float> shuffle = VectorShuffle.fromArray(fspec64, indexes[0], 0);
        for (int i = 0; i < LENGTH; i += fspec64.length()) {
            FloatVector.fromArray(fspec64, fsrc, i)
                       .rearrange(shuffle)
                       .intoArray(fdst, i);
        }
    }

    @Check(test = "rearrange_float64")
    public void rearrange_float64_verify() {
        for (int i = 0; i < LENGTH; i += fspec64.length()) {
            for (int j = 0; j < fspec64.length(); j++) {
                Asserts.assertEquals(fsrc[indexes[0][j] + i], fdst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VF, IRNode.VECTOR_SIZE_4, " >0 "}, applyIfCPUFeatureOr = {"avx", "true", "asimd", "true"})
    public void rearrange_float128() {
        VectorShuffle<Float> shuffle = VectorShuffle.fromArray(fspec128, indexes[1], 0);
        for (int i = 0; i < LENGTH; i += fspec128.length()) {
            FloatVector.fromArray(fspec128, fsrc, i)
                       .rearrange(shuffle)
                       .intoArray(fdst, i);
        }
    }

    @Check(test = "rearrange_float128")
    public void rearrange_float128_verify() {
        for (int i = 0; i < LENGTH; i += fspec128.length()) {
            for (int j = 0; j < fspec128.length(); j++) {
                Asserts.assertEquals(fsrc[indexes[1][j] + i], fdst[i + j]);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.REARRANGE_VD, IRNode.VECTOR_SIZE_2, " >0 "}, applyIfCPUFeature = {"asimd", "true"})
    public void rearrange_double128() {
        VectorShuffle<Double> shuffle = VectorShuffle.fromArray(dspec128, indexes[0], 0);
        for (int i = 0; i < LENGTH; i += dspec128.length()) {
            DoubleVector.fromArray(dspec128, dsrc, i)
                        .rearrange(shuffle)
                        .intoArray(ddst, i);
        }
    }

    @Check(test = "rearrange_double128")
    public void rearrange_double128_verify() {
        for (int i = 0; i < LENGTH; i += dspec128.length()) {
            for (int j = 0; j < dspec128.length(); j++) {
                Asserts.assertEquals(dsrc[indexes[0][j] + i], ddst[i + j]);
            }
        }
    }

    public static void main(String[] args) {
        TestFramework testFramework = new TestFramework();
        testFramework.setDefaultWarmup(5000)
                     .addFlags("--add-modules=jdk.incubator.vector")
                     .start();
    }
}