Skip to content

Instantly share code, notes, and snippets.

@amosr
Created November 19, 2015 05:45
Show Gist options
  • Save amosr/b11e183d8035d53181de to your computer and use it in GitHub Desktop.
Save amosr/b11e183d8035d53181de to your computer and use it in GitHub Desktop.
/*
attempt at writing a faster memcmp
*/
static iint_t INLINE memcmp8 (const char *as, const char* bs, iint_t len)
{
uint64_t *a = (uint64_t*)as;
uint64_t *b = (uint64_t*)bs;
while (len > 8) {
if (*a != *b) {
return 1;
}
a++;
b++;
len -= 8;
}
uint64_t rem = len;
uint64_t mask;
switch (rem) {
case 0: mask = 0x0000000000000000;
break;
case 1: mask = 0x00000000000000FF;
break;
case 2: mask = 0x000000000000FFFF;
break;
case 3: mask = 0x0000000000FFFFFF;
break;
case 4: mask = 0x00000000FFFFFFFF;
break;
case 5: mask = 0x000000FFFFFFFFFF;
break;
case 6: mask = 0x0000FFFFFFFFFFFF;
break;
case 7: mask = 0x00FFFFFFFFFFFFFF;
break;
}
//mask = ~(mask >> ((7-rem)*8));
//printf("%llx %llx %llx %lld\n", mask, *a & mask, *b & mask, len);
if (((*a) & mask) != ((*b) & mask)) {
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment