jhash.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef _LINUX_JHASH_H
  2. #define _LINUX_JHASH_H
  3. #include "stdint.h"
  4. /* jhash.h: Jenkins hash support.
  5. *
  6. * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
  7. *
  8. * http://burtleburtle.net/bob/hash/
  9. *
  10. * These are the credits from Bob's sources:
  11. *
  12. * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
  13. * hash(), hash2(), hash3, and mix() are externally useful functions.
  14. * Routines to test the hash are included if SELF_TEST is defined.
  15. * You can use this free for any purpose. It has no warranty.
  16. *
  17. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  18. *
  19. * I've modified Bob's hash to be useful in the Linux kernel, and
  20. * any bugs present are surely my fault. -DaveM
  21. */
  22. /* NOTE: Arguments are modified. */
  23. #define __jhash_mix(a, b, c) \
  24. { \
  25. a -= b; a -= c; a ^= (c>>13); \
  26. b -= c; b -= a; b ^= (a<<8); \
  27. c -= a; c -= b; c ^= (b>>13); \
  28. a -= b; a -= c; a ^= (c>>12); \
  29. b -= c; b -= a; b ^= (a<<16); \
  30. c -= a; c -= b; c ^= (b>>5); \
  31. a -= b; a -= c; a ^= (c>>3); \
  32. b -= c; b -= a; b ^= (a<<10); \
  33. c -= a; c -= b; c ^= (b>>15); \
  34. }
  35. /* The golden ration: an arbitrary value */
  36. #define JHASH_GOLDEN_RATIO 0x9e3779b9
  37. /* The most generic version, hashes an arbitrary sequence
  38. * of bytes. No alignment or length assumptions are made about
  39. * the input key.
  40. */
  41. static __inline uint32_t jhash(const void *key, uint32_t length, uint32_t initval)
  42. {
  43. uint32_t a, b, c, len;
  44. const uint8_t *k = (const uint8_t *)key;
  45. len = length;
  46. a = b = JHASH_GOLDEN_RATIO;
  47. c = initval;
  48. while (len >= 12) {
  49. a += (k[0] +((uint32_t)k[1]<<8) +((uint32_t)k[2]<<16) +((uint32_t)k[3]<<24));
  50. b += (k[4] +((uint32_t)k[5]<<8) +((uint32_t)k[6]<<16) +((uint32_t)k[7]<<24));
  51. c += (k[8] +((uint32_t)k[9]<<8) +((uint32_t)k[10]<<16)+((uint32_t)k[11]<<24));
  52. __jhash_mix(a,b,c);
  53. k += 12;
  54. len -= 12;
  55. }
  56. c += length;
  57. switch (len) {
  58. case 11:
  59. c += ((uint32_t)k[10]<<24);
  60. break;
  61. case 10:
  62. c += ((uint32_t)k[9]<<16);
  63. break;
  64. case 9 :
  65. c += ((uint32_t)k[8]<<8);
  66. break;
  67. case 8 :
  68. b += ((uint32_t)k[7]<<24);
  69. break;
  70. case 7 :
  71. b += ((uint32_t)k[6]<<16);
  72. break;
  73. case 6 :
  74. b += ((uint32_t)k[5]<<8);
  75. break;
  76. case 5 :
  77. b += k[4];
  78. break;
  79. case 4 :
  80. a += ((uint32_t)k[3]<<24);
  81. break;
  82. case 3 :
  83. a += ((uint32_t)k[2]<<16);
  84. break;
  85. case 2 :
  86. a += ((uint32_t)k[1]<<8);
  87. break;
  88. case 1 :
  89. a += k[0];
  90. break;
  91. default:
  92. break;
  93. };
  94. __jhash_mix(a,b,c);
  95. return c;
  96. }
  97. /* A special optimized version that handles 1 or more of u32s.
  98. * The length parameter here is the number of u32s in the key.
  99. */
  100. static __inline uint32_t jhash2(const uint32_t *k, uint32_t length, uint32_t initval)
  101. {
  102. uint32_t a, b, c, len;
  103. a = b = JHASH_GOLDEN_RATIO;
  104. c = initval;
  105. len = length;
  106. while (len >= 3) {
  107. a += k[0];
  108. b += k[1];
  109. c += k[2];
  110. __jhash_mix(a, b, c);
  111. k += 3; len -= 3;
  112. }
  113. c += length * 4;
  114. switch (len) {
  115. case 2 :
  116. b += k[1];
  117. break;
  118. case 1 :
  119. a += k[0];
  120. break;
  121. default:
  122. break;
  123. };
  124. __jhash_mix(a,b,c);
  125. return c;
  126. }
  127. /* A special ultra-optimized versions that knows they are hashing exactly
  128. * 3, 2 or 1 word(s).
  129. *
  130. * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
  131. * done at the end is not done here.
  132. */
  133. static __inline uint32_t jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
  134. {
  135. a += JHASH_GOLDEN_RATIO;
  136. b += JHASH_GOLDEN_RATIO;
  137. c += initval;
  138. __jhash_mix(a, b, c);
  139. return c;
  140. }
  141. static __inline uint32_t jhash_2words(uint32_t a, uint32_t b, uint32_t initval)
  142. {
  143. return jhash_3words(a, b, 0, initval);
  144. }
  145. static __inline uint32_t jhash_1word(uint32_t a, uint32_t initval)
  146. {
  147. return jhash_3words(a, 0, 0, initval);
  148. }
  149. #endif /* _LINUX_JHASH_H */