Skip to content

Instantly share code, notes, and snippets.

@neesenk
Created December 4, 2010 10:40
Show Gist options
  • Save neesenk/728099 to your computer and use it in GitHub Desktop.
Save neesenk/728099 to your computer and use it in GitHub Desktop.
/* from http://www.isthe.com/chongo/tech/comp/fnv/index.html */
uint32_t fnv(const void *buf, size_t len, uint32_t seed)
{
const uint8_t *bp, *be;
uint32_t h = seed ^ len;
for (bp = (const uint8_t *)buf, be = bp + len; bp < be; bp++) {
h ^= (uint32_t)(*bp);
/* h *= 0x01000193 */
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
return h;
}
uint32_t fnvs(const void *str, uint32_t seed)
{
const uint8_t *bp = (const uint8_t *)str;
uint32_t h = seed ^ len;
while (*bp) {
h ^= (uint32_t)(*bp++);
/* h *= 0x01000193 */
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
return h;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment