Coverage Report

Created: 2024-09-08 06:26

/src/zstd/lib/compress/zstd_fast.c
Line
Count
Source (jump to first uncovered line)
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"  /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */
12
#include "zstd_fast.h"
13
14
static
15
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
16
void ZSTD_fillHashTableForCDict(ZSTD_matchState_t* ms,
17
                        const void* const end,
18
                        ZSTD_dictTableLoadMethod_e dtlm)
19
0
{
20
0
    const ZSTD_compressionParameters* const cParams = &ms->cParams;
21
0
    U32* const hashTable = ms->hashTable;
22
0
    U32  const hBits = cParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
23
0
    U32  const mls = cParams->minMatch;
24
0
    const BYTE* const base = ms->window.base;
25
0
    const BYTE* ip = base + ms->nextToUpdate;
26
0
    const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
27
0
    const U32 fastHashFillStep = 3;
28
29
    /* Currently, we always use ZSTD_dtlm_full for filling CDict tables.
30
     * Feel free to remove this assert if there's a good reason! */
31
0
    assert(dtlm == ZSTD_dtlm_full);
32
33
    /* Always insert every fastHashFillStep position into the hash table.
34
     * Insert the other positions if their hash entry is empty.
35
     */
36
0
    for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {
37
0
        U32 const curr = (U32)(ip - base);
38
0
        {   size_t const hashAndTag = ZSTD_hashPtr(ip, hBits, mls);
39
0
            ZSTD_writeTaggedIndex(hashTable, hashAndTag, curr);   }
40
41
0
        if (dtlm == ZSTD_dtlm_fast) continue;
42
        /* Only load extra positions for ZSTD_dtlm_full */
43
0
        {   U32 p;
44
0
            for (p = 1; p < fastHashFillStep; ++p) {
45
0
                size_t const hashAndTag = ZSTD_hashPtr(ip + p, hBits, mls);
46
0
                if (hashTable[hashAndTag >> ZSTD_SHORT_CACHE_TAG_BITS] == 0) {  /* not yet filled */
47
0
                    ZSTD_writeTaggedIndex(hashTable, hashAndTag, curr + p);
48
0
                }   }   }   }
49
0
}
50
51
static
52
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
53
void ZSTD_fillHashTableForCCtx(ZSTD_matchState_t* ms,
54
                        const void* const end,
55
                        ZSTD_dictTableLoadMethod_e dtlm)
56
311k
{
57
311k
    const ZSTD_compressionParameters* const cParams = &ms->cParams;
58
311k
    U32* const hashTable = ms->hashTable;
59
311k
    U32  const hBits = cParams->hashLog;
60
311k
    U32  const mls = cParams->minMatch;
61
311k
    const BYTE* const base = ms->window.base;
62
311k
    const BYTE* ip = base + ms->nextToUpdate;
63
311k
    const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
64
311k
    const U32 fastHashFillStep = 3;
65
66
    /* Currently, we always use ZSTD_dtlm_fast for filling CCtx tables.
67
     * Feel free to remove this assert if there's a good reason! */
68
311k
    assert(dtlm == ZSTD_dtlm_fast);
69
70
    /* Always insert every fastHashFillStep position into the hash table.
71
     * Insert the other positions if their hash entry is empty.
72
     */
73
50.3M
    for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {
74
50.0M
        U32 const curr = (U32)(ip - base);
75
50.0M
        size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);
76
50.0M
        hashTable[hash0] = curr;
77
50.0M
        if (dtlm == ZSTD_dtlm_fast) continue;
78
        /* Only load extra positions for ZSTD_dtlm_full */
79
0
        {   U32 p;
80
0
            for (p = 1; p < fastHashFillStep; ++p) {
81
0
                size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls);
82
0
                if (hashTable[hash] == 0) {  /* not yet filled */
83
0
                    hashTable[hash] = curr + p;
84
0
    }   }   }   }
85
311k
}
86
87
void ZSTD_fillHashTable(ZSTD_matchState_t* ms,
88
                        const void* const end,
89
                        ZSTD_dictTableLoadMethod_e dtlm,
90
                        ZSTD_tableFillPurpose_e tfp)
91
311k
{
92
311k
    if (tfp == ZSTD_tfp_forCDict) {
93
0
        ZSTD_fillHashTableForCDict(ms, end, dtlm);
94
311k
    } else {
95
311k
        ZSTD_fillHashTableForCCtx(ms, end, dtlm);
96
311k
    }
97
311k
}
98
99
100
/**
101
 * If you squint hard enough (and ignore repcodes), the search operation at any
102
 * given position is broken into 4 stages:
103
 *
104
 * 1. Hash   (map position to hash value via input read)
105
 * 2. Lookup (map hash val to index via hashtable read)
106
 * 3. Load   (map index to value at that position via input read)
107
 * 4. Compare
108
 *
109
 * Each of these steps involves a memory read at an address which is computed
110
 * from the previous step. This means these steps must be sequenced and their
111
 * latencies are cumulative.
112
 *
113
 * Rather than do 1->2->3->4 sequentially for a single position before moving
114
 * onto the next, this implementation interleaves these operations across the
115
 * next few positions:
116
 *
117
 * R = Repcode Read & Compare
118
 * H = Hash
119
 * T = Table Lookup
120
 * M = Match Read & Compare
121
 *
122
 * Pos | Time -->
123
 * ----+-------------------
124
 * N   | ... M
125
 * N+1 | ...   TM
126
 * N+2 |    R H   T M
127
 * N+3 |         H    TM
128
 * N+4 |           R H   T M
129
 * N+5 |                H   ...
130
 * N+6 |                  R ...
131
 *
132
 * This is very much analogous to the pipelining of execution in a CPU. And just
133
 * like a CPU, we have to dump the pipeline when we find a match (i.e., take a
134
 * branch).
135
 *
136
 * When this happens, we throw away our current state, and do the following prep
137
 * to re-enter the loop:
138
 *
139
 * Pos | Time -->
140
 * ----+-------------------
141
 * N   | H T
142
 * N+1 |  H
143
 *
144
 * This is also the work we do at the beginning to enter the loop initially.
145
 */
146
FORCE_INLINE_TEMPLATE
147
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
148
size_t ZSTD_compressBlock_fast_noDict_generic(
149
        ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
150
        void const* src, size_t srcSize,
151
        U32 const mls, U32 const hasStep)
152
341k
{
153
341k
    const ZSTD_compressionParameters* const cParams = &ms->cParams;
154
341k
    U32* const hashTable = ms->hashTable;
155
341k
    U32 const hlog = cParams->hashLog;
156
    /* support stepSize of 0 */
157
341k
    size_t const stepSize = hasStep ? (cParams->targetLength + !(cParams->targetLength) + 1) : 2;
158
341k
    const BYTE* const base = ms->window.base;
159
341k
    const BYTE* const istart = (const BYTE*)src;
160
341k
    const U32   endIndex = (U32)((size_t)(istart - base) + srcSize);
161
341k
    const U32   prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
162
341k
    const BYTE* const prefixStart = base + prefixStartIndex;
163
341k
    const BYTE* const iend = istart + srcSize;
164
341k
    const BYTE* const ilimit = iend - HASH_READ_SIZE;
165
166
341k
    const BYTE* anchor = istart;
167
341k
    const BYTE* ip0 = istart;
168
341k
    const BYTE* ip1;
169
341k
    const BYTE* ip2;
170
341k
    const BYTE* ip3;
171
341k
    U32 current0;
172
173
341k
    U32 rep_offset1 = rep[0];
174
341k
    U32 rep_offset2 = rep[1];
175
341k
    U32 offsetSaved1 = 0, offsetSaved2 = 0;
176
177
341k
    size_t hash0; /* hash for ip0 */
178
341k
    size_t hash1; /* hash for ip1 */
179
341k
    U32 idx; /* match idx for ip0 */
180
341k
    U32 mval; /* src value at match idx */
181
182
341k
    U32 offcode;
183
341k
    const BYTE* match0;
184
341k
    size_t mLength;
185
186
    /* ip0 and ip1 are always adjacent. The targetLength skipping and
187
     * uncompressibility acceleration is applied to every other position,
188
     * matching the behavior of #1562. step therefore represents the gap
189
     * between pairs of positions, from ip0 to ip2 or ip1 to ip3. */
190
341k
    size_t step;
191
341k
    const BYTE* nextStep;
192
341k
    const size_t kStepIncr = (1 << (kSearchStrength - 1));
193
194
341k
    DEBUGLOG(5, "ZSTD_compressBlock_fast_generic");
195
341k
    ip0 += (ip0 == prefixStart);
196
341k
    {   U32 const curr = (U32)(ip0 - base);
197
341k
        U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog);
198
341k
        U32 const maxRep = curr - windowLow;
199
341k
        if (rep_offset2 > maxRep) offsetSaved2 = rep_offset2, rep_offset2 = 0;
200
341k
        if (rep_offset1 > maxRep) offsetSaved1 = rep_offset1, rep_offset1 = 0;
201
341k
    }
202
203
    /* start each op */
204
1.56M
_start: /* Requires: ip0 */
205
206
1.56M
    step = stepSize;
207
1.56M
    nextStep = ip0 + kStepIncr;
208
209
    /* calculate positions, ip0 - anchor == 0, so we skip step calc */
210
1.56M
    ip1 = ip0 + 1;
211
1.56M
    ip2 = ip0 + step;
212
1.56M
    ip3 = ip2 + 1;
213
214
1.56M
    if (ip3 >= ilimit) {
215
307k
        goto _cleanup;
216
307k
    }
217
218
1.25M
    hash0 = ZSTD_hashPtr(ip0, hlog, mls);
219
1.25M
    hash1 = ZSTD_hashPtr(ip1, hlog, mls);
220
221
1.25M
    idx = hashTable[hash0];
222
223
4.74M
    do {
224
        /* load repcode match for ip[2]*/
225
4.74M
        const U32 rval = MEM_read32(ip2 - rep_offset1);
226
227
        /* write back hash table entry */
228
4.74M
        current0 = (U32)(ip0 - base);
229
4.74M
        hashTable[hash0] = current0;
230
231
        /* check repcode at ip[2] */
232
4.74M
        if ((MEM_read32(ip2) == rval) & (rep_offset1 > 0)) {
233
645k
            ip0 = ip2;
234
645k
            match0 = ip0 - rep_offset1;
235
645k
            mLength = ip0[-1] == match0[-1];
236
645k
            ip0 -= mLength;
237
645k
            match0 -= mLength;
238
645k
            offcode = REPCODE1_TO_OFFBASE;
239
0
            mLength += 4;
240
241
            /* First write next hash table entry; we've already calculated it.
242
             * This write is known to be safe because the ip1 is before the
243
             * repcode (ip2). */
244
645k
            hashTable[hash1] = (U32)(ip1 - base);
245
246
645k
            goto _match;
247
645k
        }
248
249
        /* load match for ip[0] */
250
4.09M
        if (idx >= prefixStartIndex) {
251
3.31M
            mval = MEM_read32(base + idx);
252
3.31M
        } else {
253
786k
            mval = MEM_read32(ip0) ^ 1; /* guaranteed to not match. */
254
786k
        }
255
256
        /* check match at ip[0] */
257
4.09M
        if (MEM_read32(ip0) == mval) {
258
            /* found a match! */
259
260
            /* First write next hash table entry; we've already calculated it.
261
             * This write is known to be safe because the ip1 == ip0 + 1, so
262
             * we know we will resume searching after ip1 */
263
372k
            hashTable[hash1] = (U32)(ip1 - base);
264
265
372k
            goto _offset;
266
372k
        }
267
268
        /* lookup ip[1] */
269
3.72M
        idx = hashTable[hash1];
270
271
        /* hash ip[2] */
272
3.72M
        hash0 = hash1;
273
3.72M
        hash1 = ZSTD_hashPtr(ip2, hlog, mls);
274
275
        /* advance to next positions */
276
3.72M
        ip0 = ip1;
277
3.72M
        ip1 = ip2;
278
3.72M
        ip2 = ip3;
279
280
        /* write back hash table entry */
281
3.72M
        current0 = (U32)(ip0 - base);
282
3.72M
        hashTable[hash0] = current0;
283
284
        /* load match for ip[0] */
285
3.72M
        if (idx >= prefixStartIndex) {
286
3.00M
            mval = MEM_read32(base + idx);
287
3.00M
        } else {
288
725k
            mval = MEM_read32(ip0) ^ 1; /* guaranteed to not match. */
289
725k
        }
290
291
        /* check match at ip[0] */
292
3.72M
        if (MEM_read32(ip0) == mval) {
293
            /* found a match! */
294
295
            /* first write next hash table entry; we've already calculated it */
296
208k
            if (step <= 4) {
297
                /* We need to avoid writing an index into the hash table >= the
298
                 * position at which we will pick up our searching after we've
299
                 * taken this match.
300
                 *
301
                 * The minimum possible match has length 4, so the earliest ip0
302
                 * can be after we take this match will be the current ip0 + 4.
303
                 * ip1 is ip0 + step - 1. If ip1 is >= ip0 + 4, we can't safely
304
                 * write this position.
305
                 */
306
162k
                hashTable[hash1] = (U32)(ip1 - base);
307
162k
            }
308
309
208k
            goto _offset;
310
208k
        }
311
312
        /* lookup ip[1] */
313
3.51M
        idx = hashTable[hash1];
314
315
        /* hash ip[2] */
316
3.51M
        hash0 = hash1;
317
3.51M
        hash1 = ZSTD_hashPtr(ip2, hlog, mls);
318
319
        /* advance to next positions */
320
3.51M
        ip0 = ip1;
321
3.51M
        ip1 = ip2;
322
3.51M
        ip2 = ip0 + step;
323
3.51M
        ip3 = ip1 + step;
324
325
        /* calculate step */
326
3.51M
        if (ip2 >= nextStep) {
327
93.8k
            step++;
328
93.8k
            PREFETCH_L1(ip1 + 64);
329
93.8k
            PREFETCH_L1(ip1 + 128);
330
93.8k
            nextStep += kStepIncr;
331
93.8k
        }
332
3.51M
    } while (ip3 < ilimit);
333
334
341k
_cleanup:
335
    /* Note that there are probably still a couple positions we could search.
336
     * However, it seems to be a meaningful performance hit to try to search
337
     * them. So let's not. */
338
339
    /* When the repcodes are outside of the prefix, we set them to zero before the loop.
340
     * When the offsets are still zero, we need to restore them after the block to have a correct
341
     * repcode history. If only one offset was invalid, it is easy. The tricky case is when both
342
     * offsets were invalid. We need to figure out which offset to refill with.
343
     *     - If both offsets are zero they are in the same order.
344
     *     - If both offsets are non-zero, we won't restore the offsets from `offsetSaved[12]`.
345
     *     - If only one is zero, we need to decide which offset to restore.
346
     *         - If rep_offset1 is non-zero, then rep_offset2 must be offsetSaved1.
347
     *         - It is impossible for rep_offset2 to be non-zero.
348
     *
349
     * So if rep_offset1 started invalid (offsetSaved1 != 0) and became valid (rep_offset1 != 0), then
350
     * set rep[0] = rep_offset1 and rep[1] = offsetSaved1.
351
     */
352
341k
    offsetSaved2 = ((offsetSaved1 != 0) && (rep_offset1 != 0)) ? offsetSaved1 : offsetSaved2;
353
354
    /* save reps for next block */
355
341k
    rep[0] = rep_offset1 ? rep_offset1 : offsetSaved1;
356
341k
    rep[1] = rep_offset2 ? rep_offset2 : offsetSaved2;
357
358
    /* Return the last literals size */
359
341k
    return (size_t)(iend - anchor);
360
361
580k
_offset: /* Requires: ip0, idx */
362
363
    /* Compute the offset code. */
364
580k
    match0 = base + idx;
365
580k
    rep_offset2 = rep_offset1;
366
580k
    rep_offset1 = (U32)(ip0-match0);
367
580k
    offcode = OFFSET_TO_OFFBASE(rep_offset1);
368
0
    mLength = 4;
369
370
    /* Count the backwards match length. */
371
873k
    while (((ip0>anchor) & (match0>prefixStart)) && (ip0[-1] == match0[-1])) {
372
292k
        ip0--;
373
292k
        match0--;
374
292k
        mLength++;
375
292k
    }
376
377
1.22M
_match: /* Requires: ip0, match0, offcode */
378
379
    /* Count the forward length. */
380
1.22M
    mLength += ZSTD_count(ip0 + mLength, match0 + mLength, iend);
381
382
1.22M
    ZSTD_storeSeq(seqStore, (size_t)(ip0 - anchor), anchor, iend, offcode, mLength);
383
384
1.22M
    ip0 += mLength;
385
1.22M
    anchor = ip0;
386
387
    /* Fill table and check for immediate repcode. */
388
1.22M
    if (ip0 <= ilimit) {
389
        /* Fill Table */
390
1.15M
        assert(base+current0+2 > istart);  /* check base overflow */
391
1.15M
        hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2;  /* here because current+2 could be > iend-8 */
392
1.15M
        hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
393
394
1.15M
        if (rep_offset2 > 0) { /* rep_offset2==0 means rep_offset2 is invalidated */
395
1.30M
            while ( (ip0 <= ilimit) && (MEM_read32(ip0) == MEM_read32(ip0 - rep_offset2)) ) {
396
                /* store sequence */
397
163k
                size_t const rLength = ZSTD_count(ip0+4, ip0+4-rep_offset2, iend) + 4;
398
163k
                { U32 const tmpOff = rep_offset2; rep_offset2 = rep_offset1; rep_offset1 = tmpOff; } /* swap rep_offset2 <=> rep_offset1 */
399
163k
                hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);
400
163k
                ip0 += rLength;
401
163k
                ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, REPCODE1_TO_OFFBASE, rLength);
402
0
                anchor = ip0;
403
163k
                continue;   /* faster when present (confirmed on gcc-8) ... (?) */
404
163k
    }   }   }
405
406
1.22M
    goto _start;
407
1.22M
}
408
409
#define ZSTD_GEN_FAST_FN(dictMode, mls, step)                                                            \
410
    static size_t ZSTD_compressBlock_fast_##dictMode##_##mls##_##step(                                      \
411
            ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],                    \
412
            void const* src, size_t srcSize)                                                       \
413
341k
    {                                                                                              \
414
341k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
341k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_4_1
Line
Count
Source
413
84.9k
    {                                                                                              \
414
84.9k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
84.9k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_5_1
Line
Count
Source
413
38.4k
    {                                                                                              \
414
38.4k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
38.4k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_6_1
Line
Count
Source
413
30.1k
    {                                                                                              \
414
30.1k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
30.1k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_7_1
Line
Count
Source
413
46.2k
    {                                                                                              \
414
46.2k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
46.2k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_4_0
Line
Count
Source
413
25.6k
    {                                                                                              \
414
25.6k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
25.6k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_5_0
Line
Count
Source
413
36.4k
    {                                                                                              \
414
36.4k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
36.4k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_6_0
Line
Count
Source
413
43.2k
    {                                                                                              \
414
43.2k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
43.2k
    }
zstd_fast.c:ZSTD_compressBlock_fast_noDict_7_0
Line
Count
Source
413
36.3k
    {                                                                                              \
414
36.3k
        return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \
415
36.3k
    }
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_4_0
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_5_0
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_6_0
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_7_0
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_extDict_4_0
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_extDict_5_0
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_extDict_6_0
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_extDict_7_0
416
417
ZSTD_GEN_FAST_FN(noDict, 4, 1)
418
ZSTD_GEN_FAST_FN(noDict, 5, 1)
419
ZSTD_GEN_FAST_FN(noDict, 6, 1)
420
ZSTD_GEN_FAST_FN(noDict, 7, 1)
421
422
ZSTD_GEN_FAST_FN(noDict, 4, 0)
423
ZSTD_GEN_FAST_FN(noDict, 5, 0)
424
ZSTD_GEN_FAST_FN(noDict, 6, 0)
425
ZSTD_GEN_FAST_FN(noDict, 7, 0)
426
427
size_t ZSTD_compressBlock_fast(
428
        ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
429
        void const* src, size_t srcSize)
430
341k
{
431
341k
    U32 const mls = ms->cParams.minMatch;
432
341k
    assert(ms->dictMatchState == NULL);
433
341k
    if (ms->cParams.targetLength > 1) {
434
199k
        switch(mls)
435
199k
        {
436
52.5k
        default: /* includes case 3 */
437
84.9k
        case 4 :
438
84.9k
            return ZSTD_compressBlock_fast_noDict_4_1(ms, seqStore, rep, src, srcSize);
439
38.4k
        case 5 :
440
38.4k
            return ZSTD_compressBlock_fast_noDict_5_1(ms, seqStore, rep, src, srcSize);
441
30.1k
        case 6 :
442
30.1k
            return ZSTD_compressBlock_fast_noDict_6_1(ms, seqStore, rep, src, srcSize);
443
46.2k
        case 7 :
444
46.2k
            return ZSTD_compressBlock_fast_noDict_7_1(ms, seqStore, rep, src, srcSize);
445
199k
        }
446
199k
    } else {
447
141k
        switch(mls)
448
141k
        {
449
13.5k
        default: /* includes case 3 */
450
25.6k
        case 4 :
451
25.6k
            return ZSTD_compressBlock_fast_noDict_4_0(ms, seqStore, rep, src, srcSize);
452
36.4k
        case 5 :
453
36.4k
            return ZSTD_compressBlock_fast_noDict_5_0(ms, seqStore, rep, src, srcSize);
454
43.2k
        case 6 :
455
43.2k
            return ZSTD_compressBlock_fast_noDict_6_0(ms, seqStore, rep, src, srcSize);
456
36.3k
        case 7 :
457
36.3k
            return ZSTD_compressBlock_fast_noDict_7_0(ms, seqStore, rep, src, srcSize);
458
141k
        }
459
460
141k
    }
461
341k
}
462
463
FORCE_INLINE_TEMPLATE
464
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
465
size_t ZSTD_compressBlock_fast_dictMatchState_generic(
466
        ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
467
        void const* src, size_t srcSize, U32 const mls, U32 const hasStep)
468
0
{
469
0
    const ZSTD_compressionParameters* const cParams = &ms->cParams;
470
0
    U32* const hashTable = ms->hashTable;
471
0
    U32 const hlog = cParams->hashLog;
472
    /* support stepSize of 0 */
473
0
    U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
474
0
    const BYTE* const base = ms->window.base;
475
0
    const BYTE* const istart = (const BYTE*)src;
476
0
    const BYTE* ip0 = istart;
477
0
    const BYTE* ip1 = ip0 + stepSize; /* we assert below that stepSize >= 1 */
478
0
    const BYTE* anchor = istart;
479
0
    const U32   prefixStartIndex = ms->window.dictLimit;
480
0
    const BYTE* const prefixStart = base + prefixStartIndex;
481
0
    const BYTE* const iend = istart + srcSize;
482
0
    const BYTE* const ilimit = iend - HASH_READ_SIZE;
483
0
    U32 offset_1=rep[0], offset_2=rep[1];
484
485
0
    const ZSTD_matchState_t* const dms = ms->dictMatchState;
486
0
    const ZSTD_compressionParameters* const dictCParams = &dms->cParams ;
487
0
    const U32* const dictHashTable = dms->hashTable;
488
0
    const U32 dictStartIndex       = dms->window.dictLimit;
489
0
    const BYTE* const dictBase     = dms->window.base;
490
0
    const BYTE* const dictStart    = dictBase + dictStartIndex;
491
0
    const BYTE* const dictEnd      = dms->window.nextSrc;
492
0
    const U32 dictIndexDelta       = prefixStartIndex - (U32)(dictEnd - dictBase);
493
0
    const U32 dictAndPrefixLength  = (U32)(istart - prefixStart + dictEnd - dictStart);
494
0
    const U32 dictHBits            = dictCParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS;
495
496
    /* if a dictionary is still attached, it necessarily means that
497
     * it is within window size. So we just check it. */
498
0
    const U32 maxDistance = 1U << cParams->windowLog;
499
0
    const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
500
0
    assert(endIndex - prefixStartIndex <= maxDistance);
501
0
    (void)maxDistance; (void)endIndex;   /* these variables are not used when assert() is disabled */
502
503
0
    (void)hasStep; /* not currently specialized on whether it's accelerated */
504
505
    /* ensure there will be no underflow
506
     * when translating a dict index into a local index */
507
0
    assert(prefixStartIndex >= (U32)(dictEnd - dictBase));
508
509
0
    if (ms->prefetchCDictTables) {
510
0
        size_t const hashTableBytes = (((size_t)1) << dictCParams->hashLog) * sizeof(U32);
511
0
        PREFETCH_AREA(dictHashTable, hashTableBytes);
512
0
    }
513
514
    /* init */
515
0
    DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic");
516
0
    ip0 += (dictAndPrefixLength == 0);
517
    /* dictMatchState repCode checks don't currently handle repCode == 0
518
     * disabling. */
519
0
    assert(offset_1 <= dictAndPrefixLength);
520
0
    assert(offset_2 <= dictAndPrefixLength);
521
522
    /* Outer search loop */
523
0
    assert(stepSize >= 1);
524
0
    while (ip1 <= ilimit) {   /* repcode check at (ip0 + 1) is safe because ip0 < ip1 */
525
0
        size_t mLength;
526
0
        size_t hash0 = ZSTD_hashPtr(ip0, hlog, mls);
527
528
0
        size_t const dictHashAndTag0 = ZSTD_hashPtr(ip0, dictHBits, mls);
529
0
        U32 dictMatchIndexAndTag = dictHashTable[dictHashAndTag0 >> ZSTD_SHORT_CACHE_TAG_BITS];
530
0
        int dictTagsMatch = ZSTD_comparePackedTags(dictMatchIndexAndTag, dictHashAndTag0);
531
532
0
        U32 matchIndex = hashTable[hash0];
533
0
        U32 curr = (U32)(ip0 - base);
534
0
        size_t step = stepSize;
535
0
        const size_t kStepIncr = 1 << kSearchStrength;
536
0
        const BYTE* nextStep = ip0 + kStepIncr;
537
538
        /* Inner search loop */
539
0
        while (1) {
540
0
            const BYTE* match = base + matchIndex;
541
0
            const U32 repIndex = curr + 1 - offset_1;
542
0
            const BYTE* repMatch = (repIndex < prefixStartIndex) ?
543
0
                                   dictBase + (repIndex - dictIndexDelta) :
544
0
                                   base + repIndex;
545
0
            const size_t hash1 = ZSTD_hashPtr(ip1, hlog, mls);
546
0
            size_t const dictHashAndTag1 = ZSTD_hashPtr(ip1, dictHBits, mls);
547
0
            hashTable[hash0] = curr;   /* update hash table */
548
549
0
            if ((ZSTD_index_overlap_check(prefixStartIndex, repIndex)) 
550
0
                && (MEM_read32(repMatch) == MEM_read32(ip0 + 1))) {
551
0
                const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
552
0
                mLength = ZSTD_count_2segments(ip0 + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4;
553
0
                ip0++;
554
0
                ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength);
555
0
                break;
556
0
            }
557
558
0
            if (dictTagsMatch) {
559
                /* Found a possible dict match */
560
0
                const U32 dictMatchIndex = dictMatchIndexAndTag >> ZSTD_SHORT_CACHE_TAG_BITS;
561
0
                const BYTE* dictMatch = dictBase + dictMatchIndex;
562
0
                if (dictMatchIndex > dictStartIndex &&
563
0
                    MEM_read32(dictMatch) == MEM_read32(ip0)) {
564
                    /* To replicate extDict parse behavior, we only use dict matches when the normal matchIndex is invalid */
565
0
                    if (matchIndex <= prefixStartIndex) {
566
0
                        U32 const offset = (U32) (curr - dictMatchIndex - dictIndexDelta);
567
0
                        mLength = ZSTD_count_2segments(ip0 + 4, dictMatch + 4, iend, dictEnd, prefixStart) + 4;
568
0
                        while (((ip0 > anchor) & (dictMatch > dictStart))
569
0
                            && (ip0[-1] == dictMatch[-1])) {
570
0
                            ip0--;
571
0
                            dictMatch--;
572
0
                            mLength++;
573
0
                        } /* catch up */
574
0
                        offset_2 = offset_1;
575
0
                        offset_1 = offset;
576
0
                        ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
577
0
                        break;
578
0
                    }
579
0
                }
580
0
            }
581
582
0
            if (matchIndex > prefixStartIndex && MEM_read32(match) == MEM_read32(ip0)) {
583
                /* found a regular match */
584
0
                U32 const offset = (U32) (ip0 - match);
585
0
                mLength = ZSTD_count(ip0 + 4, match + 4, iend) + 4;
586
0
                while (((ip0 > anchor) & (match > prefixStart))
587
0
                       && (ip0[-1] == match[-1])) {
588
0
                    ip0--;
589
0
                    match--;
590
0
                    mLength++;
591
0
                } /* catch up */
592
0
                offset_2 = offset_1;
593
0
                offset_1 = offset;
594
0
                ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength);
595
0
                break;
596
0
            }
597
598
            /* Prepare for next iteration */
599
0
            dictMatchIndexAndTag = dictHashTable[dictHashAndTag1 >> ZSTD_SHORT_CACHE_TAG_BITS];
600
0
            dictTagsMatch = ZSTD_comparePackedTags(dictMatchIndexAndTag, dictHashAndTag1);
601
0
            matchIndex = hashTable[hash1];
602
603
0
            if (ip1 >= nextStep) {
604
0
                step++;
605
0
                nextStep += kStepIncr;
606
0
            }
607
0
            ip0 = ip1;
608
0
            ip1 = ip1 + step;
609
0
            if (ip1 > ilimit) goto _cleanup;
610
611
0
            curr = (U32)(ip0 - base);
612
0
            hash0 = hash1;
613
0
        }   /* end inner search loop */
614
615
        /* match found */
616
0
        assert(mLength);
617
0
        ip0 += mLength;
618
0
        anchor = ip0;
619
620
0
        if (ip0 <= ilimit) {
621
            /* Fill Table */
622
0
            assert(base+curr+2 > istart);  /* check base overflow */
623
0
            hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2;  /* here because curr+2 could be > iend-8 */
624
0
            hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
625
626
            /* check immediate repcode */
627
0
            while (ip0 <= ilimit) {
628
0
                U32 const current2 = (U32)(ip0-base);
629
0
                U32 const repIndex2 = current2 - offset_2;
630
0
                const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?
631
0
                        dictBase - dictIndexDelta + repIndex2 :
632
0
                        base + repIndex2;
633
0
                if ( (ZSTD_index_overlap_check(prefixStartIndex, repIndex2))
634
0
                   && (MEM_read32(repMatch2) == MEM_read32(ip0))) {
635
0
                    const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
636
0
                    size_t const repLength2 = ZSTD_count_2segments(ip0+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
637
0
                    U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
638
0
                    ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
639
0
                    hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = current2;
640
0
                    ip0 += repLength2;
641
0
                    anchor = ip0;
642
0
                    continue;
643
0
                }
644
0
                break;
645
0
            }
646
0
        }
647
648
        /* Prepare for next iteration */
649
0
        assert(ip0 == anchor);
650
0
        ip1 = ip0 + stepSize;
651
0
    }
652
653
0
_cleanup:
654
    /* save reps for next block */
655
0
    rep[0] = offset_1;
656
0
    rep[1] = offset_2;
657
658
    /* Return the last literals size */
659
0
    return (size_t)(iend - anchor);
660
0
}
661
662
663
ZSTD_GEN_FAST_FN(dictMatchState, 4, 0)
664
ZSTD_GEN_FAST_FN(dictMatchState, 5, 0)
665
ZSTD_GEN_FAST_FN(dictMatchState, 6, 0)
666
ZSTD_GEN_FAST_FN(dictMatchState, 7, 0)
667
668
size_t ZSTD_compressBlock_fast_dictMatchState(
669
        ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
670
        void const* src, size_t srcSize)
671
0
{
672
0
    U32 const mls = ms->cParams.minMatch;
673
0
    assert(ms->dictMatchState != NULL);
674
0
    switch(mls)
675
0
    {
676
0
    default: /* includes case 3 */
677
0
    case 4 :
678
0
        return ZSTD_compressBlock_fast_dictMatchState_4_0(ms, seqStore, rep, src, srcSize);
679
0
    case 5 :
680
0
        return ZSTD_compressBlock_fast_dictMatchState_5_0(ms, seqStore, rep, src, srcSize);
681
0
    case 6 :
682
0
        return ZSTD_compressBlock_fast_dictMatchState_6_0(ms, seqStore, rep, src, srcSize);
683
0
    case 7 :
684
0
        return ZSTD_compressBlock_fast_dictMatchState_7_0(ms, seqStore, rep, src, srcSize);
685
0
    }
686
0
}
687
688
689
static
690
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
691
size_t ZSTD_compressBlock_fast_extDict_generic(
692
        ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
693
        void const* src, size_t srcSize, U32 const mls, U32 const hasStep)
694
0
{
695
0
    const ZSTD_compressionParameters* const cParams = &ms->cParams;
696
0
    U32* const hashTable = ms->hashTable;
697
0
    U32 const hlog = cParams->hashLog;
698
    /* support stepSize of 0 */
699
0
    size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1;
700
0
    const BYTE* const base = ms->window.base;
701
0
    const BYTE* const dictBase = ms->window.dictBase;
702
0
    const BYTE* const istart = (const BYTE*)src;
703
0
    const BYTE* anchor = istart;
704
0
    const U32   endIndex = (U32)((size_t)(istart - base) + srcSize);
705
0
    const U32   lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
706
0
    const U32   dictStartIndex = lowLimit;
707
0
    const BYTE* const dictStart = dictBase + dictStartIndex;
708
0
    const U32   dictLimit = ms->window.dictLimit;
709
0
    const U32   prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit;
710
0
    const BYTE* const prefixStart = base + prefixStartIndex;
711
0
    const BYTE* const dictEnd = dictBase + prefixStartIndex;
712
0
    const BYTE* const iend = istart + srcSize;
713
0
    const BYTE* const ilimit = iend - 8;
714
0
    U32 offset_1=rep[0], offset_2=rep[1];
715
0
    U32 offsetSaved1 = 0, offsetSaved2 = 0;
716
717
0
    const BYTE* ip0 = istart;
718
0
    const BYTE* ip1;
719
0
    const BYTE* ip2;
720
0
    const BYTE* ip3;
721
0
    U32 current0;
722
723
724
0
    size_t hash0; /* hash for ip0 */
725
0
    size_t hash1; /* hash for ip1 */
726
0
    U32 idx; /* match idx for ip0 */
727
0
    const BYTE* idxBase; /* base pointer for idx */
728
729
0
    U32 offcode;
730
0
    const BYTE* match0;
731
0
    size_t mLength;
732
0
    const BYTE* matchEnd = 0; /* initialize to avoid warning, assert != 0 later */
733
734
0
    size_t step;
735
0
    const BYTE* nextStep;
736
0
    const size_t kStepIncr = (1 << (kSearchStrength - 1));
737
738
0
    (void)hasStep; /* not currently specialized on whether it's accelerated */
739
740
0
    DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic (offset_1=%u)", offset_1);
741
742
    /* switch to "regular" variant if extDict is invalidated due to maxDistance */
743
0
    if (prefixStartIndex == dictStartIndex)
744
0
        return ZSTD_compressBlock_fast(ms, seqStore, rep, src, srcSize);
745
746
0
    {   U32 const curr = (U32)(ip0 - base);
747
0
        U32 const maxRep = curr - dictStartIndex;
748
0
        if (offset_2 >= maxRep) offsetSaved2 = offset_2, offset_2 = 0;
749
0
        if (offset_1 >= maxRep) offsetSaved1 = offset_1, offset_1 = 0;
750
0
    }
751
752
    /* start each op */
753
0
_start: /* Requires: ip0 */
754
755
0
    step = stepSize;
756
0
    nextStep = ip0 + kStepIncr;
757
758
    /* calculate positions, ip0 - anchor == 0, so we skip step calc */
759
0
    ip1 = ip0 + 1;
760
0
    ip2 = ip0 + step;
761
0
    ip3 = ip2 + 1;
762
763
0
    if (ip3 >= ilimit) {
764
0
        goto _cleanup;
765
0
    }
766
767
0
    hash0 = ZSTD_hashPtr(ip0, hlog, mls);
768
0
    hash1 = ZSTD_hashPtr(ip1, hlog, mls);
769
770
0
    idx = hashTable[hash0];
771
0
    idxBase = idx < prefixStartIndex ? dictBase : base;
772
773
0
    do {
774
0
        {   /* load repcode match for ip[2] */
775
0
            U32 const current2 = (U32)(ip2 - base);
776
0
            U32 const repIndex = current2 - offset_1;
777
0
            const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
778
0
            U32 rval;
779
0
            if ( ((U32)(prefixStartIndex - repIndex) >= 4) /* intentional underflow */
780
0
                 & (offset_1 > 0) ) {
781
0
                rval = MEM_read32(repBase + repIndex);
782
0
            } else {
783
0
                rval = MEM_read32(ip2) ^ 1; /* guaranteed to not match. */
784
0
            }
785
786
            /* write back hash table entry */
787
0
            current0 = (U32)(ip0 - base);
788
0
            hashTable[hash0] = current0;
789
790
            /* check repcode at ip[2] */
791
0
            if (MEM_read32(ip2) == rval) {
792
0
                ip0 = ip2;
793
0
                match0 = repBase + repIndex;
794
0
                matchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
795
0
                assert((match0 != prefixStart) & (match0 != dictStart));
796
0
                mLength = ip0[-1] == match0[-1];
797
0
                ip0 -= mLength;
798
0
                match0 -= mLength;
799
0
                offcode = REPCODE1_TO_OFFBASE;
800
0
                mLength += 4;
801
0
                goto _match;
802
0
        }   }
803
804
0
        {   /* load match for ip[0] */
805
0
            U32 const mval = idx >= dictStartIndex ?
806
0
                    MEM_read32(idxBase + idx) :
807
0
                    MEM_read32(ip0) ^ 1; /* guaranteed not to match */
808
809
            /* check match at ip[0] */
810
0
            if (MEM_read32(ip0) == mval) {
811
                /* found a match! */
812
0
                goto _offset;
813
0
        }   }
814
815
        /* lookup ip[1] */
816
0
        idx = hashTable[hash1];
817
0
        idxBase = idx < prefixStartIndex ? dictBase : base;
818
819
        /* hash ip[2] */
820
0
        hash0 = hash1;
821
0
        hash1 = ZSTD_hashPtr(ip2, hlog, mls);
822
823
        /* advance to next positions */
824
0
        ip0 = ip1;
825
0
        ip1 = ip2;
826
0
        ip2 = ip3;
827
828
        /* write back hash table entry */
829
0
        current0 = (U32)(ip0 - base);
830
0
        hashTable[hash0] = current0;
831
832
0
        {   /* load match for ip[0] */
833
0
            U32 const mval = idx >= dictStartIndex ?
834
0
                    MEM_read32(idxBase + idx) :
835
0
                    MEM_read32(ip0) ^ 1; /* guaranteed not to match */
836
837
            /* check match at ip[0] */
838
0
            if (MEM_read32(ip0) == mval) {
839
                /* found a match! */
840
0
                goto _offset;
841
0
        }   }
842
843
        /* lookup ip[1] */
844
0
        idx = hashTable[hash1];
845
0
        idxBase = idx < prefixStartIndex ? dictBase : base;
846
847
        /* hash ip[2] */
848
0
        hash0 = hash1;
849
0
        hash1 = ZSTD_hashPtr(ip2, hlog, mls);
850
851
        /* advance to next positions */
852
0
        ip0 = ip1;
853
0
        ip1 = ip2;
854
0
        ip2 = ip0 + step;
855
0
        ip3 = ip1 + step;
856
857
        /* calculate step */
858
0
        if (ip2 >= nextStep) {
859
0
            step++;
860
0
            PREFETCH_L1(ip1 + 64);
861
0
            PREFETCH_L1(ip1 + 128);
862
0
            nextStep += kStepIncr;
863
0
        }
864
0
    } while (ip3 < ilimit);
865
866
0
_cleanup:
867
    /* Note that there are probably still a couple positions we could search.
868
     * However, it seems to be a meaningful performance hit to try to search
869
     * them. So let's not. */
870
871
    /* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0),
872
     * rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */
873
0
    offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2;
874
875
    /* save reps for next block */
876
0
    rep[0] = offset_1 ? offset_1 : offsetSaved1;
877
0
    rep[1] = offset_2 ? offset_2 : offsetSaved2;
878
879
    /* Return the last literals size */
880
0
    return (size_t)(iend - anchor);
881
882
0
_offset: /* Requires: ip0, idx, idxBase */
883
884
    /* Compute the offset code. */
885
0
    {   U32 const offset = current0 - idx;
886
0
        const BYTE* const lowMatchPtr = idx < prefixStartIndex ? dictStart : prefixStart;
887
0
        matchEnd = idx < prefixStartIndex ? dictEnd : iend;
888
0
        match0 = idxBase + idx;
889
0
        offset_2 = offset_1;
890
0
        offset_1 = offset;
891
0
        offcode = OFFSET_TO_OFFBASE(offset);
892
0
        mLength = 4;
893
894
        /* Count the backwards match length. */
895
0
        while (((ip0>anchor) & (match0>lowMatchPtr)) && (ip0[-1] == match0[-1])) {
896
0
            ip0--;
897
0
            match0--;
898
0
            mLength++;
899
0
    }   }
900
901
0
_match: /* Requires: ip0, match0, offcode, matchEnd */
902
903
    /* Count the forward length. */
904
0
    assert(matchEnd != 0);
905
0
    mLength += ZSTD_count_2segments(ip0 + mLength, match0 + mLength, iend, matchEnd, prefixStart);
906
907
0
    ZSTD_storeSeq(seqStore, (size_t)(ip0 - anchor), anchor, iend, offcode, mLength);
908
909
0
    ip0 += mLength;
910
0
    anchor = ip0;
911
912
    /* write next hash table entry */
913
0
    if (ip1 < ip0) {
914
0
        hashTable[hash1] = (U32)(ip1 - base);
915
0
    }
916
917
    /* Fill table and check for immediate repcode. */
918
0
    if (ip0 <= ilimit) {
919
        /* Fill Table */
920
0
        assert(base+current0+2 > istart);  /* check base overflow */
921
0
        hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2;  /* here because current+2 could be > iend-8 */
922
0
        hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
923
924
0
        while (ip0 <= ilimit) {
925
0
            U32 const repIndex2 = (U32)(ip0-base) - offset_2;
926
0
            const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
927
0
            if ( ((ZSTD_index_overlap_check(prefixStartIndex, repIndex2)) & (offset_2 > 0))
928
0
                 && (MEM_read32(repMatch2) == MEM_read32(ip0)) ) {
929
0
                const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
930
0
                size_t const repLength2 = ZSTD_count_2segments(ip0+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
931
0
                { U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; }  /* swap offset_2 <=> offset_1 */
932
0
                ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, REPCODE1_TO_OFFBASE, repLength2);
933
0
                hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);
934
0
                ip0 += repLength2;
935
0
                anchor = ip0;
936
0
                continue;
937
0
            }
938
0
            break;
939
0
    }   }
940
941
0
    goto _start;
942
0
}
943
944
ZSTD_GEN_FAST_FN(extDict, 4, 0)
945
ZSTD_GEN_FAST_FN(extDict, 5, 0)
946
ZSTD_GEN_FAST_FN(extDict, 6, 0)
947
ZSTD_GEN_FAST_FN(extDict, 7, 0)
948
949
size_t ZSTD_compressBlock_fast_extDict(
950
        ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
951
        void const* src, size_t srcSize)
952
0
{
953
0
    U32 const mls = ms->cParams.minMatch;
954
0
    assert(ms->dictMatchState == NULL);
955
0
    switch(mls)
956
0
    {
957
0
    default: /* includes case 3 */
958
0
    case 4 :
959
0
        return ZSTD_compressBlock_fast_extDict_4_0(ms, seqStore, rep, src, srcSize);
960
0
    case 5 :
961
0
        return ZSTD_compressBlock_fast_extDict_5_0(ms, seqStore, rep, src, srcSize);
962
0
    case 6 :
963
0
        return ZSTD_compressBlock_fast_extDict_6_0(ms, seqStore, rep, src, srcSize);
964
0
    case 7 :
965
0
        return ZSTD_compressBlock_fast_extDict_7_0(ms, seqStore, rep, src, srcSize);
966
0
    }
967
0
}