Coverage Report

Created: 2024-11-22 11:49

/root/doris/be/src/gutil/hash/city.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2010 Google Inc. All Rights Reserved.
2
// Authors: gpike@google.com (Geoff Pike), jyrki@google.com (Jyrki Alakuijala)
3
//
4
// This file provides CityHash64() and related functions.
5
//
6
// The externally visible functions follow the naming conventions of
7
// hash.h, where the size of the output is part of the name.  For
8
// example, CityHash64 returns a 64-bit hash.  The internal helpers do
9
// not have the return type in their name, but instead have names like
10
// HashLenXX or HashLenXXtoYY, where XX and YY are input string lengths.
11
//
12
// Most of the constants and tricks here were copied from murmur.cc or
13
// hash.h, or discovered by trial and error.  It's probably possible to further
14
// optimize the code here by writing a program that systematically explores
15
// more of the space of possible hash functions, or by using SIMD instructions.
16
17
#include "gutil/hash/city.h"
18
19
// IWYU pragma: no_include <pstl/glue_algorithm_defs.h>
20
21
#include <sys/types.h>
22
23
#include <algorithm>
24
#include <iterator>
25
26
using std::copy;
27
using std::max;
28
using std::min;
29
using std::reverse;
30
using std::sort;
31
using std::swap;
32
#include <utility>
33
34
using std::make_pair;
35
using std::pair;
36
37
#include "common/logging.h"
38
#include "gutil/endian.h"
39
#include "gutil/integral_types.h"
40
#include "gutil/port.h"
41
42
namespace util_hash {
43
44
// Some primes between 2^63 and 2^64 for various uses.
45
static const uint64 k0 = 0xa5b85c5e198ed849ULL;
46
static const uint64 k1 = 0x8d58ac26afe12e47ULL;
47
static const uint64 k2 = 0xc47b6e9e3a970ed3ULL;
48
static const uint64 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
0
static uint64 Rotate(uint64 val, int shift) {
53
0
    DCHECK_GE(shift, 0);
54
0
    DCHECK_LE(shift, 63);
55
    // Avoid shifting by 64: doing so yields an undefined result.
56
0
    return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
57
0
}
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
0
static uint64 RotateByAtLeast1(uint64 val, int shift) {
63
0
    DCHECK_GE(shift, 1);
64
0
    DCHECK_LE(shift, 63);
65
0
    return (val >> shift) | (val << (64 - shift));
66
0
}
67
68
10
static uint64 ShiftMix(uint64 val) {
69
10
    return val ^ (val >> 47);
70
10
}
71
72
68.5k
uint64 HashLen16(uint64 u, uint64 v) {
73
68.5k
    const uint64 kMul = 0xc6a4a7935bd1e995ULL;
74
68.5k
    uint64 a = (u ^ v) * kMul;
75
68.5k
    a ^= (a >> 47);
76
68.5k
    uint64 b = (v ^ a) * kMul;
77
68.5k
    b ^= (b >> 47);
78
68.5k
    b *= kMul;
79
68.5k
    return b;
80
68.5k
}
81
82
14
static uint64 HashLen0to16(const char* s, size_t len) {
83
14
    DCHECK_GE(len, 0);
84
14
    DCHECK_LE(len, 16);
85
14
    if (len > 8) {
86
0
        uint64 a = LittleEndian::Load64(s);
87
0
        uint64 b = LittleEndian::Load64(s + len - 8);
88
0
        return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b;
89
0
    }
90
14
    if (len >= 4) {
91
4
        uint64 a = LittleEndian::Load32(s);
92
4
        return HashLen16(len + (a << 3), LittleEndian::Load32(s + len - 4));
93
4
    }
94
10
    if (len > 0) {
95
10
        uint8 a = s[0];
96
10
        uint8 b = s[len >> 1];
97
10
        uint8 c = s[len - 1];
98
10
        uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
99
10
        uint32 z = len + (static_cast<uint32>(c) << 2);
100
10
        return ShiftMix(y * k2 ^ z * k3) * k2;
101
10
    }
102
0
    return k2;
103
10
}
104
105
// This probably works well for 16-byte strings as well, but it may be overkill
106
// in that case.
107
0
static uint64 HashLen17to32(const char* s, size_t len) {
108
0
    DCHECK_GE(len, 17);
109
0
    DCHECK_LE(len, 32);
110
0
    uint64 a = LittleEndian::Load64(s) * k1;
111
0
    uint64 b = LittleEndian::Load64(s + 8);
112
0
    uint64 c = LittleEndian::Load64(s + len - 8) * k2;
113
0
    uint64 d = LittleEndian::Load64(s + len - 16) * k0;
114
0
    return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d, a + Rotate(b ^ k3, 20) - c + len);
115
0
}
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 pair<uint64, uint64> WeakHashLen32WithSeeds(uint64 w, uint64 x, uint64 y, uint64 z, uint64 a,
121
0
                                                   uint64 b) {
122
0
    a += w;
123
0
    b = Rotate(b + a + z, 51);
124
0
    uint64 c = a;
125
0
    a += x;
126
0
    a += y;
127
0
    b += Rotate(a, 23);
128
0
    return make_pair(a + z, b + c);
129
0
}
130
131
// Return a 16-byte hash for s[0] ... s[31], a, and b.  Quick and dirty.
132
0
static pair<uint64, uint64> WeakHashLen32WithSeeds(const char* s, uint64 a, uint64 b) {
133
0
    return WeakHashLen32WithSeeds(LittleEndian::Load64(s), LittleEndian::Load64(s + 8),
134
0
                                  LittleEndian::Load64(s + 16), LittleEndian::Load64(s + 24), a, b);
135
0
}
136
137
// Return an 8-byte hash for 33 to 64 bytes.
138
0
static uint64 HashLen33to64(const char* s, size_t len) {
139
0
    uint64 z = LittleEndian::Load64(s + 24);
140
0
    uint64 a = LittleEndian::Load64(s) + (len + LittleEndian::Load64(s + len - 16)) * k0;
141
0
    uint64 b = Rotate(a + z, 52);
142
0
    uint64 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 vf = a + z;
147
0
    uint64 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 wf = a + z;
156
0
    uint64 ws = b + Rotate(a, 31) + c;
157
0
    uint64 r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0);
158
0
    return ShiftMix(r * k0 + vs) * k2;
159
0
}
160
161
14
uint64 CityHash64(const char* s, size_t len) {
162
14
    if (len <= 32) {
163
14
        if (len <= 16) {
164
14
            return HashLen0to16(s, len);
165
14
        } else {
166
0
            return HashLen17to32(s, len);
167
0
        }
168
14
    } 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
0
    uint64 x = LittleEndian::Load64(s + len - 40);
175
0
    uint64 y = LittleEndian::Load64(s + len - 16) + LittleEndian::Load64(s + len - 56);
176
0
    uint64 z =
177
0
            HashLen16(LittleEndian::Load64(s + len - 48) + len, LittleEndian::Load64(s + len - 24));
178
0
    pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
179
0
    pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
180
0
    x = x * k1 + LittleEndian::Load64(s);
181
182
    // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
183
0
    len = (len - 1) & ~static_cast<size_t>(63);
184
0
    DCHECK_GT(len, 0);
185
0
    DCHECK_EQ(len, len / 64 * 64);
186
0
    do {
187
0
        x = Rotate(x + y + v.first + LittleEndian::Load64(s + 8), 37) * k1;
188
0
        y = Rotate(y + v.second + LittleEndian::Load64(s + 48), 42) * k1;
189
0
        x ^= w.second;
190
0
        y += v.first + LittleEndian::Load64(s + 40);
191
0
        z = Rotate(z + w.first, 33) * k1;
192
0
        v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
193
0
        w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + LittleEndian::Load64(s + 16));
194
0
        std::swap(z, x);
195
0
        s += 64;
196
0
        len -= 64;
197
0
    } while (len != 0);
198
0
    return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
199
0
                     HashLen16(v.second, w.second) + x);
200
14
}
201
202
14
uint64 CityHash64WithSeed(const char* s, size_t len, uint64 seed) {
203
14
    return CityHash64WithSeeds(s, len, k2, seed);
204
14
}
205
206
14
uint64 CityHash64WithSeeds(const char* s, size_t len, uint64 seed0, uint64 seed1) {
207
14
    return HashLen16(CityHash64(s, len) - seed0, seed1);
208
14
}
209
} // namespace util_hash