title: Quickly find whether a value is present in a C array?
body: <p>I have an embedded application with a time-critical ISR that needs to iterate through an array of size 256 (preferably 1024, but 256 is the minimum) and check if a value matches the arrays contents. A <code>bool</code> will be set to true is this is the case.</p>
  
  <p>The microcontroller is an NXP LPC4357, ARM Cortex M4 core, and the compiler is GCC. I already have combined optimisation level 2 (3 is slower) and placing the function in RAM instead of flash. I also use pointer arithmetic and a <code>for</code> loop, which does down-counting instead of up (checking if <code>i!=0</code> is faster than checking if <code>i&lt;256</code>). All in all, I end up with a duration of 12.5&nbsp;µs which has to be reduced drastically to be feasible. This is the (pseudo) code I use now:</p>
  
  <pre><code>uint32_t i;
  uint32_t *array_ptr = &amp;theArray[0];
  uint32_t compareVal = 0x1234ABCD;
  bool validFlag = false;
  
  for (i=256; i!=0; i--)
  {
      if (compareVal == *array_ptr++)
      {
           validFlag = true;
           break;
       }
  }
  </code></pre>
  
  <p>What would be the absolute fastest way to do this? Using inline assembly is allowed. Other 'less elegant' tricks are also allowed.</p>