jhash.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef _LINUX_JHASH_H
  2. #define _LINUX_JHASH_H
  3. /* jhash.h: Jenkins hash support.
  4. *
  5. * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net)
  6. *
  7. * https://burtleburtle.net/bob/hash/
  8. *
  9. * These are the credits from Bob's sources:
  10. *
  11. * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
  12. *
  13. * These are functions for producing 32-bit hashes for hash table lookup.
  14. * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
  15. * are externally useful functions. Routines to test the hash are included
  16. * if SELF_TEST is defined. You can use this free for any purpose. It's in
  17. * the public domain. It has no warranty.
  18. *
  19. * Copyright (C) 2009-2010 Jozsef Kadlecsik (kadlec@netfilter.org)
  20. *
  21. * I've modified Bob's hash to be useful in the Linux kernel, and
  22. * any bugs present are my fault.
  23. * Jozsef
  24. */
  25. #include <linux/bitops.h>
  26. #include <linux/unaligned/packed_struct.h>
  27. /* Best hash sizes are of power of two */
  28. #define jhash_size(n) ((u32)1<<(n))
  29. /* Mask the hash value, i.e (value & jhash_mask(n)) instead of (value % n) */
  30. #define jhash_mask(n) (jhash_size(n)-1)
  31. /* __jhash_mix -- mix 3 32-bit values reversibly. */
  32. #define __jhash_mix(a, b, c) \
  33. { \
  34. a -= c; a ^= rol32(c, 4); c += b; \
  35. b -= a; b ^= rol32(a, 6); a += c; \
  36. c -= b; c ^= rol32(b, 8); b += a; \
  37. a -= c; a ^= rol32(c, 16); c += b; \
  38. b -= a; b ^= rol32(a, 19); a += c; \
  39. c -= b; c ^= rol32(b, 4); b += a; \
  40. }
  41. /* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
  42. #define __jhash_final(a, b, c) \
  43. { \
  44. c ^= b; c -= rol32(b, 14); \
  45. a ^= c; a -= rol32(c, 11); \
  46. b ^= a; b -= rol32(a, 25); \
  47. c ^= b; c -= rol32(b, 16); \
  48. a ^= c; a -= rol32(c, 4); \
  49. b ^= a; b -= rol32(a, 14); \
  50. c ^= b; c -= rol32(b, 24); \
  51. }
  52. /* An arbitrary initial parameter */
  53. #define JHASH_INITVAL 0xdeadbeef
  54. /* jhash - hash an arbitrary key
  55. * @k: sequence of bytes as key
  56. * @length: the length of the key
  57. * @initval: the previous hash, or an arbitray value
  58. *
  59. * The generic version, hashes an arbitrary sequence of bytes.
  60. * No alignment or length assumptions are made about the input key.
  61. *
  62. * Returns the hash value of the key. The result depends on endianness.
  63. */
  64. static inline u32 jhash(const void *key, u32 length, u32 initval)
  65. {
  66. u32 a, b, c;
  67. const u8 *k = key;
  68. /* Set up the internal state */
  69. a = b = c = JHASH_INITVAL + length + initval;
  70. /* All but the last block: affect some 32 bits of (a,b,c) */
  71. while (length > 12) {
  72. a += __get_unaligned_cpu32(k);
  73. b += __get_unaligned_cpu32(k + 4);
  74. c += __get_unaligned_cpu32(k + 8);
  75. __jhash_mix(a, b, c);
  76. length -= 12;
  77. k += 12;
  78. }
  79. /* Last block: affect all 32 bits of (c) */
  80. switch (length) {
  81. case 12: c += (u32)k[11]<<24; fallthrough;
  82. case 11: c += (u32)k[10]<<16; fallthrough;
  83. case 10: c += (u32)k[9]<<8; fallthrough;
  84. case 9: c += k[8]; fallthrough;
  85. case 8: b += (u32)k[7]<<24; fallthrough;
  86. case 7: b += (u32)k[6]<<16; fallthrough;
  87. case 6: b += (u32)k[5]<<8; fallthrough;
  88. case 5: b += k[4]; fallthrough;
  89. case 4: a += (u32)k[3]<<24; fallthrough;
  90. case 3: a += (u32)k[2]<<16; fallthrough;
  91. case 2: a += (u32)k[1]<<8; fallthrough;
  92. case 1: a += k[0];
  93. __jhash_final(a, b, c);
  94. break;
  95. case 0: /* Nothing left to add */
  96. break;
  97. }
  98. return c;
  99. }
  100. /* jhash2 - hash an array of u32's
  101. * @k: the key which must be an array of u32's
  102. * @length: the number of u32's in the key
  103. * @initval: the previous hash, or an arbitray value
  104. *
  105. * Returns the hash value of the key.
  106. */
  107. static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
  108. {
  109. u32 a, b, c;
  110. /* Set up the internal state */
  111. a = b = c = JHASH_INITVAL + (length<<2) + initval;
  112. /* Handle most of the key */
  113. while (length > 3) {
  114. a += k[0];
  115. b += k[1];
  116. c += k[2];
  117. __jhash_mix(a, b, c);
  118. length -= 3;
  119. k += 3;
  120. }
  121. /* Handle the last 3 u32's */
  122. switch (length) {
  123. case 3: c += k[2]; fallthrough;
  124. case 2: b += k[1]; fallthrough;
  125. case 1: a += k[0];
  126. __jhash_final(a, b, c);
  127. break;
  128. case 0: /* Nothing left to add */
  129. break;
  130. }
  131. return c;
  132. }
  133. /* __jhash_nwords - hash exactly 3, 2 or 1 word(s) */
  134. static inline u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
  135. {
  136. a += initval;
  137. b += initval;
  138. c += initval;
  139. __jhash_final(a, b, c);
  140. return c;
  141. }
  142. static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
  143. {
  144. return __jhash_nwords(a, b, c, initval + JHASH_INITVAL + (3 << 2));
  145. }
  146. static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
  147. {
  148. return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
  149. }
  150. static inline u32 jhash_1word(u32 a, u32 initval)
  151. {
  152. return __jhash_nwords(a, 0, 0, initval + JHASH_INITVAL + (1 << 2));
  153. }
  154. #endif /* _LINUX_JHASH_H */