blob: 47faf6d641db507e585a055a5341b0592ae0c61c [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;
William Kurkianea869482019-04-09 15:16:11 -040046 }
47 }
48}
49
50
51FORCE_INLINE_TEMPLATE
52size_t ZSTD_compressBlock_doubleFast_generic(
53 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +053054 void const* src, size_t srcSize,
55 U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
William Kurkianea869482019-04-09 15:16:11 -040056{
Abhilash S.L3b494632019-07-16 15:51:09 +053057 ZSTD_compressionParameters const* cParams = &ms->cParams;
William Kurkianea869482019-04-09 15:16:11 -040058 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;
Abhilash S.L3b494632019-07-16 15:51:09 +053066 const U32 prefixLowestIndex = ms->window.dictLimit;
67 const BYTE* const prefixLowest = base + prefixLowestIndex;
William Kurkianea869482019-04-09 15:16:11 -040068 const BYTE* const iend = istart + srcSize;
69 const BYTE* const ilimit = iend - HASH_READ_SIZE;
70 U32 offset_1=rep[0], offset_2=rep[1];
71 U32 offsetSaved = 0;
72
Abhilash S.L3b494632019-07-16 15:51:09 +053073 const ZSTD_matchState_t* const dms = ms->dictMatchState;
74 const ZSTD_compressionParameters* const dictCParams =
75 dictMode == ZSTD_dictMatchState ?
76 &dms->cParams : NULL;
77 const U32* const dictHashLong = dictMode == ZSTD_dictMatchState ?
78 dms->hashTable : NULL;
79 const U32* const dictHashSmall = dictMode == ZSTD_dictMatchState ?
80 dms->chainTable : NULL;
81 const U32 dictStartIndex = dictMode == ZSTD_dictMatchState ?
82 dms->window.dictLimit : 0;
83 const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ?
84 dms->window.base : NULL;
85 const BYTE* const dictStart = dictMode == ZSTD_dictMatchState ?
86 dictBase + dictStartIndex : NULL;
87 const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ?
88 dms->window.nextSrc : NULL;
89 const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ?
90 prefixLowestIndex - (U32)(dictEnd - dictBase) :
91 0;
92 const U32 dictHBitsL = dictMode == ZSTD_dictMatchState ?
93 dictCParams->hashLog : hBitsL;
94 const U32 dictHBitsS = dictMode == ZSTD_dictMatchState ?
95 dictCParams->chainLog : hBitsS;
96 const U32 dictAndPrefixLength = (U32)(ip - prefixLowest + dictEnd - dictStart);
97
98 assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
99
William Kurkianea869482019-04-09 15:16:11 -0400100 /* init */
Abhilash S.L3b494632019-07-16 15:51:09 +0530101 ip += (dictAndPrefixLength == 0);
102 if (dictMode == ZSTD_noDict) {
103 U32 const maxRep = (U32)(ip - prefixLowest);
William Kurkianea869482019-04-09 15:16:11 -0400104 if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
105 if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
106 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530107 if (dictMode == ZSTD_dictMatchState) {
108 /* dictMatchState repCode checks don't currently handle repCode == 0
109 * disabling. */
110 assert(offset_1 <= dictAndPrefixLength);
111 assert(offset_2 <= dictAndPrefixLength);
112 }
William Kurkianea869482019-04-09 15:16:11 -0400113
114 /* Main Search Loop */
115 while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
116 size_t mLength;
Abhilash S.L3b494632019-07-16 15:51:09 +0530117 U32 offset;
William Kurkianea869482019-04-09 15:16:11 -0400118 size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
119 size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
Abhilash S.L3b494632019-07-16 15:51:09 +0530120 size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
121 size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
William Kurkianea869482019-04-09 15:16:11 -0400122 U32 const current = (U32)(ip-base);
123 U32 const matchIndexL = hashLong[h2];
Abhilash S.L3b494632019-07-16 15:51:09 +0530124 U32 matchIndexS = hashSmall[h];
William Kurkianea869482019-04-09 15:16:11 -0400125 const BYTE* matchLong = base + matchIndexL;
126 const BYTE* match = base + matchIndexS;
Abhilash S.L3b494632019-07-16 15:51:09 +0530127 const U32 repIndex = current + 1 - offset_1;
128 const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
129 && repIndex < prefixLowestIndex) ?
130 dictBase + (repIndex - dictIndexDelta) :
131 base + repIndex;
William Kurkianea869482019-04-09 15:16:11 -0400132 hashLong[h2] = hashSmall[h] = current; /* update hash tables */
133
Abhilash S.L3b494632019-07-16 15:51:09 +0530134 /* check dictMatchState repcode */
135 if (dictMode == ZSTD_dictMatchState
136 && ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
137 && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
138 const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
139 mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
140 ip++;
141 ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
142 goto _match_stored;
143 }
144
145 /* check noDict repcode */
146 if ( dictMode == ZSTD_noDict
147 && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
William Kurkianea869482019-04-09 15:16:11 -0400148 mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
149 ip++;
150 ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
Abhilash S.L3b494632019-07-16 15:51:09 +0530151 goto _match_stored;
152 }
153
154 if (matchIndexL > prefixLowestIndex) {
155 /* check prefix long match */
156 if (MEM_read64(matchLong) == MEM_read64(ip)) {
William Kurkianea869482019-04-09 15:16:11 -0400157 mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
158 offset = (U32)(ip-matchLong);
Abhilash S.L3b494632019-07-16 15:51:09 +0530159 while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
160 goto _match_found;
161 }
162 } else if (dictMode == ZSTD_dictMatchState) {
163 /* check dictMatchState long match */
164 U32 const dictMatchIndexL = dictHashLong[dictHL];
165 const BYTE* dictMatchL = dictBase + dictMatchIndexL;
166 assert(dictMatchL < dictEnd);
167
168 if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
169 mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
170 offset = (U32)(current - dictMatchIndexL - dictIndexDelta);
171 while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
172 goto _match_found;
173 }
174 }
175
176 if (matchIndexS > prefixLowestIndex) {
177 /* check prefix short match */
178 if (MEM_read32(match) == MEM_read32(ip)) {
179 goto _search_next_long;
180 }
181 } else if (dictMode == ZSTD_dictMatchState) {
182 /* check dictMatchState short match */
183 U32 const dictMatchIndexS = dictHashSmall[dictHS];
184 match = dictBase + dictMatchIndexS;
185 matchIndexS = dictMatchIndexS + dictIndexDelta;
186
187 if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
188 goto _search_next_long;
189 }
190 }
191
192 ip += ((ip-anchor) >> kSearchStrength) + 1;
193 continue;
194
195_search_next_long:
196
197 {
198 size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
199 size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
200 U32 const matchIndexL3 = hashLong[hl3];
201 const BYTE* matchL3 = base + matchIndexL3;
202 hashLong[hl3] = current + 1;
203
204 /* check prefix long +1 match */
205 if (matchIndexL3 > prefixLowestIndex) {
206 if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
William Kurkianea869482019-04-09 15:16:11 -0400207 mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
208 ip++;
209 offset = (U32)(ip-matchL3);
Abhilash S.L3b494632019-07-16 15:51:09 +0530210 while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
211 goto _match_found;
William Kurkianea869482019-04-09 15:16:11 -0400212 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530213 } else if (dictMode == ZSTD_dictMatchState) {
214 /* check dict long +1 match */
215 U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
216 const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
217 assert(dictMatchL3 < dictEnd);
218 if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
219 mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
220 ip++;
221 offset = (U32)(current + 1 - dictMatchIndexL3 - dictIndexDelta);
222 while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
223 goto _match_found;
224 }
William Kurkianea869482019-04-09 15:16:11 -0400225 }
William Kurkianea869482019-04-09 15:16:11 -0400226 }
227
Abhilash S.L3b494632019-07-16 15:51:09 +0530228 /* if no long +1 match, explore the short match we found */
229 if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) {
230 mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
231 offset = (U32)(current - matchIndexS);
232 while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
233 } else {
234 mLength = ZSTD_count(ip+4, match+4, iend) + 4;
235 offset = (U32)(ip - match);
236 while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
237 }
238
239 /* fall-through */
240
241_match_found:
242 offset_2 = offset_1;
243 offset_1 = offset;
244
245 ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
246
247_match_stored:
William Kurkianea869482019-04-09 15:16:11 -0400248 /* match found */
249 ip += mLength;
250 anchor = ip;
251
252 if (ip <= ilimit) {
253 /* Fill Table */
254 hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] =
255 hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; /* here because current+2 could be > iend-8 */
256 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] =
257 hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
258
259 /* check immediate repcode */
Abhilash S.L3b494632019-07-16 15:51:09 +0530260 if (dictMode == ZSTD_dictMatchState) {
261 while (ip <= ilimit) {
262 U32 const current2 = (U32)(ip-base);
263 U32 const repIndex2 = current2 - offset_2;
264 const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
265 && repIndex2 < prefixLowestIndex ?
266 dictBase - dictIndexDelta + repIndex2 :
267 base + repIndex2;
268 if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
269 && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
270 const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
271 size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
272 U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
273 ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
274 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
275 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
276 ip += repLength2;
277 anchor = ip;
278 continue;
279 }
280 break;
281 }
282 }
283
284 if (dictMode == ZSTD_noDict) {
285 while ( (ip <= ilimit)
286 && ( (offset_2>0)
287 & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
288 /* store sequence */
289 size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
290 U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
291 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
292 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
293 ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
294 ip += rLength;
295 anchor = ip;
296 continue; /* faster when present ... (?) */
297 } } } }
William Kurkianea869482019-04-09 15:16:11 -0400298
299 /* save reps for next block */
300 rep[0] = offset_1 ? offset_1 : offsetSaved;
301 rep[1] = offset_2 ? offset_2 : offsetSaved;
302
303 /* Return the last literals size */
304 return iend - anchor;
305}
306
307
308size_t ZSTD_compressBlock_doubleFast(
309 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +0530310 void const* src, size_t srcSize)
William Kurkianea869482019-04-09 15:16:11 -0400311{
Abhilash S.L3b494632019-07-16 15:51:09 +0530312 const U32 mls = ms->cParams.minMatch;
William Kurkianea869482019-04-09 15:16:11 -0400313 switch(mls)
314 {
315 default: /* includes case 3 */
316 case 4 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530317 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
William Kurkianea869482019-04-09 15:16:11 -0400318 case 5 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530319 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
William Kurkianea869482019-04-09 15:16:11 -0400320 case 6 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530321 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
William Kurkianea869482019-04-09 15:16:11 -0400322 case 7 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530323 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
324 }
325}
326
327
328size_t ZSTD_compressBlock_doubleFast_dictMatchState(
329 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
330 void const* src, size_t srcSize)
331{
332 const U32 mls = ms->cParams.minMatch;
333 switch(mls)
334 {
335 default: /* includes case 3 */
336 case 4 :
337 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
338 case 5 :
339 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
340 case 6 :
341 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
342 case 7 :
343 return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
William Kurkianea869482019-04-09 15:16:11 -0400344 }
345}
346
347
348static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
349 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +0530350 void const* src, size_t srcSize,
William Kurkianea869482019-04-09 15:16:11 -0400351 U32 const mls /* template */)
352{
Abhilash S.L3b494632019-07-16 15:51:09 +0530353 ZSTD_compressionParameters const* cParams = &ms->cParams;
William Kurkianea869482019-04-09 15:16:11 -0400354 U32* const hashLong = ms->hashTable;
355 U32 const hBitsL = cParams->hashLog;
356 U32* const hashSmall = ms->chainTable;
357 U32 const hBitsS = cParams->chainLog;
William Kurkianea869482019-04-09 15:16:11 -0400358 const BYTE* const istart = (const BYTE*)src;
359 const BYTE* ip = istart;
360 const BYTE* anchor = istart;
William Kurkianea869482019-04-09 15:16:11 -0400361 const BYTE* const iend = istart + srcSize;
362 const BYTE* const ilimit = iend - 8;
Abhilash S.L3b494632019-07-16 15:51:09 +0530363 const U32 prefixStartIndex = ms->window.dictLimit;
364 const BYTE* const base = ms->window.base;
365 const BYTE* const prefixStart = base + prefixStartIndex;
366 const U32 dictStartIndex = ms->window.lowLimit;
367 const BYTE* const dictBase = ms->window.dictBase;
368 const BYTE* const dictStart = dictBase + dictStartIndex;
369 const BYTE* const dictEnd = dictBase + prefixStartIndex;
William Kurkianea869482019-04-09 15:16:11 -0400370 U32 offset_1=rep[0], offset_2=rep[1];
371
Abhilash S.L3b494632019-07-16 15:51:09 +0530372 DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
373
William Kurkianea869482019-04-09 15:16:11 -0400374 /* Search Loop */
375 while (ip < ilimit) { /* < instead of <=, because (ip+1) */
376 const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
377 const U32 matchIndex = hashSmall[hSmall];
Abhilash S.L3b494632019-07-16 15:51:09 +0530378 const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
William Kurkianea869482019-04-09 15:16:11 -0400379 const BYTE* match = matchBase + matchIndex;
380
381 const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
382 const U32 matchLongIndex = hashLong[hLong];
Abhilash S.L3b494632019-07-16 15:51:09 +0530383 const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
William Kurkianea869482019-04-09 15:16:11 -0400384 const BYTE* matchLong = matchLongBase + matchLongIndex;
385
386 const U32 current = (U32)(ip-base);
387 const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */
Abhilash S.L3b494632019-07-16 15:51:09 +0530388 const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
389 const BYTE* const repMatch = repBase + repIndex;
William Kurkianea869482019-04-09 15:16:11 -0400390 size_t mLength;
391 hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */
392
Abhilash S.L3b494632019-07-16 15:51:09 +0530393 if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
394 & (repIndex > dictStartIndex))
395 && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
396 const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
397 mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
William Kurkianea869482019-04-09 15:16:11 -0400398 ip++;
399 ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
400 } else {
Abhilash S.L3b494632019-07-16 15:51:09 +0530401 if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
402 const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
403 const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
William Kurkianea869482019-04-09 15:16:11 -0400404 U32 offset;
Abhilash S.L3b494632019-07-16 15:51:09 +0530405 mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
William Kurkianea869482019-04-09 15:16:11 -0400406 offset = current - matchLongIndex;
407 while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
408 offset_2 = offset_1;
409 offset_1 = offset;
410 ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
411
Abhilash S.L3b494632019-07-16 15:51:09 +0530412 } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
William Kurkianea869482019-04-09 15:16:11 -0400413 size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
414 U32 const matchIndex3 = hashLong[h3];
Abhilash S.L3b494632019-07-16 15:51:09 +0530415 const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
William Kurkianea869482019-04-09 15:16:11 -0400416 const BYTE* match3 = match3Base + matchIndex3;
417 U32 offset;
418 hashLong[h3] = current + 1;
Abhilash S.L3b494632019-07-16 15:51:09 +0530419 if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
420 const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
421 const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
422 mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
William Kurkianea869482019-04-09 15:16:11 -0400423 ip++;
424 offset = current+1 - matchIndex3;
425 while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
426 } else {
Abhilash S.L3b494632019-07-16 15:51:09 +0530427 const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
428 const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
429 mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
William Kurkianea869482019-04-09 15:16:11 -0400430 offset = current - matchIndex;
431 while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
432 }
433 offset_2 = offset_1;
434 offset_1 = offset;
435 ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
436
437 } else {
438 ip += ((ip-anchor) >> kSearchStrength) + 1;
439 continue;
440 } }
441
442 /* found a match : store it */
443 ip += mLength;
444 anchor = ip;
445
446 if (ip <= ilimit) {
447 /* Fill Table */
448 hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2;
449 hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2;
450 hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
451 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
452 /* check immediate repcode */
453 while (ip <= ilimit) {
454 U32 const current2 = (U32)(ip-base);
455 U32 const repIndex2 = current2 - offset_2;
Abhilash S.L3b494632019-07-16 15:51:09 +0530456 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
457 if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
458 & (repIndex2 > dictStartIndex))
459 && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
460 const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
461 size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
462 U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
William Kurkianea869482019-04-09 15:16:11 -0400463 ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
464 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
465 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
466 ip += repLength2;
467 anchor = ip;
468 continue;
469 }
470 break;
471 } } }
472
473 /* save reps for next block */
474 rep[0] = offset_1;
475 rep[1] = offset_2;
476
477 /* Return the last literals size */
478 return iend - anchor;
479}
480
481
482size_t ZSTD_compressBlock_doubleFast_extDict(
483 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
Abhilash S.L3b494632019-07-16 15:51:09 +0530484 void const* src, size_t srcSize)
William Kurkianea869482019-04-09 15:16:11 -0400485{
Abhilash S.L3b494632019-07-16 15:51:09 +0530486 U32 const mls = ms->cParams.minMatch;
William Kurkianea869482019-04-09 15:16:11 -0400487 switch(mls)
488 {
489 default: /* includes case 3 */
490 case 4 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530491 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
William Kurkianea869482019-04-09 15:16:11 -0400492 case 5 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530493 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
William Kurkianea869482019-04-09 15:16:11 -0400494 case 6 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530495 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
William Kurkianea869482019-04-09 15:16:11 -0400496 case 7 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530497 return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
William Kurkianea869482019-04-09 15:16:11 -0400498 }
499}