]> mj.ucw.cz Git - libucw.git/blob - lib/hashfunc.h
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / hashfunc.h
1 /*
2  *      UCW Library -- Hyper-super-meta-alt-control-shift extra fast
3  *      str_len() and hash_*() routines
4  *
5  *      (c) 2002, Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_HASHFUNC_H
12 #define _UCW_HASHFUNC_H
13
14 #include "lib/lib.h"
15
16 /* The following functions need str to be aligned to uns.  */
17 uns str_len_aligned(const byte *str) PURE;
18 uns hash_string_aligned(const byte *str) PURE;
19 uns hash_block_aligned(const byte *str, uns len) PURE;
20
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)
25 #else
26 uns str_len(const byte *str) PURE;
27 uns hash_string(const byte *str) PURE;
28 uns hash_block(const byte *str, uns len) PURE;
29 #endif
30
31 uns hash_string_nocase(const byte *str) PURE;
32
33 /*
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).
38  */
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)); }
42
43 #endif