RFR: 8356891: Some code simplifications for basic BigIntegers' bit operations

fabioromano1 duke at openjdk.org
Tue May 13 18:02:05 UTC 2025


On Mon, 12 May 2025 01:52:51 GMT, Chen Liang <liach at openjdk.org> wrote:

>> src/java.base/share/classes/java/math/BigInteger.java line 2642:
>> 
>>> 2640:         }
>>> 2641:         // (this.abs().bitLength() - 1L) * exponent + 1L > Integer.MAX_VALUE
>>> 2642:         if (scaleFactor + bitsToShift - exponent >= Integer.MAX_VALUE) {
>> 
>> If this is overflow sensitive code, should this avoid adding on the left hand side?
>> 
>> Suggestion:
>> 
>>         if (scaleFactor - exponent >= Integer.MAX_VALUE - bitsToShift) {
>
> I think we are fine here, `scaleFactor` is already `long`

> If this is overflow sensitive code, should this avoid adding on the left hand side?

`scaleFactor` is a `long`, so `scaleFactor + bitsToShift - exponent == (bitLength() - 1L) * exponent`. Moreover, `scaleFactor + bitsToShift == (long) bitLength() * exponent`, so it does not oveflow a `long`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25166#discussion_r2084057073


More information about the core-libs-dev mailing list