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