blob: 5957255d9041cae4bd0953415518ebc7c0a763b0 [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -07001/*
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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
15void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
Scott Baker8487c5d2019-10-18 12:49:46 -070016 void const* end, ZSTD_dictTableLoadMethod_e dtlm)
Scott Baker2d897982019-09-24 11:50:08 -070017{
Scott Baker8487c5d2019-10-18 12:49:46 -070018 const ZSTD_compressionParameters* const cParams = &ms->cParams;
Scott Baker2d897982019-09-24 11:50:08 -070019 U32* const hashLarge = ms->hashTable;
20 U32 const hBitsL = cParams->hashLog;
Scott Baker8487c5d2019-10-18 12:49:46 -070021 U32 const mls = cParams->minMatch;
Scott Baker2d897982019-09-24 11:50:08 -070022 U32* const hashSmall = ms->chainTable;
23 U32 const hBitsS = cParams->chainLog;
24 const BYTE* const base = ms->window.base;
25 const BYTE* ip = base + ms->nextToUpdate;
26 const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
27 const U32 fastHashFillStep = 3;
28
29 /* Always insert every fastHashFillStep position into the hash tables.
30 * Insert the other positions into the large hash table if their entry
31 * is empty.
32 */
33 for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
34 U32 const current = (U32)(ip - base);
35 U32 i;
36 for (i = 0; i < fastHashFillStep; ++i) {
37 size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
38 size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
39 if (i == 0)
40 hashSmall[smHash] = current + i;
41 if (i == 0 || hashLarge[lgHash] == 0)
42 hashLarge[lgHash] = current + i;
Scott Baker8487c5d2019-10-18 12:49:46 -070043 /* Only load extra positions for ZSTD_dtlm_full */
44 if (dtlm == ZSTD_dtlm_fast)
45 break;
46 } }
Scott Baker2d897982019-09-24 11:50:08 -070047}
48
49
50FORCE_INLINE_TEMPLATE
51size_t ZSTD_compressBlock_doubleFast_generic(
52 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Scott Baker8487c5d2019-10-18 12:49:46 -070053 void const* src, size_t srcSize,
54 U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
Scott Baker2d897982019-09-24 11:50:08 -070055{
Scott Baker8487c5d2019-10-18 12:49:46 -070056 ZSTD_compressionParameters const* cParams = &ms->cParams;
Scott Baker2d897982019-09-24 11:50:08 -070057 U32* const hashLong = ms->hashTable;
58 const U32 hBitsL = cParams->hashLog;
59 U32* const hashSmall = ms->chainTable;
60 const U32 hBitsS = cParams->chainLog;
61 const BYTE* const base = ms->window.base;
62 const BYTE* const istart = (const BYTE*)src;
63 const BYTE* ip = istart;
64 const BYTE* anchor = istart;
Scott Baker8487c5d2019-10-18 12:49:46 -070065 const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
66 const U32 lowestValid = ms->window.dictLimit;
67 const U32 maxDistance = 1U << cParams->windowLog;
68 const U32 prefixLowestIndex = (endIndex - lowestValid > maxDistance) ? endIndex - maxDistance : lowestValid;
69 const BYTE* const prefixLowest = base + prefixLowestIndex;
Scott Baker2d897982019-09-24 11:50:08 -070070 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
Scott Baker8487c5d2019-10-18 12:49:46 -070075 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(lowestValid + maxDistance >= endIndex);
107 }
108
Scott Baker2d897982019-09-24 11:50:08 -0700109 /* init */
Scott Baker8487c5d2019-10-18 12:49:46 -0700110 ip += (dictAndPrefixLength == 0);
111 if (dictMode == ZSTD_noDict) {
112 U32 const maxRep = (U32)(ip - prefixLowest);
Scott Baker2d897982019-09-24 11:50:08 -0700113 if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
114 if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
115 }
Scott Baker8487c5d2019-10-18 12:49:46 -0700116 if (dictMode == ZSTD_dictMatchState) {
117 /* dictMatchState repCode checks don't currently handle repCode == 0
118 * disabling. */
119 assert(offset_1 <= dictAndPrefixLength);
120 assert(offset_2 <= dictAndPrefixLength);
121 }
Scott Baker2d897982019-09-24 11:50:08 -0700122
123 /* Main Search Loop */
124 while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
125 size_t mLength;
Scott Baker8487c5d2019-10-18 12:49:46 -0700126 U32 offset;
Scott Baker2d897982019-09-24 11:50:08 -0700127 size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
128 size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
Scott Baker8487c5d2019-10-18 12:49:46 -0700129 size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
130 size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
Scott Baker2d897982019-09-24 11:50:08 -0700131 U32 const current = (U32)(ip-base);
132 U32 const matchIndexL = hashLong[h2];
Scott Baker8487c5d2019-10-18 12:49:46 -0700133 U32 matchIndexS = hashSmall[h];
Scott Baker2d897982019-09-24 11:50:08 -0700134 const BYTE* matchLong = base + matchIndexL;
135 const BYTE* match = base + matchIndexS;
Scott Baker8487c5d2019-10-18 12:49:46 -0700136 const U32 repIndex = current + 1 - offset_1;
137 const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
138 && repIndex < prefixLowestIndex) ?
139 dictBase + (repIndex - dictIndexDelta) :
140 base + repIndex;
Scott Baker2d897982019-09-24 11:50:08 -0700141 hashLong[h2] = hashSmall[h] = current; /* update hash tables */
142
Scott Baker8487c5d2019-10-18 12:49:46 -0700143 /* check dictMatchState repcode */
144 if (dictMode == ZSTD_dictMatchState
145 && ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
146 && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
147 const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
148 mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
149 ip++;
150 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
151 goto _match_stored;
152 }
153
154 /* check noDict repcode */
155 if ( dictMode == ZSTD_noDict
156 && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
Scott Baker2d897982019-09-24 11:50:08 -0700157 mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
158 ip++;
Scott Baker8487c5d2019-10-18 12:49:46 -0700159 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
160 goto _match_stored;
161 }
162
163 if (matchIndexL > prefixLowestIndex) {
164 /* check prefix long match */
165 if (MEM_read64(matchLong) == MEM_read64(ip)) {
Scott Baker2d897982019-09-24 11:50:08 -0700166 mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
167 offset = (U32)(ip-matchLong);
Scott Baker8487c5d2019-10-18 12:49:46 -0700168 while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
169 goto _match_found;
170 }
171 } else if (dictMode == ZSTD_dictMatchState) {
172 /* check dictMatchState long match */
173 U32 const dictMatchIndexL = dictHashLong[dictHL];
174 const BYTE* dictMatchL = dictBase + dictMatchIndexL;
175 assert(dictMatchL < dictEnd);
176
177 if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
178 mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
179 offset = (U32)(current - dictMatchIndexL - dictIndexDelta);
180 while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
181 goto _match_found;
182 } }
183
184 if (matchIndexS > prefixLowestIndex) {
185 /* check prefix short match */
186 if (MEM_read32(match) == MEM_read32(ip)) {
187 goto _search_next_long;
188 }
189 } else if (dictMode == ZSTD_dictMatchState) {
190 /* check dictMatchState short match */
191 U32 const dictMatchIndexS = dictHashSmall[dictHS];
192 match = dictBase + dictMatchIndexS;
193 matchIndexS = dictMatchIndexS + dictIndexDelta;
194
195 if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
196 goto _search_next_long;
197 } }
198
199 ip += ((ip-anchor) >> kSearchStrength) + 1;
200 continue;
201
202_search_next_long:
203
204 { size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
205 size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
206 U32 const matchIndexL3 = hashLong[hl3];
207 const BYTE* matchL3 = base + matchIndexL3;
208 hashLong[hl3] = current + 1;
209
210 /* check prefix long +1 match */
211 if (matchIndexL3 > prefixLowestIndex) {
212 if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
Scott Baker2d897982019-09-24 11:50:08 -0700213 mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
214 ip++;
215 offset = (U32)(ip-matchL3);
Scott Baker8487c5d2019-10-18 12:49:46 -0700216 while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
217 goto _match_found;
Scott Baker2d897982019-09-24 11:50:08 -0700218 }
Scott Baker8487c5d2019-10-18 12:49:46 -0700219 } else if (dictMode == ZSTD_dictMatchState) {
220 /* check dict long +1 match */
221 U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
222 const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
223 assert(dictMatchL3 < dictEnd);
224 if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
225 mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
226 ip++;
227 offset = (U32)(current + 1 - dictMatchIndexL3 - dictIndexDelta);
228 while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
229 goto _match_found;
230 } } }
Scott Baker2d897982019-09-24 11:50:08 -0700231
Scott Baker8487c5d2019-10-18 12:49:46 -0700232 /* if no long +1 match, explore the short match we found */
233 if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) {
234 mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
235 offset = (U32)(current - matchIndexS);
236 while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
237 } else {
238 mLength = ZSTD_count(ip+4, match+4, iend) + 4;
239 offset = (U32)(ip - match);
240 while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
Scott Baker2d897982019-09-24 11:50:08 -0700241 }
242
Scott Baker8487c5d2019-10-18 12:49:46 -0700243 /* fall-through */
244
245_match_found:
246 offset_2 = offset_1;
247 offset_1 = offset;
248
249 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
250
251_match_stored:
Scott Baker2d897982019-09-24 11:50:08 -0700252 /* match found */
253 ip += mLength;
254 anchor = ip;
255
256 if (ip <= ilimit) {
Scott Baker8487c5d2019-10-18 12:49:46 -0700257 /* Complementary insertion */
258 /* done after iLimit test, as candidates could be > iend-8 */
259 { U32 const indexToInsert = current+2;
260 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
261 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
262 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
263 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
264 }
Scott Baker2d897982019-09-24 11:50:08 -0700265
266 /* check immediate repcode */
Scott Baker8487c5d2019-10-18 12:49:46 -0700267 if (dictMode == ZSTD_dictMatchState) {
268 while (ip <= ilimit) {
269 U32 const current2 = (U32)(ip-base);
270 U32 const repIndex2 = current2 - offset_2;
271 const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
272 && repIndex2 < prefixLowestIndex ?
273 dictBase - dictIndexDelta + repIndex2 :
274 base + repIndex2;
275 if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
276 && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
277 const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
278 size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
279 U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
280 ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
281 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
282 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
283 ip += repLength2;
284 anchor = ip;
285 continue;
286 }
287 break;
288 } }
289
290 if (dictMode == ZSTD_noDict) {
291 while ( (ip <= ilimit)
292 && ( (offset_2>0)
293 & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
294 /* store sequence */
295 size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
296 U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
297 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
298 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
299 ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
300 ip += rLength;
301 anchor = ip;
302 continue; /* faster when present ... (?) */
303 } } }
304 } /* while (ip < ilimit) */
Scott Baker2d897982019-09-24 11:50:08 -0700305
306 /* save reps for next block */
307 rep[0] = offset_1 ? offset_1 : offsetSaved;
308 rep[1] = offset_2 ? offset_2 : offsetSaved;
309
310 /* Return the last literals size */
Scott Baker8487c5d2019-10-18 12:49:46 -0700311 return (size_t)(iend - anchor);
Scott Baker2d897982019-09-24 11:50:08 -0700312}
313
314
315size_t ZSTD_compressBlock_doubleFast(
316 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Scott Baker8487c5d2019-10-18 12:49:46 -0700317 void const* src, size_t srcSize)
Scott Baker2d897982019-09-24 11:50:08 -0700318{
Scott Baker8487c5d2019-10-18 12:49:46 -0700319 const U32 mls = ms->cParams.minMatch;
Scott Baker2d897982019-09-24 11:50:08 -0700320 switch(mls)
321 {
322 default: /* includes case 3 */
323 case 4 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700324 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
Scott Baker2d897982019-09-24 11:50:08 -0700325 case 5 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700326 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
Scott Baker2d897982019-09-24 11:50:08 -0700327 case 6 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700328 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
Scott Baker2d897982019-09-24 11:50:08 -0700329 case 7 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700330 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
331 }
332}
333
334
335size_t ZSTD_compressBlock_doubleFast_dictMatchState(
336 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
337 void const* src, size_t srcSize)
338{
339 const U32 mls = ms->cParams.minMatch;
340 switch(mls)
341 {
342 default: /* includes case 3 */
343 case 4 :
344 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
345 case 5 :
346 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
347 case 6 :
348 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
349 case 7 :
350 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
Scott Baker2d897982019-09-24 11:50:08 -0700351 }
352}
353
354
355static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
356 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Scott Baker8487c5d2019-10-18 12:49:46 -0700357 void const* src, size_t srcSize,
Scott Baker2d897982019-09-24 11:50:08 -0700358 U32 const mls /* template */)
359{
Scott Baker8487c5d2019-10-18 12:49:46 -0700360 ZSTD_compressionParameters const* cParams = &ms->cParams;
Scott Baker2d897982019-09-24 11:50:08 -0700361 U32* const hashLong = ms->hashTable;
362 U32 const hBitsL = cParams->hashLog;
363 U32* const hashSmall = ms->chainTable;
364 U32 const hBitsS = cParams->chainLog;
Scott Baker2d897982019-09-24 11:50:08 -0700365 const BYTE* const istart = (const BYTE*)src;
366 const BYTE* ip = istart;
367 const BYTE* anchor = istart;
Scott Baker2d897982019-09-24 11:50:08 -0700368 const BYTE* const iend = istart + srcSize;
369 const BYTE* const ilimit = iend - 8;
Scott Baker8487c5d2019-10-18 12:49:46 -0700370 const BYTE* const base = ms->window.base;
371 const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
372 const U32 maxDistance = 1U << cParams->windowLog;
373 const U32 lowestValid = ms->window.lowLimit;
374 const U32 lowLimit = (endIndex - lowestValid > maxDistance) ? endIndex - maxDistance : lowestValid;
375 const U32 dictStartIndex = lowLimit;
376 const U32 dictLimit = ms->window.dictLimit;
377 const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
378 const BYTE* const prefixStart = base + prefixStartIndex;
379 const BYTE* const dictBase = ms->window.dictBase;
380 const BYTE* const dictStart = dictBase + dictStartIndex;
381 const BYTE* const dictEnd = dictBase + prefixStartIndex;
Scott Baker2d897982019-09-24 11:50:08 -0700382 U32 offset_1=rep[0], offset_2=rep[1];
383
Scott Baker8487c5d2019-10-18 12:49:46 -0700384 DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
385
386 /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
387 if (prefixStartIndex == dictStartIndex)
388 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_noDict);
389
Scott Baker2d897982019-09-24 11:50:08 -0700390 /* Search Loop */
391 while (ip < ilimit) { /* < instead of <=, because (ip+1) */
392 const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
393 const U32 matchIndex = hashSmall[hSmall];
Scott Baker8487c5d2019-10-18 12:49:46 -0700394 const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
Scott Baker2d897982019-09-24 11:50:08 -0700395 const BYTE* match = matchBase + matchIndex;
396
397 const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
398 const U32 matchLongIndex = hashLong[hLong];
Scott Baker8487c5d2019-10-18 12:49:46 -0700399 const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
Scott Baker2d897982019-09-24 11:50:08 -0700400 const BYTE* matchLong = matchLongBase + matchLongIndex;
401
402 const U32 current = (U32)(ip-base);
403 const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */
Scott Baker8487c5d2019-10-18 12:49:46 -0700404 const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
405 const BYTE* const repMatch = repBase + repIndex;
Scott Baker2d897982019-09-24 11:50:08 -0700406 size_t mLength;
407 hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */
408
Scott Baker8487c5d2019-10-18 12:49:46 -0700409 if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
410 & (repIndex > dictStartIndex))
411 && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
412 const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
413 mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
Scott Baker2d897982019-09-24 11:50:08 -0700414 ip++;
Scott Baker8487c5d2019-10-18 12:49:46 -0700415 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
Scott Baker2d897982019-09-24 11:50:08 -0700416 } else {
Scott Baker8487c5d2019-10-18 12:49:46 -0700417 if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
418 const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
419 const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
Scott Baker2d897982019-09-24 11:50:08 -0700420 U32 offset;
Scott Baker8487c5d2019-10-18 12:49:46 -0700421 mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
Scott Baker2d897982019-09-24 11:50:08 -0700422 offset = current - matchLongIndex;
423 while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
424 offset_2 = offset_1;
425 offset_1 = offset;
Scott Baker8487c5d2019-10-18 12:49:46 -0700426 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
Scott Baker2d897982019-09-24 11:50:08 -0700427
Scott Baker8487c5d2019-10-18 12:49:46 -0700428 } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
Scott Baker2d897982019-09-24 11:50:08 -0700429 size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
430 U32 const matchIndex3 = hashLong[h3];
Scott Baker8487c5d2019-10-18 12:49:46 -0700431 const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
Scott Baker2d897982019-09-24 11:50:08 -0700432 const BYTE* match3 = match3Base + matchIndex3;
433 U32 offset;
434 hashLong[h3] = current + 1;
Scott Baker8487c5d2019-10-18 12:49:46 -0700435 if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
436 const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
437 const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
438 mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
Scott Baker2d897982019-09-24 11:50:08 -0700439 ip++;
440 offset = current+1 - matchIndex3;
441 while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
442 } else {
Scott Baker8487c5d2019-10-18 12:49:46 -0700443 const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
444 const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
445 mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
Scott Baker2d897982019-09-24 11:50:08 -0700446 offset = current - matchIndex;
447 while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
448 }
449 offset_2 = offset_1;
450 offset_1 = offset;
Scott Baker8487c5d2019-10-18 12:49:46 -0700451 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
Scott Baker2d897982019-09-24 11:50:08 -0700452
453 } else {
454 ip += ((ip-anchor) >> kSearchStrength) + 1;
455 continue;
456 } }
457
Scott Baker8487c5d2019-10-18 12:49:46 -0700458 /* move to next sequence start */
Scott Baker2d897982019-09-24 11:50:08 -0700459 ip += mLength;
460 anchor = ip;
461
462 if (ip <= ilimit) {
Scott Baker8487c5d2019-10-18 12:49:46 -0700463 /* Complementary insertion */
464 /* done after iLimit test, as candidates could be > iend-8 */
465 { U32 const indexToInsert = current+2;
466 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
467 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
468 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
469 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
470 }
471
Scott Baker2d897982019-09-24 11:50:08 -0700472 /* check immediate repcode */
473 while (ip <= ilimit) {
474 U32 const current2 = (U32)(ip-base);
475 U32 const repIndex2 = current2 - offset_2;
Scott Baker8487c5d2019-10-18 12:49:46 -0700476 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
477 if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
478 & (repIndex2 > dictStartIndex))
479 && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
480 const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
481 size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
482 U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
Scott Baker2d897982019-09-24 11:50:08 -0700483 ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
484 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
485 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
486 ip += repLength2;
487 anchor = ip;
488 continue;
489 }
490 break;
491 } } }
492
493 /* save reps for next block */
494 rep[0] = offset_1;
495 rep[1] = offset_2;
496
497 /* Return the last literals size */
Scott Baker8487c5d2019-10-18 12:49:46 -0700498 return (size_t)(iend - anchor);
Scott Baker2d897982019-09-24 11:50:08 -0700499}
500
501
502size_t ZSTD_compressBlock_doubleFast_extDict(
503 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Scott Baker8487c5d2019-10-18 12:49:46 -0700504 void const* src, size_t srcSize)
Scott Baker2d897982019-09-24 11:50:08 -0700505{
Scott Baker8487c5d2019-10-18 12:49:46 -0700506 U32 const mls = ms->cParams.minMatch;
Scott Baker2d897982019-09-24 11:50:08 -0700507 switch(mls)
508 {
509 default: /* includes case 3 */
510 case 4 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700511 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
Scott Baker2d897982019-09-24 11:50:08 -0700512 case 5 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700513 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
Scott Baker2d897982019-09-24 11:50:08 -0700514 case 6 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700515 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
Scott Baker2d897982019-09-24 11:50:08 -0700516 case 7 :
Scott Baker8487c5d2019-10-18 12:49:46 -0700517 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
Scott Baker2d897982019-09-24 11:50:08 -0700518 }
519}