| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 1 | /* |
| Yann Collet | 71bcdb5 | 2015-10-29 17:08:03 +0100 | [diff] [blame] | 2 | zstd_internal - common functions to include |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 3 | Header File for include |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 4 | Copyright (C) 2014-2016, Yann Collet. |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 5 | |
| 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 Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 30 | - zstd homepage : https://www.zstd.net |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 31 | */ |
| 32 | #ifndef ZSTD_CCOMMON_H_MODULE |
| 33 | #define ZSTD_CCOMMON_H_MODULE |
| 34 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 35 | /*-************************************* |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 36 | * Dependencies |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 37 | ***************************************/ |
| 38 | #include "mem.h" |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 39 | #include "error_private.h" |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 40 | #include "zstd_static.h" |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 41 | |
| 42 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 43 | /*-************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 44 | * Common macros |
| 45 | ***************************************/ |
| Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 46 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 47 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 48 | |
| 49 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 50 | /*-************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 51 | * Common constants |
| 52 | ***************************************/ |
| inikep | 2d55563 | 2016-02-29 22:07:40 +0100 | [diff] [blame] | 53 | #define ZSTD_OPT_DEBUG 0 // 1 = tableID=0; 3 = price func tests; 5 = check encoded sequences; 9 = full logs |
| inikep | a4dde25 | 2016-03-01 14:14:35 +0100 | [diff] [blame] | 54 | #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(...) |
| inikep | c3a9a9c | 2016-02-19 11:05:25 +0100 | [diff] [blame] | 65 | #endif |
| 66 | |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 67 | #define ZSTD_OPT_NUM (1<<12) |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 68 | #define ZSTD_DICT_MAGIC 0xEC30A435 |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 69 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 70 | #define KB *(1 <<10) |
| 71 | #define MB *(1 <<20) |
| 72 | #define GB *(1U<<30) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 73 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 74 | #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 Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 81 | #define ZSTD_WINDOWLOG_ABSOLUTEMIN 12 |
| 82 | static 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 .... :( */ |
| 85 | static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE; |
| 86 | typedef 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 Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 93 | #define IS_HUF 0 |
| 94 | #define IS_PCH 1 |
| 95 | #define IS_RAW 2 |
| 96 | #define IS_RLE 3 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 97 | |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 98 | #define LONGNBSEQ 0x7F00 |
| 99 | |
| inikep | 84f43e2 | 2016-02-22 11:34:07 +0100 | [diff] [blame] | 100 | #define MINMATCH 4 |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 101 | #define REPCODE_STARTVALUE 1 |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 102 | #define HASHLOG3 17 |
| Yann Collet | 4db09ef | 2016-03-18 22:23:49 +0100 | [diff] [blame] | 103 | |
| inikep | 3bfcfc7 | 2016-02-03 18:47:30 +0100 | [diff] [blame] | 104 | #define Litbits 8 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 105 | #define Offbits 5 |
| inikep | 70b0545 | 2016-02-03 22:56:55 +0100 | [diff] [blame] | 106 | #define MaxLit ((1<<Litbits) - 1) |
| Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 107 | #define MaxML 52 |
| Yann Collet | b0aec17 | 2016-03-21 13:24:16 +0100 | [diff] [blame] | 108 | #define MaxLL 35 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 109 | #define MaxOff ((1<<Offbits)- 1) |
| Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 110 | #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */ |
| 111 | #define MLFSELog 9 |
| Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 112 | #define LLFSELog 9 |
| Yann Collet | 646693e | 2016-03-24 02:31:27 +0100 | [diff] [blame] | 113 | #define OffFSELog 8 |
| inikep | f3c6503 | 2016-03-04 20:04:25 +0100 | [diff] [blame] | 114 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 115 | #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 Collet | b0aec17 | 2016-03-21 13:24:16 +0100 | [diff] [blame] | 120 | static 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 }; |
| 123 | static 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 }; |
| 126 | static const U32 LL_defaultNormLog = 6; |
| 127 | |
| Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 128 | static 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 }; |
| 132 | static 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, }; |
| 136 | static const U32 ML_defaultNormLog = 6; |
| 137 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 138 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 139 | /*-******************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 140 | * Shared functions to include for inlining |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 141 | *********************************************/ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 142 | static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 143 | #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } |
| 144 | |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 145 | /*! ZSTD_wildcopy() : |
| 146 | * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */ |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 147 | #define WILDCOPY_OVERLENGTH 8 |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 148 | MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 149 | { |
| 150 | const BYTE* ip = (const BYTE*)src; |
| 151 | BYTE* op = (BYTE*)dst; |
| 152 | BYTE* const oend = op + length; |
| Yann Collet | 50c5cdb | 2015-11-04 20:35:33 +0100 | [diff] [blame] | 153 | do |
| 154 | COPY8(op, ip) |
| 155 | while (op < oend); |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 158 | MEM_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 Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 178 | } |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 179 | |
| 180 | |
| 181 | /*-******************************************* |
| 182 | * Private interfaces |
| 183 | *********************************************/ |
| 184 | typedef struct { |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 185 | U32 off; |
| 186 | U32 len; |
| 187 | } ZSTD_match_t; |
| 188 | |
| 189 | typedef struct { |
| 190 | U32 price; |
| 191 | U32 off; |
| 192 | U32 mlen; |
| 193 | U32 litlen; |
| 194 | U32 rep; |
| 195 | U32 rep2; |
| 196 | } ZSTD_optimal_t; |
| 197 | |
| 198 | typedef struct { |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 199 | void* buffer; |
| 200 | U32* offsetStart; |
| 201 | U32* offset; |
| 202 | BYTE* offCodeStart; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 203 | BYTE* litStart; |
| 204 | BYTE* lit; |
| Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 205 | U16* litLengthStart; |
| 206 | U16* litLength; |
| 207 | BYTE* llCodeStart; |
| Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 208 | U16* matchLengthStart; |
| 209 | U16* matchLength; |
| 210 | BYTE* mlCodeStart; |
| Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 211 | U32 longLength; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 212 | /* opt */ |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 213 | ZSTD_optimal_t* priceTable; |
| 214 | ZSTD_match_t* matchTable; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 215 | U32* matchLengthFreq; |
| 216 | U32* litLengthFreq; |
| 217 | U32* litFreq; |
| 218 | U32* offCodeFreq; |
| 219 | U32 matchLengthSum; |
| inikep | e0010e9 | 2016-02-23 16:25:04 +0100 | [diff] [blame] | 220 | U32 matchSum; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 221 | U32 litLengthSum; |
| 222 | U32 litSum; |
| 223 | U32 offCodeSum; |
| inikep | b5a519f | 2016-03-09 15:45:01 +0100 | [diff] [blame] | 224 | U32 log2matchLengthSum; |
| 225 | U32 log2matchSum; |
| 226 | U32 log2litLengthSum; |
| 227 | U32 log2litSum; |
| 228 | U32 log2offCodeSum; |
| 229 | U32 factor; |
| inikep | e29caf7 | 2016-03-04 19:52:23 +0100 | [diff] [blame] | 230 | #if ZSTD_OPT_DEBUG == 3 |
| inikep | 15174b0 | 2016-02-23 12:41:56 +0100 | [diff] [blame] | 231 | U32 realMatchSum; |
| 232 | U32 realLitSum; |
| 233 | U32 realSeqSum; |
| 234 | U32 realRepSum; |
| inikep | e0010e9 | 2016-02-23 16:25:04 +0100 | [diff] [blame] | 235 | U32 priceFunc; |
| inikep | e29caf7 | 2016-03-04 19:52:23 +0100 | [diff] [blame] | 236 | #endif |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 237 | } seqStore_t; |
| 238 | |
| Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame^] | 239 | const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); |
| 240 | void ZSTD_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq); |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 241 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 242 | |
| 243 | #endif /* ZSTD_CCOMMON_H_MODULE */ |