zstd-ruby 1.3.1.1 → 1.3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/zstdruby/libzstd/.gitignore +1 -0
  4. data/ext/zstdruby/libzstd/Makefile +40 -26
  5. data/ext/zstdruby/libzstd/README.md +68 -45
  6. data/ext/zstdruby/libzstd/common/bitstream.h +35 -23
  7. data/ext/zstdruby/libzstd/common/compiler.h +1 -0
  8. data/ext/zstdruby/libzstd/common/error_private.c +4 -2
  9. data/ext/zstdruby/libzstd/common/error_private.h +4 -4
  10. data/ext/zstdruby/libzstd/common/fse.h +1 -1
  11. data/ext/zstdruby/libzstd/common/huf.h +1 -1
  12. data/ext/zstdruby/libzstd/common/mem.h +1 -0
  13. data/ext/zstdruby/libzstd/common/pool.c +61 -46
  14. data/ext/zstdruby/libzstd/common/pool.h +4 -0
  15. data/ext/zstdruby/libzstd/common/threading.c +11 -15
  16. data/ext/zstdruby/libzstd/common/threading.h +52 -32
  17. data/ext/zstdruby/libzstd/common/zstd_common.c +2 -2
  18. data/ext/zstdruby/libzstd/common/zstd_errors.h +3 -1
  19. data/ext/zstdruby/libzstd/common/zstd_internal.h +95 -21
  20. data/ext/zstdruby/libzstd/compress/fse_compress.c +3 -1
  21. data/ext/zstdruby/libzstd/compress/huf_compress.c +4 -3
  22. data/ext/zstdruby/libzstd/compress/zstd_compress.c +922 -2102
  23. data/ext/zstdruby/libzstd/compress/zstd_compress.h +307 -0
  24. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +308 -0
  25. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +28 -0
  26. data/ext/zstdruby/libzstd/compress/zstd_fast.c +242 -0
  27. data/ext/zstdruby/libzstd/compress/zstd_fast.h +30 -0
  28. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +749 -0
  29. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +38 -0
  30. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +707 -0
  31. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +67 -0
  32. data/ext/zstdruby/libzstd/compress/zstd_opt.c +957 -0
  33. data/ext/zstdruby/libzstd/compress/zstd_opt.h +14 -922
  34. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +210 -133
  35. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +20 -3
  36. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +373 -196
  37. data/ext/zstdruby/libzstd/deprecated/zbuff.h +1 -0
  38. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +1 -0
  39. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -0
  40. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -0
  41. data/ext/zstdruby/libzstd/dictBuilder/cover.c +33 -22
  42. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +8 -5
  43. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +1 -0
  44. data/ext/zstdruby/libzstd/dll/example/Makefile +5 -5
  45. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +1 -0
  46. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +1 -0
  47. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -0
  48. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +1 -0
  49. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -0
  50. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +1 -0
  51. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -0
  52. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +1 -0
  53. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -0
  54. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +1 -0
  55. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +1 -0
  56. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +1 -0
  57. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -0
  58. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +1 -0
  59. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -0
  60. data/ext/zstdruby/libzstd/zstd.h +366 -118
  61. data/lib/zstd-ruby/version.rb +1 -1
  62. metadata +11 -1
@@ -5,934 +5,26 @@
5
5
  * This source code is licensed under both the BSD-style license (found in the
6
6
  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
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.
8
9
  */
9
10
 
11
+ #ifndef ZSTD_OPT_H
12
+ #define ZSTD_OPT_H
10
13
 
11
- /* Note : this file is intended to be included within zstd_compress.c */
14
+ #include "zstd_compress.h"
12
15
 
16
+ #if defined (__cplusplus)
17
+ extern "C" {
18
+ #endif
13
19
 
14
- #ifndef ZSTD_OPT_H_91842398743
15
- #define ZSTD_OPT_H_91842398743
20
+ size_t ZSTD_compressBlock_btopt(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
21
+ size_t ZSTD_compressBlock_btultra(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
16
22
 
23
+ size_t ZSTD_compressBlock_btopt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
24
+ size_t ZSTD_compressBlock_btultra_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize);
17
25
 
18
- #define ZSTD_LITFREQ_ADD 2
19
- #define ZSTD_FREQ_DIV 4
20
- #define ZSTD_MAX_PRICE (1<<30)
21
-
22
- /*-*************************************
23
- * Price functions for optimal parser
24
- ***************************************/
25
- static void ZSTD_setLog2Prices(optState_t* optPtr)
26
- {
27
- optPtr->log2matchLengthSum = ZSTD_highbit32(optPtr->matchLengthSum+1);
28
- optPtr->log2litLengthSum = ZSTD_highbit32(optPtr->litLengthSum+1);
29
- optPtr->log2litSum = ZSTD_highbit32(optPtr->litSum+1);
30
- optPtr->log2offCodeSum = ZSTD_highbit32(optPtr->offCodeSum+1);
31
- optPtr->factor = 1 + ((optPtr->litSum>>5) / optPtr->litLengthSum) + ((optPtr->litSum<<1) / (optPtr->litSum + optPtr->matchSum));
32
- }
33
-
34
-
35
- static void ZSTD_rescaleFreqs(optState_t* optPtr, const BYTE* src, size_t srcSize)
36
- {
37
- unsigned u;
38
-
39
- optPtr->cachedLiterals = NULL;
40
- optPtr->cachedPrice = optPtr->cachedLitLength = 0;
41
- optPtr->staticPrices = 0;
42
-
43
- if (optPtr->litLengthSum == 0) {
44
- if (srcSize <= 1024) optPtr->staticPrices = 1;
45
-
46
- assert(optPtr->litFreq!=NULL);
47
- for (u=0; u<=MaxLit; u++)
48
- optPtr->litFreq[u] = 0;
49
- for (u=0; u<srcSize; u++)
50
- optPtr->litFreq[src[u]]++;
51
-
52
- optPtr->litSum = 0;
53
- optPtr->litLengthSum = MaxLL+1;
54
- optPtr->matchLengthSum = MaxML+1;
55
- optPtr->offCodeSum = (MaxOff+1);
56
- optPtr->matchSum = (ZSTD_LITFREQ_ADD<<Litbits);
57
-
58
- for (u=0; u<=MaxLit; u++) {
59
- optPtr->litFreq[u] = 1 + (optPtr->litFreq[u]>>ZSTD_FREQ_DIV);
60
- optPtr->litSum += optPtr->litFreq[u];
61
- }
62
- for (u=0; u<=MaxLL; u++)
63
- optPtr->litLengthFreq[u] = 1;
64
- for (u=0; u<=MaxML; u++)
65
- optPtr->matchLengthFreq[u] = 1;
66
- for (u=0; u<=MaxOff; u++)
67
- optPtr->offCodeFreq[u] = 1;
68
- } else {
69
- optPtr->matchLengthSum = 0;
70
- optPtr->litLengthSum = 0;
71
- optPtr->offCodeSum = 0;
72
- optPtr->matchSum = 0;
73
- optPtr->litSum = 0;
74
-
75
- for (u=0; u<=MaxLit; u++) {
76
- optPtr->litFreq[u] = 1 + (optPtr->litFreq[u]>>(ZSTD_FREQ_DIV+1));
77
- optPtr->litSum += optPtr->litFreq[u];
78
- }
79
- for (u=0; u<=MaxLL; u++) {
80
- optPtr->litLengthFreq[u] = 1 + (optPtr->litLengthFreq[u]>>(ZSTD_FREQ_DIV+1));
81
- optPtr->litLengthSum += optPtr->litLengthFreq[u];
82
- }
83
- for (u=0; u<=MaxML; u++) {
84
- optPtr->matchLengthFreq[u] = 1 + (optPtr->matchLengthFreq[u]>>ZSTD_FREQ_DIV);
85
- optPtr->matchLengthSum += optPtr->matchLengthFreq[u];
86
- optPtr->matchSum += optPtr->matchLengthFreq[u] * (u + 3);
87
- }
88
- optPtr->matchSum *= ZSTD_LITFREQ_ADD;
89
- for (u=0; u<=MaxOff; u++) {
90
- optPtr->offCodeFreq[u] = 1 + (optPtr->offCodeFreq[u]>>ZSTD_FREQ_DIV);
91
- optPtr->offCodeSum += optPtr->offCodeFreq[u];
92
- }
93
- }
94
-
95
- ZSTD_setLog2Prices(optPtr);
96
- }
97
-
98
-
99
- static U32 ZSTD_getLiteralPrice(optState_t* optPtr, U32 litLength, const BYTE* literals)
100
- {
101
- U32 price, u;
102
-
103
- if (optPtr->staticPrices)
104
- return ZSTD_highbit32((U32)litLength+1) + (litLength*6);
105
-
106
- if (litLength == 0)
107
- return optPtr->log2litLengthSum - ZSTD_highbit32(optPtr->litLengthFreq[0]+1);
108
-
109
- /* literals */
110
- if (optPtr->cachedLiterals == literals) {
111
- U32 const additional = litLength - optPtr->cachedLitLength;
112
- const BYTE* literals2 = optPtr->cachedLiterals + optPtr->cachedLitLength;
113
- price = optPtr->cachedPrice + additional * optPtr->log2litSum;
114
- for (u=0; u < additional; u++)
115
- price -= ZSTD_highbit32(optPtr->litFreq[literals2[u]]+1);
116
- optPtr->cachedPrice = price;
117
- optPtr->cachedLitLength = litLength;
118
- } else {
119
- price = litLength * optPtr->log2litSum;
120
- for (u=0; u < litLength; u++)
121
- price -= ZSTD_highbit32(optPtr->litFreq[literals[u]]+1);
122
-
123
- if (litLength >= 12) {
124
- optPtr->cachedLiterals = literals;
125
- optPtr->cachedPrice = price;
126
- optPtr->cachedLitLength = litLength;
127
- }
128
- }
129
-
130
- /* literal Length */
131
- { const BYTE LL_deltaCode = 19;
132
- const BYTE llCode = (litLength>63) ? (BYTE)ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
133
- price += LL_bits[llCode] + optPtr->log2litLengthSum - ZSTD_highbit32(optPtr->litLengthFreq[llCode]+1);
134
- }
135
-
136
- return price;
137
- }
138
-
139
-
140
- FORCE_INLINE_TEMPLATE U32 ZSTD_getPrice(optState_t* optPtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength, const int ultra)
141
- {
142
- /* offset */
143
- U32 price;
144
- BYTE const offCode = (BYTE)ZSTD_highbit32(offset+1);
145
-
146
- if (optPtr->staticPrices)
147
- return ZSTD_getLiteralPrice(optPtr, litLength, literals) + ZSTD_highbit32((U32)matchLength+1) + 16 + offCode;
148
-
149
- price = offCode + optPtr->log2offCodeSum - ZSTD_highbit32(optPtr->offCodeFreq[offCode]+1);
150
- if (!ultra && offCode >= 20) price += (offCode-19)*2;
151
-
152
- /* match Length */
153
- { const BYTE ML_deltaCode = 36;
154
- const BYTE mlCode = (matchLength>127) ? (BYTE)ZSTD_highbit32(matchLength) + ML_deltaCode : ML_Code[matchLength];
155
- price += ML_bits[mlCode] + optPtr->log2matchLengthSum - ZSTD_highbit32(optPtr->matchLengthFreq[mlCode]+1);
156
- }
157
-
158
- return price + ZSTD_getLiteralPrice(optPtr, litLength, literals) + optPtr->factor;
159
- }
160
-
161
-
162
- static void ZSTD_updatePrice(optState_t* optPtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength)
163
- {
164
- U32 u;
165
-
166
- /* literals */
167
- optPtr->litSum += litLength*ZSTD_LITFREQ_ADD;
168
- for (u=0; u < litLength; u++)
169
- optPtr->litFreq[literals[u]] += ZSTD_LITFREQ_ADD;
170
-
171
- /* literal Length */
172
- { const BYTE LL_deltaCode = 19;
173
- const BYTE llCode = (litLength>63) ? (BYTE)ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
174
- optPtr->litLengthFreq[llCode]++;
175
- optPtr->litLengthSum++;
176
- }
177
-
178
- /* match offset */
179
- { BYTE const offCode = (BYTE)ZSTD_highbit32(offset+1);
180
- optPtr->offCodeSum++;
181
- optPtr->offCodeFreq[offCode]++;
182
- }
183
-
184
- /* match Length */
185
- { const BYTE ML_deltaCode = 36;
186
- const BYTE mlCode = (matchLength>127) ? (BYTE)ZSTD_highbit32(matchLength) + ML_deltaCode : ML_Code[matchLength];
187
- optPtr->matchLengthFreq[mlCode]++;
188
- optPtr->matchLengthSum++;
189
- }
190
-
191
- ZSTD_setLog2Prices(optPtr);
192
- }
193
-
194
-
195
- #define SET_PRICE(pos, mlen_, offset_, litlen_, price_) \
196
- { \
197
- while (last_pos < pos) { opt[last_pos+1].price = ZSTD_MAX_PRICE; last_pos++; } \
198
- opt[pos].mlen = mlen_; \
199
- opt[pos].off = offset_; \
200
- opt[pos].litlen = litlen_; \
201
- opt[pos].price = price_; \
202
- }
203
-
204
-
205
- /* function safe only for comparisons */
206
- static U32 ZSTD_readMINMATCH(const void* memPtr, U32 length)
207
- {
208
- switch (length)
209
- {
210
- default :
211
- case 4 : return MEM_read32(memPtr);
212
- case 3 : if (MEM_isLittleEndian())
213
- return MEM_read32(memPtr)<<8;
214
- else
215
- return MEM_read32(memPtr)>>8;
216
- }
217
- }
218
-
219
-
220
- /* Update hashTable3 up to ip (excluded)
221
- Assumption : always within prefix (i.e. not within extDict) */
222
- static
223
- U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_CCtx* zc, const BYTE* ip)
224
- {
225
- U32* const hashTable3 = zc->hashTable3;
226
- U32 const hashLog3 = zc->hashLog3;
227
- const BYTE* const base = zc->base;
228
- U32 idx = zc->nextToUpdate3;
229
- const U32 target = zc->nextToUpdate3 = (U32)(ip - base);
230
- const size_t hash3 = ZSTD_hash3Ptr(ip, hashLog3);
231
-
232
- while(idx < target) {
233
- hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx;
234
- idx++;
235
- }
236
-
237
- return hashTable3[hash3];
238
- }
239
-
240
-
241
- /*-*************************************
242
- * Binary Tree search
243
- ***************************************/
244
- static U32 ZSTD_insertBtAndGetAllMatches (
245
- ZSTD_CCtx* zc,
246
- const BYTE* const ip, const BYTE* const iLimit,
247
- U32 nbCompares, const U32 mls,
248
- U32 extDict, ZSTD_match_t* matches, const U32 minMatchLen)
249
- {
250
- const BYTE* const base = zc->base;
251
- const U32 current = (U32)(ip-base);
252
- const U32 hashLog = zc->appliedParams.cParams.hashLog;
253
- const size_t h = ZSTD_hashPtr(ip, hashLog, mls);
254
- U32* const hashTable = zc->hashTable;
255
- U32 matchIndex = hashTable[h];
256
- U32* const bt = zc->chainTable;
257
- const U32 btLog = zc->appliedParams.cParams.chainLog - 1;
258
- const U32 btMask= (1U << btLog) - 1;
259
- size_t commonLengthSmaller=0, commonLengthLarger=0;
260
- const BYTE* const dictBase = zc->dictBase;
261
- const U32 dictLimit = zc->dictLimit;
262
- const BYTE* const dictEnd = dictBase + dictLimit;
263
- const BYTE* const prefixStart = base + dictLimit;
264
- const U32 btLow = btMask >= current ? 0 : current - btMask;
265
- const U32 windowLow = zc->lowLimit;
266
- U32* smallerPtr = bt + 2*(current&btMask);
267
- U32* largerPtr = bt + 2*(current&btMask) + 1;
268
- U32 matchEndIdx = current+8;
269
- U32 dummy32; /* to be nullified at the end */
270
- U32 mnum = 0;
271
-
272
- const U32 minMatch = (mls == 3) ? 3 : 4;
273
- size_t bestLength = minMatchLen-1;
274
-
275
- if (minMatch == 3) { /* HC3 match finder */
276
- U32 const matchIndex3 = ZSTD_insertAndFindFirstIndexHash3 (zc, ip);
277
- if (matchIndex3>windowLow && (current - matchIndex3 < (1<<18))) {
278
- const BYTE* match;
279
- size_t currentMl=0;
280
- if ((!extDict) || matchIndex3 >= dictLimit) {
281
- match = base + matchIndex3;
282
- if (match[bestLength] == ip[bestLength]) currentMl = ZSTD_count(ip, match, iLimit);
283
- } else {
284
- match = dictBase + matchIndex3;
285
- if (ZSTD_readMINMATCH(match, MINMATCH) == ZSTD_readMINMATCH(ip, MINMATCH)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */
286
- currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iLimit, dictEnd, prefixStart) + MINMATCH;
287
- }
288
-
289
- /* save best solution */
290
- if (currentMl > bestLength) {
291
- bestLength = currentMl;
292
- matches[mnum].off = ZSTD_REP_MOVE_OPT + current - matchIndex3;
293
- matches[mnum].len = (U32)currentMl;
294
- mnum++;
295
- if (currentMl > ZSTD_OPT_NUM) goto update;
296
- if (ip+currentMl == iLimit) goto update; /* best possible, and avoid read overflow*/
297
- }
298
- }
299
- }
300
-
301
- hashTable[h] = current; /* Update Hash Table */
302
-
303
- while (nbCompares-- && (matchIndex > windowLow)) {
304
- U32* nextPtr = bt + 2*(matchIndex & btMask);
305
- size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
306
- const BYTE* match;
307
-
308
- if ((!extDict) || (matchIndex+matchLength >= dictLimit)) {
309
- match = base + matchIndex;
310
- if (match[matchLength] == ip[matchLength]) {
311
- matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iLimit) +1;
312
- }
313
- } else {
314
- match = dictBase + matchIndex;
315
- matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iLimit, dictEnd, prefixStart);
316
- if (matchIndex+matchLength >= dictLimit)
317
- match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
318
- }
319
-
320
- if (matchLength > bestLength) {
321
- if (matchLength > matchEndIdx - matchIndex) matchEndIdx = matchIndex + (U32)matchLength;
322
- bestLength = matchLength;
323
- matches[mnum].off = ZSTD_REP_MOVE_OPT + current - matchIndex;
324
- matches[mnum].len = (U32)matchLength;
325
- mnum++;
326
- if (matchLength > ZSTD_OPT_NUM) break;
327
- if (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */
328
- break; /* drop, to guarantee consistency (miss a little bit of compression) */
329
- }
330
-
331
- if (match[matchLength] < ip[matchLength]) {
332
- /* match is smaller than current */
333
- *smallerPtr = matchIndex; /* update smaller idx */
334
- commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
335
- if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */
336
- smallerPtr = nextPtr+1; /* new "smaller" => larger of match */
337
- matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
338
- } else {
339
- /* match is larger than current */
340
- *largerPtr = matchIndex;
341
- commonLengthLarger = matchLength;
342
- if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */
343
- largerPtr = nextPtr;
344
- matchIndex = nextPtr[0];
345
- } }
346
-
347
- *smallerPtr = *largerPtr = 0;
348
-
349
- update:
350
- zc->nextToUpdate = (matchEndIdx > current + 8) ? matchEndIdx - 8 : current+1;
351
- return mnum;
352
- }
353
-
354
-
355
- /** Tree updater, providing best match */
356
- static U32 ZSTD_BtGetAllMatches (
357
- ZSTD_CCtx* zc,
358
- const BYTE* const ip, const BYTE* const iLimit,
359
- const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches, const U32 minMatchLen)
360
- {
361
- if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */
362
- ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls);
363
- return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 0, matches, minMatchLen);
364
- }
365
-
366
-
367
- static U32 ZSTD_BtGetAllMatches_selectMLS (
368
- ZSTD_CCtx* zc, /* Index table will be updated */
369
- const BYTE* ip, const BYTE* const iHighLimit,
370
- const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, const U32 minMatchLen)
371
- {
372
- switch(matchLengthSearch)
373
- {
374
- case 3 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 3, matches, minMatchLen);
375
- default :
376
- case 4 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minMatchLen);
377
- case 5 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minMatchLen);
378
- case 7 :
379
- case 6 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minMatchLen);
380
- }
381
- }
382
-
383
- /** Tree updater, providing best match */
384
- static U32 ZSTD_BtGetAllMatches_extDict (
385
- ZSTD_CCtx* zc,
386
- const BYTE* const ip, const BYTE* const iLimit,
387
- const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches, const U32 minMatchLen)
388
- {
389
- if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */
390
- ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls);
391
- return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 1, matches, minMatchLen);
392
- }
393
-
394
-
395
- static U32 ZSTD_BtGetAllMatches_selectMLS_extDict (
396
- ZSTD_CCtx* zc, /* Index table will be updated */
397
- const BYTE* ip, const BYTE* const iHighLimit,
398
- const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, const U32 minMatchLen)
399
- {
400
- switch(matchLengthSearch)
401
- {
402
- case 3 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 3, matches, minMatchLen);
403
- default :
404
- case 4 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minMatchLen);
405
- case 5 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minMatchLen);
406
- case 7 :
407
- case 6 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minMatchLen);
408
- }
409
- }
410
-
411
-
412
- /*-*******************************
413
- * Optimal parser
414
- *********************************/
415
- FORCE_INLINE_TEMPLATE
416
- void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
417
- const void* src, size_t srcSize, const int ultra)
418
- {
419
- seqStore_t* seqStorePtr = &(ctx->seqStore);
420
- optState_t* optStatePtr = &(ctx->optState);
421
- const BYTE* const istart = (const BYTE*)src;
422
- const BYTE* ip = istart;
423
- const BYTE* anchor = istart;
424
- const BYTE* const iend = istart + srcSize;
425
- const BYTE* const ilimit = iend - 8;
426
- const BYTE* const base = ctx->base;
427
- const BYTE* const prefixStart = base + ctx->dictLimit;
428
-
429
- const U32 maxSearches = 1U << ctx->appliedParams.cParams.searchLog;
430
- const U32 sufficient_len = ctx->appliedParams.cParams.targetLength;
431
- const U32 mls = ctx->appliedParams.cParams.searchLength;
432
- const U32 minMatch = (ctx->appliedParams.cParams.searchLength == 3) ? 3 : 4;
433
-
434
- ZSTD_optimal_t* opt = optStatePtr->priceTable;
435
- ZSTD_match_t* matches = optStatePtr->matchTable;
436
- const BYTE* inr;
437
- U32 offset, rep[ZSTD_REP_NUM];
438
-
439
- /* init */
440
- ctx->nextToUpdate3 = ctx->nextToUpdate;
441
- ZSTD_rescaleFreqs(optStatePtr, (const BYTE*)src, srcSize);
442
- ip += (ip==prefixStart);
443
- { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) rep[i]=seqStorePtr->rep[i]; }
444
-
445
- /* Match Loop */
446
- while (ip < ilimit) {
447
- U32 cur, match_num, last_pos, litlen, price;
448
- U32 u, mlen, best_mlen, best_off, litLength;
449
- memset(opt, 0, sizeof(ZSTD_optimal_t));
450
- last_pos = 0;
451
- litlen = (U32)(ip - anchor);
452
-
453
- /* check repCode */
454
- { U32 i, last_i = ZSTD_REP_CHECK + (ip==anchor);
455
- for (i=(ip == anchor); i<last_i; i++) {
456
- const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : rep[i];
457
- if ( (repCur > 0) && (repCur < (S32)(ip-prefixStart))
458
- && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(ip - repCur, minMatch))) {
459
- mlen = (U32)ZSTD_count(ip+minMatch, ip+minMatch-repCur, iend) + minMatch;
460
- if (mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) {
461
- best_mlen = mlen; best_off = i; cur = 0; last_pos = 1;
462
- goto _storeSequence;
463
- }
464
- best_off = i - (ip == anchor);
465
- do {
466
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
467
- if (mlen > last_pos || price < opt[mlen].price)
468
- SET_PRICE(mlen, mlen, i, litlen, price); /* note : macro modifies last_pos */
469
- mlen--;
470
- } while (mlen >= minMatch);
471
- } } }
472
-
473
- match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, ip, iend, maxSearches, mls, matches, minMatch);
474
-
475
- if (!last_pos && !match_num) { ip++; continue; }
476
-
477
- if (match_num && (matches[match_num-1].len > sufficient_len || matches[match_num-1].len >= ZSTD_OPT_NUM)) {
478
- best_mlen = matches[match_num-1].len;
479
- best_off = matches[match_num-1].off;
480
- cur = 0;
481
- last_pos = 1;
482
- goto _storeSequence;
483
- }
484
-
485
- /* set prices using matches at position = 0 */
486
- best_mlen = (last_pos) ? last_pos : minMatch;
487
- for (u = 0; u < match_num; u++) {
488
- mlen = (u>0) ? matches[u-1].len+1 : best_mlen;
489
- best_mlen = matches[u].len;
490
- while (mlen <= best_mlen) {
491
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra);
492
- if (mlen > last_pos || price < opt[mlen].price)
493
- SET_PRICE(mlen, mlen, matches[u].off, litlen, price); /* note : macro modifies last_pos */
494
- mlen++;
495
- } }
496
-
497
- if (last_pos < minMatch) { ip++; continue; }
498
-
499
- /* initialize opt[0] */
500
- { U32 i ; for (i=0; i<ZSTD_REP_NUM; i++) opt[0].rep[i] = rep[i]; }
501
- opt[0].mlen = 1;
502
- opt[0].litlen = litlen;
503
-
504
- /* check further positions */
505
- for (cur = 1; cur <= last_pos; cur++) {
506
- inr = ip + cur;
507
-
508
- if (opt[cur-1].mlen == 1) {
509
- litlen = opt[cur-1].litlen + 1;
510
- if (cur > litlen) {
511
- price = opt[cur - litlen].price + ZSTD_getLiteralPrice(optStatePtr, litlen, inr-litlen);
512
- } else
513
- price = ZSTD_getLiteralPrice(optStatePtr, litlen, anchor);
514
- } else {
515
- litlen = 1;
516
- price = opt[cur - 1].price + ZSTD_getLiteralPrice(optStatePtr, litlen, inr-1);
517
- }
518
-
519
- if (cur > last_pos || price <= opt[cur].price)
520
- SET_PRICE(cur, 1, 0, litlen, price);
521
-
522
- if (cur == last_pos) break;
523
-
524
- if (inr > ilimit) /* last match must start at a minimum distance of 8 from oend */
525
- continue;
526
-
527
- mlen = opt[cur].mlen;
528
- if (opt[cur].off > ZSTD_REP_MOVE_OPT) {
529
- opt[cur].rep[2] = opt[cur-mlen].rep[1];
530
- opt[cur].rep[1] = opt[cur-mlen].rep[0];
531
- opt[cur].rep[0] = opt[cur].off - ZSTD_REP_MOVE_OPT;
532
- } else {
533
- opt[cur].rep[2] = (opt[cur].off > 1) ? opt[cur-mlen].rep[1] : opt[cur-mlen].rep[2];
534
- opt[cur].rep[1] = (opt[cur].off > 0) ? opt[cur-mlen].rep[0] : opt[cur-mlen].rep[1];
535
- opt[cur].rep[0] = ((opt[cur].off==ZSTD_REP_MOVE_OPT) && (mlen != 1)) ? (opt[cur-mlen].rep[0] - 1) : (opt[cur-mlen].rep[opt[cur].off]);
536
- }
537
-
538
- best_mlen = minMatch;
539
- { U32 i, last_i = ZSTD_REP_CHECK + (mlen != 1);
540
- for (i=(opt[cur].mlen != 1); i<last_i; i++) { /* check rep */
541
- const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (opt[cur].rep[0] - 1) : opt[cur].rep[i];
542
- if ( (repCur > 0) && (repCur < (S32)(inr-prefixStart))
543
- && (ZSTD_readMINMATCH(inr, minMatch) == ZSTD_readMINMATCH(inr - repCur, minMatch))) {
544
- mlen = (U32)ZSTD_count(inr+minMatch, inr+minMatch - repCur, iend) + minMatch;
545
-
546
- if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) {
547
- best_mlen = mlen; best_off = i; last_pos = cur + 1;
548
- goto _storeSequence;
549
- }
550
-
551
- best_off = i - (opt[cur].mlen != 1);
552
- if (mlen > best_mlen) best_mlen = mlen;
553
-
554
- do {
555
- if (opt[cur].mlen == 1) {
556
- litlen = opt[cur].litlen;
557
- if (cur > litlen) {
558
- price = opt[cur - litlen].price + ZSTD_getPrice(optStatePtr, litlen, inr-litlen, best_off, mlen - MINMATCH, ultra);
559
- } else
560
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
561
- } else {
562
- litlen = 0;
563
- price = opt[cur].price + ZSTD_getPrice(optStatePtr, 0, NULL, best_off, mlen - MINMATCH, ultra);
564
- }
565
-
566
- if (cur + mlen > last_pos || price <= opt[cur + mlen].price)
567
- SET_PRICE(cur + mlen, mlen, i, litlen, price);
568
- mlen--;
569
- } while (mlen >= minMatch);
570
- } } }
571
-
572
- match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, inr, iend, maxSearches, mls, matches, best_mlen);
573
-
574
- if (match_num > 0 && (matches[match_num-1].len > sufficient_len || cur + matches[match_num-1].len >= ZSTD_OPT_NUM)) {
575
- best_mlen = matches[match_num-1].len;
576
- best_off = matches[match_num-1].off;
577
- last_pos = cur + 1;
578
- goto _storeSequence;
579
- }
580
-
581
- /* set prices using matches at position = cur */
582
- for (u = 0; u < match_num; u++) {
583
- mlen = (u>0) ? matches[u-1].len+1 : best_mlen;
584
- best_mlen = matches[u].len;
585
-
586
- while (mlen <= best_mlen) {
587
- if (opt[cur].mlen == 1) {
588
- litlen = opt[cur].litlen;
589
- if (cur > litlen)
590
- price = opt[cur - litlen].price + ZSTD_getPrice(optStatePtr, litlen, ip+cur-litlen, matches[u].off-1, mlen - MINMATCH, ultra);
591
- else
592
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra);
593
- } else {
594
- litlen = 0;
595
- price = opt[cur].price + ZSTD_getPrice(optStatePtr, 0, NULL, matches[u].off-1, mlen - MINMATCH, ultra);
596
- }
597
-
598
- if (cur + mlen > last_pos || (price < opt[cur + mlen].price))
599
- SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price);
600
-
601
- mlen++;
602
- } } }
603
-
604
- best_mlen = opt[last_pos].mlen;
605
- best_off = opt[last_pos].off;
606
- cur = last_pos - best_mlen;
607
-
608
- /* store sequence */
609
- _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */
610
- opt[0].mlen = 1;
611
-
612
- while (1) {
613
- mlen = opt[cur].mlen;
614
- offset = opt[cur].off;
615
- opt[cur].mlen = best_mlen;
616
- opt[cur].off = best_off;
617
- best_mlen = mlen;
618
- best_off = offset;
619
- if (mlen > cur) break;
620
- cur -= mlen;
621
- }
622
-
623
- for (u = 0; u <= last_pos;) {
624
- u += opt[u].mlen;
625
- }
626
-
627
- for (cur=0; cur < last_pos; ) {
628
- mlen = opt[cur].mlen;
629
- if (mlen == 1) { ip++; cur++; continue; }
630
- offset = opt[cur].off;
631
- cur += mlen;
632
- litLength = (U32)(ip - anchor);
633
-
634
- if (offset > ZSTD_REP_MOVE_OPT) {
635
- rep[2] = rep[1];
636
- rep[1] = rep[0];
637
- rep[0] = offset - ZSTD_REP_MOVE_OPT;
638
- offset--;
639
- } else {
640
- if (offset != 0) {
641
- best_off = (offset==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : (rep[offset]);
642
- if (offset != 1) rep[2] = rep[1];
643
- rep[1] = rep[0];
644
- rep[0] = best_off;
645
- }
646
- if (litLength==0) offset--;
647
- }
648
-
649
- ZSTD_updatePrice(optStatePtr, litLength, anchor, offset, mlen-MINMATCH);
650
- ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH);
651
- anchor = ip = ip + mlen;
652
- } } /* for (cur=0; cur < last_pos; ) */
653
-
654
- /* Save reps for next block */
655
- { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqStorePtr->repToConfirm[i] = rep[i]; }
656
-
657
- /* Last Literals */
658
- { size_t const lastLLSize = iend - anchor;
659
- memcpy(seqStorePtr->lit, anchor, lastLLSize);
660
- seqStorePtr->lit += lastLLSize;
661
- }
662
- }
663
-
664
-
665
- FORCE_INLINE_TEMPLATE
666
- void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx,
667
- const void* src, size_t srcSize, const int ultra)
668
- {
669
- seqStore_t* seqStorePtr = &(ctx->seqStore);
670
- optState_t* optStatePtr = &(ctx->optState);
671
- const BYTE* const istart = (const BYTE*)src;
672
- const BYTE* ip = istart;
673
- const BYTE* anchor = istart;
674
- const BYTE* const iend = istart + srcSize;
675
- const BYTE* const ilimit = iend - 8;
676
- const BYTE* const base = ctx->base;
677
- const U32 lowestIndex = ctx->lowLimit;
678
- const U32 dictLimit = ctx->dictLimit;
679
- const BYTE* const prefixStart = base + dictLimit;
680
- const BYTE* const dictBase = ctx->dictBase;
681
- const BYTE* const dictEnd = dictBase + dictLimit;
682
-
683
- const U32 maxSearches = 1U << ctx->appliedParams.cParams.searchLog;
684
- const U32 sufficient_len = ctx->appliedParams.cParams.targetLength;
685
- const U32 mls = ctx->appliedParams.cParams.searchLength;
686
- const U32 minMatch = (ctx->appliedParams.cParams.searchLength == 3) ? 3 : 4;
687
-
688
- ZSTD_optimal_t* opt = optStatePtr->priceTable;
689
- ZSTD_match_t* matches = optStatePtr->matchTable;
690
- const BYTE* inr;
691
-
692
- /* init */
693
- U32 offset, rep[ZSTD_REP_NUM];
694
- { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) rep[i]=seqStorePtr->rep[i]; }
695
-
696
- ctx->nextToUpdate3 = ctx->nextToUpdate;
697
- ZSTD_rescaleFreqs(optStatePtr, (const BYTE*)src, srcSize);
698
- ip += (ip==prefixStart);
699
-
700
- /* Match Loop */
701
- while (ip < ilimit) {
702
- U32 cur, match_num, last_pos, litlen, price;
703
- U32 u, mlen, best_mlen, best_off, litLength;
704
- U32 current = (U32)(ip-base);
705
- memset(opt, 0, sizeof(ZSTD_optimal_t));
706
- last_pos = 0;
707
- opt[0].litlen = (U32)(ip - anchor);
708
-
709
- /* check repCode */
710
- { U32 i, last_i = ZSTD_REP_CHECK + (ip==anchor);
711
- for (i = (ip==anchor); i<last_i; i++) {
712
- const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : rep[i];
713
- const U32 repIndex = (U32)(current - repCur);
714
- const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
715
- const BYTE* const repMatch = repBase + repIndex;
716
- if ( (repCur > 0 && repCur <= (S32)current)
717
- && (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex>lowestIndex)) /* intentional overflow */
718
- && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) ) {
719
- /* repcode detected we should take it */
720
- const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
721
- mlen = (U32)ZSTD_count_2segments(ip+minMatch, repMatch+minMatch, iend, repEnd, prefixStart) + minMatch;
722
-
723
- if (mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) {
724
- best_mlen = mlen; best_off = i; cur = 0; last_pos = 1;
725
- goto _storeSequence;
726
- }
727
-
728
- best_off = i - (ip==anchor);
729
- litlen = opt[0].litlen;
730
- do {
731
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
732
- if (mlen > last_pos || price < opt[mlen].price)
733
- SET_PRICE(mlen, mlen, i, litlen, price); /* note : macro modifies last_pos */
734
- mlen--;
735
- } while (mlen >= minMatch);
736
- } } }
737
-
738
- match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, ip, iend, maxSearches, mls, matches, minMatch); /* first search (depth 0) */
739
-
740
- if (!last_pos && !match_num) { ip++; continue; }
741
-
742
- { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) opt[0].rep[i] = rep[i]; }
743
- opt[0].mlen = 1;
744
-
745
- if (match_num && (matches[match_num-1].len > sufficient_len || matches[match_num-1].len >= ZSTD_OPT_NUM)) {
746
- best_mlen = matches[match_num-1].len;
747
- best_off = matches[match_num-1].off;
748
- cur = 0;
749
- last_pos = 1;
750
- goto _storeSequence;
751
- }
752
-
753
- best_mlen = (last_pos) ? last_pos : minMatch;
754
-
755
- /* set prices using matches at position = 0 */
756
- for (u = 0; u < match_num; u++) {
757
- mlen = (u>0) ? matches[u-1].len+1 : best_mlen;
758
- best_mlen = matches[u].len;
759
- litlen = opt[0].litlen;
760
- while (mlen <= best_mlen) {
761
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra);
762
- if (mlen > last_pos || price < opt[mlen].price)
763
- SET_PRICE(mlen, mlen, matches[u].off, litlen, price);
764
- mlen++;
765
- } }
766
-
767
- if (last_pos < minMatch) {
768
- ip++; continue;
769
- }
770
-
771
- /* check further positions */
772
- for (cur = 1; cur <= last_pos; cur++) {
773
- inr = ip + cur;
774
-
775
- if (opt[cur-1].mlen == 1) {
776
- litlen = opt[cur-1].litlen + 1;
777
- if (cur > litlen) {
778
- price = opt[cur - litlen].price + ZSTD_getLiteralPrice(optStatePtr, litlen, inr-litlen);
779
- } else
780
- price = ZSTD_getLiteralPrice(optStatePtr, litlen, anchor);
781
- } else {
782
- litlen = 1;
783
- price = opt[cur - 1].price + ZSTD_getLiteralPrice(optStatePtr, litlen, inr-1);
784
- }
785
-
786
- if (cur > last_pos || price <= opt[cur].price)
787
- SET_PRICE(cur, 1, 0, litlen, price);
788
-
789
- if (cur == last_pos) break;
790
-
791
- if (inr > ilimit) /* last match must start at a minimum distance of 8 from oend */
792
- continue;
793
-
794
- mlen = opt[cur].mlen;
795
- if (opt[cur].off > ZSTD_REP_MOVE_OPT) {
796
- opt[cur].rep[2] = opt[cur-mlen].rep[1];
797
- opt[cur].rep[1] = opt[cur-mlen].rep[0];
798
- opt[cur].rep[0] = opt[cur].off - ZSTD_REP_MOVE_OPT;
799
- } else {
800
- opt[cur].rep[2] = (opt[cur].off > 1) ? opt[cur-mlen].rep[1] : opt[cur-mlen].rep[2];
801
- opt[cur].rep[1] = (opt[cur].off > 0) ? opt[cur-mlen].rep[0] : opt[cur-mlen].rep[1];
802
- opt[cur].rep[0] = ((opt[cur].off==ZSTD_REP_MOVE_OPT) && (mlen != 1)) ? (opt[cur-mlen].rep[0] - 1) : (opt[cur-mlen].rep[opt[cur].off]);
803
- }
804
-
805
- best_mlen = minMatch;
806
- { U32 i, last_i = ZSTD_REP_CHECK + (mlen != 1);
807
- for (i = (mlen != 1); i<last_i; i++) {
808
- const S32 repCur = (i==ZSTD_REP_MOVE_OPT) ? (opt[cur].rep[0] - 1) : opt[cur].rep[i];
809
- const U32 repIndex = (U32)(current+cur - repCur);
810
- const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
811
- const BYTE* const repMatch = repBase + repIndex;
812
- if ( (repCur > 0 && repCur <= (S32)(current+cur))
813
- && (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex>lowestIndex)) /* intentional overflow */
814
- && (ZSTD_readMINMATCH(inr, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) ) {
815
- /* repcode detected */
816
- const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
817
- mlen = (U32)ZSTD_count_2segments(inr+minMatch, repMatch+minMatch, iend, repEnd, prefixStart) + minMatch;
818
-
819
- if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) {
820
- best_mlen = mlen; best_off = i; last_pos = cur + 1;
821
- goto _storeSequence;
822
- }
823
-
824
- best_off = i - (opt[cur].mlen != 1);
825
- if (mlen > best_mlen) best_mlen = mlen;
826
-
827
- do {
828
- if (opt[cur].mlen == 1) {
829
- litlen = opt[cur].litlen;
830
- if (cur > litlen) {
831
- price = opt[cur - litlen].price + ZSTD_getPrice(optStatePtr, litlen, inr-litlen, best_off, mlen - MINMATCH, ultra);
832
- } else
833
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
834
- } else {
835
- litlen = 0;
836
- price = opt[cur].price + ZSTD_getPrice(optStatePtr, 0, NULL, best_off, mlen - MINMATCH, ultra);
837
- }
838
-
839
- if (cur + mlen > last_pos || price <= opt[cur + mlen].price)
840
- SET_PRICE(cur + mlen, mlen, i, litlen, price);
841
- mlen--;
842
- } while (mlen >= minMatch);
843
- } } }
844
-
845
- match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, iend, maxSearches, mls, matches, minMatch);
846
-
847
- if (match_num > 0 && (matches[match_num-1].len > sufficient_len || cur + matches[match_num-1].len >= ZSTD_OPT_NUM)) {
848
- best_mlen = matches[match_num-1].len;
849
- best_off = matches[match_num-1].off;
850
- last_pos = cur + 1;
851
- goto _storeSequence;
852
- }
853
-
854
- /* set prices using matches at position = cur */
855
- for (u = 0; u < match_num; u++) {
856
- mlen = (u>0) ? matches[u-1].len+1 : best_mlen;
857
- best_mlen = matches[u].len;
858
-
859
- while (mlen <= best_mlen) {
860
- if (opt[cur].mlen == 1) {
861
- litlen = opt[cur].litlen;
862
- if (cur > litlen)
863
- price = opt[cur - litlen].price + ZSTD_getPrice(optStatePtr, litlen, ip+cur-litlen, matches[u].off-1, mlen - MINMATCH, ultra);
864
- else
865
- price = ZSTD_getPrice(optStatePtr, litlen, anchor, matches[u].off-1, mlen - MINMATCH, ultra);
866
- } else {
867
- litlen = 0;
868
- price = opt[cur].price + ZSTD_getPrice(optStatePtr, 0, NULL, matches[u].off-1, mlen - MINMATCH, ultra);
869
- }
870
-
871
- if (cur + mlen > last_pos || (price < opt[cur + mlen].price))
872
- SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price);
873
-
874
- mlen++;
875
- } } } /* for (cur = 1; cur <= last_pos; cur++) */
876
-
877
- best_mlen = opt[last_pos].mlen;
878
- best_off = opt[last_pos].off;
879
- cur = last_pos - best_mlen;
880
-
881
- /* store sequence */
882
- _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */
883
- opt[0].mlen = 1;
884
-
885
- while (1) {
886
- mlen = opt[cur].mlen;
887
- offset = opt[cur].off;
888
- opt[cur].mlen = best_mlen;
889
- opt[cur].off = best_off;
890
- best_mlen = mlen;
891
- best_off = offset;
892
- if (mlen > cur) break;
893
- cur -= mlen;
894
- }
895
-
896
- for (u = 0; u <= last_pos; ) {
897
- u += opt[u].mlen;
898
- }
899
-
900
- for (cur=0; cur < last_pos; ) {
901
- mlen = opt[cur].mlen;
902
- if (mlen == 1) { ip++; cur++; continue; }
903
- offset = opt[cur].off;
904
- cur += mlen;
905
- litLength = (U32)(ip - anchor);
906
-
907
- if (offset > ZSTD_REP_MOVE_OPT) {
908
- rep[2] = rep[1];
909
- rep[1] = rep[0];
910
- rep[0] = offset - ZSTD_REP_MOVE_OPT;
911
- offset--;
912
- } else {
913
- if (offset != 0) {
914
- best_off = (offset==ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : (rep[offset]);
915
- if (offset != 1) rep[2] = rep[1];
916
- rep[1] = rep[0];
917
- rep[0] = best_off;
918
- }
919
-
920
- if (litLength==0) offset--;
921
- }
922
-
923
- ZSTD_updatePrice(optStatePtr, litLength, anchor, offset, mlen-MINMATCH);
924
- ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH);
925
- anchor = ip = ip + mlen;
926
- } } /* for (cur=0; cur < last_pos; ) */
927
-
928
- /* Save reps for next block */
929
- { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqStorePtr->repToConfirm[i] = rep[i]; }
930
-
931
- /* Last Literals */
932
- { size_t lastLLSize = iend - anchor;
933
- memcpy(seqStorePtr->lit, anchor, lastLLSize);
934
- seqStorePtr->lit += lastLLSize;
935
- }
26
+ #if defined (__cplusplus)
936
27
  }
28
+ #endif
937
29
 
938
- #endif /* ZSTD_OPT_H_91842398743 */
30
+ #endif /* ZSTD_OPT_H */