File Coverage

xxhash.c
Criterion Covered Total %
statement 92 303 30.3
branch 22 368 5.9
condition n/a
subroutine n/a
pod n/a
total 114 671 16.9


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