hash.h 3.0 KB

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