cityhash.h

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 152 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/ClickHouse.pm ↑ Summary xs/lz4.c »
1 /*
2 * CityHash v1.0.2 — minimal implementation of CityHash128.
3 * Used for ClickHouse native protocol compression checksums.
4 *
5 * Based on CityHash by Geoff Pike and Jyrki Alakuijala (Google).
6 * Original code: https://github.com/google/cityhash
7 * Reference: https://github.com/ClickHouse/ClickHouse/tree/master/contrib/cityhash102
8 * License: MIT
9 *
10 * IMPORTANT: This is CityHash v1.0.2 specifically. Later versions
11 * changed the algorithm and produce different results.
12 */
13  
14 #ifndef CITYHASH_H
15 #define CITYHASH_H
16  
17 #include <stdint.h>
18 #include <string.h>
19  
20 typedef struct { uint64_t lo, hi; } ch_uint128_t;
21  
22 0 static inline uint64_t ch_fetch64(const char *p) {
23 uint64_t r;
24 0 memcpy(&r, p, 8);
25 0 return r;
26 }
27  
28 0 static inline uint32_t ch_fetch32(const char *p) {
29 uint32_t r;
30 0 memcpy(&r, p, 4);
31 0 return r;
32 }
33  
34 0 static inline uint64_t ch_rotate(uint64_t val, int shift) {
35 0 ▶ return shift == 0 ? val : (val >> shift) | (val << (64 - shift));
Branch: 1 branch
BranchTrueFalseTotal
return shift == 0 ? val : (val >> shift) | (val << (64 - shift)); 0 0 0
36 }
37  
38 0 static inline uint64_t ch_shift_mix(uint64_t val) {
39 0 return val ^ (val >> 47);
40 }
41  
42 static const uint64_t ch_k0 = 0xc3a5c85c97cb3127ULL;
43 static const uint64_t ch_k1 = 0xb492b66fbe98f273ULL;
44 static const uint64_t ch_k2 = 0x9ae16a3b2f90404fULL;
45 static const uint64_t ch_k3 = 0xc949d7c7509e6557ULL;
46  
47 0 static inline uint64_t ch_hash128to64(ch_uint128_t x) {
48 0 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
49 0 uint64_t a = (x.lo ^ x.hi) * kMul;
50 0 a ^= (a >> 47);
51 0 uint64_t b = (x.hi ^ a) * kMul;
52 0 b ^= (b >> 47);
53 0 b *= kMul;
54 0 return b;
55 }
56  
57 0 static inline uint64_t ch_hash_len16(uint64_t u, uint64_t v) {
58 0 ch_uint128_t x = { u, v };
59 0 return ch_hash128to64(x);
60 }
61  
62 0 static inline uint64_t ch_hash_len0to16(const char *s, size_t len) {
63 0 ▶ if (len > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 8) { 0 0 0
64 0 uint64_t a = ch_fetch64(s);
65 0 uint64_t b = ch_fetch64(s + len - 8);
66 0 return ch_hash_len16(a, ch_rotate(b + len, (int)len)) ^ b;
67 }
68 0 ▶ if (len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 4) { 0 0 0
69 0 uint64_t a = ch_fetch32(s);
70 0 return ch_hash_len16(len + (a << 3), ch_fetch32(s + len - 4));
71 }
72 0 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 0 0 0
73 0 uint8_t a = (uint8_t)s[0];
74 0 uint8_t b = (uint8_t)s[len >> 1];
75 0 uint8_t c = (uint8_t)s[len - 1];
76 0 uint32_t y = (uint32_t)a + ((uint32_t)b << 8);
77 0 uint32_t z = (uint32_t)len + ((uint32_t)c << 2);
78 0 return ch_shift_mix(y * ch_k2 ^ z * ch_k3) * ch_k2;
79 }
80 0 return ch_k2;
81 }
82  
83 0 static inline void ch_weak_hash_len32_with_seeds_vals(
84 uint64_t w, uint64_t x, uint64_t y, uint64_t z,
85 uint64_t a, uint64_t b,
86 uint64_t *out_lo, uint64_t *out_hi) {
87 0 a += w;
88 0 b = ch_rotate(b + a + z, 21);
89 0 uint64_t c = a;
90 0 a += x;
91 0 a += y;
92 0 b += ch_rotate(a, 44);
93 0 *out_lo = a + z;
94 0 *out_hi = b + c;
95 0 }
96  
97 0 static inline void ch_weak_hash_len32_with_seeds(
98 const char *s, uint64_t a, uint64_t b,
99 uint64_t *out_lo, uint64_t *out_hi) {
100 0 ch_weak_hash_len32_with_seeds_vals(
101 ch_fetch64(s), ch_fetch64(s + 8),
102 ch_fetch64(s + 16), ch_fetch64(s + 24),
103 a, b, out_lo, out_hi);
104 0 }
105  
106 0 static ch_uint128_t ch_city_murmur(const char *s, size_t len, ch_uint128_t seed) {
107 0 uint64_t a = seed.lo;
108 0 uint64_t b = seed.hi;
109 0 uint64_t c = 0;
110 0 uint64_t d = 0;
111  
112 0 ▶ if (len <= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 16) { 0 0 0
113 0 a = ch_shift_mix(a * ch_k1) * ch_k1;
114 0 c = b * ch_k1 + ch_hash_len0to16(s, len);
115 0 ▶ d = ch_shift_mix(a + (len >= 8 ? ch_fetch64(s) : c));
Branch: 1 branch
BranchTrueFalseTotal
d = ch_shift_mix(a + (len >= 8 ? ch_fetch64(s) : c)); 0 0 0
116 } else {
117 0 c = ch_hash_len16(ch_fetch64(s + len - 8) + ch_k1, a);
118 0 d = ch_hash_len16(b + len, c + ch_fetch64(s + len - 16));
119 0 a += d;
120 do {
121 0 a ^= ch_shift_mix(ch_fetch64(s) * ch_k1) * ch_k1;
122 0 a *= ch_k1;
123 0 b ^= a;
124 0 c ^= ch_shift_mix(ch_fetch64(s + 8) * ch_k1) * ch_k1;
125 0 c *= ch_k1;
126 0 d ^= c;
127 0 s += 16;
128 0 len -= 16;
129 0 ▶ } while (len > 16);
Branch: 1 branch
BranchTrueFalseTotal
} while (len > 16); 0 0 0
130 }
131 0 a = ch_hash_len16(a, c);
132 0 b = ch_hash_len16(d, b);
133  
134 0 ch_uint128_t r = { a ^ b, ch_hash_len16(b, a) };
135 0 return r;
136 }
137  
138 0 static ch_uint128_t ch_city_hash128_with_seed(const char *s, size_t len, ch_uint128_t seed) {
139 0 ▶ if (len < 128) return ch_city_murmur(s, len, seed);
Branch: 1 branch
BranchTrueFalseTotal
if (len < 128) return ch_city_murmur(s, len, seed); 0 0 0
140  
141 0 uint64_t x = seed.lo;
142 0 uint64_t y = seed.hi;
143 0 uint64_t z = (uint64_t)len * ch_k1;
144 uint64_t vlo, vhi, wlo, whi;
145 0 vlo = ch_rotate(y ^ ch_k1, 49) * ch_k1 + ch_fetch64(s);
146 0 vhi = ch_rotate(vlo, 42) * ch_k1 + ch_fetch64(s + 8);
147 0 wlo = ch_rotate(y + z, 35) * ch_k1 + x;
148 0 whi = ch_rotate(x + ch_fetch64(s + 88), 53) * ch_k1;
149  
150 do {
151 0 x = ch_rotate(x + y + vlo + ch_fetch64(s + 16), 37) * ch_k1;
152 0 y = ch_rotate(y + vhi + ch_fetch64(s + 48), 42) * ch_k1;
153 0 x ^= whi;
154 0 y ^= vlo;
155 0 z = ch_rotate(z ^ wlo, 33);
156 0 ch_weak_hash_len32_with_seeds(s, vhi * ch_k1, x + wlo, &vlo, &vhi);
157 0 ch_weak_hash_len32_with_seeds(s + 32, z + whi, y, &wlo, &whi);
158 0 { uint64_t tmp = z; z = x; x = tmp; }
159 0 s += 64;
160  
161 0 x = ch_rotate(x + y + vlo + ch_fetch64(s + 16), 37) * ch_k1;
162 0 y = ch_rotate(y + vhi + ch_fetch64(s + 48), 42) * ch_k1;
163 0 x ^= whi;
164 0 y ^= vlo;
165 0 z = ch_rotate(z ^ wlo, 33);
166 0 ch_weak_hash_len32_with_seeds(s, vhi * ch_k1, x + wlo, &vlo, &vhi);
167 0 ch_weak_hash_len32_with_seeds(s + 32, z + whi, y, &wlo, &whi);
168 0 { uint64_t tmp = z; z = x; x = tmp; }
169 0 s += 64;
170  
171 0 len -= 128;
172 0 ▶ } while (len >= 128);
Branch: 1 branch
BranchTrueFalseTotal
} while (len >= 128); 0 0 0
173  
174 0 y += ch_rotate(wlo, 37) * ch_k0 + z;
175 0 x += ch_rotate(vlo + z, 49) * ch_k0;
176  
177 size_t tail_done;
178 0 ▶ for (tail_done = 0; tail_done < len; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (tail_done = 0; tail_done < len; ) { 0 0 0
179 0 tail_done += 32;
180 0 y = ch_rotate(y - x, 42) * ch_k0 + vhi;
181 0 wlo += ch_fetch64(s + len - tail_done + 16);
182 0 x = ch_rotate(x, 49) * ch_k0 + wlo;
183 0 wlo += vlo;
184 0 ch_weak_hash_len32_with_seeds(s + len - tail_done, vlo, vhi, &vlo, &vhi);
185 }
186  
187 0 x = ch_hash_len16(x, vlo);
188 0 y = ch_hash_len16(y, wlo);
189  
190 0 ch_uint128_t r = {
191 0 ch_hash_len16(x + vhi, whi) + y,
192 0 ch_hash_len16(x + whi, y + vhi)
193 };
194 0 return r;
195 }
196  
197 0 static ch_uint128_t ch_city_hash128(const char *s, size_t len) {
198 0 ▶ if (len >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 16) { 0 0 0
199 0 ch_uint128_t seed = { ch_fetch64(s) ^ ch_k3, ch_fetch64(s + 8) };
200 0 return ch_city_hash128_with_seed(s + 16, len - 16, seed);
201 }
202 0 ▶ if (len >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 8) { 0 0 0
203 0 ch_uint128_t seed = { ch_fetch64(s) ^ ((uint64_t)len * ch_k0), ch_fetch64(s + len - 8) ^ ch_k1 };
204 0 return ch_city_hash128_with_seed(NULL, 0, seed);
205 }
206 0 ch_uint128_t seed = { ch_k0, ch_k1 };
207 0 return ch_city_hash128_with_seed(s, len, seed);
208 }
209  
210 #endif /* CITYHASH_H */
« blib/lib/EV/ClickHouse.pm ↑ Summary xs/lz4.c »