xref: /aosp_15_r20/external/zstd/lib/common/zstd_internal.h (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui /*
2*01826a49SYabin Cui  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui  * All rights reserved.
4*01826a49SYabin Cui  *
5*01826a49SYabin Cui  * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui  * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui  * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui  */
10*01826a49SYabin Cui 
11*01826a49SYabin Cui #ifndef ZSTD_CCOMMON_H_MODULE
12*01826a49SYabin Cui #define ZSTD_CCOMMON_H_MODULE
13*01826a49SYabin Cui 
14*01826a49SYabin Cui /* this module contains definitions which must be identical
15*01826a49SYabin Cui  * across compression, decompression and dictBuilder.
16*01826a49SYabin Cui  * It also contains a few functions useful to at least 2 of them
17*01826a49SYabin Cui  * and which benefit from being inlined */
18*01826a49SYabin Cui 
19*01826a49SYabin Cui /*-*************************************
20*01826a49SYabin Cui *  Dependencies
21*01826a49SYabin Cui ***************************************/
22*01826a49SYabin Cui #include "compiler.h"
23*01826a49SYabin Cui #include "cpu.h"
24*01826a49SYabin Cui #include "mem.h"
25*01826a49SYabin Cui #include "debug.h"                 /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
26*01826a49SYabin Cui #include "error_private.h"
27*01826a49SYabin Cui #define ZSTD_STATIC_LINKING_ONLY
28*01826a49SYabin Cui #include "../zstd.h"
29*01826a49SYabin Cui #define FSE_STATIC_LINKING_ONLY
30*01826a49SYabin Cui #include "fse.h"
31*01826a49SYabin Cui #include "huf.h"
32*01826a49SYabin Cui #ifndef XXH_STATIC_LINKING_ONLY
33*01826a49SYabin Cui #  define XXH_STATIC_LINKING_ONLY  /* XXH64_state_t */
34*01826a49SYabin Cui #endif
35*01826a49SYabin Cui #include "xxhash.h"                /* XXH_reset, update, digest */
36*01826a49SYabin Cui #ifndef ZSTD_NO_TRACE
37*01826a49SYabin Cui #  include "zstd_trace.h"
38*01826a49SYabin Cui #else
39*01826a49SYabin Cui #  define ZSTD_TRACE 0
40*01826a49SYabin Cui #endif
41*01826a49SYabin Cui 
42*01826a49SYabin Cui #if defined (__cplusplus)
43*01826a49SYabin Cui extern "C" {
44*01826a49SYabin Cui #endif
45*01826a49SYabin Cui 
46*01826a49SYabin Cui /* ---- static assert (debug) --- */
47*01826a49SYabin Cui #define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
48*01826a49SYabin Cui #define ZSTD_isError ERR_isError   /* for inlining */
49*01826a49SYabin Cui #define FSE_isError  ERR_isError
50*01826a49SYabin Cui #define HUF_isError  ERR_isError
51*01826a49SYabin Cui 
52*01826a49SYabin Cui 
53*01826a49SYabin Cui /*-*************************************
54*01826a49SYabin Cui *  shared macros
55*01826a49SYabin Cui ***************************************/
56*01826a49SYabin Cui #undef MIN
57*01826a49SYabin Cui #undef MAX
58*01826a49SYabin Cui #define MIN(a,b) ((a)<(b) ? (a) : (b))
59*01826a49SYabin Cui #define MAX(a,b) ((a)>(b) ? (a) : (b))
60*01826a49SYabin Cui #define BOUNDED(min,val,max) (MAX(min,MIN(val,max)))
61*01826a49SYabin Cui 
62*01826a49SYabin Cui 
63*01826a49SYabin Cui /*-*************************************
64*01826a49SYabin Cui *  Common constants
65*01826a49SYabin Cui ***************************************/
66*01826a49SYabin Cui #define ZSTD_OPT_NUM    (1<<12)
67*01826a49SYabin Cui 
68*01826a49SYabin Cui #define ZSTD_REP_NUM      3                 /* number of repcodes */
69*01826a49SYabin Cui static UNUSED_ATTR const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
70*01826a49SYabin Cui 
71*01826a49SYabin Cui #define KB *(1 <<10)
72*01826a49SYabin Cui #define MB *(1 <<20)
73*01826a49SYabin Cui #define GB *(1U<<30)
74*01826a49SYabin Cui 
75*01826a49SYabin Cui #define BIT7 128
76*01826a49SYabin Cui #define BIT6  64
77*01826a49SYabin Cui #define BIT5  32
78*01826a49SYabin Cui #define BIT4  16
79*01826a49SYabin Cui #define BIT1   2
80*01826a49SYabin Cui #define BIT0   1
81*01826a49SYabin Cui 
82*01826a49SYabin Cui #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
83*01826a49SYabin Cui static UNUSED_ATTR const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
84*01826a49SYabin Cui static UNUSED_ATTR const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
85*01826a49SYabin Cui 
86*01826a49SYabin Cui #define ZSTD_FRAMEIDSIZE 4   /* magic number size */
87*01826a49SYabin Cui 
88*01826a49SYabin Cui #define ZSTD_BLOCKHEADERSIZE 3   /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
89*01826a49SYabin Cui static UNUSED_ATTR const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
90*01826a49SYabin Cui typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
91*01826a49SYabin Cui 
92*01826a49SYabin Cui #define ZSTD_FRAMECHECKSUMSIZE 4
93*01826a49SYabin Cui 
94*01826a49SYabin Cui #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
95*01826a49SYabin Cui #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */)   /* for a non-null block */
96*01826a49SYabin Cui #define MIN_LITERALS_FOR_4_STREAMS 6
97*01826a49SYabin Cui 
98*01826a49SYabin Cui typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
99*01826a49SYabin Cui 
100*01826a49SYabin Cui #define LONGNBSEQ 0x7F00
101*01826a49SYabin Cui 
102*01826a49SYabin Cui #define MINMATCH 3
103*01826a49SYabin Cui 
104*01826a49SYabin Cui #define Litbits  8
105*01826a49SYabin Cui #define LitHufLog 11
106*01826a49SYabin Cui #define MaxLit ((1<<Litbits) - 1)
107*01826a49SYabin Cui #define MaxML   52
108*01826a49SYabin Cui #define MaxLL   35
109*01826a49SYabin Cui #define DefaultMaxOff 28
110*01826a49SYabin Cui #define MaxOff  31
111*01826a49SYabin Cui #define MaxSeq MAX(MaxLL, MaxML)   /* Assumption : MaxOff < MaxLL,MaxML */
112*01826a49SYabin Cui #define MLFSELog    9
113*01826a49SYabin Cui #define LLFSELog    9
114*01826a49SYabin Cui #define OffFSELog   8
115*01826a49SYabin Cui #define MaxFSELog  MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
116*01826a49SYabin Cui #define MaxMLBits 16
117*01826a49SYabin Cui #define MaxLLBits 16
118*01826a49SYabin Cui 
119*01826a49SYabin Cui #define ZSTD_MAX_HUF_HEADER_SIZE 128 /* header + <= 127 byte tree description */
120*01826a49SYabin Cui /* Each table cannot take more than #symbols * FSELog bits */
121*01826a49SYabin Cui #define ZSTD_MAX_FSE_HEADERS_SIZE (((MaxML + 1) * MLFSELog + (MaxLL + 1) * LLFSELog + (MaxOff + 1) * OffFSELog + 7) / 8)
122*01826a49SYabin Cui 
123*01826a49SYabin Cui static UNUSED_ATTR const U8 LL_bits[MaxLL+1] = {
124*01826a49SYabin Cui      0, 0, 0, 0, 0, 0, 0, 0,
125*01826a49SYabin Cui      0, 0, 0, 0, 0, 0, 0, 0,
126*01826a49SYabin Cui      1, 1, 1, 1, 2, 2, 3, 3,
127*01826a49SYabin Cui      4, 6, 7, 8, 9,10,11,12,
128*01826a49SYabin Cui     13,14,15,16
129*01826a49SYabin Cui };
130*01826a49SYabin Cui static UNUSED_ATTR const S16 LL_defaultNorm[MaxLL+1] = {
131*01826a49SYabin Cui      4, 3, 2, 2, 2, 2, 2, 2,
132*01826a49SYabin Cui      2, 2, 2, 2, 2, 1, 1, 1,
133*01826a49SYabin Cui      2, 2, 2, 2, 2, 2, 2, 2,
134*01826a49SYabin Cui      2, 3, 2, 1, 1, 1, 1, 1,
135*01826a49SYabin Cui     -1,-1,-1,-1
136*01826a49SYabin Cui };
137*01826a49SYabin Cui #define LL_DEFAULTNORMLOG 6  /* for static allocation */
138*01826a49SYabin Cui static UNUSED_ATTR const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
139*01826a49SYabin Cui 
140*01826a49SYabin Cui static UNUSED_ATTR const U8 ML_bits[MaxML+1] = {
141*01826a49SYabin Cui      0, 0, 0, 0, 0, 0, 0, 0,
142*01826a49SYabin Cui      0, 0, 0, 0, 0, 0, 0, 0,
143*01826a49SYabin Cui      0, 0, 0, 0, 0, 0, 0, 0,
144*01826a49SYabin Cui      0, 0, 0, 0, 0, 0, 0, 0,
145*01826a49SYabin Cui      1, 1, 1, 1, 2, 2, 3, 3,
146*01826a49SYabin Cui      4, 4, 5, 7, 8, 9,10,11,
147*01826a49SYabin Cui     12,13,14,15,16
148*01826a49SYabin Cui };
149*01826a49SYabin Cui static UNUSED_ATTR const S16 ML_defaultNorm[MaxML+1] = {
150*01826a49SYabin Cui      1, 4, 3, 2, 2, 2, 2, 2,
151*01826a49SYabin Cui      2, 1, 1, 1, 1, 1, 1, 1,
152*01826a49SYabin Cui      1, 1, 1, 1, 1, 1, 1, 1,
153*01826a49SYabin Cui      1, 1, 1, 1, 1, 1, 1, 1,
154*01826a49SYabin Cui      1, 1, 1, 1, 1, 1, 1, 1,
155*01826a49SYabin Cui      1, 1, 1, 1, 1, 1,-1,-1,
156*01826a49SYabin Cui     -1,-1,-1,-1,-1
157*01826a49SYabin Cui };
158*01826a49SYabin Cui #define ML_DEFAULTNORMLOG 6  /* for static allocation */
159*01826a49SYabin Cui static UNUSED_ATTR const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
160*01826a49SYabin Cui 
161*01826a49SYabin Cui static UNUSED_ATTR const S16 OF_defaultNorm[DefaultMaxOff+1] = {
162*01826a49SYabin Cui      1, 1, 1, 1, 1, 1, 2, 2,
163*01826a49SYabin Cui      2, 1, 1, 1, 1, 1, 1, 1,
164*01826a49SYabin Cui      1, 1, 1, 1, 1, 1, 1, 1,
165*01826a49SYabin Cui     -1,-1,-1,-1,-1
166*01826a49SYabin Cui };
167*01826a49SYabin Cui #define OF_DEFAULTNORMLOG 5  /* for static allocation */
168*01826a49SYabin Cui static UNUSED_ATTR const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
169*01826a49SYabin Cui 
170*01826a49SYabin Cui 
171*01826a49SYabin Cui /*-*******************************************
172*01826a49SYabin Cui *  Shared functions to include for inlining
173*01826a49SYabin Cui *********************************************/
ZSTD_copy8(void * dst,const void * src)174*01826a49SYabin Cui static void ZSTD_copy8(void* dst, const void* src) {
175*01826a49SYabin Cui #if defined(ZSTD_ARCH_ARM_NEON)
176*01826a49SYabin Cui     vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));
177*01826a49SYabin Cui #else
178*01826a49SYabin Cui     ZSTD_memcpy(dst, src, 8);
179*01826a49SYabin Cui #endif
180*01826a49SYabin Cui }
181*01826a49SYabin Cui #define COPY8(d,s) do { ZSTD_copy8(d,s); d+=8; s+=8; } while (0)
182*01826a49SYabin Cui 
183*01826a49SYabin Cui /* Need to use memmove here since the literal buffer can now be located within
184*01826a49SYabin Cui    the dst buffer. In circumstances where the op "catches up" to where the
185*01826a49SYabin Cui    literal buffer is, there can be partial overlaps in this call on the final
186*01826a49SYabin Cui    copy if the literal is being shifted by less than 16 bytes. */
ZSTD_copy16(void * dst,const void * src)187*01826a49SYabin Cui static void ZSTD_copy16(void* dst, const void* src) {
188*01826a49SYabin Cui #if defined(ZSTD_ARCH_ARM_NEON)
189*01826a49SYabin Cui     vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));
190*01826a49SYabin Cui #elif defined(ZSTD_ARCH_X86_SSE2)
191*01826a49SYabin Cui     _mm_storeu_si128((__m128i*)dst, _mm_loadu_si128((const __m128i*)src));
192*01826a49SYabin Cui #elif defined(__clang__)
193*01826a49SYabin Cui     ZSTD_memmove(dst, src, 16);
194*01826a49SYabin Cui #else
195*01826a49SYabin Cui     /* ZSTD_memmove is not inlined properly by gcc */
196*01826a49SYabin Cui     BYTE copy16_buf[16];
197*01826a49SYabin Cui     ZSTD_memcpy(copy16_buf, src, 16);
198*01826a49SYabin Cui     ZSTD_memcpy(dst, copy16_buf, 16);
199*01826a49SYabin Cui #endif
200*01826a49SYabin Cui }
201*01826a49SYabin Cui #define COPY16(d,s) do { ZSTD_copy16(d,s); d+=16; s+=16; } while (0)
202*01826a49SYabin Cui 
203*01826a49SYabin Cui #define WILDCOPY_OVERLENGTH 32
204*01826a49SYabin Cui #define WILDCOPY_VECLEN 16
205*01826a49SYabin Cui 
206*01826a49SYabin Cui typedef enum {
207*01826a49SYabin Cui     ZSTD_no_overlap,
208*01826a49SYabin Cui     ZSTD_overlap_src_before_dst
209*01826a49SYabin Cui     /*  ZSTD_overlap_dst_before_src, */
210*01826a49SYabin Cui } ZSTD_overlap_e;
211*01826a49SYabin Cui 
212*01826a49SYabin Cui /*! ZSTD_wildcopy() :
213*01826a49SYabin Cui  *  Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
214*01826a49SYabin Cui  *  @param ovtype controls the overlap detection
215*01826a49SYabin Cui  *         - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
216*01826a49SYabin Cui  *         - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
217*01826a49SYabin Cui  *           The src buffer must be before the dst buffer.
218*01826a49SYabin Cui  */
219*01826a49SYabin Cui MEM_STATIC FORCE_INLINE_ATTR
ZSTD_wildcopy(void * dst,const void * src,ptrdiff_t length,ZSTD_overlap_e const ovtype)220*01826a49SYabin Cui void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
221*01826a49SYabin Cui {
222*01826a49SYabin Cui     ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
223*01826a49SYabin Cui     const BYTE* ip = (const BYTE*)src;
224*01826a49SYabin Cui     BYTE* op = (BYTE*)dst;
225*01826a49SYabin Cui     BYTE* const oend = op + length;
226*01826a49SYabin Cui 
227*01826a49SYabin Cui     if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
228*01826a49SYabin Cui         /* Handle short offset copies. */
229*01826a49SYabin Cui         do {
230*01826a49SYabin Cui             COPY8(op, ip);
231*01826a49SYabin Cui         } while (op < oend);
232*01826a49SYabin Cui     } else {
233*01826a49SYabin Cui         assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);
234*01826a49SYabin Cui         /* Separate out the first COPY16() call because the copy length is
235*01826a49SYabin Cui          * almost certain to be short, so the branches have different
236*01826a49SYabin Cui          * probabilities. Since it is almost certain to be short, only do
237*01826a49SYabin Cui          * one COPY16() in the first call. Then, do two calls per loop since
238*01826a49SYabin Cui          * at that point it is more likely to have a high trip count.
239*01826a49SYabin Cui          */
240*01826a49SYabin Cui         ZSTD_copy16(op, ip);
241*01826a49SYabin Cui         if (16 >= length) return;
242*01826a49SYabin Cui         op += 16;
243*01826a49SYabin Cui         ip += 16;
244*01826a49SYabin Cui         do {
245*01826a49SYabin Cui             COPY16(op, ip);
246*01826a49SYabin Cui             COPY16(op, ip);
247*01826a49SYabin Cui         }
248*01826a49SYabin Cui         while (op < oend);
249*01826a49SYabin Cui     }
250*01826a49SYabin Cui }
251*01826a49SYabin Cui 
ZSTD_limitCopy(void * dst,size_t dstCapacity,const void * src,size_t srcSize)252*01826a49SYabin Cui MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
253*01826a49SYabin Cui {
254*01826a49SYabin Cui     size_t const length = MIN(dstCapacity, srcSize);
255*01826a49SYabin Cui     if (length > 0) {
256*01826a49SYabin Cui         ZSTD_memcpy(dst, src, length);
257*01826a49SYabin Cui     }
258*01826a49SYabin Cui     return length;
259*01826a49SYabin Cui }
260*01826a49SYabin Cui 
261*01826a49SYabin Cui /* define "workspace is too large" as this number of times larger than needed */
262*01826a49SYabin Cui #define ZSTD_WORKSPACETOOLARGE_FACTOR 3
263*01826a49SYabin Cui 
264*01826a49SYabin Cui /* when workspace is continuously too large
265*01826a49SYabin Cui  * during at least this number of times,
266*01826a49SYabin Cui  * context's memory usage is considered wasteful,
267*01826a49SYabin Cui  * because it's sized to handle a worst case scenario which rarely happens.
268*01826a49SYabin Cui  * In which case, resize it down to free some memory */
269*01826a49SYabin Cui #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128
270*01826a49SYabin Cui 
271*01826a49SYabin Cui /* Controls whether the input/output buffer is buffered or stable. */
272*01826a49SYabin Cui typedef enum {
273*01826a49SYabin Cui     ZSTD_bm_buffered = 0,  /* Buffer the input/output */
274*01826a49SYabin Cui     ZSTD_bm_stable = 1     /* ZSTD_inBuffer/ZSTD_outBuffer is stable */
275*01826a49SYabin Cui } ZSTD_bufferMode_e;
276*01826a49SYabin Cui 
277*01826a49SYabin Cui 
278*01826a49SYabin Cui /*-*******************************************
279*01826a49SYabin Cui *  Private declarations
280*01826a49SYabin Cui *********************************************/
281*01826a49SYabin Cui typedef struct seqDef_s {
282*01826a49SYabin Cui     U32 offBase;   /* offBase == Offset + ZSTD_REP_NUM, or repcode 1,2,3 */
283*01826a49SYabin Cui     U16 litLength;
284*01826a49SYabin Cui     U16 mlBase;    /* mlBase == matchLength - MINMATCH */
285*01826a49SYabin Cui } seqDef;
286*01826a49SYabin Cui 
287*01826a49SYabin Cui /* Controls whether seqStore has a single "long" litLength or matchLength. See seqStore_t. */
288*01826a49SYabin Cui typedef enum {
289*01826a49SYabin Cui     ZSTD_llt_none = 0,             /* no longLengthType */
290*01826a49SYabin Cui     ZSTD_llt_literalLength = 1,    /* represents a long literal */
291*01826a49SYabin Cui     ZSTD_llt_matchLength = 2       /* represents a long match */
292*01826a49SYabin Cui } ZSTD_longLengthType_e;
293*01826a49SYabin Cui 
294*01826a49SYabin Cui typedef struct {
295*01826a49SYabin Cui     seqDef* sequencesStart;
296*01826a49SYabin Cui     seqDef* sequences;      /* ptr to end of sequences */
297*01826a49SYabin Cui     BYTE*  litStart;
298*01826a49SYabin Cui     BYTE*  lit;             /* ptr to end of literals */
299*01826a49SYabin Cui     BYTE*  llCode;
300*01826a49SYabin Cui     BYTE*  mlCode;
301*01826a49SYabin Cui     BYTE*  ofCode;
302*01826a49SYabin Cui     size_t maxNbSeq;
303*01826a49SYabin Cui     size_t maxNbLit;
304*01826a49SYabin Cui 
305*01826a49SYabin Cui     /* longLengthPos and longLengthType to allow us to represent either a single litLength or matchLength
306*01826a49SYabin Cui      * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment
307*01826a49SYabin Cui      * the existing value of the litLength or matchLength by 0x10000.
308*01826a49SYabin Cui      */
309*01826a49SYabin Cui     ZSTD_longLengthType_e longLengthType;
310*01826a49SYabin Cui     U32                   longLengthPos;  /* Index of the sequence to apply long length modification to */
311*01826a49SYabin Cui } seqStore_t;
312*01826a49SYabin Cui 
313*01826a49SYabin Cui typedef struct {
314*01826a49SYabin Cui     U32 litLength;
315*01826a49SYabin Cui     U32 matchLength;
316*01826a49SYabin Cui } ZSTD_sequenceLength;
317*01826a49SYabin Cui 
318*01826a49SYabin Cui /**
319*01826a49SYabin Cui  * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences
320*01826a49SYabin Cui  * indicated by longLengthPos and longLengthType, and adds MINMATCH back to matchLength.
321*01826a49SYabin Cui  */
ZSTD_getSequenceLength(seqStore_t const * seqStore,seqDef const * seq)322*01826a49SYabin Cui MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq)
323*01826a49SYabin Cui {
324*01826a49SYabin Cui     ZSTD_sequenceLength seqLen;
325*01826a49SYabin Cui     seqLen.litLength = seq->litLength;
326*01826a49SYabin Cui     seqLen.matchLength = seq->mlBase + MINMATCH;
327*01826a49SYabin Cui     if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {
328*01826a49SYabin Cui         if (seqStore->longLengthType == ZSTD_llt_literalLength) {
329*01826a49SYabin Cui             seqLen.litLength += 0x10000;
330*01826a49SYabin Cui         }
331*01826a49SYabin Cui         if (seqStore->longLengthType == ZSTD_llt_matchLength) {
332*01826a49SYabin Cui             seqLen.matchLength += 0x10000;
333*01826a49SYabin Cui         }
334*01826a49SYabin Cui     }
335*01826a49SYabin Cui     return seqLen;
336*01826a49SYabin Cui }
337*01826a49SYabin Cui 
338*01826a49SYabin Cui /**
339*01826a49SYabin Cui  * Contains the compressed frame size and an upper-bound for the decompressed frame size.
340*01826a49SYabin Cui  * Note: before using `compressedSize`, check for errors using ZSTD_isError().
341*01826a49SYabin Cui  *       similarly, before using `decompressedBound`, check for errors using:
342*01826a49SYabin Cui  *          `decompressedBound != ZSTD_CONTENTSIZE_ERROR`
343*01826a49SYabin Cui  */
344*01826a49SYabin Cui typedef struct {
345*01826a49SYabin Cui     size_t nbBlocks;
346*01826a49SYabin Cui     size_t compressedSize;
347*01826a49SYabin Cui     unsigned long long decompressedBound;
348*01826a49SYabin Cui } ZSTD_frameSizeInfo;   /* decompress & legacy */
349*01826a49SYabin Cui 
350*01826a49SYabin Cui const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);   /* compress & dictBuilder */
351*01826a49SYabin Cui int ZSTD_seqToCodes(const seqStore_t* seqStorePtr);   /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
352*01826a49SYabin Cui 
353*01826a49SYabin Cui 
354*01826a49SYabin Cui /* ZSTD_invalidateRepCodes() :
355*01826a49SYabin Cui  * ensures next compression will not use repcodes from previous block.
356*01826a49SYabin Cui  * Note : only works with regular variant;
357*01826a49SYabin Cui  *        do not use with extDict variant ! */
358*01826a49SYabin Cui void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx);   /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
359*01826a49SYabin Cui 
360*01826a49SYabin Cui 
361*01826a49SYabin Cui typedef struct {
362*01826a49SYabin Cui     blockType_e blockType;
363*01826a49SYabin Cui     U32 lastBlock;
364*01826a49SYabin Cui     U32 origSize;
365*01826a49SYabin Cui } blockProperties_t;   /* declared here for decompress and fullbench */
366*01826a49SYabin Cui 
367*01826a49SYabin Cui /*! ZSTD_getcBlockSize() :
368*01826a49SYabin Cui  *  Provides the size of compressed block from block header `src` */
369*01826a49SYabin Cui /*  Used by: decompress, fullbench */
370*01826a49SYabin Cui size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
371*01826a49SYabin Cui                           blockProperties_t* bpPtr);
372*01826a49SYabin Cui 
373*01826a49SYabin Cui /*! ZSTD_decodeSeqHeaders() :
374*01826a49SYabin Cui  *  decode sequence header from src */
375*01826a49SYabin Cui /*  Used by: zstd_decompress_block, fullbench */
376*01826a49SYabin Cui size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
377*01826a49SYabin Cui                        const void* src, size_t srcSize);
378*01826a49SYabin Cui 
379*01826a49SYabin Cui /**
380*01826a49SYabin Cui  * @returns true iff the CPU supports dynamic BMI2 dispatch.
381*01826a49SYabin Cui  */
ZSTD_cpuSupportsBmi2(void)382*01826a49SYabin Cui MEM_STATIC int ZSTD_cpuSupportsBmi2(void)
383*01826a49SYabin Cui {
384*01826a49SYabin Cui     ZSTD_cpuid_t cpuid = ZSTD_cpuid();
385*01826a49SYabin Cui     return ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid);
386*01826a49SYabin Cui }
387*01826a49SYabin Cui 
388*01826a49SYabin Cui #if defined (__cplusplus)
389*01826a49SYabin Cui }
390*01826a49SYabin Cui #endif
391*01826a49SYabin Cui 
392*01826a49SYabin Cui #endif   /* ZSTD_CCOMMON_H_MODULE */
393