]> mj.ucw.cz Git - libucw.git/blobdiff - lib/hashfunc.h
Use $(COPT2) instead of -O6.
[libucw.git] / lib / hashfunc.h
index 14133220faef80ef1b3d69dfbcdfacb3ca23aeef..c10f224910d9ba563ef401adc45ae57db011912a 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *     Hyper-super-meta-alt-control-shift extra fast str_len() and hash_*()
- *     routines
+ *     UCW Library -- Hyper-super-meta-alt-control-shift extra fast
+ *     str_len() and hash_*() routines
  *
  *     (c) 2002, Robert Spalek <robert@ucw.cz>
  *
@@ -8,8 +8,8 @@
  *     of the GNU Lesser General Public License.
  */
 
-#ifndef _SHERLOCK_HASHFUNC_H
-#define _SHERLOCK_HASHFUNC_H
+#ifndef _UCW_HASHFUNC_H
+#define _UCW_HASHFUNC_H
 
 #include "lib/lib.h"
 
@@ -33,6 +33,14 @@ 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_u32(uns x) { return 0x01008041*x; }
+static inline uns CONST hash_u64(u64 x) { return hash_u32((uns)x ^ (uns)(x >> 32)); }
+static inline uns CONST hash_pointer(void *x) { return ((sizeof(x) <= 4) ? hash_u32((uns)(addr_int_t)x) : hash_u64((u64)(addr_int_t)x)); }
 
 #endif