RFR: 8349637: Integer.numberOfLeadingZeros outputs incorrectly in certain cases
Jasmine Karthikeyan
jkarthikeyan at openjdk.org
Wed Feb 12 05:52:51 UTC 2025
Hi all,
This is a fix for a miscompile in the AVX2 implementation of `CountLeadingZerosV` for int types. Currently, the implementation turns ints into floats, in order to calculating the leading zeros based on the exponent part of the float. Unfortunately, floats can only accurately represent integers up to 2^24. After that, multiple integer values can map onto the same floating point value. The issue manifests when an int is converted to a floating point representation that is higher than it, crossing a bit boundary. As an example, `(float)0x01FFFFFF == (float)0x02000000`, but `lzcnt(0x01FFFFFF) == 7` and `lzcnt(0x02000000) == 6`. The values are incorrectly rounded up.
This patch fixes the issue by masking the input in the cases where it is larger than 2^24, to set the low bits to 0. Removing these bits prevents the accidental rounding behavior. I've added these cases to`TestNumberOfContinuousZeros`, and removed the set random seed so that it can produce random inputs to test with.
Reviews would be appreciated!
-------------
Commit messages:
- Fix CountLeadingZerosV miscompile on AVX2
Changes: https://git.openjdk.org/jdk/pull/23579/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=23579&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8349637
Stats: 49 lines in 2 files changed: 48 ins; 0 del; 1 mod
Patch: https://git.openjdk.org/jdk/pull/23579.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/23579/head:pull/23579
PR: https://git.openjdk.org/jdk/pull/23579
More information about the hotspot-compiler-dev
mailing list