On Fri, 2 May 2025 16:53:39 GMT, Raffaello Giulietti <rgiulie...@openjdk.org> 
wrote:

> As for the `bitLength` check, the product might overflow. Further, 
> `bitLength` might not be that cheap.

The current implementations of `bitLength()` calls `numberOfLeadingZeros`, 
which are:

public static int numberOfLeadingZeros(long i) {
        int x = (int)(i >>> 32);
        return x == 0 ? 32 + Integer.numberOfLeadingZeros((int)i)
                : Integer.numberOfLeadingZeros(x);
    }

public static int numberOfLeadingZeros(int i) {
        // HD, Count leading 0's
        if (i <= 0)
            return i == 0 ? 32 : 0;
        int n = 31;
        if (i >= 1 << 16) { n -= 16; i >>>= 16; }
        if (i >= 1 <<  8) { n -=  8; i >>>=  8; }
        if (i >= 1 <<  4) { n -=  4; i >>>=  4; }
        if (i >= 1 <<  2) { n -=  2; i >>>=  2; }
        return n - (i >>> 1);
    }

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25003#discussion_r2071934498

Reply via email to