Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmzstd/lib/compress/zstd_double_fast.c
5020 views
1
/*
2
* Copyright (c) Meta Platforms, Inc. and affiliates.
3
* All rights reserved.
4
*
5
* This source code is licensed under both the BSD-style license (found in the
6
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
* in the COPYING file in the root directory of this source tree).
8
* You may select, at your option, one of the above-listed licenses.
9
*/
10
11
#include "zstd_compress_internal.h"
12
#include "zstd_double_fast.h"
13
14
#ifndef ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR
15
16
static
17
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
18
void ZSTD_fillDoubleHashTableForCDict(ZSTD_MatchState_t* ms,
19
void const* end, ZSTD_dictTableLoadMethod_e dtlm)
20
{
21
const ZSTD_compressionParameters* const cParams = &ms->cParams;
22
U32* const hashLarge = ms->hashTable;
23
U32 const hBitsL = cParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
24
U32 const mls = cParams->minMatch;
25
U32* const hashSmall = ms->chainTable;
26
U32 const hBitsS = cParams->chainLog + ZSTD_SHORT_CACHE_TAG_BITS;
27
const BYTE* const base = ms->window.base;
28
const BYTE* ip = base + ms->nextToUpdate;
29
const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
30
const U32 fastHashFillStep = 3;
31
32
/* Always insert every fastHashFillStep position into the hash tables.
33
* Insert the other positions into the large hash table if their entry
34
* is empty.
35
*/
36
for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
37
U32 const curr = (U32)(ip - base);
38
U32 i;
39
for (i = 0; i < fastHashFillStep; ++i) {
40
size_t const smHashAndTag = ZSTD_hashPtr(ip + i, hBitsS, mls);
41
size_t const lgHashAndTag = ZSTD_hashPtr(ip + i, hBitsL, 8);
42
if (i == 0) {
43
ZSTD_writeTaggedIndex(hashSmall, smHashAndTag, curr + i);
44
}
45
if (i == 0 || hashLarge[lgHashAndTag >> ZSTD_SHORT_CACHE_TAG_BITS] == 0) {
46
ZSTD_writeTaggedIndex(hashLarge, lgHashAndTag, curr + i);
47
}
48
/* Only load extra positions for ZSTD_dtlm_full */
49
if (dtlm == ZSTD_dtlm_fast)
50
break;
51
} }
52
}
53
54
static
55
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
56
void ZSTD_fillDoubleHashTableForCCtx(ZSTD_MatchState_t* ms,
57
void const* end, ZSTD_dictTableLoadMethod_e dtlm)
58
{
59
const ZSTD_compressionParameters* const cParams = &ms->cParams;
60
U32* const hashLarge = ms->hashTable;
61
U32 const hBitsL = cParams->hashLog;
62
U32 const mls = cParams->minMatch;
63
U32* const hashSmall = ms->chainTable;
64
U32 const hBitsS = cParams->chainLog;
65
const BYTE* const base = ms->window.base;
66
const BYTE* ip = base + ms->nextToUpdate;
67
const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
68
const U32 fastHashFillStep = 3;
69
70
/* Always insert every fastHashFillStep position into the hash tables.
71
* Insert the other positions into the large hash table if their entry
72
* is empty.
73
*/
74
for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
75
U32 const curr = (U32)(ip - base);
76
U32 i;
77
for (i = 0; i < fastHashFillStep; ++i) {
78
size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
79
size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
80
if (i == 0)
81
hashSmall[smHash] = curr + i;
82
if (i == 0 || hashLarge[lgHash] == 0)
83
hashLarge[lgHash] = curr + i;
84
/* Only load extra positions for ZSTD_dtlm_full */
85
if (dtlm == ZSTD_dtlm_fast)
86
break;
87
} }
88
}
89
90
void ZSTD_fillDoubleHashTable(ZSTD_MatchState_t* ms,
91
const void* const end,
92
ZSTD_dictTableLoadMethod_e dtlm,
93
ZSTD_tableFillPurpose_e tfp)
94
{
95
if (tfp == ZSTD_tfp_forCDict) {
96
ZSTD_fillDoubleHashTableForCDict(ms, end, dtlm);
97
} else {
98
ZSTD_fillDoubleHashTableForCCtx(ms, end, dtlm);
99
}
100
}
101
102
103
FORCE_INLINE_TEMPLATE
104
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
105
size_t ZSTD_compressBlock_doubleFast_noDict_generic(
106
ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
107
void const* src, size_t srcSize, U32 const mls /* template */)
108
{
109
ZSTD_compressionParameters const* cParams = &ms->cParams;
110
U32* const hashLong = ms->hashTable;
111
const U32 hBitsL = cParams->hashLog;
112
U32* const hashSmall = ms->chainTable;
113
const U32 hBitsS = cParams->chainLog;
114
const BYTE* const base = ms->window.base;
115
const BYTE* const istart = (const BYTE*)src;
116
const BYTE* anchor = istart;
117
const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
118
/* presumes that, if there is a dictionary, it must be using Attach mode */
119
const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
120
const BYTE* const prefixLowest = base + prefixLowestIndex;
121
const BYTE* const iend = istart + srcSize;
122
const BYTE* const ilimit = iend - HASH_READ_SIZE;
123
U32 offset_1=rep[0], offset_2=rep[1];
124
U32 offsetSaved1 = 0, offsetSaved2 = 0;
125
126
size_t mLength;
127
U32 offset;
128
U32 curr;
129
130
/* how many positions to search before increasing step size */
131
const size_t kStepIncr = 1 << kSearchStrength;
132
/* the position at which to increment the step size if no match is found */
133
const BYTE* nextStep;
134
size_t step; /* the current step size */
135
136
size_t hl0; /* the long hash at ip */
137
size_t hl1; /* the long hash at ip1 */
138
139
U32 idxl0; /* the long match index for ip */
140
U32 idxl1; /* the long match index for ip1 */
141
142
const BYTE* matchl0; /* the long match for ip */
143
const BYTE* matchs0; /* the short match for ip */
144
const BYTE* matchl1; /* the long match for ip1 */
145
const BYTE* matchs0_safe; /* matchs0 or safe address */
146
147
const BYTE* ip = istart; /* the current position */
148
const BYTE* ip1; /* the next position */
149
/* Array of ~random data, should have low probability of matching data
150
* we load from here instead of from tables, if matchl0/matchl1 are
151
* invalid indices. Used to avoid unpredictable branches. */
152
const BYTE dummy[] = {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0xe2,0xb4};
153
154
DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_noDict_generic");
155
156
/* init */
157
ip += ((ip - prefixLowest) == 0);
158
{
159
U32 const current = (U32)(ip - base);
160
U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, current, cParams->windowLog);
161
U32 const maxRep = current - windowLow;
162
if (offset_2 > maxRep) offsetSaved2 = offset_2, offset_2 = 0;
163
if (offset_1 > maxRep) offsetSaved1 = offset_1, offset_1 = 0;
164
}
165
166
/* Outer Loop: one iteration per match found and stored */
167
while (1) {
168
step = 1;
169
nextStep = ip + kStepIncr;
170
ip1 = ip + step;
171
172
if (ip1 > ilimit) {
173
goto _cleanup;
174
}
175
176
hl0 = ZSTD_hashPtr(ip, hBitsL, 8);
177
idxl0 = hashLong[hl0];
178
matchl0 = base + idxl0;
179
180
/* Inner Loop: one iteration per search / position */
181
do {
182
const size_t hs0 = ZSTD_hashPtr(ip, hBitsS, mls);
183
const U32 idxs0 = hashSmall[hs0];
184
curr = (U32)(ip-base);
185
matchs0 = base + idxs0;
186
187
hashLong[hl0] = hashSmall[hs0] = curr; /* update hash tables */
188
189
/* check noDict repcode */
190
if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
191
mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
192
ip++;
193
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
194
goto _match_stored;
195
}
196
197
hl1 = ZSTD_hashPtr(ip1, hBitsL, 8);
198
199
/* idxl0 > prefixLowestIndex is a (somewhat) unpredictable branch.
200
* However expression below complies into conditional move. Since
201
* match is unlikely and we only *branch* on idxl0 > prefixLowestIndex
202
* if there is a match, all branches become predictable. */
203
{ const BYTE* const matchl0_safe = ZSTD_selectAddr(idxl0, prefixLowestIndex, matchl0, &dummy[0]);
204
205
/* check prefix long match */
206
if (MEM_read64(matchl0_safe) == MEM_read64(ip) && matchl0_safe == matchl0) {
207
mLength = ZSTD_count(ip+8, matchl0+8, iend) + 8;
208
offset = (U32)(ip-matchl0);
209
while (((ip>anchor) & (matchl0>prefixLowest)) && (ip[-1] == matchl0[-1])) { ip--; matchl0--; mLength++; } /* catch up */
210
goto _match_found;
211
} }
212
213
idxl1 = hashLong[hl1];
214
matchl1 = base + idxl1;
215
216
/* Same optimization as matchl0 above */
217
matchs0_safe = ZSTD_selectAddr(idxs0, prefixLowestIndex, matchs0, &dummy[0]);
218
219
/* check prefix short match */
220
if(MEM_read32(matchs0_safe) == MEM_read32(ip) && matchs0_safe == matchs0) {
221
goto _search_next_long;
222
}
223
224
if (ip1 >= nextStep) {
225
PREFETCH_L1(ip1 + 64);
226
PREFETCH_L1(ip1 + 128);
227
step++;
228
nextStep += kStepIncr;
229
}
230
ip = ip1;
231
ip1 += step;
232
233
hl0 = hl1;
234
idxl0 = idxl1;
235
matchl0 = matchl1;
236
#if defined(__aarch64__)
237
PREFETCH_L1(ip+256);
238
#endif
239
} while (ip1 <= ilimit);
240
241
_cleanup:
242
/* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0),
243
* rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */
244
offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2;
245
246
/* save reps for next block */
247
rep[0] = offset_1 ? offset_1 : offsetSaved1;
248
rep[1] = offset_2 ? offset_2 : offsetSaved2;
249
250
/* Return the last literals size */
251
return (size_t)(iend - anchor);
252
253
_search_next_long:
254
255
/* short match found: let's check for a longer one */
256
mLength = ZSTD_count(ip+4, matchs0+4, iend) + 4;
257
offset = (U32)(ip - matchs0);
258
259
/* check long match at +1 position */
260
if ((idxl1 > prefixLowestIndex) && (MEM_read64(matchl1) == MEM_read64(ip1))) {
261
size_t const l1len = ZSTD_count(ip1+8, matchl1+8, iend) + 8;
262
if (l1len > mLength) {
263
/* use the long match instead */
264
ip = ip1;
265
mLength = l1len;
266
offset = (U32)(ip-matchl1);
267
matchs0 = matchl1;
268
}
269
}
270
271
while (((ip>anchor) & (matchs0>prefixLowest)) && (ip[-1] == matchs0[-1])) { ip--; matchs0--; mLength++; } /* complete backward */
272
273
/* fall-through */
274
275
_match_found: /* requires ip, offset, mLength */
276
offset_2 = offset_1;
277
offset_1 = offset;
278
279
if (step < 4) {
280
/* It is unsafe to write this value back to the hashtable when ip1 is
281
* greater than or equal to the new ip we will have after we're done
282
* processing this match. Rather than perform that test directly
283
* (ip1 >= ip + mLength), which costs speed in practice, we do a simpler
284
* more predictable test. The minmatch even if we take a short match is
285
* 4 bytes, so as long as step, the distance between ip and ip1
286
* (initially) is less than 4, we know ip1 < new ip. */
287
hashLong[hl1] = (U32)(ip1 - base);
288
}
289
290
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
291
292
_match_stored:
293
/* match found */
294
ip += mLength;
295
anchor = ip;
296
297
if (ip <= ilimit) {
298
/* Complementary insertion */
299
/* done after iLimit test, as candidates could be > iend-8 */
300
{ U32 const indexToInsert = curr+2;
301
hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
302
hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
303
hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
304
hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
305
}
306
307
/* check immediate repcode */
308
while ( (ip <= ilimit)
309
&& ( (offset_2>0)
310
& (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
311
/* store sequence */
312
size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
313
U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
314
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
315
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
316
ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, rLength);
317
ip += rLength;
318
anchor = ip;
319
continue; /* faster when present ... (?) */
320
}
321
}
322
}
323
}
324
325
326
FORCE_INLINE_TEMPLATE
327
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
328
size_t ZSTD_compressBlock_doubleFast_dictMatchState_generic(
329
ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
330
void const* src, size_t srcSize,
331
U32 const mls /* template */)
332
{
333
ZSTD_compressionParameters const* cParams = &ms->cParams;
334
U32* const hashLong = ms->hashTable;
335
const U32 hBitsL = cParams->hashLog;
336
U32* const hashSmall = ms->chainTable;
337
const U32 hBitsS = cParams->chainLog;
338
const BYTE* const base = ms->window.base;
339
const BYTE* const istart = (const BYTE*)src;
340
const BYTE* ip = istart;
341
const BYTE* anchor = istart;
342
const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
343
/* presumes that, if there is a dictionary, it must be using Attach mode */
344
const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
345
const BYTE* const prefixLowest = base + prefixLowestIndex;
346
const BYTE* const iend = istart + srcSize;
347
const BYTE* const ilimit = iend - HASH_READ_SIZE;
348
U32 offset_1=rep[0], offset_2=rep[1];
349
350
const ZSTD_MatchState_t* const dms = ms->dictMatchState;
351
const ZSTD_compressionParameters* const dictCParams = &dms->cParams;
352
const U32* const dictHashLong = dms->hashTable;
353
const U32* const dictHashSmall = dms->chainTable;
354
const U32 dictStartIndex = dms->window.dictLimit;
355
const BYTE* const dictBase = dms->window.base;
356
const BYTE* const dictStart = dictBase + dictStartIndex;
357
const BYTE* const dictEnd = dms->window.nextSrc;
358
const U32 dictIndexDelta = prefixLowestIndex - (U32)(dictEnd - dictBase);
359
const U32 dictHBitsL = dictCParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
360
const U32 dictHBitsS = dictCParams->chainLog + ZSTD_SHORT_CACHE_TAG_BITS;
361
const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictStart));
362
363
DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_dictMatchState_generic");
364
365
/* if a dictionary is attached, it must be within window range */
366
assert(ms->window.dictLimit + (1U << cParams->windowLog) >= endIndex);
367
368
if (ms->prefetchCDictTables) {
369
size_t const hashTableBytes = (((size_t)1) << dictCParams->hashLog) * sizeof(U32);
370
size_t const chainTableBytes = (((size_t)1) << dictCParams->chainLog) * sizeof(U32);
371
PREFETCH_AREA(dictHashLong, hashTableBytes);
372
PREFETCH_AREA(dictHashSmall, chainTableBytes);
373
}
374
375
/* init */
376
ip += (dictAndPrefixLength == 0);
377
378
/* dictMatchState repCode checks don't currently handle repCode == 0
379
* disabling. */
380
assert(offset_1 <= dictAndPrefixLength);
381
assert(offset_2 <= dictAndPrefixLength);
382
383
/* Main Search Loop */
384
while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
385
size_t mLength;
386
U32 offset;
387
size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
388
size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
389
size_t const dictHashAndTagL = ZSTD_hashPtr(ip, dictHBitsL, 8);
390
size_t const dictHashAndTagS = ZSTD_hashPtr(ip, dictHBitsS, mls);
391
U32 const dictMatchIndexAndTagL = dictHashLong[dictHashAndTagL >> ZSTD_SHORT_CACHE_TAG_BITS];
392
U32 const dictMatchIndexAndTagS = dictHashSmall[dictHashAndTagS >> ZSTD_SHORT_CACHE_TAG_BITS];
393
int const dictTagsMatchL = ZSTD_comparePackedTags(dictMatchIndexAndTagL, dictHashAndTagL);
394
int const dictTagsMatchS = ZSTD_comparePackedTags(dictMatchIndexAndTagS, dictHashAndTagS);
395
U32 const curr = (U32)(ip-base);
396
U32 const matchIndexL = hashLong[h2];
397
U32 matchIndexS = hashSmall[h];
398
const BYTE* matchLong = base + matchIndexL;
399
const BYTE* match = base + matchIndexS;
400
const U32 repIndex = curr + 1 - offset_1;
401
const BYTE* repMatch = (repIndex < prefixLowestIndex) ?
402
dictBase + (repIndex - dictIndexDelta) :
403
base + repIndex;
404
hashLong[h2] = hashSmall[h] = curr; /* update hash tables */
405
406
/* check repcode */
407
if ((ZSTD_index_overlap_check(prefixLowestIndex, repIndex))
408
&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
409
const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
410
mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
411
ip++;
412
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
413
goto _match_stored;
414
}
415
416
if ((matchIndexL >= prefixLowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
417
/* check prefix long match */
418
mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
419
offset = (U32)(ip-matchLong);
420
while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
421
goto _match_found;
422
} else if (dictTagsMatchL) {
423
/* check dictMatchState long match */
424
U32 const dictMatchIndexL = dictMatchIndexAndTagL >> ZSTD_SHORT_CACHE_TAG_BITS;
425
const BYTE* dictMatchL = dictBase + dictMatchIndexL;
426
assert(dictMatchL < dictEnd);
427
428
if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
429
mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
430
offset = (U32)(curr - dictMatchIndexL - dictIndexDelta);
431
while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
432
goto _match_found;
433
} }
434
435
if (matchIndexS > prefixLowestIndex) {
436
/* short match candidate */
437
if (MEM_read32(match) == MEM_read32(ip)) {
438
goto _search_next_long;
439
}
440
} else if (dictTagsMatchS) {
441
/* check dictMatchState short match */
442
U32 const dictMatchIndexS = dictMatchIndexAndTagS >> ZSTD_SHORT_CACHE_TAG_BITS;
443
match = dictBase + dictMatchIndexS;
444
matchIndexS = dictMatchIndexS + dictIndexDelta;
445
446
if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
447
goto _search_next_long;
448
} }
449
450
ip += ((ip-anchor) >> kSearchStrength) + 1;
451
#if defined(__aarch64__)
452
PREFETCH_L1(ip+256);
453
#endif
454
continue;
455
456
_search_next_long:
457
{ size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
458
size_t const dictHashAndTagL3 = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
459
U32 const matchIndexL3 = hashLong[hl3];
460
U32 const dictMatchIndexAndTagL3 = dictHashLong[dictHashAndTagL3 >> ZSTD_SHORT_CACHE_TAG_BITS];
461
int const dictTagsMatchL3 = ZSTD_comparePackedTags(dictMatchIndexAndTagL3, dictHashAndTagL3);
462
const BYTE* matchL3 = base + matchIndexL3;
463
hashLong[hl3] = curr + 1;
464
465
/* check prefix long +1 match */
466
if ((matchIndexL3 >= prefixLowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1))) {
467
mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
468
ip++;
469
offset = (U32)(ip-matchL3);
470
while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
471
goto _match_found;
472
} else if (dictTagsMatchL3) {
473
/* check dict long +1 match */
474
U32 const dictMatchIndexL3 = dictMatchIndexAndTagL3 >> ZSTD_SHORT_CACHE_TAG_BITS;
475
const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
476
assert(dictMatchL3 < dictEnd);
477
if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
478
mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
479
ip++;
480
offset = (U32)(curr + 1 - dictMatchIndexL3 - dictIndexDelta);
481
while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
482
goto _match_found;
483
} } }
484
485
/* if no long +1 match, explore the short match we found */
486
if (matchIndexS < prefixLowestIndex) {
487
mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
488
offset = (U32)(curr - matchIndexS);
489
while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
490
} else {
491
mLength = ZSTD_count(ip+4, match+4, iend) + 4;
492
offset = (U32)(ip - match);
493
while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
494
}
495
496
_match_found:
497
offset_2 = offset_1;
498
offset_1 = offset;
499
500
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
501
502
_match_stored:
503
/* match found */
504
ip += mLength;
505
anchor = ip;
506
507
if (ip <= ilimit) {
508
/* Complementary insertion */
509
/* done after iLimit test, as candidates could be > iend-8 */
510
{ U32 const indexToInsert = curr+2;
511
hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
512
hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
513
hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
514
hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
515
}
516
517
/* check immediate repcode */
518
while (ip <= ilimit) {
519
U32 const current2 = (U32)(ip-base);
520
U32 const repIndex2 = current2 - offset_2;
521
const BYTE* repMatch2 = repIndex2 < prefixLowestIndex ?
522
dictBase + repIndex2 - dictIndexDelta :
523
base + repIndex2;
524
if ( (ZSTD_index_overlap_check(prefixLowestIndex, repIndex2))
525
&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
526
const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
527
size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
528
U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
529
ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
530
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
531
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
532
ip += repLength2;
533
anchor = ip;
534
continue;
535
}
536
break;
537
}
538
}
539
} /* while (ip < ilimit) */
540
541
/* save reps for next block */
542
rep[0] = offset_1;
543
rep[1] = offset_2;
544
545
/* Return the last literals size */
546
return (size_t)(iend - anchor);
547
}
548
549
#define ZSTD_GEN_DFAST_FN(dictMode, mls) \
550
static size_t ZSTD_compressBlock_doubleFast_##dictMode##_##mls( \
551
ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], \
552
void const* src, size_t srcSize) \
553
{ \
554
return ZSTD_compressBlock_doubleFast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls); \
555
}
556
557
ZSTD_GEN_DFAST_FN(noDict, 4)
558
ZSTD_GEN_DFAST_FN(noDict, 5)
559
ZSTD_GEN_DFAST_FN(noDict, 6)
560
ZSTD_GEN_DFAST_FN(noDict, 7)
561
562
ZSTD_GEN_DFAST_FN(dictMatchState, 4)
563
ZSTD_GEN_DFAST_FN(dictMatchState, 5)
564
ZSTD_GEN_DFAST_FN(dictMatchState, 6)
565
ZSTD_GEN_DFAST_FN(dictMatchState, 7)
566
567
568
size_t ZSTD_compressBlock_doubleFast(
569
ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
570
void const* src, size_t srcSize)
571
{
572
const U32 mls = ms->cParams.minMatch;
573
switch(mls)
574
{
575
default: /* includes case 3 */
576
case 4 :
577
return ZSTD_compressBlock_doubleFast_noDict_4(ms, seqStore, rep, src, srcSize);
578
case 5 :
579
return ZSTD_compressBlock_doubleFast_noDict_5(ms, seqStore, rep, src, srcSize);
580
case 6 :
581
return ZSTD_compressBlock_doubleFast_noDict_6(ms, seqStore, rep, src, srcSize);
582
case 7 :
583
return ZSTD_compressBlock_doubleFast_noDict_7(ms, seqStore, rep, src, srcSize);
584
}
585
}
586
587
588
size_t ZSTD_compressBlock_doubleFast_dictMatchState(
589
ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
590
void const* src, size_t srcSize)
591
{
592
const U32 mls = ms->cParams.minMatch;
593
switch(mls)
594
{
595
default: /* includes case 3 */
596
case 4 :
597
return ZSTD_compressBlock_doubleFast_dictMatchState_4(ms, seqStore, rep, src, srcSize);
598
case 5 :
599
return ZSTD_compressBlock_doubleFast_dictMatchState_5(ms, seqStore, rep, src, srcSize);
600
case 6 :
601
return ZSTD_compressBlock_doubleFast_dictMatchState_6(ms, seqStore, rep, src, srcSize);
602
case 7 :
603
return ZSTD_compressBlock_doubleFast_dictMatchState_7(ms, seqStore, rep, src, srcSize);
604
}
605
}
606
607
608
static
609
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
610
size_t ZSTD_compressBlock_doubleFast_extDict_generic(
611
ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
612
void const* src, size_t srcSize,
613
U32 const mls /* template */)
614
{
615
ZSTD_compressionParameters const* cParams = &ms->cParams;
616
U32* const hashLong = ms->hashTable;
617
U32 const hBitsL = cParams->hashLog;
618
U32* const hashSmall = ms->chainTable;
619
U32 const hBitsS = cParams->chainLog;
620
const BYTE* const istart = (const BYTE*)src;
621
const BYTE* ip = istart;
622
const BYTE* anchor = istart;
623
const BYTE* const iend = istart + srcSize;
624
const BYTE* const ilimit = iend - 8;
625
const BYTE* const base = ms->window.base;
626
const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
627
const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
628
const U32 dictStartIndex = lowLimit;
629
const U32 dictLimit = ms->window.dictLimit;
630
const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
631
const BYTE* const prefixStart = base + prefixStartIndex;
632
const BYTE* const dictBase = ms->window.dictBase;
633
const BYTE* const dictStart = dictBase + dictStartIndex;
634
const BYTE* const dictEnd = dictBase + prefixStartIndex;
635
U32 offset_1=rep[0], offset_2=rep[1];
636
637
DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
638
639
/* if extDict is invalidated due to maxDistance, switch to "regular" variant */
640
if (prefixStartIndex == dictStartIndex)
641
return ZSTD_compressBlock_doubleFast(ms, seqStore, rep, src, srcSize);
642
643
/* Search Loop */
644
while (ip < ilimit) { /* < instead of <=, because (ip+1) */
645
const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
646
const U32 matchIndex = hashSmall[hSmall];
647
const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
648
const BYTE* match = matchBase + matchIndex;
649
650
const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
651
const U32 matchLongIndex = hashLong[hLong];
652
const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
653
const BYTE* matchLong = matchLongBase + matchLongIndex;
654
655
const U32 curr = (U32)(ip-base);
656
const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
657
const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
658
const BYTE* const repMatch = repBase + repIndex;
659
size_t mLength;
660
hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
661
662
if (((ZSTD_index_overlap_check(prefixStartIndex, repIndex))
663
& (offset_1 <= curr+1 - dictStartIndex)) /* note: we are searching at curr+1 */
664
&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
665
const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
666
mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
667
ip++;
668
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
669
} else {
670
if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
671
const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
672
const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
673
U32 offset;
674
mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
675
offset = curr - matchLongIndex;
676
while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
677
offset_2 = offset_1;
678
offset_1 = offset;
679
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
680
681
} else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
682
size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
683
U32 const matchIndex3 = hashLong[h3];
684
const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
685
const BYTE* match3 = match3Base + matchIndex3;
686
U32 offset;
687
hashLong[h3] = curr + 1;
688
if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
689
const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
690
const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
691
mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
692
ip++;
693
offset = curr+1 - matchIndex3;
694
while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
695
} else {
696
const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
697
const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
698
mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
699
offset = curr - matchIndex;
700
while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
701
}
702
offset_2 = offset_1;
703
offset_1 = offset;
704
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
705
706
} else {
707
ip += ((ip-anchor) >> kSearchStrength) + 1;
708
continue;
709
} }
710
711
/* move to next sequence start */
712
ip += mLength;
713
anchor = ip;
714
715
if (ip <= ilimit) {
716
/* Complementary insertion */
717
/* done after iLimit test, as candidates could be > iend-8 */
718
{ U32 const indexToInsert = curr+2;
719
hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
720
hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
721
hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
722
hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
723
}
724
725
/* check immediate repcode */
726
while (ip <= ilimit) {
727
U32 const current2 = (U32)(ip-base);
728
U32 const repIndex2 = current2 - offset_2;
729
const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
730
if ( ((ZSTD_index_overlap_check(prefixStartIndex, repIndex2))
731
& (offset_2 <= current2 - dictStartIndex))
732
&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
733
const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
734
size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
735
U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
736
ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
737
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
738
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
739
ip += repLength2;
740
anchor = ip;
741
continue;
742
}
743
break;
744
} } }
745
746
/* save reps for next block */
747
rep[0] = offset_1;
748
rep[1] = offset_2;
749
750
/* Return the last literals size */
751
return (size_t)(iend - anchor);
752
}
753
754
ZSTD_GEN_DFAST_FN(extDict, 4)
755
ZSTD_GEN_DFAST_FN(extDict, 5)
756
ZSTD_GEN_DFAST_FN(extDict, 6)
757
ZSTD_GEN_DFAST_FN(extDict, 7)
758
759
size_t ZSTD_compressBlock_doubleFast_extDict(
760
ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
761
void const* src, size_t srcSize)
762
{
763
U32 const mls = ms->cParams.minMatch;
764
switch(mls)
765
{
766
default: /* includes case 3 */
767
case 4 :
768
return ZSTD_compressBlock_doubleFast_extDict_4(ms, seqStore, rep, src, srcSize);
769
case 5 :
770
return ZSTD_compressBlock_doubleFast_extDict_5(ms, seqStore, rep, src, srcSize);
771
case 6 :
772
return ZSTD_compressBlock_doubleFast_extDict_6(ms, seqStore, rep, src, srcSize);
773
case 7 :
774
return ZSTD_compressBlock_doubleFast_extDict_7(ms, seqStore, rep, src, srcSize);
775
}
776
}
777
778
#endif /* ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR */
779
780