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