/* * UCW Library -- Hyper-super-meta-alt-control-shift extra fast * str_len() and hash_*() routines * * It is always at least as fast as the classical strlen() routine and for * strings longer than 100 characters, it is substantially faster. * * (c) 2002, Robert Spalek * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. */ #include #include #include /* The number of bits the hash in the function hash_*() is rotated by after * every pass. It should be prime with the word size. */ #define SHIFT_BITS 7 /* A bit-mask which clears higher bytes than a given threshold. */ static uint mask_higher_bits[sizeof(uint)]; static void CONSTRUCTOR hashfunc_init(void) { uint i, j; byte *str; for (i=0; i= sizeof(uint)) { hash = ROL(hash, SHIFT_BITS) ^ *u++; len -= sizeof(uint); } hash = ROL(hash, SHIFT_BITS) ^ (*u & mask_higher_bits[len]); return hash; } #ifndef CPU_ALLOW_UNALIGNED uint str_len(const char *str) { uint shift = UNALIGNED_PART(str, uint); if (!shift) return str_len_aligned(str); else { uint i; shift = sizeof(uint) - shift; for (i=0; i= len) break; hash ^= buf[i] << (shift * 8); } return hash; } } #endif uint hash_string_nocase(const char *str) { const byte *s = str; uint hash = 0; uint i; for (i=0; ; i++) { uint modulo = i % sizeof(uint); uint shift; #ifdef CPU_LITTLE_ENDIAN shift = modulo; #else shift = sizeof(uint) - 1 - modulo; #endif if (!modulo) hash = ROL(hash, SHIFT_BITS); if (!s[i]) break; hash ^= Cupcase(s[i]) << (shift * 8); } return hash; }