]> mj.ucw.cz Git - libucw.git/blob - ucw/hashfunc.h
tableprinter: code cleanup
[libucw.git] / ucw / 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 <ucw/lib.h>
15
16 #ifdef CONFIG_UCW_CLEAN_ABI
17 #define hash_block ucw_hash_block
18 #define hash_block_aligned ucw_hash_block_aligned
19 #define hash_string ucw_hash_string
20 #define hash_string_aligned ucw_hash_string_aligned
21 #define hash_string_nocase ucw_hash_string_nocase
22 #define str_len ucw_str_len
23 #define str_len_aligned ucw_str_len_aligned
24 #endif
25
26 /*** === String hashes [[strhash]] ***/
27
28 /* The following functions need str to be aligned to sizeof(uint).  */
29 uint str_len_aligned(const char *str) PURE; /** Get the string length (not a really useful hash function, but there is no better place for it). The string must be aligned to sizeof(uint). For unaligned see @str_len(). **/
30 uint hash_string_aligned(const char *str) PURE; /** Hash the string. The string must be aligned to sizeof(uint). For unaligned see @hash_string(). **/
31 uint hash_block_aligned(const byte *buf, uint len) PURE; /** Hash arbitrary data. They must be aligned to sizeof(uint). For unaligned see @hash_block(). **/
32
33 #ifdef CPU_ALLOW_UNALIGNED
34 #undef str_len
35 #undef hash_string
36 #undef hash_block
37 #define str_len(str)            str_len_aligned(str)
38 #define hash_string(str)        hash_string_aligned(str)
39 #define hash_block(str, len)    hash_block_aligned(str, len)
40 #else
41 uint str_len(const char *str) PURE; /** Get the string length. If you know it is aligned to sizeof(uint), you can use faster @str_len_aligned(). **/
42 uint hash_string(const char *str) PURE; /** Hash the string. If it is aligned to sizeof(uint), you can use faster @hash_string_aligned(). **/
43 uint hash_block(const byte *buf, uint len) PURE; /** Hash arbitrary data. If they are aligned to sizeof(uint), use faster @hash_block_aligned(). **/
44 #endif
45
46 uint hash_string_nocase(const char *str) PURE; /** Hash the string in a case insensitive way. Works only with ASCII characters. **/
47
48 /*** === Integer hashes [[inthash]] ***/
49
50 /***
51  * We hash integers by multiplying by a reasonably large prime with
52  * few ones in its binary form (to give the compiler the possibility
53  * of using shifts and adds on architectures where multiplication
54  * instructions are slow).
55  */
56 static inline uint CONST hash_u32(uint x) { return 0x01008041*x; } /** Hash a 32 bit unsigned integer. **/
57 static inline uint CONST hash_u64(u64 x) { return hash_u32((uint)x ^ (uint)(x >> 32)); } /** Hash a 64 bit unsigned integer. **/
58 static inline uint CONST hash_pointer(void *x) { return ((sizeof(x) <= 4) ? hash_u32((uint)(uintptr_t)x) : hash_u64((u64)(uintptr_t)x)); } /** Hash a pointer. **/
59
60 #endif