From eca2ec0b3bd9aa599441abd13e7d7249e4ff273b Mon Sep 17 00:00:00 2001 From: inikep Date: Fri, 18 Mar 2016 11:03:43 +0100 Subject: [PATCH] introduced ZSTD_REP_MOVE and ZSTD_REP_INIT --- lib/zstd_compress.c | 36 ++++++++++++++++++------------------ lib/zstd_decompress.c | 28 +++++++++++++++++++++------- lib/zstd_internal.h | 10 +++++++++- lib/zstd_opt.h | 12 ++++++------ 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 6f74d1d3..7c857034 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -993,7 +993,7 @@ void ZSTD_compressBlock_fast_generic(ZSTD_CCtx* zc, offset_2 = offset_1; offset_1 = offset; - ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_NUM - 1, mlCode); + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mlCode); } /* match found */ @@ -1105,7 +1105,7 @@ static void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx, offset = current - matchIndex; offset_2 = offset_1; offset_1 = offset; - ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_NUM - 1, mlCode); + ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mlCode); } } /* found a match : store it */ @@ -1563,8 +1563,8 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS; /* init */ - U32 rep[ZSTD_REP_NUM+1]; - for (int i=0; i matchLength) - matchLength = ml2, start = ip, offset=offsetFound + (ZSTD_REP_NUM - 1); + matchLength = ml2, start = ip, offset=offsetFound + ZSTD_REP_MOVE; } if (matchLength < MINMATCH) { @@ -1624,7 +1624,7 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4); if ((ml2 >= MINMATCH) && (gain2 > gain1)) { - matchLength = ml2, offset = offset2 + (ZSTD_REP_NUM - 1), start = ip; + matchLength = ml2, offset = offset2 + ZSTD_REP_MOVE, start = ip; continue; /* search a better one */ } } @@ -1645,7 +1645,7 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7); if ((ml2 >= MINMATCH) && (gain2 > gain1)) { - matchLength = ml2, offset = offset2 + (ZSTD_REP_NUM - 1), start = ip; + matchLength = ml2, offset = offset2 + ZSTD_REP_MOVE, start = ip; continue; } } } break; /* nothing found : store previous solution */ @@ -1653,7 +1653,7 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, /* catch up */ if (offset >= ZSTD_REP_NUM) { - while ((start>anchor) && (start>base+offset-(ZSTD_REP_NUM-1)) && (start[-1] == start[-1-offset+(ZSTD_REP_NUM-1)])) /* only search for offset within prefix */ + while ((start>anchor) && (start>base+offset-ZSTD_REP_MOVE) && (start[-1] == start[-1-offset+ZSTD_REP_MOVE])) /* only search for offset within prefix */ { start--; matchLength++; } } @@ -1666,7 +1666,7 @@ _storeSequence: rep[3] = rep[2]; rep[2] = rep[1]; rep[1] = rep[0]; - rep[0] = offset - (ZSTD_REP_NUM - 1); + rep[0] = offset - ZSTD_REP_MOVE; #else if (kSlotNew < 3) rep[3] = rep[2]; @@ -1674,7 +1674,7 @@ _storeSequence: rep[2] = rep[1]; if (kSlotNew < 1) rep[1] = rep[0]; - rep[kSlotNew] = offset - (ZSTD_REP_NUM - 1); + rep[kSlotNew] = offset - ZSTD_REP_MOVE; #endif } else { if (offset != 0) { @@ -1691,7 +1691,7 @@ _storeSequence: } #else if (offset >= ZSTD_REP_NUM) { - rep[1] = rep[0]; rep[0] = offset - (ZSTD_REP_NUM - 1); + rep[1] = rep[0]; rep[0] = offset - ZSTD_REP_MOVE; } #endif size_t litLength = start - anchor; @@ -1764,8 +1764,8 @@ void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, printf("ZSTD_compressBlock_lazy_extDict_generic reps not implemented!\n"); exit(1); /* init */ - U32 rep[ZSTD_REP_NUM]; - for (int i=0; i matchLength) - matchLength = ml2, start = ip, offset=offsetFound + (ZSTD_REP_NUM - 1); + matchLength = ml2, start = ip, offset=offsetFound + ZSTD_REP_MOVE; } if (matchLength < MINMATCH) { @@ -1831,7 +1831,7 @@ void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4); if ((ml2 >= MINMATCH) && (gain2 > gain1)) { - matchLength = ml2, offset = offset2 + (ZSTD_REP_NUM - 1), start = ip; + matchLength = ml2, offset = offset2 + ZSTD_REP_MOVE, start = ip; continue; /* search a better one */ } } @@ -1862,7 +1862,7 @@ void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7); if ((ml2 >= MINMATCH) && (gain2 > gain1)) { - matchLength = ml2, offset = offset2 + (ZSTD_REP_NUM - 1), start = ip; + matchLength = ml2, offset = offset2 + ZSTD_REP_MOVE, start = ip; continue; } } } break; /* nothing found : store previous solution */ @@ -1870,11 +1870,11 @@ void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, /* catch up */ if (offset >= ZSTD_REP_NUM) { - U32 matchIndex = (U32)((start-base) - (offset - (ZSTD_REP_NUM - 1))); + U32 matchIndex = (U32)((start-base) - (offset - ZSTD_REP_MOVE)); const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex; const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart; while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */ - rep[1] = rep[0]; rep[0] = offset - (ZSTD_REP_NUM - 1); + rep[1] = rep[0]; rep[0] = offset - ZSTD_REP_MOVE; } /* store sequence */ diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index 6e0e39b7..201e6751 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -615,7 +615,7 @@ typedef struct { FSE_DState_t stateLL; FSE_DState_t stateOffb; FSE_DState_t stateML; - size_t prevOffset[ZSTD_REP_NUM]; + size_t prevOffset[ZSTD_REP_INIT]; const BYTE* dumps; const BYTE* dumpsEnd; } seqState_t; @@ -672,13 +672,13 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState, const U32 mls) else offset = seqState->prevOffset[0]; } else { - offset -= ZSTD_REP_NUM - 1; -#if 0 + offset -= ZSTD_REP_MOVE; + #if 0 seqState->prevOffset[3] = seqState->prevOffset[2]; seqState->prevOffset[2] = seqState->prevOffset[1]; seqState->prevOffset[1] = seqState->prevOffset[0]; seqState->prevOffset[0] = offset; -#else + #else if (kSlotNew < 3) seqState->prevOffset[3] = seqState->prevOffset[2]; if (kSlotNew < 2) @@ -686,11 +686,25 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState, const U32 mls) if (kSlotNew < 1) seqState->prevOffset[1] = seqState->prevOffset[0]; seqState->prevOffset[kSlotNew] = offset; -#endif + #endif } -#else +#else // ZSTD_REP_NUM == 1 + #if 0 if (offsetCode==0) offset = litLength ? seq->offset : seqState->prevOffset[0]; /* repcode, cmove */ + else offset -= ZSTD_REP_MOVE; if (offsetCode | !litLength) seqState->prevOffset[0] = seq->offset; /* cmove */ + #else + if (offsetCode==0) { + if (!litLength) { + offset = seqState->prevOffset[0]; /* repcode, cmove */ + seqState->prevOffset[0] = seq->offset; /* cmove */ + } else + offset = seq->offset; /* repcode, cmove */ + } else { + seqState->prevOffset[0] = seq->offset; /* cmove */ + offset -= ZSTD_REP_MOVE; + } + #endif #endif FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* update */ // printf("offsetCode=%d nbBits=%d offset=%d\n", offsetCode, nbBits, (int)offset); fflush(stdout); @@ -845,7 +859,7 @@ static size_t ZSTD_decompressSequences( sequence.offset = REPCODE_STARTVALUE; seqState.dumps = dumps; seqState.dumpsEnd = dumps + dumpsLength; - for (int i=0; inextToUpdate3 = ctx->nextToUpdate; @@ -688,7 +688,7 @@ _storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */ #endif ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-minMatch); - ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset ? offset + ZSTD_REP_NUM - 1 : 0, mlen-minMatch); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset ? offset + ZSTD_REP_MOVE : 0, mlen-minMatch); anchor = ip = ip + mlen; } /* for (cur=0; cur < last_pos; ) */ @@ -748,8 +748,8 @@ void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx* ctx, U32 cur, match_num, last_pos, litlen, price; /* init */ - U32 rep[ZSTD_REP_NUM+1]; - for (int i=0; inextToUpdate3 = ctx->nextToUpdate; @@ -1036,7 +1036,7 @@ _storeSequence: // cur, last_pos, best_mlen, best_off have to be set #endif ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen-minMatch); - ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset ? offset + ZSTD_REP_NUM - 1 : 0, mlen-minMatch); + ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset ? offset + ZSTD_REP_MOVE : 0, mlen-minMatch); anchor = ip = ip + mlen; }