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.ByteVector;
  25 import jdk.incubator.vector.Vector;
  26 import jdk.incubator.vector.Vector.Species;
  27 
  28 public class VectorArrays {
  29     static boolean equals(byte[] a, byte[] b) {
  30         if (a == b)
  31             return true;
  32         if (a == null || b == null)
  33             return false;
  34 
  35         int length = a.length;
  36         if (b.length != length)
  37             return false;
  38 
  39         return mismatch(a, b) < 0;
  40     }
  41 
  42     static int compare(byte[] a, byte[] b) {
  43         if (a == b)
  44             return 0;
  45         if (a == null || b == null)
  46             return a == null ? -1 : 1;
  47 
  48         int i = mismatch(a, b);
  49         if (i >= 0) {
  50             return Byte.compare(a[i], b[i]);
  51         }
  52 
  53         return a.length - b.length;
  54 
  55     }
  56 
  57     static int mismatch(byte[] a, byte[] b) {
  58         Species<Byte> species = ByteVector.SPECIES_256;
  59         return mismatch(a, b, species);
  60     }
  61 
  62     static int mismatch(byte[] a, byte[] b, Species<Byte> species) {
  63         int length = Math.min(a.length, b.length);
  64         if (a == b)
  65             return -1;
  66 
  67         int i = 0;
  68         // @@@ Method on species to truncate the length?
  69         for (; i < (length & ~(species.length() - 1)); i += species.length()) {
  70             Vector<Byte> va = ByteVector.fromArray(species, a, i);
  71             Vector<Byte> vb = ByteVector.fromArray(species, b, i);
  72             Vector.Mask<Byte> m = va.notEqual(vb);
  73             // @@@ count number of leading zeros with explicit method
  74             if (m.anyTrue()) {
  75                 break; // mismatch found
  76             }
  77         }
  78 
  79         // @@@ Can use a shape of half the bit size
  80         //     or a mask
  81         for (; i < length; i++) {
  82             if (a[i] != b[i])
  83                 return i;
  84         }
  85 
  86         return (a.length != b.length) ? length : -1;
  87     }
  88 }