Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/xxhash.cpp
Line
Count
Source
1
/*
2
*  xxHash - Fast Hash algorithm
3
*  Copyright (C) 2012-2016, Yann Collet
4
*
5
*  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
*
7
*  Redistribution and use in source and binary forms, with or without
8
*  modification, are permitted provided that the following conditions are
9
*  met:
10
*
11
*  * Redistributions of source code must retain the above copyright
12
*  notice, this list of conditions and the following disclaimer.
13
*  * Redistributions in binary form must reproduce the above
14
*  copyright notice, this list of conditions and the following disclaimer
15
*  in the documentation and/or other materials provided with the
16
*  distribution.
17
*
18
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
*
30
*  You can contact the author at :
31
*  - xxHash homepage: http://www.xxhash.com
32
*  - xxHash source repository : https://github.com/Cyan4973/xxHash
33
*/
34
35
/* based on revision d2df04efcbef7d7f6886d345861e5dfda4edacc1 Removed
36
 * everything but a simple interface for computing XXh64. */
37
38
#include "llvm/Support/xxhash.h"
39
#include "llvm/Support/Endian.h"
40
41
#include <stdlib.h>
42
#include <string.h>
43
44
using namespace llvm;
45
using namespace support;
46
47
1.02M
static uint64_t rotl64(uint64_t X, size_t R) {
48
1.02M
  return (X << R) | (X >> (64 - R));
49
1.02M
}
50
51
static const uint64_t PRIME64_1 = 11400714785074694791ULL;
52
static const uint64_t PRIME64_2 = 14029467366897019727ULL;
53
static const uint64_t PRIME64_3 = 1609587929392839161ULL;
54
static const uint64_t PRIME64_4 = 9650029242287828579ULL;
55
static const uint64_t PRIME64_5 = 2870177450012600261ULL;
56
57
1.00M
static uint64_t round(uint64_t Acc, uint64_t Input) {
58
1.00M
  Acc += Input * PRIME64_2;
59
1.00M
  Acc = rotl64(Acc, 31);
60
1.00M
  Acc *= PRIME64_1;
61
1.00M
  return Acc;
62
1.00M
}
63
64
8.24k
static uint64_t mergeRound(uint64_t Acc, uint64_t Val) {
65
8.24k
  Val = round(0, Val);
66
8.24k
  Acc ^= Val;
67
8.24k
  Acc = Acc * PRIME64_1 + PRIME64_4;
68
8.24k
  return Acc;
69
8.24k
}
70
71
7.49k
uint64_t llvm::xxHash64(StringRef Data) {
72
7.49k
  size_t Len = Data.size();
73
7.49k
  uint64_t Seed = 0;
74
7.49k
  const unsigned char *P = Data.bytes_begin();
75
7.49k
  const unsigned char *const BEnd = Data.bytes_end();
76
7.49k
  uint64_t H64;
77
7.49k
78
7.49k
  if (Len >= 32) {
79
2.06k
    const unsigned char *const Limit = BEnd - 32;
80
2.06k
    uint64_t V1 = Seed + PRIME64_1 + PRIME64_2;
81
2.06k
    uint64_t V2 = Seed + PRIME64_2;
82
2.06k
    uint64_t V3 = Seed + 0;
83
2.06k
    uint64_t V4 = Seed - PRIME64_1;
84
2.06k
85
247k
    do {
86
247k
      V1 = round(V1, endian::read64le(P));
87
247k
      P += 8;
88
247k
      V2 = round(V2, endian::read64le(P));
89
247k
      P += 8;
90
247k
      V3 = round(V3, endian::read64le(P));
91
247k
      P += 8;
92
247k
      V4 = round(V4, endian::read64le(P));
93
247k
      P += 8;
94
247k
    } while (P <= Limit);
95
2.06k
96
2.06k
    H64 = rotl64(V1, 1) + rotl64(V2, 7) + rotl64(V3, 12) + rotl64(V4, 18);
97
2.06k
    H64 = mergeRound(H64, V1);
98
2.06k
    H64 = mergeRound(H64, V2);
99
2.06k
    H64 = mergeRound(H64, V3);
100
2.06k
    H64 = mergeRound(H64, V4);
101
2.06k
102
5.43k
  } else {
103
5.43k
    H64 = Seed + PRIME64_5;
104
5.43k
  }
105
7.49k
106
7.49k
  H64 += (uint64_t)Len;
107
7.49k
108
13.2k
  while (P + 8 <= BEnd) {
109
5.78k
    uint64_t const K1 = round(0, endian::read64le(P));
110
5.78k
    H64 ^= K1;
111
5.78k
    H64 = rotl64(H64, 27) * PRIME64_1 + PRIME64_4;
112
5.78k
    P += 8;
113
5.78k
  }
114
7.49k
115
7.49k
  if (P + 4 <= BEnd) {
116
1.33k
    H64 ^= (uint64_t)(endian::read32le(P)) * PRIME64_1;
117
1.33k
    H64 = rotl64(H64, 23) * PRIME64_2 + PRIME64_3;
118
1.33k
    P += 4;
119
1.33k
  }
120
7.49k
121
9.21k
  while (P < BEnd) {
122
1.72k
    H64 ^= (*P) * PRIME64_5;
123
1.72k
    H64 = rotl64(H64, 11) * PRIME64_1;
124
1.72k
    P++;
125
1.72k
  }
126
7.49k
127
7.49k
  H64 ^= H64 >> 33;
128
7.49k
  H64 *= PRIME64_2;
129
7.49k
  H64 ^= H64 >> 29;
130
7.49k
  H64 *= PRIME64_3;
131
7.49k
  H64 ^= H64 >> 32;
132
7.49k
133
7.49k
  return H64;
134
7.49k
}
135
136
3.98k
uint64_t llvm::xxHash64(ArrayRef<uint8_t> Data) {
137
3.98k
  return xxHash64({(const char *)Data.data(), Data.size()});
138
3.98k
}