RFR: 8349637: Integer.numberOfLeadingZeros outputs incorrectly in certain cases [v3]

Emanuel Peter epeter at openjdk.org
Fri Feb 14 16:29:11 UTC 2025


On Wed, 12 Feb 2025 19:45:34 GMT, Jasmine Karthikeyan <jkarthikeyan at openjdk.org> wrote:

>> 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!
>
> Jasmine Karthikeyan has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Improve explanation of logic

So far I found 4 operations that have issues with **truncation**.


// java -Xbatch -XX:UseAVX=2 -XX:CompileCommand=compileonly,TestShort::test -XX:CompileCommand=printcompilation,TestShort::test -XX:CompileCommand=TraceAutoVectorization,TestShort::test,SW_REJECTIONS -XX:+TraceNewVectors TestShort.java

import java.util.Random;

public class TestShort {
    static Random RANDOM = new Random();
    static int SIZE = 16 * 1024;

    static short[] a = new short[SIZE];
    static short[] r = new short[SIZE];
    static short[] gold = new short[SIZE];


    public static void test(short[] out) {
        for (int i = 0; i < a.length; ++i) {
            // List of problematic operations, where truncation happens:
            //out[i] = (short)Integer.numberOfLeadingZeros(a[i]);
            //out[i] = (short)Integer.numberOfTrailingZeros(a[i]);
            //out[i] = (short)Integer.reverse(a[i]);
            out[i] = (short)Integer.bitCount(a[i]);

            // Seem ok, maybe because they do not vectorize
            //out[i] = (short)Integer.signum(a[i]);
            //out[i] = (short)Integer.highestOneBit(a[i]);
            //out[i] = (short)Integer.lowestOneBit(a[i]);

            // While we are at it, we should also have tests for this, even though it currently does not vectorize,
            // but it may in the future and then we have to catch the truncation.
            // out[i] = (short)Long.bitCount(a[i]);
        }
   }

    public static void main(String[] args) {
        for (int i = 0; i < a.length; ++i) {
            a[i] = (short)RANDOM.nextInt();
        }
        System.out.println("Compute gold:");
        test(gold);
        System.out.println("Compile and compute:");
        for (int j = 0; j < 1000; ++j) {
            test(r);
        }
        System.out.println("Verify:");
        for (int i = 0; i < a.length; ++i) {
            if (gold[i] != r[i]) throw new RuntimeException("Wrong result " + r[i] + " vs " + gold[i] + " for " + a[i] + " at " + i);
        }
    }
}

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

PR Comment: https://git.openjdk.org/jdk/pull/23579#issuecomment-2659767156


More information about the hotspot-compiler-dev mailing list