ceph_hash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "types.h"
  2. /*
  3. * Robert Jenkin's hash function.
  4. * http://burtleburtle.net/bob/hash/evahash.html
  5. * This is in the public domain.
  6. */
  7. #define mix(a, b, c) \
  8. 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. unsigned ceph_str_hash_rjenkins(const char *str, unsigned length)
  20. {
  21. const unsigned char *k = (const unsigned char *)str;
  22. __u32 a, b, c; /* the internal state */
  23. __u32 len; /* how many key bytes still need mixing */
  24. /* Set up the internal state */
  25. len = length;
  26. a = 0x9e3779b9; /* the golden ratio; an arbitrary value */
  27. b = a;
  28. c = 0; /* variable initialization of internal state */
  29. /* handle most of the key */
  30. while (len >= 12) {
  31. a = a + (k[0] + ((__u32)k[1] << 8) + ((__u32)k[2] << 16) +
  32. ((__u32)k[3] << 24));
  33. b = b + (k[4] + ((__u32)k[5] << 8) + ((__u32)k[6] << 16) +
  34. ((__u32)k[7] << 24));
  35. c = c + (k[8] + ((__u32)k[9] << 8) + ((__u32)k[10] << 16) +
  36. ((__u32)k[11] << 24));
  37. mix(a, b, c);
  38. k = k + 12;
  39. len = len - 12;
  40. }
  41. /* handle the last 11 bytes */
  42. c = c + length;
  43. switch (len) { /* all the case statements fall through */
  44. case 11:
  45. c = c + ((__u32)k[10] << 24);
  46. case 10:
  47. c = c + ((__u32)k[9] << 16);
  48. case 9:
  49. c = c + ((__u32)k[8] << 8);
  50. /* the first byte of c is reserved for the length */
  51. case 8:
  52. b = b + ((__u32)k[7] << 24);
  53. case 7:
  54. b = b + ((__u32)k[6] << 16);
  55. case 6:
  56. b = b + ((__u32)k[5] << 8);
  57. case 5:
  58. b = b + k[4];
  59. case 4:
  60. a = a + ((__u32)k[3] << 24);
  61. case 3:
  62. a = a + ((__u32)k[2] << 16);
  63. case 2:
  64. a = a + ((__u32)k[1] << 8);
  65. case 1:
  66. a = a + k[0];
  67. /* case 0: nothing left to add */
  68. }
  69. mix(a, b, c);
  70. return c;
  71. }
  72. /*
  73. * linux dcache hash
  74. */
  75. unsigned ceph_str_hash_linux(const char *str, unsigned length)
  76. {
  77. unsigned long hash = 0;
  78. unsigned char c;
  79. while (length--) {
  80. c = *str++;
  81. hash = (hash + (c << 4) + (c >> 4)) * 11;
  82. }
  83. return hash;
  84. }
  85. unsigned ceph_str_hash(int type, const char *s, unsigned len)
  86. {
  87. switch (type) {
  88. case CEPH_STR_HASH_LINUX:
  89. return ceph_str_hash_linux(s, len);
  90. case CEPH_STR_HASH_RJENKINS:
  91. return ceph_str_hash_rjenkins(s, len);
  92. default:
  93. return -1;
  94. }
  95. }
  96. const char *ceph_str_hash_name(int type)
  97. {
  98. switch (type) {
  99. case CEPH_STR_HASH_LINUX:
  100. return "linux";
  101. case CEPH_STR_HASH_RJENKINS:
  102. return "rjenkins";
  103. default:
  104. return "unknown";
  105. }
  106. }