xref: /freebsd/sys/contrib/openzfs/module/zstd/lib/common/huf.h (revision c03c5b1c80914ec656fbee84539355d1fad68bf9)
1*c03c5b1cSMartin Matuska /* ******************************************************************
2*c03c5b1cSMartin Matuska  * huff0 huffman codec,
3*c03c5b1cSMartin Matuska  * part of Finite State Entropy library
4*c03c5b1cSMartin Matuska  * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
5*c03c5b1cSMartin Matuska  *
6*c03c5b1cSMartin Matuska  * You can contact the author at :
7*c03c5b1cSMartin Matuska  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8*c03c5b1cSMartin Matuska  *
9*c03c5b1cSMartin Matuska  * This source code is licensed under both the BSD-style license (found in the
10*c03c5b1cSMartin Matuska  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11*c03c5b1cSMartin Matuska  * in the COPYING file in the root directory of this source tree).
12*c03c5b1cSMartin Matuska  * You may select, at your option, one of the above-listed licenses.
13*c03c5b1cSMartin Matuska ****************************************************************** */
14*c03c5b1cSMartin Matuska 
15*c03c5b1cSMartin Matuska #if defined (__cplusplus)
16*c03c5b1cSMartin Matuska extern "C" {
17*c03c5b1cSMartin Matuska #endif
18*c03c5b1cSMartin Matuska 
19*c03c5b1cSMartin Matuska #ifndef HUF_H_298734234
20*c03c5b1cSMartin Matuska #define HUF_H_298734234
21*c03c5b1cSMartin Matuska 
22*c03c5b1cSMartin Matuska /* *** Dependencies *** */
23*c03c5b1cSMartin Matuska #include <stddef.h>    /* size_t */
24*c03c5b1cSMartin Matuska 
25*c03c5b1cSMartin Matuska 
26*c03c5b1cSMartin Matuska /* *** library symbols visibility *** */
27*c03c5b1cSMartin Matuska /* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual,
28*c03c5b1cSMartin Matuska  *        HUF symbols remain "private" (internal symbols for library only).
29*c03c5b1cSMartin Matuska  *        Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */
30*c03c5b1cSMartin Matuska #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
31*c03c5b1cSMartin Matuska #  define HUF_PUBLIC_API __attribute__ ((visibility ("default")))
32*c03c5b1cSMartin Matuska #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1)   /* Visual expected */
33*c03c5b1cSMartin Matuska #  define HUF_PUBLIC_API __declspec(dllexport)
34*c03c5b1cSMartin Matuska #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
35*c03c5b1cSMartin Matuska #  define HUF_PUBLIC_API __declspec(dllimport)  /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */
36*c03c5b1cSMartin Matuska #else
37*c03c5b1cSMartin Matuska #  define HUF_PUBLIC_API
38*c03c5b1cSMartin Matuska #endif
39*c03c5b1cSMartin Matuska 
40*c03c5b1cSMartin Matuska 
41*c03c5b1cSMartin Matuska /* ========================== */
42*c03c5b1cSMartin Matuska /* ***  simple functions  *** */
43*c03c5b1cSMartin Matuska /* ========================== */
44*c03c5b1cSMartin Matuska 
45*c03c5b1cSMartin Matuska /** HUF_compress() :
46*c03c5b1cSMartin Matuska  *  Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.
47*c03c5b1cSMartin Matuska  * 'dst' buffer must be already allocated.
48*c03c5b1cSMartin Matuska  *  Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).
49*c03c5b1cSMartin Matuska  * `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.
50*c03c5b1cSMartin Matuska  * @return : size of compressed data (<= `dstCapacity`).
51*c03c5b1cSMartin Matuska  *  Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
52*c03c5b1cSMartin Matuska  *                   if HUF_isError(return), compression failed (more details using HUF_getErrorName())
53*c03c5b1cSMartin Matuska  */
54*c03c5b1cSMartin Matuska HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,
55*c03c5b1cSMartin Matuska                              const void* src, size_t srcSize);
56*c03c5b1cSMartin Matuska 
57*c03c5b1cSMartin Matuska /** HUF_decompress() :
58*c03c5b1cSMartin Matuska  *  Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
59*c03c5b1cSMartin Matuska  *  into already allocated buffer 'dst', of minimum size 'dstSize'.
60*c03c5b1cSMartin Matuska  * `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.
61*c03c5b1cSMartin Matuska  *  Note : in contrast with FSE, HUF_decompress can regenerate
62*c03c5b1cSMartin Matuska  *         RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
63*c03c5b1cSMartin Matuska  *         because it knows size to regenerate (originalSize).
64*c03c5b1cSMartin Matuska  * @return : size of regenerated data (== originalSize),
65*c03c5b1cSMartin Matuska  *           or an error code, which can be tested using HUF_isError()
66*c03c5b1cSMartin Matuska  */
67*c03c5b1cSMartin Matuska HUF_PUBLIC_API size_t HUF_decompress(void* dst,  size_t originalSize,
68*c03c5b1cSMartin Matuska                                const void* cSrc, size_t cSrcSize);
69*c03c5b1cSMartin Matuska 
70*c03c5b1cSMartin Matuska 
71*c03c5b1cSMartin Matuska /* ***   Tool functions *** */
72*c03c5b1cSMartin Matuska #define HUF_BLOCKSIZE_MAX (128 * 1024)                  /**< maximum input size for a single block compressed with HUF_compress */
73*c03c5b1cSMartin Matuska HUF_PUBLIC_API size_t HUF_compressBound(size_t size);   /**< maximum compressed size (worst case) */
74*c03c5b1cSMartin Matuska 
75*c03c5b1cSMartin Matuska /* Error Management */
76*c03c5b1cSMartin Matuska HUF_PUBLIC_API unsigned    HUF_isError(size_t code);       /**< tells if a return value is an error code */
77*c03c5b1cSMartin Matuska HUF_PUBLIC_API const char* HUF_getErrorName(size_t code);  /**< provides error code string (useful for debugging) */
78*c03c5b1cSMartin Matuska 
79*c03c5b1cSMartin Matuska 
80*c03c5b1cSMartin Matuska /* ***   Advanced function   *** */
81*c03c5b1cSMartin Matuska 
82*c03c5b1cSMartin Matuska /** HUF_compress2() :
83*c03c5b1cSMartin Matuska  *  Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.
84*c03c5b1cSMartin Matuska  * `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .
85*c03c5b1cSMartin Matuska  * `tableLog` must be `<= HUF_TABLELOG_MAX` . */
86*c03c5b1cSMartin Matuska HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
87*c03c5b1cSMartin Matuska                                const void* src, size_t srcSize,
88*c03c5b1cSMartin Matuska                                unsigned maxSymbolValue, unsigned tableLog);
89*c03c5b1cSMartin Matuska 
90*c03c5b1cSMartin Matuska /** HUF_compress4X_wksp() :
91*c03c5b1cSMartin Matuska  *  Same as HUF_compress2(), but uses externally allocated `workSpace`.
92*c03c5b1cSMartin Matuska  * `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */
93*c03c5b1cSMartin Matuska #define HUF_WORKSPACE_SIZE ((6 << 10) + 256)
94*c03c5b1cSMartin Matuska #define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
95*c03c5b1cSMartin Matuska HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
96*c03c5b1cSMartin Matuska                                      const void* src, size_t srcSize,
97*c03c5b1cSMartin Matuska                                      unsigned maxSymbolValue, unsigned tableLog,
98*c03c5b1cSMartin Matuska                                      void* workSpace, size_t wkspSize);
99*c03c5b1cSMartin Matuska 
100*c03c5b1cSMartin Matuska #endif   /* HUF_H_298734234 */
101*c03c5b1cSMartin Matuska 
102*c03c5b1cSMartin Matuska /* ******************************************************************
103*c03c5b1cSMartin Matuska  *  WARNING !!
104*c03c5b1cSMartin Matuska  *  The following section contains advanced and experimental definitions
105*c03c5b1cSMartin Matuska  *  which shall never be used in the context of a dynamic library,
106*c03c5b1cSMartin Matuska  *  because they are not guaranteed to remain stable in the future.
107*c03c5b1cSMartin Matuska  *  Only consider them in association with static linking.
108*c03c5b1cSMartin Matuska  * *****************************************************************/
109*c03c5b1cSMartin Matuska #if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)
110*c03c5b1cSMartin Matuska #define HUF_H_HUF_STATIC_LINKING_ONLY
111*c03c5b1cSMartin Matuska 
112*c03c5b1cSMartin Matuska /* *** Dependencies *** */
113*c03c5b1cSMartin Matuska #include "mem.h"   /* U32 */
114*c03c5b1cSMartin Matuska 
115*c03c5b1cSMartin Matuska 
116*c03c5b1cSMartin Matuska /* *** Constants *** */
117*c03c5b1cSMartin Matuska #define HUF_TABLELOG_MAX      12      /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
118*c03c5b1cSMartin Matuska #define HUF_TABLELOG_DEFAULT  11      /* default tableLog value when none specified */
119*c03c5b1cSMartin Matuska #define HUF_SYMBOLVALUE_MAX  255
120*c03c5b1cSMartin Matuska 
121*c03c5b1cSMartin Matuska #define HUF_TABLELOG_ABSOLUTEMAX  15  /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
122*c03c5b1cSMartin Matuska #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
123*c03c5b1cSMartin Matuska #  error "HUF_TABLELOG_MAX is too large !"
124*c03c5b1cSMartin Matuska #endif
125*c03c5b1cSMartin Matuska 
126*c03c5b1cSMartin Matuska 
127*c03c5b1cSMartin Matuska /* ****************************************
128*c03c5b1cSMartin Matuska *  Static allocation
129*c03c5b1cSMartin Matuska ******************************************/
130*c03c5b1cSMartin Matuska /* HUF buffer bounds */
131*c03c5b1cSMartin Matuska #define HUF_CTABLEBOUND 129
132*c03c5b1cSMartin Matuska #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8)   /* only true when incompressible is pre-filtered with fast heuristic */
133*c03c5b1cSMartin Matuska #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size))   /* Macro version, useful for static allocation */
134*c03c5b1cSMartin Matuska 
135*c03c5b1cSMartin Matuska /* static allocation of HUF's Compression Table */
136*c03c5b1cSMartin Matuska #define HUF_CTABLE_SIZE_U32(maxSymbolValue)   ((maxSymbolValue)+1)   /* Use tables of U32, for proper alignment */
137*c03c5b1cSMartin Matuska #define HUF_CTABLE_SIZE(maxSymbolValue)       (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32))
138*c03c5b1cSMartin Matuska #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
139*c03c5b1cSMartin Matuska     U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \
140*c03c5b1cSMartin Matuska     void* name##hv = &(name##hb); \
141*c03c5b1cSMartin Matuska     HUF_CElt* name = (HUF_CElt*)(name##hv)   /* no final ; */
142*c03c5b1cSMartin Matuska 
143*c03c5b1cSMartin Matuska /* static allocation of HUF's DTable */
144*c03c5b1cSMartin Matuska typedef U32 HUF_DTable;
145*c03c5b1cSMartin Matuska #define HUF_DTABLE_SIZE(maxTableLog)   (1 + (1<<(maxTableLog)))
146*c03c5b1cSMartin Matuska #define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \
147*c03c5b1cSMartin Matuska         HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
148*c03c5b1cSMartin Matuska #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
149*c03c5b1cSMartin Matuska         HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
150*c03c5b1cSMartin Matuska 
151*c03c5b1cSMartin Matuska 
152*c03c5b1cSMartin Matuska /* ****************************************
153*c03c5b1cSMartin Matuska *  Advanced decompression functions
154*c03c5b1cSMartin Matuska ******************************************/
155*c03c5b1cSMartin Matuska size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< single-symbol decoder */
156*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X1
157*c03c5b1cSMartin Matuska size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< double-symbols decoder */
158*c03c5b1cSMartin Matuska #endif
159*c03c5b1cSMartin Matuska 
160*c03c5b1cSMartin Matuska size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< decodes RLE and uncompressed */
161*c03c5b1cSMartin Matuska size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */
162*c03c5b1cSMartin Matuska size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< considers RLE and uncompressed as errors */
163*c03c5b1cSMartin Matuska size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< single-symbol decoder */
164*c03c5b1cSMartin Matuska size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< single-symbol decoder */
165*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X1
166*c03c5b1cSMartin Matuska size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< double-symbols decoder */
167*c03c5b1cSMartin Matuska size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< double-symbols decoder */
168*c03c5b1cSMartin Matuska #endif
169*c03c5b1cSMartin Matuska 
170*c03c5b1cSMartin Matuska 
171*c03c5b1cSMartin Matuska /* ****************************************
172*c03c5b1cSMartin Matuska  *  HUF detailed API
173*c03c5b1cSMartin Matuska  * ****************************************/
174*c03c5b1cSMartin Matuska 
175*c03c5b1cSMartin Matuska /*! HUF_compress() does the following:
176*c03c5b1cSMartin Matuska  *  1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h")
177*c03c5b1cSMartin Matuska  *  2. (optional) refine tableLog using HUF_optimalTableLog()
178*c03c5b1cSMartin Matuska  *  3. build Huffman table from count using HUF_buildCTable()
179*c03c5b1cSMartin Matuska  *  4. save Huffman table to memory buffer using HUF_writeCTable()
180*c03c5b1cSMartin Matuska  *  5. encode the data stream using HUF_compress4X_usingCTable()
181*c03c5b1cSMartin Matuska  *
182*c03c5b1cSMartin Matuska  *  The following API allows targeting specific sub-functions for advanced tasks.
183*c03c5b1cSMartin Matuska  *  For example, it's possible to compress several blocks using the same 'CTable',
184*c03c5b1cSMartin Matuska  *  or to save and regenerate 'CTable' using external methods.
185*c03c5b1cSMartin Matuska  */
186*c03c5b1cSMartin Matuska unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
187*c03c5b1cSMartin Matuska typedef struct HUF_CElt_s HUF_CElt;   /* incomplete type */
188*c03c5b1cSMartin Matuska size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits);   /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */
189*c03c5b1cSMartin Matuska size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
190*c03c5b1cSMartin Matuska size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
191*c03c5b1cSMartin Matuska size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
192*c03c5b1cSMartin Matuska int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
193*c03c5b1cSMartin Matuska 
194*c03c5b1cSMartin Matuska typedef enum {
195*c03c5b1cSMartin Matuska    HUF_repeat_none,  /**< Cannot use the previous table */
196*c03c5b1cSMartin Matuska    HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */
197*c03c5b1cSMartin Matuska    HUF_repeat_valid  /**< Can use the previous table and it is assumed to be valid */
198*c03c5b1cSMartin Matuska  } HUF_repeat;
199*c03c5b1cSMartin Matuska /** HUF_compress4X_repeat() :
200*c03c5b1cSMartin Matuska  *  Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
201*c03c5b1cSMartin Matuska  *  If it uses hufTable it does not modify hufTable or repeat.
202*c03c5b1cSMartin Matuska  *  If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
203*c03c5b1cSMartin Matuska  *  If preferRepeat then the old table will always be used if valid. */
204*c03c5b1cSMartin Matuska size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
205*c03c5b1cSMartin Matuska                        const void* src, size_t srcSize,
206*c03c5b1cSMartin Matuska                        unsigned maxSymbolValue, unsigned tableLog,
207*c03c5b1cSMartin Matuska                        void* workSpace, size_t wkspSize,    /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
208*c03c5b1cSMartin Matuska                        HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
209*c03c5b1cSMartin Matuska 
210*c03c5b1cSMartin Matuska /** HUF_buildCTable_wksp() :
211*c03c5b1cSMartin Matuska  *  Same as HUF_buildCTable(), but using externally allocated scratch buffer.
212*c03c5b1cSMartin Matuska  * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.
213*c03c5b1cSMartin Matuska  */
214*c03c5b1cSMartin Matuska #define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)
215*c03c5b1cSMartin Matuska #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
216*c03c5b1cSMartin Matuska size_t HUF_buildCTable_wksp (HUF_CElt* tree,
217*c03c5b1cSMartin Matuska                        const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,
218*c03c5b1cSMartin Matuska                              void* workSpace, size_t wkspSize);
219*c03c5b1cSMartin Matuska 
220*c03c5b1cSMartin Matuska /*! HUF_readStats() :
221*c03c5b1cSMartin Matuska  *  Read compact Huffman tree, saved by HUF_writeCTable().
222*c03c5b1cSMartin Matuska  * `huffWeight` is destination buffer.
223*c03c5b1cSMartin Matuska  * @return : size read from `src` , or an error Code .
224*c03c5b1cSMartin Matuska  *  Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
225*c03c5b1cSMartin Matuska size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
226*c03c5b1cSMartin Matuska                      U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
227*c03c5b1cSMartin Matuska                      const void* src, size_t srcSize);
228*c03c5b1cSMartin Matuska 
229*c03c5b1cSMartin Matuska /** HUF_readCTable() :
230*c03c5b1cSMartin Matuska  *  Loading a CTable saved with HUF_writeCTable() */
231*c03c5b1cSMartin Matuska size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
232*c03c5b1cSMartin Matuska 
233*c03c5b1cSMartin Matuska /** HUF_getNbBits() :
234*c03c5b1cSMartin Matuska  *  Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
235*c03c5b1cSMartin Matuska  *  Note 1 : is not inlined, as HUF_CElt definition is private
236*c03c5b1cSMartin Matuska  *  Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */
237*c03c5b1cSMartin Matuska U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);
238*c03c5b1cSMartin Matuska 
239*c03c5b1cSMartin Matuska /*
240*c03c5b1cSMartin Matuska  * HUF_decompress() does the following:
241*c03c5b1cSMartin Matuska  * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics
242*c03c5b1cSMartin Matuska  * 2. build Huffman table from save, using HUF_readDTableX?()
243*c03c5b1cSMartin Matuska  * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()
244*c03c5b1cSMartin Matuska  */
245*c03c5b1cSMartin Matuska 
246*c03c5b1cSMartin Matuska /** HUF_selectDecoder() :
247*c03c5b1cSMartin Matuska  *  Tells which decoder is likely to decode faster,
248*c03c5b1cSMartin Matuska  *  based on a set of pre-computed metrics.
249*c03c5b1cSMartin Matuska  * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .
250*c03c5b1cSMartin Matuska  *  Assumption : 0 < dstSize <= 128 KB */
251*c03c5b1cSMartin Matuska U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
252*c03c5b1cSMartin Matuska 
253*c03c5b1cSMartin Matuska /**
254*c03c5b1cSMartin Matuska  *  The minimum workspace size for the `workSpace` used in
255*c03c5b1cSMartin Matuska  *  HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp().
256*c03c5b1cSMartin Matuska  *
257*c03c5b1cSMartin Matuska  *  The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
258*c03c5b1cSMartin Matuska  *  HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
259*c03c5b1cSMartin Matuska  *  Buffer overflow errors may potentially occur if code modifications result in
260*c03c5b1cSMartin Matuska  *  a required workspace size greater than that specified in the following
261*c03c5b1cSMartin Matuska  *  macro.
262*c03c5b1cSMartin Matuska  */
263*c03c5b1cSMartin Matuska #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
264*c03c5b1cSMartin Matuska #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
265*c03c5b1cSMartin Matuska 
266*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X2
267*c03c5b1cSMartin Matuska size_t HUF_readDTableX1 (HUF_DTable* DTable, const void* src, size_t srcSize);
268*c03c5b1cSMartin Matuska size_t HUF_readDTableX1_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
269*c03c5b1cSMartin Matuska #endif
270*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X1
271*c03c5b1cSMartin Matuska size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);
272*c03c5b1cSMartin Matuska size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
273*c03c5b1cSMartin Matuska #endif
274*c03c5b1cSMartin Matuska 
275*c03c5b1cSMartin Matuska size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
276*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X2
277*c03c5b1cSMartin Matuska size_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
278*c03c5b1cSMartin Matuska #endif
279*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X1
280*c03c5b1cSMartin Matuska size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
281*c03c5b1cSMartin Matuska #endif
282*c03c5b1cSMartin Matuska 
283*c03c5b1cSMartin Matuska 
284*c03c5b1cSMartin Matuska /* ====================== */
285*c03c5b1cSMartin Matuska /* single stream variants */
286*c03c5b1cSMartin Matuska /* ====================== */
287*c03c5b1cSMartin Matuska 
288*c03c5b1cSMartin Matuska size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
289*c03c5b1cSMartin Matuska size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);  /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
290*c03c5b1cSMartin Matuska size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
291*c03c5b1cSMartin Matuska /** HUF_compress1X_repeat() :
292*c03c5b1cSMartin Matuska  *  Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
293*c03c5b1cSMartin Matuska  *  If it uses hufTable it does not modify hufTable or repeat.
294*c03c5b1cSMartin Matuska  *  If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
295*c03c5b1cSMartin Matuska  *  If preferRepeat then the old table will always be used if valid. */
296*c03c5b1cSMartin Matuska size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
297*c03c5b1cSMartin Matuska                        const void* src, size_t srcSize,
298*c03c5b1cSMartin Matuska                        unsigned maxSymbolValue, unsigned tableLog,
299*c03c5b1cSMartin Matuska                        void* workSpace, size_t wkspSize,   /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
300*c03c5b1cSMartin Matuska                        HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
301*c03c5b1cSMartin Matuska 
302*c03c5b1cSMartin Matuska size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /* single-symbol decoder */
303*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X1
304*c03c5b1cSMartin Matuska size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /* double-symbol decoder */
305*c03c5b1cSMartin Matuska #endif
306*c03c5b1cSMartin Matuska 
307*c03c5b1cSMartin Matuska size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
308*c03c5b1cSMartin Matuska size_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);
309*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X2
310*c03c5b1cSMartin Matuska size_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< single-symbol decoder */
311*c03c5b1cSMartin Matuska size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< single-symbol decoder */
312*c03c5b1cSMartin Matuska #endif
313*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X1
314*c03c5b1cSMartin Matuska size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< double-symbols decoder */
315*c03c5b1cSMartin Matuska size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< double-symbols decoder */
316*c03c5b1cSMartin Matuska #endif
317*c03c5b1cSMartin Matuska 
318*c03c5b1cSMartin Matuska size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);   /**< automatic selection of sing or double symbol decoder, based on DTable */
319*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X2
320*c03c5b1cSMartin Matuska size_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
321*c03c5b1cSMartin Matuska #endif
322*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X1
323*c03c5b1cSMartin Matuska size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
324*c03c5b1cSMartin Matuska #endif
325*c03c5b1cSMartin Matuska 
326*c03c5b1cSMartin Matuska /* BMI2 variants.
327*c03c5b1cSMartin Matuska  * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
328*c03c5b1cSMartin Matuska  */
329*c03c5b1cSMartin Matuska size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
330*c03c5b1cSMartin Matuska #ifndef HUF_FORCE_DECOMPRESS_X2
331*c03c5b1cSMartin Matuska size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
332*c03c5b1cSMartin Matuska #endif
333*c03c5b1cSMartin Matuska size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
334*c03c5b1cSMartin Matuska size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
335*c03c5b1cSMartin Matuska 
336*c03c5b1cSMartin Matuska #endif /* HUF_STATIC_LINKING_ONLY */
337*c03c5b1cSMartin Matuska 
338*c03c5b1cSMartin Matuska #if defined (__cplusplus)
339*c03c5b1cSMartin Matuska }
340*c03c5b1cSMartin Matuska #endif
341