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 char *str) PURE;
18 uns hash_string_aligned(const char *str) PURE;
19 uns hash_block_aligned(const byte *str, uns len) PURE;
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 char *str) PURE;
27 uns hash_string(const char *str) PURE;
28 uns hash_block(const byte *str, uns len) PURE;
31 uns hash_string_nocase(const char *str) PURE;
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)(uintptr_t)x) : hash_u64((u64)(uintptr_t)x)); }