SHARE
TWEET

Untitled

a guest Feb 16th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::env::args;
  2. use std::mem::transmute;
  3.  
  4. fn main() {
  5.     let x: f32 = args().skip(1).next().unwrap().parse().unwrap();
  6.     let t = old_inv_sqrt(x);
  7.     let h = inv_sqrt(x);
  8.     let s = sqrt(x);
  9.  
  10.     println!("{}\n{}\n{}", t, h, s);
  11. }
  12.  
  13. fn old_inv_sqrt(x: f32) -> f32 {
  14.     let y: u32 = x.to_bits();
  15.     let half: f32 = x * 0.5;
  16.  
  17.     let z: u64 = y as u64;
  18.     let f: i64 = unsafe { transmute(z) };
  19.  
  20.     let r: i64 = 0x5f3759df - (f >> 1);
  21.     let b: u64 = unsafe { transmute(r) };
  22.     let b = b as u32;
  23.  
  24.  
  25.     let a = f32::from_bits(b);
  26.  
  27.     a * (1.5 - (half * a * a))
  28. }
  29.  
  30. fn inv_sqrt(x: f32) -> f32 {
  31.     let i = x.to_bits();
  32.     let s = 0x5f3759df - (i >> 1);
  33.     let r = f32::from_bits(s);
  34.  
  35.     r * (1.5 - (x/2.0 * r * r))
  36. }
  37.  
  38. fn sqrt(x: f32) -> f32 {
  39.     let r = inv_sqrt(x);
  40.     r * x
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top