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

Add rsqrt method to Float trait #1

Open
cuviper opened this issue Dec 19, 2017 · 7 comments
Open

Add rsqrt method to Float trait #1

cuviper opened this issue Dec 19, 2017 · 7 comments

Comments

@cuviper
Copy link
Member

cuviper commented Dec 19, 2017

From @davll on November 9, 2017 8:48

rsqrt is a widely used math function in game development, and is faster than combining the two functions: recip(sqrt(x)) thanks to x86 SSE instructions RSQRTSS, RSQRTPS, RSQRTSD, and RSQRTPD. Should we consider add rsqrt to Float trait?

  • x86/x86_64 SSE: _mm_rsqrt_ps (note that RSQRT instruction is approximate, less accurate than SQRT)
  • ARM Neon VRSQRTE
  • PowerPC Altivec: vec_rsqrte

Copied from original issue: rust-num/num#343

@cuviper
Copy link
Member Author

cuviper commented Dec 19, 2017

This should probably be proposed for the standard library first. So far we don't have any arch-specific code -- I'd rather just have num's impl Float for f64 forward to an optimized/intrinsic call in std.

@cuviper
Copy link
Member Author

cuviper commented Dec 19, 2017

From @clarcharr on November 10, 2017 1:8

I agree; it makes sense to offer this in libstd.

@cuviper
Copy link
Member Author

cuviper commented Dec 19, 2017

From @davll on November 10, 2017 3:10

Reasonable, I'll propose it in rust-lang/rust.
I'll keep the issue open as reminder.

@clarfonthey
Copy link
Contributor

This appears to have been in libstd originally, then later removed. I don't think that anyone actually opened an issue for it, because I can't find it.

@cuviper
Copy link
Member Author

cuviper commented Dec 21, 2017

rust-lang/rust#23549 added:

#[deprecated(since = "1.0.0", reason = "use self.sqrt().recip() instead")]

and then rust-lang/rust#24636 removed it. Neither PR mentioned rsqrt specifically.

If you propose it back to std, make sure to point out the possibility of intrinsics doing better than sqrt().recip(). However, you should also make sure that LLVM isn't already optimizing it to one op. It might not be able to do that because of the difference in precision, but it's worth checking.

@upsuper
Copy link

upsuper commented Jun 16, 2018

Looks like LLVM wouldn't optimize it automatically. Also it's probably worth following progress on fast math support in rust-lang/rust#21690, since LLVM may generate rsqrt when the corresponding code uses fdiv fast intrinsic, see https://github.com/llvm-mirror/llvm/blob/5e8f334dfad1ec4b8bcaae385d1c2598e18a03af/test/CodeGen/X86/sqrt-fastmath.ll#L245-L279

@andrewhickman
Copy link

This should probably be added to the Real trait at the same time

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

No branches or pull requests

4 participants