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