/src/zstd/lib/compress/zstd_fast.c
Line | Count | Source (jump to first uncovered line) |
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_internal.h" /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */ |
12 | | #include "zstd_fast.h" |
13 | | |
14 | | static |
15 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
16 | | void ZSTD_fillHashTableForCDict(ZSTD_MatchState_t* ms, |
17 | | const void* const end, |
18 | | ZSTD_dictTableLoadMethod_e dtlm) |
19 | 65.9k | { |
20 | 65.9k | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
21 | 65.9k | U32* const hashTable = ms->hashTable; |
22 | 65.9k | U32 const hBits = cParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS; |
23 | 65.9k | U32 const mls = cParams->minMatch; |
24 | 65.9k | const BYTE* const base = ms->window.base; |
25 | 65.9k | const BYTE* ip = base + ms->nextToUpdate; |
26 | 65.9k | const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; |
27 | 65.9k | const U32 fastHashFillStep = 3; |
28 | | |
29 | | /* Currently, we always use ZSTD_dtlm_full for filling CDict tables. |
30 | | * Feel free to remove this assert if there's a good reason! */ |
31 | 65.9k | assert(dtlm == ZSTD_dtlm_full); |
32 | | |
33 | | /* Always insert every fastHashFillStep position into the hash table. |
34 | | * Insert the other positions if their hash entry is empty. |
35 | | */ |
36 | 29.2M | for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) { |
37 | 29.1M | U32 const curr = (U32)(ip - base); |
38 | 29.1M | { size_t const hashAndTag = ZSTD_hashPtr(ip, hBits, mls); |
39 | 29.1M | ZSTD_writeTaggedIndex(hashTable, hashAndTag, curr); } |
40 | | |
41 | 29.1M | if (dtlm == ZSTD_dtlm_fast) continue; |
42 | | /* Only load extra positions for ZSTD_dtlm_full */ |
43 | 29.1M | { U32 p; |
44 | 87.5M | for (p = 1; p < fastHashFillStep; ++p) { |
45 | 58.3M | size_t const hashAndTag = ZSTD_hashPtr(ip + p, hBits, mls); |
46 | 58.3M | if (hashTable[hashAndTag >> ZSTD_SHORT_CACHE_TAG_BITS] == 0) { /* not yet filled */ |
47 | 22.3M | ZSTD_writeTaggedIndex(hashTable, hashAndTag, curr + p); |
48 | 22.3M | } } } } |
49 | 65.9k | } |
50 | | |
51 | | static |
52 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
53 | | void ZSTD_fillHashTableForCCtx(ZSTD_MatchState_t* ms, |
54 | | const void* const end, |
55 | | ZSTD_dictTableLoadMethod_e dtlm) |
56 | 1.43M | { |
57 | 1.43M | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
58 | 1.43M | U32* const hashTable = ms->hashTable; |
59 | 1.43M | U32 const hBits = cParams->hashLog; |
60 | 1.43M | U32 const mls = cParams->minMatch; |
61 | 1.43M | const BYTE* const base = ms->window.base; |
62 | 1.43M | const BYTE* ip = base + ms->nextToUpdate; |
63 | 1.43M | const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; |
64 | 1.43M | const U32 fastHashFillStep = 3; |
65 | | |
66 | | /* Currently, we always use ZSTD_dtlm_fast for filling CCtx tables. |
67 | | * Feel free to remove this assert if there's a good reason! */ |
68 | 1.43M | assert(dtlm == ZSTD_dtlm_fast); |
69 | | |
70 | | /* Always insert every fastHashFillStep position into the hash table. |
71 | | * Insert the other positions if their hash entry is empty. |
72 | | */ |
73 | 234M | for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) { |
74 | 233M | U32 const curr = (U32)(ip - base); |
75 | 233M | size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls); |
76 | 233M | hashTable[hash0] = curr; |
77 | 233M | if (dtlm == ZSTD_dtlm_fast) continue; |
78 | | /* Only load extra positions for ZSTD_dtlm_full */ |
79 | 0 | { U32 p; |
80 | 0 | for (p = 1; p < fastHashFillStep; ++p) { |
81 | 0 | size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls); |
82 | 0 | if (hashTable[hash] == 0) { /* not yet filled */ |
83 | 0 | hashTable[hash] = curr + p; |
84 | 0 | } } } } |
85 | 1.43M | } |
86 | | |
87 | | void ZSTD_fillHashTable(ZSTD_MatchState_t* ms, |
88 | | const void* const end, |
89 | | ZSTD_dictTableLoadMethod_e dtlm, |
90 | | ZSTD_tableFillPurpose_e tfp) |
91 | 1.50M | { |
92 | 1.50M | if (tfp == ZSTD_tfp_forCDict) { |
93 | 65.9k | ZSTD_fillHashTableForCDict(ms, end, dtlm); |
94 | 1.43M | } else { |
95 | 1.43M | ZSTD_fillHashTableForCCtx(ms, end, dtlm); |
96 | 1.43M | } |
97 | 1.50M | } |
98 | | |
99 | | |
100 | | typedef int (*ZSTD_match4Found) (const BYTE* currentPtr, const BYTE* matchAddress, U32 matchIdx, U32 idxLowLimit); |
101 | | |
102 | | static int |
103 | | ZSTD_match4Found_cmov(const BYTE* currentPtr, const BYTE* matchAddress, U32 matchIdx, U32 idxLowLimit) |
104 | 121M | { |
105 | | /* Array of ~random data, should have low probability of matching data. |
106 | | * Load from here if the index is invalid. |
107 | | * Used to avoid unpredictable branches. */ |
108 | 121M | static const BYTE dummy[] = {0x12,0x34,0x56,0x78}; |
109 | | |
110 | | /* currentIdx >= lowLimit is a (somewhat) unpredictable branch. |
111 | | * However expression below compiles into conditional move. |
112 | | */ |
113 | 121M | const BYTE* mvalAddr = ZSTD_selectAddr(matchIdx, idxLowLimit, matchAddress, dummy); |
114 | | /* Note: this used to be written as : return test1 && test2; |
115 | | * Unfortunately, once inlined, these tests become branches, |
116 | | * in which case it becomes critical that they are executed in the right order (test1 then test2). |
117 | | * So we have to write these tests in a specific manner to ensure their ordering. |
118 | | */ |
119 | 121M | if (MEM_read32(currentPtr) != MEM_read32(mvalAddr)) return 0; |
120 | | /* force ordering of these tests, which matters once the function is inlined, as they become branches */ |
121 | 10.8M | #if defined(__GNUC__) |
122 | 10.8M | __asm__(""); |
123 | 10.8M | #endif |
124 | 10.8M | return matchIdx >= idxLowLimit; |
125 | 121M | } |
126 | | |
127 | | static int |
128 | | ZSTD_match4Found_branch(const BYTE* currentPtr, const BYTE* matchAddress, U32 matchIdx, U32 idxLowLimit) |
129 | 82.2M | { |
130 | | /* using a branch instead of a cmov, |
131 | | * because it's faster in scenarios where matchIdx >= idxLowLimit is generally true, |
132 | | * aka almost all candidates are within range */ |
133 | 82.2M | U32 mval; |
134 | 82.2M | if (matchIdx >= idxLowLimit) { |
135 | 59.5M | mval = MEM_read32(matchAddress); |
136 | 59.5M | } else { |
137 | 22.6M | mval = MEM_read32(currentPtr) ^ 1; /* guaranteed to not match. */ |
138 | 22.6M | } |
139 | | |
140 | 82.2M | return (MEM_read32(currentPtr) == mval); |
141 | 82.2M | } |
142 | | |
143 | | |
144 | | /** |
145 | | * If you squint hard enough (and ignore repcodes), the search operation at any |
146 | | * given position is broken into 4 stages: |
147 | | * |
148 | | * 1. Hash (map position to hash value via input read) |
149 | | * 2. Lookup (map hash val to index via hashtable read) |
150 | | * 3. Load (map index to value at that position via input read) |
151 | | * 4. Compare |
152 | | * |
153 | | * Each of these steps involves a memory read at an address which is computed |
154 | | * from the previous step. This means these steps must be sequenced and their |
155 | | * latencies are cumulative. |
156 | | * |
157 | | * Rather than do 1->2->3->4 sequentially for a single position before moving |
158 | | * onto the next, this implementation interleaves these operations across the |
159 | | * next few positions: |
160 | | * |
161 | | * R = Repcode Read & Compare |
162 | | * H = Hash |
163 | | * T = Table Lookup |
164 | | * M = Match Read & Compare |
165 | | * |
166 | | * Pos | Time --> |
167 | | * ----+------------------- |
168 | | * N | ... M |
169 | | * N+1 | ... TM |
170 | | * N+2 | R H T M |
171 | | * N+3 | H TM |
172 | | * N+4 | R H T M |
173 | | * N+5 | H ... |
174 | | * N+6 | R ... |
175 | | * |
176 | | * This is very much analogous to the pipelining of execution in a CPU. And just |
177 | | * like a CPU, we have to dump the pipeline when we find a match (i.e., take a |
178 | | * branch). |
179 | | * |
180 | | * When this happens, we throw away our current state, and do the following prep |
181 | | * to re-enter the loop: |
182 | | * |
183 | | * Pos | Time --> |
184 | | * ----+------------------- |
185 | | * N | H T |
186 | | * N+1 | H |
187 | | * |
188 | | * This is also the work we do at the beginning to enter the loop initially. |
189 | | */ |
190 | | FORCE_INLINE_TEMPLATE |
191 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
192 | | size_t ZSTD_compressBlock_fast_noDict_generic( |
193 | | ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
194 | | void const* src, size_t srcSize, |
195 | | U32 const mls, int useCmov) |
196 | 1.49M | { |
197 | 1.49M | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
198 | 1.49M | U32* const hashTable = ms->hashTable; |
199 | 1.49M | U32 const hlog = cParams->hashLog; |
200 | 1.49M | size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1; /* min 2 */ |
201 | 1.49M | const BYTE* const base = ms->window.base; |
202 | 1.49M | const BYTE* const istart = (const BYTE*)src; |
203 | 1.49M | const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); |
204 | 1.49M | const U32 prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog); |
205 | 1.49M | const BYTE* const prefixStart = base + prefixStartIndex; |
206 | 1.49M | const BYTE* const iend = istart + srcSize; |
207 | 1.49M | const BYTE* const ilimit = iend - HASH_READ_SIZE; |
208 | | |
209 | 1.49M | const BYTE* anchor = istart; |
210 | 1.49M | const BYTE* ip0 = istart; |
211 | 1.49M | const BYTE* ip1; |
212 | 1.49M | const BYTE* ip2; |
213 | 1.49M | const BYTE* ip3; |
214 | 1.49M | U32 current0; |
215 | | |
216 | 1.49M | U32 rep_offset1 = rep[0]; |
217 | 1.49M | U32 rep_offset2 = rep[1]; |
218 | 1.49M | U32 offsetSaved1 = 0, offsetSaved2 = 0; |
219 | | |
220 | 1.49M | size_t hash0; /* hash for ip0 */ |
221 | 1.49M | size_t hash1; /* hash for ip1 */ |
222 | 1.49M | U32 matchIdx; /* match idx for ip0 */ |
223 | | |
224 | 1.49M | U32 offcode; |
225 | 1.49M | const BYTE* match0; |
226 | 1.49M | size_t mLength; |
227 | | |
228 | | /* ip0 and ip1 are always adjacent. The targetLength skipping and |
229 | | * uncompressibility acceleration is applied to every other position, |
230 | | * matching the behavior of #1562. step therefore represents the gap |
231 | | * between pairs of positions, from ip0 to ip2 or ip1 to ip3. */ |
232 | 1.49M | size_t step; |
233 | 1.49M | const BYTE* nextStep; |
234 | 1.49M | const size_t kStepIncr = (1 << (kSearchStrength - 1)); |
235 | 1.49M | const ZSTD_match4Found matchFound = useCmov ? ZSTD_match4Found_cmov : ZSTD_match4Found_branch; |
236 | | |
237 | 1.49M | DEBUGLOG(5, "ZSTD_compressBlock_fast_generic"); |
238 | 1.49M | ip0 += (ip0 == prefixStart); |
239 | 1.49M | { U32 const curr = (U32)(ip0 - base); |
240 | 1.49M | U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog); |
241 | 1.49M | U32 const maxRep = curr - windowLow; |
242 | 1.49M | if (rep_offset2 > maxRep) offsetSaved2 = rep_offset2, rep_offset2 = 0; |
243 | 1.49M | if (rep_offset1 > maxRep) offsetSaved1 = rep_offset1, rep_offset1 = 0; |
244 | 1.49M | } |
245 | | |
246 | | /* start each op */ |
247 | 24.0M | _start: /* Requires: ip0 */ |
248 | | |
249 | 24.0M | step = stepSize; |
250 | 24.0M | nextStep = ip0 + kStepIncr; |
251 | | |
252 | | /* calculate positions, ip0 - anchor == 0, so we skip step calc */ |
253 | 24.0M | ip1 = ip0 + 1; |
254 | 24.0M | ip2 = ip0 + step; |
255 | 24.0M | ip3 = ip2 + 1; |
256 | | |
257 | 24.0M | if (ip3 >= ilimit) { |
258 | 1.30M | goto _cleanup; |
259 | 1.30M | } |
260 | | |
261 | 22.7M | hash0 = ZSTD_hashPtr(ip0, hlog, mls); |
262 | 22.7M | hash1 = ZSTD_hashPtr(ip1, hlog, mls); |
263 | | |
264 | 22.7M | matchIdx = hashTable[hash0]; |
265 | | |
266 | 87.1M | do { |
267 | | /* load repcode match for ip[2]*/ |
268 | 87.1M | const U32 rval = MEM_read32(ip2 - rep_offset1); |
269 | | |
270 | | /* write back hash table entry */ |
271 | 87.1M | current0 = (U32)(ip0 - base); |
272 | 87.1M | hashTable[hash0] = current0; |
273 | | |
274 | | /* check repcode at ip[2] */ |
275 | 87.1M | if ((MEM_read32(ip2) == rval) & (rep_offset1 > 0)) { |
276 | 10.4M | ip0 = ip2; |
277 | 10.4M | match0 = ip0 - rep_offset1; |
278 | 10.4M | mLength = ip0[-1] == match0[-1]; |
279 | 10.4M | ip0 -= mLength; |
280 | 10.4M | match0 -= mLength; |
281 | 10.4M | offcode = REPCODE1_TO_OFFBASE; |
282 | 0 | mLength += 4; |
283 | | |
284 | | /* Write next hash table entry: it's already calculated. |
285 | | * This write is known to be safe because ip1 is before the |
286 | | * repcode (ip2). */ |
287 | 10.4M | hashTable[hash1] = (U32)(ip1 - base); |
288 | | |
289 | 10.4M | goto _match; |
290 | 10.4M | } |
291 | | |
292 | 76.6M | if (matchFound(ip0, base + matchIdx, matchIdx, prefixStartIndex)) { |
293 | | /* Write next hash table entry (it's already calculated). |
294 | | * This write is known to be safe because the ip1 == ip0 + 1, |
295 | | * so searching will resume after ip1 */ |
296 | 8.60M | hashTable[hash1] = (U32)(ip1 - base); |
297 | | |
298 | 8.60M | goto _offset; |
299 | 8.60M | } |
300 | | |
301 | | /* lookup ip[1] */ |
302 | 68.0M | matchIdx = hashTable[hash1]; |
303 | | |
304 | | /* hash ip[2] */ |
305 | 68.0M | hash0 = hash1; |
306 | 68.0M | hash1 = ZSTD_hashPtr(ip2, hlog, mls); |
307 | | |
308 | | /* advance to next positions */ |
309 | 68.0M | ip0 = ip1; |
310 | 68.0M | ip1 = ip2; |
311 | 68.0M | ip2 = ip3; |
312 | | |
313 | | /* write back hash table entry */ |
314 | 68.0M | current0 = (U32)(ip0 - base); |
315 | 68.0M | hashTable[hash0] = current0; |
316 | | |
317 | 68.0M | if (matchFound(ip0, base + matchIdx, matchIdx, prefixStartIndex)) { |
318 | | /* Write next hash table entry, since it's already calculated */ |
319 | 3.49M | if (step <= 4) { |
320 | | /* Avoid writing an index if it's >= position where search will resume. |
321 | | * The minimum possible match has length 4, so search can resume at ip0 + 4. |
322 | | */ |
323 | 3.24M | hashTable[hash1] = (U32)(ip1 - base); |
324 | 3.24M | } |
325 | 3.49M | goto _offset; |
326 | 3.49M | } |
327 | | |
328 | | /* lookup ip[1] */ |
329 | 64.5M | matchIdx = hashTable[hash1]; |
330 | | |
331 | | /* hash ip[2] */ |
332 | 64.5M | hash0 = hash1; |
333 | 64.5M | hash1 = ZSTD_hashPtr(ip2, hlog, mls); |
334 | | |
335 | | /* advance to next positions */ |
336 | 64.5M | ip0 = ip1; |
337 | 64.5M | ip1 = ip2; |
338 | 64.5M | ip2 = ip0 + step; |
339 | 64.5M | ip3 = ip1 + step; |
340 | | |
341 | | /* calculate step */ |
342 | 64.5M | if (ip2 >= nextStep) { |
343 | 2.82M | step++; |
344 | 2.82M | PREFETCH_L1(ip1 + 64); |
345 | 2.82M | PREFETCH_L1(ip1 + 128); |
346 | 2.82M | nextStep += kStepIncr; |
347 | 2.82M | } |
348 | 64.5M | } while (ip3 < ilimit); |
349 | | |
350 | 1.49M | _cleanup: |
351 | | /* Note that there are probably still a couple positions one could search. |
352 | | * However, it seems to be a meaningful performance hit to try to search |
353 | | * them. So let's not. */ |
354 | | |
355 | | /* When the repcodes are outside of the prefix, we set them to zero before the loop. |
356 | | * When the offsets are still zero, we need to restore them after the block to have a correct |
357 | | * repcode history. If only one offset was invalid, it is easy. The tricky case is when both |
358 | | * offsets were invalid. We need to figure out which offset to refill with. |
359 | | * - If both offsets are zero they are in the same order. |
360 | | * - If both offsets are non-zero, we won't restore the offsets from `offsetSaved[12]`. |
361 | | * - If only one is zero, we need to decide which offset to restore. |
362 | | * - If rep_offset1 is non-zero, then rep_offset2 must be offsetSaved1. |
363 | | * - It is impossible for rep_offset2 to be non-zero. |
364 | | * |
365 | | * So if rep_offset1 started invalid (offsetSaved1 != 0) and became valid (rep_offset1 != 0), then |
366 | | * set rep[0] = rep_offset1 and rep[1] = offsetSaved1. |
367 | | */ |
368 | 1.49M | offsetSaved2 = ((offsetSaved1 != 0) && (rep_offset1 != 0)) ? offsetSaved1 : offsetSaved2; |
369 | | |
370 | | /* save reps for next block */ |
371 | 1.49M | rep[0] = rep_offset1 ? rep_offset1 : offsetSaved1; |
372 | 1.49M | rep[1] = rep_offset2 ? rep_offset2 : offsetSaved2; |
373 | | |
374 | | /* Return the last literals size */ |
375 | 1.49M | return (size_t)(iend - anchor); |
376 | | |
377 | 12.0M | _offset: /* Requires: ip0, idx */ |
378 | | |
379 | | /* Compute the offset code. */ |
380 | 12.0M | match0 = base + matchIdx; |
381 | 12.0M | rep_offset2 = rep_offset1; |
382 | 12.0M | rep_offset1 = (U32)(ip0-match0); |
383 | 12.0M | offcode = OFFSET_TO_OFFBASE(rep_offset1); |
384 | 0 | mLength = 4; |
385 | | |
386 | | /* Count the backwards match length. */ |
387 | 18.5M | while (((ip0>anchor) & (match0>prefixStart)) && (ip0[-1] == match0[-1])) { |
388 | 6.43M | ip0--; |
389 | 6.43M | match0--; |
390 | 6.43M | mLength++; |
391 | 6.43M | } |
392 | | |
393 | 22.5M | _match: /* Requires: ip0, match0, offcode */ |
394 | | |
395 | | /* Count the forward length. */ |
396 | 22.5M | mLength += ZSTD_count(ip0 + mLength, match0 + mLength, iend); |
397 | | |
398 | 22.5M | ZSTD_storeSeq(seqStore, (size_t)(ip0 - anchor), anchor, iend, offcode, mLength); |
399 | | |
400 | 22.5M | ip0 += mLength; |
401 | 22.5M | anchor = ip0; |
402 | | |
403 | | /* Fill table and check for immediate repcode. */ |
404 | 22.5M | if (ip0 <= ilimit) { |
405 | | /* Fill Table */ |
406 | 22.2M | assert(base+current0+2 > istart); /* check base overflow */ |
407 | 22.2M | hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */ |
408 | 22.2M | hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base); |
409 | | |
410 | 22.2M | if (rep_offset2 > 0) { /* rep_offset2==0 means rep_offset2 is invalidated */ |
411 | 25.3M | while ( (ip0 <= ilimit) && (MEM_read32(ip0) == MEM_read32(ip0 - rep_offset2)) ) { |
412 | | /* store sequence */ |
413 | 3.36M | size_t const rLength = ZSTD_count(ip0+4, ip0+4-rep_offset2, iend) + 4; |
414 | 3.36M | { U32 const tmpOff = rep_offset2; rep_offset2 = rep_offset1; rep_offset1 = tmpOff; } /* swap rep_offset2 <=> rep_offset1 */ |
415 | 3.36M | hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base); |
416 | 3.36M | ip0 += rLength; |
417 | 3.36M | ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, REPCODE1_TO_OFFBASE, rLength); |
418 | 0 | anchor = ip0; |
419 | 3.36M | continue; /* faster when present (confirmed on gcc-8) ... (?) */ |
420 | 3.36M | } } } |
421 | | |
422 | 22.5M | goto _start; |
423 | 22.5M | } |
424 | | |
425 | | #define ZSTD_GEN_FAST_FN(dictMode, mml, cmov) \ |
426 | | static size_t ZSTD_compressBlock_fast_##dictMode##_##mml##_##cmov( \ |
427 | | ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], \ |
428 | | void const* src, size_t srcSize) \ |
429 | 2.95M | { \ |
430 | 2.95M | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ |
431 | 2.95M | } zstd_fast.c:ZSTD_compressBlock_fast_noDict_4_1 Line | Count | Source | 429 | 437k | { \ | 430 | 437k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 437k | } |
zstd_fast.c:ZSTD_compressBlock_fast_noDict_5_1 Line | Count | Source | 429 | 408k | { \ | 430 | 408k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 408k | } |
zstd_fast.c:ZSTD_compressBlock_fast_noDict_6_1 Line | Count | Source | 429 | 343k | { \ | 430 | 343k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 343k | } |
zstd_fast.c:ZSTD_compressBlock_fast_noDict_7_1 Line | Count | Source | 429 | 284k | { \ | 430 | 284k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 284k | } |
Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_noDict_4_0 Unexecuted instantiation: zstd_fast.c:ZSTD_compressBlock_fast_noDict_5_0 zstd_fast.c:ZSTD_compressBlock_fast_noDict_6_0 Line | Count | Source | 429 | 5.89k | { \ | 430 | 5.89k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 5.89k | } |
zstd_fast.c:ZSTD_compressBlock_fast_noDict_7_0 Line | Count | Source | 429 | 12.1k | { \ | 430 | 12.1k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 12.1k | } |
zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_4_0 Line | Count | Source | 429 | 37.5k | { \ | 430 | 37.5k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 37.5k | } |
zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_5_0 Line | Count | Source | 429 | 1.11M | { \ | 430 | 1.11M | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 1.11M | } |
zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_6_0 Line | Count | Source | 429 | 71.8k | { \ | 430 | 71.8k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 71.8k | } |
zstd_fast.c:ZSTD_compressBlock_fast_dictMatchState_7_0 Line | Count | Source | 429 | 23.5k | { \ | 430 | 23.5k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 23.5k | } |
zstd_fast.c:ZSTD_compressBlock_fast_extDict_4_0 Line | Count | Source | 429 | 65.5k | { \ | 430 | 65.5k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 65.5k | } |
zstd_fast.c:ZSTD_compressBlock_fast_extDict_5_0 Line | Count | Source | 429 | 64.3k | { \ | 430 | 64.3k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 64.3k | } |
zstd_fast.c:ZSTD_compressBlock_fast_extDict_6_0 Line | Count | Source | 429 | 54.3k | { \ | 430 | 54.3k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 54.3k | } |
zstd_fast.c:ZSTD_compressBlock_fast_extDict_7_0 Line | Count | Source | 429 | 27.3k | { \ | 430 | 27.3k | return ZSTD_compressBlock_fast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mml, cmov); \ | 431 | 27.3k | } |
|
432 | | |
433 | | ZSTD_GEN_FAST_FN(noDict, 4, 1) |
434 | | ZSTD_GEN_FAST_FN(noDict, 5, 1) |
435 | | ZSTD_GEN_FAST_FN(noDict, 6, 1) |
436 | | ZSTD_GEN_FAST_FN(noDict, 7, 1) |
437 | | |
438 | | ZSTD_GEN_FAST_FN(noDict, 4, 0) |
439 | | ZSTD_GEN_FAST_FN(noDict, 5, 0) |
440 | | ZSTD_GEN_FAST_FN(noDict, 6, 0) |
441 | | ZSTD_GEN_FAST_FN(noDict, 7, 0) |
442 | | |
443 | | size_t ZSTD_compressBlock_fast( |
444 | | ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
445 | | void const* src, size_t srcSize) |
446 | 1.49M | { |
447 | 1.49M | U32 const mml = ms->cParams.minMatch; |
448 | | /* use cmov when "candidate in range" branch is likely unpredictable */ |
449 | 1.49M | int const useCmov = ms->cParams.windowLog < 19; |
450 | 1.49M | assert(ms->dictMatchState == NULL); |
451 | 1.49M | if (useCmov) { |
452 | 1.47M | switch(mml) |
453 | 1.47M | { |
454 | 390k | default: /* includes case 3 */ |
455 | 437k | case 4 : |
456 | 437k | return ZSTD_compressBlock_fast_noDict_4_1(ms, seqStore, rep, src, srcSize); |
457 | 408k | case 5 : |
458 | 408k | return ZSTD_compressBlock_fast_noDict_5_1(ms, seqStore, rep, src, srcSize); |
459 | 343k | case 6 : |
460 | 343k | return ZSTD_compressBlock_fast_noDict_6_1(ms, seqStore, rep, src, srcSize); |
461 | 284k | case 7 : |
462 | 284k | return ZSTD_compressBlock_fast_noDict_7_1(ms, seqStore, rep, src, srcSize); |
463 | 1.47M | } |
464 | 1.47M | } else { |
465 | | /* use a branch instead */ |
466 | 18.0k | switch(mml) |
467 | 18.0k | { |
468 | 0 | default: /* includes case 3 */ |
469 | 0 | case 4 : |
470 | 0 | return ZSTD_compressBlock_fast_noDict_4_0(ms, seqStore, rep, src, srcSize); |
471 | 0 | case 5 : |
472 | 0 | return ZSTD_compressBlock_fast_noDict_5_0(ms, seqStore, rep, src, srcSize); |
473 | 5.89k | case 6 : |
474 | 5.89k | return ZSTD_compressBlock_fast_noDict_6_0(ms, seqStore, rep, src, srcSize); |
475 | 12.1k | case 7 : |
476 | 12.1k | return ZSTD_compressBlock_fast_noDict_7_0(ms, seqStore, rep, src, srcSize); |
477 | 18.0k | } |
478 | 18.0k | } |
479 | 1.49M | } |
480 | | |
481 | | FORCE_INLINE_TEMPLATE |
482 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
483 | | size_t ZSTD_compressBlock_fast_dictMatchState_generic( |
484 | | ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
485 | | void const* src, size_t srcSize, U32 const mls, U32 const hasStep) |
486 | 1.25M | { |
487 | 1.25M | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
488 | 1.25M | U32* const hashTable = ms->hashTable; |
489 | 1.25M | U32 const hlog = cParams->hashLog; |
490 | | /* support stepSize of 0 */ |
491 | 1.25M | U32 const stepSize = cParams->targetLength + !(cParams->targetLength); |
492 | 1.25M | const BYTE* const base = ms->window.base; |
493 | 1.25M | const BYTE* const istart = (const BYTE*)src; |
494 | 1.25M | const BYTE* ip0 = istart; |
495 | 1.25M | const BYTE* ip1 = ip0 + stepSize; /* we assert below that stepSize >= 1 */ |
496 | 1.25M | const BYTE* anchor = istart; |
497 | 1.25M | const U32 prefixStartIndex = ms->window.dictLimit; |
498 | 1.25M | const BYTE* const prefixStart = base + prefixStartIndex; |
499 | 1.25M | const BYTE* const iend = istart + srcSize; |
500 | 1.25M | const BYTE* const ilimit = iend - HASH_READ_SIZE; |
501 | 1.25M | U32 offset_1=rep[0], offset_2=rep[1]; |
502 | | |
503 | 1.25M | const ZSTD_MatchState_t* const dms = ms->dictMatchState; |
504 | 1.25M | const ZSTD_compressionParameters* const dictCParams = &dms->cParams ; |
505 | 1.25M | const U32* const dictHashTable = dms->hashTable; |
506 | 1.25M | const U32 dictStartIndex = dms->window.dictLimit; |
507 | 1.25M | const BYTE* const dictBase = dms->window.base; |
508 | 1.25M | const BYTE* const dictStart = dictBase + dictStartIndex; |
509 | 1.25M | const BYTE* const dictEnd = dms->window.nextSrc; |
510 | 1.25M | const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase); |
511 | 1.25M | const U32 dictAndPrefixLength = (U32)(istart - prefixStart + dictEnd - dictStart); |
512 | 1.25M | const U32 dictHBits = dictCParams->hashLog + ZSTD_SHORT_CACHE_TAG_BITS; |
513 | | |
514 | | /* if a dictionary is still attached, it necessarily means that |
515 | | * it is within window size. So we just check it. */ |
516 | 1.25M | const U32 maxDistance = 1U << cParams->windowLog; |
517 | 1.25M | const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); |
518 | 1.25M | assert(endIndex - prefixStartIndex <= maxDistance); |
519 | 1.25M | (void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */ |
520 | | |
521 | 1.25M | (void)hasStep; /* not currently specialized on whether it's accelerated */ |
522 | | |
523 | | /* ensure there will be no underflow |
524 | | * when translating a dict index into a local index */ |
525 | 1.25M | assert(prefixStartIndex >= (U32)(dictEnd - dictBase)); |
526 | | |
527 | 1.25M | if (ms->prefetchCDictTables) { |
528 | 22.4k | size_t const hashTableBytes = (((size_t)1) << dictCParams->hashLog) * sizeof(U32); |
529 | 22.4k | PREFETCH_AREA(dictHashTable, hashTableBytes); |
530 | 22.4k | } |
531 | | |
532 | | /* init */ |
533 | 1.25M | DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic"); |
534 | 1.25M | ip0 += (dictAndPrefixLength == 0); |
535 | | /* dictMatchState repCode checks don't currently handle repCode == 0 |
536 | | * disabling. */ |
537 | 1.25M | assert(offset_1 <= dictAndPrefixLength); |
538 | 1.25M | assert(offset_2 <= dictAndPrefixLength); |
539 | | |
540 | | /* Outer search loop */ |
541 | 1.25M | assert(stepSize >= 1); |
542 | 17.0M | while (ip1 <= ilimit) { /* repcode check at (ip0 + 1) is safe because ip0 < ip1 */ |
543 | 15.8M | size_t mLength; |
544 | 15.8M | size_t hash0 = ZSTD_hashPtr(ip0, hlog, mls); |
545 | | |
546 | 15.8M | size_t const dictHashAndTag0 = ZSTD_hashPtr(ip0, dictHBits, mls); |
547 | 15.8M | U32 dictMatchIndexAndTag = dictHashTable[dictHashAndTag0 >> ZSTD_SHORT_CACHE_TAG_BITS]; |
548 | 15.8M | int dictTagsMatch = ZSTD_comparePackedTags(dictMatchIndexAndTag, dictHashAndTag0); |
549 | | |
550 | 15.8M | U32 matchIndex = hashTable[hash0]; |
551 | 15.8M | U32 curr = (U32)(ip0 - base); |
552 | 15.8M | size_t step = stepSize; |
553 | 15.8M | const size_t kStepIncr = 1 << kSearchStrength; |
554 | 15.8M | const BYTE* nextStep = ip0 + kStepIncr; |
555 | | |
556 | | /* Inner search loop */ |
557 | 70.0M | while (1) { |
558 | 70.0M | const BYTE* match = base + matchIndex; |
559 | 70.0M | const U32 repIndex = curr + 1 - offset_1; |
560 | 70.0M | const BYTE* repMatch = (repIndex < prefixStartIndex) ? |
561 | 48.0M | dictBase + (repIndex - dictIndexDelta) : |
562 | 70.0M | base + repIndex; |
563 | 70.0M | const size_t hash1 = ZSTD_hashPtr(ip1, hlog, mls); |
564 | 70.0M | size_t const dictHashAndTag1 = ZSTD_hashPtr(ip1, dictHBits, mls); |
565 | 70.0M | hashTable[hash0] = curr; /* update hash table */ |
566 | | |
567 | 70.0M | if ((ZSTD_index_overlap_check(prefixStartIndex, repIndex)) |
568 | 70.0M | && (MEM_read32(repMatch) == MEM_read32(ip0 + 1))) { |
569 | 2.46M | const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; |
570 | 2.46M | mLength = ZSTD_count_2segments(ip0 + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4; |
571 | 2.46M | ip0++; |
572 | 2.46M | ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, REPCODE1_TO_OFFBASE, mLength); |
573 | 0 | break; |
574 | 2.46M | } |
575 | | |
576 | 67.5M | if (dictTagsMatch) { |
577 | | /* Found a possible dict match */ |
578 | 13.2M | const U32 dictMatchIndex = dictMatchIndexAndTag >> ZSTD_SHORT_CACHE_TAG_BITS; |
579 | 13.2M | const BYTE* dictMatch = dictBase + dictMatchIndex; |
580 | 13.2M | if (dictMatchIndex > dictStartIndex && |
581 | 13.2M | MEM_read32(dictMatch) == MEM_read32(ip0)) { |
582 | | /* To replicate extDict parse behavior, we only use dict matches when the normal matchIndex is invalid */ |
583 | 12.9M | if (matchIndex <= prefixStartIndex) { |
584 | 8.52M | U32 const offset = (U32) (curr - dictMatchIndex - dictIndexDelta); |
585 | 8.52M | mLength = ZSTD_count_2segments(ip0 + 4, dictMatch + 4, iend, dictEnd, prefixStart) + 4; |
586 | 12.2M | while (((ip0 > anchor) & (dictMatch > dictStart)) |
587 | 12.2M | && (ip0[-1] == dictMatch[-1])) { |
588 | 3.70M | ip0--; |
589 | 3.70M | dictMatch--; |
590 | 3.70M | mLength++; |
591 | 3.70M | } /* catch up */ |
592 | 8.52M | offset_2 = offset_1; |
593 | 8.52M | offset_1 = offset; |
594 | 8.52M | ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength); |
595 | 0 | break; |
596 | 8.52M | } |
597 | 12.9M | } |
598 | 13.2M | } |
599 | | |
600 | 59.0M | if (ZSTD_match4Found_cmov(ip0, match, matchIndex, prefixStartIndex)) { |
601 | | /* found a regular match of size >= 4 */ |
602 | 4.81M | U32 const offset = (U32) (ip0 - match); |
603 | 4.81M | mLength = ZSTD_count(ip0 + 4, match + 4, iend) + 4; |
604 | 5.05M | while (((ip0 > anchor) & (match > prefixStart)) |
605 | 5.05M | && (ip0[-1] == match[-1])) { |
606 | 240k | ip0--; |
607 | 240k | match--; |
608 | 240k | mLength++; |
609 | 240k | } /* catch up */ |
610 | 4.81M | offset_2 = offset_1; |
611 | 4.81M | offset_1 = offset; |
612 | 4.81M | ZSTD_storeSeq(seqStore, (size_t) (ip0 - anchor), anchor, iend, OFFSET_TO_OFFBASE(offset), mLength); |
613 | 0 | break; |
614 | 4.81M | } |
615 | | |
616 | | /* Prepare for next iteration */ |
617 | 54.2M | dictMatchIndexAndTag = dictHashTable[dictHashAndTag1 >> ZSTD_SHORT_CACHE_TAG_BITS]; |
618 | 54.2M | dictTagsMatch = ZSTD_comparePackedTags(dictMatchIndexAndTag, dictHashAndTag1); |
619 | 54.2M | matchIndex = hashTable[hash1]; |
620 | | |
621 | 54.2M | if (ip1 >= nextStep) { |
622 | 63.3k | step++; |
623 | 63.3k | nextStep += kStepIncr; |
624 | 63.3k | } |
625 | 54.2M | ip0 = ip1; |
626 | 54.2M | ip1 = ip1 + step; |
627 | 54.2M | if (ip1 > ilimit) goto _cleanup; |
628 | | |
629 | 54.1M | curr = (U32)(ip0 - base); |
630 | 54.1M | hash0 = hash1; |
631 | 54.1M | } /* end inner search loop */ |
632 | | |
633 | | /* match found */ |
634 | 15.8M | assert(mLength); |
635 | 15.8M | ip0 += mLength; |
636 | 15.8M | anchor = ip0; |
637 | | |
638 | 15.8M | if (ip0 <= ilimit) { |
639 | | /* Fill Table */ |
640 | 14.8M | assert(base+curr+2 > istart); /* check base overflow */ |
641 | 14.8M | hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2; /* here because curr+2 could be > iend-8 */ |
642 | 14.8M | hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base); |
643 | | |
644 | | /* check immediate repcode */ |
645 | 16.1M | while (ip0 <= ilimit) { |
646 | 16.0M | U32 const current2 = (U32)(ip0-base); |
647 | 16.0M | U32 const repIndex2 = current2 - offset_2; |
648 | 16.0M | const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? |
649 | 8.08M | dictBase - dictIndexDelta + repIndex2 : |
650 | 16.0M | base + repIndex2; |
651 | 16.0M | if ( (ZSTD_index_overlap_check(prefixStartIndex, repIndex2)) |
652 | 16.0M | && (MEM_read32(repMatch2) == MEM_read32(ip0))) { |
653 | 1.31M | const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; |
654 | 1.31M | size_t const repLength2 = ZSTD_count_2segments(ip0+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; |
655 | 1.31M | U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ |
656 | 1.31M | ZSTD_storeSeq(seqStore, 0, anchor, iend, REPCODE1_TO_OFFBASE, repLength2); |
657 | 0 | hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = current2; |
658 | 1.31M | ip0 += repLength2; |
659 | 1.31M | anchor = ip0; |
660 | 1.31M | continue; |
661 | 1.31M | } |
662 | 14.7M | break; |
663 | 16.0M | } |
664 | 14.8M | } |
665 | | |
666 | | /* Prepare for next iteration */ |
667 | 15.8M | assert(ip0 == anchor); |
668 | 15.8M | ip1 = ip0 + stepSize; |
669 | 15.8M | } |
670 | | |
671 | 1.25M | _cleanup: |
672 | | /* save reps for next block */ |
673 | 1.25M | rep[0] = offset_1; |
674 | 1.25M | rep[1] = offset_2; |
675 | | |
676 | | /* Return the last literals size */ |
677 | 1.25M | return (size_t)(iend - anchor); |
678 | 1.25M | } |
679 | | |
680 | | |
681 | | ZSTD_GEN_FAST_FN(dictMatchState, 4, 0) |
682 | | ZSTD_GEN_FAST_FN(dictMatchState, 5, 0) |
683 | | ZSTD_GEN_FAST_FN(dictMatchState, 6, 0) |
684 | | ZSTD_GEN_FAST_FN(dictMatchState, 7, 0) |
685 | | |
686 | | size_t ZSTD_compressBlock_fast_dictMatchState( |
687 | | ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
688 | | void const* src, size_t srcSize) |
689 | 1.25M | { |
690 | 1.25M | U32 const mls = ms->cParams.minMatch; |
691 | 1.25M | assert(ms->dictMatchState != NULL); |
692 | 1.25M | switch(mls) |
693 | 1.25M | { |
694 | 23.7k | default: /* includes case 3 */ |
695 | 37.5k | case 4 : |
696 | 37.5k | return ZSTD_compressBlock_fast_dictMatchState_4_0(ms, seqStore, rep, src, srcSize); |
697 | 1.11M | case 5 : |
698 | 1.11M | return ZSTD_compressBlock_fast_dictMatchState_5_0(ms, seqStore, rep, src, srcSize); |
699 | 71.8k | case 6 : |
700 | 71.8k | return ZSTD_compressBlock_fast_dictMatchState_6_0(ms, seqStore, rep, src, srcSize); |
701 | 23.5k | case 7 : |
702 | 23.5k | return ZSTD_compressBlock_fast_dictMatchState_7_0(ms, seqStore, rep, src, srcSize); |
703 | 1.25M | } |
704 | 1.25M | } |
705 | | |
706 | | |
707 | | static |
708 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
709 | | size_t ZSTD_compressBlock_fast_extDict_generic( |
710 | | ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
711 | | void const* src, size_t srcSize, U32 const mls, U32 const hasStep) |
712 | 211k | { |
713 | 211k | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
714 | 211k | U32* const hashTable = ms->hashTable; |
715 | 211k | U32 const hlog = cParams->hashLog; |
716 | | /* support stepSize of 0 */ |
717 | 211k | size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1; |
718 | 211k | const BYTE* const base = ms->window.base; |
719 | 211k | const BYTE* const dictBase = ms->window.dictBase; |
720 | 211k | const BYTE* const istart = (const BYTE*)src; |
721 | 211k | const BYTE* anchor = istart; |
722 | 211k | const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); |
723 | 211k | const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog); |
724 | 211k | const U32 dictStartIndex = lowLimit; |
725 | 211k | const BYTE* const dictStart = dictBase + dictStartIndex; |
726 | 211k | const U32 dictLimit = ms->window.dictLimit; |
727 | 211k | const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit; |
728 | 211k | const BYTE* const prefixStart = base + prefixStartIndex; |
729 | 211k | const BYTE* const dictEnd = dictBase + prefixStartIndex; |
730 | 211k | const BYTE* const iend = istart + srcSize; |
731 | 211k | const BYTE* const ilimit = iend - 8; |
732 | 211k | U32 offset_1=rep[0], offset_2=rep[1]; |
733 | 211k | U32 offsetSaved1 = 0, offsetSaved2 = 0; |
734 | | |
735 | 211k | const BYTE* ip0 = istart; |
736 | 211k | const BYTE* ip1; |
737 | 211k | const BYTE* ip2; |
738 | 211k | const BYTE* ip3; |
739 | 211k | U32 current0; |
740 | | |
741 | | |
742 | 211k | size_t hash0; /* hash for ip0 */ |
743 | 211k | size_t hash1; /* hash for ip1 */ |
744 | 211k | U32 idx; /* match idx for ip0 */ |
745 | 211k | const BYTE* idxBase; /* base pointer for idx */ |
746 | | |
747 | 211k | U32 offcode; |
748 | 211k | const BYTE* match0; |
749 | 211k | size_t mLength; |
750 | 211k | const BYTE* matchEnd = 0; /* initialize to avoid warning, assert != 0 later */ |
751 | | |
752 | 211k | size_t step; |
753 | 211k | const BYTE* nextStep; |
754 | 211k | const size_t kStepIncr = (1 << (kSearchStrength - 1)); |
755 | | |
756 | 211k | (void)hasStep; /* not currently specialized on whether it's accelerated */ |
757 | | |
758 | 211k | DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic (offset_1=%u)", offset_1); |
759 | | |
760 | | /* switch to "regular" variant if extDict is invalidated due to maxDistance */ |
761 | 211k | if (prefixStartIndex == dictStartIndex) |
762 | 26.6k | return ZSTD_compressBlock_fast(ms, seqStore, rep, src, srcSize); |
763 | | |
764 | 185k | { U32 const curr = (U32)(ip0 - base); |
765 | 185k | U32 const maxRep = curr - dictStartIndex; |
766 | 185k | if (offset_2 >= maxRep) offsetSaved2 = offset_2, offset_2 = 0; |
767 | 185k | if (offset_1 >= maxRep) offsetSaved1 = offset_1, offset_1 = 0; |
768 | 185k | } |
769 | | |
770 | | /* start each op */ |
771 | 3.64M | _start: /* Requires: ip0 */ |
772 | | |
773 | 3.64M | step = stepSize; |
774 | 3.64M | nextStep = ip0 + kStepIncr; |
775 | | |
776 | | /* calculate positions, ip0 - anchor == 0, so we skip step calc */ |
777 | 3.64M | ip1 = ip0 + 1; |
778 | 3.64M | ip2 = ip0 + step; |
779 | 3.64M | ip3 = ip2 + 1; |
780 | | |
781 | 3.64M | if (ip3 >= ilimit) { |
782 | 170k | goto _cleanup; |
783 | 170k | } |
784 | | |
785 | 3.47M | hash0 = ZSTD_hashPtr(ip0, hlog, mls); |
786 | 3.47M | hash1 = ZSTD_hashPtr(ip1, hlog, mls); |
787 | | |
788 | 3.47M | idx = hashTable[hash0]; |
789 | 3.47M | idxBase = idx < prefixStartIndex ? dictBase : base; |
790 | | |
791 | 11.3M | do { |
792 | 11.3M | { /* load repcode match for ip[2] */ |
793 | 11.3M | U32 const current2 = (U32)(ip2 - base); |
794 | 11.3M | U32 const repIndex = current2 - offset_1; |
795 | 11.3M | const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base; |
796 | 11.3M | U32 rval; |
797 | 11.3M | if ( ((U32)(prefixStartIndex - repIndex) >= 4) /* intentional underflow */ |
798 | 11.3M | & (offset_1 > 0) ) { |
799 | 11.3M | rval = MEM_read32(repBase + repIndex); |
800 | 11.3M | } else { |
801 | 14.5k | rval = MEM_read32(ip2) ^ 1; /* guaranteed to not match. */ |
802 | 14.5k | } |
803 | | |
804 | | /* write back hash table entry */ |
805 | 11.3M | current0 = (U32)(ip0 - base); |
806 | 11.3M | hashTable[hash0] = current0; |
807 | | |
808 | | /* check repcode at ip[2] */ |
809 | 11.3M | if (MEM_read32(ip2) == rval) { |
810 | 1.33M | ip0 = ip2; |
811 | 1.33M | match0 = repBase + repIndex; |
812 | 1.33M | matchEnd = repIndex < prefixStartIndex ? dictEnd : iend; |
813 | 1.33M | assert((match0 != prefixStart) & (match0 != dictStart)); |
814 | 1.33M | mLength = ip0[-1] == match0[-1]; |
815 | 1.33M | ip0 -= mLength; |
816 | 1.33M | match0 -= mLength; |
817 | 1.33M | offcode = REPCODE1_TO_OFFBASE; |
818 | 0 | mLength += 4; |
819 | 1.33M | goto _match; |
820 | 1.33M | } } |
821 | | |
822 | 9.99M | { /* load match for ip[0] */ |
823 | 9.99M | U32 const mval = idx >= dictStartIndex ? |
824 | 8.31M | MEM_read32(idxBase + idx) : |
825 | 9.99M | MEM_read32(ip0) ^ 1; /* guaranteed not to match */ |
826 | | |
827 | | /* check match at ip[0] */ |
828 | 9.99M | if (MEM_read32(ip0) == mval) { |
829 | | /* found a match! */ |
830 | 1.52M | goto _offset; |
831 | 1.52M | } } |
832 | | |
833 | | /* lookup ip[1] */ |
834 | 8.46M | idx = hashTable[hash1]; |
835 | 8.46M | idxBase = idx < prefixStartIndex ? dictBase : base; |
836 | | |
837 | | /* hash ip[2] */ |
838 | 8.46M | hash0 = hash1; |
839 | 8.46M | hash1 = ZSTD_hashPtr(ip2, hlog, mls); |
840 | | |
841 | | /* advance to next positions */ |
842 | 8.46M | ip0 = ip1; |
843 | 8.46M | ip1 = ip2; |
844 | 8.46M | ip2 = ip3; |
845 | | |
846 | | /* write back hash table entry */ |
847 | 8.46M | current0 = (U32)(ip0 - base); |
848 | 8.46M | hashTable[hash0] = current0; |
849 | | |
850 | 8.46M | { /* load match for ip[0] */ |
851 | 8.46M | U32 const mval = idx >= dictStartIndex ? |
852 | 6.93M | MEM_read32(idxBase + idx) : |
853 | 8.46M | MEM_read32(ip0) ^ 1; /* guaranteed not to match */ |
854 | | |
855 | | /* check match at ip[0] */ |
856 | 8.46M | if (MEM_read32(ip0) == mval) { |
857 | | /* found a match! */ |
858 | 594k | goto _offset; |
859 | 594k | } } |
860 | | |
861 | | /* lookup ip[1] */ |
862 | 7.87M | idx = hashTable[hash1]; |
863 | 7.87M | idxBase = idx < prefixStartIndex ? dictBase : base; |
864 | | |
865 | | /* hash ip[2] */ |
866 | 7.87M | hash0 = hash1; |
867 | 7.87M | hash1 = ZSTD_hashPtr(ip2, hlog, mls); |
868 | | |
869 | | /* advance to next positions */ |
870 | 7.87M | ip0 = ip1; |
871 | 7.87M | ip1 = ip2; |
872 | 7.87M | ip2 = ip0 + step; |
873 | 7.87M | ip3 = ip1 + step; |
874 | | |
875 | | /* calculate step */ |
876 | 7.87M | if (ip2 >= nextStep) { |
877 | 278k | step++; |
878 | 278k | PREFETCH_L1(ip1 + 64); |
879 | 278k | PREFETCH_L1(ip1 + 128); |
880 | 278k | nextStep += kStepIncr; |
881 | 278k | } |
882 | 7.87M | } while (ip3 < ilimit); |
883 | | |
884 | 185k | _cleanup: |
885 | | /* Note that there are probably still a couple positions we could search. |
886 | | * However, it seems to be a meaningful performance hit to try to search |
887 | | * them. So let's not. */ |
888 | | |
889 | | /* If offset_1 started invalid (offsetSaved1 != 0) and became valid (offset_1 != 0), |
890 | | * rotate saved offsets. See comment in ZSTD_compressBlock_fast_noDict for more context. */ |
891 | 185k | offsetSaved2 = ((offsetSaved1 != 0) && (offset_1 != 0)) ? offsetSaved1 : offsetSaved2; |
892 | | |
893 | | /* save reps for next block */ |
894 | 185k | rep[0] = offset_1 ? offset_1 : offsetSaved1; |
895 | 185k | rep[1] = offset_2 ? offset_2 : offsetSaved2; |
896 | | |
897 | | /* Return the last literals size */ |
898 | 185k | return (size_t)(iend - anchor); |
899 | | |
900 | 2.11M | _offset: /* Requires: ip0, idx, idxBase */ |
901 | | |
902 | | /* Compute the offset code. */ |
903 | 2.11M | { U32 const offset = current0 - idx; |
904 | 2.11M | const BYTE* const lowMatchPtr = idx < prefixStartIndex ? dictStart : prefixStart; |
905 | 2.11M | matchEnd = idx < prefixStartIndex ? dictEnd : iend; |
906 | 2.11M | match0 = idxBase + idx; |
907 | 2.11M | offset_2 = offset_1; |
908 | 2.11M | offset_1 = offset; |
909 | 2.11M | offcode = OFFSET_TO_OFFBASE(offset); |
910 | 0 | mLength = 4; |
911 | | |
912 | | /* Count the backwards match length. */ |
913 | 3.66M | while (((ip0>anchor) & (match0>lowMatchPtr)) && (ip0[-1] == match0[-1])) { |
914 | 1.54M | ip0--; |
915 | 1.54M | match0--; |
916 | 1.54M | mLength++; |
917 | 1.54M | } } |
918 | | |
919 | 3.45M | _match: /* Requires: ip0, match0, offcode, matchEnd */ |
920 | | |
921 | | /* Count the forward length. */ |
922 | 3.45M | assert(matchEnd != 0); |
923 | 3.45M | mLength += ZSTD_count_2segments(ip0 + mLength, match0 + mLength, iend, matchEnd, prefixStart); |
924 | | |
925 | 3.45M | ZSTD_storeSeq(seqStore, (size_t)(ip0 - anchor), anchor, iend, offcode, mLength); |
926 | | |
927 | 3.45M | ip0 += mLength; |
928 | 3.45M | anchor = ip0; |
929 | | |
930 | | /* write next hash table entry */ |
931 | 3.45M | if (ip1 < ip0) { |
932 | 3.44M | hashTable[hash1] = (U32)(ip1 - base); |
933 | 3.44M | } |
934 | | |
935 | | /* Fill table and check for immediate repcode. */ |
936 | 3.45M | if (ip0 <= ilimit) { |
937 | | /* Fill Table */ |
938 | 3.42M | assert(base+current0+2 > istart); /* check base overflow */ |
939 | 3.42M | hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */ |
940 | 3.42M | hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base); |
941 | | |
942 | 3.66M | while (ip0 <= ilimit) { |
943 | 3.66M | U32 const repIndex2 = (U32)(ip0-base) - offset_2; |
944 | 3.66M | const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2; |
945 | 3.66M | if ( ((ZSTD_index_overlap_check(prefixStartIndex, repIndex2)) & (offset_2 > 0)) |
946 | 3.66M | && (MEM_read32(repMatch2) == MEM_read32(ip0)) ) { |
947 | 236k | const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; |
948 | 236k | size_t const repLength2 = ZSTD_count_2segments(ip0+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; |
949 | 236k | { U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */ |
950 | 236k | ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, REPCODE1_TO_OFFBASE, repLength2); |
951 | 0 | hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base); |
952 | 236k | ip0 += repLength2; |
953 | 236k | anchor = ip0; |
954 | 236k | continue; |
955 | 236k | } |
956 | 3.42M | break; |
957 | 3.66M | } } |
958 | | |
959 | 3.45M | goto _start; |
960 | 3.45M | } |
961 | | |
962 | | ZSTD_GEN_FAST_FN(extDict, 4, 0) |
963 | | ZSTD_GEN_FAST_FN(extDict, 5, 0) |
964 | | ZSTD_GEN_FAST_FN(extDict, 6, 0) |
965 | | ZSTD_GEN_FAST_FN(extDict, 7, 0) |
966 | | |
967 | | size_t ZSTD_compressBlock_fast_extDict( |
968 | | ZSTD_MatchState_t* ms, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
969 | | void const* src, size_t srcSize) |
970 | 211k | { |
971 | 211k | U32 const mls = ms->cParams.minMatch; |
972 | 211k | assert(ms->dictMatchState == NULL); |
973 | 211k | switch(mls) |
974 | 211k | { |
975 | 58.4k | default: /* includes case 3 */ |
976 | 65.5k | case 4 : |
977 | 65.5k | return ZSTD_compressBlock_fast_extDict_4_0(ms, seqStore, rep, src, srcSize); |
978 | 64.3k | case 5 : |
979 | 64.3k | return ZSTD_compressBlock_fast_extDict_5_0(ms, seqStore, rep, src, srcSize); |
980 | 54.3k | case 6 : |
981 | 54.3k | return ZSTD_compressBlock_fast_extDict_6_0(ms, seqStore, rep, src, srcSize); |
982 | 27.3k | case 7 : |
983 | 27.3k | return ZSTD_compressBlock_fast_extDict_7_0(ms, seqStore, rep, src, srcSize); |
984 | 211k | } |
985 | 211k | } |