hash.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /*
  6. * Knuth recommends primes in approximately golden ratio to the maximum
  7. * integer representable by a machine word for multiplicative hashing.
  8. * Chuck Lever verified the effectiveness of this technique:
  9. * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
  10. *
  11. * These primes are chosen to be bit-sparse, that is operations on
  12. * them can use shifts and additions instead of multiplications for
  13. * machines where multiplications are slow.
  14. */
  15. #include <asm/types.h>
  16. #include <asm/hash.h>
  17. #include <linux/compiler.h>
  18. /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
  19. #define GOLDEN_RATIO_PRIME_32 0x9e370001UL
  20. /* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
  21. #define GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001UL
  22. #if BITS_PER_LONG == 32
  23. #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_PRIME_32
  24. #define hash_long(val, bits) hash_32(val, bits)
  25. #elif BITS_PER_LONG == 64
  26. #define hash_long(val, bits) hash_64(val, bits)
  27. #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_PRIME_64
  28. #else
  29. #error Wordsize not 32 or 64
  30. #endif
  31. static __always_inline u64 hash_64(u64 val, unsigned int bits)
  32. {
  33. u64 hash = val;
  34. #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  35. hash = hash * GOLDEN_RATIO_PRIME_64;
  36. #else
  37. /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
  38. u64 n = hash;
  39. n <<= 18;
  40. hash -= n;
  41. n <<= 33;
  42. hash -= n;
  43. n <<= 3;
  44. hash += n;
  45. n <<= 3;
  46. hash -= n;
  47. n <<= 4;
  48. hash += n;
  49. n <<= 2;
  50. hash += n;
  51. #endif
  52. /* High bits are more random, so use them. */
  53. return hash >> (64 - bits);
  54. }
  55. static inline u32 hash_32(u32 val, unsigned int bits)
  56. {
  57. /* On some cpus multiply is faster, on others gcc will do shifts */
  58. u32 hash = val * GOLDEN_RATIO_PRIME_32;
  59. /* High bits are more random, so use them. */
  60. return hash >> (32 - bits);
  61. }
  62. static inline unsigned long hash_ptr(const void *ptr, unsigned int bits)
  63. {
  64. return hash_long((unsigned long)ptr, bits);
  65. }
  66. static inline u32 hash32_ptr(const void *ptr)
  67. {
  68. unsigned long val = (unsigned long)ptr;
  69. #if BITS_PER_LONG == 64
  70. val ^= (val >> 32);
  71. #endif
  72. return (u32)val;
  73. }
  74. struct fast_hash_ops {
  75. u32 (*hash)(const void *data, u32 len, u32 seed);
  76. u32 (*hash2)(const u32 *data, u32 len, u32 seed);
  77. };
  78. /**
  79. * arch_fast_hash - Caclulates a hash over a given buffer that can have
  80. * arbitrary size. This function will eventually use an
  81. * architecture-optimized hashing implementation if
  82. * available, and trades off distribution for speed.
  83. *
  84. * @data: buffer to hash
  85. * @len: length of buffer in bytes
  86. * @seed: start seed
  87. *
  88. * Returns 32bit hash.
  89. */
  90. extern u32 arch_fast_hash(const void *data, u32 len, u32 seed);
  91. /**
  92. * arch_fast_hash2 - Caclulates a hash over a given buffer that has a
  93. * size that is of a multiple of 32bit words. This
  94. * function will eventually use an architecture-
  95. * optimized hashing implementation if available,
  96. * and trades off distribution for speed.
  97. *
  98. * @data: buffer to hash (must be 32bit padded)
  99. * @len: number of 32bit words
  100. * @seed: start seed
  101. *
  102. * Returns 32bit hash.
  103. */
  104. extern u32 arch_fast_hash2(const u32 *data, u32 len, u32 seed);
  105. #endif /* _LINUX_HASH_H */