From a9bde809a9c13fb4ccf59a0309e5733c84436085 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 29 Mar 2009 22:27:07 -0400 Subject: [PATCH] Put in a much better string hashing algorithm. --- mojoshader_preprocessor.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mojoshader_preprocessor.c b/mojoshader_preprocessor.c index a96857fa..e410ad5f 100644 --- a/mojoshader_preprocessor.c +++ b/mojoshader_preprocessor.c @@ -383,19 +383,25 @@ IMPLEMENT_POOL(Define, define) // Preprocessor define hashtable stuff... -static unsigned char hash_define(const char *sym) +// this is djb's xor hashing function. +static inline uint32 hash_string_djbxor(const char *sym) { - unsigned char retval = 0; + register uint32 hash = 5381; while (*sym) - retval += *(sym++); - return retval; + hash = ((hash << 5) + hash) ^ *(sym++); + return hash; +} // hash_string_djbxor + +static inline uint8 hash_define(const char *sym) +{ + return (uint8) hash_string_djbxor(sym); } // hash_define static int add_define(Context *ctx, const char *sym, const char *val, char **parameters, unsigned int paramcount) { - const unsigned char hash = hash_define(sym); + const uint8 hash = hash_define(sym); Define *bucket = ctx->define_hashtable[hash]; while (bucket) { @@ -435,7 +441,7 @@ static void free_define(Context *ctx, Define *def) static int remove_define(Context *ctx, const char *sym) { - const unsigned char hash = hash_define(sym); + const uint8 hash = hash_define(sym); Define *bucket = ctx->define_hashtable[hash]; Define *prev = NULL; while (bucket) @@ -459,7 +465,7 @@ static int remove_define(Context *ctx, const char *sym) static const Define *find_define(Context *ctx, const char *sym) { - const unsigned char hash = hash_define(sym); + const uint8 hash = hash_define(sym); Define *bucket = ctx->define_hashtable[hash]; while (bucket) {