hash.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef _GENERIC_HASH_H
  2. #define _GENERIC_HASH_H
  3. #include "bitmap.h"
  4. #include "jhash.h"
  5. /* Fast hashing routine for ints, longs and pointers.
  6. (C) 2002 Nadia Yvette Chambers, IBM */
  7. #ifndef __WORDSIZE
  8. #define __WORDSIZE (__SIZEOF_LONG__ * 8)
  9. #endif
  10. #ifndef BITS_PER_LONG
  11. # define BITS_PER_LONG __WORDSIZE
  12. #endif
  13. /*
  14. * non-constant log of base 2 calculators
  15. * - the arch may override these in asm/bitops.h if they can be implemented
  16. * more efficiently than using fls() and fls64()
  17. * - the arch is not required to handle n==0 if implementing the fallback
  18. */
  19. static inline __attribute__((const))
  20. int __ilog2_u32(uint32_t n)
  21. {
  22. return fls(n) - 1;
  23. }
  24. static inline __attribute__((const))
  25. int __ilog2_u64(uint64_t n)
  26. {
  27. return fls64(n) - 1;
  28. }
  29. /**
  30. * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value
  31. * @n - parameter
  32. *
  33. * constant-capable log of base 2 calculation
  34. * - this can be used to initialise global variables from constant data, hence
  35. * the massive ternary operator construction
  36. *
  37. * selects the appropriately-sized optimised version depending on sizeof(n)
  38. */
  39. #define ilog2(n) \
  40. ( \
  41. __builtin_constant_p(n) ? ( \
  42. (n) < 2 ? 0 : \
  43. (n) & (1ULL << 63) ? 63 : \
  44. (n) & (1ULL << 62) ? 62 : \
  45. (n) & (1ULL << 61) ? 61 : \
  46. (n) & (1ULL << 60) ? 60 : \
  47. (n) & (1ULL << 59) ? 59 : \
  48. (n) & (1ULL << 58) ? 58 : \
  49. (n) & (1ULL << 57) ? 57 : \
  50. (n) & (1ULL << 56) ? 56 : \
  51. (n) & (1ULL << 55) ? 55 : \
  52. (n) & (1ULL << 54) ? 54 : \
  53. (n) & (1ULL << 53) ? 53 : \
  54. (n) & (1ULL << 52) ? 52 : \
  55. (n) & (1ULL << 51) ? 51 : \
  56. (n) & (1ULL << 50) ? 50 : \
  57. (n) & (1ULL << 49) ? 49 : \
  58. (n) & (1ULL << 48) ? 48 : \
  59. (n) & (1ULL << 47) ? 47 : \
  60. (n) & (1ULL << 46) ? 46 : \
  61. (n) & (1ULL << 45) ? 45 : \
  62. (n) & (1ULL << 44) ? 44 : \
  63. (n) & (1ULL << 43) ? 43 : \
  64. (n) & (1ULL << 42) ? 42 : \
  65. (n) & (1ULL << 41) ? 41 : \
  66. (n) & (1ULL << 40) ? 40 : \
  67. (n) & (1ULL << 39) ? 39 : \
  68. (n) & (1ULL << 38) ? 38 : \
  69. (n) & (1ULL << 37) ? 37 : \
  70. (n) & (1ULL << 36) ? 36 : \
  71. (n) & (1ULL << 35) ? 35 : \
  72. (n) & (1ULL << 34) ? 34 : \
  73. (n) & (1ULL << 33) ? 33 : \
  74. (n) & (1ULL << 32) ? 32 : \
  75. (n) & (1ULL << 31) ? 31 : \
  76. (n) & (1ULL << 30) ? 30 : \
  77. (n) & (1ULL << 29) ? 29 : \
  78. (n) & (1ULL << 28) ? 28 : \
  79. (n) & (1ULL << 27) ? 27 : \
  80. (n) & (1ULL << 26) ? 26 : \
  81. (n) & (1ULL << 25) ? 25 : \
  82. (n) & (1ULL << 24) ? 24 : \
  83. (n) & (1ULL << 23) ? 23 : \
  84. (n) & (1ULL << 22) ? 22 : \
  85. (n) & (1ULL << 21) ? 21 : \
  86. (n) & (1ULL << 20) ? 20 : \
  87. (n) & (1ULL << 19) ? 19 : \
  88. (n) & (1ULL << 18) ? 18 : \
  89. (n) & (1ULL << 17) ? 17 : \
  90. (n) & (1ULL << 16) ? 16 : \
  91. (n) & (1ULL << 15) ? 15 : \
  92. (n) & (1ULL << 14) ? 14 : \
  93. (n) & (1ULL << 13) ? 13 : \
  94. (n) & (1ULL << 12) ? 12 : \
  95. (n) & (1ULL << 11) ? 11 : \
  96. (n) & (1ULL << 10) ? 10 : \
  97. (n) & (1ULL << 9) ? 9 : \
  98. (n) & (1ULL << 8) ? 8 : \
  99. (n) & (1ULL << 7) ? 7 : \
  100. (n) & (1ULL << 6) ? 6 : \
  101. (n) & (1ULL << 5) ? 5 : \
  102. (n) & (1ULL << 4) ? 4 : \
  103. (n) & (1ULL << 3) ? 3 : \
  104. (n) & (1ULL << 2) ? 2 : \
  105. 1 ) : \
  106. (sizeof(n) <= 4) ? \
  107. __ilog2_u32(n) : \
  108. __ilog2_u64(n) \
  109. )
  110. #if BITS_PER_LONG == 32
  111. #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
  112. #define hash_long(val, bits) hash_32(val, bits)
  113. #elif BITS_PER_LONG == 64
  114. #define hash_long(val, bits) hash_64(val, bits)
  115. #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
  116. #else
  117. #error Wordsize not 32 or 64
  118. #endif
  119. /*
  120. * This hash multiplies the input by a large odd number and takes the
  121. * high bits. Since multiplication propagates changes to the most
  122. * significant end only, it is essential that the high bits of the
  123. * product be used for the hash value.
  124. *
  125. * Chuck Lever verified the effectiveness of this technique:
  126. * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
  127. *
  128. * Although a random odd number will do, it turns out that the golden
  129. * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
  130. * properties. (See Knuth vol 3, section 6.4, exercise 9.)
  131. *
  132. * These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
  133. * which is very slightly easier to multiply by and makes no
  134. * difference to the hash distribution.
  135. */
  136. #define GOLDEN_RATIO_32 0x61C88647
  137. #define GOLDEN_RATIO_64 0x61C8864680B583EBull
  138. /*
  139. * The _generic versions exist only so lib/test_hash.c can compare
  140. * the arch-optimized versions with the generic.
  141. *
  142. * Note that if you change these, any <asm/hash.h> that aren't updated
  143. * to match need to have their HAVE_ARCH_* define values updated so the
  144. * self-test will not false-positive.
  145. */
  146. #ifndef HAVE_ARCH__HASH_32
  147. #define __hash_32 __hash_32_generic
  148. #endif
  149. static inline uint32_t __hash_32_generic(uint32_t val)
  150. {
  151. return val * GOLDEN_RATIO_32;
  152. }
  153. #ifndef HAVE_ARCH_HASH_32
  154. #define hash_32 hash_32_generic
  155. #endif
  156. static inline uint32_t hash_32_generic(uint32_t val, unsigned int bits)
  157. {
  158. /* High bits are more random, so use them. */
  159. return __hash_32(val) >> (32 - bits);
  160. }
  161. #ifndef HAVE_ARCH_HASH_64
  162. #define hash_64 hash_64_generic
  163. #endif
  164. static inline uint32_t hash_64(uint64_t val, unsigned int bits)
  165. {
  166. #if BITS_PER_LONG == 64
  167. /* 64x64-bit multiply is efficient on all 64-bit processors */
  168. return val * GOLDEN_RATIO_64 >> (64 - bits);
  169. #else
  170. /* Hash 64 bits using only 32x32-bit multiply. */
  171. return hash_32((uint32_t)val ^ __hash_32(val >> 32), bits);
  172. #endif
  173. }
  174. static inline uint32_t hash_ptr(const void *ptr, unsigned int bits)
  175. {
  176. return hash_long((unsigned long)ptr, bits);
  177. }
  178. /* This really should be called fold32_ptr; it does no hashing to speak of. */
  179. static inline uint32_t hash32_ptr(const void *ptr)
  180. {
  181. unsigned long val = (unsigned long)ptr;
  182. #if BITS_PER_LONG == 64
  183. val ^= (val >> 32);
  184. #endif
  185. return (uint32_t)val;
  186. }
  187. static inline unsigned long
  188. hash_string(const char *str)
  189. {
  190. unsigned long v = 0;
  191. const char *c;
  192. for (c = str; *c; )
  193. v = (((v << 1) + (v >> 14)) ^ (*c++)) & 0x3fff;
  194. return(v);
  195. }
  196. #endif /* _GENERIC_HASH_H */