/root/doris/be/src/util/hash/city.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | // Copyright 2010 Google Inc. All Rights Reserved. |
19 | | // Authors: gpike@google.com (Geoff Pike), jyrki@google.com (Jyrki Alakuijala) |
20 | | // |
21 | | // This file provides CityHash64() and related functions. |
22 | | // |
23 | | // The externally visible functions follow the naming conventions of |
24 | | // hash.h, where the size of the output is part of the name. For |
25 | | // example, CityHash64 returns a 64-bit hash. The internal helpers do |
26 | | // not have the return type in their name, but instead have names like |
27 | | // HashLenXX or HashLenXXtoYY, where XX and YY are input string lengths. |
28 | | // |
29 | | // Most of the constants and tricks here were copied from murmur.cc or |
30 | | // hash.h, or discovered by trial and error. It's probably possible to further |
31 | | // optimize the code here by writing a program that systematically explores |
32 | | // more of the space of possible hash functions, or by using SIMD instructions. |
33 | | |
34 | | #include "util/hash/city.h" |
35 | | |
36 | | #include <algorithm> |
37 | | #include <utility> |
38 | | |
39 | | #include "common/logging.h" |
40 | | #include "vec/common/endian.h" |
41 | | |
42 | | namespace doris::util_hash { |
43 | | #include "common/compile_check_begin.h" |
44 | | // Some primes between 2^63 and 2^64 for various uses. |
45 | | static const uint64_t k0 = 0xa5b85c5e198ed849ULL; |
46 | | static const uint64_t k1 = 0x8d58ac26afe12e47ULL; |
47 | | static const uint64_t k2 = 0xc47b6e9e3a970ed3ULL; |
48 | | static const uint64_t k3 = 0xc70f6907e782aa0bULL; |
49 | | |
50 | | // Bitwise right rotate. Normally this will compile to a single |
51 | | // instruction, especially if the shift is a manifest constant. |
52 | 2.34k | static uint64_t Rotate(uint64_t val, int shift) { |
53 | 2.34k | DCHECK_GE(shift, 0); |
54 | 2.34k | DCHECK_LE(shift, 63); |
55 | | // Avoid shifting by 64: doing so yields an undefined result. |
56 | 2.34k | return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); |
57 | 2.34k | } |
58 | | |
59 | | // Equivalent to Rotate(), but requires the second arg to be non-zero. |
60 | | // On x86-64, and probably others, it's possible for this to compile |
61 | | // to a single instruction if both args are already in registers. |
62 | 18 | static uint64_t RotateByAtLeast1(uint64_t val, int shift) { |
63 | 18 | DCHECK_GE(shift, 1); |
64 | 18 | DCHECK_LE(shift, 63); |
65 | 18 | return (val >> shift) | (val << (64 - shift)); |
66 | 18 | } |
67 | | |
68 | 38 | static uint64_t ShiftMix(uint64_t val) { |
69 | 38 | return val ^ (val >> 47); |
70 | 38 | } |
71 | | |
72 | 1.19M | uint64_t HashLen16(uint64_t u, uint64_t v) { |
73 | 1.19M | const uint64_t kMul = 0xc6a4a7935bd1e995ULL; |
74 | 1.19M | uint64_t a = (u ^ v) * kMul; |
75 | 1.19M | a ^= (a >> 47); |
76 | 1.19M | uint64_t b = (u ^ a) * kMul; |
77 | 1.19M | b ^= (b >> 47); |
78 | 1.19M | b *= kMul; |
79 | 1.19M | return b; |
80 | 1.19M | } |
81 | | |
82 | 128k | static uint64_t HashLen0to16(const char* s, size_t len) { |
83 | 128k | DCHECK_GE(len, 0); |
84 | 128k | DCHECK_LE(len, 16); |
85 | 128k | if (len > 8) { |
86 | 18 | uint64_t a = LittleEndian::Load64(s); |
87 | 18 | uint64_t b = LittleEndian::Load64(s + len - 8); |
88 | 18 | return HashLen16(a, RotateByAtLeast1(b + len, int(len))) ^ b; |
89 | 18 | } |
90 | 128k | if (len >= 4) { |
91 | 128k | uint64_t a = LittleEndian::Load32(s); |
92 | 128k | return HashLen16(len + (a << 3), LittleEndian::Load32(s + len - 4)); |
93 | 128k | } |
94 | 18 | if (len > 0) { |
95 | 18 | uint8_t a = s[0]; |
96 | 18 | uint8_t b = s[len >> 1]; |
97 | 18 | uint8_t c = s[len - 1]; |
98 | 18 | uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8); |
99 | 18 | uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2); |
100 | 18 | return ShiftMix(y * k2 ^ z * k3) * k2; |
101 | 18 | } |
102 | 0 | return k2; |
103 | 18 | } |
104 | | |
105 | | // This probably works well for 16-byte strings as well, but it may be overkill |
106 | | // in that case. |
107 | 2 | static uint64_t HashLen17to32(const char* s, size_t len) { |
108 | 2 | DCHECK_GE(len, 17); |
109 | 2 | DCHECK_LE(len, 32); |
110 | 2 | uint64_t a = LittleEndian::Load64(s) * k1; |
111 | 2 | uint64_t b = LittleEndian::Load64(s + 8); |
112 | 2 | uint64_t c = LittleEndian::Load64(s + len - 8) * k2; |
113 | 2 | uint64_t d = LittleEndian::Load64(s + len - 16) * k0; |
114 | 2 | return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d, a + Rotate(b ^ k3, 20) - c + len); |
115 | 2 | } |
116 | | |
117 | | // Return a 16-byte hash for 48 bytes. Quick and dirty. |
118 | | // Callers do best to use "random-looking" values for a and b. |
119 | | // (For more, see the code review discussion of CL 18799087.) |
120 | | static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(uint64_t w, uint64_t x, uint64_t y, |
121 | 684 | uint64_t z, uint64_t a, uint64_t b) { |
122 | 684 | a += w; |
123 | 684 | b = Rotate(b + a + z, 51); |
124 | 684 | uint64_t c = a; |
125 | 684 | a += x; |
126 | 684 | a += y; |
127 | 684 | b += Rotate(a, 23); |
128 | 684 | return std::make_pair(a + z, b + c); |
129 | 684 | } |
130 | | |
131 | | // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. |
132 | 684 | static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(const char* s, uint64_t a, uint64_t b) { |
133 | 684 | return WeakHashLen32WithSeeds(LittleEndian::Load64(s), LittleEndian::Load64(s + 8), |
134 | 684 | LittleEndian::Load64(s + 16), LittleEndian::Load64(s + 24), a, b); |
135 | 684 | } |
136 | | |
137 | | // Return an 8-byte hash for 33 to 64 bytes. |
138 | 0 | static uint64_t HashLen33to64(const char* s, size_t len) { |
139 | 0 | uint64_t z = LittleEndian::Load64(s + 24); |
140 | 0 | uint64_t a = LittleEndian::Load64(s) + (len + LittleEndian::Load64(s + len - 16)) * k0; |
141 | 0 | uint64_t b = Rotate(a + z, 52); |
142 | 0 | uint64_t c = Rotate(a, 37); |
143 | 0 | a += LittleEndian::Load64(s + 8); |
144 | 0 | c += Rotate(a, 7); |
145 | 0 | a += LittleEndian::Load64(s + 16); |
146 | 0 | uint64_t vf = a + z; |
147 | 0 | uint64_t vs = b + Rotate(a, 31) + c; |
148 | 0 | a = LittleEndian::Load64(s + 16) + LittleEndian::Load64(s + len - 32); |
149 | 0 | z += LittleEndian::Load64(s + len - 8); |
150 | 0 | b = Rotate(a + z, 52); |
151 | 0 | c = Rotate(a, 37); |
152 | 0 | a += LittleEndian::Load64(s + len - 24); |
153 | 0 | c += Rotate(a, 7); |
154 | 0 | a += LittleEndian::Load64(s + len - 16); |
155 | 0 | uint64_t wf = a + z; |
156 | 0 | uint64_t ws = b + Rotate(a, 31) + c; |
157 | 0 | uint64_t r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0); |
158 | 0 | return ShiftMix(r * k0 + vs) * k2; |
159 | 0 | } |
160 | | |
161 | 128k | uint64_t CityHash64(const char* s, size_t len) { |
162 | 128k | if (len <= 32) { |
163 | 128k | if (len <= 16) { |
164 | 128k | return HashLen0to16(s, len); |
165 | 128k | } else { |
166 | 2 | return HashLen17to32(s, len); |
167 | 2 | } |
168 | 128k | } else if (len <= 64) { |
169 | 0 | return HashLen33to64(s, len); |
170 | 0 | } |
171 | | |
172 | | // For strings over 64 bytes we hash the end first, and then as we |
173 | | // loop we keep 56 bytes of state: v, w, x, y, and z. |
174 | 20 | uint64_t x = LittleEndian::Load64(s + len - 40); |
175 | 20 | uint64_t y = LittleEndian::Load64(s + len - 16) + LittleEndian::Load64(s + len - 56); |
176 | 20 | uint64_t z = |
177 | 20 | HashLen16(LittleEndian::Load64(s + len - 48) + len, LittleEndian::Load64(s + len - 24)); |
178 | 20 | std::pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(s + len - 64, len, z); |
179 | 20 | std::pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x); |
180 | 20 | x = x * k1 + LittleEndian::Load64(s); |
181 | | |
182 | | // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. |
183 | 20 | len = (len - 1) & ~static_cast<size_t>(63); |
184 | 20 | DCHECK_GT(len, 0); |
185 | 20 | DCHECK_EQ(len, len / 64 * 64); |
186 | 322 | do { |
187 | 322 | x = Rotate(x + y + v.first + LittleEndian::Load64(s + 8), 37) * k1; |
188 | 322 | y = Rotate(y + v.second + LittleEndian::Load64(s + 48), 42) * k1; |
189 | 322 | x ^= w.second; |
190 | 322 | y += v.first + LittleEndian::Load64(s + 40); |
191 | 322 | z = Rotate(z + w.first, 33) * k1; |
192 | 322 | v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); |
193 | 322 | w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + LittleEndian::Load64(s + 16)); |
194 | 322 | std::swap(z, x); |
195 | 322 | s += 64; |
196 | 322 | len -= 64; |
197 | 322 | } while (len != 0); |
198 | 20 | return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z, |
199 | 20 | HashLen16(v.second, w.second) + x); |
200 | 128k | } |
201 | | |
202 | 128k | uint64_t CityHash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1) { |
203 | 128k | return HashLen16(CityHash64(s, len) - seed0, seed1); |
204 | 128k | } |
205 | | |
206 | 128k | uint64_t CityHash64WithSeed(const char* s, size_t len, uint64_t seed) { |
207 | 128k | return CityHash64WithSeeds(s, len, k2, seed); |
208 | 128k | } |
209 | | } // namespace doris::util_hash |