zstd-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +11 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +29 -0
  9. data/README.md +63 -0
  10. data/Rakefile +22 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/ext/zstdruby/extconf.rb +23 -0
  14. data/ext/zstdruby/libzstd/.gitignore +2 -0
  15. data/ext/zstdruby/libzstd/Makefile +133 -0
  16. data/ext/zstdruby/libzstd/README.md +77 -0
  17. data/ext/zstdruby/libzstd/common/bitstream.h +414 -0
  18. data/ext/zstdruby/libzstd/common/entropy_common.c +227 -0
  19. data/ext/zstdruby/libzstd/common/error_private.c +43 -0
  20. data/ext/zstdruby/libzstd/common/error_private.h +76 -0
  21. data/ext/zstdruby/libzstd/common/fse.h +668 -0
  22. data/ext/zstdruby/libzstd/common/fse_decompress.c +329 -0
  23. data/ext/zstdruby/libzstd/common/huf.h +238 -0
  24. data/ext/zstdruby/libzstd/common/mem.h +372 -0
  25. data/ext/zstdruby/libzstd/common/xxhash.c +867 -0
  26. data/ext/zstdruby/libzstd/common/xxhash.h +309 -0
  27. data/ext/zstdruby/libzstd/common/zstd_common.c +77 -0
  28. data/ext/zstdruby/libzstd/common/zstd_errors.h +60 -0
  29. data/ext/zstdruby/libzstd/common/zstd_internal.h +270 -0
  30. data/ext/zstdruby/libzstd/compress/fse_compress.c +850 -0
  31. data/ext/zstdruby/libzstd/compress/huf_compress.c +609 -0
  32. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3291 -0
  33. data/ext/zstdruby/libzstd/compress/zstd_opt.h +919 -0
  34. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +885 -0
  35. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +2154 -0
  36. data/ext/zstdruby/libzstd/deprecated/zbuff.h +210 -0
  37. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +145 -0
  38. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +74 -0
  39. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1913 -0
  40. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.h +67 -0
  41. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +1012 -0
  42. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +111 -0
  43. data/ext/zstdruby/libzstd/dll/example/Makefile +47 -0
  44. data/ext/zstdruby/libzstd/dll/example/README.md +69 -0
  45. data/ext/zstdruby/libzstd/dll/example/build_package.bat +17 -0
  46. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +25 -0
  47. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +179 -0
  48. data/ext/zstdruby/libzstd/dll/libzstd.def +86 -0
  49. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +259 -0
  50. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +2095 -0
  51. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +80 -0
  52. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +3518 -0
  53. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +79 -0
  54. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +3159 -0
  55. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +79 -0
  56. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +3795 -0
  57. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +128 -0
  58. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +4056 -0
  59. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +149 -0
  60. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +4167 -0
  61. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +159 -0
  62. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +4540 -0
  63. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +173 -0
  64. data/ext/zstdruby/libzstd/libzstd.pc.in +14 -0
  65. data/ext/zstdruby/libzstd/zstd.h +673 -0
  66. data/ext/zstdruby/zstdruby.c +117 -0
  67. data/ext/zstdruby/zstdruby.h +6 -0
  68. data/lib/zstd-ruby.rb +6 -0
  69. data/lib/zstd-ruby/version.rb +3 -0
  70. data/zstd-ruby.gemspec +37 -0
  71. metadata +170 -0
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+
10
+ #ifndef MEM_H_MODULE
11
+ #define MEM_H_MODULE
12
+
13
+ #if defined (__cplusplus)
14
+ extern "C" {
15
+ #endif
16
+
17
+ /*-****************************************
18
+ * Dependencies
19
+ ******************************************/
20
+ #include <stddef.h> /* size_t, ptrdiff_t */
21
+ #include <string.h> /* memcpy */
22
+
23
+
24
+ /*-****************************************
25
+ * Compiler specifics
26
+ ******************************************/
27
+ #if defined(_MSC_VER) /* Visual Studio */
28
+ # include <stdlib.h> /* _byteswap_ulong */
29
+ # include <intrin.h> /* _byteswap_* */
30
+ #endif
31
+ #if defined(__GNUC__)
32
+ # define MEM_STATIC static __inline __attribute__((unused))
33
+ #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
34
+ # define MEM_STATIC static inline
35
+ #elif defined(_MSC_VER)
36
+ # define MEM_STATIC static __inline
37
+ #else
38
+ # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
39
+ #endif
40
+
41
+ /* code only tested on 32 and 64 bits systems */
42
+ #define MEM_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; }
43
+ MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
44
+
45
+
46
+ /*-**************************************************************
47
+ * Basic Types
48
+ *****************************************************************/
49
+ #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
50
+ # include <stdint.h>
51
+ typedef uint8_t BYTE;
52
+ typedef uint16_t U16;
53
+ typedef int16_t S16;
54
+ typedef uint32_t U32;
55
+ typedef int32_t S32;
56
+ typedef uint64_t U64;
57
+ typedef int64_t S64;
58
+ typedef intptr_t iPtrDiff;
59
+ #else
60
+ typedef unsigned char BYTE;
61
+ typedef unsigned short U16;
62
+ typedef signed short S16;
63
+ typedef unsigned int U32;
64
+ typedef signed int S32;
65
+ typedef unsigned long long U64;
66
+ typedef signed long long S64;
67
+ typedef ptrdiff_t iPtrDiff;
68
+ #endif
69
+
70
+
71
+ /*-**************************************************************
72
+ * Memory I/O
73
+ *****************************************************************/
74
+ /* MEM_FORCE_MEMORY_ACCESS :
75
+ * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
76
+ * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
77
+ * The below switch allow to select different access method for improved performance.
78
+ * Method 0 (default) : use `memcpy()`. Safe and portable.
79
+ * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
80
+ * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
81
+ * Method 2 : direct access. This method is portable but violate C standard.
82
+ * It can generate buggy code on targets depending on alignment.
83
+ * In some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
84
+ * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
85
+ * Prefer these methods in priority order (0 > 1 > 2)
86
+ */
87
+ #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
88
+ # 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__) )
89
+ # define MEM_FORCE_MEMORY_ACCESS 2
90
+ # elif defined(__INTEL_COMPILER) /*|| defined(_MSC_VER)*/ || \
91
+ (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
92
+ # define MEM_FORCE_MEMORY_ACCESS 1
93
+ # endif
94
+ #endif
95
+
96
+ MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }
97
+ MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
98
+
99
+ MEM_STATIC unsigned MEM_isLittleEndian(void)
100
+ {
101
+ const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
102
+ return one.c[0];
103
+ }
104
+
105
+ #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
106
+
107
+ /* violates C standard, by lying on structure alignment.
108
+ Only use if no other choice to achieve best performance on target platform */
109
+ MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
110
+ MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
111
+ MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
112
+ MEM_STATIC U64 MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }
113
+
114
+ MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
115
+ MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
116
+ MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
117
+
118
+ #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
119
+
120
+ /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
121
+ /* currently only defined for gcc and icc */
122
+ #if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
123
+ __pragma( pack(push, 1) )
124
+ typedef union { U16 u16; U32 u32; U64 u64; size_t st; } unalign;
125
+ __pragma( pack(pop) )
126
+ #else
127
+ typedef union { U16 u16; U32 u32; U64 u64; size_t st; } __attribute__((packed)) unalign;
128
+ #endif
129
+
130
+ MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
131
+ MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
132
+ MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
133
+ MEM_STATIC U64 MEM_readST(const void* ptr) { return ((const unalign*)ptr)->st; }
134
+
135
+ MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
136
+ MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; }
137
+ MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign*)memPtr)->u64 = value; }
138
+
139
+ #else
140
+
141
+ /* default method, safe and standard.
142
+ can sometimes prove slower */
143
+
144
+ MEM_STATIC U16 MEM_read16(const void* memPtr)
145
+ {
146
+ U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
147
+ }
148
+
149
+ MEM_STATIC U32 MEM_read32(const void* memPtr)
150
+ {
151
+ U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
152
+ }
153
+
154
+ MEM_STATIC U64 MEM_read64(const void* memPtr)
155
+ {
156
+ U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
157
+ }
158
+
159
+ MEM_STATIC size_t MEM_readST(const void* memPtr)
160
+ {
161
+ size_t val; memcpy(&val, memPtr, sizeof(val)); return val;
162
+ }
163
+
164
+ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
165
+ {
166
+ memcpy(memPtr, &value, sizeof(value));
167
+ }
168
+
169
+ MEM_STATIC void MEM_write32(void* memPtr, U32 value)
170
+ {
171
+ memcpy(memPtr, &value, sizeof(value));
172
+ }
173
+
174
+ MEM_STATIC void MEM_write64(void* memPtr, U64 value)
175
+ {
176
+ memcpy(memPtr, &value, sizeof(value));
177
+ }
178
+
179
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
180
+
181
+ MEM_STATIC U32 MEM_swap32(U32 in)
182
+ {
183
+ #if defined(_MSC_VER) /* Visual Studio */
184
+ return _byteswap_ulong(in);
185
+ #elif defined (__GNUC__)
186
+ return __builtin_bswap32(in);
187
+ #else
188
+ return ((in << 24) & 0xff000000 ) |
189
+ ((in << 8) & 0x00ff0000 ) |
190
+ ((in >> 8) & 0x0000ff00 ) |
191
+ ((in >> 24) & 0x000000ff );
192
+ #endif
193
+ }
194
+
195
+ MEM_STATIC U64 MEM_swap64(U64 in)
196
+ {
197
+ #if defined(_MSC_VER) /* Visual Studio */
198
+ return _byteswap_uint64(in);
199
+ #elif defined (__GNUC__)
200
+ return __builtin_bswap64(in);
201
+ #else
202
+ return ((in << 56) & 0xff00000000000000ULL) |
203
+ ((in << 40) & 0x00ff000000000000ULL) |
204
+ ((in << 24) & 0x0000ff0000000000ULL) |
205
+ ((in << 8) & 0x000000ff00000000ULL) |
206
+ ((in >> 8) & 0x00000000ff000000ULL) |
207
+ ((in >> 24) & 0x0000000000ff0000ULL) |
208
+ ((in >> 40) & 0x000000000000ff00ULL) |
209
+ ((in >> 56) & 0x00000000000000ffULL);
210
+ #endif
211
+ }
212
+
213
+ MEM_STATIC size_t MEM_swapST(size_t in)
214
+ {
215
+ if (MEM_32bits())
216
+ return (size_t)MEM_swap32((U32)in);
217
+ else
218
+ return (size_t)MEM_swap64((U64)in);
219
+ }
220
+
221
+ /*=== Little endian r/w ===*/
222
+
223
+ MEM_STATIC U16 MEM_readLE16(const void* memPtr)
224
+ {
225
+ if (MEM_isLittleEndian())
226
+ return MEM_read16(memPtr);
227
+ else {
228
+ const BYTE* p = (const BYTE*)memPtr;
229
+ return (U16)(p[0] + (p[1]<<8));
230
+ }
231
+ }
232
+
233
+ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
234
+ {
235
+ if (MEM_isLittleEndian()) {
236
+ MEM_write16(memPtr, val);
237
+ } else {
238
+ BYTE* p = (BYTE*)memPtr;
239
+ p[0] = (BYTE)val;
240
+ p[1] = (BYTE)(val>>8);
241
+ }
242
+ }
243
+
244
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
245
+ {
246
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
247
+ }
248
+
249
+ MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)
250
+ {
251
+ MEM_writeLE16(memPtr, (U16)val);
252
+ ((BYTE*)memPtr)[2] = (BYTE)(val>>16);
253
+ }
254
+
255
+ MEM_STATIC U32 MEM_readLE32(const void* memPtr)
256
+ {
257
+ if (MEM_isLittleEndian())
258
+ return MEM_read32(memPtr);
259
+ else
260
+ return MEM_swap32(MEM_read32(memPtr));
261
+ }
262
+
263
+ MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
264
+ {
265
+ if (MEM_isLittleEndian())
266
+ MEM_write32(memPtr, val32);
267
+ else
268
+ MEM_write32(memPtr, MEM_swap32(val32));
269
+ }
270
+
271
+ MEM_STATIC U64 MEM_readLE64(const void* memPtr)
272
+ {
273
+ if (MEM_isLittleEndian())
274
+ return MEM_read64(memPtr);
275
+ else
276
+ return MEM_swap64(MEM_read64(memPtr));
277
+ }
278
+
279
+ MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
280
+ {
281
+ if (MEM_isLittleEndian())
282
+ MEM_write64(memPtr, val64);
283
+ else
284
+ MEM_write64(memPtr, MEM_swap64(val64));
285
+ }
286
+
287
+ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
288
+ {
289
+ if (MEM_32bits())
290
+ return (size_t)MEM_readLE32(memPtr);
291
+ else
292
+ return (size_t)MEM_readLE64(memPtr);
293
+ }
294
+
295
+ MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
296
+ {
297
+ if (MEM_32bits())
298
+ MEM_writeLE32(memPtr, (U32)val);
299
+ else
300
+ MEM_writeLE64(memPtr, (U64)val);
301
+ }
302
+
303
+ /*=== Big endian r/w ===*/
304
+
305
+ MEM_STATIC U32 MEM_readBE32(const void* memPtr)
306
+ {
307
+ if (MEM_isLittleEndian())
308
+ return MEM_swap32(MEM_read32(memPtr));
309
+ else
310
+ return MEM_read32(memPtr);
311
+ }
312
+
313
+ MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32)
314
+ {
315
+ if (MEM_isLittleEndian())
316
+ MEM_write32(memPtr, MEM_swap32(val32));
317
+ else
318
+ MEM_write32(memPtr, val32);
319
+ }
320
+
321
+ MEM_STATIC U64 MEM_readBE64(const void* memPtr)
322
+ {
323
+ if (MEM_isLittleEndian())
324
+ return MEM_swap64(MEM_read64(memPtr));
325
+ else
326
+ return MEM_read64(memPtr);
327
+ }
328
+
329
+ MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64)
330
+ {
331
+ if (MEM_isLittleEndian())
332
+ MEM_write64(memPtr, MEM_swap64(val64));
333
+ else
334
+ MEM_write64(memPtr, val64);
335
+ }
336
+
337
+ MEM_STATIC size_t MEM_readBEST(const void* memPtr)
338
+ {
339
+ if (MEM_32bits())
340
+ return (size_t)MEM_readBE32(memPtr);
341
+ else
342
+ return (size_t)MEM_readBE64(memPtr);
343
+ }
344
+
345
+ MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)
346
+ {
347
+ if (MEM_32bits())
348
+ MEM_writeBE32(memPtr, (U32)val);
349
+ else
350
+ MEM_writeBE64(memPtr, (U64)val);
351
+ }
352
+
353
+
354
+ /* function safe only for comparisons */
355
+ MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length)
356
+ {
357
+ switch (length)
358
+ {
359
+ default :
360
+ case 4 : return MEM_read32(memPtr);
361
+ case 3 : if (MEM_isLittleEndian())
362
+ return MEM_read32(memPtr)<<8;
363
+ else
364
+ return MEM_read32(memPtr)>>8;
365
+ }
366
+ }
367
+
368
+ #if defined (__cplusplus)
369
+ }
370
+ #endif
371
+
372
+ #endif /* MEM_H_MODULE */
@@ -0,0 +1,867 @@
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
+ # 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-independant 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-independance 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 <stdlib.h>
101
+ static void* XXH_malloc(size_t s) { return malloc(s); }
102
+ static void XXH_free (void* p) { free(p); }
103
+ /* for memcpy() */
104
+ #include <string.h>
105
+ 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
+ # define FORCE_INLINE static __forceinline
117
+ #else
118
+ # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
119
+ # ifdef __GNUC__
120
+ # define FORCE_INLINE static inline __attribute__((always_inline))
121
+ # else
122
+ # define FORCE_INLINE static inline
123
+ # endif
124
+ # else
125
+ # define FORCE_INLINE static
126
+ # endif /* __STDC_VERSION__ */
127
+ #endif
128
+
129
+
130
+ /* *************************************
131
+ * Basic Types
132
+ ***************************************/
133
+ #ifndef MEM_MODULE
134
+ # define MEM_MODULE
135
+ # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
136
+ # include <stdint.h>
137
+ typedef uint8_t BYTE;
138
+ typedef uint16_t U16;
139
+ typedef uint32_t U32;
140
+ typedef int32_t S32;
141
+ typedef uint64_t U64;
142
+ # else
143
+ typedef unsigned char BYTE;
144
+ typedef unsigned short U16;
145
+ typedef unsigned int U32;
146
+ typedef signed int S32;
147
+ 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. */
148
+ # endif
149
+ #endif
150
+
151
+
152
+ #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
153
+
154
+ /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
155
+ static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; }
156
+ static U64 XXH_read64(const void* memPtr) { return *(const U64*) 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; U64 u64; } __attribute__((packed)) unalign;
163
+
164
+ static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
165
+ static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
166
+
167
+ #else
168
+
169
+ /* portable and safe solution. Generally efficient.
170
+ * see : http://stackoverflow.com/a/32095106/646947
171
+ */
172
+
173
+ static U32 XXH_read32(const void* memPtr)
174
+ {
175
+ U32 val;
176
+ memcpy(&val, memPtr, sizeof(val));
177
+ return val;
178
+ }
179
+
180
+ static U64 XXH_read64(const void* memPtr)
181
+ {
182
+ U64 val;
183
+ memcpy(&val, memPtr, sizeof(val));
184
+ return val;
185
+ }
186
+
187
+ #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
188
+
189
+
190
+ /* ****************************************
191
+ * Compiler-specific Functions and Macros
192
+ ******************************************/
193
+ #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
194
+
195
+ /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
196
+ #if defined(_MSC_VER)
197
+ # define XXH_rotl32(x,r) _rotl(x,r)
198
+ # define XXH_rotl64(x,r) _rotl64(x,r)
199
+ #else
200
+ # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
201
+ # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
202
+ #endif
203
+
204
+ #if defined(_MSC_VER) /* Visual Studio */
205
+ # define XXH_swap32 _byteswap_ulong
206
+ # define XXH_swap64 _byteswap_uint64
207
+ #elif GCC_VERSION >= 403
208
+ # define XXH_swap32 __builtin_bswap32
209
+ # define XXH_swap64 __builtin_bswap64
210
+ #else
211
+ static U32 XXH_swap32 (U32 x)
212
+ {
213
+ return ((x << 24) & 0xff000000 ) |
214
+ ((x << 8) & 0x00ff0000 ) |
215
+ ((x >> 8) & 0x0000ff00 ) |
216
+ ((x >> 24) & 0x000000ff );
217
+ }
218
+ static U64 XXH_swap64 (U64 x)
219
+ {
220
+ return ((x << 56) & 0xff00000000000000ULL) |
221
+ ((x << 40) & 0x00ff000000000000ULL) |
222
+ ((x << 24) & 0x0000ff0000000000ULL) |
223
+ ((x << 8) & 0x000000ff00000000ULL) |
224
+ ((x >> 8) & 0x00000000ff000000ULL) |
225
+ ((x >> 24) & 0x0000000000ff0000ULL) |
226
+ ((x >> 40) & 0x000000000000ff00ULL) |
227
+ ((x >> 56) & 0x00000000000000ffULL);
228
+ }
229
+ #endif
230
+
231
+
232
+ /* *************************************
233
+ * Architecture Macros
234
+ ***************************************/
235
+ typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
236
+
237
+ /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
238
+ #ifndef XXH_CPU_LITTLE_ENDIAN
239
+ static const int g_one = 1;
240
+ # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one))
241
+ #endif
242
+
243
+
244
+ /* ***************************
245
+ * Memory reads
246
+ *****************************/
247
+ typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
248
+
249
+ FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
250
+ {
251
+ if (align==XXH_unaligned)
252
+ return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
253
+ else
254
+ return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);
255
+ }
256
+
257
+ FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian)
258
+ {
259
+ return XXH_readLE32_align(ptr, endian, XXH_unaligned);
260
+ }
261
+
262
+ static U32 XXH_readBE32(const void* ptr)
263
+ {
264
+ return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
265
+ }
266
+
267
+ FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
268
+ {
269
+ if (align==XXH_unaligned)
270
+ return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
271
+ else
272
+ return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);
273
+ }
274
+
275
+ FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian)
276
+ {
277
+ return XXH_readLE64_align(ptr, endian, XXH_unaligned);
278
+ }
279
+
280
+ static U64 XXH_readBE64(const void* ptr)
281
+ {
282
+ return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
283
+ }
284
+
285
+
286
+ /* *************************************
287
+ * Macros
288
+ ***************************************/
289
+ #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
290
+
291
+
292
+ /* *************************************
293
+ * Constants
294
+ ***************************************/
295
+ static const U32 PRIME32_1 = 2654435761U;
296
+ static const U32 PRIME32_2 = 2246822519U;
297
+ static const U32 PRIME32_3 = 3266489917U;
298
+ static const U32 PRIME32_4 = 668265263U;
299
+ static const U32 PRIME32_5 = 374761393U;
300
+
301
+ static const U64 PRIME64_1 = 11400714785074694791ULL;
302
+ static const U64 PRIME64_2 = 14029467366897019727ULL;
303
+ static const U64 PRIME64_3 = 1609587929392839161ULL;
304
+ static const U64 PRIME64_4 = 9650029242287828579ULL;
305
+ static const U64 PRIME64_5 = 2870177450012600261ULL;
306
+
307
+ XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
308
+
309
+
310
+ /* **************************
311
+ * Utils
312
+ ****************************/
313
+ XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState)
314
+ {
315
+ memcpy(dstState, srcState, sizeof(*dstState));
316
+ }
317
+
318
+ XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState)
319
+ {
320
+ memcpy(dstState, srcState, sizeof(*dstState));
321
+ }
322
+
323
+
324
+ /* ***************************
325
+ * Simple Hash Functions
326
+ *****************************/
327
+
328
+ static U32 XXH32_round(U32 seed, U32 input)
329
+ {
330
+ seed += input * PRIME32_2;
331
+ seed = XXH_rotl32(seed, 13);
332
+ seed *= PRIME32_1;
333
+ return seed;
334
+ }
335
+
336
+ FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)
337
+ {
338
+ const BYTE* p = (const BYTE*)input;
339
+ const BYTE* bEnd = p + len;
340
+ U32 h32;
341
+ #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)
342
+
343
+ #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
344
+ if (p==NULL) {
345
+ len=0;
346
+ bEnd=p=(const BYTE*)(size_t)16;
347
+ }
348
+ #endif
349
+
350
+ if (len>=16) {
351
+ const BYTE* const limit = bEnd - 16;
352
+ U32 v1 = seed + PRIME32_1 + PRIME32_2;
353
+ U32 v2 = seed + PRIME32_2;
354
+ U32 v3 = seed + 0;
355
+ U32 v4 = seed - PRIME32_1;
356
+
357
+ do {
358
+ v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4;
359
+ v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4;
360
+ v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4;
361
+ v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4;
362
+ } while (p<=limit);
363
+
364
+ h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
365
+ } else {
366
+ h32 = seed + PRIME32_5;
367
+ }
368
+
369
+ h32 += (U32) len;
370
+
371
+ while (p+4<=bEnd) {
372
+ h32 += XXH_get32bits(p) * PRIME32_3;
373
+ h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
374
+ p+=4;
375
+ }
376
+
377
+ while (p<bEnd) {
378
+ h32 += (*p) * PRIME32_5;
379
+ h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
380
+ p++;
381
+ }
382
+
383
+ h32 ^= h32 >> 15;
384
+ h32 *= PRIME32_2;
385
+ h32 ^= h32 >> 13;
386
+ h32 *= PRIME32_3;
387
+ h32 ^= h32 >> 16;
388
+
389
+ return h32;
390
+ }
391
+
392
+
393
+ XXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed)
394
+ {
395
+ #if 0
396
+ /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
397
+ XXH32_CREATESTATE_STATIC(state);
398
+ XXH32_reset(state, seed);
399
+ XXH32_update(state, input, len);
400
+ return XXH32_digest(state);
401
+ #else
402
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
403
+
404
+ if (XXH_FORCE_ALIGN_CHECK) {
405
+ if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
406
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
407
+ return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
408
+ else
409
+ return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
410
+ } }
411
+
412
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
413
+ return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
414
+ else
415
+ return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
416
+ #endif
417
+ }
418
+
419
+
420
+ static U64 XXH64_round(U64 acc, U64 input)
421
+ {
422
+ acc += input * PRIME64_2;
423
+ acc = XXH_rotl64(acc, 31);
424
+ acc *= PRIME64_1;
425
+ return acc;
426
+ }
427
+
428
+ static U64 XXH64_mergeRound(U64 acc, U64 val)
429
+ {
430
+ val = XXH64_round(0, val);
431
+ acc ^= val;
432
+ acc = acc * PRIME64_1 + PRIME64_4;
433
+ return acc;
434
+ }
435
+
436
+ FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
437
+ {
438
+ const BYTE* p = (const BYTE*)input;
439
+ const BYTE* const bEnd = p + len;
440
+ U64 h64;
441
+ #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)
442
+
443
+ #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
444
+ if (p==NULL) {
445
+ len=0;
446
+ bEnd=p=(const BYTE*)(size_t)32;
447
+ }
448
+ #endif
449
+
450
+ if (len>=32) {
451
+ const BYTE* const limit = bEnd - 32;
452
+ U64 v1 = seed + PRIME64_1 + PRIME64_2;
453
+ U64 v2 = seed + PRIME64_2;
454
+ U64 v3 = seed + 0;
455
+ U64 v4 = seed - PRIME64_1;
456
+
457
+ do {
458
+ v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;
459
+ v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;
460
+ v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;
461
+ v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;
462
+ } while (p<=limit);
463
+
464
+ h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
465
+ h64 = XXH64_mergeRound(h64, v1);
466
+ h64 = XXH64_mergeRound(h64, v2);
467
+ h64 = XXH64_mergeRound(h64, v3);
468
+ h64 = XXH64_mergeRound(h64, v4);
469
+
470
+ } else {
471
+ h64 = seed + PRIME64_5;
472
+ }
473
+
474
+ h64 += (U64) len;
475
+
476
+ while (p+8<=bEnd) {
477
+ U64 const k1 = XXH64_round(0, XXH_get64bits(p));
478
+ h64 ^= k1;
479
+ h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
480
+ p+=8;
481
+ }
482
+
483
+ if (p+4<=bEnd) {
484
+ h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
485
+ h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
486
+ p+=4;
487
+ }
488
+
489
+ while (p<bEnd) {
490
+ h64 ^= (*p) * PRIME64_5;
491
+ h64 = XXH_rotl64(h64, 11) * PRIME64_1;
492
+ p++;
493
+ }
494
+
495
+ h64 ^= h64 >> 33;
496
+ h64 *= PRIME64_2;
497
+ h64 ^= h64 >> 29;
498
+ h64 *= PRIME64_3;
499
+ h64 ^= h64 >> 32;
500
+
501
+ return h64;
502
+ }
503
+
504
+
505
+ XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
506
+ {
507
+ #if 0
508
+ /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
509
+ XXH64_CREATESTATE_STATIC(state);
510
+ XXH64_reset(state, seed);
511
+ XXH64_update(state, input, len);
512
+ return XXH64_digest(state);
513
+ #else
514
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
515
+
516
+ if (XXH_FORCE_ALIGN_CHECK) {
517
+ if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
518
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
519
+ return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
520
+ else
521
+ return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
522
+ } }
523
+
524
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
525
+ return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
526
+ else
527
+ return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
528
+ #endif
529
+ }
530
+
531
+
532
+ /* **************************************************
533
+ * Advanced Hash Functions
534
+ ****************************************************/
535
+
536
+ XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
537
+ {
538
+ return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
539
+ }
540
+ XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
541
+ {
542
+ XXH_free(statePtr);
543
+ return XXH_OK;
544
+ }
545
+
546
+ XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
547
+ {
548
+ return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
549
+ }
550
+ XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
551
+ {
552
+ XXH_free(statePtr);
553
+ return XXH_OK;
554
+ }
555
+
556
+
557
+ /*** Hash feed ***/
558
+
559
+ XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed)
560
+ {
561
+ XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
562
+ memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */
563
+ state.v1 = seed + PRIME32_1 + PRIME32_2;
564
+ state.v2 = seed + PRIME32_2;
565
+ state.v3 = seed + 0;
566
+ state.v4 = seed - PRIME32_1;
567
+ memcpy(statePtr, &state, sizeof(state));
568
+ return XXH_OK;
569
+ }
570
+
571
+
572
+ XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
573
+ {
574
+ XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
575
+ memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
576
+ state.v1 = seed + PRIME64_1 + PRIME64_2;
577
+ state.v2 = seed + PRIME64_2;
578
+ state.v3 = seed + 0;
579
+ state.v4 = seed - PRIME64_1;
580
+ memcpy(statePtr, &state, sizeof(state));
581
+ return XXH_OK;
582
+ }
583
+
584
+
585
+ FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian)
586
+ {
587
+ const BYTE* p = (const BYTE*)input;
588
+ const BYTE* const bEnd = p + len;
589
+
590
+ #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
591
+ if (input==NULL) return XXH_ERROR;
592
+ #endif
593
+
594
+ state->total_len_32 += (unsigned)len;
595
+ state->large_len |= (len>=16) | (state->total_len_32>=16);
596
+
597
+ if (state->memsize + len < 16) { /* fill in tmp buffer */
598
+ XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len);
599
+ state->memsize += (unsigned)len;
600
+ return XXH_OK;
601
+ }
602
+
603
+ if (state->memsize) { /* some data left from previous update */
604
+ XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize);
605
+ { const U32* p32 = state->mem32;
606
+ state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++;
607
+ state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++;
608
+ state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++;
609
+ state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++;
610
+ }
611
+ p += 16-state->memsize;
612
+ state->memsize = 0;
613
+ }
614
+
615
+ if (p <= bEnd-16) {
616
+ const BYTE* const limit = bEnd - 16;
617
+ U32 v1 = state->v1;
618
+ U32 v2 = state->v2;
619
+ U32 v3 = state->v3;
620
+ U32 v4 = state->v4;
621
+
622
+ do {
623
+ v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4;
624
+ v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4;
625
+ v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4;
626
+ v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4;
627
+ } while (p<=limit);
628
+
629
+ state->v1 = v1;
630
+ state->v2 = v2;
631
+ state->v3 = v3;
632
+ state->v4 = v4;
633
+ }
634
+
635
+ if (p < bEnd) {
636
+ XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
637
+ state->memsize = (unsigned)(bEnd-p);
638
+ }
639
+
640
+ return XXH_OK;
641
+ }
642
+
643
+ XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)
644
+ {
645
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
646
+
647
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
648
+ return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
649
+ else
650
+ return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
651
+ }
652
+
653
+
654
+
655
+ FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian)
656
+ {
657
+ const BYTE * p = (const BYTE*)state->mem32;
658
+ const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize;
659
+ U32 h32;
660
+
661
+ if (state->large_len) {
662
+ h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
663
+ } else {
664
+ h32 = state->v3 /* == seed */ + PRIME32_5;
665
+ }
666
+
667
+ h32 += state->total_len_32;
668
+
669
+ while (p+4<=bEnd) {
670
+ h32 += XXH_readLE32(p, endian) * PRIME32_3;
671
+ h32 = XXH_rotl32(h32, 17) * PRIME32_4;
672
+ p+=4;
673
+ }
674
+
675
+ while (p<bEnd) {
676
+ h32 += (*p) * PRIME32_5;
677
+ h32 = XXH_rotl32(h32, 11) * PRIME32_1;
678
+ p++;
679
+ }
680
+
681
+ h32 ^= h32 >> 15;
682
+ h32 *= PRIME32_2;
683
+ h32 ^= h32 >> 13;
684
+ h32 *= PRIME32_3;
685
+ h32 ^= h32 >> 16;
686
+
687
+ return h32;
688
+ }
689
+
690
+
691
+ XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in)
692
+ {
693
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
694
+
695
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
696
+ return XXH32_digest_endian(state_in, XXH_littleEndian);
697
+ else
698
+ return XXH32_digest_endian(state_in, XXH_bigEndian);
699
+ }
700
+
701
+
702
+
703
+ /* **** XXH64 **** */
704
+
705
+ FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian)
706
+ {
707
+ const BYTE* p = (const BYTE*)input;
708
+ const BYTE* const bEnd = p + len;
709
+
710
+ #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
711
+ if (input==NULL) return XXH_ERROR;
712
+ #endif
713
+
714
+ state->total_len += len;
715
+
716
+ if (state->memsize + len < 32) { /* fill in tmp buffer */
717
+ XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);
718
+ state->memsize += (U32)len;
719
+ return XXH_OK;
720
+ }
721
+
722
+ if (state->memsize) { /* tmp buffer is full */
723
+ XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);
724
+ state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));
725
+ state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));
726
+ state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));
727
+ state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));
728
+ p += 32-state->memsize;
729
+ state->memsize = 0;
730
+ }
731
+
732
+ if (p+32 <= bEnd) {
733
+ const BYTE* const limit = bEnd - 32;
734
+ U64 v1 = state->v1;
735
+ U64 v2 = state->v2;
736
+ U64 v3 = state->v3;
737
+ U64 v4 = state->v4;
738
+
739
+ do {
740
+ v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;
741
+ v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;
742
+ v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;
743
+ v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;
744
+ } while (p<=limit);
745
+
746
+ state->v1 = v1;
747
+ state->v2 = v2;
748
+ state->v3 = v3;
749
+ state->v4 = v4;
750
+ }
751
+
752
+ if (p < bEnd) {
753
+ XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
754
+ state->memsize = (unsigned)(bEnd-p);
755
+ }
756
+
757
+ return XXH_OK;
758
+ }
759
+
760
+ XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
761
+ {
762
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
763
+
764
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
765
+ return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
766
+ else
767
+ return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
768
+ }
769
+
770
+
771
+
772
+ FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian)
773
+ {
774
+ const BYTE * p = (const BYTE*)state->mem64;
775
+ const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize;
776
+ U64 h64;
777
+
778
+ if (state->total_len >= 32) {
779
+ U64 const v1 = state->v1;
780
+ U64 const v2 = state->v2;
781
+ U64 const v3 = state->v3;
782
+ U64 const v4 = state->v4;
783
+
784
+ h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
785
+ h64 = XXH64_mergeRound(h64, v1);
786
+ h64 = XXH64_mergeRound(h64, v2);
787
+ h64 = XXH64_mergeRound(h64, v3);
788
+ h64 = XXH64_mergeRound(h64, v4);
789
+ } else {
790
+ h64 = state->v3 + PRIME64_5;
791
+ }
792
+
793
+ h64 += (U64) state->total_len;
794
+
795
+ while (p+8<=bEnd) {
796
+ U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian));
797
+ h64 ^= k1;
798
+ h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
799
+ p+=8;
800
+ }
801
+
802
+ if (p+4<=bEnd) {
803
+ h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1;
804
+ h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
805
+ p+=4;
806
+ }
807
+
808
+ while (p<bEnd) {
809
+ h64 ^= (*p) * PRIME64_5;
810
+ h64 = XXH_rotl64(h64, 11) * PRIME64_1;
811
+ p++;
812
+ }
813
+
814
+ h64 ^= h64 >> 33;
815
+ h64 *= PRIME64_2;
816
+ h64 ^= h64 >> 29;
817
+ h64 *= PRIME64_3;
818
+ h64 ^= h64 >> 32;
819
+
820
+ return h64;
821
+ }
822
+
823
+
824
+ XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)
825
+ {
826
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
827
+
828
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
829
+ return XXH64_digest_endian(state_in, XXH_littleEndian);
830
+ else
831
+ return XXH64_digest_endian(state_in, XXH_bigEndian);
832
+ }
833
+
834
+
835
+ /* **************************
836
+ * Canonical representation
837
+ ****************************/
838
+
839
+ /*! Default XXH result types are basic unsigned 32 and 64 bits.
840
+ * The canonical representation follows human-readable write convention, aka big-endian (large digits first).
841
+ * These functions allow transformation of hash result into and from its canonical format.
842
+ * This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs.
843
+ */
844
+
845
+ XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
846
+ {
847
+ XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
848
+ if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
849
+ memcpy(dst, &hash, sizeof(*dst));
850
+ }
851
+
852
+ XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
853
+ {
854
+ XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
855
+ if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
856
+ memcpy(dst, &hash, sizeof(*dst));
857
+ }
858
+
859
+ XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
860
+ {
861
+ return XXH_readBE32(src);
862
+ }
863
+
864
+ XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
865
+ {
866
+ return XXH_readBE64(src);
867
+ }