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