2 * Hyper-super-meta-alt-control-shift extra fast str_len() and hash_*()
5 * (c) 2002, Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #ifndef _SHERLOCK_HASHFUNC_H
12 #define _SHERLOCK_HASHFUNC_H
16 /* An equivalent of the Intel's rol instruction. */
17 #define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits))))
19 /* The following functions need str to be aligned to uns. */
20 uns str_len_aligned(const byte *str) CONST;
21 uns hash_string_aligned(const byte *str) CONST;
22 uns hash_block_aligned(const byte *str, uns len) CONST;
24 #ifdef CPU_ALLOW_UNALIGNED
25 #define str_len(str) str_len_aligned(str)
26 #define hash_string(str) hash_string_aligned(str)
27 #define hash_block(str, len) hash_block_aligned(str, len)
29 uns str_len(const byte *str) CONST;
30 uns hash_string(const byte *str) CONST;
31 uns hash_block(const byte *str, uns len) CONST;
34 uns hash_string_nocase(const byte *str) CONST;
37 * We hash integers by multiplying by a reasonably large prime with
38 * few ones in its binary form (to gave the compiler the possibility
39 * of using shifts and adds on architectures where multiplication
40 * instructions are slow).
42 static inline uns CONST hash_int(uns x) { return 0x01008041*x; }
43 static inline uns CONST hash_pointer(void *x) { return hash_int((uns)x); }