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

fast_rsqrt for f32 and f64 types #13718

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,23 @@ impl Float for f32 {
#[inline]
fn rsqrt(self) -> f32 { self.sqrt().recip() }

/// The reciprocal of the square root of a number, quickly using
/// Newton's method to approximate the value.
/// Originally seen in Quake arena
fn fast_rsqrt(self) -> f32 {
let mut i: i32;
let mut x2: f32;
let mut y: f32;

y = self;
x2 = y * 0.5;
i = unsafe{cast::transmute::<f32, i32>(y)}; // stark lack of evil floating point bit
i = 0x5f3759df - ( i >> 1 ); // level hacking
y = unsafe{cast::transmute::<i32, f32>(i)};
y = y * (1.5 - (x2 * y * y));
y
}

#[inline]
fn cbrt(self) -> f32 {
unsafe { cmath::cbrtf(self) }
Expand Down
22 changes: 22 additions & 0 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,28 @@ impl Float for f64 {
#[inline]
fn rsqrt(self) -> f64 { self.sqrt().recip() }

/// The reciprocal of the square root of a number, quickly using
/// Newton's method to approximate the value. For f64 values,
/// the calculations are still done with 32 bit precision due to
/// the math not working the same way in higher bit types. If you're
/// calculating the "fast" inverse square root, however, this should
/// not be a major problem
/// Originally seen in Quake arena
fn fast_rsqrt(self) -> f64 {
let mut i: i32;
let mut x2: f32;
let mut y: f32;

y = self as f32;
x2 = y * 0.5;
i = unsafe{cast::transmute::<f32, i32>(y)}; // stark lack of evil floating point bit
i = 0x5f3759df - ( i >> 1 ); // level hacking
y = unsafe{cast::transmute::<i32, f32>(i)};
y = y * (1.5 - (x2 * y * y));
y as f64
}


#[inline]
fn cbrt(self) -> f64 {
unsafe { cmath::cbrt(self) }
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ pub trait Float: Signed + Primitive {
fn sqrt(self) -> Self;
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
fn rsqrt(self) -> Self;
/// Take the fast reciprocal (inverse) square root of a number, `1/sqrt(x)`
fn fast_rsqrt(self) -> Self;
/// Take the cubic root of a number.
fn cbrt(self) -> Self;
/// Calculate the length of the hypotenuse of a right-angle triangle given
Expand Down