Path: blob/master/Utilities/cmzstd/lib/common/fse_decompress.c
5021 views
/* ******************************************************************1* FSE : Finite State Entropy decoder2* Copyright (c) Meta Platforms, Inc. and affiliates.3*4* You can contact the author at :5* - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy6* - Public forum : https://groups.google.com/forum/#!forum/lz4c7*8* This source code is licensed under both the BSD-style license (found in the9* LICENSE file in the root directory of this source tree) and the GPLv2 (found10* in the COPYING file in the root directory of this source tree).11* You may select, at your option, one of the above-listed licenses.12****************************************************************** */131415/* **************************************************************16* Includes17****************************************************************/18#include "debug.h" /* assert */19#include "bitstream.h"20#include "compiler.h"21#define FSE_STATIC_LINKING_ONLY22#include "fse.h"23#include "error_private.h"24#include "zstd_deps.h" /* ZSTD_memcpy */25#include "bits.h" /* ZSTD_highbit32 */262728/* **************************************************************29* Error Management30****************************************************************/31#define FSE_isError ERR_isError32#define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */333435/* **************************************************************36* Templates37****************************************************************/38/*39designed to be included40for type-specific functions (template emulation in C)41Objective is to write these functions only once, for improved maintenance42*/4344/* safety checks */45#ifndef FSE_FUNCTION_EXTENSION46# error "FSE_FUNCTION_EXTENSION must be defined"47#endif48#ifndef FSE_FUNCTION_TYPE49# error "FSE_FUNCTION_TYPE must be defined"50#endif5152/* Function names */53#define FSE_CAT(X,Y) X##Y54#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)55#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)5657static size_t FSE_buildDTable_internal(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)58{59void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */60FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);61U16* symbolNext = (U16*)workSpace;62BYTE* spread = (BYTE*)(symbolNext + maxSymbolValue + 1);6364U32 const maxSV1 = maxSymbolValue + 1;65U32 const tableSize = 1 << tableLog;66U32 highThreshold = tableSize-1;6768/* Sanity Checks */69if (FSE_BUILD_DTABLE_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(maxSymbolValue_tooLarge);70if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);71if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);7273/* Init, lay down lowprob symbols */74{ FSE_DTableHeader DTableH;75DTableH.tableLog = (U16)tableLog;76DTableH.fastMode = 1;77{ S16 const largeLimit= (S16)(1 << (tableLog-1));78U32 s;79for (s=0; s<maxSV1; s++) {80if (normalizedCounter[s]==-1) {81tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;82symbolNext[s] = 1;83} else {84if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;85symbolNext[s] = (U16)normalizedCounter[s];86} } }87ZSTD_memcpy(dt, &DTableH, sizeof(DTableH));88}8990/* Spread symbols */91if (highThreshold == tableSize - 1) {92size_t const tableMask = tableSize-1;93size_t const step = FSE_TABLESTEP(tableSize);94/* First lay down the symbols in order.95* We use a uint64_t to lay down 8 bytes at a time. This reduces branch96* misses since small blocks generally have small table logs, so nearly97* all symbols have counts <= 8. We ensure we have 8 bytes at the end of98* our buffer to handle the over-write.99*/100{ U64 const add = 0x0101010101010101ull;101size_t pos = 0;102U64 sv = 0;103U32 s;104for (s=0; s<maxSV1; ++s, sv += add) {105int i;106int const n = normalizedCounter[s];107MEM_write64(spread + pos, sv);108for (i = 8; i < n; i += 8) {109MEM_write64(spread + pos + i, sv);110}111pos += (size_t)n;112} }113/* Now we spread those positions across the table.114* The benefit of doing it in two stages is that we avoid the115* variable size inner loop, which caused lots of branch misses.116* Now we can run through all the positions without any branch misses.117* We unroll the loop twice, since that is what empirically worked best.118*/119{120size_t position = 0;121size_t s;122size_t const unroll = 2;123assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */124for (s = 0; s < (size_t)tableSize; s += unroll) {125size_t u;126for (u = 0; u < unroll; ++u) {127size_t const uPosition = (position + (u * step)) & tableMask;128tableDecode[uPosition].symbol = spread[s + u];129}130position = (position + (unroll * step)) & tableMask;131}132assert(position == 0);133}134} else {135U32 const tableMask = tableSize-1;136U32 const step = FSE_TABLESTEP(tableSize);137U32 s, position = 0;138for (s=0; s<maxSV1; s++) {139int i;140for (i=0; i<normalizedCounter[s]; i++) {141tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;142position = (position + step) & tableMask;143while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */144} }145if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */146}147148/* Build Decoding table */149{ U32 u;150for (u=0; u<tableSize; u++) {151FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);152U32 const nextState = symbolNext[symbol]++;153tableDecode[u].nbBits = (BYTE) (tableLog - ZSTD_highbit32(nextState) );154tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);155} }156157return 0;158}159160size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)161{162return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize);163}164165166#ifndef FSE_COMMONDEFS_ONLY167168/*-*******************************************************169* Decompression (Byte symbols)170*********************************************************/171172FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(173void* dst, size_t maxDstSize,174const void* cSrc, size_t cSrcSize,175const FSE_DTable* dt, const unsigned fast)176{177BYTE* const ostart = (BYTE*) dst;178BYTE* op = ostart;179BYTE* const omax = op + maxDstSize;180BYTE* const olimit = omax-3;181182BIT_DStream_t bitD;183FSE_DState_t state1;184FSE_DState_t state2;185186/* Init */187CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));188189FSE_initDState(&state1, &bitD, dt);190FSE_initDState(&state2, &bitD, dt);191192RETURN_ERROR_IF(BIT_reloadDStream(&bitD)==BIT_DStream_overflow, corruption_detected, "");193194#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)195196/* 4 symbols per loop */197for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {198op[0] = FSE_GETSYMBOL(&state1);199200if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */201BIT_reloadDStream(&bitD);202203op[1] = FSE_GETSYMBOL(&state2);204205if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */206{ if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }207208op[2] = FSE_GETSYMBOL(&state1);209210if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */211BIT_reloadDStream(&bitD);212213op[3] = FSE_GETSYMBOL(&state2);214}215216/* tail */217/* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */218while (1) {219if (op>(omax-2)) return ERROR(dstSize_tooSmall);220*op++ = FSE_GETSYMBOL(&state1);221if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {222*op++ = FSE_GETSYMBOL(&state2);223break;224}225226if (op>(omax-2)) return ERROR(dstSize_tooSmall);227*op++ = FSE_GETSYMBOL(&state2);228if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {229*op++ = FSE_GETSYMBOL(&state1);230break;231} }232233assert(op >= ostart);234return (size_t)(op-ostart);235}236237typedef struct {238short ncount[FSE_MAX_SYMBOL_VALUE + 1];239} FSE_DecompressWksp;240241242FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body(243void* dst, size_t dstCapacity,244const void* cSrc, size_t cSrcSize,245unsigned maxLog, void* workSpace, size_t wkspSize,246int bmi2)247{248const BYTE* const istart = (const BYTE*)cSrc;249const BYTE* ip = istart;250unsigned tableLog;251unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;252FSE_DecompressWksp* const wksp = (FSE_DecompressWksp*)workSpace;253size_t const dtablePos = sizeof(FSE_DecompressWksp) / sizeof(FSE_DTable);254FSE_DTable* const dtable = (FSE_DTable*)workSpace + dtablePos;255256FSE_STATIC_ASSERT((FSE_MAX_SYMBOL_VALUE + 1) % 2 == 0);257if (wkspSize < sizeof(*wksp)) return ERROR(GENERIC);258259/* correct offset to dtable depends on this property */260FSE_STATIC_ASSERT(sizeof(FSE_DecompressWksp) % sizeof(FSE_DTable) == 0);261262/* normal FSE decoding mode */263{ size_t const NCountLength =264FSE_readNCount_bmi2(wksp->ncount, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2);265if (FSE_isError(NCountLength)) return NCountLength;266if (tableLog > maxLog) return ERROR(tableLog_tooLarge);267assert(NCountLength <= cSrcSize);268ip += NCountLength;269cSrcSize -= NCountLength;270}271272if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(tableLog_tooLarge);273assert(sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog) <= wkspSize);274workSpace = (BYTE*)workSpace + sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);275wkspSize -= sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);276277CHECK_F( FSE_buildDTable_internal(dtable, wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize) );278279{280const void* ptr = dtable;281const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;282const U32 fastMode = DTableH->fastMode;283284/* select fast mode (static) */285if (fastMode) return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, dtable, 1);286return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, dtable, 0);287}288}289290/* Avoids the FORCE_INLINE of the _body() function. */291static size_t FSE_decompress_wksp_body_default(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize)292{293return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0);294}295296#if DYNAMIC_BMI2297BMI2_TARGET_ATTRIBUTE static size_t FSE_decompress_wksp_body_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize)298{299return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1);300}301#endif302303size_t FSE_decompress_wksp_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize, int bmi2)304{305#if DYNAMIC_BMI2306if (bmi2) {307return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);308}309#endif310(void)bmi2;311return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);312}313314#endif /* FSE_COMMONDEFS_ONLY */315316317