xref: /freebsd/sys/contrib/openzfs/module/zstd/lib/compress/zstd_double_fast.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under both the BSD-style license (found in the
7  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8  * in the COPYING file in the root directory of this source tree).
9  * You may select, at your option, one of the above-listed licenses.
10  */
11 
12 #include "zstd_compress_internal.h"
13 #include "zstd_double_fast.h"
14 
15 
ZSTD_fillDoubleHashTable(ZSTD_matchState_t * ms,void const * end,ZSTD_dictTableLoadMethod_e dtlm)16 void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
17                               void const* end, ZSTD_dictTableLoadMethod_e dtlm)
18 {
19     const ZSTD_compressionParameters* const cParams = &ms->cParams;
20     U32* const hashLarge = ms->hashTable;
21     U32  const hBitsL = cParams->hashLog;
22     U32  const mls = cParams->minMatch;
23     U32* const hashSmall = ms->chainTable;
24     U32  const hBitsS = cParams->chainLog;
25     const BYTE* const base = ms->window.base;
26     const BYTE* ip = base + ms->nextToUpdate;
27     const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
28     const U32 fastHashFillStep = 3;
29 
30     /* Always insert every fastHashFillStep position into the hash tables.
31      * Insert the other positions into the large hash table if their entry
32      * is empty.
33      */
34     for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
35         U32 const current = (U32)(ip - base);
36         U32 i;
37         for (i = 0; i < fastHashFillStep; ++i) {
38             size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
39             size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
40             if (i == 0)
41                 hashSmall[smHash] = current + i;
42             if (i == 0 || hashLarge[lgHash] == 0)
43                 hashLarge[lgHash] = current + i;
44             /* Only load extra positions for ZSTD_dtlm_full */
45             if (dtlm == ZSTD_dtlm_fast)
46                 break;
47     }   }
48 }
49 
50 
51 FORCE_INLINE_TEMPLATE
ZSTD_compressBlock_doubleFast_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize,U32 const mls,ZSTD_dictMode_e const dictMode)52 size_t ZSTD_compressBlock_doubleFast_generic(
53         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
54         void const* src, size_t srcSize,
55         U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
56 {
57     ZSTD_compressionParameters const* cParams = &ms->cParams;
58     U32* const hashLong = ms->hashTable;
59     const U32 hBitsL = cParams->hashLog;
60     U32* const hashSmall = ms->chainTable;
61     const U32 hBitsS = cParams->chainLog;
62     const BYTE* const base = ms->window.base;
63     const BYTE* const istart = (const BYTE*)src;
64     const BYTE* ip = istart;
65     const BYTE* anchor = istart;
66     const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
67     /* presumes that, if there is a dictionary, it must be using Attach mode */
68     const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
69     const BYTE* const prefixLowest = base + prefixLowestIndex;
70     const BYTE* const iend = istart + srcSize;
71     const BYTE* const ilimit = iend - HASH_READ_SIZE;
72     U32 offset_1=rep[0], offset_2=rep[1];
73     U32 offsetSaved = 0;
74 
75     const ZSTD_matchState_t* const dms = ms->dictMatchState;
76     const ZSTD_compressionParameters* const dictCParams =
77                                      dictMode == ZSTD_dictMatchState ?
78                                      &dms->cParams : NULL;
79     const U32* const dictHashLong  = dictMode == ZSTD_dictMatchState ?
80                                      dms->hashTable : NULL;
81     const U32* const dictHashSmall = dictMode == ZSTD_dictMatchState ?
82                                      dms->chainTable : NULL;
83     const U32 dictStartIndex       = dictMode == ZSTD_dictMatchState ?
84                                      dms->window.dictLimit : 0;
85     const BYTE* const dictBase     = dictMode == ZSTD_dictMatchState ?
86                                      dms->window.base : NULL;
87     const BYTE* const dictStart    = dictMode == ZSTD_dictMatchState ?
88                                      dictBase + dictStartIndex : NULL;
89     const BYTE* const dictEnd      = dictMode == ZSTD_dictMatchState ?
90                                      dms->window.nextSrc : NULL;
91     const U32 dictIndexDelta       = dictMode == ZSTD_dictMatchState ?
92                                      prefixLowestIndex - (U32)(dictEnd - dictBase) :
93                                      0;
94     const U32 dictHBitsL           = dictMode == ZSTD_dictMatchState ?
95                                      dictCParams->hashLog : hBitsL;
96     const U32 dictHBitsS           = dictMode == ZSTD_dictMatchState ?
97                                      dictCParams->chainLog : hBitsS;
98     const U32 dictAndPrefixLength  = (U32)((ip - prefixLowest) + (dictEnd - dictStart));
99 
100     DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_generic");
101 
102     assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
103 
104     /* if a dictionary is attached, it must be within window range */
105     if (dictMode == ZSTD_dictMatchState) {
106         assert(ms->window.dictLimit + (1U << cParams->windowLog) >= endIndex);
107     }
108 
109     /* init */
110     ip += (dictAndPrefixLength == 0);
111     if (dictMode == ZSTD_noDict) {
112         U32 const current = (U32)(ip - base);
113         U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, current, cParams->windowLog);
114         U32 const maxRep = current - windowLow;
115         if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
116         if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
117     }
118     if (dictMode == ZSTD_dictMatchState) {
119         /* dictMatchState repCode checks don't currently handle repCode == 0
120          * disabling. */
121         assert(offset_1 <= dictAndPrefixLength);
122         assert(offset_2 <= dictAndPrefixLength);
123     }
124 
125     /* Main Search Loop */
126     while (ip < ilimit) {   /* < instead of <=, because repcode check at (ip+1) */
127         size_t mLength;
128         U32 offset;
129         size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
130         size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
131         size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
132         size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
133         U32 const current = (U32)(ip-base);
134         U32 const matchIndexL = hashLong[h2];
135         U32 matchIndexS = hashSmall[h];
136         const BYTE* matchLong = base + matchIndexL;
137         const BYTE* match = base + matchIndexS;
138         const U32 repIndex = current + 1 - offset_1;
139         const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
140                             && repIndex < prefixLowestIndex) ?
141                                dictBase + (repIndex - dictIndexDelta) :
142                                base + repIndex;
143         hashLong[h2] = hashSmall[h] = current;   /* update hash tables */
144 
145         /* check dictMatchState repcode */
146         if (dictMode == ZSTD_dictMatchState
147             && ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
148             && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
149             const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
150             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
151             ip++;
152             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
153             goto _match_stored;
154         }
155 
156         /* check noDict repcode */
157         if ( dictMode == ZSTD_noDict
158           && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
159             mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
160             ip++;
161             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
162             goto _match_stored;
163         }
164 
165         if (matchIndexL > prefixLowestIndex) {
166             /* check prefix long match */
167             if (MEM_read64(matchLong) == MEM_read64(ip)) {
168                 mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
169                 offset = (U32)(ip-matchLong);
170                 while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
171                 goto _match_found;
172             }
173         } else if (dictMode == ZSTD_dictMatchState) {
174             /* check dictMatchState long match */
175             U32 const dictMatchIndexL = dictHashLong[dictHL];
176             const BYTE* dictMatchL = dictBase + dictMatchIndexL;
177             assert(dictMatchL < dictEnd);
178 
179             if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
180                 mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
181                 offset = (U32)(current - dictMatchIndexL - dictIndexDelta);
182                 while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
183                 goto _match_found;
184         }   }
185 
186         if (matchIndexS > prefixLowestIndex) {
187             /* check prefix short match */
188             if (MEM_read32(match) == MEM_read32(ip)) {
189                 goto _search_next_long;
190             }
191         } else if (dictMode == ZSTD_dictMatchState) {
192             /* check dictMatchState short match */
193             U32 const dictMatchIndexS = dictHashSmall[dictHS];
194             match = dictBase + dictMatchIndexS;
195             matchIndexS = dictMatchIndexS + dictIndexDelta;
196 
197             if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
198                 goto _search_next_long;
199         }   }
200 
201         ip += ((ip-anchor) >> kSearchStrength) + 1;
202 #if defined(__aarch64__)
203         PREFETCH_L1(ip+256);
204 #endif
205         continue;
206 
207 _search_next_long:
208 
209         {   size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
210             size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
211             U32 const matchIndexL3 = hashLong[hl3];
212             const BYTE* matchL3 = base + matchIndexL3;
213             hashLong[hl3] = current + 1;
214 
215             /* check prefix long +1 match */
216             if (matchIndexL3 > prefixLowestIndex) {
217                 if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
218                     mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
219                     ip++;
220                     offset = (U32)(ip-matchL3);
221                     while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
222                     goto _match_found;
223                 }
224             } else if (dictMode == ZSTD_dictMatchState) {
225                 /* check dict long +1 match */
226                 U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
227                 const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
228                 assert(dictMatchL3 < dictEnd);
229                 if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
230                     mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
231                     ip++;
232                     offset = (U32)(current + 1 - dictMatchIndexL3 - dictIndexDelta);
233                     while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
234                     goto _match_found;
235         }   }   }
236 
237         /* if no long +1 match, explore the short match we found */
238         if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) {
239             mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
240             offset = (U32)(current - matchIndexS);
241             while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
242         } else {
243             mLength = ZSTD_count(ip+4, match+4, iend) + 4;
244             offset = (U32)(ip - match);
245             while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
246         }
247 
248         /* fall-through */
249 
250 _match_found:
251         offset_2 = offset_1;
252         offset_1 = offset;
253 
254         ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
255 
256 _match_stored:
257         /* match found */
258         ip += mLength;
259         anchor = ip;
260 
261         if (ip <= ilimit) {
262             /* Complementary insertion */
263             /* done after iLimit test, as candidates could be > iend-8 */
264             {   U32 const indexToInsert = current+2;
265                 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
266                 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
267                 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
268                 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
269             }
270 
271             /* check immediate repcode */
272             if (dictMode == ZSTD_dictMatchState) {
273                 while (ip <= ilimit) {
274                     U32 const current2 = (U32)(ip-base);
275                     U32 const repIndex2 = current2 - offset_2;
276                     const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
277                         && repIndex2 < prefixLowestIndex ?
278                             dictBase + repIndex2 - dictIndexDelta :
279                             base + repIndex2;
280                     if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
281                        && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
282                         const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
283                         size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
284                         U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
285                         ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
286                         hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
287                         hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
288                         ip += repLength2;
289                         anchor = ip;
290                         continue;
291                     }
292                     break;
293             }   }
294 
295             if (dictMode == ZSTD_noDict) {
296                 while ( (ip <= ilimit)
297                      && ( (offset_2>0)
298                         & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
299                     /* store sequence */
300                     size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
301                     U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff;  /* swap offset_2 <=> offset_1 */
302                     hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
303                     hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
304                     ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, rLength-MINMATCH);
305                     ip += rLength;
306                     anchor = ip;
307                     continue;   /* faster when present ... (?) */
308         }   }   }
309     }   /* while (ip < ilimit) */
310 
311     /* save reps for next block */
312     rep[0] = offset_1 ? offset_1 : offsetSaved;
313     rep[1] = offset_2 ? offset_2 : offsetSaved;
314 
315     /* Return the last literals size */
316     return (size_t)(iend - anchor);
317 }
318 
319 
ZSTD_compressBlock_doubleFast(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)320 size_t ZSTD_compressBlock_doubleFast(
321         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
322         void const* src, size_t srcSize)
323 {
324     const U32 mls = ms->cParams.minMatch;
325     switch(mls)
326     {
327     default: /* includes case 3 */
328     case 4 :
329         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
330     case 5 :
331         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
332     case 6 :
333         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
334     case 7 :
335         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
336     }
337 }
338 
339 
ZSTD_compressBlock_doubleFast_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)340 size_t ZSTD_compressBlock_doubleFast_dictMatchState(
341         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
342         void const* src, size_t srcSize)
343 {
344     const U32 mls = ms->cParams.minMatch;
345     switch(mls)
346     {
347     default: /* includes case 3 */
348     case 4 :
349         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
350     case 5 :
351         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
352     case 6 :
353         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
354     case 7 :
355         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
356     }
357 }
358 
359 
ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize,U32 const mls)360 static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
361         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
362         void const* src, size_t srcSize,
363         U32 const mls /* template */)
364 {
365     ZSTD_compressionParameters const* cParams = &ms->cParams;
366     U32* const hashLong = ms->hashTable;
367     U32  const hBitsL = cParams->hashLog;
368     U32* const hashSmall = ms->chainTable;
369     U32  const hBitsS = cParams->chainLog;
370     const BYTE* const istart = (const BYTE*)src;
371     const BYTE* ip = istart;
372     const BYTE* anchor = istart;
373     const BYTE* const iend = istart + srcSize;
374     const BYTE* const ilimit = iend - 8;
375     const BYTE* const base = ms->window.base;
376     const U32   endIndex = (U32)((size_t)(istart - base) + srcSize);
377     const U32   lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
378     const U32   dictStartIndex = lowLimit;
379     const U32   dictLimit = ms->window.dictLimit;
380     const U32   prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
381     const BYTE* const prefixStart = base + prefixStartIndex;
382     const BYTE* const dictBase = ms->window.dictBase;
383     const BYTE* const dictStart = dictBase + dictStartIndex;
384     const BYTE* const dictEnd = dictBase + prefixStartIndex;
385     U32 offset_1=rep[0], offset_2=rep[1];
386 
387     DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
388 
389     /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
390     if (prefixStartIndex == dictStartIndex)
391         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_noDict);
392 
393     /* Search Loop */
394     while (ip < ilimit) {  /* < instead of <=, because (ip+1) */
395         const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
396         const U32 matchIndex = hashSmall[hSmall];
397         const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
398         const BYTE* match = matchBase + matchIndex;
399 
400         const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
401         const U32 matchLongIndex = hashLong[hLong];
402         const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
403         const BYTE* matchLong = matchLongBase + matchLongIndex;
404 
405         const U32 current = (U32)(ip-base);
406         const U32 repIndex = current + 1 - offset_1;   /* offset_1 expected <= current +1 */
407         const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
408         const BYTE* const repMatch = repBase + repIndex;
409         size_t mLength;
410         hashSmall[hSmall] = hashLong[hLong] = current;   /* update hash table */
411 
412         if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
413             & (offset_1 < current+1 - dictStartIndex)) /* note: we are searching at current+1 */
414           && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
415             const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
416             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
417             ip++;
418             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
419         } else {
420             if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
421                 const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
422                 const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
423                 U32 offset;
424                 mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
425                 offset = current - matchLongIndex;
426                 while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; }   /* catch up */
427                 offset_2 = offset_1;
428                 offset_1 = offset;
429                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
430 
431             } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
432                 size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
433                 U32 const matchIndex3 = hashLong[h3];
434                 const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
435                 const BYTE* match3 = match3Base + matchIndex3;
436                 U32 offset;
437                 hashLong[h3] = current + 1;
438                 if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
439                     const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
440                     const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
441                     mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
442                     ip++;
443                     offset = current+1 - matchIndex3;
444                     while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
445                 } else {
446                     const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
447                     const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
448                     mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
449                     offset = current - matchIndex;
450                     while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; }   /* catch up */
451                 }
452                 offset_2 = offset_1;
453                 offset_1 = offset;
454                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
455 
456             } else {
457                 ip += ((ip-anchor) >> kSearchStrength) + 1;
458                 continue;
459         }   }
460 
461         /* move to next sequence start */
462         ip += mLength;
463         anchor = ip;
464 
465         if (ip <= ilimit) {
466             /* Complementary insertion */
467             /* done after iLimit test, as candidates could be > iend-8 */
468             {   U32 const indexToInsert = current+2;
469                 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
470                 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
471                 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
472                 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
473             }
474 
475             /* check immediate repcode */
476             while (ip <= ilimit) {
477                 U32 const current2 = (U32)(ip-base);
478                 U32 const repIndex2 = current2 - offset_2;
479                 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
480                 if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3)   /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
481                     & (offset_2 < current2 - dictStartIndex))
482                   && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
483                     const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
484                     size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
485                     U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
486                     ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
487                     hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
488                     hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
489                     ip += repLength2;
490                     anchor = ip;
491                     continue;
492                 }
493                 break;
494     }   }   }
495 
496     /* save reps for next block */
497     rep[0] = offset_1;
498     rep[1] = offset_2;
499 
500     /* Return the last literals size */
501     return (size_t)(iend - anchor);
502 }
503 
504 
ZSTD_compressBlock_doubleFast_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)505 size_t ZSTD_compressBlock_doubleFast_extDict(
506         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
507         void const* src, size_t srcSize)
508 {
509     U32 const mls = ms->cParams.minMatch;
510     switch(mls)
511     {
512     default: /* includes case 3 */
513     case 4 :
514         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
515     case 5 :
516         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
517     case 6 :
518         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
519     case 7 :
520         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
521     }
522 }
523