Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include "zstd_compress_internal.h" |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 12 | #include "hist.h" |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 13 | #include "zstd_opt.h" |
| 14 | |
| 15 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 16 | #define ZSTD_LITFREQ_ADD 2 /* scaling factor for litFreq, so that frequencies adapt faster to new stats */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 17 | #define ZSTD_FREQ_DIV 4 /* log factor when using previous stats to init next stats */ |
| 18 | #define ZSTD_MAX_PRICE (1<<30) |
| 19 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 20 | #define ZSTD_PREDEF_THRESHOLD 1024 /* if srcSize < ZSTD_PREDEF_THRESHOLD, symbols' cost is assumed static, directly determined by pre-defined distributions */ |
| 21 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 22 | |
| 23 | /*-************************************* |
| 24 | * Price functions for optimal parser |
| 25 | ***************************************/ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 26 | |
| 27 | #if 0 /* approximation at bit level */ |
| 28 | # define BITCOST_ACCURACY 0 |
| 29 | # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY) |
| 30 | # define WEIGHT(stat) ((void)opt, ZSTD_bitWeight(stat)) |
| 31 | #elif 0 /* fractional bit accuracy */ |
| 32 | # define BITCOST_ACCURACY 8 |
| 33 | # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY) |
| 34 | # define WEIGHT(stat,opt) ((void)opt, ZSTD_fracWeight(stat)) |
| 35 | #else /* opt==approx, ultra==accurate */ |
| 36 | # define BITCOST_ACCURACY 8 |
| 37 | # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY) |
| 38 | # define WEIGHT(stat,opt) (opt ? ZSTD_fracWeight(stat) : ZSTD_bitWeight(stat)) |
| 39 | #endif |
| 40 | |
| 41 | MEM_STATIC U32 ZSTD_bitWeight(U32 stat) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 42 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 43 | return (ZSTD_highbit32(stat+1) * BITCOST_MULTIPLIER); |
| 44 | } |
| 45 | |
| 46 | MEM_STATIC U32 ZSTD_fracWeight(U32 rawStat) |
| 47 | { |
| 48 | U32 const stat = rawStat + 1; |
| 49 | U32 const hb = ZSTD_highbit32(stat); |
| 50 | U32 const BWeight = hb * BITCOST_MULTIPLIER; |
| 51 | U32 const FWeight = (stat << BITCOST_ACCURACY) >> hb; |
| 52 | U32 const weight = BWeight + FWeight; |
| 53 | assert(hb + BITCOST_ACCURACY < 31); |
| 54 | return weight; |
| 55 | } |
| 56 | |
| 57 | #if (DEBUGLEVEL>=2) |
| 58 | /* debugging function, |
| 59 | * @return price in bytes as fractional value |
| 60 | * for debug messages only */ |
| 61 | MEM_STATIC double ZSTD_fCost(U32 price) |
| 62 | { |
| 63 | return (double)price / (BITCOST_MULTIPLIER*8); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | static int ZSTD_compressedLiterals(optState_t const* const optPtr) |
| 68 | { |
| 69 | return optPtr->literalCompressionMode != ZSTD_lcm_uncompressed; |
| 70 | } |
| 71 | |
| 72 | static void ZSTD_setBasePrices(optState_t* optPtr, int optLevel) |
| 73 | { |
| 74 | if (ZSTD_compressedLiterals(optPtr)) |
| 75 | optPtr->litSumBasePrice = WEIGHT(optPtr->litSum, optLevel); |
| 76 | optPtr->litLengthSumBasePrice = WEIGHT(optPtr->litLengthSum, optLevel); |
| 77 | optPtr->matchLengthSumBasePrice = WEIGHT(optPtr->matchLengthSum, optLevel); |
| 78 | optPtr->offCodeSumBasePrice = WEIGHT(optPtr->offCodeSum, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 82 | /* ZSTD_downscaleStat() : |
| 83 | * reduce all elements in table by a factor 2^(ZSTD_FREQ_DIV+malus) |
| 84 | * return the resulting sum of elements */ |
| 85 | static U32 ZSTD_downscaleStat(unsigned* table, U32 lastEltIndex, int malus) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 86 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 87 | U32 s, sum=0; |
| 88 | DEBUGLOG(5, "ZSTD_downscaleStat (nbElts=%u)", (unsigned)lastEltIndex+1); |
| 89 | assert(ZSTD_FREQ_DIV+malus > 0 && ZSTD_FREQ_DIV+malus < 31); |
| 90 | for (s=0; s<lastEltIndex+1; s++) { |
| 91 | table[s] = 1 + (table[s] >> (ZSTD_FREQ_DIV+malus)); |
| 92 | sum += table[s]; |
| 93 | } |
| 94 | return sum; |
| 95 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 96 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 97 | /* ZSTD_rescaleFreqs() : |
| 98 | * if first block (detected by optPtr->litLengthSum == 0) : init statistics |
| 99 | * take hints from dictionary if there is one |
| 100 | * or init from zero, using src for literals stats, or flat 1 for match symbols |
| 101 | * otherwise downscale existing stats, to be used as seed for next block. |
| 102 | */ |
| 103 | static void |
| 104 | ZSTD_rescaleFreqs(optState_t* const optPtr, |
| 105 | const BYTE* const src, size_t const srcSize, |
| 106 | int const optLevel) |
| 107 | { |
| 108 | int const compressedLiterals = ZSTD_compressedLiterals(optPtr); |
| 109 | DEBUGLOG(5, "ZSTD_rescaleFreqs (srcSize=%u)", (unsigned)srcSize); |
| 110 | optPtr->priceType = zop_dynamic; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 111 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 112 | if (optPtr->litLengthSum == 0) { /* first block : init */ |
| 113 | if (srcSize <= ZSTD_PREDEF_THRESHOLD) { /* heuristic */ |
| 114 | DEBUGLOG(5, "(srcSize <= ZSTD_PREDEF_THRESHOLD) => zop_predef"); |
| 115 | optPtr->priceType = zop_predef; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 118 | assert(optPtr->symbolCosts != NULL); |
| 119 | if (optPtr->symbolCosts->huf.repeatMode == HUF_repeat_valid) { |
| 120 | /* huffman table presumed generated by dictionary */ |
| 121 | optPtr->priceType = zop_dynamic; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 122 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 123 | if (compressedLiterals) { |
| 124 | unsigned lit; |
| 125 | assert(optPtr->litFreq != NULL); |
| 126 | optPtr->litSum = 0; |
| 127 | for (lit=0; lit<=MaxLit; lit++) { |
| 128 | U32 const scaleLog = 11; /* scale to 2K */ |
| 129 | U32 const bitCost = HUF_getNbBits(optPtr->symbolCosts->huf.CTable, lit); |
| 130 | assert(bitCost <= scaleLog); |
| 131 | optPtr->litFreq[lit] = bitCost ? 1 << (scaleLog-bitCost) : 1 /*minimum to calculate cost*/; |
| 132 | optPtr->litSum += optPtr->litFreq[lit]; |
| 133 | } } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 134 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 135 | { unsigned ll; |
| 136 | FSE_CState_t llstate; |
| 137 | FSE_initCState(&llstate, optPtr->symbolCosts->fse.litlengthCTable); |
| 138 | optPtr->litLengthSum = 0; |
| 139 | for (ll=0; ll<=MaxLL; ll++) { |
| 140 | U32 const scaleLog = 10; /* scale to 1K */ |
| 141 | U32 const bitCost = FSE_getMaxNbBits(llstate.symbolTT, ll); |
| 142 | assert(bitCost < scaleLog); |
| 143 | optPtr->litLengthFreq[ll] = bitCost ? 1 << (scaleLog-bitCost) : 1 /*minimum to calculate cost*/; |
| 144 | optPtr->litLengthSum += optPtr->litLengthFreq[ll]; |
| 145 | } } |
| 146 | |
| 147 | { unsigned ml; |
| 148 | FSE_CState_t mlstate; |
| 149 | FSE_initCState(&mlstate, optPtr->symbolCosts->fse.matchlengthCTable); |
| 150 | optPtr->matchLengthSum = 0; |
| 151 | for (ml=0; ml<=MaxML; ml++) { |
| 152 | U32 const scaleLog = 10; |
| 153 | U32 const bitCost = FSE_getMaxNbBits(mlstate.symbolTT, ml); |
| 154 | assert(bitCost < scaleLog); |
| 155 | optPtr->matchLengthFreq[ml] = bitCost ? 1 << (scaleLog-bitCost) : 1 /*minimum to calculate cost*/; |
| 156 | optPtr->matchLengthSum += optPtr->matchLengthFreq[ml]; |
| 157 | } } |
| 158 | |
| 159 | { unsigned of; |
| 160 | FSE_CState_t ofstate; |
| 161 | FSE_initCState(&ofstate, optPtr->symbolCosts->fse.offcodeCTable); |
| 162 | optPtr->offCodeSum = 0; |
| 163 | for (of=0; of<=MaxOff; of++) { |
| 164 | U32 const scaleLog = 10; |
| 165 | U32 const bitCost = FSE_getMaxNbBits(ofstate.symbolTT, of); |
| 166 | assert(bitCost < scaleLog); |
| 167 | optPtr->offCodeFreq[of] = bitCost ? 1 << (scaleLog-bitCost) : 1 /*minimum to calculate cost*/; |
| 168 | optPtr->offCodeSum += optPtr->offCodeFreq[of]; |
| 169 | } } |
| 170 | |
| 171 | } else { /* not a dictionary */ |
| 172 | |
| 173 | assert(optPtr->litFreq != NULL); |
| 174 | if (compressedLiterals) { |
| 175 | unsigned lit = MaxLit; |
| 176 | HIST_count_simple(optPtr->litFreq, &lit, src, srcSize); /* use raw first block to init statistics */ |
| 177 | optPtr->litSum = ZSTD_downscaleStat(optPtr->litFreq, MaxLit, 1); |
| 178 | } |
| 179 | |
| 180 | { unsigned ll; |
| 181 | for (ll=0; ll<=MaxLL; ll++) |
| 182 | optPtr->litLengthFreq[ll] = 1; |
| 183 | } |
| 184 | optPtr->litLengthSum = MaxLL+1; |
| 185 | |
| 186 | { unsigned ml; |
| 187 | for (ml=0; ml<=MaxML; ml++) |
| 188 | optPtr->matchLengthFreq[ml] = 1; |
| 189 | } |
| 190 | optPtr->matchLengthSum = MaxML+1; |
| 191 | |
| 192 | { unsigned of; |
| 193 | for (of=0; of<=MaxOff; of++) |
| 194 | optPtr->offCodeFreq[of] = 1; |
| 195 | } |
| 196 | optPtr->offCodeSum = MaxOff+1; |
| 197 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 198 | } |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 199 | |
| 200 | } else { /* new block : re-use previous statistics, scaled down */ |
| 201 | |
| 202 | if (compressedLiterals) |
| 203 | optPtr->litSum = ZSTD_downscaleStat(optPtr->litFreq, MaxLit, 1); |
| 204 | optPtr->litLengthSum = ZSTD_downscaleStat(optPtr->litLengthFreq, MaxLL, 0); |
| 205 | optPtr->matchLengthSum = ZSTD_downscaleStat(optPtr->matchLengthFreq, MaxML, 0); |
| 206 | optPtr->offCodeSum = ZSTD_downscaleStat(optPtr->offCodeFreq, MaxOff, 0); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 209 | ZSTD_setBasePrices(optPtr, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 212 | /* ZSTD_rawLiteralsCost() : |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 213 | * price of literals (only) in specified segment (which length can be 0). |
| 214 | * does not include price of literalLength symbol */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 215 | static U32 ZSTD_rawLiteralsCost(const BYTE* const literals, U32 const litLength, |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 216 | const optState_t* const optPtr, |
| 217 | int optLevel) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 218 | { |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 219 | if (litLength == 0) return 0; |
| 220 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 221 | if (!ZSTD_compressedLiterals(optPtr)) |
| 222 | return (litLength << 3) * BITCOST_MULTIPLIER; /* Uncompressed - 8 bytes per literal. */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 223 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 224 | if (optPtr->priceType == zop_predef) |
| 225 | return (litLength*6) * BITCOST_MULTIPLIER; /* 6 bit per literal - no statistic used */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 226 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 227 | /* dynamic statistics */ |
| 228 | { U32 price = litLength * optPtr->litSumBasePrice; |
| 229 | U32 u; |
| 230 | for (u=0; u < litLength; u++) { |
| 231 | assert(WEIGHT(optPtr->litFreq[literals[u]], optLevel) <= optPtr->litSumBasePrice); /* literal cost should never be negative */ |
| 232 | price -= WEIGHT(optPtr->litFreq[literals[u]], optLevel); |
| 233 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 234 | return price; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* ZSTD_litLengthPrice() : |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 239 | * cost of literalLength symbol */ |
| 240 | static U32 ZSTD_litLengthPrice(U32 const litLength, const optState_t* const optPtr, int optLevel) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 241 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 242 | if (optPtr->priceType == zop_predef) return WEIGHT(litLength, optLevel); |
| 243 | |
| 244 | /* dynamic statistics */ |
| 245 | { U32 const llCode = ZSTD_LLcode(litLength); |
| 246 | return (LL_bits[llCode] * BITCOST_MULTIPLIER) |
| 247 | + optPtr->litLengthSumBasePrice |
| 248 | - WEIGHT(optPtr->litLengthFreq[llCode], optLevel); |
| 249 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* ZSTD_litLengthContribution() : |
| 253 | * @return ( cost(litlength) - cost(0) ) |
| 254 | * this value can then be added to rawLiteralsCost() |
| 255 | * to provide a cost which is directly comparable to a match ending at same position */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 256 | static int ZSTD_litLengthContribution(U32 const litLength, const optState_t* const optPtr, int optLevel) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 257 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 258 | if (optPtr->priceType >= zop_predef) return (int)WEIGHT(litLength, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 259 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 260 | /* dynamic statistics */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 261 | { U32 const llCode = ZSTD_LLcode(litLength); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 262 | int const contribution = (int)(LL_bits[llCode] * BITCOST_MULTIPLIER) |
| 263 | + (int)WEIGHT(optPtr->litLengthFreq[0], optLevel) /* note: log2litLengthSum cancel out */ |
| 264 | - (int)WEIGHT(optPtr->litLengthFreq[llCode], optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 265 | #if 1 |
| 266 | return contribution; |
| 267 | #else |
| 268 | return MAX(0, contribution); /* sometimes better, sometimes not ... */ |
| 269 | #endif |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* ZSTD_literalsContribution() : |
| 274 | * creates a fake cost for the literals part of a sequence |
| 275 | * which can be compared to the ending cost of a match |
| 276 | * should a new match start at this position */ |
| 277 | static int ZSTD_literalsContribution(const BYTE* const literals, U32 const litLength, |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 278 | const optState_t* const optPtr, |
| 279 | int optLevel) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 280 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 281 | int const contribution = (int)ZSTD_rawLiteralsCost(literals, litLength, optPtr, optLevel) |
| 282 | + ZSTD_litLengthContribution(litLength, optPtr, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 283 | return contribution; |
| 284 | } |
| 285 | |
| 286 | /* ZSTD_getMatchPrice() : |
| 287 | * Provides the cost of the match part (offset + matchLength) of a sequence |
| 288 | * Must be combined with ZSTD_fullLiteralsCost() to get the full cost of a sequence. |
| 289 | * optLevel: when <2, favors small offset for decompression speed (improved cache efficiency) */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 290 | FORCE_INLINE_TEMPLATE U32 |
| 291 | ZSTD_getMatchPrice(U32 const offset, |
| 292 | U32 const matchLength, |
| 293 | const optState_t* const optPtr, |
| 294 | int const optLevel) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 295 | { |
| 296 | U32 price; |
| 297 | U32 const offCode = ZSTD_highbit32(offset+1); |
| 298 | U32 const mlBase = matchLength - MINMATCH; |
| 299 | assert(matchLength >= MINMATCH); |
| 300 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 301 | if (optPtr->priceType == zop_predef) /* fixed scheme, do not use statistics */ |
| 302 | return WEIGHT(mlBase, optLevel) + ((16 + offCode) * BITCOST_MULTIPLIER); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 303 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 304 | /* dynamic statistics */ |
| 305 | price = (offCode * BITCOST_MULTIPLIER) + (optPtr->offCodeSumBasePrice - WEIGHT(optPtr->offCodeFreq[offCode], optLevel)); |
| 306 | if ((optLevel<2) /*static*/ && offCode >= 20) |
| 307 | price += (offCode-19)*2 * BITCOST_MULTIPLIER; /* handicap for long distance offsets, favor decompression speed */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 308 | |
| 309 | /* match Length */ |
| 310 | { U32 const mlCode = ZSTD_MLcode(mlBase); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 311 | price += (ML_bits[mlCode] * BITCOST_MULTIPLIER) + (optPtr->matchLengthSumBasePrice - WEIGHT(optPtr->matchLengthFreq[mlCode], optLevel)); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 314 | price += BITCOST_MULTIPLIER / 5; /* heuristic : make matches a bit more costly to favor less sequences -> faster decompression speed */ |
| 315 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 316 | DEBUGLOG(8, "ZSTD_getMatchPrice(ml:%u) = %u", matchLength, price); |
| 317 | return price; |
| 318 | } |
| 319 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 320 | /* ZSTD_updateStats() : |
| 321 | * assumption : literals + litLengtn <= iend */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 322 | static void ZSTD_updateStats(optState_t* const optPtr, |
| 323 | U32 litLength, const BYTE* literals, |
| 324 | U32 offsetCode, U32 matchLength) |
| 325 | { |
| 326 | /* literals */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 327 | if (ZSTD_compressedLiterals(optPtr)) { |
| 328 | U32 u; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 329 | for (u=0; u < litLength; u++) |
| 330 | optPtr->litFreq[literals[u]] += ZSTD_LITFREQ_ADD; |
| 331 | optPtr->litSum += litLength*ZSTD_LITFREQ_ADD; |
| 332 | } |
| 333 | |
| 334 | /* literal Length */ |
| 335 | { U32 const llCode = ZSTD_LLcode(litLength); |
| 336 | optPtr->litLengthFreq[llCode]++; |
| 337 | optPtr->litLengthSum++; |
| 338 | } |
| 339 | |
| 340 | /* match offset code (0-2=>repCode; 3+=>offset+2) */ |
| 341 | { U32 const offCode = ZSTD_highbit32(offsetCode+1); |
| 342 | assert(offCode <= MaxOff); |
| 343 | optPtr->offCodeFreq[offCode]++; |
| 344 | optPtr->offCodeSum++; |
| 345 | } |
| 346 | |
| 347 | /* match Length */ |
| 348 | { U32 const mlBase = matchLength - MINMATCH; |
| 349 | U32 const mlCode = ZSTD_MLcode(mlBase); |
| 350 | optPtr->matchLengthFreq[mlCode]++; |
| 351 | optPtr->matchLengthSum++; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /* ZSTD_readMINMATCH() : |
| 357 | * function safe only for comparisons |
| 358 | * assumption : memPtr must be at least 4 bytes before end of buffer */ |
| 359 | MEM_STATIC U32 ZSTD_readMINMATCH(const void* memPtr, U32 length) |
| 360 | { |
| 361 | switch (length) |
| 362 | { |
| 363 | default : |
| 364 | case 4 : return MEM_read32(memPtr); |
| 365 | case 3 : if (MEM_isLittleEndian()) |
| 366 | return MEM_read32(memPtr)<<8; |
| 367 | else |
| 368 | return MEM_read32(memPtr)>>8; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /* Update hashTable3 up to ip (excluded) |
| 374 | Assumption : always within prefix (i.e. not within extDict) */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 375 | static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_matchState_t* ms, |
| 376 | U32* nextToUpdate3, |
| 377 | const BYTE* const ip) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 378 | { |
| 379 | U32* const hashTable3 = ms->hashTable3; |
| 380 | U32 const hashLog3 = ms->hashLog3; |
| 381 | const BYTE* const base = ms->window.base; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 382 | U32 idx = *nextToUpdate3; |
| 383 | U32 const target = (U32)(ip - base); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 384 | size_t const hash3 = ZSTD_hash3Ptr(ip, hashLog3); |
| 385 | assert(hashLog3 > 0); |
| 386 | |
| 387 | while(idx < target) { |
| 388 | hashTable3[ZSTD_hash3Ptr(base+idx, hashLog3)] = idx; |
| 389 | idx++; |
| 390 | } |
| 391 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 392 | *nextToUpdate3 = target; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 393 | return hashTable3[hash3]; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /*-************************************* |
| 398 | * Binary Tree search |
| 399 | ***************************************/ |
| 400 | /** ZSTD_insertBt1() : add one or multiple positions to tree. |
| 401 | * ip : assumed <= iend-8 . |
| 402 | * @return : nb of positions added */ |
| 403 | static U32 ZSTD_insertBt1( |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 404 | ZSTD_matchState_t* ms, |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 405 | const BYTE* const ip, const BYTE* const iend, |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 406 | U32 const mls, const int extDict) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 407 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 408 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 409 | U32* const hashTable = ms->hashTable; |
| 410 | U32 const hashLog = cParams->hashLog; |
| 411 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
| 412 | U32* const bt = ms->chainTable; |
| 413 | U32 const btLog = cParams->chainLog - 1; |
| 414 | U32 const btMask = (1 << btLog) - 1; |
| 415 | U32 matchIndex = hashTable[h]; |
| 416 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 417 | const BYTE* const base = ms->window.base; |
| 418 | const BYTE* const dictBase = ms->window.dictBase; |
| 419 | const U32 dictLimit = ms->window.dictLimit; |
| 420 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 421 | const BYTE* const prefixStart = base + dictLimit; |
| 422 | const BYTE* match; |
| 423 | const U32 current = (U32)(ip-base); |
| 424 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
| 425 | U32* smallerPtr = bt + 2*(current&btMask); |
| 426 | U32* largerPtr = smallerPtr + 1; |
| 427 | U32 dummy32; /* to be nullified at the end */ |
| 428 | U32 const windowLow = ms->window.lowLimit; |
| 429 | U32 matchEndIdx = current+8+1; |
| 430 | size_t bestLength = 8; |
| 431 | U32 nbCompares = 1U << cParams->searchLog; |
| 432 | #ifdef ZSTD_C_PREDICT |
| 433 | U32 predictedSmall = *(bt + 2*((current-1)&btMask) + 0); |
| 434 | U32 predictedLarge = *(bt + 2*((current-1)&btMask) + 1); |
| 435 | predictedSmall += (predictedSmall>0); |
| 436 | predictedLarge += (predictedLarge>0); |
| 437 | #endif /* ZSTD_C_PREDICT */ |
| 438 | |
| 439 | DEBUGLOG(8, "ZSTD_insertBt1 (%u)", current); |
| 440 | |
| 441 | assert(ip <= iend-8); /* required for h calculation */ |
| 442 | hashTable[h] = current; /* Update Hash Table */ |
| 443 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 444 | assert(windowLow > 0); |
| 445 | while (nbCompares-- && (matchIndex >= windowLow)) { |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 446 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
| 447 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 448 | assert(matchIndex < current); |
| 449 | |
| 450 | #ifdef ZSTD_C_PREDICT /* note : can create issues when hlog small <= 11 */ |
| 451 | const U32* predictPtr = bt + 2*((matchIndex-1) & btMask); /* written this way, as bt is a roll buffer */ |
| 452 | if (matchIndex == predictedSmall) { |
| 453 | /* no need to check length, result known */ |
| 454 | *smallerPtr = matchIndex; |
| 455 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 456 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 457 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
| 458 | predictedSmall = predictPtr[1] + (predictPtr[1]>0); |
| 459 | continue; |
| 460 | } |
| 461 | if (matchIndex == predictedLarge) { |
| 462 | *largerPtr = matchIndex; |
| 463 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 464 | largerPtr = nextPtr; |
| 465 | matchIndex = nextPtr[0]; |
| 466 | predictedLarge = predictPtr[0] + (predictPtr[0]>0); |
| 467 | continue; |
| 468 | } |
| 469 | #endif |
| 470 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 471 | if (!extDict || (matchIndex+matchLength >= dictLimit)) { |
| 472 | assert(matchIndex+matchLength >= dictLimit); /* might be wrong if actually extDict */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 473 | match = base + matchIndex; |
| 474 | matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend); |
| 475 | } else { |
| 476 | match = dictBase + matchIndex; |
| 477 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
| 478 | if (matchIndex+matchLength >= dictLimit) |
| 479 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
| 480 | } |
| 481 | |
| 482 | if (matchLength > bestLength) { |
| 483 | bestLength = matchLength; |
| 484 | if (matchLength > matchEndIdx - matchIndex) |
| 485 | matchEndIdx = matchIndex + (U32)matchLength; |
| 486 | } |
| 487 | |
| 488 | if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */ |
| 489 | break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt tree */ |
| 490 | } |
| 491 | |
| 492 | if (match[matchLength] < ip[matchLength]) { /* necessarily within buffer */ |
| 493 | /* match is smaller than current */ |
| 494 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 495 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
| 496 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop searching */ |
| 497 | smallerPtr = nextPtr+1; /* new "candidate" => larger than match, which was smaller than target */ |
| 498 | matchIndex = nextPtr[1]; /* new matchIndex, larger than previous and closer to current */ |
| 499 | } else { |
| 500 | /* match is larger than current */ |
| 501 | *largerPtr = matchIndex; |
| 502 | commonLengthLarger = matchLength; |
| 503 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop searching */ |
| 504 | largerPtr = nextPtr; |
| 505 | matchIndex = nextPtr[0]; |
| 506 | } } |
| 507 | |
| 508 | *smallerPtr = *largerPtr = 0; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 509 | { U32 positions = 0; |
| 510 | if (bestLength > 384) positions = MIN(192, (U32)(bestLength - 384)); /* speed optimization */ |
| 511 | assert(matchEndIdx > current + 8); |
| 512 | return MAX(positions, matchEndIdx - (current + 8)); |
| 513 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | FORCE_INLINE_TEMPLATE |
| 517 | void ZSTD_updateTree_internal( |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 518 | ZSTD_matchState_t* ms, |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 519 | const BYTE* const ip, const BYTE* const iend, |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 520 | const U32 mls, const ZSTD_dictMode_e dictMode) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 521 | { |
| 522 | const BYTE* const base = ms->window.base; |
| 523 | U32 const target = (U32)(ip - base); |
| 524 | U32 idx = ms->nextToUpdate; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 525 | DEBUGLOG(6, "ZSTD_updateTree_internal, from %u to %u (dictMode:%u)", |
| 526 | idx, target, dictMode); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 527 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 528 | while(idx < target) { |
| 529 | U32 const forward = ZSTD_insertBt1(ms, base+idx, iend, mls, dictMode == ZSTD_extDict); |
| 530 | assert(idx < (U32)(idx + forward)); |
| 531 | idx += forward; |
| 532 | } |
| 533 | assert((size_t)(ip - base) <= (size_t)(U32)(-1)); |
| 534 | assert((size_t)(iend - base) <= (size_t)(U32)(-1)); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 535 | ms->nextToUpdate = target; |
| 536 | } |
| 537 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 538 | void ZSTD_updateTree(ZSTD_matchState_t* ms, const BYTE* ip, const BYTE* iend) { |
| 539 | ZSTD_updateTree_internal(ms, ip, iend, ms->cParams.minMatch, ZSTD_noDict); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | FORCE_INLINE_TEMPLATE |
| 543 | U32 ZSTD_insertBtAndGetAllMatches ( |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 544 | ZSTD_match_t* matches, /* store result (found matches) in this table (presumed large enough) */ |
| 545 | ZSTD_matchState_t* ms, |
| 546 | U32* nextToUpdate3, |
| 547 | const BYTE* const ip, const BYTE* const iLimit, const ZSTD_dictMode_e dictMode, |
| 548 | const U32 rep[ZSTD_REP_NUM], |
| 549 | U32 const ll0, /* tells if associated literal length is 0 or not. This value must be 0 or 1 */ |
| 550 | const U32 lengthToBeat, |
| 551 | U32 const mls /* template */) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 552 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 553 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 554 | U32 const sufficient_len = MIN(cParams->targetLength, ZSTD_OPT_NUM -1); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 555 | U32 const maxDistance = 1U << cParams->windowLog; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 556 | const BYTE* const base = ms->window.base; |
| 557 | U32 const current = (U32)(ip-base); |
| 558 | U32 const hashLog = cParams->hashLog; |
| 559 | U32 const minMatch = (mls==3) ? 3 : 4; |
| 560 | U32* const hashTable = ms->hashTable; |
| 561 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
| 562 | U32 matchIndex = hashTable[h]; |
| 563 | U32* const bt = ms->chainTable; |
| 564 | U32 const btLog = cParams->chainLog - 1; |
| 565 | U32 const btMask= (1U << btLog) - 1; |
| 566 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 567 | const BYTE* const dictBase = ms->window.dictBase; |
| 568 | U32 const dictLimit = ms->window.dictLimit; |
| 569 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 570 | const BYTE* const prefixStart = base + dictLimit; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 571 | U32 const btLow = (btMask >= current) ? 0 : current - btMask; |
| 572 | U32 const windowValid = ms->window.lowLimit; |
| 573 | U32 const windowLow = ((current - windowValid) > maxDistance) ? current - maxDistance : windowValid; |
| 574 | U32 const matchLow = windowLow ? windowLow : 1; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 575 | U32* smallerPtr = bt + 2*(current&btMask); |
| 576 | U32* largerPtr = bt + 2*(current&btMask) + 1; |
| 577 | U32 matchEndIdx = current+8+1; /* farthest referenced position of any match => detects repetitive patterns */ |
| 578 | U32 dummy32; /* to be nullified at the end */ |
| 579 | U32 mnum = 0; |
| 580 | U32 nbCompares = 1U << cParams->searchLog; |
| 581 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 582 | const ZSTD_matchState_t* dms = dictMode == ZSTD_dictMatchState ? ms->dictMatchState : NULL; |
| 583 | const ZSTD_compressionParameters* const dmsCParams = |
| 584 | dictMode == ZSTD_dictMatchState ? &dms->cParams : NULL; |
| 585 | const BYTE* const dmsBase = dictMode == ZSTD_dictMatchState ? dms->window.base : NULL; |
| 586 | const BYTE* const dmsEnd = dictMode == ZSTD_dictMatchState ? dms->window.nextSrc : NULL; |
| 587 | U32 const dmsHighLimit = dictMode == ZSTD_dictMatchState ? (U32)(dmsEnd - dmsBase) : 0; |
| 588 | U32 const dmsLowLimit = dictMode == ZSTD_dictMatchState ? dms->window.lowLimit : 0; |
| 589 | U32 const dmsIndexDelta = dictMode == ZSTD_dictMatchState ? windowLow - dmsHighLimit : 0; |
| 590 | U32 const dmsHashLog = dictMode == ZSTD_dictMatchState ? dmsCParams->hashLog : hashLog; |
| 591 | U32 const dmsBtLog = dictMode == ZSTD_dictMatchState ? dmsCParams->chainLog - 1 : btLog; |
| 592 | U32 const dmsBtMask = dictMode == ZSTD_dictMatchState ? (1U << dmsBtLog) - 1 : 0; |
| 593 | U32 const dmsBtLow = dictMode == ZSTD_dictMatchState && dmsBtMask < dmsHighLimit - dmsLowLimit ? dmsHighLimit - dmsBtMask : dmsLowLimit; |
| 594 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 595 | size_t bestLength = lengthToBeat-1; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 596 | DEBUGLOG(8, "ZSTD_insertBtAndGetAllMatches: current=%u", current); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 597 | |
| 598 | /* check repCode */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 599 | assert(ll0 <= 1); /* necessarily 1 or 0 */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 600 | { U32 const lastR = ZSTD_REP_NUM + ll0; |
| 601 | U32 repCode; |
| 602 | for (repCode = ll0; repCode < lastR; repCode++) { |
| 603 | U32 const repOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode]; |
| 604 | U32 const repIndex = current - repOffset; |
| 605 | U32 repLen = 0; |
| 606 | assert(current >= dictLimit); |
| 607 | if (repOffset-1 /* intentional overflow, discards 0 and -1 */ < current-dictLimit) { /* equivalent to `current > repIndex >= dictLimit` */ |
| 608 | if (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(ip - repOffset, minMatch)) { |
| 609 | repLen = (U32)ZSTD_count(ip+minMatch, ip+minMatch-repOffset, iLimit) + minMatch; |
| 610 | } |
| 611 | } else { /* repIndex < dictLimit || repIndex >= current */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 612 | const BYTE* const repMatch = dictMode == ZSTD_dictMatchState ? |
| 613 | dmsBase + repIndex - dmsIndexDelta : |
| 614 | dictBase + repIndex; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 615 | assert(current >= windowLow); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 616 | if ( dictMode == ZSTD_extDict |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 617 | && ( ((repOffset-1) /*intentional overflow*/ < current - windowLow) /* equivalent to `current > repIndex >= windowLow` */ |
| 618 | & (((U32)((dictLimit-1) - repIndex) >= 3) ) /* intentional overflow : do not test positions overlapping 2 memory segments */) |
| 619 | && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) ) { |
| 620 | repLen = (U32)ZSTD_count_2segments(ip+minMatch, repMatch+minMatch, iLimit, dictEnd, prefixStart) + minMatch; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 621 | } |
| 622 | if (dictMode == ZSTD_dictMatchState |
| 623 | && ( ((repOffset-1) /*intentional overflow*/ < current - (dmsLowLimit + dmsIndexDelta)) /* equivalent to `current > repIndex >= dmsLowLimit` */ |
| 624 | & ((U32)((dictLimit-1) - repIndex) >= 3) ) /* intentional overflow : do not test positions overlapping 2 memory segments */ |
| 625 | && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) ) { |
| 626 | repLen = (U32)ZSTD_count_2segments(ip+minMatch, repMatch+minMatch, iLimit, dmsEnd, prefixStart) + minMatch; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 627 | } } |
| 628 | /* save longer solution */ |
| 629 | if (repLen > bestLength) { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 630 | DEBUGLOG(8, "found repCode %u (ll0:%u, offset:%u) of length %u", |
| 631 | repCode, ll0, repOffset, repLen); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 632 | bestLength = repLen; |
| 633 | matches[mnum].off = repCode - ll0; |
| 634 | matches[mnum].len = (U32)repLen; |
| 635 | mnum++; |
| 636 | if ( (repLen > sufficient_len) |
| 637 | | (ip+repLen == iLimit) ) { /* best possible */ |
| 638 | return mnum; |
| 639 | } } } } |
| 640 | |
| 641 | /* HC3 match finder */ |
| 642 | if ((mls == 3) /*static*/ && (bestLength < mls)) { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 643 | U32 const matchIndex3 = ZSTD_insertAndFindFirstIndexHash3(ms, nextToUpdate3, ip); |
| 644 | if ((matchIndex3 >= matchLow) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 645 | & (current - matchIndex3 < (1<<18)) /*heuristic : longer distance likely too expensive*/ ) { |
| 646 | size_t mlen; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 647 | if ((dictMode == ZSTD_noDict) /*static*/ || (dictMode == ZSTD_dictMatchState) /*static*/ || (matchIndex3 >= dictLimit)) { |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 648 | const BYTE* const match = base + matchIndex3; |
| 649 | mlen = ZSTD_count(ip, match, iLimit); |
| 650 | } else { |
| 651 | const BYTE* const match = dictBase + matchIndex3; |
| 652 | mlen = ZSTD_count_2segments(ip, match, iLimit, dictEnd, prefixStart); |
| 653 | } |
| 654 | |
| 655 | /* save best solution */ |
| 656 | if (mlen >= mls /* == 3 > bestLength */) { |
| 657 | DEBUGLOG(8, "found small match with hlog3, of length %u", |
| 658 | (U32)mlen); |
| 659 | bestLength = mlen; |
| 660 | assert(current > matchIndex3); |
| 661 | assert(mnum==0); /* no prior solution */ |
| 662 | matches[0].off = (current - matchIndex3) + ZSTD_REP_MOVE; |
| 663 | matches[0].len = (U32)mlen; |
| 664 | mnum = 1; |
| 665 | if ( (mlen > sufficient_len) | |
| 666 | (ip+mlen == iLimit) ) { /* best possible length */ |
| 667 | ms->nextToUpdate = current+1; /* skip insertion */ |
| 668 | return 1; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 669 | } } } |
| 670 | /* no dictMatchState lookup: dicts don't have a populated HC3 table */ |
| 671 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 672 | |
| 673 | hashTable[h] = current; /* Update Hash Table */ |
| 674 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 675 | while (nbCompares-- && (matchIndex >= matchLow)) { |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 676 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
| 677 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 678 | const BYTE* match; |
| 679 | assert(current > matchIndex); |
| 680 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 681 | if ((dictMode == ZSTD_noDict) || (dictMode == ZSTD_dictMatchState) || (matchIndex+matchLength >= dictLimit)) { |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 682 | assert(matchIndex+matchLength >= dictLimit); /* ensure the condition is correct when !extDict */ |
| 683 | match = base + matchIndex; |
| 684 | matchLength += ZSTD_count(ip+matchLength, match+matchLength, iLimit); |
| 685 | } else { |
| 686 | match = dictBase + matchIndex; |
| 687 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iLimit, dictEnd, prefixStart); |
| 688 | if (matchIndex+matchLength >= dictLimit) |
| 689 | match = base + matchIndex; /* prepare for match[matchLength] */ |
| 690 | } |
| 691 | |
| 692 | if (matchLength > bestLength) { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 693 | DEBUGLOG(8, "found match of length %u at distance %u (offCode=%u)", |
| 694 | (U32)matchLength, current - matchIndex, current - matchIndex + ZSTD_REP_MOVE); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 695 | assert(matchEndIdx > matchIndex); |
| 696 | if (matchLength > matchEndIdx - matchIndex) |
| 697 | matchEndIdx = matchIndex + (U32)matchLength; |
| 698 | bestLength = matchLength; |
| 699 | matches[mnum].off = (current - matchIndex) + ZSTD_REP_MOVE; |
| 700 | matches[mnum].len = (U32)matchLength; |
| 701 | mnum++; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 702 | if ( (matchLength > ZSTD_OPT_NUM) |
| 703 | | (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) { |
| 704 | if (dictMode == ZSTD_dictMatchState) nbCompares = 0; /* break should also skip searching dms */ |
| 705 | break; /* drop, to preserve bt consistency (miss a little bit of compression) */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | if (match[matchLength] < ip[matchLength]) { |
| 710 | /* match smaller than current */ |
| 711 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 712 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
| 713 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 714 | smallerPtr = nextPtr+1; /* new candidate => larger than match, which was smaller than current */ |
| 715 | matchIndex = nextPtr[1]; /* new matchIndex, larger than previous, closer to current */ |
| 716 | } else { |
| 717 | *largerPtr = matchIndex; |
| 718 | commonLengthLarger = matchLength; |
| 719 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 720 | largerPtr = nextPtr; |
| 721 | matchIndex = nextPtr[0]; |
| 722 | } } |
| 723 | |
| 724 | *smallerPtr = *largerPtr = 0; |
| 725 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 726 | if (dictMode == ZSTD_dictMatchState && nbCompares) { |
| 727 | size_t const dmsH = ZSTD_hashPtr(ip, dmsHashLog, mls); |
| 728 | U32 dictMatchIndex = dms->hashTable[dmsH]; |
| 729 | const U32* const dmsBt = dms->chainTable; |
| 730 | commonLengthSmaller = commonLengthLarger = 0; |
| 731 | while (nbCompares-- && (dictMatchIndex > dmsLowLimit)) { |
| 732 | const U32* const nextPtr = dmsBt + 2*(dictMatchIndex & dmsBtMask); |
| 733 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 734 | const BYTE* match = dmsBase + dictMatchIndex; |
| 735 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iLimit, dmsEnd, prefixStart); |
| 736 | if (dictMatchIndex+matchLength >= dmsHighLimit) |
| 737 | match = base + dictMatchIndex + dmsIndexDelta; /* to prepare for next usage of match[matchLength] */ |
| 738 | |
| 739 | if (matchLength > bestLength) { |
| 740 | matchIndex = dictMatchIndex + dmsIndexDelta; |
| 741 | DEBUGLOG(8, "found dms match of length %u at distance %u (offCode=%u)", |
| 742 | (U32)matchLength, current - matchIndex, current - matchIndex + ZSTD_REP_MOVE); |
| 743 | if (matchLength > matchEndIdx - matchIndex) |
| 744 | matchEndIdx = matchIndex + (U32)matchLength; |
| 745 | bestLength = matchLength; |
| 746 | matches[mnum].off = (current - matchIndex) + ZSTD_REP_MOVE; |
| 747 | matches[mnum].len = (U32)matchLength; |
| 748 | mnum++; |
| 749 | if ( (matchLength > ZSTD_OPT_NUM) |
| 750 | | (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) { |
| 751 | break; /* drop, to guarantee consistency (miss a little bit of compression) */ |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | if (dictMatchIndex <= dmsBtLow) { break; } /* beyond tree size, stop the search */ |
| 756 | if (match[matchLength] < ip[matchLength]) { |
| 757 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
| 758 | dictMatchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
| 759 | } else { |
| 760 | /* match is larger than current */ |
| 761 | commonLengthLarger = matchLength; |
| 762 | dictMatchIndex = nextPtr[0]; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 767 | assert(matchEndIdx > current+8); |
| 768 | ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */ |
| 769 | return mnum; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | FORCE_INLINE_TEMPLATE U32 ZSTD_BtGetAllMatches ( |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 774 | ZSTD_match_t* matches, /* store result (match found, increasing size) in this table */ |
| 775 | ZSTD_matchState_t* ms, |
| 776 | U32* nextToUpdate3, |
| 777 | const BYTE* ip, const BYTE* const iHighLimit, const ZSTD_dictMode_e dictMode, |
| 778 | const U32 rep[ZSTD_REP_NUM], |
| 779 | U32 const ll0, |
| 780 | U32 const lengthToBeat) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 781 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 782 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
| 783 | U32 const matchLengthSearch = cParams->minMatch; |
| 784 | DEBUGLOG(8, "ZSTD_BtGetAllMatches"); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 785 | if (ip < ms->window.base + ms->nextToUpdate) return 0; /* skipped area */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 786 | ZSTD_updateTree_internal(ms, ip, iHighLimit, matchLengthSearch, dictMode); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 787 | switch(matchLengthSearch) |
| 788 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 789 | case 3 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 3); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 790 | default : |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 791 | case 4 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 4); |
| 792 | case 5 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 5); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 793 | case 7 : |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 794 | case 6 : return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 6); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | |
| 799 | /*-******************************* |
| 800 | * Optimal parser |
| 801 | *********************************/ |
| 802 | typedef struct repcodes_s { |
| 803 | U32 rep[3]; |
| 804 | } repcodes_t; |
| 805 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 806 | static repcodes_t ZSTD_updateRep(U32 const rep[3], U32 const offset, U32 const ll0) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 807 | { |
| 808 | repcodes_t newReps; |
| 809 | if (offset >= ZSTD_REP_NUM) { /* full offset */ |
| 810 | newReps.rep[2] = rep[1]; |
| 811 | newReps.rep[1] = rep[0]; |
| 812 | newReps.rep[0] = offset - ZSTD_REP_MOVE; |
| 813 | } else { /* repcode */ |
| 814 | U32 const repCode = offset + ll0; |
| 815 | if (repCode > 0) { /* note : if repCode==0, no change */ |
| 816 | U32 const currentOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode]; |
| 817 | newReps.rep[2] = (repCode >= 2) ? rep[1] : rep[2]; |
| 818 | newReps.rep[1] = rep[0]; |
| 819 | newReps.rep[0] = currentOffset; |
| 820 | } else { /* repCode == 0 */ |
| 821 | memcpy(&newReps, rep, sizeof(newReps)); |
| 822 | } |
| 823 | } |
| 824 | return newReps; |
| 825 | } |
| 826 | |
| 827 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 828 | static U32 ZSTD_totalLen(ZSTD_optimal_t sol) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 829 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 830 | return sol.litlen + sol.mlen; |
| 831 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 832 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 833 | #if 0 /* debug */ |
| 834 | |
| 835 | static void |
| 836 | listStats(const U32* table, int lastEltID) |
| 837 | { |
| 838 | int const nbElts = lastEltID + 1; |
| 839 | int enb; |
| 840 | for (enb=0; enb < nbElts; enb++) { |
| 841 | (void)table; |
| 842 | //RAWLOG(2, "%3i:%3i, ", enb, table[enb]); |
| 843 | RAWLOG(2, "%4i,", table[enb]); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 844 | } |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 845 | RAWLOG(2, " \n"); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 848 | #endif |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 849 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 850 | FORCE_INLINE_TEMPLATE size_t |
| 851 | ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms, |
| 852 | seqStore_t* seqStore, |
| 853 | U32 rep[ZSTD_REP_NUM], |
| 854 | const void* src, size_t srcSize, |
| 855 | const int optLevel, |
| 856 | const ZSTD_dictMode_e dictMode) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 857 | { |
| 858 | optState_t* const optStatePtr = &ms->opt; |
| 859 | const BYTE* const istart = (const BYTE*)src; |
| 860 | const BYTE* ip = istart; |
| 861 | const BYTE* anchor = istart; |
| 862 | const BYTE* const iend = istart + srcSize; |
| 863 | const BYTE* const ilimit = iend - 8; |
| 864 | const BYTE* const base = ms->window.base; |
| 865 | const BYTE* const prefixStart = base + ms->window.dictLimit; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 866 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 867 | |
| 868 | U32 const sufficient_len = MIN(cParams->targetLength, ZSTD_OPT_NUM -1); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 869 | U32 const minMatch = (cParams->minMatch == 3) ? 3 : 4; |
| 870 | U32 nextToUpdate3 = ms->nextToUpdate; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 871 | |
| 872 | ZSTD_optimal_t* const opt = optStatePtr->priceTable; |
| 873 | ZSTD_match_t* const matches = optStatePtr->matchTable; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 874 | ZSTD_optimal_t lastSequence; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 875 | |
| 876 | /* init */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 877 | DEBUGLOG(5, "ZSTD_compressBlock_opt_generic: current=%u, prefix=%u, nextToUpdate=%u", |
| 878 | (U32)(ip - base), ms->window.dictLimit, ms->nextToUpdate); |
| 879 | assert(optLevel <= 2); |
| 880 | ZSTD_rescaleFreqs(optStatePtr, (const BYTE*)src, srcSize, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 881 | ip += (ip==prefixStart); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 882 | |
| 883 | /* Match Loop */ |
| 884 | while (ip < ilimit) { |
| 885 | U32 cur, last_pos = 0; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 886 | |
| 887 | /* find first match */ |
| 888 | { U32 const litlen = (U32)(ip - anchor); |
| 889 | U32 const ll0 = !litlen; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 890 | U32 const nbMatches = ZSTD_BtGetAllMatches(matches, ms, &nextToUpdate3, ip, iend, dictMode, rep, ll0, minMatch); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 891 | if (!nbMatches) { ip++; continue; } |
| 892 | |
| 893 | /* initialize opt[0] */ |
| 894 | { U32 i ; for (i=0; i<ZSTD_REP_NUM; i++) opt[0].rep[i] = rep[i]; } |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 895 | opt[0].mlen = 0; /* means is_a_literal */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 896 | opt[0].litlen = litlen; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 897 | opt[0].price = ZSTD_literalsContribution(anchor, litlen, optStatePtr, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 898 | |
| 899 | /* large match -> immediate encoding */ |
| 900 | { U32 const maxML = matches[nbMatches-1].len; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 901 | U32 const maxOffset = matches[nbMatches-1].off; |
| 902 | DEBUGLOG(6, "found %u matches of maxLength=%u and maxOffCode=%u at cPos=%u => start new series", |
| 903 | nbMatches, maxML, maxOffset, (U32)(ip-prefixStart)); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 904 | |
| 905 | if (maxML > sufficient_len) { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 906 | lastSequence.litlen = litlen; |
| 907 | lastSequence.mlen = maxML; |
| 908 | lastSequence.off = maxOffset; |
| 909 | DEBUGLOG(6, "large match (%u>%u), immediate encoding", |
| 910 | maxML, sufficient_len); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 911 | cur = 0; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 912 | last_pos = ZSTD_totalLen(lastSequence); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 913 | goto _shortestPath; |
| 914 | } } |
| 915 | |
| 916 | /* set prices for first matches starting position == 0 */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 917 | { U32 const literalsPrice = opt[0].price + ZSTD_litLengthPrice(0, optStatePtr, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 918 | U32 pos; |
| 919 | U32 matchNb; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 920 | for (pos = 1; pos < minMatch; pos++) { |
| 921 | opt[pos].price = ZSTD_MAX_PRICE; /* mlen, litlen and price will be fixed during forward scanning */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 922 | } |
| 923 | for (matchNb = 0; matchNb < nbMatches; matchNb++) { |
| 924 | U32 const offset = matches[matchNb].off; |
| 925 | U32 const end = matches[matchNb].len; |
| 926 | repcodes_t const repHistory = ZSTD_updateRep(rep, offset, ll0); |
| 927 | for ( ; pos <= end ; pos++ ) { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 928 | U32 const matchPrice = ZSTD_getMatchPrice(offset, pos, optStatePtr, optLevel); |
| 929 | U32 const sequencePrice = literalsPrice + matchPrice; |
| 930 | DEBUGLOG(7, "rPos:%u => set initial price : %.2f", |
| 931 | pos, ZSTD_fCost(sequencePrice)); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 932 | opt[pos].mlen = pos; |
| 933 | opt[pos].off = offset; |
| 934 | opt[pos].litlen = litlen; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 935 | opt[pos].price = sequencePrice; |
| 936 | ZSTD_STATIC_ASSERT(sizeof(opt[pos].rep) == sizeof(repHistory)); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 937 | memcpy(opt[pos].rep, &repHistory, sizeof(repHistory)); |
| 938 | } } |
| 939 | last_pos = pos-1; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /* check further positions */ |
| 944 | for (cur = 1; cur <= last_pos; cur++) { |
| 945 | const BYTE* const inr = ip + cur; |
| 946 | assert(cur < ZSTD_OPT_NUM); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 947 | DEBUGLOG(7, "cPos:%zi==rPos:%u", inr-istart, cur) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 948 | |
| 949 | /* Fix current position with one literal if cheaper */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 950 | { U32 const litlen = (opt[cur-1].mlen == 0) ? opt[cur-1].litlen + 1 : 1; |
| 951 | int const price = opt[cur-1].price |
| 952 | + ZSTD_rawLiteralsCost(ip+cur-1, 1, optStatePtr, optLevel) |
| 953 | + ZSTD_litLengthPrice(litlen, optStatePtr, optLevel) |
| 954 | - ZSTD_litLengthPrice(litlen-1, optStatePtr, optLevel); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 955 | assert(price < 1000000000); /* overflow check */ |
| 956 | if (price <= opt[cur].price) { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 957 | DEBUGLOG(7, "cPos:%zi==rPos:%u : better price (%.2f<=%.2f) using literal (ll==%u) (hist:%u,%u,%u)", |
| 958 | inr-istart, cur, ZSTD_fCost(price), ZSTD_fCost(opt[cur].price), litlen, |
| 959 | opt[cur-1].rep[0], opt[cur-1].rep[1], opt[cur-1].rep[2]); |
| 960 | opt[cur].mlen = 0; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 961 | opt[cur].off = 0; |
| 962 | opt[cur].litlen = litlen; |
| 963 | opt[cur].price = price; |
| 964 | memcpy(opt[cur].rep, opt[cur-1].rep, sizeof(opt[cur].rep)); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 965 | } else { |
| 966 | DEBUGLOG(7, "cPos:%zi==rPos:%u : literal would cost more (%.2f>%.2f) (hist:%u,%u,%u)", |
| 967 | inr-istart, cur, ZSTD_fCost(price), ZSTD_fCost(opt[cur].price), |
| 968 | opt[cur].rep[0], opt[cur].rep[1], opt[cur].rep[2]); |
| 969 | } |
| 970 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 971 | |
| 972 | /* last match must start at a minimum distance of 8 from oend */ |
| 973 | if (inr > ilimit) continue; |
| 974 | |
| 975 | if (cur == last_pos) break; |
| 976 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 977 | if ( (optLevel==0) /*static_test*/ |
| 978 | && (opt[cur+1].price <= opt[cur].price + (BITCOST_MULTIPLIER/2)) ) { |
| 979 | DEBUGLOG(7, "move to next rPos:%u : price is <=", cur+1); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 980 | continue; /* skip unpromising positions; about ~+6% speed, -0.01 ratio */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 981 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 982 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 983 | { U32 const ll0 = (opt[cur].mlen != 0); |
| 984 | U32 const litlen = (opt[cur].mlen == 0) ? opt[cur].litlen : 0; |
| 985 | U32 const previousPrice = opt[cur].price; |
| 986 | U32 const basePrice = previousPrice + ZSTD_litLengthPrice(0, optStatePtr, optLevel); |
| 987 | U32 const nbMatches = ZSTD_BtGetAllMatches(matches, ms, &nextToUpdate3, inr, iend, dictMode, opt[cur].rep, ll0, minMatch); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 988 | U32 matchNb; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 989 | if (!nbMatches) { |
| 990 | DEBUGLOG(7, "rPos:%u : no match found", cur); |
| 991 | continue; |
| 992 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 993 | |
| 994 | { U32 const maxML = matches[nbMatches-1].len; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 995 | DEBUGLOG(7, "cPos:%zi==rPos:%u, found %u matches, of maxLength=%u", |
| 996 | inr-istart, cur, nbMatches, maxML); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 997 | |
| 998 | if ( (maxML > sufficient_len) |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 999 | || (cur + maxML >= ZSTD_OPT_NUM) ) { |
| 1000 | lastSequence.mlen = maxML; |
| 1001 | lastSequence.off = matches[nbMatches-1].off; |
| 1002 | lastSequence.litlen = litlen; |
| 1003 | cur -= (opt[cur].mlen==0) ? opt[cur].litlen : 0; /* last sequence is actually only literals, fix cur to last match - note : may underflow, in which case, it's first sequence, and it's okay */ |
| 1004 | last_pos = cur + ZSTD_totalLen(lastSequence); |
| 1005 | if (cur > ZSTD_OPT_NUM) cur = 0; /* underflow => first match */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1006 | goto _shortestPath; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1007 | } } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1008 | |
| 1009 | /* set prices using matches found at position == cur */ |
| 1010 | for (matchNb = 0; matchNb < nbMatches; matchNb++) { |
| 1011 | U32 const offset = matches[matchNb].off; |
| 1012 | repcodes_t const repHistory = ZSTD_updateRep(opt[cur].rep, offset, ll0); |
| 1013 | U32 const lastML = matches[matchNb].len; |
| 1014 | U32 const startML = (matchNb>0) ? matches[matchNb-1].len+1 : minMatch; |
| 1015 | U32 mlen; |
| 1016 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1017 | DEBUGLOG(7, "testing match %u => offCode=%4u, mlen=%2u, llen=%2u", |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1018 | matchNb, matches[matchNb].off, lastML, litlen); |
| 1019 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1020 | for (mlen = lastML; mlen >= startML; mlen--) { /* scan downward */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1021 | U32 const pos = cur + mlen; |
| 1022 | int const price = basePrice + ZSTD_getMatchPrice(offset, mlen, optStatePtr, optLevel); |
| 1023 | |
| 1024 | if ((pos > last_pos) || (price < opt[pos].price)) { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1025 | DEBUGLOG(7, "rPos:%u (ml=%2u) => new better price (%.2f<%.2f)", |
| 1026 | pos, mlen, ZSTD_fCost(price), ZSTD_fCost(opt[pos].price)); |
| 1027 | while (last_pos < pos) { opt[last_pos+1].price = ZSTD_MAX_PRICE; last_pos++; } /* fill empty positions */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1028 | opt[pos].mlen = mlen; |
| 1029 | opt[pos].off = offset; |
| 1030 | opt[pos].litlen = litlen; |
| 1031 | opt[pos].price = price; |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1032 | ZSTD_STATIC_ASSERT(sizeof(opt[pos].rep) == sizeof(repHistory)); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1033 | memcpy(opt[pos].rep, &repHistory, sizeof(repHistory)); |
| 1034 | } else { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1035 | DEBUGLOG(7, "rPos:%u (ml=%2u) => new price is worse (%.2f>=%.2f)", |
| 1036 | pos, mlen, ZSTD_fCost(price), ZSTD_fCost(opt[pos].price)); |
| 1037 | if (optLevel==0) break; /* early update abort; gets ~+10% speed for about -0.01 ratio loss */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1038 | } |
| 1039 | } } } |
| 1040 | } /* for (cur = 1; cur <= last_pos; cur++) */ |
| 1041 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1042 | lastSequence = opt[last_pos]; |
| 1043 | cur = last_pos > ZSTD_totalLen(lastSequence) ? last_pos - ZSTD_totalLen(lastSequence) : 0; /* single sequence, and it starts before `ip` */ |
| 1044 | assert(cur < ZSTD_OPT_NUM); /* control overflow*/ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1045 | |
| 1046 | _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1047 | assert(opt[0].mlen == 0); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1048 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1049 | { U32 const storeEnd = cur + 1; |
| 1050 | U32 storeStart = storeEnd; |
| 1051 | U32 seqPos = cur; |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1052 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1053 | DEBUGLOG(6, "start reverse traversal (last_pos:%u, cur:%u)", |
| 1054 | last_pos, cur); (void)last_pos; |
| 1055 | assert(storeEnd < ZSTD_OPT_NUM); |
| 1056 | DEBUGLOG(6, "last sequence copied into pos=%u (llen=%u,mlen=%u,ofc=%u)", |
| 1057 | storeEnd, lastSequence.litlen, lastSequence.mlen, lastSequence.off); |
| 1058 | opt[storeEnd] = lastSequence; |
| 1059 | while (seqPos > 0) { |
| 1060 | U32 const backDist = ZSTD_totalLen(opt[seqPos]); |
| 1061 | storeStart--; |
| 1062 | DEBUGLOG(6, "sequence from rPos=%u copied into pos=%u (llen=%u,mlen=%u,ofc=%u)", |
| 1063 | seqPos, storeStart, opt[seqPos].litlen, opt[seqPos].mlen, opt[seqPos].off); |
| 1064 | opt[storeStart] = opt[seqPos]; |
| 1065 | seqPos = (seqPos > backDist) ? seqPos - backDist : 0; |
| 1066 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1067 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1068 | /* save sequences */ |
| 1069 | DEBUGLOG(6, "sending selected sequences into seqStore") |
| 1070 | { U32 storePos; |
| 1071 | for (storePos=storeStart; storePos <= storeEnd; storePos++) { |
| 1072 | U32 const llen = opt[storePos].litlen; |
| 1073 | U32 const mlen = opt[storePos].mlen; |
| 1074 | U32 const offCode = opt[storePos].off; |
| 1075 | U32 const advance = llen + mlen; |
| 1076 | DEBUGLOG(6, "considering seq starting at %zi, llen=%u, mlen=%u", |
| 1077 | anchor - istart, (unsigned)llen, (unsigned)mlen); |
| 1078 | |
| 1079 | if (mlen==0) { /* only literals => must be last "sequence", actually starting a new stream of sequences */ |
| 1080 | assert(storePos == storeEnd); /* must be last sequence */ |
| 1081 | ip = anchor + llen; /* last "sequence" is a bunch of literals => don't progress anchor */ |
| 1082 | continue; /* will finish */ |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1083 | } |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1084 | |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1085 | /* repcodes update : like ZSTD_updateRep(), but update in place */ |
| 1086 | if (offCode >= ZSTD_REP_NUM) { /* full offset */ |
| 1087 | rep[2] = rep[1]; |
| 1088 | rep[1] = rep[0]; |
| 1089 | rep[0] = offCode - ZSTD_REP_MOVE; |
| 1090 | } else { /* repcode */ |
| 1091 | U32 const repCode = offCode + (llen==0); |
| 1092 | if (repCode) { /* note : if repCode==0, no change */ |
| 1093 | U32 const currentOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode]; |
| 1094 | if (repCode >= 2) rep[2] = rep[1]; |
| 1095 | rep[1] = rep[0]; |
| 1096 | rep[0] = currentOffset; |
| 1097 | } } |
| 1098 | |
| 1099 | assert(anchor + llen <= iend); |
| 1100 | ZSTD_updateStats(optStatePtr, llen, anchor, offCode, mlen); |
| 1101 | ZSTD_storeSeq(seqStore, llen, anchor, offCode, mlen-MINMATCH); |
| 1102 | anchor += advance; |
| 1103 | ip = anchor; |
| 1104 | } } |
| 1105 | ZSTD_setBasePrices(optStatePtr, optLevel); |
| 1106 | } |
| 1107 | |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1108 | } /* while (ip < ilimit) */ |
| 1109 | |
| 1110 | /* Return the last literals size */ |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1111 | return (size_t)(iend - anchor); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | size_t ZSTD_compressBlock_btopt( |
| 1116 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1117 | const void* src, size_t srcSize) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1118 | { |
| 1119 | DEBUGLOG(5, "ZSTD_compressBlock_btopt"); |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1120 | return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0 /*optLevel*/, ZSTD_noDict); |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | /* used in 2-pass strategy */ |
| 1125 | static U32 ZSTD_upscaleStat(unsigned* table, U32 lastEltIndex, int bonus) |
| 1126 | { |
| 1127 | U32 s, sum=0; |
| 1128 | assert(ZSTD_FREQ_DIV+bonus >= 0); |
| 1129 | for (s=0; s<lastEltIndex+1; s++) { |
| 1130 | table[s] <<= ZSTD_FREQ_DIV+bonus; |
| 1131 | table[s]--; |
| 1132 | sum += table[s]; |
| 1133 | } |
| 1134 | return sum; |
| 1135 | } |
| 1136 | |
| 1137 | /* used in 2-pass strategy */ |
| 1138 | MEM_STATIC void ZSTD_upscaleStats(optState_t* optPtr) |
| 1139 | { |
| 1140 | if (ZSTD_compressedLiterals(optPtr)) |
| 1141 | optPtr->litSum = ZSTD_upscaleStat(optPtr->litFreq, MaxLit, 0); |
| 1142 | optPtr->litLengthSum = ZSTD_upscaleStat(optPtr->litLengthFreq, MaxLL, 0); |
| 1143 | optPtr->matchLengthSum = ZSTD_upscaleStat(optPtr->matchLengthFreq, MaxML, 0); |
| 1144 | optPtr->offCodeSum = ZSTD_upscaleStat(optPtr->offCodeFreq, MaxOff, 0); |
| 1145 | } |
| 1146 | |
| 1147 | /* ZSTD_initStats_ultra(): |
| 1148 | * make a first compression pass, just to seed stats with more accurate starting values. |
| 1149 | * only works on first block, with no dictionary and no ldm. |
| 1150 | * this function cannot error, hence its contract must be respected. |
| 1151 | */ |
| 1152 | static void |
| 1153 | ZSTD_initStats_ultra(ZSTD_matchState_t* ms, |
| 1154 | seqStore_t* seqStore, |
| 1155 | U32 rep[ZSTD_REP_NUM], |
| 1156 | const void* src, size_t srcSize) |
| 1157 | { |
| 1158 | U32 tmpRep[ZSTD_REP_NUM]; /* updated rep codes will sink here */ |
| 1159 | memcpy(tmpRep, rep, sizeof(tmpRep)); |
| 1160 | |
| 1161 | DEBUGLOG(4, "ZSTD_initStats_ultra (srcSize=%zu)", srcSize); |
| 1162 | assert(ms->opt.litLengthSum == 0); /* first block */ |
| 1163 | assert(seqStore->sequences == seqStore->sequencesStart); /* no ldm */ |
| 1164 | assert(ms->window.dictLimit == ms->window.lowLimit); /* no dictionary */ |
| 1165 | assert(ms->window.dictLimit - ms->nextToUpdate <= 1); /* no prefix (note: intentional overflow, defined as 2-complement) */ |
| 1166 | |
| 1167 | ZSTD_compressBlock_opt_generic(ms, seqStore, tmpRep, src, srcSize, 2 /*optLevel*/, ZSTD_noDict); /* generate stats into ms->opt*/ |
| 1168 | |
| 1169 | /* invalidate first scan from history */ |
| 1170 | ZSTD_resetSeqStore(seqStore); |
| 1171 | ms->window.base -= srcSize; |
| 1172 | ms->window.dictLimit += (U32)srcSize; |
| 1173 | ms->window.lowLimit = ms->window.dictLimit; |
| 1174 | ms->nextToUpdate = ms->window.dictLimit; |
| 1175 | |
| 1176 | /* re-inforce weight of collected statistics */ |
| 1177 | ZSTD_upscaleStats(&ms->opt); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | size_t ZSTD_compressBlock_btultra( |
| 1181 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1182 | const void* src, size_t srcSize) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1183 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1184 | DEBUGLOG(5, "ZSTD_compressBlock_btultra (srcSize=%zu)", srcSize); |
| 1185 | return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_noDict); |
| 1186 | } |
| 1187 | |
| 1188 | size_t ZSTD_compressBlock_btultra2( |
| 1189 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
| 1190 | const void* src, size_t srcSize) |
| 1191 | { |
| 1192 | U32 const current = (U32)((const BYTE*)src - ms->window.base); |
| 1193 | DEBUGLOG(5, "ZSTD_compressBlock_btultra2 (srcSize=%zu)", srcSize); |
| 1194 | |
| 1195 | /* 2-pass strategy: |
| 1196 | * this strategy makes a first pass over first block to collect statistics |
| 1197 | * and seed next round's statistics with it. |
| 1198 | * After 1st pass, function forgets everything, and starts a new block. |
| 1199 | * Consequently, this can only work if no data has been previously loaded in tables, |
| 1200 | * aka, no dictionary, no prefix, no ldm preprocessing. |
| 1201 | * The compression ratio gain is generally small (~0.5% on first block), |
| 1202 | * the cost is 2x cpu time on first block. */ |
| 1203 | assert(srcSize <= ZSTD_BLOCKSIZE_MAX); |
| 1204 | if ( (ms->opt.litLengthSum==0) /* first block */ |
| 1205 | && (seqStore->sequences == seqStore->sequencesStart) /* no ldm */ |
| 1206 | && (ms->window.dictLimit == ms->window.lowLimit) /* no dictionary */ |
| 1207 | && (current == ms->window.dictLimit) /* start of frame, nothing already loaded nor skipped */ |
| 1208 | && (srcSize > ZSTD_PREDEF_THRESHOLD) |
| 1209 | ) { |
| 1210 | ZSTD_initStats_ultra(ms, seqStore, rep, src, srcSize); |
| 1211 | } |
| 1212 | |
| 1213 | return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_noDict); |
| 1214 | } |
| 1215 | |
| 1216 | size_t ZSTD_compressBlock_btopt_dictMatchState( |
| 1217 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
| 1218 | const void* src, size_t srcSize) |
| 1219 | { |
| 1220 | return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0 /*optLevel*/, ZSTD_dictMatchState); |
| 1221 | } |
| 1222 | |
| 1223 | size_t ZSTD_compressBlock_btultra_dictMatchState( |
| 1224 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
| 1225 | const void* src, size_t srcSize) |
| 1226 | { |
| 1227 | return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_dictMatchState); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | size_t ZSTD_compressBlock_btopt_extDict( |
| 1231 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1232 | const void* src, size_t srcSize) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1233 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1234 | return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0 /*optLevel*/, ZSTD_extDict); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | size_t ZSTD_compressBlock_btultra_extDict( |
| 1238 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1239 | const void* src, size_t srcSize) |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1240 | { |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1241 | return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2 /*optLevel*/, ZSTD_extDict); |
Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1242 | } |
Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1243 | |
| 1244 | /* note : no btultra2 variant for extDict nor dictMatchState, |
| 1245 | * because btultra2 is not meant to work with dictionaries |
| 1246 | * and is only specific for the first block (no prefix) */ |