scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 0f7ab686171677c9d8e7e6d6c5c518038c8e25cb
parent b206061137fe1f86fd9669679e055bc826816f99
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 26 May 2015 08:16:00 +0200

Improve hash function

This is the hash function found in k&r second edition
mixed with djb2 because it uses xor instead of adding.
This new version gets a better distribution of the
symbols in the hash table.

Diffstat:
Mcc1/symbol.c | 10+++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -7,7 +7,7 @@ #include "../inc/sizes.h" #include "cc1.h" -#define NR_SYM_HASH 32 +#define NR_SYM_HASH 64 static unsigned curctx; static short localcnt; @@ -19,11 +19,11 @@ static Symbol *htab[NR_SYM_HASH]; static inline unsigned hash(const char *s) { - unsigned h, ch; + unsigned c, h; - for (h = 0; ch = *s++; h += ch) - /* nothing */; - return h & NR_SYM_HASH - 1; + for (h = 0; c = *s; ++s) + h ^= 33 * c; + return h & NR_SYM_HASH-1; } void