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 import jdk.incubator.vector.IntVector;
25 import jdk.incubator.vector.Vector.Shape;
26 import jdk.incubator.vector.Vector.Species;
27 import jdk.incubator.vector.Vector;
28
29 import java.util.stream.IntStream;
30
31 /**
32 * @test
33 * @modules jdk.incubator.vector
34 */
35
36 public class VectorRuns {
37
38 public static void main(String[] args) {
39
40 for (int i = 1; i < 1024; i++) {
41 int[] a = IntStream.range(0, 1024).toArray();
42 a[i] = -1;
43 countRunAscending(a);
44
45 assertEquals(countRunAscending(a), i);
46 assertEquals(countRunAscendingVector(a), i);
48 }
49
50 static void assertEquals(Object a, Object b) {
51 if (!a.equals(b)) throw new AssertionError(a + " " + b);
52 }
53
54 // Count run of a[0] > a[1] > a[2] > ...
55 static int countRunAscending(int[] a) {
56 int r = 1;
57 if (r >= a.length)
58 return a.length;
59
60 while (r < a.length && a[r - 1] <= a[r]) {
61 r++;
62 }
63 return r;
64 }
65
66
67 static int countRunAscendingVector(int[] a) {
68 Species<Integer> species = IntVector.SPECIES_256;
69
70 int r = 1;
71 if (r >= a.length)
72 return a.length;
73
74 int length = a.length & (species.length() - 1);
75 if (length == a.length) length -= species.length();
76 while (r < length) {
77 IntVector vl = IntVector.fromArray(species, a, r - 1);
78 IntVector vr = IntVector.fromArray(species, a, r);
79 Vector.Mask<Integer> m = vl.greaterThan(vr);
80 if (m.anyTrue())
81 return r + Long.numberOfTrailingZeros(m.toLong());
82 r += species.length();
83 }
84
85 while (r < a.length && a[r - 1] <= a[r]) {
86 r++;
87 }
88 return r;
89 }
90 }
|
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 import jdk.incubator.vector.IntVector;
25 import jdk.incubator.vector.VectorMask;
26 import jdk.incubator.vector.VectorShape;
27 import jdk.incubator.vector.VectorSpecies;
28 import jdk.incubator.vector.Vector;
29
30 import java.util.stream.IntStream;
31
32 /**
33 * @test
34 * @modules jdk.incubator.vector
35 */
36
37 public class VectorRuns {
38
39 public static void main(String[] args) {
40
41 for (int i = 1; i < 1024; i++) {
42 int[] a = IntStream.range(0, 1024).toArray();
43 a[i] = -1;
44 countRunAscending(a);
45
46 assertEquals(countRunAscending(a), i);
47 assertEquals(countRunAscendingVector(a), i);
49 }
50
51 static void assertEquals(Object a, Object b) {
52 if (!a.equals(b)) throw new AssertionError(a + " " + b);
53 }
54
55 // Count run of a[0] > a[1] > a[2] > ...
56 static int countRunAscending(int[] a) {
57 int r = 1;
58 if (r >= a.length)
59 return a.length;
60
61 while (r < a.length && a[r - 1] <= a[r]) {
62 r++;
63 }
64 return r;
65 }
66
67
68 static int countRunAscendingVector(int[] a) {
69 VectorSpecies<Integer> species = IntVector.SPECIES_256;
70
71 int r = 1;
72 if (r >= a.length)
73 return a.length;
74
75 int length = a.length & (species.length() - 1);
76 if (length == a.length) length -= species.length();
77 while (r < length) {
78 IntVector vl = IntVector.fromArray(species, a, r - 1);
79 IntVector vr = IntVector.fromArray(species, a, r);
80 VectorMask<Integer> m = vl.greaterThan(vr);
81 if (m.anyTrue())
82 return r + Long.numberOfTrailingZeros(m.toLong());
83 r += species.length();
84 }
85
86 while (r < a.length && a[r - 1] <= a[r]) {
87 r++;
88 }
89 return r;
90 }
91 }
|