<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Hi,<br>
<br>
Another API alternative would be to add a version that triggers a
lambda on overflow and allow the caller to determine the value or
throw.<br>
<div style="background-color:#ffffff;color:#080808">
<div style="background-color:#ffffff;color:#080808">
<pre style="font-family:'NanumGothicCoding',monospace;font-size:10.5pt;"><span style="color:#0033b3;">public static long </span><span style="color:#00627a;">multiplyExact</span>(<span style="color:#0033b3;">long </span><span style="color:#000000;">x</span>, <span style="color:#0033b3;">long </span><span style="color:#000000;">y</span>, <span style="color:#000000;">LongBinaryOperator recompute</span>) {
<span style="color:#0033b3;">long </span><span style="color:#000000;">r </span>= <span style="color:#000000;">x </span>* <span style="color:#000000;">y</span>;
<span style="color:#0033b3;">long </span><span style="color:#000000;">ax </span>= <span style="color:#000000;">Math</span>.<span style="font-style:italic;">abs</span>(<span style="color:#000000;">x</span>);
<span style="color:#0033b3;">long </span><span style="color:#000000;">ay </span>= <span style="color:#000000;">Math</span>.<span style="font-style:italic;">abs</span>(<span style="color:#000000;">y</span>);
<span style="color:#0033b3;">if </span>(((<span style="color:#000000;">ax </span>| <span style="color:#000000;">ay</span>) >>> <span style="color:#1750eb;">31 </span>!= <span style="color:#1750eb;">0</span>)) {
<span style="color:#8c8c8c;font-style:italic;">// Some bits greater than 2^31 that might cause overflow
</span><span style="color:#8c8c8c;font-style:italic;"> // Check the result using the divide operator
</span><span style="color:#8c8c8c;font-style:italic;"> // and check for the special case of Long.MIN_VALUE * -1
</span><span style="color:#8c8c8c;font-style:italic;"> </span><span style="color:#0033b3;">if </span>(((<span style="color:#000000;">y </span>!= <span style="color:#1750eb;">0</span>) && (<span style="color:#000000;">r </span>/ <span style="color:#000000;">y </span>!= <span style="color:#000000;">x</span>)) ||
(<span style="color:#000000;">x </span>== <span style="color:#000000;">Long</span>.<span style="color:#871094;font-style:italic;">MIN_VALUE </span>&& <span style="color:#000000;">y </span>== -<span style="color:#1750eb;">1</span>)) {
<span style="color:#0033b3;">return </span><span style="color:#000000;">recompute</span>.applyAsLong(<span style="color:#000000;">x</span>, <span style="color:#000000;">y</span>);
}
}
<span style="color:#0033b3;">return </span><span style="color:#000000;">r</span>;
}</pre>
</div>
</div>
$.02, Roger<br>
<br>
<div class="moz-cite-prefix">On 12/13/24 8:22 AM, Raffaello
Giulietti wrote:<br>
</div>
<blockquote type="cite" cite="mid:c701bf05-070d-464b-8016-d1e691e195d8@oracle.com">
<br>
<br>
On 2024-12-12 23:58, Charles Oliver Nutter wrote:
<br>
<blockquote type="cite">
<br>
Question two: Am I losing the benefits of *Exact if I use the
following code to "pre-check" for overflow?
<br>
<br>
long high = Math.multiplyHigh(a, b);
<br>
if (high == 0) return Math.multiplyExact(a, b);
<br>
return bigIntegerMultiply(a, b);
<br>
<br>
</blockquote>
<br>
For your specific multiplication use case you might try with
<br>
<br>
long high = Math.multiplyHigh(a, b);
<br>
long low = a * b;
<br>
if (high == 0) return low;
<br>
return "the big integer consisting of high and low";
<br>
<br>
It might be possible that the multiplyHigh() and * on the same
operands, when appearing adjacent to each other, get optimized to
just one instruction.
<br>
And if not, they might be executed "in parallel" inside the CPU.
<br>
<br>
<br>
HTH
<br>
Raffaello
<br>
<br>
</blockquote>
<br>
</body>
</html>