X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=hash.h;h=2c04bc296974dbe7708fe2ef940afd5893a92ec0;hp=02b06140146b469abd05772406102fec845747fc;hb=548b363c08875165a018788195e8fd2304c2ce24;hpb=c19df83fb6d4ed23d59f2038b145054a1996cfb6 diff --git a/hash.h b/hash.h index 02b06140..2c04bc29 100644 --- a/hash.h +++ b/hash.h @@ -3,6 +3,7 @@ #include #include "arch/arch.h" +#include "compiler/compiler.h" /* Fast hashing routine for a long. (C) 2002 William Lee Irwin III, IBM */ @@ -28,15 +29,31 @@ #error Define GOLDEN_RATIO_PRIME for your wordsize. #endif -#define GR_PRIME_64 0x9e37fffffffc0001ULL +/* + * The above primes are actively bad for hashing, since they are + * too sparse. The 32-bit one is mostly ok, the 64-bit one causes + * real problems. Besides, the "prime" part is pointless for the + * multiplicative hash. + * + * Although a random odd number will do, it turns out that the golden + * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice + * properties. + * + * These are the negative, (1 - phi) = (phi^2) = (3 - sqrt(5))/2. + * (See Knuth vol 3, section 6.4, exercise 9.) + */ +#define GOLDEN_RATIO_32 0x61C88647 +#define GOLDEN_RATIO_64 0x61C8864680B583EBull -static inline unsigned long __hash_long(unsigned long val) +static inline unsigned long __hash_long(uint64_t val) { - unsigned long hash = val; + uint64_t hash = val; #if BITS_PER_LONG == 64 + hash *= GOLDEN_RATIO_64; +#else /* Sigh, gcc can't optimise this alone like it does for 32 bits. */ - unsigned long n = hash; + uint64_t n = hash; n <<= 18; hash -= n; n <<= 33; @@ -49,9 +66,6 @@ static inline unsigned long __hash_long(unsigned long val) hash += n; n <<= 2; hash += n; -#else - /* On some cpus multiply is faster, on others gcc will do shifts */ - hash *= GOLDEN_RATIO_PRIME; #endif return hash; @@ -65,7 +79,7 @@ static inline unsigned long hash_long(unsigned long val, unsigned int bits) static inline uint64_t __hash_u64(uint64_t val) { - return val * GR_PRIME_64; + return val * GOLDEN_RATIO_64; } static inline unsigned long hash_ptr(void *ptr, unsigned int bits) @@ -77,7 +91,7 @@ static inline unsigned long hash_ptr(void *ptr, unsigned int bits) * Bob Jenkins jhash */ -#define JHASH_INITVAL GOLDEN_RATIO_PRIME +#define JHASH_INITVAL GOLDEN_RATIO_32 static inline uint32_t rol32(uint32_t word, uint32_t shift) { @@ -128,19 +142,20 @@ static inline uint32_t jhash(const void *key, uint32_t length, uint32_t initval) /* Last block: affect all 32 bits of (c) */ /* All the case statements fall through */ switch (length) { - case 12: c += (uint32_t) k[11] << 24; - case 11: c += (uint32_t) k[10] << 16; - case 10: c += (uint32_t) k[9] << 8; - case 9: c += k[8]; - case 8: b += (uint32_t) k[7] << 24; - case 7: b += (uint32_t) k[6] << 16; - case 6: b += (uint32_t) k[5] << 8; - case 5: b += k[4]; - case 4: a += (uint32_t) k[3] << 24; - case 3: a += (uint32_t) k[2] << 16; - case 2: a += (uint32_t) k[1] << 8; + case 12: c += (uint32_t) k[11] << 24; fallthrough; + case 11: c += (uint32_t) k[10] << 16; fallthrough; + case 10: c += (uint32_t) k[9] << 8; fallthrough; + case 9: c += k[8]; fallthrough; + case 8: b += (uint32_t) k[7] << 24; fallthrough; + case 7: b += (uint32_t) k[6] << 16; fallthrough; + case 6: b += (uint32_t) k[5] << 8; fallthrough; + case 5: b += k[4]; fallthrough; + case 4: a += (uint32_t) k[3] << 24; fallthrough; + case 3: a += (uint32_t) k[2] << 16; fallthrough; + case 2: a += (uint32_t) k[1] << 8; fallthrough; case 1: a += k[0]; __jhash_final(a, b, c); + fallthrough; case 0: /* Nothing left to add */ break; }