X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fhashfunc.h;h=e60e3f70628f38feaa843374f2e665ef9a3ac7df;hb=396571ed89ec4260ba3fd1b7ce2b38b07bd90ab7;hp=d79e44ecfdaf695439a026496287ccab2e9ef76b;hpb=b0cbde12a98264c2629b52f618678e7f1eacf097;p=libucw.git diff --git a/lib/hashfunc.h b/lib/hashfunc.h index d79e44ec..e60e3f70 100644 --- a/lib/hashfunc.h +++ b/lib/hashfunc.h @@ -1,18 +1,18 @@ /* - * Hyper-super-meta-alt-control-shift extra fast str_len() and hash_*() - * routines + * UCW Library -- Hyper-super-meta-alt-control-shift extra fast + * str_len() and hash_*() routines * * (c) 2002, Robert Spalek + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ -#ifndef _SHERLOCK_HASHFUNC_H -#define _SHERLOCK_HASHFUNC_H +#ifndef _UCW_HASHFUNC_H +#define _UCW_HASHFUNC_H #include "lib/lib.h" -/* An equivalent of the Intel's rol instruction. */ -#define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits)))) - /* The following functions need str to be aligned to uns. */ uns str_len_aligned(const byte *str) CONST; uns hash_string_aligned(const byte *str) CONST; @@ -30,6 +30,14 @@ uns hash_block(const byte *str, uns len) CONST; uns hash_string_nocase(const byte *str) CONST; -static inline uns CONST hash_int(uns x) { return 6442450967*x; } +/* + * 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). + */ +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)(addr_int_t)x) : hash_u64((u64)(addr_int_t)x)); } #endif