zstd-ruby 1.4.5.0 → 1.5.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +8 -0
  3. data/.github/workflows/ruby.yml +35 -0
  4. data/README.md +2 -2
  5. data/ext/zstdruby/extconf.rb +2 -1
  6. data/ext/zstdruby/libzstd/BUCK +5 -7
  7. data/ext/zstdruby/libzstd/Makefile +225 -222
  8. data/ext/zstdruby/libzstd/README.md +43 -5
  9. data/ext/zstdruby/libzstd/common/bitstream.h +46 -22
  10. data/ext/zstdruby/libzstd/common/compiler.h +182 -22
  11. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  12. data/ext/zstdruby/libzstd/common/debug.c +1 -1
  13. data/ext/zstdruby/libzstd/common/debug.h +12 -19
  14. data/ext/zstdruby/libzstd/common/entropy_common.c +196 -44
  15. data/ext/zstdruby/libzstd/common/error_private.c +2 -1
  16. data/ext/zstdruby/libzstd/common/error_private.h +82 -3
  17. data/ext/zstdruby/libzstd/common/fse.h +41 -12
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +139 -22
  19. data/ext/zstdruby/libzstd/common/huf.h +47 -23
  20. data/ext/zstdruby/libzstd/common/mem.h +87 -98
  21. data/ext/zstdruby/libzstd/common/pool.c +23 -17
  22. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +131 -0
  24. data/ext/zstdruby/libzstd/common/threading.c +6 -5
  25. data/ext/zstdruby/libzstd/common/xxhash.c +6 -846
  26. data/ext/zstdruby/libzstd/common/xxhash.h +5568 -167
  27. data/ext/zstdruby/libzstd/common/zstd_common.c +10 -10
  28. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  29. data/ext/zstdruby/libzstd/common/zstd_internal.h +189 -142
  30. data/ext/zstdruby/libzstd/common/zstd_trace.h +163 -0
  31. data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
  32. data/ext/zstdruby/libzstd/compress/fse_compress.c +89 -46
  33. data/ext/zstdruby/libzstd/compress/hist.c +27 -29
  34. data/ext/zstdruby/libzstd/compress/hist.h +2 -2
  35. data/ext/zstdruby/libzstd/compress/huf_compress.c +770 -198
  36. data/ext/zstdruby/libzstd/compress/zstd_compress.c +2894 -863
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +390 -90
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +12 -11
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +4 -2
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +31 -8
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +1 -1
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +25 -297
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +1 -1
  44. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +206 -69
  45. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +307 -132
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +1 -1
  47. data/ext/zstdruby/libzstd/compress/zstd_fast.c +322 -143
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.h +1 -1
  49. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1136 -174
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +59 -1
  51. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +316 -213
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +9 -2
  53. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +106 -0
  54. data/ext/zstdruby/libzstd/compress/zstd_opt.c +373 -150
  55. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  56. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +152 -444
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +31 -113
  58. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1044 -403
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +571 -0
  60. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +9 -9
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  62. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +450 -105
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +913 -273
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +14 -5
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +59 -12
  66. data/ext/zstdruby/libzstd/deprecated/zbuff.h +1 -1
  67. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +1 -1
  68. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +24 -4
  69. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/cover.c +55 -38
  71. data/ext/zstdruby/libzstd/dictBuilder/cover.h +7 -6
  72. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  73. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +43 -34
  74. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +128 -58
  75. data/ext/zstdruby/libzstd/dll/example/Makefile +1 -1
  76. data/ext/zstdruby/libzstd/dll/example/README.md +16 -22
  77. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +1 -1
  78. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +8 -8
  79. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  80. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +9 -9
  81. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  82. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +9 -9
  83. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  84. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +10 -10
  85. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  86. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +13 -13
  87. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +1 -1
  88. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +13 -13
  89. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  90. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +13 -13
  91. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  92. data/ext/zstdruby/libzstd/libzstd.mk +185 -0
  93. data/ext/zstdruby/libzstd/libzstd.pc.in +4 -3
  94. data/ext/zstdruby/libzstd/modulemap/module.modulemap +4 -0
  95. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +154 -7
  96. data/ext/zstdruby/libzstd/zstd.h +699 -214
  97. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +2 -1
  98. data/ext/zstdruby/zstdruby.c +2 -2
  99. data/lib/zstd-ruby/version.rb +1 -1
  100. metadata +15 -6
  101. data/.travis.yml +0 -14
@@ -1,6 +1,6 @@
1
1
  /* ******************************************************************
2
2
  * Common functions of New Generation Entropy library
3
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
+ * Copyright (c) Yann Collet, Facebook, Inc.
4
4
  *
5
5
  * You can contact the author at :
6
6
  * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -38,8 +38,37 @@ const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
38
38
  /*-**************************************************************
39
39
  * FSE NCount encoding-decoding
40
40
  ****************************************************************/
41
- size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
42
- const void* headerBuffer, size_t hbSize)
41
+ static U32 FSE_ctz(U32 val)
42
+ {
43
+ assert(val != 0);
44
+ {
45
+ # if defined(_MSC_VER) /* Visual */
46
+ if (val != 0) {
47
+ unsigned long r;
48
+ _BitScanForward(&r, val);
49
+ return (unsigned)r;
50
+ } else {
51
+ /* Should not reach this code path */
52
+ __assume(0);
53
+ }
54
+ # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
55
+ return __builtin_ctz(val);
56
+ # elif defined(__ICCARM__) /* IAR Intrinsic */
57
+ return __CTZ(val);
58
+ # else /* Software version */
59
+ U32 count = 0;
60
+ while ((val & 1) == 0) {
61
+ val >>= 1;
62
+ ++count;
63
+ }
64
+ return count;
65
+ # endif
66
+ }
67
+ }
68
+
69
+ FORCE_INLINE_TEMPLATE
70
+ size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
71
+ const void* headerBuffer, size_t hbSize)
43
72
  {
44
73
  const BYTE* const istart = (const BYTE*) headerBuffer;
45
74
  const BYTE* const iend = istart + hbSize;
@@ -50,23 +79,23 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
50
79
  U32 bitStream;
51
80
  int bitCount;
52
81
  unsigned charnum = 0;
82
+ unsigned const maxSV1 = *maxSVPtr + 1;
53
83
  int previous0 = 0;
54
84
 
55
- if (hbSize < 4) {
56
- /* This function only works when hbSize >= 4 */
57
- char buffer[4];
58
- memset(buffer, 0, sizeof(buffer));
59
- memcpy(buffer, headerBuffer, hbSize);
85
+ if (hbSize < 8) {
86
+ /* This function only works when hbSize >= 8 */
87
+ char buffer[8] = {0};
88
+ ZSTD_memcpy(buffer, headerBuffer, hbSize);
60
89
  { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
61
90
  buffer, sizeof(buffer));
62
91
  if (FSE_isError(countSize)) return countSize;
63
92
  if (countSize > hbSize) return ERROR(corruption_detected);
64
93
  return countSize;
65
94
  } }
66
- assert(hbSize >= 4);
95
+ assert(hbSize >= 8);
67
96
 
68
97
  /* init */
69
- memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
98
+ ZSTD_memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
70
99
  bitStream = MEM_readLE32(ip);
71
100
  nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
72
101
  if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
@@ -77,36 +106,58 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
77
106
  threshold = 1<<nbBits;
78
107
  nbBits++;
79
108
 
80
- while ((remaining>1) & (charnum<=*maxSVPtr)) {
109
+ for (;;) {
81
110
  if (previous0) {
82
- unsigned n0 = charnum;
83
- while ((bitStream & 0xFFFF) == 0xFFFF) {
84
- n0 += 24;
85
- if (ip < iend-5) {
86
- ip += 2;
87
- bitStream = MEM_readLE32(ip) >> bitCount;
111
+ /* Count the number of repeats. Each time the
112
+ * 2-bit repeat code is 0b11 there is another
113
+ * repeat.
114
+ * Avoid UB by setting the high bit to 1.
115
+ */
116
+ int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
117
+ while (repeats >= 12) {
118
+ charnum += 3 * 12;
119
+ if (LIKELY(ip <= iend-7)) {
120
+ ip += 3;
88
121
  } else {
89
- bitStream >>= 16;
90
- bitCount += 16;
91
- } }
92
- while ((bitStream & 3) == 3) {
93
- n0 += 3;
94
- bitStream >>= 2;
95
- bitCount += 2;
122
+ bitCount -= (int)(8 * (iend - 7 - ip));
123
+ bitCount &= 31;
124
+ ip = iend - 4;
125
+ }
126
+ bitStream = MEM_readLE32(ip) >> bitCount;
127
+ repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
96
128
  }
97
- n0 += bitStream & 3;
129
+ charnum += 3 * repeats;
130
+ bitStream >>= 2 * repeats;
131
+ bitCount += 2 * repeats;
132
+
133
+ /* Add the final repeat which isn't 0b11. */
134
+ assert((bitStream & 3) < 3);
135
+ charnum += bitStream & 3;
98
136
  bitCount += 2;
99
- if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
100
- while (charnum < n0) normalizedCounter[charnum++] = 0;
101
- if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
137
+
138
+ /* This is an error, but break and return an error
139
+ * at the end, because returning out of a loop makes
140
+ * it harder for the compiler to optimize.
141
+ */
142
+ if (charnum >= maxSV1) break;
143
+
144
+ /* We don't need to set the normalized count to 0
145
+ * because we already memset the whole buffer to 0.
146
+ */
147
+
148
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
102
149
  assert((bitCount >> 3) <= 3); /* For first condition to work */
103
150
  ip += bitCount>>3;
104
151
  bitCount &= 7;
105
- bitStream = MEM_readLE32(ip) >> bitCount;
106
152
  } else {
107
- bitStream >>= 2;
108
- } }
109
- { int const max = (2*threshold-1) - remaining;
153
+ bitCount -= (int)(8 * (iend - 4 - ip));
154
+ bitCount &= 31;
155
+ ip = iend - 4;
156
+ }
157
+ bitStream = MEM_readLE32(ip) >> bitCount;
158
+ }
159
+ {
160
+ int const max = (2*threshold-1) - remaining;
110
161
  int count;
111
162
 
112
163
  if ((bitStream & (threshold-1)) < (U32)max) {
@@ -119,24 +170,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
119
170
  }
120
171
 
121
172
  count--; /* extra accuracy */
122
- remaining -= count < 0 ? -count : count; /* -1 means +1 */
173
+ /* When it matters (small blocks), this is a
174
+ * predictable branch, because we don't use -1.
175
+ */
176
+ if (count >= 0) {
177
+ remaining -= count;
178
+ } else {
179
+ assert(count == -1);
180
+ remaining += count;
181
+ }
123
182
  normalizedCounter[charnum++] = (short)count;
124
183
  previous0 = !count;
125
- while (remaining < threshold) {
126
- nbBits--;
127
- threshold >>= 1;
184
+
185
+ assert(threshold > 1);
186
+ if (remaining < threshold) {
187
+ /* This branch can be folded into the
188
+ * threshold update condition because we
189
+ * know that threshold > 1.
190
+ */
191
+ if (remaining <= 1) break;
192
+ nbBits = BIT_highbit32(remaining) + 1;
193
+ threshold = 1 << (nbBits - 1);
128
194
  }
195
+ if (charnum >= maxSV1) break;
129
196
 
130
- if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
197
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
131
198
  ip += bitCount>>3;
132
199
  bitCount &= 7;
133
200
  } else {
134
201
  bitCount -= (int)(8 * (iend - 4 - ip));
202
+ bitCount &= 31;
135
203
  ip = iend - 4;
136
204
  }
137
- bitStream = MEM_readLE32(ip) >> (bitCount & 31);
138
- } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
205
+ bitStream = MEM_readLE32(ip) >> bitCount;
206
+ } }
139
207
  if (remaining != 1) return ERROR(corruption_detected);
208
+ /* Only possible when there are too many zeros. */
209
+ if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
140
210
  if (bitCount > 32) return ERROR(corruption_detected);
141
211
  *maxSVPtr = charnum-1;
142
212
 
@@ -144,6 +214,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
144
214
  return ip-istart;
145
215
  }
146
216
 
217
+ /* Avoids the FORCE_INLINE of the _body() function. */
218
+ static size_t FSE_readNCount_body_default(
219
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
220
+ const void* headerBuffer, size_t hbSize)
221
+ {
222
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
223
+ }
224
+
225
+ #if DYNAMIC_BMI2
226
+ BMI2_TARGET_ATTRIBUTE static size_t FSE_readNCount_body_bmi2(
227
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
228
+ const void* headerBuffer, size_t hbSize)
229
+ {
230
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
231
+ }
232
+ #endif
233
+
234
+ size_t FSE_readNCount_bmi2(
235
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
236
+ const void* headerBuffer, size_t hbSize, int bmi2)
237
+ {
238
+ #if DYNAMIC_BMI2
239
+ if (bmi2) {
240
+ return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
241
+ }
242
+ #endif
243
+ (void)bmi2;
244
+ return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
245
+ }
246
+
247
+ size_t FSE_readNCount(
248
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
249
+ const void* headerBuffer, size_t hbSize)
250
+ {
251
+ return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0);
252
+ }
253
+
147
254
 
148
255
  /*! HUF_readStats() :
149
256
  Read compact Huffman tree, saved by HUF_writeCTable().
@@ -155,6 +262,17 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
155
262
  size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
156
263
  U32* nbSymbolsPtr, U32* tableLogPtr,
157
264
  const void* src, size_t srcSize)
265
+ {
266
+ U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
267
+ return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0);
268
+ }
269
+
270
+ FORCE_INLINE_TEMPLATE size_t
271
+ HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats,
272
+ U32* nbSymbolsPtr, U32* tableLogPtr,
273
+ const void* src, size_t srcSize,
274
+ void* workSpace, size_t wkspSize,
275
+ int bmi2)
158
276
  {
159
277
  U32 weightTotal;
160
278
  const BYTE* ip = (const BYTE*) src;
@@ -163,7 +281,7 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
163
281
 
164
282
  if (!srcSize) return ERROR(srcSize_wrong);
165
283
  iSize = ip[0];
166
- /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
284
+ /* ZSTD_memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
167
285
 
168
286
  if (iSize >= 128) { /* special header */
169
287
  oSize = iSize - 127;
@@ -177,17 +295,17 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
177
295
  huffWeight[n+1] = ip[n/2] & 15;
178
296
  } } }
179
297
  else { /* header compressed with FSE (normal case) */
180
- FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
181
298
  if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
182
- oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */
299
+ /* max (hwSize-1) values decoded, as last one is implied */
300
+ oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize, bmi2);
183
301
  if (FSE_isError(oSize)) return oSize;
184
302
  }
185
303
 
186
304
  /* collect weight stats */
187
- memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
305
+ ZSTD_memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
188
306
  weightTotal = 0;
189
307
  { U32 n; for (n=0; n<oSize; n++) {
190
- if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
308
+ if (huffWeight[n] > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
191
309
  rankStats[huffWeight[n]]++;
192
310
  weightTotal += (1 << huffWeight[n]) >> 1;
193
311
  } }
@@ -214,3 +332,37 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
214
332
  *nbSymbolsPtr = (U32)(oSize+1);
215
333
  return iSize+1;
216
334
  }
335
+
336
+ /* Avoids the FORCE_INLINE of the _body() function. */
337
+ static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats,
338
+ U32* nbSymbolsPtr, U32* tableLogPtr,
339
+ const void* src, size_t srcSize,
340
+ void* workSpace, size_t wkspSize)
341
+ {
342
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
343
+ }
344
+
345
+ #if DYNAMIC_BMI2
346
+ static BMI2_TARGET_ATTRIBUTE size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats,
347
+ U32* nbSymbolsPtr, U32* tableLogPtr,
348
+ const void* src, size_t srcSize,
349
+ void* workSpace, size_t wkspSize)
350
+ {
351
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
352
+ }
353
+ #endif
354
+
355
+ size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
356
+ U32* nbSymbolsPtr, U32* tableLogPtr,
357
+ const void* src, size_t srcSize,
358
+ void* workSpace, size_t wkspSize,
359
+ int bmi2)
360
+ {
361
+ #if DYNAMIC_BMI2
362
+ if (bmi2) {
363
+ return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
364
+ }
365
+ #endif
366
+ (void)bmi2;
367
+ return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
368
+ }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -48,6 +48,7 @@ const char* ERR_getErrorString(ERR_enum code)
48
48
  case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
49
49
  case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
50
50
  case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
51
+ case PREFIX(srcBuffer_wrong): return "Source buffer is wrong";
51
52
  case PREFIX(maxCode):
52
53
  default: return notErrorCode;
53
54
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -21,8 +21,10 @@ extern "C" {
21
21
  /* ****************************************
22
22
  * Dependencies
23
23
  ******************************************/
24
- #include <stddef.h> /* size_t */
25
- #include "zstd_errors.h" /* enum list */
24
+ #include "../zstd_errors.h" /* enum list */
25
+ #include "compiler.h"
26
+ #include "debug.h"
27
+ #include "zstd_deps.h" /* size_t */
26
28
 
27
29
 
28
30
  /* ****************************************
@@ -73,6 +75,83 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
73
75
  return ERR_getErrorString(ERR_getErrorCode(code));
74
76
  }
75
77
 
78
+ /**
79
+ * Ignore: this is an internal helper.
80
+ *
81
+ * This is a helper function to help force C99-correctness during compilation.
82
+ * Under strict compilation modes, variadic macro arguments can't be empty.
83
+ * However, variadic function arguments can be. Using a function therefore lets
84
+ * us statically check that at least one (string) argument was passed,
85
+ * independent of the compilation flags.
86
+ */
87
+ static INLINE_KEYWORD UNUSED_ATTR
88
+ void _force_has_format_string(const char *format, ...) {
89
+ (void)format;
90
+ }
91
+
92
+ /**
93
+ * Ignore: this is an internal helper.
94
+ *
95
+ * We want to force this function invocation to be syntactically correct, but
96
+ * we don't want to force runtime evaluation of its arguments.
97
+ */
98
+ #define _FORCE_HAS_FORMAT_STRING(...) \
99
+ if (0) { \
100
+ _force_has_format_string(__VA_ARGS__); \
101
+ }
102
+
103
+ #define ERR_QUOTE(str) #str
104
+
105
+ /**
106
+ * Return the specified error if the condition evaluates to true.
107
+ *
108
+ * In debug modes, prints additional information.
109
+ * In order to do that (particularly, printing the conditional that failed),
110
+ * this can't just wrap RETURN_ERROR().
111
+ */
112
+ #define RETURN_ERROR_IF(cond, err, ...) \
113
+ if (cond) { \
114
+ RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
115
+ __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
116
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
117
+ RAWLOG(3, ": " __VA_ARGS__); \
118
+ RAWLOG(3, "\n"); \
119
+ return ERROR(err); \
120
+ }
121
+
122
+ /**
123
+ * Unconditionally return the specified error.
124
+ *
125
+ * In debug modes, prints additional information.
126
+ */
127
+ #define RETURN_ERROR(err, ...) \
128
+ do { \
129
+ RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
130
+ __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
131
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
132
+ RAWLOG(3, ": " __VA_ARGS__); \
133
+ RAWLOG(3, "\n"); \
134
+ return ERROR(err); \
135
+ } while(0);
136
+
137
+ /**
138
+ * If the provided expression evaluates to an error code, returns that error code.
139
+ *
140
+ * In debug modes, prints additional information.
141
+ */
142
+ #define FORWARD_IF_ERROR(err, ...) \
143
+ do { \
144
+ size_t const err_code = (err); \
145
+ if (ERR_isError(err_code)) { \
146
+ RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
147
+ __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
148
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
149
+ RAWLOG(3, ": " __VA_ARGS__); \
150
+ RAWLOG(3, "\n"); \
151
+ return err_code; \
152
+ } \
153
+ } while(0);
154
+
76
155
  #if defined (__cplusplus)
77
156
  }
78
157
  #endif
@@ -1,7 +1,7 @@
1
1
  /* ******************************************************************
2
2
  * FSE : Finite State Entropy codec
3
3
  * Public Prototypes declaration
4
- * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
4
+ * Copyright (c) Yann Collet, Facebook, Inc.
5
5
  *
6
6
  * You can contact the author at :
7
7
  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -23,7 +23,7 @@ extern "C" {
23
23
  /*-*****************************************
24
24
  * Dependencies
25
25
  ******************************************/
26
- #include <stddef.h> /* size_t, ptrdiff_t */
26
+ #include "zstd_deps.h" /* size_t, ptrdiff_t */
27
27
 
28
28
 
29
29
  /*-*****************************************
@@ -137,10 +137,16 @@ FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize
137
137
  /*! FSE_normalizeCount():
138
138
  normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)
139
139
  'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).
140
+ useLowProbCount is a boolean parameter which trades off compressed size for
141
+ faster header decoding. When it is set to 1, the compressed data will be slightly
142
+ smaller. And when it is set to 0, FSE_readNCount() and FSE_buildDTable() will be
143
+ faster. If you are compressing a small amount of data (< 2 KB) then useLowProbCount=0
144
+ is a good default, since header deserialization makes a big speed difference.
145
+ Otherwise, useLowProbCount=1 is a good default, since the speed difference is small.
140
146
  @return : tableLog,
141
147
  or an errorCode, which can be tested using FSE_isError() */
142
148
  FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog,
143
- const unsigned* count, size_t srcSize, unsigned maxSymbolValue);
149
+ const unsigned* count, size_t srcSize, unsigned maxSymbolValue, unsigned useLowProbCount);
144
150
 
145
151
  /*! FSE_NCountWriteBound():
146
152
  Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.
@@ -228,6 +234,13 @@ FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter,
228
234
  unsigned* maxSymbolValuePtr, unsigned* tableLogPtr,
229
235
  const void* rBuffer, size_t rBuffSize);
230
236
 
237
+ /*! FSE_readNCount_bmi2():
238
+ * Same as FSE_readNCount() but pass bmi2=1 when your CPU supports BMI2 and 0 otherwise.
239
+ */
240
+ FSE_PUBLIC_API size_t FSE_readNCount_bmi2(short* normalizedCounter,
241
+ unsigned* maxSymbolValuePtr, unsigned* tableLogPtr,
242
+ const void* rBuffer, size_t rBuffSize, int bmi2);
243
+
231
244
  /*! Constructor and Destructor of FSE_DTable.
232
245
  Note that its size depends on 'tableLog' */
233
246
  typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
@@ -288,12 +301,12 @@ If there is an error, the function will return an error code, which can be teste
288
301
  *******************************************/
289
302
  /* FSE buffer bounds */
290
303
  #define FSE_NCOUNTBOUND 512
291
- #define FSE_BLOCKBOUND(size) (size + (size>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */)
304
+ #define FSE_BLOCKBOUND(size) ((size) + ((size)>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */)
292
305
  #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
293
306
 
294
307
  /* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */
295
- #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
296
- #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
308
+ #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<((maxTableLog)-1)) + (((maxSymbolValue)+1)*2))
309
+ #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<(maxTableLog)))
297
310
 
298
311
  /* or use the size to malloc() space directly. Pay attention to alignment restrictions though */
299
312
  #define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable))
@@ -309,9 +322,9 @@ unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsi
309
322
 
310
323
  /* FSE_compress_wksp() :
311
324
  * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
312
- * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.
325
+ * FSE_COMPRESS_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.
313
326
  */
314
- #define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) )
327
+ #define FSE_COMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) )
315
328
  size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
316
329
 
317
330
  size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits);
@@ -322,18 +335,31 @@ size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);
322
335
 
323
336
  /* FSE_buildCTable_wksp() :
324
337
  * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
325
- * `wkspSize` must be >= `(1<<tableLog)`.
338
+ * `wkspSize` must be >= `FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog)` of `unsigned`.
339
+ * See FSE_buildCTable_wksp() for breakdown of workspace usage.
326
340
  */
341
+ #define FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog) (((maxSymbolValue + 2) + (1ull << (tableLog)))/2 + sizeof(U64)/sizeof(U32) /* additional 8 bytes for potential table overwrite */)
342
+ #define FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) (sizeof(unsigned) * FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog))
327
343
  size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
328
344
 
345
+ #define FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) (sizeof(short) * (maxSymbolValue + 1) + (1ULL << maxTableLog) + 8)
346
+ #define FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ((FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) + sizeof(unsigned) - 1) / sizeof(unsigned))
347
+ FSE_PUBLIC_API size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
348
+ /**< Same as FSE_buildDTable(), using an externally allocated `workspace` produced with `FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxSymbolValue)` */
349
+
329
350
  size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
330
351
  /**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */
331
352
 
332
353
  size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
333
354
  /**< build a fake FSE_DTable, designed to always generate the same symbolValue */
334
355
 
335
- size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog);
336
- /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */
356
+ #define FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) (FSE_DTABLE_SIZE_U32(maxTableLog) + FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) + (FSE_MAX_SYMBOL_VALUE + 1) / 2 + 1)
357
+ #define FSE_DECOMPRESS_WKSP_SIZE(maxTableLog, maxSymbolValue) (FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(unsigned))
358
+ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize);
359
+ /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DECOMPRESS_WKSP_SIZE_U32(maxLog, maxSymbolValue)` */
360
+
361
+ size_t FSE_decompress_wksp_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize, int bmi2);
362
+ /**< Same as FSE_decompress_wksp() but with dynamic BMI2 support. Pass 1 if your CPU supports BMI2 or 0 if it doesn't. */
337
363
 
338
364
  typedef enum {
339
365
  FSE_repeat_none, /**< Cannot use the previous table */
@@ -644,6 +670,9 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
644
670
  #ifndef FSE_DEFAULT_MEMORY_USAGE
645
671
  # define FSE_DEFAULT_MEMORY_USAGE 13
646
672
  #endif
673
+ #if (FSE_DEFAULT_MEMORY_USAGE > FSE_MAX_MEMORY_USAGE)
674
+ # error "FSE_DEFAULT_MEMORY_USAGE must be <= FSE_MAX_MEMORY_USAGE"
675
+ #endif
647
676
 
648
677
  /*!FSE_MAX_SYMBOL_VALUE :
649
678
  * Maximum symbol value authorized.
@@ -677,7 +706,7 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
677
706
  # error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
678
707
  #endif
679
708
 
680
- #define FSE_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3)
709
+ #define FSE_TABLESTEP(tableSize) (((tableSize)>>1) + ((tableSize)>>3) + 3)
681
710
 
682
711
 
683
712
  #endif /* FSE_STATIC_LINKING_ONLY */