title: Most efficient way to find the greatest number not greater than A, which is divisible by B
body: <p>I have 2 numbers A and B. I want to find <code>C = A - (A % B)</code>, but there are some problems. First of all, if <code>C</code> and <code>D = A / B</code> should have the same parity ((even and even) or (odd and odd)), otherwise C should be incremented (<code>++C</code>). The second problem is that I constantly do this calculation, so I want the cost of it to be as small as possible. Right now my solution looks like this:</p>
  
  <pre><code>uint32_t D = A / B;
  C = D * B;
  if ((C ^ D) &amp; 0x1) ++C;
  </code></pre>
  
  <p>Is there a better way to do this? Maybe <code>(C % 2) != (D % 2)</code> is faster because of compiler optimizations, but I can't proof it. I would also like to know if it can be done with some specific intel functions (registers).</p>