util/hash_table: Pull the details of the FNV-1a into helpers

This way the basics of the FNV-1a hash can be reused to easily create other
hashing functions.

Reviewed-by: Eric Anholt <eric@anholt.net>
This commit is contained in:
Jason Ekstrand
2015-01-05 22:31:23 -08:00
parent e4115ca9d8
commit a3b73ccf6d
2 changed files with 23 additions and 13 deletions
+4 -13
View File
@@ -431,27 +431,18 @@ _mesa_hash_table_random_entry(struct hash_table *ht,
uint32_t
_mesa_hash_data(const void *data, size_t size)
{
uint32_t hash = 2166136261ul;
const uint8_t *bytes = data;
while (size-- != 0) {
hash ^= *bytes;
hash = hash * 0x01000193;
bytes++;
}
return hash;
return _mesa_fnv32_1a_accumulate_block(_mesa_fnv32_1a_offset_bias,
data, size);
}
/** FNV-1a string hash implementation */
uint32_t
_mesa_hash_string(const char *key)
{
uint32_t hash = 2166136261ul;
uint32_t hash = _mesa_fnv32_1a_offset_bias;
while (*key != 0) {
hash ^= *key;
hash = hash * 0x01000193;
hash = _mesa_fnv32_1a_accumulate(hash, *key);
key++;
}
+19
View File
@@ -101,6 +101,25 @@ static inline uint32_t _mesa_hash_pointer(const void *pointer)
return _mesa_hash_data(&pointer, sizeof(pointer));
}
static const uint32_t _mesa_fnv32_1a_offset_bias = 2166136261u;
static inline uint32_t
_mesa_fnv32_1a_accumulate_block(uint32_t hash, const void *data, size_t size)
{
const uint8_t *bytes = (const uint8_t *)data;
while (size-- != 0) {
hash ^= *bytes;
hash = hash * 0x01000193;
bytes++;
}
return hash;
}
#define _mesa_fnv32_1a_accumulate(hash, expr) \
_mesa_fnv32_1a_accumulate_block(hash, &(expr), sizeof(expr))
/**
* This foreach function is safe against deletion (which just replaces
* an entry's data with the deleted marker), but not against insertion