hash: Improve hash function for integers.
authorBen Pfaff <blp@nicira.com>
Thu, 22 Oct 2009 19:58:41 +0000 (12:58 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 4 Nov 2009 23:01:02 +0000 (15:01 -0800)
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.

lib/hash.h

index 33c5c42..3f14038 100644 (file)
@@ -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