| 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 : |
| 30 | - zstd source repository : https://github.com/Cyan4973/zstd |
| 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 BLOCKSIZE (128 KB) /* define, for static allocation */ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 75 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 76 | static const size_t ZSTD_blockHeaderSize = 3; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 77 | static const size_t ZSTD_frameHeaderSize_min = 5; |
| 78 | #define ZSTD_frameHeaderSize_max 5 /* define, for static allocation */ |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 79 | |
| 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 Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 87 | #define IS_HUF 0 |
| 88 | #define IS_PCH 1 |
| 89 | #define IS_RAW 2 |
| 90 | #define IS_RLE 3 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 91 | |
| inikep | 84f43e2 | 2016-02-22 11:34:07 +0100 | [diff] [blame] | 92 | #define MINMATCH 4 |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 93 | #define REPCODE_STARTVALUE 1 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 94 | |
| inikep | 3bfcfc7 | 2016-02-03 18:47:30 +0100 | [diff] [blame] | 95 | #define Litbits 8 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 96 | #define MLbits 7 |
| 97 | #define LLbits 6 |
| 98 | #define Offbits 5 |
| inikep | 70b0545 | 2016-02-03 22:56:55 +0100 | [diff] [blame] | 99 | #define MaxLit ((1<<Litbits) - 1) |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 100 | #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 | |
| inikep | 89c9e1a | 2016-03-06 23:21:52 +0100 | [diff] [blame] | 108 | #define LONGNBSEQ 0x7F00 |
| inikep | f3c6503 | 2016-03-04 20:04:25 +0100 | [diff] [blame] | 109 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 110 | #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 Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 115 | #define HufLog 12 |
| 116 | |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 117 | #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 Collet | bc4c8aa | 2016-01-25 17:26:01 +0100 | [diff] [blame] | 119 | |
| 120 | #define WILDCOPY_OVERLENGTH 8 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 121 | |
| 122 | typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t; |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 123 | |
| 124 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 125 | /*-******************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 126 | * Shared functions to include for inlining |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 127 | *********************************************/ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 128 | static 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 Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 132 | /*! ZSTD_wildcopy() : |
| 133 | * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */ |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 134 | 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] | 135 | { |
| 136 | const BYTE* ip = (const BYTE*)src; |
| 137 | BYTE* op = (BYTE*)dst; |
| 138 | BYTE* const oend = op + length; |
| Yann Collet | 50c5cdb | 2015-11-04 20:35:33 +0100 | [diff] [blame] | 139 | do |
| 140 | COPY8(op, ip) |
| 141 | while (op < oend); |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 144 | MEM_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 Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 164 | } |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 165 | |
| 166 | |
| 167 | /*-******************************************* |
| 168 | * Private interfaces |
| 169 | *********************************************/ |
| 170 | typedef struct { |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 171 | U32 off; |
| 172 | U32 len; |
| 173 | } ZSTD_match_t; |
| 174 | |
| 175 | typedef struct { |
| 176 | U32 price; |
| 177 | U32 off; |
| 178 | U32 mlen; |
| 179 | U32 litlen; |
| 180 | U32 rep; |
| 181 | U32 rep2; |
| 182 | } ZSTD_optimal_t; |
| 183 | |
| 184 | typedef struct { |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 185 | 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 */ |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 199 | ZSTD_optimal_t* priceTable; |
| 200 | ZSTD_match_t* matchTable; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 201 | U32* matchLengthFreq; |
| 202 | U32* litLengthFreq; |
| 203 | U32* litFreq; |
| 204 | U32* offCodeFreq; |
| 205 | U32 matchLengthSum; |
| inikep | e0010e9 | 2016-02-23 16:25:04 +0100 | [diff] [blame] | 206 | U32 matchSum; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 207 | U32 litLengthSum; |
| 208 | U32 litSum; |
| 209 | U32 offCodeSum; |
| inikep | b5a519f | 2016-03-09 15:45:01 +0100 | [diff] [blame^] | 210 | U32 log2matchLengthSum; |
| 211 | U32 log2matchSum; |
| 212 | U32 log2litLengthSum; |
| 213 | U32 log2litSum; |
| 214 | U32 log2offCodeSum; |
| 215 | U32 factor; |
| inikep | e29caf7 | 2016-03-04 19:52:23 +0100 | [diff] [blame] | 216 | #if ZSTD_OPT_DEBUG == 3 |
| inikep | 15174b0 | 2016-02-23 12:41:56 +0100 | [diff] [blame] | 217 | U32 realMatchSum; |
| 218 | U32 realLitSum; |
| 219 | U32 realSeqSum; |
| 220 | U32 realRepSum; |
| inikep | e0010e9 | 2016-02-23 16:25:04 +0100 | [diff] [blame] | 221 | U32 priceFunc; |
| inikep | e29caf7 | 2016-03-04 19:52:23 +0100 | [diff] [blame] | 222 | #endif |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 223 | } seqStore_t; |
| 224 | |
| 225 | seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx); |
| 226 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 227 | |
| 228 | #endif /* ZSTD_CCOMMON_H_MODULE */ |