Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
12
// between switch labels. The real definition should be provided externally.
13
// This one is a fallback version for unsupported compilers.
14
#ifndef FALLTHROUGH_INTENDED
15
#define FALLTHROUGH_INTENDED \
16
  do {                       \
17
  } while (0)
18
#endif
19
20
namespace leveldb {
21
22
11.8k
uint32_t Hash(const char* data, size_t n, uint32_t seed) {
23
  // Similar to murmur hash
24
11.8k
  const uint32_t m = 0xc6a4a793;
25
11.8k
  const uint32_t r = 24;
26
11.8k
  const char* limit = data + n;
27
11.8k
  uint32_t h = seed ^ (n * m);
28
29
  // Pick up four bytes at a time
30
18.0k
  while (limit - data >= 4) {
  Branch (30:10): [True: 6.21k, False: 11.8k]
31
6.21k
    uint32_t w = DecodeFixed32(data);
32
6.21k
    data += 4;
33
6.21k
    h += w;
34
6.21k
    h *= m;
35
6.21k
    h ^= (h >> 16);
36
6.21k
  }
37
38
  // Pick up remaining bytes
39
11.8k
  switch (limit - data) {
  Branch (39:11): [True: 72, False: 11.7k]
40
0
    case 3:
  Branch (40:5): [True: 0, False: 11.8k]
41
0
      h += static_cast<uint8_t>(data[2]) << 16;
42
0
      FALLTHROUGH_INTENDED;
43
0
    case 2:
  Branch (43:5): [True: 0, False: 11.8k]
44
0
      h += static_cast<uint8_t>(data[1]) << 8;
45
0
      FALLTHROUGH_INTENDED;
46
11.7k
    case 1:
  Branch (46:5): [True: 11.7k, False: 72]
47
11.7k
      h += static_cast<uint8_t>(data[0]);
48
11.7k
      h *= m;
49
11.7k
      h ^= (h >> r);
50
11.7k
      break;
51
11.8k
  }
52
11.8k
  return h;
53
11.8k
}
54
55
}  // namespace leveldb