title: Fastest way to compute absolute value using SSE
body: <p>I am aware of 3 methods, but as far as I know, only the first 2 are generally used:</p>
  
  <ol>
  <li><p>Mask off the sign bit using <code>andps</code> or <code>andnotps</code>.</p>
  
  <ul>
  <li>Pros: One fast instruction if the mask is already in a register, which makes it perfect for doing this many times in a loop.</li>
  <li>Cons: The mask may not be in a register or worse, not even in a cache, causing a very long memory fetch.</li>
  </ul></li>
  <li><p>Subtract the value from zero to negate, and then get the max of the original and negated.</p>
  
  <ul>
  <li>Pros: Fixed cost because nothing is needed to fetch, like a mask.</li>
  <li>Cons: Will always be slower than the mask method if conditions are ideal, and we have to wait for the <code>subps</code> to complete before using the <code>maxps</code> instruction.</li>
  </ul></li>
  <li><p>Similar to option 2, subtract the original value from zero to negate, but then "bitwise and" the result with the original using <code>andps</code>. I ran a test comparing this to method 2, and it seems to behave identically to method 2, aside from when dealing with <code>NaN</code>s, in which case the result will be a different <code>NaN</code> than method 2's result.</p>
  
  <ul>
  <li>Pros: Should be slightly faster than method 2 because <code>andps</code> is usually faster than <code>maxps</code>.</li>
  <li>Cons: Can this result in any unintended behavior when <code>NaN</code>s are involved? Maybe not, because a <code>NaN</code> is still a <code>NaN</code>, even if it's a different value of <code>NaN</code>, right?</li>
  </ul></li>
  </ol>
  
  <p>Thoughts and opinions are welcome.</p>