Skip to content

Instantly share code, notes, and snippets.

@Themaister
Created February 22, 2014 11:46
Show Gist options
  • Save Themaister/9152665 to your computer and use it in GitHub Desktop.
Save Themaister/9152665 to your computer and use it in GitHub Desktop.
#if __SSE2__
#include <emmintrin.h>
// There's no equivalent in libc, you'd think so ... std::mismatch exists, but it's not optimized at all. :(
static unsigned find_mismatch(const uint32_t *a, const uint32_t *b, unsigned samples)
{
unsigned i;
unsigned sse_samples = samples & ~3;
for (i = 0; i < sse_samples; i += 4)
{
__m128i v0 = _mm_loadu_si128((const __m128i*)(a + i));
__m128i v1 = _mm_loadu_si128((const __m128i*)(b + i));
__m128i c = _mm_cmpeq_epi32(v0, v1);
uint32_t mask = _mm_movemask_epi8(c);
if (mask != 0xffff) // Something has changed, figure out where.
return i + (__builtin_ctz(~mask) >> 2);
}
for (; i < samples; i++)
if (a[i] != b[i])
return i;
return samples;
}
#else
static unsigned find_mismatch(const uint32_t *a, const uint32_t *b, unsigned samples)
{
unsigned i;
for (i = 0; i < samples; i++)
if (a[i] != b[i])
return i;
return samples;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment