Path: blob/main/sys/contrib/zstd/lib/compress/zstd_fast.c
48378 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#include "zstd_compress_internal.h" /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */11#include "zstd_fast.h"121314void ZSTD_fillHashTable(ZSTD_matchState_t* ms,15const void* const end,16ZSTD_dictTableLoadMethod_e dtlm)17{18const ZSTD_compressionParameters* const cParams = &ms->cParams;19U32* const hashTable = ms->hashTable;20U32 const hBits = cParams->hashLog;21U32 const mls = cParams->minMatch;22const BYTE* const base = ms->window.base;23const BYTE* ip = base + ms->nextToUpdate;24const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;25const U32 fastHashFillStep = 3;2627/* Always insert every fastHashFillStep position into the hash table.28* Insert the other positions if their hash entry is empty.29*/30for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {31U32 const curr = (U32)(ip - base);32size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);33hashTable[hash0] = curr;34if (dtlm == ZSTD_dtlm_fast) continue;35/* Only load extra positions for ZSTD_dtlm_full */36{ U32 p;37for (p = 1; p < fastHashFillStep; ++p) {38size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls);39if (hashTable[hash] == 0) { /* not yet filled */40hashTable[hash] = curr + p;41} } } }42}434445/**46* If you squint hard enough (and ignore repcodes), the search operation at any47* given position is broken into 4 stages:48*49* 1. Hash (map position to hash value via input read)50* 2. Lookup (map hash val to index via hashtable read)51* 3. Load (map index to value at that position via input read)52* 4. Compare53*54* Each of these steps involves a memory read at an address which is computed55* from the previous step. This means these steps must be sequenced and their56* latencies are cumulative.57*58* Rather than do 1->2->3->4 sequentially for a single position before moving59* onto the next, this implementation interleaves these operations across the60* next few positions:61*62* R = Repcode Read & Compare63* H = Hash64* T = Table Lookup65* M = Match Read & Compare66*67* Pos | Time -->68* ----+-------------------69* N | ... M70* N+1 | ... TM71* N+2 | R H T M72* N+3 | H TM73* N+4 | R H T M74* N+5 | H ...75* N+6 | R ...76*77* This is very much analogous to the pipelining of execution in a CPU. And just78* like a CPU, we have to dump the pipeline when we find a match (i.e., take a79* branch).80*81* When this happens, we throw away our current state, and do the following prep82* to re-enter the loop:83*84* Pos | Time -->85* ----+-------------------86* N | H T87* N+1 | H88*89* This is also the work we do at the beginning to enter the loop initially.90*/91FORCE_INLINE_TEMPLATE size_t92ZSTD_compressBlock_fast_noDict_generic(93ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],94void const* src, size_t srcSize,95U32 const mls, U32 const hasStep)96{97const ZSTD_compressionParameters* const cParams = &ms->cParams;98U32* const hashTable = ms->hashTable;99U32 const hlog = cParams->hashLog;100/* support stepSize of 0 */101size_t const stepSize = hasStep ? (cParams->targetLength + !(cParams->targetLength) + 1) : 2;102const BYTE* const base = ms->window.base;103const BYTE* const istart = (const BYTE*)src;104const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);105const U32 prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);106const BYTE* const prefixStart = base + prefixStartIndex;107const BYTE* const iend = istart + srcSize;108const BYTE* const ilimit = iend - HASH_READ_SIZE;109110const BYTE* anchor = istart;111const BYTE* ip0 = istart;112const BYTE* ip1;113const BYTE* ip2;114const BYTE* ip3;115U32 current0;116117U32 rep_offset1 = rep[0];118U32 rep_offset2 = rep[1];119U32 offsetSaved = 0;120121size_t hash0; /* hash for ip0 */122size_t hash1; /* hash for ip1 */123U32 idx; /* match idx for ip0 */124U32 mval; /* src value at match idx */125126U32 offcode;127const BYTE* match0;128size_t mLength;129130/* ip0 and ip1 are always adjacent. The targetLength skipping and131* uncompressibility acceleration is applied to every other position,132* matching the behavior of #1562. step therefore represents the gap133* between pairs of positions, from ip0 to ip2 or ip1 to ip3. */134size_t step;135const BYTE* nextStep;136const size_t kStepIncr = (1 << (kSearchStrength - 1));137138DEBUGLOG(5, "ZSTD_compressBlock_fast_generic");139ip0 += (ip0 == prefixStart);140{ U32 const curr = (U32)(ip0 - base);141U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog);142U32 const maxRep = curr - windowLow;143if (rep_offset2 > maxRep) offsetSaved = rep_offset2, rep_offset2 = 0;144if (rep_offset1 > maxRep) offsetSaved = rep_offset1, rep_offset1 = 0;145}146147/* start each op */148_start: /* Requires: ip0 */149150step = stepSize;151nextStep = ip0 + kStepIncr;152153/* calculate positions, ip0 - anchor == 0, so we skip step calc */154ip1 = ip0 + 1;155ip2 = ip0 + step;156ip3 = ip2 + 1;157158if (ip3 >= ilimit) {159goto _cleanup;160}161162hash0 = ZSTD_hashPtr(ip0, hlog, mls);163hash1 = ZSTD_hashPtr(ip1, hlog, mls);164165idx = hashTable[hash0];166167do {168/* load repcode match for ip[2]*/169const U32 rval = MEM_read32(ip2 - rep_offset1);170171/* write back hash table entry */172current0 = (U32)(ip0 - base);173hashTable[hash0] = current0;174175/* check repcode at ip[2] */176if ((MEM_read32(ip2) == rval) & (rep_offset1 > 0)) {177ip0 = ip2;178match0 = ip0 - rep_offset1;179mLength = ip0[-1] == match0[-1];180ip0 -= mLength;181match0 -= mLength;182offcode = STORE_REPCODE_1;183mLength += 4;184goto _match;185}186187/* load match for ip[0] */188if (idx >= prefixStartIndex) {189mval = MEM_read32(base + idx);190} else {191mval = MEM_read32(ip0) ^ 1; /* guaranteed to not match. */192}193194/* check match at ip[0] */195if (MEM_read32(ip0) == mval) {196/* found a match! */197goto _offset;198}199200/* lookup ip[1] */201idx = hashTable[hash1];202203/* hash ip[2] */204hash0 = hash1;205hash1 = ZSTD_hashPtr(ip2, hlog, mls);206207/* advance to next positions */208ip0 = ip1;209ip1 = ip2;210ip2 = ip3;211212/* write back hash table entry */213current0 = (U32)(ip0 - base);214hashTable[hash0] = current0;215216/* load match for ip[0] */217if (idx >= prefixStartIndex) {218mval = MEM_read32(base + idx);219} else {220mval = MEM_read32(ip0) ^ 1; /* guaranteed to not match. */221}222223/* check match at ip[0] */224if (MEM_read32(ip0) == mval) {225/* found a match! */226goto _offset;227}228229/* lookup ip[1] */230idx = hashTable[hash1];231232/* hash ip[2] */233hash0 = hash1;234hash1 = ZSTD_hashPtr(ip2, hlog, mls);235236/* advance to next positions */237ip0 = ip1;238ip1 = ip2;239ip2 = ip0 + step;240ip3 = ip1 + step;241242/* calculate step */243if (ip2 >= nextStep) {244step++;245PREFETCH_L1(ip1 + 64);246PREFETCH_L1(ip1 + 128);247nextStep += kStepIncr;248}249} while (ip3 < ilimit);250251_cleanup:252/* Note that there are probably still a couple positions we could search.253* However, it seems to be a meaningful performance hit to try to search254* them. So let's not. */255256/* save reps for next block */257rep[0] = rep_offset1 ? rep_offset1 : offsetSaved;258rep[1] = rep_offset2 ? rep_offset2 : offsetSaved;259260/* Return the last literals size */261return (size_t)(iend - anchor);262263_offset: /* Requires: ip0, idx */264265/* Compute the offset code. */266match0 = base + idx;267rep_offset2 = rep_offset1;268rep_offset1 = (U32)(ip0-match0);269offcode = STORE_OFFSET(rep_offset1);270mLength = 4;271272/* Count the backwards match length. */273while (((ip0>anchor) & (match0>prefixStart)) && (ip0[-1] == match0[-1])) {274ip0--;275match0--;276mLength++;277}278279_match: /* Requires: ip0, match0, offcode */280281/* Count the forward length. */282mLength += ZSTD_count(ip0 + mLength, match0 + mLength, iend);283284ZSTD_storeSeq(seqStore, (size_t)(ip0 - anchor), anchor, iend, offcode, mLength);285286ip0 += mLength;287anchor = ip0;288289/* write next hash table entry */290if (ip1 < ip0) {291hashTable[hash1] = (U32)(ip1 - base);292}293294/* Fill table and check for immediate repcode. */295if (ip0 <= ilimit) {296/* Fill Table */297assert(base+current0+2 > istart); /* check base overflow */298hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */299hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);300301if (rep_offset2 > 0) { /* rep_offset2==0 means rep_offset2 is invalidated */302while ( (ip0 <= ilimit) && (MEM_read32(ip0) == MEM_read32(ip0 - rep_offset2)) ) {303/* store sequence */304size_t const rLength = ZSTD_count(ip0+4, ip0+4-rep_offset2, iend) + 4;305{ U32 const tmpOff = rep_offset2; rep_offset2 = rep_offset1; rep_offset1 = tmpOff; } /* swap rep_offset2 <=> rep_offset1 */306hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);307ip0 += rLength;308ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, STORE_REPCODE_1, rLength);309anchor = ip0;310continue; /* faster when present (confirmed on gcc-8) ... (?) */311} } }312313goto _start;314}315316#define ZSTD_GEN_FAST_FN(dictMode, mls, step) \317static size_t ZSTD_compressBlock_fast_##dictMode##_##mls##_##step( \318ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], \319void const* src, size_t srcSize) \320{ \321return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls, step); \322}323324ZSTD_GEN_FAST_FN(noDict, 4, 1)325ZSTD_GEN_FAST_FN(noDict, 5, 1)326ZSTD_GEN_FAST_FN(noDict, 6, 1)327ZSTD_GEN_FAST_FN(noDict, 7, 1)328329ZSTD_GEN_FAST_FN(noDict, 4, 0)330ZSTD_GEN_FAST_FN(noDict, 5, 0)331ZSTD_GEN_FAST_FN(noDict, 6, 0)332ZSTD_GEN_FAST_FN(noDict, 7, 0)333334size_t ZSTD_compressBlock_fast(335ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],336void const* src, size_t srcSize)337{338U32 const mls = ms->cParams.minMatch;339assert(ms->dictMatchState == NULL);340if (ms->cParams.targetLength > 1) {341switch(mls)342{343default: /* includes case 3 */344case 4 :345return ZSTD_compressBlock_fast_noDict_4_1(ms, seqStore, rep, src, srcSize);346case 5 :347return ZSTD_compressBlock_fast_noDict_5_1(ms, seqStore, rep, src, srcSize);348case 6 :349return ZSTD_compressBlock_fast_noDict_6_1(ms, seqStore, rep, src, srcSize);350case 7 :351return ZSTD_compressBlock_fast_noDict_7_1(ms, seqStore, rep, src, srcSize);352}353} else {354switch(mls)355{356default: /* includes case 3 */357case 4 :358return ZSTD_compressBlock_fast_noDict_4_0(ms, seqStore, rep, src, srcSize);359case 5 :360return ZSTD_compressBlock_fast_noDict_5_0(ms, seqStore, rep, src, srcSize);361case 6 :362return ZSTD_compressBlock_fast_noDict_6_0(ms, seqStore, rep, src, srcSize);363case 7 :364return ZSTD_compressBlock_fast_noDict_7_0(ms, seqStore, rep, src, srcSize);365}366367}368}369370FORCE_INLINE_TEMPLATE371size_t ZSTD_compressBlock_fast_dictMatchState_generic(372ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],373void const* src, size_t srcSize, U32 const mls, U32 const hasStep)374{375const ZSTD_compressionParameters* const cParams = &ms->cParams;376U32* const hashTable = ms->hashTable;377U32 const hlog = cParams->hashLog;378/* support stepSize of 0 */379U32 const stepSize = cParams->targetLength + !(cParams->targetLength);380const BYTE* const base = ms->window.base;381const BYTE* const istart = (const BYTE*)src;382const BYTE* ip = istart;383const BYTE* anchor = istart;384const U32 prefixStartIndex = ms->window.dictLimit;385const BYTE* const prefixStart = base + prefixStartIndex;386const BYTE* const iend = istart + srcSize;387const BYTE* const ilimit = iend - HASH_READ_SIZE;388U32 offset_1=rep[0], offset_2=rep[1];389U32 offsetSaved = 0;390391const ZSTD_matchState_t* const dms = ms->dictMatchState;392const ZSTD_compressionParameters* const dictCParams = &dms->cParams ;393const U32* const dictHashTable = dms->hashTable;394const U32 dictStartIndex = dms->window.dictLimit;395const BYTE* const dictBase = dms->window.base;396const BYTE* const dictStart = dictBase + dictStartIndex;397const BYTE* const dictEnd = dms->window.nextSrc;398const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase);399const U32 dictAndPrefixLength = (U32)(ip - prefixStart + dictEnd - dictStart);400const U32 dictHLog = dictCParams->hashLog;401402/* if a dictionary is still attached, it necessarily means that403* it is within window size. So we just check it. */404const U32 maxDistance = 1U << cParams->windowLog;405const U32 endIndex = (U32)((size_t)(ip - base) + srcSize);406assert(endIndex - prefixStartIndex <= maxDistance);407(void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */408409(void)hasStep; /* not currently specialized on whether it's accelerated */410411/* ensure there will be no underflow412* when translating a dict index into a local index */413assert(prefixStartIndex >= (U32)(dictEnd - dictBase));414415/* init */416DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic");417ip += (dictAndPrefixLength == 0);418/* dictMatchState repCode checks don't currently handle repCode == 0419* disabling. */420assert(offset_1 <= dictAndPrefixLength);421assert(offset_2 <= dictAndPrefixLength);422423/* Main Search Loop */424while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */425size_t mLength;426size_t const h = ZSTD_hashPtr(ip, hlog, mls);427U32 const curr = (U32)(ip-base);428U32 const matchIndex = hashTable[h];429const BYTE* match = base + matchIndex;430const U32 repIndex = curr + 1 - offset_1;431const BYTE* repMatch = (repIndex < prefixStartIndex) ?432dictBase + (repIndex - dictIndexDelta) :433base + repIndex;434hashTable[h] = curr; /* update hash table */435436if ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */437&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {438const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;439mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;440ip++;441ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_REPCODE_1, mLength);442} else if ( (matchIndex <= prefixStartIndex) ) {443size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls);444U32 const dictMatchIndex = dictHashTable[dictHash];445const BYTE* dictMatch = dictBase + dictMatchIndex;446if (dictMatchIndex <= dictStartIndex ||447MEM_read32(dictMatch) != MEM_read32(ip)) {448assert(stepSize >= 1);449ip += ((ip-anchor) >> kSearchStrength) + stepSize;450continue;451} else {452/* found a dict match */453U32 const offset = (U32)(curr-dictMatchIndex-dictIndexDelta);454mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4;455while (((ip>anchor) & (dictMatch>dictStart))456&& (ip[-1] == dictMatch[-1])) {457ip--; dictMatch--; mLength++;458} /* catch up */459offset_2 = offset_1;460offset_1 = offset;461ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_OFFSET(offset), mLength);462}463} else if (MEM_read32(match) != MEM_read32(ip)) {464/* it's not a match, and we're not going to check the dictionary */465assert(stepSize >= 1);466ip += ((ip-anchor) >> kSearchStrength) + stepSize;467continue;468} else {469/* found a regular match */470U32 const offset = (U32)(ip-match);471mLength = ZSTD_count(ip+4, match+4, iend) + 4;472while (((ip>anchor) & (match>prefixStart))473&& (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */474offset_2 = offset_1;475offset_1 = offset;476ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_OFFSET(offset), mLength);477}478479/* match found */480ip += mLength;481anchor = ip;482483if (ip <= ilimit) {484/* Fill Table */485assert(base+curr+2 > istart); /* check base overflow */486hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2; /* here because curr+2 could be > iend-8 */487hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);488489/* check immediate repcode */490while (ip <= ilimit) {491U32 const current2 = (U32)(ip-base);492U32 const repIndex2 = current2 - offset_2;493const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?494dictBase - dictIndexDelta + repIndex2 :495base + repIndex2;496if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)497&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {498const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;499size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;500U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */501ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, repLength2);502hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;503ip += repLength2;504anchor = ip;505continue;506}507break;508}509}510}511512/* save reps for next block */513rep[0] = offset_1 ? offset_1 : offsetSaved;514rep[1] = offset_2 ? offset_2 : offsetSaved;515516/* Return the last literals size */517return (size_t)(iend - anchor);518}519520521ZSTD_GEN_FAST_FN(dictMatchState, 4, 0)522ZSTD_GEN_FAST_FN(dictMatchState, 5, 0)523ZSTD_GEN_FAST_FN(dictMatchState, 6, 0)524ZSTD_GEN_FAST_FN(dictMatchState, 7, 0)525526size_t ZSTD_compressBlock_fast_dictMatchState(527ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],528void const* src, size_t srcSize)529{530U32 const mls = ms->cParams.minMatch;531assert(ms->dictMatchState != NULL);532switch(mls)533{534default: /* includes case 3 */535case 4 :536return ZSTD_compressBlock_fast_dictMatchState_4_0(ms, seqStore, rep, src, srcSize);537case 5 :538return ZSTD_compressBlock_fast_dictMatchState_5_0(ms, seqStore, rep, src, srcSize);539case 6 :540return ZSTD_compressBlock_fast_dictMatchState_6_0(ms, seqStore, rep, src, srcSize);541case 7 :542return ZSTD_compressBlock_fast_dictMatchState_7_0(ms, seqStore, rep, src, srcSize);543}544}545546547static size_t ZSTD_compressBlock_fast_extDict_generic(548ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],549void const* src, size_t srcSize, U32 const mls, U32 const hasStep)550{551const ZSTD_compressionParameters* const cParams = &ms->cParams;552U32* const hashTable = ms->hashTable;553U32 const hlog = cParams->hashLog;554/* support stepSize of 0 */555U32 const stepSize = cParams->targetLength + !(cParams->targetLength);556const BYTE* const base = ms->window.base;557const BYTE* const dictBase = ms->window.dictBase;558const BYTE* const istart = (const BYTE*)src;559const BYTE* ip = istart;560const BYTE* anchor = istart;561const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);562const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);563const U32 dictStartIndex = lowLimit;564const BYTE* const dictStart = dictBase + dictStartIndex;565const U32 dictLimit = ms->window.dictLimit;566const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit;567const BYTE* const prefixStart = base + prefixStartIndex;568const BYTE* const dictEnd = dictBase + prefixStartIndex;569const BYTE* const iend = istart + srcSize;570const BYTE* const ilimit = iend - 8;571U32 offset_1=rep[0], offset_2=rep[1];572573(void)hasStep; /* not currently specialized on whether it's accelerated */574575DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic (offset_1=%u)", offset_1);576577/* switch to "regular" variant if extDict is invalidated due to maxDistance */578if (prefixStartIndex == dictStartIndex)579return ZSTD_compressBlock_fast(ms, seqStore, rep, src, srcSize);580581/* Search Loop */582while (ip < ilimit) { /* < instead of <=, because (ip+1) */583const size_t h = ZSTD_hashPtr(ip, hlog, mls);584const U32 matchIndex = hashTable[h];585const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;586const BYTE* match = matchBase + matchIndex;587const U32 curr = (U32)(ip-base);588const U32 repIndex = curr + 1 - offset_1;589const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;590const BYTE* const repMatch = repBase + repIndex;591hashTable[h] = curr; /* update hash table */592DEBUGLOG(7, "offset_1 = %u , curr = %u", offset_1, curr);593594if ( ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */595& (offset_1 <= curr+1 - dictStartIndex) ) /* note: we are searching at curr+1 */596&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {597const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;598size_t const rLength = ZSTD_count_2segments(ip+1 +4, repMatch +4, iend, repMatchEnd, prefixStart) + 4;599ip++;600ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_REPCODE_1, rLength);601ip += rLength;602anchor = ip;603} else {604if ( (matchIndex < dictStartIndex) ||605(MEM_read32(match) != MEM_read32(ip)) ) {606assert(stepSize >= 1);607ip += ((ip-anchor) >> kSearchStrength) + stepSize;608continue;609}610{ const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;611const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;612U32 const offset = curr - matchIndex;613size_t mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;614while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */615offset_2 = offset_1; offset_1 = offset; /* update offset history */616ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_OFFSET(offset), mLength);617ip += mLength;618anchor = ip;619} }620621if (ip <= ilimit) {622/* Fill Table */623hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2;624hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);625/* check immediate repcode */626while (ip <= ilimit) {627U32 const current2 = (U32)(ip-base);628U32 const repIndex2 = current2 - offset_2;629const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;630if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (offset_2 <= curr - dictStartIndex)) /* intentional overflow */631&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {632const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;633size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;634{ U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */635ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, STORE_REPCODE_1, repLength2);636hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;637ip += repLength2;638anchor = ip;639continue;640}641break;642} } }643644/* save reps for next block */645rep[0] = offset_1;646rep[1] = offset_2;647648/* Return the last literals size */649return (size_t)(iend - anchor);650}651652ZSTD_GEN_FAST_FN(extDict, 4, 0)653ZSTD_GEN_FAST_FN(extDict, 5, 0)654ZSTD_GEN_FAST_FN(extDict, 6, 0)655ZSTD_GEN_FAST_FN(extDict, 7, 0)656657size_t ZSTD_compressBlock_fast_extDict(658ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],659void const* src, size_t srcSize)660{661U32 const mls = ms->cParams.minMatch;662switch(mls)663{664default: /* includes case 3 */665case 4 :666return ZSTD_compressBlock_fast_extDict_4_0(ms, seqStore, rep, src, srcSize);667case 5 :668return ZSTD_compressBlock_fast_extDict_5_0(ms, seqStore, rep, src, srcSize);669case 6 :670return ZSTD_compressBlock_fast_extDict_6_0(ms, seqStore, rep, src, srcSize);671case 7 :672return ZSTD_compressBlock_fast_extDict_7_0(ms, seqStore, rep, src, srcSize);673}674}675676677