updated fse (minor refactor)
parent
18dedece91
commit
a5dacdf551
|
@ -111,17 +111,13 @@ void FSE_freeDTable (FSE_DTable* dt)
|
||||||
size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
|
size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
|
||||||
{
|
{
|
||||||
FSE_DTableHeader DTableH;
|
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);
|
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];
|
U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
|
||||||
|
|
||||||
|
U32 const maxSV1 = maxSymbolValue + 1;
|
||||||
|
U32 const tableSize = 1 << tableLog;
|
||||||
U32 highThreshold = tableSize-1;
|
U32 highThreshold = tableSize-1;
|
||||||
S16 const largeLimit= (S16)(1 << (tableLog-1));
|
|
||||||
U32 noLarge = 1;
|
|
||||||
U32 s;
|
|
||||||
|
|
||||||
/* Sanity Checks */
|
/* Sanity Checks */
|
||||||
if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
|
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 */
|
/* Init, lay down lowprob symbols */
|
||||||
DTableH.tableLog = (U16)tableLog;
|
DTableH.tableLog = (U16)tableLog;
|
||||||
for (s=0; s<=maxSymbolValue; s++) {
|
DTableH.fastMode = 1;
|
||||||
if (normalizedCounter[s]==-1) {
|
{ S16 const largeLimit= (S16)(1 << (tableLog-1));
|
||||||
tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
|
U32 s;
|
||||||
symbolNext[s] = 1;
|
for (s=0; s<maxSV1; s++) {
|
||||||
} else {
|
if (normalizedCounter[s]==-1) {
|
||||||
if (normalizedCounter[s] >= largeLimit) noLarge=0;
|
tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
|
||||||
symbolNext[s] = normalizedCounter[s];
|
symbolNext[s] = 1;
|
||||||
} }
|
} else {
|
||||||
|
if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
|
||||||
|
symbolNext[s] = normalizedCounter[s];
|
||||||
|
} } }
|
||||||
|
memcpy(dt, &DTableH, sizeof(DTableH));
|
||||||
|
|
||||||
/* Spread symbols */
|
/* Spread symbols */
|
||||||
{ U32 position = 0;
|
{ U32 const tableMask = tableSize-1;
|
||||||
for (s=0; s<=maxSymbolValue; s++) {
|
U32 const step = FSE_TABLESTEP(tableSize);
|
||||||
|
U32 s, position = 0;
|
||||||
|
for (s=0; s<maxSV1; s++) {
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<normalizedCounter[s]; i++) {
|
for (i=0; i<normalizedCounter[s]; i++) {
|
||||||
tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
|
tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
|
||||||
|
@ -154,15 +156,12 @@ size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned
|
||||||
/* Build Decoding table */
|
/* Build Decoding table */
|
||||||
{ U32 u;
|
{ U32 u;
|
||||||
for (u=0; u<tableSize; u++) {
|
for (u=0; u<tableSize; u++) {
|
||||||
FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
|
FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
|
||||||
|
|
||||||
U16 nextState = symbolNext[symbol]++;
|
U16 nextState = symbolNext[symbol]++;
|
||||||
tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
|
tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
|
||||||
tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
|
tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
|
||||||
} }
|
} }
|
||||||
|
|
||||||
DTableH.fastMode = (U16)noLarge;
|
|
||||||
memcpy(dt, &DTableH, sizeof(DTableH));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +198,7 @@ size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
|
||||||
FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;
|
FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;
|
||||||
const unsigned tableSize = 1 << nbBits;
|
const unsigned tableSize = 1 << nbBits;
|
||||||
const unsigned tableMask = tableSize - 1;
|
const unsigned tableMask = tableSize - 1;
|
||||||
const unsigned maxSymbolValue = tableMask;
|
const unsigned maxSV1 = tableMask+1;
|
||||||
unsigned s;
|
unsigned s;
|
||||||
|
|
||||||
/* Sanity checks */
|
/* Sanity checks */
|
||||||
|
@ -208,7 +207,7 @@ size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
|
||||||
/* Build Decoding Table */
|
/* Build Decoding Table */
|
||||||
DTableH->tableLog = (U16)nbBits;
|
DTableH->tableLog = (U16)nbBits;
|
||||||
DTableH->fastMode = 1;
|
DTableH->fastMode = 1;
|
||||||
for (s=0; s<=maxSymbolValue; s++) {
|
for (s=0; s<maxSV1; s++) {
|
||||||
dinfo[s].newState = 0;
|
dinfo[s].newState = 0;
|
||||||
dinfo[s].symbol = (BYTE)s;
|
dinfo[s].symbol = (BYTE)s;
|
||||||
dinfo[s].nbBits = (BYTE)nbBits;
|
dinfo[s].nbBits = (BYTE)nbBits;
|
||||||
|
@ -230,11 +229,10 @@ FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
|
||||||
BIT_DStream_t bitD;
|
BIT_DStream_t bitD;
|
||||||
FSE_DState_t state1;
|
FSE_DState_t state1;
|
||||||
FSE_DState_t state2;
|
FSE_DState_t state2;
|
||||||
size_t errorCode;
|
|
||||||
|
|
||||||
/* Init */
|
/* Init */
|
||||||
errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
|
{ size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
|
||||||
if (FSE_isError(errorCode)) return errorCode;
|
if (FSE_isError(errorCode)) return errorCode; }
|
||||||
|
|
||||||
FSE_initDState(&state1, &bitD, dt);
|
FSE_initDState(&state1, &bitD, dt);
|
||||||
FSE_initDState(&state2, &bitD, dt);
|
FSE_initDState(&state2, &bitD, dt);
|
||||||
|
@ -308,22 +306,21 @@ size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSr
|
||||||
DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
|
DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
|
||||||
unsigned tableLog;
|
unsigned tableLog;
|
||||||
unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
|
unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
|
||||||
size_t errorCode;
|
|
||||||
|
|
||||||
if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
|
if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
|
||||||
|
|
||||||
/* normal FSE decoding mode */
|
/* normal FSE decoding mode */
|
||||||
errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
|
{ size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
|
||||||
if (FSE_isError(errorCode)) return errorCode;
|
if (FSE_isError(NCountLength)) return NCountLength;
|
||||||
if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
|
if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
|
||||||
ip += errorCode;
|
ip += NCountLength;
|
||||||
cSrcSize -= errorCode;
|
cSrcSize -= NCountLength;
|
||||||
|
}
|
||||||
|
|
||||||
errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);
|
{ size_t const errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);
|
||||||
if (FSE_isError(errorCode)) return errorCode;
|
if (FSE_isError(errorCode)) return errorCode; }
|
||||||
|
|
||||||
/* always return, even if it is an error code */
|
return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); /* always return, even if it is an error code */
|
||||||
return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -114,7 +114,7 @@ size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
|
||||||
|
|
||||||
/* Prepare ranks */
|
/* Prepare ranks */
|
||||||
nextRankStart = 0;
|
nextRankStart = 0;
|
||||||
for (n=1; n<=tableLog; n++) {
|
for (n=1; n<tableLog+1; n++) {
|
||||||
U32 current = nextRankStart;
|
U32 current = nextRankStart;
|
||||||
nextRankStart += (rankVal[n] << (n-1));
|
nextRankStart += (rankVal[n] << (n-1));
|
||||||
rankVal[n] = current;
|
rankVal[n] = current;
|
||||||
|
@ -442,7 +442,7 @@ size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
|
||||||
|
|
||||||
/* Get start index of each weight */
|
/* Get start index of each weight */
|
||||||
{ U32 w, nextRankStart = 0;
|
{ U32 w, nextRankStart = 0;
|
||||||
for (w=1; w<=maxW; w++) {
|
for (w=1; w<maxW+1; w++) {
|
||||||
U32 current = nextRankStart;
|
U32 current = nextRankStart;
|
||||||
nextRankStart += rankStats[w];
|
nextRankStart += rankStats[w];
|
||||||
rankStart[w] = current;
|
rankStart[w] = current;
|
||||||
|
@ -454,8 +454,8 @@ size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
|
||||||
/* sort symbols by weight */
|
/* sort symbols by weight */
|
||||||
{ U32 s;
|
{ U32 s;
|
||||||
for (s=0; s<nbSymbols; s++) {
|
for (s=0; s<nbSymbols; s++) {
|
||||||
U32 w = weightList[s];
|
U32 const w = weightList[s];
|
||||||
U32 r = rankStart[w]++;
|
U32 const r = rankStart[w]++;
|
||||||
sortedSymbol[r].symbol = (BYTE)s;
|
sortedSymbol[r].symbol = (BYTE)s;
|
||||||
sortedSymbol[r].weight = (BYTE)w;
|
sortedSymbol[r].weight = (BYTE)w;
|
||||||
}
|
}
|
||||||
|
@ -463,21 +463,23 @@ size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Build rankVal */
|
/* Build rankVal */
|
||||||
{ const U32 minBits = tableLog+1 - maxW;
|
{ U32* const rankVal0 = rankVal[0];
|
||||||
U32 nextRankVal = 0;
|
{ int const rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
|
||||||
U32 w, consumed;
|
U32 nextRankVal = 0;
|
||||||
const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
|
U32 w;
|
||||||
U32* rankVal0 = rankVal[0];
|
for (w=1; w<maxW+1; w++) {
|
||||||
for (w=1; w<=maxW; w++) {
|
U32 current = nextRankVal;
|
||||||
U32 current = nextRankVal;
|
nextRankVal += rankStats[w] << (w+rescale);
|
||||||
nextRankVal += rankStats[w] << (w+rescale);
|
rankVal0[w] = current;
|
||||||
rankVal0[w] = current;
|
} }
|
||||||
}
|
{ U32 const minBits = tableLog+1 - maxW;
|
||||||
for (consumed = minBits; consumed <= memLog - minBits; consumed++) {
|
U32 consumed;
|
||||||
U32* rankValPtr = rankVal[consumed];
|
for (consumed = minBits; consumed < memLog - minBits + 1; consumed++) {
|
||||||
for (w = 1; w <= maxW; w++) {
|
U32* const rankValPtr = rankVal[consumed];
|
||||||
rankValPtr[w] = rankVal0[w] >> consumed;
|
U32 w;
|
||||||
} } }
|
for (w = 1; w < maxW+1; w++) {
|
||||||
|
rankValPtr[w] = rankVal0[w] >> consumed;
|
||||||
|
} } } }
|
||||||
|
|
||||||
HUF_fillDTableX4(dt, memLog,
|
HUF_fillDTableX4(dt, memLog,
|
||||||
sortedSymbol, sizeOfSort,
|
sortedSymbol, sizeOfSort,
|
||||||
|
@ -788,7 +790,7 @@ size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
|
||||||
|
|
||||||
/* Get start index of each weight */
|
/* Get start index of each weight */
|
||||||
{ U32 w, nextRankStart = 0;
|
{ U32 w, nextRankStart = 0;
|
||||||
for (w=1; w<=maxW; w++) {
|
for (w=1; w<maxW+1; w++) {
|
||||||
U32 current = nextRankStart;
|
U32 current = nextRankStart;
|
||||||
nextRankStart += rankStats[w];
|
nextRankStart += rankStats[w];
|
||||||
rankStart[w] = current;
|
rankStart[w] = current;
|
||||||
|
@ -814,14 +816,14 @@ size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
|
||||||
U32 w, consumed;
|
U32 w, consumed;
|
||||||
const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
|
const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
|
||||||
U32* rankVal0 = rankVal[0];
|
U32* rankVal0 = rankVal[0];
|
||||||
for (w=1; w<=maxW; w++) {
|
for (w=1; w<maxW+1; w++) {
|
||||||
U32 current = nextRankVal;
|
U32 current = nextRankVal;
|
||||||
nextRankVal += rankStats[w] << (w+rescale);
|
nextRankVal += rankStats[w] << (w+rescale);
|
||||||
rankVal0[w] = current;
|
rankVal0[w] = current;
|
||||||
}
|
}
|
||||||
for (consumed = minBits; consumed <= memLog - minBits; consumed++) {
|
for (consumed = minBits; consumed <= memLog - minBits; consumed++) {
|
||||||
U32* rankValPtr = rankVal[consumed];
|
U32* rankValPtr = rankVal[consumed];
|
||||||
for (w = 1; w <= maxW; w++) {
|
for (w = 1; w < maxW+1; w++) {
|
||||||
rankValPtr[w] = rankVal0[w] >> consumed;
|
rankValPtr[w] = rankVal0[w] >> consumed;
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue