1 /*
   2  * Copyright (c) 2017, 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.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);
  48         }
  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 }