zstd_lazy.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #include "zstd_compress_internal.h"
  11. #include "zstd_lazy.h"
  12. /*-*************************************
  13. * Binary Tree search
  14. ***************************************/
  15. static void
  16. ZSTD_updateDUBT(ZSTD_matchState_t* ms,
  17. const BYTE* ip, const BYTE* iend,
  18. U32 mls)
  19. {
  20. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  21. U32* const hashTable = ms->hashTable;
  22. U32 const hashLog = cParams->hashLog;
  23. U32* const bt = ms->chainTable;
  24. U32 const btLog = cParams->chainLog - 1;
  25. U32 const btMask = (1 << btLog) - 1;
  26. const BYTE* const base = ms->window.base;
  27. U32 const target = (U32)(ip - base);
  28. U32 idx = ms->nextToUpdate;
  29. if (idx != target)
  30. DEBUGLOG(7, "ZSTD_updateDUBT, from %u to %u (dictLimit:%u)",
  31. idx, target, ms->window.dictLimit);
  32. assert(ip + 8 <= iend); /* condition for ZSTD_hashPtr */
  33. (void)iend;
  34. assert(idx >= ms->window.dictLimit); /* condition for valid base+idx */
  35. for ( ; idx < target ; idx++) {
  36. size_t const h = ZSTD_hashPtr(base + idx, hashLog, mls); /* assumption : ip + 8 <= iend */
  37. U32 const matchIndex = hashTable[h];
  38. U32* const nextCandidatePtr = bt + 2*(idx&btMask);
  39. U32* const sortMarkPtr = nextCandidatePtr + 1;
  40. DEBUGLOG(8, "ZSTD_updateDUBT: insert %u", idx);
  41. hashTable[h] = idx; /* Update Hash Table */
  42. *nextCandidatePtr = matchIndex; /* update BT like a chain */
  43. *sortMarkPtr = ZSTD_DUBT_UNSORTED_MARK;
  44. }
  45. ms->nextToUpdate = target;
  46. }
  47. /** ZSTD_insertDUBT1() :
  48. * sort one already inserted but unsorted position
  49. * assumption : current >= btlow == (current - btmask)
  50. * doesn't fail */
  51. static void
  52. ZSTD_insertDUBT1(ZSTD_matchState_t* ms,
  53. U32 current, const BYTE* inputEnd,
  54. U32 nbCompares, U32 btLow,
  55. const ZSTD_dictMode_e dictMode)
  56. {
  57. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  58. U32* const bt = ms->chainTable;
  59. U32 const btLog = cParams->chainLog - 1;
  60. U32 const btMask = (1 << btLog) - 1;
  61. size_t commonLengthSmaller=0, commonLengthLarger=0;
  62. const BYTE* const base = ms->window.base;
  63. const BYTE* const dictBase = ms->window.dictBase;
  64. const U32 dictLimit = ms->window.dictLimit;
  65. const BYTE* const ip = (current>=dictLimit) ? base + current : dictBase + current;
  66. const BYTE* const iend = (current>=dictLimit) ? inputEnd : dictBase + dictLimit;
  67. const BYTE* const dictEnd = dictBase + dictLimit;
  68. const BYTE* const prefixStart = base + dictLimit;
  69. const BYTE* match;
  70. U32* smallerPtr = bt + 2*(current&btMask);
  71. U32* largerPtr = smallerPtr + 1;
  72. U32 matchIndex = *smallerPtr; /* this candidate is unsorted : next sorted candidate is reached through *smallerPtr, while *largerPtr contains previous unsorted candidate (which is already saved and can be overwritten) */
  73. U32 dummy32; /* to be nullified at the end */
  74. U32 const windowLow = ms->window.lowLimit;
  75. DEBUGLOG(8, "ZSTD_insertDUBT1(%u) (dictLimit=%u, lowLimit=%u)",
  76. current, dictLimit, windowLow);
  77. assert(current >= btLow);
  78. assert(ip < iend); /* condition for ZSTD_count */
  79. while (nbCompares-- && (matchIndex > windowLow)) {
  80. U32* const nextPtr = bt + 2*(matchIndex & btMask);
  81. size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
  82. assert(matchIndex < current);
  83. /* note : all candidates are now supposed sorted,
  84. * but it's still possible to have nextPtr[1] == ZSTD_DUBT_UNSORTED_MARK
  85. * when a real index has the same value as ZSTD_DUBT_UNSORTED_MARK */
  86. if ( (dictMode != ZSTD_extDict)
  87. || (matchIndex+matchLength >= dictLimit) /* both in current segment*/
  88. || (current < dictLimit) /* both in extDict */) {
  89. const BYTE* const mBase = ( (dictMode != ZSTD_extDict)
  90. || (matchIndex+matchLength >= dictLimit)) ?
  91. base : dictBase;
  92. assert( (matchIndex+matchLength >= dictLimit) /* might be wrong if extDict is incorrectly set to 0 */
  93. || (current < dictLimit) );
  94. match = mBase + matchIndex;
  95. matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
  96. } else {
  97. match = dictBase + matchIndex;
  98. matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
  99. if (matchIndex+matchLength >= dictLimit)
  100. match = base + matchIndex; /* preparation for next read of match[matchLength] */
  101. }
  102. DEBUGLOG(8, "ZSTD_insertDUBT1: comparing %u with %u : found %u common bytes ",
  103. current, matchIndex, (U32)matchLength);
  104. if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
  105. break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt tree */
  106. }
  107. if (match[matchLength] < ip[matchLength]) { /* necessarily within buffer */
  108. /* match is smaller than current */
  109. *smallerPtr = matchIndex; /* update smaller idx */
  110. commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
  111. if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop searching */
  112. DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is smaller : next => %u",
  113. matchIndex, btLow, nextPtr[1]);
  114. smallerPtr = nextPtr+1; /* new "candidate" => larger than match, which was smaller than target */
  115. matchIndex = nextPtr[1]; /* new matchIndex, larger than previous and closer to current */
  116. } else {
  117. /* match is larger than current */
  118. *largerPtr = matchIndex;
  119. commonLengthLarger = matchLength;
  120. if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop searching */
  121. DEBUGLOG(8, "ZSTD_insertDUBT1: %u (>btLow=%u) is larger => %u",
  122. matchIndex, btLow, nextPtr[0]);
  123. largerPtr = nextPtr;
  124. matchIndex = nextPtr[0];
  125. } }
  126. *smallerPtr = *largerPtr = 0;
  127. }
  128. static size_t
  129. ZSTD_DUBT_findBetterDictMatch (
  130. ZSTD_matchState_t* ms,
  131. const BYTE* const ip, const BYTE* const iend,
  132. size_t* offsetPtr,
  133. size_t bestLength,
  134. U32 nbCompares,
  135. U32 const mls,
  136. const ZSTD_dictMode_e dictMode)
  137. {
  138. const ZSTD_matchState_t * const dms = ms->dictMatchState;
  139. const ZSTD_compressionParameters* const dmsCParams = &dms->cParams;
  140. const U32 * const dictHashTable = dms->hashTable;
  141. U32 const hashLog = dmsCParams->hashLog;
  142. size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
  143. U32 dictMatchIndex = dictHashTable[h];
  144. const BYTE* const base = ms->window.base;
  145. const BYTE* const prefixStart = base + ms->window.dictLimit;
  146. U32 const current = (U32)(ip-base);
  147. const BYTE* const dictBase = dms->window.base;
  148. const BYTE* const dictEnd = dms->window.nextSrc;
  149. U32 const dictHighLimit = (U32)(dms->window.nextSrc - dms->window.base);
  150. U32 const dictLowLimit = dms->window.lowLimit;
  151. U32 const dictIndexDelta = ms->window.lowLimit - dictHighLimit;
  152. U32* const dictBt = dms->chainTable;
  153. U32 const btLog = dmsCParams->chainLog - 1;
  154. U32 const btMask = (1 << btLog) - 1;
  155. U32 const btLow = (btMask >= dictHighLimit - dictLowLimit) ? dictLowLimit : dictHighLimit - btMask;
  156. size_t commonLengthSmaller=0, commonLengthLarger=0;
  157. (void)dictMode;
  158. assert(dictMode == ZSTD_dictMatchState);
  159. while (nbCompares-- && (dictMatchIndex > dictLowLimit)) {
  160. U32* const nextPtr = dictBt + 2*(dictMatchIndex & btMask);
  161. size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
  162. const BYTE* match = dictBase + dictMatchIndex;
  163. matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
  164. if (dictMatchIndex+matchLength >= dictHighLimit)
  165. match = base + dictMatchIndex + dictIndexDelta; /* to prepare for next usage of match[matchLength] */
  166. if (matchLength > bestLength) {
  167. U32 matchIndex = dictMatchIndex + dictIndexDelta;
  168. if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(current-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) ) {
  169. DEBUGLOG(9, "ZSTD_DUBT_findBetterDictMatch(%u) : found better match length %u -> %u and offsetCode %u -> %u (dictMatchIndex %u, matchIndex %u)",
  170. current, (U32)bestLength, (U32)matchLength, (U32)*offsetPtr, ZSTD_REP_MOVE + current - matchIndex, dictMatchIndex, matchIndex);
  171. bestLength = matchLength, *offsetPtr = ZSTD_REP_MOVE + current - matchIndex;
  172. }
  173. if (ip+matchLength == iend) { /* reached end of input : ip[matchLength] is not valid, no way to know if it's larger or smaller than match */
  174. break; /* drop, to guarantee consistency (miss a little bit of compression) */
  175. }
  176. }
  177. if (match[matchLength] < ip[matchLength]) {
  178. if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
  179. commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
  180. dictMatchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
  181. } else {
  182. /* match is larger than current */
  183. if (dictMatchIndex <= btLow) { break; } /* beyond tree size, stop the search */
  184. commonLengthLarger = matchLength;
  185. dictMatchIndex = nextPtr[0];
  186. }
  187. }
  188. if (bestLength >= MINMATCH) {
  189. U32 const mIndex = current - ((U32)*offsetPtr - ZSTD_REP_MOVE); (void)mIndex;
  190. DEBUGLOG(8, "ZSTD_DUBT_findBetterDictMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
  191. current, (U32)bestLength, (U32)*offsetPtr, mIndex);
  192. }
  193. return bestLength;
  194. }
  195. static size_t
  196. ZSTD_DUBT_findBestMatch(ZSTD_matchState_t* ms,
  197. const BYTE* const ip, const BYTE* const iend,
  198. size_t* offsetPtr,
  199. U32 const mls,
  200. const ZSTD_dictMode_e dictMode)
  201. {
  202. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  203. U32* const hashTable = ms->hashTable;
  204. U32 const hashLog = cParams->hashLog;
  205. size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
  206. U32 matchIndex = hashTable[h];
  207. const BYTE* const base = ms->window.base;
  208. U32 const current = (U32)(ip-base);
  209. U32 const windowLow = ms->window.lowLimit;
  210. U32* const bt = ms->chainTable;
  211. U32 const btLog = cParams->chainLog - 1;
  212. U32 const btMask = (1 << btLog) - 1;
  213. U32 const btLow = (btMask >= current) ? 0 : current - btMask;
  214. U32 const unsortLimit = MAX(btLow, windowLow);
  215. U32* nextCandidate = bt + 2*(matchIndex&btMask);
  216. U32* unsortedMark = bt + 2*(matchIndex&btMask) + 1;
  217. U32 nbCompares = 1U << cParams->searchLog;
  218. U32 nbCandidates = nbCompares;
  219. U32 previousCandidate = 0;
  220. DEBUGLOG(7, "ZSTD_DUBT_findBestMatch (%u) ", current);
  221. assert(ip <= iend-8); /* required for h calculation */
  222. /* reach end of unsorted candidates list */
  223. while ( (matchIndex > unsortLimit)
  224. && (*unsortedMark == ZSTD_DUBT_UNSORTED_MARK)
  225. && (nbCandidates > 1) ) {
  226. DEBUGLOG(8, "ZSTD_DUBT_findBestMatch: candidate %u is unsorted",
  227. matchIndex);
  228. *unsortedMark = previousCandidate; /* the unsortedMark becomes a reversed chain, to move up back to original position */
  229. previousCandidate = matchIndex;
  230. matchIndex = *nextCandidate;
  231. nextCandidate = bt + 2*(matchIndex&btMask);
  232. unsortedMark = bt + 2*(matchIndex&btMask) + 1;
  233. nbCandidates --;
  234. }
  235. /* nullify last candidate if it's still unsorted
  236. * simplification, detrimental to compression ratio, beneficial for speed */
  237. if ( (matchIndex > unsortLimit)
  238. && (*unsortedMark==ZSTD_DUBT_UNSORTED_MARK) ) {
  239. DEBUGLOG(7, "ZSTD_DUBT_findBestMatch: nullify last unsorted candidate %u",
  240. matchIndex);
  241. *nextCandidate = *unsortedMark = 0;
  242. }
  243. /* batch sort stacked candidates */
  244. matchIndex = previousCandidate;
  245. while (matchIndex) { /* will end on matchIndex == 0 */
  246. U32* const nextCandidateIdxPtr = bt + 2*(matchIndex&btMask) + 1;
  247. U32 const nextCandidateIdx = *nextCandidateIdxPtr;
  248. ZSTD_insertDUBT1(ms, matchIndex, iend,
  249. nbCandidates, unsortLimit, dictMode);
  250. matchIndex = nextCandidateIdx;
  251. nbCandidates++;
  252. }
  253. /* find longest match */
  254. { size_t commonLengthSmaller = 0, commonLengthLarger = 0;
  255. const BYTE* const dictBase = ms->window.dictBase;
  256. const U32 dictLimit = ms->window.dictLimit;
  257. const BYTE* const dictEnd = dictBase + dictLimit;
  258. const BYTE* const prefixStart = base + dictLimit;
  259. U32* smallerPtr = bt + 2*(current&btMask);
  260. U32* largerPtr = bt + 2*(current&btMask) + 1;
  261. U32 matchEndIdx = current + 8 + 1;
  262. U32 dummy32; /* to be nullified at the end */
  263. size_t bestLength = 0;
  264. matchIndex = hashTable[h];
  265. hashTable[h] = current; /* Update Hash Table */
  266. while (nbCompares-- && (matchIndex > windowLow)) {
  267. U32* const nextPtr = bt + 2*(matchIndex & btMask);
  268. size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
  269. const BYTE* match;
  270. if ((dictMode != ZSTD_extDict) || (matchIndex+matchLength >= dictLimit)) {
  271. match = base + matchIndex;
  272. matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend);
  273. } else {
  274. match = dictBase + matchIndex;
  275. matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart);
  276. if (matchIndex+matchLength >= dictLimit)
  277. match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
  278. }
  279. if (matchLength > bestLength) {
  280. if (matchLength > matchEndIdx - matchIndex)
  281. matchEndIdx = matchIndex + (U32)matchLength;
  282. if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(current-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) )
  283. bestLength = matchLength, *offsetPtr = ZSTD_REP_MOVE + current - matchIndex;
  284. if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */
  285. if (dictMode == ZSTD_dictMatchState) {
  286. nbCompares = 0; /* in addition to avoiding checking any
  287. * further in this loop, make sure we
  288. * skip checking in the dictionary. */
  289. }
  290. break; /* drop, to guarantee consistency (miss a little bit of compression) */
  291. }
  292. }
  293. if (match[matchLength] < ip[matchLength]) {
  294. /* match is smaller than current */
  295. *smallerPtr = matchIndex; /* update smaller idx */
  296. commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
  297. if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */
  298. smallerPtr = nextPtr+1; /* new "smaller" => larger of match */
  299. matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */
  300. } else {
  301. /* match is larger than current */
  302. *largerPtr = matchIndex;
  303. commonLengthLarger = matchLength;
  304. if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */
  305. largerPtr = nextPtr;
  306. matchIndex = nextPtr[0];
  307. } }
  308. *smallerPtr = *largerPtr = 0;
  309. if (dictMode == ZSTD_dictMatchState && nbCompares) {
  310. bestLength = ZSTD_DUBT_findBetterDictMatch(
  311. ms, ip, iend,
  312. offsetPtr, bestLength, nbCompares,
  313. mls, dictMode);
  314. }
  315. assert(matchEndIdx > current+8); /* ensure nextToUpdate is increased */
  316. ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */
  317. if (bestLength >= MINMATCH) {
  318. U32 const mIndex = current - ((U32)*offsetPtr - ZSTD_REP_MOVE); (void)mIndex;
  319. DEBUGLOG(8, "ZSTD_DUBT_findBestMatch(%u) : found match of length %u and offsetCode %u (pos %u)",
  320. current, (U32)bestLength, (U32)*offsetPtr, mIndex);
  321. }
  322. return bestLength;
  323. }
  324. }
  325. /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */
  326. FORCE_INLINE_TEMPLATE size_t
  327. ZSTD_BtFindBestMatch( ZSTD_matchState_t* ms,
  328. const BYTE* const ip, const BYTE* const iLimit,
  329. size_t* offsetPtr,
  330. const U32 mls /* template */,
  331. const ZSTD_dictMode_e dictMode)
  332. {
  333. DEBUGLOG(7, "ZSTD_BtFindBestMatch");
  334. if (ip < ms->window.base + ms->nextToUpdate) return 0; /* skipped area */
  335. ZSTD_updateDUBT(ms, ip, iLimit, mls);
  336. return ZSTD_DUBT_findBestMatch(ms, ip, iLimit, offsetPtr, mls, dictMode);
  337. }
  338. static size_t
  339. ZSTD_BtFindBestMatch_selectMLS ( ZSTD_matchState_t* ms,
  340. const BYTE* ip, const BYTE* const iLimit,
  341. size_t* offsetPtr)
  342. {
  343. switch(ms->cParams.minMatch)
  344. {
  345. default : /* includes case 3 */
  346. case 4 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_noDict);
  347. case 5 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_noDict);
  348. case 7 :
  349. case 6 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_noDict);
  350. }
  351. }
  352. static size_t ZSTD_BtFindBestMatch_dictMatchState_selectMLS (
  353. ZSTD_matchState_t* ms,
  354. const BYTE* ip, const BYTE* const iLimit,
  355. size_t* offsetPtr)
  356. {
  357. switch(ms->cParams.minMatch)
  358. {
  359. default : /* includes case 3 */
  360. case 4 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMatchState);
  361. case 5 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMatchState);
  362. case 7 :
  363. case 6 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMatchState);
  364. }
  365. }
  366. static size_t ZSTD_BtFindBestMatch_extDict_selectMLS (
  367. ZSTD_matchState_t* ms,
  368. const BYTE* ip, const BYTE* const iLimit,
  369. size_t* offsetPtr)
  370. {
  371. switch(ms->cParams.minMatch)
  372. {
  373. default : /* includes case 3 */
  374. case 4 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_extDict);
  375. case 5 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_extDict);
  376. case 7 :
  377. case 6 : return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_extDict);
  378. }
  379. }
  380. /* *********************************
  381. * Hash Chain
  382. ***********************************/
  383. #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & (mask)]
  384. /* Update chains up to ip (excluded)
  385. Assumption : always within prefix (i.e. not within extDict) */
  386. static U32 ZSTD_insertAndFindFirstIndex_internal(
  387. ZSTD_matchState_t* ms,
  388. const ZSTD_compressionParameters* const cParams,
  389. const BYTE* ip, U32 const mls)
  390. {
  391. U32* const hashTable = ms->hashTable;
  392. const U32 hashLog = cParams->hashLog;
  393. U32* const chainTable = ms->chainTable;
  394. const U32 chainMask = (1 << cParams->chainLog) - 1;
  395. const BYTE* const base = ms->window.base;
  396. const U32 target = (U32)(ip - base);
  397. U32 idx = ms->nextToUpdate;
  398. while(idx < target) { /* catch up */
  399. size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls);
  400. NEXT_IN_CHAIN(idx, chainMask) = hashTable[h];
  401. hashTable[h] = idx;
  402. idx++;
  403. }
  404. ms->nextToUpdate = target;
  405. return hashTable[ZSTD_hashPtr(ip, hashLog, mls)];
  406. }
  407. U32 ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t* ms, const BYTE* ip) {
  408. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  409. return ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, ms->cParams.minMatch);
  410. }
  411. /* inlining is important to hardwire a hot branch (template emulation) */
  412. FORCE_INLINE_TEMPLATE
  413. size_t ZSTD_HcFindBestMatch_generic (
  414. ZSTD_matchState_t* ms,
  415. const BYTE* const ip, const BYTE* const iLimit,
  416. size_t* offsetPtr,
  417. const U32 mls, const ZSTD_dictMode_e dictMode)
  418. {
  419. const ZSTD_compressionParameters* const cParams = &ms->cParams;
  420. U32* const chainTable = ms->chainTable;
  421. const U32 chainSize = (1 << cParams->chainLog);
  422. const U32 chainMask = chainSize-1;
  423. const BYTE* const base = ms->window.base;
  424. const BYTE* const dictBase = ms->window.dictBase;
  425. const U32 dictLimit = ms->window.dictLimit;
  426. const BYTE* const prefixStart = base + dictLimit;
  427. const BYTE* const dictEnd = dictBase + dictLimit;
  428. const U32 lowLimit = ms->window.lowLimit;
  429. const U32 current = (U32)(ip-base);
  430. const U32 minChain = current > chainSize ? current - chainSize : 0;
  431. U32 nbAttempts = 1U << cParams->searchLog;
  432. size_t ml=4-1;
  433. /* HC4 match finder */
  434. U32 matchIndex = ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, mls);
  435. for ( ; (matchIndex>lowLimit) & (nbAttempts>0) ; nbAttempts--) {
  436. size_t currentMl=0;
  437. if ((dictMode != ZSTD_extDict) || matchIndex >= dictLimit) {
  438. const BYTE* const match = base + matchIndex;
  439. assert(matchIndex >= dictLimit); /* ensures this is true if dictMode != ZSTD_extDict */
  440. if (match[ml] == ip[ml]) /* potentially better */
  441. currentMl = ZSTD_count(ip, match, iLimit);
  442. } else {
  443. const BYTE* const match = dictBase + matchIndex;
  444. assert(match+4 <= dictEnd);
  445. if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
  446. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4;
  447. }
  448. /* save best solution */
  449. if (currentMl > ml) {
  450. ml = currentMl;
  451. *offsetPtr = current - matchIndex + ZSTD_REP_MOVE;
  452. if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
  453. }
  454. if (matchIndex <= minChain) break;
  455. matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask);
  456. }
  457. if (dictMode == ZSTD_dictMatchState) {
  458. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  459. const U32* const dmsChainTable = dms->chainTable;
  460. const U32 dmsChainSize = (1 << dms->cParams.chainLog);
  461. const U32 dmsChainMask = dmsChainSize - 1;
  462. const U32 dmsLowestIndex = dms->window.dictLimit;
  463. const BYTE* const dmsBase = dms->window.base;
  464. const BYTE* const dmsEnd = dms->window.nextSrc;
  465. const U32 dmsSize = (U32)(dmsEnd - dmsBase);
  466. const U32 dmsIndexDelta = dictLimit - dmsSize;
  467. const U32 dmsMinChain = dmsSize > dmsChainSize ? dmsSize - dmsChainSize : 0;
  468. matchIndex = dms->hashTable[ZSTD_hashPtr(ip, dms->cParams.hashLog, mls)];
  469. for ( ; (matchIndex>dmsLowestIndex) & (nbAttempts>0) ; nbAttempts--) {
  470. size_t currentMl=0;
  471. const BYTE* const match = dmsBase + matchIndex;
  472. assert(match+4 <= dmsEnd);
  473. if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
  474. currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dmsEnd, prefixStart) + 4;
  475. /* save best solution */
  476. if (currentMl > ml) {
  477. ml = currentMl;
  478. *offsetPtr = current - (matchIndex + dmsIndexDelta) + ZSTD_REP_MOVE;
  479. if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */
  480. }
  481. if (matchIndex <= dmsMinChain) break;
  482. matchIndex = dmsChainTable[matchIndex & dmsChainMask];
  483. }
  484. }
  485. return ml;
  486. }
  487. FORCE_INLINE_TEMPLATE size_t ZSTD_HcFindBestMatch_selectMLS (
  488. ZSTD_matchState_t* ms,
  489. const BYTE* ip, const BYTE* const iLimit,
  490. size_t* offsetPtr)
  491. {
  492. switch(ms->cParams.minMatch)
  493. {
  494. default : /* includes case 3 */
  495. case 4 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 4, ZSTD_noDict);
  496. case 5 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 5, ZSTD_noDict);
  497. case 7 :
  498. case 6 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 6, ZSTD_noDict);
  499. }
  500. }
  501. static size_t ZSTD_HcFindBestMatch_dictMatchState_selectMLS (
  502. ZSTD_matchState_t* ms,
  503. const BYTE* ip, const BYTE* const iLimit,
  504. size_t* offsetPtr)
  505. {
  506. switch(ms->cParams.minMatch)
  507. {
  508. default : /* includes case 3 */
  509. case 4 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMatchState);
  510. case 5 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMatchState);
  511. case 7 :
  512. case 6 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMatchState);
  513. }
  514. }
  515. FORCE_INLINE_TEMPLATE size_t ZSTD_HcFindBestMatch_extDict_selectMLS (
  516. ZSTD_matchState_t* ms,
  517. const BYTE* ip, const BYTE* const iLimit,
  518. size_t* offsetPtr)
  519. {
  520. switch(ms->cParams.minMatch)
  521. {
  522. default : /* includes case 3 */
  523. case 4 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 4, ZSTD_extDict);
  524. case 5 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 5, ZSTD_extDict);
  525. case 7 :
  526. case 6 : return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, 6, ZSTD_extDict);
  527. }
  528. }
  529. /* *******************************
  530. * Common parser - lazy strategy
  531. *********************************/
  532. FORCE_INLINE_TEMPLATE
  533. size_t ZSTD_compressBlock_lazy_generic(
  534. ZSTD_matchState_t* ms, seqStore_t* seqStore,
  535. U32 rep[ZSTD_REP_NUM],
  536. const void* src, size_t srcSize,
  537. const U32 searchMethod, const U32 depth,
  538. ZSTD_dictMode_e const dictMode)
  539. {
  540. const BYTE* const istart = (const BYTE*)src;
  541. const BYTE* ip = istart;
  542. const BYTE* anchor = istart;
  543. const BYTE* const iend = istart + srcSize;
  544. const BYTE* const ilimit = iend - 8;
  545. const BYTE* const base = ms->window.base;
  546. const U32 prefixLowestIndex = ms->window.dictLimit;
  547. const BYTE* const prefixLowest = base + prefixLowestIndex;
  548. typedef size_t (*searchMax_f)(
  549. ZSTD_matchState_t* ms,
  550. const BYTE* ip, const BYTE* iLimit, size_t* offsetPtr);
  551. searchMax_f const searchMax = dictMode == ZSTD_dictMatchState ?
  552. (searchMethod ? ZSTD_BtFindBestMatch_dictMatchState_selectMLS : ZSTD_HcFindBestMatch_dictMatchState_selectMLS) :
  553. (searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS);
  554. U32 offset_1 = rep[0], offset_2 = rep[1], savedOffset=0;
  555. const ZSTD_matchState_t* const dms = ms->dictMatchState;
  556. const U32 dictLowestIndex = dictMode == ZSTD_dictMatchState ?
  557. dms->window.dictLimit : 0;
  558. const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ?
  559. dms->window.base : NULL;
  560. const BYTE* const dictLowest = dictMode == ZSTD_dictMatchState ?
  561. dictBase + dictLowestIndex : NULL;
  562. const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ?
  563. dms->window.nextSrc : NULL;
  564. const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ?
  565. prefixLowestIndex - (U32)(dictEnd - dictBase) :
  566. 0;
  567. const U32 dictAndPrefixLength = (U32)(ip - prefixLowest + dictEnd - dictLowest);
  568. /* init */
  569. ip += (dictAndPrefixLength == 0);
  570. ms->nextToUpdate3 = ms->nextToUpdate;
  571. if (dictMode == ZSTD_noDict) {
  572. U32 const maxRep = (U32)(ip - prefixLowest);
  573. if (offset_2 > maxRep) savedOffset = offset_2, offset_2 = 0;
  574. if (offset_1 > maxRep) savedOffset = offset_1, offset_1 = 0;
  575. }
  576. if (dictMode == ZSTD_dictMatchState) {
  577. /* dictMatchState repCode checks don't currently handle repCode == 0
  578. * disabling. */
  579. assert(offset_1 <= dictAndPrefixLength);
  580. assert(offset_2 <= dictAndPrefixLength);
  581. }
  582. /* Match Loop */
  583. while (ip < ilimit) {
  584. size_t matchLength=0;
  585. size_t offset=0;
  586. const BYTE* start=ip+1;
  587. /* check repCode */
  588. if (dictMode == ZSTD_dictMatchState) {
  589. const U32 repIndex = (U32)(ip - base) + 1 - offset_1;
  590. const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
  591. && repIndex < prefixLowestIndex) ?
  592. dictBase + (repIndex - dictIndexDelta) :
  593. base + repIndex;
  594. if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  595. && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
  596. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  597. matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  598. if (depth==0) goto _storeSequence;
  599. }
  600. }
  601. if ( dictMode == ZSTD_noDict
  602. && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
  603. matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
  604. if (depth==0) goto _storeSequence;
  605. }
  606. /* first search (depth 0) */
  607. { size_t offsetFound = 999999999;
  608. size_t const ml2 = searchMax(ms, ip, iend, &offsetFound);
  609. if (ml2 > matchLength)
  610. matchLength = ml2, start = ip, offset=offsetFound;
  611. }
  612. if (matchLength < 4) {
  613. ip += ((ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */
  614. continue;
  615. }
  616. /* let's try to find a better solution */
  617. if (depth>=1)
  618. while (ip<ilimit) {
  619. ip ++;
  620. if ( (dictMode == ZSTD_noDict)
  621. && (offset) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
  622. size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
  623. int const gain2 = (int)(mlRep * 3);
  624. int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1);
  625. if ((mlRep >= 4) && (gain2 > gain1))
  626. matchLength = mlRep, offset = 0, start = ip;
  627. }
  628. if (dictMode == ZSTD_dictMatchState) {
  629. const U32 repIndex = (U32)(ip - base) - offset_1;
  630. const BYTE* repMatch = repIndex < prefixLowestIndex ?
  631. dictBase + (repIndex - dictIndexDelta) :
  632. base + repIndex;
  633. if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  634. && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
  635. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  636. size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  637. int const gain2 = (int)(mlRep * 3);
  638. int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1);
  639. if ((mlRep >= 4) && (gain2 > gain1))
  640. matchLength = mlRep, offset = 0, start = ip;
  641. }
  642. }
  643. { size_t offset2=999999999;
  644. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  645. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */
  646. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4);
  647. if ((ml2 >= 4) && (gain2 > gain1)) {
  648. matchLength = ml2, offset = offset2, start = ip;
  649. continue; /* search a better one */
  650. } }
  651. /* let's find an even better one */
  652. if ((depth==2) && (ip<ilimit)) {
  653. ip ++;
  654. if ( (dictMode == ZSTD_noDict)
  655. && (offset) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) {
  656. size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4;
  657. int const gain2 = (int)(mlRep * 4);
  658. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1);
  659. if ((mlRep >= 4) && (gain2 > gain1))
  660. matchLength = mlRep, offset = 0, start = ip;
  661. }
  662. if (dictMode == ZSTD_dictMatchState) {
  663. const U32 repIndex = (U32)(ip - base) - offset_1;
  664. const BYTE* repMatch = repIndex < prefixLowestIndex ?
  665. dictBase + (repIndex - dictIndexDelta) :
  666. base + repIndex;
  667. if (((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
  668. && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
  669. const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
  670. size_t const mlRep = ZSTD_count_2segments(ip+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
  671. int const gain2 = (int)(mlRep * 4);
  672. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1);
  673. if ((mlRep >= 4) && (gain2 > gain1))
  674. matchLength = mlRep, offset = 0, start = ip;
  675. }
  676. }
  677. { size_t offset2=999999999;
  678. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  679. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */
  680. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7);
  681. if ((ml2 >= 4) && (gain2 > gain1)) {
  682. matchLength = ml2, offset = offset2, start = ip;
  683. continue;
  684. } } }
  685. break; /* nothing found : store previous solution */
  686. }
  687. /* NOTE:
  688. * start[-offset+ZSTD_REP_MOVE-1] is undefined behavior.
  689. * (-offset+ZSTD_REP_MOVE-1) is unsigned, and is added to start, which
  690. * overflows the pointer, which is undefined behavior.
  691. */
  692. /* catch up */
  693. if (offset) {
  694. if (dictMode == ZSTD_noDict) {
  695. while ( ((start > anchor) & (start - (offset-ZSTD_REP_MOVE) > prefixLowest))
  696. && (start[-1] == (start-(offset-ZSTD_REP_MOVE))[-1]) ) /* only search for offset within prefix */
  697. { start--; matchLength++; }
  698. }
  699. if (dictMode == ZSTD_dictMatchState) {
  700. U32 const matchIndex = (U32)((start-base) - (offset - ZSTD_REP_MOVE));
  701. const BYTE* match = (matchIndex < prefixLowestIndex) ? dictBase + matchIndex - dictIndexDelta : base + matchIndex;
  702. const BYTE* const mStart = (matchIndex < prefixLowestIndex) ? dictLowest : prefixLowest;
  703. while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
  704. }
  705. offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE);
  706. }
  707. /* store sequence */
  708. _storeSequence:
  709. { size_t const litLength = start - anchor;
  710. ZSTD_storeSeq(seqStore, litLength, anchor, (U32)offset, matchLength-MINMATCH);
  711. anchor = ip = start + matchLength;
  712. }
  713. /* check immediate repcode */
  714. if (dictMode == ZSTD_dictMatchState) {
  715. while (ip <= ilimit) {
  716. U32 const current2 = (U32)(ip-base);
  717. U32 const repIndex = current2 - offset_2;
  718. const BYTE* repMatch = dictMode == ZSTD_dictMatchState
  719. && repIndex < prefixLowestIndex ?
  720. dictBase - dictIndexDelta + repIndex :
  721. base + repIndex;
  722. if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex) >= 3 /* intentional overflow */)
  723. && (MEM_read32(repMatch) == MEM_read32(ip)) ) {
  724. const BYTE* const repEnd2 = repIndex < prefixLowestIndex ? dictEnd : iend;
  725. matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd2, prefixLowest) + 4;
  726. offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap offset_2 <=> offset_1 */
  727. ZSTD_storeSeq(seqStore, 0, anchor, 0, matchLength-MINMATCH);
  728. ip += matchLength;
  729. anchor = ip;
  730. continue;
  731. }
  732. break;
  733. }
  734. }
  735. if (dictMode == ZSTD_noDict) {
  736. while ( ((ip <= ilimit) & (offset_2>0))
  737. && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) {
  738. /* store sequence */
  739. matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
  740. offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap repcodes */
  741. ZSTD_storeSeq(seqStore, 0, anchor, 0, matchLength-MINMATCH);
  742. ip += matchLength;
  743. anchor = ip;
  744. continue; /* faster when present ... (?) */
  745. } } }
  746. /* Save reps for next block */
  747. rep[0] = offset_1 ? offset_1 : savedOffset;
  748. rep[1] = offset_2 ? offset_2 : savedOffset;
  749. /* Return the last literals size */
  750. return iend - anchor;
  751. }
  752. size_t ZSTD_compressBlock_btlazy2(
  753. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  754. void const* src, size_t srcSize)
  755. {
  756. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 1, 2, ZSTD_noDict);
  757. }
  758. size_t ZSTD_compressBlock_lazy2(
  759. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  760. void const* src, size_t srcSize)
  761. {
  762. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 0, 2, ZSTD_noDict);
  763. }
  764. size_t ZSTD_compressBlock_lazy(
  765. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  766. void const* src, size_t srcSize)
  767. {
  768. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 0, 1, ZSTD_noDict);
  769. }
  770. size_t ZSTD_compressBlock_greedy(
  771. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  772. void const* src, size_t srcSize)
  773. {
  774. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 0, 0, ZSTD_noDict);
  775. }
  776. size_t ZSTD_compressBlock_btlazy2_dictMatchState(
  777. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  778. void const* src, size_t srcSize)
  779. {
  780. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 1, 2, ZSTD_dictMatchState);
  781. }
  782. size_t ZSTD_compressBlock_lazy2_dictMatchState(
  783. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  784. void const* src, size_t srcSize)
  785. {
  786. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 0, 2, ZSTD_dictMatchState);
  787. }
  788. size_t ZSTD_compressBlock_lazy_dictMatchState(
  789. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  790. void const* src, size_t srcSize)
  791. {
  792. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 0, 1, ZSTD_dictMatchState);
  793. }
  794. size_t ZSTD_compressBlock_greedy_dictMatchState(
  795. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  796. void const* src, size_t srcSize)
  797. {
  798. return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, 0, 0, ZSTD_dictMatchState);
  799. }
  800. FORCE_INLINE_TEMPLATE
  801. size_t ZSTD_compressBlock_lazy_extDict_generic(
  802. ZSTD_matchState_t* ms, seqStore_t* seqStore,
  803. U32 rep[ZSTD_REP_NUM],
  804. const void* src, size_t srcSize,
  805. const U32 searchMethod, const U32 depth)
  806. {
  807. const BYTE* const istart = (const BYTE*)src;
  808. const BYTE* ip = istart;
  809. const BYTE* anchor = istart;
  810. const BYTE* const iend = istart + srcSize;
  811. const BYTE* const ilimit = iend - 8;
  812. const BYTE* const base = ms->window.base;
  813. const U32 dictLimit = ms->window.dictLimit;
  814. const U32 lowestIndex = ms->window.lowLimit;
  815. const BYTE* const prefixStart = base + dictLimit;
  816. const BYTE* const dictBase = ms->window.dictBase;
  817. const BYTE* const dictEnd = dictBase + dictLimit;
  818. const BYTE* const dictStart = dictBase + lowestIndex;
  819. typedef size_t (*searchMax_f)(
  820. ZSTD_matchState_t* ms,
  821. const BYTE* ip, const BYTE* iLimit, size_t* offsetPtr);
  822. searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_extDict_selectMLS : ZSTD_HcFindBestMatch_extDict_selectMLS;
  823. U32 offset_1 = rep[0], offset_2 = rep[1];
  824. /* init */
  825. ms->nextToUpdate3 = ms->nextToUpdate;
  826. ip += (ip == prefixStart);
  827. /* Match Loop */
  828. while (ip < ilimit) {
  829. size_t matchLength=0;
  830. size_t offset=0;
  831. const BYTE* start=ip+1;
  832. U32 current = (U32)(ip-base);
  833. /* check repCode */
  834. { const U32 repIndex = (U32)(current+1 - offset_1);
  835. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  836. const BYTE* const repMatch = repBase + repIndex;
  837. if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
  838. if (MEM_read32(ip+1) == MEM_read32(repMatch)) {
  839. /* repcode detected we should take it */
  840. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  841. matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  842. if (depth==0) goto _storeSequence;
  843. } }
  844. /* first search (depth 0) */
  845. { size_t offsetFound = 999999999;
  846. size_t const ml2 = searchMax(ms, ip, iend, &offsetFound);
  847. if (ml2 > matchLength)
  848. matchLength = ml2, start = ip, offset=offsetFound;
  849. }
  850. if (matchLength < 4) {
  851. ip += ((ip-anchor) >> kSearchStrength) + 1; /* jump faster over incompressible sections */
  852. continue;
  853. }
  854. /* let's try to find a better solution */
  855. if (depth>=1)
  856. while (ip<ilimit) {
  857. ip ++;
  858. current++;
  859. /* check repCode */
  860. if (offset) {
  861. const U32 repIndex = (U32)(current - offset_1);
  862. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  863. const BYTE* const repMatch = repBase + repIndex;
  864. if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
  865. if (MEM_read32(ip) == MEM_read32(repMatch)) {
  866. /* repcode detected */
  867. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  868. size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  869. int const gain2 = (int)(repLength * 3);
  870. int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1);
  871. if ((repLength >= 4) && (gain2 > gain1))
  872. matchLength = repLength, offset = 0, start = ip;
  873. } }
  874. /* search match, depth 1 */
  875. { size_t offset2=999999999;
  876. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  877. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */
  878. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4);
  879. if ((ml2 >= 4) && (gain2 > gain1)) {
  880. matchLength = ml2, offset = offset2, start = ip;
  881. continue; /* search a better one */
  882. } }
  883. /* let's find an even better one */
  884. if ((depth==2) && (ip<ilimit)) {
  885. ip ++;
  886. current++;
  887. /* check repCode */
  888. if (offset) {
  889. const U32 repIndex = (U32)(current - offset_1);
  890. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  891. const BYTE* const repMatch = repBase + repIndex;
  892. if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
  893. if (MEM_read32(ip) == MEM_read32(repMatch)) {
  894. /* repcode detected */
  895. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  896. size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  897. int const gain2 = (int)(repLength * 4);
  898. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1);
  899. if ((repLength >= 4) && (gain2 > gain1))
  900. matchLength = repLength, offset = 0, start = ip;
  901. } }
  902. /* search match, depth 2 */
  903. { size_t offset2=999999999;
  904. size_t const ml2 = searchMax(ms, ip, iend, &offset2);
  905. int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */
  906. int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7);
  907. if ((ml2 >= 4) && (gain2 > gain1)) {
  908. matchLength = ml2, offset = offset2, start = ip;
  909. continue;
  910. } } }
  911. break; /* nothing found : store previous solution */
  912. }
  913. /* catch up */
  914. if (offset) {
  915. U32 const matchIndex = (U32)((start-base) - (offset - ZSTD_REP_MOVE));
  916. const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex;
  917. const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart;
  918. while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */
  919. offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE);
  920. }
  921. /* store sequence */
  922. _storeSequence:
  923. { size_t const litLength = start - anchor;
  924. ZSTD_storeSeq(seqStore, litLength, anchor, (U32)offset, matchLength-MINMATCH);
  925. anchor = ip = start + matchLength;
  926. }
  927. /* check immediate repcode */
  928. while (ip <= ilimit) {
  929. const U32 repIndex = (U32)((ip-base) - offset_2);
  930. const BYTE* const repBase = repIndex < dictLimit ? dictBase : base;
  931. const BYTE* const repMatch = repBase + repIndex;
  932. if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
  933. if (MEM_read32(ip) == MEM_read32(repMatch)) {
  934. /* repcode detected we should take it */
  935. const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend;
  936. matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4;
  937. offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap offset history */
  938. ZSTD_storeSeq(seqStore, 0, anchor, 0, matchLength-MINMATCH);
  939. ip += matchLength;
  940. anchor = ip;
  941. continue; /* faster when present ... (?) */
  942. }
  943. break;
  944. } }
  945. /* Save reps for next block */
  946. rep[0] = offset_1;
  947. rep[1] = offset_2;
  948. /* Return the last literals size */
  949. return iend - anchor;
  950. }
  951. size_t ZSTD_compressBlock_greedy_extDict(
  952. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  953. void const* src, size_t srcSize)
  954. {
  955. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, 0, 0);
  956. }
  957. size_t ZSTD_compressBlock_lazy_extDict(
  958. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  959. void const* src, size_t srcSize)
  960. {
  961. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, 0, 1);
  962. }
  963. size_t ZSTD_compressBlock_lazy2_extDict(
  964. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  965. void const* src, size_t srcSize)
  966. {
  967. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, 0, 2);
  968. }
  969. size_t ZSTD_compressBlock_btlazy2_extDict(
  970. ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
  971. void const* src, size_t srcSize)
  972. {
  973. return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, 1, 2);
  974. }