/Users/deen/code/yugabyte-db/src/yb/util/hash_util.h
Line | Count | Source (jump to first uncovered line) |
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 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <stdint.h> |
34 | | |
35 | | #ifndef YB_UTIL_HASH_UTIL_H |
36 | | #define YB_UTIL_HASH_UTIL_H |
37 | | |
38 | | namespace yb { |
39 | | |
40 | | /// Utility class to compute hash values. |
41 | | class HashUtil { |
42 | | public: |
43 | | |
44 | | static const uint64_t MURMUR_PRIME = 0xc6a4a7935bd1e995; |
45 | | static const int MURMUR_R = 47; |
46 | | |
47 | | /// Murmur2 hash implementation returning 64-bit hashes. |
48 | 5.88k | static uint64_t MurmurHash2_64(const void* input, size_t len, uint64_t seed) { |
49 | 5.88k | uint64_t h = seed ^ (len * MURMUR_PRIME); |
50 | | |
51 | 5.88k | const uint64_t* data = reinterpret_cast<const uint64_t*>(input); |
52 | 5.88k | const uint64_t* end = data + (len / sizeof(uint64_t)); |
53 | | |
54 | 11.7k | while (data != end) { |
55 | 5.88k | uint64_t k = *data++; |
56 | 5.88k | k *= MURMUR_PRIME; |
57 | 5.88k | k ^= k >> MURMUR_R; |
58 | 5.88k | k *= MURMUR_PRIME; |
59 | 5.88k | h ^= k; |
60 | 5.88k | h *= MURMUR_PRIME; |
61 | 5.88k | } |
62 | | |
63 | 5.88k | const uint8_t* data2 = reinterpret_cast<const uint8_t*>(data); |
64 | 5.88k | switch (len & 7) { |
65 | 2 | case 7: h ^= static_cast<uint64_t>(data2[6]) << 48; FALLTHROUGH_INTENDED; |
66 | 2 | case 6: h ^= static_cast<uint64_t>(data2[5]) << 40; FALLTHROUGH_INTENDED; |
67 | 2 | case 5: h ^= static_cast<uint64_t>(data2[4]) << 32; FALLTHROUGH_INTENDED; |
68 | 2 | case 4: h ^= static_cast<uint64_t>(data2[3]) << 24; FALLTHROUGH_INTENDED; |
69 | 2.86k | case 3: h ^= static_cast<uint64_t>(data2[2]) << 16; FALLTHROUGH_INTENDED; |
70 | 4.34k | case 2: h ^= static_cast<uint64_t>(data2[1]) << 8; FALLTHROUGH_INTENDED; |
71 | 5.88k | case 1: h ^= static_cast<uint64_t>(data2[0]); |
72 | 5.88k | h *= MURMUR_PRIME; |
73 | 5.88k | } |
74 | | |
75 | 5.88k | h ^= h >> MURMUR_R; |
76 | 5.88k | h *= MURMUR_PRIME; |
77 | 5.88k | h ^= h >> MURMUR_R; |
78 | 5.88k | return h; |
79 | 5.88k | } |
80 | | }; |
81 | | |
82 | | } // namespace yb |
83 | | |
84 | | #endif // YB_UTIL_HASH_UTIL_H |