^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #ifndef _LINUX_HASH_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #define _LINUX_HASH_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /* Fast hashing routine for ints, longs and pointers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) (C) 2002 Nadia Yvette Chambers, IBM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * fs/inode.c. It's not actually prime any more (the previous primes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * were actively bad for hashing), but the name remains.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #if BITS_PER_LONG == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define hash_long(val, bits) hash_32(val, bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #elif BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define hash_long(val, bits) hash_64(val, bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #error Wordsize not 32 or 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * This hash multiplies the input by a large odd number and takes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * high bits. Since multiplication propagates changes to the most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * significant end only, it is essential that the high bits of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * product be used for the hash value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Chuck Lever verified the effectiveness of this technique:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Although a random odd number will do, it turns out that the golden
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * properties. (See Knuth vol 3, section 6.4, exercise 9.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * which is very slightly easier to multiply by and makes no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * difference to the hash distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define GOLDEN_RATIO_32 0x61C88647
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define GOLDEN_RATIO_64 0x61C8864680B583EBull
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #ifdef CONFIG_HAVE_ARCH_HASH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* This header may use the GOLDEN_RATIO_xx constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <asm/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * The _generic versions exist only so lib/test_hash.c can compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * the arch-optimized versions with the generic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Note that if you change these, any <asm/hash.h> that aren't updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * to match need to have their HAVE_ARCH_* define values updated so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * self-test will not false-positive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #ifndef HAVE_ARCH__HASH_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define __hash_32 __hash_32_generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline u32 __hash_32_generic(u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return val * GOLDEN_RATIO_32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #ifndef HAVE_ARCH_HASH_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define hash_32 hash_32_generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static inline u32 hash_32_generic(u32 val, unsigned int bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* High bits are more random, so use them. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return __hash_32(val) >> (32 - bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #ifndef HAVE_ARCH_HASH_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define hash_64 hash_64_generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static __always_inline u32 hash_64_generic(u64 val, unsigned int bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #if BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* 64x64-bit multiply is efficient on all 64-bit processors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return val * GOLDEN_RATIO_64 >> (64 - bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Hash 64 bits using only 32x32-bit multiply. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return hash_32((u32)val ^ __hash_32(val >> 32), bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static inline u32 hash_ptr(const void *ptr, unsigned int bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return hash_long((unsigned long)ptr, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* This really should be called fold32_ptr; it does no hashing to speak of. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static inline u32 hash32_ptr(const void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned long val = (unsigned long)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #if BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) val ^= (val >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return (u32)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #endif /* _LINUX_HASH_H */