title: Fastest way to determine if an integer is between two integers (inclusive) with known sets of values
body: <p>Is there a faster way than <code>x &gt;= start &amp;&amp; x &lt;= end</code> in C or C++ to test if an integer is between two integers?</p>
  
  <p><em>UPDATE</em>: My specific platform is iOS. This is part of a box blur function that restricts pixels to a circle in a given square.</p>
  
  <p><em>UPDATE</em>: After trying the <a href="https://stackoverflow.com/a/17095534/1165522">accepted answer</a>, I got an order of magnitude speedup on the one line of code over doing it the normal <code>x &gt;= start &amp;&amp; x &lt;= end</code> way.</p>
  
  <p><em>UPDATE</em>: Here is the after and before code with assembler from XCode:</p>
  
  <p><strong>NEW WAY</strong></p>
  
  <pre><code>// diff = (end - start) + 1
  #define POINT_IN_RANGE_AND_INCREMENT(p, range) ((p++ - range.start) &lt; range.diff)
  
  Ltmp1313:
   ldr    r0, [sp, #176] @ 4-byte Reload
   ldr    r1, [sp, #164] @ 4-byte Reload
   ldr    r0, [r0]
   ldr    r1, [r1]
   sub.w  r0, r9, r0
   cmp    r0, r1
   blo    LBB44_30
  </code></pre>
  
  <p><strong>OLD WAY</strong></p>
  
  <pre><code>#define POINT_IN_RANGE_AND_INCREMENT(p, range) (p &lt;= range.end &amp;&amp; p++ &gt;= range.start)
  
  Ltmp1301:
   ldr    r1, [sp, #172] @ 4-byte Reload
   ldr    r1, [r1]
   cmp    r0, r1
   bls    LBB44_32
   mov    r6, r0
   b      LBB44_33
  LBB44_32:
   ldr    r1, [sp, #188] @ 4-byte Reload
   adds   r6, r0, #1
  Ltmp1302:
   ldr    r1, [r1]
   cmp    r0, r1
   bhs    LBB44_36
  </code></pre>
  
  <p>Pretty amazing how reducing or eliminating branching can provide such a dramatic speed up.</p>