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 /*** == String hashes [[strhash]] ***/
18 /* The following functions need str to be aligned to uns. */
19 uns str_len_aligned(const char *str) PURE; /** Get the string length (actually hash function too). The string must be aligned to uns. For unaligned see str_len(). **/
20 uns hash_string_aligned(const char *str) PURE; /** Hash the string. The string must be aligned to uns. For unaligned see hash_string(). **/
21 uns hash_block_aligned(const byte *str, uns len) PURE; /** Hash arbitrary data. They must be aligned to uns. For unaligned see hash_block(). **/
23 #ifdef CPU_ALLOW_UNALIGNED
24 #define str_len(str) str_len_aligned(str)
25 #define hash_string(str) hash_string_aligned(str)
26 #define hash_block(str, len) hash_block_aligned(str, len)
28 uns str_len(const char *str) PURE; /** Get the string length (actually a hash function too). If you know it is aligned to uns, you can use faster str_len_aligned(). **/
29 uns hash_string(const char *str) PURE; /** Hash the string. If it is aligned to uns, you can use faster hash_string_aligned(). **/
30 uns hash_block(const byte *str, uns len) PURE; /** Hash arbitrary data. If they are aligned to uns, use faster hash_block_aligned(). **/
33 uns hash_string_nocase(const char *str) PURE; /** Hash the string in a case insensitive way. **/
35 /*** == Integer hashes [[inthash]] ***/
38 * We hash integers by multiplying by a reasonably large prime with
39 * few ones in its binary form (to gave the compiler the possibility
40 * of using shifts and adds on architectures where multiplication
41 * instructions are slow).
43 static inline uns CONST hash_u32(uns x) { return 0x01008041*x; } /** Hash a 32 bit unsigned integer. **/
44 static inline uns CONST hash_u64(u64 x) { return hash_u32((uns)x ^ (uns)(x >> 32)); } /** Hash a 64 bit unsigned integer. **/
45 static inline uns CONST hash_pointer(void *x) { return ((sizeof(x) <= 4) ? hash_u32((uns)(uintptr_t)x) : hash_u64((u64)(uintptr_t)x)); } /** Hash a pointer. **/