22#define FSE_STATIC_LINKING_ONLY
32#define FSE_isError ERR_isError
33#define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
46#ifndef FSE_FUNCTION_EXTENSION
47# error "FSE_FUNCTION_EXTENSION must be defined"
49#ifndef FSE_FUNCTION_TYPE
50# error "FSE_FUNCTION_TYPE must be defined"
54#define FSE_CAT(X,Y) X##Y
55#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
56#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
58static size_t FSE_buildDTable_internal(
FSE_DTable* dt,
const short* normalizedCounter,
unsigned maxSymbolValue,
unsigned tableLog,
void* workSpace,
size_t wkspSize)
60 void*
const tdPtr = dt+1;
61 FSE_DECODE_TYPE*
const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);
62 U16* symbolNext = (
U16*)workSpace;
63 BYTE* spread = (
BYTE*)(symbolNext + maxSymbolValue + 1);
65 U32 const maxSV1 = maxSymbolValue + 1;
66 U32 const tableSize = 1 << tableLog;
67 U32 highThreshold = tableSize-1;
70 if (FSE_BUILD_DTABLE_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize)
return ERROR(maxSymbolValue_tooLarge);
71 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE)
return ERROR(maxSymbolValue_tooLarge);
72 if (tableLog > FSE_MAX_TABLELOG)
return ERROR(tableLog_tooLarge);
75 { FSE_DTableHeader DTableH;
76 DTableH.tableLog = (
U16)tableLog;
78 {
S16 const largeLimit= (
S16)(1 << (tableLog-1));
80 for (s=0; s<maxSV1; s++) {
81 if (normalizedCounter[s]==-1) {
82 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
85 if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
86 symbolNext[s] = (
U16)normalizedCounter[s];
92 if (highThreshold == tableSize - 1) {
93 size_t const tableMask = tableSize-1;
94 size_t const step = FSE_TABLESTEP(tableSize);
101 {
U64 const add = 0x0101010101010101ull;
105 for (s=0; s<maxSV1; ++s, sv += add) {
107 int const n = normalizedCounter[s];
109 for (i = 8; i < n; i += 8) {
123 size_t const unroll = 2;
124 assert(tableSize % unroll == 0);
125 for (s = 0; s < (size_t)tableSize; s += unroll) {
127 for (u = 0; u < unroll; ++u) {
128 size_t const uPosition = (position + (u * step)) & tableMask;
129 tableDecode[uPosition].symbol = spread[s + u];
131 position = (position + (unroll * step)) & tableMask;
136 U32 const tableMask = tableSize-1;
137 U32 const step = FSE_TABLESTEP(tableSize);
139 for (s=0; s<maxSV1; s++) {
141 for (i=0; i<normalizedCounter[s]; i++) {
142 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
143 position = (position + step) & tableMask;
144 while (position > highThreshold) position = (position + step) & tableMask;
146 if (position!=0)
return ERROR(GENERIC);
151 for (u=0; u<tableSize; u++) {
152 FSE_FUNCTION_TYPE
const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
153 U32 const nextState = symbolNext[symbol]++;
155 tableDecode[u].newState = (
U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
163 return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize);
167#ifndef FSE_COMMONDEFS_ONLY
174 void* dst,
size_t maxDstSize,
175 const void* cSrc,
size_t cSrcSize,
180 BYTE*
const omax = op + maxDstSize;
181 BYTE*
const olimit = omax-3;
190 FSE_initDState(&state1, &bitD, dt);
191 FSE_initDState(&state2, &bitD, dt);
193#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
218 if (op>(omax-2))
return ERROR(dstSize_tooSmall);
225 if (op>(omax-2))
return ERROR(dstSize_tooSmall);
233 return (
size_t)(op-ostart);
237 short ncount[FSE_MAX_SYMBOL_VALUE + 1];
242 void* dst,
size_t dstCapacity,
243 const void* cSrc,
size_t cSrcSize,
244 unsigned maxLog,
void* workSpace,
size_t wkspSize,
247 const BYTE*
const istart = (
const BYTE*)cSrc;
248 const BYTE* ip = istart;
250 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
256 if (wkspSize <
sizeof(*wksp))
return ERROR(GENERIC);
262 {
size_t const NCountLength =
264 if (
FSE_isError(NCountLength))
return NCountLength;
265 if (tableLog > maxLog)
return ERROR(tableLog_tooLarge);
266 assert(NCountLength <= cSrcSize);
268 cSrcSize -= NCountLength;
271 if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize)
return ERROR(tableLog_tooLarge);
272 assert(
sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog) <= wkspSize);
273 workSpace = (
BYTE*)workSpace +
sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);
274 wkspSize -=
sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);
276 CHECK_F( FSE_buildDTable_internal(dtable, wksp->
ncount, maxSymbolValue, tableLog, workSpace, wkspSize) );
279 const void* ptr = dtable;
280 const FSE_DTableHeader* DTableH = (
const FSE_DTableHeader*)ptr;
281 const U32 fastMode = DTableH->fastMode;
290static 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)
296BMI2_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)
302size_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)
306 return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
310 return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
MEM_STATIC unsigned ZSTD_highbit32(U32 val)
MEM_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize)
MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD)
#define BMI2_TARGET_ATTRIBUTE
#define FORCE_INLINE_TEMPLATE
#define assert(condition)
size_t FSE_readNCount_bmi2(short *normalizedCounter, unsigned *maxSVPtr, unsigned *tableLogPtr, const void *headerBuffer, size_t hbSize, int bmi2)
#define FSE_GETSYMBOL(statePtr)
size_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)
FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workSpace, size_t wkspSize, int bmi2)
FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt, const unsigned fast)
size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize)
#define FSE_STATIC_ASSERT(c)
MEM_STATIC void MEM_write64(void *memPtr, U64 value)
BitContainerType bitContainer
short ncount[FSE_MAX_SYMBOL_VALUE+1]
#define ZSTD_memcpy(d, s, l)