title: Is it possible to write Quake's fast InvSqrt() function in Rust?
body: <p>This is just to satisfy my own curiosity.</p>
  
  <p>Is there an implementation of this:</p>
  
  <pre class="lang-c prettyprint-override"><code>float InvSqrt (float x)
  {
     float xhalf = 0.5f*x;
     int i = *(int*)&amp;x;
     i = 0x5f3759df - (i&gt;&gt;1);
     x = *(float*)&amp;i;
     x = x*(1.5f - xhalf*x*x);
     return x;
  }
  </code></pre>
  
  <p>in Rust? If it exists, post the code.</p>
  
  <p>I tried it and failed. I don't know how to encode the float number using integer format. Here is my attempt:</p>
  
  <pre><code>fn main() {
      println!("Hello, world!");
      println!("sqrt1: {}, ",sqrt2(100f64));
  }
  
  fn sqrt1(x: f64) -&gt; f64 {
      x.sqrt()
  }
  
  fn sqrt2(x: f64) -&gt; f64 {
      let mut x = x;
      let xhalf = 0.5*x;
      let mut i = x as i64;
      println!("sqrt1: {}, ", i);
  
      i = 0x5f375a86 as i64 - (i&gt;&gt;1);
  
      x = i as f64;
      x = x*(1.5f64 - xhalf*x*x);
      1.0/x
  }
  </code></pre>
  
  <p>Reference:<br>
  1. <a href="https://www.beyond3d.com/content/articles/8/" rel="noreferrer">Origin of Quake3's Fast InvSqrt() - Page 1</a><br>
  2. <a href="https://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/" rel="noreferrer">Understanding Quake’s Fast Inverse Square Root</a><br>
  3. <a href="http://www.lomont.org/papers/2003/InvSqrt.pdf" rel="noreferrer">FAST INVERSE SQUARE ROOT.pdf</a><br>
  4. <a href="https://github.com/id-Software/Quake-III-Arena/blob/master/code/game/q_math.c#L552-L572" rel="noreferrer">source code: q_math.c#L552-L572</a>  </p>