/src/duckdb/third_party/zstd/compress/zstd_lazy.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
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/zstd_compress_internal.h" |
12 | | #include "zstd/compress/zstd_lazy.h" |
13 | | #include "zstd/common/bits.h" /* ZSTD_countTrailingZeros64 */ |
14 | | |
15 | | namespace duckdb_zstd { |
16 | | |
17 | | #if !defined(ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR) \ |
18 | | || !defined(ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR) \ |
19 | | || !defined(ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR) \ |
20 | | || !defined(ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR) |
21 | | |
22 | 0 | #define kLazySkippingStep 8 |
23 | | |
24 | | |
25 | | /*-************************************* |
26 | | * Binary Tree search |
27 | | ***************************************/ |
28 | | |
29 | | static |
30 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
31 | | void ZSTD_updateDUBT(ZSTD_matchState_t* ms, |
32 | | const BYTE* ip, const BYTE* iend, |
33 | | U32 mls) |
34 | 0 | { |
35 | 0 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
36 | 0 | U32* const hashTable = ms->hashTable; |
37 | 0 | U32 const hashLog = cParams->hashLog; |
38 | |
|
39 | 0 | U32* const bt = ms->chainTable; |
40 | 0 | U32 const btLog = cParams->chainLog - 1; |
41 | 0 | U32 const btMask = (1 << btLog) - 1; |
42 | |
|
43 | 0 | const BYTE* const base = ms->window.base; |
44 | 0 | U32 const target = (U32)(ip - base); |
45 | 0 | U32 idx = ms->nextToUpdate; |
46 | |
|
47 | 0 | if (idx != target) |
48 | 0 | DEBUGLOG(7, "ZSTD_updateDUBT, from %u to %u (dictLimit:%u)", |
49 | 0 | idx, target, ms->window.dictLimit); |
50 | 0 | assert(ip + 8 <= iend); /* condition for ZSTD_hashPtr */ |
51 | 0 | (void)iend; |
52 | |
|
53 | 0 | assert(idx >= ms->window.dictLimit); /* condition for valid base+idx */ |
54 | 0 | for ( ; idx < target ; idx++) { |
55 | 0 | size_t const h = ZSTD_hashPtr(base + idx, hashLog, mls); /* assumption : ip + 8 <= iend */ |
56 | 0 | U32 const matchIndex = hashTable[h]; |
57 | |
|
58 | 0 | U32* const nextCandidatePtr = bt + 2*(idx&btMask); |
59 | 0 | U32* const sortMarkPtr = nextCandidatePtr + 1; |
60 | |
|
61 | 0 | DEBUGLOG(8, "ZSTD_updateDUBT: insert %u", idx); |
62 | 0 | hashTable[h] = idx; /* Update Hash Table */ |
63 | 0 | *nextCandidatePtr = matchIndex; /* update BT like a chain */ |
64 | 0 | *sortMarkPtr = ZSTD_DUBT_UNSORTED_MARK; |
65 | 0 | } |
66 | 0 | ms->nextToUpdate = target; |
67 | 0 | } |
68 | | |
69 | | |
70 | | /** ZSTD_insertDUBT1() : |
71 | | * sort one already inserted but unsorted position |
72 | | * assumption : curr >= btlow == (curr - btmask) |
73 | | * doesn't fail */ |
74 | | static |
75 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
76 | | void ZSTD_insertDUBT1(const ZSTD_matchState_t* ms, |
77 | | U32 curr, const BYTE* inputEnd, |
78 | | U32 nbCompares, U32 btLow, |
79 | | const ZSTD_dictMode_e dictMode) |
80 | 0 | { |
81 | 0 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
82 | 0 | U32* const bt = ms->chainTable; |
83 | 0 | U32 const btLog = cParams->chainLog - 1; |
84 | 0 | U32 const btMask = (1 << btLog) - 1; |
85 | 0 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
86 | 0 | const BYTE* const base = ms->window.base; |
87 | 0 | const BYTE* const dictBase = ms->window.dictBase; |
88 | 0 | const U32 dictLimit = ms->window.dictLimit; |
89 | 0 | const BYTE* const ip = (curr>=dictLimit) ? base + curr : dictBase + curr; |
90 | 0 | const BYTE* const iend = (curr>=dictLimit) ? inputEnd : dictBase + dictLimit; |
91 | 0 | const BYTE* const dictEnd = dictBase + dictLimit; |
92 | 0 | const BYTE* const prefixStart = base + dictLimit; |
93 | 0 | const BYTE* match; |
94 | 0 | U32* smallerPtr = bt + 2*(curr&btMask); |
95 | 0 | U32* largerPtr = smallerPtr + 1; |
96 | 0 | U32 matchIndex = *smallerPtr; /* this candidate is unsorted : next sorted candidate is reached through *smallerPtr, while *largerPtr contains previous unsorted candidate (which is already saved and can be overwritten) */ |
97 | 0 | U32 dummy32; /* to be nullified at the end */ |
98 | 0 | U32 const windowValid = ms->window.lowLimit; |
99 | 0 | U32 const maxDistance = 1U << cParams->windowLog; |
100 | 0 | U32 const windowLow = (curr - windowValid > maxDistance) ? curr - maxDistance : windowValid; |
101 | | |
102 | |
|
103 | 0 | DEBUGLOG(8, "ZSTD_insertDUBT1(%u) (dictLimit=%u, lowLimit=%u)", |
104 | 0 | curr, dictLimit, windowLow); |
105 | 0 | assert(curr >= btLow); |
106 | 0 | assert(ip < iend); /* condition for ZSTD_count */ |
107 | |
|
108 | 0 | for (; nbCompares && (matchIndex > windowLow); --nbCompares) { |
109 | 0 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
110 | 0 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
111 | 0 | assert(matchIndex < curr); |
112 | | /* note : all candidates are now supposed sorted, |
113 | | * but it's still possible to have nextPtr[1] == ZSTD_DUBT_UNSORTED_MARK |
114 | | * when a real index has the same value as ZSTD_DUBT_UNSORTED_MARK */ |
115 | |
|
116 | 0 | if ( (dictMode != ZSTD_extDict) |
117 | 0 | || (matchIndex+matchLength >= dictLimit) /* both in current segment*/ |
118 | 0 | || (curr < dictLimit) /* both in extDict */) { |
119 | 0 | const BYTE* const mBase = ( (dictMode != ZSTD_extDict) |
120 | 0 | || (matchIndex+matchLength >= dictLimit)) ? |
121 | 0 | base : dictBase; |
122 | 0 | assert( (matchIndex+matchLength >= dictLimit) /* might be wrong if extDict is incorrectly set to 0 */ |
123 | 0 | || (curr < dictLimit) ); |
124 | 0 | match = mBase + matchIndex; |
125 | 0 | matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend); |
126 | 0 | } else { |
127 | 0 | match = dictBase + matchIndex; |
128 | 0 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
129 | 0 | if (matchIndex+matchLength >= dictLimit) |
130 | 0 | match = base + matchIndex; /* preparation for next read of match[matchLength] */ |
131 | 0 | } |
132 | |
|
133 | 0 | DEBUGLOG(8, "ZSTD_insertDUBT1: comparing %u with %u : found %u common bytes ", |
134 | 0 | curr, matchIndex, (U32)matchLength); |
135 | |
|
136 | 0 | if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */ |
137 | 0 | break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt tree */ |
138 | 0 | } |
139 | | |
140 | 0 | if (match[matchLength] < ip[matchLength]) { /* necessarily within buffer */ |
141 | | /* match is smaller than current */ |
142 | 0 | *smallerPtr = matchIndex; /* update smaller idx */ |
143 | 0 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
144 | 0 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop searching */ |
145 | 0 | DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is smaller : next => %u", |
146 | 0 | matchIndex, btLow, nextPtr[1]); |
147 | 0 | smallerPtr = nextPtr+1; /* new "candidate" => larger than match, which was smaller than target */ |
148 | 0 | matchIndex = nextPtr[1]; /* new matchIndex, larger than previous and closer to current */ |
149 | 0 | } else { |
150 | | /* match is larger than current */ |
151 | 0 | *largerPtr = matchIndex; |
152 | 0 | commonLengthLarger = matchLength; |
153 | 0 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop searching */ |
154 | 0 | DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is larger => %u", |
155 | 0 | matchIndex, btLow, nextPtr[0]); |
156 | 0 | largerPtr = nextPtr; |
157 | 0 | matchIndex = nextPtr[0]; |
158 | 0 | } } |
159 | |
|
160 | 0 | *smallerPtr = *largerPtr = 0; |
161 | 0 | } |
162 | | |
163 | | |
164 | | static |
165 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
166 | | size_t ZSTD_DUBT_findBetterDictMatch ( |
167 | | const ZSTD_matchState_t* ms, |
168 | | const BYTE* const ip, const BYTE* const iend, |
169 | | size_t* offsetPtr, |
170 | | size_t bestLength, |
171 | | U32 nbCompares, |
172 | | U32 const mls, |
173 | | const ZSTD_dictMode_e dictMode) |
174 | 0 | { |
175 | 0 | const ZSTD_matchState_t * const dms = ms->dictMatchState; |
176 | 0 | const ZSTD_compressionParameters* const dmsCParams = &dms->cParams; |
177 | 0 | const U32 * const dictHashTable = dms->hashTable; |
178 | 0 | U32 const hashLog = dmsCParams->hashLog; |
179 | 0 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
180 | 0 | U32 dictMatchIndex = dictHashTable[h]; |
181 | |
|
182 | 0 | const BYTE* const base = ms->window.base; |
183 | 0 | const BYTE* const prefixStart = base + ms->window.dictLimit; |
184 | 0 | U32 const curr = (U32)(ip-base); |
185 | 0 | const BYTE* const dictBase = dms->window.base; |
186 | 0 | const BYTE* const dictEnd = dms->window.nextSrc; |
187 | 0 | U32 const dictHighLimit = (U32)(dms->window.nextSrc - dms->window.base); |
188 | 0 | U32 const dictLowLimit = dms->window.lowLimit; |
189 | 0 | U32 const dictIndexDelta = ms->window.lowLimit - dictHighLimit; |
190 | |
|
191 | 0 | U32* const dictBt = dms->chainTable; |
192 | 0 | U32 const btLog = dmsCParams->chainLog - 1; |
193 | 0 | U32 const btMask = (1 << btLog) - 1; |
194 | 0 | U32 const btLow = (btMask >= dictHighLimit - dictLowLimit) ? dictLowLimit : dictHighLimit - btMask; |
195 | |
|
196 | 0 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
197 | |
|
198 | 0 | (void)dictMode; |
199 | 0 | assert(dictMode == ZSTD_dictMatchState); |
200 | |
|
201 | 0 | for (; nbCompares && (dictMatchIndex > dictLowLimit); --nbCompares) { |
202 | 0 | U32* const nextPtr = dictBt + 2*(dictMatchIndex & btMask); |
203 | 0 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
204 | 0 | const BYTE* match = dictBase + dictMatchIndex; |
205 | 0 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
206 | 0 | if (dictMatchIndex+matchLength >= dictHighLimit) |
207 | 0 | match = base + dictMatchIndex + dictIndexDelta; /* to prepare for next usage of match[matchLength] */ |
208 | |
|
209 | 0 | if (matchLength > bestLength) { |
210 | 0 | U32 matchIndex = dictMatchIndex + dictIndexDelta; |
211 | 0 | if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) ) { |
212 | 0 | DEBUGLOG(9, "ZSTD_DUBT_findBetterDictMatch(%u) : found better match length %u -> %u and offsetCode %u -> %u (dictMatchIndex %u, matchIndex %u)", |
213 | 0 | curr, (U32)bestLength, (U32)matchLength, (U32)*offsetPtr, OFFSET_TO_OFFBASE(curr - matchIndex), dictMatchIndex, matchIndex); |
214 | 0 | bestLength = matchLength, *offsetPtr = OFFSET_TO_OFFBASE(curr - matchIndex); |
215 | 0 | } |
216 | 0 | if (ip+matchLength == iend) { /* reached end of input : ip[matchLength] is not valid, no way to know if it's larger or smaller than match */ |
217 | 0 | break; /* drop, to guarantee consistency (miss a little bit of compression) */ |
218 | 0 | } |
219 | 0 | } |
220 | | |
221 | 0 | if (match[matchLength] < ip[matchLength]) { |
222 | 0 | if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */ |
223 | 0 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
224 | 0 | dictMatchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
225 | 0 | } else { |
226 | | /* match is larger than current */ |
227 | 0 | if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */ |
228 | 0 | commonLengthLarger = matchLength; |
229 | 0 | dictMatchIndex = nextPtr[0]; |
230 | 0 | } |
231 | 0 | } |
232 | |
|
233 | 0 | if (bestLength >= MINMATCH) { |
234 | 0 | U32 const mIndex = curr - (U32)OFFBASE_TO_OFFSET(*offsetPtr); (void)mIndex; |
235 | 0 | DEBUGLOG(8, "ZSTD_DUBT_findBetterDictMatch(%u) : found match of length %u and offsetCode %u (pos %u)", |
236 | 0 | curr, (U32)bestLength, (U32)*offsetPtr, mIndex); |
237 | 0 | } |
238 | 0 | return bestLength; |
239 | |
|
240 | 0 | } |
241 | | |
242 | | |
243 | | static |
244 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
245 | | size_t ZSTD_DUBT_findBestMatch(ZSTD_matchState_t* ms, |
246 | | const BYTE* const ip, const BYTE* const iend, |
247 | | size_t* offBasePtr, |
248 | | U32 const mls, |
249 | | const ZSTD_dictMode_e dictMode) |
250 | 0 | { |
251 | 0 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
252 | 0 | U32* const hashTable = ms->hashTable; |
253 | 0 | U32 const hashLog = cParams->hashLog; |
254 | 0 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
255 | 0 | U32 matchIndex = hashTable[h]; |
256 | |
|
257 | 0 | const BYTE* const base = ms->window.base; |
258 | 0 | U32 const curr = (U32)(ip-base); |
259 | 0 | U32 const windowLow = ZSTD_getLowestMatchIndex(ms, curr, cParams->windowLog); |
260 | |
|
261 | 0 | U32* const bt = ms->chainTable; |
262 | 0 | U32 const btLog = cParams->chainLog - 1; |
263 | 0 | U32 const btMask = (1 << btLog) - 1; |
264 | 0 | U32 const btLow = (btMask >= curr) ? 0 : curr - btMask; |
265 | 0 | U32 const unsortLimit = MAX(btLow, windowLow); |
266 | |
|
267 | 0 | U32* nextCandidate = bt + 2*(matchIndex&btMask); |
268 | 0 | U32* unsortedMark = bt + 2*(matchIndex&btMask) + 1; |
269 | 0 | U32 nbCompares = 1U << cParams->searchLog; |
270 | 0 | U32 nbCandidates = nbCompares; |
271 | 0 | U32 previousCandidate = 0; |
272 | |
|
273 | 0 | DEBUGLOG(7, "ZSTD_DUBT_findBestMatch (%u) ", curr); |
274 | 0 | assert(ip <= iend-8); /* required for h calculation */ |
275 | 0 | assert(dictMode != ZSTD_dedicatedDictSearch); |
276 | | |
277 | | /* reach end of unsorted candidates list */ |
278 | 0 | while ( (matchIndex > unsortLimit) |
279 | 0 | && (*unsortedMark == ZSTD_DUBT_UNSORTED_MARK) |
280 | 0 | && (nbCandidates > 1) ) { |
281 | 0 | DEBUGLOG(8, "ZSTD_DUBT_findBestMatch: candidate %u is unsorted", |
282 | 0 | matchIndex); |
283 | 0 | *unsortedMark = previousCandidate; /* the unsortedMark becomes a reversed chain, to move up back to original position */ |
284 | 0 | previousCandidate = matchIndex; |
285 | 0 | matchIndex = *nextCandidate; |
286 | 0 | nextCandidate = bt + 2*(matchIndex&btMask); |
287 | 0 | unsortedMark = bt + 2*(matchIndex&btMask) + 1; |
288 | 0 | nbCandidates --; |
289 | 0 | } |
290 | | |
291 | | /* nullify last candidate if it's still unsorted |
292 | | * simplification, detrimental to compression ratio, beneficial for speed */ |
293 | 0 | if ( (matchIndex > unsortLimit) |
294 | 0 | && (*unsortedMark==ZSTD_DUBT_UNSORTED_MARK) ) { |
295 | 0 | DEBUGLOG(7, "ZSTD_DUBT_findBestMatch: nullify last unsorted candidate %u", |
296 | 0 | matchIndex); |
297 | 0 | *nextCandidate = *unsortedMark = 0; |
298 | 0 | } |
299 | | |
300 | | /* batch sort stacked candidates */ |
301 | 0 | matchIndex = previousCandidate; |
302 | 0 | while (matchIndex) { /* will end on matchIndex == 0 */ |
303 | 0 | U32* const nextCandidateIdxPtr = bt + 2*(matchIndex&btMask) + 1; |
304 | 0 | U32 const nextCandidateIdx = *nextCandidateIdxPtr; |
305 | 0 | ZSTD_insertDUBT1(ms, matchIndex, iend, |
306 | 0 | nbCandidates, unsortLimit, dictMode); |
307 | 0 | matchIndex = nextCandidateIdx; |
308 | 0 | nbCandidates++; |
309 | 0 | } |
310 | | |
311 | | /* find longest match */ |
312 | 0 | { size_t commonLengthSmaller = 0, commonLengthLarger = 0; |
313 | 0 | const BYTE* const dictBase = ms->window.dictBase; |
314 | 0 | const U32 dictLimit = ms->window.dictLimit; |
315 | 0 | const BYTE* const dictEnd = dictBase + dictLimit; |
316 | 0 | const BYTE* const prefixStart = base + dictLimit; |
317 | 0 | U32* smallerPtr = bt + 2*(curr&btMask); |
318 | 0 | U32* largerPtr = bt + 2*(curr&btMask) + 1; |
319 | 0 | U32 matchEndIdx = curr + 8 + 1; |
320 | 0 | U32 dummy32; /* to be nullified at the end */ |
321 | 0 | size_t bestLength = 0; |
322 | |
|
323 | 0 | matchIndex = hashTable[h]; |
324 | 0 | hashTable[h] = curr; /* Update Hash Table */ |
325 | |
|
326 | 0 | for (; nbCompares && (matchIndex > windowLow); --nbCompares) { |
327 | 0 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
328 | 0 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
329 | 0 | const BYTE* match; |
330 | |
|
331 | 0 | if ((dictMode != ZSTD_extDict) || (matchIndex+matchLength >= dictLimit)) { |
332 | 0 | match = base + matchIndex; |
333 | 0 | matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend); |
334 | 0 | } else { |
335 | 0 | match = dictBase + matchIndex; |
336 | 0 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
337 | 0 | if (matchIndex+matchLength >= dictLimit) |
338 | 0 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
339 | 0 | } |
340 | |
|
341 | 0 | if (matchLength > bestLength) { |
342 | 0 | if (matchLength > matchEndIdx - matchIndex) |
343 | 0 | matchEndIdx = matchIndex + (U32)matchLength; |
344 | 0 | if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(curr - matchIndex + 1) - ZSTD_highbit32((U32)*offBasePtr)) ) |
345 | 0 | bestLength = matchLength, *offBasePtr = OFFSET_TO_OFFBASE(curr - matchIndex); |
346 | 0 | if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */ |
347 | 0 | if (dictMode == ZSTD_dictMatchState) { |
348 | 0 | nbCompares = 0; /* in addition to avoiding checking any |
349 | | * further in this loop, make sure we |
350 | | * skip checking in the dictionary. */ |
351 | 0 | } |
352 | 0 | break; /* drop, to guarantee consistency (miss a little bit of compression) */ |
353 | 0 | } |
354 | 0 | } |
355 | | |
356 | 0 | if (match[matchLength] < ip[matchLength]) { |
357 | | /* match is smaller than current */ |
358 | 0 | *smallerPtr = matchIndex; /* update smaller idx */ |
359 | 0 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
360 | 0 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
361 | 0 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
362 | 0 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
363 | 0 | } else { |
364 | | /* match is larger than current */ |
365 | 0 | *largerPtr = matchIndex; |
366 | 0 | commonLengthLarger = matchLength; |
367 | 0 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
368 | 0 | largerPtr = nextPtr; |
369 | 0 | matchIndex = nextPtr[0]; |
370 | 0 | } } |
371 | |
|
372 | 0 | *smallerPtr = *largerPtr = 0; |
373 | |
|
374 | 0 | assert(nbCompares <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */ |
375 | 0 | if (dictMode == ZSTD_dictMatchState && nbCompares) { |
376 | 0 | bestLength = ZSTD_DUBT_findBetterDictMatch( |
377 | 0 | ms, ip, iend, |
378 | 0 | offBasePtr, bestLength, nbCompares, |
379 | 0 | mls, dictMode); |
380 | 0 | } |
381 | |
|
382 | 0 | assert(matchEndIdx > curr+8); /* ensure nextToUpdate is increased */ |
383 | 0 | ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */ |
384 | 0 | if (bestLength >= MINMATCH) { |
385 | 0 | U32 const mIndex = curr - (U32)OFFBASE_TO_OFFSET(*offBasePtr); (void)mIndex; |
386 | 0 | DEBUGLOG(8, "ZSTD_DUBT_findBestMatch(%u) : found match of length %u and offsetCode %u (pos %u)", |
387 | 0 | curr, (U32)bestLength, (U32)*offBasePtr, mIndex); |
388 | 0 | } |
389 | 0 | return bestLength; |
390 | 0 | } |
391 | 0 | } |
392 | | |
393 | | |
394 | | /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */ |
395 | | FORCE_INLINE_TEMPLATE |
396 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
397 | | size_t ZSTD_BtFindBestMatch( ZSTD_matchState_t* ms, |
398 | | const BYTE* const ip, const BYTE* const iLimit, |
399 | | size_t* offBasePtr, |
400 | | const U32 mls /* template */, |
401 | | const ZSTD_dictMode_e dictMode) |
402 | 0 | { |
403 | 0 | DEBUGLOG(7, "ZSTD_BtFindBestMatch"); |
404 | 0 | if (ip < ms->window.base + ms->nextToUpdate) return 0; /* skipped area */ |
405 | 0 | ZSTD_updateDUBT(ms, ip, iLimit, mls); |
406 | 0 | return ZSTD_DUBT_findBestMatch(ms, ip, iLimit, offBasePtr, mls, dictMode); |
407 | 0 | } |
408 | | |
409 | | /*********************************** |
410 | | * Dedicated dict search |
411 | | ***********************************/ |
412 | | |
413 | | void ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_matchState_t* ms, const BYTE* const ip) |
414 | 0 | { |
415 | 0 | const BYTE* const base = ms->window.base; |
416 | 0 | U32 const target = (U32)(ip - base); |
417 | 0 | U32* const hashTable = ms->hashTable; |
418 | 0 | U32* const chainTable = ms->chainTable; |
419 | 0 | U32 const chainSize = 1 << ms->cParams.chainLog; |
420 | 0 | U32 idx = ms->nextToUpdate; |
421 | 0 | U32 const minChain = chainSize < target - idx ? target - chainSize : idx; |
422 | 0 | U32 const bucketSize = 1 << ZSTD_LAZY_DDSS_BUCKET_LOG; |
423 | 0 | U32 const cacheSize = bucketSize - 1; |
424 | 0 | U32 const chainAttempts = (1 << ms->cParams.searchLog) - cacheSize; |
425 | 0 | U32 const chainLimit = chainAttempts > 255 ? 255 : chainAttempts; |
426 | | |
427 | | /* We know the hashtable is oversized by a factor of `bucketSize`. |
428 | | * We are going to temporarily pretend `bucketSize == 1`, keeping only a |
429 | | * single entry. We will use the rest of the space to construct a temporary |
430 | | * chaintable. |
431 | | */ |
432 | 0 | U32 const hashLog = ms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG; |
433 | 0 | U32* const tmpHashTable = hashTable; |
434 | 0 | U32* const tmpChainTable = hashTable + ((size_t)1 << hashLog); |
435 | 0 | U32 const tmpChainSize = (U32)((1 << ZSTD_LAZY_DDSS_BUCKET_LOG) - 1) << hashLog; |
436 | 0 | U32 const tmpMinChain = tmpChainSize < target ? target - tmpChainSize : idx; |
437 | 0 | U32 hashIdx; |
438 | |
|
439 | 0 | assert(ms->cParams.chainLog <= 24); |
440 | 0 | assert(ms->cParams.hashLog > ms->cParams.chainLog); |
441 | 0 | assert(idx != 0); |
442 | 0 | assert(tmpMinChain <= minChain); |
443 | | |
444 | | /* fill conventional hash table and conventional chain table */ |
445 | 0 | for ( ; idx < target; idx++) { |
446 | 0 | U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch); |
447 | 0 | if (idx >= tmpMinChain) { |
448 | 0 | tmpChainTable[idx - tmpMinChain] = hashTable[h]; |
449 | 0 | } |
450 | 0 | tmpHashTable[h] = idx; |
451 | 0 | } |
452 | | |
453 | | /* sort chains into ddss chain table */ |
454 | 0 | { |
455 | 0 | U32 chainPos = 0; |
456 | 0 | for (hashIdx = 0; hashIdx < (1U << hashLog); hashIdx++) { |
457 | 0 | U32 count; |
458 | 0 | U32 countBeyondMinChain = 0; |
459 | 0 | U32 i = tmpHashTable[hashIdx]; |
460 | 0 | for (count = 0; i >= tmpMinChain && count < cacheSize; count++) { |
461 | | /* skip through the chain to the first position that won't be |
462 | | * in the hash cache bucket */ |
463 | 0 | if (i < minChain) { |
464 | 0 | countBeyondMinChain++; |
465 | 0 | } |
466 | 0 | i = tmpChainTable[i - tmpMinChain]; |
467 | 0 | } |
468 | 0 | if (count == cacheSize) { |
469 | 0 | for (count = 0; count < chainLimit;) { |
470 | 0 | if (i < minChain) { |
471 | 0 | if (!i || ++countBeyondMinChain > cacheSize) { |
472 | | /* only allow pulling `cacheSize` number of entries |
473 | | * into the cache or chainTable beyond `minChain`, |
474 | | * to replace the entries pulled out of the |
475 | | * chainTable into the cache. This lets us reach |
476 | | * back further without increasing the total number |
477 | | * of entries in the chainTable, guaranteeing the |
478 | | * DDSS chain table will fit into the space |
479 | | * allocated for the regular one. */ |
480 | 0 | break; |
481 | 0 | } |
482 | 0 | } |
483 | 0 | chainTable[chainPos++] = i; |
484 | 0 | count++; |
485 | 0 | if (i < tmpMinChain) { |
486 | 0 | break; |
487 | 0 | } |
488 | 0 | i = tmpChainTable[i - tmpMinChain]; |
489 | 0 | } |
490 | 0 | } else { |
491 | 0 | count = 0; |
492 | 0 | } |
493 | 0 | if (count) { |
494 | 0 | tmpHashTable[hashIdx] = ((chainPos - count) << 8) + count; |
495 | 0 | } else { |
496 | 0 | tmpHashTable[hashIdx] = 0; |
497 | 0 | } |
498 | 0 | } |
499 | 0 | assert(chainPos <= chainSize); /* I believe this is guaranteed... */ |
500 | 0 | } |
501 | | |
502 | | /* move chain pointers into the last entry of each hash bucket */ |
503 | 0 | for (hashIdx = (1 << hashLog); hashIdx; ) { |
504 | 0 | U32 const bucketIdx = --hashIdx << ZSTD_LAZY_DDSS_BUCKET_LOG; |
505 | 0 | U32 const chainPackedPointer = tmpHashTable[hashIdx]; |
506 | 0 | U32 i; |
507 | 0 | for (i = 0; i < cacheSize; i++) { |
508 | 0 | hashTable[bucketIdx + i] = 0; |
509 | 0 | } |
510 | 0 | hashTable[bucketIdx + bucketSize - 1] = chainPackedPointer; |
511 | 0 | } |
512 | | |
513 | | /* fill the buckets of the hash table */ |
514 | 0 | for (idx = ms->nextToUpdate; idx < target; idx++) { |
515 | 0 | U32 const h = (U32)ZSTD_hashPtr(base + idx, hashLog, ms->cParams.minMatch) |
516 | 0 | << ZSTD_LAZY_DDSS_BUCKET_LOG; |
517 | 0 | U32 i; |
518 | | /* Shift hash cache down 1. */ |
519 | 0 | for (i = cacheSize - 1; i; i--) |
520 | 0 | hashTable[h + i] = hashTable[h + i - 1]; |
521 | 0 | hashTable[h] = idx; |
522 | 0 | } |
523 | |
|
524 | 0 | ms->nextToUpdate = target; |
525 | 0 | } |
526 | | |
527 | | /* Returns the longest match length found in the dedicated dict search structure. |
528 | | * If none are longer than the argument ml, then ml will be returned. |
529 | | */ |
530 | | FORCE_INLINE_TEMPLATE |
531 | | size_t ZSTD_dedicatedDictSearch_lazy_search(size_t* offsetPtr, size_t ml, U32 nbAttempts, |
532 | | const ZSTD_matchState_t* const dms, |
533 | | const BYTE* const ip, const BYTE* const iLimit, |
534 | | const BYTE* const prefixStart, const U32 curr, |
535 | 0 | const U32 dictLimit, const size_t ddsIdx) { |
536 | 0 | const U32 ddsLowestIndex = dms->window.dictLimit; |
537 | 0 | const BYTE* const ddsBase = dms->window.base; |
538 | 0 | const BYTE* const ddsEnd = dms->window.nextSrc; |
539 | 0 | const U32 ddsSize = (U32)(ddsEnd - ddsBase); |
540 | 0 | const U32 ddsIndexDelta = dictLimit - ddsSize; |
541 | 0 | const U32 bucketSize = (1 << ZSTD_LAZY_DDSS_BUCKET_LOG); |
542 | 0 | const U32 bucketLimit = nbAttempts < bucketSize - 1 ? nbAttempts : bucketSize - 1; |
543 | 0 | U32 ddsAttempt; |
544 | 0 | U32 matchIndex; |
545 | |
|
546 | 0 | for (ddsAttempt = 0; ddsAttempt < bucketSize - 1; ddsAttempt++) { |
547 | 0 | PREFETCH_L1(ddsBase + dms->hashTable[ddsIdx + ddsAttempt]); |
548 | 0 | } |
549 | |
|
550 | 0 | { |
551 | 0 | U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1]; |
552 | 0 | U32 const chainIndex = chainPackedPointer >> 8; |
553 | |
|
554 | 0 | PREFETCH_L1(&dms->chainTable[chainIndex]); |
555 | 0 | } |
556 | |
|
557 | 0 | for (ddsAttempt = 0; ddsAttempt < bucketLimit; ddsAttempt++) { |
558 | 0 | size_t currentMl=0; |
559 | 0 | const BYTE* match; |
560 | 0 | matchIndex = dms->hashTable[ddsIdx + ddsAttempt]; |
561 | 0 | match = ddsBase + matchIndex; |
562 | |
|
563 | 0 | if (!matchIndex) { |
564 | 0 | return ml; |
565 | 0 | } |
566 | | |
567 | | /* guaranteed by table construction */ |
568 | 0 | (void)ddsLowestIndex; |
569 | 0 | assert(matchIndex >= ddsLowestIndex); |
570 | 0 | assert(match+4 <= ddsEnd); |
571 | 0 | if (MEM_read32(match) == MEM_read32(ip)) { |
572 | | /* assumption : matchIndex <= dictLimit-4 (by table construction) */ |
573 | 0 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4; |
574 | 0 | } |
575 | | |
576 | | /* save best solution */ |
577 | 0 | if (currentMl > ml) { |
578 | 0 | ml = currentMl; |
579 | 0 | *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + ddsIndexDelta)); |
580 | 0 | if (ip+currentMl == iLimit) { |
581 | | /* best possible, avoids read overflow on next attempt */ |
582 | 0 | return ml; |
583 | 0 | } |
584 | 0 | } |
585 | 0 | } |
586 | | |
587 | 0 | { |
588 | 0 | U32 const chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1]; |
589 | 0 | U32 chainIndex = chainPackedPointer >> 8; |
590 | 0 | U32 const chainLength = chainPackedPointer & 0xFF; |
591 | 0 | U32 const chainAttempts = nbAttempts - ddsAttempt; |
592 | 0 | U32 const chainLimit = chainAttempts > chainLength ? chainLength : chainAttempts; |
593 | 0 | U32 chainAttempt; |
594 | |
|
595 | 0 | for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++) { |
596 | 0 | PREFETCH_L1(ddsBase + dms->chainTable[chainIndex + chainAttempt]); |
597 | 0 | } |
598 | |
|
599 | 0 | for (chainAttempt = 0 ; chainAttempt < chainLimit; chainAttempt++, chainIndex++) { |
600 | 0 | size_t currentMl=0; |
601 | 0 | const BYTE* match; |
602 | 0 | matchIndex = dms->chainTable[chainIndex]; |
603 | 0 | match = ddsBase + matchIndex; |
604 | | |
605 | | /* guaranteed by table construction */ |
606 | 0 | assert(matchIndex >= ddsLowestIndex); |
607 | 0 | assert(match+4 <= ddsEnd); |
608 | 0 | if (MEM_read32(match) == MEM_read32(ip)) { |
609 | | /* assumption : matchIndex <= dictLimit-4 (by table construction) */ |
610 | 0 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, ddsEnd, prefixStart) + 4; |
611 | 0 | } |
612 | | |
613 | | /* save best solution */ |
614 | 0 | if (currentMl > ml) { |
615 | 0 | ml = currentMl; |
616 | 0 | *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + ddsIndexDelta)); |
617 | 0 | if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */ |
618 | 0 | } |
619 | 0 | } |
620 | 0 | } |
621 | 0 | return ml; |
622 | 0 | } |
623 | | |
624 | | |
625 | | /* ********************************* |
626 | | * Hash Chain |
627 | | ***********************************/ |
628 | 0 | #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & (mask)] |
629 | | |
630 | | /* Update chains up to ip (excluded) |
631 | | Assumption : always within prefix (i.e. not within extDict) */ |
632 | | FORCE_INLINE_TEMPLATE |
633 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
634 | | U32 ZSTD_insertAndFindFirstIndex_internal( |
635 | | ZSTD_matchState_t* ms, |
636 | | const ZSTD_compressionParameters* const cParams, |
637 | | const BYTE* ip, U32 const mls, U32 const lazySkipping) |
638 | 0 | { |
639 | 0 | U32* const hashTable = ms->hashTable; |
640 | 0 | const U32 hashLog = cParams->hashLog; |
641 | 0 | U32* const chainTable = ms->chainTable; |
642 | 0 | const U32 chainMask = (1 << cParams->chainLog) - 1; |
643 | 0 | const BYTE* const base = ms->window.base; |
644 | 0 | const U32 target = (U32)(ip - base); |
645 | 0 | U32 idx = ms->nextToUpdate; |
646 | |
|
647 | 0 | while(idx < target) { /* catch up */ |
648 | 0 | size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls); |
649 | 0 | NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; |
650 | 0 | hashTable[h] = idx; |
651 | 0 | idx++; |
652 | | /* Stop inserting every position when in the lazy skipping mode. */ |
653 | 0 | if (lazySkipping) |
654 | 0 | break; |
655 | 0 | } |
656 | |
|
657 | 0 | ms->nextToUpdate = target; |
658 | 0 | return hashTable[ZSTD_hashPtr(ip, hashLog, mls)]; |
659 | 0 | } |
660 | | |
661 | 0 | U32 ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t* ms, const BYTE* ip) { |
662 | 0 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
663 | 0 | return ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, ms->cParams.minMatch, /* lazySkipping*/ 0); |
664 | 0 | } |
665 | | |
666 | | /* inlining is important to hardwire a hot branch (template emulation) */ |
667 | | FORCE_INLINE_TEMPLATE |
668 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
669 | | size_t ZSTD_HcFindBestMatch( |
670 | | ZSTD_matchState_t* ms, |
671 | | const BYTE* const ip, const BYTE* const iLimit, |
672 | | size_t* offsetPtr, |
673 | | const U32 mls, const ZSTD_dictMode_e dictMode) |
674 | 0 | { |
675 | 0 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
676 | 0 | U32* const chainTable = ms->chainTable; |
677 | 0 | const U32 chainSize = (1 << cParams->chainLog); |
678 | 0 | const U32 chainMask = chainSize-1; |
679 | 0 | const BYTE* const base = ms->window.base; |
680 | 0 | const BYTE* const dictBase = ms->window.dictBase; |
681 | 0 | const U32 dictLimit = ms->window.dictLimit; |
682 | 0 | const BYTE* const prefixStart = base + dictLimit; |
683 | 0 | const BYTE* const dictEnd = dictBase + dictLimit; |
684 | 0 | const U32 curr = (U32)(ip-base); |
685 | 0 | const U32 maxDistance = 1U << cParams->windowLog; |
686 | 0 | const U32 lowestValid = ms->window.lowLimit; |
687 | 0 | const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid; |
688 | 0 | const U32 isDictionary = (ms->loadedDictEnd != 0); |
689 | 0 | const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance; |
690 | 0 | const U32 minChain = curr > chainSize ? curr - chainSize : 0; |
691 | 0 | U32 nbAttempts = 1U << cParams->searchLog; |
692 | 0 | size_t ml=4-1; |
693 | |
|
694 | 0 | const ZSTD_matchState_t* const dms = ms->dictMatchState; |
695 | 0 | const U32 ddsHashLog = dictMode == ZSTD_dedicatedDictSearch |
696 | 0 | ? dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG : 0; |
697 | 0 | const size_t ddsIdx = dictMode == ZSTD_dedicatedDictSearch |
698 | 0 | ? ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG : 0; |
699 | |
|
700 | 0 | U32 matchIndex; |
701 | |
|
702 | 0 | if (dictMode == ZSTD_dedicatedDictSearch) { |
703 | 0 | const U32* entry = &dms->hashTable[ddsIdx]; |
704 | 0 | PREFETCH_L1(entry); |
705 | 0 | } |
706 | | |
707 | | /* HC4 match finder */ |
708 | 0 | matchIndex = ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, mls, ms->lazySkipping); |
709 | |
|
710 | 0 | for ( ; (matchIndex>=lowLimit) & (nbAttempts>0) ; nbAttempts--) { |
711 | 0 | size_t currentMl=0; |
712 | 0 | if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) { |
713 | 0 | const BYTE* const match = base + matchIndex; |
714 | 0 | assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */ |
715 | | /* read 4B starting from (match + ml + 1 - sizeof(U32)) */ |
716 | 0 | if (MEM_read32(match + ml - 3) == MEM_read32(ip + ml - 3)) /* potentially better */ |
717 | 0 | currentMl = ZSTD_count(ip, match, iLimit); |
718 | 0 | } else { |
719 | 0 | const BYTE* const match = dictBase + matchIndex; |
720 | 0 | assert(match+4 <= dictEnd); |
721 | 0 | if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */ |
722 | 0 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4; |
723 | 0 | } |
724 | | |
725 | | /* save best solution */ |
726 | 0 | if (currentMl > ml) { |
727 | 0 | ml = currentMl; |
728 | 0 | *offsetPtr = OFFSET_TO_OFFBASE(curr - matchIndex); |
729 | 0 | if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */ |
730 | 0 | } |
731 | | |
732 | 0 | if (matchIndex <= minChain) break; |
733 | 0 | matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask); |
734 | 0 | } |
735 | |
|
736 | 0 | assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */ |
737 | 0 | if (dictMode == ZSTD_dedicatedDictSearch) { |
738 | 0 | ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts, dms, |
739 | 0 | ip, iLimit, prefixStart, curr, dictLimit, ddsIdx); |
740 | 0 | } else if (dictMode == ZSTD_dictMatchState) { |
741 | 0 | const U32* const dmsChainTable = dms->chainTable; |
742 | 0 | const U32 dmsChainSize = (1 << dms->cParams.chainLog); |
743 | 0 | const U32 dmsChainMask = dmsChainSize - 1; |
744 | 0 | const U32 dmsLowestIndex = dms->window.dictLimit; |
745 | 0 | const BYTE* const dmsBase = dms->window.base; |
746 | 0 | const BYTE* const dmsEnd = dms->window.nextSrc; |
747 | 0 | const U32 dmsSize = (U32)(dmsEnd - dmsBase); |
748 | 0 | const U32 dmsIndexDelta = dictLimit - dmsSize; |
749 | 0 | const U32 dmsMinChain = dmsSize > dmsChainSize ? dmsSize - dmsChainSize : 0; |
750 | |
|
751 | 0 | matchIndex = dms->hashTable[ZSTD_hashPtr(ip, dms->cParams.hashLog, mls)]; |
752 | |
|
753 | 0 | for ( ; (matchIndex>=dmsLowestIndex) & (nbAttempts>0) ; nbAttempts--) { |
754 | 0 | size_t currentMl=0; |
755 | 0 | const BYTE* const match = dmsBase + matchIndex; |
756 | 0 | assert(match+4 <= dmsEnd); |
757 | 0 | if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */ |
758 | 0 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4; |
759 | | |
760 | | /* save best solution */ |
761 | 0 | if (currentMl > ml) { |
762 | 0 | ml = currentMl; |
763 | 0 | assert(curr > matchIndex + dmsIndexDelta); |
764 | 0 | *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + dmsIndexDelta)); |
765 | 0 | if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */ |
766 | 0 | } |
767 | | |
768 | 0 | if (matchIndex <= dmsMinChain) break; |
769 | | |
770 | 0 | matchIndex = dmsChainTable[matchIndex & dmsChainMask]; |
771 | 0 | } |
772 | 0 | } |
773 | |
|
774 | 0 | return ml; |
775 | 0 | } |
776 | | |
777 | | /* ********************************* |
778 | | * (SIMD) Row-based matchfinder |
779 | | ***********************************/ |
780 | | /* Constants for row-based hash */ |
781 | 0 | #define ZSTD_ROW_HASH_TAG_MASK ((1u << ZSTD_ROW_HASH_TAG_BITS) - 1) |
782 | | #define ZSTD_ROW_HASH_MAX_ENTRIES 64 /* absolute maximum number of entries per row, for all configurations */ |
783 | | |
784 | 0 | #define ZSTD_ROW_HASH_CACHE_MASK (ZSTD_ROW_HASH_CACHE_SIZE - 1) |
785 | | |
786 | | typedef U64 ZSTD_VecMask; /* Clarifies when we are interacting with a U64 representing a mask of matches */ |
787 | | |
788 | | /* ZSTD_VecMask_next(): |
789 | | * Starting from the LSB, returns the idx of the next non-zero bit. |
790 | | * Basically counting the nb of trailing zeroes. |
791 | | */ |
792 | 0 | MEM_STATIC U32 ZSTD_VecMask_next(ZSTD_VecMask val) { |
793 | 0 | return ZSTD_countTrailingZeros64(val); |
794 | 0 | } |
795 | | |
796 | | /* ZSTD_row_nextIndex(): |
797 | | * Returns the next index to insert at within a tagTable row, and updates the "head" |
798 | | * value to reflect the update. Essentially cycles backwards from [1, {entries per row}) |
799 | | */ |
800 | 0 | FORCE_INLINE_TEMPLATE U32 ZSTD_row_nextIndex(BYTE* const tagRow, U32 const rowMask) { |
801 | 0 | U32 next = (*tagRow-1) & rowMask; |
802 | 0 | next += (next == 0) ? rowMask : 0; /* skip first position */ |
803 | 0 | *tagRow = (BYTE)next; |
804 | 0 | return next; |
805 | 0 | } |
806 | | |
807 | | /* ZSTD_isAligned(): |
808 | | * Checks that a pointer is aligned to "align" bytes which must be a power of 2. |
809 | | */ |
810 | 0 | MEM_STATIC int ZSTD_isAligned(void const* ptr, size_t align) { |
811 | 0 | assert((align & (align - 1)) == 0); |
812 | 0 | return (((size_t)ptr) & (align - 1)) == 0; |
813 | 0 | } |
814 | | |
815 | | /* ZSTD_row_prefetch(): |
816 | | * Performs prefetching for the hashTable and tagTable at a given row. |
817 | | */ |
818 | 0 | FORCE_INLINE_TEMPLATE void ZSTD_row_prefetch(U32 const* hashTable, BYTE const* tagTable, U32 const relRow, U32 const rowLog) { |
819 | 0 | PREFETCH_L1(hashTable + relRow); |
820 | 0 | if (rowLog >= 5) { |
821 | 0 | PREFETCH_L1(hashTable + relRow + 16); |
822 | | /* Note: prefetching more of the hash table does not appear to be beneficial for 128-entry rows */ |
823 | 0 | } |
824 | 0 | PREFETCH_L1(tagTable + relRow); |
825 | 0 | if (rowLog == 6) { |
826 | 0 | PREFETCH_L1(tagTable + relRow + 32); |
827 | 0 | } |
828 | 0 | assert(rowLog == 4 || rowLog == 5 || rowLog == 6); |
829 | 0 | assert(ZSTD_isAligned(hashTable + relRow, 64)); /* prefetched hash row always 64-byte aligned */ |
830 | 0 | assert(ZSTD_isAligned(tagTable + relRow, (size_t)1 << rowLog)); /* prefetched tagRow sits on correct multiple of bytes (32,64,128) */ |
831 | 0 | } |
832 | | |
833 | | /* ZSTD_row_fillHashCache(): |
834 | | * Fill up the hash cache starting at idx, prefetching up to ZSTD_ROW_HASH_CACHE_SIZE entries, |
835 | | * but not beyond iLimit. |
836 | | */ |
837 | | FORCE_INLINE_TEMPLATE |
838 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
839 | | void ZSTD_row_fillHashCache(ZSTD_matchState_t* ms, const BYTE* base, |
840 | | U32 const rowLog, U32 const mls, |
841 | | U32 idx, const BYTE* const iLimit) |
842 | 0 | { |
843 | 0 | U32 const* const hashTable = ms->hashTable; |
844 | 0 | BYTE const* const tagTable = ms->tagTable; |
845 | 0 | U32 const hashLog = ms->rowHashLog; |
846 | 0 | U32 const maxElemsToPrefetch = (base + idx) > iLimit ? 0 : (U32)(iLimit - (base + idx) + 1); |
847 | 0 | U32 const lim = idx + MIN(ZSTD_ROW_HASH_CACHE_SIZE, maxElemsToPrefetch); |
848 | |
|
849 | 0 | for (; idx < lim; ++idx) { |
850 | 0 | U32 const hash = (U32)ZSTD_hashPtrSalted(base + idx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, ms->hashSalt); |
851 | 0 | U32 const row = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog; |
852 | 0 | ZSTD_row_prefetch(hashTable, tagTable, row, rowLog); |
853 | 0 | ms->hashCache[idx & ZSTD_ROW_HASH_CACHE_MASK] = hash; |
854 | 0 | } |
855 | |
|
856 | 0 | DEBUGLOG(6, "ZSTD_row_fillHashCache(): [%u %u %u %u %u %u %u %u]", ms->hashCache[0], ms->hashCache[1], |
857 | 0 | ms->hashCache[2], ms->hashCache[3], ms->hashCache[4], |
858 | 0 | ms->hashCache[5], ms->hashCache[6], ms->hashCache[7]); |
859 | 0 | } |
860 | | |
861 | | /* ZSTD_row_nextCachedHash(): |
862 | | * Returns the hash of base + idx, and replaces the hash in the hash cache with the byte at |
863 | | * base + idx + ZSTD_ROW_HASH_CACHE_SIZE. Also prefetches the appropriate rows from hashTable and tagTable. |
864 | | */ |
865 | | FORCE_INLINE_TEMPLATE |
866 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
867 | | U32 ZSTD_row_nextCachedHash(U32* cache, U32 const* hashTable, |
868 | | BYTE const* tagTable, BYTE const* base, |
869 | | U32 idx, U32 const hashLog, |
870 | | U32 const rowLog, U32 const mls, |
871 | | U64 const hashSalt) |
872 | 0 | { |
873 | 0 | U32 const newHash = (U32)ZSTD_hashPtrSalted(base+idx+ZSTD_ROW_HASH_CACHE_SIZE, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, hashSalt); |
874 | 0 | U32 const row = (newHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog; |
875 | 0 | ZSTD_row_prefetch(hashTable, tagTable, row, rowLog); |
876 | 0 | { U32 const hash = cache[idx & ZSTD_ROW_HASH_CACHE_MASK]; |
877 | 0 | cache[idx & ZSTD_ROW_HASH_CACHE_MASK] = newHash; |
878 | 0 | return hash; |
879 | 0 | } |
880 | 0 | } |
881 | | |
882 | | /* ZSTD_row_update_internalImpl(): |
883 | | * Updates the hash table with positions starting from updateStartIdx until updateEndIdx. |
884 | | */ |
885 | | FORCE_INLINE_TEMPLATE |
886 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
887 | | void ZSTD_row_update_internalImpl(ZSTD_matchState_t* ms, |
888 | | U32 updateStartIdx, U32 const updateEndIdx, |
889 | | U32 const mls, U32 const rowLog, |
890 | | U32 const rowMask, U32 const useCache) |
891 | 0 | { |
892 | 0 | U32* const hashTable = ms->hashTable; |
893 | 0 | BYTE* const tagTable = ms->tagTable; |
894 | 0 | U32 const hashLog = ms->rowHashLog; |
895 | 0 | const BYTE* const base = ms->window.base; |
896 | |
|
897 | 0 | DEBUGLOG(6, "ZSTD_row_update_internalImpl(): updateStartIdx=%u, updateEndIdx=%u", updateStartIdx, updateEndIdx); |
898 | 0 | for (; updateStartIdx < updateEndIdx; ++updateStartIdx) { |
899 | 0 | U32 const hash = useCache ? ZSTD_row_nextCachedHash(ms->hashCache, hashTable, tagTable, base, updateStartIdx, hashLog, rowLog, mls, ms->hashSalt) |
900 | 0 | : (U32)ZSTD_hashPtrSalted(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, ms->hashSalt); |
901 | 0 | U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog; |
902 | 0 | U32* const row = hashTable + relRow; |
903 | 0 | BYTE* tagRow = tagTable + relRow; |
904 | 0 | U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask); |
905 | |
|
906 | 0 | assert(hash == ZSTD_hashPtrSalted(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, ms->hashSalt)); |
907 | 0 | tagRow[pos] = hash & ZSTD_ROW_HASH_TAG_MASK; |
908 | 0 | row[pos] = updateStartIdx; |
909 | 0 | } |
910 | 0 | } |
911 | | |
912 | | /* ZSTD_row_update_internal(): |
913 | | * Inserts the byte at ip into the appropriate position in the hash table, and updates ms->nextToUpdate. |
914 | | * Skips sections of long matches as is necessary. |
915 | | */ |
916 | | FORCE_INLINE_TEMPLATE |
917 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
918 | | void ZSTD_row_update_internal(ZSTD_matchState_t* ms, const BYTE* ip, |
919 | | U32 const mls, U32 const rowLog, |
920 | | U32 const rowMask, U32 const useCache) |
921 | 0 | { |
922 | 0 | U32 idx = ms->nextToUpdate; |
923 | 0 | const BYTE* const base = ms->window.base; |
924 | 0 | const U32 target = (U32)(ip - base); |
925 | 0 | const U32 kSkipThreshold = 384; |
926 | 0 | const U32 kMaxMatchStartPositionsToUpdate = 96; |
927 | 0 | const U32 kMaxMatchEndPositionsToUpdate = 32; |
928 | |
|
929 | 0 | if (useCache) { |
930 | | /* Only skip positions when using hash cache, i.e. |
931 | | * if we are loading a dict, don't skip anything. |
932 | | * If we decide to skip, then we only update a set number |
933 | | * of positions at the beginning and end of the match. |
934 | | */ |
935 | 0 | if (UNLIKELY(target - idx > kSkipThreshold)) { |
936 | 0 | U32 const bound = idx + kMaxMatchStartPositionsToUpdate; |
937 | 0 | ZSTD_row_update_internalImpl(ms, idx, bound, mls, rowLog, rowMask, useCache); |
938 | 0 | idx = target - kMaxMatchEndPositionsToUpdate; |
939 | 0 | ZSTD_row_fillHashCache(ms, base, rowLog, mls, idx, ip+1); |
940 | 0 | } |
941 | 0 | } |
942 | 0 | assert(target >= idx); |
943 | 0 | ZSTD_row_update_internalImpl(ms, idx, target, mls, rowLog, rowMask, useCache); |
944 | 0 | ms->nextToUpdate = target; |
945 | 0 | } |
946 | | |
947 | | /* ZSTD_row_update(): |
948 | | * External wrapper for ZSTD_row_update_internal(). Used for filling the hashtable during dictionary |
949 | | * processing. |
950 | | */ |
951 | 0 | void ZSTD_row_update(ZSTD_matchState_t* const ms, const BYTE* ip) { |
952 | 0 | const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6); |
953 | 0 | const U32 rowMask = (1u << rowLog) - 1; |
954 | 0 | const U32 mls = MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */); |
955 | |
|
956 | 0 | DEBUGLOG(5, "ZSTD_row_update(), rowLog=%u", rowLog); |
957 | 0 | ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 0 /* don't use cache */); |
958 | 0 | } |
959 | | |
960 | | /* Returns the mask width of bits group of which will be set to 1. Given not all |
961 | | * architectures have easy movemask instruction, this helps to iterate over |
962 | | * groups of bits easier and faster. |
963 | | */ |
964 | | FORCE_INLINE_TEMPLATE U32 |
965 | | ZSTD_row_matchMaskGroupWidth(const U32 rowEntries) |
966 | 0 | { |
967 | 0 | assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64); |
968 | 0 | assert(rowEntries <= ZSTD_ROW_HASH_MAX_ENTRIES); |
969 | 0 | (void)rowEntries; |
970 | | #if defined(ZSTD_ARCH_ARM_NEON) |
971 | | /* NEON path only works for little endian */ |
972 | | if (!MEM_isLittleEndian()) { |
973 | | return 1; |
974 | | } |
975 | | if (rowEntries == 16) { |
976 | | return 4; |
977 | | } |
978 | | if (rowEntries == 32) { |
979 | | return 2; |
980 | | } |
981 | | if (rowEntries == 64) { |
982 | | return 1; |
983 | | } |
984 | | #endif |
985 | 0 | return 1; |
986 | 0 | } |
987 | | |
988 | | #if defined(ZSTD_ARCH_X86_SSE2) |
989 | | FORCE_INLINE_TEMPLATE ZSTD_VecMask |
990 | | ZSTD_row_getSSEMask(int nbChunks, const BYTE* const src, const BYTE tag, const U32 head) |
991 | 0 | { |
992 | 0 | const __m128i comparisonMask = _mm_set1_epi8((char)tag); |
993 | 0 | int matches[4] = {0}; |
994 | 0 | int i; |
995 | 0 | assert(nbChunks == 1 || nbChunks == 2 || nbChunks == 4); |
996 | 0 | for (i=0; i<nbChunks; i++) { |
997 | 0 | const __m128i chunk = _mm_loadu_si128((const __m128i*)(const void*)(src + 16*i)); |
998 | 0 | const __m128i equalMask = _mm_cmpeq_epi8(chunk, comparisonMask); |
999 | 0 | matches[i] = _mm_movemask_epi8(equalMask); |
1000 | 0 | } |
1001 | 0 | if (nbChunks == 1) return ZSTD_rotateRight_U16((U16)matches[0], head); |
1002 | 0 | if (nbChunks == 2) return ZSTD_rotateRight_U32((U32)matches[1] << 16 | (U32)matches[0], head); |
1003 | 0 | assert(nbChunks == 4); |
1004 | 0 | return ZSTD_rotateRight_U64((U64)matches[3] << 48 | (U64)matches[2] << 32 | (U64)matches[1] << 16 | (U64)matches[0], head); |
1005 | 0 | } |
1006 | | #endif |
1007 | | |
1008 | | #if defined(ZSTD_ARCH_ARM_NEON) |
1009 | | FORCE_INLINE_TEMPLATE ZSTD_VecMask |
1010 | | ZSTD_row_getNEONMask(const U32 rowEntries, const BYTE* const src, const BYTE tag, const U32 headGrouped) |
1011 | | { |
1012 | | assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64); |
1013 | | if (rowEntries == 16) { |
1014 | | /* vshrn_n_u16 shifts by 4 every u16 and narrows to 8 lower bits. |
1015 | | * After that groups of 4 bits represent the equalMask. We lower |
1016 | | * all bits except the highest in these groups by doing AND with |
1017 | | * 0x88 = 0b10001000. |
1018 | | */ |
1019 | | const uint8x16_t chunk = vld1q_u8(src); |
1020 | | const uint16x8_t equalMask = vreinterpretq_u16_u8(vceqq_u8(chunk, vdupq_n_u8(tag))); |
1021 | | const uint8x8_t res = vshrn_n_u16(equalMask, 4); |
1022 | | const U64 matches = vget_lane_u64(vreinterpret_u64_u8(res), 0); |
1023 | | return ZSTD_rotateRight_U64(matches, headGrouped) & 0x8888888888888888ull; |
1024 | | } else if (rowEntries == 32) { |
1025 | | /* Same idea as with rowEntries == 16 but doing AND with |
1026 | | * 0x55 = 0b01010101. |
1027 | | */ |
1028 | | const uint16x8x2_t chunk = vld2q_u16((const uint16_t*)(const void*)src); |
1029 | | const uint8x16_t chunk0 = vreinterpretq_u8_u16(chunk.val[0]); |
1030 | | const uint8x16_t chunk1 = vreinterpretq_u8_u16(chunk.val[1]); |
1031 | | const uint8x16_t dup = vdupq_n_u8(tag); |
1032 | | const uint8x8_t t0 = vshrn_n_u16(vreinterpretq_u16_u8(vceqq_u8(chunk0, dup)), 6); |
1033 | | const uint8x8_t t1 = vshrn_n_u16(vreinterpretq_u16_u8(vceqq_u8(chunk1, dup)), 6); |
1034 | | const uint8x8_t res = vsli_n_u8(t0, t1, 4); |
1035 | | const U64 matches = vget_lane_u64(vreinterpret_u64_u8(res), 0) ; |
1036 | | return ZSTD_rotateRight_U64(matches, headGrouped) & 0x5555555555555555ull; |
1037 | | } else { /* rowEntries == 64 */ |
1038 | | const uint8x16x4_t chunk = vld4q_u8(src); |
1039 | | const uint8x16_t dup = vdupq_n_u8(tag); |
1040 | | const uint8x16_t cmp0 = vceqq_u8(chunk.val[0], dup); |
1041 | | const uint8x16_t cmp1 = vceqq_u8(chunk.val[1], dup); |
1042 | | const uint8x16_t cmp2 = vceqq_u8(chunk.val[2], dup); |
1043 | | const uint8x16_t cmp3 = vceqq_u8(chunk.val[3], dup); |
1044 | | |
1045 | | const uint8x16_t t0 = vsriq_n_u8(cmp1, cmp0, 1); |
1046 | | const uint8x16_t t1 = vsriq_n_u8(cmp3, cmp2, 1); |
1047 | | const uint8x16_t t2 = vsriq_n_u8(t1, t0, 2); |
1048 | | const uint8x16_t t3 = vsriq_n_u8(t2, t2, 4); |
1049 | | const uint8x8_t t4 = vshrn_n_u16(vreinterpretq_u16_u8(t3), 4); |
1050 | | const U64 matches = vget_lane_u64(vreinterpret_u64_u8(t4), 0); |
1051 | | return ZSTD_rotateRight_U64(matches, headGrouped); |
1052 | | } |
1053 | | } |
1054 | | #endif |
1055 | | |
1056 | | /* Returns a ZSTD_VecMask (U64) that has the nth group (determined by |
1057 | | * ZSTD_row_matchMaskGroupWidth) of bits set to 1 if the newly-computed "tag" |
1058 | | * matches the hash at the nth position in a row of the tagTable. |
1059 | | * Each row is a circular buffer beginning at the value of "headGrouped". So we |
1060 | | * must rotate the "matches" bitfield to match up with the actual layout of the |
1061 | | * entries within the hashTable */ |
1062 | | FORCE_INLINE_TEMPLATE ZSTD_VecMask |
1063 | | ZSTD_row_getMatchMask(const BYTE* const tagRow, const BYTE tag, const U32 headGrouped, const U32 rowEntries) |
1064 | 0 | { |
1065 | 0 | const BYTE* const src = tagRow; |
1066 | 0 | assert((rowEntries == 16) || (rowEntries == 32) || rowEntries == 64); |
1067 | 0 | assert(rowEntries <= ZSTD_ROW_HASH_MAX_ENTRIES); |
1068 | 0 | assert(ZSTD_row_matchMaskGroupWidth(rowEntries) * rowEntries <= sizeof(ZSTD_VecMask) * 8); |
1069 | |
|
1070 | 0 | #if defined(ZSTD_ARCH_X86_SSE2) |
1071 | |
|
1072 | 0 | return ZSTD_row_getSSEMask(rowEntries / 16, src, tag, headGrouped); |
1073 | |
|
1074 | | #else /* SW or NEON-LE */ |
1075 | | |
1076 | | # if defined(ZSTD_ARCH_ARM_NEON) |
1077 | | /* This NEON path only works for little endian - otherwise use SWAR below */ |
1078 | | if (MEM_isLittleEndian()) { |
1079 | | return ZSTD_row_getNEONMask(rowEntries, src, tag, headGrouped); |
1080 | | } |
1081 | | # endif /* ZSTD_ARCH_ARM_NEON */ |
1082 | | /* SWAR */ |
1083 | | { const int chunkSize = sizeof(size_t); |
1084 | | const size_t shiftAmount = ((chunkSize * 8) - chunkSize); |
1085 | | const size_t xFF = ~((size_t)0); |
1086 | | const size_t x01 = xFF / 0xFF; |
1087 | | const size_t x80 = x01 << 7; |
1088 | | const size_t splatChar = tag * x01; |
1089 | | ZSTD_VecMask matches = 0; |
1090 | | int i = rowEntries - chunkSize; |
1091 | | assert((sizeof(size_t) == 4) || (sizeof(size_t) == 8)); |
1092 | | if (MEM_isLittleEndian()) { /* runtime check so have two loops */ |
1093 | | const size_t extractMagic = (xFF / 0x7F) >> chunkSize; |
1094 | | do { |
1095 | | size_t chunk = MEM_readST(&src[i]); |
1096 | | chunk ^= splatChar; |
1097 | | chunk = (((chunk | x80) - x01) | chunk) & x80; |
1098 | | matches <<= chunkSize; |
1099 | | matches |= (chunk * extractMagic) >> shiftAmount; |
1100 | | i -= chunkSize; |
1101 | | } while (i >= 0); |
1102 | | } else { /* big endian: reverse bits during extraction */ |
1103 | | const size_t msb = xFF ^ (xFF >> 1); |
1104 | | const size_t extractMagic = (msb / 0x1FF) | msb; |
1105 | | do { |
1106 | | size_t chunk = MEM_readST(&src[i]); |
1107 | | chunk ^= splatChar; |
1108 | | chunk = (((chunk | x80) - x01) | chunk) & x80; |
1109 | | matches <<= chunkSize; |
1110 | | matches |= ((chunk >> 7) * extractMagic) >> shiftAmount; |
1111 | | i -= chunkSize; |
1112 | | } while (i >= 0); |
1113 | | } |
1114 | | matches = ~matches; |
1115 | | if (rowEntries == 16) { |
1116 | | return ZSTD_rotateRight_U16((U16)matches, headGrouped); |
1117 | | } else if (rowEntries == 32) { |
1118 | | return ZSTD_rotateRight_U32((U32)matches, headGrouped); |
1119 | | } else { |
1120 | | return ZSTD_rotateRight_U64((U64)matches, headGrouped); |
1121 | | } |
1122 | | } |
1123 | | #endif |
1124 | 0 | } |
1125 | | |
1126 | | /* The high-level approach of the SIMD row based match finder is as follows: |
1127 | | * - Figure out where to insert the new entry: |
1128 | | * - Generate a hash for current input posistion and split it into a one byte of tag and `rowHashLog` bits of index. |
1129 | | * - The hash is salted by a value that changes on every contex reset, so when the same table is used |
1130 | | * we will avoid collisions that would otherwise slow us down by intorducing phantom matches. |
1131 | | * - The hashTable is effectively split into groups or "rows" of 15 or 31 entries of U32, and the index determines |
1132 | | * which row to insert into. |
1133 | | * - Determine the correct position within the row to insert the entry into. Each row of 15 or 31 can |
1134 | | * be considered as a circular buffer with a "head" index that resides in the tagTable (overall 16 or 32 bytes |
1135 | | * per row). |
1136 | | * - Use SIMD to efficiently compare the tags in the tagTable to the 1-byte tag calculated for the position and |
1137 | | * generate a bitfield that we can cycle through to check the collisions in the hash table. |
1138 | | * - Pick the longest match. |
1139 | | * - Insert the tag into the equivalent row and position in the tagTable. |
1140 | | */ |
1141 | | FORCE_INLINE_TEMPLATE |
1142 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
1143 | | size_t ZSTD_RowFindBestMatch( |
1144 | | ZSTD_matchState_t* ms, |
1145 | | const BYTE* const ip, const BYTE* const iLimit, |
1146 | | size_t* offsetPtr, |
1147 | | const U32 mls, const ZSTD_dictMode_e dictMode, |
1148 | | const U32 rowLog) |
1149 | 0 | { |
1150 | 0 | U32* const hashTable = ms->hashTable; |
1151 | 0 | BYTE* const tagTable = ms->tagTable; |
1152 | 0 | U32* const hashCache = ms->hashCache; |
1153 | 0 | const U32 hashLog = ms->rowHashLog; |
1154 | 0 | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
1155 | 0 | const BYTE* const base = ms->window.base; |
1156 | 0 | const BYTE* const dictBase = ms->window.dictBase; |
1157 | 0 | const U32 dictLimit = ms->window.dictLimit; |
1158 | 0 | const BYTE* const prefixStart = base + dictLimit; |
1159 | 0 | const BYTE* const dictEnd = dictBase + dictLimit; |
1160 | 0 | const U32 curr = (U32)(ip-base); |
1161 | 0 | const U32 maxDistance = 1U << cParams->windowLog; |
1162 | 0 | const U32 lowestValid = ms->window.lowLimit; |
1163 | 0 | const U32 withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid; |
1164 | 0 | const U32 isDictionary = (ms->loadedDictEnd != 0); |
1165 | 0 | const U32 lowLimit = isDictionary ? lowestValid : withinMaxDistance; |
1166 | 0 | const U32 rowEntries = (1U << rowLog); |
1167 | 0 | const U32 rowMask = rowEntries - 1; |
1168 | 0 | const U32 cappedSearchLog = MIN(cParams->searchLog, rowLog); /* nb of searches is capped at nb entries per row */ |
1169 | 0 | const U32 groupWidth = ZSTD_row_matchMaskGroupWidth(rowEntries); |
1170 | 0 | const U64 hashSalt = ms->hashSalt; |
1171 | 0 | U32 nbAttempts = 1U << cappedSearchLog; |
1172 | 0 | size_t ml=4-1; |
1173 | 0 | U32 hash; |
1174 | | |
1175 | | /* DMS/DDS variables that may be referenced laster */ |
1176 | 0 | const ZSTD_matchState_t* const dms = ms->dictMatchState; |
1177 | | |
1178 | | /* Initialize the following variables to satisfy static analyzer */ |
1179 | 0 | size_t ddsIdx = 0; |
1180 | 0 | U32 ddsExtraAttempts = 0; /* cctx hash tables are limited in searches, but allow extra searches into DDS */ |
1181 | 0 | U32 dmsTag = 0; |
1182 | 0 | U32* dmsRow = NULL; |
1183 | 0 | BYTE* dmsTagRow = NULL; |
1184 | |
|
1185 | 0 | if (dictMode == ZSTD_dedicatedDictSearch) { |
1186 | 0 | const U32 ddsHashLog = dms->cParams.hashLog - ZSTD_LAZY_DDSS_BUCKET_LOG; |
1187 | 0 | { /* Prefetch DDS hashtable entry */ |
1188 | 0 | ddsIdx = ZSTD_hashPtr(ip, ddsHashLog, mls) << ZSTD_LAZY_DDSS_BUCKET_LOG; |
1189 | 0 | PREFETCH_L1(&dms->hashTable[ddsIdx]); |
1190 | 0 | } |
1191 | 0 | ddsExtraAttempts = cParams->searchLog > rowLog ? 1U << (cParams->searchLog - rowLog) : 0; |
1192 | 0 | } |
1193 | |
|
1194 | 0 | if (dictMode == ZSTD_dictMatchState) { |
1195 | | /* Prefetch DMS rows */ |
1196 | 0 | U32* const dmsHashTable = dms->hashTable; |
1197 | 0 | BYTE* const dmsTagTable = dms->tagTable; |
1198 | 0 | U32 const dmsHash = (U32)ZSTD_hashPtr(ip, dms->rowHashLog + ZSTD_ROW_HASH_TAG_BITS, mls); |
1199 | 0 | U32 const dmsRelRow = (dmsHash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog; |
1200 | 0 | dmsTag = dmsHash & ZSTD_ROW_HASH_TAG_MASK; |
1201 | 0 | dmsTagRow = (BYTE*)(dmsTagTable + dmsRelRow); |
1202 | 0 | dmsRow = dmsHashTable + dmsRelRow; |
1203 | 0 | ZSTD_row_prefetch(dmsHashTable, dmsTagTable, dmsRelRow, rowLog); |
1204 | 0 | } |
1205 | | |
1206 | | /* Update the hashTable and tagTable up to (but not including) ip */ |
1207 | 0 | if (!ms->lazySkipping) { |
1208 | 0 | ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 1 /* useCache */); |
1209 | 0 | hash = ZSTD_row_nextCachedHash(hashCache, hashTable, tagTable, base, curr, hashLog, rowLog, mls, hashSalt); |
1210 | 0 | } else { |
1211 | | /* Stop inserting every position when in the lazy skipping mode. |
1212 | | * The hash cache is also not kept up to date in this mode. |
1213 | | */ |
1214 | 0 | hash = (U32)ZSTD_hashPtrSalted(ip, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, hashSalt); |
1215 | 0 | ms->nextToUpdate = curr; |
1216 | 0 | } |
1217 | 0 | ms->hashSaltEntropy += hash; /* collect salt entropy */ |
1218 | |
|
1219 | 0 | { /* Get the hash for ip, compute the appropriate row */ |
1220 | 0 | U32 const relRow = (hash >> ZSTD_ROW_HASH_TAG_BITS) << rowLog; |
1221 | 0 | U32 const tag = hash & ZSTD_ROW_HASH_TAG_MASK; |
1222 | 0 | U32* const row = hashTable + relRow; |
1223 | 0 | BYTE* tagRow = (BYTE*)(tagTable + relRow); |
1224 | 0 | U32 const headGrouped = (*tagRow & rowMask) * groupWidth; |
1225 | 0 | U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES]; |
1226 | 0 | size_t numMatches = 0; |
1227 | 0 | size_t currMatch = 0; |
1228 | 0 | ZSTD_VecMask matches = ZSTD_row_getMatchMask(tagRow, (BYTE)tag, headGrouped, rowEntries); |
1229 | | |
1230 | | /* Cycle through the matches and prefetch */ |
1231 | 0 | for (; (matches > 0) && (nbAttempts > 0); matches &= (matches - 1)) { |
1232 | 0 | U32 const matchPos = ((headGrouped + ZSTD_VecMask_next(matches)) / groupWidth) & rowMask; |
1233 | 0 | U32 const matchIndex = row[matchPos]; |
1234 | 0 | if(matchPos == 0) continue; |
1235 | 0 | assert(numMatches < rowEntries); |
1236 | 0 | if (matchIndex < lowLimit) |
1237 | 0 | break; |
1238 | 0 | if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) { |
1239 | 0 | PREFETCH_L1(base + matchIndex); |
1240 | 0 | } else { |
1241 | 0 | PREFETCH_L1(dictBase + matchIndex); |
1242 | 0 | } |
1243 | 0 | matchBuffer[numMatches++] = matchIndex; |
1244 | 0 | --nbAttempts; |
1245 | 0 | } |
1246 | | |
1247 | | /* Speed opt: insert current byte into hashtable too. This allows us to avoid one iteration of the loop |
1248 | | in ZSTD_row_update_internal() at the next search. */ |
1249 | 0 | { |
1250 | 0 | U32 const pos = ZSTD_row_nextIndex(tagRow, rowMask); |
1251 | 0 | tagRow[pos] = (BYTE)tag; |
1252 | 0 | row[pos] = ms->nextToUpdate++; |
1253 | 0 | } |
1254 | | |
1255 | | /* Return the longest match */ |
1256 | 0 | for (; currMatch < numMatches; ++currMatch) { |
1257 | 0 | U32 const matchIndex = matchBuffer[currMatch]; |
1258 | 0 | size_t currentMl=0; |
1259 | 0 | assert(matchIndex < curr); |
1260 | 0 | assert(matchIndex >= lowLimit); |
1261 | |
|
1262 | 0 | if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) { |
1263 | 0 | const BYTE* const match = base + matchIndex; |
1264 | 0 | assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */ |
1265 | | /* read 4B starting from (match + ml + 1 - sizeof(U32)) */ |
1266 | 0 | if (MEM_read32(match + ml - 3) == MEM_read32(ip + ml - 3)) /* potentially better */ |
1267 | 0 | currentMl = ZSTD_count(ip, match, iLimit); |
1268 | 0 | } else { |
1269 | 0 | const BYTE* const match = dictBase + matchIndex; |
1270 | 0 | assert(match+4 <= dictEnd); |
1271 | 0 | if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */ |
1272 | 0 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4; |
1273 | 0 | } |
1274 | | |
1275 | | /* Save best solution */ |
1276 | 0 | if (currentMl > ml) { |
1277 | 0 | ml = currentMl; |
1278 | 0 | *offsetPtr = OFFSET_TO_OFFBASE(curr - matchIndex); |
1279 | 0 | if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */ |
1280 | 0 | } |
1281 | 0 | } |
1282 | 0 | } |
1283 | |
|
1284 | 0 | assert(nbAttempts <= (1U << ZSTD_SEARCHLOG_MAX)); /* Check we haven't underflowed. */ |
1285 | 0 | if (dictMode == ZSTD_dedicatedDictSearch) { |
1286 | 0 | ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts + ddsExtraAttempts, dms, |
1287 | 0 | ip, iLimit, prefixStart, curr, dictLimit, ddsIdx); |
1288 | 0 | } else if (dictMode == ZSTD_dictMatchState) { |
1289 | | /* TODO: Measure and potentially add prefetching to DMS */ |
1290 | 0 | const U32 dmsLowestIndex = dms->window.dictLimit; |
1291 | 0 | const BYTE* const dmsBase = dms->window.base; |
1292 | 0 | const BYTE* const dmsEnd = dms->window.nextSrc; |
1293 | 0 | const U32 dmsSize = (U32)(dmsEnd - dmsBase); |
1294 | 0 | const U32 dmsIndexDelta = dictLimit - dmsSize; |
1295 | |
|
1296 | 0 | { U32 const headGrouped = (*dmsTagRow & rowMask) * groupWidth; |
1297 | 0 | U32 matchBuffer[ZSTD_ROW_HASH_MAX_ENTRIES]; |
1298 | 0 | size_t numMatches = 0; |
1299 | 0 | size_t currMatch = 0; |
1300 | 0 | ZSTD_VecMask matches = ZSTD_row_getMatchMask(dmsTagRow, (BYTE)dmsTag, headGrouped, rowEntries); |
1301 | |
|
1302 | 0 | for (; (matches > 0) && (nbAttempts > 0); matches &= (matches - 1)) { |
1303 | 0 | U32 const matchPos = ((headGrouped + ZSTD_VecMask_next(matches)) / groupWidth) & rowMask; |
1304 | 0 | U32 const matchIndex = dmsRow[matchPos]; |
1305 | 0 | if(matchPos == 0) continue; |
1306 | 0 | if (matchIndex < dmsLowestIndex) |
1307 | 0 | break; |
1308 | 0 | PREFETCH_L1(dmsBase + matchIndex); |
1309 | 0 | matchBuffer[numMatches++] = matchIndex; |
1310 | 0 | --nbAttempts; |
1311 | 0 | } |
1312 | | |
1313 | | /* Return the longest match */ |
1314 | 0 | for (; currMatch < numMatches; ++currMatch) { |
1315 | 0 | U32 const matchIndex = matchBuffer[currMatch]; |
1316 | 0 | size_t currentMl=0; |
1317 | 0 | assert(matchIndex >= dmsLowestIndex); |
1318 | 0 | assert(matchIndex < curr); |
1319 | |
|
1320 | 0 | { const BYTE* const match = dmsBase + matchIndex; |
1321 | 0 | assert(match+4 <= dmsEnd); |
1322 | 0 | if (MEM_read32(match) == MEM_read32(ip)) |
1323 | 0 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4; |
1324 | 0 | } |
1325 | |
|
1326 | 0 | if (currentMl > ml) { |
1327 | 0 | ml = currentMl; |
1328 | 0 | assert(curr > matchIndex + dmsIndexDelta); |
1329 | 0 | *offsetPtr = OFFSET_TO_OFFBASE(curr - (matchIndex + dmsIndexDelta)); |
1330 | 0 | if (ip+currentMl == iLimit) break; |
1331 | 0 | } |
1332 | 0 | } |
1333 | 0 | } |
1334 | 0 | } |
1335 | 0 | return ml; |
1336 | 0 | } |
1337 | | |
1338 | | |
1339 | | /** |
1340 | | * Generate search functions templated on (dictMode, mls, rowLog). |
1341 | | * These functions are outlined for code size & compilation time. |
1342 | | * ZSTD_searchMax() dispatches to the correct implementation function. |
1343 | | * |
1344 | | * TODO: The start of the search function involves loading and calculating a |
1345 | | * bunch of constants from the ZSTD_matchState_t. These computations could be |
1346 | | * done in an initialization function, and saved somewhere in the match state. |
1347 | | * Then we could pass a pointer to the saved state instead of the match state, |
1348 | | * and avoid duplicate computations. |
1349 | | * |
1350 | | * TODO: Move the match re-winding into searchMax. This improves compression |
1351 | | * ratio, and unlocks further simplifications with the next TODO. |
1352 | | * |
1353 | | * TODO: Try moving the repcode search into searchMax. After the re-winding |
1354 | | * and repcode search are in searchMax, there is no more logic in the match |
1355 | | * finder loop that requires knowledge about the dictMode. So we should be |
1356 | | * able to avoid force inlining it, and we can join the extDict loop with |
1357 | | * the single segment loop. It should go in searchMax instead of its own |
1358 | | * function to avoid having multiple virtual function calls per search. |
1359 | | */ |
1360 | | |
1361 | 0 | #define ZSTD_BT_SEARCH_FN(dictMode, mls) ZSTD_BtFindBestMatch_##dictMode##_##mls |
1362 | 0 | #define ZSTD_HC_SEARCH_FN(dictMode, mls) ZSTD_HcFindBestMatch_##dictMode##_##mls |
1363 | 0 | #define ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog) ZSTD_RowFindBestMatch_##dictMode##_##mls##_##rowLog |
1364 | | |
1365 | | #define ZSTD_SEARCH_FN_ATTRS FORCE_NOINLINE |
1366 | | |
1367 | | #define GEN_ZSTD_BT_SEARCH_FN(dictMode, mls) \ |
1368 | | ZSTD_SEARCH_FN_ATTRS size_t ZSTD_BT_SEARCH_FN(dictMode, mls)( \ |
1369 | | ZSTD_matchState_t* ms, \ |
1370 | | const BYTE* ip, const BYTE* const iLimit, \ |
1371 | | size_t* offBasePtr) \ |
1372 | 0 | { \ |
1373 | 0 | assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \ |
1374 | 0 | return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, mls, ZSTD_##dictMode); \ |
1375 | 0 | } \ Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_noDict_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_noDict_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_noDict_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_extDict_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_extDict_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_extDict_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_dictMatchState_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_dictMatchState_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_dictMatchState_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_dedicatedDictSearch_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_dedicatedDictSearch_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_BtFindBestMatch_dedicatedDictSearch_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) |
1376 | | |
1377 | | #define GEN_ZSTD_HC_SEARCH_FN(dictMode, mls) \ |
1378 | | ZSTD_SEARCH_FN_ATTRS size_t ZSTD_HC_SEARCH_FN(dictMode, mls)( \ |
1379 | | ZSTD_matchState_t* ms, \ |
1380 | | const BYTE* ip, const BYTE* const iLimit, \ |
1381 | | size_t* offsetPtr) \ |
1382 | 0 | { \ |
1383 | 0 | assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \ |
1384 | 0 | return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode); \ |
1385 | 0 | } \ Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_noDict_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_noDict_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_noDict_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_extDict_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_extDict_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_extDict_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_dictMatchState_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_dictMatchState_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_dictMatchState_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_dedicatedDictSearch_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_dedicatedDictSearch_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_HcFindBestMatch_dedicatedDictSearch_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) |
1386 | | |
1387 | | #define GEN_ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog) \ |
1388 | | ZSTD_SEARCH_FN_ATTRS size_t ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog)( \ |
1389 | | ZSTD_matchState_t* ms, \ |
1390 | | const BYTE* ip, const BYTE* const iLimit, \ |
1391 | | size_t* offsetPtr) \ |
1392 | 0 | { \ |
1393 | 0 | assert(MAX(4, MIN(6, ms->cParams.minMatch)) == mls); \ |
1394 | 0 | assert(MAX(4, MIN(6, ms->cParams.searchLog)) == rowLog); \ |
1395 | 0 | return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_##dictMode, rowLog); \ |
1396 | 0 | } \ Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_4_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_4_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_4_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_5_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_5_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_5_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_6_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_6_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_noDict_6_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_4_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_4_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_4_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_5_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_5_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_5_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_6_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_6_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_extDict_6_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_4_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_4_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_4_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_5_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_5_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_5_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_6_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_6_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dictMatchState_6_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_4_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_4_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_4_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_5_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_5_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_5_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_6_4(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_6_5(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) Unexecuted instantiation: zstd_lazy.cpp:duckdb_zstd::ZSTD_RowFindBestMatch_dedicatedDictSearch_6_6(duckdb_zstd::ZSTD_matchState_t*, unsigned char const*, unsigned char const*, unsigned long*) |
1397 | | |
1398 | | #define ZSTD_FOR_EACH_ROWLOG(X, dictMode, mls) \ |
1399 | 0 | X(dictMode, mls, 4) \ |
1400 | 0 | X(dictMode, mls, 5) \ |
1401 | 0 | X(dictMode, mls, 6) |
1402 | | |
1403 | | #define ZSTD_FOR_EACH_MLS_ROWLOG(X, dictMode) \ |
1404 | | ZSTD_FOR_EACH_ROWLOG(X, dictMode, 4) \ |
1405 | | ZSTD_FOR_EACH_ROWLOG(X, dictMode, 5) \ |
1406 | | ZSTD_FOR_EACH_ROWLOG(X, dictMode, 6) |
1407 | | |
1408 | | #define ZSTD_FOR_EACH_MLS(X, dictMode) \ |
1409 | 0 | X(dictMode, 4) \ |
1410 | 0 | X(dictMode, 5) \ |
1411 | 0 | X(dictMode, 6) |
1412 | | |
1413 | | #define ZSTD_FOR_EACH_DICT_MODE(X, ...) \ |
1414 | | X(__VA_ARGS__, noDict) \ |
1415 | | X(__VA_ARGS__, extDict) \ |
1416 | | X(__VA_ARGS__, dictMatchState) \ |
1417 | | X(__VA_ARGS__, dedicatedDictSearch) |
1418 | | |
1419 | | /* Generate row search fns for each combination of (dictMode, mls, rowLog) */ |
1420 | | ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS_ROWLOG, GEN_ZSTD_ROW_SEARCH_FN) |
1421 | | /* Generate binary Tree search fns for each combination of (dictMode, mls) */ |
1422 | | ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_BT_SEARCH_FN) |
1423 | | /* Generate hash chain search fns for each combination of (dictMode, mls) */ |
1424 | | ZSTD_FOR_EACH_DICT_MODE(ZSTD_FOR_EACH_MLS, GEN_ZSTD_HC_SEARCH_FN) |
1425 | | |
1426 | | typedef enum { search_hashChain=0, search_binaryTree=1, search_rowHash=2 } searchMethod_e; |
1427 | | |
1428 | | #define GEN_ZSTD_CALL_BT_SEARCH_FN(dictMode, mls) \ |
1429 | 0 | case mls: \ |
1430 | 0 | return ZSTD_BT_SEARCH_FN(dictMode, mls)(ms, ip, iend, offsetPtr); |
1431 | | #define GEN_ZSTD_CALL_HC_SEARCH_FN(dictMode, mls) \ |
1432 | 0 | case mls: \ |
1433 | 0 | return ZSTD_HC_SEARCH_FN(dictMode, mls)(ms, ip, iend, offsetPtr); |
1434 | | #define GEN_ZSTD_CALL_ROW_SEARCH_FN(dictMode, mls, rowLog) \ |
1435 | 0 | case rowLog: \ |
1436 | 0 | return ZSTD_ROW_SEARCH_FN(dictMode, mls, rowLog)(ms, ip, iend, offsetPtr); |
1437 | | |
1438 | | #define ZSTD_SWITCH_MLS(X, dictMode) \ |
1439 | 0 | switch (mls) { \ |
1440 | 0 | ZSTD_FOR_EACH_MLS(X, dictMode) \ |
1441 | 0 | } |
1442 | | |
1443 | | #define ZSTD_SWITCH_ROWLOG(dictMode, mls) \ |
1444 | 0 | case mls: \ |
1445 | 0 | switch (rowLog) { \ |
1446 | 0 | ZSTD_FOR_EACH_ROWLOG(GEN_ZSTD_CALL_ROW_SEARCH_FN, dictMode, mls) \ |
1447 | 0 | } \ |
1448 | 0 | ZSTD_UNREACHABLE; \ |
1449 | 0 | break; |
1450 | | |
1451 | | #define ZSTD_SWITCH_SEARCH_METHOD(dictMode) \ |
1452 | 0 | switch (searchMethod) { \ |
1453 | 0 | case search_hashChain: \ |
1454 | 0 | ZSTD_SWITCH_MLS(GEN_ZSTD_CALL_HC_SEARCH_FN, dictMode) \ |
1455 | 0 | break; \ |
1456 | 0 | case search_binaryTree: \ |
1457 | 0 | ZSTD_SWITCH_MLS(GEN_ZSTD_CALL_BT_SEARCH_FN, dictMode) \ |
1458 | 0 | break; \ |
1459 | 0 | case search_rowHash: \ |
1460 | 0 | ZSTD_SWITCH_MLS(ZSTD_SWITCH_ROWLOG, dictMode) \ |
1461 | 0 | break; \ |
1462 | 0 | } \ |
1463 | 0 | ZSTD_UNREACHABLE; |
1464 | | |
1465 | | /** |
1466 | | * Searches for the longest match at @p ip. |
1467 | | * Dispatches to the correct implementation function based on the |
1468 | | * (searchMethod, dictMode, mls, rowLog). We use switch statements |
1469 | | * here instead of using an indirect function call through a function |
1470 | | * pointer because after Spectre and Meltdown mitigations, indirect |
1471 | | * function calls can be very costly, especially in the kernel. |
1472 | | * |
1473 | | * NOTE: dictMode and searchMethod should be templated, so those switch |
1474 | | * statements should be optimized out. Only the mls & rowLog switches |
1475 | | * should be left. |
1476 | | * |
1477 | | * @param ms The match state. |
1478 | | * @param ip The position to search at. |
1479 | | * @param iend The end of the input data. |
1480 | | * @param[out] offsetPtr Stores the match offset into this pointer. |
1481 | | * @param mls The minimum search length, in the range [4, 6]. |
1482 | | * @param rowLog The row log (if applicable), in the range [4, 6]. |
1483 | | * @param searchMethod The search method to use (templated). |
1484 | | * @param dictMode The dictMode (templated). |
1485 | | * |
1486 | | * @returns The length of the longest match found, or < mls if no match is found. |
1487 | | * If a match is found its offset is stored in @p offsetPtr. |
1488 | | */ |
1489 | | FORCE_INLINE_TEMPLATE size_t ZSTD_searchMax( |
1490 | | ZSTD_matchState_t* ms, |
1491 | | const BYTE* ip, |
1492 | | const BYTE* iend, |
1493 | | size_t* offsetPtr, |
1494 | | U32 const mls, |
1495 | | U32 const rowLog, |
1496 | | searchMethod_e const searchMethod, |
1497 | | ZSTD_dictMode_e const dictMode) |
1498 | 0 | { |
1499 | 0 | if (dictMode == ZSTD_noDict) { |
1500 | 0 | ZSTD_SWITCH_SEARCH_METHOD(noDict) |
1501 | 0 | } else if (dictMode == ZSTD_extDict) { |
1502 | 0 | ZSTD_SWITCH_SEARCH_METHOD(extDict) |
1503 | 0 | } else if (dictMode == ZSTD_dictMatchState) { |
1504 | 0 | ZSTD_SWITCH_SEARCH_METHOD(dictMatchState) |
1505 | 0 | } else if (dictMode == ZSTD_dedicatedDictSearch) { |
1506 | 0 | ZSTD_SWITCH_SEARCH_METHOD(dedicatedDictSearch) |
1507 | 0 | } |
1508 | 0 | ZSTD_UNREACHABLE; |
1509 | 0 | return 0; |
1510 | 0 | } |
1511 | | |
1512 | | /* ******************************* |
1513 | | * Common parser - lazy strategy |
1514 | | *********************************/ |
1515 | | |
1516 | | FORCE_INLINE_TEMPLATE |
1517 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
1518 | | size_t ZSTD_compressBlock_lazy_generic( |
1519 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, |
1520 | | U32 rep[ZSTD_REP_NUM], |
1521 | | const void* src, size_t srcSize, |
1522 | | const searchMethod_e searchMethod, const U32 depth, |
1523 | | ZSTD_dictMode_e const dictMode) |
1524 | 0 | { |
1525 | 0 | const BYTE* const istart = (const BYTE*)src; |
1526 | 0 | const BYTE* ip = istart; |
1527 | 0 | const BYTE* anchor = istart; |
1528 | 0 | const BYTE* const iend = istart + srcSize; |
1529 | 0 | const BYTE* const ilimit = (searchMethod == search_rowHash) ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8; |
1530 | 0 | const BYTE* const base = ms->window.base; |
1531 | 0 | const U32 prefixLowestIndex = ms->window.dictLimit; |
1532 | 0 | const BYTE* const prefixLowest = base + prefixLowestIndex; |
1533 | 0 | const U32 mls = BOUNDED(4, ms->cParams.minMatch, 6); |
1534 | 0 | const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6); |
1535 | |
|
1536 | 0 | U32 offset_1 = rep[0], offset_2 = rep[1]; |
1537 | 0 | U32 offsetSaved1 = 0, offsetSaved2 = 0; |
1538 | |
|
1539 | 0 | const int isDMS = dictMode == ZSTD_dictMatchState; |
1540 | 0 | const int isDDS = dictMode == ZSTD_dedicatedDictSearch; |
1541 | 0 | const int isDxS = isDMS || isDDS; |
1542 | 0 | const ZSTD_matchState_t* const dms = ms->dictMatchState; |
1543 | 0 | const U32 dictLowestIndex = isDxS ? dms->window.dictLimit : 0; |
1544 | 0 | const BYTE* const dictBase = isDxS ? dms->window.base : NULL; |
1545 | 0 | const BYTE* const dictLowest = isDxS ? dictBase + dictLowestIndex : NULL; |
1546 | 0 | const BYTE* const dictEnd = isDxS ? dms->window.nextSrc : NULL; |
1547 | 0 | const U32 dictIndexDelta = isDxS ? |
1548 | 0 | prefixLowestIndex - (U32)(dictEnd - dictBase) : |
1549 | 0 | 0; |
1550 | 0 | const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictLowest)); |
1551 | |
|
1552 | 0 | DEBUGLOG(5, "ZSTD_compressBlock_lazy_generic (dictMode=%u) (searchFunc=%u)", (U32)dictMode, (U32)searchMethod); |
1553 | 0 | ip += (dictAndPrefixLength == 0); |
1554 | 0 | if (dictMode == ZSTD_noDict) { |
1555 | 0 | U32 const curr = (U32)(ip - base); |
1556 | 0 | U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, ms->cParams.windowLog); |
1557 | 0 | U32 const maxRep = curr - windowLow; |
1558 | 0 | if (offset_2 > maxRep) offsetSaved2 = offset_2, offset_2 = 0; |
1559 | 0 | if (offset_1 > maxRep) offsetSaved1 = offset_1, offset_1 = 0; |
1560 | 0 | } |
1561 | 0 | if (isDxS) { |
1562 | | /* dictMatchState repCode checks don't currently handle repCode == 0 |
1563 | | * disabling. */ |
1564 | 0 | assert(offset_1 <= dictAndPrefixLength); |
1565 | 0 | assert(offset_2 <= dictAndPrefixLength); |
1566 | 0 | } |
1567 | | |
1568 | | /* Reset the lazy skipping state */ |
1569 | 0 | ms->lazySkipping = 0; |
1570 | |
|
1571 | 0 | if (searchMethod == search_rowHash) { |
1572 | 0 | ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit); |
1573 | 0 | } |
1574 | | |
1575 | | /* Match Loop */ |
1576 | 0 | #if defined(__GNUC__) && defined(__x86_64__) |
1577 | | /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the |
1578 | | * code alignment is perturbed. To fix the instability align the loop on 32-bytes. |
1579 | | */ |
1580 | 0 | __asm__(".p2align 5"); |
1581 | 0 | #endif |
1582 | 0 | while (ip < ilimit) { |
1583 | 0 | size_t matchLength=0; |
1584 | 0 | size_t offBase = REPCODE1_TO_OFFBASE; |
1585 | 0 | const BYTE* start=ip+1; |
1586 | 0 | DEBUGLOG(7, "search baseline (depth 0)"); |
1587 | | |
1588 | | /* check repCode */ |
1589 | 0 | if (isDxS) { |
1590 | 0 | const U32 repIndex = (U32)(ip - base) + 1 - offset_1; |
1591 | 0 | const BYTE* repMatch = ((dictMode == ZSTD_dictMatchState || dictMode == ZSTD_dedicatedDictSearch) |
1592 | 0 | && repIndex < prefixLowestIndex) ? |
1593 | 0 | dictBase + (repIndex - dictIndexDelta) : |
1594 | 0 | base + repIndex; |
1595 | 0 | if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */) |
1596 | 0 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
1597 | 0 | const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; |
1598 | 0 | matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4; |
1599 | 0 | if (depth==0) goto _storeSequence; |
1600 | 0 | } |
1601 | 0 | } |
1602 | 0 | if ( dictMode == ZSTD_noDict |
1603 | 0 | && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) { |
1604 | 0 | matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; |
1605 | 0 | if (depth==0) goto _storeSequence; |
1606 | 0 | } |
1607 | | |
1608 | | /* first search (depth 0) */ |
1609 | 0 | { size_t offbaseFound = 999999999; |
1610 | 0 | size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &offbaseFound, mls, rowLog, searchMethod, dictMode); |
1611 | 0 | if (ml2 > matchLength) |
1612 | 0 | matchLength = ml2, start = ip, offBase = offbaseFound; |
1613 | 0 | } |
1614 | |
|
1615 | 0 | if (matchLength < 4) { |
1616 | 0 | size_t const step = ((size_t)(ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */; |
1617 | 0 | ip += step; |
1618 | | /* Enter the lazy skipping mode once we are skipping more than 8 bytes at a time. |
1619 | | * In this mode we stop inserting every position into our tables, and only insert |
1620 | | * positions that we search, which is one in step positions. |
1621 | | * The exact cutoff is flexible, I've just chosen a number that is reasonably high, |
1622 | | * so we minimize the compression ratio loss in "normal" scenarios. This mode gets |
1623 | | * triggered once we've gone 2KB without finding any matches. |
1624 | | */ |
1625 | 0 | ms->lazySkipping = step > kLazySkippingStep; |
1626 | 0 | continue; |
1627 | 0 | } |
1628 | | |
1629 | | /* let's try to find a better solution */ |
1630 | 0 | if (depth>=1) |
1631 | 0 | while (ip<ilimit) { |
1632 | 0 | DEBUGLOG(7, "search depth 1"); |
1633 | 0 | ip ++; |
1634 | 0 | if ( (dictMode == ZSTD_noDict) |
1635 | 0 | && (offBase) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) { |
1636 | 0 | size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4; |
1637 | 0 | int const gain2 = (int)(mlRep * 3); |
1638 | 0 | int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offBase) + 1); |
1639 | 0 | if ((mlRep >= 4) && (gain2 > gain1)) |
1640 | 0 | matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip; |
1641 | 0 | } |
1642 | 0 | if (isDxS) { |
1643 | 0 | const U32 repIndex = (U32)(ip - base) - offset_1; |
1644 | 0 | const BYTE* repMatch = repIndex < prefixLowestIndex ? |
1645 | 0 | dictBase + (repIndex - dictIndexDelta) : |
1646 | 0 | base + repIndex; |
1647 | 0 | if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */) |
1648 | 0 | && (MEM_read32(repMatch) == MEM_read32(ip)) ) { |
1649 | 0 | const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; |
1650 | 0 | size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4; |
1651 | 0 | int const gain2 = (int)(mlRep * 3); |
1652 | 0 | int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offBase) + 1); |
1653 | 0 | if ((mlRep >= 4) && (gain2 > gain1)) |
1654 | 0 | matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip; |
1655 | 0 | } |
1656 | 0 | } |
1657 | 0 | { size_t ofbCandidate=999999999; |
1658 | 0 | size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode); |
1659 | 0 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */ |
1660 | 0 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 4); |
1661 | 0 | if ((ml2 >= 4) && (gain2 > gain1)) { |
1662 | 0 | matchLength = ml2, offBase = ofbCandidate, start = ip; |
1663 | 0 | continue; /* search a better one */ |
1664 | 0 | } } |
1665 | | |
1666 | | /* let's find an even better one */ |
1667 | 0 | if ((depth==2) && (ip<ilimit)) { |
1668 | 0 | DEBUGLOG(7, "search depth 2"); |
1669 | 0 | ip ++; |
1670 | 0 | if ( (dictMode == ZSTD_noDict) |
1671 | 0 | && (offBase) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) { |
1672 | 0 | size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4; |
1673 | 0 | int const gain2 = (int)(mlRep * 4); |
1674 | 0 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 1); |
1675 | 0 | if ((mlRep >= 4) && (gain2 > gain1)) |
1676 | 0 | matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip; |
1677 | 0 | } |
1678 | 0 | if (isDxS) { |
1679 | 0 | const U32 repIndex = (U32)(ip - base) - offset_1; |
1680 | 0 | const BYTE* repMatch = repIndex < prefixLowestIndex ? |
1681 | 0 | dictBase + (repIndex - dictIndexDelta) : |
1682 | 0 | base + repIndex; |
1683 | 0 | if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */) |
1684 | 0 | && (MEM_read32(repMatch) == MEM_read32(ip)) ) { |
1685 | 0 | const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; |
1686 | 0 | size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4; |
1687 | 0 | int const gain2 = (int)(mlRep * 4); |
1688 | 0 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 1); |
1689 | 0 | if ((mlRep >= 4) && (gain2 > gain1)) |
1690 | 0 | matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip; |
1691 | 0 | } |
1692 | 0 | } |
1693 | 0 | { size_t ofbCandidate=999999999; |
1694 | 0 | size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode); |
1695 | 0 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */ |
1696 | 0 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 7); |
1697 | 0 | if ((ml2 >= 4) && (gain2 > gain1)) { |
1698 | 0 | matchLength = ml2, offBase = ofbCandidate, start = ip; |
1699 | 0 | continue; |
1700 | 0 | } } } |
1701 | 0 | break; /* nothing found : store previous solution */ |
1702 | 0 | } |
1703 | | |
1704 | | /* NOTE: |
1705 | | * Pay attention that `start[-value]` can lead to strange undefined behavior |
1706 | | * notably if `value` is unsigned, resulting in a large positive `-value`. |
1707 | | */ |
1708 | | /* catch up */ |
1709 | 0 | if (OFFBASE_IS_OFFSET(offBase)) { |
1710 | 0 | if (dictMode == ZSTD_noDict) { |
1711 | 0 | while ( ((start > anchor) & (start - OFFBASE_TO_OFFSET(offBase) > prefixLowest)) |
1712 | 0 | && (start[-1] == (start-OFFBASE_TO_OFFSET(offBase))[-1]) ) /* only search for offset within prefix */ |
1713 | 0 | { start--; matchLength++; } |
1714 | 0 | } |
1715 | 0 | if (isDxS) { |
1716 | 0 | U32 const matchIndex = (U32)((size_t)(start-base) - OFFBASE_TO_OFFSET(offBase)); |
1717 | 0 | const BYTE* match = (matchIndex < prefixLowestIndex) ? dictBase + matchIndex - dictIndexDelta : base + matchIndex; |
1718 | 0 | const BYTE* const mStart = (matchIndex < prefixLowestIndex) ? dictLowest : prefixLowest; |
1719 | 0 | while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */ |
1720 | 0 | } |
1721 | 0 | offset_2 = offset_1; offset_1 = (U32)OFFBASE_TO_OFFSET(offBase); |
1722 | 0 | } |
1723 | | /* store sequence */ |
1724 | 0 | _storeSequence: |
1725 | 0 | { size_t const litLength = (size_t)(start - anchor); |
1726 | 0 | ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offBase, matchLength); |
1727 | 0 | anchor = ip = start + matchLength; |
1728 | 0 | } |
1729 | 0 | if (ms->lazySkipping) { |
1730 | | /* We've found a match, disable lazy skipping mode, and refill the hash cache. */ |
1731 | 0 | if (searchMethod == search_rowHash) { |
1732 | 0 | ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit); |
1733 | 0 | } |
1734 | 0 | ms->lazySkipping = 0; |
1735 | 0 | } |
1736 | | |
1737 | | /* check immediate repcode */ |
1738 | 0 | if (isDxS) { |
1739 | 0 | while (ip <= ilimit) { |
1740 | 0 | U32 const current2 = (U32)(ip-base); |
1741 | 0 | U32 const repIndex = current2 - offset_2; |
1742 | 0 | const BYTE* repMatch = repIndex < prefixLowestIndex ? |
1743 | 0 | dictBase - dictIndexDelta + repIndex : |
1744 | 0 | base + repIndex; |
1745 | 0 | if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex) >= 3 /* intentional overflow */) |
1746 | 0 | && (MEM_read32(repMatch) == MEM_read32(ip)) ) { |
1747 | 0 | const BYTE* const repEnd2 = repIndex < prefixLowestIndex ? dictEnd : iend; |
1748 | 0 | matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd2, prefixLowest) + 4; |
1749 | 0 | offBase = offset_2; offset_2 = offset_1; offset_1 = (U32)offBase; /* swap offset_2 <=> offset_1 */ |
1750 | 0 | ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, matchLength); |
1751 | 0 | ip += matchLength; |
1752 | 0 | anchor = ip; |
1753 | 0 | continue; |
1754 | 0 | } |
1755 | 0 | break; |
1756 | 0 | } |
1757 | 0 | } |
1758 | |
|
1759 | 0 | if (dictMode == ZSTD_noDict) { |
1760 | 0 | while ( ((ip <= ilimit) & (offset_2>0)) |
1761 | 0 | && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) { |
1762 | | /* store sequence */ |
1763 | 0 | matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; |
1764 | 0 | offBase = offset_2; offset_2 = offset_1; offset_1 = (U32)offBase; /* swap repcodes */ |
1765 | 0 | ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, matchLength); |
1766 | 0 | ip += matchLength; |
1767 | 0 | anchor = ip; |
1768 | 0 | continue; /* faster when present ... (?) */ |
1769 | 0 | } } } |
1770 | | |
1771 | | /* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0), |
1772 | | * rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */ |
1773 | 0 | offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2; |
1774 | | |
1775 | | /* save reps for next block */ |
1776 | 0 | rep[0] = offset_1 ? offset_1 : offsetSaved1; |
1777 | 0 | rep[1] = offset_2 ? offset_2 : offsetSaved2; |
1778 | | |
1779 | | /* Return the last literals size */ |
1780 | 0 | return (size_t)(iend - anchor); |
1781 | 0 | } |
1782 | | #endif /* build exclusions */ |
1783 | | |
1784 | | |
1785 | | #ifndef ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR |
1786 | | size_t ZSTD_compressBlock_greedy( |
1787 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1788 | | void const* src, size_t srcSize) |
1789 | 0 | { |
1790 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_noDict); |
1791 | 0 | } |
1792 | | |
1793 | | size_t ZSTD_compressBlock_greedy_dictMatchState( |
1794 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1795 | | void const* src, size_t srcSize) |
1796 | 0 | { |
1797 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dictMatchState); |
1798 | 0 | } |
1799 | | |
1800 | | size_t ZSTD_compressBlock_greedy_dedicatedDictSearch( |
1801 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1802 | | void const* src, size_t srcSize) |
1803 | 0 | { |
1804 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0, ZSTD_dedicatedDictSearch); |
1805 | 0 | } |
1806 | | |
1807 | | size_t ZSTD_compressBlock_greedy_row( |
1808 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1809 | | void const* src, size_t srcSize) |
1810 | 0 | { |
1811 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_noDict); |
1812 | 0 | } |
1813 | | |
1814 | | size_t ZSTD_compressBlock_greedy_dictMatchState_row( |
1815 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1816 | | void const* src, size_t srcSize) |
1817 | 0 | { |
1818 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dictMatchState); |
1819 | 0 | } |
1820 | | |
1821 | | size_t ZSTD_compressBlock_greedy_dedicatedDictSearch_row( |
1822 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1823 | | void const* src, size_t srcSize) |
1824 | 0 | { |
1825 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0, ZSTD_dedicatedDictSearch); |
1826 | 0 | } |
1827 | | #endif |
1828 | | |
1829 | | #ifndef ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR |
1830 | | size_t ZSTD_compressBlock_lazy( |
1831 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1832 | | void const* src, size_t srcSize) |
1833 | 0 | { |
1834 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_noDict); |
1835 | 0 | } |
1836 | | |
1837 | | size_t ZSTD_compressBlock_lazy_dictMatchState( |
1838 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1839 | | void const* src, size_t srcSize) |
1840 | 0 | { |
1841 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dictMatchState); |
1842 | 0 | } |
1843 | | |
1844 | | size_t ZSTD_compressBlock_lazy_dedicatedDictSearch( |
1845 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1846 | | void const* src, size_t srcSize) |
1847 | 0 | { |
1848 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1, ZSTD_dedicatedDictSearch); |
1849 | 0 | } |
1850 | | |
1851 | | size_t ZSTD_compressBlock_lazy_row( |
1852 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1853 | | void const* src, size_t srcSize) |
1854 | 0 | { |
1855 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_noDict); |
1856 | 0 | } |
1857 | | |
1858 | | size_t ZSTD_compressBlock_lazy_dictMatchState_row( |
1859 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1860 | | void const* src, size_t srcSize) |
1861 | 0 | { |
1862 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dictMatchState); |
1863 | 0 | } |
1864 | | |
1865 | | size_t ZSTD_compressBlock_lazy_dedicatedDictSearch_row( |
1866 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1867 | | void const* src, size_t srcSize) |
1868 | 0 | { |
1869 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1, ZSTD_dedicatedDictSearch); |
1870 | 0 | } |
1871 | | #endif |
1872 | | |
1873 | | #ifndef ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR |
1874 | | size_t ZSTD_compressBlock_lazy2( |
1875 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1876 | | void const* src, size_t srcSize) |
1877 | 0 | { |
1878 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_noDict); |
1879 | 0 | } |
1880 | | |
1881 | | size_t ZSTD_compressBlock_lazy2_dictMatchState( |
1882 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1883 | | void const* src, size_t srcSize) |
1884 | 0 | { |
1885 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dictMatchState); |
1886 | 0 | } |
1887 | | |
1888 | | size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch( |
1889 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1890 | | void const* src, size_t srcSize) |
1891 | 0 | { |
1892 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2, ZSTD_dedicatedDictSearch); |
1893 | 0 | } |
1894 | | |
1895 | | size_t ZSTD_compressBlock_lazy2_row( |
1896 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1897 | | void const* src, size_t srcSize) |
1898 | 0 | { |
1899 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_noDict); |
1900 | 0 | } |
1901 | | |
1902 | | size_t ZSTD_compressBlock_lazy2_dictMatchState_row( |
1903 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1904 | | void const* src, size_t srcSize) |
1905 | 0 | { |
1906 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dictMatchState); |
1907 | 0 | } |
1908 | | |
1909 | | size_t ZSTD_compressBlock_lazy2_dedicatedDictSearch_row( |
1910 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1911 | | void const* src, size_t srcSize) |
1912 | 0 | { |
1913 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2, ZSTD_dedicatedDictSearch); |
1914 | 0 | } |
1915 | | #endif |
1916 | | |
1917 | | #ifndef ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR |
1918 | | size_t ZSTD_compressBlock_btlazy2( |
1919 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1920 | | void const* src, size_t srcSize) |
1921 | 0 | { |
1922 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_noDict); |
1923 | 0 | } |
1924 | | |
1925 | | size_t ZSTD_compressBlock_btlazy2_dictMatchState( |
1926 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
1927 | | void const* src, size_t srcSize) |
1928 | 0 | { |
1929 | 0 | return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2, ZSTD_dictMatchState); |
1930 | 0 | } |
1931 | | #endif |
1932 | | |
1933 | | #if !defined(ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR) \ |
1934 | | || !defined(ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR) \ |
1935 | | || !defined(ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR) \ |
1936 | | || !defined(ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR) |
1937 | | FORCE_INLINE_TEMPLATE |
1938 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
1939 | | size_t ZSTD_compressBlock_lazy_extDict_generic( |
1940 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, |
1941 | | U32 rep[ZSTD_REP_NUM], |
1942 | | const void* src, size_t srcSize, |
1943 | | const searchMethod_e searchMethod, const U32 depth) |
1944 | 0 | { |
1945 | 0 | const BYTE* const istart = (const BYTE*)src; |
1946 | 0 | const BYTE* ip = istart; |
1947 | 0 | const BYTE* anchor = istart; |
1948 | 0 | const BYTE* const iend = istart + srcSize; |
1949 | 0 | const BYTE* const ilimit = searchMethod == search_rowHash ? iend - 8 - ZSTD_ROW_HASH_CACHE_SIZE : iend - 8; |
1950 | 0 | const BYTE* const base = ms->window.base; |
1951 | 0 | const U32 dictLimit = ms->window.dictLimit; |
1952 | 0 | const BYTE* const prefixStart = base + dictLimit; |
1953 | 0 | const BYTE* const dictBase = ms->window.dictBase; |
1954 | 0 | const BYTE* const dictEnd = dictBase + dictLimit; |
1955 | 0 | const BYTE* const dictStart = dictBase + ms->window.lowLimit; |
1956 | 0 | const U32 windowLog = ms->cParams.windowLog; |
1957 | 0 | const U32 mls = BOUNDED(4, ms->cParams.minMatch, 6); |
1958 | 0 | const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6); |
1959 | |
|
1960 | 0 | U32 offset_1 = rep[0], offset_2 = rep[1]; |
1961 | |
|
1962 | 0 | DEBUGLOG(5, "ZSTD_compressBlock_lazy_extDict_generic (searchFunc=%u)", (U32)searchMethod); |
1963 | | |
1964 | | /* Reset the lazy skipping state */ |
1965 | 0 | ms->lazySkipping = 0; |
1966 | | |
1967 | | /* init */ |
1968 | 0 | ip += (ip == prefixStart); |
1969 | 0 | if (searchMethod == search_rowHash) { |
1970 | 0 | ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit); |
1971 | 0 | } |
1972 | | |
1973 | | /* Match Loop */ |
1974 | 0 | #if defined(__GNUC__) && defined(__x86_64__) |
1975 | | /* I've measured random a 5% speed loss on levels 5 & 6 (greedy) when the |
1976 | | * code alignment is perturbed. To fix the instability align the loop on 32-bytes. |
1977 | | */ |
1978 | 0 | __asm__(".p2align 5"); |
1979 | 0 | #endif |
1980 | 0 | while (ip < ilimit) { |
1981 | 0 | size_t matchLength=0; |
1982 | 0 | size_t offBase = REPCODE1_TO_OFFBASE; |
1983 | 0 | const BYTE* start=ip+1; |
1984 | 0 | U32 curr = (U32)(ip-base); |
1985 | | |
1986 | | /* check repCode */ |
1987 | 0 | { const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr+1, windowLog); |
1988 | 0 | const U32 repIndex = (U32)(curr+1 - offset_1); |
1989 | 0 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
1990 | 0 | const BYTE* const repMatch = repBase + repIndex; |
1991 | 0 | if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ |
1992 | 0 | & (offset_1 <= curr+1 - windowLow) ) /* note: we are searching at curr+1 */ |
1993 | 0 | if (MEM_read32(ip+1) == MEM_read32(repMatch)) { |
1994 | | /* repcode detected we should take it */ |
1995 | 0 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
1996 | 0 | matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
1997 | 0 | if (depth==0) goto _storeSequence; |
1998 | 0 | } } |
1999 | | |
2000 | | /* first search (depth 0) */ |
2001 | 0 | { size_t ofbCandidate = 999999999; |
2002 | 0 | size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_extDict); |
2003 | 0 | if (ml2 > matchLength) |
2004 | 0 | matchLength = ml2, start = ip, offBase = ofbCandidate; |
2005 | 0 | } |
2006 | |
|
2007 | 0 | if (matchLength < 4) { |
2008 | 0 | size_t const step = ((size_t)(ip-anchor) >> kSearchStrength); |
2009 | 0 | ip += step + 1; /* jump faster over incompressible sections */ |
2010 | | /* Enter the lazy skipping mode once we are skipping more than 8 bytes at a time. |
2011 | | * In this mode we stop inserting every position into our tables, and only insert |
2012 | | * positions that we search, which is one in step positions. |
2013 | | * The exact cutoff is flexible, I've just chosen a number that is reasonably high, |
2014 | | * so we minimize the compression ratio loss in "normal" scenarios. This mode gets |
2015 | | * triggered once we've gone 2KB without finding any matches. |
2016 | | */ |
2017 | 0 | ms->lazySkipping = step > kLazySkippingStep; |
2018 | 0 | continue; |
2019 | 0 | } |
2020 | | |
2021 | | /* let's try to find a better solution */ |
2022 | 0 | if (depth>=1) |
2023 | 0 | while (ip<ilimit) { |
2024 | 0 | ip ++; |
2025 | 0 | curr++; |
2026 | | /* check repCode */ |
2027 | 0 | if (offBase) { |
2028 | 0 | const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog); |
2029 | 0 | const U32 repIndex = (U32)(curr - offset_1); |
2030 | 0 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
2031 | 0 | const BYTE* const repMatch = repBase + repIndex; |
2032 | 0 | if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */ |
2033 | 0 | & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */ |
2034 | 0 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
2035 | | /* repcode detected */ |
2036 | 0 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
2037 | 0 | size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
2038 | 0 | int const gain2 = (int)(repLength * 3); |
2039 | 0 | int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offBase) + 1); |
2040 | 0 | if ((repLength >= 4) && (gain2 > gain1)) |
2041 | 0 | matchLength = repLength, offBase = REPCODE1_TO_OFFBASE, start = ip; |
2042 | 0 | } } |
2043 | | |
2044 | | /* search match, depth 1 */ |
2045 | 0 | { size_t ofbCandidate = 999999999; |
2046 | 0 | size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_extDict); |
2047 | 0 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */ |
2048 | 0 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 4); |
2049 | 0 | if ((ml2 >= 4) && (gain2 > gain1)) { |
2050 | 0 | matchLength = ml2, offBase = ofbCandidate, start = ip; |
2051 | 0 | continue; /* search a better one */ |
2052 | 0 | } } |
2053 | | |
2054 | | /* let's find an even better one */ |
2055 | 0 | if ((depth==2) && (ip<ilimit)) { |
2056 | 0 | ip ++; |
2057 | 0 | curr++; |
2058 | | /* check repCode */ |
2059 | 0 | if (offBase) { |
2060 | 0 | const U32 windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog); |
2061 | 0 | const U32 repIndex = (U32)(curr - offset_1); |
2062 | 0 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
2063 | 0 | const BYTE* const repMatch = repBase + repIndex; |
2064 | 0 | if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */ |
2065 | 0 | & (offset_1 <= curr - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */ |
2066 | 0 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
2067 | | /* repcode detected */ |
2068 | 0 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
2069 | 0 | size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
2070 | 0 | int const gain2 = (int)(repLength * 4); |
2071 | 0 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 1); |
2072 | 0 | if ((repLength >= 4) && (gain2 > gain1)) |
2073 | 0 | matchLength = repLength, offBase = REPCODE1_TO_OFFBASE, start = ip; |
2074 | 0 | } } |
2075 | | |
2076 | | /* search match, depth 2 */ |
2077 | 0 | { size_t ofbCandidate = 999999999; |
2078 | 0 | size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_extDict); |
2079 | 0 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */ |
2080 | 0 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offBase) + 7); |
2081 | 0 | if ((ml2 >= 4) && (gain2 > gain1)) { |
2082 | 0 | matchLength = ml2, offBase = ofbCandidate, start = ip; |
2083 | 0 | continue; |
2084 | 0 | } } } |
2085 | 0 | break; /* nothing found : store previous solution */ |
2086 | 0 | } |
2087 | | |
2088 | | /* catch up */ |
2089 | 0 | if (OFFBASE_IS_OFFSET(offBase)) { |
2090 | 0 | U32 const matchIndex = (U32)((size_t)(start-base) - OFFBASE_TO_OFFSET(offBase)); |
2091 | 0 | const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex; |
2092 | 0 | const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart; |
2093 | 0 | while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */ |
2094 | 0 | offset_2 = offset_1; offset_1 = (U32)OFFBASE_TO_OFFSET(offBase); |
2095 | 0 | } |
2096 | | |
2097 | | /* store sequence */ |
2098 | 0 | _storeSequence: |
2099 | 0 | { size_t const litLength = (size_t)(start - anchor); |
2100 | 0 | ZSTD_storeSeq(seqStore, litLength, anchor, iend, (U32)offBase, matchLength); |
2101 | 0 | anchor = ip = start + matchLength; |
2102 | 0 | } |
2103 | 0 | if (ms->lazySkipping) { |
2104 | | /* We've found a match, disable lazy skipping mode, and refill the hash cache. */ |
2105 | 0 | if (searchMethod == search_rowHash) { |
2106 | 0 | ZSTD_row_fillHashCache(ms, base, rowLog, mls, ms->nextToUpdate, ilimit); |
2107 | 0 | } |
2108 | 0 | ms->lazySkipping = 0; |
2109 | 0 | } |
2110 | | |
2111 | | /* check immediate repcode */ |
2112 | 0 | while (ip <= ilimit) { |
2113 | 0 | const U32 repCurrent = (U32)(ip-base); |
2114 | 0 | const U32 windowLow = ZSTD_getLowestMatchIndex(ms, repCurrent, windowLog); |
2115 | 0 | const U32 repIndex = repCurrent - offset_2; |
2116 | 0 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
2117 | 0 | const BYTE* const repMatch = repBase + repIndex; |
2118 | 0 | if ( ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow : do not test positions overlapping 2 memory segments */ |
2119 | 0 | & (offset_2 <= repCurrent - windowLow) ) /* equivalent to `curr > repIndex >= windowLow` */ |
2120 | 0 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
2121 | | /* repcode detected we should take it */ |
2122 | 0 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
2123 | 0 | matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
2124 | 0 | offBase = offset_2; offset_2 = offset_1; offset_1 = (U32)offBase; /* swap offset history */ |
2125 | 0 | ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, matchLength); |
2126 | 0 | ip += matchLength; |
2127 | 0 | anchor = ip; |
2128 | 0 | continue; /* faster when present ... (?) */ |
2129 | 0 | } |
2130 | 0 | break; |
2131 | 0 | } } |
2132 | | |
2133 | | /* Save reps for next block */ |
2134 | 0 | rep[0] = offset_1; |
2135 | 0 | rep[1] = offset_2; |
2136 | | |
2137 | | /* Return the last literals size */ |
2138 | 0 | return (size_t)(iend - anchor); |
2139 | 0 | } |
2140 | | #endif /* build exclusions */ |
2141 | | |
2142 | | #ifndef ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR |
2143 | | size_t ZSTD_compressBlock_greedy_extDict( |
2144 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
2145 | | void const* src, size_t srcSize) |
2146 | 0 | { |
2147 | 0 | return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 0); |
2148 | 0 | } |
2149 | | |
2150 | | size_t ZSTD_compressBlock_greedy_extDict_row( |
2151 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
2152 | | void const* src, size_t srcSize) |
2153 | 0 | { |
2154 | 0 | return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 0); |
2155 | 0 | } |
2156 | | #endif |
2157 | | |
2158 | | #ifndef ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR |
2159 | | size_t ZSTD_compressBlock_lazy_extDict( |
2160 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
2161 | | void const* src, size_t srcSize) |
2162 | | |
2163 | 0 | { |
2164 | 0 | return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 1); |
2165 | 0 | } |
2166 | | |
2167 | | size_t ZSTD_compressBlock_lazy_extDict_row( |
2168 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
2169 | | void const* src, size_t srcSize) |
2170 | | |
2171 | 0 | { |
2172 | 0 | return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 1); |
2173 | 0 | } |
2174 | | #endif |
2175 | | |
2176 | | #ifndef ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR |
2177 | | size_t ZSTD_compressBlock_lazy2_extDict( |
2178 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
2179 | | void const* src, size_t srcSize) |
2180 | | |
2181 | 0 | { |
2182 | 0 | return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_hashChain, 2); |
2183 | 0 | } |
2184 | | |
2185 | | size_t ZSTD_compressBlock_lazy2_extDict_row( |
2186 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
2187 | | void const* src, size_t srcSize) |
2188 | 0 | { |
2189 | 0 | return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_rowHash, 2); |
2190 | 0 | } |
2191 | | #endif |
2192 | | |
2193 | | #ifndef ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR |
2194 | | size_t ZSTD_compressBlock_btlazy2_extDict( |
2195 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
2196 | | void const* src, size_t srcSize) |
2197 | | |
2198 | 0 | { |
2199 | 0 | return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, search_binaryTree, 2); |
2200 | 0 | } |
2201 | | #endif |
2202 | | |
2203 | | } // namespace duckdb_zstd |