microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
zhanxi/debug_android_pack

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cmake/externals/farmhash/dev/farmhash.h

277lines · modecode

1// Copyright (c) 2011 Google, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20//
21// FarmHash, by Geoff Pike
22//
23// http://code.google.com/p/farmhash/
24//
25// This file provides a few functions for hashing strings and other
26// data. All of them are high-quality functions in the sense that
27// they do well on standard tests such as Austin Appleby's SMHasher.
28// They're also fast. FarmHash is the successor to CityHash.
29//
30// Functions in the FarmHash family are not suitable for cryptography.
31//
32// WARNING: This code has been only lightly tested on big-endian platforms!
33// It is known to work well on little-endian platforms that have a small penalty
34// for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
35// It should work on all 32-bit and 64-bit platforms that allow unaligned reads;
36// bug reports are welcome.
37//
38// By the way, for some hash functions, given strings a and b, the hash
39// of a+b is easily derived from the hashes of a and b. This property
40// doesn't hold for any hash functions in this file.
41
42#ifndef FARM_HASH_H_
43#define FARM_HASH_H_
44
45#include <assert.h>
46#include <stdint.h>
47#include <stdlib.h>
48#include <string.h> // for memcpy and memset
49#include <utility>
50
51#ifndef NAMESPACE_FOR_HASH_FUNCTIONS
52#define NAMESPACE_FOR_HASH_FUNCTIONS util
53#endif
54
55namespace NAMESPACE_FOR_HASH_FUNCTIONS {
56
57typedef std::pair<uint64_t, uint64_t> uint128_t;
58inline uint64_t Uint128Low64(const NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) { return x.first; }
59inline uint64_t Uint128High64(const NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) { return x.second; }
60inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Uint128(uint64_t lo, uint64_t hi) { return NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t(lo, hi); }
61
62
63// BASIC STRING HASHING
64
65// Hash function for a byte array.
66// May change from time to time, may differ on different platforms, may differ
67// depending on NDEBUG.
68size_t Hash(const char* s, size_t len);
69
70// Hash function for a byte array. Most useful in 32-bit binaries.
71// May change from time to time, may differ on different platforms, may differ
72// depending on NDEBUG.
73uint32_t Hash32(const char* s, size_t len);
74
75// Hash function for a byte array. For convenience, a 32-bit seed is also
76// hashed into the result.
77// May change from time to time, may differ on different platforms, may differ
78// depending on NDEBUG.
79uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed);
80
81// Hash 128 input bits down to 64 bits of output.
82// Hash function for a byte array.
83// May change from time to time, may differ on different platforms, may differ
84// depending on NDEBUG.
85uint64_t Hash64(const char* s, size_t len);
86
87// Hash function for a byte array. For convenience, a 64-bit seed is also
88// hashed into the result.
89// May change from time to time, may differ on different platforms, may differ
90// depending on NDEBUG.
91uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed);
92
93// Hash function for a byte array. For convenience, two seeds are also
94// hashed into the result.
95// May change from time to time, may differ on different platforms, may differ
96// depending on NDEBUG.
97uint64_t Hash64WithSeeds(const char* s, size_t len,
98 uint64_t seed0, uint64_t seed1);
99
100// Hash function for a byte array.
101// May change from time to time, may differ on different platforms, may differ
102// depending on NDEBUG.
103NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128(const char* s, size_t len);
104
105// Hash function for a byte array. For convenience, a 128-bit seed is also
106// hashed into the result.
107// May change from time to time, may differ on different platforms, may differ
108// depending on NDEBUG.
109NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128WithSeed(const char* s, size_t len, NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t seed);
110
111// BASIC NON-STRING HASHING
112
113// This is intended to be a reasonably good hash function.
114// May change from time to time, may differ on different platforms, may differ
115// depending on NDEBUG.
116inline uint64_t Hash128to64(NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) {
117 // Murmur-inspired hashing.
118 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
119 uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;
120 a ^= (a >> 47);
121 uint64_t b = (Uint128High64(x) ^ a) * kMul;
122 b ^= (b >> 47);
123 b *= kMul;
124 return b;
125}
126
127// FINGERPRINTING (i.e., good, portable, forever-fixed hash functions)
128
129// Fingerprint function for a byte array. Most useful in 32-bit binaries.
130uint32_t Fingerprint32(const char* s, size_t len);
131
132// Fingerprint function for a byte array.
133uint64_t Fingerprint64(const char* s, size_t len);
134
135// Fingerprint function for a byte array.
136NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Fingerprint128(const char* s, size_t len);
137
138// This is intended to be a good fingerprinting primitive.
139// See below for more overloads.
140inline uint64_t Fingerprint(NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) {
141 // Murmur-inspired hashing.
142 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
143 uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;
144 a ^= (a >> 47);
145 uint64_t b = (Uint128High64(x) ^ a) * kMul;
146 b ^= (b >> 44);
147 b *= kMul;
148 b ^= (b >> 41);
149 b *= kMul;
150 return b;
151}
152
153// This is intended to be a good fingerprinting primitive.
154inline uint64_t Fingerprint(uint64_t x) {
155 // Murmur-inspired hashing.
156 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
157 uint64_t b = x * kMul;
158 b ^= (b >> 44);
159 b *= kMul;
160 b ^= (b >> 41);
161 b *= kMul;
162 return b;
163}
164
165#ifndef FARMHASH_NO_CXX_STRING
166
167// Convenience functions to hash or fingerprint C++ strings.
168// These require that Str::data() return a pointer to the first char
169// (as a const char*) and that Str::length() return the string's length;
170// they work with std::string, for example.
171
172// Hash function for a byte array.
173// May change from time to time, may differ on different platforms, may differ
174// depending on NDEBUG.
175template <typename Str>
176inline size_t Hash(const Str& s) {
177 assert(sizeof(s[0]) == 1);
178 return Hash(s.data(), s.length());
179}
180
181// Hash function for a byte array. Most useful in 32-bit binaries.
182// May change from time to time, may differ on different platforms, may differ
183// depending on NDEBUG.
184template <typename Str>
185inline uint32_t Hash32(const Str& s) {
186 assert(sizeof(s[0]) == 1);
187 return Hash32(s.data(), s.length());
188}
189
190// Hash function for a byte array. For convenience, a 32-bit seed is also
191// hashed into the result.
192// May change from time to time, may differ on different platforms, may differ
193// depending on NDEBUG.
194template <typename Str>
195inline uint32_t Hash32WithSeed(const Str& s, uint32_t seed) {
196 assert(sizeof(s[0]) == 1);
197 return Hash32WithSeed(s.data(), s.length(), seed);
198}
199
200// Hash 128 input bits down to 64 bits of output.
201// Hash function for a byte array.
202// May change from time to time, may differ on different platforms, may differ
203// depending on NDEBUG.
204template <typename Str>
205inline uint64_t Hash64(const Str& s) {
206 assert(sizeof(s[0]) == 1);
207 return Hash64(s.data(), s.length());
208}
209
210// Hash function for a byte array. For convenience, a 64-bit seed is also
211// hashed into the result.
212// May change from time to time, may differ on different platforms, may differ
213// depending on NDEBUG.
214template <typename Str>
215inline uint64_t Hash64WithSeed(const Str& s, uint64_t seed) {
216 assert(sizeof(s[0]) == 1);
217 return Hash64WithSeed(s.data(), s.length(), seed);
218}
219
220// Hash function for a byte array. For convenience, two seeds are also
221// hashed into the result.
222// May change from time to time, may differ on different platforms, may differ
223// depending on NDEBUG.
224template <typename Str>
225inline uint64_t Hash64WithSeeds(const Str& s, uint64_t seed0, uint64_t seed1) {
226 assert(sizeof(s[0]) == 1);
227 return Hash64WithSeeds(s.data(), s.length(), seed0, seed1);
228}
229
230// Hash function for a byte array.
231// May change from time to time, may differ on different platforms, may differ
232// depending on NDEBUG.
233template <typename Str>
234inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128(const Str& s) {
235 assert(sizeof(s[0]) == 1);
236 return Hash128(s.data(), s.length());
237}
238
239// Hash function for a byte array. For convenience, a 128-bit seed is also
240// hashed into the result.
241// May change from time to time, may differ on different platforms, may differ
242// depending on NDEBUG.
243template <typename Str>
244inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128WithSeed(const Str& s, NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t seed) {
245 assert(sizeof(s[0]) == 1);
246 return Hash128(s.data(), s.length(), seed);
247}
248
249// FINGERPRINTING (i.e., good, portable, forever-fixed hash functions)
250
251// Fingerprint function for a byte array. Most useful in 32-bit binaries.
252template <typename Str>
253inline uint32_t Fingerprint32(const Str& s) {
254 assert(sizeof(s[0]) == 1);
255 return Fingerprint32(s.data(), s.length());
256}
257
258// Fingerprint 128 input bits down to 64 bits of output.
259// Fingerprint function for a byte array.
260template <typename Str>
261inline uint64_t Fingerprint64(const Str& s) {
262 assert(sizeof(s[0]) == 1);
263 return Fingerprint64(s.data(), s.length());
264}
265
266// Fingerprint function for a byte array.
267template <typename Str>
268inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Fingerprint128(const Str& s) {
269 assert(sizeof(s[0]) == 1);
270 return Fingerprint128(s.data(), s.length());
271}
272
273#endif
274
275} // namespace NAMESPACE_FOR_HASH_FUNCTIONS
276
277#endif // FARM_HASH_H_
278