10c16b537SWarner Losh /*
25ff13fbcSAllan Jude  * Copyright (c) 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_double_fast.h"
130c16b537SWarner Losh 
140c16b537SWarner Losh 
ZSTD_fillDoubleHashTable(ZSTD_matchState_t * ms,void const * end,ZSTD_dictTableLoadMethod_e dtlm)1519fcbaf1SConrad Meyer void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
160f743729SConrad Meyer                               void const* end, ZSTD_dictTableLoadMethod_e dtlm)
170c16b537SWarner Losh {
180f743729SConrad Meyer     const ZSTD_compressionParameters* const cParams = &ms->cParams;
1919fcbaf1SConrad Meyer     U32* const hashLarge = ms->hashTable;
2019fcbaf1SConrad Meyer     U32  const hBitsL = cParams->hashLog;
21a0483764SConrad Meyer     U32  const mls = cParams->minMatch;
2219fcbaf1SConrad Meyer     U32* const hashSmall = ms->chainTable;
2319fcbaf1SConrad Meyer     U32  const hBitsS = cParams->chainLog;
2419fcbaf1SConrad Meyer     const BYTE* const base = ms->window.base;
2519fcbaf1SConrad Meyer     const BYTE* ip = base + ms->nextToUpdate;
260c16b537SWarner Losh     const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
2719fcbaf1SConrad Meyer     const U32 fastHashFillStep = 3;
280c16b537SWarner Losh 
2919fcbaf1SConrad Meyer     /* Always insert every fastHashFillStep position into the hash tables.
3019fcbaf1SConrad Meyer      * Insert the other positions into the large hash table if their entry
3119fcbaf1SConrad Meyer      * is empty.
3219fcbaf1SConrad Meyer      */
3319fcbaf1SConrad Meyer     for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
34f7cd7fe5SConrad Meyer         U32 const curr = (U32)(ip - base);
3519fcbaf1SConrad Meyer         U32 i;
3619fcbaf1SConrad Meyer         for (i = 0; i < fastHashFillStep; ++i) {
3719fcbaf1SConrad Meyer             size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
3819fcbaf1SConrad Meyer             size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
3919fcbaf1SConrad Meyer             if (i == 0)
40f7cd7fe5SConrad Meyer                 hashSmall[smHash] = curr + i;
4119fcbaf1SConrad Meyer             if (i == 0 || hashLarge[lgHash] == 0)
42f7cd7fe5SConrad Meyer                 hashLarge[lgHash] = curr + i;
430f743729SConrad Meyer             /* Only load extra positions for ZSTD_dtlm_full */
440f743729SConrad Meyer             if (dtlm == ZSTD_dtlm_fast)
450f743729SConrad Meyer                 break;
464d3f1eafSConrad Meyer     }   }
470c16b537SWarner Losh }
480c16b537SWarner Losh 
490c16b537SWarner Losh 
500c16b537SWarner Losh FORCE_INLINE_TEMPLATE
ZSTD_compressBlock_doubleFast_noDict_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize,U32 const mls)515ff13fbcSAllan Jude size_t ZSTD_compressBlock_doubleFast_noDict_generic(
525ff13fbcSAllan Jude         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
535ff13fbcSAllan Jude         void const* src, size_t srcSize, U32 const mls /* template */)
545ff13fbcSAllan Jude {
555ff13fbcSAllan Jude     ZSTD_compressionParameters const* cParams = &ms->cParams;
565ff13fbcSAllan Jude     U32* const hashLong = ms->hashTable;
575ff13fbcSAllan Jude     const U32 hBitsL = cParams->hashLog;
585ff13fbcSAllan Jude     U32* const hashSmall = ms->chainTable;
595ff13fbcSAllan Jude     const U32 hBitsS = cParams->chainLog;
605ff13fbcSAllan Jude     const BYTE* const base = ms->window.base;
615ff13fbcSAllan Jude     const BYTE* const istart = (const BYTE*)src;
625ff13fbcSAllan Jude     const BYTE* anchor = istart;
635ff13fbcSAllan Jude     const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
645ff13fbcSAllan Jude     /* presumes that, if there is a dictionary, it must be using Attach mode */
655ff13fbcSAllan Jude     const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
665ff13fbcSAllan Jude     const BYTE* const prefixLowest = base + prefixLowestIndex;
675ff13fbcSAllan Jude     const BYTE* const iend = istart + srcSize;
685ff13fbcSAllan Jude     const BYTE* const ilimit = iend - HASH_READ_SIZE;
695ff13fbcSAllan Jude     U32 offset_1=rep[0], offset_2=rep[1];
705ff13fbcSAllan Jude     U32 offsetSaved = 0;
715ff13fbcSAllan Jude 
725ff13fbcSAllan Jude     size_t mLength;
735ff13fbcSAllan Jude     U32 offset;
745ff13fbcSAllan Jude     U32 curr;
755ff13fbcSAllan Jude 
765ff13fbcSAllan Jude     /* how many positions to search before increasing step size */
775ff13fbcSAllan Jude     const size_t kStepIncr = 1 << kSearchStrength;
785ff13fbcSAllan Jude     /* the position at which to increment the step size if no match is found */
795ff13fbcSAllan Jude     const BYTE* nextStep;
805ff13fbcSAllan Jude     size_t step; /* the current step size */
815ff13fbcSAllan Jude 
825ff13fbcSAllan Jude     size_t hl0; /* the long hash at ip */
835ff13fbcSAllan Jude     size_t hl1; /* the long hash at ip1 */
845ff13fbcSAllan Jude 
855ff13fbcSAllan Jude     U32 idxl0; /* the long match index for ip */
865ff13fbcSAllan Jude     U32 idxl1; /* the long match index for ip1 */
875ff13fbcSAllan Jude 
885ff13fbcSAllan Jude     const BYTE* matchl0; /* the long match for ip */
895ff13fbcSAllan Jude     const BYTE* matchs0; /* the short match for ip */
905ff13fbcSAllan Jude     const BYTE* matchl1; /* the long match for ip1 */
915ff13fbcSAllan Jude 
925ff13fbcSAllan Jude     const BYTE* ip = istart; /* the current position */
935ff13fbcSAllan Jude     const BYTE* ip1; /* the next position */
945ff13fbcSAllan Jude 
955ff13fbcSAllan Jude     DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_noDict_generic");
965ff13fbcSAllan Jude 
975ff13fbcSAllan Jude     /* init */
985ff13fbcSAllan Jude     ip += ((ip - prefixLowest) == 0);
995ff13fbcSAllan Jude     {
1005ff13fbcSAllan Jude         U32 const current = (U32)(ip - base);
1015ff13fbcSAllan Jude         U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, current, cParams->windowLog);
1025ff13fbcSAllan Jude         U32 const maxRep = current - windowLow;
1035ff13fbcSAllan Jude         if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
1045ff13fbcSAllan Jude         if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
1055ff13fbcSAllan Jude     }
1065ff13fbcSAllan Jude 
1075ff13fbcSAllan Jude     /* Outer Loop: one iteration per match found and stored */
1085ff13fbcSAllan Jude     while (1) {
1095ff13fbcSAllan Jude         step = 1;
1105ff13fbcSAllan Jude         nextStep = ip + kStepIncr;
1115ff13fbcSAllan Jude         ip1 = ip + step;
1125ff13fbcSAllan Jude 
1135ff13fbcSAllan Jude         if (ip1 > ilimit) {
1145ff13fbcSAllan Jude             goto _cleanup;
1155ff13fbcSAllan Jude         }
1165ff13fbcSAllan Jude 
1175ff13fbcSAllan Jude         hl0 = ZSTD_hashPtr(ip, hBitsL, 8);
1185ff13fbcSAllan Jude         idxl0 = hashLong[hl0];
1195ff13fbcSAllan Jude         matchl0 = base + idxl0;
1205ff13fbcSAllan Jude 
1215ff13fbcSAllan Jude         /* Inner Loop: one iteration per search / position */
1225ff13fbcSAllan Jude         do {
1235ff13fbcSAllan Jude             const size_t hs0 = ZSTD_hashPtr(ip, hBitsS, mls);
1245ff13fbcSAllan Jude             const U32 idxs0 = hashSmall[hs0];
1255ff13fbcSAllan Jude             curr = (U32)(ip-base);
1265ff13fbcSAllan Jude             matchs0 = base + idxs0;
1275ff13fbcSAllan Jude 
1285ff13fbcSAllan Jude             hashLong[hl0] = hashSmall[hs0] = curr;   /* update hash tables */
1295ff13fbcSAllan Jude 
1305ff13fbcSAllan Jude             /* check noDict repcode */
1315ff13fbcSAllan Jude             if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
1325ff13fbcSAllan Jude                 mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
1335ff13fbcSAllan Jude                 ip++;
1345ff13fbcSAllan Jude                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_REPCODE_1, mLength);
1355ff13fbcSAllan Jude                 goto _match_stored;
1365ff13fbcSAllan Jude             }
1375ff13fbcSAllan Jude 
1385ff13fbcSAllan Jude             hl1 = ZSTD_hashPtr(ip1, hBitsL, 8);
1395ff13fbcSAllan Jude 
1405ff13fbcSAllan Jude             if (idxl0 > prefixLowestIndex) {
1415ff13fbcSAllan Jude                 /* check prefix long match */
1425ff13fbcSAllan Jude                 if (MEM_read64(matchl0) == MEM_read64(ip)) {
1435ff13fbcSAllan Jude                     mLength = ZSTD_count(ip+8, matchl0+8, iend) + 8;
1445ff13fbcSAllan Jude                     offset = (U32)(ip-matchl0);
1455ff13fbcSAllan Jude                     while (((ip>anchor) & (matchl0>prefixLowest)) && (ip[-1] == matchl0[-1])) { ip--; matchl0--; mLength++; } /* catch up */
1465ff13fbcSAllan Jude                     goto _match_found;
1475ff13fbcSAllan Jude                 }
1485ff13fbcSAllan Jude             }
1495ff13fbcSAllan Jude 
1505ff13fbcSAllan Jude             idxl1 = hashLong[hl1];
1515ff13fbcSAllan Jude             matchl1 = base + idxl1;
1525ff13fbcSAllan Jude 
1535ff13fbcSAllan Jude             if (idxs0 > prefixLowestIndex) {
1545ff13fbcSAllan Jude                 /* check prefix short match */
1555ff13fbcSAllan Jude                 if (MEM_read32(matchs0) == MEM_read32(ip)) {
1565ff13fbcSAllan Jude                     goto _search_next_long;
1575ff13fbcSAllan Jude                 }
1585ff13fbcSAllan Jude             }
1595ff13fbcSAllan Jude 
1605ff13fbcSAllan Jude             if (ip1 >= nextStep) {
1615ff13fbcSAllan Jude                 PREFETCH_L1(ip1 + 64);
1625ff13fbcSAllan Jude                 PREFETCH_L1(ip1 + 128);
1635ff13fbcSAllan Jude                 step++;
1645ff13fbcSAllan Jude                 nextStep += kStepIncr;
1655ff13fbcSAllan Jude             }
1665ff13fbcSAllan Jude             ip = ip1;
1675ff13fbcSAllan Jude             ip1 += step;
1685ff13fbcSAllan Jude 
1695ff13fbcSAllan Jude             hl0 = hl1;
1705ff13fbcSAllan Jude             idxl0 = idxl1;
1715ff13fbcSAllan Jude             matchl0 = matchl1;
1725ff13fbcSAllan Jude     #if defined(__aarch64__)
1735ff13fbcSAllan Jude             PREFETCH_L1(ip+256);
1745ff13fbcSAllan Jude     #endif
1755ff13fbcSAllan Jude         } while (ip1 <= ilimit);
1765ff13fbcSAllan Jude 
1775ff13fbcSAllan Jude _cleanup:
1785ff13fbcSAllan Jude         /* save reps for next block */
1795ff13fbcSAllan Jude         rep[0] = offset_1 ? offset_1 : offsetSaved;
1805ff13fbcSAllan Jude         rep[1] = offset_2 ? offset_2 : offsetSaved;
1815ff13fbcSAllan Jude 
1825ff13fbcSAllan Jude         /* Return the last literals size */
1835ff13fbcSAllan Jude         return (size_t)(iend - anchor);
1845ff13fbcSAllan Jude 
1855ff13fbcSAllan Jude _search_next_long:
1865ff13fbcSAllan Jude 
1875ff13fbcSAllan Jude         /* check prefix long +1 match */
1885ff13fbcSAllan Jude         if (idxl1 > prefixLowestIndex) {
1895ff13fbcSAllan Jude             if (MEM_read64(matchl1) == MEM_read64(ip1)) {
1905ff13fbcSAllan Jude                 ip = ip1;
1915ff13fbcSAllan Jude                 mLength = ZSTD_count(ip+8, matchl1+8, iend) + 8;
1925ff13fbcSAllan Jude                 offset = (U32)(ip-matchl1);
1935ff13fbcSAllan Jude                 while (((ip>anchor) & (matchl1>prefixLowest)) && (ip[-1] == matchl1[-1])) { ip--; matchl1--; mLength++; } /* catch up */
1945ff13fbcSAllan Jude                 goto _match_found;
1955ff13fbcSAllan Jude             }
1965ff13fbcSAllan Jude         }
1975ff13fbcSAllan Jude 
1985ff13fbcSAllan Jude         /* if no long +1 match, explore the short match we found */
1995ff13fbcSAllan Jude         mLength = ZSTD_count(ip+4, matchs0+4, iend) + 4;
2005ff13fbcSAllan Jude         offset = (U32)(ip - matchs0);
2015ff13fbcSAllan Jude         while (((ip>anchor) & (matchs0>prefixLowest)) && (ip[-1] == matchs0[-1])) { ip--; matchs0--; mLength++; } /* catch up */
2025ff13fbcSAllan Jude 
2035ff13fbcSAllan Jude         /* fall-through */
2045ff13fbcSAllan Jude 
2055ff13fbcSAllan Jude _match_found: /* requires ip, offset, mLength */
2065ff13fbcSAllan Jude         offset_2 = offset_1;
2075ff13fbcSAllan Jude         offset_1 = offset;
2085ff13fbcSAllan Jude 
2095ff13fbcSAllan Jude         if (step < 4) {
2105ff13fbcSAllan Jude             /* It is unsafe to write this value back to the hashtable when ip1 is
2115ff13fbcSAllan Jude              * greater than or equal to the new ip we will have after we're done
2125ff13fbcSAllan Jude              * processing this match. Rather than perform that test directly
2135ff13fbcSAllan Jude              * (ip1 >= ip + mLength), which costs speed in practice, we do a simpler
2145ff13fbcSAllan Jude              * more predictable test. The minmatch even if we take a short match is
2155ff13fbcSAllan Jude              * 4 bytes, so as long as step, the distance between ip and ip1
2165ff13fbcSAllan Jude              * (initially) is less than 4, we know ip1 < new ip. */
2175ff13fbcSAllan Jude             hashLong[hl1] = (U32)(ip1 - base);
2185ff13fbcSAllan Jude         }
2195ff13fbcSAllan Jude 
2205ff13fbcSAllan Jude         ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_OFFSET(offset), mLength);
2215ff13fbcSAllan Jude 
2225ff13fbcSAllan Jude _match_stored:
2235ff13fbcSAllan Jude         /* match found */
2245ff13fbcSAllan Jude         ip += mLength;
2255ff13fbcSAllan Jude         anchor = ip;
2265ff13fbcSAllan Jude 
2275ff13fbcSAllan Jude         if (ip <= ilimit) {
2285ff13fbcSAllan Jude             /* Complementary insertion */
2295ff13fbcSAllan Jude             /* done after iLimit test, as candidates could be > iend-8 */
2305ff13fbcSAllan Jude             {   U32 const indexToInsert = curr+2;
2315ff13fbcSAllan Jude                 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
2325ff13fbcSAllan Jude                 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
2335ff13fbcSAllan Jude                 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
2345ff13fbcSAllan Jude                 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
2355ff13fbcSAllan Jude             }
2365ff13fbcSAllan Jude 
2375ff13fbcSAllan Jude             /* check immediate repcode */
2385ff13fbcSAllan Jude             while ( (ip <= ilimit)
2395ff13fbcSAllan Jude                  && ( (offset_2>0)
2405ff13fbcSAllan Jude                     & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
2415ff13fbcSAllan Jude                 /* store sequence */
2425ff13fbcSAllan Jude                 size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
2435ff13fbcSAllan Jude                 U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff;  /* swap offset_2 <=> offset_1 */
2445ff13fbcSAllan Jude                 hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
2455ff13fbcSAllan Jude                 hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
2465ff13fbcSAllan Jude                 ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, rLength);
2475ff13fbcSAllan Jude                 ip += rLength;
2485ff13fbcSAllan Jude                 anchor = ip;
2495ff13fbcSAllan Jude                 continue;   /* faster when present ... (?) */
2505ff13fbcSAllan Jude             }
2515ff13fbcSAllan Jude         }
2525ff13fbcSAllan Jude     }
2535ff13fbcSAllan Jude }
2545ff13fbcSAllan Jude 
2555ff13fbcSAllan Jude 
2565ff13fbcSAllan Jude FORCE_INLINE_TEMPLATE
ZSTD_compressBlock_doubleFast_dictMatchState_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize,U32 const mls)2575ff13fbcSAllan Jude size_t ZSTD_compressBlock_doubleFast_dictMatchState_generic(
25819fcbaf1SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
2590f743729SConrad Meyer         void const* src, size_t srcSize,
2605ff13fbcSAllan Jude         U32 const mls /* template */)
2610c16b537SWarner Losh {
2620f743729SConrad Meyer     ZSTD_compressionParameters const* cParams = &ms->cParams;
26319fcbaf1SConrad Meyer     U32* const hashLong = ms->hashTable;
26419fcbaf1SConrad Meyer     const U32 hBitsL = cParams->hashLog;
26519fcbaf1SConrad Meyer     U32* const hashSmall = ms->chainTable;
26619fcbaf1SConrad Meyer     const U32 hBitsS = cParams->chainLog;
26719fcbaf1SConrad Meyer     const BYTE* const base = ms->window.base;
2680c16b537SWarner Losh     const BYTE* const istart = (const BYTE*)src;
2690c16b537SWarner Losh     const BYTE* ip = istart;
2700c16b537SWarner Losh     const BYTE* anchor = istart;
2714d3f1eafSConrad Meyer     const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
2729cbefe25SConrad Meyer     /* presumes that, if there is a dictionary, it must be using Attach mode */
27337f1f268SConrad Meyer     const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
2740f743729SConrad Meyer     const BYTE* const prefixLowest = base + prefixLowestIndex;
2750c16b537SWarner Losh     const BYTE* const iend = istart + srcSize;
2760c16b537SWarner Losh     const BYTE* const ilimit = iend - HASH_READ_SIZE;
27719fcbaf1SConrad Meyer     U32 offset_1=rep[0], offset_2=rep[1];
2780c16b537SWarner Losh     U32 offsetSaved = 0;
2790c16b537SWarner Losh 
2800f743729SConrad Meyer     const ZSTD_matchState_t* const dms = ms->dictMatchState;
2815ff13fbcSAllan Jude     const ZSTD_compressionParameters* const dictCParams = &dms->cParams;
2825ff13fbcSAllan Jude     const U32* const dictHashLong  = dms->hashTable;
2835ff13fbcSAllan Jude     const U32* const dictHashSmall = dms->chainTable;
2845ff13fbcSAllan Jude     const U32 dictStartIndex       = dms->window.dictLimit;
2855ff13fbcSAllan Jude     const BYTE* const dictBase     = dms->window.base;
2865ff13fbcSAllan Jude     const BYTE* const dictStart    = dictBase + dictStartIndex;
2875ff13fbcSAllan Jude     const BYTE* const dictEnd      = dms->window.nextSrc;
2885ff13fbcSAllan Jude     const U32 dictIndexDelta       = prefixLowestIndex - (U32)(dictEnd - dictBase);
2895ff13fbcSAllan Jude     const U32 dictHBitsL           = dictCParams->hashLog;
2905ff13fbcSAllan Jude     const U32 dictHBitsS           = dictCParams->chainLog;
29137f1f268SConrad Meyer     const U32 dictAndPrefixLength  = (U32)((ip - prefixLowest) + (dictEnd - dictStart));
2920f743729SConrad Meyer 
2935ff13fbcSAllan Jude     DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_dictMatchState_generic");
2940f743729SConrad Meyer 
2954d3f1eafSConrad Meyer     /* if a dictionary is attached, it must be within window range */
29637f1f268SConrad Meyer     assert(ms->window.dictLimit + (1U << cParams->windowLog) >= endIndex);
2974d3f1eafSConrad Meyer 
2980c16b537SWarner Losh     /* init */
2990f743729SConrad Meyer     ip += (dictAndPrefixLength == 0);
3005ff13fbcSAllan Jude 
3010f743729SConrad Meyer     /* dictMatchState repCode checks don't currently handle repCode == 0
3020f743729SConrad Meyer      * disabling. */
3030f743729SConrad Meyer     assert(offset_1 <= dictAndPrefixLength);
3040f743729SConrad Meyer     assert(offset_2 <= dictAndPrefixLength);
3050c16b537SWarner Losh 
3060c16b537SWarner Losh     /* Main Search Loop */
3070c16b537SWarner Losh     while (ip < ilimit) {   /* < instead of <=, because repcode check at (ip+1) */
3080c16b537SWarner Losh         size_t mLength;
3090f743729SConrad Meyer         U32 offset;
3100c16b537SWarner Losh         size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
3110c16b537SWarner Losh         size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
3120f743729SConrad Meyer         size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
3130f743729SConrad Meyer         size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
314f7cd7fe5SConrad Meyer         U32 const curr = (U32)(ip-base);
3150c16b537SWarner Losh         U32 const matchIndexL = hashLong[h2];
3160f743729SConrad Meyer         U32 matchIndexS = hashSmall[h];
3170c16b537SWarner Losh         const BYTE* matchLong = base + matchIndexL;
3180c16b537SWarner Losh         const BYTE* match = base + matchIndexS;
319f7cd7fe5SConrad Meyer         const U32 repIndex = curr + 1 - offset_1;
3205ff13fbcSAllan Jude         const BYTE* repMatch = (repIndex < prefixLowestIndex) ?
3210f743729SConrad Meyer                                dictBase + (repIndex - dictIndexDelta) :
3220f743729SConrad Meyer                                base + repIndex;
323f7cd7fe5SConrad Meyer         hashLong[h2] = hashSmall[h] = curr;   /* update hash tables */
3240c16b537SWarner Losh 
3255ff13fbcSAllan Jude         /* check repcode */
3265ff13fbcSAllan Jude         if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
3270f743729SConrad Meyer             && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
3280f743729SConrad Meyer             const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
3290f743729SConrad Meyer             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
3300f743729SConrad Meyer             ip++;
3315ff13fbcSAllan Jude             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_REPCODE_1, mLength);
3320f743729SConrad Meyer             goto _match_stored;
3330f743729SConrad Meyer         }
3340f743729SConrad Meyer 
3350f743729SConrad Meyer         if (matchIndexL > prefixLowestIndex) {
3360f743729SConrad Meyer             /* check prefix long match */
3370f743729SConrad Meyer             if (MEM_read64(matchLong) == MEM_read64(ip)) {
3380c16b537SWarner Losh                 mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
3390c16b537SWarner Losh                 offset = (U32)(ip-matchLong);
3400f743729SConrad Meyer                 while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
3410f743729SConrad Meyer                 goto _match_found;
3420f743729SConrad Meyer             }
3435ff13fbcSAllan Jude         } else {
3440f743729SConrad Meyer             /* check dictMatchState long match */
3450f743729SConrad Meyer             U32 const dictMatchIndexL = dictHashLong[dictHL];
3460f743729SConrad Meyer             const BYTE* dictMatchL = dictBase + dictMatchIndexL;
3470f743729SConrad Meyer             assert(dictMatchL < dictEnd);
3480f743729SConrad Meyer 
3490f743729SConrad Meyer             if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
3500f743729SConrad Meyer                 mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
351f7cd7fe5SConrad Meyer                 offset = (U32)(curr - dictMatchIndexL - dictIndexDelta);
3520f743729SConrad Meyer                 while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
3530f743729SConrad Meyer                 goto _match_found;
3544d3f1eafSConrad Meyer         }   }
3550f743729SConrad Meyer 
3560f743729SConrad Meyer         if (matchIndexS > prefixLowestIndex) {
3570f743729SConrad Meyer             /* check prefix short match */
3580f743729SConrad Meyer             if (MEM_read32(match) == MEM_read32(ip)) {
3590f743729SConrad Meyer                 goto _search_next_long;
3600f743729SConrad Meyer             }
3615ff13fbcSAllan Jude         } else {
3620f743729SConrad Meyer             /* check dictMatchState short match */
3630f743729SConrad Meyer             U32 const dictMatchIndexS = dictHashSmall[dictHS];
3640f743729SConrad Meyer             match = dictBase + dictMatchIndexS;
3650f743729SConrad Meyer             matchIndexS = dictMatchIndexS + dictIndexDelta;
3660f743729SConrad Meyer 
3670f743729SConrad Meyer             if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
3680f743729SConrad Meyer                 goto _search_next_long;
3694d3f1eafSConrad Meyer         }   }
3700f743729SConrad Meyer 
3710f743729SConrad Meyer         ip += ((ip-anchor) >> kSearchStrength) + 1;
37237f1f268SConrad Meyer #if defined(__aarch64__)
37337f1f268SConrad Meyer         PREFETCH_L1(ip+256);
37437f1f268SConrad Meyer #endif
3750f743729SConrad Meyer         continue;
3760f743729SConrad Meyer 
3770f743729SConrad Meyer _search_next_long:
3780f743729SConrad Meyer 
3794d3f1eafSConrad Meyer         {   size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
3800f743729SConrad Meyer             size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
3810c16b537SWarner Losh             U32 const matchIndexL3 = hashLong[hl3];
3820c16b537SWarner Losh             const BYTE* matchL3 = base + matchIndexL3;
383f7cd7fe5SConrad Meyer             hashLong[hl3] = curr + 1;
3840f743729SConrad Meyer 
3850f743729SConrad Meyer             /* check prefix long +1 match */
3860f743729SConrad Meyer             if (matchIndexL3 > prefixLowestIndex) {
3870f743729SConrad Meyer                 if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
3880c16b537SWarner Losh                     mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
3890c16b537SWarner Losh                     ip++;
3900c16b537SWarner Losh                     offset = (U32)(ip-matchL3);
3910f743729SConrad Meyer                     while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
3920f743729SConrad Meyer                     goto _match_found;
3930f743729SConrad Meyer                 }
3945ff13fbcSAllan Jude             } else {
3950f743729SConrad Meyer                 /* check dict long +1 match */
3960f743729SConrad Meyer                 U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
3970f743729SConrad Meyer                 const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
3980f743729SConrad Meyer                 assert(dictMatchL3 < dictEnd);
3990f743729SConrad Meyer                 if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
4000f743729SConrad Meyer                     mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
4010f743729SConrad Meyer                     ip++;
402f7cd7fe5SConrad Meyer                     offset = (U32)(curr + 1 - dictMatchIndexL3 - dictIndexDelta);
4030f743729SConrad Meyer                     while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
4040f743729SConrad Meyer                     goto _match_found;
4054d3f1eafSConrad Meyer         }   }   }
4060f743729SConrad Meyer 
4070f743729SConrad Meyer         /* if no long +1 match, explore the short match we found */
4085ff13fbcSAllan Jude         if (matchIndexS < prefixLowestIndex) {
4090f743729SConrad Meyer             mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
410f7cd7fe5SConrad Meyer             offset = (U32)(curr - matchIndexS);
4110f743729SConrad Meyer             while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
4120c16b537SWarner Losh         } else {
4130c16b537SWarner Losh             mLength = ZSTD_count(ip+4, match+4, iend) + 4;
4140c16b537SWarner Losh             offset = (U32)(ip - match);
4150f743729SConrad Meyer             while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
4160c16b537SWarner Losh         }
4170c16b537SWarner Losh 
4180f743729SConrad Meyer _match_found:
4190c16b537SWarner Losh         offset_2 = offset_1;
4200c16b537SWarner Losh         offset_1 = offset;
4210c16b537SWarner Losh 
4225ff13fbcSAllan Jude         ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_OFFSET(offset), mLength);
4230c16b537SWarner Losh 
4240f743729SConrad Meyer _match_stored:
4250c16b537SWarner Losh         /* match found */
4260c16b537SWarner Losh         ip += mLength;
4270c16b537SWarner Losh         anchor = ip;
4280c16b537SWarner Losh 
4290c16b537SWarner Losh         if (ip <= ilimit) {
4304d3f1eafSConrad Meyer             /* Complementary insertion */
4314d3f1eafSConrad Meyer             /* done after iLimit test, as candidates could be > iend-8 */
432f7cd7fe5SConrad Meyer             {   U32 const indexToInsert = curr+2;
4334d3f1eafSConrad Meyer                 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
4344d3f1eafSConrad Meyer                 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
4354d3f1eafSConrad Meyer                 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
4364d3f1eafSConrad Meyer                 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
4374d3f1eafSConrad Meyer             }
4380c16b537SWarner Losh 
4390c16b537SWarner Losh             /* check immediate repcode */
4400f743729SConrad Meyer             while (ip <= ilimit) {
4410f743729SConrad Meyer                 U32 const current2 = (U32)(ip-base);
4420f743729SConrad Meyer                 U32 const repIndex2 = current2 - offset_2;
4435ff13fbcSAllan Jude                 const BYTE* repMatch2 = repIndex2 < prefixLowestIndex ?
44437f1f268SConrad Meyer                         dictBase + repIndex2 - dictIndexDelta :
4450f743729SConrad Meyer                         base + repIndex2;
4460f743729SConrad Meyer                 if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
4470f743729SConrad Meyer                    && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
4480f743729SConrad Meyer                     const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
4490f743729SConrad Meyer                     size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
4500f743729SConrad Meyer                     U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
4515ff13fbcSAllan Jude                     ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, repLength2);
4520f743729SConrad Meyer                     hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
4530f743729SConrad Meyer                     hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
4540f743729SConrad Meyer                     ip += repLength2;
4550f743729SConrad Meyer                     anchor = ip;
4560f743729SConrad Meyer                     continue;
4570f743729SConrad Meyer                 }
4580f743729SConrad Meyer                 break;
4595ff13fbcSAllan Jude             }
4605ff13fbcSAllan Jude         }
4614d3f1eafSConrad Meyer     }   /* while (ip < ilimit) */
4620c16b537SWarner Losh 
4630c16b537SWarner Losh     /* save reps for next block */
46419fcbaf1SConrad Meyer     rep[0] = offset_1 ? offset_1 : offsetSaved;
46519fcbaf1SConrad Meyer     rep[1] = offset_2 ? offset_2 : offsetSaved;
4660c16b537SWarner Losh 
4670c16b537SWarner Losh     /* Return the last literals size */
4684d3f1eafSConrad Meyer     return (size_t)(iend - anchor);
4690c16b537SWarner Losh }
4700c16b537SWarner Losh 
4715ff13fbcSAllan Jude #define ZSTD_GEN_DFAST_FN(dictMode, mls)                                                                 \
4725ff13fbcSAllan Jude     static size_t ZSTD_compressBlock_doubleFast_##dictMode##_##mls(                                      \
4735ff13fbcSAllan Jude             ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],                          \
4745ff13fbcSAllan Jude             void const* src, size_t srcSize)                                                             \
4755ff13fbcSAllan Jude     {                                                                                                    \
4765ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_##dictMode##_generic(ms, seqStore, rep, src, srcSize, mls); \
4775ff13fbcSAllan Jude     }
4785ff13fbcSAllan Jude 
4795ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(noDict, 4)
4805ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(noDict, 5)
4815ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(noDict, 6)
4825ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(noDict, 7)
4835ff13fbcSAllan Jude 
4845ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(dictMatchState, 4)
4855ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(dictMatchState, 5)
4865ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(dictMatchState, 6)
4875ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(dictMatchState, 7)
4885ff13fbcSAllan Jude 
4890c16b537SWarner Losh 
ZSTD_compressBlock_doubleFast(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)49019fcbaf1SConrad Meyer size_t ZSTD_compressBlock_doubleFast(
49119fcbaf1SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
4920f743729SConrad Meyer         void const* src, size_t srcSize)
4930c16b537SWarner Losh {
494a0483764SConrad Meyer     const U32 mls = ms->cParams.minMatch;
4950c16b537SWarner Losh     switch(mls)
4960c16b537SWarner Losh     {
4970c16b537SWarner Losh     default: /* includes case 3 */
4980c16b537SWarner Losh     case 4 :
4995ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_noDict_4(ms, seqStore, rep, src, srcSize);
5000c16b537SWarner Losh     case 5 :
5015ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_noDict_5(ms, seqStore, rep, src, srcSize);
5020c16b537SWarner Losh     case 6 :
5035ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_noDict_6(ms, seqStore, rep, src, srcSize);
5040c16b537SWarner Losh     case 7 :
5055ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_noDict_7(ms, seqStore, rep, src, srcSize);
5060f743729SConrad Meyer     }
5070f743729SConrad Meyer }
5080f743729SConrad Meyer 
5090f743729SConrad Meyer 
ZSTD_compressBlock_doubleFast_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)5100f743729SConrad Meyer size_t ZSTD_compressBlock_doubleFast_dictMatchState(
5110f743729SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
5120f743729SConrad Meyer         void const* src, size_t srcSize)
5130f743729SConrad Meyer {
514a0483764SConrad Meyer     const U32 mls = ms->cParams.minMatch;
5150f743729SConrad Meyer     switch(mls)
5160f743729SConrad Meyer     {
5170f743729SConrad Meyer     default: /* includes case 3 */
5180f743729SConrad Meyer     case 4 :
5195ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_dictMatchState_4(ms, seqStore, rep, src, srcSize);
5200f743729SConrad Meyer     case 5 :
5215ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_dictMatchState_5(ms, seqStore, rep, src, srcSize);
5220f743729SConrad Meyer     case 6 :
5235ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_dictMatchState_6(ms, seqStore, rep, src, srcSize);
5240f743729SConrad Meyer     case 7 :
5255ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_dictMatchState_7(ms, seqStore, rep, src, srcSize);
5260c16b537SWarner Losh     }
5270c16b537SWarner Losh }
5280c16b537SWarner Losh 
5290c16b537SWarner Losh 
ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize,U32 const mls)53019fcbaf1SConrad Meyer static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
53119fcbaf1SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
5320f743729SConrad Meyer         void const* src, size_t srcSize,
53319fcbaf1SConrad Meyer         U32 const mls /* template */)
5340c16b537SWarner Losh {
5350f743729SConrad Meyer     ZSTD_compressionParameters const* cParams = &ms->cParams;
53619fcbaf1SConrad Meyer     U32* const hashLong = ms->hashTable;
53719fcbaf1SConrad Meyer     U32  const hBitsL = cParams->hashLog;
53819fcbaf1SConrad Meyer     U32* const hashSmall = ms->chainTable;
53919fcbaf1SConrad Meyer     U32  const hBitsS = cParams->chainLog;
5400c16b537SWarner Losh     const BYTE* const istart = (const BYTE*)src;
5410c16b537SWarner Losh     const BYTE* ip = istart;
5420c16b537SWarner Losh     const BYTE* anchor = istart;
5430c16b537SWarner Losh     const BYTE* const iend = istart + srcSize;
5440c16b537SWarner Losh     const BYTE* const ilimit = iend - 8;
5450f743729SConrad Meyer     const BYTE* const base = ms->window.base;
5464d3f1eafSConrad Meyer     const U32   endIndex = (U32)((size_t)(istart - base) + srcSize);
5479cbefe25SConrad Meyer     const U32   lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
5484d3f1eafSConrad Meyer     const U32   dictStartIndex = lowLimit;
5494d3f1eafSConrad Meyer     const U32   dictLimit = ms->window.dictLimit;
5504d3f1eafSConrad Meyer     const U32   prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
5510f743729SConrad Meyer     const BYTE* const prefixStart = base + prefixStartIndex;
5520f743729SConrad Meyer     const BYTE* const dictBase = ms->window.dictBase;
5530f743729SConrad Meyer     const BYTE* const dictStart = dictBase + dictStartIndex;
5540f743729SConrad Meyer     const BYTE* const dictEnd = dictBase + prefixStartIndex;
55519fcbaf1SConrad Meyer     U32 offset_1=rep[0], offset_2=rep[1];
5560c16b537SWarner Losh 
5570f743729SConrad Meyer     DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
5580f743729SConrad Meyer 
5594d3f1eafSConrad Meyer     /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
5604d3f1eafSConrad Meyer     if (prefixStartIndex == dictStartIndex)
5615ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast(ms, seqStore, rep, src, srcSize);
5624d3f1eafSConrad Meyer 
5630c16b537SWarner Losh     /* Search Loop */
5640c16b537SWarner Losh     while (ip < ilimit) {  /* < instead of <=, because (ip+1) */
5650c16b537SWarner Losh         const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
5660c16b537SWarner Losh         const U32 matchIndex = hashSmall[hSmall];
5670f743729SConrad Meyer         const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
5680c16b537SWarner Losh         const BYTE* match = matchBase + matchIndex;
5690c16b537SWarner Losh 
5700c16b537SWarner Losh         const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
5710c16b537SWarner Losh         const U32 matchLongIndex = hashLong[hLong];
5720f743729SConrad Meyer         const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
5730c16b537SWarner Losh         const BYTE* matchLong = matchLongBase + matchLongIndex;
5740c16b537SWarner Losh 
575f7cd7fe5SConrad Meyer         const U32 curr = (U32)(ip-base);
576f7cd7fe5SConrad Meyer         const U32 repIndex = curr + 1 - offset_1;   /* offset_1 expected <= curr +1 */
5770f743729SConrad Meyer         const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
5780f743729SConrad Meyer         const BYTE* const repMatch = repBase + repIndex;
5790c16b537SWarner Losh         size_t mLength;
580f7cd7fe5SConrad Meyer         hashSmall[hSmall] = hashLong[hLong] = curr;   /* update hash table */
5810c16b537SWarner Losh 
5820f743729SConrad Meyer         if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
5835ff13fbcSAllan Jude             & (offset_1 <= curr+1 - dictStartIndex)) /* note: we are searching at curr+1 */
5840c16b537SWarner Losh           && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
5850f743729SConrad Meyer             const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
5860f743729SConrad Meyer             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
5870c16b537SWarner Losh             ip++;
5885ff13fbcSAllan Jude             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_REPCODE_1, mLength);
5890c16b537SWarner Losh         } else {
5900f743729SConrad Meyer             if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
5910f743729SConrad Meyer                 const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
5920f743729SConrad Meyer                 const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
5930c16b537SWarner Losh                 U32 offset;
5940f743729SConrad Meyer                 mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
595f7cd7fe5SConrad Meyer                 offset = curr - matchLongIndex;
5960c16b537SWarner Losh                 while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; }   /* catch up */
5970c16b537SWarner Losh                 offset_2 = offset_1;
5980c16b537SWarner Losh                 offset_1 = offset;
5995ff13fbcSAllan Jude                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_OFFSET(offset), mLength);
6000c16b537SWarner Losh 
6010f743729SConrad Meyer             } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
6020c16b537SWarner Losh                 size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
6030c16b537SWarner Losh                 U32 const matchIndex3 = hashLong[h3];
6040f743729SConrad Meyer                 const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
6050c16b537SWarner Losh                 const BYTE* match3 = match3Base + matchIndex3;
6060c16b537SWarner Losh                 U32 offset;
607f7cd7fe5SConrad Meyer                 hashLong[h3] = curr + 1;
6080f743729SConrad Meyer                 if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
6090f743729SConrad Meyer                     const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
6100f743729SConrad Meyer                     const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
6110f743729SConrad Meyer                     mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
6120c16b537SWarner Losh                     ip++;
613f7cd7fe5SConrad Meyer                     offset = curr+1 - matchIndex3;
6140c16b537SWarner Losh                     while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
6150c16b537SWarner Losh                 } else {
6160f743729SConrad Meyer                     const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
6170f743729SConrad Meyer                     const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
6180f743729SConrad Meyer                     mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
619f7cd7fe5SConrad Meyer                     offset = curr - matchIndex;
6200c16b537SWarner Losh                     while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; }   /* catch up */
6210c16b537SWarner Losh                 }
6220c16b537SWarner Losh                 offset_2 = offset_1;
6230c16b537SWarner Losh                 offset_1 = offset;
6245ff13fbcSAllan Jude                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, STORE_OFFSET(offset), mLength);
6250c16b537SWarner Losh 
6260c16b537SWarner Losh             } else {
62719fcbaf1SConrad Meyer                 ip += ((ip-anchor) >> kSearchStrength) + 1;
6280c16b537SWarner Losh                 continue;
6290c16b537SWarner Losh         }   }
6300c16b537SWarner Losh 
6314d3f1eafSConrad Meyer         /* move to next sequence start */
6320c16b537SWarner Losh         ip += mLength;
6330c16b537SWarner Losh         anchor = ip;
6340c16b537SWarner Losh 
6350c16b537SWarner Losh         if (ip <= ilimit) {
6364d3f1eafSConrad Meyer             /* Complementary insertion */
6374d3f1eafSConrad Meyer             /* done after iLimit test, as candidates could be > iend-8 */
638f7cd7fe5SConrad Meyer             {   U32 const indexToInsert = curr+2;
6394d3f1eafSConrad Meyer                 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
6400c16b537SWarner Losh                 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
6414d3f1eafSConrad Meyer                 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
6424d3f1eafSConrad Meyer                 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
6434d3f1eafSConrad Meyer             }
6444d3f1eafSConrad Meyer 
6450c16b537SWarner Losh             /* check immediate repcode */
6460c16b537SWarner Losh             while (ip <= ilimit) {
6470c16b537SWarner Losh                 U32 const current2 = (U32)(ip-base);
6480c16b537SWarner Losh                 U32 const repIndex2 = current2 - offset_2;
6490f743729SConrad Meyer                 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
6500f743729SConrad Meyer                 if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3)   /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
6515ff13fbcSAllan Jude                     & (offset_2 <= current2 - dictStartIndex))
6520c16b537SWarner Losh                   && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
6530f743729SConrad Meyer                     const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
6540f743729SConrad Meyer                     size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
6550f743729SConrad Meyer                     U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
6565ff13fbcSAllan Jude                     ZSTD_storeSeq(seqStore, 0, anchor, iend, STORE_REPCODE_1, repLength2);
6570c16b537SWarner Losh                     hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
6580c16b537SWarner Losh                     hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
6590c16b537SWarner Losh                     ip += repLength2;
6600c16b537SWarner Losh                     anchor = ip;
6610c16b537SWarner Losh                     continue;
6620c16b537SWarner Losh                 }
6630c16b537SWarner Losh                 break;
6640c16b537SWarner Losh     }   }   }
6650c16b537SWarner Losh 
6660c16b537SWarner Losh     /* save reps for next block */
66719fcbaf1SConrad Meyer     rep[0] = offset_1;
66819fcbaf1SConrad Meyer     rep[1] = offset_2;
6690c16b537SWarner Losh 
6700c16b537SWarner Losh     /* Return the last literals size */
6714d3f1eafSConrad Meyer     return (size_t)(iend - anchor);
6720c16b537SWarner Losh }
6730c16b537SWarner Losh 
6745ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(extDict, 4)
6755ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(extDict, 5)
6765ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(extDict, 6)
6775ff13fbcSAllan Jude ZSTD_GEN_DFAST_FN(extDict, 7)
6780c16b537SWarner Losh 
ZSTD_compressBlock_doubleFast_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)67919fcbaf1SConrad Meyer size_t ZSTD_compressBlock_doubleFast_extDict(
68019fcbaf1SConrad Meyer         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
6810f743729SConrad Meyer         void const* src, size_t srcSize)
6820c16b537SWarner Losh {
683a0483764SConrad Meyer     U32 const mls = ms->cParams.minMatch;
6840c16b537SWarner Losh     switch(mls)
6850c16b537SWarner Losh     {
6860c16b537SWarner Losh     default: /* includes case 3 */
6870c16b537SWarner Losh     case 4 :
6885ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_extDict_4(ms, seqStore, rep, src, srcSize);
6890c16b537SWarner Losh     case 5 :
6905ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_extDict_5(ms, seqStore, rep, src, srcSize);
6910c16b537SWarner Losh     case 6 :
6925ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_extDict_6(ms, seqStore, rep, src, srcSize);
6930c16b537SWarner Losh     case 7 :
6945ff13fbcSAllan Jude         return ZSTD_compressBlock_doubleFast_extDict_7(ms, seqStore, rep, src, srcSize);
6950c16b537SWarner Losh     }
6960c16b537SWarner Losh }
697