xref: /netbsd-src/external/bsd/zstd/dist/lib/common/huf.h (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /* ******************************************************************
2*3117ece4Schristos  * huff0 huffman codec,
3*3117ece4Schristos  * part of Finite State Entropy library
4*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
5*3117ece4Schristos  *
6*3117ece4Schristos  * You can contact the author at :
7*3117ece4Schristos  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
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 #if defined (__cplusplus)
16*3117ece4Schristos extern "C" {
17*3117ece4Schristos #endif
18*3117ece4Schristos 
19*3117ece4Schristos #ifndef HUF_H_298734234
20*3117ece4Schristos #define HUF_H_298734234
21*3117ece4Schristos 
22*3117ece4Schristos /* *** Dependencies *** */
23*3117ece4Schristos #include "zstd_deps.h"    /* size_t */
24*3117ece4Schristos #include "mem.h"          /* U32 */
25*3117ece4Schristos #define FSE_STATIC_LINKING_ONLY
26*3117ece4Schristos #include "fse.h"
27*3117ece4Schristos 
28*3117ece4Schristos 
29*3117ece4Schristos /* ***   Tool functions *** */
30*3117ece4Schristos #define HUF_BLOCKSIZE_MAX (128 * 1024)   /**< maximum input size for a single block compressed with HUF_compress */
31*3117ece4Schristos size_t HUF_compressBound(size_t size);   /**< maximum compressed size (worst case) */
32*3117ece4Schristos 
33*3117ece4Schristos /* Error Management */
34*3117ece4Schristos unsigned    HUF_isError(size_t code);       /**< tells if a return value is an error code */
35*3117ece4Schristos const char* HUF_getErrorName(size_t code);  /**< provides error code string (useful for debugging) */
36*3117ece4Schristos 
37*3117ece4Schristos 
38*3117ece4Schristos #define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */)
39*3117ece4Schristos #define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64))
40*3117ece4Schristos 
41*3117ece4Schristos /* *** Constants *** */
42*3117ece4Schristos #define HUF_TABLELOG_MAX      12      /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_TABLELOG_ABSOLUTEMAX */
43*3117ece4Schristos #define HUF_TABLELOG_DEFAULT  11      /* default tableLog value when none specified */
44*3117ece4Schristos #define HUF_SYMBOLVALUE_MAX  255
45*3117ece4Schristos 
46*3117ece4Schristos #define HUF_TABLELOG_ABSOLUTEMAX  12  /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
47*3117ece4Schristos #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
48*3117ece4Schristos #  error "HUF_TABLELOG_MAX is too large !"
49*3117ece4Schristos #endif
50*3117ece4Schristos 
51*3117ece4Schristos 
52*3117ece4Schristos /* ****************************************
53*3117ece4Schristos *  Static allocation
54*3117ece4Schristos ******************************************/
55*3117ece4Schristos /* HUF buffer bounds */
56*3117ece4Schristos #define HUF_CTABLEBOUND 129
57*3117ece4Schristos #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8)   /* only true when incompressible is pre-filtered with fast heuristic */
58*3117ece4Schristos #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size))   /* Macro version, useful for static allocation */
59*3117ece4Schristos 
60*3117ece4Schristos /* static allocation of HUF's Compression Table */
61*3117ece4Schristos /* this is a private definition, just exposed for allocation and strict aliasing purpose. never EVER access its members directly */
62*3117ece4Schristos typedef size_t HUF_CElt;   /* consider it an incomplete type */
63*3117ece4Schristos #define HUF_CTABLE_SIZE_ST(maxSymbolValue)   ((maxSymbolValue)+2)   /* Use tables of size_t, for proper alignment */
64*3117ece4Schristos #define HUF_CTABLE_SIZE(maxSymbolValue)       (HUF_CTABLE_SIZE_ST(maxSymbolValue) * sizeof(size_t))
65*3117ece4Schristos #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
66*3117ece4Schristos     HUF_CElt name[HUF_CTABLE_SIZE_ST(maxSymbolValue)] /* no final ; */
67*3117ece4Schristos 
68*3117ece4Schristos /* static allocation of HUF's DTable */
69*3117ece4Schristos typedef U32 HUF_DTable;
70*3117ece4Schristos #define HUF_DTABLE_SIZE(maxTableLog)   (1 + (1<<(maxTableLog)))
71*3117ece4Schristos #define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \
72*3117ece4Schristos         HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
73*3117ece4Schristos #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
74*3117ece4Schristos         HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
75*3117ece4Schristos 
76*3117ece4Schristos 
77*3117ece4Schristos /* ****************************************
78*3117ece4Schristos *  Advanced decompression functions
79*3117ece4Schristos ******************************************/
80*3117ece4Schristos 
81*3117ece4Schristos /**
82*3117ece4Schristos  * Huffman flags bitset.
83*3117ece4Schristos  * For all flags, 0 is the default value.
84*3117ece4Schristos  */
85*3117ece4Schristos typedef enum {
86*3117ece4Schristos     /**
87*3117ece4Schristos      * If compiled with DYNAMIC_BMI2: Set flag only if the CPU supports BMI2 at runtime.
88*3117ece4Schristos      * Otherwise: Ignored.
89*3117ece4Schristos      */
90*3117ece4Schristos     HUF_flags_bmi2 = (1 << 0),
91*3117ece4Schristos     /**
92*3117ece4Schristos      * If set: Test possible table depths to find the one that produces the smallest header + encoded size.
93*3117ece4Schristos      * If unset: Use heuristic to find the table depth.
94*3117ece4Schristos      */
95*3117ece4Schristos     HUF_flags_optimalDepth = (1 << 1),
96*3117ece4Schristos     /**
97*3117ece4Schristos      * If set: If the previous table can encode the input, always reuse the previous table.
98*3117ece4Schristos      * If unset: If the previous table can encode the input, reuse the previous table if it results in a smaller output.
99*3117ece4Schristos      */
100*3117ece4Schristos     HUF_flags_preferRepeat = (1 << 2),
101*3117ece4Schristos     /**
102*3117ece4Schristos      * If set: Sample the input and check if the sample is uncompressible, if it is then don't attempt to compress.
103*3117ece4Schristos      * If unset: Always histogram the entire input.
104*3117ece4Schristos      */
105*3117ece4Schristos     HUF_flags_suspectUncompressible = (1 << 3),
106*3117ece4Schristos     /**
107*3117ece4Schristos      * If set: Don't use assembly implementations
108*3117ece4Schristos      * If unset: Allow using assembly implementations
109*3117ece4Schristos      */
110*3117ece4Schristos     HUF_flags_disableAsm = (1 << 4),
111*3117ece4Schristos     /**
112*3117ece4Schristos      * If set: Don't use the fast decoding loop, always use the fallback decoding loop.
113*3117ece4Schristos      * If unset: Use the fast decoding loop when possible.
114*3117ece4Schristos      */
115*3117ece4Schristos     HUF_flags_disableFast = (1 << 5)
116*3117ece4Schristos } HUF_flags_e;
117*3117ece4Schristos 
118*3117ece4Schristos 
119*3117ece4Schristos /* ****************************************
120*3117ece4Schristos  *  HUF detailed API
121*3117ece4Schristos  * ****************************************/
122*3117ece4Schristos #define HUF_OPTIMAL_DEPTH_THRESHOLD ZSTD_btultra
123*3117ece4Schristos 
124*3117ece4Schristos /*! HUF_compress() does the following:
125*3117ece4Schristos  *  1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h")
126*3117ece4Schristos  *  2. (optional) refine tableLog using HUF_optimalTableLog()
127*3117ece4Schristos  *  3. build Huffman table from count using HUF_buildCTable()
128*3117ece4Schristos  *  4. save Huffman table to memory buffer using HUF_writeCTable()
129*3117ece4Schristos  *  5. encode the data stream using HUF_compress4X_usingCTable()
130*3117ece4Schristos  *
131*3117ece4Schristos  *  The following API allows targeting specific sub-functions for advanced tasks.
132*3117ece4Schristos  *  For example, it's possible to compress several blocks using the same 'CTable',
133*3117ece4Schristos  *  or to save and regenerate 'CTable' using external methods.
134*3117ece4Schristos  */
135*3117ece4Schristos unsigned HUF_minTableLog(unsigned symbolCardinality);
136*3117ece4Schristos unsigned HUF_cardinality(const unsigned* count, unsigned maxSymbolValue);
137*3117ece4Schristos unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, void* workSpace,
138*3117ece4Schristos  size_t wkspSize, HUF_CElt* table, const unsigned* count, int flags); /* table is used as scratch space for building and testing tables, not a return value */
139*3117ece4Schristos size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
140*3117ece4Schristos size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags);
141*3117ece4Schristos size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
142*3117ece4Schristos int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
143*3117ece4Schristos 
144*3117ece4Schristos typedef enum {
145*3117ece4Schristos    HUF_repeat_none,  /**< Cannot use the previous table */
146*3117ece4Schristos    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 */
147*3117ece4Schristos    HUF_repeat_valid  /**< Can use the previous table and it is assumed to be valid */
148*3117ece4Schristos  } HUF_repeat;
149*3117ece4Schristos 
150*3117ece4Schristos /** HUF_compress4X_repeat() :
151*3117ece4Schristos  *  Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
152*3117ece4Schristos  *  If it uses hufTable it does not modify hufTable or repeat.
153*3117ece4Schristos  *  If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
154*3117ece4Schristos  *  If preferRepeat then the old table will always be used if valid.
155*3117ece4Schristos  *  If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
156*3117ece4Schristos size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
157*3117ece4Schristos                        const void* src, size_t srcSize,
158*3117ece4Schristos                        unsigned maxSymbolValue, unsigned tableLog,
159*3117ece4Schristos                        void* workSpace, size_t wkspSize,    /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
160*3117ece4Schristos                        HUF_CElt* hufTable, HUF_repeat* repeat, int flags);
161*3117ece4Schristos 
162*3117ece4Schristos /** HUF_buildCTable_wksp() :
163*3117ece4Schristos  *  Same as HUF_buildCTable(), but using externally allocated scratch buffer.
164*3117ece4Schristos  * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.
165*3117ece4Schristos  */
166*3117ece4Schristos #define HUF_CTABLE_WORKSPACE_SIZE_U32 ((4 * (HUF_SYMBOLVALUE_MAX + 1)) + 192)
167*3117ece4Schristos #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
168*3117ece4Schristos size_t HUF_buildCTable_wksp (HUF_CElt* tree,
169*3117ece4Schristos                        const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,
170*3117ece4Schristos                              void* workSpace, size_t wkspSize);
171*3117ece4Schristos 
172*3117ece4Schristos /*! HUF_readStats() :
173*3117ece4Schristos  *  Read compact Huffman tree, saved by HUF_writeCTable().
174*3117ece4Schristos  * `huffWeight` is destination buffer.
175*3117ece4Schristos  * @return : size read from `src` , or an error Code .
176*3117ece4Schristos  *  Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
177*3117ece4Schristos size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
178*3117ece4Schristos                      U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
179*3117ece4Schristos                      const void* src, size_t srcSize);
180*3117ece4Schristos 
181*3117ece4Schristos /*! HUF_readStats_wksp() :
182*3117ece4Schristos  * Same as HUF_readStats() but takes an external workspace which must be
183*3117ece4Schristos  * 4-byte aligned and its size must be >= HUF_READ_STATS_WORKSPACE_SIZE.
184*3117ece4Schristos  * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
185*3117ece4Schristos  */
186*3117ece4Schristos #define HUF_READ_STATS_WORKSPACE_SIZE_U32 FSE_DECOMPRESS_WKSP_SIZE_U32(6, HUF_TABLELOG_MAX-1)
187*3117ece4Schristos #define HUF_READ_STATS_WORKSPACE_SIZE (HUF_READ_STATS_WORKSPACE_SIZE_U32 * sizeof(unsigned))
188*3117ece4Schristos size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize,
189*3117ece4Schristos                           U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
190*3117ece4Schristos                           const void* src, size_t srcSize,
191*3117ece4Schristos                           void* workspace, size_t wkspSize,
192*3117ece4Schristos                           int flags);
193*3117ece4Schristos 
194*3117ece4Schristos /** HUF_readCTable() :
195*3117ece4Schristos  *  Loading a CTable saved with HUF_writeCTable() */
196*3117ece4Schristos size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
197*3117ece4Schristos 
198*3117ece4Schristos /** HUF_getNbBitsFromCTable() :
199*3117ece4Schristos  *  Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
200*3117ece4Schristos  *  Note 1 : If symbolValue > HUF_readCTableHeader(symbolTable).maxSymbolValue, returns 0
201*3117ece4Schristos  *  Note 2 : is not inlined, as HUF_CElt definition is private
202*3117ece4Schristos  */
203*3117ece4Schristos U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue);
204*3117ece4Schristos 
205*3117ece4Schristos typedef struct {
206*3117ece4Schristos     BYTE tableLog;
207*3117ece4Schristos     BYTE maxSymbolValue;
208*3117ece4Schristos     BYTE unused[sizeof(size_t) - 2];
209*3117ece4Schristos } HUF_CTableHeader;
210*3117ece4Schristos 
211*3117ece4Schristos /** HUF_readCTableHeader() :
212*3117ece4Schristos  *  @returns The header from the CTable specifying the tableLog and the maxSymbolValue.
213*3117ece4Schristos  */
214*3117ece4Schristos HUF_CTableHeader HUF_readCTableHeader(HUF_CElt const* ctable);
215*3117ece4Schristos 
216*3117ece4Schristos /*
217*3117ece4Schristos  * HUF_decompress() does the following:
218*3117ece4Schristos  * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics
219*3117ece4Schristos  * 2. build Huffman table from save, using HUF_readDTableX?()
220*3117ece4Schristos  * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()
221*3117ece4Schristos  */
222*3117ece4Schristos 
223*3117ece4Schristos /** HUF_selectDecoder() :
224*3117ece4Schristos  *  Tells which decoder is likely to decode faster,
225*3117ece4Schristos  *  based on a set of pre-computed metrics.
226*3117ece4Schristos  * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .
227*3117ece4Schristos  *  Assumption : 0 < dstSize <= 128 KB */
228*3117ece4Schristos U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
229*3117ece4Schristos 
230*3117ece4Schristos /**
231*3117ece4Schristos  *  The minimum workspace size for the `workSpace` used in
232*3117ece4Schristos  *  HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp().
233*3117ece4Schristos  *
234*3117ece4Schristos  *  The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
235*3117ece4Schristos  *  HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
236*3117ece4Schristos  *  Buffer overflow errors may potentially occur if code modifications result in
237*3117ece4Schristos  *  a required workspace size greater than that specified in the following
238*3117ece4Schristos  *  macro.
239*3117ece4Schristos  */
240*3117ece4Schristos #define HUF_DECOMPRESS_WORKSPACE_SIZE ((2 << 10) + (1 << 9))
241*3117ece4Schristos #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
242*3117ece4Schristos 
243*3117ece4Schristos 
244*3117ece4Schristos /* ====================== */
245*3117ece4Schristos /* single stream variants */
246*3117ece4Schristos /* ====================== */
247*3117ece4Schristos 
248*3117ece4Schristos size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags);
249*3117ece4Schristos /** HUF_compress1X_repeat() :
250*3117ece4Schristos  *  Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
251*3117ece4Schristos  *  If it uses hufTable it does not modify hufTable or repeat.
252*3117ece4Schristos  *  If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
253*3117ece4Schristos  *  If preferRepeat then the old table will always be used if valid.
254*3117ece4Schristos  *  If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
255*3117ece4Schristos size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
256*3117ece4Schristos                        const void* src, size_t srcSize,
257*3117ece4Schristos                        unsigned maxSymbolValue, unsigned tableLog,
258*3117ece4Schristos                        void* workSpace, size_t wkspSize,   /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
259*3117ece4Schristos                        HUF_CElt* hufTable, HUF_repeat* repeat, int flags);
260*3117ece4Schristos 
261*3117ece4Schristos 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, int flags);
262*3117ece4Schristos #ifndef HUF_FORCE_DECOMPRESS_X1
263*3117ece4Schristos 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, int flags);   /**< double-symbols decoder */
264*3117ece4Schristos #endif
265*3117ece4Schristos 
266*3117ece4Schristos /* BMI2 variants.
267*3117ece4Schristos  * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
268*3117ece4Schristos  */
269*3117ece4Schristos size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
270*3117ece4Schristos #ifndef HUF_FORCE_DECOMPRESS_X2
271*3117ece4Schristos 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, int flags);
272*3117ece4Schristos #endif
273*3117ece4Schristos size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
274*3117ece4Schristos 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, int flags);
275*3117ece4Schristos #ifndef HUF_FORCE_DECOMPRESS_X2
276*3117ece4Schristos size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
277*3117ece4Schristos #endif
278*3117ece4Schristos #ifndef HUF_FORCE_DECOMPRESS_X1
279*3117ece4Schristos size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
280*3117ece4Schristos #endif
281*3117ece4Schristos 
282*3117ece4Schristos #endif   /* HUF_H_298734234 */
283*3117ece4Schristos 
284*3117ece4Schristos #if defined (__cplusplus)
285*3117ece4Schristos }
286*3117ece4Schristos #endif
287