jhash.h 3.7 KB

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