From cc52a97214f53fe2605b3999686e0402cfcbf2b4 Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 19 Feb 2016 10:09:35 +0100 Subject: [PATCH 001/219] 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 7bea6abea..26898c598 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 61216535b..6a92a458b 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 002/219] 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 26fc85785..3d60e87b6 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 ec9a2a158..ba970c8f1 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 003/219] 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 ba970c8f1..98ed77d64 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 004/219] 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 3d60e87b6..620572e51 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 98ed77d64..a25e4642a 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 005/219] 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 26898c598..8c917752f 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 a25e4642a..accefe525 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 6a92a458b..692b147d1 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 006/219] 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 accefe525..9b1fffd7d 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 007/219] 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 8c917752f..8e572b4ac 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 620572e51..b9826aa78 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 9b1fffd7d..24343d691 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 000000000..29e936d38 --- /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 692b147d1..0e280ceff 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 008/219] 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 8e572b4ac..bb15afb89 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 bfa0ea334..8cedb3ea9 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 009/219] 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 bb15afb89..6f753fc9c 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 b9826aa78..3d60e87b6 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 24343d691..dfe76223f 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 010/219] 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 6f753fc9c..6312ea34e 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 3d60e87b6..39b76d8a0 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 dfe76223f..b5b10efde 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 29e936d38..47d5a498f 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 011/219] 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 6312ea34e..8a5f6f696 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 39b76d8a0..c575f4631 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 b5b10efde..4169f874d 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 47d5a498f..e9ca5f2e0 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 012/219] 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 c575f4631..c3874e4eb 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 4169f874d..cfd17e5ac 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 e9ca5f2e0..c23d5d740 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 013/219] 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 c3874e4eb..d5266c448 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 cfd17e5ac..5bb40e607 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 c23d5d740..862887529 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 014/219] 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 5bb40e607..55acef67f 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 862887529..a40cad068 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 ee0229287c1a87f7d917d0ee7e0ea4829e287a2b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 25 Feb 2016 14:53:35 +0100 Subject: [PATCH 015/219] changed version to v0.6.0 --- lib/zstd.h | 4 ++-- lib/zstd_static.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/zstd.h b/lib/zstd.h index 53ed69739..26c6e275f 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -60,8 +60,8 @@ extern "C" { * Version ***************************************/ #define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */ -#define ZSTD_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */ -#define ZSTD_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */ +#define ZSTD_VERSION_MINOR 6 /* for new (non-breaking) interface capabilities */ +#define ZSTD_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */ #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) ZSTDLIB_API unsigned ZSTD_versionNumber (void); diff --git a/lib/zstd_static.h b/lib/zstd_static.h index 61216535b..8ca97f5b4 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -51,7 +51,7 @@ extern "C" { /*-************************************* * Constants ***************************************/ -#define ZSTD_MAGICNUMBER 0xFD2FB525 /* v0.5 */ +#define ZSTD_MAGICNUMBER 0xFD2FB526 /* v0.6 */ /*-************************************* From afa9c9f5ab4499731ca185e299a9610736697265 Mon Sep 17 00:00:00 2001 From: inikep Date: Thu, 25 Feb 2016 16:47:57 +0100 Subject: [PATCH 016/219] 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 55acef67f..922a569cf 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 017/219] 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 a40cad068..0186ebc2b 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 23a54d466..db8ff0e72 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 018/219] 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 8a5f6f696..5edd34343 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 8cedb3ea9..a57469866 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 019/219] 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 5edd34343..362d714bc 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 922a569cf..5cb04cd8c 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 0186ebc2b..81022dcb1 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 020/219] 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 362d714bc..04f5be097 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 d5266c448..54f9aaf6b 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 5cb04cd8c..739ef7a61 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 81022dcb1..d643c6926 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 021/219] 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 04f5be097..c9847dafa 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 a57469866..e9d3bdb69 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 54f9aaf6b..68b66621f 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 739ef7a61..a95f97b89 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 d643c6926..576f4115c 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; From 338533f741f79439662e05a1a3b7d77cd8eaa050 Mon Sep 17 00:00:00 2001 From: inikep Date: Wed, 2 Mar 2016 14:36:41 +0100 Subject: [PATCH 022/219] improved price function initialization --- lib/zstd_opt_internal.h | 53 ++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index 576f4115c..f0a0e69e9 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -41,8 +41,6 @@ *********************************************/ /*- Constants -*/ #define ZSTD_OPT_NUM (1<<12) -#define ZSTD_FREQ_START 1 -#define ZSTD_FREQ_STEP 1 #define ZSTD_FREQ_DIV 5 @@ -66,74 +64,75 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) unsigned u; if (ssPtr->litLengthSum == 0) { - ssPtr->matchLengthSum = ZSTD_FREQ_START*(1<litLengthSum = ZSTD_FREQ_START*(1<litSum = ZSTD_FREQ_START*(1<offCodeSum = ZSTD_FREQ_START*(1<matchSum = ssPtr->litSum; - + ssPtr->litSum = 2*(1<litLengthSum = 1*(1<matchLengthSum = 1*(1<offCodeSum = 1*(1<matchSum = 2*(1<litFreq[u] = ZSTD_FREQ_START; + ssPtr->litFreq[u] = 2; for (u=0; u<=MaxLL; u++) - ssPtr->litLengthFreq[u] = ZSTD_FREQ_START; + ssPtr->litLengthFreq[u] = 1; for (u=0; u<=MaxML; u++) - ssPtr->matchLengthFreq[u] = ZSTD_FREQ_START; + ssPtr->matchLengthFreq[u] = 1; for (u=0; u<=MaxOff; u++) - ssPtr->offCodeFreq[u] = ZSTD_FREQ_START; + ssPtr->offCodeFreq[u] = 1; } else { ssPtr->matchLengthSum = 0; ssPtr->litLengthSum = 0; - ssPtr->litSum = 0; ssPtr->offCodeSum = 0; ssPtr->matchSum = 0; + ssPtr->litSum = 0; for (u=0; u<=MaxLit; u++) { - ssPtr->litFreq[u] = ZSTD_FREQ_START + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litFreq[u] = 1 + (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->litLengthFreq[u] = 1 + (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->matchLengthFreq[u] = 1 + (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); + ssPtr->offCodeFreq[u] = 1 + (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; + seqStorePtr->litSum += litLength; for (u=0; u < litLength; u++) - seqStorePtr->litFreq[literals[u]] += ZSTD_FREQ_STEP; + seqStorePtr->litFreq[literals[u]]++; /* literal Length */ - seqStorePtr->litLengthSum += ZSTD_FREQ_STEP; + seqStorePtr->litLengthSum++; if (litLength >= MaxLL) - seqStorePtr->litLengthFreq[MaxLL] += ZSTD_FREQ_STEP; + seqStorePtr->litLengthFreq[MaxLL]++; else - seqStorePtr->litLengthFreq[litLength] += ZSTD_FREQ_STEP; + seqStorePtr->litLengthFreq[litLength]++; /* match offset */ - seqStorePtr->offCodeSum += ZSTD_FREQ_STEP; + seqStorePtr->offCodeSum++; BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset+1) + 1 : 0; - seqStorePtr->offCodeFreq[offCode] += ZSTD_FREQ_STEP; + seqStorePtr->offCodeFreq[offCode]++; /* match Length */ - seqStorePtr->matchLengthSum += ZSTD_FREQ_STEP; + seqStorePtr->matchLengthSum++; if (matchLength >= MaxML) - seqStorePtr->matchLengthFreq[MaxML] += ZSTD_FREQ_STEP; + seqStorePtr->matchLengthFreq[MaxML]++; else - seqStorePtr->matchLengthFreq[matchLength] += ZSTD_FREQ_STEP; + seqStorePtr->matchLengthFreq[matchLength]++; } FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) From 87d4f3daa41ad92cbf7345f9a056b822ad3c1dbd Mon Sep 17 00:00:00 2001 From: inikep Date: Wed, 2 Mar 2016 15:56:24 +0100 Subject: [PATCH 023/219] priceTable moved to heap --- lib/zstd_compress.c | 23 +++++++++------- lib/zstd_internal.h | 17 ++++++++++++ lib/zstd_opt.h | 59 ++++++++++++++++++++--------------------- lib/zstd_opt_internal.h | 16 ----------- 4 files changed, 59 insertions(+), 56 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index c9847dafa..2eb1e6d2c 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -174,7 +174,9 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, /* reserve table memory */ const U32 contentLog = (params.strategy == ZSTD_fast) ? 1 : params.contentLog; 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); zc->workSpace = malloc(neededSpace); @@ -199,20 +201,21 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->params = params; zc->blockSize = blockSize; - zc->seqStore.litFreq = (U32*) (zc->seqStore.buffer); - zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1<seqStore.offsetStart = zc->seqStore.offCodeFreq + (1<seqStore.offsetStart = (U32*) (zc->seqStore.buffer); zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart + (blockSize>>2)); zc->seqStore.litStart = zc->seqStore.offCodeStart + (blockSize>>2); zc->seqStore.litLengthStart = zc->seqStore.litStart + blockSize; 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->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); + zc->seqStore.litFreq = (U32*)(zc->seqStore.dumpsStart + (blockSize>>2)); + zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1<seqStore.matchTable = (ZSTD_match_t*)(zc->seqStore.offCodeFreq + (1<seqStore.priceTable = (ZSTD_optimal_t*)(zc->seqStore.matchTable + ZSTD_OPT_NUM+1); + + zc->seqStore.litLengthSum = 0; zc->hbSize = 0; zc->stage = 0; zc->loadedDictEnd = 0; diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 68b66621f..4948e2397 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -64,6 +64,7 @@ #define ZSTD_LOG_BLOCK(...) #endif +#define ZSTD_OPT_NUM (1<<12) #define ZSTD_DICT_MAGIC 0xEC30A435 #define KB *(1 <<10) @@ -165,6 +166,20 @@ MEM_STATIC unsigned ZSTD_highbit(U32 val) /*-******************************************* * Private interfaces *********************************************/ +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; + typedef struct { void* buffer; U32* offsetStart; @@ -180,6 +195,8 @@ typedef struct { BYTE* dumpsStart; BYTE* dumps; /* opt */ + ZSTD_optimal_t* priceTable; + ZSTD_match_t* matchTable; U32* matchLengthFreq; U32* litLengthFreq; U32* litFreq; diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index a95f97b89..f61485987 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -269,8 +269,8 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, const U32 mls = ctx->params.searchLength; const U32 sufficient_len = ctx->params.targetLength; - ZSTD_optimal_t opt[ZSTD_OPT_NUM+1]; - ZSTD_match_t matches[ZSTD_OPT_NUM+1]; + ZSTD_optimal_t* opt = seqStorePtr->priceTable; + ZSTD_match_t* matches = seqStorePtr->matchTable; const BYTE* inr; U32 cur, match_num, last_pos, litlen, price; @@ -280,32 +280,6 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, ZSTD_rescaleFreqs(seqStorePtr); if ((ip-prefixStart) < REPCODE_STARTVALUE) ip = prefixStart + REPCODE_STARTVALUE; -#if ZSTD_OPT_DEBUG >= 3 - 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)); -#endif - -#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 - 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 */ @@ -619,8 +593,8 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, const U32 mls = ctx->params.searchLength; const U32 sufficient_len = ctx->params.targetLength; - ZSTD_optimal_t opt[ZSTD_OPT_NUM+1]; - ZSTD_match_t matches[ZSTD_OPT_NUM+1]; + ZSTD_optimal_t* opt = seqStorePtr->priceTable; + ZSTD_match_t* matches = seqStorePtr->matchTable; const BYTE* inr; U32 cur, match_num, last_pos, litlen, price; @@ -913,7 +887,31 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set anchor = ip = ip + mlen; } +#if 0 /* check immediate repcode */ + while ((anchor >= base + lowLimit + rep_2) && (anchor <= ilimit)) { + if ((anchor - rep_2) >= prefixStart) { + if (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(anchor - rep_2)) + mlen = (U32)ZSTD_count(anchor+MINMATCHOPT, anchor - rep_2 + MINMATCHOPT, iend) + MINMATCHOPT; + else + break; + } else { + const BYTE* repMatch = dictBase + ((anchor-base) - rep_2); + if ((repMatch + MINMATCHOPT <= dictEnd) && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(repMatch))) + mlen = (U32)ZSTD_count_2segments(anchor+MINMATCHOPT, repMatch+MINMATCHOPT, iend, dictEnd, prefixStart) + MINMATCHOPT; + else + break; + } + + 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-MINMATCHOPT); + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, mlen-MINMATCHOPT); + anchor += mlen; + } +#else + /* check immediate repcode */ + /* minimal correctness condition = while ((anchor >= prefixStart + REPCODE_STARTVALUE) && (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; @@ -932,6 +930,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set } break; } +#endif if (anchor > ip) ip = anchor; } diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index f0a0e69e9..b980eab08 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -40,25 +40,9 @@ * The optimal parser *********************************************/ /*- Constants -*/ -#define ZSTD_OPT_NUM (1<<12) #define ZSTD_FREQ_DIV 5 -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; From 51bb9a0064fa8ef70d75e130027895d4c97ed14a Mon Sep 17 00:00:00 2001 From: inikep Date: Wed, 2 Mar 2016 19:17:13 +0100 Subject: [PATCH 024/219] fix clang -Wcast-align warning --- lib/zstd_compress.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 2eb1e6d2c..6a4cfe4cb 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -202,13 +202,13 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->blockSize = blockSize; zc->seqStore.offsetStart = (U32*) (zc->seqStore.buffer); - zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart + (blockSize>>2)); + zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart) + blockSize; zc->seqStore.litStart = zc->seqStore.offCodeStart + (blockSize>>2); zc->seqStore.litLengthStart = zc->seqStore.litStart + blockSize; zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (blockSize>>2); - zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); - - zc->seqStore.litFreq = (U32*)(zc->seqStore.dumpsStart + (blockSize>>2)); + zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); + BYTE* dumpsEnd = zc->seqStore.dumpsStart + (blockSize>>2); + zc->seqStore.litFreq = (U32*)(dumpsEnd); zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1< Date: Wed, 2 Mar 2016 20:37:49 +0100 Subject: [PATCH 025/219] fix clang -Wcast-align warning (part 2) --- lib/zstd_compress.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 6a4cfe4cb..50fa2fdc2 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -207,8 +207,8 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->seqStore.litLengthStart = zc->seqStore.litStart + blockSize; zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (blockSize>>2); zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); - BYTE* dumpsEnd = zc->seqStore.dumpsStart + (blockSize>>2); - zc->seqStore.litFreq = (U32*)(dumpsEnd); + + zc->seqStore.litFreq = (U32*)((void*)(zc->seqStore.dumpsStart + (blockSize>>2))); zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1< Date: Fri, 4 Mar 2016 14:45:31 +0100 Subject: [PATCH 026/219] Support for nbSeq > 32767 tamed -Wstrict-aliasing warning --- lib/zstd_compress.c | 18 ++++++------------ lib/zstd_decompress.c | 9 +++++++-- lib/zstd_internal.h | 3 ++- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 50fa2fdc2..0c6ee8814 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -212,8 +212,8 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1<seqStore.matchTable = (ZSTD_match_t*)(zc->seqStore.offCodeFreq + (1<seqStore.priceTable = (ZSTD_optimal_t*)(zc->seqStore.matchTable + ZSTD_OPT_NUM+1); + zc->seqStore.matchTable = (ZSTD_match_t*)(void*)(zc->seqStore.offCodeFreq + (1<seqStore.priceTable = (ZSTD_optimal_t*)(void*)(zc->seqStore.matchTable + ZSTD_OPT_NUM+1); zc->seqStore.litLengthSum = 0; zc->hbSize = 0; @@ -232,7 +232,7 @@ 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) + (1 << srcCCtx->params.hashLog3)) * sizeof(U32); - + if (srcCCtx->stage!=0) return ERROR(stage_wrong); ZSTD_resetCCtx_advanced(dstCCtx, srcCCtx->params); @@ -546,17 +546,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; - else { - op[0] = (BYTE)((nbSeq>>8) + 128); op[1] = (BYTE)nbSeq; op+=2; - } + if (nbSeq < 0x7F) *op++ = (BYTE)nbSeq; + else if (nbSeq < LONGNBSEQ) op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2; + else op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3; if (nbSeq==0) goto _check_compressibility; /* dumps : contains rests of large lengths */ diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index e9d3bdb69..c4338bcc2 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -496,9 +496,14 @@ size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLen /* SeqHead */ *nbSeq = *ip++; if (*nbSeq==0) return 1; - if (*nbSeq >= 128) - *nbSeq = ((nbSeq[0]-128)<<8) + *ip++; + if (*nbSeq >= 0x7F) { + if (*nbSeq == 0xFF) + *nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2; + else + *nbSeq = ((nbSeq[0]-0x80)<<8) + *ip++; + } + /* FSE table descriptors */ LLtype = *ip >> 6; Offtype = (*ip >> 4) & 3; MLtype = (*ip >> 2) & 3; diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 4948e2397..bbd19b004 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -105,12 +105,13 @@ static const size_t ZSTD_frameHeaderSize_min = 5; #define OffFSELog 9 #define MaxSeq MAX(MaxLL, MaxML) +#define LONGNBSEQ 0xFF00 + #define FSE_ENCODING_RAW 0 #define FSE_ENCODING_RLE 1 #define FSE_ENCODING_STATIC 2 #define FSE_ENCODING_DYNAMIC 3 - #define HufLog 12 #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ From 4c01580362e3a95638996ce939e6bb74506b00bc Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 4 Mar 2016 17:07:54 +0100 Subject: [PATCH 027/219] finer searchLength validation --- lib/zstd_compress.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 0c6ee8814..49eefb262 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -145,6 +145,8 @@ static unsigned ZSTD_highbit(U32 val); void ZSTD_validateParams(ZSTD_parameters* params) { const U32 btPlus = (params->strategy == ZSTD_btlazy2) || (params->strategy == ZSTD_btopt); + const U32 searchLengthMax = (params->strategy == ZSTD_fast) ? ZSTD_SEARCHLENGTH_MAX : ZSTD_SEARCHLENGTH_MAX-1; + const U32 searchLengthMin = (params->strategy == ZSTD_btopt) ? ZSTD_SEARCHLENGTH_MIN : ZSTD_SEARCHLENGTH_MIN+1; /* validate params */ if (MEM_32bits()) if (params->windowLog > 25) params->windowLog = 25; /* 32 bits mode cannot flush > 24 bits */ @@ -153,7 +155,7 @@ void ZSTD_validateParams(ZSTD_parameters* params) 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->searchLength, searchLengthMin, searchLengthMax); CLAMP(params->targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); if ((U32)params->strategy>(U32)ZSTD_btopt) params->strategy = ZSTD_btopt; From 4ec2998b1f4b9dcfda951abc988c54c39b711790 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 4 Mar 2016 19:09:28 +0100 Subject: [PATCH 028/219] Added zeroes test (#137) --- programs/fuzzer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/programs/fuzzer.c b/programs/fuzzer.c index f09cf06f2..7cbfd7940 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -171,6 +171,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); if (ZSTD_isError(result)) goto _output_error; + if (result != COMPRESSIBLE_NOISE_LENGTH) goto _output_error; DISPLAYLEVEL(4, "OK \n"); { @@ -195,6 +196,22 @@ static int basicUnitTests(U32 seed, double compressibility) if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; DISPLAYLEVEL(4, "OK \n"); + /* All zeroes test (#137 verif) */ + #define ZEROESLENGTH 100 + DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); + memset(CNBuffer, 0, ZEROESLENGTH); + result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1); + if (ZSTD_isError(result)) goto _output_error; + cSize = result; + DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); + + DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); + result = ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize); + if (ZSTD_isError(result)) goto _output_error; + if (result != ZEROESLENGTH) goto _output_error; + DISPLAYLEVEL(4, "OK \n"); + + /* Dictionary and Duplication tests */ { ZSTD_CCtx* ctxOrig = ZSTD_createCCtx(); From 4ab9c913a585da4f703a859cd1a7aecef7f8958f Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 4 Mar 2016 19:17:31 +0100 Subject: [PATCH 029/219] MEM_readMINMATCH replaced with inline function --- lib/mem.h | 10 +++ lib/zstd_compress.c | 35 ++++----- lib/zstd_opt.h | 161 ++++++++++++++++++++-------------------- lib/zstd_opt_internal.h | 41 ++-------- 4 files changed, 114 insertions(+), 133 deletions(-) diff --git a/lib/mem.h b/lib/mem.h index 0e357e530..a369033b1 100644 --- a/lib/mem.h +++ b/lib/mem.h @@ -188,6 +188,16 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value) #endif /* MEM_FORCE_MEMORY_ACCESS */ +MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length) +{ + switch (length) + { + default : + case 4 : return MEM_read32(memPtr); + case 3 : return MEM_read32(memPtr)<<8; + } +} + MEM_STATIC U16 MEM_readLE16(const void* memPtr) { if (MEM_isLittleEndian()) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 50fa2fdc2..637ffb982 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -207,15 +207,16 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->seqStore.litLengthStart = zc->seqStore.litStart + blockSize; zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (blockSize>>2); zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); + if (params.strategy == ZSTD_btopt) { + zc->seqStore.litFreq = (U32*)((void*)(zc->seqStore.dumpsStart + (blockSize>>2))); + zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1<seqStore.matchTable = (ZSTD_match_t*)((void*)(zc->seqStore.offCodeFreq + (1<seqStore.priceTable = (ZSTD_optimal_t*)((void*)(zc->seqStore.matchTable + ZSTD_OPT_NUM+1)); + zc->seqStore.litLengthSum = 0; + } - zc->seqStore.litFreq = (U32*)((void*)(zc->seqStore.dumpsStart + (blockSize>>2))); - zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1<seqStore.matchTable = (ZSTD_match_t*)(zc->seqStore.offCodeFreq + (1<seqStore.priceTable = (ZSTD_optimal_t*)(zc->seqStore.matchTable + ZSTD_OPT_NUM+1); - - zc->seqStore.litLengthSum = 0; zc->hbSize = 0; zc->stage = 0; zc->loadedDictEnd = 0; @@ -1661,10 +1662,7 @@ _storeSequence: static void ZSTD_compressBlock_btopt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) { - if (ctx->params.searchLength == 3) - ZSTD_compressBlock_opt_generic3(ctx, src, srcSize, 2); - else - ZSTD_compressBlock_opt_generic4(ctx, src, srcSize, 2); + ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 2); } static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) @@ -1883,10 +1881,7 @@ 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) { - if (ctx->params.searchLength == 3) - ZSTD_compressBlock_opt_extDict_generic3(ctx, src, srcSize, 2); - else - ZSTD_compressBlock_opt_extDict_generic4(ctx, src, srcSize, 2); + ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 2); } @@ -2404,10 +2399,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 */ + { 0, 14, 15, 15, 0, 14, 3,256, ZSTD_btopt }, /* level 21-2 */ + { 0, 14, 15, 15, 0, 15, 3,256, ZSTD_btopt }, /* level 21-3 */ + { 0, 14, 15, 15, 0, 16, 3,256, ZSTD_btopt }, /* level 21-4 */ + { 0, 14, 15, 15, 0, 17, 3,256, ZSTD_btopt }, /* level 21-5 */ }, }; diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index f61485987..66700514f 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -41,7 +41,6 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT 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+1) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]+1); @@ -95,36 +94,36 @@ static U32 ZSTD_INSERTBTANDGETALLMATCHES ( U32 dummy32; /* to be nullified at the end */ U32 mnum = 0; - size_t bestLength = MINMATCHOPT-1; + const U32 minMatch = (mls == 3) ? 3 : 4; + size_t bestLength = minMatch-1; hashTable[h] = current; /* Update Hash Table */ -#if MINMATCHOPT == 3 - /* HC3 match finder */ - U32 matchIndex3 = ZSTD_insertAndFindFirstIndexHash3 (zc, ip); + 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); - } else { - match = dictBase + matchIndex3; - if (MEM_readMINMATCH(match) == MEM_readMINMATCH(ip)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */ - currentMl = ZSTD_count_2segments(ip+MINMATCHOPT, match+MINMATCHOPT, iLimit, dictEnd, prefixStart) + MINMATCHOPT; - } + 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); + } else { + match = dictBase + matchIndex3; + if (MEM_readMINMATCH(match, minMatch) == MEM_readMINMATCH(ip, minMatch)) /* 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; - mnum++; - if (currentMl > ZSTD_OPT_NUM) return mnum; - if (ip+currentMl == iLimit) return mnum; /* best possible, and avoid read overflow*/ + /* save best solution */ + if (currentMl > bestLength) { + bestLength = currentMl; + matches[mnum].off = current - matchIndex3; + matches[mnum].len = (U32)currentMl; + 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); @@ -266,8 +265,9 @@ 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; + const U32 mls = ctx->params.searchLength; + const U32 minMatch = (ctx->params.searchLength == 3) ? 3 : 4; ZSTD_optimal_t* opt = seqStorePtr->priceTable; ZSTD_match_t* matches = seqStorePtr->matchTable; @@ -295,9 +295,9 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, opt[0].litlen = (U32)(ip - litstart); /* check repCode */ - if (MEM_readMINMATCH(ip+1) == MEM_readMINMATCH(ip+1 - rep_1)) { + if (MEM_readMINMATCH(ip+1, minMatch) == MEM_readMINMATCH(ip+1 - rep_1, minMatch)) { /* repcode : we take it */ - mlen = (U32)ZSTD_count(ip+1+MINMATCHOPT, ip+1+MINMATCHOPT-rep_1, iend) + MINMATCHOPT; + mlen = (U32)ZSTD_count(ip+1+minMatch, ip+1+minMatch-rep_1, iend) + minMatch; 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) { @@ -307,11 +307,11 @@ 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 - minMatch); 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 >= MINMATCHOPT); + } while (mlen >= minMatch); } match_num = ZSTD_BTGETALLMATCHES_SELECTMLS(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ @@ -331,7 +331,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, goto _storeSequence; } - best_mlen = (last_pos) ? last_pos : MINMATCHOPT; + best_mlen = (last_pos) ? last_pos : minMatch; // set prices using matches at position = 0 for (u = 0; u < match_num; u++) { @@ -340,13 +340,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 - minMatch); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; } } - if (last_pos < MINMATCHOPT) { ip++; continue; } + if (last_pos < minMatch) { ip++; continue; } /* check further positions */ for (cur = 1; cur <= last_pos; cur++) { @@ -401,8 +401,8 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, 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 - mlen = (U32)ZSTD_count(inr+MINMATCHOPT, inr+MINMATCHOPT - cur_rep, iend) + MINMATCHOPT; + if (MEM_readMINMATCH(inr, minMatch) == MEM_readMINMATCH(inr - cur_rep, minMatch)) { // 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); if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) { @@ -416,12 +416,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, inr-litlen, 0, mlen); + price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, inr-litlen, 0, mlen - minMatch); } else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen - minMatch); } else { litlen = 0; - price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen); + price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen - minMatch); } best_mlen = mlen; @@ -431,7 +431,7 @@ 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 >= MINMATCHOPT); + } while (mlen >= minMatch); } match_num = ZSTD_BTGETALLMATCHES_SELECTMLS(ctx, inr, ip, iend, maxSearches, mls, matches); @@ -444,7 +444,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, goto _storeSequence; } - best_mlen = (best_mlen > MINMATCHOPT) ? best_mlen : MINMATCHOPT; + best_mlen = (best_mlen > minMatch) ? best_mlen : minMatch; /* set prices using matches at position = cur */ for (u = 0; u < match_num; u++) { @@ -457,12 +457,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 - minMatch); else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); } 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 - minMatch); } // 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); @@ -530,7 +530,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) && (ml2 < mlen || ml2 < MINMATCHOPT)) { + if ((offset >= 8) && (ml2 < mlen || ml2 < minMatch)) { 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); } @@ -538,23 +538,23 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ 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); - ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); + ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-minMatch); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-minMatch); anchor = ip = ip + mlen; } /* for (cur=0; cur < last_pos; ) */ /* check immediate repcode */ while ((anchor >= prefixStart + rep_2) && (anchor <= ilimit) - && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(anchor - rep_2)) ) { + && (MEM_readMINMATCH(anchor, minMatch) == MEM_readMINMATCH(anchor - rep_2, minMatch)) ) { /* store sequence */ - best_mlen = (U32)ZSTD_count(anchor+MINMATCHOPT, anchor+MINMATCHOPT-rep_2, iend); + best_mlen = (U32)ZSTD_count(anchor+minMatch, anchor+minMatch-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+MINMATCHOPT; + anchor += best_mlen+minMatch; continue; /* faster when present ... (?) */ } if (anchor > ip) ip = anchor; @@ -590,8 +590,9 @@ 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; + const U32 mls = ctx->params.searchLength; + const U32 minMatch = (ctx->params.searchLength == 3) ? 3 : 4; ZSTD_optimal_t* opt = seqStorePtr->priceTable; ZSTD_match_t* matches = seqStorePtr->matchTable; @@ -623,10 +624,10 @@ 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_readMINMATCH(ip+1) == MEM_readMINMATCH(repMatch)) ) { + && (MEM_readMINMATCH(ip+1, minMatch) == MEM_readMINMATCH(repMatch, minMatch)) ) { /* repcode detected we should take it */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; - mlen = (U32)ZSTD_count_2segments(ip+1+MINMATCHOPT, repMatch+MINMATCHOPT, iend, repEnd, prefixStart) + MINMATCHOPT; + mlen = (U32)ZSTD_count_2segments(ip+1+minMatch, repMatch+minMatch, iend, repEnd, prefixStart) + minMatch; 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) { @@ -636,14 +637,14 @@ 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 - minMatch); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); mlen--; - } while (mlen >= MINMATCHOPT); + } while (mlen >= minMatch); } } - best_mlen = (last_pos) ? last_pos : MINMATCHOPT; + best_mlen = (last_pos) ? last_pos : minMatch; match_num = ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ @@ -669,13 +670,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 - minMatch); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; } } - if (last_pos < MINMATCHOPT) { + if (last_pos < minMatch) { // ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ ip++; continue; } @@ -737,10 +738,10 @@ 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_readMINMATCH(inr) == MEM_readMINMATCH(repMatch)) ) { + && (MEM_readMINMATCH(inr, minMatch) == MEM_readMINMATCH(repMatch, minMatch)) ) { /* repcode detected */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; - mlen = (U32)ZSTD_count_2segments(inr+MINMATCHOPT, repMatch+MINMATCHOPT, iend, repEnd, prefixStart) + MINMATCHOPT; + mlen = (U32)ZSTD_count_2segments(inr+minMatch, repMatch+minMatch, iend, repEnd, prefixStart) + 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); if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) { @@ -754,12 +755,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, inr-litlen, 0, mlen); + price = opt[cur - litlen].price + ZSTD_GETPRICE(seqStorePtr, litlen, inr-litlen, 0, mlen - minMatch); } else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen - minMatch); } else { litlen = 0; - price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen); + price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen - minMatch); } best_mlen = mlen; @@ -770,10 +771,10 @@ 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 >= MINMATCHOPT); + } while (mlen >= minMatch); } - best_mlen = (best_mlen > MINMATCHOPT) ? best_mlen : MINMATCHOPT; + best_mlen = (best_mlen > minMatch) ? best_mlen : minMatch; 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); @@ -796,12 +797,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 - minMatch); else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen); + price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); } 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 - minMatch); } // 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); @@ -874,7 +875,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set else ml2 = (U32)ZSTD_count(ip, ip-offset, iend); } else ml2 = (U32)ZSTD_count(ip, ip-rep_1, iend); - if ((offset >= 8) && (ml2 < mlen || ml2 < MINMATCHOPT)) { + if ((offset >= 8) && (ml2 < mlen || ml2 < minMatch)) { 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); } @@ -882,8 +883,8 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set 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); - ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCHOPT); + ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-minMatch); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-minMatch); anchor = ip = ip + mlen; } @@ -891,22 +892,22 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set /* check immediate repcode */ while ((anchor >= base + lowLimit + rep_2) && (anchor <= ilimit)) { if ((anchor - rep_2) >= prefixStart) { - if (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(anchor - rep_2)) - mlen = (U32)ZSTD_count(anchor+MINMATCHOPT, anchor - rep_2 + MINMATCHOPT, iend) + MINMATCHOPT; + if (MEM_readMINMATCH(anchor, minMatch) == MEM_readMINMATCH(anchor - rep_2, minMatch)) + mlen = (U32)ZSTD_count(anchor+minMatch, anchor - rep_2 + minMatch, iend) + minMatch; else break; } else { const BYTE* repMatch = dictBase + ((anchor-base) - rep_2); - if ((repMatch + MINMATCHOPT <= dictEnd) && (MEM_readMINMATCH(anchor) == MEM_readMINMATCH(repMatch))) - mlen = (U32)ZSTD_count_2segments(anchor+MINMATCHOPT, repMatch+MINMATCHOPT, iend, dictEnd, prefixStart) + MINMATCHOPT; + if ((repMatch + minMatch <= dictEnd) && (MEM_readMINMATCH(anchor, minMatch) == MEM_readMINMATCH(repMatch, minMatch))) + mlen = (U32)ZSTD_count_2segments(anchor+minMatch, repMatch+minMatch, iend, dictEnd, prefixStart) + minMatch; else break; } 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-MINMATCHOPT); - ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, mlen-MINMATCHOPT); + ZSTD_updatePrice(seqStorePtr, 0, anchor, 0, mlen-minMatch); + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, mlen-minMatch); anchor += mlen; } #else @@ -917,14 +918,14 @@ _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_readMINMATCH(anchor) == MEM_readMINMATCH(repMatch)) ) { + && (MEM_readMINMATCH(anchor, minMatch) == MEM_readMINMATCH(repMatch, minMatch)) ) { /* repcode detected, let's take it */ const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; - mlen = (U32)ZSTD_count_2segments(anchor+MINMATCHOPT, repMatch+MINMATCHOPT, iend, repEnd, prefixStart) + MINMATCHOPT; + mlen = (U32)ZSTD_count_2segments(anchor+minMatch, repMatch+minMatch, iend, repEnd, prefixStart) + minMatch; 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-MINMATCHOPT); - ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, mlen-MINMATCHOPT); + ZSTD_updatePrice(seqStorePtr, 0, anchor, 0, mlen-minMatch); + ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, mlen-minMatch); anchor += mlen; continue; /* faster when present ... (?) */ } diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h index b980eab08..6dd5f5165 100644 --- a/lib/zstd_opt_internal.h +++ b/lib/zstd_opt_internal.h @@ -168,39 +168,14 @@ static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_CCtx* zc, const BYTE* ip) 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 +#define ZSTD_GETPRICE ZSTD_getPrice +#define ZSTD_INSERTBTANDGETALLMATCHES ZSTD_insertBtAndGetAllMatches +#define ZSTD_BTGETALLMATCHES ZSTD_BtGetAllMatches +#define ZSTD_BTGETALLMATCHES_SELECTMLS ZSTD_BtGetAllMatches_selectMLS +#define ZSTD_BTGETALLMATCHES_EXTDICT ZSTD_BtGetAllMatches_extDict +#define ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT ZSTD_BtGetAllMatches_selectMLS_extDict +#define ZSTD_COMPRESSBLOCK_OPT_GENERIC ZSTD_compressBlock_opt_generic +#define ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC ZSTD_compressBlock_opt_extDict_generic #include "zstd_opt.h" From 944d0d22852696d25e0b749961ab25236a92d45f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 4 Mar 2016 19:26:59 +0100 Subject: [PATCH 030/219] updated fse version --- lib/fse.c | 53 +++++++++--------- lib/fse.h | 164 ++++++++++++++++++++++++------------------------------ 2 files changed, 100 insertions(+), 117 deletions(-) diff --git a/lib/fse.c b/lib/fse.c index 986a0da15..a445f3285 100644 --- a/lib/fse.c +++ b/lib/fse.c @@ -628,12 +628,12 @@ size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, /*-************************************************************** * FSE Compression Code ****************************************************************/ -/*! -FSE_CTable is a variable size structure which contains : - U16 tableLog; - U16 maxSymbolValue; - U16 nextStateNumber[1 << tableLog]; // This size is variable - FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1]; // This size is variable +/*! FSE_sizeof_CTable() : + FSE_CTable is a variable size structure which contains : + `U16 tableLog;` + `U16 maxSymbolValue;` + `U16 nextStateNumber[1 << tableLog];` // This size is variable + `FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1];` // This size is variable Allocation is manual, since C standard does not support variable-size structures. */ @@ -654,10 +654,7 @@ FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog) return (FSE_CTable*)malloc(size); } -void FSE_freeCTable (FSE_CTable* ct) -{ - free(ct); -} +void FSE_freeCTable (FSE_CTable* ct) { free(ct); } /* provides the minimum logSize to safely represent a distribution */ static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue) @@ -888,31 +885,32 @@ static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize, const FSE_CTable* ct, const unsigned fast) { const BYTE* const istart = (const BYTE*) src; - const BYTE* ip; const BYTE* const iend = istart + srcSize; + const BYTE* ip=iend; size_t errorCode; BIT_CStream_t bitC; FSE_CState_t CState1, CState2; - /* init */ + if (srcSize <= 2) return 0; errorCode = BIT_initCStream(&bitC, dst, dstSize); if (FSE_isError(errorCode)) return 0; - FSE_initCState(&CState1, ct); - CState2 = CState1; - - ip=iend; #define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s)) - /* join to even */ if (srcSize & 1) { + FSE_initCState2(&CState1, ct, *--ip); + FSE_initCState2(&CState2, ct, *--ip); FSE_encodeSymbol(&bitC, &CState1, *--ip); FSE_FLUSHBITS(&bitC); + } else { + FSE_initCState2(&CState2, ct, *--ip); + FSE_initCState2(&CState1, ct, *--ip); } /* join to mod 4 */ + srcSize -= 2; if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) { /* test bit 2 */ FSE_encodeSymbol(&bitC, &CState2, *--ip); FSE_encodeSymbol(&bitC, &CState1, *--ip); @@ -1106,24 +1104,25 @@ FORCE_INLINE size_t FSE_decompress_usingDTable_generic( /* tail */ /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ while (1) { - if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) ) - break; + if (op>(omax-2)) return ERROR(dstSize_tooSmall); *op++ = FSE_GETSYMBOL(&state1); - if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) ) + if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { + *op++ = FSE_GETSYMBOL(&state2); break; + } + + if (op>(omax-2)) return ERROR(dstSize_tooSmall); *op++ = FSE_GETSYMBOL(&state2); - } - /* end ? */ - if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2)) - return op-ostart; + if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { + *op++ = FSE_GETSYMBOL(&state1); + break; + } } - if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */ - - return ERROR(corruption_detected); + return op-ostart; } diff --git a/lib/fse.h b/lib/fse.h index db6f49cfa..6dce68300 100644 --- a/lib/fse.h +++ b/lib/fse.h @@ -1,7 +1,7 @@ /* ****************************************************************** - FSE : Finite State Entropy coder - header file - Copyright (C) 2013-2015, Yann Collet. + FSE : Finite State Entropy codec + Public Prototypes declaration + Copyright (C) 2013-2016, Yann Collet. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) @@ -30,7 +30,6 @@ You can contact the author at : - Source repository : https://github.com/Cyan4973/FiniteStateEntropy - - Public forum : https://groups.google.com/forum/#!forum/lz4c ****************************************************************** */ #ifndef FSE_H #define FSE_H @@ -40,8 +39,8 @@ extern "C" { #endif -/* ***************************************** -* Includes +/*-***************************************** +* Dependencies ******************************************/ #include /* size_t, ptrdiff_t */ @@ -49,32 +48,32 @@ extern "C" { /*-**************************************** * FSE simple functions ******************************************/ -size_t FSE_compress(void* dst, size_t maxDstSize, - const void* src, size_t srcSize); -size_t FSE_decompress(void* dst, size_t maxDstSize, - const void* cSrc, size_t cSrcSize); -/*! -FSE_compress(): +/*! FSE_compress() : Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'. - 'dst' buffer must be already allocated. Compression runs faster is maxDstSize >= FSE_compressBound(srcSize) - return : size of compressed data (<= maxDstSize) + 'dst' buffer must be already allocated. Compression runs faster is dstCapacity >= FSE_compressBound(srcSize). + @return : size of compressed data (<= dstCapacity). Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead. if FSE_isError(return), compression failed (more details using FSE_getErrorName()) +*/ +size_t FSE_compress(void* dst, size_t dstCapacity, + const void* src, size_t srcSize); -FSE_decompress(): +/*! FSE_decompress(): Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', - into already allocated destination buffer 'dst', of size 'maxDstSize'. - return : size of regenerated data (<= maxDstSize) - or an error code, which can be tested using FSE_isError() + into already allocated destination buffer 'dst', of size 'dstCapacity'. + @return : size of regenerated data (<= maxDstSize), + or an error code, which can be tested using FSE_isError() . - ** Important ** : FSE_decompress() doesn't decompress non-compressible nor RLE data !!! + ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!! Why ? : making this distinction requires a header. Header management is intentionally delegated to the user layer, which can better manage special cases. */ +size_t FSE_decompress(void* dst, size_t dstCapacity, + const void* cSrc, size_t cSrcSize); -/* ***************************************** +/*-***************************************** * Tool functions ******************************************/ size_t FSE_compressBound(size_t size); /* maximum compressed size */ @@ -84,14 +83,13 @@ unsigned FSE_isError(size_t code); /* tells if a return value is an er const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */ -/* ***************************************** +/*-***************************************** * FSE advanced functions ******************************************/ -/*! -FSE_compress2(): +/*! FSE_compress2() : Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog' Both parameters can be defined as '0' to mean : use default value - return : size of compressed data + @return : size of compressed data Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc !!! if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression. if FSE_isError(return), it's an error code. @@ -99,7 +97,7 @@ FSE_compress2(): size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog); -/* ***************************************** +/*-***************************************** * FSE detailed API ******************************************/ /*! @@ -122,65 +120,56 @@ or to save and provide normalized distribution using external method. /* *** COMPRESSION *** */ -/*! -FSE_count(): - Provides the precise count of each byte within a table 'count' - 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). - *maxSymbolValuePtr will be updated if detected smaller than initial value. - @return : the count of the most frequent symbol (which is not identified) - if return == srcSize, there is only one symbol. - Can also return an error code, which can be tested with FSE_isError() */ +/*! FSE_count(): + Provides the precise count of each byte within a table 'count'. + 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). + *maxSymbolValuePtr will be updated if detected smaller than initial value. + @return : the count of the most frequent symbol (which is not identified). + if return == srcSize, there is only one symbol. + Can also return an error code, which can be tested with FSE_isError(). */ size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); -/*! -FSE_optimalTableLog(): - dynamically downsize 'tableLog' when conditions are met. - It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. - return : recommended tableLog (necessarily <= initial 'tableLog') */ +/*! FSE_optimalTableLog(): + dynamically downsize 'tableLog' when conditions are met. + It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. + @return : recommended tableLog (necessarily <= initial 'tableLog') */ unsigned FSE_optimalTableLog(unsigned tableLog, size_t srcSize, unsigned maxSymbolValue); -/*! -FSE_normalizeCount(): - normalize counters so that sum(count[]) == Power_of_2 (2^tableLog) - 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). - return : tableLog, - or an errorCode, which can be tested using FSE_isError() */ +/*! FSE_normalizeCount(): + normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) + 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). + @return : tableLog, + or an errorCode, which can be tested using FSE_isError() */ size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue); -/*! -FSE_NCountWriteBound(): - Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog' - Typically useful for allocation purpose. */ +/*! FSE_NCountWriteBound(): + Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'. + Typically useful for allocation purpose. */ size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); -/*! -FSE_writeNCount(): - Compactly save 'normalizedCounter' into 'buffer'. - return : size of the compressed table - or an errorCode, which can be tested using FSE_isError() */ +/*! FSE_writeNCount(): + Compactly save 'normalizedCounter' into 'buffer'. + @return : size of the compressed table, + or an errorCode, which can be tested using FSE_isError(). */ size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); -/*! -Constructor and Destructor of type FSE_CTable - Note that its size depends on 'tableLog' and 'maxSymbolValue' */ +/*! Constructor and Destructor of FSE_CTable. + Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */ typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ FSE_CTable* FSE_createCTable (unsigned tableLog, unsigned maxSymbolValue); void FSE_freeCTable (FSE_CTable* ct); -/*! -FSE_buildCTable(): - Builds @ct, which must be already allocated, using FSE_createCTable() - return : 0 - or an errorCode, which can be tested using FSE_isError() */ +/*! FSE_buildCTable(): + Builds `ct`, which must be already allocated, using FSE_createCTable(). + @return : 0, or an errorCode, which can be tested using FSE_isError() */ size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); -/*! -FSE_compress_usingCTable(): - Compress @src using @ct into @dst which must be already allocated - return : size of compressed data (<= @dstCapacity) - or 0 if compressed data could not fit into @dst - or an errorCode, which can be tested using FSE_isError() */ +/*! FSE_compress_usingCTable(): + Compress `src` using `ct` into `dst` which must be already allocated. + @return : size of compressed data (<= `dstCapacity`), + or 0 if compressed data could not fit into `dst`, + or an errorCode, which can be tested using FSE_isError() */ size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct); /*! @@ -221,7 +210,7 @@ If there is an error, both functions will return an ErrorCode (which can be test 'CTable' can then be used to compress 'src', with FSE_compress_usingCTable(). Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize' -The function returns the size of compressed data (without header), necessarily <= @dstCapacity. +The function returns the size of compressed data (without header), necessarily <= `dstCapacity`. If it returns '0', compressed data could not fit into 'dst'. If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). */ @@ -229,34 +218,29 @@ If there is an error, the function will return an ErrorCode (which can be tested /* *** DECOMPRESSION *** */ -/*! -FSE_readNCount(): - Read compactly saved 'normalizedCounter' from 'rBuffer'. - return : size read from 'rBuffer' - or an errorCode, which can be tested using FSE_isError() - maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ +/*! FSE_readNCount(): + Read compactly saved 'normalizedCounter' from 'rBuffer'. + @return : size read from 'rBuffer', + or an errorCode, which can be tested using FSE_isError(). + maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize); -/*! -Constructor and Destructor of type FSE_DTable +/*! Constructor and Destructor of FSE_DTable. Note that its size depends on 'tableLog' */ typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ FSE_DTable* FSE_createDTable(unsigned tableLog); void FSE_freeDTable(FSE_DTable* dt); -/*! -FSE_buildDTable(): - Builds 'dt', which must be already allocated, using FSE_createDTable() - return : 0, - or an errorCode, which can be tested using FSE_isError() */ +/*! FSE_buildDTable(): + Builds 'dt', which must be already allocated, using FSE_createDTable(). + return : 0, or an errorCode, which can be tested using FSE_isError() */ size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); -/*! -FSE_decompress_usingDTable(): - Decompress compressed source @cSrc of size @cSrcSize using @dt - into @dst which must be already allocated. - return : size of regenerated data (necessarily <= @dstCapacity) - or an errorCode, which can be tested using FSE_isError() */ +/*! FSE_decompress_usingDTable(): + Decompress compressed source `cSrc` of size `cSrcSize` using `dt` + into `dst` which must be already allocated. + @return : size of regenerated data (necessarily <= `dstCapacity`), + or an errorCode, which can be tested using FSE_isError() */ size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt); /*! @@ -281,9 +265,9 @@ This is performed by the function FSE_buildDTable(). The space required by 'FSE_DTable' must be already allocated using FSE_createDTable(). If there is an error, the function will return an error code, which can be tested using FSE_isError(). -'FSE_DTable' can then be used to decompress 'cSrc', with FSE_decompress_usingDTable(). -'cSrcSize' must be strictly correct, otherwise decompression will fail. -FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=maxDstSize). +`FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable(). +`cSrcSize` must be strictly correct, otherwise decompression will fail. +FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`). If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small) */ From 805d2a7465bd921cd5b2d0044875bb636ffc2952 Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 4 Mar 2016 19:31:57 +0100 Subject: [PATCH 031/219] removed zstd_opt_internal.h --- lib/zstd_compress.c | 2 +- lib/zstd_opt.h | 209 +++++++++++++++++++++++++++++++++------- lib/zstd_opt_internal.h | 182 ---------------------------------- 3 files changed, 173 insertions(+), 220 deletions(-) delete mode 100644 lib/zstd_opt_internal.h diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 637ffb982..8f14ff036 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -1380,7 +1380,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" +#include "zstd_opt.h" /** Tree updater, providing best match */ static size_t ZSTD_BtFindBestMatch_extDict ( diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 66700514f..f6f9acecf 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -31,10 +31,83 @@ - Zstd source repository : https://www.zstd.net */ -/* Note : this file is intended to be included within zstd_opt_internal.h */ +/* Note : this file is intended to be included within zstd_compress.c */ -FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) +#define ZSTD_FREQ_DIV 5 + +/*-************************************* +* Price functions for optimal parser +***************************************/ +MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) +{ + unsigned u; + + if (ssPtr->litLengthSum == 0) { + ssPtr->litSum = 2*(1<litLengthSum = 1*(1<matchLengthSum = 1*(1<offCodeSum = 1*(1<matchSum = 2*(1<litFreq[u] = 2; + 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->offCodeSum = 0; + ssPtr->matchSum = 0; + ssPtr->litSum = 0; + + for (u=0; u<=MaxLit; u++) { + ssPtr->litFreq[u] = 1 + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litSum += ssPtr->litFreq[u]; + } + for (u=0; u<=MaxLL; u++) { + ssPtr->litLengthFreq[u] = 1 + (ssPtr->litLengthFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->litLengthSum += ssPtr->litLengthFreq[u]; + } + for (u=0; u<=MaxML; u++) { + ssPtr->matchLengthFreq[u] = 1 + (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] = 1 + (ssPtr->offCodeFreq[u]>>ZSTD_FREQ_DIV); + ssPtr->offCodeSum += ssPtr->offCodeFreq[u]; + } + } +} + + +FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) +{ + U32 price, u; + + if (litLength == 0) + return ZSTD_highbit(seqStorePtr->litLengthSum+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[0]+1); + + /* literals */ + price = litLength * ZSTD_highbit(seqStorePtr->litSum+1); + for (u=0; u < litLength; 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+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]+1); + + return price; +} + + +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) + 1 : 0; @@ -63,10 +136,72 @@ FORCE_INLINE U32 ZSTD_GETPRICE(seqStore_t* seqStorePtr, U32 litLength, const BYT } +MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals, U32 offset, U32 matchLength) +{ + U32 u; + + /* literals */ + seqStorePtr->litSum += litLength; + for (u=0; u < litLength; u++) + seqStorePtr->litFreq[literals[u]]++; + + /* literal Length */ + seqStorePtr->litLengthSum++; + if (litLength >= MaxLL) + seqStorePtr->litLengthFreq[MaxLL]++; + else + seqStorePtr->litLengthFreq[litLength]++; + + /* match offset */ + seqStorePtr->offCodeSum++; + BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset+1) + 1 : 0; + seqStorePtr->offCodeFreq[offCode]++; + + /* match Length */ + seqStorePtr->matchLengthSum++; + if (matchLength >= MaxML) + seqStorePtr->matchLengthFreq[MaxML]++; + else + seqStorePtr->matchLengthFreq[matchLength]++; +} + + +#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 ***************************************/ -static U32 ZSTD_INSERTBTANDGETALLMATCHES ( +/* 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)]; +} + + +static U32 ZSTD_insertBtAndGetAllMatches ( ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iLimit, U32 nbCompares, const U32 mls, @@ -191,18 +326,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) @@ -211,25 +346,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) @@ -238,9 +373,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); } } @@ -249,7 +384,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) { @@ -307,14 +442,14 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen - minMatch); 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); } - match_num = ZSTD_BTGETALLMATCHES_SELECTMLS(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; } @@ -340,7 +475,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, litstart, matches[u].off, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; @@ -416,12 +551,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, inr-litlen, 0, mlen - minMatch); + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen - minMatch); } else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen - minMatch); } else { litlen = 0; - price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen - minMatch); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen - minMatch); } best_mlen = mlen; @@ -434,7 +569,7 @@ void ZSTD_COMPRESSBLOCK_OPT_GENERIC(ZSTD_CCtx* ctx, } while (mlen >= minMatch); } - match_num = ZSTD_BTGETALLMATCHES_SELECTMLS(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) { @@ -457,12 +592,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 - minMatch); + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen - minMatch); else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); } else { litlen = 0; - price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, matches[u].off, mlen - minMatch); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen - minMatch); } // 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); @@ -570,7 +705,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) { @@ -637,7 +772,7 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, litlen = opt[0].litlen + 1; do { - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen - minMatch); if (mlen + 1 > last_pos || price < opt[mlen + 1].price) SET_PRICE(mlen + 1, mlen, 0, litlen, price); mlen--; @@ -646,7 +781,7 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, best_mlen = (last_pos) ? last_pos : minMatch; - 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; } @@ -670,7 +805,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, litstart, matches[u].off, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); if (mlen > last_pos || price < opt[mlen].price) SET_PRICE(mlen, mlen, matches[u].off, litlen, price); mlen++; @@ -755,12 +890,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, inr-litlen, 0, mlen - minMatch); + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr-litlen, 0, mlen - minMatch); } else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, 0, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, 0, mlen - minMatch); } else { litlen = 0; - price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, 0, mlen - minMatch); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, 0, mlen - minMatch); } best_mlen = mlen; @@ -776,7 +911,7 @@ void ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC(ZSTD_CCtx* ctx, best_mlen = (best_mlen > minMatch) ? best_mlen : minMatch; - 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) { @@ -797,12 +932,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 - minMatch); + price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip+cur-litlen, matches[u].off, mlen - minMatch); else - price = ZSTD_GETPRICE(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); + price = ZSTD_getPrice(seqStorePtr, litlen, litstart, matches[u].off, mlen - minMatch); } else { litlen = 0; - price = opt[cur].price + ZSTD_GETPRICE(seqStorePtr, 0, NULL, matches[u].off, mlen - minMatch); + price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off, mlen - minMatch); } // 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); diff --git a/lib/zstd_opt_internal.h b/lib/zstd_opt_internal.h deleted file mode 100644 index 6dd5f5165..000000000 --- a/lib/zstd_opt_internal.h +++ /dev/null @@ -1,182 +0,0 @@ -/* - 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_FREQ_DIV 5 - - -MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) -{ - unsigned u; - - if (ssPtr->litLengthSum == 0) { - ssPtr->litSum = 2*(1<litLengthSum = 1*(1<matchLengthSum = 1*(1<offCodeSum = 1*(1<matchSum = 2*(1<litFreq[u] = 2; - 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->offCodeSum = 0; - ssPtr->matchSum = 0; - ssPtr->litSum = 0; - - for (u=0; u<=MaxLit; u++) { - ssPtr->litFreq[u] = 1 + (ssPtr->litFreq[u]>>ZSTD_FREQ_DIV); - ssPtr->litSum += ssPtr->litFreq[u]; - } - for (u=0; u<=MaxLL; u++) { - ssPtr->litLengthFreq[u] = 1 + (ssPtr->litLengthFreq[u]>>ZSTD_FREQ_DIV); - ssPtr->litLengthSum += ssPtr->litLengthFreq[u]; - } - for (u=0; u<=MaxML; u++) { - ssPtr->matchLengthFreq[u] = 1 + (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] = 1 + (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; - for (u=0; u < litLength; u++) - seqStorePtr->litFreq[literals[u]]++; - - /* literal Length */ - seqStorePtr->litLengthSum++; - if (litLength >= MaxLL) - seqStorePtr->litLengthFreq[MaxLL]++; - else - seqStorePtr->litLengthFreq[litLength]++; - - /* match offset */ - seqStorePtr->offCodeSum++; - BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset+1) + 1 : 0; - seqStorePtr->offCodeFreq[offCode]++; - - /* match Length */ - seqStorePtr->matchLengthSum++; - if (matchLength >= MaxML) - seqStorePtr->matchLengthFreq[MaxML]++; - else - seqStorePtr->matchLengthFreq[matchLength]++; -} - -FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, const BYTE* literals) -{ - U32 price, u; - - if (litLength == 0) - return ZSTD_highbit(seqStorePtr->litLengthSum+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[0]+1); - - /* literals */ - price = litLength * ZSTD_highbit(seqStorePtr->litSum+1); - for (u=0; u < litLength; 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+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]+1); - - 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 ZSTD_GETPRICE ZSTD_getPrice -#define ZSTD_INSERTBTANDGETALLMATCHES ZSTD_insertBtAndGetAllMatches -#define ZSTD_BTGETALLMATCHES ZSTD_BtGetAllMatches -#define ZSTD_BTGETALLMATCHES_SELECTMLS ZSTD_BtGetAllMatches_selectMLS -#define ZSTD_BTGETALLMATCHES_EXTDICT ZSTD_BtGetAllMatches_extDict -#define ZSTD_BTGETALLMATCHES_SELECTMLS_EXTDICT ZSTD_BtGetAllMatches_selectMLS_extDict -#define ZSTD_COMPRESSBLOCK_OPT_GENERIC ZSTD_compressBlock_opt_generic -#define ZSTD_COMPRESSBLOCK_OPT_EXTDICT_GENERIC ZSTD_compressBlock_opt_extDict_generic -#include "zstd_opt.h" - - -#endif /* ZSTD_OPT_INTERNAL_H_MODULE */ From e29caf7d1c5327ff7c2d585d55dec505f53ed325 Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 4 Mar 2016 19:52:23 +0100 Subject: [PATCH 032/219] code cleanup --- lib/zstd_compress.c | 5 +++-- lib/zstd_internal.h | 4 ++-- lib/zstd_opt.h | 8 ++------ 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 8f14ff036..385b99dcb 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -731,7 +731,7 @@ 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 ZSTD_OPT_DEBUG == 3 if (offsetCode == 0) seqStorePtr->realRepSum++; seqStorePtr->realSeqSum++; seqStorePtr->realMatchSum += matchCode; @@ -1917,11 +1917,12 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, BYTE* const ostart = (BYTE*)dst; BYTE* op = ostart; const U32 maxDist = 1 << zc->params.windowLog; +#if ZSTD_OPT_DEBUG == 3 seqStore_t* ssPtr = &zc->seqStore; static U32 priceFunc = 0; - ssPtr->realMatchSum = ssPtr->realLitSum = ssPtr->realSeqSum = ssPtr->realRepSum = 1; ssPtr->priceFunc = priceFunc; +#endif while (remaining) { size_t cSize; diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 4948e2397..e136a89cc 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -206,13 +206,13 @@ typedef struct { U32 litLengthSum; U32 litSum; U32 offCodeSum; +#if ZSTD_OPT_DEBUG == 3 U32 realMatchSum; U32 realLitSum; U32 realSeqSum; U32 realRepSum; - U32 factor; - U32 factor2; U32 priceFunc; +#endif } seqStore_t; seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx); diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index f6f9acecf..780c5d415 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -118,16 +118,12 @@ 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 +#if ZSTD_OPT_DEBUG == 3 switch (seqStorePtr->priceFunc) { default: case 0: - return 1 + price + seqStorePtr->factor + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); + return 1 + price + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); case 1: - return 1 + price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); - case 2: - return 1 + price + seqStorePtr->factor + ((seqStorePtr->factor2) ? ((seqStorePtr->litSum>>4) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)) : 0); - case 3: return 1 + price; } #else From f3c6503e5517a855f065428eca44971c1a0dd482 Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 4 Mar 2016 20:04:25 +0100 Subject: [PATCH 033/219] resolve conflict in zstd_internal.h --- lib/zstd_internal.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index e136a89cc..b06097a21 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -105,12 +105,13 @@ static const size_t ZSTD_frameHeaderSize_min = 5; #define OffFSELog 9 #define MaxSeq MAX(MaxLL, MaxML) +#define LONGNBSEQ 0xFF00 + #define FSE_ENCODING_RAW 0 #define FSE_ENCODING_RLE 1 #define FSE_ENCODING_STATIC 2 #define FSE_ENCODING_DYNAMIC 3 - #define HufLog 12 #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ From 1df2594bfe9cdd8364193e943a4b37c7ce9d3d34 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 5 Mar 2016 18:43:21 +0100 Subject: [PATCH 034/219] new compression level scale for <= 16KB --- lib/zstd_compress.c | 48 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index e24016c7a..bc56b4e5c 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -2377,35 +2377,35 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 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, 4, 4, 4, ZSTD_greedy }, /* level 3.*/ + { 0, 14, 14, 14, 0, 3, 4, 4, ZSTD_lazy }, /* level 4.*/ + { 0, 14, 14, 14, 0, 4, 4, 4, ZSTD_lazy2 }, /* 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 */ - { 0, 14, 15, 15, 0, 14, 3,256, ZSTD_btopt }, /* level 21-2 */ - { 0, 14, 15, 15, 0, 15, 3,256, ZSTD_btopt }, /* level 21-3 */ - { 0, 14, 15, 15, 0, 16, 3,256, ZSTD_btopt }, /* level 21-4 */ - { 0, 14, 15, 15, 0, 17, 3,256, ZSTD_btopt }, /* level 21-5 */ + { 0, 14, 14, 14, 0, 6, 4, 4, ZSTD_lazy2 }, /* level 7.*/ + { 0, 14, 14, 14, 0, 7, 4, 4, ZSTD_lazy2 }, /* level 8.*/ + { 0, 14, 15, 14, 0, 6, 4, 4, ZSTD_btlazy2 }, /* level 9.*/ + { 0, 14, 15, 14, 0, 3, 3, 6, ZSTD_btopt }, /* level 10.*/ + { 0, 14, 15, 14, 0, 6, 3, 8, ZSTD_btopt }, /* level 11.*/ + { 0, 14, 15, 14, 0, 6, 3, 16, ZSTD_btopt }, /* level 12.*/ + { 0, 14, 15, 14, 0, 6, 3, 24, ZSTD_btopt }, /* level 13.*/ + { 0, 14, 15, 15, 0, 6, 3, 48, ZSTD_btopt }, /* level 14.*/ + { 0, 14, 15, 15, 0, 6, 3, 64, ZSTD_btopt }, /* level 15.*/ + { 0, 14, 15, 15, 0, 6, 3, 96, ZSTD_btopt }, /* level 16.*/ + { 0, 14, 15, 15, 0, 6, 3,128, ZSTD_btopt }, /* level 17.*/ + { 0, 14, 15, 15, 0, 6, 3,256, ZSTD_btopt }, /* level 18.*/ + { 0, 14, 15, 15, 0, 7, 3,256, ZSTD_btopt }, /* level 19.*/ + { 0, 14, 15, 15, 0, 8, 3,256, ZSTD_btopt }, /* level 20.*/ + { 0, 14, 15, 15, 0, 9, 3,256, ZSTD_btopt }, /* level 21.*/ + { 0, 14, 15, 15, 0, 10, 3,256, ZSTD_btopt }, /* level 22.*/ + { 0, 14, 15, 15, 0, 11, 3,256, ZSTD_btopt }, /* level 23.*/ + { 0, 14, 15, 15, 0, 12, 3,256, ZSTD_btopt }, /* level 24.*/ + { 0, 14, 15, 15, 0, 13, 3,256, ZSTD_btopt }, /* level 25.*/ }, }; -/*! ZSTD_getParams +/*! ZSTD_getParams() : * @return ZSTD_parameters structure for a selected compression level and srcSize. -* @srcSizeHint value is optional, select 0 if not known */ +* `srcSizeHint` value is optional, select 0 if not known */ ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSizeHint) { ZSTD_parameters result; From 89c9e1a847625aa143b79735a8f5a4633e95925a Mon Sep 17 00:00:00 2001 From: inikep Date: Sun, 6 Mar 2016 23:21:52 +0100 Subject: [PATCH 035/219] added missing "case 3:" in ZSTD_BtGetAllMatches --- lib/zstd_internal.h | 2 +- lib/zstd_opt.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index b06097a21..462161bd7 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -105,7 +105,7 @@ static const size_t ZSTD_frameHeaderSize_min = 5; #define OffFSELog 9 #define MaxSeq MAX(MaxLL, MaxML) -#define LONGNBSEQ 0xFF00 +#define LONGNBSEQ 0x7F00 #define FSE_ENCODING_RAW 0 #define FSE_ENCODING_RLE 1 diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 780c5d415..c6205089f 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -342,6 +342,7 @@ static U32 ZSTD_BtGetAllMatches_selectMLS ( switch(matchLengthSearch) { default : + case 3 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 3, 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); @@ -369,6 +370,7 @@ static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( switch(matchLengthSearch) { default : + case 3 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 3, 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); From e2446b0e36d01e4452ac23f6913b361242dac466 Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 7 Mar 2016 10:07:08 +0100 Subject: [PATCH 036/219] added support for MINMATCH=3 on big endian architecture changed ZSTD_HASHLOG3_MIN to 2 (4 bytes for MINMACH > 3) --- lib/mem.h | 25 ++++++++++++++----------- lib/zstd_compress.c | 2 +- lib/zstd_opt.h | 14 ++++++-------- lib/zstd_static.h | 2 +- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/mem.h b/lib/mem.h index a369033b1..ceafd57b4 100644 --- a/lib/mem.h +++ b/lib/mem.h @@ -187,17 +187,6 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value) #endif /* MEM_FORCE_MEMORY_ACCESS */ - -MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length) -{ - switch (length) - { - default : - case 4 : return MEM_read32(memPtr); - case 3 : return MEM_read32(memPtr)<<8; - } -} - MEM_STATIC U16 MEM_readLE16(const void* memPtr) { if (MEM_isLittleEndian()) @@ -286,6 +275,20 @@ MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val) MEM_writeLE64(memPtr, (U64)val); } + /* function safe only for comparisons */ +MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length) +{ + switch (length) + { + default : + case 4 : return MEM_read32(memPtr); + case 3 : if (MEM_isLittleEndian()) + return MEM_read32(memPtr)<<8; + else + return MEM_read32(memPtr)>>8; + } +} + #if defined (__cplusplus) } #endif diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index e24016c7a..87e8e72ee 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -876,7 +876,7 @@ static size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE ***************************************/ 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 size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h); } static const U32 prime4bytes = 2654435761U; static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; } diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index c6205089f..0c4a80f77 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -335,10 +335,9 @@ static U32 ZSTD_BtGetAllMatches ( 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 BYTE* ip, const BYTE* const iHighLimit, const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) { - (void)iLowLimit; /* unused */ switch(matchLengthSearch) { default : @@ -363,10 +362,9 @@ static U32 ZSTD_BtGetAllMatches_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 BYTE* ip, const BYTE* const iHighLimit, const U32 maxNbAttempts, const U32 matchLengthSearch, ZSTD_match_t* matches) { - (void)iLowLimit; switch(matchLengthSearch) { default : @@ -447,7 +445,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, } while (mlen >= minMatch); } - match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, ip, ip, iend, maxSearches, mls, matches); /* first search (depth 0) */ + match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, 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; } @@ -567,7 +565,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx, } while (mlen >= minMatch); } - match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, inr, ip, iend, maxSearches, mls, matches); + match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, inr, 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) { @@ -779,7 +777,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, best_mlen = (last_pos) ? last_pos : minMatch; - 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, 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; } @@ -909,7 +907,7 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, best_mlen = (best_mlen > minMatch) ? best_mlen : minMatch; - match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, ip, iend, maxSearches, mls, matches); + match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, 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) { diff --git a/lib/zstd_static.h b/lib/zstd_static.h index fa285ad4c..0effaa7ce 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -65,7 +65,7 @@ extern "C" { #define ZSTD_HASHLOG_MAX 28 #define ZSTD_HASHLOG_MIN 12 #define ZSTD_HASHLOG3_MAX 24 -#define ZSTD_HASHLOG3_MIN 12 +#define ZSTD_HASHLOG3_MIN 2 #define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1) #define ZSTD_SEARCHLOG_MIN 1 #define ZSTD_SEARCHLENGTH_MAX 7 From 4f7f79ef9dd03eb128abb9a489eb4daab10766ee Mon Sep 17 00:00:00 2001 From: inikep Date: Mon, 7 Mar 2016 16:14:58 +0100 Subject: [PATCH 037/219] fixed update of nextToUpdate --- lib/zstd_opt.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 0c4a80f77..68a3c315a 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -250,8 +250,8 @@ static U32 ZSTD_insertBtAndGetAllMatches ( matches[mnum].off = current - matchIndex3; matches[mnum].len = (U32)currentMl; mnum++; - if (currentMl > ZSTD_OPT_NUM) return mnum; - if (ip+currentMl == iLimit) return mnum; /* best possible, and avoid read overflow*/ + if (currentMl > ZSTD_OPT_NUM) goto update; + if (ip+currentMl == iLimit) goto update; /* best possible, and avoid read overflow*/ } } } @@ -316,6 +316,7 @@ static U32 ZSTD_insertBtAndGetAllMatches ( *smallerPtr = *largerPtr = 0; +update: zc->nextToUpdate = (matchEndIdx > current + 8) ? matchEndIdx - 8 : current+1; return mnum; } @@ -340,8 +341,8 @@ static U32 ZSTD_BtGetAllMatches_selectMLS ( { switch(matchLengthSearch) { - default : case 3 : return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 3, matches); + 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); @@ -367,8 +368,8 @@ static U32 ZSTD_BtGetAllMatches_selectMLS_extDict ( { switch(matchLengthSearch) { - default : case 3 : return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 3, matches); + 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); From 4ba85344e3205ab77e8f7b18a858c9eee48418bb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 7 Mar 2016 20:01:45 +0100 Subject: [PATCH 038/219] added test to generate lots of small sequences (3-bytes) --- programs/fuzzer.c | 68 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/programs/fuzzer.c b/programs/fuzzer.c index 7cbfd7940..50d496b96 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -196,22 +196,6 @@ static int basicUnitTests(U32 seed, double compressibility) if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; DISPLAYLEVEL(4, "OK \n"); - /* All zeroes test (#137 verif) */ - #define ZEROESLENGTH 100 - DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); - memset(CNBuffer, 0, ZEROESLENGTH); - result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1); - if (ZSTD_isError(result)) goto _output_error; - cSize = result; - DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); - - DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); - result = ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize); - if (ZSTD_isError(result)) goto _output_error; - if (result != ZEROESLENGTH) goto _output_error; - DISPLAYLEVEL(4, "OK \n"); - - /* Dictionary and Duplication tests */ { ZSTD_CCtx* ctxOrig = ZSTD_createCCtx(); @@ -344,6 +328,58 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "OK \n"); } + /* All zeroes test (#137 verif) */ + #define ZEROESLENGTH 100 + DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); + memset(CNBuffer, 0, ZEROESLENGTH); + result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1); + if (ZSTD_isError(result)) goto _output_error; + cSize = result; + DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); + + DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); + result = ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize); + if (ZSTD_isError(result)) goto _output_error; + if (result != ZEROESLENGTH) goto _output_error; + DISPLAYLEVEL(4, "OK \n"); + + /* nbSeq limit test */ + { + #define _3BYTESTESTLENGTH 131000 + #define NB3BYTESSEQLOG 9 + #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG) + #define NB3BYTESSEQMASK (NB3BYTESSEQ-1) + BYTE _3BytesSeqs[NB3BYTESSEQ][3]; + U32 r = 1; + int i; + + for (i=0; i < NB3BYTESSEQ; i++) { + _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&r) & 255); + _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&r) & 255); + _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&r) & 255); + } + + for (i=0; i < _3BYTESTESTLENGTH; ){ + U32 id = FUZ_rand(&r) & NB3BYTESSEQMASK; + ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0]; + ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1]; + ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2]; + i += 3; + } + + DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++); + result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), CNBuffer, _3BYTESTESTLENGTH, 19); + if (ZSTD_isError(result)) goto _output_error; + cSize = result; + DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100); + + DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++); + result = ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize); + if (ZSTD_isError(result)) goto _output_error; + if (result != _3BYTESTESTLENGTH) goto _output_error; + DISPLAYLEVEL(4, "OK \n"); + } + _end: free(CNBuffer); free(compressedBuffer); From 370b08e84041845f8ae7845aaad65b90cb6541f7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 8 Mar 2016 00:03:59 +0100 Subject: [PATCH 039/219] fix compression ratio for blocks <= 16 KB --- lib/zstd_compress.c | 52 ++++++++++++++++++++++--------------------- lib/zstd_decompress.c | 3 ++- programs/fuzzer.c | 1 + 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 4858831e3..a5127b748 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -147,13 +147,14 @@ void ZSTD_validateParams(ZSTD_parameters* params) const U32 btPlus = (params->strategy == ZSTD_btlazy2) || (params->strategy == ZSTD_btopt); const U32 searchLengthMax = (params->strategy == ZSTD_fast) ? ZSTD_SEARCHLENGTH_MAX : ZSTD_SEARCHLENGTH_MAX-1; const U32 searchLengthMin = (params->strategy == ZSTD_btopt) ? ZSTD_SEARCHLENGTH_MIN : ZSTD_SEARCHLENGTH_MIN+1; + const U32 hashLog3Min = (params->strategy == ZSTD_btopt) ? ZSTD_HASHLOG3_MIN : 0; /* validate params */ if (MEM_32bits()) if (params->windowLog > 25) params->windowLog = 25; /* 32 bits mode cannot flush > 24 bits */ 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->hashLog3, hashLog3Min, ZSTD_HASHLOG3_MAX); CLAMP(params->searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); CLAMP(params->searchLength, searchLengthMin, searchLengthMax); CLAMP(params->targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); @@ -174,10 +175,11 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, { /* note : params considered validated here */ 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 U32 contentLog = (params.strategy == ZSTD_fast) ? 1 : params.contentLog; const size_t tableSpace = ((1 << contentLog) + (1 << params.hashLog) + (1 << params.hashLog3)) * sizeof(U32); - const size_t neededSpace = tableSpace + (256*sizeof(U32)) + (3*blockSize) - + ((params.strategy == ZSTD_btopt) ? ((1<workSpaceSize < neededSpace) { free(zc->workSpace); @@ -204,7 +206,7 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->blockSize = blockSize; zc->seqStore.offsetStart = (U32*) (zc->seqStore.buffer); - zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart) + blockSize; + zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart + (blockSize>>2)); zc->seqStore.litStart = zc->seqStore.offCodeStart + (blockSize>>2); zc->seqStore.litLengthStart = zc->seqStore.litStart + blockSize; zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (blockSize>>2); @@ -227,9 +229,9 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, } -/*! ZSTD_copyCCtx -* Duplicate an existing context @srcCCtx into another one @dstCCtx. -* Only works during stage 0 (i.e. before first call to ZSTD_compressContinue()) +/*! ZSTD_copyCCtx() : +* Duplicate an existing context `srcCCtx` into another one `dstCCtx`. +* Only works during stage 0 (i.e. before first call to ZSTD_compressContinue()). * @return : 0, or an error code */ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx) { @@ -2363,7 +2365,7 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 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, 16, 9, 3,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 */ @@ -2384,22 +2386,22 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 0, 14, 14, 14, 0, 6, 4, 4, ZSTD_lazy2 }, /* level 7.*/ { 0, 14, 14, 14, 0, 7, 4, 4, ZSTD_lazy2 }, /* level 8.*/ { 0, 14, 15, 14, 0, 6, 4, 4, ZSTD_btlazy2 }, /* level 9.*/ - { 0, 14, 15, 14, 0, 3, 3, 6, ZSTD_btopt }, /* level 10.*/ - { 0, 14, 15, 14, 0, 6, 3, 8, ZSTD_btopt }, /* level 11.*/ - { 0, 14, 15, 14, 0, 6, 3, 16, ZSTD_btopt }, /* level 12.*/ - { 0, 14, 15, 14, 0, 6, 3, 24, ZSTD_btopt }, /* level 13.*/ - { 0, 14, 15, 15, 0, 6, 3, 48, ZSTD_btopt }, /* level 14.*/ - { 0, 14, 15, 15, 0, 6, 3, 64, ZSTD_btopt }, /* level 15.*/ - { 0, 14, 15, 15, 0, 6, 3, 96, ZSTD_btopt }, /* level 16.*/ - { 0, 14, 15, 15, 0, 6, 3,128, ZSTD_btopt }, /* level 17.*/ - { 0, 14, 15, 15, 0, 6, 3,256, ZSTD_btopt }, /* level 18.*/ - { 0, 14, 15, 15, 0, 7, 3,256, ZSTD_btopt }, /* level 19.*/ - { 0, 14, 15, 15, 0, 8, 3,256, ZSTD_btopt }, /* level 20.*/ - { 0, 14, 15, 15, 0, 9, 3,256, ZSTD_btopt }, /* level 21.*/ - { 0, 14, 15, 15, 0, 10, 3,256, ZSTD_btopt }, /* level 22.*/ - { 0, 14, 15, 15, 0, 11, 3,256, ZSTD_btopt }, /* level 23.*/ - { 0, 14, 15, 15, 0, 12, 3,256, ZSTD_btopt }, /* level 24.*/ - { 0, 14, 15, 15, 0, 13, 3,256, ZSTD_btopt }, /* level 25.*/ + { 0, 14, 15, 14, 16, 3, 3, 6, ZSTD_btopt }, /* level 10.*/ + { 0, 14, 15, 14, 16, 6, 3, 8, ZSTD_btopt }, /* level 11.*/ + { 0, 14, 15, 14, 16, 6, 3, 16, ZSTD_btopt }, /* level 12.*/ + { 0, 14, 15, 14, 16, 6, 3, 24, ZSTD_btopt }, /* level 13.*/ + { 0, 14, 15, 15, 16, 6, 3, 48, ZSTD_btopt }, /* level 14.*/ + { 0, 14, 15, 15, 16, 6, 3, 64, ZSTD_btopt }, /* level 15.*/ + { 0, 14, 15, 15, 16, 6, 3, 96, ZSTD_btopt }, /* level 16.*/ + { 0, 14, 15, 15, 16, 6, 3,128, ZSTD_btopt }, /* level 17.*/ + { 0, 14, 15, 15, 16, 6, 3,256, ZSTD_btopt }, /* level 18.*/ + { 0, 14, 15, 15, 16, 7, 3,256, ZSTD_btopt }, /* level 19.*/ + { 0, 14, 15, 15, 16, 8, 3,256, ZSTD_btopt }, /* level 20.*/ + { 0, 14, 15, 15, 16, 9, 3,256, ZSTD_btopt }, /* level 21.*/ + { 0, 14, 15, 15, 16, 10, 3,256, ZSTD_btopt }, /* level 22.*/ + { 0, 14, 15, 15, 16, 11, 3,256, ZSTD_btopt }, /* level 23.*/ + { 0, 14, 15, 15, 16, 12, 3,256, ZSTD_btopt }, /* level 24.*/ + { 0, 14, 15, 15, 16, 13, 3,256, ZSTD_btopt }, /* level 25.*/ }, }; diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index c4338bcc2..844aa976e 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -822,7 +822,8 @@ static size_t ZSTD_decompressSequences( nbSeq--; ZSTD_decodeSequence(&sequence, &seqState, mls); oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd); - if (ZSTD_isError(oneSeqSize)) return oneSeqSize; + if (ZSTD_isError(oneSeqSize)) + return oneSeqSize; op += oneSeqSize; } diff --git a/programs/fuzzer.c b/programs/fuzzer.c index 50d496b96..6d57080d3 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -344,6 +344,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(4, "OK \n"); /* nbSeq limit test */ + if (0) { #define _3BYTESTESTLENGTH 131000 #define NB3BYTESSEQLOG 9 From dd54bbc184c7b260a7ed419fbbc248e03afd9942 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 8 Mar 2016 02:35:34 +0100 Subject: [PATCH 040/219] Fixed large NbSeq > 32 K Added a test in Fuzzer to check NbSeq > 32 K --- lib/zstd_compress.c | 19 +++++++++++-------- programs/fuzzer.c | 3 +-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index a5127b748..50979ce58 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -165,8 +165,8 @@ void ZSTD_validateParams(ZSTD_parameters* params) U32 srcLog = ZSTD_highbit((U32)(params->srcSize)-1) + 1; if (params->windowLog > srcLog) params->windowLog = srcLog; } - if (params->windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN) params->windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */ - if (params->contentLog > params->windowLog+btPlus) params->contentLog = params->windowLog+btPlus; /* <= ZSTD_CONTENTLOG_MAX */ + if (params->windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN) params->windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */ + if (params->contentLog > params->windowLog+btPlus) params->contentLog = params->windowLog+btPlus; /* <= ZSTD_CONTENTLOG_MAX */ } @@ -176,9 +176,12 @@ 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 U32 divider = (params.searchLength==3) ? 3 : 4; + const size_t maxNbSeq = blockSize / divider; + const size_t tokenSpace = blockSize + 8*maxNbSeq; const size_t tableSpace = ((1 << contentLog) + (1 << params.hashLog) + (1 << params.hashLog3)) * sizeof(U32); const size_t optSpace = ((1<workSpaceSize < neededSpace) { @@ -206,13 +209,13 @@ static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, zc->blockSize = blockSize; zc->seqStore.offsetStart = (U32*) (zc->seqStore.buffer); - zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart + (blockSize>>2)); - zc->seqStore.litStart = zc->seqStore.offCodeStart + (blockSize>>2); + zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart + maxNbSeq); + zc->seqStore.litStart = zc->seqStore.offCodeStart + maxNbSeq; zc->seqStore.litLengthStart = zc->seqStore.litStart + blockSize; - zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (blockSize>>2); - zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); + zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + maxNbSeq; + zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + maxNbSeq; if (params.strategy == ZSTD_btopt) { - zc->seqStore.litFreq = (U32*)((void*)(zc->seqStore.dumpsStart + (blockSize>>2))); + zc->seqStore.litFreq = (U32*)((void*)(zc->seqStore.dumpsStart + maxNbSeq)); zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1< Date: Tue, 8 Mar 2016 11:03:55 +0100 Subject: [PATCH 041/219] fixed update of hashTable for matches with length of 4+ --- lib/zstd_opt.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 68a3c315a..961964bd2 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -227,7 +227,6 @@ static U32 ZSTD_insertBtAndGetAllMatches ( const U32 minMatch = (mls == 3) ? 3 : 4; size_t bestLength = minMatch-1; - hashTable[h] = current; /* Update Hash Table */ if (minMatch == 3) { /* HC3 match finder */ U32 matchIndex3 = ZSTD_insertAndFindFirstIndexHash3 (zc, ip); @@ -256,6 +255,8 @@ static U32 ZSTD_insertBtAndGetAllMatches ( } } + hashTable[h] = current; /* Update Hash Table */ + while (nbCompares-- && (matchIndex > windowLow)) { U32* nextPtr = bt + 2*(matchIndex & btMask); size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ From 2e91dde43e3ef09cde7ee967316a3af427fcfe8d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 8 Mar 2016 12:22:11 +0100 Subject: [PATCH 042/219] improved memory size evaluation by paramgrill --- lib/zstd_compress.c | 19 +++++++++++++++++-- programs/.gitignore | 4 +++- programs/paramgrill.c | 7 +++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 50979ce58..b668a934d 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -129,7 +129,7 @@ size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx) return 0; /* reserved as a potential error code in the future */ } -seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx) +seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx) /* hidden interface */ { return ctx->seqStore; } @@ -170,11 +170,26 @@ void ZSTD_validateParams(ZSTD_parameters* params) } +size_t ZSTD_sizeofCCtx(ZSTD_parameters params) /* hidden interface, for paramagrill */ +{ /* copy / pasted from ZSTD_resetCCtx_advanced */ + const size_t blockSize = MIN(BLOCKSIZE, (size_t)1 << params.windowLog); + const U32 contentLog = (params.strategy == ZSTD_fast) ? 1 : params.contentLog; + const U32 divider = (params.searchLength==3) ? 3 : 4; + const size_t maxNbSeq = blockSize / divider; + const size_t tokenSpace = blockSize + 8*maxNbSeq; + const size_t tableSpace = ((1 << contentLog) + (1 << params.hashLog) + (1 << params.hashLog3)) * sizeof(U32); + const size_t optSpace = ((1<