The proposed patch is an improvement for Byte/Short/Int/LongBuffer classes to add compareToUnsigned() that works like compareTo() but treats the values as unsigned to leverage the vectorized mismatch implementations, which are not publicly accessible.

In addition to that, it adds mismatch() for all *Buffer classes.

Both new methods basically expose all the goodness of jdk.internal.util.ArraysSupport to *Buffer.

The patch includes adoptions to the existing unit tests as well.

Robert

--
Robert Stupp
@snazy

# HG changeset patch
# User Robert Stupp <sn...@snazy.de>
# Date 1524399060 -7200
#      Sun Apr 22 14:11:00 2018 +0200
# Branch buffer-compUns-mismatch
# Node ID 06f0013900eba02b3136aa3bd72b3f981e3339df
# Parent  fcd5df7aa235ca39852b04eb589e25f156870ce4
Add unsigned comparison to *Buffer

diff --git a/src/java.base/share/classes/java/nio/X-Buffer.java.template 
b/src/java.base/share/classes/java/nio/X-Buffer.java.template
--- a/src/java.base/share/classes/java/nio/X-Buffer.java.template
+++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template
@@ -1342,6 +1342,35 @@
         return this.remaining() - that.remaining();
     }
 
+#if[!char]
+#if[integralType]
+    /**
+     * Compares this buffer to another treating the values as unsigned.
+     *
+     * <p> Two $type$ buffers are compared by comparing their sequences of
+     * remaining elements lexicographically, without regard to the starting
+     * position of each sequence within its corresponding buffer.
+     * Pairs of {@code $type$} elements are compared as if by invoking
+     * {@link $Fulltype$#compareUnsigned($type$,$type$)}.
+     *
+     * <p> A $type$ buffer is not comparable to any other type of object.
+     *
+     * @param   that the $type$ buffer to be compared.
+     * @return  A negative integer, zero, or a positive integer as this buffer
+     *          is less than, equal to, or greater than the given buffer
+     */
+    public int compareToUnsigned($Type$Buffer that) {
+        int i = BufferMismatch.mismatch(this, this.position(),
+                                        that, that.position(),
+                                        Math.min(this.remaining(), 
that.remaining()));
+        if (i >= 0) {
+            return compareUnsigned(this.get(this.position() + i), 
that.get(that.position() + i));
+        }
+        return this.remaining() - that.remaining();
+    }
+#end[integralType]
+#end[!char]
+
     private static int compare($type$ x, $type$ y) {
 #if[floatingPointType]
         return ((x < y)  ? -1 :
@@ -1353,6 +1382,34 @@
 #end[floatingPointType]
     }
 
+#if[!char]
+#if[integralType]
+    private static int compareUnsigned($type$ x, $type$ y) {
+        return $Fulltype$.compareUnsigned(x, y);
+    }
+#end[integralType]
+#end[!char]
+
+    /**
+     * Find the relative index of a mismatch between this buffer and another
+     * starting the current positions.
+     *
+     * <p> A $type$ buffer is not comparable to any other type of object.
+     *
+     * @param  that the $type$ buffer to be compared.
+     * @return the relative index of a mismatch between the two buffers,
+     *         otherwise -1 if no mismatch.  The index will be within the 
range of
+     *         (inclusive) {@link #position()} to (exclusive) the smaller of
+     *         {@link #remaining() this.position()+this.remaining()} and
+     *         {@link #remaining() this.position()+that.remaining()}.
+     */
+    public int mismatch($Type$Buffer that) {
+        int r = BufferMismatch.mismatch(this, this.position(),
+                                        that, that.position(),
+                                        Math.min(this.remaining(), 
that.remaining()));
+        return r == -1 ? -1 : r + this.position();
+    }
+
     // -- Other char stuff --
 
 #if[char]
diff --git a/test/jdk/java/nio/Buffer/Basic-X.java.template 
b/test/jdk/java/nio/Buffer/Basic-X.java.template
--- a/test/jdk/java/nio/Buffer/Basic-X.java.template
+++ b/test/jdk/java/nio/Buffer/Basic-X.java.template
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+#if[compareToUnsigned]
+        if (b.compareToUnsigned(b2) != 0) {
+            fail("Unsighed comparison to identical buffer != 0", b, b2);
+        }
+#end[compareToUnsigned]
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put(($type$)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+#if[compareToUnsigned]
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser shorter buffer <= 0", b, b2);
+        }
+#end[compareToUnsigned]
         b.limit(b.limit() - 1);
 
         b.put(2, ($type$)42);
@@ -713,16 +729,29 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
+#if[compareToUnsigned]
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser value <= 0", b, b2);
+        }
+#end[compareToUnsigned]
 
-        // Check equals and compareTo with interesting values
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for ($type$ x : VALUES) {
             $Type$Buffer xb = $Type$Buffer.wrap(new $type$[] { x });
             if (xb.compareTo(xb) != 0) {
                 fail("compareTo not reflexive", xb, xb, x, x);
             }
+#if[compareToUnsigned]
+            if (xb.compareToUnsigned(xb) != 0) {
+                fail("compareToUnsigned not reflexive", xb, xb, x, x);
+            }
+#end[compareToUnsigned]
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for ($type$ y : VALUES) {
                 $Type$Buffer yb = $Type$Buffer.wrap(new $type$[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +772,34 @@
                     fail("Incorrect results for $Type$Buffer.compareTo",
                          xb, yb, x, y);
                 }
+#if[compareToUnsigned]
+                if (xb.compareToUnsigned(yb) != - yb.compareToUnsigned(xb)) {
+                    fail("compareToUnsigned not anti-symmetric",
+                         xb, yb, x, y);
+                }
+                if ((xb.compareToUnsigned(yb) == 0) != xb.equals(yb)) {
+                    fail("compareToUnsigned inconsistent with equals",
+                         xb, yb, x, y);
+                }
+                if (xb.compareToUnsigned(yb) != $Fulltype$.compareUnsigned(x, 
y)) {
+#if[float]
+                    if (x == 0.0 && y == 0.0) continue;
+#end[float]
+#if[double]
+                    if (x == 0.0 && y == 0.0) continue;
+#end[double]
+                    fail("Incorrect results for 
$Type$Buffer.compareToUnsigned",
+                         xb, yb, x, y);
+                }
+#end[compareToUnsigned]
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for $Type$Buffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for $Type$Buffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/BasicByte.java 
b/test/jdk/java/nio/Buffer/BasicByte.java
--- a/test/jdk/java/nio/Buffer/BasicByte.java
+++ b/test/jdk/java/nio/Buffer/BasicByte.java
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+
+        if (b.compareToUnsigned(b2) != 0) {
+            fail("Unsighed comparison to identical buffer != 0", b, b2);
+        }
+
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((byte)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser shorter buffer <= 0", b, b2);
+        }
+
         b.limit(b.limit() - 1);
 
         b.put(2, (byte)42);
@@ -714,7 +730,12 @@
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
 
-        // Check equals and compareTo with interesting values
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser value <= 0", b, b2);
+        }
+
+
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for (byte x : VALUES) {
             ByteBuffer xb = ByteBuffer.wrap(new byte[] { x });
             if (xb.compareTo(xb) != 0) {
@@ -723,6 +744,9 @@
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for (byte y : VALUES) {
                 ByteBuffer yb = ByteBuffer.wrap(new byte[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +767,34 @@
                     fail("Incorrect results for ByteBuffer.compareTo",
                          xb, yb, x, y);
                 }
+
+                if (xb.compareToUnsigned(yb) != - yb.compareToUnsigned(xb)) {
+                    fail("compareToUnsigned not anti-symmetric",
+                         xb, yb, x, y);
+                }
+                if ((xb.compareToUnsigned(yb) == 0) != xb.equals(yb)) {
+                    fail("compareToUnsigned inconsistent with equals",
+                         xb, yb, x, y);
+                }
+                if (xb.compareToUnsigned(yb) != Byte.compareUnsigned(x, y)) {
+
+
+
+
+
+
+                    fail("Incorrect results for ByteBuffer.compareToUnsigned",
+                         xb, yb, x, y);
+                }
+
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for ByteBuffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for ByteBuffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/BasicChar.java 
b/test/jdk/java/nio/Buffer/BasicChar.java
--- a/test/jdk/java/nio/Buffer/BasicChar.java
+++ b/test/jdk/java/nio/Buffer/BasicChar.java
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+
+
+
+
+
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((char)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+
+
+
+
+
         b.limit(b.limit() - 1);
 
         b.put(2, (char)42);
@@ -714,7 +730,12 @@
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
 
-        // Check equals and compareTo with interesting values
+
+
+
+
+
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for (char x : VALUES) {
             CharBuffer xb = CharBuffer.wrap(new char[] { x });
             if (xb.compareTo(xb) != 0) {
@@ -723,6 +744,9 @@
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for (char y : VALUES) {
                 CharBuffer yb = CharBuffer.wrap(new char[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +767,34 @@
                     fail("Incorrect results for CharBuffer.compareTo",
                          xb, yb, x, y);
                 }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for CharBuffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for CharBuffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/BasicDouble.java 
b/test/jdk/java/nio/Buffer/BasicDouble.java
--- a/test/jdk/java/nio/Buffer/BasicDouble.java
+++ b/test/jdk/java/nio/Buffer/BasicDouble.java
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+
+
+
+
+
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((double)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+
+
+
+
+
         b.limit(b.limit() - 1);
 
         b.put(2, (double)42);
@@ -714,7 +730,12 @@
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
 
-        // Check equals and compareTo with interesting values
+
+
+
+
+
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for (double x : VALUES) {
             DoubleBuffer xb = DoubleBuffer.wrap(new double[] { x });
             if (xb.compareTo(xb) != 0) {
@@ -723,6 +744,9 @@
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for (double y : VALUES) {
                 DoubleBuffer yb = DoubleBuffer.wrap(new double[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +767,34 @@
                     fail("Incorrect results for DoubleBuffer.compareTo",
                          xb, yb, x, y);
                 }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for DoubleBuffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for DoubleBuffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/BasicFloat.java 
b/test/jdk/java/nio/Buffer/BasicFloat.java
--- a/test/jdk/java/nio/Buffer/BasicFloat.java
+++ b/test/jdk/java/nio/Buffer/BasicFloat.java
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+
+
+
+
+
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((float)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+
+
+
+
+
         b.limit(b.limit() - 1);
 
         b.put(2, (float)42);
@@ -714,7 +730,12 @@
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
 
-        // Check equals and compareTo with interesting values
+
+
+
+
+
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for (float x : VALUES) {
             FloatBuffer xb = FloatBuffer.wrap(new float[] { x });
             if (xb.compareTo(xb) != 0) {
@@ -723,6 +744,9 @@
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for (float y : VALUES) {
                 FloatBuffer yb = FloatBuffer.wrap(new float[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +767,34 @@
                     fail("Incorrect results for FloatBuffer.compareTo",
                          xb, yb, x, y);
                 }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for FloatBuffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for FloatBuffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/BasicInt.java 
b/test/jdk/java/nio/Buffer/BasicInt.java
--- a/test/jdk/java/nio/Buffer/BasicInt.java
+++ b/test/jdk/java/nio/Buffer/BasicInt.java
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+
+        if (b.compareToUnsigned(b2) != 0) {
+            fail("Unsighed comparison to identical buffer != 0", b, b2);
+        }
+
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((int)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser shorter buffer <= 0", b, b2);
+        }
+
         b.limit(b.limit() - 1);
 
         b.put(2, (int)42);
@@ -714,7 +730,12 @@
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
 
-        // Check equals and compareTo with interesting values
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser value <= 0", b, b2);
+        }
+
+
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for (int x : VALUES) {
             IntBuffer xb = IntBuffer.wrap(new int[] { x });
             if (xb.compareTo(xb) != 0) {
@@ -723,6 +744,9 @@
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for (int y : VALUES) {
                 IntBuffer yb = IntBuffer.wrap(new int[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +767,34 @@
                     fail("Incorrect results for IntBuffer.compareTo",
                          xb, yb, x, y);
                 }
+
+                if (xb.compareToUnsigned(yb) != - yb.compareToUnsigned(xb)) {
+                    fail("compareToUnsigned not anti-symmetric",
+                         xb, yb, x, y);
+                }
+                if ((xb.compareToUnsigned(yb) == 0) != xb.equals(yb)) {
+                    fail("compareToUnsigned inconsistent with equals",
+                         xb, yb, x, y);
+                }
+                if (xb.compareToUnsigned(yb) != Integer.compareUnsigned(x, y)) 
{
+
+
+
+
+
+
+                    fail("Incorrect results for IntBuffer.compareToUnsigned",
+                         xb, yb, x, y);
+                }
+
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for IntBuffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for IntBuffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/BasicLong.java 
b/test/jdk/java/nio/Buffer/BasicLong.java
--- a/test/jdk/java/nio/Buffer/BasicLong.java
+++ b/test/jdk/java/nio/Buffer/BasicLong.java
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+
+        if (b.compareToUnsigned(b2) != 0) {
+            fail("Unsighed comparison to identical buffer != 0", b, b2);
+        }
+
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((long)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser shorter buffer <= 0", b, b2);
+        }
+
         b.limit(b.limit() - 1);
 
         b.put(2, (long)42);
@@ -714,7 +730,12 @@
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
 
-        // Check equals and compareTo with interesting values
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser value <= 0", b, b2);
+        }
+
+
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for (long x : VALUES) {
             LongBuffer xb = LongBuffer.wrap(new long[] { x });
             if (xb.compareTo(xb) != 0) {
@@ -723,6 +744,9 @@
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for (long y : VALUES) {
                 LongBuffer yb = LongBuffer.wrap(new long[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +767,34 @@
                     fail("Incorrect results for LongBuffer.compareTo",
                          xb, yb, x, y);
                 }
+
+                if (xb.compareToUnsigned(yb) != - yb.compareToUnsigned(xb)) {
+                    fail("compareToUnsigned not anti-symmetric",
+                         xb, yb, x, y);
+                }
+                if ((xb.compareToUnsigned(yb) == 0) != xb.equals(yb)) {
+                    fail("compareToUnsigned inconsistent with equals",
+                         xb, yb, x, y);
+                }
+                if (xb.compareToUnsigned(yb) != Long.compareUnsigned(x, y)) {
+
+
+
+
+
+
+                    fail("Incorrect results for LongBuffer.compareToUnsigned",
+                         xb, yb, x, y);
+                }
+
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for LongBuffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for LongBuffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/BasicShort.java 
b/test/jdk/java/nio/Buffer/BasicShort.java
--- a/test/jdk/java/nio/Buffer/BasicShort.java
+++ b/test/jdk/java/nio/Buffer/BasicShort.java
@@ -697,6 +697,17 @@
         if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
         }
+
+        if (b.compareToUnsigned(b2) != 0) {
+            fail("Unsighed comparison to identical buffer != 0", b, b2);
+        }
+
+        if (b.mismatch(b2) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
+        if (b2.mismatch(b) != -1) {
+            fail("Mismatch to identical buffer != -1", b, b2);
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((short)99);
@@ -706,6 +717,11 @@
             fail("Non-identical buffers equal", b, b2);
         if (b.compareTo(b2) <= 0)
             fail("Comparison to shorter buffer <= 0", b, b2);
+
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser shorter buffer <= 0", b, b2);
+        }
+
         b.limit(b.limit() - 1);
 
         b.put(2, (short)42);
@@ -714,7 +730,12 @@
         if (b.compareTo(b2) <= 0)
             fail("Comparison to lesser buffer <= 0", b, b2);
 
-        // Check equals and compareTo with interesting values
+        if (b.compareToUnsigned(b2) <= 0) {
+            fail("Unsighed comparison to lesser value <= 0", b, b2);
+        }
+
+
+        // Check equals, compareTo, compareToUnsigned, mismatch with 
interesting values
         for (short x : VALUES) {
             ShortBuffer xb = ShortBuffer.wrap(new short[] { x });
             if (xb.compareTo(xb) != 0) {
@@ -723,6 +744,9 @@
             if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
+            if (xb.mismatch(xb) != -1) {
+                fail("mismatch not reflexive", xb, xb, x, x);
+            }
             for (short y : VALUES) {
                 ShortBuffer yb = ShortBuffer.wrap(new short[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
@@ -743,10 +767,34 @@
                     fail("Incorrect results for ShortBuffer.compareTo",
                          xb, yb, x, y);
                 }
+
+                if (xb.compareToUnsigned(yb) != - yb.compareToUnsigned(xb)) {
+                    fail("compareToUnsigned not anti-symmetric",
+                         xb, yb, x, y);
+                }
+                if ((xb.compareToUnsigned(yb) == 0) != xb.equals(yb)) {
+                    fail("compareToUnsigned inconsistent with equals",
+                         xb, yb, x, y);
+                }
+                if (xb.compareToUnsigned(yb) != Short.compareUnsigned(x, y)) {
+
+
+
+
+
+
+                    fail("Incorrect results for ShortBuffer.compareToUnsigned",
+                         xb, yb, x, y);
+                }
+
                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
                     fail("Incorrect results for ShortBuffer.equals",
                          xb, yb, x, y);
                 }
+                if (xb.mismatch(yb) == -1 != ((x == y) || ((x != x) && (y != 
y)))) {
+                    fail("Incorrect results for ShortBuffer.mismatch",
+                         xb, yb, x, y);
+                }
             }
         }
 
diff --git a/test/jdk/java/nio/Buffer/EqualsCompareTest.java 
b/test/jdk/java/nio/Buffer/EqualsCompareTest.java
--- a/test/jdk/java/nio/Buffer/EqualsCompareTest.java
+++ b/test/jdk/java/nio/Buffer/EqualsCompareTest.java
@@ -83,22 +83,28 @@
         final BufferKind k;
         final Class<?> bufferType;
         final Class<?> elementType;
+        final boolean withCompareUnsigned;
 
         final MethodHandle eq;
         final MethodHandle cmp;
+        final MethodHandle cmpUns;
+        final MethodHandle mism;
 
         final MethodHandle getter;
         final MethodHandle setter;
 
-        BufferType(BufferKind k, Class<T> bufferType, Class<?> elementType) {
+        BufferType(BufferKind k, Class<T> bufferType, Class<?> elementType, 
boolean withCompareUnsigned) {
             this.k = k;
             this.bufferType = bufferType;
             this.elementType = elementType;
+            this.withCompareUnsigned = withCompareUnsigned;
 
             var lookup = MethodHandles.lookup();
             try {
                 eq = lookup.findVirtual(bufferType, "equals", 
MethodType.methodType(boolean.class, Object.class));
                 cmp = lookup.findVirtual(bufferType, "compareTo", 
MethodType.methodType(int.class, bufferType));
+                cmpUns = withCompareUnsigned ? lookup.findVirtual(bufferType, 
"compareToUnsigned", MethodType.methodType(int.class, bufferType)) : null;
+                mism = lookup.findVirtual(bufferType, "mismatch", 
MethodType.methodType(int.class, bufferType));
 
                 getter = lookup.findVirtual(bufferType, "get", 
MethodType.methodType(elementType, int.class));
                 setter = lookup.findVirtual(bufferType, "put", 
MethodType.methodType(bufferType, int.class, elementType));
@@ -176,6 +182,30 @@
             }
         }
 
+        int compareUnsigned(T a, T b) {
+            try {
+                return (int) cmpUns.invoke(a, b);
+            }
+            catch (RuntimeException | Error e) {
+                throw e;
+            }
+            catch (Throwable t) {
+                throw new Error(t);
+            }
+        }
+
+        int mismatch(T a, T b) {
+            try {
+                return (int) mism.invoke(a, b);
+            }
+            catch (RuntimeException | Error e) {
+                throw e;
+            }
+            catch (Throwable t) {
+                throw new Error(t);
+            }
+        }
+
         boolean pairWiseEquals(T a, T b) {
             if (a.remaining() != b.remaining())
                 return false;
@@ -186,9 +216,16 @@
             return true;
         }
 
+        int pairWiseMismatch(T a, T b) {
+            for (int i = a.position(), j = b.position(); i < a.position() + 
a.remaining() && j < b.position() + b.remaining(); i++, j++)
+                if (!get(a, i).equals(get(b, j)))
+                    return i;
+            return -1;
+        }
+
         static class Bytes extends BufferType<ByteBuffer, Byte> {
             Bytes(BufferKind k) {
-                super(k, ByteBuffer.class, byte.class);
+                super(k, ByteBuffer.class, byte.class, true);
             }
 
             @Override
@@ -213,7 +250,7 @@
 
         static class Chars extends BufferType<CharBuffer, Character> {
             Chars(BufferKind k) {
-                super(k, CharBuffer.class, char.class);
+                super(k, CharBuffer.class, char.class, false);
             }
 
             @Override
@@ -249,7 +286,7 @@
 
         static class Shorts extends BufferType<ShortBuffer, Short> {
             Shorts(BufferKind k) {
-                super(k, ShortBuffer.class, short.class);
+                super(k, ShortBuffer.class, short.class, true);
             }
 
             @Override
@@ -279,7 +316,7 @@
 
         static class Ints extends BufferType<IntBuffer, Integer> {
             Ints(BufferKind k) {
-                super(k, IntBuffer.class, int.class);
+                super(k, IntBuffer.class, int.class, true);
             }
 
             @Override
@@ -306,7 +343,7 @@
 
         static class Floats extends BufferType<FloatBuffer, Float> {
             Floats(BufferKind k) {
-                super(k, FloatBuffer.class, float.class);
+                super(k, FloatBuffer.class, float.class, false);
             }
 
             @Override
@@ -346,11 +383,22 @@
                 }
                 return true;
             }
+
+            @Override
+            int pairWiseMismatch(FloatBuffer a, FloatBuffer b) {
+                for (int i = a.position(), j = b.position(); i < a.position() 
+ a.remaining() && j < b.position() + b.remaining(); i++, j++) {
+                    float av = a.get(i);
+                    float bv = b.get(j);
+                    if (av != bv && (!Float.isNaN(av) || !Float.isNaN(bv)))
+                        return i;
+                }
+                return -1;
+            }
         }
 
         static class Longs extends BufferType<LongBuffer, Long> {
             Longs(BufferKind k) {
-                super(k, LongBuffer.class, long.class);
+                super(k, LongBuffer.class, long.class, true);
             }
 
             @Override
@@ -380,7 +428,7 @@
 
         static class Doubles extends BufferType<DoubleBuffer, Double> {
             Doubles(BufferKind k) {
-                super(k, DoubleBuffer.class, double.class);
+                super(k, DoubleBuffer.class, double.class, false);
             }
 
             @Override
@@ -420,6 +468,17 @@
                 }
                 return true;
             }
+
+            @Override
+            int pairWiseMismatch(DoubleBuffer a, DoubleBuffer b) {
+                for (int i = a.position(), j = b.position(); i < a.position() 
+ a.remaining() && j < b.position() + b.remaining(); i++, j++) {
+                    double av = a.get(i);
+                    double bv = b.get(j);
+                    if (av != bv && (!Double.isNaN(av) || !Double.isNaN(bv)))
+                        return i;
+                }
+                return -1;
+            }
         }
     }
 
@@ -635,13 +694,28 @@
                                 if (eq) {
                                     Assert.assertEquals(bt.compare(as, bs), 0);
                                     Assert.assertEquals(bt.compare(bs, as), 0);
+
+                                    if (bt.withCompareUnsigned) {
+                                        
Assert.assertEquals(bt.compareUnsigned(as, bs), 0);
+                                        
Assert.assertEquals(bt.compareUnsigned(bs, as), 0);
+                                    }
                                 }
                                 else {
                                     int aCb = bt.compare(as, bs);
                                     int bCa = bt.compare(bs, as);
                                     int v = Integer.signum(aCb) * 
Integer.signum(bCa);
                                     Assert.assertTrue(v == -1);
+
+                                    if (bt.withCompareUnsigned) {
+                                        aCb = bt.compareUnsigned(as, bs);
+                                        bCa = bt.compareUnsigned(bs, as);
+                                        v = Integer.signum(aCb) * 
Integer.signum(bCa);
+                                        Assert.assertTrue(v == -1);
+                                    }
                                 }
+
+                                Assert.assertEquals(bt.mismatch(as, bs), 
bt.pairWiseMismatch(as, bs));
+                                Assert.assertEquals(bt.mismatch(bs, as), 
bt.pairWiseMismatch(bs, as));
                             }
                         }
 
@@ -661,6 +735,16 @@
                                 int aCc = bt.compare(as, cs);
                                 int v = Integer.signum(cCa) * 
Integer.signum(aCc);
                                 Assert.assertTrue(v == -1);
+
+                                if (bt.withCompareUnsigned) {
+                                    cCa = bt.compareUnsigned(cs, as);
+                                    aCc = bt.compareUnsigned(as, cs);
+                                    v = Integer.signum(cCa) * 
Integer.signum(aCc);
+                                    Assert.assertTrue(v == -1);
+                                }
+
+                                Assert.assertEquals(bt.mismatch(as, cs), 
bt.pairWiseMismatch(as, cs));
+                                Assert.assertEquals(bt.mismatch(cs, as), 
bt.pairWiseMismatch(cs, as));
                             }
                         }
                     }
diff --git a/test/jdk/java/nio/Buffer/genBasic.sh 
b/test/jdk/java/nio/Buffer/genBasic.sh
--- a/test/jdk/java/nio/Buffer/genBasic.sh
+++ b/test/jdk/java/nio/Buffer/genBasic.sh
@@ -23,16 +23,20 @@
 # questions.
 #
 
-javac -d . ../../../../make/src/classes/build/tools/spp/Spp.java
+javac -d . ../../../../../make/jdk/src/classes/build/tools/spp/Spp.java
 
 gen() {
     java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -DFulltype=$3 
<Basic-X.java.template >Basic$2.java
 }
 
-gen byte Byte Byte
+genu() {
+    java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -DFulltype=$3 
-KcompareToUnsigned <Basic-X.java.template >Basic$2.java
+}
+
+genu byte Byte Byte
 gen char Char Character
-gen short Short Short
-gen int Int Integer
-gen long Long Long
+genu short Short Short
+genu int Int Integer
+genu long Long Long
 gen float Float Float
 gen double Double Double
diff --git a/test/jdk/java/nio/Buffer/genCopyDirectMemory.sh 
b/test/jdk/java/nio/Buffer/genCopyDirectMemory.sh
--- a/test/jdk/java/nio/Buffer/genCopyDirectMemory.sh
+++ b/test/jdk/java/nio/Buffer/genCopyDirectMemory.sh
@@ -23,7 +23,7 @@
 # questions.
 #
 
-javac -d . ../../../../make/src/classes/build/tools/spp/Spp.java > Spp.java
+javac -d . ../../../../../make/jdk/src/classes/build/tools/spp/Spp.java
 
 gen() {
     java  build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 
-DFulltype=$3<CopyDirect-X-Memory.java.template >CopyDirect$2Memory.java
diff --git a/test/jdk/java/nio/Buffer/genOrder.sh 
b/test/jdk/java/nio/Buffer/genOrder.sh
--- a/test/jdk/java/nio/Buffer/genOrder.sh
+++ b/test/jdk/java/nio/Buffer/genOrder.sh
@@ -23,7 +23,7 @@
 # questions.
 #
 
-javac -d . ../../../../make/src/classes/build/tools/spp/Spp.java > Spp.java
+javac -d . ../../../../../make/jdk/src/classes/build/tools/spp/Spp.java
 
 gen() {
     java  build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 
-DFulltype=$3<Order-X.java.template >Order$2.java

Reply via email to