xref: /freebsd/sys/contrib/zstd/lib/compress/zstd_fast.c (revision 4d3f1eafc9372600fc1e5472187846d07fe96c54)
10c16b537SWarner Losh /*
20c16b537SWarner Losh  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh  */
100c16b537SWarner Losh 
11052d3c12SConrad Meyer #include "zstd_compress_internal.h"
120c16b537SWarner Losh #include "zstd_fast.h"
130c16b537SWarner Losh 
140c16b537SWarner Losh 
1519fcbaf1SConrad Meyer void ZSTD_fillHashTable(ZSTD_matchState_t* ms,
16*4d3f1eafSConrad Meyer                         const void* const end,
17*4d3f1eafSConrad Meyer                         ZSTD_dictTableLoadMethod_e dtlm)
180c16b537SWarner Losh {
190f743729SConrad Meyer     const ZSTD_compressionParameters* const cParams = &ms->cParams;
2019fcbaf1SConrad Meyer     U32* const hashTable = ms->hashTable;
2119fcbaf1SConrad Meyer     U32  const hBits = cParams->hashLog;
22a0483764SConrad Meyer     U32  const mls = cParams->minMatch;
2319fcbaf1SConrad Meyer     const BYTE* const base = ms->window.base;
2419fcbaf1SConrad Meyer     const BYTE* ip = base + ms->nextToUpdate;
250c16b537SWarner Losh     const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
2619fcbaf1SConrad Meyer     const U32 fastHashFillStep = 3;
270c16b537SWarner Losh 
2819fcbaf1SConrad Meyer     /* Always insert every fastHashFillStep position into the hash table.
2919fcbaf1SConrad Meyer      * Insert the other positions if their hash entry is empty.
3019fcbaf1SConrad Meyer      */
31a0483764SConrad Meyer     for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {
3219fcbaf1SConrad Meyer         U32 const current = (U32)(ip - base);
33a0483764SConrad Meyer         size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);
34a0483764SConrad Meyer         hashTable[hash0] = current;
35a0483764SConrad Meyer         if (dtlm == ZSTD_dtlm_fast) continue;
360f743729SConrad Meyer         /* Only load extra positions for ZSTD_dtlm_full */
37a0483764SConrad Meyer         {   U32 p;
38a0483764SConrad Meyer             for (p = 1; p < fastHashFillStep; ++p) {
39a0483764SConrad Meyer                 size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls);
40a0483764SConrad Meyer                 if (hashTable[hash] == 0) {  /* not yet filled */
41a0483764SConrad Meyer                     hashTable[hash] = current + p;
42a0483764SConrad Meyer     }   }   }   }
4319fcbaf1SConrad Meyer }
440c16b537SWarner Losh 
45*4d3f1eafSConrad Meyer 
460c16b537SWarner Losh FORCE_INLINE_TEMPLATE
4719fcbaf1SConrad Meyer size_t ZSTD_compressBlock_fast_generic(
4819fcbaf1SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
4919fcbaf1SConrad Meyer         void const* src, size_t srcSize,
502b9c00cbSConrad Meyer         U32 const mls)
512b9c00cbSConrad Meyer {
522b9c00cbSConrad Meyer     const ZSTD_compressionParameters* const cParams = &ms->cParams;
532b9c00cbSConrad Meyer     U32* const hashTable = ms->hashTable;
542b9c00cbSConrad Meyer     U32 const hlog = cParams->hashLog;
552b9c00cbSConrad Meyer     /* support stepSize of 0 */
562b9c00cbSConrad Meyer     size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1;
572b9c00cbSConrad Meyer     const BYTE* const base = ms->window.base;
582b9c00cbSConrad Meyer     const BYTE* const istart = (const BYTE*)src;
592b9c00cbSConrad Meyer     /* We check ip0 (ip + 0) and ip1 (ip + 1) each loop */
602b9c00cbSConrad Meyer     const BYTE* ip0 = istart;
612b9c00cbSConrad Meyer     const BYTE* ip1;
622b9c00cbSConrad Meyer     const BYTE* anchor = istart;
63*4d3f1eafSConrad Meyer     const U32   endIndex = (U32)((size_t)(istart - base) + srcSize);
64*4d3f1eafSConrad Meyer     const U32   maxDistance = 1U << cParams->windowLog;
65*4d3f1eafSConrad Meyer     const U32   validStartIndex = ms->window.dictLimit;
66*4d3f1eafSConrad Meyer     const U32   prefixStartIndex = (endIndex - validStartIndex > maxDistance) ? endIndex - maxDistance : validStartIndex;
672b9c00cbSConrad Meyer     const BYTE* const prefixStart = base + prefixStartIndex;
682b9c00cbSConrad Meyer     const BYTE* const iend = istart + srcSize;
692b9c00cbSConrad Meyer     const BYTE* const ilimit = iend - HASH_READ_SIZE;
702b9c00cbSConrad Meyer     U32 offset_1=rep[0], offset_2=rep[1];
712b9c00cbSConrad Meyer     U32 offsetSaved = 0;
722b9c00cbSConrad Meyer 
732b9c00cbSConrad Meyer     /* init */
742b9c00cbSConrad Meyer     ip0 += (ip0 == prefixStart);
752b9c00cbSConrad Meyer     ip1 = ip0 + 1;
762b9c00cbSConrad Meyer     {
772b9c00cbSConrad Meyer         U32 const maxRep = (U32)(ip0 - prefixStart);
782b9c00cbSConrad Meyer         if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
792b9c00cbSConrad Meyer         if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
802b9c00cbSConrad Meyer     }
812b9c00cbSConrad Meyer 
822b9c00cbSConrad Meyer     /* Main Search Loop */
832b9c00cbSConrad Meyer     while (ip1 < ilimit) {   /* < instead of <=, because check at ip0+2 */
842b9c00cbSConrad Meyer         size_t mLength;
852b9c00cbSConrad Meyer         BYTE const* ip2 = ip0 + 2;
862b9c00cbSConrad Meyer         size_t const h0 = ZSTD_hashPtr(ip0, hlog, mls);
872b9c00cbSConrad Meyer         U32 const val0 = MEM_read32(ip0);
882b9c00cbSConrad Meyer         size_t const h1 = ZSTD_hashPtr(ip1, hlog, mls);
892b9c00cbSConrad Meyer         U32 const val1 = MEM_read32(ip1);
902b9c00cbSConrad Meyer         U32 const current0 = (U32)(ip0-base);
912b9c00cbSConrad Meyer         U32 const current1 = (U32)(ip1-base);
922b9c00cbSConrad Meyer         U32 const matchIndex0 = hashTable[h0];
932b9c00cbSConrad Meyer         U32 const matchIndex1 = hashTable[h1];
942b9c00cbSConrad Meyer         BYTE const* repMatch = ip2-offset_1;
952b9c00cbSConrad Meyer         const BYTE* match0 = base + matchIndex0;
962b9c00cbSConrad Meyer         const BYTE* match1 = base + matchIndex1;
972b9c00cbSConrad Meyer         U32 offcode;
982b9c00cbSConrad Meyer         hashTable[h0] = current0;   /* update hash table */
992b9c00cbSConrad Meyer         hashTable[h1] = current1;   /* update hash table */
1002b9c00cbSConrad Meyer 
1012b9c00cbSConrad Meyer         assert(ip0 + 1 == ip1);
1022b9c00cbSConrad Meyer 
1032b9c00cbSConrad Meyer         if ((offset_1 > 0) & (MEM_read32(repMatch) == MEM_read32(ip2))) {
1042b9c00cbSConrad Meyer             mLength = ip2[-1] == repMatch[-1] ? 1 : 0;
1052b9c00cbSConrad Meyer             ip0 = ip2 - mLength;
1062b9c00cbSConrad Meyer             match0 = repMatch - mLength;
1072b9c00cbSConrad Meyer             offcode = 0;
1082b9c00cbSConrad Meyer             goto _match;
1092b9c00cbSConrad Meyer         }
1102b9c00cbSConrad Meyer         if ((matchIndex0 > prefixStartIndex) && MEM_read32(match0) == val0) {
1112b9c00cbSConrad Meyer             /* found a regular match */
1122b9c00cbSConrad Meyer             goto _offset;
1132b9c00cbSConrad Meyer         }
1142b9c00cbSConrad Meyer         if ((matchIndex1 > prefixStartIndex) && MEM_read32(match1) == val1) {
1152b9c00cbSConrad Meyer             /* found a regular match after one literal */
1162b9c00cbSConrad Meyer             ip0 = ip1;
1172b9c00cbSConrad Meyer             match0 = match1;
1182b9c00cbSConrad Meyer             goto _offset;
1192b9c00cbSConrad Meyer         }
1202b9c00cbSConrad Meyer         {
1212b9c00cbSConrad Meyer             size_t const step = ((ip0-anchor) >> (kSearchStrength - 1)) + stepSize;
1222b9c00cbSConrad Meyer             assert(step >= 2);
1232b9c00cbSConrad Meyer             ip0 += step;
1242b9c00cbSConrad Meyer             ip1 += step;
1252b9c00cbSConrad Meyer             continue;
1262b9c00cbSConrad Meyer         }
1272b9c00cbSConrad Meyer _offset: /* Requires: ip0, match0 */
1282b9c00cbSConrad Meyer         /* Compute the offset code */
1292b9c00cbSConrad Meyer         offset_2 = offset_1;
1302b9c00cbSConrad Meyer         offset_1 = (U32)(ip0-match0);
1312b9c00cbSConrad Meyer         offcode = offset_1 + ZSTD_REP_MOVE;
1322b9c00cbSConrad Meyer         mLength = 0;
1332b9c00cbSConrad Meyer         /* Count the backwards match length */
1342b9c00cbSConrad Meyer         while (((ip0>anchor) & (match0>prefixStart))
1352b9c00cbSConrad Meyer              && (ip0[-1] == match0[-1])) { ip0--; match0--; mLength++; } /* catch up */
1362b9c00cbSConrad Meyer 
1372b9c00cbSConrad Meyer _match: /* Requires: ip0, match0, offcode */
1382b9c00cbSConrad Meyer         /* Count the forward length */
1392b9c00cbSConrad Meyer         mLength += ZSTD_count(ip0+mLength+4, match0+mLength+4, iend) + 4;
1402b9c00cbSConrad Meyer         ZSTD_storeSeq(seqStore, ip0-anchor, anchor, offcode, mLength-MINMATCH);
1412b9c00cbSConrad Meyer         /* match found */
1422b9c00cbSConrad Meyer         ip0 += mLength;
1432b9c00cbSConrad Meyer         anchor = ip0;
1442b9c00cbSConrad Meyer         ip1 = ip0 + 1;
1452b9c00cbSConrad Meyer 
1462b9c00cbSConrad Meyer         if (ip0 <= ilimit) {
1472b9c00cbSConrad Meyer             /* Fill Table */
1482b9c00cbSConrad Meyer             assert(base+current0+2 > istart);  /* check base overflow */
1492b9c00cbSConrad Meyer             hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2;  /* here because current+2 could be > iend-8 */
1502b9c00cbSConrad Meyer             hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base);
1512b9c00cbSConrad Meyer 
1522b9c00cbSConrad Meyer             while ( (ip0 <= ilimit)
1532b9c00cbSConrad Meyer                  && ( (offset_2>0)
1542b9c00cbSConrad Meyer                     & (MEM_read32(ip0) == MEM_read32(ip0 - offset_2)) )) {
1552b9c00cbSConrad Meyer                 /* store sequence */
1562b9c00cbSConrad Meyer                 size_t const rLength = ZSTD_count(ip0+4, ip0+4-offset_2, iend) + 4;
1572b9c00cbSConrad Meyer                 U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff;  /* swap offset_2 <=> offset_1 */
1582b9c00cbSConrad Meyer                 hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base);
1592b9c00cbSConrad Meyer                 ip0 += rLength;
1602b9c00cbSConrad Meyer                 ip1 = ip0 + 1;
1612b9c00cbSConrad Meyer                 ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
1622b9c00cbSConrad Meyer                 anchor = ip0;
1632b9c00cbSConrad Meyer                 continue;   /* faster when present (confirmed on gcc-8) ... (?) */
1642b9c00cbSConrad Meyer             }
1652b9c00cbSConrad Meyer         }
1662b9c00cbSConrad Meyer     }
1672b9c00cbSConrad Meyer 
1682b9c00cbSConrad Meyer     /* save reps for next block */
1692b9c00cbSConrad Meyer     rep[0] = offset_1 ? offset_1 : offsetSaved;
1702b9c00cbSConrad Meyer     rep[1] = offset_2 ? offset_2 : offsetSaved;
1712b9c00cbSConrad Meyer 
1722b9c00cbSConrad Meyer     /* Return the last literals size */
173*4d3f1eafSConrad Meyer     return (size_t)(iend - anchor);
1742b9c00cbSConrad Meyer }
1752b9c00cbSConrad Meyer 
1762b9c00cbSConrad Meyer 
1772b9c00cbSConrad Meyer size_t ZSTD_compressBlock_fast(
1782b9c00cbSConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
1792b9c00cbSConrad Meyer         void const* src, size_t srcSize)
1802b9c00cbSConrad Meyer {
1812b9c00cbSConrad Meyer     ZSTD_compressionParameters const* cParams = &ms->cParams;
1822b9c00cbSConrad Meyer     U32 const mls = cParams->minMatch;
1832b9c00cbSConrad Meyer     assert(ms->dictMatchState == NULL);
1842b9c00cbSConrad Meyer     switch(mls)
1852b9c00cbSConrad Meyer     {
1862b9c00cbSConrad Meyer     default: /* includes case 3 */
1872b9c00cbSConrad Meyer     case 4 :
1882b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4);
1892b9c00cbSConrad Meyer     case 5 :
1902b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5);
1912b9c00cbSConrad Meyer     case 6 :
1922b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6);
1932b9c00cbSConrad Meyer     case 7 :
1942b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7);
1952b9c00cbSConrad Meyer     }
1962b9c00cbSConrad Meyer }
1972b9c00cbSConrad Meyer 
1982b9c00cbSConrad Meyer FORCE_INLINE_TEMPLATE
1992b9c00cbSConrad Meyer size_t ZSTD_compressBlock_fast_dictMatchState_generic(
2002b9c00cbSConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2012b9c00cbSConrad Meyer         void const* src, size_t srcSize, U32 const mls)
2020c16b537SWarner Losh {
2030f743729SConrad Meyer     const ZSTD_compressionParameters* const cParams = &ms->cParams;
20419fcbaf1SConrad Meyer     U32* const hashTable = ms->hashTable;
2050f743729SConrad Meyer     U32 const hlog = cParams->hashLog;
2060f743729SConrad Meyer     /* support stepSize of 0 */
2070f743729SConrad Meyer     U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
20819fcbaf1SConrad Meyer     const BYTE* const base = ms->window.base;
2090c16b537SWarner Losh     const BYTE* const istart = (const BYTE*)src;
2100c16b537SWarner Losh     const BYTE* ip = istart;
2110c16b537SWarner Losh     const BYTE* anchor = istart;
2120f743729SConrad Meyer     const U32   prefixStartIndex = ms->window.dictLimit;
2130f743729SConrad Meyer     const BYTE* const prefixStart = base + prefixStartIndex;
2140c16b537SWarner Losh     const BYTE* const iend = istart + srcSize;
2150c16b537SWarner Losh     const BYTE* const ilimit = iend - HASH_READ_SIZE;
21619fcbaf1SConrad Meyer     U32 offset_1=rep[0], offset_2=rep[1];
2170c16b537SWarner Losh     U32 offsetSaved = 0;
2180c16b537SWarner Losh 
2190f743729SConrad Meyer     const ZSTD_matchState_t* const dms = ms->dictMatchState;
2202b9c00cbSConrad Meyer     const ZSTD_compressionParameters* const dictCParams = &dms->cParams ;
2212b9c00cbSConrad Meyer     const U32* const dictHashTable = dms->hashTable;
2222b9c00cbSConrad Meyer     const U32 dictStartIndex       = dms->window.dictLimit;
2232b9c00cbSConrad Meyer     const BYTE* const dictBase     = dms->window.base;
2242b9c00cbSConrad Meyer     const BYTE* const dictStart    = dictBase + dictStartIndex;
2252b9c00cbSConrad Meyer     const BYTE* const dictEnd      = dms->window.nextSrc;
2262b9c00cbSConrad Meyer     const U32 dictIndexDelta       = prefixStartIndex - (U32)(dictEnd - dictBase);
2270f743729SConrad Meyer     const U32 dictAndPrefixLength  = (U32)(ip - prefixStart + dictEnd - dictStart);
2282b9c00cbSConrad Meyer     const U32 dictHLog             = dictCParams->hashLog;
2290f743729SConrad Meyer 
230*4d3f1eafSConrad Meyer     /* if a dictionary is still attached, it necessarily means that
231*4d3f1eafSConrad Meyer      * it is within window size. So we just check it. */
232*4d3f1eafSConrad Meyer     const U32 maxDistance = 1U << cParams->windowLog;
233*4d3f1eafSConrad Meyer     const U32 endIndex = (U32)((size_t)(ip - base) + srcSize);
234*4d3f1eafSConrad Meyer     assert(endIndex - prefixStartIndex <= maxDistance);
235*4d3f1eafSConrad Meyer     (void)maxDistance; (void)endIndex;   /* these variables are not used when assert() is disabled */
236*4d3f1eafSConrad Meyer 
237*4d3f1eafSConrad Meyer     /* ensure there will be no no underflow
238*4d3f1eafSConrad Meyer      * when translating a dict index into a local index */
2392b9c00cbSConrad Meyer     assert(prefixStartIndex >= (U32)(dictEnd - dictBase));
2400f743729SConrad Meyer 
2410c16b537SWarner Losh     /* init */
2420f743729SConrad Meyer     ip += (dictAndPrefixLength == 0);
2430f743729SConrad Meyer     /* dictMatchState repCode checks don't currently handle repCode == 0
2440f743729SConrad Meyer      * disabling. */
2450f743729SConrad Meyer     assert(offset_1 <= dictAndPrefixLength);
2460f743729SConrad Meyer     assert(offset_2 <= dictAndPrefixLength);
2470c16b537SWarner Losh 
2480c16b537SWarner Losh     /* Main Search Loop */
2490c16b537SWarner Losh     while (ip < ilimit) {   /* < instead of <=, because repcode check at (ip+1) */
2500c16b537SWarner Losh         size_t mLength;
25119fcbaf1SConrad Meyer         size_t const h = ZSTD_hashPtr(ip, hlog, mls);
2520c16b537SWarner Losh         U32 const current = (U32)(ip-base);
2530c16b537SWarner Losh         U32 const matchIndex = hashTable[h];
2540c16b537SWarner Losh         const BYTE* match = base + matchIndex;
2550f743729SConrad Meyer         const U32 repIndex = current + 1 - offset_1;
2562b9c00cbSConrad Meyer         const BYTE* repMatch = (repIndex < prefixStartIndex) ?
2570f743729SConrad Meyer                                dictBase + (repIndex - dictIndexDelta) :
2580f743729SConrad Meyer                                base + repIndex;
2590c16b537SWarner Losh         hashTable[h] = current;   /* update hash table */
2600c16b537SWarner Losh 
2612b9c00cbSConrad Meyer         if ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */
2620f743729SConrad Meyer           && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
2630f743729SConrad Meyer             const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
2640f743729SConrad Meyer             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
2650f743729SConrad Meyer             ip++;
266*4d3f1eafSConrad Meyer             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
2670f743729SConrad Meyer         } else if ( (matchIndex <= prefixStartIndex) ) {
2680f743729SConrad Meyer             size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls);
2690f743729SConrad Meyer             U32 const dictMatchIndex = dictHashTable[dictHash];
2700f743729SConrad Meyer             const BYTE* dictMatch = dictBase + dictMatchIndex;
2710f743729SConrad Meyer             if (dictMatchIndex <= dictStartIndex ||
2720f743729SConrad Meyer                 MEM_read32(dictMatch) != MEM_read32(ip)) {
2730f743729SConrad Meyer                 assert(stepSize >= 1);
2740f743729SConrad Meyer                 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
2750f743729SConrad Meyer                 continue;
2760c16b537SWarner Losh             } else {
2770f743729SConrad Meyer                 /* found a dict match */
2780f743729SConrad Meyer                 U32 const offset = (U32)(current-dictMatchIndex-dictIndexDelta);
2790f743729SConrad Meyer                 mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4;
2800f743729SConrad Meyer                 while (((ip>anchor) & (dictMatch>dictStart))
2810f743729SConrad Meyer                      && (ip[-1] == dictMatch[-1])) {
2820f743729SConrad Meyer                     ip--; dictMatch--; mLength++;
2830f743729SConrad Meyer                 } /* catch up */
2840f743729SConrad Meyer                 offset_2 = offset_1;
2850f743729SConrad Meyer                 offset_1 = offset;
286*4d3f1eafSConrad Meyer                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
2870f743729SConrad Meyer             }
2880f743729SConrad Meyer         } else if (MEM_read32(match) != MEM_read32(ip)) {
2890f743729SConrad Meyer             /* it's not a match, and we're not going to check the dictionary */
2900f743729SConrad Meyer             assert(stepSize >= 1);
2910f743729SConrad Meyer             ip += ((ip-anchor) >> kSearchStrength) + stepSize;
2920f743729SConrad Meyer             continue;
2930f743729SConrad Meyer         } else {
2940f743729SConrad Meyer             /* found a regular match */
2950f743729SConrad Meyer             U32 const offset = (U32)(ip-match);
2960c16b537SWarner Losh             mLength = ZSTD_count(ip+4, match+4, iend) + 4;
2970f743729SConrad Meyer             while (((ip>anchor) & (match>prefixStart))
2980f743729SConrad Meyer                  && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
2990c16b537SWarner Losh             offset_2 = offset_1;
3000c16b537SWarner Losh             offset_1 = offset;
301*4d3f1eafSConrad Meyer             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
3020f743729SConrad Meyer         }
3030c16b537SWarner Losh 
3040c16b537SWarner Losh         /* match found */
3050c16b537SWarner Losh         ip += mLength;
3060c16b537SWarner Losh         anchor = ip;
3070c16b537SWarner Losh 
3080c16b537SWarner Losh         if (ip <= ilimit) {
3090c16b537SWarner Losh             /* Fill Table */
3100f743729SConrad Meyer             assert(base+current+2 > istart);  /* check base overflow */
31119fcbaf1SConrad Meyer             hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2;  /* here because current+2 could be > iend-8 */
31219fcbaf1SConrad Meyer             hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
3130f743729SConrad Meyer 
3140c16b537SWarner Losh             /* check immediate repcode */
3150f743729SConrad Meyer             while (ip <= ilimit) {
3160f743729SConrad Meyer                 U32 const current2 = (U32)(ip-base);
3170f743729SConrad Meyer                 U32 const repIndex2 = current2 - offset_2;
3180f743729SConrad Meyer                 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?
3190f743729SConrad Meyer                         dictBase - dictIndexDelta + repIndex2 :
3200f743729SConrad Meyer                         base + repIndex2;
3210f743729SConrad Meyer                 if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
3220f743729SConrad Meyer                    && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
3230f743729SConrad Meyer                     const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
3240f743729SConrad Meyer                     size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
3250f743729SConrad Meyer                     U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
3260f743729SConrad Meyer                     ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
3270f743729SConrad Meyer                     hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
3280f743729SConrad Meyer                     ip += repLength2;
3290f743729SConrad Meyer                     anchor = ip;
3300f743729SConrad Meyer                     continue;
3310f743729SConrad Meyer                 }
3320f743729SConrad Meyer                 break;
3330f743729SConrad Meyer             }
3340f743729SConrad Meyer         }
3352b9c00cbSConrad Meyer     }
3360c16b537SWarner Losh 
3370c16b537SWarner Losh     /* save reps for next block */
33819fcbaf1SConrad Meyer     rep[0] = offset_1 ? offset_1 : offsetSaved;
33919fcbaf1SConrad Meyer     rep[1] = offset_2 ? offset_2 : offsetSaved;
3400c16b537SWarner Losh 
3410c16b537SWarner Losh     /* Return the last literals size */
342*4d3f1eafSConrad Meyer     return (size_t)(iend - anchor);
3430c16b537SWarner Losh }
3440c16b537SWarner Losh 
3450f743729SConrad Meyer size_t ZSTD_compressBlock_fast_dictMatchState(
3460f743729SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
3470f743729SConrad Meyer         void const* src, size_t srcSize)
3480f743729SConrad Meyer {
3490f743729SConrad Meyer     ZSTD_compressionParameters const* cParams = &ms->cParams;
350a0483764SConrad Meyer     U32 const mls = cParams->minMatch;
3510f743729SConrad Meyer     assert(ms->dictMatchState != NULL);
3520f743729SConrad Meyer     switch(mls)
3530f743729SConrad Meyer     {
3540f743729SConrad Meyer     default: /* includes case 3 */
3550f743729SConrad Meyer     case 4 :
3562b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4);
3570f743729SConrad Meyer     case 5 :
3582b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5);
3590f743729SConrad Meyer     case 6 :
3602b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6);
3610f743729SConrad Meyer     case 7 :
3622b9c00cbSConrad Meyer         return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7);
3630c16b537SWarner Losh     }
3640c16b537SWarner Losh }
3650c16b537SWarner Losh 
3660c16b537SWarner Losh 
36719fcbaf1SConrad Meyer static size_t ZSTD_compressBlock_fast_extDict_generic(
36819fcbaf1SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
3690f743729SConrad Meyer         void const* src, size_t srcSize, U32 const mls)
3700c16b537SWarner Losh {
3710f743729SConrad Meyer     const ZSTD_compressionParameters* const cParams = &ms->cParams;
3720f743729SConrad Meyer     U32* const hashTable = ms->hashTable;
3730f743729SConrad Meyer     U32 const hlog = cParams->hashLog;
3740f743729SConrad Meyer     /* support stepSize of 0 */
3750f743729SConrad Meyer     U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
37619fcbaf1SConrad Meyer     const BYTE* const base = ms->window.base;
37719fcbaf1SConrad Meyer     const BYTE* const dictBase = ms->window.dictBase;
3780c16b537SWarner Losh     const BYTE* const istart = (const BYTE*)src;
3790c16b537SWarner Losh     const BYTE* ip = istart;
3800c16b537SWarner Losh     const BYTE* anchor = istart;
381*4d3f1eafSConrad Meyer     const U32   endIndex = (U32)((size_t)(istart - base) + srcSize);
382*4d3f1eafSConrad Meyer     const U32   maxDistance = 1U << cParams->windowLog;
383*4d3f1eafSConrad Meyer     const U32   validLow = ms->window.lowLimit;
384*4d3f1eafSConrad Meyer     const U32   lowLimit = (endIndex - validLow > maxDistance) ? endIndex - maxDistance : validLow;
385*4d3f1eafSConrad Meyer     const U32   dictStartIndex = lowLimit;
3860f743729SConrad Meyer     const BYTE* const dictStart = dictBase + dictStartIndex;
387*4d3f1eafSConrad Meyer     const U32   dictLimit = ms->window.dictLimit;
388*4d3f1eafSConrad Meyer     const U32   prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit;
3890f743729SConrad Meyer     const BYTE* const prefixStart = base + prefixStartIndex;
3900f743729SConrad Meyer     const BYTE* const dictEnd = dictBase + prefixStartIndex;
3910c16b537SWarner Losh     const BYTE* const iend = istart + srcSize;
3920c16b537SWarner Losh     const BYTE* const ilimit = iend - 8;
39319fcbaf1SConrad Meyer     U32 offset_1=rep[0], offset_2=rep[1];
3940c16b537SWarner Losh 
395*4d3f1eafSConrad Meyer     /* switch to "regular" variant if extDict is invalidated due to maxDistance */
396*4d3f1eafSConrad Meyer     if (prefixStartIndex == dictStartIndex)
397*4d3f1eafSConrad Meyer         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, mls);
398*4d3f1eafSConrad Meyer 
3990c16b537SWarner Losh     /* Search Loop */
4000c16b537SWarner Losh     while (ip < ilimit) {  /* < instead of <=, because (ip+1) */
40119fcbaf1SConrad Meyer         const size_t h = ZSTD_hashPtr(ip, hlog, mls);
4020c16b537SWarner Losh         const U32    matchIndex = hashTable[h];
4030f743729SConrad Meyer         const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
4040c16b537SWarner Losh         const BYTE*  match = matchBase + matchIndex;
4050c16b537SWarner Losh         const U32    current = (U32)(ip-base);
4060f743729SConrad Meyer         const U32    repIndex = current + 1 - offset_1;
4070f743729SConrad Meyer         const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
4080f743729SConrad Meyer         const BYTE* const repMatch = repBase + repIndex;
4090c16b537SWarner Losh         size_t mLength;
4100c16b537SWarner Losh         hashTable[h] = current;   /* update hash table */
4110f743729SConrad Meyer         assert(offset_1 <= current +1);   /* check repIndex */
4120c16b537SWarner Losh 
4130f743729SConrad Meyer         if ( (((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > dictStartIndex))
4140c16b537SWarner Losh            && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
4150f743729SConrad Meyer             const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
4160f743729SConrad Meyer             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
4170c16b537SWarner Losh             ip++;
418*4d3f1eafSConrad Meyer             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, 0, mLength-MINMATCH);
4190c16b537SWarner Losh         } else {
4200f743729SConrad Meyer             if ( (matchIndex < dictStartIndex) ||
4210c16b537SWarner Losh                  (MEM_read32(match) != MEM_read32(ip)) ) {
42219fcbaf1SConrad Meyer                 assert(stepSize >= 1);
42319fcbaf1SConrad Meyer                 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
4240c16b537SWarner Losh                 continue;
4250c16b537SWarner Losh             }
4260f743729SConrad Meyer             {   const BYTE* matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
4270f743729SConrad Meyer                 const BYTE* lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
4280c16b537SWarner Losh                 U32 offset;
4290f743729SConrad Meyer                 mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
4300c16b537SWarner Losh                 while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; }   /* catch up */
4310c16b537SWarner Losh                 offset = current - matchIndex;
4320c16b537SWarner Losh                 offset_2 = offset_1;
4330c16b537SWarner Losh                 offset_1 = offset;
434*4d3f1eafSConrad Meyer                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
4350c16b537SWarner Losh         }   }
4360c16b537SWarner Losh 
4370c16b537SWarner Losh         /* found a match : store it */
4380c16b537SWarner Losh         ip += mLength;
4390c16b537SWarner Losh         anchor = ip;
4400c16b537SWarner Losh 
4410c16b537SWarner Losh         if (ip <= ilimit) {
4420c16b537SWarner Losh             /* Fill Table */
44319fcbaf1SConrad Meyer             hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2;
44419fcbaf1SConrad Meyer             hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
4450c16b537SWarner Losh             /* check immediate repcode */
4460c16b537SWarner Losh             while (ip <= ilimit) {
4470c16b537SWarner Losh                 U32 const current2 = (U32)(ip-base);
4480c16b537SWarner Losh                 U32 const repIndex2 = current2 - offset_2;
4490f743729SConrad Meyer                 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
4500f743729SConrad Meyer                 if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (repIndex2 > dictStartIndex))  /* intentional overflow */
4510c16b537SWarner Losh                    && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
4520f743729SConrad Meyer                     const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
4530f743729SConrad Meyer                     size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
4540c16b537SWarner Losh                     U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
45519fcbaf1SConrad Meyer                     ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
45619fcbaf1SConrad Meyer                     hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
4570c16b537SWarner Losh                     ip += repLength2;
4580c16b537SWarner Losh                     anchor = ip;
4590c16b537SWarner Losh                     continue;
4600c16b537SWarner Losh                 }
4610c16b537SWarner Losh                 break;
4620c16b537SWarner Losh     }   }   }
4630c16b537SWarner Losh 
4640c16b537SWarner Losh     /* save reps for next block */
46519fcbaf1SConrad Meyer     rep[0] = offset_1;
46619fcbaf1SConrad Meyer     rep[1] = offset_2;
4670c16b537SWarner Losh 
4680c16b537SWarner Losh     /* Return the last literals size */
469*4d3f1eafSConrad Meyer     return (size_t)(iend - anchor);
4700c16b537SWarner Losh }
4710c16b537SWarner Losh 
4720c16b537SWarner Losh 
47319fcbaf1SConrad Meyer size_t ZSTD_compressBlock_fast_extDict(
47419fcbaf1SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
4750f743729SConrad Meyer         void const* src, size_t srcSize)
4760c16b537SWarner Losh {
4770f743729SConrad Meyer     ZSTD_compressionParameters const* cParams = &ms->cParams;
478a0483764SConrad Meyer     U32 const mls = cParams->minMatch;
4790c16b537SWarner Losh     switch(mls)
4800c16b537SWarner Losh     {
4810c16b537SWarner Losh     default: /* includes case 3 */
4820c16b537SWarner Losh     case 4 :
4830f743729SConrad Meyer         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
4840c16b537SWarner Losh     case 5 :
4850f743729SConrad Meyer         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
4860c16b537SWarner Losh     case 6 :
4870f743729SConrad Meyer         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
4880c16b537SWarner Losh     case 7 :
4890f743729SConrad Meyer         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
4900c16b537SWarner Losh     }
4910c16b537SWarner Losh }
492