-
Notifications
You must be signed in to change notification settings - Fork 918
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
compiler: implement most math/bits functions #3620
Conversation
These functions can be implemented more efficiently using LLVM intrinsics. That makes them the Go equivalent of functions like __builtin_clz which are also implemented using these LLVM intrinsics. I believe the Go compiler does something very similar: IIRC it converts calls to these functions into optimal instructions for the given architecture. I tested these by running `tinygo test math/bits` after uncommenting the tests that would always fail (the *PanicZero and *PanicOverflow tests).
My only concern is that the llvm versions are "slightly" different from the Go versions. But I suppose running the tests with all their edge cases probably verifies that they're not. |
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.
LGTM
My only concern is that the llvm versions are "slightly" different from the Go versions. But I suppose running the tests with all their edge cases probably verifies that they're not. |
Thanks @aykevl for the maths bits 😺 |
In what way are they different? They should have identical behavior the way I implemented it, or did I miss something? For example, here is OnesCount32:
And here is the LLVM version:
To me, that's exactly the same thing. |
Oops, that was poor writing on my part. "My only concern would be if the two versions are slightly different." But it turns out they're not and running all the tests shows that to be the case. |
Ah I see. Yes, the tests all pass which is good. (Which is also a reason to not replace the package entirely: this way, we keep all existing tests). |
These functions can be implemented more efficiently using LLVM intrinsics (at the moment, they're implemented as regular Go code). That makes them the Go equivalent of functions like __builtin_clz which are also implemented using these LLVM intrinsics.
I believe the Go compiler does something very similar: IIRC it converts calls to these functions into optimal instructions for the given architecture.
I tested these by running
tinygo test math/bits
afteruncommentingcommenting the tests that would always fail (the *PanicZero and *PanicOverflow tests). We should either implement-test.skip
or the ability to catch runtime panics to be able to test this automatically.EDIT: s/uncommenting/commenting