0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include "debug.h" /* assert */
0020 #include "bitstream.h"
0021 #include "compiler.h"
0022 #define FSE_STATIC_LINKING_ONLY
0023 #include "fse.h"
0024 #include "error_private.h"
0025 #define ZSTD_DEPS_NEED_MALLOC
0026 #include "zstd_deps.h"
0027
0028
0029
0030
0031
0032 #define FSE_isError ERR_isError
0033 #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 #ifndef FSE_FUNCTION_EXTENSION
0047 # error "FSE_FUNCTION_EXTENSION must be defined"
0048 #endif
0049 #ifndef FSE_FUNCTION_TYPE
0050 # error "FSE_FUNCTION_TYPE must be defined"
0051 #endif
0052
0053
0054 #define FSE_CAT(X,Y) X##Y
0055 #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
0056 #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
0057
0058
0059
0060 FSE_DTable* FSE_createDTable (unsigned tableLog)
0061 {
0062 if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
0063 return (FSE_DTable*)ZSTD_malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32) );
0064 }
0065
0066 void FSE_freeDTable (FSE_DTable* dt)
0067 {
0068 ZSTD_free(dt);
0069 }
0070
0071 static size_t FSE_buildDTable_internal(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
0072 {
0073 void* const tdPtr = dt+1;
0074 FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);
0075 U16* symbolNext = (U16*)workSpace;
0076 BYTE* spread = (BYTE*)(symbolNext + maxSymbolValue + 1);
0077
0078 U32 const maxSV1 = maxSymbolValue + 1;
0079 U32 const tableSize = 1 << tableLog;
0080 U32 highThreshold = tableSize-1;
0081
0082
0083 if (FSE_BUILD_DTABLE_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(maxSymbolValue_tooLarge);
0084 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
0085 if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
0086
0087
0088 { FSE_DTableHeader DTableH;
0089 DTableH.tableLog = (U16)tableLog;
0090 DTableH.fastMode = 1;
0091 { S16 const largeLimit= (S16)(1 << (tableLog-1));
0092 U32 s;
0093 for (s=0; s<maxSV1; s++) {
0094 if (normalizedCounter[s]==-1) {
0095 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
0096 symbolNext[s] = 1;
0097 } else {
0098 if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
0099 symbolNext[s] = normalizedCounter[s];
0100 } } }
0101 ZSTD_memcpy(dt, &DTableH, sizeof(DTableH));
0102 }
0103
0104
0105 if (highThreshold == tableSize - 1) {
0106 size_t const tableMask = tableSize-1;
0107 size_t const step = FSE_TABLESTEP(tableSize);
0108
0109
0110
0111
0112
0113
0114 {
0115 U64 const add = 0x0101010101010101ull;
0116 size_t pos = 0;
0117 U64 sv = 0;
0118 U32 s;
0119 for (s=0; s<maxSV1; ++s, sv += add) {
0120 int i;
0121 int const n = normalizedCounter[s];
0122 MEM_write64(spread + pos, sv);
0123 for (i = 8; i < n; i += 8) {
0124 MEM_write64(spread + pos + i, sv);
0125 }
0126 pos += n;
0127 }
0128 }
0129
0130
0131
0132
0133
0134
0135 {
0136 size_t position = 0;
0137 size_t s;
0138 size_t const unroll = 2;
0139 assert(tableSize % unroll == 0);
0140 for (s = 0; s < (size_t)tableSize; s += unroll) {
0141 size_t u;
0142 for (u = 0; u < unroll; ++u) {
0143 size_t const uPosition = (position + (u * step)) & tableMask;
0144 tableDecode[uPosition].symbol = spread[s + u];
0145 }
0146 position = (position + (unroll * step)) & tableMask;
0147 }
0148 assert(position == 0);
0149 }
0150 } else {
0151 U32 const tableMask = tableSize-1;
0152 U32 const step = FSE_TABLESTEP(tableSize);
0153 U32 s, position = 0;
0154 for (s=0; s<maxSV1; s++) {
0155 int i;
0156 for (i=0; i<normalizedCounter[s]; i++) {
0157 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
0158 position = (position + step) & tableMask;
0159 while (position > highThreshold) position = (position + step) & tableMask;
0160 } }
0161 if (position!=0) return ERROR(GENERIC);
0162 }
0163
0164
0165 { U32 u;
0166 for (u=0; u<tableSize; u++) {
0167 FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
0168 U32 const nextState = symbolNext[symbol]++;
0169 tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
0170 tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
0171 } }
0172
0173 return 0;
0174 }
0175
0176 size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
0177 {
0178 return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize);
0179 }
0180
0181
0182 #ifndef FSE_COMMONDEFS_ONLY
0183
0184
0185
0186
0187 size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
0188 {
0189 void* ptr = dt;
0190 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
0191 void* dPtr = dt + 1;
0192 FSE_decode_t* const cell = (FSE_decode_t*)dPtr;
0193
0194 DTableH->tableLog = 0;
0195 DTableH->fastMode = 0;
0196
0197 cell->newState = 0;
0198 cell->symbol = symbolValue;
0199 cell->nbBits = 0;
0200
0201 return 0;
0202 }
0203
0204
0205 size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
0206 {
0207 void* ptr = dt;
0208 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
0209 void* dPtr = dt + 1;
0210 FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;
0211 const unsigned tableSize = 1 << nbBits;
0212 const unsigned tableMask = tableSize - 1;
0213 const unsigned maxSV1 = tableMask+1;
0214 unsigned s;
0215
0216
0217 if (nbBits < 1) return ERROR(GENERIC);
0218
0219
0220 DTableH->tableLog = (U16)nbBits;
0221 DTableH->fastMode = 1;
0222 for (s=0; s<maxSV1; s++) {
0223 dinfo[s].newState = 0;
0224 dinfo[s].symbol = (BYTE)s;
0225 dinfo[s].nbBits = (BYTE)nbBits;
0226 }
0227
0228 return 0;
0229 }
0230
0231 FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(
0232 void* dst, size_t maxDstSize,
0233 const void* cSrc, size_t cSrcSize,
0234 const FSE_DTable* dt, const unsigned fast)
0235 {
0236 BYTE* const ostart = (BYTE*) dst;
0237 BYTE* op = ostart;
0238 BYTE* const omax = op + maxDstSize;
0239 BYTE* const olimit = omax-3;
0240
0241 BIT_DStream_t bitD;
0242 FSE_DState_t state1;
0243 FSE_DState_t state2;
0244
0245
0246 CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
0247
0248 FSE_initDState(&state1, &bitD, dt);
0249 FSE_initDState(&state2, &bitD, dt);
0250
0251 #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
0252
0253
0254 for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {
0255 op[0] = FSE_GETSYMBOL(&state1);
0256
0257 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8)
0258 BIT_reloadDStream(&bitD);
0259
0260 op[1] = FSE_GETSYMBOL(&state2);
0261
0262 if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8)
0263 { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
0264
0265 op[2] = FSE_GETSYMBOL(&state1);
0266
0267 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8)
0268 BIT_reloadDStream(&bitD);
0269
0270 op[3] = FSE_GETSYMBOL(&state2);
0271 }
0272
0273
0274
0275 while (1) {
0276 if (op>(omax-2)) return ERROR(dstSize_tooSmall);
0277 *op++ = FSE_GETSYMBOL(&state1);
0278 if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
0279 *op++ = FSE_GETSYMBOL(&state2);
0280 break;
0281 }
0282
0283 if (op>(omax-2)) return ERROR(dstSize_tooSmall);
0284 *op++ = FSE_GETSYMBOL(&state2);
0285 if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
0286 *op++ = FSE_GETSYMBOL(&state1);
0287 break;
0288 } }
0289
0290 return op-ostart;
0291 }
0292
0293
0294 size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
0295 const void* cSrc, size_t cSrcSize,
0296 const FSE_DTable* dt)
0297 {
0298 const void* ptr = dt;
0299 const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;
0300 const U32 fastMode = DTableH->fastMode;
0301
0302
0303 if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
0304 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
0305 }
0306
0307
0308 size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize)
0309 {
0310 return FSE_decompress_wksp_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0);
0311 }
0312
0313 typedef struct {
0314 short ncount[FSE_MAX_SYMBOL_VALUE + 1];
0315 FSE_DTable dtable[1];
0316 } FSE_DecompressWksp;
0317
0318
0319 FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body(
0320 void* dst, size_t dstCapacity,
0321 const void* cSrc, size_t cSrcSize,
0322 unsigned maxLog, void* workSpace, size_t wkspSize,
0323 int bmi2)
0324 {
0325 const BYTE* const istart = (const BYTE*)cSrc;
0326 const BYTE* ip = istart;
0327 unsigned tableLog;
0328 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
0329 FSE_DecompressWksp* const wksp = (FSE_DecompressWksp*)workSpace;
0330
0331 DEBUG_STATIC_ASSERT((FSE_MAX_SYMBOL_VALUE + 1) % 2 == 0);
0332 if (wkspSize < sizeof(*wksp)) return ERROR(GENERIC);
0333
0334
0335 {
0336 size_t const NCountLength = FSE_readNCount_bmi2(wksp->ncount, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2);
0337 if (FSE_isError(NCountLength)) return NCountLength;
0338 if (tableLog > maxLog) return ERROR(tableLog_tooLarge);
0339 assert(NCountLength <= cSrcSize);
0340 ip += NCountLength;
0341 cSrcSize -= NCountLength;
0342 }
0343
0344 if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(tableLog_tooLarge);
0345 workSpace = wksp->dtable + FSE_DTABLE_SIZE_U32(tableLog);
0346 wkspSize -= sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);
0347
0348 CHECK_F( FSE_buildDTable_internal(wksp->dtable, wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize) );
0349
0350 {
0351 const void* ptr = wksp->dtable;
0352 const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;
0353 const U32 fastMode = DTableH->fastMode;
0354
0355
0356 if (fastMode) return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 1);
0357 return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 0);
0358 }
0359 }
0360
0361
0362 static 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)
0363 {
0364 return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0);
0365 }
0366
0367 #if DYNAMIC_BMI2
0368 TARGET_ATTRIBUTE("bmi2") 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)
0369 {
0370 return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1);
0371 }
0372 #endif
0373
0374 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)
0375 {
0376 #if DYNAMIC_BMI2
0377 if (bmi2) {
0378 return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
0379 }
0380 #endif
0381 (void)bmi2;
0382 return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
0383 }
0384
0385
0386 typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
0387
0388
0389
0390 #endif