From cc52a97214f53fe2605b3999686e0402cfcbf2b4 Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 19 Feb 2016 10:09:35 +0100 Subject: [PATCH 01/20] added hashLog3 --- lib/zstd_compress.c | 235 ++++++++++++++++++++++---------------------- lib/zstd_static.h | 3 + 2 files changed, 123 insertions(+), 115 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 7bea6abe..26898c59 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -74,24 +74,6 @@ size_t ZSTD_compressBound(size_t srcSize) { return FSE_compressBound(srcSize) + /*-************************************* * Sequence storage ***************************************/ -/** ZSTD_resetFreqs() : for opt variants */ -static void ZSTD_resetFreqs(seqStore_t* ssPtr) -{ - unsigned u; - ssPtr->matchLengthSum = 512; // (1<litLengthSum = 256; // (1<litSum = (1<offCodeSum = (1<litFreq[u] = 1; - for (u=0; u<=MaxLL; u++) - ssPtr->litLengthFreq[u] = 1; - for (u=0; u<=MaxML; u++) - ssPtr->matchLengthFreq[u] = 1; - for (u=0; u<=MaxOff; u++) - ssPtr->offCodeFreq[u] = 1; -} static void ZSTD_resetSeqStore(seqStore_t* ssPtr) { @@ -114,6 +96,7 @@ struct ZSTD_CCtx_s U32 dictLimit; /* below that point, need extDict */ U32 lowLimit; /* below that point, no more data */ U32 nextToUpdate; /* index from which to continue dictionary update */ + U32 nextToUpdate3; /* index from which to continue dictionary update */ U32 loadedDictEnd; U32 stage; ZSTD_parameters params; @@ -125,6 +108,7 @@ struct ZSTD_CCtx_s seqStore_t seqStore; /* sequences storage ptrs */ U32* hashTable; + U32* hashTable3; U32* contentTable; HUF_CElt* hufTable; U32 flagStaticTables; @@ -167,6 +151,7 @@ void ZSTD_validateParams(ZSTD_parameters* params) CLAMP(params->windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); CLAMP(params->contentLog, ZSTD_CONTENTLOG_MIN, ZSTD_CONTENTLOG_MAX); CLAMP(params->hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); + CLAMP(params->hashLog3, ZSTD_HASHLOG3_MIN, ZSTD_HASHLOG3_MAX); CLAMP(params->searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); CLAMP(params->searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); CLAMP(params->targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); @@ -188,7 +173,7 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, const size_t blockSize = MIN(BLOCKSIZE, (size_t)1 << params.windowLog); /* reserve table memory */ const U32 contentLog = (params.strategy == ZSTD_fast) ? 1 : params.contentLog; - const size_t tableSpace = ((1 << contentLog) + (1 << params.hashLog)) * sizeof(U32); + const size_t tableSpace = ((1 << contentLog) + (1 << params.hashLog) + (1 << params.hashLog3)) * sizeof(U32); const size_t neededSpace = tableSpace + (256*sizeof(U32)) + (3*blockSize) + ((1<workSpaceSize < neededSpace) { free(zc->workSpace); @@ -197,7 +182,8 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->workSpaceSize = neededSpace; } memset(zc->workSpace, 0, tableSpace ); /* reset only tables */ - zc->hashTable = (U32*)(zc->workSpace); + zc->hashTable3 = (U32*)(zc->workSpace); + zc->hashTable = zc->hashTable3 + ((size_t)1 << params.hashLog3); zc->contentTable = zc->hashTable + ((size_t)1 << params.hashLog); zc->seqStore.buffer = zc->contentTable + ((size_t)1 << contentLog); zc->hufTable = (HUF_CElt*)zc->seqStore.buffer; @@ -225,6 +211,7 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (blockSize>>2); zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); // zc->seqStore.XXX = zc->seqStore.dumpsStart + (blockSize>>4); + zc->seqStore.litLengthSum = 0; zc->hbSize = 0; zc->stage = 0; @@ -248,7 +235,7 @@ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx) ZSTD_resetCCtx_advanced(dstCCtx, srcCCtx->params); /* copy tables */ - memcpy(dstCCtx->hashTable, srcCCtx->hashTable, tableSpace); + memcpy(dstCCtx->workSpace, srcCCtx->workSpace, tableSpace); /* copy frame header */ dstCCtx->hbSize = srcCCtx->hbSize; @@ -555,6 +542,11 @@ size_t ZSTD_compressSequences(ZSTD_CCtx* zc, op += cSize; } +#if ZSTD_OPT_DEBUG >= 5 + if (nbSeq >= 32768) + printf("ERROR: nbSeq=%d\n", (int)nbSeq); +#endif + /* Sequences Header */ if ((oend-op) < MIN_SEQUENCES_SIZE) return ERROR(dstSize_tooSmall); if (nbSeq < 128) *op++ = (BYTE)nbSeq; @@ -732,7 +724,7 @@ MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const B if (g_start==NULL) g_start = literals; //if (literals - g_start == 8695) printf("pos %6u : %3u literals & match %3u bytes at distance %6u \n", - (U32)(literals - g_start), (U32)litLength, (U32)matchCode+4, (U32)offsetCode); + (U32)(literals - g_start), (U32)litLength, (U32)matchCode+MINMATCH, (U32)offsetCode); #endif /* copy Literals */ @@ -875,6 +867,10 @@ static size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE /*-************************************* * Hashes ***************************************/ +static const U32 prime3bytes = 506832829U; +static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32-24)) * prime3bytes) >> (32-h) ; } +static size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_read32(ptr), h); } + static const U32 prime4bytes = 2654435761U; static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; } static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_read32(ptr), h); } @@ -1414,6 +1410,10 @@ static size_t ZSTD_BtFindBestMatch_selectMLS_extDict ( FORCE_INLINE U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) { +#if MINMATCH == 3 + U32* const hashTable3 = zc->hashTable3; + const U32 hashLog3 = zc->params.hashLog3; +#endif U32* const hashTable = zc->hashTable; const U32 hashLog = zc->params.hashLog; U32* const chainTable = zc->contentTable; @@ -1426,6 +1426,9 @@ U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) size_t h = ZSTD_hashPtr(base+idx, hashLog, mls); NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; hashTable[h] = idx; +#if MINMATCH == 3 + hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx; +#endif idx++; } @@ -1433,6 +1436,8 @@ U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) return hashTable[ZSTD_hashPtr(ip, hashLog, mls)]; } +#include "zstd_opt.h" + FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ size_t ZSTD_HcFindBestMatch_generic ( @@ -1651,7 +1656,6 @@ _storeSequence: } } -#include "zstd_opt.h" static void ZSTD_compressBlock_opt_bt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) { @@ -2271,107 +2275,108 @@ size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSi #define ZSTD_MAX_CLEVEL 21 unsigned ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } + static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { /* "default" */ - /* l, W, C, H, S, L, SL, strat */ - { 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */ - { 0, 19, 13, 14, 1, 7, 4, ZSTD_fast }, /* level 1 */ - { 0, 19, 15, 16, 1, 6, 4, ZSTD_fast }, /* level 2 */ - { 0, 20, 18, 20, 1, 6, 4, ZSTD_fast }, /* level 3 */ - { 0, 21, 19, 21, 1, 6, 4, ZSTD_fast }, /* level 4 */ - { 0, 20, 14, 18, 3, 5, 4, ZSTD_greedy }, /* level 5 */ - { 0, 20, 18, 19, 3, 5, 4, ZSTD_greedy }, /* level 6 */ - { 0, 21, 17, 20, 3, 5, 4, ZSTD_lazy }, /* level 7 */ - { 0, 21, 19, 20, 3, 5, 4, ZSTD_lazy }, /* level 8 */ - { 0, 21, 20, 20, 3, 5, 4, ZSTD_lazy2 }, /* level 9 */ - { 0, 21, 19, 21, 4, 5, 4, ZSTD_lazy2 }, /* level 10 */ - { 0, 22, 20, 22, 4, 5, 4, ZSTD_lazy2 }, /* level 11 */ - { 0, 22, 20, 22, 5, 5, 4, ZSTD_lazy2 }, /* level 12 */ - { 0, 22, 21, 22, 5, 5, 4, ZSTD_lazy2 }, /* level 13 */ - { 0, 22, 22, 23, 5, 5, 4, ZSTD_lazy2 }, /* level 14 */ - { 0, 23, 23, 23, 5, 5, 4, ZSTD_lazy2 }, /* level 15 */ - { 0, 23, 22, 22, 5, 5, 4, ZSTD_btlazy2 }, /* level 16 */ - { 0, 24, 24, 23, 4, 5, 4, ZSTD_btlazy2 }, /* level 17 */ - { 0, 24, 24, 23, 5, 5, 30, ZSTD_btopt }, /* level 18 */ - { 0, 25, 25, 24, 5, 4, 40, ZSTD_btopt }, /* level 19 */ - { 0, 26, 26, 25, 8, 4,256, ZSTD_btopt }, /* level 20 */ - { 0, 26, 27, 25, 10, 4,256, ZSTD_btopt }, /* level 21 */ + /* l, W, C, H, H3, S, L, SL, strat */ + { 0, 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */ + { 0, 19, 13, 14, 0, 1, 7, 4, ZSTD_fast }, /* level 1 */ + { 0, 19, 15, 16, 0, 1, 6, 4, ZSTD_fast }, /* level 2 */ + { 0, 20, 18, 20, 0, 1, 6, 4, ZSTD_fast }, /* level 3 */ + { 0, 21, 19, 21, 0, 1, 6, 4, ZSTD_fast }, /* level 4 */ + { 0, 20, 14, 18, 0, 3, 5, 4, ZSTD_greedy }, /* level 5 */ + { 0, 20, 18, 19, 0, 3, 5, 4, ZSTD_greedy }, /* level 6 */ + { 0, 21, 17, 20, 0, 3, 5, 4, ZSTD_lazy }, /* level 7 */ + { 0, 21, 19, 20, 0, 3, 5, 4, ZSTD_lazy }, /* level 8 */ + { 0, 21, 20, 20, 0, 3, 5, 4, ZSTD_lazy2 }, /* level 9 */ + { 0, 21, 19, 21, 0, 4, 5, 4, ZSTD_lazy2 }, /* level 10 */ + { 0, 22, 20, 22, 0, 4, 5, 4, ZSTD_lazy2 }, /* level 11 */ + { 0, 22, 20, 22, 0, 5, 5, 4, ZSTD_lazy2 }, /* level 12 */ + { 0, 22, 21, 22, 0, 5, 5, 4, ZSTD_lazy2 }, /* level 13 */ + { 0, 22, 22, 23, 0, 5, 5, 4, ZSTD_lazy2 }, /* level 14 */ + { 0, 23, 23, 23, 0, 5, 5, 4, ZSTD_lazy2 }, /* level 15 */ + { 0, 23, 22, 22, 0, 5, 5, 4, ZSTD_btlazy2 }, /* level 16 */ + { 0, 24, 24, 23, 0, 4, 5, 4, ZSTD_btlazy2 }, /* level 17 */ + { 0, 24, 24, 23, 16, 5, 5, 30, ZSTD_btopt }, /* level 18 */ + { 0, 25, 25, 24, 16, 5, 4, 40, ZSTD_btopt }, /* level 19 */ + { 0, 26, 26, 25, 16, 8, 4,256, ZSTD_btopt }, /* level 20 */ + { 0, 26, 27, 25, 24, 10, 4,256, ZSTD_btopt }, /* level 21 */ }, { /* for srcSize <= 256 KB */ - /* l, W, C, H, S, L, T, strat */ - { 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 */ - { 0, 18, 14, 15, 1, 6, 4, ZSTD_fast }, /* level 1 */ - { 0, 18, 14, 16, 1, 5, 4, ZSTD_fast }, /* level 2 */ - { 0, 18, 14, 17, 1, 5, 4, ZSTD_fast }, /* level 3.*/ - { 0, 18, 14, 15, 4, 4, 4, ZSTD_greedy }, /* level 4 */ - { 0, 18, 16, 17, 4, 4, 4, ZSTD_greedy }, /* level 5 */ - { 0, 18, 17, 17, 3, 4, 4, ZSTD_lazy }, /* level 6 */ - { 0, 18, 17, 17, 4, 4, 4, ZSTD_lazy }, /* level 7 */ - { 0, 18, 17, 17, 4, 4, 4, ZSTD_lazy2 }, /* level 8 */ - { 0, 18, 17, 17, 5, 4, 4, ZSTD_lazy2 }, /* level 9 */ - { 0, 18, 17, 17, 6, 4, 4, ZSTD_lazy2 }, /* level 10 */ - { 0, 18, 17, 17, 7, 4, 4, ZSTD_lazy2 }, /* level 11 */ - { 0, 18, 18, 17, 4, 4, 4, ZSTD_btlazy2 }, /* level 12 */ - { 0, 18, 19, 17, 7, 4, 4, ZSTD_btlazy2 }, /* level 13.*/ - { 0, 18, 17, 19, 8, 4, 24, ZSTD_btopt }, /* level 14.*/ - { 0, 18, 19, 19, 8, 4, 48, ZSTD_btopt }, /* level 15.*/ - { 0, 18, 19, 18, 9, 4,128, ZSTD_btopt }, /* level 16.*/ - { 0, 18, 19, 18, 9, 4,192, ZSTD_btopt }, /* level 17.*/ - { 0, 18, 19, 18, 9, 4,256, ZSTD_btopt }, /* level 18.*/ - { 0, 18, 19, 18, 10, 4,256, ZSTD_btopt }, /* level 19.*/ - { 0, 18, 19, 18, 11, 4,256, ZSTD_btopt }, /* level 20.*/ - { 0, 18, 19, 18, 12, 4,256, ZSTD_btopt }, /* level 21.*/ + /* l, W, C, H, H3, S, L, T, strat */ + { 0, 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 */ + { 0, 18, 14, 15, 0, 1, 6, 4, ZSTD_fast }, /* level 1 */ + { 0, 18, 14, 16, 0, 1, 5, 4, ZSTD_fast }, /* level 2 */ + { 0, 18, 14, 17, 0, 1, 5, 4, ZSTD_fast }, /* level 3.*/ + { 0, 18, 14, 15, 0, 4, 4, 4, ZSTD_greedy }, /* level 4 */ + { 0, 18, 16, 17, 0, 4, 4, 4, ZSTD_greedy }, /* level 5 */ + { 0, 18, 17, 17, 0, 3, 4, 4, ZSTD_lazy }, /* level 6 */ + { 0, 18, 17, 17, 0, 4, 4, 4, ZSTD_lazy }, /* level 7 */ + { 0, 18, 17, 17, 0, 4, 4, 4, ZSTD_lazy2 }, /* level 8 */ + { 0, 18, 17, 17, 0, 5, 4, 4, ZSTD_lazy2 }, /* level 9 */ + { 0, 18, 17, 17, 0, 6, 4, 4, ZSTD_lazy2 }, /* level 10 */ + { 0, 18, 17, 17, 0, 7, 4, 4, ZSTD_lazy2 }, /* level 11 */ + { 0, 18, 18, 17, 0, 4, 4, 4, ZSTD_btlazy2 }, /* level 12 */ + { 0, 18, 19, 17, 0, 7, 4, 4, ZSTD_btlazy2 }, /* level 13.*/ + { 0, 18, 17, 19, 0, 8, 4, 24, ZSTD_btopt }, /* level 14.*/ + { 0, 18, 19, 19, 0, 8, 4, 48, ZSTD_btopt }, /* level 15.*/ + { 0, 18, 19, 18, 0, 9, 4,128, ZSTD_btopt }, /* level 16.*/ + { 0, 18, 19, 18, 0, 9, 4,192, ZSTD_btopt }, /* level 17.*/ + { 0, 18, 19, 18, 0, 9, 4,256, ZSTD_btopt }, /* level 18.*/ + { 0, 18, 19, 18, 0, 10, 4,256, ZSTD_btopt }, /* level 19.*/ + { 0, 18, 19, 18, 0, 11, 4,256, ZSTD_btopt }, /* level 20.*/ + { 0, 18, 19, 18, 0, 12, 4,256, ZSTD_btopt }, /* level 21.*/ }, { /* for srcSize <= 128 KB */ - /* l, W, C, H, S, L, T, strat */ - { 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */ - { 0, 17, 12, 13, 1, 6, 4, ZSTD_fast }, /* level 1 */ - { 0, 17, 13, 16, 1, 5, 4, ZSTD_fast }, /* level 2 */ - { 0, 17, 13, 14, 2, 5, 4, ZSTD_greedy }, /* level 3 */ - { 0, 17, 13, 15, 3, 4, 4, ZSTD_greedy }, /* level 4 */ - { 0, 17, 15, 17, 4, 4, 4, ZSTD_greedy }, /* level 5 */ - { 0, 17, 16, 17, 3, 4, 4, ZSTD_lazy }, /* level 6 */ - { 0, 17, 16, 17, 4, 4, 4, ZSTD_lazy }, /* level 7 */ - { 0, 17, 17, 16, 4, 4, 4, ZSTD_lazy2 }, /* level 8 */ - { 0, 17, 17, 16, 5, 4, 4, ZSTD_lazy2 }, /* level 9 */ - { 0, 17, 17, 16, 6, 4, 4, ZSTD_lazy2 }, /* level 10 */ - { 0, 17, 17, 17, 7, 4, 4, ZSTD_lazy2 }, /* level 11 */ - { 0, 17, 17, 17, 8, 4, 4, ZSTD_lazy2 }, /* level 12 */ - { 0, 17, 17, 17, 9, 4, 4, ZSTD_lazy2 }, /* level 13 */ - { 0, 17, 18, 16, 5, 4, 20, ZSTD_btopt }, /* level 14 */ - { 0, 17, 18, 16, 9, 4, 48, ZSTD_btopt }, /* level 15 */ - { 0, 17, 18, 17, 7, 4,128, ZSTD_btopt }, /* level 16 */ - { 0, 17, 18, 17, 8, 4,128, ZSTD_btopt }, /* level 17 */ - { 0, 17, 18, 17, 8, 4,256, ZSTD_btopt }, /* level 18 */ - { 0, 17, 18, 17, 9, 4,256, ZSTD_btopt }, /* level 19 */ - { 0, 17, 18, 17, 10, 4,512, ZSTD_btopt }, /* level 20 */ - { 0, 17, 18, 17, 11, 4,512, ZSTD_btopt }, /* level 21 */ + /* l, W, C, H, H3, S, L, T, strat */ + { 0, 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */ + { 0, 17, 12, 13, 0, 1, 6, 4, ZSTD_fast }, /* level 1 */ + { 0, 17, 13, 16, 0, 1, 5, 4, ZSTD_fast }, /* level 2 */ + { 0, 17, 13, 14, 0, 2, 5, 4, ZSTD_greedy }, /* level 3 */ + { 0, 17, 13, 15, 0, 3, 4, 4, ZSTD_greedy }, /* level 4 */ + { 0, 17, 15, 17, 0, 4, 4, 4, ZSTD_greedy }, /* level 5 */ + { 0, 17, 16, 17, 0, 3, 4, 4, ZSTD_lazy }, /* level 6 */ + { 0, 17, 16, 17, 0, 4, 4, 4, ZSTD_lazy }, /* level 7 */ + { 0, 17, 17, 16, 0, 4, 4, 4, ZSTD_lazy2 }, /* level 8 */ + { 0, 17, 17, 16, 0, 5, 4, 4, ZSTD_lazy2 }, /* level 9 */ + { 0, 17, 17, 16, 0, 6, 4, 4, ZSTD_lazy2 }, /* level 10 */ + { 0, 17, 17, 17, 0, 7, 4, 4, ZSTD_lazy2 }, /* level 11 */ + { 0, 17, 17, 17, 0, 8, 4, 4, ZSTD_lazy2 }, /* level 12 */ + { 0, 17, 17, 17, 0, 9, 4, 4, ZSTD_lazy2 }, /* level 13 */ + { 0, 17, 18, 16, 0, 5, 4, 20, ZSTD_btopt }, /* level 14 */ + { 0, 17, 18, 16, 0, 9, 4, 48, ZSTD_btopt }, /* level 15 */ + { 0, 17, 18, 17, 0, 7, 4,128, ZSTD_btopt }, /* level 16 */ + { 0, 17, 18, 17, 0, 8, 4,128, ZSTD_btopt }, /* level 17 */ + { 0, 17, 18, 17, 0, 8, 4,256, ZSTD_btopt }, /* level 18 */ + { 0, 17, 18, 17, 0, 9, 4,256, ZSTD_btopt }, /* level 19 */ + { 0, 17, 18, 17, 0, 10, 4,512, ZSTD_btopt }, /* level 20 */ + { 0, 17, 18, 17, 0, 11, 4,512, ZSTD_btopt }, /* level 21 */ }, { /* for srcSize <= 16 KB */ - /* l, W, C, H, S, L, T, strat */ - { 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 -- never used */ - { 0, 14, 14, 14, 1, 4, 4, ZSTD_fast }, /* level 1 */ - { 0, 14, 14, 15, 1, 4, 4, ZSTD_fast }, /* level 2 */ - { 0, 14, 13, 15, 4, 4, 4, ZSTD_greedy }, /* level 3 */ - { 0, 14, 14, 15, 3, 4, 4, ZSTD_lazy }, /* level 4 */ - { 0, 14, 14, 14, 6, 4, 4, ZSTD_lazy }, /* level 5 */ - { 0, 14, 14, 14, 5, 4, 4, ZSTD_lazy2 }, /* level 6 */ - { 0, 14, 14, 14, 7, 4, 4, ZSTD_lazy2 }, /* level 7 */ - { 0, 14, 14, 14, 8, 4, 4, ZSTD_lazy2 }, /* level 8 */ - { 0, 14, 14, 14, 9, 4, 4, ZSTD_lazy2 }, /* level 9 */ - { 0, 14, 14, 14, 10, 4, 4, ZSTD_lazy2 }, /* level 10 */ - { 0, 14, 14, 14, 11, 4, 4, ZSTD_lazy2 }, /* level 11 */ - { 0, 14, 15, 15, 12, 4, 32, ZSTD_btopt }, /* level 12 */ - { 0, 14, 15, 15, 12, 4, 64, ZSTD_btopt }, /* level 13 */ - { 0, 14, 15, 15, 12, 4, 96, ZSTD_btopt }, /* level 14 */ - { 0, 14, 15, 15, 12, 4,128, ZSTD_btopt }, /* level 15 */ - { 0, 14, 15, 15, 12, 4,256, ZSTD_btopt }, /* level 16 */ - { 0, 14, 15, 15, 13, 4,256, ZSTD_btopt }, /* level 17 */ - { 0, 14, 15, 15, 14, 4,256, ZSTD_btopt }, /* level 18 */ - { 0, 14, 15, 15, 15, 4,256, ZSTD_btopt }, /* level 19 */ - { 0, 14, 15, 15, 16, 4,256, ZSTD_btopt }, /* level 20 */ - { 0, 14, 15, 15, 17, 4,256, ZSTD_btopt }, /* level 21 */ + /* l, W, C, H, H3, S, L, T, strat */ + { 0, 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 -- never used */ + { 0, 14, 14, 14, 0, 1, 4, 4, ZSTD_fast }, /* level 1 */ + { 0, 14, 14, 15, 0, 1, 4, 4, ZSTD_fast }, /* level 2 */ + { 0, 14, 13, 15, 0, 4, 4, 4, ZSTD_greedy }, /* level 3 */ + { 0, 14, 14, 15, 0, 3, 4, 4, ZSTD_lazy }, /* level 4 */ + { 0, 14, 14, 14, 0, 6, 4, 4, ZSTD_lazy }, /* level 5 */ + { 0, 14, 14, 14, 0, 5, 4, 4, ZSTD_lazy2 }, /* level 6 */ + { 0, 14, 14, 14, 0, 7, 4, 4, ZSTD_lazy2 }, /* level 7 */ + { 0, 14, 14, 14, 0, 8, 4, 4, ZSTD_lazy2 }, /* level 8 */ + { 0, 14, 14, 14, 0, 9, 4, 4, ZSTD_lazy2 }, /* level 9 */ + { 0, 14, 14, 14, 0, 10, 4, 4, ZSTD_lazy2 }, /* level 10 */ + { 0, 14, 14, 14, 0, 11, 4, 4, ZSTD_lazy2 }, /* level 11 */ + { 0, 14, 15, 15, 0, 12, 4, 32, ZSTD_btopt }, /* level 12 */ + { 0, 14, 15, 15, 0, 12, 4, 64, ZSTD_btopt }, /* level 13 */ + { 0, 14, 15, 15, 0, 12, 4, 96, ZSTD_btopt }, /* level 14 */ + { 0, 14, 15, 15, 0, 12, 4,128, ZSTD_btopt }, /* level 15 */ + { 0, 14, 15, 15, 0, 12, 4,256, ZSTD_btopt }, /* level 16 */ + { 0, 14, 15, 15, 0, 13, 4,256, ZSTD_btopt }, /* level 17 */ + { 0, 14, 15, 15, 0, 14, 4,256, ZSTD_btopt }, /* level 18 */ + { 0, 14, 15, 15, 0, 15, 4,256, ZSTD_btopt }, /* level 19 */ + { 0, 14, 15, 15, 0, 16, 4,256, ZSTD_btopt }, /* level 20 */ + { 0, 14, 15, 15, 0, 17, 4,256, ZSTD_btopt }, /* level 21 */ }, }; diff --git a/lib/zstd_static.h b/lib/zstd_static.h index 61216535..6a92a458 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -64,6 +64,8 @@ extern "C" { #define ZSTD_CONTENTLOG_MIN 4 #define ZSTD_HASHLOG_MAX 28 #define ZSTD_HASHLOG_MIN 12 +#define ZSTD_HASHLOG3_MAX 24 +#define ZSTD_HASHLOG3_MIN 12 #define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1) #define ZSTD_SEARCHLOG_MIN 1 #define ZSTD_SEARCHLENGTH_MAX 7 @@ -80,6 +82,7 @@ typedef struct U32 windowLog; /* largest match distance : larger == more compression, more memory needed during decompression */ U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */ U32 hashLog; /* dispatch table : larger == faster, more memory */ + U32 hashLog3; /* dispatch table : larger == faster, more memory */ U32 searchLog; /* nb of searches : larger == more compression, slower */ U32 searchLength; /* match length searched : larger == faster decompression, sometimes less compression */ U32 targetLength; /* acceptable match size for optimal parser (only) : larger == more compression, slower */ From c3a9a9ca5636fd4ed480a04d39439c5200b74a30 Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 19 Feb 2016 11:05:25 +0100 Subject: [PATCH 02/20] ZSTD_rescaleFreqs and removed minml --- lib/zstd_internal.h | 5 + lib/zstd_opt.h | 377 +++++++++++++++++++++++++++++--------------- 2 files changed, 253 insertions(+), 129 deletions(-) diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 26fc8578..3d60e87b 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -50,6 +50,11 @@ /*-************************************* * Common constants ***************************************/ +#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 5 = check encoded sequences; 9 = full logs +#if ZSTD_OPT_DEBUG > 0 + #include /* for debug */ +#endif + #define ZSTD_DICT_MAGIC 0xEC30A435 #define KB *(1 <<10) diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index ec9a2a15..ba970c8f 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -33,9 +33,6 @@ /* Note : this file is intended to be included within zstd_compress.c */ -/*- Dependencies -*/ -#include /* for debug */ - /*- Local types -*/ typedef struct { @@ -55,13 +52,13 @@ typedef struct { /*- Constants -*/ -#define ZSTD_OPT_NUM (1<<12) -#define ZSTD_FREQ_THRESHOLD (256) +#define ZSTD_OPT_NUM (1<<12) +#define ZSTD_FREQ_START 1 +#define ZSTD_FREQ_STEP 1 +#define ZSTD_FREQ_DIV 6 /*- Debug -*/ -#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 5 = check encoded sequences - -#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=1 +#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) #define ZSTD_LOG_TRY_PRICE(...) printf(__VA_ARGS__) @@ -72,11 +69,18 @@ typedef struct { #endif -FORCE_INLINE U32 ZSTD_getLiteralPriceReal(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) +#if MINMATCH == 3 + #define MEM_readMINMATCH(ptr) ((U32)(MEM_read32(ptr)<<8)) +#else + #define MEM_readMINMATCH(ptr) (U32)(MEM_read32(ptr)) +#endif + + +FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) { U32 price, freq, u; - if (!litLength) return 1; /* special case */ + if (!litLength) return 0; /* special case */ /* literals */ price = litLength * ZSTD_highbit(seqStorePtr->litSum); @@ -84,7 +88,7 @@ FORCE_INLINE U32 ZSTD_getLiteralPriceReal(seqStore_t* seqStorePtr, U32 litLength price -= ZSTD_highbit(seqStorePtr->litFreq[literals[u]]); /* literal Length */ - price += ((litLength >= MaxLL)*8) + ((litLength >= 255+MaxLL)*16) + ((litLength>=(1<<15))*8); + price += ((litLength >= MaxLL)<<3) + ((litLength >= 255+MaxLL)<<4) + ((litLength>=(1<<15))<<3); if (litLength >= MaxLL) litLength = MaxLL; freq = seqStorePtr->litLengthFreq[litLength]; price += ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(freq); @@ -93,37 +97,65 @@ FORCE_INLINE U32 ZSTD_getLiteralPriceReal(seqStore_t* seqStorePtr, U32 litLength } -FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) -{ - if (seqStorePtr->litSum > ZSTD_FREQ_THRESHOLD) - return ZSTD_getLiteralPriceReal(seqStorePtr, litLength, literals); - /* backup eval */ - return 1 + (litLength<<3); -} - - -FORCE_INLINE U32 ZSTD_getMatchPriceReal(seqStore_t* seqStorePtr, U32 offset, U32 matchLength) +FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) { /* offset */ BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset) + 1 : 0; - U32 price = ZSTD_highbit(seqStorePtr->offCodeSum) - ZSTD_highbit(seqStorePtr->offCodeFreq[offCode]); - price += offCode; + U32 price = offCode + ZSTD_highbit(seqStorePtr->offCodeSum) - ZSTD_highbit(seqStorePtr->offCodeFreq[offCode]); /* match Length */ - price += ((matchLength >= MaxML)*8) + ((matchLength >= 255+MaxML)*16) + ((matchLength>=(1<<15))*8); + matchLength -= MINMATCH; + price += ((matchLength >= MaxML)<<3) + ((matchLength >= 255+MaxML)<<4) + ((matchLength>=(1<<15))<<3); if (matchLength >= MaxML) matchLength = MaxML; - price += ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); + price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); - return price; + return ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0) + (litLength==0) + price; } -FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) +MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) { - if (seqStorePtr->litSum > ZSTD_FREQ_THRESHOLD) - return ZSTD_getLiteralPriceReal(seqStorePtr, litLength, literals) + ZSTD_getMatchPriceReal(seqStorePtr, offset, matchLength); - /* backup eval */ - return (litLength<<3) + ZSTD_highbit((U32)matchLength+1) + Offbits + ZSTD_highbit((U32)offset+1); + unsigned u; + + // printf("matchLengthSum=%d litLengthSum=%d litSum=%d offCodeSum=%d\n", ssPtr->matchLengthSum, ssPtr->litLengthSum, ssPtr->litSum, ssPtr->offCodeSum); + + if (ssPtr->litLengthSum == 0) { + ssPtr->matchLengthSum = (1<litLengthSum = (1<litSum = (1<offCodeSum = (1<litFreq[u] = 1; + for (u=0; u<=MaxLL; u++) + ssPtr->litLengthFreq[u] = 1; + for (u=0; u<=MaxML; u++) + ssPtr->matchLengthFreq[u] = 1; + for (u=0; u<=MaxOff; u++) + ssPtr->offCodeFreq[u] = 1; + } else { + ssPtr->matchLengthSum = 0; + ssPtr->litLengthSum = 0; + ssPtr->litSum = 0; + ssPtr->offCodeSum = 0; + + for (u=0; u<=MaxLit; u++) { + ssPtr->litFreq[u] = ZSTD_FREQ_START + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litSum += ssPtr->litFreq[u]; + } + for (u=0; u<=MaxLL; u++) { + ssPtr->litLengthFreq[u] = ZSTD_FREQ_START + (ssPtr->litLengthFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litLengthSum += ssPtr->litLengthFreq[u]; + } + for (u=0; u<=MaxML; u++) { + ssPtr->matchLengthFreq[u] = ZSTD_FREQ_START + (ssPtr->matchLengthFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->matchLengthSum += ssPtr->matchLengthFreq[u]; + } + for (u=0; u<=MaxOff; u++) { + ssPtr->offCodeFreq[u] = ZSTD_FREQ_START + (ssPtr->offCodeFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->offCodeSum += ssPtr->offCodeFreq[u]; + } + } } @@ -132,29 +164,28 @@ MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const B U32 u; /* literals */ - seqStorePtr->litSum += litLength; + seqStorePtr->litSum += litLength * ZSTD_FREQ_STEP; for (u=0; u < litLength; u++) - seqStorePtr->litFreq[literals[u]]++; + seqStorePtr->litFreq[literals[u]] += ZSTD_FREQ_STEP; /* literal Length */ - seqStorePtr->litLengthSum++; + seqStorePtr->litLengthSum += ZSTD_FREQ_STEP; if (litLength >= MaxLL) - seqStorePtr->litLengthFreq[MaxLL]++; + seqStorePtr->litLengthFreq[MaxLL] += ZSTD_FREQ_STEP; else - seqStorePtr->litLengthFreq[litLength]++; + seqStorePtr->litLengthFreq[litLength] += ZSTD_FREQ_STEP; /* match offset */ - seqStorePtr->offCodeSum++; - BYTE offCode = (BYTE)ZSTD_highbit(offset) + 1; - if (offset==0) offCode=0; - seqStorePtr->offCodeFreq[offCode]++; + seqStorePtr->offCodeSum += ZSTD_FREQ_STEP; + BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset) + 1 : 0; + seqStorePtr->offCodeFreq[offCode] += ZSTD_FREQ_STEP; /* match Length */ - seqStorePtr->matchLengthSum++; + seqStorePtr->matchLengthSum += ZSTD_FREQ_STEP; if (matchLength >= MaxML) - seqStorePtr->matchLengthFreq[MaxML]++; + seqStorePtr->matchLengthFreq[MaxML] += ZSTD_FREQ_STEP; else - seqStorePtr->matchLengthFreq[matchLength]++; + seqStorePtr->matchLengthFreq[matchLength] += ZSTD_FREQ_STEP; } @@ -173,11 +204,33 @@ MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const B /*-************************************* * Binary Tree search ***************************************/ +/* Update hashTable3 up to ip (excluded) + Assumption : always within prefix (ie. not within extDict) */ +#if MINMATCH == 3 +static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_CCtx* zc, const BYTE* ip) +{ + U32* const hashTable3 = zc->hashTable3; + const U32 hashLog3 = zc->params.hashLog3; + const BYTE* const base = zc->base; + const U32 target = (U32)(ip - base); + U32 idx = zc->nextToUpdate3; + + while(idx < target) { + hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx; + idx++; + } + + zc->nextToUpdate3 = target; + return hashTable3[ZSTD_hash3Ptr(ip, hashLog3)]; +} +#endif + + static U32 ZSTD_insertBtAndGetAllMatches ( ZSTD_CCtx* zc, - const BYTE* const ip, const BYTE* const iend, + const BYTE* const ip, const BYTE* const iLimit, U32 nbCompares, const U32 mls, - U32 extDict, ZSTD_match_t* matches, size_t bestLength) + U32 extDict, ZSTD_match_t* matches) { const BYTE* const base = zc->base; const U32 current = (U32)(ip-base); @@ -201,9 +254,39 @@ static U32 ZSTD_insertBtAndGetAllMatches ( U32 dummy32; /* to be nullified at the end */ U32 mnum = 0; - bestLength = MINMATCH-1; + size_t bestLength = MINMATCH-1; hashTable[h] = current; /* Update Hash Table */ +#if MINMATCH == 3 + /* HC3 match finder */ + U32 matchIndex3 = ZSTD_insertAndFindFirstIndexHash3 (zc, ip); + + if (matchIndex3>windowLow) { + const BYTE* match; + size_t currentMl=0; + if ((!extDict) || matchIndex3 >= dictLimit) { + match = base + matchIndex3; + if (match[bestLength] == ip[bestLength]) currentMl = ZSTD_count(ip, match, iLimit); // faster + // if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) currentMl = ZSTD_count(ip+MINMATCH, match+MINMATCH, iLimit)+MINMATCH; // stronger + } else { + match = dictBase + matchIndex3; + if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */ + currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iLimit, dictEnd, prefixStart) + MINMATCH; + } + + /* save best solution */ + if (currentMl > bestLength) { + bestLength = currentMl; + matches[mnum].off = current - matchIndex3; + matches[mnum].len = (U32)currentMl; + matches[mnum].back = 0; + mnum++; + if (currentMl > ZSTD_OPT_NUM) return mnum; + if (ip+currentMl == iLimit) return mnum; /* best possible, and avoid read overflow*/ + } + } +#endif + while (nbCompares-- && (matchIndex > windowLow)) { U32* nextPtr = bt + 2*(matchIndex & btMask); size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ @@ -211,11 +294,16 @@ static U32 ZSTD_insertBtAndGetAllMatches ( if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { match = base + matchIndex; - if (match[matchLength] == ip[matchLength]) - matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; + if (match[matchLength] == ip[matchLength]) { +#if ZSTD_OPT_DEBUG >= 5 + if (memcmp(match, ip, matchLength) != 0) + printf("%d: ERROR: matchLength=%d ZSTD_count=%d\n", current, (int)matchLength, (int)ZSTD_count(ip, match, ip+matchLength)); +#endif + matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iLimit) +1; + } } else { match = dictBase + matchIndex; - matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); + matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iLimit, dictEnd, prefixStart); if (matchIndex+matchLength >= dictLimit) match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ } @@ -228,7 +316,7 @@ static U32 ZSTD_insertBtAndGetAllMatches ( matches[mnum].back = 0; mnum++; if (matchLength > ZSTD_OPT_NUM) break; - if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ + if (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */ break; /* drop, to guarantee consistency (miss a little bit of compression) */ } @@ -259,26 +347,26 @@ static U32 ZSTD_insertBtAndGetAllMatches ( static U32 ZSTD_BtGetAllMatches ( ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iLimit, - const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches, U32 minml) + const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches) { if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls); - return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 0, matches, minml); + return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 0, matches); } static U32 ZSTD_BtGetAllMatches_selectMLS ( ZSTD_CCtx* zc, /* Index table will be updated */ const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, U32 minml) + const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) { (void)iLowLimit; /* unused */ switch(matchLengthSearch) { default : - case 4 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minml); - case 5 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minml); - case 6 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minml); + case 4 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 4, matches); + case 5 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 5, matches); + case 6 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 6, matches); } } @@ -286,26 +374,26 @@ static U32 ZSTD_BtGetAllMatches_selectMLS ( static U32 ZSTD_BtGetAllMatches_extDict ( ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iLimit, - const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches, U32 minml) + const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches) { if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls); - return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 1, matches, minml); + return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 1, matches); } static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( ZSTD_CCtx* zc, /* Index table will be updated */ const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, U32 minml) + const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) { (void)iLowLimit; switch(matchLengthSearch) { default : - case 4 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minml); - case 5 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minml); - case 6 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minml); + case 4 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 4, matches); + case 5 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 5, matches); + case 6 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 6, matches); } } @@ -317,7 +405,7 @@ FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulati U32 ZSTD_HcGetAllMatches_generic ( ZSTD_CCtx* zc, /* Index table will be updated */ const BYTE* const ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 mls, const U32 extDict, ZSTD_match_t* matches, size_t minml) + const U32 maxNbAttempts, const U32 mls, const U32 extDict, ZSTD_match_t* matches) { U32* const chainTable = zc->contentTable; const U32 chainSize = (1U << zc->params.contentLog); @@ -335,11 +423,40 @@ U32 ZSTD_HcGetAllMatches_generic ( U32 mnum = 0; const BYTE* match; U32 nbAttempts=maxNbAttempts; - minml=MINMATCH-1; + size_t minml=MINMATCH-1; - /* HC4 match finder */ + /* HC4 match finder with update */ matchIndex = ZSTD_insertAndFindFirstIndex (zc, ip, mls); +#if MINMATCH == 3 + /* HC3 match finder */ + U32 matchIndex3 = zc->hashTable3[ZSTD_hash3Ptr(ip, zc->params.hashLog3)]; + + if (matchIndex3>lowLimit) { + size_t currentMl=0; + if ((!extDict) || matchIndex3 >= dictLimit) { + match = base + matchIndex3; + if (match[minml] == ip[minml]) currentMl = ZSTD_count(ip, match, iHighLimit); // faster + //if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) currentMl = ZSTD_count(ip+MINMATCH, match+MINMATCH, iHighLimit)+MINMATCH; // stronger + } else { + match = dictBase + matchIndex3; + if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */ + currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iHighLimit, dictEnd, prefixStart) + MINMATCH; + } + + /* save best solution */ + if (currentMl > minml) { + minml = currentMl; + matches[mnum].off = current - matchIndex3; + matches[mnum].len = (U32)currentMl; + matches[mnum].back = 0; + mnum++; + if (currentMl > ZSTD_OPT_NUM) return mnum; + if (ip+currentMl == iHighLimit) return mnum; /* best possible, and avoid read overflow*/ + } + } +#endif + while ((matchIndex>lowLimit) && (nbAttempts)) { size_t currentMl=0; int back = 0; @@ -347,16 +464,16 @@ U32 ZSTD_HcGetAllMatches_generic ( if ((!extDict) || matchIndex >= dictLimit) { match = base + matchIndex; if (match[minml] == ip[minml]) currentMl = ZSTD_count(ip, match, iHighLimit); if (currentMl>0) { // faster - //if (MEM_read32(match) == MEM_read32(ip)) { currentMl = ZSTD_count(ip+MINMATCH, match+MINMATCH, iHighLimit)+MINMATCH; // stronger - while ((match-back > prefixStart) && (ip-back > iLowLimit) && (ip[-back-1] == match[-back-1])) back++; - currentMl += back; + //if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) { currentMl = ZSTD_count(ip+MINMATCH, match+MINMATCH, iHighLimit)+MINMATCH; // stronger + while ((match+back > prefixStart) && (ip+back > iLowLimit) && (ip[back-1] == match[back-1])) back--; + currentMl += (U32)(-back); } } else { match = dictBase + matchIndex; - if (MEM_read32(match) == MEM_read32(ip)) { /* assumption : matchIndex <= dictLimit-4 (by table construction) */ + if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) { /* assumption : matchIndex <= dictLimit-4 (by table construction) */ currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iHighLimit, dictEnd, prefixStart) + MINMATCH; - while ((match-back > dictStart) && (ip-back > iLowLimit) && (ip[-back-1] == match[-back-1])) back++; /* backward match extension */ - currentMl += back; + while ((match+back > dictStart) && (ip+back > iLowLimit) && (ip[back-1] == match[back-1])) back--; /* backward match extension */ + currentMl += (U32)(-back); } } /* save best solution */ @@ -364,7 +481,7 @@ U32 ZSTD_HcGetAllMatches_generic ( minml = currentMl; matches[mnum].off = current - matchIndex; matches[mnum].len = (U32)currentMl; - matches[mnum].back = back; + matches[mnum].back = (U32)(-back); mnum++; if (currentMl > ZSTD_OPT_NUM) break; if (ip+currentMl == iHighLimit) break; /* best possible, and avoid read overflow*/ @@ -381,28 +498,28 @@ U32 ZSTD_HcGetAllMatches_generic ( static U32 ZSTD_HcGetAllMatches_selectMLS ( ZSTD_CCtx* zc, const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, U32 minml) + const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) { switch(matchLengthSearch) { default : - case 4 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 4, 0, matches, minml); - case 5 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 5, 0, matches, minml); - case 6 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 6, 0, matches, minml); + case 4 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 4, 0, matches); + case 5 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 5, 0, matches); + case 6 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 6, 0, matches); } } static U32 ZSTD_HcGetAllMatches_selectMLS_extDict ( ZSTD_CCtx* zc, const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches, U32 minml) + const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) { switch(matchLengthSearch) { default : - case 4 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 4, 1, matches, minml); - case 5 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 5, 1, matches, minml); - case 6 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 6, 1, matches, minml); + case 4 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 4, 1, matches); + case 5 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 5, 1, matches); + case 6 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 6, 1, matches); } } @@ -428,7 +545,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, const U32 mls = ctx->params.searchLength; typedef U32 (*getAllMatches_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLowLimit, const BYTE* iHighLimit, - U32 maxNbAttempts, U32 matchLengthSearch, ZSTD_match_t* matches, U32 minml); + U32 maxNbAttempts, U32 matchLengthSearch, ZSTD_match_t* matches); getAllMatches_f getAllMatches = searchMethod ? ZSTD_BtGetAllMatches_selectMLS : ZSTD_HcGetAllMatches_selectMLS; ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; @@ -439,10 +556,10 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, const U32 sufficient_len = ctx->params.targetLength; const U32 faster_get_matches = (ctx->params.strategy == ZSTD_opt); - /* init */ + ctx->nextToUpdate3 = ctx->nextToUpdate; ZSTD_resetSeqStore(seqStorePtr); - ZSTD_resetFreqs(seqStorePtr); + ZSTD_rescaleFreqs(seqStorePtr); if ((ip-base) < REPCODE_STARTVALUE) ip = base + REPCODE_STARTVALUE; /* Match Loop */ @@ -457,7 +574,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, opt[0].litlen = (U32)(ip - anchor); /* check repCode */ - if (MEM_read32(ip+1) == MEM_read32(ip+1 - rep_1)) { + if (MEM_readMINMATCH(ip+1) == MEM_readMINMATCH(ip+1 - rep_1)) { /* repcode : we take it */ mlen = (U32)ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-rep_1, iend) + MINMATCH; @@ -469,19 +586,17 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); /* note : macro modifies last_pos */ mlen--; } while (mlen >= MINMATCH); } - best_mlen = (last_pos) ? last_pos : MINMATCH; - if (faster_get_matches && last_pos) match_num = 0; else - match_num = getAllMatches(ctx, ip, ip, iend, maxSearches, mls, matches, best_mlen); /* first search (depth 0) */ + match_num = getAllMatches(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ ZSTD_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-base), match_num, last_pos); if (!last_pos && !match_num) { ip++; continue; } @@ -498,6 +613,8 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, goto _storeSequence; } + best_mlen = (last_pos) ? last_pos : MINMATCH; + // set prices using matches at position = 0 for (u = 0; u < match_num; u++) { mlen = (u>0) ? matches[u-1].len+1 : best_mlen; @@ -505,7 +622,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-base), matches[u].len, matches[u].off, (int)best_mlen, (int)last_pos); litlen = opt[0].litlen; while (mlen <= best_mlen) { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; @@ -570,7 +687,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: try REP1 rep=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); } - if (MEM_read32(inr) == MEM_read32(inr - cur_rep)) { // check rep + if (MEM_readMINMATCH(inr) == MEM_readMINMATCH(inr - cur_rep)) { // check rep mlen = (U32)ZSTD_count(inr+MINMATCH, inr+MINMATCH - cur_rep, iend) + MINMATCH; ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d rep=%d opt[%d].off=%d\n", (int)(inr-base), mlen, 0, opt[cur].rep, cur, opt[cur].off); @@ -585,19 +702,19 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (opt[cur].mlen == 1) { litlen = opt[cur].litlen; if (cur > litlen) { - price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen - MINMATCH); + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen); ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); } else { litlen = 0; - price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen - MINMATCH); - ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen - MINMATCH)); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen); + ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen)); } best_mlen = mlen; if (faster_get_matches) skip_num = best_mlen; - ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)(inr-base), mlen, 0, price, litlen, cur - litlen, opt[cur - litlen].price); + ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, 0, price, litlen); do { if (cur + mlen > last_pos || price <= opt[cur + mlen].price) @@ -608,9 +725,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (faster_get_matches && skip_num > 0) { skip_num--; continue; } - best_mlen = (best_mlen > MINMATCH) ? best_mlen : MINMATCH; - - match_num = getAllMatches(ctx, inr, ip, iend, maxSearches, mls, matches, best_mlen); + match_num = getAllMatches(ctx, inr, ip, iend, maxSearches, mls, matches); ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { @@ -621,6 +736,8 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, goto _storeSequence; } + best_mlen = (best_mlen > MINMATCH) ? best_mlen : MINMATCH; + /* set prices using matches at position = cur */ for (u = 0; u < match_num; u++) { mlen = (u>0) ? matches[u-1].len+1 : best_mlen; @@ -635,15 +752,15 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (opt[cur2].mlen == 1) { litlen = opt[cur2].litlen; if (cur2 > litlen) - price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen - MINMATCH); + price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen); else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); } else { litlen = 0; - price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen - MINMATCH); + price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); } - ZSTD_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)(inr-base), matches[u].back, mlen, best_mlen, matches[u].off, price, litlen, cur - litlen, opt[cur - litlen].price); + ZSTD_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), matches[u].back, mlen, best_mlen, matches[u].off, price, litlen); ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur2 + mlen, opt[cur2 + mlen].price); if (cur2 + mlen > last_pos || (price < opt[cur2 + mlen].price)) @@ -691,7 +808,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ cur += mlen; U32 litLength = (U32)(ip - anchor); - ZSTD_LOG_ENCODE("%d/%d: ENCODE1 literals=%d mlen=%d off=%d rep1=%d rep2=%d\n", (int)(ip-base), (int)(iend-base), (int)(litLength), (int)mlen, (int)(offset), (int)rep_1, (int)rep_2); + ZSTD_LOG_ENCODE("%d/%d: ENCODE literals=%d mlen=%d off=%d rep1=%d rep2=%d\n", (int)(ip-base), (int)(iend-base), (int)(litLength), (int)mlen, (int)(offset), (int)rep_1, (int)rep_2); if (offset) { rep_2 = rep_1; @@ -703,14 +820,15 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ rep_1 = best_off; } } - ZSTD_LOG_ENCODE("%d/%d: ENCODE2 literals=%d mlen=%d off=%d rep1=%d rep2=%d\n", (int)(ip-base), (int)(iend-base), (int)(litLength), (int)mlen, (int)(offset), (int)rep_1, (int)rep_2); + // ZSTD_LOG_ENCODE("%d/%d: ENCODE2 literals=%d mlen=%d off=%d rep1=%d rep2=%d\n", (int)(ip-base), (int)(iend-base), (int)(litLength), (int)mlen, (int)(offset), (int)rep_1, (int)rep_2); #if ZSTD_OPT_DEBUG >= 5 - int ml2; + U32 ml2; if (offset) - ml2 = ZSTD_count(ip, ip-offset, iend); + ml2 = (U32)ZSTD_count(ip, ip-offset, iend); else - ml2 = ZSTD_count(ip, ip-rep_1, iend); + ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); + if (offset == 0 || offset >= 8) if (ml2 < mlen && ml2 < MINMATCH) { printf("%d: ERROR iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { @@ -732,7 +850,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ /* check immediate repcode */ while ( (anchor <= ilimit) - && (MEM_read32(anchor) == MEM_read32(anchor - rep_2)) ) { + && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(anchor - rep_2)) ) { /* store sequence */ best_mlen = (U32)ZSTD_count(anchor+MINMATCH, anchor+MINMATCH-rep_2, iend); best_off = rep_2; @@ -778,7 +896,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, const U32 mls = ctx->params.searchLength; typedef U32 (*getAllMatches_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLowLimit, const BYTE* iHighLimit, - U32 maxNbAttempts, U32 matchLengthSearch, ZSTD_match_t* matches, U32 minml); + U32 maxNbAttempts, U32 matchLengthSearch, ZSTD_match_t* matches); getAllMatches_f getAllMatches = searchMethod ? ZSTD_BtGetAllMatches_selectMLS_extDict : ZSTD_HcGetAllMatches_selectMLS_extDict; ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; @@ -790,8 +908,9 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, const U32 faster_get_matches = (ctx->params.strategy == ZSTD_opt); /* init */ + ctx->nextToUpdate3 = ctx->nextToUpdate; ZSTD_resetSeqStore(seqStorePtr); - ZSTD_resetFreqs(seqStorePtr); + ZSTD_rescaleFreqs(seqStorePtr); if ((ip - prefixStart) < REPCODE_STARTVALUE) ip += REPCODE_STARTVALUE; /* Match Loop */ @@ -810,7 +929,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; const BYTE* const repMatch = repBase + repIndex; if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ - && (MEM_read32(ip+1) == MEM_read32(repMatch)) ) { + && (MEM_readMINMATCH(ip+1) == MEM_readMINMATCH(repMatch)) ) { /* repcode detected we should take it */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; mlen = (U32)ZSTD_count_2segments(ip+1+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; @@ -823,7 +942,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); mlen--; @@ -835,7 +954,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, if (faster_get_matches && last_pos) match_num = 0; else - match_num = getAllMatches(ctx, ip, ip, iend, maxSearches, mls, matches, best_mlen); /* first search (depth 0) */ + match_num = getAllMatches(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ ZSTD_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-base), match_num, last_pos); if (!last_pos && !match_num) { ip++; continue; } @@ -859,7 +978,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-base), matches[u].len, matches[u].off, (int)best_mlen, (int)last_pos); litlen = opt[0].litlen; while (mlen <= best_mlen) { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; @@ -931,7 +1050,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; const BYTE* const repMatch = repBase + repIndex; if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ - &&(MEM_read32(inr) == MEM_read32(repMatch)) ) { + &&(MEM_readMINMATCH(inr) == MEM_readMINMATCH(repMatch)) ) { /* repcode detected */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; mlen = (U32)ZSTD_count_2segments(inr+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; @@ -948,20 +1067,20 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, if (opt[cur].mlen == 1) { litlen = opt[cur].litlen; if (cur > litlen) { - price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen - MINMATCH); + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen); ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); } else { litlen = 0; - price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen - MINMATCH); - ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen - MINMATCH)); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen); + ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen)); } best_mlen = mlen; if (faster_get_matches) skip_num = best_mlen; - ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)(inr-base), mlen, 0, price, litlen, cur - litlen, opt[cur - litlen].price); + ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, 0, price, litlen); do { if (cur + mlen > last_pos || price <= opt[cur + mlen].price) // || ((price == opt[cur + mlen].price) && (opt[cur].mlen == 1) && (cur != litlen))) // at equal price prefer REP instead of MATCH @@ -974,7 +1093,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, best_mlen = (best_mlen > MINMATCH) ? best_mlen : MINMATCH; - match_num = getAllMatches(ctx, inr, ip, iend, maxSearches, mls, matches, best_mlen); + match_num = getAllMatches(ctx, inr, ip, iend, maxSearches, mls, matches); ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { @@ -999,15 +1118,15 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, if (opt[cur2].mlen == 1) { litlen = opt[cur2].litlen; if (cur2 > litlen) - price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen - MINMATCH); + price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen); else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen - MINMATCH); + price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); } else { litlen = 0; - price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen - MINMATCH); + price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); } - ZSTD_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)(inr-base), matches[u].back, mlen, best_mlen, matches[u].off, price, litlen, cur - litlen, opt[cur - litlen].price); + ZSTD_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), matches[u].back, mlen, best_mlen, matches[u].off, price, litlen); ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur2 + mlen, opt[cur2 + mlen].price); if (cur2 + mlen > last_pos || (price < opt[cur2 + mlen].price)) @@ -1070,11 +1189,11 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set ZSTD_LOG_ENCODE("%d/%d: ENCODE2 literals=%d mlen=%d off=%d rep1=%d rep2=%d\n", (int)(ip-base), (int)(iend-base), (int)(litLength), (int)mlen, (int)(offset), (int)rep_1, (int)rep_2); #if ZSTD_OPT_DEBUG >= 5 - int ml2; + U32 ml2; if (offset) - ml2 = ZSTD_count(ip, ip-offset, iend); + ml2 = (U32)ZSTD_count(ip, ip-offset, iend); else - ml2 = ZSTD_count(ip, ip-rep_1, iend); + ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); if (ml2 < mlen && ml2 < MINMATCH) { printf("%d: ERROR iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { @@ -1100,7 +1219,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; const BYTE* const repMatch = repBase + repIndex; if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ - && (MEM_read32(anchor) == MEM_read32(repMatch)) ) { + && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(repMatch)) ) { /* repcode detected, let's take it */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; mlen = (U32)ZSTD_count_2segments(anchor+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; From fcafb26b2e6afed63de1528274d328705d18783f Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 19 Feb 2016 11:59:44 +0100 Subject: [PATCH 03/20] speed optimiaztions --- lib/zstd_opt.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index ba970c8f..98ed77d6 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -78,9 +78,7 @@ typedef struct { FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) { - U32 price, freq, u; - - if (!litLength) return 0; /* special case */ + U32 price, u; /* literals */ price = litLength * ZSTD_highbit(seqStorePtr->litSum); @@ -90,8 +88,7 @@ FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, co /* literal Length */ price += ((litLength >= MaxLL)<<3) + ((litLength >= 255+MaxLL)<<4) + ((litLength>=(1<<15))<<3); if (litLength >= MaxLL) litLength = MaxLL; - freq = seqStorePtr->litLengthFreq[litLength]; - price += ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(freq); + price += ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]); return price; } @@ -107,9 +104,12 @@ FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYT matchLength -= MINMATCH; price += ((matchLength >= MaxML)<<3) + ((matchLength >= 255+MaxML)<<4) + ((matchLength>=(1<<15))<<3); if (matchLength >= MaxML) matchLength = MaxML; - price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); + price += ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); - return ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0) + (litLength==0) + price; + if (!litLength) + return price + 1; /* special case */ + + return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); //((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0) + (litLength==0) + price; } From 6291c54006e21abd4b516ec0a96488c4056ead46 Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 19 Feb 2016 18:24:14 +0100 Subject: [PATCH 04/20] litlen bounded to 128 bytes --- lib/zstd_internal.h | 4 ++-- lib/zstd_opt.h | 32 ++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 3d60e87b..620572e5 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -50,7 +50,7 @@ /*-************************************* * Common constants ***************************************/ -#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 5 = check encoded sequences; 9 = full logs +#define ZSTD_OPT_DEBUG 1 // 1 = tableID=0; 5 = check encoded sequences; 9 = full logs #if ZSTD_OPT_DEBUG > 0 #include /* for debug */ #endif @@ -79,7 +79,7 @@ static const size_t ZSTD_frameHeaderSize_min = 5; #define IS_RAW 2 #define IS_RLE 3 -#define MINMATCH 4 +#define MINMATCH 3 #define REPCODE_STARTVALUE 1 #define Litbits 8 diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 98ed77d6..a25e4642 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -107,9 +107,9 @@ FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYT price += ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); if (!litLength) - return price + 1; /* special case */ + return price + 1 + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); - return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); //((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0) + (litLength==0) + price; + return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); } @@ -536,6 +536,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, const BYTE* const istart = (const BYTE*)src; const BYTE* ip = istart; const BYTE* anchor = istart; + const BYTE* litstart; const BYTE* const iend = istart + srcSize; const BYTE* const ilimit = iend - 8; const BYTE* const base = ctx->base + ctx->dictLimit; @@ -571,7 +572,8 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, memset(opt, 0, sizeof(ZSTD_optimal_t)); last_pos = 0; inr = ip; - opt[0].litlen = (U32)(ip - anchor); + litstart = ((U32)(ip - anchor) > 128) ? ip - 128 : anchor; + opt[0].litlen = (U32)(ip - litstart); /* check repCode */ if (MEM_readMINMATCH(ip+1) == MEM_readMINMATCH(ip+1 - rep_1)) { @@ -586,7 +588,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); /* note : macro modifies last_pos */ mlen--; @@ -622,7 +624,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-base), matches[u].len, matches[u].off, (int)best_mlen, (int)last_pos); litlen = opt[0].litlen; while (mlen <= best_mlen) { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; @@ -641,7 +643,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-litlen); ZSTD_LOG_TRY_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getLiteralPrice(seqStorePtr, litlen, anchor); + price = ZSTD_getLiteralPrice(seqStorePtr, litlen, litstart); } else { litlen = 1; price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1); @@ -705,7 +707,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen); ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); } else { litlen = 0; price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen); @@ -754,7 +756,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (cur2 > litlen) price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen); else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); } else { litlen = 0; price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); @@ -883,6 +885,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, const BYTE* const istart = (const BYTE*)src; const BYTE* ip = istart; const BYTE* anchor = istart; + const BYTE* litstart; const BYTE* const iend = istart + srcSize; const BYTE* const ilimit = iend - 8; const BYTE* const base = ctx->base; @@ -921,7 +924,8 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, memset(opt, 0, sizeof(ZSTD_optimal_t)); last_pos = 0; inr = ip; - opt[0].litlen = (U32)(ip - anchor); + litstart = ((U32)(ip - anchor) > 128) ? ip - 128 : anchor; + opt[0].litlen = (U32)(ip - litstart); /* check repCode */ { @@ -942,7 +946,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); mlen--; @@ -978,7 +982,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-base), matches[u].len, matches[u].off, (int)best_mlen, (int)last_pos); litlen = opt[0].litlen; while (mlen <= best_mlen) { - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; @@ -1000,7 +1004,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-litlen); ZSTD_LOG_TRY_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getLiteralPrice(seqStorePtr, litlen, anchor); + price = ZSTD_getLiteralPrice(seqStorePtr, litlen, litstart); } else { litlen = 1; price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1); @@ -1070,7 +1074,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen); ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, 0, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); } else { litlen = 0; price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen); @@ -1120,7 +1124,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, if (cur2 > litlen) price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen); else - price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off, mlen); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); } else { litlen = 0; price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); From d3b8d7a4e533eeb29a4df93cf6ef870a4a1bb9ad Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 22 Feb 2016 10:06:17 +0100 Subject: [PATCH 05/20] removed ZSTD_opt parser --- lib/zstd_compress.c | 25 ++----- lib/zstd_opt.h | 174 ++++---------------------------------------- lib/zstd_static.h | 2 +- 3 files changed, 21 insertions(+), 180 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 26898c59..8c917752 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -1657,14 +1657,9 @@ _storeSequence: } -static void ZSTD_compressBlock_opt_bt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +static void ZSTD_compressBlock_btopt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) { - ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 1, 2); -} - -static void ZSTD_compressBlock_opt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) -{ - ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 0, 2); + ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 2); } static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) @@ -1881,14 +1876,9 @@ static void ZSTD_compressBlock_btlazy2_extDict(ZSTD_CCtx* ctx, const void* src, ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 1, 2); } -static void ZSTD_compressBlock_opt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) +static void ZSTD_compressBlock_btopt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) { - ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 0, 2); -} - -static void ZSTD_compressBlock_opt_bt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) -{ - ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 1, 2); + ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 2); } @@ -1896,9 +1886,9 @@ typedef void (*ZSTD_blockCompressor) (ZSTD_CCtx* ctx, const void* src, size_t sr static ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict) { - static const ZSTD_blockCompressor blockCompressor[2][7] = { - { ZSTD_compressBlock_fast, ZSTD_compressBlock_greedy, ZSTD_compressBlock_lazy,ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, ZSTD_compressBlock_opt, ZSTD_compressBlock_opt_bt }, - { ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_greedy_extDict, ZSTD_compressBlock_lazy_extDict,ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, ZSTD_compressBlock_opt_extDict, ZSTD_compressBlock_opt_bt_extDict } + static const ZSTD_blockCompressor blockCompressor[2][6] = { + { ZSTD_compressBlock_fast, ZSTD_compressBlock_greedy, ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, ZSTD_compressBlock_btopt }, + { ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_greedy_extDict, ZSTD_compressBlock_lazy_extDict,ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, ZSTD_compressBlock_btopt_extDict } }; return blockCompressor[extDict][(U32)strat]; @@ -2062,7 +2052,6 @@ static size_t ZSTD_loadDictionaryContent(ZSTD_CCtx* zc, const void* src, size_t case ZSTD_greedy: case ZSTD_lazy: case ZSTD_lazy2: - case ZSTD_opt: ZSTD_insertAndFindFirstIndex (zc, iend-8, zc->params.searchLength); break; diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index a25e4642..accefe52 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -398,139 +398,13 @@ static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( } -/* *********************** -* Hash Chain -*************************/ -FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ -U32 ZSTD_HcGetAllMatches_generic ( - ZSTD_CCtx* zc, /* Index table will be updated */ - const BYTE* const ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 mls, const U32 extDict, ZSTD_match_t* matches) -{ - U32* const chainTable = zc->contentTable; - const U32 chainSize = (1U << zc->params.contentLog); - const U32 chainMask = chainSize-1; - const BYTE* const base = zc->base; - const BYTE* const dictBase = zc->dictBase; - const U32 dictLimit = zc->dictLimit; - const BYTE* const prefixStart = base + dictLimit; - const BYTE* const dictEnd = dictBase + dictLimit; - const BYTE* const dictStart = dictBase + zc->lowLimit; - const U32 lowLimit = zc->lowLimit; - const U32 current = (U32)(ip-base); - const U32 minChain = current > chainSize ? current - chainSize : 0; - U32 matchIndex; - U32 mnum = 0; - const BYTE* match; - U32 nbAttempts=maxNbAttempts; - size_t minml=MINMATCH-1; - - /* HC4 match finder with update */ - matchIndex = ZSTD_insertAndFindFirstIndex (zc, ip, mls); - -#if MINMATCH == 3 - /* HC3 match finder */ - U32 matchIndex3 = zc->hashTable3[ZSTD_hash3Ptr(ip, zc->params.hashLog3)]; - - if (matchIndex3>lowLimit) { - size_t currentMl=0; - if ((!extDict) || matchIndex3 >= dictLimit) { - match = base + matchIndex3; - if (match[minml] == ip[minml]) currentMl = ZSTD_count(ip, match, iHighLimit); // faster - //if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) currentMl = ZSTD_count(ip+MINMATCH, match+MINMATCH, iHighLimit)+MINMATCH; // stronger - } else { - match = dictBase + matchIndex3; - if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */ - currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iHighLimit, dictEnd, prefixStart) + MINMATCH; - } - - /* save best solution */ - if (currentMl > minml) { - minml = currentMl; - matches[mnum].off = current - matchIndex3; - matches[mnum].len = (U32)currentMl; - matches[mnum].back = 0; - mnum++; - if (currentMl > ZSTD_OPT_NUM) return mnum; - if (ip+currentMl == iHighLimit) return mnum; /* best possible, and avoid read overflow*/ - } - } -#endif - - while ((matchIndex>lowLimit) && (nbAttempts)) { - size_t currentMl=0; - int back = 0; - nbAttempts--; - if ((!extDict) || matchIndex >= dictLimit) { - match = base + matchIndex; - if (match[minml] == ip[minml]) currentMl = ZSTD_count(ip, match, iHighLimit); if (currentMl>0) { // faster - //if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) { currentMl = ZSTD_count(ip+MINMATCH, match+MINMATCH, iHighLimit)+MINMATCH; // stronger - while ((match+back > prefixStart) && (ip+back > iLowLimit) && (ip[back-1] == match[back-1])) back--; - currentMl += (U32)(-back); - } - } else { - match = dictBase + matchIndex; - if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) { /* assumption : matchIndex <= dictLimit-4 (by table construction) */ - currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iHighLimit, dictEnd, prefixStart) + MINMATCH; - while ((match+back > dictStart) && (ip+back > iLowLimit) && (ip[back-1] == match[back-1])) back--; /* backward match extension */ - currentMl += (U32)(-back); - } } - - /* save best solution */ - if (currentMl > minml) { - minml = currentMl; - matches[mnum].off = current - matchIndex; - matches[mnum].len = (U32)currentMl; - matches[mnum].back = (U32)(-back); - mnum++; - if (currentMl > ZSTD_OPT_NUM) break; - if (ip+currentMl == iHighLimit) break; /* best possible, and avoid read overflow*/ - } - - if (matchIndex <= minChain) break; - matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask); - } - - return mnum; -} - - -static U32 ZSTD_HcGetAllMatches_selectMLS ( - ZSTD_CCtx* zc, - const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) -{ - switch(matchLengthSearch) - { - default : - case 4 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 4, 0, matches); - case 5 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 5, 0, matches); - case 6 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 6, 0, matches); - } -} - -static U32 ZSTD_HcGetAllMatches_selectMLS_extDict ( - ZSTD_CCtx* zc, - const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, - const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) -{ - switch(matchLengthSearch) - { - default : - case 4 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 4, 1, matches); - case 5 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 5, 1, matches); - case 6 : return ZSTD_HcGetAllMatches_generic(zc, ip, iLowLimit, iHighLimit, maxNbAttempts, 6, 1, matches); - } -} - - /*-******************************* * Optimal parser *********************************/ FORCE_INLINE void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, const void* src, size_t srcSize, - const U32 searchMethod, const U32 depth) + const U32 depth) { seqStore_t* seqStorePtr = &(ctx->seqStore); const BYTE* const istart = (const BYTE*)src; @@ -545,17 +419,12 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, const U32 maxSearches = 1U << ctx->params.searchLog; const U32 mls = ctx->params.searchLength; - typedef U32 (*getAllMatches_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLowLimit, const BYTE* iHighLimit, - U32 maxNbAttempts, U32 matchLengthSearch, ZSTD_match_t* matches); - getAllMatches_f getAllMatches = searchMethod ? ZSTD_BtGetAllMatches_selectMLS : ZSTD_HcGetAllMatches_selectMLS; - ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; ZSTD_match_t matches[ZSTD_OPT_NUM+1]; const BYTE* inr; - U32 skip_num, cur, cur2, match_num, last_pos, litlen, price; + U32 cur, cur2, match_num, last_pos, litlen, price; const U32 sufficient_len = ctx->params.targetLength; - const U32 faster_get_matches = (ctx->params.strategy == ZSTD_opt); /* init */ ctx->nextToUpdate3 = ctx->nextToUpdate; @@ -595,10 +464,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, } while (mlen >= MINMATCH); } - if (faster_get_matches && last_pos) - match_num = 0; - else - match_num = getAllMatches(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ + match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ ZSTD_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-base), match_num, last_pos); if (!last_pos && !match_num) { ip++; continue; } @@ -633,7 +499,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (last_pos < MINMATCH) { ip++; continue; } /* check further positions */ - for (skip_num = 0, cur = 1; cur <= last_pos; cur++) { + for (cur = 1; cur <= last_pos; cur++) { size_t cur_rep; inr = ip + cur; @@ -715,8 +581,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, } best_mlen = mlen; - if (faster_get_matches) skip_num = best_mlen; - ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, 0, price, litlen); + ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, 0, price, litlen); do { if (cur + mlen > last_pos || price <= opt[cur + mlen].price) @@ -725,9 +590,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, } while (mlen >= MINMATCH); } - if (faster_get_matches && skip_num > 0) { skip_num--; continue; } - - match_num = getAllMatches(ctx, inr, ip, iend, maxSearches, mls, matches); + match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, ip, iend, maxSearches, mls, matches); ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { @@ -769,7 +632,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, SET_PRICE(cur2 + mlen, mlen, matches[u].off, litlen, price); mlen++; - } } } // for (skip_num = 0, cur = 1; cur <= last_pos; cur++) + } } } // for (cur = 1; cur <= last_pos; cur++) best_mlen = opt[last_pos].mlen; best_off = opt[last_pos].off; @@ -879,7 +742,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ FORCE_INLINE void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, const void* src, size_t srcSize, - const U32 searchMethod, const U32 depth) + const U32 depth) { seqStore_t* seqStorePtr = &(ctx->seqStore); const BYTE* const istart = (const BYTE*)src; @@ -898,17 +761,12 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, const U32 maxSearches = 1U << ctx->params.searchLog; const U32 mls = ctx->params.searchLength; - typedef U32 (*getAllMatches_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLowLimit, const BYTE* iHighLimit, - U32 maxNbAttempts, U32 matchLengthSearch, ZSTD_match_t* matches); - getAllMatches_f getAllMatches = searchMethod ? ZSTD_BtGetAllMatches_selectMLS_extDict : ZSTD_HcGetAllMatches_selectMLS_extDict; - ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; ZSTD_match_t matches[ZSTD_OPT_NUM+1]; const BYTE* inr; - U32 skip_num, cur, cur2, match_num, last_pos, litlen, price; + U32 cur, cur2, match_num, last_pos, litlen, price; const U32 sufficient_len = ctx->params.targetLength; - const U32 faster_get_matches = (ctx->params.strategy == ZSTD_opt); /* init */ ctx->nextToUpdate3 = ctx->nextToUpdate; @@ -955,10 +813,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, best_mlen = (last_pos) ? last_pos : MINMATCH; - if (faster_get_matches && last_pos) - match_num = 0; - else - match_num = getAllMatches(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ + match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ ZSTD_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-base), match_num, last_pos); if (!last_pos && !match_num) { ip++; continue; } @@ -994,7 +849,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, } /* check further positions */ - for (skip_num = 0, cur = 1; cur <= last_pos; cur++) { + for (cur = 1; cur <= last_pos; cur++) { size_t cur_rep; inr = ip + cur; @@ -1082,7 +937,6 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, } best_mlen = mlen; - if (faster_get_matches) skip_num = best_mlen; ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, 0, price, litlen); @@ -1093,11 +947,9 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, } while (mlen >= MINMATCH); } - if (faster_get_matches && skip_num > 0) { skip_num--; continue; } - best_mlen = (best_mlen > MINMATCH) ? best_mlen : MINMATCH; - match_num = getAllMatches(ctx, inr, ip, iend, maxSearches, mls, matches); + match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, ip, iend, maxSearches, mls, matches); ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { @@ -1137,7 +989,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, SET_PRICE(cur2 + mlen, mlen, matches[u].off, litlen, price); mlen++; - } } } // for (skip_num = 0, cur = 1; cur <= last_pos; cur++) + } } } // for (cur = 1; cur <= last_pos; cur++) best_mlen = opt[last_pos].mlen; best_off = opt[last_pos].off; diff --git a/lib/zstd_static.h b/lib/zstd_static.h index 6a92a458..692b147d 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -74,7 +74,7 @@ extern "C" { #define ZSTD_TARGETLENGTH_MAX 999 /* from faster to stronger */ -typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_opt, ZSTD_btopt } ZSTD_strategy; +typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt } ZSTD_strategy; typedef struct { From c950b78ce2a82a000bc412edc412d3325fa47f12 Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 22 Feb 2016 10:11:39 +0100 Subject: [PATCH 06/20] removed backward match extension --- lib/zstd_opt.h | 58 +++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index accefe52..9b1fffd7 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -279,7 +279,6 @@ static U32 ZSTD_insertBtAndGetAllMatches ( bestLength = currentMl; matches[mnum].off = current - matchIndex3; matches[mnum].len = (U32)currentMl; - matches[mnum].back = 0; mnum++; if (currentMl > ZSTD_OPT_NUM) return mnum; if (ip+currentMl == iLimit) return mnum; /* best possible, and avoid read overflow*/ @@ -313,7 +312,6 @@ static U32 ZSTD_insertBtAndGetAllMatches ( bestLength = matchLength; matches[mnum].off = current - matchIndex; matches[mnum].len = (U32)matchLength; - matches[mnum].back = 0; mnum++; if (matchLength > ZSTD_OPT_NUM) break; if (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */ @@ -422,7 +420,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; ZSTD_match_t matches[ZSTD_OPT_NUM+1]; const BYTE* inr; - U32 cur, cur2, match_num, last_pos, litlen, price; + U32 cur, match_num, last_pos, litlen, price; const U32 sufficient_len = ctx->params.targetLength; @@ -594,7 +592,6 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { - cur -= matches[match_num-1].back; best_mlen = matches[match_num-1].len; best_off = matches[match_num-1].off; last_pos = cur + 1; @@ -606,30 +603,27 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, /* set prices using matches at position = cur */ for (u = 0; u < match_num; u++) { mlen = (u>0) ? matches[u-1].len+1 : best_mlen; - cur2 = cur - matches[u].back; - best_mlen = (cur2 + matches[u].len < ZSTD_OPT_NUM) ? matches[u].len : ZSTD_OPT_NUM - cur2; + best_mlen = (cur + matches[u].len < ZSTD_OPT_NUM) ? matches[u].len : ZSTD_OPT_NUM - cur; - ZSTD_LOG_PARSER("%d: Found1 cur=%d cur2=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, cur2, matches[u].len, matches[u].off, best_mlen, last_pos); - if (mlen < matches[u].back + 1) - mlen = matches[u].back + 1; + ZSTD_LOG_PARSER("%d: Found1 cur=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, matches[u].len, matches[u].off, best_mlen, last_pos); while (mlen <= best_mlen) { - if (opt[cur2].mlen == 1) { - litlen = opt[cur2].litlen; - if (cur2 > litlen) - price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen); + if (opt[cur].mlen == 1) { + litlen = opt[cur].litlen; + if (cur > litlen) + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen); else price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); } else { litlen = 0; - price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); } - ZSTD_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), matches[u].back, mlen, best_mlen, matches[u].off, price, litlen); - ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur2 + mlen, opt[cur2 + mlen].price); + ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); + ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur + mlen, opt[cur + mlen].price); - if (cur2 + mlen > last_pos || (price < opt[cur2 + mlen].price)) - SET_PRICE(cur2 + mlen, mlen, matches[u].off, litlen, price); + if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) + SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); mlen++; } } } // for (cur = 1; cur <= last_pos; cur++) @@ -764,7 +758,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; ZSTD_match_t matches[ZSTD_OPT_NUM+1]; const BYTE* inr; - U32 cur, cur2, match_num, last_pos, litlen, price; + U32 cur, match_num, last_pos, litlen, price; const U32 sufficient_len = ctx->params.targetLength; @@ -953,7 +947,6 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { - cur -= matches[match_num-1].back; best_mlen = matches[match_num-1].len; best_off = matches[match_num-1].off; last_pos = cur + 1; @@ -963,30 +956,27 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, // set prices using matches at position = cur for (u = 0; u < match_num; u++) { mlen = (u>0) ? matches[u-1].len+1 : best_mlen; - cur2 = cur - matches[u].back; - best_mlen = (cur2 + matches[u].len < ZSTD_OPT_NUM) ? matches[u].len : ZSTD_OPT_NUM - cur2; + best_mlen = (cur + matches[u].len < ZSTD_OPT_NUM) ? matches[u].len : ZSTD_OPT_NUM - cur; - ZSTD_LOG_PARSER("%d: Found1 cur=%d cur2=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, cur2, matches[u].len, matches[u].off, best_mlen, last_pos); - if (mlen < matches[u].back + 1) - mlen = matches[u].back + 1; + ZSTD_LOG_PARSER("%d: Found1 cur=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, matches[u].len, matches[u].off, best_mlen, last_pos); while (mlen <= best_mlen) { - if (opt[cur2].mlen == 1) { - litlen = opt[cur2].litlen; - if (cur2 > litlen) - price = opt[cur2 - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur2-litlen, matches[u].off, mlen); + if (opt[cur].mlen == 1) { + litlen = opt[cur].litlen; + if (cur > litlen) + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen); else price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); } else { litlen = 0; - price = opt[cur2].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); } - ZSTD_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), matches[u].back, mlen, best_mlen, matches[u].off, price, litlen); - ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur2 + mlen, opt[cur2 + mlen].price); + ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); + ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur + mlen, opt[cur + mlen].price); - if (cur2 + mlen > last_pos || (price < opt[cur2 + mlen].price)) - SET_PRICE(cur2 + mlen, mlen, matches[u].off, litlen, price); + if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) + SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); mlen++; } } } // for (cur = 1; cur <= last_pos; cur++) From 84f43e235979e75fa6c42b73faac535c75ad3398 Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 22 Feb 2016 11:34:07 +0100 Subject: [PATCH 07/20] support for searchLength from 3 to 7 for optimal parser --- lib/zstd_compress.c | 23 ++- lib/zstd_internal.h | 2 +- lib/zstd_opt.h | 327 ++++++++++------------------------------ lib/zstd_opt_internal.h | 231 ++++++++++++++++++++++++++++ lib/zstd_static.h | 2 +- 5 files changed, 321 insertions(+), 264 deletions(-) create mode 100644 lib/zstd_opt_internal.h diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 8c917752..8e572b4a 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -1369,6 +1369,7 @@ static void ZSTD_updateTree_extDict(ZSTD_CCtx* zc, const BYTE* const ip, const B while (idx < target) idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 1); } +#include "zstd_opt_internal.h" /** Tree updater, providing best match */ static size_t ZSTD_BtFindBestMatch_extDict ( @@ -1410,10 +1411,6 @@ static size_t ZSTD_BtFindBestMatch_selectMLS_extDict ( FORCE_INLINE U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) { -#if MINMATCH == 3 - U32* const hashTable3 = zc->hashTable3; - const U32 hashLog3 = zc->params.hashLog3; -#endif U32* const hashTable = zc->hashTable; const U32 hashLog = zc->params.hashLog; U32* const chainTable = zc->contentTable; @@ -1426,9 +1423,6 @@ U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) size_t h = ZSTD_hashPtr(base+idx, hashLog, mls); NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; hashTable[h] = idx; -#if MINMATCH == 3 - hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx; -#endif idx++; } @@ -1436,8 +1430,6 @@ U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) return hashTable[ZSTD_hashPtr(ip, hashLog, mls)]; } -#include "zstd_opt.h" - FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ size_t ZSTD_HcFindBestMatch_generic ( @@ -1659,7 +1651,10 @@ _storeSequence: static void ZSTD_compressBlock_btopt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) { - ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 2); + if (ctx->params.searchLength == 3) + ZSTD_compressBlock_opt_generic3(ctx, src, srcSize, 2); + else + ZSTD_compressBlock_opt_generic4(ctx, src, srcSize, 2); } static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) @@ -1878,7 +1873,10 @@ static void ZSTD_compressBlock_btlazy2_extDict(ZSTD_CCtx* ctx, const void* src, static void ZSTD_compressBlock_btopt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) { - ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 2); + if (ctx->params.searchLength == 3) + ZSTD_compressBlock_opt_extDict_generic3(ctx, src, srcSize, 2); + else + ZSTD_compressBlock_opt_extDict_generic4(ctx, src, srcSize, 2); } @@ -2261,7 +2259,7 @@ size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSi /*-===== Pre-defined compression levels =====-*/ -#define ZSTD_MAX_CLEVEL 21 +#define ZSTD_MAX_CLEVEL 22 unsigned ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } @@ -2290,6 +2288,7 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 0, 25, 25, 24, 16, 5, 4, 40, ZSTD_btopt }, /* level 19 */ { 0, 26, 26, 25, 16, 8, 4,256, ZSTD_btopt }, /* level 20 */ { 0, 26, 27, 25, 24, 10, 4,256, ZSTD_btopt }, /* level 21 */ + { 0, 26, 26, 25, 16, 8, 3,256, ZSTD_btopt }, /* level 20+MM3 */ }, { /* for srcSize <= 256 KB */ /* l, W, C, H, H3, S, L, T, strat */ diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 620572e5..b9826aa7 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -79,7 +79,7 @@ static const size_t ZSTD_frameHeaderSize_min = 5; #define IS_RAW 2 #define IS_RLE 3 -#define MINMATCH 3 +#define MINMATCH 4 #define REPCODE_STARTVALUE 1 #define Litbits 8 diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 9b1fffd7..24343d69 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -31,77 +31,17 @@ - Zstd source repository : https://www.zstd.net */ -/* Note : this file is intended to be included within zstd_compress.c */ +/* Note : this file is intended to be included within zstd_opt_internal.h */ -/*- Local types -*/ -typedef struct { - U32 off; - U32 len; - U32 back; -} ZSTD_match_t; - -typedef struct { - U32 price; - U32 off; - U32 mlen; - U32 litlen; - U32 rep; - U32 rep2; -} ZSTD_optimal_t; - - -/*- Constants -*/ -#define ZSTD_OPT_NUM (1<<12) -#define ZSTD_FREQ_START 1 -#define ZSTD_FREQ_STEP 1 -#define ZSTD_FREQ_DIV 6 - -/*- Debug -*/ -#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 - #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) - #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) - #define ZSTD_LOG_TRY_PRICE(...) printf(__VA_ARGS__) -#else - #define ZSTD_LOG_PARSER(...) - #define ZSTD_LOG_ENCODE(...) - #define ZSTD_LOG_TRY_PRICE(...) -#endif - - -#if MINMATCH == 3 - #define MEM_readMINMATCH(ptr) ((U32)(MEM_read32(ptr)<<8)) -#else - #define MEM_readMINMATCH(ptr) (U32)(MEM_read32(ptr)) -#endif - - -FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) -{ - U32 price, u; - - /* literals */ - price = litLength * ZSTD_highbit(seqStorePtr->litSum); - for (u=0; u < litLength; u++) - price -= ZSTD_highbit(seqStorePtr->litFreq[literals[u]]); - - /* literal Length */ - price += ((litLength >= MaxLL)<<3) + ((litLength >= 255+MaxLL)<<4) + ((litLength>=(1<<15))<<3); - if (litLength >= MaxLL) litLength = MaxLL; - price += ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]); - - return price; -} - - -FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) +FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) { /* offset */ BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset) + 1 : 0; U32 price = offCode + ZSTD_highbit(seqStorePtr->offCodeSum) - ZSTD_highbit(seqStorePtr->offCodeFreq[offCode]); /* match Length */ - matchLength -= MINMATCH; + matchLength -= MINMATCHOPT; price += ((matchLength >= MaxML)<<3) + ((matchLength >= 255+MaxML)<<4) + ((matchLength>=(1<<15))<<3); if (matchLength >= MaxML) matchLength = MaxML; price += ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); @@ -113,120 +53,10 @@ FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYT } -MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) -{ - unsigned u; - - // printf("matchLengthSum=%d litLengthSum=%d litSum=%d offCodeSum=%d\n", ssPtr->matchLengthSum, ssPtr->litLengthSum, ssPtr->litSum, ssPtr->offCodeSum); - - if (ssPtr->litLengthSum == 0) { - ssPtr->matchLengthSum = (1<litLengthSum = (1<litSum = (1<offCodeSum = (1<litFreq[u] = 1; - for (u=0; u<=MaxLL; u++) - ssPtr->litLengthFreq[u] = 1; - for (u=0; u<=MaxML; u++) - ssPtr->matchLengthFreq[u] = 1; - for (u=0; u<=MaxOff; u++) - ssPtr->offCodeFreq[u] = 1; - } else { - ssPtr->matchLengthSum = 0; - ssPtr->litLengthSum = 0; - ssPtr->litSum = 0; - ssPtr->offCodeSum = 0; - - for (u=0; u<=MaxLit; u++) { - ssPtr->litFreq[u] = ZSTD_FREQ_START + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); - ssPtr->litSum += ssPtr->litFreq[u]; - } - for (u=0; u<=MaxLL; u++) { - ssPtr->litLengthFreq[u] = ZSTD_FREQ_START + (ssPtr->litLengthFreq[u]>>ZSTD_FREQ_DIV); - ssPtr->litLengthSum += ssPtr->litLengthFreq[u]; - } - for (u=0; u<=MaxML; u++) { - ssPtr->matchLengthFreq[u] = ZSTD_FREQ_START + (ssPtr->matchLengthFreq[u]>>ZSTD_FREQ_DIV); - ssPtr->matchLengthSum += ssPtr->matchLengthFreq[u]; - } - for (u=0; u<=MaxOff; u++) { - ssPtr->offCodeFreq[u] = ZSTD_FREQ_START + (ssPtr->offCodeFreq[u]>>ZSTD_FREQ_DIV); - ssPtr->offCodeSum += ssPtr->offCodeFreq[u]; - } - } -} - - -MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) -{ - U32 u; - - /* literals */ - seqStorePtr->litSum += litLength * ZSTD_FREQ_STEP; - for (u=0; u < litLength; u++) - seqStorePtr->litFreq[literals[u]] += ZSTD_FREQ_STEP; - - /* literal Length */ - seqStorePtr->litLengthSum += ZSTD_FREQ_STEP; - if (litLength >= MaxLL) - seqStorePtr->litLengthFreq[MaxLL] += ZSTD_FREQ_STEP; - else - seqStorePtr->litLengthFreq[litLength] += ZSTD_FREQ_STEP; - - /* match offset */ - seqStorePtr->offCodeSum += ZSTD_FREQ_STEP; - BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset) + 1 : 0; - seqStorePtr->offCodeFreq[offCode] += ZSTD_FREQ_STEP; - - /* match Length */ - seqStorePtr->matchLengthSum += ZSTD_FREQ_STEP; - if (matchLength >= MaxML) - seqStorePtr->matchLengthFreq[MaxML] += ZSTD_FREQ_STEP; - else - seqStorePtr->matchLengthFreq[matchLength] += ZSTD_FREQ_STEP; -} - - -#define SET_PRICE(pos, mlen_, offset_, litlen_, price_) \ - { \ - while (last_pos < pos) { opt[last_pos+1].price = 1<<30; last_pos++; } \ - opt[pos].mlen = mlen_; \ - opt[pos].off = offset_; \ - opt[pos].litlen = litlen_; \ - opt[pos].price = price_; \ - ZSTD_LOG_PARSER("%d: SET price[%d/%d]=%d litlen=%d len=%d off=%d\n", (int)(inr-base), (int)pos, (int)last_pos, opt[pos].price, opt[pos].litlen, opt[pos].mlen, opt[pos].off); \ - } - - - /*-************************************* * Binary Tree search ***************************************/ -/* Update hashTable3 up to ip (excluded) - Assumption : always within prefix (ie. not within extDict) */ -#if MINMATCH == 3 -static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_CCtx* zc, const BYTE* ip) -{ - U32* const hashTable3 = zc->hashTable3; - const U32 hashLog3 = zc->params.hashLog3; - const BYTE* const base = zc->base; - const U32 target = (U32)(ip - base); - U32 idx = zc->nextToUpdate3; - - while(idx < target) { - hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx; - idx++; - } - - zc->nextToUpdate3 = target; - return hashTable3[ZSTD_hash3Ptr(ip, hashLog3)]; -} -#endif - - -static U32 ZSTD_insertBtAndGetAllMatches ( +static U32 ZSTD_INSERTBTANDGETALLMATCHES ( ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iLimit, U32 nbCompares, const U32 mls, @@ -254,10 +84,10 @@ static U32 ZSTD_insertBtAndGetAllMatches ( U32 dummy32; /* to be nullified at the end */ U32 mnum = 0; - size_t bestLength = MINMATCH-1; + size_t bestLength = MINMATCHOPT-1; hashTable[h] = current; /* Update Hash Table */ -#if MINMATCH == 3 +#if MINMATCHOPT == 3 /* HC3 match finder */ U32 matchIndex3 = ZSTD_insertAndFindFirstIndexHash3 (zc, ip); @@ -266,12 +96,11 @@ static U32 ZSTD_insertBtAndGetAllMatches ( size_t currentMl=0; if ((!extDict) || matchIndex3 >= dictLimit) { match = base + matchIndex3; - if (match[bestLength] == ip[bestLength]) currentMl = ZSTD_count(ip, match, iLimit); // faster - // if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) currentMl = ZSTD_count(ip+MINMATCH, match+MINMATCH, iLimit)+MINMATCH; // stronger + if (match[bestLength] == ip[bestLength]) currentMl = ZSTD_count(ip, match, iLimit); } else { match = dictBase + matchIndex3; if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */ - currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iLimit, dictEnd, prefixStart) + MINMATCH; + currentMl = ZSTD_count_2segments(ip+MINMATCHOPT, match+MINMATCHOPT, iLimit, dictEnd, prefixStart) + MINMATCHOPT; } /* save best solution */ @@ -342,18 +171,18 @@ static U32 ZSTD_insertBtAndGetAllMatches ( /** Tree updater, providing best match */ -static U32 ZSTD_BtGetAllMatches ( +static U32 ZSTD_BTGETALLMATCHES ( ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iLimit, const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches) { if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls); - return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 0, matches); + return ZSTD_INSERTBTANDGETALLMATCHES(zc, ip, iLimit, maxNbAttempts, mls, 0, matches); } -static U32 ZSTD_BtGetAllMatches_selectMLS ( +static U32 ZSTD_BTGETALLMATCHES_SELECTMLS ( ZSTD_CCtx* zc, /* Index table will be updated */ const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) @@ -362,25 +191,25 @@ static U32 ZSTD_BtGetAllMatches_selectMLS ( switch(matchLengthSearch) { default : - case 4 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 4, matches); - case 5 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 5, matches); - case 6 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 6, matches); + case 4 : return ZSTD_BTGETALLMATCHES(zc, ip, iHighLimit, maxNbAttempts, 4, matches); + case 5 : return ZSTD_BTGETALLMATCHES(zc, ip, iHighLimit, maxNbAttempts, 5, matches); + case 6 : return ZSTD_BTGETALLMATCHES(zc, ip, iHighLimit, maxNbAttempts, 6, matches); } } /** Tree updater, providing best match */ -static U32 ZSTD_BtGetAllMatches_extDict ( +static U32 ZSTD_BTGETALLMATCHES_EXTDICT ( ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iLimit, const U32 maxNbAttempts, const U32 mls, ZSTD_match_t* matches) { if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls); - return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 1, matches); + return ZSTD_INSERTBTANDGETALLMATCHES(zc, ip, iLimit, maxNbAttempts, mls, 1, matches); } -static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( +static U32 ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT ( ZSTD_CCtx* zc, /* Index table will be updated */ const BYTE* ip, const BYTE* const iLowLimit, const BYTE* const iHighLimit, const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) @@ -389,9 +218,9 @@ static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( switch(matchLengthSearch) { default : - case 4 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 4, matches); - case 5 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 5, matches); - case 6 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 6, matches); + case 4 : return ZSTD_BTGETALLMATCHES_EXTDICT(zc, ip, iHighLimit, maxNbAttempts, 4, matches); + case 5 : return ZSTD_BTGETALLMATCHES_EXTDICT(zc, ip, iHighLimit, maxNbAttempts, 5, matches); + case 6 : return ZSTD_BTGETALLMATCHES_EXTDICT(zc, ip, iHighLimit, maxNbAttempts, 6, matches); } } @@ -400,7 +229,7 @@ static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( * Optimal parser *********************************/ FORCE_INLINE -void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, +void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, const void* src, size_t srcSize, const U32 depth) { @@ -416,14 +245,13 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, U32 rep_2=REPCODE_STARTVALUE, rep_1=REPCODE_STARTVALUE; const U32 maxSearches = 1U << ctx->params.searchLog; const U32 mls = ctx->params.searchLength; + const U32 sufficient_len = ctx->params.targetLength; - ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; + ZSTD_optimal_t opt[ZSTD_OPT_NUM+1]; ZSTD_match_t matches[ZSTD_OPT_NUM+1]; const BYTE* inr; U32 cur, match_num, last_pos, litlen, price; - const U32 sufficient_len = ctx->params.targetLength; - /* init */ ctx->nextToUpdate3 = ctx->nextToUpdate; ZSTD_resetSeqStore(seqStorePtr); @@ -445,7 +273,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, /* check repCode */ if (MEM_readMINMATCH(ip+1) == MEM_readMINMATCH(ip+1 - rep_1)) { /* repcode : we take it */ - mlen = (U32)ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-rep_1, iend) + MINMATCH; + mlen = (U32)ZSTD_count(ip+1+MINMATCHOPT, ip+1+MINMATCHOPT-rep_1, iend) + MINMATCHOPT; ZSTD_LOG_PARSER("%d: start try REP rep=%d mlen=%d\n", (int)(ip-base), (int)rep_1, (int)mlen); if (depth==0 || mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) { @@ -455,14 +283,14 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); /* note : macro modifies last_pos */ mlen--; - } while (mlen >= MINMATCH); + } while (mlen >= MINMATCHOPT); } - match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ + match_num = ZSTD_BTGETALLMATCHES_SELECTMLS(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ ZSTD_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-base), match_num, last_pos); if (!last_pos && !match_num) { ip++; continue; } @@ -479,7 +307,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, goto _storeSequence; } - best_mlen = (last_pos) ? last_pos : MINMATCH; + best_mlen = (last_pos) ? last_pos : MINMATCHOPT; // set prices using matches at position = 0 for (u = 0; u < match_num; u++) { @@ -488,13 +316,13 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-base), matches[u].len, matches[u].off, (int)best_mlen, (int)last_pos); litlen = opt[0].litlen; while (mlen <= best_mlen) { - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; } } - if (last_pos < MINMATCH) { ip++; continue; } + if (last_pos < MINMATCHOPT) { ip++; continue; } /* check further positions */ for (cur = 1; cur <= last_pos; cur++) { @@ -554,7 +382,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, } if (MEM_readMINMATCH(inr) == MEM_readMINMATCH(inr - cur_rep)) { // check rep - mlen = (U32)ZSTD_count(inr+MINMATCH, inr+MINMATCH - cur_rep, iend) + MINMATCH; + mlen = (U32)ZSTD_count(inr+MINMATCHOPT, inr+MINMATCHOPT - cur_rep, iend) + MINMATCHOPT; ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d rep=%d opt[%d].off=%d\n", (int)(inr-base), mlen, 0, opt[cur].rep, cur, opt[cur].off); if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) { @@ -568,14 +396,14 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (opt[cur].mlen == 1) { litlen = opt[cur].litlen; if (cur > litlen) { - price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen); + price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, inr-litlen, 0, mlen); ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); } else { litlen = 0; - price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen); - ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen)); + price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen); + ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen)); } best_mlen = mlen; @@ -585,10 +413,10 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (cur + mlen > last_pos || price <= opt[cur + mlen].price) SET_PRICE(cur + mlen, mlen, 0, litlen, price); mlen--; - } while (mlen >= MINMATCH); + } while (mlen >= MINMATCHOPT); } - match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, ip, iend, maxSearches, mls, matches); + match_num = ZSTD_BTGETALLMATCHES_SELECTMLS(ctx, inr, ip, iend, maxSearches, mls, matches); ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { @@ -598,7 +426,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, goto _storeSequence; } - best_mlen = (best_mlen > MINMATCH) ? best_mlen : MINMATCH; + best_mlen = (best_mlen > MINMATCHOPT) ? best_mlen : MINMATCHOPT; /* set prices using matches at position = cur */ for (u = 0; u < match_num; u++) { @@ -611,12 +439,12 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, if (opt[cur].mlen == 1) { litlen = opt[cur].litlen; if (cur > litlen) - price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen); + price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen); else - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen); } else { litlen = 0; - price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); + price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, matches[u].off, mlen); } ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); @@ -688,7 +516,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); if (offset == 0 || offset >= 8) - if (ml2 < mlen && ml2 < MINMATCH) { + if (ml2 < mlen && ml2 < MINMATCHOPT) { printf("%d: ERROR iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { printf("%d: ERROR ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } @@ -696,14 +524,14 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ printf("%d: ERROR ip - offset < base iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if ((int)offset >= (1 << ctx->params.windowLog)) { printf("%d: offset >= (1 << params.windowLog) iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if (mlen < MINMATCH) { - printf("%d: ERROR mlen < MINMATCH iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } + if (mlen < MINMATCHOPT) { + printf("%d: ERROR mlen < MINMATCHOPT iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if (ip + mlen > iend) { printf("%d: ERROR ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } #endif - ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); - ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); + ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); anchor = ip = ip + mlen; } /* for (cur=0; cur < last_pos; ) */ @@ -711,14 +539,14 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ while ( (anchor <= ilimit) && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(anchor - rep_2)) ) { /* store sequence */ - best_mlen = (U32)ZSTD_count(anchor+MINMATCH, anchor+MINMATCH-rep_2, iend); + best_mlen = (U32)ZSTD_count(anchor+MINMATCHOPT, anchor+MINMATCHOPT-rep_2, iend); best_off = rep_2; rep_2 = rep_1; rep_1 = best_off; ZSTD_LOG_ENCODE("%d/%d: ENCODE REP literals=%d mlen=%d off=%d rep1=%d rep2=%d\n", (int)(anchor-base), (int)(iend-base), (int)(0), (int)best_mlen, (int)(0), (int)rep_1, (int)rep_2); ZSTD_updatePrice(seqStorePtr, 0, anchor, 0, best_mlen); ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, best_mlen); - anchor += best_mlen+MINMATCH; + anchor += best_mlen+MINMATCHOPT; continue; /* faster when present ... (?) */ } if (anchor > ip) ip = anchor; @@ -734,7 +562,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ FORCE_INLINE -void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, +void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, const void* src, size_t srcSize, const U32 depth) { @@ -754,14 +582,13 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, U32 rep_2=REPCODE_STARTVALUE, rep_1=REPCODE_STARTVALUE; const U32 maxSearches = 1U << ctx->params.searchLog; const U32 mls = ctx->params.searchLength; + const U32 sufficient_len = ctx->params.targetLength; - ZSTD_optimal_t opt[ZSTD_OPT_NUM+4]; + ZSTD_optimal_t opt[ZSTD_OPT_NUM+1]; ZSTD_match_t matches[ZSTD_OPT_NUM+1]; const BYTE* inr; U32 cur, match_num, last_pos, litlen, price; - const U32 sufficient_len = ctx->params.targetLength; - /* init */ ctx->nextToUpdate3 = ctx->nextToUpdate; ZSTD_resetSeqStore(seqStorePtr); @@ -788,7 +615,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, && (MEM_readMINMATCH(ip+1) == MEM_readMINMATCH(repMatch)) ) { /* repcode detected we should take it */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; - mlen = (U32)ZSTD_count_2segments(ip+1+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; + mlen = (U32)ZSTD_count_2segments(ip+1+MINMATCHOPT, repMatch+MINMATCHOPT, iend, repEnd, prefixStart) + MINMATCHOPT; ZSTD_LOG_PARSER("%d: start try REP rep=%d mlen=%d\n", (int)(ip-base), (int)rep_1, (int)mlen); if (depth==0 || mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) { @@ -798,16 +625,16 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); mlen--; - } while (mlen >= MINMATCH); + } while (mlen >= MINMATCHOPT); } } - best_mlen = (last_pos) ? last_pos : MINMATCH; + best_mlen = (last_pos) ? last_pos : MINMATCHOPT; - match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ + match_num = ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ ZSTD_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-base), match_num, last_pos); if (!last_pos && !match_num) { ip++; continue; } @@ -831,13 +658,13 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, ZSTD_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-base), matches[u].len, matches[u].off, (int)best_mlen, (int)last_pos); litlen = opt[0].litlen; while (mlen <= best_mlen) { - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; } } - if (last_pos < MINMATCH) { + if (last_pos < MINMATCHOPT) { // ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ ip++; continue; } @@ -906,7 +733,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, &&(MEM_readMINMATCH(inr) == MEM_readMINMATCH(repMatch)) ) { /* repcode detected */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; - mlen = (U32)ZSTD_count_2segments(inr+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; + mlen = (U32)ZSTD_count_2segments(inr+MINMATCHOPT, repMatch+MINMATCHOPT, iend, repEnd, prefixStart) + MINMATCHOPT; ZSTD_LOG_PARSER("%d: Found REP mlen=%d off=%d rep=%d opt[%d].off=%d\n", (int)(inr-base), mlen, 0, opt[cur].rep, cur, opt[cur].off); if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) { @@ -920,14 +747,14 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, if (opt[cur].mlen == 1) { litlen = opt[cur].litlen; if (cur > litlen) { - price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen); + price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, inr-litlen, 0, mlen); ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); } else { litlen = 0; - price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen); - ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen)); + price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen); + ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen)); } best_mlen = mlen; @@ -938,12 +765,12 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, if (cur + mlen > last_pos || price <= opt[cur + mlen].price) // || ((price == opt[cur + mlen].price) && (opt[cur].mlen == 1) && (cur != litlen))) // at equal price prefer REP instead of MATCH SET_PRICE(cur + mlen, mlen, 0, litlen, price); mlen--; - } while (mlen >= MINMATCH); + } while (mlen >= MINMATCHOPT); } - best_mlen = (best_mlen > MINMATCH) ? best_mlen : MINMATCH; + best_mlen = (best_mlen > MINMATCHOPT) ? best_mlen : MINMATCHOPT; - match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, ip, iend, maxSearches, mls, matches); + match_num = ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT(ctx, inr, ip, iend, maxSearches, mls, matches); ZSTD_LOG_PARSER("%d: ZSTD_GetAllMatches match_num=%d\n", (int)(inr-base), match_num); if (match_num > 0 && matches[match_num-1].len > sufficient_len) { @@ -964,12 +791,12 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, if (opt[cur].mlen == 1) { litlen = opt[cur].litlen; if (cur > litlen) - price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen); + price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen); else - price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen); } else { litlen = 0; - price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen); + price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, matches[u].off, mlen); } ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); @@ -1040,7 +867,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set ml2 = (U32)ZSTD_count(ip, ip-offset, iend); else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); - if (ml2 < mlen && ml2 < MINMATCH) { + if (ml2 < mlen && ml2 < MINMATCHOPT) { printf("%d: ERROR iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { printf("%d: ERROR ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } @@ -1048,14 +875,14 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set printf("%d: ERROR ip - offset < base iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if ((int)offset >= (1 << ctx->params.windowLog)) { printf("%d: offset >= (1 << params.windowLog) iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if (mlen < MINMATCH) { - printf("%d: ERROR mlen < MINMATCH iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } + if (mlen < MINMATCHOPT) { + printf("%d: ERROR mlen < MINMATCHOPT iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if (ip + mlen > iend) { printf("%d: ERROR ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } #endif - ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); - ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); + ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); anchor = ip = ip + mlen; } @@ -1068,11 +895,11 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(repMatch)) ) { /* repcode detected, let's take it */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; - mlen = (U32)ZSTD_count_2segments(anchor+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; + mlen = (U32)ZSTD_count_2segments(anchor+MINMATCHOPT, repMatch+MINMATCHOPT, iend, repEnd, prefixStart) + MINMATCHOPT; offset = rep_2; rep_2 = rep_1; rep_1 = offset; /* swap offset history */ ZSTD_LOG_ENCODE("%d/%d: ENCODE REP literals=%d mlen=%d off=%d rep1=%d rep2=%d\n", (int)(anchor-base), (int)(iend-base), (int)(0), (int)best_mlen, (int)(0), (int)rep_1, (int)rep_2); - ZSTD_updatePrice(seqStorePtr, 0, anchor, 0, mlen-MINMATCH); - ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, mlen-MINMATCH); + ZSTD_updatePrice(seqStorePtr, 0, anchor, 0, mlen-MINMATCHOPT); + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, mlen-MINMATCHOPT); anchor += mlen; continue; /* faster when present ... (?) */ } diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h new file mode 100644 index 00000000..29e936d3 --- /dev/null +++ b/lib/zstd_opt_internal.h @@ -0,0 +1,231 @@ +/* + zstd_opt_internal - common optimal parser functions to include + Header File for include + Copyright (C) 2016, Przemyslaw Skibinski, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - zstd source repository : https://github.com/Cyan4973/zstd +*/ + +/* Note : this file is intended to be included within zstd_compress.c */ + +#ifndef ZSTD_OPT_INTERNAL_H_MODULE +#define ZSTD_OPT_INTERNAL_H_MODULE + + +/*-******************************************* +* The optimal parser +*********************************************/ +/*- Constants -*/ +#define ZSTD_OPT_NUM (1<<12) +#define ZSTD_FREQ_START 1 +#define ZSTD_FREQ_STEP 1 +#define ZSTD_FREQ_DIV 6 + +/*- Debug -*/ +#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 + #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) + #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) + #define ZSTD_LOG_TRY_PRICE(...) printf(__VA_ARGS__) +#else + #define ZSTD_LOG_PARSER(...) + #define ZSTD_LOG_ENCODE(...) + #define ZSTD_LOG_TRY_PRICE(...) +#endif + + +typedef struct { + U32 off; + U32 len; +} ZSTD_match_t; + +typedef struct { + U32 price; + U32 off; + U32 mlen; + U32 litlen; + U32 rep; + U32 rep2; +} ZSTD_optimal_t; + + +MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) +{ + unsigned u; + + // printf("matchLengthSum=%d litLengthSum=%d litSum=%d offCodeSum=%d\n", ssPtr->matchLengthSum, ssPtr->litLengthSum, ssPtr->litSum, ssPtr->offCodeSum); + + if (ssPtr->litLengthSum == 0) { + ssPtr->matchLengthSum = (1<litLengthSum = (1<litSum = (1<offCodeSum = (1<litFreq[u] = 1; + for (u=0; u<=MaxLL; u++) + ssPtr->litLengthFreq[u] = 1; + for (u=0; u<=MaxML; u++) + ssPtr->matchLengthFreq[u] = 1; + for (u=0; u<=MaxOff; u++) + ssPtr->offCodeFreq[u] = 1; + } else { + ssPtr->matchLengthSum = 0; + ssPtr->litLengthSum = 0; + ssPtr->litSum = 0; + ssPtr->offCodeSum = 0; + + for (u=0; u<=MaxLit; u++) { + ssPtr->litFreq[u] = ZSTD_FREQ_START + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litSum += ssPtr->litFreq[u]; + } + for (u=0; u<=MaxLL; u++) { + ssPtr->litLengthFreq[u] = ZSTD_FREQ_START + (ssPtr->litLengthFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litLengthSum += ssPtr->litLengthFreq[u]; + } + for (u=0; u<=MaxML; u++) { + ssPtr->matchLengthFreq[u] = ZSTD_FREQ_START + (ssPtr->matchLengthFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->matchLengthSum += ssPtr->matchLengthFreq[u]; + } + for (u=0; u<=MaxOff; u++) { + ssPtr->offCodeFreq[u] = ZSTD_FREQ_START + (ssPtr->offCodeFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->offCodeSum += ssPtr->offCodeFreq[u]; + } + } +} + +MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) +{ + U32 u; + + /* literals */ + seqStorePtr->litSum += litLength * ZSTD_FREQ_STEP; + for (u=0; u < litLength; u++) + seqStorePtr->litFreq[literals[u]] += ZSTD_FREQ_STEP; + + /* literal Length */ + seqStorePtr->litLengthSum += ZSTD_FREQ_STEP; + if (litLength >= MaxLL) + seqStorePtr->litLengthFreq[MaxLL] += ZSTD_FREQ_STEP; + else + seqStorePtr->litLengthFreq[litLength] += ZSTD_FREQ_STEP; + + /* match offset */ + seqStorePtr->offCodeSum += ZSTD_FREQ_STEP; + BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset) + 1 : 0; + seqStorePtr->offCodeFreq[offCode] += ZSTD_FREQ_STEP; + + /* match Length */ + seqStorePtr->matchLengthSum += ZSTD_FREQ_STEP; + if (matchLength >= MaxML) + seqStorePtr->matchLengthFreq[MaxML] += ZSTD_FREQ_STEP; + else + seqStorePtr->matchLengthFreq[matchLength] += ZSTD_FREQ_STEP; +} + +FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) +{ + U32 price, u; + + /* literals */ + price = litLength * ZSTD_highbit(seqStorePtr->litSum); + for (u=0; u < litLength; u++) + price -= ZSTD_highbit(seqStorePtr->litFreq[literals[u]]); + + /* literal Length */ + price += ((litLength >= MaxLL)<<3) + ((litLength >= 255+MaxLL)<<4) + ((litLength>=(1<<15))<<3); + if (litLength >= MaxLL) litLength = MaxLL; + price += ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]); + + return price; +} + +#define SET_PRICE(pos, mlen_, offset_, litlen_, price_) \ + { \ + while (last_pos < pos) { opt[last_pos+1].price = 1<<30; last_pos++; } \ + opt[pos].mlen = mlen_; \ + opt[pos].off = offset_; \ + opt[pos].litlen = litlen_; \ + opt[pos].price = price_; \ + ZSTD_LOG_PARSER("%d: SET price[%d/%d]=%d litlen=%d len=%d off=%d\n", (int)(inr-base), (int)pos, (int)last_pos, opt[pos].price, opt[pos].litlen, opt[pos].mlen, opt[pos].off); \ + } + +/* Update hashTable3 up to ip (excluded) + Assumption : always within prefix (ie. not within extDict) */ +static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_CCtx* zc, const BYTE* ip) +{ + U32* const hashTable3 = zc->hashTable3; + const U32 hashLog3 = zc->params.hashLog3; + const BYTE* const base = zc->base; + const U32 target = (U32)(ip - base); + U32 idx = zc->nextToUpdate3; + + while(idx < target) { + hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx; + idx++; + } + + zc->nextToUpdate3 = target; + return hashTable3[ZSTD_hash3Ptr(ip, hashLog3)]; +} + + +#define MINMATCHOPT 4 +#define MEM_readMINMATCH(ptr) (U32)(MEM_read32(ptr)) +#define ZSTD_GETPRICE ZSTD_getPrice4 +#define ZSTD_INSERTBTANDGETALLMATCHES ZSTD_insertBtAndGetAllMatches4 +#define ZSTD_BTGETALLMATCHES ZSTD_BtGetAllMatches4 +#define ZSTD_BTGETALLMATCHES_SELECTMLS ZSTD_BtGetAllMatches_selectMLS4 +#define ZSTD_BTGETALLMATCHES_EXTDICT ZSTD_BtGetAllMatches_extDict4 +#define ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT ZSTD_BtGetAllMatches_selectMLS_extDict4 +#define ZSTD_COMPRESSBLOCK_OPT_GENERIC ZSTD_compressBlock_opt_generic4 +#define ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC ZSTD_compressBlock_opt_extDict_generic4 +#include "zstd_opt.h" +#undef MINMATCHOPT +#undef MEM_readMINMATCH +#undef ZSTD_GETPRICE +#undef ZSTD_INSERTBTANDGETALLMATCHES +#undef ZSTD_BTGETALLMATCHES +#undef ZSTD_BTGETALLMATCHES_SELECTMLS +#undef ZSTD_BTGETALLMATCHES_EXTDICT +#undef ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT +#undef ZSTD_COMPRESSBLOCK_OPT_GENERIC +#undef ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC + +#define MINMATCHOPT 3 +#define MEM_readMINMATCH(ptr) ((U32)(MEM_read32(ptr)<<8)) +#define ZSTD_GETPRICE ZSTD_getPrice3 +#define ZSTD_INSERTBTANDGETALLMATCHES ZSTD_insertBtAndGetAllMatches3 +#define ZSTD_BTGETALLMATCHES ZSTD_BtGetAllMatches3 +#define ZSTD_BTGETALLMATCHES_SELECTMLS ZSTD_BtGetAllMatches_selectMLS3 +#define ZSTD_BTGETALLMATCHES_EXTDICT ZSTD_BtGetAllMatches_extDict3 +#define ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT ZSTD_BtGetAllMatches_selectMLS_extDict3 +#define ZSTD_COMPRESSBLOCK_OPT_GENERIC ZSTD_compressBlock_opt_generic3 +#define ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC ZSTD_compressBlock_opt_extDict_generic3 +#include "zstd_opt.h" + + +#endif /* ZSTD_OPT_INTERNAL_H_MODULE */ diff --git a/lib/zstd_static.h b/lib/zstd_static.h index 692b147d..0e280cef 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -69,7 +69,7 @@ extern "C" { #define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1) #define ZSTD_SEARCHLOG_MIN 1 #define ZSTD_SEARCHLENGTH_MAX 7 -#define ZSTD_SEARCHLENGTH_MIN 4 +#define ZSTD_SEARCHLENGTH_MIN 3 #define ZSTD_TARGETLENGTH_MIN 4 #define ZSTD_TARGETLENGTH_MAX 999 From 6b3739c8e5cc3f8e96d45d72a23708b76fcfa0be Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 22 Feb 2016 15:53:42 +0100 Subject: [PATCH 08/20] MINMATCH=3 or 4 selected in ZSTD frame --- lib/zstd_compress.c | 17 ++++++++++------- lib/zstd_decompress.c | 10 ++++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 8e572b4a..bb15afb8 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -2144,7 +2144,7 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* zc, if (ZSTD_isError(errorCode)) return errorCode; MEM_writeLE32(zc->headerBuffer, ZSTD_MAGICNUMBER); /* Write Header */ - ((BYTE*)zc->headerBuffer)[4] = (BYTE)(params.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN); + ((BYTE*)zc->headerBuffer)[4] = (BYTE)(params.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN + ((params.searchLength==3)<<4)); zc->hbSize = ZSTD_frameHeaderSize_min; zc->stage = 0; @@ -2259,7 +2259,7 @@ size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSi /*-===== Pre-defined compression levels =====-*/ -#define ZSTD_MAX_CLEVEL 22 +#define ZSTD_MAX_CLEVEL 25 unsigned ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } @@ -2284,11 +2284,14 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 0, 23, 23, 23, 0, 5, 5, 4, ZSTD_lazy2 }, /* level 15 */ { 0, 23, 22, 22, 0, 5, 5, 4, ZSTD_btlazy2 }, /* level 16 */ { 0, 24, 24, 23, 0, 4, 5, 4, ZSTD_btlazy2 }, /* level 17 */ - { 0, 24, 24, 23, 16, 5, 5, 30, ZSTD_btopt }, /* level 18 */ - { 0, 25, 25, 24, 16, 5, 4, 40, ZSTD_btopt }, /* level 19 */ - { 0, 26, 26, 25, 16, 8, 4,256, ZSTD_btopt }, /* level 20 */ - { 0, 26, 27, 25, 24, 10, 4,256, ZSTD_btopt }, /* level 21 */ - { 0, 26, 26, 25, 16, 8, 3,256, ZSTD_btopt }, /* level 20+MM3 */ + { 0, 24, 24, 23, 0, 5, 5, 30, ZSTD_btopt }, /* level 18 */ + { 0, 25, 25, 24, 0, 5, 4, 40, ZSTD_btopt }, /* level 19 */ + { 0, 26, 26, 25, 0, 8, 4,256, ZSTD_btopt }, /* level 20 */ + { 0, 26, 27, 25, 0, 10, 4,256, ZSTD_btopt }, /* level 21 */ + { 0, 24, 24, 23, 16, 5, 3, 30, ZSTD_btopt }, /* level 22 */ + { 0, 25, 25, 24, 16, 5, 3, 40, ZSTD_btopt }, /* level 23 */ + { 0, 26, 26, 25, 16, 8, 3,256, ZSTD_btopt }, /* level 24 */ + { 0, 26, 27, 25, 24, 10, 3,256, ZSTD_btopt }, /* level 25 */ }, { /* for srcSize <= 256 KB */ /* l, W, C, H, H3, S, L, T, strat */ diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index bfa0ea33..8cedb3ea 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -292,7 +292,8 @@ size_t ZSTD_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcS if (magicNumber != ZSTD_MAGICNUMBER) return ERROR(prefix_unknown); memset(params, 0, sizeof(*params)); params->windowLog = (((const BYTE*)src)[4] & 15) + ZSTD_WINDOWLOG_ABSOLUTEMIN; - if ((((const BYTE*)src)[4] >> 4) != 0) return ERROR(frameParameter_unsupported); /* reserved bits */ + params->searchLength = (((const BYTE*)src)[4] & 16) ? MINMATCH-1 : MINMATCH; + if ((((const BYTE*)src)[4] >> 5) != 0) return ERROR(frameParameter_unsupported); /* reserved 3 bits */ return 0; } @@ -614,7 +615,7 @@ typedef struct { -static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState) +static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState, const U32 mls) { size_t litLength; size_t prevOffset; @@ -669,7 +670,7 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState) } if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */ } - matchLength += MINMATCH; + matchLength += mls; /* save result */ seq->litLength = litLength; @@ -784,6 +785,7 @@ static size_t ZSTD_decompressSequences( const BYTE* const base = (const BYTE*) (dctx->base); const BYTE* const vBase = (const BYTE*) (dctx->vBase); const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); + const U32 mls = dctx->params.searchLength; /* Build Decoding Tables */ errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength, @@ -811,7 +813,7 @@ static size_t ZSTD_decompressSequences( for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) { size_t oneSeqSize; nbSeq--; - ZSTD_decodeSequence(&sequence, &seqState); + ZSTD_decodeSequence(&sequence, &seqState, mls); oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd); if (ZSTD_isError(oneSeqSize)) return oneSeqSize; op += oneSeqSize; From 9f754d23dc93c6e2ab60e72d1c0df58bd5a0c5c7 Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 22 Feb 2016 17:00:04 +0100 Subject: [PATCH 09/20] dummy levels 22-25 --- lib/zstd_compress.c | 12 ++++++++++++ lib/zstd_internal.h | 2 +- lib/zstd_opt.h | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index bb15afb8..6f753fc9 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -2317,6 +2317,10 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 0, 18, 19, 18, 0, 10, 4,256, ZSTD_btopt }, /* level 19.*/ { 0, 18, 19, 18, 0, 11, 4,256, ZSTD_btopt }, /* level 20.*/ { 0, 18, 19, 18, 0, 12, 4,256, ZSTD_btopt }, /* level 21.*/ + { 0, 18, 19, 18, 0, 12, 4,256, ZSTD_btopt }, /* level 21-2*/ + { 0, 18, 19, 18, 0, 12, 4,256, ZSTD_btopt }, /* level 21-3*/ + { 0, 18, 19, 18, 0, 12, 4,256, ZSTD_btopt }, /* level 21-4*/ + { 0, 18, 19, 18, 0, 12, 4,256, ZSTD_btopt }, /* level 21-5*/ }, { /* for srcSize <= 128 KB */ /* l, W, C, H, H3, S, L, T, strat */ @@ -2342,6 +2346,10 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 0, 17, 18, 17, 0, 9, 4,256, ZSTD_btopt }, /* level 19 */ { 0, 17, 18, 17, 0, 10, 4,512, ZSTD_btopt }, /* level 20 */ { 0, 17, 18, 17, 0, 11, 4,512, ZSTD_btopt }, /* level 21 */ + { 0, 17, 18, 17, 0, 11, 4,512, ZSTD_btopt }, /* level 21-2 */ + { 0, 17, 18, 17, 0, 11, 4,512, ZSTD_btopt }, /* level 21-3 */ + { 0, 17, 18, 17, 0, 11, 4,512, ZSTD_btopt }, /* level 21-4 */ + { 0, 17, 18, 17, 0, 11, 4,512, ZSTD_btopt }, /* level 21-5 */ }, { /* for srcSize <= 16 KB */ @@ -2368,6 +2376,10 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 0, 14, 15, 15, 0, 15, 4,256, ZSTD_btopt }, /* level 19 */ { 0, 14, 15, 15, 0, 16, 4,256, ZSTD_btopt }, /* level 20 */ { 0, 14, 15, 15, 0, 17, 4,256, ZSTD_btopt }, /* level 21 */ + { 0, 14, 15, 15, 0, 17, 4,256, ZSTD_btopt }, /* level 21-2 */ + { 0, 14, 15, 15, 0, 17, 4,256, ZSTD_btopt }, /* level 21-3 */ + { 0, 14, 15, 15, 0, 17, 4,256, ZSTD_btopt }, /* level 21-4 */ + { 0, 14, 15, 15, 0, 17, 4,256, ZSTD_btopt }, /* level 21-5 */ }, }; diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index b9826aa7..3d60e87b 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -50,7 +50,7 @@ /*-************************************* * Common constants ***************************************/ -#define ZSTD_OPT_DEBUG 1 // 1 = tableID=0; 5 = check encoded sequences; 9 = full logs +#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 5 = check encoded sequences; 9 = full logs #if ZSTD_OPT_DEBUG > 0 #include /* for debug */ #endif diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 24343d69..dfe76223 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -50,6 +50,7 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT return price + 1 + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); +// return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); } From 15174b0cfb4ba73ac7dfa37473299c8d0be6f48a Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 23 Feb 2016 12:41:56 +0100 Subject: [PATCH 10/20] statistics of encoded sequences --- lib/zstd_compress.c | 14 ++++++++++++++ lib/zstd_internal.h | 6 +++++- lib/zstd_opt.h | 7 +++++++ lib/zstd_opt_internal.h | 4 +--- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 6f753fc9..6312ea34 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -726,6 +726,12 @@ MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const B printf("pos %6u : %3u literals & match %3u bytes at distance %6u \n", (U32)(literals - g_start), (U32)litLength, (U32)matchCode+MINMATCH, (U32)offsetCode); #endif +#if ZSTD_OPT_DEBUG >= 3 + if (offsetCode == 0) seqStorePtr->realRepSum++; + seqStorePtr->realSeqSum++; + seqStorePtr->realMatchSum += matchCode; + seqStorePtr->realLitSum += litLength; +#endif /* copy Literals */ ZSTD_wildcopy(seqStorePtr->lit, literals, litLength); @@ -1912,6 +1918,9 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, BYTE* const ostart = (BYTE*)dst; BYTE* op = ostart; const U32 maxDist = 1 << zc->params.windowLog; + seqStore_t* ssPtr = &zc->seqStore; + + ssPtr->realMatchSum = ssPtr->realLitSum = ssPtr->realSeqSum = ssPtr->realRepSum = 0; while (remaining) { size_t cSize; @@ -1945,6 +1954,11 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, op += cSize; } +#if ZSTD_OPT_DEBUG >= 3 + ssPtr->realMatchSum += ssPtr->realSeqSum * ((zc->params.searchLength == 3) ? 3 : 4); + printf("avgMatchL=%.2f avgLitL=%.2f match=%.1f%% lit=%.1f%% reps=%d seq=%d\n", (float)ssPtr->realMatchSum/ssPtr->realSeqSum, (float)ssPtr->realLitSum/ssPtr->realSeqSum, 100.0*ssPtr->realMatchSum/(ssPtr->realMatchSum+ssPtr->realLitSum), 100.0*ssPtr->realLitSum/(ssPtr->realMatchSum+ssPtr->realLitSum), ssPtr->realRepSum, ssPtr->realSeqSum); +#endif + return op-ostart; } diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 3d60e87b..39b76d8a 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -50,7 +50,7 @@ /*-************************************* * Common constants ***************************************/ -#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 5 = check encoded sequences; 9 = full logs +#define ZSTD_OPT_DEBUG 3 // 1 = tableID=0; 3 = print block stats; 5 = check encoded sequences; 9 = full logs #if ZSTD_OPT_DEBUG > 0 #include /* for debug */ #endif @@ -179,6 +179,10 @@ typedef struct { U32 litLengthSum; U32 litSum; U32 offCodeSum; + U32 realMatchSum; + U32 realLitSum; + U32 realSeqSum; + U32 realRepSum; } seqStore_t; seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx); diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index dfe76223..b5b10efd 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -46,11 +46,18 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT if (matchLength >= MaxML) matchLength = MaxML; price += ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); +#if 0 if (!litLength) return price + 1 + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); +#else + if (!litLength) + return price + 1; + + return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); // return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); +#endif } diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index 29e936d3..47d5a498 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -75,9 +75,7 @@ typedef struct { MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) { unsigned u; - - // printf("matchLengthSum=%d litLengthSum=%d litSum=%d offCodeSum=%d\n", ssPtr->matchLengthSum, ssPtr->litLengthSum, ssPtr->litSum, ssPtr->offCodeSum); - + if (ssPtr->litLengthSum == 0) { ssPtr->matchLengthSum = (1<litLengthSum = (1< Date: Tue, 23 Feb 2016 16:25:04 +0100 Subject: [PATCH 11/20] priceFunc --- lib/zstd_compress.c | 8 ++++++-- lib/zstd_internal.h | 2 ++ lib/zstd_opt.h | 26 ++++++++++++++------------ lib/zstd_opt_internal.h | 4 +++- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 6312ea34..8a5f6f69 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -1919,8 +1919,10 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, BYTE* op = ostart; const U32 maxDist = 1 << zc->params.windowLog; seqStore_t* ssPtr = &zc->seqStore; + static U32 priceFunc = 0; - ssPtr->realMatchSum = ssPtr->realLitSum = ssPtr->realSeqSum = ssPtr->realRepSum = 0; + ssPtr->realMatchSum = ssPtr->realLitSum = ssPtr->realSeqSum = ssPtr->realRepSum = 1; + ssPtr->priceFunc = priceFunc; while (remaining) { size_t cSize; @@ -1954,9 +1956,11 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, op += cSize; } + #if ZSTD_OPT_DEBUG >= 3 ssPtr->realMatchSum += ssPtr->realSeqSum * ((zc->params.searchLength == 3) ? 3 : 4); - printf("avgMatchL=%.2f avgLitL=%.2f match=%.1f%% lit=%.1f%% reps=%d seq=%d\n", (float)ssPtr->realMatchSum/ssPtr->realSeqSum, (float)ssPtr->realLitSum/ssPtr->realSeqSum, 100.0*ssPtr->realMatchSum/(ssPtr->realMatchSum+ssPtr->realLitSum), 100.0*ssPtr->realLitSum/(ssPtr->realMatchSum+ssPtr->realLitSum), ssPtr->realRepSum, ssPtr->realSeqSum); + printf("avgMatchL=%.2f avgLitL=%.2f match=%.1f%% lit=%.1f%% reps=%d seq=%d priceFunc=%d\n", (float)ssPtr->realMatchSum/ssPtr->realSeqSum, (float)ssPtr->realLitSum/ssPtr->realSeqSum, 100.0*ssPtr->realMatchSum/(ssPtr->realMatchSum+ssPtr->realLitSum), 100.0*ssPtr->realLitSum/(ssPtr->realMatchSum+ssPtr->realLitSum), ssPtr->realRepSum, ssPtr->realSeqSum, ssPtr->priceFunc); + priceFunc++; #endif return op-ostart; diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 39b76d8a..c575f463 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -176,6 +176,7 @@ typedef struct { U32* litFreq; U32* offCodeFreq; U32 matchLengthSum; + U32 matchSum; U32 litLengthSum; U32 litSum; U32 offCodeSum; @@ -183,6 +184,7 @@ typedef struct { U32 realLitSum; U32 realSeqSum; U32 realRepSum; + U32 priceFunc; } seqStore_t; seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx); diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index b5b10efd..4169f874 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -46,18 +46,20 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT if (matchLength >= MaxML) matchLength = MaxML; price += ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); -#if 0 - if (!litLength) - return price + 1 + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); - - return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + (matchLength==0); -#else - if (!litLength) - return price + 1; - - return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); -// return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); -#endif +#define ZSTD_PRICE_MULT 2 + switch (seqStorePtr->priceFunc) + { + default: + case 0: + if (!litLength) return price + 1 + ((seqStorePtr->litSum<litSum + seqStorePtr->matchSum)) + (matchLength==0); + return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum<litSum + seqStorePtr->matchSum)) + (matchLength==0); + case 1: + if (!litLength) return price + 1 + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + (matchLength==0); + return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + (matchLength==0); + case 2: + if (!litLength) return price + 1; + return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); + } } diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index 47d5a498..e9ca5f2e 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -43,7 +43,7 @@ #define ZSTD_OPT_NUM (1<<12) #define ZSTD_FREQ_START 1 #define ZSTD_FREQ_STEP 1 -#define ZSTD_FREQ_DIV 6 +#define ZSTD_FREQ_DIV 5 /*- Debug -*/ #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 @@ -94,6 +94,7 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) ssPtr->matchLengthSum = 0; ssPtr->litLengthSum = 0; ssPtr->litSum = 0; + ssPtr->matchSum = 0; ssPtr->offCodeSum = 0; for (u=0; u<=MaxLit; u++) { @@ -107,6 +108,7 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) for (u=0; u<=MaxML; u++) { ssPtr->matchLengthFreq[u] = ZSTD_FREQ_START + (ssPtr->matchLengthFreq[u]>>ZSTD_FREQ_DIV); ssPtr->matchLengthSum += ssPtr->matchLengthFreq[u]; + ssPtr->matchSum += ssPtr->matchLengthFreq[u] * (u + 3); } for (u=0; u<=MaxOff; u++) { ssPtr->offCodeFreq[u] = ZSTD_FREQ_START + (ssPtr->offCodeFreq[u]>>ZSTD_FREQ_DIV); From 4a981f79374077ea8b896ea7a3fd3ee187f7d2d0 Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 23 Feb 2016 19:08:20 +0100 Subject: [PATCH 12/20] improved ZSTD_GETPRICE --- lib/zstd_internal.h | 2 +- lib/zstd_opt.h | 12 ++++-------- lib/zstd_opt_internal.h | 10 +++++++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index c575f463..c3874e4e 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -50,7 +50,7 @@ /*-************************************* * Common constants ***************************************/ -#define ZSTD_OPT_DEBUG 3 // 1 = tableID=0; 3 = print block stats; 5 = check encoded sequences; 9 = full logs +#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 3 = print block stats; 5 = check encoded sequences; 9 = full logs #if ZSTD_OPT_DEBUG > 0 #include /* for debug */ #endif diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 4169f874..cfd17e5a 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -44,21 +44,17 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT matchLength -= MINMATCHOPT; price += ((matchLength >= MaxML)<<3) + ((matchLength >= 255+MaxML)<<4) + ((matchLength>=(1<<15))<<3); if (matchLength >= MaxML) matchLength = MaxML; - price += ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); + price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); -#define ZSTD_PRICE_MULT 2 switch (seqStorePtr->priceFunc) { default: case 0: - if (!litLength) return price + 1 + ((seqStorePtr->litSum<litSum + seqStorePtr->matchSum)) + (matchLength==0); - return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum<litSum + seqStorePtr->matchSum)) + (matchLength==0); + return price + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum); case 1: - if (!litLength) return price + 1 + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + (matchLength==0); - return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + (matchLength==0); + return price + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); case 2: - if (!litLength) return price + 1; - return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals); + return price; } } diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index e9ca5f2e..c23d5d74 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -43,7 +43,7 @@ #define ZSTD_OPT_NUM (1<<12) #define ZSTD_FREQ_START 1 #define ZSTD_FREQ_STEP 1 -#define ZSTD_FREQ_DIV 5 +#define ZSTD_FREQ_DIV 4 /*- Debug -*/ #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 @@ -81,7 +81,8 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) ssPtr->litLengthSum = (1<litSum = (1<offCodeSum = (1<matchSum = 0; + for (u=0; u<=MaxLit; u++) ssPtr->litFreq[u] = 1; for (u=0; u<=MaxLL; u++) @@ -94,8 +95,8 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) ssPtr->matchLengthSum = 0; ssPtr->litLengthSum = 0; ssPtr->litSum = 0; - ssPtr->matchSum = 0; ssPtr->offCodeSum = 0; + ssPtr->matchSum = 0; for (u=0; u<=MaxLit; u++) { ssPtr->litFreq[u] = ZSTD_FREQ_START + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); @@ -150,6 +151,9 @@ FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, co { U32 price, u; + if (litLength == 0) + return ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(seqStorePtr->litLengthFreq[0]); + /* literals */ price = litLength * ZSTD_highbit(seqStorePtr->litSum); for (u=0; u < litLength; u++) From ee55628c9dc93f036e82b192745cee3bc6b5cdc3 Mon Sep 17 00:00:00 2001 From: inikep Date: Wed, 24 Feb 2016 14:40:30 +0100 Subject: [PATCH 13/20] combined price function --- lib/zstd_internal.h | 2 ++ lib/zstd_opt.h | 35 ++++++++++++++++++++++++++++++++--- lib/zstd_opt_internal.h | 20 ++++++++++---------- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index c3874e4e..d5266c44 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -184,6 +184,8 @@ typedef struct { U32 realLitSum; U32 realSeqSum; U32 realRepSum; + U32 factor; + U32 factor2; U32 priceFunc; } seqStore_t; diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index cfd17e5a..5bb40e60 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -48,12 +48,14 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT switch (seqStorePtr->priceFunc) { - default: + default: case 0: - return price + ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum); + return price + seqStorePtr->factor + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); case 1: - return price + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); + return price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); case 2: + return price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); + case 3: return price; } } @@ -264,6 +266,33 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, ZSTD_rescaleFreqs(seqStorePtr); if ((ip-base) < REPCODE_STARTVALUE) ip = base + REPCODE_STARTVALUE; + + size_t mostFrequent; + unsigned count[256], maxSymbolValue, usedSymbols = 0; + maxSymbolValue = 255; + mostFrequent = FSE_count(count, &maxSymbolValue, src, srcSize); + for (unsigned i=0; i<=maxSymbolValue; i++) + if (count[i]) usedSymbols++; + + seqStorePtr->factor = ((usedSymbols <= 18) && (mostFrequent < (1<<14))) ? mostFrequent>>10 : 0; // helps RTF files + seqStorePtr->factor2 = (usedSymbols==256) && (mostFrequent > (1<<14)); + +#if 0 + if (seqStorePtr->factor2) + printf("FACTOR2 usedSymbols==256;mostFrequent>(1<<14) maxSymbolValue=%d mostFrequent=%d usedSymbols=%d\n", maxSymbolValue, (int)mostFrequent, usedSymbols); + if (seqStorePtr->factor) { + printf("FACTOR1 usedSymbols<56;mostFrequent<(1<<14) maxSymbolValue=%d mostFrequent=%d usedSymbols=%d\n", maxSymbolValue, (int)mostFrequent, usedSymbols); +#if 0 + for (int i=0; i<256; i++) + if (count[i]) printf("%d=%d ", i, count[i]); + printf("\n"); + +#endif + } +#endif + + + /* Match Loop */ while (ip < ilimit) { U32 u; diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index c23d5d74..86288752 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -43,7 +43,7 @@ #define ZSTD_OPT_NUM (1<<12) #define ZSTD_FREQ_START 1 #define ZSTD_FREQ_STEP 1 -#define ZSTD_FREQ_DIV 4 +#define ZSTD_FREQ_DIV 5 /*- Debug -*/ #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 @@ -77,20 +77,20 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) unsigned u; if (ssPtr->litLengthSum == 0) { - ssPtr->matchLengthSum = (1<litLengthSum = (1<litSum = (1<offCodeSum = (1<matchSum = 0; + ssPtr->matchLengthSum = ZSTD_FREQ_START*(1<litLengthSum = ZSTD_FREQ_START*(1<litSum = ZSTD_FREQ_START*(1<offCodeSum = ZSTD_FREQ_START*(1<matchSum = ZSTD_FREQ_START*ssPtr->litSum; for (u=0; u<=MaxLit; u++) - ssPtr->litFreq[u] = 1; + ssPtr->litFreq[u] = ZSTD_FREQ_START; for (u=0; u<=MaxLL; u++) - ssPtr->litLengthFreq[u] = 1; + ssPtr->litLengthFreq[u] = ZSTD_FREQ_START; for (u=0; u<=MaxML; u++) - ssPtr->matchLengthFreq[u] = 1; + ssPtr->matchLengthFreq[u] = ZSTD_FREQ_START; for (u=0; u<=MaxOff; u++) - ssPtr->offCodeFreq[u] = 1; + ssPtr->offCodeFreq[u] = ZSTD_FREQ_START; } else { ssPtr->matchLengthSum = 0; ssPtr->litLengthSum = 0; From 02137f8c4262292bab948a046704fb90082ed4d0 Mon Sep 17 00:00:00 2001 From: inikep Date: Wed, 24 Feb 2016 18:09:36 +0100 Subject: [PATCH 14/20] more accurate gain function --- lib/zstd_opt.h | 16 ++++++++-------- lib/zstd_opt_internal.h | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 5bb40e60..55acef67 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -37,26 +37,26 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) { /* offset */ - BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset) + 1 : 0; - U32 price = offCode + ZSTD_highbit(seqStorePtr->offCodeSum) - ZSTD_highbit(seqStorePtr->offCodeFreq[offCode]); + BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset+1) + 1 : 0; + U32 price = (offCode-1) + (!offCode) + ZSTD_highbit(seqStorePtr->offCodeSum+1) - ZSTD_highbit(seqStorePtr->offCodeFreq[offCode]+1); /* match Length */ matchLength -= MINMATCHOPT; price += ((matchLength >= MaxML)<<3) + ((matchLength >= 255+MaxML)<<4) + ((matchLength>=(1<<15))<<3); if (matchLength >= MaxML) matchLength = MaxML; - price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]); + price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum+1) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]+1); switch (seqStorePtr->priceFunc) { - default: + default: case 0: - return price + seqStorePtr->factor + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); + return 1 + price + seqStorePtr->factor + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); case 1: - return price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); + return 1 + price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); case 2: - return price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); + return 1 + price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); case 3: - return price; + return 1 + price; } } diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index 86288752..a40cad06 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -52,7 +52,7 @@ #define ZSTD_LOG_TRY_PRICE(...) printf(__VA_ARGS__) #else #define ZSTD_LOG_PARSER(...) - #define ZSTD_LOG_ENCODE(...) + #define ZSTD_LOG_ENCODE(...) // printf(__VA_ARGS__) #define ZSTD_LOG_TRY_PRICE(...) #endif @@ -136,7 +136,7 @@ MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const B /* match offset */ seqStorePtr->offCodeSum += ZSTD_FREQ_STEP; - BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset) + 1 : 0; + BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset+1) + 1 : 0; seqStorePtr->offCodeFreq[offCode] += ZSTD_FREQ_STEP; /* match Length */ @@ -152,17 +152,17 @@ FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, co U32 price, u; if (litLength == 0) - return ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(seqStorePtr->litLengthFreq[0]); + return ZSTD_highbit(seqStorePtr->litLengthSum+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[0]+1); /* literals */ - price = litLength * ZSTD_highbit(seqStorePtr->litSum); + price = litLength * ZSTD_highbit(seqStorePtr->litSum+1); for (u=0; u < litLength; u++) - price -= ZSTD_highbit(seqStorePtr->litFreq[literals[u]]); + price -= ZSTD_highbit(seqStorePtr->litFreq[literals[u]]+1); /* literal Length */ price += ((litLength >= MaxLL)<<3) + ((litLength >= 255+MaxLL)<<4) + ((litLength>=(1<<15))<<3); if (litLength >= MaxLL) litLength = MaxLL; - price += ZSTD_highbit(seqStorePtr->litLengthSum) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]); + price += ZSTD_highbit(seqStorePtr->litLengthSum+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]+1); return price; } From afa9c9f5ab4499731ca185e299a9610736697265 Mon Sep 17 00:00:00 2001 From: inikep Date: Thu, 25 Feb 2016 16:47:57 +0100 Subject: [PATCH 15/20] turn off RTF optimization --- lib/zstd_opt.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 55acef67..922a569c 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -46,6 +46,7 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT if (matchLength >= MaxML) matchLength = MaxML; price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum+1) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]+1); +#if ZSTD_OPT_DEBUG >= 3 switch (seqStorePtr->priceFunc) { default: @@ -58,6 +59,9 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT case 3: return 1 + price; } +#else + return 1 + price + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); +#endif } @@ -266,7 +270,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, ZSTD_rescaleFreqs(seqStorePtr); if ((ip-base) < REPCODE_STARTVALUE) ip = base + REPCODE_STARTVALUE; - +#if ZSTD_OPT_DEBUG >= 3 size_t mostFrequent; unsigned count[256], maxSymbolValue, usedSymbols = 0; maxSymbolValue = 255; @@ -276,6 +280,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, seqStorePtr->factor = ((usedSymbols <= 18) && (mostFrequent < (1<<14))) ? mostFrequent>>10 : 0; // helps RTF files seqStorePtr->factor2 = (usedSymbols==256) && (mostFrequent > (1<<14)); +#endif #if 0 if (seqStorePtr->factor2) From 59493e8669832b56624c1e1f844d5915a64bc147 Mon Sep 17 00:00:00 2001 From: inikep Date: Thu, 25 Feb 2016 19:15:08 +0100 Subject: [PATCH 16/20] fixed paramgrill --- lib/zstd_opt_internal.h | 2 +- programs/paramgrill.c | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index a40cad06..0186ebc2 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -81,7 +81,7 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) ssPtr->litLengthSum = ZSTD_FREQ_START*(1<litSum = ZSTD_FREQ_START*(1<offCodeSum = ZSTD_FREQ_START*(1<matchSum = ZSTD_FREQ_START*ssPtr->litSum; + ssPtr->matchSum = ssPtr->litSum; for (u=0; u<=MaxLit; u++) ssPtr->litFreq[u] = ZSTD_FREQ_START; diff --git a/programs/paramgrill.c b/programs/paramgrill.c index 23a54d46..db8ff0e7 100644 --- a/programs/paramgrill.c +++ b/programs/paramgrill.c @@ -127,7 +127,7 @@ static U32 g_rand = 1; static U32 g_singleRun = 0; static U32 g_target = 0; static U32 g_noSeed = 0; -static ZSTD_parameters g_params = { 0, 0, 0, 0, 0, 0, 0, ZSTD_greedy }; +static ZSTD_parameters g_params = { 0, 0, 0, 0, 0, 0, 0, 0, ZSTD_greedy }; void BMK_SetNbIterations(int nbLoops) { @@ -406,7 +406,6 @@ const char* g_stratName[] = { "ZSTD_fast ", "ZSTD_lazy ", "ZSTD_lazy2 ", "ZSTD_btlazy2", - "ZSTD_opt ", "ZSTD_btopt " }; static void BMK_printWinner(FILE* f, U32 cLevel, BMK_result_t result, ZSTD_parameters params, size_t srcSize) @@ -549,7 +548,7 @@ static ZSTD_parameters* sanitizeParams(ZSTD_parameters params) g_params = params; if (params.strategy == ZSTD_fast) g_params.contentLog = 0, g_params.searchLog = 0; - if ((params.strategy != ZSTD_opt) && (params.strategy != ZSTD_btopt )) + if (params.strategy != ZSTD_btopt ) g_params.targetLength = 0; return &g_params; } From f414647c6b6639cb92a85faca16341e9f0a1eba3 Mon Sep 17 00:00:00 2001 From: inikep Date: Thu, 25 Feb 2016 22:31:07 +0100 Subject: [PATCH 17/20] fixed ZSTD_copyCCtx --- lib/zstd_compress.c | 5 +++-- lib/zstd_decompress.c | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 8a5f6f69..5edd3434 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -228,8 +228,8 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx) { const U32 contentLog = (srcCCtx->params.strategy == ZSTD_fast) ? 1 : srcCCtx->params.contentLog; - const size_t tableSpace = ((1 << contentLog) + (1 << srcCCtx->params.hashLog)) * sizeof(U32); - + const size_t tableSpace = ((1 << contentLog) + (1 << srcCCtx->params.hashLog) + (1 << srcCCtx->params.hashLog3)) * sizeof(U32); + if (srcCCtx->stage!=0) return ERROR(stage_wrong); ZSTD_resetCCtx_advanced(dstCCtx, srcCCtx->params); @@ -243,6 +243,7 @@ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx) /* copy dictionary pointers */ dstCCtx->nextToUpdate= srcCCtx->nextToUpdate; + dstCCtx->nextToUpdate3 = srcCCtx->nextToUpdate3; dstCCtx->nextSrc = srcCCtx->nextSrc; dstCCtx->base = srcCCtx->base; dstCCtx->dictBase = srcCCtx->dictBase; diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index 8cedb3ea..a5746986 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -159,6 +159,7 @@ size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) dctx->dictEnd = NULL; dctx->hufTableX4[0] = HufLog; dctx->flagStaticTables = 0; + dctx->params.searchLength = MINMATCH; return 0; } From f647d99d61a884d70f18770072e8c9270818bd79 Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 29 Feb 2016 12:33:08 +0100 Subject: [PATCH 18/20] fixed bug with 3 or more succesive rep codes --- lib/zstd_compress.c | 2 +- lib/zstd_opt.h | 74 ++++++++++++++++------------------------- lib/zstd_opt_internal.h | 2 -- 3 files changed, 30 insertions(+), 48 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 5edd3434..362d714b 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -1958,7 +1958,7 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, } -#if ZSTD_OPT_DEBUG >= 3 +#if ZSTD_OPT_DEBUG == 3 ssPtr->realMatchSum += ssPtr->realSeqSum * ((zc->params.searchLength == 3) ? 3 : 4); printf("avgMatchL=%.2f avgLitL=%.2f match=%.1f%% lit=%.1f%% reps=%d seq=%d priceFunc=%d\n", (float)ssPtr->realMatchSum/ssPtr->realSeqSum, (float)ssPtr->realLitSum/ssPtr->realSeqSum, 100.0*ssPtr->realMatchSum/(ssPtr->realMatchSum+ssPtr->realLitSum), 100.0*ssPtr->realLitSum/(ssPtr->realMatchSum+ssPtr->realLitSum), ssPtr->realRepSum, ssPtr->realSeqSum, ssPtr->priceFunc); priceFunc++; diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 922a569c..5cb04cd8 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -373,17 +373,13 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, litlen = opt[cur-1].litlen + 1; if (cur > litlen) { price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-litlen); - ZSTD_LOG_TRY_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else price = ZSTD_getLiteralPrice(seqStorePtr, litlen, litstart); } else { litlen = 1; price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1); - ZSTD_LOG_TRY_PRICE("%d: TRY3 price=%d cur=%d litlen=%d litonly=%d\n", (int)(inr-base), price, cur, litlen, (int)ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1)); } - ZSTD_LOG_TRY_PRICE("%d: TRY4 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur, opt[cur].price); - if (cur > last_pos || price <= opt[cur].price) // || ((price == opt[cur].price) && (opt[cur-1].mlen == 1) && (cur != litlen))) SET_PRICE(cur, 1, 0, litlen, price); @@ -392,33 +388,33 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, if (inr > ilimit) /* last match must start at a minimum distance of 8 from oend */ continue; - mlen = opt[cur].mlen; + mlen = opt[cur].mlen; - if (opt[cur-mlen].off) { + if (opt[cur].off) { opt[cur].rep2 = opt[cur-mlen].rep; - opt[cur].rep = opt[cur-mlen].off; - ZSTD_LOG_PARSER("%d: COPYREP1 cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); - } else { - if (cur!=mlen && opt[cur-mlen].litlen == 0) { + opt[cur].rep = opt[cur].off; + ZSTD_LOG_ENCODE("%d: COPYREP_OFF cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); + } else { + if (cur!=mlen && opt[cur].litlen == 0) { opt[cur].rep2 = opt[cur-mlen].rep; opt[cur].rep = opt[cur-mlen].rep2; - ZSTD_LOG_PARSER("%d: COPYREP2 cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); + ZSTD_LOG_ENCODE("%d: COPYREP_SWI cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); } else { opt[cur].rep2 = opt[cur-mlen].rep2; opt[cur].rep = opt[cur-mlen].rep; - ZSTD_LOG_PARSER("%d: COPYREP3 cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); - } } + ZSTD_LOG_ENCODE("%d: COPYREP_NOR cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); + } } - ZSTD_LOG_PARSER("%d: CURRENT price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep, opt[cur].rep2); + ZSTD_LOG_PARSER("%d: CURRENT_NoExt price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep, opt[cur].rep2); best_mlen = 0; - if (!opt[cur].off && opt[cur].mlen != 1) { + if (opt[cur].mlen != 1) { cur_rep = opt[cur].rep2; - ZSTD_LOG_PARSER("%d: try REP2 rep2=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); + ZSTD_LOG_PARSER("%d: tryNoExt REP2 rep2=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); } else { cur_rep = opt[cur].rep; - ZSTD_LOG_PARSER("%d: try REP1 rep=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); + ZSTD_LOG_PARSER("%d: tryNoExt REP1 rep=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); } if (MEM_readMINMATCH(inr) == MEM_readMINMATCH(inr - cur_rep)) { // check rep @@ -437,13 +433,11 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, litlen = opt[cur].litlen; if (cur > litlen) { price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, inr-litlen, 0, mlen); - ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); } else { litlen = 0; price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen); - ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen)); } best_mlen = mlen; @@ -488,7 +482,6 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, } ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); - ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur + mlen, opt[cur + mlen].price); if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); @@ -512,7 +505,6 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ while (1) { mlen = opt[cur].mlen; - ZSTD_LOG_PARSER("%d: cur=%d mlen=%d\n", (int)(ip-base), cur, mlen); offset = opt[cur].off; opt[cur].mlen = best_mlen; opt[cur].off = best_off; @@ -557,7 +549,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); if (offset == 0 || offset >= 8) if (ml2 < mlen && ml2 < MINMATCHOPT) { - printf("%d: ERROR iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } + printf("%d: ERROR_NoExt iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { printf("%d: ERROR ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if (ip - offset < ctx->base) { @@ -718,17 +710,13 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, litlen = opt[cur-1].litlen + 1; if (cur > litlen) { price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-litlen); - ZSTD_LOG_TRY_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else price = ZSTD_getLiteralPrice(seqStorePtr, litlen, litstart); } else { litlen = 1; price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1); - ZSTD_LOG_TRY_PRICE("%d: TRY3 price=%d cur=%d litlen=%d litonly=%d\n", (int)(inr-base), price, cur, litlen, (int)ZSTD_getLiteralPrice(seqStorePtr, litlen, inr-1)); } - ZSTD_LOG_TRY_PRICE("%d: TRY4 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur, opt[cur].price); - if (cur > last_pos || price <= opt[cur].price) // || ((price == opt[cur].price) && (opt[cur-1].mlen == 1) && (cur != litlen))) SET_PRICE(cur, 1, 0, litlen, price); @@ -737,33 +725,33 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, if (inr > ilimit) // last match must start at a minimum distance of 8 from oend continue; - mlen = opt[cur].mlen; + mlen = opt[cur].mlen; - if (opt[cur-mlen].off) { + if (opt[cur].off) { opt[cur].rep2 = opt[cur-mlen].rep; - opt[cur].rep = opt[cur-mlen].off; - ZSTD_LOG_PARSER("%d: COPYREP1 cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); - } else { - if (cur!=mlen && opt[cur-mlen].litlen == 0) { + opt[cur].rep = opt[cur].off; + ZSTD_LOG_ENCODE("%d: COPYREP_OFF cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); + } else { + if (cur!=mlen && opt[cur].litlen == 0) { opt[cur].rep2 = opt[cur-mlen].rep; opt[cur].rep = opt[cur-mlen].rep2; - ZSTD_LOG_PARSER("%d: COPYREP2 cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); + ZSTD_LOG_ENCODE("%d: COPYREP_SWI cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); } else { opt[cur].rep2 = opt[cur-mlen].rep2; opt[cur].rep = opt[cur-mlen].rep; - ZSTD_LOG_PARSER("%d: COPYREP3 cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); - } } + ZSTD_LOG_ENCODE("%d: COPYREP_NOR cur=%d mlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, mlen, opt[cur].rep, opt[cur].rep2); + } } - ZSTD_LOG_PARSER("%d: CURRENT price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep, opt[cur].rep2); + ZSTD_LOG_PARSER("%d: CURRENT_Ext price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d rep2=%d\n", (int)(inr-base), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep, opt[cur].rep2); best_mlen = 0; - if (!opt[cur].off && opt[cur].mlen != 1) { + if (opt[cur].mlen != 1) { cur_rep = opt[cur].rep2; - ZSTD_LOG_PARSER("%d: try REP2 rep2=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); + ZSTD_LOG_PARSER("%d: tryExt REP2 rep2=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); } else { cur_rep = opt[cur].rep; - ZSTD_LOG_PARSER("%d: try REP1 rep=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); + ZSTD_LOG_PARSER("%d: tryExt REP1 rep=%u mlen=%u\n", (int)(inr-base), (U32)cur_rep, mlen); } const U32 repIndex = (U32)(current+cur - cur_rep); @@ -788,13 +776,11 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, litlen = opt[cur].litlen; if (cur > litlen) { price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, inr-litlen, 0, mlen); - ZSTD_LOG_TRY_PRICE("%d: TRY5 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), cur - litlen, opt[cur - litlen].price, price, cur, litlen); } else price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); } else { litlen = 0; price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen); - ZSTD_LOG_TRY_PRICE("%d: TRY7 price=%d cur=%d litlen=0 getprice=%d\n", (int)(inr-base), price, cur, (int)ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen)); } best_mlen = mlen; @@ -840,7 +826,6 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, } ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); - ZSTD_LOG_TRY_PRICE("%d: TRY8 price=%d opt[%d].price=%d\n", (int)(inr-base), price, cur + mlen, opt[cur + mlen].price); if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); @@ -863,7 +848,6 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set while (1) { mlen = opt[cur].mlen; - ZSTD_LOG_PARSER("%d: cur=%d mlen=%d\n", (int)(ip-base), cur, mlen); offset = opt[cur].off; opt[cur].mlen = best_mlen; opt[cur].off = best_off; @@ -907,8 +891,8 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set ml2 = (U32)ZSTD_count(ip, ip-offset, iend); else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); - if (ml2 < mlen && ml2 < MINMATCHOPT) { - printf("%d: ERROR iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } + // if (ml2 < mlen && ml2 < MINMATCHOPT) { + // printf("%d: ERROR_Ext iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { printf("%d: ERROR ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if (ip - offset < ctx->base) { diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index 0186ebc2..81022dcb 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -49,11 +49,9 @@ #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) - #define ZSTD_LOG_TRY_PRICE(...) printf(__VA_ARGS__) #else #define ZSTD_LOG_PARSER(...) #define ZSTD_LOG_ENCODE(...) // printf(__VA_ARGS__) - #define ZSTD_LOG_TRY_PRICE(...) #endif From 2d55563b92365b3fa5fc337b1c7db7149ee17a28 Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 29 Feb 2016 22:07:40 +0100 Subject: [PATCH 19/20] better logs --- lib/zstd_compress.c | 1 + lib/zstd_internal.h | 2 +- lib/zstd_opt.h | 75 +++++++++++++++++++++++------------------ lib/zstd_opt_internal.h | 4 ++- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 362d714b..04f5be09 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -2040,6 +2040,7 @@ size_t ZSTD_compressContinue (ZSTD_CCtx* zc, size_t ZSTD_compressBlock(ZSTD_CCtx* zc, void* dst, size_t maxDstSize, const void* src, size_t srcSize) { if (srcSize > BLOCKSIZE) return ERROR(srcSize_wrong); + zc->params.searchLength = MINMATCH; return ZSTD_compressContinue_internal(zc, dst, maxDstSize, src, srcSize, 0); } diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index d5266c44..54f9aaf6 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -50,7 +50,7 @@ /*-************************************* * Common constants ***************************************/ -#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 3 = print block stats; 5 = check encoded sequences; 9 = full logs +#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 3 = price func tests; 5 = check encoded sequences; 9 = full logs #if ZSTD_OPT_DEBUG > 0 #include /* for debug */ #endif diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 5cb04cd8..739ef7a6 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -136,14 +136,24 @@ static U32 ZSTD_INSERTBTANDGETALLMATCHES ( match = base + matchIndex; if (match[matchLength] == ip[matchLength]) { #if ZSTD_OPT_DEBUG >= 5 - if (memcmp(match, ip, matchLength) != 0) - printf("%d: ERROR: matchLength=%d ZSTD_count=%d\n", current, (int)matchLength, (int)ZSTD_count(ip, match, ip+matchLength)); + size_t ml; + if (matchIndex < dictLimit) + ml = ZSTD_count_2segments(ip, dictBase + matchIndex, iLimit, dictEnd, prefixStart); + else + ml = ZSTD_count(ip, match, ip+matchLength); + if (ml < matchLength) + printf("%d: ERROR_NOEXT: offset=%d matchLength=%d matchIndex=%d dictLimit=%d ml=%d\n", current, (int)(current - matchIndex), (int)matchLength, (int)matchIndex, (int)dictLimit, (int)ml), exit(0); #endif matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iLimit) +1; } } else { match = dictBase + matchIndex; +#if ZSTD_OPT_DEBUG >= 5 + if (memcmp(match, ip, matchLength) != 0) + printf("%d: ERROR_EXT: matchLength=%d ZSTD_count=%d\n", current, (int)matchLength, (int)ZSTD_count_2segments(ip+matchLength, match+matchLength, iLimit, dictEnd, prefixStart)), exit(0); +#endif matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iLimit, dictEnd, prefixStart); + ZSTD_LOG_PARSER("%d: ZSTD_INSERTBTANDGETALLMATCHES=%d offset=%d dictBase=%p dictEnd=%p prefixStart=%p ip=%p match=%p\n", (int)current, (int)matchLength, (int)(current - matchIndex), dictBase, dictEnd, prefixStart, ip, match); if (matchIndex+matchLength >= dictLimit) match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ } @@ -252,8 +262,9 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, const BYTE* litstart; const BYTE* const iend = istart + srcSize; const BYTE* const ilimit = iend - 8; - const BYTE* const base = ctx->base + ctx->dictLimit; - + const BYTE* const base = ctx->base; + const BYTE* const prefixStart = base + ctx->dictLimit; + U32 rep_2=REPCODE_STARTVALUE, rep_1=REPCODE_STARTVALUE; const U32 maxSearches = 1U << ctx->params.searchLog; const U32 mls = ctx->params.searchLength; @@ -268,7 +279,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, ctx->nextToUpdate3 = ctx->nextToUpdate; ZSTD_resetSeqStore(seqStorePtr); ZSTD_rescaleFreqs(seqStorePtr); - if ((ip-base) < REPCODE_STARTVALUE) ip = base + REPCODE_STARTVALUE; + if ((ip-prefixStart) < REPCODE_STARTVALUE) ip = prefixStart + REPCODE_STARTVALUE; #if ZSTD_OPT_DEBUG >= 3 size_t mostFrequent; @@ -296,7 +307,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, } #endif - + ZSTD_LOG_BLOCK("%d: COMPBLOCK_OPT_GENERIC srcSz=%d maxSrch=%d mls=%d sufLen=%d\n", (int)(ip-base), (int)srcSize, maxSearches, mls, sufficient_len); /* Match Loop */ while (ip < ilimit) { @@ -467,7 +478,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, mlen = (u>0) ? matches[u-1].len+1 : best_mlen; best_mlen = (cur + matches[u].len < ZSTD_OPT_NUM) ? matches[u].len : ZSTD_OPT_NUM - cur; - ZSTD_LOG_PARSER("%d: Found1 cur=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, matches[u].len, matches[u].off, best_mlen, last_pos); + // ZSTD_LOG_PARSER("%d: Found1 cur=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, matches[u].len, matches[u].off, best_mlen, last_pos); while (mlen <= best_mlen) { if (opt[cur].mlen == 1) { @@ -481,7 +492,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, matches[u].off, mlen); } - ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); + // ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); @@ -492,7 +503,6 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, best_mlen = opt[last_pos].mlen; best_off = opt[last_pos].off; cur = last_pos - best_mlen; - // printf("%d: start=%d best_mlen=%d best_off=%d cur=%d\n", (int)(ip - base), (int)(start - ip), (int)best_mlen, (int)best_off, cur); /* store sequence */ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ @@ -547,19 +557,13 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ ml2 = (U32)ZSTD_count(ip, ip-offset, iend); else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); - if (offset == 0 || offset >= 8) - if (ml2 < mlen && ml2 < MINMATCHOPT) { + if (offset >= 8) + if (ml2 < mlen || ml2 < MINMATCHOPT) { printf("%d: ERROR_NoExt iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { - printf("%d: ERROR ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if (ip - offset < ctx->base) { - printf("%d: ERROR ip - offset < base iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if ((int)offset >= (1 << ctx->params.windowLog)) { - printf("%d: offset >= (1 << params.windowLog) iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if (mlen < MINMATCHOPT) { - printf("%d: ERROR mlen < MINMATCHOPT iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } + printf("%d: ERROR_NoExt ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if (ip + mlen > iend) { - printf("%d: ERROR ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } + printf("%d: ERROR_NoExt ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } #endif ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); @@ -627,6 +631,8 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, ZSTD_rescaleFreqs(seqStorePtr); if ((ip - prefixStart) < REPCODE_STARTVALUE) ip += REPCODE_STARTVALUE; + ZSTD_LOG_BLOCK("%d: COMPBLOCK_OPT_EXTDICT srcSz=%d maxSrch=%d mls=%d sufLen=%d\n", (int)(ip-base), (int)srcSize, maxSearches, mls, sufficient_len); + /* Match Loop */ while (ip < ilimit) { U32 u, offset, best_off=0; @@ -811,7 +817,7 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, mlen = (u>0) ? matches[u-1].len+1 : best_mlen; best_mlen = (cur + matches[u].len < ZSTD_OPT_NUM) ? matches[u].len : ZSTD_OPT_NUM - cur; - ZSTD_LOG_PARSER("%d: Found1 cur=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, matches[u].len, matches[u].off, best_mlen, last_pos); + // ZSTD_LOG_PARSER("%d: Found1 cur=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), cur, matches[u].len, matches[u].off, best_mlen, last_pos); while (mlen <= best_mlen) { if (opt[cur].mlen == 1) { @@ -825,7 +831,7 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, matches[u].off, mlen); } - ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); + // ZSTD_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d\n", (int)(inr-base), mlen, best_mlen, matches[u].off, price, litlen); if (cur + mlen > last_pos || (price < opt[cur + mlen].price)) SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price); @@ -836,7 +842,6 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, best_mlen = opt[last_pos].mlen; best_off = opt[last_pos].off; cur = last_pos - best_mlen; - // printf("%d: start=%d best_mlen=%d best_off=%d cur=%d\n", (int)(ip - base), (int)(start - ip), (int)best_mlen, (int)best_off, cur); /* store sequence */ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set @@ -888,21 +893,25 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set #if ZSTD_OPT_DEBUG >= 5 U32 ml2; if (offset) - ml2 = (U32)ZSTD_count(ip, ip-offset, iend); + { + if (offset > (size_t)(ip - prefixStart)) + { + const BYTE* match = dictEnd - (offset - (ip - prefixStart)); + ml2 = ZSTD_count_2segments(ip, match, iend, dictEnd, prefixStart); + ZSTD_LOG_PARSER("%d: ZSTD_count_2segments=%d offset=%d dictBase=%p dictEnd=%p prefixStart=%p ip=%p match=%p\n", (int)current, (int)ml2, (int)offset, dictBase, dictEnd, prefixStart, ip, match); + } + else + ml2 = (U32)ZSTD_count(ip, ip-offset, iend); + } else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); - // if (ml2 < mlen && ml2 < MINMATCHOPT) { - // printf("%d: ERROR_Ext iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } + if (offset >= 8) + if (ml2 < mlen || ml2 < MINMATCHOPT) { + printf("%d: ERROR_Ext iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { - printf("%d: ERROR ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if (ip - offset < ctx->base) { - printf("%d: ERROR ip - offset < base iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if ((int)offset >= (1 << ctx->params.windowLog)) { - printf("%d: offset >= (1 << params.windowLog) iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } - if (mlen < MINMATCHOPT) { - printf("%d: ERROR mlen < MINMATCHOPT iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } + printf("%d: ERROR_Ext ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } if (ip + mlen > iend) { - printf("%d: ERROR ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } + printf("%d: ERROR_Ext ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } #endif ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index 81022dcb..d643c692 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -49,9 +49,11 @@ #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) + #define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__) #else #define ZSTD_LOG_PARSER(...) - #define ZSTD_LOG_ENCODE(...) // printf(__VA_ARGS__) + #define ZSTD_LOG_ENCODE(...) + #define ZSTD_LOG_BLOCK(...) #endif From a4dde2549817b113fc7f594a9a3563333cacb3c4 Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 1 Mar 2016 14:14:35 +0100 Subject: [PATCH 20/20] fixed repcode before lowLimit --- lib/zstd_compress.c | 7 ++++++- lib/zstd_decompress.c | 8 ++++++-- lib/zstd_internal.h | 13 +++++++++++-- lib/zstd_opt.h | 28 +++++++++++----------------- lib/zstd_opt_internal.h | 11 ----------- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 04f5be09..c9847daf 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -2040,7 +2040,8 @@ size_t ZSTD_compressContinue (ZSTD_CCtx* zc, size_t ZSTD_compressBlock(ZSTD_CCtx* zc, void* dst, size_t maxDstSize, const void* src, size_t srcSize) { if (srcSize > BLOCKSIZE) return ERROR(srcSize_wrong); - zc->params.searchLength = MINMATCH; + zc->params.searchLength = MINMATCH; /* force ZSTD_btopt to MINMATCH in block mode */ + ZSTD_LOG_BLOCK("%p: ZSTD_compressBlock searchLength=%d\n", zc->base, zc->params.searchLength); return ZSTD_compressContinue_internal(zc, dst, maxDstSize, src, srcSize, 0); } @@ -2174,11 +2175,13 @@ size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* zc, size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* zc, const void* dict, size_t dictSize, int compressionLevel) { + ZSTD_LOG_BLOCK("%p: ZSTD_compressBegin_usingDict compressionLevel=%d\n", zc->base, compressionLevel); return ZSTD_compressBegin_advanced(zc, dict, dictSize, ZSTD_getParams(compressionLevel, MAX(128 KB, dictSize))); } size_t ZSTD_compressBegin(ZSTD_CCtx* zc, int compressionLevel) { + ZSTD_LOG_BLOCK("%p: ZSTD_compressBegin compressionLevel=%d\n", zc->base, compressionLevel); return ZSTD_compressBegin_advanced(zc, NULL, 0, ZSTD_getParams(compressionLevel, 0)); } @@ -2258,11 +2261,13 @@ size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, const void* dict, size_t dictSize, int compressionLevel) { + ZSTD_LOG_BLOCK("%p: ZSTD_compress_usingDict srcSize=%d dictSize=%d compressionLevel=%d\n", ctx->base, (int)srcSize, (int)dictSize, compressionLevel); return ZSTD_compress_advanced(ctx, dst, maxDstSize, src, srcSize, dict, dictSize, ZSTD_getParams(compressionLevel, srcSize)); } size_t ZSTD_compressCCtx (ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel) { + ZSTD_LOG_BLOCK("%p: ZSTD_compressCCtx srcSize=%d compressionLevel=%d\n", ctx->base, (int)srcSize, compressionLevel); return ZSTD_compress_advanced(ctx, dst, maxDstSize, src, srcSize, NULL, 0, ZSTD_getParams(compressionLevel, srcSize)); } diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index a5746986..e9d3bdb6 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -159,7 +159,8 @@ size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) dctx->dictEnd = NULL; dctx->hufTableX4[0] = HufLog; dctx->flagStaticTables = 0; - dctx->params.searchLength = MINMATCH; + dctx->params.searchLength = MINMATCH; /* overwritten by frame but forces ZSTD_btopt to MINMATCH in block mode */ + ZSTD_LOG_BLOCK("%p: ZSTD_decompressBegin searchLength=%d\n", dctx->base, dctx->params.searchLength); return 0; } @@ -749,7 +750,7 @@ FORCE_INLINE size_t ZSTD_execSequence(BYTE* op, } op += 8; match += 8; - if (oMatchEnd > oend-12) { + if (oMatchEnd > oend-(16-3)) { // 3 = MINMATCH if (op < oend_8) { ZSTD_wildcopy(op, match, oend_8 - op); match += oend_8 - op; @@ -857,6 +858,8 @@ static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx, if (srcSize >= BLOCKSIZE) return ERROR(srcSize_wrong); + ZSTD_LOG_BLOCK("%p: ZSTD_decompressBlock_internal searchLength=%d\n", dctx->base, dctx->params.searchLength); + /* Decode literals sub-block */ litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize); if (ZSTD_isError(litCSize)) return litCSize; @@ -966,6 +969,7 @@ size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) { ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); + ZSTD_LOG_BLOCK("%p: ZSTD_decompressBegin_usingDict searchLength=%d\n", dctx->base, dctx->params.searchLength); ZSTD_checkContinuity(dctx, dst); return ZSTD_decompress_continueDCtx(dctx, dst, maxDstSize, src, srcSize); } diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 54f9aaf6..68b66621 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -51,8 +51,17 @@ * Common constants ***************************************/ #define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 3 = price func tests; 5 = check encoded sequences; 9 = full logs -#if ZSTD_OPT_DEBUG > 0 - #include /* for debug */ +#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>0 + #include +#endif +#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 + #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) + #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) + #define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__) +#else + #define ZSTD_LOG_PARSER(...) + #define ZSTD_LOG_ENCODE(...) + #define ZSTD_LOG_BLOCK(...) #endif #define ZSTD_DICT_MAGIC 0xEC30A435 diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 739ef7a6..a95f97b8 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -47,8 +47,7 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum+1) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]+1); #if ZSTD_OPT_DEBUG >= 3 - switch (seqStorePtr->priceFunc) - { + switch (seqStorePtr->priceFunc) { default: case 0: return 1 + price + seqStorePtr->factor + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); @@ -557,8 +556,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ ml2 = (U32)ZSTD_count(ip, ip-offset, iend); else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); - if (offset >= 8) - if (ml2 < mlen || ml2 < MINMATCHOPT) { + if ((offset >= 8) && (ml2 < mlen || ml2 < MINMATCHOPT)) { printf("%d: ERROR_NoExt iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { printf("%d: ERROR_NoExt ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } @@ -572,7 +570,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ } /* for (cur=0; cur < last_pos; ) */ /* check immediate repcode */ - while ( (anchor <= ilimit) + while ((anchor >= prefixStart + rep_2) && (anchor <= ilimit) && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(anchor - rep_2)) ) { /* store sequence */ best_mlen = (U32)ZSTD_count(anchor+MINMATCHOPT, anchor+MINMATCHOPT-rep_2, iend); @@ -614,7 +612,8 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, const BYTE* const prefixStart = base + dictLimit; const BYTE* const dictBase = ctx->dictBase; const BYTE* const dictEnd = dictBase + dictLimit; - + const U32 lowLimit = ctx->lowLimit; + U32 rep_2=REPCODE_STARTVALUE, rep_1=REPCODE_STARTVALUE; const U32 maxSearches = 1U << ctx->params.searchLog; const U32 mls = ctx->params.searchLength; @@ -892,21 +891,16 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set #if ZSTD_OPT_DEBUG >= 5 U32 ml2; - if (offset) - { - if (offset > (size_t)(ip - prefixStart)) - { + if (offset) { + if (offset > (size_t)(ip - prefixStart)) { const BYTE* match = dictEnd - (offset - (ip - prefixStart)); ml2 = ZSTD_count_2segments(ip, match, iend, dictEnd, prefixStart); ZSTD_LOG_PARSER("%d: ZSTD_count_2segments=%d offset=%d dictBase=%p dictEnd=%p prefixStart=%p ip=%p match=%p\n", (int)current, (int)ml2, (int)offset, dictBase, dictEnd, prefixStart, ip, match); } - else - ml2 = (U32)ZSTD_count(ip, ip-offset, iend); + else ml2 = (U32)ZSTD_count(ip, ip-offset, iend); } - else - ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); - if (offset >= 8) - if (ml2 < mlen || ml2 < MINMATCHOPT) { + else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); + if ((offset >= 8) && (ml2 < mlen || ml2 < MINMATCHOPT)) { printf("%d: ERROR_Ext iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); exit(0); } if (ip < anchor) { printf("%d: ERROR_Ext ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset); exit(0); } @@ -920,7 +914,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set } /* check immediate repcode */ - while (anchor <= ilimit) { + while ((anchor >= base + lowLimit + rep_2) && (anchor <= ilimit)) { const U32 repIndex = (U32)((anchor-base) - rep_2); const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; const BYTE* const repMatch = repBase + repIndex; diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index d643c692..576f4115 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -45,17 +45,6 @@ #define ZSTD_FREQ_STEP 1 #define ZSTD_FREQ_DIV 5 -/*- Debug -*/ -#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 - #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) - #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) - #define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__) -#else - #define ZSTD_LOG_PARSER(...) - #define ZSTD_LOG_ENCODE(...) - #define ZSTD_LOG_BLOCK(...) -#endif - typedef struct { U32 off;