title: Truncating to Word type
body: <p>The following code truncates a number of type <code>Double</code> to one in the type <code>Word16</code> (although I suspect any other word type behaves similarly, I had to choose one for the example).</p>
  
  <pre><code>truncate1 :: Double -&gt; Word16
  truncate1 = fromIntegral . (truncate :: Double -&gt; Int)
  </code></pre>
  
  <p>As you can read, I first truncate it to <code>Int</code> and only then I cast it to <code>Word16</code>. I benchmarked this function agains a direct truncation:</p>
  
  <pre><code>truncate2 :: Double -&gt; Word16
  truncate2 = truncate
  </code></pre>
  
  <p>Surprisingly to me, the first version (going thru the <code>Int</code> type first) performed much better. Or the second much worse. According to the criterion output:</p>
  
  <pre><code>benchmarking truncate/truncate1
  mean: 25.42399 ns, lb -47.40484 ps, ub 67.87578 ns, ci 0.950
  std dev: 145.5661 ns, lb 84.90195 ns, ub 244.2057 ns, ci 0.950
  found 197 outliers among 100 samples (197.0%)
    97 (97.0%) low severe
    100 (100.0%) high severe
  variance introduced by outliers: 99.000%
  variance is severely inflated by outliers
  
  benchmarking truncate/truncate2
  mean: 781.0604 ns, lb 509.3264 ns, ub 1.086767 us, ci 0.950
  std dev: 1.436660 us, lb 1.218997 us, ub 1.592479 us, ci 0.950
  found 177 outliers among 100 samples (177.0%)
    77 (77.0%) low severe
    100 (100.0%) high severe
  variance introduced by outliers: 98.995%
  variance is severely inflated by outliers
  </code></pre>
  
  <p>To be honest, I just started using Criterion, so I'm not an expert using it, but I understand that <code>25.42399 ns</code> is a shorter execution time than <code>781.0604 ns</code>. I suspect that some specialization is playing a role here. Is it <code>truncate2</code> too slow? Being the case, can <code>truncate</code> be improved? Furthermore, anybody knows an even faster way? I feel like doing something wrong casting to a type I don't really use.</p>
  
  <p>Thanks in advance.</p>
  
  <p>I am compiling with GHC-7.4.2, optimizations enabled (<code>-O2</code>).</p>