2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 /* This header contains definitions
12 * that shall **only** be used by modules within lib/compress.
15 #ifndef ZSTD_COMPRESS_H
16 #define ZSTD_COMPRESS_H
18 /*-*************************************
20 ***************************************/
21 #include "zstd_internal.h"
22 #ifdef ZSTD_MULTITHREAD
23 # include "zstdmt_compress.h"
26 #if defined (__cplusplus)
30 /*-*************************************
32 ***************************************/
33 static const U32 g_searchStrength = 8;
34 #define HASH_READ_SIZE 8
37 /*-*************************************
38 * Context memory management
39 ***************************************/
40 typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e;
41 typedef enum { zcss_init=0, zcss_load, zcss_flush } ZSTD_cStreamStage;
43 typedef struct ZSTD_prefixDict_s {
46 ZSTD_dictMode_e dictMode;
50 U32 hufCTable[HUF_CTABLE_SIZE_U32(255)];
51 FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
52 FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
53 FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
54 U32 workspace[HUF_WORKSPACE_SIZE_U32];
55 HUF_repeat hufCTable_repeatMode;
56 FSE_repeat offcode_repeatMode;
57 FSE_repeat matchlength_repeatMode;
58 FSE_repeat litlength_repeatMode;
59 } ZSTD_entropyCTables_t;
71 U32 rep[ZSTD_REP_NUM];
75 /* All tables are allocated inside cctx->workspace by ZSTD_resetCCtx_internal() */
76 U32* litFreq; /* table of literals statistics, of size 256 */
77 U32* litLengthFreq; /* table of litLength statistics, of size (MaxLL+1) */
78 U32* matchLengthFreq; /* table of matchLength statistics, of size (MaxML+1) */
79 U32* offCodeFreq; /* table of offCode statistics, of size (MaxOff+1) */
80 ZSTD_match_t* matchTable; /* list of found matches, of size ZSTD_OPT_NUM+1 */
81 ZSTD_optimal_t* priceTable; /* All positions tracked by optimal parser, of size ZSTD_OPT_NUM+1 */
83 U32 litSum; /* nb of literals */
84 U32 litLengthSum; /* nb of litLength codes */
85 U32 matchLengthSum; /* nb of matchLength codes */
86 U32 offCodeSum; /* nb of offset codes */
87 /* begin updated by ZSTD_setLog2Prices */
88 U32 log2litSum; /* pow2 to compare log2(litfreq) to */
89 U32 log2litLengthSum; /* pow2 to compare log2(llfreq) to */
90 U32 log2matchLengthSum; /* pow2 to compare log2(mlfreq) to */
91 U32 log2offCodeSum; /* pow2 to compare log2(offreq) to */
92 /* end : updated by ZSTD_setLog2Prices */
93 U32 staticPrices; /* prices follow a pre-defined cost structure, statistics are irrelevant */
102 ldmEntry_t* hashTable;
103 BYTE* bucketOffsets; /* Next position in bucket to insert entry */
104 U64 hashPower; /* Used to compute the rolling hash.
105 * Depends on ldmParams.minMatchLength */
109 U32 enableLdm; /* 1 if enable long distance matching */
110 U32 hashLog; /* Log size of hashTable */
111 U32 bucketSizeLog; /* Log bucket size for collision resolution, at most 8 */
112 U32 minMatchLength; /* Minimum match length */
113 U32 hashEveryLog; /* Log number of entries to skip */
116 struct ZSTD_CCtx_params_s {
117 ZSTD_format_e format;
118 ZSTD_compressionParameters cParams;
119 ZSTD_frameParameters fParams;
121 int compressionLevel;
122 U32 forceWindow; /* force back-references to respect limit of
123 * 1<<wLog, even for dictionary */
125 /* Multithreading: used to pass parameters to mtctx */
128 unsigned overlapSizeLog;
130 /* Long distance matching parameters */
131 ldmParams_t ldmParams;
133 /* For use with createCCtxParams() and freeCCtxParams() only */
134 ZSTD_customMem customMem;
136 }; /* typedef'd to ZSTD_CCtx_params within "zstd.h" */
139 const BYTE* nextSrc; /* next block here to continue on current prefix */
140 const BYTE* base; /* All regular indexes relative to this position */
141 const BYTE* dictBase; /* extDict indexes relative to this position */
142 U32 dictLimit; /* below that point, need extDict */
143 U32 lowLimit; /* below that point, no more data */
144 U32 nextToUpdate; /* index from which to continue dictionary update */
145 U32 nextToUpdate3; /* index from which to continue dictionary update */
146 U32 hashLog3; /* dispatch table : larger == faster, more memory */
147 U32 loadedDictEnd; /* index of end of dictionary */
148 ZSTD_compressionStage_e stage;
150 ZSTD_CCtx_params requestedParams;
151 ZSTD_CCtx_params appliedParams;
153 size_t workSpaceSize;
155 U64 pledgedSrcSizePlusOne; /* this way, 0 (default) == unknown */
157 XXH64_state_t xxhState;
158 ZSTD_customMem customMem;
161 seqStore_t seqStore; /* sequences storage ptrs */
163 ldmState_t ldmState; /* long distance matching state */
167 ZSTD_entropyCTables_t* entropy;
177 size_t outBuffContentSize;
178 size_t outBuffFlushedSize;
179 ZSTD_cStreamStage streamStage;
183 ZSTD_CDict* cdictLocal;
184 const ZSTD_CDict* cdict;
185 ZSTD_prefixDict prefixDict; /* single-usage dictionary */
187 /* Multi-threading */
188 #ifdef ZSTD_MULTITHREAD
194 MEM_STATIC U32 ZSTD_LLcode(U32 litLength)
196 static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7,
197 8, 9, 10, 11, 12, 13, 14, 15,
198 16, 16, 17, 17, 18, 18, 19, 19,
199 20, 20, 20, 20, 21, 21, 21, 21,
200 22, 22, 22, 22, 22, 22, 22, 22,
201 23, 23, 23, 23, 23, 23, 23, 23,
202 24, 24, 24, 24, 24, 24, 24, 24,
203 24, 24, 24, 24, 24, 24, 24, 24 };
204 static const U32 LL_deltaCode = 19;
205 return (litLength > 63) ? ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
209 * note : mlBase = matchLength - MINMATCH;
210 * because it's the format it's stored in seqStore->sequences */
211 MEM_STATIC U32 ZSTD_MLcode(U32 mlBase)
213 static const BYTE ML_Code[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
214 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
215 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
216 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
217 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
218 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
219 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
220 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
221 static const U32 ML_deltaCode = 36;
222 return (mlBase > 127) ? ZSTD_highbit32(mlBase) + ML_deltaCode : ML_Code[mlBase];
225 /*! ZSTD_storeSeq() :
226 * Store a sequence (literal length, literals, offset code and match length code) into seqStore_t.
227 * `offsetCode` : distance to match + 3 (values 1-3 are repCodes).
228 * `mlBase` : matchLength - MINMATCH
230 MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const void* literals, U32 offsetCode, size_t mlBase)
232 #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 6)
233 static const BYTE* g_start = NULL;
234 if (g_start==NULL) g_start = (const BYTE*)literals; /* note : index only works for compression within a single segment */
235 { U32 const pos = (U32)((const BYTE*)literals - g_start);
236 DEBUGLOG(6, "Cpos%7u :%3u literals, match%3u bytes at dist.code%7u",
237 pos, (U32)litLength, (U32)mlBase+MINMATCH, (U32)offsetCode);
241 assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + 128 KB);
242 ZSTD_wildcopy(seqStorePtr->lit, literals, litLength);
243 seqStorePtr->lit += litLength;
246 if (litLength>0xFFFF) {
247 assert(seqStorePtr->longLengthID == 0); /* there can only be a single long length */
248 seqStorePtr->longLengthID = 1;
249 seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
251 seqStorePtr->sequences[0].litLength = (U16)litLength;
254 seqStorePtr->sequences[0].offset = offsetCode + 1;
258 assert(seqStorePtr->longLengthID == 0); /* there can only be a single long length */
259 seqStorePtr->longLengthID = 2;
260 seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
262 seqStorePtr->sequences[0].matchLength = (U16)mlBase;
264 seqStorePtr->sequences++;
268 /*-*************************************
269 * Match length counter
270 ***************************************/
271 static unsigned ZSTD_NbCommonBytes (size_t val)
273 if (MEM_isLittleEndian()) {
275 # if defined(_MSC_VER) && defined(_WIN64)
277 _BitScanForward64( &r, (U64)val );
278 return (unsigned)(r>>3);
279 # elif defined(__GNUC__) && (__GNUC__ >= 4)
280 return (__builtin_ctzll((U64)val) >> 3);
282 static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2,
283 0, 3, 1, 3, 1, 4, 2, 7,
284 0, 2, 3, 6, 1, 5, 3, 5,
285 1, 3, 4, 4, 2, 5, 6, 7,
286 7, 0, 1, 2, 3, 3, 4, 6,
287 2, 6, 5, 5, 3, 4, 5, 6,
288 7, 1, 2, 4, 6, 4, 4, 5,
289 7, 2, 6, 5, 7, 6, 7, 7 };
290 return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
292 } else { /* 32 bits */
293 # if defined(_MSC_VER)
295 _BitScanForward( &r, (U32)val );
296 return (unsigned)(r>>3);
297 # elif defined(__GNUC__) && (__GNUC__ >= 3)
298 return (__builtin_ctz((U32)val) >> 3);
300 static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0,
301 3, 2, 2, 1, 3, 2, 0, 1,
302 3, 3, 1, 2, 2, 2, 2, 0,
303 3, 1, 2, 0, 1, 0, 1, 1 };
304 return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
307 } else { /* Big Endian CPU */
309 # if defined(_MSC_VER) && defined(_WIN64)
311 _BitScanReverse64( &r, val );
312 return (unsigned)(r>>3);
313 # elif defined(__GNUC__) && (__GNUC__ >= 4) && __has_builtin(__builtin_clzll)
314 return (__builtin_clzll(val) >> 3);
317 const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */
318 if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; }
319 if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
323 } else { /* 32 bits */
324 # if defined(_MSC_VER)
326 _BitScanReverse( &r, (unsigned long)val );
327 return (unsigned)(r>>3);
328 # elif defined(__GNUC__) && (__GNUC__ >= 3) && __has_builtin(__builtin_clz)
329 return (__builtin_clz((U32)val) >> 3);
332 if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
340 MEM_STATIC size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* const pInLimit)
342 const BYTE* const pStart = pIn;
343 const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1);
345 if (pIn < pInLoopLimit) {
346 { size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
347 if (diff) return ZSTD_NbCommonBytes(diff); }
348 pIn+=sizeof(size_t); pMatch+=sizeof(size_t);
349 while (pIn < pInLoopLimit) {
350 size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
351 if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
352 pIn += ZSTD_NbCommonBytes(diff);
353 return (size_t)(pIn - pStart);
355 if (MEM_64bits() && (pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
356 if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
357 if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
358 return (size_t)(pIn - pStart);
361 /** ZSTD_count_2segments() :
362 * can count match length with `ip` & `match` in 2 different segments.
363 * convention : on reaching mEnd, match count continue starting from iStart
365 MEM_STATIC size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart)
367 const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd);
368 size_t const matchLength = ZSTD_count(ip, match, vEnd);
369 if (match + matchLength != mEnd) return matchLength;
370 return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd);
374 /*-*************************************
376 ***************************************/
377 static const U32 prime3bytes = 506832829U;
378 static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32-24)) * prime3bytes) >> (32-h) ; }
379 MEM_STATIC size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h); } /* only in zstd_opt.h */
381 static const U32 prime4bytes = 2654435761U;
382 static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; }
383 static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_read32(ptr), h); }
385 static const U64 prime5bytes = 889523592379ULL;
386 static size_t ZSTD_hash5(U64 u, U32 h) { return (size_t)(((u << (64-40)) * prime5bytes) >> (64-h)) ; }
387 static size_t ZSTD_hash5Ptr(const void* p, U32 h) { return ZSTD_hash5(MEM_readLE64(p), h); }
389 static const U64 prime6bytes = 227718039650203ULL;
390 static size_t ZSTD_hash6(U64 u, U32 h) { return (size_t)(((u << (64-48)) * prime6bytes) >> (64-h)) ; }
391 static size_t ZSTD_hash6Ptr(const void* p, U32 h) { return ZSTD_hash6(MEM_readLE64(p), h); }
393 static const U64 prime7bytes = 58295818150454627ULL;
394 static size_t ZSTD_hash7(U64 u, U32 h) { return (size_t)(((u << (64-56)) * prime7bytes) >> (64-h)) ; }
395 static size_t ZSTD_hash7Ptr(const void* p, U32 h) { return ZSTD_hash7(MEM_readLE64(p), h); }
397 static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL;
398 static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u) * prime8bytes) >> (64-h)) ; }
399 static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h); }
401 MEM_STATIC size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls)
406 case 4: return ZSTD_hash4Ptr(p, hBits);
407 case 5: return ZSTD_hash5Ptr(p, hBits);
408 case 6: return ZSTD_hash6Ptr(p, hBits);
409 case 7: return ZSTD_hash7Ptr(p, hBits);
410 case 8: return ZSTD_hash8Ptr(p, hBits);
414 #if defined (__cplusplus)
419 /* ==============================================================
420 * Private declarations
421 * These prototypes shall only be called from within lib/compress
422 * ============================================================== */
424 /*! ZSTD_initCStream_internal() :
425 * Private use only. Init streaming operation.
426 * expects params to be valid.
427 * must receive dict, or cdict, or none, but not both.
428 * @return : 0, or an error code */
429 size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs,
430 const void* dict, size_t dictSize,
431 const ZSTD_CDict* cdict,
432 ZSTD_CCtx_params params, unsigned long long pledgedSrcSize);
434 /*! ZSTD_compressStream_generic() :
435 * Private use only. To be called from zstdmt_compress.c in single-thread mode. */
436 size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
437 ZSTD_outBuffer* output,
438 ZSTD_inBuffer* input,
439 ZSTD_EndDirective const flushMode);
441 /*! ZSTD_getCParamsFromCDict() :
442 * as the name implies */
443 ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict);
445 /* ZSTD_compressBegin_advanced_internal() :
446 * Private use only. To be called from zstdmt_compress.c. */
447 size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx,
448 const void* dict, size_t dictSize,
449 ZSTD_dictMode_e dictMode,
450 const ZSTD_CDict* cdict,
451 ZSTD_CCtx_params params,
452 unsigned long long pledgedSrcSize);
454 /* ZSTD_compress_advanced_internal() :
455 * Private use only. To be called from zstdmt_compress.c. */
456 size_t ZSTD_compress_advanced_internal(ZSTD_CCtx* cctx,
457 void* dst, size_t dstCapacity,
458 const void* src, size_t srcSize,
459 const void* dict,size_t dictSize,
460 ZSTD_CCtx_params params);
462 #endif /* ZSTD_COMPRESS_H */