William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-present, 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" |
| 12 | #include "zstd_double_fast.h" |
| 13 | |
| 14 | |
| 15 | void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms, |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 16 | void const* end, ZSTD_dictTableLoadMethod_e dtlm) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 17 | { |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 18 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 19 | U32* const hashLarge = ms->hashTable; |
| 20 | U32 const hBitsL = cParams->hashLog; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 21 | U32 const mls = cParams->minMatch; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 22 | U32* const hashSmall = ms->chainTable; |
| 23 | U32 const hBitsS = cParams->chainLog; |
| 24 | const BYTE* const base = ms->window.base; |
| 25 | const BYTE* ip = base + ms->nextToUpdate; |
| 26 | const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; |
| 27 | const U32 fastHashFillStep = 3; |
| 28 | |
| 29 | /* Always insert every fastHashFillStep position into the hash tables. |
| 30 | * Insert the other positions into the large hash table if their entry |
| 31 | * is empty. |
| 32 | */ |
| 33 | for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) { |
| 34 | U32 const current = (U32)(ip - base); |
| 35 | U32 i; |
| 36 | for (i = 0; i < fastHashFillStep; ++i) { |
| 37 | size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls); |
| 38 | size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8); |
| 39 | if (i == 0) |
| 40 | hashSmall[smHash] = current + i; |
| 41 | if (i == 0 || hashLarge[lgHash] == 0) |
| 42 | hashLarge[lgHash] = current + i; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 43 | /* Only load extra positions for ZSTD_dtlm_full */ |
| 44 | if (dtlm == ZSTD_dtlm_fast) |
| 45 | break; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 46 | } } |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
| 50 | FORCE_INLINE_TEMPLATE |
| 51 | size_t ZSTD_compressBlock_doubleFast_generic( |
| 52 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 53 | void const* src, size_t srcSize, |
| 54 | U32 const mls /* template */, ZSTD_dictMode_e const dictMode) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 55 | { |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 56 | ZSTD_compressionParameters const* cParams = &ms->cParams; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 57 | U32* const hashLong = ms->hashTable; |
| 58 | const U32 hBitsL = cParams->hashLog; |
| 59 | U32* const hashSmall = ms->chainTable; |
| 60 | const U32 hBitsS = cParams->chainLog; |
| 61 | const BYTE* const base = ms->window.base; |
| 62 | const BYTE* const istart = (const BYTE*)src; |
| 63 | const BYTE* ip = istart; |
| 64 | const BYTE* anchor = istart; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 65 | const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); |
| 66 | const U32 lowestValid = ms->window.dictLimit; |
| 67 | const U32 maxDistance = 1U << cParams->windowLog; |
| 68 | const U32 prefixLowestIndex = (endIndex - lowestValid > maxDistance) ? endIndex - maxDistance : lowestValid; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 69 | const BYTE* const prefixLowest = base + prefixLowestIndex; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 70 | const BYTE* const iend = istart + srcSize; |
| 71 | const BYTE* const ilimit = iend - HASH_READ_SIZE; |
| 72 | U32 offset_1=rep[0], offset_2=rep[1]; |
| 73 | U32 offsetSaved = 0; |
| 74 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 75 | const ZSTD_matchState_t* const dms = ms->dictMatchState; |
| 76 | const ZSTD_compressionParameters* const dictCParams = |
| 77 | dictMode == ZSTD_dictMatchState ? |
| 78 | &dms->cParams : NULL; |
| 79 | const U32* const dictHashLong = dictMode == ZSTD_dictMatchState ? |
| 80 | dms->hashTable : NULL; |
| 81 | const U32* const dictHashSmall = dictMode == ZSTD_dictMatchState ? |
| 82 | dms->chainTable : NULL; |
| 83 | const U32 dictStartIndex = dictMode == ZSTD_dictMatchState ? |
| 84 | dms->window.dictLimit : 0; |
| 85 | const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ? |
| 86 | dms->window.base : NULL; |
| 87 | const BYTE* const dictStart = dictMode == ZSTD_dictMatchState ? |
| 88 | dictBase + dictStartIndex : NULL; |
| 89 | const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ? |
| 90 | dms->window.nextSrc : NULL; |
| 91 | const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ? |
| 92 | prefixLowestIndex - (U32)(dictEnd - dictBase) : |
| 93 | 0; |
| 94 | const U32 dictHBitsL = dictMode == ZSTD_dictMatchState ? |
| 95 | dictCParams->hashLog : hBitsL; |
| 96 | const U32 dictHBitsS = dictMode == ZSTD_dictMatchState ? |
| 97 | dictCParams->chainLog : hBitsS; |
| 98 | const U32 dictAndPrefixLength = (U32)(ip - prefixLowest + dictEnd - dictStart); |
| 99 | |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 100 | DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_generic"); |
| 101 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 102 | assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState); |
| 103 | |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 104 | /* if a dictionary is attached, it must be within window range */ |
| 105 | if (dictMode == ZSTD_dictMatchState) { |
| 106 | assert(lowestValid + maxDistance >= endIndex); |
| 107 | } |
| 108 | |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 109 | /* init */ |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 110 | ip += (dictAndPrefixLength == 0); |
| 111 | if (dictMode == ZSTD_noDict) { |
| 112 | U32 const maxRep = (U32)(ip - prefixLowest); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 113 | if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; |
| 114 | if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; |
| 115 | } |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 116 | if (dictMode == ZSTD_dictMatchState) { |
| 117 | /* dictMatchState repCode checks don't currently handle repCode == 0 |
| 118 | * disabling. */ |
| 119 | assert(offset_1 <= dictAndPrefixLength); |
| 120 | assert(offset_2 <= dictAndPrefixLength); |
| 121 | } |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 122 | |
| 123 | /* Main Search Loop */ |
| 124 | while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ |
| 125 | size_t mLength; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 126 | U32 offset; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 127 | size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8); |
| 128 | size_t const h = ZSTD_hashPtr(ip, hBitsS, mls); |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 129 | size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8); |
| 130 | size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 131 | U32 const current = (U32)(ip-base); |
| 132 | U32 const matchIndexL = hashLong[h2]; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 133 | U32 matchIndexS = hashSmall[h]; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 134 | const BYTE* matchLong = base + matchIndexL; |
| 135 | const BYTE* match = base + matchIndexS; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 136 | const U32 repIndex = current + 1 - offset_1; |
| 137 | const BYTE* repMatch = (dictMode == ZSTD_dictMatchState |
| 138 | && repIndex < prefixLowestIndex) ? |
| 139 | dictBase + (repIndex - dictIndexDelta) : |
| 140 | base + repIndex; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 141 | hashLong[h2] = hashSmall[h] = current; /* update hash tables */ |
| 142 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 143 | /* check dictMatchState repcode */ |
| 144 | if (dictMode == ZSTD_dictMatchState |
| 145 | && ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */) |
| 146 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
| 147 | const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; |
| 148 | mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4; |
| 149 | ip++; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 150 | ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH); |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 151 | goto _match_stored; |
| 152 | } |
| 153 | |
| 154 | /* check noDict repcode */ |
| 155 | if ( dictMode == ZSTD_noDict |
| 156 | && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) { |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 157 | mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; |
| 158 | ip++; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 159 | ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH); |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 160 | goto _match_stored; |
| 161 | } |
| 162 | |
| 163 | if (matchIndexL > prefixLowestIndex) { |
| 164 | /* check prefix long match */ |
| 165 | if (MEM_read64(matchLong) == MEM_read64(ip)) { |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 166 | mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8; |
| 167 | offset = (U32)(ip-matchLong); |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 168 | while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ |
| 169 | goto _match_found; |
| 170 | } |
| 171 | } else if (dictMode == ZSTD_dictMatchState) { |
| 172 | /* check dictMatchState long match */ |
| 173 | U32 const dictMatchIndexL = dictHashLong[dictHL]; |
| 174 | const BYTE* dictMatchL = dictBase + dictMatchIndexL; |
| 175 | assert(dictMatchL < dictEnd); |
| 176 | |
| 177 | if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) { |
| 178 | mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8; |
| 179 | offset = (U32)(current - dictMatchIndexL - dictIndexDelta); |
| 180 | while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */ |
| 181 | goto _match_found; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 182 | } } |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 183 | |
| 184 | if (matchIndexS > prefixLowestIndex) { |
| 185 | /* check prefix short match */ |
| 186 | if (MEM_read32(match) == MEM_read32(ip)) { |
| 187 | goto _search_next_long; |
| 188 | } |
| 189 | } else if (dictMode == ZSTD_dictMatchState) { |
| 190 | /* check dictMatchState short match */ |
| 191 | U32 const dictMatchIndexS = dictHashSmall[dictHS]; |
| 192 | match = dictBase + dictMatchIndexS; |
| 193 | matchIndexS = dictMatchIndexS + dictIndexDelta; |
| 194 | |
| 195 | if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) { |
| 196 | goto _search_next_long; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 197 | } } |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 198 | |
| 199 | ip += ((ip-anchor) >> kSearchStrength) + 1; |
| 200 | continue; |
| 201 | |
| 202 | _search_next_long: |
| 203 | |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 204 | { size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8); |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 205 | size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8); |
| 206 | U32 const matchIndexL3 = hashLong[hl3]; |
| 207 | const BYTE* matchL3 = base + matchIndexL3; |
| 208 | hashLong[hl3] = current + 1; |
| 209 | |
| 210 | /* check prefix long +1 match */ |
| 211 | if (matchIndexL3 > prefixLowestIndex) { |
| 212 | if (MEM_read64(matchL3) == MEM_read64(ip+1)) { |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 213 | mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8; |
| 214 | ip++; |
| 215 | offset = (U32)(ip-matchL3); |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 216 | while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */ |
| 217 | goto _match_found; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 218 | } |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 219 | } else if (dictMode == ZSTD_dictMatchState) { |
| 220 | /* check dict long +1 match */ |
| 221 | U32 const dictMatchIndexL3 = dictHashLong[dictHLNext]; |
| 222 | const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3; |
| 223 | assert(dictMatchL3 < dictEnd); |
| 224 | if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) { |
| 225 | mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8; |
| 226 | ip++; |
| 227 | offset = (U32)(current + 1 - dictMatchIndexL3 - dictIndexDelta); |
| 228 | while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */ |
| 229 | goto _match_found; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 230 | } } } |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 231 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 232 | /* if no long +1 match, explore the short match we found */ |
| 233 | if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) { |
| 234 | mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4; |
| 235 | offset = (U32)(current - matchIndexS); |
| 236 | while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
| 237 | } else { |
| 238 | mLength = ZSTD_count(ip+4, match+4, iend) + 4; |
| 239 | offset = (U32)(ip - match); |
| 240 | while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
| 241 | } |
| 242 | |
| 243 | /* fall-through */ |
| 244 | |
| 245 | _match_found: |
| 246 | offset_2 = offset_1; |
| 247 | offset_1 = offset; |
| 248 | |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 249 | ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 250 | |
| 251 | _match_stored: |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 252 | /* match found */ |
| 253 | ip += mLength; |
| 254 | anchor = ip; |
| 255 | |
| 256 | if (ip <= ilimit) { |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 257 | /* Complementary insertion */ |
| 258 | /* done after iLimit test, as candidates could be > iend-8 */ |
| 259 | { U32 const indexToInsert = current+2; |
| 260 | hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert; |
| 261 | hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base); |
| 262 | hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert; |
| 263 | hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base); |
| 264 | } |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 265 | |
| 266 | /* check immediate repcode */ |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 267 | if (dictMode == ZSTD_dictMatchState) { |
| 268 | while (ip <= ilimit) { |
| 269 | U32 const current2 = (U32)(ip-base); |
| 270 | U32 const repIndex2 = current2 - offset_2; |
| 271 | const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState |
| 272 | && repIndex2 < prefixLowestIndex ? |
| 273 | dictBase - dictIndexDelta + repIndex2 : |
| 274 | base + repIndex2; |
| 275 | if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */) |
| 276 | && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { |
| 277 | const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend; |
| 278 | size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4; |
| 279 | U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ |
| 280 | ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH); |
| 281 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2; |
| 282 | hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2; |
| 283 | ip += repLength2; |
| 284 | anchor = ip; |
| 285 | continue; |
| 286 | } |
| 287 | break; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 288 | } } |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 289 | |
| 290 | if (dictMode == ZSTD_noDict) { |
| 291 | while ( (ip <= ilimit) |
| 292 | && ( (offset_2>0) |
| 293 | & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { |
| 294 | /* store sequence */ |
| 295 | size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; |
| 296 | U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */ |
| 297 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base); |
| 298 | hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base); |
| 299 | ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH); |
| 300 | ip += rLength; |
| 301 | anchor = ip; |
| 302 | continue; /* faster when present ... (?) */ |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 303 | } } } |
| 304 | } /* while (ip < ilimit) */ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 305 | |
| 306 | /* save reps for next block */ |
| 307 | rep[0] = offset_1 ? offset_1 : offsetSaved; |
| 308 | rep[1] = offset_2 ? offset_2 : offsetSaved; |
| 309 | |
| 310 | /* Return the last literals size */ |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 311 | return (size_t)(iend - anchor); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | size_t ZSTD_compressBlock_doubleFast( |
| 316 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 317 | void const* src, size_t srcSize) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 318 | { |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 319 | const U32 mls = ms->cParams.minMatch; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 320 | switch(mls) |
| 321 | { |
| 322 | default: /* includes case 3 */ |
| 323 | case 4 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 324 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 325 | case 5 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 326 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 327 | case 6 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 328 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 329 | case 7 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 330 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | size_t ZSTD_compressBlock_doubleFast_dictMatchState( |
| 336 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
| 337 | void const* src, size_t srcSize) |
| 338 | { |
| 339 | const U32 mls = ms->cParams.minMatch; |
| 340 | switch(mls) |
| 341 | { |
| 342 | default: /* includes case 3 */ |
| 343 | case 4 : |
| 344 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState); |
| 345 | case 5 : |
| 346 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState); |
| 347 | case 6 : |
| 348 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState); |
| 349 | case 7 : |
| 350 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | |
| 355 | static size_t ZSTD_compressBlock_doubleFast_extDict_generic( |
| 356 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 357 | void const* src, size_t srcSize, |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 358 | U32 const mls /* template */) |
| 359 | { |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 360 | ZSTD_compressionParameters const* cParams = &ms->cParams; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 361 | U32* const hashLong = ms->hashTable; |
| 362 | U32 const hBitsL = cParams->hashLog; |
| 363 | U32* const hashSmall = ms->chainTable; |
| 364 | U32 const hBitsS = cParams->chainLog; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 365 | const BYTE* const istart = (const BYTE*)src; |
| 366 | const BYTE* ip = istart; |
| 367 | const BYTE* anchor = istart; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 368 | const BYTE* const iend = istart + srcSize; |
| 369 | const BYTE* const ilimit = iend - 8; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 370 | const BYTE* const base = ms->window.base; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 371 | const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); |
| 372 | const U32 maxDistance = 1U << cParams->windowLog; |
| 373 | const U32 lowestValid = ms->window.lowLimit; |
| 374 | const U32 lowLimit = (endIndex - lowestValid > maxDistance) ? endIndex - maxDistance : lowestValid; |
| 375 | const U32 dictStartIndex = lowLimit; |
| 376 | const U32 dictLimit = ms->window.dictLimit; |
| 377 | const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 378 | const BYTE* const prefixStart = base + prefixStartIndex; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 379 | const BYTE* const dictBase = ms->window.dictBase; |
| 380 | const BYTE* const dictStart = dictBase + dictStartIndex; |
| 381 | const BYTE* const dictEnd = dictBase + prefixStartIndex; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 382 | U32 offset_1=rep[0], offset_2=rep[1]; |
| 383 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 384 | DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize); |
| 385 | |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 386 | /* if extDict is invalidated due to maxDistance, switch to "regular" variant */ |
| 387 | if (prefixStartIndex == dictStartIndex) |
| 388 | return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_noDict); |
| 389 | |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 390 | /* Search Loop */ |
| 391 | while (ip < ilimit) { /* < instead of <=, because (ip+1) */ |
| 392 | const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls); |
| 393 | const U32 matchIndex = hashSmall[hSmall]; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 394 | const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 395 | const BYTE* match = matchBase + matchIndex; |
| 396 | |
| 397 | const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8); |
| 398 | const U32 matchLongIndex = hashLong[hLong]; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 399 | const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 400 | const BYTE* matchLong = matchLongBase + matchLongIndex; |
| 401 | |
| 402 | const U32 current = (U32)(ip-base); |
| 403 | const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */ |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 404 | const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base; |
| 405 | const BYTE* const repMatch = repBase + repIndex; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 406 | size_t mLength; |
| 407 | hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */ |
| 408 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 409 | if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */ |
| 410 | & (repIndex > dictStartIndex)) |
| 411 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
| 412 | const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; |
| 413 | mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 414 | ip++; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 415 | ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 416 | } else { |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 417 | if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) { |
| 418 | const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend; |
| 419 | const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 420 | U32 offset; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 421 | mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 422 | offset = current - matchLongIndex; |
| 423 | while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ |
| 424 | offset_2 = offset_1; |
| 425 | offset_1 = offset; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 426 | ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 427 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 428 | } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) { |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 429 | size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8); |
| 430 | U32 const matchIndex3 = hashLong[h3]; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 431 | const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 432 | const BYTE* match3 = match3Base + matchIndex3; |
| 433 | U32 offset; |
| 434 | hashLong[h3] = current + 1; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 435 | if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) { |
| 436 | const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend; |
| 437 | const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart; |
| 438 | mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 439 | ip++; |
| 440 | offset = current+1 - matchIndex3; |
| 441 | while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */ |
| 442 | } else { |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 443 | const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend; |
| 444 | const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart; |
| 445 | mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 446 | offset = current - matchIndex; |
| 447 | while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
| 448 | } |
| 449 | offset_2 = offset_1; |
| 450 | offset_1 = offset; |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 451 | ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 452 | |
| 453 | } else { |
| 454 | ip += ((ip-anchor) >> kSearchStrength) + 1; |
| 455 | continue; |
| 456 | } } |
| 457 | |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 458 | /* move to next sequence start */ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 459 | ip += mLength; |
| 460 | anchor = ip; |
| 461 | |
| 462 | if (ip <= ilimit) { |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 463 | /* Complementary insertion */ |
| 464 | /* done after iLimit test, as candidates could be > iend-8 */ |
| 465 | { U32 const indexToInsert = current+2; |
| 466 | hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert; |
| 467 | hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base); |
| 468 | hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert; |
| 469 | hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base); |
| 470 | } |
| 471 | |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 472 | /* check immediate repcode */ |
| 473 | while (ip <= ilimit) { |
| 474 | U32 const current2 = (U32)(ip-base); |
| 475 | U32 const repIndex2 = current2 - offset_2; |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 476 | const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2; |
| 477 | if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */ |
| 478 | & (repIndex2 > dictStartIndex)) |
| 479 | && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { |
| 480 | const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; |
| 481 | size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; |
| 482 | U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 483 | ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH); |
| 484 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2; |
| 485 | hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2; |
| 486 | ip += repLength2; |
| 487 | anchor = ip; |
| 488 | continue; |
| 489 | } |
| 490 | break; |
| 491 | } } } |
| 492 | |
| 493 | /* save reps for next block */ |
| 494 | rep[0] = offset_1; |
| 495 | rep[1] = offset_2; |
| 496 | |
| 497 | /* Return the last literals size */ |
Devmalya Paul | eb2e955 | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 498 | return (size_t)(iend - anchor); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
| 502 | size_t ZSTD_compressBlock_doubleFast_extDict( |
| 503 | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 504 | void const* src, size_t srcSize) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 505 | { |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 506 | U32 const mls = ms->cParams.minMatch; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 507 | switch(mls) |
| 508 | { |
| 509 | default: /* includes case 3 */ |
| 510 | case 4 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 511 | return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 512 | case 5 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 513 | return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 514 | case 6 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 515 | return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 516 | case 7 : |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 517 | return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 518 | } |
| 519 | } |