201-jhash3.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. --- a/include/linux/jhash.h
  2. +++ b/include/linux/jhash.h
  3. @@ -3,80 +3,95 @@
  4. /* jhash.h: Jenkins hash support.
  5. *
  6. - * Copyright (C) 1996 Bob Jenkins ([email protected])
  7. + * Copyright (C) 2006. Bob Jenkins ([email protected])
  8. *
  9. * http://burtleburtle.net/bob/hash/
  10. *
  11. * These are the credits from Bob's sources:
  12. *
  13. - * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
  14. - * hash(), hash2(), hash3, and mix() are externally useful functions.
  15. - * Routines to test the hash are included if SELF_TEST is defined.
  16. - * You can use this free for any purpose. It has no warranty.
  17. + * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
  18. *
  19. - * Copyright (C) 2003 David S. Miller ([email protected])
  20. + * These are functions for producing 32-bit hashes for hash table lookup.
  21. + * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
  22. + * are externally useful functions. Routines to test the hash are included
  23. + * if SELF_TEST is defined. You can use this free for any purpose. It's in
  24. + * the public domain. It has no warranty.
  25. + *
  26. + * Copyright (C) 2009 Jozsef Kadlecsik ([email protected])
  27. *
  28. * I've modified Bob's hash to be useful in the Linux kernel, and
  29. - * any bugs present are surely my fault. -DaveM
  30. + * any bugs present are my fault. Jozsef
  31. */
  32. -/* NOTE: Arguments are modified. */
  33. -#define __jhash_mix(a, b, c) \
  34. +#define __rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  35. +
  36. +/* __jhash_mix - mix 3 32-bit values reversibly. */
  37. +#define __jhash_mix(a,b,c) \
  38. +{ \
  39. + a -= c; a ^= __rot(c, 4); c += b; \
  40. + b -= a; b ^= __rot(a, 6); a += c; \
  41. + c -= b; c ^= __rot(b, 8); b += a; \
  42. + a -= c; a ^= __rot(c,16); c += b; \
  43. + b -= a; b ^= __rot(a,19); a += c; \
  44. + c -= b; c ^= __rot(b, 4); b += a; \
  45. +}
  46. +
  47. +/* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
  48. +#define __jhash_final(a,b,c) \
  49. { \
  50. - a -= b; a -= c; a ^= (c>>13); \
  51. - b -= c; b -= a; b ^= (a<<8); \
  52. - c -= a; c -= b; c ^= (b>>13); \
  53. - a -= b; a -= c; a ^= (c>>12); \
  54. - b -= c; b -= a; b ^= (a<<16); \
  55. - c -= a; c -= b; c ^= (b>>5); \
  56. - a -= b; a -= c; a ^= (c>>3); \
  57. - b -= c; b -= a; b ^= (a<<10); \
  58. - c -= a; c -= b; c ^= (b>>15); \
  59. + c ^= b; c -= __rot(b,14); \
  60. + a ^= c; a -= __rot(c,11); \
  61. + b ^= a; b -= __rot(a,25); \
  62. + c ^= b; c -= __rot(b,16); \
  63. + a ^= c; a -= __rot(c,4); \
  64. + b ^= a; b -= __rot(a,14); \
  65. + c ^= b; c -= __rot(b,24); \
  66. }
  67. -/* The golden ration: an arbitrary value */
  68. -#define JHASH_GOLDEN_RATIO 0x9e3779b9
  69. +/* An arbitrary initial parameter */
  70. +#define JHASH_GOLDEN_RATIO 0xdeadbeef
  71. /* The most generic version, hashes an arbitrary sequence
  72. * of bytes. No alignment or length assumptions are made about
  73. - * the input key.
  74. + * the input key. The result depends on endianness.
  75. */
  76. static inline u32 jhash(const void *key, u32 length, u32 initval)
  77. {
  78. - u32 a, b, c, len;
  79. + u32 a,b,c;
  80. const u8 *k = key;
  81. - len = length;
  82. - a = b = JHASH_GOLDEN_RATIO;
  83. - c = initval;
  84. -
  85. - while (len >= 12) {
  86. - a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24));
  87. - b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24));
  88. - c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24));
  89. -
  90. - __jhash_mix(a,b,c);
  91. + /* Set up the internal state */
  92. + a = b = c = JHASH_GOLDEN_RATIO + length + initval;
  93. + /* all but the last block: affect some 32 bits of (a,b,c) */
  94. + while (length > 12) {
  95. + a += (k[0] + ((u32)k[1]<<8) + ((u32)k[2]<<16) + ((u32)k[3]<<24));
  96. + b += (k[4] + ((u32)k[5]<<8) + ((u32)k[6]<<16) + ((u32)k[7]<<24));
  97. + c += (k[8] + ((u32)k[9]<<8) + ((u32)k[10]<<16) + ((u32)k[11]<<24));
  98. + __jhash_mix(a, b, c);
  99. + length -= 12;
  100. k += 12;
  101. - len -= 12;
  102. }
  103. - c += length;
  104. - switch (len) {
  105. - case 11: c += ((u32)k[10]<<24);
  106. - case 10: c += ((u32)k[9]<<16);
  107. - case 9 : c += ((u32)k[8]<<8);
  108. - case 8 : b += ((u32)k[7]<<24);
  109. - case 7 : b += ((u32)k[6]<<16);
  110. - case 6 : b += ((u32)k[5]<<8);
  111. + /* last block: affect all 32 bits of (c) */
  112. + /* all the case statements fall through */
  113. + switch (length) {
  114. + case 12: c += (u32)k[11]<<24;
  115. + case 11: c += (u32)k[10]<<16;
  116. + case 10: c += (u32)k[9]<<8;
  117. + case 9 : c += k[8];
  118. + case 8 : b += (u32)k[7]<<24;
  119. + case 7 : b += (u32)k[6]<<16;
  120. + case 6 : b += (u32)k[5]<<8;
  121. case 5 : b += k[4];
  122. - case 4 : a += ((u32)k[3]<<24);
  123. - case 3 : a += ((u32)k[2]<<16);
  124. - case 2 : a += ((u32)k[1]<<8);
  125. + case 4 : a += (u32)k[3]<<24;
  126. + case 3 : a += (u32)k[2]<<16;
  127. + case 2 : a += (u32)k[1]<<8;
  128. case 1 : a += k[0];
  129. - };
  130. -
  131. - __jhash_mix(a,b,c);
  132. + __jhash_final(a, b, c);
  133. + case 0 :
  134. + break;
  135. + }
  136. return c;
  137. }
  138. @@ -86,58 +101,57 @@ static inline u32 jhash(const void *key,
  139. */
  140. static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
  141. {
  142. - u32 a, b, c, len;
  143. + u32 a, b, c;
  144. - a = b = JHASH_GOLDEN_RATIO;
  145. - c = initval;
  146. - len = length;
  147. + /* Set up the internal state */
  148. + a = b = c = JHASH_GOLDEN_RATIO + (length<<2) + initval;
  149. - while (len >= 3) {
  150. + /* handle most of the key */
  151. + while (length > 3) {
  152. a += k[0];
  153. b += k[1];
  154. c += k[2];
  155. __jhash_mix(a, b, c);
  156. - k += 3; len -= 3;
  157. + length -= 3;
  158. + k += 3;
  159. }
  160. - c += length * 4;
  161. -
  162. - switch (len) {
  163. - case 2 : b += k[1];
  164. - case 1 : a += k[0];
  165. - };
  166. -
  167. - __jhash_mix(a,b,c);
  168. + /* handle the last 3 u32's */
  169. + /* all the case statements fall through */
  170. + switch (length) {
  171. + case 3: c += k[2];
  172. + case 2: b += k[1];
  173. + case 1: a += k[0];
  174. + __jhash_final(a, b, c);
  175. + case 0: /* case 0: nothing left to add */
  176. + break;
  177. + }
  178. return c;
  179. }
  180. -
  181. /* A special ultra-optimized versions that knows they are hashing exactly
  182. * 3, 2 or 1 word(s).
  183. - *
  184. - * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
  185. - * done at the end is not done here.
  186. */
  187. static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
  188. {
  189. - a += JHASH_GOLDEN_RATIO;
  190. - b += JHASH_GOLDEN_RATIO;
  191. - c += initval;
  192. + a += JHASH_GOLDEN_RATIO + initval;
  193. + b += JHASH_GOLDEN_RATIO + initval;
  194. + c += JHASH_GOLDEN_RATIO + initval;
  195. - __jhash_mix(a, b, c);
  196. + __jhash_final(a, b, c);
  197. return c;
  198. }
  199. static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
  200. {
  201. - return jhash_3words(a, b, 0, initval);
  202. + return jhash_3words(0, a, b, initval);
  203. }
  204. static inline u32 jhash_1word(u32 a, u32 initval)
  205. {
  206. - return jhash_3words(a, 0, 0, initval);
  207. + return jhash_3words(0, 0, a, initval);
  208. }
  209. #endif /* _LINUX_JHASH_H */