From 474ac0a4e73366d6f88daf75ea7af53244d62756 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 25 Nov 2014 15:26:45 -0800 Subject: Add a variant of the hash funciton for arbitrary data. Mesa was using this for hashing various key structures. --- fnv_hash.c | 15 +++++++++++++++ fnv_hash.h | 1 + 2 files changed, 16 insertions(+) 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 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); -- cgit v1.2.3