diff options
author | Eric Anholt <eric@anholt.net> | 2014-11-25 15:26:45 -0800 |
---|---|---|
committer | Eric Anholt <eric@anholt.net> | 2014-11-25 15:27:09 -0800 |
commit | 474ac0a4e73366d6f88daf75ea7af53244d62756 (patch) | |
tree | 97165f96d072f1e8f831711b789ad5be6bd85890 | |
parent | 2f56d34cbe834ebacb89a9551daf4b3ba78fc7a8 (diff) |
Add a variant of the hash funciton for arbitrary data.
Mesa was using this for hashing various key structures.
-rw-r--r-- | fnv_hash.c | 15 | ||||
-rw-r--r-- | fnv_hash.h | 1 |
2 files changed, 16 insertions, 0 deletions
@@ -50,6 +50,21 @@ fnv1_hash_string(const char *key) return hash; } +uint32_t +fnv1_hash_data(const void *data, size_t size) +{ + uint32_t hash = 2166136261ul; + const uint8_t *bytes = (uint8_t *)data; + + while (size-- != 0) { + hash ^= *bytes; + hash = hash * 0x01000193; + bytes++; + } + + return hash; +} + int string_key_equals(const void *a, const void *b) { @@ -31,4 +31,5 @@ #include <inttypes.h> uint32_t fnv1_hash_string(const char *key); +uint32_t fnv1_hash_data(const void *data, size_t size); int string_key_equals(const void *a, const void *b); |