minor code refactoring

This commit is contained in:
Yann Collet 2016-03-19 18:08:32 +01:00
parent c6eea2b2f2
commit 70e4577179
2 changed files with 66 additions and 78 deletions

View File

@ -155,7 +155,7 @@ void ZSTD_validateParams(ZSTD_parameters* params)
CLAMP(params->targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); CLAMP(params->targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX);
if ((U32)params->strategy>(U32)ZSTD_btopt) params->strategy = ZSTD_btopt; if ((U32)params->strategy>(U32)ZSTD_btopt) params->strategy = ZSTD_btopt;
/* correct params, to use less memory */ /* resize params, to use less memory when necessary */
if ((params->srcSize > 0) && (params->srcSize < (1<<ZSTD_WINDOWLOG_MAX))) { if ((params->srcSize > 0) && (params->srcSize < (1<<ZSTD_WINDOWLOG_MAX))) {
U32 const srcLog = ZSTD_highbit((U32)(params->srcSize)-1) + 1; U32 const srcLog = ZSTD_highbit((U32)(params->srcSize)-1) + 1;
if (params->windowLog > srcLog) params->windowLog = srcLog; if (params->windowLog > srcLog) params->windowLog = srcLog;
@ -703,8 +703,7 @@ size_t ZSTD_compressSequences(ZSTD_CCtx* zc,
zc->flagStaticTables = 0; zc->flagStaticTables = 0;
/* Encoding Sequences */ /* Encoding Sequences */
{ { BIT_CStream_t blockStream;
BIT_CStream_t blockStream;
FSE_CState_t stateMatchLength; FSE_CState_t stateMatchLength;
FSE_CState_t stateOffsetBits; FSE_CState_t stateOffsetBits;
FSE_CState_t stateLitLength; FSE_CState_t stateLitLength;
@ -739,14 +738,13 @@ size_t ZSTD_compressSequences(ZSTD_CCtx* zc,
FSE_flushCState(&blockStream, &stateLitLength); FSE_flushCState(&blockStream, &stateLitLength);
{ size_t const streamSize = BIT_closeCStream(&blockStream); { size_t const streamSize = BIT_closeCStream(&blockStream);
if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */ if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */
op += streamSize; } op += streamSize; }
} }
/* check compressibility */ /* check compressibility */
_check_compressibility: _check_compressibility:
{ { size_t const minGain = ZSTD_minGain(srcSize);
size_t const minGain = ZSTD_minGain(srcSize);
size_t const maxCSize = srcSize - minGain; size_t const maxCSize = srcSize - minGain;
if ((size_t)(op-ostart) >= maxCSize) return 0; if ((size_t)(op-ostart) >= maxCSize) return 0;
} }
@ -1028,8 +1026,8 @@ void ZSTD_compressBlock_fast_generic(ZSTD_CCtx* zc,
while ( (ip <= ilimit) while ( (ip <= ilimit)
&& (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) { && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) {
/* store sequence */ /* store sequence */
size_t rlCode = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend); size_t const rlCode = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend);
size_t tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */ { size_t const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base); hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base);
ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rlCode); ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rlCode);
ip += rlCode+MINMATCH; ip += rlCode+MINMATCH;
@ -1037,8 +1035,8 @@ void ZSTD_compressBlock_fast_generic(ZSTD_CCtx* zc,
continue; /* faster when present ... (?) */ continue; /* faster when present ... (?) */
} } } } } }
{ /* Last Literals */ /* Last Literals */
size_t lastLLSize = iend - anchor; { size_t const lastLLSize = iend - anchor;
memcpy(seqStorePtr->lit, anchor, lastLLSize); memcpy(seqStorePtr->lit, anchor, lastLLSize);
seqStorePtr->lit += lastLLSize; seqStorePtr->lit += lastLLSize;
} }
@ -1156,8 +1154,7 @@ static void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx,
} } } } } }
/* Last Literals */ /* Last Literals */
{ { size_t const lastLLSize = iend - anchor;
size_t lastLLSize = iend - anchor;
memcpy(seqStorePtr->lit, anchor, lastLLSize); memcpy(seqStorePtr->lit, anchor, lastLLSize);
seqStorePtr->lit += lastLLSize; seqStorePtr->lit += lastLLSize;
} }
@ -1600,10 +1597,9 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx,
if (depth==0) goto _storeSequence; if (depth==0) goto _storeSequence;
} }
{ /* first search (depth 0) */
/* first search (depth 0) */ { size_t offsetFound = 99999999;
size_t offsetFound = 99999999; size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
size_t ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
if (ml2 > matchLength) if (ml2 > matchLength)
matchLength = ml2, start = ip, offset=offsetFound; matchLength = ml2, start = ip, offset=offsetFound;
} }
@ -1618,17 +1614,16 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx,
while (ip<ilimit) { while (ip<ilimit) {
ip ++; ip ++;
if ((offset) && (MEM_read32(ip) == MEM_read32(ip - offset_1))) { if ((offset) && (MEM_read32(ip) == MEM_read32(ip - offset_1))) {
size_t mlRep = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH; size_t const mlRep = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH;
int gain2 = (int)(mlRep * 3); int const gain2 = (int)(mlRep * 3);
int gain1 = (int)(matchLength*3 - ZSTD_highbit((U32)offset+1) + 1); int const gain1 = (int)(matchLength*3 - ZSTD_highbit((U32)offset+1) + 1);
if ((mlRep >= MINMATCH) && (gain2 > gain1)) if ((mlRep >= MINMATCH) && (gain2 > gain1))
matchLength = mlRep, offset = 0, start = ip; matchLength = mlRep, offset = 0, start = ip;
} }
{ { size_t offset2=99999999;
size_t offset2=999999; size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); int const gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */
int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int const gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4);
int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4);
if ((ml2 >= MINMATCH) && (gain2 > gain1)) { if ((ml2 >= MINMATCH) && (gain2 > gain1)) {
matchLength = ml2, offset = offset2, start = ip; matchLength = ml2, offset = offset2, start = ip;
continue; /* search a better one */ continue; /* search a better one */
@ -1638,17 +1633,16 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx,
if ((depth==2) && (ip<ilimit)) { if ((depth==2) && (ip<ilimit)) {
ip ++; ip ++;
if ((offset) && (MEM_read32(ip) == MEM_read32(ip - offset_1))) { if ((offset) && (MEM_read32(ip) == MEM_read32(ip - offset_1))) {
size_t ml2 = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH; size_t const ml2 = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH;
int gain2 = (int)(ml2 * 4); int const gain2 = (int)(ml2 * 4);
int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 1); int const gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 1);
if ((ml2 >= MINMATCH) && (gain2 > gain1)) if ((ml2 >= MINMATCH) && (gain2 > gain1))
matchLength = ml2, offset = 0, start = ip; matchLength = ml2, offset = 0, start = ip;
} }
{ { size_t offset2=99999999;
size_t offset2=999999; size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); int const gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */
int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int const gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7);
int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7);
if ((ml2 >= MINMATCH) && (gain2 > gain1)) { if ((ml2 >= MINMATCH) && (gain2 > gain1)) {
matchLength = ml2, offset = offset2, start = ip; matchLength = ml2, offset = offset2, start = ip;
continue; continue;
@ -1665,8 +1659,7 @@ void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx,
/* store sequence */ /* store sequence */
_storeSequence: _storeSequence:
{ { size_t const litLength = start - anchor;
size_t litLength = start - anchor;
ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH); ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH);
anchor = ip = start + matchLength; anchor = ip = start + matchLength;
} }
@ -1686,8 +1679,7 @@ _storeSequence:
} } } }
/* Last Literals */ /* Last Literals */
{ { size_t const lastLLSize = iend - anchor;
size_t lastLLSize = iend - anchor;
memcpy(seqStorePtr->lit, anchor, lastLLSize); memcpy(seqStorePtr->lit, anchor, lastLLSize);
seqStorePtr->lit += lastLLSize; seqStorePtr->lit += lastLLSize;
} }
@ -1771,10 +1763,9 @@ void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx,
if (depth==0) goto _storeSequence; if (depth==0) goto _storeSequence;
} } } }
{ /* first search (depth 0) */
/* first search (depth 0) */ { size_t offsetFound = 99999999;
size_t offsetFound = 99999999; size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
size_t ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
if (ml2 > matchLength) if (ml2 > matchLength)
matchLength = ml2, start = ip, offset=offsetFound; matchLength = ml2, start = ip, offset=offsetFound;
} }
@ -1798,19 +1789,18 @@ void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx,
if (MEM_read32(ip) == MEM_read32(repMatch)) { if (MEM_read32(ip) == MEM_read32(repMatch)) {
/* repcode detected */ /* repcode detected */
const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
size_t repLength = ZSTD_count_2segments(ip+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; size_t const repLength = ZSTD_count_2segments(ip+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH;
int gain2 = (int)(repLength * 3); int const gain2 = (int)(repLength * 3);
int gain1 = (int)(matchLength*3 - ZSTD_highbit((U32)offset+1) + 1); int const gain1 = (int)(matchLength*3 - ZSTD_highbit((U32)offset+1) + 1);
if ((repLength >= MINMATCH) && (gain2 > gain1)) if ((repLength >= MINMATCH) && (gain2 > gain1))
matchLength = repLength, offset = 0, start = ip; matchLength = repLength, offset = 0, start = ip;
} } } }
/* search match, depth 1 */ /* search match, depth 1 */
{ { size_t offset2=99999999;
size_t offset2=999999; size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); int const gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */
int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int const gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4);
int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4);
if ((ml2 >= MINMATCH) && (gain2 > gain1)) { if ((ml2 >= MINMATCH) && (gain2 > gain1)) {
matchLength = ml2, offset = offset2, start = ip; matchLength = ml2, offset = offset2, start = ip;
continue; /* search a better one */ continue; /* search a better one */
@ -1837,11 +1827,10 @@ void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx,
} } } }
/* search match, depth 2 */ /* search match, depth 2 */
{ { size_t offset2=99999999;
size_t offset2=999999; size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); int const gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */
int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ int const gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7);
int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7);
if ((ml2 >= MINMATCH) && (gain2 > gain1)) { if ((ml2 >= MINMATCH) && (gain2 > gain1)) {
matchLength = ml2, offset = offset2, start = ip; matchLength = ml2, offset = offset2, start = ip;
continue; continue;
@ -1964,8 +1953,9 @@ static size_t ZSTD_compress_generic (ZSTD_CCtx* zc,
if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE) return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */ if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE) return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */
if (remaining < blockSize) blockSize = remaining; if (remaining < blockSize) blockSize = remaining;
if ((U32)(ip+blockSize - zc->base) > zc->loadedDictEnd + maxDist) { /* enforce maxDist */ if ((U32)(ip+blockSize - zc->base) > zc->loadedDictEnd + maxDist) {
U32 newLowLimit = (U32)(ip+blockSize - zc->base) - maxDist; /* enforce maxDist */
U32 const newLowLimit = (U32)(ip+blockSize - zc->base) - maxDist;
if (zc->lowLimit < newLowLimit) zc->lowLimit = newLowLimit; if (zc->lowLimit < newLowLimit) zc->lowLimit = newLowLimit;
if (zc->dictLimit < zc->lowLimit) zc->dictLimit = zc->lowLimit; if (zc->dictLimit < zc->lowLimit) zc->dictLimit = zc->lowLimit;
} }
@ -2020,7 +2010,7 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* zc,
/* Check if blocks follow each other */ /* Check if blocks follow each other */
if (src != zc->nextSrc) { if (src != zc->nextSrc) {
/* not contiguous */ /* not contiguous */
size_t delta = zc->nextSrc - ip; size_t const delta = zc->nextSrc - ip;
zc->lowLimit = zc->dictLimit; zc->lowLimit = zc->dictLimit;
zc->dictLimit = (U32)(zc->nextSrc - zc->base); zc->dictLimit = (U32)(zc->nextSrc - zc->base);
zc->dictBase = zc->base; zc->dictBase = zc->base;
@ -2031,10 +2021,10 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* zc,
/* preemptive overflow correction */ /* preemptive overflow correction */
if (zc->lowLimit > (1<<30)) { if (zc->lowLimit > (1<<30)) {
U32 btplus = (zc->params.strategy == ZSTD_btlazy2) || (zc->params.strategy == ZSTD_btopt); U32 const btplus = (zc->params.strategy == ZSTD_btlazy2) || (zc->params.strategy == ZSTD_btopt);
U32 contentMask = (1 << (zc->params.contentLog - btplus)) - 1; U32 const contentMask = (1 << (zc->params.contentLog - btplus)) - 1;
U32 newLowLimit = zc->lowLimit & contentMask; /* preserve position % contentSize */ U32 const newLowLimit = zc->lowLimit & contentMask; /* preserve position % contentSize */
U32 correction = zc->lowLimit - newLowLimit; U32 const correction = zc->lowLimit - newLowLimit;
ZSTD_reduceIndex(zc, correction); ZSTD_reduceIndex(zc, correction);
zc->base += correction; zc->base += correction;
zc->dictBase += correction; zc->dictBase += correction;
@ -2051,10 +2041,9 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* zc,
} }
zc->nextSrc = ip + srcSize; zc->nextSrc = ip + srcSize;
{ { size_t const cSize = frame ?
size_t cSize; ZSTD_compress_generic (zc, dst, dstSize, src, srcSize) :
if (frame) cSize = ZSTD_compress_generic (zc, dst, dstSize, src, srcSize); ZSTD_compressBlock_internal (zc, dst, dstSize, src, srcSize);
else cSize = ZSTD_compressBlock_internal (zc, dst, dstSize, src, srcSize);
if (ZSTD_isError(cSize)) return cSize; if (ZSTD_isError(cSize)) return cSize;
return cSize + hbSize; return cSize + hbSize;
} }
@ -2138,7 +2127,7 @@ static size_t ZSTD_loadDictEntropyStats(ZSTD_CCtx* zc, const void* dict, size_t
short litlengthNCount[MaxLL+1]; short litlengthNCount[MaxLL+1];
unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSELog; unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSELog;
const size_t hufHeaderSize = HUF_readCTable(zc->hufTable, 255, dict, dictSize); size_t const hufHeaderSize = HUF_readCTable(zc->hufTable, 255, dict, dictSize);
if (HUF_isError(hufHeaderSize)) return ERROR(dictionary_corrupted); if (HUF_isError(hufHeaderSize)) return ERROR(dictionary_corrupted);
zc->flagStaticTables = 1; zc->flagStaticTables = 1;
dict = (const char*)dict + hufHeaderSize; dict = (const char*)dict + hufHeaderSize;
@ -2241,7 +2230,7 @@ size_t ZSTD_compressEnd(ZSTD_CCtx* zc, void* dst, size_t dstCapacity)
BYTE* op = (BYTE*)dst; BYTE* op = (BYTE*)dst;
size_t hbSize = 0; size_t hbSize = 0;
/* empty frame */ /* special case : empty frame : header still within internal buffer */
if (zc->stage==0) { if (zc->stage==0) {
hbSize = zc->hbSize; hbSize = zc->hbSize;
if (dstCapacity <= hbSize) return ERROR(dstSize_tooSmall); if (dstCapacity <= hbSize) return ERROR(dstSize_tooSmall);
@ -2265,16 +2254,15 @@ size_t ZSTD_compress_usingPreparedCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* prepare
void* dst, size_t dstCapacity, void* dst, size_t dstCapacity,
const void* src, size_t srcSize) const void* src, size_t srcSize)
{ {
size_t outSize; { size_t const errorCode = ZSTD_copyCCtx(cctx, preparedCCtx);
size_t errorCode = ZSTD_copyCCtx(cctx, preparedCCtx); if (ZSTD_isError(errorCode)) return errorCode;
if (ZSTD_isError(errorCode)) return errorCode; }
errorCode = ZSTD_compressContinue(cctx, dst, dstCapacity, src, srcSize); { size_t const cSize = ZSTD_compressContinue(cctx, dst, dstCapacity, src, srcSize);
if (ZSTD_isError(errorCode)) return errorCode; if (ZSTD_isError(cSize)) return cSize;
outSize = errorCode; { size_t const endSize = ZSTD_compressEnd(cctx, (char*)dst+cSize, dstCapacity-cSize);
errorCode = ZSTD_compressEnd(cctx, (char*)dst+outSize, dstCapacity-outSize); if (ZSTD_isError(endSize)) return endSize;
if (ZSTD_isError(errorCode)) return errorCode; return cSize + endSize;
outSize += errorCode; } }
return outSize;
} }

View File

@ -347,7 +347,7 @@ _findError:
#endif #endif
} }
DISPLAY("%2i-\n", cLevel); DISPLAY("%2i#\n", cLevel);
} }
/* clean up */ /* clean up */