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