2 * UCW Library -- Hyper-super-meta-alt-control-shift extra fast
3 * str_len() and hash_*() routines
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 _UCW_HASHFUNC_H
12 #define _UCW_HASHFUNC_H
16 /* The following functions need str to be aligned to uns. */
17 uns str_len_aligned(const byte *str) CONST;
18 uns hash_string_aligned(const byte *str) CONST;
19 uns hash_block_aligned(const byte *str, uns len) CONST;
21 #ifdef CPU_ALLOW_UNALIGNED
22 #define str_len(str) str_len_aligned(str)
23 #define hash_string(str) hash_string_aligned(str)
24 #define hash_block(str, len) hash_block_aligned(str, len)
26 uns str_len(const byte *str) CONST;
27 uns hash_string(const byte *str) CONST;
28 uns hash_block(const byte *str, uns len) CONST;
31 uns hash_string_nocase(const byte *str) CONST;
34 * We hash integers by multiplying by a reasonably large prime with
35 * few ones in its binary form (to gave the compiler the possibility
36 * of using shifts and adds on architectures where multiplication
37 * instructions are slow).
39 static inline uns CONST hash_u32(uns x) { return 0x01008041*x; }
40 static inline uns CONST hash_u64(u64 x) { return hash_u32((uns)x ^ (uns)(x >> 32)); }
41 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)); }