* HC Compression
***************************************/
/* Update chains up to ip (excluded) */
-static U32 ZSTD_HC_insertAndFindFirstIndex (ZSTD_HC_CCtx* zc, const BYTE* ip, U32 mls)
+static U32 ZSTD_HC_insertAndFindFirstIndex (ZSTD_HC_CCtx* zc, const BYTE* ip, U32 mls)
{
U32* const hashTable = zc->hashTable;
const U32 hashLog = zc->params.hashLog;
}
zc->nextToUpdate = target;
- return hashTable[ZSTD_HC_hashPtr(ip, hashLog, mls)];
+ return hashTable[ZSTD_HC_hashPtr(ip, hashLog, mls)];
}
size_t ml=0;
/* HC4 match finder */
- matchIndex = ZSTD_HC_insertAndFindFirstIndex(zc, ip, matchLengthSearch);
+ matchIndex = ZSTD_HC_insertAndFindFirstIndex (zc, ip, matchLengthSearch);
while ((matchIndex>=lowLimit) && (nbAttempts))
{
}
-static size_t ZSTD_HC_insertAndFindBestMatch_selectMLS (
+FORCE_INLINE size_t ZSTD_HC_insertAndFindBestMatch_selectMLS (
ZSTD_HC_CCtx* zc, /* Index table will be updated */
const BYTE* ip, const BYTE* const iLimit,
const BYTE** matchpos,
}
-size_t ZSTD_HC_compressBlock_greedy(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
+size_t ZSTD_HC_compressBlock_lazy(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
{
seqStore_t* seqStorePtr = &(ctx->seqStore);
const BYTE* const istart = (const BYTE*)src;
if (((ip-ctx->base) - ctx->dictLimit) < REPCODE_STARTVALUE) ip += REPCODE_STARTVALUE;
/* Match Loop */
- while (ip < ilimit)
+ while (ip <= ilimit)
{
- /* repcode */
+ size_t matchLength;
+ size_t offset;
+ const BYTE* start;
+
+ /* try to find a first match */
if (MEM_read32(ip) == MEM_read32(ip - offset_2))
{
- /* store sequence */
- size_t matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend);
- size_t litLength = ip-anchor;
- size_t offset = offset_2;
+ /* repcode : we take it*/
+ size_t offtmp = offset_2;
+ size_t litLength = ip - anchor;
+ matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend);
offset_2 = offset_1;
- offset_1 = offset;
+ offset_1 = offtmp;
ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength);
ip += matchLength+MINMATCH;
anchor = ip;
continue;
}
- offset_2 = offset_1; /* failed once : necessarily offset_1 now */
+ offset_2 = offset_1;
+ matchLength = ZSTD_HC_insertAndFindBestMatch_selectMLS(ctx, ip, iend, &match, maxSearches, mls);
+ if (!matchLength) { ip++; continue; }
- /* repcode at ip+1 */
- if (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1))
- {
- size_t matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend);
- size_t litLength = ip+1-anchor;
- ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength);
- ip += 1+matchLength+MINMATCH;
- anchor = ip;
- continue;
- }
+ /* let's try to find a better solution */
+ offset = ip - match;
+ start = ip;
- /* search */
+ while (ip<ilimit)
{
- size_t matchLength = ZSTD_HC_insertAndFindBestMatch_selectMLS(ctx, ip, iend, &match, maxSearches, mls);
- if (!matchLength) { ip++; continue; }
- /* store sequence */
+ ip ++;
+ if (MEM_read32(ip) == MEM_read32(ip - offset_1))
{
- size_t litLength = ip-anchor;
- offset_1 = ip-match;
- ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset_1, matchLength-MINMATCH);
- ip += matchLength;
- anchor = ip;
+ size_t ml2 = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH;
+ int gain2 = (int)(ml2 * 4);
+ int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset));
+ if (gain2 > gain1)
+ {
+ matchLength = ml2, offset = 0, start = ip;
+ break;
+ }
}
+ {
+ size_t ml2 = ZSTD_HC_insertAndFindBestMatch_selectMLS(ctx, ip, iend, &match, maxSearches, mls);
+ size_t offset2 = ip - match;
+ int gain2 = (int)(ml2*5 - ZSTD_highbit((U32)offset2)); /* raw approx */
+ int gain1 = (int)(matchLength*5 - ZSTD_highbit((U32)offset));
+ if (gain2 > gain1)
+ {
+ matchLength = ml2, offset = offset2, start = ip;
+ continue; /* search a better one */
+ }
+ }
+
+ break; /* nothing found : store previous one */
}
+
+ /* store sequence */
+ {
+ size_t litLength = start - anchor;
+ if (offset) offset_1 = offset;
+ ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH);
+ ip = start + matchLength;
+ anchor = ip;
+ }
+
}
/* Last Literals */
seqStorePtr, srcSize);
}
-size_t ZSTD_HC_compressBlock_lazy(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
+
+size_t ZSTD_HC_compressBlock_greedy(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
{
seqStore_t* seqStorePtr = &(ctx->seqStore);
const BYTE* const istart = (const BYTE*)src;
if (((ip-ctx->base) - ctx->dictLimit) < REPCODE_STARTVALUE) ip += REPCODE_STARTVALUE;
/* Match Loop */
- while (ip <= ilimit)
+ while (ip < ilimit)
{
- size_t matchLength;
- size_t offset;
- const BYTE* start;
-
- /* try to find a first match */
+ /* repcode */
if (MEM_read32(ip) == MEM_read32(ip - offset_2))
{
- /* repcode : we take it*/
- size_t offtmp = offset_2;
- size_t litLength = ip - anchor;
- matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend);
+ /* store sequence */
+ size_t matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend);
+ size_t litLength = ip-anchor;
+ size_t offset = offset_2;
offset_2 = offset_1;
- offset_1 = offtmp;
+ offset_1 = offset;
ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength);
ip += matchLength+MINMATCH;
anchor = ip;
continue;
}
- offset_2 = offset_1;
- matchLength = ZSTD_HC_insertAndFindBestMatch_selectMLS(ctx, ip, iend, &match, maxSearches, mls);
- if (!matchLength) { ip++; continue; }
-
- /* let's try to find a better solution */
- offset = ip - match;
- start = ip;
+ offset_2 = offset_1; /* failed once : necessarily offset_1 now */
- while (ip<ilimit)
+ /* repcode at ip+1 */
+ if (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1))
{
- ip ++;
- if (MEM_read32(ip) == MEM_read32(ip - offset_1))
- {
- size_t ml2 = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH;
- int gain1 = matchLength - (ZSTD_highbit((U32)offset) / 3);
- if ((int)ml2 > gain1)
- {
- matchLength = ml2, offset = 0, start = ip;
- break;
- }
- }
- {
- size_t ml2 = ZSTD_HC_insertAndFindBestMatch_selectMLS(ctx, ip, iend, &match, maxSearches, mls);
- size_t offset2 = ip - match;
- int gain2 = ml2 - (ZSTD_highbit((U32)offset2) / 4); /* raw approx */
- int gain1 = matchLength - (ZSTD_highbit((U32)offset) / 4);
- if (gain2 > gain1)
- {
- matchLength = ml2, offset = offset2, start = ip;
- continue; /* search a better one */
- }
- }
-
- break; /* nothing found : store previous one */
+ size_t matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend);
+ size_t litLength = ip+1-anchor;
+ ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength);
+ ip += 1+matchLength+MINMATCH;
+ anchor = ip;
+ continue;
}
- /* store sequence */
+ /* search */
{
- size_t litLength = start - anchor;
- if (offset) offset_1 = offset;
- ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH);
- ip = start + matchLength;
- anchor = ip;
+ size_t matchLength = ZSTD_HC_insertAndFindBestMatch_selectMLS(ctx, ip, iend, &match, maxSearches, mls);
+ if (!matchLength) { ip++; continue; }
+ /* store sequence */
+ {
+ size_t litLength = ip-anchor;
+ offset_1 = ip-match;
+ ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset_1, matchLength-MINMATCH);
+ ip += matchLength;
+ anchor = ip;
+ }
}
-
}
/* Last Literals */
/* Final compression stage */
return ZSTD_compressSequences((BYTE*)dst, maxDstSize,
+
seqStorePtr, srcSize);
}
size_t ZSTD_HC_compressBlock(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
{
- if (ctx->params.strategy==ZSTD_HC_greedy)
- return ZSTD_HC_compressBlock_greedy(ctx, dst, maxDstSize, src, srcSize);
- return ZSTD_HC_compressBlock_lazy(ctx, dst, maxDstSize, src, srcSize);
+ if (ctx->params.strategy == ZSTD_HC_greedy)
+ return ZSTD_HC_compressBlock_greedy(ctx, dst, maxDstSize, src, srcSize);
+ return ZSTD_HC_compressBlock_lazy(ctx, dst, maxDstSize, src, srcSize);
}
BYTE* const ostart = (BYTE*)dst;
BYTE* op = ostart;
BYTE* const oend = op + maxDstSize;
+ size_t (*blockCompressor) (ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
+
+ if (ctxPtr->params.strategy == ZSTD_HC_greedy)
+ blockCompressor = ZSTD_HC_compressBlock_greedy;
+ else
+ blockCompressor = ZSTD_HC_compressBlock_lazy;
+
while (remaining > blockSize)
{
- size_t cSize = ZSTD_HC_compressBlock(ctxPtr, op+3, oend-op, ip, blockSize);
+ size_t cSize = blockCompressor(ctxPtr, op+3, oend-op, ip, blockSize);
if (cSize == 0)
{
/* last block */
{
- size_t cSize = ZSTD_HC_compressBlock(ctxPtr, op+3, oend-op, ip, remaining);
+ size_t cSize = blockCompressor(ctxPtr, op+3, oend-op, ip, remaining);
if (cSize == 0)
{