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

Raffaello Giulietti raffaello.giulietti at gmail.com
Fri Jul 2 11:26:16 UTC 2021


... or even as a one liner, like in the test

	return Math.multiplyHigh(x, y) + ((x >> (Long.SIZE - 1)) & y) + ((y >> 
(Long.SIZE - 1)) & x);



On 2021-07-02 13:08, Andrew Dinn wrote:
> 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