Project Alice
Loading...
Searching...
No Matches
huf.h
Go to the documentation of this file.
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#if defined (__cplusplus)
16extern "C" {
17#endif
18
19#ifndef HUF_H_298734234
20#define HUF_H_298734234
21
22/* *** Dependencies *** */
23#include "zstd_deps.h" /* size_t */
24#include "mem.h" /* U32 */
25#define FSE_STATIC_LINKING_ONLY
26#include "fse.h"
27
28
29/* *** Tool functions *** */
30#define HUF_BLOCKSIZE_MAX (128 * 1024)
31size_t HUF_compressBound(size_t size);
33/* Error Management */
34unsigned HUF_isError(size_t code);
35const char* HUF_getErrorName(size_t code);
38#define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */)
39#define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64))
40
41/* *** Constants *** */
42#define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_TABLELOG_ABSOLUTEMAX */
43#define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */
44#define HUF_SYMBOLVALUE_MAX 255
45
46#define HUF_TABLELOG_ABSOLUTEMAX 12 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
47#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
48# error "HUF_TABLELOG_MAX is too large !"
49#endif
50
51
52/* ****************************************
53* Static allocation
54******************************************/
55/* HUF buffer bounds */
56#define HUF_CTABLEBOUND 129
57#define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true when incompressible is pre-filtered with fast heuristic */
58#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
59
60/* static allocation of HUF's Compression Table */
61/* this is a private definition, just exposed for allocation and strict aliasing purpose. never EVER access its members directly */
62typedef size_t HUF_CElt; /* consider it an incomplete type */
63#define HUF_CTABLE_SIZE_ST(maxSymbolValue) ((maxSymbolValue)+2) /* Use tables of size_t, for proper alignment */
64#define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_ST(maxSymbolValue) * sizeof(size_t))
65#define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
66 HUF_CElt name[HUF_CTABLE_SIZE_ST(maxSymbolValue)] /* no final ; */
67
68/* static allocation of HUF's DTable */
70#define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))
71#define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \
72 HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
73#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
74 HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
75
76
77/* ****************************************
78* Advanced decompression functions
79******************************************/
80
85typedef enum {
90 HUF_flags_bmi2 = (1 << 0),
115 HUF_flags_disableFast = (1 << 5)
117
118
119/* ****************************************
120 * HUF detailed API
121 * ****************************************/
122#define HUF_OPTIMAL_DEPTH_THRESHOLD ZSTD_btultra
123
135unsigned HUF_minTableLog(unsigned symbolCardinality);
136unsigned HUF_cardinality(const unsigned* count, unsigned maxSymbolValue);
137unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, void* workSpace,
138 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 */
139size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
140size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags);
141size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
142int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
143
144typedef enum {
149
156size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
157 const void* src, size_t srcSize,
158 unsigned maxSymbolValue, unsigned tableLog,
159 void* workSpace, size_t wkspSize,
160 HUF_CElt* hufTable, HUF_repeat* repeat, int flags);
161
166#define HUF_CTABLE_WORKSPACE_SIZE_U32 ((4 * (HUF_SYMBOLVALUE_MAX + 1)) + 192)
167#define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
168size_t HUF_buildCTable_wksp (HUF_CElt* tree,
169 const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,
170 void* workSpace, size_t wkspSize);
171
177size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
178 U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
179 const void* src, size_t srcSize);
180
186#define HUF_READ_STATS_WORKSPACE_SIZE_U32 FSE_DECOMPRESS_WKSP_SIZE_U32(6, HUF_TABLELOG_MAX-1)
187#define HUF_READ_STATS_WORKSPACE_SIZE (HUF_READ_STATS_WORKSPACE_SIZE_U32 * sizeof(unsigned))
188size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize,
189 U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
190 const void* src, size_t srcSize,
191 void* workspace, size_t wkspSize,
192 int flags);
193
196size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
197
203U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue);
204
205typedef struct {
208 BYTE unused[sizeof(size_t) - 2];
210
215
216/*
217 * HUF_decompress() does the following:
218 * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics
219 * 2. build Huffman table from save, using HUF_readDTableX?()
220 * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()
221 */
222
228U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
229
240#define HUF_DECOMPRESS_WORKSPACE_SIZE ((2 << 10) + (1 << 9))
241#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
242
243
244/* ====================== */
245/* single stream variants */
246/* ====================== */
247
248size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags);
255size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
256 const void* src, size_t srcSize,
257 unsigned maxSymbolValue, unsigned tableLog,
258 void* workSpace, size_t wkspSize,
259 HUF_CElt* hufTable, HUF_repeat* repeat, int flags);
260
261size_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#ifndef HUF_FORCE_DECOMPRESS_X1
263size_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);
264#endif
265
266/* BMI2 variants.
267 * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
268 */
269size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
270#ifndef HUF_FORCE_DECOMPRESS_X2
271size_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#endif
273size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
274size_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#ifndef HUF_FORCE_DECOMPRESS_X2
276size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
277#endif
278#ifndef HUF_FORCE_DECOMPRESS_X1
279size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
280#endif
281
282#endif /* HUF_H_298734234 */
283
284#if defined (__cplusplus)
285}
286#endif
size_t HUF_readDTableX2_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workSpace, size_t wkspSize, int flags)
size_t HUF_compressBound(size_t size)
Definition: huf_compress.c:821
const char * HUF_getErrorName(size_t code)
int HUF_validateCTable(const HUF_CElt *CTable, const unsigned *count, unsigned maxSymbolValue)
Definition: huf_compress.c:804
U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize)
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)
size_t HUF_writeCTable_wksp(void *dst, size_t maxDstSize, const HUF_CElt *CTable, unsigned maxSymbolValue, unsigned huffLog, void *workspace, size_t workspaceSize)
Definition: huf_compress.c:248
unsigned HUF_cardinality(const unsigned *count, unsigned maxSymbolValue)
HUF_flags_e
Definition: huf.h:85
@ HUF_flags_bmi2
Definition: huf.h:90
@ HUF_flags_suspectUncompressible
Definition: huf.h:105
@ HUF_flags_disableFast
Definition: huf.h:115
@ HUF_flags_optimalDepth
Definition: huf.h:95
@ HUF_flags_disableAsm
Definition: huf.h:110
@ HUF_flags_preferRepeat
Definition: huf.h:100
U32 HUF_getNbBitsFromCTable(const HUF_CElt *symbolTable, U32 symbolValue)
Definition: huf_compress.c:345
size_t HUF_CElt
Definition: huf.h:62
size_t HUF_compress4X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable, int flags)
size_t HUF_compress1X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, int flags)
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)
size_t HUF_estimateCompressedSize(const HUF_CElt *CTable, const unsigned *count, unsigned maxSymbolValue)
Definition: huf_compress.c:793
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)
size_t HUF_readStats(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize)
U32 HUF_DTable
Definition: huf.h:69
HUF_CTableHeader HUF_readCTableHeader(HUF_CElt const *ctable)
Definition: huf_compress.c:223
size_t HUF_compress1X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable, int flags)
size_t HUF_decompress4X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable, int flags)
size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, void *workspace, size_t wkspSize, int flags)
size_t HUF_buildCTable_wksp(HUF_CElt *tree, const unsigned *count, U32 maxSymbolValue, U32 maxNbBits, void *workSpace, size_t wkspSize)
Definition: huf_compress.c:756
size_t HUF_compress4X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, int flags)
size_t HUF_readDTableX1_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workSpace, size_t wkspSize, int flags)
size_t HUF_decompress1X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable, int flags)
size_t HUF_readCTable(HUF_CElt *CTable, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize, unsigned *hasZeroWeights)
Definition: huf_compress.c:292
HUF_repeat
Definition: huf.h:144
@ HUF_repeat_valid
Definition: huf.h:147
@ HUF_repeat_none
Definition: huf.h:145
@ HUF_repeat_check
Definition: huf.h:146
unsigned HUF_minTableLog(unsigned symbolCardinality)
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)
unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, void *workSpace, size_t wkspSize, HUF_CElt *table, const unsigned *count, int flags)
unsigned char BYTE
Definition: mem.h:58
unsigned int U32
Definition: mem.h:69
Definition: table.hpp:10
BYTE maxSymbolValue
Definition: huf.h:207
BYTE tableLog
Definition: huf.h:206
#define HUF_isError
Definition: zstd_internal.h:50