X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ucw%2Fhashfunc.h;h=e3cfe4d845b2462bbab90daceb571c607609cb95;hb=17b1bcca21624757189a619506e93b42f4706b77;hp=49572882dd8255bdd520808ff4e759ad58639988;hpb=031256ad2e123eec58521f8e3eb9496c197641d2;p=libucw.git diff --git a/ucw/hashfunc.h b/ucw/hashfunc.h index 49572882..e3cfe4d8 100644 --- a/ucw/hashfunc.h +++ b/ucw/hashfunc.h @@ -11,33 +11,37 @@ #ifndef _UCW_HASHFUNC_H #define _UCW_HASHFUNC_H -#include "ucw/lib.h" +#include -/* The following functions need str to be aligned to uns. */ -uns str_len_aligned(const char *str) PURE; -uns hash_string_aligned(const char *str) PURE; -uns hash_block_aligned(const byte *str, uns len) PURE; +/*** === String hashes [[strhash]] ***/ + +/* The following functions need str to be aligned to sizeof(uns). */ +uns 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(uns). For unaligned see @str_len(). **/ +uns hash_string_aligned(const char *str) PURE; /** Hash the string. The string must be aligned to sizeof(uns). For unaligned see @hash_string(). **/ +uns hash_block_aligned(const byte *buf, uns len) PURE; /** Hash arbitrary data. They must be aligned to sizeof(uns). For unaligned see @hash_block(). **/ #ifdef CPU_ALLOW_UNALIGNED #define str_len(str) str_len_aligned(str) #define hash_string(str) hash_string_aligned(str) #define hash_block(str, len) hash_block_aligned(str, len) #else -uns str_len(const char *str) PURE; -uns hash_string(const char *str) PURE; -uns hash_block(const byte *str, uns len) PURE; +uns str_len(const char *str) PURE; /** Get the string length. If you know it is aligned to sizeof(uns), you can use faster @str_len_aligned(). **/ +uns hash_string(const char *str) PURE; /** Hash the string. If it is aligned to sizeof(uns), you can use faster @hash_string_aligned(). **/ +uns hash_block(const byte *buf, uns len) PURE; /** Hash arbitrary data. If they are aligned to sizeof(uns), use faster @hash_block_aligned(). **/ #endif -uns hash_string_nocase(const char *str) PURE; +uns hash_string_nocase(const char *str) PURE; /** Hash the string in a case insensitive way. Works only with ASCII characters. **/ -/* - * We hash integers by multiplying by a reasonably large prime with - * few ones in its binary form (to gave the compiler the possibility - * of using shifts and adds on architectures where multiplication - * instructions are slow). +/*** === Integer hashes [[inthash]] ***/ + +/*** + * We hash integers by multiplying by a reasonably large prime with + * few ones in its binary form (to give the compiler the possibility + * of using shifts and adds on architectures where multiplication + * instructions are slow). */ -static inline uns CONST hash_u32(uns x) { return 0x01008041*x; } -static inline uns CONST hash_u64(u64 x) { return hash_u32((uns)x ^ (uns)(x >> 32)); } -static inline uns CONST hash_pointer(void *x) { return ((sizeof(x) <= 4) ? hash_u32((uns)(uintptr_t)x) : hash_u64((u64)(uintptr_t)x)); } +static inline uns CONST hash_u32(uns x) { return 0x01008041*x; } /** Hash a 32 bit unsigned integer. **/ +static inline uns CONST hash_u64(u64 x) { return hash_u32((uns)x ^ (uns)(x >> 32)); } /** Hash a 64 bit unsigned integer. **/ +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. **/ #endif