zstdlib 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/ext/zstdlib/extconf.rb +1 -1
- data/ext/zstdlib/ruby/zlib-3.0/zstdlib.c +4994 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/bitstream.h +25 -16
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/compiler.h +118 -4
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/cpu.h +1 -3
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/debug.c +1 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/debug.h +12 -19
- data/ext/zstdlib/zstd-1.5.0/lib/common/entropy_common.c +362 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/error_private.c +2 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/error_private.h +3 -3
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/fse.h +40 -12
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/fse_decompress.c +139 -22
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/huf.h +29 -7
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/mem.h +69 -98
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/pool.c +23 -17
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/pool.h +2 -2
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/threading.c +6 -5
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/threading.h +0 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/xxhash.c +20 -60
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/xxhash.h +2 -2
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/zstd_common.c +10 -10
- data/ext/zstdlib/zstd-1.5.0/lib/common/zstd_deps.h +111 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/zstd_internal.h +105 -62
- data/ext/zstdlib/zstd-1.5.0/lib/common/zstd_trace.h +154 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/fse_compress.c +31 -24
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/hist.c +27 -29
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/hist.h +2 -2
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/huf_compress.c +265 -126
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress.c +2843 -728
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_internal.h +305 -63
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_literals.c +8 -8
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_literals.h +1 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_sequences.c +29 -7
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_sequences.h +1 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_superblock.c +22 -295
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_superblock.h +1 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_cwksp.h +204 -67
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_double_fast.c +25 -25
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_double_fast.h +1 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_fast.c +23 -23
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_fast.h +1 -1
- data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_lazy.c +2184 -0
- data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_lazy.h +125 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_ldm.c +314 -211
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_ldm.h +9 -2
- data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_ldm_geartab.h +103 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_opt.c +191 -46
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_opt.h +1 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstdmt_compress.c +93 -415
- data/ext/zstdlib/zstd-1.5.0/lib/compress/zstdmt_compress.h +110 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/huf_decompress.c +342 -239
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_ddict.c +9 -9
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_ddict.h +2 -2
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress.c +369 -87
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_block.c +191 -75
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_block.h +6 -3
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_internal.h +27 -11
- data/ext/zstdlib/zstd-1.5.0/lib/zdict.h +452 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/zstd.h +568 -126
- data/ext/zstdlib/{zstd-1.4.5/lib/common → zstd-1.5.0/lib}/zstd_errors.h +2 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzclose.c +0 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzcompatibility.h +1 -1
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzguts.h +0 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzlib.c +0 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzread.c +0 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzwrite.c +0 -0
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/zstd_zlibwrapper.c +126 -44
- data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/zstd_zlibwrapper.h +1 -1
- metadata +70 -65
- data/ext/zstdlib/zstd-1.4.5/lib/common/entropy_common.c +0 -216
- data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.c +0 -1138
- data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.h +0 -67
- data/ext/zstdlib/zstd-1.4.5/lib/compress/zstdmt_compress.h +0 -192
@@ -1,7 +1,7 @@
|
|
1
1
|
/* ******************************************************************
|
2
2
|
* hist : Histogram functions
|
3
3
|
* part of Finite State Entropy project
|
4
|
-
* Copyright (c)
|
4
|
+
* Copyright (c) Yann Collet, Facebook, Inc.
|
5
5
|
*
|
6
6
|
* You can contact the author at :
|
7
7
|
* - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
|
@@ -14,7 +14,7 @@
|
|
14
14
|
****************************************************************** */
|
15
15
|
|
16
16
|
/* --- dependencies --- */
|
17
|
-
#include
|
17
|
+
#include "../common/zstd_deps.h" /* size_t */
|
18
18
|
|
19
19
|
|
20
20
|
/* --- simple histogram functions --- */
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/* ******************************************************************
|
2
2
|
* Huffman encoder, part of New Generation Entropy library
|
3
|
-
* Copyright (c)
|
3
|
+
* Copyright (c) Yann Collet, Facebook, Inc.
|
4
4
|
*
|
5
5
|
* You can contact the author at :
|
6
6
|
* - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
|
@@ -23,8 +23,7 @@
|
|
23
23
|
/* **************************************************************
|
24
24
|
* Includes
|
25
25
|
****************************************************************/
|
26
|
-
#include
|
27
|
-
#include <stdio.h> /* printf (debug) */
|
26
|
+
#include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset */
|
28
27
|
#include "../common/compiler.h"
|
29
28
|
#include "../common/bitstream.h"
|
30
29
|
#include "hist.h"
|
@@ -60,7 +59,15 @@ unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxS
|
|
60
59
|
* Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX.
|
61
60
|
*/
|
62
61
|
#define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
|
63
|
-
|
62
|
+
|
63
|
+
typedef struct {
|
64
|
+
FSE_CTable CTable[FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX)];
|
65
|
+
U32 scratchBuffer[FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(HUF_TABLELOG_MAX, MAX_FSE_TABLELOG_FOR_HUFF_HEADER)];
|
66
|
+
unsigned count[HUF_TABLELOG_MAX+1];
|
67
|
+
S16 norm[HUF_TABLELOG_MAX+1];
|
68
|
+
} HUF_CompressWeightsWksp;
|
69
|
+
|
70
|
+
static size_t HUF_compressWeights(void* dst, size_t dstSize, const void* weightTable, size_t wtSize, void* workspace, size_t workspaceSize)
|
64
71
|
{
|
65
72
|
BYTE* const ostart = (BYTE*) dst;
|
66
73
|
BYTE* op = ostart;
|
@@ -68,33 +75,30 @@ static size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weight
|
|
68
75
|
|
69
76
|
unsigned maxSymbolValue = HUF_TABLELOG_MAX;
|
70
77
|
U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER;
|
78
|
+
HUF_CompressWeightsWksp* wksp = (HUF_CompressWeightsWksp*)workspace;
|
71
79
|
|
72
|
-
|
73
|
-
BYTE scratchBuffer[1<<MAX_FSE_TABLELOG_FOR_HUFF_HEADER];
|
74
|
-
|
75
|
-
unsigned count[HUF_TABLELOG_MAX+1];
|
76
|
-
S16 norm[HUF_TABLELOG_MAX+1];
|
80
|
+
if (workspaceSize < sizeof(HUF_CompressWeightsWksp)) return ERROR(GENERIC);
|
77
81
|
|
78
82
|
/* init conditions */
|
79
83
|
if (wtSize <= 1) return 0; /* Not compressible */
|
80
84
|
|
81
85
|
/* Scan input and build symbol stats */
|
82
|
-
{ unsigned const maxCount = HIST_count_simple(count, &maxSymbolValue, weightTable, wtSize); /* never fails */
|
86
|
+
{ unsigned const maxCount = HIST_count_simple(wksp->count, &maxSymbolValue, weightTable, wtSize); /* never fails */
|
83
87
|
if (maxCount == wtSize) return 1; /* only a single symbol in src : rle */
|
84
88
|
if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */
|
85
89
|
}
|
86
90
|
|
87
91
|
tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue);
|
88
|
-
CHECK_F( FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue) );
|
92
|
+
CHECK_F( FSE_normalizeCount(wksp->norm, tableLog, wksp->count, wtSize, maxSymbolValue, /* useLowProbCount */ 0) );
|
89
93
|
|
90
94
|
/* Write table description header */
|
91
|
-
{ CHECK_V_F(hSize, FSE_writeNCount(op, (size_t)(oend-op), norm, maxSymbolValue, tableLog) );
|
95
|
+
{ CHECK_V_F(hSize, FSE_writeNCount(op, (size_t)(oend-op), wksp->norm, maxSymbolValue, tableLog) );
|
92
96
|
op += hSize;
|
93
97
|
}
|
94
98
|
|
95
99
|
/* Compress */
|
96
|
-
CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, sizeof(scratchBuffer)) );
|
97
|
-
{ CHECK_V_F(cSize, FSE_compress_usingCTable(op, (size_t)(oend - op), weightTable, wtSize, CTable) );
|
100
|
+
CHECK_F( FSE_buildCTable_wksp(wksp->CTable, wksp->norm, maxSymbolValue, tableLog, wksp->scratchBuffer, sizeof(wksp->scratchBuffer)) );
|
101
|
+
{ CHECK_V_F(cSize, FSE_compress_usingCTable(op, (size_t)(oend - op), weightTable, wtSize, wksp->CTable) );
|
98
102
|
if (cSize == 0) return 0; /* not enough space for compressed data */
|
99
103
|
op += cSize;
|
100
104
|
}
|
@@ -103,34 +107,33 @@ static size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weight
|
|
103
107
|
}
|
104
108
|
|
105
109
|
|
106
|
-
struct
|
107
|
-
|
108
|
-
BYTE nbBits;
|
109
|
-
}; /* typedef'd to HUF_CElt within "huf.h" */
|
110
|
-
|
111
|
-
/*! HUF_writeCTable() :
|
112
|
-
`CTable` : Huffman tree to save, using huf representation.
|
113
|
-
@return : size of saved CTable */
|
114
|
-
size_t HUF_writeCTable (void* dst, size_t maxDstSize,
|
115
|
-
const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog)
|
116
|
-
{
|
110
|
+
typedef struct {
|
111
|
+
HUF_CompressWeightsWksp wksp;
|
117
112
|
BYTE bitsToWeight[HUF_TABLELOG_MAX + 1]; /* precomputed conversion table */
|
118
113
|
BYTE huffWeight[HUF_SYMBOLVALUE_MAX];
|
114
|
+
} HUF_WriteCTableWksp;
|
115
|
+
|
116
|
+
size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize,
|
117
|
+
const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog,
|
118
|
+
void* workspace, size_t workspaceSize)
|
119
|
+
{
|
119
120
|
BYTE* op = (BYTE*)dst;
|
120
121
|
U32 n;
|
122
|
+
HUF_WriteCTableWksp* wksp = (HUF_WriteCTableWksp*)workspace;
|
121
123
|
|
122
|
-
|
124
|
+
/* check conditions */
|
125
|
+
if (workspaceSize < sizeof(HUF_WriteCTableWksp)) return ERROR(GENERIC);
|
123
126
|
if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge);
|
124
127
|
|
125
128
|
/* convert to weight */
|
126
|
-
bitsToWeight[0] = 0;
|
129
|
+
wksp->bitsToWeight[0] = 0;
|
127
130
|
for (n=1; n<huffLog+1; n++)
|
128
|
-
bitsToWeight[n] = (BYTE)(huffLog + 1 - n);
|
131
|
+
wksp->bitsToWeight[n] = (BYTE)(huffLog + 1 - n);
|
129
132
|
for (n=0; n<maxSymbolValue; n++)
|
130
|
-
huffWeight[n] = bitsToWeight[CTable[n].nbBits];
|
133
|
+
wksp->huffWeight[n] = wksp->bitsToWeight[CTable[n].nbBits];
|
131
134
|
|
132
135
|
/* attempt weights compression by FSE */
|
133
|
-
{ CHECK_V_F(hSize, HUF_compressWeights(op+1, maxDstSize-1, huffWeight, maxSymbolValue) );
|
136
|
+
{ CHECK_V_F(hSize, HUF_compressWeights(op+1, maxDstSize-1, wksp->huffWeight, maxSymbolValue, &wksp->wksp, sizeof(wksp->wksp)) );
|
134
137
|
if ((hSize>1) & (hSize < maxSymbolValue/2)) { /* FSE compressed */
|
135
138
|
op[0] = (BYTE)hSize;
|
136
139
|
return hSize+1;
|
@@ -140,12 +143,22 @@ size_t HUF_writeCTable (void* dst, size_t maxDstSize,
|
|
140
143
|
if (maxSymbolValue > (256-128)) return ERROR(GENERIC); /* should not happen : likely means source cannot be compressed */
|
141
144
|
if (((maxSymbolValue+1)/2) + 1 > maxDstSize) return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */
|
142
145
|
op[0] = (BYTE)(128 /*special case*/ + (maxSymbolValue-1));
|
143
|
-
huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause msan issue in final combination */
|
146
|
+
wksp->huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause msan issue in final combination */
|
144
147
|
for (n=0; n<maxSymbolValue; n+=2)
|
145
|
-
op[(n/2)+1] = (BYTE)((huffWeight[n] << 4) + huffWeight[n+1]);
|
148
|
+
op[(n/2)+1] = (BYTE)((wksp->huffWeight[n] << 4) + wksp->huffWeight[n+1]);
|
146
149
|
return ((maxSymbolValue+1)/2) + 1;
|
147
150
|
}
|
148
151
|
|
152
|
+
/*! HUF_writeCTable() :
|
153
|
+
`CTable` : Huffman tree to save, using huf representation.
|
154
|
+
@return : size of saved CTable */
|
155
|
+
size_t HUF_writeCTable (void* dst, size_t maxDstSize,
|
156
|
+
const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog)
|
157
|
+
{
|
158
|
+
HUF_WriteCTableWksp wksp;
|
159
|
+
return HUF_writeCTable_wksp(dst, maxDstSize, CTable, maxSymbolValue, huffLog, &wksp, sizeof(wksp));
|
160
|
+
}
|
161
|
+
|
149
162
|
|
150
163
|
size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned* hasZeroWeights)
|
151
164
|
{
|
@@ -156,6 +169,7 @@ size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void
|
|
156
169
|
|
157
170
|
/* get symbol weights */
|
158
171
|
CHECK_V_F(readSize, HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX+1, rankVal, &nbSymbols, &tableLog, src, srcSize));
|
172
|
+
*hasZeroWeights = (rankVal[0] > 0);
|
159
173
|
|
160
174
|
/* check result */
|
161
175
|
if (tableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
|
@@ -164,16 +178,14 @@ size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void
|
|
164
178
|
/* Prepare base value per rank */
|
165
179
|
{ U32 n, nextRankStart = 0;
|
166
180
|
for (n=1; n<=tableLog; n++) {
|
167
|
-
U32
|
181
|
+
U32 curr = nextRankStart;
|
168
182
|
nextRankStart += (rankVal[n] << (n-1));
|
169
|
-
rankVal[n] =
|
183
|
+
rankVal[n] = curr;
|
170
184
|
} }
|
171
185
|
|
172
186
|
/* fill nbBits */
|
173
|
-
*hasZeroWeights = 0;
|
174
187
|
{ U32 n; for (n=0; n<nbSymbols; n++) {
|
175
188
|
const U32 w = huffWeight[n];
|
176
|
-
*hasZeroWeights |= (w == 0);
|
177
189
|
CTable[n].nbBits = (BYTE)(tableLog + 1 - w) & -(w != 0);
|
178
190
|
} }
|
179
191
|
|
@@ -212,32 +224,63 @@ typedef struct nodeElt_s {
|
|
212
224
|
BYTE nbBits;
|
213
225
|
} nodeElt;
|
214
226
|
|
227
|
+
/**
|
228
|
+
* HUF_setMaxHeight():
|
229
|
+
* Enforces maxNbBits on the Huffman tree described in huffNode.
|
230
|
+
*
|
231
|
+
* It sets all nodes with nbBits > maxNbBits to be maxNbBits. Then it adjusts
|
232
|
+
* the tree to so that it is a valid canonical Huffman tree.
|
233
|
+
*
|
234
|
+
* @pre The sum of the ranks of each symbol == 2^largestBits,
|
235
|
+
* where largestBits == huffNode[lastNonNull].nbBits.
|
236
|
+
* @post The sum of the ranks of each symbol == 2^largestBits,
|
237
|
+
* where largestBits is the return value <= maxNbBits.
|
238
|
+
*
|
239
|
+
* @param huffNode The Huffman tree modified in place to enforce maxNbBits.
|
240
|
+
* @param lastNonNull The symbol with the lowest count in the Huffman tree.
|
241
|
+
* @param maxNbBits The maximum allowed number of bits, which the Huffman tree
|
242
|
+
* may not respect. After this function the Huffman tree will
|
243
|
+
* respect maxNbBits.
|
244
|
+
* @return The maximum number of bits of the Huffman tree after adjustment,
|
245
|
+
* necessarily no more than maxNbBits.
|
246
|
+
*/
|
215
247
|
static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
|
216
248
|
{
|
217
249
|
const U32 largestBits = huffNode[lastNonNull].nbBits;
|
218
|
-
|
250
|
+
/* early exit : no elt > maxNbBits, so the tree is already valid. */
|
251
|
+
if (largestBits <= maxNbBits) return largestBits;
|
219
252
|
|
220
253
|
/* there are several too large elements (at least >= 2) */
|
221
254
|
{ int totalCost = 0;
|
222
255
|
const U32 baseCost = 1 << (largestBits - maxNbBits);
|
223
256
|
int n = (int)lastNonNull;
|
224
257
|
|
258
|
+
/* Adjust any ranks > maxNbBits to maxNbBits.
|
259
|
+
* Compute totalCost, which is how far the sum of the ranks is
|
260
|
+
* we are over 2^largestBits after adjust the offending ranks.
|
261
|
+
*/
|
225
262
|
while (huffNode[n].nbBits > maxNbBits) {
|
226
263
|
totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits));
|
227
264
|
huffNode[n].nbBits = (BYTE)maxNbBits;
|
228
|
-
n
|
229
|
-
}
|
230
|
-
|
265
|
+
n--;
|
266
|
+
}
|
267
|
+
/* n stops at huffNode[n].nbBits <= maxNbBits */
|
268
|
+
assert(huffNode[n].nbBits <= maxNbBits);
|
269
|
+
/* n end at index of smallest symbol using < maxNbBits */
|
270
|
+
while (huffNode[n].nbBits == maxNbBits) --n;
|
231
271
|
|
232
|
-
/* renorm totalCost
|
233
|
-
|
272
|
+
/* renorm totalCost from 2^largestBits to 2^maxNbBits
|
273
|
+
* note : totalCost is necessarily a multiple of baseCost */
|
274
|
+
assert((totalCost & (baseCost - 1)) == 0);
|
275
|
+
totalCost >>= (largestBits - maxNbBits);
|
276
|
+
assert(totalCost > 0);
|
234
277
|
|
235
278
|
/* repay normalized cost */
|
236
279
|
{ U32 const noSymbol = 0xF0F0F0F0;
|
237
280
|
U32 rankLast[HUF_TABLELOG_MAX+2];
|
238
281
|
|
239
|
-
/* Get pos of last (smallest) symbol per rank */
|
240
|
-
|
282
|
+
/* Get pos of last (smallest = lowest cum. count) symbol per rank */
|
283
|
+
ZSTD_memset(rankLast, 0xF0, sizeof(rankLast));
|
241
284
|
{ U32 currentNbBits = maxNbBits;
|
242
285
|
int pos;
|
243
286
|
for (pos=n ; pos >= 0; pos--) {
|
@@ -247,34 +290,65 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
|
|
247
290
|
} }
|
248
291
|
|
249
292
|
while (totalCost > 0) {
|
293
|
+
/* Try to reduce the next power of 2 above totalCost because we
|
294
|
+
* gain back half the rank.
|
295
|
+
*/
|
250
296
|
U32 nBitsToDecrease = BIT_highbit32((U32)totalCost) + 1;
|
251
297
|
for ( ; nBitsToDecrease > 1; nBitsToDecrease--) {
|
252
298
|
U32 const highPos = rankLast[nBitsToDecrease];
|
253
299
|
U32 const lowPos = rankLast[nBitsToDecrease-1];
|
254
300
|
if (highPos == noSymbol) continue;
|
301
|
+
/* Decrease highPos if no symbols of lowPos or if it is
|
302
|
+
* not cheaper to remove 2 lowPos than highPos.
|
303
|
+
*/
|
255
304
|
if (lowPos == noSymbol) break;
|
256
305
|
{ U32 const highTotal = huffNode[highPos].count;
|
257
306
|
U32 const lowTotal = 2 * huffNode[lowPos].count;
|
258
307
|
if (highTotal <= lowTotal) break;
|
259
308
|
} }
|
260
309
|
/* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */
|
310
|
+
assert(rankLast[nBitsToDecrease] != noSymbol || nBitsToDecrease == 1);
|
261
311
|
/* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */
|
262
312
|
while ((nBitsToDecrease<=HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol))
|
263
|
-
nBitsToDecrease
|
313
|
+
nBitsToDecrease++;
|
314
|
+
assert(rankLast[nBitsToDecrease] != noSymbol);
|
315
|
+
/* Increase the number of bits to gain back half the rank cost. */
|
264
316
|
totalCost -= 1 << (nBitsToDecrease-1);
|
317
|
+
huffNode[rankLast[nBitsToDecrease]].nbBits++;
|
318
|
+
|
319
|
+
/* Fix up the new rank.
|
320
|
+
* If the new rank was empty, this symbol is now its smallest.
|
321
|
+
* Otherwise, this symbol will be the largest in the new rank so no adjustment.
|
322
|
+
*/
|
265
323
|
if (rankLast[nBitsToDecrease-1] == noSymbol)
|
266
|
-
rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease];
|
267
|
-
|
324
|
+
rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease];
|
325
|
+
/* Fix up the old rank.
|
326
|
+
* If the symbol was at position 0, meaning it was the highest weight symbol in the tree,
|
327
|
+
* it must be the only symbol in its rank, so the old rank now has no symbols.
|
328
|
+
* Otherwise, since the Huffman nodes are sorted by count, the previous position is now
|
329
|
+
* the smallest node in the rank. If the previous position belongs to a different rank,
|
330
|
+
* then the rank is now empty.
|
331
|
+
*/
|
268
332
|
if (rankLast[nBitsToDecrease] == 0) /* special case, reached largest symbol */
|
269
333
|
rankLast[nBitsToDecrease] = noSymbol;
|
270
334
|
else {
|
271
335
|
rankLast[nBitsToDecrease]--;
|
272
336
|
if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease)
|
273
337
|
rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */
|
274
|
-
|
275
|
-
|
338
|
+
}
|
339
|
+
} /* while (totalCost > 0) */
|
340
|
+
|
341
|
+
/* If we've removed too much weight, then we have to add it back.
|
342
|
+
* To avoid overshooting again, we only adjust the smallest rank.
|
343
|
+
* We take the largest nodes from the lowest rank 0 and move them
|
344
|
+
* to rank 1. There's guaranteed to be enough rank 0 symbols because
|
345
|
+
* TODO.
|
346
|
+
*/
|
276
347
|
while (totalCost < 0) { /* Sometimes, cost correction overshoot */
|
277
|
-
|
348
|
+
/* special case : no rank 1 symbol (using maxNbBits-1);
|
349
|
+
* let's create one from largest rank 0 (using maxNbBits).
|
350
|
+
*/
|
351
|
+
if (rankLast[1] == noSymbol) {
|
278
352
|
while (huffNode[n].nbBits == maxNbBits) n--;
|
279
353
|
huffNode[n+1].nbBits--;
|
280
354
|
assert(n >= 0);
|
@@ -285,14 +359,16 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
|
|
285
359
|
huffNode[ rankLast[1] + 1 ].nbBits--;
|
286
360
|
rankLast[1]++;
|
287
361
|
totalCost ++;
|
288
|
-
|
362
|
+
}
|
363
|
+
} /* repay normalized cost */
|
364
|
+
} /* there are several too large elements (at least >= 2) */
|
289
365
|
|
290
366
|
return maxNbBits;
|
291
367
|
}
|
292
368
|
|
293
369
|
typedef struct {
|
294
370
|
U32 base;
|
295
|
-
U32
|
371
|
+
U32 curr;
|
296
372
|
} rankPos;
|
297
373
|
|
298
374
|
typedef nodeElt huffNodeTable[HUF_CTABLE_WORKSPACE_SIZE_U32];
|
@@ -304,21 +380,45 @@ typedef struct {
|
|
304
380
|
rankPos rankPosition[RANK_POSITION_TABLE_SIZE];
|
305
381
|
} HUF_buildCTable_wksp_tables;
|
306
382
|
|
383
|
+
/**
|
384
|
+
* HUF_sort():
|
385
|
+
* Sorts the symbols [0, maxSymbolValue] by count[symbol] in decreasing order.
|
386
|
+
*
|
387
|
+
* @param[out] huffNode Sorted symbols by decreasing count. Only members `.count` and `.byte` are filled.
|
388
|
+
* Must have (maxSymbolValue + 1) entries.
|
389
|
+
* @param[in] count Histogram of the symbols.
|
390
|
+
* @param[in] maxSymbolValue Maximum symbol value.
|
391
|
+
* @param rankPosition This is a scratch workspace. Must have RANK_POSITION_TABLE_SIZE entries.
|
392
|
+
*/
|
307
393
|
static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValue, rankPos* rankPosition)
|
308
394
|
{
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
395
|
+
int n;
|
396
|
+
int const maxSymbolValue1 = (int)maxSymbolValue + 1;
|
397
|
+
|
398
|
+
/* Compute base and set curr to base.
|
399
|
+
* For symbol s let lowerRank = BIT_highbit32(count[n]+1) and rank = lowerRank + 1.
|
400
|
+
* Then 2^lowerRank <= count[n]+1 <= 2^rank.
|
401
|
+
* We attribute each symbol to lowerRank's base value, because we want to know where
|
402
|
+
* each rank begins in the output, so for rank R we want to count ranks R+1 and above.
|
403
|
+
*/
|
404
|
+
ZSTD_memset(rankPosition, 0, sizeof(*rankPosition) * RANK_POSITION_TABLE_SIZE);
|
405
|
+
for (n = 0; n < maxSymbolValue1; ++n) {
|
406
|
+
U32 lowerRank = BIT_highbit32(count[n] + 1);
|
407
|
+
rankPosition[lowerRank].base++;
|
315
408
|
}
|
316
|
-
|
317
|
-
for (n=
|
318
|
-
|
409
|
+
assert(rankPosition[RANK_POSITION_TABLE_SIZE - 1].base == 0);
|
410
|
+
for (n = RANK_POSITION_TABLE_SIZE - 1; n > 0; --n) {
|
411
|
+
rankPosition[n-1].base += rankPosition[n].base;
|
412
|
+
rankPosition[n-1].curr = rankPosition[n-1].base;
|
413
|
+
}
|
414
|
+
/* Sort */
|
415
|
+
for (n = 0; n < maxSymbolValue1; ++n) {
|
319
416
|
U32 const c = count[n];
|
320
417
|
U32 const r = BIT_highbit32(c+1) + 1;
|
321
|
-
U32 pos = rankPosition[r].
|
418
|
+
U32 pos = rankPosition[r].curr++;
|
419
|
+
/* Insert into the correct position in the rank.
|
420
|
+
* We have at most 256 symbols, so this insertion should be fine.
|
421
|
+
*/
|
322
422
|
while ((pos > rankPosition[r].base) && (c > huffNode[pos-1].count)) {
|
323
423
|
huffNode[pos] = huffNode[pos-1];
|
324
424
|
pos--;
|
@@ -335,28 +435,20 @@ static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValu
|
|
335
435
|
*/
|
336
436
|
#define STARTNODE (HUF_SYMBOLVALUE_MAX+1)
|
337
437
|
|
338
|
-
|
438
|
+
/* HUF_buildTree():
|
439
|
+
* Takes the huffNode array sorted by HUF_sort() and builds an unlimited-depth Huffman tree.
|
440
|
+
*
|
441
|
+
* @param huffNode The array sorted by HUF_sort(). Builds the Huffman tree in this array.
|
442
|
+
* @param maxSymbolValue The maximum symbol value.
|
443
|
+
* @return The smallest node in the Huffman tree (by count).
|
444
|
+
*/
|
445
|
+
static int HUF_buildTree(nodeElt* huffNode, U32 maxSymbolValue)
|
339
446
|
{
|
340
|
-
|
341
|
-
nodeElt* const huffNode0 = wksp_tables->huffNodeTbl;
|
342
|
-
nodeElt* const huffNode = huffNode0+1;
|
447
|
+
nodeElt* const huffNode0 = huffNode - 1;
|
343
448
|
int nonNullRank;
|
344
449
|
int lowS, lowN;
|
345
450
|
int nodeNb = STARTNODE;
|
346
451
|
int n, nodeRoot;
|
347
|
-
|
348
|
-
/* safety checks */
|
349
|
-
if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
|
350
|
-
if (wkspSize < sizeof(HUF_buildCTable_wksp_tables))
|
351
|
-
return ERROR(workSpace_tooSmall);
|
352
|
-
if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
|
353
|
-
if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
|
354
|
-
return ERROR(maxSymbolValue_tooLarge);
|
355
|
-
memset(huffNode0, 0, sizeof(huffNodeTable));
|
356
|
-
|
357
|
-
/* sort, decreasing order */
|
358
|
-
HUF_sort(huffNode, count, maxSymbolValue, wksp_tables->rankPosition);
|
359
|
-
|
360
452
|
/* init for parents */
|
361
453
|
nonNullRank = (int)maxSymbolValue;
|
362
454
|
while(huffNode[nonNullRank].count == 0) nonNullRank--;
|
@@ -383,42 +475,72 @@ size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbo
|
|
383
475
|
for (n=0; n<=nonNullRank; n++)
|
384
476
|
huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
|
385
477
|
|
478
|
+
return nonNullRank;
|
479
|
+
}
|
480
|
+
|
481
|
+
/**
|
482
|
+
* HUF_buildCTableFromTree():
|
483
|
+
* Build the CTable given the Huffman tree in huffNode.
|
484
|
+
*
|
485
|
+
* @param[out] CTable The output Huffman CTable.
|
486
|
+
* @param huffNode The Huffman tree.
|
487
|
+
* @param nonNullRank The last and smallest node in the Huffman tree.
|
488
|
+
* @param maxSymbolValue The maximum symbol value.
|
489
|
+
* @param maxNbBits The exact maximum number of bits used in the Huffman tree.
|
490
|
+
*/
|
491
|
+
static void HUF_buildCTableFromTree(HUF_CElt* CTable, nodeElt const* huffNode, int nonNullRank, U32 maxSymbolValue, U32 maxNbBits)
|
492
|
+
{
|
493
|
+
/* fill result into ctable (val, nbBits) */
|
494
|
+
int n;
|
495
|
+
U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
|
496
|
+
U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
|
497
|
+
int const alphabetSize = (int)(maxSymbolValue + 1);
|
498
|
+
for (n=0; n<=nonNullRank; n++)
|
499
|
+
nbPerRank[huffNode[n].nbBits]++;
|
500
|
+
/* determine starting value per rank */
|
501
|
+
{ U16 min = 0;
|
502
|
+
for (n=(int)maxNbBits; n>0; n--) {
|
503
|
+
valPerRank[n] = min; /* get starting value within each rank */
|
504
|
+
min += nbPerRank[n];
|
505
|
+
min >>= 1;
|
506
|
+
} }
|
507
|
+
for (n=0; n<alphabetSize; n++)
|
508
|
+
CTable[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
|
509
|
+
for (n=0; n<alphabetSize; n++)
|
510
|
+
CTable[n].val = valPerRank[CTable[n].nbBits]++; /* assign value within rank, symbol order */
|
511
|
+
}
|
512
|
+
|
513
|
+
size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize)
|
514
|
+
{
|
515
|
+
HUF_buildCTable_wksp_tables* const wksp_tables = (HUF_buildCTable_wksp_tables*)workSpace;
|
516
|
+
nodeElt* const huffNode0 = wksp_tables->huffNodeTbl;
|
517
|
+
nodeElt* const huffNode = huffNode0+1;
|
518
|
+
int nonNullRank;
|
519
|
+
|
520
|
+
/* safety checks */
|
521
|
+
if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
|
522
|
+
if (wkspSize < sizeof(HUF_buildCTable_wksp_tables))
|
523
|
+
return ERROR(workSpace_tooSmall);
|
524
|
+
if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
|
525
|
+
if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
|
526
|
+
return ERROR(maxSymbolValue_tooLarge);
|
527
|
+
ZSTD_memset(huffNode0, 0, sizeof(huffNodeTable));
|
528
|
+
|
529
|
+
/* sort, decreasing order */
|
530
|
+
HUF_sort(huffNode, count, maxSymbolValue, wksp_tables->rankPosition);
|
531
|
+
|
532
|
+
/* build tree */
|
533
|
+
nonNullRank = HUF_buildTree(huffNode, maxSymbolValue);
|
534
|
+
|
386
535
|
/* enforce maxTableLog */
|
387
536
|
maxNbBits = HUF_setMaxHeight(huffNode, (U32)nonNullRank, maxNbBits);
|
537
|
+
if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */
|
388
538
|
|
389
|
-
|
390
|
-
{ U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
|
391
|
-
U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
|
392
|
-
int const alphabetSize = (int)(maxSymbolValue + 1);
|
393
|
-
if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */
|
394
|
-
for (n=0; n<=nonNullRank; n++)
|
395
|
-
nbPerRank[huffNode[n].nbBits]++;
|
396
|
-
/* determine stating value per rank */
|
397
|
-
{ U16 min = 0;
|
398
|
-
for (n=(int)maxNbBits; n>0; n--) {
|
399
|
-
valPerRank[n] = min; /* get starting value within each rank */
|
400
|
-
min += nbPerRank[n];
|
401
|
-
min >>= 1;
|
402
|
-
} }
|
403
|
-
for (n=0; n<alphabetSize; n++)
|
404
|
-
tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
|
405
|
-
for (n=0; n<alphabetSize; n++)
|
406
|
-
tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */
|
407
|
-
}
|
539
|
+
HUF_buildCTableFromTree(tree, huffNode, nonNullRank, maxSymbolValue, maxNbBits);
|
408
540
|
|
409
541
|
return maxNbBits;
|
410
542
|
}
|
411
543
|
|
412
|
-
/** HUF_buildCTable() :
|
413
|
-
* @return : maxNbBits
|
414
|
-
* Note : count is used before tree is written, so they can safely overlap
|
415
|
-
*/
|
416
|
-
size_t HUF_buildCTable (HUF_CElt* tree, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits)
|
417
|
-
{
|
418
|
-
HUF_buildCTable_wksp_tables workspace;
|
419
|
-
return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, &workspace, sizeof(workspace));
|
420
|
-
}
|
421
|
-
|
422
544
|
size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue)
|
423
545
|
{
|
424
546
|
size_t nbBits = 0;
|
@@ -629,29 +751,33 @@ static size_t HUF_compressCTable_internal(
|
|
629
751
|
typedef struct {
|
630
752
|
unsigned count[HUF_SYMBOLVALUE_MAX + 1];
|
631
753
|
HUF_CElt CTable[HUF_SYMBOLVALUE_MAX + 1];
|
632
|
-
|
754
|
+
union {
|
755
|
+
HUF_buildCTable_wksp_tables buildCTable_wksp;
|
756
|
+
HUF_WriteCTableWksp writeCTable_wksp;
|
757
|
+
} wksps;
|
633
758
|
} HUF_compress_tables_t;
|
634
759
|
|
635
760
|
/* HUF_compress_internal() :
|
636
|
-
* `
|
761
|
+
* `workSpace_align4` must be aligned on 4-bytes boundaries,
|
762
|
+
* and occupies the same space as a table of HUF_WORKSPACE_SIZE_U32 unsigned */
|
637
763
|
static size_t
|
638
764
|
HUF_compress_internal (void* dst, size_t dstSize,
|
639
765
|
const void* src, size_t srcSize,
|
640
766
|
unsigned maxSymbolValue, unsigned huffLog,
|
641
767
|
HUF_nbStreams_e nbStreams,
|
642
|
-
void*
|
768
|
+
void* workSpace_align4, size_t wkspSize,
|
643
769
|
HUF_CElt* oldHufTable, HUF_repeat* repeat, int preferRepeat,
|
644
770
|
const int bmi2)
|
645
771
|
{
|
646
|
-
HUF_compress_tables_t* const table = (HUF_compress_tables_t*)
|
772
|
+
HUF_compress_tables_t* const table = (HUF_compress_tables_t*)workSpace_align4;
|
647
773
|
BYTE* const ostart = (BYTE*)dst;
|
648
774
|
BYTE* const oend = ostart + dstSize;
|
649
775
|
BYTE* op = ostart;
|
650
776
|
|
651
777
|
HUF_STATIC_ASSERT(sizeof(*table) <= HUF_WORKSPACE_SIZE);
|
778
|
+
assert(((size_t)workSpace_align4 & 3) == 0); /* must be aligned on 4-bytes boundaries */
|
652
779
|
|
653
780
|
/* checks & inits */
|
654
|
-
if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
|
655
781
|
if (wkspSize < HUF_WORKSPACE_SIZE) return ERROR(workSpace_tooSmall);
|
656
782
|
if (!srcSize) return 0; /* Uncompressed */
|
657
783
|
if (!dstSize) return 0; /* cannot fit anything within dst budget */
|
@@ -669,7 +795,7 @@ HUF_compress_internal (void* dst, size_t dstSize,
|
|
669
795
|
}
|
670
796
|
|
671
797
|
/* Scan input and build symbol stats */
|
672
|
-
{ CHECK_V_F(largest, HIST_count_wksp (table->count, &maxSymbolValue, (const BYTE*)src, srcSize,
|
798
|
+
{ CHECK_V_F(largest, HIST_count_wksp (table->count, &maxSymbolValue, (const BYTE*)src, srcSize, workSpace_align4, wkspSize) );
|
673
799
|
if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; } /* single symbol, rle */
|
674
800
|
if (largest <= (srcSize >> 7)+4) return 0; /* heuristic : probably not compressible enough */
|
675
801
|
}
|
@@ -691,16 +817,17 @@ HUF_compress_internal (void* dst, size_t dstSize,
|
|
691
817
|
huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue);
|
692
818
|
{ size_t const maxBits = HUF_buildCTable_wksp(table->CTable, table->count,
|
693
819
|
maxSymbolValue, huffLog,
|
694
|
-
&table->buildCTable_wksp, sizeof(table->buildCTable_wksp));
|
820
|
+
&table->wksps.buildCTable_wksp, sizeof(table->wksps.buildCTable_wksp));
|
695
821
|
CHECK_F(maxBits);
|
696
822
|
huffLog = (U32)maxBits;
|
697
823
|
/* Zero unused symbols in CTable, so we can check it for validity */
|
698
|
-
|
824
|
+
ZSTD_memset(table->CTable + (maxSymbolValue + 1), 0,
|
699
825
|
sizeof(table->CTable) - ((maxSymbolValue + 1) * sizeof(HUF_CElt)));
|
700
826
|
}
|
701
827
|
|
702
828
|
/* Write table description header */
|
703
|
-
{ CHECK_V_F(hSize,
|
829
|
+
{ CHECK_V_F(hSize, HUF_writeCTable_wksp(op, dstSize, table->CTable, maxSymbolValue, huffLog,
|
830
|
+
&table->wksps.writeCTable_wksp, sizeof(table->wksps.writeCTable_wksp)) );
|
704
831
|
/* Check if using previous huffman table is beneficial */
|
705
832
|
if (repeat && *repeat != HUF_repeat_none) {
|
706
833
|
size_t const oldSize = HUF_estimateCompressedSize(oldHufTable, table->count, maxSymbolValue);
|
@@ -716,7 +843,7 @@ HUF_compress_internal (void* dst, size_t dstSize,
|
|
716
843
|
op += hSize;
|
717
844
|
if (repeat) { *repeat = HUF_repeat_none; }
|
718
845
|
if (oldHufTable)
|
719
|
-
|
846
|
+
ZSTD_memcpy(oldHufTable, table->CTable, sizeof(table->CTable)); /* Save new table */
|
720
847
|
}
|
721
848
|
return HUF_compressCTable_internal(ostart, op, oend,
|
722
849
|
src, srcSize,
|
@@ -747,14 +874,6 @@ size_t HUF_compress1X_repeat (void* dst, size_t dstSize,
|
|
747
874
|
repeat, preferRepeat, bmi2);
|
748
875
|
}
|
749
876
|
|
750
|
-
size_t HUF_compress1X (void* dst, size_t dstSize,
|
751
|
-
const void* src, size_t srcSize,
|
752
|
-
unsigned maxSymbolValue, unsigned huffLog)
|
753
|
-
{
|
754
|
-
unsigned workSpace[HUF_WORKSPACE_SIZE_U32];
|
755
|
-
return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace));
|
756
|
-
}
|
757
|
-
|
758
877
|
/* HUF_compress4X_repeat():
|
759
878
|
* compress input using 4 streams.
|
760
879
|
* provide workspace to generate compression tables */
|
@@ -784,6 +903,25 @@ size_t HUF_compress4X_repeat (void* dst, size_t dstSize,
|
|
784
903
|
hufTable, repeat, preferRepeat, bmi2);
|
785
904
|
}
|
786
905
|
|
906
|
+
#ifndef ZSTD_NO_UNUSED_FUNCTIONS
|
907
|
+
/** HUF_buildCTable() :
|
908
|
+
* @return : maxNbBits
|
909
|
+
* Note : count is used before tree is written, so they can safely overlap
|
910
|
+
*/
|
911
|
+
size_t HUF_buildCTable (HUF_CElt* tree, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits)
|
912
|
+
{
|
913
|
+
HUF_buildCTable_wksp_tables workspace;
|
914
|
+
return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, &workspace, sizeof(workspace));
|
915
|
+
}
|
916
|
+
|
917
|
+
size_t HUF_compress1X (void* dst, size_t dstSize,
|
918
|
+
const void* src, size_t srcSize,
|
919
|
+
unsigned maxSymbolValue, unsigned huffLog)
|
920
|
+
{
|
921
|
+
unsigned workSpace[HUF_WORKSPACE_SIZE_U32];
|
922
|
+
return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace));
|
923
|
+
}
|
924
|
+
|
787
925
|
size_t HUF_compress2 (void* dst, size_t dstSize,
|
788
926
|
const void* src, size_t srcSize,
|
789
927
|
unsigned maxSymbolValue, unsigned huffLog)
|
@@ -796,3 +934,4 @@ size_t HUF_compress (void* dst, size_t maxDstSize, const void* src, size_t srcSi
|
|
796
934
|
{
|
797
935
|
return HUF_compress2(dst, maxDstSize, src, srcSize, 255, HUF_TABLELOG_DEFAULT);
|
798
936
|
}
|
937
|
+
#endif
|