YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
1.38k
  static uint64_t MurmurHash2_64(const void* input, size_t len, uint64_t seed) {
49
1.38k
    uint64_t h = seed ^ (len * MURMUR_PRIME);
50
51
1.38k
    const uint64_t* data = reinterpret_cast<const uint64_t*>(input);
52
1.38k
    const uint64_t* end = data + (len / sizeof(uint64_t));
53
54
2.75k
    while (data != end) {
55
1.37k
      uint64_t k = *data++;
56
1.37k
      k *= MURMUR_PRIME;
57
1.37k
      k ^= k >> MURMUR_R;
58
1.37k
      k *= MURMUR_PRIME;
59
1.37k
      h ^= k;
60
1.37k
      h *= MURMUR_PRIME;
61
1.37k
    }
62
63
1.38k
    const uint8_t* data2 = reinterpret_cast<const uint8_t*>(data);
64
1.38k
    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
782
      case 3: h ^= static_cast<uint64_t>(data2[2]) << 16; FALLTHROUGH_INTENDED;
70
1.32k
      case 2: h ^= static_cast<uint64_t>(data2[1]) << 8; FALLTHROUGH_INTENDED;
71
1.38k
      case 1: h ^= static_cast<uint64_t>(data2[0]);
72
1.38k
              h *= MURMUR_PRIME;
73
1.38k
    }
74
75
1.38k
    h ^= h >> MURMUR_R;
76
1.38k
    h *= MURMUR_PRIME;
77
1.38k
    h ^= h >> MURMUR_R;
78
1.38k
    return h;
79
1.38k
  }
80
};
81
82
} // namespace yb
83
84
#endif // YB_UTIL_HASH_UTIL_H