2017-09-02 01:28:35 +00:00
|
|
|
/*
|
2020-03-26 22:19:05 +00:00
|
|
|
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
|
2017-09-02 01:28:35 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2017-09-02 01:28:35 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-08 00:15:23 +00:00
|
|
|
#include "zstd_compress_internal.h"
|
2017-09-02 01:28:35 +00:00
|
|
|
#include "zstd_double_fast.h"
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
|
2018-04-02 21:41:30 +00:00
|
|
|
void const* end, ZSTD_dictTableLoadMethod_e dtlm)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2018-08-23 19:08:03 +00:00
|
|
|
const ZSTD_compressionParameters* const cParams = &ms->cParams;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32* const hashLarge = ms->hashTable;
|
|
|
|
U32 const hBitsL = cParams->hashLog;
|
2018-11-20 22:56:07 +00:00
|
|
|
U32 const mls = cParams->minMatch;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32* const hashSmall = ms->chainTable;
|
|
|
|
U32 const hBitsS = cParams->chainLog;
|
2018-02-24 00:48:18 +00:00
|
|
|
const BYTE* const base = ms->window.base;
|
2017-12-13 00:51:00 +00:00
|
|
|
const BYTE* ip = base + ms->nextToUpdate;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
|
2018-01-12 20:26:47 +00:00
|
|
|
const U32 fastHashFillStep = 3;
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-01-12 20:26:47 +00:00
|
|
|
/* Always insert every fastHashFillStep position into the hash tables.
|
|
|
|
* Insert the other positions into the large hash table if their entry
|
|
|
|
* is empty.
|
|
|
|
*/
|
|
|
|
for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
|
2020-08-11 21:31:09 +00:00
|
|
|
U32 const curr = (U32)(ip - base);
|
2018-01-12 20:26:47 +00:00
|
|
|
U32 i;
|
|
|
|
for (i = 0; i < fastHashFillStep; ++i) {
|
|
|
|
size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
|
|
|
|
size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
|
|
|
|
if (i == 0)
|
2020-08-11 21:31:09 +00:00
|
|
|
hashSmall[smHash] = curr + i;
|
2018-01-12 20:26:47 +00:00
|
|
|
if (i == 0 || hashLarge[lgHash] == 0)
|
2020-08-11 21:31:09 +00:00
|
|
|
hashLarge[lgHash] = curr + i;
|
2018-04-02 21:41:30 +00:00
|
|
|
/* Only load extra positions for ZSTD_dtlm_full */
|
|
|
|
if (dtlm == ZSTD_dtlm_fast)
|
|
|
|
break;
|
2019-05-31 23:50:04 +00:00
|
|
|
} }
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_TEMPLATE
|
2017-12-13 00:51:00 +00:00
|
|
|
size_t ZSTD_compressBlock_doubleFast_generic(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
2018-08-23 18:53:34 +00:00
|
|
|
void const* src, size_t srcSize,
|
2018-05-04 19:59:27 +00:00
|
|
|
U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2018-08-23 18:53:34 +00:00
|
|
|
ZSTD_compressionParameters const* cParams = &ms->cParams;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32* const hashLong = ms->hashTable;
|
|
|
|
const U32 hBitsL = cParams->hashLog;
|
|
|
|
U32* const hashSmall = ms->chainTable;
|
|
|
|
const U32 hBitsS = cParams->chainLog;
|
2018-02-24 00:48:18 +00:00
|
|
|
const BYTE* const base = ms->window.base;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const istart = (const BYTE*)src;
|
|
|
|
const BYTE* ip = istart;
|
|
|
|
const BYTE* anchor = istart;
|
2019-05-31 23:50:04 +00:00
|
|
|
const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
|
2019-08-05 13:18:43 +00:00
|
|
|
/* presumes that, if there is a dictionary, it must be using Attach mode */
|
2020-05-12 18:57:15 +00:00
|
|
|
const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
|
2018-05-15 22:40:07 +00:00
|
|
|
const BYTE* const prefixLowest = base + prefixLowestIndex;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const iend = istart + srcSize;
|
|
|
|
const BYTE* const ilimit = iend - HASH_READ_SIZE;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32 offset_1=rep[0], offset_2=rep[1];
|
2017-09-02 01:28:35 +00:00
|
|
|
U32 offsetSaved = 0;
|
|
|
|
|
2018-05-09 19:28:47 +00:00
|
|
|
const ZSTD_matchState_t* const dms = ms->dictMatchState;
|
2018-08-27 22:23:51 +00:00
|
|
|
const ZSTD_compressionParameters* const dictCParams =
|
|
|
|
dictMode == ZSTD_dictMatchState ?
|
|
|
|
&dms->cParams : NULL;
|
2018-05-09 19:28:47 +00:00
|
|
|
const U32* const dictHashLong = dictMode == ZSTD_dictMatchState ?
|
|
|
|
dms->hashTable : NULL;
|
|
|
|
const U32* const dictHashSmall = dictMode == ZSTD_dictMatchState ?
|
2018-05-23 21:37:47 +00:00
|
|
|
dms->chainTable : NULL;
|
2018-06-05 00:10:50 +00:00
|
|
|
const U32 dictStartIndex = dictMode == ZSTD_dictMatchState ?
|
2018-05-09 19:28:47 +00:00
|
|
|
dms->window.dictLimit : 0;
|
|
|
|
const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ?
|
|
|
|
dms->window.base : NULL;
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const dictStart = dictMode == ZSTD_dictMatchState ?
|
|
|
|
dictBase + dictStartIndex : NULL;
|
2018-05-09 19:28:47 +00:00
|
|
|
const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ?
|
|
|
|
dms->window.nextSrc : NULL;
|
|
|
|
const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ?
|
2018-05-15 22:40:07 +00:00
|
|
|
prefixLowestIndex - (U32)(dictEnd - dictBase) :
|
2018-05-09 19:28:47 +00:00
|
|
|
0;
|
2018-08-27 22:23:51 +00:00
|
|
|
const U32 dictHBitsL = dictMode == ZSTD_dictMatchState ?
|
2018-09-13 18:13:08 +00:00
|
|
|
dictCParams->hashLog : hBitsL;
|
2018-08-27 22:23:51 +00:00
|
|
|
const U32 dictHBitsS = dictMode == ZSTD_dictMatchState ?
|
2018-09-13 18:13:08 +00:00
|
|
|
dictCParams->chainLog : hBitsS;
|
2019-11-21 02:21:51 +00:00
|
|
|
const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictStart));
|
2018-05-09 19:28:47 +00:00
|
|
|
|
2019-06-01 00:06:28 +00:00
|
|
|
DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_generic");
|
|
|
|
|
2018-05-09 19:23:22 +00:00
|
|
|
assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
|
2018-05-04 19:59:27 +00:00
|
|
|
|
2019-05-31 23:50:04 +00:00
|
|
|
/* if a dictionary is attached, it must be within window range */
|
2019-06-01 00:06:28 +00:00
|
|
|
if (dictMode == ZSTD_dictMatchState) {
|
2020-05-12 18:57:15 +00:00
|
|
|
assert(ms->window.dictLimit + (1U << cParams->windowLog) >= endIndex);
|
2019-06-01 00:06:28 +00:00
|
|
|
}
|
2019-05-31 23:50:04 +00:00
|
|
|
|
2017-09-02 01:28:35 +00:00
|
|
|
/* init */
|
2018-05-23 16:51:09 +00:00
|
|
|
ip += (dictAndPrefixLength == 0);
|
|
|
|
if (dictMode == ZSTD_noDict) {
|
2020-08-11 21:31:09 +00:00
|
|
|
U32 const curr = (U32)(ip - base);
|
|
|
|
U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog);
|
|
|
|
U32 const maxRep = curr - windowLow;
|
2017-09-02 01:28:35 +00:00
|
|
|
if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
|
|
|
|
if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
|
|
|
|
}
|
2018-05-23 16:51:09 +00:00
|
|
|
if (dictMode == ZSTD_dictMatchState) {
|
|
|
|
/* dictMatchState repCode checks don't currently handle repCode == 0
|
|
|
|
* disabling. */
|
|
|
|
assert(offset_1 <= dictAndPrefixLength);
|
|
|
|
assert(offset_2 <= dictAndPrefixLength);
|
|
|
|
}
|
2017-09-02 01:28:35 +00:00
|
|
|
|
|
|
|
/* Main Search Loop */
|
|
|
|
while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
|
|
|
|
size_t mLength;
|
2018-05-16 18:55:20 +00:00
|
|
|
U32 offset;
|
2017-09-02 01:28:35 +00:00
|
|
|
size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
|
|
|
|
size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
|
2018-08-27 22:23:51 +00:00
|
|
|
size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
|
|
|
|
size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
|
2020-08-11 21:31:09 +00:00
|
|
|
U32 const curr = (U32)(ip-base);
|
2017-09-02 01:28:35 +00:00
|
|
|
U32 const matchIndexL = hashLong[h2];
|
2018-05-23 20:43:33 +00:00
|
|
|
U32 matchIndexS = hashSmall[h];
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* matchLong = base + matchIndexL;
|
|
|
|
const BYTE* match = base + matchIndexS;
|
2020-08-11 21:31:09 +00:00
|
|
|
const U32 repIndex = curr + 1 - offset_1;
|
2018-05-15 22:40:07 +00:00
|
|
|
const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
|
|
|
|
&& repIndex < prefixLowestIndex) ?
|
|
|
|
dictBase + (repIndex - dictIndexDelta) :
|
|
|
|
base + repIndex;
|
2020-08-11 21:31:09 +00:00
|
|
|
hashLong[h2] = hashSmall[h] = curr; /* update hash tables */
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-05-16 18:55:20 +00:00
|
|
|
/* check dictMatchState repcode */
|
2018-05-09 22:37:24 +00:00
|
|
|
if (dictMode == ZSTD_dictMatchState
|
2018-05-15 22:40:07 +00:00
|
|
|
&& ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
|
2018-05-09 22:37:24 +00:00
|
|
|
&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
|
2018-05-15 22:40:07 +00:00
|
|
|
const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
|
2018-06-01 22:18:32 +00:00
|
|
|
mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
|
2018-05-09 22:37:24 +00:00
|
|
|
ip++;
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
|
2018-05-16 18:55:20 +00:00
|
|
|
goto _match_stored;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check noDict repcode */
|
|
|
|
if ( dictMode == ZSTD_noDict
|
|
|
|
&& ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
|
2017-09-02 01:28:35 +00:00
|
|
|
mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
|
|
|
|
ip++;
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
|
2018-05-16 18:55:20 +00:00
|
|
|
goto _match_stored;
|
|
|
|
}
|
|
|
|
|
2018-09-14 22:24:25 +00:00
|
|
|
if (matchIndexL > prefixLowestIndex) {
|
|
|
|
/* check prefix long match */
|
|
|
|
if (MEM_read64(matchLong) == MEM_read64(ip)) {
|
|
|
|
mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
|
|
|
|
offset = (U32)(ip-matchLong);
|
|
|
|
while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
|
|
|
|
goto _match_found;
|
|
|
|
}
|
|
|
|
} else if (dictMode == ZSTD_dictMatchState) {
|
|
|
|
/* check dictMatchState long match */
|
2018-08-27 22:23:51 +00:00
|
|
|
U32 const dictMatchIndexL = dictHashLong[dictHL];
|
2018-05-16 19:20:16 +00:00
|
|
|
const BYTE* dictMatchL = dictBase + dictMatchIndexL;
|
|
|
|
assert(dictMatchL < dictEnd);
|
|
|
|
|
2018-06-05 00:10:50 +00:00
|
|
|
if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
|
2018-05-16 19:20:16 +00:00
|
|
|
mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
|
2020-08-11 21:31:09 +00:00
|
|
|
offset = (U32)(curr - dictMatchIndexL - dictIndexDelta);
|
2018-06-05 00:10:50 +00:00
|
|
|
while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
|
2018-05-16 19:20:16 +00:00
|
|
|
goto _match_found;
|
2019-06-01 00:27:07 +00:00
|
|
|
} }
|
2018-05-16 19:20:16 +00:00
|
|
|
|
2018-09-14 22:24:25 +00:00
|
|
|
if (matchIndexS > prefixLowestIndex) {
|
|
|
|
/* check prefix short match */
|
|
|
|
if (MEM_read32(match) == MEM_read32(ip)) {
|
|
|
|
goto _search_next_long;
|
|
|
|
}
|
|
|
|
} else if (dictMode == ZSTD_dictMatchState) {
|
|
|
|
/* check dictMatchState short match */
|
2018-08-27 22:23:51 +00:00
|
|
|
U32 const dictMatchIndexS = dictHashSmall[dictHS];
|
2018-05-23 20:43:33 +00:00
|
|
|
match = dictBase + dictMatchIndexS;
|
|
|
|
matchIndexS = dictMatchIndexS + dictIndexDelta;
|
|
|
|
|
2018-06-05 00:10:50 +00:00
|
|
|
if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
|
2018-05-23 20:43:33 +00:00
|
|
|
goto _search_next_long;
|
2019-06-01 00:27:07 +00:00
|
|
|
} }
|
2018-05-23 20:43:33 +00:00
|
|
|
|
|
|
|
ip += ((ip-anchor) >> kSearchStrength) + 1;
|
2020-03-14 07:25:09 +00:00
|
|
|
#if defined(__aarch64__)
|
|
|
|
PREFETCH_L1(ip+256);
|
|
|
|
#endif
|
2018-05-23 20:43:33 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
_search_next_long:
|
2018-06-01 22:18:32 +00:00
|
|
|
|
2019-06-01 00:27:07 +00:00
|
|
|
{ size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
|
2018-08-27 22:23:51 +00:00
|
|
|
size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
|
2018-05-16 18:55:20 +00:00
|
|
|
U32 const matchIndexL3 = hashLong[hl3];
|
|
|
|
const BYTE* matchL3 = base + matchIndexL3;
|
2020-08-11 21:31:09 +00:00
|
|
|
hashLong[hl3] = curr + 1;
|
2018-05-16 18:55:20 +00:00
|
|
|
|
2018-05-23 20:43:33 +00:00
|
|
|
/* check prefix long +1 match */
|
2018-09-14 22:24:25 +00:00
|
|
|
if (matchIndexL3 > prefixLowestIndex) {
|
|
|
|
if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
|
|
|
|
mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
|
|
|
|
ip++;
|
|
|
|
offset = (U32)(ip-matchL3);
|
|
|
|
while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
|
|
|
|
goto _match_found;
|
|
|
|
}
|
|
|
|
} else if (dictMode == ZSTD_dictMatchState) {
|
|
|
|
/* check dict long +1 match */
|
2018-08-27 22:23:51 +00:00
|
|
|
U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
|
2018-05-16 18:55:20 +00:00
|
|
|
const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
|
|
|
|
assert(dictMatchL3 < dictEnd);
|
2018-06-05 00:10:50 +00:00
|
|
|
if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
|
2018-05-16 18:55:20 +00:00
|
|
|
mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
|
2017-09-02 01:28:35 +00:00
|
|
|
ip++;
|
2020-08-11 21:31:09 +00:00
|
|
|
offset = (U32)(curr + 1 - dictMatchIndexL3 - dictIndexDelta);
|
2018-06-05 00:10:50 +00:00
|
|
|
while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
|
2018-05-23 20:43:33 +00:00
|
|
|
goto _match_found;
|
2019-06-01 00:27:07 +00:00
|
|
|
} } }
|
2018-06-01 22:18:32 +00:00
|
|
|
|
2018-05-23 20:43:33 +00:00
|
|
|
/* if no long +1 match, explore the short match we found */
|
|
|
|
if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) {
|
2018-06-01 22:18:32 +00:00
|
|
|
mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
|
2020-08-11 21:31:09 +00:00
|
|
|
offset = (U32)(curr - matchIndexS);
|
2018-06-05 00:10:50 +00:00
|
|
|
while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
|
2018-05-23 20:43:33 +00:00
|
|
|
} else {
|
|
|
|
mLength = ZSTD_count(ip+4, match+4, iend) + 4;
|
|
|
|
offset = (U32)(ip - match);
|
|
|
|
while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
|
|
|
|
}
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-05-23 20:43:33 +00:00
|
|
|
/* fall-through */
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-05-16 18:55:20 +00:00
|
|
|
_match_found:
|
|
|
|
offset_2 = offset_1;
|
|
|
|
offset_1 = offset;
|
|
|
|
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-05-16 18:55:20 +00:00
|
|
|
_match_stored:
|
2017-09-02 01:28:35 +00:00
|
|
|
/* match found */
|
|
|
|
ip += mLength;
|
|
|
|
anchor = ip;
|
|
|
|
|
|
|
|
if (ip <= ilimit) {
|
2019-07-11 22:25:22 +00:00
|
|
|
/* Complementary insertion */
|
|
|
|
/* done after iLimit test, as candidates could be > iend-8 */
|
2020-08-11 21:31:09 +00:00
|
|
|
{ U32 const indexToInsert = curr+2;
|
2019-07-12 18:34:53 +00:00
|
|
|
hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
|
|
|
|
hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
|
|
|
|
hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
|
|
|
|
hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
|
2019-07-11 22:25:22 +00:00
|
|
|
}
|
2017-09-02 01:28:35 +00:00
|
|
|
|
|
|
|
/* check immediate repcode */
|
2018-05-09 22:46:33 +00:00
|
|
|
if (dictMode == ZSTD_dictMatchState) {
|
|
|
|
while (ip <= ilimit) {
|
|
|
|
U32 const current2 = (U32)(ip-base);
|
2018-05-15 22:40:07 +00:00
|
|
|
U32 const repIndex2 = current2 - offset_2;
|
2018-05-09 22:46:33 +00:00
|
|
|
const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
|
2018-05-15 22:40:07 +00:00
|
|
|
&& repIndex2 < prefixLowestIndex ?
|
2019-11-21 02:21:51 +00:00
|
|
|
dictBase + repIndex2 - dictIndexDelta :
|
2018-05-09 22:46:33 +00:00
|
|
|
base + repIndex2;
|
2018-05-15 22:40:07 +00:00
|
|
|
if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
|
2018-05-09 22:46:33 +00:00
|
|
|
&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
|
2018-05-15 22:40:07 +00:00
|
|
|
const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
|
2018-06-01 22:18:32 +00:00
|
|
|
size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
|
2018-05-09 22:46:33 +00:00
|
|
|
U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
|
2018-05-09 22:46:33 +00:00
|
|
|
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
|
|
|
|
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
|
|
|
|
ip += repLength2;
|
|
|
|
anchor = ip;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2019-06-01 00:27:07 +00:00
|
|
|
} }
|
2018-05-09 22:46:33 +00:00
|
|
|
|
|
|
|
if (dictMode == ZSTD_noDict) {
|
|
|
|
while ( (ip <= ilimit)
|
|
|
|
&& ( (offset_2>0)
|
|
|
|
& (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
|
|
|
|
/* store sequence */
|
|
|
|
size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
|
|
|
|
U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
|
|
|
|
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
|
|
|
|
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, rLength-MINMATCH);
|
2018-05-09 22:46:33 +00:00
|
|
|
ip += rLength;
|
|
|
|
anchor = ip;
|
|
|
|
continue; /* faster when present ... (?) */
|
2019-06-01 00:27:07 +00:00
|
|
|
} } }
|
|
|
|
} /* while (ip < ilimit) */
|
2017-09-02 01:28:35 +00:00
|
|
|
|
|
|
|
/* save reps for next block */
|
2017-12-13 00:51:00 +00:00
|
|
|
rep[0] = offset_1 ? offset_1 : offsetSaved;
|
|
|
|
rep[1] = offset_2 ? offset_2 : offsetSaved;
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2017-09-06 22:56:32 +00:00
|
|
|
/* Return the last literals size */
|
2019-05-31 23:50:04 +00:00
|
|
|
return (size_t)(iend - anchor);
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
size_t ZSTD_compressBlock_doubleFast(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
2018-08-23 18:53:34 +00:00
|
|
|
void const* src, size_t srcSize)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2018-11-20 22:56:07 +00:00
|
|
|
const U32 mls = ms->cParams.minMatch;
|
2017-09-02 01:28:35 +00:00
|
|
|
switch(mls)
|
|
|
|
{
|
|
|
|
default: /* includes case 3 */
|
|
|
|
case 4 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
|
2018-05-04 19:59:27 +00:00
|
|
|
case 5 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
|
2018-05-04 19:59:27 +00:00
|
|
|
case 6 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
|
2018-05-04 19:59:27 +00:00
|
|
|
case 7 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
|
2018-05-04 19:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ZSTD_compressBlock_doubleFast_dictMatchState(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
2018-08-23 18:53:34 +00:00
|
|
|
void const* src, size_t srcSize)
|
2018-05-04 19:59:27 +00:00
|
|
|
{
|
2018-11-20 22:56:07 +00:00
|
|
|
const U32 mls = ms->cParams.minMatch;
|
2018-05-04 19:59:27 +00:00
|
|
|
switch(mls)
|
|
|
|
{
|
|
|
|
default: /* includes case 3 */
|
|
|
|
case 4 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 5 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 6 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 7 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
2018-08-23 18:53:34 +00:00
|
|
|
void const* src, size_t srcSize,
|
2017-12-13 00:51:00 +00:00
|
|
|
U32 const mls /* template */)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2018-08-23 18:53:34 +00:00
|
|
|
ZSTD_compressionParameters const* cParams = &ms->cParams;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32* const hashLong = ms->hashTable;
|
|
|
|
U32 const hBitsL = cParams->hashLog;
|
|
|
|
U32* const hashSmall = ms->chainTable;
|
|
|
|
U32 const hBitsS = cParams->chainLog;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const istart = (const BYTE*)src;
|
|
|
|
const BYTE* ip = istart;
|
|
|
|
const BYTE* anchor = istart;
|
|
|
|
const BYTE* const iend = istart + srcSize;
|
|
|
|
const BYTE* const ilimit = iend - 8;
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const base = ms->window.base;
|
2019-05-31 23:50:04 +00:00
|
|
|
const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
|
2019-08-05 13:18:43 +00:00
|
|
|
const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
|
2019-05-31 23:50:04 +00:00
|
|
|
const U32 dictStartIndex = lowLimit;
|
|
|
|
const U32 dictLimit = ms->window.dictLimit;
|
|
|
|
const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const prefixStart = base + prefixStartIndex;
|
|
|
|
const BYTE* const dictBase = ms->window.dictBase;
|
|
|
|
const BYTE* const dictStart = dictBase + dictStartIndex;
|
|
|
|
const BYTE* const dictEnd = dictBase + prefixStartIndex;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32 offset_1=rep[0], offset_2=rep[1];
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-05-07 19:54:13 +00:00
|
|
|
DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
|
|
|
|
|
2019-05-31 23:50:04 +00:00
|
|
|
/* if extDict is invalidated due to maxDistance, switch to "regular" variant */
|
|
|
|
if (prefixStartIndex == dictStartIndex)
|
|
|
|
return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_noDict);
|
|
|
|
|
2017-09-02 01:28:35 +00:00
|
|
|
/* Search Loop */
|
|
|
|
while (ip < ilimit) { /* < instead of <=, because (ip+1) */
|
|
|
|
const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
|
|
|
|
const U32 matchIndex = hashSmall[hSmall];
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* match = matchBase + matchIndex;
|
|
|
|
|
|
|
|
const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
|
|
|
|
const U32 matchLongIndex = hashLong[hLong];
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* matchLong = matchLongBase + matchLongIndex;
|
|
|
|
|
2020-08-11 21:31:09 +00:00
|
|
|
const U32 curr = (U32)(ip-base);
|
|
|
|
const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
|
|
|
|
const BYTE* const repMatch = repBase + repIndex;
|
2017-09-02 01:28:35 +00:00
|
|
|
size_t mLength;
|
2020-08-11 21:31:09 +00:00
|
|
|
hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-06-05 00:10:50 +00:00
|
|
|
if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
|
|
|
|
& (repIndex > dictStartIndex))
|
|
|
|
&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
|
|
|
|
const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
|
|
|
|
mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
|
2017-09-02 01:28:35 +00:00
|
|
|
ip++;
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
|
2017-09-02 01:28:35 +00:00
|
|
|
} else {
|
2018-06-05 00:10:50 +00:00
|
|
|
if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
|
|
|
|
const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
|
|
|
|
const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
|
2017-09-02 01:28:35 +00:00
|
|
|
U32 offset;
|
2018-06-05 00:10:50 +00:00
|
|
|
mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
|
2020-08-11 21:31:09 +00:00
|
|
|
offset = curr - matchLongIndex;
|
2017-09-02 01:28:35 +00:00
|
|
|
while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
|
|
|
|
offset_2 = offset_1;
|
|
|
|
offset_1 = offset;
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-06-05 00:10:50 +00:00
|
|
|
} else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
|
2017-09-02 01:28:35 +00:00
|
|
|
size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
|
|
|
|
U32 const matchIndex3 = hashLong[h3];
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* match3 = match3Base + matchIndex3;
|
|
|
|
U32 offset;
|
2020-08-11 21:31:09 +00:00
|
|
|
hashLong[h3] = curr + 1;
|
2018-06-05 00:10:50 +00:00
|
|
|
if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
|
|
|
|
const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
|
|
|
|
const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
|
|
|
|
mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
|
2017-09-02 01:28:35 +00:00
|
|
|
ip++;
|
2020-08-11 21:31:09 +00:00
|
|
|
offset = curr+1 - matchIndex3;
|
2017-09-02 01:28:35 +00:00
|
|
|
while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
|
|
|
|
} else {
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
|
|
|
|
const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
|
|
|
|
mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
|
2020-08-11 21:31:09 +00:00
|
|
|
offset = curr - matchIndex;
|
2017-09-02 01:28:35 +00:00
|
|
|
while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
|
|
|
|
}
|
|
|
|
offset_2 = offset_1;
|
|
|
|
offset_1 = offset;
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
|
2017-09-02 01:28:35 +00:00
|
|
|
|
|
|
|
} else {
|
2018-02-03 00:31:20 +00:00
|
|
|
ip += ((ip-anchor) >> kSearchStrength) + 1;
|
2017-09-02 01:28:35 +00:00
|
|
|
continue;
|
|
|
|
} }
|
|
|
|
|
2019-07-11 22:25:22 +00:00
|
|
|
/* move to next sequence start */
|
2017-09-02 01:28:35 +00:00
|
|
|
ip += mLength;
|
|
|
|
anchor = ip;
|
|
|
|
|
|
|
|
if (ip <= ilimit) {
|
2019-07-11 22:25:22 +00:00
|
|
|
/* Complementary insertion */
|
|
|
|
/* done after iLimit test, as candidates could be > iend-8 */
|
2020-08-11 21:31:09 +00:00
|
|
|
{ U32 const indexToInsert = curr+2;
|
2019-07-12 21:17:17 +00:00
|
|
|
hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
|
|
|
|
hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
|
|
|
|
hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
|
|
|
|
hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
|
2019-07-11 22:25:22 +00:00
|
|
|
}
|
2019-07-12 21:17:17 +00:00
|
|
|
|
2017-09-02 01:28:35 +00:00
|
|
|
/* check immediate repcode */
|
|
|
|
while (ip <= ilimit) {
|
|
|
|
U32 const current2 = (U32)(ip-base);
|
|
|
|
U32 const repIndex2 = current2 - offset_2;
|
2018-06-05 00:10:50 +00:00
|
|
|
const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
|
|
|
|
if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
|
|
|
|
& (repIndex2 > dictStartIndex))
|
|
|
|
&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
|
|
|
|
const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
|
|
|
|
size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
|
|
|
|
U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
|
2019-09-20 07:52:55 +00:00
|
|
|
ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
|
2017-09-02 01:28:35 +00:00
|
|
|
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
|
|
|
|
hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
|
|
|
|
ip += repLength2;
|
|
|
|
anchor = ip;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} } }
|
|
|
|
|
|
|
|
/* save reps for next block */
|
2017-12-13 00:51:00 +00:00
|
|
|
rep[0] = offset_1;
|
|
|
|
rep[1] = offset_2;
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2017-09-06 22:56:32 +00:00
|
|
|
/* Return the last literals size */
|
2019-05-31 23:50:04 +00:00
|
|
|
return (size_t)(iend - anchor);
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
size_t ZSTD_compressBlock_doubleFast_extDict(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
2018-08-23 18:53:34 +00:00
|
|
|
void const* src, size_t srcSize)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2018-11-20 22:56:07 +00:00
|
|
|
U32 const mls = ms->cParams.minMatch;
|
2017-09-02 01:28:35 +00:00
|
|
|
switch(mls)
|
|
|
|
{
|
|
|
|
default: /* includes case 3 */
|
|
|
|
case 4 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 5 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 6 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 7 :
|
2018-08-23 18:53:34 +00:00
|
|
|
return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
}
|