jhash.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef _JHASH_H
  2. #define _JHASH_H
  3. #include <stdint.h>
  4. /* jhash.h: Jenkins hash support.
  5. *
  6. * Copyright (C) 2006. Bob Jenkins ([email protected])
  7. *
  8. * http://burtleburtle.net/bob/hash/
  9. *
  10. * These are the credits from Bob's sources:
  11. *
  12. * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
  13. *
  14. * These are functions for producing 32-bit hashes for hash table lookup.
  15. * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
  16. * are externally useful functions. Routines to test the hash are included
  17. * if SELF_TEST is defined. You can use this free for any purpose. It's in
  18. * the public domain. It has no warranty.
  19. *
  20. * Copyright (C) 2009-2010 Jozsef Kadlecsik ([email protected])
  21. *
  22. * I've modified Bob's hash to be useful in the Linux kernel, and
  23. * any bugs present are my fault.
  24. * Jozsef
  25. */
  26. #include "bitops.h"
  27. /* Best hash sizes are of power of two */
  28. #define jhash_size(n) ((uint32_t)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. struct __una_u32 { uint32_t x; } __packed;
  55. static inline uint32_t __get_unaligned_cpu32(const void *p)
  56. {
  57. const struct __una_u32 *ptr = (const struct __una_u32 *)p;
  58. return ptr->x;
  59. }
  60. /* jhash - hash an arbitrary key
  61. * @k: sequence of bytes as key
  62. * @length: the length of the key
  63. * @initval: the previous hash, or an arbitray value
  64. *
  65. * The generic version, hashes an arbitrary sequence of bytes.
  66. * No alignment or length assumptions are made about the input key.
  67. *
  68. * Returns the hash value of the key. The result depends on endianness.
  69. */
  70. static inline uint32_t jhash(const void *key, uint32_t length, uint32_t initval)
  71. {
  72. uint32_t a, b, c;
  73. const uint8_t *k = key;
  74. /* Set up the internal state */
  75. a = b = c = JHASH_INITVAL + length + initval;
  76. /* All but the last block: affect some 32 bits of (a,b,c) */
  77. while (length > 12) {
  78. a += __get_unaligned_cpu32(k);
  79. b += __get_unaligned_cpu32(k + 4);
  80. c += __get_unaligned_cpu32(k + 8);
  81. __jhash_mix(a, b, c);
  82. length -= 12;
  83. k += 12;
  84. }
  85. /* Last block: affect all 32 bits of (c) */
  86. /* All the case statements fall through */
  87. switch (length) {
  88. case 12: c += (uint32_t)k[11]<<24;
  89. case 11: c += (uint32_t)k[10]<<16;
  90. case 10: c += (uint32_t)k[9]<<8;
  91. case 9: c += k[8];
  92. case 8: b += (uint32_t)k[7]<<24;
  93. case 7: b += (uint32_t)k[6]<<16;
  94. case 6: b += (uint32_t)k[5]<<8;
  95. case 5: b += k[4];
  96. case 4: a += (uint32_t)k[3]<<24;
  97. case 3: a += (uint32_t)k[2]<<16;
  98. case 2: a += (uint32_t)k[1]<<8;
  99. case 1: a += k[0];
  100. __jhash_final(a, b, c);
  101. case 0: /* Nothing left to add */
  102. break;
  103. }
  104. return c;
  105. }
  106. /* jhash2 - hash an array of uint32_t's
  107. * @k: the key which must be an array of uint32_t's
  108. * @length: the number of uint32_t's in the key
  109. * @initval: the previous hash, or an arbitray value
  110. *
  111. * Returns the hash value of the key.
  112. */
  113. static inline uint32_t jhash2(const uint32_t *k, uint32_t length, uint32_t initval)
  114. {
  115. uint32_t a, b, c;
  116. /* Set up the internal state */
  117. a = b = c = JHASH_INITVAL + (length<<2) + initval;
  118. /* Handle most of the key */
  119. while (length > 3) {
  120. a += k[0];
  121. b += k[1];
  122. c += k[2];
  123. __jhash_mix(a, b, c);
  124. length -= 3;
  125. k += 3;
  126. }
  127. /* Handle the last 3 uint32_t's: all the case statements fall through */
  128. switch (length) {
  129. case 3: c += k[2];
  130. case 2: b += k[1];
  131. case 1: a += k[0];
  132. __jhash_final(a, b, c);
  133. case 0: /* Nothing left to add */
  134. break;
  135. }
  136. return c;
  137. }
  138. /* __jhash_nwords - hash exactly 3, 2 or 1 word(s) */
  139. static inline uint32_t __jhash_nwords(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
  140. {
  141. a += initval;
  142. b += initval;
  143. c += initval;
  144. __jhash_final(a, b, c);
  145. return c;
  146. }
  147. static inline uint32_t jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
  148. {
  149. return __jhash_nwords(a, b, c, initval + JHASH_INITVAL + (3 << 2));
  150. }
  151. static inline uint32_t jhash_2words(uint32_t a, uint32_t b, uint32_t initval)
  152. {
  153. return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
  154. }
  155. static inline uint32_t jhash_1word(uint32_t a, uint32_t initval)
  156. {
  157. return __jhash_nwords(a, 0, 0, initval + JHASH_INITVAL + (1 << 2));
  158. }
  159. #endif /* _JHASH_H */