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.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);
  47         }
  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 }