10c16b537SWarner Losh /* 20c16b537SWarner Losh * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh */ 90c16b537SWarner Losh 100c16b537SWarner Losh #include "zstd_ldm.h" 110c16b537SWarner Losh 120f743729SConrad Meyer #include "debug.h" 130c16b537SWarner Losh #include "zstd_fast.h" /* ZSTD_fillHashTable() */ 140c16b537SWarner Losh #include "zstd_double_fast.h" /* ZSTD_fillDoubleHashTable() */ 150c16b537SWarner Losh 160c16b537SWarner Losh #define LDM_BUCKET_SIZE_LOG 3 170c16b537SWarner Losh #define LDM_MIN_MATCH_LENGTH 64 180c16b537SWarner Losh #define LDM_HASH_RLOG 7 190c16b537SWarner Losh #define LDM_HASH_CHAR_OFFSET 10 200c16b537SWarner Losh 2119fcbaf1SConrad Meyer void ZSTD_ldm_adjustParameters(ldmParams_t* params, 2219fcbaf1SConrad Meyer ZSTD_compressionParameters const* cParams) 230c16b537SWarner Losh { 240f743729SConrad Meyer params->windowLog = cParams->windowLog; 250c16b537SWarner Losh ZSTD_STATIC_ASSERT(LDM_BUCKET_SIZE_LOG <= ZSTD_LDM_BUCKETSIZELOG_MAX); 2619fcbaf1SConrad Meyer DEBUGLOG(4, "ZSTD_ldm_adjustParameters"); 2719fcbaf1SConrad Meyer if (!params->bucketSizeLog) params->bucketSizeLog = LDM_BUCKET_SIZE_LOG; 2819fcbaf1SConrad Meyer if (!params->minMatchLength) params->minMatchLength = LDM_MIN_MATCH_LENGTH; 2919fcbaf1SConrad Meyer if (cParams->strategy >= ZSTD_btopt) { 3019fcbaf1SConrad Meyer /* Get out of the way of the optimal parser */ 3119fcbaf1SConrad Meyer U32 const minMatch = MAX(cParams->targetLength, params->minMatchLength); 3219fcbaf1SConrad Meyer assert(minMatch >= ZSTD_LDM_MINMATCH_MIN); 3319fcbaf1SConrad Meyer assert(minMatch <= ZSTD_LDM_MINMATCH_MAX); 3419fcbaf1SConrad Meyer params->minMatchLength = minMatch; 350c16b537SWarner Losh } 360c16b537SWarner Losh if (params->hashLog == 0) { 370f743729SConrad Meyer params->hashLog = MAX(ZSTD_HASHLOG_MIN, params->windowLog - LDM_HASH_RLOG); 380c16b537SWarner Losh assert(params->hashLog <= ZSTD_HASHLOG_MAX); 390c16b537SWarner Losh } 40*a0483764SConrad Meyer if (params->hashRateLog == 0) { 41*a0483764SConrad Meyer params->hashRateLog = params->windowLog < params->hashLog 420f743729SConrad Meyer ? 0 430f743729SConrad Meyer : params->windowLog - params->hashLog; 440c16b537SWarner Losh } 450c16b537SWarner Losh params->bucketSizeLog = MIN(params->bucketSizeLog, params->hashLog); 460c16b537SWarner Losh } 470c16b537SWarner Losh 4819fcbaf1SConrad Meyer size_t ZSTD_ldm_getTableSize(ldmParams_t params) 4919fcbaf1SConrad Meyer { 5019fcbaf1SConrad Meyer size_t const ldmHSize = ((size_t)1) << params.hashLog; 5119fcbaf1SConrad Meyer size_t const ldmBucketSizeLog = MIN(params.bucketSizeLog, params.hashLog); 520c16b537SWarner Losh size_t const ldmBucketSize = 5319fcbaf1SConrad Meyer ((size_t)1) << (params.hashLog - ldmBucketSizeLog); 5419fcbaf1SConrad Meyer size_t const totalSize = ldmBucketSize + ldmHSize * sizeof(ldmEntry_t); 5519fcbaf1SConrad Meyer return params.enableLdm ? totalSize : 0; 5619fcbaf1SConrad Meyer } 5719fcbaf1SConrad Meyer 5819fcbaf1SConrad Meyer size_t ZSTD_ldm_getMaxNbSeq(ldmParams_t params, size_t maxChunkSize) 5919fcbaf1SConrad Meyer { 6019fcbaf1SConrad Meyer return params.enableLdm ? (maxChunkSize / params.minMatchLength) : 0; 610c16b537SWarner Losh } 620c16b537SWarner Losh 630c16b537SWarner Losh /** ZSTD_ldm_getSmallHash() : 640c16b537SWarner Losh * numBits should be <= 32 650c16b537SWarner Losh * If numBits==0, returns 0. 660c16b537SWarner Losh * @return : the most significant numBits of value. */ 670c16b537SWarner Losh static U32 ZSTD_ldm_getSmallHash(U64 value, U32 numBits) 680c16b537SWarner Losh { 690c16b537SWarner Losh assert(numBits <= 32); 700c16b537SWarner Losh return numBits == 0 ? 0 : (U32)(value >> (64 - numBits)); 710c16b537SWarner Losh } 720c16b537SWarner Losh 730c16b537SWarner Losh /** ZSTD_ldm_getChecksum() : 740c16b537SWarner Losh * numBitsToDiscard should be <= 32 750c16b537SWarner Losh * @return : the next most significant 32 bits after numBitsToDiscard */ 760c16b537SWarner Losh static U32 ZSTD_ldm_getChecksum(U64 hash, U32 numBitsToDiscard) 770c16b537SWarner Losh { 780c16b537SWarner Losh assert(numBitsToDiscard <= 32); 790c16b537SWarner Losh return (hash >> (64 - 32 - numBitsToDiscard)) & 0xFFFFFFFF; 800c16b537SWarner Losh } 810c16b537SWarner Losh 820c16b537SWarner Losh /** ZSTD_ldm_getTag() ; 830c16b537SWarner Losh * Given the hash, returns the most significant numTagBits bits 840c16b537SWarner Losh * after (32 + hbits) bits. 850c16b537SWarner Losh * 860c16b537SWarner Losh * If there are not enough bits remaining, return the last 870c16b537SWarner Losh * numTagBits bits. */ 880c16b537SWarner Losh static U32 ZSTD_ldm_getTag(U64 hash, U32 hbits, U32 numTagBits) 890c16b537SWarner Losh { 900c16b537SWarner Losh assert(numTagBits < 32 && hbits <= 32); 910c16b537SWarner Losh if (32 - hbits < numTagBits) { 920c16b537SWarner Losh return hash & (((U32)1 << numTagBits) - 1); 930c16b537SWarner Losh } else { 940c16b537SWarner Losh return (hash >> (32 - hbits - numTagBits)) & (((U32)1 << numTagBits) - 1); 950c16b537SWarner Losh } 960c16b537SWarner Losh } 970c16b537SWarner Losh 980c16b537SWarner Losh /** ZSTD_ldm_getBucket() : 990c16b537SWarner Losh * Returns a pointer to the start of the bucket associated with hash. */ 1000c16b537SWarner Losh static ldmEntry_t* ZSTD_ldm_getBucket( 1010c16b537SWarner Losh ldmState_t* ldmState, size_t hash, ldmParams_t const ldmParams) 1020c16b537SWarner Losh { 1030c16b537SWarner Losh return ldmState->hashTable + (hash << ldmParams.bucketSizeLog); 1040c16b537SWarner Losh } 1050c16b537SWarner Losh 1060c16b537SWarner Losh /** ZSTD_ldm_insertEntry() : 1070c16b537SWarner Losh * Insert the entry with corresponding hash into the hash table */ 1080c16b537SWarner Losh static void ZSTD_ldm_insertEntry(ldmState_t* ldmState, 1090c16b537SWarner Losh size_t const hash, const ldmEntry_t entry, 1100c16b537SWarner Losh ldmParams_t const ldmParams) 1110c16b537SWarner Losh { 1120c16b537SWarner Losh BYTE* const bucketOffsets = ldmState->bucketOffsets; 1130c16b537SWarner Losh *(ZSTD_ldm_getBucket(ldmState, hash, ldmParams) + bucketOffsets[hash]) = entry; 1140c16b537SWarner Losh bucketOffsets[hash]++; 1150c16b537SWarner Losh bucketOffsets[hash] &= ((U32)1 << ldmParams.bucketSizeLog) - 1; 1160c16b537SWarner Losh } 1170c16b537SWarner Losh 1180c16b537SWarner Losh /** ZSTD_ldm_makeEntryAndInsertByTag() : 1190c16b537SWarner Losh * 1200c16b537SWarner Losh * Gets the small hash, checksum, and tag from the rollingHash. 1210c16b537SWarner Losh * 122*a0483764SConrad Meyer * If the tag matches (1 << ldmParams.hashRateLog)-1, then 1230c16b537SWarner Losh * creates an ldmEntry from the offset, and inserts it into the hash table. 1240c16b537SWarner Losh * 1250c16b537SWarner Losh * hBits is the length of the small hash, which is the most significant hBits 1260c16b537SWarner Losh * of rollingHash. The checksum is the next 32 most significant bits, followed 127*a0483764SConrad Meyer * by ldmParams.hashRateLog bits that make up the tag. */ 1280c16b537SWarner Losh static void ZSTD_ldm_makeEntryAndInsertByTag(ldmState_t* ldmState, 1290c16b537SWarner Losh U64 const rollingHash, 1300c16b537SWarner Losh U32 const hBits, 1310c16b537SWarner Losh U32 const offset, 1320c16b537SWarner Losh ldmParams_t const ldmParams) 1330c16b537SWarner Losh { 134*a0483764SConrad Meyer U32 const tag = ZSTD_ldm_getTag(rollingHash, hBits, ldmParams.hashRateLog); 135*a0483764SConrad Meyer U32 const tagMask = ((U32)1 << ldmParams.hashRateLog) - 1; 1360c16b537SWarner Losh if (tag == tagMask) { 1370c16b537SWarner Losh U32 const hash = ZSTD_ldm_getSmallHash(rollingHash, hBits); 1380c16b537SWarner Losh U32 const checksum = ZSTD_ldm_getChecksum(rollingHash, hBits); 1390c16b537SWarner Losh ldmEntry_t entry; 1400c16b537SWarner Losh entry.offset = offset; 1410c16b537SWarner Losh entry.checksum = checksum; 1420c16b537SWarner Losh ZSTD_ldm_insertEntry(ldmState, hash, entry, ldmParams); 1430c16b537SWarner Losh } 1440c16b537SWarner Losh } 1450c16b537SWarner Losh 1460c16b537SWarner Losh /** ZSTD_ldm_countBackwardsMatch() : 1470c16b537SWarner Losh * Returns the number of bytes that match backwards before pIn and pMatch. 1480c16b537SWarner Losh * 1490c16b537SWarner Losh * We count only bytes where pMatch >= pBase and pIn >= pAnchor. */ 1500c16b537SWarner Losh static size_t ZSTD_ldm_countBackwardsMatch( 1510c16b537SWarner Losh const BYTE* pIn, const BYTE* pAnchor, 1520c16b537SWarner Losh const BYTE* pMatch, const BYTE* pBase) 1530c16b537SWarner Losh { 1540c16b537SWarner Losh size_t matchLength = 0; 1550c16b537SWarner Losh while (pIn > pAnchor && pMatch > pBase && pIn[-1] == pMatch[-1]) { 1560c16b537SWarner Losh pIn--; 1570c16b537SWarner Losh pMatch--; 1580c16b537SWarner Losh matchLength++; 1590c16b537SWarner Losh } 1600c16b537SWarner Losh return matchLength; 1610c16b537SWarner Losh } 1620c16b537SWarner Losh 1630c16b537SWarner Losh /** ZSTD_ldm_fillFastTables() : 1640c16b537SWarner Losh * 1650c16b537SWarner Losh * Fills the relevant tables for the ZSTD_fast and ZSTD_dfast strategies. 1660c16b537SWarner Losh * This is similar to ZSTD_loadDictionaryContent. 1670c16b537SWarner Losh * 1680c16b537SWarner Losh * The tables for the other strategies are filled within their 1690c16b537SWarner Losh * block compressors. */ 17019fcbaf1SConrad Meyer static size_t ZSTD_ldm_fillFastTables(ZSTD_matchState_t* ms, 17119fcbaf1SConrad Meyer void const* end) 1720c16b537SWarner Losh { 1730c16b537SWarner Losh const BYTE* const iend = (const BYTE*)end; 1740c16b537SWarner Losh 1750f743729SConrad Meyer switch(ms->cParams.strategy) 1760c16b537SWarner Losh { 1770c16b537SWarner Losh case ZSTD_fast: 1780f743729SConrad Meyer ZSTD_fillHashTable(ms, iend, ZSTD_dtlm_fast); 1790c16b537SWarner Losh break; 1800c16b537SWarner Losh 1810c16b537SWarner Losh case ZSTD_dfast: 1820f743729SConrad Meyer ZSTD_fillDoubleHashTable(ms, iend, ZSTD_dtlm_fast); 1830c16b537SWarner Losh break; 1840c16b537SWarner Losh 1850c16b537SWarner Losh case ZSTD_greedy: 1860c16b537SWarner Losh case ZSTD_lazy: 1870c16b537SWarner Losh case ZSTD_lazy2: 1880c16b537SWarner Losh case ZSTD_btlazy2: 1890c16b537SWarner Losh case ZSTD_btopt: 1900c16b537SWarner Losh case ZSTD_btultra: 191*a0483764SConrad Meyer case ZSTD_btultra2: 1920c16b537SWarner Losh break; 1930c16b537SWarner Losh default: 1940c16b537SWarner Losh assert(0); /* not possible : not a valid strategy id */ 1950c16b537SWarner Losh } 1960c16b537SWarner Losh 1970c16b537SWarner Losh return 0; 1980c16b537SWarner Losh } 1990c16b537SWarner Losh 2000c16b537SWarner Losh /** ZSTD_ldm_fillLdmHashTable() : 2010c16b537SWarner Losh * 2020c16b537SWarner Losh * Fills hashTable from (lastHashed + 1) to iend (non-inclusive). 2030c16b537SWarner Losh * lastHash is the rolling hash that corresponds to lastHashed. 2040c16b537SWarner Losh * 2050c16b537SWarner Losh * Returns the rolling hash corresponding to position iend-1. */ 2060c16b537SWarner Losh static U64 ZSTD_ldm_fillLdmHashTable(ldmState_t* state, 2070c16b537SWarner Losh U64 lastHash, const BYTE* lastHashed, 2080c16b537SWarner Losh const BYTE* iend, const BYTE* base, 2090c16b537SWarner Losh U32 hBits, ldmParams_t const ldmParams) 2100c16b537SWarner Losh { 2110c16b537SWarner Losh U64 rollingHash = lastHash; 2120c16b537SWarner Losh const BYTE* cur = lastHashed + 1; 2130c16b537SWarner Losh 2140c16b537SWarner Losh while (cur < iend) { 215*a0483764SConrad Meyer rollingHash = ZSTD_rollingHash_rotate(rollingHash, cur[-1], 2160c16b537SWarner Losh cur[ldmParams.minMatchLength-1], 2170c16b537SWarner Losh state->hashPower); 2180c16b537SWarner Losh ZSTD_ldm_makeEntryAndInsertByTag(state, 2190c16b537SWarner Losh rollingHash, hBits, 2200c16b537SWarner Losh (U32)(cur - base), ldmParams); 2210c16b537SWarner Losh ++cur; 2220c16b537SWarner Losh } 2230c16b537SWarner Losh return rollingHash; 2240c16b537SWarner Losh } 2250c16b537SWarner Losh 2260c16b537SWarner Losh 2270c16b537SWarner Losh /** ZSTD_ldm_limitTableUpdate() : 2280c16b537SWarner Losh * 2290c16b537SWarner Losh * Sets cctx->nextToUpdate to a position corresponding closer to anchor 2300c16b537SWarner Losh * if it is far way 2310c16b537SWarner Losh * (after a long match, only update tables a limited amount). */ 23219fcbaf1SConrad Meyer static void ZSTD_ldm_limitTableUpdate(ZSTD_matchState_t* ms, const BYTE* anchor) 2330c16b537SWarner Losh { 23419fcbaf1SConrad Meyer U32 const current = (U32)(anchor - ms->window.base); 23519fcbaf1SConrad Meyer if (current > ms->nextToUpdate + 1024) { 23619fcbaf1SConrad Meyer ms->nextToUpdate = 23719fcbaf1SConrad Meyer current - MIN(512, current - ms->nextToUpdate - 1024); 2380c16b537SWarner Losh } 2390c16b537SWarner Losh } 2400c16b537SWarner Losh 24119fcbaf1SConrad Meyer static size_t ZSTD_ldm_generateSequences_internal( 24219fcbaf1SConrad Meyer ldmState_t* ldmState, rawSeqStore_t* rawSeqStore, 24319fcbaf1SConrad Meyer ldmParams_t const* params, void const* src, size_t srcSize) 2440c16b537SWarner Losh { 24519fcbaf1SConrad Meyer /* LDM parameters */ 24619fcbaf1SConrad Meyer int const extDict = ZSTD_window_hasExtDict(ldmState->window); 24719fcbaf1SConrad Meyer U32 const minMatchLength = params->minMatchLength; 24819fcbaf1SConrad Meyer U64 const hashPower = ldmState->hashPower; 24919fcbaf1SConrad Meyer U32 const hBits = params->hashLog - params->bucketSizeLog; 25019fcbaf1SConrad Meyer U32 const ldmBucketSize = 1U << params->bucketSizeLog; 251*a0483764SConrad Meyer U32 const hashRateLog = params->hashRateLog; 252*a0483764SConrad Meyer U32 const ldmTagMask = (1U << params->hashRateLog) - 1; 25319fcbaf1SConrad Meyer /* Prefix and extDict parameters */ 25419fcbaf1SConrad Meyer U32 const dictLimit = ldmState->window.dictLimit; 25519fcbaf1SConrad Meyer U32 const lowestIndex = extDict ? ldmState->window.lowLimit : dictLimit; 25619fcbaf1SConrad Meyer BYTE const* const base = ldmState->window.base; 25719fcbaf1SConrad Meyer BYTE const* const dictBase = extDict ? ldmState->window.dictBase : NULL; 25819fcbaf1SConrad Meyer BYTE const* const dictStart = extDict ? dictBase + lowestIndex : NULL; 25919fcbaf1SConrad Meyer BYTE const* const dictEnd = extDict ? dictBase + dictLimit : NULL; 26019fcbaf1SConrad Meyer BYTE const* const lowPrefixPtr = base + dictLimit; 26119fcbaf1SConrad Meyer /* Input bounds */ 26219fcbaf1SConrad Meyer BYTE const* const istart = (BYTE const*)src; 26319fcbaf1SConrad Meyer BYTE const* const iend = istart + srcSize; 26419fcbaf1SConrad Meyer BYTE const* const ilimit = iend - MAX(minMatchLength, HASH_READ_SIZE); 26519fcbaf1SConrad Meyer /* Input positions */ 26619fcbaf1SConrad Meyer BYTE const* anchor = istart; 26719fcbaf1SConrad Meyer BYTE const* ip = istart; 26819fcbaf1SConrad Meyer /* Rolling hash */ 26919fcbaf1SConrad Meyer BYTE const* lastHashed = NULL; 2700c16b537SWarner Losh U64 rollingHash = 0; 2710c16b537SWarner Losh 27219fcbaf1SConrad Meyer while (ip <= ilimit) { 2730c16b537SWarner Losh size_t mLength; 2740c16b537SWarner Losh U32 const current = (U32)(ip - base); 2750c16b537SWarner Losh size_t forwardMatchLength = 0, backwardMatchLength = 0; 2760c16b537SWarner Losh ldmEntry_t* bestEntry = NULL; 2770c16b537SWarner Losh if (ip != istart) { 278*a0483764SConrad Meyer rollingHash = ZSTD_rollingHash_rotate(rollingHash, lastHashed[0], 27919fcbaf1SConrad Meyer lastHashed[minMatchLength], 2800c16b537SWarner Losh hashPower); 2810c16b537SWarner Losh } else { 282*a0483764SConrad Meyer rollingHash = ZSTD_rollingHash_compute(ip, minMatchLength); 2830c16b537SWarner Losh } 2840c16b537SWarner Losh lastHashed = ip; 2850c16b537SWarner Losh 2860c16b537SWarner Losh /* Do not insert and do not look for a match */ 287*a0483764SConrad Meyer if (ZSTD_ldm_getTag(rollingHash, hBits, hashRateLog) != ldmTagMask) { 2880c16b537SWarner Losh ip++; 2890c16b537SWarner Losh continue; 2900c16b537SWarner Losh } 2910c16b537SWarner Losh 2920c16b537SWarner Losh /* Get the best entry and compute the match lengths */ 2930c16b537SWarner Losh { 2940c16b537SWarner Losh ldmEntry_t* const bucket = 2950c16b537SWarner Losh ZSTD_ldm_getBucket(ldmState, 2960c16b537SWarner Losh ZSTD_ldm_getSmallHash(rollingHash, hBits), 29719fcbaf1SConrad Meyer *params); 2980c16b537SWarner Losh ldmEntry_t* cur; 2990c16b537SWarner Losh size_t bestMatchLength = 0; 3000c16b537SWarner Losh U32 const checksum = ZSTD_ldm_getChecksum(rollingHash, hBits); 3010c16b537SWarner Losh 3020c16b537SWarner Losh for (cur = bucket; cur < bucket + ldmBucketSize; ++cur) { 3030c16b537SWarner Losh size_t curForwardMatchLength, curBackwardMatchLength, 3040c16b537SWarner Losh curTotalMatchLength; 3050c16b537SWarner Losh if (cur->checksum != checksum || cur->offset <= lowestIndex) { 3060c16b537SWarner Losh continue; 3070c16b537SWarner Losh } 30819fcbaf1SConrad Meyer if (extDict) { 30919fcbaf1SConrad Meyer BYTE const* const curMatchBase = 31019fcbaf1SConrad Meyer cur->offset < dictLimit ? dictBase : base; 31119fcbaf1SConrad Meyer BYTE const* const pMatch = curMatchBase + cur->offset; 31219fcbaf1SConrad Meyer BYTE const* const matchEnd = 31319fcbaf1SConrad Meyer cur->offset < dictLimit ? dictEnd : iend; 31419fcbaf1SConrad Meyer BYTE const* const lowMatchPtr = 31519fcbaf1SConrad Meyer cur->offset < dictLimit ? dictStart : lowPrefixPtr; 3160c16b537SWarner Losh 31719fcbaf1SConrad Meyer curForwardMatchLength = ZSTD_count_2segments( 31819fcbaf1SConrad Meyer ip, pMatch, iend, 31919fcbaf1SConrad Meyer matchEnd, lowPrefixPtr); 32019fcbaf1SConrad Meyer if (curForwardMatchLength < minMatchLength) { 3210c16b537SWarner Losh continue; 3220c16b537SWarner Losh } 32319fcbaf1SConrad Meyer curBackwardMatchLength = 32419fcbaf1SConrad Meyer ZSTD_ldm_countBackwardsMatch(ip, anchor, pMatch, 32519fcbaf1SConrad Meyer lowMatchPtr); 3260c16b537SWarner Losh curTotalMatchLength = curForwardMatchLength + 3270c16b537SWarner Losh curBackwardMatchLength; 32819fcbaf1SConrad Meyer } else { /* !extDict */ 32919fcbaf1SConrad Meyer BYTE const* const pMatch = base + cur->offset; 33019fcbaf1SConrad Meyer curForwardMatchLength = ZSTD_count(ip, pMatch, iend); 33119fcbaf1SConrad Meyer if (curForwardMatchLength < minMatchLength) { 33219fcbaf1SConrad Meyer continue; 33319fcbaf1SConrad Meyer } 33419fcbaf1SConrad Meyer curBackwardMatchLength = 33519fcbaf1SConrad Meyer ZSTD_ldm_countBackwardsMatch(ip, anchor, pMatch, 33619fcbaf1SConrad Meyer lowPrefixPtr); 33719fcbaf1SConrad Meyer curTotalMatchLength = curForwardMatchLength + 33819fcbaf1SConrad Meyer curBackwardMatchLength; 33919fcbaf1SConrad Meyer } 3400c16b537SWarner Losh 3410c16b537SWarner Losh if (curTotalMatchLength > bestMatchLength) { 3420c16b537SWarner Losh bestMatchLength = curTotalMatchLength; 3430c16b537SWarner Losh forwardMatchLength = curForwardMatchLength; 3440c16b537SWarner Losh backwardMatchLength = curBackwardMatchLength; 3450c16b537SWarner Losh bestEntry = cur; 3460c16b537SWarner Losh } 3470c16b537SWarner Losh } 3480c16b537SWarner Losh } 3490c16b537SWarner Losh 3500c16b537SWarner Losh /* No match found -- continue searching */ 3510c16b537SWarner Losh if (bestEntry == NULL) { 3520c16b537SWarner Losh ZSTD_ldm_makeEntryAndInsertByTag(ldmState, rollingHash, 3530c16b537SWarner Losh hBits, current, 35419fcbaf1SConrad Meyer *params); 3550c16b537SWarner Losh ip++; 3560c16b537SWarner Losh continue; 3570c16b537SWarner Losh } 3580c16b537SWarner Losh 3590c16b537SWarner Losh /* Match found */ 3600c16b537SWarner Losh mLength = forwardMatchLength + backwardMatchLength; 3610c16b537SWarner Losh ip -= backwardMatchLength; 3620c16b537SWarner Losh 3630c16b537SWarner Losh { 36419fcbaf1SConrad Meyer /* Store the sequence: 36519fcbaf1SConrad Meyer * ip = current - backwardMatchLength 36619fcbaf1SConrad Meyer * The match is at (bestEntry->offset - backwardMatchLength) 36719fcbaf1SConrad Meyer */ 3680c16b537SWarner Losh U32 const matchIndex = bestEntry->offset; 36919fcbaf1SConrad Meyer U32 const offset = current - matchIndex; 37019fcbaf1SConrad Meyer rawSeq* const seq = rawSeqStore->seq + rawSeqStore->size; 3710c16b537SWarner Losh 37219fcbaf1SConrad Meyer /* Out of sequence storage */ 37319fcbaf1SConrad Meyer if (rawSeqStore->size == rawSeqStore->capacity) 37419fcbaf1SConrad Meyer return ERROR(dstSize_tooSmall); 37519fcbaf1SConrad Meyer seq->litLength = (U32)(ip - anchor); 37619fcbaf1SConrad Meyer seq->matchLength = (U32)mLength; 37719fcbaf1SConrad Meyer seq->offset = offset; 37819fcbaf1SConrad Meyer rawSeqStore->size++; 3790c16b537SWarner Losh } 3800c16b537SWarner Losh 3810c16b537SWarner Losh /* Insert the current entry into the hash table */ 3820c16b537SWarner Losh ZSTD_ldm_makeEntryAndInsertByTag(ldmState, rollingHash, hBits, 3830c16b537SWarner Losh (U32)(lastHashed - base), 38419fcbaf1SConrad Meyer *params); 3850c16b537SWarner Losh 3860c16b537SWarner Losh assert(ip + backwardMatchLength == lastHashed); 3870c16b537SWarner Losh 3880c16b537SWarner Losh /* Fill the hash table from lastHashed+1 to ip+mLength*/ 3890c16b537SWarner Losh /* Heuristic: don't need to fill the entire table at end of block */ 39019fcbaf1SConrad Meyer if (ip + mLength <= ilimit) { 3910c16b537SWarner Losh rollingHash = ZSTD_ldm_fillLdmHashTable( 3920c16b537SWarner Losh ldmState, rollingHash, lastHashed, 39319fcbaf1SConrad Meyer ip + mLength, base, hBits, *params); 3940c16b537SWarner Losh lastHashed = ip + mLength - 1; 3950c16b537SWarner Losh } 3960c16b537SWarner Losh ip += mLength; 3970c16b537SWarner Losh anchor = ip; 39819fcbaf1SConrad Meyer } 39919fcbaf1SConrad Meyer return iend - anchor; 40019fcbaf1SConrad Meyer } 4010c16b537SWarner Losh 40219fcbaf1SConrad Meyer /*! ZSTD_ldm_reduceTable() : 40319fcbaf1SConrad Meyer * reduce table indexes by `reducerValue` */ 40419fcbaf1SConrad Meyer static void ZSTD_ldm_reduceTable(ldmEntry_t* const table, U32 const size, 40519fcbaf1SConrad Meyer U32 const reducerValue) 4060c16b537SWarner Losh { 40719fcbaf1SConrad Meyer U32 u; 40819fcbaf1SConrad Meyer for (u = 0; u < size; u++) { 40919fcbaf1SConrad Meyer if (table[u].offset < reducerValue) table[u].offset = 0; 41019fcbaf1SConrad Meyer else table[u].offset -= reducerValue; 4110c16b537SWarner Losh } 4120c16b537SWarner Losh } 4130c16b537SWarner Losh 41419fcbaf1SConrad Meyer size_t ZSTD_ldm_generateSequences( 41519fcbaf1SConrad Meyer ldmState_t* ldmState, rawSeqStore_t* sequences, 41619fcbaf1SConrad Meyer ldmParams_t const* params, void const* src, size_t srcSize) 4170c16b537SWarner Losh { 41819fcbaf1SConrad Meyer U32 const maxDist = 1U << params->windowLog; 41919fcbaf1SConrad Meyer BYTE const* const istart = (BYTE const*)src; 42019fcbaf1SConrad Meyer BYTE const* const iend = istart + srcSize; 42119fcbaf1SConrad Meyer size_t const kMaxChunkSize = 1 << 20; 42219fcbaf1SConrad Meyer size_t const nbChunks = (srcSize / kMaxChunkSize) + ((srcSize % kMaxChunkSize) != 0); 42319fcbaf1SConrad Meyer size_t chunk; 42419fcbaf1SConrad Meyer size_t leftoverSize = 0; 42519fcbaf1SConrad Meyer 42619fcbaf1SConrad Meyer assert(ZSTD_CHUNKSIZE_MAX >= kMaxChunkSize); 42719fcbaf1SConrad Meyer /* Check that ZSTD_window_update() has been called for this chunk prior 42819fcbaf1SConrad Meyer * to passing it to this function. 42919fcbaf1SConrad Meyer */ 43019fcbaf1SConrad Meyer assert(ldmState->window.nextSrc >= (BYTE const*)src + srcSize); 43119fcbaf1SConrad Meyer /* The input could be very large (in zstdmt), so it must be broken up into 43219fcbaf1SConrad Meyer * chunks to enforce the maximmum distance and handle overflow correction. 43319fcbaf1SConrad Meyer */ 43419fcbaf1SConrad Meyer assert(sequences->pos <= sequences->size); 43519fcbaf1SConrad Meyer assert(sequences->size <= sequences->capacity); 43619fcbaf1SConrad Meyer for (chunk = 0; chunk < nbChunks && sequences->size < sequences->capacity; ++chunk) { 43719fcbaf1SConrad Meyer BYTE const* const chunkStart = istart + chunk * kMaxChunkSize; 43819fcbaf1SConrad Meyer size_t const remaining = (size_t)(iend - chunkStart); 43919fcbaf1SConrad Meyer BYTE const *const chunkEnd = 44019fcbaf1SConrad Meyer (remaining < kMaxChunkSize) ? iend : chunkStart + kMaxChunkSize; 44119fcbaf1SConrad Meyer size_t const chunkSize = chunkEnd - chunkStart; 44219fcbaf1SConrad Meyer size_t newLeftoverSize; 44319fcbaf1SConrad Meyer size_t const prevSize = sequences->size; 44419fcbaf1SConrad Meyer 44519fcbaf1SConrad Meyer assert(chunkStart < iend); 44619fcbaf1SConrad Meyer /* 1. Perform overflow correction if necessary. */ 44719fcbaf1SConrad Meyer if (ZSTD_window_needOverflowCorrection(ldmState->window, chunkEnd)) { 44819fcbaf1SConrad Meyer U32 const ldmHSize = 1U << params->hashLog; 44919fcbaf1SConrad Meyer U32 const correction = ZSTD_window_correctOverflow( 45019fcbaf1SConrad Meyer &ldmState->window, /* cycleLog */ 0, maxDist, src); 45119fcbaf1SConrad Meyer ZSTD_ldm_reduceTable(ldmState->hashTable, ldmHSize, correction); 4520c16b537SWarner Losh } 45319fcbaf1SConrad Meyer /* 2. We enforce the maximum offset allowed. 45419fcbaf1SConrad Meyer * 45519fcbaf1SConrad Meyer * kMaxChunkSize should be small enough that we don't lose too much of 45619fcbaf1SConrad Meyer * the window through early invalidation. 45719fcbaf1SConrad Meyer * TODO: * Test the chunk size. 45819fcbaf1SConrad Meyer * * Try invalidation after the sequence generation and test the 45919fcbaf1SConrad Meyer * the offset against maxDist directly. 46019fcbaf1SConrad Meyer */ 4610f743729SConrad Meyer ZSTD_window_enforceMaxDist(&ldmState->window, chunkEnd, maxDist, NULL, NULL); 46219fcbaf1SConrad Meyer /* 3. Generate the sequences for the chunk, and get newLeftoverSize. */ 46319fcbaf1SConrad Meyer newLeftoverSize = ZSTD_ldm_generateSequences_internal( 46419fcbaf1SConrad Meyer ldmState, sequences, params, chunkStart, chunkSize); 46519fcbaf1SConrad Meyer if (ZSTD_isError(newLeftoverSize)) 46619fcbaf1SConrad Meyer return newLeftoverSize; 46719fcbaf1SConrad Meyer /* 4. We add the leftover literals from previous iterations to the first 46819fcbaf1SConrad Meyer * newly generated sequence, or add the `newLeftoverSize` if none are 46919fcbaf1SConrad Meyer * generated. 47019fcbaf1SConrad Meyer */ 47119fcbaf1SConrad Meyer /* Prepend the leftover literals from the last call */ 47219fcbaf1SConrad Meyer if (prevSize < sequences->size) { 47319fcbaf1SConrad Meyer sequences->seq[prevSize].litLength += (U32)leftoverSize; 47419fcbaf1SConrad Meyer leftoverSize = newLeftoverSize; 4750c16b537SWarner Losh } else { 47619fcbaf1SConrad Meyer assert(newLeftoverSize == chunkSize); 47719fcbaf1SConrad Meyer leftoverSize += chunkSize; 4780c16b537SWarner Losh } 47919fcbaf1SConrad Meyer } 48019fcbaf1SConrad Meyer return 0; 4810c16b537SWarner Losh } 4820c16b537SWarner Losh 48319fcbaf1SConrad Meyer void ZSTD_ldm_skipSequences(rawSeqStore_t* rawSeqStore, size_t srcSize, U32 const minMatch) { 48419fcbaf1SConrad Meyer while (srcSize > 0 && rawSeqStore->pos < rawSeqStore->size) { 48519fcbaf1SConrad Meyer rawSeq* seq = rawSeqStore->seq + rawSeqStore->pos; 48619fcbaf1SConrad Meyer if (srcSize <= seq->litLength) { 48719fcbaf1SConrad Meyer /* Skip past srcSize literals */ 48819fcbaf1SConrad Meyer seq->litLength -= (U32)srcSize; 48919fcbaf1SConrad Meyer return; 49019fcbaf1SConrad Meyer } 49119fcbaf1SConrad Meyer srcSize -= seq->litLength; 49219fcbaf1SConrad Meyer seq->litLength = 0; 49319fcbaf1SConrad Meyer if (srcSize < seq->matchLength) { 49419fcbaf1SConrad Meyer /* Skip past the first srcSize of the match */ 49519fcbaf1SConrad Meyer seq->matchLength -= (U32)srcSize; 49619fcbaf1SConrad Meyer if (seq->matchLength < minMatch) { 49719fcbaf1SConrad Meyer /* The match is too short, omit it */ 49819fcbaf1SConrad Meyer if (rawSeqStore->pos + 1 < rawSeqStore->size) { 49919fcbaf1SConrad Meyer seq[1].litLength += seq[0].matchLength; 50019fcbaf1SConrad Meyer } 50119fcbaf1SConrad Meyer rawSeqStore->pos++; 50219fcbaf1SConrad Meyer } 50319fcbaf1SConrad Meyer return; 50419fcbaf1SConrad Meyer } 50519fcbaf1SConrad Meyer srcSize -= seq->matchLength; 50619fcbaf1SConrad Meyer seq->matchLength = 0; 50719fcbaf1SConrad Meyer rawSeqStore->pos++; 50819fcbaf1SConrad Meyer } 50919fcbaf1SConrad Meyer } 51019fcbaf1SConrad Meyer 51119fcbaf1SConrad Meyer /** 51219fcbaf1SConrad Meyer * If the sequence length is longer than remaining then the sequence is split 51319fcbaf1SConrad Meyer * between this block and the next. 51419fcbaf1SConrad Meyer * 51519fcbaf1SConrad Meyer * Returns the current sequence to handle, or if the rest of the block should 51619fcbaf1SConrad Meyer * be literals, it returns a sequence with offset == 0. 51719fcbaf1SConrad Meyer */ 51819fcbaf1SConrad Meyer static rawSeq maybeSplitSequence(rawSeqStore_t* rawSeqStore, 51919fcbaf1SConrad Meyer U32 const remaining, U32 const minMatch) 5200c16b537SWarner Losh { 52119fcbaf1SConrad Meyer rawSeq sequence = rawSeqStore->seq[rawSeqStore->pos]; 52219fcbaf1SConrad Meyer assert(sequence.offset > 0); 52319fcbaf1SConrad Meyer /* Likely: No partial sequence */ 52419fcbaf1SConrad Meyer if (remaining >= sequence.litLength + sequence.matchLength) { 52519fcbaf1SConrad Meyer rawSeqStore->pos++; 52619fcbaf1SConrad Meyer return sequence; 5270c16b537SWarner Losh } 52819fcbaf1SConrad Meyer /* Cut the sequence short (offset == 0 ==> rest is literals). */ 52919fcbaf1SConrad Meyer if (remaining <= sequence.litLength) { 53019fcbaf1SConrad Meyer sequence.offset = 0; 53119fcbaf1SConrad Meyer } else if (remaining < sequence.litLength + sequence.matchLength) { 53219fcbaf1SConrad Meyer sequence.matchLength = remaining - sequence.litLength; 53319fcbaf1SConrad Meyer if (sequence.matchLength < minMatch) { 53419fcbaf1SConrad Meyer sequence.offset = 0; 5350c16b537SWarner Losh } 5360c16b537SWarner Losh } 53719fcbaf1SConrad Meyer /* Skip past `remaining` bytes for the future sequences. */ 53819fcbaf1SConrad Meyer ZSTD_ldm_skipSequences(rawSeqStore, remaining, minMatch); 53919fcbaf1SConrad Meyer return sequence; 5400c16b537SWarner Losh } 5410c16b537SWarner Losh 54219fcbaf1SConrad Meyer size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore, 54319fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 5440f743729SConrad Meyer void const* src, size_t srcSize) 5450c16b537SWarner Losh { 5460f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams; 547*a0483764SConrad Meyer unsigned const minMatch = cParams->minMatch; 54819fcbaf1SConrad Meyer ZSTD_blockCompressor const blockCompressor = 5490f743729SConrad Meyer ZSTD_selectBlockCompressor(cParams->strategy, ZSTD_matchState_dictMode(ms)); 55019fcbaf1SConrad Meyer /* Input bounds */ 55119fcbaf1SConrad Meyer BYTE const* const istart = (BYTE const*)src; 55219fcbaf1SConrad Meyer BYTE const* const iend = istart + srcSize; 55319fcbaf1SConrad Meyer /* Input positions */ 55419fcbaf1SConrad Meyer BYTE const* ip = istart; 5550c16b537SWarner Losh 5560f743729SConrad Meyer DEBUGLOG(5, "ZSTD_ldm_blockCompress: srcSize=%zu", srcSize); 55719fcbaf1SConrad Meyer assert(rawSeqStore->pos <= rawSeqStore->size); 55819fcbaf1SConrad Meyer assert(rawSeqStore->size <= rawSeqStore->capacity); 55919fcbaf1SConrad Meyer /* Loop through each sequence and apply the block compressor to the lits */ 56019fcbaf1SConrad Meyer while (rawSeqStore->pos < rawSeqStore->size && ip < iend) { 56119fcbaf1SConrad Meyer /* maybeSplitSequence updates rawSeqStore->pos */ 56219fcbaf1SConrad Meyer rawSeq const sequence = maybeSplitSequence(rawSeqStore, 56319fcbaf1SConrad Meyer (U32)(iend - ip), minMatch); 56419fcbaf1SConrad Meyer int i; 56519fcbaf1SConrad Meyer /* End signal */ 56619fcbaf1SConrad Meyer if (sequence.offset == 0) 5670c16b537SWarner Losh break; 56819fcbaf1SConrad Meyer 56919fcbaf1SConrad Meyer assert(sequence.offset <= (1U << cParams->windowLog)); 57019fcbaf1SConrad Meyer assert(ip + sequence.litLength + sequence.matchLength <= iend); 57119fcbaf1SConrad Meyer 57219fcbaf1SConrad Meyer /* Fill tables for block compressor */ 57319fcbaf1SConrad Meyer ZSTD_ldm_limitTableUpdate(ms, ip); 5740f743729SConrad Meyer ZSTD_ldm_fillFastTables(ms, ip); 57519fcbaf1SConrad Meyer /* Run the block compressor */ 5760f743729SConrad Meyer DEBUGLOG(5, "calling block compressor on segment of size %u", sequence.litLength); 57719fcbaf1SConrad Meyer { 57819fcbaf1SConrad Meyer size_t const newLitLength = 5790f743729SConrad Meyer blockCompressor(ms, seqStore, rep, ip, sequence.litLength); 58019fcbaf1SConrad Meyer ip += sequence.litLength; 58119fcbaf1SConrad Meyer /* Update the repcodes */ 58219fcbaf1SConrad Meyer for (i = ZSTD_REP_NUM - 1; i > 0; i--) 58319fcbaf1SConrad Meyer rep[i] = rep[i-1]; 58419fcbaf1SConrad Meyer rep[0] = sequence.offset; 58519fcbaf1SConrad Meyer /* Store the sequence */ 58619fcbaf1SConrad Meyer ZSTD_storeSeq(seqStore, newLitLength, ip - newLitLength, 58719fcbaf1SConrad Meyer sequence.offset + ZSTD_REP_MOVE, 58819fcbaf1SConrad Meyer sequence.matchLength - MINMATCH); 58919fcbaf1SConrad Meyer ip += sequence.matchLength; 5900c16b537SWarner Losh } 5910c16b537SWarner Losh } 59219fcbaf1SConrad Meyer /* Fill the tables for the block compressor */ 59319fcbaf1SConrad Meyer ZSTD_ldm_limitTableUpdate(ms, ip); 5940f743729SConrad Meyer ZSTD_ldm_fillFastTables(ms, ip); 59519fcbaf1SConrad Meyer /* Compress the last literals */ 5960f743729SConrad Meyer return blockCompressor(ms, seqStore, rep, ip, iend - ip); 5970c16b537SWarner Losh } 598