Path: blob/master/Utilities/cmzstd/lib/common/fse_decompress.c
3158 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#define ZSTD_DEPS_NEED_MALLOC25#include "zstd_deps.h"26#include "bits.h" /* ZSTD_highbit32 */272829/* **************************************************************30* Error Management31****************************************************************/32#define FSE_isError ERR_isError33#define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */343536/* **************************************************************37* Templates38****************************************************************/39/*40designed to be included41for type-specific functions (template emulation in C)42Objective is to write these functions only once, for improved maintenance43*/4445/* safety checks */46#ifndef FSE_FUNCTION_EXTENSION47# error "FSE_FUNCTION_EXTENSION must be defined"48#endif49#ifndef FSE_FUNCTION_TYPE50# error "FSE_FUNCTION_TYPE must be defined"51#endif5253/* Function names */54#define FSE_CAT(X,Y) X##Y55#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)56#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)5758static size_t FSE_buildDTable_internal(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)59{60void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */61FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);62U16* symbolNext = (U16*)workSpace;63BYTE* spread = (BYTE*)(symbolNext + maxSymbolValue + 1);6465U32 const maxSV1 = maxSymbolValue + 1;66U32 const tableSize = 1 << tableLog;67U32 highThreshold = tableSize-1;6869/* Sanity Checks */70if (FSE_BUILD_DTABLE_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(maxSymbolValue_tooLarge);71if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);72if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);7374/* Init, lay down lowprob symbols */75{ FSE_DTableHeader DTableH;76DTableH.tableLog = (U16)tableLog;77DTableH.fastMode = 1;78{ S16 const largeLimit= (S16)(1 << (tableLog-1));79U32 s;80for (s=0; s<maxSV1; s++) {81if (normalizedCounter[s]==-1) {82tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;83symbolNext[s] = 1;84} else {85if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;86symbolNext[s] = normalizedCounter[s];87} } }88ZSTD_memcpy(dt, &DTableH, sizeof(DTableH));89}9091/* Spread symbols */92if (highThreshold == tableSize - 1) {93size_t const tableMask = tableSize-1;94size_t const step = FSE_TABLESTEP(tableSize);95/* First lay down the symbols in order.96* We use a uint64_t to lay down 8 bytes at a time. This reduces branch97* misses since small blocks generally have small table logs, so nearly98* all symbols have counts <= 8. We ensure we have 8 bytes at the end of99* our buffer to handle the over-write.100*/101{102U64 const add = 0x0101010101010101ull;103size_t pos = 0;104U64 sv = 0;105U32 s;106for (s=0; s<maxSV1; ++s, sv += add) {107int i;108int const n = normalizedCounter[s];109MEM_write64(spread + pos, sv);110for (i = 8; i < n; i += 8) {111MEM_write64(spread + pos + i, sv);112}113pos += n;114}115}116/* Now we spread those positions across the table.117* The benefit of doing it in two stages is that we avoid the118* variable size inner loop, which caused lots of branch misses.119* Now we can run through all the positions without any branch misses.120* We unroll the loop twice, since that is what empirically worked best.121*/122{123size_t position = 0;124size_t s;125size_t const unroll = 2;126assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */127for (s = 0; s < (size_t)tableSize; s += unroll) {128size_t u;129for (u = 0; u < unroll; ++u) {130size_t const uPosition = (position + (u * step)) & tableMask;131tableDecode[uPosition].symbol = spread[s + u];132}133position = (position + (unroll * step)) & tableMask;134}135assert(position == 0);136}137} else {138U32 const tableMask = tableSize-1;139U32 const step = FSE_TABLESTEP(tableSize);140U32 s, position = 0;141for (s=0; s<maxSV1; s++) {142int i;143for (i=0; i<normalizedCounter[s]; i++) {144tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;145position = (position + step) & tableMask;146while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */147} }148if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */149}150151/* Build Decoding table */152{ U32 u;153for (u=0; u<tableSize; u++) {154FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);155U32 const nextState = symbolNext[symbol]++;156tableDecode[u].nbBits = (BYTE) (tableLog - ZSTD_highbit32(nextState) );157tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);158} }159160return 0;161}162163size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)164{165return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize);166}167168169#ifndef FSE_COMMONDEFS_ONLY170171/*-*******************************************************172* Decompression (Byte symbols)173*********************************************************/174175FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(176void* dst, size_t maxDstSize,177const void* cSrc, size_t cSrcSize,178const FSE_DTable* dt, const unsigned fast)179{180BYTE* const ostart = (BYTE*) dst;181BYTE* op = ostart;182BYTE* const omax = op + maxDstSize;183BYTE* const olimit = omax-3;184185BIT_DStream_t bitD;186FSE_DState_t state1;187FSE_DState_t state2;188189/* Init */190CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));191192FSE_initDState(&state1, &bitD, dt);193FSE_initDState(&state2, &bitD, dt);194195#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)196197/* 4 symbols per loop */198for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {199op[0] = FSE_GETSYMBOL(&state1);200201if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */202BIT_reloadDStream(&bitD);203204op[1] = FSE_GETSYMBOL(&state2);205206if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */207{ if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }208209op[2] = FSE_GETSYMBOL(&state1);210211if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */212BIT_reloadDStream(&bitD);213214op[3] = FSE_GETSYMBOL(&state2);215}216217/* tail */218/* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */219while (1) {220if (op>(omax-2)) return ERROR(dstSize_tooSmall);221*op++ = FSE_GETSYMBOL(&state1);222if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {223*op++ = FSE_GETSYMBOL(&state2);224break;225}226227if (op>(omax-2)) return ERROR(dstSize_tooSmall);228*op++ = FSE_GETSYMBOL(&state2);229if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {230*op++ = FSE_GETSYMBOL(&state1);231break;232} }233234return op-ostart;235}236237typedef struct {238short ncount[FSE_MAX_SYMBOL_VALUE + 1];239FSE_DTable dtable[1]; /* Dynamically sized */240} FSE_DecompressWksp;241242243FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body(244void* dst, size_t dstCapacity,245const void* cSrc, size_t cSrcSize,246unsigned maxLog, void* workSpace, size_t wkspSize,247int bmi2)248{249const BYTE* const istart = (const BYTE*)cSrc;250const BYTE* ip = istart;251unsigned tableLog;252unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;253FSE_DecompressWksp* const wksp = (FSE_DecompressWksp*)workSpace;254255DEBUG_STATIC_ASSERT((FSE_MAX_SYMBOL_VALUE + 1) % 2 == 0);256if (wkspSize < sizeof(*wksp)) return ERROR(GENERIC);257258/* normal FSE decoding mode */259{260size_t const NCountLength = FSE_readNCount_bmi2(wksp->ncount, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2);261if (FSE_isError(NCountLength)) return NCountLength;262if (tableLog > maxLog) return ERROR(tableLog_tooLarge);263assert(NCountLength <= cSrcSize);264ip += NCountLength;265cSrcSize -= NCountLength;266}267268if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(tableLog_tooLarge);269assert(sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog) <= wkspSize);270workSpace = (BYTE*)workSpace + sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);271wkspSize -= sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);272273CHECK_F( FSE_buildDTable_internal(wksp->dtable, wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize) );274275{276const void* ptr = wksp->dtable;277const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;278const U32 fastMode = DTableH->fastMode;279280/* select fast mode (static) */281if (fastMode) return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 1);282return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 0);283}284}285286/* Avoids the FORCE_INLINE of the _body() function. */287static 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)288{289return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0);290}291292#if DYNAMIC_BMI2293BMI2_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)294{295return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1);296}297#endif298299size_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)300{301#if DYNAMIC_BMI2302if (bmi2) {303return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);304}305#endif306(void)bmi2;307return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);308}309310#endif /* FSE_COMMONDEFS_ONLY */311312313