10c16b537SWarner Losh /*
2*5ff13fbcSAllan Jude * Copyright (c) Yann Collet, Facebook, Inc.
30c16b537SWarner Losh * All rights reserved.
40c16b537SWarner Losh *
50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh */
100c16b537SWarner Losh
11052d3c12SConrad Meyer #include "zstd_compress_internal.h"
120c16b537SWarner Losh #include "zstd_lazy.h"
130c16b537SWarner Losh
140c16b537SWarner Losh
150c16b537SWarner Losh /*-*************************************
160c16b537SWarner Losh * Binary Tree search
170c16b537SWarner Losh ***************************************/
1819fcbaf1SConrad Meyer
190f743729SConrad Meyer static void
ZSTD_updateDUBT(ZSTD_matchState_t * ms,const BYTE * ip,const BYTE * iend,U32 mls)200f743729SConrad Meyer ZSTD_updateDUBT(ZSTD_matchState_t* ms,
2119fcbaf1SConrad Meyer const BYTE* ip, const BYTE* iend,
2219fcbaf1SConrad Meyer U32 mls)
230c16b537SWarner Losh {
240f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams;
2519fcbaf1SConrad Meyer U32* const hashTable = ms->hashTable;
2619fcbaf1SConrad Meyer U32 const hashLog = cParams->hashLog;
2719fcbaf1SConrad Meyer
2819fcbaf1SConrad Meyer U32* const bt = ms->chainTable;
2919fcbaf1SConrad Meyer U32 const btLog = cParams->chainLog - 1;
300c16b537SWarner Losh U32 const btMask = (1 << btLog) - 1;
3119fcbaf1SConrad Meyer
3219fcbaf1SConrad Meyer const BYTE* const base = ms->window.base;
3319fcbaf1SConrad Meyer U32 const target = (U32)(ip - base);
3419fcbaf1SConrad Meyer U32 idx = ms->nextToUpdate;
3519fcbaf1SConrad Meyer
3619fcbaf1SConrad Meyer if (idx != target)
3719fcbaf1SConrad Meyer DEBUGLOG(7, "ZSTD_updateDUBT, from %u to %u (dictLimit:%u)",
3819fcbaf1SConrad Meyer idx, target, ms->window.dictLimit);
3919fcbaf1SConrad Meyer assert(ip + 8 <= iend); /* condition for ZSTD_hashPtr */
4019fcbaf1SConrad Meyer (void)iend;
4119fcbaf1SConrad Meyer
4219fcbaf1SConrad Meyer assert(idx >= ms->window.dictLimit); /* condition for valid base+idx */
4319fcbaf1SConrad Meyer for ( ; idx < target ; idx++) {
4419fcbaf1SConrad Meyer size_t const h = ZSTD_hashPtr(base + idx, hashLog, mls); /* assumption : ip + 8 <= iend */
4519fcbaf1SConrad Meyer U32 const matchIndex = hashTable[h];
4619fcbaf1SConrad Meyer
4719fcbaf1SConrad Meyer U32* const nextCandidatePtr = bt + 2*(idx&btMask);
4819fcbaf1SConrad Meyer U32* const sortMarkPtr = nextCandidatePtr + 1;
4919fcbaf1SConrad Meyer
5019fcbaf1SConrad Meyer DEBUGLOG(8, "ZSTD_updateDUBT: insert %u", idx);
5119fcbaf1SConrad Meyer hashTable[h] = idx; /* Update Hash Table */
5219fcbaf1SConrad Meyer *nextCandidatePtr = matchIndex; /* update BT like a chain */
5319fcbaf1SConrad Meyer *sortMarkPtr = ZSTD_DUBT_UNSORTED_MARK;
5419fcbaf1SConrad Meyer }
5519fcbaf1SConrad Meyer ms->nextToUpdate = target;
5619fcbaf1SConrad Meyer }
5719fcbaf1SConrad Meyer
5819fcbaf1SConrad Meyer
5919fcbaf1SConrad Meyer /** ZSTD_insertDUBT1() :
6019fcbaf1SConrad Meyer * sort one already inserted but unsorted position
61f7cd7fe5SConrad Meyer * assumption : curr >= btlow == (curr - btmask)
6219fcbaf1SConrad Meyer * doesn't fail */
630f743729SConrad Meyer static void
ZSTD_insertDUBT1(const ZSTD_matchState_t * ms,U32 curr,const BYTE * inputEnd,U32 nbCompares,U32 btLow,const ZSTD_dictMode_e dictMode)64*5ff13fbcSAllan Jude ZSTD_insertDUBT1(const ZSTD_matchState_t* ms,
65f7cd7fe5SConrad Meyer U32 curr, const BYTE* inputEnd,
66a0483764SConrad Meyer U32 nbCompares, U32 btLow,
67a0483764SConrad Meyer const ZSTD_dictMode_e dictMode)
6819fcbaf1SConrad Meyer {
690f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams;
7019fcbaf1SConrad Meyer U32* const bt = ms->chainTable;
7119fcbaf1SConrad Meyer U32 const btLog = cParams->chainLog - 1;
7219fcbaf1SConrad Meyer U32 const btMask = (1 << btLog) - 1;
730c16b537SWarner Losh size_t commonLengthSmaller=0, commonLengthLarger=0;
7419fcbaf1SConrad Meyer const BYTE* const base = ms->window.base;
7519fcbaf1SConrad Meyer const BYTE* const dictBase = ms->window.dictBase;
7619fcbaf1SConrad Meyer const U32 dictLimit = ms->window.dictLimit;
77f7cd7fe5SConrad Meyer const BYTE* const ip = (curr>=dictLimit) ? base + curr : dictBase + curr;
78f7cd7fe5SConrad Meyer const BYTE* const iend = (curr>=dictLimit) ? inputEnd : dictBase + dictLimit;
790c16b537SWarner Losh const BYTE* const dictEnd = dictBase + dictLimit;
800c16b537SWarner Losh const BYTE* const prefixStart = base + dictLimit;
810c16b537SWarner Losh const BYTE* match;
82f7cd7fe5SConrad Meyer U32* smallerPtr = bt + 2*(curr&btMask);
830c16b537SWarner Losh U32* largerPtr = smallerPtr + 1;
84a0483764SConrad Meyer 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) */
850c16b537SWarner Losh U32 dummy32; /* to be nullified at the end */
864d3f1eafSConrad Meyer U32 const windowValid = ms->window.lowLimit;
874d3f1eafSConrad Meyer U32 const maxDistance = 1U << cParams->windowLog;
88f7cd7fe5SConrad Meyer U32 const windowLow = (curr - windowValid > maxDistance) ? curr - maxDistance : windowValid;
894d3f1eafSConrad Meyer
900c16b537SWarner Losh
9119fcbaf1SConrad Meyer DEBUGLOG(8, "ZSTD_insertDUBT1(%u) (dictLimit=%u, lowLimit=%u)",
92f7cd7fe5SConrad Meyer curr, dictLimit, windowLow);
93f7cd7fe5SConrad Meyer assert(curr >= btLow);
9419fcbaf1SConrad Meyer assert(ip < iend); /* condition for ZSTD_count */
950c16b537SWarner Losh
96*5ff13fbcSAllan Jude for (; nbCompares && (matchIndex > windowLow); --nbCompares) {
970c16b537SWarner Losh U32* const nextPtr = bt + 2*(matchIndex & btMask);
980c16b537SWarner Losh size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
99f7cd7fe5SConrad Meyer assert(matchIndex < curr);
100a0483764SConrad Meyer /* note : all candidates are now supposed sorted,
101a0483764SConrad Meyer * but it's still possible to have nextPtr[1] == ZSTD_DUBT_UNSORTED_MARK
102a0483764SConrad Meyer * when a real index has the same value as ZSTD_DUBT_UNSORTED_MARK */
1030c16b537SWarner Losh
1040f743729SConrad Meyer if ( (dictMode != ZSTD_extDict)
10519fcbaf1SConrad Meyer || (matchIndex+matchLength >= dictLimit) /* both in current segment*/
106f7cd7fe5SConrad Meyer || (curr < dictLimit) /* both in extDict */) {
1070f743729SConrad Meyer const BYTE* const mBase = ( (dictMode != ZSTD_extDict)
1080f743729SConrad Meyer || (matchIndex+matchLength >= dictLimit)) ?
1090f743729SConrad Meyer base : dictBase;
11019fcbaf1SConrad Meyer assert( (matchIndex+matchLength >= dictLimit) /* might be wrong if extDict is incorrectly set to 0 */
111f7cd7fe5SConrad Meyer || (curr < dictLimit) );
11219fcbaf1SConrad Meyer match = mBase + matchIndex;
113052d3c12SConrad Meyer matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
1140c16b537SWarner Losh } else {
1150c16b537SWarner Losh match = dictBase + matchIndex;
1160c16b537SWarner Losh matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
1170c16b537SWarner Losh if (matchIndex+matchLength >= dictLimit)
118a0483764SConrad Meyer match = base + matchIndex; /* preparation for next read of match[matchLength] */
1190c16b537SWarner Losh }
1200c16b537SWarner Losh
12119fcbaf1SConrad Meyer DEBUGLOG(8, "ZSTD_insertDUBT1: comparing %u with %u : found %u common bytes ",
122f7cd7fe5SConrad Meyer curr, matchIndex, (U32)matchLength);
1230c16b537SWarner Losh
124052d3c12SConrad Meyer if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
1250c16b537SWarner Losh break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt tree */
126052d3c12SConrad Meyer }
1270c16b537SWarner Losh
1280c16b537SWarner Losh if (match[matchLength] < ip[matchLength]) { /* necessarily within buffer */
129052d3c12SConrad Meyer /* match is smaller than current */
1300c16b537SWarner Losh *smallerPtr = matchIndex; /* update smaller idx */
1310c16b537SWarner Losh commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
1320c16b537SWarner Losh if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop searching */
13319fcbaf1SConrad Meyer DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is smaller : next => %u",
13419fcbaf1SConrad Meyer matchIndex, btLow, nextPtr[1]);
135052d3c12SConrad Meyer smallerPtr = nextPtr+1; /* new "candidate" => larger than match, which was smaller than target */
136052d3c12SConrad Meyer matchIndex = nextPtr[1]; /* new matchIndex, larger than previous and closer to current */
1370c16b537SWarner Losh } else {
1380c16b537SWarner Losh /* match is larger than current */
1390c16b537SWarner Losh *largerPtr = matchIndex;
1400c16b537SWarner Losh commonLengthLarger = matchLength;
1410c16b537SWarner Losh if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop searching */
14219fcbaf1SConrad Meyer DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is larger => %u",
14319fcbaf1SConrad Meyer matchIndex, btLow, nextPtr[0]);
1440c16b537SWarner Losh largerPtr = nextPtr;
1450c16b537SWarner Losh matchIndex = nextPtr[0];
1460c16b537SWarner Losh } }
1470c16b537SWarner Losh
1480c16b537SWarner Losh *smallerPtr = *largerPtr = 0;
1490c16b537SWarner Losh }
1500c16b537SWarner Losh
1510c16b537SWarner Losh
1520f743729SConrad Meyer 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)1530f743729SConrad Meyer ZSTD_DUBT_findBetterDictMatch (
154*5ff13fbcSAllan Jude const ZSTD_matchState_t* ms,
1550f743729SConrad Meyer const BYTE* const ip, const BYTE* const iend,
1560f743729SConrad Meyer size_t* offsetPtr,
1570f743729SConrad Meyer size_t bestLength,
1580f743729SConrad Meyer U32 nbCompares,
1590f743729SConrad Meyer U32 const mls,
1600f743729SConrad Meyer const ZSTD_dictMode_e dictMode)
1610f743729SConrad Meyer {
1620f743729SConrad Meyer const ZSTD_matchState_t * const dms = ms->dictMatchState;
1630f743729SConrad Meyer const ZSTD_compressionParameters* const dmsCParams = &dms->cParams;
1640f743729SConrad Meyer const U32 * const dictHashTable = dms->hashTable;
1650f743729SConrad Meyer U32 const hashLog = dmsCParams->hashLog;
1660f743729SConrad Meyer size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
1670f743729SConrad Meyer U32 dictMatchIndex = dictHashTable[h];
1680f743729SConrad Meyer
1690f743729SConrad Meyer const BYTE* const base = ms->window.base;
1700f743729SConrad Meyer const BYTE* const prefixStart = base + ms->window.dictLimit;
171f7cd7fe5SConrad Meyer U32 const curr = (U32)(ip-base);
1720f743729SConrad Meyer const BYTE* const dictBase = dms->window.base;
1730f743729SConrad Meyer const BYTE* const dictEnd = dms->window.nextSrc;
1740f743729SConrad Meyer U32 const dictHighLimit = (U32)(dms->window.nextSrc - dms->window.base);
1750f743729SConrad Meyer U32 const dictLowLimit = dms->window.lowLimit;
1760f743729SConrad Meyer U32 const dictIndexDelta = ms->window.lowLimit - dictHighLimit;
1770f743729SConrad Meyer
1780f743729SConrad Meyer U32* const dictBt = dms->chainTable;
1790f743729SConrad Meyer U32 const btLog = dmsCParams->chainLog - 1;
1800f743729SConrad Meyer U32 const btMask = (1 << btLog) - 1;
1810f743729SConrad Meyer U32 const btLow = (btMask >= dictHighLimit - dictLowLimit) ? dictLowLimit : dictHighLimit - btMask;
1820f743729SConrad Meyer
1830f743729SConrad Meyer size_t commonLengthSmaller=0, commonLengthLarger=0;
1840f743729SConrad Meyer
1850f743729SConrad Meyer (void)dictMode;
1860f743729SConrad Meyer assert(dictMode == ZSTD_dictMatchState);
1870f743729SConrad Meyer
188*5ff13fbcSAllan Jude for (; nbCompares && (dictMatchIndex > dictLowLimit); --nbCompares) {
1890f743729SConrad Meyer U32* const nextPtr = dictBt + 2*(dictMatchIndex & btMask);
1900f743729SConrad Meyer size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
1910f743729SConrad Meyer const BYTE* match = dictBase + dictMatchIndex;
1920f743729SConrad Meyer matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
1930f743729SConrad Meyer if (dictMatchIndex+matchLength >= dictHighLimit)
1940f743729SConrad Meyer match = base + dictMatchIndex + dictIndexDelta; /* to prepare for next usage of match[matchLength] */
1950f743729SConrad Meyer
1960f743729SConrad Meyer if (matchLength > bestLength) {
1970f743729SConrad Meyer U32 matchIndex = dictMatchIndex + dictIndexDelta;
198f7cd7fe5SConrad Meyer if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) ) {
1990f743729SConrad Meyer DEBUGLOG(9, "ZSTD_DUBT_findBetterDictMatch(%u) : found better match length %u -> %u and offsetCode %u -> %u (dictMatchIndex %u, matchIndex %u)",
200*5ff13fbcSAllan Jude curr, (U32)bestLength, (U32)matchLength, (U32)*offsetPtr, STORE_OFFSET(curr - matchIndex), dictMatchIndex, matchIndex);
201*5ff13fbcSAllan Jude bestLength = matchLength, *offsetPtr = STORE_OFFSET(curr - matchIndex);
2020f743729SConrad Meyer }
2030f743729SConrad Meyer 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 */
2040f743729SConrad Meyer break; /* drop, to guarantee consistency (miss a little bit of compression) */
2050f743729SConrad Meyer }
2060f743729SConrad Meyer }
2070f743729SConrad Meyer
2080f743729SConrad Meyer if (match[matchLength] < ip[matchLength]) {
2090f743729SConrad Meyer if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
2100f743729SConrad Meyer commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
2110f743729SConrad Meyer dictMatchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
2120f743729SConrad Meyer } else {
2130f743729SConrad Meyer /* match is larger than current */
2140f743729SConrad Meyer if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
2150f743729SConrad Meyer commonLengthLarger = matchLength;
2160f743729SConrad Meyer dictMatchIndex = nextPtr[0];
2170f743729SConrad Meyer }
2180f743729SConrad Meyer }
2190f743729SConrad Meyer
2200f743729SConrad Meyer if (bestLength >= MINMATCH) {
221*5ff13fbcSAllan Jude U32 const mIndex = curr - (U32)STORED_OFFSET(*offsetPtr); (void)mIndex;
2220f743729SConrad Meyer DEBUGLOG(8, "ZSTD_DUBT_findBetterDictMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
223f7cd7fe5SConrad Meyer curr, (U32)bestLength, (U32)*offsetPtr, mIndex);
2240f743729SConrad Meyer }
2250f743729SConrad Meyer return bestLength;
2260f743729SConrad Meyer
2270f743729SConrad Meyer }
2280f743729SConrad Meyer
2290f743729SConrad Meyer
2300f743729SConrad Meyer 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)2310f743729SConrad Meyer ZSTD_DUBT_findBestMatch(ZSTD_matchState_t* ms,
2320c16b537SWarner Losh const BYTE* const ip, const BYTE* const iend,
2330c16b537SWarner Losh size_t* offsetPtr,
23419fcbaf1SConrad Meyer U32 const mls,
2350f743729SConrad Meyer const ZSTD_dictMode_e dictMode)
2360c16b537SWarner Losh {
2370f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams;
23819fcbaf1SConrad Meyer U32* const hashTable = ms->hashTable;
23919fcbaf1SConrad Meyer U32 const hashLog = cParams->hashLog;
2400c16b537SWarner Losh size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
2410c16b537SWarner Losh U32 matchIndex = hashTable[h];
24219fcbaf1SConrad Meyer
24319fcbaf1SConrad Meyer const BYTE* const base = ms->window.base;
244f7cd7fe5SConrad Meyer U32 const curr = (U32)(ip-base);
245f7cd7fe5SConrad Meyer U32 const windowLow = ZSTD_getLowestMatchIndex(ms, curr, cParams->windowLog);
24619fcbaf1SConrad Meyer
24719fcbaf1SConrad Meyer U32* const bt = ms->chainTable;
24819fcbaf1SConrad Meyer U32 const btLog = cParams->chainLog - 1;
24919fcbaf1SConrad Meyer U32 const btMask = (1 << btLog) - 1;
250f7cd7fe5SConrad Meyer U32 const btLow = (btMask >= curr) ? 0 : curr - btMask;
25119fcbaf1SConrad Meyer U32 const unsortLimit = MAX(btLow, windowLow);
25219fcbaf1SConrad Meyer
25319fcbaf1SConrad Meyer U32* nextCandidate = bt + 2*(matchIndex&btMask);
25419fcbaf1SConrad Meyer U32* unsortedMark = bt + 2*(matchIndex&btMask) + 1;
25519fcbaf1SConrad Meyer U32 nbCompares = 1U << cParams->searchLog;
25619fcbaf1SConrad Meyer U32 nbCandidates = nbCompares;
25719fcbaf1SConrad Meyer U32 previousCandidate = 0;
25819fcbaf1SConrad Meyer
259f7cd7fe5SConrad Meyer DEBUGLOG(7, "ZSTD_DUBT_findBestMatch (%u) ", curr);
26019fcbaf1SConrad Meyer assert(ip <= iend-8); /* required for h calculation */
261f7cd7fe5SConrad Meyer assert(dictMode != ZSTD_dedicatedDictSearch);
26219fcbaf1SConrad Meyer
26319fcbaf1SConrad Meyer /* reach end of unsorted candidates list */
26419fcbaf1SConrad Meyer while ( (matchIndex > unsortLimit)
26519fcbaf1SConrad Meyer && (*unsortedMark == ZSTD_DUBT_UNSORTED_MARK)
26619fcbaf1SConrad Meyer && (nbCandidates > 1) ) {
26719fcbaf1SConrad Meyer DEBUGLOG(8, "ZSTD_DUBT_findBestMatch: candidate %u is unsorted",
26819fcbaf1SConrad Meyer matchIndex);
269a0483764SConrad Meyer *unsortedMark = previousCandidate; /* the unsortedMark becomes a reversed chain, to move up back to original position */
27019fcbaf1SConrad Meyer previousCandidate = matchIndex;
27119fcbaf1SConrad Meyer matchIndex = *nextCandidate;
27219fcbaf1SConrad Meyer nextCandidate = bt + 2*(matchIndex&btMask);
27319fcbaf1SConrad Meyer unsortedMark = bt + 2*(matchIndex&btMask) + 1;
27419fcbaf1SConrad Meyer nbCandidates --;
27519fcbaf1SConrad Meyer }
27619fcbaf1SConrad Meyer
277a0483764SConrad Meyer /* nullify last candidate if it's still unsorted
278a0483764SConrad Meyer * simplification, detrimental to compression ratio, beneficial for speed */
27919fcbaf1SConrad Meyer if ( (matchIndex > unsortLimit)
28019fcbaf1SConrad Meyer && (*unsortedMark==ZSTD_DUBT_UNSORTED_MARK) ) {
28119fcbaf1SConrad Meyer DEBUGLOG(7, "ZSTD_DUBT_findBestMatch: nullify last unsorted candidate %u",
28219fcbaf1SConrad Meyer matchIndex);
283a0483764SConrad Meyer *nextCandidate = *unsortedMark = 0;
28419fcbaf1SConrad Meyer }
28519fcbaf1SConrad Meyer
28619fcbaf1SConrad Meyer /* batch sort stacked candidates */
28719fcbaf1SConrad Meyer matchIndex = previousCandidate;
28819fcbaf1SConrad Meyer while (matchIndex) { /* will end on matchIndex == 0 */
28919fcbaf1SConrad Meyer U32* const nextCandidateIdxPtr = bt + 2*(matchIndex&btMask) + 1;
29019fcbaf1SConrad Meyer U32 const nextCandidateIdx = *nextCandidateIdxPtr;
2910f743729SConrad Meyer ZSTD_insertDUBT1(ms, matchIndex, iend,
2920f743729SConrad Meyer nbCandidates, unsortLimit, dictMode);
29319fcbaf1SConrad Meyer matchIndex = nextCandidateIdx;
29419fcbaf1SConrad Meyer nbCandidates++;
29519fcbaf1SConrad Meyer }
29619fcbaf1SConrad Meyer
29719fcbaf1SConrad Meyer /* find longest match */
29819fcbaf1SConrad Meyer { size_t commonLengthSmaller = 0, commonLengthLarger = 0;
29919fcbaf1SConrad Meyer const BYTE* const dictBase = ms->window.dictBase;
30019fcbaf1SConrad Meyer const U32 dictLimit = ms->window.dictLimit;
3010c16b537SWarner Losh const BYTE* const dictEnd = dictBase + dictLimit;
3020c16b537SWarner Losh const BYTE* const prefixStart = base + dictLimit;
303f7cd7fe5SConrad Meyer U32* smallerPtr = bt + 2*(curr&btMask);
304f7cd7fe5SConrad Meyer U32* largerPtr = bt + 2*(curr&btMask) + 1;
305f7cd7fe5SConrad Meyer U32 matchEndIdx = curr + 8 + 1;
3060c16b537SWarner Losh U32 dummy32; /* to be nullified at the end */
3070c16b537SWarner Losh size_t bestLength = 0;
3080c16b537SWarner Losh
30919fcbaf1SConrad Meyer matchIndex = hashTable[h];
310f7cd7fe5SConrad Meyer hashTable[h] = curr; /* Update Hash Table */
3110c16b537SWarner Losh
312*5ff13fbcSAllan Jude for (; nbCompares && (matchIndex > windowLow); --nbCompares) {
3130c16b537SWarner Losh U32* const nextPtr = bt + 2*(matchIndex & btMask);
3140c16b537SWarner Losh size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
3150c16b537SWarner Losh const BYTE* match;
3160c16b537SWarner Losh
3170f743729SConrad Meyer if ((dictMode != ZSTD_extDict) || (matchIndex+matchLength >= dictLimit)) {
3180c16b537SWarner Losh match = base + matchIndex;
319052d3c12SConrad Meyer matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
3200c16b537SWarner Losh } else {
3210c16b537SWarner Losh match = dictBase + matchIndex;
3220c16b537SWarner Losh matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
3230c16b537SWarner Losh if (matchIndex+matchLength >= dictLimit)
3240c16b537SWarner Losh match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
3250c16b537SWarner Losh }
3260c16b537SWarner Losh
3270c16b537SWarner Losh if (matchLength > bestLength) {
3280c16b537SWarner Losh if (matchLength > matchEndIdx - matchIndex)
3290c16b537SWarner Losh matchEndIdx = matchIndex + (U32)matchLength;
330f7cd7fe5SConrad Meyer if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) )
331*5ff13fbcSAllan Jude bestLength = matchLength, *offsetPtr = STORE_OFFSET(curr - matchIndex);
332052d3c12SConrad Meyer if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
3330f743729SConrad Meyer if (dictMode == ZSTD_dictMatchState) {
3340f743729SConrad Meyer nbCompares = 0; /* in addition to avoiding checking any
3350f743729SConrad Meyer * further in this loop, make sure we
3360f743729SConrad Meyer * skip checking in the dictionary. */
3370f743729SConrad Meyer }
3380c16b537SWarner Losh break; /* drop, to guarantee consistency (miss a little bit of compression) */
3390c16b537SWarner Losh }
340052d3c12SConrad Meyer }
3410c16b537SWarner Losh
3420c16b537SWarner Losh if (match[matchLength] < ip[matchLength]) {
3430c16b537SWarner Losh /* match is smaller than current */
3440c16b537SWarner Losh *smallerPtr = matchIndex; /* update smaller idx */
3450c16b537SWarner Losh commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
3460c16b537SWarner Losh if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */
3470c16b537SWarner Losh smallerPtr = nextPtr+1; /* new "smaller" => larger of match */
3480c16b537SWarner Losh matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
3490c16b537SWarner Losh } else {
3500c16b537SWarner Losh /* match is larger than current */
3510c16b537SWarner Losh *largerPtr = matchIndex;
3520c16b537SWarner Losh commonLengthLarger = matchLength;
3530c16b537SWarner Losh if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */
3540c16b537SWarner Losh largerPtr = nextPtr;
3550c16b537SWarner Losh matchIndex = nextPtr[0];
3560c16b537SWarner Losh } }
3570c16b537SWarner Losh
3580c16b537SWarner Losh *smallerPtr = *largerPtr = 0;
3590c16b537SWarner Losh
360*5ff13fbcSAllan Jude assert(nbCompares <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
3610f743729SConrad Meyer if (dictMode == ZSTD_dictMatchState && nbCompares) {
3620f743729SConrad Meyer bestLength = ZSTD_DUBT_findBetterDictMatch(
3630f743729SConrad Meyer ms, ip, iend,
3640f743729SConrad Meyer offsetPtr, bestLength, nbCompares,
3650f743729SConrad Meyer mls, dictMode);
3660f743729SConrad Meyer }
3670f743729SConrad Meyer
368f7cd7fe5SConrad Meyer assert(matchEndIdx > curr+8); /* ensure nextToUpdate is increased */
36919fcbaf1SConrad Meyer ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */
37019fcbaf1SConrad Meyer if (bestLength >= MINMATCH) {
371*5ff13fbcSAllan Jude U32 const mIndex = curr - (U32)STORED_OFFSET(*offsetPtr); (void)mIndex;
37219fcbaf1SConrad Meyer DEBUGLOG(8, "ZSTD_DUBT_findBestMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
373f7cd7fe5SConrad Meyer curr, (U32)bestLength, (U32)*offsetPtr, mIndex);
37419fcbaf1SConrad Meyer }
3750c16b537SWarner Losh return bestLength;
3760c16b537SWarner Losh }
37719fcbaf1SConrad Meyer }
3780c16b537SWarner Losh
3790c16b537SWarner Losh
3800c16b537SWarner Losh /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */
3810f743729SConrad Meyer 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)3820f743729SConrad Meyer ZSTD_BtFindBestMatch( ZSTD_matchState_t* ms,
3830c16b537SWarner Losh const BYTE* const ip, const BYTE* const iLimit,
3840c16b537SWarner Losh size_t* offsetPtr,
3850f743729SConrad Meyer const U32 mls /* template */,
3860f743729SConrad Meyer const ZSTD_dictMode_e dictMode)
3870c16b537SWarner Losh {
38819fcbaf1SConrad Meyer DEBUGLOG(7, "ZSTD_BtFindBestMatch");
38919fcbaf1SConrad Meyer if (ip < ms->window.base + ms->nextToUpdate) return 0; /* skipped area */
3900f743729SConrad Meyer ZSTD_updateDUBT(ms, ip, iLimit, mls);
3910f743729SConrad Meyer return ZSTD_DUBT_findBestMatch(ms, ip, iLimit, offsetPtr, mls, dictMode);
3920c16b537SWarner Losh }
3930c16b537SWarner Losh
394*5ff13fbcSAllan Jude /***********************************
395*5ff13fbcSAllan Jude * Dedicated dict search
3960c16b537SWarner Losh ***********************************/
39719fcbaf1SConrad Meyer
ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_matchState_t * ms,const BYTE * const ip)398f7cd7fe5SConrad Meyer void ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_matchState_t* ms, const BYTE* const ip)
399f7cd7fe5SConrad Meyer {
400f7cd7fe5SConrad Meyer const BYTE* const base = ms->window.base;
401f7cd7fe5SConrad Meyer U32 const target = (U32)(ip - base);
402f7cd7fe5SConrad Meyer U32* const hashTable = ms->hashTable;
403f7cd7fe5SConrad Meyer U32* const chainTable = ms->chainTable;
404f7cd7fe5SConrad Meyer U32 const chainSize = 1 << ms->cParams.chainLog;
405f7cd7fe5SConrad Meyer U32 idx = ms->nextToUpdate;
406*5ff13fbcSAllan Jude U32 const minChain = chainSize < target - idx ? target - chainSize : idx;
407f7cd7fe5SConrad Meyer U32 const bucketSize = 1 << ZSTD_LAZY_DDSS_BUCKET_LOG;
408f7cd7fe5SConrad Meyer U32 const cacheSize = bucketSize - 1;
409f7cd7fe5SConrad Meyer U32 const chainAttempts = (1 << ms->cParams.searchLog) - cacheSize;
410f7cd7fe5SConrad Meyer U32 const chainLimit = chainAttempts > 255 ? 255 : chainAttempts;
411f7cd7fe5SConrad Meyer
412f7cd7fe5SConrad Meyer /* We know the hashtable is oversized by a factor of `bucketSize`.
413f7cd7fe5SConrad Meyer * We are going to temporarily pretend `bucketSize == 1`, keeping only a
414f7cd7fe5SConrad Meyer * single entry. We will use the rest of the space to construct a temporary
415f7cd7fe5SConrad Meyer * chaintable.
416f7cd7fe5SConrad Meyer */
417f7cd7fe5SConrad Meyer U32 const hashLog = ms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG;
418f7cd7fe5SConrad Meyer U32* const tmpHashTable = hashTable;
419f7cd7fe5SConrad Meyer U32* const tmpChainTable = hashTable + ((size_t)1 << hashLog);
420*5ff13fbcSAllan Jude U32 const tmpChainSize = (U32)((1 << ZSTD_LAZY_DDSS_BUCKET_LOG) - 1) << hashLog;
421f7cd7fe5SConrad Meyer U32 const tmpMinChain = tmpChainSize < target ? target - tmpChainSize : idx;
422f7cd7fe5SConrad Meyer U32 hashIdx;
423f7cd7fe5SConrad Meyer
424f7cd7fe5SConrad Meyer assert(ms->cParams.chainLog <= 24);
425*5ff13fbcSAllan Jude assert(ms->cParams.hashLog > ms->cParams.chainLog);
426f7cd7fe5SConrad Meyer assert(idx != 0);
427f7cd7fe5SConrad Meyer assert(tmpMinChain <= minChain);
428f7cd7fe5SConrad Meyer
429f7cd7fe5SConrad Meyer /* fill conventional hash table and conventional chain table */
430f7cd7fe5SConrad Meyer for ( ; idx < target; idx++) {
431f7cd7fe5SConrad Meyer U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch);
432f7cd7fe5SConrad Meyer if (idx >= tmpMinChain) {
433f7cd7fe5SConrad Meyer tmpChainTable[idx - tmpMinChain] = hashTable[h];
434f7cd7fe5SConrad Meyer }
435f7cd7fe5SConrad Meyer tmpHashTable[h] = idx;
436f7cd7fe5SConrad Meyer }
437f7cd7fe5SConrad Meyer
438f7cd7fe5SConrad Meyer /* sort chains into ddss chain table */
439f7cd7fe5SConrad Meyer {
440f7cd7fe5SConrad Meyer U32 chainPos = 0;
441f7cd7fe5SConrad Meyer for (hashIdx = 0; hashIdx < (1U << hashLog); hashIdx++) {
442f7cd7fe5SConrad Meyer U32 count;
443f7cd7fe5SConrad Meyer U32 countBeyondMinChain = 0;
444f7cd7fe5SConrad Meyer U32 i = tmpHashTable[hashIdx];
445f7cd7fe5SConrad Meyer for (count = 0; i >= tmpMinChain && count < cacheSize; count++) {
446f7cd7fe5SConrad Meyer /* skip through the chain to the first position that won't be
447f7cd7fe5SConrad Meyer * in the hash cache bucket */
448f7cd7fe5SConrad Meyer if (i < minChain) {
449f7cd7fe5SConrad Meyer countBeyondMinChain++;
450f7cd7fe5SConrad Meyer }
451f7cd7fe5SConrad Meyer i = tmpChainTable[i - tmpMinChain];
452f7cd7fe5SConrad Meyer }
453f7cd7fe5SConrad Meyer if (count == cacheSize) {
454f7cd7fe5SConrad Meyer for (count = 0; count < chainLimit;) {
455f7cd7fe5SConrad Meyer if (i < minChain) {
456*5ff13fbcSAllan Jude if (!i || ++countBeyondMinChain > cacheSize) {
457f7cd7fe5SConrad Meyer /* only allow pulling `cacheSize` number of entries
458f7cd7fe5SConrad Meyer * into the cache or chainTable beyond `minChain`,
459f7cd7fe5SConrad Meyer * to replace the entries pulled out of the
460f7cd7fe5SConrad Meyer * chainTable into the cache. This lets us reach
461f7cd7fe5SConrad Meyer * back further without increasing the total number
462f7cd7fe5SConrad Meyer * of entries in the chainTable, guaranteeing the
463f7cd7fe5SConrad Meyer * DDSS chain table will fit into the space
464f7cd7fe5SConrad Meyer * allocated for the regular one. */
465f7cd7fe5SConrad Meyer break;
466f7cd7fe5SConrad Meyer }
467f7cd7fe5SConrad Meyer }
468f7cd7fe5SConrad Meyer chainTable[chainPos++] = i;
469f7cd7fe5SConrad Meyer count++;
470f7cd7fe5SConrad Meyer if (i < tmpMinChain) {
471f7cd7fe5SConrad Meyer break;
472f7cd7fe5SConrad Meyer }
473f7cd7fe5SConrad Meyer i = tmpChainTable[i - tmpMinChain];
474f7cd7fe5SConrad Meyer }
475f7cd7fe5SConrad Meyer } else {
476f7cd7fe5SConrad Meyer count = 0;
477f7cd7fe5SConrad Meyer }
478f7cd7fe5SConrad Meyer if (count) {
479f7cd7fe5SConrad Meyer tmpHashTable[hashIdx] = ((chainPos - count) << 8) + count;
480f7cd7fe5SConrad Meyer } else {
481f7cd7fe5SConrad Meyer tmpHashTable[hashIdx] = 0;
482f7cd7fe5SConrad Meyer }
483f7cd7fe5SConrad Meyer }
484f7cd7fe5SConrad Meyer assert(chainPos <= chainSize); /* I believe this is guaranteed... */
485f7cd7fe5SConrad Meyer }
486f7cd7fe5SConrad Meyer
487f7cd7fe5SConrad Meyer /* move chain pointers into the last entry of each hash bucket */
488f7cd7fe5SConrad Meyer for (hashIdx = (1 << hashLog); hashIdx; ) {
489f7cd7fe5SConrad Meyer U32 const bucketIdx = --hashIdx << ZSTD_LAZY_DDSS_BUCKET_LOG;
490f7cd7fe5SConrad Meyer U32 const chainPackedPointer = tmpHashTable[hashIdx];
491f7cd7fe5SConrad Meyer U32 i;
492f7cd7fe5SConrad Meyer for (i = 0; i < cacheSize; i++) {
493f7cd7fe5SConrad Meyer hashTable[bucketIdx + i] = 0;
494f7cd7fe5SConrad Meyer }
495f7cd7fe5SConrad Meyer hashTable[bucketIdx + bucketSize - 1] = chainPackedPointer;
496f7cd7fe5SConrad Meyer }
497f7cd7fe5SConrad Meyer
498f7cd7fe5SConrad Meyer /* fill the buckets of the hash table */
499f7cd7fe5SConrad Meyer for (idx = ms->nextToUpdate; idx < target; idx++) {
500f7cd7fe5SConrad Meyer U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch)
501f7cd7fe5SConrad Meyer << ZSTD_LAZY_DDSS_BUCKET_LOG;
502f7cd7fe5SConrad Meyer U32 i;
503f7cd7fe5SConrad Meyer /* Shift hash cache down 1. */
504f7cd7fe5SConrad Meyer for (i = cacheSize - 1; i; i--)
505f7cd7fe5SConrad Meyer hashTable[h + i] = hashTable[h + i - 1];
506f7cd7fe5SConrad Meyer hashTable[h] = idx;
507f7cd7fe5SConrad Meyer }
508f7cd7fe5SConrad Meyer
509f7cd7fe5SConrad Meyer ms->nextToUpdate = target;
510f7cd7fe5SConrad Meyer }
511f7cd7fe5SConrad Meyer
512*5ff13fbcSAllan Jude /* Returns the longest match length found in the dedicated dict search structure.
513*5ff13fbcSAllan Jude * If none are longer than the argument ml, then ml will be returned.
514*5ff13fbcSAllan Jude */
515*5ff13fbcSAllan Jude 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*5ff13fbcSAllan Jude size_t ZSTD_dedicatedDictSearch_lazy_search(size_t* offsetPtr, size_t ml, U32 nbAttempts,
517*5ff13fbcSAllan Jude const ZSTD_matchState_t* const dms,
518*5ff13fbcSAllan Jude const BYTE* const ip, const BYTE* const iLimit,
519*5ff13fbcSAllan Jude const BYTE* const prefixStart, const U32 curr,
520*5ff13fbcSAllan Jude const U32 dictLimit, const size_t ddsIdx) {
521*5ff13fbcSAllan Jude const U32 ddsLowestIndex = dms->window.dictLimit;
522*5ff13fbcSAllan Jude const BYTE* const ddsBase = dms->window.base;
523*5ff13fbcSAllan Jude const BYTE* const ddsEnd = dms->window.nextSrc;
524*5ff13fbcSAllan Jude const U32 ddsSize = (U32)(ddsEnd - ddsBase);
525*5ff13fbcSAllan Jude const U32 ddsIndexDelta = dictLimit - ddsSize;
526*5ff13fbcSAllan Jude const U32 bucketSize = (1 << ZSTD_LAZY_DDSS_BUCKET_LOG);
527*5ff13fbcSAllan Jude const U32 bucketLimit = nbAttempts < bucketSize - 1 ? nbAttempts : bucketSize - 1;
528*5ff13fbcSAllan Jude U32 ddsAttempt;
529*5ff13fbcSAllan Jude U32 matchIndex;
530*5ff13fbcSAllan Jude
531*5ff13fbcSAllan Jude for (ddsAttempt = 0; ddsAttempt < bucketSize - 1; ddsAttempt++) {
532*5ff13fbcSAllan Jude PREFETCH_L1(ddsBase + dms->hashTable[ddsIdx + ddsAttempt]);
533*5ff13fbcSAllan Jude }
534*5ff13fbcSAllan Jude
535*5ff13fbcSAllan Jude {
536*5ff13fbcSAllan Jude U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1];
537*5ff13fbcSAllan Jude U32 const chainIndex = chainPackedPointer >> 8;
538*5ff13fbcSAllan Jude
539*5ff13fbcSAllan Jude PREFETCH_L1(&dms->chainTable[chainIndex]);
540*5ff13fbcSAllan Jude }
541*5ff13fbcSAllan Jude
542*5ff13fbcSAllan Jude for (ddsAttempt = 0; ddsAttempt < bucketLimit; ddsAttempt++) {
543*5ff13fbcSAllan Jude size_t currentMl=0;
544*5ff13fbcSAllan Jude const BYTE* match;
545*5ff13fbcSAllan Jude matchIndex = dms->hashTable[ddsIdx + ddsAttempt];
546*5ff13fbcSAllan Jude match = ddsBase + matchIndex;
547*5ff13fbcSAllan Jude
548*5ff13fbcSAllan Jude if (!matchIndex) {
549*5ff13fbcSAllan Jude return ml;
550*5ff13fbcSAllan Jude }
551*5ff13fbcSAllan Jude
552*5ff13fbcSAllan Jude /* guaranteed by table construction */
553*5ff13fbcSAllan Jude (void)ddsLowestIndex;
554*5ff13fbcSAllan Jude assert(matchIndex >= ddsLowestIndex);
555*5ff13fbcSAllan Jude assert(match+4 <= ddsEnd);
556*5ff13fbcSAllan Jude if (MEM_read32(match) == MEM_read32(ip)) {
557*5ff13fbcSAllan Jude /* assumption : matchIndex <= dictLimit-4 (by table construction) */
558*5ff13fbcSAllan Jude currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4;
559*5ff13fbcSAllan Jude }
560*5ff13fbcSAllan Jude
561*5ff13fbcSAllan Jude /* save best solution */
562*5ff13fbcSAllan Jude if (currentMl > ml) {
563*5ff13fbcSAllan Jude ml = currentMl;
564*5ff13fbcSAllan Jude *offsetPtr = STORE_OFFSET(curr - (matchIndex + ddsIndexDelta));
565*5ff13fbcSAllan Jude if (ip+currentMl == iLimit) {
566*5ff13fbcSAllan Jude /* best possible, avoids read overflow on next attempt */
567*5ff13fbcSAllan Jude return ml;
568*5ff13fbcSAllan Jude }
569*5ff13fbcSAllan Jude }
570*5ff13fbcSAllan Jude }
571*5ff13fbcSAllan Jude
572*5ff13fbcSAllan Jude {
573*5ff13fbcSAllan Jude U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1];
574*5ff13fbcSAllan Jude U32 chainIndex = chainPackedPointer >> 8;
575*5ff13fbcSAllan Jude U32 const chainLength = chainPackedPointer & 0xFF;
576*5ff13fbcSAllan Jude U32 const chainAttempts = nbAttempts - ddsAttempt;
577*5ff13fbcSAllan Jude U32 const chainLimit = chainAttempts > chainLength ? chainLength : chainAttempts;
578*5ff13fbcSAllan Jude U32 chainAttempt;
579*5ff13fbcSAllan Jude
580*5ff13fbcSAllan Jude for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++) {
581*5ff13fbcSAllan Jude PREFETCH_L1(ddsBase + dms->chainTable[chainIndex + chainAttempt]);
582*5ff13fbcSAllan Jude }
583*5ff13fbcSAllan Jude
584*5ff13fbcSAllan Jude for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++, chainIndex++) {
585*5ff13fbcSAllan Jude size_t currentMl=0;
586*5ff13fbcSAllan Jude const BYTE* match;
587*5ff13fbcSAllan Jude matchIndex = dms->chainTable[chainIndex];
588*5ff13fbcSAllan Jude match = ddsBase + matchIndex;
589*5ff13fbcSAllan Jude
590*5ff13fbcSAllan Jude /* guaranteed by table construction */
591*5ff13fbcSAllan Jude assert(matchIndex >= ddsLowestIndex);
592*5ff13fbcSAllan Jude assert(match+4 <= ddsEnd);
593*5ff13fbcSAllan Jude if (MEM_read32(match) == MEM_read32(ip)) {
594*5ff13fbcSAllan Jude /* assumption : matchIndex <= dictLimit-4 (by table construction) */
595*5ff13fbcSAllan Jude currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4;
596*5ff13fbcSAllan Jude }
597*5ff13fbcSAllan Jude
598*5ff13fbcSAllan Jude /* save best solution */
599*5ff13fbcSAllan Jude if (currentMl > ml) {
600*5ff13fbcSAllan Jude ml = currentMl;
601*5ff13fbcSAllan Jude *offsetPtr = STORE_OFFSET(curr - (matchIndex + ddsIndexDelta));
602*5ff13fbcSAllan Jude if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
603*5ff13fbcSAllan Jude }
604*5ff13fbcSAllan Jude }
605*5ff13fbcSAllan Jude }
606*5ff13fbcSAllan Jude return ml;
607*5ff13fbcSAllan Jude }
608*5ff13fbcSAllan Jude
609*5ff13fbcSAllan Jude
610*5ff13fbcSAllan Jude /* *********************************
611*5ff13fbcSAllan Jude * Hash Chain
612*5ff13fbcSAllan Jude ***********************************/
613*5ff13fbcSAllan Jude #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & (mask)]
614*5ff13fbcSAllan Jude
615*5ff13fbcSAllan Jude /* Update chains up to ip (excluded)
616*5ff13fbcSAllan Jude 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*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE U32 ZSTD_insertAndFindFirstIndex_internal(
618*5ff13fbcSAllan Jude ZSTD_matchState_t* ms,
619*5ff13fbcSAllan Jude const ZSTD_compressionParameters* const cParams,
620*5ff13fbcSAllan Jude const BYTE* ip, U32 const mls)
621*5ff13fbcSAllan Jude {
622*5ff13fbcSAllan Jude U32* const hashTable = ms->hashTable;
623*5ff13fbcSAllan Jude const U32 hashLog = cParams->hashLog;
624*5ff13fbcSAllan Jude U32* const chainTable = ms->chainTable;
625*5ff13fbcSAllan Jude const U32 chainMask = (1 << cParams->chainLog) - 1;
626*5ff13fbcSAllan Jude const BYTE* const base = ms->window.base;
627*5ff13fbcSAllan Jude const U32 target = (U32)(ip - base);
628*5ff13fbcSAllan Jude U32 idx = ms->nextToUpdate;
629*5ff13fbcSAllan Jude
630*5ff13fbcSAllan Jude while(idx < target) { /* catch up */
631*5ff13fbcSAllan Jude size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls);
632*5ff13fbcSAllan Jude NEXT_IN_CHAIN(idx, chainMask) = hashTable[h];
633*5ff13fbcSAllan Jude hashTable[h] = idx;
634*5ff13fbcSAllan Jude idx++;
635*5ff13fbcSAllan Jude }
636*5ff13fbcSAllan Jude
637*5ff13fbcSAllan Jude ms->nextToUpdate = target;
638*5ff13fbcSAllan Jude return hashTable[ZSTD_hashPtr(ip, hashLog, mls)];
639*5ff13fbcSAllan Jude }
640*5ff13fbcSAllan Jude
ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t * ms,const BYTE * ip)641*5ff13fbcSAllan Jude U32 ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t* ms, const BYTE* ip) {
642*5ff13fbcSAllan Jude const ZSTD_compressionParameters* const cParams = &ms->cParams;
643*5ff13fbcSAllan Jude return ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, ms->cParams.minMatch);
644*5ff13fbcSAllan Jude }
6450c16b537SWarner Losh
6460c16b537SWarner Losh /* inlining is important to hardwire a hot branch (template emulation) */
6470c16b537SWarner Losh 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*5ff13fbcSAllan Jude size_t ZSTD_HcFindBestMatch(
6490f743729SConrad Meyer ZSTD_matchState_t* ms,
6500c16b537SWarner Losh const BYTE* const ip, const BYTE* const iLimit,
6510c16b537SWarner Losh size_t* offsetPtr,
6520f743729SConrad Meyer const U32 mls, const ZSTD_dictMode_e dictMode)
6530c16b537SWarner Losh {
6540f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams;
65519fcbaf1SConrad Meyer U32* const chainTable = ms->chainTable;
65619fcbaf1SConrad Meyer const U32 chainSize = (1 << cParams->chainLog);
6570c16b537SWarner Losh const U32 chainMask = chainSize-1;
65819fcbaf1SConrad Meyer const BYTE* const base = ms->window.base;
65919fcbaf1SConrad Meyer const BYTE* const dictBase = ms->window.dictBase;
66019fcbaf1SConrad Meyer const U32 dictLimit = ms->window.dictLimit;
6610c16b537SWarner Losh const BYTE* const prefixStart = base + dictLimit;
6620c16b537SWarner Losh const BYTE* const dictEnd = dictBase + dictLimit;
663f7cd7fe5SConrad Meyer const U32 curr = (U32)(ip-base);
6644d3f1eafSConrad Meyer const U32 maxDistance = 1U << cParams->windowLog;
6659cbefe25SConrad Meyer const U32 lowestValid = ms->window.lowLimit;
666f7cd7fe5SConrad Meyer const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
6679cbefe25SConrad Meyer const U32 isDictionary = (ms->loadedDictEnd != 0);
6689cbefe25SConrad Meyer const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance;
669f7cd7fe5SConrad Meyer const U32 minChain = curr > chainSize ? curr - chainSize : 0;
67019fcbaf1SConrad Meyer U32 nbAttempts = 1U << cParams->searchLog;
6710c16b537SWarner Losh size_t ml=4-1;
6720c16b537SWarner Losh
673f7cd7fe5SConrad Meyer const ZSTD_matchState_t* const dms = ms->dictMatchState;
674f7cd7fe5SConrad Meyer const U32 ddsHashLog = dictMode == ZSTD_dedicatedDictSearch
675f7cd7fe5SConrad Meyer ? dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG : 0;
676f7cd7fe5SConrad Meyer const size_t ddsIdx = dictMode == ZSTD_dedicatedDictSearch
677f7cd7fe5SConrad Meyer ? ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG : 0;
6780c16b537SWarner Losh
679f7cd7fe5SConrad Meyer U32 matchIndex;
680f7cd7fe5SConrad Meyer
681f7cd7fe5SConrad Meyer if (dictMode == ZSTD_dedicatedDictSearch) {
682f7cd7fe5SConrad Meyer const U32* entry = &dms->hashTable[ddsIdx];
683f7cd7fe5SConrad Meyer PREFETCH_L1(entry);
684f7cd7fe5SConrad Meyer }
685f7cd7fe5SConrad Meyer
686f7cd7fe5SConrad Meyer /* HC4 match finder */
687f7cd7fe5SConrad Meyer matchIndex = ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, mls);
688f7cd7fe5SConrad Meyer
689f7cd7fe5SConrad Meyer for ( ; (matchIndex>=lowLimit) & (nbAttempts>0) ; nbAttempts--) {
6900c16b537SWarner Losh size_t currentMl=0;
6910f743729SConrad Meyer if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
692052d3c12SConrad Meyer const BYTE* const match = base + matchIndex;
693a0483764SConrad Meyer assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */
6940c16b537SWarner Losh if (match[ml] == ip[ml]) /* potentially better */
6950c16b537SWarner Losh currentMl = ZSTD_count(ip, match, iLimit);
6960c16b537SWarner Losh } else {
697052d3c12SConrad Meyer const BYTE* const match = dictBase + matchIndex;
698052d3c12SConrad Meyer assert(match+4 <= dictEnd);
6990c16b537SWarner Losh if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
7000c16b537SWarner Losh currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4;
7010c16b537SWarner Losh }
7020c16b537SWarner Losh
7030c16b537SWarner Losh /* save best solution */
7040c16b537SWarner Losh if (currentMl > ml) {
7050c16b537SWarner Losh ml = currentMl;
706*5ff13fbcSAllan Jude *offsetPtr = STORE_OFFSET(curr - matchIndex);
7070c16b537SWarner Losh if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
7080c16b537SWarner Losh }
7090c16b537SWarner Losh
7100c16b537SWarner Losh if (matchIndex <= minChain) break;
7110c16b537SWarner Losh matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask);
7120c16b537SWarner Losh }
7130c16b537SWarner Losh
714*5ff13fbcSAllan Jude assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
715f7cd7fe5SConrad Meyer if (dictMode == ZSTD_dedicatedDictSearch) {
716*5ff13fbcSAllan Jude ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts, dms,
717*5ff13fbcSAllan Jude ip, iLimit, prefixStart, curr, dictLimit, ddsIdx);
718f7cd7fe5SConrad Meyer } else if (dictMode == ZSTD_dictMatchState) {
7190f743729SConrad Meyer const U32* const dmsChainTable = dms->chainTable;
7200f743729SConrad Meyer const U32 dmsChainSize = (1 << dms->cParams.chainLog);
7210f743729SConrad Meyer const U32 dmsChainMask = dmsChainSize - 1;
7220f743729SConrad Meyer const U32 dmsLowestIndex = dms->window.dictLimit;
7230f743729SConrad Meyer const BYTE* const dmsBase = dms->window.base;
7240f743729SConrad Meyer const BYTE* const dmsEnd = dms->window.nextSrc;
7250f743729SConrad Meyer const U32 dmsSize = (U32)(dmsEnd - dmsBase);
7260f743729SConrad Meyer const U32 dmsIndexDelta = dictLimit - dmsSize;
7270f743729SConrad Meyer const U32 dmsMinChain = dmsSize > dmsChainSize ? dmsSize - dmsChainSize : 0;
7280f743729SConrad Meyer
7290f743729SConrad Meyer matchIndex = dms->hashTable[ZSTD_hashPtr(ip, dms->cParams.hashLog, mls)];
7300f743729SConrad Meyer
731f7cd7fe5SConrad Meyer for ( ; (matchIndex>=dmsLowestIndex) & (nbAttempts>0) ; nbAttempts--) {
7320f743729SConrad Meyer size_t currentMl=0;
7330f743729SConrad Meyer const BYTE* const match = dmsBase + matchIndex;
7340f743729SConrad Meyer assert(match+4 <= dmsEnd);
7350f743729SConrad Meyer if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
7360f743729SConrad Meyer currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4;
7370f743729SConrad Meyer
7380f743729SConrad Meyer /* save best solution */
7390f743729SConrad Meyer if (currentMl > ml) {
7400f743729SConrad Meyer ml = currentMl;
741*5ff13fbcSAllan Jude assert(curr > matchIndex + dmsIndexDelta);
742*5ff13fbcSAllan Jude *offsetPtr = STORE_OFFSET(curr - (matchIndex + dmsIndexDelta));
7430f743729SConrad Meyer if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
7440f743729SConrad Meyer }
7450f743729SConrad Meyer
7460f743729SConrad Meyer if (matchIndex <= dmsMinChain) break;
747f7cd7fe5SConrad Meyer
7480f743729SConrad Meyer matchIndex = dmsChainTable[matchIndex & dmsChainMask];
7490f743729SConrad Meyer }
7500f743729SConrad Meyer }
7510f743729SConrad Meyer
7520c16b537SWarner Losh return ml;
7530c16b537SWarner Losh }
7540c16b537SWarner Losh
755*5ff13fbcSAllan Jude /* *********************************
756*5ff13fbcSAllan Jude * (SIMD) Row-based matchfinder
757*5ff13fbcSAllan Jude ***********************************/
758*5ff13fbcSAllan Jude /* Constants for row-based hash */
759*5ff13fbcSAllan Jude #define ZSTD_ROW_HASH_TAG_OFFSET 16 /* byte offset of hashes in the match state's tagTable from the beginning of a row */
760*5ff13fbcSAllan Jude #define ZSTD_ROW_HASH_TAG_BITS 8 /* nb bits to use for the tag */
761*5ff13fbcSAllan Jude #define ZSTD_ROW_HASH_TAG_MASK ((1u << ZSTD_ROW_HASH_TAG_BITS) - 1)
762*5ff13fbcSAllan Jude #define ZSTD_ROW_HASH_MAX_ENTRIES 64 /* absolute maximum number of entries per row, for all configurations */
7630c16b537SWarner Losh
764*5ff13fbcSAllan Jude #define ZSTD_ROW_HASH_CACHE_MASK (ZSTD_ROW_HASH_CACHE_SIZE - 1)
765*5ff13fbcSAllan Jude
766*5ff13fbcSAllan Jude typedef U64 ZSTD_VecMask; /* Clarifies when we are interacting with a U64 representing a mask of matches */
767*5ff13fbcSAllan Jude
768*5ff13fbcSAllan Jude /* ZSTD_VecMask_next():
769*5ff13fbcSAllan Jude * Starting from the LSB, returns the idx of the next non-zero bit.
770*5ff13fbcSAllan Jude * Basically counting the nb of trailing zeroes.
771*5ff13fbcSAllan Jude */
ZSTD_VecMask_next(ZSTD_VecMask val)772*5ff13fbcSAllan Jude static U32 ZSTD_VecMask_next(ZSTD_VecMask val) {
773*5ff13fbcSAllan Jude assert(val != 0);
774*5ff13fbcSAllan Jude # if defined(_MSC_VER) && defined(_WIN64)
775*5ff13fbcSAllan Jude if (val != 0) {
776*5ff13fbcSAllan Jude unsigned long r;
777*5ff13fbcSAllan Jude _BitScanForward64(&r, val);
778*5ff13fbcSAllan Jude return (U32)(r);
779*5ff13fbcSAllan Jude } else {
780*5ff13fbcSAllan Jude /* Should not reach this code path */
781*5ff13fbcSAllan Jude __assume(0);
782*5ff13fbcSAllan Jude }
783*5ff13fbcSAllan Jude # elif (defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))
784*5ff13fbcSAllan Jude if (sizeof(size_t) == 4) {
785*5ff13fbcSAllan Jude U32 mostSignificantWord = (U32)(val >> 32);
786*5ff13fbcSAllan Jude U32 leastSignificantWord = (U32)val;
787*5ff13fbcSAllan Jude if (leastSignificantWord == 0) {
788*5ff13fbcSAllan Jude return 32 + (U32)__builtin_ctz(mostSignificantWord);
789*5ff13fbcSAllan Jude } else {
790*5ff13fbcSAllan Jude return (U32)__builtin_ctz(leastSignificantWord);
791*5ff13fbcSAllan Jude }
792*5ff13fbcSAllan Jude } else {
793*5ff13fbcSAllan Jude return (U32)__builtin_ctzll(val);
794*5ff13fbcSAllan Jude }
795*5ff13fbcSAllan Jude # else
796*5ff13fbcSAllan Jude /* Software ctz version: http://aggregate.org/MAGIC/#Trailing%20Zero%20Count
797*5ff13fbcSAllan Jude * and: https://stackoverflow.com/questions/2709430/count-number-of-bits-in-a-64-bit-long-big-integer
798*5ff13fbcSAllan Jude */
799*5ff13fbcSAllan Jude val = ~val & (val - 1ULL); /* Lowest set bit mask */
800*5ff13fbcSAllan Jude val = val - ((val >> 1) & 0x5555555555555555);
801*5ff13fbcSAllan Jude val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
802*5ff13fbcSAllan Jude return (U32)((((val + (val >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);
803*5ff13fbcSAllan Jude # endif
804*5ff13fbcSAllan Jude }
805*5ff13fbcSAllan Jude
806*5ff13fbcSAllan Jude /* ZSTD_rotateRight_*():
807*5ff13fbcSAllan Jude * Rotates a bitfield to the right by "count" bits.
808*5ff13fbcSAllan Jude * https://en.wikipedia.org/w/index.php?title=Circular_shift&oldid=991635599#Implementing_circular_shifts
809*5ff13fbcSAllan Jude */
810*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE
ZSTD_rotateRight_U64(U64 const value,U32 count)811*5ff13fbcSAllan Jude U64 ZSTD_rotateRight_U64(U64 const value, U32 count) {
812*5ff13fbcSAllan Jude assert(count < 64);
813*5ff13fbcSAllan Jude count &= 0x3F; /* for fickle pattern recognition */
814*5ff13fbcSAllan Jude return (value >> count) | (U64)(value << ((0U - count) & 0x3F));
815*5ff13fbcSAllan Jude }
816*5ff13fbcSAllan Jude
817*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE
ZSTD_rotateRight_U32(U32 const value,U32 count)818*5ff13fbcSAllan Jude U32 ZSTD_rotateRight_U32(U32 const value, U32 count) {
819*5ff13fbcSAllan Jude assert(count < 32);
820*5ff13fbcSAllan Jude count &= 0x1F; /* for fickle pattern recognition */
821*5ff13fbcSAllan Jude return (value >> count) | (U32)(value << ((0U - count) & 0x1F));
822*5ff13fbcSAllan Jude }
823*5ff13fbcSAllan Jude
824*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE
ZSTD_rotateRight_U16(U16 const value,U32 count)825*5ff13fbcSAllan Jude U16 ZSTD_rotateRight_U16(U16 const value, U32 count) {
826*5ff13fbcSAllan Jude assert(count < 16);
827*5ff13fbcSAllan Jude count &= 0x0F; /* for fickle pattern recognition */
828*5ff13fbcSAllan Jude return (value >> count) | (U16)(value << ((0U - count) & 0x0F));
829*5ff13fbcSAllan Jude }
830*5ff13fbcSAllan Jude
831*5ff13fbcSAllan Jude /* ZSTD_row_nextIndex():
832*5ff13fbcSAllan Jude * Returns the next index to insert at within a tagTable row, and updates the "head"
833*5ff13fbcSAllan Jude * value to reflect the update. Essentially cycles backwards from [0, {entries per row})
834*5ff13fbcSAllan Jude */
ZSTD_row_nextIndex(BYTE * const tagRow,U32 const rowMask)835*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE U32 ZSTD_row_nextIndex(BYTE* const tagRow, U32 const rowMask) {
836*5ff13fbcSAllan Jude U32 const next = (*tagRow - 1) & rowMask;
837*5ff13fbcSAllan Jude *tagRow = (BYTE)next;
838*5ff13fbcSAllan Jude return next;
839*5ff13fbcSAllan Jude }
840*5ff13fbcSAllan Jude
841*5ff13fbcSAllan Jude /* ZSTD_isAligned():
842*5ff13fbcSAllan Jude * Checks that a pointer is aligned to "align" bytes which must be a power of 2.
843*5ff13fbcSAllan Jude */
ZSTD_isAligned(void const * ptr,size_t align)844*5ff13fbcSAllan Jude MEM_STATIC int ZSTD_isAligned(void const* ptr, size_t align) {
845*5ff13fbcSAllan Jude assert((align & (align - 1)) == 0);
846*5ff13fbcSAllan Jude return (((size_t)ptr) & (align - 1)) == 0;
847*5ff13fbcSAllan Jude }
848*5ff13fbcSAllan Jude
849*5ff13fbcSAllan Jude /* ZSTD_row_prefetch():
850*5ff13fbcSAllan Jude * Performs prefetching for the hashTable and tagTable at a given row.
851*5ff13fbcSAllan Jude */
ZSTD_row_prefetch(U32 const * hashTable,U16 const * tagTable,U32 const relRow,U32 const rowLog)852*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE void ZSTD_row_prefetch(U32 const* hashTable, U16 const* tagTable, U32 const relRow, U32 const rowLog) {
853*5ff13fbcSAllan Jude PREFETCH_L1(hashTable + relRow);
854*5ff13fbcSAllan Jude if (rowLog >= 5) {
855*5ff13fbcSAllan Jude PREFETCH_L1(hashTable + relRow + 16);
856*5ff13fbcSAllan Jude /* Note: prefetching more of the hash table does not appear to be beneficial for 128-entry rows */
857*5ff13fbcSAllan Jude }
858*5ff13fbcSAllan Jude PREFETCH_L1(tagTable + relRow);
859*5ff13fbcSAllan Jude if (rowLog == 6) {
860*5ff13fbcSAllan Jude PREFETCH_L1(tagTable + relRow + 32);
861*5ff13fbcSAllan Jude }
862*5ff13fbcSAllan Jude assert(rowLog == 4 || rowLog == 5 || rowLog == 6);
863*5ff13fbcSAllan Jude assert(ZSTD_isAligned(hashTable + relRow, 64)); /* prefetched hash row always 64-byte aligned */
864*5ff13fbcSAllan Jude assert(ZSTD_isAligned(tagTable + relRow, (size_t)1 << rowLog)); /* prefetched tagRow sits on correct multiple of bytes (32,64,128) */
865*5ff13fbcSAllan Jude }
866*5ff13fbcSAllan Jude
867*5ff13fbcSAllan Jude /* ZSTD_row_fillHashCache():
868*5ff13fbcSAllan Jude * Fill up the hash cache starting at idx, prefetching up to ZSTD_ROW_HASH_CACHE_SIZE entries,
869*5ff13fbcSAllan Jude * but not beyond iLimit.
870*5ff13fbcSAllan Jude */
ZSTD_row_fillHashCache(ZSTD_matchState_t * ms,const BYTE * base,U32 const rowLog,U32 const mls,U32 idx,const BYTE * const iLimit)871*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE void ZSTD_row_fillHashCache(ZSTD_matchState_t* ms, const BYTE* base,
872*5ff13fbcSAllan Jude U32 const rowLog, U32 const mls,
873*5ff13fbcSAllan Jude U32 idx, const BYTE* const iLimit)
874*5ff13fbcSAllan Jude {
875*5ff13fbcSAllan Jude U32 const* const hashTable = ms->hashTable;
876*5ff13fbcSAllan Jude U16 const* const tagTable = ms->tagTable;
877*5ff13fbcSAllan Jude U32 const hashLog = ms->rowHashLog;
878*5ff13fbcSAllan Jude U32 const maxElemsToPrefetch = (base + idx) > iLimit ? 0 : (U32)(iLimit - (base + idx) + 1);
879*5ff13fbcSAllan Jude U32 const lim = idx + MIN(ZSTD_ROW_HASH_CACHE_SIZE, maxElemsToPrefetch);
880*5ff13fbcSAllan Jude
881*5ff13fbcSAllan Jude for (; idx < lim; ++idx) {
882*5ff13fbcSAllan Jude U32 const hash = (U32)ZSTD_hashPtr(base + idx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
883*5ff13fbcSAllan Jude U32 const row = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
884*5ff13fbcSAllan Jude ZSTD_row_prefetch(hashTable, tagTable, row, rowLog);
885*5ff13fbcSAllan Jude ms->hashCache[idx & ZSTD_ROW_HASH_CACHE_MASK] = hash;
886*5ff13fbcSAllan Jude }
887*5ff13fbcSAllan Jude
888*5ff13fbcSAllan Jude DEBUGLOG(6, "ZSTD_row_fillHashCache(): [%u %u %u %u %u %u %u %u]", ms->hashCache[0], ms->hashCache[1],
889*5ff13fbcSAllan Jude ms->hashCache[2], ms->hashCache[3], ms->hashCache[4],
890*5ff13fbcSAllan Jude ms->hashCache[5], ms->hashCache[6], ms->hashCache[7]);
891*5ff13fbcSAllan Jude }
892*5ff13fbcSAllan Jude
893*5ff13fbcSAllan Jude /* ZSTD_row_nextCachedHash():
894*5ff13fbcSAllan Jude * Returns the hash of base + idx, and replaces the hash in the hash cache with the byte at
895*5ff13fbcSAllan Jude * base + idx + ZSTD_ROW_HASH_CACHE_SIZE. Also prefetches the appropriate rows from hashTable and tagTable.
896*5ff13fbcSAllan Jude */
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)897*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE U32 ZSTD_row_nextCachedHash(U32* cache, U32 const* hashTable,
898*5ff13fbcSAllan Jude U16 const* tagTable, BYTE const* base,
899*5ff13fbcSAllan Jude U32 idx, U32 const hashLog,
900*5ff13fbcSAllan Jude U32 const rowLog, U32 const mls)
901*5ff13fbcSAllan Jude {
902*5ff13fbcSAllan Jude U32 const newHash = (U32)ZSTD_hashPtr(base+idx+ZSTD_ROW_HASH_CACHE_SIZE, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
903*5ff13fbcSAllan Jude U32 const row = (newHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
904*5ff13fbcSAllan Jude ZSTD_row_prefetch(hashTable, tagTable, row, rowLog);
905*5ff13fbcSAllan Jude { U32 const hash = cache[idx & ZSTD_ROW_HASH_CACHE_MASK];
906*5ff13fbcSAllan Jude cache[idx & ZSTD_ROW_HASH_CACHE_MASK] = newHash;
907*5ff13fbcSAllan Jude return hash;
908*5ff13fbcSAllan Jude }
909*5ff13fbcSAllan Jude }
910*5ff13fbcSAllan Jude
911*5ff13fbcSAllan Jude /* ZSTD_row_update_internalImpl():
912*5ff13fbcSAllan Jude * Updates the hash table with positions starting from updateStartIdx until updateEndIdx.
913*5ff13fbcSAllan Jude */
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)914*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE void ZSTD_row_update_internalImpl(ZSTD_matchState_t* ms,
915*5ff13fbcSAllan Jude U32 updateStartIdx, U32 const updateEndIdx,
916*5ff13fbcSAllan Jude U32 const mls, U32 const rowLog,
917*5ff13fbcSAllan Jude U32 const rowMask, U32 const useCache)
918*5ff13fbcSAllan Jude {
919*5ff13fbcSAllan Jude U32* const hashTable = ms->hashTable;
920*5ff13fbcSAllan Jude U16* const tagTable = ms->tagTable;
921*5ff13fbcSAllan Jude U32 const hashLog = ms->rowHashLog;
922*5ff13fbcSAllan Jude const BYTE* const base = ms->window.base;
923*5ff13fbcSAllan Jude
924*5ff13fbcSAllan Jude DEBUGLOG(6, "ZSTD_row_update_internalImpl(): updateStartIdx=%u, updateEndIdx=%u", updateStartIdx, updateEndIdx);
925*5ff13fbcSAllan Jude for (; updateStartIdx < updateEndIdx; ++updateStartIdx) {
926*5ff13fbcSAllan Jude U32 const hash = useCache ? ZSTD_row_nextCachedHash(ms->hashCache, hashTable, tagTable, base, updateStartIdx, hashLog, rowLog, mls)
927*5ff13fbcSAllan Jude : (U32)ZSTD_hashPtr(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
928*5ff13fbcSAllan Jude U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
929*5ff13fbcSAllan Jude U32* const row = hashTable + relRow;
930*5ff13fbcSAllan Jude BYTE* tagRow = (BYTE*)(tagTable + relRow); /* Though tagTable is laid out as a table of U16, each tag is only 1 byte.
931*5ff13fbcSAllan Jude Explicit cast allows us to get exact desired position within each row */
932*5ff13fbcSAllan Jude U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask);
933*5ff13fbcSAllan Jude
934*5ff13fbcSAllan Jude assert(hash == ZSTD_hashPtr(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls));
935*5ff13fbcSAllan Jude ((BYTE*)tagRow)[pos + ZSTD_ROW_HASH_TAG_OFFSET] = hash & ZSTD_ROW_HASH_TAG_MASK;
936*5ff13fbcSAllan Jude row[pos] = updateStartIdx;
937*5ff13fbcSAllan Jude }
938*5ff13fbcSAllan Jude }
939*5ff13fbcSAllan Jude
940*5ff13fbcSAllan Jude /* ZSTD_row_update_internal():
941*5ff13fbcSAllan Jude * Inserts the byte at ip into the appropriate position in the hash table, and updates ms->nextToUpdate.
942*5ff13fbcSAllan Jude * Skips sections of long matches as is necessary.
943*5ff13fbcSAllan Jude */
ZSTD_row_update_internal(ZSTD_matchState_t * ms,const BYTE * ip,U32 const mls,U32 const rowLog,U32 const rowMask,U32 const useCache)944*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE void ZSTD_row_update_internal(ZSTD_matchState_t* ms, const BYTE* ip,
945*5ff13fbcSAllan Jude U32 const mls, U32 const rowLog,
946*5ff13fbcSAllan Jude U32 const rowMask, U32 const useCache)
947*5ff13fbcSAllan Jude {
948*5ff13fbcSAllan Jude U32 idx = ms->nextToUpdate;
949*5ff13fbcSAllan Jude const BYTE* const base = ms->window.base;
950*5ff13fbcSAllan Jude const U32 target = (U32)(ip - base);
951*5ff13fbcSAllan Jude const U32 kSkipThreshold = 384;
952*5ff13fbcSAllan Jude const U32 kMaxMatchStartPositionsToUpdate = 96;
953*5ff13fbcSAllan Jude const U32 kMaxMatchEndPositionsToUpdate = 32;
954*5ff13fbcSAllan Jude
955*5ff13fbcSAllan Jude if (useCache) {
956*5ff13fbcSAllan Jude /* Only skip positions when using hash cache, i.e.
957*5ff13fbcSAllan Jude * if we are loading a dict, don't skip anything.
958*5ff13fbcSAllan Jude * If we decide to skip, then we only update a set number
959*5ff13fbcSAllan Jude * of positions at the beginning and end of the match.
960*5ff13fbcSAllan Jude */
961*5ff13fbcSAllan Jude if (UNLIKELY(target - idx > kSkipThreshold)) {
962*5ff13fbcSAllan Jude U32 const bound = idx + kMaxMatchStartPositionsToUpdate;
963*5ff13fbcSAllan Jude ZSTD_row_update_internalImpl(ms, idx, bound, mls, rowLog, rowMask, useCache);
964*5ff13fbcSAllan Jude idx = target - kMaxMatchEndPositionsToUpdate;
965*5ff13fbcSAllan Jude ZSTD_row_fillHashCache(ms, base, rowLog, mls, idx, ip+1);
966*5ff13fbcSAllan Jude }
967*5ff13fbcSAllan Jude }
968*5ff13fbcSAllan Jude assert(target >= idx);
969*5ff13fbcSAllan Jude ZSTD_row_update_internalImpl(ms, idx, target, mls, rowLog, rowMask, useCache);
970*5ff13fbcSAllan Jude ms->nextToUpdate = target;
971*5ff13fbcSAllan Jude }
972*5ff13fbcSAllan Jude
973*5ff13fbcSAllan Jude /* ZSTD_row_update():
974*5ff13fbcSAllan Jude * External wrapper for ZSTD_row_update_internal(). Used for filling the hashtable during dictionary
975*5ff13fbcSAllan Jude * processing.
976*5ff13fbcSAllan Jude */
ZSTD_row_update(ZSTD_matchState_t * const ms,const BYTE * ip)977*5ff13fbcSAllan Jude void ZSTD_row_update(ZSTD_matchState_t* const ms, const BYTE* ip) {
978*5ff13fbcSAllan Jude const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6);
979*5ff13fbcSAllan Jude const U32 rowMask = (1u << rowLog) - 1;
980*5ff13fbcSAllan Jude const U32 mls = MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */);
981*5ff13fbcSAllan Jude
982*5ff13fbcSAllan Jude DEBUGLOG(5, "ZSTD_row_update(), rowLog=%u", rowLog);
983*5ff13fbcSAllan Jude ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 0 /* dont use cache */);
984*5ff13fbcSAllan Jude }
985*5ff13fbcSAllan Jude
986*5ff13fbcSAllan Jude #if defined(ZSTD_ARCH_X86_SSE2)
987*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE ZSTD_VecMask
ZSTD_row_getSSEMask(int nbChunks,const BYTE * const src,const BYTE tag,const U32 head)988*5ff13fbcSAllan Jude ZSTD_row_getSSEMask(int nbChunks, const BYTE* const src, const BYTE tag, const U32 head)
989*5ff13fbcSAllan Jude {
990*5ff13fbcSAllan Jude const __m128i comparisonMask = _mm_set1_epi8((char)tag);
991*5ff13fbcSAllan Jude int matches[4] = {0};
992*5ff13fbcSAllan Jude int i;
993*5ff13fbcSAllan Jude assert(nbChunks == 1 || nbChunks == 2 || nbChunks == 4);
994*5ff13fbcSAllan Jude for (i=0; i<nbChunks; i++) {
995*5ff13fbcSAllan Jude const __m128i chunk = _mm_loadu_si128((const __m128i*)(const void*)(src + 16*i));
996*5ff13fbcSAllan Jude const __m128i equalMask = _mm_cmpeq_epi8(chunk, comparisonMask);
997*5ff13fbcSAllan Jude matches[i] = _mm_movemask_epi8(equalMask);
998*5ff13fbcSAllan Jude }
999*5ff13fbcSAllan Jude if (nbChunks == 1) return ZSTD_rotateRight_U16((U16)matches[0], head);
1000*5ff13fbcSAllan Jude if (nbChunks == 2) return ZSTD_rotateRight_U32((U32)matches[1] << 16 | (U32)matches[0], head);
1001*5ff13fbcSAllan Jude assert(nbChunks == 4);
1002*5ff13fbcSAllan Jude return ZSTD_rotateRight_U64((U64)matches[3] << 48 | (U64)matches[2] << 32 | (U64)matches[1] << 16 | (U64)matches[0], head);
1003*5ff13fbcSAllan Jude }
1004*5ff13fbcSAllan Jude #endif
1005*5ff13fbcSAllan Jude
1006*5ff13fbcSAllan Jude /* Returns a ZSTD_VecMask (U32) that has the nth bit set to 1 if the newly-computed "tag" matches
1007*5ff13fbcSAllan Jude * the hash at the nth position in a row of the tagTable.
1008*5ff13fbcSAllan Jude * Each row is a circular buffer beginning at the value of "head". So we must rotate the "matches" bitfield
1009*5ff13fbcSAllan Jude * to match up with the actual layout of the entries within the hashTable */
1010*5ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE ZSTD_VecMask
ZSTD_row_getMatchMask(const BYTE * const tagRow,const BYTE tag,const U32 head,const U32 rowEntries)1011*5ff13fbcSAllan Jude ZSTD_row_getMatchMask(const BYTE* const tagRow, const BYTE tag, const U32 head, const U32 rowEntries)
1012*5ff13fbcSAllan Jude {
1013*5ff13fbcSAllan Jude const BYTE* const src = tagRow + ZSTD_ROW_HASH_TAG_OFFSET;
1014*5ff13fbcSAllan Jude assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64);
1015*5ff13fbcSAllan Jude assert(rowEntries <= ZSTD_ROW_HASH_MAX_ENTRIES);
1016*5ff13fbcSAllan Jude
1017*5ff13fbcSAllan Jude #if defined(ZSTD_ARCH_X86_SSE2)
1018*5ff13fbcSAllan Jude
1019*5ff13fbcSAllan Jude return ZSTD_row_getSSEMask(rowEntries / 16, src, tag, head);
1020*5ff13fbcSAllan Jude
1021*5ff13fbcSAllan Jude #else /* SW or NEON-LE */
1022*5ff13fbcSAllan Jude
1023*5ff13fbcSAllan Jude # if defined(ZSTD_ARCH_ARM_NEON)
1024*5ff13fbcSAllan Jude /* This NEON path only works for little endian - otherwise use SWAR below */
1025*5ff13fbcSAllan Jude if (MEM_isLittleEndian()) {
1026*5ff13fbcSAllan Jude if (rowEntries == 16) {
1027*5ff13fbcSAllan Jude const uint8x16_t chunk = vld1q_u8(src);
1028*5ff13fbcSAllan Jude const uint16x8_t equalMask = vreinterpretq_u16_u8(vceqq_u8(chunk, vdupq_n_u8(tag)));
1029*5ff13fbcSAllan Jude const uint16x8_t t0 = vshlq_n_u16(equalMask, 7);
1030*5ff13fbcSAllan Jude const uint32x4_t t1 = vreinterpretq_u32_u16(vsriq_n_u16(t0, t0, 14));
1031*5ff13fbcSAllan Jude const uint64x2_t t2 = vreinterpretq_u64_u32(vshrq_n_u32(t1, 14));
1032*5ff13fbcSAllan Jude const uint8x16_t t3 = vreinterpretq_u8_u64(vsraq_n_u64(t2, t2, 28));
1033*5ff13fbcSAllan Jude const U16 hi = (U16)vgetq_lane_u8(t3, 8);
1034*5ff13fbcSAllan Jude const U16 lo = (U16)vgetq_lane_u8(t3, 0);
1035*5ff13fbcSAllan Jude return ZSTD_rotateRight_U16((hi << 8) | lo, head);
1036*5ff13fbcSAllan Jude } else if (rowEntries == 32) {
1037*5ff13fbcSAllan Jude const uint16x8x2_t chunk = vld2q_u16((const U16*)(const void*)src);
1038*5ff13fbcSAllan Jude const uint8x16_t chunk0 = vreinterpretq_u8_u16(chunk.val[0]);
1039*5ff13fbcSAllan Jude const uint8x16_t chunk1 = vreinterpretq_u8_u16(chunk.val[1]);
1040*5ff13fbcSAllan Jude const uint8x16_t equalMask0 = vceqq_u8(chunk0, vdupq_n_u8(tag));
1041*5ff13fbcSAllan Jude const uint8x16_t equalMask1 = vceqq_u8(chunk1, vdupq_n_u8(tag));
1042*5ff13fbcSAllan Jude const int8x8_t pack0 = vqmovn_s16(vreinterpretq_s16_u8(equalMask0));
1043*5ff13fbcSAllan Jude const int8x8_t pack1 = vqmovn_s16(vreinterpretq_s16_u8(equalMask1));
1044*5ff13fbcSAllan Jude const uint8x8_t t0 = vreinterpret_u8_s8(pack0);
1045*5ff13fbcSAllan Jude const uint8x8_t t1 = vreinterpret_u8_s8(pack1);
1046*5ff13fbcSAllan Jude const uint8x8_t t2 = vsri_n_u8(t1, t0, 2);
1047*5ff13fbcSAllan Jude const uint8x8x2_t t3 = vuzp_u8(t2, t0);
1048*5ff13fbcSAllan Jude const uint8x8_t t4 = vsri_n_u8(t3.val[1], t3.val[0], 4);
1049*5ff13fbcSAllan Jude const U32 matches = vget_lane_u32(vreinterpret_u32_u8(t4), 0);
1050*5ff13fbcSAllan Jude return ZSTD_rotateRight_U32(matches, head);
1051*5ff13fbcSAllan Jude } else { /* rowEntries == 64 */
1052*5ff13fbcSAllan Jude const uint8x16x4_t chunk = vld4q_u8(src);
1053*5ff13fbcSAllan Jude const uint8x16_t dup = vdupq_n_u8(tag);
1054*5ff13fbcSAllan Jude const uint8x16_t cmp0 = vceqq_u8(chunk.val[0], dup);
1055*5ff13fbcSAllan Jude const uint8x16_t cmp1 = vceqq_u8(chunk.val[1], dup);
1056*5ff13fbcSAllan Jude const uint8x16_t cmp2 = vceqq_u8(chunk.val[2], dup);
1057*5ff13fbcSAllan Jude const uint8x16_t cmp3 = vceqq_u8(chunk.val[3], dup);
1058*5ff13fbcSAllan Jude
1059*5ff13fbcSAllan Jude const uint8x16_t t0 = vsriq_n_u8(cmp1, cmp0, 1);
1060*5ff13fbcSAllan Jude const uint8x16_t t1 = vsriq_n_u8(cmp3, cmp2, 1);
1061*5ff13fbcSAllan Jude const uint8x16_t t2 = vsriq_n_u8(t1, t0, 2);
1062*5ff13fbcSAllan Jude const uint8x16_t t3 = vsriq_n_u8(t2, t2, 4);
1063*5ff13fbcSAllan Jude const uint8x8_t t4 = vshrn_n_u16(vreinterpretq_u16_u8(t3), 4);
1064*5ff13fbcSAllan Jude const U64 matches = vget_lane_u64(vreinterpret_u64_u8(t4), 0);
1065*5ff13fbcSAllan Jude return ZSTD_rotateRight_U64(matches, head);
1066*5ff13fbcSAllan Jude }
1067*5ff13fbcSAllan Jude }
1068*5ff13fbcSAllan Jude # endif /* ZSTD_ARCH_ARM_NEON */
1069*5ff13fbcSAllan Jude /* SWAR */
1070*5ff13fbcSAllan Jude { const size_t chunkSize = sizeof(size_t);
1071*5ff13fbcSAllan Jude const size_t shiftAmount = ((chunkSize * 8) - chunkSize);
1072*5ff13fbcSAllan Jude const size_t xFF = ~((size_t)0);
1073*5ff13fbcSAllan Jude const size_t x01 = xFF / 0xFF;
1074*5ff13fbcSAllan Jude const size_t x80 = x01 << 7;
1075*5ff13fbcSAllan Jude const size_t splatChar = tag * x01;
1076*5ff13fbcSAllan Jude ZSTD_VecMask matches = 0;
1077*5ff13fbcSAllan Jude int i = rowEntries - chunkSize;
1078*5ff13fbcSAllan Jude assert((sizeof(size_t) == 4) || (sizeof(size_t) == 8));
1079*5ff13fbcSAllan Jude if (MEM_isLittleEndian()) { /* runtime check so have two loops */
1080*5ff13fbcSAllan Jude const size_t extractMagic = (xFF / 0x7F) >> chunkSize;
1081*5ff13fbcSAllan Jude do {
1082*5ff13fbcSAllan Jude size_t chunk = MEM_readST(&src[i]);
1083*5ff13fbcSAllan Jude chunk ^= splatChar;
1084*5ff13fbcSAllan Jude chunk = (((chunk | x80) - x01) | chunk) & x80;
1085*5ff13fbcSAllan Jude matches <<= chunkSize;
1086*5ff13fbcSAllan Jude matches |= (chunk * extractMagic) >> shiftAmount;
1087*5ff13fbcSAllan Jude i -= chunkSize;
1088*5ff13fbcSAllan Jude } while (i >= 0);
1089*5ff13fbcSAllan Jude } else { /* big endian: reverse bits during extraction */
1090*5ff13fbcSAllan Jude const size_t msb = xFF ^ (xFF >> 1);
1091*5ff13fbcSAllan Jude const size_t extractMagic = (msb / 0x1FF) | msb;
1092*5ff13fbcSAllan Jude do {
1093*5ff13fbcSAllan Jude size_t chunk = MEM_readST(&src[i]);
1094*5ff13fbcSAllan Jude chunk ^= splatChar;
1095*5ff13fbcSAllan Jude chunk = (((chunk | x80) - x01) | chunk) & x80;
1096*5ff13fbcSAllan Jude matches <<= chunkSize;
1097*5ff13fbcSAllan Jude matches |= ((chunk >> 7) * extractMagic) >> shiftAmount;
1098*5ff13fbcSAllan Jude i -= chunkSize;
1099*5ff13fbcSAllan Jude } while (i >= 0);
1100*5ff13fbcSAllan Jude }
1101*5ff13fbcSAllan Jude matches = ~matches;
1102*5ff13fbcSAllan Jude if (rowEntries == 16) {
1103*5ff13fbcSAllan Jude return ZSTD_rotateRight_U16((U16)matches, head);
1104*5ff13fbcSAllan Jude } else if (rowEntries == 32) {
1105*5ff13fbcSAllan Jude return ZSTD_rotateRight_U32((U32)matches, head);
1106*5ff13fbcSAllan Jude } else {
1107*5ff13fbcSAllan Jude return ZSTD_rotateRight_U64((U64)matches, head);
1108*5ff13fbcSAllan Jude }
1109*5ff13fbcSAllan Jude }
1110*5ff13fbcSAllan Jude #endif
1111*5ff13fbcSAllan Jude }
1112*5ff13fbcSAllan Jude
1113*5ff13fbcSAllan Jude /* The high-level approach of the SIMD row based match finder is as follows:
1114*5ff13fbcSAllan Jude * - Figure out where to insert the new entry:
1115*5ff13fbcSAllan Jude * - Generate a hash from a byte along with an additional 1-byte "short hash". The additional byte is our "tag"
1116*5ff13fbcSAllan Jude * - The hashTable is effectively split into groups or "rows" of 16 or 32 entries of U32, and the hash determines
1117*5ff13fbcSAllan Jude * which row to insert into.
1118*5ff13fbcSAllan Jude * - Determine the correct position within the row to insert the entry into. Each row of 16 or 32 can
1119*5ff13fbcSAllan Jude * be considered as a circular buffer with a "head" index that resides in the tagTable.
1120*5ff13fbcSAllan Jude * - Also insert the "tag" into the equivalent row and position in the tagTable.
1121*5ff13fbcSAllan Jude * - Note: The tagTable has 17 or 33 1-byte entries per row, due to 16 or 32 tags, and 1 "head" entry.
1122*5ff13fbcSAllan Jude * The 17 or 33 entry rows are spaced out to occur every 32 or 64 bytes, respectively,
1123*5ff13fbcSAllan Jude * for alignment/performance reasons, leaving some bytes unused.
1124*5ff13fbcSAllan Jude * - Use SIMD to efficiently compare the tags in the tagTable to the 1-byte "short hash" and
1125*5ff13fbcSAllan Jude * generate a bitfield that we can cycle through to check the collisions in the hash table.
1126*5ff13fbcSAllan Jude * - Pick the longest match.
1127*5ff13fbcSAllan Jude */
1128*5ff13fbcSAllan Jude 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)1129*5ff13fbcSAllan Jude size_t ZSTD_RowFindBestMatch(
11300f743729SConrad Meyer ZSTD_matchState_t* ms,
1131*5ff13fbcSAllan Jude const BYTE* const ip, const BYTE* const iLimit,
1132*5ff13fbcSAllan Jude size_t* offsetPtr,
1133*5ff13fbcSAllan Jude const U32 mls, const ZSTD_dictMode_e dictMode,
1134*5ff13fbcSAllan Jude const U32 rowLog)
11350c16b537SWarner Losh {
1136*5ff13fbcSAllan Jude U32* const hashTable = ms->hashTable;
1137*5ff13fbcSAllan Jude U16* const tagTable = ms->tagTable;
1138*5ff13fbcSAllan Jude U32* const hashCache = ms->hashCache;
1139*5ff13fbcSAllan Jude const U32 hashLog = ms->rowHashLog;
1140*5ff13fbcSAllan Jude const ZSTD_compressionParameters* const cParams = &ms->cParams;
1141*5ff13fbcSAllan Jude const BYTE* const base = ms->window.base;
1142*5ff13fbcSAllan Jude const BYTE* const dictBase = ms->window.dictBase;
1143*5ff13fbcSAllan Jude const U32 dictLimit = ms->window.dictLimit;
1144*5ff13fbcSAllan Jude const BYTE* const prefixStart = base + dictLimit;
1145*5ff13fbcSAllan Jude const BYTE* const dictEnd = dictBase + dictLimit;
1146*5ff13fbcSAllan Jude const U32 curr = (U32)(ip-base);
1147*5ff13fbcSAllan Jude const U32 maxDistance = 1U << cParams->windowLog;
1148*5ff13fbcSAllan Jude const U32 lowestValid = ms->window.lowLimit;
1149*5ff13fbcSAllan Jude const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
1150*5ff13fbcSAllan Jude const U32 isDictionary = (ms->loadedDictEnd != 0);
1151*5ff13fbcSAllan Jude const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance;
1152*5ff13fbcSAllan Jude const U32 rowEntries = (1U << rowLog);
1153*5ff13fbcSAllan Jude const U32 rowMask = rowEntries - 1;
1154*5ff13fbcSAllan Jude const U32 cappedSearchLog = MIN(cParams->searchLog, rowLog); /* nb of searches is capped at nb entries per row */
1155*5ff13fbcSAllan Jude U32 nbAttempts = 1U << cappedSearchLog;
1156*5ff13fbcSAllan Jude size_t ml=4-1;
1157*5ff13fbcSAllan Jude
1158*5ff13fbcSAllan Jude /* DMS/DDS variables that may be referenced laster */
1159*5ff13fbcSAllan Jude const ZSTD_matchState_t* const dms = ms->dictMatchState;
1160*5ff13fbcSAllan Jude
1161*5ff13fbcSAllan Jude /* Initialize the following variables to satisfy static analyzer */
1162*5ff13fbcSAllan Jude size_t ddsIdx = 0;
1163*5ff13fbcSAllan Jude U32 ddsExtraAttempts = 0; /* cctx hash tables are limited in searches, but allow extra searches into DDS */
1164*5ff13fbcSAllan Jude U32 dmsTag = 0;
1165*5ff13fbcSAllan Jude U32* dmsRow = NULL;
1166*5ff13fbcSAllan Jude BYTE* dmsTagRow = NULL;
1167*5ff13fbcSAllan Jude
1168*5ff13fbcSAllan Jude if (dictMode == ZSTD_dedicatedDictSearch) {
1169*5ff13fbcSAllan Jude const U32 ddsHashLog = dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG;
1170*5ff13fbcSAllan Jude { /* Prefetch DDS hashtable entry */
1171*5ff13fbcSAllan Jude ddsIdx = ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG;
1172*5ff13fbcSAllan Jude PREFETCH_L1(&dms->hashTable[ddsIdx]);
1173*5ff13fbcSAllan Jude }
1174*5ff13fbcSAllan Jude ddsExtraAttempts = cParams->searchLog > rowLog ? 1U << (cParams->searchLog - rowLog) : 0;
1175*5ff13fbcSAllan Jude }
1176*5ff13fbcSAllan Jude
1177*5ff13fbcSAllan Jude if (dictMode == ZSTD_dictMatchState) {
1178*5ff13fbcSAllan Jude /* Prefetch DMS rows */
1179*5ff13fbcSAllan Jude U32* const dmsHashTable = dms->hashTable;
1180*5ff13fbcSAllan Jude U16* const dmsTagTable = dms->tagTable;
1181*5ff13fbcSAllan Jude U32 const dmsHash = (U32)ZSTD_hashPtr(ip, dms->rowHashLog + ZSTD_ROW_HASH_TAG_BITS, mls);
1182*5ff13fbcSAllan Jude U32 const dmsRelRow = (dmsHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
1183*5ff13fbcSAllan Jude dmsTag = dmsHash & ZSTD_ROW_HASH_TAG_MASK;
1184*5ff13fbcSAllan Jude dmsTagRow = (BYTE*)(dmsTagTable + dmsRelRow);
1185*5ff13fbcSAllan Jude dmsRow = dmsHashTable + dmsRelRow;
1186*5ff13fbcSAllan Jude ZSTD_row_prefetch(dmsHashTable, dmsTagTable, dmsRelRow, rowLog);
1187*5ff13fbcSAllan Jude }
1188*5ff13fbcSAllan Jude
1189*5ff13fbcSAllan Jude /* Update the hashTable and tagTable up to (but not including) ip */
1190*5ff13fbcSAllan Jude ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 1 /* useCache */);
1191*5ff13fbcSAllan Jude { /* Get the hash for ip, compute the appropriate row */
1192*5ff13fbcSAllan Jude U32 const hash = ZSTD_row_nextCachedHash(hashCache, hashTable, tagTable, base, curr, hashLog, rowLog, mls);
1193*5ff13fbcSAllan Jude U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog;
1194*5ff13fbcSAllan Jude U32 const tag = hash & ZSTD_ROW_HASH_TAG_MASK;
1195*5ff13fbcSAllan Jude U32* const row = hashTable + relRow;
1196*5ff13fbcSAllan Jude BYTE* tagRow = (BYTE*)(tagTable + relRow);
1197*5ff13fbcSAllan Jude U32 const head = *tagRow & rowMask;
1198*5ff13fbcSAllan Jude U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES];
1199*5ff13fbcSAllan Jude size_t numMatches = 0;
1200*5ff13fbcSAllan Jude size_t currMatch = 0;
1201*5ff13fbcSAllan Jude ZSTD_VecMask matches = ZSTD_row_getMatchMask(tagRow, (BYTE)tag, head, rowEntries);
1202*5ff13fbcSAllan Jude
1203*5ff13fbcSAllan Jude /* Cycle through the matches and prefetch */
1204*5ff13fbcSAllan Jude for (; (matches > 0) && (nbAttempts > 0); --nbAttempts, matches &= (matches - 1)) {
1205*5ff13fbcSAllan Jude U32 const matchPos = (head + ZSTD_VecMask_next(matches)) & rowMask;
1206*5ff13fbcSAllan Jude U32 const matchIndex = row[matchPos];
1207*5ff13fbcSAllan Jude assert(numMatches < rowEntries);
1208*5ff13fbcSAllan Jude if (matchIndex < lowLimit)
1209*5ff13fbcSAllan Jude break;
1210*5ff13fbcSAllan Jude if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
1211*5ff13fbcSAllan Jude PREFETCH_L1(base + matchIndex);
1212*5ff13fbcSAllan Jude } else {
1213*5ff13fbcSAllan Jude PREFETCH_L1(dictBase + matchIndex);
1214*5ff13fbcSAllan Jude }
1215*5ff13fbcSAllan Jude matchBuffer[numMatches++] = matchIndex;
1216*5ff13fbcSAllan Jude }
1217*5ff13fbcSAllan Jude
1218*5ff13fbcSAllan Jude /* Speed opt: insert current byte into hashtable too. This allows us to avoid one iteration of the loop
1219*5ff13fbcSAllan Jude in ZSTD_row_update_internal() at the next search. */
12200c16b537SWarner Losh {
1221*5ff13fbcSAllan Jude U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask);
1222*5ff13fbcSAllan Jude tagRow[pos + ZSTD_ROW_HASH_TAG_OFFSET] = (BYTE)tag;
1223*5ff13fbcSAllan Jude row[pos] = ms->nextToUpdate++;
1224*5ff13fbcSAllan Jude }
1225*5ff13fbcSAllan Jude
1226*5ff13fbcSAllan Jude /* Return the longest match */
1227*5ff13fbcSAllan Jude for (; currMatch < numMatches; ++currMatch) {
1228*5ff13fbcSAllan Jude U32 const matchIndex = matchBuffer[currMatch];
1229*5ff13fbcSAllan Jude size_t currentMl=0;
1230*5ff13fbcSAllan Jude assert(matchIndex < curr);
1231*5ff13fbcSAllan Jude assert(matchIndex >= lowLimit);
1232*5ff13fbcSAllan Jude
1233*5ff13fbcSAllan Jude if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
1234*5ff13fbcSAllan Jude const BYTE* const match = base + matchIndex;
1235*5ff13fbcSAllan Jude assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */
1236*5ff13fbcSAllan Jude if (match[ml] == ip[ml]) /* potentially better */
1237*5ff13fbcSAllan Jude currentMl = ZSTD_count(ip, match, iLimit);
1238*5ff13fbcSAllan Jude } else {
1239*5ff13fbcSAllan Jude const BYTE* const match = dictBase + matchIndex;
1240*5ff13fbcSAllan Jude assert(match+4 <= dictEnd);
1241*5ff13fbcSAllan Jude if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
1242*5ff13fbcSAllan Jude currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4;
1243*5ff13fbcSAllan Jude }
1244*5ff13fbcSAllan Jude
1245*5ff13fbcSAllan Jude /* Save best solution */
1246*5ff13fbcSAllan Jude if (currentMl > ml) {
1247*5ff13fbcSAllan Jude ml = currentMl;
1248*5ff13fbcSAllan Jude *offsetPtr = STORE_OFFSET(curr - matchIndex);
1249*5ff13fbcSAllan Jude if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
1250*5ff13fbcSAllan Jude }
12510f743729SConrad Meyer }
12520f743729SConrad Meyer }
12530f743729SConrad Meyer
1254*5ff13fbcSAllan Jude assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */
1255*5ff13fbcSAllan Jude if (dictMode == ZSTD_dedicatedDictSearch) {
1256*5ff13fbcSAllan Jude ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts + ddsExtraAttempts, dms,
1257*5ff13fbcSAllan Jude ip, iLimit, prefixStart, curr, dictLimit, ddsIdx);
1258*5ff13fbcSAllan Jude } else if (dictMode == ZSTD_dictMatchState) {
1259*5ff13fbcSAllan Jude /* TODO: Measure and potentially add prefetching to DMS */
1260*5ff13fbcSAllan Jude const U32 dmsLowestIndex = dms->window.dictLimit;
1261*5ff13fbcSAllan Jude const BYTE* const dmsBase = dms->window.base;
1262*5ff13fbcSAllan Jude const BYTE* const dmsEnd = dms->window.nextSrc;
1263*5ff13fbcSAllan Jude const U32 dmsSize = (U32)(dmsEnd - dmsBase);
1264*5ff13fbcSAllan Jude const U32 dmsIndexDelta = dictLimit - dmsSize;
12650f743729SConrad Meyer
1266*5ff13fbcSAllan Jude { U32 const head = *dmsTagRow & rowMask;
1267*5ff13fbcSAllan Jude U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES];
1268*5ff13fbcSAllan Jude size_t numMatches = 0;
1269*5ff13fbcSAllan Jude size_t currMatch = 0;
1270*5ff13fbcSAllan Jude ZSTD_VecMask matches = ZSTD_row_getMatchMask(dmsTagRow, (BYTE)dmsTag, head, rowEntries);
1271*5ff13fbcSAllan Jude
1272*5ff13fbcSAllan Jude for (; (matches > 0) && (nbAttempts > 0); --nbAttempts, matches &= (matches - 1)) {
1273*5ff13fbcSAllan Jude U32 const matchPos = (head + ZSTD_VecMask_next(matches)) & rowMask;
1274*5ff13fbcSAllan Jude U32 const matchIndex = dmsRow[matchPos];
1275*5ff13fbcSAllan Jude if (matchIndex < dmsLowestIndex)
1276*5ff13fbcSAllan Jude break;
1277*5ff13fbcSAllan Jude PREFETCH_L1(dmsBase + matchIndex);
1278*5ff13fbcSAllan Jude matchBuffer[numMatches++] = matchIndex;
1279*5ff13fbcSAllan Jude }
1280*5ff13fbcSAllan Jude
1281*5ff13fbcSAllan Jude /* Return the longest match */
1282*5ff13fbcSAllan Jude for (; currMatch < numMatches; ++currMatch) {
1283*5ff13fbcSAllan Jude U32 const matchIndex = matchBuffer[currMatch];
1284*5ff13fbcSAllan Jude size_t currentMl=0;
1285*5ff13fbcSAllan Jude assert(matchIndex >= dmsLowestIndex);
1286*5ff13fbcSAllan Jude assert(matchIndex < curr);
1287*5ff13fbcSAllan Jude
1288*5ff13fbcSAllan Jude { const BYTE* const match = dmsBase + matchIndex;
1289*5ff13fbcSAllan Jude assert(match+4 <= dmsEnd);
1290*5ff13fbcSAllan Jude if (MEM_read32(match) == MEM_read32(ip))
1291*5ff13fbcSAllan Jude currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4;
1292*5ff13fbcSAllan Jude }
1293*5ff13fbcSAllan Jude
1294*5ff13fbcSAllan Jude if (currentMl > ml) {
1295*5ff13fbcSAllan Jude ml = currentMl;
1296*5ff13fbcSAllan Jude assert(curr > matchIndex + dmsIndexDelta);
1297*5ff13fbcSAllan Jude *offsetPtr = STORE_OFFSET(curr - (matchIndex + dmsIndexDelta));
1298*5ff13fbcSAllan Jude if (ip+currentMl == iLimit) break;
1299*5ff13fbcSAllan Jude }
1300*5ff13fbcSAllan Jude }
1301*5ff13fbcSAllan Jude }
1302*5ff13fbcSAllan Jude }
1303*5ff13fbcSAllan Jude return ml;
1304*5ff13fbcSAllan Jude }
1305*5ff13fbcSAllan Jude
1306*5ff13fbcSAllan Jude
1307*5ff13fbcSAllan Jude typedef size_t (*searchMax_f)(
13080f743729SConrad Meyer ZSTD_matchState_t* ms,
1309*5ff13fbcSAllan Jude const BYTE* ip, const BYTE* iLimit, size_t* offsetPtr);
1310*5ff13fbcSAllan Jude
1311*5ff13fbcSAllan Jude /**
1312*5ff13fbcSAllan Jude * This struct contains the functions necessary for lazy to search.
1313*5ff13fbcSAllan Jude * Currently, that is only searchMax. However, it is still valuable to have the
1314*5ff13fbcSAllan Jude * VTable because this makes it easier to add more functions to the VTable later.
1315*5ff13fbcSAllan Jude *
1316*5ff13fbcSAllan Jude * TODO: The start of the search function involves loading and calculating a
1317*5ff13fbcSAllan Jude * bunch of constants from the ZSTD_matchState_t. These computations could be
1318*5ff13fbcSAllan Jude * done in an initialization function, and saved somewhere in the match state.
1319*5ff13fbcSAllan Jude * Then we could pass a pointer to the saved state instead of the match state,
1320*5ff13fbcSAllan Jude * and avoid duplicate computations.
1321*5ff13fbcSAllan Jude *
1322*5ff13fbcSAllan Jude * TODO: Move the match re-winding into searchMax. This improves compression
1323*5ff13fbcSAllan Jude * ratio, and unlocks further simplifications with the next TODO.
1324*5ff13fbcSAllan Jude *
1325*5ff13fbcSAllan Jude * TODO: Try moving the repcode search into searchMax. After the re-winding
1326*5ff13fbcSAllan Jude * and repcode search are in searchMax, there is no more logic in the match
1327*5ff13fbcSAllan Jude * finder loop that requires knowledge about the dictMode. So we should be
1328*5ff13fbcSAllan Jude * able to avoid force inlining it, and we can join the extDict loop with
1329*5ff13fbcSAllan Jude * the single segment loop. It should go in searchMax instead of its own
1330*5ff13fbcSAllan Jude * function to avoid having multiple virtual function calls per search.
1331*5ff13fbcSAllan Jude */
1332*5ff13fbcSAllan Jude typedef struct {
1333*5ff13fbcSAllan Jude searchMax_f searchMax;
1334*5ff13fbcSAllan Jude } ZSTD_LazyVTable;
1335*5ff13fbcSAllan Jude
1336*5ff13fbcSAllan Jude #define GEN_ZSTD_BT_VTABLE(dictMode, mls) \
1337*5ff13fbcSAllan Jude static size_t ZSTD_BtFindBestMatch_##dictMode##_##mls( \
1338*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, \
1339*5ff13fbcSAllan Jude const BYTE* ip, const BYTE* const iLimit, \
1340*5ff13fbcSAllan Jude size_t* offsetPtr) \
1341*5ff13fbcSAllan Jude { \
1342*5ff13fbcSAllan Jude assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
1343*5ff13fbcSAllan Jude return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode); \
1344*5ff13fbcSAllan Jude } \
1345*5ff13fbcSAllan Jude static const ZSTD_LazyVTable ZSTD_BtVTable_##dictMode##_##mls = { \
1346*5ff13fbcSAllan Jude ZSTD_BtFindBestMatch_##dictMode##_##mls \
1347*5ff13fbcSAllan Jude };
1348*5ff13fbcSAllan Jude
1349*5ff13fbcSAllan Jude #define GEN_ZSTD_HC_VTABLE(dictMode, mls) \
1350*5ff13fbcSAllan Jude static size_t ZSTD_HcFindBestMatch_##dictMode##_##mls( \
1351*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, \
1352*5ff13fbcSAllan Jude const BYTE* ip, const BYTE* const iLimit, \
1353*5ff13fbcSAllan Jude size_t* offsetPtr) \
1354*5ff13fbcSAllan Jude { \
1355*5ff13fbcSAllan Jude assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
1356*5ff13fbcSAllan Jude return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode); \
1357*5ff13fbcSAllan Jude } \
1358*5ff13fbcSAllan Jude static const ZSTD_LazyVTable ZSTD_HcVTable_##dictMode##_##mls = { \
1359*5ff13fbcSAllan Jude ZSTD_HcFindBestMatch_##dictMode##_##mls \
1360*5ff13fbcSAllan Jude };
1361*5ff13fbcSAllan Jude
1362*5ff13fbcSAllan Jude #define GEN_ZSTD_ROW_VTABLE(dictMode, mls, rowLog) \
1363*5ff13fbcSAllan Jude static size_t ZSTD_RowFindBestMatch_##dictMode##_##mls##_##rowLog( \
1364*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, \
1365*5ff13fbcSAllan Jude const BYTE* ip, const BYTE* const iLimit, \
1366*5ff13fbcSAllan Jude size_t* offsetPtr) \
1367*5ff13fbcSAllan Jude { \
1368*5ff13fbcSAllan Jude assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \
1369*5ff13fbcSAllan Jude assert(MAX(4, MIN(6, ms->cParams.searchLog)) == rowLog); \
1370*5ff13fbcSAllan Jude return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode, rowLog); \
1371*5ff13fbcSAllan Jude } \
1372*5ff13fbcSAllan Jude static const ZSTD_LazyVTable ZSTD_RowVTable_##dictMode##_##mls##_##rowLog = { \
1373*5ff13fbcSAllan Jude ZSTD_RowFindBestMatch_##dictMode##_##mls##_##rowLog \
1374*5ff13fbcSAllan Jude };
1375*5ff13fbcSAllan Jude
1376*5ff13fbcSAllan Jude #define ZSTD_FOR_EACH_ROWLOG(X, dictMode, mls) \
1377*5ff13fbcSAllan Jude X(dictMode, mls, 4) \
1378*5ff13fbcSAllan Jude X(dictMode, mls, 5) \
1379*5ff13fbcSAllan Jude X(dictMode, mls, 6)
1380*5ff13fbcSAllan Jude
1381*5ff13fbcSAllan Jude #define ZSTD_FOR_EACH_MLS_ROWLOG(X, dictMode) \
1382*5ff13fbcSAllan Jude ZSTD_FOR_EACH_ROWLOG(X, dictMode, 4) \
1383*5ff13fbcSAllan Jude ZSTD_FOR_EACH_ROWLOG(X, dictMode, 5) \
1384*5ff13fbcSAllan Jude ZSTD_FOR_EACH_ROWLOG(X, dictMode, 6)
1385*5ff13fbcSAllan Jude
1386*5ff13fbcSAllan Jude #define ZSTD_FOR_EACH_MLS(X, dictMode) \
1387*5ff13fbcSAllan Jude X(dictMode, 4) \
1388*5ff13fbcSAllan Jude X(dictMode, 5) \
1389*5ff13fbcSAllan Jude X(dictMode, 6)
1390*5ff13fbcSAllan Jude
1391*5ff13fbcSAllan Jude #define ZSTD_FOR_EACH_DICT_MODE(X, ...) \
1392*5ff13fbcSAllan Jude X(__VA_ARGS__, noDict) \
1393*5ff13fbcSAllan Jude X(__VA_ARGS__, extDict) \
1394*5ff13fbcSAllan Jude X(__VA_ARGS__, dictMatchState) \
1395*5ff13fbcSAllan Jude X(__VA_ARGS__, dedicatedDictSearch)
1396*5ff13fbcSAllan Jude
1397*5ff13fbcSAllan Jude /* Generate Row VTables for each combination of (dictMode, mls, rowLog) */
1398*5ff13fbcSAllan Jude ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS_ROWLOG, GEN_ZSTD_ROW_VTABLE)
1399*5ff13fbcSAllan Jude /* Generate Binary Tree VTables for each combination of (dictMode, mls) */
1400*5ff13fbcSAllan Jude ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_BT_VTABLE)
1401*5ff13fbcSAllan Jude /* Generate Hash Chain VTables for each combination of (dictMode, mls) */
1402*5ff13fbcSAllan Jude ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_HC_VTABLE)
1403*5ff13fbcSAllan Jude
1404*5ff13fbcSAllan Jude #define GEN_ZSTD_BT_VTABLE_ARRAY(dictMode) \
1405*5ff13fbcSAllan Jude { \
1406*5ff13fbcSAllan Jude &ZSTD_BtVTable_##dictMode##_4, \
1407*5ff13fbcSAllan Jude &ZSTD_BtVTable_##dictMode##_5, \
1408*5ff13fbcSAllan Jude &ZSTD_BtVTable_##dictMode##_6 \
14090c16b537SWarner Losh }
14100c16b537SWarner Losh
1411*5ff13fbcSAllan Jude #define GEN_ZSTD_HC_VTABLE_ARRAY(dictMode) \
1412*5ff13fbcSAllan Jude { \
1413*5ff13fbcSAllan Jude &ZSTD_HcVTable_##dictMode##_4, \
1414*5ff13fbcSAllan Jude &ZSTD_HcVTable_##dictMode##_5, \
1415*5ff13fbcSAllan Jude &ZSTD_HcVTable_##dictMode##_6 \
1416f7cd7fe5SConrad Meyer }
1417f7cd7fe5SConrad Meyer
1418*5ff13fbcSAllan Jude #define GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, mls) \
1419*5ff13fbcSAllan Jude { \
1420*5ff13fbcSAllan Jude &ZSTD_RowVTable_##dictMode##_##mls##_4, \
1421*5ff13fbcSAllan Jude &ZSTD_RowVTable_##dictMode##_##mls##_5, \
1422*5ff13fbcSAllan Jude &ZSTD_RowVTable_##dictMode##_##mls##_6 \
14230c16b537SWarner Losh }
14240c16b537SWarner Losh
1425*5ff13fbcSAllan Jude #define GEN_ZSTD_ROW_VTABLE_ARRAY(dictMode) \
1426*5ff13fbcSAllan Jude { \
1427*5ff13fbcSAllan Jude GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, 4), \
1428*5ff13fbcSAllan Jude GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, 5), \
1429*5ff13fbcSAllan Jude GEN_ZSTD_ROW_VTABLE_ARRAY_(dictMode, 6) \
1430*5ff13fbcSAllan Jude }
1431*5ff13fbcSAllan Jude
1432*5ff13fbcSAllan Jude #define GEN_ZSTD_VTABLE_ARRAY(X) \
1433*5ff13fbcSAllan Jude { \
1434*5ff13fbcSAllan Jude X(noDict), \
1435*5ff13fbcSAllan Jude X(extDict), \
1436*5ff13fbcSAllan Jude X(dictMatchState), \
1437*5ff13fbcSAllan Jude X(dedicatedDictSearch) \
1438*5ff13fbcSAllan Jude }
14390c16b537SWarner Losh
14400c16b537SWarner Losh /* *******************************
14410c16b537SWarner Losh * Common parser - lazy strategy
14420c16b537SWarner Losh *********************************/
1443*5ff13fbcSAllan Jude typedef enum { search_hashChain=0, search_binaryTree=1, search_rowHash=2 } searchMethod_e;
1444*5ff13fbcSAllan Jude
1445*5ff13fbcSAllan Jude /**
1446*5ff13fbcSAllan Jude * This table is indexed first by the four ZSTD_dictMode_e values, and then
1447*5ff13fbcSAllan Jude * by the two searchMethod_e values. NULLs are placed for configurations
1448*5ff13fbcSAllan Jude * that should never occur (extDict modes go to the other implementation
1449*5ff13fbcSAllan Jude * below and there is no DDSS for binary tree search yet).
1450*5ff13fbcSAllan Jude */
1451*5ff13fbcSAllan Jude
1452*5ff13fbcSAllan Jude static ZSTD_LazyVTable const*
ZSTD_selectLazyVTable(ZSTD_matchState_t const * ms,searchMethod_e searchMethod,ZSTD_dictMode_e dictMode)1453*5ff13fbcSAllan Jude ZSTD_selectLazyVTable(ZSTD_matchState_t const* ms, searchMethod_e searchMethod, ZSTD_dictMode_e dictMode)
1454*5ff13fbcSAllan Jude {
1455*5ff13fbcSAllan Jude /* Fill the Hc/Bt VTable arrays with the right functions for the (dictMode, mls) combination. */
1456*5ff13fbcSAllan Jude ZSTD_LazyVTable const* const hcVTables[4][3] = GEN_ZSTD_VTABLE_ARRAY(GEN_ZSTD_HC_VTABLE_ARRAY);
1457*5ff13fbcSAllan Jude ZSTD_LazyVTable const* const btVTables[4][3] = GEN_ZSTD_VTABLE_ARRAY(GEN_ZSTD_BT_VTABLE_ARRAY);
1458*5ff13fbcSAllan Jude /* Fill the Row VTable array with the right functions for the (dictMode, mls, rowLog) combination. */
1459*5ff13fbcSAllan Jude ZSTD_LazyVTable const* const rowVTables[4][3][3] = GEN_ZSTD_VTABLE_ARRAY(GEN_ZSTD_ROW_VTABLE_ARRAY);
1460*5ff13fbcSAllan Jude
1461*5ff13fbcSAllan Jude U32 const mls = MAX(4, MIN(6, ms->cParams.minMatch));
1462*5ff13fbcSAllan Jude U32 const rowLog = MAX(4, MIN(6, ms->cParams.searchLog));
1463*5ff13fbcSAllan Jude switch (searchMethod) {
1464*5ff13fbcSAllan Jude case search_hashChain:
1465*5ff13fbcSAllan Jude return hcVTables[dictMode][mls - 4];
1466*5ff13fbcSAllan Jude case search_binaryTree:
1467*5ff13fbcSAllan Jude return btVTables[dictMode][mls - 4];
1468*5ff13fbcSAllan Jude case search_rowHash:
1469*5ff13fbcSAllan Jude return rowVTables[dictMode][mls - 4][rowLog - 4];
1470*5ff13fbcSAllan Jude default:
1471*5ff13fbcSAllan Jude return NULL;
1472*5ff13fbcSAllan Jude }
1473*5ff13fbcSAllan Jude }
14749cbefe25SConrad Meyer
14759cbefe25SConrad Meyer 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)14769cbefe25SConrad Meyer ZSTD_compressBlock_lazy_generic(
147719fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore,
147819fcbaf1SConrad Meyer U32 rep[ZSTD_REP_NUM],
14790c16b537SWarner Losh const void* src, size_t srcSize,
14809cbefe25SConrad Meyer const searchMethod_e searchMethod, const U32 depth,
14810f743729SConrad Meyer ZSTD_dictMode_e const dictMode)
14820c16b537SWarner Losh {
14830c16b537SWarner Losh const BYTE* const istart = (const BYTE*)src;
14840c16b537SWarner Losh const BYTE* ip = istart;
14850c16b537SWarner Losh const BYTE* anchor = istart;
14860c16b537SWarner Losh const BYTE* const iend = istart + srcSize;
1487*5ff13fbcSAllan Jude const BYTE* const ilimit = (searchMethod == search_rowHash) ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8;
14880f743729SConrad Meyer const BYTE* const base = ms->window.base;
14890f743729SConrad Meyer const U32 prefixLowestIndex = ms->window.dictLimit;
14900f743729SConrad Meyer const BYTE* const prefixLowest = base + prefixLowestIndex;
14910c16b537SWarner Losh
1492*5ff13fbcSAllan Jude searchMax_f const searchMax = ZSTD_selectLazyVTable(ms, searchMethod, dictMode)->searchMax;
149319fcbaf1SConrad Meyer U32 offset_1 = rep[0], offset_2 = rep[1], savedOffset=0;
14940c16b537SWarner Losh
1495f7cd7fe5SConrad Meyer const int isDMS = dictMode == ZSTD_dictMatchState;
1496f7cd7fe5SConrad Meyer const int isDDS = dictMode == ZSTD_dedicatedDictSearch;
1497f7cd7fe5SConrad Meyer const int isDxS = isDMS || isDDS;
14980f743729SConrad Meyer const ZSTD_matchState_t* const dms = ms->dictMatchState;
1499f7cd7fe5SConrad Meyer const U32 dictLowestIndex = isDxS ? dms->window.dictLimit : 0;
1500f7cd7fe5SConrad Meyer const BYTE* const dictBase = isDxS ? dms->window.base : NULL;
1501f7cd7fe5SConrad Meyer const BYTE* const dictLowest = isDxS ? dictBase + dictLowestIndex : NULL;
1502f7cd7fe5SConrad Meyer const BYTE* const dictEnd = isDxS ? dms->window.nextSrc : NULL;
1503f7cd7fe5SConrad Meyer const U32 dictIndexDelta = isDxS ?
15040f743729SConrad Meyer prefixLowestIndex - (U32)(dictEnd - dictBase) :
15050f743729SConrad Meyer 0;
150637f1f268SConrad Meyer const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictLowest));
150737f1f268SConrad Meyer
1508f7cd7fe5SConrad Meyer assert(searchMax != NULL);
1509f7cd7fe5SConrad Meyer
1510*5ff13fbcSAllan Jude DEBUGLOG(5, "ZSTD_compressBlock_lazy_generic (dictMode=%u) (searchFunc=%u)", (U32)dictMode, (U32)searchMethod);
15110f743729SConrad Meyer ip += (dictAndPrefixLength == 0);
15120f743729SConrad Meyer if (dictMode == ZSTD_noDict) {
1513f7cd7fe5SConrad Meyer U32 const curr = (U32)(ip - base);
1514f7cd7fe5SConrad Meyer U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, ms->cParams.windowLog);
1515f7cd7fe5SConrad Meyer U32 const maxRep = curr - windowLow;
15160c16b537SWarner Losh if (offset_2 > maxRep) savedOffset = offset_2, offset_2 = 0;
15170c16b537SWarner Losh if (offset_1 > maxRep) savedOffset = offset_1, offset_1 = 0;
15180c16b537SWarner Losh }
1519f7cd7fe5SConrad Meyer if (isDxS) {
15200f743729SConrad Meyer /* dictMatchState repCode checks don't currently handle repCode == 0
15210f743729SConrad Meyer * disabling. */
15220f743729SConrad Meyer assert(offset_1 <= dictAndPrefixLength);
15230f743729SConrad Meyer assert(offset_2 <= dictAndPrefixLength);
15240f743729SConrad Meyer }
15250c16b537SWarner Losh
1526*5ff13fbcSAllan Jude if (searchMethod == search_rowHash) {
1527*5ff13fbcSAllan Jude const U32 rowLog = MAX(4, MIN(6, ms->cParams.searchLog));
1528*5ff13fbcSAllan Jude ZSTD_row_fillHashCache(ms, base, rowLog,
1529*5ff13fbcSAllan Jude MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */),
1530*5ff13fbcSAllan Jude ms->nextToUpdate, ilimit);
1531*5ff13fbcSAllan Jude }
1532*5ff13fbcSAllan Jude
15330c16b537SWarner Losh /* Match Loop */
153437f1f268SConrad Meyer #if defined(__GNUC__) && defined(__x86_64__)
153537f1f268SConrad Meyer /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the
153637f1f268SConrad Meyer * code alignment is perturbed. To fix the instability align the loop on 32-bytes.
153737f1f268SConrad Meyer */
153837f1f268SConrad Meyer __asm__(".p2align 5");
153937f1f268SConrad Meyer #endif
15400c16b537SWarner Losh while (ip < ilimit) {
15410c16b537SWarner Losh size_t matchLength=0;
1542*5ff13fbcSAllan Jude size_t offcode=STORE_REPCODE_1;
15430c16b537SWarner Losh const BYTE* start=ip+1;
1544*5ff13fbcSAllan Jude DEBUGLOG(7, "search baseline (depth 0)");
15450c16b537SWarner Losh
15460c16b537SWarner Losh /* check repCode */
1547f7cd7fe5SConrad Meyer if (isDxS) {
15480f743729SConrad Meyer const U32 repIndex = (U32)(ip - base) + 1 - offset_1;
1549f7cd7fe5SConrad Meyer const BYTE* repMatch = ((dictMode == ZSTD_dictMatchState || dictMode == ZSTD_dedicatedDictSearch)
15500f743729SConrad Meyer && repIndex < prefixLowestIndex) ?
15510f743729SConrad Meyer dictBase + (repIndex - dictIndexDelta) :
15520f743729SConrad Meyer base + repIndex;
15530f743729SConrad Meyer if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
15540f743729SConrad Meyer && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
15550f743729SConrad Meyer const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
15560f743729SConrad Meyer matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
15570f743729SConrad Meyer if (depth==0) goto _storeSequence;
15580f743729SConrad Meyer }
15590f743729SConrad Meyer }
15600f743729SConrad Meyer if ( dictMode == ZSTD_noDict
15610f743729SConrad Meyer && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
15620c16b537SWarner Losh matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
15630c16b537SWarner Losh if (depth==0) goto _storeSequence;
15640c16b537SWarner Losh }
15650c16b537SWarner Losh
15660c16b537SWarner Losh /* first search (depth 0) */
15670f743729SConrad Meyer { size_t offsetFound = 999999999;
15680f743729SConrad Meyer size_t const ml2 = searchMax(ms, ip, iend, &offsetFound);
15690c16b537SWarner Losh if (ml2 > matchLength)
1570*5ff13fbcSAllan Jude matchLength = ml2, start = ip, offcode=offsetFound;
15710c16b537SWarner Losh }
15720c16b537SWarner Losh
15730c16b537SWarner Losh if (matchLength < 4) {
157419fcbaf1SConrad Meyer ip += ((ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */
15750c16b537SWarner Losh continue;
15760c16b537SWarner Losh }
15770c16b537SWarner Losh
15780c16b537SWarner Losh /* let's try to find a better solution */
15790c16b537SWarner Losh if (depth>=1)
15800c16b537SWarner Losh while (ip<ilimit) {
1581*5ff13fbcSAllan Jude DEBUGLOG(7, "search depth 1");
15820c16b537SWarner Losh ip ++;
15830f743729SConrad Meyer if ( (dictMode == ZSTD_noDict)
1584*5ff13fbcSAllan Jude && (offcode) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
15850c16b537SWarner Losh size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
15860c16b537SWarner Losh int const gain2 = (int)(mlRep * 3);
1587*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
15880c16b537SWarner Losh if ((mlRep >= 4) && (gain2 > gain1))
1589*5ff13fbcSAllan Jude matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
15900c16b537SWarner Losh }
1591f7cd7fe5SConrad Meyer if (isDxS) {
15920f743729SConrad Meyer const U32 repIndex = (U32)(ip - base) - offset_1;
15930f743729SConrad Meyer const BYTE* repMatch = repIndex < prefixLowestIndex ?
15940f743729SConrad Meyer dictBase + (repIndex - dictIndexDelta) :
15950f743729SConrad Meyer base + repIndex;
15960f743729SConrad Meyer if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
15970f743729SConrad Meyer && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
15980f743729SConrad Meyer const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
15990f743729SConrad Meyer size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
16000f743729SConrad Meyer int const gain2 = (int)(mlRep * 3);
1601*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
16020f743729SConrad Meyer if ((mlRep >= 4) && (gain2 > gain1))
1603*5ff13fbcSAllan Jude matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
16040f743729SConrad Meyer }
16050f743729SConrad Meyer }
16060f743729SConrad Meyer { size_t offset2=999999999;
16070f743729SConrad Meyer size_t const ml2 = searchMax(ms, ip, iend, &offset2);
1608*5ff13fbcSAllan Jude int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
1609*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 4);
16100c16b537SWarner Losh if ((ml2 >= 4) && (gain2 > gain1)) {
1611*5ff13fbcSAllan Jude matchLength = ml2, offcode = offset2, start = ip;
16120c16b537SWarner Losh continue; /* search a better one */
16130c16b537SWarner Losh } }
16140c16b537SWarner Losh
16150c16b537SWarner Losh /* let's find an even better one */
16160c16b537SWarner Losh if ((depth==2) && (ip<ilimit)) {
1617*5ff13fbcSAllan Jude DEBUGLOG(7, "search depth 2");
16180c16b537SWarner Losh ip ++;
16190f743729SConrad Meyer if ( (dictMode == ZSTD_noDict)
1620*5ff13fbcSAllan Jude && (offcode) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
16210f743729SConrad Meyer size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
16220f743729SConrad Meyer int const gain2 = (int)(mlRep * 4);
1623*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
16240f743729SConrad Meyer if ((mlRep >= 4) && (gain2 > gain1))
1625*5ff13fbcSAllan Jude matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
16260c16b537SWarner Losh }
1627f7cd7fe5SConrad Meyer if (isDxS) {
16280f743729SConrad Meyer const U32 repIndex = (U32)(ip - base) - offset_1;
16290f743729SConrad Meyer const BYTE* repMatch = repIndex < prefixLowestIndex ?
16300f743729SConrad Meyer dictBase + (repIndex - dictIndexDelta) :
16310f743729SConrad Meyer base + repIndex;
16320f743729SConrad Meyer if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
16330f743729SConrad Meyer && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
16340f743729SConrad Meyer const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
16350f743729SConrad Meyer size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
16360f743729SConrad Meyer int const gain2 = (int)(mlRep * 4);
1637*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
16380f743729SConrad Meyer if ((mlRep >= 4) && (gain2 > gain1))
1639*5ff13fbcSAllan Jude matchLength = mlRep, offcode = STORE_REPCODE_1, start = ip;
16400f743729SConrad Meyer }
16410f743729SConrad Meyer }
16420f743729SConrad Meyer { size_t offset2=999999999;
16430f743729SConrad Meyer size_t const ml2 = searchMax(ms, ip, iend, &offset2);
1644*5ff13fbcSAllan Jude int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
1645*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 7);
16460c16b537SWarner Losh if ((ml2 >= 4) && (gain2 > gain1)) {
1647*5ff13fbcSAllan Jude matchLength = ml2, offcode = offset2, start = ip;
16480c16b537SWarner Losh continue;
16490c16b537SWarner Losh } } }
16500c16b537SWarner Losh break; /* nothing found : store previous solution */
16510c16b537SWarner Losh }
16520c16b537SWarner Losh
16530c16b537SWarner Losh /* NOTE:
1654*5ff13fbcSAllan Jude * Pay attention that `start[-value]` can lead to strange undefined behavior
1655*5ff13fbcSAllan Jude * notably if `value` is unsigned, resulting in a large positive `-value`.
16560c16b537SWarner Losh */
16570c16b537SWarner Losh /* catch up */
1658*5ff13fbcSAllan Jude if (STORED_IS_OFFSET(offcode)) {
16590f743729SConrad Meyer if (dictMode == ZSTD_noDict) {
1660*5ff13fbcSAllan Jude while ( ((start > anchor) & (start - STORED_OFFSET(offcode) > prefixLowest))
1661*5ff13fbcSAllan Jude && (start[-1] == (start-STORED_OFFSET(offcode))[-1]) ) /* only search for offset within prefix */
16620c16b537SWarner Losh { start--; matchLength++; }
16630f743729SConrad Meyer }
1664f7cd7fe5SConrad Meyer if (isDxS) {
1665*5ff13fbcSAllan Jude U32 const matchIndex = (U32)((size_t)(start-base) - STORED_OFFSET(offcode));
16660f743729SConrad Meyer const BYTE* match = (matchIndex < prefixLowestIndex) ? dictBase + matchIndex - dictIndexDelta : base + matchIndex;
16670f743729SConrad Meyer const BYTE* const mStart = (matchIndex < prefixLowestIndex) ? dictLowest : prefixLowest;
16680f743729SConrad Meyer while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
16690f743729SConrad Meyer }
1670*5ff13fbcSAllan Jude offset_2 = offset_1; offset_1 = (U32)STORED_OFFSET(offcode);
16710c16b537SWarner Losh }
16720c16b537SWarner Losh /* store sequence */
16730c16b537SWarner Losh _storeSequence:
1674*5ff13fbcSAllan Jude { size_t const litLength = (size_t)(start - anchor);
1675*5ff13fbcSAllan Jude ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offcode, matchLength);
16760c16b537SWarner Losh anchor = ip = start + matchLength;
16770c16b537SWarner Losh }
16780c16b537SWarner Losh
16790c16b537SWarner Losh /* check immediate repcode */
1680f7cd7fe5SConrad Meyer if (isDxS) {
16810f743729SConrad Meyer while (ip <= ilimit) {
16820f743729SConrad Meyer U32 const current2 = (U32)(ip-base);
16830f743729SConrad Meyer U32 const repIndex = current2 - offset_2;
1684f7cd7fe5SConrad Meyer const BYTE* repMatch = repIndex < prefixLowestIndex ?
16850f743729SConrad Meyer dictBase - dictIndexDelta + repIndex :
16860f743729SConrad Meyer base + repIndex;
16870f743729SConrad Meyer if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex) >= 3 /* intentional overflow */)
16880f743729SConrad Meyer && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
16890f743729SConrad Meyer const BYTE* const repEnd2 = repIndex < prefixLowestIndex ? dictEnd : iend;
16900f743729SConrad Meyer matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd2, prefixLowest) + 4;
1691*5ff13fbcSAllan Jude offcode = offset_2; offset_2 = offset_1; offset_1 = (U32)offcode; /* swap offset_2 <=> offset_1 */
1692*5ff13fbcSAllan Jude ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, matchLength);
16930f743729SConrad Meyer ip += matchLength;
16940f743729SConrad Meyer anchor = ip;
16950f743729SConrad Meyer continue;
16960f743729SConrad Meyer }
16970f743729SConrad Meyer break;
16980f743729SConrad Meyer }
16990f743729SConrad Meyer }
17000f743729SConrad Meyer
17010f743729SConrad Meyer if (dictMode == ZSTD_noDict) {
1702052d3c12SConrad Meyer while ( ((ip <= ilimit) & (offset_2>0))
1703052d3c12SConrad Meyer && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) {
17040c16b537SWarner Losh /* store sequence */
17050c16b537SWarner Losh matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
1706*5ff13fbcSAllan Jude offcode = offset_2; offset_2 = offset_1; offset_1 = (U32)offcode; /* swap repcodes */
1707*5ff13fbcSAllan Jude ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, matchLength);
17080c16b537SWarner Losh ip += matchLength;
17090c16b537SWarner Losh anchor = ip;
17100c16b537SWarner Losh continue; /* faster when present ... (?) */
17110f743729SConrad Meyer } } }
17120c16b537SWarner Losh
17130c16b537SWarner Losh /* Save reps for next block */
171419fcbaf1SConrad Meyer rep[0] = offset_1 ? offset_1 : savedOffset;
171519fcbaf1SConrad Meyer rep[1] = offset_2 ? offset_2 : savedOffset;
17160c16b537SWarner Losh
17170c16b537SWarner Losh /* Return the last literals size */
17189cbefe25SConrad Meyer return (size_t)(iend - anchor);
17190c16b537SWarner Losh }
17200c16b537SWarner Losh
17210c16b537SWarner Losh
ZSTD_compressBlock_btlazy2(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)172219fcbaf1SConrad Meyer size_t ZSTD_compressBlock_btlazy2(
172319fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17240f743729SConrad Meyer void const* src, size_t srcSize)
17250c16b537SWarner Losh {
17269cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_noDict);
17270c16b537SWarner Losh }
17280c16b537SWarner Losh
ZSTD_compressBlock_lazy2(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)172919fcbaf1SConrad Meyer size_t ZSTD_compressBlock_lazy2(
173019fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17310f743729SConrad Meyer void const* src, size_t srcSize)
17320c16b537SWarner Losh {
17339cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_noDict);
17340c16b537SWarner Losh }
17350c16b537SWarner Losh
ZSTD_compressBlock_lazy(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)173619fcbaf1SConrad Meyer size_t ZSTD_compressBlock_lazy(
173719fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17380f743729SConrad Meyer void const* src, size_t srcSize)
17390c16b537SWarner Losh {
17409cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_noDict);
17410c16b537SWarner Losh }
17420c16b537SWarner Losh
ZSTD_compressBlock_greedy(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)174319fcbaf1SConrad Meyer size_t ZSTD_compressBlock_greedy(
174419fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17450f743729SConrad Meyer void const* src, size_t srcSize)
17460c16b537SWarner Losh {
17479cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_noDict);
17480f743729SConrad Meyer }
17490f743729SConrad Meyer
ZSTD_compressBlock_btlazy2_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)17500f743729SConrad Meyer size_t ZSTD_compressBlock_btlazy2_dictMatchState(
17510f743729SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17520f743729SConrad Meyer void const* src, size_t srcSize)
17530f743729SConrad Meyer {
17549cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_dictMatchState);
17550f743729SConrad Meyer }
17560f743729SConrad Meyer
ZSTD_compressBlock_lazy2_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)17570f743729SConrad Meyer size_t ZSTD_compressBlock_lazy2_dictMatchState(
17580f743729SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17590f743729SConrad Meyer void const* src, size_t srcSize)
17600f743729SConrad Meyer {
17619cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dictMatchState);
17620f743729SConrad Meyer }
17630f743729SConrad Meyer
ZSTD_compressBlock_lazy_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)17640f743729SConrad Meyer size_t ZSTD_compressBlock_lazy_dictMatchState(
17650f743729SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17660f743729SConrad Meyer void const* src, size_t srcSize)
17670f743729SConrad Meyer {
17689cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dictMatchState);
17690f743729SConrad Meyer }
17700f743729SConrad Meyer
ZSTD_compressBlock_greedy_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)17710f743729SConrad Meyer size_t ZSTD_compressBlock_greedy_dictMatchState(
17720f743729SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
17730f743729SConrad Meyer void const* src, size_t srcSize)
17740f743729SConrad Meyer {
17759cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dictMatchState);
17760c16b537SWarner Losh }
17770c16b537SWarner Losh
17780c16b537SWarner Losh
ZSTD_compressBlock_lazy2_dedicatedDictSearch(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1779f7cd7fe5SConrad Meyer size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch(
1780f7cd7fe5SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1781f7cd7fe5SConrad Meyer void const* src, size_t srcSize)
1782f7cd7fe5SConrad Meyer {
1783f7cd7fe5SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dedicatedDictSearch);
1784f7cd7fe5SConrad Meyer }
1785f7cd7fe5SConrad Meyer
ZSTD_compressBlock_lazy_dedicatedDictSearch(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1786f7cd7fe5SConrad Meyer size_t ZSTD_compressBlock_lazy_dedicatedDictSearch(
1787f7cd7fe5SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1788f7cd7fe5SConrad Meyer void const* src, size_t srcSize)
1789f7cd7fe5SConrad Meyer {
1790f7cd7fe5SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dedicatedDictSearch);
1791f7cd7fe5SConrad Meyer }
1792f7cd7fe5SConrad Meyer
ZSTD_compressBlock_greedy_dedicatedDictSearch(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1793f7cd7fe5SConrad Meyer size_t ZSTD_compressBlock_greedy_dedicatedDictSearch(
1794f7cd7fe5SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1795f7cd7fe5SConrad Meyer void const* src, size_t srcSize)
1796f7cd7fe5SConrad Meyer {
1797f7cd7fe5SConrad Meyer return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dedicatedDictSearch);
1798f7cd7fe5SConrad Meyer }
1799f7cd7fe5SConrad Meyer
1800*5ff13fbcSAllan Jude /* 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)1801*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy2_row(
1802*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1803*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1804*5ff13fbcSAllan Jude {
1805*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_noDict);
1806*5ff13fbcSAllan Jude }
1807*5ff13fbcSAllan Jude
ZSTD_compressBlock_lazy_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1808*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy_row(
1809*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1810*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1811*5ff13fbcSAllan Jude {
1812*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_noDict);
1813*5ff13fbcSAllan Jude }
1814*5ff13fbcSAllan Jude
ZSTD_compressBlock_greedy_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1815*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_greedy_row(
1816*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1817*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1818*5ff13fbcSAllan Jude {
1819*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_noDict);
1820*5ff13fbcSAllan Jude }
1821*5ff13fbcSAllan Jude
ZSTD_compressBlock_lazy2_dictMatchState_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1822*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy2_dictMatchState_row(
1823*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1824*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1825*5ff13fbcSAllan Jude {
1826*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dictMatchState);
1827*5ff13fbcSAllan Jude }
1828*5ff13fbcSAllan Jude
ZSTD_compressBlock_lazy_dictMatchState_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1829*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy_dictMatchState_row(
1830*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1831*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1832*5ff13fbcSAllan Jude {
1833*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dictMatchState);
1834*5ff13fbcSAllan Jude }
1835*5ff13fbcSAllan Jude
ZSTD_compressBlock_greedy_dictMatchState_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1836*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_greedy_dictMatchState_row(
1837*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1838*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1839*5ff13fbcSAllan Jude {
1840*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dictMatchState);
1841*5ff13fbcSAllan Jude }
1842*5ff13fbcSAllan Jude
1843*5ff13fbcSAllan Jude
ZSTD_compressBlock_lazy2_dedicatedDictSearch_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1844*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch_row(
1845*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1846*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1847*5ff13fbcSAllan Jude {
1848*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dedicatedDictSearch);
1849*5ff13fbcSAllan Jude }
1850*5ff13fbcSAllan Jude
ZSTD_compressBlock_lazy_dedicatedDictSearch_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1851*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy_dedicatedDictSearch_row(
1852*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1853*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1854*5ff13fbcSAllan Jude {
1855*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dedicatedDictSearch);
1856*5ff13fbcSAllan Jude }
1857*5ff13fbcSAllan Jude
ZSTD_compressBlock_greedy_dedicatedDictSearch_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)1858*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_greedy_dedicatedDictSearch_row(
1859*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1860*5ff13fbcSAllan Jude void const* src, size_t srcSize)
1861*5ff13fbcSAllan Jude {
1862*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dedicatedDictSearch);
1863*5ff13fbcSAllan Jude }
1864f7cd7fe5SConrad Meyer
18650c16b537SWarner Losh 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)186619fcbaf1SConrad Meyer size_t ZSTD_compressBlock_lazy_extDict_generic(
186719fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore,
186819fcbaf1SConrad Meyer U32 rep[ZSTD_REP_NUM],
18690c16b537SWarner Losh const void* src, size_t srcSize,
18709cbefe25SConrad Meyer const searchMethod_e searchMethod, const U32 depth)
18710c16b537SWarner Losh {
18720c16b537SWarner Losh const BYTE* const istart = (const BYTE*)src;
18730c16b537SWarner Losh const BYTE* ip = istart;
18740c16b537SWarner Losh const BYTE* anchor = istart;
18750c16b537SWarner Losh const BYTE* const iend = istart + srcSize;
1876*5ff13fbcSAllan Jude const BYTE* const ilimit = searchMethod == search_rowHash ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8;
187719fcbaf1SConrad Meyer const BYTE* const base = ms->window.base;
187819fcbaf1SConrad Meyer const U32 dictLimit = ms->window.dictLimit;
18790c16b537SWarner Losh const BYTE* const prefixStart = base + dictLimit;
188019fcbaf1SConrad Meyer const BYTE* const dictBase = ms->window.dictBase;
18810c16b537SWarner Losh const BYTE* const dictEnd = dictBase + dictLimit;
188237f1f268SConrad Meyer const BYTE* const dictStart = dictBase + ms->window.lowLimit;
188337f1f268SConrad Meyer const U32 windowLog = ms->cParams.windowLog;
1884*5ff13fbcSAllan Jude const U32 rowLog = ms->cParams.searchLog < 5 ? 4 : 5;
18850c16b537SWarner Losh
1886*5ff13fbcSAllan Jude searchMax_f const searchMax = ZSTD_selectLazyVTable(ms, searchMethod, ZSTD_extDict)->searchMax;
188719fcbaf1SConrad Meyer U32 offset_1 = rep[0], offset_2 = rep[1];
18880c16b537SWarner Losh
1889*5ff13fbcSAllan Jude DEBUGLOG(5, "ZSTD_compressBlock_lazy_extDict_generic (searchFunc=%u)", (U32)searchMethod);
189037f1f268SConrad Meyer
18910c16b537SWarner Losh /* init */
18920c16b537SWarner Losh ip += (ip == prefixStart);
1893*5ff13fbcSAllan Jude if (searchMethod == search_rowHash) {
1894*5ff13fbcSAllan Jude ZSTD_row_fillHashCache(ms, base, rowLog,
1895*5ff13fbcSAllan Jude MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */),
1896*5ff13fbcSAllan Jude ms->nextToUpdate, ilimit);
1897*5ff13fbcSAllan Jude }
18980c16b537SWarner Losh
18990c16b537SWarner Losh /* Match Loop */
190037f1f268SConrad Meyer #if defined(__GNUC__) && defined(__x86_64__)
190137f1f268SConrad Meyer /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the
190237f1f268SConrad Meyer * code alignment is perturbed. To fix the instability align the loop on 32-bytes.
190337f1f268SConrad Meyer */
190437f1f268SConrad Meyer __asm__(".p2align 5");
190537f1f268SConrad Meyer #endif
19060c16b537SWarner Losh while (ip < ilimit) {
19070c16b537SWarner Losh size_t matchLength=0;
1908*5ff13fbcSAllan Jude size_t offcode=STORE_REPCODE_1;
19090c16b537SWarner Losh const BYTE* start=ip+1;
1910f7cd7fe5SConrad Meyer U32 curr = (U32)(ip-base);
19110c16b537SWarner Losh
19120c16b537SWarner Losh /* check repCode */
1913f7cd7fe5SConrad Meyer { const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr+1, windowLog);
1914f7cd7fe5SConrad Meyer const U32 repIndex = (U32)(curr+1 - offset_1);
19150c16b537SWarner Losh const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
19160c16b537SWarner Losh const BYTE* const repMatch = repBase + repIndex;
1917*5ff13fbcSAllan Jude if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */
1918*5ff13fbcSAllan Jude & (offset_1 <= curr+1 - windowLow) ) /* note: we are searching at curr+1 */
19190c16b537SWarner Losh if (MEM_read32(ip+1) == MEM_read32(repMatch)) {
19200c16b537SWarner Losh /* repcode detected we should take it */
19210c16b537SWarner Losh const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
19220c16b537SWarner Losh matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4;
19230c16b537SWarner Losh if (depth==0) goto _storeSequence;
19240c16b537SWarner Losh } }
19250c16b537SWarner Losh
19260c16b537SWarner Losh /* first search (depth 0) */
19270f743729SConrad Meyer { size_t offsetFound = 999999999;
19280f743729SConrad Meyer size_t const ml2 = searchMax(ms, ip, iend, &offsetFound);
19290c16b537SWarner Losh if (ml2 > matchLength)
1930*5ff13fbcSAllan Jude matchLength = ml2, start = ip, offcode=offsetFound;
19310c16b537SWarner Losh }
19320c16b537SWarner Losh
19330c16b537SWarner Losh if (matchLength < 4) {
193419fcbaf1SConrad Meyer ip += ((ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */
19350c16b537SWarner Losh continue;
19360c16b537SWarner Losh }
19370c16b537SWarner Losh
19380c16b537SWarner Losh /* let's try to find a better solution */
19390c16b537SWarner Losh if (depth>=1)
19400c16b537SWarner Losh while (ip<ilimit) {
19410c16b537SWarner Losh ip ++;
1942f7cd7fe5SConrad Meyer curr++;
19430c16b537SWarner Losh /* check repCode */
1944*5ff13fbcSAllan Jude if (offcode) {
1945f7cd7fe5SConrad Meyer const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog);
1946f7cd7fe5SConrad Meyer const U32 repIndex = (U32)(curr - offset_1);
19470c16b537SWarner Losh const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
19480c16b537SWarner Losh const BYTE* const repMatch = repBase + repIndex;
1949*5ff13fbcSAllan Jude if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
1950*5ff13fbcSAllan Jude & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
19510c16b537SWarner Losh if (MEM_read32(ip) == MEM_read32(repMatch)) {
19520c16b537SWarner Losh /* repcode detected */
19530c16b537SWarner Losh const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
19540c16b537SWarner Losh size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
19550c16b537SWarner Losh int const gain2 = (int)(repLength * 3);
1956*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
19570c16b537SWarner Losh if ((repLength >= 4) && (gain2 > gain1))
1958*5ff13fbcSAllan Jude matchLength = repLength, offcode = STORE_REPCODE_1, start = ip;
19590c16b537SWarner Losh } }
19600c16b537SWarner Losh
19610c16b537SWarner Losh /* search match, depth 1 */
19620f743729SConrad Meyer { size_t offset2=999999999;
19630f743729SConrad Meyer size_t const ml2 = searchMax(ms, ip, iend, &offset2);
1964*5ff13fbcSAllan Jude int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
1965*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 4);
19660c16b537SWarner Losh if ((ml2 >= 4) && (gain2 > gain1)) {
1967*5ff13fbcSAllan Jude matchLength = ml2, offcode = offset2, start = ip;
19680c16b537SWarner Losh continue; /* search a better one */
19690c16b537SWarner Losh } }
19700c16b537SWarner Losh
19710c16b537SWarner Losh /* let's find an even better one */
19720c16b537SWarner Losh if ((depth==2) && (ip<ilimit)) {
19730c16b537SWarner Losh ip ++;
1974f7cd7fe5SConrad Meyer curr++;
19750c16b537SWarner Losh /* check repCode */
1976*5ff13fbcSAllan Jude if (offcode) {
1977f7cd7fe5SConrad Meyer const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog);
1978f7cd7fe5SConrad Meyer const U32 repIndex = (U32)(curr - offset_1);
19790c16b537SWarner Losh const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
19800c16b537SWarner Losh const BYTE* const repMatch = repBase + repIndex;
1981*5ff13fbcSAllan Jude if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
1982*5ff13fbcSAllan Jude & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
19830c16b537SWarner Losh if (MEM_read32(ip) == MEM_read32(repMatch)) {
19840c16b537SWarner Losh /* repcode detected */
19850c16b537SWarner Losh const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
19860c16b537SWarner Losh size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
19870c16b537SWarner Losh int const gain2 = (int)(repLength * 4);
1988*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 1);
19890c16b537SWarner Losh if ((repLength >= 4) && (gain2 > gain1))
1990*5ff13fbcSAllan Jude matchLength = repLength, offcode = STORE_REPCODE_1, start = ip;
19910c16b537SWarner Losh } }
19920c16b537SWarner Losh
19930c16b537SWarner Losh /* search match, depth 2 */
19940f743729SConrad Meyer { size_t offset2=999999999;
19950f743729SConrad Meyer size_t const ml2 = searchMax(ms, ip, iend, &offset2);
1996*5ff13fbcSAllan Jude int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offset2))); /* raw approx */
1997*5ff13fbcSAllan Jude int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)STORED_TO_OFFBASE(offcode)) + 7);
19980c16b537SWarner Losh if ((ml2 >= 4) && (gain2 > gain1)) {
1999*5ff13fbcSAllan Jude matchLength = ml2, offcode = offset2, start = ip;
20000c16b537SWarner Losh continue;
20010c16b537SWarner Losh } } }
20020c16b537SWarner Losh break; /* nothing found : store previous solution */
20030c16b537SWarner Losh }
20040c16b537SWarner Losh
20050c16b537SWarner Losh /* catch up */
2006*5ff13fbcSAllan Jude if (STORED_IS_OFFSET(offcode)) {
2007*5ff13fbcSAllan Jude U32 const matchIndex = (U32)((size_t)(start-base) - STORED_OFFSET(offcode));
20080c16b537SWarner Losh const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex;
20090c16b537SWarner Losh const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart;
20100c16b537SWarner Losh while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
2011*5ff13fbcSAllan Jude offset_2 = offset_1; offset_1 = (U32)STORED_OFFSET(offcode);
20120c16b537SWarner Losh }
20130c16b537SWarner Losh
20140c16b537SWarner Losh /* store sequence */
20150c16b537SWarner Losh _storeSequence:
2016*5ff13fbcSAllan Jude { size_t const litLength = (size_t)(start - anchor);
2017*5ff13fbcSAllan Jude ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offcode, matchLength);
20180c16b537SWarner Losh anchor = ip = start + matchLength;
20190c16b537SWarner Losh }
20200c16b537SWarner Losh
20210c16b537SWarner Losh /* check immediate repcode */
20220c16b537SWarner Losh while (ip <= ilimit) {
202337f1f268SConrad Meyer const U32 repCurrent = (U32)(ip-base);
202437f1f268SConrad Meyer const U32 windowLow = ZSTD_getLowestMatchIndex(ms, repCurrent, windowLog);
202537f1f268SConrad Meyer const U32 repIndex = repCurrent - offset_2;
20260c16b537SWarner Losh const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
20270c16b537SWarner Losh const BYTE* const repMatch = repBase + repIndex;
2028*5ff13fbcSAllan Jude if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */
2029*5ff13fbcSAllan Jude & (offset_2 <= repCurrent - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */
20300c16b537SWarner Losh if (MEM_read32(ip) == MEM_read32(repMatch)) {
20310c16b537SWarner Losh /* repcode detected we should take it */
20320c16b537SWarner Losh const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
20330c16b537SWarner Losh matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
2034*5ff13fbcSAllan Jude offcode = offset_2; offset_2 = offset_1; offset_1 = (U32)offcode; /* swap offset history */
2035*5ff13fbcSAllan Jude ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, matchLength);
20360c16b537SWarner Losh ip += matchLength;
20370c16b537SWarner Losh anchor = ip;
20380c16b537SWarner Losh continue; /* faster when present ... (?) */
20390c16b537SWarner Losh }
20400c16b537SWarner Losh break;
20410c16b537SWarner Losh } }
20420c16b537SWarner Losh
20430c16b537SWarner Losh /* Save reps for next block */
204419fcbaf1SConrad Meyer rep[0] = offset_1;
204519fcbaf1SConrad Meyer rep[1] = offset_2;
20460c16b537SWarner Losh
20470c16b537SWarner Losh /* Return the last literals size */
20489cbefe25SConrad Meyer return (size_t)(iend - anchor);
20490c16b537SWarner Losh }
20500c16b537SWarner Losh
20510c16b537SWarner Losh
ZSTD_compressBlock_greedy_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)205219fcbaf1SConrad Meyer size_t ZSTD_compressBlock_greedy_extDict(
205319fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
20540f743729SConrad Meyer void const* src, size_t srcSize)
20550c16b537SWarner Losh {
20569cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0);
20570c16b537SWarner Losh }
20580c16b537SWarner Losh
ZSTD_compressBlock_lazy_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)205919fcbaf1SConrad Meyer size_t ZSTD_compressBlock_lazy_extDict(
206019fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
20610f743729SConrad Meyer void const* src, size_t srcSize)
206219fcbaf1SConrad Meyer
20630c16b537SWarner Losh {
20649cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1);
20650c16b537SWarner Losh }
20660c16b537SWarner Losh
ZSTD_compressBlock_lazy2_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)206719fcbaf1SConrad Meyer size_t ZSTD_compressBlock_lazy2_extDict(
206819fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
20690f743729SConrad Meyer void const* src, size_t srcSize)
207019fcbaf1SConrad Meyer
20710c16b537SWarner Losh {
20729cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2);
20730c16b537SWarner Losh }
20740c16b537SWarner Losh
ZSTD_compressBlock_btlazy2_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)207519fcbaf1SConrad Meyer size_t ZSTD_compressBlock_btlazy2_extDict(
207619fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
20770f743729SConrad Meyer void const* src, size_t srcSize)
207819fcbaf1SConrad Meyer
20790c16b537SWarner Losh {
20809cbefe25SConrad Meyer return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2);
20810c16b537SWarner Losh }
2082*5ff13fbcSAllan Jude
ZSTD_compressBlock_greedy_extDict_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2083*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_greedy_extDict_row(
2084*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2085*5ff13fbcSAllan Jude void const* src, size_t srcSize)
2086*5ff13fbcSAllan Jude {
2087*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0);
2088*5ff13fbcSAllan Jude }
2089*5ff13fbcSAllan Jude
ZSTD_compressBlock_lazy_extDict_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2090*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy_extDict_row(
2091*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2092*5ff13fbcSAllan Jude void const* src, size_t srcSize)
2093*5ff13fbcSAllan Jude
2094*5ff13fbcSAllan Jude {
2095*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1);
2096*5ff13fbcSAllan Jude }
2097*5ff13fbcSAllan Jude
ZSTD_compressBlock_lazy2_extDict_row(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)2098*5ff13fbcSAllan Jude size_t ZSTD_compressBlock_lazy2_extDict_row(
2099*5ff13fbcSAllan Jude ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2100*5ff13fbcSAllan Jude void const* src, size_t srcSize)
2101*5ff13fbcSAllan Jude
2102*5ff13fbcSAllan Jude {
2103*5ff13fbcSAllan Jude return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2);
2104*5ff13fbcSAllan Jude }
2105