Skip to content
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

Merged
merged 1 commit into from
Mar 29, 2023
Merged

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Mar 29, 2023

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 after uncommenting commenting 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

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).
@dgryski
Copy link
Member

dgryski commented Mar 29, 2023

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.

Copy link
Member

@dgryski dgryski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dgryski
Copy link
Member

dgryski commented Mar 29, 2023

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.

@deadprogram
Copy link
Member

Thanks @aykevl for the maths bits 😺

@deadprogram deadprogram merged commit 464ebc4 into dev Mar 29, 2023
@deadprogram deadprogram deleted the math-bits-intrinsics branch March 29, 2023 18:55
@aykevl
Copy link
Member Author

aykevl commented Mar 30, 2023

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.

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:

OnesCount32 returns the number of one bits ("population count") in x.

And here is the LLVM version:

The ‘llvm.ctpop’ family of intrinsics counts the number of bits set in a value.

To me, that's exactly the same thing.

@dgryski
Copy link
Member

dgryski commented Mar 30, 2023

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.

@aykevl
Copy link
Member Author

aykevl commented Mar 30, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants