Skip to content

Instantly share code, notes, and snippets.

@lojikil
Created March 23, 2013 14:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lojikil/5228011 to your computer and use it in GitHub Desktop.
Save lojikil/5228011 to your computer and use it in GitHub Desktop.
Fowler-Nolls-Vo hash implementations
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
uint64_t
fnv(char *key, uint32_t len)
{
uint64_t hash = 14695981039346656037;
uint32_t idx = 0;
for(; idx < len; idx++)
{
hash *= 1099511628211;
hash ^= key[idx];
}
return hash;
}
uint64_t
fnv1a(char *key, uint32_t len)
{
uint64_t hash = 14695981039346656037;
uint32_t idx = 0;
for(; idx < len; idx++)
{
hash ^= key[idx];
hash *= 1099511628211;
}
return hash;
}
uint32_t
ul_strlen(char *s)
{
uint32_t i = 0;
while(s[i] != '\0') i++;
return i;
}
int
main(int ac, char **al)
{
if(ac != 2)
{
printf("usage: fnv <key>\n");
return 1;
}
printf("%llx\n", fnv(al[1], ul_strlen(al[1])));
printf("%llx\n", fnv1a(al[1], ul_strlen(al[1])));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment