jhash.h 3.2 KB

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