Skip to content

Commit

Permalink
hash: Improve hash function for integers.
Browse files Browse the repository at this point in the history
As previously defined, the following both returned the same value for
given values of 'basis':
	hash_int(0, hash_int(1, basis))
	hash_int(1, hash_int(0, basis))
because hash_int(0, basis) evaluated to basis and hash_int(1, basis)
evaluated to c + basis for some constant c.

This commit fixes the problem, by eliminating any simple linear
relationship between basis and the hash value.

We should write some tests for hash function quality.
  • Loading branch information
blp committed Nov 4, 2009
1 parent 8e54211 commit 44528c5
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ static inline uint32_t hash_int(uint32_t x, uint32_t basis)
x ^= x >> 17;
x -= x << 9;
x ^= x << 4;
x += basis;
x -= x << 3;
x ^= x << 10;
x ^= x >> 15;
return x + basis;
return x;
}

/* An attempt at a useful 1-bit hash function. Has not been analyzed for
Expand Down

0 comments on commit 44528c5

Please sign in to comment.