1*01826a49SYabin Cui /* ******************************************************************
2*01826a49SYabin Cui * huff0 huffman decoder,
3*01826a49SYabin Cui * part of Finite State Entropy library
4*01826a49SYabin Cui * Copyright (c) Meta Platforms, Inc. and affiliates.
5*01826a49SYabin Cui *
6*01826a49SYabin Cui * You can contact the author at :
7*01826a49SYabin Cui * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
8*01826a49SYabin Cui *
9*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the
10*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree).
12*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses.
13*01826a49SYabin Cui ****************************************************************** */
14*01826a49SYabin Cui
15*01826a49SYabin Cui /* **************************************************************
16*01826a49SYabin Cui * Dependencies
17*01826a49SYabin Cui ****************************************************************/
18*01826a49SYabin Cui #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset */
19*01826a49SYabin Cui #include "../common/compiler.h"
20*01826a49SYabin Cui #include "../common/bitstream.h" /* BIT_* */
21*01826a49SYabin Cui #include "../common/fse.h" /* to compress headers */
22*01826a49SYabin Cui #include "../common/huf.h"
23*01826a49SYabin Cui #include "../common/error_private.h"
24*01826a49SYabin Cui #include "../common/zstd_internal.h"
25*01826a49SYabin Cui #include "../common/bits.h" /* ZSTD_highbit32, ZSTD_countTrailingZeros64 */
26*01826a49SYabin Cui
27*01826a49SYabin Cui /* **************************************************************
28*01826a49SYabin Cui * Constants
29*01826a49SYabin Cui ****************************************************************/
30*01826a49SYabin Cui
31*01826a49SYabin Cui #define HUF_DECODER_FAST_TABLELOG 11
32*01826a49SYabin Cui
33*01826a49SYabin Cui /* **************************************************************
34*01826a49SYabin Cui * Macros
35*01826a49SYabin Cui ****************************************************************/
36*01826a49SYabin Cui
37*01826a49SYabin Cui #ifdef HUF_DISABLE_FAST_DECODE
38*01826a49SYabin Cui # define HUF_ENABLE_FAST_DECODE 0
39*01826a49SYabin Cui #else
40*01826a49SYabin Cui # define HUF_ENABLE_FAST_DECODE 1
41*01826a49SYabin Cui #endif
42*01826a49SYabin Cui
43*01826a49SYabin Cui /* These two optional macros force the use one way or another of the two
44*01826a49SYabin Cui * Huffman decompression implementations. You can't force in both directions
45*01826a49SYabin Cui * at the same time.
46*01826a49SYabin Cui */
47*01826a49SYabin Cui #if defined(HUF_FORCE_DECOMPRESS_X1) && \
48*01826a49SYabin Cui defined(HUF_FORCE_DECOMPRESS_X2)
49*01826a49SYabin Cui #error "Cannot force the use of the X1 and X2 decoders at the same time!"
50*01826a49SYabin Cui #endif
51*01826a49SYabin Cui
52*01826a49SYabin Cui /* When DYNAMIC_BMI2 is enabled, fast decoders are only called when bmi2 is
53*01826a49SYabin Cui * supported at runtime, so we can add the BMI2 target attribute.
54*01826a49SYabin Cui * When it is disabled, we will still get BMI2 if it is enabled statically.
55*01826a49SYabin Cui */
56*01826a49SYabin Cui #if DYNAMIC_BMI2
57*01826a49SYabin Cui # define HUF_FAST_BMI2_ATTRS BMI2_TARGET_ATTRIBUTE
58*01826a49SYabin Cui #else
59*01826a49SYabin Cui # define HUF_FAST_BMI2_ATTRS
60*01826a49SYabin Cui #endif
61*01826a49SYabin Cui
62*01826a49SYabin Cui #ifdef __cplusplus
63*01826a49SYabin Cui # define HUF_EXTERN_C extern "C"
64*01826a49SYabin Cui #else
65*01826a49SYabin Cui # define HUF_EXTERN_C
66*01826a49SYabin Cui #endif
67*01826a49SYabin Cui #define HUF_ASM_DECL HUF_EXTERN_C
68*01826a49SYabin Cui
69*01826a49SYabin Cui #if DYNAMIC_BMI2
70*01826a49SYabin Cui # define HUF_NEED_BMI2_FUNCTION 1
71*01826a49SYabin Cui #else
72*01826a49SYabin Cui # define HUF_NEED_BMI2_FUNCTION 0
73*01826a49SYabin Cui #endif
74*01826a49SYabin Cui
75*01826a49SYabin Cui /* **************************************************************
76*01826a49SYabin Cui * Error Management
77*01826a49SYabin Cui ****************************************************************/
78*01826a49SYabin Cui #define HUF_isError ERR_isError
79*01826a49SYabin Cui
80*01826a49SYabin Cui
81*01826a49SYabin Cui /* **************************************************************
82*01826a49SYabin Cui * Byte alignment for workSpace management
83*01826a49SYabin Cui ****************************************************************/
84*01826a49SYabin Cui #define HUF_ALIGN(x, a) HUF_ALIGN_MASK((x), (a) - 1)
85*01826a49SYabin Cui #define HUF_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
86*01826a49SYabin Cui
87*01826a49SYabin Cui
88*01826a49SYabin Cui /* **************************************************************
89*01826a49SYabin Cui * BMI2 Variant Wrappers
90*01826a49SYabin Cui ****************************************************************/
91*01826a49SYabin Cui typedef size_t (*HUF_DecompressUsingDTableFn)(void *dst, size_t dstSize,
92*01826a49SYabin Cui const void *cSrc,
93*01826a49SYabin Cui size_t cSrcSize,
94*01826a49SYabin Cui const HUF_DTable *DTable);
95*01826a49SYabin Cui
96*01826a49SYabin Cui #if DYNAMIC_BMI2
97*01826a49SYabin Cui
98*01826a49SYabin Cui #define HUF_DGEN(fn) \
99*01826a49SYabin Cui \
100*01826a49SYabin Cui static size_t fn##_default( \
101*01826a49SYabin Cui void* dst, size_t dstSize, \
102*01826a49SYabin Cui const void* cSrc, size_t cSrcSize, \
103*01826a49SYabin Cui const HUF_DTable* DTable) \
104*01826a49SYabin Cui { \
105*01826a49SYabin Cui return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
106*01826a49SYabin Cui } \
107*01826a49SYabin Cui \
108*01826a49SYabin Cui static BMI2_TARGET_ATTRIBUTE size_t fn##_bmi2( \
109*01826a49SYabin Cui void* dst, size_t dstSize, \
110*01826a49SYabin Cui const void* cSrc, size_t cSrcSize, \
111*01826a49SYabin Cui const HUF_DTable* DTable) \
112*01826a49SYabin Cui { \
113*01826a49SYabin Cui return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
114*01826a49SYabin Cui } \
115*01826a49SYabin Cui \
116*01826a49SYabin Cui static size_t fn(void* dst, size_t dstSize, void const* cSrc, \
117*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable, int flags) \
118*01826a49SYabin Cui { \
119*01826a49SYabin Cui if (flags & HUF_flags_bmi2) { \
120*01826a49SYabin Cui return fn##_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); \
121*01826a49SYabin Cui } \
122*01826a49SYabin Cui return fn##_default(dst, dstSize, cSrc, cSrcSize, DTable); \
123*01826a49SYabin Cui }
124*01826a49SYabin Cui
125*01826a49SYabin Cui #else
126*01826a49SYabin Cui
127*01826a49SYabin Cui #define HUF_DGEN(fn) \
128*01826a49SYabin Cui static size_t fn(void* dst, size_t dstSize, void const* cSrc, \
129*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable, int flags) \
130*01826a49SYabin Cui { \
131*01826a49SYabin Cui (void)flags; \
132*01826a49SYabin Cui return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \
133*01826a49SYabin Cui }
134*01826a49SYabin Cui
135*01826a49SYabin Cui #endif
136*01826a49SYabin Cui
137*01826a49SYabin Cui
138*01826a49SYabin Cui /*-***************************/
139*01826a49SYabin Cui /* generic DTableDesc */
140*01826a49SYabin Cui /*-***************************/
141*01826a49SYabin Cui typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc;
142*01826a49SYabin Cui
HUF_getDTableDesc(const HUF_DTable * table)143*01826a49SYabin Cui static DTableDesc HUF_getDTableDesc(const HUF_DTable* table)
144*01826a49SYabin Cui {
145*01826a49SYabin Cui DTableDesc dtd;
146*01826a49SYabin Cui ZSTD_memcpy(&dtd, table, sizeof(dtd));
147*01826a49SYabin Cui return dtd;
148*01826a49SYabin Cui }
149*01826a49SYabin Cui
HUF_initFastDStream(BYTE const * ip)150*01826a49SYabin Cui static size_t HUF_initFastDStream(BYTE const* ip) {
151*01826a49SYabin Cui BYTE const lastByte = ip[7];
152*01826a49SYabin Cui size_t const bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0;
153*01826a49SYabin Cui size_t const value = MEM_readLEST(ip) | 1;
154*01826a49SYabin Cui assert(bitsConsumed <= 8);
155*01826a49SYabin Cui assert(sizeof(size_t) == 8);
156*01826a49SYabin Cui return value << bitsConsumed;
157*01826a49SYabin Cui }
158*01826a49SYabin Cui
159*01826a49SYabin Cui
160*01826a49SYabin Cui /**
161*01826a49SYabin Cui * The input/output arguments to the Huffman fast decoding loop:
162*01826a49SYabin Cui *
163*01826a49SYabin Cui * ip [in/out] - The input pointers, must be updated to reflect what is consumed.
164*01826a49SYabin Cui * op [in/out] - The output pointers, must be updated to reflect what is written.
165*01826a49SYabin Cui * bits [in/out] - The bitstream containers, must be updated to reflect the current state.
166*01826a49SYabin Cui * dt [in] - The decoding table.
167*01826a49SYabin Cui * ilowest [in] - The beginning of the valid range of the input. Decoders may read
168*01826a49SYabin Cui * down to this pointer. It may be below iend[0].
169*01826a49SYabin Cui * oend [in] - The end of the output stream. op[3] must not cross oend.
170*01826a49SYabin Cui * iend [in] - The end of each input stream. ip[i] may cross iend[i],
171*01826a49SYabin Cui * as long as it is above ilowest, but that indicates corruption.
172*01826a49SYabin Cui */
173*01826a49SYabin Cui typedef struct {
174*01826a49SYabin Cui BYTE const* ip[4];
175*01826a49SYabin Cui BYTE* op[4];
176*01826a49SYabin Cui U64 bits[4];
177*01826a49SYabin Cui void const* dt;
178*01826a49SYabin Cui BYTE const* ilowest;
179*01826a49SYabin Cui BYTE* oend;
180*01826a49SYabin Cui BYTE const* iend[4];
181*01826a49SYabin Cui } HUF_DecompressFastArgs;
182*01826a49SYabin Cui
183*01826a49SYabin Cui typedef void (*HUF_DecompressFastLoopFn)(HUF_DecompressFastArgs*);
184*01826a49SYabin Cui
185*01826a49SYabin Cui /**
186*01826a49SYabin Cui * Initializes args for the fast decoding loop.
187*01826a49SYabin Cui * @returns 1 on success
188*01826a49SYabin Cui * 0 if the fallback implementation should be used.
189*01826a49SYabin Cui * Or an error code on failure.
190*01826a49SYabin Cui */
HUF_DecompressFastArgs_init(HUF_DecompressFastArgs * args,void * dst,size_t dstSize,void const * src,size_t srcSize,const HUF_DTable * DTable)191*01826a49SYabin Cui static size_t HUF_DecompressFastArgs_init(HUF_DecompressFastArgs* args, void* dst, size_t dstSize, void const* src, size_t srcSize, const HUF_DTable* DTable)
192*01826a49SYabin Cui {
193*01826a49SYabin Cui void const* dt = DTable + 1;
194*01826a49SYabin Cui U32 const dtLog = HUF_getDTableDesc(DTable).tableLog;
195*01826a49SYabin Cui
196*01826a49SYabin Cui const BYTE* const istart = (const BYTE*)src;
197*01826a49SYabin Cui
198*01826a49SYabin Cui BYTE* const oend = ZSTD_maybeNullPtrAdd((BYTE*)dst, dstSize);
199*01826a49SYabin Cui
200*01826a49SYabin Cui /* The fast decoding loop assumes 64-bit little-endian.
201*01826a49SYabin Cui * This condition is false on x32.
202*01826a49SYabin Cui */
203*01826a49SYabin Cui if (!MEM_isLittleEndian() || MEM_32bits())
204*01826a49SYabin Cui return 0;
205*01826a49SYabin Cui
206*01826a49SYabin Cui /* Avoid nullptr addition */
207*01826a49SYabin Cui if (dstSize == 0)
208*01826a49SYabin Cui return 0;
209*01826a49SYabin Cui assert(dst != NULL);
210*01826a49SYabin Cui
211*01826a49SYabin Cui /* strict minimum : jump table + 1 byte per stream */
212*01826a49SYabin Cui if (srcSize < 10)
213*01826a49SYabin Cui return ERROR(corruption_detected);
214*01826a49SYabin Cui
215*01826a49SYabin Cui /* Must have at least 8 bytes per stream because we don't handle initializing smaller bit containers.
216*01826a49SYabin Cui * If table log is not correct at this point, fallback to the old decoder.
217*01826a49SYabin Cui * On small inputs we don't have enough data to trigger the fast loop, so use the old decoder.
218*01826a49SYabin Cui */
219*01826a49SYabin Cui if (dtLog != HUF_DECODER_FAST_TABLELOG)
220*01826a49SYabin Cui return 0;
221*01826a49SYabin Cui
222*01826a49SYabin Cui /* Read the jump table. */
223*01826a49SYabin Cui {
224*01826a49SYabin Cui size_t const length1 = MEM_readLE16(istart);
225*01826a49SYabin Cui size_t const length2 = MEM_readLE16(istart+2);
226*01826a49SYabin Cui size_t const length3 = MEM_readLE16(istart+4);
227*01826a49SYabin Cui size_t const length4 = srcSize - (length1 + length2 + length3 + 6);
228*01826a49SYabin Cui args->iend[0] = istart + 6; /* jumpTable */
229*01826a49SYabin Cui args->iend[1] = args->iend[0] + length1;
230*01826a49SYabin Cui args->iend[2] = args->iend[1] + length2;
231*01826a49SYabin Cui args->iend[3] = args->iend[2] + length3;
232*01826a49SYabin Cui
233*01826a49SYabin Cui /* HUF_initFastDStream() requires this, and this small of an input
234*01826a49SYabin Cui * won't benefit from the ASM loop anyways.
235*01826a49SYabin Cui */
236*01826a49SYabin Cui if (length1 < 8 || length2 < 8 || length3 < 8 || length4 < 8)
237*01826a49SYabin Cui return 0;
238*01826a49SYabin Cui if (length4 > srcSize) return ERROR(corruption_detected); /* overflow */
239*01826a49SYabin Cui }
240*01826a49SYabin Cui /* ip[] contains the position that is currently loaded into bits[]. */
241*01826a49SYabin Cui args->ip[0] = args->iend[1] - sizeof(U64);
242*01826a49SYabin Cui args->ip[1] = args->iend[2] - sizeof(U64);
243*01826a49SYabin Cui args->ip[2] = args->iend[3] - sizeof(U64);
244*01826a49SYabin Cui args->ip[3] = (BYTE const*)src + srcSize - sizeof(U64);
245*01826a49SYabin Cui
246*01826a49SYabin Cui /* op[] contains the output pointers. */
247*01826a49SYabin Cui args->op[0] = (BYTE*)dst;
248*01826a49SYabin Cui args->op[1] = args->op[0] + (dstSize+3)/4;
249*01826a49SYabin Cui args->op[2] = args->op[1] + (dstSize+3)/4;
250*01826a49SYabin Cui args->op[3] = args->op[2] + (dstSize+3)/4;
251*01826a49SYabin Cui
252*01826a49SYabin Cui /* No point to call the ASM loop for tiny outputs. */
253*01826a49SYabin Cui if (args->op[3] >= oend)
254*01826a49SYabin Cui return 0;
255*01826a49SYabin Cui
256*01826a49SYabin Cui /* bits[] is the bit container.
257*01826a49SYabin Cui * It is read from the MSB down to the LSB.
258*01826a49SYabin Cui * It is shifted left as it is read, and zeros are
259*01826a49SYabin Cui * shifted in. After the lowest valid bit a 1 is
260*01826a49SYabin Cui * set, so that CountTrailingZeros(bits[]) can be used
261*01826a49SYabin Cui * to count how many bits we've consumed.
262*01826a49SYabin Cui */
263*01826a49SYabin Cui args->bits[0] = HUF_initFastDStream(args->ip[0]);
264*01826a49SYabin Cui args->bits[1] = HUF_initFastDStream(args->ip[1]);
265*01826a49SYabin Cui args->bits[2] = HUF_initFastDStream(args->ip[2]);
266*01826a49SYabin Cui args->bits[3] = HUF_initFastDStream(args->ip[3]);
267*01826a49SYabin Cui
268*01826a49SYabin Cui /* The decoders must be sure to never read beyond ilowest.
269*01826a49SYabin Cui * This is lower than iend[0], but allowing decoders to read
270*01826a49SYabin Cui * down to ilowest can allow an extra iteration or two in the
271*01826a49SYabin Cui * fast loop.
272*01826a49SYabin Cui */
273*01826a49SYabin Cui args->ilowest = istart;
274*01826a49SYabin Cui
275*01826a49SYabin Cui args->oend = oend;
276*01826a49SYabin Cui args->dt = dt;
277*01826a49SYabin Cui
278*01826a49SYabin Cui return 1;
279*01826a49SYabin Cui }
280*01826a49SYabin Cui
HUF_initRemainingDStream(BIT_DStream_t * bit,HUF_DecompressFastArgs const * args,int stream,BYTE * segmentEnd)281*01826a49SYabin Cui static size_t HUF_initRemainingDStream(BIT_DStream_t* bit, HUF_DecompressFastArgs const* args, int stream, BYTE* segmentEnd)
282*01826a49SYabin Cui {
283*01826a49SYabin Cui /* Validate that we haven't overwritten. */
284*01826a49SYabin Cui if (args->op[stream] > segmentEnd)
285*01826a49SYabin Cui return ERROR(corruption_detected);
286*01826a49SYabin Cui /* Validate that we haven't read beyond iend[].
287*01826a49SYabin Cui * Note that ip[] may be < iend[] because the MSB is
288*01826a49SYabin Cui * the next bit to read, and we may have consumed 100%
289*01826a49SYabin Cui * of the stream, so down to iend[i] - 8 is valid.
290*01826a49SYabin Cui */
291*01826a49SYabin Cui if (args->ip[stream] < args->iend[stream] - 8)
292*01826a49SYabin Cui return ERROR(corruption_detected);
293*01826a49SYabin Cui
294*01826a49SYabin Cui /* Construct the BIT_DStream_t. */
295*01826a49SYabin Cui assert(sizeof(size_t) == 8);
296*01826a49SYabin Cui bit->bitContainer = MEM_readLEST(args->ip[stream]);
297*01826a49SYabin Cui bit->bitsConsumed = ZSTD_countTrailingZeros64(args->bits[stream]);
298*01826a49SYabin Cui bit->start = (const char*)args->ilowest;
299*01826a49SYabin Cui bit->limitPtr = bit->start + sizeof(size_t);
300*01826a49SYabin Cui bit->ptr = (const char*)args->ip[stream];
301*01826a49SYabin Cui
302*01826a49SYabin Cui return 0;
303*01826a49SYabin Cui }
304*01826a49SYabin Cui
305*01826a49SYabin Cui /* Calls X(N) for each stream 0, 1, 2, 3. */
306*01826a49SYabin Cui #define HUF_4X_FOR_EACH_STREAM(X) \
307*01826a49SYabin Cui do { \
308*01826a49SYabin Cui X(0); \
309*01826a49SYabin Cui X(1); \
310*01826a49SYabin Cui X(2); \
311*01826a49SYabin Cui X(3); \
312*01826a49SYabin Cui } while (0)
313*01826a49SYabin Cui
314*01826a49SYabin Cui /* Calls X(N, var) for each stream 0, 1, 2, 3. */
315*01826a49SYabin Cui #define HUF_4X_FOR_EACH_STREAM_WITH_VAR(X, var) \
316*01826a49SYabin Cui do { \
317*01826a49SYabin Cui X(0, (var)); \
318*01826a49SYabin Cui X(1, (var)); \
319*01826a49SYabin Cui X(2, (var)); \
320*01826a49SYabin Cui X(3, (var)); \
321*01826a49SYabin Cui } while (0)
322*01826a49SYabin Cui
323*01826a49SYabin Cui
324*01826a49SYabin Cui #ifndef HUF_FORCE_DECOMPRESS_X2
325*01826a49SYabin Cui
326*01826a49SYabin Cui /*-***************************/
327*01826a49SYabin Cui /* single-symbol decoding */
328*01826a49SYabin Cui /*-***************************/
329*01826a49SYabin Cui typedef struct { BYTE nbBits; BYTE byte; } HUF_DEltX1; /* single-symbol decoding */
330*01826a49SYabin Cui
331*01826a49SYabin Cui /**
332*01826a49SYabin Cui * Packs 4 HUF_DEltX1 structs into a U64. This is used to lay down 4 entries at
333*01826a49SYabin Cui * a time.
334*01826a49SYabin Cui */
HUF_DEltX1_set4(BYTE symbol,BYTE nbBits)335*01826a49SYabin Cui static U64 HUF_DEltX1_set4(BYTE symbol, BYTE nbBits) {
336*01826a49SYabin Cui U64 D4;
337*01826a49SYabin Cui if (MEM_isLittleEndian()) {
338*01826a49SYabin Cui D4 = (U64)((symbol << 8) + nbBits);
339*01826a49SYabin Cui } else {
340*01826a49SYabin Cui D4 = (U64)(symbol + (nbBits << 8));
341*01826a49SYabin Cui }
342*01826a49SYabin Cui assert(D4 < (1U << 16));
343*01826a49SYabin Cui D4 *= 0x0001000100010001ULL;
344*01826a49SYabin Cui return D4;
345*01826a49SYabin Cui }
346*01826a49SYabin Cui
347*01826a49SYabin Cui /**
348*01826a49SYabin Cui * Increase the tableLog to targetTableLog and rescales the stats.
349*01826a49SYabin Cui * If tableLog > targetTableLog this is a no-op.
350*01826a49SYabin Cui * @returns New tableLog
351*01826a49SYabin Cui */
HUF_rescaleStats(BYTE * huffWeight,U32 * rankVal,U32 nbSymbols,U32 tableLog,U32 targetTableLog)352*01826a49SYabin Cui static U32 HUF_rescaleStats(BYTE* huffWeight, U32* rankVal, U32 nbSymbols, U32 tableLog, U32 targetTableLog)
353*01826a49SYabin Cui {
354*01826a49SYabin Cui if (tableLog > targetTableLog)
355*01826a49SYabin Cui return tableLog;
356*01826a49SYabin Cui if (tableLog < targetTableLog) {
357*01826a49SYabin Cui U32 const scale = targetTableLog - tableLog;
358*01826a49SYabin Cui U32 s;
359*01826a49SYabin Cui /* Increase the weight for all non-zero probability symbols by scale. */
360*01826a49SYabin Cui for (s = 0; s < nbSymbols; ++s) {
361*01826a49SYabin Cui huffWeight[s] += (BYTE)((huffWeight[s] == 0) ? 0 : scale);
362*01826a49SYabin Cui }
363*01826a49SYabin Cui /* Update rankVal to reflect the new weights.
364*01826a49SYabin Cui * All weights except 0 get moved to weight + scale.
365*01826a49SYabin Cui * Weights [1, scale] are empty.
366*01826a49SYabin Cui */
367*01826a49SYabin Cui for (s = targetTableLog; s > scale; --s) {
368*01826a49SYabin Cui rankVal[s] = rankVal[s - scale];
369*01826a49SYabin Cui }
370*01826a49SYabin Cui for (s = scale; s > 0; --s) {
371*01826a49SYabin Cui rankVal[s] = 0;
372*01826a49SYabin Cui }
373*01826a49SYabin Cui }
374*01826a49SYabin Cui return targetTableLog;
375*01826a49SYabin Cui }
376*01826a49SYabin Cui
377*01826a49SYabin Cui typedef struct {
378*01826a49SYabin Cui U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1];
379*01826a49SYabin Cui U32 rankStart[HUF_TABLELOG_ABSOLUTEMAX + 1];
380*01826a49SYabin Cui U32 statsWksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
381*01826a49SYabin Cui BYTE symbols[HUF_SYMBOLVALUE_MAX + 1];
382*01826a49SYabin Cui BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1];
383*01826a49SYabin Cui } HUF_ReadDTableX1_Workspace;
384*01826a49SYabin Cui
HUF_readDTableX1_wksp(HUF_DTable * DTable,const void * src,size_t srcSize,void * workSpace,size_t wkspSize,int flags)385*01826a49SYabin Cui size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags)
386*01826a49SYabin Cui {
387*01826a49SYabin Cui U32 tableLog = 0;
388*01826a49SYabin Cui U32 nbSymbols = 0;
389*01826a49SYabin Cui size_t iSize;
390*01826a49SYabin Cui void* const dtPtr = DTable + 1;
391*01826a49SYabin Cui HUF_DEltX1* const dt = (HUF_DEltX1*)dtPtr;
392*01826a49SYabin Cui HUF_ReadDTableX1_Workspace* wksp = (HUF_ReadDTableX1_Workspace*)workSpace;
393*01826a49SYabin Cui
394*01826a49SYabin Cui DEBUG_STATIC_ASSERT(HUF_DECOMPRESS_WORKSPACE_SIZE >= sizeof(*wksp));
395*01826a49SYabin Cui if (sizeof(*wksp) > wkspSize) return ERROR(tableLog_tooLarge);
396*01826a49SYabin Cui
397*01826a49SYabin Cui DEBUG_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
398*01826a49SYabin Cui /* ZSTD_memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
399*01826a49SYabin Cui
400*01826a49SYabin Cui iSize = HUF_readStats_wksp(wksp->huffWeight, HUF_SYMBOLVALUE_MAX + 1, wksp->rankVal, &nbSymbols, &tableLog, src, srcSize, wksp->statsWksp, sizeof(wksp->statsWksp), flags);
401*01826a49SYabin Cui if (HUF_isError(iSize)) return iSize;
402*01826a49SYabin Cui
403*01826a49SYabin Cui
404*01826a49SYabin Cui /* Table header */
405*01826a49SYabin Cui { DTableDesc dtd = HUF_getDTableDesc(DTable);
406*01826a49SYabin Cui U32 const maxTableLog = dtd.maxTableLog + 1;
407*01826a49SYabin Cui U32 const targetTableLog = MIN(maxTableLog, HUF_DECODER_FAST_TABLELOG);
408*01826a49SYabin Cui tableLog = HUF_rescaleStats(wksp->huffWeight, wksp->rankVal, nbSymbols, tableLog, targetTableLog);
409*01826a49SYabin Cui if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */
410*01826a49SYabin Cui dtd.tableType = 0;
411*01826a49SYabin Cui dtd.tableLog = (BYTE)tableLog;
412*01826a49SYabin Cui ZSTD_memcpy(DTable, &dtd, sizeof(dtd));
413*01826a49SYabin Cui }
414*01826a49SYabin Cui
415*01826a49SYabin Cui /* Compute symbols and rankStart given rankVal:
416*01826a49SYabin Cui *
417*01826a49SYabin Cui * rankVal already contains the number of values of each weight.
418*01826a49SYabin Cui *
419*01826a49SYabin Cui * symbols contains the symbols ordered by weight. First are the rankVal[0]
420*01826a49SYabin Cui * weight 0 symbols, followed by the rankVal[1] weight 1 symbols, and so on.
421*01826a49SYabin Cui * symbols[0] is filled (but unused) to avoid a branch.
422*01826a49SYabin Cui *
423*01826a49SYabin Cui * rankStart contains the offset where each rank belongs in the DTable.
424*01826a49SYabin Cui * rankStart[0] is not filled because there are no entries in the table for
425*01826a49SYabin Cui * weight 0.
426*01826a49SYabin Cui */
427*01826a49SYabin Cui { int n;
428*01826a49SYabin Cui U32 nextRankStart = 0;
429*01826a49SYabin Cui int const unroll = 4;
430*01826a49SYabin Cui int const nLimit = (int)nbSymbols - unroll + 1;
431*01826a49SYabin Cui for (n=0; n<(int)tableLog+1; n++) {
432*01826a49SYabin Cui U32 const curr = nextRankStart;
433*01826a49SYabin Cui nextRankStart += wksp->rankVal[n];
434*01826a49SYabin Cui wksp->rankStart[n] = curr;
435*01826a49SYabin Cui }
436*01826a49SYabin Cui for (n=0; n < nLimit; n += unroll) {
437*01826a49SYabin Cui int u;
438*01826a49SYabin Cui for (u=0; u < unroll; ++u) {
439*01826a49SYabin Cui size_t const w = wksp->huffWeight[n+u];
440*01826a49SYabin Cui wksp->symbols[wksp->rankStart[w]++] = (BYTE)(n+u);
441*01826a49SYabin Cui }
442*01826a49SYabin Cui }
443*01826a49SYabin Cui for (; n < (int)nbSymbols; ++n) {
444*01826a49SYabin Cui size_t const w = wksp->huffWeight[n];
445*01826a49SYabin Cui wksp->symbols[wksp->rankStart[w]++] = (BYTE)n;
446*01826a49SYabin Cui }
447*01826a49SYabin Cui }
448*01826a49SYabin Cui
449*01826a49SYabin Cui /* fill DTable
450*01826a49SYabin Cui * We fill all entries of each weight in order.
451*01826a49SYabin Cui * That way length is a constant for each iteration of the outer loop.
452*01826a49SYabin Cui * We can switch based on the length to a different inner loop which is
453*01826a49SYabin Cui * optimized for that particular case.
454*01826a49SYabin Cui */
455*01826a49SYabin Cui { U32 w;
456*01826a49SYabin Cui int symbol = wksp->rankVal[0];
457*01826a49SYabin Cui int rankStart = 0;
458*01826a49SYabin Cui for (w=1; w<tableLog+1; ++w) {
459*01826a49SYabin Cui int const symbolCount = wksp->rankVal[w];
460*01826a49SYabin Cui int const length = (1 << w) >> 1;
461*01826a49SYabin Cui int uStart = rankStart;
462*01826a49SYabin Cui BYTE const nbBits = (BYTE)(tableLog + 1 - w);
463*01826a49SYabin Cui int s;
464*01826a49SYabin Cui int u;
465*01826a49SYabin Cui switch (length) {
466*01826a49SYabin Cui case 1:
467*01826a49SYabin Cui for (s=0; s<symbolCount; ++s) {
468*01826a49SYabin Cui HUF_DEltX1 D;
469*01826a49SYabin Cui D.byte = wksp->symbols[symbol + s];
470*01826a49SYabin Cui D.nbBits = nbBits;
471*01826a49SYabin Cui dt[uStart] = D;
472*01826a49SYabin Cui uStart += 1;
473*01826a49SYabin Cui }
474*01826a49SYabin Cui break;
475*01826a49SYabin Cui case 2:
476*01826a49SYabin Cui for (s=0; s<symbolCount; ++s) {
477*01826a49SYabin Cui HUF_DEltX1 D;
478*01826a49SYabin Cui D.byte = wksp->symbols[symbol + s];
479*01826a49SYabin Cui D.nbBits = nbBits;
480*01826a49SYabin Cui dt[uStart+0] = D;
481*01826a49SYabin Cui dt[uStart+1] = D;
482*01826a49SYabin Cui uStart += 2;
483*01826a49SYabin Cui }
484*01826a49SYabin Cui break;
485*01826a49SYabin Cui case 4:
486*01826a49SYabin Cui for (s=0; s<symbolCount; ++s) {
487*01826a49SYabin Cui U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);
488*01826a49SYabin Cui MEM_write64(dt + uStart, D4);
489*01826a49SYabin Cui uStart += 4;
490*01826a49SYabin Cui }
491*01826a49SYabin Cui break;
492*01826a49SYabin Cui case 8:
493*01826a49SYabin Cui for (s=0; s<symbolCount; ++s) {
494*01826a49SYabin Cui U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);
495*01826a49SYabin Cui MEM_write64(dt + uStart, D4);
496*01826a49SYabin Cui MEM_write64(dt + uStart + 4, D4);
497*01826a49SYabin Cui uStart += 8;
498*01826a49SYabin Cui }
499*01826a49SYabin Cui break;
500*01826a49SYabin Cui default:
501*01826a49SYabin Cui for (s=0; s<symbolCount; ++s) {
502*01826a49SYabin Cui U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);
503*01826a49SYabin Cui for (u=0; u < length; u += 16) {
504*01826a49SYabin Cui MEM_write64(dt + uStart + u + 0, D4);
505*01826a49SYabin Cui MEM_write64(dt + uStart + u + 4, D4);
506*01826a49SYabin Cui MEM_write64(dt + uStart + u + 8, D4);
507*01826a49SYabin Cui MEM_write64(dt + uStart + u + 12, D4);
508*01826a49SYabin Cui }
509*01826a49SYabin Cui assert(u == length);
510*01826a49SYabin Cui uStart += length;
511*01826a49SYabin Cui }
512*01826a49SYabin Cui break;
513*01826a49SYabin Cui }
514*01826a49SYabin Cui symbol += symbolCount;
515*01826a49SYabin Cui rankStart += symbolCount * length;
516*01826a49SYabin Cui }
517*01826a49SYabin Cui }
518*01826a49SYabin Cui return iSize;
519*01826a49SYabin Cui }
520*01826a49SYabin Cui
521*01826a49SYabin Cui FORCE_INLINE_TEMPLATE BYTE
HUF_decodeSymbolX1(BIT_DStream_t * Dstream,const HUF_DEltX1 * dt,const U32 dtLog)522*01826a49SYabin Cui HUF_decodeSymbolX1(BIT_DStream_t* Dstream, const HUF_DEltX1* dt, const U32 dtLog)
523*01826a49SYabin Cui {
524*01826a49SYabin Cui size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
525*01826a49SYabin Cui BYTE const c = dt[val].byte;
526*01826a49SYabin Cui BIT_skipBits(Dstream, dt[val].nbBits);
527*01826a49SYabin Cui return c;
528*01826a49SYabin Cui }
529*01826a49SYabin Cui
530*01826a49SYabin Cui #define HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr) \
531*01826a49SYabin Cui do { *ptr++ = HUF_decodeSymbolX1(DStreamPtr, dt, dtLog); } while (0)
532*01826a49SYabin Cui
533*01826a49SYabin Cui #define HUF_DECODE_SYMBOLX1_1(ptr, DStreamPtr) \
534*01826a49SYabin Cui do { \
535*01826a49SYabin Cui if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
536*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr); \
537*01826a49SYabin Cui } while (0)
538*01826a49SYabin Cui
539*01826a49SYabin Cui #define HUF_DECODE_SYMBOLX1_2(ptr, DStreamPtr) \
540*01826a49SYabin Cui do { \
541*01826a49SYabin Cui if (MEM_64bits()) \
542*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr); \
543*01826a49SYabin Cui } while (0)
544*01826a49SYabin Cui
545*01826a49SYabin Cui HINT_INLINE size_t
HUF_decodeStreamX1(BYTE * p,BIT_DStream_t * const bitDPtr,BYTE * const pEnd,const HUF_DEltX1 * const dt,const U32 dtLog)546*01826a49SYabin Cui HUF_decodeStreamX1(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX1* const dt, const U32 dtLog)
547*01826a49SYabin Cui {
548*01826a49SYabin Cui BYTE* const pStart = p;
549*01826a49SYabin Cui
550*01826a49SYabin Cui /* up to 4 symbols at a time */
551*01826a49SYabin Cui if ((pEnd - p) > 3) {
552*01826a49SYabin Cui while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-3)) {
553*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(p, bitDPtr);
554*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_1(p, bitDPtr);
555*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(p, bitDPtr);
556*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(p, bitDPtr);
557*01826a49SYabin Cui }
558*01826a49SYabin Cui } else {
559*01826a49SYabin Cui BIT_reloadDStream(bitDPtr);
560*01826a49SYabin Cui }
561*01826a49SYabin Cui
562*01826a49SYabin Cui /* [0-3] symbols remaining */
563*01826a49SYabin Cui if (MEM_32bits())
564*01826a49SYabin Cui while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd))
565*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(p, bitDPtr);
566*01826a49SYabin Cui
567*01826a49SYabin Cui /* no more data to retrieve from bitstream, no need to reload */
568*01826a49SYabin Cui while (p < pEnd)
569*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(p, bitDPtr);
570*01826a49SYabin Cui
571*01826a49SYabin Cui return (size_t)(pEnd-pStart);
572*01826a49SYabin Cui }
573*01826a49SYabin Cui
574*01826a49SYabin Cui FORCE_INLINE_TEMPLATE size_t
HUF_decompress1X1_usingDTable_internal_body(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable)575*01826a49SYabin Cui HUF_decompress1X1_usingDTable_internal_body(
576*01826a49SYabin Cui void* dst, size_t dstSize,
577*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
578*01826a49SYabin Cui const HUF_DTable* DTable)
579*01826a49SYabin Cui {
580*01826a49SYabin Cui BYTE* op = (BYTE*)dst;
581*01826a49SYabin Cui BYTE* const oend = ZSTD_maybeNullPtrAdd(op, dstSize);
582*01826a49SYabin Cui const void* dtPtr = DTable + 1;
583*01826a49SYabin Cui const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;
584*01826a49SYabin Cui BIT_DStream_t bitD;
585*01826a49SYabin Cui DTableDesc const dtd = HUF_getDTableDesc(DTable);
586*01826a49SYabin Cui U32 const dtLog = dtd.tableLog;
587*01826a49SYabin Cui
588*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );
589*01826a49SYabin Cui
590*01826a49SYabin Cui HUF_decodeStreamX1(op, &bitD, oend, dt, dtLog);
591*01826a49SYabin Cui
592*01826a49SYabin Cui if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
593*01826a49SYabin Cui
594*01826a49SYabin Cui return dstSize;
595*01826a49SYabin Cui }
596*01826a49SYabin Cui
597*01826a49SYabin Cui /* HUF_decompress4X1_usingDTable_internal_body():
598*01826a49SYabin Cui * Conditions :
599*01826a49SYabin Cui * @dstSize >= 6
600*01826a49SYabin Cui */
601*01826a49SYabin Cui FORCE_INLINE_TEMPLATE size_t
HUF_decompress4X1_usingDTable_internal_body(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable)602*01826a49SYabin Cui HUF_decompress4X1_usingDTable_internal_body(
603*01826a49SYabin Cui void* dst, size_t dstSize,
604*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
605*01826a49SYabin Cui const HUF_DTable* DTable)
606*01826a49SYabin Cui {
607*01826a49SYabin Cui /* Check */
608*01826a49SYabin Cui if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
609*01826a49SYabin Cui if (dstSize < 6) return ERROR(corruption_detected); /* stream 4-split doesn't work */
610*01826a49SYabin Cui
611*01826a49SYabin Cui { const BYTE* const istart = (const BYTE*) cSrc;
612*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
613*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
614*01826a49SYabin Cui BYTE* const olimit = oend - 3;
615*01826a49SYabin Cui const void* const dtPtr = DTable + 1;
616*01826a49SYabin Cui const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;
617*01826a49SYabin Cui
618*01826a49SYabin Cui /* Init */
619*01826a49SYabin Cui BIT_DStream_t bitD1;
620*01826a49SYabin Cui BIT_DStream_t bitD2;
621*01826a49SYabin Cui BIT_DStream_t bitD3;
622*01826a49SYabin Cui BIT_DStream_t bitD4;
623*01826a49SYabin Cui size_t const length1 = MEM_readLE16(istart);
624*01826a49SYabin Cui size_t const length2 = MEM_readLE16(istart+2);
625*01826a49SYabin Cui size_t const length3 = MEM_readLE16(istart+4);
626*01826a49SYabin Cui size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
627*01826a49SYabin Cui const BYTE* const istart1 = istart + 6; /* jumpTable */
628*01826a49SYabin Cui const BYTE* const istart2 = istart1 + length1;
629*01826a49SYabin Cui const BYTE* const istart3 = istart2 + length2;
630*01826a49SYabin Cui const BYTE* const istart4 = istart3 + length3;
631*01826a49SYabin Cui const size_t segmentSize = (dstSize+3) / 4;
632*01826a49SYabin Cui BYTE* const opStart2 = ostart + segmentSize;
633*01826a49SYabin Cui BYTE* const opStart3 = opStart2 + segmentSize;
634*01826a49SYabin Cui BYTE* const opStart4 = opStart3 + segmentSize;
635*01826a49SYabin Cui BYTE* op1 = ostart;
636*01826a49SYabin Cui BYTE* op2 = opStart2;
637*01826a49SYabin Cui BYTE* op3 = opStart3;
638*01826a49SYabin Cui BYTE* op4 = opStart4;
639*01826a49SYabin Cui DTableDesc const dtd = HUF_getDTableDesc(DTable);
640*01826a49SYabin Cui U32 const dtLog = dtd.tableLog;
641*01826a49SYabin Cui U32 endSignal = 1;
642*01826a49SYabin Cui
643*01826a49SYabin Cui if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
644*01826a49SYabin Cui if (opStart4 > oend) return ERROR(corruption_detected); /* overflow */
645*01826a49SYabin Cui assert(dstSize >= 6); /* validated above */
646*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );
647*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );
648*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );
649*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );
650*01826a49SYabin Cui
651*01826a49SYabin Cui /* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */
652*01826a49SYabin Cui if ((size_t)(oend - op4) >= sizeof(size_t)) {
653*01826a49SYabin Cui for ( ; (endSignal) & (op4 < olimit) ; ) {
654*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op1, &bitD1);
655*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op2, &bitD2);
656*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op3, &bitD3);
657*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op4, &bitD4);
658*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_1(op1, &bitD1);
659*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_1(op2, &bitD2);
660*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_1(op3, &bitD3);
661*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_1(op4, &bitD4);
662*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op1, &bitD1);
663*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op2, &bitD2);
664*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op3, &bitD3);
665*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_2(op4, &bitD4);
666*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(op1, &bitD1);
667*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(op2, &bitD2);
668*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(op3, &bitD3);
669*01826a49SYabin Cui HUF_DECODE_SYMBOLX1_0(op4, &bitD4);
670*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;
671*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;
672*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;
673*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;
674*01826a49SYabin Cui }
675*01826a49SYabin Cui }
676*01826a49SYabin Cui
677*01826a49SYabin Cui /* check corruption */
678*01826a49SYabin Cui /* note : should not be necessary : op# advance in lock step, and we control op4.
679*01826a49SYabin Cui * but curiously, binary generated by gcc 7.2 & 7.3 with -mbmi2 runs faster when >=1 test is present */
680*01826a49SYabin Cui if (op1 > opStart2) return ERROR(corruption_detected);
681*01826a49SYabin Cui if (op2 > opStart3) return ERROR(corruption_detected);
682*01826a49SYabin Cui if (op3 > opStart4) return ERROR(corruption_detected);
683*01826a49SYabin Cui /* note : op4 supposed already verified within main loop */
684*01826a49SYabin Cui
685*01826a49SYabin Cui /* finish bitStreams one by one */
686*01826a49SYabin Cui HUF_decodeStreamX1(op1, &bitD1, opStart2, dt, dtLog);
687*01826a49SYabin Cui HUF_decodeStreamX1(op2, &bitD2, opStart3, dt, dtLog);
688*01826a49SYabin Cui HUF_decodeStreamX1(op3, &bitD3, opStart4, dt, dtLog);
689*01826a49SYabin Cui HUF_decodeStreamX1(op4, &bitD4, oend, dt, dtLog);
690*01826a49SYabin Cui
691*01826a49SYabin Cui /* check */
692*01826a49SYabin Cui { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
693*01826a49SYabin Cui if (!endCheck) return ERROR(corruption_detected); }
694*01826a49SYabin Cui
695*01826a49SYabin Cui /* decoded size */
696*01826a49SYabin Cui return dstSize;
697*01826a49SYabin Cui }
698*01826a49SYabin Cui }
699*01826a49SYabin Cui
700*01826a49SYabin Cui #if HUF_NEED_BMI2_FUNCTION
701*01826a49SYabin Cui static BMI2_TARGET_ATTRIBUTE
HUF_decompress4X1_usingDTable_internal_bmi2(void * dst,size_t dstSize,void const * cSrc,size_t cSrcSize,HUF_DTable const * DTable)702*01826a49SYabin Cui size_t HUF_decompress4X1_usingDTable_internal_bmi2(void* dst, size_t dstSize, void const* cSrc,
703*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable) {
704*01826a49SYabin Cui return HUF_decompress4X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);
705*01826a49SYabin Cui }
706*01826a49SYabin Cui #endif
707*01826a49SYabin Cui
708*01826a49SYabin Cui static
HUF_decompress4X1_usingDTable_internal_default(void * dst,size_t dstSize,void const * cSrc,size_t cSrcSize,HUF_DTable const * DTable)709*01826a49SYabin Cui size_t HUF_decompress4X1_usingDTable_internal_default(void* dst, size_t dstSize, void const* cSrc,
710*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable) {
711*01826a49SYabin Cui return HUF_decompress4X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);
712*01826a49SYabin Cui }
713*01826a49SYabin Cui
714*01826a49SYabin Cui #if ZSTD_ENABLE_ASM_X86_64_BMI2
715*01826a49SYabin Cui
716*01826a49SYabin Cui HUF_ASM_DECL void HUF_decompress4X1_usingDTable_internal_fast_asm_loop(HUF_DecompressFastArgs* args) ZSTDLIB_HIDDEN;
717*01826a49SYabin Cui
718*01826a49SYabin Cui #endif
719*01826a49SYabin Cui
720*01826a49SYabin Cui static HUF_FAST_BMI2_ATTRS
HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs * args)721*01826a49SYabin Cui void HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs* args)
722*01826a49SYabin Cui {
723*01826a49SYabin Cui U64 bits[4];
724*01826a49SYabin Cui BYTE const* ip[4];
725*01826a49SYabin Cui BYTE* op[4];
726*01826a49SYabin Cui U16 const* const dtable = (U16 const*)args->dt;
727*01826a49SYabin Cui BYTE* const oend = args->oend;
728*01826a49SYabin Cui BYTE const* const ilowest = args->ilowest;
729*01826a49SYabin Cui
730*01826a49SYabin Cui /* Copy the arguments to local variables */
731*01826a49SYabin Cui ZSTD_memcpy(&bits, &args->bits, sizeof(bits));
732*01826a49SYabin Cui ZSTD_memcpy((void*)(&ip), &args->ip, sizeof(ip));
733*01826a49SYabin Cui ZSTD_memcpy(&op, &args->op, sizeof(op));
734*01826a49SYabin Cui
735*01826a49SYabin Cui assert(MEM_isLittleEndian());
736*01826a49SYabin Cui assert(!MEM_32bits());
737*01826a49SYabin Cui
738*01826a49SYabin Cui for (;;) {
739*01826a49SYabin Cui BYTE* olimit;
740*01826a49SYabin Cui int stream;
741*01826a49SYabin Cui
742*01826a49SYabin Cui /* Assert loop preconditions */
743*01826a49SYabin Cui #ifndef NDEBUG
744*01826a49SYabin Cui for (stream = 0; stream < 4; ++stream) {
745*01826a49SYabin Cui assert(op[stream] <= (stream == 3 ? oend : op[stream + 1]));
746*01826a49SYabin Cui assert(ip[stream] >= ilowest);
747*01826a49SYabin Cui }
748*01826a49SYabin Cui #endif
749*01826a49SYabin Cui /* Compute olimit */
750*01826a49SYabin Cui {
751*01826a49SYabin Cui /* Each iteration produces 5 output symbols per stream */
752*01826a49SYabin Cui size_t const oiters = (size_t)(oend - op[3]) / 5;
753*01826a49SYabin Cui /* Each iteration consumes up to 11 bits * 5 = 55 bits < 7 bytes
754*01826a49SYabin Cui * per stream.
755*01826a49SYabin Cui */
756*01826a49SYabin Cui size_t const iiters = (size_t)(ip[0] - ilowest) / 7;
757*01826a49SYabin Cui /* We can safely run iters iterations before running bounds checks */
758*01826a49SYabin Cui size_t const iters = MIN(oiters, iiters);
759*01826a49SYabin Cui size_t const symbols = iters * 5;
760*01826a49SYabin Cui
761*01826a49SYabin Cui /* We can simply check that op[3] < olimit, instead of checking all
762*01826a49SYabin Cui * of our bounds, since we can't hit the other bounds until we've run
763*01826a49SYabin Cui * iters iterations, which only happens when op[3] == olimit.
764*01826a49SYabin Cui */
765*01826a49SYabin Cui olimit = op[3] + symbols;
766*01826a49SYabin Cui
767*01826a49SYabin Cui /* Exit fast decoding loop once we reach the end. */
768*01826a49SYabin Cui if (op[3] == olimit)
769*01826a49SYabin Cui break;
770*01826a49SYabin Cui
771*01826a49SYabin Cui /* Exit the decoding loop if any input pointer has crossed the
772*01826a49SYabin Cui * previous one. This indicates corruption, and a precondition
773*01826a49SYabin Cui * to our loop is that ip[i] >= ip[0].
774*01826a49SYabin Cui */
775*01826a49SYabin Cui for (stream = 1; stream < 4; ++stream) {
776*01826a49SYabin Cui if (ip[stream] < ip[stream - 1])
777*01826a49SYabin Cui goto _out;
778*01826a49SYabin Cui }
779*01826a49SYabin Cui }
780*01826a49SYabin Cui
781*01826a49SYabin Cui #ifndef NDEBUG
782*01826a49SYabin Cui for (stream = 1; stream < 4; ++stream) {
783*01826a49SYabin Cui assert(ip[stream] >= ip[stream - 1]);
784*01826a49SYabin Cui }
785*01826a49SYabin Cui #endif
786*01826a49SYabin Cui
787*01826a49SYabin Cui #define HUF_4X1_DECODE_SYMBOL(_stream, _symbol) \
788*01826a49SYabin Cui do { \
789*01826a49SYabin Cui int const index = (int)(bits[(_stream)] >> 53); \
790*01826a49SYabin Cui int const entry = (int)dtable[index]; \
791*01826a49SYabin Cui bits[(_stream)] <<= (entry & 0x3F); \
792*01826a49SYabin Cui op[(_stream)][(_symbol)] = (BYTE)((entry >> 8) & 0xFF); \
793*01826a49SYabin Cui } while (0)
794*01826a49SYabin Cui
795*01826a49SYabin Cui #define HUF_4X1_RELOAD_STREAM(_stream) \
796*01826a49SYabin Cui do { \
797*01826a49SYabin Cui int const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
798*01826a49SYabin Cui int const nbBits = ctz & 7; \
799*01826a49SYabin Cui int const nbBytes = ctz >> 3; \
800*01826a49SYabin Cui op[(_stream)] += 5; \
801*01826a49SYabin Cui ip[(_stream)] -= nbBytes; \
802*01826a49SYabin Cui bits[(_stream)] = MEM_read64(ip[(_stream)]) | 1; \
803*01826a49SYabin Cui bits[(_stream)] <<= nbBits; \
804*01826a49SYabin Cui } while (0)
805*01826a49SYabin Cui
806*01826a49SYabin Cui /* Manually unroll the loop because compilers don't consistently
807*01826a49SYabin Cui * unroll the inner loops, which destroys performance.
808*01826a49SYabin Cui */
809*01826a49SYabin Cui do {
810*01826a49SYabin Cui /* Decode 5 symbols in each of the 4 streams */
811*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 0);
812*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 1);
813*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 2);
814*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 3);
815*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 4);
816*01826a49SYabin Cui
817*01826a49SYabin Cui /* Reload each of the 4 the bitstreams */
818*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM(HUF_4X1_RELOAD_STREAM);
819*01826a49SYabin Cui } while (op[3] < olimit);
820*01826a49SYabin Cui
821*01826a49SYabin Cui #undef HUF_4X1_DECODE_SYMBOL
822*01826a49SYabin Cui #undef HUF_4X1_RELOAD_STREAM
823*01826a49SYabin Cui }
824*01826a49SYabin Cui
825*01826a49SYabin Cui _out:
826*01826a49SYabin Cui
827*01826a49SYabin Cui /* Save the final values of each of the state variables back to args. */
828*01826a49SYabin Cui ZSTD_memcpy(&args->bits, &bits, sizeof(bits));
829*01826a49SYabin Cui ZSTD_memcpy((void*)(&args->ip), &ip, sizeof(ip));
830*01826a49SYabin Cui ZSTD_memcpy(&args->op, &op, sizeof(op));
831*01826a49SYabin Cui }
832*01826a49SYabin Cui
833*01826a49SYabin Cui /**
834*01826a49SYabin Cui * @returns @p dstSize on success (>= 6)
835*01826a49SYabin Cui * 0 if the fallback implementation should be used
836*01826a49SYabin Cui * An error if an error occurred
837*01826a49SYabin Cui */
838*01826a49SYabin Cui static HUF_FAST_BMI2_ATTRS
839*01826a49SYabin Cui size_t
HUF_decompress4X1_usingDTable_internal_fast(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable,HUF_DecompressFastLoopFn loopFn)840*01826a49SYabin Cui HUF_decompress4X1_usingDTable_internal_fast(
841*01826a49SYabin Cui void* dst, size_t dstSize,
842*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
843*01826a49SYabin Cui const HUF_DTable* DTable,
844*01826a49SYabin Cui HUF_DecompressFastLoopFn loopFn)
845*01826a49SYabin Cui {
846*01826a49SYabin Cui void const* dt = DTable + 1;
847*01826a49SYabin Cui BYTE const* const ilowest = (BYTE const*)cSrc;
848*01826a49SYabin Cui BYTE* const oend = ZSTD_maybeNullPtrAdd((BYTE*)dst, dstSize);
849*01826a49SYabin Cui HUF_DecompressFastArgs args;
850*01826a49SYabin Cui { size_t const ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable);
851*01826a49SYabin Cui FORWARD_IF_ERROR(ret, "Failed to init fast loop args");
852*01826a49SYabin Cui if (ret == 0)
853*01826a49SYabin Cui return 0;
854*01826a49SYabin Cui }
855*01826a49SYabin Cui
856*01826a49SYabin Cui assert(args.ip[0] >= args.ilowest);
857*01826a49SYabin Cui loopFn(&args);
858*01826a49SYabin Cui
859*01826a49SYabin Cui /* Our loop guarantees that ip[] >= ilowest and that we haven't
860*01826a49SYabin Cui * overwritten any op[].
861*01826a49SYabin Cui */
862*01826a49SYabin Cui assert(args.ip[0] >= ilowest);
863*01826a49SYabin Cui assert(args.ip[0] >= ilowest);
864*01826a49SYabin Cui assert(args.ip[1] >= ilowest);
865*01826a49SYabin Cui assert(args.ip[2] >= ilowest);
866*01826a49SYabin Cui assert(args.ip[3] >= ilowest);
867*01826a49SYabin Cui assert(args.op[3] <= oend);
868*01826a49SYabin Cui
869*01826a49SYabin Cui assert(ilowest == args.ilowest);
870*01826a49SYabin Cui assert(ilowest + 6 == args.iend[0]);
871*01826a49SYabin Cui (void)ilowest;
872*01826a49SYabin Cui
873*01826a49SYabin Cui /* finish bit streams one by one. */
874*01826a49SYabin Cui { size_t const segmentSize = (dstSize+3) / 4;
875*01826a49SYabin Cui BYTE* segmentEnd = (BYTE*)dst;
876*01826a49SYabin Cui int i;
877*01826a49SYabin Cui for (i = 0; i < 4; ++i) {
878*01826a49SYabin Cui BIT_DStream_t bit;
879*01826a49SYabin Cui if (segmentSize <= (size_t)(oend - segmentEnd))
880*01826a49SYabin Cui segmentEnd += segmentSize;
881*01826a49SYabin Cui else
882*01826a49SYabin Cui segmentEnd = oend;
883*01826a49SYabin Cui FORWARD_IF_ERROR(HUF_initRemainingDStream(&bit, &args, i, segmentEnd), "corruption");
884*01826a49SYabin Cui /* Decompress and validate that we've produced exactly the expected length. */
885*01826a49SYabin Cui args.op[i] += HUF_decodeStreamX1(args.op[i], &bit, segmentEnd, (HUF_DEltX1 const*)dt, HUF_DECODER_FAST_TABLELOG);
886*01826a49SYabin Cui if (args.op[i] != segmentEnd) return ERROR(corruption_detected);
887*01826a49SYabin Cui }
888*01826a49SYabin Cui }
889*01826a49SYabin Cui
890*01826a49SYabin Cui /* decoded size */
891*01826a49SYabin Cui assert(dstSize != 0);
892*01826a49SYabin Cui return dstSize;
893*01826a49SYabin Cui }
894*01826a49SYabin Cui
HUF_DGEN(HUF_decompress1X1_usingDTable_internal)895*01826a49SYabin Cui HUF_DGEN(HUF_decompress1X1_usingDTable_internal)
896*01826a49SYabin Cui
897*01826a49SYabin Cui static size_t HUF_decompress4X1_usingDTable_internal(void* dst, size_t dstSize, void const* cSrc,
898*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable, int flags)
899*01826a49SYabin Cui {
900*01826a49SYabin Cui HUF_DecompressUsingDTableFn fallbackFn = HUF_decompress4X1_usingDTable_internal_default;
901*01826a49SYabin Cui HUF_DecompressFastLoopFn loopFn = HUF_decompress4X1_usingDTable_internal_fast_c_loop;
902*01826a49SYabin Cui
903*01826a49SYabin Cui #if DYNAMIC_BMI2
904*01826a49SYabin Cui if (flags & HUF_flags_bmi2) {
905*01826a49SYabin Cui fallbackFn = HUF_decompress4X1_usingDTable_internal_bmi2;
906*01826a49SYabin Cui # if ZSTD_ENABLE_ASM_X86_64_BMI2
907*01826a49SYabin Cui if (!(flags & HUF_flags_disableAsm)) {
908*01826a49SYabin Cui loopFn = HUF_decompress4X1_usingDTable_internal_fast_asm_loop;
909*01826a49SYabin Cui }
910*01826a49SYabin Cui # endif
911*01826a49SYabin Cui } else {
912*01826a49SYabin Cui return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);
913*01826a49SYabin Cui }
914*01826a49SYabin Cui #endif
915*01826a49SYabin Cui
916*01826a49SYabin Cui #if ZSTD_ENABLE_ASM_X86_64_BMI2 && defined(__BMI2__)
917*01826a49SYabin Cui if (!(flags & HUF_flags_disableAsm)) {
918*01826a49SYabin Cui loopFn = HUF_decompress4X1_usingDTable_internal_fast_asm_loop;
919*01826a49SYabin Cui }
920*01826a49SYabin Cui #endif
921*01826a49SYabin Cui
922*01826a49SYabin Cui if (HUF_ENABLE_FAST_DECODE && !(flags & HUF_flags_disableFast)) {
923*01826a49SYabin Cui size_t const ret = HUF_decompress4X1_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn);
924*01826a49SYabin Cui if (ret != 0)
925*01826a49SYabin Cui return ret;
926*01826a49SYabin Cui }
927*01826a49SYabin Cui return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);
928*01826a49SYabin Cui }
929*01826a49SYabin Cui
HUF_decompress4X1_DCtx_wksp(HUF_DTable * dctx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,void * workSpace,size_t wkspSize,int flags)930*01826a49SYabin Cui static size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
931*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
932*01826a49SYabin Cui void* workSpace, size_t wkspSize, int flags)
933*01826a49SYabin Cui {
934*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
935*01826a49SYabin Cui
936*01826a49SYabin Cui size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize, flags);
937*01826a49SYabin Cui if (HUF_isError(hSize)) return hSize;
938*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
939*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
940*01826a49SYabin Cui
941*01826a49SYabin Cui return HUF_decompress4X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, flags);
942*01826a49SYabin Cui }
943*01826a49SYabin Cui
944*01826a49SYabin Cui #endif /* HUF_FORCE_DECOMPRESS_X2 */
945*01826a49SYabin Cui
946*01826a49SYabin Cui
947*01826a49SYabin Cui #ifndef HUF_FORCE_DECOMPRESS_X1
948*01826a49SYabin Cui
949*01826a49SYabin Cui /* *************************/
950*01826a49SYabin Cui /* double-symbols decoding */
951*01826a49SYabin Cui /* *************************/
952*01826a49SYabin Cui
953*01826a49SYabin Cui typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX2; /* double-symbols decoding */
954*01826a49SYabin Cui typedef struct { BYTE symbol; } sortedSymbol_t;
955*01826a49SYabin Cui typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];
956*01826a49SYabin Cui typedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX];
957*01826a49SYabin Cui
958*01826a49SYabin Cui /**
959*01826a49SYabin Cui * Constructs a HUF_DEltX2 in a U32.
960*01826a49SYabin Cui */
HUF_buildDEltX2U32(U32 symbol,U32 nbBits,U32 baseSeq,int level)961*01826a49SYabin Cui static U32 HUF_buildDEltX2U32(U32 symbol, U32 nbBits, U32 baseSeq, int level)
962*01826a49SYabin Cui {
963*01826a49SYabin Cui U32 seq;
964*01826a49SYabin Cui DEBUG_STATIC_ASSERT(offsetof(HUF_DEltX2, sequence) == 0);
965*01826a49SYabin Cui DEBUG_STATIC_ASSERT(offsetof(HUF_DEltX2, nbBits) == 2);
966*01826a49SYabin Cui DEBUG_STATIC_ASSERT(offsetof(HUF_DEltX2, length) == 3);
967*01826a49SYabin Cui DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U32));
968*01826a49SYabin Cui if (MEM_isLittleEndian()) {
969*01826a49SYabin Cui seq = level == 1 ? symbol : (baseSeq + (symbol << 8));
970*01826a49SYabin Cui return seq + (nbBits << 16) + ((U32)level << 24);
971*01826a49SYabin Cui } else {
972*01826a49SYabin Cui seq = level == 1 ? (symbol << 8) : ((baseSeq << 8) + symbol);
973*01826a49SYabin Cui return (seq << 16) + (nbBits << 8) + (U32)level;
974*01826a49SYabin Cui }
975*01826a49SYabin Cui }
976*01826a49SYabin Cui
977*01826a49SYabin Cui /**
978*01826a49SYabin Cui * Constructs a HUF_DEltX2.
979*01826a49SYabin Cui */
HUF_buildDEltX2(U32 symbol,U32 nbBits,U32 baseSeq,int level)980*01826a49SYabin Cui static HUF_DEltX2 HUF_buildDEltX2(U32 symbol, U32 nbBits, U32 baseSeq, int level)
981*01826a49SYabin Cui {
982*01826a49SYabin Cui HUF_DEltX2 DElt;
983*01826a49SYabin Cui U32 const val = HUF_buildDEltX2U32(symbol, nbBits, baseSeq, level);
984*01826a49SYabin Cui DEBUG_STATIC_ASSERT(sizeof(DElt) == sizeof(val));
985*01826a49SYabin Cui ZSTD_memcpy(&DElt, &val, sizeof(val));
986*01826a49SYabin Cui return DElt;
987*01826a49SYabin Cui }
988*01826a49SYabin Cui
989*01826a49SYabin Cui /**
990*01826a49SYabin Cui * Constructs 2 HUF_DEltX2s and packs them into a U64.
991*01826a49SYabin Cui */
HUF_buildDEltX2U64(U32 symbol,U32 nbBits,U16 baseSeq,int level)992*01826a49SYabin Cui static U64 HUF_buildDEltX2U64(U32 symbol, U32 nbBits, U16 baseSeq, int level)
993*01826a49SYabin Cui {
994*01826a49SYabin Cui U32 DElt = HUF_buildDEltX2U32(symbol, nbBits, baseSeq, level);
995*01826a49SYabin Cui return (U64)DElt + ((U64)DElt << 32);
996*01826a49SYabin Cui }
997*01826a49SYabin Cui
998*01826a49SYabin Cui /**
999*01826a49SYabin Cui * Fills the DTable rank with all the symbols from [begin, end) that are each
1000*01826a49SYabin Cui * nbBits long.
1001*01826a49SYabin Cui *
1002*01826a49SYabin Cui * @param DTableRank The start of the rank in the DTable.
1003*01826a49SYabin Cui * @param begin The first symbol to fill (inclusive).
1004*01826a49SYabin Cui * @param end The last symbol to fill (exclusive).
1005*01826a49SYabin Cui * @param nbBits Each symbol is nbBits long.
1006*01826a49SYabin Cui * @param tableLog The table log.
1007*01826a49SYabin Cui * @param baseSeq If level == 1 { 0 } else { the first level symbol }
1008*01826a49SYabin Cui * @param level The level in the table. Must be 1 or 2.
1009*01826a49SYabin Cui */
HUF_fillDTableX2ForWeight(HUF_DEltX2 * DTableRank,sortedSymbol_t const * begin,sortedSymbol_t const * end,U32 nbBits,U32 tableLog,U16 baseSeq,int const level)1010*01826a49SYabin Cui static void HUF_fillDTableX2ForWeight(
1011*01826a49SYabin Cui HUF_DEltX2* DTableRank,
1012*01826a49SYabin Cui sortedSymbol_t const* begin, sortedSymbol_t const* end,
1013*01826a49SYabin Cui U32 nbBits, U32 tableLog,
1014*01826a49SYabin Cui U16 baseSeq, int const level)
1015*01826a49SYabin Cui {
1016*01826a49SYabin Cui U32 const length = 1U << ((tableLog - nbBits) & 0x1F /* quiet static-analyzer */);
1017*01826a49SYabin Cui const sortedSymbol_t* ptr;
1018*01826a49SYabin Cui assert(level >= 1 && level <= 2);
1019*01826a49SYabin Cui switch (length) {
1020*01826a49SYabin Cui case 1:
1021*01826a49SYabin Cui for (ptr = begin; ptr != end; ++ptr) {
1022*01826a49SYabin Cui HUF_DEltX2 const DElt = HUF_buildDEltX2(ptr->symbol, nbBits, baseSeq, level);
1023*01826a49SYabin Cui *DTableRank++ = DElt;
1024*01826a49SYabin Cui }
1025*01826a49SYabin Cui break;
1026*01826a49SYabin Cui case 2:
1027*01826a49SYabin Cui for (ptr = begin; ptr != end; ++ptr) {
1028*01826a49SYabin Cui HUF_DEltX2 const DElt = HUF_buildDEltX2(ptr->symbol, nbBits, baseSeq, level);
1029*01826a49SYabin Cui DTableRank[0] = DElt;
1030*01826a49SYabin Cui DTableRank[1] = DElt;
1031*01826a49SYabin Cui DTableRank += 2;
1032*01826a49SYabin Cui }
1033*01826a49SYabin Cui break;
1034*01826a49SYabin Cui case 4:
1035*01826a49SYabin Cui for (ptr = begin; ptr != end; ++ptr) {
1036*01826a49SYabin Cui U64 const DEltX2 = HUF_buildDEltX2U64(ptr->symbol, nbBits, baseSeq, level);
1037*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 0, &DEltX2, sizeof(DEltX2));
1038*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 2, &DEltX2, sizeof(DEltX2));
1039*01826a49SYabin Cui DTableRank += 4;
1040*01826a49SYabin Cui }
1041*01826a49SYabin Cui break;
1042*01826a49SYabin Cui case 8:
1043*01826a49SYabin Cui for (ptr = begin; ptr != end; ++ptr) {
1044*01826a49SYabin Cui U64 const DEltX2 = HUF_buildDEltX2U64(ptr->symbol, nbBits, baseSeq, level);
1045*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 0, &DEltX2, sizeof(DEltX2));
1046*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 2, &DEltX2, sizeof(DEltX2));
1047*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 4, &DEltX2, sizeof(DEltX2));
1048*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 6, &DEltX2, sizeof(DEltX2));
1049*01826a49SYabin Cui DTableRank += 8;
1050*01826a49SYabin Cui }
1051*01826a49SYabin Cui break;
1052*01826a49SYabin Cui default:
1053*01826a49SYabin Cui for (ptr = begin; ptr != end; ++ptr) {
1054*01826a49SYabin Cui U64 const DEltX2 = HUF_buildDEltX2U64(ptr->symbol, nbBits, baseSeq, level);
1055*01826a49SYabin Cui HUF_DEltX2* const DTableRankEnd = DTableRank + length;
1056*01826a49SYabin Cui for (; DTableRank != DTableRankEnd; DTableRank += 8) {
1057*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 0, &DEltX2, sizeof(DEltX2));
1058*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 2, &DEltX2, sizeof(DEltX2));
1059*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 4, &DEltX2, sizeof(DEltX2));
1060*01826a49SYabin Cui ZSTD_memcpy(DTableRank + 6, &DEltX2, sizeof(DEltX2));
1061*01826a49SYabin Cui }
1062*01826a49SYabin Cui }
1063*01826a49SYabin Cui break;
1064*01826a49SYabin Cui }
1065*01826a49SYabin Cui }
1066*01826a49SYabin Cui
1067*01826a49SYabin Cui /* HUF_fillDTableX2Level2() :
1068*01826a49SYabin Cui * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */
HUF_fillDTableX2Level2(HUF_DEltX2 * DTable,U32 targetLog,const U32 consumedBits,const U32 * rankVal,const int minWeight,const int maxWeight1,const sortedSymbol_t * sortedSymbols,U32 const * rankStart,U32 nbBitsBaseline,U16 baseSeq)1069*01826a49SYabin Cui static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, U32 targetLog, const U32 consumedBits,
1070*01826a49SYabin Cui const U32* rankVal, const int minWeight, const int maxWeight1,
1071*01826a49SYabin Cui const sortedSymbol_t* sortedSymbols, U32 const* rankStart,
1072*01826a49SYabin Cui U32 nbBitsBaseline, U16 baseSeq)
1073*01826a49SYabin Cui {
1074*01826a49SYabin Cui /* Fill skipped values (all positions up to rankVal[minWeight]).
1075*01826a49SYabin Cui * These are positions only get a single symbol because the combined weight
1076*01826a49SYabin Cui * is too large.
1077*01826a49SYabin Cui */
1078*01826a49SYabin Cui if (minWeight>1) {
1079*01826a49SYabin Cui U32 const length = 1U << ((targetLog - consumedBits) & 0x1F /* quiet static-analyzer */);
1080*01826a49SYabin Cui U64 const DEltX2 = HUF_buildDEltX2U64(baseSeq, consumedBits, /* baseSeq */ 0, /* level */ 1);
1081*01826a49SYabin Cui int const skipSize = rankVal[minWeight];
1082*01826a49SYabin Cui assert(length > 1);
1083*01826a49SYabin Cui assert((U32)skipSize < length);
1084*01826a49SYabin Cui switch (length) {
1085*01826a49SYabin Cui case 2:
1086*01826a49SYabin Cui assert(skipSize == 1);
1087*01826a49SYabin Cui ZSTD_memcpy(DTable, &DEltX2, sizeof(DEltX2));
1088*01826a49SYabin Cui break;
1089*01826a49SYabin Cui case 4:
1090*01826a49SYabin Cui assert(skipSize <= 4);
1091*01826a49SYabin Cui ZSTD_memcpy(DTable + 0, &DEltX2, sizeof(DEltX2));
1092*01826a49SYabin Cui ZSTD_memcpy(DTable + 2, &DEltX2, sizeof(DEltX2));
1093*01826a49SYabin Cui break;
1094*01826a49SYabin Cui default:
1095*01826a49SYabin Cui {
1096*01826a49SYabin Cui int i;
1097*01826a49SYabin Cui for (i = 0; i < skipSize; i += 8) {
1098*01826a49SYabin Cui ZSTD_memcpy(DTable + i + 0, &DEltX2, sizeof(DEltX2));
1099*01826a49SYabin Cui ZSTD_memcpy(DTable + i + 2, &DEltX2, sizeof(DEltX2));
1100*01826a49SYabin Cui ZSTD_memcpy(DTable + i + 4, &DEltX2, sizeof(DEltX2));
1101*01826a49SYabin Cui ZSTD_memcpy(DTable + i + 6, &DEltX2, sizeof(DEltX2));
1102*01826a49SYabin Cui }
1103*01826a49SYabin Cui }
1104*01826a49SYabin Cui }
1105*01826a49SYabin Cui }
1106*01826a49SYabin Cui
1107*01826a49SYabin Cui /* Fill each of the second level symbols by weight. */
1108*01826a49SYabin Cui {
1109*01826a49SYabin Cui int w;
1110*01826a49SYabin Cui for (w = minWeight; w < maxWeight1; ++w) {
1111*01826a49SYabin Cui int const begin = rankStart[w];
1112*01826a49SYabin Cui int const end = rankStart[w+1];
1113*01826a49SYabin Cui U32 const nbBits = nbBitsBaseline - w;
1114*01826a49SYabin Cui U32 const totalBits = nbBits + consumedBits;
1115*01826a49SYabin Cui HUF_fillDTableX2ForWeight(
1116*01826a49SYabin Cui DTable + rankVal[w],
1117*01826a49SYabin Cui sortedSymbols + begin, sortedSymbols + end,
1118*01826a49SYabin Cui totalBits, targetLog,
1119*01826a49SYabin Cui baseSeq, /* level */ 2);
1120*01826a49SYabin Cui }
1121*01826a49SYabin Cui }
1122*01826a49SYabin Cui }
1123*01826a49SYabin Cui
HUF_fillDTableX2(HUF_DEltX2 * DTable,const U32 targetLog,const sortedSymbol_t * sortedList,const U32 * rankStart,rankValCol_t * rankValOrigin,const U32 maxWeight,const U32 nbBitsBaseline)1124*01826a49SYabin Cui static void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog,
1125*01826a49SYabin Cui const sortedSymbol_t* sortedList,
1126*01826a49SYabin Cui const U32* rankStart, rankValCol_t* rankValOrigin, const U32 maxWeight,
1127*01826a49SYabin Cui const U32 nbBitsBaseline)
1128*01826a49SYabin Cui {
1129*01826a49SYabin Cui U32* const rankVal = rankValOrigin[0];
1130*01826a49SYabin Cui const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
1131*01826a49SYabin Cui const U32 minBits = nbBitsBaseline - maxWeight;
1132*01826a49SYabin Cui int w;
1133*01826a49SYabin Cui int const wEnd = (int)maxWeight + 1;
1134*01826a49SYabin Cui
1135*01826a49SYabin Cui /* Fill DTable in order of weight. */
1136*01826a49SYabin Cui for (w = 1; w < wEnd; ++w) {
1137*01826a49SYabin Cui int const begin = (int)rankStart[w];
1138*01826a49SYabin Cui int const end = (int)rankStart[w+1];
1139*01826a49SYabin Cui U32 const nbBits = nbBitsBaseline - w;
1140*01826a49SYabin Cui
1141*01826a49SYabin Cui if (targetLog-nbBits >= minBits) {
1142*01826a49SYabin Cui /* Enough room for a second symbol. */
1143*01826a49SYabin Cui int start = rankVal[w];
1144*01826a49SYabin Cui U32 const length = 1U << ((targetLog - nbBits) & 0x1F /* quiet static-analyzer */);
1145*01826a49SYabin Cui int minWeight = nbBits + scaleLog;
1146*01826a49SYabin Cui int s;
1147*01826a49SYabin Cui if (minWeight < 1) minWeight = 1;
1148*01826a49SYabin Cui /* Fill the DTable for every symbol of weight w.
1149*01826a49SYabin Cui * These symbols get at least 1 second symbol.
1150*01826a49SYabin Cui */
1151*01826a49SYabin Cui for (s = begin; s != end; ++s) {
1152*01826a49SYabin Cui HUF_fillDTableX2Level2(
1153*01826a49SYabin Cui DTable + start, targetLog, nbBits,
1154*01826a49SYabin Cui rankValOrigin[nbBits], minWeight, wEnd,
1155*01826a49SYabin Cui sortedList, rankStart,
1156*01826a49SYabin Cui nbBitsBaseline, sortedList[s].symbol);
1157*01826a49SYabin Cui start += length;
1158*01826a49SYabin Cui }
1159*01826a49SYabin Cui } else {
1160*01826a49SYabin Cui /* Only a single symbol. */
1161*01826a49SYabin Cui HUF_fillDTableX2ForWeight(
1162*01826a49SYabin Cui DTable + rankVal[w],
1163*01826a49SYabin Cui sortedList + begin, sortedList + end,
1164*01826a49SYabin Cui nbBits, targetLog,
1165*01826a49SYabin Cui /* baseSeq */ 0, /* level */ 1);
1166*01826a49SYabin Cui }
1167*01826a49SYabin Cui }
1168*01826a49SYabin Cui }
1169*01826a49SYabin Cui
1170*01826a49SYabin Cui typedef struct {
1171*01826a49SYabin Cui rankValCol_t rankVal[HUF_TABLELOG_MAX];
1172*01826a49SYabin Cui U32 rankStats[HUF_TABLELOG_MAX + 1];
1173*01826a49SYabin Cui U32 rankStart0[HUF_TABLELOG_MAX + 3];
1174*01826a49SYabin Cui sortedSymbol_t sortedSymbol[HUF_SYMBOLVALUE_MAX + 1];
1175*01826a49SYabin Cui BYTE weightList[HUF_SYMBOLVALUE_MAX + 1];
1176*01826a49SYabin Cui U32 calleeWksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
1177*01826a49SYabin Cui } HUF_ReadDTableX2_Workspace;
1178*01826a49SYabin Cui
HUF_readDTableX2_wksp(HUF_DTable * DTable,const void * src,size_t srcSize,void * workSpace,size_t wkspSize,int flags)1179*01826a49SYabin Cui size_t HUF_readDTableX2_wksp(HUF_DTable* DTable,
1180*01826a49SYabin Cui const void* src, size_t srcSize,
1181*01826a49SYabin Cui void* workSpace, size_t wkspSize, int flags)
1182*01826a49SYabin Cui {
1183*01826a49SYabin Cui U32 tableLog, maxW, nbSymbols;
1184*01826a49SYabin Cui DTableDesc dtd = HUF_getDTableDesc(DTable);
1185*01826a49SYabin Cui U32 maxTableLog = dtd.maxTableLog;
1186*01826a49SYabin Cui size_t iSize;
1187*01826a49SYabin Cui void* dtPtr = DTable+1; /* force compiler to avoid strict-aliasing */
1188*01826a49SYabin Cui HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;
1189*01826a49SYabin Cui U32 *rankStart;
1190*01826a49SYabin Cui
1191*01826a49SYabin Cui HUF_ReadDTableX2_Workspace* const wksp = (HUF_ReadDTableX2_Workspace*)workSpace;
1192*01826a49SYabin Cui
1193*01826a49SYabin Cui if (sizeof(*wksp) > wkspSize) return ERROR(GENERIC);
1194*01826a49SYabin Cui
1195*01826a49SYabin Cui rankStart = wksp->rankStart0 + 1;
1196*01826a49SYabin Cui ZSTD_memset(wksp->rankStats, 0, sizeof(wksp->rankStats));
1197*01826a49SYabin Cui ZSTD_memset(wksp->rankStart0, 0, sizeof(wksp->rankStart0));
1198*01826a49SYabin Cui
1199*01826a49SYabin Cui DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */
1200*01826a49SYabin Cui if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
1201*01826a49SYabin Cui /* ZSTD_memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
1202*01826a49SYabin Cui
1203*01826a49SYabin Cui iSize = HUF_readStats_wksp(wksp->weightList, HUF_SYMBOLVALUE_MAX + 1, wksp->rankStats, &nbSymbols, &tableLog, src, srcSize, wksp->calleeWksp, sizeof(wksp->calleeWksp), flags);
1204*01826a49SYabin Cui if (HUF_isError(iSize)) return iSize;
1205*01826a49SYabin Cui
1206*01826a49SYabin Cui /* check result */
1207*01826a49SYabin Cui if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
1208*01826a49SYabin Cui if (tableLog <= HUF_DECODER_FAST_TABLELOG && maxTableLog > HUF_DECODER_FAST_TABLELOG) maxTableLog = HUF_DECODER_FAST_TABLELOG;
1209*01826a49SYabin Cui
1210*01826a49SYabin Cui /* find maxWeight */
1211*01826a49SYabin Cui for (maxW = tableLog; wksp->rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
1212*01826a49SYabin Cui
1213*01826a49SYabin Cui /* Get start index of each weight */
1214*01826a49SYabin Cui { U32 w, nextRankStart = 0;
1215*01826a49SYabin Cui for (w=1; w<maxW+1; w++) {
1216*01826a49SYabin Cui U32 curr = nextRankStart;
1217*01826a49SYabin Cui nextRankStart += wksp->rankStats[w];
1218*01826a49SYabin Cui rankStart[w] = curr;
1219*01826a49SYabin Cui }
1220*01826a49SYabin Cui rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
1221*01826a49SYabin Cui rankStart[maxW+1] = nextRankStart;
1222*01826a49SYabin Cui }
1223*01826a49SYabin Cui
1224*01826a49SYabin Cui /* sort symbols by weight */
1225*01826a49SYabin Cui { U32 s;
1226*01826a49SYabin Cui for (s=0; s<nbSymbols; s++) {
1227*01826a49SYabin Cui U32 const w = wksp->weightList[s];
1228*01826a49SYabin Cui U32 const r = rankStart[w]++;
1229*01826a49SYabin Cui wksp->sortedSymbol[r].symbol = (BYTE)s;
1230*01826a49SYabin Cui }
1231*01826a49SYabin Cui rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
1232*01826a49SYabin Cui }
1233*01826a49SYabin Cui
1234*01826a49SYabin Cui /* Build rankVal */
1235*01826a49SYabin Cui { U32* const rankVal0 = wksp->rankVal[0];
1236*01826a49SYabin Cui { int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */
1237*01826a49SYabin Cui U32 nextRankVal = 0;
1238*01826a49SYabin Cui U32 w;
1239*01826a49SYabin Cui for (w=1; w<maxW+1; w++) {
1240*01826a49SYabin Cui U32 curr = nextRankVal;
1241*01826a49SYabin Cui nextRankVal += wksp->rankStats[w] << (w+rescale);
1242*01826a49SYabin Cui rankVal0[w] = curr;
1243*01826a49SYabin Cui } }
1244*01826a49SYabin Cui { U32 const minBits = tableLog+1 - maxW;
1245*01826a49SYabin Cui U32 consumed;
1246*01826a49SYabin Cui for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
1247*01826a49SYabin Cui U32* const rankValPtr = wksp->rankVal[consumed];
1248*01826a49SYabin Cui U32 w;
1249*01826a49SYabin Cui for (w = 1; w < maxW+1; w++) {
1250*01826a49SYabin Cui rankValPtr[w] = rankVal0[w] >> consumed;
1251*01826a49SYabin Cui } } } }
1252*01826a49SYabin Cui
1253*01826a49SYabin Cui HUF_fillDTableX2(dt, maxTableLog,
1254*01826a49SYabin Cui wksp->sortedSymbol,
1255*01826a49SYabin Cui wksp->rankStart0, wksp->rankVal, maxW,
1256*01826a49SYabin Cui tableLog+1);
1257*01826a49SYabin Cui
1258*01826a49SYabin Cui dtd.tableLog = (BYTE)maxTableLog;
1259*01826a49SYabin Cui dtd.tableType = 1;
1260*01826a49SYabin Cui ZSTD_memcpy(DTable, &dtd, sizeof(dtd));
1261*01826a49SYabin Cui return iSize;
1262*01826a49SYabin Cui }
1263*01826a49SYabin Cui
1264*01826a49SYabin Cui
1265*01826a49SYabin Cui FORCE_INLINE_TEMPLATE U32
HUF_decodeSymbolX2(void * op,BIT_DStream_t * DStream,const HUF_DEltX2 * dt,const U32 dtLog)1266*01826a49SYabin Cui HUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)
1267*01826a49SYabin Cui {
1268*01826a49SYabin Cui size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
1269*01826a49SYabin Cui ZSTD_memcpy(op, &dt[val].sequence, 2);
1270*01826a49SYabin Cui BIT_skipBits(DStream, dt[val].nbBits);
1271*01826a49SYabin Cui return dt[val].length;
1272*01826a49SYabin Cui }
1273*01826a49SYabin Cui
1274*01826a49SYabin Cui FORCE_INLINE_TEMPLATE U32
HUF_decodeLastSymbolX2(void * op,BIT_DStream_t * DStream,const HUF_DEltX2 * dt,const U32 dtLog)1275*01826a49SYabin Cui HUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)
1276*01826a49SYabin Cui {
1277*01826a49SYabin Cui size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
1278*01826a49SYabin Cui ZSTD_memcpy(op, &dt[val].sequence, 1);
1279*01826a49SYabin Cui if (dt[val].length==1) {
1280*01826a49SYabin Cui BIT_skipBits(DStream, dt[val].nbBits);
1281*01826a49SYabin Cui } else {
1282*01826a49SYabin Cui if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {
1283*01826a49SYabin Cui BIT_skipBits(DStream, dt[val].nbBits);
1284*01826a49SYabin Cui if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
1285*01826a49SYabin Cui /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
1286*01826a49SYabin Cui DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);
1287*01826a49SYabin Cui }
1288*01826a49SYabin Cui }
1289*01826a49SYabin Cui return 1;
1290*01826a49SYabin Cui }
1291*01826a49SYabin Cui
1292*01826a49SYabin Cui #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
1293*01826a49SYabin Cui do { ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog); } while (0)
1294*01826a49SYabin Cui
1295*01826a49SYabin Cui #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
1296*01826a49SYabin Cui do { \
1297*01826a49SYabin Cui if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
1298*01826a49SYabin Cui ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog); \
1299*01826a49SYabin Cui } while (0)
1300*01826a49SYabin Cui
1301*01826a49SYabin Cui #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
1302*01826a49SYabin Cui do { \
1303*01826a49SYabin Cui if (MEM_64bits()) \
1304*01826a49SYabin Cui ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog); \
1305*01826a49SYabin Cui } while (0)
1306*01826a49SYabin Cui
1307*01826a49SYabin Cui HINT_INLINE size_t
HUF_decodeStreamX2(BYTE * p,BIT_DStream_t * bitDPtr,BYTE * const pEnd,const HUF_DEltX2 * const dt,const U32 dtLog)1308*01826a49SYabin Cui HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd,
1309*01826a49SYabin Cui const HUF_DEltX2* const dt, const U32 dtLog)
1310*01826a49SYabin Cui {
1311*01826a49SYabin Cui BYTE* const pStart = p;
1312*01826a49SYabin Cui
1313*01826a49SYabin Cui /* up to 8 symbols at a time */
1314*01826a49SYabin Cui if ((size_t)(pEnd - p) >= sizeof(bitDPtr->bitContainer)) {
1315*01826a49SYabin Cui if (dtLog <= 11 && MEM_64bits()) {
1316*01826a49SYabin Cui /* up to 10 symbols at a time */
1317*01826a49SYabin Cui while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-9)) {
1318*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1319*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1320*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1321*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1322*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1323*01826a49SYabin Cui }
1324*01826a49SYabin Cui } else {
1325*01826a49SYabin Cui /* up to 8 symbols at a time */
1326*01826a49SYabin Cui while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) {
1327*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1328*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
1329*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1330*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1331*01826a49SYabin Cui }
1332*01826a49SYabin Cui }
1333*01826a49SYabin Cui } else {
1334*01826a49SYabin Cui BIT_reloadDStream(bitDPtr);
1335*01826a49SYabin Cui }
1336*01826a49SYabin Cui
1337*01826a49SYabin Cui /* closer to end : up to 2 symbols at a time */
1338*01826a49SYabin Cui if ((size_t)(pEnd - p) >= 2) {
1339*01826a49SYabin Cui while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2))
1340*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1341*01826a49SYabin Cui
1342*01826a49SYabin Cui while (p <= pEnd-2)
1343*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
1344*01826a49SYabin Cui }
1345*01826a49SYabin Cui
1346*01826a49SYabin Cui if (p < pEnd)
1347*01826a49SYabin Cui p += HUF_decodeLastSymbolX2(p, bitDPtr, dt, dtLog);
1348*01826a49SYabin Cui
1349*01826a49SYabin Cui return p-pStart;
1350*01826a49SYabin Cui }
1351*01826a49SYabin Cui
1352*01826a49SYabin Cui FORCE_INLINE_TEMPLATE size_t
HUF_decompress1X2_usingDTable_internal_body(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable)1353*01826a49SYabin Cui HUF_decompress1X2_usingDTable_internal_body(
1354*01826a49SYabin Cui void* dst, size_t dstSize,
1355*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1356*01826a49SYabin Cui const HUF_DTable* DTable)
1357*01826a49SYabin Cui {
1358*01826a49SYabin Cui BIT_DStream_t bitD;
1359*01826a49SYabin Cui
1360*01826a49SYabin Cui /* Init */
1361*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );
1362*01826a49SYabin Cui
1363*01826a49SYabin Cui /* decode */
1364*01826a49SYabin Cui { BYTE* const ostart = (BYTE*) dst;
1365*01826a49SYabin Cui BYTE* const oend = ZSTD_maybeNullPtrAdd(ostart, dstSize);
1366*01826a49SYabin Cui const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */
1367*01826a49SYabin Cui const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
1368*01826a49SYabin Cui DTableDesc const dtd = HUF_getDTableDesc(DTable);
1369*01826a49SYabin Cui HUF_decodeStreamX2(ostart, &bitD, oend, dt, dtd.tableLog);
1370*01826a49SYabin Cui }
1371*01826a49SYabin Cui
1372*01826a49SYabin Cui /* check */
1373*01826a49SYabin Cui if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
1374*01826a49SYabin Cui
1375*01826a49SYabin Cui /* decoded size */
1376*01826a49SYabin Cui return dstSize;
1377*01826a49SYabin Cui }
1378*01826a49SYabin Cui
1379*01826a49SYabin Cui /* HUF_decompress4X2_usingDTable_internal_body():
1380*01826a49SYabin Cui * Conditions:
1381*01826a49SYabin Cui * @dstSize >= 6
1382*01826a49SYabin Cui */
1383*01826a49SYabin Cui FORCE_INLINE_TEMPLATE size_t
HUF_decompress4X2_usingDTable_internal_body(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable)1384*01826a49SYabin Cui HUF_decompress4X2_usingDTable_internal_body(
1385*01826a49SYabin Cui void* dst, size_t dstSize,
1386*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1387*01826a49SYabin Cui const HUF_DTable* DTable)
1388*01826a49SYabin Cui {
1389*01826a49SYabin Cui if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
1390*01826a49SYabin Cui if (dstSize < 6) return ERROR(corruption_detected); /* stream 4-split doesn't work */
1391*01826a49SYabin Cui
1392*01826a49SYabin Cui { const BYTE* const istart = (const BYTE*) cSrc;
1393*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
1394*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
1395*01826a49SYabin Cui BYTE* const olimit = oend - (sizeof(size_t)-1);
1396*01826a49SYabin Cui const void* const dtPtr = DTable+1;
1397*01826a49SYabin Cui const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
1398*01826a49SYabin Cui
1399*01826a49SYabin Cui /* Init */
1400*01826a49SYabin Cui BIT_DStream_t bitD1;
1401*01826a49SYabin Cui BIT_DStream_t bitD2;
1402*01826a49SYabin Cui BIT_DStream_t bitD3;
1403*01826a49SYabin Cui BIT_DStream_t bitD4;
1404*01826a49SYabin Cui size_t const length1 = MEM_readLE16(istart);
1405*01826a49SYabin Cui size_t const length2 = MEM_readLE16(istart+2);
1406*01826a49SYabin Cui size_t const length3 = MEM_readLE16(istart+4);
1407*01826a49SYabin Cui size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
1408*01826a49SYabin Cui const BYTE* const istart1 = istart + 6; /* jumpTable */
1409*01826a49SYabin Cui const BYTE* const istart2 = istart1 + length1;
1410*01826a49SYabin Cui const BYTE* const istart3 = istart2 + length2;
1411*01826a49SYabin Cui const BYTE* const istart4 = istart3 + length3;
1412*01826a49SYabin Cui size_t const segmentSize = (dstSize+3) / 4;
1413*01826a49SYabin Cui BYTE* const opStart2 = ostart + segmentSize;
1414*01826a49SYabin Cui BYTE* const opStart3 = opStart2 + segmentSize;
1415*01826a49SYabin Cui BYTE* const opStart4 = opStart3 + segmentSize;
1416*01826a49SYabin Cui BYTE* op1 = ostart;
1417*01826a49SYabin Cui BYTE* op2 = opStart2;
1418*01826a49SYabin Cui BYTE* op3 = opStart3;
1419*01826a49SYabin Cui BYTE* op4 = opStart4;
1420*01826a49SYabin Cui U32 endSignal = 1;
1421*01826a49SYabin Cui DTableDesc const dtd = HUF_getDTableDesc(DTable);
1422*01826a49SYabin Cui U32 const dtLog = dtd.tableLog;
1423*01826a49SYabin Cui
1424*01826a49SYabin Cui if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
1425*01826a49SYabin Cui if (opStart4 > oend) return ERROR(corruption_detected); /* overflow */
1426*01826a49SYabin Cui assert(dstSize >= 6 /* validated above */);
1427*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );
1428*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );
1429*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );
1430*01826a49SYabin Cui CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );
1431*01826a49SYabin Cui
1432*01826a49SYabin Cui /* 16-32 symbols per loop (4-8 symbols per stream) */
1433*01826a49SYabin Cui if ((size_t)(oend - op4) >= sizeof(size_t)) {
1434*01826a49SYabin Cui for ( ; (endSignal) & (op4 < olimit); ) {
1435*01826a49SYabin Cui #if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
1436*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1437*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
1438*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1439*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
1440*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1441*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
1442*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1443*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
1444*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;
1445*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;
1446*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1447*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
1448*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1449*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
1450*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1451*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
1452*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1453*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
1454*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;
1455*01826a49SYabin Cui endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;
1456*01826a49SYabin Cui #else
1457*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1458*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1459*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1460*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1461*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
1462*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
1463*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
1464*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
1465*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1466*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1467*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1468*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1469*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
1470*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
1471*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
1472*01826a49SYabin Cui HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
1473*01826a49SYabin Cui endSignal = (U32)LIKELY((U32)
1474*01826a49SYabin Cui (BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished)
1475*01826a49SYabin Cui & (BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished)
1476*01826a49SYabin Cui & (BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished)
1477*01826a49SYabin Cui & (BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished));
1478*01826a49SYabin Cui #endif
1479*01826a49SYabin Cui }
1480*01826a49SYabin Cui }
1481*01826a49SYabin Cui
1482*01826a49SYabin Cui /* check corruption */
1483*01826a49SYabin Cui if (op1 > opStart2) return ERROR(corruption_detected);
1484*01826a49SYabin Cui if (op2 > opStart3) return ERROR(corruption_detected);
1485*01826a49SYabin Cui if (op3 > opStart4) return ERROR(corruption_detected);
1486*01826a49SYabin Cui /* note : op4 already verified within main loop */
1487*01826a49SYabin Cui
1488*01826a49SYabin Cui /* finish bitStreams one by one */
1489*01826a49SYabin Cui HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
1490*01826a49SYabin Cui HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
1491*01826a49SYabin Cui HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
1492*01826a49SYabin Cui HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
1493*01826a49SYabin Cui
1494*01826a49SYabin Cui /* check */
1495*01826a49SYabin Cui { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
1496*01826a49SYabin Cui if (!endCheck) return ERROR(corruption_detected); }
1497*01826a49SYabin Cui
1498*01826a49SYabin Cui /* decoded size */
1499*01826a49SYabin Cui return dstSize;
1500*01826a49SYabin Cui }
1501*01826a49SYabin Cui }
1502*01826a49SYabin Cui
1503*01826a49SYabin Cui #if HUF_NEED_BMI2_FUNCTION
1504*01826a49SYabin Cui static BMI2_TARGET_ATTRIBUTE
HUF_decompress4X2_usingDTable_internal_bmi2(void * dst,size_t dstSize,void const * cSrc,size_t cSrcSize,HUF_DTable const * DTable)1505*01826a49SYabin Cui size_t HUF_decompress4X2_usingDTable_internal_bmi2(void* dst, size_t dstSize, void const* cSrc,
1506*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable) {
1507*01826a49SYabin Cui return HUF_decompress4X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);
1508*01826a49SYabin Cui }
1509*01826a49SYabin Cui #endif
1510*01826a49SYabin Cui
1511*01826a49SYabin Cui static
HUF_decompress4X2_usingDTable_internal_default(void * dst,size_t dstSize,void const * cSrc,size_t cSrcSize,HUF_DTable const * DTable)1512*01826a49SYabin Cui size_t HUF_decompress4X2_usingDTable_internal_default(void* dst, size_t dstSize, void const* cSrc,
1513*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable) {
1514*01826a49SYabin Cui return HUF_decompress4X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable);
1515*01826a49SYabin Cui }
1516*01826a49SYabin Cui
1517*01826a49SYabin Cui #if ZSTD_ENABLE_ASM_X86_64_BMI2
1518*01826a49SYabin Cui
1519*01826a49SYabin Cui HUF_ASM_DECL void HUF_decompress4X2_usingDTable_internal_fast_asm_loop(HUF_DecompressFastArgs* args) ZSTDLIB_HIDDEN;
1520*01826a49SYabin Cui
1521*01826a49SYabin Cui #endif
1522*01826a49SYabin Cui
1523*01826a49SYabin Cui static HUF_FAST_BMI2_ATTRS
HUF_decompress4X2_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs * args)1524*01826a49SYabin Cui void HUF_decompress4X2_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs* args)
1525*01826a49SYabin Cui {
1526*01826a49SYabin Cui U64 bits[4];
1527*01826a49SYabin Cui BYTE const* ip[4];
1528*01826a49SYabin Cui BYTE* op[4];
1529*01826a49SYabin Cui BYTE* oend[4];
1530*01826a49SYabin Cui HUF_DEltX2 const* const dtable = (HUF_DEltX2 const*)args->dt;
1531*01826a49SYabin Cui BYTE const* const ilowest = args->ilowest;
1532*01826a49SYabin Cui
1533*01826a49SYabin Cui /* Copy the arguments to local registers. */
1534*01826a49SYabin Cui ZSTD_memcpy(&bits, &args->bits, sizeof(bits));
1535*01826a49SYabin Cui ZSTD_memcpy((void*)(&ip), &args->ip, sizeof(ip));
1536*01826a49SYabin Cui ZSTD_memcpy(&op, &args->op, sizeof(op));
1537*01826a49SYabin Cui
1538*01826a49SYabin Cui oend[0] = op[1];
1539*01826a49SYabin Cui oend[1] = op[2];
1540*01826a49SYabin Cui oend[2] = op[3];
1541*01826a49SYabin Cui oend[3] = args->oend;
1542*01826a49SYabin Cui
1543*01826a49SYabin Cui assert(MEM_isLittleEndian());
1544*01826a49SYabin Cui assert(!MEM_32bits());
1545*01826a49SYabin Cui
1546*01826a49SYabin Cui for (;;) {
1547*01826a49SYabin Cui BYTE* olimit;
1548*01826a49SYabin Cui int stream;
1549*01826a49SYabin Cui
1550*01826a49SYabin Cui /* Assert loop preconditions */
1551*01826a49SYabin Cui #ifndef NDEBUG
1552*01826a49SYabin Cui for (stream = 0; stream < 4; ++stream) {
1553*01826a49SYabin Cui assert(op[stream] <= oend[stream]);
1554*01826a49SYabin Cui assert(ip[stream] >= ilowest);
1555*01826a49SYabin Cui }
1556*01826a49SYabin Cui #endif
1557*01826a49SYabin Cui /* Compute olimit */
1558*01826a49SYabin Cui {
1559*01826a49SYabin Cui /* Each loop does 5 table lookups for each of the 4 streams.
1560*01826a49SYabin Cui * Each table lookup consumes up to 11 bits of input, and produces
1561*01826a49SYabin Cui * up to 2 bytes of output.
1562*01826a49SYabin Cui */
1563*01826a49SYabin Cui /* We can consume up to 7 bytes of input per iteration per stream.
1564*01826a49SYabin Cui * We also know that each input pointer is >= ip[0]. So we can run
1565*01826a49SYabin Cui * iters loops before running out of input.
1566*01826a49SYabin Cui */
1567*01826a49SYabin Cui size_t iters = (size_t)(ip[0] - ilowest) / 7;
1568*01826a49SYabin Cui /* Each iteration can produce up to 10 bytes of output per stream.
1569*01826a49SYabin Cui * Each output stream my advance at different rates. So take the
1570*01826a49SYabin Cui * minimum number of safe iterations among all the output streams.
1571*01826a49SYabin Cui */
1572*01826a49SYabin Cui for (stream = 0; stream < 4; ++stream) {
1573*01826a49SYabin Cui size_t const oiters = (size_t)(oend[stream] - op[stream]) / 10;
1574*01826a49SYabin Cui iters = MIN(iters, oiters);
1575*01826a49SYabin Cui }
1576*01826a49SYabin Cui
1577*01826a49SYabin Cui /* Each iteration produces at least 5 output symbols. So until
1578*01826a49SYabin Cui * op[3] crosses olimit, we know we haven't executed iters
1579*01826a49SYabin Cui * iterations yet. This saves us maintaining an iters counter,
1580*01826a49SYabin Cui * at the expense of computing the remaining # of iterations
1581*01826a49SYabin Cui * more frequently.
1582*01826a49SYabin Cui */
1583*01826a49SYabin Cui olimit = op[3] + (iters * 5);
1584*01826a49SYabin Cui
1585*01826a49SYabin Cui /* Exit the fast decoding loop once we reach the end. */
1586*01826a49SYabin Cui if (op[3] == olimit)
1587*01826a49SYabin Cui break;
1588*01826a49SYabin Cui
1589*01826a49SYabin Cui /* Exit the decoding loop if any input pointer has crossed the
1590*01826a49SYabin Cui * previous one. This indicates corruption, and a precondition
1591*01826a49SYabin Cui * to our loop is that ip[i] >= ip[0].
1592*01826a49SYabin Cui */
1593*01826a49SYabin Cui for (stream = 1; stream < 4; ++stream) {
1594*01826a49SYabin Cui if (ip[stream] < ip[stream - 1])
1595*01826a49SYabin Cui goto _out;
1596*01826a49SYabin Cui }
1597*01826a49SYabin Cui }
1598*01826a49SYabin Cui
1599*01826a49SYabin Cui #ifndef NDEBUG
1600*01826a49SYabin Cui for (stream = 1; stream < 4; ++stream) {
1601*01826a49SYabin Cui assert(ip[stream] >= ip[stream - 1]);
1602*01826a49SYabin Cui }
1603*01826a49SYabin Cui #endif
1604*01826a49SYabin Cui
1605*01826a49SYabin Cui #define HUF_4X2_DECODE_SYMBOL(_stream, _decode3) \
1606*01826a49SYabin Cui do { \
1607*01826a49SYabin Cui if ((_decode3) || (_stream) != 3) { \
1608*01826a49SYabin Cui int const index = (int)(bits[(_stream)] >> 53); \
1609*01826a49SYabin Cui HUF_DEltX2 const entry = dtable[index]; \
1610*01826a49SYabin Cui MEM_write16(op[(_stream)], entry.sequence); \
1611*01826a49SYabin Cui bits[(_stream)] <<= (entry.nbBits) & 0x3F; \
1612*01826a49SYabin Cui op[(_stream)] += (entry.length); \
1613*01826a49SYabin Cui } \
1614*01826a49SYabin Cui } while (0)
1615*01826a49SYabin Cui
1616*01826a49SYabin Cui #define HUF_4X2_RELOAD_STREAM(_stream) \
1617*01826a49SYabin Cui do { \
1618*01826a49SYabin Cui HUF_4X2_DECODE_SYMBOL(3, 1); \
1619*01826a49SYabin Cui { \
1620*01826a49SYabin Cui int const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
1621*01826a49SYabin Cui int const nbBits = ctz & 7; \
1622*01826a49SYabin Cui int const nbBytes = ctz >> 3; \
1623*01826a49SYabin Cui ip[(_stream)] -= nbBytes; \
1624*01826a49SYabin Cui bits[(_stream)] = MEM_read64(ip[(_stream)]) | 1; \
1625*01826a49SYabin Cui bits[(_stream)] <<= nbBits; \
1626*01826a49SYabin Cui } \
1627*01826a49SYabin Cui } while (0)
1628*01826a49SYabin Cui
1629*01826a49SYabin Cui /* Manually unroll the loop because compilers don't consistently
1630*01826a49SYabin Cui * unroll the inner loops, which destroys performance.
1631*01826a49SYabin Cui */
1632*01826a49SYabin Cui do {
1633*01826a49SYabin Cui /* Decode 5 symbols from each of the first 3 streams.
1634*01826a49SYabin Cui * The final stream will be decoded during the reload phase
1635*01826a49SYabin Cui * to reduce register pressure.
1636*01826a49SYabin Cui */
1637*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1638*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1639*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1640*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1641*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0);
1642*01826a49SYabin Cui
1643*01826a49SYabin Cui /* Decode one symbol from the final stream */
1644*01826a49SYabin Cui HUF_4X2_DECODE_SYMBOL(3, 1);
1645*01826a49SYabin Cui
1646*01826a49SYabin Cui /* Decode 4 symbols from the final stream & reload bitstreams.
1647*01826a49SYabin Cui * The final stream is reloaded last, meaning that all 5 symbols
1648*01826a49SYabin Cui * are decoded from the final stream before it is reloaded.
1649*01826a49SYabin Cui */
1650*01826a49SYabin Cui HUF_4X_FOR_EACH_STREAM(HUF_4X2_RELOAD_STREAM);
1651*01826a49SYabin Cui } while (op[3] < olimit);
1652*01826a49SYabin Cui }
1653*01826a49SYabin Cui
1654*01826a49SYabin Cui #undef HUF_4X2_DECODE_SYMBOL
1655*01826a49SYabin Cui #undef HUF_4X2_RELOAD_STREAM
1656*01826a49SYabin Cui
1657*01826a49SYabin Cui _out:
1658*01826a49SYabin Cui
1659*01826a49SYabin Cui /* Save the final values of each of the state variables back to args. */
1660*01826a49SYabin Cui ZSTD_memcpy(&args->bits, &bits, sizeof(bits));
1661*01826a49SYabin Cui ZSTD_memcpy((void*)(&args->ip), &ip, sizeof(ip));
1662*01826a49SYabin Cui ZSTD_memcpy(&args->op, &op, sizeof(op));
1663*01826a49SYabin Cui }
1664*01826a49SYabin Cui
1665*01826a49SYabin Cui
1666*01826a49SYabin Cui static HUF_FAST_BMI2_ATTRS size_t
HUF_decompress4X2_usingDTable_internal_fast(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable,HUF_DecompressFastLoopFn loopFn)1667*01826a49SYabin Cui HUF_decompress4X2_usingDTable_internal_fast(
1668*01826a49SYabin Cui void* dst, size_t dstSize,
1669*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1670*01826a49SYabin Cui const HUF_DTable* DTable,
1671*01826a49SYabin Cui HUF_DecompressFastLoopFn loopFn) {
1672*01826a49SYabin Cui void const* dt = DTable + 1;
1673*01826a49SYabin Cui const BYTE* const ilowest = (const BYTE*)cSrc;
1674*01826a49SYabin Cui BYTE* const oend = ZSTD_maybeNullPtrAdd((BYTE*)dst, dstSize);
1675*01826a49SYabin Cui HUF_DecompressFastArgs args;
1676*01826a49SYabin Cui {
1677*01826a49SYabin Cui size_t const ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable);
1678*01826a49SYabin Cui FORWARD_IF_ERROR(ret, "Failed to init asm args");
1679*01826a49SYabin Cui if (ret == 0)
1680*01826a49SYabin Cui return 0;
1681*01826a49SYabin Cui }
1682*01826a49SYabin Cui
1683*01826a49SYabin Cui assert(args.ip[0] >= args.ilowest);
1684*01826a49SYabin Cui loopFn(&args);
1685*01826a49SYabin Cui
1686*01826a49SYabin Cui /* note : op4 already verified within main loop */
1687*01826a49SYabin Cui assert(args.ip[0] >= ilowest);
1688*01826a49SYabin Cui assert(args.ip[1] >= ilowest);
1689*01826a49SYabin Cui assert(args.ip[2] >= ilowest);
1690*01826a49SYabin Cui assert(args.ip[3] >= ilowest);
1691*01826a49SYabin Cui assert(args.op[3] <= oend);
1692*01826a49SYabin Cui
1693*01826a49SYabin Cui assert(ilowest == args.ilowest);
1694*01826a49SYabin Cui assert(ilowest + 6 == args.iend[0]);
1695*01826a49SYabin Cui (void)ilowest;
1696*01826a49SYabin Cui
1697*01826a49SYabin Cui /* finish bitStreams one by one */
1698*01826a49SYabin Cui {
1699*01826a49SYabin Cui size_t const segmentSize = (dstSize+3) / 4;
1700*01826a49SYabin Cui BYTE* segmentEnd = (BYTE*)dst;
1701*01826a49SYabin Cui int i;
1702*01826a49SYabin Cui for (i = 0; i < 4; ++i) {
1703*01826a49SYabin Cui BIT_DStream_t bit;
1704*01826a49SYabin Cui if (segmentSize <= (size_t)(oend - segmentEnd))
1705*01826a49SYabin Cui segmentEnd += segmentSize;
1706*01826a49SYabin Cui else
1707*01826a49SYabin Cui segmentEnd = oend;
1708*01826a49SYabin Cui FORWARD_IF_ERROR(HUF_initRemainingDStream(&bit, &args, i, segmentEnd), "corruption");
1709*01826a49SYabin Cui args.op[i] += HUF_decodeStreamX2(args.op[i], &bit, segmentEnd, (HUF_DEltX2 const*)dt, HUF_DECODER_FAST_TABLELOG);
1710*01826a49SYabin Cui if (args.op[i] != segmentEnd)
1711*01826a49SYabin Cui return ERROR(corruption_detected);
1712*01826a49SYabin Cui }
1713*01826a49SYabin Cui }
1714*01826a49SYabin Cui
1715*01826a49SYabin Cui /* decoded size */
1716*01826a49SYabin Cui return dstSize;
1717*01826a49SYabin Cui }
1718*01826a49SYabin Cui
HUF_decompress4X2_usingDTable_internal(void * dst,size_t dstSize,void const * cSrc,size_t cSrcSize,HUF_DTable const * DTable,int flags)1719*01826a49SYabin Cui static size_t HUF_decompress4X2_usingDTable_internal(void* dst, size_t dstSize, void const* cSrc,
1720*01826a49SYabin Cui size_t cSrcSize, HUF_DTable const* DTable, int flags)
1721*01826a49SYabin Cui {
1722*01826a49SYabin Cui HUF_DecompressUsingDTableFn fallbackFn = HUF_decompress4X2_usingDTable_internal_default;
1723*01826a49SYabin Cui HUF_DecompressFastLoopFn loopFn = HUF_decompress4X2_usingDTable_internal_fast_c_loop;
1724*01826a49SYabin Cui
1725*01826a49SYabin Cui #if DYNAMIC_BMI2
1726*01826a49SYabin Cui if (flags & HUF_flags_bmi2) {
1727*01826a49SYabin Cui fallbackFn = HUF_decompress4X2_usingDTable_internal_bmi2;
1728*01826a49SYabin Cui # if ZSTD_ENABLE_ASM_X86_64_BMI2
1729*01826a49SYabin Cui if (!(flags & HUF_flags_disableAsm)) {
1730*01826a49SYabin Cui loopFn = HUF_decompress4X2_usingDTable_internal_fast_asm_loop;
1731*01826a49SYabin Cui }
1732*01826a49SYabin Cui # endif
1733*01826a49SYabin Cui } else {
1734*01826a49SYabin Cui return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);
1735*01826a49SYabin Cui }
1736*01826a49SYabin Cui #endif
1737*01826a49SYabin Cui
1738*01826a49SYabin Cui #if ZSTD_ENABLE_ASM_X86_64_BMI2 && defined(__BMI2__)
1739*01826a49SYabin Cui if (!(flags & HUF_flags_disableAsm)) {
1740*01826a49SYabin Cui loopFn = HUF_decompress4X2_usingDTable_internal_fast_asm_loop;
1741*01826a49SYabin Cui }
1742*01826a49SYabin Cui #endif
1743*01826a49SYabin Cui
1744*01826a49SYabin Cui if (HUF_ENABLE_FAST_DECODE && !(flags & HUF_flags_disableFast)) {
1745*01826a49SYabin Cui size_t const ret = HUF_decompress4X2_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn);
1746*01826a49SYabin Cui if (ret != 0)
1747*01826a49SYabin Cui return ret;
1748*01826a49SYabin Cui }
1749*01826a49SYabin Cui return fallbackFn(dst, dstSize, cSrc, cSrcSize, DTable);
1750*01826a49SYabin Cui }
1751*01826a49SYabin Cui
HUF_DGEN(HUF_decompress1X2_usingDTable_internal)1752*01826a49SYabin Cui HUF_DGEN(HUF_decompress1X2_usingDTable_internal)
1753*01826a49SYabin Cui
1754*01826a49SYabin Cui size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
1755*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1756*01826a49SYabin Cui void* workSpace, size_t wkspSize, int flags)
1757*01826a49SYabin Cui {
1758*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
1759*01826a49SYabin Cui
1760*01826a49SYabin Cui size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize,
1761*01826a49SYabin Cui workSpace, wkspSize, flags);
1762*01826a49SYabin Cui if (HUF_isError(hSize)) return hSize;
1763*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
1764*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
1765*01826a49SYabin Cui
1766*01826a49SYabin Cui return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, flags);
1767*01826a49SYabin Cui }
1768*01826a49SYabin Cui
HUF_decompress4X2_DCtx_wksp(HUF_DTable * dctx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,void * workSpace,size_t wkspSize,int flags)1769*01826a49SYabin Cui static size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
1770*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1771*01826a49SYabin Cui void* workSpace, size_t wkspSize, int flags)
1772*01826a49SYabin Cui {
1773*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
1774*01826a49SYabin Cui
1775*01826a49SYabin Cui size_t hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize,
1776*01826a49SYabin Cui workSpace, wkspSize, flags);
1777*01826a49SYabin Cui if (HUF_isError(hSize)) return hSize;
1778*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
1779*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
1780*01826a49SYabin Cui
1781*01826a49SYabin Cui return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, flags);
1782*01826a49SYabin Cui }
1783*01826a49SYabin Cui
1784*01826a49SYabin Cui #endif /* HUF_FORCE_DECOMPRESS_X1 */
1785*01826a49SYabin Cui
1786*01826a49SYabin Cui
1787*01826a49SYabin Cui /* ***********************************/
1788*01826a49SYabin Cui /* Universal decompression selectors */
1789*01826a49SYabin Cui /* ***********************************/
1790*01826a49SYabin Cui
1791*01826a49SYabin Cui
1792*01826a49SYabin Cui #if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)
1793*01826a49SYabin Cui typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
1794*01826a49SYabin Cui static const algo_time_t algoTime[16 /* Quantization */][2 /* single, double */] =
1795*01826a49SYabin Cui {
1796*01826a49SYabin Cui /* single, double, quad */
1797*01826a49SYabin Cui {{0,0}, {1,1}}, /* Q==0 : impossible */
1798*01826a49SYabin Cui {{0,0}, {1,1}}, /* Q==1 : impossible */
1799*01826a49SYabin Cui {{ 150,216}, { 381,119}}, /* Q == 2 : 12-18% */
1800*01826a49SYabin Cui {{ 170,205}, { 514,112}}, /* Q == 3 : 18-25% */
1801*01826a49SYabin Cui {{ 177,199}, { 539,110}}, /* Q == 4 : 25-32% */
1802*01826a49SYabin Cui {{ 197,194}, { 644,107}}, /* Q == 5 : 32-38% */
1803*01826a49SYabin Cui {{ 221,192}, { 735,107}}, /* Q == 6 : 38-44% */
1804*01826a49SYabin Cui {{ 256,189}, { 881,106}}, /* Q == 7 : 44-50% */
1805*01826a49SYabin Cui {{ 359,188}, {1167,109}}, /* Q == 8 : 50-56% */
1806*01826a49SYabin Cui {{ 582,187}, {1570,114}}, /* Q == 9 : 56-62% */
1807*01826a49SYabin Cui {{ 688,187}, {1712,122}}, /* Q ==10 : 62-69% */
1808*01826a49SYabin Cui {{ 825,186}, {1965,136}}, /* Q ==11 : 69-75% */
1809*01826a49SYabin Cui {{ 976,185}, {2131,150}}, /* Q ==12 : 75-81% */
1810*01826a49SYabin Cui {{1180,186}, {2070,175}}, /* Q ==13 : 81-87% */
1811*01826a49SYabin Cui {{1377,185}, {1731,202}}, /* Q ==14 : 87-93% */
1812*01826a49SYabin Cui {{1412,185}, {1695,202}}, /* Q ==15 : 93-99% */
1813*01826a49SYabin Cui };
1814*01826a49SYabin Cui #endif
1815*01826a49SYabin Cui
1816*01826a49SYabin Cui /** HUF_selectDecoder() :
1817*01826a49SYabin Cui * Tells which decoder is likely to decode faster,
1818*01826a49SYabin Cui * based on a set of pre-computed metrics.
1819*01826a49SYabin Cui * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .
1820*01826a49SYabin Cui * Assumption : 0 < dstSize <= 128 KB */
HUF_selectDecoder(size_t dstSize,size_t cSrcSize)1821*01826a49SYabin Cui U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize)
1822*01826a49SYabin Cui {
1823*01826a49SYabin Cui assert(dstSize > 0);
1824*01826a49SYabin Cui assert(dstSize <= 128*1024);
1825*01826a49SYabin Cui #if defined(HUF_FORCE_DECOMPRESS_X1)
1826*01826a49SYabin Cui (void)dstSize;
1827*01826a49SYabin Cui (void)cSrcSize;
1828*01826a49SYabin Cui return 0;
1829*01826a49SYabin Cui #elif defined(HUF_FORCE_DECOMPRESS_X2)
1830*01826a49SYabin Cui (void)dstSize;
1831*01826a49SYabin Cui (void)cSrcSize;
1832*01826a49SYabin Cui return 1;
1833*01826a49SYabin Cui #else
1834*01826a49SYabin Cui /* decoder timing evaluation */
1835*01826a49SYabin Cui { U32 const Q = (cSrcSize >= dstSize) ? 15 : (U32)(cSrcSize * 16 / dstSize); /* Q < 16 */
1836*01826a49SYabin Cui U32 const D256 = (U32)(dstSize >> 8);
1837*01826a49SYabin Cui U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);
1838*01826a49SYabin Cui U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);
1839*01826a49SYabin Cui DTime1 += DTime1 >> 5; /* small advantage to algorithm using less memory, to reduce cache eviction */
1840*01826a49SYabin Cui return DTime1 < DTime0;
1841*01826a49SYabin Cui }
1842*01826a49SYabin Cui #endif
1843*01826a49SYabin Cui }
1844*01826a49SYabin Cui
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)1845*01826a49SYabin Cui size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
1846*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1847*01826a49SYabin Cui void* workSpace, size_t wkspSize, int flags)
1848*01826a49SYabin Cui {
1849*01826a49SYabin Cui /* validation checks */
1850*01826a49SYabin Cui if (dstSize == 0) return ERROR(dstSize_tooSmall);
1851*01826a49SYabin Cui if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
1852*01826a49SYabin Cui if (cSrcSize == dstSize) { ZSTD_memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
1853*01826a49SYabin Cui if (cSrcSize == 1) { ZSTD_memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
1854*01826a49SYabin Cui
1855*01826a49SYabin Cui { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
1856*01826a49SYabin Cui #if defined(HUF_FORCE_DECOMPRESS_X1)
1857*01826a49SYabin Cui (void)algoNb;
1858*01826a49SYabin Cui assert(algoNb == 0);
1859*01826a49SYabin Cui return HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,
1860*01826a49SYabin Cui cSrcSize, workSpace, wkspSize, flags);
1861*01826a49SYabin Cui #elif defined(HUF_FORCE_DECOMPRESS_X2)
1862*01826a49SYabin Cui (void)algoNb;
1863*01826a49SYabin Cui assert(algoNb == 1);
1864*01826a49SYabin Cui return HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,
1865*01826a49SYabin Cui cSrcSize, workSpace, wkspSize, flags);
1866*01826a49SYabin Cui #else
1867*01826a49SYabin Cui return algoNb ? HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,
1868*01826a49SYabin Cui cSrcSize, workSpace, wkspSize, flags):
1869*01826a49SYabin Cui HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,
1870*01826a49SYabin Cui cSrcSize, workSpace, wkspSize, flags);
1871*01826a49SYabin Cui #endif
1872*01826a49SYabin Cui }
1873*01826a49SYabin Cui }
1874*01826a49SYabin Cui
1875*01826a49SYabin Cui
HUF_decompress1X_usingDTable(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable,int flags)1876*01826a49SYabin Cui size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags)
1877*01826a49SYabin Cui {
1878*01826a49SYabin Cui DTableDesc const dtd = HUF_getDTableDesc(DTable);
1879*01826a49SYabin Cui #if defined(HUF_FORCE_DECOMPRESS_X1)
1880*01826a49SYabin Cui (void)dtd;
1881*01826a49SYabin Cui assert(dtd.tableType == 0);
1882*01826a49SYabin Cui return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);
1883*01826a49SYabin Cui #elif defined(HUF_FORCE_DECOMPRESS_X2)
1884*01826a49SYabin Cui (void)dtd;
1885*01826a49SYabin Cui assert(dtd.tableType == 1);
1886*01826a49SYabin Cui return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);
1887*01826a49SYabin Cui #else
1888*01826a49SYabin Cui return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags) :
1889*01826a49SYabin Cui HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);
1890*01826a49SYabin Cui #endif
1891*01826a49SYabin Cui }
1892*01826a49SYabin Cui
1893*01826a49SYabin Cui #ifndef HUF_FORCE_DECOMPRESS_X2
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)1894*01826a49SYabin Cui 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)
1895*01826a49SYabin Cui {
1896*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
1897*01826a49SYabin Cui
1898*01826a49SYabin Cui size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize, flags);
1899*01826a49SYabin Cui if (HUF_isError(hSize)) return hSize;
1900*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
1901*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
1902*01826a49SYabin Cui
1903*01826a49SYabin Cui return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, flags);
1904*01826a49SYabin Cui }
1905*01826a49SYabin Cui #endif
1906*01826a49SYabin Cui
HUF_decompress4X_usingDTable(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const HUF_DTable * DTable,int flags)1907*01826a49SYabin Cui size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags)
1908*01826a49SYabin Cui {
1909*01826a49SYabin Cui DTableDesc const dtd = HUF_getDTableDesc(DTable);
1910*01826a49SYabin Cui #if defined(HUF_FORCE_DECOMPRESS_X1)
1911*01826a49SYabin Cui (void)dtd;
1912*01826a49SYabin Cui assert(dtd.tableType == 0);
1913*01826a49SYabin Cui return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);
1914*01826a49SYabin Cui #elif defined(HUF_FORCE_DECOMPRESS_X2)
1915*01826a49SYabin Cui (void)dtd;
1916*01826a49SYabin Cui assert(dtd.tableType == 1);
1917*01826a49SYabin Cui return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);
1918*01826a49SYabin Cui #else
1919*01826a49SYabin Cui return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags) :
1920*01826a49SYabin Cui HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags);
1921*01826a49SYabin Cui #endif
1922*01826a49SYabin Cui }
1923*01826a49SYabin Cui
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)1924*01826a49SYabin Cui 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)
1925*01826a49SYabin Cui {
1926*01826a49SYabin Cui /* validation checks */
1927*01826a49SYabin Cui if (dstSize == 0) return ERROR(dstSize_tooSmall);
1928*01826a49SYabin Cui if (cSrcSize == 0) return ERROR(corruption_detected);
1929*01826a49SYabin Cui
1930*01826a49SYabin Cui { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
1931*01826a49SYabin Cui #if defined(HUF_FORCE_DECOMPRESS_X1)
1932*01826a49SYabin Cui (void)algoNb;
1933*01826a49SYabin Cui assert(algoNb == 0);
1934*01826a49SYabin Cui return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags);
1935*01826a49SYabin Cui #elif defined(HUF_FORCE_DECOMPRESS_X2)
1936*01826a49SYabin Cui (void)algoNb;
1937*01826a49SYabin Cui assert(algoNb == 1);
1938*01826a49SYabin Cui return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags);
1939*01826a49SYabin Cui #else
1940*01826a49SYabin Cui return algoNb ? HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags) :
1941*01826a49SYabin Cui HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags);
1942*01826a49SYabin Cui #endif
1943*01826a49SYabin Cui }
1944*01826a49SYabin Cui }
1945