title: What's the most efficient way to compare two blocks of memory in the D language?
body: <p>I need a comparison function for blocks of memory for doing binary searches on arrays of bytes in the D programming language.  It does not need to have any useful semantics.  It only needs to be fast and be a valid comparison function (one that produces a total ordering).  The blocks of memory to be compared are already known to be the same length.</p>
  
  <p>C's <code>memcmp</code> is actually pretty slow because it tries to preserve useful string comparison semantics, which I don't need.  Below is the best I've come up with so far.  Does anyone know of anything better, preferably w/o dipping into non-portable CPU-specific instructions?</p>
  
  <pre><code>// Faster than C's memcmp because it doesn't preserve any meaningful
  // semantics.  It's just a completely arbitrary, but really fast,
  // comparison function.
  int memoryCompare(const(void)* lhs, const(void)* rhs, size_t n) {
      for(; n &gt;= uint.sizeof; n -= uint.sizeof) {
          if( *(cast(uint*) lhs) &lt; *(cast(uint*) rhs)) {
              return -1;
          } else if( *(cast(uint*) lhs) &gt; *(cast(uint*) rhs)) {
              return 1;
          }
          lhs += uint.sizeof;
          rhs += uint.sizeof;
      }
  
      for(; n &gt;= ubyte.sizeof; n -= ubyte.sizeof) {
          if( *(cast(ubyte*) lhs) &lt; *(cast(ubyte*) rhs)) {
              return -1;
          } else if( *(cast(ubyte*) lhs) &gt; *(cast(ubyte*) rhs)) {
              return 1;
          }
          lhs += ubyte.sizeof;
          rhs += ubyte.sizeof;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Edit:  I've read up on SSE and I don't want to use it for 3 reasons:</p>
  
  <ol>
  <li>It's not portable.</li>
  <li>It requires programming in ASM.</li>
  <li>The comparison instructions assume your data is floating points, which might be problematic if some data happens to match the pattern for a NaN.</li>
  </ol>