RFR: 8188044: We need Math.unsignedMultiplyHigh [v2]

Andrew Dinn adinn at openjdk.java.net
Fri Jul 2 11:08:59 UTC 2021


On Fri, 2 Jul 2021 09:39:46 GMT, Andrew Haley <aph at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/Math.java line 1211:
>> 
>>> 1209:         long z1 = t >>> 32;
>>> 1210: 
>>> 1211:         return x1 * y1 + z1 + (z0 >>> 32);
>> 
>> Suggestion:
>> 
>>         long result = Math.multiplyHigh(x, y);
>>         if (x < 0) result += y;
>>         if (y < 0) result += x;
>>         return result;
>
> This is just subtracting the 2's-complement offset. I guess the idea, longer term, is that this be an intrinsic anyway, but if you do `unsignedMultiplyHigh` this way you'll utilize the existing `multiplyHigh` intrinsic on all platforms that have it.

You can also do that branchlessly which might prove better

         long result = Math.multiplyHigh(x, y);
         result += (y & (x >> 63));
         result += (x & (y >> 63));
         return result;

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

PR: https://git.openjdk.java.net/jdk/pull/4644


More information about the core-libs-dev mailing list