better logging

This commit is contained in:
inikep 2016-02-02 15:41:16 +01:00
parent 59140c5480
commit e1b8e53e84
3 changed files with 112 additions and 520 deletions

2
.gitignore vendored
View File

@ -46,3 +46,5 @@ ipch/
.directory .directory
_codelite _codelite
_zstdbench _zstdbench
lib/zstd_opt_LZ5.c

View File

@ -2302,8 +2302,8 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = {
{ 0, 25, 24, 23, 5, 5, ZSTD_btlazy2 }, /* level 18 */ { 0, 25, 24, 23, 5, 5, ZSTD_btlazy2 }, /* level 18 */
{ 0, 25, 26, 23, 5, 5, ZSTD_btlazy2 }, /* level 19 */ { 0, 25, 26, 23, 5, 5, ZSTD_btlazy2 }, /* level 19 */
{ 0, 26, 27, 25, 9, 5, ZSTD_btlazy2 }, /* level 20 */ { 0, 26, 27, 25, 9, 5, ZSTD_btlazy2 }, /* level 20 */
{ 0, 22, 20, 22, 4, 4, ZSTD_lazy2 }, /* level 11 + L=4 */ // 41902762 42087013 { 0, 22, 20, 22, 4, 4, ZSTD_lazy2 }, /* level 11 + L=4 */ // 41902762 lazy1=42087013 norep1=42911693
{ 0, 23, 21, 22, 5, 4, ZSTD_btlazy2 }, /* level 16 + L=4 */ // 41233150 41560211 { 0, 23, 21, 22, 5, 4, ZSTD_btlazy2 }, /* level 16 + L=4 */ // 41233150 btlazy1=41560211 norep1=42322286
{ 0, 23, 21, 22, 5, 4, ZSTD_opt }, /* level 23 */ { 0, 23, 21, 22, 5, 4, ZSTD_opt }, /* level 23 */
{ 0, 23, 21, 22, 5, 4, ZSTD_opt_bt }, /* level 23 */ { 0, 23, 21, 22, 5, 4, ZSTD_opt_bt }, /* level 23 */
}, },

View File

@ -16,41 +16,49 @@ typedef struct
int rep; int rep;
} LZ5HC_optimal_t; } LZ5HC_optimal_t;
#if 1
#define LZ5HC_DEBUG(fmt, args...) ;//printf(fmt, ##args) #define LZ5_LOG_PARSER(fmt, args...) ;// printf(fmt, ##args)
#define LZ5_LOG_PARSER(fmt, args...) ;// printf(fmt, ##args) #define LZ5_LOG_PRICE(fmt, args...) ;//printf(fmt, ##args)
#define LZ5_LOG_PRICE(fmt, args...) ;//printf(fmt, ##args) #define LZ5_LOG_ENCODE(fmt, args...) ;//printf(fmt, ##args)
#define LZ5_LOG_ENCODE(fmt, args...) ;//printf(fmt, ##args) #else
#define LZ5_LOG_PARSER(fmt, args...) printf(fmt, ##args)
#define LZ5_LOG_PRICE(fmt, args...) printf(fmt, ##args)
#define LZ5_LOG_ENCODE(fmt, args...) printf(fmt, ##args)
#endif
#define LZ5_OPT_NUM (1<<12) #define LZ5_OPT_NUM (1<<12)
#define LZ5_SHORT_OFFSET_BITS 10
#define LZ5_SHORT_OFFSET_DISTANCE (1<<LZ5_SHORT_OFFSET_BITS)
#define LZ5_MID_OFFSET_BITS 16
#define LZ5_MID_OFFSET_DISTANCE (1<<LZ5_MID_OFFSET_BITS)
#define RUN_BITS 3 /*
#define RUN_BITS2 2 I assume that you are using 4 entropy-coder tables:
litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream));
matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
HUF_decompress(dst, litSize, ip+5, litCSize)
#define LZ5_SHORT_LITERALS ((1<<RUN_BITS2)-1) With the following max values:
#define LZ5_LITERALS ((1<<RUN_BITS)-1) #define MLbits 7
#define LLbits 6
#define Offbits 5
#define MaxML ((1<<MLbits) - 1)
#define MaxLL ((1<<LLbits) - 1)
#define MaxOff ((1<<Offbits)- 1)
When value >= maxValue then use additional 1 byte (if value <
maxValue+255) or 3 bytes (to encode value up to MaxValue+255+65535).
#define LZ5_SHORT_LITLEN_COST(len) (len<LZ5_SHORT_LITERALS ? 0 : (len-LZ5_SHORT_LITERALS < 255 ? 1 : (len-LZ5_SHORT_LITERALS-255 < (1<<7) ? 2 : 3))) Am I right? For offsets only the numbers of bits == log2(offset) are
#define LZ5_LEN_COST(len) (len<LZ5_LITERALS ? 0 : (len-LZ5_LITERALS < 255 ? 1 : (len-LZ5_LITERALS-255 < (1<<7) ? 2 : 3))) entropy encoded and the offset is put into a binary stream.
This is important to make an estimation.
static size_t LZ5_LIT_COST(size_t len, size_t offset){ return (len)+(((offset > LZ5_MID_OFFSET_DISTANCE) || (offset<LZ5_SHORT_OFFSET_DISTANCE)) ? LZ5_SHORT_LITLEN_COST(len) : LZ5_LEN_COST(len)); } */
static size_t LZ5_MATCH_COST(size_t mlen, size_t offset) { return LZ5_LEN_COST(mlen) + ((offset == 0) ? 1 : (offset<LZ5_SHORT_OFFSET_DISTANCE ? 2 : (offset<LZ5_MID_OFFSET_DISTANCE ? 3 : 4))); }
#define LZ5_CODEWORD_COST(litlen,offset,mlen) (LZ5_MATCH_COST(mlen,offset) + LZ5_LIT_COST(litlen,offset))
#define LZ5_LIT_ONLY_COST(len) ((len)+(LZ5_LEN_COST(len))+1)
#define LZ5_NORMAL_MATCH_COST(mlen,offset) (LZ5_MATCH_COST(mlen,offset))
#define LZ5_NORMAL_LIT_COST(len) (len)
#define LZ5_LIT_ONLY_COST(len) (((len)<<3)+8+LLbits)
FORCE_INLINE U32 LZ5HC_get_price(U32 litlen, U32 offset, U32 mlen) FORCE_INLINE U32 LZ5HC_get_price(U32 litlen, U32 offset, U32 mlen)
{ {
return LZ5_CODEWORD_COST(litlen, offset, mlen); size_t lit_cost = (litlen<<3)+LLbits;
// size_t match_cost = (mlen<32 ? 0 : (mlen < 255 ? 8 : (mlen-255 < (1<<7) ? 16 : 24))) + ((offset == 0) ? 8 : (offset<(1<<10) ? 16 : (offset<(1<<16) ? 24 : 32)));
size_t match_cost = MLbits + ZSTD_highbit((U32)mlen+1) + Offbits + ZSTD_highbit((U32)offset+1);
return lit_cost + match_cost;
} }
@ -61,7 +69,8 @@ FORCE_INLINE U32 LZ5HC_get_price(U32 litlen, U32 offset, U32 mlen)
opt[pos].off = offset; \ opt[pos].off = offset; \
opt[pos].litlen = litlen; \ opt[pos].litlen = litlen; \
opt[pos].price = price; \ opt[pos].price = price; \
LZ5_LOG_PARSER("%d: SET price[%d/%d]=%d litlen=%d len=%d off=%d\n", (int)(inr-istart), (int)pos, (int)last_pos, opt[pos].price, opt[pos].litlen, opt[pos].mlen, opt[pos].off); \ LZ5_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); \
if (mlen > 1 && mlen < MINMATCH) { printf("%d: ERROR 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); exit(0); }; \
} }
@ -121,13 +130,16 @@ size_t ZSTD_insertBtAndGetAllMatches (
if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit(current-matchIndex+1) - ZSTD_highbit((U32)offsetPtr[0]+1)) ) if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit(current-matchIndex+1) - ZSTD_highbit((U32)offsetPtr[0]+1)) )
bestLength = matchLength, *offsetPtr = current - matchIndex; bestLength = matchLength, *offsetPtr = current - matchIndex;
#else #else
if (mnum == 0 || (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit(current-matchIndex+1) - ZSTD_highbit((U32)matches[mnum-1].off+1)) ) if (mnum == 0 || (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit(current-matchIndex+1) - ZSTD_highbit((U32)matches[mnum-1].off+1)) )
{ {
bestLength = matchLength; if (matchLength >= MINMATCH)
matches[mnum].off = current - matchIndex; {
matches[mnum].len = matchLength; bestLength = matchLength;
matches[mnum].back = 0; matches[mnum].off = current - matchIndex;
mnum++; matches[mnum].len = matchLength;
matches[mnum].back = 0;
mnum++;
}
if (matchLength > LZ5_OPT_NUM) break; if (matchLength > LZ5_OPT_NUM) break;
} }
#endif #endif
@ -303,12 +315,15 @@ void ZSTD_compressBlock_opt2_generic(ZSTD_CCtx* ctx,
typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit,
size_t* offsetPtr, size_t* offsetPtr,
U32 maxNbAttempts, U32 matchLengthSearch); U32 maxNbAttempts, U32 matchLengthSearch);
searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS;
#if 0
typedef size_t (*getAllMatches_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, typedef size_t (*getAllMatches_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit,
U32 maxNbAttempts, U32 matchLengthSearch, LZ5HC_match_t* matches); U32 maxNbAttempts, U32 matchLengthSearch, LZ5HC_match_t* matches);
searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS;
getAllMatches_f getAllMatches = searchMethod ? ZSTD_BtGetAllMatches_selectMLS : ZSTD_HcGetAllMatches_selectMLS; getAllMatches_f getAllMatches = searchMethod ? ZSTD_BtGetAllMatches_selectMLS : ZSTD_HcGetAllMatches_selectMLS;
LZ5HC_match_t matches[LZ5_OPT_NUM+1]; LZ5HC_match_t matches[LZ5_OPT_NUM+1];
#endif
/* init */ /* init */
ZSTD_resetSeqStore(seqStorePtr); ZSTD_resetSeqStore(seqStorePtr);
@ -320,6 +335,7 @@ void ZSTD_compressBlock_opt2_generic(ZSTD_CCtx* ctx,
size_t offset=0; size_t offset=0;
const BYTE* start=ip+1; const BYTE* start=ip+1;
//#define ZSTD_USE_REP
#ifdef ZSTD_USE_REP #ifdef ZSTD_USE_REP
/* check repCode */ /* check repCode */
if (MEM_read32(start) == MEM_read32(start - offset_1)) { if (MEM_read32(start) == MEM_read32(start - offset_1)) {
@ -390,7 +406,7 @@ void ZSTD_compressBlock_opt2_generic(ZSTD_CCtx* ctx,
_storeSequence: _storeSequence:
{ {
size_t litLength = start - anchor; size_t litLength = start - anchor;
LZ5_LOG_ENCODE("%d/%d: ENCODE literals=%d off=%d mlen=%d\n", (int)(ip-istart), (int)(iend-istart), (int)(litLength), (int)(offset), (int)matchLength); LZ5_LOG_ENCODE("%d/%d: ENCODE literals=%d off=%d mlen=%d\n", (int)(ip-base), (int)(iend-base), (int)(litLength), (int)(offset), (int)matchLength);
ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH); ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH);
anchor = ip = start + matchLength; anchor = ip = start + matchLength;
} }
@ -414,7 +430,7 @@ _storeSequence:
/* Last Literals */ /* Last Literals */
{ {
size_t lastLLSize = iend - anchor; size_t lastLLSize = iend - anchor;
LZ5_LOG_ENCODE("%d/%d: ENCODE lastLLSize=%d\n", (int)(ip-istart), (int)(iend-istart), (int)(lastLLSize)); LZ5_LOG_ENCODE("%d/%d: ENCODE lastLLSize=%d\n", (int)(ip-base), (int)(iend-base), (int)(lastLLSize));
memcpy(seqStorePtr->lit, anchor, lastLLSize); memcpy(seqStorePtr->lit, anchor, lastLLSize);
seqStorePtr->lit += lastLLSize; seqStorePtr->lit += lastLLSize;
} }
@ -454,7 +470,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
int llen, litlen, price, match_num, last_pos; int llen, litlen, price, match_num, last_pos;
const int sufficient_len = 128; //ctx->params.sufficientLength; const int sufficient_len = 128; //ctx->params.sufficientLength;
const int faster_get_matches = 0;//(ctx->params.strategy == ZSTD_opt); const int faster_get_matches = (ctx->params.strategy == ZSTD_opt);
// printf("orig_file="); print_hex_text(ip, srcSize, 0); // printf("orig_file="); print_hex_text(ip, srcSize, 0);
@ -474,13 +490,13 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
llen = ip - anchor; llen = ip - anchor;
inr = ip; inr = ip;
#if 0 #ifdef ZSTD_USE_REP
/* check repCode */ /* check repCode */
if (MEM_read32(ip+1) == MEM_read32(ip+1 - rep_1)) { if (MEM_read32(ip+1) == MEM_read32(ip+1 - rep_1)) {
/* repcode : we take it */ /* repcode : we take it */
mlen = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-rep_1, iend) + MINMATCH; mlen = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-rep_1, iend) + MINMATCH;
LZ5_LOG_PARSER("%d: start try REP rep=%d mlen=%d\n", (int)(ip-istart), (int)rep_1, (int)mlen); LZ5_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 >= LZ5_OPT_NUM) { if (depth==0 || mlen > sufficient_len || mlen >= LZ5_OPT_NUM) {
ip++; best_mlen = mlen; best_off = 0; cur = 0; last_pos = 1; ip++; best_mlen = mlen; best_off = 0; cur = 0; last_pos = 1;
goto _storeSequence; goto _storeSequence;
@ -498,7 +514,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
} }
#endif #endif
// best_mlen = (last_pos) ? last_pos : MINMATCH; best_mlen = (last_pos) ? last_pos : MINMATCH;
if (faster_get_matches && last_pos) if (faster_get_matches && last_pos)
match_num = 0; match_num = 0;
@ -508,7 +524,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
match_num = getAllMatches(ctx, ip, iend, maxSearches, mls, matches); match_num = getAllMatches(ctx, ip, iend, maxSearches, mls, matches);
} }
LZ5_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-istart), match_num, last_pos); LZ5_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-base), match_num, last_pos);
if (!last_pos && !match_num) { ip++; continue; } if (!last_pos && !match_num) { ip++; continue; }
if (match_num && matches[match_num-1].len > sufficient_len) if (match_num && matches[match_num-1].len > sufficient_len)
@ -525,7 +541,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
{ {
mlen = (i>0) ? matches[i-1].len+1 : best_mlen; mlen = (i>0) ? matches[i-1].len+1 : best_mlen;
best_mlen = (matches[i].len < LZ5_OPT_NUM) ? matches[i].len : LZ5_OPT_NUM; best_mlen = (matches[i].len < LZ5_OPT_NUM) ? matches[i].len : LZ5_OPT_NUM;
LZ5_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-istart), matches[i].len, matches[i].off, (int)best_mlen, (int)last_pos); LZ5_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-base), matches[i].len, matches[i].off, (int)best_mlen, (int)last_pos);
while (mlen <= best_mlen) while (mlen <= best_mlen)
{ {
litlen = 0; litlen = 0;
@ -541,14 +557,14 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
ip++; continue; ip++; continue;
} }
// printf("%d: last_pos=%d\n", (int)(ip - istart), (int)last_pos); // printf("%d: last_pos=%d\n", (int)(ip - base), (int)last_pos);
// opt[0].rep = opt[1].rep = rep_1; // opt[0].rep = opt[1].rep = rep_1;
// opt[0].mlen = opt[1].mlen = 1; // opt[0].mlen = opt[1].mlen = 1;
opt[0].rep = rep_1; opt[0].rep = rep_1;
opt[0].mlen = 1; opt[0].mlen = 1;
#if 1
// check further positions // check further positions
for (skip_num = 0, cur = 1; cur <= last_pos; cur++) for (skip_num = 0, cur = 1; cur <= last_pos; cur++)
{ {
@ -561,24 +577,24 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
if (cur != litlen) if (cur != litlen)
{ {
price = opt[cur - litlen].price + LZ5_LIT_ONLY_COST(litlen); price = opt[cur - litlen].price + LZ5_LIT_ONLY_COST(litlen);
LZ5_LOG_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)((char*)inr-source), cur - litlen, opt[cur - litlen].price, price, cur, litlen); LZ5_LOG_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 else
{ {
price = LZ5_LIT_ONLY_COST(llen + litlen) - llen; price = LZ5_LIT_ONLY_COST(llen + litlen) - llen;
LZ5_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)((char*)inr-source), price, cur, litlen, llen); LZ5_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)(inr-base), price, cur, litlen, llen);
} }
} }
else else
{ {
litlen = 1; litlen = 1;
price = opt[cur - 1].price + LZ5_LIT_ONLY_COST(litlen); price = opt[cur - 1].price + LZ5_LIT_ONLY_COST(litlen);
LZ5_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d litonly=%d\n", (int)((char*)inr-source), price, cur, litlen, LZ5_LIT_ONLY_COST(litlen)); LZ5_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d litonly=%d\n", (int)(inr-base), price, cur, litlen, LZ5_LIT_ONLY_COST(litlen));
} }
mlen = 1; mlen = 1;
best_mlen = 0; best_mlen = 0;
LZ5_LOG_PARSER("%d: TRY price=%d opt[%d].price=%d\n", (int)((char*)inr-source), price, cur, opt[cur].price); LZ5_LOG_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))) if (cur > last_pos || price <= opt[cur].price) // || ((price == opt[cur].price) && (opt[cur-1].mlen == 1) && (cur != litlen)))
SET_PRICE(cur, mlen, best_mlen, litlen, price); SET_PRICE(cur, mlen, best_mlen, litlen, price);
@ -591,12 +607,12 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
if (opt[cur].off < 1) if (opt[cur].off < 1)
{ {
opt[cur].rep = opt[cur-mlen].rep; opt[cur].rep = opt[cur-mlen].rep;
LZ5_LOG_PARSER("%d: COPYREP1 cur=%d mlen=%d rep=%d\n", (int)((char*)inr-source), cur, mlen, opt[cur-mlen].rep); LZ5_LOG_PARSER("%d: COPYREP1 cur=%d mlen=%d rep=%d\n", (int)(inr-base), cur, mlen, opt[cur-mlen].rep);
} }
else else
{ {
opt[cur].rep = 0; opt[cur].rep = 0;
LZ5_LOG_PARSER("%d: COPYREP2 cur=%d offset=%d rep=%d\n", (int)((char*)inr-source), cur, 0, opt[cur].rep); LZ5_LOG_PARSER("%d: COPYREP2 cur=%d offset=%d rep=%d\n", (int)(inr-base), cur, 0, opt[cur].rep);
} }
} }
else else
@ -605,22 +621,22 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
} }
LZ5_LOG_PARSER("%d: CURRENT price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)inr-source), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep); LZ5_LOG_PARSER("%d: CURRENT price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(inr-base), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep);
#if 0 #ifdef ZSTD_USE_REP
// check rep // check rep
// best_mlen = 0; // best_mlen = 0;
mlen = ZSTD_count(inr, inr - opt[cur].rep, iend); mlen = ZSTD_count(inr, inr - opt[cur].rep, iend);
if (mlen >= MINMATCH && mlen > best_mlen) if (mlen >= MINMATCH && mlen > best_mlen)
{ {
LZ5_LOG_PARSER("%d: try REP rep=%d mlen=%d\n", (int)((char*)inr-source), opt[cur].rep, mlen); LZ5_LOG_PARSER("%d: try REP rep=%d mlen=%d\n", (int)(inr-base), opt[cur].rep, mlen);
LZ5_LOG_PARSER("%d: Found REP mlen=%d off=%d rep=%d opt[%d].off=%d\n", (int)((char*)inr-source), mlen, 0, opt[cur].rep, cur, opt[cur].off); LZ5_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 >= LZ5_OPT_NUM) if (mlen > sufficient_len || cur + mlen >= LZ5_OPT_NUM)
{ {
best_mlen = mlen; best_mlen = mlen;
best_off = 0; best_off = 0;
LZ5_LOG_PARSER("%d: REP sufficient_len=%d best_mlen=%d best_off=%d last_pos=%d\n", (int)((char*)inr-source), sufficient_len, best_mlen, best_off, last_pos); LZ5_LOG_PARSER("%d: REP sufficient_len=%d best_mlen=%d best_off=%d last_pos=%d\n", (int)(inr-base), sufficient_len, best_mlen, best_off, last_pos);
last_pos = cur + 1; last_pos = cur + 1;
goto _storeSequence; goto _storeSequence;
} }
@ -632,26 +648,26 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
if (cur != litlen) if (cur != litlen)
{ {
price = opt[cur - litlen].price + LZ5HC_get_price(litlen, 0, mlen - MINMATCH); price = opt[cur - litlen].price + LZ5HC_get_price(litlen, 0, mlen - MINMATCH);
LZ5_LOG_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)((char*)inr-source), cur - litlen, opt[cur - litlen].price, price, cur, litlen); LZ5_LOG_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 else
{ {
price = LZ5HC_get_price(llen + litlen, 0, mlen - MINMATCH) - llen; price = LZ5HC_get_price(llen + litlen, 0, mlen - MINMATCH) - llen;
LZ5_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)((char*)inr-source), price, cur, litlen, llen); LZ5_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)(inr-base), price, cur, litlen, llen);
} }
} }
else else
{ {
litlen = 0; litlen = 0;
price = opt[cur].price + LZ5HC_get_price(litlen, 0, mlen - MINMATCH); price = opt[cur].price + LZ5HC_get_price(litlen, 0, mlen - MINMATCH);
LZ5_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d getprice=%d\n", (int)((char*)inr-source), price, cur, litlen, LZ5HC_get_price(litlen, 0, mlen - MINMATCH)); LZ5_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d getprice=%d\n", (int)(inr-base), price, cur, litlen, LZ5HC_get_price(litlen, 0, mlen - MINMATCH));
} }
best_mlen = mlen; best_mlen = mlen;
if (faster_get_matches) if (faster_get_matches)
skip_num = best_mlen; skip_num = best_mlen;
LZ5_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)((char*)inr-source), mlen, 0, price, litlen, cur - litlen, opt[cur - litlen].price); LZ5_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);
do do
{ {
@ -674,7 +690,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
match_num = getAllMatches(ctx, inr, iend, maxSearches, mls, matches); match_num = getAllMatches(ctx, inr, iend, maxSearches, mls, matches);
// match_num = LZ5HC_GetAllMatches(ctx, inr, ip, matchlimit, best_mlen, matches); // match_num = LZ5HC_GetAllMatches(ctx, inr, ip, matchlimit, best_mlen, matches);
LZ5_LOG_PARSER("%d: LZ5HC_GetAllMatches match_num=%d\n", (int)((char*)inr-source), match_num); LZ5_LOG_PARSER("%d: LZ5HC_GetAllMatches match_num=%d\n", (int)(inr-base), match_num);
if (match_num > 0 && matches[match_num-1].len > sufficient_len) if (match_num > 0 && matches[match_num-1].len > sufficient_len)
@ -694,7 +710,16 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
mlen = (i>0) ? matches[i-1].len+1 : best_mlen; mlen = (i>0) ? matches[i-1].len+1 : best_mlen;
cur2 = cur - matches[i].back; cur2 = cur - matches[i].back;
best_mlen = (cur2 + matches[i].len < LZ5_OPT_NUM) ? matches[i].len : LZ5_OPT_NUM - cur2; best_mlen = (cur2 + matches[i].len < LZ5_OPT_NUM) ? matches[i].len : LZ5_OPT_NUM - cur2;
LZ5_LOG_PARSER("%d: Found1 cur=%d cur2=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)((char*)inr-source), cur, cur2, matches[i].len, matches[i].off, best_mlen, last_pos);
#if 0
if (mlen < MINMATCH)
{
printf("i=%d match_num=%d matches[i-1].len=%d\n", i, match_num, matches[i-1].len);
printf("%d: ERROR mlen=%d Found1 cur=%d cur2=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-base), mlen, cur, cur2, matches[i].len, matches[i].off, best_mlen, last_pos), exit(0);
}
#endif
LZ5_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[i].len, matches[i].off, best_mlen, last_pos);
while (mlen <= best_mlen) while (mlen <= best_mlen)
{ {
@ -713,7 +738,8 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
price = opt[cur2].price + LZ5HC_get_price(litlen, matches[i].off, mlen - MINMATCH); price = opt[cur2].price + LZ5HC_get_price(litlen, matches[i].off, mlen - MINMATCH);
} }
LZ5_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)((char*)inr-source), matches[i].back, mlen, best_mlen, matches[i].off, price, litlen, cur - litlen, opt[cur - litlen].price); LZ5_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[i].back, mlen, best_mlen, matches[i].off, price, litlen, cur - litlen, opt[cur - litlen].price);
// LZ5_LOG_PRICE("%d: TRY5 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) if (cur2 + mlen > last_pos || price < opt[cur2 + mlen].price)
{ {
SET_PRICE(cur2 + mlen, mlen, matches[i].off, litlen, price); SET_PRICE(cur2 + mlen, mlen, matches[i].off, litlen, price);
@ -730,7 +756,7 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
{ {
for (int i=cur_min-1; i<=last_pos; i++) for (int i=cur_min-1; i<=last_pos; i++)
{ {
LZ5_LOG_PARSER("%d: BEFORE price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep); LZ5_LOG_PARSER("%d: BEFORE price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-base+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
} }
for (int i=cur_min+1; i<=last_pos; i++) for (int i=cur_min+1; i<=last_pos; i++)
@ -760,32 +786,33 @@ void ZSTD_compressBlock_opt_generic(ZSTD_CCtx* ctx,
mlen = 1; mlen = 1;
best_mlen = 0; best_mlen = 0;
LZ5_LOG_PRICE("%d: TRY6 price=%d opt[%d].price=%d\n", (int)(inr-base), price, i + mlen, opt[i + mlen].price);
SET_PRICE(i, mlen, best_mlen, litlen, price); SET_PRICE(i, mlen, best_mlen, litlen, price);
opt[i].rep = opt[i-1].rep; // copy reps opt[i].rep = opt[i-1].rep; // copy reps
LZ5_LOG_PARSER("%d: INVALIDATE pred=%d price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)inr-source), cur-cur_min, i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep); LZ5_LOG_PARSER("%d: INVALIDATE pred=%d price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(inr-base), cur-cur_min, i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
} }
for (int i=cur_min-1; i<=last_pos; i++) for (int i=cur_min-1; i<=last_pos; i++)
{ {
LZ5_LOG_PARSER("%d: AFTER price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep); LZ5_LOG_PARSER("%d: AFTER price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-base+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
} }
} }
} // for (skip_num = 0, cur = 1; cur <= last_pos; cur++) } // for (skip_num = 0, cur = 1; cur <= last_pos; cur++)
#endif
best_mlen = opt[last_pos].mlen; best_mlen = opt[last_pos].mlen;
best_off = opt[last_pos].off; best_off = opt[last_pos].off;
cur = last_pos - best_mlen; cur = last_pos - best_mlen;
// printf("%d: start=%d best_mlen=%d best_off=%d cur=%d\n", (int)(ip - istart), (int)(start - ip), (int)best_mlen, (int)best_off, cur); // 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 */ /* store sequence */
_storeSequence: // cur, last_pos, best_mlen, best_off have to be set _storeSequence: // cur, last_pos, best_mlen, best_off have to be set
for (int i = 1; i <= last_pos; i++) for (int i = 1; i <= last_pos; i++)
LZ5_LOG_PARSER("%d: price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-istart+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep); LZ5_LOG_PARSER("%d: price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-base+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
LZ5_LOG_PARSER("%d: cur=%d/%d best_mlen=%d best_off=%d rep=%d\n", (int)(ip-istart+cur), (int)cur, (int)last_pos, (int)best_mlen, (int)best_off, opt[cur].rep); LZ5_LOG_PARSER("%d: cur=%d/%d best_mlen=%d best_off=%d rep=%d\n", (int)(ip-base+cur), (int)cur, (int)last_pos, (int)best_mlen, (int)best_off, opt[cur].rep);
opt[0].mlen = 1; opt[0].mlen = 1;
size_t offset; size_t offset;
@ -803,7 +830,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set
for (int i = 0; i <= last_pos;) for (int i = 0; i <= last_pos;)
{ {
LZ5_LOG_PARSER("%d: price2[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-istart+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep); LZ5_LOG_PARSER("%d: price2[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-base+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
i += opt[i].mlen; i += opt[i].mlen;
} }
@ -811,7 +838,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set
while (cur < last_pos) while (cur < last_pos)
{ {
LZ5_LOG_PARSER("%d: price3[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-istart+cur), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep); LZ5_LOG_PARSER("%d: price3[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)(ip-base+cur), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep);
mlen = opt[cur].mlen; mlen = opt[cur].mlen;
if (mlen == 1) { ip++; cur++; continue; } if (mlen == 1) { ip++; cur++; continue; }
offset = opt[cur].off; offset = opt[cur].off;
@ -821,40 +848,39 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set
if (offset) if (offset)
{ {
size_t ml2 = ZSTD_count(ip, ip-offset, iend); size_t ml2 = ZSTD_count(ip, ip-offset, iend);
// printf("%d: mlen=%d offset=%d\n", (int)(ip - istart), (int)mlen, (int)offset);
if (ml2 < mlen && ml2 < MINMATCH) if (ml2 < mlen && ml2 < MINMATCH)
{ {
printf("ERROR %d: iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - istart), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2); printf("ERROR %d: iend=%d mlen=%d offset=%d ml2=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset, (int)ml2);
exit(0); exit(0);
} }
} }
else else
printf("ERROR %d: mlen=%d offset=%d\n", (int)(ip - istart), (int)mlen, (int)offset); printf("ERROR %d: mlen=%d offset=%d\n", (int)(ip - base), (int)mlen, (int)offset);
if (ip < anchor) if (ip < anchor)
{ {
printf("ERROR %d: ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - istart), (int)(iend - ip), (int)mlen, (int)offset); printf("ERROR %d: ip < anchor iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset);
exit(0); exit(0);
} }
if (ip - offset < base) if (ip - offset < base)
{ {
printf("ERROR %d: ip - offset < istart iend=%d mlen=%d offset=%d\n", (int)(ip - istart), (int)(iend - ip), (int)mlen, (int)offset); printf("ERROR %d: ip - offset < base iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset);
exit(0); exit(0);
} }
if (mlen < MINMATCH) if (mlen < MINMATCH)
{ {
printf("ERROR %d: mlen < MINMATCH iend=%d mlen=%d offset=%d\n", (int)(ip - istart), (int)(iend - ip), (int)mlen, (int)offset); printf("ERROR %d: mlen < MINMATCH iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset);
exit(0); exit(0);
} }
if (ip + mlen > iend) if (ip + mlen > iend)
{ {
printf("ERROR %d: ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - istart), (int)(iend - ip), (int)mlen, (int)offset); printf("ERROR %d: ip + mlen >= iend iend=%d mlen=%d offset=%d\n", (int)(ip - base), (int)(iend - ip), (int)mlen, (int)offset);
exit(0); exit(0);
} }
#endif #endif
size_t litLength = ip - anchor; size_t litLength = ip - anchor;
LZ5_LOG_ENCODE("%d/%d: ENCODE literals=%d off=%d mlen=%d\n", (int)(ip-istart), (int)(iend-istart), (int)(litLength), (int)(offset), (int)mlen); LZ5_LOG_ENCODE("%d/%d: ENCODE literals=%d off=%d mlen=%d\n", (int)(ip-base), (int)(iend-base), (int)(litLength), (int)(offset), (int)mlen);
// printf("orig="); print_hex_text(ip, mlen, 0); // printf("orig="); print_hex_text(ip, mlen, 0);
// printf("match="); print_hex_text(ip-offset, mlen, 0); // printf("match="); print_hex_text(ip-offset, mlen, 0);
ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH); ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen-MINMATCH);
@ -863,11 +889,11 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set
if (offset) if (offset)
rep_2 = rep_1, rep_1 = offset; rep_2 = rep_1, rep_1 = offset;
LZ5_LOG_PARSER("%d: offset=%d rep=%d\n", (int)(ip-istart), (int)offset, (int)rep_1); LZ5_LOG_PARSER("%d: offset=%d rep=%d\n", (int)(ip-base), (int)offset, (int)rep_1);
} }
#if 0 #ifdef ZSTD_USE_REP
// check immediate repcode // check immediate repcode
while ( (ip <= ilimit) while ( (ip <= ilimit)
&& (MEM_read32(ip) == MEM_read32(ip - rep_2)) ) { && (MEM_read32(ip) == MEM_read32(ip - rep_2)) ) {
@ -888,7 +914,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set
/* Last Literals */ /* Last Literals */
{ {
size_t lastLLSize = iend - anchor; size_t lastLLSize = iend - anchor;
LZ5_LOG_ENCODE("%d: lastLLSize literals=%d\n", (int)(ip-istart), (int)(lastLLSize)); LZ5_LOG_ENCODE("%d: lastLLSize literals=%d\n", (int)(ip-base), (int)(lastLLSize));
memcpy(seqStorePtr->lit, anchor, lastLLSize); memcpy(seqStorePtr->lit, anchor, lastLLSize);
seqStorePtr->lit += lastLLSize; seqStorePtr->lit += lastLLSize;
} }
@ -896,439 +922,3 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set
#if 0
static int LZ5HC_compress_optimal_price (
LZ5HC_Data_Structure* ctx,
const char* source,
char* dest,
int inputSize,
int maxOutputSize,
limitedOutput_directive limit
)
{
ctx->inputBuffer = (const BYTE*) source;
ctx->outputBuffer = (const BYTE*) dest;
const BYTE* ip = (const BYTE*) source;
const BYTE* anchor = ip;
const BYTE* const iend = ip + inputSize;
const BYTE* const mflimit = iend - MFLIMIT;
const BYTE* const matchlimit = (iend - LASTLITERALS);
BYTE* op = (BYTE*) dest;
BYTE* const oend = op + maxOutputSize;
const int sufficient_len = ctx->params.sufficientLength;
const bool faster_get_matches = (ctx->params.fullSearch == 0);
LZ5HC_optimal_t opt[LZ5_OPT_NUM+4];
LZ5HC_match_t matches[LZ5_OPT_NUM+1];
const uint8_t *inr;
int res, cur, cur2, cur_min, skip_num = 0;
int rep, llen, litlen, mlen, best_mlen, price, offset, best_off, match_num, last_pos;
rep = 1;
/* init */
ctx->end += inputSize;
ip++;
/* Main Loop */
while (ip < mflimit)
{
memset(opt, 0, sizeof(LZ5HC_optimal_t));
last_pos = 0;
llen = ip - anchor;
inr = ip;
// check rep
mlen = MEM_count(ip, ip - rep, matchlimit);
if (mlen >= MINMATCH)
{
LZ5_LOG_PARSER("%d: start try REP rep=%d mlen=%d\n", (int)((char*)ip-source), rep, mlen);
if (mlen > sufficient_len || mlen >= LZ5_OPT_NUM)
{
best_mlen = mlen; best_off = 0; cur = 0; last_pos = 1;
goto encode;
}
do
{
litlen = 0;
price = LZ5HC_get_price(llen, 0, mlen - MINMATCH) - llen;
if (mlen > last_pos || price < opt[mlen].price)
SET_PRICE(mlen, mlen, 0, litlen, price);
mlen--;
}
while (mlen >= MINMATCH);
}
best_mlen = (last_pos) ? last_pos : MINMATCH;
if (faster_get_matches && last_pos)
match_num = 0;
else
{
if (ctx->params.strategy == LZ5HC_optimal_price)
{
LZ5HC_Insert(ctx, ip);
match_num = LZ5HC_GetAllMatches(ctx, ip, ip, matchlimit, best_mlen, matches);
}
else
{
if (ctx->params.fullSearch < 2)
LZ5HC_BinTree_Insert(ctx, ip);
else
LZ5HC_BinTree_InsertFull(ctx, ip, matchlimit);
match_num = LZ5HC_BinTree_GetAllMatches(ctx, ip, matchlimit, best_mlen, matches);
}
}
LZ5_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)((char*)ip-source), match_num, last_pos);
if (!last_pos && !match_num) { ip++; continue; }
if (match_num && matches[match_num-1].len > sufficient_len)
{
best_mlen = matches[match_num-1].len;
best_off = matches[match_num-1].off;
cur = 0;
last_pos = 1;
goto encode;
}
// set prices using matches at position = 0
for (int i = 0; i < match_num; i++)
{
mlen = (i>0) ? matches[i-1].len+1 : best_mlen;
best_mlen = (matches[i].len < LZ5_OPT_NUM) ? matches[i].len : LZ5_OPT_NUM;
LZ5_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)((char*)ip-source), matches[i].len, matches[i].off, best_mlen, last_pos);
while (mlen <= best_mlen)
{
litlen = 0;
price = LZ5HC_get_price(llen + litlen, matches[i].off, mlen - MINMATCH) - llen;
if (mlen > last_pos || price < opt[mlen].price)
SET_PRICE(mlen, mlen, matches[i].off, litlen, price);
mlen++;
}
}
if (!last_pos) { ip++; continue; }
opt[0].rep = rep;
opt[0].mlen = 1;
// check further positions
for (skip_num = 0, cur = 1; cur <= last_pos; cur++)
{
inr = ip + cur;
if (opt[cur-1].mlen == 1)
{
litlen = opt[cur-1].litlen + 1;
if (cur != litlen)
{
price = opt[cur - litlen].price + LZ5_LIT_ONLY_COST(litlen);
LZ5_LOG_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)((char*)inr-source), cur - litlen, opt[cur - litlen].price, price, cur, litlen);
}
else
{
price = LZ5_LIT_ONLY_COST(llen + litlen) - llen;
LZ5_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)((char*)inr-source), price, cur, litlen, llen);
}
}
else
{
litlen = 1;
price = opt[cur - 1].price + LZ5_LIT_ONLY_COST(litlen);
LZ5_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d litonly=%d\n", (int)((char*)inr-source), price, cur, litlen, LZ5_LIT_ONLY_COST(litlen));
}
mlen = 1;
best_mlen = 0;
LZ5_LOG_PARSER("%d: TRY price=%d opt[%d].price=%d\n", (int)((char*)inr-source), 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, mlen, best_mlen, litlen, price);
if (cur == last_pos) break;
if (opt[cur].mlen > 1)
{
mlen = opt[cur].mlen;
offset = opt[cur].off;
if (offset < 1)
{
opt[cur].rep = opt[cur-mlen].rep;
LZ5_LOG_PARSER("%d: COPYREP1 cur=%d mlen=%d rep=%d\n", (int)((char*)inr-source), cur, mlen, opt[cur-mlen].rep);
}
else
{
opt[cur].rep = offset;
LZ5_LOG_PARSER("%d: COPYREP2 cur=%d offset=%d rep=%d\n", (int)((char*)inr-source), cur, offset, opt[cur].rep);
}
}
else
{
opt[cur].rep = opt[cur-1].rep; // copy rep
}
LZ5_LOG_PARSER("%d: CURRENT price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)inr-source), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep);
// check rep
// best_mlen = 0;
mlen = MEM_count(inr, inr - opt[cur].rep, matchlimit);
if (mlen >= MINMATCH && mlen > best_mlen)
{
LZ5_LOG_PARSER("%d: try REP rep=%d mlen=%d\n", (int)((char*)inr-source), opt[cur].rep, mlen);
LZ5_LOG_PARSER("%d: Found REP mlen=%d off=%d rep=%d opt[%d].off=%d\n", (int)((char*)inr-source), mlen, 0, opt[cur].rep, cur, opt[cur].off);
if (mlen > sufficient_len || cur + mlen >= LZ5_OPT_NUM)
{
best_mlen = mlen;
best_off = 0;
LZ5_LOG_PARSER("%d: REP sufficient_len=%d best_mlen=%d best_off=%d last_pos=%d\n", (int)((char*)inr-source), sufficient_len, best_mlen, best_off, last_pos);
last_pos = cur + 1;
goto encode;
}
if (opt[cur].mlen == 1)
{
litlen = opt[cur].litlen;
if (cur != litlen)
{
price = opt[cur - litlen].price + LZ5HC_get_price(litlen, 0, mlen - MINMATCH);
LZ5_LOG_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)((char*)inr-source), cur - litlen, opt[cur - litlen].price, price, cur, litlen);
}
else
{
price = LZ5HC_get_price(llen + litlen, 0, mlen - MINMATCH) - llen;
LZ5_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)((char*)inr-source), price, cur, litlen, llen);
}
}
else
{
litlen = 0;
price = opt[cur].price + LZ5HC_get_price(litlen, 0, mlen - MINMATCH);
LZ5_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d getprice=%d\n", (int)((char*)inr-source), price, cur, litlen, LZ5HC_get_price(litlen, 0, mlen - MINMATCH));
}
best_mlen = mlen;
if (faster_get_matches)
skip_num = best_mlen;
LZ5_LOG_PARSER("%d: Found REP mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)((char*)inr-source), mlen, 0, price, litlen, cur - litlen, opt[cur - litlen].price);
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
SET_PRICE(cur + mlen, mlen, 0, litlen, price);
mlen--;
}
while (mlen >= MINMATCH);
}
if (faster_get_matches && skip_num > 0)
{
skip_num--;
continue;
}
best_mlen = (best_mlen > MINMATCH) ? best_mlen : MINMATCH;
if (ctx->params.strategy == LZ5HC_optimal_price)
{
LZ5HC_Insert(ctx, inr);
match_num = LZ5HC_GetAllMatches(ctx, inr, ip, matchlimit, best_mlen, matches);
LZ5_LOG_PARSER("%d: LZ5HC_GetAllMatches match_num=%d\n", (int)((char*)inr-source), match_num);
}
else
{
if (ctx->params.fullSearch < 2)
LZ5HC_BinTree_Insert(ctx, inr);
else
LZ5HC_BinTree_InsertFull(ctx, inr, matchlimit);
match_num = LZ5HC_BinTree_GetAllMatches(ctx, inr, matchlimit, best_mlen, matches);
LZ5_LOG_PARSER("%d: LZ5HC_BinTree_GetAllMatches match_num=%d\n", (int)((char*)inr-source), 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;
goto encode;
}
cur_min = cur;
// set prices using matches at position = cur
for (int i = 0; i < match_num; i++)
{
mlen = (i>0) ? matches[i-1].len+1 : best_mlen;
cur2 = cur - matches[i].back;
best_mlen = (cur2 + matches[i].len < LZ5_OPT_NUM) ? matches[i].len : LZ5_OPT_NUM - cur2;
LZ5_LOG_PARSER("%d: Found1 cur=%d cur2=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)((char*)inr-source), cur, cur2, matches[i].len, matches[i].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 + LZ5HC_get_price(litlen, matches[i].off, mlen - MINMATCH);
else
price = LZ5HC_get_price(llen + litlen, matches[i].off, mlen - MINMATCH) - llen;
}
else
{
litlen = 0;
price = opt[cur2].price + LZ5HC_get_price(litlen, matches[i].off, mlen - MINMATCH);
}
LZ5_LOG_PARSER("%d: Found2 pred=%d mlen=%d best_mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)((char*)inr-source), matches[i].back, mlen, best_mlen, matches[i].off, price, litlen, cur - litlen, opt[cur - litlen].price);
if (cur2 + mlen > last_pos || price < opt[cur2 + mlen].price)
{
SET_PRICE(cur2 + mlen, mlen, matches[i].off, litlen, price);
opt[cur2 + mlen].rep = matches[i].off; // update reps
if (cur2 < cur_min) cur_min = cur2;
}
mlen++;
}
}
if (cur_min < cur)
{
for (int i=cur_min-1; i<=last_pos; i++)
{
LZ5_LOG_PARSER("%d: BEFORE price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
}
for (int i=cur_min+1; i<=last_pos; i++)
if (opt[i].price < (1<<30) && (opt[i].off) < 1 && i - opt[i].mlen > cur_min) // invalidate reps
{
if (opt[i-1].mlen == 1)
{
litlen = opt[i-1].litlen + 1;
if (i != litlen)
{
price = opt[i - litlen].price + LZ5_LIT_ONLY_COST(litlen);
// LZ5_LOG_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-base), i - litlen, opt[i - litlen].price, price, i, litlen);
}
else
{
price = LZ5_LIT_ONLY_COST(llen + litlen) - llen;
// LZ5_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)(inr-base), price, i, litlen, llen);
}
}
else
{
litlen = 1;
price = opt[i - 1].price + LZ5_LIT_ONLY_COST(litlen);
// LZ5_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d\n", (int)(inr-base), price, i, litlen);
}
mlen = 1;
best_mlen = 0;
SET_PRICE(i, mlen, best_mlen, litlen, price);
opt[i].rep = opt[i-1].rep; // copy reps
LZ5_LOG_PARSER("%d: INVALIDATE pred=%d price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)inr-source), cur-cur_min, i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
}
for (int i=cur_min-1; i<=last_pos; i++)
{
LZ5_LOG_PARSER("%d: AFTER price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
}
}
} // for (skip_num = 0, cur = 1; cur <= last_pos; cur++)
best_mlen = opt[last_pos].mlen;
best_off = opt[last_pos].off;
cur = last_pos - best_mlen;
encode: // cur, last_pos, best_mlen, best_off have to be set
for (int i = 1; i <= last_pos; i++)
{
LZ5_LOG_PARSER("%d: price[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
}
LZ5_LOG_PARSER("%d: cur=%d/%d best_mlen=%d best_off=%d rep=%d\n", (int)((char*)ip-source+cur), cur, last_pos, best_mlen, best_off, opt[cur].rep);
opt[0].mlen = 1;
while (cur >= 0)
{
mlen = opt[cur].mlen;
offset = opt[cur].off;
opt[cur].mlen = best_mlen;
opt[cur].off = best_off;
best_mlen = mlen;
best_off = offset;
cur -= mlen;
}
for (int i = 0; i <= last_pos;)
{
LZ5_LOG_PARSER("%d: price2[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen, opt[i].rep);
i += opt[i].mlen;
}
cur = 0;
while (cur < last_pos)
{
LZ5_LOG_PARSER("%d: price3[%d/%d]=%d off=%d mlen=%d litlen=%d rep=%d\n", (int)((char*)ip-source+cur), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen, opt[cur].rep);
mlen = opt[cur].mlen;
if (mlen == 1) { ip++; cur++; continue; }
offset = opt[cur].off;
cur += mlen;
LZ5_LOG_ENCODE("%d: ENCODE literals=%d off=%d mlen=%d ", (int)((char*)ip-source), (int)(ip-anchor), (int)(offset), mlen);
if (offset == 0)
{
res = LZ5HC_encodeSequence(ctx, &ip, &op, &anchor, mlen, ip - rep, limit, oend);
}
else
{
res = LZ5HC_encodeSequence(ctx, &ip, &op, &anchor, mlen, ip - offset, limit, oend);
rep = offset;
}
LZ5_LOG_ENCODE("out=%d\n", (int)((char*)op - dest));
if (res) return 0;
LZ5_LOG_PARSER("%d: offset=%d rep=%d\n", (int)((char*)ip-source), offset, rep);
}
}
/* Encode Last Literals */
{
int lastRun = (int)(iend - anchor);
// if (inputSize > LASTLITERALS && lastRun < LASTLITERALS) { printf("ERROR: lastRun=%d\n", lastRun); }
if ((limit) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) return 0; /* Check output limit */
if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
else *op++ = (BYTE)(lastRun<<ML_BITS);
LZ5_LOG_ENCODE("%d: ENCODE_LAST literals=%d out=%d\n", (int)((char*)ip-source), (int)(iend-anchor), (int)((char*)op -dest));
memcpy(op, anchor, iend - anchor);
op += iend-anchor;
}
/* End */
return (int) ((char*)op-dest);
}
#endif