ceph_hash.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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) {
  45. case 11:
  46. c = c + ((__u32)k[10] << 24);
  47. /* fall through */
  48. case 10:
  49. c = c + ((__u32)k[9] << 16);
  50. /* fall through */
  51. case 9:
  52. c = c + ((__u32)k[8] << 8);
  53. /* the first byte of c is reserved for the length */
  54. /* fall through */
  55. case 8:
  56. b = b + ((__u32)k[7] << 24);
  57. /* fall through */
  58. case 7:
  59. b = b + ((__u32)k[6] << 16);
  60. /* fall through */
  61. case 6:
  62. b = b + ((__u32)k[5] << 8);
  63. /* fall through */
  64. case 5:
  65. b = b + k[4];
  66. /* fall through */
  67. case 4:
  68. a = a + ((__u32)k[3] << 24);
  69. /* fall through */
  70. case 3:
  71. a = a + ((__u32)k[2] << 16);
  72. /* fall through */
  73. case 2:
  74. a = a + ((__u32)k[1] << 8);
  75. /* fall through */
  76. case 1:
  77. a = a + k[0];
  78. /* case 0: nothing left to add */
  79. }
  80. mix(a, b, c);
  81. return c;
  82. }
  83. /*
  84. * linux dcache hash
  85. */
  86. unsigned int ceph_str_hash_linux(const char *str, unsigned int length)
  87. {
  88. unsigned long hash = 0;
  89. unsigned char c;
  90. while (length--) {
  91. c = *str++;
  92. hash = (hash + (c << 4) + (c >> 4)) * 11;
  93. }
  94. return hash;
  95. }
  96. unsigned int ceph_str_hash(int type, const char *s, unsigned int len)
  97. {
  98. switch (type) {
  99. case CEPH_STR_HASH_LINUX:
  100. return ceph_str_hash_linux(s, len);
  101. case CEPH_STR_HASH_RJENKINS:
  102. return ceph_str_hash_rjenkins(s, len);
  103. default:
  104. return -1;
  105. }
  106. }
  107. EXPORT_SYMBOL(ceph_str_hash);
  108. const char *ceph_str_hash_name(int type)
  109. {
  110. switch (type) {
  111. case CEPH_STR_HASH_LINUX:
  112. return "linux";
  113. case CEPH_STR_HASH_RJENKINS:
  114. return "rjenkins";
  115. default:
  116. return "unknown";
  117. }
  118. }
  119. EXPORT_SYMBOL(ceph_str_hash_name);