Created
March 23, 2013 14:58
-
-
Save lojikil/5228011 to your computer and use it in GitHub Desktop.
Fowler-Nolls-Vo hash implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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