From a5dacdf55142a4db8dca4029eafffd3d3068d0a3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 6 May 2016 16:55:27 +0200 Subject: [PATCH] updated fse (minor refactor) --- lib/decompress/fse_decompress.c | 69 ++++++++++++++++----------------- lib/decompress/huf_decompress.c | 46 +++++++++++----------- 2 files changed, 57 insertions(+), 58 deletions(-) diff --git a/lib/decompress/fse_decompress.c b/lib/decompress/fse_decompress.c index 698dbfc2..8091c845 100644 --- a/lib/decompress/fse_decompress.c +++ b/lib/decompress/fse_decompress.c @@ -111,17 +111,13 @@ void FSE_freeDTable (FSE_DTable* dt) size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) { FSE_DTableHeader DTableH; - void* const tdPtr = dt+1; /* because dt is unsigned, 32-bits aligned on 32-bits */ + void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); - const U32 tableSize = 1 << tableLog; - const U32 tableMask = tableSize-1; - const U32 step = FSE_TABLESTEP(tableSize); U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; + U32 const maxSV1 = maxSymbolValue + 1; + U32 const tableSize = 1 << tableLog; U32 highThreshold = tableSize-1; - S16 const largeLimit= (S16)(1 << (tableLog-1)); - U32 noLarge = 1; - U32 s; /* Sanity Checks */ if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge); @@ -129,18 +125,24 @@ size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned /* Init, lay down lowprob symbols */ DTableH.tableLog = (U16)tableLog; - for (s=0; s<=maxSymbolValue; s++) { - if (normalizedCounter[s]==-1) { - tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; - symbolNext[s] = 1; - } else { - if (normalizedCounter[s] >= largeLimit) noLarge=0; - symbolNext[s] = normalizedCounter[s]; - } } + DTableH.fastMode = 1; + { S16 const largeLimit= (S16)(1 << (tableLog-1)); + U32 s; + for (s=0; s= largeLimit) DTableH.fastMode=0; + symbolNext[s] = normalizedCounter[s]; + } } } + memcpy(dt, &DTableH, sizeof(DTableH)); /* Spread symbols */ - { U32 position = 0; - for (s=0; s<=maxSymbolValue; s++) { + { U32 const tableMask = tableSize-1; + U32 const step = FSE_TABLESTEP(tableSize); + U32 s, position = 0; + for (s=0; stableLog = (U16)nbBits; DTableH->fastMode = 1; - for (s=0; s<=maxSymbolValue; s++) { + for (s=0; s= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */ - ip += errorCode; - cSrcSize -= errorCode; + { size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); + if (FSE_isError(NCountLength)) return NCountLength; + if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */ + ip += NCountLength; + cSrcSize -= NCountLength; + } - errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog); - if (FSE_isError(errorCode)) return errorCode; + { size_t const errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog); + if (FSE_isError(errorCode)) return errorCode; } - /* always return, even if it is an error code */ - return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); + return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); /* always return, even if it is an error code */ } diff --git a/lib/decompress/huf_decompress.c b/lib/decompress/huf_decompress.c index d64808f6..55f417ba 100644 --- a/lib/decompress/huf_decompress.c +++ b/lib/decompress/huf_decompress.c @@ -114,7 +114,7 @@ size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize) /* Prepare ranks */ nextRankStart = 0; - for (n=1; n<=tableLog; n++) { + for (n=1; n> consumed; - } } } + { U32* const rankVal0 = rankVal[0]; + { int const rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */ + U32 nextRankVal = 0; + U32 w; + for (w=1; w> consumed; + } } } } HUF_fillDTableX4(dt, memLog, sortedSymbol, sizeOfSort, @@ -788,7 +790,7 @@ size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize) /* Get start index of each weight */ { U32 w, nextRankStart = 0; - for (w=1; w<=maxW; w++) { + for (w=1; w> consumed; } } }