blob: 130eb210da9bf5a3e235e0d321dfa840acaf9cdc [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 :
30 - zstd source repository : https://github.com/Cyan4973/zstd
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 BLOCKSIZE (128 KB) /* define, for static allocation */
Yann Collet2acb5d32015-10-29 16:49:43 +010075
Yann Collet14983e72015-11-11 21:38:21 +010076static const size_t ZSTD_blockHeaderSize = 3;
Yann Collet88fcd292015-11-25 14:42:45 +010077static const size_t ZSTD_frameHeaderSize_min = 5;
78#define ZSTD_frameHeaderSize_max 5 /* define, for static allocation */
Yann Collet14983e72015-11-11 21:38:21 +010079
80#define BIT7 128
81#define BIT6 64
82#define BIT5 32
83#define BIT4 16
84#define BIT1 2
85#define BIT0 1
86
Yann Collet59d1f792016-01-23 19:28:41 +010087#define IS_HUF 0
88#define IS_PCH 1
89#define IS_RAW 2
90#define IS_RLE 3
Yann Collet14983e72015-11-11 21:38:21 +010091
inikep84f43e22016-02-22 11:34:07 +010092#define MINMATCH 4
Yann Collet61e16ce2016-01-31 02:04:15 +010093#define REPCODE_STARTVALUE 1
Yann Collet14983e72015-11-11 21:38:21 +010094
inikep3bfcfc72016-02-03 18:47:30 +010095#define Litbits 8
Yann Collet14983e72015-11-11 21:38:21 +010096#define MLbits 7
97#define LLbits 6
98#define Offbits 5
inikep70b05452016-02-03 22:56:55 +010099#define MaxLit ((1<<Litbits) - 1)
Yann Collet14983e72015-11-11 21:38:21 +0100100#define MaxML ((1<<MLbits) - 1)
101#define MaxLL ((1<<LLbits) - 1)
102#define MaxOff ((1<<Offbits)- 1)
103#define MLFSELog 10
104#define LLFSELog 10
105#define OffFSELog 9
106#define MaxSeq MAX(MaxLL, MaxML)
107
inikep89c9e1a2016-03-06 23:21:52 +0100108#define LONGNBSEQ 0x7F00
inikepf3c65032016-03-04 20:04:25 +0100109
Yann Colletfb810d62016-01-28 00:18:06 +0100110#define FSE_ENCODING_RAW 0
111#define FSE_ENCODING_RLE 1
112#define FSE_ENCODING_STATIC 2
113#define FSE_ENCODING_DYNAMIC 3
114
Yann Colletb923f652016-01-26 03:14:20 +0100115#define HufLog 12
116
Yann Colletb010b3b2016-02-03 12:39:34 +0100117#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
118#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
Yann Colletbc4c8aa2016-01-25 17:26:01 +0100119
120#define WILDCOPY_OVERLENGTH 8
Yann Collet14983e72015-11-11 21:38:21 +0100121
122typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
Yann Collet2acb5d32015-10-29 16:49:43 +0100123
124
Yann Colletfb810d62016-01-28 00:18:06 +0100125/*-*******************************************
Yann Collet14983e72015-11-11 21:38:21 +0100126* Shared functions to include for inlining
Yann Colletfb810d62016-01-28 00:18:06 +0100127*********************************************/
Yann Collet2acb5d32015-10-29 16:49:43 +0100128static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
129
130#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
131
Yann Collet953ce722016-02-04 15:28:14 +0100132/*! ZSTD_wildcopy() :
133* custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
Yann Collet59d1f792016-01-23 19:28:41 +0100134MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
Yann Collet2acb5d32015-10-29 16:49:43 +0100135{
136 const BYTE* ip = (const BYTE*)src;
137 BYTE* op = (BYTE*)dst;
138 BYTE* const oend = op + length;
Yann Collet50c5cdb2015-11-04 20:35:33 +0100139 do
140 COPY8(op, ip)
141 while (op < oend);
Yann Collet2acb5d32015-10-29 16:49:43 +0100142}
143
Yann Collet7d360282016-02-12 00:07:30 +0100144MEM_STATIC unsigned ZSTD_highbit(U32 val)
145{
146# if defined(_MSC_VER) /* Visual */
147 unsigned long r=0;
148 _BitScanReverse(&r, val);
149 return (unsigned)r;
150# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
151 return 31 - __builtin_clz(val);
152# else /* Software version */
153 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 };
154 U32 v = val;
155 int r;
156 v |= v >> 1;
157 v |= v >> 2;
158 v |= v >> 4;
159 v |= v >> 8;
160 v |= v >> 16;
161 r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
162 return r;
163# endif
Yann Collet2acb5d32015-10-29 16:49:43 +0100164}
Yann Collet7d360282016-02-12 00:07:30 +0100165
166
167/*-*******************************************
168* Private interfaces
169*********************************************/
170typedef struct {
inikep87d4f3d2016-03-02 15:56:24 +0100171 U32 off;
172 U32 len;
173} ZSTD_match_t;
174
175typedef struct {
176 U32 price;
177 U32 off;
178 U32 mlen;
179 U32 litlen;
180 U32 rep;
181 U32 rep2;
182} ZSTD_optimal_t;
183
184typedef struct {
Yann Collet7d360282016-02-12 00:07:30 +0100185 void* buffer;
186 U32* offsetStart;
187 U32* offset;
188 BYTE* offCodeStart;
189 BYTE* offCode;
190 BYTE* litStart;
191 BYTE* lit;
192 BYTE* litLengthStart;
193 BYTE* litLength;
194 BYTE* matchLengthStart;
195 BYTE* matchLength;
196 BYTE* dumpsStart;
197 BYTE* dumps;
198 /* opt */
inikep87d4f3d2016-03-02 15:56:24 +0100199 ZSTD_optimal_t* priceTable;
200 ZSTD_match_t* matchTable;
Yann Collet7d360282016-02-12 00:07:30 +0100201 U32* matchLengthFreq;
202 U32* litLengthFreq;
203 U32* litFreq;
204 U32* offCodeFreq;
205 U32 matchLengthSum;
inikepe0010e92016-02-23 16:25:04 +0100206 U32 matchSum;
Yann Collet7d360282016-02-12 00:07:30 +0100207 U32 litLengthSum;
208 U32 litSum;
209 U32 offCodeSum;
inikepb5a519f2016-03-09 15:45:01 +0100210 U32 log2matchLengthSum;
211 U32 log2matchSum;
212 U32 log2litLengthSum;
213 U32 log2litSum;
214 U32 log2offCodeSum;
215 U32 factor;
inikepe29caf72016-03-04 19:52:23 +0100216#if ZSTD_OPT_DEBUG == 3
inikep15174b02016-02-23 12:41:56 +0100217 U32 realMatchSum;
218 U32 realLitSum;
219 U32 realSeqSum;
220 U32 realRepSum;
inikepe0010e92016-02-23 16:25:04 +0100221 U32 priceFunc;
inikepe29caf72016-03-04 19:52:23 +0100222#endif
Yann Collet7d360282016-02-12 00:07:30 +0100223} seqStore_t;
224
225seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx);
226
Yann Collet2acb5d32015-10-29 16:49:43 +0100227
228#endif /* ZSTD_CCOMMON_H_MODULE */