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