I recently noticed a behavioral discrepancy in 
jdk.internal.util.ArraysSupport.vectorizedMismatch between the Java 
implementation and the platform intrinsic implementations.

Current behavior

The Java implementation may leave a tail of elements unchecked, returning the 
bitwise complement of the number of remaining elements (i.e., ~remaining).
The x86_64 intrinsic, by contrast, compares all elements and simply returns -1 
when no mismatch is found.
Because of this inconsistency, every caller has to handle the "remaining 
elements" case defensively:


int i = vectorizedMismatch(...);
if (i >= 0) {
    return i; // mismatch found
} else {
    length -= ~i; // fall through to handle remaining elements
}

Proposed change

This PR refines the Java implementation so that it always compares all elements 
and returns -1 when no mismatch is found, matching the x86_64 intrinsic 
behavior. This also simplify all callsite because iteliminates the need for 
callers to handle remaining elements.

A regression test is included at 
`test/hotspot/jtreg/compiler/intrinsics/VectorizedMismatchReturnDiffTest.java` 
which demonstrates the original behavioral difference.

## Test
- [x] tier1 test suites on linux x86_64
- [ ] tier1 test suites on linux aarch64

---------
- [x] I confirm that I make this contribution in accordance with the [OpenJDK 
Interim AI Policy](https://openjdk.org/legal/ai).

-------------

Commit messages:
 - 8387812: Refine ArraysSupport.vectorizedMismatch to compare all elements.

Changes: https://git.openjdk.org/jdk/pull/31802/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=31802&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8387812
  Stats: 377 lines in 5 files changed: 222 ins; 92 del; 63 mod
  Patch: https://git.openjdk.org/jdk/pull/31802.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/31802/head:pull/31802

PR: https://git.openjdk.org/jdk/pull/31802

Reply via email to