]> mj.ucw.cz Git - libucw.git/commitdiff
A better function for hashing integers (the old multiplier was completely
authorMartin Mares <mj@ucw.cz>
Sat, 15 Nov 2003 10:41:41 +0000 (10:41 +0000)
committerMartin Mares <mj@ucw.cz>
Sat, 15 Nov 2003 10:41:41 +0000 (10:41 +0000)
bogus as it didn't fit in a 32-bit integer) and also a new function
for hashing pointers.

lib/hashfunc.h

index 14133220faef80ef1b3d69dfbcdfacb3ca23aeef..cc799476a87938e6349e786c6b6f4d72de9c2df1 100644 (file)
@@ -33,6 +33,13 @@ uns hash_block(const byte *str, uns len) CONST;
 
 uns hash_string_nocase(const byte *str) CONST;
 
-static inline uns CONST hash_int(uns x) { return 6442450967*x; }
+/*
+ *  We hash integers by multiplying by a reasonably large prime with
+ *  few ones in its binary form (to gave the compiler the possibility
+ *  of using shifts and adds on architectures where multiplication
+ *  instructions are slow).
+ */
+static inline uns CONST hash_int(uns x) { return 0x01008041*x; }
+static inline uns CONST hash_pointer(void *x) { return hash_int((uns)x); }
 
 #endif