zstd-ruby 1.5.0.0 → 1.5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +2 -2
  3. data/README.md +1 -1
  4. data/ext/zstdruby/extconf.rb +2 -1
  5. data/ext/zstdruby/libzstd/Makefile +50 -175
  6. data/ext/zstdruby/libzstd/README.md +7 -1
  7. data/ext/zstdruby/libzstd/common/bitstream.h +24 -9
  8. data/ext/zstdruby/libzstd/common/compiler.h +89 -43
  9. data/ext/zstdruby/libzstd/common/entropy_common.c +11 -5
  10. data/ext/zstdruby/libzstd/common/error_private.h +79 -0
  11. data/ext/zstdruby/libzstd/common/fse.h +2 -1
  12. data/ext/zstdruby/libzstd/common/fse_decompress.c +1 -1
  13. data/ext/zstdruby/libzstd/common/huf.h +24 -22
  14. data/ext/zstdruby/libzstd/common/mem.h +18 -0
  15. data/ext/zstdruby/libzstd/common/pool.c +11 -6
  16. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  17. data/ext/zstdruby/libzstd/common/portability_macros.h +137 -0
  18. data/ext/zstdruby/libzstd/common/xxhash.c +5 -805
  19. data/ext/zstdruby/libzstd/common/xxhash.h +5568 -167
  20. data/ext/zstdruby/libzstd/common/zstd_internal.h +95 -92
  21. data/ext/zstdruby/libzstd/common/zstd_trace.h +12 -3
  22. data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
  23. data/ext/zstdruby/libzstd/compress/fse_compress.c +63 -27
  24. data/ext/zstdruby/libzstd/compress/huf_compress.c +537 -104
  25. data/ext/zstdruby/libzstd/compress/zstd_compress.c +307 -373
  26. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +174 -83
  27. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +4 -3
  28. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +3 -1
  29. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +15 -14
  30. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +4 -3
  31. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +41 -27
  32. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +295 -120
  33. data/ext/zstdruby/libzstd/compress/zstd_fast.c +309 -130
  34. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +482 -562
  35. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +9 -7
  36. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +1 -1
  37. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +4 -1
  38. data/ext/zstdruby/libzstd/compress/zstd_opt.c +249 -148
  39. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +76 -38
  40. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +4 -1
  41. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +727 -189
  42. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +585 -0
  43. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +85 -22
  44. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +744 -220
  45. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +8 -2
  46. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +34 -3
  47. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +23 -3
  48. data/ext/zstdruby/libzstd/dictBuilder/cover.c +9 -2
  49. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +11 -4
  50. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +101 -30
  51. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +2 -6
  52. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +3 -7
  53. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +3 -7
  54. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +3 -7
  55. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +3 -7
  56. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +3 -7
  57. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +3 -7
  58. data/ext/zstdruby/libzstd/libzstd.mk +203 -0
  59. data/ext/zstdruby/libzstd/libzstd.pc.in +1 -0
  60. data/ext/zstdruby/libzstd/module.modulemap +25 -0
  61. data/ext/zstdruby/libzstd/zdict.h +4 -4
  62. data/ext/zstdruby/libzstd/zstd.h +179 -136
  63. data/ext/zstdruby/zstdruby.c +2 -2
  64. data/lib/zstd-ruby/version.rb +1 -1
  65. metadata +11 -6
@@ -14,7 +14,6 @@
14
14
 
15
15
 
16
16
  #define ZSTD_LITFREQ_ADD 2 /* scaling factor for litFreq, so that frequencies adapt faster to new stats */
17
- #define ZSTD_FREQ_DIV 4 /* log factor when using previous stats to init next stats */
18
17
  #define ZSTD_MAX_PRICE (1<<30)
19
18
 
20
19
  #define ZSTD_PREDEF_THRESHOLD 1024 /* if srcSize < ZSTD_PREDEF_THRESHOLD, symbols' cost is assumed static, directly determined by pre-defined distributions */
@@ -24,11 +23,11 @@
24
23
  * Price functions for optimal parser
25
24
  ***************************************/
26
25
 
27
- #if 0 /* approximation at bit level */
26
+ #if 0 /* approximation at bit level (for tests) */
28
27
  # define BITCOST_ACCURACY 0
29
28
  # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY)
30
- # define WEIGHT(stat) ((void)opt, ZSTD_bitWeight(stat))
31
- #elif 0 /* fractional bit accuracy */
29
+ # define WEIGHT(stat, opt) ((void)opt, ZSTD_bitWeight(stat))
30
+ #elif 0 /* fractional bit accuracy (for tests) */
32
31
  # define BITCOST_ACCURACY 8
33
32
  # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY)
34
33
  # define WEIGHT(stat,opt) ((void)opt, ZSTD_fracWeight(stat))
@@ -66,7 +65,7 @@ MEM_STATIC double ZSTD_fCost(U32 price)
66
65
 
67
66
  static int ZSTD_compressedLiterals(optState_t const* const optPtr)
68
67
  {
69
- return optPtr->literalCompressionMode != ZSTD_lcm_uncompressed;
68
+ return optPtr->literalCompressionMode != ZSTD_ps_disable;
70
69
  }
71
70
 
72
71
  static void ZSTD_setBasePrices(optState_t* optPtr, int optLevel)
@@ -79,25 +78,46 @@ static void ZSTD_setBasePrices(optState_t* optPtr, int optLevel)
79
78
  }
80
79
 
81
80
 
82
- /* ZSTD_downscaleStat() :
83
- * reduce all elements in table by a factor 2^(ZSTD_FREQ_DIV+malus)
84
- * return the resulting sum of elements */
85
- static U32 ZSTD_downscaleStat(unsigned* table, U32 lastEltIndex, int malus)
81
+ static U32 sum_u32(const unsigned table[], size_t nbElts)
82
+ {
83
+ size_t n;
84
+ U32 total = 0;
85
+ for (n=0; n<nbElts; n++) {
86
+ total += table[n];
87
+ }
88
+ return total;
89
+ }
90
+
91
+ static U32 ZSTD_downscaleStats(unsigned* table, U32 lastEltIndex, U32 shift)
86
92
  {
87
93
  U32 s, sum=0;
88
- DEBUGLOG(5, "ZSTD_downscaleStat (nbElts=%u)", (unsigned)lastEltIndex+1);
89
- assert(ZSTD_FREQ_DIV+malus > 0 && ZSTD_FREQ_DIV+malus < 31);
94
+ DEBUGLOG(5, "ZSTD_downscaleStats (nbElts=%u, shift=%u)", (unsigned)lastEltIndex+1, (unsigned)shift);
95
+ assert(shift < 30);
90
96
  for (s=0; s<lastEltIndex+1; s++) {
91
- table[s] = 1 + (table[s] >> (ZSTD_FREQ_DIV+malus));
97
+ table[s] = 1 + (table[s] >> shift);
92
98
  sum += table[s];
93
99
  }
94
100
  return sum;
95
101
  }
96
102
 
103
+ /* ZSTD_scaleStats() :
104
+ * reduce all elements in table is sum too large
105
+ * return the resulting sum of elements */
106
+ static U32 ZSTD_scaleStats(unsigned* table, U32 lastEltIndex, U32 logTarget)
107
+ {
108
+ U32 const prevsum = sum_u32(table, lastEltIndex+1);
109
+ U32 const factor = prevsum >> logTarget;
110
+ DEBUGLOG(5, "ZSTD_scaleStats (nbElts=%u, target=%u)", (unsigned)lastEltIndex+1, (unsigned)logTarget);
111
+ assert(logTarget < 30);
112
+ if (factor <= 1) return prevsum;
113
+ return ZSTD_downscaleStats(table, lastEltIndex, ZSTD_highbit32(factor));
114
+ }
115
+
97
116
  /* ZSTD_rescaleFreqs() :
98
117
  * if first block (detected by optPtr->litLengthSum == 0) : init statistics
99
118
  * take hints from dictionary if there is one
100
- * or init from zero, using src for literals stats, or flat 1 for match symbols
119
+ * and init from zero if there is none,
120
+ * using src for literals stats, and baseline stats for sequence symbols
101
121
  * otherwise downscale existing stats, to be used as seed for next block.
102
122
  */
103
123
  static void
@@ -126,7 +146,7 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
126
146
  optPtr->litSum = 0;
127
147
  for (lit=0; lit<=MaxLit; lit++) {
128
148
  U32 const scaleLog = 11; /* scale to 2K */
129
- U32 const bitCost = HUF_getNbBits(optPtr->symbolCosts->huf.CTable, lit);
149
+ U32 const bitCost = HUF_getNbBitsFromCTable(optPtr->symbolCosts->huf.CTable, lit);
130
150
  assert(bitCost <= scaleLog);
131
151
  optPtr->litFreq[lit] = bitCost ? 1 << (scaleLog-bitCost) : 1 /*minimum to calculate cost*/;
132
152
  optPtr->litSum += optPtr->litFreq[lit];
@@ -174,14 +194,19 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
174
194
  if (compressedLiterals) {
175
195
  unsigned lit = MaxLit;
176
196
  HIST_count_simple(optPtr->litFreq, &lit, src, srcSize); /* use raw first block to init statistics */
177
- optPtr->litSum = ZSTD_downscaleStat(optPtr->litFreq, MaxLit, 1);
197
+ optPtr->litSum = ZSTD_downscaleStats(optPtr->litFreq, MaxLit, 8);
178
198
  }
179
199
 
180
- { unsigned ll;
181
- for (ll=0; ll<=MaxLL; ll++)
182
- optPtr->litLengthFreq[ll] = 1;
200
+ { unsigned const baseLLfreqs[MaxLL+1] = {
201
+ 4, 2, 1, 1, 1, 1, 1, 1,
202
+ 1, 1, 1, 1, 1, 1, 1, 1,
203
+ 1, 1, 1, 1, 1, 1, 1, 1,
204
+ 1, 1, 1, 1, 1, 1, 1, 1,
205
+ 1, 1, 1, 1
206
+ };
207
+ ZSTD_memcpy(optPtr->litLengthFreq, baseLLfreqs, sizeof(baseLLfreqs));
208
+ optPtr->litLengthSum = sum_u32(baseLLfreqs, MaxLL+1);
183
209
  }
184
- optPtr->litLengthSum = MaxLL+1;
185
210
 
186
211
  { unsigned ml;
187
212
  for (ml=0; ml<=MaxML; ml++)
@@ -189,21 +214,26 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
189
214
  }
190
215
  optPtr->matchLengthSum = MaxML+1;
191
216
 
192
- { unsigned of;
193
- for (of=0; of<=MaxOff; of++)
194
- optPtr->offCodeFreq[of] = 1;
217
+ { unsigned const baseOFCfreqs[MaxOff+1] = {
218
+ 6, 2, 1, 1, 2, 3, 4, 4,
219
+ 4, 3, 2, 1, 1, 1, 1, 1,
220
+ 1, 1, 1, 1, 1, 1, 1, 1,
221
+ 1, 1, 1, 1, 1, 1, 1, 1
222
+ };
223
+ ZSTD_memcpy(optPtr->offCodeFreq, baseOFCfreqs, sizeof(baseOFCfreqs));
224
+ optPtr->offCodeSum = sum_u32(baseOFCfreqs, MaxOff+1);
195
225
  }
196
- optPtr->offCodeSum = MaxOff+1;
226
+
197
227
 
198
228
  }
199
229
 
200
230
  } else { /* new block : re-use previous statistics, scaled down */
201
231
 
202
232
  if (compressedLiterals)
203
- optPtr->litSum = ZSTD_downscaleStat(optPtr->litFreq, MaxLit, 1);
204
- optPtr->litLengthSum = ZSTD_downscaleStat(optPtr->litLengthFreq, MaxLL, 0);
205
- optPtr->matchLengthSum = ZSTD_downscaleStat(optPtr->matchLengthFreq, MaxML, 0);
206
- optPtr->offCodeSum = ZSTD_downscaleStat(optPtr->offCodeFreq, MaxOff, 0);
233
+ optPtr->litSum = ZSTD_scaleStats(optPtr->litFreq, MaxLit, 12);
234
+ optPtr->litLengthSum = ZSTD_scaleStats(optPtr->litLengthFreq, MaxLL, 11);
235
+ optPtr->matchLengthSum = ZSTD_scaleStats(optPtr->matchLengthFreq, MaxML, 11);
236
+ optPtr->offCodeSum = ZSTD_scaleStats(optPtr->offCodeFreq, MaxOff, 11);
207
237
  }
208
238
 
209
239
  ZSTD_setBasePrices(optPtr, optLevel);
@@ -239,7 +269,16 @@ static U32 ZSTD_rawLiteralsCost(const BYTE* const literals, U32 const litLength,
239
269
  * cost of literalLength symbol */
240
270
  static U32 ZSTD_litLengthPrice(U32 const litLength, const optState_t* const optPtr, int optLevel)
241
271
  {
242
- if (optPtr->priceType == zop_predef) return WEIGHT(litLength, optLevel);
272
+ assert(litLength <= ZSTD_BLOCKSIZE_MAX);
273
+ if (optPtr->priceType == zop_predef)
274
+ return WEIGHT(litLength, optLevel);
275
+ /* We can't compute the litLength price for sizes >= ZSTD_BLOCKSIZE_MAX
276
+ * because it isn't representable in the zstd format. So instead just
277
+ * call it 1 bit more than ZSTD_BLOCKSIZE_MAX - 1. In this case the block
278
+ * would be all literals.
279
+ */
280
+ if (litLength == ZSTD_BLOCKSIZE_MAX)
281
+ return BITCOST_MULTIPLIER + ZSTD_litLengthPrice(ZSTD_BLOCKSIZE_MAX - 1, optPtr, optLevel);
243
282
 
244
283
  /* dynamic statistics */
245
284
  { U32 const llCode = ZSTD_LLcode(litLength);
@@ -252,15 +291,17 @@ static U32 ZSTD_litLengthPrice(U32 const litLength, const optState_t* const optP
252
291
  /* ZSTD_getMatchPrice() :
253
292
  * Provides the cost of the match part (offset + matchLength) of a sequence
254
293
  * Must be combined with ZSTD_fullLiteralsCost() to get the full cost of a sequence.
255
- * optLevel: when <2, favors small offset for decompression speed (improved cache efficiency) */
294
+ * @offcode : expects a scale where 0,1,2 are repcodes 1-3, and 3+ are real_offsets+2
295
+ * @optLevel: when <2, favors small offset for decompression speed (improved cache efficiency)
296
+ */
256
297
  FORCE_INLINE_TEMPLATE U32
257
- ZSTD_getMatchPrice(U32 const offset,
298
+ ZSTD_getMatchPrice(U32 const offcode,
258
299
  U32 const matchLength,
259
300
  const optState_t* const optPtr,
260
301
  int const optLevel)
261
302
  {
262
303
  U32 price;
263
- U32 const offCode = ZSTD_highbit32(offset+1);
304
+ U32 const offCode = ZSTD_highbit32(STORED_TO_OFFBASE(offcode));
264
305
  U32 const mlBase = matchLength - MINMATCH;
265
306
  assert(matchLength >= MINMATCH);
266
307
 
@@ -303,8 +344,8 @@ static void ZSTD_updateStats(optState_t* const optPtr,
303
344
  optPtr->litLengthSum++;
304
345
  }
305
346
 
306
- /* match offset code (0-2=>repCode; 3+=>offset+2) */
307
- { U32 const offCode = ZSTD_highbit32(offsetCode+1);
347
+ /* offset code : expected to follow storeSeq() numeric representation */
348
+ { U32 const offCode = ZSTD_highbit32(STORED_TO_OFFBASE(offsetCode));
308
349
  assert(offCode <= MaxOff);
309
350
  optPtr->offCodeFreq[offCode]++;
310
351
  optPtr->offCodeSum++;
@@ -338,7 +379,7 @@ MEM_STATIC U32 ZSTD_readMINMATCH(const void* memPtr, U32 length)
338
379
 
339
380
  /* Update hashTable3 up to ip (excluded)
340
381
  Assumption : always within prefix (i.e. not within extDict) */
341
- static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_matchState_t* ms,
382
+ static U32 ZSTD_insertAndFindFirstIndexHash3 (const ZSTD_matchState_t* ms,
342
383
  U32* nextToUpdate3,
343
384
  const BYTE* const ip)
344
385
  {
@@ -364,11 +405,13 @@ static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_matchState_t* ms,
364
405
  * Binary Tree search
365
406
  ***************************************/
366
407
  /** ZSTD_insertBt1() : add one or multiple positions to tree.
367
- * ip : assumed <= iend-8 .
408
+ * @param ip assumed <= iend-8 .
409
+ * @param target The target of ZSTD_updateTree_internal() - we are filling to this position
368
410
  * @return : nb of positions added */
369
411
  static U32 ZSTD_insertBt1(
370
- ZSTD_matchState_t* ms,
412
+ const ZSTD_matchState_t* ms,
371
413
  const BYTE* const ip, const BYTE* const iend,
414
+ U32 const target,
372
415
  U32 const mls, const int extDict)
373
416
  {
374
417
  const ZSTD_compressionParameters* const cParams = &ms->cParams;
@@ -391,7 +434,10 @@ static U32 ZSTD_insertBt1(
391
434
  U32* smallerPtr = bt + 2*(curr&btMask);
392
435
  U32* largerPtr = smallerPtr + 1;
393
436
  U32 dummy32; /* to be nullified at the end */
394
- U32 const windowLow = ms->window.lowLimit;
437
+ /* windowLow is based on target because
438
+ * we only need positions that will be in the window at the end of the tree update.
439
+ */
440
+ U32 const windowLow = ZSTD_getLowestMatchIndex(ms, target, cParams->windowLog);
395
441
  U32 matchEndIdx = curr+8+1;
396
442
  size_t bestLength = 8;
397
443
  U32 nbCompares = 1U << cParams->searchLog;
@@ -404,11 +450,12 @@ static U32 ZSTD_insertBt1(
404
450
 
405
451
  DEBUGLOG(8, "ZSTD_insertBt1 (%u)", curr);
406
452
 
453
+ assert(curr <= target);
407
454
  assert(ip <= iend-8); /* required for h calculation */
408
455
  hashTable[h] = curr; /* Update Hash Table */
409
456
 
410
457
  assert(windowLow > 0);
411
- while (nbCompares-- && (matchIndex >= windowLow)) {
458
+ for (; nbCompares && (matchIndex >= windowLow); --nbCompares) {
412
459
  U32* const nextPtr = bt + 2*(matchIndex & btMask);
413
460
  size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
414
461
  assert(matchIndex < curr);
@@ -492,7 +539,7 @@ void ZSTD_updateTree_internal(
492
539
  idx, target, dictMode);
493
540
 
494
541
  while(idx < target) {
495
- U32 const forward = ZSTD_insertBt1(ms, base+idx, iend, mls, dictMode == ZSTD_extDict);
542
+ U32 const forward = ZSTD_insertBt1(ms, base+idx, iend, target, mls, dictMode == ZSTD_extDict);
496
543
  assert(idx < (U32)(idx + forward));
497
544
  idx += forward;
498
545
  }
@@ -597,7 +644,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
597
644
  DEBUGLOG(8, "found repCode %u (ll0:%u, offset:%u) of length %u",
598
645
  repCode, ll0, repOffset, repLen);
599
646
  bestLength = repLen;
600
- matches[mnum].off = repCode - ll0;
647
+ matches[mnum].off = STORE_REPCODE(repCode - ll0 + 1); /* expect value between 1 and 3 */
601
648
  matches[mnum].len = (U32)repLen;
602
649
  mnum++;
603
650
  if ( (repLen > sufficient_len)
@@ -626,7 +673,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
626
673
  bestLength = mlen;
627
674
  assert(curr > matchIndex3);
628
675
  assert(mnum==0); /* no prior solution */
629
- matches[0].off = (curr - matchIndex3) + ZSTD_REP_MOVE;
676
+ matches[0].off = STORE_OFFSET(curr - matchIndex3);
630
677
  matches[0].len = (U32)mlen;
631
678
  mnum = 1;
632
679
  if ( (mlen > sufficient_len) |
@@ -635,11 +682,11 @@ U32 ZSTD_insertBtAndGetAllMatches (
635
682
  return 1;
636
683
  } } }
637
684
  /* no dictMatchState lookup: dicts don't have a populated HC3 table */
638
- }
685
+ } /* if (mls == 3) */
639
686
 
640
687
  hashTable[h] = curr; /* Update Hash Table */
641
688
 
642
- while (nbCompares-- && (matchIndex >= matchLow)) {
689
+ for (; nbCompares && (matchIndex >= matchLow); --nbCompares) {
643
690
  U32* const nextPtr = bt + 2*(matchIndex & btMask);
644
691
  const BYTE* match;
645
692
  size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
@@ -660,20 +707,19 @@ U32 ZSTD_insertBtAndGetAllMatches (
660
707
 
661
708
  if (matchLength > bestLength) {
662
709
  DEBUGLOG(8, "found match of length %u at distance %u (offCode=%u)",
663
- (U32)matchLength, curr - matchIndex, curr - matchIndex + ZSTD_REP_MOVE);
710
+ (U32)matchLength, curr - matchIndex, STORE_OFFSET(curr - matchIndex));
664
711
  assert(matchEndIdx > matchIndex);
665
712
  if (matchLength > matchEndIdx - matchIndex)
666
713
  matchEndIdx = matchIndex + (U32)matchLength;
667
714
  bestLength = matchLength;
668
- matches[mnum].off = (curr - matchIndex) + ZSTD_REP_MOVE;
715
+ matches[mnum].off = STORE_OFFSET(curr - matchIndex);
669
716
  matches[mnum].len = (U32)matchLength;
670
717
  mnum++;
671
718
  if ( (matchLength > ZSTD_OPT_NUM)
672
719
  | (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) {
673
720
  if (dictMode == ZSTD_dictMatchState) nbCompares = 0; /* break should also skip searching dms */
674
721
  break; /* drop, to preserve bt consistency (miss a little bit of compression) */
675
- }
676
- }
722
+ } }
677
723
 
678
724
  if (match[matchLength] < ip[matchLength]) {
679
725
  /* match smaller than current */
@@ -692,12 +738,13 @@ U32 ZSTD_insertBtAndGetAllMatches (
692
738
 
693
739
  *smallerPtr = *largerPtr = 0;
694
740
 
741
+ assert(nbCompares <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
695
742
  if (dictMode == ZSTD_dictMatchState && nbCompares) {
696
743
  size_t const dmsH = ZSTD_hashPtr(ip, dmsHashLog, mls);
697
744
  U32 dictMatchIndex = dms->hashTable[dmsH];
698
745
  const U32* const dmsBt = dms->chainTable;
699
746
  commonLengthSmaller = commonLengthLarger = 0;
700
- while (nbCompares-- && (dictMatchIndex > dmsLowLimit)) {
747
+ for (; nbCompares && (dictMatchIndex > dmsLowLimit); --nbCompares) {
701
748
  const U32* const nextPtr = dmsBt + 2*(dictMatchIndex & dmsBtMask);
702
749
  size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
703
750
  const BYTE* match = dmsBase + dictMatchIndex;
@@ -708,18 +755,17 @@ U32 ZSTD_insertBtAndGetAllMatches (
708
755
  if (matchLength > bestLength) {
709
756
  matchIndex = dictMatchIndex + dmsIndexDelta;
710
757
  DEBUGLOG(8, "found dms match of length %u at distance %u (offCode=%u)",
711
- (U32)matchLength, curr - matchIndex, curr - matchIndex + ZSTD_REP_MOVE);
758
+ (U32)matchLength, curr - matchIndex, STORE_OFFSET(curr - matchIndex));
712
759
  if (matchLength > matchEndIdx - matchIndex)
713
760
  matchEndIdx = matchIndex + (U32)matchLength;
714
761
  bestLength = matchLength;
715
- matches[mnum].off = (curr - matchIndex) + ZSTD_REP_MOVE;
762
+ matches[mnum].off = STORE_OFFSET(curr - matchIndex);
716
763
  matches[mnum].len = (U32)matchLength;
717
764
  mnum++;
718
765
  if ( (matchLength > ZSTD_OPT_NUM)
719
766
  | (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) {
720
767
  break; /* drop, to guarantee consistency (miss a little bit of compression) */
721
- }
722
- }
768
+ } }
723
769
 
724
770
  if (dictMatchIndex <= dmsBtLow) { break; } /* beyond tree size, stop the search */
725
771
  if (match[matchLength] < ip[matchLength]) {
@@ -729,39 +775,91 @@ U32 ZSTD_insertBtAndGetAllMatches (
729
775
  /* match is larger than current */
730
776
  commonLengthLarger = matchLength;
731
777
  dictMatchIndex = nextPtr[0];
732
- }
733
- }
734
- }
778
+ } } } /* if (dictMode == ZSTD_dictMatchState) */
735
779
 
736
780
  assert(matchEndIdx > curr+8);
737
781
  ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */
738
782
  return mnum;
739
783
  }
740
784
 
741
-
742
- FORCE_INLINE_TEMPLATE U32 ZSTD_BtGetAllMatches (
743
- ZSTD_match_t* matches, /* store result (match found, increasing size) in this table */
744
- ZSTD_matchState_t* ms,
745
- U32* nextToUpdate3,
746
- const BYTE* ip, const BYTE* const iHighLimit, const ZSTD_dictMode_e dictMode,
747
- const U32 rep[ZSTD_REP_NUM],
748
- U32 const ll0,
749
- U32 const lengthToBeat)
785
+ typedef U32 (*ZSTD_getAllMatchesFn)(
786
+ ZSTD_match_t*,
787
+ ZSTD_matchState_t*,
788
+ U32*,
789
+ const BYTE*,
790
+ const BYTE*,
791
+ const U32 rep[ZSTD_REP_NUM],
792
+ U32 const ll0,
793
+ U32 const lengthToBeat);
794
+
795
+ FORCE_INLINE_TEMPLATE U32 ZSTD_btGetAllMatches_internal(
796
+ ZSTD_match_t* matches,
797
+ ZSTD_matchState_t* ms,
798
+ U32* nextToUpdate3,
799
+ const BYTE* ip,
800
+ const BYTE* const iHighLimit,
801
+ const U32 rep[ZSTD_REP_NUM],
802
+ U32 const ll0,
803
+ U32 const lengthToBeat,
804
+ const ZSTD_dictMode_e dictMode,
805
+ const U32 mls)
750
806
  {
751
- const ZSTD_compressionParameters* const cParams = &ms->cParams;
752
- U32 const matchLengthSearch = cParams->minMatch;
753
- DEBUGLOG(8, "ZSTD_BtGetAllMatches");
754
- if (ip < ms->window.base + ms->nextToUpdate) return 0; /* skipped area */
755
- ZSTD_updateTree_internal(ms, ip, iHighLimit, matchLengthSearch, dictMode);
756
- switch(matchLengthSearch)
757
- {
758
- case 3 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 3);
759
- default :
760
- case 4 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 4);
761
- case 5 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 5);
762
- case 7 :
763
- case 6 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 6);
807
+ assert(BOUNDED(3, ms->cParams.minMatch, 6) == mls);
808
+ DEBUGLOG(8, "ZSTD_BtGetAllMatches(dictMode=%d, mls=%u)", (int)dictMode, mls);
809
+ if (ip < ms->window.base + ms->nextToUpdate)
810
+ return 0; /* skipped area */
811
+ ZSTD_updateTree_internal(ms, ip, iHighLimit, mls, dictMode);
812
+ return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, mls);
813
+ }
814
+
815
+ #define ZSTD_BT_GET_ALL_MATCHES_FN(dictMode, mls) ZSTD_btGetAllMatches_##dictMode##_##mls
816
+
817
+ #define GEN_ZSTD_BT_GET_ALL_MATCHES_(dictMode, mls) \
818
+ static U32 ZSTD_BT_GET_ALL_MATCHES_FN(dictMode, mls)( \
819
+ ZSTD_match_t* matches, \
820
+ ZSTD_matchState_t* ms, \
821
+ U32* nextToUpdate3, \
822
+ const BYTE* ip, \
823
+ const BYTE* const iHighLimit, \
824
+ const U32 rep[ZSTD_REP_NUM], \
825
+ U32 const ll0, \
826
+ U32 const lengthToBeat) \
827
+ { \
828
+ return ZSTD_btGetAllMatches_internal( \
829
+ matches, ms, nextToUpdate3, ip, iHighLimit, \
830
+ rep, ll0, lengthToBeat, ZSTD_##dictMode, mls); \
831
+ }
832
+
833
+ #define GEN_ZSTD_BT_GET_ALL_MATCHES(dictMode) \
834
+ GEN_ZSTD_BT_GET_ALL_MATCHES_(dictMode, 3) \
835
+ GEN_ZSTD_BT_GET_ALL_MATCHES_(dictMode, 4) \
836
+ GEN_ZSTD_BT_GET_ALL_MATCHES_(dictMode, 5) \
837
+ GEN_ZSTD_BT_GET_ALL_MATCHES_(dictMode, 6)
838
+
839
+ GEN_ZSTD_BT_GET_ALL_MATCHES(noDict)
840
+ GEN_ZSTD_BT_GET_ALL_MATCHES(extDict)
841
+ GEN_ZSTD_BT_GET_ALL_MATCHES(dictMatchState)
842
+
843
+ #define ZSTD_BT_GET_ALL_MATCHES_ARRAY(dictMode) \
844
+ { \
845
+ ZSTD_BT_GET_ALL_MATCHES_FN(dictMode, 3), \
846
+ ZSTD_BT_GET_ALL_MATCHES_FN(dictMode, 4), \
847
+ ZSTD_BT_GET_ALL_MATCHES_FN(dictMode, 5), \
848
+ ZSTD_BT_GET_ALL_MATCHES_FN(dictMode, 6) \
764
849
  }
850
+
851
+ static ZSTD_getAllMatchesFn
852
+ ZSTD_selectBtGetAllMatches(ZSTD_matchState_t const* ms, ZSTD_dictMode_e const dictMode)
853
+ {
854
+ ZSTD_getAllMatchesFn const getAllMatchesFns[3][4] = {
855
+ ZSTD_BT_GET_ALL_MATCHES_ARRAY(noDict),
856
+ ZSTD_BT_GET_ALL_MATCHES_ARRAY(extDict),
857
+ ZSTD_BT_GET_ALL_MATCHES_ARRAY(dictMatchState)
858
+ };
859
+ U32 const mls = BOUNDED(3, ms->cParams.minMatch, 6);
860
+ assert((U32)dictMode < 3);
861
+ assert(mls - 3 < 4);
862
+ return getAllMatchesFns[(int)dictMode][mls - 3];
765
863
  }
766
864
 
767
865
  /*************************
@@ -770,16 +868,18 @@ FORCE_INLINE_TEMPLATE U32 ZSTD_BtGetAllMatches (
770
868
 
771
869
  /* Struct containing info needed to make decision about ldm inclusion */
772
870
  typedef struct {
773
- rawSeqStore_t seqStore; /* External match candidates store for this block */
774
- U32 startPosInBlock; /* Start position of the current match candidate */
775
- U32 endPosInBlock; /* End position of the current match candidate */
776
- U32 offset; /* Offset of the match candidate */
871
+ rawSeqStore_t seqStore; /* External match candidates store for this block */
872
+ U32 startPosInBlock; /* Start position of the current match candidate */
873
+ U32 endPosInBlock; /* End position of the current match candidate */
874
+ U32 offset; /* Offset of the match candidate */
777
875
  } ZSTD_optLdm_t;
778
876
 
779
877
  /* ZSTD_optLdm_skipRawSeqStoreBytes():
780
- * Moves forward in rawSeqStore by nbBytes, which will update the fields 'pos' and 'posInSequence'.
878
+ * Moves forward in @rawSeqStore by @nbBytes,
879
+ * which will update the fields 'pos' and 'posInSequence'.
781
880
  */
782
- static void ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes) {
881
+ static void ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes)
882
+ {
783
883
  U32 currPos = (U32)(rawSeqStore->posInSequence + nbBytes);
784
884
  while (currPos && rawSeqStore->pos < rawSeqStore->size) {
785
885
  rawSeq currSeq = rawSeqStore->seq[rawSeqStore->pos];
@@ -800,8 +900,10 @@ static void ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t
800
900
  * Calculates the beginning and end of the next match in the current block.
801
901
  * Updates 'pos' and 'posInSequence' of the ldmSeqStore.
802
902
  */
803
- static void ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 currPosInBlock,
804
- U32 blockBytesRemaining) {
903
+ static void
904
+ ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 currPosInBlock,
905
+ U32 blockBytesRemaining)
906
+ {
805
907
  rawSeq currSeq;
806
908
  U32 currBlockEndPos;
807
909
  U32 literalsBytesRemaining;
@@ -813,8 +915,8 @@ static void ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 cu
813
915
  optLdm->endPosInBlock = UINT_MAX;
814
916
  return;
815
917
  }
816
- /* Calculate appropriate bytes left in matchLength and litLength after adjusting
817
- based on ldmSeqStore->posInSequence */
918
+ /* Calculate appropriate bytes left in matchLength and litLength
919
+ * after adjusting based on ldmSeqStore->posInSequence */
818
920
  currSeq = optLdm->seqStore.seq[optLdm->seqStore.pos];
819
921
  assert(optLdm->seqStore.posInSequence <= currSeq.litLength + currSeq.matchLength);
820
922
  currBlockEndPos = currPosInBlock + blockBytesRemaining;
@@ -850,15 +952,16 @@ static void ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 cu
850
952
  }
851
953
 
852
954
  /* ZSTD_optLdm_maybeAddMatch():
853
- * Adds a match if it's long enough, based on it's 'matchStartPosInBlock'
854
- * and 'matchEndPosInBlock', into 'matches'. Maintains the correct ordering of 'matches'
955
+ * Adds a match if it's long enough,
956
+ * based on it's 'matchStartPosInBlock' and 'matchEndPosInBlock',
957
+ * into 'matches'. Maintains the correct ordering of 'matches'.
855
958
  */
856
959
  static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
857
- ZSTD_optLdm_t* optLdm, U32 currPosInBlock) {
858
- U32 posDiff = currPosInBlock - optLdm->startPosInBlock;
960
+ const ZSTD_optLdm_t* optLdm, U32 currPosInBlock)
961
+ {
962
+ U32 const posDiff = currPosInBlock - optLdm->startPosInBlock;
859
963
  /* Note: ZSTD_match_t actually contains offCode and matchLength (before subtracting MINMATCH) */
860
- U32 candidateMatchLength = optLdm->endPosInBlock - optLdm->startPosInBlock - posDiff;
861
- U32 candidateOffCode = optLdm->offset + ZSTD_REP_MOVE;
964
+ U32 const candidateMatchLength = optLdm->endPosInBlock - optLdm->startPosInBlock - posDiff;
862
965
 
863
966
  /* Ensure that current block position is not outside of the match */
864
967
  if (currPosInBlock < optLdm->startPosInBlock
@@ -868,6 +971,7 @@ static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
868
971
  }
869
972
 
870
973
  if (*nbMatches == 0 || ((candidateMatchLength > matches[*nbMatches-1].len) && *nbMatches < ZSTD_OPT_NUM)) {
974
+ U32 const candidateOffCode = STORE_OFFSET(optLdm->offset);
871
975
  DEBUGLOG(6, "ZSTD_optLdm_maybeAddMatch(): Adding ldm candidate match (offCode: %u matchLength %u) at block position=%u",
872
976
  candidateOffCode, candidateMatchLength, currPosInBlock);
873
977
  matches[*nbMatches].len = candidateMatchLength;
@@ -879,8 +983,11 @@ static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
879
983
  /* ZSTD_optLdm_processMatchCandidate():
880
984
  * Wrapper function to update ldm seq store and call ldm functions as necessary.
881
985
  */
882
- static void ZSTD_optLdm_processMatchCandidate(ZSTD_optLdm_t* optLdm, ZSTD_match_t* matches, U32* nbMatches,
883
- U32 currPosInBlock, U32 remainingBytes) {
986
+ static void
987
+ ZSTD_optLdm_processMatchCandidate(ZSTD_optLdm_t* optLdm,
988
+ ZSTD_match_t* matches, U32* nbMatches,
989
+ U32 currPosInBlock, U32 remainingBytes)
990
+ {
884
991
  if (optLdm->seqStore.size == 0 || optLdm->seqStore.pos >= optLdm->seqStore.size) {
885
992
  return;
886
993
  }
@@ -891,19 +998,19 @@ static void ZSTD_optLdm_processMatchCandidate(ZSTD_optLdm_t* optLdm, ZSTD_match_
891
998
  * at the end of a match from the ldm seq store, and will often be some bytes
892
999
  * over beyond matchEndPosInBlock. As such, we need to correct for these "overshoots"
893
1000
  */
894
- U32 posOvershoot = currPosInBlock - optLdm->endPosInBlock;
1001
+ U32 const posOvershoot = currPosInBlock - optLdm->endPosInBlock;
895
1002
  ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, posOvershoot);
896
- }
1003
+ }
897
1004
  ZSTD_opt_getNextMatchAndUpdateSeqStore(optLdm, currPosInBlock, remainingBytes);
898
1005
  }
899
1006
  ZSTD_optLdm_maybeAddMatch(matches, nbMatches, optLdm, currPosInBlock);
900
1007
  }
901
1008
 
1009
+
902
1010
  /*-*******************************
903
1011
  * Optimal parser
904
1012
  *********************************/
905
1013
 
906
-
907
1014
  static U32 ZSTD_totalLen(ZSTD_optimal_t sol)
908
1015
  {
909
1016
  return sol.litlen + sol.mlen;
@@ -944,6 +1051,8 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
944
1051
  const BYTE* const prefixStart = base + ms->window.dictLimit;
945
1052
  const ZSTD_compressionParameters* const cParams = &ms->cParams;
946
1053
 
1054
+ ZSTD_getAllMatchesFn getAllMatches = ZSTD_selectBtGetAllMatches(ms, dictMode);
1055
+
947
1056
  U32 const sufficient_len = MIN(cParams->targetLength, ZSTD_OPT_NUM -1);
948
1057
  U32 const minMatch = (cParams->minMatch == 3) ? 3 : 4;
949
1058
  U32 nextToUpdate3 = ms->nextToUpdate;
@@ -971,7 +1080,7 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
971
1080
  /* find first match */
972
1081
  { U32 const litlen = (U32)(ip - anchor);
973
1082
  U32 const ll0 = !litlen;
974
- U32 nbMatches = ZSTD_BtGetAllMatches(matches, ms, &nextToUpdate3, ip, iend, dictMode, rep, ll0, minMatch);
1083
+ U32 nbMatches = getAllMatches(matches, ms, &nextToUpdate3, ip, iend, rep, ll0, minMatch);
975
1084
  ZSTD_optLdm_processMatchCandidate(&optLdm, matches, &nbMatches,
976
1085
  (U32)(ip-istart), (U32)(iend - ip));
977
1086
  if (!nbMatches) { ip++; continue; }
@@ -985,18 +1094,18 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
985
1094
  * in every price. We include the literal length to avoid negative
986
1095
  * prices when we subtract the previous literal length.
987
1096
  */
988
- opt[0].price = ZSTD_litLengthPrice(litlen, optStatePtr, optLevel);
1097
+ opt[0].price = (int)ZSTD_litLengthPrice(litlen, optStatePtr, optLevel);
989
1098
 
990
1099
  /* large match -> immediate encoding */
991
1100
  { U32 const maxML = matches[nbMatches-1].len;
992
- U32 const maxOffset = matches[nbMatches-1].off;
1101
+ U32 const maxOffcode = matches[nbMatches-1].off;
993
1102
  DEBUGLOG(6, "found %u matches of maxLength=%u and maxOffCode=%u at cPos=%u => start new series",
994
- nbMatches, maxML, maxOffset, (U32)(ip-prefixStart));
1103
+ nbMatches, maxML, maxOffcode, (U32)(ip-prefixStart));
995
1104
 
996
1105
  if (maxML > sufficient_len) {
997
1106
  lastSequence.litlen = litlen;
998
1107
  lastSequence.mlen = maxML;
999
- lastSequence.off = maxOffset;
1108
+ lastSequence.off = maxOffcode;
1000
1109
  DEBUGLOG(6, "large match (%u>%u), immediate encoding",
1001
1110
  maxML, sufficient_len);
1002
1111
  cur = 0;
@@ -1005,24 +1114,25 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1005
1114
  } }
1006
1115
 
1007
1116
  /* set prices for first matches starting position == 0 */
1008
- { U32 const literalsPrice = opt[0].price + ZSTD_litLengthPrice(0, optStatePtr, optLevel);
1117
+ assert(opt[0].price >= 0);
1118
+ { U32 const literalsPrice = (U32)opt[0].price + ZSTD_litLengthPrice(0, optStatePtr, optLevel);
1009
1119
  U32 pos;
1010
1120
  U32 matchNb;
1011
1121
  for (pos = 1; pos < minMatch; pos++) {
1012
1122
  opt[pos].price = ZSTD_MAX_PRICE; /* mlen, litlen and price will be fixed during forward scanning */
1013
1123
  }
1014
1124
  for (matchNb = 0; matchNb < nbMatches; matchNb++) {
1015
- U32 const offset = matches[matchNb].off;
1125
+ U32 const offcode = matches[matchNb].off;
1016
1126
  U32 const end = matches[matchNb].len;
1017
1127
  for ( ; pos <= end ; pos++ ) {
1018
- U32 const matchPrice = ZSTD_getMatchPrice(offset, pos, optStatePtr, optLevel);
1128
+ U32 const matchPrice = ZSTD_getMatchPrice(offcode, pos, optStatePtr, optLevel);
1019
1129
  U32 const sequencePrice = literalsPrice + matchPrice;
1020
1130
  DEBUGLOG(7, "rPos:%u => set initial price : %.2f",
1021
1131
  pos, ZSTD_fCost(sequencePrice));
1022
1132
  opt[pos].mlen = pos;
1023
- opt[pos].off = offset;
1133
+ opt[pos].off = offcode;
1024
1134
  opt[pos].litlen = litlen;
1025
- opt[pos].price = sequencePrice;
1135
+ opt[pos].price = (int)sequencePrice;
1026
1136
  } }
1027
1137
  last_pos = pos-1;
1028
1138
  }
@@ -1037,9 +1147,9 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1037
1147
  /* Fix current position with one literal if cheaper */
1038
1148
  { U32 const litlen = (opt[cur-1].mlen == 0) ? opt[cur-1].litlen + 1 : 1;
1039
1149
  int const price = opt[cur-1].price
1040
- + ZSTD_rawLiteralsCost(ip+cur-1, 1, optStatePtr, optLevel)
1041
- + ZSTD_litLengthPrice(litlen, optStatePtr, optLevel)
1042
- - ZSTD_litLengthPrice(litlen-1, optStatePtr, optLevel);
1150
+ + (int)ZSTD_rawLiteralsCost(ip+cur-1, 1, optStatePtr, optLevel)
1151
+ + (int)ZSTD_litLengthPrice(litlen, optStatePtr, optLevel)
1152
+ - (int)ZSTD_litLengthPrice(litlen-1, optStatePtr, optLevel);
1043
1153
  assert(price < 1000000000); /* overflow check */
1044
1154
  if (price <= opt[cur].price) {
1045
1155
  DEBUGLOG(7, "cPos:%zi==rPos:%u : better price (%.2f<=%.2f) using literal (ll==%u) (hist:%u,%u,%u)",
@@ -1065,7 +1175,7 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1065
1175
  assert(cur >= opt[cur].mlen);
1066
1176
  if (opt[cur].mlen != 0) {
1067
1177
  U32 const prev = cur - opt[cur].mlen;
1068
- repcodes_t newReps = ZSTD_updateRep(opt[prev].rep, opt[cur].off, opt[cur].litlen==0);
1178
+ repcodes_t const newReps = ZSTD_newRep(opt[prev].rep, opt[cur].off, opt[cur].litlen==0);
1069
1179
  ZSTD_memcpy(opt[cur].rep, &newReps, sizeof(repcodes_t));
1070
1180
  } else {
1071
1181
  ZSTD_memcpy(opt[cur].rep, opt[cur - 1].rep, sizeof(repcodes_t));
@@ -1082,11 +1192,12 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1082
1192
  continue; /* skip unpromising positions; about ~+6% speed, -0.01 ratio */
1083
1193
  }
1084
1194
 
1195
+ assert(opt[cur].price >= 0);
1085
1196
  { U32 const ll0 = (opt[cur].mlen != 0);
1086
1197
  U32 const litlen = (opt[cur].mlen == 0) ? opt[cur].litlen : 0;
1087
- U32 const previousPrice = opt[cur].price;
1198
+ U32 const previousPrice = (U32)opt[cur].price;
1088
1199
  U32 const basePrice = previousPrice + ZSTD_litLengthPrice(0, optStatePtr, optLevel);
1089
- U32 nbMatches = ZSTD_BtGetAllMatches(matches, ms, &nextToUpdate3, inr, iend, dictMode, opt[cur].rep, ll0, minMatch);
1200
+ U32 nbMatches = getAllMatches(matches, ms, &nextToUpdate3, inr, iend, opt[cur].rep, ll0, minMatch);
1090
1201
  U32 matchNb;
1091
1202
 
1092
1203
  ZSTD_optLdm_processMatchCandidate(&optLdm, matches, &nbMatches,
@@ -1124,7 +1235,7 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1124
1235
 
1125
1236
  for (mlen = lastML; mlen >= startML; mlen--) { /* scan downward */
1126
1237
  U32 const pos = cur + mlen;
1127
- int const price = basePrice + ZSTD_getMatchPrice(offset, mlen, optStatePtr, optLevel);
1238
+ int const price = (int)basePrice + (int)ZSTD_getMatchPrice(offset, mlen, optStatePtr, optLevel);
1128
1239
 
1129
1240
  if ((pos > last_pos) || (price < opt[pos].price)) {
1130
1241
  DEBUGLOG(7, "rPos:%u (ml=%2u) => new better price (%.2f<%.2f)",
@@ -1154,7 +1265,7 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1154
1265
  * update them while traversing the sequences.
1155
1266
  */
1156
1267
  if (lastSequence.mlen != 0) {
1157
- repcodes_t reps = ZSTD_updateRep(opt[cur].rep, lastSequence.off, lastSequence.litlen==0);
1268
+ repcodes_t const reps = ZSTD_newRep(opt[cur].rep, lastSequence.off, lastSequence.litlen==0);
1158
1269
  ZSTD_memcpy(rep, &reps, sizeof(reps));
1159
1270
  } else {
1160
1271
  ZSTD_memcpy(rep, opt[cur].rep, sizeof(repcodes_t));
@@ -1198,7 +1309,7 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1198
1309
 
1199
1310
  assert(anchor + llen <= iend);
1200
1311
  ZSTD_updateStats(optStatePtr, llen, anchor, offCode, mlen);
1201
- ZSTD_storeSeq(seqStore, llen, anchor, iend, offCode, mlen-MINMATCH);
1312
+ ZSTD_storeSeq(seqStore, llen, anchor, iend, offCode, mlen);
1202
1313
  anchor += advance;
1203
1314
  ip = anchor;
1204
1315
  } }
@@ -1210,38 +1321,30 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1210
1321
  return (size_t)(iend - anchor);
1211
1322
  }
1212
1323
 
1324
+ static size_t ZSTD_compressBlock_opt0(
1325
+ ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1326
+ const void* src, size_t srcSize, const ZSTD_dictMode_e dictMode)
1327
+ {
1328
+ return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0 /* optLevel */, dictMode);
1329
+ }
1330
+
1331
+ static size_t ZSTD_compressBlock_opt2(
1332
+ ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1333
+ const void* src, size_t srcSize, const ZSTD_dictMode_e dictMode)
1334
+ {
1335
+ return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /* optLevel */, dictMode);
1336
+ }
1213
1337
 
1214
1338
  size_t ZSTD_compressBlock_btopt(
1215
1339
  ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1216
1340
  const void* src, size_t srcSize)
1217
1341
  {
1218
1342
  DEBUGLOG(5, "ZSTD_compressBlock_btopt");
1219
- return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0 /*optLevel*/, ZSTD_noDict);
1343
+ return ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_noDict);
1220
1344
  }
1221
1345
 
1222
1346
 
1223
- /* used in 2-pass strategy */
1224
- static U32 ZSTD_upscaleStat(unsigned* table, U32 lastEltIndex, int bonus)
1225
- {
1226
- U32 s, sum=0;
1227
- assert(ZSTD_FREQ_DIV+bonus >= 0);
1228
- for (s=0; s<lastEltIndex+1; s++) {
1229
- table[s] <<= ZSTD_FREQ_DIV+bonus;
1230
- table[s]--;
1231
- sum += table[s];
1232
- }
1233
- return sum;
1234
- }
1235
1347
 
1236
- /* used in 2-pass strategy */
1237
- MEM_STATIC void ZSTD_upscaleStats(optState_t* optPtr)
1238
- {
1239
- if (ZSTD_compressedLiterals(optPtr))
1240
- optPtr->litSum = ZSTD_upscaleStat(optPtr->litFreq, MaxLit, 0);
1241
- optPtr->litLengthSum = ZSTD_upscaleStat(optPtr->litLengthFreq, MaxLL, 0);
1242
- optPtr->matchLengthSum = ZSTD_upscaleStat(optPtr->matchLengthFreq, MaxML, 0);
1243
- optPtr->offCodeSum = ZSTD_upscaleStat(optPtr->offCodeFreq, MaxOff, 0);
1244
- }
1245
1348
 
1246
1349
  /* ZSTD_initStats_ultra():
1247
1350
  * make a first compression pass, just to seed stats with more accurate starting values.
@@ -1263,7 +1366,7 @@ ZSTD_initStats_ultra(ZSTD_matchState_t* ms,
1263
1366
  assert(ms->window.dictLimit == ms->window.lowLimit); /* no dictionary */
1264
1367
  assert(ms->window.dictLimit - ms->nextToUpdate <= 1); /* no prefix (note: intentional overflow, defined as 2-complement) */
1265
1368
 
1266
- ZSTD_compressBlock_opt_generic(ms, seqStore, tmpRep, src, srcSize, 2 /*optLevel*/, ZSTD_noDict); /* generate stats into ms->opt*/
1369
+ ZSTD_compressBlock_opt2(ms, seqStore, tmpRep, src, srcSize, ZSTD_noDict); /* generate stats into ms->opt*/
1267
1370
 
1268
1371
  /* invalidate first scan from history */
1269
1372
  ZSTD_resetSeqStore(seqStore);
@@ -1272,8 +1375,6 @@ ZSTD_initStats_ultra(ZSTD_matchState_t* ms,
1272
1375
  ms->window.lowLimit = ms->window.dictLimit;
1273
1376
  ms->nextToUpdate = ms->window.dictLimit;
1274
1377
 
1275
- /* re-inforce weight of collected statistics */
1276
- ZSTD_upscaleStats(&ms->opt);
1277
1378
  }
1278
1379
 
1279
1380
  size_t ZSTD_compressBlock_btultra(
@@ -1281,7 +1382,7 @@ size_t ZSTD_compressBlock_btultra(
1281
1382
  const void* src, size_t srcSize)
1282
1383
  {
1283
1384
  DEBUGLOG(5, "ZSTD_compressBlock_btultra (srcSize=%zu)", srcSize);
1284
- return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_noDict);
1385
+ return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_noDict);
1285
1386
  }
1286
1387
 
1287
1388
  size_t ZSTD_compressBlock_btultra2(
@@ -1309,35 +1410,35 @@ size_t ZSTD_compressBlock_btultra2(
1309
1410
  ZSTD_initStats_ultra(ms, seqStore, rep, src, srcSize);
1310
1411
  }
1311
1412
 
1312
- return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_noDict);
1413
+ return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_noDict);
1313
1414
  }
1314
1415
 
1315
1416
  size_t ZSTD_compressBlock_btopt_dictMatchState(
1316
1417
  ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1317
1418
  const void* src, size_t srcSize)
1318
1419
  {
1319
- return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0 /*optLevel*/, ZSTD_dictMatchState);
1420
+ return ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_dictMatchState);
1320
1421
  }
1321
1422
 
1322
1423
  size_t ZSTD_compressBlock_btultra_dictMatchState(
1323
1424
  ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1324
1425
  const void* src, size_t srcSize)
1325
1426
  {
1326
- return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_dictMatchState);
1427
+ return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_dictMatchState);
1327
1428
  }
1328
1429
 
1329
1430
  size_t ZSTD_compressBlock_btopt_extDict(
1330
1431
  ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1331
1432
  const void* src, size_t srcSize)
1332
1433
  {
1333
- return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0 /*optLevel*/, ZSTD_extDict);
1434
+ return ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_extDict);
1334
1435
  }
1335
1436
 
1336
1437
  size_t ZSTD_compressBlock_btultra_extDict(
1337
1438
  ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1338
1439
  const void* src, size_t srcSize)
1339
1440
  {
1340
- return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_extDict);
1441
+ return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_extDict);
1341
1442
  }
1342
1443
 
1343
1444
  /* note : no btultra2 variant for extDict nor dictMatchState,