xref: /freebsd/sys/contrib/zstd/lib/compress/zstd_double_fast.c (revision 224e0c2f61307308a0bd951960e2eb50b4c1bbf4)
1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 #include "zstd_double_fast.h"
12 
13 
14 void ZSTD_fillDoubleHashTable(ZSTD_CCtx* cctx, const void* end, const U32 mls)
15 {
16     U32* const hashLarge = cctx->hashTable;
17     U32  const hBitsL = cctx->appliedParams.cParams.hashLog;
18     U32* const hashSmall = cctx->chainTable;
19     U32  const hBitsS = cctx->appliedParams.cParams.chainLog;
20     const BYTE* const base = cctx->base;
21     const BYTE* ip = base + cctx->nextToUpdate;
22     const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
23     const size_t fastHashFillStep = 3;
24 
25     while(ip <= iend) {
26         hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base);
27         hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base);
28         ip += fastHashFillStep;
29     }
30 }
31 
32 
33 FORCE_INLINE_TEMPLATE
34 size_t ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx,
35                                  const void* src, size_t srcSize,
36                                  const U32 mls)
37 {
38     U32* const hashLong = cctx->hashTable;
39     const U32 hBitsL = cctx->appliedParams.cParams.hashLog;
40     U32* const hashSmall = cctx->chainTable;
41     const U32 hBitsS = cctx->appliedParams.cParams.chainLog;
42     seqStore_t* seqStorePtr = &(cctx->seqStore);
43     const BYTE* const base = cctx->base;
44     const BYTE* const istart = (const BYTE*)src;
45     const BYTE* ip = istart;
46     const BYTE* anchor = istart;
47     const U32 lowestIndex = cctx->dictLimit;
48     const BYTE* const lowest = base + lowestIndex;
49     const BYTE* const iend = istart + srcSize;
50     const BYTE* const ilimit = iend - HASH_READ_SIZE;
51     U32 offset_1=seqStorePtr->rep[0], offset_2=seqStorePtr->rep[1];
52     U32 offsetSaved = 0;
53 
54     /* init */
55     ip += (ip==lowest);
56     {   U32 const maxRep = (U32)(ip-lowest);
57         if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
58         if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
59     }
60 
61     /* Main Search Loop */
62     while (ip < ilimit) {   /* < instead of <=, because repcode check at (ip+1) */
63         size_t mLength;
64         size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
65         size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
66         U32 const current = (U32)(ip-base);
67         U32 const matchIndexL = hashLong[h2];
68         U32 const matchIndexS = hashSmall[h];
69         const BYTE* matchLong = base + matchIndexL;
70         const BYTE* match = base + matchIndexS;
71         hashLong[h2] = hashSmall[h] = current;   /* update hash tables */
72 
73         assert(offset_1 <= current);   /* supposed guaranteed by construction */
74         if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
75             /* favor repcode */
76             mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
77             ip++;
78             ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH);
79         } else {
80             U32 offset;
81             if ( (matchIndexL > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip)) ) {
82                 mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
83                 offset = (U32)(ip-matchLong);
84                 while (((ip>anchor) & (matchLong>lowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
85             } else if ( (matchIndexS > lowestIndex) && (MEM_read32(match) == MEM_read32(ip)) ) {
86                 size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
87                 U32 const matchIndexL3 = hashLong[hl3];
88                 const BYTE* matchL3 = base + matchIndexL3;
89                 hashLong[hl3] = current + 1;
90                 if ( (matchIndexL3 > lowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1)) ) {
91                     mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
92                     ip++;
93                     offset = (U32)(ip-matchL3);
94                     while (((ip>anchor) & (matchL3>lowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
95                 } else {
96                     mLength = ZSTD_count(ip+4, match+4, iend) + 4;
97                     offset = (U32)(ip-match);
98                     while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
99                 }
100             } else {
101                 ip += ((ip-anchor) >> g_searchStrength) + 1;
102                 continue;
103             }
104 
105             offset_2 = offset_1;
106             offset_1 = offset;
107 
108             ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
109         }
110 
111         /* match found */
112         ip += mLength;
113         anchor = ip;
114 
115         if (ip <= ilimit) {
116             /* Fill Table */
117             hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] =
118                 hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2;  /* here because current+2 could be > iend-8 */
119             hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] =
120                 hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
121 
122             /* check immediate repcode */
123             while ( (ip <= ilimit)
124                  && ( (offset_2>0)
125                  & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
126                 /* store sequence */
127                 size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
128                 { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
129                 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
130                 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
131                 ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH);
132                 ip += rLength;
133                 anchor = ip;
134                 continue;   /* faster when present ... (?) */
135     }   }   }
136 
137     /* save reps for next block */
138     seqStorePtr->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved;
139     seqStorePtr->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved;
140 
141     /* Return the last literals size */
142     return iend - anchor;
143 }
144 
145 
146 size_t ZSTD_compressBlock_doubleFast(ZSTD_CCtx* ctx, const void* src, size_t srcSize)
147 {
148     const U32 mls = ctx->appliedParams.cParams.searchLength;
149     switch(mls)
150     {
151     default: /* includes case 3 */
152     case 4 :
153         return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4);
154     case 5 :
155         return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5);
156     case 6 :
157         return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6);
158     case 7 :
159         return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7);
160     }
161 }
162 
163 
164 static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
165                                  const void* src, size_t srcSize,
166                                  const U32 mls)
167 {
168     U32* const hashLong = ctx->hashTable;
169     U32  const hBitsL = ctx->appliedParams.cParams.hashLog;
170     U32* const hashSmall = ctx->chainTable;
171     U32  const hBitsS = ctx->appliedParams.cParams.chainLog;
172     seqStore_t* seqStorePtr = &(ctx->seqStore);
173     const BYTE* const base = ctx->base;
174     const BYTE* const dictBase = ctx->dictBase;
175     const BYTE* const istart = (const BYTE*)src;
176     const BYTE* ip = istart;
177     const BYTE* anchor = istart;
178     const U32   lowestIndex = ctx->lowLimit;
179     const BYTE* const dictStart = dictBase + lowestIndex;
180     const U32   dictLimit = ctx->dictLimit;
181     const BYTE* const lowPrefixPtr = base + dictLimit;
182     const BYTE* const dictEnd = dictBase + dictLimit;
183     const BYTE* const iend = istart + srcSize;
184     const BYTE* const ilimit = iend - 8;
185     U32 offset_1=seqStorePtr->rep[0], offset_2=seqStorePtr->rep[1];
186 
187     /* Search Loop */
188     while (ip < ilimit) {  /* < instead of <=, because (ip+1) */
189         const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
190         const U32 matchIndex = hashSmall[hSmall];
191         const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base;
192         const BYTE* match = matchBase + matchIndex;
193 
194         const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
195         const U32 matchLongIndex = hashLong[hLong];
196         const BYTE* matchLongBase = matchLongIndex < dictLimit ? dictBase : base;
197         const BYTE* matchLong = matchLongBase + matchLongIndex;
198 
199         const U32 current = (U32)(ip-base);
200         const U32 repIndex = current + 1 - offset_1;   /* offset_1 expected <= current +1 */
201         const BYTE* repBase = repIndex < dictLimit ? dictBase : base;
202         const BYTE* repMatch = repBase + repIndex;
203         size_t mLength;
204         hashSmall[hSmall] = hashLong[hLong] = current;   /* update hash table */
205 
206         if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex))
207            && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
208             const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
209             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4;
210             ip++;
211             ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH);
212         } else {
213             if ((matchLongIndex > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
214                 const BYTE* matchEnd = matchLongIndex < dictLimit ? dictEnd : iend;
215                 const BYTE* lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr;
216                 U32 offset;
217                 mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, lowPrefixPtr) + 8;
218                 offset = current - matchLongIndex;
219                 while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; }   /* catch up */
220                 offset_2 = offset_1;
221                 offset_1 = offset;
222                 ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
223 
224             } else if ((matchIndex > lowestIndex) && (MEM_read32(match) == MEM_read32(ip))) {
225                 size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
226                 U32 const matchIndex3 = hashLong[h3];
227                 const BYTE* const match3Base = matchIndex3 < dictLimit ? dictBase : base;
228                 const BYTE* match3 = match3Base + matchIndex3;
229                 U32 offset;
230                 hashLong[h3] = current + 1;
231                 if ( (matchIndex3 > lowestIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
232                     const BYTE* matchEnd = matchIndex3 < dictLimit ? dictEnd : iend;
233                     const BYTE* lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr;
234                     mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, lowPrefixPtr) + 8;
235                     ip++;
236                     offset = current+1 - matchIndex3;
237                     while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
238                 } else {
239                     const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend;
240                     const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
241                     mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4;
242                     offset = current - matchIndex;
243                     while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; }   /* catch up */
244                 }
245                 offset_2 = offset_1;
246                 offset_1 = offset;
247                 ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
248 
249             } else {
250                 ip += ((ip-anchor) >> g_searchStrength) + 1;
251                 continue;
252         }   }
253 
254         /* found a match : store it */
255         ip += mLength;
256         anchor = ip;
257 
258         if (ip <= ilimit) {
259             /* Fill Table */
260             hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2;
261             hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2;
262             hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
263             hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
264             /* check immediate repcode */
265             while (ip <= ilimit) {
266                 U32 const current2 = (U32)(ip-base);
267                 U32 const repIndex2 = current2 - offset_2;
268                 const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
269                 if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex))  /* intentional overflow */
270                    && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
271                     const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
272                     size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4;
273                     U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
274                     ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH);
275                     hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
276                     hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
277                     ip += repLength2;
278                     anchor = ip;
279                     continue;
280                 }
281                 break;
282     }   }   }
283 
284     /* save reps for next block */
285     seqStorePtr->repToConfirm[0] = offset_1; seqStorePtr->repToConfirm[1] = offset_2;
286 
287     /* Return the last literals size */
288     return iend - anchor;
289 }
290 
291 
292 size_t ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx* ctx,
293                          const void* src, size_t srcSize)
294 {
295     U32 const mls = ctx->appliedParams.cParams.searchLength;
296     switch(mls)
297     {
298     default: /* includes case 3 */
299     case 4 :
300         return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4);
301     case 5 :
302         return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5);
303     case 6 :
304         return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6);
305     case 7 :
306         return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7);
307     }
308 }
309