|
1 | 1 | /*
|
2 | 2 | * Copyright (c) 2022, 2023, Arm Limited. All rights reserved.
|
| 3 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 5 | *
|
5 | 6 | * This code is free software; you can redistribute it and/or modify it
|
|
23 | 24 |
|
24 | 25 | /**
|
25 | 26 | * @test
|
| 27 | +* @bug 8297172 8331993 8349637 |
26 | 28 | * @key randomness
|
27 | 29 | * @summary Test vectorization of numberOfTrailingZeros/numberOfLeadingZeros for Long
|
28 | 30 | * @requires vm.compiler2.enabled
|
29 | 31 | * @requires (os.simpleArch == "x64" & vm.cpu.features ~= ".*avx2.*") |
|
30 | 32 | * (os.simpleArch == "aarch64" & vm.cpu.features ~= ".*sve.*") |
|
31 | 33 | * (os.simpleArch == "riscv64" & vm.cpu.features ~= ".*zvbb.*")
|
32 | 34 | * @library /test/lib /
|
| 35 | +* @modules jdk.incubator.vector |
33 | 36 | * @run driver compiler.vectorization.TestNumberOfContinuousZeros
|
34 | 37 | */
|
35 | 38 |
|
36 | 39 | package compiler.vectorization;
|
37 | 40 |
|
| 41 | +import jdk.incubator.vector.*; |
38 | 42 | import compiler.lib.ir_framework.*;
|
39 | 43 | import java.util.Random;
|
40 | 44 | import jdk.test.lib.Asserts;
|
| 45 | +import jdk.test.lib.Utils; |
41 | 46 |
|
42 | 47 | public class TestNumberOfContinuousZeros {
|
| 48 | + private static final int[] SPECIAL = { 0x01FFFFFF, 0x03FFFFFE, 0x07FFFFFC, 0x0FFFFFF8, 0x1FFFFFF0, 0x3FFFFFE0, 0xFFFFFFFF }; |
43 | 49 | private long[] inputLong;
|
44 | 50 | private int[] outputLong;
|
45 | 51 | private int[] inputInt;
|
46 | 52 | private int[] outputInt;
|
47 | 53 | private static final int LEN = 1024;
|
48 | 54 | private Random rng;
|
49 | 55 |
|
50 |
| - public static void main(String args[]) { |
51 |
| - TestFramework.run(); |
| 56 | + public static void main(String[] args) { |
| 57 | + TestFramework.runWithFlags("--add-modules=jdk.incubator.vector"); |
52 | 58 | }
|
53 | 59 |
|
54 | 60 | public TestNumberOfContinuousZeros() {
|
55 | 61 | inputLong = new long[LEN];
|
56 | 62 | outputLong = new int[LEN];
|
57 | 63 | inputInt = new int[LEN];
|
58 | 64 | outputInt = new int[LEN];
|
59 |
| - rng = new Random(42); |
| 65 | + rng = Utils.getRandomInstance(); |
60 | 66 | for (int i = 0; i < LEN; ++i) {
|
61 | 67 | inputLong[i] = rng.nextLong();
|
62 | 68 | inputInt[i] = rng.nextInt();
|
@@ -119,5 +125,80 @@ public void checkResultInt() {
|
119 | 125 | Asserts.assertEquals(outputInt[i], Integer.numberOfLeadingZeros(inputInt[i]));
|
120 | 126 | }
|
121 | 127 | }
|
| 128 | + |
| 129 | + @Setup |
| 130 | + static Object[] setupSpecialIntArray() { |
| 131 | + int[] res = new int[LEN]; |
| 132 | + |
| 133 | + for (int i = 0; i < LEN; i++) { |
| 134 | + res[i] = SPECIAL[i % SPECIAL.length]; |
| 135 | + } |
| 136 | + |
| 137 | + return new Object[] { res }; |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + @IR(counts = {IRNode.COUNT_LEADING_ZEROS_VI, "> 0"}) |
| 142 | + @Arguments(setup = "setupSpecialIntArray") |
| 143 | + public Object[] testSpecialIntLeadingZeros(int[] ints) { |
| 144 | + int[] res = new int[LEN]; |
| 145 | + |
| 146 | + for (int i = 0; i < LEN; ++i) { |
| 147 | + res[i] = Integer.numberOfLeadingZeros(ints[i]); |
| 148 | + } |
| 149 | + |
| 150 | + return new Object[] { ints, res }; |
| 151 | + } |
| 152 | + |
| 153 | + @Check(test = "testSpecialIntLeadingZeros") |
| 154 | + public void checkSpecialIntLeadingZeros(Object[] vals) { |
| 155 | + int[] in = (int[]) vals[0]; |
| 156 | + int[] out = (int[]) vals[1]; |
| 157 | + |
| 158 | + for (int i = 0; i < LEN; ++i) { |
| 159 | + int value = Integer.numberOfLeadingZeros(in[i]); |
| 160 | + |
| 161 | + if (out[i] != value) { |
| 162 | + throw new IllegalStateException("Expected lzcnt(" + in[i] + ") to be " + value + " but got " + out[i]); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private static final VectorSpecies<Integer> SPECIES = IntVector.SPECIES_PREFERRED; |
| 168 | + |
| 169 | + @Test |
| 170 | + @IR(counts = {IRNode.COUNT_LEADING_ZEROS_VI, "> 0"}) |
| 171 | + @Arguments(setup = "setupSpecialIntArray") |
| 172 | + public Object[] checkSpecialIntLeadingZerosVector(int[] ints) { |
| 173 | + int[] res = new int[LEN]; |
| 174 | + |
| 175 | + for (int i = 0; i < ints.length; i += SPECIES.length()) { |
| 176 | + IntVector av = IntVector.fromArray(SPECIES, ints, i); |
| 177 | + av.lanewise(VectorOperators.LEADING_ZEROS_COUNT).intoArray(res, i); |
| 178 | + } |
| 179 | + |
| 180 | + return new Object[] { ints, res }; |
| 181 | + } |
| 182 | + |
| 183 | + @Check(test = "checkSpecialIntLeadingZerosVector") |
| 184 | + public void checkSpecialIntLeadingZerosVector(Object[] vals) { |
| 185 | + int[] ints = (int[]) vals[0]; |
| 186 | + int[] res = (int[]) vals[1]; |
| 187 | + |
| 188 | + // Verification |
| 189 | + |
| 190 | + int[] check = new int[LEN]; |
| 191 | + |
| 192 | + for (int i = 0; i < ints.length; i += SPECIES.length()) { |
| 193 | + IntVector av = IntVector.fromArray(SPECIES, ints, i); |
| 194 | + av.lanewise(VectorOperators.LEADING_ZEROS_COUNT).intoArray(check, i); |
| 195 | + } |
| 196 | + |
| 197 | + for (int i = 0; i < LEN; i++) { |
| 198 | + if (res[i] != check[i]) { |
| 199 | + throw new IllegalStateException("Expected " + check[i] + " but got " + res[i]); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
122 | 203 | }
|
123 | 204 |
|
0 commit comments