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 /* ****************************
77 ******************************/
78 #include <stddef.h> /* size_t */
79 typedef enum { XXH_OK
=0, XXH_ERROR
} XXH_errorcode
;
82 /* ****************************
84 ******************************/
86 * Transforms all publics symbols within `xxhash.c` into private ones.
88 * instead of : #include "xxhash.h"
90 * #define XXH_PRIVATE_API
91 * #include "xxhash.c" // note the .c , instead of .h
92 * also : don't compile and link xxhash.c separately
94 #ifdef XXH_PRIVATE_API
95 # if defined(__GNUC__)
96 # define XXH_PUBLIC_API static __attribute__((unused))
97 # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
98 # define XXH_PUBLIC_API static inline
99 # elif defined(_MSC_VER)
100 # define XXH_PUBLIC_API static __inline
102 # define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */
105 # define XXH_PUBLIC_API /* do nothing */
108 /*!XXH_NAMESPACE, aka Namespace Emulation :
110 If you want to include _and expose_ xxHash functions from within your own library,
111 but also want to avoid symbol collisions with another library which also includes xxHash,
113 you can use XXH_NAMESPACE, to automatically prefix any public symbol from `xxhash.c`
114 with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values).
116 Note that no change is required within the calling program as long as it also includes `xxhash.h` :
117 regular symbol name will be automatically translated by this header.
120 # define XXH_CAT(A,B) A##B
121 # define XXH_NAME2(A,B) XXH_CAT(A,B)
122 # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
123 # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
124 # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
125 # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
126 # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
127 # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
128 # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
129 # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
130 # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
131 # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
132 # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
133 # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
134 # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
138 /* *************************************
140 ***************************************/
141 #define XXH_VERSION_MAJOR 0
142 #define XXH_VERSION_MINOR 5
143 #define XXH_VERSION_RELEASE 1
144 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
145 XXH_PUBLIC_API
unsigned XXH_versionNumber (void);
148 /* ****************************
149 * Simple Hash Functions
150 ******************************/
151 typedef unsigned int XXH32_hash_t
;
152 typedef unsigned long long XXH64_hash_t
;
154 XXH_PUBLIC_API XXH32_hash_t
XXH32 (const void* input
, size_t length
, unsigned int seed
);
155 XXH_PUBLIC_API XXH64_hash_t
XXH64 (const void* input
, size_t length
, unsigned long long seed
);
159 Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input".
160 The memory between input & input+length must be valid (allocated and read-accessible).
161 "seed" can be used to alter the result predictably.
162 Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
164 Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".
165 "seed" can be used to alter the result predictably.
166 This function runs faster on 64-bits systems, but slower on 32-bits systems (see benchmark).
170 /* ****************************
171 * Streaming Hash Functions
172 ******************************/
173 typedef struct XXH32_state_s XXH32_state_t
; /* incomplete type */
174 typedef struct XXH64_state_s XXH64_state_t
; /* incomplete type */
177 /*! Static allocation
178 For static linking only, do not use in the context of DLL !
179 XXHnn_CREATESTATE_STATIC(name);
180 is static-allocation equivalent of :
181 XXHnn_state_t* name = XXHnn_createState();
183 typedef struct { long long ll
[ 6]; } XXH32_stateBody_t
;
184 typedef struct { long long ll
[11]; } XXH64_stateBody_t
;
186 #define XXH32_CREATESTATE_STATIC(name) XXH32_stateBody_t name##xxhbody; void* name##xxhvoid = &(name##xxhbody); XXH32_state_t* name = (XXH32_state_t*)(name##xxhvoid) /* no final ; */
187 #define XXH64_CREATESTATE_STATIC(name) XXH64_stateBody_t name##xxhbody; void* name##xxhvoid = &(name##xxhbody); XXH64_state_t* name = (XXH64_state_t*)(name##xxhvoid) /* no final ; */
190 /*!Dynamic allocation
191 To be preferred in the context of DLL */
193 XXH_PUBLIC_API XXH32_state_t
* XXH32_createState(void);
194 XXH_PUBLIC_API XXH_errorcode
XXH32_freeState(XXH32_state_t
* statePtr
);
196 XXH_PUBLIC_API XXH64_state_t
* XXH64_createState(void);
197 XXH_PUBLIC_API XXH_errorcode
XXH64_freeState(XXH64_state_t
* statePtr
);
202 XXH_PUBLIC_API XXH_errorcode
XXH32_reset (XXH32_state_t
* statePtr
, unsigned int seed
);
203 XXH_PUBLIC_API XXH_errorcode
XXH32_update (XXH32_state_t
* statePtr
, const void* input
, size_t length
);
204 XXH_PUBLIC_API XXH32_hash_t
XXH32_digest (const XXH32_state_t
* statePtr
);
206 XXH_PUBLIC_API XXH_errorcode
XXH64_reset (XXH64_state_t
* statePtr
, unsigned long long seed
);
207 XXH_PUBLIC_API XXH_errorcode
XXH64_update (XXH64_state_t
* statePtr
, const void* input
, size_t length
);
208 XXH_PUBLIC_API XXH64_hash_t
XXH64_digest (const XXH64_state_t
* statePtr
);
211 These functions generate the xxHash of an input provided in multiple segments,
212 as opposed to provided as a single block.
214 XXH state must first be allocated, using either static or dynamic method provided above.
216 Start a new hash by initializing state with a seed, using XXHnn_reset().
218 Then, feed the hash state by calling XXHnn_update() as many times as necessary.
219 Obviously, input must be valid, hence allocated and read accessible.
220 The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
222 Finally, a hash value can be produced anytime, by using XXHnn_digest().
223 This function returns the nn-bits hash as an int or long long.
225 It's still possible to continue inserting input into the hash state after a digest,
226 and later on generate some new hashes, by calling again XXHnn_digest().
228 When done, free XXH state space if it was allocated dynamically.
232 /* **************************
233 * Canonical representation
234 ****************************/
235 typedef struct { unsigned char digest
[4]; } XXH32_canonical_t
;
236 typedef struct { unsigned char digest
[8]; } XXH64_canonical_t
;
238 XXH_PUBLIC_API
void XXH32_canonicalFromHash(XXH32_canonical_t
* dst
, XXH32_hash_t hash
);
239 XXH_PUBLIC_API
void XXH64_canonicalFromHash(XXH64_canonical_t
* dst
, XXH64_hash_t hash
);
241 XXH_PUBLIC_API XXH32_hash_t
XXH32_hashFromCanonical(const XXH32_canonical_t
* src
);
242 XXH_PUBLIC_API XXH64_hash_t
XXH64_hashFromCanonical(const XXH64_canonical_t
* src
);
244 /*! Default result type for XXH functions are primitive unsigned 32 and 64 bits.
245 * The canonical representation uses human-readable write convention, aka big-endian (large digits first).
246 * These functions allow transformation of hash result into and from its canonical format.
247 * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
251 #if defined (__cplusplus)
255 #endif /* XXHASH_H_5627135585666179 */