1 /* 2 xxHash - Extremely Fast Hash algorithm 3 Header File 4 Copyright (C) 2012-2016, Yann Collet. 5 6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 7 8 Redistribution and use in source and binary forms, with or without 9 modification, are permitted provided that the following conditions are 10 met: 11 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 17 distribution. 18 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. 30 31 You can contact the author at : 32 - xxHash source repository : https://github.com/Cyan4973/xxHash 33 */ 34 35 /* Notice extracted from xxHash homepage : 36 37 xxHash is an extremely fast Hash algorithm, running at RAM speed limits. 38 It also successfully passes all tests from the SMHasher suite. 39 40 Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) 41 42 Name Speed Q.Score Author 43 xxHash 5.4 GB/s 10 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 52 CRC32 0.43 GB/s 9 53 MD5-32 0.33 GB/s 10 Ronald L. Rivest 54 SHA1-32 0.28 GB/s 10 55 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. 59 60 A 64-bit version, named XXH64, is available since r35. 61 It offers much better speed, but for 64-bit 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 65 */ 66 67 #ifndef XXHASH_H_5627135585666179 68 #define XXHASH_H_5627135585666179 1 69 70 #if defined(__cplusplus) 71 extern "C" { 72 #endif 73 74 /* **************************** 75 * Definitions 76 ******************************/ 77 #include <stddef.h> /* size_t */ 78 typedef enum 79 { 80 XXH_OK = 0, 81 XXH_ERROR 82 } XXH_errorcode; 83 84 /* **************************** 85 * API modifier 86 ******************************/ 87 /** XXH_INLINE_ALL (and XXH_PRIVATE_API) 88 * This is useful to include xxhash functions in `static` mode 89 * in order to inline them, and remove their symbol from the public list. 90 * Inlining can offer dramatic performance improvement on small keys. 91 * Methodology : 92 * #define XXH_INLINE_ALL 93 * #include "xxhash.h" 94 * `xxhash.c` is automatically included. 95 * It's not useful to compile and link it as a separate module. 96 */ 97 #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) 98 # ifndef XXH_STATIC_LINKING_ONLY 99 # define XXH_STATIC_LINKING_ONLY 100 # endif 101 # if defined(__GNUC__) 102 # define XXH_PUBLIC_API static __inline __attribute__((unused)) 103 #elif defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 \ 104 */) 105 # define XXH_PUBLIC_API static inline 106 # elif defined(_MSC_VER) 107 # define XXH_PUBLIC_API static __inline 108 # else 109 /* this version may generate warnings for unused static functions */ 110 # define XXH_PUBLIC_API static 111 # endif 112 #else 113 # define XXH_PUBLIC_API /* do nothing */ 114 #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */ 115 116 /*! XXH_NAMESPACE, aka Namespace Emulation : 117 * 118 * If you want to include _and expose_ xxHash functions from within your own library, 119 * but also want to avoid symbol collisions with other libraries which may also include xxHash, 120 * 121 * you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library 122 * with the value of XXH_NAMESPACE (therefore, avoid NULL and numeric values). 123 * 124 * Note that no change is required within the calling program as long as it includes `xxhash.h` : 125 * regular symbol name will be automatically translated by this header. 126 */ 127 #ifdef XXH_NAMESPACE 128 # define XXH_CAT(A, B) A##B 129 # define XXH_NAME2(A, B) XXH_CAT(A, B) 130 # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber) 131 # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32) 132 # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState) 133 # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState) 134 # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset) 135 # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update) 136 # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest) 137 # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState) 138 # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash) 139 # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical) 140 # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64) 141 # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState) 142 # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState) 143 # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset) 144 # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update) 145 # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest) 146 # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState) 147 # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash) 148 # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical) 149 #endif 150 151 /* ************************************* 152 * Version 153 ***************************************/ 154 #define XXH_VERSION_MAJOR 0 155 #define XXH_VERSION_MINOR 6 156 #define XXH_VERSION_RELEASE 5 157 #define XXH_VERSION_NUMBER \ 158 (XXH_VERSION_MAJOR * 100 * 100 + XXH_VERSION_MINOR * 100 + XXH_VERSION_RELEASE) 159 XXH_PUBLIC_API unsigned XXH_versionNumber(void); 160 161 /*-********************************************************************** 162 * 32-bit hash 163 ************************************************************************/ 164 typedef unsigned int XXH32_hash_t; 165 166 /*! XXH32() : 167 Calculate the 32-bit hash of sequence "length" bytes stored at memory address "input". 168 The memory between input & input+length must be valid (allocated and read-accessible). 169 "seed" can be used to alter the result predictably. 170 Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s */ 171 XXH_PUBLIC_API XXH32_hash_t XXH32(const void *input, size_t length, unsigned int seed); 172 173 /*====== Streaming ======*/ 174 typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */ 175 XXH_PUBLIC_API XXH32_state_t *XXH32_createState(void); 176 XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t *statePtr); 177 XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t *dst_state, const XXH32_state_t *src_state); 178 179 XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t *statePtr, unsigned int seed); 180 XXH_PUBLIC_API XXH_errorcode XXH32_update(XXH32_state_t *statePtr, 181 const void *input, 182 size_t length); 183 XXH_PUBLIC_API XXH32_hash_t XXH32_digest(const XXH32_state_t *statePtr); 184 185 /* 186 * Streaming functions generate the xxHash of an input provided in multiple segments. 187 * Note that, for small input, they are slower than single-call functions, due to state 188 * management. For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized. 189 * 190 * XXH state must first be allocated, using XXH*_createState() . 191 * 192 * Start a new hash by initializing state with a seed, using XXH*_reset(). 193 * 194 * Then, feed the hash state by calling XXH*_update() as many times as necessary. 195 * The function returns an error code, with 0 meaning OK, and any other value meaning there is 196 * an error. 197 * 198 * Finally, a hash value can be produced anytime, by using XXH*_digest(). 199 * This function returns the nn-bits hash as an int or long long. 200 * 201 * It's still possible to continue inserting input into the hash state after a digest, 202 * and generate some new hashes later on, by calling again XXH*_digest(). 203 * 204 * When done, free XXH state space if it was allocated dynamically. 205 */ 206 207 /*====== Canonical representation ======*/ 208 209 typedef struct 210 { 211 unsigned char digest[4]; 212 } XXH32_canonical_t; 213 XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t *dst, XXH32_hash_t hash); 214 XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t *src); 215 216 /* Default result type for XXH functions are primitive unsigned 32 and 64 bits. 217 * The canonical representation uses human-readable write convention, aka big-endian (large 218 * digits first). These functions allow transformation of hash result into and from its 219 * canonical format. This way, hash values can be written into a file / memory, and remain 220 * comparable on different systems and programs. 221 */ 222 223 #ifndef XXH_NO_LONG_LONG 224 /*-********************************************************************** 225 * 64-bit hash 226 ************************************************************************/ 227 typedef unsigned long long XXH64_hash_t; 228 229 /*! XXH64() : 230 Calculate the 64-bit hash of sequence of length "len" stored at memory address "input". 231 "seed" can be used to alter the result predictably. 232 This function runs faster on 64-bit systems, but slower on 32-bit systems (see benchmark). 233 */ 234 XXH_PUBLIC_API XXH64_hash_t XXH64(const void *input, size_t length, unsigned long long seed); 235 236 /*====== Streaming ======*/ 237 typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */ 238 XXH_PUBLIC_API XXH64_state_t *XXH64_createState(void); 239 XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t *statePtr); 240 XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t *dst_state, const XXH64_state_t *src_state); 241 242 XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t *statePtr, unsigned long long seed); 243 XXH_PUBLIC_API XXH_errorcode XXH64_update(XXH64_state_t *statePtr, 244 const void *input, 245 size_t length); 246 XXH_PUBLIC_API XXH64_hash_t XXH64_digest(const XXH64_state_t *statePtr); 247 248 /*====== Canonical representation ======*/ 249 typedef struct 250 { 251 unsigned char digest[8]; 252 } XXH64_canonical_t; 253 XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t *dst, XXH64_hash_t hash); 254 XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t *src); 255 #endif /* XXH_NO_LONG_LONG */ 256 257 #ifdef XXH_STATIC_LINKING_ONLY 258 259 /* ================================================================================================ 260 This section contains declarations which are not guaranteed to remain stable. 261 They may change in future versions, becoming incompatible with a different version of the 262 library. These declarations should only be used with static linking. Never use them in 263 association with dynamic linking ! 264 =================================================================================================== 265 */ 266 267 /* These definitions are only present to allow 268 * static allocation of XXH state, on stack or in a struct for example. 269 * Never **ever** use members directly. */ 270 271 # if !defined(__VMS) && (defined(__cplusplus) || (defined(__STDC_VERSION__) && \ 272 (__STDC_VERSION__ >= 199901L) /* C99 */)) 273 # include <stdint.h> 274 275 struct XXH32_state_s 276 { 277 uint32_t total_len_32; 278 uint32_t large_len; 279 uint32_t v1; 280 uint32_t v2; 281 uint32_t v3; 282 uint32_t v4; 283 uint32_t mem32[4]; 284 uint32_t memsize; 285 uint32_t reserved; /* never read nor write, might be removed in a future version */ 286 }; /* typedef'd to XXH32_state_t */ 287 288 struct XXH64_state_s 289 { 290 uint64_t total_len; 291 uint64_t v1; 292 uint64_t v2; 293 uint64_t v3; 294 uint64_t v4; 295 uint64_t mem64[4]; 296 uint32_t memsize; 297 uint32_t reserved[2]; /* never read nor write, might be removed in a future version */ 298 }; /* typedef'd to XXH64_state_t */ 299 300 # else 301 302 struct XXH32_state_s 303 { 304 unsigned total_len_32; 305 unsigned large_len; 306 unsigned v1; 307 unsigned v2; 308 unsigned v3; 309 unsigned v4; 310 unsigned mem32[4]; 311 unsigned memsize; 312 unsigned reserved; /* never read nor write, might be removed in a future version */ 313 }; /* typedef'd to XXH32_state_t */ 314 315 # ifndef XXH_NO_LONG_LONG /* remove 64-bit support */ 316 struct XXH64_state_s 317 { 318 unsigned long long total_len; 319 unsigned long long v1; 320 unsigned long long v2; 321 unsigned long long v3; 322 unsigned long long v4; 323 unsigned long long mem64[4]; 324 unsigned memsize; 325 unsigned reserved[2]; /* never read nor write, might be removed in a future version */ 326 }; /* typedef'd to XXH64_state_t */ 327 # endif 328 329 # endif 330 331 # if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) 332 # include "xxhash.c" /* include xxhash function bodies as `static`, for inlining */ 333 # endif 334 335 #endif /* XXH_STATIC_LINKING_ONLY */ 336 337 #if defined(__cplusplus) 338 } 339 #endif 340 341 #endif /* XXHASH_H_5627135585666179 */ 342