2 xxHash - Extremely Fast Hash algorithm
4 Copyright (C) 2012-2016, Yann Collet.
6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
12 * Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above
15 copyright notice, this list of conditions and the following disclaimer
16 in the documentation and/or other materials provided with the
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 You can contact the author at :
32 - xxHash source repository : https://github.com/Cyan4973/xxHash
35 /* Notice extracted from xxHash homepage :
37 xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
38 It also successfully passes all tests from the SMHasher suite.
40 Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
42 Name Speed Q.Score Author
44 CrapWow 3.2 GB/s 2 Andrew
45 MumurHash 3a 2.7 GB/s 10 Austin Appleby
46 SpookyHash 2.0 GB/s 10 Bob Jenkins
47 SBox 1.4 GB/s 9 Bret Mulvey
48 Lookup3 1.2 GB/s 9 Bob Jenkins
49 SuperFastHash 1.2 GB/s 1 Paul Hsieh
50 CityHash64 1.05 GB/s 10 Pike & Alakuijala
51 FNV 0.55 GB/s 5 Fowler, Noll, Vo
53 MD5-32 0.33 GB/s 10 Ronald L. Rivest
56 Q.Score is a measure of quality of the hash function.
57 It depends on successfully passing SMHasher test set.
58 10 is a perfect score.
60 A 64-bits version, named XXH64, is available since r35.
61 It offers much better speed, but for 64-bits applications only.
62 Name Speed on 64 bits Speed on 32 bits
63 XXH64 13.8 GB/s 1.9 GB/s
64 XXH32 6.8 GB/s 6.0 GB/s
67 #ifndef XXHASH_H_5627135585666179
68 #define XXHASH_H_5627135585666179 1
70 #if defined (__cplusplus)
75 # define XXH_NAMESPACE ZSTD_ /* Zstandard specific */
79 /* ****************************
81 ******************************/
82 #include <stddef.h> /* size_t */
83 typedef enum { XXH_OK
=0, XXH_ERROR
} XXH_errorcode
;
86 /* ****************************
88 ******************************/
90 * This is useful if you want to include xxhash functions in `static` mode
91 * in order to inline them, and remove their symbol from the public list.
93 * #define XXH_PRIVATE_API
95 * `xxhash.c` is automatically included.
96 * It's not useful to compile and link it as a separate module anymore.
98 #ifdef XXH_PRIVATE_API
99 # ifndef XXH_STATIC_LINKING_ONLY
100 # define XXH_STATIC_LINKING_ONLY
102 # if defined(__GNUC__)
103 # define XXH_PUBLIC_API static __inline __attribute__((unused))
104 # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
105 # define XXH_PUBLIC_API static inline
106 # elif defined(_MSC_VER)
107 # define XXH_PUBLIC_API static __inline
109 # define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */
112 # define XXH_PUBLIC_API /* do nothing */
113 #endif /* XXH_PRIVATE_API */
115 /*!XXH_NAMESPACE, aka Namespace Emulation :
117 If you want to include _and expose_ xxHash functions from within your own library,
118 but also want to avoid symbol collisions with another library which also includes xxHash,
120 you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library
121 with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values).
123 Note that no change is required within the calling program as long as it includes `xxhash.h` :
124 regular symbol name will be automatically translated by this header.
127 # define XXH_CAT(A,B) A##B
128 # define XXH_NAME2(A,B) XXH_CAT(A,B)
129 # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
130 # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
131 # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
132 # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
133 # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
134 # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
135 # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
136 # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
137 # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
138 # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
139 # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
140 # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
141 # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
142 # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
143 # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
144 # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
145 # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
146 # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
147 # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
151 /* *************************************
153 ***************************************/
154 #define XXH_VERSION_MAJOR 0
155 #define XXH_VERSION_MINOR 6
156 #define XXH_VERSION_RELEASE 2
157 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
158 XXH_PUBLIC_API
unsigned XXH_versionNumber (void);
161 /* ****************************
162 * Simple Hash Functions
163 ******************************/
164 typedef unsigned int XXH32_hash_t
;
165 typedef unsigned long long XXH64_hash_t
;
167 XXH_PUBLIC_API XXH32_hash_t
XXH32 (const void* input
, size_t length
, unsigned int seed
);
168 XXH_PUBLIC_API XXH64_hash_t
XXH64 (const void* input
, size_t length
, unsigned long long seed
);
172 Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input".
173 The memory between input & input+length must be valid (allocated and read-accessible).
174 "seed" can be used to alter the result predictably.
175 Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
177 Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".
178 "seed" can be used to alter the result predictably.
179 This function runs 2x faster on 64-bits systems, but slower on 32-bits systems (see benchmark).
183 /* ****************************
184 * Streaming Hash Functions
185 ******************************/
186 typedef struct XXH32_state_s XXH32_state_t
; /* incomplete type */
187 typedef struct XXH64_state_s XXH64_state_t
; /* incomplete type */
189 /*! State allocation, compatible with dynamic libraries */
191 XXH_PUBLIC_API XXH32_state_t
* XXH32_createState(void);
192 XXH_PUBLIC_API XXH_errorcode
XXH32_freeState(XXH32_state_t
* statePtr
);
194 XXH_PUBLIC_API XXH64_state_t
* XXH64_createState(void);
195 XXH_PUBLIC_API XXH_errorcode
XXH64_freeState(XXH64_state_t
* statePtr
);
200 XXH_PUBLIC_API XXH_errorcode
XXH32_reset (XXH32_state_t
* statePtr
, unsigned int seed
);
201 XXH_PUBLIC_API XXH_errorcode
XXH32_update (XXH32_state_t
* statePtr
, const void* input
, size_t length
);
202 XXH_PUBLIC_API XXH32_hash_t
XXH32_digest (const XXH32_state_t
* statePtr
);
204 XXH_PUBLIC_API XXH_errorcode
XXH64_reset (XXH64_state_t
* statePtr
, unsigned long long seed
);
205 XXH_PUBLIC_API XXH_errorcode
XXH64_update (XXH64_state_t
* statePtr
, const void* input
, size_t length
);
206 XXH_PUBLIC_API XXH64_hash_t
XXH64_digest (const XXH64_state_t
* statePtr
);
209 These functions generate the xxHash of an input provided in multiple segments.
210 Note that, for small input, they are slower than single-call functions, due to state management.
211 For small input, prefer `XXH32()` and `XXH64()` .
213 XXH state must first be allocated, using XXH*_createState() .
215 Start a new hash by initializing state with a seed, using XXH*_reset().
217 Then, feed the hash state by calling XXH*_update() as many times as necessary.
218 Obviously, input must be allocated and read accessible.
219 The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
221 Finally, a hash value can be produced anytime, by using XXH*_digest().
222 This function returns the nn-bits hash as an int or long long.
224 It's still possible to continue inserting input into the hash state after a digest,
225 and generate some new hashes later on, by calling again XXH*_digest().
227 When done, free XXH state space if it was allocated dynamically.
231 /* **************************
233 ****************************/
234 #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* ! C99 */
235 # define restrict /* disable restrict */
238 XXH_PUBLIC_API
void XXH32_copyState(XXH32_state_t
* restrict dst_state
, const XXH32_state_t
* restrict src_state
);
239 XXH_PUBLIC_API
void XXH64_copyState(XXH64_state_t
* restrict dst_state
, const XXH64_state_t
* restrict src_state
);
242 /* **************************
243 * Canonical representation
244 ****************************/
245 typedef struct { unsigned char digest
[4]; } XXH32_canonical_t
;
246 typedef struct { unsigned char digest
[8]; } XXH64_canonical_t
;
248 XXH_PUBLIC_API
void XXH32_canonicalFromHash(XXH32_canonical_t
* dst
, XXH32_hash_t hash
);
249 XXH_PUBLIC_API
void XXH64_canonicalFromHash(XXH64_canonical_t
* dst
, XXH64_hash_t hash
);
251 XXH_PUBLIC_API XXH32_hash_t
XXH32_hashFromCanonical(const XXH32_canonical_t
* src
);
252 XXH_PUBLIC_API XXH64_hash_t
XXH64_hashFromCanonical(const XXH64_canonical_t
* src
);
254 /* Default result type for XXH functions are primitive unsigned 32 and 64 bits.
255 * The canonical representation uses human-readable write convention, aka big-endian (large digits first).
256 * These functions allow transformation of hash result into and from its canonical format.
257 * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
261 #ifdef XXH_STATIC_LINKING_ONLY
263 /* ================================================================================================
264 This section contains definitions which are not guaranteed to remain stable.
265 They may change in future versions, becoming incompatible with a different version of the library.
266 They shall only be used with static linking.
267 Never use these definitions in association with dynamic linking !
268 =================================================================================================== */
270 /* These definitions are only meant to allow allocation of XXH state
271 statically, on stack, or in a struct for example.
272 Do not use members directly. */
274 struct XXH32_state_s
{
275 unsigned total_len_32
;
281 unsigned mem32
[4]; /* buffer defined as U32 for alignment */
283 unsigned reserved
; /* never read nor write, will be removed in a future version */
284 }; /* typedef'd to XXH32_state_t */
286 struct XXH64_state_s
{
287 unsigned long long total_len
;
288 unsigned long long v1
;
289 unsigned long long v2
;
290 unsigned long long v3
;
291 unsigned long long v4
;
292 unsigned long long mem64
[4]; /* buffer defined as U64 for alignment */
294 unsigned reserved
[2]; /* never read nor write, will be removed in a future version */
295 }; /* typedef'd to XXH64_state_t */
298 # ifdef XXH_PRIVATE_API
299 # include "xxhash.c" /* include xxhash functions as `static`, for inlining */
302 #endif /* XXH_STATIC_LINKING_ONLY */
305 #if defined (__cplusplus)
309 #endif /* XXHASH_H_5627135585666179 */