extzstd 0.3.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -14
  3. data/contrib/zstd/CHANGELOG +301 -56
  4. data/contrib/zstd/CONTRIBUTING.md +169 -72
  5. data/contrib/zstd/LICENSE +4 -4
  6. data/contrib/zstd/Makefile +116 -87
  7. data/contrib/zstd/Package.swift +36 -0
  8. data/contrib/zstd/README.md +62 -32
  9. data/contrib/zstd/TESTING.md +2 -3
  10. data/contrib/zstd/appveyor.yml +52 -136
  11. data/contrib/zstd/lib/BUCK +5 -7
  12. data/contrib/zstd/lib/Makefile +225 -222
  13. data/contrib/zstd/lib/README.md +51 -6
  14. data/contrib/zstd/lib/common/allocations.h +55 -0
  15. data/contrib/zstd/lib/common/bits.h +200 -0
  16. data/contrib/zstd/lib/common/bitstream.h +45 -62
  17. data/contrib/zstd/lib/common/compiler.h +205 -22
  18. data/contrib/zstd/lib/common/cpu.h +1 -3
  19. data/contrib/zstd/lib/common/debug.c +1 -1
  20. data/contrib/zstd/lib/common/debug.h +12 -19
  21. data/contrib/zstd/lib/common/entropy_common.c +172 -48
  22. data/contrib/zstd/lib/common/error_private.c +10 -2
  23. data/contrib/zstd/lib/common/error_private.h +82 -3
  24. data/contrib/zstd/lib/common/fse.h +37 -86
  25. data/contrib/zstd/lib/common/fse_decompress.c +117 -92
  26. data/contrib/zstd/lib/common/huf.h +99 -166
  27. data/contrib/zstd/lib/common/mem.h +124 -142
  28. data/contrib/zstd/lib/common/pool.c +54 -27
  29. data/contrib/zstd/lib/common/pool.h +10 -4
  30. data/contrib/zstd/lib/common/portability_macros.h +156 -0
  31. data/contrib/zstd/lib/common/threading.c +74 -19
  32. data/contrib/zstd/lib/common/threading.h +5 -10
  33. data/contrib/zstd/lib/common/xxhash.c +7 -847
  34. data/contrib/zstd/lib/common/xxhash.h +5568 -167
  35. data/contrib/zstd/lib/common/zstd_common.c +2 -37
  36. data/contrib/zstd/lib/common/zstd_deps.h +111 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +132 -187
  38. data/contrib/zstd/lib/common/zstd_trace.h +163 -0
  39. data/contrib/zstd/lib/compress/clevels.h +134 -0
  40. data/contrib/zstd/lib/compress/fse_compress.c +83 -157
  41. data/contrib/zstd/lib/compress/hist.c +27 -29
  42. data/contrib/zstd/lib/compress/hist.h +2 -2
  43. data/contrib/zstd/lib/compress/huf_compress.c +916 -279
  44. data/contrib/zstd/lib/compress/zstd_compress.c +3773 -1019
  45. data/contrib/zstd/lib/compress/zstd_compress_internal.h +610 -203
  46. data/contrib/zstd/lib/compress/zstd_compress_literals.c +119 -42
  47. data/contrib/zstd/lib/compress/zstd_compress_literals.h +16 -6
  48. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +42 -19
  49. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +1 -1
  50. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +49 -317
  51. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +1 -1
  52. data/contrib/zstd/lib/compress/zstd_cwksp.h +320 -103
  53. data/contrib/zstd/lib/compress/zstd_double_fast.c +388 -151
  54. data/contrib/zstd/lib/compress/zstd_double_fast.h +3 -2
  55. data/contrib/zstd/lib/compress/zstd_fast.c +729 -265
  56. data/contrib/zstd/lib/compress/zstd_fast.h +3 -2
  57. data/contrib/zstd/lib/compress/zstd_lazy.c +1270 -251
  58. data/contrib/zstd/lib/compress/zstd_lazy.h +61 -1
  59. data/contrib/zstd/lib/compress/zstd_ldm.c +324 -219
  60. data/contrib/zstd/lib/compress/zstd_ldm.h +9 -2
  61. data/contrib/zstd/lib/compress/zstd_ldm_geartab.h +106 -0
  62. data/contrib/zstd/lib/compress/zstd_opt.c +481 -209
  63. data/contrib/zstd/lib/compress/zstd_opt.h +1 -1
  64. data/contrib/zstd/lib/compress/zstdmt_compress.c +181 -457
  65. data/contrib/zstd/lib/compress/zstdmt_compress.h +34 -113
  66. data/contrib/zstd/lib/decompress/huf_decompress.c +1199 -565
  67. data/contrib/zstd/lib/decompress/huf_decompress_amd64.S +576 -0
  68. data/contrib/zstd/lib/decompress/zstd_ddict.c +12 -12
  69. data/contrib/zstd/lib/decompress/zstd_ddict.h +2 -2
  70. data/contrib/zstd/lib/decompress/zstd_decompress.c +627 -157
  71. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1086 -326
  72. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +19 -5
  73. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +62 -13
  74. data/contrib/zstd/lib/deprecated/zbuff.h +1 -1
  75. data/contrib/zstd/lib/deprecated/zbuff_common.c +1 -1
  76. data/contrib/zstd/lib/deprecated/zbuff_compress.c +24 -4
  77. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +3 -1
  78. data/contrib/zstd/lib/dictBuilder/cover.c +73 -52
  79. data/contrib/zstd/lib/dictBuilder/cover.h +7 -6
  80. data/contrib/zstd/lib/dictBuilder/divsufsort.c +1 -1
  81. data/contrib/zstd/lib/dictBuilder/fastcover.c +44 -35
  82. data/contrib/zstd/lib/dictBuilder/zdict.c +103 -111
  83. data/contrib/zstd/lib/legacy/zstd_legacy.h +8 -1
  84. data/contrib/zstd/lib/legacy/zstd_v01.c +21 -54
  85. data/contrib/zstd/lib/legacy/zstd_v01.h +1 -1
  86. data/contrib/zstd/lib/legacy/zstd_v02.c +29 -70
  87. data/contrib/zstd/lib/legacy/zstd_v02.h +1 -1
  88. data/contrib/zstd/lib/legacy/zstd_v03.c +30 -73
  89. data/contrib/zstd/lib/legacy/zstd_v03.h +1 -1
  90. data/contrib/zstd/lib/legacy/zstd_v04.c +29 -71
  91. data/contrib/zstd/lib/legacy/zstd_v04.h +1 -1
  92. data/contrib/zstd/lib/legacy/zstd_v05.c +40 -86
  93. data/contrib/zstd/lib/legacy/zstd_v05.h +1 -1
  94. data/contrib/zstd/lib/legacy/zstd_v06.c +47 -88
  95. data/contrib/zstd/lib/legacy/zstd_v06.h +1 -1
  96. data/contrib/zstd/lib/legacy/zstd_v07.c +40 -83
  97. data/contrib/zstd/lib/legacy/zstd_v07.h +1 -1
  98. data/contrib/zstd/lib/libzstd.mk +214 -0
  99. data/contrib/zstd/lib/libzstd.pc.in +7 -6
  100. data/contrib/zstd/lib/module.modulemap +35 -0
  101. data/contrib/zstd/lib/{dictBuilder/zdict.h → zdict.h} +203 -34
  102. data/contrib/zstd/lib/zstd.h +1217 -287
  103. data/contrib/zstd/lib/{common/zstd_errors.h → zstd_errors.h} +28 -8
  104. data/ext/extconf.rb +7 -6
  105. data/ext/extzstd.c +19 -10
  106. data/ext/extzstd.h +6 -0
  107. data/ext/libzstd_conf.h +0 -1
  108. data/ext/zstd_decompress_asm.S +1 -0
  109. data/gemstub.rb +3 -21
  110. data/lib/extzstd/version.rb +6 -1
  111. data/lib/extzstd.rb +0 -2
  112. data/test/test_basic.rb +0 -5
  113. metadata +18 -6
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -13,8 +13,7 @@
13
13
  /*-*************************************
14
14
  * Dependencies
15
15
  ***************************************/
16
- #include <stdlib.h> /* malloc, calloc, free */
17
- #include <string.h> /* memset */
16
+ #define ZSTD_DEPS_NEED_MALLOC
18
17
  #include "error_private.h"
19
18
  #include "zstd_internal.h"
20
19
 
@@ -47,37 +46,3 @@ ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
47
46
  /*! ZSTD_getErrorString() :
48
47
  * provides error code string from enum */
49
48
  const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
50
-
51
-
52
-
53
- /*=**************************************************************
54
- * Custom allocator
55
- ****************************************************************/
56
- void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
57
- {
58
- if (customMem.customAlloc)
59
- return customMem.customAlloc(customMem.opaque, size);
60
- return malloc(size);
61
- }
62
-
63
- void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
64
- {
65
- if (customMem.customAlloc) {
66
- /* calloc implemented as malloc+memset;
67
- * not as efficient as calloc, but next best guess for custom malloc */
68
- void* const ptr = customMem.customAlloc(customMem.opaque, size);
69
- memset(ptr, 0, size);
70
- return ptr;
71
- }
72
- return calloc(1, size);
73
- }
74
-
75
- void ZSTD_free(void* ptr, ZSTD_customMem customMem)
76
- {
77
- if (ptr!=NULL) {
78
- if (customMem.customFree)
79
- customMem.customFree(customMem.opaque, ptr);
80
- else
81
- free(ptr);
82
- }
83
- }
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
11
+ /* This file provides common libc dependencies that zstd requires.
12
+ * The purpose is to allow replacing this file with a custom implementation
13
+ * to compile zstd without libc support.
14
+ */
15
+
16
+ /* Need:
17
+ * NULL
18
+ * INT_MAX
19
+ * UINT_MAX
20
+ * ZSTD_memcpy()
21
+ * ZSTD_memset()
22
+ * ZSTD_memmove()
23
+ */
24
+ #ifndef ZSTD_DEPS_COMMON
25
+ #define ZSTD_DEPS_COMMON
26
+
27
+ #include <limits.h>
28
+ #include <stddef.h>
29
+ #include <string.h>
30
+
31
+ #if defined(__GNUC__) && __GNUC__ >= 4
32
+ # define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l))
33
+ # define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l))
34
+ # define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l))
35
+ #else
36
+ # define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l))
37
+ # define ZSTD_memmove(d,s,l) memmove((d),(s),(l))
38
+ # define ZSTD_memset(p,v,l) memset((p),(v),(l))
39
+ #endif
40
+
41
+ #endif /* ZSTD_DEPS_COMMON */
42
+
43
+ /* Need:
44
+ * ZSTD_malloc()
45
+ * ZSTD_free()
46
+ * ZSTD_calloc()
47
+ */
48
+ #ifdef ZSTD_DEPS_NEED_MALLOC
49
+ #ifndef ZSTD_DEPS_MALLOC
50
+ #define ZSTD_DEPS_MALLOC
51
+
52
+ #include <stdlib.h>
53
+
54
+ #define ZSTD_malloc(s) malloc(s)
55
+ #define ZSTD_calloc(n,s) calloc((n), (s))
56
+ #define ZSTD_free(p) free((p))
57
+
58
+ #endif /* ZSTD_DEPS_MALLOC */
59
+ #endif /* ZSTD_DEPS_NEED_MALLOC */
60
+
61
+ /*
62
+ * Provides 64-bit math support.
63
+ * Need:
64
+ * U64 ZSTD_div64(U64 dividend, U32 divisor)
65
+ */
66
+ #ifdef ZSTD_DEPS_NEED_MATH64
67
+ #ifndef ZSTD_DEPS_MATH64
68
+ #define ZSTD_DEPS_MATH64
69
+
70
+ #define ZSTD_div64(dividend, divisor) ((dividend) / (divisor))
71
+
72
+ #endif /* ZSTD_DEPS_MATH64 */
73
+ #endif /* ZSTD_DEPS_NEED_MATH64 */
74
+
75
+ /* Need:
76
+ * assert()
77
+ */
78
+ #ifdef ZSTD_DEPS_NEED_ASSERT
79
+ #ifndef ZSTD_DEPS_ASSERT
80
+ #define ZSTD_DEPS_ASSERT
81
+
82
+ #include <assert.h>
83
+
84
+ #endif /* ZSTD_DEPS_ASSERT */
85
+ #endif /* ZSTD_DEPS_NEED_ASSERT */
86
+
87
+ /* Need:
88
+ * ZSTD_DEBUG_PRINT()
89
+ */
90
+ #ifdef ZSTD_DEPS_NEED_IO
91
+ #ifndef ZSTD_DEPS_IO
92
+ #define ZSTD_DEPS_IO
93
+
94
+ #include <stdio.h>
95
+ #define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
96
+
97
+ #endif /* ZSTD_DEPS_IO */
98
+ #endif /* ZSTD_DEPS_NEED_IO */
99
+
100
+ /* Only requested when <stdint.h> is known to be present.
101
+ * Need:
102
+ * intptr_t
103
+ */
104
+ #ifdef ZSTD_DEPS_NEED_STDINT
105
+ #ifndef ZSTD_DEPS_STDINT
106
+ #define ZSTD_DEPS_STDINT
107
+
108
+ #include <stdint.h>
109
+
110
+ #endif /* ZSTD_DEPS_STDINT */
111
+ #endif /* ZSTD_DEPS_NEED_STDINT */
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -19,10 +19,8 @@
19
19
  /*-*************************************
20
20
  * Dependencies
21
21
  ***************************************/
22
- #ifdef __aarch64__
23
- #include <arm_neon.h>
24
- #endif
25
22
  #include "compiler.h"
23
+ #include "cpu.h"
26
24
  #include "mem.h"
27
25
  #include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
28
26
  #include "error_private.h"
@@ -30,12 +28,16 @@
30
28
  #include "../zstd.h"
31
29
  #define FSE_STATIC_LINKING_ONLY
32
30
  #include "fse.h"
33
- #define HUF_STATIC_LINKING_ONLY
34
31
  #include "huf.h"
35
32
  #ifndef XXH_STATIC_LINKING_ONLY
36
33
  # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
37
34
  #endif
38
35
  #include "xxhash.h" /* XXH_reset, update, digest */
36
+ #ifndef ZSTD_NO_TRACE
37
+ # include "zstd_trace.h"
38
+ #else
39
+ # define ZSTD_TRACE 0
40
+ #endif
39
41
 
40
42
  #if defined (__cplusplus)
41
43
  extern "C" {
@@ -55,81 +57,7 @@ extern "C" {
55
57
  #undef MAX
56
58
  #define MIN(a,b) ((a)<(b) ? (a) : (b))
57
59
  #define MAX(a,b) ((a)>(b) ? (a) : (b))
58
-
59
- /**
60
- * Ignore: this is an internal helper.
61
- *
62
- * This is a helper function to help force C99-correctness during compilation.
63
- * Under strict compilation modes, variadic macro arguments can't be empty.
64
- * However, variadic function arguments can be. Using a function therefore lets
65
- * us statically check that at least one (string) argument was passed,
66
- * independent of the compilation flags.
67
- */
68
- static INLINE_KEYWORD UNUSED_ATTR
69
- void _force_has_format_string(const char *format, ...) {
70
- (void)format;
71
- }
72
-
73
- /**
74
- * Ignore: this is an internal helper.
75
- *
76
- * We want to force this function invocation to be syntactically correct, but
77
- * we don't want to force runtime evaluation of its arguments.
78
- */
79
- #define _FORCE_HAS_FORMAT_STRING(...) \
80
- if (0) { \
81
- _force_has_format_string(__VA_ARGS__); \
82
- }
83
-
84
- /**
85
- * Return the specified error if the condition evaluates to true.
86
- *
87
- * In debug modes, prints additional information.
88
- * In order to do that (particularly, printing the conditional that failed),
89
- * this can't just wrap RETURN_ERROR().
90
- */
91
- #define RETURN_ERROR_IF(cond, err, ...) \
92
- if (cond) { \
93
- RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
94
- __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \
95
- _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
96
- RAWLOG(3, ": " __VA_ARGS__); \
97
- RAWLOG(3, "\n"); \
98
- return ERROR(err); \
99
- }
100
-
101
- /**
102
- * Unconditionally return the specified error.
103
- *
104
- * In debug modes, prints additional information.
105
- */
106
- #define RETURN_ERROR(err, ...) \
107
- do { \
108
- RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
109
- __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \
110
- _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
111
- RAWLOG(3, ": " __VA_ARGS__); \
112
- RAWLOG(3, "\n"); \
113
- return ERROR(err); \
114
- } while(0);
115
-
116
- /**
117
- * If the provided expression evaluates to an error code, returns that error code.
118
- *
119
- * In debug modes, prints additional information.
120
- */
121
- #define FORWARD_IF_ERROR(err, ...) \
122
- do { \
123
- size_t const err_code = (err); \
124
- if (ERR_isError(err_code)) { \
125
- RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
126
- __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \
127
- _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
128
- RAWLOG(3, ": " __VA_ARGS__); \
129
- RAWLOG(3, "\n"); \
130
- return err_code; \
131
- } \
132
- } while(0);
60
+ #define BOUNDED(min,val,max) (MAX(min,MIN(val,max)))
133
61
 
134
62
 
135
63
  /*-*************************************
@@ -138,8 +66,7 @@ void _force_has_format_string(const char *format, ...) {
138
66
  #define ZSTD_OPT_NUM (1<<12)
139
67
 
140
68
  #define ZSTD_REP_NUM 3 /* number of repcodes */
141
- #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
142
- static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
69
+ static UNUSED_ATTR const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
143
70
 
144
71
  #define KB *(1 <<10)
145
72
  #define MB *(1 <<20)
@@ -153,21 +80,21 @@ static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
153
80
  #define BIT0 1
154
81
 
155
82
  #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
156
- static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
157
- static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
83
+ static UNUSED_ATTR const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
84
+ static UNUSED_ATTR const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
158
85
 
159
86
  #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
160
87
 
161
88
  #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
162
- static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
89
+ static UNUSED_ATTR const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
163
90
  typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
164
91
 
165
92
  #define ZSTD_FRAMECHECKSUMSIZE 4
166
93
 
167
94
  #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
168
- #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
95
+ #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */) /* for a non-null block */
96
+ #define MIN_LITERALS_FOR_4_STREAMS 6
169
97
 
170
- #define HufLog 12
171
98
  typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
172
99
 
173
100
  #define LONGNBSEQ 0x7F00
@@ -175,6 +102,7 @@ typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingTy
175
102
  #define MINMATCH 3
176
103
 
177
104
  #define Litbits 8
105
+ #define LitHufLog 11
178
106
  #define MaxLit ((1<<Litbits) - 1)
179
107
  #define MaxML 52
180
108
  #define MaxLL 35
@@ -185,62 +113,89 @@ typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingTy
185
113
  #define LLFSELog 9
186
114
  #define OffFSELog 8
187
115
  #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
188
-
189
- static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
190
- 0, 0, 0, 0, 0, 0, 0, 0,
191
- 1, 1, 1, 1, 2, 2, 3, 3,
192
- 4, 6, 7, 8, 9,10,11,12,
193
- 13,14,15,16 };
194
- static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
195
- 2, 2, 2, 2, 2, 1, 1, 1,
196
- 2, 2, 2, 2, 2, 2, 2, 2,
197
- 2, 3, 2, 1, 1, 1, 1, 1,
198
- -1,-1,-1,-1 };
116
+ #define MaxMLBits 16
117
+ #define MaxLLBits 16
118
+
119
+ #define ZSTD_MAX_HUF_HEADER_SIZE 128 /* header + <= 127 byte tree description */
120
+ /* Each table cannot take more than #symbols * FSELog bits */
121
+ #define ZSTD_MAX_FSE_HEADERS_SIZE (((MaxML + 1) * MLFSELog + (MaxLL + 1) * LLFSELog + (MaxOff + 1) * OffFSELog + 7) / 8)
122
+
123
+ static UNUSED_ATTR const U8 LL_bits[MaxLL+1] = {
124
+ 0, 0, 0, 0, 0, 0, 0, 0,
125
+ 0, 0, 0, 0, 0, 0, 0, 0,
126
+ 1, 1, 1, 1, 2, 2, 3, 3,
127
+ 4, 6, 7, 8, 9,10,11,12,
128
+ 13,14,15,16
129
+ };
130
+ static UNUSED_ATTR const S16 LL_defaultNorm[MaxLL+1] = {
131
+ 4, 3, 2, 2, 2, 2, 2, 2,
132
+ 2, 2, 2, 2, 2, 1, 1, 1,
133
+ 2, 2, 2, 2, 2, 2, 2, 2,
134
+ 2, 3, 2, 1, 1, 1, 1, 1,
135
+ -1,-1,-1,-1
136
+ };
199
137
  #define LL_DEFAULTNORMLOG 6 /* for static allocation */
200
- static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
201
-
202
- static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
203
- 0, 0, 0, 0, 0, 0, 0, 0,
204
- 0, 0, 0, 0, 0, 0, 0, 0,
205
- 0, 0, 0, 0, 0, 0, 0, 0,
206
- 1, 1, 1, 1, 2, 2, 3, 3,
207
- 4, 4, 5, 7, 8, 9,10,11,
208
- 12,13,14,15,16 };
209
- static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
210
- 2, 1, 1, 1, 1, 1, 1, 1,
211
- 1, 1, 1, 1, 1, 1, 1, 1,
212
- 1, 1, 1, 1, 1, 1, 1, 1,
213
- 1, 1, 1, 1, 1, 1, 1, 1,
214
- 1, 1, 1, 1, 1, 1,-1,-1,
215
- -1,-1,-1,-1,-1 };
138
+ static UNUSED_ATTR const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
139
+
140
+ static UNUSED_ATTR const U8 ML_bits[MaxML+1] = {
141
+ 0, 0, 0, 0, 0, 0, 0, 0,
142
+ 0, 0, 0, 0, 0, 0, 0, 0,
143
+ 0, 0, 0, 0, 0, 0, 0, 0,
144
+ 0, 0, 0, 0, 0, 0, 0, 0,
145
+ 1, 1, 1, 1, 2, 2, 3, 3,
146
+ 4, 4, 5, 7, 8, 9,10,11,
147
+ 12,13,14,15,16
148
+ };
149
+ static UNUSED_ATTR const S16 ML_defaultNorm[MaxML+1] = {
150
+ 1, 4, 3, 2, 2, 2, 2, 2,
151
+ 2, 1, 1, 1, 1, 1, 1, 1,
152
+ 1, 1, 1, 1, 1, 1, 1, 1,
153
+ 1, 1, 1, 1, 1, 1, 1, 1,
154
+ 1, 1, 1, 1, 1, 1, 1, 1,
155
+ 1, 1, 1, 1, 1, 1,-1,-1,
156
+ -1,-1,-1,-1,-1
157
+ };
216
158
  #define ML_DEFAULTNORMLOG 6 /* for static allocation */
217
- static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
218
-
219
- static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
220
- 2, 1, 1, 1, 1, 1, 1, 1,
221
- 1, 1, 1, 1, 1, 1, 1, 1,
222
- -1,-1,-1,-1,-1 };
159
+ static UNUSED_ATTR const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
160
+
161
+ static UNUSED_ATTR const S16 OF_defaultNorm[DefaultMaxOff+1] = {
162
+ 1, 1, 1, 1, 1, 1, 2, 2,
163
+ 2, 1, 1, 1, 1, 1, 1, 1,
164
+ 1, 1, 1, 1, 1, 1, 1, 1,
165
+ -1,-1,-1,-1,-1
166
+ };
223
167
  #define OF_DEFAULTNORMLOG 5 /* for static allocation */
224
- static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
168
+ static UNUSED_ATTR const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
225
169
 
226
170
 
227
171
  /*-*******************************************
228
172
  * Shared functions to include for inlining
229
173
  *********************************************/
230
174
  static void ZSTD_copy8(void* dst, const void* src) {
231
- #ifdef __aarch64__
175
+ #if defined(ZSTD_ARCH_ARM_NEON)
232
176
  vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));
233
177
  #else
234
- memcpy(dst, src, 8);
178
+ ZSTD_memcpy(dst, src, 8);
235
179
  #endif
236
180
  }
237
-
238
181
  #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
182
+
183
+ /* Need to use memmove here since the literal buffer can now be located within
184
+ the dst buffer. In circumstances where the op "catches up" to where the
185
+ literal buffer is, there can be partial overlaps in this call on the final
186
+ copy if the literal is being shifted by less than 16 bytes. */
239
187
  static void ZSTD_copy16(void* dst, const void* src) {
240
- #ifdef __aarch64__
188
+ #if defined(ZSTD_ARCH_ARM_NEON)
241
189
  vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));
190
+ #elif defined(ZSTD_ARCH_X86_SSE2)
191
+ _mm_storeu_si128((__m128i*)dst, _mm_loadu_si128((const __m128i*)src));
192
+ #elif defined(__clang__)
193
+ ZSTD_memmove(dst, src, 16);
242
194
  #else
243
- memcpy(dst, src, 16);
195
+ /* ZSTD_memmove is not inlined properly by gcc */
196
+ BYTE copy16_buf[16];
197
+ ZSTD_memcpy(copy16_buf, src, 16);
198
+ ZSTD_memcpy(dst, copy16_buf, 16);
244
199
  #endif
245
200
  }
246
201
  #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }
@@ -255,13 +210,13 @@ typedef enum {
255
210
  } ZSTD_overlap_e;
256
211
 
257
212
  /*! ZSTD_wildcopy() :
258
- * Custom version of memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
213
+ * Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
259
214
  * @param ovtype controls the overlap detection
260
215
  * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
261
216
  * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
262
217
  * The src buffer must be before the dst buffer.
263
218
  */
264
- MEM_STATIC FORCE_INLINE_ATTR
219
+ MEM_STATIC FORCE_INLINE_ATTR
265
220
  void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
266
221
  {
267
222
  ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
@@ -269,8 +224,6 @@ void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e
269
224
  BYTE* op = (BYTE*)dst;
270
225
  BYTE* const oend = op + length;
271
226
 
272
- assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN));
273
-
274
227
  if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
275
228
  /* Handle short offset copies. */
276
229
  do {
@@ -284,20 +237,15 @@ void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e
284
237
  * one COPY16() in the first call. Then, do two calls per loop since
285
238
  * at that point it is more likely to have a high trip count.
286
239
  */
287
- #ifndef __aarch64__
288
- do {
289
- COPY16(op, ip);
290
- }
291
- while (op < oend);
292
- #else
293
- COPY16(op, ip);
294
- if (op >= oend) return;
240
+ ZSTD_copy16(op, ip);
241
+ if (16 >= length) return;
242
+ op += 16;
243
+ ip += 16;
295
244
  do {
296
245
  COPY16(op, ip);
297
246
  COPY16(op, ip);
298
247
  }
299
248
  while (op < oend);
300
- #endif
301
249
  }
302
250
  }
303
251
 
@@ -305,7 +253,7 @@ MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src,
305
253
  {
306
254
  size_t const length = MIN(dstCapacity, srcSize);
307
255
  if (length > 0) {
308
- memcpy(dst, src, length);
256
+ ZSTD_memcpy(dst, src, length);
309
257
  }
310
258
  return length;
311
259
  }
@@ -320,28 +268,46 @@ MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src,
320
268
  * In which case, resize it down to free some memory */
321
269
  #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128
322
270
 
271
+ /* Controls whether the input/output buffer is buffered or stable. */
272
+ typedef enum {
273
+ ZSTD_bm_buffered = 0, /* Buffer the input/output */
274
+ ZSTD_bm_stable = 1 /* ZSTD_inBuffer/ZSTD_outBuffer is stable */
275
+ } ZSTD_bufferMode_e;
276
+
323
277
 
324
278
  /*-*******************************************
325
279
  * Private declarations
326
280
  *********************************************/
327
281
  typedef struct seqDef_s {
328
- U32 offset;
282
+ U32 offBase; /* offBase == Offset + ZSTD_REP_NUM, or repcode 1,2,3 */
329
283
  U16 litLength;
330
- U16 matchLength;
284
+ U16 mlBase; /* mlBase == matchLength - MINMATCH */
331
285
  } seqDef;
332
286
 
287
+ /* Controls whether seqStore has a single "long" litLength or matchLength. See seqStore_t. */
288
+ typedef enum {
289
+ ZSTD_llt_none = 0, /* no longLengthType */
290
+ ZSTD_llt_literalLength = 1, /* represents a long literal */
291
+ ZSTD_llt_matchLength = 2 /* represents a long match */
292
+ } ZSTD_longLengthType_e;
293
+
333
294
  typedef struct {
334
295
  seqDef* sequencesStart;
335
- seqDef* sequences;
336
- BYTE* litStart;
337
- BYTE* lit;
338
- BYTE* llCode;
339
- BYTE* mlCode;
340
- BYTE* ofCode;
296
+ seqDef* sequences; /* ptr to end of sequences */
297
+ BYTE* litStart;
298
+ BYTE* lit; /* ptr to end of literals */
299
+ BYTE* llCode;
300
+ BYTE* mlCode;
301
+ BYTE* ofCode;
341
302
  size_t maxNbSeq;
342
303
  size_t maxNbLit;
343
- U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
344
- U32 longLengthPos;
304
+
305
+ /* longLengthPos and longLengthType to allow us to represent either a single litLength or matchLength
306
+ * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment
307
+ * the existing value of the litLength or matchLength by 0x10000.
308
+ */
309
+ ZSTD_longLengthType_e longLengthType;
310
+ U32 longLengthPos; /* Index of the sequence to apply long length modification to */
345
311
  } seqStore_t;
346
312
 
347
313
  typedef struct {
@@ -351,19 +317,19 @@ typedef struct {
351
317
 
352
318
  /**
353
319
  * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences
354
- * indicated by longLengthPos and longLengthID, and adds MINMATCH back to matchLength.
320
+ * indicated by longLengthPos and longLengthType, and adds MINMATCH back to matchLength.
355
321
  */
356
322
  MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq)
357
323
  {
358
324
  ZSTD_sequenceLength seqLen;
359
325
  seqLen.litLength = seq->litLength;
360
- seqLen.matchLength = seq->matchLength + MINMATCH;
326
+ seqLen.matchLength = seq->mlBase + MINMATCH;
361
327
  if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {
362
- if (seqStore->longLengthID == 1) {
363
- seqLen.litLength += 0xFFFF;
328
+ if (seqStore->longLengthType == ZSTD_llt_literalLength) {
329
+ seqLen.litLength += 0x10000;
364
330
  }
365
- if (seqStore->longLengthID == 2) {
366
- seqLen.matchLength += 0xFFFF;
331
+ if (seqStore->longLengthType == ZSTD_llt_matchLength) {
332
+ seqLen.matchLength += 0x10000;
367
333
  }
368
334
  }
369
335
  return seqLen;
@@ -376,42 +342,13 @@ MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore
376
342
  * `decompressedBound != ZSTD_CONTENTSIZE_ERROR`
377
343
  */
378
344
  typedef struct {
345
+ size_t nbBlocks;
379
346
  size_t compressedSize;
380
347
  unsigned long long decompressedBound;
381
348
  } ZSTD_frameSizeInfo; /* decompress & legacy */
382
349
 
383
350
  const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */
384
- void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
385
-
386
- /* custom memory allocation functions */
387
- void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
388
- void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
389
- void ZSTD_free(void* ptr, ZSTD_customMem customMem);
390
-
391
-
392
- MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
393
- {
394
- assert(val != 0);
395
- {
396
- # if defined(_MSC_VER) /* Visual */
397
- unsigned long r=0;
398
- return _BitScanReverse(&r, val) ? (unsigned)r : 0;
399
- # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
400
- return __builtin_clz (val) ^ 31;
401
- # elif defined(__ICCARM__) /* IAR Intrinsic */
402
- return 31 - __CLZ(val);
403
- # else /* Software version */
404
- static const U32 DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
405
- U32 v = val;
406
- v |= v >> 1;
407
- v |= v >> 2;
408
- v |= v >> 4;
409
- v |= v >> 8;
410
- v |= v >> 16;
411
- return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
412
- # endif
413
- }
414
- }
351
+ int ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
415
352
 
416
353
 
417
354
  /* ZSTD_invalidateRepCodes() :
@@ -439,6 +376,14 @@ size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
439
376
  size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
440
377
  const void* src, size_t srcSize);
441
378
 
379
+ /**
380
+ * @returns true iff the CPU supports dynamic BMI2 dispatch.
381
+ */
382
+ MEM_STATIC int ZSTD_cpuSupportsBmi2(void)
383
+ {
384
+ ZSTD_cpuid_t cpuid = ZSTD_cpuid();
385
+ return ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid);
386
+ }
442
387
 
443
388
  #if defined (__cplusplus)
444
389
  }