High cost of failed Math.*Exact calls
Raffaello Giulietti
raffaello.giulietti at oracle.com
Fri Dec 13 13:22:00 UTC 2024
On 2024-12-12 23:58, Charles Oliver Nutter wrote:
>
> Question two: Am I losing the benefits of *Exact if I use the following
> code to "pre-check" for overflow?
>
> long high = Math.multiplyHigh(a, b);
> if (high == 0) return Math.multiplyExact(a, b);
> return bigIntegerMultiply(a, b);
>
For your specific multiplication use case you might try with
long high = Math.multiplyHigh(a, b);
long low = a * b;
if (high == 0) return low;
return "the big integer consisting of high and low";
It might be possible that the multiplyHigh() and * on the same operands,
when appearing adjacent to each other, get optimized to just one
instruction.
And if not, they might be executed "in parallel" inside the CPU.
HTH
Raffaello
More information about the core-libs-dev
mailing list