forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hash.cc
75 lines (65 loc) · 2.04 KB
/
hash.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/base/hash.h"
namespace operations_research {
// This is a copy of the code from https://github.com/ztanml/fast-hash
// We include it here for build simplicity.
//
// We have made 2 modification:
// - replace the #define with an inline method to fix compilation on windows.
// - rename mix into mix_internal and hide inside a unnamed namespace.
namespace {
// Compression function for Merkle-Damgard construction.
// This function is generated using the framework provided.
inline uint64_t mix_internal(uint64_t h) {
h ^= h >> 23;
h *= 0x2127599bf4325c37ULL;
h ^= h >> 47;
return h;
}
} // namespace
uint64_t fasthash64(const void* buf, size_t len, uint64_t seed) {
const uint64_t m = 0x880355f21e6d1965ULL;
const uint64_t* pos = (const uint64_t*)buf;
const uint64_t* end = pos + (len / 8);
const unsigned char* pos2;
uint64_t h = seed ^ (len * m);
uint64_t v;
while (pos != end) {
v = *pos++;
h ^= mix_internal(v);
h *= m;
}
pos2 = (const unsigned char*)pos;
v = 0;
switch (len & 7) {
case 7:
v ^= (uint64_t)pos2[6] << 48;
case 6:
v ^= (uint64_t)pos2[5] << 40;
case 5:
v ^= (uint64_t)pos2[4] << 32;
case 4:
v ^= (uint64_t)pos2[3] << 24;
case 3:
v ^= (uint64_t)pos2[2] << 16;
case 2:
v ^= (uint64_t)pos2[1] << 8;
case 1:
v ^= (uint64_t)pos2[0];
h ^= mix_internal(v);
h *= m;
}
return mix_internal(h);
}
} // namespace operations_research