1 /* 2 * xxHash - Extremely Fast Hash algorithm 3 * Header File 4 * Copyright (c) 2012-2020, Yann Collet, Facebook, Inc. 5 * 6 * You can contact the author at : 7 * - xxHash source repository : https://github.com/Cyan4973/xxHash 8 * 9 * This source code is licensed under both the BSD-style license (found in the 10 * LICENSE file in the root directory of this source tree) and the GPLv2 (found 11 * in the COPYING file in the root directory of this source tree). 12 * You may select, at your option, one of the above-listed licenses. 13 */ 14 15 /* Notice extracted from xxHash homepage : 16 17 xxHash is an extremely fast Hash algorithm, running at RAM speed limits. 18 It also successfully passes all tests from the SMHasher suite. 19 20 Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) 21 22 Name Speed Q.Score Author 23 xxHash 5.4 GB/s 10 24 CrapWow 3.2 GB/s 2 Andrew 25 MumurHash 3a 2.7 GB/s 10 Austin Appleby 26 SpookyHash 2.0 GB/s 10 Bob Jenkins 27 SBox 1.4 GB/s 9 Bret Mulvey 28 Lookup3 1.2 GB/s 9 Bob Jenkins 29 SuperFastHash 1.2 GB/s 1 Paul Hsieh 30 CityHash64 1.05 GB/s 10 Pike & Alakuijala 31 FNV 0.55 GB/s 5 Fowler, Noll, Vo 32 CRC32 0.43 GB/s 9 33 MD5-32 0.33 GB/s 10 Ronald L. Rivest 34 SHA1-32 0.28 GB/s 10 35 36 Q.Score is a measure of quality of the hash function. 37 It depends on successfully passing SMHasher test set. 38 10 is a perfect score. 39 40 A 64-bits version, named XXH64, is available since r35. 41 It offers much better speed, but for 64-bits applications only. 42 Name Speed on 64 bits Speed on 32 bits 43 XXH64 13.8 GB/s 1.9 GB/s 44 XXH32 6.8 GB/s 6.0 GB/s 45 */ 46 47 #if defined (__cplusplus) 48 extern "C" { 49 #endif 50 51 #ifndef XXHASH_H_5627135585666179 52 #define XXHASH_H_5627135585666179 1 53 54 55 /* **************************** 56 * Definitions 57 ******************************/ 58 #include "zstd_deps.h" 59 typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; 60 61 62 /* **************************** 63 * API modifier 64 ******************************/ 65 /** XXH_PRIVATE_API 66 * This is useful if you want to include xxhash functions in `static` mode 67 * in order to inline them, and remove their symbol from the public list. 68 * Methodology : 69 * #define XXH_PRIVATE_API 70 * #include "xxhash.h" 71 * `xxhash.c` is automatically included. 72 * It's not useful to compile and link it as a separate module anymore. 73 */ 74 #ifdef XXH_PRIVATE_API 75 # ifndef XXH_STATIC_LINKING_ONLY 76 # define XXH_STATIC_LINKING_ONLY 77 # endif 78 # if defined(__GNUC__) 79 # define XXH_PUBLIC_API static __inline __attribute__((unused)) 80 # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 81 # define XXH_PUBLIC_API static inline 82 # elif defined(_MSC_VER) 83 # define XXH_PUBLIC_API static __inline 84 # else 85 # define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */ 86 # endif 87 #else 88 # define XXH_PUBLIC_API /* do nothing */ 89 #endif /* XXH_PRIVATE_API */ 90 91 /*!XXH_NAMESPACE, aka Namespace Emulation : 92 93 If you want to include _and expose_ xxHash functions from within your own library, 94 but also want to avoid symbol collisions with another library which also includes xxHash, 95 96 you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library 97 with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values). 98 99 Note that no change is required within the calling program as long as it includes `xxhash.h` : 100 regular symbol name will be automatically translated by this header. 101 */ 102 #ifdef XXH_NAMESPACE 103 # define XXH_CAT(A,B) A##B 104 # define XXH_NAME2(A,B) XXH_CAT(A,B) 105 # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32) 106 # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64) 107 # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber) 108 # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState) 109 # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState) 110 # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState) 111 # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState) 112 # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset) 113 # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset) 114 # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update) 115 # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update) 116 # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest) 117 # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest) 118 # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState) 119 # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState) 120 # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash) 121 # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash) 122 # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical) 123 # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical) 124 #endif 125 126 127 /* ************************************* 128 * Version 129 ***************************************/ 130 #define XXH_VERSION_MAJOR 0 131 #define XXH_VERSION_MINOR 6 132 #define XXH_VERSION_RELEASE 2 133 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE) 134 XXH_PUBLIC_API unsigned XXH_versionNumber (void); 135 136 137 /* **************************** 138 * Simple Hash Functions 139 ******************************/ 140 typedef unsigned int XXH32_hash_t; 141 typedef unsigned long long XXH64_hash_t; 142 143 XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed); 144 /* Begin FreeBSD - This symbol is needed by dll-linked CLI zstd(1). */ 145 __attribute__((visibility ("default"))) 146 /* End FreeBSD */ 147 XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed); 148 149 /*! 150 XXH32() : 151 Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". 152 The memory between input & input+length must be valid (allocated and read-accessible). 153 "seed" can be used to alter the result predictably. 154 Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s 155 XXH64() : 156 Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". 157 "seed" can be used to alter the result predictably. 158 This function runs 2x faster on 64-bits systems, but slower on 32-bits systems (see benchmark). 159 */ 160 161 162 /* **************************** 163 * Streaming Hash Functions 164 ******************************/ 165 typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */ 166 typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */ 167 168 /*! State allocation, compatible with dynamic libraries */ 169 170 XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void); 171 XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); 172 173 XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void); 174 XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); 175 176 177 /* hash streaming */ 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, const void* input, size_t length); 181 XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr); 182 183 XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed); 184 XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); 185 XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr); 186 187 /* 188 These functions generate the xxHash of an input provided in multiple segments. 189 Note that, for small input, they are slower than single-call functions, due to state management. 190 For small input, prefer `XXH32()` and `XXH64()` . 191 192 XXH state must first be allocated, using XXH*_createState() . 193 194 Start a new hash by initializing state with a seed, using XXH*_reset(). 195 196 Then, feed the hash state by calling XXH*_update() as many times as necessary. 197 Obviously, input must be allocated and read accessible. 198 The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. 199 200 Finally, a hash value can be produced anytime, by using XXH*_digest(). 201 This function returns the nn-bits hash as an int or long long. 202 203 It's still possible to continue inserting input into the hash state after a digest, 204 and generate some new hashes later on, by calling again XXH*_digest(). 205 206 When done, free XXH state space if it was allocated dynamically. 207 */ 208 209 210 /* ************************** 211 * Utils 212 ****************************/ 213 #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* ! C99 */ 214 # define restrict /* disable restrict */ 215 #endif 216 217 XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dst_state, const XXH32_state_t* restrict src_state); 218 XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dst_state, const XXH64_state_t* restrict src_state); 219 220 221 /* ************************** 222 * Canonical representation 223 ****************************/ 224 /* Default result type for XXH functions are primitive unsigned 32 and 64 bits. 225 * The canonical representation uses human-readable write convention, aka big-endian (large digits first). 226 * These functions allow transformation of hash result into and from its canonical format. 227 * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs. 228 */ 229 typedef struct { unsigned char digest[4]; } XXH32_canonical_t; 230 typedef struct { unsigned char digest[8]; } XXH64_canonical_t; 231 232 XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash); 233 XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash); 234 235 XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src); 236 XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src); 237 238 #endif /* XXHASH_H_5627135585666179 */ 239 240 241 242 /* ================================================================================================ 243 This section contains definitions which are not guaranteed to remain stable. 244 They may change in future versions, becoming incompatible with a different version of the library. 245 They shall only be used with static linking. 246 Never use these definitions in association with dynamic linking ! 247 =================================================================================================== */ 248 #if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXH_STATIC_H_3543687687345) 249 #define XXH_STATIC_H_3543687687345 250 251 /* These definitions are only meant to allow allocation of XXH state 252 statically, on stack, or in a struct for example. 253 Do not use members directly. */ 254 255 struct XXH32_state_s { 256 unsigned total_len_32; 257 unsigned large_len; 258 unsigned v1; 259 unsigned v2; 260 unsigned v3; 261 unsigned v4; 262 unsigned mem32[4]; /* buffer defined as U32 for alignment */ 263 unsigned memsize; 264 unsigned reserved; /* never read nor write, will be removed in a future version */ 265 }; /* typedef'd to XXH32_state_t */ 266 267 struct XXH64_state_s { 268 unsigned long long total_len; 269 unsigned long long v1; 270 unsigned long long v2; 271 unsigned long long v3; 272 unsigned long long v4; 273 unsigned long long mem64[4]; /* buffer defined as U64 for alignment */ 274 unsigned memsize; 275 unsigned reserved[2]; /* never read nor write, will be removed in a future version */ 276 }; /* typedef'd to XXH64_state_t */ 277 278 279 # ifdef XXH_PRIVATE_API 280 # include "xxhash.c" /* include xxhash functions as `static`, for inlining */ 281 # endif 282 283 #endif /* XXH_STATIC_LINKING_ONLY && XXH_STATIC_H_3543687687345 */ 284 285 286 #if defined (__cplusplus) 287 } 288 #endif 289