From 339bb3c2bd40ffad3f3fff791c309d5c9192b784 Mon Sep 17 00:00:00 2001 From: Sebastian Hack Date: Thu, 16 Dec 2004 09:02:56 +0000 Subject: [PATCH] Added proper string hash function. [r4667] --- ir/adt/hashptr.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/ir/adt/hashptr.h b/ir/adt/hashptr.h index f61d53912..cc02b3497 100644 --- a/ir/adt/hashptr.h +++ b/ir/adt/hashptr.h @@ -12,10 +12,36 @@ #ifndef __HASHPTR_H__ #define __HASHPTR_H__ +#include "firm_config.h" + +#define _FIRM_FNV_OFFSET_BASIS 2166136261 +#define _FIRM_FNV_FNV_PRIME 16777619 + +static INLINE unsigned firm_fnv_hash(const unsigned char *data, unsigned bytes) +{ + unsigned i; + unsigned hash = _FIRM_FNV_OFFSET_BASIS; + + for(i = 0; i < bytes; ++i) { + hash *= _FIRM_FNV_FNV_PRIME; + hash ^= data[i]; + } + + return hash; +} + /** * hash a pointer value: Pointer addresses are mostly aligned to 4 * or 8 bytes. So we remove the lowest 3 bits */ -#define HASH_PTR(ptr) (((char *)ptr - (char *)0) >> 3) +#define HASH_PTR(ptr) (((char *) (ptr) - (char *)0) >> 3) + +/** + * Hash a string. + * @param str The string (can be const). + * @param len The length of the string. + * @return A hash value for the string. + */ +#define HASH_STR(str,len) firm_fnv_hash((const unsigned char *) (str), (len)) #endif /* __HASHPTR_H__ */ -- 2.20.1