hash.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <linux/types.h>
  2. #include <linux/crush/hash.h>
  3. /*
  4. * Robert Jenkins' function for mixing 32-bit values
  5. * http://burtleburtle.net/bob/hash/evahash.html
  6. * a, b = random bits, c = input and output
  7. */
  8. #define crush_hashmix(a, b, c) do { \
  9. a = a-b; a = a-c; a = a^(c>>13); \
  10. b = b-c; b = b-a; b = b^(a<<8); \
  11. c = c-a; c = c-b; c = c^(b>>13); \
  12. a = a-b; a = a-c; a = a^(c>>12); \
  13. b = b-c; b = b-a; b = b^(a<<16); \
  14. c = c-a; c = c-b; c = c^(b>>5); \
  15. a = a-b; a = a-c; a = a^(c>>3); \
  16. b = b-c; b = b-a; b = b^(a<<10); \
  17. c = c-a; c = c-b; c = c^(b>>15); \
  18. } while (0)
  19. #define crush_hash_seed 1315423911
  20. static __u32 crush_hash32_rjenkins1(__u32 a)
  21. {
  22. __u32 hash = crush_hash_seed ^ a;
  23. __u32 b = a;
  24. __u32 x = 231232;
  25. __u32 y = 1232;
  26. crush_hashmix(b, x, hash);
  27. crush_hashmix(y, a, hash);
  28. return hash;
  29. }
  30. static __u32 crush_hash32_rjenkins1_2(__u32 a, __u32 b)
  31. {
  32. __u32 hash = crush_hash_seed ^ a ^ b;
  33. __u32 x = 231232;
  34. __u32 y = 1232;
  35. crush_hashmix(a, b, hash);
  36. crush_hashmix(x, a, hash);
  37. crush_hashmix(b, y, hash);
  38. return hash;
  39. }
  40. static __u32 crush_hash32_rjenkins1_3(__u32 a, __u32 b, __u32 c)
  41. {
  42. __u32 hash = crush_hash_seed ^ a ^ b ^ c;
  43. __u32 x = 231232;
  44. __u32 y = 1232;
  45. crush_hashmix(a, b, hash);
  46. crush_hashmix(c, x, hash);
  47. crush_hashmix(y, a, hash);
  48. crush_hashmix(b, x, hash);
  49. crush_hashmix(y, c, hash);
  50. return hash;
  51. }
  52. static __u32 crush_hash32_rjenkins1_4(__u32 a, __u32 b, __u32 c, __u32 d)
  53. {
  54. __u32 hash = crush_hash_seed ^ a ^ b ^ c ^ d;
  55. __u32 x = 231232;
  56. __u32 y = 1232;
  57. crush_hashmix(a, b, hash);
  58. crush_hashmix(c, d, hash);
  59. crush_hashmix(a, x, hash);
  60. crush_hashmix(y, b, hash);
  61. crush_hashmix(c, x, hash);
  62. crush_hashmix(y, d, hash);
  63. return hash;
  64. }
  65. static __u32 crush_hash32_rjenkins1_5(__u32 a, __u32 b, __u32 c, __u32 d,
  66. __u32 e)
  67. {
  68. __u32 hash = crush_hash_seed ^ a ^ b ^ c ^ d ^ e;
  69. __u32 x = 231232;
  70. __u32 y = 1232;
  71. crush_hashmix(a, b, hash);
  72. crush_hashmix(c, d, hash);
  73. crush_hashmix(e, x, hash);
  74. crush_hashmix(y, a, hash);
  75. crush_hashmix(b, x, hash);
  76. crush_hashmix(y, c, hash);
  77. crush_hashmix(d, x, hash);
  78. crush_hashmix(y, e, hash);
  79. return hash;
  80. }
  81. __u32 crush_hash32(int type, __u32 a)
  82. {
  83. switch (type) {
  84. case CRUSH_HASH_RJENKINS1:
  85. return crush_hash32_rjenkins1(a);
  86. default:
  87. return 0;
  88. }
  89. }
  90. __u32 crush_hash32_2(int type, __u32 a, __u32 b)
  91. {
  92. switch (type) {
  93. case CRUSH_HASH_RJENKINS1:
  94. return crush_hash32_rjenkins1_2(a, b);
  95. default:
  96. return 0;
  97. }
  98. }
  99. __u32 crush_hash32_3(int type, __u32 a, __u32 b, __u32 c)
  100. {
  101. switch (type) {
  102. case CRUSH_HASH_RJENKINS1:
  103. return crush_hash32_rjenkins1_3(a, b, c);
  104. default:
  105. return 0;
  106. }
  107. }
  108. __u32 crush_hash32_4(int type, __u32 a, __u32 b, __u32 c, __u32 d)
  109. {
  110. switch (type) {
  111. case CRUSH_HASH_RJENKINS1:
  112. return crush_hash32_rjenkins1_4(a, b, c, d);
  113. default:
  114. return 0;
  115. }
  116. }
  117. __u32 crush_hash32_5(int type, __u32 a, __u32 b, __u32 c, __u32 d, __u32 e)
  118. {
  119. switch (type) {
  120. case CRUSH_HASH_RJENKINS1:
  121. return crush_hash32_rjenkins1_5(a, b, c, d, e);
  122. default:
  123. return 0;
  124. }
  125. }
  126. const char *crush_hash_name(int type)
  127. {
  128. switch (type) {
  129. case CRUSH_HASH_RJENKINS1:
  130. return "rjenkins1";
  131. default:
  132. return "unknown";
  133. }
  134. }