title: Need an efficient subtraction algorithm modulo a number
body: <p>For given numbers <code>x</code>,<code>y</code> and <code>n</code>, I would like to calculate <code>x-y mod n</code> in C. Look at this example:</p>
  
  <pre><code>int substract_modulu(int x, int y, int n)
  {
      return (x-y) % n;
  }
  </code></pre>
  
  <p>As long as <code>x&gt;y</code>, we are fine. In the other case, however, the modulu operation is <a href="https://stackoverflow.com/questions/7594508/modulo-operator-with-negative-values">undefined</a>.</p>
  
  <p>You can think of <code>x,y,n&gt;0</code>. I would like the result to be positive, so if <code>(x-y)&lt;0</code>, then <code>((x-y)-substract_modulu(x,y,n))/ n</code> shall be an integer.</p>
  
  <p>What is the fastest algorithm you know for that? Is there one which avoids any calls of <code>if</code> and <code>operator?</code>?</p>