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