summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Anholt <eric@anholt.net>2014-11-25 15:26:45 -0800
committerEric Anholt <eric@anholt.net>2014-11-25 15:27:09 -0800
commit474ac0a4e73366d6f88daf75ea7af53244d62756 (patch)
tree97165f96d072f1e8f831711b789ad5be6bd85890
parent2f56d34cbe834ebacb89a9551daf4b3ba78fc7a8 (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.c15
-rw-r--r--fnv_hash.h1
2 files changed, 16 insertions, 0 deletions
diff --git a/fnv_hash.c b/fnv_hash.c
index 870dc0d..99801ce 100644
--- a/fnv_hash.c
+++ b/fnv_hash.c
@@ -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)
{
diff --git a/fnv_hash.h b/fnv_hash.h
index 12ced2d..eb98205 100644
--- a/fnv_hash.h
+++ b/fnv_hash.h
@@ -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);