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 }
|
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 }
|