/bitcoin/src/leveldb/util/hash.cc
Line | Count | Source |
1 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
4 | | |
5 | | #include "util/hash.h" |
6 | | |
7 | | #include <string.h> |
8 | | |
9 | | #include "util/coding.h" |
10 | | |
11 | | namespace leveldb { |
12 | | |
13 | 4.16k | uint32_t Hash(const char* data, size_t n, uint32_t seed) { |
14 | | // Similar to murmur hash |
15 | 4.16k | const uint32_t m = 0xc6a4a793; |
16 | 4.16k | const uint32_t r = 24; |
17 | 4.16k | const char* limit = data + n; |
18 | 4.16k | uint32_t h = seed ^ (n * m); |
19 | | |
20 | | // Pick up four bytes at a time |
21 | 33.9k | while (limit - data >= 4) { Branch (21:10): [True: 29.8k, False: 4.16k]
|
22 | 29.8k | uint32_t w = DecodeFixed32(data); |
23 | 29.8k | data += 4; |
24 | 29.8k | h += w; |
25 | 29.8k | h *= m; |
26 | 29.8k | h ^= (h >> 16); |
27 | 29.8k | } |
28 | | |
29 | | // Pick up remaining bytes |
30 | 4.16k | switch (limit - data) { Branch (30:11): [True: 458, False: 3.71k]
|
31 | 16 | case 3: Branch (31:5): [True: 16, False: 4.15k]
|
32 | 16 | h += static_cast<uint8_t>(data[2]) << 16; |
33 | 16 | [[fallthrough]]; |
34 | 3.61k | case 2: Branch (34:5): [True: 3.60k, False: 566]
|
35 | 3.61k | h += static_cast<uint8_t>(data[1]) << 8; |
36 | 3.61k | [[fallthrough]]; |
37 | 3.71k | case 1: Branch (37:5): [True: 92, False: 4.07k]
|
38 | 3.71k | h += static_cast<uint8_t>(data[0]); |
39 | 3.71k | h *= m; |
40 | 3.71k | h ^= (h >> r); |
41 | 3.71k | break; |
42 | 4.16k | } |
43 | 4.16k | return h; |
44 | 4.16k | } |
45 | | |
46 | | } // namespace leveldb |