Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 1 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/types.h> |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 3 | #include <linux/module.h> |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 4 | |
| 5 | /* |
| 6 | * Robert Jenkin's hash function. |
Alexander A. Klimov | 94f17c0 | 2020-07-08 08:53:28 +0200 | [diff] [blame] | 7 | * https://burtleburtle.net/bob/hash/evahash.html |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 8 | * This is in the public domain. |
| 9 | */ |
| 10 | #define mix(a, b, c) \ |
| 11 | do { \ |
| 12 | a = a - b; a = a - c; a = a ^ (c >> 13); \ |
| 13 | b = b - c; b = b - a; b = b ^ (a << 8); \ |
| 14 | c = c - a; c = c - b; c = c ^ (b >> 13); \ |
| 15 | a = a - b; a = a - c; a = a ^ (c >> 12); \ |
| 16 | b = b - c; b = b - a; b = b ^ (a << 16); \ |
| 17 | c = c - a; c = c - b; c = c ^ (b >> 5); \ |
| 18 | a = a - b; a = a - c; a = a ^ (c >> 3); \ |
| 19 | b = b - c; b = b - a; b = b ^ (a << 10); \ |
| 20 | c = c - a; c = c - b; c = c ^ (b >> 15); \ |
| 21 | } while (0) |
| 22 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 23 | unsigned int ceph_str_hash_rjenkins(const char *str, unsigned int length) |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 24 | { |
| 25 | const unsigned char *k = (const unsigned char *)str; |
| 26 | __u32 a, b, c; /* the internal state */ |
| 27 | __u32 len; /* how many key bytes still need mixing */ |
| 28 | |
| 29 | /* Set up the internal state */ |
| 30 | len = length; |
| 31 | a = 0x9e3779b9; /* the golden ratio; an arbitrary value */ |
| 32 | b = a; |
| 33 | c = 0; /* variable initialization of internal state */ |
| 34 | |
| 35 | /* handle most of the key */ |
| 36 | while (len >= 12) { |
| 37 | a = a + (k[0] + ((__u32)k[1] << 8) + ((__u32)k[2] << 16) + |
| 38 | ((__u32)k[3] << 24)); |
| 39 | b = b + (k[4] + ((__u32)k[5] << 8) + ((__u32)k[6] << 16) + |
| 40 | ((__u32)k[7] << 24)); |
| 41 | c = c + (k[8] + ((__u32)k[9] << 8) + ((__u32)k[10] << 16) + |
| 42 | ((__u32)k[11] << 24)); |
| 43 | mix(a, b, c); |
| 44 | k = k + 12; |
| 45 | len = len - 12; |
| 46 | } |
| 47 | |
| 48 | /* handle the last 11 bytes */ |
| 49 | c = c + length; |
Gustavo A. R. Silva | 18370b3 | 2017-10-15 12:55:23 -0500 | [diff] [blame] | 50 | switch (len) { |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 51 | case 11: |
| 52 | c = c + ((__u32)k[10] << 24); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 53 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 54 | case 10: |
| 55 | c = c + ((__u32)k[9] << 16); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 56 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 57 | case 9: |
| 58 | c = c + ((__u32)k[8] << 8); |
| 59 | /* the first byte of c is reserved for the length */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 60 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 61 | case 8: |
| 62 | b = b + ((__u32)k[7] << 24); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 63 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 64 | case 7: |
| 65 | b = b + ((__u32)k[6] << 16); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 66 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 67 | case 6: |
| 68 | b = b + ((__u32)k[5] << 8); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 69 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 70 | case 5: |
| 71 | b = b + k[4]; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 72 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 73 | case 4: |
| 74 | a = a + ((__u32)k[3] << 24); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 75 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 76 | case 3: |
| 77 | a = a + ((__u32)k[2] << 16); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 78 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 79 | case 2: |
| 80 | a = a + ((__u32)k[1] << 8); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 81 | fallthrough; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 82 | case 1: |
| 83 | a = a + k[0]; |
| 84 | /* case 0: nothing left to add */ |
| 85 | } |
| 86 | mix(a, b, c); |
| 87 | |
| 88 | return c; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * linux dcache hash |
| 93 | */ |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 94 | unsigned int ceph_str_hash_linux(const char *str, unsigned int length) |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 95 | { |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 96 | unsigned long hash = 0; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 97 | unsigned char c; |
| 98 | |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 99 | while (length--) { |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 100 | c = *str++; |
| 101 | hash = (hash + (c << 4) + (c >> 4)) * 11; |
| 102 | } |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 103 | return hash; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 107 | unsigned int ceph_str_hash(int type, const char *s, unsigned int len) |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 108 | { |
| 109 | switch (type) { |
| 110 | case CEPH_STR_HASH_LINUX: |
| 111 | return ceph_str_hash_linux(s, len); |
| 112 | case CEPH_STR_HASH_RJENKINS: |
| 113 | return ceph_str_hash_rjenkins(s, len); |
| 114 | default: |
| 115 | return -1; |
| 116 | } |
| 117 | } |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 118 | EXPORT_SYMBOL(ceph_str_hash); |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 119 | |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 120 | const char *ceph_str_hash_name(int type) |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 121 | { |
| 122 | switch (type) { |
| 123 | case CEPH_STR_HASH_LINUX: |
| 124 | return "linux"; |
| 125 | case CEPH_STR_HASH_RJENKINS: |
| 126 | return "rjenkins"; |
| 127 | default: |
| 128 | return "unknown"; |
| 129 | } |
| 130 | } |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 131 | EXPORT_SYMBOL(ceph_str_hash_name); |