New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Math.multiplyHigh for Long #3480
Conversation
Could it be done more efficiently as mapping to C functions, like here? |
We also discussed on Discord once if it can be implemented more efficiently by using the LLVM intrinsics with i128 integers. https://discord.com/channels/632150470000902164/635668881951686686/1140736146326949900 With that said, this PR is an improvement over the status quo, and more optimizations can be follow-up. |
I do not mean to sound harsh here but to reinforce community standards. Posting a URL makes it too easy, especially for first time |
Nice to see this contribution. I suggest that you not be concerned or spend time on the "multiarch" linux-arm-64 CI I suspect something went bump in the night in mainline some where around Sept 6 or 7. I hope to I will monitor this PR to see if any other errors look related. My hope is to save you time & anxiety, so Please let me know if you have questions or concerns. |
Thank you for this contribution. The only thing that is missing is probably some test, you create them in Note: if testing with Scala 2 replace the |
219d2f4
to
3151a7f
Compare
Thanks for all the helpful comments! And apologies for being slow with the update. I've read now the contributions guidelines, apologies for the hickup with regards to the OpenJDK Code; once seen it cannot be unseen. I've cleaned up the code and tested it. However, as mentioned above by others, the possibility of implementing with LLVM intrinsics or similar seems to be the most promising solution. For these reasons I think it might be better to go with the LLVM solution. I have posted an update anyways, just to finalize the work that was started. SummaryI've fixed the code, and added some test cases. Let me know what is appropriate. It runs on my machine with the Scala 3 and 2 tests for JVM and Native. Some further comments:It was unclear if to use |
To allow for usage of LLVM intrinsics, then we probably would first need to introduce concept of Int128 (possibly in both primitive and boxed form) which we later would be able to use in LLVM |
@@ -275,4 +275,57 @@ class MathTest { | |||
} | |||
} | |||
|
|||
@Test def multiplyHighTests(): Unit = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI fails becouse JDK 8 does not define this method. Math.multipleHight
was added in JDK 9, so we need to put the test in this file
Line 6 in bc2d1cb
class MathTestOnJDK9 { |
The
require-jdkX
ensures that given test is only compiled when using JDK X or later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the hint! I've moved the test to the linked file. Hopefully all checks are successful.
I also tested on some random numbers as a sanity check, but the test might be too heavy for the CI, so I'll just paste it here instead. Perhaps someone will find it useful. import scala.util.Random
import scala.math.BigInt
for (i <- 0 until 1000000) {
val a = BigInt(Random.nextLong())
val b = BigInt(Random.nextLong())
val result = Math.multiplyHigh(a.toLong, b.toLong)
val ab = a * b
val expected = (ab >> 64).longValue
assertTrue(
s"Math.multiplyHigh(${a.toLong}, ${b.toLong}) result: ${result} != expected: ${expected}",
Math.multiplyHigh(a.toLong, b.toLong) == expected
)
} |
* Created Math multiplyHigh method * Move multiplyHigh tests to JDK9 tests dir (cherry picked from commit 6c89cde)
* Created Math multiplyHigh method * Move multiplyHigh tests to JDK9 tests dir (cherry picked from commit 6c89cde)
This is a draft PR towards implementing the
Math.multiplyHigh
method for Long #2473. Currently missing are some tests, and will also look over the method implementation with fresh eyes next week. This was done as part of the Scala-Spree in Madrid.The algorithm used here is based on the one presented in Hacker's Delight (Algorithm 8.2).