8188044: We need Math.unsignedMultiplyHigh#4644
Conversation
|
👋 Welcome back bpb! A progress list of the required criteria for merging this PR into |
Webrevs
|
|
This PR does not address intrinsics for the proposed method; that aspect can be handled subsequently. |
|
/csr |
|
@bplb this pull request will not be integrated until the CSR request JDK-8269705 for issue JDK-8188044 has been approved. |
|
|
|
Yes, I am aware of those methods on |
|
Oh right. Then obviously this is the right place for the new method. Sorry for the noise. |
| long x0 = x & 0xffffffffL; | ||
| long x1 = x >>> 32; | ||
| long y0 = y & 0xffffffffL; | ||
| long y1 = y >>> 32; | ||
|
|
||
| long t = x1 * y0 + ((x0 * y0) >>> 32); | ||
| long z0 = x0 * y1 + (t & 0xffffffffL); | ||
| long z1 = t >>> 32; | ||
|
|
||
| return x1 * y1 + z1 + (z0 >>> 32); |
There was a problem hiding this comment.
| long x0 = x & 0xffffffffL; | |
| long x1 = x >>> 32; | |
| long y0 = y & 0xffffffffL; | |
| long y1 = y >>> 32; | |
| long t = x1 * y0 + ((x0 * y0) >>> 32); | |
| long z0 = x0 * y1 + (t & 0xffffffffL); | |
| long z1 = t >>> 32; | |
| return x1 * y1 + z1 + (z0 >>> 32); | |
| long result = Math.multiplyHigh(x, y); | |
| if (x < 0) result += y; | |
| if (y < 0) result += x; | |
| return result; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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;
I doubt very much that it would be better, because these days branch prediction is excellent, and we also have conditional select instructions. Exposing the condition helps C2 to eliminate it if the range of args is known. The if code is easier to understand.
Benchmark results, with one of the operands changing signs every iteration, 1000 iterations:
Benchmark Mode Cnt Score Error Units
MulHiTest.mulHiTest1 (aph) avgt 3 1570.587 ± 16.602 ns/op
MulHiTest.mulHiTest2 (adinn) avgt 3 2237.637 ± 4.740 ns/op
In any case, note that with this optimization the unsigned mulHi is in the nanosecond range, so Good Enough. IMO.
There was a problem hiding this comment.
But weirdly, it's the other way around on AArch64, but there's little in it:
Benchmark Mode Cnt Score Error Units
MulHiTest.mulHiTest1 avgt 3 1492.108 ± 0.301 ns/op
MulHiTest.mulHiTest2 avgt 3 1219.521 ± 1.516 ns/op
but this is only in the case where we have unpredictable branches. Go with simple and easy to understand; it doesn't much matter.
2ad5e39 to
a499b2e
Compare
|
@bplb This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 174 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
|
/integrate |
|
Going to push as commit ca4bea4.
Your commit was automatically rebased without conflicts. |
|
Mailing list message from Raffaello Giulietti on core-libs-dev: ... or even as a one liner, like in the test (Long.SIZE - 1)) & x); On 2021-07-02 13:08, Andrew Dinn wrote: |
2 similar comments
|
Mailing list message from Raffaello Giulietti on core-libs-dev: ... or even as a one liner, like in the test (Long.SIZE - 1)) & x); On 2021-07-02 13:08, Andrew Dinn wrote: |
|
Mailing list message from Raffaello Giulietti on core-libs-dev: ... or even as a one liner, like in the test (Long.SIZE - 1)) & x); On 2021-07-02 13:08, Andrew Dinn wrote: |
|
Mailing list message from Andrew Haley on core-libs-dev: On 7/2/21 12:26 PM, Raffaello Giulietti wrote:
It was hard to write, so it should be hard to read too! :-) |
|
Mailing list message from Raffaello Giulietti on core-libs-dev: FWIW, adinn's branchless code together with Greetings On 2021-07-02 15:50, Andrew Haley wrote: |
|
Mailing list message from Andrew Haley on core-libs-dev: On 7/2/21 12:26 PM, Raffaello Giulietti wrote:
It was hard to write, so it should be hard to read too! :-) |
|
Mailing list message from Raffaello Giulietti on core-libs-dev: FWIW, adinn's branchless code together with Greetings On 2021-07-02 15:50, Andrew Haley wrote: |
|
Mailing list message from Andrew Haley on core-libs-dev: On 7/2/21 12:26 PM, Raffaello Giulietti wrote:
It was hard to write, so it should be hard to read too! :-) |
|
Mailing list message from Andrew Dinn on core-libs-dev: On 02/07/2021 16:30, Raffaello Giulietti wrote:
That was indeed the point of the change. However, I doubt the difference regards, Andrew Dinn |
1 similar comment
|
Mailing list message from Andrew Dinn on core-libs-dev: On 02/07/2021 16:30, Raffaello Giulietti wrote:
That was indeed the point of the change. However, I doubt the difference regards, Andrew Dinn |
|
Mailing list message from Raffaello Giulietti on core-libs-dev: FWIW, adinn's branchless code together with Greetings On 2021-07-02 15:50, Andrew Haley wrote: |
|
Mailing list message from Andrew Dinn on core-libs-dev: On 02/07/2021 16:30, Raffaello Giulietti wrote:
That was indeed the point of the change. However, I doubt the difference regards, Andrew Dinn |
|
Mailing list message from Andrew Haley on core-libs-dev: On 7/2/21 4:30 PM, Raffaello Giulietti wrote:
I doubt it, because HotSpot generates conditional select instructions for I guess it might on a C1-only or pure interpreter system. That certainly -- |
2 similar comments
|
Mailing list message from Andrew Haley on core-libs-dev: On 7/2/21 4:30 PM, Raffaello Giulietti wrote:
I doubt it, because HotSpot generates conditional select instructions for I guess it might on a C1-only or pure interpreter system. That certainly -- |
|
Mailing list message from Andrew Haley on core-libs-dev: On 7/2/21 4:30 PM, Raffaello Giulietti wrote:
I doubt it, because HotSpot generates conditional select instructions for I guess it might on a C1-only or pure interpreter system. That certainly -- |
Please consider this proposal to add a method
unsignedMultiplyHigh(long,long)to each ofjava.lang.Mathandjava.lang.StrictMath.Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4644/head:pull/4644$ git checkout pull/4644Update a local copy of the PR:
$ git checkout pull/4644$ git pull https://git.openjdk.java.net/jdk pull/4644/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 4644View PR using the GUI difftool:
$ git pr show -t 4644Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4644.diff