/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/hash.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | |
24 | | #include <string.h> |
25 | | #include "yb/rocksdb/util/coding.h" |
26 | | #include "yb/rocksdb/util/hash.h" |
27 | | |
28 | | #include "yb/gutil/macros.h" |
29 | | |
30 | | namespace rocksdb { |
31 | | |
32 | 225M | uint32_t Hash(const char* data, size_t n, uint32_t seed) { |
33 | | // Similar to murmur hash |
34 | 225M | const uint32_t m = 0xc6a4a793; |
35 | 225M | const uint32_t r = 24; |
36 | 225M | const char* limit = data + n; |
37 | 225M | uint32_t h = static_cast<uint32_t>(seed ^ (n * m)); |
38 | | |
39 | | // Pick up four bytes at a time |
40 | 828M | while (data + 4 <= limit) { |
41 | 602M | uint32_t w = DecodeFixed32(data); |
42 | 602M | data += 4; |
43 | 602M | h += w; |
44 | 602M | h *= m; |
45 | 602M | h ^= (h >> 16); |
46 | 602M | } |
47 | | |
48 | | // Pick up remaining bytes |
49 | 225M | switch (limit - data) { |
50 | | // Note: It would be better if this was cast to unsigned char, but that |
51 | | // would be a disk format change since we previously didn't have any cast |
52 | | // at all (so gcc used signed char). |
53 | | // To understand the difference between shifting unsigned and signed chars, |
54 | | // let's use 250 as an example. unsigned char will be 250, while signed char |
55 | | // will be -6. Bit-wise, they are equivalent: 11111010. However, when |
56 | | // converting negative number (signed char) to int, it will be converted |
57 | | // into negative int (of equivalent value, which is -6), while converting |
58 | | // positive number (unsigned char) will be converted to 250. Bitwise, |
59 | | // this looks like this: |
60 | | // signed char 11111010 -> int 11111111111111111111111111111010 |
61 | | // unsigned char 11111010 -> int 00000000000000000000000011111010 |
62 | 17.2M | case 3: |
63 | 17.2M | h += static_cast<uint32_t>(static_cast<signed char>(data[2]) << 16); |
64 | 17.2M | FALLTHROUGH_INTENDED; |
65 | 49.4M | case 2: |
66 | 49.4M | h += static_cast<uint32_t>(static_cast<signed char>(data[1]) << 8); |
67 | 49.4M | FALLTHROUGH_INTENDED; |
68 | 103M | case 1: |
69 | 103M | h += static_cast<uint32_t>(static_cast<signed char>(data[0])); |
70 | 103M | h *= m; |
71 | 103M | h ^= (h >> r); |
72 | 103M | break; |
73 | 225M | } |
74 | 228M | return h; |
75 | 225M | } |
76 | | |
77 | | } // namespace rocksdb |