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