1*e0c1b49fSNick Terrell /* 2*e0c1b49fSNick Terrell * Copyright (c) Yann Collet, Facebook, Inc. 3*e0c1b49fSNick Terrell * All rights reserved. 4*e0c1b49fSNick Terrell * 5*e0c1b49fSNick Terrell * This source code is licensed under both the BSD-style license (found in the 6*e0c1b49fSNick Terrell * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*e0c1b49fSNick Terrell * in the COPYING file in the root directory of this source tree). 8*e0c1b49fSNick Terrell * You may select, at your option, one of the above-listed licenses. 9*e0c1b49fSNick Terrell */ 10*e0c1b49fSNick Terrell 11*e0c1b49fSNick Terrell #include "zstd_compress_internal.h" /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */ 12*e0c1b49fSNick Terrell #include "zstd_fast.h" 13*e0c1b49fSNick Terrell 14*e0c1b49fSNick Terrell 15*e0c1b49fSNick Terrell void ZSTD_fillHashTable(ZSTD_matchState_t* ms, 16*e0c1b49fSNick Terrell const void* const end, 17*e0c1b49fSNick Terrell ZSTD_dictTableLoadMethod_e dtlm) 18*e0c1b49fSNick Terrell { 19*e0c1b49fSNick Terrell const ZSTD_compressionParameters* const cParams = &ms->cParams; 20*e0c1b49fSNick Terrell U32* const hashTable = ms->hashTable; 21*e0c1b49fSNick Terrell U32 const hBits = cParams->hashLog; 22*e0c1b49fSNick Terrell U32 const mls = cParams->minMatch; 23*e0c1b49fSNick Terrell const BYTE* const base = ms->window.base; 24*e0c1b49fSNick Terrell const BYTE* ip = base + ms->nextToUpdate; 25*e0c1b49fSNick Terrell const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; 26*e0c1b49fSNick Terrell const U32 fastHashFillStep = 3; 27*e0c1b49fSNick Terrell 28*e0c1b49fSNick Terrell /* Always insert every fastHashFillStep position into the hash table. 29*e0c1b49fSNick Terrell * Insert the other positions if their hash entry is empty. 30*e0c1b49fSNick Terrell */ 31*e0c1b49fSNick Terrell for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) { 32*e0c1b49fSNick Terrell U32 const curr = (U32)(ip - base); 33*e0c1b49fSNick Terrell size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls); 34*e0c1b49fSNick Terrell hashTable[hash0] = curr; 35*e0c1b49fSNick Terrell if (dtlm == ZSTD_dtlm_fast) continue; 36*e0c1b49fSNick Terrell /* Only load extra positions for ZSTD_dtlm_full */ 37*e0c1b49fSNick Terrell { U32 p; 38*e0c1b49fSNick Terrell for (p = 1; p < fastHashFillStep; ++p) { 39*e0c1b49fSNick Terrell size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls); 40*e0c1b49fSNick Terrell if (hashTable[hash] == 0) { /* not yet filled */ 41*e0c1b49fSNick Terrell hashTable[hash] = curr + p; 42*e0c1b49fSNick Terrell } } } } 43*e0c1b49fSNick Terrell } 44*e0c1b49fSNick Terrell 45*e0c1b49fSNick Terrell 46*e0c1b49fSNick Terrell FORCE_INLINE_TEMPLATE size_t 47*e0c1b49fSNick Terrell ZSTD_compressBlock_fast_generic( 48*e0c1b49fSNick Terrell ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 49*e0c1b49fSNick Terrell void const* src, size_t srcSize, 50*e0c1b49fSNick Terrell U32 const mls) 51*e0c1b49fSNick Terrell { 52*e0c1b49fSNick Terrell const ZSTD_compressionParameters* const cParams = &ms->cParams; 53*e0c1b49fSNick Terrell U32* const hashTable = ms->hashTable; 54*e0c1b49fSNick Terrell U32 const hlog = cParams->hashLog; 55*e0c1b49fSNick Terrell /* support stepSize of 0 */ 56*e0c1b49fSNick Terrell size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1; 57*e0c1b49fSNick Terrell const BYTE* const base = ms->window.base; 58*e0c1b49fSNick Terrell const BYTE* const istart = (const BYTE*)src; 59*e0c1b49fSNick Terrell /* We check ip0 (ip + 0) and ip1 (ip + 1) each loop */ 60*e0c1b49fSNick Terrell const BYTE* ip0 = istart; 61*e0c1b49fSNick Terrell const BYTE* ip1; 62*e0c1b49fSNick Terrell const BYTE* anchor = istart; 63*e0c1b49fSNick Terrell const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); 64*e0c1b49fSNick Terrell const U32 prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog); 65*e0c1b49fSNick Terrell const BYTE* const prefixStart = base + prefixStartIndex; 66*e0c1b49fSNick Terrell const BYTE* const iend = istart + srcSize; 67*e0c1b49fSNick Terrell const BYTE* const ilimit = iend - HASH_READ_SIZE; 68*e0c1b49fSNick Terrell U32 offset_1=rep[0], offset_2=rep[1]; 69*e0c1b49fSNick Terrell U32 offsetSaved = 0; 70*e0c1b49fSNick Terrell 71*e0c1b49fSNick Terrell /* init */ 72*e0c1b49fSNick Terrell DEBUGLOG(5, "ZSTD_compressBlock_fast_generic"); 73*e0c1b49fSNick Terrell ip0 += (ip0 == prefixStart); 74*e0c1b49fSNick Terrell ip1 = ip0 + 1; 75*e0c1b49fSNick Terrell { U32 const curr = (U32)(ip0 - base); 76*e0c1b49fSNick Terrell U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog); 77*e0c1b49fSNick Terrell U32 const maxRep = curr - windowLow; 78*e0c1b49fSNick Terrell if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; 79*e0c1b49fSNick Terrell if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; 80*e0c1b49fSNick Terrell } 81*e0c1b49fSNick Terrell 82*e0c1b49fSNick Terrell /* Main Search Loop */ 83*e0c1b49fSNick Terrell #ifdef __INTEL_COMPILER 84*e0c1b49fSNick Terrell /* From intel 'The vector pragma indicates that the loop should be 85*e0c1b49fSNick Terrell * vectorized if it is legal to do so'. Can be used together with 86*e0c1b49fSNick Terrell * #pragma ivdep (but have opted to exclude that because intel 87*e0c1b49fSNick Terrell * warns against using it).*/ 88*e0c1b49fSNick Terrell #pragma vector always 89*e0c1b49fSNick Terrell #endif 90*e0c1b49fSNick Terrell while (ip1 < ilimit) { /* < instead of <=, because check at ip0+2 */ 91*e0c1b49fSNick Terrell size_t mLength; 92*e0c1b49fSNick Terrell BYTE const* ip2 = ip0 + 2; 93*e0c1b49fSNick Terrell size_t const h0 = ZSTD_hashPtr(ip0, hlog, mls); 94*e0c1b49fSNick Terrell U32 const val0 = MEM_read32(ip0); 95*e0c1b49fSNick Terrell size_t const h1 = ZSTD_hashPtr(ip1, hlog, mls); 96*e0c1b49fSNick Terrell U32 const val1 = MEM_read32(ip1); 97*e0c1b49fSNick Terrell U32 const current0 = (U32)(ip0-base); 98*e0c1b49fSNick Terrell U32 const current1 = (U32)(ip1-base); 99*e0c1b49fSNick Terrell U32 const matchIndex0 = hashTable[h0]; 100*e0c1b49fSNick Terrell U32 const matchIndex1 = hashTable[h1]; 101*e0c1b49fSNick Terrell BYTE const* repMatch = ip2 - offset_1; 102*e0c1b49fSNick Terrell const BYTE* match0 = base + matchIndex0; 103*e0c1b49fSNick Terrell const BYTE* match1 = base + matchIndex1; 104*e0c1b49fSNick Terrell U32 offcode; 105*e0c1b49fSNick Terrell 106*e0c1b49fSNick Terrell #if defined(__aarch64__) 107*e0c1b49fSNick Terrell PREFETCH_L1(ip0+256); 108*e0c1b49fSNick Terrell #endif 109*e0c1b49fSNick Terrell 110*e0c1b49fSNick Terrell hashTable[h0] = current0; /* update hash table */ 111*e0c1b49fSNick Terrell hashTable[h1] = current1; /* update hash table */ 112*e0c1b49fSNick Terrell 113*e0c1b49fSNick Terrell assert(ip0 + 1 == ip1); 114*e0c1b49fSNick Terrell 115*e0c1b49fSNick Terrell if ((offset_1 > 0) & (MEM_read32(repMatch) == MEM_read32(ip2))) { 116*e0c1b49fSNick Terrell mLength = (ip2[-1] == repMatch[-1]) ? 1 : 0; 117*e0c1b49fSNick Terrell ip0 = ip2 - mLength; 118*e0c1b49fSNick Terrell match0 = repMatch - mLength; 119*e0c1b49fSNick Terrell mLength += 4; 120*e0c1b49fSNick Terrell offcode = 0; 121*e0c1b49fSNick Terrell goto _match; 122*e0c1b49fSNick Terrell } 123*e0c1b49fSNick Terrell if ((matchIndex0 > prefixStartIndex) && MEM_read32(match0) == val0) { 124*e0c1b49fSNick Terrell /* found a regular match */ 125*e0c1b49fSNick Terrell goto _offset; 126*e0c1b49fSNick Terrell } 127*e0c1b49fSNick Terrell if ((matchIndex1 > prefixStartIndex) && MEM_read32(match1) == val1) { 128*e0c1b49fSNick Terrell /* found a regular match after one literal */ 129*e0c1b49fSNick Terrell ip0 = ip1; 130*e0c1b49fSNick Terrell match0 = match1; 131*e0c1b49fSNick Terrell goto _offset; 132*e0c1b49fSNick Terrell } 133*e0c1b49fSNick Terrell { size_t const step = ((size_t)(ip0-anchor) >> (kSearchStrength - 1)) + stepSize; 134*e0c1b49fSNick Terrell assert(step >= 2); 135*e0c1b49fSNick Terrell ip0 += step; 136*e0c1b49fSNick Terrell ip1 += step; 137*e0c1b49fSNick Terrell continue; 138*e0c1b49fSNick Terrell } 139*e0c1b49fSNick Terrell _offset: /* Requires: ip0, match0 */ 140*e0c1b49fSNick Terrell /* Compute the offset code */ 141*e0c1b49fSNick Terrell offset_2 = offset_1; 142*e0c1b49fSNick Terrell offset_1 = (U32)(ip0-match0); 143*e0c1b49fSNick Terrell offcode = offset_1 + ZSTD_REP_MOVE; 144*e0c1b49fSNick Terrell mLength = 4; 145*e0c1b49fSNick Terrell /* Count the backwards match length */ 146*e0c1b49fSNick Terrell while (((ip0>anchor) & (match0>prefixStart)) 147*e0c1b49fSNick Terrell && (ip0[-1] == match0[-1])) { ip0--; match0--; mLength++; } /* catch up */ 148*e0c1b49fSNick Terrell 149*e0c1b49fSNick Terrell _match: /* Requires: ip0, match0, offcode */ 150*e0c1b49fSNick Terrell /* Count the forward length */ 151*e0c1b49fSNick Terrell mLength += ZSTD_count(ip0+mLength, match0+mLength, iend); 152*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, (size_t)(ip0-anchor), anchor, iend, offcode, mLength-MINMATCH); 153*e0c1b49fSNick Terrell /* match found */ 154*e0c1b49fSNick Terrell ip0 += mLength; 155*e0c1b49fSNick Terrell anchor = ip0; 156*e0c1b49fSNick Terrell 157*e0c1b49fSNick Terrell if (ip0 <= ilimit) { 158*e0c1b49fSNick Terrell /* Fill Table */ 159*e0c1b49fSNick Terrell assert(base+current0+2 > istart); /* check base overflow */ 160*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */ 161*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base); 162*e0c1b49fSNick Terrell 163*e0c1b49fSNick Terrell if (offset_2 > 0) { /* offset_2==0 means offset_2 is invalidated */ 164*e0c1b49fSNick Terrell while ( (ip0 <= ilimit) && (MEM_read32(ip0) == MEM_read32(ip0 - offset_2)) ) { 165*e0c1b49fSNick Terrell /* store sequence */ 166*e0c1b49fSNick Terrell size_t const rLength = ZSTD_count(ip0+4, ip0+4-offset_2, iend) + 4; 167*e0c1b49fSNick Terrell { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */ 168*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base); 169*e0c1b49fSNick Terrell ip0 += rLength; 170*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, 0 /*offCode*/, rLength-MINMATCH); 171*e0c1b49fSNick Terrell anchor = ip0; 172*e0c1b49fSNick Terrell continue; /* faster when present (confirmed on gcc-8) ... (?) */ 173*e0c1b49fSNick Terrell } } } 174*e0c1b49fSNick Terrell ip1 = ip0 + 1; 175*e0c1b49fSNick Terrell } 176*e0c1b49fSNick Terrell 177*e0c1b49fSNick Terrell /* save reps for next block */ 178*e0c1b49fSNick Terrell rep[0] = offset_1 ? offset_1 : offsetSaved; 179*e0c1b49fSNick Terrell rep[1] = offset_2 ? offset_2 : offsetSaved; 180*e0c1b49fSNick Terrell 181*e0c1b49fSNick Terrell /* Return the last literals size */ 182*e0c1b49fSNick Terrell return (size_t)(iend - anchor); 183*e0c1b49fSNick Terrell } 184*e0c1b49fSNick Terrell 185*e0c1b49fSNick Terrell 186*e0c1b49fSNick Terrell size_t ZSTD_compressBlock_fast( 187*e0c1b49fSNick Terrell ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 188*e0c1b49fSNick Terrell void const* src, size_t srcSize) 189*e0c1b49fSNick Terrell { 190*e0c1b49fSNick Terrell U32 const mls = ms->cParams.minMatch; 191*e0c1b49fSNick Terrell assert(ms->dictMatchState == NULL); 192*e0c1b49fSNick Terrell switch(mls) 193*e0c1b49fSNick Terrell { 194*e0c1b49fSNick Terrell default: /* includes case 3 */ 195*e0c1b49fSNick Terrell case 4 : 196*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4); 197*e0c1b49fSNick Terrell case 5 : 198*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5); 199*e0c1b49fSNick Terrell case 6 : 200*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6); 201*e0c1b49fSNick Terrell case 7 : 202*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7); 203*e0c1b49fSNick Terrell } 204*e0c1b49fSNick Terrell } 205*e0c1b49fSNick Terrell 206*e0c1b49fSNick Terrell FORCE_INLINE_TEMPLATE 207*e0c1b49fSNick Terrell size_t ZSTD_compressBlock_fast_dictMatchState_generic( 208*e0c1b49fSNick Terrell ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 209*e0c1b49fSNick Terrell void const* src, size_t srcSize, U32 const mls) 210*e0c1b49fSNick Terrell { 211*e0c1b49fSNick Terrell const ZSTD_compressionParameters* const cParams = &ms->cParams; 212*e0c1b49fSNick Terrell U32* const hashTable = ms->hashTable; 213*e0c1b49fSNick Terrell U32 const hlog = cParams->hashLog; 214*e0c1b49fSNick Terrell /* support stepSize of 0 */ 215*e0c1b49fSNick Terrell U32 const stepSize = cParams->targetLength + !(cParams->targetLength); 216*e0c1b49fSNick Terrell const BYTE* const base = ms->window.base; 217*e0c1b49fSNick Terrell const BYTE* const istart = (const BYTE*)src; 218*e0c1b49fSNick Terrell const BYTE* ip = istart; 219*e0c1b49fSNick Terrell const BYTE* anchor = istart; 220*e0c1b49fSNick Terrell const U32 prefixStartIndex = ms->window.dictLimit; 221*e0c1b49fSNick Terrell const BYTE* const prefixStart = base + prefixStartIndex; 222*e0c1b49fSNick Terrell const BYTE* const iend = istart + srcSize; 223*e0c1b49fSNick Terrell const BYTE* const ilimit = iend - HASH_READ_SIZE; 224*e0c1b49fSNick Terrell U32 offset_1=rep[0], offset_2=rep[1]; 225*e0c1b49fSNick Terrell U32 offsetSaved = 0; 226*e0c1b49fSNick Terrell 227*e0c1b49fSNick Terrell const ZSTD_matchState_t* const dms = ms->dictMatchState; 228*e0c1b49fSNick Terrell const ZSTD_compressionParameters* const dictCParams = &dms->cParams ; 229*e0c1b49fSNick Terrell const U32* const dictHashTable = dms->hashTable; 230*e0c1b49fSNick Terrell const U32 dictStartIndex = dms->window.dictLimit; 231*e0c1b49fSNick Terrell const BYTE* const dictBase = dms->window.base; 232*e0c1b49fSNick Terrell const BYTE* const dictStart = dictBase + dictStartIndex; 233*e0c1b49fSNick Terrell const BYTE* const dictEnd = dms->window.nextSrc; 234*e0c1b49fSNick Terrell const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase); 235*e0c1b49fSNick Terrell const U32 dictAndPrefixLength = (U32)(ip - prefixStart + dictEnd - dictStart); 236*e0c1b49fSNick Terrell const U32 dictHLog = dictCParams->hashLog; 237*e0c1b49fSNick Terrell 238*e0c1b49fSNick Terrell /* if a dictionary is still attached, it necessarily means that 239*e0c1b49fSNick Terrell * it is within window size. So we just check it. */ 240*e0c1b49fSNick Terrell const U32 maxDistance = 1U << cParams->windowLog; 241*e0c1b49fSNick Terrell const U32 endIndex = (U32)((size_t)(ip - base) + srcSize); 242*e0c1b49fSNick Terrell assert(endIndex - prefixStartIndex <= maxDistance); 243*e0c1b49fSNick Terrell (void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */ 244*e0c1b49fSNick Terrell 245*e0c1b49fSNick Terrell /* ensure there will be no underflow 246*e0c1b49fSNick Terrell * when translating a dict index into a local index */ 247*e0c1b49fSNick Terrell assert(prefixStartIndex >= (U32)(dictEnd - dictBase)); 248*e0c1b49fSNick Terrell 249*e0c1b49fSNick Terrell /* init */ 250*e0c1b49fSNick Terrell DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic"); 251*e0c1b49fSNick Terrell ip += (dictAndPrefixLength == 0); 252*e0c1b49fSNick Terrell /* dictMatchState repCode checks don't currently handle repCode == 0 253*e0c1b49fSNick Terrell * disabling. */ 254*e0c1b49fSNick Terrell assert(offset_1 <= dictAndPrefixLength); 255*e0c1b49fSNick Terrell assert(offset_2 <= dictAndPrefixLength); 256*e0c1b49fSNick Terrell 257*e0c1b49fSNick Terrell /* Main Search Loop */ 258*e0c1b49fSNick Terrell while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ 259*e0c1b49fSNick Terrell size_t mLength; 260*e0c1b49fSNick Terrell size_t const h = ZSTD_hashPtr(ip, hlog, mls); 261*e0c1b49fSNick Terrell U32 const curr = (U32)(ip-base); 262*e0c1b49fSNick Terrell U32 const matchIndex = hashTable[h]; 263*e0c1b49fSNick Terrell const BYTE* match = base + matchIndex; 264*e0c1b49fSNick Terrell const U32 repIndex = curr + 1 - offset_1; 265*e0c1b49fSNick Terrell const BYTE* repMatch = (repIndex < prefixStartIndex) ? 266*e0c1b49fSNick Terrell dictBase + (repIndex - dictIndexDelta) : 267*e0c1b49fSNick Terrell base + repIndex; 268*e0c1b49fSNick Terrell hashTable[h] = curr; /* update hash table */ 269*e0c1b49fSNick Terrell 270*e0c1b49fSNick Terrell if ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */ 271*e0c1b49fSNick Terrell && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { 272*e0c1b49fSNick Terrell const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; 273*e0c1b49fSNick Terrell mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4; 274*e0c1b49fSNick Terrell ip++; 275*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH); 276*e0c1b49fSNick Terrell } else if ( (matchIndex <= prefixStartIndex) ) { 277*e0c1b49fSNick Terrell size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls); 278*e0c1b49fSNick Terrell U32 const dictMatchIndex = dictHashTable[dictHash]; 279*e0c1b49fSNick Terrell const BYTE* dictMatch = dictBase + dictMatchIndex; 280*e0c1b49fSNick Terrell if (dictMatchIndex <= dictStartIndex || 281*e0c1b49fSNick Terrell MEM_read32(dictMatch) != MEM_read32(ip)) { 282*e0c1b49fSNick Terrell assert(stepSize >= 1); 283*e0c1b49fSNick Terrell ip += ((ip-anchor) >> kSearchStrength) + stepSize; 284*e0c1b49fSNick Terrell continue; 285*e0c1b49fSNick Terrell } else { 286*e0c1b49fSNick Terrell /* found a dict match */ 287*e0c1b49fSNick Terrell U32 const offset = (U32)(curr-dictMatchIndex-dictIndexDelta); 288*e0c1b49fSNick Terrell mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4; 289*e0c1b49fSNick Terrell while (((ip>anchor) & (dictMatch>dictStart)) 290*e0c1b49fSNick Terrell && (ip[-1] == dictMatch[-1])) { 291*e0c1b49fSNick Terrell ip--; dictMatch--; mLength++; 292*e0c1b49fSNick Terrell } /* catch up */ 293*e0c1b49fSNick Terrell offset_2 = offset_1; 294*e0c1b49fSNick Terrell offset_1 = offset; 295*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH); 296*e0c1b49fSNick Terrell } 297*e0c1b49fSNick Terrell } else if (MEM_read32(match) != MEM_read32(ip)) { 298*e0c1b49fSNick Terrell /* it's not a match, and we're not going to check the dictionary */ 299*e0c1b49fSNick Terrell assert(stepSize >= 1); 300*e0c1b49fSNick Terrell ip += ((ip-anchor) >> kSearchStrength) + stepSize; 301*e0c1b49fSNick Terrell continue; 302*e0c1b49fSNick Terrell } else { 303*e0c1b49fSNick Terrell /* found a regular match */ 304*e0c1b49fSNick Terrell U32 const offset = (U32)(ip-match); 305*e0c1b49fSNick Terrell mLength = ZSTD_count(ip+4, match+4, iend) + 4; 306*e0c1b49fSNick Terrell while (((ip>anchor) & (match>prefixStart)) 307*e0c1b49fSNick Terrell && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ 308*e0c1b49fSNick Terrell offset_2 = offset_1; 309*e0c1b49fSNick Terrell offset_1 = offset; 310*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH); 311*e0c1b49fSNick Terrell } 312*e0c1b49fSNick Terrell 313*e0c1b49fSNick Terrell /* match found */ 314*e0c1b49fSNick Terrell ip += mLength; 315*e0c1b49fSNick Terrell anchor = ip; 316*e0c1b49fSNick Terrell 317*e0c1b49fSNick Terrell if (ip <= ilimit) { 318*e0c1b49fSNick Terrell /* Fill Table */ 319*e0c1b49fSNick Terrell assert(base+curr+2 > istart); /* check base overflow */ 320*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2; /* here because curr+2 could be > iend-8 */ 321*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base); 322*e0c1b49fSNick Terrell 323*e0c1b49fSNick Terrell /* check immediate repcode */ 324*e0c1b49fSNick Terrell while (ip <= ilimit) { 325*e0c1b49fSNick Terrell U32 const current2 = (U32)(ip-base); 326*e0c1b49fSNick Terrell U32 const repIndex2 = current2 - offset_2; 327*e0c1b49fSNick Terrell const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? 328*e0c1b49fSNick Terrell dictBase - dictIndexDelta + repIndex2 : 329*e0c1b49fSNick Terrell base + repIndex2; 330*e0c1b49fSNick Terrell if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */) 331*e0c1b49fSNick Terrell && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { 332*e0c1b49fSNick Terrell const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; 333*e0c1b49fSNick Terrell size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; 334*e0c1b49fSNick Terrell U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ 335*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH); 336*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2; 337*e0c1b49fSNick Terrell ip += repLength2; 338*e0c1b49fSNick Terrell anchor = ip; 339*e0c1b49fSNick Terrell continue; 340*e0c1b49fSNick Terrell } 341*e0c1b49fSNick Terrell break; 342*e0c1b49fSNick Terrell } 343*e0c1b49fSNick Terrell } 344*e0c1b49fSNick Terrell } 345*e0c1b49fSNick Terrell 346*e0c1b49fSNick Terrell /* save reps for next block */ 347*e0c1b49fSNick Terrell rep[0] = offset_1 ? offset_1 : offsetSaved; 348*e0c1b49fSNick Terrell rep[1] = offset_2 ? offset_2 : offsetSaved; 349*e0c1b49fSNick Terrell 350*e0c1b49fSNick Terrell /* Return the last literals size */ 351*e0c1b49fSNick Terrell return (size_t)(iend - anchor); 352*e0c1b49fSNick Terrell } 353*e0c1b49fSNick Terrell 354*e0c1b49fSNick Terrell size_t ZSTD_compressBlock_fast_dictMatchState( 355*e0c1b49fSNick Terrell ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 356*e0c1b49fSNick Terrell void const* src, size_t srcSize) 357*e0c1b49fSNick Terrell { 358*e0c1b49fSNick Terrell U32 const mls = ms->cParams.minMatch; 359*e0c1b49fSNick Terrell assert(ms->dictMatchState != NULL); 360*e0c1b49fSNick Terrell switch(mls) 361*e0c1b49fSNick Terrell { 362*e0c1b49fSNick Terrell default: /* includes case 3 */ 363*e0c1b49fSNick Terrell case 4 : 364*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4); 365*e0c1b49fSNick Terrell case 5 : 366*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5); 367*e0c1b49fSNick Terrell case 6 : 368*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6); 369*e0c1b49fSNick Terrell case 7 : 370*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7); 371*e0c1b49fSNick Terrell } 372*e0c1b49fSNick Terrell } 373*e0c1b49fSNick Terrell 374*e0c1b49fSNick Terrell 375*e0c1b49fSNick Terrell static size_t ZSTD_compressBlock_fast_extDict_generic( 376*e0c1b49fSNick Terrell ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 377*e0c1b49fSNick Terrell void const* src, size_t srcSize, U32 const mls) 378*e0c1b49fSNick Terrell { 379*e0c1b49fSNick Terrell const ZSTD_compressionParameters* const cParams = &ms->cParams; 380*e0c1b49fSNick Terrell U32* const hashTable = ms->hashTable; 381*e0c1b49fSNick Terrell U32 const hlog = cParams->hashLog; 382*e0c1b49fSNick Terrell /* support stepSize of 0 */ 383*e0c1b49fSNick Terrell U32 const stepSize = cParams->targetLength + !(cParams->targetLength); 384*e0c1b49fSNick Terrell const BYTE* const base = ms->window.base; 385*e0c1b49fSNick Terrell const BYTE* const dictBase = ms->window.dictBase; 386*e0c1b49fSNick Terrell const BYTE* const istart = (const BYTE*)src; 387*e0c1b49fSNick Terrell const BYTE* ip = istart; 388*e0c1b49fSNick Terrell const BYTE* anchor = istart; 389*e0c1b49fSNick Terrell const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); 390*e0c1b49fSNick Terrell const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog); 391*e0c1b49fSNick Terrell const U32 dictStartIndex = lowLimit; 392*e0c1b49fSNick Terrell const BYTE* const dictStart = dictBase + dictStartIndex; 393*e0c1b49fSNick Terrell const U32 dictLimit = ms->window.dictLimit; 394*e0c1b49fSNick Terrell const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit; 395*e0c1b49fSNick Terrell const BYTE* const prefixStart = base + prefixStartIndex; 396*e0c1b49fSNick Terrell const BYTE* const dictEnd = dictBase + prefixStartIndex; 397*e0c1b49fSNick Terrell const BYTE* const iend = istart + srcSize; 398*e0c1b49fSNick Terrell const BYTE* const ilimit = iend - 8; 399*e0c1b49fSNick Terrell U32 offset_1=rep[0], offset_2=rep[1]; 400*e0c1b49fSNick Terrell 401*e0c1b49fSNick Terrell DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic (offset_1=%u)", offset_1); 402*e0c1b49fSNick Terrell 403*e0c1b49fSNick Terrell /* switch to "regular" variant if extDict is invalidated due to maxDistance */ 404*e0c1b49fSNick Terrell if (prefixStartIndex == dictStartIndex) 405*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, mls); 406*e0c1b49fSNick Terrell 407*e0c1b49fSNick Terrell /* Search Loop */ 408*e0c1b49fSNick Terrell while (ip < ilimit) { /* < instead of <=, because (ip+1) */ 409*e0c1b49fSNick Terrell const size_t h = ZSTD_hashPtr(ip, hlog, mls); 410*e0c1b49fSNick Terrell const U32 matchIndex = hashTable[h]; 411*e0c1b49fSNick Terrell const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base; 412*e0c1b49fSNick Terrell const BYTE* match = matchBase + matchIndex; 413*e0c1b49fSNick Terrell const U32 curr = (U32)(ip-base); 414*e0c1b49fSNick Terrell const U32 repIndex = curr + 1 - offset_1; 415*e0c1b49fSNick Terrell const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base; 416*e0c1b49fSNick Terrell const BYTE* const repMatch = repBase + repIndex; 417*e0c1b49fSNick Terrell hashTable[h] = curr; /* update hash table */ 418*e0c1b49fSNick Terrell DEBUGLOG(7, "offset_1 = %u , curr = %u", offset_1, curr); 419*e0c1b49fSNick Terrell assert(offset_1 <= curr +1); /* check repIndex */ 420*e0c1b49fSNick Terrell 421*e0c1b49fSNick Terrell if ( (((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > dictStartIndex)) 422*e0c1b49fSNick Terrell && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { 423*e0c1b49fSNick Terrell const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; 424*e0c1b49fSNick Terrell size_t const rLength = ZSTD_count_2segments(ip+1 +4, repMatch +4, iend, repMatchEnd, prefixStart) + 4; 425*e0c1b49fSNick Terrell ip++; 426*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, rLength-MINMATCH); 427*e0c1b49fSNick Terrell ip += rLength; 428*e0c1b49fSNick Terrell anchor = ip; 429*e0c1b49fSNick Terrell } else { 430*e0c1b49fSNick Terrell if ( (matchIndex < dictStartIndex) || 431*e0c1b49fSNick Terrell (MEM_read32(match) != MEM_read32(ip)) ) { 432*e0c1b49fSNick Terrell assert(stepSize >= 1); 433*e0c1b49fSNick Terrell ip += ((ip-anchor) >> kSearchStrength) + stepSize; 434*e0c1b49fSNick Terrell continue; 435*e0c1b49fSNick Terrell } 436*e0c1b49fSNick Terrell { const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend; 437*e0c1b49fSNick Terrell const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart; 438*e0c1b49fSNick Terrell U32 const offset = curr - matchIndex; 439*e0c1b49fSNick Terrell size_t mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4; 440*e0c1b49fSNick Terrell while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ 441*e0c1b49fSNick Terrell offset_2 = offset_1; offset_1 = offset; /* update offset history */ 442*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH); 443*e0c1b49fSNick Terrell ip += mLength; 444*e0c1b49fSNick Terrell anchor = ip; 445*e0c1b49fSNick Terrell } } 446*e0c1b49fSNick Terrell 447*e0c1b49fSNick Terrell if (ip <= ilimit) { 448*e0c1b49fSNick Terrell /* Fill Table */ 449*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(base+curr+2, hlog, mls)] = curr+2; 450*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base); 451*e0c1b49fSNick Terrell /* check immediate repcode */ 452*e0c1b49fSNick Terrell while (ip <= ilimit) { 453*e0c1b49fSNick Terrell U32 const current2 = (U32)(ip-base); 454*e0c1b49fSNick Terrell U32 const repIndex2 = current2 - offset_2; 455*e0c1b49fSNick Terrell const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2; 456*e0c1b49fSNick Terrell if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (repIndex2 > dictStartIndex)) /* intentional overflow */ 457*e0c1b49fSNick Terrell && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { 458*e0c1b49fSNick Terrell const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; 459*e0c1b49fSNick Terrell size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; 460*e0c1b49fSNick Terrell { U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */ 461*e0c1b49fSNick Terrell ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, 0 /*offcode*/, repLength2-MINMATCH); 462*e0c1b49fSNick Terrell hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2; 463*e0c1b49fSNick Terrell ip += repLength2; 464*e0c1b49fSNick Terrell anchor = ip; 465*e0c1b49fSNick Terrell continue; 466*e0c1b49fSNick Terrell } 467*e0c1b49fSNick Terrell break; 468*e0c1b49fSNick Terrell } } } 469*e0c1b49fSNick Terrell 470*e0c1b49fSNick Terrell /* save reps for next block */ 471*e0c1b49fSNick Terrell rep[0] = offset_1; 472*e0c1b49fSNick Terrell rep[1] = offset_2; 473*e0c1b49fSNick Terrell 474*e0c1b49fSNick Terrell /* Return the last literals size */ 475*e0c1b49fSNick Terrell return (size_t)(iend - anchor); 476*e0c1b49fSNick Terrell } 477*e0c1b49fSNick Terrell 478*e0c1b49fSNick Terrell 479*e0c1b49fSNick Terrell size_t ZSTD_compressBlock_fast_extDict( 480*e0c1b49fSNick Terrell ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 481*e0c1b49fSNick Terrell void const* src, size_t srcSize) 482*e0c1b49fSNick Terrell { 483*e0c1b49fSNick Terrell U32 const mls = ms->cParams.minMatch; 484*e0c1b49fSNick Terrell switch(mls) 485*e0c1b49fSNick Terrell { 486*e0c1b49fSNick Terrell default: /* includes case 3 */ 487*e0c1b49fSNick Terrell case 4 : 488*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4); 489*e0c1b49fSNick Terrell case 5 : 490*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5); 491*e0c1b49fSNick Terrell case 6 : 492*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6); 493*e0c1b49fSNick Terrell case 7 : 494*e0c1b49fSNick Terrell return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7); 495*e0c1b49fSNick Terrell } 496*e0c1b49fSNick Terrell } 497