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

Float round and roundf are slow #154

Open
raphlinus opened this issue Oct 17, 2018 · 0 comments
Open

Float round and roundf are slow #154

raphlinus opened this issue Oct 17, 2018 · 0 comments

Comments

@raphlinus
Copy link

raphlinus commented Oct 17, 2018

This bug is branched from rust-lang/rust#55107.

The code for round (adapted from musl libm) is correct (I verified it for all f32 values) but slow, containing many branches.

The following code is also correct and should be much faster (this is the f32 version, which is the main thing I'm testing now, since I'm looking at simd). This is the positive branch.

    ((x + (0.25 - 0.5 * f32::EPSILON)) + (0.25 + 0.5 * f32::EPSILON)).floor()

There are two ways to make this work for signed values: one is to branch on the sign of the argument, the other is to do copysign(... x.abs() ..., x). The copysign intrinsic is not currently exposed in the Rust std lib though, for which I'll file an issue. One of the nice things about copysign is that there's a wasm intrinsic. That should get this function down to around 9 asm instructions from the current ~92.

It would also be really nice to expose the nearest wasm instruction as well; the problem is that there's no llvm intrinsic with the exactly correct semantics. I'm going to be exploring that as well.

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

No branches or pull requests

1 participant