Path: blob/main/sys/contrib/openzfs/module/zstd/lib/compress/zstd_fast.c
48774 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/*2* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.3* All rights reserved.4*5* This source code is licensed under both the BSD-style license (found in the6* LICENSE file in the root directory of this source tree) and the GPLv2 (found7* in the COPYING file in the root directory of this source tree).8* You may select, at your option, one of the above-listed licenses.9*/1011#include "zstd_compress_internal.h" /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */12#include "zstd_fast.h"131415void ZSTD_fillHashTable(ZSTD_matchState_t* ms,16const void* const end,17ZSTD_dictTableLoadMethod_e dtlm)18{19const ZSTD_compressionParameters* const cParams = &ms->cParams;20U32* const hashTable = ms->hashTable;21U32 const hBits = cParams->hashLog;22U32 const mls = cParams->minMatch;23const BYTE* const base = ms->window.base;24const BYTE* ip = base + ms->nextToUpdate;25const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;26const U32 fastHashFillStep = 3;2728/* Always insert every fastHashFillStep position into the hash table.29* Insert the other positions if their hash entry is empty.30*/31for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {32U32 const current = (U32)(ip - base);33size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);34hashTable[hash0] = current;35if (dtlm == ZSTD_dtlm_fast) continue;36/* Only load extra positions for ZSTD_dtlm_full */37{ U32 p;38for (p = 1; p < fastHashFillStep; ++p) {39size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls);40if (hashTable[hash] == 0) { /* not yet filled */41hashTable[hash] = current + p;42} } } }43}444546FORCE_INLINE_TEMPLATE size_t47ZSTD_compressBlock_fast_generic(48ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],49void const* src, size_t srcSize,50U32 const mls)51{52const ZSTD_compressionParameters* const cParams = &ms->cParams;53U32* const hashTable = ms->hashTable;54U32 const hlog = cParams->hashLog;55/* support stepSize of 0 */56size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1;57const BYTE* const base = ms->window.base;58const BYTE* const istart = (const BYTE*)src;59/* We check ip0 (ip + 0) and ip1 (ip + 1) each loop */60const BYTE* ip0 = istart;61const BYTE* ip1;62const BYTE* anchor = istart;63const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);64const U32 prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);65const BYTE* const prefixStart = base + prefixStartIndex;66const BYTE* const iend = istart + srcSize;67const BYTE* const ilimit = iend - HASH_READ_SIZE;68U32 offset_1=rep[0], offset_2=rep[1];69U32 offsetSaved = 0;7071/* init */72DEBUGLOG(5, "ZSTD_compressBlock_fast_generic");73ip0 += (ip0 == prefixStart);74ip1 = ip0 + 1;75{ U32 const current = (U32)(ip0 - base);76U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, current, cParams->windowLog);77U32 const maxRep = current - windowLow;78if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;79if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;80}8182/* Main Search Loop */83#ifdef __INTEL_COMPILER84/* From intel 'The vector pragma indicates that the loop should be85* vectorized if it is legal to do so'. Can be used together with86* #pragma ivdep (but have opted to exclude that because intel87* warns against using it).*/88#pragma vector always89#endif90while (ip1 < ilimit) { /* < instead of <=, because check at ip0+2 */91size_t mLength;92BYTE const* ip2 = ip0 + 2;93size_t const h0 = ZSTD_hashPtr(ip0, hlog, mls);94U32 const val0 = MEM_read32(ip0);95size_t const h1 = ZSTD_hashPtr(ip1, hlog, mls);96U32 const val1 = MEM_read32(ip1);97U32 const current0 = (U32)(ip0-base);98U32 const current1 = (U32)(ip1-base);99U32 const matchIndex0 = hashTable[h0];100U32 const matchIndex1 = hashTable[h1];101BYTE const* repMatch = ip2 - offset_1;102const BYTE* match0 = base + matchIndex0;103const BYTE* match1 = base + matchIndex1;104U32 offcode;105106#if defined(__aarch64__)107PREFETCH_L1(ip0+256);108#endif109110hashTable[h0] = current0; /* update hash table */111hashTable[h1] = current1; /* update hash table */112113assert(ip0 + 1 == ip1);114115if ((offset_1 > 0) & (MEM_read32(repMatch) == MEM_read32(ip2))) {116mLength = (ip2[-1] == repMatch[-1]) ? 1 : 0;117ip0 = ip2 - mLength;118match0 = repMatch - mLength;119mLength += 4;120offcode = 0;121goto _match;122}123if ((matchIndex0 > prefixStartIndex) && MEM_read32(match0) == val0) {124/* found a regular match */125goto _offset;126}127if ((matchIndex1 > prefixStartIndex) && MEM_read32(match1) == val1) {128/* found a regular match after one literal */129ip0 = ip1;130match0 = match1;131goto _offset;132}133{ size_t const step = ((size_t)(ip0-anchor) >> (kSearchStrength - 1)) + stepSize;134assert(step >= 2);135ip0 += step;136ip1 += step;137continue;138}139_offset: /* Requires: ip0, match0 */140/* Compute the offset code */141offset_2 = offset_1;142offset_1 = (U32)(ip0-match0);143offcode = offset_1 + ZSTD_REP_MOVE;144mLength = 4;145/* Count the backwards match length */146while (((ip0>anchor) & (match0>prefixStart))147&& (ip0[-1] == match0[-1])) { ip0--; match0--; mLength++; } /* catch up */148149_match: /* Requires: ip0, match0, offcode */150/* Count the forward length */151mLength += ZSTD_count(ip0+mLength, match0+mLength, iend);152ZSTD_storeSeq(seqStore, (size_t)(ip0-anchor), anchor, iend, offcode, mLength-MINMATCH);153/* match found */154ip0 += mLength;155anchor = ip0;156157if (ip0 <= ilimit) {158/* Fill Table */159assert(base+current0+2 > istart); /* check base overflow */160hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */161hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);162163if (offset_2 > 0) { /* offset_2==0 means offset_2 is invalidated */164while ( (ip0 <= ilimit) && (MEM_read32(ip0) == MEM_read32(ip0 - offset_2)) ) {165/* store sequence */166size_t const rLength = ZSTD_count(ip0+4, ip0+4-offset_2, iend) + 4;167{ U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */168hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);169ip0 += rLength;170ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, 0 /*offCode*/, rLength-MINMATCH);171anchor = ip0;172continue; /* faster when present (confirmed on gcc-8) ... (?) */173} } }174ip1 = ip0 + 1;175}176177/* save reps for next block */178rep[0] = offset_1 ? offset_1 : offsetSaved;179rep[1] = offset_2 ? offset_2 : offsetSaved;180181/* Return the last literals size */182return (size_t)(iend - anchor);183}184185186size_t ZSTD_compressBlock_fast(187ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],188void const* src, size_t srcSize)189{190U32 const mls = ms->cParams.minMatch;191assert(ms->dictMatchState == NULL);192switch(mls)193{194default: /* includes case 3 */195case 4 :196return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4);197case 5 :198return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5);199case 6 :200return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6);201case 7 :202return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7);203}204}205206FORCE_INLINE_TEMPLATE207size_t ZSTD_compressBlock_fast_dictMatchState_generic(208ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],209void const* src, size_t srcSize, U32 const mls)210{211const ZSTD_compressionParameters* const cParams = &ms->cParams;212U32* const hashTable = ms->hashTable;213U32 const hlog = cParams->hashLog;214/* support stepSize of 0 */215U32 const stepSize = cParams->targetLength + !(cParams->targetLength);216const BYTE* const base = ms->window.base;217const BYTE* const istart = (const BYTE*)src;218const BYTE* ip = istart;219const BYTE* anchor = istart;220const U32 prefixStartIndex = ms->window.dictLimit;221const BYTE* const prefixStart = base + prefixStartIndex;222const BYTE* const iend = istart + srcSize;223const BYTE* const ilimit = iend - HASH_READ_SIZE;224U32 offset_1=rep[0], offset_2=rep[1];225U32 offsetSaved = 0;226227const ZSTD_matchState_t* const dms = ms->dictMatchState;228const ZSTD_compressionParameters* const dictCParams = &dms->cParams ;229const U32* const dictHashTable = dms->hashTable;230const U32 dictStartIndex = dms->window.dictLimit;231const BYTE* const dictBase = dms->window.base;232const BYTE* const dictStart = dictBase + dictStartIndex;233const BYTE* const dictEnd = dms->window.nextSrc;234const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase);235const U32 dictAndPrefixLength = (U32)(ip - prefixStart + dictEnd - dictStart);236const U32 dictHLog = dictCParams->hashLog;237238/* if a dictionary is still attached, it necessarily means that239* it is within window size. So we just check it. */240const U32 maxDistance = 1U << cParams->windowLog;241const U32 endIndex = (U32)((size_t)(ip - base) + srcSize);242assert(endIndex - prefixStartIndex <= maxDistance);243(void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */244245/* ensure there will be no no underflow246* when translating a dict index into a local index */247assert(prefixStartIndex >= (U32)(dictEnd - dictBase));248249/* init */250DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic");251ip += (dictAndPrefixLength == 0);252/* dictMatchState repCode checks don't currently handle repCode == 0253* disabling. */254assert(offset_1 <= dictAndPrefixLength);255assert(offset_2 <= dictAndPrefixLength);256257/* Main Search Loop */258while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */259size_t mLength;260size_t const h = ZSTD_hashPtr(ip, hlog, mls);261U32 const current = (U32)(ip-base);262U32 const matchIndex = hashTable[h];263const BYTE* match = base + matchIndex;264const U32 repIndex = current + 1 - offset_1;265const BYTE* repMatch = (repIndex < prefixStartIndex) ?266dictBase + (repIndex - dictIndexDelta) :267base + repIndex;268hashTable[h] = current; /* update hash table */269270if ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */271&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {272const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;273mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;274ip++;275ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);276} else if ( (matchIndex <= prefixStartIndex) ) {277size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls);278U32 const dictMatchIndex = dictHashTable[dictHash];279const BYTE* dictMatch = dictBase + dictMatchIndex;280if (dictMatchIndex <= dictStartIndex ||281MEM_read32(dictMatch) != MEM_read32(ip)) {282assert(stepSize >= 1);283ip += ((ip-anchor) >> kSearchStrength) + stepSize;284continue;285} else {286/* found a dict match */287U32 const offset = (U32)(current-dictMatchIndex-dictIndexDelta);288mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4;289while (((ip>anchor) & (dictMatch>dictStart))290&& (ip[-1] == dictMatch[-1])) {291ip--; dictMatch--; mLength++;292} /* catch up */293offset_2 = offset_1;294offset_1 = offset;295ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);296}297} else if (MEM_read32(match) != MEM_read32(ip)) {298/* it's not a match, and we're not going to check the dictionary */299assert(stepSize >= 1);300ip += ((ip-anchor) >> kSearchStrength) + stepSize;301continue;302} else {303/* found a regular match */304U32 const offset = (U32)(ip-match);305mLength = ZSTD_count(ip+4, match+4, iend) + 4;306while (((ip>anchor) & (match>prefixStart))307&& (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */308offset_2 = offset_1;309offset_1 = offset;310ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);311}312313/* match found */314ip += mLength;315anchor = ip;316317if (ip <= ilimit) {318/* Fill Table */319assert(base+current+2 > istart); /* check base overflow */320hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2; /* here because current+2 could be > iend-8 */321hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);322323/* check immediate repcode */324while (ip <= ilimit) {325U32 const current2 = (U32)(ip-base);326U32 const repIndex2 = current2 - offset_2;327const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?328dictBase - dictIndexDelta + repIndex2 :329base + repIndex2;330if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)331&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {332const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;333size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;334U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */335ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);336hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;337ip += repLength2;338anchor = ip;339continue;340}341break;342}343}344}345346/* save reps for next block */347rep[0] = offset_1 ? offset_1 : offsetSaved;348rep[1] = offset_2 ? offset_2 : offsetSaved;349350/* Return the last literals size */351return (size_t)(iend - anchor);352}353354size_t ZSTD_compressBlock_fast_dictMatchState(355ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],356void const* src, size_t srcSize)357{358U32 const mls = ms->cParams.minMatch;359assert(ms->dictMatchState != NULL);360switch(mls)361{362default: /* includes case 3 */363case 4 :364return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4);365case 5 :366return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5);367case 6 :368return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6);369case 7 :370return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7);371}372}373374375static size_t ZSTD_compressBlock_fast_extDict_generic(376ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],377void const* src, size_t srcSize, U32 const mls)378{379const ZSTD_compressionParameters* const cParams = &ms->cParams;380U32* const hashTable = ms->hashTable;381U32 const hlog = cParams->hashLog;382/* support stepSize of 0 */383U32 const stepSize = cParams->targetLength + !(cParams->targetLength);384const BYTE* const base = ms->window.base;385const BYTE* const dictBase = ms->window.dictBase;386const BYTE* const istart = (const BYTE*)src;387const BYTE* ip = istart;388const BYTE* anchor = istart;389const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);390const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);391const U32 dictStartIndex = lowLimit;392const BYTE* const dictStart = dictBase + dictStartIndex;393const U32 dictLimit = ms->window.dictLimit;394const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit;395const BYTE* const prefixStart = base + prefixStartIndex;396const BYTE* const dictEnd = dictBase + prefixStartIndex;397const BYTE* const iend = istart + srcSize;398const BYTE* const ilimit = iend - 8;399U32 offset_1=rep[0], offset_2=rep[1];400401DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic (offset_1=%u)", offset_1);402403/* switch to "regular" variant if extDict is invalidated due to maxDistance */404if (prefixStartIndex == dictStartIndex)405return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, mls);406407/* Search Loop */408while (ip < ilimit) { /* < instead of <=, because (ip+1) */409const size_t h = ZSTD_hashPtr(ip, hlog, mls);410const U32 matchIndex = hashTable[h];411const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;412const BYTE* match = matchBase + matchIndex;413const U32 current = (U32)(ip-base);414const U32 repIndex = current + 1 - offset_1;415const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;416const BYTE* const repMatch = repBase + repIndex;417hashTable[h] = current; /* update hash table */418DEBUGLOG(7, "offset_1 = %u , current = %u", offset_1, current);419420if ( ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */421& (offset_1 < current+1 - dictStartIndex) ) /* note: we are searching at current+1 */422&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {423const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;424size_t const rLength = ZSTD_count_2segments(ip+1 +4, repMatch +4, iend, repMatchEnd, prefixStart) + 4;425ip++;426ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, rLength-MINMATCH);427ip += rLength;428anchor = ip;429} else {430if ( (matchIndex < dictStartIndex) ||431(MEM_read32(match) != MEM_read32(ip)) ) {432assert(stepSize >= 1);433ip += ((ip-anchor) >> kSearchStrength) + stepSize;434continue;435}436{ const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;437const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;438U32 const offset = current - matchIndex;439size_t mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;440while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */441offset_2 = offset_1; offset_1 = offset; /* update offset history */442ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);443ip += mLength;444anchor = ip;445} }446447if (ip <= ilimit) {448/* Fill Table */449hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2;450hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);451/* check immediate repcode */452while (ip <= ilimit) {453U32 const current2 = (U32)(ip-base);454U32 const repIndex2 = current2 - offset_2;455const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;456if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (offset_2 < current - dictStartIndex)) /* intentional overflow */457&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {458const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;459size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;460{ U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */461ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, 0 /*offcode*/, repLength2-MINMATCH);462hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;463ip += repLength2;464anchor = ip;465continue;466}467break;468} } }469470/* save reps for next block */471rep[0] = offset_1;472rep[1] = offset_2;473474/* Return the last literals size */475return (size_t)(iend - anchor);476}477478479size_t ZSTD_compressBlock_fast_extDict(480ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],481void const* src, size_t srcSize)482{483U32 const mls = ms->cParams.minMatch;484switch(mls)485{486default: /* includes case 3 */487case 4 :488return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);489case 5 :490return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);491case 6 :492return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);493case 7 :494return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);495}496}497498499