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