blob: ff2713408fa9dc3ec86410df4e623031bb32e985 [file] [log] [blame]
Yann Collet2acb5d32015-10-29 16:49:43 +01001/*
Yann Collet71bcdb52015-10-29 17:08:03 +01002 zstd_internal - common functions to include
Yann Collet2acb5d32015-10-29 16:49:43 +01003 Header File for include
Yann Collet953ce722016-02-04 15:28:14 +01004 Copyright (C) 2014-2016, Yann Collet.
Yann Collet2acb5d32015-10-29 16:49:43 +01005
6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following disclaimer
15 in the documentation and/or other materials provided with the
16 distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 You can contact the author at :
Yann Collet569b81a2016-03-16 15:26:51 +010030 - zstd homepage : https://www.zstd.net
Yann Collet2acb5d32015-10-29 16:49:43 +010031*/
32#ifndef ZSTD_CCOMMON_H_MODULE
33#define ZSTD_CCOMMON_H_MODULE
34
Yann Collet7d360282016-02-12 00:07:30 +010035/*-*************************************
Yann Collet953ce722016-02-04 15:28:14 +010036* Dependencies
Yann Collet2acb5d32015-10-29 16:49:43 +010037***************************************/
38#include "mem.h"
Yann Collet977f1f32016-01-21 15:38:47 +010039#include "error_private.h"
Yann Collet59d1f792016-01-23 19:28:41 +010040#include "zstd_static.h"
Yann Collet2acb5d32015-10-29 16:49:43 +010041
42
Yann Collet7d360282016-02-12 00:07:30 +010043/*-*************************************
Yann Collet14983e72015-11-11 21:38:21 +010044* Common macros
45***************************************/
Yann Colletbe2010e2015-10-31 12:57:14 +010046#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet14983e72015-11-11 21:38:21 +010047#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet2acb5d32015-10-29 16:49:43 +010048
49
Yann Collet7d360282016-02-12 00:07:30 +010050/*-*************************************
Yann Collet14983e72015-11-11 21:38:21 +010051* Common constants
52***************************************/
inikep2d555632016-02-29 22:07:40 +010053#define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 3 = price func tests; 5 = check encoded sequences; 9 = full logs
inikepa4dde252016-03-01 14:14:35 +010054#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>0
55 #include <stdio.h>
56#endif
57#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9
58 #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__)
59 #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__)
60 #define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__)
61#else
62 #define ZSTD_LOG_PARSER(...)
63 #define ZSTD_LOG_ENCODE(...)
64 #define ZSTD_LOG_BLOCK(...)
inikepc3a9a9c2016-02-19 11:05:25 +010065#endif
66
inikep87d4f3d2016-03-02 15:56:24 +010067#define ZSTD_OPT_NUM (1<<12)
Yann Colletb923f652016-01-26 03:14:20 +010068#define ZSTD_DICT_MAGIC 0xEC30A435
Yann Collet88fcd292015-11-25 14:42:45 +010069
Yann Collet14983e72015-11-11 21:38:21 +010070#define KB *(1 <<10)
71#define MB *(1 <<20)
72#define GB *(1U<<30)
Yann Collet2acb5d32015-10-29 16:49:43 +010073
Yann Collet14983e72015-11-11 21:38:21 +010074#define BIT7 128
75#define BIT6 64
76#define BIT5 32
77#define BIT4 16
78#define BIT1 2
79#define BIT0 1
80
Yann Collet37f3d1b2016-03-19 15:11:42 +010081#define ZSTD_WINDOWLOG_ABSOLUTEMIN 12
82static const size_t ZSTD_fcs_fieldSize[4] = { 0, 1, 2, 8 };
83
84#define ZSTD_BLOCKHEADERSIZE 3 /* because C standard does not allow a static const value to be defined using another static const value .... :( */
85static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
86typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
87
88#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
89#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
90
91#define HufLog 12
92
Yann Collet59d1f792016-01-23 19:28:41 +010093#define IS_HUF 0
94#define IS_PCH 1
95#define IS_RAW 2
96#define IS_RLE 3
Yann Collet14983e72015-11-11 21:38:21 +010097
Yann Collet37f3d1b2016-03-19 15:11:42 +010098#define LONGNBSEQ 0x7F00
99
inikep84f43e22016-02-22 11:34:07 +0100100#define MINMATCH 4
Yann Collet61e16ce2016-01-31 02:04:15 +0100101#define REPCODE_STARTVALUE 1
Yann Collet37f3d1b2016-03-19 15:11:42 +0100102#define HASHLOG3 17
Yann Collet4db09ef2016-03-18 22:23:49 +0100103
inikep3bfcfc72016-02-03 18:47:30 +0100104#define Litbits 8
Yann Collet14983e72015-11-11 21:38:21 +0100105#define Offbits 5
inikep70b05452016-02-03 22:56:55 +0100106#define MaxLit ((1<<Litbits) - 1)
Yann Colletfadda6c2016-03-22 12:14:26 +0100107#define MaxML 52
Yann Colletb0aec172016-03-21 13:24:16 +0100108#define MaxLL 35
Yann Collet14983e72015-11-11 21:38:21 +0100109#define MaxOff ((1<<Offbits)- 1)
Yann Colletbe391432016-03-22 23:19:28 +0100110#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
111#define MLFSELog 9
Yann Colletd64f4352016-03-21 00:07:42 +0100112#define LLFSELog 9
Yann Collet646693e2016-03-24 02:31:27 +0100113#define OffFSELog 8
inikepf3c65032016-03-04 20:04:25 +0100114
Yann Colletfb810d62016-01-28 00:18:06 +0100115#define FSE_ENCODING_RAW 0
116#define FSE_ENCODING_RLE 1
117#define FSE_ENCODING_STATIC 2
118#define FSE_ENCODING_DYNAMIC 3
119
Yann Colletb0aec172016-03-21 13:24:16 +0100120static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
122 13,14,15,16 };
123static const S16 LL_defaultNorm[MaxLL+1] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
124 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1,
125 1, 1, 1, 1 };
126static const U32 LL_defaultNormLog = 6;
127
Yann Colletfadda6c2016-03-22 12:14:26 +0100128static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
131 12,13,14,15,16 };
132static const S16 ML_defaultNorm[MaxML+1] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
133 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
134 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
135 1, 1, 1, 1, 1, };
136static const U32 ML_defaultNormLog = 6;
137
Yann Collet2acb5d32015-10-29 16:49:43 +0100138
Yann Colletfb810d62016-01-28 00:18:06 +0100139/*-*******************************************
Yann Collet14983e72015-11-11 21:38:21 +0100140* Shared functions to include for inlining
Yann Colletfb810d62016-01-28 00:18:06 +0100141*********************************************/
Yann Collet2acb5d32015-10-29 16:49:43 +0100142static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
Yann Collet2acb5d32015-10-29 16:49:43 +0100143#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
144
Yann Collet953ce722016-02-04 15:28:14 +0100145/*! ZSTD_wildcopy() :
146* custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
Yann Collet37f3d1b2016-03-19 15:11:42 +0100147#define WILDCOPY_OVERLENGTH 8
Yann Collet59d1f792016-01-23 19:28:41 +0100148MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
Yann Collet2acb5d32015-10-29 16:49:43 +0100149{
150 const BYTE* ip = (const BYTE*)src;
151 BYTE* op = (BYTE*)dst;
152 BYTE* const oend = op + length;
Yann Collet50c5cdb2015-11-04 20:35:33 +0100153 do
154 COPY8(op, ip)
155 while (op < oend);
Yann Collet2acb5d32015-10-29 16:49:43 +0100156}
157
Yann Collet7d360282016-02-12 00:07:30 +0100158MEM_STATIC unsigned ZSTD_highbit(U32 val)
159{
160# if defined(_MSC_VER) /* Visual */
161 unsigned long r=0;
162 _BitScanReverse(&r, val);
163 return (unsigned)r;
164# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
165 return 31 - __builtin_clz(val);
166# else /* Software version */
167 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 };
168 U32 v = val;
169 int r;
170 v |= v >> 1;
171 v |= v >> 2;
172 v |= v >> 4;
173 v |= v >> 8;
174 v |= v >> 16;
175 r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
176 return r;
177# endif
Yann Collet2acb5d32015-10-29 16:49:43 +0100178}
Yann Collet7d360282016-02-12 00:07:30 +0100179
180
181/*-*******************************************
182* Private interfaces
183*********************************************/
184typedef struct {
inikep87d4f3d2016-03-02 15:56:24 +0100185 U32 off;
186 U32 len;
187} ZSTD_match_t;
188
189typedef struct {
190 U32 price;
191 U32 off;
192 U32 mlen;
193 U32 litlen;
194 U32 rep;
195 U32 rep2;
196} ZSTD_optimal_t;
197
198typedef struct {
Yann Collet7d360282016-02-12 00:07:30 +0100199 void* buffer;
200 U32* offsetStart;
201 U32* offset;
202 BYTE* offCodeStart;
Yann Collet7d360282016-02-12 00:07:30 +0100203 BYTE* litStart;
204 BYTE* lit;
Yann Collet597847a2016-03-20 19:14:22 +0100205 U16* litLengthStart;
206 U16* litLength;
207 BYTE* llCodeStart;
Yann Colletfadda6c2016-03-22 12:14:26 +0100208 U16* matchLengthStart;
209 U16* matchLength;
210 BYTE* mlCodeStart;
Yann Colletfadda6c2016-03-22 12:14:26 +0100211 U32 longLength;
Yann Collet7d360282016-02-12 00:07:30 +0100212 /* opt */
inikep87d4f3d2016-03-02 15:56:24 +0100213 ZSTD_optimal_t* priceTable;
214 ZSTD_match_t* matchTable;
Yann Collet7d360282016-02-12 00:07:30 +0100215 U32* matchLengthFreq;
216 U32* litLengthFreq;
217 U32* litFreq;
218 U32* offCodeFreq;
219 U32 matchLengthSum;
inikepe0010e92016-02-23 16:25:04 +0100220 U32 matchSum;
Yann Collet7d360282016-02-12 00:07:30 +0100221 U32 litLengthSum;
222 U32 litSum;
223 U32 offCodeSum;
inikepb5a519f2016-03-09 15:45:01 +0100224 U32 log2matchLengthSum;
225 U32 log2matchSum;
226 U32 log2litLengthSum;
227 U32 log2litSum;
228 U32 log2offCodeSum;
229 U32 factor;
inikepe29caf72016-03-04 19:52:23 +0100230#if ZSTD_OPT_DEBUG == 3
inikep15174b02016-02-23 12:41:56 +0100231 U32 realMatchSum;
232 U32 realLitSum;
233 U32 realSeqSum;
234 U32 realRepSum;
inikepe0010e92016-02-23 16:25:04 +0100235 U32 priceFunc;
inikepe29caf72016-03-04 19:52:23 +0100236#endif
Yann Collet7d360282016-02-12 00:07:30 +0100237} seqStore_t;
238
Yann Colletb44be742016-03-26 20:52:14 +0100239const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
240void ZSTD_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq);
Yann Collet7d360282016-02-12 00:07:30 +0100241
Yann Collet2acb5d32015-10-29 16:49:43 +0100242
243#endif /* ZSTD_CCOMMON_H_MODULE */