ceph_hash.c 2.8 KB

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