1*3117ece4Schristos /* ****************************************************************** 2*3117ece4Schristos * FSE : Finite State Entropy decoder 3*3117ece4Schristos * Copyright (c) Meta Platforms, Inc. and affiliates. 4*3117ece4Schristos * 5*3117ece4Schristos * You can contact the author at : 6*3117ece4Schristos * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy 7*3117ece4Schristos * - Public forum : https://groups.google.com/forum/#!forum/lz4c 8*3117ece4Schristos * 9*3117ece4Schristos * This source code is licensed under both the BSD-style license (found in the 10*3117ece4Schristos * LICENSE file in the root directory of this source tree) and the GPLv2 (found 11*3117ece4Schristos * in the COPYING file in the root directory of this source tree). 12*3117ece4Schristos * You may select, at your option, one of the above-listed licenses. 13*3117ece4Schristos ****************************************************************** */ 14*3117ece4Schristos 15*3117ece4Schristos 16*3117ece4Schristos /* ************************************************************** 17*3117ece4Schristos * Includes 18*3117ece4Schristos ****************************************************************/ 19*3117ece4Schristos #include "debug.h" /* assert */ 20*3117ece4Schristos #include "bitstream.h" 21*3117ece4Schristos #include "compiler.h" 22*3117ece4Schristos #define FSE_STATIC_LINKING_ONLY 23*3117ece4Schristos #include "fse.h" 24*3117ece4Schristos #include "error_private.h" 25*3117ece4Schristos #include "zstd_deps.h" /* ZSTD_memcpy */ 26*3117ece4Schristos #include "bits.h" /* ZSTD_highbit32 */ 27*3117ece4Schristos 28*3117ece4Schristos 29*3117ece4Schristos /* ************************************************************** 30*3117ece4Schristos * Error Management 31*3117ece4Schristos ****************************************************************/ 32*3117ece4Schristos #define FSE_isError ERR_isError 33*3117ece4Schristos #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */ 34*3117ece4Schristos 35*3117ece4Schristos 36*3117ece4Schristos /* ************************************************************** 37*3117ece4Schristos * Templates 38*3117ece4Schristos ****************************************************************/ 39*3117ece4Schristos /* 40*3117ece4Schristos designed to be included 41*3117ece4Schristos for type-specific functions (template emulation in C) 42*3117ece4Schristos Objective is to write these functions only once, for improved maintenance 43*3117ece4Schristos */ 44*3117ece4Schristos 45*3117ece4Schristos /* safety checks */ 46*3117ece4Schristos #ifndef FSE_FUNCTION_EXTENSION 47*3117ece4Schristos # error "FSE_FUNCTION_EXTENSION must be defined" 48*3117ece4Schristos #endif 49*3117ece4Schristos #ifndef FSE_FUNCTION_TYPE 50*3117ece4Schristos # error "FSE_FUNCTION_TYPE must be defined" 51*3117ece4Schristos #endif 52*3117ece4Schristos 53*3117ece4Schristos /* Function names */ 54*3117ece4Schristos #define FSE_CAT(X,Y) X##Y 55*3117ece4Schristos #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) 56*3117ece4Schristos #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) 57*3117ece4Schristos 58*3117ece4Schristos static size_t FSE_buildDTable_internal(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize) 59*3117ece4Schristos { 60*3117ece4Schristos void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ 61*3117ece4Schristos FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); 62*3117ece4Schristos U16* symbolNext = (U16*)workSpace; 63*3117ece4Schristos BYTE* spread = (BYTE*)(symbolNext + maxSymbolValue + 1); 64*3117ece4Schristos 65*3117ece4Schristos U32 const maxSV1 = maxSymbolValue + 1; 66*3117ece4Schristos U32 const tableSize = 1 << tableLog; 67*3117ece4Schristos U32 highThreshold = tableSize-1; 68*3117ece4Schristos 69*3117ece4Schristos /* Sanity Checks */ 70*3117ece4Schristos if (FSE_BUILD_DTABLE_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(maxSymbolValue_tooLarge); 71*3117ece4Schristos if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge); 72*3117ece4Schristos if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); 73*3117ece4Schristos 74*3117ece4Schristos /* Init, lay down lowprob symbols */ 75*3117ece4Schristos { FSE_DTableHeader DTableH; 76*3117ece4Schristos DTableH.tableLog = (U16)tableLog; 77*3117ece4Schristos DTableH.fastMode = 1; 78*3117ece4Schristos { S16 const largeLimit= (S16)(1 << (tableLog-1)); 79*3117ece4Schristos U32 s; 80*3117ece4Schristos for (s=0; s<maxSV1; s++) { 81*3117ece4Schristos if (normalizedCounter[s]==-1) { 82*3117ece4Schristos tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; 83*3117ece4Schristos symbolNext[s] = 1; 84*3117ece4Schristos } else { 85*3117ece4Schristos if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0; 86*3117ece4Schristos symbolNext[s] = (U16)normalizedCounter[s]; 87*3117ece4Schristos } } } 88*3117ece4Schristos ZSTD_memcpy(dt, &DTableH, sizeof(DTableH)); 89*3117ece4Schristos } 90*3117ece4Schristos 91*3117ece4Schristos /* Spread symbols */ 92*3117ece4Schristos if (highThreshold == tableSize - 1) { 93*3117ece4Schristos size_t const tableMask = tableSize-1; 94*3117ece4Schristos size_t const step = FSE_TABLESTEP(tableSize); 95*3117ece4Schristos /* First lay down the symbols in order. 96*3117ece4Schristos * We use a uint64_t to lay down 8 bytes at a time. This reduces branch 97*3117ece4Schristos * misses since small blocks generally have small table logs, so nearly 98*3117ece4Schristos * all symbols have counts <= 8. We ensure we have 8 bytes at the end of 99*3117ece4Schristos * our buffer to handle the over-write. 100*3117ece4Schristos */ 101*3117ece4Schristos { U64 const add = 0x0101010101010101ull; 102*3117ece4Schristos size_t pos = 0; 103*3117ece4Schristos U64 sv = 0; 104*3117ece4Schristos U32 s; 105*3117ece4Schristos for (s=0; s<maxSV1; ++s, sv += add) { 106*3117ece4Schristos int i; 107*3117ece4Schristos int const n = normalizedCounter[s]; 108*3117ece4Schristos MEM_write64(spread + pos, sv); 109*3117ece4Schristos for (i = 8; i < n; i += 8) { 110*3117ece4Schristos MEM_write64(spread + pos + i, sv); 111*3117ece4Schristos } 112*3117ece4Schristos pos += (size_t)n; 113*3117ece4Schristos } } 114*3117ece4Schristos /* Now we spread those positions across the table. 115*3117ece4Schristos * The benefit of doing it in two stages is that we avoid the 116*3117ece4Schristos * variable size inner loop, which caused lots of branch misses. 117*3117ece4Schristos * Now we can run through all the positions without any branch misses. 118*3117ece4Schristos * We unroll the loop twice, since that is what empirically worked best. 119*3117ece4Schristos */ 120*3117ece4Schristos { 121*3117ece4Schristos size_t position = 0; 122*3117ece4Schristos size_t s; 123*3117ece4Schristos size_t const unroll = 2; 124*3117ece4Schristos assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */ 125*3117ece4Schristos for (s = 0; s < (size_t)tableSize; s += unroll) { 126*3117ece4Schristos size_t u; 127*3117ece4Schristos for (u = 0; u < unroll; ++u) { 128*3117ece4Schristos size_t const uPosition = (position + (u * step)) & tableMask; 129*3117ece4Schristos tableDecode[uPosition].symbol = spread[s + u]; 130*3117ece4Schristos } 131*3117ece4Schristos position = (position + (unroll * step)) & tableMask; 132*3117ece4Schristos } 133*3117ece4Schristos assert(position == 0); 134*3117ece4Schristos } 135*3117ece4Schristos } else { 136*3117ece4Schristos U32 const tableMask = tableSize-1; 137*3117ece4Schristos U32 const step = FSE_TABLESTEP(tableSize); 138*3117ece4Schristos U32 s, position = 0; 139*3117ece4Schristos for (s=0; s<maxSV1; s++) { 140*3117ece4Schristos int i; 141*3117ece4Schristos for (i=0; i<normalizedCounter[s]; i++) { 142*3117ece4Schristos tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; 143*3117ece4Schristos position = (position + step) & tableMask; 144*3117ece4Schristos while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ 145*3117ece4Schristos } } 146*3117ece4Schristos if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ 147*3117ece4Schristos } 148*3117ece4Schristos 149*3117ece4Schristos /* Build Decoding table */ 150*3117ece4Schristos { U32 u; 151*3117ece4Schristos for (u=0; u<tableSize; u++) { 152*3117ece4Schristos FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol); 153*3117ece4Schristos U32 const nextState = symbolNext[symbol]++; 154*3117ece4Schristos tableDecode[u].nbBits = (BYTE) (tableLog - ZSTD_highbit32(nextState) ); 155*3117ece4Schristos tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize); 156*3117ece4Schristos } } 157*3117ece4Schristos 158*3117ece4Schristos return 0; 159*3117ece4Schristos } 160*3117ece4Schristos 161*3117ece4Schristos size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize) 162*3117ece4Schristos { 163*3117ece4Schristos return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize); 164*3117ece4Schristos } 165*3117ece4Schristos 166*3117ece4Schristos 167*3117ece4Schristos #ifndef FSE_COMMONDEFS_ONLY 168*3117ece4Schristos 169*3117ece4Schristos /*-******************************************************* 170*3117ece4Schristos * Decompression (Byte symbols) 171*3117ece4Schristos *********************************************************/ 172*3117ece4Schristos 173*3117ece4Schristos FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic( 174*3117ece4Schristos void* dst, size_t maxDstSize, 175*3117ece4Schristos const void* cSrc, size_t cSrcSize, 176*3117ece4Schristos const FSE_DTable* dt, const unsigned fast) 177*3117ece4Schristos { 178*3117ece4Schristos BYTE* const ostart = (BYTE*) dst; 179*3117ece4Schristos BYTE* op = ostart; 180*3117ece4Schristos BYTE* const omax = op + maxDstSize; 181*3117ece4Schristos BYTE* const olimit = omax-3; 182*3117ece4Schristos 183*3117ece4Schristos BIT_DStream_t bitD; 184*3117ece4Schristos FSE_DState_t state1; 185*3117ece4Schristos FSE_DState_t state2; 186*3117ece4Schristos 187*3117ece4Schristos /* Init */ 188*3117ece4Schristos CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); 189*3117ece4Schristos 190*3117ece4Schristos FSE_initDState(&state1, &bitD, dt); 191*3117ece4Schristos FSE_initDState(&state2, &bitD, dt); 192*3117ece4Schristos 193*3117ece4Schristos #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) 194*3117ece4Schristos 195*3117ece4Schristos /* 4 symbols per loop */ 196*3117ece4Schristos for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) { 197*3117ece4Schristos op[0] = FSE_GETSYMBOL(&state1); 198*3117ece4Schristos 199*3117ece4Schristos if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ 200*3117ece4Schristos BIT_reloadDStream(&bitD); 201*3117ece4Schristos 202*3117ece4Schristos op[1] = FSE_GETSYMBOL(&state2); 203*3117ece4Schristos 204*3117ece4Schristos if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ 205*3117ece4Schristos { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } } 206*3117ece4Schristos 207*3117ece4Schristos op[2] = FSE_GETSYMBOL(&state1); 208*3117ece4Schristos 209*3117ece4Schristos if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ 210*3117ece4Schristos BIT_reloadDStream(&bitD); 211*3117ece4Schristos 212*3117ece4Schristos op[3] = FSE_GETSYMBOL(&state2); 213*3117ece4Schristos } 214*3117ece4Schristos 215*3117ece4Schristos /* tail */ 216*3117ece4Schristos /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ 217*3117ece4Schristos while (1) { 218*3117ece4Schristos if (op>(omax-2)) return ERROR(dstSize_tooSmall); 219*3117ece4Schristos *op++ = FSE_GETSYMBOL(&state1); 220*3117ece4Schristos if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { 221*3117ece4Schristos *op++ = FSE_GETSYMBOL(&state2); 222*3117ece4Schristos break; 223*3117ece4Schristos } 224*3117ece4Schristos 225*3117ece4Schristos if (op>(omax-2)) return ERROR(dstSize_tooSmall); 226*3117ece4Schristos *op++ = FSE_GETSYMBOL(&state2); 227*3117ece4Schristos if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { 228*3117ece4Schristos *op++ = FSE_GETSYMBOL(&state1); 229*3117ece4Schristos break; 230*3117ece4Schristos } } 231*3117ece4Schristos 232*3117ece4Schristos assert(op >= ostart); 233*3117ece4Schristos return (size_t)(op-ostart); 234*3117ece4Schristos } 235*3117ece4Schristos 236*3117ece4Schristos typedef struct { 237*3117ece4Schristos short ncount[FSE_MAX_SYMBOL_VALUE + 1]; 238*3117ece4Schristos } FSE_DecompressWksp; 239*3117ece4Schristos 240*3117ece4Schristos 241*3117ece4Schristos FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body( 242*3117ece4Schristos void* dst, size_t dstCapacity, 243*3117ece4Schristos const void* cSrc, size_t cSrcSize, 244*3117ece4Schristos unsigned maxLog, void* workSpace, size_t wkspSize, 245*3117ece4Schristos int bmi2) 246*3117ece4Schristos { 247*3117ece4Schristos const BYTE* const istart = (const BYTE*)cSrc; 248*3117ece4Schristos const BYTE* ip = istart; 249*3117ece4Schristos unsigned tableLog; 250*3117ece4Schristos unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; 251*3117ece4Schristos FSE_DecompressWksp* const wksp = (FSE_DecompressWksp*)workSpace; 252*3117ece4Schristos size_t const dtablePos = sizeof(FSE_DecompressWksp) / sizeof(FSE_DTable); 253*3117ece4Schristos FSE_DTable* const dtable = (FSE_DTable*)workSpace + dtablePos; 254*3117ece4Schristos 255*3117ece4Schristos FSE_STATIC_ASSERT((FSE_MAX_SYMBOL_VALUE + 1) % 2 == 0); 256*3117ece4Schristos if (wkspSize < sizeof(*wksp)) return ERROR(GENERIC); 257*3117ece4Schristos 258*3117ece4Schristos /* correct offset to dtable depends on this property */ 259*3117ece4Schristos FSE_STATIC_ASSERT(sizeof(FSE_DecompressWksp) % sizeof(FSE_DTable) == 0); 260*3117ece4Schristos 261*3117ece4Schristos /* normal FSE decoding mode */ 262*3117ece4Schristos { size_t const NCountLength = 263*3117ece4Schristos FSE_readNCount_bmi2(wksp->ncount, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2); 264*3117ece4Schristos if (FSE_isError(NCountLength)) return NCountLength; 265*3117ece4Schristos if (tableLog > maxLog) return ERROR(tableLog_tooLarge); 266*3117ece4Schristos assert(NCountLength <= cSrcSize); 267*3117ece4Schristos ip += NCountLength; 268*3117ece4Schristos cSrcSize -= NCountLength; 269*3117ece4Schristos } 270*3117ece4Schristos 271*3117ece4Schristos if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(tableLog_tooLarge); 272*3117ece4Schristos assert(sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog) <= wkspSize); 273*3117ece4Schristos workSpace = (BYTE*)workSpace + sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog); 274*3117ece4Schristos wkspSize -= sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog); 275*3117ece4Schristos 276*3117ece4Schristos CHECK_F( FSE_buildDTable_internal(dtable, wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize) ); 277*3117ece4Schristos 278*3117ece4Schristos { 279*3117ece4Schristos const void* ptr = dtable; 280*3117ece4Schristos const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr; 281*3117ece4Schristos const U32 fastMode = DTableH->fastMode; 282*3117ece4Schristos 283*3117ece4Schristos /* select fast mode (static) */ 284*3117ece4Schristos if (fastMode) return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, dtable, 1); 285*3117ece4Schristos return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, dtable, 0); 286*3117ece4Schristos } 287*3117ece4Schristos } 288*3117ece4Schristos 289*3117ece4Schristos /* Avoids the FORCE_INLINE of the _body() function. */ 290*3117ece4Schristos 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) 291*3117ece4Schristos { 292*3117ece4Schristos return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0); 293*3117ece4Schristos } 294*3117ece4Schristos 295*3117ece4Schristos #if DYNAMIC_BMI2 296*3117ece4Schristos BMI2_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) 297*3117ece4Schristos { 298*3117ece4Schristos return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1); 299*3117ece4Schristos } 300*3117ece4Schristos #endif 301*3117ece4Schristos 302*3117ece4Schristos 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) 303*3117ece4Schristos { 304*3117ece4Schristos #if DYNAMIC_BMI2 305*3117ece4Schristos if (bmi2) { 306*3117ece4Schristos return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize); 307*3117ece4Schristos } 308*3117ece4Schristos #endif 309*3117ece4Schristos (void)bmi2; 310*3117ece4Schristos return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize); 311*3117ece4Schristos } 312*3117ece4Schristos 313*3117ece4Schristos #endif /* FSE_COMMONDEFS_ONLY */ 314