ReactOS  0.4.14-dev-358-gbef841c
xxhash.c
Go to the documentation of this file.
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 ***************************************/
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 # define XXH_FORCE_MEMORY_ACCESS 1
58 # endif
59 #endif
60 
66 /* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */
67 
76 #ifndef XXH_FORCE_NATIVE_FORMAT /* can be defined externally */
77 # define XXH_FORCE_NATIVE_FORMAT 0
78 #endif
79 
86 #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
87 # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
88 # define XXH_FORCE_ALIGN_CHECK 0
89 # else
90 # define XXH_FORCE_ALIGN_CHECK 1
91 # endif
92 #endif
93 
94 
95 /* *************************************
96 * Includes & Memory related functions
97 ***************************************/
98 /* Modify the local functions below should you wish to use some other memory routines */
99 /* for malloc(), free() */
100 #include <stdlib.h>
101 #include <stddef.h> /* size_t */
102 #include <ntifs.h>
103 #include <ntddk.h>
104 
105 #define XXH_ALLOC_TAG 0x32485858 // "XXH "
106 
107 static void* XXH_malloc(size_t s) {
109 }
110 
111 static void XXH_free (void* p) {
112  ExFreePool(p);
113 }
114 
115 /* for memcpy() */
116 #include <string.h>
117 static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
118 
119 #ifndef XXH_STATIC_LINKING_ONLY
120 # define XXH_STATIC_LINKING_ONLY
121 #endif
122 #include "xxhash.h"
123 
124 
125 /* *************************************
126 * Compiler Specific Options
127 ***************************************/
128 #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
129 # define INLINE_KEYWORD inline
130 #else
131 # define INLINE_KEYWORD
132 #endif
133 
134 #if defined(__GNUC__)
135 # define FORCE_INLINE_ATTR __attribute__((always_inline))
136 #elif defined(_MSC_VER)
137 # define FORCE_INLINE_ATTR __forceinline
138 #else
139 # define FORCE_INLINE_ATTR
140 #endif
141 
142 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
143 
144 
145 #ifdef _MSC_VER
146 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
147 #endif
148 
149 
150 /* *************************************
151 * Basic Types
152 ***************************************/
153 #ifndef MEM_MODULE
154 # define MEM_MODULE
155 # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
156 # include <stdint.h>
157  typedef uint8_t BYTE;
158  typedef uint16_t U16;
159  typedef uint32_t U32;
160  typedef int32_t S32;
161  typedef uint64_t U64;
162 # else
163  typedef unsigned char BYTE;
164  typedef unsigned short U16;
165  typedef unsigned int U32;
166  typedef signed int S32;
167  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. */
168 # endif
169 #endif
170 
171 
172 #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
173 
174 /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
175 static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; }
176 static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; }
177 
178 #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
179 
180 /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
181 /* currently only defined for gcc and icc */
182 typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign;
183 
184 static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
185 static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
186 
187 #else
188 
189 /* portable and safe solution. Generally efficient.
190  * see : http://stackoverflow.com/a/32095106/646947
191  */
192 
193 static U32 XXH_read32(const void* memPtr)
194 {
195  U32 val;
196  memcpy(&val, memPtr, sizeof(val));
197  return val;
198 }
199 
200 static U64 XXH_read64(const void* memPtr)
201 {
202  U64 val;
203  memcpy(&val, memPtr, sizeof(val));
204  return val;
205 }
206 
207 #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
208 
209 
210 /* ****************************************
211 * Compiler-specific Functions and Macros
212 ******************************************/
213 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
214 
215 /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
216 #if defined(_MSC_VER)
217 # define XXH_rotl32(x,r) _rotl(x,r)
218 # define XXH_rotl64(x,r) _rotl64(x,r)
219 #else
220 # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
221 # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
222 #endif
223 
224 #if defined(_MSC_VER) /* Visual Studio */
225 # define XXH_swap32 _byteswap_ulong
226 # define XXH_swap64 _byteswap_uint64
227 #elif GCC_VERSION >= 403
228 # define XXH_swap32 __builtin_bswap32
229 # define XXH_swap64 __builtin_bswap64
230 #else
231 static U32 XXH_swap32 (U32 x)
232 {
233  return ((x << 24) & 0xff000000 ) |
234  ((x << 8) & 0x00ff0000 ) |
235  ((x >> 8) & 0x0000ff00 ) |
236  ((x >> 24) & 0x000000ff );
237 }
238 static U64 XXH_swap64 (U64 x)
239 {
240  return ((x << 56) & 0xff00000000000000ULL) |
241  ((x << 40) & 0x00ff000000000000ULL) |
242  ((x << 24) & 0x0000ff0000000000ULL) |
243  ((x << 8) & 0x000000ff00000000ULL) |
244  ((x >> 8) & 0x00000000ff000000ULL) |
245  ((x >> 24) & 0x0000000000ff0000ULL) |
246  ((x >> 40) & 0x000000000000ff00ULL) |
247  ((x >> 56) & 0x00000000000000ffULL);
248 }
249 #endif
250 
251 
252 /* *************************************
253 * Architecture Macros
254 ***************************************/
256 
257 /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
258 #ifndef XXH_CPU_LITTLE_ENDIAN
259  static const int g_one = 1;
260 # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one))
261 #endif
262 
263 
264 /* ***************************
265 * Memory reads
266 *****************************/
268 
270 {
271  if (align==XXH_unaligned)
272  return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
273  else
274  return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);
275 }
276 
278 {
279  return XXH_readLE32_align(ptr, endian, XXH_unaligned);
280 }
281 
282 static U32 XXH_readBE32(const void* ptr)
283 {
285 }
286 
288 {
289  if (align==XXH_unaligned)
290  return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
291  else
292  return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);
293 }
294 
296 {
297  return XXH_readLE64_align(ptr, endian, XXH_unaligned);
298 }
299 
300 static U64 XXH_readBE64(const void* ptr)
301 {
303 }
304 
305 
306 /* *************************************
307 * Macros
308 ***************************************/
309 #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
310 
311 
312 /* *************************************
313 * Constants
314 ***************************************/
315 static const U32 PRIME32_1 = 2654435761U;
316 static const U32 PRIME32_2 = 2246822519U;
317 static const U32 PRIME32_3 = 3266489917U;
318 static const U32 PRIME32_4 = 668265263U;
319 static const U32 PRIME32_5 = 374761393U;
320 
321 static const U64 PRIME64_1 = 11400714785074694791ULL;
322 static const U64 PRIME64_2 = 14029467366897019727ULL;
323 static const U64 PRIME64_3 = 1609587929392839161ULL;
324 static const U64 PRIME64_4 = 9650029242287828579ULL;
325 static const U64 PRIME64_5 = 2870177450012600261ULL;
326 
328 
329 
330 /* **************************
331 * Utils
332 ****************************/
334 {
335  memcpy(dstState, srcState, sizeof(*dstState));
336 }
337 
339 {
340  memcpy(dstState, srcState, sizeof(*dstState));
341 }
342 
343 
344 /* ***************************
345 * Simple Hash Functions
346 *****************************/
347 
348 static U32 XXH32_round(U32 seed, U32 input)
349 {
350  seed += input * PRIME32_2;
351  seed = XXH_rotl32(seed, 13);
352  seed *= PRIME32_1;
353  return seed;
354 }
355 
357 {
358  const BYTE* p = (const BYTE*)input;
359  const BYTE* bEnd = p + len;
360  U32 h32;
361 #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)
362 
363 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
364  if (p==NULL) {
365  len=0;
366  bEnd=p=(const BYTE*)(size_t)16;
367  }
368 #endif
369 
370  if (len>=16) {
371  const BYTE* const limit = bEnd - 16;
372  U32 v1 = seed + PRIME32_1 + PRIME32_2;
373  U32 v2 = seed + PRIME32_2;
374  U32 v3 = seed + 0;
375  U32 v4 = seed - PRIME32_1;
376 
377  do {
378  v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4;
379  v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4;
380  v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4;
381  v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4;
382  } while (p<=limit);
383 
384  h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
385  } else {
386  h32 = seed + PRIME32_5;
387  }
388 
389  h32 += (U32) len;
390 
391  while (p+4<=bEnd) {
392  h32 += XXH_get32bits(p) * PRIME32_3;
393  h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
394  p+=4;
395  }
396 
397  while (p<bEnd) {
398  h32 += (*p) * PRIME32_5;
399  h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
400  p++;
401  }
402 
403  h32 ^= h32 >> 15;
404  h32 *= PRIME32_2;
405  h32 ^= h32 >> 13;
406  h32 *= PRIME32_3;
407  h32 ^= h32 >> 16;
408 
409  return h32;
410 }
411 
412 
413 XXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed)
414 {
415 #if 0
416  /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
417  XXH32_CREATESTATE_STATIC(state);
418  XXH32_reset(state, seed);
420  return XXH32_digest(state);
421 #else
423 
424  if (XXH_FORCE_ALIGN_CHECK) {
425  if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
426  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
428  else
430  } }
431 
432  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
434  else
436 #endif
437 }
438 
439 
441 {
442  acc += input * PRIME64_2;
443  acc = XXH_rotl64(acc, 31);
444  acc *= PRIME64_1;
445  return acc;
446 }
447 
449 {
450  val = XXH64_round(0, val);
451  acc ^= val;
452  acc = acc * PRIME64_1 + PRIME64_4;
453  return acc;
454 }
455 
457 {
458  const BYTE* p = (const BYTE*)input;
459  const BYTE* const bEnd = p + len;
460  U64 h64;
461 #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)
462 
463 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
464  if (p==NULL) {
465  len=0;
466  bEnd=p=(const BYTE*)(size_t)32;
467  }
468 #endif
469 
470  if (len>=32) {
471  const BYTE* const limit = bEnd - 32;
472  U64 v1 = seed + PRIME64_1 + PRIME64_2;
473  U64 v2 = seed + PRIME64_2;
474  U64 v3 = seed + 0;
475  U64 v4 = seed - PRIME64_1;
476 
477  do {
478  v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;
479  v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;
480  v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;
481  v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;
482  } while (p<=limit);
483 
484  h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
485  h64 = XXH64_mergeRound(h64, v1);
486  h64 = XXH64_mergeRound(h64, v2);
487  h64 = XXH64_mergeRound(h64, v3);
488  h64 = XXH64_mergeRound(h64, v4);
489 
490  } else {
491  h64 = seed + PRIME64_5;
492  }
493 
494  h64 += (U64) len;
495 
496  while (p+8<=bEnd) {
497  U64 const k1 = XXH64_round(0, XXH_get64bits(p));
498  h64 ^= k1;
499  h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
500  p+=8;
501  }
502 
503  if (p+4<=bEnd) {
504  h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
505  h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
506  p+=4;
507  }
508 
509  while (p<bEnd) {
510  h64 ^= (*p) * PRIME64_5;
511  h64 = XXH_rotl64(h64, 11) * PRIME64_1;
512  p++;
513  }
514 
515  h64 ^= h64 >> 33;
516  h64 *= PRIME64_2;
517  h64 ^= h64 >> 29;
518  h64 *= PRIME64_3;
519  h64 ^= h64 >> 32;
520 
521  return h64;
522 }
523 
524 
525 XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
526 {
527 #if 0
528  /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
529  XXH64_CREATESTATE_STATIC(state);
530  XXH64_reset(state, seed);
532  return XXH64_digest(state);
533 #else
535 
536  if (XXH_FORCE_ALIGN_CHECK) {
537  if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
538  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
540  else
542  } }
543 
544  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
546  else
548 #endif
549 }
550 
551 
552 /* **************************************************
553 * Advanced Hash Functions
554 ****************************************************/
555 
557 {
558  return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
559 }
561 {
562  XXH_free(statePtr);
563  return XXH_OK;
564 }
565 
567 {
568  return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
569 }
571 {
572  XXH_free(statePtr);
573  return XXH_OK;
574 }
575 
576 
577 /*** Hash feed ***/
578 
580 {
581  XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
582  memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */
583  state.v1 = seed + PRIME32_1 + PRIME32_2;
584  state.v2 = seed + PRIME32_2;
585  state.v3 = seed + 0;
586  state.v4 = seed - PRIME32_1;
587  memcpy(statePtr, &state, sizeof(state));
588  return XXH_OK;
589 }
590 
591 
592 XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
593 {
594  XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
595  memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
596  state.v1 = seed + PRIME64_1 + PRIME64_2;
597  state.v2 = seed + PRIME64_2;
598  state.v3 = seed + 0;
599  state.v4 = seed - PRIME64_1;
600  memcpy(statePtr, &state, sizeof(state));
601  return XXH_OK;
602 }
603 
604 
606 {
607  const BYTE* p = (const BYTE*)input;
608  const BYTE* const bEnd = p + len;
609 
610 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
611  if (input==NULL) return XXH_ERROR;
612 #endif
613 
614  state->total_len_32 += (unsigned)len;
615  state->large_len |= (len>=16) | (state->total_len_32>=16);
616 
617  if (state->memsize + len < 16) { /* fill in tmp buffer */
618  XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len);
619  state->memsize += (unsigned)len;
620  return XXH_OK;
621  }
622 
623  if (state->memsize) { /* some data left from previous update */
624  XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize);
625  { const U32* p32 = state->mem32;
626  state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++;
627  state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++;
628  state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++;
629  state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++;
630  }
631  p += 16-state->memsize;
632  state->memsize = 0;
633  }
634 
635  if (p <= bEnd-16) {
636  const BYTE* const limit = bEnd - 16;
637  U32 v1 = state->v1;
638  U32 v2 = state->v2;
639  U32 v3 = state->v3;
640  U32 v4 = state->v4;
641 
642  do {
643  v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4;
644  v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4;
645  v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4;
646  v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4;
647  } while (p<=limit);
648 
649  state->v1 = v1;
650  state->v2 = v2;
651  state->v3 = v3;
652  state->v4 = v4;
653  }
654 
655  if (p < bEnd) {
656  XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
657  state->memsize = (unsigned)(bEnd-p);
658  }
659 
660  return XXH_OK;
661 }
662 
664 {
666 
667  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
668  return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
669  else
670  return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
671 }
672 
673 
674 
676 {
677  const BYTE * p = (const BYTE*)state->mem32;
678  const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize;
679  U32 h32;
680 
681  if (state->large_len) {
682  h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
683  } else {
684  h32 = state->v3 /* == seed */ + PRIME32_5;
685  }
686 
687  h32 += state->total_len_32;
688 
689  while (p+4<=bEnd) {
690  h32 += XXH_readLE32(p, endian) * PRIME32_3;
691  h32 = XXH_rotl32(h32, 17) * PRIME32_4;
692  p+=4;
693  }
694 
695  while (p<bEnd) {
696  h32 += (*p) * PRIME32_5;
697  h32 = XXH_rotl32(h32, 11) * PRIME32_1;
698  p++;
699  }
700 
701  h32 ^= h32 >> 15;
702  h32 *= PRIME32_2;
703  h32 ^= h32 >> 13;
704  h32 *= PRIME32_3;
705  h32 ^= h32 >> 16;
706 
707  return h32;
708 }
709 
710 
711 XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in)
712 {
714 
715  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
716  return XXH32_digest_endian(state_in, XXH_littleEndian);
717  else
718  return XXH32_digest_endian(state_in, XXH_bigEndian);
719 }
720 
721 
722 
723 /* **** XXH64 **** */
724 
726 {
727  const BYTE* p = (const BYTE*)input;
728  const BYTE* const bEnd = p + len;
729 
730 #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
731  if (input==NULL) return XXH_ERROR;
732 #endif
733 
734  state->total_len += len;
735 
736  if (state->memsize + len < 32) { /* fill in tmp buffer */
737  XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);
738  state->memsize += (U32)len;
739  return XXH_OK;
740  }
741 
742  if (state->memsize) { /* tmp buffer is full */
743  XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);
744  state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));
745  state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));
746  state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));
747  state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));
748  p += 32-state->memsize;
749  state->memsize = 0;
750  }
751 
752  if (p+32 <= bEnd) {
753  const BYTE* const limit = bEnd - 32;
754  U64 v1 = state->v1;
755  U64 v2 = state->v2;
756  U64 v3 = state->v3;
757  U64 v4 = state->v4;
758 
759  do {
760  v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;
761  v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;
762  v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;
763  v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;
764  } while (p<=limit);
765 
766  state->v1 = v1;
767  state->v2 = v2;
768  state->v3 = v3;
769  state->v4 = v4;
770  }
771 
772  if (p < bEnd) {
773  XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
774  state->memsize = (unsigned)(bEnd-p);
775  }
776 
777  return XXH_OK;
778 }
779 
781 {
783 
784  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
785  return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
786  else
787  return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
788 }
789 
790 
791 
793 {
794  const BYTE * p = (const BYTE*)state->mem64;
795  const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize;
796  U64 h64;
797 
798  if (state->total_len >= 32) {
799  U64 const v1 = state->v1;
800  U64 const v2 = state->v2;
801  U64 const v3 = state->v3;
802  U64 const v4 = state->v4;
803 
804  h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
805  h64 = XXH64_mergeRound(h64, v1);
806  h64 = XXH64_mergeRound(h64, v2);
807  h64 = XXH64_mergeRound(h64, v3);
808  h64 = XXH64_mergeRound(h64, v4);
809  } else {
810  h64 = state->v3 + PRIME64_5;
811  }
812 
813  h64 += (U64) state->total_len;
814 
815  while (p+8<=bEnd) {
816  U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian));
817  h64 ^= k1;
818  h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
819  p+=8;
820  }
821 
822  if (p+4<=bEnd) {
823  h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1;
824  h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
825  p+=4;
826  }
827 
828  while (p<bEnd) {
829  h64 ^= (*p) * PRIME64_5;
830  h64 = XXH_rotl64(h64, 11) * PRIME64_1;
831  p++;
832  }
833 
834  h64 ^= h64 >> 33;
835  h64 *= PRIME64_2;
836  h64 ^= h64 >> 29;
837  h64 *= PRIME64_3;
838  h64 ^= h64 >> 32;
839 
840  return h64;
841 }
842 
843 
844 XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)
845 {
847 
848  if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
849  return XXH64_digest_endian(state_in, XXH_littleEndian);
850  else
851  return XXH64_digest_endian(state_in, XXH_bigEndian);
852 }
853 
854 
855 /* **************************
856 * Canonical representation
857 ****************************/
858 
866 {
869  memcpy(dst, &hash, sizeof(*dst));
870 }
871 
873 {
876  memcpy(dst, &hash, sizeof(*dst));
877 }
878 
880 {
881  return XXH_readBE32(src);
882 }
883 
885 {
886  return XXH_readBE64(src);
887 }
static U64 XXH_swap64(U64 x)
Definition: xxhash.c:238
FORCE_INLINE_TEMPLATE XXH_errorcode XXH64_update_endian(XXH64_state_t *state, const void *input, size_t len, XXH_endianess endian)
Definition: xxhash.c:725
static U32 XXH32_round(U32 seed, U32 input)
Definition: xxhash.c:348
GLuint GLenum GLsizei GLsizei GLint GLint GLboolean packed
Definition: glext.h:9271
XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t *statePtr, unsigned int seed)
Definition: xxhash.c:579
unsigned char BYTE
Definition: xxhash.c:163
struct XXH64_state_s XXH64_state_t
Definition: xxhash.h:183
XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t *dst, XXH32_hash_t hash)
Definition: xxhash.c:865
#define U(x)
Definition: wordpad.c:44
static const int g_one
Definition: xxhash.c:259
#define XXH_rotl64(x, r)
Definition: xxhash.c:221
XXH_PUBLIC_API XXH_errorcode XXH32_update(XXH32_state_t *state_in, const void *input, size_t len)
Definition: xxhash.c:663
static const U32 PRIME32_5
Definition: xxhash.c:319
unsigned int U32
Definition: mem.h:77
FORCE_INLINE_TEMPLATE U32 XXH32_digest_endian(const XXH32_state_t *state, XXH_endianess endian)
Definition: xxhash.c:675
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
static U32 XXH_read32(const void *memPtr)
Definition: xxhash.c:193
int align(int length, int align)
Definition: dsound8.c:36
#define XXH_ALLOC_TAG
Definition: xxhash.c:105
unsigned short int uint16_t
Definition: acefiex.h:54
FORCE_INLINE_TEMPLATE U64 XXH_readLE64_align(const void *ptr, XXH_endianess endian, XXH_alignment align)
Definition: xxhash.c:287
ULONG32 u32
Definition: btrfs.h:14
#define XXH_VERSION_NUMBER
Definition: xxhash.h:153
#define XXH_CPU_LITTLE_ENDIAN
Definition: xxhash.c:260
static U64 XXH64_round(U64 acc, U64 input)
Definition: xxhash.c:440
GLint limit
Definition: glext.h:10326
FORCE_INLINE_TEMPLATE U64 XXH64_endian_align(const void *input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
Definition: xxhash.c:456
XXH_PUBLIC_API unsigned long long XXH64_digest(const XXH64_state_t *state_in)
Definition: xxhash.c:844
#define __attribute__(x)
Definition: wpp_private.h:207
static const U64 PRIME64_2
Definition: xxhash.c:322
FORCE_INLINE_TEMPLATE U32 XXH32_endian_align(const void *input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)
Definition: xxhash.c:356
while(1)
Definition: macro.lex.yy.c:740
static U64 XXH_read64(const void *memPtr)
Definition: xxhash.c:200
XXH_PUBLIC_API XXH64_state_t * XXH64_createState(void)
Definition: xxhash.c:566
static void * XXH_malloc(size_t s)
Definition: xxhash.c:107
static U64 XXH64_mergeRound(U64 acc, U64 val)
Definition: xxhash.c:448
static const U32 PRIME32_3
Definition: xxhash.c:317
static const U32 PRIME32_1
Definition: xxhash.c:315
XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t *statePtr)
Definition: xxhash.c:560
static PVOID ptr
Definition: dispmode.c:27
XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t *statePtr)
Definition: xxhash.c:570
XXH_PUBLIC_API unsigned int XXH32(const void *input, size_t len, unsigned int seed)
Definition: xxhash.c:413
static U32 XXH_readBE32(const void *ptr)
Definition: xxhash.c:282
XXH_alignment
Definition: xxhash.c:267
smooth NULL
Definition: ftsmooth.c:416
#define XXH_get64bits(p)
#define XXH_rotl32(x, r)
Definition: xxhash.c:220
FORCE_INLINE_TEMPLATE U64 XXH64_digest_endian(const XXH64_state_t *state, XXH_endianess endian)
Definition: xxhash.c:792
static void * XXH_memcpy(void *dest, const void *src, size_t size)
Definition: xxhash.c:117
#define ULL(a, b)
Definition: format_msg.c:27
GLuint GLfloat * val
Definition: glext.h:7180
signed int S32
Definition: xxhash.c:166
#define restrict
Definition: xxhash.h:231
unsigned long long XXH64_hash_t
Definition: xxhash.h:161
static const U32 PRIME32_2
Definition: xxhash.c:316
unsigned int XXH32_hash_t
Definition: xxhash.h:160
GLsizeiptr size
Definition: glext.h:5919
if(!(yy_init))
Definition: macro.lex.yy.c:714
XXH_PUBLIC_API XXH_errorcode XXH64_update(XXH64_state_t *state_in, const void *input, size_t len)
Definition: xxhash.c:780
#define XXH_get32bits(p)
static U64 XXH_readBE64(const void *ptr)
Definition: xxhash.c:300
XXH_errorcode
Definition: xxhash.h:79
XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t *restrict dstState, const XXH32_state_t *restrict srcState)
Definition: xxhash.c:333
static void XXH_free(void *p)
Definition: xxhash.c:111
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
static const U64 PRIME64_4
Definition: xxhash.c:324
ULONG64 u64
Definition: btrfs.h:15
static const U64 PRIME64_1
Definition: xxhash.c:321
static const U64 PRIME64_3
Definition: xxhash.c:323
static int state
Definition: maze.c:121
Definition: xxhash.h:79
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
unsigned char BYTE
Definition: mem.h:68
GLdouble s
Definition: gl.h:2039
GLenum src
Definition: glext.h:6340
GLfloat GLfloat GLfloat GLfloat v3
Definition: glext.h:6064
BYTE uint8_t
Definition: msvideo1.c:66
XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t *src)
Definition: xxhash.c:884
XXH_PUBLIC_API unsigned long long XXH64(const void *input, size_t len, unsigned long long seed)
Definition: xxhash.c:525
INT32 int32_t
Definition: types.h:71
#define XXH_STATIC_ASSERT(c)
Definition: xxhash.c:309
GLfloat GLfloat GLfloat v2
Definition: glext.h:6063
XXH_PUBLIC_API XXH32_state_t * XXH32_createState(void)
Definition: xxhash.c:556
unsigned short U16
Definition: xxhash.c:164
UINT64 uint64_t
Definition: types.h:77
GLenum GLenum GLenum input
Definition: glext.h:9031
unsigned long long U64
Definition: xxhash.c:167
#define XXH_FORCE_ALIGN_CHECK
Definition: xxhash.c:90
unsigned long long U64
Definition: mem.h:81
GLenum GLenum dst
Definition: glext.h:6340
static U32 XXH_swap32(U32 x)
Definition: xxhash.c:231
#define XXH_PUBLIC_API
Definition: xxhash.h:108
FORCE_INLINE_TEMPLATE U32 XXH_readLE32(const void *ptr, XXH_endianess endian)
Definition: xxhash.c:277
XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t *restrict dstState, const XXH64_state_t *restrict srcState)
Definition: xxhash.c:338
UINT32 uint32_t
Definition: types.h:75
static const U64 PRIME64_5
Definition: xxhash.c:325
static const U32 PRIME32_4
Definition: xxhash.c:318
FORCE_INLINE_TEMPLATE U32 XXH_readLE32_align(const void *ptr, XXH_endianess endian, XXH_alignment align)
Definition: xxhash.c:269
XXH_PUBLIC_API unsigned XXH_versionNumber(void)
Definition: xxhash.c:327
static char * dest
Definition: rtl.c:135
XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t *statePtr, unsigned long long seed)
Definition: xxhash.c:592
#define XXH_FORCE_NATIVE_FORMAT
Definition: xxhash.c:77
struct XXH32_state_s XXH32_state_t
Definition: xxhash.h:182
FORCE_INLINE_TEMPLATE U64 XXH_readLE64(const void *ptr, XXH_endianess endian)
Definition: xxhash.c:295
GLfloat GLfloat p
Definition: glext.h:8902
Definition: _hash_fun.h:40
#define memset(x, y, z)
Definition: compat.h:39
XXH_endianess
Definition: xxhash.c:255
GLfloat GLfloat v1
Definition: glext.h:6062
unsigned int U32
Definition: xxhash.c:165
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
XXH_PUBLIC_API unsigned int XXH32_digest(const XXH32_state_t *state_in)
Definition: xxhash.c:711
XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t *dst, XXH64_hash_t hash)
Definition: xxhash.c:872
#define FORCE_INLINE_TEMPLATE
Definition: xxhash.c:142
FORCE_INLINE_TEMPLATE XXH_errorcode XXH32_update_endian(XXH32_state_t *state, const void *input, size_t len, XXH_endianess endian)
Definition: xxhash.c:605
XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t *src)
Definition: xxhash.c:879