hash.c 3.2 KB

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