title: The fastest way to detect if a double is finite?
body: <p>What is the fastest way to detect whether a <code>double</code> value is a finite value (neither NaN nor positive/negative infinity) in IL without throwing an exception?</p>
  
  <p>I was considering the following approaches (c# notation for reader's convenience only, in my project I'm using IL for this):</p>
  
  <ol>
  <li><p><code>!double.IsNaN(x) &amp;&amp; !double.IsInfinity(x)</code> - the most obvious and, probably, the slowest because 2 method calls are involved. </p></li>
  <li><p><code>(*(((long*) &amp;x)) &amp; 0x7fffffffffffffffL) &lt; 0x7ff0000000000000L</code> </p></li>
  </ol>
  
  <p>or in IL:</p>
  
  <pre><code>  ldloca x
    conv.u 
    ldind.i8 
    ldc.i8 0x7fffffffffffffff
    and 
    ldc.i8 0x7ff0000000000000
    clt 
  </code></pre>
  
  <p>My questions about the second approach are:</p>
  
  <ol>
  <li><p>According to my research, this should precisely determine whether any given <code>x</code> is finite. Is this true?</p></li>
  <li><p>Is it the best possible way (performance-wise) to solve the task in IL, or is there a better (faster) solution?</p></li>
  </ol>
  
  <p>P.S. I do appreciate recommendations to run my own benchmarks and find out, and will most certainly do this. Just thought maybe someone already had similar problem and knows the answer.
  P.P.S. Yes, I realize that we are talking abot nanoseconds here, and yes, they are important for my particular case</p>