1 /* 2 * xxHash - Fast Hash algorithm 3 * Copyright (C) 2012-2016, Yann Collet 4 * 5 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following disclaimer 15 * in the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * You can contact the author at : 31 * - xxHash homepage: http://www.xxhash.com 32 * - xxHash source repository : https://github.com/Cyan4973/xxHash 33 */ 34 35 36 /* ************************************* 37 * Tuning parameters 38 ***************************************/ 39 /*!XXH_FORCE_MEMORY_ACCESS : 40 * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. 41 * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. 42 * The below switch allow to select different access method for improved performance. 43 * Method 0 (default) : use `memcpy()`. Safe and portable. 44 * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable). 45 * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. 46 * Method 2 : direct access. This method doesn't depend on compiler but violate C standard. 47 * It can generate buggy code on targets which do not support unaligned memory accesses. 48 * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) 49 * See http://stackoverflow.com/a/32095106/646947 for details. 50 * Prefer these methods in priority order (0 > 1 > 2) 51 */ 52 #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */ 53 # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) 54 # define XXH_FORCE_MEMORY_ACCESS 2 55 # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \ 56 (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) )) || \ 57 defined(__ICCARM__) 58 # define XXH_FORCE_MEMORY_ACCESS 1 59 # endif 60 #endif 61 62 /*!XXH_ACCEPT_NULL_INPUT_POINTER : 63 * If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer. 64 * When this option is enabled, xxHash output for null input pointers will be the same as a null-length input. 65 * By default, this option is disabled. To enable it, uncomment below define : 66 */ 67 /* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */ 68 69 /*!XXH_FORCE_NATIVE_FORMAT : 70 * By default, xxHash library provides endian-independent Hash values, based on little-endian convention. 71 * Results are therefore identical for little-endian and big-endian CPU. 72 * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format. 73 * Should endian-independence be of no importance for your application, you may set the #define below to 1, 74 * to improve speed for Big-endian CPU. 75 * This option has no impact on Little_Endian CPU. 76 */ 77 #ifndef XXH_FORCE_NATIVE_FORMAT /* can be defined externally */ 78 # define XXH_FORCE_NATIVE_FORMAT 0 79 #endif 80 81 /*!XXH_FORCE_ALIGN_CHECK : 82 * This is a minor performance trick, only useful with lots of very small keys. 83 * It means : check for aligned/unaligned input. 84 * The check costs one initial branch per hash; set to 0 when the input data 85 * is guaranteed to be aligned. 86 */ 87 #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */ 88 # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) 89 # define XXH_FORCE_ALIGN_CHECK 0 90 # else 91 # define XXH_FORCE_ALIGN_CHECK 1 92 # endif 93 #endif 94 95 96 /* ************************************* 97 * Includes & Memory related functions 98 ***************************************/ 99 /* Modify the local functions below should you wish to use some other memory routines */ 100 /* for malloc(), free() */ 101 #include <stdlib.h> 102 #include <stddef.h> /* size_t */ 103 static void* XXH_malloc(size_t s) { return malloc(s); } 104 static void XXH_free (void* p) { free(p); } 105 /* for memcpy() */ 106 #include <string.h> 107 static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); } 108 109 #ifndef XXH_STATIC_LINKING_ONLY 110 # define XXH_STATIC_LINKING_ONLY 111 #endif 112 #include "xxhash.h" 113 114 115 /* ************************************* 116 * Compiler Specific Options 117 ***************************************/ 118 #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ 119 # define INLINE_KEYWORD inline 120 #else 121 # define INLINE_KEYWORD 122 #endif 123 124 #if defined(__GNUC__) || defined(__ICCARM__) 125 # define FORCE_INLINE_ATTR __attribute__((always_inline)) 126 #elif defined(_MSC_VER) 127 # define FORCE_INLINE_ATTR __forceinline 128 #else 129 # define FORCE_INLINE_ATTR 130 #endif 131 132 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR 133 134 135 #ifdef _MSC_VER 136 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ 137 #endif 138 139 140 /* ************************************* 141 * Basic Types 142 ***************************************/ 143 #ifndef MEM_MODULE 144 # define MEM_MODULE 145 # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) 146 # include <stdint.h> 147 typedef uint8_t BYTE; 148 typedef uint16_t U16; 149 typedef uint32_t U32; 150 typedef int32_t S32; 151 typedef uint64_t U64; 152 # else 153 typedef unsigned char BYTE; 154 typedef unsigned short U16; 155 typedef unsigned int U32; 156 typedef signed int S32; 157 typedef unsigned long long U64; /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */ 158 # endif 159 #endif 160 161 162 #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2)) 163 164 /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */ 165 static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; } 166 static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; } 167 168 #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1)) 169 170 /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ 171 /* currently only defined for gcc and icc */ 172 typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign; 173 174 static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } 175 static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; } 176 177 #else 178 179 /* portable and safe solution. Generally efficient. 180 * see : http://stackoverflow.com/a/32095106/646947 181 */ 182 183 static U32 XXH_read32(const void* memPtr) 184 { 185 U32 val; 186 memcpy(&val, memPtr, sizeof(val)); 187 return val; 188 } 189 190 static U64 XXH_read64(const void* memPtr) 191 { 192 U64 val; 193 memcpy(&val, memPtr, sizeof(val)); 194 return val; 195 } 196 197 #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */ 198 199 200 /* **************************************** 201 * Compiler-specific Functions and Macros 202 ******************************************/ 203 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) 204 205 /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */ 206 #if defined(_MSC_VER) 207 # define XXH_rotl32(x,r) _rotl(x,r) 208 # define XXH_rotl64(x,r) _rotl64(x,r) 209 #else 210 #if defined(__ICCARM__) 211 # include <intrinsics.h> 212 # define XXH_rotl32(x,r) __ROR(x,(32 - r)) 213 #else 214 # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r))) 215 #endif 216 # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r))) 217 #endif 218 219 #if defined(_MSC_VER) /* Visual Studio */ 220 # define XXH_swap32 _byteswap_ulong 221 # define XXH_swap64 _byteswap_uint64 222 #elif GCC_VERSION >= 403 223 # define XXH_swap32 __builtin_bswap32 224 # define XXH_swap64 __builtin_bswap64 225 #else 226 static U32 XXH_swap32 (U32 x) 227 { 228 return ((x << 24) & 0xff000000 ) | 229 ((x << 8) & 0x00ff0000 ) | 230 ((x >> 8) & 0x0000ff00 ) | 231 ((x >> 24) & 0x000000ff ); 232 } 233 static U64 XXH_swap64 (U64 x) 234 { 235 return ((x << 56) & 0xff00000000000000ULL) | 236 ((x << 40) & 0x00ff000000000000ULL) | 237 ((x << 24) & 0x0000ff0000000000ULL) | 238 ((x << 8) & 0x000000ff00000000ULL) | 239 ((x >> 8) & 0x00000000ff000000ULL) | 240 ((x >> 24) & 0x0000000000ff0000ULL) | 241 ((x >> 40) & 0x000000000000ff00ULL) | 242 ((x >> 56) & 0x00000000000000ffULL); 243 } 244 #endif 245 246 247 /* ************************************* 248 * Architecture Macros 249 ***************************************/ 250 typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; 251 252 /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */ 253 #ifndef XXH_CPU_LITTLE_ENDIAN 254 static const int g_one = 1; 255 # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one)) 256 #endif 257 258 259 /* *************************** 260 * Memory reads 261 *****************************/ 262 typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment; 263 264 FORCE_INLINE_TEMPLATE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align) 265 { 266 if (align==XXH_unaligned) 267 return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr)); 268 else 269 return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr); 270 } 271 272 FORCE_INLINE_TEMPLATE U32 XXH_readLE32(const void* ptr, XXH_endianess endian) 273 { 274 return XXH_readLE32_align(ptr, endian, XXH_unaligned); 275 } 276 277 static U32 XXH_readBE32(const void* ptr) 278 { 279 return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr); 280 } 281 282 FORCE_INLINE_TEMPLATE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) 283 { 284 if (align==XXH_unaligned) 285 return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr)); 286 else 287 return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr); 288 } 289 290 FORCE_INLINE_TEMPLATE U64 XXH_readLE64(const void* ptr, XXH_endianess endian) 291 { 292 return XXH_readLE64_align(ptr, endian, XXH_unaligned); 293 } 294 295 static U64 XXH_readBE64(const void* ptr) 296 { 297 return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr); 298 } 299 300 301 /* ************************************* 302 * Macros 303 ***************************************/ 304 #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ 305 306 307 /* ************************************* 308 * Constants 309 ***************************************/ 310 static const U32 PRIME32_1 = 2654435761U; 311 static const U32 PRIME32_2 = 2246822519U; 312 static const U32 PRIME32_3 = 3266489917U; 313 static const U32 PRIME32_4 = 668265263U; 314 static const U32 PRIME32_5 = 374761393U; 315 316 static const U64 PRIME64_1 = 11400714785074694791ULL; 317 static const U64 PRIME64_2 = 14029467366897019727ULL; 318 static const U64 PRIME64_3 = 1609587929392839161ULL; 319 static const U64 PRIME64_4 = 9650029242287828579ULL; 320 static const U64 PRIME64_5 = 2870177450012600261ULL; 321 322 XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; } 323 324 325 /* ************************** 326 * Utils 327 ****************************/ 328 XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState) 329 { 330 memcpy(dstState, srcState, sizeof(*dstState)); 331 } 332 333 XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState) 334 { 335 memcpy(dstState, srcState, sizeof(*dstState)); 336 } 337 338 339 /* *************************** 340 * Simple Hash Functions 341 *****************************/ 342 343 static U32 XXH32_round(U32 seed, U32 input) 344 { 345 seed += input * PRIME32_2; 346 seed = XXH_rotl32(seed, 13); 347 seed *= PRIME32_1; 348 return seed; 349 } 350 351 FORCE_INLINE_TEMPLATE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align) 352 { 353 const BYTE* p = (const BYTE*)input; 354 const BYTE* bEnd = p + len; 355 U32 h32; 356 #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align) 357 358 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER 359 if (p==NULL) { 360 len=0; 361 bEnd=p=(const BYTE*)(size_t)16; 362 } 363 #endif 364 365 if (len>=16) { 366 const BYTE* const limit = bEnd - 16; 367 U32 v1 = seed + PRIME32_1 + PRIME32_2; 368 U32 v2 = seed + PRIME32_2; 369 U32 v3 = seed + 0; 370 U32 v4 = seed - PRIME32_1; 371 372 do { 373 v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4; 374 v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4; 375 v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4; 376 v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4; 377 } while (p<=limit); 378 379 h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); 380 } else { 381 h32 = seed + PRIME32_5; 382 } 383 384 h32 += (U32) len; 385 386 while (p+4<=bEnd) { 387 h32 += XXH_get32bits(p) * PRIME32_3; 388 h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; 389 p+=4; 390 } 391 392 while (p<bEnd) { 393 h32 += (*p) * PRIME32_5; 394 h32 = XXH_rotl32(h32, 11) * PRIME32_1 ; 395 p++; 396 } 397 398 h32 ^= h32 >> 15; 399 h32 *= PRIME32_2; 400 h32 ^= h32 >> 13; 401 h32 *= PRIME32_3; 402 h32 ^= h32 >> 16; 403 404 return h32; 405 } 406 407 408 XXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed) 409 { 410 #if 0 411 /* Simple version, good for code maintenance, but unfortunately slow for small inputs */ 412 XXH32_CREATESTATE_STATIC(state); 413 XXH32_reset(state, seed); 414 XXH32_update(state, input, len); 415 return XXH32_digest(state); 416 #else 417 XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; 418 419 if (XXH_FORCE_ALIGN_CHECK) { 420 if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */ 421 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 422 return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); 423 else 424 return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); 425 } } 426 427 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 428 return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); 429 else 430 return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); 431 #endif 432 } 433 434 435 static U64 XXH64_round(U64 acc, U64 input) 436 { 437 acc += input * PRIME64_2; 438 acc = XXH_rotl64(acc, 31); 439 acc *= PRIME64_1; 440 return acc; 441 } 442 443 static U64 XXH64_mergeRound(U64 acc, U64 val) 444 { 445 val = XXH64_round(0, val); 446 acc ^= val; 447 acc = acc * PRIME64_1 + PRIME64_4; 448 return acc; 449 } 450 451 FORCE_INLINE_TEMPLATE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align) 452 { 453 const BYTE* p = (const BYTE*)input; 454 const BYTE* const bEnd = p + len; 455 U64 h64; 456 #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align) 457 458 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER 459 if (p==NULL) { 460 len=0; 461 bEnd=p=(const BYTE*)(size_t)32; 462 } 463 #endif 464 465 if (len>=32) { 466 const BYTE* const limit = bEnd - 32; 467 U64 v1 = seed + PRIME64_1 + PRIME64_2; 468 U64 v2 = seed + PRIME64_2; 469 U64 v3 = seed + 0; 470 U64 v4 = seed - PRIME64_1; 471 472 do { 473 v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8; 474 v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8; 475 v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8; 476 v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8; 477 } while (p<=limit); 478 479 h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); 480 h64 = XXH64_mergeRound(h64, v1); 481 h64 = XXH64_mergeRound(h64, v2); 482 h64 = XXH64_mergeRound(h64, v3); 483 h64 = XXH64_mergeRound(h64, v4); 484 485 } else { 486 h64 = seed + PRIME64_5; 487 } 488 489 h64 += (U64) len; 490 491 while (p+8<=bEnd) { 492 U64 const k1 = XXH64_round(0, XXH_get64bits(p)); 493 h64 ^= k1; 494 h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; 495 p+=8; 496 } 497 498 if (p+4<=bEnd) { 499 h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; 500 h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; 501 p+=4; 502 } 503 504 while (p<bEnd) { 505 h64 ^= (*p) * PRIME64_5; 506 h64 = XXH_rotl64(h64, 11) * PRIME64_1; 507 p++; 508 } 509 510 h64 ^= h64 >> 33; 511 h64 *= PRIME64_2; 512 h64 ^= h64 >> 29; 513 h64 *= PRIME64_3; 514 h64 ^= h64 >> 32; 515 516 return h64; 517 } 518 519 520 XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed) 521 { 522 #if 0 523 /* Simple version, good for code maintenance, but unfortunately slow for small inputs */ 524 XXH64_CREATESTATE_STATIC(state); 525 XXH64_reset(state, seed); 526 XXH64_update(state, input, len); 527 return XXH64_digest(state); 528 #else 529 XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; 530 531 if (XXH_FORCE_ALIGN_CHECK) { 532 if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */ 533 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 534 return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); 535 else 536 return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); 537 } } 538 539 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 540 return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); 541 else 542 return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); 543 #endif 544 } 545 546 547 /* ************************************************** 548 * Advanced Hash Functions 549 ****************************************************/ 550 551 XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void) 552 { 553 return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t)); 554 } 555 XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr) 556 { 557 XXH_free(statePtr); 558 return XXH_OK; 559 } 560 561 XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void) 562 { 563 return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t)); 564 } 565 XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr) 566 { 567 XXH_free(statePtr); 568 return XXH_OK; 569 } 570 571 572 /*** Hash feed ***/ 573 574 XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed) 575 { 576 XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ 577 memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */ 578 state.v1 = seed + PRIME32_1 + PRIME32_2; 579 state.v2 = seed + PRIME32_2; 580 state.v3 = seed + 0; 581 state.v4 = seed - PRIME32_1; 582 memcpy(statePtr, &state, sizeof(state)); 583 return XXH_OK; 584 } 585 586 587 XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed) 588 { 589 XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ 590 memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */ 591 state.v1 = seed + PRIME64_1 + PRIME64_2; 592 state.v2 = seed + PRIME64_2; 593 state.v3 = seed + 0; 594 state.v4 = seed - PRIME64_1; 595 memcpy(statePtr, &state, sizeof(state)); 596 return XXH_OK; 597 } 598 599 600 FORCE_INLINE_TEMPLATE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian) 601 { 602 const BYTE* p = (const BYTE*)input; 603 const BYTE* const bEnd = p + len; 604 605 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER 606 if (input==NULL) return XXH_ERROR; 607 #endif 608 609 state->total_len_32 += (unsigned)len; 610 state->large_len |= (len>=16) | (state->total_len_32>=16); 611 612 if (state->memsize + len < 16) { /* fill in tmp buffer */ 613 XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len); 614 state->memsize += (unsigned)len; 615 return XXH_OK; 616 } 617 618 if (state->memsize) { /* some data left from previous update */ 619 XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize); 620 { const U32* p32 = state->mem32; 621 state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++; 622 state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++; 623 state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++; 624 state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++; 625 } 626 p += 16-state->memsize; 627 state->memsize = 0; 628 } 629 630 if (p <= bEnd-16) { 631 const BYTE* const limit = bEnd - 16; 632 U32 v1 = state->v1; 633 U32 v2 = state->v2; 634 U32 v3 = state->v3; 635 U32 v4 = state->v4; 636 637 do { 638 v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4; 639 v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4; 640 v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4; 641 v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4; 642 } while (p<=limit); 643 644 state->v1 = v1; 645 state->v2 = v2; 646 state->v3 = v3; 647 state->v4 = v4; 648 } 649 650 if (p < bEnd) { 651 XXH_memcpy(state->mem32, p, (size_t)(bEnd-p)); 652 state->memsize = (unsigned)(bEnd-p); 653 } 654 655 return XXH_OK; 656 } 657 658 XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len) 659 { 660 XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; 661 662 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 663 return XXH32_update_endian(state_in, input, len, XXH_littleEndian); 664 else 665 return XXH32_update_endian(state_in, input, len, XXH_bigEndian); 666 } 667 668 669 670 FORCE_INLINE_TEMPLATE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian) 671 { 672 const BYTE * p = (const BYTE*)state->mem32; 673 const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize; 674 U32 h32; 675 676 if (state->large_len) { 677 h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); 678 } else { 679 h32 = state->v3 /* == seed */ + PRIME32_5; 680 } 681 682 h32 += state->total_len_32; 683 684 while (p+4<=bEnd) { 685 h32 += XXH_readLE32(p, endian) * PRIME32_3; 686 h32 = XXH_rotl32(h32, 17) * PRIME32_4; 687 p+=4; 688 } 689 690 while (p<bEnd) { 691 h32 += (*p) * PRIME32_5; 692 h32 = XXH_rotl32(h32, 11) * PRIME32_1; 693 p++; 694 } 695 696 h32 ^= h32 >> 15; 697 h32 *= PRIME32_2; 698 h32 ^= h32 >> 13; 699 h32 *= PRIME32_3; 700 h32 ^= h32 >> 16; 701 702 return h32; 703 } 704 705 706 XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in) 707 { 708 XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; 709 710 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 711 return XXH32_digest_endian(state_in, XXH_littleEndian); 712 else 713 return XXH32_digest_endian(state_in, XXH_bigEndian); 714 } 715 716 717 718 /* **** XXH64 **** */ 719 720 FORCE_INLINE_TEMPLATE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian) 721 { 722 const BYTE* p = (const BYTE*)input; 723 const BYTE* const bEnd = p + len; 724 725 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER 726 if (input==NULL) return XXH_ERROR; 727 #endif 728 729 state->total_len += len; 730 731 if (state->memsize + len < 32) { /* fill in tmp buffer */ 732 XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len); 733 state->memsize += (U32)len; 734 return XXH_OK; 735 } 736 737 if (state->memsize) { /* tmp buffer is full */ 738 XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize); 739 state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian)); 740 state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian)); 741 state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian)); 742 state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian)); 743 p += 32-state->memsize; 744 state->memsize = 0; 745 } 746 747 if (p+32 <= bEnd) { 748 const BYTE* const limit = bEnd - 32; 749 U64 v1 = state->v1; 750 U64 v2 = state->v2; 751 U64 v3 = state->v3; 752 U64 v4 = state->v4; 753 754 do { 755 v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8; 756 v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8; 757 v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8; 758 v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8; 759 } while (p<=limit); 760 761 state->v1 = v1; 762 state->v2 = v2; 763 state->v3 = v3; 764 state->v4 = v4; 765 } 766 767 if (p < bEnd) { 768 XXH_memcpy(state->mem64, p, (size_t)(bEnd-p)); 769 state->memsize = (unsigned)(bEnd-p); 770 } 771 772 return XXH_OK; 773 } 774 775 XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len) 776 { 777 XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; 778 779 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 780 return XXH64_update_endian(state_in, input, len, XXH_littleEndian); 781 else 782 return XXH64_update_endian(state_in, input, len, XXH_bigEndian); 783 } 784 785 786 787 FORCE_INLINE_TEMPLATE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian) 788 { 789 const BYTE * p = (const BYTE*)state->mem64; 790 const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize; 791 U64 h64; 792 793 if (state->total_len >= 32) { 794 U64 const v1 = state->v1; 795 U64 const v2 = state->v2; 796 U64 const v3 = state->v3; 797 U64 const v4 = state->v4; 798 799 h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); 800 h64 = XXH64_mergeRound(h64, v1); 801 h64 = XXH64_mergeRound(h64, v2); 802 h64 = XXH64_mergeRound(h64, v3); 803 h64 = XXH64_mergeRound(h64, v4); 804 } else { 805 h64 = state->v3 + PRIME64_5; 806 } 807 808 h64 += (U64) state->total_len; 809 810 while (p+8<=bEnd) { 811 U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian)); 812 h64 ^= k1; 813 h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; 814 p+=8; 815 } 816 817 if (p+4<=bEnd) { 818 h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1; 819 h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; 820 p+=4; 821 } 822 823 while (p<bEnd) { 824 h64 ^= (*p) * PRIME64_5; 825 h64 = XXH_rotl64(h64, 11) * PRIME64_1; 826 p++; 827 } 828 829 h64 ^= h64 >> 33; 830 h64 *= PRIME64_2; 831 h64 ^= h64 >> 29; 832 h64 *= PRIME64_3; 833 h64 ^= h64 >> 32; 834 835 return h64; 836 } 837 838 839 XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in) 840 { 841 XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; 842 843 if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) 844 return XXH64_digest_endian(state_in, XXH_littleEndian); 845 else 846 return XXH64_digest_endian(state_in, XXH_bigEndian); 847 } 848 849 850 /* ************************** 851 * Canonical representation 852 ****************************/ 853 854 /*! Default XXH result types are basic unsigned 32 and 64 bits. 855 * The canonical representation follows human-readable write convention, aka big-endian (large digits first). 856 * These functions allow transformation of hash result into and from its canonical format. 857 * This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs. 858 */ 859 860 XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash) 861 { 862 XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t)); 863 if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash); 864 memcpy(dst, &hash, sizeof(*dst)); 865 } 866 867 XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash) 868 { 869 XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t)); 870 if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash); 871 memcpy(dst, &hash, sizeof(*dst)); 872 } 873 874 XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src) 875 { 876 return XXH_readBE32(src); 877 } 878 879 XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src) 880 { 881 return XXH_readBE64(src); 882 } 883