title: Speeding up a bit test operation by using assembly
body: <p>I have a performance problem - I can't beat the release version speed of the compiler generated code at all. It is 25% slower. The function I wrote is being called around 20 million times in my test, so making it run faster will pay off.</p>
  
  <p>the code in C++ is pretty simple:</p>
  
  <pre><code>static inline char GetBit(char *data, size_t bit)
  { 
      return 0 != (data[bit / 8] &amp; (1 &lt;&lt; (bit % 8))); 
  }
  </code></pre>
  
  <p>And this is the version I wrote for 64bit MASM:</p>
  
  <pre><code>mov   rax, rdx  
  mov   r10, 8h
  xor   rdx, rdx  
  div   rax, r10  
  mov   al, byte ptr [rax+rcx]  
  mov   bl, 1h
  mov   cl, dl  
  shl   bl, cl  
  and   al, bl  
  shr   al, cl  
  ret 
  </code></pre>
  
  <p>Well I'm not much of an assembler guy, but I don't think the compiler can make 25% faster code just creating better assembly. So the trick is [probably] in the function call. It respects the inline keyword for the C++ code and generates no call, but I just can't make it working for the asm code:</p>
  
  <pre><code>extern "C" inline char GetBitAsm(char *data, size_t bit);
  </code></pre>
  
  <p>I've disassembled the code using <strong>dumpbin</strong> and I can clearly see my code + function call. while no call is generated for the compiler's version:</p>
  
  <pre><code>mov   rdx, qword ptr [bit]  
  mov   rcx, qword ptr [data]  
  call  GetBitAsm (013F588EFDh)  
  mov   byte ptr [isbit], al  
  </code></pre>
  
  <p>There are additionally 2 readings and one writing to the memory, while in what the compiler generates, there are probably only 1 reading. I read somewhere that <strong>div</strong> op-code takes around 20 cycles, while single memory access costs 100 cycles at least. So removing mov rdx and mov rcx from the memory, replacing them with values from registers in the parent function will, I think, do the trick</p>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>Is that really the reason it runs so slow ?</p></li>
  <li><p>How to make function written in asm inline in release version ?</p></li>
  <li><p>How can I further enhance my assembly code, to make it even faster ?</p></li>
  </ol>