1*01826a49SYabin Cui /*
2*01826a49SYabin Cui * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui * All rights reserved.
4*01826a49SYabin Cui *
5*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui */
10*01826a49SYabin Cui
11*01826a49SYabin Cui #include "zstd_compress_internal.h"
12*01826a49SYabin Cui #include "zstd_lazy.h"
13*01826a49SYabin Cui #include "../common/bits.h" /* ZSTD_countTrailingZeros64 */
14*01826a49SYabin Cui
15*01826a49SYabin Cui #if !defined(ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR) \
16*01826a49SYabin Cui || !defined(ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR) \
17*01826a49SYabin Cui || !defined(ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR) \
18*01826a49SYabin Cui || !defined(ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR)
19*01826a49SYabin Cui
20*01826a49SYabin Cui #define kLazySkippingStep 8
21*01826a49SYabin Cui
22*01826a49SYabin Cui
23*01826a49SYabin Cui /*-*************************************
24*01826a49SYabin Cui * Binary Tree search
25*01826a49SYabin Cui ***************************************/
26*01826a49SYabin Cui
27*01826a49SYabin Cui static
28*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_updateDUBT(ZSTD_matchState_t * ms,const BYTE * ip,const BYTE * iend,U32 mls)29*01826a49SYabin Cui void ZSTD_updateDUBT(ZSTD_matchState_t* ms,
30*01826a49SYabin Cui const BYTE* ip, const BYTE* iend,
31*01826a49SYabin Cui U32 mls)
32*01826a49SYabin Cui {
33*01826a49SYabin Cui const ZSTD_compressionParameters* const cParams = &ms->cParams;
34*01826a49SYabin Cui U32* const hashTable = ms->hashTable;
35*01826a49SYabin Cui U32 const hashLog = cParams->hashLog;
36*01826a49SYabin Cui
37*01826a49SYabin Cui U32* const bt = ms->chainTable;
38*01826a49SYabin Cui U32 const btLog = cParams->chainLog - 1;
39*01826a49SYabin Cui U32 const btMask = (1 << btLog) - 1;
40*01826a49SYabin Cui
41*01826a49SYabin Cui const BYTE* const base = ms->window.base;
42*01826a49SYabin Cui U32 const target = (U32)(ip - base);
43*01826a49SYabin Cui U32 idx = ms->nextToUpdate;
44*01826a49SYabin Cui
45*01826a49SYabin Cui if (idx != target)
46*01826a49SYabin Cui DEBUGLOG(7, "ZSTD_updateDUBT, from %u to %u (dictLimit:%u)",
47*01826a49SYabin Cui idx, target, ms->window.dictLimit);
48*01826a49SYabin Cui assert(ip + 8 <= iend); /* condition for ZSTD_hashPtr */
49*01826a49SYabin Cui (void)iend;
50*01826a49SYabin Cui
51*01826a49SYabin Cui assert(idx >= ms->window.dictLimit); /* condition for valid base+idx */
52*01826a49SYabin Cui for ( ; idx < target ; idx++) {
53*01826a49SYabin Cui size_t const h = ZSTD_hashPtr(base + idx, hashLog, mls); /* assumption : ip + 8 <= iend */
54*01826a49SYabin Cui U32 const matchIndex = hashTable[h];
55*01826a49SYabin Cui
56*01826a49SYabin Cui U32* const nextCandidatePtr = bt + 2*(idx&btMask);
57*01826a49SYabin Cui U32* const sortMarkPtr = nextCandidatePtr + 1;
58*01826a49SYabin Cui
59*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_updateDUBT: insert %u", idx);
60*01826a49SYabin Cui hashTable[h] = idx; /* Update Hash Table */
61*01826a49SYabin Cui *nextCandidatePtr = matchIndex; /* update BT like a chain */
62*01826a49SYabin Cui *sortMarkPtr = ZSTD_DUBT_UNSORTED_MARK;
63*01826a49SYabin Cui }
64*01826a49SYabin Cui ms->nextToUpdate = target;
65*01826a49SYabin Cui }
66*01826a49SYabin Cui
67*01826a49SYabin Cui
68*01826a49SYabin Cui /** ZSTD_insertDUBT1() :
69*01826a49SYabin Cui * sort one already inserted but unsorted position
70*01826a49SYabin Cui * assumption : curr >= btlow == (curr - btmask)
71*01826a49SYabin Cui * doesn't fail */
72*01826a49SYabin Cui static
73*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_insertDUBT1(const ZSTD_matchState_t * ms,U32 curr,const BYTE * inputEnd,U32 nbCompares,U32 btLow,const ZSTD_dictMode_e dictMode)74*01826a49SYabin Cui void ZSTD_insertDUBT1(const ZSTD_matchState_t* ms,
75*01826a49SYabin Cui U32 curr, const BYTE* inputEnd,
76*01826a49SYabin Cui U32 nbCompares, U32 btLow,
77*01826a49SYabin Cui const ZSTD_dictMode_e dictMode)
78*01826a49SYabin Cui {
79*01826a49SYabin Cui const ZSTD_compressionParameters* const cParams = &ms->cParams;
80*01826a49SYabin Cui U32* const bt = ms->chainTable;
81*01826a49SYabin Cui U32 const btLog = cParams->chainLog - 1;
82*01826a49SYabin Cui U32 const btMask = (1 << btLog) - 1;
83*01826a49SYabin Cui size_t commonLengthSmaller=0, commonLengthLarger=0;
84*01826a49SYabin Cui const BYTE* const base = ms->window.base;
85*01826a49SYabin Cui const BYTE* const dictBase = ms->window.dictBase;
86*01826a49SYabin Cui const U32 dictLimit = ms->window.dictLimit;
87*01826a49SYabin Cui const BYTE* const ip = (curr>=dictLimit) ? base + curr : dictBase + curr;
88*01826a49SYabin Cui const BYTE* const iend = (curr>=dictLimit) ? inputEnd : dictBase + dictLimit;
89*01826a49SYabin Cui const BYTE* const dictEnd = dictBase + dictLimit;
90*01826a49SYabin Cui const BYTE* const prefixStart = base + dictLimit;
91*01826a49SYabin Cui const BYTE* match;
92*01826a49SYabin Cui U32* smallerPtr = bt + 2*(curr&btMask);
93*01826a49SYabin Cui U32* largerPtr = smallerPtr + 1;
94*01826a49SYabin Cui U32 matchIndex = *smallerPtr; /* this candidate is unsorted : next sorted candidate is reached through *smallerPtr, while *largerPtr contains previous unsorted candidate (which is already saved and can be overwritten) */
95*01826a49SYabin Cui U32 dummy32; /* to be nullified at the end */
96*01826a49SYabin Cui U32 const windowValid = ms->window.lowLimit;
97*01826a49SYabin Cui U32 const maxDistance = 1U << cParams->windowLog;
98*01826a49SYabin Cui U32 const windowLow = (curr - windowValid > maxDistance) ? curr - maxDistance : windowValid;
99*01826a49SYabin Cui
100*01826a49SYabin Cui
101*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_insertDUBT1(%u) (dictLimit=%u, lowLimit=%u)",
102*01826a49SYabin Cui curr, dictLimit, windowLow);
103*01826a49SYabin Cui assert(curr >= btLow);
104*01826a49SYabin Cui assert(ip < iend); /* condition for ZSTD_count */
105*01826a49SYabin Cui
106*01826a49SYabin Cui for (; nbCompares && (matchIndex > windowLow); --nbCompares) {
107*01826a49SYabin Cui U32* const nextPtr = bt + 2*(matchIndex & btMask);
108*01826a49SYabin Cui size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
109*01826a49SYabin Cui assert(matchIndex < curr);
110*01826a49SYabin Cui /* note : all candidates are now supposed sorted,
111*01826a49SYabin Cui * but it's still possible to have nextPtr[1] == ZSTD_DUBT_UNSORTED_MARK
112*01826a49SYabin Cui * when a real index has the same value as ZSTD_DUBT_UNSORTED_MARK */
113*01826a49SYabin Cui
114*01826a49SYabin Cui if ( (dictMode != ZSTD_extDict)
115*01826a49SYabin Cui || (matchIndex+matchLength >= dictLimit) /* both in current segment*/
116*01826a49SYabin Cui || (curr < dictLimit) /* both in extDict */) {
117*01826a49SYabin Cui const BYTE* const mBase = ( (dictMode != ZSTD_extDict)
118*01826a49SYabin Cui || (matchIndex+matchLength >= dictLimit)) ?
119*01826a49SYabin Cui base : dictBase;
120*01826a49SYabin Cui assert( (matchIndex+matchLength >= dictLimit) /* might be wrong if extDict is incorrectly set to 0 */
121*01826a49SYabin Cui || (curr < dictLimit) );
122*01826a49SYabin Cui match = mBase + matchIndex;
123*01826a49SYabin Cui matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
124*01826a49SYabin Cui } else {
125*01826a49SYabin Cui match = dictBase + matchIndex;
126*01826a49SYabin Cui matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
127*01826a49SYabin Cui if (matchIndex+matchLength >= dictLimit)
128*01826a49SYabin Cui match = base + matchIndex; /* preparation for next read of match[matchLength] */
129*01826a49SYabin Cui }
130*01826a49SYabin Cui
131*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_insertDUBT1: comparing %u with %u : found %u common bytes ",
132*01826a49SYabin Cui curr, matchIndex, (U32)matchLength);
133*01826a49SYabin Cui
134*01826a49SYabin Cui if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
135*01826a49SYabin Cui break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt tree */
136*01826a49SYabin Cui }
137*01826a49SYabin Cui
138*01826a49SYabin Cui if (match[matchLength] < ip[matchLength]) { /* necessarily within buffer */
139*01826a49SYabin Cui /* match is smaller than current */
140*01826a49SYabin Cui *smallerPtr = matchIndex; /* update smaller idx */
141*01826a49SYabin Cui commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
142*01826a49SYabin Cui if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop searching */
143*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is smaller : next => %u",
144*01826a49SYabin Cui matchIndex, btLow, nextPtr[1]);
145*01826a49SYabin Cui smallerPtr = nextPtr+1; /* new "candidate" => larger than match, which was smaller than target */
146*01826a49SYabin Cui matchIndex = nextPtr[1]; /* new matchIndex, larger than previous and closer to current */
147*01826a49SYabin Cui } else {
148*01826a49SYabin Cui /* match is larger than current */
149*01826a49SYabin Cui *largerPtr = matchIndex;
150*01826a49SYabin Cui commonLengthLarger = matchLength;
151*01826a49SYabin Cui if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop searching */
152*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is larger => %u",
153*01826a49SYabin Cui matchIndex, btLow, nextPtr[0]);
154*01826a49SYabin Cui largerPtr = nextPtr;
155*01826a49SYabin Cui matchIndex = nextPtr[0];
156*01826a49SYabin Cui } }
157*01826a49SYabin Cui
158*01826a49SYabin Cui *smallerPtr = *largerPtr = 0;
159*01826a49SYabin Cui }
160*01826a49SYabin Cui
161*01826a49SYabin Cui
162*01826a49SYabin Cui static
163*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_DUBT_findBetterDictMatch(const ZSTD_matchState_t * ms,const BYTE * const ip,const BYTE * const iend,size_t * offsetPtr,size_t bestLength,U32 nbCompares,U32 const mls,const ZSTD_dictMode_e dictMode)164*01826a49SYabin Cui size_t ZSTD_DUBT_findBetterDictMatch (
165*01826a49SYabin Cui const ZSTD_matchState_t* ms,
166*01826a49SYabin Cui const BYTE* const ip, const BYTE* const iend,
167*01826a49SYabin Cui size_t* offsetPtr,
168*01826a49SYabin Cui size_t bestLength,
169*01826a49SYabin Cui U32 nbCompares,
170*01826a49SYabin Cui U32 const mls,
171*01826a49SYabin Cui const ZSTD_dictMode_e dictMode)
172*01826a49SYabin Cui {
173*01826a49SYabin Cui const ZSTD_matchState_t * const dms = ms->dictMatchState;
174*01826a49SYabin Cui const ZSTD_compressionParameters* const dmsCParams = &dms->cParams;
175*01826a49SYabin Cui const U32 * const dictHashTable = dms->hashTable;
176*01826a49SYabin Cui U32 const hashLog = dmsCParams->hashLog;
177*01826a49SYabin Cui size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
178*01826a49SYabin Cui U32 dictMatchIndex = dictHashTable[h];
179*01826a49SYabin Cui
180*01826a49SYabin Cui const BYTE* const base = ms->window.base;
181*01826a49SYabin Cui const BYTE* const prefixStart = base + ms->window.dictLimit;
182*01826a49SYabin Cui U32 const curr = (U32)(ip-base);
183*01826a49SYabin Cui const BYTE* const dictBase = dms->window.base;
184*01826a49SYabin Cui const BYTE* const dictEnd = dms->window.nextSrc;
185*01826a49SYabin Cui U32 const dictHighLimit = (U32)(dms->window.nextSrc - dms->window.base);
186*01826a49SYabin Cui U32 const dictLowLimit = dms->window.lowLimit;
187*01826a49SYabin Cui U32 const dictIndexDelta = ms->window.lowLimit - dictHighLimit;
188*01826a49SYabin Cui
189*01826a49SYabin Cui U32* const dictBt = dms->chainTable;
190*01826a49SYabin Cui U32 const btLog = dmsCParams->chainLog - 1;
191*01826a49SYabin Cui U32 const btMask = (1 << btLog) - 1;
192*01826a49SYabin Cui U32 const btLow = (btMask >= dictHighLimit - dictLowLimit) ? dictLowLimit : dictHighLimit - btMask;
193*01826a49SYabin Cui
194*01826a49SYabin Cui size_t commonLengthSmaller=0, commonLengthLarger=0;
195*01826a49SYabin Cui
196*01826a49SYabin Cui (void)dictMode;
197*01826a49SYabin Cui assert(dictMode == ZSTD_dictMatchState);
198*01826a49SYabin Cui
199*01826a49SYabin Cui for (; nbCompares && (dictMatchIndex > dictLowLimit); --nbCompares) {
200*01826a49SYabin Cui U32* const nextPtr = dictBt + 2*(dictMatchIndex & btMask);
201*01826a49SYabin Cui size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
202*01826a49SYabin Cui const BYTE* match = dictBase + dictMatchIndex;
203*01826a49SYabin Cui matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
204*01826a49SYabin Cui if (dictMatchIndex+matchLength >= dictHighLimit)
205*01826a49SYabin Cui match = base + dictMatchIndex + dictIndexDelta; /* to prepare for next usage of match[matchLength] */
206*01826a49SYabin Cui
207*01826a49SYabin Cui if (matchLength > bestLength) {
208*01826a49SYabin Cui U32 matchIndex = dictMatchIndex + dictIndexDelta;
209*01826a49SYabin Cui if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) ) {
210*01826a49SYabin Cui DEBUGLOG(9, "ZSTD_DUBT_findBetterDictMatch(%u) : found better match length %u -> %u and offsetCode %u -> %u (dictMatchIndex %u, matchIndex %u)",
211*01826a49SYabin Cui curr, (U32)bestLength, (U32)matchLength, (U32)*offsetPtr, OFFSET_TO_OFFBASE(curr - matchIndex), dictMatchIndex, matchIndex);
212*01826a49SYabin Cui bestLength = matchLength, *offsetPtr = OFFSET_TO_OFFBASE(curr - matchIndex);
213*01826a49SYabin Cui }
214*01826a49SYabin Cui if (ip+matchLength == iend) { /* reached end of input : ip[matchLength] is not valid, no way to know if it's larger or smaller than match */
215*01826a49SYabin Cui break; /* drop, to guarantee consistency (miss a little bit of compression) */
216*01826a49SYabin Cui }
217*01826a49SYabin Cui }
218*01826a49SYabin Cui
219*01826a49SYabin Cui if (match[matchLength] < ip[matchLength]) {
220*01826a49SYabin Cui if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
221*01826a49SYabin Cui commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
222*01826a49SYabin Cui dictMatchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
223*01826a49SYabin Cui } else {
224*01826a49SYabin Cui /* match is larger than current */
225*01826a49SYabin Cui if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
226*01826a49SYabin Cui commonLengthLarger = matchLength;
227*01826a49SYabin Cui dictMatchIndex = nextPtr[0];
228*01826a49SYabin Cui }
229*01826a49SYabin Cui }
230*01826a49SYabin Cui
231*01826a49SYabin Cui if (bestLength >= MINMATCH) {
232*01826a49SYabin Cui U32 const mIndex = curr - (U32)OFFBASE_TO_OFFSET(*offsetPtr); (void)mIndex;
233*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_DUBT_findBetterDictMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
234*01826a49SYabin Cui curr, (U32)bestLength, (U32)*offsetPtr, mIndex);
235*01826a49SYabin Cui }
236*01826a49SYabin Cui return bestLength;
237*01826a49SYabin Cui
238*01826a49SYabin Cui }
239*01826a49SYabin Cui
240*01826a49SYabin Cui
241*01826a49SYabin Cui static
242*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_DUBT_findBestMatch(ZSTD_matchState_t * ms,const BYTE * const ip,const BYTE * const iend,size_t * offBasePtr,U32 const mls,const ZSTD_dictMode_e dictMode)243*01826a49SYabin Cui size_t ZSTD_DUBT_findBestMatch(ZSTD_matchState_t* ms,
244*01826a49SYabin Cui const BYTE* const ip, const BYTE* const iend,
245*01826a49SYabin Cui size_t* offBasePtr,
246*01826a49SYabin Cui U32 const mls,
247*01826a49SYabin Cui const ZSTD_dictMode_e dictMode)
248*01826a49SYabin Cui {
249*01826a49SYabin Cui const ZSTD_compressionParameters* const cParams = &ms->cParams;
250*01826a49SYabin Cui U32* const hashTable = ms->hashTable;
251*01826a49SYabin Cui U32 const hashLog = cParams->hashLog;
252*01826a49SYabin Cui size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
253*01826a49SYabin Cui U32 matchIndex = hashTable[h];
254*01826a49SYabin Cui
255*01826a49SYabin Cui const BYTE* const base = ms->window.base;
256*01826a49SYabin Cui U32 const curr = (U32)(ip-base);
257*01826a49SYabin Cui U32 const windowLow = ZSTD_getLowestMatchIndex(ms, curr, cParams->windowLog);
258*01826a49SYabin Cui
259*01826a49SYabin Cui U32* const bt = ms->chainTable;
260*01826a49SYabin Cui U32 const btLog = cParams->chainLog - 1;
261*01826a49SYabin Cui U32 const btMask = (1 << btLog) - 1;
262*01826a49SYabin Cui U32 const btLow = (btMask >= curr) ? 0 : curr - btMask;
263*01826a49SYabin Cui U32 const unsortLimit = MAX(btLow, windowLow);
264*01826a49SYabin Cui
265*01826a49SYabin Cui U32* nextCandidate = bt + 2*(matchIndex&btMask);
266*01826a49SYabin Cui U32* unsortedMark = bt + 2*(matchIndex&btMask) + 1;
267*01826a49SYabin Cui U32 nbCompares = 1U << cParams->searchLog;
268*01826a49SYabin Cui U32 nbCandidates = nbCompares;
269*01826a49SYabin Cui U32 previousCandidate = 0;
270*01826a49SYabin Cui
271*01826a49SYabin Cui DEBUGLOG(7, "ZSTD_DUBT_findBestMatch (%u) ", curr);
272*01826a49SYabin Cui assert(ip <= iend-8); /* required for h calculation */
273*01826a49SYabin Cui assert(dictMode != ZSTD_dedicatedDictSearch);
274*01826a49SYabin Cui
275*01826a49SYabin Cui /* reach end of unsorted candidates list */
276*01826a49SYabin Cui while ( (matchIndex > unsortLimit)
277*01826a49SYabin Cui && (*unsortedMark == ZSTD_DUBT_UNSORTED_MARK)
278*01826a49SYabin Cui && (nbCandidates > 1) ) {
279*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_DUBT_findBestMatch: candidate %u is unsorted",
280*01826a49SYabin Cui matchIndex);
281*01826a49SYabin Cui *unsortedMark = previousCandidate; /* the unsortedMark becomes a reversed chain, to move up back to original position */
282*01826a49SYabin Cui previousCandidate = matchIndex;
283*01826a49SYabin Cui matchIndex = *nextCandidate;
284*01826a49SYabin Cui nextCandidate = bt + 2*(matchIndex&btMask);
285*01826a49SYabin Cui unsortedMark = bt + 2*(matchIndex&btMask) + 1;
286*01826a49SYabin Cui nbCandidates --;
287*01826a49SYabin Cui }
288*01826a49SYabin Cui
289*01826a49SYabin Cui /* nullify last candidate if it's still unsorted
290*01826a49SYabin Cui * simplification, detrimental to compression ratio, beneficial for speed */
291*01826a49SYabin Cui if ( (matchIndex > unsortLimit)
292*01826a49SYabin Cui && (*unsortedMark==ZSTD_DUBT_UNSORTED_MARK) ) {
293*01826a49SYabin Cui DEBUGLOG(7, "ZSTD_DUBT_findBestMatch: nullify last unsorted candidate %u",
294*01826a49SYabin Cui matchIndex);
295*01826a49SYabin Cui *nextCandidate = *unsortedMark = 0;
296*01826a49SYabin Cui }
297*01826a49SYabin Cui
298*01826a49SYabin Cui /* batch sort stacked candidates */
299*01826a49SYabin Cui matchIndex = previousCandidate;
300*01826a49SYabin Cui while (matchIndex) { /* will end on matchIndex == 0 */
301*01826a49SYabin Cui U32* const nextCandidateIdxPtr = bt + 2*(matchIndex&btMask) + 1;
302*01826a49SYabin Cui U32 const nextCandidateIdx = *nextCandidateIdxPtr;
303*01826a49SYabin Cui ZSTD_insertDUBT1(ms, matchIndex, iend,
304*01826a49SYabin Cui nbCandidates, unsortLimit, dictMode);
305*01826a49SYabin Cui matchIndex = nextCandidateIdx;
306*01826a49SYabin Cui nbCandidates++;
307*01826a49SYabin Cui }
308*01826a49SYabin Cui
309*01826a49SYabin Cui /* find longest match */
310*01826a49SYabin Cui { size_t commonLengthSmaller = 0, commonLengthLarger = 0;
311*01826a49SYabin Cui const BYTE* const dictBase = ms->window.dictBase;
312*01826a49SYabin Cui const U32 dictLimit = ms->window.dictLimit;
313*01826a49SYabin Cui const BYTE* const dictEnd = dictBase + dictLimit;
314*01826a49SYabin Cui const BYTE* const prefixStart = base + dictLimit;
315*01826a49SYabin Cui U32* smallerPtr = bt + 2*(curr&btMask);
316*01826a49SYabin Cui U32* largerPtr = bt + 2*(curr&btMask) + 1;
317*01826a49SYabin Cui U32 matchEndIdx = curr + 8 + 1;
318*01826a49SYabin Cui U32 dummy32; /* to be nullified at the end */
319*01826a49SYabin Cui size_t bestLength = 0;
320*01826a49SYabin Cui
321*01826a49SYabin Cui matchIndex = hashTable[h];
322*01826a49SYabin Cui hashTable[h] = curr; /* Update Hash Table */
323*01826a49SYabin Cui
324*01826a49SYabin Cui for (; nbCompares && (matchIndex > windowLow); --nbCompares) {
325*01826a49SYabin Cui U32* const nextPtr = bt + 2*(matchIndex & btMask);
326*01826a49SYabin Cui size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
327*01826a49SYabin Cui const BYTE* match;
328*01826a49SYabin Cui
329*01826a49SYabin Cui if ((dictMode != ZSTD_extDict) || (matchIndex+matchLength >= dictLimit)) {
330*01826a49SYabin Cui match = base + matchIndex;
331*01826a49SYabin Cui matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
332*01826a49SYabin Cui } else {
333*01826a49SYabin Cui match = dictBase + matchIndex;
334*01826a49SYabin Cui matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
335*01826a49SYabin Cui if (matchIndex+matchLength >= dictLimit)
336*01826a49SYabin Cui match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
337*01826a49SYabin Cui }
338*01826a49SYabin Cui
339*01826a49SYabin Cui if (matchLength > bestLength) {
340*01826a49SYabin Cui if (matchLength > matchEndIdx - matchIndex)
341*01826a49SYabin Cui matchEndIdx = matchIndex + (U32)matchLength;
342*01826a49SYabin Cui if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr - matchIndex + 1) - ZSTD_highbit32((U32)*offBasePtr)) )
343*01826a49SYabin Cui bestLength = matchLength, *offBasePtr = OFFSET_TO_OFFBASE(curr - matchIndex);
344*01826a49SYabin Cui if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
345*01826a49SYabin Cui if (dictMode == ZSTD_dictMatchState) {
346*01826a49SYabin Cui nbCompares = 0; /* in addition to avoiding checking any
347*01826a49SYabin Cui * further in this loop, make sure we
348*01826a49SYabin Cui * skip checking in the dictionary. */
349*01826a49SYabin Cui }
350*01826a49SYabin Cui break; /* drop, to guarantee consistency (miss a little bit of compression) */
351*01826a49SYabin Cui }
352*01826a49SYabin Cui }
353*01826a49SYabin Cui
354*01826a49SYabin Cui if (match[matchLength] < ip[matchLength]) {
355*01826a49SYabin Cui /* match is smaller than current */
356*01826a49SYabin Cui *smallerPtr = matchIndex; /* update smaller idx */
357*01826a49SYabin Cui commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
358*01826a49SYabin Cui if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */
359*01826a49SYabin Cui smallerPtr = nextPtr+1; /* new "smaller" => larger of match */
360*01826a49SYabin Cui matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
361*01826a49SYabin Cui } else {
362*01826a49SYabin Cui /* match is larger than current */
363*01826a49SYabin Cui *largerPtr = matchIndex;
364*01826a49SYabin Cui commonLengthLarger = matchLength;
365*01826a49SYabin Cui if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */
366*01826a49SYabin Cui largerPtr = nextPtr;
367*01826a49SYabin Cui matchIndex = nextPtr[0];
368*01826a49SYabin Cui } }
369*01826a49SYabin Cui
370*01826a49SYabin Cui *smallerPtr = *largerPtr = 0;
371*01826a49SYabin Cui
372*01826a49SYabin Cui assert(nbCompares <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
373*01826a49SYabin Cui if (dictMode == ZSTD_dictMatchState && nbCompares) {
374*01826a49SYabin Cui bestLength = ZSTD_DUBT_findBetterDictMatch(
375*01826a49SYabin Cui ms, ip, iend,
376*01826a49SYabin Cui offBasePtr, bestLength, nbCompares,
377*01826a49SYabin Cui mls, dictMode);
378*01826a49SYabin Cui }
379*01826a49SYabin Cui
380*01826a49SYabin Cui assert(matchEndIdx > curr+8); /* ensure nextToUpdate is increased */
381*01826a49SYabin Cui ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */
382*01826a49SYabin Cui if (bestLength >= MINMATCH) {
383*01826a49SYabin Cui U32 const mIndex = curr - (U32)OFFBASE_TO_OFFSET(*offBasePtr); (void)mIndex;
384*01826a49SYabin Cui DEBUGLOG(8, "ZSTD_DUBT_findBestMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
385*01826a49SYabin Cui curr, (U32)bestLength, (U32)*offBasePtr, mIndex);
386*01826a49SYabin Cui }
387*01826a49SYabin Cui return bestLength;
388*01826a49SYabin Cui }
389*01826a49SYabin Cui }
390*01826a49SYabin Cui
391*01826a49SYabin Cui
392*01826a49SYabin Cui /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */
393*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
394*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_BtFindBestMatch(ZSTD_matchState_t * ms,const BYTE * const ip,const BYTE * const iLimit,size_t * offBasePtr,const U32 mls,const ZSTD_dictMode_e dictMode)395*01826a49SYabin Cui size_t ZSTD_BtFindBestMatch( ZSTD_matchState_t* ms,
396*01826a49SYabin Cui const BYTE* const ip, const BYTE* const iLimit,
397*01826a49SYabin Cui size_t* offBasePtr,
398*01826a49SYabin Cui const U32 mls /* template */,
399*01826a49SYabin Cui const ZSTD_dictMode_e dictMode)
400*01826a49SYabin Cui {
401*01826a49SYabin Cui DEBUGLOG(7, "ZSTD_BtFindBestMatch");
402*01826a49SYabin Cui if (ip < ms->window.base + ms->nextToUpdate) return 0; /* skipped area */
403*01826a49SYabin Cui ZSTD_updateDUBT(ms, ip, iLimit, mls);
404*01826a49SYabin Cui return ZSTD_DUBT_findBestMatch(ms, ip, iLimit, offBasePtr, mls, dictMode);
405*01826a49SYabin Cui }
406*01826a49SYabin Cui
407*01826a49SYabin Cui /***********************************
408*01826a49SYabin Cui * Dedicated dict search
409*01826a49SYabin Cui ***********************************/
410*01826a49SYabin Cui
ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_matchState_t * ms,const BYTE * const ip)411*01826a49SYabin Cui void ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_matchState_t* ms, const BYTE* const ip)
412*01826a49SYabin Cui {
413*01826a49SYabin Cui const BYTE* const base = ms->window.base;
414*01826a49SYabin Cui U32 const target = (U32)(ip - base);
415*01826a49SYabin Cui U32* const hashTable = ms->hashTable;
416*01826a49SYabin Cui U32* const chainTable = ms->chainTable;
417*01826a49SYabin Cui U32 const chainSize = 1 << ms->cParams.chainLog;
418*01826a49SYabin Cui U32 idx = ms->nextToUpdate;
419*01826a49SYabin Cui U32 const minChain = chainSize < target - idx ? target - chainSize : idx;
420*01826a49SYabin Cui U32 const bucketSize = 1 << ZSTD_LAZY_DDSS_BUCKET_LOG;
421*01826a49SYabin Cui U32 const cacheSize = bucketSize - 1;
422*01826a49SYabin Cui U32 const chainAttempts = (1 << ms->cParams.searchLog) - cacheSize;
423*01826a49SYabin Cui U32 const chainLimit = chainAttempts > 255 ? 255 : chainAttempts;
424*01826a49SYabin Cui
425*01826a49SYabin Cui /* We know the hashtable is oversized by a factor of `bucketSize`.
426*01826a49SYabin Cui * We are going to temporarily pretend `bucketSize == 1`, keeping only a
427*01826a49SYabin Cui * single entry. We will use the rest of the space to construct a temporary
428*01826a49SYabin Cui * chaintable.
429*01826a49SYabin Cui */
430*01826a49SYabin Cui U32 const hashLog = ms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG;
431*01826a49SYabin Cui U32* const tmpHashTable = hashTable;
432*01826a49SYabin Cui U32* const tmpChainTable = hashTable + ((size_t)1 << hashLog);
433*01826a49SYabin Cui U32 const tmpChainSize = (U32)((1 << ZSTD_LAZY_DDSS_BUCKET_LOG) - 1) << hashLog;
434*01826a49SYabin Cui U32 const tmpMinChain = tmpChainSize < target ? target - tmpChainSize : idx;
435*01826a49SYabin Cui U32 hashIdx;
436*01826a49SYabin Cui
437*01826a49SYabin Cui assert(ms->cParams.chainLog <= 24);
438*01826a49SYabin Cui assert(ms->cParams.hashLog > ms->cParams.chainLog);
439*01826a49SYabin Cui assert(idx != 0);
440*01826a49SYabin Cui assert(tmpMinChain <= minChain);
441*01826a49SYabin Cui
442*01826a49SYabin Cui /* fill conventional hash table and conventional chain table */
443*01826a49SYabin Cui for ( ; idx < target; idx++) {
444*01826a49SYabin Cui U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch);
445*01826a49SYabin Cui if (idx >= tmpMinChain) {
446*01826a49SYabin Cui tmpChainTable[idx - tmpMinChain] = hashTable[h];
447*01826a49SYabin Cui }
448*01826a49SYabin Cui tmpHashTable[h] = idx;
449*01826a49SYabin Cui }
450*01826a49SYabin Cui
451*01826a49SYabin Cui /* sort chains into ddss chain table */
452*01826a49SYabin Cui {
453*01826a49SYabin Cui U32 chainPos = 0;
454*01826a49SYabin Cui for (hashIdx = 0; hashIdx < (1U << hashLog); hashIdx++) {
455*01826a49SYabin Cui U32 count;
456*01826a49SYabin Cui U32 countBeyondMinChain = 0;
457*01826a49SYabin Cui U32 i = tmpHashTable[hashIdx];
458*01826a49SYabin Cui for (count = 0; i >= tmpMinChain && count < cacheSize; count++) {
459*01826a49SYabin Cui /* skip through the chain to the first position that won't be
460*01826a49SYabin Cui * in the hash cache bucket */
461*01826a49SYabin Cui if (i < minChain) {
462*01826a49SYabin Cui countBeyondMinChain++;
463*01826a49SYabin Cui }
464*01826a49SYabin Cui i = tmpChainTable[i - tmpMinChain];
465*01826a49SYabin Cui }
466*01826a49SYabin Cui if (count == cacheSize) {
467*01826a49SYabin Cui for (count = 0; count < chainLimit;) {
468*01826a49SYabin Cui if (i < minChain) {
469*01826a49SYabin Cui if (!i || ++countBeyondMinChain > cacheSize) {
470*01826a49SYabin Cui /* only allow pulling `cacheSize` number of entries
471*01826a49SYabin Cui * into the cache or chainTable beyond `minChain`,
472*01826a49SYabin Cui * to replace the entries pulled out of the
473*01826a49SYabin Cui * chainTable into the cache. This lets us reach
474*01826a49SYabin Cui * back further without increasing the total number
475*01826a49SYabin Cui * of entries in the chainTable, guaranteeing the
476*01826a49SYabin Cui * DDSS chain table will fit into the space
477*01826a49SYabin Cui * allocated for the regular one. */
478*01826a49SYabin Cui break;
479*01826a49SYabin Cui }
480*01826a49SYabin Cui }
481*01826a49SYabin Cui chainTable[chainPos++] = i;
482*01826a49SYabin Cui count++;
483*01826a49SYabin Cui if (i < tmpMinChain) {
484*01826a49SYabin Cui break;
485*01826a49SYabin Cui }
486*01826a49SYabin Cui i = tmpChainTable[i - tmpMinChain];
487*01826a49SYabin Cui }
488*01826a49SYabin Cui } else {
489*01826a49SYabin Cui count = 0;
490*01826a49SYabin Cui }
491*01826a49SYabin Cui if (count) {
492*01826a49SYabin Cui tmpHashTable[hashIdx] = ((chainPos - count) << 8) + count;
493*01826a49SYabin Cui } else {
494*01826a49SYabin Cui tmpHashTable[hashIdx] = 0;
495*01826a49SYabin Cui }
496*01826a49SYabin Cui }
497*01826a49SYabin Cui assert(chainPos <= chainSize); /* I believe this is guaranteed... */
498*01826a49SYabin Cui }
499*01826a49SYabin Cui
500*01826a49SYabin Cui /* move chain pointers into the last entry of each hash bucket */
501*01826a49SYabin Cui for (hashIdx = (1 << hashLog); hashIdx; ) {
502*01826a49SYabin Cui U32 const bucketIdx = --hashIdx << ZSTD_LAZY_DDSS_BUCKET_LOG;
503*01826a49SYabin Cui U32 const chainPackedPointer = tmpHashTable[hashIdx];
504*01826a49SYabin Cui U32 i;
505*01826a49SYabin Cui for (i = 0; i < cacheSize; i++) {
506*01826a49SYabin Cui hashTable[bucketIdx + i] = 0;
507*01826a49SYabin Cui }
508*01826a49SYabin Cui hashTable[bucketIdx + bucketSize - 1] = chainPackedPointer;
509*01826a49SYabin Cui }
510*01826a49SYabin Cui
511*01826a49SYabin Cui /* fill the buckets of the hash table */
512*01826a49SYabin Cui for (idx = ms->nextToUpdate; idx < target; idx++) {
513*01826a49SYabin Cui U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch)
514*01826a49SYabin Cui << ZSTD_LAZY_DDSS_BUCKET_LOG;
515*01826a49SYabin Cui U32 i;
516*01826a49SYabin Cui /* Shift hash cache down 1. */
517*01826a49SYabin Cui for (i = cacheSize - 1; i; i--)
518*01826a49SYabin Cui hashTable[h + i] = hashTable[h + i - 1];
519*01826a49SYabin Cui hashTable[h] = idx;
520*01826a49SYabin Cui }
521*01826a49SYabin Cui
522*01826a49SYabin Cui ms->nextToUpdate = target;
523*01826a49SYabin Cui }
524*01826a49SYabin Cui
525*01826a49SYabin Cui /* Returns the longest match length found in the dedicated dict search structure.
526*01826a49SYabin Cui * If none are longer than the argument ml, then ml will be returned.
527*01826a49SYabin Cui */
528*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
ZSTD_dedicatedDictSearch_lazy_search(size_t * offsetPtr,size_t ml,U32 nbAttempts,const ZSTD_matchState_t * const dms,const BYTE * const ip,const BYTE * const iLimit,const BYTE * const prefixStart,const U32 curr,const U32 dictLimit,const size_t ddsIdx)529*01826a49SYabin Cui size_t ZSTD_dedicatedDictSearch_lazy_search(size_t* offsetPtr, size_t ml, U32 nbAttempts,
530*01826a49SYabin Cui const ZSTD_matchState_t* const dms,
531*01826a49SYabin Cui const BYTE* const ip, const BYTE* const iLimit,
532*01826a49SYabin Cui const BYTE* const prefixStart, const U32 curr,
533*01826a49SYabin Cui const U32 dictLimit, const size_t ddsIdx) {
534*01826a49SYabin Cui const U32 ddsLowestIndex = dms->window.dictLimit;
535*01826a49SYabin Cui const BYTE* const ddsBase = dms->window.base;
536*01826a49SYabin Cui const BYTE* const ddsEnd = dms->window.nextSrc;
537*01826a49SYabin Cui const U32 ddsSize = (U32)(ddsEnd - ddsBase);
538*01826a49SYabin Cui const U32 ddsIndexDelta = dictLimit - ddsSize;
539*01826a49SYabin Cui const U32 bucketSize = (1 << ZSTD_LAZY_DDSS_BUCKET_LOG);
540*01826a49SYabin Cui const U32 bucketLimit = nbAttempts < bucketSize - 1 ? nbAttempts : bucketSize - 1;
541*01826a49SYabin Cui U32 ddsAttempt;
542*01826a49SYabin Cui U32 matchIndex;
543*01826a49SYabin Cui
544*01826a49SYabin Cui for (ddsAttempt = 0; ddsAttempt < bucketSize - 1; ddsAttempt++) {
545*01826a49SYabin Cui PREFETCH_L1(ddsBase + dms->hashTable[ddsIdx + ddsAttempt]);
546*01826a49SYabin Cui }
547*01826a49SYabin Cui
548*01826a49SYabin Cui {
549*01826a49SYabin Cui U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1];
550*01826a49SYabin Cui U32 const chainIndex = chainPackedPointer >> 8;
551*01826a49SYabin Cui
552*01826a49SYabin Cui PREFETCH_L1(&dms->chainTable[chainIndex]);
553*01826a49SYabin Cui }
554*01826a49SYabin Cui
555*01826a49SYabin Cui for (ddsAttempt = 0; ddsAttempt < bucketLimit; ddsAttempt++) {
556*01826a49SYabin Cui size_t currentMl=0;
557*01826a49SYabin Cui const BYTE* match;
558*01826a49SYabin Cui matchIndex = dms->hashTable[ddsIdx + ddsAttempt];
559*01826a49SYabin Cui match = ddsBase + matchIndex;
560*01826a49SYabin Cui
561*01826a49SYabin Cui if (!matchIndex) {
562*01826a49SYabin Cui return ml;
563*01826a49SYabin Cui }
564*01826a49SYabin Cui
565*01826a49SYabin Cui /* guaranteed by table construction */
566*01826a49SYabin Cui (void)ddsLowestIndex;
567*01826a49SYabin Cui assert(matchIndex >= ddsLowestIndex);
568*01826a49SYabin Cui assert(match+4 <= ddsEnd);
569*01826a49SYabin Cui if (MEM_read32(match) == MEM_read32(ip)) {
570*01826a49SYabin Cui /* assumption : matchIndex <= dictLimit-4 (by table construction) */
571*01826a49SYabin Cui currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4;
572*01826a49SYabin Cui }
573*01826a49SYabin Cui
574*01826a49SYabin Cui /* save best solution */
575*01826a49SYabin Cui if (currentMl > ml) {
576*01826a49SYabin Cui ml = currentMl;
577*01826a49SYabin Cui *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + ddsIndexDelta));
578*01826a49SYabin Cui if (ip+currentMl == iLimit) {
579*01826a49SYabin Cui /* best possible, avoids read overflow on next attempt */
580*01826a49SYabin Cui return ml;
581*01826a49SYabin Cui }
582*01826a49SYabin Cui }
583*01826a49SYabin Cui }
584*01826a49SYabin Cui
585*01826a49SYabin Cui {
586*01826a49SYabin Cui U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1];
587*01826a49SYabin Cui U32 chainIndex = chainPackedPointer >> 8;
588*01826a49SYabin Cui U32 const chainLength = chainPackedPointer & 0xFF;
589*01826a49SYabin Cui U32 const chainAttempts = nbAttempts - ddsAttempt;
590*01826a49SYabin Cui U32 const chainLimit = chainAttempts > chainLength ? chainLength : chainAttempts;
591*01826a49SYabin Cui U32 chainAttempt;
592*01826a49SYabin Cui
593*01826a49SYabin Cui for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++) {
594*01826a49SYabin Cui PREFETCH_L1(ddsBase + dms->chainTable[chainIndex + chainAttempt]);
595*01826a49SYabin Cui }
596*01826a49SYabin Cui
597*01826a49SYabin Cui for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++, chainIndex++) {
598*01826a49SYabin Cui size_t currentMl=0;
599*01826a49SYabin Cui const BYTE* match;
600*01826a49SYabin Cui matchIndex = dms->chainTable[chainIndex];
601*01826a49SYabin Cui match = ddsBase + matchIndex;
602*01826a49SYabin Cui
603*01826a49SYabin Cui /* guaranteed by table construction */
604*01826a49SYabin Cui assert(matchIndex >= ddsLowestIndex);
605*01826a49SYabin Cui assert(match+4 <= ddsEnd);
606*01826a49SYabin Cui if (MEM_read32(match) == MEM_read32(ip)) {
607*01826a49SYabin Cui /* assumption : matchIndex <= dictLimit-4 (by table construction) */
608*01826a49SYabin Cui currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4;
609*01826a49SYabin Cui }
610*01826a49SYabin Cui
611*01826a49SYabin Cui /* save best solution */
612*01826a49SYabin Cui if (currentMl > ml) {
613*01826a49SYabin Cui ml = currentMl;
614*01826a49SYabin Cui *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + ddsIndexDelta));
615*01826a49SYabin Cui if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
616*01826a49SYabin Cui }
617*01826a49SYabin Cui }
618*01826a49SYabin Cui }
619*01826a49SYabin Cui return ml;
620*01826a49SYabin Cui }
621*01826a49SYabin Cui
622*01826a49SYabin Cui
623*01826a49SYabin Cui /* *********************************
624*01826a49SYabin Cui * Hash Chain
625*01826a49SYabin Cui ***********************************/
626*01826a49SYabin Cui #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & (mask)]
627*01826a49SYabin Cui
628*01826a49SYabin Cui /* Update chains up to ip (excluded)
629*01826a49SYabin Cui Assumption : always within prefix (i.e. not within extDict) */
630*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
631*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_insertAndFindFirstIndex_internal(ZSTD_matchState_t * ms,const ZSTD_compressionParameters * const cParams,const BYTE * ip,U32 const mls,U32 const lazySkipping)632*01826a49SYabin Cui U32 ZSTD_insertAndFindFirstIndex_internal(
633*01826a49SYabin Cui ZSTD_matchState_t* ms,
634*01826a49SYabin Cui const ZSTD_compressionParameters* const cParams,
635*01826a49SYabin Cui const BYTE* ip, U32 const mls, U32 const lazySkipping)
636*01826a49SYabin Cui {
637*01826a49SYabin Cui U32* const hashTable = ms->hashTable;
638*01826a49SYabin Cui const U32 hashLog = cParams->hashLog;
639*01826a49SYabin Cui U32* const chainTable = ms->chainTable;
640*01826a49SYabin Cui const U32 chainMask = (1 << cParams->chainLog) - 1;
641*01826a49SYabin Cui const BYTE* const base = ms->window.base;
642*01826a49SYabin Cui const U32 target = (U32)(ip - base);
643*01826a49SYabin Cui U32 idx = ms->nextToUpdate;
644*01826a49SYabin Cui
645*01826a49SYabin Cui while(idx < target) { /* catch up */
646*01826a49SYabin Cui size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls);
647*01826a49SYabin Cui NEXT_IN_CHAIN(idx, chainMask) = hashTable[h];
648*01826a49SYabin Cui hashTable[h] = idx;
649*01826a49SYabin Cui idx++;
650*01826a49SYabin Cui /* Stop inserting every position when in the lazy skipping mode. */
651*01826a49SYabin Cui if (lazySkipping)
652*01826a49SYabin Cui break;
653*01826a49SYabin Cui }
654*01826a49SYabin Cui
655*01826a49SYabin Cui ms->nextToUpdate = target;
656*01826a49SYabin Cui return hashTable[ZSTD_hashPtr(ip, hashLog, mls)];
657*01826a49SYabin Cui }
658*01826a49SYabin Cui
ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t * ms,const BYTE * ip)659*01826a49SYabin Cui U32 ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t* ms, const BYTE* ip) {
660*01826a49SYabin Cui const ZSTD_compressionParameters* const cParams = &ms->cParams;
661*01826a49SYabin Cui return ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, ms->cParams.minMatch, /* lazySkipping*/ 0);
662*01826a49SYabin Cui }
663*01826a49SYabin Cui
664*01826a49SYabin Cui /* inlining is important to hardwire a hot branch (template emulation) */
665*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
666*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_HcFindBestMatch(ZSTD_matchState_t * ms,const BYTE * const ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 mls,const ZSTD_dictMode_e dictMode)667*01826a49SYabin Cui size_t ZSTD_HcFindBestMatch(
668*01826a49SYabin Cui ZSTD_matchState_t* ms,
669*01826a49SYabin Cui const BYTE* const ip, const BYTE* const iLimit,
670*01826a49SYabin Cui size_t* offsetPtr,
671*01826a49SYabin Cui const U32 mls, const ZSTD_dictMode_e dictMode)
672*01826a49SYabin Cui {
673*01826a49SYabin Cui const ZSTD_compressionParameters* const cParams = &ms->cParams;
674*01826a49SYabin Cui U32* const chainTable = ms->chainTable;
675*01826a49SYabin Cui const U32 chainSize = (1 << cParams->chainLog);
676*01826a49SYabin Cui const U32 chainMask = chainSize-1;
677*01826a49SYabin Cui const BYTE* const base = ms->window.base;
678*01826a49SYabin Cui const BYTE* const dictBase = ms->window.dictBase;
679*01826a49SYabin Cui const U32 dictLimit = ms->window.dictLimit;
680*01826a49SYabin Cui const BYTE* const prefixStart = base + dictLimit;
681*01826a49SYabin Cui const BYTE* const dictEnd = dictBase + dictLimit;
682*01826a49SYabin Cui const U32 curr = (U32)(ip-base);
683*01826a49SYabin Cui const U32 maxDistance = 1U << cParams->windowLog;
684*01826a49SYabin Cui const U32 lowestValid = ms->window.lowLimit;
685*01826a49SYabin Cui const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
686*01826a49SYabin Cui const U32 isDictionary = (ms->loadedDictEnd != 0);
687*01826a49SYabin Cui const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance;
688*01826a49SYabin Cui const U32 minChain = curr > chainSize ? curr - chainSize : 0;
689*01826a49SYabin Cui U32 nbAttempts = 1U << cParams->searchLog;
690*01826a49SYabin Cui size_t ml=4-1;
691*01826a49SYabin Cui
692*01826a49SYabin Cui const ZSTD_matchState_t* const dms = ms->dictMatchState;
693*01826a49SYabin Cui const U32 ddsHashLog = dictMode == ZSTD_dedicatedDictSearch
694*01826a49SYabin Cui ? dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG : 0;
695*01826a49SYabin Cui const size_t ddsIdx = dictMode == ZSTD_dedicatedDictSearch
696*01826a49SYabin Cui ? ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG : 0;
697*01826a49SYabin Cui
698*01826a49SYabin Cui U32 matchIndex;
699*01826a49SYabin Cui
700*01826a49SYabin Cui if (dictMode == ZSTD_dedicatedDictSearch) {
701*01826a49SYabin Cui const U32* entry = &dms->hashTable[ddsIdx];
702*01826a49SYabin Cui PREFETCH_L1(entry);
703*01826a49SYabin Cui }
704*01826a49SYabin Cui
705*01826a49SYabin Cui /* HC4 match finder */
706*01826a49SYabin Cui matchIndex = ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, mls, ms->lazySkipping);
707*01826a49SYabin Cui
708*01826a49SYabin Cui for ( ; (matchIndex>=lowLimit) & (nbAttempts>0) ; nbAttempts--) {
709*01826a49SYabin Cui size_t currentMl=0;
710*01826a49SYabin Cui if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
711*01826a49SYabin Cui const BYTE* const match = base + matchIndex;
712*01826a49SYabin Cui assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */
713*01826a49SYabin Cui /* read 4B starting from (match + ml + 1 - sizeof(U32)) */
714*01826a49SYabin Cui if (MEM_read32(match + ml - 3) == MEM_read32(ip + ml - 3)) /* potentially better */
715*01826a49SYabin Cui currentMl = ZSTD_count(ip, match, iLimit);
716*01826a49SYabin Cui } else {
717*01826a49SYabin Cui const BYTE* const match = dictBase + matchIndex;
718*01826a49SYabin Cui assert(match+4 <= dictEnd);
719*01826a49SYabin Cui if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
720*01826a49SYabin Cui currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4;
721*01826a49SYabin Cui }
722*01826a49SYabin Cui
723*01826a49SYabin Cui /* save best solution */
724*01826a49SYabin Cui if (currentMl > ml) {
725*01826a49SYabin Cui ml = currentMl;
726*01826a49SYabin Cui *offsetPtr = OFFSET_TO_OFFBASE(curr - matchIndex);
727*01826a49SYabin Cui if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
728*01826a49SYabin Cui }
729*01826a49SYabin Cui
730*01826a49SYabin Cui if (matchIndex <= minChain) break;
731*01826a49SYabin Cui matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask);
732*01826a49SYabin Cui }
733*01826a49SYabin Cui
734*01826a49SYabin Cui assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
735*01826a49SYabin Cui if (dictMode == ZSTD_dedicatedDictSearch) {
736*01826a49SYabin Cui ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts, dms,
737*01826a49SYabin Cui ip, iLimit, prefixStart, curr, dictLimit, ddsIdx);
738*01826a49SYabin Cui } else if (dictMode == ZSTD_dictMatchState) {
739*01826a49SYabin Cui const U32* const dmsChainTable = dms->chainTable;
740*01826a49SYabin Cui const U32 dmsChainSize = (1 << dms->cParams.chainLog);
741*01826a49SYabin Cui const U32 dmsChainMask = dmsChainSize - 1;
742*01826a49SYabin Cui const U32 dmsLowestIndex = dms->window.dictLimit;
743*01826a49SYabin Cui const BYTE* const dmsBase = dms->window.base;
744*01826a49SYabin Cui const BYTE* const dmsEnd = dms->window.nextSrc;
745*01826a49SYabin Cui const U32 dmsSize = (U32)(dmsEnd - dmsBase);
746*01826a49SYabin Cui const U32 dmsIndexDelta = dictLimit - dmsSize;
747*01826a49SYabin Cui const U32 dmsMinChain = dmsSize > dmsChainSize ? dmsSize - dmsChainSize : 0;
748*01826a49SYabin Cui
749*01826a49SYabin Cui matchIndex = dms->hashTable[ZSTD_hashPtr(ip, dms->cParams.hashLog, mls)];
750*01826a49SYabin Cui
751*01826a49SYabin Cui for ( ; (matchIndex>=dmsLowestIndex) & (nbAttempts>0) ; nbAttempts--) {
752*01826a49SYabin Cui size_t currentMl=0;
753*01826a49SYabin Cui const BYTE* const match = dmsBase + matchIndex;
754*01826a49SYabin Cui assert(match+4 <= dmsEnd);
755*01826a49SYabin Cui if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
756*01826a49SYabin Cui currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4;
757*01826a49SYabin Cui
758*01826a49SYabin Cui /* save best solution */
759*01826a49SYabin Cui if (currentMl > ml) {
760*01826a49SYabin Cui ml = currentMl;
761*01826a49SYabin Cui assert(curr > matchIndex + dmsIndexDelta);
762*01826a49SYabin Cui *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + dmsIndexDelta));
763*01826a49SYabin Cui if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
764*01826a49SYabin Cui }
765*01826a49SYabin Cui
766*01826a49SYabin Cui if (matchIndex <= dmsMinChain) break;
767*01826a49SYabin Cui
768*01826a49SYabin Cui matchIndex = dmsChainTable[matchIndex & dmsChainMask];
769*01826a49SYabin Cui }
770*01826a49SYabin Cui }
771*01826a49SYabin Cui
772*01826a49SYabin Cui return ml;
773*01826a49SYabin Cui }
774*01826a49SYabin Cui
775*01826a49SYabin Cui /* *********************************
776*01826a49SYabin Cui * (SIMD) Row-based matchfinder
777*01826a49SYabin Cui ***********************************/
778*01826a49SYabin Cui /* Constants for row-based hash */
779*01826a49SYabin Cui #define ZSTD_ROW_HASH_TAG_MASK ((1u << ZSTD_ROW_HASH_TAG_BITS) - 1)
780*01826a49SYabin Cui #define ZSTD_ROW_HASH_MAX_ENTRIES 64 /* absolute maximum number of entries per row, for all configurations */
781*01826a49SYabin Cui
782*01826a49SYabin Cui #define ZSTD_ROW_HASH_CACHE_MASK (ZSTD_ROW_HASH_CACHE_SIZE - 1)
783*01826a49SYabin Cui
784*01826a49SYabin Cui typedef U64 ZSTD_VecMask; /* Clarifies when we are interacting with a U64 representing a mask of matches */
785*01826a49SYabin Cui
786*01826a49SYabin Cui /* ZSTD_VecMask_next():
787*01826a49SYabin Cui * Starting from the LSB, returns the idx of the next non-zero bit.
788*01826a49SYabin Cui * Basically counting the nb of trailing zeroes.
789*01826a49SYabin Cui */
ZSTD_VecMask_next(ZSTD_VecMask val)790*01826a49SYabin Cui MEM_STATIC U32 ZSTD_VecMask_next(ZSTD_VecMask val) {
791*01826a49SYabin Cui return ZSTD_countTrailingZeros64(val);
792*01826a49SYabin Cui }
793*01826a49SYabin Cui
794*01826a49SYabin Cui /* ZSTD_row_nextIndex():
795*01826a49SYabin Cui * Returns the next index to insert at within a tagTable row, and updates the "head"
796*01826a49SYabin Cui * value to reflect the update. Essentially cycles backwards from [1, {entries per row})
797*01826a49SYabin Cui */
ZSTD_row_nextIndex(BYTE * const tagRow,U32 const rowMask)798*01826a49SYabin Cui FORCE_INLINE_TEMPLATE U32 ZSTD_row_nextIndex(BYTE* const tagRow, U32 const rowMask) {
799*01826a49SYabin Cui U32 next = (*tagRow-1) & rowMask;
800*01826a49SYabin Cui next += (next == 0) ? rowMask : 0; /* skip first position */
801*01826a49SYabin Cui *tagRow = (BYTE)next;
802*01826a49SYabin Cui return next;
803*01826a49SYabin Cui }
804*01826a49SYabin Cui
805*01826a49SYabin Cui /* ZSTD_isAligned():
806*01826a49SYabin Cui * Checks that a pointer is aligned to "align" bytes which must be a power of 2.
807*01826a49SYabin Cui */
ZSTD_isAligned(void const * ptr,size_t align)808*01826a49SYabin Cui MEM_STATIC int ZSTD_isAligned(void const* ptr, size_t align) {
809*01826a49SYabin Cui assert((align & (align - 1)) == 0);
810*01826a49SYabin Cui return (((size_t)ptr) & (align - 1)) == 0;
811*01826a49SYabin Cui }
812*01826a49SYabin Cui
813*01826a49SYabin Cui /* ZSTD_row_prefetch():
814*01826a49SYabin Cui * Performs prefetching for the hashTable and tagTable at a given row.
815*01826a49SYabin Cui */
ZSTD_row_prefetch(U32 const * hashTable,BYTE const * tagTable,U32 const relRow,U32 const rowLog)816*01826a49SYabin Cui FORCE_INLINE_TEMPLATE void ZSTD_row_prefetch(U32 const* hashTable, BYTE const* tagTable, U32 const relRow, U32 const rowLog) {
817*01826a49SYabin Cui PREFETCH_L1(hashTable + relRow);
818*01826a49SYabin Cui if (rowLog >= 5) {
819*01826a49SYabin Cui PREFETCH_L1(hashTable + relRow + 16);
820*01826a49SYabin Cui /* Note: prefetching more of the hash table does not appear to be beneficial for 128-entry rows */
821*01826a49SYabin Cui }
822*01826a49SYabin Cui PREFETCH_L1(tagTable + relRow);
823*01826a49SYabin Cui if (rowLog == 6) {
824*01826a49SYabin Cui PREFETCH_L1(tagTable + relRow + 32);
825*01826a49SYabin Cui }
826*01826a49SYabin Cui assert(rowLog == 4 || rowLog == 5 || rowLog == 6);
827*01826a49SYabin Cui assert(ZSTD_isAligned(hashTable + relRow, 64)); /* prefetched hash row always 64-byte aligned */
828*01826a49SYabin Cui assert(ZSTD_isAligned(tagTable + relRow, (size_t)1 << rowLog)); /* prefetched tagRow sits on correct multiple of bytes (32,64,128) */
829*01826a49SYabin Cui }
830*01826a49SYabin Cui
831*01826a49SYabin Cui /* ZSTD_row_fillHashCache():
832*01826a49SYabin Cui * Fill up the hash cache starting at idx, prefetching up to ZSTD_ROW_HASH_CACHE_SIZE entries,
833*01826a49SYabin Cui * but not beyond iLimit.
834*01826a49SYabin Cui */
835*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
836*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_row_fillHashCache(ZSTD_matchState_t * ms,const BYTE * base,U32 const rowLog,U32 const mls,U32 idx,const BYTE * const iLimit)837*01826a49SYabin Cui void ZSTD_row_fillHashCache(ZSTD_matchState_t* ms, const BYTE* base,
838*01826a49SYabin Cui U32 const rowLog, U32 const mls,
839*01826a49SYabin Cui U32 idx, const BYTE* const iLimit)
840*01826a49SYabin Cui {
841*01826a49SYabin Cui U32 const* const hashTable = ms->hashTable;
842*01826a49SYabin Cui BYTE const* const tagTable = ms->tagTable;
843*01826a49SYabin Cui U32 const hashLog = ms->rowHashLog;
844*01826a49SYabin Cui U32 const maxElemsToPrefetch = (base + idx) > iLimit ? 0 : (U32)(iLimit - (base + idx) + 1);
845*01826a49SYabin Cui U32 const lim = idx + MIN(ZSTD_ROW_HASH_CACHE_SIZE, maxElemsToPrefetch);
846*01826a49SYabin Cui
847*01826a49SYabin Cui for (; idx < lim; ++idx) {
848*01826a49SYabin Cui U32 const hash = (U32)ZSTD_hashPtrSalted(base + idx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, ms->hashSalt);
849*01826a49SYabin Cui U32 const row = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
850*01826a49SYabin Cui ZSTD_row_prefetch(hashTable, tagTable, row, rowLog);
851*01826a49SYabin Cui ms->hashCache[idx & ZSTD_ROW_HASH_CACHE_MASK] = hash;
852*01826a49SYabin Cui }
853*01826a49SYabin Cui
854*01826a49SYabin Cui DEBUGLOG(6, "ZSTD_row_fillHashCache(): [%u %u %u %u %u %u %u %u]", ms->hashCache[0], ms->hashCache[1],
855*01826a49SYabin Cui ms->hashCache[2], ms->hashCache[3], ms->hashCache[4],
856*01826a49SYabin Cui ms->hashCache[5], ms->hashCache[6], ms->hashCache[7]);
857*01826a49SYabin Cui }
858*01826a49SYabin Cui
859*01826a49SYabin Cui /* ZSTD_row_nextCachedHash():
860*01826a49SYabin Cui * Returns the hash of base + idx, and replaces the hash in the hash cache with the byte at
861*01826a49SYabin Cui * base + idx + ZSTD_ROW_HASH_CACHE_SIZE. Also prefetches the appropriate rows from hashTable and tagTable.
862*01826a49SYabin Cui */
863*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
864*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_row_nextCachedHash(U32 * cache,U32 const * hashTable,BYTE const * tagTable,BYTE const * base,U32 idx,U32 const hashLog,U32 const rowLog,U32 const mls,U64 const hashSalt)865*01826a49SYabin Cui U32 ZSTD_row_nextCachedHash(U32* cache, U32 const* hashTable,
866*01826a49SYabin Cui BYTE const* tagTable, BYTE const* base,
867*01826a49SYabin Cui U32 idx, U32 const hashLog,
868*01826a49SYabin Cui U32 const rowLog, U32 const mls,
869*01826a49SYabin Cui U64 const hashSalt)
870*01826a49SYabin Cui {
871*01826a49SYabin Cui U32 const newHash = (U32)ZSTD_hashPtrSalted(base+idx+ZSTD_ROW_HASH_CACHE_SIZE, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, hashSalt);
872*01826a49SYabin Cui U32 const row = (newHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
873*01826a49SYabin Cui ZSTD_row_prefetch(hashTable, tagTable, row, rowLog);
874*01826a49SYabin Cui { U32 const hash = cache[idx & ZSTD_ROW_HASH_CACHE_MASK];
875*01826a49SYabin Cui cache[idx & ZSTD_ROW_HASH_CACHE_MASK] = newHash;
876*01826a49SYabin Cui return hash;
877*01826a49SYabin Cui }
878*01826a49SYabin Cui }
879*01826a49SYabin Cui
880*01826a49SYabin Cui /* ZSTD_row_update_internalImpl():
881*01826a49SYabin Cui * Updates the hash table with positions starting from updateStartIdx until updateEndIdx.
882*01826a49SYabin Cui */
883*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
884*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_row_update_internalImpl(ZSTD_matchState_t * ms,U32 updateStartIdx,U32 const updateEndIdx,U32 const mls,U32 const rowLog,U32 const rowMask,U32 const useCache)885*01826a49SYabin Cui void ZSTD_row_update_internalImpl(ZSTD_matchState_t* ms,
886*01826a49SYabin Cui U32 updateStartIdx, U32 const updateEndIdx,
887*01826a49SYabin Cui U32 const mls, U32 const rowLog,
888*01826a49SYabin Cui U32 const rowMask, U32 const useCache)
889*01826a49SYabin Cui {
890*01826a49SYabin Cui U32* const hashTable = ms->hashTable;
891*01826a49SYabin Cui BYTE* const tagTable = ms->tagTable;
892*01826a49SYabin Cui U32 const hashLog = ms->rowHashLog;
893*01826a49SYabin Cui const BYTE* const base = ms->window.base;
894*01826a49SYabin Cui
895*01826a49SYabin Cui DEBUGLOG(6, "ZSTD_row_update_internalImpl(): updateStartIdx=%u, updateEndIdx=%u", updateStartIdx, updateEndIdx);
896*01826a49SYabin Cui for (; updateStartIdx < updateEndIdx; ++updateStartIdx) {
897*01826a49SYabin Cui U32 const hash = useCache ? ZSTD_row_nextCachedHash(ms->hashCache, hashTable, tagTable, base, updateStartIdx, hashLog, rowLog, mls, ms->hashSalt)
898*01826a49SYabin Cui : (U32)ZSTD_hashPtrSalted(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, ms->hashSalt);
899*01826a49SYabin Cui U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
900*01826a49SYabin Cui U32* const row = hashTable + relRow;
901*01826a49SYabin Cui BYTE* tagRow = tagTable + relRow;
902*01826a49SYabin Cui U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask);
903*01826a49SYabin Cui
904*01826a49SYabin Cui assert(hash == ZSTD_hashPtrSalted(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, ms->hashSalt));
905*01826a49SYabin Cui tagRow[pos] = hash & ZSTD_ROW_HASH_TAG_MASK;
906*01826a49SYabin Cui row[pos] = updateStartIdx;
907*01826a49SYabin Cui }
908*01826a49SYabin Cui }
909*01826a49SYabin Cui
910*01826a49SYabin Cui /* ZSTD_row_update_internal():
911*01826a49SYabin Cui * Inserts the byte at ip into the appropriate position in the hash table, and updates ms->nextToUpdate.
912*01826a49SYabin Cui * Skips sections of long matches as is necessary.
913*01826a49SYabin Cui */
914*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
915*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_row_update_internal(ZSTD_matchState_t * ms,const BYTE * ip,U32 const mls,U32 const rowLog,U32 const rowMask,U32 const useCache)916*01826a49SYabin Cui void ZSTD_row_update_internal(ZSTD_matchState_t* ms, const BYTE* ip,
917*01826a49SYabin Cui U32 const mls, U32 const rowLog,
918*01826a49SYabin Cui U32 const rowMask, U32 const useCache)
919*01826a49SYabin Cui {
920*01826a49SYabin Cui U32 idx = ms->nextToUpdate;
921*01826a49SYabin Cui const BYTE* const base = ms->window.base;
922*01826a49SYabin Cui const U32 target = (U32)(ip - base);
923*01826a49SYabin Cui const U32 kSkipThreshold = 384;
924*01826a49SYabin Cui const U32 kMaxMatchStartPositionsToUpdate = 96;
925*01826a49SYabin Cui const U32 kMaxMatchEndPositionsToUpdate = 32;
926*01826a49SYabin Cui
927*01826a49SYabin Cui if (useCache) {
928*01826a49SYabin Cui /* Only skip positions when using hash cache, i.e.
929*01826a49SYabin Cui * if we are loading a dict, don't skip anything.
930*01826a49SYabin Cui * If we decide to skip, then we only update a set number
931*01826a49SYabin Cui * of positions at the beginning and end of the match.
932*01826a49SYabin Cui */
933*01826a49SYabin Cui if (UNLIKELY(target - idx > kSkipThreshold)) {
934*01826a49SYabin Cui U32 const bound = idx + kMaxMatchStartPositionsToUpdate;
935*01826a49SYabin Cui ZSTD_row_update_internalImpl(ms, idx, bound, mls, rowLog, rowMask, useCache);
936*01826a49SYabin Cui idx = target - kMaxMatchEndPositionsToUpdate;
937*01826a49SYabin Cui ZSTD_row_fillHashCache(ms, base, rowLog, mls, idx, ip+1);
938*01826a49SYabin Cui }
939*01826a49SYabin Cui }
940*01826a49SYabin Cui assert(target >= idx);
941*01826a49SYabin Cui ZSTD_row_update_internalImpl(ms, idx, target, mls, rowLog, rowMask, useCache);
942*01826a49SYabin Cui ms->nextToUpdate = target;
943*01826a49SYabin Cui }
944*01826a49SYabin Cui
945*01826a49SYabin Cui /* ZSTD_row_update():
946*01826a49SYabin Cui * External wrapper for ZSTD_row_update_internal(). Used for filling the hashtable during dictionary
947*01826a49SYabin Cui * processing.
948*01826a49SYabin Cui */
ZSTD_row_update(ZSTD_matchState_t * const ms,const BYTE * ip)949*01826a49SYabin Cui void ZSTD_row_update(ZSTD_matchState_t* const ms, const BYTE* ip) {
950*01826a49SYabin Cui const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6);
951*01826a49SYabin Cui const U32 rowMask = (1u << rowLog) - 1;
952*01826a49SYabin Cui const U32 mls = MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */);
953*01826a49SYabin Cui
954*01826a49SYabin Cui DEBUGLOG(5, "ZSTD_row_update(), rowLog=%u", rowLog);
955*01826a49SYabin Cui ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 0 /* don't use cache */);
956*01826a49SYabin Cui }
957*01826a49SYabin Cui
958*01826a49SYabin Cui /* Returns the mask width of bits group of which will be set to 1. Given not all
959*01826a49SYabin Cui * architectures have easy movemask instruction, this helps to iterate over
960*01826a49SYabin Cui * groups of bits easier and faster.
961*01826a49SYabin Cui */
962*01826a49SYabin Cui FORCE_INLINE_TEMPLATE U32
ZSTD_row_matchMaskGroupWidth(const U32 rowEntries)963*01826a49SYabin Cui ZSTD_row_matchMaskGroupWidth(const U32 rowEntries)
964*01826a49SYabin Cui {
965*01826a49SYabin Cui assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64);
966*01826a49SYabin Cui assert(rowEntries <= ZSTD_ROW_HASH_MAX_ENTRIES);
967*01826a49SYabin Cui (void)rowEntries;
968*01826a49SYabin Cui #if defined(ZSTD_ARCH_ARM_NEON)
969*01826a49SYabin Cui /* NEON path only works for little endian */
970*01826a49SYabin Cui if (!MEM_isLittleEndian()) {
971*01826a49SYabin Cui return 1;
972*01826a49SYabin Cui }
973*01826a49SYabin Cui if (rowEntries == 16) {
974*01826a49SYabin Cui return 4;
975*01826a49SYabin Cui }
976*01826a49SYabin Cui if (rowEntries == 32) {
977*01826a49SYabin Cui return 2;
978*01826a49SYabin Cui }
979*01826a49SYabin Cui if (rowEntries == 64) {
980*01826a49SYabin Cui return 1;
981*01826a49SYabin Cui }
982*01826a49SYabin Cui #endif
983*01826a49SYabin Cui return 1;
984*01826a49SYabin Cui }
985*01826a49SYabin Cui
986*01826a49SYabin Cui #if defined(ZSTD_ARCH_X86_SSE2)
987*01826a49SYabin Cui FORCE_INLINE_TEMPLATE ZSTD_VecMask
ZSTD_row_getSSEMask(int nbChunks,const BYTE * const src,const BYTE tag,const U32 head)988*01826a49SYabin Cui ZSTD_row_getSSEMask(int nbChunks, const BYTE* const src, const BYTE tag, const U32 head)
989*01826a49SYabin Cui {
990*01826a49SYabin Cui const __m128i comparisonMask = _mm_set1_epi8((char)tag);
991*01826a49SYabin Cui int matches[4] = {0};
992*01826a49SYabin Cui int i;
993*01826a49SYabin Cui assert(nbChunks == 1 || nbChunks == 2 || nbChunks == 4);
994*01826a49SYabin Cui for (i=0; i<nbChunks; i++) {
995*01826a49SYabin Cui const __m128i chunk = _mm_loadu_si128((const __m128i*)(const void*)(src + 16*i));
996*01826a49SYabin Cui const __m128i equalMask = _mm_cmpeq_epi8(chunk, comparisonMask);
997*01826a49SYabin Cui matches[i] = _mm_movemask_epi8(equalMask);
998*01826a49SYabin Cui }
999*01826a49SYabin Cui if (nbChunks == 1) return ZSTD_rotateRight_U16((U16)matches[0], head);
1000*01826a49SYabin Cui if (nbChunks == 2) return ZSTD_rotateRight_U32((U32)matches[1] << 16 | (U32)matches[0], head);
1001*01826a49SYabin Cui assert(nbChunks == 4);
1002*01826a49SYabin Cui return ZSTD_rotateRight_U64((U64)matches[3] << 48 | (U64)matches[2] << 32 | (U64)matches[1] << 16 | (U64)matches[0], head);
1003*01826a49SYabin Cui }
1004*01826a49SYabin Cui #endif
1005*01826a49SYabin Cui
1006*01826a49SYabin Cui #if defined(ZSTD_ARCH_ARM_NEON)
1007*01826a49SYabin Cui FORCE_INLINE_TEMPLATE ZSTD_VecMask
ZSTD_row_getNEONMask(const U32 rowEntries,const BYTE * const src,const BYTE tag,const U32 headGrouped)1008*01826a49SYabin Cui ZSTD_row_getNEONMask(const U32 rowEntries, const BYTE* const src, const BYTE tag, const U32 headGrouped)
1009*01826a49SYabin Cui {
1010*01826a49SYabin Cui assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64);
1011*01826a49SYabin Cui if (rowEntries == 16) {
1012*01826a49SYabin Cui /* vshrn_n_u16 shifts by 4 every u16 and narrows to 8 lower bits.
1013*01826a49SYabin Cui * After that groups of 4 bits represent the equalMask. We lower
1014*01826a49SYabin Cui * all bits except the highest in these groups by doing AND with
1015*01826a49SYabin Cui * 0x88 = 0b10001000.
1016*01826a49SYabin Cui */
1017*01826a49SYabin Cui const uint8x16_t chunk = vld1q_u8(src);
1018*01826a49SYabin Cui const uint16x8_t equalMask = vreinterpretq_u16_u8(vceqq_u8(chunk, vdupq_n_u8(tag)));
1019*01826a49SYabin Cui const uint8x8_t res = vshrn_n_u16(equalMask, 4);
1020*01826a49SYabin Cui const U64 matches = vget_lane_u64(vreinterpret_u64_u8(res), 0);
1021*01826a49SYabin Cui return ZSTD_rotateRight_U64(matches, headGrouped) & 0x8888888888888888ull;
1022*01826a49SYabin Cui } else if (rowEntries == 32) {
1023*01826a49SYabin Cui /* Same idea as with rowEntries == 16 but doing AND with
1024*01826a49SYabin Cui * 0x55 = 0b01010101.
1025*01826a49SYabin Cui */
1026*01826a49SYabin Cui const uint16x8x2_t chunk = vld2q_u16((const uint16_t*)(const void*)src);
1027*01826a49SYabin Cui const uint8x16_t chunk0 = vreinterpretq_u8_u16(chunk.val[0]);
1028*01826a49SYabin Cui const uint8x16_t chunk1 = vreinterpretq_u8_u16(chunk.val[1]);
1029*01826a49SYabin Cui const uint8x16_t dup = vdupq_n_u8(tag);
1030*01826a49SYabin Cui const uint8x8_t t0 = vshrn_n_u16(vreinterpretq_u16_u8(vceqq_u8(chunk0, dup)), 6);
1031*01826a49SYabin Cui const uint8x8_t t1 = vshrn_n_u16(vreinterpretq_u16_u8(vceqq_u8(chunk1, dup)), 6);
1032*01826a49SYabin Cui const uint8x8_t res = vsli_n_u8(t0, t1, 4);
1033*01826a49SYabin Cui const U64 matches = vget_lane_u64(vreinterpret_u64_u8(res), 0) ;
1034*01826a49SYabin Cui return ZSTD_rotateRight_U64(matches, headGrouped) & 0x5555555555555555ull;
1035*01826a49SYabin Cui } else { /* rowEntries == 64 */
1036*01826a49SYabin Cui const uint8x16x4_t chunk = vld4q_u8(src);
1037*01826a49SYabin Cui const uint8x16_t dup = vdupq_n_u8(tag);
1038*01826a49SYabin Cui const uint8x16_t cmp0 = vceqq_u8(chunk.val[0], dup);
1039*01826a49SYabin Cui const uint8x16_t cmp1 = vceqq_u8(chunk.val[1], dup);
1040*01826a49SYabin Cui const uint8x16_t cmp2 = vceqq_u8(chunk.val[2], dup);
1041*01826a49SYabin Cui const uint8x16_t cmp3 = vceqq_u8(chunk.val[3], dup);
1042*01826a49SYabin Cui
1043*01826a49SYabin Cui const uint8x16_t t0 = vsriq_n_u8(cmp1, cmp0, 1);
1044*01826a49SYabin Cui const uint8x16_t t1 = vsriq_n_u8(cmp3, cmp2, 1);
1045*01826a49SYabin Cui const uint8x16_t t2 = vsriq_n_u8(t1, t0, 2);
1046*01826a49SYabin Cui const uint8x16_t t3 = vsriq_n_u8(t2, t2, 4);
1047*01826a49SYabin Cui const uint8x8_t t4 = vshrn_n_u16(vreinterpretq_u16_u8(t3), 4);
1048*01826a49SYabin Cui const U64 matches = vget_lane_u64(vreinterpret_u64_u8(t4), 0);
1049*01826a49SYabin Cui return ZSTD_rotateRight_U64(matches, headGrouped);
1050*01826a49SYabin Cui }
1051*01826a49SYabin Cui }
1052*01826a49SYabin Cui #endif
1053*01826a49SYabin Cui
1054*01826a49SYabin Cui /* Returns a ZSTD_VecMask (U64) that has the nth group (determined by
1055*01826a49SYabin Cui * ZSTD_row_matchMaskGroupWidth) of bits set to 1 if the newly-computed "tag"
1056*01826a49SYabin Cui * matches the hash at the nth position in a row of the tagTable.
1057*01826a49SYabin Cui * Each row is a circular buffer beginning at the value of "headGrouped". So we
1058*01826a49SYabin Cui * must rotate the "matches" bitfield to match up with the actual layout of the
1059*01826a49SYabin Cui * entries within the hashTable */
1060*01826a49SYabin Cui FORCE_INLINE_TEMPLATE ZSTD_VecMask
ZSTD_row_getMatchMask(const BYTE * const tagRow,const BYTE tag,const U32 headGrouped,const U32 rowEntries)1061*01826a49SYabin Cui ZSTD_row_getMatchMask(const BYTE* const tagRow, const BYTE tag, const U32 headGrouped, const U32 rowEntries)
1062*01826a49SYabin Cui {
1063*01826a49SYabin Cui const BYTE* const src = tagRow;
1064*01826a49SYabin Cui assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64);
1065*01826a49SYabin Cui assert(rowEntries <= ZSTD_ROW_HASH_MAX_ENTRIES);
1066*01826a49SYabin Cui assert(ZSTD_row_matchMaskGroupWidth(rowEntries) * rowEntries <= sizeof(ZSTD_VecMask) * 8);
1067*01826a49SYabin Cui
1068*01826a49SYabin Cui #if defined(ZSTD_ARCH_X86_SSE2)
1069*01826a49SYabin Cui
1070*01826a49SYabin Cui return ZSTD_row_getSSEMask(rowEntries / 16, src, tag, headGrouped);
1071*01826a49SYabin Cui
1072*01826a49SYabin Cui #else /* SW or NEON-LE */
1073*01826a49SYabin Cui
1074*01826a49SYabin Cui # if defined(ZSTD_ARCH_ARM_NEON)
1075*01826a49SYabin Cui /* This NEON path only works for little endian - otherwise use SWAR below */
1076*01826a49SYabin Cui if (MEM_isLittleEndian()) {
1077*01826a49SYabin Cui return ZSTD_row_getNEONMask(rowEntries, src, tag, headGrouped);
1078*01826a49SYabin Cui }
1079*01826a49SYabin Cui # endif /* ZSTD_ARCH_ARM_NEON */
1080*01826a49SYabin Cui /* SWAR */
1081*01826a49SYabin Cui { const int chunkSize = sizeof(size_t);
1082*01826a49SYabin Cui const size_t shiftAmount = ((chunkSize * 8) - chunkSize);
1083*01826a49SYabin Cui const size_t xFF = ~((size_t)0);
1084*01826a49SYabin Cui const size_t x01 = xFF / 0xFF;
1085*01826a49SYabin Cui const size_t x80 = x01 << 7;
1086*01826a49SYabin Cui const size_t splatChar = tag * x01;
1087*01826a49SYabin Cui ZSTD_VecMask matches = 0;
1088*01826a49SYabin Cui int i = rowEntries - chunkSize;
1089*01826a49SYabin Cui assert((sizeof(size_t) == 4) || (sizeof(size_t) == 8));
1090*01826a49SYabin Cui if (MEM_isLittleEndian()) { /* runtime check so have two loops */
1091*01826a49SYabin Cui const size_t extractMagic = (xFF / 0x7F) >> chunkSize;
1092*01826a49SYabin Cui do {
1093*01826a49SYabin Cui size_t chunk = MEM_readST(&src[i]);
1094*01826a49SYabin Cui chunk ^= splatChar;
1095*01826a49SYabin Cui chunk = (((chunk | x80) - x01) | chunk) & x80;
1096*01826a49SYabin Cui matches <<= chunkSize;
1097*01826a49SYabin Cui matches |= (chunk * extractMagic) >> shiftAmount;
1098*01826a49SYabin Cui i -= chunkSize;
1099*01826a49SYabin Cui } while (i >= 0);
1100*01826a49SYabin Cui } else { /* big endian: reverse bits during extraction */
1101*01826a49SYabin Cui const size_t msb = xFF ^ (xFF >> 1);
1102*01826a49SYabin Cui const size_t extractMagic = (msb / 0x1FF) | msb;
1103*01826a49SYabin Cui do {
1104*01826a49SYabin Cui size_t chunk = MEM_readST(&src[i]);
1105*01826a49SYabin Cui chunk ^= splatChar;
1106*01826a49SYabin Cui chunk = (((chunk | x80) - x01) | chunk) & x80;
1107*01826a49SYabin Cui matches <<= chunkSize;
1108*01826a49SYabin Cui matches |= ((chunk >> 7) * extractMagic) >> shiftAmount;
1109*01826a49SYabin Cui i -= chunkSize;
1110*01826a49SYabin Cui } while (i >= 0);
1111*01826a49SYabin Cui }
1112*01826a49SYabin Cui matches = ~matches;
1113*01826a49SYabin Cui if (rowEntries == 16) {
1114*01826a49SYabin Cui return ZSTD_rotateRight_U16((U16)matches, headGrouped);
1115*01826a49SYabin Cui } else if (rowEntries == 32) {
1116*01826a49SYabin Cui return ZSTD_rotateRight_U32((U32)matches, headGrouped);
1117*01826a49SYabin Cui } else {
1118*01826a49SYabin Cui return ZSTD_rotateRight_U64((U64)matches, headGrouped);
1119*01826a49SYabin Cui }
1120*01826a49SYabin Cui }
1121*01826a49SYabin Cui #endif
1122*01826a49SYabin Cui }
1123*01826a49SYabin Cui
1124*01826a49SYabin Cui /* The high-level approach of the SIMD row based match finder is as follows:
1125*01826a49SYabin Cui * - Figure out where to insert the new entry:
1126*01826a49SYabin Cui * - Generate a hash for current input posistion and split it into a one byte of tag and `rowHashLog` bits of index.
1127*01826a49SYabin Cui * - The hash is salted by a value that changes on every contex reset, so when the same table is used
1128*01826a49SYabin Cui * we will avoid collisions that would otherwise slow us down by intorducing phantom matches.
1129*01826a49SYabin Cui * - The hashTable is effectively split into groups or "rows" of 15 or 31 entries of U32, and the index determines
1130*01826a49SYabin Cui * which row to insert into.
1131*01826a49SYabin Cui * - Determine the correct position within the row to insert the entry into. Each row of 15 or 31 can
1132*01826a49SYabin Cui * be considered as a circular buffer with a "head" index that resides in the tagTable (overall 16 or 32 bytes
1133*01826a49SYabin Cui * per row).
1134*01826a49SYabin Cui * - Use SIMD to efficiently compare the tags in the tagTable to the 1-byte tag calculated for the position and
1135*01826a49SYabin Cui * generate a bitfield that we can cycle through to check the collisions in the hash table.
1136*01826a49SYabin Cui * - Pick the longest match.
1137*01826a49SYabin Cui * - Insert the tag into the equivalent row and position in the tagTable.
1138*01826a49SYabin Cui */
1139*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
1140*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_RowFindBestMatch(ZSTD_matchState_t * ms,const BYTE * const ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 mls,const ZSTD_dictMode_e dictMode,const U32 rowLog)1141*01826a49SYabin Cui size_t ZSTD_RowFindBestMatch(
1142*01826a49SYabin Cui ZSTD_matchState_t* ms,
1143*01826a49SYabin Cui const BYTE* const ip, const BYTE* const iLimit,
1144*01826a49SYabin Cui size_t* offsetPtr,
1145*01826a49SYabin Cui const U32 mls, const ZSTD_dictMode_e dictMode,
1146*01826a49SYabin Cui const U32 rowLog)
1147*01826a49SYabin Cui {
1148*01826a49SYabin Cui U32* const hashTable = ms->hashTable;
1149*01826a49SYabin Cui BYTE* const tagTable = ms->tagTable;
1150*01826a49SYabin Cui U32* const hashCache = ms->hashCache;
1151*01826a49SYabin Cui const U32 hashLog = ms->rowHashLog;
1152*01826a49SYabin Cui const ZSTD_compressionParameters* const cParams = &ms->cParams;
1153*01826a49SYabin Cui const BYTE* const base = ms->window.base;
1154*01826a49SYabin Cui const BYTE* const dictBase = ms->window.dictBase;
1155*01826a49SYabin Cui const U32 dictLimit = ms->window.dictLimit;
1156*01826a49SYabin Cui const BYTE* const prefixStart = base + dictLimit;
1157*01826a49SYabin Cui const BYTE* const dictEnd = dictBase + dictLimit;
1158*01826a49SYabin Cui const U32 curr = (U32)(ip-base);
1159*01826a49SYabin Cui const U32 maxDistance = 1U << cParams->windowLog;
1160*01826a49SYabin Cui const U32 lowestValid = ms->window.lowLimit;
1161*01826a49SYabin Cui const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
1162*01826a49SYabin Cui const U32 isDictionary = (ms->loadedDictEnd != 0);
1163*01826a49SYabin Cui const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance;
1164*01826a49SYabin Cui const U32 rowEntries = (1U << rowLog);
1165*01826a49SYabin Cui const U32 rowMask = rowEntries - 1;
1166*01826a49SYabin Cui const U32 cappedSearchLog = MIN(cParams->searchLog, rowLog); /* nb of searches is capped at nb entries per row */
1167*01826a49SYabin Cui const U32 groupWidth = ZSTD_row_matchMaskGroupWidth(rowEntries);
1168*01826a49SYabin Cui const U64 hashSalt = ms->hashSalt;
1169*01826a49SYabin Cui U32 nbAttempts = 1U << cappedSearchLog;
1170*01826a49SYabin Cui size_t ml=4-1;
1171*01826a49SYabin Cui U32 hash;
1172*01826a49SYabin Cui
1173*01826a49SYabin Cui /* DMS/DDS variables that may be referenced laster */
1174*01826a49SYabin Cui const ZSTD_matchState_t* const dms = ms->dictMatchState;
1175*01826a49SYabin Cui
1176*01826a49SYabin Cui /* Initialize the following variables to satisfy static analyzer */
1177*01826a49SYabin Cui size_t ddsIdx = 0;
1178*01826a49SYabin Cui U32 ddsExtraAttempts = 0; /* cctx hash tables are limited in searches, but allow extra searches into DDS */
1179*01826a49SYabin Cui U32 dmsTag = 0;
1180*01826a49SYabin Cui U32* dmsRow = NULL;
1181*01826a49SYabin Cui BYTE* dmsTagRow = NULL;
1182*01826a49SYabin Cui
1183*01826a49SYabin Cui if (dictMode == ZSTD_dedicatedDictSearch) {
1184*01826a49SYabin Cui const U32 ddsHashLog = dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG;
1185*01826a49SYabin Cui { /* Prefetch DDS hashtable entry */
1186*01826a49SYabin Cui ddsIdx = ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG;
1187*01826a49SYabin Cui PREFETCH_L1(&dms->hashTable[ddsIdx]);
1188*01826a49SYabin Cui }
1189*01826a49SYabin Cui ddsExtraAttempts = cParams->searchLog > rowLog ? 1U << (cParams->searchLog - rowLog) : 0;
1190*01826a49SYabin Cui }
1191*01826a49SYabin Cui
1192*01826a49SYabin Cui if (dictMode == ZSTD_dictMatchState) {
1193*01826a49SYabin Cui /* Prefetch DMS rows */
1194*01826a49SYabin Cui U32* const dmsHashTable = dms->hashTable;
1195*01826a49SYabin Cui BYTE* const dmsTagTable = dms->tagTable;
1196*01826a49SYabin Cui U32 const dmsHash = (U32)ZSTD_hashPtr(ip, dms->rowHashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
1197*01826a49SYabin Cui U32 const dmsRelRow = (dmsHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
1198*01826a49SYabin Cui dmsTag = dmsHash & ZSTD_ROW_HASH_TAG_MASK;
1199*01826a49SYabin Cui dmsTagRow = (BYTE*)(dmsTagTable + dmsRelRow);
1200*01826a49SYabin Cui dmsRow = dmsHashTable + dmsRelRow;
1201*01826a49SYabin Cui ZSTD_row_prefetch(dmsHashTable, dmsTagTable, dmsRelRow, rowLog);
1202*01826a49SYabin Cui }
1203*01826a49SYabin Cui
1204*01826a49SYabin Cui /* Update the hashTable and tagTable up to (but not including) ip */
1205*01826a49SYabin Cui if (!ms->lazySkipping) {
1206*01826a49SYabin Cui ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 1 /* useCache */);
1207*01826a49SYabin Cui hash = ZSTD_row_nextCachedHash(hashCache, hashTable, tagTable, base, curr, hashLog, rowLog, mls, hashSalt);
1208*01826a49SYabin Cui } else {
1209*01826a49SYabin Cui /* Stop inserting every position when in the lazy skipping mode.
1210*01826a49SYabin Cui * The hash cache is also not kept up to date in this mode.
1211*01826a49SYabin Cui */
1212*01826a49SYabin Cui hash = (U32)ZSTD_hashPtrSalted(ip, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, hashSalt);
1213*01826a49SYabin Cui ms->nextToUpdate = curr;
1214*01826a49SYabin Cui }
1215*01826a49SYabin Cui ms->hashSaltEntropy += hash; /* collect salt entropy */
1216*01826a49SYabin Cui
1217*01826a49SYabin Cui { /* Get the hash for ip, compute the appropriate row */
1218*01826a49SYabin Cui U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
1219*01826a49SYabin Cui U32 const tag = hash & ZSTD_ROW_HASH_TAG_MASK;
1220*01826a49SYabin Cui U32* const row = hashTable + relRow;
1221*01826a49SYabin Cui BYTE* tagRow = (BYTE*)(tagTable + relRow);
1222*01826a49SYabin Cui U32 const headGrouped = (*tagRow & rowMask) * groupWidth;
1223*01826a49SYabin Cui U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES];
1224*01826a49SYabin Cui size_t numMatches = 0;
1225*01826a49SYabin Cui size_t currMatch = 0;
1226*01826a49SYabin Cui ZSTD_VecMask matches = ZSTD_row_getMatchMask(tagRow, (BYTE)tag, headGrouped, rowEntries);
1227*01826a49SYabin Cui
1228*01826a49SYabin Cui /* Cycle through the matches and prefetch */
1229*01826a49SYabin Cui for (; (matches > 0) && (nbAttempts > 0); matches &= (matches - 1)) {
1230*01826a49SYabin Cui U32 const matchPos = ((headGrouped + ZSTD_VecMask_next(matches)) / groupWidth) & rowMask;
1231*01826a49SYabin Cui U32 const matchIndex = row[matchPos];
1232*01826a49SYabin Cui if(matchPos == 0) continue;
1233*01826a49SYabin Cui assert(numMatches < rowEntries);
1234*01826a49SYabin Cui if (matchIndex < lowLimit)
1235*01826a49SYabin Cui break;
1236*01826a49SYabin Cui if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
1237*01826a49SYabin Cui PREFETCH_L1(base + matchIndex);
1238*01826a49SYabin Cui } else {
1239*01826a49SYabin Cui PREFETCH_L1(dictBase + matchIndex);
1240*01826a49SYabin Cui }
1241*01826a49SYabin Cui matchBuffer[numMatches++] = matchIndex;
1242*01826a49SYabin Cui --nbAttempts;
1243*01826a49SYabin Cui }
1244*01826a49SYabin Cui
1245*01826a49SYabin Cui /* Speed opt: insert current byte into hashtable too. This allows us to avoid one iteration of the loop
1246*01826a49SYabin Cui in ZSTD_row_update_internal() at the next search. */
1247*01826a49SYabin Cui {
1248*01826a49SYabin Cui U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask);
1249*01826a49SYabin Cui tagRow[pos] = (BYTE)tag;
1250*01826a49SYabin Cui row[pos] = ms->nextToUpdate++;
1251*01826a49SYabin Cui }
1252*01826a49SYabin Cui
1253*01826a49SYabin Cui /* Return the longest match */
1254*01826a49SYabin Cui for (; currMatch < numMatches; ++currMatch) {
1255*01826a49SYabin Cui U32 const matchIndex = matchBuffer[currMatch];
1256*01826a49SYabin Cui size_t currentMl=0;
1257*01826a49SYabin Cui assert(matchIndex < curr);
1258*01826a49SYabin Cui assert(matchIndex >= lowLimit);
1259*01826a49SYabin Cui
1260*01826a49SYabin Cui if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
1261*01826a49SYabin Cui const BYTE* const match = base + matchIndex;
1262*01826a49SYabin Cui assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */
1263*01826a49SYabin Cui /* read 4B starting from (match + ml + 1 - sizeof(U32)) */
1264*01826a49SYabin Cui if (MEM_read32(match + ml - 3) == MEM_read32(ip + ml - 3)) /* potentially better */
1265*01826a49SYabin Cui currentMl = ZSTD_count(ip, match, iLimit);
1266*01826a49SYabin Cui } else {
1267*01826a49SYabin Cui const BYTE* const match = dictBase + matchIndex;
1268*01826a49SYabin Cui assert(match+4 <= dictEnd);
1269*01826a49SYabin Cui if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
1270*01826a49SYabin Cui currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4;
1271*01826a49SYabin Cui }
1272*01826a49SYabin Cui
1273*01826a49SYabin Cui /* Save best solution */
1274*01826a49SYabin Cui if (currentMl > ml) {
1275*01826a49SYabin Cui ml = currentMl;
1276*01826a49SYabin Cui *offsetPtr = OFFSET_TO_OFFBASE(curr - matchIndex);
1277*01826a49SYabin Cui if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
1278*01826a49SYabin Cui }
1279*01826a49SYabin Cui }
1280*01826a49SYabin Cui }
1281*01826a49SYabin Cui
1282*01826a49SYabin Cui assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
1283*01826a49SYabin Cui if (dictMode == ZSTD_dedicatedDictSearch) {
1284*01826a49SYabin Cui ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts + ddsExtraAttempts, dms,
1285*01826a49SYabin Cui ip, iLimit, prefixStart, curr, dictLimit, ddsIdx);
1286*01826a49SYabin Cui } else if (dictMode == ZSTD_dictMatchState) {
1287*01826a49SYabin Cui /* TODO: Measure and potentially add prefetching to DMS */
1288*01826a49SYabin Cui const U32 dmsLowestIndex = dms->window.dictLimit;
1289*01826a49SYabin Cui const BYTE* const dmsBase = dms->window.base;
1290*01826a49SYabin Cui const BYTE* const dmsEnd = dms->window.nextSrc;
1291*01826a49SYabin Cui const U32 dmsSize = (U32)(dmsEnd - dmsBase);
1292*01826a49SYabin Cui const U32 dmsIndexDelta = dictLimit - dmsSize;
1293*01826a49SYabin Cui
1294*01826a49SYabin Cui { U32 const headGrouped = (*dmsTagRow & rowMask) * groupWidth;
1295*01826a49SYabin Cui U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES];
1296*01826a49SYabin Cui size_t numMatches = 0;
1297*01826a49SYabin Cui size_t currMatch = 0;
1298*01826a49SYabin Cui ZSTD_VecMask matches = ZSTD_row_getMatchMask(dmsTagRow, (BYTE)dmsTag, headGrouped, rowEntries);
1299*01826a49SYabin Cui
1300*01826a49SYabin Cui for (; (matches > 0) && (nbAttempts > 0); matches &= (matches - 1)) {
1301*01826a49SYabin Cui U32 const matchPos = ((headGrouped + ZSTD_VecMask_next(matches)) / groupWidth) & rowMask;
1302*01826a49SYabin Cui U32 const matchIndex = dmsRow[matchPos];
1303*01826a49SYabin Cui if(matchPos == 0) continue;
1304*01826a49SYabin Cui if (matchIndex < dmsLowestIndex)
1305*01826a49SYabin Cui break;
1306*01826a49SYabin Cui PREFETCH_L1(dmsBase + matchIndex);
1307*01826a49SYabin Cui matchBuffer[numMatches++] = matchIndex;
1308*01826a49SYabin Cui --nbAttempts;
1309*01826a49SYabin Cui }
1310*01826a49SYabin Cui
1311*01826a49SYabin Cui /* Return the longest match */
1312*01826a49SYabin Cui for (; currMatch < numMatches; ++currMatch) {
1313*01826a49SYabin Cui U32 const matchIndex = matchBuffer[currMatch];
1314*01826a49SYabin Cui size_t currentMl=0;
1315*01826a49SYabin Cui assert(matchIndex >= dmsLowestIndex);
1316*01826a49SYabin Cui assert(matchIndex < curr);
1317*01826a49SYabin Cui
1318*01826a49SYabin Cui { const BYTE* const match = dmsBase + matchIndex;
1319*01826a49SYabin Cui assert(match+4 <= dmsEnd);
1320*01826a49SYabin Cui if (MEM_read32(match) == MEM_read32(ip))
1321*01826a49SYabin Cui currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4;
1322*01826a49SYabin Cui }
1323*01826a49SYabin Cui
1324*01826a49SYabin Cui if (currentMl > ml) {
1325*01826a49SYabin Cui ml = currentMl;
1326*01826a49SYabin Cui assert(curr > matchIndex + dmsIndexDelta);
1327*01826a49SYabin Cui *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + dmsIndexDelta));
1328*01826a49SYabin Cui if (ip+currentMl == iLimit) break;
1329*01826a49SYabin Cui }
1330*01826a49SYabin Cui }
1331*01826a49SYabin Cui }
1332*01826a49SYabin Cui }
1333*01826a49SYabin Cui return ml;
1334*01826a49SYabin Cui }
1335*01826a49SYabin Cui
1336*01826a49SYabin Cui
1337*01826a49SYabin Cui /**
1338*01826a49SYabin Cui * Generate search functions templated on (dictMode, mls, rowLog).
1339*01826a49SYabin Cui * These functions are outlined for code size & compilation time.
1340*01826a49SYabin Cui * ZSTD_searchMax() dispatches to the correct implementation function.
1341*01826a49SYabin Cui *
1342*01826a49SYabin Cui * TODO: The start of the search function involves loading and calculating a
1343*01826a49SYabin Cui * bunch of constants from the ZSTD_matchState_t. These computations could be
1344*01826a49SYabin Cui * done in an initialization function, and saved somewhere in the match state.
1345*01826a49SYabin Cui * Then we could pass a pointer to the saved state instead of the match state,
1346*01826a49SYabin Cui * and avoid duplicate computations.
1347*01826a49SYabin Cui *
1348*01826a49SYabin Cui * TODO: Move the match re-winding into searchMax. This improves compression
1349*01826a49SYabin Cui * ratio, and unlocks further simplifications with the next TODO.
1350*01826a49SYabin Cui *
1351*01826a49SYabin Cui * TODO: Try moving the repcode search into searchMax. After the re-winding
1352*01826a49SYabin Cui * and repcode search are in searchMax, there is no more logic in the match
1353*01826a49SYabin Cui * finder loop that requires knowledge about the dictMode. So we should be
1354*01826a49SYabin Cui * able to avoid force inlining it, and we can join the extDict loop with
1355*01826a49SYabin Cui * the single segment loop. It should go in searchMax instead of its own
1356*01826a49SYabin Cui * function to avoid having multiple virtual function calls per search.
1357*01826a49SYabin Cui */
1358*01826a49SYabin Cui
1359*01826a49SYabin Cui #define ZSTD_BT_SEARCH_FN(dictMode, mls) ZSTD_BtFindBestMatch_##dictMode##_##mls
1360*01826a49SYabin Cui #define ZSTD_HC_SEARCH_FN(dictMode, mls) ZSTD_HcFindBestMatch_##dictMode##_##mls
1361*01826a49SYabin Cui #define ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog) ZSTD_RowFindBestMatch_##dictMode##_##mls##_##rowLog
1362*01826a49SYabin Cui
1363*01826a49SYabin Cui #define ZSTD_SEARCH_FN_ATTRS FORCE_NOINLINE
1364*01826a49SYabin Cui
1365*01826a49SYabin Cui #define GEN_ZSTD_BT_SEARCH_FN(dictMode, mls) \
1366*01826a49SYabin Cui ZSTD_SEARCH_FN_ATTRS size_t ZSTD_BT_SEARCH_FN(dictMode, mls)( \
1367*01826a49SYabin Cui ZSTD_matchState_t* ms, \
1368*01826a49SYabin Cui const BYTE* ip, const BYTE* const iLimit, \
1369*01826a49SYabin Cui size_t* offBasePtr) \
1370*01826a49SYabin Cui { \
1371*01826a49SYabin Cui assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
1372*01826a49SYabin Cui return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, mls, ZSTD_##dictMode); \
1373*01826a49SYabin Cui } \
1374*01826a49SYabin Cui
1375*01826a49SYabin Cui #define GEN_ZSTD_HC_SEARCH_FN(dictMode, mls) \
1376*01826a49SYabin Cui ZSTD_SEARCH_FN_ATTRS size_t ZSTD_HC_SEARCH_FN(dictMode, mls)( \
1377*01826a49SYabin Cui ZSTD_matchState_t* ms, \
1378*01826a49SYabin Cui const BYTE* ip, const BYTE* const iLimit, \
1379*01826a49SYabin Cui size_t* offsetPtr) \
1380*01826a49SYabin Cui { \
1381*01826a49SYabin Cui assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
1382*01826a49SYabin Cui return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode); \
1383*01826a49SYabin Cui } \
1384*01826a49SYabin Cui
1385*01826a49SYabin Cui #define GEN_ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog) \
1386*01826a49SYabin Cui ZSTD_SEARCH_FN_ATTRS size_t ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog)( \
1387*01826a49SYabin Cui ZSTD_matchState_t* ms, \
1388*01826a49SYabin Cui const BYTE* ip, const BYTE* const iLimit, \
1389*01826a49SYabin Cui size_t* offsetPtr) \
1390*01826a49SYabin Cui { \
1391*01826a49SYabin Cui assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
1392*01826a49SYabin Cui assert(MAX(4, MIN(6, ms->cParams.searchLog)) == rowLog); \
1393*01826a49SYabin Cui return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode, rowLog); \
1394*01826a49SYabin Cui } \
1395*01826a49SYabin Cui
1396*01826a49SYabin Cui #define ZSTD_FOR_EACH_ROWLOG(X, dictMode, mls) \
1397*01826a49SYabin Cui X(dictMode, mls, 4) \
1398*01826a49SYabin Cui X(dictMode, mls, 5) \
1399*01826a49SYabin Cui X(dictMode, mls, 6)
1400*01826a49SYabin Cui
1401*01826a49SYabin Cui #define ZSTD_FOR_EACH_MLS_ROWLOG(X, dictMode) \
1402*01826a49SYabin Cui ZSTD_FOR_EACH_ROWLOG(X, dictMode, 4) \
1403*01826a49SYabin Cui ZSTD_FOR_EACH_ROWLOG(X, dictMode, 5) \
1404*01826a49SYabin Cui ZSTD_FOR_EACH_ROWLOG(X, dictMode, 6)
1405*01826a49SYabin Cui
1406*01826a49SYabin Cui #define ZSTD_FOR_EACH_MLS(X, dictMode) \
1407*01826a49SYabin Cui X(dictMode, 4) \
1408*01826a49SYabin Cui X(dictMode, 5) \
1409*01826a49SYabin Cui X(dictMode, 6)
1410*01826a49SYabin Cui
1411*01826a49SYabin Cui #define ZSTD_FOR_EACH_DICT_MODE(X, ...) \
1412*01826a49SYabin Cui X(__VA_ARGS__, noDict) \
1413*01826a49SYabin Cui X(__VA_ARGS__, extDict) \
1414*01826a49SYabin Cui X(__VA_ARGS__, dictMatchState) \
1415*01826a49SYabin Cui X(__VA_ARGS__, dedicatedDictSearch)
1416*01826a49SYabin Cui
1417*01826a49SYabin Cui /* Generate row search fns for each combination of (dictMode, mls, rowLog) */
1418*01826a49SYabin Cui ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS_ROWLOG, GEN_ZSTD_ROW_SEARCH_FN)
1419*01826a49SYabin Cui /* Generate binary Tree search fns for each combination of (dictMode, mls) */
1420*01826a49SYabin Cui ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_BT_SEARCH_FN)
1421*01826a49SYabin Cui /* Generate hash chain search fns for each combination of (dictMode, mls) */
1422*01826a49SYabin Cui ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_HC_SEARCH_FN)
1423*01826a49SYabin Cui
1424*01826a49SYabin Cui typedef enum { search_hashChain=0, search_binaryTree=1, search_rowHash=2 } searchMethod_e;
1425*01826a49SYabin Cui
1426*01826a49SYabin Cui #define GEN_ZSTD_CALL_BT_SEARCH_FN(dictMode, mls) \
1427*01826a49SYabin Cui case mls: \
1428*01826a49SYabin Cui return ZSTD_BT_SEARCH_FN(dictMode, mls)(ms, ip, iend, offsetPtr);
1429*01826a49SYabin Cui #define GEN_ZSTD_CALL_HC_SEARCH_FN(dictMode, mls) \
1430*01826a49SYabin Cui case mls: \
1431*01826a49SYabin Cui return ZSTD_HC_SEARCH_FN(dictMode, mls)(ms, ip, iend, offsetPtr);
1432*01826a49SYabin Cui #define GEN_ZSTD_CALL_ROW_SEARCH_FN(dictMode, mls, rowLog) \
1433*01826a49SYabin Cui case rowLog: \
1434*01826a49SYabin Cui return ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog)(ms, ip, iend, offsetPtr);
1435*01826a49SYabin Cui
1436*01826a49SYabin Cui #define ZSTD_SWITCH_MLS(X, dictMode) \
1437*01826a49SYabin Cui switch (mls) { \
1438*01826a49SYabin Cui ZSTD_FOR_EACH_MLS(X, dictMode) \
1439*01826a49SYabin Cui }
1440*01826a49SYabin Cui
1441*01826a49SYabin Cui #define ZSTD_SWITCH_ROWLOG(dictMode, mls) \
1442*01826a49SYabin Cui case mls: \
1443*01826a49SYabin Cui switch (rowLog) { \
1444*01826a49SYabin Cui ZSTD_FOR_EACH_ROWLOG(GEN_ZSTD_CALL_ROW_SEARCH_FN, dictMode, mls) \
1445*01826a49SYabin Cui } \
1446*01826a49SYabin Cui ZSTD_UNREACHABLE; \
1447*01826a49SYabin Cui break;
1448*01826a49SYabin Cui
1449*01826a49SYabin Cui #define ZSTD_SWITCH_SEARCH_METHOD(dictMode) \
1450*01826a49SYabin Cui switch (searchMethod) { \
1451*01826a49SYabin Cui case search_hashChain: \
1452*01826a49SYabin Cui ZSTD_SWITCH_MLS(GEN_ZSTD_CALL_HC_SEARCH_FN, dictMode) \
1453*01826a49SYabin Cui break; \
1454*01826a49SYabin Cui case search_binaryTree: \
1455*01826a49SYabin Cui ZSTD_SWITCH_MLS(GEN_ZSTD_CALL_BT_SEARCH_FN, dictMode) \
1456*01826a49SYabin Cui break; \
1457*01826a49SYabin Cui case search_rowHash: \
1458*01826a49SYabin Cui ZSTD_SWITCH_MLS(ZSTD_SWITCH_ROWLOG, dictMode) \
1459*01826a49SYabin Cui break; \
1460*01826a49SYabin Cui } \
1461*01826a49SYabin Cui ZSTD_UNREACHABLE;
1462*01826a49SYabin Cui
1463*01826a49SYabin Cui /**
1464*01826a49SYabin Cui * Searches for the longest match at @p ip.
1465*01826a49SYabin Cui * Dispatches to the correct implementation function based on the
1466*01826a49SYabin Cui * (searchMethod, dictMode, mls, rowLog). We use switch statements
1467*01826a49SYabin Cui * here instead of using an indirect function call through a function
1468*01826a49SYabin Cui * pointer because after Spectre and Meltdown mitigations, indirect
1469*01826a49SYabin Cui * function calls can be very costly, especially in the kernel.
1470*01826a49SYabin Cui *
1471*01826a49SYabin Cui * NOTE: dictMode and searchMethod should be templated, so those switch
1472*01826a49SYabin Cui * statements should be optimized out. Only the mls & rowLog switches
1473*01826a49SYabin Cui * should be left.
1474*01826a49SYabin Cui *
1475*01826a49SYabin Cui * @param ms The match state.
1476*01826a49SYabin Cui * @param ip The position to search at.
1477*01826a49SYabin Cui * @param iend The end of the input data.
1478*01826a49SYabin Cui * @param[out] offsetPtr Stores the match offset into this pointer.
1479*01826a49SYabin Cui * @param mls The minimum search length, in the range [4, 6].
1480*01826a49SYabin Cui * @param rowLog The row log (if applicable), in the range [4, 6].
1481*01826a49SYabin Cui * @param searchMethod The search method to use (templated).
1482*01826a49SYabin Cui * @param dictMode The dictMode (templated).
1483*01826a49SYabin Cui *
1484*01826a49SYabin Cui * @returns The length of the longest match found, or < mls if no match is found.
1485*01826a49SYabin Cui * If a match is found its offset is stored in @p offsetPtr.
1486*01826a49SYabin Cui */
ZSTD_searchMax(ZSTD_matchState_t * ms,const BYTE * ip,const BYTE * iend,size_t * offsetPtr,U32 const mls,U32 const rowLog,searchMethod_e const searchMethod,ZSTD_dictMode_e const dictMode)1487*01826a49SYabin Cui FORCE_INLINE_TEMPLATE size_t ZSTD_searchMax(
1488*01826a49SYabin Cui ZSTD_matchState_t* ms,
1489*01826a49SYabin Cui const BYTE* ip,
1490*01826a49SYabin Cui const BYTE* iend,
1491*01826a49SYabin Cui size_t* offsetPtr,
1492*01826a49SYabin Cui U32 const mls,
1493*01826a49SYabin Cui U32 const rowLog,
1494*01826a49SYabin Cui searchMethod_e const searchMethod,
1495*01826a49SYabin Cui ZSTD_dictMode_e const dictMode)
1496*01826a49SYabin Cui {
1497*01826a49SYabin Cui if (dictMode == ZSTD_noDict) {
1498*01826a49SYabin Cui ZSTD_SWITCH_SEARCH_METHOD(noDict)
1499*01826a49SYabin Cui } else if (dictMode == ZSTD_extDict) {
1500*01826a49SYabin Cui ZSTD_SWITCH_SEARCH_METHOD(extDict)
1501*01826a49SYabin Cui } else if (dictMode == ZSTD_dictMatchState) {
1502*01826a49SYabin Cui ZSTD_SWITCH_SEARCH_METHOD(dictMatchState)
1503*01826a49SYabin Cui } else if (dictMode == ZSTD_dedicatedDictSearch) {
1504*01826a49SYabin Cui ZSTD_SWITCH_SEARCH_METHOD(dedicatedDictSearch)
1505*01826a49SYabin Cui }
1506*01826a49SYabin Cui ZSTD_UNREACHABLE;
1507*01826a49SYabin Cui return 0;
1508*01826a49SYabin Cui }
1509*01826a49SYabin Cui
1510*01826a49SYabin Cui /* *******************************
1511*01826a49SYabin Cui * Common parser - lazy strategy
1512*01826a49SYabin Cui *********************************/
1513*01826a49SYabin Cui
1514*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
1515*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_compressBlock_lazy_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],const void * src,size_t srcSize,const searchMethod_e searchMethod,const U32 depth,ZSTD_dictMode_e const dictMode)1516*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_generic(
1517*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore,
1518*01826a49SYabin Cui U32 rep[ZSTD_REP_NUM],
1519*01826a49SYabin Cui const void* src, size_t srcSize,
1520*01826a49SYabin Cui const searchMethod_e searchMethod, const U32 depth,
1521*01826a49SYabin Cui ZSTD_dictMode_e const dictMode)
1522*01826a49SYabin Cui {
1523*01826a49SYabin Cui const BYTE* const istart = (const BYTE*)src;
1524*01826a49SYabin Cui const BYTE* ip = istart;
1525*01826a49SYabin Cui const BYTE* anchor = istart;
1526*01826a49SYabin Cui const BYTE* const iend = istart + srcSize;
1527*01826a49SYabin Cui const BYTE* const ilimit = (searchMethod == search_rowHash) ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8;
1528*01826a49SYabin Cui const BYTE* const base = ms->window.base;
1529*01826a49SYabin Cui const U32 prefixLowestIndex = ms->window.dictLimit;
1530*01826a49SYabin Cui const BYTE* const prefixLowest = base + prefixLowestIndex;
1531*01826a49SYabin Cui const U32 mls = BOUNDED(4, ms->cParams.minMatch, 6);
1532*01826a49SYabin Cui const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6);
1533*01826a49SYabin Cui
1534*01826a49SYabin Cui U32 offset_1 = rep[0], offset_2 = rep[1];
1535*01826a49SYabin Cui U32 offsetSaved1 = 0, offsetSaved2 = 0;
1536*01826a49SYabin Cui
1537*01826a49SYabin Cui const int isDMS = dictMode == ZSTD_dictMatchState;
1538*01826a49SYabin Cui const int isDDS = dictMode == ZSTD_dedicatedDictSearch;
1539*01826a49SYabin Cui const int isDxS = isDMS || isDDS;
1540*01826a49SYabin Cui const ZSTD_matchState_t* const dms = ms->dictMatchState;
1541*01826a49SYabin Cui const U32 dictLowestIndex = isDxS ? dms->window.dictLimit : 0;
1542*01826a49SYabin Cui const BYTE* const dictBase = isDxS ? dms->window.base : NULL;
1543*01826a49SYabin Cui const BYTE* const dictLowest = isDxS ? dictBase + dictLowestIndex : NULL;
1544*01826a49SYabin Cui const BYTE* const dictEnd = isDxS ? dms->window.nextSrc : NULL;
1545*01826a49SYabin Cui const U32 dictIndexDelta = isDxS ?
1546*01826a49SYabin Cui prefixLowestIndex - (U32)(dictEnd - dictBase) :
1547*01826a49SYabin Cui 0;
1548*01826a49SYabin Cui const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictLowest));
1549*01826a49SYabin Cui
1550*01826a49SYabin Cui DEBUGLOG(5, "ZSTD_compressBlock_lazy_generic (dictMode=%u) (searchFunc=%u)", (U32)dictMode, (U32)searchMethod);
1551*01826a49SYabin Cui ip += (dictAndPrefixLength == 0);
1552*01826a49SYabin Cui if (dictMode == ZSTD_noDict) {
1553*01826a49SYabin Cui U32 const curr = (U32)(ip - base);
1554*01826a49SYabin Cui U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, ms->cParams.windowLog);
1555*01826a49SYabin Cui U32 const maxRep = curr - windowLow;
1556*01826a49SYabin Cui if (offset_2 > maxRep) offsetSaved2 = offset_2, offset_2 = 0;
1557*01826a49SYabin Cui if (offset_1 > maxRep) offsetSaved1 = offset_1, offset_1 = 0;
1558*01826a49SYabin Cui }
1559*01826a49SYabin Cui if (isDxS) {
1560*01826a49SYabin Cui /* dictMatchState repCode checks don't currently handle repCode == 0
1561*01826a49SYabin Cui * disabling. */
1562*01826a49SYabin Cui assert(offset_1 <= dictAndPrefixLength);
1563*01826a49SYabin Cui assert(offset_2 <= dictAndPrefixLength);
1564*01826a49SYabin Cui }
1565*01826a49SYabin Cui
1566*01826a49SYabin Cui /* Reset the lazy skipping state */
1567*01826a49SYabin Cui ms->lazySkipping = 0;
1568*01826a49SYabin Cui
1569*01826a49SYabin Cui if (searchMethod == search_rowHash) {
1570*01826a49SYabin Cui ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit);
1571*01826a49SYabin Cui }
1572*01826a49SYabin Cui
1573*01826a49SYabin Cui /* Match Loop */
1574*01826a49SYabin Cui #if defined(__GNUC__) && defined(__x86_64__)
1575*01826a49SYabin Cui /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the
1576*01826a49SYabin Cui * code alignment is perturbed. To fix the instability align the loop on 32-bytes.
1577*01826a49SYabin Cui */
1578*01826a49SYabin Cui __asm__(".p2align 5");
1579*01826a49SYabin Cui #endif
1580*01826a49SYabin Cui while (ip < ilimit) {
1581*01826a49SYabin Cui size_t matchLength=0;
1582*01826a49SYabin Cui size_t offBase = REPCODE1_TO_OFFBASE;
1583*01826a49SYabin Cui const BYTE* start=ip+1;
1584*01826a49SYabin Cui DEBUGLOG(7, "search baseline (depth 0)");
1585*01826a49SYabin Cui
1586*01826a49SYabin Cui /* check repCode */
1587*01826a49SYabin Cui if (isDxS) {
1588*01826a49SYabin Cui const U32 repIndex = (U32)(ip - base) + 1 - offset_1;
1589*01826a49SYabin Cui const BYTE* repMatch = ((dictMode == ZSTD_dictMatchState || dictMode == ZSTD_dedicatedDictSearch)
1590*01826a49SYabin Cui && repIndex < prefixLowestIndex) ?
1591*01826a49SYabin Cui dictBase + (repIndex - dictIndexDelta) :
1592*01826a49SYabin Cui base + repIndex;
1593*01826a49SYabin Cui if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
1594*01826a49SYabin Cui && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
1595*01826a49SYabin Cui const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
1596*01826a49SYabin Cui matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
1597*01826a49SYabin Cui if (depth==0) goto _storeSequence;
1598*01826a49SYabin Cui }
1599*01826a49SYabin Cui }
1600*01826a49SYabin Cui if ( dictMode == ZSTD_noDict
1601*01826a49SYabin Cui && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
1602*01826a49SYabin Cui matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
1603*01826a49SYabin Cui if (depth==0) goto _storeSequence;
1604*01826a49SYabin Cui }
1605*01826a49SYabin Cui
1606*01826a49SYabin Cui /* first search (depth 0) */
1607*01826a49SYabin Cui { size_t offbaseFound = 999999999;
1608*01826a49SYabin Cui size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &offbaseFound, mls, rowLog, searchMethod, dictMode);
1609*01826a49SYabin Cui if (ml2 > matchLength)
1610*01826a49SYabin Cui matchLength = ml2, start = ip, offBase = offbaseFound;
1611*01826a49SYabin Cui }
1612*01826a49SYabin Cui
1613*01826a49SYabin Cui if (matchLength < 4) {
1614*01826a49SYabin Cui size_t const step = ((size_t)(ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */;
1615*01826a49SYabin Cui ip += step;
1616*01826a49SYabin Cui /* Enter the lazy skipping mode once we are skipping more than 8 bytes at a time.
1617*01826a49SYabin Cui * In this mode we stop inserting every position into our tables, and only insert
1618*01826a49SYabin Cui * positions that we search, which is one in step positions.
1619*01826a49SYabin Cui * The exact cutoff is flexible, I've just chosen a number that is reasonably high,
1620*01826a49SYabin Cui * so we minimize the compression ratio loss in "normal" scenarios. This mode gets
1621*01826a49SYabin Cui * triggered once we've gone 2KB without finding any matches.
1622*01826a49SYabin Cui */
1623*01826a49SYabin Cui ms->lazySkipping = step > kLazySkippingStep;
1624*01826a49SYabin Cui continue;
1625*01826a49SYabin Cui }
1626*01826a49SYabin Cui
1627*01826a49SYabin Cui /* let's try to find a better solution */
1628*01826a49SYabin Cui if (depth>=1)
1629*01826a49SYabin Cui while (ip<ilimit) {
1630*01826a49SYabin Cui DEBUGLOG(7, "search depth 1");
1631*01826a49SYabin Cui ip ++;
1632*01826a49SYabin Cui if ( (dictMode == ZSTD_noDict)
1633*01826a49SYabin Cui && (offBase) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
1634*01826a49SYabin Cui size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
1635*01826a49SYabin Cui int const gain2 = (int)(mlRep * 3);
1636*01826a49SYabin Cui int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offBase) + 1);
1637*01826a49SYabin Cui if ((mlRep >= 4) && (gain2 > gain1))
1638*01826a49SYabin Cui matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip;
1639*01826a49SYabin Cui }
1640*01826a49SYabin Cui if (isDxS) {
1641*01826a49SYabin Cui const U32 repIndex = (U32)(ip - base) - offset_1;
1642*01826a49SYabin Cui const BYTE* repMatch = repIndex < prefixLowestIndex ?
1643*01826a49SYabin Cui dictBase + (repIndex - dictIndexDelta) :
1644*01826a49SYabin Cui base + repIndex;
1645*01826a49SYabin Cui if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
1646*01826a49SYabin Cui && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
1647*01826a49SYabin Cui const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
1648*01826a49SYabin Cui size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
1649*01826a49SYabin Cui int const gain2 = (int)(mlRep * 3);
1650*01826a49SYabin Cui int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offBase) + 1);
1651*01826a49SYabin Cui if ((mlRep >= 4) && (gain2 > gain1))
1652*01826a49SYabin Cui matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip;
1653*01826a49SYabin Cui }
1654*01826a49SYabin Cui }
1655*01826a49SYabin Cui { size_t ofbCandidate=999999999;
1656*01826a49SYabin Cui size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode);
1657*01826a49SYabin Cui int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */
1658*01826a49SYabin Cui int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 4);
1659*01826a49SYabin Cui if ((ml2 >= 4) && (gain2 > gain1)) {
1660*01826a49SYabin Cui matchLength = ml2, offBase = ofbCandidate, start = ip;
1661*01826a49SYabin Cui continue; /* search a better one */
1662*01826a49SYabin Cui } }
1663*01826a49SYabin Cui
1664*01826a49SYabin Cui /* let's find an even better one */
1665*01826a49SYabin Cui if ((depth==2) && (ip<ilimit)) {
1666*01826a49SYabin Cui DEBUGLOG(7, "search depth 2");
1667*01826a49SYabin Cui ip ++;
1668*01826a49SYabin Cui if ( (dictMode == ZSTD_noDict)
1669*01826a49SYabin Cui && (offBase) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
1670*01826a49SYabin Cui size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
1671*01826a49SYabin Cui int const gain2 = (int)(mlRep * 4);
1672*01826a49SYabin Cui int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 1);
1673*01826a49SYabin Cui if ((mlRep >= 4) && (gain2 > gain1))
1674*01826a49SYabin Cui matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip;
1675*01826a49SYabin Cui }
1676*01826a49SYabin Cui if (isDxS) {
1677*01826a49SYabin Cui const U32 repIndex = (U32)(ip - base) - offset_1;
1678*01826a49SYabin Cui const BYTE* repMatch = repIndex < prefixLowestIndex ?
1679*01826a49SYabin Cui dictBase + (repIndex - dictIndexDelta) :
1680*01826a49SYabin Cui base + repIndex;
1681*01826a49SYabin Cui if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
1682*01826a49SYabin Cui && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
1683*01826a49SYabin Cui const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
1684*01826a49SYabin Cui size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
1685*01826a49SYabin Cui int const gain2 = (int)(mlRep * 4);
1686*01826a49SYabin Cui int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 1);
1687*01826a49SYabin Cui if ((mlRep >= 4) && (gain2 > gain1))
1688*01826a49SYabin Cui matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip;
1689*01826a49SYabin Cui }
1690*01826a49SYabin Cui }
1691*01826a49SYabin Cui { size_t ofbCandidate=999999999;
1692*01826a49SYabin Cui size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode);
1693*01826a49SYabin Cui int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */
1694*01826a49SYabin Cui int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 7);
1695*01826a49SYabin Cui if ((ml2 >= 4) && (gain2 > gain1)) {
1696*01826a49SYabin Cui matchLength = ml2, offBase = ofbCandidate, start = ip;
1697*01826a49SYabin Cui continue;
1698*01826a49SYabin Cui } } }
1699*01826a49SYabin Cui break; /* nothing found : store previous solution */
1700*01826a49SYabin Cui }
1701*01826a49SYabin Cui
1702*01826a49SYabin Cui /* NOTE:
1703*01826a49SYabin Cui * Pay attention that `start[-value]` can lead to strange undefined behavior
1704*01826a49SYabin Cui * notably if `value` is unsigned, resulting in a large positive `-value`.
1705*01826a49SYabin Cui */
1706*01826a49SYabin Cui /* catch up */
1707*01826a49SYabin Cui if (OFFBASE_IS_OFFSET(offBase)) {
1708*01826a49SYabin Cui if (dictMode == ZSTD_noDict) {
1709*01826a49SYabin Cui while ( ((start > anchor) & (start - OFFBASE_TO_OFFSET(offBase) > prefixLowest))
1710*01826a49SYabin Cui && (start[-1] == (start-OFFBASE_TO_OFFSET(offBase))[-1]) ) /* only search for offset within prefix */
1711*01826a49SYabin Cui { start--; matchLength++; }
1712*01826a49SYabin Cui }
1713*01826a49SYabin Cui if (isDxS) {
1714*01826a49SYabin Cui U32 const matchIndex = (U32)((size_t)(start-base) - OFFBASE_TO_OFFSET(offBase));
1715*01826a49SYabin Cui const BYTE* match = (matchIndex < prefixLowestIndex) ? dictBase + matchIndex - dictIndexDelta : base + matchIndex;
1716*01826a49SYabin Cui const BYTE* const mStart = (matchIndex < prefixLowestIndex) ? dictLowest : prefixLowest;
1717*01826a49SYabin Cui while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
1718*01826a49SYabin Cui }
1719*01826a49SYabin Cui offset_2 = offset_1; offset_1 = (U32)OFFBASE_TO_OFFSET(offBase);
1720*01826a49SYabin Cui }
1721*01826a49SYabin Cui /* store sequence */
1722*01826a49SYabin Cui _storeSequence:
1723*01826a49SYabin Cui { size_t const litLength = (size_t)(start - anchor);
1724*01826a49SYabin Cui ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offBase, matchLength);
1725*01826a49SYabin Cui anchor = ip = start + matchLength;
1726*01826a49SYabin Cui }
1727*01826a49SYabin Cui if (ms->lazySkipping) {
1728*01826a49SYabin Cui /* We've found a match, disable lazy skipping mode, and refill the hash cache. */
1729*01826a49SYabin Cui if (searchMethod == search_rowHash) {
1730*01826a49SYabin Cui ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit);
1731*01826a49SYabin Cui }
1732*01826a49SYabin Cui ms->lazySkipping = 0;
1733*01826a49SYabin Cui }
1734*01826a49SYabin Cui
1735*01826a49SYabin Cui /* check immediate repcode */
1736*01826a49SYabin Cui if (isDxS) {
1737*01826a49SYabin Cui while (ip <= ilimit) {
1738*01826a49SYabin Cui U32 const current2 = (U32)(ip-base);
1739*01826a49SYabin Cui U32 const repIndex = current2 - offset_2;
1740*01826a49SYabin Cui const BYTE* repMatch = repIndex < prefixLowestIndex ?
1741*01826a49SYabin Cui dictBase - dictIndexDelta + repIndex :
1742*01826a49SYabin Cui base + repIndex;
1743*01826a49SYabin Cui if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex) >= 3 /* intentional overflow */)
1744*01826a49SYabin Cui && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
1745*01826a49SYabin Cui const BYTE* const repEnd2 = repIndex < prefixLowestIndex ? dictEnd : iend;
1746*01826a49SYabin Cui matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd2, prefixLowest) + 4;
1747*01826a49SYabin Cui offBase = offset_2; offset_2 = offset_1; offset_1 = (U32)offBase; /* swap offset_2 <=> offset_1 */
1748*01826a49SYabin Cui ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, matchLength);
1749*01826a49SYabin Cui ip += matchLength;
1750*01826a49SYabin Cui anchor = ip;
1751*01826a49SYabin Cui continue;
1752*01826a49SYabin Cui }
1753*01826a49SYabin Cui break;
1754*01826a49SYabin Cui }
1755*01826a49SYabin Cui }
1756*01826a49SYabin Cui
1757*01826a49SYabin Cui if (dictMode == ZSTD_noDict) {
1758*01826a49SYabin Cui while ( ((ip <= ilimit) & (offset_2>0))
1759*01826a49SYabin Cui && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) {
1760*01826a49SYabin Cui /* store sequence */
1761*01826a49SYabin Cui matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
1762*01826a49SYabin Cui offBase = offset_2; offset_2 = offset_1; offset_1 = (U32)offBase; /* swap repcodes */
1763*01826a49SYabin Cui ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, matchLength);
1764*01826a49SYabin Cui ip += matchLength;
1765*01826a49SYabin Cui anchor = ip;
1766*01826a49SYabin Cui continue; /* faster when present ... (?) */
1767*01826a49SYabin Cui } } }
1768*01826a49SYabin Cui
1769*01826a49SYabin Cui /* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0),
1770*01826a49SYabin Cui * rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */
1771*01826a49SYabin Cui offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2;
1772*01826a49SYabin Cui
1773*01826a49SYabin Cui /* save reps for next block */
1774*01826a49SYabin Cui rep[0] = offset_1 ? offset_1 : offsetSaved1;
1775*01826a49SYabin Cui rep[1] = offset_2 ? offset_2 : offsetSaved2;
1776*01826a49SYabin Cui
1777*01826a49SYabin Cui /* Return the last literals size */
1778*01826a49SYabin Cui return (size_t)(iend - anchor);
1779*01826a49SYabin Cui }
1780*01826a49SYabin Cui #endif /* build exclusions */
1781*01826a49SYabin Cui
1782*01826a49SYabin Cui
1783*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR
ZSTD_compressBlock_greedy(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1784*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy(
1785*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1786*01826a49SYabin Cui void const* src, size_t srcSize)
1787*01826a49SYabin Cui {
1788*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_noDict);
1789*01826a49SYabin Cui }
1790*01826a49SYabin Cui
ZSTD_compressBlock_greedy_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1791*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy_dictMatchState(
1792*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1793*01826a49SYabin Cui void const* src, size_t srcSize)
1794*01826a49SYabin Cui {
1795*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dictMatchState);
1796*01826a49SYabin Cui }
1797*01826a49SYabin Cui
ZSTD_compressBlock_greedy_dedicatedDictSearch(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1798*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy_dedicatedDictSearch(
1799*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1800*01826a49SYabin Cui void const* src, size_t srcSize)
1801*01826a49SYabin Cui {
1802*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dedicatedDictSearch);
1803*01826a49SYabin Cui }
1804*01826a49SYabin Cui
ZSTD_compressBlock_greedy_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1805*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy_row(
1806*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1807*01826a49SYabin Cui void const* src, size_t srcSize)
1808*01826a49SYabin Cui {
1809*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_noDict);
1810*01826a49SYabin Cui }
1811*01826a49SYabin Cui
ZSTD_compressBlock_greedy_dictMatchState_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1812*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy_dictMatchState_row(
1813*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1814*01826a49SYabin Cui void const* src, size_t srcSize)
1815*01826a49SYabin Cui {
1816*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dictMatchState);
1817*01826a49SYabin Cui }
1818*01826a49SYabin Cui
ZSTD_compressBlock_greedy_dedicatedDictSearch_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1819*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy_dedicatedDictSearch_row(
1820*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1821*01826a49SYabin Cui void const* src, size_t srcSize)
1822*01826a49SYabin Cui {
1823*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dedicatedDictSearch);
1824*01826a49SYabin Cui }
1825*01826a49SYabin Cui #endif
1826*01826a49SYabin Cui
1827*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR
ZSTD_compressBlock_lazy(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1828*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy(
1829*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1830*01826a49SYabin Cui void const* src, size_t srcSize)
1831*01826a49SYabin Cui {
1832*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_noDict);
1833*01826a49SYabin Cui }
1834*01826a49SYabin Cui
ZSTD_compressBlock_lazy_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1835*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_dictMatchState(
1836*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1837*01826a49SYabin Cui void const* src, size_t srcSize)
1838*01826a49SYabin Cui {
1839*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dictMatchState);
1840*01826a49SYabin Cui }
1841*01826a49SYabin Cui
ZSTD_compressBlock_lazy_dedicatedDictSearch(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1842*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_dedicatedDictSearch(
1843*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1844*01826a49SYabin Cui void const* src, size_t srcSize)
1845*01826a49SYabin Cui {
1846*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dedicatedDictSearch);
1847*01826a49SYabin Cui }
1848*01826a49SYabin Cui
ZSTD_compressBlock_lazy_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1849*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_row(
1850*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1851*01826a49SYabin Cui void const* src, size_t srcSize)
1852*01826a49SYabin Cui {
1853*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_noDict);
1854*01826a49SYabin Cui }
1855*01826a49SYabin Cui
ZSTD_compressBlock_lazy_dictMatchState_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1856*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_dictMatchState_row(
1857*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1858*01826a49SYabin Cui void const* src, size_t srcSize)
1859*01826a49SYabin Cui {
1860*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dictMatchState);
1861*01826a49SYabin Cui }
1862*01826a49SYabin Cui
ZSTD_compressBlock_lazy_dedicatedDictSearch_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1863*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_dedicatedDictSearch_row(
1864*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1865*01826a49SYabin Cui void const* src, size_t srcSize)
1866*01826a49SYabin Cui {
1867*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dedicatedDictSearch);
1868*01826a49SYabin Cui }
1869*01826a49SYabin Cui #endif
1870*01826a49SYabin Cui
1871*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR
ZSTD_compressBlock_lazy2(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1872*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2(
1873*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1874*01826a49SYabin Cui void const* src, size_t srcSize)
1875*01826a49SYabin Cui {
1876*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_noDict);
1877*01826a49SYabin Cui }
1878*01826a49SYabin Cui
ZSTD_compressBlock_lazy2_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1879*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2_dictMatchState(
1880*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1881*01826a49SYabin Cui void const* src, size_t srcSize)
1882*01826a49SYabin Cui {
1883*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dictMatchState);
1884*01826a49SYabin Cui }
1885*01826a49SYabin Cui
ZSTD_compressBlock_lazy2_dedicatedDictSearch(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1886*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch(
1887*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1888*01826a49SYabin Cui void const* src, size_t srcSize)
1889*01826a49SYabin Cui {
1890*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dedicatedDictSearch);
1891*01826a49SYabin Cui }
1892*01826a49SYabin Cui
ZSTD_compressBlock_lazy2_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1893*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2_row(
1894*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1895*01826a49SYabin Cui void const* src, size_t srcSize)
1896*01826a49SYabin Cui {
1897*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_noDict);
1898*01826a49SYabin Cui }
1899*01826a49SYabin Cui
ZSTD_compressBlock_lazy2_dictMatchState_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1900*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2_dictMatchState_row(
1901*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1902*01826a49SYabin Cui void const* src, size_t srcSize)
1903*01826a49SYabin Cui {
1904*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dictMatchState);
1905*01826a49SYabin Cui }
1906*01826a49SYabin Cui
ZSTD_compressBlock_lazy2_dedicatedDictSearch_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1907*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch_row(
1908*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1909*01826a49SYabin Cui void const* src, size_t srcSize)
1910*01826a49SYabin Cui {
1911*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dedicatedDictSearch);
1912*01826a49SYabin Cui }
1913*01826a49SYabin Cui #endif
1914*01826a49SYabin Cui
1915*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR
ZSTD_compressBlock_btlazy2(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1916*01826a49SYabin Cui size_t ZSTD_compressBlock_btlazy2(
1917*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1918*01826a49SYabin Cui void const* src, size_t srcSize)
1919*01826a49SYabin Cui {
1920*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_noDict);
1921*01826a49SYabin Cui }
1922*01826a49SYabin Cui
ZSTD_compressBlock_btlazy2_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1923*01826a49SYabin Cui size_t ZSTD_compressBlock_btlazy2_dictMatchState(
1924*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1925*01826a49SYabin Cui void const* src, size_t srcSize)
1926*01826a49SYabin Cui {
1927*01826a49SYabin Cui return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_dictMatchState);
1928*01826a49SYabin Cui }
1929*01826a49SYabin Cui #endif
1930*01826a49SYabin Cui
1931*01826a49SYabin Cui #if !defined(ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR) \
1932*01826a49SYabin Cui || !defined(ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR) \
1933*01826a49SYabin Cui || !defined(ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR) \
1934*01826a49SYabin Cui || !defined(ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR)
1935*01826a49SYabin Cui FORCE_INLINE_TEMPLATE
1936*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_compressBlock_lazy_extDict_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],const void * src,size_t srcSize,const searchMethod_e searchMethod,const U32 depth)1937*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_extDict_generic(
1938*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore,
1939*01826a49SYabin Cui U32 rep[ZSTD_REP_NUM],
1940*01826a49SYabin Cui const void* src, size_t srcSize,
1941*01826a49SYabin Cui const searchMethod_e searchMethod, const U32 depth)
1942*01826a49SYabin Cui {
1943*01826a49SYabin Cui const BYTE* const istart = (const BYTE*)src;
1944*01826a49SYabin Cui const BYTE* ip = istart;
1945*01826a49SYabin Cui const BYTE* anchor = istart;
1946*01826a49SYabin Cui const BYTE* const iend = istart + srcSize;
1947*01826a49SYabin Cui const BYTE* const ilimit = searchMethod == search_rowHash ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8;
1948*01826a49SYabin Cui const BYTE* const base = ms->window.base;
1949*01826a49SYabin Cui const U32 dictLimit = ms->window.dictLimit;
1950*01826a49SYabin Cui const BYTE* const prefixStart = base + dictLimit;
1951*01826a49SYabin Cui const BYTE* const dictBase = ms->window.dictBase;
1952*01826a49SYabin Cui const BYTE* const dictEnd = dictBase + dictLimit;
1953*01826a49SYabin Cui const BYTE* const dictStart = dictBase + ms->window.lowLimit;
1954*01826a49SYabin Cui const U32 windowLog = ms->cParams.windowLog;
1955*01826a49SYabin Cui const U32 mls = BOUNDED(4, ms->cParams.minMatch, 6);
1956*01826a49SYabin Cui const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6);
1957*01826a49SYabin Cui
1958*01826a49SYabin Cui U32 offset_1 = rep[0], offset_2 = rep[1];
1959*01826a49SYabin Cui
1960*01826a49SYabin Cui DEBUGLOG(5, "ZSTD_compressBlock_lazy_extDict_generic (searchFunc=%u)", (U32)searchMethod);
1961*01826a49SYabin Cui
1962*01826a49SYabin Cui /* Reset the lazy skipping state */
1963*01826a49SYabin Cui ms->lazySkipping = 0;
1964*01826a49SYabin Cui
1965*01826a49SYabin Cui /* init */
1966*01826a49SYabin Cui ip += (ip == prefixStart);
1967*01826a49SYabin Cui if (searchMethod == search_rowHash) {
1968*01826a49SYabin Cui ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit);
1969*01826a49SYabin Cui }
1970*01826a49SYabin Cui
1971*01826a49SYabin Cui /* Match Loop */
1972*01826a49SYabin Cui #if defined(__GNUC__) && defined(__x86_64__)
1973*01826a49SYabin Cui /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the
1974*01826a49SYabin Cui * code alignment is perturbed. To fix the instability align the loop on 32-bytes.
1975*01826a49SYabin Cui */
1976*01826a49SYabin Cui __asm__(".p2align 5");
1977*01826a49SYabin Cui #endif
1978*01826a49SYabin Cui while (ip < ilimit) {
1979*01826a49SYabin Cui size_t matchLength=0;
1980*01826a49SYabin Cui size_t offBase = REPCODE1_TO_OFFBASE;
1981*01826a49SYabin Cui const BYTE* start=ip+1;
1982*01826a49SYabin Cui U32 curr = (U32)(ip-base);
1983*01826a49SYabin Cui
1984*01826a49SYabin Cui /* check repCode */
1985*01826a49SYabin Cui { const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr+1, windowLog);
1986*01826a49SYabin Cui const U32 repIndex = (U32)(curr+1 - offset_1);
1987*01826a49SYabin Cui const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
1988*01826a49SYabin Cui const BYTE* const repMatch = repBase + repIndex;
1989*01826a49SYabin Cui if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */
1990*01826a49SYabin Cui & (offset_1 <= curr+1 - windowLow) ) /* note: we are searching at curr+1 */
1991*01826a49SYabin Cui if (MEM_read32(ip+1) == MEM_read32(repMatch)) {
1992*01826a49SYabin Cui /* repcode detected we should take it */
1993*01826a49SYabin Cui const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
1994*01826a49SYabin Cui matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4;
1995*01826a49SYabin Cui if (depth==0) goto _storeSequence;
1996*01826a49SYabin Cui } }
1997*01826a49SYabin Cui
1998*01826a49SYabin Cui /* first search (depth 0) */
1999*01826a49SYabin Cui { size_t ofbCandidate = 999999999;
2000*01826a49SYabin Cui size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_extDict);
2001*01826a49SYabin Cui if (ml2 > matchLength)
2002*01826a49SYabin Cui matchLength = ml2, start = ip, offBase = ofbCandidate;
2003*01826a49SYabin Cui }
2004*01826a49SYabin Cui
2005*01826a49SYabin Cui if (matchLength < 4) {
2006*01826a49SYabin Cui size_t const step = ((size_t)(ip-anchor) >> kSearchStrength);
2007*01826a49SYabin Cui ip += step + 1; /* jump faster over incompressible sections */
2008*01826a49SYabin Cui /* Enter the lazy skipping mode once we are skipping more than 8 bytes at a time.
2009*01826a49SYabin Cui * In this mode we stop inserting every position into our tables, and only insert
2010*01826a49SYabin Cui * positions that we search, which is one in step positions.
2011*01826a49SYabin Cui * The exact cutoff is flexible, I've just chosen a number that is reasonably high,
2012*01826a49SYabin Cui * so we minimize the compression ratio loss in "normal" scenarios. This mode gets
2013*01826a49SYabin Cui * triggered once we've gone 2KB without finding any matches.
2014*01826a49SYabin Cui */
2015*01826a49SYabin Cui ms->lazySkipping = step > kLazySkippingStep;
2016*01826a49SYabin Cui continue;
2017*01826a49SYabin Cui }
2018*01826a49SYabin Cui
2019*01826a49SYabin Cui /* let's try to find a better solution */
2020*01826a49SYabin Cui if (depth>=1)
2021*01826a49SYabin Cui while (ip<ilimit) {
2022*01826a49SYabin Cui ip ++;
2023*01826a49SYabin Cui curr++;
2024*01826a49SYabin Cui /* check repCode */
2025*01826a49SYabin Cui if (offBase) {
2026*01826a49SYabin Cui const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog);
2027*01826a49SYabin Cui const U32 repIndex = (U32)(curr - offset_1);
2028*01826a49SYabin Cui const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
2029*01826a49SYabin Cui const BYTE* const repMatch = repBase + repIndex;
2030*01826a49SYabin Cui if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
2031*01826a49SYabin Cui & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
2032*01826a49SYabin Cui if (MEM_read32(ip) == MEM_read32(repMatch)) {
2033*01826a49SYabin Cui /* repcode detected */
2034*01826a49SYabin Cui const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
2035*01826a49SYabin Cui size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
2036*01826a49SYabin Cui int const gain2 = (int)(repLength * 3);
2037*01826a49SYabin Cui int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offBase) + 1);
2038*01826a49SYabin Cui if ((repLength >= 4) && (gain2 > gain1))
2039*01826a49SYabin Cui matchLength = repLength, offBase = REPCODE1_TO_OFFBASE, start = ip;
2040*01826a49SYabin Cui } }
2041*01826a49SYabin Cui
2042*01826a49SYabin Cui /* search match, depth 1 */
2043*01826a49SYabin Cui { size_t ofbCandidate = 999999999;
2044*01826a49SYabin Cui size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_extDict);
2045*01826a49SYabin Cui int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */
2046*01826a49SYabin Cui int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 4);
2047*01826a49SYabin Cui if ((ml2 >= 4) && (gain2 > gain1)) {
2048*01826a49SYabin Cui matchLength = ml2, offBase = ofbCandidate, start = ip;
2049*01826a49SYabin Cui continue; /* search a better one */
2050*01826a49SYabin Cui } }
2051*01826a49SYabin Cui
2052*01826a49SYabin Cui /* let's find an even better one */
2053*01826a49SYabin Cui if ((depth==2) && (ip<ilimit)) {
2054*01826a49SYabin Cui ip ++;
2055*01826a49SYabin Cui curr++;
2056*01826a49SYabin Cui /* check repCode */
2057*01826a49SYabin Cui if (offBase) {
2058*01826a49SYabin Cui const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog);
2059*01826a49SYabin Cui const U32 repIndex = (U32)(curr - offset_1);
2060*01826a49SYabin Cui const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
2061*01826a49SYabin Cui const BYTE* const repMatch = repBase + repIndex;
2062*01826a49SYabin Cui if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
2063*01826a49SYabin Cui & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
2064*01826a49SYabin Cui if (MEM_read32(ip) == MEM_read32(repMatch)) {
2065*01826a49SYabin Cui /* repcode detected */
2066*01826a49SYabin Cui const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
2067*01826a49SYabin Cui size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
2068*01826a49SYabin Cui int const gain2 = (int)(repLength * 4);
2069*01826a49SYabin Cui int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 1);
2070*01826a49SYabin Cui if ((repLength >= 4) && (gain2 > gain1))
2071*01826a49SYabin Cui matchLength = repLength, offBase = REPCODE1_TO_OFFBASE, start = ip;
2072*01826a49SYabin Cui } }
2073*01826a49SYabin Cui
2074*01826a49SYabin Cui /* search match, depth 2 */
2075*01826a49SYabin Cui { size_t ofbCandidate = 999999999;
2076*01826a49SYabin Cui size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_extDict);
2077*01826a49SYabin Cui int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */
2078*01826a49SYabin Cui int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 7);
2079*01826a49SYabin Cui if ((ml2 >= 4) && (gain2 > gain1)) {
2080*01826a49SYabin Cui matchLength = ml2, offBase = ofbCandidate, start = ip;
2081*01826a49SYabin Cui continue;
2082*01826a49SYabin Cui } } }
2083*01826a49SYabin Cui break; /* nothing found : store previous solution */
2084*01826a49SYabin Cui }
2085*01826a49SYabin Cui
2086*01826a49SYabin Cui /* catch up */
2087*01826a49SYabin Cui if (OFFBASE_IS_OFFSET(offBase)) {
2088*01826a49SYabin Cui U32 const matchIndex = (U32)((size_t)(start-base) - OFFBASE_TO_OFFSET(offBase));
2089*01826a49SYabin Cui const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex;
2090*01826a49SYabin Cui const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart;
2091*01826a49SYabin Cui while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
2092*01826a49SYabin Cui offset_2 = offset_1; offset_1 = (U32)OFFBASE_TO_OFFSET(offBase);
2093*01826a49SYabin Cui }
2094*01826a49SYabin Cui
2095*01826a49SYabin Cui /* store sequence */
2096*01826a49SYabin Cui _storeSequence:
2097*01826a49SYabin Cui { size_t const litLength = (size_t)(start - anchor);
2098*01826a49SYabin Cui ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offBase, matchLength);
2099*01826a49SYabin Cui anchor = ip = start + matchLength;
2100*01826a49SYabin Cui }
2101*01826a49SYabin Cui if (ms->lazySkipping) {
2102*01826a49SYabin Cui /* We've found a match, disable lazy skipping mode, and refill the hash cache. */
2103*01826a49SYabin Cui if (searchMethod == search_rowHash) {
2104*01826a49SYabin Cui ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit);
2105*01826a49SYabin Cui }
2106*01826a49SYabin Cui ms->lazySkipping = 0;
2107*01826a49SYabin Cui }
2108*01826a49SYabin Cui
2109*01826a49SYabin Cui /* check immediate repcode */
2110*01826a49SYabin Cui while (ip <= ilimit) {
2111*01826a49SYabin Cui const U32 repCurrent = (U32)(ip-base);
2112*01826a49SYabin Cui const U32 windowLow = ZSTD_getLowestMatchIndex(ms, repCurrent, windowLog);
2113*01826a49SYabin Cui const U32 repIndex = repCurrent - offset_2;
2114*01826a49SYabin Cui const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
2115*01826a49SYabin Cui const BYTE* const repMatch = repBase + repIndex;
2116*01826a49SYabin Cui if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
2117*01826a49SYabin Cui & (offset_2 <= repCurrent - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
2118*01826a49SYabin Cui if (MEM_read32(ip) == MEM_read32(repMatch)) {
2119*01826a49SYabin Cui /* repcode detected we should take it */
2120*01826a49SYabin Cui const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
2121*01826a49SYabin Cui matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
2122*01826a49SYabin Cui offBase = offset_2; offset_2 = offset_1; offset_1 = (U32)offBase; /* swap offset history */
2123*01826a49SYabin Cui ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, matchLength);
2124*01826a49SYabin Cui ip += matchLength;
2125*01826a49SYabin Cui anchor = ip;
2126*01826a49SYabin Cui continue; /* faster when present ... (?) */
2127*01826a49SYabin Cui }
2128*01826a49SYabin Cui break;
2129*01826a49SYabin Cui } }
2130*01826a49SYabin Cui
2131*01826a49SYabin Cui /* Save reps for next block */
2132*01826a49SYabin Cui rep[0] = offset_1;
2133*01826a49SYabin Cui rep[1] = offset_2;
2134*01826a49SYabin Cui
2135*01826a49SYabin Cui /* Return the last literals size */
2136*01826a49SYabin Cui return (size_t)(iend - anchor);
2137*01826a49SYabin Cui }
2138*01826a49SYabin Cui #endif /* build exclusions */
2139*01826a49SYabin Cui
2140*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR
ZSTD_compressBlock_greedy_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2141*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy_extDict(
2142*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2143*01826a49SYabin Cui void const* src, size_t srcSize)
2144*01826a49SYabin Cui {
2145*01826a49SYabin Cui return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0);
2146*01826a49SYabin Cui }
2147*01826a49SYabin Cui
ZSTD_compressBlock_greedy_extDict_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2148*01826a49SYabin Cui size_t ZSTD_compressBlock_greedy_extDict_row(
2149*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2150*01826a49SYabin Cui void const* src, size_t srcSize)
2151*01826a49SYabin Cui {
2152*01826a49SYabin Cui return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0);
2153*01826a49SYabin Cui }
2154*01826a49SYabin Cui #endif
2155*01826a49SYabin Cui
2156*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR
ZSTD_compressBlock_lazy_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2157*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_extDict(
2158*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2159*01826a49SYabin Cui void const* src, size_t srcSize)
2160*01826a49SYabin Cui
2161*01826a49SYabin Cui {
2162*01826a49SYabin Cui return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1);
2163*01826a49SYabin Cui }
2164*01826a49SYabin Cui
ZSTD_compressBlock_lazy_extDict_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2165*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy_extDict_row(
2166*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2167*01826a49SYabin Cui void const* src, size_t srcSize)
2168*01826a49SYabin Cui
2169*01826a49SYabin Cui {
2170*01826a49SYabin Cui return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1);
2171*01826a49SYabin Cui }
2172*01826a49SYabin Cui #endif
2173*01826a49SYabin Cui
2174*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR
ZSTD_compressBlock_lazy2_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2175*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2_extDict(
2176*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2177*01826a49SYabin Cui void const* src, size_t srcSize)
2178*01826a49SYabin Cui
2179*01826a49SYabin Cui {
2180*01826a49SYabin Cui return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2);
2181*01826a49SYabin Cui }
2182*01826a49SYabin Cui
ZSTD_compressBlock_lazy2_extDict_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2183*01826a49SYabin Cui size_t ZSTD_compressBlock_lazy2_extDict_row(
2184*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2185*01826a49SYabin Cui void const* src, size_t srcSize)
2186*01826a49SYabin Cui {
2187*01826a49SYabin Cui return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2);
2188*01826a49SYabin Cui }
2189*01826a49SYabin Cui #endif
2190*01826a49SYabin Cui
2191*01826a49SYabin Cui #ifndef ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR
ZSTD_compressBlock_btlazy2_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2192*01826a49SYabin Cui size_t ZSTD_compressBlock_btlazy2_extDict(
2193*01826a49SYabin Cui ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2194*01826a49SYabin Cui void const* src, size_t srcSize)
2195*01826a49SYabin Cui
2196*01826a49SYabin Cui {
2197*01826a49SYabin Cui return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2);
2198*01826a49SYabin Cui }
2199*01826a49SYabin Cui #endif
2200