2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
10 #ifndef ZSTD_CCOMMON_H_MODULE
11 #define ZSTD_CCOMMON_H_MODULE
13 /*-*******************************************************
15 *********************************************************/
16 #ifdef _MSC_VER /* Visual Studio */
17 # define FORCE_INLINE static __forceinline
18 # include <intrin.h> /* For Visual 2005 */
19 # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
20 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
21 # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
22 # pragma warning(disable : 4324) /* disable: C4324: padded structure */
24 # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
26 # define FORCE_INLINE static inline __attribute__((always_inline))
28 # define FORCE_INLINE static inline
31 # define FORCE_INLINE static
32 # endif /* __STDC_VERSION__ */
36 # define FORCE_NOINLINE static __declspec(noinline)
39 # define FORCE_NOINLINE static __attribute__((__noinline__))
41 # define FORCE_NOINLINE static
46 /*-*************************************
48 ***************************************/
50 #include "error_private.h"
51 #define ZSTD_STATIC_LINKING_ONLY
53 #ifndef XXH_STATIC_LINKING_ONLY
54 # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
56 #include "xxhash.h" /* XXH_reset, update, digest */
59 /*-*************************************
61 ***************************************/
62 #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=1)
66 # define assert(condition) ((void)0)
70 #define ZSTD_STATIC_ASSERT(c) { enum { ZSTD_static_assert = 1/(int)(!!(c)) }; }
72 #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=2)
74 /* recommended values for ZSTD_DEBUG display levels :
75 * 1 : no display, enables assert() only
76 * 2 : reserved for currently active debugging path
77 * 3 : events once per object lifetime (CCtx, CDict)
78 * 4 : events once per frame
79 * 5 : events once per block
80 * 6 : events once per sequence (*very* verbose) */
81 # define DEBUGLOG(l, ...) { \
82 if (l<=ZSTD_DEBUG) { \
83 fprintf(stderr, __FILE__ ": "); \
84 fprintf(stderr, __VA_ARGS__); \
85 fprintf(stderr, " \n"); \
88 # define DEBUGLOG(l, ...) {} /* disabled */
92 /*-*************************************
94 ***************************************/
97 #define MIN(a,b) ((a)<(b) ? (a) : (b))
98 #define MAX(a,b) ((a)>(b) ? (a) : (b))
99 #define CHECK_F(f) { size_t const errcod = f; if (ERR_isError(errcod)) return errcod; } /* check and Forward error code */
100 #define CHECK_E(f, e) { size_t const errcod = f; if (ERR_isError(errcod)) return ERROR(e); } /* check and send Error code */
103 /*-*************************************
105 ***************************************/
106 #define ZSTD_OPT_NUM (1<<12)
108 #define ZSTD_REP_NUM 3 /* number of repcodes */
109 #define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
110 #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
111 #define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
112 static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
125 #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
126 static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
127 static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
129 #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
130 static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
131 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
133 #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
134 #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
137 typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
139 #define LONGNBSEQ 0x7F00
144 #define MaxLit ((1<<Litbits) - 1)
148 #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
153 static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
154 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
156 static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
157 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
159 #define LL_DEFAULTNORMLOG 6 /* for static allocation */
160 static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
162 static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
166 static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
167 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
168 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
170 #define ML_DEFAULTNORMLOG 6 /* for static allocation */
171 static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
173 static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
174 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
175 #define OF_DEFAULTNORMLOG 5 /* for static allocation */
176 static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
179 /*-*******************************************
180 * Shared functions to include for inlining
181 *********************************************/
182 static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
183 #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
185 /*! ZSTD_wildcopy() :
186 * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
187 #define WILDCOPY_OVERLENGTH 8
188 MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
190 const BYTE* ip = (const BYTE*)src;
191 BYTE* op = (BYTE*)dst;
192 BYTE* const oend = op + length;
198 MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
200 const BYTE* ip = (const BYTE*)src;
201 BYTE* op = (BYTE*)dst;
202 BYTE* const oend = (BYTE*)dstEnd;
209 /*-*******************************************
211 *********************************************/
212 typedef struct ZSTD_stats_s ZSTD_stats_t;
224 U32 rep[ZSTD_REP_NUM];
228 typedef struct seqDef_s {
236 seqDef* sequencesStart;
243 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
246 ZSTD_optimal_t* priceTable;
247 ZSTD_match_t* matchTable;
248 U32* matchLengthFreq;
257 U32 log2matchLengthSum;
259 U32 log2litLengthSum;
266 const BYTE* cachedLiterals;
269 const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
270 void ZSTD_seqToCodes(const seqStore_t* seqStorePtr);
272 /* custom memory allocation functions */
273 void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
274 void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
275 void ZSTD_free(void* ptr, ZSTD_customMem customMem);
278 /*====== common function ======*/
280 MEM_STATIC U32 ZSTD_highbit32(U32 val)
282 # if defined(_MSC_VER) /* Visual */
284 _BitScanReverse(&r, val);
286 # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
287 return 31 - __builtin_clz(val);
288 # else /* Software version */
289 static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
297 r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
303 /* hidden functions */
305 /* ZSTD_invalidateRepCodes() :
306 * ensures next compression will not use repcodes from previous block.
307 * Note : only works with regular variant;
308 * do not use with extDict variant ! */
309 void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx);
312 /*! ZSTD_initCStream_internal() :
313 * Private use only. Init streaming operation.
314 * expects params to be valid.
315 * must receive dict, or cdict, or none, but not both.
316 * @return : 0, or an error code */
317 size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs,
318 const void* dict, size_t dictSize,
319 const ZSTD_CDict* cdict,
320 ZSTD_parameters params, unsigned long long pledgedSrcSize);
322 /*! ZSTD_compressStream_generic() :
323 * Private use only. To be called from zstdmt_compress.c in single-thread mode. */
324 size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
325 ZSTD_outBuffer* output,
326 ZSTD_inBuffer* input,
327 ZSTD_EndDirective const flushMode);
329 /*! ZSTD_getParamsFromCDict() :
330 * as the name implies */
331 ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict);
334 #endif /* ZSTD_CCOMMON_H_MODULE */