blob: 5957255d9041cae4bd0953415518ebc7c0a763b0 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001/*
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,
Abhilash S.L3b494632019-07-16 15:51:09 +053016 void const* end, ZSTD_dictTableLoadMethod_e dtlm)
William Kurkianea869482019-04-09 15:16:11 -040017{
Abhilash S.L3b494632019-07-16 15:51:09 +053018 const ZSTD_compressionParameters* const cParams = &ms->cParams;
William Kurkianea869482019-04-09 15:16:11 -040019 U32* const hashLarge = ms->hashTable;
20 U32 const hBitsL = cParams->hashLog;
Abhilash S.L3b494632019-07-16 15:51:09 +053021 U32 const mls = cParams->minMatch;
William Kurkianea869482019-04-09 15:16:11 -040022 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;
Abhilash S.L3b494632019-07-16 15:51:09 +053043 /* Only load extra positions for ZSTD_dtlm_full */
44 if (dtlm == ZSTD_dtlm_fast)
45 break;
Devmalya Pauleb2e9552019-08-27 19:42:00 -040046 } }
William Kurkianea869482019-04-09 15:16:11 -040047}
48
49
50FORCE_INLINE_TEMPLATE
51size_t ZSTD_compressBlock_doubleFast_generic(
52 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +053053 void const* src, size_t srcSize,
54 U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
William Kurkianea869482019-04-09 15:16:11 -040055{
Abhilash S.L3b494632019-07-16 15:51:09 +053056 ZSTD_compressionParameters const* cParams = &ms->cParams;
William Kurkianea869482019-04-09 15:16:11 -040057 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;
Devmalya Pauleb2e9552019-08-27 19:42:00 -040065 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;
Abhilash S.L3b494632019-07-16 15:51:09 +053069 const BYTE* const prefixLowest = base + prefixLowestIndex;
William Kurkianea869482019-04-09 15:16:11 -040070 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
Abhilash S.L3b494632019-07-16 15:51:09 +053075 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
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400100 DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_generic");
101
Abhilash S.L3b494632019-07-16 15:51:09 +0530102 assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
103
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400104 /* if a dictionary is attached, it must be within window range */
105 if (dictMode == ZSTD_dictMatchState) {
106 assert(lowestValid + maxDistance >= endIndex);
107 }
108
William Kurkianea869482019-04-09 15:16:11 -0400109 /* init */
Abhilash S.L3b494632019-07-16 15:51:09 +0530110 ip += (dictAndPrefixLength == 0);
111 if (dictMode == ZSTD_noDict) {
112 U32 const maxRep = (U32)(ip - prefixLowest);
William Kurkianea869482019-04-09 15:16:11 -0400113 if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
114 if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
115 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530116 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 }
William Kurkianea869482019-04-09 15:16:11 -0400122
123 /* Main Search Loop */
124 while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
125 size_t mLength;
Abhilash S.L3b494632019-07-16 15:51:09 +0530126 U32 offset;
William Kurkianea869482019-04-09 15:16:11 -0400127 size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
128 size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
Abhilash S.L3b494632019-07-16 15:51:09 +0530129 size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
130 size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
William Kurkianea869482019-04-09 15:16:11 -0400131 U32 const current = (U32)(ip-base);
132 U32 const matchIndexL = hashLong[h2];
Abhilash S.L3b494632019-07-16 15:51:09 +0530133 U32 matchIndexS = hashSmall[h];
William Kurkianea869482019-04-09 15:16:11 -0400134 const BYTE* matchLong = base + matchIndexL;
135 const BYTE* match = base + matchIndexS;
Abhilash S.L3b494632019-07-16 15:51:09 +0530136 const U32 repIndex = current + 1 - offset_1;
137 const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
138 && repIndex < prefixLowestIndex) ?
139 dictBase + (repIndex - dictIndexDelta) :
140 base + repIndex;
William Kurkianea869482019-04-09 15:16:11 -0400141 hashLong[h2] = hashSmall[h] = current; /* update hash tables */
142
Abhilash S.L3b494632019-07-16 15:51:09 +0530143 /* 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++;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400150 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
Abhilash S.L3b494632019-07-16 15:51:09 +0530151 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)))) {
William Kurkianea869482019-04-09 15:16:11 -0400157 mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
158 ip++;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400159 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
Abhilash S.L3b494632019-07-16 15:51:09 +0530160 goto _match_stored;
161 }
162
163 if (matchIndexL > prefixLowestIndex) {
164 /* check prefix long match */
165 if (MEM_read64(matchLong) == MEM_read64(ip)) {
William Kurkianea869482019-04-09 15:16:11 -0400166 mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
167 offset = (U32)(ip-matchLong);
Abhilash S.L3b494632019-07-16 15:51:09 +0530168 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;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400182 } }
Abhilash S.L3b494632019-07-16 15:51:09 +0530183
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;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400197 } }
Abhilash S.L3b494632019-07-16 15:51:09 +0530198
199 ip += ((ip-anchor) >> kSearchStrength) + 1;
200 continue;
201
202_search_next_long:
203
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400204 { size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
Abhilash S.L3b494632019-07-16 15:51:09 +0530205 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)) {
William Kurkianea869482019-04-09 15:16:11 -0400213 mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
214 ip++;
215 offset = (U32)(ip-matchL3);
Abhilash S.L3b494632019-07-16 15:51:09 +0530216 while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
217 goto _match_found;
William Kurkianea869482019-04-09 15:16:11 -0400218 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530219 } 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;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400230 } } }
William Kurkianea869482019-04-09 15:16:11 -0400231
Abhilash S.L3b494632019-07-16 15:51:09 +0530232 /* 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 */
241 }
242
243 /* fall-through */
244
245_match_found:
246 offset_2 = offset_1;
247 offset_1 = offset;
248
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400249 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
Abhilash S.L3b494632019-07-16 15:51:09 +0530250
251_match_stored:
William Kurkianea869482019-04-09 15:16:11 -0400252 /* match found */
253 ip += mLength;
254 anchor = ip;
255
256 if (ip <= ilimit) {
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400257 /* 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 }
William Kurkianea869482019-04-09 15:16:11 -0400265
266 /* check immediate repcode */
Abhilash S.L3b494632019-07-16 15:51:09 +0530267 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;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400288 } }
Abhilash S.L3b494632019-07-16 15:51:09 +0530289
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 ... (?) */
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400303 } } }
304 } /* while (ip < ilimit) */
William Kurkianea869482019-04-09 15:16:11 -0400305
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 */
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400311 return (size_t)(iend - anchor);
William Kurkianea869482019-04-09 15:16:11 -0400312}
313
314
315size_t ZSTD_compressBlock_doubleFast(
316 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +0530317 void const* src, size_t srcSize)
William Kurkianea869482019-04-09 15:16:11 -0400318{
Abhilash S.L3b494632019-07-16 15:51:09 +0530319 const U32 mls = ms->cParams.minMatch;
William Kurkianea869482019-04-09 15:16:11 -0400320 switch(mls)
321 {
322 default: /* includes case 3 */
323 case 4 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530324 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
William Kurkianea869482019-04-09 15:16:11 -0400325 case 5 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530326 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
William Kurkianea869482019-04-09 15:16:11 -0400327 case 6 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530328 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
William Kurkianea869482019-04-09 15:16:11 -0400329 case 7 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530330 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);
William Kurkianea869482019-04-09 15:16:11 -0400351 }
352}
353
354
355static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
356 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +0530357 void const* src, size_t srcSize,
William Kurkianea869482019-04-09 15:16:11 -0400358 U32 const mls /* template */)
359{
Abhilash S.L3b494632019-07-16 15:51:09 +0530360 ZSTD_compressionParameters const* cParams = &ms->cParams;
William Kurkianea869482019-04-09 15:16:11 -0400361 U32* const hashLong = ms->hashTable;
362 U32 const hBitsL = cParams->hashLog;
363 U32* const hashSmall = ms->chainTable;
364 U32 const hBitsS = cParams->chainLog;
William Kurkianea869482019-04-09 15:16:11 -0400365 const BYTE* const istart = (const BYTE*)src;
366 const BYTE* ip = istart;
367 const BYTE* anchor = istart;
William Kurkianea869482019-04-09 15:16:11 -0400368 const BYTE* const iend = istart + srcSize;
369 const BYTE* const ilimit = iend - 8;
Abhilash S.L3b494632019-07-16 15:51:09 +0530370 const BYTE* const base = ms->window.base;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400371 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;
Abhilash S.L3b494632019-07-16 15:51:09 +0530378 const BYTE* const prefixStart = base + prefixStartIndex;
Abhilash S.L3b494632019-07-16 15:51:09 +0530379 const BYTE* const dictBase = ms->window.dictBase;
380 const BYTE* const dictStart = dictBase + dictStartIndex;
381 const BYTE* const dictEnd = dictBase + prefixStartIndex;
William Kurkianea869482019-04-09 15:16:11 -0400382 U32 offset_1=rep[0], offset_2=rep[1];
383
Abhilash S.L3b494632019-07-16 15:51:09 +0530384 DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
385
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400386 /* 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
William Kurkianea869482019-04-09 15:16:11 -0400390 /* 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];
Abhilash S.L3b494632019-07-16 15:51:09 +0530394 const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
William Kurkianea869482019-04-09 15:16:11 -0400395 const BYTE* match = matchBase + matchIndex;
396
397 const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
398 const U32 matchLongIndex = hashLong[hLong];
Abhilash S.L3b494632019-07-16 15:51:09 +0530399 const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
William Kurkianea869482019-04-09 15:16:11 -0400400 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 */
Abhilash S.L3b494632019-07-16 15:51:09 +0530404 const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
405 const BYTE* const repMatch = repBase + repIndex;
William Kurkianea869482019-04-09 15:16:11 -0400406 size_t mLength;
407 hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */
408
Abhilash S.L3b494632019-07-16 15:51:09 +0530409 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;
William Kurkianea869482019-04-09 15:16:11 -0400414 ip++;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400415 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
William Kurkianea869482019-04-09 15:16:11 -0400416 } else {
Abhilash S.L3b494632019-07-16 15:51:09 +0530417 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;
William Kurkianea869482019-04-09 15:16:11 -0400420 U32 offset;
Abhilash S.L3b494632019-07-16 15:51:09 +0530421 mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
William Kurkianea869482019-04-09 15:16:11 -0400422 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;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400426 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
William Kurkianea869482019-04-09 15:16:11 -0400427
Abhilash S.L3b494632019-07-16 15:51:09 +0530428 } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
William Kurkianea869482019-04-09 15:16:11 -0400429 size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
430 U32 const matchIndex3 = hashLong[h3];
Abhilash S.L3b494632019-07-16 15:51:09 +0530431 const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
William Kurkianea869482019-04-09 15:16:11 -0400432 const BYTE* match3 = match3Base + matchIndex3;
433 U32 offset;
434 hashLong[h3] = current + 1;
Abhilash S.L3b494632019-07-16 15:51:09 +0530435 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;
William Kurkianea869482019-04-09 15:16:11 -0400439 ip++;
440 offset = current+1 - matchIndex3;
441 while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
442 } else {
Abhilash S.L3b494632019-07-16 15:51:09 +0530443 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;
William Kurkianea869482019-04-09 15:16:11 -0400446 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;
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400451 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
William Kurkianea869482019-04-09 15:16:11 -0400452
453 } else {
454 ip += ((ip-anchor) >> kSearchStrength) + 1;
455 continue;
456 } }
457
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400458 /* move to next sequence start */
William Kurkianea869482019-04-09 15:16:11 -0400459 ip += mLength;
460 anchor = ip;
461
462 if (ip <= ilimit) {
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400463 /* 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
William Kurkianea869482019-04-09 15:16:11 -0400472 /* check immediate repcode */
473 while (ip <= ilimit) {
474 U32 const current2 = (U32)(ip-base);
475 U32 const repIndex2 = current2 - offset_2;
Abhilash S.L3b494632019-07-16 15:51:09 +0530476 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 */
William Kurkianea869482019-04-09 15:16:11 -0400483 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 */
Devmalya Pauleb2e9552019-08-27 19:42:00 -0400498 return (size_t)(iend - anchor);
William Kurkianea869482019-04-09 15:16:11 -0400499}
500
501
502size_t ZSTD_compressBlock_doubleFast_extDict(
503 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +0530504 void const* src, size_t srcSize)
William Kurkianea869482019-04-09 15:16:11 -0400505{
Abhilash S.L3b494632019-07-16 15:51:09 +0530506 U32 const mls = ms->cParams.minMatch;
William Kurkianea869482019-04-09 15:16:11 -0400507 switch(mls)
508 {
509 default: /* includes case 3 */
510 case 4 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530511 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
William Kurkianea869482019-04-09 15:16:11 -0400512 case 5 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530513 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
William Kurkianea869482019-04-09 15:16:11 -0400514 case 6 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530515 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
William Kurkianea869482019-04-09 15:16:11 -0400516 case 7 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530517 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
William Kurkianea869482019-04-09 15:16:11 -0400518 }
519}